blob: 1e124d4673c65bd3b235f41632f69a6148390394 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/dcache.c
3 *
4 * Complete reimplementation
5 * (C) 1997 Thomas Schoebel-Theuer,
6 * with heavy changes by Linus Torvalds
7 */
8
9/*
10 * Notes on the allocation strategy:
11 *
12 * The dcache is a master of the icache - whenever a dcache entry
13 * exists, the inode will always exist. "iput()" is done either when
14 * the dcache entry is deleted or garbage collected.
15 */
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/syscalls.h>
18#include <linux/string.h>
19#include <linux/mm.h>
20#include <linux/fs.h>
John McCutchan7a91bf72005-08-08 13:52:16 -040021#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/slab.h>
23#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/hash.h>
25#include <linux/cache.h>
26#include <linux/module.h>
27#include <linux/mount.h>
28#include <linux/file.h>
29#include <asm/uaccess.h>
30#include <linux/security.h>
31#include <linux/seqlock.h>
32#include <linux/swap.h>
33#include <linux/bootmem.h>
Al Viro5ad4e532009-03-29 19:50:06 -040034#include <linux/fs_struct.h>
Frederic Weisbecker613afbf2009-07-16 15:44:29 +020035#include <linux/hardirq.h>
David Howells07f3f052006-09-30 20:52:18 +020036#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Nick Piggin789680d2011-01-07 17:49:30 +110038/*
39 * Usage:
40 * dcache_hash_lock protects dcache hash table, s_anon lists
41 *
42 * Ordering:
43 * dcache_lock
44 * dentry->d_lock
45 * dcache_hash_lock
46 *
47 * if (dentry1 < dentry2)
48 * dentry1->d_lock
49 * dentry2->d_lock
50 */
Eric Dumazetfa3536c2006-03-26 01:37:24 -080051int sysctl_vfs_cache_pressure __read_mostly = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052EXPORT_SYMBOL_GPL(sysctl_vfs_cache_pressure);
53
Nick Piggin789680d2011-01-07 17:49:30 +110054static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_hash_lock);
55__cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_lock);
Al Viro74c3cbe2007-07-22 08:04:18 -040056__cacheline_aligned_in_smp DEFINE_SEQLOCK(rename_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58EXPORT_SYMBOL(dcache_lock);
59
Christoph Lametere18b8902006-12-06 20:33:20 -080060static struct kmem_cache *dentry_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62#define DNAME_INLINE_LEN (sizeof(struct dentry)-offsetof(struct dentry,d_iname))
63
64/*
65 * This is the single most critical data structure when it comes
66 * to the dcache: the hashtable for lookups. Somebody should try
67 * to make this good - I've just made it work.
68 *
69 * This hash-function tries to avoid losing too many bits of hash
70 * information, yet avoid using a prime hash-size or similar.
71 */
72#define D_HASHBITS d_hash_shift
73#define D_HASHMASK d_hash_mask
74
Eric Dumazetfa3536c2006-03-26 01:37:24 -080075static unsigned int d_hash_mask __read_mostly;
76static unsigned int d_hash_shift __read_mostly;
77static struct hlist_head *dentry_hashtable __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79/* Statistics gathering. */
80struct dentry_stat_t dentry_stat = {
81 .age_limit = 45,
82};
83
Nick Piggin3e880fb2011-01-07 17:49:19 +110084static DEFINE_PER_CPU(unsigned int, nr_dentry);
Christoph Hellwig312d3ca2010-10-10 05:36:23 -040085
86#if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS)
Nick Piggin3e880fb2011-01-07 17:49:19 +110087static int get_nr_dentry(void)
88{
89 int i;
90 int sum = 0;
91 for_each_possible_cpu(i)
92 sum += per_cpu(nr_dentry, i);
93 return sum < 0 ? 0 : sum;
94}
95
Christoph Hellwig312d3ca2010-10-10 05:36:23 -040096int proc_nr_dentry(ctl_table *table, int write, void __user *buffer,
97 size_t *lenp, loff_t *ppos)
98{
Nick Piggin3e880fb2011-01-07 17:49:19 +110099 dentry_stat.nr_dentry = get_nr_dentry();
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400100 return proc_dointvec(table, write, buffer, lenp, ppos);
101}
102#endif
103
Christoph Hellwig9c82ab92010-10-10 05:36:22 -0400104static void __d_free(struct rcu_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
Christoph Hellwig9c82ab92010-10-10 05:36:22 -0400106 struct dentry *dentry = container_of(head, struct dentry, d_u.d_rcu);
107
Arjan van de Venfd217f42008-10-21 06:47:33 -0700108 WARN_ON(!list_empty(&dentry->d_alias));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 if (dname_external(dentry))
110 kfree(dentry->d_name.name);
111 kmem_cache_free(dentry_cache, dentry);
112}
113
114/*
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400115 * no dcache_lock, please.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 */
117static void d_free(struct dentry *dentry)
118{
Nick Piggin3e880fb2011-01-07 17:49:19 +1100119 this_cpu_dec(nr_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 if (dentry->d_op && dentry->d_op->d_release)
121 dentry->d_op->d_release(dentry);
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400122
Eric Dumazetb3423412006-12-06 20:38:48 -0800123 /* if dentry was never inserted into hash, immediate free is OK */
Akinobu Mitae8462ca2008-02-06 01:37:07 -0800124 if (hlist_unhashed(&dentry->d_hash))
Christoph Hellwig9c82ab92010-10-10 05:36:22 -0400125 __d_free(&dentry->d_u.d_rcu);
Eric Dumazetb3423412006-12-06 20:38:48 -0800126 else
Christoph Hellwig9c82ab92010-10-10 05:36:22 -0400127 call_rcu(&dentry->d_u.d_rcu, __d_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
129
130/*
131 * Release the dentry's inode, using the filesystem
132 * d_iput() operation if defined.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800134static void dentry_iput(struct dentry * dentry)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200135 __releases(dentry->d_lock)
136 __releases(dcache_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
138 struct inode *inode = dentry->d_inode;
139 if (inode) {
140 dentry->d_inode = NULL;
141 list_del_init(&dentry->d_alias);
142 spin_unlock(&dentry->d_lock);
143 spin_unlock(&dcache_lock);
Linus Torvaldsf805fbd2005-09-19 19:54:29 -0700144 if (!inode->i_nlink)
145 fsnotify_inoderemove(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 if (dentry->d_op && dentry->d_op->d_iput)
147 dentry->d_op->d_iput(dentry, inode);
148 else
149 iput(inode);
150 } else {
151 spin_unlock(&dentry->d_lock);
152 spin_unlock(&dcache_lock);
153 }
154}
155
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700156/*
Christoph Hellwiga4633352010-10-10 05:36:26 -0400157 * dentry_lru_(add|del|move_tail) must be called with dcache_lock held.
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700158 */
159static void dentry_lru_add(struct dentry *dentry)
160{
Christoph Hellwiga4633352010-10-10 05:36:26 -0400161 if (list_empty(&dentry->d_lru)) {
162 list_add(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
163 dentry->d_sb->s_nr_dentry_unused++;
Nick Piggin86c87492011-01-07 17:49:18 +1100164 dentry_stat.nr_unused++;
Christoph Hellwiga4633352010-10-10 05:36:26 -0400165 }
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700166}
167
168static void dentry_lru_del(struct dentry *dentry)
169{
170 if (!list_empty(&dentry->d_lru)) {
Christoph Hellwiga4633352010-10-10 05:36:26 -0400171 list_del_init(&dentry->d_lru);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700172 dentry->d_sb->s_nr_dentry_unused--;
Nick Piggin86c87492011-01-07 17:49:18 +1100173 dentry_stat.nr_unused--;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700174 }
175}
176
Christoph Hellwiga4633352010-10-10 05:36:26 -0400177static void dentry_lru_move_tail(struct dentry *dentry)
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700178{
Christoph Hellwiga4633352010-10-10 05:36:26 -0400179 if (list_empty(&dentry->d_lru)) {
180 list_add_tail(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
181 dentry->d_sb->s_nr_dentry_unused++;
Nick Piggin86c87492011-01-07 17:49:18 +1100182 dentry_stat.nr_unused++;
Christoph Hellwiga4633352010-10-10 05:36:26 -0400183 } else {
184 list_move_tail(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700185 }
186}
187
Miklos Szeredid52b9082007-05-08 00:23:46 -0700188/**
189 * d_kill - kill dentry and return parent
190 * @dentry: dentry to kill
191 *
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200192 * The dentry must already be unhashed and removed from the LRU.
Miklos Szeredid52b9082007-05-08 00:23:46 -0700193 *
194 * If this is the root of the dentry tree, return NULL.
195 */
196static struct dentry *d_kill(struct dentry *dentry)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200197 __releases(dentry->d_lock)
198 __releases(dcache_lock)
Miklos Szeredid52b9082007-05-08 00:23:46 -0700199{
200 struct dentry *parent;
201
202 list_del(&dentry->d_u.d_child);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700203 /*drops the locks, at that point nobody can reach this dentry */
204 dentry_iput(dentry);
OGAWA Hirofumi871c0062008-10-16 07:50:27 +0900205 if (IS_ROOT(dentry))
206 parent = NULL;
207 else
208 parent = dentry->d_parent;
Miklos Szeredid52b9082007-05-08 00:23:46 -0700209 d_free(dentry);
OGAWA Hirofumi871c0062008-10-16 07:50:27 +0900210 return parent;
Miklos Szeredid52b9082007-05-08 00:23:46 -0700211}
212
Nick Piggin789680d2011-01-07 17:49:30 +1100213/**
214 * d_drop - drop a dentry
215 * @dentry: dentry to drop
216 *
217 * d_drop() unhashes the entry from the parent dentry hashes, so that it won't
218 * be found through a VFS lookup any more. Note that this is different from
219 * deleting the dentry - d_delete will try to mark the dentry negative if
220 * possible, giving a successful _negative_ lookup, while d_drop will
221 * just make the cache lookup fail.
222 *
223 * d_drop() is used mainly for stuff that wants to invalidate a dentry for some
224 * reason (NFS timeouts or autofs deletes).
225 *
226 * __d_drop requires dentry->d_lock.
227 */
228void __d_drop(struct dentry *dentry)
229{
230 if (!(dentry->d_flags & DCACHE_UNHASHED)) {
231 dentry->d_flags |= DCACHE_UNHASHED;
232 spin_lock(&dcache_hash_lock);
233 hlist_del_rcu(&dentry->d_hash);
234 spin_unlock(&dcache_hash_lock);
235 }
236}
237EXPORT_SYMBOL(__d_drop);
238
239void d_drop(struct dentry *dentry)
240{
241 spin_lock(&dcache_lock);
242 spin_lock(&dentry->d_lock);
243 __d_drop(dentry);
244 spin_unlock(&dentry->d_lock);
245 spin_unlock(&dcache_lock);
246}
247EXPORT_SYMBOL(d_drop);
248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249/*
250 * This is dput
251 *
252 * This is complicated by the fact that we do not want to put
253 * dentries that are no longer on any hash chain on the unused
254 * list: we'd much rather just get rid of them immediately.
255 *
256 * However, that implies that we have to traverse the dentry
257 * tree upwards to the parents which might _also_ now be
258 * scheduled for deletion (it may have been only waiting for
259 * its last child to go away).
260 *
261 * This tail recursion is done by hand as we don't want to depend
262 * on the compiler to always get this right (gcc generally doesn't).
263 * Real recursion would eat up our stack space.
264 */
265
266/*
267 * dput - release a dentry
268 * @dentry: dentry to release
269 *
270 * Release a dentry. This will drop the usage count and if appropriate
271 * call the dentry unlink method as well as removing it from the queues and
272 * releasing its resources. If the parent dentries were scheduled for release
273 * they too may now get deleted.
274 *
275 * no dcache lock, please.
276 */
277
278void dput(struct dentry *dentry)
279{
280 if (!dentry)
281 return;
282
283repeat:
284 if (atomic_read(&dentry->d_count) == 1)
285 might_sleep();
286 if (!atomic_dec_and_lock(&dentry->d_count, &dcache_lock))
287 return;
288
289 spin_lock(&dentry->d_lock);
290 if (atomic_read(&dentry->d_count)) {
291 spin_unlock(&dentry->d_lock);
292 spin_unlock(&dcache_lock);
293 return;
294 }
295
296 /*
297 * AV: ->d_delete() is _NOT_ allowed to block now.
298 */
299 if (dentry->d_op && dentry->d_op->d_delete) {
300 if (dentry->d_op->d_delete(dentry))
301 goto unhash_it;
302 }
Nick Piggin265ac902010-10-10 05:36:24 -0400303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 /* Unreachable? Get rid of it */
305 if (d_unhashed(dentry))
306 goto kill_it;
Nick Piggin265ac902010-10-10 05:36:24 -0400307
308 /* Otherwise leave it cached and ensure it's on the LRU */
309 dentry->d_flags |= DCACHE_REFERENCED;
Christoph Hellwiga4633352010-10-10 05:36:26 -0400310 dentry_lru_add(dentry);
Nick Piggin265ac902010-10-10 05:36:24 -0400311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 spin_unlock(&dentry->d_lock);
313 spin_unlock(&dcache_lock);
314 return;
315
316unhash_it:
317 __d_drop(dentry);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700318kill_it:
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700319 /* if dentry was on the d_lru list delete it from there */
320 dentry_lru_del(dentry);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700321 dentry = d_kill(dentry);
322 if (dentry)
323 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700325EXPORT_SYMBOL(dput);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327/**
328 * d_invalidate - invalidate a dentry
329 * @dentry: dentry to invalidate
330 *
331 * Try to invalidate the dentry if it turns out to be
332 * possible. If there are other dentries that can be
333 * reached through this one we can't delete it and we
334 * return -EBUSY. On success we return 0.
335 *
336 * no dcache lock.
337 */
338
339int d_invalidate(struct dentry * dentry)
340{
341 /*
342 * If it's already been dropped, return OK.
343 */
344 spin_lock(&dcache_lock);
345 if (d_unhashed(dentry)) {
346 spin_unlock(&dcache_lock);
347 return 0;
348 }
349 /*
350 * Check whether to do a partial shrink_dcache
351 * to get rid of unused child entries.
352 */
353 if (!list_empty(&dentry->d_subdirs)) {
354 spin_unlock(&dcache_lock);
355 shrink_dcache_parent(dentry);
356 spin_lock(&dcache_lock);
357 }
358
359 /*
360 * Somebody else still using it?
361 *
362 * If it's a directory, we can't drop it
363 * for fear of somebody re-populating it
364 * with children (even though dropping it
365 * would make it unreachable from the root,
366 * we might still populate it if it was a
367 * working directory or similar).
368 */
369 spin_lock(&dentry->d_lock);
370 if (atomic_read(&dentry->d_count) > 1) {
371 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
372 spin_unlock(&dentry->d_lock);
373 spin_unlock(&dcache_lock);
374 return -EBUSY;
375 }
376 }
377
378 __d_drop(dentry);
379 spin_unlock(&dentry->d_lock);
380 spin_unlock(&dcache_lock);
381 return 0;
382}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700383EXPORT_SYMBOL(d_invalidate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385/* This should be called _only_ with dcache_lock held */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386static inline struct dentry * __dget_locked(struct dentry *dentry)
387{
388 atomic_inc(&dentry->d_count);
Christoph Hellwiga4633352010-10-10 05:36:26 -0400389 dentry_lru_del(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 return dentry;
391}
392
393struct dentry * dget_locked(struct dentry *dentry)
394{
395 return __dget_locked(dentry);
396}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700397EXPORT_SYMBOL(dget_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399/**
400 * d_find_alias - grab a hashed alias of inode
401 * @inode: inode in question
402 * @want_discon: flag, used by d_splice_alias, to request
403 * that only a DISCONNECTED alias be returned.
404 *
405 * If inode has a hashed alias, or is a directory and has any alias,
406 * acquire the reference to alias and return it. Otherwise return NULL.
407 * Notice that if inode is a directory there can be only one alias and
408 * it can be unhashed only if it has no children, or if it is the root
409 * of a filesystem.
410 *
NeilBrown21c0d8f2006-10-04 02:16:16 -0700411 * If the inode has an IS_ROOT, DCACHE_DISCONNECTED alias, then prefer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 * any other hashed alias over that one unless @want_discon is set,
NeilBrown21c0d8f2006-10-04 02:16:16 -0700413 * in which case only return an IS_ROOT, DCACHE_DISCONNECTED alias.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 */
415
416static struct dentry * __d_find_alias(struct inode *inode, int want_discon)
417{
418 struct list_head *head, *next, *tmp;
419 struct dentry *alias, *discon_alias=NULL;
420
421 head = &inode->i_dentry;
422 next = inode->i_dentry.next;
423 while (next != head) {
424 tmp = next;
425 next = tmp->next;
426 prefetch(next);
427 alias = list_entry(tmp, struct dentry, d_alias);
428 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
NeilBrown21c0d8f2006-10-04 02:16:16 -0700429 if (IS_ROOT(alias) &&
430 (alias->d_flags & DCACHE_DISCONNECTED))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 discon_alias = alias;
432 else if (!want_discon) {
433 __dget_locked(alias);
434 return alias;
435 }
436 }
437 }
438 if (discon_alias)
439 __dget_locked(discon_alias);
440 return discon_alias;
441}
442
443struct dentry * d_find_alias(struct inode *inode)
444{
David Howells214fda12006-03-25 03:06:36 -0800445 struct dentry *de = NULL;
446
447 if (!list_empty(&inode->i_dentry)) {
448 spin_lock(&dcache_lock);
449 de = __d_find_alias(inode, 0);
450 spin_unlock(&dcache_lock);
451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 return de;
453}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700454EXPORT_SYMBOL(d_find_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456/*
457 * Try to kill dentries associated with this inode.
458 * WARNING: you must own a reference to inode.
459 */
460void d_prune_aliases(struct inode *inode)
461{
Domen Puncer0cdca3f2005-09-10 00:27:07 -0700462 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463restart:
464 spin_lock(&dcache_lock);
Domen Puncer0cdca3f2005-09-10 00:27:07 -0700465 list_for_each_entry(dentry, &inode->i_dentry, d_alias) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 spin_lock(&dentry->d_lock);
467 if (!atomic_read(&dentry->d_count)) {
468 __dget_locked(dentry);
469 __d_drop(dentry);
470 spin_unlock(&dentry->d_lock);
471 spin_unlock(&dcache_lock);
472 dput(dentry);
473 goto restart;
474 }
475 spin_unlock(&dentry->d_lock);
476 }
477 spin_unlock(&dcache_lock);
478}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700479EXPORT_SYMBOL(d_prune_aliases);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481/*
Andrew Mortond702ccb2006-06-22 14:47:31 -0700482 * Throw away a dentry - free the inode, dput the parent. This requires that
483 * the LRU list has already been removed.
484 *
Miklos Szeredi85864e12007-10-16 23:27:09 -0700485 * Try to prune ancestors as well. This is necessary to prevent
486 * quadratic behavior of shrink_dcache_parent(), but is also expected
487 * to be beneficial in reducing dentry cache fragmentation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 */
Miklos Szeredi85864e12007-10-16 23:27:09 -0700489static void prune_one_dentry(struct dentry * dentry)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200490 __releases(dentry->d_lock)
491 __releases(dcache_lock)
492 __acquires(dcache_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 __d_drop(dentry);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700495 dentry = d_kill(dentry);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700496
497 /*
498 * Prune ancestors. Locking is simpler than in dput(),
499 * because dcache_lock needs to be taken anyway.
500 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 spin_lock(&dcache_lock);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700502 while (dentry) {
503 if (!atomic_dec_and_lock(&dentry->d_count, &dentry->d_lock))
504 return;
505
Christoph Hellwiga4633352010-10-10 05:36:26 -0400506 dentry_lru_del(dentry);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700507 __d_drop(dentry);
508 dentry = d_kill(dentry);
509 spin_lock(&dcache_lock);
510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511}
512
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400513static void shrink_dentry_list(struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700515 struct dentry *dentry;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700516
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400517 while (!list_empty(list)) {
518 dentry = list_entry(list->prev, struct dentry, d_lru);
Christoph Hellwiga4633352010-10-10 05:36:26 -0400519 dentry_lru_del(dentry);
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 /*
522 * We found an inuse dentry which was not removed from
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700523 * the LRU because of laziness during lookup. Do not free
524 * it - just keep it off the LRU list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 */
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400526 spin_lock(&dentry->d_lock);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700527 if (atomic_read(&dentry->d_count)) {
528 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 continue;
530 }
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700531 prune_one_dentry(dentry);
532 /* dentry->d_lock was dropped in prune_one_dentry() */
533 cond_resched_lock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 }
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400535}
536
537/**
538 * __shrink_dcache_sb - shrink the dentry LRU on a given superblock
539 * @sb: superblock to shrink dentry LRU.
540 * @count: number of entries to prune
541 * @flags: flags to control the dentry processing
542 *
543 * If flags contains DCACHE_REFERENCED reference dentries will not be pruned.
544 */
545static void __shrink_dcache_sb(struct super_block *sb, int *count, int flags)
546{
547 /* called from prune_dcache() and shrink_dcache_parent() */
548 struct dentry *dentry;
549 LIST_HEAD(referenced);
550 LIST_HEAD(tmp);
551 int cnt = *count;
552
553 spin_lock(&dcache_lock);
554 while (!list_empty(&sb->s_dentry_lru)) {
555 dentry = list_entry(sb->s_dentry_lru.prev,
556 struct dentry, d_lru);
557 BUG_ON(dentry->d_sb != sb);
558
559 /*
560 * If we are honouring the DCACHE_REFERENCED flag and the
561 * dentry has this flag set, don't free it. Clear the flag
562 * and put it back on the LRU.
563 */
564 if (flags & DCACHE_REFERENCED) {
565 spin_lock(&dentry->d_lock);
566 if (dentry->d_flags & DCACHE_REFERENCED) {
567 dentry->d_flags &= ~DCACHE_REFERENCED;
568 list_move(&dentry->d_lru, &referenced);
569 spin_unlock(&dentry->d_lock);
570 cond_resched_lock(&dcache_lock);
571 continue;
572 }
573 spin_unlock(&dentry->d_lock);
574 }
575
576 list_move_tail(&dentry->d_lru, &tmp);
577 if (!--cnt)
578 break;
579 cond_resched_lock(&dcache_lock);
580 }
581
582 *count = cnt;
583 shrink_dentry_list(&tmp);
584
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700585 if (!list_empty(&referenced))
586 list_splice(&referenced, &sb->s_dentry_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 spin_unlock(&dcache_lock);
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589}
590
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700591/**
592 * prune_dcache - shrink the dcache
593 * @count: number of entries to try to free
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 *
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700595 * Shrink the dcache. This is done when we need more memory, or simply when we
596 * need to unmount something (at which point we need to unuse all dentries).
597 *
598 * This function may fail to free any resources if all the dentries are in use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 */
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700600static void prune_dcache(int count)
601{
Al Virodca33252010-07-25 02:31:46 +0400602 struct super_block *sb, *p = NULL;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700603 int w_count;
Nick Piggin86c87492011-01-07 17:49:18 +1100604 int unused = dentry_stat.nr_unused;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700605 int prune_ratio;
606 int pruned;
607
608 if (unused == 0 || count == 0)
609 return;
610 spin_lock(&dcache_lock);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700611 if (count >= unused)
612 prune_ratio = 1;
613 else
614 prune_ratio = unused / count;
615 spin_lock(&sb_lock);
Al Virodca33252010-07-25 02:31:46 +0400616 list_for_each_entry(sb, &super_blocks, s_list) {
Al Viro551de6f2010-03-22 19:36:35 -0400617 if (list_empty(&sb->s_instances))
618 continue;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700619 if (sb->s_nr_dentry_unused == 0)
620 continue;
621 sb->s_count++;
622 /* Now, we reclaim unused dentrins with fairness.
623 * We reclaim them same percentage from each superblock.
624 * We calculate number of dentries to scan on this sb
625 * as follows, but the implementation is arranged to avoid
626 * overflows:
627 * number of dentries to scan on this sb =
628 * count * (number of dentries on this sb /
629 * number of dentries in the machine)
630 */
631 spin_unlock(&sb_lock);
632 if (prune_ratio != 1)
633 w_count = (sb->s_nr_dentry_unused / prune_ratio) + 1;
634 else
635 w_count = sb->s_nr_dentry_unused;
636 pruned = w_count;
637 /*
638 * We need to be sure this filesystem isn't being unmounted,
639 * otherwise we could race with generic_shutdown_super(), and
640 * end up holding a reference to an inode while the filesystem
641 * is unmounted. So we try to get s_umount, and make sure
642 * s_root isn't NULL.
643 */
644 if (down_read_trylock(&sb->s_umount)) {
645 if ((sb->s_root != NULL) &&
646 (!list_empty(&sb->s_dentry_lru))) {
647 spin_unlock(&dcache_lock);
648 __shrink_dcache_sb(sb, &w_count,
649 DCACHE_REFERENCED);
650 pruned -= w_count;
651 spin_lock(&dcache_lock);
652 }
653 up_read(&sb->s_umount);
654 }
655 spin_lock(&sb_lock);
Al Virodca33252010-07-25 02:31:46 +0400656 if (p)
657 __put_super(p);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700658 count -= pruned;
Al Virodca33252010-07-25 02:31:46 +0400659 p = sb;
Al Viro79893c12010-03-22 20:27:55 -0400660 /* more work left to do? */
661 if (count <= 0)
662 break;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700663 }
Al Virodca33252010-07-25 02:31:46 +0400664 if (p)
665 __put_super(p);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700666 spin_unlock(&sb_lock);
667 spin_unlock(&dcache_lock);
668}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670/**
671 * shrink_dcache_sb - shrink dcache for a superblock
672 * @sb: superblock
673 *
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400674 * Shrink the dcache for the specified super block. This is used to free
675 * the dcache before unmounting a file system.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 */
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400677void shrink_dcache_sb(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400679 LIST_HEAD(tmp);
680
681 spin_lock(&dcache_lock);
682 while (!list_empty(&sb->s_dentry_lru)) {
683 list_splice_init(&sb->s_dentry_lru, &tmp);
684 shrink_dentry_list(&tmp);
685 }
686 spin_unlock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700688EXPORT_SYMBOL(shrink_dcache_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690/*
David Howellsc636ebd2006-10-11 01:22:19 -0700691 * destroy a single subtree of dentries for unmount
692 * - see the comments on shrink_dcache_for_umount() for a description of the
693 * locking
694 */
695static void shrink_dcache_for_umount_subtree(struct dentry *dentry)
696{
697 struct dentry *parent;
David Howellsf8713572006-10-28 10:38:46 -0700698 unsigned detached = 0;
David Howellsc636ebd2006-10-11 01:22:19 -0700699
700 BUG_ON(!IS_ROOT(dentry));
701
702 /* detach this root from the system */
703 spin_lock(&dcache_lock);
Christoph Hellwiga4633352010-10-10 05:36:26 -0400704 dentry_lru_del(dentry);
David Howellsc636ebd2006-10-11 01:22:19 -0700705 __d_drop(dentry);
706 spin_unlock(&dcache_lock);
707
708 for (;;) {
709 /* descend to the first leaf in the current subtree */
710 while (!list_empty(&dentry->d_subdirs)) {
711 struct dentry *loop;
712
713 /* this is a branch with children - detach all of them
714 * from the system in one go */
715 spin_lock(&dcache_lock);
716 list_for_each_entry(loop, &dentry->d_subdirs,
717 d_u.d_child) {
Christoph Hellwiga4633352010-10-10 05:36:26 -0400718 dentry_lru_del(loop);
David Howellsc636ebd2006-10-11 01:22:19 -0700719 __d_drop(loop);
720 cond_resched_lock(&dcache_lock);
721 }
722 spin_unlock(&dcache_lock);
723
724 /* move to the first child */
725 dentry = list_entry(dentry->d_subdirs.next,
726 struct dentry, d_u.d_child);
727 }
728
729 /* consume the dentries from this leaf up through its parents
730 * until we find one with children or run out altogether */
731 do {
732 struct inode *inode;
733
734 if (atomic_read(&dentry->d_count) != 0) {
735 printk(KERN_ERR
736 "BUG: Dentry %p{i=%lx,n=%s}"
737 " still in use (%d)"
738 " [unmount of %s %s]\n",
739 dentry,
740 dentry->d_inode ?
741 dentry->d_inode->i_ino : 0UL,
742 dentry->d_name.name,
743 atomic_read(&dentry->d_count),
744 dentry->d_sb->s_type->name,
745 dentry->d_sb->s_id);
746 BUG();
747 }
748
OGAWA Hirofumi871c0062008-10-16 07:50:27 +0900749 if (IS_ROOT(dentry))
David Howellsc636ebd2006-10-11 01:22:19 -0700750 parent = NULL;
OGAWA Hirofumi871c0062008-10-16 07:50:27 +0900751 else {
752 parent = dentry->d_parent;
David Howellsc636ebd2006-10-11 01:22:19 -0700753 atomic_dec(&parent->d_count);
OGAWA Hirofumi871c0062008-10-16 07:50:27 +0900754 }
David Howellsc636ebd2006-10-11 01:22:19 -0700755
756 list_del(&dentry->d_u.d_child);
David Howellsf8713572006-10-28 10:38:46 -0700757 detached++;
David Howellsc636ebd2006-10-11 01:22:19 -0700758
759 inode = dentry->d_inode;
760 if (inode) {
761 dentry->d_inode = NULL;
762 list_del_init(&dentry->d_alias);
763 if (dentry->d_op && dentry->d_op->d_iput)
764 dentry->d_op->d_iput(dentry, inode);
765 else
766 iput(inode);
767 }
768
769 d_free(dentry);
770
771 /* finished when we fall off the top of the tree,
772 * otherwise we ascend to the parent and move to the
773 * next sibling if there is one */
774 if (!parent)
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400775 return;
David Howellsc636ebd2006-10-11 01:22:19 -0700776 dentry = parent;
David Howellsc636ebd2006-10-11 01:22:19 -0700777 } while (list_empty(&dentry->d_subdirs));
778
779 dentry = list_entry(dentry->d_subdirs.next,
780 struct dentry, d_u.d_child);
781 }
782}
783
784/*
785 * destroy the dentries attached to a superblock on unmounting
786 * - we don't need to use dentry->d_lock, and only need dcache_lock when
787 * removing the dentry from the system lists and hashes because:
788 * - the superblock is detached from all mountings and open files, so the
789 * dentry trees will not be rearranged by the VFS
790 * - s_umount is write-locked, so the memory pressure shrinker will ignore
791 * any dentries belonging to this superblock that it comes across
792 * - the filesystem itself is no longer permitted to rearrange the dentries
793 * in this superblock
794 */
795void shrink_dcache_for_umount(struct super_block *sb)
796{
797 struct dentry *dentry;
798
799 if (down_read_trylock(&sb->s_umount))
800 BUG();
801
802 dentry = sb->s_root;
803 sb->s_root = NULL;
804 atomic_dec(&dentry->d_count);
805 shrink_dcache_for_umount_subtree(dentry);
806
807 while (!hlist_empty(&sb->s_anon)) {
808 dentry = hlist_entry(sb->s_anon.first, struct dentry, d_hash);
809 shrink_dcache_for_umount_subtree(dentry);
810 }
811}
812
813/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 * Search for at least 1 mount point in the dentry's subdirs.
815 * We descend to the next level whenever the d_subdirs
816 * list is non-empty and continue searching.
817 */
818
819/**
820 * have_submounts - check for mounts over a dentry
821 * @parent: dentry to check.
822 *
823 * Return true if the parent or its subdirectories contain
824 * a mount point
825 */
826
827int have_submounts(struct dentry *parent)
828{
829 struct dentry *this_parent = parent;
830 struct list_head *next;
831
832 spin_lock(&dcache_lock);
833 if (d_mountpoint(parent))
834 goto positive;
835repeat:
836 next = this_parent->d_subdirs.next;
837resume:
838 while (next != &this_parent->d_subdirs) {
839 struct list_head *tmp = next;
Eric Dumazet5160ee62006-01-08 01:03:32 -0800840 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 next = tmp->next;
842 /* Have we found a mount point ? */
843 if (d_mountpoint(dentry))
844 goto positive;
845 if (!list_empty(&dentry->d_subdirs)) {
846 this_parent = dentry;
847 goto repeat;
848 }
849 }
850 /*
851 * All done at this level ... ascend and resume the search.
852 */
853 if (this_parent != parent) {
Eric Dumazet5160ee62006-01-08 01:03:32 -0800854 next = this_parent->d_u.d_child.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 this_parent = this_parent->d_parent;
856 goto resume;
857 }
858 spin_unlock(&dcache_lock);
859 return 0; /* No mount points found in tree */
860positive:
861 spin_unlock(&dcache_lock);
862 return 1;
863}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700864EXPORT_SYMBOL(have_submounts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
866/*
867 * Search the dentry child list for the specified parent,
868 * and move any unused dentries to the end of the unused
869 * list for prune_dcache(). We descend to the next level
870 * whenever the d_subdirs list is non-empty and continue
871 * searching.
872 *
873 * It returns zero iff there are no unused children,
874 * otherwise it returns the number of children moved to
875 * the end of the unused list. This may not be the total
876 * number of unused children, because select_parent can
877 * drop the lock and return early due to latency
878 * constraints.
879 */
880static int select_parent(struct dentry * parent)
881{
882 struct dentry *this_parent = parent;
883 struct list_head *next;
884 int found = 0;
885
886 spin_lock(&dcache_lock);
887repeat:
888 next = this_parent->d_subdirs.next;
889resume:
890 while (next != &this_parent->d_subdirs) {
891 struct list_head *tmp = next;
Eric Dumazet5160ee62006-01-08 01:03:32 -0800892 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 next = tmp->next;
894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 /*
896 * move only zero ref count dentries to the end
897 * of the unused list for prune_dcache
898 */
899 if (!atomic_read(&dentry->d_count)) {
Christoph Hellwiga4633352010-10-10 05:36:26 -0400900 dentry_lru_move_tail(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 found++;
Christoph Hellwiga4633352010-10-10 05:36:26 -0400902 } else {
903 dentry_lru_del(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 }
905
906 /*
907 * We can return to the caller if we have found some (this
908 * ensures forward progress). We'll be coming back to find
909 * the rest.
910 */
911 if (found && need_resched())
912 goto out;
913
914 /*
915 * Descend a level if the d_subdirs list is non-empty.
916 */
917 if (!list_empty(&dentry->d_subdirs)) {
918 this_parent = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 goto repeat;
920 }
921 }
922 /*
923 * All done at this level ... ascend and resume the search.
924 */
925 if (this_parent != parent) {
Eric Dumazet5160ee62006-01-08 01:03:32 -0800926 next = this_parent->d_u.d_child.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 this_parent = this_parent->d_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 goto resume;
929 }
930out:
931 spin_unlock(&dcache_lock);
932 return found;
933}
934
935/**
936 * shrink_dcache_parent - prune dcache
937 * @parent: parent of entries to prune
938 *
939 * Prune the dcache to remove unused children of the parent dentry.
940 */
941
942void shrink_dcache_parent(struct dentry * parent)
943{
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700944 struct super_block *sb = parent->d_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 int found;
946
947 while ((found = select_parent(parent)) != 0)
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700948 __shrink_dcache_sb(sb, &found, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700950EXPORT_SYMBOL(shrink_dcache_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952/*
953 * Scan `nr' dentries and return the number which remain.
954 *
955 * We need to avoid reentering the filesystem if the caller is performing a
956 * GFP_NOFS allocation attempt. One example deadlock is:
957 *
958 * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache->
959 * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->put_inode->
960 * ext2_discard_prealloc->ext2_free_blocks->lock_super->DEADLOCK.
961 *
962 * In this case we return -1 to tell the caller that we baled.
963 */
Dave Chinner7f8275d2010-07-19 14:56:17 +1000964static int shrink_dcache_memory(struct shrinker *shrink, int nr, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965{
966 if (nr) {
967 if (!(gfp_mask & __GFP_FS))
968 return -1;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700969 prune_dcache(nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 }
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400971
Nick Piggin86c87492011-01-07 17:49:18 +1100972 return (dentry_stat.nr_unused / 100) * sysctl_vfs_cache_pressure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973}
974
Rusty Russell8e1f9362007-07-17 04:03:17 -0700975static struct shrinker dcache_shrinker = {
976 .shrink = shrink_dcache_memory,
977 .seeks = DEFAULT_SEEKS,
978};
979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980/**
981 * d_alloc - allocate a dcache entry
982 * @parent: parent of entry to allocate
983 * @name: qstr of the name
984 *
985 * Allocates a dentry. It returns %NULL if there is insufficient memory
986 * available. On a success the dentry is returned. The name passed in is
987 * copied and the copy passed in may be reused after this call.
988 */
989
990struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
991{
992 struct dentry *dentry;
993 char *dname;
994
Mel Gormane12ba742007-10-16 01:25:52 -0700995 dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 if (!dentry)
997 return NULL;
998
999 if (name->len > DNAME_INLINE_LEN-1) {
1000 dname = kmalloc(name->len + 1, GFP_KERNEL);
1001 if (!dname) {
1002 kmem_cache_free(dentry_cache, dentry);
1003 return NULL;
1004 }
1005 } else {
1006 dname = dentry->d_iname;
1007 }
1008 dentry->d_name.name = dname;
1009
1010 dentry->d_name.len = name->len;
1011 dentry->d_name.hash = name->hash;
1012 memcpy(dname, name->name, name->len);
1013 dname[name->len] = 0;
1014
1015 atomic_set(&dentry->d_count, 1);
1016 dentry->d_flags = DCACHE_UNHASHED;
1017 spin_lock_init(&dentry->d_lock);
1018 dentry->d_inode = NULL;
1019 dentry->d_parent = NULL;
1020 dentry->d_sb = NULL;
1021 dentry->d_op = NULL;
1022 dentry->d_fsdata = NULL;
1023 dentry->d_mounted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 INIT_HLIST_NODE(&dentry->d_hash);
1025 INIT_LIST_HEAD(&dentry->d_lru);
1026 INIT_LIST_HEAD(&dentry->d_subdirs);
1027 INIT_LIST_HEAD(&dentry->d_alias);
1028
1029 if (parent) {
1030 dentry->d_parent = dget(parent);
1031 dentry->d_sb = parent->d_sb;
1032 } else {
Eric Dumazet5160ee62006-01-08 01:03:32 -08001033 INIT_LIST_HEAD(&dentry->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 }
1035
1036 spin_lock(&dcache_lock);
1037 if (parent)
Eric Dumazet5160ee62006-01-08 01:03:32 -08001038 list_add(&dentry->d_u.d_child, &parent->d_subdirs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 spin_unlock(&dcache_lock);
1040
Nick Piggin3e880fb2011-01-07 17:49:19 +11001041 this_cpu_inc(nr_dentry);
Christoph Hellwig312d3ca2010-10-10 05:36:23 -04001042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 return dentry;
1044}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001045EXPORT_SYMBOL(d_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
1047struct dentry *d_alloc_name(struct dentry *parent, const char *name)
1048{
1049 struct qstr q;
1050
1051 q.name = name;
1052 q.len = strlen(name);
1053 q.hash = full_name_hash(q.name, q.len);
1054 return d_alloc(parent, &q);
1055}
H Hartley Sweetenef26ca92009-09-29 20:09:42 -04001056EXPORT_SYMBOL(d_alloc_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001058/* the caller must hold dcache_lock */
1059static void __d_instantiate(struct dentry *dentry, struct inode *inode)
1060{
1061 if (inode)
1062 list_add(&dentry->d_alias, &inode->i_dentry);
1063 dentry->d_inode = inode;
1064 fsnotify_d_instantiate(dentry, inode);
1065}
1066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067/**
1068 * d_instantiate - fill in inode information for a dentry
1069 * @entry: dentry to complete
1070 * @inode: inode to attach to this dentry
1071 *
1072 * Fill in inode information in the entry.
1073 *
1074 * This turns negative dentries into productive full members
1075 * of society.
1076 *
1077 * NOTE! This assumes that the inode count has been incremented
1078 * (or otherwise set) by the caller to indicate that it is now
1079 * in use by the dcache.
1080 */
1081
1082void d_instantiate(struct dentry *entry, struct inode * inode)
1083{
Eric Sesterhenn28133c72006-03-26 18:25:39 +02001084 BUG_ON(!list_empty(&entry->d_alias));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 spin_lock(&dcache_lock);
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001086 __d_instantiate(entry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 spin_unlock(&dcache_lock);
1088 security_d_instantiate(entry, inode);
1089}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001090EXPORT_SYMBOL(d_instantiate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
1092/**
1093 * d_instantiate_unique - instantiate a non-aliased dentry
1094 * @entry: dentry to instantiate
1095 * @inode: inode to attach to this dentry
1096 *
1097 * Fill in inode information in the entry. On success, it returns NULL.
1098 * If an unhashed alias of "entry" already exists, then we return the
Oleg Drokine866cfa2006-01-09 20:52:51 -08001099 * aliased dentry instead and drop one reference to inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 *
1101 * Note that in order to avoid conflicts with rename() etc, the caller
1102 * had better be holding the parent directory semaphore.
Oleg Drokine866cfa2006-01-09 20:52:51 -08001103 *
1104 * This also assumes that the inode count has been incremented
1105 * (or otherwise set) by the caller to indicate that it is now
1106 * in use by the dcache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 */
David Howells770bfad2006-08-22 20:06:07 -04001108static struct dentry *__d_instantiate_unique(struct dentry *entry,
1109 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110{
1111 struct dentry *alias;
1112 int len = entry->d_name.len;
1113 const char *name = entry->d_name.name;
1114 unsigned int hash = entry->d_name.hash;
1115
David Howells770bfad2006-08-22 20:06:07 -04001116 if (!inode) {
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001117 __d_instantiate(entry, NULL);
David Howells770bfad2006-08-22 20:06:07 -04001118 return NULL;
1119 }
1120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 list_for_each_entry(alias, &inode->i_dentry, d_alias) {
1122 struct qstr *qstr = &alias->d_name;
1123
1124 if (qstr->hash != hash)
1125 continue;
1126 if (alias->d_parent != entry->d_parent)
1127 continue;
1128 if (qstr->len != len)
1129 continue;
1130 if (memcmp(qstr->name, name, len))
1131 continue;
1132 dget_locked(alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 return alias;
1134 }
David Howells770bfad2006-08-22 20:06:07 -04001135
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001136 __d_instantiate(entry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 return NULL;
1138}
David Howells770bfad2006-08-22 20:06:07 -04001139
1140struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode)
1141{
1142 struct dentry *result;
1143
1144 BUG_ON(!list_empty(&entry->d_alias));
1145
1146 spin_lock(&dcache_lock);
1147 result = __d_instantiate_unique(entry, inode);
1148 spin_unlock(&dcache_lock);
1149
1150 if (!result) {
1151 security_d_instantiate(entry, inode);
1152 return NULL;
1153 }
1154
1155 BUG_ON(!d_unhashed(result));
1156 iput(inode);
1157 return result;
1158}
1159
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160EXPORT_SYMBOL(d_instantiate_unique);
1161
1162/**
1163 * d_alloc_root - allocate root dentry
1164 * @root_inode: inode to allocate the root for
1165 *
1166 * Allocate a root ("/") dentry for the inode given. The inode is
1167 * instantiated and returned. %NULL is returned if there is insufficient
1168 * memory or the inode passed is %NULL.
1169 */
1170
1171struct dentry * d_alloc_root(struct inode * root_inode)
1172{
1173 struct dentry *res = NULL;
1174
1175 if (root_inode) {
1176 static const struct qstr name = { .name = "/", .len = 1 };
1177
1178 res = d_alloc(NULL, &name);
1179 if (res) {
1180 res->d_sb = root_inode->i_sb;
1181 res->d_parent = res;
1182 d_instantiate(res, root_inode);
1183 }
1184 }
1185 return res;
1186}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001187EXPORT_SYMBOL(d_alloc_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
1189static inline struct hlist_head *d_hash(struct dentry *parent,
1190 unsigned long hash)
1191{
1192 hash += ((unsigned long) parent ^ GOLDEN_RATIO_PRIME) / L1_CACHE_BYTES;
1193 hash = hash ^ ((hash ^ GOLDEN_RATIO_PRIME) >> D_HASHBITS);
1194 return dentry_hashtable + (hash & D_HASHMASK);
1195}
1196
1197/**
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001198 * d_obtain_alias - find or allocate a dentry for a given inode
1199 * @inode: inode to allocate the dentry for
1200 *
1201 * Obtain a dentry for an inode resulting from NFS filehandle conversion or
1202 * similar open by handle operations. The returned dentry may be anonymous,
1203 * or may have a full name (if the inode was already in the cache).
1204 *
1205 * When called on a directory inode, we must ensure that the inode only ever
1206 * has one dentry. If a dentry is found, that is returned instead of
1207 * allocating a new one.
1208 *
1209 * On successful return, the reference to the inode has been transferred
Christoph Hellwig44003722008-08-11 15:49:04 +02001210 * to the dentry. In case of an error the reference on the inode is released.
1211 * To make it easier to use in export operations a %NULL or IS_ERR inode may
1212 * be passed in and will be the error will be propagate to the return value,
1213 * with a %NULL @inode replaced by ERR_PTR(-ESTALE).
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001214 */
1215struct dentry *d_obtain_alias(struct inode *inode)
1216{
Christoph Hellwig9308a612008-08-11 15:49:12 +02001217 static const struct qstr anonstring = { .name = "" };
1218 struct dentry *tmp;
1219 struct dentry *res;
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001220
1221 if (!inode)
Christoph Hellwig44003722008-08-11 15:49:04 +02001222 return ERR_PTR(-ESTALE);
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001223 if (IS_ERR(inode))
1224 return ERR_CAST(inode);
1225
Christoph Hellwig9308a612008-08-11 15:49:12 +02001226 res = d_find_alias(inode);
1227 if (res)
1228 goto out_iput;
1229
1230 tmp = d_alloc(NULL, &anonstring);
1231 if (!tmp) {
1232 res = ERR_PTR(-ENOMEM);
1233 goto out_iput;
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001234 }
Christoph Hellwig9308a612008-08-11 15:49:12 +02001235 tmp->d_parent = tmp; /* make sure dput doesn't croak */
1236
1237 spin_lock(&dcache_lock);
1238 res = __d_find_alias(inode, 0);
1239 if (res) {
1240 spin_unlock(&dcache_lock);
1241 dput(tmp);
1242 goto out_iput;
1243 }
1244
1245 /* attach a disconnected dentry */
1246 spin_lock(&tmp->d_lock);
1247 tmp->d_sb = inode->i_sb;
1248 tmp->d_inode = inode;
1249 tmp->d_flags |= DCACHE_DISCONNECTED;
1250 tmp->d_flags &= ~DCACHE_UNHASHED;
1251 list_add(&tmp->d_alias, &inode->i_dentry);
Nick Piggin789680d2011-01-07 17:49:30 +11001252 spin_lock(&dcache_hash_lock);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001253 hlist_add_head(&tmp->d_hash, &inode->i_sb->s_anon);
Nick Piggin789680d2011-01-07 17:49:30 +11001254 spin_unlock(&dcache_hash_lock);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001255 spin_unlock(&tmp->d_lock);
1256
1257 spin_unlock(&dcache_lock);
1258 return tmp;
1259
1260 out_iput:
1261 iput(inode);
1262 return res;
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001263}
Benny Halevyadc48722009-02-27 14:02:59 -08001264EXPORT_SYMBOL(d_obtain_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
1266/**
1267 * d_splice_alias - splice a disconnected dentry into the tree if one exists
1268 * @inode: the inode which may have a disconnected dentry
1269 * @dentry: a negative dentry which we want to point to the inode.
1270 *
1271 * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
1272 * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
1273 * and return it, else simply d_add the inode to the dentry and return NULL.
1274 *
1275 * This is needed in the lookup routine of any filesystem that is exportable
1276 * (via knfsd) so that we can build dcache paths to directories effectively.
1277 *
1278 * If a dentry was found and moved, then it is returned. Otherwise NULL
1279 * is returned. This matches the expected return value of ->lookup.
1280 *
1281 */
1282struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
1283{
1284 struct dentry *new = NULL;
1285
NeilBrown21c0d8f2006-10-04 02:16:16 -07001286 if (inode && S_ISDIR(inode->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 spin_lock(&dcache_lock);
1288 new = __d_find_alias(inode, 1);
1289 if (new) {
1290 BUG_ON(!(new->d_flags & DCACHE_DISCONNECTED));
1291 spin_unlock(&dcache_lock);
1292 security_d_instantiate(new, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 d_move(new, dentry);
1294 iput(inode);
1295 } else {
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001296 /* already taking dcache_lock, so d_add() by hand */
1297 __d_instantiate(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 spin_unlock(&dcache_lock);
1299 security_d_instantiate(dentry, inode);
1300 d_rehash(dentry);
1301 }
1302 } else
1303 d_add(dentry, inode);
1304 return new;
1305}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001306EXPORT_SYMBOL(d_splice_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
Barry Naujok94035402008-05-21 16:50:46 +10001308/**
1309 * d_add_ci - lookup or allocate new dentry with case-exact name
1310 * @inode: the inode case-insensitive lookup has found
1311 * @dentry: the negative dentry that was passed to the parent's lookup func
1312 * @name: the case-exact name to be associated with the returned dentry
1313 *
1314 * This is to avoid filling the dcache with case-insensitive names to the
1315 * same inode, only the actual correct case is stored in the dcache for
1316 * case-insensitive filesystems.
1317 *
1318 * For a case-insensitive lookup match and if the the case-exact dentry
1319 * already exists in in the dcache, use it and return it.
1320 *
1321 * If no entry exists with the exact case name, allocate new dentry with
1322 * the exact case, and return the spliced entry.
1323 */
Christoph Hellwige45b5902008-08-07 23:49:07 +02001324struct dentry *d_add_ci(struct dentry *dentry, struct inode *inode,
Barry Naujok94035402008-05-21 16:50:46 +10001325 struct qstr *name)
1326{
1327 int error;
1328 struct dentry *found;
1329 struct dentry *new;
1330
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001331 /*
1332 * First check if a dentry matching the name already exists,
1333 * if not go ahead and create it now.
1334 */
Barry Naujok94035402008-05-21 16:50:46 +10001335 found = d_hash_and_lookup(dentry->d_parent, name);
Barry Naujok94035402008-05-21 16:50:46 +10001336 if (!found) {
1337 new = d_alloc(dentry->d_parent, name);
1338 if (!new) {
1339 error = -ENOMEM;
1340 goto err_out;
1341 }
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001342
Barry Naujok94035402008-05-21 16:50:46 +10001343 found = d_splice_alias(inode, new);
1344 if (found) {
1345 dput(new);
1346 return found;
1347 }
1348 return new;
1349 }
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001350
1351 /*
1352 * If a matching dentry exists, and it's not negative use it.
1353 *
1354 * Decrement the reference count to balance the iget() done
1355 * earlier on.
1356 */
Barry Naujok94035402008-05-21 16:50:46 +10001357 if (found->d_inode) {
1358 if (unlikely(found->d_inode != inode)) {
1359 /* This can't happen because bad inodes are unhashed. */
1360 BUG_ON(!is_bad_inode(inode));
1361 BUG_ON(!is_bad_inode(found->d_inode));
1362 }
Barry Naujok94035402008-05-21 16:50:46 +10001363 iput(inode);
1364 return found;
1365 }
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001366
Barry Naujok94035402008-05-21 16:50:46 +10001367 /*
1368 * Negative dentry: instantiate it unless the inode is a directory and
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001369 * already has a dentry.
Barry Naujok94035402008-05-21 16:50:46 +10001370 */
Barry Naujok94035402008-05-21 16:50:46 +10001371 spin_lock(&dcache_lock);
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001372 if (!S_ISDIR(inode->i_mode) || list_empty(&inode->i_dentry)) {
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001373 __d_instantiate(found, inode);
Barry Naujok94035402008-05-21 16:50:46 +10001374 spin_unlock(&dcache_lock);
1375 security_d_instantiate(found, inode);
1376 return found;
1377 }
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001378
Barry Naujok94035402008-05-21 16:50:46 +10001379 /*
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001380 * In case a directory already has a (disconnected) entry grab a
1381 * reference to it, move it in place and use it.
Barry Naujok94035402008-05-21 16:50:46 +10001382 */
1383 new = list_entry(inode->i_dentry.next, struct dentry, d_alias);
1384 dget_locked(new);
1385 spin_unlock(&dcache_lock);
Barry Naujok94035402008-05-21 16:50:46 +10001386 security_d_instantiate(found, inode);
Barry Naujok94035402008-05-21 16:50:46 +10001387 d_move(new, found);
Barry Naujok94035402008-05-21 16:50:46 +10001388 iput(inode);
Barry Naujok94035402008-05-21 16:50:46 +10001389 dput(found);
Barry Naujok94035402008-05-21 16:50:46 +10001390 return new;
1391
1392err_out:
1393 iput(inode);
1394 return ERR_PTR(error);
1395}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001396EXPORT_SYMBOL(d_add_ci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
1398/**
1399 * d_lookup - search for a dentry
1400 * @parent: parent dentry
1401 * @name: qstr of name we wish to find
Nick Pigginb04f7842010-08-18 04:37:34 +10001402 * Returns: dentry, or NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 *
Nick Pigginb04f7842010-08-18 04:37:34 +10001404 * d_lookup searches the children of the parent dentry for the name in
1405 * question. If the dentry is found its reference count is incremented and the
1406 * dentry is returned. The caller must use dput to free the entry when it has
1407 * finished using it. %NULL is returned if the dentry does not exist.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409struct dentry * d_lookup(struct dentry * parent, struct qstr * name)
1410{
1411 struct dentry * dentry = NULL;
1412 unsigned long seq;
1413
1414 do {
1415 seq = read_seqbegin(&rename_lock);
1416 dentry = __d_lookup(parent, name);
1417 if (dentry)
1418 break;
1419 } while (read_seqretry(&rename_lock, seq));
1420 return dentry;
1421}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001422EXPORT_SYMBOL(d_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
Nick Pigginb04f7842010-08-18 04:37:34 +10001424/*
1425 * __d_lookup - search for a dentry (racy)
1426 * @parent: parent dentry
1427 * @name: qstr of name we wish to find
1428 * Returns: dentry, or NULL
1429 *
1430 * __d_lookup is like d_lookup, however it may (rarely) return a
1431 * false-negative result due to unrelated rename activity.
1432 *
1433 * __d_lookup is slightly faster by avoiding rename_lock read seqlock,
1434 * however it must be used carefully, eg. with a following d_lookup in
1435 * the case of failure.
1436 *
1437 * __d_lookup callers must be commented.
1438 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439struct dentry * __d_lookup(struct dentry * parent, struct qstr * name)
1440{
1441 unsigned int len = name->len;
1442 unsigned int hash = name->hash;
1443 const unsigned char *str = name->name;
1444 struct hlist_head *head = d_hash(parent,hash);
1445 struct dentry *found = NULL;
1446 struct hlist_node *node;
Paul E. McKenney665a7582005-11-07 00:59:17 -08001447 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Nick Pigginb04f7842010-08-18 04:37:34 +10001449 /*
1450 * The hash list is protected using RCU.
1451 *
1452 * Take d_lock when comparing a candidate dentry, to avoid races
1453 * with d_move().
1454 *
1455 * It is possible that concurrent renames can mess up our list
1456 * walk here and result in missing our dentry, resulting in the
1457 * false-negative result. d_lookup() protects against concurrent
1458 * renames using rename_lock seqlock.
1459 *
1460 * See Documentation/vfs/dcache-locking.txt for more details.
1461 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 rcu_read_lock();
1463
Paul E. McKenney665a7582005-11-07 00:59:17 -08001464 hlist_for_each_entry_rcu(dentry, node, head, d_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 struct qstr *qstr;
1466
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 if (dentry->d_name.hash != hash)
1468 continue;
1469 if (dentry->d_parent != parent)
1470 continue;
1471
1472 spin_lock(&dentry->d_lock);
1473
1474 /*
1475 * Recheck the dentry after taking the lock - d_move may have
Nick Pigginb04f7842010-08-18 04:37:34 +10001476 * changed things. Don't bother checking the hash because
1477 * we're about to compare the whole name anyway.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 */
1479 if (dentry->d_parent != parent)
1480 goto next;
1481
Linus Torvaldsd0185c02008-09-29 07:42:57 -07001482 /* non-existing due to RCU? */
1483 if (d_unhashed(dentry))
1484 goto next;
1485
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 /*
1487 * It is safe to compare names since d_move() cannot
1488 * change the qstr (protected by d_lock).
1489 */
1490 qstr = &dentry->d_name;
1491 if (parent->d_op && parent->d_op->d_compare) {
Nick Piggin621e1552011-01-07 17:49:27 +11001492 if (parent->d_op->d_compare(parent, parent->d_inode,
1493 dentry, dentry->d_inode,
1494 qstr->len, qstr->name, name))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 goto next;
1496 } else {
1497 if (qstr->len != len)
1498 goto next;
1499 if (memcmp(qstr->name, str, len))
1500 goto next;
1501 }
1502
Linus Torvaldsd0185c02008-09-29 07:42:57 -07001503 atomic_inc(&dentry->d_count);
1504 found = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 spin_unlock(&dentry->d_lock);
1506 break;
1507next:
1508 spin_unlock(&dentry->d_lock);
1509 }
1510 rcu_read_unlock();
1511
1512 return found;
1513}
1514
1515/**
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08001516 * d_hash_and_lookup - hash the qstr then search for a dentry
1517 * @dir: Directory to search in
1518 * @name: qstr of name we wish to find
1519 *
1520 * On hash failure or on lookup failure NULL is returned.
1521 */
1522struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name)
1523{
1524 struct dentry *dentry = NULL;
1525
1526 /*
1527 * Check for a fs-specific hash function. Note that we must
1528 * calculate the standard hash first, as the d_op->d_hash()
1529 * routine may choose to leave the hash value unchanged.
1530 */
1531 name->hash = full_name_hash(name->name, name->len);
1532 if (dir->d_op && dir->d_op->d_hash) {
Nick Pigginb1e6a012011-01-07 17:49:28 +11001533 if (dir->d_op->d_hash(dir, dir->d_inode, name) < 0)
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08001534 goto out;
1535 }
1536 dentry = d_lookup(dir, name);
1537out:
1538 return dentry;
1539}
1540
1541/**
Nick Piggin786a5e12011-01-07 17:49:16 +11001542 * d_validate - verify dentry provided from insecure source (deprecated)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 * @dentry: The dentry alleged to be valid child of @dparent
1544 * @dparent: The parent dentry (known to be valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 *
1546 * An insecure source has sent us a dentry, here we verify it and dget() it.
1547 * This is used by ncpfs in its readdir implementation.
1548 * Zero is returned in the dentry is invalid.
Nick Piggin786a5e12011-01-07 17:49:16 +11001549 *
1550 * This function is slow for big directories, and deprecated, do not use it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 */
Nick Piggind3a23e12011-01-05 20:01:21 +11001552int d_validate(struct dentry *dentry, struct dentry *dparent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553{
Nick Piggin786a5e12011-01-07 17:49:16 +11001554 struct dentry *child;
Nick Piggind3a23e12011-01-05 20:01:21 +11001555
1556 spin_lock(&dcache_lock);
Nick Piggin786a5e12011-01-07 17:49:16 +11001557 list_for_each_entry(child, &dparent->d_subdirs, d_u.d_child) {
1558 if (dentry == child) {
Nick Piggind3a23e12011-01-05 20:01:21 +11001559 __dget_locked(dentry);
1560 spin_unlock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 return 1;
1562 }
1563 }
Nick Piggind3a23e12011-01-05 20:01:21 +11001564 spin_unlock(&dcache_lock);
Nick Piggin786a5e12011-01-07 17:49:16 +11001565
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 return 0;
1567}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001568EXPORT_SYMBOL(d_validate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
1570/*
1571 * When a file is deleted, we have two options:
1572 * - turn this dentry into a negative dentry
1573 * - unhash this dentry and free it.
1574 *
1575 * Usually, we want to just turn this into
1576 * a negative dentry, but if anybody else is
1577 * currently using the dentry or the inode
1578 * we can't do that and we fall back on removing
1579 * it from the hash queues and waiting for
1580 * it to be deleted later when it has no users
1581 */
1582
1583/**
1584 * d_delete - delete a dentry
1585 * @dentry: The dentry to delete
1586 *
1587 * Turn the dentry into a negative dentry if possible, otherwise
1588 * remove it from the hash queues so it can be deleted later
1589 */
1590
1591void d_delete(struct dentry * dentry)
1592{
John McCutchan7a91bf72005-08-08 13:52:16 -04001593 int isdir = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 /*
1595 * Are we the only user?
1596 */
1597 spin_lock(&dcache_lock);
1598 spin_lock(&dentry->d_lock);
John McCutchan7a91bf72005-08-08 13:52:16 -04001599 isdir = S_ISDIR(dentry->d_inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 if (atomic_read(&dentry->d_count) == 1) {
Al Viro13e3c5e2010-05-21 16:11:04 -04001601 dentry->d_flags &= ~DCACHE_CANT_MOUNT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 dentry_iput(dentry);
John McCutchan7a91bf72005-08-08 13:52:16 -04001603 fsnotify_nameremove(dentry, isdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 return;
1605 }
1606
1607 if (!d_unhashed(dentry))
1608 __d_drop(dentry);
1609
1610 spin_unlock(&dentry->d_lock);
1611 spin_unlock(&dcache_lock);
John McCutchan7a91bf72005-08-08 13:52:16 -04001612
1613 fsnotify_nameremove(dentry, isdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001615EXPORT_SYMBOL(d_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
1617static void __d_rehash(struct dentry * entry, struct hlist_head *list)
1618{
1619
1620 entry->d_flags &= ~DCACHE_UNHASHED;
1621 hlist_add_head_rcu(&entry->d_hash, list);
1622}
1623
David Howells770bfad2006-08-22 20:06:07 -04001624static void _d_rehash(struct dentry * entry)
1625{
1626 __d_rehash(entry, d_hash(entry->d_parent, entry->d_name.hash));
1627}
1628
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629/**
1630 * d_rehash - add an entry back to the hash
1631 * @entry: dentry to add to the hash
1632 *
1633 * Adds a dentry to the hash according to its name.
1634 */
1635
1636void d_rehash(struct dentry * entry)
1637{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 spin_lock(&dcache_lock);
1639 spin_lock(&entry->d_lock);
Nick Piggin789680d2011-01-07 17:49:30 +11001640 spin_lock(&dcache_hash_lock);
David Howells770bfad2006-08-22 20:06:07 -04001641 _d_rehash(entry);
Nick Piggin789680d2011-01-07 17:49:30 +11001642 spin_unlock(&dcache_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 spin_unlock(&entry->d_lock);
1644 spin_unlock(&dcache_lock);
1645}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001646EXPORT_SYMBOL(d_rehash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
Nick Pigginfb2d5b82011-01-07 17:49:26 +11001648/**
1649 * dentry_update_name_case - update case insensitive dentry with a new name
1650 * @dentry: dentry to be updated
1651 * @name: new name
1652 *
1653 * Update a case insensitive dentry with new case of name.
1654 *
1655 * dentry must have been returned by d_lookup with name @name. Old and new
1656 * name lengths must match (ie. no d_compare which allows mismatched name
1657 * lengths).
1658 *
1659 * Parent inode i_mutex must be held over d_lookup and into this call (to
1660 * keep renames and concurrent inserts, and readdir(2) away).
1661 */
1662void dentry_update_name_case(struct dentry *dentry, struct qstr *name)
1663{
1664 BUG_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
1665 BUG_ON(dentry->d_name.len != name->len); /* d_lookup gives this */
1666
1667 spin_lock(&dcache_lock);
1668 spin_lock(&dentry->d_lock);
1669 memcpy((unsigned char *)dentry->d_name.name, name->name, name->len);
1670 spin_unlock(&dentry->d_lock);
1671 spin_unlock(&dcache_lock);
1672}
1673EXPORT_SYMBOL(dentry_update_name_case);
1674
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675/*
1676 * When switching names, the actual string doesn't strictly have to
1677 * be preserved in the target - because we're dropping the target
1678 * anyway. As such, we can just do a simple memcpy() to copy over
1679 * the new name before we switch.
1680 *
1681 * Note that we have to be a lot more careful about getting the hash
1682 * switched - we have to switch the hash value properly even if it
1683 * then no longer matches the actual (corrupted) string of the target.
1684 * The hash value has to match the hash queue that the dentry is on..
1685 */
1686static void switch_names(struct dentry *dentry, struct dentry *target)
1687{
1688 if (dname_external(target)) {
1689 if (dname_external(dentry)) {
1690 /*
1691 * Both external: swap the pointers
1692 */
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08001693 swap(target->d_name.name, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 } else {
1695 /*
1696 * dentry:internal, target:external. Steal target's
1697 * storage and make target internal.
1698 */
J. Bruce Fields321bcf92007-10-21 16:41:38 -07001699 memcpy(target->d_iname, dentry->d_name.name,
1700 dentry->d_name.len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 dentry->d_name.name = target->d_name.name;
1702 target->d_name.name = target->d_iname;
1703 }
1704 } else {
1705 if (dname_external(dentry)) {
1706 /*
1707 * dentry:external, target:internal. Give dentry's
1708 * storage to target and make dentry internal
1709 */
1710 memcpy(dentry->d_iname, target->d_name.name,
1711 target->d_name.len + 1);
1712 target->d_name.name = dentry->d_name.name;
1713 dentry->d_name.name = dentry->d_iname;
1714 } else {
1715 /*
1716 * Both are internal. Just copy target to dentry
1717 */
1718 memcpy(dentry->d_iname, target->d_name.name,
1719 target->d_name.len + 1);
Al Virodc711ca2008-11-03 15:03:50 -05001720 dentry->d_name.len = target->d_name.len;
1721 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 }
1723 }
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08001724 swap(dentry->d_name.len, target->d_name.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725}
1726
1727/*
1728 * We cannibalize "target" when moving dentry on top of it,
1729 * because it's going to be thrown away anyway. We could be more
1730 * polite about it, though.
1731 *
1732 * This forceful removal will result in ugly /proc output if
1733 * somebody holds a file open that got deleted due to a rename.
1734 * We could be nicer about the deleted file, and let it show
J. Bruce Fieldsbc154b12007-10-16 23:29:42 -07001735 * up under the name it had before it was deleted rather than
1736 * under the original name of the file that was moved on top of it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 */
1738
Trond Myklebust9eaef272006-10-21 10:24:20 -07001739/*
1740 * d_move_locked - move a dentry
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 * @dentry: entry to move
1742 * @target: new dentry
1743 *
1744 * Update the dcache to reflect the move of a file name. Negative
1745 * dcache entries should not be moved in this way.
1746 */
Trond Myklebust9eaef272006-10-21 10:24:20 -07001747static void d_move_locked(struct dentry * dentry, struct dentry * target)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 if (!dentry->d_inode)
1750 printk(KERN_WARNING "VFS: moving negative dcache entry\n");
1751
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 write_seqlock(&rename_lock);
1753 /*
1754 * XXXX: do we really need to take target->d_lock?
1755 */
1756 if (target < dentry) {
1757 spin_lock(&target->d_lock);
Ingo Molnara90b9c02006-07-03 00:25:04 -07001758 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 } else {
1760 spin_lock(&dentry->d_lock);
Ingo Molnara90b9c02006-07-03 00:25:04 -07001761 spin_lock_nested(&target->d_lock, DENTRY_D_LOCK_NESTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 }
1763
1764 /* Move the dentry to the target hash queue, if on different bucket */
Nick Piggin789680d2011-01-07 17:49:30 +11001765 spin_lock(&dcache_hash_lock);
1766 if (!d_unhashed(dentry))
1767 hlist_del_rcu(&dentry->d_hash);
1768 __d_rehash(dentry, d_hash(target->d_parent, target->d_name.hash));
1769 spin_unlock(&dcache_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
1771 /* Unhash the target: dput() will then get rid of it */
1772 __d_drop(target);
1773
Eric Dumazet5160ee62006-01-08 01:03:32 -08001774 list_del(&dentry->d_u.d_child);
1775 list_del(&target->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
1777 /* Switch the names.. */
1778 switch_names(dentry, target);
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08001779 swap(dentry->d_name.hash, target->d_name.hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
1781 /* ... and switch the parents */
1782 if (IS_ROOT(dentry)) {
1783 dentry->d_parent = target->d_parent;
1784 target->d_parent = target;
Eric Dumazet5160ee62006-01-08 01:03:32 -08001785 INIT_LIST_HEAD(&target->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 } else {
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08001787 swap(dentry->d_parent, target->d_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
1789 /* And add them back to the (new) parent lists */
Eric Dumazet5160ee62006-01-08 01:03:32 -08001790 list_add(&target->d_u.d_child, &target->d_parent->d_subdirs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 }
1792
Eric Dumazet5160ee62006-01-08 01:03:32 -08001793 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 spin_unlock(&target->d_lock);
Nick Pigginc32ccd82006-03-25 03:07:09 -08001795 fsnotify_d_move(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 spin_unlock(&dentry->d_lock);
1797 write_sequnlock(&rename_lock);
Trond Myklebust9eaef272006-10-21 10:24:20 -07001798}
1799
1800/**
1801 * d_move - move a dentry
1802 * @dentry: entry to move
1803 * @target: new dentry
1804 *
1805 * Update the dcache to reflect the move of a file name. Negative
1806 * dcache entries should not be moved in this way.
1807 */
1808
1809void d_move(struct dentry * dentry, struct dentry * target)
1810{
1811 spin_lock(&dcache_lock);
1812 d_move_locked(dentry, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 spin_unlock(&dcache_lock);
1814}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001815EXPORT_SYMBOL(d_move);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001817/**
1818 * d_ancestor - search for an ancestor
1819 * @p1: ancestor dentry
1820 * @p2: child dentry
1821 *
1822 * Returns the ancestor dentry of p2 which is a child of p1, if p1 is
1823 * an ancestor of p2, else NULL.
Trond Myklebust9eaef272006-10-21 10:24:20 -07001824 */
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001825struct dentry *d_ancestor(struct dentry *p1, struct dentry *p2)
Trond Myklebust9eaef272006-10-21 10:24:20 -07001826{
1827 struct dentry *p;
1828
OGAWA Hirofumi871c0062008-10-16 07:50:27 +09001829 for (p = p2; !IS_ROOT(p); p = p->d_parent) {
Trond Myklebust9eaef272006-10-21 10:24:20 -07001830 if (p->d_parent == p1)
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001831 return p;
Trond Myklebust9eaef272006-10-21 10:24:20 -07001832 }
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001833 return NULL;
Trond Myklebust9eaef272006-10-21 10:24:20 -07001834}
1835
1836/*
1837 * This helper attempts to cope with remotely renamed directories
1838 *
1839 * It assumes that the caller is already holding
1840 * dentry->d_parent->d_inode->i_mutex and the dcache_lock
1841 *
1842 * Note: If ever the locking in lock_rename() changes, then please
1843 * remember to update this too...
Trond Myklebust9eaef272006-10-21 10:24:20 -07001844 */
1845static struct dentry *__d_unalias(struct dentry *dentry, struct dentry *alias)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +02001846 __releases(dcache_lock)
Trond Myklebust9eaef272006-10-21 10:24:20 -07001847{
1848 struct mutex *m1 = NULL, *m2 = NULL;
1849 struct dentry *ret;
1850
1851 /* If alias and dentry share a parent, then no extra locks required */
1852 if (alias->d_parent == dentry->d_parent)
1853 goto out_unalias;
1854
1855 /* Check for loops */
1856 ret = ERR_PTR(-ELOOP);
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001857 if (d_ancestor(alias, dentry))
Trond Myklebust9eaef272006-10-21 10:24:20 -07001858 goto out_err;
1859
1860 /* See lock_rename() */
1861 ret = ERR_PTR(-EBUSY);
1862 if (!mutex_trylock(&dentry->d_sb->s_vfs_rename_mutex))
1863 goto out_err;
1864 m1 = &dentry->d_sb->s_vfs_rename_mutex;
1865 if (!mutex_trylock(&alias->d_parent->d_inode->i_mutex))
1866 goto out_err;
1867 m2 = &alias->d_parent->d_inode->i_mutex;
1868out_unalias:
1869 d_move_locked(alias, dentry);
1870 ret = alias;
1871out_err:
1872 spin_unlock(&dcache_lock);
1873 if (m2)
1874 mutex_unlock(m2);
1875 if (m1)
1876 mutex_unlock(m1);
1877 return ret;
1878}
1879
1880/*
David Howells770bfad2006-08-22 20:06:07 -04001881 * Prepare an anonymous dentry for life in the superblock's dentry tree as a
1882 * named dentry in place of the dentry to be replaced.
1883 */
1884static void __d_materialise_dentry(struct dentry *dentry, struct dentry *anon)
1885{
1886 struct dentry *dparent, *aparent;
1887
1888 switch_names(dentry, anon);
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08001889 swap(dentry->d_name.hash, anon->d_name.hash);
David Howells770bfad2006-08-22 20:06:07 -04001890
1891 dparent = dentry->d_parent;
1892 aparent = anon->d_parent;
1893
1894 dentry->d_parent = (aparent == anon) ? dentry : aparent;
1895 list_del(&dentry->d_u.d_child);
1896 if (!IS_ROOT(dentry))
1897 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
1898 else
1899 INIT_LIST_HEAD(&dentry->d_u.d_child);
1900
1901 anon->d_parent = (dparent == dentry) ? anon : dparent;
1902 list_del(&anon->d_u.d_child);
1903 if (!IS_ROOT(anon))
1904 list_add(&anon->d_u.d_child, &anon->d_parent->d_subdirs);
1905 else
1906 INIT_LIST_HEAD(&anon->d_u.d_child);
1907
1908 anon->d_flags &= ~DCACHE_DISCONNECTED;
1909}
1910
1911/**
1912 * d_materialise_unique - introduce an inode into the tree
1913 * @dentry: candidate dentry
1914 * @inode: inode to bind to the dentry, to which aliases may be attached
1915 *
1916 * Introduces an dentry into the tree, substituting an extant disconnected
1917 * root directory alias in its place if there is one
1918 */
1919struct dentry *d_materialise_unique(struct dentry *dentry, struct inode *inode)
1920{
Trond Myklebust9eaef272006-10-21 10:24:20 -07001921 struct dentry *actual;
David Howells770bfad2006-08-22 20:06:07 -04001922
1923 BUG_ON(!d_unhashed(dentry));
1924
1925 spin_lock(&dcache_lock);
1926
1927 if (!inode) {
1928 actual = dentry;
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001929 __d_instantiate(dentry, NULL);
David Howells770bfad2006-08-22 20:06:07 -04001930 goto found_lock;
1931 }
1932
Trond Myklebust9eaef272006-10-21 10:24:20 -07001933 if (S_ISDIR(inode->i_mode)) {
1934 struct dentry *alias;
David Howells770bfad2006-08-22 20:06:07 -04001935
Trond Myklebust9eaef272006-10-21 10:24:20 -07001936 /* Does an aliased dentry already exist? */
1937 alias = __d_find_alias(inode, 0);
1938 if (alias) {
1939 actual = alias;
1940 /* Is this an anonymous mountpoint that we could splice
1941 * into our tree? */
1942 if (IS_ROOT(alias)) {
1943 spin_lock(&alias->d_lock);
1944 __d_materialise_dentry(dentry, alias);
1945 __d_drop(alias);
1946 goto found;
1947 }
1948 /* Nope, but we must(!) avoid directory aliasing */
1949 actual = __d_unalias(dentry, alias);
1950 if (IS_ERR(actual))
1951 dput(alias);
1952 goto out_nolock;
1953 }
David Howells770bfad2006-08-22 20:06:07 -04001954 }
1955
1956 /* Add a unique reference */
1957 actual = __d_instantiate_unique(dentry, inode);
1958 if (!actual)
1959 actual = dentry;
1960 else if (unlikely(!d_unhashed(actual)))
1961 goto shouldnt_be_hashed;
1962
1963found_lock:
1964 spin_lock(&actual->d_lock);
1965found:
Nick Piggin789680d2011-01-07 17:49:30 +11001966 spin_lock(&dcache_hash_lock);
David Howells770bfad2006-08-22 20:06:07 -04001967 _d_rehash(actual);
Nick Piggin789680d2011-01-07 17:49:30 +11001968 spin_unlock(&dcache_hash_lock);
David Howells770bfad2006-08-22 20:06:07 -04001969 spin_unlock(&actual->d_lock);
1970 spin_unlock(&dcache_lock);
Trond Myklebust9eaef272006-10-21 10:24:20 -07001971out_nolock:
David Howells770bfad2006-08-22 20:06:07 -04001972 if (actual == dentry) {
1973 security_d_instantiate(dentry, inode);
1974 return NULL;
1975 }
1976
1977 iput(inode);
1978 return actual;
1979
David Howells770bfad2006-08-22 20:06:07 -04001980shouldnt_be_hashed:
1981 spin_unlock(&dcache_lock);
1982 BUG();
David Howells770bfad2006-08-22 20:06:07 -04001983}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001984EXPORT_SYMBOL_GPL(d_materialise_unique);
David Howells770bfad2006-08-22 20:06:07 -04001985
Miklos Szeredicdd16d02008-06-23 18:11:53 +02001986static int prepend(char **buffer, int *buflen, const char *str, int namelen)
Ram Pai6092d042008-03-27 13:06:20 +01001987{
1988 *buflen -= namelen;
1989 if (*buflen < 0)
1990 return -ENAMETOOLONG;
1991 *buffer -= namelen;
1992 memcpy(*buffer, str, namelen);
1993 return 0;
1994}
1995
Miklos Szeredicdd16d02008-06-23 18:11:53 +02001996static int prepend_name(char **buffer, int *buflen, struct qstr *name)
1997{
1998 return prepend(buffer, buflen, name->name, name->len);
1999}
2000
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001/**
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002002 * Prepend path string to a buffer
2003 *
2004 * @path: the dentry/vfsmount to report
2005 * @root: root vfsmnt/dentry (may be modified by this function)
2006 * @buffer: pointer to the end of the buffer
2007 * @buflen: pointer to buffer length
2008 *
2009 * Caller holds the dcache_lock.
2010 *
2011 * If path is not reachable from the supplied root, then the value of
2012 * root is changed (without modifying refcounts).
2013 */
2014static int prepend_path(const struct path *path, struct path *root,
2015 char **buffer, int *buflen)
2016{
2017 struct dentry *dentry = path->dentry;
2018 struct vfsmount *vfsmnt = path->mnt;
2019 bool slash = false;
2020 int error = 0;
2021
Nick Piggin99b7db72010-08-18 04:37:39 +10002022 br_read_lock(vfsmount_lock);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002023 while (dentry != root->dentry || vfsmnt != root->mnt) {
2024 struct dentry * parent;
2025
2026 if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
2027 /* Global root? */
2028 if (vfsmnt->mnt_parent == vfsmnt) {
2029 goto global_root;
2030 }
2031 dentry = vfsmnt->mnt_mountpoint;
2032 vfsmnt = vfsmnt->mnt_parent;
2033 continue;
2034 }
2035 parent = dentry->d_parent;
2036 prefetch(parent);
2037 error = prepend_name(buffer, buflen, &dentry->d_name);
2038 if (!error)
2039 error = prepend(buffer, buflen, "/", 1);
2040 if (error)
2041 break;
2042
2043 slash = true;
2044 dentry = parent;
2045 }
2046
2047out:
2048 if (!error && !slash)
2049 error = prepend(buffer, buflen, "/", 1);
2050
Nick Piggin99b7db72010-08-18 04:37:39 +10002051 br_read_unlock(vfsmount_lock);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002052 return error;
2053
2054global_root:
2055 /*
2056 * Filesystems needing to implement special "root names"
2057 * should do so with ->d_dname()
2058 */
2059 if (IS_ROOT(dentry) &&
2060 (dentry->d_name.len != 1 || dentry->d_name.name[0] != '/')) {
2061 WARN(1, "Root dentry has weird name <%.*s>\n",
2062 (int) dentry->d_name.len, dentry->d_name.name);
2063 }
2064 root->mnt = vfsmnt;
2065 root->dentry = dentry;
2066 goto out;
2067}
2068
2069/**
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +02002070 * __d_path - return the path of a dentry
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002071 * @path: the dentry/vfsmount to report
2072 * @root: root vfsmnt/dentry (may be modified by this function)
Randy Dunlapcd956a12010-08-14 13:05:31 -07002073 * @buf: buffer to return value in
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 * @buflen: buffer length
2075 *
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002076 * Convert a dentry into an ASCII path name.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 *
Arjan van de Ven52afeef2008-12-01 14:35:00 -08002078 * Returns a pointer into the buffer or an error code if the
2079 * path was too long.
Linus Torvalds552ce542007-02-13 12:08:18 -08002080 *
Christoph Hellwigbe148242010-10-10 05:36:21 -04002081 * "buflen" should be positive.
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002082 *
2083 * If path is not reachable from the supplied root, then the value of
2084 * root is changed (without modifying refcounts).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 */
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002086char *__d_path(const struct path *path, struct path *root,
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002087 char *buf, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088{
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002089 char *res = buf + buflen;
2090 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002092 prepend(&res, &buflen, "\0", 1);
Christoph Hellwigbe148242010-10-10 05:36:21 -04002093 spin_lock(&dcache_lock);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002094 error = prepend_path(path, root, &res, &buflen);
Christoph Hellwigbe148242010-10-10 05:36:21 -04002095 spin_unlock(&dcache_lock);
2096
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002097 if (error)
2098 return ERR_PTR(error);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002099 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100}
2101
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002102/*
2103 * same as __d_path but appends "(deleted)" for unlinked files.
2104 */
2105static int path_with_deleted(const struct path *path, struct path *root,
2106 char **buf, int *buflen)
2107{
2108 prepend(buf, buflen, "\0", 1);
2109 if (d_unlinked(path->dentry)) {
2110 int error = prepend(buf, buflen, " (deleted)", 10);
2111 if (error)
2112 return error;
2113 }
2114
2115 return prepend_path(path, root, buf, buflen);
2116}
2117
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002118static int prepend_unreachable(char **buffer, int *buflen)
2119{
2120 return prepend(buffer, buflen, "(unreachable)", 13);
2121}
2122
Jan Bluncka03a8a702008-02-14 19:38:32 -08002123/**
2124 * d_path - return the path of a dentry
Jan Blunckcf28b482008-02-14 19:38:44 -08002125 * @path: path to report
Jan Bluncka03a8a702008-02-14 19:38:32 -08002126 * @buf: buffer to return value in
2127 * @buflen: buffer length
2128 *
2129 * Convert a dentry into an ASCII path name. If the entry has been deleted
2130 * the string " (deleted)" is appended. Note that this is ambiguous.
2131 *
Arjan van de Ven52afeef2008-12-01 14:35:00 -08002132 * Returns a pointer into the buffer or an error code if the path was
2133 * too long. Note: Callers should use the returned pointer, not the passed
2134 * in buffer, to use the name! The implementation often starts at an offset
2135 * into the buffer, and may leave 0 bytes at the start.
Jan Bluncka03a8a702008-02-14 19:38:32 -08002136 *
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +02002137 * "buflen" should be positive.
Jan Bluncka03a8a702008-02-14 19:38:32 -08002138 */
Jan Engelhardt20d4fdc2008-06-09 16:40:36 -07002139char *d_path(const struct path *path, char *buf, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140{
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002141 char *res = buf + buflen;
Jan Blunck6ac08c32008-02-14 19:34:38 -08002142 struct path root;
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002143 struct path tmp;
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002144 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
Eric Dumazetc23fbb62007-05-08 00:26:18 -07002146 /*
2147 * We have various synthetic filesystems that never get mounted. On
2148 * these filesystems dentries are never used for lookup purposes, and
2149 * thus don't need to be hashed. They also don't need a name until a
2150 * user wants to identify the object in /proc/pid/fd/. The little hack
2151 * below allows us to generate a name for these objects on demand:
2152 */
Jan Blunckcf28b482008-02-14 19:38:44 -08002153 if (path->dentry->d_op && path->dentry->d_op->d_dname)
2154 return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
Eric Dumazetc23fbb62007-05-08 00:26:18 -07002155
Miklos Szeredif7ad3c62010-08-10 11:41:36 +02002156 get_fs_root(current->fs, &root);
Linus Torvalds552ce542007-02-13 12:08:18 -08002157 spin_lock(&dcache_lock);
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002158 tmp = root;
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002159 error = path_with_deleted(path, &tmp, &res, &buflen);
2160 if (error)
2161 res = ERR_PTR(error);
Linus Torvalds552ce542007-02-13 12:08:18 -08002162 spin_unlock(&dcache_lock);
Jan Blunck6ac08c32008-02-14 19:34:38 -08002163 path_put(&root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 return res;
2165}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002166EXPORT_SYMBOL(d_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002168/**
2169 * d_path_with_unreachable - return the path of a dentry
2170 * @path: path to report
2171 * @buf: buffer to return value in
2172 * @buflen: buffer length
2173 *
2174 * The difference from d_path() is that this prepends "(unreachable)"
2175 * to paths which are unreachable from the current process' root.
2176 */
2177char *d_path_with_unreachable(const struct path *path, char *buf, int buflen)
2178{
2179 char *res = buf + buflen;
2180 struct path root;
2181 struct path tmp;
2182 int error;
2183
2184 if (path->dentry->d_op && path->dentry->d_op->d_dname)
2185 return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
2186
2187 get_fs_root(current->fs, &root);
2188 spin_lock(&dcache_lock);
2189 tmp = root;
2190 error = path_with_deleted(path, &tmp, &res, &buflen);
2191 if (!error && !path_equal(&tmp, &root))
2192 error = prepend_unreachable(&res, &buflen);
2193 spin_unlock(&dcache_lock);
2194 path_put(&root);
2195 if (error)
2196 res = ERR_PTR(error);
2197
2198 return res;
2199}
2200
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201/*
Eric Dumazetc23fbb62007-05-08 00:26:18 -07002202 * Helper function for dentry_operations.d_dname() members
2203 */
2204char *dynamic_dname(struct dentry *dentry, char *buffer, int buflen,
2205 const char *fmt, ...)
2206{
2207 va_list args;
2208 char temp[64];
2209 int sz;
2210
2211 va_start(args, fmt);
2212 sz = vsnprintf(temp, sizeof(temp), fmt, args) + 1;
2213 va_end(args);
2214
2215 if (sz > sizeof(temp) || sz > buflen)
2216 return ERR_PTR(-ENAMETOOLONG);
2217
2218 buffer += buflen - sz;
2219 return memcpy(buffer, temp, sz);
2220}
2221
2222/*
Ram Pai6092d042008-03-27 13:06:20 +01002223 * Write full pathname from the root of the filesystem into the buffer.
2224 */
Nick Pigginec2447c2011-01-07 17:49:29 +11002225static char *__dentry_path(struct dentry *dentry, char *buf, int buflen)
Ram Pai6092d042008-03-27 13:06:20 +01002226{
2227 char *end = buf + buflen;
2228 char *retval;
2229
Ram Pai6092d042008-03-27 13:06:20 +01002230 prepend(&end, &buflen, "\0", 1);
Ram Pai6092d042008-03-27 13:06:20 +01002231 if (buflen < 1)
2232 goto Elong;
2233 /* Get '/' right */
2234 retval = end-1;
2235 *retval = '/';
2236
Miklos Szeredicdd16d02008-06-23 18:11:53 +02002237 while (!IS_ROOT(dentry)) {
2238 struct dentry *parent = dentry->d_parent;
Ram Pai6092d042008-03-27 13:06:20 +01002239
Ram Pai6092d042008-03-27 13:06:20 +01002240 prefetch(parent);
Miklos Szeredicdd16d02008-06-23 18:11:53 +02002241 if ((prepend_name(&end, &buflen, &dentry->d_name) != 0) ||
Ram Pai6092d042008-03-27 13:06:20 +01002242 (prepend(&end, &buflen, "/", 1) != 0))
2243 goto Elong;
2244
2245 retval = end;
2246 dentry = parent;
2247 }
Al Viroc1031352010-06-06 22:31:14 -04002248 return retval;
2249Elong:
2250 return ERR_PTR(-ENAMETOOLONG);
2251}
Nick Pigginec2447c2011-01-07 17:49:29 +11002252
2253char *dentry_path_raw(struct dentry *dentry, char *buf, int buflen)
2254{
2255 char *retval;
2256
2257 spin_lock(&dcache_lock);
2258 retval = __dentry_path(dentry, buf, buflen);
2259 spin_unlock(&dcache_lock);
2260
2261 return retval;
2262}
2263EXPORT_SYMBOL(dentry_path_raw);
Al Viroc1031352010-06-06 22:31:14 -04002264
2265char *dentry_path(struct dentry *dentry, char *buf, int buflen)
2266{
2267 char *p = NULL;
2268 char *retval;
2269
2270 spin_lock(&dcache_lock);
2271 if (d_unlinked(dentry)) {
2272 p = buf + buflen;
2273 if (prepend(&p, &buflen, "//deleted", 10) != 0)
2274 goto Elong;
2275 buflen++;
2276 }
2277 retval = __dentry_path(dentry, buf, buflen);
Ram Pai6092d042008-03-27 13:06:20 +01002278 spin_unlock(&dcache_lock);
Al Viroc1031352010-06-06 22:31:14 -04002279 if (!IS_ERR(retval) && p)
2280 *p = '/'; /* restore '/' overriden with '\0' */
Ram Pai6092d042008-03-27 13:06:20 +01002281 return retval;
2282Elong:
2283 spin_unlock(&dcache_lock);
2284 return ERR_PTR(-ENAMETOOLONG);
2285}
2286
2287/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 * NOTE! The user-level library version returns a
2289 * character pointer. The kernel system call just
2290 * returns the length of the buffer filled (which
2291 * includes the ending '\0' character), or a negative
2292 * error value. So libc would do something like
2293 *
2294 * char *getcwd(char * buf, size_t size)
2295 * {
2296 * int retval;
2297 *
2298 * retval = sys_getcwd(buf, size);
2299 * if (retval >= 0)
2300 * return buf;
2301 * errno = -retval;
2302 * return NULL;
2303 * }
2304 */
Heiko Carstens3cdad422009-01-14 14:14:22 +01002305SYSCALL_DEFINE2(getcwd, char __user *, buf, unsigned long, size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306{
Linus Torvalds552ce542007-02-13 12:08:18 -08002307 int error;
Jan Blunck6ac08c32008-02-14 19:34:38 -08002308 struct path pwd, root;
Linus Torvalds552ce542007-02-13 12:08:18 -08002309 char *page = (char *) __get_free_page(GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
2311 if (!page)
2312 return -ENOMEM;
2313
Miklos Szeredif7ad3c62010-08-10 11:41:36 +02002314 get_fs_root_and_pwd(current->fs, &root, &pwd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
Linus Torvalds552ce542007-02-13 12:08:18 -08002316 error = -ENOENT;
Linus Torvalds552ce542007-02-13 12:08:18 -08002317 spin_lock(&dcache_lock);
Alexey Dobriyanf3da3922009-05-04 03:32:03 +04002318 if (!d_unlinked(pwd.dentry)) {
Linus Torvalds552ce542007-02-13 12:08:18 -08002319 unsigned long len;
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002320 struct path tmp = root;
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002321 char *cwd = page + PAGE_SIZE;
2322 int buflen = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002324 prepend(&cwd, &buflen, "\0", 1);
2325 error = prepend_path(&pwd, &tmp, &cwd, &buflen);
Linus Torvalds552ce542007-02-13 12:08:18 -08002326 spin_unlock(&dcache_lock);
2327
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002328 if (error)
Linus Torvalds552ce542007-02-13 12:08:18 -08002329 goto out;
2330
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002331 /* Unreachable from current root */
2332 if (!path_equal(&tmp, &root)) {
2333 error = prepend_unreachable(&cwd, &buflen);
2334 if (error)
2335 goto out;
2336 }
2337
Linus Torvalds552ce542007-02-13 12:08:18 -08002338 error = -ERANGE;
2339 len = PAGE_SIZE + page - cwd;
2340 if (len <= size) {
2341 error = len;
2342 if (copy_to_user(buf, cwd, len))
2343 error = -EFAULT;
2344 }
2345 } else
2346 spin_unlock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347
2348out:
Jan Blunck6ac08c32008-02-14 19:34:38 -08002349 path_put(&pwd);
2350 path_put(&root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 free_page((unsigned long) page);
2352 return error;
2353}
2354
2355/*
2356 * Test whether new_dentry is a subdirectory of old_dentry.
2357 *
2358 * Trivially implemented using the dcache structure
2359 */
2360
2361/**
2362 * is_subdir - is new dentry a subdirectory of old_dentry
2363 * @new_dentry: new dentry
2364 * @old_dentry: old dentry
2365 *
2366 * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
2367 * Returns 0 otherwise.
2368 * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
2369 */
2370
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002371int is_subdir(struct dentry *new_dentry, struct dentry *old_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372{
2373 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 unsigned long seq;
2375
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002376 if (new_dentry == old_dentry)
2377 return 1;
2378
2379 /*
2380 * Need rcu_readlock to protect against the d_parent trashing
2381 * due to d_move
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 */
2383 rcu_read_lock();
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002384 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 /* for restarting inner loop in case of seq retry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 seq = read_seqbegin(&rename_lock);
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002387 if (d_ancestor(old_dentry, new_dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 result = 1;
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002389 else
2390 result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 } while (read_seqretry(&rename_lock, seq));
2392 rcu_read_unlock();
2393
2394 return result;
2395}
2396
Al Viro2096f752010-01-30 13:16:21 -05002397int path_is_under(struct path *path1, struct path *path2)
2398{
2399 struct vfsmount *mnt = path1->mnt;
2400 struct dentry *dentry = path1->dentry;
2401 int res;
Nick Piggin99b7db72010-08-18 04:37:39 +10002402
2403 br_read_lock(vfsmount_lock);
Al Viro2096f752010-01-30 13:16:21 -05002404 if (mnt != path2->mnt) {
2405 for (;;) {
2406 if (mnt->mnt_parent == mnt) {
Nick Piggin99b7db72010-08-18 04:37:39 +10002407 br_read_unlock(vfsmount_lock);
Al Viro2096f752010-01-30 13:16:21 -05002408 return 0;
2409 }
2410 if (mnt->mnt_parent == path2->mnt)
2411 break;
2412 mnt = mnt->mnt_parent;
2413 }
2414 dentry = mnt->mnt_mountpoint;
2415 }
2416 res = is_subdir(dentry, path2->dentry);
Nick Piggin99b7db72010-08-18 04:37:39 +10002417 br_read_unlock(vfsmount_lock);
Al Viro2096f752010-01-30 13:16:21 -05002418 return res;
2419}
2420EXPORT_SYMBOL(path_is_under);
2421
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422void d_genocide(struct dentry *root)
2423{
2424 struct dentry *this_parent = root;
2425 struct list_head *next;
2426
2427 spin_lock(&dcache_lock);
2428repeat:
2429 next = this_parent->d_subdirs.next;
2430resume:
2431 while (next != &this_parent->d_subdirs) {
2432 struct list_head *tmp = next;
Eric Dumazet5160ee62006-01-08 01:03:32 -08002433 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 next = tmp->next;
2435 if (d_unhashed(dentry)||!dentry->d_inode)
2436 continue;
2437 if (!list_empty(&dentry->d_subdirs)) {
2438 this_parent = dentry;
2439 goto repeat;
2440 }
2441 atomic_dec(&dentry->d_count);
2442 }
2443 if (this_parent != root) {
Eric Dumazet5160ee62006-01-08 01:03:32 -08002444 next = this_parent->d_u.d_child.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 atomic_dec(&this_parent->d_count);
2446 this_parent = this_parent->d_parent;
2447 goto resume;
2448 }
2449 spin_unlock(&dcache_lock);
2450}
2451
2452/**
2453 * find_inode_number - check for dentry with name
2454 * @dir: directory to check
2455 * @name: Name to find.
2456 *
2457 * Check whether a dentry already exists for the given name,
2458 * and return the inode number if it has an inode. Otherwise
2459 * 0 is returned.
2460 *
2461 * This routine is used to post-process directory listings for
2462 * filesystems using synthetic inode numbers, and is necessary
2463 * to keep getcwd() working.
2464 */
2465
2466ino_t find_inode_number(struct dentry *dir, struct qstr *name)
2467{
2468 struct dentry * dentry;
2469 ino_t ino = 0;
2470
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08002471 dentry = d_hash_and_lookup(dir, name);
2472 if (dentry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 if (dentry->d_inode)
2474 ino = dentry->d_inode->i_ino;
2475 dput(dentry);
2476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 return ino;
2478}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002479EXPORT_SYMBOL(find_inode_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480
2481static __initdata unsigned long dhash_entries;
2482static int __init set_dhash_entries(char *str)
2483{
2484 if (!str)
2485 return 0;
2486 dhash_entries = simple_strtoul(str, &str, 0);
2487 return 1;
2488}
2489__setup("dhash_entries=", set_dhash_entries);
2490
2491static void __init dcache_init_early(void)
2492{
2493 int loop;
2494
2495 /* If hashes are distributed across NUMA nodes, defer
2496 * hash allocation until vmalloc space is available.
2497 */
2498 if (hashdist)
2499 return;
2500
2501 dentry_hashtable =
2502 alloc_large_system_hash("Dentry cache",
2503 sizeof(struct hlist_head),
2504 dhash_entries,
2505 13,
2506 HASH_EARLY,
2507 &d_hash_shift,
2508 &d_hash_mask,
2509 0);
2510
2511 for (loop = 0; loop < (1 << d_hash_shift); loop++)
2512 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
2513}
2514
Denis Cheng74bf17c2007-10-16 23:26:30 -07002515static void __init dcache_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516{
2517 int loop;
2518
2519 /*
2520 * A constructor could be added for stable state like the lists,
2521 * but it is probably not worth it because of the cache nature
2522 * of the dcache.
2523 */
Christoph Lameter0a31bd52007-05-06 14:49:57 -07002524 dentry_cache = KMEM_CACHE(dentry,
2525 SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|SLAB_MEM_SPREAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526
Rusty Russell8e1f9362007-07-17 04:03:17 -07002527 register_shrinker(&dcache_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528
2529 /* Hash may have been set up in dcache_init_early */
2530 if (!hashdist)
2531 return;
2532
2533 dentry_hashtable =
2534 alloc_large_system_hash("Dentry cache",
2535 sizeof(struct hlist_head),
2536 dhash_entries,
2537 13,
2538 0,
2539 &d_hash_shift,
2540 &d_hash_mask,
2541 0);
2542
2543 for (loop = 0; loop < (1 << d_hash_shift); loop++)
2544 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
2545}
2546
2547/* SLAB cache for __getname() consumers */
Christoph Lametere18b8902006-12-06 20:33:20 -08002548struct kmem_cache *names_cachep __read_mostly;
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002549EXPORT_SYMBOL(names_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551EXPORT_SYMBOL(d_genocide);
2552
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553void __init vfs_caches_init_early(void)
2554{
2555 dcache_init_early();
2556 inode_init_early();
2557}
2558
2559void __init vfs_caches_init(unsigned long mempages)
2560{
2561 unsigned long reserve;
2562
2563 /* Base hash sizes on available memory, with a reserve equal to
2564 150% of current kernel size */
2565
2566 reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);
2567 mempages -= reserve;
2568
2569 names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09002570 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571
Denis Cheng74bf17c2007-10-16 23:26:30 -07002572 dcache_init();
2573 inode_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 files_init(mempages);
Denis Cheng74bf17c2007-10-16 23:26:30 -07002575 mnt_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 bdev_cache_init();
2577 chrdev_init();
2578}