blob: f62ba90bce91ffaa237502d0f04ac94f8b3a570d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/dcache.c
3 *
4 * Complete reimplementation
5 * (C) 1997 Thomas Schoebel-Theuer,
6 * with heavy changes by Linus Torvalds
7 */
8
9/*
10 * Notes on the allocation strategy:
11 *
12 * The dcache is a master of the icache - whenever a dcache entry
13 * exists, the inode will always exist. "iput()" is done either when
14 * the dcache entry is deleted or garbage collected.
15 */
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/syscalls.h>
18#include <linux/string.h>
19#include <linux/mm.h>
20#include <linux/fs.h>
John McCutchan7a91bf72005-08-08 13:52:16 -040021#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/slab.h>
23#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/hash.h>
25#include <linux/cache.h>
26#include <linux/module.h>
27#include <linux/mount.h>
28#include <linux/file.h>
29#include <asm/uaccess.h>
30#include <linux/security.h>
31#include <linux/seqlock.h>
32#include <linux/swap.h>
33#include <linux/bootmem.h>
Al Viro5ad4e532009-03-29 19:50:06 -040034#include <linux/fs_struct.h>
Frederic Weisbecker613afbf2009-07-16 15:44:29 +020035#include <linux/hardirq.h>
David Howells07f3f052006-09-30 20:52:18 +020036#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Eric Dumazetfa3536c2006-03-26 01:37:24 -080038int sysctl_vfs_cache_pressure __read_mostly = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039EXPORT_SYMBOL_GPL(sysctl_vfs_cache_pressure);
40
41 __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_lock);
Al Viro74c3cbe2007-07-22 08:04:18 -040042__cacheline_aligned_in_smp DEFINE_SEQLOCK(rename_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44EXPORT_SYMBOL(dcache_lock);
45
Christoph Lametere18b8902006-12-06 20:33:20 -080046static struct kmem_cache *dentry_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48#define DNAME_INLINE_LEN (sizeof(struct dentry)-offsetof(struct dentry,d_iname))
49
50/*
51 * This is the single most critical data structure when it comes
52 * to the dcache: the hashtable for lookups. Somebody should try
53 * to make this good - I've just made it work.
54 *
55 * This hash-function tries to avoid losing too many bits of hash
56 * information, yet avoid using a prime hash-size or similar.
57 */
58#define D_HASHBITS d_hash_shift
59#define D_HASHMASK d_hash_mask
60
Eric Dumazetfa3536c2006-03-26 01:37:24 -080061static unsigned int d_hash_mask __read_mostly;
62static unsigned int d_hash_shift __read_mostly;
63static struct hlist_head *dentry_hashtable __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65/* Statistics gathering. */
66struct dentry_stat_t dentry_stat = {
67 .age_limit = 45,
68};
69
Christoph Hellwig312d3ca2010-10-10 05:36:23 -040070static struct percpu_counter nr_dentry __cacheline_aligned_in_smp;
Christoph Hellwig312d3ca2010-10-10 05:36:23 -040071
72#if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS)
73int proc_nr_dentry(ctl_table *table, int write, void __user *buffer,
74 size_t *lenp, loff_t *ppos)
75{
76 dentry_stat.nr_dentry = percpu_counter_sum_positive(&nr_dentry);
Christoph Hellwig312d3ca2010-10-10 05:36:23 -040077 return proc_dointvec(table, write, buffer, lenp, ppos);
78}
79#endif
80
Christoph Hellwig9c82ab92010-10-10 05:36:22 -040081static void __d_free(struct rcu_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
Christoph Hellwig9c82ab92010-10-10 05:36:22 -040083 struct dentry *dentry = container_of(head, struct dentry, d_u.d_rcu);
84
Arjan van de Venfd217f42008-10-21 06:47:33 -070085 WARN_ON(!list_empty(&dentry->d_alias));
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 if (dname_external(dentry))
87 kfree(dentry->d_name.name);
88 kmem_cache_free(dentry_cache, dentry);
89}
90
91/*
Christoph Hellwig312d3ca2010-10-10 05:36:23 -040092 * no dcache_lock, please.
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 */
94static void d_free(struct dentry *dentry)
95{
Christoph Hellwig312d3ca2010-10-10 05:36:23 -040096 percpu_counter_dec(&nr_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 if (dentry->d_op && dentry->d_op->d_release)
98 dentry->d_op->d_release(dentry);
Christoph Hellwig312d3ca2010-10-10 05:36:23 -040099
Eric Dumazetb3423412006-12-06 20:38:48 -0800100 /* if dentry was never inserted into hash, immediate free is OK */
Akinobu Mitae8462ca2008-02-06 01:37:07 -0800101 if (hlist_unhashed(&dentry->d_hash))
Christoph Hellwig9c82ab92010-10-10 05:36:22 -0400102 __d_free(&dentry->d_u.d_rcu);
Eric Dumazetb3423412006-12-06 20:38:48 -0800103 else
Christoph Hellwig9c82ab92010-10-10 05:36:22 -0400104 call_rcu(&dentry->d_u.d_rcu, __d_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
106
107/*
108 * Release the dentry's inode, using the filesystem
109 * d_iput() operation if defined.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800111static void dentry_iput(struct dentry * dentry)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200112 __releases(dentry->d_lock)
113 __releases(dcache_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
115 struct inode *inode = dentry->d_inode;
116 if (inode) {
117 dentry->d_inode = NULL;
118 list_del_init(&dentry->d_alias);
119 spin_unlock(&dentry->d_lock);
120 spin_unlock(&dcache_lock);
Linus Torvaldsf805fbd2005-09-19 19:54:29 -0700121 if (!inode->i_nlink)
122 fsnotify_inoderemove(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 if (dentry->d_op && dentry->d_op->d_iput)
124 dentry->d_op->d_iput(dentry, inode);
125 else
126 iput(inode);
127 } else {
128 spin_unlock(&dentry->d_lock);
129 spin_unlock(&dcache_lock);
130 }
131}
132
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700133/*
Christoph Hellwiga4633352010-10-10 05:36:26 -0400134 * dentry_lru_(add|del|move_tail) must be called with dcache_lock held.
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700135 */
136static void dentry_lru_add(struct dentry *dentry)
137{
Christoph Hellwiga4633352010-10-10 05:36:26 -0400138 if (list_empty(&dentry->d_lru)) {
139 list_add(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
140 dentry->d_sb->s_nr_dentry_unused++;
Nick Piggin86c87492011-01-07 17:49:18 +1100141 dentry_stat.nr_unused++;
Christoph Hellwiga4633352010-10-10 05:36:26 -0400142 }
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700143}
144
145static void dentry_lru_del(struct dentry *dentry)
146{
147 if (!list_empty(&dentry->d_lru)) {
Christoph Hellwiga4633352010-10-10 05:36:26 -0400148 list_del_init(&dentry->d_lru);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700149 dentry->d_sb->s_nr_dentry_unused--;
Nick Piggin86c87492011-01-07 17:49:18 +1100150 dentry_stat.nr_unused--;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700151 }
152}
153
Christoph Hellwiga4633352010-10-10 05:36:26 -0400154static void dentry_lru_move_tail(struct dentry *dentry)
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700155{
Christoph Hellwiga4633352010-10-10 05:36:26 -0400156 if (list_empty(&dentry->d_lru)) {
157 list_add_tail(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
158 dentry->d_sb->s_nr_dentry_unused++;
Nick Piggin86c87492011-01-07 17:49:18 +1100159 dentry_stat.nr_unused++;
Christoph Hellwiga4633352010-10-10 05:36:26 -0400160 } else {
161 list_move_tail(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700162 }
163}
164
Miklos Szeredid52b9082007-05-08 00:23:46 -0700165/**
166 * d_kill - kill dentry and return parent
167 * @dentry: dentry to kill
168 *
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200169 * The dentry must already be unhashed and removed from the LRU.
Miklos Szeredid52b9082007-05-08 00:23:46 -0700170 *
171 * If this is the root of the dentry tree, return NULL.
172 */
173static struct dentry *d_kill(struct dentry *dentry)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200174 __releases(dentry->d_lock)
175 __releases(dcache_lock)
Miklos Szeredid52b9082007-05-08 00:23:46 -0700176{
177 struct dentry *parent;
178
179 list_del(&dentry->d_u.d_child);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700180 /*drops the locks, at that point nobody can reach this dentry */
181 dentry_iput(dentry);
OGAWA Hirofumi871c0062008-10-16 07:50:27 +0900182 if (IS_ROOT(dentry))
183 parent = NULL;
184 else
185 parent = dentry->d_parent;
Miklos Szeredid52b9082007-05-08 00:23:46 -0700186 d_free(dentry);
OGAWA Hirofumi871c0062008-10-16 07:50:27 +0900187 return parent;
Miklos Szeredid52b9082007-05-08 00:23:46 -0700188}
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190/*
191 * This is dput
192 *
193 * This is complicated by the fact that we do not want to put
194 * dentries that are no longer on any hash chain on the unused
195 * list: we'd much rather just get rid of them immediately.
196 *
197 * However, that implies that we have to traverse the dentry
198 * tree upwards to the parents which might _also_ now be
199 * scheduled for deletion (it may have been only waiting for
200 * its last child to go away).
201 *
202 * This tail recursion is done by hand as we don't want to depend
203 * on the compiler to always get this right (gcc generally doesn't).
204 * Real recursion would eat up our stack space.
205 */
206
207/*
208 * dput - release a dentry
209 * @dentry: dentry to release
210 *
211 * Release a dentry. This will drop the usage count and if appropriate
212 * call the dentry unlink method as well as removing it from the queues and
213 * releasing its resources. If the parent dentries were scheduled for release
214 * they too may now get deleted.
215 *
216 * no dcache lock, please.
217 */
218
219void dput(struct dentry *dentry)
220{
221 if (!dentry)
222 return;
223
224repeat:
225 if (atomic_read(&dentry->d_count) == 1)
226 might_sleep();
227 if (!atomic_dec_and_lock(&dentry->d_count, &dcache_lock))
228 return;
229
230 spin_lock(&dentry->d_lock);
231 if (atomic_read(&dentry->d_count)) {
232 spin_unlock(&dentry->d_lock);
233 spin_unlock(&dcache_lock);
234 return;
235 }
236
237 /*
238 * AV: ->d_delete() is _NOT_ allowed to block now.
239 */
240 if (dentry->d_op && dentry->d_op->d_delete) {
241 if (dentry->d_op->d_delete(dentry))
242 goto unhash_it;
243 }
Nick Piggin265ac902010-10-10 05:36:24 -0400244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 /* Unreachable? Get rid of it */
246 if (d_unhashed(dentry))
247 goto kill_it;
Nick Piggin265ac902010-10-10 05:36:24 -0400248
249 /* Otherwise leave it cached and ensure it's on the LRU */
250 dentry->d_flags |= DCACHE_REFERENCED;
Christoph Hellwiga4633352010-10-10 05:36:26 -0400251 dentry_lru_add(dentry);
Nick Piggin265ac902010-10-10 05:36:24 -0400252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 spin_unlock(&dentry->d_lock);
254 spin_unlock(&dcache_lock);
255 return;
256
257unhash_it:
258 __d_drop(dentry);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700259kill_it:
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700260 /* if dentry was on the d_lru list delete it from there */
261 dentry_lru_del(dentry);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700262 dentry = d_kill(dentry);
263 if (dentry)
264 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700266EXPORT_SYMBOL(dput);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268/**
269 * d_invalidate - invalidate a dentry
270 * @dentry: dentry to invalidate
271 *
272 * Try to invalidate the dentry if it turns out to be
273 * possible. If there are other dentries that can be
274 * reached through this one we can't delete it and we
275 * return -EBUSY. On success we return 0.
276 *
277 * no dcache lock.
278 */
279
280int d_invalidate(struct dentry * dentry)
281{
282 /*
283 * If it's already been dropped, return OK.
284 */
285 spin_lock(&dcache_lock);
286 if (d_unhashed(dentry)) {
287 spin_unlock(&dcache_lock);
288 return 0;
289 }
290 /*
291 * Check whether to do a partial shrink_dcache
292 * to get rid of unused child entries.
293 */
294 if (!list_empty(&dentry->d_subdirs)) {
295 spin_unlock(&dcache_lock);
296 shrink_dcache_parent(dentry);
297 spin_lock(&dcache_lock);
298 }
299
300 /*
301 * Somebody else still using it?
302 *
303 * If it's a directory, we can't drop it
304 * for fear of somebody re-populating it
305 * with children (even though dropping it
306 * would make it unreachable from the root,
307 * we might still populate it if it was a
308 * working directory or similar).
309 */
310 spin_lock(&dentry->d_lock);
311 if (atomic_read(&dentry->d_count) > 1) {
312 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
313 spin_unlock(&dentry->d_lock);
314 spin_unlock(&dcache_lock);
315 return -EBUSY;
316 }
317 }
318
319 __d_drop(dentry);
320 spin_unlock(&dentry->d_lock);
321 spin_unlock(&dcache_lock);
322 return 0;
323}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700324EXPORT_SYMBOL(d_invalidate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326/* This should be called _only_ with dcache_lock held */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327static inline struct dentry * __dget_locked(struct dentry *dentry)
328{
329 atomic_inc(&dentry->d_count);
Christoph Hellwiga4633352010-10-10 05:36:26 -0400330 dentry_lru_del(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 return dentry;
332}
333
334struct dentry * dget_locked(struct dentry *dentry)
335{
336 return __dget_locked(dentry);
337}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700338EXPORT_SYMBOL(dget_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340/**
341 * d_find_alias - grab a hashed alias of inode
342 * @inode: inode in question
343 * @want_discon: flag, used by d_splice_alias, to request
344 * that only a DISCONNECTED alias be returned.
345 *
346 * If inode has a hashed alias, or is a directory and has any alias,
347 * acquire the reference to alias and return it. Otherwise return NULL.
348 * Notice that if inode is a directory there can be only one alias and
349 * it can be unhashed only if it has no children, or if it is the root
350 * of a filesystem.
351 *
NeilBrown21c0d8f2006-10-04 02:16:16 -0700352 * If the inode has an IS_ROOT, DCACHE_DISCONNECTED alias, then prefer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 * any other hashed alias over that one unless @want_discon is set,
NeilBrown21c0d8f2006-10-04 02:16:16 -0700354 * in which case only return an IS_ROOT, DCACHE_DISCONNECTED alias.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 */
356
357static struct dentry * __d_find_alias(struct inode *inode, int want_discon)
358{
359 struct list_head *head, *next, *tmp;
360 struct dentry *alias, *discon_alias=NULL;
361
362 head = &inode->i_dentry;
363 next = inode->i_dentry.next;
364 while (next != head) {
365 tmp = next;
366 next = tmp->next;
367 prefetch(next);
368 alias = list_entry(tmp, struct dentry, d_alias);
369 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
NeilBrown21c0d8f2006-10-04 02:16:16 -0700370 if (IS_ROOT(alias) &&
371 (alias->d_flags & DCACHE_DISCONNECTED))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 discon_alias = alias;
373 else if (!want_discon) {
374 __dget_locked(alias);
375 return alias;
376 }
377 }
378 }
379 if (discon_alias)
380 __dget_locked(discon_alias);
381 return discon_alias;
382}
383
384struct dentry * d_find_alias(struct inode *inode)
385{
David Howells214fda12006-03-25 03:06:36 -0800386 struct dentry *de = NULL;
387
388 if (!list_empty(&inode->i_dentry)) {
389 spin_lock(&dcache_lock);
390 de = __d_find_alias(inode, 0);
391 spin_unlock(&dcache_lock);
392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 return de;
394}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700395EXPORT_SYMBOL(d_find_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397/*
398 * Try to kill dentries associated with this inode.
399 * WARNING: you must own a reference to inode.
400 */
401void d_prune_aliases(struct inode *inode)
402{
Domen Puncer0cdca3f2005-09-10 00:27:07 -0700403 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404restart:
405 spin_lock(&dcache_lock);
Domen Puncer0cdca3f2005-09-10 00:27:07 -0700406 list_for_each_entry(dentry, &inode->i_dentry, d_alias) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 spin_lock(&dentry->d_lock);
408 if (!atomic_read(&dentry->d_count)) {
409 __dget_locked(dentry);
410 __d_drop(dentry);
411 spin_unlock(&dentry->d_lock);
412 spin_unlock(&dcache_lock);
413 dput(dentry);
414 goto restart;
415 }
416 spin_unlock(&dentry->d_lock);
417 }
418 spin_unlock(&dcache_lock);
419}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700420EXPORT_SYMBOL(d_prune_aliases);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422/*
Andrew Mortond702ccb2006-06-22 14:47:31 -0700423 * Throw away a dentry - free the inode, dput the parent. This requires that
424 * the LRU list has already been removed.
425 *
Miklos Szeredi85864e12007-10-16 23:27:09 -0700426 * Try to prune ancestors as well. This is necessary to prevent
427 * quadratic behavior of shrink_dcache_parent(), but is also expected
428 * to be beneficial in reducing dentry cache fragmentation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 */
Miklos Szeredi85864e12007-10-16 23:27:09 -0700430static void prune_one_dentry(struct dentry * dentry)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200431 __releases(dentry->d_lock)
432 __releases(dcache_lock)
433 __acquires(dcache_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 __d_drop(dentry);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700436 dentry = d_kill(dentry);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700437
438 /*
439 * Prune ancestors. Locking is simpler than in dput(),
440 * because dcache_lock needs to be taken anyway.
441 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 spin_lock(&dcache_lock);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700443 while (dentry) {
444 if (!atomic_dec_and_lock(&dentry->d_count, &dentry->d_lock))
445 return;
446
447 if (dentry->d_op && dentry->d_op->d_delete)
448 dentry->d_op->d_delete(dentry);
Christoph Hellwiga4633352010-10-10 05:36:26 -0400449 dentry_lru_del(dentry);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700450 __d_drop(dentry);
451 dentry = d_kill(dentry);
452 spin_lock(&dcache_lock);
453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
455
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400456static void shrink_dentry_list(struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700458 struct dentry *dentry;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700459
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400460 while (!list_empty(list)) {
461 dentry = list_entry(list->prev, struct dentry, d_lru);
Christoph Hellwiga4633352010-10-10 05:36:26 -0400462 dentry_lru_del(dentry);
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 /*
465 * We found an inuse dentry which was not removed from
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700466 * the LRU because of laziness during lookup. Do not free
467 * it - just keep it off the LRU list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 */
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400469 spin_lock(&dentry->d_lock);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700470 if (atomic_read(&dentry->d_count)) {
471 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 continue;
473 }
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700474 prune_one_dentry(dentry);
475 /* dentry->d_lock was dropped in prune_one_dentry() */
476 cond_resched_lock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400478}
479
480/**
481 * __shrink_dcache_sb - shrink the dentry LRU on a given superblock
482 * @sb: superblock to shrink dentry LRU.
483 * @count: number of entries to prune
484 * @flags: flags to control the dentry processing
485 *
486 * If flags contains DCACHE_REFERENCED reference dentries will not be pruned.
487 */
488static void __shrink_dcache_sb(struct super_block *sb, int *count, int flags)
489{
490 /* called from prune_dcache() and shrink_dcache_parent() */
491 struct dentry *dentry;
492 LIST_HEAD(referenced);
493 LIST_HEAD(tmp);
494 int cnt = *count;
495
496 spin_lock(&dcache_lock);
497 while (!list_empty(&sb->s_dentry_lru)) {
498 dentry = list_entry(sb->s_dentry_lru.prev,
499 struct dentry, d_lru);
500 BUG_ON(dentry->d_sb != sb);
501
502 /*
503 * If we are honouring the DCACHE_REFERENCED flag and the
504 * dentry has this flag set, don't free it. Clear the flag
505 * and put it back on the LRU.
506 */
507 if (flags & DCACHE_REFERENCED) {
508 spin_lock(&dentry->d_lock);
509 if (dentry->d_flags & DCACHE_REFERENCED) {
510 dentry->d_flags &= ~DCACHE_REFERENCED;
511 list_move(&dentry->d_lru, &referenced);
512 spin_unlock(&dentry->d_lock);
513 cond_resched_lock(&dcache_lock);
514 continue;
515 }
516 spin_unlock(&dentry->d_lock);
517 }
518
519 list_move_tail(&dentry->d_lru, &tmp);
520 if (!--cnt)
521 break;
522 cond_resched_lock(&dcache_lock);
523 }
524
525 *count = cnt;
526 shrink_dentry_list(&tmp);
527
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700528 if (!list_empty(&referenced))
529 list_splice(&referenced, &sb->s_dentry_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 spin_unlock(&dcache_lock);
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532}
533
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700534/**
535 * prune_dcache - shrink the dcache
536 * @count: number of entries to try to free
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 *
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700538 * Shrink the dcache. This is done when we need more memory, or simply when we
539 * need to unmount something (at which point we need to unuse all dentries).
540 *
541 * This function may fail to free any resources if all the dentries are in use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 */
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700543static void prune_dcache(int count)
544{
Al Virodca33252010-07-25 02:31:46 +0400545 struct super_block *sb, *p = NULL;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700546 int w_count;
Nick Piggin86c87492011-01-07 17:49:18 +1100547 int unused = dentry_stat.nr_unused;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700548 int prune_ratio;
549 int pruned;
550
551 if (unused == 0 || count == 0)
552 return;
553 spin_lock(&dcache_lock);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700554 if (count >= unused)
555 prune_ratio = 1;
556 else
557 prune_ratio = unused / count;
558 spin_lock(&sb_lock);
Al Virodca33252010-07-25 02:31:46 +0400559 list_for_each_entry(sb, &super_blocks, s_list) {
Al Viro551de6f2010-03-22 19:36:35 -0400560 if (list_empty(&sb->s_instances))
561 continue;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700562 if (sb->s_nr_dentry_unused == 0)
563 continue;
564 sb->s_count++;
565 /* Now, we reclaim unused dentrins with fairness.
566 * We reclaim them same percentage from each superblock.
567 * We calculate number of dentries to scan on this sb
568 * as follows, but the implementation is arranged to avoid
569 * overflows:
570 * number of dentries to scan on this sb =
571 * count * (number of dentries on this sb /
572 * number of dentries in the machine)
573 */
574 spin_unlock(&sb_lock);
575 if (prune_ratio != 1)
576 w_count = (sb->s_nr_dentry_unused / prune_ratio) + 1;
577 else
578 w_count = sb->s_nr_dentry_unused;
579 pruned = w_count;
580 /*
581 * We need to be sure this filesystem isn't being unmounted,
582 * otherwise we could race with generic_shutdown_super(), and
583 * end up holding a reference to an inode while the filesystem
584 * is unmounted. So we try to get s_umount, and make sure
585 * s_root isn't NULL.
586 */
587 if (down_read_trylock(&sb->s_umount)) {
588 if ((sb->s_root != NULL) &&
589 (!list_empty(&sb->s_dentry_lru))) {
590 spin_unlock(&dcache_lock);
591 __shrink_dcache_sb(sb, &w_count,
592 DCACHE_REFERENCED);
593 pruned -= w_count;
594 spin_lock(&dcache_lock);
595 }
596 up_read(&sb->s_umount);
597 }
598 spin_lock(&sb_lock);
Al Virodca33252010-07-25 02:31:46 +0400599 if (p)
600 __put_super(p);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700601 count -= pruned;
Al Virodca33252010-07-25 02:31:46 +0400602 p = sb;
Al Viro79893c12010-03-22 20:27:55 -0400603 /* more work left to do? */
604 if (count <= 0)
605 break;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700606 }
Al Virodca33252010-07-25 02:31:46 +0400607 if (p)
608 __put_super(p);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700609 spin_unlock(&sb_lock);
610 spin_unlock(&dcache_lock);
611}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613/**
614 * shrink_dcache_sb - shrink dcache for a superblock
615 * @sb: superblock
616 *
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400617 * Shrink the dcache for the specified super block. This is used to free
618 * the dcache before unmounting a file system.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 */
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400620void shrink_dcache_sb(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400622 LIST_HEAD(tmp);
623
624 spin_lock(&dcache_lock);
625 while (!list_empty(&sb->s_dentry_lru)) {
626 list_splice_init(&sb->s_dentry_lru, &tmp);
627 shrink_dentry_list(&tmp);
628 }
629 spin_unlock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700631EXPORT_SYMBOL(shrink_dcache_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633/*
David Howellsc636ebd2006-10-11 01:22:19 -0700634 * destroy a single subtree of dentries for unmount
635 * - see the comments on shrink_dcache_for_umount() for a description of the
636 * locking
637 */
638static void shrink_dcache_for_umount_subtree(struct dentry *dentry)
639{
640 struct dentry *parent;
David Howellsf8713572006-10-28 10:38:46 -0700641 unsigned detached = 0;
David Howellsc636ebd2006-10-11 01:22:19 -0700642
643 BUG_ON(!IS_ROOT(dentry));
644
645 /* detach this root from the system */
646 spin_lock(&dcache_lock);
Christoph Hellwiga4633352010-10-10 05:36:26 -0400647 dentry_lru_del(dentry);
David Howellsc636ebd2006-10-11 01:22:19 -0700648 __d_drop(dentry);
649 spin_unlock(&dcache_lock);
650
651 for (;;) {
652 /* descend to the first leaf in the current subtree */
653 while (!list_empty(&dentry->d_subdirs)) {
654 struct dentry *loop;
655
656 /* this is a branch with children - detach all of them
657 * from the system in one go */
658 spin_lock(&dcache_lock);
659 list_for_each_entry(loop, &dentry->d_subdirs,
660 d_u.d_child) {
Christoph Hellwiga4633352010-10-10 05:36:26 -0400661 dentry_lru_del(loop);
David Howellsc636ebd2006-10-11 01:22:19 -0700662 __d_drop(loop);
663 cond_resched_lock(&dcache_lock);
664 }
665 spin_unlock(&dcache_lock);
666
667 /* move to the first child */
668 dentry = list_entry(dentry->d_subdirs.next,
669 struct dentry, d_u.d_child);
670 }
671
672 /* consume the dentries from this leaf up through its parents
673 * until we find one with children or run out altogether */
674 do {
675 struct inode *inode;
676
677 if (atomic_read(&dentry->d_count) != 0) {
678 printk(KERN_ERR
679 "BUG: Dentry %p{i=%lx,n=%s}"
680 " still in use (%d)"
681 " [unmount of %s %s]\n",
682 dentry,
683 dentry->d_inode ?
684 dentry->d_inode->i_ino : 0UL,
685 dentry->d_name.name,
686 atomic_read(&dentry->d_count),
687 dentry->d_sb->s_type->name,
688 dentry->d_sb->s_id);
689 BUG();
690 }
691
OGAWA Hirofumi871c0062008-10-16 07:50:27 +0900692 if (IS_ROOT(dentry))
David Howellsc636ebd2006-10-11 01:22:19 -0700693 parent = NULL;
OGAWA Hirofumi871c0062008-10-16 07:50:27 +0900694 else {
695 parent = dentry->d_parent;
David Howellsc636ebd2006-10-11 01:22:19 -0700696 atomic_dec(&parent->d_count);
OGAWA Hirofumi871c0062008-10-16 07:50:27 +0900697 }
David Howellsc636ebd2006-10-11 01:22:19 -0700698
699 list_del(&dentry->d_u.d_child);
David Howellsf8713572006-10-28 10:38:46 -0700700 detached++;
David Howellsc636ebd2006-10-11 01:22:19 -0700701
702 inode = dentry->d_inode;
703 if (inode) {
704 dentry->d_inode = NULL;
705 list_del_init(&dentry->d_alias);
706 if (dentry->d_op && dentry->d_op->d_iput)
707 dentry->d_op->d_iput(dentry, inode);
708 else
709 iput(inode);
710 }
711
712 d_free(dentry);
713
714 /* finished when we fall off the top of the tree,
715 * otherwise we ascend to the parent and move to the
716 * next sibling if there is one */
717 if (!parent)
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400718 return;
David Howellsc636ebd2006-10-11 01:22:19 -0700719 dentry = parent;
David Howellsc636ebd2006-10-11 01:22:19 -0700720 } while (list_empty(&dentry->d_subdirs));
721
722 dentry = list_entry(dentry->d_subdirs.next,
723 struct dentry, d_u.d_child);
724 }
725}
726
727/*
728 * destroy the dentries attached to a superblock on unmounting
729 * - we don't need to use dentry->d_lock, and only need dcache_lock when
730 * removing the dentry from the system lists and hashes because:
731 * - the superblock is detached from all mountings and open files, so the
732 * dentry trees will not be rearranged by the VFS
733 * - s_umount is write-locked, so the memory pressure shrinker will ignore
734 * any dentries belonging to this superblock that it comes across
735 * - the filesystem itself is no longer permitted to rearrange the dentries
736 * in this superblock
737 */
738void shrink_dcache_for_umount(struct super_block *sb)
739{
740 struct dentry *dentry;
741
742 if (down_read_trylock(&sb->s_umount))
743 BUG();
744
745 dentry = sb->s_root;
746 sb->s_root = NULL;
747 atomic_dec(&dentry->d_count);
748 shrink_dcache_for_umount_subtree(dentry);
749
750 while (!hlist_empty(&sb->s_anon)) {
751 dentry = hlist_entry(sb->s_anon.first, struct dentry, d_hash);
752 shrink_dcache_for_umount_subtree(dentry);
753 }
754}
755
756/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 * Search for at least 1 mount point in the dentry's subdirs.
758 * We descend to the next level whenever the d_subdirs
759 * list is non-empty and continue searching.
760 */
761
762/**
763 * have_submounts - check for mounts over a dentry
764 * @parent: dentry to check.
765 *
766 * Return true if the parent or its subdirectories contain
767 * a mount point
768 */
769
770int have_submounts(struct dentry *parent)
771{
772 struct dentry *this_parent = parent;
773 struct list_head *next;
774
775 spin_lock(&dcache_lock);
776 if (d_mountpoint(parent))
777 goto positive;
778repeat:
779 next = this_parent->d_subdirs.next;
780resume:
781 while (next != &this_parent->d_subdirs) {
782 struct list_head *tmp = next;
Eric Dumazet5160ee62006-01-08 01:03:32 -0800783 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 next = tmp->next;
785 /* Have we found a mount point ? */
786 if (d_mountpoint(dentry))
787 goto positive;
788 if (!list_empty(&dentry->d_subdirs)) {
789 this_parent = dentry;
790 goto repeat;
791 }
792 }
793 /*
794 * All done at this level ... ascend and resume the search.
795 */
796 if (this_parent != parent) {
Eric Dumazet5160ee62006-01-08 01:03:32 -0800797 next = this_parent->d_u.d_child.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 this_parent = this_parent->d_parent;
799 goto resume;
800 }
801 spin_unlock(&dcache_lock);
802 return 0; /* No mount points found in tree */
803positive:
804 spin_unlock(&dcache_lock);
805 return 1;
806}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700807EXPORT_SYMBOL(have_submounts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809/*
810 * Search the dentry child list for the specified parent,
811 * and move any unused dentries to the end of the unused
812 * list for prune_dcache(). We descend to the next level
813 * whenever the d_subdirs list is non-empty and continue
814 * searching.
815 *
816 * It returns zero iff there are no unused children,
817 * otherwise it returns the number of children moved to
818 * the end of the unused list. This may not be the total
819 * number of unused children, because select_parent can
820 * drop the lock and return early due to latency
821 * constraints.
822 */
823static int select_parent(struct dentry * parent)
824{
825 struct dentry *this_parent = parent;
826 struct list_head *next;
827 int found = 0;
828
829 spin_lock(&dcache_lock);
830repeat:
831 next = this_parent->d_subdirs.next;
832resume:
833 while (next != &this_parent->d_subdirs) {
834 struct list_head *tmp = next;
Eric Dumazet5160ee62006-01-08 01:03:32 -0800835 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 next = tmp->next;
837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 /*
839 * move only zero ref count dentries to the end
840 * of the unused list for prune_dcache
841 */
842 if (!atomic_read(&dentry->d_count)) {
Christoph Hellwiga4633352010-10-10 05:36:26 -0400843 dentry_lru_move_tail(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 found++;
Christoph Hellwiga4633352010-10-10 05:36:26 -0400845 } else {
846 dentry_lru_del(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 }
848
849 /*
850 * We can return to the caller if we have found some (this
851 * ensures forward progress). We'll be coming back to find
852 * the rest.
853 */
854 if (found && need_resched())
855 goto out;
856
857 /*
858 * Descend a level if the d_subdirs list is non-empty.
859 */
860 if (!list_empty(&dentry->d_subdirs)) {
861 this_parent = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 goto repeat;
863 }
864 }
865 /*
866 * All done at this level ... ascend and resume the search.
867 */
868 if (this_parent != parent) {
Eric Dumazet5160ee62006-01-08 01:03:32 -0800869 next = this_parent->d_u.d_child.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 this_parent = this_parent->d_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 goto resume;
872 }
873out:
874 spin_unlock(&dcache_lock);
875 return found;
876}
877
878/**
879 * shrink_dcache_parent - prune dcache
880 * @parent: parent of entries to prune
881 *
882 * Prune the dcache to remove unused children of the parent dentry.
883 */
884
885void shrink_dcache_parent(struct dentry * parent)
886{
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700887 struct super_block *sb = parent->d_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 int found;
889
890 while ((found = select_parent(parent)) != 0)
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700891 __shrink_dcache_sb(sb, &found, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700893EXPORT_SYMBOL(shrink_dcache_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895/*
896 * Scan `nr' dentries and return the number which remain.
897 *
898 * We need to avoid reentering the filesystem if the caller is performing a
899 * GFP_NOFS allocation attempt. One example deadlock is:
900 *
901 * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache->
902 * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->put_inode->
903 * ext2_discard_prealloc->ext2_free_blocks->lock_super->DEADLOCK.
904 *
905 * In this case we return -1 to tell the caller that we baled.
906 */
Dave Chinner7f8275d2010-07-19 14:56:17 +1000907static int shrink_dcache_memory(struct shrinker *shrink, int nr, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
909 if (nr) {
910 if (!(gfp_mask & __GFP_FS))
911 return -1;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700912 prune_dcache(nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 }
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400914
Nick Piggin86c87492011-01-07 17:49:18 +1100915 return (dentry_stat.nr_unused / 100) * sysctl_vfs_cache_pressure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916}
917
Rusty Russell8e1f9362007-07-17 04:03:17 -0700918static struct shrinker dcache_shrinker = {
919 .shrink = shrink_dcache_memory,
920 .seeks = DEFAULT_SEEKS,
921};
922
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923/**
924 * d_alloc - allocate a dcache entry
925 * @parent: parent of entry to allocate
926 * @name: qstr of the name
927 *
928 * Allocates a dentry. It returns %NULL if there is insufficient memory
929 * available. On a success the dentry is returned. The name passed in is
930 * copied and the copy passed in may be reused after this call.
931 */
932
933struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
934{
935 struct dentry *dentry;
936 char *dname;
937
Mel Gormane12ba742007-10-16 01:25:52 -0700938 dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 if (!dentry)
940 return NULL;
941
942 if (name->len > DNAME_INLINE_LEN-1) {
943 dname = kmalloc(name->len + 1, GFP_KERNEL);
944 if (!dname) {
945 kmem_cache_free(dentry_cache, dentry);
946 return NULL;
947 }
948 } else {
949 dname = dentry->d_iname;
950 }
951 dentry->d_name.name = dname;
952
953 dentry->d_name.len = name->len;
954 dentry->d_name.hash = name->hash;
955 memcpy(dname, name->name, name->len);
956 dname[name->len] = 0;
957
958 atomic_set(&dentry->d_count, 1);
959 dentry->d_flags = DCACHE_UNHASHED;
960 spin_lock_init(&dentry->d_lock);
961 dentry->d_inode = NULL;
962 dentry->d_parent = NULL;
963 dentry->d_sb = NULL;
964 dentry->d_op = NULL;
965 dentry->d_fsdata = NULL;
966 dentry->d_mounted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 INIT_HLIST_NODE(&dentry->d_hash);
968 INIT_LIST_HEAD(&dentry->d_lru);
969 INIT_LIST_HEAD(&dentry->d_subdirs);
970 INIT_LIST_HEAD(&dentry->d_alias);
971
972 if (parent) {
973 dentry->d_parent = dget(parent);
974 dentry->d_sb = parent->d_sb;
975 } else {
Eric Dumazet5160ee62006-01-08 01:03:32 -0800976 INIT_LIST_HEAD(&dentry->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 }
978
979 spin_lock(&dcache_lock);
980 if (parent)
Eric Dumazet5160ee62006-01-08 01:03:32 -0800981 list_add(&dentry->d_u.d_child, &parent->d_subdirs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 spin_unlock(&dcache_lock);
983
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400984 percpu_counter_inc(&nr_dentry);
985
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 return dentry;
987}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700988EXPORT_SYMBOL(d_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
990struct dentry *d_alloc_name(struct dentry *parent, const char *name)
991{
992 struct qstr q;
993
994 q.name = name;
995 q.len = strlen(name);
996 q.hash = full_name_hash(q.name, q.len);
997 return d_alloc(parent, &q);
998}
H Hartley Sweetenef26ca92009-09-29 20:09:42 -0400999EXPORT_SYMBOL(d_alloc_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001001/* the caller must hold dcache_lock */
1002static void __d_instantiate(struct dentry *dentry, struct inode *inode)
1003{
1004 if (inode)
1005 list_add(&dentry->d_alias, &inode->i_dentry);
1006 dentry->d_inode = inode;
1007 fsnotify_d_instantiate(dentry, inode);
1008}
1009
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010/**
1011 * d_instantiate - fill in inode information for a dentry
1012 * @entry: dentry to complete
1013 * @inode: inode to attach to this dentry
1014 *
1015 * Fill in inode information in the entry.
1016 *
1017 * This turns negative dentries into productive full members
1018 * of society.
1019 *
1020 * NOTE! This assumes that the inode count has been incremented
1021 * (or otherwise set) by the caller to indicate that it is now
1022 * in use by the dcache.
1023 */
1024
1025void d_instantiate(struct dentry *entry, struct inode * inode)
1026{
Eric Sesterhenn28133c72006-03-26 18:25:39 +02001027 BUG_ON(!list_empty(&entry->d_alias));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 spin_lock(&dcache_lock);
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001029 __d_instantiate(entry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 spin_unlock(&dcache_lock);
1031 security_d_instantiate(entry, inode);
1032}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001033EXPORT_SYMBOL(d_instantiate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
1035/**
1036 * d_instantiate_unique - instantiate a non-aliased dentry
1037 * @entry: dentry to instantiate
1038 * @inode: inode to attach to this dentry
1039 *
1040 * Fill in inode information in the entry. On success, it returns NULL.
1041 * If an unhashed alias of "entry" already exists, then we return the
Oleg Drokine866cfa2006-01-09 20:52:51 -08001042 * aliased dentry instead and drop one reference to inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 *
1044 * Note that in order to avoid conflicts with rename() etc, the caller
1045 * had better be holding the parent directory semaphore.
Oleg Drokine866cfa2006-01-09 20:52:51 -08001046 *
1047 * This also assumes that the inode count has been incremented
1048 * (or otherwise set) by the caller to indicate that it is now
1049 * in use by the dcache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 */
David Howells770bfad2006-08-22 20:06:07 -04001051static struct dentry *__d_instantiate_unique(struct dentry *entry,
1052 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053{
1054 struct dentry *alias;
1055 int len = entry->d_name.len;
1056 const char *name = entry->d_name.name;
1057 unsigned int hash = entry->d_name.hash;
1058
David Howells770bfad2006-08-22 20:06:07 -04001059 if (!inode) {
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001060 __d_instantiate(entry, NULL);
David Howells770bfad2006-08-22 20:06:07 -04001061 return NULL;
1062 }
1063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 list_for_each_entry(alias, &inode->i_dentry, d_alias) {
1065 struct qstr *qstr = &alias->d_name;
1066
1067 if (qstr->hash != hash)
1068 continue;
1069 if (alias->d_parent != entry->d_parent)
1070 continue;
1071 if (qstr->len != len)
1072 continue;
1073 if (memcmp(qstr->name, name, len))
1074 continue;
1075 dget_locked(alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 return alias;
1077 }
David Howells770bfad2006-08-22 20:06:07 -04001078
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001079 __d_instantiate(entry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 return NULL;
1081}
David Howells770bfad2006-08-22 20:06:07 -04001082
1083struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode)
1084{
1085 struct dentry *result;
1086
1087 BUG_ON(!list_empty(&entry->d_alias));
1088
1089 spin_lock(&dcache_lock);
1090 result = __d_instantiate_unique(entry, inode);
1091 spin_unlock(&dcache_lock);
1092
1093 if (!result) {
1094 security_d_instantiate(entry, inode);
1095 return NULL;
1096 }
1097
1098 BUG_ON(!d_unhashed(result));
1099 iput(inode);
1100 return result;
1101}
1102
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103EXPORT_SYMBOL(d_instantiate_unique);
1104
1105/**
1106 * d_alloc_root - allocate root dentry
1107 * @root_inode: inode to allocate the root for
1108 *
1109 * Allocate a root ("/") dentry for the inode given. The inode is
1110 * instantiated and returned. %NULL is returned if there is insufficient
1111 * memory or the inode passed is %NULL.
1112 */
1113
1114struct dentry * d_alloc_root(struct inode * root_inode)
1115{
1116 struct dentry *res = NULL;
1117
1118 if (root_inode) {
1119 static const struct qstr name = { .name = "/", .len = 1 };
1120
1121 res = d_alloc(NULL, &name);
1122 if (res) {
1123 res->d_sb = root_inode->i_sb;
1124 res->d_parent = res;
1125 d_instantiate(res, root_inode);
1126 }
1127 }
1128 return res;
1129}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001130EXPORT_SYMBOL(d_alloc_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
1132static inline struct hlist_head *d_hash(struct dentry *parent,
1133 unsigned long hash)
1134{
1135 hash += ((unsigned long) parent ^ GOLDEN_RATIO_PRIME) / L1_CACHE_BYTES;
1136 hash = hash ^ ((hash ^ GOLDEN_RATIO_PRIME) >> D_HASHBITS);
1137 return dentry_hashtable + (hash & D_HASHMASK);
1138}
1139
1140/**
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001141 * d_obtain_alias - find or allocate a dentry for a given inode
1142 * @inode: inode to allocate the dentry for
1143 *
1144 * Obtain a dentry for an inode resulting from NFS filehandle conversion or
1145 * similar open by handle operations. The returned dentry may be anonymous,
1146 * or may have a full name (if the inode was already in the cache).
1147 *
1148 * When called on a directory inode, we must ensure that the inode only ever
1149 * has one dentry. If a dentry is found, that is returned instead of
1150 * allocating a new one.
1151 *
1152 * On successful return, the reference to the inode has been transferred
Christoph Hellwig44003722008-08-11 15:49:04 +02001153 * to the dentry. In case of an error the reference on the inode is released.
1154 * To make it easier to use in export operations a %NULL or IS_ERR inode may
1155 * be passed in and will be the error will be propagate to the return value,
1156 * with a %NULL @inode replaced by ERR_PTR(-ESTALE).
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001157 */
1158struct dentry *d_obtain_alias(struct inode *inode)
1159{
Christoph Hellwig9308a612008-08-11 15:49:12 +02001160 static const struct qstr anonstring = { .name = "" };
1161 struct dentry *tmp;
1162 struct dentry *res;
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001163
1164 if (!inode)
Christoph Hellwig44003722008-08-11 15:49:04 +02001165 return ERR_PTR(-ESTALE);
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001166 if (IS_ERR(inode))
1167 return ERR_CAST(inode);
1168
Christoph Hellwig9308a612008-08-11 15:49:12 +02001169 res = d_find_alias(inode);
1170 if (res)
1171 goto out_iput;
1172
1173 tmp = d_alloc(NULL, &anonstring);
1174 if (!tmp) {
1175 res = ERR_PTR(-ENOMEM);
1176 goto out_iput;
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001177 }
Christoph Hellwig9308a612008-08-11 15:49:12 +02001178 tmp->d_parent = tmp; /* make sure dput doesn't croak */
1179
1180 spin_lock(&dcache_lock);
1181 res = __d_find_alias(inode, 0);
1182 if (res) {
1183 spin_unlock(&dcache_lock);
1184 dput(tmp);
1185 goto out_iput;
1186 }
1187
1188 /* attach a disconnected dentry */
1189 spin_lock(&tmp->d_lock);
1190 tmp->d_sb = inode->i_sb;
1191 tmp->d_inode = inode;
1192 tmp->d_flags |= DCACHE_DISCONNECTED;
1193 tmp->d_flags &= ~DCACHE_UNHASHED;
1194 list_add(&tmp->d_alias, &inode->i_dentry);
1195 hlist_add_head(&tmp->d_hash, &inode->i_sb->s_anon);
1196 spin_unlock(&tmp->d_lock);
1197
1198 spin_unlock(&dcache_lock);
1199 return tmp;
1200
1201 out_iput:
1202 iput(inode);
1203 return res;
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001204}
Benny Halevyadc48722009-02-27 14:02:59 -08001205EXPORT_SYMBOL(d_obtain_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
1207/**
1208 * d_splice_alias - splice a disconnected dentry into the tree if one exists
1209 * @inode: the inode which may have a disconnected dentry
1210 * @dentry: a negative dentry which we want to point to the inode.
1211 *
1212 * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
1213 * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
1214 * and return it, else simply d_add the inode to the dentry and return NULL.
1215 *
1216 * This is needed in the lookup routine of any filesystem that is exportable
1217 * (via knfsd) so that we can build dcache paths to directories effectively.
1218 *
1219 * If a dentry was found and moved, then it is returned. Otherwise NULL
1220 * is returned. This matches the expected return value of ->lookup.
1221 *
1222 */
1223struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
1224{
1225 struct dentry *new = NULL;
1226
NeilBrown21c0d8f2006-10-04 02:16:16 -07001227 if (inode && S_ISDIR(inode->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 spin_lock(&dcache_lock);
1229 new = __d_find_alias(inode, 1);
1230 if (new) {
1231 BUG_ON(!(new->d_flags & DCACHE_DISCONNECTED));
1232 spin_unlock(&dcache_lock);
1233 security_d_instantiate(new, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 d_move(new, dentry);
1235 iput(inode);
1236 } else {
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001237 /* already taking dcache_lock, so d_add() by hand */
1238 __d_instantiate(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 spin_unlock(&dcache_lock);
1240 security_d_instantiate(dentry, inode);
1241 d_rehash(dentry);
1242 }
1243 } else
1244 d_add(dentry, inode);
1245 return new;
1246}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001247EXPORT_SYMBOL(d_splice_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Barry Naujok94035402008-05-21 16:50:46 +10001249/**
1250 * d_add_ci - lookup or allocate new dentry with case-exact name
1251 * @inode: the inode case-insensitive lookup has found
1252 * @dentry: the negative dentry that was passed to the parent's lookup func
1253 * @name: the case-exact name to be associated with the returned dentry
1254 *
1255 * This is to avoid filling the dcache with case-insensitive names to the
1256 * same inode, only the actual correct case is stored in the dcache for
1257 * case-insensitive filesystems.
1258 *
1259 * For a case-insensitive lookup match and if the the case-exact dentry
1260 * already exists in in the dcache, use it and return it.
1261 *
1262 * If no entry exists with the exact case name, allocate new dentry with
1263 * the exact case, and return the spliced entry.
1264 */
Christoph Hellwige45b5902008-08-07 23:49:07 +02001265struct dentry *d_add_ci(struct dentry *dentry, struct inode *inode,
Barry Naujok94035402008-05-21 16:50:46 +10001266 struct qstr *name)
1267{
1268 int error;
1269 struct dentry *found;
1270 struct dentry *new;
1271
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001272 /*
1273 * First check if a dentry matching the name already exists,
1274 * if not go ahead and create it now.
1275 */
Barry Naujok94035402008-05-21 16:50:46 +10001276 found = d_hash_and_lookup(dentry->d_parent, name);
Barry Naujok94035402008-05-21 16:50:46 +10001277 if (!found) {
1278 new = d_alloc(dentry->d_parent, name);
1279 if (!new) {
1280 error = -ENOMEM;
1281 goto err_out;
1282 }
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001283
Barry Naujok94035402008-05-21 16:50:46 +10001284 found = d_splice_alias(inode, new);
1285 if (found) {
1286 dput(new);
1287 return found;
1288 }
1289 return new;
1290 }
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001291
1292 /*
1293 * If a matching dentry exists, and it's not negative use it.
1294 *
1295 * Decrement the reference count to balance the iget() done
1296 * earlier on.
1297 */
Barry Naujok94035402008-05-21 16:50:46 +10001298 if (found->d_inode) {
1299 if (unlikely(found->d_inode != inode)) {
1300 /* This can't happen because bad inodes are unhashed. */
1301 BUG_ON(!is_bad_inode(inode));
1302 BUG_ON(!is_bad_inode(found->d_inode));
1303 }
Barry Naujok94035402008-05-21 16:50:46 +10001304 iput(inode);
1305 return found;
1306 }
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001307
Barry Naujok94035402008-05-21 16:50:46 +10001308 /*
1309 * Negative dentry: instantiate it unless the inode is a directory and
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001310 * already has a dentry.
Barry Naujok94035402008-05-21 16:50:46 +10001311 */
Barry Naujok94035402008-05-21 16:50:46 +10001312 spin_lock(&dcache_lock);
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001313 if (!S_ISDIR(inode->i_mode) || list_empty(&inode->i_dentry)) {
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001314 __d_instantiate(found, inode);
Barry Naujok94035402008-05-21 16:50:46 +10001315 spin_unlock(&dcache_lock);
1316 security_d_instantiate(found, inode);
1317 return found;
1318 }
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001319
Barry Naujok94035402008-05-21 16:50:46 +10001320 /*
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001321 * In case a directory already has a (disconnected) entry grab a
1322 * reference to it, move it in place and use it.
Barry Naujok94035402008-05-21 16:50:46 +10001323 */
1324 new = list_entry(inode->i_dentry.next, struct dentry, d_alias);
1325 dget_locked(new);
1326 spin_unlock(&dcache_lock);
Barry Naujok94035402008-05-21 16:50:46 +10001327 security_d_instantiate(found, inode);
Barry Naujok94035402008-05-21 16:50:46 +10001328 d_move(new, found);
Barry Naujok94035402008-05-21 16:50:46 +10001329 iput(inode);
Barry Naujok94035402008-05-21 16:50:46 +10001330 dput(found);
Barry Naujok94035402008-05-21 16:50:46 +10001331 return new;
1332
1333err_out:
1334 iput(inode);
1335 return ERR_PTR(error);
1336}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001337EXPORT_SYMBOL(d_add_ci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
1339/**
1340 * d_lookup - search for a dentry
1341 * @parent: parent dentry
1342 * @name: qstr of name we wish to find
Nick Pigginb04f7842010-08-18 04:37:34 +10001343 * Returns: dentry, or NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 *
Nick Pigginb04f7842010-08-18 04:37:34 +10001345 * d_lookup searches the children of the parent dentry for the name in
1346 * question. If the dentry is found its reference count is incremented and the
1347 * dentry is returned. The caller must use dput to free the entry when it has
1348 * finished using it. %NULL is returned if the dentry does not exist.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350struct dentry * d_lookup(struct dentry * parent, struct qstr * name)
1351{
1352 struct dentry * dentry = NULL;
1353 unsigned long seq;
1354
1355 do {
1356 seq = read_seqbegin(&rename_lock);
1357 dentry = __d_lookup(parent, name);
1358 if (dentry)
1359 break;
1360 } while (read_seqretry(&rename_lock, seq));
1361 return dentry;
1362}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001363EXPORT_SYMBOL(d_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Nick Pigginb04f7842010-08-18 04:37:34 +10001365/*
1366 * __d_lookup - search for a dentry (racy)
1367 * @parent: parent dentry
1368 * @name: qstr of name we wish to find
1369 * Returns: dentry, or NULL
1370 *
1371 * __d_lookup is like d_lookup, however it may (rarely) return a
1372 * false-negative result due to unrelated rename activity.
1373 *
1374 * __d_lookup is slightly faster by avoiding rename_lock read seqlock,
1375 * however it must be used carefully, eg. with a following d_lookup in
1376 * the case of failure.
1377 *
1378 * __d_lookup callers must be commented.
1379 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380struct dentry * __d_lookup(struct dentry * parent, struct qstr * name)
1381{
1382 unsigned int len = name->len;
1383 unsigned int hash = name->hash;
1384 const unsigned char *str = name->name;
1385 struct hlist_head *head = d_hash(parent,hash);
1386 struct dentry *found = NULL;
1387 struct hlist_node *node;
Paul E. McKenney665a7582005-11-07 00:59:17 -08001388 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
Nick Pigginb04f7842010-08-18 04:37:34 +10001390 /*
1391 * The hash list is protected using RCU.
1392 *
1393 * Take d_lock when comparing a candidate dentry, to avoid races
1394 * with d_move().
1395 *
1396 * It is possible that concurrent renames can mess up our list
1397 * walk here and result in missing our dentry, resulting in the
1398 * false-negative result. d_lookup() protects against concurrent
1399 * renames using rename_lock seqlock.
1400 *
1401 * See Documentation/vfs/dcache-locking.txt for more details.
1402 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 rcu_read_lock();
1404
Paul E. McKenney665a7582005-11-07 00:59:17 -08001405 hlist_for_each_entry_rcu(dentry, node, head, d_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 struct qstr *qstr;
1407
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 if (dentry->d_name.hash != hash)
1409 continue;
1410 if (dentry->d_parent != parent)
1411 continue;
1412
1413 spin_lock(&dentry->d_lock);
1414
1415 /*
1416 * Recheck the dentry after taking the lock - d_move may have
Nick Pigginb04f7842010-08-18 04:37:34 +10001417 * changed things. Don't bother checking the hash because
1418 * we're about to compare the whole name anyway.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 */
1420 if (dentry->d_parent != parent)
1421 goto next;
1422
Linus Torvaldsd0185c02008-09-29 07:42:57 -07001423 /* non-existing due to RCU? */
1424 if (d_unhashed(dentry))
1425 goto next;
1426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 /*
1428 * It is safe to compare names since d_move() cannot
1429 * change the qstr (protected by d_lock).
1430 */
1431 qstr = &dentry->d_name;
1432 if (parent->d_op && parent->d_op->d_compare) {
1433 if (parent->d_op->d_compare(parent, qstr, name))
1434 goto next;
1435 } else {
1436 if (qstr->len != len)
1437 goto next;
1438 if (memcmp(qstr->name, str, len))
1439 goto next;
1440 }
1441
Linus Torvaldsd0185c02008-09-29 07:42:57 -07001442 atomic_inc(&dentry->d_count);
1443 found = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 spin_unlock(&dentry->d_lock);
1445 break;
1446next:
1447 spin_unlock(&dentry->d_lock);
1448 }
1449 rcu_read_unlock();
1450
1451 return found;
1452}
1453
1454/**
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08001455 * d_hash_and_lookup - hash the qstr then search for a dentry
1456 * @dir: Directory to search in
1457 * @name: qstr of name we wish to find
1458 *
1459 * On hash failure or on lookup failure NULL is returned.
1460 */
1461struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name)
1462{
1463 struct dentry *dentry = NULL;
1464
1465 /*
1466 * Check for a fs-specific hash function. Note that we must
1467 * calculate the standard hash first, as the d_op->d_hash()
1468 * routine may choose to leave the hash value unchanged.
1469 */
1470 name->hash = full_name_hash(name->name, name->len);
1471 if (dir->d_op && dir->d_op->d_hash) {
1472 if (dir->d_op->d_hash(dir, name) < 0)
1473 goto out;
1474 }
1475 dentry = d_lookup(dir, name);
1476out:
1477 return dentry;
1478}
1479
1480/**
Nick Piggin786a5e12011-01-07 17:49:16 +11001481 * d_validate - verify dentry provided from insecure source (deprecated)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 * @dentry: The dentry alleged to be valid child of @dparent
1483 * @dparent: The parent dentry (known to be valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 *
1485 * An insecure source has sent us a dentry, here we verify it and dget() it.
1486 * This is used by ncpfs in its readdir implementation.
1487 * Zero is returned in the dentry is invalid.
Nick Piggin786a5e12011-01-07 17:49:16 +11001488 *
1489 * This function is slow for big directories, and deprecated, do not use it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 */
Nick Piggind3a23e12011-01-05 20:01:21 +11001491int d_validate(struct dentry *dentry, struct dentry *dparent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492{
Nick Piggin786a5e12011-01-07 17:49:16 +11001493 struct dentry *child;
Nick Piggind3a23e12011-01-05 20:01:21 +11001494
1495 spin_lock(&dcache_lock);
Nick Piggin786a5e12011-01-07 17:49:16 +11001496 list_for_each_entry(child, &dparent->d_subdirs, d_u.d_child) {
1497 if (dentry == child) {
Nick Piggind3a23e12011-01-05 20:01:21 +11001498 __dget_locked(dentry);
1499 spin_unlock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 return 1;
1501 }
1502 }
Nick Piggind3a23e12011-01-05 20:01:21 +11001503 spin_unlock(&dcache_lock);
Nick Piggin786a5e12011-01-07 17:49:16 +11001504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 return 0;
1506}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001507EXPORT_SYMBOL(d_validate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
1509/*
1510 * When a file is deleted, we have two options:
1511 * - turn this dentry into a negative dentry
1512 * - unhash this dentry and free it.
1513 *
1514 * Usually, we want to just turn this into
1515 * a negative dentry, but if anybody else is
1516 * currently using the dentry or the inode
1517 * we can't do that and we fall back on removing
1518 * it from the hash queues and waiting for
1519 * it to be deleted later when it has no users
1520 */
1521
1522/**
1523 * d_delete - delete a dentry
1524 * @dentry: The dentry to delete
1525 *
1526 * Turn the dentry into a negative dentry if possible, otherwise
1527 * remove it from the hash queues so it can be deleted later
1528 */
1529
1530void d_delete(struct dentry * dentry)
1531{
John McCutchan7a91bf72005-08-08 13:52:16 -04001532 int isdir = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 /*
1534 * Are we the only user?
1535 */
1536 spin_lock(&dcache_lock);
1537 spin_lock(&dentry->d_lock);
John McCutchan7a91bf72005-08-08 13:52:16 -04001538 isdir = S_ISDIR(dentry->d_inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 if (atomic_read(&dentry->d_count) == 1) {
Al Viro13e3c5e2010-05-21 16:11:04 -04001540 dentry->d_flags &= ~DCACHE_CANT_MOUNT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 dentry_iput(dentry);
John McCutchan7a91bf72005-08-08 13:52:16 -04001542 fsnotify_nameremove(dentry, isdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 return;
1544 }
1545
1546 if (!d_unhashed(dentry))
1547 __d_drop(dentry);
1548
1549 spin_unlock(&dentry->d_lock);
1550 spin_unlock(&dcache_lock);
John McCutchan7a91bf72005-08-08 13:52:16 -04001551
1552 fsnotify_nameremove(dentry, isdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001554EXPORT_SYMBOL(d_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
1556static void __d_rehash(struct dentry * entry, struct hlist_head *list)
1557{
1558
1559 entry->d_flags &= ~DCACHE_UNHASHED;
1560 hlist_add_head_rcu(&entry->d_hash, list);
1561}
1562
David Howells770bfad2006-08-22 20:06:07 -04001563static void _d_rehash(struct dentry * entry)
1564{
1565 __d_rehash(entry, d_hash(entry->d_parent, entry->d_name.hash));
1566}
1567
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568/**
1569 * d_rehash - add an entry back to the hash
1570 * @entry: dentry to add to the hash
1571 *
1572 * Adds a dentry to the hash according to its name.
1573 */
1574
1575void d_rehash(struct dentry * entry)
1576{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 spin_lock(&dcache_lock);
1578 spin_lock(&entry->d_lock);
David Howells770bfad2006-08-22 20:06:07 -04001579 _d_rehash(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 spin_unlock(&entry->d_lock);
1581 spin_unlock(&dcache_lock);
1582}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001583EXPORT_SYMBOL(d_rehash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585/*
1586 * When switching names, the actual string doesn't strictly have to
1587 * be preserved in the target - because we're dropping the target
1588 * anyway. As such, we can just do a simple memcpy() to copy over
1589 * the new name before we switch.
1590 *
1591 * Note that we have to be a lot more careful about getting the hash
1592 * switched - we have to switch the hash value properly even if it
1593 * then no longer matches the actual (corrupted) string of the target.
1594 * The hash value has to match the hash queue that the dentry is on..
1595 */
1596static void switch_names(struct dentry *dentry, struct dentry *target)
1597{
1598 if (dname_external(target)) {
1599 if (dname_external(dentry)) {
1600 /*
1601 * Both external: swap the pointers
1602 */
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08001603 swap(target->d_name.name, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 } else {
1605 /*
1606 * dentry:internal, target:external. Steal target's
1607 * storage and make target internal.
1608 */
J. Bruce Fields321bcf92007-10-21 16:41:38 -07001609 memcpy(target->d_iname, dentry->d_name.name,
1610 dentry->d_name.len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 dentry->d_name.name = target->d_name.name;
1612 target->d_name.name = target->d_iname;
1613 }
1614 } else {
1615 if (dname_external(dentry)) {
1616 /*
1617 * dentry:external, target:internal. Give dentry's
1618 * storage to target and make dentry internal
1619 */
1620 memcpy(dentry->d_iname, target->d_name.name,
1621 target->d_name.len + 1);
1622 target->d_name.name = dentry->d_name.name;
1623 dentry->d_name.name = dentry->d_iname;
1624 } else {
1625 /*
1626 * Both are internal. Just copy target to dentry
1627 */
1628 memcpy(dentry->d_iname, target->d_name.name,
1629 target->d_name.len + 1);
Al Virodc711ca2008-11-03 15:03:50 -05001630 dentry->d_name.len = target->d_name.len;
1631 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 }
1633 }
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08001634 swap(dentry->d_name.len, target->d_name.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635}
1636
1637/*
1638 * We cannibalize "target" when moving dentry on top of it,
1639 * because it's going to be thrown away anyway. We could be more
1640 * polite about it, though.
1641 *
1642 * This forceful removal will result in ugly /proc output if
1643 * somebody holds a file open that got deleted due to a rename.
1644 * We could be nicer about the deleted file, and let it show
J. Bruce Fieldsbc154b12007-10-16 23:29:42 -07001645 * up under the name it had before it was deleted rather than
1646 * under the original name of the file that was moved on top of it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 */
1648
Trond Myklebust9eaef272006-10-21 10:24:20 -07001649/*
1650 * d_move_locked - move a dentry
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 * @dentry: entry to move
1652 * @target: new dentry
1653 *
1654 * Update the dcache to reflect the move of a file name. Negative
1655 * dcache entries should not be moved in this way.
1656 */
Trond Myklebust9eaef272006-10-21 10:24:20 -07001657static void d_move_locked(struct dentry * dentry, struct dentry * target)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658{
1659 struct hlist_head *list;
1660
1661 if (!dentry->d_inode)
1662 printk(KERN_WARNING "VFS: moving negative dcache entry\n");
1663
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 write_seqlock(&rename_lock);
1665 /*
1666 * XXXX: do we really need to take target->d_lock?
1667 */
1668 if (target < dentry) {
1669 spin_lock(&target->d_lock);
Ingo Molnara90b9c02006-07-03 00:25:04 -07001670 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 } else {
1672 spin_lock(&dentry->d_lock);
Ingo Molnara90b9c02006-07-03 00:25:04 -07001673 spin_lock_nested(&target->d_lock, DENTRY_D_LOCK_NESTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 }
1675
1676 /* Move the dentry to the target hash queue, if on different bucket */
Denis Chengf77e3492007-10-16 23:30:11 -07001677 if (d_unhashed(dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 goto already_unhashed;
1679
1680 hlist_del_rcu(&dentry->d_hash);
1681
1682already_unhashed:
1683 list = d_hash(target->d_parent, target->d_name.hash);
1684 __d_rehash(dentry, list);
1685
1686 /* Unhash the target: dput() will then get rid of it */
1687 __d_drop(target);
1688
Eric Dumazet5160ee62006-01-08 01:03:32 -08001689 list_del(&dentry->d_u.d_child);
1690 list_del(&target->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
1692 /* Switch the names.. */
1693 switch_names(dentry, target);
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08001694 swap(dentry->d_name.hash, target->d_name.hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
1696 /* ... and switch the parents */
1697 if (IS_ROOT(dentry)) {
1698 dentry->d_parent = target->d_parent;
1699 target->d_parent = target;
Eric Dumazet5160ee62006-01-08 01:03:32 -08001700 INIT_LIST_HEAD(&target->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 } else {
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08001702 swap(dentry->d_parent, target->d_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
1704 /* And add them back to the (new) parent lists */
Eric Dumazet5160ee62006-01-08 01:03:32 -08001705 list_add(&target->d_u.d_child, &target->d_parent->d_subdirs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 }
1707
Eric Dumazet5160ee62006-01-08 01:03:32 -08001708 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 spin_unlock(&target->d_lock);
Nick Pigginc32ccd82006-03-25 03:07:09 -08001710 fsnotify_d_move(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 spin_unlock(&dentry->d_lock);
1712 write_sequnlock(&rename_lock);
Trond Myklebust9eaef272006-10-21 10:24:20 -07001713}
1714
1715/**
1716 * d_move - move a dentry
1717 * @dentry: entry to move
1718 * @target: new dentry
1719 *
1720 * Update the dcache to reflect the move of a file name. Negative
1721 * dcache entries should not be moved in this way.
1722 */
1723
1724void d_move(struct dentry * dentry, struct dentry * target)
1725{
1726 spin_lock(&dcache_lock);
1727 d_move_locked(dentry, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 spin_unlock(&dcache_lock);
1729}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001730EXPORT_SYMBOL(d_move);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001732/**
1733 * d_ancestor - search for an ancestor
1734 * @p1: ancestor dentry
1735 * @p2: child dentry
1736 *
1737 * Returns the ancestor dentry of p2 which is a child of p1, if p1 is
1738 * an ancestor of p2, else NULL.
Trond Myklebust9eaef272006-10-21 10:24:20 -07001739 */
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001740struct dentry *d_ancestor(struct dentry *p1, struct dentry *p2)
Trond Myklebust9eaef272006-10-21 10:24:20 -07001741{
1742 struct dentry *p;
1743
OGAWA Hirofumi871c0062008-10-16 07:50:27 +09001744 for (p = p2; !IS_ROOT(p); p = p->d_parent) {
Trond Myklebust9eaef272006-10-21 10:24:20 -07001745 if (p->d_parent == p1)
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001746 return p;
Trond Myklebust9eaef272006-10-21 10:24:20 -07001747 }
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001748 return NULL;
Trond Myklebust9eaef272006-10-21 10:24:20 -07001749}
1750
1751/*
1752 * This helper attempts to cope with remotely renamed directories
1753 *
1754 * It assumes that the caller is already holding
1755 * dentry->d_parent->d_inode->i_mutex and the dcache_lock
1756 *
1757 * Note: If ever the locking in lock_rename() changes, then please
1758 * remember to update this too...
Trond Myklebust9eaef272006-10-21 10:24:20 -07001759 */
1760static struct dentry *__d_unalias(struct dentry *dentry, struct dentry *alias)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +02001761 __releases(dcache_lock)
Trond Myklebust9eaef272006-10-21 10:24:20 -07001762{
1763 struct mutex *m1 = NULL, *m2 = NULL;
1764 struct dentry *ret;
1765
1766 /* If alias and dentry share a parent, then no extra locks required */
1767 if (alias->d_parent == dentry->d_parent)
1768 goto out_unalias;
1769
1770 /* Check for loops */
1771 ret = ERR_PTR(-ELOOP);
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001772 if (d_ancestor(alias, dentry))
Trond Myklebust9eaef272006-10-21 10:24:20 -07001773 goto out_err;
1774
1775 /* See lock_rename() */
1776 ret = ERR_PTR(-EBUSY);
1777 if (!mutex_trylock(&dentry->d_sb->s_vfs_rename_mutex))
1778 goto out_err;
1779 m1 = &dentry->d_sb->s_vfs_rename_mutex;
1780 if (!mutex_trylock(&alias->d_parent->d_inode->i_mutex))
1781 goto out_err;
1782 m2 = &alias->d_parent->d_inode->i_mutex;
1783out_unalias:
1784 d_move_locked(alias, dentry);
1785 ret = alias;
1786out_err:
1787 spin_unlock(&dcache_lock);
1788 if (m2)
1789 mutex_unlock(m2);
1790 if (m1)
1791 mutex_unlock(m1);
1792 return ret;
1793}
1794
1795/*
David Howells770bfad2006-08-22 20:06:07 -04001796 * Prepare an anonymous dentry for life in the superblock's dentry tree as a
1797 * named dentry in place of the dentry to be replaced.
1798 */
1799static void __d_materialise_dentry(struct dentry *dentry, struct dentry *anon)
1800{
1801 struct dentry *dparent, *aparent;
1802
1803 switch_names(dentry, anon);
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08001804 swap(dentry->d_name.hash, anon->d_name.hash);
David Howells770bfad2006-08-22 20:06:07 -04001805
1806 dparent = dentry->d_parent;
1807 aparent = anon->d_parent;
1808
1809 dentry->d_parent = (aparent == anon) ? dentry : aparent;
1810 list_del(&dentry->d_u.d_child);
1811 if (!IS_ROOT(dentry))
1812 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
1813 else
1814 INIT_LIST_HEAD(&dentry->d_u.d_child);
1815
1816 anon->d_parent = (dparent == dentry) ? anon : dparent;
1817 list_del(&anon->d_u.d_child);
1818 if (!IS_ROOT(anon))
1819 list_add(&anon->d_u.d_child, &anon->d_parent->d_subdirs);
1820 else
1821 INIT_LIST_HEAD(&anon->d_u.d_child);
1822
1823 anon->d_flags &= ~DCACHE_DISCONNECTED;
1824}
1825
1826/**
1827 * d_materialise_unique - introduce an inode into the tree
1828 * @dentry: candidate dentry
1829 * @inode: inode to bind to the dentry, to which aliases may be attached
1830 *
1831 * Introduces an dentry into the tree, substituting an extant disconnected
1832 * root directory alias in its place if there is one
1833 */
1834struct dentry *d_materialise_unique(struct dentry *dentry, struct inode *inode)
1835{
Trond Myklebust9eaef272006-10-21 10:24:20 -07001836 struct dentry *actual;
David Howells770bfad2006-08-22 20:06:07 -04001837
1838 BUG_ON(!d_unhashed(dentry));
1839
1840 spin_lock(&dcache_lock);
1841
1842 if (!inode) {
1843 actual = dentry;
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001844 __d_instantiate(dentry, NULL);
David Howells770bfad2006-08-22 20:06:07 -04001845 goto found_lock;
1846 }
1847
Trond Myklebust9eaef272006-10-21 10:24:20 -07001848 if (S_ISDIR(inode->i_mode)) {
1849 struct dentry *alias;
David Howells770bfad2006-08-22 20:06:07 -04001850
Trond Myklebust9eaef272006-10-21 10:24:20 -07001851 /* Does an aliased dentry already exist? */
1852 alias = __d_find_alias(inode, 0);
1853 if (alias) {
1854 actual = alias;
1855 /* Is this an anonymous mountpoint that we could splice
1856 * into our tree? */
1857 if (IS_ROOT(alias)) {
1858 spin_lock(&alias->d_lock);
1859 __d_materialise_dentry(dentry, alias);
1860 __d_drop(alias);
1861 goto found;
1862 }
1863 /* Nope, but we must(!) avoid directory aliasing */
1864 actual = __d_unalias(dentry, alias);
1865 if (IS_ERR(actual))
1866 dput(alias);
1867 goto out_nolock;
1868 }
David Howells770bfad2006-08-22 20:06:07 -04001869 }
1870
1871 /* Add a unique reference */
1872 actual = __d_instantiate_unique(dentry, inode);
1873 if (!actual)
1874 actual = dentry;
1875 else if (unlikely(!d_unhashed(actual)))
1876 goto shouldnt_be_hashed;
1877
1878found_lock:
1879 spin_lock(&actual->d_lock);
1880found:
1881 _d_rehash(actual);
1882 spin_unlock(&actual->d_lock);
1883 spin_unlock(&dcache_lock);
Trond Myklebust9eaef272006-10-21 10:24:20 -07001884out_nolock:
David Howells770bfad2006-08-22 20:06:07 -04001885 if (actual == dentry) {
1886 security_d_instantiate(dentry, inode);
1887 return NULL;
1888 }
1889
1890 iput(inode);
1891 return actual;
1892
David Howells770bfad2006-08-22 20:06:07 -04001893shouldnt_be_hashed:
1894 spin_unlock(&dcache_lock);
1895 BUG();
David Howells770bfad2006-08-22 20:06:07 -04001896}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001897EXPORT_SYMBOL_GPL(d_materialise_unique);
David Howells770bfad2006-08-22 20:06:07 -04001898
Miklos Szeredicdd16d02008-06-23 18:11:53 +02001899static int prepend(char **buffer, int *buflen, const char *str, int namelen)
Ram Pai6092d042008-03-27 13:06:20 +01001900{
1901 *buflen -= namelen;
1902 if (*buflen < 0)
1903 return -ENAMETOOLONG;
1904 *buffer -= namelen;
1905 memcpy(*buffer, str, namelen);
1906 return 0;
1907}
1908
Miklos Szeredicdd16d02008-06-23 18:11:53 +02001909static int prepend_name(char **buffer, int *buflen, struct qstr *name)
1910{
1911 return prepend(buffer, buflen, name->name, name->len);
1912}
1913
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914/**
Miklos Szeredif2eb6572010-08-10 11:41:39 +02001915 * Prepend path string to a buffer
1916 *
1917 * @path: the dentry/vfsmount to report
1918 * @root: root vfsmnt/dentry (may be modified by this function)
1919 * @buffer: pointer to the end of the buffer
1920 * @buflen: pointer to buffer length
1921 *
1922 * Caller holds the dcache_lock.
1923 *
1924 * If path is not reachable from the supplied root, then the value of
1925 * root is changed (without modifying refcounts).
1926 */
1927static int prepend_path(const struct path *path, struct path *root,
1928 char **buffer, int *buflen)
1929{
1930 struct dentry *dentry = path->dentry;
1931 struct vfsmount *vfsmnt = path->mnt;
1932 bool slash = false;
1933 int error = 0;
1934
Nick Piggin99b7db72010-08-18 04:37:39 +10001935 br_read_lock(vfsmount_lock);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02001936 while (dentry != root->dentry || vfsmnt != root->mnt) {
1937 struct dentry * parent;
1938
1939 if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
1940 /* Global root? */
1941 if (vfsmnt->mnt_parent == vfsmnt) {
1942 goto global_root;
1943 }
1944 dentry = vfsmnt->mnt_mountpoint;
1945 vfsmnt = vfsmnt->mnt_parent;
1946 continue;
1947 }
1948 parent = dentry->d_parent;
1949 prefetch(parent);
1950 error = prepend_name(buffer, buflen, &dentry->d_name);
1951 if (!error)
1952 error = prepend(buffer, buflen, "/", 1);
1953 if (error)
1954 break;
1955
1956 slash = true;
1957 dentry = parent;
1958 }
1959
1960out:
1961 if (!error && !slash)
1962 error = prepend(buffer, buflen, "/", 1);
1963
Nick Piggin99b7db72010-08-18 04:37:39 +10001964 br_read_unlock(vfsmount_lock);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02001965 return error;
1966
1967global_root:
1968 /*
1969 * Filesystems needing to implement special "root names"
1970 * should do so with ->d_dname()
1971 */
1972 if (IS_ROOT(dentry) &&
1973 (dentry->d_name.len != 1 || dentry->d_name.name[0] != '/')) {
1974 WARN(1, "Root dentry has weird name <%.*s>\n",
1975 (int) dentry->d_name.len, dentry->d_name.name);
1976 }
1977 root->mnt = vfsmnt;
1978 root->dentry = dentry;
1979 goto out;
1980}
1981
1982/**
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +02001983 * __d_path - return the path of a dentry
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01001984 * @path: the dentry/vfsmount to report
1985 * @root: root vfsmnt/dentry (may be modified by this function)
Randy Dunlapcd956a12010-08-14 13:05:31 -07001986 * @buf: buffer to return value in
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 * @buflen: buffer length
1988 *
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02001989 * Convert a dentry into an ASCII path name.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 *
Arjan van de Ven52afeef2008-12-01 14:35:00 -08001991 * Returns a pointer into the buffer or an error code if the
1992 * path was too long.
Linus Torvalds552ce542007-02-13 12:08:18 -08001993 *
Christoph Hellwigbe148242010-10-10 05:36:21 -04001994 * "buflen" should be positive.
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01001995 *
1996 * If path is not reachable from the supplied root, then the value of
1997 * root is changed (without modifying refcounts).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 */
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01001999char *__d_path(const struct path *path, struct path *root,
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002000 char *buf, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001{
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002002 char *res = buf + buflen;
2003 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002005 prepend(&res, &buflen, "\0", 1);
Christoph Hellwigbe148242010-10-10 05:36:21 -04002006 spin_lock(&dcache_lock);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002007 error = prepend_path(path, root, &res, &buflen);
Christoph Hellwigbe148242010-10-10 05:36:21 -04002008 spin_unlock(&dcache_lock);
2009
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002010 if (error)
2011 return ERR_PTR(error);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002012 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013}
2014
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002015/*
2016 * same as __d_path but appends "(deleted)" for unlinked files.
2017 */
2018static int path_with_deleted(const struct path *path, struct path *root,
2019 char **buf, int *buflen)
2020{
2021 prepend(buf, buflen, "\0", 1);
2022 if (d_unlinked(path->dentry)) {
2023 int error = prepend(buf, buflen, " (deleted)", 10);
2024 if (error)
2025 return error;
2026 }
2027
2028 return prepend_path(path, root, buf, buflen);
2029}
2030
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002031static int prepend_unreachable(char **buffer, int *buflen)
2032{
2033 return prepend(buffer, buflen, "(unreachable)", 13);
2034}
2035
Jan Bluncka03a8a702008-02-14 19:38:32 -08002036/**
2037 * d_path - return the path of a dentry
Jan Blunckcf28b482008-02-14 19:38:44 -08002038 * @path: path to report
Jan Bluncka03a8a702008-02-14 19:38:32 -08002039 * @buf: buffer to return value in
2040 * @buflen: buffer length
2041 *
2042 * Convert a dentry into an ASCII path name. If the entry has been deleted
2043 * the string " (deleted)" is appended. Note that this is ambiguous.
2044 *
Arjan van de Ven52afeef2008-12-01 14:35:00 -08002045 * Returns a pointer into the buffer or an error code if the path was
2046 * too long. Note: Callers should use the returned pointer, not the passed
2047 * in buffer, to use the name! The implementation often starts at an offset
2048 * into the buffer, and may leave 0 bytes at the start.
Jan Bluncka03a8a702008-02-14 19:38:32 -08002049 *
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +02002050 * "buflen" should be positive.
Jan Bluncka03a8a702008-02-14 19:38:32 -08002051 */
Jan Engelhardt20d4fdc2008-06-09 16:40:36 -07002052char *d_path(const struct path *path, char *buf, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053{
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002054 char *res = buf + buflen;
Jan Blunck6ac08c32008-02-14 19:34:38 -08002055 struct path root;
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002056 struct path tmp;
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002057 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
Eric Dumazetc23fbb62007-05-08 00:26:18 -07002059 /*
2060 * We have various synthetic filesystems that never get mounted. On
2061 * these filesystems dentries are never used for lookup purposes, and
2062 * thus don't need to be hashed. They also don't need a name until a
2063 * user wants to identify the object in /proc/pid/fd/. The little hack
2064 * below allows us to generate a name for these objects on demand:
2065 */
Jan Blunckcf28b482008-02-14 19:38:44 -08002066 if (path->dentry->d_op && path->dentry->d_op->d_dname)
2067 return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
Eric Dumazetc23fbb62007-05-08 00:26:18 -07002068
Miklos Szeredif7ad3c62010-08-10 11:41:36 +02002069 get_fs_root(current->fs, &root);
Linus Torvalds552ce542007-02-13 12:08:18 -08002070 spin_lock(&dcache_lock);
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002071 tmp = root;
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002072 error = path_with_deleted(path, &tmp, &res, &buflen);
2073 if (error)
2074 res = ERR_PTR(error);
Linus Torvalds552ce542007-02-13 12:08:18 -08002075 spin_unlock(&dcache_lock);
Jan Blunck6ac08c32008-02-14 19:34:38 -08002076 path_put(&root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 return res;
2078}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002079EXPORT_SYMBOL(d_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002081/**
2082 * d_path_with_unreachable - return the path of a dentry
2083 * @path: path to report
2084 * @buf: buffer to return value in
2085 * @buflen: buffer length
2086 *
2087 * The difference from d_path() is that this prepends "(unreachable)"
2088 * to paths which are unreachable from the current process' root.
2089 */
2090char *d_path_with_unreachable(const struct path *path, char *buf, int buflen)
2091{
2092 char *res = buf + buflen;
2093 struct path root;
2094 struct path tmp;
2095 int error;
2096
2097 if (path->dentry->d_op && path->dentry->d_op->d_dname)
2098 return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
2099
2100 get_fs_root(current->fs, &root);
2101 spin_lock(&dcache_lock);
2102 tmp = root;
2103 error = path_with_deleted(path, &tmp, &res, &buflen);
2104 if (!error && !path_equal(&tmp, &root))
2105 error = prepend_unreachable(&res, &buflen);
2106 spin_unlock(&dcache_lock);
2107 path_put(&root);
2108 if (error)
2109 res = ERR_PTR(error);
2110
2111 return res;
2112}
2113
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114/*
Eric Dumazetc23fbb62007-05-08 00:26:18 -07002115 * Helper function for dentry_operations.d_dname() members
2116 */
2117char *dynamic_dname(struct dentry *dentry, char *buffer, int buflen,
2118 const char *fmt, ...)
2119{
2120 va_list args;
2121 char temp[64];
2122 int sz;
2123
2124 va_start(args, fmt);
2125 sz = vsnprintf(temp, sizeof(temp), fmt, args) + 1;
2126 va_end(args);
2127
2128 if (sz > sizeof(temp) || sz > buflen)
2129 return ERR_PTR(-ENAMETOOLONG);
2130
2131 buffer += buflen - sz;
2132 return memcpy(buffer, temp, sz);
2133}
2134
2135/*
Ram Pai6092d042008-03-27 13:06:20 +01002136 * Write full pathname from the root of the filesystem into the buffer.
2137 */
Al Viroc1031352010-06-06 22:31:14 -04002138char *__dentry_path(struct dentry *dentry, char *buf, int buflen)
Ram Pai6092d042008-03-27 13:06:20 +01002139{
2140 char *end = buf + buflen;
2141 char *retval;
2142
Ram Pai6092d042008-03-27 13:06:20 +01002143 prepend(&end, &buflen, "\0", 1);
Ram Pai6092d042008-03-27 13:06:20 +01002144 if (buflen < 1)
2145 goto Elong;
2146 /* Get '/' right */
2147 retval = end-1;
2148 *retval = '/';
2149
Miklos Szeredicdd16d02008-06-23 18:11:53 +02002150 while (!IS_ROOT(dentry)) {
2151 struct dentry *parent = dentry->d_parent;
Ram Pai6092d042008-03-27 13:06:20 +01002152
Ram Pai6092d042008-03-27 13:06:20 +01002153 prefetch(parent);
Miklos Szeredicdd16d02008-06-23 18:11:53 +02002154 if ((prepend_name(&end, &buflen, &dentry->d_name) != 0) ||
Ram Pai6092d042008-03-27 13:06:20 +01002155 (prepend(&end, &buflen, "/", 1) != 0))
2156 goto Elong;
2157
2158 retval = end;
2159 dentry = parent;
2160 }
Al Viroc1031352010-06-06 22:31:14 -04002161 return retval;
2162Elong:
2163 return ERR_PTR(-ENAMETOOLONG);
2164}
2165EXPORT_SYMBOL(__dentry_path);
2166
2167char *dentry_path(struct dentry *dentry, char *buf, int buflen)
2168{
2169 char *p = NULL;
2170 char *retval;
2171
2172 spin_lock(&dcache_lock);
2173 if (d_unlinked(dentry)) {
2174 p = buf + buflen;
2175 if (prepend(&p, &buflen, "//deleted", 10) != 0)
2176 goto Elong;
2177 buflen++;
2178 }
2179 retval = __dentry_path(dentry, buf, buflen);
Ram Pai6092d042008-03-27 13:06:20 +01002180 spin_unlock(&dcache_lock);
Al Viroc1031352010-06-06 22:31:14 -04002181 if (!IS_ERR(retval) && p)
2182 *p = '/'; /* restore '/' overriden with '\0' */
Ram Pai6092d042008-03-27 13:06:20 +01002183 return retval;
2184Elong:
2185 spin_unlock(&dcache_lock);
2186 return ERR_PTR(-ENAMETOOLONG);
2187}
2188
2189/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 * NOTE! The user-level library version returns a
2191 * character pointer. The kernel system call just
2192 * returns the length of the buffer filled (which
2193 * includes the ending '\0' character), or a negative
2194 * error value. So libc would do something like
2195 *
2196 * char *getcwd(char * buf, size_t size)
2197 * {
2198 * int retval;
2199 *
2200 * retval = sys_getcwd(buf, size);
2201 * if (retval >= 0)
2202 * return buf;
2203 * errno = -retval;
2204 * return NULL;
2205 * }
2206 */
Heiko Carstens3cdad422009-01-14 14:14:22 +01002207SYSCALL_DEFINE2(getcwd, char __user *, buf, unsigned long, size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208{
Linus Torvalds552ce542007-02-13 12:08:18 -08002209 int error;
Jan Blunck6ac08c32008-02-14 19:34:38 -08002210 struct path pwd, root;
Linus Torvalds552ce542007-02-13 12:08:18 -08002211 char *page = (char *) __get_free_page(GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
2213 if (!page)
2214 return -ENOMEM;
2215
Miklos Szeredif7ad3c62010-08-10 11:41:36 +02002216 get_fs_root_and_pwd(current->fs, &root, &pwd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217
Linus Torvalds552ce542007-02-13 12:08:18 -08002218 error = -ENOENT;
Linus Torvalds552ce542007-02-13 12:08:18 -08002219 spin_lock(&dcache_lock);
Alexey Dobriyanf3da3922009-05-04 03:32:03 +04002220 if (!d_unlinked(pwd.dentry)) {
Linus Torvalds552ce542007-02-13 12:08:18 -08002221 unsigned long len;
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002222 struct path tmp = root;
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002223 char *cwd = page + PAGE_SIZE;
2224 int buflen = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002226 prepend(&cwd, &buflen, "\0", 1);
2227 error = prepend_path(&pwd, &tmp, &cwd, &buflen);
Linus Torvalds552ce542007-02-13 12:08:18 -08002228 spin_unlock(&dcache_lock);
2229
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002230 if (error)
Linus Torvalds552ce542007-02-13 12:08:18 -08002231 goto out;
2232
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002233 /* Unreachable from current root */
2234 if (!path_equal(&tmp, &root)) {
2235 error = prepend_unreachable(&cwd, &buflen);
2236 if (error)
2237 goto out;
2238 }
2239
Linus Torvalds552ce542007-02-13 12:08:18 -08002240 error = -ERANGE;
2241 len = PAGE_SIZE + page - cwd;
2242 if (len <= size) {
2243 error = len;
2244 if (copy_to_user(buf, cwd, len))
2245 error = -EFAULT;
2246 }
2247 } else
2248 spin_unlock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249
2250out:
Jan Blunck6ac08c32008-02-14 19:34:38 -08002251 path_put(&pwd);
2252 path_put(&root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 free_page((unsigned long) page);
2254 return error;
2255}
2256
2257/*
2258 * Test whether new_dentry is a subdirectory of old_dentry.
2259 *
2260 * Trivially implemented using the dcache structure
2261 */
2262
2263/**
2264 * is_subdir - is new dentry a subdirectory of old_dentry
2265 * @new_dentry: new dentry
2266 * @old_dentry: old dentry
2267 *
2268 * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
2269 * Returns 0 otherwise.
2270 * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
2271 */
2272
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002273int is_subdir(struct dentry *new_dentry, struct dentry *old_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274{
2275 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 unsigned long seq;
2277
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002278 if (new_dentry == old_dentry)
2279 return 1;
2280
2281 /*
2282 * Need rcu_readlock to protect against the d_parent trashing
2283 * due to d_move
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 */
2285 rcu_read_lock();
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002286 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 /* for restarting inner loop in case of seq retry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 seq = read_seqbegin(&rename_lock);
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002289 if (d_ancestor(old_dentry, new_dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 result = 1;
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002291 else
2292 result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 } while (read_seqretry(&rename_lock, seq));
2294 rcu_read_unlock();
2295
2296 return result;
2297}
2298
Al Viro2096f752010-01-30 13:16:21 -05002299int path_is_under(struct path *path1, struct path *path2)
2300{
2301 struct vfsmount *mnt = path1->mnt;
2302 struct dentry *dentry = path1->dentry;
2303 int res;
Nick Piggin99b7db72010-08-18 04:37:39 +10002304
2305 br_read_lock(vfsmount_lock);
Al Viro2096f752010-01-30 13:16:21 -05002306 if (mnt != path2->mnt) {
2307 for (;;) {
2308 if (mnt->mnt_parent == mnt) {
Nick Piggin99b7db72010-08-18 04:37:39 +10002309 br_read_unlock(vfsmount_lock);
Al Viro2096f752010-01-30 13:16:21 -05002310 return 0;
2311 }
2312 if (mnt->mnt_parent == path2->mnt)
2313 break;
2314 mnt = mnt->mnt_parent;
2315 }
2316 dentry = mnt->mnt_mountpoint;
2317 }
2318 res = is_subdir(dentry, path2->dentry);
Nick Piggin99b7db72010-08-18 04:37:39 +10002319 br_read_unlock(vfsmount_lock);
Al Viro2096f752010-01-30 13:16:21 -05002320 return res;
2321}
2322EXPORT_SYMBOL(path_is_under);
2323
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324void d_genocide(struct dentry *root)
2325{
2326 struct dentry *this_parent = root;
2327 struct list_head *next;
2328
2329 spin_lock(&dcache_lock);
2330repeat:
2331 next = this_parent->d_subdirs.next;
2332resume:
2333 while (next != &this_parent->d_subdirs) {
2334 struct list_head *tmp = next;
Eric Dumazet5160ee62006-01-08 01:03:32 -08002335 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 next = tmp->next;
2337 if (d_unhashed(dentry)||!dentry->d_inode)
2338 continue;
2339 if (!list_empty(&dentry->d_subdirs)) {
2340 this_parent = dentry;
2341 goto repeat;
2342 }
2343 atomic_dec(&dentry->d_count);
2344 }
2345 if (this_parent != root) {
Eric Dumazet5160ee62006-01-08 01:03:32 -08002346 next = this_parent->d_u.d_child.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 atomic_dec(&this_parent->d_count);
2348 this_parent = this_parent->d_parent;
2349 goto resume;
2350 }
2351 spin_unlock(&dcache_lock);
2352}
2353
2354/**
2355 * find_inode_number - check for dentry with name
2356 * @dir: directory to check
2357 * @name: Name to find.
2358 *
2359 * Check whether a dentry already exists for the given name,
2360 * and return the inode number if it has an inode. Otherwise
2361 * 0 is returned.
2362 *
2363 * This routine is used to post-process directory listings for
2364 * filesystems using synthetic inode numbers, and is necessary
2365 * to keep getcwd() working.
2366 */
2367
2368ino_t find_inode_number(struct dentry *dir, struct qstr *name)
2369{
2370 struct dentry * dentry;
2371 ino_t ino = 0;
2372
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08002373 dentry = d_hash_and_lookup(dir, name);
2374 if (dentry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 if (dentry->d_inode)
2376 ino = dentry->d_inode->i_ino;
2377 dput(dentry);
2378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 return ino;
2380}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002381EXPORT_SYMBOL(find_inode_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382
2383static __initdata unsigned long dhash_entries;
2384static int __init set_dhash_entries(char *str)
2385{
2386 if (!str)
2387 return 0;
2388 dhash_entries = simple_strtoul(str, &str, 0);
2389 return 1;
2390}
2391__setup("dhash_entries=", set_dhash_entries);
2392
2393static void __init dcache_init_early(void)
2394{
2395 int loop;
2396
2397 /* If hashes are distributed across NUMA nodes, defer
2398 * hash allocation until vmalloc space is available.
2399 */
2400 if (hashdist)
2401 return;
2402
2403 dentry_hashtable =
2404 alloc_large_system_hash("Dentry cache",
2405 sizeof(struct hlist_head),
2406 dhash_entries,
2407 13,
2408 HASH_EARLY,
2409 &d_hash_shift,
2410 &d_hash_mask,
2411 0);
2412
2413 for (loop = 0; loop < (1 << d_hash_shift); loop++)
2414 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
2415}
2416
Denis Cheng74bf17c2007-10-16 23:26:30 -07002417static void __init dcache_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418{
2419 int loop;
2420
Christoph Hellwig312d3ca2010-10-10 05:36:23 -04002421 percpu_counter_init(&nr_dentry, 0);
Christoph Hellwig312d3ca2010-10-10 05:36:23 -04002422
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 /*
2424 * A constructor could be added for stable state like the lists,
2425 * but it is probably not worth it because of the cache nature
2426 * of the dcache.
2427 */
Christoph Lameter0a31bd52007-05-06 14:49:57 -07002428 dentry_cache = KMEM_CACHE(dentry,
2429 SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|SLAB_MEM_SPREAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430
Rusty Russell8e1f9362007-07-17 04:03:17 -07002431 register_shrinker(&dcache_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432
2433 /* Hash may have been set up in dcache_init_early */
2434 if (!hashdist)
2435 return;
2436
2437 dentry_hashtable =
2438 alloc_large_system_hash("Dentry cache",
2439 sizeof(struct hlist_head),
2440 dhash_entries,
2441 13,
2442 0,
2443 &d_hash_shift,
2444 &d_hash_mask,
2445 0);
2446
2447 for (loop = 0; loop < (1 << d_hash_shift); loop++)
2448 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
2449}
2450
2451/* SLAB cache for __getname() consumers */
Christoph Lametere18b8902006-12-06 20:33:20 -08002452struct kmem_cache *names_cachep __read_mostly;
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002453EXPORT_SYMBOL(names_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455EXPORT_SYMBOL(d_genocide);
2456
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457void __init vfs_caches_init_early(void)
2458{
2459 dcache_init_early();
2460 inode_init_early();
2461}
2462
2463void __init vfs_caches_init(unsigned long mempages)
2464{
2465 unsigned long reserve;
2466
2467 /* Base hash sizes on available memory, with a reserve equal to
2468 150% of current kernel size */
2469
2470 reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);
2471 mempages -= reserve;
2472
2473 names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09002474 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
Denis Cheng74bf17c2007-10-16 23:26:30 -07002476 dcache_init();
2477 inode_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 files_init(mempages);
Denis Cheng74bf17c2007-10-16 23:26:30 -07002479 mnt_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 bdev_cache_init();
2481 chrdev_init();
2482}