blob: 1a976d4efbe1c20fc75a776cb79c9b1c48a403b3 [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 }
Nick Piggin265ac902010-10-10 05:36:24 -0400249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 /* Unreachable? Get rid of it */
251 if (d_unhashed(dentry))
252 goto kill_it;
Nick Piggin265ac902010-10-10 05:36:24 -0400253
254 /* Otherwise leave it cached and ensure it's on the LRU */
255 dentry->d_flags |= DCACHE_REFERENCED;
256 if (list_empty(&dentry->d_lru))
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700257 dentry_lru_add(dentry);
Nick Piggin265ac902010-10-10 05:36:24 -0400258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 spin_unlock(&dentry->d_lock);
260 spin_unlock(&dcache_lock);
261 return;
262
263unhash_it:
264 __d_drop(dentry);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700265kill_it:
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700266 /* if dentry was on the d_lru list delete it from there */
267 dentry_lru_del(dentry);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700268 dentry = d_kill(dentry);
269 if (dentry)
270 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700272EXPORT_SYMBOL(dput);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274/**
275 * d_invalidate - invalidate a dentry
276 * @dentry: dentry to invalidate
277 *
278 * Try to invalidate the dentry if it turns out to be
279 * possible. If there are other dentries that can be
280 * reached through this one we can't delete it and we
281 * return -EBUSY. On success we return 0.
282 *
283 * no dcache lock.
284 */
285
286int d_invalidate(struct dentry * dentry)
287{
288 /*
289 * If it's already been dropped, return OK.
290 */
291 spin_lock(&dcache_lock);
292 if (d_unhashed(dentry)) {
293 spin_unlock(&dcache_lock);
294 return 0;
295 }
296 /*
297 * Check whether to do a partial shrink_dcache
298 * to get rid of unused child entries.
299 */
300 if (!list_empty(&dentry->d_subdirs)) {
301 spin_unlock(&dcache_lock);
302 shrink_dcache_parent(dentry);
303 spin_lock(&dcache_lock);
304 }
305
306 /*
307 * Somebody else still using it?
308 *
309 * If it's a directory, we can't drop it
310 * for fear of somebody re-populating it
311 * with children (even though dropping it
312 * would make it unreachable from the root,
313 * we might still populate it if it was a
314 * working directory or similar).
315 */
316 spin_lock(&dentry->d_lock);
317 if (atomic_read(&dentry->d_count) > 1) {
318 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
319 spin_unlock(&dentry->d_lock);
320 spin_unlock(&dcache_lock);
321 return -EBUSY;
322 }
323 }
324
325 __d_drop(dentry);
326 spin_unlock(&dentry->d_lock);
327 spin_unlock(&dcache_lock);
328 return 0;
329}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700330EXPORT_SYMBOL(d_invalidate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332/* This should be called _only_ with dcache_lock held */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333static inline struct dentry * __dget_locked(struct dentry *dentry)
334{
335 atomic_inc(&dentry->d_count);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700336 dentry_lru_del_init(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 return dentry;
338}
339
340struct dentry * dget_locked(struct dentry *dentry)
341{
342 return __dget_locked(dentry);
343}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700344EXPORT_SYMBOL(dget_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346/**
347 * d_find_alias - grab a hashed alias of inode
348 * @inode: inode in question
349 * @want_discon: flag, used by d_splice_alias, to request
350 * that only a DISCONNECTED alias be returned.
351 *
352 * If inode has a hashed alias, or is a directory and has any alias,
353 * acquire the reference to alias and return it. Otherwise return NULL.
354 * Notice that if inode is a directory there can be only one alias and
355 * it can be unhashed only if it has no children, or if it is the root
356 * of a filesystem.
357 *
NeilBrown21c0d8f2006-10-04 02:16:16 -0700358 * If the inode has an IS_ROOT, DCACHE_DISCONNECTED alias, then prefer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 * any other hashed alias over that one unless @want_discon is set,
NeilBrown21c0d8f2006-10-04 02:16:16 -0700360 * in which case only return an IS_ROOT, DCACHE_DISCONNECTED alias.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 */
362
363static struct dentry * __d_find_alias(struct inode *inode, int want_discon)
364{
365 struct list_head *head, *next, *tmp;
366 struct dentry *alias, *discon_alias=NULL;
367
368 head = &inode->i_dentry;
369 next = inode->i_dentry.next;
370 while (next != head) {
371 tmp = next;
372 next = tmp->next;
373 prefetch(next);
374 alias = list_entry(tmp, struct dentry, d_alias);
375 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
NeilBrown21c0d8f2006-10-04 02:16:16 -0700376 if (IS_ROOT(alias) &&
377 (alias->d_flags & DCACHE_DISCONNECTED))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 discon_alias = alias;
379 else if (!want_discon) {
380 __dget_locked(alias);
381 return alias;
382 }
383 }
384 }
385 if (discon_alias)
386 __dget_locked(discon_alias);
387 return discon_alias;
388}
389
390struct dentry * d_find_alias(struct inode *inode)
391{
David Howells214fda12006-03-25 03:06:36 -0800392 struct dentry *de = NULL;
393
394 if (!list_empty(&inode->i_dentry)) {
395 spin_lock(&dcache_lock);
396 de = __d_find_alias(inode, 0);
397 spin_unlock(&dcache_lock);
398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 return de;
400}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700401EXPORT_SYMBOL(d_find_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403/*
404 * Try to kill dentries associated with this inode.
405 * WARNING: you must own a reference to inode.
406 */
407void d_prune_aliases(struct inode *inode)
408{
Domen Puncer0cdca3f2005-09-10 00:27:07 -0700409 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410restart:
411 spin_lock(&dcache_lock);
Domen Puncer0cdca3f2005-09-10 00:27:07 -0700412 list_for_each_entry(dentry, &inode->i_dentry, d_alias) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 spin_lock(&dentry->d_lock);
414 if (!atomic_read(&dentry->d_count)) {
415 __dget_locked(dentry);
416 __d_drop(dentry);
417 spin_unlock(&dentry->d_lock);
418 spin_unlock(&dcache_lock);
419 dput(dentry);
420 goto restart;
421 }
422 spin_unlock(&dentry->d_lock);
423 }
424 spin_unlock(&dcache_lock);
425}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700426EXPORT_SYMBOL(d_prune_aliases);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428/*
Andrew Mortond702ccb2006-06-22 14:47:31 -0700429 * Throw away a dentry - free the inode, dput the parent. This requires that
430 * the LRU list has already been removed.
431 *
Miklos Szeredi85864e12007-10-16 23:27:09 -0700432 * Try to prune ancestors as well. This is necessary to prevent
433 * quadratic behavior of shrink_dcache_parent(), but is also expected
434 * to be beneficial in reducing dentry cache fragmentation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 */
Miklos Szeredi85864e12007-10-16 23:27:09 -0700436static void prune_one_dentry(struct dentry * dentry)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200437 __releases(dentry->d_lock)
438 __releases(dcache_lock)
439 __acquires(dcache_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 __d_drop(dentry);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700442 dentry = d_kill(dentry);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700443
444 /*
445 * Prune ancestors. Locking is simpler than in dput(),
446 * because dcache_lock needs to be taken anyway.
447 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 spin_lock(&dcache_lock);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700449 while (dentry) {
450 if (!atomic_dec_and_lock(&dentry->d_count, &dentry->d_lock))
451 return;
452
453 if (dentry->d_op && dentry->d_op->d_delete)
454 dentry->d_op->d_delete(dentry);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700455 dentry_lru_del_init(dentry);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700456 __d_drop(dentry);
457 dentry = d_kill(dentry);
458 spin_lock(&dcache_lock);
459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460}
461
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700462/*
463 * Shrink the dentry LRU on a given superblock.
464 * @sb : superblock to shrink dentry LRU.
465 * @count: If count is NULL, we prune all dentries on superblock.
466 * @flags: If flags is non-zero, we need to do special processing based on
467 * which flags are set. This means we don't need to maintain multiple
468 * similar copies of this loop.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 */
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700470static void __shrink_dcache_sb(struct super_block *sb, int *count, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700472 LIST_HEAD(referenced);
473 LIST_HEAD(tmp);
474 struct dentry *dentry;
475 int cnt = 0;
476
477 BUG_ON(!sb);
478 BUG_ON((flags & DCACHE_REFERENCED) && count == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 spin_lock(&dcache_lock);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700480 if (count != NULL)
481 /* called from prune_dcache() and shrink_dcache_parent() */
482 cnt = *count;
483restart:
484 if (count == NULL)
485 list_splice_init(&sb->s_dentry_lru, &tmp);
486 else {
487 while (!list_empty(&sb->s_dentry_lru)) {
488 dentry = list_entry(sb->s_dentry_lru.prev,
489 struct dentry, d_lru);
490 BUG_ON(dentry->d_sb != sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700492 spin_lock(&dentry->d_lock);
493 /*
494 * If we are honouring the DCACHE_REFERENCED flag and
495 * the dentry has this flag set, don't free it. Clear
496 * the flag and put it back on the LRU.
NeilBrown0feae5c2006-06-22 14:47:28 -0700497 */
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700498 if ((flags & DCACHE_REFERENCED)
499 && (dentry->d_flags & DCACHE_REFERENCED)) {
500 dentry->d_flags &= ~DCACHE_REFERENCED;
npiggin@suse.dec490d792009-04-26 20:25:53 +1000501 list_move(&dentry->d_lru, &referenced);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700502 spin_unlock(&dentry->d_lock);
503 } else {
504 list_move_tail(&dentry->d_lru, &tmp);
505 spin_unlock(&dentry->d_lock);
506 cnt--;
507 if (!cnt)
508 break;
NeilBrown0feae5c2006-06-22 14:47:28 -0700509 }
Kentaro Makitaf3c6ba92008-07-25 19:44:40 -0700510 cond_resched_lock(&dcache_lock);
NeilBrown0feae5c2006-06-22 14:47:28 -0700511 }
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700512 }
513 while (!list_empty(&tmp)) {
514 dentry = list_entry(tmp.prev, struct dentry, d_lru);
515 dentry_lru_del_init(dentry);
516 spin_lock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 /*
518 * We found an inuse dentry which was not removed from
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700519 * the LRU because of laziness during lookup. Do not free
520 * it - just keep it off the LRU list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 */
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700522 if (atomic_read(&dentry->d_count)) {
523 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 continue;
525 }
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700526 prune_one_dentry(dentry);
527 /* dentry->d_lock was dropped in prune_one_dentry() */
528 cond_resched_lock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 }
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700530 if (count == NULL && !list_empty(&sb->s_dentry_lru))
531 goto restart;
532 if (count != NULL)
533 *count = cnt;
534 if (!list_empty(&referenced))
535 list_splice(&referenced, &sb->s_dentry_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 spin_unlock(&dcache_lock);
537}
538
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700539/**
540 * prune_dcache - shrink the dcache
541 * @count: number of entries to try to free
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 *
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700543 * Shrink the dcache. This is done when we need more memory, or simply when we
544 * need to unmount something (at which point we need to unuse all dentries).
545 *
546 * This function may fail to free any resources if all the dentries are in use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 */
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700548static void prune_dcache(int count)
549{
Al Virodca33252010-07-25 02:31:46 +0400550 struct super_block *sb, *p = NULL;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700551 int w_count;
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400552 int unused = percpu_counter_sum_positive(&nr_dentry_unused);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700553 int prune_ratio;
554 int pruned;
555
556 if (unused == 0 || count == 0)
557 return;
558 spin_lock(&dcache_lock);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700559 if (count >= unused)
560 prune_ratio = 1;
561 else
562 prune_ratio = unused / count;
563 spin_lock(&sb_lock);
Al Virodca33252010-07-25 02:31:46 +0400564 list_for_each_entry(sb, &super_blocks, s_list) {
Al Viro551de6f2010-03-22 19:36:35 -0400565 if (list_empty(&sb->s_instances))
566 continue;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700567 if (sb->s_nr_dentry_unused == 0)
568 continue;
569 sb->s_count++;
570 /* Now, we reclaim unused dentrins with fairness.
571 * We reclaim them same percentage from each superblock.
572 * We calculate number of dentries to scan on this sb
573 * as follows, but the implementation is arranged to avoid
574 * overflows:
575 * number of dentries to scan on this sb =
576 * count * (number of dentries on this sb /
577 * number of dentries in the machine)
578 */
579 spin_unlock(&sb_lock);
580 if (prune_ratio != 1)
581 w_count = (sb->s_nr_dentry_unused / prune_ratio) + 1;
582 else
583 w_count = sb->s_nr_dentry_unused;
584 pruned = w_count;
585 /*
586 * We need to be sure this filesystem isn't being unmounted,
587 * otherwise we could race with generic_shutdown_super(), and
588 * end up holding a reference to an inode while the filesystem
589 * is unmounted. So we try to get s_umount, and make sure
590 * s_root isn't NULL.
591 */
592 if (down_read_trylock(&sb->s_umount)) {
593 if ((sb->s_root != NULL) &&
594 (!list_empty(&sb->s_dentry_lru))) {
595 spin_unlock(&dcache_lock);
596 __shrink_dcache_sb(sb, &w_count,
597 DCACHE_REFERENCED);
598 pruned -= w_count;
599 spin_lock(&dcache_lock);
600 }
601 up_read(&sb->s_umount);
602 }
603 spin_lock(&sb_lock);
Al Virodca33252010-07-25 02:31:46 +0400604 if (p)
605 __put_super(p);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700606 count -= pruned;
Al Virodca33252010-07-25 02:31:46 +0400607 p = sb;
Al Viro79893c12010-03-22 20:27:55 -0400608 /* more work left to do? */
609 if (count <= 0)
610 break;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700611 }
Al Virodca33252010-07-25 02:31:46 +0400612 if (p)
613 __put_super(p);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700614 spin_unlock(&sb_lock);
615 spin_unlock(&dcache_lock);
616}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618/**
619 * shrink_dcache_sb - shrink dcache for a superblock
620 * @sb: superblock
621 *
622 * Shrink the dcache for the specified super block. This
623 * is used to free the dcache before unmounting a file
624 * system
625 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626void shrink_dcache_sb(struct super_block * sb)
627{
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700628 __shrink_dcache_sb(sb, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700630EXPORT_SYMBOL(shrink_dcache_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632/*
David Howellsc636ebd2006-10-11 01:22:19 -0700633 * destroy a single subtree of dentries for unmount
634 * - see the comments on shrink_dcache_for_umount() for a description of the
635 * locking
636 */
637static void shrink_dcache_for_umount_subtree(struct dentry *dentry)
638{
639 struct dentry *parent;
David Howellsf8713572006-10-28 10:38:46 -0700640 unsigned detached = 0;
David Howellsc636ebd2006-10-11 01:22:19 -0700641
642 BUG_ON(!IS_ROOT(dentry));
643
644 /* detach this root from the system */
645 spin_lock(&dcache_lock);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700646 dentry_lru_del_init(dentry);
David Howellsc636ebd2006-10-11 01:22:19 -0700647 __d_drop(dentry);
648 spin_unlock(&dcache_lock);
649
650 for (;;) {
651 /* descend to the first leaf in the current subtree */
652 while (!list_empty(&dentry->d_subdirs)) {
653 struct dentry *loop;
654
655 /* this is a branch with children - detach all of them
656 * from the system in one go */
657 spin_lock(&dcache_lock);
658 list_for_each_entry(loop, &dentry->d_subdirs,
659 d_u.d_child) {
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700660 dentry_lru_del_init(loop);
David Howellsc636ebd2006-10-11 01:22:19 -0700661 __d_drop(loop);
662 cond_resched_lock(&dcache_lock);
663 }
664 spin_unlock(&dcache_lock);
665
666 /* move to the first child */
667 dentry = list_entry(dentry->d_subdirs.next,
668 struct dentry, d_u.d_child);
669 }
670
671 /* consume the dentries from this leaf up through its parents
672 * until we find one with children or run out altogether */
673 do {
674 struct inode *inode;
675
676 if (atomic_read(&dentry->d_count) != 0) {
677 printk(KERN_ERR
678 "BUG: Dentry %p{i=%lx,n=%s}"
679 " still in use (%d)"
680 " [unmount of %s %s]\n",
681 dentry,
682 dentry->d_inode ?
683 dentry->d_inode->i_ino : 0UL,
684 dentry->d_name.name,
685 atomic_read(&dentry->d_count),
686 dentry->d_sb->s_type->name,
687 dentry->d_sb->s_id);
688 BUG();
689 }
690
OGAWA Hirofumi871c0062008-10-16 07:50:27 +0900691 if (IS_ROOT(dentry))
David Howellsc636ebd2006-10-11 01:22:19 -0700692 parent = NULL;
OGAWA Hirofumi871c0062008-10-16 07:50:27 +0900693 else {
694 parent = dentry->d_parent;
David Howellsc636ebd2006-10-11 01:22:19 -0700695 atomic_dec(&parent->d_count);
OGAWA Hirofumi871c0062008-10-16 07:50:27 +0900696 }
David Howellsc636ebd2006-10-11 01:22:19 -0700697
698 list_del(&dentry->d_u.d_child);
David Howellsf8713572006-10-28 10:38:46 -0700699 detached++;
David Howellsc636ebd2006-10-11 01:22:19 -0700700
701 inode = dentry->d_inode;
702 if (inode) {
703 dentry->d_inode = NULL;
704 list_del_init(&dentry->d_alias);
705 if (dentry->d_op && dentry->d_op->d_iput)
706 dentry->d_op->d_iput(dentry, inode);
707 else
708 iput(inode);
709 }
710
711 d_free(dentry);
712
713 /* finished when we fall off the top of the tree,
714 * otherwise we ascend to the parent and move to the
715 * next sibling if there is one */
716 if (!parent)
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400717 return;
David Howellsc636ebd2006-10-11 01:22:19 -0700718 dentry = parent;
David Howellsc636ebd2006-10-11 01:22:19 -0700719 } while (list_empty(&dentry->d_subdirs));
720
721 dentry = list_entry(dentry->d_subdirs.next,
722 struct dentry, d_u.d_child);
723 }
724}
725
726/*
727 * destroy the dentries attached to a superblock on unmounting
728 * - we don't need to use dentry->d_lock, and only need dcache_lock when
729 * removing the dentry from the system lists and hashes because:
730 * - the superblock is detached from all mountings and open files, so the
731 * dentry trees will not be rearranged by the VFS
732 * - s_umount is write-locked, so the memory pressure shrinker will ignore
733 * any dentries belonging to this superblock that it comes across
734 * - the filesystem itself is no longer permitted to rearrange the dentries
735 * in this superblock
736 */
737void shrink_dcache_for_umount(struct super_block *sb)
738{
739 struct dentry *dentry;
740
741 if (down_read_trylock(&sb->s_umount))
742 BUG();
743
744 dentry = sb->s_root;
745 sb->s_root = NULL;
746 atomic_dec(&dentry->d_count);
747 shrink_dcache_for_umount_subtree(dentry);
748
749 while (!hlist_empty(&sb->s_anon)) {
750 dentry = hlist_entry(sb->s_anon.first, struct dentry, d_hash);
751 shrink_dcache_for_umount_subtree(dentry);
752 }
753}
754
755/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 * Search for at least 1 mount point in the dentry's subdirs.
757 * We descend to the next level whenever the d_subdirs
758 * list is non-empty and continue searching.
759 */
760
761/**
762 * have_submounts - check for mounts over a dentry
763 * @parent: dentry to check.
764 *
765 * Return true if the parent or its subdirectories contain
766 * a mount point
767 */
768
769int have_submounts(struct dentry *parent)
770{
771 struct dentry *this_parent = parent;
772 struct list_head *next;
773
774 spin_lock(&dcache_lock);
775 if (d_mountpoint(parent))
776 goto positive;
777repeat:
778 next = this_parent->d_subdirs.next;
779resume:
780 while (next != &this_parent->d_subdirs) {
781 struct list_head *tmp = next;
Eric Dumazet5160ee62006-01-08 01:03:32 -0800782 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 next = tmp->next;
784 /* Have we found a mount point ? */
785 if (d_mountpoint(dentry))
786 goto positive;
787 if (!list_empty(&dentry->d_subdirs)) {
788 this_parent = dentry;
789 goto repeat;
790 }
791 }
792 /*
793 * All done at this level ... ascend and resume the search.
794 */
795 if (this_parent != parent) {
Eric Dumazet5160ee62006-01-08 01:03:32 -0800796 next = this_parent->d_u.d_child.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 this_parent = this_parent->d_parent;
798 goto resume;
799 }
800 spin_unlock(&dcache_lock);
801 return 0; /* No mount points found in tree */
802positive:
803 spin_unlock(&dcache_lock);
804 return 1;
805}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700806EXPORT_SYMBOL(have_submounts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
808/*
809 * Search the dentry child list for the specified parent,
810 * and move any unused dentries to the end of the unused
811 * list for prune_dcache(). We descend to the next level
812 * whenever the d_subdirs list is non-empty and continue
813 * searching.
814 *
815 * It returns zero iff there are no unused children,
816 * otherwise it returns the number of children moved to
817 * the end of the unused list. This may not be the total
818 * number of unused children, because select_parent can
819 * drop the lock and return early due to latency
820 * constraints.
821 */
822static int select_parent(struct dentry * parent)
823{
824 struct dentry *this_parent = parent;
825 struct list_head *next;
826 int found = 0;
827
828 spin_lock(&dcache_lock);
829repeat:
830 next = this_parent->d_subdirs.next;
831resume:
832 while (next != &this_parent->d_subdirs) {
833 struct list_head *tmp = next;
Eric Dumazet5160ee62006-01-08 01:03:32 -0800834 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 next = tmp->next;
836
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700837 dentry_lru_del_init(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 /*
839 * move only zero ref count dentries to the end
840 * of the unused list for prune_dcache
841 */
842 if (!atomic_read(&dentry->d_count)) {
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700843 dentry_lru_add_tail(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 found++;
845 }
846
847 /*
848 * We can return to the caller if we have found some (this
849 * ensures forward progress). We'll be coming back to find
850 * the rest.
851 */
852 if (found && need_resched())
853 goto out;
854
855 /*
856 * Descend a level if the d_subdirs list is non-empty.
857 */
858 if (!list_empty(&dentry->d_subdirs)) {
859 this_parent = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 goto repeat;
861 }
862 }
863 /*
864 * All done at this level ... ascend and resume the search.
865 */
866 if (this_parent != parent) {
Eric Dumazet5160ee62006-01-08 01:03:32 -0800867 next = this_parent->d_u.d_child.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 this_parent = this_parent->d_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 goto resume;
870 }
871out:
872 spin_unlock(&dcache_lock);
873 return found;
874}
875
876/**
877 * shrink_dcache_parent - prune dcache
878 * @parent: parent of entries to prune
879 *
880 * Prune the dcache to remove unused children of the parent dentry.
881 */
882
883void shrink_dcache_parent(struct dentry * parent)
884{
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700885 struct super_block *sb = parent->d_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 int found;
887
888 while ((found = select_parent(parent)) != 0)
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700889 __shrink_dcache_sb(sb, &found, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700891EXPORT_SYMBOL(shrink_dcache_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893/*
894 * Scan `nr' dentries and return the number which remain.
895 *
896 * We need to avoid reentering the filesystem if the caller is performing a
897 * GFP_NOFS allocation attempt. One example deadlock is:
898 *
899 * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache->
900 * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->put_inode->
901 * ext2_discard_prealloc->ext2_free_blocks->lock_super->DEADLOCK.
902 *
903 * In this case we return -1 to tell the caller that we baled.
904 */
Dave Chinner7f8275d2010-07-19 14:56:17 +1000905static int shrink_dcache_memory(struct shrinker *shrink, int nr, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400907 int nr_unused;
908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 if (nr) {
910 if (!(gfp_mask & __GFP_FS))
911 return -1;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700912 prune_dcache(nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 }
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400914
915 nr_unused = percpu_counter_sum_positive(&nr_dentry_unused);
916 return (nr_unused / 100) * sysctl_vfs_cache_pressure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917}
918
Rusty Russell8e1f9362007-07-17 04:03:17 -0700919static struct shrinker dcache_shrinker = {
920 .shrink = shrink_dcache_memory,
921 .seeks = DEFAULT_SEEKS,
922};
923
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924/**
925 * d_alloc - allocate a dcache entry
926 * @parent: parent of entry to allocate
927 * @name: qstr of the name
928 *
929 * Allocates a dentry. It returns %NULL if there is insufficient memory
930 * available. On a success the dentry is returned. The name passed in is
931 * copied and the copy passed in may be reused after this call.
932 */
933
934struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
935{
936 struct dentry *dentry;
937 char *dname;
938
Mel Gormane12ba742007-10-16 01:25:52 -0700939 dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 if (!dentry)
941 return NULL;
942
943 if (name->len > DNAME_INLINE_LEN-1) {
944 dname = kmalloc(name->len + 1, GFP_KERNEL);
945 if (!dname) {
946 kmem_cache_free(dentry_cache, dentry);
947 return NULL;
948 }
949 } else {
950 dname = dentry->d_iname;
951 }
952 dentry->d_name.name = dname;
953
954 dentry->d_name.len = name->len;
955 dentry->d_name.hash = name->hash;
956 memcpy(dname, name->name, name->len);
957 dname[name->len] = 0;
958
959 atomic_set(&dentry->d_count, 1);
960 dentry->d_flags = DCACHE_UNHASHED;
961 spin_lock_init(&dentry->d_lock);
962 dentry->d_inode = NULL;
963 dentry->d_parent = NULL;
964 dentry->d_sb = NULL;
965 dentry->d_op = NULL;
966 dentry->d_fsdata = NULL;
967 dentry->d_mounted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 INIT_HLIST_NODE(&dentry->d_hash);
969 INIT_LIST_HEAD(&dentry->d_lru);
970 INIT_LIST_HEAD(&dentry->d_subdirs);
971 INIT_LIST_HEAD(&dentry->d_alias);
972
973 if (parent) {
974 dentry->d_parent = dget(parent);
975 dentry->d_sb = parent->d_sb;
976 } else {
Eric Dumazet5160ee62006-01-08 01:03:32 -0800977 INIT_LIST_HEAD(&dentry->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 }
979
980 spin_lock(&dcache_lock);
981 if (parent)
Eric Dumazet5160ee62006-01-08 01:03:32 -0800982 list_add(&dentry->d_u.d_child, &parent->d_subdirs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 spin_unlock(&dcache_lock);
984
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400985 percpu_counter_inc(&nr_dentry);
986
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 return dentry;
988}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700989EXPORT_SYMBOL(d_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
991struct dentry *d_alloc_name(struct dentry *parent, const char *name)
992{
993 struct qstr q;
994
995 q.name = name;
996 q.len = strlen(name);
997 q.hash = full_name_hash(q.name, q.len);
998 return d_alloc(parent, &q);
999}
H Hartley Sweetenef26ca92009-09-29 20:09:42 -04001000EXPORT_SYMBOL(d_alloc_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001002/* the caller must hold dcache_lock */
1003static void __d_instantiate(struct dentry *dentry, struct inode *inode)
1004{
1005 if (inode)
1006 list_add(&dentry->d_alias, &inode->i_dentry);
1007 dentry->d_inode = inode;
1008 fsnotify_d_instantiate(dentry, inode);
1009}
1010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011/**
1012 * d_instantiate - fill in inode information for a dentry
1013 * @entry: dentry to complete
1014 * @inode: inode to attach to this dentry
1015 *
1016 * Fill in inode information in the entry.
1017 *
1018 * This turns negative dentries into productive full members
1019 * of society.
1020 *
1021 * NOTE! This assumes that the inode count has been incremented
1022 * (or otherwise set) by the caller to indicate that it is now
1023 * in use by the dcache.
1024 */
1025
1026void d_instantiate(struct dentry *entry, struct inode * inode)
1027{
Eric Sesterhenn28133c72006-03-26 18:25:39 +02001028 BUG_ON(!list_empty(&entry->d_alias));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 spin_lock(&dcache_lock);
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001030 __d_instantiate(entry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 spin_unlock(&dcache_lock);
1032 security_d_instantiate(entry, inode);
1033}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001034EXPORT_SYMBOL(d_instantiate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
1036/**
1037 * d_instantiate_unique - instantiate a non-aliased dentry
1038 * @entry: dentry to instantiate
1039 * @inode: inode to attach to this dentry
1040 *
1041 * Fill in inode information in the entry. On success, it returns NULL.
1042 * If an unhashed alias of "entry" already exists, then we return the
Oleg Drokine866cfa2006-01-09 20:52:51 -08001043 * aliased dentry instead and drop one reference to inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 *
1045 * Note that in order to avoid conflicts with rename() etc, the caller
1046 * had better be holding the parent directory semaphore.
Oleg Drokine866cfa2006-01-09 20:52:51 -08001047 *
1048 * This also assumes that the inode count has been incremented
1049 * (or otherwise set) by the caller to indicate that it is now
1050 * in use by the dcache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 */
David Howells770bfad2006-08-22 20:06:07 -04001052static struct dentry *__d_instantiate_unique(struct dentry *entry,
1053 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054{
1055 struct dentry *alias;
1056 int len = entry->d_name.len;
1057 const char *name = entry->d_name.name;
1058 unsigned int hash = entry->d_name.hash;
1059
David Howells770bfad2006-08-22 20:06:07 -04001060 if (!inode) {
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001061 __d_instantiate(entry, NULL);
David Howells770bfad2006-08-22 20:06:07 -04001062 return NULL;
1063 }
1064
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 list_for_each_entry(alias, &inode->i_dentry, d_alias) {
1066 struct qstr *qstr = &alias->d_name;
1067
1068 if (qstr->hash != hash)
1069 continue;
1070 if (alias->d_parent != entry->d_parent)
1071 continue;
1072 if (qstr->len != len)
1073 continue;
1074 if (memcmp(qstr->name, name, len))
1075 continue;
1076 dget_locked(alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 return alias;
1078 }
David Howells770bfad2006-08-22 20:06:07 -04001079
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001080 __d_instantiate(entry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 return NULL;
1082}
David Howells770bfad2006-08-22 20:06:07 -04001083
1084struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode)
1085{
1086 struct dentry *result;
1087
1088 BUG_ON(!list_empty(&entry->d_alias));
1089
1090 spin_lock(&dcache_lock);
1091 result = __d_instantiate_unique(entry, inode);
1092 spin_unlock(&dcache_lock);
1093
1094 if (!result) {
1095 security_d_instantiate(entry, inode);
1096 return NULL;
1097 }
1098
1099 BUG_ON(!d_unhashed(result));
1100 iput(inode);
1101 return result;
1102}
1103
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104EXPORT_SYMBOL(d_instantiate_unique);
1105
1106/**
1107 * d_alloc_root - allocate root dentry
1108 * @root_inode: inode to allocate the root for
1109 *
1110 * Allocate a root ("/") dentry for the inode given. The inode is
1111 * instantiated and returned. %NULL is returned if there is insufficient
1112 * memory or the inode passed is %NULL.
1113 */
1114
1115struct dentry * d_alloc_root(struct inode * root_inode)
1116{
1117 struct dentry *res = NULL;
1118
1119 if (root_inode) {
1120 static const struct qstr name = { .name = "/", .len = 1 };
1121
1122 res = d_alloc(NULL, &name);
1123 if (res) {
1124 res->d_sb = root_inode->i_sb;
1125 res->d_parent = res;
1126 d_instantiate(res, root_inode);
1127 }
1128 }
1129 return res;
1130}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001131EXPORT_SYMBOL(d_alloc_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
1133static inline struct hlist_head *d_hash(struct dentry *parent,
1134 unsigned long hash)
1135{
1136 hash += ((unsigned long) parent ^ GOLDEN_RATIO_PRIME) / L1_CACHE_BYTES;
1137 hash = hash ^ ((hash ^ GOLDEN_RATIO_PRIME) >> D_HASHBITS);
1138 return dentry_hashtable + (hash & D_HASHMASK);
1139}
1140
1141/**
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001142 * d_obtain_alias - find or allocate a dentry for a given inode
1143 * @inode: inode to allocate the dentry for
1144 *
1145 * Obtain a dentry for an inode resulting from NFS filehandle conversion or
1146 * similar open by handle operations. The returned dentry may be anonymous,
1147 * or may have a full name (if the inode was already in the cache).
1148 *
1149 * When called on a directory inode, we must ensure that the inode only ever
1150 * has one dentry. If a dentry is found, that is returned instead of
1151 * allocating a new one.
1152 *
1153 * On successful return, the reference to the inode has been transferred
Christoph Hellwig44003722008-08-11 15:49:04 +02001154 * to the dentry. In case of an error the reference on the inode is released.
1155 * To make it easier to use in export operations a %NULL or IS_ERR inode may
1156 * be passed in and will be the error will be propagate to the return value,
1157 * with a %NULL @inode replaced by ERR_PTR(-ESTALE).
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001158 */
1159struct dentry *d_obtain_alias(struct inode *inode)
1160{
Christoph Hellwig9308a612008-08-11 15:49:12 +02001161 static const struct qstr anonstring = { .name = "" };
1162 struct dentry *tmp;
1163 struct dentry *res;
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001164
1165 if (!inode)
Christoph Hellwig44003722008-08-11 15:49:04 +02001166 return ERR_PTR(-ESTALE);
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001167 if (IS_ERR(inode))
1168 return ERR_CAST(inode);
1169
Christoph Hellwig9308a612008-08-11 15:49:12 +02001170 res = d_find_alias(inode);
1171 if (res)
1172 goto out_iput;
1173
1174 tmp = d_alloc(NULL, &anonstring);
1175 if (!tmp) {
1176 res = ERR_PTR(-ENOMEM);
1177 goto out_iput;
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001178 }
Christoph Hellwig9308a612008-08-11 15:49:12 +02001179 tmp->d_parent = tmp; /* make sure dput doesn't croak */
1180
1181 spin_lock(&dcache_lock);
1182 res = __d_find_alias(inode, 0);
1183 if (res) {
1184 spin_unlock(&dcache_lock);
1185 dput(tmp);
1186 goto out_iput;
1187 }
1188
1189 /* attach a disconnected dentry */
1190 spin_lock(&tmp->d_lock);
1191 tmp->d_sb = inode->i_sb;
1192 tmp->d_inode = inode;
1193 tmp->d_flags |= DCACHE_DISCONNECTED;
1194 tmp->d_flags &= ~DCACHE_UNHASHED;
1195 list_add(&tmp->d_alias, &inode->i_dentry);
1196 hlist_add_head(&tmp->d_hash, &inode->i_sb->s_anon);
1197 spin_unlock(&tmp->d_lock);
1198
1199 spin_unlock(&dcache_lock);
1200 return tmp;
1201
1202 out_iput:
1203 iput(inode);
1204 return res;
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001205}
Benny Halevyadc48722009-02-27 14:02:59 -08001206EXPORT_SYMBOL(d_obtain_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
1208/**
1209 * d_splice_alias - splice a disconnected dentry into the tree if one exists
1210 * @inode: the inode which may have a disconnected dentry
1211 * @dentry: a negative dentry which we want to point to the inode.
1212 *
1213 * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
1214 * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
1215 * and return it, else simply d_add the inode to the dentry and return NULL.
1216 *
1217 * This is needed in the lookup routine of any filesystem that is exportable
1218 * (via knfsd) so that we can build dcache paths to directories effectively.
1219 *
1220 * If a dentry was found and moved, then it is returned. Otherwise NULL
1221 * is returned. This matches the expected return value of ->lookup.
1222 *
1223 */
1224struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
1225{
1226 struct dentry *new = NULL;
1227
NeilBrown21c0d8f2006-10-04 02:16:16 -07001228 if (inode && S_ISDIR(inode->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 spin_lock(&dcache_lock);
1230 new = __d_find_alias(inode, 1);
1231 if (new) {
1232 BUG_ON(!(new->d_flags & DCACHE_DISCONNECTED));
1233 spin_unlock(&dcache_lock);
1234 security_d_instantiate(new, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 d_move(new, dentry);
1236 iput(inode);
1237 } else {
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001238 /* already taking dcache_lock, so d_add() by hand */
1239 __d_instantiate(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 spin_unlock(&dcache_lock);
1241 security_d_instantiate(dentry, inode);
1242 d_rehash(dentry);
1243 }
1244 } else
1245 d_add(dentry, inode);
1246 return new;
1247}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001248EXPORT_SYMBOL(d_splice_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
Barry Naujok94035402008-05-21 16:50:46 +10001250/**
1251 * d_add_ci - lookup or allocate new dentry with case-exact name
1252 * @inode: the inode case-insensitive lookup has found
1253 * @dentry: the negative dentry that was passed to the parent's lookup func
1254 * @name: the case-exact name to be associated with the returned dentry
1255 *
1256 * This is to avoid filling the dcache with case-insensitive names to the
1257 * same inode, only the actual correct case is stored in the dcache for
1258 * case-insensitive filesystems.
1259 *
1260 * For a case-insensitive lookup match and if the the case-exact dentry
1261 * already exists in in the dcache, use it and return it.
1262 *
1263 * If no entry exists with the exact case name, allocate new dentry with
1264 * the exact case, and return the spliced entry.
1265 */
Christoph Hellwige45b5902008-08-07 23:49:07 +02001266struct dentry *d_add_ci(struct dentry *dentry, struct inode *inode,
Barry Naujok94035402008-05-21 16:50:46 +10001267 struct qstr *name)
1268{
1269 int error;
1270 struct dentry *found;
1271 struct dentry *new;
1272
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001273 /*
1274 * First check if a dentry matching the name already exists,
1275 * if not go ahead and create it now.
1276 */
Barry Naujok94035402008-05-21 16:50:46 +10001277 found = d_hash_and_lookup(dentry->d_parent, name);
Barry Naujok94035402008-05-21 16:50:46 +10001278 if (!found) {
1279 new = d_alloc(dentry->d_parent, name);
1280 if (!new) {
1281 error = -ENOMEM;
1282 goto err_out;
1283 }
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001284
Barry Naujok94035402008-05-21 16:50:46 +10001285 found = d_splice_alias(inode, new);
1286 if (found) {
1287 dput(new);
1288 return found;
1289 }
1290 return new;
1291 }
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001292
1293 /*
1294 * If a matching dentry exists, and it's not negative use it.
1295 *
1296 * Decrement the reference count to balance the iget() done
1297 * earlier on.
1298 */
Barry Naujok94035402008-05-21 16:50:46 +10001299 if (found->d_inode) {
1300 if (unlikely(found->d_inode != inode)) {
1301 /* This can't happen because bad inodes are unhashed. */
1302 BUG_ON(!is_bad_inode(inode));
1303 BUG_ON(!is_bad_inode(found->d_inode));
1304 }
Barry Naujok94035402008-05-21 16:50:46 +10001305 iput(inode);
1306 return found;
1307 }
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001308
Barry Naujok94035402008-05-21 16:50:46 +10001309 /*
1310 * Negative dentry: instantiate it unless the inode is a directory and
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001311 * already has a dentry.
Barry Naujok94035402008-05-21 16:50:46 +10001312 */
Barry Naujok94035402008-05-21 16:50:46 +10001313 spin_lock(&dcache_lock);
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001314 if (!S_ISDIR(inode->i_mode) || list_empty(&inode->i_dentry)) {
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001315 __d_instantiate(found, inode);
Barry Naujok94035402008-05-21 16:50:46 +10001316 spin_unlock(&dcache_lock);
1317 security_d_instantiate(found, inode);
1318 return found;
1319 }
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001320
Barry Naujok94035402008-05-21 16:50:46 +10001321 /*
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001322 * In case a directory already has a (disconnected) entry grab a
1323 * reference to it, move it in place and use it.
Barry Naujok94035402008-05-21 16:50:46 +10001324 */
1325 new = list_entry(inode->i_dentry.next, struct dentry, d_alias);
1326 dget_locked(new);
1327 spin_unlock(&dcache_lock);
Barry Naujok94035402008-05-21 16:50:46 +10001328 security_d_instantiate(found, inode);
Barry Naujok94035402008-05-21 16:50:46 +10001329 d_move(new, found);
Barry Naujok94035402008-05-21 16:50:46 +10001330 iput(inode);
Barry Naujok94035402008-05-21 16:50:46 +10001331 dput(found);
Barry Naujok94035402008-05-21 16:50:46 +10001332 return new;
1333
1334err_out:
1335 iput(inode);
1336 return ERR_PTR(error);
1337}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001338EXPORT_SYMBOL(d_add_ci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
1340/**
1341 * d_lookup - search for a dentry
1342 * @parent: parent dentry
1343 * @name: qstr of name we wish to find
Nick Pigginb04f7842010-08-18 04:37:34 +10001344 * Returns: dentry, or NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 *
Nick Pigginb04f7842010-08-18 04:37:34 +10001346 * d_lookup searches the children of the parent dentry for the name in
1347 * question. If the dentry is found its reference count is incremented and the
1348 * dentry is returned. The caller must use dput to free the entry when it has
1349 * finished using it. %NULL is returned if the dentry does not exist.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351struct dentry * d_lookup(struct dentry * parent, struct qstr * name)
1352{
1353 struct dentry * dentry = NULL;
1354 unsigned long seq;
1355
1356 do {
1357 seq = read_seqbegin(&rename_lock);
1358 dentry = __d_lookup(parent, name);
1359 if (dentry)
1360 break;
1361 } while (read_seqretry(&rename_lock, seq));
1362 return dentry;
1363}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001364EXPORT_SYMBOL(d_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Nick Pigginb04f7842010-08-18 04:37:34 +10001366/*
1367 * __d_lookup - search for a dentry (racy)
1368 * @parent: parent dentry
1369 * @name: qstr of name we wish to find
1370 * Returns: dentry, or NULL
1371 *
1372 * __d_lookup is like d_lookup, however it may (rarely) return a
1373 * false-negative result due to unrelated rename activity.
1374 *
1375 * __d_lookup is slightly faster by avoiding rename_lock read seqlock,
1376 * however it must be used carefully, eg. with a following d_lookup in
1377 * the case of failure.
1378 *
1379 * __d_lookup callers must be commented.
1380 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381struct dentry * __d_lookup(struct dentry * parent, struct qstr * name)
1382{
1383 unsigned int len = name->len;
1384 unsigned int hash = name->hash;
1385 const unsigned char *str = name->name;
1386 struct hlist_head *head = d_hash(parent,hash);
1387 struct dentry *found = NULL;
1388 struct hlist_node *node;
Paul E. McKenney665a7582005-11-07 00:59:17 -08001389 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
Nick Pigginb04f7842010-08-18 04:37:34 +10001391 /*
1392 * The hash list is protected using RCU.
1393 *
1394 * Take d_lock when comparing a candidate dentry, to avoid races
1395 * with d_move().
1396 *
1397 * It is possible that concurrent renames can mess up our list
1398 * walk here and result in missing our dentry, resulting in the
1399 * false-negative result. d_lookup() protects against concurrent
1400 * renames using rename_lock seqlock.
1401 *
1402 * See Documentation/vfs/dcache-locking.txt for more details.
1403 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 rcu_read_lock();
1405
Paul E. McKenney665a7582005-11-07 00:59:17 -08001406 hlist_for_each_entry_rcu(dentry, node, head, d_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 struct qstr *qstr;
1408
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 if (dentry->d_name.hash != hash)
1410 continue;
1411 if (dentry->d_parent != parent)
1412 continue;
1413
1414 spin_lock(&dentry->d_lock);
1415
1416 /*
1417 * Recheck the dentry after taking the lock - d_move may have
Nick Pigginb04f7842010-08-18 04:37:34 +10001418 * changed things. Don't bother checking the hash because
1419 * we're about to compare the whole name anyway.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 */
1421 if (dentry->d_parent != parent)
1422 goto next;
1423
Linus Torvaldsd0185c02008-09-29 07:42:57 -07001424 /* non-existing due to RCU? */
1425 if (d_unhashed(dentry))
1426 goto next;
1427
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 /*
1429 * It is safe to compare names since d_move() cannot
1430 * change the qstr (protected by d_lock).
1431 */
1432 qstr = &dentry->d_name;
1433 if (parent->d_op && parent->d_op->d_compare) {
1434 if (parent->d_op->d_compare(parent, qstr, name))
1435 goto next;
1436 } else {
1437 if (qstr->len != len)
1438 goto next;
1439 if (memcmp(qstr->name, str, len))
1440 goto next;
1441 }
1442
Linus Torvaldsd0185c02008-09-29 07:42:57 -07001443 atomic_inc(&dentry->d_count);
1444 found = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 spin_unlock(&dentry->d_lock);
1446 break;
1447next:
1448 spin_unlock(&dentry->d_lock);
1449 }
1450 rcu_read_unlock();
1451
1452 return found;
1453}
1454
1455/**
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08001456 * d_hash_and_lookup - hash the qstr then search for a dentry
1457 * @dir: Directory to search in
1458 * @name: qstr of name we wish to find
1459 *
1460 * On hash failure or on lookup failure NULL is returned.
1461 */
1462struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name)
1463{
1464 struct dentry *dentry = NULL;
1465
1466 /*
1467 * Check for a fs-specific hash function. Note that we must
1468 * calculate the standard hash first, as the d_op->d_hash()
1469 * routine may choose to leave the hash value unchanged.
1470 */
1471 name->hash = full_name_hash(name->name, name->len);
1472 if (dir->d_op && dir->d_op->d_hash) {
1473 if (dir->d_op->d_hash(dir, name) < 0)
1474 goto out;
1475 }
1476 dentry = d_lookup(dir, name);
1477out:
1478 return dentry;
1479}
1480
1481/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 * d_validate - verify dentry provided from insecure source
1483 * @dentry: The dentry alleged to be valid child of @dparent
1484 * @dparent: The parent dentry (known to be valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 *
1486 * An insecure source has sent us a dentry, here we verify it and dget() it.
1487 * This is used by ncpfs in its readdir implementation.
1488 * Zero is returned in the dentry is invalid.
1489 */
1490
1491int d_validate(struct dentry *dentry, struct dentry *dparent)
1492{
1493 struct hlist_head *base;
1494 struct hlist_node *lhp;
1495
1496 /* Check whether the ptr might be valid at all.. */
1497 if (!kmem_ptr_validate(dentry_cache, dentry))
1498 goto out;
1499
1500 if (dentry->d_parent != dparent)
1501 goto out;
1502
1503 spin_lock(&dcache_lock);
1504 base = d_hash(dparent, dentry->d_name.hash);
1505 hlist_for_each(lhp,base) {
Paul E. McKenney665a7582005-11-07 00:59:17 -08001506 /* hlist_for_each_entry_rcu() not required for d_hash list
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 * as it is parsed under dcache_lock
1508 */
1509 if (dentry == hlist_entry(lhp, struct dentry, d_hash)) {
1510 __dget_locked(dentry);
1511 spin_unlock(&dcache_lock);
1512 return 1;
1513 }
1514 }
1515 spin_unlock(&dcache_lock);
1516out:
1517 return 0;
1518}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001519EXPORT_SYMBOL(d_validate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
1521/*
1522 * When a file is deleted, we have two options:
1523 * - turn this dentry into a negative dentry
1524 * - unhash this dentry and free it.
1525 *
1526 * Usually, we want to just turn this into
1527 * a negative dentry, but if anybody else is
1528 * currently using the dentry or the inode
1529 * we can't do that and we fall back on removing
1530 * it from the hash queues and waiting for
1531 * it to be deleted later when it has no users
1532 */
1533
1534/**
1535 * d_delete - delete a dentry
1536 * @dentry: The dentry to delete
1537 *
1538 * Turn the dentry into a negative dentry if possible, otherwise
1539 * remove it from the hash queues so it can be deleted later
1540 */
1541
1542void d_delete(struct dentry * dentry)
1543{
John McCutchan7a91bf72005-08-08 13:52:16 -04001544 int isdir = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 /*
1546 * Are we the only user?
1547 */
1548 spin_lock(&dcache_lock);
1549 spin_lock(&dentry->d_lock);
John McCutchan7a91bf72005-08-08 13:52:16 -04001550 isdir = S_ISDIR(dentry->d_inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 if (atomic_read(&dentry->d_count) == 1) {
Al Viro13e3c5e2010-05-21 16:11:04 -04001552 dentry->d_flags &= ~DCACHE_CANT_MOUNT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 dentry_iput(dentry);
John McCutchan7a91bf72005-08-08 13:52:16 -04001554 fsnotify_nameremove(dentry, isdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 return;
1556 }
1557
1558 if (!d_unhashed(dentry))
1559 __d_drop(dentry);
1560
1561 spin_unlock(&dentry->d_lock);
1562 spin_unlock(&dcache_lock);
John McCutchan7a91bf72005-08-08 13:52:16 -04001563
1564 fsnotify_nameremove(dentry, isdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001566EXPORT_SYMBOL(d_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
1568static void __d_rehash(struct dentry * entry, struct hlist_head *list)
1569{
1570
1571 entry->d_flags &= ~DCACHE_UNHASHED;
1572 hlist_add_head_rcu(&entry->d_hash, list);
1573}
1574
David Howells770bfad2006-08-22 20:06:07 -04001575static void _d_rehash(struct dentry * entry)
1576{
1577 __d_rehash(entry, d_hash(entry->d_parent, entry->d_name.hash));
1578}
1579
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580/**
1581 * d_rehash - add an entry back to the hash
1582 * @entry: dentry to add to the hash
1583 *
1584 * Adds a dentry to the hash according to its name.
1585 */
1586
1587void d_rehash(struct dentry * entry)
1588{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 spin_lock(&dcache_lock);
1590 spin_lock(&entry->d_lock);
David Howells770bfad2006-08-22 20:06:07 -04001591 _d_rehash(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 spin_unlock(&entry->d_lock);
1593 spin_unlock(&dcache_lock);
1594}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001595EXPORT_SYMBOL(d_rehash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597/*
1598 * When switching names, the actual string doesn't strictly have to
1599 * be preserved in the target - because we're dropping the target
1600 * anyway. As such, we can just do a simple memcpy() to copy over
1601 * the new name before we switch.
1602 *
1603 * Note that we have to be a lot more careful about getting the hash
1604 * switched - we have to switch the hash value properly even if it
1605 * then no longer matches the actual (corrupted) string of the target.
1606 * The hash value has to match the hash queue that the dentry is on..
1607 */
1608static void switch_names(struct dentry *dentry, struct dentry *target)
1609{
1610 if (dname_external(target)) {
1611 if (dname_external(dentry)) {
1612 /*
1613 * Both external: swap the pointers
1614 */
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08001615 swap(target->d_name.name, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 } else {
1617 /*
1618 * dentry:internal, target:external. Steal target's
1619 * storage and make target internal.
1620 */
J. Bruce Fields321bcf92007-10-21 16:41:38 -07001621 memcpy(target->d_iname, dentry->d_name.name,
1622 dentry->d_name.len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 dentry->d_name.name = target->d_name.name;
1624 target->d_name.name = target->d_iname;
1625 }
1626 } else {
1627 if (dname_external(dentry)) {
1628 /*
1629 * dentry:external, target:internal. Give dentry's
1630 * storage to target and make dentry internal
1631 */
1632 memcpy(dentry->d_iname, target->d_name.name,
1633 target->d_name.len + 1);
1634 target->d_name.name = dentry->d_name.name;
1635 dentry->d_name.name = dentry->d_iname;
1636 } else {
1637 /*
1638 * Both are internal. Just copy target to dentry
1639 */
1640 memcpy(dentry->d_iname, target->d_name.name,
1641 target->d_name.len + 1);
Al Virodc711ca2008-11-03 15:03:50 -05001642 dentry->d_name.len = target->d_name.len;
1643 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 }
1645 }
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08001646 swap(dentry->d_name.len, target->d_name.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647}
1648
1649/*
1650 * We cannibalize "target" when moving dentry on top of it,
1651 * because it's going to be thrown away anyway. We could be more
1652 * polite about it, though.
1653 *
1654 * This forceful removal will result in ugly /proc output if
1655 * somebody holds a file open that got deleted due to a rename.
1656 * We could be nicer about the deleted file, and let it show
J. Bruce Fieldsbc154b12007-10-16 23:29:42 -07001657 * up under the name it had before it was deleted rather than
1658 * under the original name of the file that was moved on top of it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 */
1660
Trond Myklebust9eaef272006-10-21 10:24:20 -07001661/*
1662 * d_move_locked - move a dentry
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 * @dentry: entry to move
1664 * @target: new dentry
1665 *
1666 * Update the dcache to reflect the move of a file name. Negative
1667 * dcache entries should not be moved in this way.
1668 */
Trond Myklebust9eaef272006-10-21 10:24:20 -07001669static void d_move_locked(struct dentry * dentry, struct dentry * target)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670{
1671 struct hlist_head *list;
1672
1673 if (!dentry->d_inode)
1674 printk(KERN_WARNING "VFS: moving negative dcache entry\n");
1675
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 write_seqlock(&rename_lock);
1677 /*
1678 * XXXX: do we really need to take target->d_lock?
1679 */
1680 if (target < dentry) {
1681 spin_lock(&target->d_lock);
Ingo Molnara90b9c02006-07-03 00:25:04 -07001682 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 } else {
1684 spin_lock(&dentry->d_lock);
Ingo Molnara90b9c02006-07-03 00:25:04 -07001685 spin_lock_nested(&target->d_lock, DENTRY_D_LOCK_NESTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 }
1687
1688 /* Move the dentry to the target hash queue, if on different bucket */
Denis Chengf77e3492007-10-16 23:30:11 -07001689 if (d_unhashed(dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 goto already_unhashed;
1691
1692 hlist_del_rcu(&dentry->d_hash);
1693
1694already_unhashed:
1695 list = d_hash(target->d_parent, target->d_name.hash);
1696 __d_rehash(dentry, list);
1697
1698 /* Unhash the target: dput() will then get rid of it */
1699 __d_drop(target);
1700
Eric Dumazet5160ee62006-01-08 01:03:32 -08001701 list_del(&dentry->d_u.d_child);
1702 list_del(&target->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
1704 /* Switch the names.. */
1705 switch_names(dentry, target);
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08001706 swap(dentry->d_name.hash, target->d_name.hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
1708 /* ... and switch the parents */
1709 if (IS_ROOT(dentry)) {
1710 dentry->d_parent = target->d_parent;
1711 target->d_parent = target;
Eric Dumazet5160ee62006-01-08 01:03:32 -08001712 INIT_LIST_HEAD(&target->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 } else {
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08001714 swap(dentry->d_parent, target->d_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
1716 /* And add them back to the (new) parent lists */
Eric Dumazet5160ee62006-01-08 01:03:32 -08001717 list_add(&target->d_u.d_child, &target->d_parent->d_subdirs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 }
1719
Eric Dumazet5160ee62006-01-08 01:03:32 -08001720 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 spin_unlock(&target->d_lock);
Nick Pigginc32ccd82006-03-25 03:07:09 -08001722 fsnotify_d_move(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 spin_unlock(&dentry->d_lock);
1724 write_sequnlock(&rename_lock);
Trond Myklebust9eaef272006-10-21 10:24:20 -07001725}
1726
1727/**
1728 * d_move - move a dentry
1729 * @dentry: entry to move
1730 * @target: new dentry
1731 *
1732 * Update the dcache to reflect the move of a file name. Negative
1733 * dcache entries should not be moved in this way.
1734 */
1735
1736void d_move(struct dentry * dentry, struct dentry * target)
1737{
1738 spin_lock(&dcache_lock);
1739 d_move_locked(dentry, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 spin_unlock(&dcache_lock);
1741}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001742EXPORT_SYMBOL(d_move);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001744/**
1745 * d_ancestor - search for an ancestor
1746 * @p1: ancestor dentry
1747 * @p2: child dentry
1748 *
1749 * Returns the ancestor dentry of p2 which is a child of p1, if p1 is
1750 * an ancestor of p2, else NULL.
Trond Myklebust9eaef272006-10-21 10:24:20 -07001751 */
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001752struct dentry *d_ancestor(struct dentry *p1, struct dentry *p2)
Trond Myklebust9eaef272006-10-21 10:24:20 -07001753{
1754 struct dentry *p;
1755
OGAWA Hirofumi871c0062008-10-16 07:50:27 +09001756 for (p = p2; !IS_ROOT(p); p = p->d_parent) {
Trond Myklebust9eaef272006-10-21 10:24:20 -07001757 if (p->d_parent == p1)
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001758 return p;
Trond Myklebust9eaef272006-10-21 10:24:20 -07001759 }
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001760 return NULL;
Trond Myklebust9eaef272006-10-21 10:24:20 -07001761}
1762
1763/*
1764 * This helper attempts to cope with remotely renamed directories
1765 *
1766 * It assumes that the caller is already holding
1767 * dentry->d_parent->d_inode->i_mutex and the dcache_lock
1768 *
1769 * Note: If ever the locking in lock_rename() changes, then please
1770 * remember to update this too...
Trond Myklebust9eaef272006-10-21 10:24:20 -07001771 */
1772static struct dentry *__d_unalias(struct dentry *dentry, struct dentry *alias)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +02001773 __releases(dcache_lock)
Trond Myklebust9eaef272006-10-21 10:24:20 -07001774{
1775 struct mutex *m1 = NULL, *m2 = NULL;
1776 struct dentry *ret;
1777
1778 /* If alias and dentry share a parent, then no extra locks required */
1779 if (alias->d_parent == dentry->d_parent)
1780 goto out_unalias;
1781
1782 /* Check for loops */
1783 ret = ERR_PTR(-ELOOP);
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001784 if (d_ancestor(alias, dentry))
Trond Myklebust9eaef272006-10-21 10:24:20 -07001785 goto out_err;
1786
1787 /* See lock_rename() */
1788 ret = ERR_PTR(-EBUSY);
1789 if (!mutex_trylock(&dentry->d_sb->s_vfs_rename_mutex))
1790 goto out_err;
1791 m1 = &dentry->d_sb->s_vfs_rename_mutex;
1792 if (!mutex_trylock(&alias->d_parent->d_inode->i_mutex))
1793 goto out_err;
1794 m2 = &alias->d_parent->d_inode->i_mutex;
1795out_unalias:
1796 d_move_locked(alias, dentry);
1797 ret = alias;
1798out_err:
1799 spin_unlock(&dcache_lock);
1800 if (m2)
1801 mutex_unlock(m2);
1802 if (m1)
1803 mutex_unlock(m1);
1804 return ret;
1805}
1806
1807/*
David Howells770bfad2006-08-22 20:06:07 -04001808 * Prepare an anonymous dentry for life in the superblock's dentry tree as a
1809 * named dentry in place of the dentry to be replaced.
1810 */
1811static void __d_materialise_dentry(struct dentry *dentry, struct dentry *anon)
1812{
1813 struct dentry *dparent, *aparent;
1814
1815 switch_names(dentry, anon);
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08001816 swap(dentry->d_name.hash, anon->d_name.hash);
David Howells770bfad2006-08-22 20:06:07 -04001817
1818 dparent = dentry->d_parent;
1819 aparent = anon->d_parent;
1820
1821 dentry->d_parent = (aparent == anon) ? dentry : aparent;
1822 list_del(&dentry->d_u.d_child);
1823 if (!IS_ROOT(dentry))
1824 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
1825 else
1826 INIT_LIST_HEAD(&dentry->d_u.d_child);
1827
1828 anon->d_parent = (dparent == dentry) ? anon : dparent;
1829 list_del(&anon->d_u.d_child);
1830 if (!IS_ROOT(anon))
1831 list_add(&anon->d_u.d_child, &anon->d_parent->d_subdirs);
1832 else
1833 INIT_LIST_HEAD(&anon->d_u.d_child);
1834
1835 anon->d_flags &= ~DCACHE_DISCONNECTED;
1836}
1837
1838/**
1839 * d_materialise_unique - introduce an inode into the tree
1840 * @dentry: candidate dentry
1841 * @inode: inode to bind to the dentry, to which aliases may be attached
1842 *
1843 * Introduces an dentry into the tree, substituting an extant disconnected
1844 * root directory alias in its place if there is one
1845 */
1846struct dentry *d_materialise_unique(struct dentry *dentry, struct inode *inode)
1847{
Trond Myklebust9eaef272006-10-21 10:24:20 -07001848 struct dentry *actual;
David Howells770bfad2006-08-22 20:06:07 -04001849
1850 BUG_ON(!d_unhashed(dentry));
1851
1852 spin_lock(&dcache_lock);
1853
1854 if (!inode) {
1855 actual = dentry;
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001856 __d_instantiate(dentry, NULL);
David Howells770bfad2006-08-22 20:06:07 -04001857 goto found_lock;
1858 }
1859
Trond Myklebust9eaef272006-10-21 10:24:20 -07001860 if (S_ISDIR(inode->i_mode)) {
1861 struct dentry *alias;
David Howells770bfad2006-08-22 20:06:07 -04001862
Trond Myklebust9eaef272006-10-21 10:24:20 -07001863 /* Does an aliased dentry already exist? */
1864 alias = __d_find_alias(inode, 0);
1865 if (alias) {
1866 actual = alias;
1867 /* Is this an anonymous mountpoint that we could splice
1868 * into our tree? */
1869 if (IS_ROOT(alias)) {
1870 spin_lock(&alias->d_lock);
1871 __d_materialise_dentry(dentry, alias);
1872 __d_drop(alias);
1873 goto found;
1874 }
1875 /* Nope, but we must(!) avoid directory aliasing */
1876 actual = __d_unalias(dentry, alias);
1877 if (IS_ERR(actual))
1878 dput(alias);
1879 goto out_nolock;
1880 }
David Howells770bfad2006-08-22 20:06:07 -04001881 }
1882
1883 /* Add a unique reference */
1884 actual = __d_instantiate_unique(dentry, inode);
1885 if (!actual)
1886 actual = dentry;
1887 else if (unlikely(!d_unhashed(actual)))
1888 goto shouldnt_be_hashed;
1889
1890found_lock:
1891 spin_lock(&actual->d_lock);
1892found:
1893 _d_rehash(actual);
1894 spin_unlock(&actual->d_lock);
1895 spin_unlock(&dcache_lock);
Trond Myklebust9eaef272006-10-21 10:24:20 -07001896out_nolock:
David Howells770bfad2006-08-22 20:06:07 -04001897 if (actual == dentry) {
1898 security_d_instantiate(dentry, inode);
1899 return NULL;
1900 }
1901
1902 iput(inode);
1903 return actual;
1904
David Howells770bfad2006-08-22 20:06:07 -04001905shouldnt_be_hashed:
1906 spin_unlock(&dcache_lock);
1907 BUG();
David Howells770bfad2006-08-22 20:06:07 -04001908}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001909EXPORT_SYMBOL_GPL(d_materialise_unique);
David Howells770bfad2006-08-22 20:06:07 -04001910
Miklos Szeredicdd16d02008-06-23 18:11:53 +02001911static int prepend(char **buffer, int *buflen, const char *str, int namelen)
Ram Pai6092d042008-03-27 13:06:20 +01001912{
1913 *buflen -= namelen;
1914 if (*buflen < 0)
1915 return -ENAMETOOLONG;
1916 *buffer -= namelen;
1917 memcpy(*buffer, str, namelen);
1918 return 0;
1919}
1920
Miklos Szeredicdd16d02008-06-23 18:11:53 +02001921static int prepend_name(char **buffer, int *buflen, struct qstr *name)
1922{
1923 return prepend(buffer, buflen, name->name, name->len);
1924}
1925
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926/**
Miklos Szeredif2eb6572010-08-10 11:41:39 +02001927 * Prepend path string to a buffer
1928 *
1929 * @path: the dentry/vfsmount to report
1930 * @root: root vfsmnt/dentry (may be modified by this function)
1931 * @buffer: pointer to the end of the buffer
1932 * @buflen: pointer to buffer length
1933 *
1934 * Caller holds the dcache_lock.
1935 *
1936 * If path is not reachable from the supplied root, then the value of
1937 * root is changed (without modifying refcounts).
1938 */
1939static int prepend_path(const struct path *path, struct path *root,
1940 char **buffer, int *buflen)
1941{
1942 struct dentry *dentry = path->dentry;
1943 struct vfsmount *vfsmnt = path->mnt;
1944 bool slash = false;
1945 int error = 0;
1946
Nick Piggin99b7db72010-08-18 04:37:39 +10001947 br_read_lock(vfsmount_lock);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02001948 while (dentry != root->dentry || vfsmnt != root->mnt) {
1949 struct dentry * parent;
1950
1951 if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
1952 /* Global root? */
1953 if (vfsmnt->mnt_parent == vfsmnt) {
1954 goto global_root;
1955 }
1956 dentry = vfsmnt->mnt_mountpoint;
1957 vfsmnt = vfsmnt->mnt_parent;
1958 continue;
1959 }
1960 parent = dentry->d_parent;
1961 prefetch(parent);
1962 error = prepend_name(buffer, buflen, &dentry->d_name);
1963 if (!error)
1964 error = prepend(buffer, buflen, "/", 1);
1965 if (error)
1966 break;
1967
1968 slash = true;
1969 dentry = parent;
1970 }
1971
1972out:
1973 if (!error && !slash)
1974 error = prepend(buffer, buflen, "/", 1);
1975
Nick Piggin99b7db72010-08-18 04:37:39 +10001976 br_read_unlock(vfsmount_lock);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02001977 return error;
1978
1979global_root:
1980 /*
1981 * Filesystems needing to implement special "root names"
1982 * should do so with ->d_dname()
1983 */
1984 if (IS_ROOT(dentry) &&
1985 (dentry->d_name.len != 1 || dentry->d_name.name[0] != '/')) {
1986 WARN(1, "Root dentry has weird name <%.*s>\n",
1987 (int) dentry->d_name.len, dentry->d_name.name);
1988 }
1989 root->mnt = vfsmnt;
1990 root->dentry = dentry;
1991 goto out;
1992}
1993
1994/**
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +02001995 * __d_path - return the path of a dentry
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01001996 * @path: the dentry/vfsmount to report
1997 * @root: root vfsmnt/dentry (may be modified by this function)
Randy Dunlapcd956a12010-08-14 13:05:31 -07001998 * @buf: buffer to return value in
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 * @buflen: buffer length
2000 *
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002001 * Convert a dentry into an ASCII path name.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 *
Arjan van de Ven52afeef2008-12-01 14:35:00 -08002003 * Returns a pointer into the buffer or an error code if the
2004 * path was too long.
Linus Torvalds552ce542007-02-13 12:08:18 -08002005 *
Christoph Hellwigbe148242010-10-10 05:36:21 -04002006 * "buflen" should be positive.
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002007 *
2008 * If path is not reachable from the supplied root, then the value of
2009 * root is changed (without modifying refcounts).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 */
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002011char *__d_path(const struct path *path, struct path *root,
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002012 char *buf, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013{
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002014 char *res = buf + buflen;
2015 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002017 prepend(&res, &buflen, "\0", 1);
Christoph Hellwigbe148242010-10-10 05:36:21 -04002018 spin_lock(&dcache_lock);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002019 error = prepend_path(path, root, &res, &buflen);
Christoph Hellwigbe148242010-10-10 05:36:21 -04002020 spin_unlock(&dcache_lock);
2021
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002022 if (error)
2023 return ERR_PTR(error);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002024 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025}
2026
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002027/*
2028 * same as __d_path but appends "(deleted)" for unlinked files.
2029 */
2030static int path_with_deleted(const struct path *path, struct path *root,
2031 char **buf, int *buflen)
2032{
2033 prepend(buf, buflen, "\0", 1);
2034 if (d_unlinked(path->dentry)) {
2035 int error = prepend(buf, buflen, " (deleted)", 10);
2036 if (error)
2037 return error;
2038 }
2039
2040 return prepend_path(path, root, buf, buflen);
2041}
2042
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002043static int prepend_unreachable(char **buffer, int *buflen)
2044{
2045 return prepend(buffer, buflen, "(unreachable)", 13);
2046}
2047
Jan Bluncka03a8a702008-02-14 19:38:32 -08002048/**
2049 * d_path - return the path of a dentry
Jan Blunckcf28b482008-02-14 19:38:44 -08002050 * @path: path to report
Jan Bluncka03a8a702008-02-14 19:38:32 -08002051 * @buf: buffer to return value in
2052 * @buflen: buffer length
2053 *
2054 * Convert a dentry into an ASCII path name. If the entry has been deleted
2055 * the string " (deleted)" is appended. Note that this is ambiguous.
2056 *
Arjan van de Ven52afeef2008-12-01 14:35:00 -08002057 * Returns a pointer into the buffer or an error code if the path was
2058 * too long. Note: Callers should use the returned pointer, not the passed
2059 * in buffer, to use the name! The implementation often starts at an offset
2060 * into the buffer, and may leave 0 bytes at the start.
Jan Bluncka03a8a702008-02-14 19:38:32 -08002061 *
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +02002062 * "buflen" should be positive.
Jan Bluncka03a8a702008-02-14 19:38:32 -08002063 */
Jan Engelhardt20d4fdc2008-06-09 16:40:36 -07002064char *d_path(const struct path *path, char *buf, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065{
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002066 char *res = buf + buflen;
Jan Blunck6ac08c32008-02-14 19:34:38 -08002067 struct path root;
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002068 struct path tmp;
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002069 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
Eric Dumazetc23fbb62007-05-08 00:26:18 -07002071 /*
2072 * We have various synthetic filesystems that never get mounted. On
2073 * these filesystems dentries are never used for lookup purposes, and
2074 * thus don't need to be hashed. They also don't need a name until a
2075 * user wants to identify the object in /proc/pid/fd/. The little hack
2076 * below allows us to generate a name for these objects on demand:
2077 */
Jan Blunckcf28b482008-02-14 19:38:44 -08002078 if (path->dentry->d_op && path->dentry->d_op->d_dname)
2079 return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
Eric Dumazetc23fbb62007-05-08 00:26:18 -07002080
Miklos Szeredif7ad3c62010-08-10 11:41:36 +02002081 get_fs_root(current->fs, &root);
Linus Torvalds552ce542007-02-13 12:08:18 -08002082 spin_lock(&dcache_lock);
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002083 tmp = root;
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002084 error = path_with_deleted(path, &tmp, &res, &buflen);
2085 if (error)
2086 res = ERR_PTR(error);
Linus Torvalds552ce542007-02-13 12:08:18 -08002087 spin_unlock(&dcache_lock);
Jan Blunck6ac08c32008-02-14 19:34:38 -08002088 path_put(&root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 return res;
2090}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002091EXPORT_SYMBOL(d_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002093/**
2094 * d_path_with_unreachable - return the path of a dentry
2095 * @path: path to report
2096 * @buf: buffer to return value in
2097 * @buflen: buffer length
2098 *
2099 * The difference from d_path() is that this prepends "(unreachable)"
2100 * to paths which are unreachable from the current process' root.
2101 */
2102char *d_path_with_unreachable(const struct path *path, char *buf, int buflen)
2103{
2104 char *res = buf + buflen;
2105 struct path root;
2106 struct path tmp;
2107 int error;
2108
2109 if (path->dentry->d_op && path->dentry->d_op->d_dname)
2110 return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
2111
2112 get_fs_root(current->fs, &root);
2113 spin_lock(&dcache_lock);
2114 tmp = root;
2115 error = path_with_deleted(path, &tmp, &res, &buflen);
2116 if (!error && !path_equal(&tmp, &root))
2117 error = prepend_unreachable(&res, &buflen);
2118 spin_unlock(&dcache_lock);
2119 path_put(&root);
2120 if (error)
2121 res = ERR_PTR(error);
2122
2123 return res;
2124}
2125
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126/*
Eric Dumazetc23fbb62007-05-08 00:26:18 -07002127 * Helper function for dentry_operations.d_dname() members
2128 */
2129char *dynamic_dname(struct dentry *dentry, char *buffer, int buflen,
2130 const char *fmt, ...)
2131{
2132 va_list args;
2133 char temp[64];
2134 int sz;
2135
2136 va_start(args, fmt);
2137 sz = vsnprintf(temp, sizeof(temp), fmt, args) + 1;
2138 va_end(args);
2139
2140 if (sz > sizeof(temp) || sz > buflen)
2141 return ERR_PTR(-ENAMETOOLONG);
2142
2143 buffer += buflen - sz;
2144 return memcpy(buffer, temp, sz);
2145}
2146
2147/*
Ram Pai6092d042008-03-27 13:06:20 +01002148 * Write full pathname from the root of the filesystem into the buffer.
2149 */
Al Viroc1031352010-06-06 22:31:14 -04002150char *__dentry_path(struct dentry *dentry, char *buf, int buflen)
Ram Pai6092d042008-03-27 13:06:20 +01002151{
2152 char *end = buf + buflen;
2153 char *retval;
2154
Ram Pai6092d042008-03-27 13:06:20 +01002155 prepend(&end, &buflen, "\0", 1);
Ram Pai6092d042008-03-27 13:06:20 +01002156 if (buflen < 1)
2157 goto Elong;
2158 /* Get '/' right */
2159 retval = end-1;
2160 *retval = '/';
2161
Miklos Szeredicdd16d02008-06-23 18:11:53 +02002162 while (!IS_ROOT(dentry)) {
2163 struct dentry *parent = dentry->d_parent;
Ram Pai6092d042008-03-27 13:06:20 +01002164
Ram Pai6092d042008-03-27 13:06:20 +01002165 prefetch(parent);
Miklos Szeredicdd16d02008-06-23 18:11:53 +02002166 if ((prepend_name(&end, &buflen, &dentry->d_name) != 0) ||
Ram Pai6092d042008-03-27 13:06:20 +01002167 (prepend(&end, &buflen, "/", 1) != 0))
2168 goto Elong;
2169
2170 retval = end;
2171 dentry = parent;
2172 }
Al Viroc1031352010-06-06 22:31:14 -04002173 return retval;
2174Elong:
2175 return ERR_PTR(-ENAMETOOLONG);
2176}
2177EXPORT_SYMBOL(__dentry_path);
2178
2179char *dentry_path(struct dentry *dentry, char *buf, int buflen)
2180{
2181 char *p = NULL;
2182 char *retval;
2183
2184 spin_lock(&dcache_lock);
2185 if (d_unlinked(dentry)) {
2186 p = buf + buflen;
2187 if (prepend(&p, &buflen, "//deleted", 10) != 0)
2188 goto Elong;
2189 buflen++;
2190 }
2191 retval = __dentry_path(dentry, buf, buflen);
Ram Pai6092d042008-03-27 13:06:20 +01002192 spin_unlock(&dcache_lock);
Al Viroc1031352010-06-06 22:31:14 -04002193 if (!IS_ERR(retval) && p)
2194 *p = '/'; /* restore '/' overriden with '\0' */
Ram Pai6092d042008-03-27 13:06:20 +01002195 return retval;
2196Elong:
2197 spin_unlock(&dcache_lock);
2198 return ERR_PTR(-ENAMETOOLONG);
2199}
2200
2201/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 * NOTE! The user-level library version returns a
2203 * character pointer. The kernel system call just
2204 * returns the length of the buffer filled (which
2205 * includes the ending '\0' character), or a negative
2206 * error value. So libc would do something like
2207 *
2208 * char *getcwd(char * buf, size_t size)
2209 * {
2210 * int retval;
2211 *
2212 * retval = sys_getcwd(buf, size);
2213 * if (retval >= 0)
2214 * return buf;
2215 * errno = -retval;
2216 * return NULL;
2217 * }
2218 */
Heiko Carstens3cdad422009-01-14 14:14:22 +01002219SYSCALL_DEFINE2(getcwd, char __user *, buf, unsigned long, size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220{
Linus Torvalds552ce542007-02-13 12:08:18 -08002221 int error;
Jan Blunck6ac08c32008-02-14 19:34:38 -08002222 struct path pwd, root;
Linus Torvalds552ce542007-02-13 12:08:18 -08002223 char *page = (char *) __get_free_page(GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224
2225 if (!page)
2226 return -ENOMEM;
2227
Miklos Szeredif7ad3c62010-08-10 11:41:36 +02002228 get_fs_root_and_pwd(current->fs, &root, &pwd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229
Linus Torvalds552ce542007-02-13 12:08:18 -08002230 error = -ENOENT;
Linus Torvalds552ce542007-02-13 12:08:18 -08002231 spin_lock(&dcache_lock);
Alexey Dobriyanf3da3922009-05-04 03:32:03 +04002232 if (!d_unlinked(pwd.dentry)) {
Linus Torvalds552ce542007-02-13 12:08:18 -08002233 unsigned long len;
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002234 struct path tmp = root;
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002235 char *cwd = page + PAGE_SIZE;
2236 int buflen = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002238 prepend(&cwd, &buflen, "\0", 1);
2239 error = prepend_path(&pwd, &tmp, &cwd, &buflen);
Linus Torvalds552ce542007-02-13 12:08:18 -08002240 spin_unlock(&dcache_lock);
2241
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002242 if (error)
Linus Torvalds552ce542007-02-13 12:08:18 -08002243 goto out;
2244
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002245 /* Unreachable from current root */
2246 if (!path_equal(&tmp, &root)) {
2247 error = prepend_unreachable(&cwd, &buflen);
2248 if (error)
2249 goto out;
2250 }
2251
Linus Torvalds552ce542007-02-13 12:08:18 -08002252 error = -ERANGE;
2253 len = PAGE_SIZE + page - cwd;
2254 if (len <= size) {
2255 error = len;
2256 if (copy_to_user(buf, cwd, len))
2257 error = -EFAULT;
2258 }
2259 } else
2260 spin_unlock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261
2262out:
Jan Blunck6ac08c32008-02-14 19:34:38 -08002263 path_put(&pwd);
2264 path_put(&root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 free_page((unsigned long) page);
2266 return error;
2267}
2268
2269/*
2270 * Test whether new_dentry is a subdirectory of old_dentry.
2271 *
2272 * Trivially implemented using the dcache structure
2273 */
2274
2275/**
2276 * is_subdir - is new dentry a subdirectory of old_dentry
2277 * @new_dentry: new dentry
2278 * @old_dentry: old dentry
2279 *
2280 * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
2281 * Returns 0 otherwise.
2282 * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
2283 */
2284
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002285int is_subdir(struct dentry *new_dentry, struct dentry *old_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286{
2287 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 unsigned long seq;
2289
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002290 if (new_dentry == old_dentry)
2291 return 1;
2292
2293 /*
2294 * Need rcu_readlock to protect against the d_parent trashing
2295 * due to d_move
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 */
2297 rcu_read_lock();
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002298 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 /* for restarting inner loop in case of seq retry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 seq = read_seqbegin(&rename_lock);
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002301 if (d_ancestor(old_dentry, new_dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 result = 1;
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002303 else
2304 result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 } while (read_seqretry(&rename_lock, seq));
2306 rcu_read_unlock();
2307
2308 return result;
2309}
2310
Al Viro2096f752010-01-30 13:16:21 -05002311int path_is_under(struct path *path1, struct path *path2)
2312{
2313 struct vfsmount *mnt = path1->mnt;
2314 struct dentry *dentry = path1->dentry;
2315 int res;
Nick Piggin99b7db72010-08-18 04:37:39 +10002316
2317 br_read_lock(vfsmount_lock);
Al Viro2096f752010-01-30 13:16:21 -05002318 if (mnt != path2->mnt) {
2319 for (;;) {
2320 if (mnt->mnt_parent == mnt) {
Nick Piggin99b7db72010-08-18 04:37:39 +10002321 br_read_unlock(vfsmount_lock);
Al Viro2096f752010-01-30 13:16:21 -05002322 return 0;
2323 }
2324 if (mnt->mnt_parent == path2->mnt)
2325 break;
2326 mnt = mnt->mnt_parent;
2327 }
2328 dentry = mnt->mnt_mountpoint;
2329 }
2330 res = is_subdir(dentry, path2->dentry);
Nick Piggin99b7db72010-08-18 04:37:39 +10002331 br_read_unlock(vfsmount_lock);
Al Viro2096f752010-01-30 13:16:21 -05002332 return res;
2333}
2334EXPORT_SYMBOL(path_is_under);
2335
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336void d_genocide(struct dentry *root)
2337{
2338 struct dentry *this_parent = root;
2339 struct list_head *next;
2340
2341 spin_lock(&dcache_lock);
2342repeat:
2343 next = this_parent->d_subdirs.next;
2344resume:
2345 while (next != &this_parent->d_subdirs) {
2346 struct list_head *tmp = next;
Eric Dumazet5160ee62006-01-08 01:03:32 -08002347 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 next = tmp->next;
2349 if (d_unhashed(dentry)||!dentry->d_inode)
2350 continue;
2351 if (!list_empty(&dentry->d_subdirs)) {
2352 this_parent = dentry;
2353 goto repeat;
2354 }
2355 atomic_dec(&dentry->d_count);
2356 }
2357 if (this_parent != root) {
Eric Dumazet5160ee62006-01-08 01:03:32 -08002358 next = this_parent->d_u.d_child.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 atomic_dec(&this_parent->d_count);
2360 this_parent = this_parent->d_parent;
2361 goto resume;
2362 }
2363 spin_unlock(&dcache_lock);
2364}
2365
2366/**
2367 * find_inode_number - check for dentry with name
2368 * @dir: directory to check
2369 * @name: Name to find.
2370 *
2371 * Check whether a dentry already exists for the given name,
2372 * and return the inode number if it has an inode. Otherwise
2373 * 0 is returned.
2374 *
2375 * This routine is used to post-process directory listings for
2376 * filesystems using synthetic inode numbers, and is necessary
2377 * to keep getcwd() working.
2378 */
2379
2380ino_t find_inode_number(struct dentry *dir, struct qstr *name)
2381{
2382 struct dentry * dentry;
2383 ino_t ino = 0;
2384
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08002385 dentry = d_hash_and_lookup(dir, name);
2386 if (dentry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 if (dentry->d_inode)
2388 ino = dentry->d_inode->i_ino;
2389 dput(dentry);
2390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 return ino;
2392}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002393EXPORT_SYMBOL(find_inode_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394
2395static __initdata unsigned long dhash_entries;
2396static int __init set_dhash_entries(char *str)
2397{
2398 if (!str)
2399 return 0;
2400 dhash_entries = simple_strtoul(str, &str, 0);
2401 return 1;
2402}
2403__setup("dhash_entries=", set_dhash_entries);
2404
2405static void __init dcache_init_early(void)
2406{
2407 int loop;
2408
2409 /* If hashes are distributed across NUMA nodes, defer
2410 * hash allocation until vmalloc space is available.
2411 */
2412 if (hashdist)
2413 return;
2414
2415 dentry_hashtable =
2416 alloc_large_system_hash("Dentry cache",
2417 sizeof(struct hlist_head),
2418 dhash_entries,
2419 13,
2420 HASH_EARLY,
2421 &d_hash_shift,
2422 &d_hash_mask,
2423 0);
2424
2425 for (loop = 0; loop < (1 << d_hash_shift); loop++)
2426 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
2427}
2428
Denis Cheng74bf17c2007-10-16 23:26:30 -07002429static void __init dcache_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430{
2431 int loop;
2432
Christoph Hellwig312d3ca2010-10-10 05:36:23 -04002433 percpu_counter_init(&nr_dentry, 0);
2434 percpu_counter_init(&nr_dentry_unused, 0);
2435
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 /*
2437 * A constructor could be added for stable state like the lists,
2438 * but it is probably not worth it because of the cache nature
2439 * of the dcache.
2440 */
Christoph Lameter0a31bd52007-05-06 14:49:57 -07002441 dentry_cache = KMEM_CACHE(dentry,
2442 SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|SLAB_MEM_SPREAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443
Rusty Russell8e1f9362007-07-17 04:03:17 -07002444 register_shrinker(&dcache_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445
2446 /* Hash may have been set up in dcache_init_early */
2447 if (!hashdist)
2448 return;
2449
2450 dentry_hashtable =
2451 alloc_large_system_hash("Dentry cache",
2452 sizeof(struct hlist_head),
2453 dhash_entries,
2454 13,
2455 0,
2456 &d_hash_shift,
2457 &d_hash_mask,
2458 0);
2459
2460 for (loop = 0; loop < (1 << d_hash_shift); loop++)
2461 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
2462}
2463
2464/* SLAB cache for __getname() consumers */
Christoph Lametere18b8902006-12-06 20:33:20 -08002465struct kmem_cache *names_cachep __read_mostly;
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002466EXPORT_SYMBOL(names_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468EXPORT_SYMBOL(d_genocide);
2469
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470void __init vfs_caches_init_early(void)
2471{
2472 dcache_init_early();
2473 inode_init_early();
2474}
2475
2476void __init vfs_caches_init(unsigned long mempages)
2477{
2478 unsigned long reserve;
2479
2480 /* Base hash sizes on available memory, with a reserve equal to
2481 150% of current kernel size */
2482
2483 reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);
2484 mempages -= reserve;
2485
2486 names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09002487 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488
Denis Cheng74bf17c2007-10-16 23:26:30 -07002489 dcache_init();
2490 inode_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 files_init(mempages);
Denis Cheng74bf17c2007-10-16 23:26:30 -07002492 mnt_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 bdev_cache_init();
2494 chrdev_init();
2495}