blob: 61beb40dd6bf31c37d70fc5286ea94a32162c334 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/dcache.c
3 *
4 * Complete reimplementation
5 * (C) 1997 Thomas Schoebel-Theuer,
6 * with heavy changes by Linus Torvalds
7 */
8
9/*
10 * Notes on the allocation strategy:
11 *
12 * The dcache is a master of the icache - whenever a dcache entry
13 * exists, the inode will always exist. "iput()" is done either when
14 * the dcache entry is deleted or garbage collected.
15 */
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/syscalls.h>
18#include <linux/string.h>
19#include <linux/mm.h>
20#include <linux/fs.h>
John McCutchan7a91bf72005-08-08 13:52:16 -040021#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/slab.h>
23#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/hash.h>
25#include <linux/cache.h>
26#include <linux/module.h>
27#include <linux/mount.h>
28#include <linux/file.h>
29#include <asm/uaccess.h>
30#include <linux/security.h>
31#include <linux/seqlock.h>
32#include <linux/swap.h>
33#include <linux/bootmem.h>
Al Viro5ad4e532009-03-29 19:50:06 -040034#include <linux/fs_struct.h>
Frederic Weisbecker613afbf2009-07-16 15:44:29 +020035#include <linux/hardirq.h>
David Howells07f3f052006-09-30 20:52:18 +020036#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Eric Dumazetfa3536c2006-03-26 01:37:24 -080038int sysctl_vfs_cache_pressure __read_mostly = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039EXPORT_SYMBOL_GPL(sysctl_vfs_cache_pressure);
40
41 __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_lock);
Al Viro74c3cbe2007-07-22 08:04:18 -040042__cacheline_aligned_in_smp DEFINE_SEQLOCK(rename_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44EXPORT_SYMBOL(dcache_lock);
45
Christoph Lametere18b8902006-12-06 20:33:20 -080046static struct kmem_cache *dentry_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48#define DNAME_INLINE_LEN (sizeof(struct dentry)-offsetof(struct dentry,d_iname))
49
50/*
51 * This is the single most critical data structure when it comes
52 * to the dcache: the hashtable for lookups. Somebody should try
53 * to make this good - I've just made it work.
54 *
55 * This hash-function tries to avoid losing too many bits of hash
56 * information, yet avoid using a prime hash-size or similar.
57 */
58#define D_HASHBITS d_hash_shift
59#define D_HASHMASK d_hash_mask
60
Eric Dumazetfa3536c2006-03-26 01:37:24 -080061static unsigned int d_hash_mask __read_mostly;
62static unsigned int d_hash_shift __read_mostly;
63static struct hlist_head *dentry_hashtable __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65/* Statistics gathering. */
66struct dentry_stat_t dentry_stat = {
67 .age_limit = 45,
68};
69
Nick Piggin3e880fb2011-01-07 17:49:19 +110070static DEFINE_PER_CPU(unsigned int, nr_dentry);
Christoph Hellwig312d3ca2010-10-10 05:36:23 -040071
72#if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS)
Nick Piggin3e880fb2011-01-07 17:49:19 +110073static int get_nr_dentry(void)
74{
75 int i;
76 int sum = 0;
77 for_each_possible_cpu(i)
78 sum += per_cpu(nr_dentry, i);
79 return sum < 0 ? 0 : sum;
80}
81
Christoph Hellwig312d3ca2010-10-10 05:36:23 -040082int proc_nr_dentry(ctl_table *table, int write, void __user *buffer,
83 size_t *lenp, loff_t *ppos)
84{
Nick Piggin3e880fb2011-01-07 17:49:19 +110085 dentry_stat.nr_dentry = get_nr_dentry();
Christoph Hellwig312d3ca2010-10-10 05:36:23 -040086 return proc_dointvec(table, write, buffer, lenp, ppos);
87}
88#endif
89
Christoph Hellwig9c82ab92010-10-10 05:36:22 -040090static void __d_free(struct rcu_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Christoph Hellwig9c82ab92010-10-10 05:36:22 -040092 struct dentry *dentry = container_of(head, struct dentry, d_u.d_rcu);
93
Arjan van de Venfd217f42008-10-21 06:47:33 -070094 WARN_ON(!list_empty(&dentry->d_alias));
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 if (dname_external(dentry))
96 kfree(dentry->d_name.name);
97 kmem_cache_free(dentry_cache, dentry);
98}
99
100/*
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400101 * no dcache_lock, please.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 */
103static void d_free(struct dentry *dentry)
104{
Nick Piggin3e880fb2011-01-07 17:49:19 +1100105 this_cpu_dec(nr_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 if (dentry->d_op && dentry->d_op->d_release)
107 dentry->d_op->d_release(dentry);
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400108
Eric Dumazetb3423412006-12-06 20:38:48 -0800109 /* if dentry was never inserted into hash, immediate free is OK */
Akinobu Mitae8462ca2008-02-06 01:37:07 -0800110 if (hlist_unhashed(&dentry->d_hash))
Christoph Hellwig9c82ab92010-10-10 05:36:22 -0400111 __d_free(&dentry->d_u.d_rcu);
Eric Dumazetb3423412006-12-06 20:38:48 -0800112 else
Christoph Hellwig9c82ab92010-10-10 05:36:22 -0400113 call_rcu(&dentry->d_u.d_rcu, __d_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
115
116/*
117 * Release the dentry's inode, using the filesystem
118 * d_iput() operation if defined.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800120static void dentry_iput(struct dentry * dentry)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200121 __releases(dentry->d_lock)
122 __releases(dcache_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
124 struct inode *inode = dentry->d_inode;
125 if (inode) {
126 dentry->d_inode = NULL;
127 list_del_init(&dentry->d_alias);
128 spin_unlock(&dentry->d_lock);
129 spin_unlock(&dcache_lock);
Linus Torvaldsf805fbd2005-09-19 19:54:29 -0700130 if (!inode->i_nlink)
131 fsnotify_inoderemove(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 if (dentry->d_op && dentry->d_op->d_iput)
133 dentry->d_op->d_iput(dentry, inode);
134 else
135 iput(inode);
136 } else {
137 spin_unlock(&dentry->d_lock);
138 spin_unlock(&dcache_lock);
139 }
140}
141
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700142/*
Christoph Hellwiga4633352010-10-10 05:36:26 -0400143 * dentry_lru_(add|del|move_tail) must be called with dcache_lock held.
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700144 */
145static void dentry_lru_add(struct dentry *dentry)
146{
Christoph Hellwiga4633352010-10-10 05:36:26 -0400147 if (list_empty(&dentry->d_lru)) {
148 list_add(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
149 dentry->d_sb->s_nr_dentry_unused++;
Nick Piggin86c87492011-01-07 17:49:18 +1100150 dentry_stat.nr_unused++;
Christoph Hellwiga4633352010-10-10 05:36:26 -0400151 }
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700152}
153
154static void dentry_lru_del(struct dentry *dentry)
155{
156 if (!list_empty(&dentry->d_lru)) {
Christoph Hellwiga4633352010-10-10 05:36:26 -0400157 list_del_init(&dentry->d_lru);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700158 dentry->d_sb->s_nr_dentry_unused--;
Nick Piggin86c87492011-01-07 17:49:18 +1100159 dentry_stat.nr_unused--;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700160 }
161}
162
Christoph Hellwiga4633352010-10-10 05:36:26 -0400163static void dentry_lru_move_tail(struct dentry *dentry)
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700164{
Christoph Hellwiga4633352010-10-10 05:36:26 -0400165 if (list_empty(&dentry->d_lru)) {
166 list_add_tail(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
167 dentry->d_sb->s_nr_dentry_unused++;
Nick Piggin86c87492011-01-07 17:49:18 +1100168 dentry_stat.nr_unused++;
Christoph Hellwiga4633352010-10-10 05:36:26 -0400169 } else {
170 list_move_tail(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700171 }
172}
173
Miklos Szeredid52b9082007-05-08 00:23:46 -0700174/**
175 * d_kill - kill dentry and return parent
176 * @dentry: dentry to kill
177 *
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200178 * The dentry must already be unhashed and removed from the LRU.
Miklos Szeredid52b9082007-05-08 00:23:46 -0700179 *
180 * If this is the root of the dentry tree, return NULL.
181 */
182static struct dentry *d_kill(struct dentry *dentry)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200183 __releases(dentry->d_lock)
184 __releases(dcache_lock)
Miklos Szeredid52b9082007-05-08 00:23:46 -0700185{
186 struct dentry *parent;
187
188 list_del(&dentry->d_u.d_child);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700189 /*drops the locks, at that point nobody can reach this dentry */
190 dentry_iput(dentry);
OGAWA Hirofumi871c0062008-10-16 07:50:27 +0900191 if (IS_ROOT(dentry))
192 parent = NULL;
193 else
194 parent = dentry->d_parent;
Miklos Szeredid52b9082007-05-08 00:23:46 -0700195 d_free(dentry);
OGAWA Hirofumi871c0062008-10-16 07:50:27 +0900196 return parent;
Miklos Szeredid52b9082007-05-08 00:23:46 -0700197}
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199/*
200 * This is dput
201 *
202 * This is complicated by the fact that we do not want to put
203 * dentries that are no longer on any hash chain on the unused
204 * list: we'd much rather just get rid of them immediately.
205 *
206 * However, that implies that we have to traverse the dentry
207 * tree upwards to the parents which might _also_ now be
208 * scheduled for deletion (it may have been only waiting for
209 * its last child to go away).
210 *
211 * This tail recursion is done by hand as we don't want to depend
212 * on the compiler to always get this right (gcc generally doesn't).
213 * Real recursion would eat up our stack space.
214 */
215
216/*
217 * dput - release a dentry
218 * @dentry: dentry to release
219 *
220 * Release a dentry. This will drop the usage count and if appropriate
221 * call the dentry unlink method as well as removing it from the queues and
222 * releasing its resources. If the parent dentries were scheduled for release
223 * they too may now get deleted.
224 *
225 * no dcache lock, please.
226 */
227
228void dput(struct dentry *dentry)
229{
230 if (!dentry)
231 return;
232
233repeat:
234 if (atomic_read(&dentry->d_count) == 1)
235 might_sleep();
236 if (!atomic_dec_and_lock(&dentry->d_count, &dcache_lock))
237 return;
238
239 spin_lock(&dentry->d_lock);
240 if (atomic_read(&dentry->d_count)) {
241 spin_unlock(&dentry->d_lock);
242 spin_unlock(&dcache_lock);
243 return;
244 }
245
246 /*
247 * AV: ->d_delete() is _NOT_ allowed to block now.
248 */
249 if (dentry->d_op && dentry->d_op->d_delete) {
250 if (dentry->d_op->d_delete(dentry))
251 goto unhash_it;
252 }
Nick Piggin265ac902010-10-10 05:36:24 -0400253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 /* Unreachable? Get rid of it */
255 if (d_unhashed(dentry))
256 goto kill_it;
Nick Piggin265ac902010-10-10 05:36:24 -0400257
258 /* Otherwise leave it cached and ensure it's on the LRU */
259 dentry->d_flags |= DCACHE_REFERENCED;
Christoph Hellwiga4633352010-10-10 05:36:26 -0400260 dentry_lru_add(dentry);
Nick Piggin265ac902010-10-10 05:36:24 -0400261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 spin_unlock(&dentry->d_lock);
263 spin_unlock(&dcache_lock);
264 return;
265
266unhash_it:
267 __d_drop(dentry);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700268kill_it:
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700269 /* if dentry was on the d_lru list delete it from there */
270 dentry_lru_del(dentry);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700271 dentry = d_kill(dentry);
272 if (dentry)
273 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700275EXPORT_SYMBOL(dput);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277/**
278 * d_invalidate - invalidate a dentry
279 * @dentry: dentry to invalidate
280 *
281 * Try to invalidate the dentry if it turns out to be
282 * possible. If there are other dentries that can be
283 * reached through this one we can't delete it and we
284 * return -EBUSY. On success we return 0.
285 *
286 * no dcache lock.
287 */
288
289int d_invalidate(struct dentry * dentry)
290{
291 /*
292 * If it's already been dropped, return OK.
293 */
294 spin_lock(&dcache_lock);
295 if (d_unhashed(dentry)) {
296 spin_unlock(&dcache_lock);
297 return 0;
298 }
299 /*
300 * Check whether to do a partial shrink_dcache
301 * to get rid of unused child entries.
302 */
303 if (!list_empty(&dentry->d_subdirs)) {
304 spin_unlock(&dcache_lock);
305 shrink_dcache_parent(dentry);
306 spin_lock(&dcache_lock);
307 }
308
309 /*
310 * Somebody else still using it?
311 *
312 * If it's a directory, we can't drop it
313 * for fear of somebody re-populating it
314 * with children (even though dropping it
315 * would make it unreachable from the root,
316 * we might still populate it if it was a
317 * working directory or similar).
318 */
319 spin_lock(&dentry->d_lock);
320 if (atomic_read(&dentry->d_count) > 1) {
321 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
322 spin_unlock(&dentry->d_lock);
323 spin_unlock(&dcache_lock);
324 return -EBUSY;
325 }
326 }
327
328 __d_drop(dentry);
329 spin_unlock(&dentry->d_lock);
330 spin_unlock(&dcache_lock);
331 return 0;
332}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700333EXPORT_SYMBOL(d_invalidate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335/* This should be called _only_ with dcache_lock held */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336static inline struct dentry * __dget_locked(struct dentry *dentry)
337{
338 atomic_inc(&dentry->d_count);
Christoph Hellwiga4633352010-10-10 05:36:26 -0400339 dentry_lru_del(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 return dentry;
341}
342
343struct dentry * dget_locked(struct dentry *dentry)
344{
345 return __dget_locked(dentry);
346}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700347EXPORT_SYMBOL(dget_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349/**
350 * d_find_alias - grab a hashed alias of inode
351 * @inode: inode in question
352 * @want_discon: flag, used by d_splice_alias, to request
353 * that only a DISCONNECTED alias be returned.
354 *
355 * If inode has a hashed alias, or is a directory and has any alias,
356 * acquire the reference to alias and return it. Otherwise return NULL.
357 * Notice that if inode is a directory there can be only one alias and
358 * it can be unhashed only if it has no children, or if it is the root
359 * of a filesystem.
360 *
NeilBrown21c0d8f2006-10-04 02:16:16 -0700361 * If the inode has an IS_ROOT, DCACHE_DISCONNECTED alias, then prefer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 * any other hashed alias over that one unless @want_discon is set,
NeilBrown21c0d8f2006-10-04 02:16:16 -0700363 * in which case only return an IS_ROOT, DCACHE_DISCONNECTED alias.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 */
365
366static struct dentry * __d_find_alias(struct inode *inode, int want_discon)
367{
368 struct list_head *head, *next, *tmp;
369 struct dentry *alias, *discon_alias=NULL;
370
371 head = &inode->i_dentry;
372 next = inode->i_dentry.next;
373 while (next != head) {
374 tmp = next;
375 next = tmp->next;
376 prefetch(next);
377 alias = list_entry(tmp, struct dentry, d_alias);
378 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
NeilBrown21c0d8f2006-10-04 02:16:16 -0700379 if (IS_ROOT(alias) &&
380 (alias->d_flags & DCACHE_DISCONNECTED))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 discon_alias = alias;
382 else if (!want_discon) {
383 __dget_locked(alias);
384 return alias;
385 }
386 }
387 }
388 if (discon_alias)
389 __dget_locked(discon_alias);
390 return discon_alias;
391}
392
393struct dentry * d_find_alias(struct inode *inode)
394{
David Howells214fda12006-03-25 03:06:36 -0800395 struct dentry *de = NULL;
396
397 if (!list_empty(&inode->i_dentry)) {
398 spin_lock(&dcache_lock);
399 de = __d_find_alias(inode, 0);
400 spin_unlock(&dcache_lock);
401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 return de;
403}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700404EXPORT_SYMBOL(d_find_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406/*
407 * Try to kill dentries associated with this inode.
408 * WARNING: you must own a reference to inode.
409 */
410void d_prune_aliases(struct inode *inode)
411{
Domen Puncer0cdca3f2005-09-10 00:27:07 -0700412 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413restart:
414 spin_lock(&dcache_lock);
Domen Puncer0cdca3f2005-09-10 00:27:07 -0700415 list_for_each_entry(dentry, &inode->i_dentry, d_alias) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 spin_lock(&dentry->d_lock);
417 if (!atomic_read(&dentry->d_count)) {
418 __dget_locked(dentry);
419 __d_drop(dentry);
420 spin_unlock(&dentry->d_lock);
421 spin_unlock(&dcache_lock);
422 dput(dentry);
423 goto restart;
424 }
425 spin_unlock(&dentry->d_lock);
426 }
427 spin_unlock(&dcache_lock);
428}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700429EXPORT_SYMBOL(d_prune_aliases);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431/*
Andrew Mortond702ccb2006-06-22 14:47:31 -0700432 * Throw away a dentry - free the inode, dput the parent. This requires that
433 * the LRU list has already been removed.
434 *
Miklos Szeredi85864e12007-10-16 23:27:09 -0700435 * Try to prune ancestors as well. This is necessary to prevent
436 * quadratic behavior of shrink_dcache_parent(), but is also expected
437 * to be beneficial in reducing dentry cache fragmentation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 */
Miklos Szeredi85864e12007-10-16 23:27:09 -0700439static void prune_one_dentry(struct dentry * dentry)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200440 __releases(dentry->d_lock)
441 __releases(dcache_lock)
442 __acquires(dcache_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 __d_drop(dentry);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700445 dentry = d_kill(dentry);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700446
447 /*
448 * Prune ancestors. Locking is simpler than in dput(),
449 * because dcache_lock needs to be taken anyway.
450 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 spin_lock(&dcache_lock);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700452 while (dentry) {
453 if (!atomic_dec_and_lock(&dentry->d_count, &dentry->d_lock))
454 return;
455
Christoph Hellwiga4633352010-10-10 05:36:26 -0400456 dentry_lru_del(dentry);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700457 __d_drop(dentry);
458 dentry = d_kill(dentry);
459 spin_lock(&dcache_lock);
460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461}
462
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400463static void shrink_dentry_list(struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700465 struct dentry *dentry;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700466
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400467 while (!list_empty(list)) {
468 dentry = list_entry(list->prev, struct dentry, d_lru);
Christoph Hellwiga4633352010-10-10 05:36:26 -0400469 dentry_lru_del(dentry);
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 /*
472 * We found an inuse dentry which was not removed from
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700473 * the LRU because of laziness during lookup. Do not free
474 * it - just keep it off the LRU list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 */
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400476 spin_lock(&dentry->d_lock);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700477 if (atomic_read(&dentry->d_count)) {
478 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 continue;
480 }
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700481 prune_one_dentry(dentry);
482 /* dentry->d_lock was dropped in prune_one_dentry() */
483 cond_resched_lock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 }
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400485}
486
487/**
488 * __shrink_dcache_sb - shrink the dentry LRU on a given superblock
489 * @sb: superblock to shrink dentry LRU.
490 * @count: number of entries to prune
491 * @flags: flags to control the dentry processing
492 *
493 * If flags contains DCACHE_REFERENCED reference dentries will not be pruned.
494 */
495static void __shrink_dcache_sb(struct super_block *sb, int *count, int flags)
496{
497 /* called from prune_dcache() and shrink_dcache_parent() */
498 struct dentry *dentry;
499 LIST_HEAD(referenced);
500 LIST_HEAD(tmp);
501 int cnt = *count;
502
503 spin_lock(&dcache_lock);
504 while (!list_empty(&sb->s_dentry_lru)) {
505 dentry = list_entry(sb->s_dentry_lru.prev,
506 struct dentry, d_lru);
507 BUG_ON(dentry->d_sb != sb);
508
509 /*
510 * If we are honouring the DCACHE_REFERENCED flag and the
511 * dentry has this flag set, don't free it. Clear the flag
512 * and put it back on the LRU.
513 */
514 if (flags & DCACHE_REFERENCED) {
515 spin_lock(&dentry->d_lock);
516 if (dentry->d_flags & DCACHE_REFERENCED) {
517 dentry->d_flags &= ~DCACHE_REFERENCED;
518 list_move(&dentry->d_lru, &referenced);
519 spin_unlock(&dentry->d_lock);
520 cond_resched_lock(&dcache_lock);
521 continue;
522 }
523 spin_unlock(&dentry->d_lock);
524 }
525
526 list_move_tail(&dentry->d_lru, &tmp);
527 if (!--cnt)
528 break;
529 cond_resched_lock(&dcache_lock);
530 }
531
532 *count = cnt;
533 shrink_dentry_list(&tmp);
534
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700535 if (!list_empty(&referenced))
536 list_splice(&referenced, &sb->s_dentry_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 spin_unlock(&dcache_lock);
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539}
540
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700541/**
542 * prune_dcache - shrink the dcache
543 * @count: number of entries to try to free
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 *
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700545 * Shrink the dcache. This is done when we need more memory, or simply when we
546 * need to unmount something (at which point we need to unuse all dentries).
547 *
548 * This function may fail to free any resources if all the dentries are in use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 */
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700550static void prune_dcache(int count)
551{
Al Virodca33252010-07-25 02:31:46 +0400552 struct super_block *sb, *p = NULL;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700553 int w_count;
Nick Piggin86c87492011-01-07 17:49:18 +1100554 int unused = dentry_stat.nr_unused;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700555 int prune_ratio;
556 int pruned;
557
558 if (unused == 0 || count == 0)
559 return;
560 spin_lock(&dcache_lock);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700561 if (count >= unused)
562 prune_ratio = 1;
563 else
564 prune_ratio = unused / count;
565 spin_lock(&sb_lock);
Al Virodca33252010-07-25 02:31:46 +0400566 list_for_each_entry(sb, &super_blocks, s_list) {
Al Viro551de6f2010-03-22 19:36:35 -0400567 if (list_empty(&sb->s_instances))
568 continue;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700569 if (sb->s_nr_dentry_unused == 0)
570 continue;
571 sb->s_count++;
572 /* Now, we reclaim unused dentrins with fairness.
573 * We reclaim them same percentage from each superblock.
574 * We calculate number of dentries to scan on this sb
575 * as follows, but the implementation is arranged to avoid
576 * overflows:
577 * number of dentries to scan on this sb =
578 * count * (number of dentries on this sb /
579 * number of dentries in the machine)
580 */
581 spin_unlock(&sb_lock);
582 if (prune_ratio != 1)
583 w_count = (sb->s_nr_dentry_unused / prune_ratio) + 1;
584 else
585 w_count = sb->s_nr_dentry_unused;
586 pruned = w_count;
587 /*
588 * We need to be sure this filesystem isn't being unmounted,
589 * otherwise we could race with generic_shutdown_super(), and
590 * end up holding a reference to an inode while the filesystem
591 * is unmounted. So we try to get s_umount, and make sure
592 * s_root isn't NULL.
593 */
594 if (down_read_trylock(&sb->s_umount)) {
595 if ((sb->s_root != NULL) &&
596 (!list_empty(&sb->s_dentry_lru))) {
597 spin_unlock(&dcache_lock);
598 __shrink_dcache_sb(sb, &w_count,
599 DCACHE_REFERENCED);
600 pruned -= w_count;
601 spin_lock(&dcache_lock);
602 }
603 up_read(&sb->s_umount);
604 }
605 spin_lock(&sb_lock);
Al Virodca33252010-07-25 02:31:46 +0400606 if (p)
607 __put_super(p);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700608 count -= pruned;
Al Virodca33252010-07-25 02:31:46 +0400609 p = sb;
Al Viro79893c12010-03-22 20:27:55 -0400610 /* more work left to do? */
611 if (count <= 0)
612 break;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700613 }
Al Virodca33252010-07-25 02:31:46 +0400614 if (p)
615 __put_super(p);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700616 spin_unlock(&sb_lock);
617 spin_unlock(&dcache_lock);
618}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
620/**
621 * shrink_dcache_sb - shrink dcache for a superblock
622 * @sb: superblock
623 *
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400624 * Shrink the dcache for the specified super block. This is used to free
625 * the dcache before unmounting a file system.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 */
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400627void shrink_dcache_sb(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400629 LIST_HEAD(tmp);
630
631 spin_lock(&dcache_lock);
632 while (!list_empty(&sb->s_dentry_lru)) {
633 list_splice_init(&sb->s_dentry_lru, &tmp);
634 shrink_dentry_list(&tmp);
635 }
636 spin_unlock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700638EXPORT_SYMBOL(shrink_dcache_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640/*
David Howellsc636ebd2006-10-11 01:22:19 -0700641 * destroy a single subtree of dentries for unmount
642 * - see the comments on shrink_dcache_for_umount() for a description of the
643 * locking
644 */
645static void shrink_dcache_for_umount_subtree(struct dentry *dentry)
646{
647 struct dentry *parent;
David Howellsf8713572006-10-28 10:38:46 -0700648 unsigned detached = 0;
David Howellsc636ebd2006-10-11 01:22:19 -0700649
650 BUG_ON(!IS_ROOT(dentry));
651
652 /* detach this root from the system */
653 spin_lock(&dcache_lock);
Christoph Hellwiga4633352010-10-10 05:36:26 -0400654 dentry_lru_del(dentry);
David Howellsc636ebd2006-10-11 01:22:19 -0700655 __d_drop(dentry);
656 spin_unlock(&dcache_lock);
657
658 for (;;) {
659 /* descend to the first leaf in the current subtree */
660 while (!list_empty(&dentry->d_subdirs)) {
661 struct dentry *loop;
662
663 /* this is a branch with children - detach all of them
664 * from the system in one go */
665 spin_lock(&dcache_lock);
666 list_for_each_entry(loop, &dentry->d_subdirs,
667 d_u.d_child) {
Christoph Hellwiga4633352010-10-10 05:36:26 -0400668 dentry_lru_del(loop);
David Howellsc636ebd2006-10-11 01:22:19 -0700669 __d_drop(loop);
670 cond_resched_lock(&dcache_lock);
671 }
672 spin_unlock(&dcache_lock);
673
674 /* move to the first child */
675 dentry = list_entry(dentry->d_subdirs.next,
676 struct dentry, d_u.d_child);
677 }
678
679 /* consume the dentries from this leaf up through its parents
680 * until we find one with children or run out altogether */
681 do {
682 struct inode *inode;
683
684 if (atomic_read(&dentry->d_count) != 0) {
685 printk(KERN_ERR
686 "BUG: Dentry %p{i=%lx,n=%s}"
687 " still in use (%d)"
688 " [unmount of %s %s]\n",
689 dentry,
690 dentry->d_inode ?
691 dentry->d_inode->i_ino : 0UL,
692 dentry->d_name.name,
693 atomic_read(&dentry->d_count),
694 dentry->d_sb->s_type->name,
695 dentry->d_sb->s_id);
696 BUG();
697 }
698
OGAWA Hirofumi871c0062008-10-16 07:50:27 +0900699 if (IS_ROOT(dentry))
David Howellsc636ebd2006-10-11 01:22:19 -0700700 parent = NULL;
OGAWA Hirofumi871c0062008-10-16 07:50:27 +0900701 else {
702 parent = dentry->d_parent;
David Howellsc636ebd2006-10-11 01:22:19 -0700703 atomic_dec(&parent->d_count);
OGAWA Hirofumi871c0062008-10-16 07:50:27 +0900704 }
David Howellsc636ebd2006-10-11 01:22:19 -0700705
706 list_del(&dentry->d_u.d_child);
David Howellsf8713572006-10-28 10:38:46 -0700707 detached++;
David Howellsc636ebd2006-10-11 01:22:19 -0700708
709 inode = dentry->d_inode;
710 if (inode) {
711 dentry->d_inode = NULL;
712 list_del_init(&dentry->d_alias);
713 if (dentry->d_op && dentry->d_op->d_iput)
714 dentry->d_op->d_iput(dentry, inode);
715 else
716 iput(inode);
717 }
718
719 d_free(dentry);
720
721 /* finished when we fall off the top of the tree,
722 * otherwise we ascend to the parent and move to the
723 * next sibling if there is one */
724 if (!parent)
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400725 return;
David Howellsc636ebd2006-10-11 01:22:19 -0700726 dentry = parent;
David Howellsc636ebd2006-10-11 01:22:19 -0700727 } while (list_empty(&dentry->d_subdirs));
728
729 dentry = list_entry(dentry->d_subdirs.next,
730 struct dentry, d_u.d_child);
731 }
732}
733
734/*
735 * destroy the dentries attached to a superblock on unmounting
736 * - we don't need to use dentry->d_lock, and only need dcache_lock when
737 * removing the dentry from the system lists and hashes because:
738 * - the superblock is detached from all mountings and open files, so the
739 * dentry trees will not be rearranged by the VFS
740 * - s_umount is write-locked, so the memory pressure shrinker will ignore
741 * any dentries belonging to this superblock that it comes across
742 * - the filesystem itself is no longer permitted to rearrange the dentries
743 * in this superblock
744 */
745void shrink_dcache_for_umount(struct super_block *sb)
746{
747 struct dentry *dentry;
748
749 if (down_read_trylock(&sb->s_umount))
750 BUG();
751
752 dentry = sb->s_root;
753 sb->s_root = NULL;
754 atomic_dec(&dentry->d_count);
755 shrink_dcache_for_umount_subtree(dentry);
756
757 while (!hlist_empty(&sb->s_anon)) {
758 dentry = hlist_entry(sb->s_anon.first, struct dentry, d_hash);
759 shrink_dcache_for_umount_subtree(dentry);
760 }
761}
762
763/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 * Search for at least 1 mount point in the dentry's subdirs.
765 * We descend to the next level whenever the d_subdirs
766 * list is non-empty and continue searching.
767 */
768
769/**
770 * have_submounts - check for mounts over a dentry
771 * @parent: dentry to check.
772 *
773 * Return true if the parent or its subdirectories contain
774 * a mount point
775 */
776
777int have_submounts(struct dentry *parent)
778{
779 struct dentry *this_parent = parent;
780 struct list_head *next;
781
782 spin_lock(&dcache_lock);
783 if (d_mountpoint(parent))
784 goto positive;
785repeat:
786 next = this_parent->d_subdirs.next;
787resume:
788 while (next != &this_parent->d_subdirs) {
789 struct list_head *tmp = next;
Eric Dumazet5160ee62006-01-08 01:03:32 -0800790 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 next = tmp->next;
792 /* Have we found a mount point ? */
793 if (d_mountpoint(dentry))
794 goto positive;
795 if (!list_empty(&dentry->d_subdirs)) {
796 this_parent = dentry;
797 goto repeat;
798 }
799 }
800 /*
801 * All done at this level ... ascend and resume the search.
802 */
803 if (this_parent != parent) {
Eric Dumazet5160ee62006-01-08 01:03:32 -0800804 next = this_parent->d_u.d_child.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 this_parent = this_parent->d_parent;
806 goto resume;
807 }
808 spin_unlock(&dcache_lock);
809 return 0; /* No mount points found in tree */
810positive:
811 spin_unlock(&dcache_lock);
812 return 1;
813}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700814EXPORT_SYMBOL(have_submounts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
816/*
817 * Search the dentry child list for the specified parent,
818 * and move any unused dentries to the end of the unused
819 * list for prune_dcache(). We descend to the next level
820 * whenever the d_subdirs list is non-empty and continue
821 * searching.
822 *
823 * It returns zero iff there are no unused children,
824 * otherwise it returns the number of children moved to
825 * the end of the unused list. This may not be the total
826 * number of unused children, because select_parent can
827 * drop the lock and return early due to latency
828 * constraints.
829 */
830static int select_parent(struct dentry * parent)
831{
832 struct dentry *this_parent = parent;
833 struct list_head *next;
834 int found = 0;
835
836 spin_lock(&dcache_lock);
837repeat:
838 next = this_parent->d_subdirs.next;
839resume:
840 while (next != &this_parent->d_subdirs) {
841 struct list_head *tmp = next;
Eric Dumazet5160ee62006-01-08 01:03:32 -0800842 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 next = tmp->next;
844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 /*
846 * move only zero ref count dentries to the end
847 * of the unused list for prune_dcache
848 */
849 if (!atomic_read(&dentry->d_count)) {
Christoph Hellwiga4633352010-10-10 05:36:26 -0400850 dentry_lru_move_tail(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 found++;
Christoph Hellwiga4633352010-10-10 05:36:26 -0400852 } else {
853 dentry_lru_del(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 }
855
856 /*
857 * We can return to the caller if we have found some (this
858 * ensures forward progress). We'll be coming back to find
859 * the rest.
860 */
861 if (found && need_resched())
862 goto out;
863
864 /*
865 * Descend a level if the d_subdirs list is non-empty.
866 */
867 if (!list_empty(&dentry->d_subdirs)) {
868 this_parent = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 goto repeat;
870 }
871 }
872 /*
873 * All done at this level ... ascend and resume the search.
874 */
875 if (this_parent != parent) {
Eric Dumazet5160ee62006-01-08 01:03:32 -0800876 next = this_parent->d_u.d_child.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 this_parent = this_parent->d_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 goto resume;
879 }
880out:
881 spin_unlock(&dcache_lock);
882 return found;
883}
884
885/**
886 * shrink_dcache_parent - prune dcache
887 * @parent: parent of entries to prune
888 *
889 * Prune the dcache to remove unused children of the parent dentry.
890 */
891
892void shrink_dcache_parent(struct dentry * parent)
893{
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700894 struct super_block *sb = parent->d_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 int found;
896
897 while ((found = select_parent(parent)) != 0)
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700898 __shrink_dcache_sb(sb, &found, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700900EXPORT_SYMBOL(shrink_dcache_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902/*
903 * Scan `nr' dentries and return the number which remain.
904 *
905 * We need to avoid reentering the filesystem if the caller is performing a
906 * GFP_NOFS allocation attempt. One example deadlock is:
907 *
908 * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache->
909 * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->put_inode->
910 * ext2_discard_prealloc->ext2_free_blocks->lock_super->DEADLOCK.
911 *
912 * In this case we return -1 to tell the caller that we baled.
913 */
Dave Chinner7f8275d2010-07-19 14:56:17 +1000914static int shrink_dcache_memory(struct shrinker *shrink, int nr, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915{
916 if (nr) {
917 if (!(gfp_mask & __GFP_FS))
918 return -1;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700919 prune_dcache(nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 }
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400921
Nick Piggin86c87492011-01-07 17:49:18 +1100922 return (dentry_stat.nr_unused / 100) * sysctl_vfs_cache_pressure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923}
924
Rusty Russell8e1f9362007-07-17 04:03:17 -0700925static struct shrinker dcache_shrinker = {
926 .shrink = shrink_dcache_memory,
927 .seeks = DEFAULT_SEEKS,
928};
929
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930/**
931 * d_alloc - allocate a dcache entry
932 * @parent: parent of entry to allocate
933 * @name: qstr of the name
934 *
935 * Allocates a dentry. It returns %NULL if there is insufficient memory
936 * available. On a success the dentry is returned. The name passed in is
937 * copied and the copy passed in may be reused after this call.
938 */
939
940struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
941{
942 struct dentry *dentry;
943 char *dname;
944
Mel Gormane12ba742007-10-16 01:25:52 -0700945 dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 if (!dentry)
947 return NULL;
948
949 if (name->len > DNAME_INLINE_LEN-1) {
950 dname = kmalloc(name->len + 1, GFP_KERNEL);
951 if (!dname) {
952 kmem_cache_free(dentry_cache, dentry);
953 return NULL;
954 }
955 } else {
956 dname = dentry->d_iname;
957 }
958 dentry->d_name.name = dname;
959
960 dentry->d_name.len = name->len;
961 dentry->d_name.hash = name->hash;
962 memcpy(dname, name->name, name->len);
963 dname[name->len] = 0;
964
965 atomic_set(&dentry->d_count, 1);
966 dentry->d_flags = DCACHE_UNHASHED;
967 spin_lock_init(&dentry->d_lock);
968 dentry->d_inode = NULL;
969 dentry->d_parent = NULL;
970 dentry->d_sb = NULL;
971 dentry->d_op = NULL;
972 dentry->d_fsdata = NULL;
973 dentry->d_mounted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 INIT_HLIST_NODE(&dentry->d_hash);
975 INIT_LIST_HEAD(&dentry->d_lru);
976 INIT_LIST_HEAD(&dentry->d_subdirs);
977 INIT_LIST_HEAD(&dentry->d_alias);
978
979 if (parent) {
980 dentry->d_parent = dget(parent);
981 dentry->d_sb = parent->d_sb;
982 } else {
Eric Dumazet5160ee62006-01-08 01:03:32 -0800983 INIT_LIST_HEAD(&dentry->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 }
985
986 spin_lock(&dcache_lock);
987 if (parent)
Eric Dumazet5160ee62006-01-08 01:03:32 -0800988 list_add(&dentry->d_u.d_child, &parent->d_subdirs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 spin_unlock(&dcache_lock);
990
Nick Piggin3e880fb2011-01-07 17:49:19 +1100991 this_cpu_inc(nr_dentry);
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 return dentry;
994}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700995EXPORT_SYMBOL(d_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
997struct dentry *d_alloc_name(struct dentry *parent, const char *name)
998{
999 struct qstr q;
1000
1001 q.name = name;
1002 q.len = strlen(name);
1003 q.hash = full_name_hash(q.name, q.len);
1004 return d_alloc(parent, &q);
1005}
H Hartley Sweetenef26ca92009-09-29 20:09:42 -04001006EXPORT_SYMBOL(d_alloc_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001008/* the caller must hold dcache_lock */
1009static void __d_instantiate(struct dentry *dentry, struct inode *inode)
1010{
1011 if (inode)
1012 list_add(&dentry->d_alias, &inode->i_dentry);
1013 dentry->d_inode = inode;
1014 fsnotify_d_instantiate(dentry, inode);
1015}
1016
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017/**
1018 * d_instantiate - fill in inode information for a dentry
1019 * @entry: dentry to complete
1020 * @inode: inode to attach to this dentry
1021 *
1022 * Fill in inode information in the entry.
1023 *
1024 * This turns negative dentries into productive full members
1025 * of society.
1026 *
1027 * NOTE! This assumes that the inode count has been incremented
1028 * (or otherwise set) by the caller to indicate that it is now
1029 * in use by the dcache.
1030 */
1031
1032void d_instantiate(struct dentry *entry, struct inode * inode)
1033{
Eric Sesterhenn28133c72006-03-26 18:25:39 +02001034 BUG_ON(!list_empty(&entry->d_alias));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 spin_lock(&dcache_lock);
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001036 __d_instantiate(entry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 spin_unlock(&dcache_lock);
1038 security_d_instantiate(entry, inode);
1039}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001040EXPORT_SYMBOL(d_instantiate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
1042/**
1043 * d_instantiate_unique - instantiate a non-aliased dentry
1044 * @entry: dentry to instantiate
1045 * @inode: inode to attach to this dentry
1046 *
1047 * Fill in inode information in the entry. On success, it returns NULL.
1048 * If an unhashed alias of "entry" already exists, then we return the
Oleg Drokine866cfa2006-01-09 20:52:51 -08001049 * aliased dentry instead and drop one reference to inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 *
1051 * Note that in order to avoid conflicts with rename() etc, the caller
1052 * had better be holding the parent directory semaphore.
Oleg Drokine866cfa2006-01-09 20:52:51 -08001053 *
1054 * This also assumes that the inode count has been incremented
1055 * (or otherwise set) by the caller to indicate that it is now
1056 * in use by the dcache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 */
David Howells770bfad2006-08-22 20:06:07 -04001058static struct dentry *__d_instantiate_unique(struct dentry *entry,
1059 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060{
1061 struct dentry *alias;
1062 int len = entry->d_name.len;
1063 const char *name = entry->d_name.name;
1064 unsigned int hash = entry->d_name.hash;
1065
David Howells770bfad2006-08-22 20:06:07 -04001066 if (!inode) {
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001067 __d_instantiate(entry, NULL);
David Howells770bfad2006-08-22 20:06:07 -04001068 return NULL;
1069 }
1070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 list_for_each_entry(alias, &inode->i_dentry, d_alias) {
1072 struct qstr *qstr = &alias->d_name;
1073
1074 if (qstr->hash != hash)
1075 continue;
1076 if (alias->d_parent != entry->d_parent)
1077 continue;
1078 if (qstr->len != len)
1079 continue;
1080 if (memcmp(qstr->name, name, len))
1081 continue;
1082 dget_locked(alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 return alias;
1084 }
David Howells770bfad2006-08-22 20:06:07 -04001085
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001086 __d_instantiate(entry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 return NULL;
1088}
David Howells770bfad2006-08-22 20:06:07 -04001089
1090struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode)
1091{
1092 struct dentry *result;
1093
1094 BUG_ON(!list_empty(&entry->d_alias));
1095
1096 spin_lock(&dcache_lock);
1097 result = __d_instantiate_unique(entry, inode);
1098 spin_unlock(&dcache_lock);
1099
1100 if (!result) {
1101 security_d_instantiate(entry, inode);
1102 return NULL;
1103 }
1104
1105 BUG_ON(!d_unhashed(result));
1106 iput(inode);
1107 return result;
1108}
1109
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110EXPORT_SYMBOL(d_instantiate_unique);
1111
1112/**
1113 * d_alloc_root - allocate root dentry
1114 * @root_inode: inode to allocate the root for
1115 *
1116 * Allocate a root ("/") dentry for the inode given. The inode is
1117 * instantiated and returned. %NULL is returned if there is insufficient
1118 * memory or the inode passed is %NULL.
1119 */
1120
1121struct dentry * d_alloc_root(struct inode * root_inode)
1122{
1123 struct dentry *res = NULL;
1124
1125 if (root_inode) {
1126 static const struct qstr name = { .name = "/", .len = 1 };
1127
1128 res = d_alloc(NULL, &name);
1129 if (res) {
1130 res->d_sb = root_inode->i_sb;
1131 res->d_parent = res;
1132 d_instantiate(res, root_inode);
1133 }
1134 }
1135 return res;
1136}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001137EXPORT_SYMBOL(d_alloc_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
1139static inline struct hlist_head *d_hash(struct dentry *parent,
1140 unsigned long hash)
1141{
1142 hash += ((unsigned long) parent ^ GOLDEN_RATIO_PRIME) / L1_CACHE_BYTES;
1143 hash = hash ^ ((hash ^ GOLDEN_RATIO_PRIME) >> D_HASHBITS);
1144 return dentry_hashtable + (hash & D_HASHMASK);
1145}
1146
1147/**
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001148 * d_obtain_alias - find or allocate a dentry for a given inode
1149 * @inode: inode to allocate the dentry for
1150 *
1151 * Obtain a dentry for an inode resulting from NFS filehandle conversion or
1152 * similar open by handle operations. The returned dentry may be anonymous,
1153 * or may have a full name (if the inode was already in the cache).
1154 *
1155 * When called on a directory inode, we must ensure that the inode only ever
1156 * has one dentry. If a dentry is found, that is returned instead of
1157 * allocating a new one.
1158 *
1159 * On successful return, the reference to the inode has been transferred
Christoph Hellwig44003722008-08-11 15:49:04 +02001160 * to the dentry. In case of an error the reference on the inode is released.
1161 * To make it easier to use in export operations a %NULL or IS_ERR inode may
1162 * be passed in and will be the error will be propagate to the return value,
1163 * with a %NULL @inode replaced by ERR_PTR(-ESTALE).
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001164 */
1165struct dentry *d_obtain_alias(struct inode *inode)
1166{
Christoph Hellwig9308a612008-08-11 15:49:12 +02001167 static const struct qstr anonstring = { .name = "" };
1168 struct dentry *tmp;
1169 struct dentry *res;
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001170
1171 if (!inode)
Christoph Hellwig44003722008-08-11 15:49:04 +02001172 return ERR_PTR(-ESTALE);
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001173 if (IS_ERR(inode))
1174 return ERR_CAST(inode);
1175
Christoph Hellwig9308a612008-08-11 15:49:12 +02001176 res = d_find_alias(inode);
1177 if (res)
1178 goto out_iput;
1179
1180 tmp = d_alloc(NULL, &anonstring);
1181 if (!tmp) {
1182 res = ERR_PTR(-ENOMEM);
1183 goto out_iput;
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001184 }
Christoph Hellwig9308a612008-08-11 15:49:12 +02001185 tmp->d_parent = tmp; /* make sure dput doesn't croak */
1186
1187 spin_lock(&dcache_lock);
1188 res = __d_find_alias(inode, 0);
1189 if (res) {
1190 spin_unlock(&dcache_lock);
1191 dput(tmp);
1192 goto out_iput;
1193 }
1194
1195 /* attach a disconnected dentry */
1196 spin_lock(&tmp->d_lock);
1197 tmp->d_sb = inode->i_sb;
1198 tmp->d_inode = inode;
1199 tmp->d_flags |= DCACHE_DISCONNECTED;
1200 tmp->d_flags &= ~DCACHE_UNHASHED;
1201 list_add(&tmp->d_alias, &inode->i_dentry);
1202 hlist_add_head(&tmp->d_hash, &inode->i_sb->s_anon);
1203 spin_unlock(&tmp->d_lock);
1204
1205 spin_unlock(&dcache_lock);
1206 return tmp;
1207
1208 out_iput:
1209 iput(inode);
1210 return res;
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001211}
Benny Halevyadc48722009-02-27 14:02:59 -08001212EXPORT_SYMBOL(d_obtain_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
1214/**
1215 * d_splice_alias - splice a disconnected dentry into the tree if one exists
1216 * @inode: the inode which may have a disconnected dentry
1217 * @dentry: a negative dentry which we want to point to the inode.
1218 *
1219 * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
1220 * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
1221 * and return it, else simply d_add the inode to the dentry and return NULL.
1222 *
1223 * This is needed in the lookup routine of any filesystem that is exportable
1224 * (via knfsd) so that we can build dcache paths to directories effectively.
1225 *
1226 * If a dentry was found and moved, then it is returned. Otherwise NULL
1227 * is returned. This matches the expected return value of ->lookup.
1228 *
1229 */
1230struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
1231{
1232 struct dentry *new = NULL;
1233
NeilBrown21c0d8f2006-10-04 02:16:16 -07001234 if (inode && S_ISDIR(inode->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 spin_lock(&dcache_lock);
1236 new = __d_find_alias(inode, 1);
1237 if (new) {
1238 BUG_ON(!(new->d_flags & DCACHE_DISCONNECTED));
1239 spin_unlock(&dcache_lock);
1240 security_d_instantiate(new, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 d_move(new, dentry);
1242 iput(inode);
1243 } else {
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001244 /* already taking dcache_lock, so d_add() by hand */
1245 __d_instantiate(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 spin_unlock(&dcache_lock);
1247 security_d_instantiate(dentry, inode);
1248 d_rehash(dentry);
1249 }
1250 } else
1251 d_add(dentry, inode);
1252 return new;
1253}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001254EXPORT_SYMBOL(d_splice_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
Barry Naujok94035402008-05-21 16:50:46 +10001256/**
1257 * d_add_ci - lookup or allocate new dentry with case-exact name
1258 * @inode: the inode case-insensitive lookup has found
1259 * @dentry: the negative dentry that was passed to the parent's lookup func
1260 * @name: the case-exact name to be associated with the returned dentry
1261 *
1262 * This is to avoid filling the dcache with case-insensitive names to the
1263 * same inode, only the actual correct case is stored in the dcache for
1264 * case-insensitive filesystems.
1265 *
1266 * For a case-insensitive lookup match and if the the case-exact dentry
1267 * already exists in in the dcache, use it and return it.
1268 *
1269 * If no entry exists with the exact case name, allocate new dentry with
1270 * the exact case, and return the spliced entry.
1271 */
Christoph Hellwige45b5902008-08-07 23:49:07 +02001272struct dentry *d_add_ci(struct dentry *dentry, struct inode *inode,
Barry Naujok94035402008-05-21 16:50:46 +10001273 struct qstr *name)
1274{
1275 int error;
1276 struct dentry *found;
1277 struct dentry *new;
1278
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001279 /*
1280 * First check if a dentry matching the name already exists,
1281 * if not go ahead and create it now.
1282 */
Barry Naujok94035402008-05-21 16:50:46 +10001283 found = d_hash_and_lookup(dentry->d_parent, name);
Barry Naujok94035402008-05-21 16:50:46 +10001284 if (!found) {
1285 new = d_alloc(dentry->d_parent, name);
1286 if (!new) {
1287 error = -ENOMEM;
1288 goto err_out;
1289 }
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001290
Barry Naujok94035402008-05-21 16:50:46 +10001291 found = d_splice_alias(inode, new);
1292 if (found) {
1293 dput(new);
1294 return found;
1295 }
1296 return new;
1297 }
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001298
1299 /*
1300 * If a matching dentry exists, and it's not negative use it.
1301 *
1302 * Decrement the reference count to balance the iget() done
1303 * earlier on.
1304 */
Barry Naujok94035402008-05-21 16:50:46 +10001305 if (found->d_inode) {
1306 if (unlikely(found->d_inode != inode)) {
1307 /* This can't happen because bad inodes are unhashed. */
1308 BUG_ON(!is_bad_inode(inode));
1309 BUG_ON(!is_bad_inode(found->d_inode));
1310 }
Barry Naujok94035402008-05-21 16:50:46 +10001311 iput(inode);
1312 return found;
1313 }
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001314
Barry Naujok94035402008-05-21 16:50:46 +10001315 /*
1316 * Negative dentry: instantiate it unless the inode is a directory and
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001317 * already has a dentry.
Barry Naujok94035402008-05-21 16:50:46 +10001318 */
Barry Naujok94035402008-05-21 16:50:46 +10001319 spin_lock(&dcache_lock);
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001320 if (!S_ISDIR(inode->i_mode) || list_empty(&inode->i_dentry)) {
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001321 __d_instantiate(found, inode);
Barry Naujok94035402008-05-21 16:50:46 +10001322 spin_unlock(&dcache_lock);
1323 security_d_instantiate(found, inode);
1324 return found;
1325 }
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001326
Barry Naujok94035402008-05-21 16:50:46 +10001327 /*
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001328 * In case a directory already has a (disconnected) entry grab a
1329 * reference to it, move it in place and use it.
Barry Naujok94035402008-05-21 16:50:46 +10001330 */
1331 new = list_entry(inode->i_dentry.next, struct dentry, d_alias);
1332 dget_locked(new);
1333 spin_unlock(&dcache_lock);
Barry Naujok94035402008-05-21 16:50:46 +10001334 security_d_instantiate(found, inode);
Barry Naujok94035402008-05-21 16:50:46 +10001335 d_move(new, found);
Barry Naujok94035402008-05-21 16:50:46 +10001336 iput(inode);
Barry Naujok94035402008-05-21 16:50:46 +10001337 dput(found);
Barry Naujok94035402008-05-21 16:50:46 +10001338 return new;
1339
1340err_out:
1341 iput(inode);
1342 return ERR_PTR(error);
1343}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001344EXPORT_SYMBOL(d_add_ci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
1346/**
1347 * d_lookup - search for a dentry
1348 * @parent: parent dentry
1349 * @name: qstr of name we wish to find
Nick Pigginb04f7842010-08-18 04:37:34 +10001350 * Returns: dentry, or NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 *
Nick Pigginb04f7842010-08-18 04:37:34 +10001352 * d_lookup searches the children of the parent dentry for the name in
1353 * question. If the dentry is found its reference count is incremented and the
1354 * dentry is returned. The caller must use dput to free the entry when it has
1355 * finished using it. %NULL is returned if the dentry does not exist.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357struct dentry * d_lookup(struct dentry * parent, struct qstr * name)
1358{
1359 struct dentry * dentry = NULL;
1360 unsigned long seq;
1361
1362 do {
1363 seq = read_seqbegin(&rename_lock);
1364 dentry = __d_lookup(parent, name);
1365 if (dentry)
1366 break;
1367 } while (read_seqretry(&rename_lock, seq));
1368 return dentry;
1369}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001370EXPORT_SYMBOL(d_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Nick Pigginb04f7842010-08-18 04:37:34 +10001372/*
1373 * __d_lookup - search for a dentry (racy)
1374 * @parent: parent dentry
1375 * @name: qstr of name we wish to find
1376 * Returns: dentry, or NULL
1377 *
1378 * __d_lookup is like d_lookup, however it may (rarely) return a
1379 * false-negative result due to unrelated rename activity.
1380 *
1381 * __d_lookup is slightly faster by avoiding rename_lock read seqlock,
1382 * however it must be used carefully, eg. with a following d_lookup in
1383 * the case of failure.
1384 *
1385 * __d_lookup callers must be commented.
1386 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387struct dentry * __d_lookup(struct dentry * parent, struct qstr * name)
1388{
1389 unsigned int len = name->len;
1390 unsigned int hash = name->hash;
1391 const unsigned char *str = name->name;
1392 struct hlist_head *head = d_hash(parent,hash);
1393 struct dentry *found = NULL;
1394 struct hlist_node *node;
Paul E. McKenney665a7582005-11-07 00:59:17 -08001395 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
Nick Pigginb04f7842010-08-18 04:37:34 +10001397 /*
1398 * The hash list is protected using RCU.
1399 *
1400 * Take d_lock when comparing a candidate dentry, to avoid races
1401 * with d_move().
1402 *
1403 * It is possible that concurrent renames can mess up our list
1404 * walk here and result in missing our dentry, resulting in the
1405 * false-negative result. d_lookup() protects against concurrent
1406 * renames using rename_lock seqlock.
1407 *
1408 * See Documentation/vfs/dcache-locking.txt for more details.
1409 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 rcu_read_lock();
1411
Paul E. McKenney665a7582005-11-07 00:59:17 -08001412 hlist_for_each_entry_rcu(dentry, node, head, d_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 struct qstr *qstr;
1414
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 if (dentry->d_name.hash != hash)
1416 continue;
1417 if (dentry->d_parent != parent)
1418 continue;
1419
1420 spin_lock(&dentry->d_lock);
1421
1422 /*
1423 * Recheck the dentry after taking the lock - d_move may have
Nick Pigginb04f7842010-08-18 04:37:34 +10001424 * changed things. Don't bother checking the hash because
1425 * we're about to compare the whole name anyway.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 */
1427 if (dentry->d_parent != parent)
1428 goto next;
1429
Linus Torvaldsd0185c02008-09-29 07:42:57 -07001430 /* non-existing due to RCU? */
1431 if (d_unhashed(dentry))
1432 goto next;
1433
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 /*
1435 * It is safe to compare names since d_move() cannot
1436 * change the qstr (protected by d_lock).
1437 */
1438 qstr = &dentry->d_name;
1439 if (parent->d_op && parent->d_op->d_compare) {
Nick Piggin621e1552011-01-07 17:49:27 +11001440 if (parent->d_op->d_compare(parent, parent->d_inode,
1441 dentry, dentry->d_inode,
1442 qstr->len, qstr->name, name))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 goto next;
1444 } else {
1445 if (qstr->len != len)
1446 goto next;
1447 if (memcmp(qstr->name, str, len))
1448 goto next;
1449 }
1450
Linus Torvaldsd0185c02008-09-29 07:42:57 -07001451 atomic_inc(&dentry->d_count);
1452 found = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 spin_unlock(&dentry->d_lock);
1454 break;
1455next:
1456 spin_unlock(&dentry->d_lock);
1457 }
1458 rcu_read_unlock();
1459
1460 return found;
1461}
1462
1463/**
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08001464 * d_hash_and_lookup - hash the qstr then search for a dentry
1465 * @dir: Directory to search in
1466 * @name: qstr of name we wish to find
1467 *
1468 * On hash failure or on lookup failure NULL is returned.
1469 */
1470struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name)
1471{
1472 struct dentry *dentry = NULL;
1473
1474 /*
1475 * Check for a fs-specific hash function. Note that we must
1476 * calculate the standard hash first, as the d_op->d_hash()
1477 * routine may choose to leave the hash value unchanged.
1478 */
1479 name->hash = full_name_hash(name->name, name->len);
1480 if (dir->d_op && dir->d_op->d_hash) {
Nick Pigginb1e6a012011-01-07 17:49:28 +11001481 if (dir->d_op->d_hash(dir, dir->d_inode, name) < 0)
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08001482 goto out;
1483 }
1484 dentry = d_lookup(dir, name);
1485out:
1486 return dentry;
1487}
1488
1489/**
Nick Piggin786a5e12011-01-07 17:49:16 +11001490 * d_validate - verify dentry provided from insecure source (deprecated)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 * @dentry: The dentry alleged to be valid child of @dparent
1492 * @dparent: The parent dentry (known to be valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 *
1494 * An insecure source has sent us a dentry, here we verify it and dget() it.
1495 * This is used by ncpfs in its readdir implementation.
1496 * Zero is returned in the dentry is invalid.
Nick Piggin786a5e12011-01-07 17:49:16 +11001497 *
1498 * This function is slow for big directories, and deprecated, do not use it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 */
Nick Piggind3a23e12011-01-05 20:01:21 +11001500int d_validate(struct dentry *dentry, struct dentry *dparent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501{
Nick Piggin786a5e12011-01-07 17:49:16 +11001502 struct dentry *child;
Nick Piggind3a23e12011-01-05 20:01:21 +11001503
1504 spin_lock(&dcache_lock);
Nick Piggin786a5e12011-01-07 17:49:16 +11001505 list_for_each_entry(child, &dparent->d_subdirs, d_u.d_child) {
1506 if (dentry == child) {
Nick Piggind3a23e12011-01-05 20:01:21 +11001507 __dget_locked(dentry);
1508 spin_unlock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 return 1;
1510 }
1511 }
Nick Piggind3a23e12011-01-05 20:01:21 +11001512 spin_unlock(&dcache_lock);
Nick Piggin786a5e12011-01-07 17:49:16 +11001513
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 return 0;
1515}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001516EXPORT_SYMBOL(d_validate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
1518/*
1519 * When a file is deleted, we have two options:
1520 * - turn this dentry into a negative dentry
1521 * - unhash this dentry and free it.
1522 *
1523 * Usually, we want to just turn this into
1524 * a negative dentry, but if anybody else is
1525 * currently using the dentry or the inode
1526 * we can't do that and we fall back on removing
1527 * it from the hash queues and waiting for
1528 * it to be deleted later when it has no users
1529 */
1530
1531/**
1532 * d_delete - delete a dentry
1533 * @dentry: The dentry to delete
1534 *
1535 * Turn the dentry into a negative dentry if possible, otherwise
1536 * remove it from the hash queues so it can be deleted later
1537 */
1538
1539void d_delete(struct dentry * dentry)
1540{
John McCutchan7a91bf72005-08-08 13:52:16 -04001541 int isdir = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 /*
1543 * Are we the only user?
1544 */
1545 spin_lock(&dcache_lock);
1546 spin_lock(&dentry->d_lock);
John McCutchan7a91bf72005-08-08 13:52:16 -04001547 isdir = S_ISDIR(dentry->d_inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 if (atomic_read(&dentry->d_count) == 1) {
Al Viro13e3c5e2010-05-21 16:11:04 -04001549 dentry->d_flags &= ~DCACHE_CANT_MOUNT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 dentry_iput(dentry);
John McCutchan7a91bf72005-08-08 13:52:16 -04001551 fsnotify_nameremove(dentry, isdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 return;
1553 }
1554
1555 if (!d_unhashed(dentry))
1556 __d_drop(dentry);
1557
1558 spin_unlock(&dentry->d_lock);
1559 spin_unlock(&dcache_lock);
John McCutchan7a91bf72005-08-08 13:52:16 -04001560
1561 fsnotify_nameremove(dentry, isdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001563EXPORT_SYMBOL(d_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
1565static void __d_rehash(struct dentry * entry, struct hlist_head *list)
1566{
1567
1568 entry->d_flags &= ~DCACHE_UNHASHED;
1569 hlist_add_head_rcu(&entry->d_hash, list);
1570}
1571
David Howells770bfad2006-08-22 20:06:07 -04001572static void _d_rehash(struct dentry * entry)
1573{
1574 __d_rehash(entry, d_hash(entry->d_parent, entry->d_name.hash));
1575}
1576
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577/**
1578 * d_rehash - add an entry back to the hash
1579 * @entry: dentry to add to the hash
1580 *
1581 * Adds a dentry to the hash according to its name.
1582 */
1583
1584void d_rehash(struct dentry * entry)
1585{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 spin_lock(&dcache_lock);
1587 spin_lock(&entry->d_lock);
David Howells770bfad2006-08-22 20:06:07 -04001588 _d_rehash(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 spin_unlock(&entry->d_lock);
1590 spin_unlock(&dcache_lock);
1591}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001592EXPORT_SYMBOL(d_rehash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
Nick Pigginfb2d5b82011-01-07 17:49:26 +11001594/**
1595 * dentry_update_name_case - update case insensitive dentry with a new name
1596 * @dentry: dentry to be updated
1597 * @name: new name
1598 *
1599 * Update a case insensitive dentry with new case of name.
1600 *
1601 * dentry must have been returned by d_lookup with name @name. Old and new
1602 * name lengths must match (ie. no d_compare which allows mismatched name
1603 * lengths).
1604 *
1605 * Parent inode i_mutex must be held over d_lookup and into this call (to
1606 * keep renames and concurrent inserts, and readdir(2) away).
1607 */
1608void dentry_update_name_case(struct dentry *dentry, struct qstr *name)
1609{
1610 BUG_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
1611 BUG_ON(dentry->d_name.len != name->len); /* d_lookup gives this */
1612
1613 spin_lock(&dcache_lock);
1614 spin_lock(&dentry->d_lock);
1615 memcpy((unsigned char *)dentry->d_name.name, name->name, name->len);
1616 spin_unlock(&dentry->d_lock);
1617 spin_unlock(&dcache_lock);
1618}
1619EXPORT_SYMBOL(dentry_update_name_case);
1620
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621/*
1622 * When switching names, the actual string doesn't strictly have to
1623 * be preserved in the target - because we're dropping the target
1624 * anyway. As such, we can just do a simple memcpy() to copy over
1625 * the new name before we switch.
1626 *
1627 * Note that we have to be a lot more careful about getting the hash
1628 * switched - we have to switch the hash value properly even if it
1629 * then no longer matches the actual (corrupted) string of the target.
1630 * The hash value has to match the hash queue that the dentry is on..
1631 */
1632static void switch_names(struct dentry *dentry, struct dentry *target)
1633{
1634 if (dname_external(target)) {
1635 if (dname_external(dentry)) {
1636 /*
1637 * Both external: swap the pointers
1638 */
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08001639 swap(target->d_name.name, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 } else {
1641 /*
1642 * dentry:internal, target:external. Steal target's
1643 * storage and make target internal.
1644 */
J. Bruce Fields321bcf92007-10-21 16:41:38 -07001645 memcpy(target->d_iname, dentry->d_name.name,
1646 dentry->d_name.len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 dentry->d_name.name = target->d_name.name;
1648 target->d_name.name = target->d_iname;
1649 }
1650 } else {
1651 if (dname_external(dentry)) {
1652 /*
1653 * dentry:external, target:internal. Give dentry's
1654 * storage to target and make dentry internal
1655 */
1656 memcpy(dentry->d_iname, target->d_name.name,
1657 target->d_name.len + 1);
1658 target->d_name.name = dentry->d_name.name;
1659 dentry->d_name.name = dentry->d_iname;
1660 } else {
1661 /*
1662 * Both are internal. Just copy target to dentry
1663 */
1664 memcpy(dentry->d_iname, target->d_name.name,
1665 target->d_name.len + 1);
Al Virodc711ca2008-11-03 15:03:50 -05001666 dentry->d_name.len = target->d_name.len;
1667 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 }
1669 }
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08001670 swap(dentry->d_name.len, target->d_name.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671}
1672
1673/*
1674 * We cannibalize "target" when moving dentry on top of it,
1675 * because it's going to be thrown away anyway. We could be more
1676 * polite about it, though.
1677 *
1678 * This forceful removal will result in ugly /proc output if
1679 * somebody holds a file open that got deleted due to a rename.
1680 * We could be nicer about the deleted file, and let it show
J. Bruce Fieldsbc154b12007-10-16 23:29:42 -07001681 * up under the name it had before it was deleted rather than
1682 * under the original name of the file that was moved on top of it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 */
1684
Trond Myklebust9eaef272006-10-21 10:24:20 -07001685/*
1686 * d_move_locked - move a dentry
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 * @dentry: entry to move
1688 * @target: new dentry
1689 *
1690 * Update the dcache to reflect the move of a file name. Negative
1691 * dcache entries should not be moved in this way.
1692 */
Trond Myklebust9eaef272006-10-21 10:24:20 -07001693static void d_move_locked(struct dentry * dentry, struct dentry * target)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694{
1695 struct hlist_head *list;
1696
1697 if (!dentry->d_inode)
1698 printk(KERN_WARNING "VFS: moving negative dcache entry\n");
1699
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 write_seqlock(&rename_lock);
1701 /*
1702 * XXXX: do we really need to take target->d_lock?
1703 */
1704 if (target < dentry) {
1705 spin_lock(&target->d_lock);
Ingo Molnara90b9c02006-07-03 00:25:04 -07001706 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 } else {
1708 spin_lock(&dentry->d_lock);
Ingo Molnara90b9c02006-07-03 00:25:04 -07001709 spin_lock_nested(&target->d_lock, DENTRY_D_LOCK_NESTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 }
1711
1712 /* Move the dentry to the target hash queue, if on different bucket */
Denis Chengf77e3492007-10-16 23:30:11 -07001713 if (d_unhashed(dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 goto already_unhashed;
1715
1716 hlist_del_rcu(&dentry->d_hash);
1717
1718already_unhashed:
1719 list = d_hash(target->d_parent, target->d_name.hash);
1720 __d_rehash(dentry, list);
1721
1722 /* Unhash the target: dput() will then get rid of it */
1723 __d_drop(target);
1724
Eric Dumazet5160ee62006-01-08 01:03:32 -08001725 list_del(&dentry->d_u.d_child);
1726 list_del(&target->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
1728 /* Switch the names.. */
1729 switch_names(dentry, target);
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08001730 swap(dentry->d_name.hash, target->d_name.hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
1732 /* ... and switch the parents */
1733 if (IS_ROOT(dentry)) {
1734 dentry->d_parent = target->d_parent;
1735 target->d_parent = target;
Eric Dumazet5160ee62006-01-08 01:03:32 -08001736 INIT_LIST_HEAD(&target->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 } else {
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08001738 swap(dentry->d_parent, target->d_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
1740 /* And add them back to the (new) parent lists */
Eric Dumazet5160ee62006-01-08 01:03:32 -08001741 list_add(&target->d_u.d_child, &target->d_parent->d_subdirs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 }
1743
Eric Dumazet5160ee62006-01-08 01:03:32 -08001744 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 spin_unlock(&target->d_lock);
Nick Pigginc32ccd82006-03-25 03:07:09 -08001746 fsnotify_d_move(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 spin_unlock(&dentry->d_lock);
1748 write_sequnlock(&rename_lock);
Trond Myklebust9eaef272006-10-21 10:24:20 -07001749}
1750
1751/**
1752 * d_move - move a dentry
1753 * @dentry: entry to move
1754 * @target: new dentry
1755 *
1756 * Update the dcache to reflect the move of a file name. Negative
1757 * dcache entries should not be moved in this way.
1758 */
1759
1760void d_move(struct dentry * dentry, struct dentry * target)
1761{
1762 spin_lock(&dcache_lock);
1763 d_move_locked(dentry, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 spin_unlock(&dcache_lock);
1765}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001766EXPORT_SYMBOL(d_move);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001768/**
1769 * d_ancestor - search for an ancestor
1770 * @p1: ancestor dentry
1771 * @p2: child dentry
1772 *
1773 * Returns the ancestor dentry of p2 which is a child of p1, if p1 is
1774 * an ancestor of p2, else NULL.
Trond Myklebust9eaef272006-10-21 10:24:20 -07001775 */
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001776struct dentry *d_ancestor(struct dentry *p1, struct dentry *p2)
Trond Myklebust9eaef272006-10-21 10:24:20 -07001777{
1778 struct dentry *p;
1779
OGAWA Hirofumi871c0062008-10-16 07:50:27 +09001780 for (p = p2; !IS_ROOT(p); p = p->d_parent) {
Trond Myklebust9eaef272006-10-21 10:24:20 -07001781 if (p->d_parent == p1)
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001782 return p;
Trond Myklebust9eaef272006-10-21 10:24:20 -07001783 }
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001784 return NULL;
Trond Myklebust9eaef272006-10-21 10:24:20 -07001785}
1786
1787/*
1788 * This helper attempts to cope with remotely renamed directories
1789 *
1790 * It assumes that the caller is already holding
1791 * dentry->d_parent->d_inode->i_mutex and the dcache_lock
1792 *
1793 * Note: If ever the locking in lock_rename() changes, then please
1794 * remember to update this too...
Trond Myklebust9eaef272006-10-21 10:24:20 -07001795 */
1796static struct dentry *__d_unalias(struct dentry *dentry, struct dentry *alias)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +02001797 __releases(dcache_lock)
Trond Myklebust9eaef272006-10-21 10:24:20 -07001798{
1799 struct mutex *m1 = NULL, *m2 = NULL;
1800 struct dentry *ret;
1801
1802 /* If alias and dentry share a parent, then no extra locks required */
1803 if (alias->d_parent == dentry->d_parent)
1804 goto out_unalias;
1805
1806 /* Check for loops */
1807 ret = ERR_PTR(-ELOOP);
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001808 if (d_ancestor(alias, dentry))
Trond Myklebust9eaef272006-10-21 10:24:20 -07001809 goto out_err;
1810
1811 /* See lock_rename() */
1812 ret = ERR_PTR(-EBUSY);
1813 if (!mutex_trylock(&dentry->d_sb->s_vfs_rename_mutex))
1814 goto out_err;
1815 m1 = &dentry->d_sb->s_vfs_rename_mutex;
1816 if (!mutex_trylock(&alias->d_parent->d_inode->i_mutex))
1817 goto out_err;
1818 m2 = &alias->d_parent->d_inode->i_mutex;
1819out_unalias:
1820 d_move_locked(alias, dentry);
1821 ret = alias;
1822out_err:
1823 spin_unlock(&dcache_lock);
1824 if (m2)
1825 mutex_unlock(m2);
1826 if (m1)
1827 mutex_unlock(m1);
1828 return ret;
1829}
1830
1831/*
David Howells770bfad2006-08-22 20:06:07 -04001832 * Prepare an anonymous dentry for life in the superblock's dentry tree as a
1833 * named dentry in place of the dentry to be replaced.
1834 */
1835static void __d_materialise_dentry(struct dentry *dentry, struct dentry *anon)
1836{
1837 struct dentry *dparent, *aparent;
1838
1839 switch_names(dentry, anon);
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08001840 swap(dentry->d_name.hash, anon->d_name.hash);
David Howells770bfad2006-08-22 20:06:07 -04001841
1842 dparent = dentry->d_parent;
1843 aparent = anon->d_parent;
1844
1845 dentry->d_parent = (aparent == anon) ? dentry : aparent;
1846 list_del(&dentry->d_u.d_child);
1847 if (!IS_ROOT(dentry))
1848 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
1849 else
1850 INIT_LIST_HEAD(&dentry->d_u.d_child);
1851
1852 anon->d_parent = (dparent == dentry) ? anon : dparent;
1853 list_del(&anon->d_u.d_child);
1854 if (!IS_ROOT(anon))
1855 list_add(&anon->d_u.d_child, &anon->d_parent->d_subdirs);
1856 else
1857 INIT_LIST_HEAD(&anon->d_u.d_child);
1858
1859 anon->d_flags &= ~DCACHE_DISCONNECTED;
1860}
1861
1862/**
1863 * d_materialise_unique - introduce an inode into the tree
1864 * @dentry: candidate dentry
1865 * @inode: inode to bind to the dentry, to which aliases may be attached
1866 *
1867 * Introduces an dentry into the tree, substituting an extant disconnected
1868 * root directory alias in its place if there is one
1869 */
1870struct dentry *d_materialise_unique(struct dentry *dentry, struct inode *inode)
1871{
Trond Myklebust9eaef272006-10-21 10:24:20 -07001872 struct dentry *actual;
David Howells770bfad2006-08-22 20:06:07 -04001873
1874 BUG_ON(!d_unhashed(dentry));
1875
1876 spin_lock(&dcache_lock);
1877
1878 if (!inode) {
1879 actual = dentry;
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001880 __d_instantiate(dentry, NULL);
David Howells770bfad2006-08-22 20:06:07 -04001881 goto found_lock;
1882 }
1883
Trond Myklebust9eaef272006-10-21 10:24:20 -07001884 if (S_ISDIR(inode->i_mode)) {
1885 struct dentry *alias;
David Howells770bfad2006-08-22 20:06:07 -04001886
Trond Myklebust9eaef272006-10-21 10:24:20 -07001887 /* Does an aliased dentry already exist? */
1888 alias = __d_find_alias(inode, 0);
1889 if (alias) {
1890 actual = alias;
1891 /* Is this an anonymous mountpoint that we could splice
1892 * into our tree? */
1893 if (IS_ROOT(alias)) {
1894 spin_lock(&alias->d_lock);
1895 __d_materialise_dentry(dentry, alias);
1896 __d_drop(alias);
1897 goto found;
1898 }
1899 /* Nope, but we must(!) avoid directory aliasing */
1900 actual = __d_unalias(dentry, alias);
1901 if (IS_ERR(actual))
1902 dput(alias);
1903 goto out_nolock;
1904 }
David Howells770bfad2006-08-22 20:06:07 -04001905 }
1906
1907 /* Add a unique reference */
1908 actual = __d_instantiate_unique(dentry, inode);
1909 if (!actual)
1910 actual = dentry;
1911 else if (unlikely(!d_unhashed(actual)))
1912 goto shouldnt_be_hashed;
1913
1914found_lock:
1915 spin_lock(&actual->d_lock);
1916found:
1917 _d_rehash(actual);
1918 spin_unlock(&actual->d_lock);
1919 spin_unlock(&dcache_lock);
Trond Myklebust9eaef272006-10-21 10:24:20 -07001920out_nolock:
David Howells770bfad2006-08-22 20:06:07 -04001921 if (actual == dentry) {
1922 security_d_instantiate(dentry, inode);
1923 return NULL;
1924 }
1925
1926 iput(inode);
1927 return actual;
1928
David Howells770bfad2006-08-22 20:06:07 -04001929shouldnt_be_hashed:
1930 spin_unlock(&dcache_lock);
1931 BUG();
David Howells770bfad2006-08-22 20:06:07 -04001932}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001933EXPORT_SYMBOL_GPL(d_materialise_unique);
David Howells770bfad2006-08-22 20:06:07 -04001934
Miklos Szeredicdd16d02008-06-23 18:11:53 +02001935static int prepend(char **buffer, int *buflen, const char *str, int namelen)
Ram Pai6092d042008-03-27 13:06:20 +01001936{
1937 *buflen -= namelen;
1938 if (*buflen < 0)
1939 return -ENAMETOOLONG;
1940 *buffer -= namelen;
1941 memcpy(*buffer, str, namelen);
1942 return 0;
1943}
1944
Miklos Szeredicdd16d02008-06-23 18:11:53 +02001945static int prepend_name(char **buffer, int *buflen, struct qstr *name)
1946{
1947 return prepend(buffer, buflen, name->name, name->len);
1948}
1949
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950/**
Miklos Szeredif2eb6572010-08-10 11:41:39 +02001951 * Prepend path string to a buffer
1952 *
1953 * @path: the dentry/vfsmount to report
1954 * @root: root vfsmnt/dentry (may be modified by this function)
1955 * @buffer: pointer to the end of the buffer
1956 * @buflen: pointer to buffer length
1957 *
1958 * Caller holds the dcache_lock.
1959 *
1960 * If path is not reachable from the supplied root, then the value of
1961 * root is changed (without modifying refcounts).
1962 */
1963static int prepend_path(const struct path *path, struct path *root,
1964 char **buffer, int *buflen)
1965{
1966 struct dentry *dentry = path->dentry;
1967 struct vfsmount *vfsmnt = path->mnt;
1968 bool slash = false;
1969 int error = 0;
1970
Nick Piggin99b7db72010-08-18 04:37:39 +10001971 br_read_lock(vfsmount_lock);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02001972 while (dentry != root->dentry || vfsmnt != root->mnt) {
1973 struct dentry * parent;
1974
1975 if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
1976 /* Global root? */
1977 if (vfsmnt->mnt_parent == vfsmnt) {
1978 goto global_root;
1979 }
1980 dentry = vfsmnt->mnt_mountpoint;
1981 vfsmnt = vfsmnt->mnt_parent;
1982 continue;
1983 }
1984 parent = dentry->d_parent;
1985 prefetch(parent);
1986 error = prepend_name(buffer, buflen, &dentry->d_name);
1987 if (!error)
1988 error = prepend(buffer, buflen, "/", 1);
1989 if (error)
1990 break;
1991
1992 slash = true;
1993 dentry = parent;
1994 }
1995
1996out:
1997 if (!error && !slash)
1998 error = prepend(buffer, buflen, "/", 1);
1999
Nick Piggin99b7db72010-08-18 04:37:39 +10002000 br_read_unlock(vfsmount_lock);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002001 return error;
2002
2003global_root:
2004 /*
2005 * Filesystems needing to implement special "root names"
2006 * should do so with ->d_dname()
2007 */
2008 if (IS_ROOT(dentry) &&
2009 (dentry->d_name.len != 1 || dentry->d_name.name[0] != '/')) {
2010 WARN(1, "Root dentry has weird name <%.*s>\n",
2011 (int) dentry->d_name.len, dentry->d_name.name);
2012 }
2013 root->mnt = vfsmnt;
2014 root->dentry = dentry;
2015 goto out;
2016}
2017
2018/**
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +02002019 * __d_path - return the path of a dentry
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002020 * @path: the dentry/vfsmount to report
2021 * @root: root vfsmnt/dentry (may be modified by this function)
Randy Dunlapcd956a12010-08-14 13:05:31 -07002022 * @buf: buffer to return value in
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 * @buflen: buffer length
2024 *
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002025 * Convert a dentry into an ASCII path name.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 *
Arjan van de Ven52afeef2008-12-01 14:35:00 -08002027 * Returns a pointer into the buffer or an error code if the
2028 * path was too long.
Linus Torvalds552ce542007-02-13 12:08:18 -08002029 *
Christoph Hellwigbe148242010-10-10 05:36:21 -04002030 * "buflen" should be positive.
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002031 *
2032 * If path is not reachable from the supplied root, then the value of
2033 * root is changed (without modifying refcounts).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 */
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002035char *__d_path(const struct path *path, struct path *root,
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002036 char *buf, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037{
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002038 char *res = buf + buflen;
2039 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002041 prepend(&res, &buflen, "\0", 1);
Christoph Hellwigbe148242010-10-10 05:36:21 -04002042 spin_lock(&dcache_lock);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002043 error = prepend_path(path, root, &res, &buflen);
Christoph Hellwigbe148242010-10-10 05:36:21 -04002044 spin_unlock(&dcache_lock);
2045
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002046 if (error)
2047 return ERR_PTR(error);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002048 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049}
2050
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002051/*
2052 * same as __d_path but appends "(deleted)" for unlinked files.
2053 */
2054static int path_with_deleted(const struct path *path, struct path *root,
2055 char **buf, int *buflen)
2056{
2057 prepend(buf, buflen, "\0", 1);
2058 if (d_unlinked(path->dentry)) {
2059 int error = prepend(buf, buflen, " (deleted)", 10);
2060 if (error)
2061 return error;
2062 }
2063
2064 return prepend_path(path, root, buf, buflen);
2065}
2066
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002067static int prepend_unreachable(char **buffer, int *buflen)
2068{
2069 return prepend(buffer, buflen, "(unreachable)", 13);
2070}
2071
Jan Bluncka03a8a702008-02-14 19:38:32 -08002072/**
2073 * d_path - return the path of a dentry
Jan Blunckcf28b482008-02-14 19:38:44 -08002074 * @path: path to report
Jan Bluncka03a8a702008-02-14 19:38:32 -08002075 * @buf: buffer to return value in
2076 * @buflen: buffer length
2077 *
2078 * Convert a dentry into an ASCII path name. If the entry has been deleted
2079 * the string " (deleted)" is appended. Note that this is ambiguous.
2080 *
Arjan van de Ven52afeef2008-12-01 14:35:00 -08002081 * Returns a pointer into the buffer or an error code if the path was
2082 * too long. Note: Callers should use the returned pointer, not the passed
2083 * in buffer, to use the name! The implementation often starts at an offset
2084 * into the buffer, and may leave 0 bytes at the start.
Jan Bluncka03a8a702008-02-14 19:38:32 -08002085 *
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +02002086 * "buflen" should be positive.
Jan Bluncka03a8a702008-02-14 19:38:32 -08002087 */
Jan Engelhardt20d4fdc2008-06-09 16:40:36 -07002088char *d_path(const struct path *path, char *buf, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089{
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002090 char *res = buf + buflen;
Jan Blunck6ac08c32008-02-14 19:34:38 -08002091 struct path root;
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002092 struct path tmp;
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002093 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
Eric Dumazetc23fbb62007-05-08 00:26:18 -07002095 /*
2096 * We have various synthetic filesystems that never get mounted. On
2097 * these filesystems dentries are never used for lookup purposes, and
2098 * thus don't need to be hashed. They also don't need a name until a
2099 * user wants to identify the object in /proc/pid/fd/. The little hack
2100 * below allows us to generate a name for these objects on demand:
2101 */
Jan Blunckcf28b482008-02-14 19:38:44 -08002102 if (path->dentry->d_op && path->dentry->d_op->d_dname)
2103 return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
Eric Dumazetc23fbb62007-05-08 00:26:18 -07002104
Miklos Szeredif7ad3c62010-08-10 11:41:36 +02002105 get_fs_root(current->fs, &root);
Linus Torvalds552ce542007-02-13 12:08:18 -08002106 spin_lock(&dcache_lock);
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002107 tmp = root;
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002108 error = path_with_deleted(path, &tmp, &res, &buflen);
2109 if (error)
2110 res = ERR_PTR(error);
Linus Torvalds552ce542007-02-13 12:08:18 -08002111 spin_unlock(&dcache_lock);
Jan Blunck6ac08c32008-02-14 19:34:38 -08002112 path_put(&root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 return res;
2114}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002115EXPORT_SYMBOL(d_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002117/**
2118 * d_path_with_unreachable - return the path of a dentry
2119 * @path: path to report
2120 * @buf: buffer to return value in
2121 * @buflen: buffer length
2122 *
2123 * The difference from d_path() is that this prepends "(unreachable)"
2124 * to paths which are unreachable from the current process' root.
2125 */
2126char *d_path_with_unreachable(const struct path *path, char *buf, int buflen)
2127{
2128 char *res = buf + buflen;
2129 struct path root;
2130 struct path tmp;
2131 int error;
2132
2133 if (path->dentry->d_op && path->dentry->d_op->d_dname)
2134 return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
2135
2136 get_fs_root(current->fs, &root);
2137 spin_lock(&dcache_lock);
2138 tmp = root;
2139 error = path_with_deleted(path, &tmp, &res, &buflen);
2140 if (!error && !path_equal(&tmp, &root))
2141 error = prepend_unreachable(&res, &buflen);
2142 spin_unlock(&dcache_lock);
2143 path_put(&root);
2144 if (error)
2145 res = ERR_PTR(error);
2146
2147 return res;
2148}
2149
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150/*
Eric Dumazetc23fbb62007-05-08 00:26:18 -07002151 * Helper function for dentry_operations.d_dname() members
2152 */
2153char *dynamic_dname(struct dentry *dentry, char *buffer, int buflen,
2154 const char *fmt, ...)
2155{
2156 va_list args;
2157 char temp[64];
2158 int sz;
2159
2160 va_start(args, fmt);
2161 sz = vsnprintf(temp, sizeof(temp), fmt, args) + 1;
2162 va_end(args);
2163
2164 if (sz > sizeof(temp) || sz > buflen)
2165 return ERR_PTR(-ENAMETOOLONG);
2166
2167 buffer += buflen - sz;
2168 return memcpy(buffer, temp, sz);
2169}
2170
2171/*
Ram Pai6092d042008-03-27 13:06:20 +01002172 * Write full pathname from the root of the filesystem into the buffer.
2173 */
Nick Pigginec2447c2011-01-07 17:49:29 +11002174static char *__dentry_path(struct dentry *dentry, char *buf, int buflen)
Ram Pai6092d042008-03-27 13:06:20 +01002175{
2176 char *end = buf + buflen;
2177 char *retval;
2178
Ram Pai6092d042008-03-27 13:06:20 +01002179 prepend(&end, &buflen, "\0", 1);
Ram Pai6092d042008-03-27 13:06:20 +01002180 if (buflen < 1)
2181 goto Elong;
2182 /* Get '/' right */
2183 retval = end-1;
2184 *retval = '/';
2185
Miklos Szeredicdd16d02008-06-23 18:11:53 +02002186 while (!IS_ROOT(dentry)) {
2187 struct dentry *parent = dentry->d_parent;
Ram Pai6092d042008-03-27 13:06:20 +01002188
Ram Pai6092d042008-03-27 13:06:20 +01002189 prefetch(parent);
Miklos Szeredicdd16d02008-06-23 18:11:53 +02002190 if ((prepend_name(&end, &buflen, &dentry->d_name) != 0) ||
Ram Pai6092d042008-03-27 13:06:20 +01002191 (prepend(&end, &buflen, "/", 1) != 0))
2192 goto Elong;
2193
2194 retval = end;
2195 dentry = parent;
2196 }
Al Viroc1031352010-06-06 22:31:14 -04002197 return retval;
2198Elong:
2199 return ERR_PTR(-ENAMETOOLONG);
2200}
Nick Pigginec2447c2011-01-07 17:49:29 +11002201
2202char *dentry_path_raw(struct dentry *dentry, char *buf, int buflen)
2203{
2204 char *retval;
2205
2206 spin_lock(&dcache_lock);
2207 retval = __dentry_path(dentry, buf, buflen);
2208 spin_unlock(&dcache_lock);
2209
2210 return retval;
2211}
2212EXPORT_SYMBOL(dentry_path_raw);
Al Viroc1031352010-06-06 22:31:14 -04002213
2214char *dentry_path(struct dentry *dentry, char *buf, int buflen)
2215{
2216 char *p = NULL;
2217 char *retval;
2218
2219 spin_lock(&dcache_lock);
2220 if (d_unlinked(dentry)) {
2221 p = buf + buflen;
2222 if (prepend(&p, &buflen, "//deleted", 10) != 0)
2223 goto Elong;
2224 buflen++;
2225 }
2226 retval = __dentry_path(dentry, buf, buflen);
Ram Pai6092d042008-03-27 13:06:20 +01002227 spin_unlock(&dcache_lock);
Al Viroc1031352010-06-06 22:31:14 -04002228 if (!IS_ERR(retval) && p)
2229 *p = '/'; /* restore '/' overriden with '\0' */
Ram Pai6092d042008-03-27 13:06:20 +01002230 return retval;
2231Elong:
2232 spin_unlock(&dcache_lock);
2233 return ERR_PTR(-ENAMETOOLONG);
2234}
2235
2236/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 * NOTE! The user-level library version returns a
2238 * character pointer. The kernel system call just
2239 * returns the length of the buffer filled (which
2240 * includes the ending '\0' character), or a negative
2241 * error value. So libc would do something like
2242 *
2243 * char *getcwd(char * buf, size_t size)
2244 * {
2245 * int retval;
2246 *
2247 * retval = sys_getcwd(buf, size);
2248 * if (retval >= 0)
2249 * return buf;
2250 * errno = -retval;
2251 * return NULL;
2252 * }
2253 */
Heiko Carstens3cdad422009-01-14 14:14:22 +01002254SYSCALL_DEFINE2(getcwd, char __user *, buf, unsigned long, size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255{
Linus Torvalds552ce542007-02-13 12:08:18 -08002256 int error;
Jan Blunck6ac08c32008-02-14 19:34:38 -08002257 struct path pwd, root;
Linus Torvalds552ce542007-02-13 12:08:18 -08002258 char *page = (char *) __get_free_page(GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
2260 if (!page)
2261 return -ENOMEM;
2262
Miklos Szeredif7ad3c62010-08-10 11:41:36 +02002263 get_fs_root_and_pwd(current->fs, &root, &pwd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
Linus Torvalds552ce542007-02-13 12:08:18 -08002265 error = -ENOENT;
Linus Torvalds552ce542007-02-13 12:08:18 -08002266 spin_lock(&dcache_lock);
Alexey Dobriyanf3da3922009-05-04 03:32:03 +04002267 if (!d_unlinked(pwd.dentry)) {
Linus Torvalds552ce542007-02-13 12:08:18 -08002268 unsigned long len;
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002269 struct path tmp = root;
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002270 char *cwd = page + PAGE_SIZE;
2271 int buflen = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002273 prepend(&cwd, &buflen, "\0", 1);
2274 error = prepend_path(&pwd, &tmp, &cwd, &buflen);
Linus Torvalds552ce542007-02-13 12:08:18 -08002275 spin_unlock(&dcache_lock);
2276
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002277 if (error)
Linus Torvalds552ce542007-02-13 12:08:18 -08002278 goto out;
2279
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002280 /* Unreachable from current root */
2281 if (!path_equal(&tmp, &root)) {
2282 error = prepend_unreachable(&cwd, &buflen);
2283 if (error)
2284 goto out;
2285 }
2286
Linus Torvalds552ce542007-02-13 12:08:18 -08002287 error = -ERANGE;
2288 len = PAGE_SIZE + page - cwd;
2289 if (len <= size) {
2290 error = len;
2291 if (copy_to_user(buf, cwd, len))
2292 error = -EFAULT;
2293 }
2294 } else
2295 spin_unlock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296
2297out:
Jan Blunck6ac08c32008-02-14 19:34:38 -08002298 path_put(&pwd);
2299 path_put(&root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 free_page((unsigned long) page);
2301 return error;
2302}
2303
2304/*
2305 * Test whether new_dentry is a subdirectory of old_dentry.
2306 *
2307 * Trivially implemented using the dcache structure
2308 */
2309
2310/**
2311 * is_subdir - is new dentry a subdirectory of old_dentry
2312 * @new_dentry: new dentry
2313 * @old_dentry: old dentry
2314 *
2315 * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
2316 * Returns 0 otherwise.
2317 * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
2318 */
2319
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002320int is_subdir(struct dentry *new_dentry, struct dentry *old_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321{
2322 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 unsigned long seq;
2324
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002325 if (new_dentry == old_dentry)
2326 return 1;
2327
2328 /*
2329 * Need rcu_readlock to protect against the d_parent trashing
2330 * due to d_move
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 */
2332 rcu_read_lock();
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002333 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 /* for restarting inner loop in case of seq retry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 seq = read_seqbegin(&rename_lock);
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002336 if (d_ancestor(old_dentry, new_dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 result = 1;
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002338 else
2339 result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 } while (read_seqretry(&rename_lock, seq));
2341 rcu_read_unlock();
2342
2343 return result;
2344}
2345
Al Viro2096f752010-01-30 13:16:21 -05002346int path_is_under(struct path *path1, struct path *path2)
2347{
2348 struct vfsmount *mnt = path1->mnt;
2349 struct dentry *dentry = path1->dentry;
2350 int res;
Nick Piggin99b7db72010-08-18 04:37:39 +10002351
2352 br_read_lock(vfsmount_lock);
Al Viro2096f752010-01-30 13:16:21 -05002353 if (mnt != path2->mnt) {
2354 for (;;) {
2355 if (mnt->mnt_parent == mnt) {
Nick Piggin99b7db72010-08-18 04:37:39 +10002356 br_read_unlock(vfsmount_lock);
Al Viro2096f752010-01-30 13:16:21 -05002357 return 0;
2358 }
2359 if (mnt->mnt_parent == path2->mnt)
2360 break;
2361 mnt = mnt->mnt_parent;
2362 }
2363 dentry = mnt->mnt_mountpoint;
2364 }
2365 res = is_subdir(dentry, path2->dentry);
Nick Piggin99b7db72010-08-18 04:37:39 +10002366 br_read_unlock(vfsmount_lock);
Al Viro2096f752010-01-30 13:16:21 -05002367 return res;
2368}
2369EXPORT_SYMBOL(path_is_under);
2370
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371void d_genocide(struct dentry *root)
2372{
2373 struct dentry *this_parent = root;
2374 struct list_head *next;
2375
2376 spin_lock(&dcache_lock);
2377repeat:
2378 next = this_parent->d_subdirs.next;
2379resume:
2380 while (next != &this_parent->d_subdirs) {
2381 struct list_head *tmp = next;
Eric Dumazet5160ee62006-01-08 01:03:32 -08002382 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 next = tmp->next;
2384 if (d_unhashed(dentry)||!dentry->d_inode)
2385 continue;
2386 if (!list_empty(&dentry->d_subdirs)) {
2387 this_parent = dentry;
2388 goto repeat;
2389 }
2390 atomic_dec(&dentry->d_count);
2391 }
2392 if (this_parent != root) {
Eric Dumazet5160ee62006-01-08 01:03:32 -08002393 next = this_parent->d_u.d_child.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 atomic_dec(&this_parent->d_count);
2395 this_parent = this_parent->d_parent;
2396 goto resume;
2397 }
2398 spin_unlock(&dcache_lock);
2399}
2400
2401/**
2402 * find_inode_number - check for dentry with name
2403 * @dir: directory to check
2404 * @name: Name to find.
2405 *
2406 * Check whether a dentry already exists for the given name,
2407 * and return the inode number if it has an inode. Otherwise
2408 * 0 is returned.
2409 *
2410 * This routine is used to post-process directory listings for
2411 * filesystems using synthetic inode numbers, and is necessary
2412 * to keep getcwd() working.
2413 */
2414
2415ino_t find_inode_number(struct dentry *dir, struct qstr *name)
2416{
2417 struct dentry * dentry;
2418 ino_t ino = 0;
2419
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08002420 dentry = d_hash_and_lookup(dir, name);
2421 if (dentry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 if (dentry->d_inode)
2423 ino = dentry->d_inode->i_ino;
2424 dput(dentry);
2425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 return ino;
2427}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002428EXPORT_SYMBOL(find_inode_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429
2430static __initdata unsigned long dhash_entries;
2431static int __init set_dhash_entries(char *str)
2432{
2433 if (!str)
2434 return 0;
2435 dhash_entries = simple_strtoul(str, &str, 0);
2436 return 1;
2437}
2438__setup("dhash_entries=", set_dhash_entries);
2439
2440static void __init dcache_init_early(void)
2441{
2442 int loop;
2443
2444 /* If hashes are distributed across NUMA nodes, defer
2445 * hash allocation until vmalloc space is available.
2446 */
2447 if (hashdist)
2448 return;
2449
2450 dentry_hashtable =
2451 alloc_large_system_hash("Dentry cache",
2452 sizeof(struct hlist_head),
2453 dhash_entries,
2454 13,
2455 HASH_EARLY,
2456 &d_hash_shift,
2457 &d_hash_mask,
2458 0);
2459
2460 for (loop = 0; loop < (1 << d_hash_shift); loop++)
2461 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
2462}
2463
Denis Cheng74bf17c2007-10-16 23:26:30 -07002464static void __init dcache_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465{
2466 int loop;
2467
2468 /*
2469 * A constructor could be added for stable state like the lists,
2470 * but it is probably not worth it because of the cache nature
2471 * of the dcache.
2472 */
Christoph Lameter0a31bd52007-05-06 14:49:57 -07002473 dentry_cache = KMEM_CACHE(dentry,
2474 SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|SLAB_MEM_SPREAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
Rusty Russell8e1f9362007-07-17 04:03:17 -07002476 register_shrinker(&dcache_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477
2478 /* Hash may have been set up in dcache_init_early */
2479 if (!hashdist)
2480 return;
2481
2482 dentry_hashtable =
2483 alloc_large_system_hash("Dentry cache",
2484 sizeof(struct hlist_head),
2485 dhash_entries,
2486 13,
2487 0,
2488 &d_hash_shift,
2489 &d_hash_mask,
2490 0);
2491
2492 for (loop = 0; loop < (1 << d_hash_shift); loop++)
2493 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
2494}
2495
2496/* SLAB cache for __getname() consumers */
Christoph Lametere18b8902006-12-06 20:33:20 -08002497struct kmem_cache *names_cachep __read_mostly;
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002498EXPORT_SYMBOL(names_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500EXPORT_SYMBOL(d_genocide);
2501
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502void __init vfs_caches_init_early(void)
2503{
2504 dcache_init_early();
2505 inode_init_early();
2506}
2507
2508void __init vfs_caches_init(unsigned long mempages)
2509{
2510 unsigned long reserve;
2511
2512 /* Base hash sizes on available memory, with a reserve equal to
2513 150% of current kernel size */
2514
2515 reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);
2516 mempages -= reserve;
2517
2518 names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09002519 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520
Denis Cheng74bf17c2007-10-16 23:26:30 -07002521 dcache_init();
2522 inode_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 files_init(mempages);
Denis Cheng74bf17c2007-10-16 23:26:30 -07002524 mnt_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 bdev_cache_init();
2526 chrdev_init();
2527}