blob: e987ad576a397c79d13f973538985578896b615b [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
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400462static void shrink_dentry_list(struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700464 struct dentry *dentry;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700465
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400466 while (!list_empty(list)) {
467 dentry = list_entry(list->prev, struct dentry, d_lru);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700468 dentry_lru_del_init(dentry);
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 /*
471 * We found an inuse dentry which was not removed from
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700472 * the LRU because of laziness during lookup. Do not free
473 * it - just keep it off the LRU list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 */
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400475 spin_lock(&dentry->d_lock);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700476 if (atomic_read(&dentry->d_count)) {
477 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 continue;
479 }
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700480 prune_one_dentry(dentry);
481 /* dentry->d_lock was dropped in prune_one_dentry() */
482 cond_resched_lock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 }
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400484}
485
486/**
487 * __shrink_dcache_sb - shrink the dentry LRU on a given superblock
488 * @sb: superblock to shrink dentry LRU.
489 * @count: number of entries to prune
490 * @flags: flags to control the dentry processing
491 *
492 * If flags contains DCACHE_REFERENCED reference dentries will not be pruned.
493 */
494static void __shrink_dcache_sb(struct super_block *sb, int *count, int flags)
495{
496 /* called from prune_dcache() and shrink_dcache_parent() */
497 struct dentry *dentry;
498 LIST_HEAD(referenced);
499 LIST_HEAD(tmp);
500 int cnt = *count;
501
502 spin_lock(&dcache_lock);
503 while (!list_empty(&sb->s_dentry_lru)) {
504 dentry = list_entry(sb->s_dentry_lru.prev,
505 struct dentry, d_lru);
506 BUG_ON(dentry->d_sb != sb);
507
508 /*
509 * If we are honouring the DCACHE_REFERENCED flag and the
510 * dentry has this flag set, don't free it. Clear the flag
511 * and put it back on the LRU.
512 */
513 if (flags & DCACHE_REFERENCED) {
514 spin_lock(&dentry->d_lock);
515 if (dentry->d_flags & DCACHE_REFERENCED) {
516 dentry->d_flags &= ~DCACHE_REFERENCED;
517 list_move(&dentry->d_lru, &referenced);
518 spin_unlock(&dentry->d_lock);
519 cond_resched_lock(&dcache_lock);
520 continue;
521 }
522 spin_unlock(&dentry->d_lock);
523 }
524
525 list_move_tail(&dentry->d_lru, &tmp);
526 if (!--cnt)
527 break;
528 cond_resched_lock(&dcache_lock);
529 }
530
531 *count = cnt;
532 shrink_dentry_list(&tmp);
533
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700534 if (!list_empty(&referenced))
535 list_splice(&referenced, &sb->s_dentry_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 spin_unlock(&dcache_lock);
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538}
539
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700540/**
541 * prune_dcache - shrink the dcache
542 * @count: number of entries to try to free
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 *
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700544 * Shrink the dcache. This is done when we need more memory, or simply when we
545 * need to unmount something (at which point we need to unuse all dentries).
546 *
547 * This function may fail to free any resources if all the dentries are in use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 */
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700549static void prune_dcache(int count)
550{
Al Virodca33252010-07-25 02:31:46 +0400551 struct super_block *sb, *p = NULL;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700552 int w_count;
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400553 int unused = percpu_counter_sum_positive(&nr_dentry_unused);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700554 int prune_ratio;
555 int pruned;
556
557 if (unused == 0 || count == 0)
558 return;
559 spin_lock(&dcache_lock);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700560 if (count >= unused)
561 prune_ratio = 1;
562 else
563 prune_ratio = unused / count;
564 spin_lock(&sb_lock);
Al Virodca33252010-07-25 02:31:46 +0400565 list_for_each_entry(sb, &super_blocks, s_list) {
Al Viro551de6f2010-03-22 19:36:35 -0400566 if (list_empty(&sb->s_instances))
567 continue;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700568 if (sb->s_nr_dentry_unused == 0)
569 continue;
570 sb->s_count++;
571 /* Now, we reclaim unused dentrins with fairness.
572 * We reclaim them same percentage from each superblock.
573 * We calculate number of dentries to scan on this sb
574 * as follows, but the implementation is arranged to avoid
575 * overflows:
576 * number of dentries to scan on this sb =
577 * count * (number of dentries on this sb /
578 * number of dentries in the machine)
579 */
580 spin_unlock(&sb_lock);
581 if (prune_ratio != 1)
582 w_count = (sb->s_nr_dentry_unused / prune_ratio) + 1;
583 else
584 w_count = sb->s_nr_dentry_unused;
585 pruned = w_count;
586 /*
587 * We need to be sure this filesystem isn't being unmounted,
588 * otherwise we could race with generic_shutdown_super(), and
589 * end up holding a reference to an inode while the filesystem
590 * is unmounted. So we try to get s_umount, and make sure
591 * s_root isn't NULL.
592 */
593 if (down_read_trylock(&sb->s_umount)) {
594 if ((sb->s_root != NULL) &&
595 (!list_empty(&sb->s_dentry_lru))) {
596 spin_unlock(&dcache_lock);
597 __shrink_dcache_sb(sb, &w_count,
598 DCACHE_REFERENCED);
599 pruned -= w_count;
600 spin_lock(&dcache_lock);
601 }
602 up_read(&sb->s_umount);
603 }
604 spin_lock(&sb_lock);
Al Virodca33252010-07-25 02:31:46 +0400605 if (p)
606 __put_super(p);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700607 count -= pruned;
Al Virodca33252010-07-25 02:31:46 +0400608 p = sb;
Al Viro79893c12010-03-22 20:27:55 -0400609 /* more work left to do? */
610 if (count <= 0)
611 break;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700612 }
Al Virodca33252010-07-25 02:31:46 +0400613 if (p)
614 __put_super(p);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700615 spin_unlock(&sb_lock);
616 spin_unlock(&dcache_lock);
617}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619/**
620 * shrink_dcache_sb - shrink dcache for a superblock
621 * @sb: superblock
622 *
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400623 * Shrink the dcache for the specified super block. This is used to free
624 * the dcache before unmounting a file system.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 */
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400626void shrink_dcache_sb(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400628 LIST_HEAD(tmp);
629
630 spin_lock(&dcache_lock);
631 while (!list_empty(&sb->s_dentry_lru)) {
632 list_splice_init(&sb->s_dentry_lru, &tmp);
633 shrink_dentry_list(&tmp);
634 }
635 spin_unlock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700637EXPORT_SYMBOL(shrink_dcache_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639/*
David Howellsc636ebd2006-10-11 01:22:19 -0700640 * destroy a single subtree of dentries for unmount
641 * - see the comments on shrink_dcache_for_umount() for a description of the
642 * locking
643 */
644static void shrink_dcache_for_umount_subtree(struct dentry *dentry)
645{
646 struct dentry *parent;
David Howellsf8713572006-10-28 10:38:46 -0700647 unsigned detached = 0;
David Howellsc636ebd2006-10-11 01:22:19 -0700648
649 BUG_ON(!IS_ROOT(dentry));
650
651 /* detach this root from the system */
652 spin_lock(&dcache_lock);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700653 dentry_lru_del_init(dentry);
David Howellsc636ebd2006-10-11 01:22:19 -0700654 __d_drop(dentry);
655 spin_unlock(&dcache_lock);
656
657 for (;;) {
658 /* descend to the first leaf in the current subtree */
659 while (!list_empty(&dentry->d_subdirs)) {
660 struct dentry *loop;
661
662 /* this is a branch with children - detach all of them
663 * from the system in one go */
664 spin_lock(&dcache_lock);
665 list_for_each_entry(loop, &dentry->d_subdirs,
666 d_u.d_child) {
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700667 dentry_lru_del_init(loop);
David Howellsc636ebd2006-10-11 01:22:19 -0700668 __d_drop(loop);
669 cond_resched_lock(&dcache_lock);
670 }
671 spin_unlock(&dcache_lock);
672
673 /* move to the first child */
674 dentry = list_entry(dentry->d_subdirs.next,
675 struct dentry, d_u.d_child);
676 }
677
678 /* consume the dentries from this leaf up through its parents
679 * until we find one with children or run out altogether */
680 do {
681 struct inode *inode;
682
683 if (atomic_read(&dentry->d_count) != 0) {
684 printk(KERN_ERR
685 "BUG: Dentry %p{i=%lx,n=%s}"
686 " still in use (%d)"
687 " [unmount of %s %s]\n",
688 dentry,
689 dentry->d_inode ?
690 dentry->d_inode->i_ino : 0UL,
691 dentry->d_name.name,
692 atomic_read(&dentry->d_count),
693 dentry->d_sb->s_type->name,
694 dentry->d_sb->s_id);
695 BUG();
696 }
697
OGAWA Hirofumi871c0062008-10-16 07:50:27 +0900698 if (IS_ROOT(dentry))
David Howellsc636ebd2006-10-11 01:22:19 -0700699 parent = NULL;
OGAWA Hirofumi871c0062008-10-16 07:50:27 +0900700 else {
701 parent = dentry->d_parent;
David Howellsc636ebd2006-10-11 01:22:19 -0700702 atomic_dec(&parent->d_count);
OGAWA Hirofumi871c0062008-10-16 07:50:27 +0900703 }
David Howellsc636ebd2006-10-11 01:22:19 -0700704
705 list_del(&dentry->d_u.d_child);
David Howellsf8713572006-10-28 10:38:46 -0700706 detached++;
David Howellsc636ebd2006-10-11 01:22:19 -0700707
708 inode = dentry->d_inode;
709 if (inode) {
710 dentry->d_inode = NULL;
711 list_del_init(&dentry->d_alias);
712 if (dentry->d_op && dentry->d_op->d_iput)
713 dentry->d_op->d_iput(dentry, inode);
714 else
715 iput(inode);
716 }
717
718 d_free(dentry);
719
720 /* finished when we fall off the top of the tree,
721 * otherwise we ascend to the parent and move to the
722 * next sibling if there is one */
723 if (!parent)
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400724 return;
David Howellsc636ebd2006-10-11 01:22:19 -0700725 dentry = parent;
David Howellsc636ebd2006-10-11 01:22:19 -0700726 } while (list_empty(&dentry->d_subdirs));
727
728 dentry = list_entry(dentry->d_subdirs.next,
729 struct dentry, d_u.d_child);
730 }
731}
732
733/*
734 * destroy the dentries attached to a superblock on unmounting
735 * - we don't need to use dentry->d_lock, and only need dcache_lock when
736 * removing the dentry from the system lists and hashes because:
737 * - the superblock is detached from all mountings and open files, so the
738 * dentry trees will not be rearranged by the VFS
739 * - s_umount is write-locked, so the memory pressure shrinker will ignore
740 * any dentries belonging to this superblock that it comes across
741 * - the filesystem itself is no longer permitted to rearrange the dentries
742 * in this superblock
743 */
744void shrink_dcache_for_umount(struct super_block *sb)
745{
746 struct dentry *dentry;
747
748 if (down_read_trylock(&sb->s_umount))
749 BUG();
750
751 dentry = sb->s_root;
752 sb->s_root = NULL;
753 atomic_dec(&dentry->d_count);
754 shrink_dcache_for_umount_subtree(dentry);
755
756 while (!hlist_empty(&sb->s_anon)) {
757 dentry = hlist_entry(sb->s_anon.first, struct dentry, d_hash);
758 shrink_dcache_for_umount_subtree(dentry);
759 }
760}
761
762/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 * Search for at least 1 mount point in the dentry's subdirs.
764 * We descend to the next level whenever the d_subdirs
765 * list is non-empty and continue searching.
766 */
767
768/**
769 * have_submounts - check for mounts over a dentry
770 * @parent: dentry to check.
771 *
772 * Return true if the parent or its subdirectories contain
773 * a mount point
774 */
775
776int have_submounts(struct dentry *parent)
777{
778 struct dentry *this_parent = parent;
779 struct list_head *next;
780
781 spin_lock(&dcache_lock);
782 if (d_mountpoint(parent))
783 goto positive;
784repeat:
785 next = this_parent->d_subdirs.next;
786resume:
787 while (next != &this_parent->d_subdirs) {
788 struct list_head *tmp = next;
Eric Dumazet5160ee62006-01-08 01:03:32 -0800789 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 next = tmp->next;
791 /* Have we found a mount point ? */
792 if (d_mountpoint(dentry))
793 goto positive;
794 if (!list_empty(&dentry->d_subdirs)) {
795 this_parent = dentry;
796 goto repeat;
797 }
798 }
799 /*
800 * All done at this level ... ascend and resume the search.
801 */
802 if (this_parent != parent) {
Eric Dumazet5160ee62006-01-08 01:03:32 -0800803 next = this_parent->d_u.d_child.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 this_parent = this_parent->d_parent;
805 goto resume;
806 }
807 spin_unlock(&dcache_lock);
808 return 0; /* No mount points found in tree */
809positive:
810 spin_unlock(&dcache_lock);
811 return 1;
812}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700813EXPORT_SYMBOL(have_submounts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
815/*
816 * Search the dentry child list for the specified parent,
817 * and move any unused dentries to the end of the unused
818 * list for prune_dcache(). We descend to the next level
819 * whenever the d_subdirs list is non-empty and continue
820 * searching.
821 *
822 * It returns zero iff there are no unused children,
823 * otherwise it returns the number of children moved to
824 * the end of the unused list. This may not be the total
825 * number of unused children, because select_parent can
826 * drop the lock and return early due to latency
827 * constraints.
828 */
829static int select_parent(struct dentry * parent)
830{
831 struct dentry *this_parent = parent;
832 struct list_head *next;
833 int found = 0;
834
835 spin_lock(&dcache_lock);
836repeat:
837 next = this_parent->d_subdirs.next;
838resume:
839 while (next != &this_parent->d_subdirs) {
840 struct list_head *tmp = next;
Eric Dumazet5160ee62006-01-08 01:03:32 -0800841 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 next = tmp->next;
843
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700844 dentry_lru_del_init(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 /*
846 * move only zero ref count dentries to the end
847 * of the unused list for prune_dcache
848 */
849 if (!atomic_read(&dentry->d_count)) {
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700850 dentry_lru_add_tail(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 found++;
852 }
853
854 /*
855 * We can return to the caller if we have found some (this
856 * ensures forward progress). We'll be coming back to find
857 * the rest.
858 */
859 if (found && need_resched())
860 goto out;
861
862 /*
863 * Descend a level if the d_subdirs list is non-empty.
864 */
865 if (!list_empty(&dentry->d_subdirs)) {
866 this_parent = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 goto repeat;
868 }
869 }
870 /*
871 * All done at this level ... ascend and resume the search.
872 */
873 if (this_parent != parent) {
Eric Dumazet5160ee62006-01-08 01:03:32 -0800874 next = this_parent->d_u.d_child.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 this_parent = this_parent->d_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 goto resume;
877 }
878out:
879 spin_unlock(&dcache_lock);
880 return found;
881}
882
883/**
884 * shrink_dcache_parent - prune dcache
885 * @parent: parent of entries to prune
886 *
887 * Prune the dcache to remove unused children of the parent dentry.
888 */
889
890void shrink_dcache_parent(struct dentry * parent)
891{
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700892 struct super_block *sb = parent->d_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 int found;
894
895 while ((found = select_parent(parent)) != 0)
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700896 __shrink_dcache_sb(sb, &found, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700898EXPORT_SYMBOL(shrink_dcache_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900/*
901 * Scan `nr' dentries and return the number which remain.
902 *
903 * We need to avoid reentering the filesystem if the caller is performing a
904 * GFP_NOFS allocation attempt. One example deadlock is:
905 *
906 * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache->
907 * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->put_inode->
908 * ext2_discard_prealloc->ext2_free_blocks->lock_super->DEADLOCK.
909 *
910 * In this case we return -1 to tell the caller that we baled.
911 */
Dave Chinner7f8275d2010-07-19 14:56:17 +1000912static int shrink_dcache_memory(struct shrinker *shrink, int nr, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400914 int nr_unused;
915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 if (nr) {
917 if (!(gfp_mask & __GFP_FS))
918 return -1;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700919 prune_dcache(nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 }
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400921
922 nr_unused = percpu_counter_sum_positive(&nr_dentry_unused);
923 return (nr_unused / 100) * sysctl_vfs_cache_pressure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924}
925
Rusty Russell8e1f9362007-07-17 04:03:17 -0700926static struct shrinker dcache_shrinker = {
927 .shrink = shrink_dcache_memory,
928 .seeks = DEFAULT_SEEKS,
929};
930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931/**
932 * d_alloc - allocate a dcache entry
933 * @parent: parent of entry to allocate
934 * @name: qstr of the name
935 *
936 * Allocates a dentry. It returns %NULL if there is insufficient memory
937 * available. On a success the dentry is returned. The name passed in is
938 * copied and the copy passed in may be reused after this call.
939 */
940
941struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
942{
943 struct dentry *dentry;
944 char *dname;
945
Mel Gormane12ba742007-10-16 01:25:52 -0700946 dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 if (!dentry)
948 return NULL;
949
950 if (name->len > DNAME_INLINE_LEN-1) {
951 dname = kmalloc(name->len + 1, GFP_KERNEL);
952 if (!dname) {
953 kmem_cache_free(dentry_cache, dentry);
954 return NULL;
955 }
956 } else {
957 dname = dentry->d_iname;
958 }
959 dentry->d_name.name = dname;
960
961 dentry->d_name.len = name->len;
962 dentry->d_name.hash = name->hash;
963 memcpy(dname, name->name, name->len);
964 dname[name->len] = 0;
965
966 atomic_set(&dentry->d_count, 1);
967 dentry->d_flags = DCACHE_UNHASHED;
968 spin_lock_init(&dentry->d_lock);
969 dentry->d_inode = NULL;
970 dentry->d_parent = NULL;
971 dentry->d_sb = NULL;
972 dentry->d_op = NULL;
973 dentry->d_fsdata = NULL;
974 dentry->d_mounted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 INIT_HLIST_NODE(&dentry->d_hash);
976 INIT_LIST_HEAD(&dentry->d_lru);
977 INIT_LIST_HEAD(&dentry->d_subdirs);
978 INIT_LIST_HEAD(&dentry->d_alias);
979
980 if (parent) {
981 dentry->d_parent = dget(parent);
982 dentry->d_sb = parent->d_sb;
983 } else {
Eric Dumazet5160ee62006-01-08 01:03:32 -0800984 INIT_LIST_HEAD(&dentry->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 }
986
987 spin_lock(&dcache_lock);
988 if (parent)
Eric Dumazet5160ee62006-01-08 01:03:32 -0800989 list_add(&dentry->d_u.d_child, &parent->d_subdirs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 spin_unlock(&dcache_lock);
991
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400992 percpu_counter_inc(&nr_dentry);
993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 return dentry;
995}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700996EXPORT_SYMBOL(d_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
998struct dentry *d_alloc_name(struct dentry *parent, const char *name)
999{
1000 struct qstr q;
1001
1002 q.name = name;
1003 q.len = strlen(name);
1004 q.hash = full_name_hash(q.name, q.len);
1005 return d_alloc(parent, &q);
1006}
H Hartley Sweetenef26ca92009-09-29 20:09:42 -04001007EXPORT_SYMBOL(d_alloc_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001009/* the caller must hold dcache_lock */
1010static void __d_instantiate(struct dentry *dentry, struct inode *inode)
1011{
1012 if (inode)
1013 list_add(&dentry->d_alias, &inode->i_dentry);
1014 dentry->d_inode = inode;
1015 fsnotify_d_instantiate(dentry, inode);
1016}
1017
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018/**
1019 * d_instantiate - fill in inode information for a dentry
1020 * @entry: dentry to complete
1021 * @inode: inode to attach to this dentry
1022 *
1023 * Fill in inode information in the entry.
1024 *
1025 * This turns negative dentries into productive full members
1026 * of society.
1027 *
1028 * NOTE! This assumes that the inode count has been incremented
1029 * (or otherwise set) by the caller to indicate that it is now
1030 * in use by the dcache.
1031 */
1032
1033void d_instantiate(struct dentry *entry, struct inode * inode)
1034{
Eric Sesterhenn28133c72006-03-26 18:25:39 +02001035 BUG_ON(!list_empty(&entry->d_alias));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 spin_lock(&dcache_lock);
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001037 __d_instantiate(entry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 spin_unlock(&dcache_lock);
1039 security_d_instantiate(entry, inode);
1040}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001041EXPORT_SYMBOL(d_instantiate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
1043/**
1044 * d_instantiate_unique - instantiate a non-aliased dentry
1045 * @entry: dentry to instantiate
1046 * @inode: inode to attach to this dentry
1047 *
1048 * Fill in inode information in the entry. On success, it returns NULL.
1049 * If an unhashed alias of "entry" already exists, then we return the
Oleg Drokine866cfa2006-01-09 20:52:51 -08001050 * aliased dentry instead and drop one reference to inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 *
1052 * Note that in order to avoid conflicts with rename() etc, the caller
1053 * had better be holding the parent directory semaphore.
Oleg Drokine866cfa2006-01-09 20:52:51 -08001054 *
1055 * This also assumes that the inode count has been incremented
1056 * (or otherwise set) by the caller to indicate that it is now
1057 * in use by the dcache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 */
David Howells770bfad2006-08-22 20:06:07 -04001059static struct dentry *__d_instantiate_unique(struct dentry *entry,
1060 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061{
1062 struct dentry *alias;
1063 int len = entry->d_name.len;
1064 const char *name = entry->d_name.name;
1065 unsigned int hash = entry->d_name.hash;
1066
David Howells770bfad2006-08-22 20:06:07 -04001067 if (!inode) {
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001068 __d_instantiate(entry, NULL);
David Howells770bfad2006-08-22 20:06:07 -04001069 return NULL;
1070 }
1071
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 list_for_each_entry(alias, &inode->i_dentry, d_alias) {
1073 struct qstr *qstr = &alias->d_name;
1074
1075 if (qstr->hash != hash)
1076 continue;
1077 if (alias->d_parent != entry->d_parent)
1078 continue;
1079 if (qstr->len != len)
1080 continue;
1081 if (memcmp(qstr->name, name, len))
1082 continue;
1083 dget_locked(alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 return alias;
1085 }
David Howells770bfad2006-08-22 20:06:07 -04001086
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001087 __d_instantiate(entry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 return NULL;
1089}
David Howells770bfad2006-08-22 20:06:07 -04001090
1091struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode)
1092{
1093 struct dentry *result;
1094
1095 BUG_ON(!list_empty(&entry->d_alias));
1096
1097 spin_lock(&dcache_lock);
1098 result = __d_instantiate_unique(entry, inode);
1099 spin_unlock(&dcache_lock);
1100
1101 if (!result) {
1102 security_d_instantiate(entry, inode);
1103 return NULL;
1104 }
1105
1106 BUG_ON(!d_unhashed(result));
1107 iput(inode);
1108 return result;
1109}
1110
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111EXPORT_SYMBOL(d_instantiate_unique);
1112
1113/**
1114 * d_alloc_root - allocate root dentry
1115 * @root_inode: inode to allocate the root for
1116 *
1117 * Allocate a root ("/") dentry for the inode given. The inode is
1118 * instantiated and returned. %NULL is returned if there is insufficient
1119 * memory or the inode passed is %NULL.
1120 */
1121
1122struct dentry * d_alloc_root(struct inode * root_inode)
1123{
1124 struct dentry *res = NULL;
1125
1126 if (root_inode) {
1127 static const struct qstr name = { .name = "/", .len = 1 };
1128
1129 res = d_alloc(NULL, &name);
1130 if (res) {
1131 res->d_sb = root_inode->i_sb;
1132 res->d_parent = res;
1133 d_instantiate(res, root_inode);
1134 }
1135 }
1136 return res;
1137}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001138EXPORT_SYMBOL(d_alloc_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
1140static inline struct hlist_head *d_hash(struct dentry *parent,
1141 unsigned long hash)
1142{
1143 hash += ((unsigned long) parent ^ GOLDEN_RATIO_PRIME) / L1_CACHE_BYTES;
1144 hash = hash ^ ((hash ^ GOLDEN_RATIO_PRIME) >> D_HASHBITS);
1145 return dentry_hashtable + (hash & D_HASHMASK);
1146}
1147
1148/**
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001149 * d_obtain_alias - find or allocate a dentry for a given inode
1150 * @inode: inode to allocate the dentry for
1151 *
1152 * Obtain a dentry for an inode resulting from NFS filehandle conversion or
1153 * similar open by handle operations. The returned dentry may be anonymous,
1154 * or may have a full name (if the inode was already in the cache).
1155 *
1156 * When called on a directory inode, we must ensure that the inode only ever
1157 * has one dentry. If a dentry is found, that is returned instead of
1158 * allocating a new one.
1159 *
1160 * On successful return, the reference to the inode has been transferred
Christoph Hellwig44003722008-08-11 15:49:04 +02001161 * to the dentry. In case of an error the reference on the inode is released.
1162 * To make it easier to use in export operations a %NULL or IS_ERR inode may
1163 * be passed in and will be the error will be propagate to the return value,
1164 * with a %NULL @inode replaced by ERR_PTR(-ESTALE).
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001165 */
1166struct dentry *d_obtain_alias(struct inode *inode)
1167{
Christoph Hellwig9308a612008-08-11 15:49:12 +02001168 static const struct qstr anonstring = { .name = "" };
1169 struct dentry *tmp;
1170 struct dentry *res;
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001171
1172 if (!inode)
Christoph Hellwig44003722008-08-11 15:49:04 +02001173 return ERR_PTR(-ESTALE);
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001174 if (IS_ERR(inode))
1175 return ERR_CAST(inode);
1176
Christoph Hellwig9308a612008-08-11 15:49:12 +02001177 res = d_find_alias(inode);
1178 if (res)
1179 goto out_iput;
1180
1181 tmp = d_alloc(NULL, &anonstring);
1182 if (!tmp) {
1183 res = ERR_PTR(-ENOMEM);
1184 goto out_iput;
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001185 }
Christoph Hellwig9308a612008-08-11 15:49:12 +02001186 tmp->d_parent = tmp; /* make sure dput doesn't croak */
1187
1188 spin_lock(&dcache_lock);
1189 res = __d_find_alias(inode, 0);
1190 if (res) {
1191 spin_unlock(&dcache_lock);
1192 dput(tmp);
1193 goto out_iput;
1194 }
1195
1196 /* attach a disconnected dentry */
1197 spin_lock(&tmp->d_lock);
1198 tmp->d_sb = inode->i_sb;
1199 tmp->d_inode = inode;
1200 tmp->d_flags |= DCACHE_DISCONNECTED;
1201 tmp->d_flags &= ~DCACHE_UNHASHED;
1202 list_add(&tmp->d_alias, &inode->i_dentry);
1203 hlist_add_head(&tmp->d_hash, &inode->i_sb->s_anon);
1204 spin_unlock(&tmp->d_lock);
1205
1206 spin_unlock(&dcache_lock);
1207 return tmp;
1208
1209 out_iput:
1210 iput(inode);
1211 return res;
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001212}
Benny Halevyadc48722009-02-27 14:02:59 -08001213EXPORT_SYMBOL(d_obtain_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
1215/**
1216 * d_splice_alias - splice a disconnected dentry into the tree if one exists
1217 * @inode: the inode which may have a disconnected dentry
1218 * @dentry: a negative dentry which we want to point to the inode.
1219 *
1220 * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
1221 * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
1222 * and return it, else simply d_add the inode to the dentry and return NULL.
1223 *
1224 * This is needed in the lookup routine of any filesystem that is exportable
1225 * (via knfsd) so that we can build dcache paths to directories effectively.
1226 *
1227 * If a dentry was found and moved, then it is returned. Otherwise NULL
1228 * is returned. This matches the expected return value of ->lookup.
1229 *
1230 */
1231struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
1232{
1233 struct dentry *new = NULL;
1234
NeilBrown21c0d8f2006-10-04 02:16:16 -07001235 if (inode && S_ISDIR(inode->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 spin_lock(&dcache_lock);
1237 new = __d_find_alias(inode, 1);
1238 if (new) {
1239 BUG_ON(!(new->d_flags & DCACHE_DISCONNECTED));
1240 spin_unlock(&dcache_lock);
1241 security_d_instantiate(new, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 d_move(new, dentry);
1243 iput(inode);
1244 } else {
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001245 /* already taking dcache_lock, so d_add() by hand */
1246 __d_instantiate(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 spin_unlock(&dcache_lock);
1248 security_d_instantiate(dentry, inode);
1249 d_rehash(dentry);
1250 }
1251 } else
1252 d_add(dentry, inode);
1253 return new;
1254}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001255EXPORT_SYMBOL(d_splice_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
Barry Naujok94035402008-05-21 16:50:46 +10001257/**
1258 * d_add_ci - lookup or allocate new dentry with case-exact name
1259 * @inode: the inode case-insensitive lookup has found
1260 * @dentry: the negative dentry that was passed to the parent's lookup func
1261 * @name: the case-exact name to be associated with the returned dentry
1262 *
1263 * This is to avoid filling the dcache with case-insensitive names to the
1264 * same inode, only the actual correct case is stored in the dcache for
1265 * case-insensitive filesystems.
1266 *
1267 * For a case-insensitive lookup match and if the the case-exact dentry
1268 * already exists in in the dcache, use it and return it.
1269 *
1270 * If no entry exists with the exact case name, allocate new dentry with
1271 * the exact case, and return the spliced entry.
1272 */
Christoph Hellwige45b5902008-08-07 23:49:07 +02001273struct dentry *d_add_ci(struct dentry *dentry, struct inode *inode,
Barry Naujok94035402008-05-21 16:50:46 +10001274 struct qstr *name)
1275{
1276 int error;
1277 struct dentry *found;
1278 struct dentry *new;
1279
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001280 /*
1281 * First check if a dentry matching the name already exists,
1282 * if not go ahead and create it now.
1283 */
Barry Naujok94035402008-05-21 16:50:46 +10001284 found = d_hash_and_lookup(dentry->d_parent, name);
Barry Naujok94035402008-05-21 16:50:46 +10001285 if (!found) {
1286 new = d_alloc(dentry->d_parent, name);
1287 if (!new) {
1288 error = -ENOMEM;
1289 goto err_out;
1290 }
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001291
Barry Naujok94035402008-05-21 16:50:46 +10001292 found = d_splice_alias(inode, new);
1293 if (found) {
1294 dput(new);
1295 return found;
1296 }
1297 return new;
1298 }
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001299
1300 /*
1301 * If a matching dentry exists, and it's not negative use it.
1302 *
1303 * Decrement the reference count to balance the iget() done
1304 * earlier on.
1305 */
Barry Naujok94035402008-05-21 16:50:46 +10001306 if (found->d_inode) {
1307 if (unlikely(found->d_inode != inode)) {
1308 /* This can't happen because bad inodes are unhashed. */
1309 BUG_ON(!is_bad_inode(inode));
1310 BUG_ON(!is_bad_inode(found->d_inode));
1311 }
Barry Naujok94035402008-05-21 16:50:46 +10001312 iput(inode);
1313 return found;
1314 }
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001315
Barry Naujok94035402008-05-21 16:50:46 +10001316 /*
1317 * Negative dentry: instantiate it unless the inode is a directory and
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001318 * already has a dentry.
Barry Naujok94035402008-05-21 16:50:46 +10001319 */
Barry Naujok94035402008-05-21 16:50:46 +10001320 spin_lock(&dcache_lock);
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001321 if (!S_ISDIR(inode->i_mode) || list_empty(&inode->i_dentry)) {
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001322 __d_instantiate(found, inode);
Barry Naujok94035402008-05-21 16:50:46 +10001323 spin_unlock(&dcache_lock);
1324 security_d_instantiate(found, inode);
1325 return found;
1326 }
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001327
Barry Naujok94035402008-05-21 16:50:46 +10001328 /*
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001329 * In case a directory already has a (disconnected) entry grab a
1330 * reference to it, move it in place and use it.
Barry Naujok94035402008-05-21 16:50:46 +10001331 */
1332 new = list_entry(inode->i_dentry.next, struct dentry, d_alias);
1333 dget_locked(new);
1334 spin_unlock(&dcache_lock);
Barry Naujok94035402008-05-21 16:50:46 +10001335 security_d_instantiate(found, inode);
Barry Naujok94035402008-05-21 16:50:46 +10001336 d_move(new, found);
Barry Naujok94035402008-05-21 16:50:46 +10001337 iput(inode);
Barry Naujok94035402008-05-21 16:50:46 +10001338 dput(found);
Barry Naujok94035402008-05-21 16:50:46 +10001339 return new;
1340
1341err_out:
1342 iput(inode);
1343 return ERR_PTR(error);
1344}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001345EXPORT_SYMBOL(d_add_ci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
1347/**
1348 * d_lookup - search for a dentry
1349 * @parent: parent dentry
1350 * @name: qstr of name we wish to find
Nick Pigginb04f7842010-08-18 04:37:34 +10001351 * Returns: dentry, or NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 *
Nick Pigginb04f7842010-08-18 04:37:34 +10001353 * d_lookup searches the children of the parent dentry for the name in
1354 * question. If the dentry is found its reference count is incremented and the
1355 * dentry is returned. The caller must use dput to free the entry when it has
1356 * finished using it. %NULL is returned if the dentry does not exist.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358struct dentry * d_lookup(struct dentry * parent, struct qstr * name)
1359{
1360 struct dentry * dentry = NULL;
1361 unsigned long seq;
1362
1363 do {
1364 seq = read_seqbegin(&rename_lock);
1365 dentry = __d_lookup(parent, name);
1366 if (dentry)
1367 break;
1368 } while (read_seqretry(&rename_lock, seq));
1369 return dentry;
1370}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001371EXPORT_SYMBOL(d_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
Nick Pigginb04f7842010-08-18 04:37:34 +10001373/*
1374 * __d_lookup - search for a dentry (racy)
1375 * @parent: parent dentry
1376 * @name: qstr of name we wish to find
1377 * Returns: dentry, or NULL
1378 *
1379 * __d_lookup is like d_lookup, however it may (rarely) return a
1380 * false-negative result due to unrelated rename activity.
1381 *
1382 * __d_lookup is slightly faster by avoiding rename_lock read seqlock,
1383 * however it must be used carefully, eg. with a following d_lookup in
1384 * the case of failure.
1385 *
1386 * __d_lookup callers must be commented.
1387 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388struct dentry * __d_lookup(struct dentry * parent, struct qstr * name)
1389{
1390 unsigned int len = name->len;
1391 unsigned int hash = name->hash;
1392 const unsigned char *str = name->name;
1393 struct hlist_head *head = d_hash(parent,hash);
1394 struct dentry *found = NULL;
1395 struct hlist_node *node;
Paul E. McKenney665a7582005-11-07 00:59:17 -08001396 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
Nick Pigginb04f7842010-08-18 04:37:34 +10001398 /*
1399 * The hash list is protected using RCU.
1400 *
1401 * Take d_lock when comparing a candidate dentry, to avoid races
1402 * with d_move().
1403 *
1404 * It is possible that concurrent renames can mess up our list
1405 * walk here and result in missing our dentry, resulting in the
1406 * false-negative result. d_lookup() protects against concurrent
1407 * renames using rename_lock seqlock.
1408 *
1409 * See Documentation/vfs/dcache-locking.txt for more details.
1410 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 rcu_read_lock();
1412
Paul E. McKenney665a7582005-11-07 00:59:17 -08001413 hlist_for_each_entry_rcu(dentry, node, head, d_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 struct qstr *qstr;
1415
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 if (dentry->d_name.hash != hash)
1417 continue;
1418 if (dentry->d_parent != parent)
1419 continue;
1420
1421 spin_lock(&dentry->d_lock);
1422
1423 /*
1424 * Recheck the dentry after taking the lock - d_move may have
Nick Pigginb04f7842010-08-18 04:37:34 +10001425 * changed things. Don't bother checking the hash because
1426 * we're about to compare the whole name anyway.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 */
1428 if (dentry->d_parent != parent)
1429 goto next;
1430
Linus Torvaldsd0185c02008-09-29 07:42:57 -07001431 /* non-existing due to RCU? */
1432 if (d_unhashed(dentry))
1433 goto next;
1434
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 /*
1436 * It is safe to compare names since d_move() cannot
1437 * change the qstr (protected by d_lock).
1438 */
1439 qstr = &dentry->d_name;
1440 if (parent->d_op && parent->d_op->d_compare) {
1441 if (parent->d_op->d_compare(parent, qstr, name))
1442 goto next;
1443 } else {
1444 if (qstr->len != len)
1445 goto next;
1446 if (memcmp(qstr->name, str, len))
1447 goto next;
1448 }
1449
Linus Torvaldsd0185c02008-09-29 07:42:57 -07001450 atomic_inc(&dentry->d_count);
1451 found = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 spin_unlock(&dentry->d_lock);
1453 break;
1454next:
1455 spin_unlock(&dentry->d_lock);
1456 }
1457 rcu_read_unlock();
1458
1459 return found;
1460}
1461
1462/**
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08001463 * d_hash_and_lookup - hash the qstr then search for a dentry
1464 * @dir: Directory to search in
1465 * @name: qstr of name we wish to find
1466 *
1467 * On hash failure or on lookup failure NULL is returned.
1468 */
1469struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name)
1470{
1471 struct dentry *dentry = NULL;
1472
1473 /*
1474 * Check for a fs-specific hash function. Note that we must
1475 * calculate the standard hash first, as the d_op->d_hash()
1476 * routine may choose to leave the hash value unchanged.
1477 */
1478 name->hash = full_name_hash(name->name, name->len);
1479 if (dir->d_op && dir->d_op->d_hash) {
1480 if (dir->d_op->d_hash(dir, name) < 0)
1481 goto out;
1482 }
1483 dentry = d_lookup(dir, name);
1484out:
1485 return dentry;
1486}
1487
1488/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 * d_validate - verify dentry provided from insecure source
1490 * @dentry: The dentry alleged to be valid child of @dparent
1491 * @dparent: The parent dentry (known to be valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 *
1493 * An insecure source has sent us a dentry, here we verify it and dget() it.
1494 * This is used by ncpfs in its readdir implementation.
1495 * Zero is returned in the dentry is invalid.
1496 */
1497
1498int d_validate(struct dentry *dentry, struct dentry *dparent)
1499{
1500 struct hlist_head *base;
1501 struct hlist_node *lhp;
1502
1503 /* Check whether the ptr might be valid at all.. */
1504 if (!kmem_ptr_validate(dentry_cache, dentry))
1505 goto out;
1506
1507 if (dentry->d_parent != dparent)
1508 goto out;
1509
1510 spin_lock(&dcache_lock);
1511 base = d_hash(dparent, dentry->d_name.hash);
1512 hlist_for_each(lhp,base) {
Paul E. McKenney665a7582005-11-07 00:59:17 -08001513 /* hlist_for_each_entry_rcu() not required for d_hash list
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 * as it is parsed under dcache_lock
1515 */
1516 if (dentry == hlist_entry(lhp, struct dentry, d_hash)) {
1517 __dget_locked(dentry);
1518 spin_unlock(&dcache_lock);
1519 return 1;
1520 }
1521 }
1522 spin_unlock(&dcache_lock);
1523out:
1524 return 0;
1525}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001526EXPORT_SYMBOL(d_validate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
1528/*
1529 * When a file is deleted, we have two options:
1530 * - turn this dentry into a negative dentry
1531 * - unhash this dentry and free it.
1532 *
1533 * Usually, we want to just turn this into
1534 * a negative dentry, but if anybody else is
1535 * currently using the dentry or the inode
1536 * we can't do that and we fall back on removing
1537 * it from the hash queues and waiting for
1538 * it to be deleted later when it has no users
1539 */
1540
1541/**
1542 * d_delete - delete a dentry
1543 * @dentry: The dentry to delete
1544 *
1545 * Turn the dentry into a negative dentry if possible, otherwise
1546 * remove it from the hash queues so it can be deleted later
1547 */
1548
1549void d_delete(struct dentry * dentry)
1550{
John McCutchan7a91bf72005-08-08 13:52:16 -04001551 int isdir = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 /*
1553 * Are we the only user?
1554 */
1555 spin_lock(&dcache_lock);
1556 spin_lock(&dentry->d_lock);
John McCutchan7a91bf72005-08-08 13:52:16 -04001557 isdir = S_ISDIR(dentry->d_inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 if (atomic_read(&dentry->d_count) == 1) {
Al Viro13e3c5e2010-05-21 16:11:04 -04001559 dentry->d_flags &= ~DCACHE_CANT_MOUNT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 dentry_iput(dentry);
John McCutchan7a91bf72005-08-08 13:52:16 -04001561 fsnotify_nameremove(dentry, isdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 return;
1563 }
1564
1565 if (!d_unhashed(dentry))
1566 __d_drop(dentry);
1567
1568 spin_unlock(&dentry->d_lock);
1569 spin_unlock(&dcache_lock);
John McCutchan7a91bf72005-08-08 13:52:16 -04001570
1571 fsnotify_nameremove(dentry, isdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001573EXPORT_SYMBOL(d_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
1575static void __d_rehash(struct dentry * entry, struct hlist_head *list)
1576{
1577
1578 entry->d_flags &= ~DCACHE_UNHASHED;
1579 hlist_add_head_rcu(&entry->d_hash, list);
1580}
1581
David Howells770bfad2006-08-22 20:06:07 -04001582static void _d_rehash(struct dentry * entry)
1583{
1584 __d_rehash(entry, d_hash(entry->d_parent, entry->d_name.hash));
1585}
1586
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587/**
1588 * d_rehash - add an entry back to the hash
1589 * @entry: dentry to add to the hash
1590 *
1591 * Adds a dentry to the hash according to its name.
1592 */
1593
1594void d_rehash(struct dentry * entry)
1595{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 spin_lock(&dcache_lock);
1597 spin_lock(&entry->d_lock);
David Howells770bfad2006-08-22 20:06:07 -04001598 _d_rehash(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 spin_unlock(&entry->d_lock);
1600 spin_unlock(&dcache_lock);
1601}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001602EXPORT_SYMBOL(d_rehash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604/*
1605 * When switching names, the actual string doesn't strictly have to
1606 * be preserved in the target - because we're dropping the target
1607 * anyway. As such, we can just do a simple memcpy() to copy over
1608 * the new name before we switch.
1609 *
1610 * Note that we have to be a lot more careful about getting the hash
1611 * switched - we have to switch the hash value properly even if it
1612 * then no longer matches the actual (corrupted) string of the target.
1613 * The hash value has to match the hash queue that the dentry is on..
1614 */
1615static void switch_names(struct dentry *dentry, struct dentry *target)
1616{
1617 if (dname_external(target)) {
1618 if (dname_external(dentry)) {
1619 /*
1620 * Both external: swap the pointers
1621 */
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08001622 swap(target->d_name.name, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 } else {
1624 /*
1625 * dentry:internal, target:external. Steal target's
1626 * storage and make target internal.
1627 */
J. Bruce Fields321bcf92007-10-21 16:41:38 -07001628 memcpy(target->d_iname, dentry->d_name.name,
1629 dentry->d_name.len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 dentry->d_name.name = target->d_name.name;
1631 target->d_name.name = target->d_iname;
1632 }
1633 } else {
1634 if (dname_external(dentry)) {
1635 /*
1636 * dentry:external, target:internal. Give dentry's
1637 * storage to target and make dentry internal
1638 */
1639 memcpy(dentry->d_iname, target->d_name.name,
1640 target->d_name.len + 1);
1641 target->d_name.name = dentry->d_name.name;
1642 dentry->d_name.name = dentry->d_iname;
1643 } else {
1644 /*
1645 * Both are internal. Just copy target to dentry
1646 */
1647 memcpy(dentry->d_iname, target->d_name.name,
1648 target->d_name.len + 1);
Al Virodc711ca2008-11-03 15:03:50 -05001649 dentry->d_name.len = target->d_name.len;
1650 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 }
1652 }
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08001653 swap(dentry->d_name.len, target->d_name.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654}
1655
1656/*
1657 * We cannibalize "target" when moving dentry on top of it,
1658 * because it's going to be thrown away anyway. We could be more
1659 * polite about it, though.
1660 *
1661 * This forceful removal will result in ugly /proc output if
1662 * somebody holds a file open that got deleted due to a rename.
1663 * We could be nicer about the deleted file, and let it show
J. Bruce Fieldsbc154b12007-10-16 23:29:42 -07001664 * up under the name it had before it was deleted rather than
1665 * under the original name of the file that was moved on top of it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 */
1667
Trond Myklebust9eaef272006-10-21 10:24:20 -07001668/*
1669 * d_move_locked - move a dentry
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 * @dentry: entry to move
1671 * @target: new dentry
1672 *
1673 * Update the dcache to reflect the move of a file name. Negative
1674 * dcache entries should not be moved in this way.
1675 */
Trond Myklebust9eaef272006-10-21 10:24:20 -07001676static void d_move_locked(struct dentry * dentry, struct dentry * target)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677{
1678 struct hlist_head *list;
1679
1680 if (!dentry->d_inode)
1681 printk(KERN_WARNING "VFS: moving negative dcache entry\n");
1682
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 write_seqlock(&rename_lock);
1684 /*
1685 * XXXX: do we really need to take target->d_lock?
1686 */
1687 if (target < dentry) {
1688 spin_lock(&target->d_lock);
Ingo Molnara90b9c02006-07-03 00:25:04 -07001689 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 } else {
1691 spin_lock(&dentry->d_lock);
Ingo Molnara90b9c02006-07-03 00:25:04 -07001692 spin_lock_nested(&target->d_lock, DENTRY_D_LOCK_NESTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 }
1694
1695 /* Move the dentry to the target hash queue, if on different bucket */
Denis Chengf77e3492007-10-16 23:30:11 -07001696 if (d_unhashed(dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 goto already_unhashed;
1698
1699 hlist_del_rcu(&dentry->d_hash);
1700
1701already_unhashed:
1702 list = d_hash(target->d_parent, target->d_name.hash);
1703 __d_rehash(dentry, list);
1704
1705 /* Unhash the target: dput() will then get rid of it */
1706 __d_drop(target);
1707
Eric Dumazet5160ee62006-01-08 01:03:32 -08001708 list_del(&dentry->d_u.d_child);
1709 list_del(&target->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710
1711 /* Switch the names.. */
1712 switch_names(dentry, target);
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08001713 swap(dentry->d_name.hash, target->d_name.hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
1715 /* ... and switch the parents */
1716 if (IS_ROOT(dentry)) {
1717 dentry->d_parent = target->d_parent;
1718 target->d_parent = target;
Eric Dumazet5160ee62006-01-08 01:03:32 -08001719 INIT_LIST_HEAD(&target->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 } else {
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08001721 swap(dentry->d_parent, target->d_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
1723 /* And add them back to the (new) parent lists */
Eric Dumazet5160ee62006-01-08 01:03:32 -08001724 list_add(&target->d_u.d_child, &target->d_parent->d_subdirs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 }
1726
Eric Dumazet5160ee62006-01-08 01:03:32 -08001727 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 spin_unlock(&target->d_lock);
Nick Pigginc32ccd82006-03-25 03:07:09 -08001729 fsnotify_d_move(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 spin_unlock(&dentry->d_lock);
1731 write_sequnlock(&rename_lock);
Trond Myklebust9eaef272006-10-21 10:24:20 -07001732}
1733
1734/**
1735 * d_move - move a dentry
1736 * @dentry: entry to move
1737 * @target: new dentry
1738 *
1739 * Update the dcache to reflect the move of a file name. Negative
1740 * dcache entries should not be moved in this way.
1741 */
1742
1743void d_move(struct dentry * dentry, struct dentry * target)
1744{
1745 spin_lock(&dcache_lock);
1746 d_move_locked(dentry, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 spin_unlock(&dcache_lock);
1748}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001749EXPORT_SYMBOL(d_move);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001751/**
1752 * d_ancestor - search for an ancestor
1753 * @p1: ancestor dentry
1754 * @p2: child dentry
1755 *
1756 * Returns the ancestor dentry of p2 which is a child of p1, if p1 is
1757 * an ancestor of p2, else NULL.
Trond Myklebust9eaef272006-10-21 10:24:20 -07001758 */
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001759struct dentry *d_ancestor(struct dentry *p1, struct dentry *p2)
Trond Myklebust9eaef272006-10-21 10:24:20 -07001760{
1761 struct dentry *p;
1762
OGAWA Hirofumi871c0062008-10-16 07:50:27 +09001763 for (p = p2; !IS_ROOT(p); p = p->d_parent) {
Trond Myklebust9eaef272006-10-21 10:24:20 -07001764 if (p->d_parent == p1)
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001765 return p;
Trond Myklebust9eaef272006-10-21 10:24:20 -07001766 }
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001767 return NULL;
Trond Myklebust9eaef272006-10-21 10:24:20 -07001768}
1769
1770/*
1771 * This helper attempts to cope with remotely renamed directories
1772 *
1773 * It assumes that the caller is already holding
1774 * dentry->d_parent->d_inode->i_mutex and the dcache_lock
1775 *
1776 * Note: If ever the locking in lock_rename() changes, then please
1777 * remember to update this too...
Trond Myklebust9eaef272006-10-21 10:24:20 -07001778 */
1779static struct dentry *__d_unalias(struct dentry *dentry, struct dentry *alias)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +02001780 __releases(dcache_lock)
Trond Myklebust9eaef272006-10-21 10:24:20 -07001781{
1782 struct mutex *m1 = NULL, *m2 = NULL;
1783 struct dentry *ret;
1784
1785 /* If alias and dentry share a parent, then no extra locks required */
1786 if (alias->d_parent == dentry->d_parent)
1787 goto out_unalias;
1788
1789 /* Check for loops */
1790 ret = ERR_PTR(-ELOOP);
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001791 if (d_ancestor(alias, dentry))
Trond Myklebust9eaef272006-10-21 10:24:20 -07001792 goto out_err;
1793
1794 /* See lock_rename() */
1795 ret = ERR_PTR(-EBUSY);
1796 if (!mutex_trylock(&dentry->d_sb->s_vfs_rename_mutex))
1797 goto out_err;
1798 m1 = &dentry->d_sb->s_vfs_rename_mutex;
1799 if (!mutex_trylock(&alias->d_parent->d_inode->i_mutex))
1800 goto out_err;
1801 m2 = &alias->d_parent->d_inode->i_mutex;
1802out_unalias:
1803 d_move_locked(alias, dentry);
1804 ret = alias;
1805out_err:
1806 spin_unlock(&dcache_lock);
1807 if (m2)
1808 mutex_unlock(m2);
1809 if (m1)
1810 mutex_unlock(m1);
1811 return ret;
1812}
1813
1814/*
David Howells770bfad2006-08-22 20:06:07 -04001815 * Prepare an anonymous dentry for life in the superblock's dentry tree as a
1816 * named dentry in place of the dentry to be replaced.
1817 */
1818static void __d_materialise_dentry(struct dentry *dentry, struct dentry *anon)
1819{
1820 struct dentry *dparent, *aparent;
1821
1822 switch_names(dentry, anon);
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08001823 swap(dentry->d_name.hash, anon->d_name.hash);
David Howells770bfad2006-08-22 20:06:07 -04001824
1825 dparent = dentry->d_parent;
1826 aparent = anon->d_parent;
1827
1828 dentry->d_parent = (aparent == anon) ? dentry : aparent;
1829 list_del(&dentry->d_u.d_child);
1830 if (!IS_ROOT(dentry))
1831 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
1832 else
1833 INIT_LIST_HEAD(&dentry->d_u.d_child);
1834
1835 anon->d_parent = (dparent == dentry) ? anon : dparent;
1836 list_del(&anon->d_u.d_child);
1837 if (!IS_ROOT(anon))
1838 list_add(&anon->d_u.d_child, &anon->d_parent->d_subdirs);
1839 else
1840 INIT_LIST_HEAD(&anon->d_u.d_child);
1841
1842 anon->d_flags &= ~DCACHE_DISCONNECTED;
1843}
1844
1845/**
1846 * d_materialise_unique - introduce an inode into the tree
1847 * @dentry: candidate dentry
1848 * @inode: inode to bind to the dentry, to which aliases may be attached
1849 *
1850 * Introduces an dentry into the tree, substituting an extant disconnected
1851 * root directory alias in its place if there is one
1852 */
1853struct dentry *d_materialise_unique(struct dentry *dentry, struct inode *inode)
1854{
Trond Myklebust9eaef272006-10-21 10:24:20 -07001855 struct dentry *actual;
David Howells770bfad2006-08-22 20:06:07 -04001856
1857 BUG_ON(!d_unhashed(dentry));
1858
1859 spin_lock(&dcache_lock);
1860
1861 if (!inode) {
1862 actual = dentry;
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001863 __d_instantiate(dentry, NULL);
David Howells770bfad2006-08-22 20:06:07 -04001864 goto found_lock;
1865 }
1866
Trond Myklebust9eaef272006-10-21 10:24:20 -07001867 if (S_ISDIR(inode->i_mode)) {
1868 struct dentry *alias;
David Howells770bfad2006-08-22 20:06:07 -04001869
Trond Myklebust9eaef272006-10-21 10:24:20 -07001870 /* Does an aliased dentry already exist? */
1871 alias = __d_find_alias(inode, 0);
1872 if (alias) {
1873 actual = alias;
1874 /* Is this an anonymous mountpoint that we could splice
1875 * into our tree? */
1876 if (IS_ROOT(alias)) {
1877 spin_lock(&alias->d_lock);
1878 __d_materialise_dentry(dentry, alias);
1879 __d_drop(alias);
1880 goto found;
1881 }
1882 /* Nope, but we must(!) avoid directory aliasing */
1883 actual = __d_unalias(dentry, alias);
1884 if (IS_ERR(actual))
1885 dput(alias);
1886 goto out_nolock;
1887 }
David Howells770bfad2006-08-22 20:06:07 -04001888 }
1889
1890 /* Add a unique reference */
1891 actual = __d_instantiate_unique(dentry, inode);
1892 if (!actual)
1893 actual = dentry;
1894 else if (unlikely(!d_unhashed(actual)))
1895 goto shouldnt_be_hashed;
1896
1897found_lock:
1898 spin_lock(&actual->d_lock);
1899found:
1900 _d_rehash(actual);
1901 spin_unlock(&actual->d_lock);
1902 spin_unlock(&dcache_lock);
Trond Myklebust9eaef272006-10-21 10:24:20 -07001903out_nolock:
David Howells770bfad2006-08-22 20:06:07 -04001904 if (actual == dentry) {
1905 security_d_instantiate(dentry, inode);
1906 return NULL;
1907 }
1908
1909 iput(inode);
1910 return actual;
1911
David Howells770bfad2006-08-22 20:06:07 -04001912shouldnt_be_hashed:
1913 spin_unlock(&dcache_lock);
1914 BUG();
David Howells770bfad2006-08-22 20:06:07 -04001915}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001916EXPORT_SYMBOL_GPL(d_materialise_unique);
David Howells770bfad2006-08-22 20:06:07 -04001917
Miklos Szeredicdd16d02008-06-23 18:11:53 +02001918static int prepend(char **buffer, int *buflen, const char *str, int namelen)
Ram Pai6092d042008-03-27 13:06:20 +01001919{
1920 *buflen -= namelen;
1921 if (*buflen < 0)
1922 return -ENAMETOOLONG;
1923 *buffer -= namelen;
1924 memcpy(*buffer, str, namelen);
1925 return 0;
1926}
1927
Miklos Szeredicdd16d02008-06-23 18:11:53 +02001928static int prepend_name(char **buffer, int *buflen, struct qstr *name)
1929{
1930 return prepend(buffer, buflen, name->name, name->len);
1931}
1932
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933/**
Miklos Szeredif2eb6572010-08-10 11:41:39 +02001934 * Prepend path string to a buffer
1935 *
1936 * @path: the dentry/vfsmount to report
1937 * @root: root vfsmnt/dentry (may be modified by this function)
1938 * @buffer: pointer to the end of the buffer
1939 * @buflen: pointer to buffer length
1940 *
1941 * Caller holds the dcache_lock.
1942 *
1943 * If path is not reachable from the supplied root, then the value of
1944 * root is changed (without modifying refcounts).
1945 */
1946static int prepend_path(const struct path *path, struct path *root,
1947 char **buffer, int *buflen)
1948{
1949 struct dentry *dentry = path->dentry;
1950 struct vfsmount *vfsmnt = path->mnt;
1951 bool slash = false;
1952 int error = 0;
1953
Nick Piggin99b7db72010-08-18 04:37:39 +10001954 br_read_lock(vfsmount_lock);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02001955 while (dentry != root->dentry || vfsmnt != root->mnt) {
1956 struct dentry * parent;
1957
1958 if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
1959 /* Global root? */
1960 if (vfsmnt->mnt_parent == vfsmnt) {
1961 goto global_root;
1962 }
1963 dentry = vfsmnt->mnt_mountpoint;
1964 vfsmnt = vfsmnt->mnt_parent;
1965 continue;
1966 }
1967 parent = dentry->d_parent;
1968 prefetch(parent);
1969 error = prepend_name(buffer, buflen, &dentry->d_name);
1970 if (!error)
1971 error = prepend(buffer, buflen, "/", 1);
1972 if (error)
1973 break;
1974
1975 slash = true;
1976 dentry = parent;
1977 }
1978
1979out:
1980 if (!error && !slash)
1981 error = prepend(buffer, buflen, "/", 1);
1982
Nick Piggin99b7db72010-08-18 04:37:39 +10001983 br_read_unlock(vfsmount_lock);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02001984 return error;
1985
1986global_root:
1987 /*
1988 * Filesystems needing to implement special "root names"
1989 * should do so with ->d_dname()
1990 */
1991 if (IS_ROOT(dentry) &&
1992 (dentry->d_name.len != 1 || dentry->d_name.name[0] != '/')) {
1993 WARN(1, "Root dentry has weird name <%.*s>\n",
1994 (int) dentry->d_name.len, dentry->d_name.name);
1995 }
1996 root->mnt = vfsmnt;
1997 root->dentry = dentry;
1998 goto out;
1999}
2000
2001/**
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +02002002 * __d_path - return the path of a dentry
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002003 * @path: the dentry/vfsmount to report
2004 * @root: root vfsmnt/dentry (may be modified by this function)
Randy Dunlapcd956a12010-08-14 13:05:31 -07002005 * @buf: buffer to return value in
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 * @buflen: buffer length
2007 *
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002008 * Convert a dentry into an ASCII path name.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 *
Arjan van de Ven52afeef2008-12-01 14:35:00 -08002010 * Returns a pointer into the buffer or an error code if the
2011 * path was too long.
Linus Torvalds552ce542007-02-13 12:08:18 -08002012 *
Christoph Hellwigbe148242010-10-10 05:36:21 -04002013 * "buflen" should be positive.
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002014 *
2015 * If path is not reachable from the supplied root, then the value of
2016 * root is changed (without modifying refcounts).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 */
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002018char *__d_path(const struct path *path, struct path *root,
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002019 char *buf, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020{
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002021 char *res = buf + buflen;
2022 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002024 prepend(&res, &buflen, "\0", 1);
Christoph Hellwigbe148242010-10-10 05:36:21 -04002025 spin_lock(&dcache_lock);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002026 error = prepend_path(path, root, &res, &buflen);
Christoph Hellwigbe148242010-10-10 05:36:21 -04002027 spin_unlock(&dcache_lock);
2028
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002029 if (error)
2030 return ERR_PTR(error);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002031 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032}
2033
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002034/*
2035 * same as __d_path but appends "(deleted)" for unlinked files.
2036 */
2037static int path_with_deleted(const struct path *path, struct path *root,
2038 char **buf, int *buflen)
2039{
2040 prepend(buf, buflen, "\0", 1);
2041 if (d_unlinked(path->dentry)) {
2042 int error = prepend(buf, buflen, " (deleted)", 10);
2043 if (error)
2044 return error;
2045 }
2046
2047 return prepend_path(path, root, buf, buflen);
2048}
2049
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002050static int prepend_unreachable(char **buffer, int *buflen)
2051{
2052 return prepend(buffer, buflen, "(unreachable)", 13);
2053}
2054
Jan Bluncka03a8a702008-02-14 19:38:32 -08002055/**
2056 * d_path - return the path of a dentry
Jan Blunckcf28b482008-02-14 19:38:44 -08002057 * @path: path to report
Jan Bluncka03a8a702008-02-14 19:38:32 -08002058 * @buf: buffer to return value in
2059 * @buflen: buffer length
2060 *
2061 * Convert a dentry into an ASCII path name. If the entry has been deleted
2062 * the string " (deleted)" is appended. Note that this is ambiguous.
2063 *
Arjan van de Ven52afeef2008-12-01 14:35:00 -08002064 * Returns a pointer into the buffer or an error code if the path was
2065 * too long. Note: Callers should use the returned pointer, not the passed
2066 * in buffer, to use the name! The implementation often starts at an offset
2067 * into the buffer, and may leave 0 bytes at the start.
Jan Bluncka03a8a702008-02-14 19:38:32 -08002068 *
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +02002069 * "buflen" should be positive.
Jan Bluncka03a8a702008-02-14 19:38:32 -08002070 */
Jan Engelhardt20d4fdc2008-06-09 16:40:36 -07002071char *d_path(const struct path *path, char *buf, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072{
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002073 char *res = buf + buflen;
Jan Blunck6ac08c32008-02-14 19:34:38 -08002074 struct path root;
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002075 struct path tmp;
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002076 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
Eric Dumazetc23fbb62007-05-08 00:26:18 -07002078 /*
2079 * We have various synthetic filesystems that never get mounted. On
2080 * these filesystems dentries are never used for lookup purposes, and
2081 * thus don't need to be hashed. They also don't need a name until a
2082 * user wants to identify the object in /proc/pid/fd/. The little hack
2083 * below allows us to generate a name for these objects on demand:
2084 */
Jan Blunckcf28b482008-02-14 19:38:44 -08002085 if (path->dentry->d_op && path->dentry->d_op->d_dname)
2086 return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
Eric Dumazetc23fbb62007-05-08 00:26:18 -07002087
Miklos Szeredif7ad3c62010-08-10 11:41:36 +02002088 get_fs_root(current->fs, &root);
Linus Torvalds552ce542007-02-13 12:08:18 -08002089 spin_lock(&dcache_lock);
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002090 tmp = root;
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002091 error = path_with_deleted(path, &tmp, &res, &buflen);
2092 if (error)
2093 res = ERR_PTR(error);
Linus Torvalds552ce542007-02-13 12:08:18 -08002094 spin_unlock(&dcache_lock);
Jan Blunck6ac08c32008-02-14 19:34:38 -08002095 path_put(&root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 return res;
2097}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002098EXPORT_SYMBOL(d_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002100/**
2101 * d_path_with_unreachable - return the path of a dentry
2102 * @path: path to report
2103 * @buf: buffer to return value in
2104 * @buflen: buffer length
2105 *
2106 * The difference from d_path() is that this prepends "(unreachable)"
2107 * to paths which are unreachable from the current process' root.
2108 */
2109char *d_path_with_unreachable(const struct path *path, char *buf, int buflen)
2110{
2111 char *res = buf + buflen;
2112 struct path root;
2113 struct path tmp;
2114 int error;
2115
2116 if (path->dentry->d_op && path->dentry->d_op->d_dname)
2117 return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
2118
2119 get_fs_root(current->fs, &root);
2120 spin_lock(&dcache_lock);
2121 tmp = root;
2122 error = path_with_deleted(path, &tmp, &res, &buflen);
2123 if (!error && !path_equal(&tmp, &root))
2124 error = prepend_unreachable(&res, &buflen);
2125 spin_unlock(&dcache_lock);
2126 path_put(&root);
2127 if (error)
2128 res = ERR_PTR(error);
2129
2130 return res;
2131}
2132
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133/*
Eric Dumazetc23fbb62007-05-08 00:26:18 -07002134 * Helper function for dentry_operations.d_dname() members
2135 */
2136char *dynamic_dname(struct dentry *dentry, char *buffer, int buflen,
2137 const char *fmt, ...)
2138{
2139 va_list args;
2140 char temp[64];
2141 int sz;
2142
2143 va_start(args, fmt);
2144 sz = vsnprintf(temp, sizeof(temp), fmt, args) + 1;
2145 va_end(args);
2146
2147 if (sz > sizeof(temp) || sz > buflen)
2148 return ERR_PTR(-ENAMETOOLONG);
2149
2150 buffer += buflen - sz;
2151 return memcpy(buffer, temp, sz);
2152}
2153
2154/*
Ram Pai6092d042008-03-27 13:06:20 +01002155 * Write full pathname from the root of the filesystem into the buffer.
2156 */
Al Viroc1031352010-06-06 22:31:14 -04002157char *__dentry_path(struct dentry *dentry, char *buf, int buflen)
Ram Pai6092d042008-03-27 13:06:20 +01002158{
2159 char *end = buf + buflen;
2160 char *retval;
2161
Ram Pai6092d042008-03-27 13:06:20 +01002162 prepend(&end, &buflen, "\0", 1);
Ram Pai6092d042008-03-27 13:06:20 +01002163 if (buflen < 1)
2164 goto Elong;
2165 /* Get '/' right */
2166 retval = end-1;
2167 *retval = '/';
2168
Miklos Szeredicdd16d02008-06-23 18:11:53 +02002169 while (!IS_ROOT(dentry)) {
2170 struct dentry *parent = dentry->d_parent;
Ram Pai6092d042008-03-27 13:06:20 +01002171
Ram Pai6092d042008-03-27 13:06:20 +01002172 prefetch(parent);
Miklos Szeredicdd16d02008-06-23 18:11:53 +02002173 if ((prepend_name(&end, &buflen, &dentry->d_name) != 0) ||
Ram Pai6092d042008-03-27 13:06:20 +01002174 (prepend(&end, &buflen, "/", 1) != 0))
2175 goto Elong;
2176
2177 retval = end;
2178 dentry = parent;
2179 }
Al Viroc1031352010-06-06 22:31:14 -04002180 return retval;
2181Elong:
2182 return ERR_PTR(-ENAMETOOLONG);
2183}
2184EXPORT_SYMBOL(__dentry_path);
2185
2186char *dentry_path(struct dentry *dentry, char *buf, int buflen)
2187{
2188 char *p = NULL;
2189 char *retval;
2190
2191 spin_lock(&dcache_lock);
2192 if (d_unlinked(dentry)) {
2193 p = buf + buflen;
2194 if (prepend(&p, &buflen, "//deleted", 10) != 0)
2195 goto Elong;
2196 buflen++;
2197 }
2198 retval = __dentry_path(dentry, buf, buflen);
Ram Pai6092d042008-03-27 13:06:20 +01002199 spin_unlock(&dcache_lock);
Al Viroc1031352010-06-06 22:31:14 -04002200 if (!IS_ERR(retval) && p)
2201 *p = '/'; /* restore '/' overriden with '\0' */
Ram Pai6092d042008-03-27 13:06:20 +01002202 return retval;
2203Elong:
2204 spin_unlock(&dcache_lock);
2205 return ERR_PTR(-ENAMETOOLONG);
2206}
2207
2208/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 * NOTE! The user-level library version returns a
2210 * character pointer. The kernel system call just
2211 * returns the length of the buffer filled (which
2212 * includes the ending '\0' character), or a negative
2213 * error value. So libc would do something like
2214 *
2215 * char *getcwd(char * buf, size_t size)
2216 * {
2217 * int retval;
2218 *
2219 * retval = sys_getcwd(buf, size);
2220 * if (retval >= 0)
2221 * return buf;
2222 * errno = -retval;
2223 * return NULL;
2224 * }
2225 */
Heiko Carstens3cdad422009-01-14 14:14:22 +01002226SYSCALL_DEFINE2(getcwd, char __user *, buf, unsigned long, size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227{
Linus Torvalds552ce542007-02-13 12:08:18 -08002228 int error;
Jan Blunck6ac08c32008-02-14 19:34:38 -08002229 struct path pwd, root;
Linus Torvalds552ce542007-02-13 12:08:18 -08002230 char *page = (char *) __get_free_page(GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231
2232 if (!page)
2233 return -ENOMEM;
2234
Miklos Szeredif7ad3c62010-08-10 11:41:36 +02002235 get_fs_root_and_pwd(current->fs, &root, &pwd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
Linus Torvalds552ce542007-02-13 12:08:18 -08002237 error = -ENOENT;
Linus Torvalds552ce542007-02-13 12:08:18 -08002238 spin_lock(&dcache_lock);
Alexey Dobriyanf3da3922009-05-04 03:32:03 +04002239 if (!d_unlinked(pwd.dentry)) {
Linus Torvalds552ce542007-02-13 12:08:18 -08002240 unsigned long len;
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002241 struct path tmp = root;
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002242 char *cwd = page + PAGE_SIZE;
2243 int buflen = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002245 prepend(&cwd, &buflen, "\0", 1);
2246 error = prepend_path(&pwd, &tmp, &cwd, &buflen);
Linus Torvalds552ce542007-02-13 12:08:18 -08002247 spin_unlock(&dcache_lock);
2248
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002249 if (error)
Linus Torvalds552ce542007-02-13 12:08:18 -08002250 goto out;
2251
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002252 /* Unreachable from current root */
2253 if (!path_equal(&tmp, &root)) {
2254 error = prepend_unreachable(&cwd, &buflen);
2255 if (error)
2256 goto out;
2257 }
2258
Linus Torvalds552ce542007-02-13 12:08:18 -08002259 error = -ERANGE;
2260 len = PAGE_SIZE + page - cwd;
2261 if (len <= size) {
2262 error = len;
2263 if (copy_to_user(buf, cwd, len))
2264 error = -EFAULT;
2265 }
2266 } else
2267 spin_unlock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268
2269out:
Jan Blunck6ac08c32008-02-14 19:34:38 -08002270 path_put(&pwd);
2271 path_put(&root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 free_page((unsigned long) page);
2273 return error;
2274}
2275
2276/*
2277 * Test whether new_dentry is a subdirectory of old_dentry.
2278 *
2279 * Trivially implemented using the dcache structure
2280 */
2281
2282/**
2283 * is_subdir - is new dentry a subdirectory of old_dentry
2284 * @new_dentry: new dentry
2285 * @old_dentry: old dentry
2286 *
2287 * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
2288 * Returns 0 otherwise.
2289 * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
2290 */
2291
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002292int is_subdir(struct dentry *new_dentry, struct dentry *old_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293{
2294 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 unsigned long seq;
2296
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002297 if (new_dentry == old_dentry)
2298 return 1;
2299
2300 /*
2301 * Need rcu_readlock to protect against the d_parent trashing
2302 * due to d_move
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 */
2304 rcu_read_lock();
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002305 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 /* for restarting inner loop in case of seq retry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 seq = read_seqbegin(&rename_lock);
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002308 if (d_ancestor(old_dentry, new_dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 result = 1;
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002310 else
2311 result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 } while (read_seqretry(&rename_lock, seq));
2313 rcu_read_unlock();
2314
2315 return result;
2316}
2317
Al Viro2096f752010-01-30 13:16:21 -05002318int path_is_under(struct path *path1, struct path *path2)
2319{
2320 struct vfsmount *mnt = path1->mnt;
2321 struct dentry *dentry = path1->dentry;
2322 int res;
Nick Piggin99b7db72010-08-18 04:37:39 +10002323
2324 br_read_lock(vfsmount_lock);
Al Viro2096f752010-01-30 13:16:21 -05002325 if (mnt != path2->mnt) {
2326 for (;;) {
2327 if (mnt->mnt_parent == mnt) {
Nick Piggin99b7db72010-08-18 04:37:39 +10002328 br_read_unlock(vfsmount_lock);
Al Viro2096f752010-01-30 13:16:21 -05002329 return 0;
2330 }
2331 if (mnt->mnt_parent == path2->mnt)
2332 break;
2333 mnt = mnt->mnt_parent;
2334 }
2335 dentry = mnt->mnt_mountpoint;
2336 }
2337 res = is_subdir(dentry, path2->dentry);
Nick Piggin99b7db72010-08-18 04:37:39 +10002338 br_read_unlock(vfsmount_lock);
Al Viro2096f752010-01-30 13:16:21 -05002339 return res;
2340}
2341EXPORT_SYMBOL(path_is_under);
2342
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343void d_genocide(struct dentry *root)
2344{
2345 struct dentry *this_parent = root;
2346 struct list_head *next;
2347
2348 spin_lock(&dcache_lock);
2349repeat:
2350 next = this_parent->d_subdirs.next;
2351resume:
2352 while (next != &this_parent->d_subdirs) {
2353 struct list_head *tmp = next;
Eric Dumazet5160ee62006-01-08 01:03:32 -08002354 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 next = tmp->next;
2356 if (d_unhashed(dentry)||!dentry->d_inode)
2357 continue;
2358 if (!list_empty(&dentry->d_subdirs)) {
2359 this_parent = dentry;
2360 goto repeat;
2361 }
2362 atomic_dec(&dentry->d_count);
2363 }
2364 if (this_parent != root) {
Eric Dumazet5160ee62006-01-08 01:03:32 -08002365 next = this_parent->d_u.d_child.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 atomic_dec(&this_parent->d_count);
2367 this_parent = this_parent->d_parent;
2368 goto resume;
2369 }
2370 spin_unlock(&dcache_lock);
2371}
2372
2373/**
2374 * find_inode_number - check for dentry with name
2375 * @dir: directory to check
2376 * @name: Name to find.
2377 *
2378 * Check whether a dentry already exists for the given name,
2379 * and return the inode number if it has an inode. Otherwise
2380 * 0 is returned.
2381 *
2382 * This routine is used to post-process directory listings for
2383 * filesystems using synthetic inode numbers, and is necessary
2384 * to keep getcwd() working.
2385 */
2386
2387ino_t find_inode_number(struct dentry *dir, struct qstr *name)
2388{
2389 struct dentry * dentry;
2390 ino_t ino = 0;
2391
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08002392 dentry = d_hash_and_lookup(dir, name);
2393 if (dentry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 if (dentry->d_inode)
2395 ino = dentry->d_inode->i_ino;
2396 dput(dentry);
2397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 return ino;
2399}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002400EXPORT_SYMBOL(find_inode_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401
2402static __initdata unsigned long dhash_entries;
2403static int __init set_dhash_entries(char *str)
2404{
2405 if (!str)
2406 return 0;
2407 dhash_entries = simple_strtoul(str, &str, 0);
2408 return 1;
2409}
2410__setup("dhash_entries=", set_dhash_entries);
2411
2412static void __init dcache_init_early(void)
2413{
2414 int loop;
2415
2416 /* If hashes are distributed across NUMA nodes, defer
2417 * hash allocation until vmalloc space is available.
2418 */
2419 if (hashdist)
2420 return;
2421
2422 dentry_hashtable =
2423 alloc_large_system_hash("Dentry cache",
2424 sizeof(struct hlist_head),
2425 dhash_entries,
2426 13,
2427 HASH_EARLY,
2428 &d_hash_shift,
2429 &d_hash_mask,
2430 0);
2431
2432 for (loop = 0; loop < (1 << d_hash_shift); loop++)
2433 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
2434}
2435
Denis Cheng74bf17c2007-10-16 23:26:30 -07002436static void __init dcache_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437{
2438 int loop;
2439
Christoph Hellwig312d3ca2010-10-10 05:36:23 -04002440 percpu_counter_init(&nr_dentry, 0);
2441 percpu_counter_init(&nr_dentry_unused, 0);
2442
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 /*
2444 * A constructor could be added for stable state like the lists,
2445 * but it is probably not worth it because of the cache nature
2446 * of the dcache.
2447 */
Christoph Lameter0a31bd52007-05-06 14:49:57 -07002448 dentry_cache = KMEM_CACHE(dentry,
2449 SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|SLAB_MEM_SPREAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450
Rusty Russell8e1f9362007-07-17 04:03:17 -07002451 register_shrinker(&dcache_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452
2453 /* Hash may have been set up in dcache_init_early */
2454 if (!hashdist)
2455 return;
2456
2457 dentry_hashtable =
2458 alloc_large_system_hash("Dentry cache",
2459 sizeof(struct hlist_head),
2460 dhash_entries,
2461 13,
2462 0,
2463 &d_hash_shift,
2464 &d_hash_mask,
2465 0);
2466
2467 for (loop = 0; loop < (1 << d_hash_shift); loop++)
2468 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
2469}
2470
2471/* SLAB cache for __getname() consumers */
Christoph Lametere18b8902006-12-06 20:33:20 -08002472struct kmem_cache *names_cachep __read_mostly;
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002473EXPORT_SYMBOL(names_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475EXPORT_SYMBOL(d_genocide);
2476
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477void __init vfs_caches_init_early(void)
2478{
2479 dcache_init_early();
2480 inode_init_early();
2481}
2482
2483void __init vfs_caches_init(unsigned long mempages)
2484{
2485 unsigned long reserve;
2486
2487 /* Base hash sizes on available memory, with a reserve equal to
2488 150% of current kernel size */
2489
2490 reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);
2491 mempages -= reserve;
2492
2493 names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09002494 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495
Denis Cheng74bf17c2007-10-16 23:26:30 -07002496 dcache_init();
2497 inode_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 files_init(mempages);
Denis Cheng74bf17c2007-10-16 23:26:30 -07002499 mnt_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 bdev_cache_init();
2501 chrdev_init();
2502}