blob: c37a656802b0f5da26b7bf800f46982834c039d0 [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
Eric Dumazetfa3536c2006-03-26 01:37:24 -080038int sysctl_vfs_cache_pressure __read_mostly = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039EXPORT_SYMBOL_GPL(sysctl_vfs_cache_pressure);
40
41 __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_lock);
Al Viro74c3cbe2007-07-22 08:04:18 -040042__cacheline_aligned_in_smp DEFINE_SEQLOCK(rename_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44EXPORT_SYMBOL(dcache_lock);
45
Christoph Lametere18b8902006-12-06 20:33:20 -080046static struct kmem_cache *dentry_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48#define DNAME_INLINE_LEN (sizeof(struct dentry)-offsetof(struct dentry,d_iname))
49
50/*
51 * This is the single most critical data structure when it comes
52 * to the dcache: the hashtable for lookups. Somebody should try
53 * to make this good - I've just made it work.
54 *
55 * This hash-function tries to avoid losing too many bits of hash
56 * information, yet avoid using a prime hash-size or similar.
57 */
58#define D_HASHBITS d_hash_shift
59#define D_HASHMASK d_hash_mask
60
Eric Dumazetfa3536c2006-03-26 01:37:24 -080061static unsigned int d_hash_mask __read_mostly;
62static unsigned int d_hash_shift __read_mostly;
63static struct hlist_head *dentry_hashtable __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65/* Statistics gathering. */
66struct dentry_stat_t dentry_stat = {
67 .age_limit = 45,
68};
69
Christoph Hellwig312d3ca2010-10-10 05:36:23 -040070static struct percpu_counter nr_dentry __cacheline_aligned_in_smp;
71static struct percpu_counter nr_dentry_unused __cacheline_aligned_in_smp;
72
73#if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS)
74int proc_nr_dentry(ctl_table *table, int write, void __user *buffer,
75 size_t *lenp, loff_t *ppos)
76{
77 dentry_stat.nr_dentry = percpu_counter_sum_positive(&nr_dentry);
78 dentry_stat.nr_unused = percpu_counter_sum_positive(&nr_dentry_unused);
79 return proc_dointvec(table, write, buffer, lenp, ppos);
80}
81#endif
82
Christoph Hellwig9c82ab92010-10-10 05:36:22 -040083static void __d_free(struct rcu_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
Christoph Hellwig9c82ab92010-10-10 05:36:22 -040085 struct dentry *dentry = container_of(head, struct dentry, d_u.d_rcu);
86
Arjan van de Venfd217f42008-10-21 06:47:33 -070087 WARN_ON(!list_empty(&dentry->d_alias));
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 if (dname_external(dentry))
89 kfree(dentry->d_name.name);
90 kmem_cache_free(dentry_cache, dentry);
91}
92
93/*
Christoph Hellwig312d3ca2010-10-10 05:36:23 -040094 * no dcache_lock, please.
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 */
96static void d_free(struct dentry *dentry)
97{
Christoph Hellwig312d3ca2010-10-10 05:36:23 -040098 percpu_counter_dec(&nr_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 if (dentry->d_op && dentry->d_op->d_release)
100 dentry->d_op->d_release(dentry);
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400101
Eric Dumazetb3423412006-12-06 20:38:48 -0800102 /* if dentry was never inserted into hash, immediate free is OK */
Akinobu Mitae8462ca2008-02-06 01:37:07 -0800103 if (hlist_unhashed(&dentry->d_hash))
Christoph Hellwig9c82ab92010-10-10 05:36:22 -0400104 __d_free(&dentry->d_u.d_rcu);
Eric Dumazetb3423412006-12-06 20:38:48 -0800105 else
Christoph Hellwig9c82ab92010-10-10 05:36:22 -0400106 call_rcu(&dentry->d_u.d_rcu, __d_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
109/*
110 * Release the dentry's inode, using the filesystem
111 * d_iput() operation if defined.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800113static void dentry_iput(struct dentry * dentry)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200114 __releases(dentry->d_lock)
115 __releases(dcache_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
117 struct inode *inode = dentry->d_inode;
118 if (inode) {
119 dentry->d_inode = NULL;
120 list_del_init(&dentry->d_alias);
121 spin_unlock(&dentry->d_lock);
122 spin_unlock(&dcache_lock);
Linus Torvaldsf805fbd2005-09-19 19:54:29 -0700123 if (!inode->i_nlink)
124 fsnotify_inoderemove(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 if (dentry->d_op && dentry->d_op->d_iput)
126 dentry->d_op->d_iput(dentry, inode);
127 else
128 iput(inode);
129 } else {
130 spin_unlock(&dentry->d_lock);
131 spin_unlock(&dcache_lock);
132 }
133}
134
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700135/*
136 * dentry_lru_(add|add_tail|del|del_init) must be called with dcache_lock held.
137 */
138static void dentry_lru_add(struct dentry *dentry)
139{
140 list_add(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
141 dentry->d_sb->s_nr_dentry_unused++;
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400142 percpu_counter_inc(&nr_dentry_unused);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700143}
144
145static void dentry_lru_add_tail(struct dentry *dentry)
146{
147 list_add_tail(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
148 dentry->d_sb->s_nr_dentry_unused++;
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400149 percpu_counter_inc(&nr_dentry_unused);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700150}
151
152static void dentry_lru_del(struct dentry *dentry)
153{
154 if (!list_empty(&dentry->d_lru)) {
155 list_del(&dentry->d_lru);
156 dentry->d_sb->s_nr_dentry_unused--;
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400157 percpu_counter_dec(&nr_dentry_unused);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700158 }
159}
160
161static void dentry_lru_del_init(struct dentry *dentry)
162{
163 if (likely(!list_empty(&dentry->d_lru))) {
164 list_del_init(&dentry->d_lru);
165 dentry->d_sb->s_nr_dentry_unused--;
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400166 percpu_counter_dec(&nr_dentry_unused);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700167 }
168}
169
Miklos Szeredid52b9082007-05-08 00:23:46 -0700170/**
171 * d_kill - kill dentry and return parent
172 * @dentry: dentry to kill
173 *
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200174 * The dentry must already be unhashed and removed from the LRU.
Miklos Szeredid52b9082007-05-08 00:23:46 -0700175 *
176 * If this is the root of the dentry tree, return NULL.
177 */
178static struct dentry *d_kill(struct dentry *dentry)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200179 __releases(dentry->d_lock)
180 __releases(dcache_lock)
Miklos Szeredid52b9082007-05-08 00:23:46 -0700181{
182 struct dentry *parent;
183
184 list_del(&dentry->d_u.d_child);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700185 /*drops the locks, at that point nobody can reach this dentry */
186 dentry_iput(dentry);
OGAWA Hirofumi871c0062008-10-16 07:50:27 +0900187 if (IS_ROOT(dentry))
188 parent = NULL;
189 else
190 parent = dentry->d_parent;
Miklos Szeredid52b9082007-05-08 00:23:46 -0700191 d_free(dentry);
OGAWA Hirofumi871c0062008-10-16 07:50:27 +0900192 return parent;
Miklos Szeredid52b9082007-05-08 00:23:46 -0700193}
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195/*
196 * This is dput
197 *
198 * This is complicated by the fact that we do not want to put
199 * dentries that are no longer on any hash chain on the unused
200 * list: we'd much rather just get rid of them immediately.
201 *
202 * However, that implies that we have to traverse the dentry
203 * tree upwards to the parents which might _also_ now be
204 * scheduled for deletion (it may have been only waiting for
205 * its last child to go away).
206 *
207 * This tail recursion is done by hand as we don't want to depend
208 * on the compiler to always get this right (gcc generally doesn't).
209 * Real recursion would eat up our stack space.
210 */
211
212/*
213 * dput - release a dentry
214 * @dentry: dentry to release
215 *
216 * Release a dentry. This will drop the usage count and if appropriate
217 * call the dentry unlink method as well as removing it from the queues and
218 * releasing its resources. If the parent dentries were scheduled for release
219 * they too may now get deleted.
220 *
221 * no dcache lock, please.
222 */
223
224void dput(struct dentry *dentry)
225{
226 if (!dentry)
227 return;
228
229repeat:
230 if (atomic_read(&dentry->d_count) == 1)
231 might_sleep();
232 if (!atomic_dec_and_lock(&dentry->d_count, &dcache_lock))
233 return;
234
235 spin_lock(&dentry->d_lock);
236 if (atomic_read(&dentry->d_count)) {
237 spin_unlock(&dentry->d_lock);
238 spin_unlock(&dcache_lock);
239 return;
240 }
241
242 /*
243 * AV: ->d_delete() is _NOT_ allowed to block now.
244 */
245 if (dentry->d_op && dentry->d_op->d_delete) {
246 if (dentry->d_op->d_delete(dentry))
247 goto unhash_it;
248 }
249 /* Unreachable? Get rid of it */
250 if (d_unhashed(dentry))
251 goto kill_it;
252 if (list_empty(&dentry->d_lru)) {
253 dentry->d_flags |= DCACHE_REFERENCED;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700254 dentry_lru_add(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 }
256 spin_unlock(&dentry->d_lock);
257 spin_unlock(&dcache_lock);
258 return;
259
260unhash_it:
261 __d_drop(dentry);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700262kill_it:
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700263 /* if dentry was on the d_lru list delete it from there */
264 dentry_lru_del(dentry);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700265 dentry = d_kill(dentry);
266 if (dentry)
267 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700269EXPORT_SYMBOL(dput);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271/**
272 * d_invalidate - invalidate a dentry
273 * @dentry: dentry to invalidate
274 *
275 * Try to invalidate the dentry if it turns out to be
276 * possible. If there are other dentries that can be
277 * reached through this one we can't delete it and we
278 * return -EBUSY. On success we return 0.
279 *
280 * no dcache lock.
281 */
282
283int d_invalidate(struct dentry * dentry)
284{
285 /*
286 * If it's already been dropped, return OK.
287 */
288 spin_lock(&dcache_lock);
289 if (d_unhashed(dentry)) {
290 spin_unlock(&dcache_lock);
291 return 0;
292 }
293 /*
294 * Check whether to do a partial shrink_dcache
295 * to get rid of unused child entries.
296 */
297 if (!list_empty(&dentry->d_subdirs)) {
298 spin_unlock(&dcache_lock);
299 shrink_dcache_parent(dentry);
300 spin_lock(&dcache_lock);
301 }
302
303 /*
304 * Somebody else still using it?
305 *
306 * If it's a directory, we can't drop it
307 * for fear of somebody re-populating it
308 * with children (even though dropping it
309 * would make it unreachable from the root,
310 * we might still populate it if it was a
311 * working directory or similar).
312 */
313 spin_lock(&dentry->d_lock);
314 if (atomic_read(&dentry->d_count) > 1) {
315 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
316 spin_unlock(&dentry->d_lock);
317 spin_unlock(&dcache_lock);
318 return -EBUSY;
319 }
320 }
321
322 __d_drop(dentry);
323 spin_unlock(&dentry->d_lock);
324 spin_unlock(&dcache_lock);
325 return 0;
326}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700327EXPORT_SYMBOL(d_invalidate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329/* This should be called _only_ with dcache_lock held */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330static inline struct dentry * __dget_locked(struct dentry *dentry)
331{
332 atomic_inc(&dentry->d_count);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700333 dentry_lru_del_init(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 return dentry;
335}
336
337struct dentry * dget_locked(struct dentry *dentry)
338{
339 return __dget_locked(dentry);
340}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700341EXPORT_SYMBOL(dget_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343/**
344 * d_find_alias - grab a hashed alias of inode
345 * @inode: inode in question
346 * @want_discon: flag, used by d_splice_alias, to request
347 * that only a DISCONNECTED alias be returned.
348 *
349 * If inode has a hashed alias, or is a directory and has any alias,
350 * acquire the reference to alias and return it. Otherwise return NULL.
351 * Notice that if inode is a directory there can be only one alias and
352 * it can be unhashed only if it has no children, or if it is the root
353 * of a filesystem.
354 *
NeilBrown21c0d8f2006-10-04 02:16:16 -0700355 * If the inode has an IS_ROOT, DCACHE_DISCONNECTED alias, then prefer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 * any other hashed alias over that one unless @want_discon is set,
NeilBrown21c0d8f2006-10-04 02:16:16 -0700357 * in which case only return an IS_ROOT, DCACHE_DISCONNECTED alias.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 */
359
360static struct dentry * __d_find_alias(struct inode *inode, int want_discon)
361{
362 struct list_head *head, *next, *tmp;
363 struct dentry *alias, *discon_alias=NULL;
364
365 head = &inode->i_dentry;
366 next = inode->i_dentry.next;
367 while (next != head) {
368 tmp = next;
369 next = tmp->next;
370 prefetch(next);
371 alias = list_entry(tmp, struct dentry, d_alias);
372 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
NeilBrown21c0d8f2006-10-04 02:16:16 -0700373 if (IS_ROOT(alias) &&
374 (alias->d_flags & DCACHE_DISCONNECTED))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 discon_alias = alias;
376 else if (!want_discon) {
377 __dget_locked(alias);
378 return alias;
379 }
380 }
381 }
382 if (discon_alias)
383 __dget_locked(discon_alias);
384 return discon_alias;
385}
386
387struct dentry * d_find_alias(struct inode *inode)
388{
David Howells214fda12006-03-25 03:06:36 -0800389 struct dentry *de = NULL;
390
391 if (!list_empty(&inode->i_dentry)) {
392 spin_lock(&dcache_lock);
393 de = __d_find_alias(inode, 0);
394 spin_unlock(&dcache_lock);
395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 return de;
397}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700398EXPORT_SYMBOL(d_find_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400/*
401 * Try to kill dentries associated with this inode.
402 * WARNING: you must own a reference to inode.
403 */
404void d_prune_aliases(struct inode *inode)
405{
Domen Puncer0cdca3f2005-09-10 00:27:07 -0700406 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407restart:
408 spin_lock(&dcache_lock);
Domen Puncer0cdca3f2005-09-10 00:27:07 -0700409 list_for_each_entry(dentry, &inode->i_dentry, d_alias) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 spin_lock(&dentry->d_lock);
411 if (!atomic_read(&dentry->d_count)) {
412 __dget_locked(dentry);
413 __d_drop(dentry);
414 spin_unlock(&dentry->d_lock);
415 spin_unlock(&dcache_lock);
416 dput(dentry);
417 goto restart;
418 }
419 spin_unlock(&dentry->d_lock);
420 }
421 spin_unlock(&dcache_lock);
422}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700423EXPORT_SYMBOL(d_prune_aliases);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425/*
Andrew Mortond702ccb2006-06-22 14:47:31 -0700426 * Throw away a dentry - free the inode, dput the parent. This requires that
427 * the LRU list has already been removed.
428 *
Miklos Szeredi85864e12007-10-16 23:27:09 -0700429 * Try to prune ancestors as well. This is necessary to prevent
430 * quadratic behavior of shrink_dcache_parent(), but is also expected
431 * to be beneficial in reducing dentry cache fragmentation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 */
Miklos Szeredi85864e12007-10-16 23:27:09 -0700433static void prune_one_dentry(struct dentry * dentry)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200434 __releases(dentry->d_lock)
435 __releases(dcache_lock)
436 __acquires(dcache_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 __d_drop(dentry);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700439 dentry = d_kill(dentry);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700440
441 /*
442 * Prune ancestors. Locking is simpler than in dput(),
443 * because dcache_lock needs to be taken anyway.
444 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 spin_lock(&dcache_lock);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700446 while (dentry) {
447 if (!atomic_dec_and_lock(&dentry->d_count, &dentry->d_lock))
448 return;
449
450 if (dentry->d_op && dentry->d_op->d_delete)
451 dentry->d_op->d_delete(dentry);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700452 dentry_lru_del_init(dentry);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700453 __d_drop(dentry);
454 dentry = d_kill(dentry);
455 spin_lock(&dcache_lock);
456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457}
458
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700459/*
460 * Shrink the dentry LRU on a given superblock.
461 * @sb : superblock to shrink dentry LRU.
462 * @count: If count is NULL, we prune all dentries on superblock.
463 * @flags: If flags is non-zero, we need to do special processing based on
464 * which flags are set. This means we don't need to maintain multiple
465 * similar copies of this loop.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 */
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700467static void __shrink_dcache_sb(struct super_block *sb, int *count, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700469 LIST_HEAD(referenced);
470 LIST_HEAD(tmp);
471 struct dentry *dentry;
472 int cnt = 0;
473
474 BUG_ON(!sb);
475 BUG_ON((flags & DCACHE_REFERENCED) && count == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 spin_lock(&dcache_lock);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700477 if (count != NULL)
478 /* called from prune_dcache() and shrink_dcache_parent() */
479 cnt = *count;
480restart:
481 if (count == NULL)
482 list_splice_init(&sb->s_dentry_lru, &tmp);
483 else {
484 while (!list_empty(&sb->s_dentry_lru)) {
485 dentry = list_entry(sb->s_dentry_lru.prev,
486 struct dentry, d_lru);
487 BUG_ON(dentry->d_sb != sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700489 spin_lock(&dentry->d_lock);
490 /*
491 * If we are honouring the DCACHE_REFERENCED flag and
492 * the dentry has this flag set, don't free it. Clear
493 * the flag and put it back on the LRU.
NeilBrown0feae5c2006-06-22 14:47:28 -0700494 */
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700495 if ((flags & DCACHE_REFERENCED)
496 && (dentry->d_flags & DCACHE_REFERENCED)) {
497 dentry->d_flags &= ~DCACHE_REFERENCED;
npiggin@suse.dec490d792009-04-26 20:25:53 +1000498 list_move(&dentry->d_lru, &referenced);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700499 spin_unlock(&dentry->d_lock);
500 } else {
501 list_move_tail(&dentry->d_lru, &tmp);
502 spin_unlock(&dentry->d_lock);
503 cnt--;
504 if (!cnt)
505 break;
NeilBrown0feae5c2006-06-22 14:47:28 -0700506 }
Kentaro Makitaf3c6ba92008-07-25 19:44:40 -0700507 cond_resched_lock(&dcache_lock);
NeilBrown0feae5c2006-06-22 14:47:28 -0700508 }
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700509 }
510 while (!list_empty(&tmp)) {
511 dentry = list_entry(tmp.prev, struct dentry, d_lru);
512 dentry_lru_del_init(dentry);
513 spin_lock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 /*
515 * We found an inuse dentry which was not removed from
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700516 * the LRU because of laziness during lookup. Do not free
517 * it - just keep it off the LRU list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 */
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700519 if (atomic_read(&dentry->d_count)) {
520 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 continue;
522 }
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700523 prune_one_dentry(dentry);
524 /* dentry->d_lock was dropped in prune_one_dentry() */
525 cond_resched_lock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 }
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700527 if (count == NULL && !list_empty(&sb->s_dentry_lru))
528 goto restart;
529 if (count != NULL)
530 *count = cnt;
531 if (!list_empty(&referenced))
532 list_splice(&referenced, &sb->s_dentry_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 spin_unlock(&dcache_lock);
534}
535
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700536/**
537 * prune_dcache - shrink the dcache
538 * @count: number of entries to try to free
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 *
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700540 * Shrink the dcache. This is done when we need more memory, or simply when we
541 * need to unmount something (at which point we need to unuse all dentries).
542 *
543 * This function may fail to free any resources if all the dentries are in use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 */
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700545static void prune_dcache(int count)
546{
Al Virodca33252010-07-25 02:31:46 +0400547 struct super_block *sb, *p = NULL;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700548 int w_count;
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400549 int unused = percpu_counter_sum_positive(&nr_dentry_unused);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700550 int prune_ratio;
551 int pruned;
552
553 if (unused == 0 || count == 0)
554 return;
555 spin_lock(&dcache_lock);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700556 if (count >= unused)
557 prune_ratio = 1;
558 else
559 prune_ratio = unused / count;
560 spin_lock(&sb_lock);
Al Virodca33252010-07-25 02:31:46 +0400561 list_for_each_entry(sb, &super_blocks, s_list) {
Al Viro551de6f2010-03-22 19:36:35 -0400562 if (list_empty(&sb->s_instances))
563 continue;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700564 if (sb->s_nr_dentry_unused == 0)
565 continue;
566 sb->s_count++;
567 /* Now, we reclaim unused dentrins with fairness.
568 * We reclaim them same percentage from each superblock.
569 * We calculate number of dentries to scan on this sb
570 * as follows, but the implementation is arranged to avoid
571 * overflows:
572 * number of dentries to scan on this sb =
573 * count * (number of dentries on this sb /
574 * number of dentries in the machine)
575 */
576 spin_unlock(&sb_lock);
577 if (prune_ratio != 1)
578 w_count = (sb->s_nr_dentry_unused / prune_ratio) + 1;
579 else
580 w_count = sb->s_nr_dentry_unused;
581 pruned = w_count;
582 /*
583 * We need to be sure this filesystem isn't being unmounted,
584 * otherwise we could race with generic_shutdown_super(), and
585 * end up holding a reference to an inode while the filesystem
586 * is unmounted. So we try to get s_umount, and make sure
587 * s_root isn't NULL.
588 */
589 if (down_read_trylock(&sb->s_umount)) {
590 if ((sb->s_root != NULL) &&
591 (!list_empty(&sb->s_dentry_lru))) {
592 spin_unlock(&dcache_lock);
593 __shrink_dcache_sb(sb, &w_count,
594 DCACHE_REFERENCED);
595 pruned -= w_count;
596 spin_lock(&dcache_lock);
597 }
598 up_read(&sb->s_umount);
599 }
600 spin_lock(&sb_lock);
Al Virodca33252010-07-25 02:31:46 +0400601 if (p)
602 __put_super(p);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700603 count -= pruned;
Al Virodca33252010-07-25 02:31:46 +0400604 p = sb;
Al Viro79893c12010-03-22 20:27:55 -0400605 /* more work left to do? */
606 if (count <= 0)
607 break;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700608 }
Al Virodca33252010-07-25 02:31:46 +0400609 if (p)
610 __put_super(p);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700611 spin_unlock(&sb_lock);
612 spin_unlock(&dcache_lock);
613}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615/**
616 * shrink_dcache_sb - shrink dcache for a superblock
617 * @sb: superblock
618 *
619 * Shrink the dcache for the specified super block. This
620 * is used to free the dcache before unmounting a file
621 * system
622 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623void shrink_dcache_sb(struct super_block * sb)
624{
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700625 __shrink_dcache_sb(sb, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700627EXPORT_SYMBOL(shrink_dcache_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629/*
David Howellsc636ebd2006-10-11 01:22:19 -0700630 * destroy a single subtree of dentries for unmount
631 * - see the comments on shrink_dcache_for_umount() for a description of the
632 * locking
633 */
634static void shrink_dcache_for_umount_subtree(struct dentry *dentry)
635{
636 struct dentry *parent;
David Howellsf8713572006-10-28 10:38:46 -0700637 unsigned detached = 0;
David Howellsc636ebd2006-10-11 01:22:19 -0700638
639 BUG_ON(!IS_ROOT(dentry));
640
641 /* detach this root from the system */
642 spin_lock(&dcache_lock);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700643 dentry_lru_del_init(dentry);
David Howellsc636ebd2006-10-11 01:22:19 -0700644 __d_drop(dentry);
645 spin_unlock(&dcache_lock);
646
647 for (;;) {
648 /* descend to the first leaf in the current subtree */
649 while (!list_empty(&dentry->d_subdirs)) {
650 struct dentry *loop;
651
652 /* this is a branch with children - detach all of them
653 * from the system in one go */
654 spin_lock(&dcache_lock);
655 list_for_each_entry(loop, &dentry->d_subdirs,
656 d_u.d_child) {
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700657 dentry_lru_del_init(loop);
David Howellsc636ebd2006-10-11 01:22:19 -0700658 __d_drop(loop);
659 cond_resched_lock(&dcache_lock);
660 }
661 spin_unlock(&dcache_lock);
662
663 /* move to the first child */
664 dentry = list_entry(dentry->d_subdirs.next,
665 struct dentry, d_u.d_child);
666 }
667
668 /* consume the dentries from this leaf up through its parents
669 * until we find one with children or run out altogether */
670 do {
671 struct inode *inode;
672
673 if (atomic_read(&dentry->d_count) != 0) {
674 printk(KERN_ERR
675 "BUG: Dentry %p{i=%lx,n=%s}"
676 " still in use (%d)"
677 " [unmount of %s %s]\n",
678 dentry,
679 dentry->d_inode ?
680 dentry->d_inode->i_ino : 0UL,
681 dentry->d_name.name,
682 atomic_read(&dentry->d_count),
683 dentry->d_sb->s_type->name,
684 dentry->d_sb->s_id);
685 BUG();
686 }
687
OGAWA Hirofumi871c0062008-10-16 07:50:27 +0900688 if (IS_ROOT(dentry))
David Howellsc636ebd2006-10-11 01:22:19 -0700689 parent = NULL;
OGAWA Hirofumi871c0062008-10-16 07:50:27 +0900690 else {
691 parent = dentry->d_parent;
David Howellsc636ebd2006-10-11 01:22:19 -0700692 atomic_dec(&parent->d_count);
OGAWA Hirofumi871c0062008-10-16 07:50:27 +0900693 }
David Howellsc636ebd2006-10-11 01:22:19 -0700694
695 list_del(&dentry->d_u.d_child);
David Howellsf8713572006-10-28 10:38:46 -0700696 detached++;
David Howellsc636ebd2006-10-11 01:22:19 -0700697
698 inode = dentry->d_inode;
699 if (inode) {
700 dentry->d_inode = NULL;
701 list_del_init(&dentry->d_alias);
702 if (dentry->d_op && dentry->d_op->d_iput)
703 dentry->d_op->d_iput(dentry, inode);
704 else
705 iput(inode);
706 }
707
708 d_free(dentry);
709
710 /* finished when we fall off the top of the tree,
711 * otherwise we ascend to the parent and move to the
712 * next sibling if there is one */
713 if (!parent)
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400714 return;
David Howellsc636ebd2006-10-11 01:22:19 -0700715 dentry = parent;
David Howellsc636ebd2006-10-11 01:22:19 -0700716 } while (list_empty(&dentry->d_subdirs));
717
718 dentry = list_entry(dentry->d_subdirs.next,
719 struct dentry, d_u.d_child);
720 }
721}
722
723/*
724 * destroy the dentries attached to a superblock on unmounting
725 * - we don't need to use dentry->d_lock, and only need dcache_lock when
726 * removing the dentry from the system lists and hashes because:
727 * - the superblock is detached from all mountings and open files, so the
728 * dentry trees will not be rearranged by the VFS
729 * - s_umount is write-locked, so the memory pressure shrinker will ignore
730 * any dentries belonging to this superblock that it comes across
731 * - the filesystem itself is no longer permitted to rearrange the dentries
732 * in this superblock
733 */
734void shrink_dcache_for_umount(struct super_block *sb)
735{
736 struct dentry *dentry;
737
738 if (down_read_trylock(&sb->s_umount))
739 BUG();
740
741 dentry = sb->s_root;
742 sb->s_root = NULL;
743 atomic_dec(&dentry->d_count);
744 shrink_dcache_for_umount_subtree(dentry);
745
746 while (!hlist_empty(&sb->s_anon)) {
747 dentry = hlist_entry(sb->s_anon.first, struct dentry, d_hash);
748 shrink_dcache_for_umount_subtree(dentry);
749 }
750}
751
752/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 * Search for at least 1 mount point in the dentry's subdirs.
754 * We descend to the next level whenever the d_subdirs
755 * list is non-empty and continue searching.
756 */
757
758/**
759 * have_submounts - check for mounts over a dentry
760 * @parent: dentry to check.
761 *
762 * Return true if the parent or its subdirectories contain
763 * a mount point
764 */
765
766int have_submounts(struct dentry *parent)
767{
768 struct dentry *this_parent = parent;
769 struct list_head *next;
770
771 spin_lock(&dcache_lock);
772 if (d_mountpoint(parent))
773 goto positive;
774repeat:
775 next = this_parent->d_subdirs.next;
776resume:
777 while (next != &this_parent->d_subdirs) {
778 struct list_head *tmp = next;
Eric Dumazet5160ee62006-01-08 01:03:32 -0800779 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 next = tmp->next;
781 /* Have we found a mount point ? */
782 if (d_mountpoint(dentry))
783 goto positive;
784 if (!list_empty(&dentry->d_subdirs)) {
785 this_parent = dentry;
786 goto repeat;
787 }
788 }
789 /*
790 * All done at this level ... ascend and resume the search.
791 */
792 if (this_parent != parent) {
Eric Dumazet5160ee62006-01-08 01:03:32 -0800793 next = this_parent->d_u.d_child.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 this_parent = this_parent->d_parent;
795 goto resume;
796 }
797 spin_unlock(&dcache_lock);
798 return 0; /* No mount points found in tree */
799positive:
800 spin_unlock(&dcache_lock);
801 return 1;
802}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700803EXPORT_SYMBOL(have_submounts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
805/*
806 * Search the dentry child list for the specified parent,
807 * and move any unused dentries to the end of the unused
808 * list for prune_dcache(). We descend to the next level
809 * whenever the d_subdirs list is non-empty and continue
810 * searching.
811 *
812 * It returns zero iff there are no unused children,
813 * otherwise it returns the number of children moved to
814 * the end of the unused list. This may not be the total
815 * number of unused children, because select_parent can
816 * drop the lock and return early due to latency
817 * constraints.
818 */
819static int select_parent(struct dentry * parent)
820{
821 struct dentry *this_parent = parent;
822 struct list_head *next;
823 int found = 0;
824
825 spin_lock(&dcache_lock);
826repeat:
827 next = this_parent->d_subdirs.next;
828resume:
829 while (next != &this_parent->d_subdirs) {
830 struct list_head *tmp = next;
Eric Dumazet5160ee62006-01-08 01:03:32 -0800831 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 next = tmp->next;
833
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700834 dentry_lru_del_init(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 /*
836 * move only zero ref count dentries to the end
837 * of the unused list for prune_dcache
838 */
839 if (!atomic_read(&dentry->d_count)) {
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700840 dentry_lru_add_tail(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 found++;
842 }
843
844 /*
845 * We can return to the caller if we have found some (this
846 * ensures forward progress). We'll be coming back to find
847 * the rest.
848 */
849 if (found && need_resched())
850 goto out;
851
852 /*
853 * Descend a level if the d_subdirs list is non-empty.
854 */
855 if (!list_empty(&dentry->d_subdirs)) {
856 this_parent = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 goto repeat;
858 }
859 }
860 /*
861 * All done at this level ... ascend and resume the search.
862 */
863 if (this_parent != parent) {
Eric Dumazet5160ee62006-01-08 01:03:32 -0800864 next = this_parent->d_u.d_child.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 this_parent = this_parent->d_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 goto resume;
867 }
868out:
869 spin_unlock(&dcache_lock);
870 return found;
871}
872
873/**
874 * shrink_dcache_parent - prune dcache
875 * @parent: parent of entries to prune
876 *
877 * Prune the dcache to remove unused children of the parent dentry.
878 */
879
880void shrink_dcache_parent(struct dentry * parent)
881{
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700882 struct super_block *sb = parent->d_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 int found;
884
885 while ((found = select_parent(parent)) != 0)
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700886 __shrink_dcache_sb(sb, &found, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700888EXPORT_SYMBOL(shrink_dcache_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890/*
891 * Scan `nr' dentries and return the number which remain.
892 *
893 * We need to avoid reentering the filesystem if the caller is performing a
894 * GFP_NOFS allocation attempt. One example deadlock is:
895 *
896 * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache->
897 * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->put_inode->
898 * ext2_discard_prealloc->ext2_free_blocks->lock_super->DEADLOCK.
899 *
900 * In this case we return -1 to tell the caller that we baled.
901 */
Dave Chinner7f8275d2010-07-19 14:56:17 +1000902static int shrink_dcache_memory(struct shrinker *shrink, int nr, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400904 int nr_unused;
905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 if (nr) {
907 if (!(gfp_mask & __GFP_FS))
908 return -1;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700909 prune_dcache(nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 }
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400911
912 nr_unused = percpu_counter_sum_positive(&nr_dentry_unused);
913 return (nr_unused / 100) * sysctl_vfs_cache_pressure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914}
915
Rusty Russell8e1f9362007-07-17 04:03:17 -0700916static struct shrinker dcache_shrinker = {
917 .shrink = shrink_dcache_memory,
918 .seeks = DEFAULT_SEEKS,
919};
920
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921/**
922 * d_alloc - allocate a dcache entry
923 * @parent: parent of entry to allocate
924 * @name: qstr of the name
925 *
926 * Allocates a dentry. It returns %NULL if there is insufficient memory
927 * available. On a success the dentry is returned. The name passed in is
928 * copied and the copy passed in may be reused after this call.
929 */
930
931struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
932{
933 struct dentry *dentry;
934 char *dname;
935
Mel Gormane12ba742007-10-16 01:25:52 -0700936 dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 if (!dentry)
938 return NULL;
939
940 if (name->len > DNAME_INLINE_LEN-1) {
941 dname = kmalloc(name->len + 1, GFP_KERNEL);
942 if (!dname) {
943 kmem_cache_free(dentry_cache, dentry);
944 return NULL;
945 }
946 } else {
947 dname = dentry->d_iname;
948 }
949 dentry->d_name.name = dname;
950
951 dentry->d_name.len = name->len;
952 dentry->d_name.hash = name->hash;
953 memcpy(dname, name->name, name->len);
954 dname[name->len] = 0;
955
956 atomic_set(&dentry->d_count, 1);
957 dentry->d_flags = DCACHE_UNHASHED;
958 spin_lock_init(&dentry->d_lock);
959 dentry->d_inode = NULL;
960 dentry->d_parent = NULL;
961 dentry->d_sb = NULL;
962 dentry->d_op = NULL;
963 dentry->d_fsdata = NULL;
964 dentry->d_mounted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 INIT_HLIST_NODE(&dentry->d_hash);
966 INIT_LIST_HEAD(&dentry->d_lru);
967 INIT_LIST_HEAD(&dentry->d_subdirs);
968 INIT_LIST_HEAD(&dentry->d_alias);
969
970 if (parent) {
971 dentry->d_parent = dget(parent);
972 dentry->d_sb = parent->d_sb;
973 } else {
Eric Dumazet5160ee62006-01-08 01:03:32 -0800974 INIT_LIST_HEAD(&dentry->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 }
976
977 spin_lock(&dcache_lock);
978 if (parent)
Eric Dumazet5160ee62006-01-08 01:03:32 -0800979 list_add(&dentry->d_u.d_child, &parent->d_subdirs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 spin_unlock(&dcache_lock);
981
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400982 percpu_counter_inc(&nr_dentry);
983
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 return dentry;
985}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700986EXPORT_SYMBOL(d_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
988struct dentry *d_alloc_name(struct dentry *parent, const char *name)
989{
990 struct qstr q;
991
992 q.name = name;
993 q.len = strlen(name);
994 q.hash = full_name_hash(q.name, q.len);
995 return d_alloc(parent, &q);
996}
H Hartley Sweetenef26ca92009-09-29 20:09:42 -0400997EXPORT_SYMBOL(d_alloc_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
OGAWA Hirofumi360da902008-10-16 07:50:28 +0900999/* the caller must hold dcache_lock */
1000static void __d_instantiate(struct dentry *dentry, struct inode *inode)
1001{
1002 if (inode)
1003 list_add(&dentry->d_alias, &inode->i_dentry);
1004 dentry->d_inode = inode;
1005 fsnotify_d_instantiate(dentry, inode);
1006}
1007
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008/**
1009 * d_instantiate - fill in inode information for a dentry
1010 * @entry: dentry to complete
1011 * @inode: inode to attach to this dentry
1012 *
1013 * Fill in inode information in the entry.
1014 *
1015 * This turns negative dentries into productive full members
1016 * of society.
1017 *
1018 * NOTE! This assumes that the inode count has been incremented
1019 * (or otherwise set) by the caller to indicate that it is now
1020 * in use by the dcache.
1021 */
1022
1023void d_instantiate(struct dentry *entry, struct inode * inode)
1024{
Eric Sesterhenn28133c72006-03-26 18:25:39 +02001025 BUG_ON(!list_empty(&entry->d_alias));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 spin_lock(&dcache_lock);
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001027 __d_instantiate(entry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 spin_unlock(&dcache_lock);
1029 security_d_instantiate(entry, inode);
1030}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001031EXPORT_SYMBOL(d_instantiate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
1033/**
1034 * d_instantiate_unique - instantiate a non-aliased dentry
1035 * @entry: dentry to instantiate
1036 * @inode: inode to attach to this dentry
1037 *
1038 * Fill in inode information in the entry. On success, it returns NULL.
1039 * If an unhashed alias of "entry" already exists, then we return the
Oleg Drokine866cfa2006-01-09 20:52:51 -08001040 * aliased dentry instead and drop one reference to inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 *
1042 * Note that in order to avoid conflicts with rename() etc, the caller
1043 * had better be holding the parent directory semaphore.
Oleg Drokine866cfa2006-01-09 20:52:51 -08001044 *
1045 * This also assumes that the inode count has been incremented
1046 * (or otherwise set) by the caller to indicate that it is now
1047 * in use by the dcache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 */
David Howells770bfad2006-08-22 20:06:07 -04001049static struct dentry *__d_instantiate_unique(struct dentry *entry,
1050 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051{
1052 struct dentry *alias;
1053 int len = entry->d_name.len;
1054 const char *name = entry->d_name.name;
1055 unsigned int hash = entry->d_name.hash;
1056
David Howells770bfad2006-08-22 20:06:07 -04001057 if (!inode) {
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001058 __d_instantiate(entry, NULL);
David Howells770bfad2006-08-22 20:06:07 -04001059 return NULL;
1060 }
1061
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 list_for_each_entry(alias, &inode->i_dentry, d_alias) {
1063 struct qstr *qstr = &alias->d_name;
1064
1065 if (qstr->hash != hash)
1066 continue;
1067 if (alias->d_parent != entry->d_parent)
1068 continue;
1069 if (qstr->len != len)
1070 continue;
1071 if (memcmp(qstr->name, name, len))
1072 continue;
1073 dget_locked(alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 return alias;
1075 }
David Howells770bfad2006-08-22 20:06:07 -04001076
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001077 __d_instantiate(entry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 return NULL;
1079}
David Howells770bfad2006-08-22 20:06:07 -04001080
1081struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode)
1082{
1083 struct dentry *result;
1084
1085 BUG_ON(!list_empty(&entry->d_alias));
1086
1087 spin_lock(&dcache_lock);
1088 result = __d_instantiate_unique(entry, inode);
1089 spin_unlock(&dcache_lock);
1090
1091 if (!result) {
1092 security_d_instantiate(entry, inode);
1093 return NULL;
1094 }
1095
1096 BUG_ON(!d_unhashed(result));
1097 iput(inode);
1098 return result;
1099}
1100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101EXPORT_SYMBOL(d_instantiate_unique);
1102
1103/**
1104 * d_alloc_root - allocate root dentry
1105 * @root_inode: inode to allocate the root for
1106 *
1107 * Allocate a root ("/") dentry for the inode given. The inode is
1108 * instantiated and returned. %NULL is returned if there is insufficient
1109 * memory or the inode passed is %NULL.
1110 */
1111
1112struct dentry * d_alloc_root(struct inode * root_inode)
1113{
1114 struct dentry *res = NULL;
1115
1116 if (root_inode) {
1117 static const struct qstr name = { .name = "/", .len = 1 };
1118
1119 res = d_alloc(NULL, &name);
1120 if (res) {
1121 res->d_sb = root_inode->i_sb;
1122 res->d_parent = res;
1123 d_instantiate(res, root_inode);
1124 }
1125 }
1126 return res;
1127}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001128EXPORT_SYMBOL(d_alloc_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
1130static inline struct hlist_head *d_hash(struct dentry *parent,
1131 unsigned long hash)
1132{
1133 hash += ((unsigned long) parent ^ GOLDEN_RATIO_PRIME) / L1_CACHE_BYTES;
1134 hash = hash ^ ((hash ^ GOLDEN_RATIO_PRIME) >> D_HASHBITS);
1135 return dentry_hashtable + (hash & D_HASHMASK);
1136}
1137
1138/**
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001139 * d_obtain_alias - find or allocate a dentry for a given inode
1140 * @inode: inode to allocate the dentry for
1141 *
1142 * Obtain a dentry for an inode resulting from NFS filehandle conversion or
1143 * similar open by handle operations. The returned dentry may be anonymous,
1144 * or may have a full name (if the inode was already in the cache).
1145 *
1146 * When called on a directory inode, we must ensure that the inode only ever
1147 * has one dentry. If a dentry is found, that is returned instead of
1148 * allocating a new one.
1149 *
1150 * On successful return, the reference to the inode has been transferred
Christoph Hellwig44003722008-08-11 15:49:04 +02001151 * to the dentry. In case of an error the reference on the inode is released.
1152 * To make it easier to use in export operations a %NULL or IS_ERR inode may
1153 * be passed in and will be the error will be propagate to the return value,
1154 * with a %NULL @inode replaced by ERR_PTR(-ESTALE).
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001155 */
1156struct dentry *d_obtain_alias(struct inode *inode)
1157{
Christoph Hellwig9308a612008-08-11 15:49:12 +02001158 static const struct qstr anonstring = { .name = "" };
1159 struct dentry *tmp;
1160 struct dentry *res;
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001161
1162 if (!inode)
Christoph Hellwig44003722008-08-11 15:49:04 +02001163 return ERR_PTR(-ESTALE);
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001164 if (IS_ERR(inode))
1165 return ERR_CAST(inode);
1166
Christoph Hellwig9308a612008-08-11 15:49:12 +02001167 res = d_find_alias(inode);
1168 if (res)
1169 goto out_iput;
1170
1171 tmp = d_alloc(NULL, &anonstring);
1172 if (!tmp) {
1173 res = ERR_PTR(-ENOMEM);
1174 goto out_iput;
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001175 }
Christoph Hellwig9308a612008-08-11 15:49:12 +02001176 tmp->d_parent = tmp; /* make sure dput doesn't croak */
1177
1178 spin_lock(&dcache_lock);
1179 res = __d_find_alias(inode, 0);
1180 if (res) {
1181 spin_unlock(&dcache_lock);
1182 dput(tmp);
1183 goto out_iput;
1184 }
1185
1186 /* attach a disconnected dentry */
1187 spin_lock(&tmp->d_lock);
1188 tmp->d_sb = inode->i_sb;
1189 tmp->d_inode = inode;
1190 tmp->d_flags |= DCACHE_DISCONNECTED;
1191 tmp->d_flags &= ~DCACHE_UNHASHED;
1192 list_add(&tmp->d_alias, &inode->i_dentry);
1193 hlist_add_head(&tmp->d_hash, &inode->i_sb->s_anon);
1194 spin_unlock(&tmp->d_lock);
1195
1196 spin_unlock(&dcache_lock);
1197 return tmp;
1198
1199 out_iput:
1200 iput(inode);
1201 return res;
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001202}
Benny Halevyadc48722009-02-27 14:02:59 -08001203EXPORT_SYMBOL(d_obtain_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
1205/**
1206 * d_splice_alias - splice a disconnected dentry into the tree if one exists
1207 * @inode: the inode which may have a disconnected dentry
1208 * @dentry: a negative dentry which we want to point to the inode.
1209 *
1210 * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
1211 * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
1212 * and return it, else simply d_add the inode to the dentry and return NULL.
1213 *
1214 * This is needed in the lookup routine of any filesystem that is exportable
1215 * (via knfsd) so that we can build dcache paths to directories effectively.
1216 *
1217 * If a dentry was found and moved, then it is returned. Otherwise NULL
1218 * is returned. This matches the expected return value of ->lookup.
1219 *
1220 */
1221struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
1222{
1223 struct dentry *new = NULL;
1224
NeilBrown21c0d8f2006-10-04 02:16:16 -07001225 if (inode && S_ISDIR(inode->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 spin_lock(&dcache_lock);
1227 new = __d_find_alias(inode, 1);
1228 if (new) {
1229 BUG_ON(!(new->d_flags & DCACHE_DISCONNECTED));
1230 spin_unlock(&dcache_lock);
1231 security_d_instantiate(new, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 d_move(new, dentry);
1233 iput(inode);
1234 } else {
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001235 /* already taking dcache_lock, so d_add() by hand */
1236 __d_instantiate(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 spin_unlock(&dcache_lock);
1238 security_d_instantiate(dentry, inode);
1239 d_rehash(dentry);
1240 }
1241 } else
1242 d_add(dentry, inode);
1243 return new;
1244}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001245EXPORT_SYMBOL(d_splice_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
Barry Naujok94035402008-05-21 16:50:46 +10001247/**
1248 * d_add_ci - lookup or allocate new dentry with case-exact name
1249 * @inode: the inode case-insensitive lookup has found
1250 * @dentry: the negative dentry that was passed to the parent's lookup func
1251 * @name: the case-exact name to be associated with the returned dentry
1252 *
1253 * This is to avoid filling the dcache with case-insensitive names to the
1254 * same inode, only the actual correct case is stored in the dcache for
1255 * case-insensitive filesystems.
1256 *
1257 * For a case-insensitive lookup match and if the the case-exact dentry
1258 * already exists in in the dcache, use it and return it.
1259 *
1260 * If no entry exists with the exact case name, allocate new dentry with
1261 * the exact case, and return the spliced entry.
1262 */
Christoph Hellwige45b5902008-08-07 23:49:07 +02001263struct dentry *d_add_ci(struct dentry *dentry, struct inode *inode,
Barry Naujok94035402008-05-21 16:50:46 +10001264 struct qstr *name)
1265{
1266 int error;
1267 struct dentry *found;
1268 struct dentry *new;
1269
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001270 /*
1271 * First check if a dentry matching the name already exists,
1272 * if not go ahead and create it now.
1273 */
Barry Naujok94035402008-05-21 16:50:46 +10001274 found = d_hash_and_lookup(dentry->d_parent, name);
Barry Naujok94035402008-05-21 16:50:46 +10001275 if (!found) {
1276 new = d_alloc(dentry->d_parent, name);
1277 if (!new) {
1278 error = -ENOMEM;
1279 goto err_out;
1280 }
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001281
Barry Naujok94035402008-05-21 16:50:46 +10001282 found = d_splice_alias(inode, new);
1283 if (found) {
1284 dput(new);
1285 return found;
1286 }
1287 return new;
1288 }
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001289
1290 /*
1291 * If a matching dentry exists, and it's not negative use it.
1292 *
1293 * Decrement the reference count to balance the iget() done
1294 * earlier on.
1295 */
Barry Naujok94035402008-05-21 16:50:46 +10001296 if (found->d_inode) {
1297 if (unlikely(found->d_inode != inode)) {
1298 /* This can't happen because bad inodes are unhashed. */
1299 BUG_ON(!is_bad_inode(inode));
1300 BUG_ON(!is_bad_inode(found->d_inode));
1301 }
Barry Naujok94035402008-05-21 16:50:46 +10001302 iput(inode);
1303 return found;
1304 }
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001305
Barry Naujok94035402008-05-21 16:50:46 +10001306 /*
1307 * Negative dentry: instantiate it unless the inode is a directory and
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001308 * already has a dentry.
Barry Naujok94035402008-05-21 16:50:46 +10001309 */
Barry Naujok94035402008-05-21 16:50:46 +10001310 spin_lock(&dcache_lock);
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001311 if (!S_ISDIR(inode->i_mode) || list_empty(&inode->i_dentry)) {
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001312 __d_instantiate(found, inode);
Barry Naujok94035402008-05-21 16:50:46 +10001313 spin_unlock(&dcache_lock);
1314 security_d_instantiate(found, inode);
1315 return found;
1316 }
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001317
Barry Naujok94035402008-05-21 16:50:46 +10001318 /*
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001319 * In case a directory already has a (disconnected) entry grab a
1320 * reference to it, move it in place and use it.
Barry Naujok94035402008-05-21 16:50:46 +10001321 */
1322 new = list_entry(inode->i_dentry.next, struct dentry, d_alias);
1323 dget_locked(new);
1324 spin_unlock(&dcache_lock);
Barry Naujok94035402008-05-21 16:50:46 +10001325 security_d_instantiate(found, inode);
Barry Naujok94035402008-05-21 16:50:46 +10001326 d_move(new, found);
Barry Naujok94035402008-05-21 16:50:46 +10001327 iput(inode);
Barry Naujok94035402008-05-21 16:50:46 +10001328 dput(found);
Barry Naujok94035402008-05-21 16:50:46 +10001329 return new;
1330
1331err_out:
1332 iput(inode);
1333 return ERR_PTR(error);
1334}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001335EXPORT_SYMBOL(d_add_ci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
1337/**
1338 * d_lookup - search for a dentry
1339 * @parent: parent dentry
1340 * @name: qstr of name we wish to find
Nick Pigginb04f7842010-08-18 04:37:34 +10001341 * Returns: dentry, or NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 *
Nick Pigginb04f7842010-08-18 04:37:34 +10001343 * d_lookup searches the children of the parent dentry for the name in
1344 * question. If the dentry is found its reference count is incremented and the
1345 * dentry is returned. The caller must use dput to free the entry when it has
1346 * finished using it. %NULL is returned if the dentry does not exist.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348struct dentry * d_lookup(struct dentry * parent, struct qstr * name)
1349{
1350 struct dentry * dentry = NULL;
1351 unsigned long seq;
1352
1353 do {
1354 seq = read_seqbegin(&rename_lock);
1355 dentry = __d_lookup(parent, name);
1356 if (dentry)
1357 break;
1358 } while (read_seqretry(&rename_lock, seq));
1359 return dentry;
1360}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001361EXPORT_SYMBOL(d_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Nick Pigginb04f7842010-08-18 04:37:34 +10001363/*
1364 * __d_lookup - search for a dentry (racy)
1365 * @parent: parent dentry
1366 * @name: qstr of name we wish to find
1367 * Returns: dentry, or NULL
1368 *
1369 * __d_lookup is like d_lookup, however it may (rarely) return a
1370 * false-negative result due to unrelated rename activity.
1371 *
1372 * __d_lookup is slightly faster by avoiding rename_lock read seqlock,
1373 * however it must be used carefully, eg. with a following d_lookup in
1374 * the case of failure.
1375 *
1376 * __d_lookup callers must be commented.
1377 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378struct dentry * __d_lookup(struct dentry * parent, struct qstr * name)
1379{
1380 unsigned int len = name->len;
1381 unsigned int hash = name->hash;
1382 const unsigned char *str = name->name;
1383 struct hlist_head *head = d_hash(parent,hash);
1384 struct dentry *found = NULL;
1385 struct hlist_node *node;
Paul E. McKenney665a7582005-11-07 00:59:17 -08001386 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
Nick Pigginb04f7842010-08-18 04:37:34 +10001388 /*
1389 * The hash list is protected using RCU.
1390 *
1391 * Take d_lock when comparing a candidate dentry, to avoid races
1392 * with d_move().
1393 *
1394 * It is possible that concurrent renames can mess up our list
1395 * walk here and result in missing our dentry, resulting in the
1396 * false-negative result. d_lookup() protects against concurrent
1397 * renames using rename_lock seqlock.
1398 *
1399 * See Documentation/vfs/dcache-locking.txt for more details.
1400 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 rcu_read_lock();
1402
Paul E. McKenney665a7582005-11-07 00:59:17 -08001403 hlist_for_each_entry_rcu(dentry, node, head, d_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 struct qstr *qstr;
1405
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 if (dentry->d_name.hash != hash)
1407 continue;
1408 if (dentry->d_parent != parent)
1409 continue;
1410
1411 spin_lock(&dentry->d_lock);
1412
1413 /*
1414 * Recheck the dentry after taking the lock - d_move may have
Nick Pigginb04f7842010-08-18 04:37:34 +10001415 * changed things. Don't bother checking the hash because
1416 * we're about to compare the whole name anyway.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 */
1418 if (dentry->d_parent != parent)
1419 goto next;
1420
Linus Torvaldsd0185c02008-09-29 07:42:57 -07001421 /* non-existing due to RCU? */
1422 if (d_unhashed(dentry))
1423 goto next;
1424
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 /*
1426 * It is safe to compare names since d_move() cannot
1427 * change the qstr (protected by d_lock).
1428 */
1429 qstr = &dentry->d_name;
1430 if (parent->d_op && parent->d_op->d_compare) {
1431 if (parent->d_op->d_compare(parent, qstr, name))
1432 goto next;
1433 } else {
1434 if (qstr->len != len)
1435 goto next;
1436 if (memcmp(qstr->name, str, len))
1437 goto next;
1438 }
1439
Linus Torvaldsd0185c02008-09-29 07:42:57 -07001440 atomic_inc(&dentry->d_count);
1441 found = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 spin_unlock(&dentry->d_lock);
1443 break;
1444next:
1445 spin_unlock(&dentry->d_lock);
1446 }
1447 rcu_read_unlock();
1448
1449 return found;
1450}
1451
1452/**
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08001453 * d_hash_and_lookup - hash the qstr then search for a dentry
1454 * @dir: Directory to search in
1455 * @name: qstr of name we wish to find
1456 *
1457 * On hash failure or on lookup failure NULL is returned.
1458 */
1459struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name)
1460{
1461 struct dentry *dentry = NULL;
1462
1463 /*
1464 * Check for a fs-specific hash function. Note that we must
1465 * calculate the standard hash first, as the d_op->d_hash()
1466 * routine may choose to leave the hash value unchanged.
1467 */
1468 name->hash = full_name_hash(name->name, name->len);
1469 if (dir->d_op && dir->d_op->d_hash) {
1470 if (dir->d_op->d_hash(dir, name) < 0)
1471 goto out;
1472 }
1473 dentry = d_lookup(dir, name);
1474out:
1475 return dentry;
1476}
1477
1478/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 * d_validate - verify dentry provided from insecure source
1480 * @dentry: The dentry alleged to be valid child of @dparent
1481 * @dparent: The parent dentry (known to be valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 *
1483 * An insecure source has sent us a dentry, here we verify it and dget() it.
1484 * This is used by ncpfs in its readdir implementation.
1485 * Zero is returned in the dentry is invalid.
1486 */
1487
1488int d_validate(struct dentry *dentry, struct dentry *dparent)
1489{
1490 struct hlist_head *base;
1491 struct hlist_node *lhp;
1492
1493 /* Check whether the ptr might be valid at all.. */
1494 if (!kmem_ptr_validate(dentry_cache, dentry))
1495 goto out;
1496
1497 if (dentry->d_parent != dparent)
1498 goto out;
1499
1500 spin_lock(&dcache_lock);
1501 base = d_hash(dparent, dentry->d_name.hash);
1502 hlist_for_each(lhp,base) {
Paul E. McKenney665a7582005-11-07 00:59:17 -08001503 /* hlist_for_each_entry_rcu() not required for d_hash list
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 * as it is parsed under dcache_lock
1505 */
1506 if (dentry == hlist_entry(lhp, struct dentry, d_hash)) {
1507 __dget_locked(dentry);
1508 spin_unlock(&dcache_lock);
1509 return 1;
1510 }
1511 }
1512 spin_unlock(&dcache_lock);
1513out:
1514 return 0;
1515}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001516EXPORT_SYMBOL(d_validate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
1518/*
1519 * When a file is deleted, we have two options:
1520 * - turn this dentry into a negative dentry
1521 * - unhash this dentry and free it.
1522 *
1523 * Usually, we want to just turn this into
1524 * a negative dentry, but if anybody else is
1525 * currently using the dentry or the inode
1526 * we can't do that and we fall back on removing
1527 * it from the hash queues and waiting for
1528 * it to be deleted later when it has no users
1529 */
1530
1531/**
1532 * d_delete - delete a dentry
1533 * @dentry: The dentry to delete
1534 *
1535 * Turn the dentry into a negative dentry if possible, otherwise
1536 * remove it from the hash queues so it can be deleted later
1537 */
1538
1539void d_delete(struct dentry * dentry)
1540{
John McCutchan7a91bf72005-08-08 13:52:16 -04001541 int isdir = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 /*
1543 * Are we the only user?
1544 */
1545 spin_lock(&dcache_lock);
1546 spin_lock(&dentry->d_lock);
John McCutchan7a91bf72005-08-08 13:52:16 -04001547 isdir = S_ISDIR(dentry->d_inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 if (atomic_read(&dentry->d_count) == 1) {
Al Viro13e3c5e2010-05-21 16:11:04 -04001549 dentry->d_flags &= ~DCACHE_CANT_MOUNT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 dentry_iput(dentry);
John McCutchan7a91bf72005-08-08 13:52:16 -04001551 fsnotify_nameremove(dentry, isdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 return;
1553 }
1554
1555 if (!d_unhashed(dentry))
1556 __d_drop(dentry);
1557
1558 spin_unlock(&dentry->d_lock);
1559 spin_unlock(&dcache_lock);
John McCutchan7a91bf72005-08-08 13:52:16 -04001560
1561 fsnotify_nameremove(dentry, isdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001563EXPORT_SYMBOL(d_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
1565static void __d_rehash(struct dentry * entry, struct hlist_head *list)
1566{
1567
1568 entry->d_flags &= ~DCACHE_UNHASHED;
1569 hlist_add_head_rcu(&entry->d_hash, list);
1570}
1571
David Howells770bfad2006-08-22 20:06:07 -04001572static void _d_rehash(struct dentry * entry)
1573{
1574 __d_rehash(entry, d_hash(entry->d_parent, entry->d_name.hash));
1575}
1576
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577/**
1578 * d_rehash - add an entry back to the hash
1579 * @entry: dentry to add to the hash
1580 *
1581 * Adds a dentry to the hash according to its name.
1582 */
1583
1584void d_rehash(struct dentry * entry)
1585{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 spin_lock(&dcache_lock);
1587 spin_lock(&entry->d_lock);
David Howells770bfad2006-08-22 20:06:07 -04001588 _d_rehash(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 spin_unlock(&entry->d_lock);
1590 spin_unlock(&dcache_lock);
1591}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001592EXPORT_SYMBOL(d_rehash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594/*
1595 * When switching names, the actual string doesn't strictly have to
1596 * be preserved in the target - because we're dropping the target
1597 * anyway. As such, we can just do a simple memcpy() to copy over
1598 * the new name before we switch.
1599 *
1600 * Note that we have to be a lot more careful about getting the hash
1601 * switched - we have to switch the hash value properly even if it
1602 * then no longer matches the actual (corrupted) string of the target.
1603 * The hash value has to match the hash queue that the dentry is on..
1604 */
1605static void switch_names(struct dentry *dentry, struct dentry *target)
1606{
1607 if (dname_external(target)) {
1608 if (dname_external(dentry)) {
1609 /*
1610 * Both external: swap the pointers
1611 */
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08001612 swap(target->d_name.name, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 } else {
1614 /*
1615 * dentry:internal, target:external. Steal target's
1616 * storage and make target internal.
1617 */
J. Bruce Fields321bcf92007-10-21 16:41:38 -07001618 memcpy(target->d_iname, dentry->d_name.name,
1619 dentry->d_name.len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 dentry->d_name.name = target->d_name.name;
1621 target->d_name.name = target->d_iname;
1622 }
1623 } else {
1624 if (dname_external(dentry)) {
1625 /*
1626 * dentry:external, target:internal. Give dentry's
1627 * storage to target and make dentry internal
1628 */
1629 memcpy(dentry->d_iname, target->d_name.name,
1630 target->d_name.len + 1);
1631 target->d_name.name = dentry->d_name.name;
1632 dentry->d_name.name = dentry->d_iname;
1633 } else {
1634 /*
1635 * Both are internal. Just copy target to dentry
1636 */
1637 memcpy(dentry->d_iname, target->d_name.name,
1638 target->d_name.len + 1);
Al Virodc711ca2008-11-03 15:03:50 -05001639 dentry->d_name.len = target->d_name.len;
1640 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 }
1642 }
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08001643 swap(dentry->d_name.len, target->d_name.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644}
1645
1646/*
1647 * We cannibalize "target" when moving dentry on top of it,
1648 * because it's going to be thrown away anyway. We could be more
1649 * polite about it, though.
1650 *
1651 * This forceful removal will result in ugly /proc output if
1652 * somebody holds a file open that got deleted due to a rename.
1653 * We could be nicer about the deleted file, and let it show
J. Bruce Fieldsbc154b12007-10-16 23:29:42 -07001654 * up under the name it had before it was deleted rather than
1655 * under the original name of the file that was moved on top of it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 */
1657
Trond Myklebust9eaef272006-10-21 10:24:20 -07001658/*
1659 * d_move_locked - move a dentry
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 * @dentry: entry to move
1661 * @target: new dentry
1662 *
1663 * Update the dcache to reflect the move of a file name. Negative
1664 * dcache entries should not be moved in this way.
1665 */
Trond Myklebust9eaef272006-10-21 10:24:20 -07001666static void d_move_locked(struct dentry * dentry, struct dentry * target)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667{
1668 struct hlist_head *list;
1669
1670 if (!dentry->d_inode)
1671 printk(KERN_WARNING "VFS: moving negative dcache entry\n");
1672
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 write_seqlock(&rename_lock);
1674 /*
1675 * XXXX: do we really need to take target->d_lock?
1676 */
1677 if (target < dentry) {
1678 spin_lock(&target->d_lock);
Ingo Molnara90b9c02006-07-03 00:25:04 -07001679 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 } else {
1681 spin_lock(&dentry->d_lock);
Ingo Molnara90b9c02006-07-03 00:25:04 -07001682 spin_lock_nested(&target->d_lock, DENTRY_D_LOCK_NESTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 }
1684
1685 /* Move the dentry to the target hash queue, if on different bucket */
Denis Chengf77e3492007-10-16 23:30:11 -07001686 if (d_unhashed(dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 goto already_unhashed;
1688
1689 hlist_del_rcu(&dentry->d_hash);
1690
1691already_unhashed:
1692 list = d_hash(target->d_parent, target->d_name.hash);
1693 __d_rehash(dentry, list);
1694
1695 /* Unhash the target: dput() will then get rid of it */
1696 __d_drop(target);
1697
Eric Dumazet5160ee62006-01-08 01:03:32 -08001698 list_del(&dentry->d_u.d_child);
1699 list_del(&target->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
1701 /* Switch the names.. */
1702 switch_names(dentry, target);
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08001703 swap(dentry->d_name.hash, target->d_name.hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
1705 /* ... and switch the parents */
1706 if (IS_ROOT(dentry)) {
1707 dentry->d_parent = target->d_parent;
1708 target->d_parent = target;
Eric Dumazet5160ee62006-01-08 01:03:32 -08001709 INIT_LIST_HEAD(&target->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 } else {
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08001711 swap(dentry->d_parent, target->d_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
1713 /* And add them back to the (new) parent lists */
Eric Dumazet5160ee62006-01-08 01:03:32 -08001714 list_add(&target->d_u.d_child, &target->d_parent->d_subdirs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 }
1716
Eric Dumazet5160ee62006-01-08 01:03:32 -08001717 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 spin_unlock(&target->d_lock);
Nick Pigginc32ccd82006-03-25 03:07:09 -08001719 fsnotify_d_move(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 spin_unlock(&dentry->d_lock);
1721 write_sequnlock(&rename_lock);
Trond Myklebust9eaef272006-10-21 10:24:20 -07001722}
1723
1724/**
1725 * d_move - move a dentry
1726 * @dentry: entry to move
1727 * @target: new dentry
1728 *
1729 * Update the dcache to reflect the move of a file name. Negative
1730 * dcache entries should not be moved in this way.
1731 */
1732
1733void d_move(struct dentry * dentry, struct dentry * target)
1734{
1735 spin_lock(&dcache_lock);
1736 d_move_locked(dentry, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 spin_unlock(&dcache_lock);
1738}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001739EXPORT_SYMBOL(d_move);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001741/**
1742 * d_ancestor - search for an ancestor
1743 * @p1: ancestor dentry
1744 * @p2: child dentry
1745 *
1746 * Returns the ancestor dentry of p2 which is a child of p1, if p1 is
1747 * an ancestor of p2, else NULL.
Trond Myklebust9eaef272006-10-21 10:24:20 -07001748 */
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001749struct dentry *d_ancestor(struct dentry *p1, struct dentry *p2)
Trond Myklebust9eaef272006-10-21 10:24:20 -07001750{
1751 struct dentry *p;
1752
OGAWA Hirofumi871c0062008-10-16 07:50:27 +09001753 for (p = p2; !IS_ROOT(p); p = p->d_parent) {
Trond Myklebust9eaef272006-10-21 10:24:20 -07001754 if (p->d_parent == p1)
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001755 return p;
Trond Myklebust9eaef272006-10-21 10:24:20 -07001756 }
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001757 return NULL;
Trond Myklebust9eaef272006-10-21 10:24:20 -07001758}
1759
1760/*
1761 * This helper attempts to cope with remotely renamed directories
1762 *
1763 * It assumes that the caller is already holding
1764 * dentry->d_parent->d_inode->i_mutex and the dcache_lock
1765 *
1766 * Note: If ever the locking in lock_rename() changes, then please
1767 * remember to update this too...
Trond Myklebust9eaef272006-10-21 10:24:20 -07001768 */
1769static struct dentry *__d_unalias(struct dentry *dentry, struct dentry *alias)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +02001770 __releases(dcache_lock)
Trond Myklebust9eaef272006-10-21 10:24:20 -07001771{
1772 struct mutex *m1 = NULL, *m2 = NULL;
1773 struct dentry *ret;
1774
1775 /* If alias and dentry share a parent, then no extra locks required */
1776 if (alias->d_parent == dentry->d_parent)
1777 goto out_unalias;
1778
1779 /* Check for loops */
1780 ret = ERR_PTR(-ELOOP);
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001781 if (d_ancestor(alias, dentry))
Trond Myklebust9eaef272006-10-21 10:24:20 -07001782 goto out_err;
1783
1784 /* See lock_rename() */
1785 ret = ERR_PTR(-EBUSY);
1786 if (!mutex_trylock(&dentry->d_sb->s_vfs_rename_mutex))
1787 goto out_err;
1788 m1 = &dentry->d_sb->s_vfs_rename_mutex;
1789 if (!mutex_trylock(&alias->d_parent->d_inode->i_mutex))
1790 goto out_err;
1791 m2 = &alias->d_parent->d_inode->i_mutex;
1792out_unalias:
1793 d_move_locked(alias, dentry);
1794 ret = alias;
1795out_err:
1796 spin_unlock(&dcache_lock);
1797 if (m2)
1798 mutex_unlock(m2);
1799 if (m1)
1800 mutex_unlock(m1);
1801 return ret;
1802}
1803
1804/*
David Howells770bfad2006-08-22 20:06:07 -04001805 * Prepare an anonymous dentry for life in the superblock's dentry tree as a
1806 * named dentry in place of the dentry to be replaced.
1807 */
1808static void __d_materialise_dentry(struct dentry *dentry, struct dentry *anon)
1809{
1810 struct dentry *dparent, *aparent;
1811
1812 switch_names(dentry, anon);
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08001813 swap(dentry->d_name.hash, anon->d_name.hash);
David Howells770bfad2006-08-22 20:06:07 -04001814
1815 dparent = dentry->d_parent;
1816 aparent = anon->d_parent;
1817
1818 dentry->d_parent = (aparent == anon) ? dentry : aparent;
1819 list_del(&dentry->d_u.d_child);
1820 if (!IS_ROOT(dentry))
1821 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
1822 else
1823 INIT_LIST_HEAD(&dentry->d_u.d_child);
1824
1825 anon->d_parent = (dparent == dentry) ? anon : dparent;
1826 list_del(&anon->d_u.d_child);
1827 if (!IS_ROOT(anon))
1828 list_add(&anon->d_u.d_child, &anon->d_parent->d_subdirs);
1829 else
1830 INIT_LIST_HEAD(&anon->d_u.d_child);
1831
1832 anon->d_flags &= ~DCACHE_DISCONNECTED;
1833}
1834
1835/**
1836 * d_materialise_unique - introduce an inode into the tree
1837 * @dentry: candidate dentry
1838 * @inode: inode to bind to the dentry, to which aliases may be attached
1839 *
1840 * Introduces an dentry into the tree, substituting an extant disconnected
1841 * root directory alias in its place if there is one
1842 */
1843struct dentry *d_materialise_unique(struct dentry *dentry, struct inode *inode)
1844{
Trond Myklebust9eaef272006-10-21 10:24:20 -07001845 struct dentry *actual;
David Howells770bfad2006-08-22 20:06:07 -04001846
1847 BUG_ON(!d_unhashed(dentry));
1848
1849 spin_lock(&dcache_lock);
1850
1851 if (!inode) {
1852 actual = dentry;
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001853 __d_instantiate(dentry, NULL);
David Howells770bfad2006-08-22 20:06:07 -04001854 goto found_lock;
1855 }
1856
Trond Myklebust9eaef272006-10-21 10:24:20 -07001857 if (S_ISDIR(inode->i_mode)) {
1858 struct dentry *alias;
David Howells770bfad2006-08-22 20:06:07 -04001859
Trond Myklebust9eaef272006-10-21 10:24:20 -07001860 /* Does an aliased dentry already exist? */
1861 alias = __d_find_alias(inode, 0);
1862 if (alias) {
1863 actual = alias;
1864 /* Is this an anonymous mountpoint that we could splice
1865 * into our tree? */
1866 if (IS_ROOT(alias)) {
1867 spin_lock(&alias->d_lock);
1868 __d_materialise_dentry(dentry, alias);
1869 __d_drop(alias);
1870 goto found;
1871 }
1872 /* Nope, but we must(!) avoid directory aliasing */
1873 actual = __d_unalias(dentry, alias);
1874 if (IS_ERR(actual))
1875 dput(alias);
1876 goto out_nolock;
1877 }
David Howells770bfad2006-08-22 20:06:07 -04001878 }
1879
1880 /* Add a unique reference */
1881 actual = __d_instantiate_unique(dentry, inode);
1882 if (!actual)
1883 actual = dentry;
1884 else if (unlikely(!d_unhashed(actual)))
1885 goto shouldnt_be_hashed;
1886
1887found_lock:
1888 spin_lock(&actual->d_lock);
1889found:
1890 _d_rehash(actual);
1891 spin_unlock(&actual->d_lock);
1892 spin_unlock(&dcache_lock);
Trond Myklebust9eaef272006-10-21 10:24:20 -07001893out_nolock:
David Howells770bfad2006-08-22 20:06:07 -04001894 if (actual == dentry) {
1895 security_d_instantiate(dentry, inode);
1896 return NULL;
1897 }
1898
1899 iput(inode);
1900 return actual;
1901
David Howells770bfad2006-08-22 20:06:07 -04001902shouldnt_be_hashed:
1903 spin_unlock(&dcache_lock);
1904 BUG();
David Howells770bfad2006-08-22 20:06:07 -04001905}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001906EXPORT_SYMBOL_GPL(d_materialise_unique);
David Howells770bfad2006-08-22 20:06:07 -04001907
Miklos Szeredicdd16d02008-06-23 18:11:53 +02001908static int prepend(char **buffer, int *buflen, const char *str, int namelen)
Ram Pai6092d042008-03-27 13:06:20 +01001909{
1910 *buflen -= namelen;
1911 if (*buflen < 0)
1912 return -ENAMETOOLONG;
1913 *buffer -= namelen;
1914 memcpy(*buffer, str, namelen);
1915 return 0;
1916}
1917
Miklos Szeredicdd16d02008-06-23 18:11:53 +02001918static int prepend_name(char **buffer, int *buflen, struct qstr *name)
1919{
1920 return prepend(buffer, buflen, name->name, name->len);
1921}
1922
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923/**
Miklos Szeredif2eb6572010-08-10 11:41:39 +02001924 * Prepend path string to a buffer
1925 *
1926 * @path: the dentry/vfsmount to report
1927 * @root: root vfsmnt/dentry (may be modified by this function)
1928 * @buffer: pointer to the end of the buffer
1929 * @buflen: pointer to buffer length
1930 *
1931 * Caller holds the dcache_lock.
1932 *
1933 * If path is not reachable from the supplied root, then the value of
1934 * root is changed (without modifying refcounts).
1935 */
1936static int prepend_path(const struct path *path, struct path *root,
1937 char **buffer, int *buflen)
1938{
1939 struct dentry *dentry = path->dentry;
1940 struct vfsmount *vfsmnt = path->mnt;
1941 bool slash = false;
1942 int error = 0;
1943
Nick Piggin99b7db72010-08-18 04:37:39 +10001944 br_read_lock(vfsmount_lock);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02001945 while (dentry != root->dentry || vfsmnt != root->mnt) {
1946 struct dentry * parent;
1947
1948 if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
1949 /* Global root? */
1950 if (vfsmnt->mnt_parent == vfsmnt) {
1951 goto global_root;
1952 }
1953 dentry = vfsmnt->mnt_mountpoint;
1954 vfsmnt = vfsmnt->mnt_parent;
1955 continue;
1956 }
1957 parent = dentry->d_parent;
1958 prefetch(parent);
1959 error = prepend_name(buffer, buflen, &dentry->d_name);
1960 if (!error)
1961 error = prepend(buffer, buflen, "/", 1);
1962 if (error)
1963 break;
1964
1965 slash = true;
1966 dentry = parent;
1967 }
1968
1969out:
1970 if (!error && !slash)
1971 error = prepend(buffer, buflen, "/", 1);
1972
Nick Piggin99b7db72010-08-18 04:37:39 +10001973 br_read_unlock(vfsmount_lock);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02001974 return error;
1975
1976global_root:
1977 /*
1978 * Filesystems needing to implement special "root names"
1979 * should do so with ->d_dname()
1980 */
1981 if (IS_ROOT(dentry) &&
1982 (dentry->d_name.len != 1 || dentry->d_name.name[0] != '/')) {
1983 WARN(1, "Root dentry has weird name <%.*s>\n",
1984 (int) dentry->d_name.len, dentry->d_name.name);
1985 }
1986 root->mnt = vfsmnt;
1987 root->dentry = dentry;
1988 goto out;
1989}
1990
1991/**
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +02001992 * __d_path - return the path of a dentry
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01001993 * @path: the dentry/vfsmount to report
1994 * @root: root vfsmnt/dentry (may be modified by this function)
Randy Dunlapcd956a12010-08-14 13:05:31 -07001995 * @buf: buffer to return value in
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 * @buflen: buffer length
1997 *
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02001998 * Convert a dentry into an ASCII path name.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 *
Arjan van de Ven52afeef2008-12-01 14:35:00 -08002000 * Returns a pointer into the buffer or an error code if the
2001 * path was too long.
Linus Torvalds552ce542007-02-13 12:08:18 -08002002 *
Christoph Hellwigbe148242010-10-10 05:36:21 -04002003 * "buflen" should be positive.
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002004 *
2005 * If path is not reachable from the supplied root, then the value of
2006 * root is changed (without modifying refcounts).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 */
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002008char *__d_path(const struct path *path, struct path *root,
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002009 char *buf, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010{
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002011 char *res = buf + buflen;
2012 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002014 prepend(&res, &buflen, "\0", 1);
Christoph Hellwigbe148242010-10-10 05:36:21 -04002015 spin_lock(&dcache_lock);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002016 error = prepend_path(path, root, &res, &buflen);
Christoph Hellwigbe148242010-10-10 05:36:21 -04002017 spin_unlock(&dcache_lock);
2018
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002019 if (error)
2020 return ERR_PTR(error);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002021 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022}
2023
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002024/*
2025 * same as __d_path but appends "(deleted)" for unlinked files.
2026 */
2027static int path_with_deleted(const struct path *path, struct path *root,
2028 char **buf, int *buflen)
2029{
2030 prepend(buf, buflen, "\0", 1);
2031 if (d_unlinked(path->dentry)) {
2032 int error = prepend(buf, buflen, " (deleted)", 10);
2033 if (error)
2034 return error;
2035 }
2036
2037 return prepend_path(path, root, buf, buflen);
2038}
2039
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002040static int prepend_unreachable(char **buffer, int *buflen)
2041{
2042 return prepend(buffer, buflen, "(unreachable)", 13);
2043}
2044
Jan Bluncka03a8a702008-02-14 19:38:32 -08002045/**
2046 * d_path - return the path of a dentry
Jan Blunckcf28b482008-02-14 19:38:44 -08002047 * @path: path to report
Jan Bluncka03a8a702008-02-14 19:38:32 -08002048 * @buf: buffer to return value in
2049 * @buflen: buffer length
2050 *
2051 * Convert a dentry into an ASCII path name. If the entry has been deleted
2052 * the string " (deleted)" is appended. Note that this is ambiguous.
2053 *
Arjan van de Ven52afeef2008-12-01 14:35:00 -08002054 * Returns a pointer into the buffer or an error code if the path was
2055 * too long. Note: Callers should use the returned pointer, not the passed
2056 * in buffer, to use the name! The implementation often starts at an offset
2057 * into the buffer, and may leave 0 bytes at the start.
Jan Bluncka03a8a702008-02-14 19:38:32 -08002058 *
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +02002059 * "buflen" should be positive.
Jan Bluncka03a8a702008-02-14 19:38:32 -08002060 */
Jan Engelhardt20d4fdc2008-06-09 16:40:36 -07002061char *d_path(const struct path *path, char *buf, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062{
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002063 char *res = buf + buflen;
Jan Blunck6ac08c32008-02-14 19:34:38 -08002064 struct path root;
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002065 struct path tmp;
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002066 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
Eric Dumazetc23fbb62007-05-08 00:26:18 -07002068 /*
2069 * We have various synthetic filesystems that never get mounted. On
2070 * these filesystems dentries are never used for lookup purposes, and
2071 * thus don't need to be hashed. They also don't need a name until a
2072 * user wants to identify the object in /proc/pid/fd/. The little hack
2073 * below allows us to generate a name for these objects on demand:
2074 */
Jan Blunckcf28b482008-02-14 19:38:44 -08002075 if (path->dentry->d_op && path->dentry->d_op->d_dname)
2076 return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
Eric Dumazetc23fbb62007-05-08 00:26:18 -07002077
Miklos Szeredif7ad3c62010-08-10 11:41:36 +02002078 get_fs_root(current->fs, &root);
Linus Torvalds552ce542007-02-13 12:08:18 -08002079 spin_lock(&dcache_lock);
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002080 tmp = root;
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002081 error = path_with_deleted(path, &tmp, &res, &buflen);
2082 if (error)
2083 res = ERR_PTR(error);
Linus Torvalds552ce542007-02-13 12:08:18 -08002084 spin_unlock(&dcache_lock);
Jan Blunck6ac08c32008-02-14 19:34:38 -08002085 path_put(&root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 return res;
2087}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002088EXPORT_SYMBOL(d_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002090/**
2091 * d_path_with_unreachable - return the path of a dentry
2092 * @path: path to report
2093 * @buf: buffer to return value in
2094 * @buflen: buffer length
2095 *
2096 * The difference from d_path() is that this prepends "(unreachable)"
2097 * to paths which are unreachable from the current process' root.
2098 */
2099char *d_path_with_unreachable(const struct path *path, char *buf, int buflen)
2100{
2101 char *res = buf + buflen;
2102 struct path root;
2103 struct path tmp;
2104 int error;
2105
2106 if (path->dentry->d_op && path->dentry->d_op->d_dname)
2107 return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
2108
2109 get_fs_root(current->fs, &root);
2110 spin_lock(&dcache_lock);
2111 tmp = root;
2112 error = path_with_deleted(path, &tmp, &res, &buflen);
2113 if (!error && !path_equal(&tmp, &root))
2114 error = prepend_unreachable(&res, &buflen);
2115 spin_unlock(&dcache_lock);
2116 path_put(&root);
2117 if (error)
2118 res = ERR_PTR(error);
2119
2120 return res;
2121}
2122
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123/*
Eric Dumazetc23fbb62007-05-08 00:26:18 -07002124 * Helper function for dentry_operations.d_dname() members
2125 */
2126char *dynamic_dname(struct dentry *dentry, char *buffer, int buflen,
2127 const char *fmt, ...)
2128{
2129 va_list args;
2130 char temp[64];
2131 int sz;
2132
2133 va_start(args, fmt);
2134 sz = vsnprintf(temp, sizeof(temp), fmt, args) + 1;
2135 va_end(args);
2136
2137 if (sz > sizeof(temp) || sz > buflen)
2138 return ERR_PTR(-ENAMETOOLONG);
2139
2140 buffer += buflen - sz;
2141 return memcpy(buffer, temp, sz);
2142}
2143
2144/*
Ram Pai6092d042008-03-27 13:06:20 +01002145 * Write full pathname from the root of the filesystem into the buffer.
2146 */
Al Viroc1031352010-06-06 22:31:14 -04002147char *__dentry_path(struct dentry *dentry, char *buf, int buflen)
Ram Pai6092d042008-03-27 13:06:20 +01002148{
2149 char *end = buf + buflen;
2150 char *retval;
2151
Ram Pai6092d042008-03-27 13:06:20 +01002152 prepend(&end, &buflen, "\0", 1);
Ram Pai6092d042008-03-27 13:06:20 +01002153 if (buflen < 1)
2154 goto Elong;
2155 /* Get '/' right */
2156 retval = end-1;
2157 *retval = '/';
2158
Miklos Szeredicdd16d02008-06-23 18:11:53 +02002159 while (!IS_ROOT(dentry)) {
2160 struct dentry *parent = dentry->d_parent;
Ram Pai6092d042008-03-27 13:06:20 +01002161
Ram Pai6092d042008-03-27 13:06:20 +01002162 prefetch(parent);
Miklos Szeredicdd16d02008-06-23 18:11:53 +02002163 if ((prepend_name(&end, &buflen, &dentry->d_name) != 0) ||
Ram Pai6092d042008-03-27 13:06:20 +01002164 (prepend(&end, &buflen, "/", 1) != 0))
2165 goto Elong;
2166
2167 retval = end;
2168 dentry = parent;
2169 }
Al Viroc1031352010-06-06 22:31:14 -04002170 return retval;
2171Elong:
2172 return ERR_PTR(-ENAMETOOLONG);
2173}
2174EXPORT_SYMBOL(__dentry_path);
2175
2176char *dentry_path(struct dentry *dentry, char *buf, int buflen)
2177{
2178 char *p = NULL;
2179 char *retval;
2180
2181 spin_lock(&dcache_lock);
2182 if (d_unlinked(dentry)) {
2183 p = buf + buflen;
2184 if (prepend(&p, &buflen, "//deleted", 10) != 0)
2185 goto Elong;
2186 buflen++;
2187 }
2188 retval = __dentry_path(dentry, buf, buflen);
Ram Pai6092d042008-03-27 13:06:20 +01002189 spin_unlock(&dcache_lock);
Al Viroc1031352010-06-06 22:31:14 -04002190 if (!IS_ERR(retval) && p)
2191 *p = '/'; /* restore '/' overriden with '\0' */
Ram Pai6092d042008-03-27 13:06:20 +01002192 return retval;
2193Elong:
2194 spin_unlock(&dcache_lock);
2195 return ERR_PTR(-ENAMETOOLONG);
2196}
2197
2198/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 * NOTE! The user-level library version returns a
2200 * character pointer. The kernel system call just
2201 * returns the length of the buffer filled (which
2202 * includes the ending '\0' character), or a negative
2203 * error value. So libc would do something like
2204 *
2205 * char *getcwd(char * buf, size_t size)
2206 * {
2207 * int retval;
2208 *
2209 * retval = sys_getcwd(buf, size);
2210 * if (retval >= 0)
2211 * return buf;
2212 * errno = -retval;
2213 * return NULL;
2214 * }
2215 */
Heiko Carstens3cdad422009-01-14 14:14:22 +01002216SYSCALL_DEFINE2(getcwd, char __user *, buf, unsigned long, size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217{
Linus Torvalds552ce542007-02-13 12:08:18 -08002218 int error;
Jan Blunck6ac08c32008-02-14 19:34:38 -08002219 struct path pwd, root;
Linus Torvalds552ce542007-02-13 12:08:18 -08002220 char *page = (char *) __get_free_page(GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221
2222 if (!page)
2223 return -ENOMEM;
2224
Miklos Szeredif7ad3c62010-08-10 11:41:36 +02002225 get_fs_root_and_pwd(current->fs, &root, &pwd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
Linus Torvalds552ce542007-02-13 12:08:18 -08002227 error = -ENOENT;
Linus Torvalds552ce542007-02-13 12:08:18 -08002228 spin_lock(&dcache_lock);
Alexey Dobriyanf3da3922009-05-04 03:32:03 +04002229 if (!d_unlinked(pwd.dentry)) {
Linus Torvalds552ce542007-02-13 12:08:18 -08002230 unsigned long len;
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002231 struct path tmp = root;
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002232 char *cwd = page + PAGE_SIZE;
2233 int buflen = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002235 prepend(&cwd, &buflen, "\0", 1);
2236 error = prepend_path(&pwd, &tmp, &cwd, &buflen);
Linus Torvalds552ce542007-02-13 12:08:18 -08002237 spin_unlock(&dcache_lock);
2238
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002239 if (error)
Linus Torvalds552ce542007-02-13 12:08:18 -08002240 goto out;
2241
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002242 /* Unreachable from current root */
2243 if (!path_equal(&tmp, &root)) {
2244 error = prepend_unreachable(&cwd, &buflen);
2245 if (error)
2246 goto out;
2247 }
2248
Linus Torvalds552ce542007-02-13 12:08:18 -08002249 error = -ERANGE;
2250 len = PAGE_SIZE + page - cwd;
2251 if (len <= size) {
2252 error = len;
2253 if (copy_to_user(buf, cwd, len))
2254 error = -EFAULT;
2255 }
2256 } else
2257 spin_unlock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
2259out:
Jan Blunck6ac08c32008-02-14 19:34:38 -08002260 path_put(&pwd);
2261 path_put(&root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 free_page((unsigned long) page);
2263 return error;
2264}
2265
2266/*
2267 * Test whether new_dentry is a subdirectory of old_dentry.
2268 *
2269 * Trivially implemented using the dcache structure
2270 */
2271
2272/**
2273 * is_subdir - is new dentry a subdirectory of old_dentry
2274 * @new_dentry: new dentry
2275 * @old_dentry: old dentry
2276 *
2277 * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
2278 * Returns 0 otherwise.
2279 * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
2280 */
2281
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002282int is_subdir(struct dentry *new_dentry, struct dentry *old_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283{
2284 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 unsigned long seq;
2286
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002287 if (new_dentry == old_dentry)
2288 return 1;
2289
2290 /*
2291 * Need rcu_readlock to protect against the d_parent trashing
2292 * due to d_move
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 */
2294 rcu_read_lock();
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002295 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 /* for restarting inner loop in case of seq retry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 seq = read_seqbegin(&rename_lock);
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002298 if (d_ancestor(old_dentry, new_dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 result = 1;
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002300 else
2301 result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 } while (read_seqretry(&rename_lock, seq));
2303 rcu_read_unlock();
2304
2305 return result;
2306}
2307
Al Viro2096f752010-01-30 13:16:21 -05002308int path_is_under(struct path *path1, struct path *path2)
2309{
2310 struct vfsmount *mnt = path1->mnt;
2311 struct dentry *dentry = path1->dentry;
2312 int res;
Nick Piggin99b7db72010-08-18 04:37:39 +10002313
2314 br_read_lock(vfsmount_lock);
Al Viro2096f752010-01-30 13:16:21 -05002315 if (mnt != path2->mnt) {
2316 for (;;) {
2317 if (mnt->mnt_parent == mnt) {
Nick Piggin99b7db72010-08-18 04:37:39 +10002318 br_read_unlock(vfsmount_lock);
Al Viro2096f752010-01-30 13:16:21 -05002319 return 0;
2320 }
2321 if (mnt->mnt_parent == path2->mnt)
2322 break;
2323 mnt = mnt->mnt_parent;
2324 }
2325 dentry = mnt->mnt_mountpoint;
2326 }
2327 res = is_subdir(dentry, path2->dentry);
Nick Piggin99b7db72010-08-18 04:37:39 +10002328 br_read_unlock(vfsmount_lock);
Al Viro2096f752010-01-30 13:16:21 -05002329 return res;
2330}
2331EXPORT_SYMBOL(path_is_under);
2332
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333void d_genocide(struct dentry *root)
2334{
2335 struct dentry *this_parent = root;
2336 struct list_head *next;
2337
2338 spin_lock(&dcache_lock);
2339repeat:
2340 next = this_parent->d_subdirs.next;
2341resume:
2342 while (next != &this_parent->d_subdirs) {
2343 struct list_head *tmp = next;
Eric Dumazet5160ee62006-01-08 01:03:32 -08002344 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 next = tmp->next;
2346 if (d_unhashed(dentry)||!dentry->d_inode)
2347 continue;
2348 if (!list_empty(&dentry->d_subdirs)) {
2349 this_parent = dentry;
2350 goto repeat;
2351 }
2352 atomic_dec(&dentry->d_count);
2353 }
2354 if (this_parent != root) {
Eric Dumazet5160ee62006-01-08 01:03:32 -08002355 next = this_parent->d_u.d_child.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 atomic_dec(&this_parent->d_count);
2357 this_parent = this_parent->d_parent;
2358 goto resume;
2359 }
2360 spin_unlock(&dcache_lock);
2361}
2362
2363/**
2364 * find_inode_number - check for dentry with name
2365 * @dir: directory to check
2366 * @name: Name to find.
2367 *
2368 * Check whether a dentry already exists for the given name,
2369 * and return the inode number if it has an inode. Otherwise
2370 * 0 is returned.
2371 *
2372 * This routine is used to post-process directory listings for
2373 * filesystems using synthetic inode numbers, and is necessary
2374 * to keep getcwd() working.
2375 */
2376
2377ino_t find_inode_number(struct dentry *dir, struct qstr *name)
2378{
2379 struct dentry * dentry;
2380 ino_t ino = 0;
2381
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08002382 dentry = d_hash_and_lookup(dir, name);
2383 if (dentry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 if (dentry->d_inode)
2385 ino = dentry->d_inode->i_ino;
2386 dput(dentry);
2387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 return ino;
2389}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002390EXPORT_SYMBOL(find_inode_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391
2392static __initdata unsigned long dhash_entries;
2393static int __init set_dhash_entries(char *str)
2394{
2395 if (!str)
2396 return 0;
2397 dhash_entries = simple_strtoul(str, &str, 0);
2398 return 1;
2399}
2400__setup("dhash_entries=", set_dhash_entries);
2401
2402static void __init dcache_init_early(void)
2403{
2404 int loop;
2405
2406 /* If hashes are distributed across NUMA nodes, defer
2407 * hash allocation until vmalloc space is available.
2408 */
2409 if (hashdist)
2410 return;
2411
2412 dentry_hashtable =
2413 alloc_large_system_hash("Dentry cache",
2414 sizeof(struct hlist_head),
2415 dhash_entries,
2416 13,
2417 HASH_EARLY,
2418 &d_hash_shift,
2419 &d_hash_mask,
2420 0);
2421
2422 for (loop = 0; loop < (1 << d_hash_shift); loop++)
2423 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
2424}
2425
Denis Cheng74bf17c2007-10-16 23:26:30 -07002426static void __init dcache_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427{
2428 int loop;
2429
Christoph Hellwig312d3ca2010-10-10 05:36:23 -04002430 percpu_counter_init(&nr_dentry, 0);
2431 percpu_counter_init(&nr_dentry_unused, 0);
2432
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 /*
2434 * A constructor could be added for stable state like the lists,
2435 * but it is probably not worth it because of the cache nature
2436 * of the dcache.
2437 */
Christoph Lameter0a31bd52007-05-06 14:49:57 -07002438 dentry_cache = KMEM_CACHE(dentry,
2439 SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|SLAB_MEM_SPREAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440
Rusty Russell8e1f9362007-07-17 04:03:17 -07002441 register_shrinker(&dcache_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442
2443 /* Hash may have been set up in dcache_init_early */
2444 if (!hashdist)
2445 return;
2446
2447 dentry_hashtable =
2448 alloc_large_system_hash("Dentry cache",
2449 sizeof(struct hlist_head),
2450 dhash_entries,
2451 13,
2452 0,
2453 &d_hash_shift,
2454 &d_hash_mask,
2455 0);
2456
2457 for (loop = 0; loop < (1 << d_hash_shift); loop++)
2458 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
2459}
2460
2461/* SLAB cache for __getname() consumers */
Christoph Lametere18b8902006-12-06 20:33:20 -08002462struct kmem_cache *names_cachep __read_mostly;
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002463EXPORT_SYMBOL(names_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465EXPORT_SYMBOL(d_genocide);
2466
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467void __init vfs_caches_init_early(void)
2468{
2469 dcache_init_early();
2470 inode_init_early();
2471}
2472
2473void __init vfs_caches_init(unsigned long mempages)
2474{
2475 unsigned long reserve;
2476
2477 /* Base hash sizes on available memory, with a reserve equal to
2478 150% of current kernel size */
2479
2480 reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);
2481 mempages -= reserve;
2482
2483 names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09002484 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485
Denis Cheng74bf17c2007-10-16 23:26:30 -07002486 dcache_init();
2487 inode_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 files_init(mempages);
Denis Cheng74bf17c2007-10-16 23:26:30 -07002489 mnt_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 bdev_cache_init();
2491 chrdev_init();
2492}