blob: f74399f5b24303d6c93516196f0338aab41fd17e [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>
Paul Gortmaker630d9c42011-11-16 23:57:37 -050026#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#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>
Nick Pigginceb5bdc2011-01-07 17:50:05 +110036#include <linux/bit_spinlock.h>
37#include <linux/rculist_bl.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -070038#include <linux/prefetch.h>
David Howellsdd179942011-08-16 15:31:30 +010039#include <linux/ratelimit.h>
David Howells07f3f052006-09-30 20:52:18 +020040#include "internal.h"
Al Virob2dba1a2011-11-23 19:26:23 -050041#include "mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Nick Piggin789680d2011-01-07 17:49:30 +110043/*
44 * Usage:
Nick Piggin873feea2011-01-07 17:50:06 +110045 * dcache->d_inode->i_lock protects:
Al Viroa7cca092014-10-26 19:19:16 -040046 * - i_dentry, d_u.d_alias, d_inode of aliases
Nick Pigginceb5bdc2011-01-07 17:50:05 +110047 * dcache_hash_bucket lock protects:
48 * - the dcache hash table
49 * s_anon bl list spinlock protects:
50 * - the s_anon list (see __d_drop)
Nick Piggin23044502011-01-07 17:49:31 +110051 * dcache_lru_lock protects:
52 * - the dcache lru lists and counters
53 * d_lock protects:
54 * - d_flags
55 * - d_name
56 * - d_lru
Nick Pigginb7ab39f2011-01-07 17:49:32 +110057 * - d_count
Nick Pigginda502952011-01-07 17:49:33 +110058 * - d_unhashed()
Nick Piggin2fd6b7f2011-01-07 17:49:34 +110059 * - d_parent and d_subdirs
60 * - childrens' d_child and d_parent
Al Viroa7cca092014-10-26 19:19:16 -040061 * - d_u.d_alias, d_inode
Nick Piggin789680d2011-01-07 17:49:30 +110062 *
63 * Ordering:
Nick Piggin873feea2011-01-07 17:50:06 +110064 * dentry->d_inode->i_lock
Nick Pigginb5c84bf2011-01-07 17:49:38 +110065 * dentry->d_lock
66 * dcache_lru_lock
Nick Pigginceb5bdc2011-01-07 17:50:05 +110067 * dcache_hash_bucket lock
68 * s_anon lock
Nick Piggin789680d2011-01-07 17:49:30 +110069 *
Nick Pigginda502952011-01-07 17:49:33 +110070 * If there is an ancestor relationship:
71 * dentry->d_parent->...->d_parent->d_lock
72 * ...
73 * dentry->d_parent->d_lock
74 * dentry->d_lock
75 *
76 * If no ancestor relationship:
Nick Piggin789680d2011-01-07 17:49:30 +110077 * if (dentry1 < dentry2)
78 * dentry1->d_lock
79 * dentry2->d_lock
80 */
Eric Dumazetfa3536c2006-03-26 01:37:24 -080081int sysctl_vfs_cache_pressure __read_mostly = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082EXPORT_SYMBOL_GPL(sysctl_vfs_cache_pressure);
83
Nick Piggin23044502011-01-07 17:49:31 +110084static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_lru_lock);
Al Viro74c3cbe2007-07-22 08:04:18 -040085__cacheline_aligned_in_smp DEFINE_SEQLOCK(rename_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Nick Piggin949854d2011-01-07 17:49:37 +110087EXPORT_SYMBOL(rename_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Christoph Lametere18b8902006-12-06 20:33:20 -080089static struct kmem_cache *dentry_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Linus Torvalds1da177e2005-04-16 15:20:36 -070091/*
92 * This is the single most critical data structure when it comes
93 * to the dcache: the hashtable for lookups. Somebody should try
94 * to make this good - I've just made it work.
95 *
96 * This hash-function tries to avoid losing too many bits of hash
97 * information, yet avoid using a prime hash-size or similar.
98 */
99#define D_HASHBITS d_hash_shift
100#define D_HASHMASK d_hash_mask
101
Eric Dumazetfa3536c2006-03-26 01:37:24 -0800102static unsigned int d_hash_mask __read_mostly;
103static unsigned int d_hash_shift __read_mostly;
Nick Pigginceb5bdc2011-01-07 17:50:05 +1100104
Linus Torvaldsb07ad992011-04-23 22:32:03 -0700105static struct hlist_bl_head *dentry_hashtable __read_mostly;
Nick Pigginceb5bdc2011-01-07 17:50:05 +1100106
Linus Torvalds8966be92012-03-02 14:23:30 -0800107static inline struct hlist_bl_head *d_hash(const struct dentry *parent,
Linus Torvalds6d7d1a02012-03-19 16:19:53 -0700108 unsigned int hash)
Nick Pigginceb5bdc2011-01-07 17:50:05 +1100109{
Linus Torvalds6d7d1a02012-03-19 16:19:53 -0700110 hash += (unsigned long) parent / L1_CACHE_BYTES;
111 hash = hash + (hash >> D_HASHBITS);
Nick Pigginceb5bdc2011-01-07 17:50:05 +1100112 return dentry_hashtable + (hash & D_HASHMASK);
113}
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115/* Statistics gathering. */
116struct dentry_stat_t dentry_stat = {
117 .age_limit = 45,
118};
119
Nick Piggin3e880fb2011-01-07 17:49:19 +1100120static DEFINE_PER_CPU(unsigned int, nr_dentry);
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400121
122#if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS)
Nick Piggin3e880fb2011-01-07 17:49:19 +1100123static int get_nr_dentry(void)
124{
125 int i;
126 int sum = 0;
127 for_each_possible_cpu(i)
128 sum += per_cpu(nr_dentry, i);
129 return sum < 0 ? 0 : sum;
130}
131
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400132int proc_nr_dentry(ctl_table *table, int write, void __user *buffer,
133 size_t *lenp, loff_t *ppos)
134{
Nick Piggin3e880fb2011-01-07 17:49:19 +1100135 dentry_stat.nr_dentry = get_nr_dentry();
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400136 return proc_dointvec(table, write, buffer, lenp, ppos);
137}
138#endif
139
Linus Torvalds5483f182012-03-04 15:51:42 -0800140/*
141 * Compare 2 name strings, return 0 if they match, otherwise non-zero.
142 * The strings are both count bytes long, and count is non-zero.
143 */
Linus Torvaldse419b4c2012-05-03 10:16:43 -0700144#ifdef CONFIG_DCACHE_WORD_ACCESS
145
146#include <asm/word-at-a-time.h>
147/*
148 * NOTE! 'cs' and 'scount' come from a dentry, so it has a
149 * aligned allocation for this particular component. We don't
150 * strictly need the load_unaligned_zeropad() safety, but it
151 * doesn't hurt either.
152 *
153 * In contrast, 'ct' and 'tcount' can be from a pathname, and do
154 * need the careful unaligned handling.
155 */
Linus Torvaldsda992372012-05-10 12:19:19 -0700156static inline int dentry_string_cmp(const unsigned char *cs, const unsigned char *ct, unsigned tcount)
Linus Torvalds5483f182012-03-04 15:51:42 -0800157{
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -0800158 unsigned long a,b,mask;
159
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -0800160 for (;;) {
Linus Torvaldsb7d3740d2012-05-04 14:59:14 -0700161 a = *(unsigned long *)cs;
Linus Torvaldse419b4c2012-05-03 10:16:43 -0700162 b = load_unaligned_zeropad(ct);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -0800163 if (tcount < sizeof(unsigned long))
164 break;
165 if (unlikely(a != b))
166 return 1;
167 cs += sizeof(unsigned long);
168 ct += sizeof(unsigned long);
169 tcount -= sizeof(unsigned long);
170 if (!tcount)
171 return 0;
172 }
173 mask = ~(~0ul << tcount*8);
174 return unlikely(!!((a ^ b) & mask));
Linus Torvaldse419b4c2012-05-03 10:16:43 -0700175}
176
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -0800177#else
Linus Torvaldse419b4c2012-05-03 10:16:43 -0700178
Linus Torvaldsda992372012-05-10 12:19:19 -0700179static inline int dentry_string_cmp(const unsigned char *cs, const unsigned char *ct, unsigned tcount)
Linus Torvaldse419b4c2012-05-03 10:16:43 -0700180{
Linus Torvalds5483f182012-03-04 15:51:42 -0800181 do {
182 if (*cs != *ct)
183 return 1;
184 cs++;
185 ct++;
186 tcount--;
187 } while (tcount);
188 return 0;
189}
190
Linus Torvaldse419b4c2012-05-03 10:16:43 -0700191#endif
192
Al Viro443c3682014-09-29 14:54:27 -0400193struct external_name {
194 union {
195 atomic_t count;
196 struct rcu_head head;
197 } u;
198 unsigned char name[];
199};
200
201static inline struct external_name *external_name(struct dentry *dentry)
202{
203 return container_of(dentry->d_name.name, struct external_name, name[0]);
204}
205
Linus Torvaldsda992372012-05-10 12:19:19 -0700206static inline int dentry_cmp(const struct dentry *dentry, const unsigned char *ct, unsigned tcount)
207{
Linus Torvalds8c27a1e2012-05-21 16:14:04 -0700208 const unsigned char *cs;
Linus Torvaldsda992372012-05-10 12:19:19 -0700209 /*
210 * Be careful about RCU walk racing with rename:
211 * use ACCESS_ONCE to fetch the name pointer.
212 *
213 * NOTE! Even if a rename will mean that the length
214 * was not loaded atomically, we don't care. The
215 * RCU walk will check the sequence count eventually,
216 * and catch it. And we won't overrun the buffer,
217 * because we're reading the name pointer atomically,
218 * and a dentry name is guaranteed to be properly
219 * terminated with a NUL byte.
220 *
221 * End result: even if 'len' is wrong, we'll exit
222 * early because the data cannot match (there can
223 * be no NUL in the ct/tcount data)
224 */
Linus Torvalds8c27a1e2012-05-21 16:14:04 -0700225 cs = ACCESS_ONCE(dentry->d_name.name);
226 smp_read_barrier_depends();
227 return dentry_string_cmp(cs, ct, tcount);
Linus Torvaldsda992372012-05-10 12:19:19 -0700228}
229
Christoph Hellwig9c82ab92010-10-10 05:36:22 -0400230static void __d_free(struct rcu_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
Christoph Hellwig9c82ab92010-10-10 05:36:22 -0400232 struct dentry *dentry = container_of(head, struct dentry, d_u.d_rcu);
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 kmem_cache_free(dentry_cache, dentry);
235}
236
Al Viro443c3682014-09-29 14:54:27 -0400237static void __d_free_external(struct rcu_head *head)
238{
239 struct dentry *dentry = container_of(head, struct dentry, d_u.d_rcu);
240 kfree(external_name(dentry));
241 kmem_cache_free(dentry_cache, dentry);
242}
243
Al Viroedb6ff12014-04-29 23:40:14 -0400244static void dentry_free(struct dentry *dentry)
245{
Al Viro443c3682014-09-29 14:54:27 -0400246 if (unlikely(dname_external(dentry))) {
247 struct external_name *p = external_name(dentry);
248 if (likely(atomic_dec_and_test(&p->u.count))) {
249 call_rcu(&dentry->d_u.d_rcu, __d_free_external);
250 return;
251 }
252 }
Al Viroedb6ff12014-04-29 23:40:14 -0400253 /* if dentry was never visible to RCU, immediate free is OK */
254 if (!(dentry->d_flags & DCACHE_RCUACCESS))
255 __d_free(&dentry->d_u.d_rcu);
256 else
257 call_rcu(&dentry->d_u.d_rcu, __d_free);
258}
259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260/*
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100261 * no locks, please.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 */
263static void d_free(struct dentry *dentry)
264{
Al Viroa7cca092014-10-26 19:19:16 -0400265 WARN_ON(!list_empty(&dentry->d_u.d_alias));
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100266 BUG_ON(dentry->d_count);
Nick Piggin3e880fb2011-01-07 17:49:19 +1100267 this_cpu_dec(nr_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 if (dentry->d_op && dentry->d_op->d_release)
269 dentry->d_op->d_release(dentry);
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400270
Al Viroedb6ff12014-04-29 23:40:14 -0400271 dentry_free(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272}
273
Al Viro6e71d852017-07-07 14:51:19 -0400274void take_dentry_name_snapshot(struct name_snapshot *name, struct dentry *dentry)
275{
276 spin_lock(&dentry->d_lock);
277 if (unlikely(dname_external(dentry))) {
278 u32 len;
279 char *p;
280
281 for (;;) {
282 len = dentry->d_name.len;
283 spin_unlock(&dentry->d_lock);
284
285 p = kmalloc(len + 1, GFP_KERNEL | __GFP_NOFAIL);
286
287 spin_lock(&dentry->d_lock);
288 if (dentry->d_name.len <= len)
289 break;
290 kfree(p);
291 }
292 memcpy(p, dentry->d_name.name, dentry->d_name.len + 1);
293 spin_unlock(&dentry->d_lock);
294
295 name->name = p;
296 } else {
297 memcpy(name->inline_name, dentry->d_iname, DNAME_INLINE_LEN);
298 spin_unlock(&dentry->d_lock);
299 name->name = name->inline_name;
300 }
301}
302EXPORT_SYMBOL(take_dentry_name_snapshot);
303
304void release_dentry_name_snapshot(struct name_snapshot *name)
305{
306 if (unlikely(name->name != name->inline_name))
307 kfree(name->name);
308}
309EXPORT_SYMBOL(release_dentry_name_snapshot);
310
Nick Piggin31e6b012011-01-07 17:49:52 +1100311/**
312 * dentry_rcuwalk_barrier - invalidate in-progress rcu-walk lookups
Randy Dunlapff5fdb62011-01-22 20:16:06 -0800313 * @dentry: the target dentry
Nick Piggin31e6b012011-01-07 17:49:52 +1100314 * After this call, in-progress rcu-walk path lookup will fail. This
315 * should be called after unhashing, and after changing d_inode (if
316 * the dentry has not already been unhashed).
317 */
318static inline void dentry_rcuwalk_barrier(struct dentry *dentry)
319{
320 assert_spin_locked(&dentry->d_lock);
321 /* Go through a barrier */
322 write_seqcount_barrier(&dentry->d_seq);
323}
324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325/*
326 * Release the dentry's inode, using the filesystem
Nick Piggin31e6b012011-01-07 17:49:52 +1100327 * d_iput() operation if defined. Dentry has no refcount
328 * and is unhashed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800330static void dentry_iput(struct dentry * dentry)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200331 __releases(dentry->d_lock)
Nick Piggin873feea2011-01-07 17:50:06 +1100332 __releases(dentry->d_inode->i_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
334 struct inode *inode = dentry->d_inode;
335 if (inode) {
336 dentry->d_inode = NULL;
Al Viroa7cca092014-10-26 19:19:16 -0400337 list_del_init(&dentry->d_u.d_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 spin_unlock(&dentry->d_lock);
Nick Piggin873feea2011-01-07 17:50:06 +1100339 spin_unlock(&inode->i_lock);
Linus Torvaldsf805fbd2005-09-19 19:54:29 -0700340 if (!inode->i_nlink)
341 fsnotify_inoderemove(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 if (dentry->d_op && dentry->d_op->d_iput)
343 dentry->d_op->d_iput(dentry, inode);
344 else
345 iput(inode);
346 } else {
347 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 }
349}
350
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700351/*
Nick Piggin31e6b012011-01-07 17:49:52 +1100352 * Release the dentry's inode, using the filesystem
353 * d_iput() operation if defined. dentry remains in-use.
354 */
355static void dentry_unlink_inode(struct dentry * dentry)
356 __releases(dentry->d_lock)
Nick Piggin873feea2011-01-07 17:50:06 +1100357 __releases(dentry->d_inode->i_lock)
Nick Piggin31e6b012011-01-07 17:49:52 +1100358{
359 struct inode *inode = dentry->d_inode;
360 dentry->d_inode = NULL;
Al Viroa7cca092014-10-26 19:19:16 -0400361 list_del_init(&dentry->d_u.d_alias);
Nick Piggin31e6b012011-01-07 17:49:52 +1100362 dentry_rcuwalk_barrier(dentry);
363 spin_unlock(&dentry->d_lock);
Nick Piggin873feea2011-01-07 17:50:06 +1100364 spin_unlock(&inode->i_lock);
Nick Piggin31e6b012011-01-07 17:49:52 +1100365 if (!inode->i_nlink)
366 fsnotify_inoderemove(inode);
367 if (dentry->d_op && dentry->d_op->d_iput)
368 dentry->d_op->d_iput(dentry, inode);
369 else
370 iput(inode);
371}
372
373/*
Sage Weilf0023bc2011-10-28 10:02:42 -0700374 * dentry_lru_(add|del|prune|move_tail) must be called with d_lock held.
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700375 */
376static void dentry_lru_add(struct dentry *dentry)
377{
Christoph Hellwiga4633352010-10-10 05:36:26 -0400378 if (list_empty(&dentry->d_lru)) {
Nick Piggin23044502011-01-07 17:49:31 +1100379 spin_lock(&dcache_lru_lock);
Christoph Hellwiga4633352010-10-10 05:36:26 -0400380 list_add(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
381 dentry->d_sb->s_nr_dentry_unused++;
Nick Piggin86c87492011-01-07 17:49:18 +1100382 dentry_stat.nr_unused++;
Nick Piggin23044502011-01-07 17:49:31 +1100383 spin_unlock(&dcache_lru_lock);
Christoph Hellwiga4633352010-10-10 05:36:26 -0400384 }
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700385}
386
Nick Piggin23044502011-01-07 17:49:31 +1100387static void __dentry_lru_del(struct dentry *dentry)
388{
389 list_del_init(&dentry->d_lru);
Miklos Szeredieaf5f902012-01-10 18:22:25 +0100390 dentry->d_flags &= ~DCACHE_SHRINK_LIST;
Nick Piggin23044502011-01-07 17:49:31 +1100391 dentry->d_sb->s_nr_dentry_unused--;
392 dentry_stat.nr_unused--;
393}
394
Sage Weilf0023bc2011-10-28 10:02:42 -0700395/*
396 * Remove a dentry with references from the LRU.
397 */
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700398static void dentry_lru_del(struct dentry *dentry)
399{
400 if (!list_empty(&dentry->d_lru)) {
Nick Piggin23044502011-01-07 17:49:31 +1100401 spin_lock(&dcache_lru_lock);
402 __dentry_lru_del(dentry);
403 spin_unlock(&dcache_lru_lock);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700404 }
405}
406
Sage Weilf0023bc2011-10-28 10:02:42 -0700407/*
408 * Remove a dentry that is unreferenced and about to be pruned
409 * (unhashed and destroyed) from the LRU, and inform the file system.
410 * This wrapper should be called _prior_ to unhashing a victim dentry.
411 */
412static void dentry_lru_prune(struct dentry *dentry)
413{
414 if (!list_empty(&dentry->d_lru)) {
415 if (dentry->d_flags & DCACHE_OP_PRUNE)
416 dentry->d_op->d_prune(dentry);
417
418 spin_lock(&dcache_lru_lock);
419 __dentry_lru_del(dentry);
420 spin_unlock(&dcache_lru_lock);
421 }
422}
423
Dave Chinnerb48f03b2011-08-23 18:56:24 +1000424static void dentry_lru_move_list(struct dentry *dentry, struct list_head *list)
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700425{
Nick Piggin23044502011-01-07 17:49:31 +1100426 spin_lock(&dcache_lru_lock);
Christoph Hellwiga4633352010-10-10 05:36:26 -0400427 if (list_empty(&dentry->d_lru)) {
Dave Chinnerb48f03b2011-08-23 18:56:24 +1000428 list_add_tail(&dentry->d_lru, list);
Christoph Hellwiga4633352010-10-10 05:36:26 -0400429 dentry->d_sb->s_nr_dentry_unused++;
Nick Piggin86c87492011-01-07 17:49:18 +1100430 dentry_stat.nr_unused++;
Christoph Hellwiga4633352010-10-10 05:36:26 -0400431 } else {
Dave Chinnerb48f03b2011-08-23 18:56:24 +1000432 list_move_tail(&dentry->d_lru, list);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700433 }
Nick Piggin23044502011-01-07 17:49:31 +1100434 spin_unlock(&dcache_lru_lock);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700435}
436
Miklos Szeredid52b9082007-05-08 00:23:46 -0700437/**
438 * d_kill - kill dentry and return parent
439 * @dentry: dentry to kill
Randy Dunlapff5fdb62011-01-22 20:16:06 -0800440 * @parent: parent dentry
Miklos Szeredid52b9082007-05-08 00:23:46 -0700441 *
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200442 * The dentry must already be unhashed and removed from the LRU.
Miklos Szeredid52b9082007-05-08 00:23:46 -0700443 *
444 * If this is the root of the dentry tree, return NULL.
Nick Piggin23044502011-01-07 17:49:31 +1100445 *
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100446 * dentry->d_lock and parent->d_lock must be held by caller, and are dropped by
447 * d_kill.
Miklos Szeredid52b9082007-05-08 00:23:46 -0700448 */
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100449static struct dentry *d_kill(struct dentry *dentry, struct dentry *parent)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200450 __releases(dentry->d_lock)
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100451 __releases(parent->d_lock)
Nick Piggin873feea2011-01-07 17:50:06 +1100452 __releases(dentry->d_inode->i_lock)
Miklos Szeredid52b9082007-05-08 00:23:46 -0700453{
Al Viro01306a52014-10-26 19:31:10 -0400454 __list_del_entry(&dentry->d_child);
Trond Myklebustc83ce982011-03-15 13:36:43 -0400455 /*
Al Viro01306a52014-10-26 19:31:10 -0400456 * Inform ascending readers that we are no longer attached to the
Trond Myklebustc83ce982011-03-15 13:36:43 -0400457 * dentry tree
458 */
Miklos Szeredibd92dd0d2012-09-17 22:31:38 +0200459 dentry->d_flags |= DCACHE_DENTRY_KILLED;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100460 if (parent)
461 spin_unlock(&parent->d_lock);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700462 dentry_iput(dentry);
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100463 /*
464 * dentry_iput drops the locks, at which point nobody (except
465 * transient RCU lookups) can reach this dentry.
466 */
Miklos Szeredid52b9082007-05-08 00:23:46 -0700467 d_free(dentry);
OGAWA Hirofumi871c0062008-10-16 07:50:27 +0900468 return parent;
Miklos Szeredid52b9082007-05-08 00:23:46 -0700469}
470
David Howellsc6627c62011-06-07 14:09:20 +0100471/*
472 * Unhash a dentry without inserting an RCU walk barrier or checking that
473 * dentry->d_lock is locked. The caller must take care of that, if
474 * appropriate.
475 */
476static void __d_shrink(struct dentry *dentry)
477{
478 if (!d_unhashed(dentry)) {
479 struct hlist_bl_head *b;
480 if (unlikely(dentry->d_flags & DCACHE_DISCONNECTED))
481 b = &dentry->d_sb->s_anon;
482 else
483 b = d_hash(dentry->d_parent, dentry->d_name.hash);
484
485 hlist_bl_lock(b);
486 __hlist_bl_del(&dentry->d_hash);
487 dentry->d_hash.pprev = NULL;
488 hlist_bl_unlock(b);
489 }
490}
491
Nick Piggin789680d2011-01-07 17:49:30 +1100492/**
493 * d_drop - drop a dentry
494 * @dentry: dentry to drop
495 *
496 * d_drop() unhashes the entry from the parent dentry hashes, so that it won't
497 * be found through a VFS lookup any more. Note that this is different from
498 * deleting the dentry - d_delete will try to mark the dentry negative if
499 * possible, giving a successful _negative_ lookup, while d_drop will
500 * just make the cache lookup fail.
501 *
502 * d_drop() is used mainly for stuff that wants to invalidate a dentry for some
503 * reason (NFS timeouts or autofs deletes).
504 *
505 * __d_drop requires dentry->d_lock.
506 */
507void __d_drop(struct dentry *dentry)
508{
Linus Torvaldsdea36672011-04-24 07:58:46 -0700509 if (!d_unhashed(dentry)) {
David Howellsc6627c62011-06-07 14:09:20 +0100510 __d_shrink(dentry);
Linus Torvaldsdea36672011-04-24 07:58:46 -0700511 dentry_rcuwalk_barrier(dentry);
Nick Piggin789680d2011-01-07 17:49:30 +1100512 }
513}
514EXPORT_SYMBOL(__d_drop);
515
516void d_drop(struct dentry *dentry)
517{
Nick Piggin789680d2011-01-07 17:49:30 +1100518 spin_lock(&dentry->d_lock);
519 __d_drop(dentry);
520 spin_unlock(&dentry->d_lock);
Nick Piggin789680d2011-01-07 17:49:30 +1100521}
522EXPORT_SYMBOL(d_drop);
523
Nick Piggin77812a12011-01-07 17:49:48 +1100524/*
Josef Bacik44396f42011-05-31 11:58:49 -0400525 * d_clear_need_lookup - drop a dentry from cache and clear the need lookup flag
526 * @dentry: dentry to drop
527 *
528 * This is called when we do a lookup on a placeholder dentry that needed to be
529 * looked up. The dentry should have been hashed in order for it to be found by
530 * the lookup code, but now needs to be unhashed while we do the actual lookup
531 * and clear the DCACHE_NEED_LOOKUP flag.
532 */
533void d_clear_need_lookup(struct dentry *dentry)
534{
535 spin_lock(&dentry->d_lock);
536 __d_drop(dentry);
537 dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
538 spin_unlock(&dentry->d_lock);
539}
540EXPORT_SYMBOL(d_clear_need_lookup);
541
542/*
Nick Piggin77812a12011-01-07 17:49:48 +1100543 * Finish off a dentry we've decided to kill.
544 * dentry->d_lock must be held, returns with it unlocked.
545 * If ref is non-zero, then decrement the refcount too.
546 * Returns dentry requiring refcount drop, or NULL if we're done.
547 */
548static inline struct dentry *dentry_kill(struct dentry *dentry, int ref)
549 __releases(dentry->d_lock)
550{
Nick Piggin873feea2011-01-07 17:50:06 +1100551 struct inode *inode;
Nick Piggin77812a12011-01-07 17:49:48 +1100552 struct dentry *parent;
553
Nick Piggin873feea2011-01-07 17:50:06 +1100554 inode = dentry->d_inode;
555 if (inode && !spin_trylock(&inode->i_lock)) {
Nick Piggin77812a12011-01-07 17:49:48 +1100556relock:
557 spin_unlock(&dentry->d_lock);
558 cpu_relax();
559 return dentry; /* try again with same dentry */
560 }
561 if (IS_ROOT(dentry))
562 parent = NULL;
563 else
564 parent = dentry->d_parent;
565 if (parent && !spin_trylock(&parent->d_lock)) {
Nick Piggin873feea2011-01-07 17:50:06 +1100566 if (inode)
567 spin_unlock(&inode->i_lock);
Nick Piggin77812a12011-01-07 17:49:48 +1100568 goto relock;
569 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100570
Nick Piggin77812a12011-01-07 17:49:48 +1100571 if (ref)
572 dentry->d_count--;
Sage Weilf0023bc2011-10-28 10:02:42 -0700573 /*
574 * if dentry was on the d_lru list delete it from there.
575 * inform the fs via d_prune that this dentry is about to be
576 * unhashed and destroyed.
577 */
578 dentry_lru_prune(dentry);
Nick Piggin77812a12011-01-07 17:49:48 +1100579 /* if it was on the hash then remove it */
580 __d_drop(dentry);
581 return d_kill(dentry, parent);
582}
583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584/*
585 * This is dput
586 *
587 * This is complicated by the fact that we do not want to put
588 * dentries that are no longer on any hash chain on the unused
589 * list: we'd much rather just get rid of them immediately.
590 *
591 * However, that implies that we have to traverse the dentry
592 * tree upwards to the parents which might _also_ now be
593 * scheduled for deletion (it may have been only waiting for
594 * its last child to go away).
595 *
596 * This tail recursion is done by hand as we don't want to depend
597 * on the compiler to always get this right (gcc generally doesn't).
598 * Real recursion would eat up our stack space.
599 */
600
601/*
602 * dput - release a dentry
603 * @dentry: dentry to release
604 *
605 * Release a dentry. This will drop the usage count and if appropriate
606 * call the dentry unlink method as well as removing it from the queues and
607 * releasing its resources. If the parent dentries were scheduled for release
608 * they too may now get deleted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610void dput(struct dentry *dentry)
611{
612 if (!dentry)
613 return;
614
615repeat:
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100616 if (dentry->d_count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 spin_lock(&dentry->d_lock);
Nick Piggin61f3dee2011-01-07 17:49:40 +1100619 BUG_ON(!dentry->d_count);
620 if (dentry->d_count > 1) {
621 dentry->d_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return;
624 }
625
Nick Pigginfb045ad2011-01-07 17:49:55 +1100626 if (dentry->d_flags & DCACHE_OP_DELETE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 if (dentry->d_op->d_delete(dentry))
Nick Piggin61f3dee2011-01-07 17:49:40 +1100628 goto kill_it;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 }
Nick Piggin265ac902010-10-10 05:36:24 -0400630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 /* Unreachable? Get rid of it */
632 if (d_unhashed(dentry))
633 goto kill_it;
Nick Piggin265ac902010-10-10 05:36:24 -0400634
Josef Bacik44396f42011-05-31 11:58:49 -0400635 /*
636 * If this dentry needs lookup, don't set the referenced flag so that it
637 * is more likely to be cleaned up by the dcache shrinker in case of
638 * memory pressure.
639 */
640 if (!d_need_lookup(dentry))
641 dentry->d_flags |= DCACHE_REFERENCED;
Christoph Hellwiga4633352010-10-10 05:36:26 -0400642 dentry_lru_add(dentry);
Nick Piggin265ac902010-10-10 05:36:24 -0400643
Nick Piggin61f3dee2011-01-07 17:49:40 +1100644 dentry->d_count--;
645 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 return;
647
Miklos Szeredid52b9082007-05-08 00:23:46 -0700648kill_it:
Nick Piggin77812a12011-01-07 17:49:48 +1100649 dentry = dentry_kill(dentry, 1);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700650 if (dentry)
651 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700653EXPORT_SYMBOL(dput);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655/**
656 * d_invalidate - invalidate a dentry
657 * @dentry: dentry to invalidate
658 *
659 * Try to invalidate the dentry if it turns out to be
660 * possible. If there are other dentries that can be
661 * reached through this one we can't delete it and we
662 * return -EBUSY. On success we return 0.
663 *
664 * no dcache lock.
665 */
666
667int d_invalidate(struct dentry * dentry)
668{
669 /*
670 * If it's already been dropped, return OK.
671 */
Nick Pigginda502952011-01-07 17:49:33 +1100672 spin_lock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 if (d_unhashed(dentry)) {
Nick Pigginda502952011-01-07 17:49:33 +1100674 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 return 0;
676 }
677 /*
678 * Check whether to do a partial shrink_dcache
679 * to get rid of unused child entries.
680 */
681 if (!list_empty(&dentry->d_subdirs)) {
Nick Pigginda502952011-01-07 17:49:33 +1100682 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 shrink_dcache_parent(dentry);
Nick Pigginda502952011-01-07 17:49:33 +1100684 spin_lock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 }
686
687 /*
688 * Somebody else still using it?
689 *
690 * If it's a directory, we can't drop it
691 * for fear of somebody re-populating it
692 * with children (even though dropping it
693 * would make it unreachable from the root,
694 * we might still populate it if it was a
695 * working directory or similar).
Al Viro50e69632011-11-07 16:39:57 +0000696 * We also need to leave mountpoints alone,
697 * directory or not.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 */
Al Viro50e69632011-11-07 16:39:57 +0000699 if (dentry->d_count > 1 && dentry->d_inode) {
700 if (S_ISDIR(dentry->d_inode->i_mode) || d_mountpoint(dentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 return -EBUSY;
703 }
704 }
705
706 __d_drop(dentry);
707 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 return 0;
709}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700710EXPORT_SYMBOL(d_invalidate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100712/* This must be called with d_lock held */
Nick Piggindc0474b2011-01-07 17:49:43 +1100713static inline void __dget_dlock(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100715 dentry->d_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716}
717
Nick Piggindc0474b2011-01-07 17:49:43 +1100718static inline void __dget(struct dentry *dentry)
Nick Piggin23044502011-01-07 17:49:31 +1100719{
Nick Piggin23044502011-01-07 17:49:31 +1100720 spin_lock(&dentry->d_lock);
Nick Piggindc0474b2011-01-07 17:49:43 +1100721 __dget_dlock(dentry);
Nick Piggin23044502011-01-07 17:49:31 +1100722 spin_unlock(&dentry->d_lock);
Nick Piggin23044502011-01-07 17:49:31 +1100723}
724
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100725struct dentry *dget_parent(struct dentry *dentry)
726{
727 struct dentry *ret;
728
729repeat:
Nick Piggina734eb42011-01-07 17:49:44 +1100730 /*
731 * Don't need rcu_dereference because we re-check it was correct under
732 * the lock.
733 */
734 rcu_read_lock();
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100735 ret = dentry->d_parent;
Nick Piggina734eb42011-01-07 17:49:44 +1100736 spin_lock(&ret->d_lock);
737 if (unlikely(ret != dentry->d_parent)) {
738 spin_unlock(&ret->d_lock);
739 rcu_read_unlock();
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100740 goto repeat;
741 }
Nick Piggina734eb42011-01-07 17:49:44 +1100742 rcu_read_unlock();
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100743 BUG_ON(!ret->d_count);
744 ret->d_count++;
745 spin_unlock(&ret->d_lock);
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100746 return ret;
747}
748EXPORT_SYMBOL(dget_parent);
749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750/**
751 * d_find_alias - grab a hashed alias of inode
752 * @inode: inode in question
753 * @want_discon: flag, used by d_splice_alias, to request
754 * that only a DISCONNECTED alias be returned.
755 *
756 * If inode has a hashed alias, or is a directory and has any alias,
757 * acquire the reference to alias and return it. Otherwise return NULL.
758 * Notice that if inode is a directory there can be only one alias and
759 * it can be unhashed only if it has no children, or if it is the root
760 * of a filesystem.
761 *
NeilBrown21c0d8f2006-10-04 02:16:16 -0700762 * If the inode has an IS_ROOT, DCACHE_DISCONNECTED alias, then prefer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 * any other hashed alias over that one unless @want_discon is set,
NeilBrown21c0d8f2006-10-04 02:16:16 -0700764 * in which case only return an IS_ROOT, DCACHE_DISCONNECTED alias.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 */
Nick Pigginda502952011-01-07 17:49:33 +1100766static struct dentry *__d_find_alias(struct inode *inode, int want_discon)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
Nick Pigginda502952011-01-07 17:49:33 +1100768 struct dentry *alias, *discon_alias;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Nick Pigginda502952011-01-07 17:49:33 +1100770again:
771 discon_alias = NULL;
Al Viroa7cca092014-10-26 19:19:16 -0400772 list_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
Nick Pigginda502952011-01-07 17:49:33 +1100773 spin_lock(&alias->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
NeilBrown21c0d8f2006-10-04 02:16:16 -0700775 if (IS_ROOT(alias) &&
Nick Pigginda502952011-01-07 17:49:33 +1100776 (alias->d_flags & DCACHE_DISCONNECTED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 discon_alias = alias;
Nick Pigginda502952011-01-07 17:49:33 +1100778 } else if (!want_discon) {
Nick Piggindc0474b2011-01-07 17:49:43 +1100779 __dget_dlock(alias);
Nick Pigginda502952011-01-07 17:49:33 +1100780 spin_unlock(&alias->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 return alias;
782 }
783 }
Nick Pigginda502952011-01-07 17:49:33 +1100784 spin_unlock(&alias->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 }
Nick Pigginda502952011-01-07 17:49:33 +1100786 if (discon_alias) {
787 alias = discon_alias;
788 spin_lock(&alias->d_lock);
789 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
790 if (IS_ROOT(alias) &&
791 (alias->d_flags & DCACHE_DISCONNECTED)) {
Nick Piggindc0474b2011-01-07 17:49:43 +1100792 __dget_dlock(alias);
Nick Pigginda502952011-01-07 17:49:33 +1100793 spin_unlock(&alias->d_lock);
794 return alias;
795 }
796 }
797 spin_unlock(&alias->d_lock);
798 goto again;
799 }
800 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801}
802
Nick Pigginda502952011-01-07 17:49:33 +1100803struct dentry *d_find_alias(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
David Howells214fda12006-03-25 03:06:36 -0800805 struct dentry *de = NULL;
806
807 if (!list_empty(&inode->i_dentry)) {
Nick Piggin873feea2011-01-07 17:50:06 +1100808 spin_lock(&inode->i_lock);
David Howells214fda12006-03-25 03:06:36 -0800809 de = __d_find_alias(inode, 0);
Nick Piggin873feea2011-01-07 17:50:06 +1100810 spin_unlock(&inode->i_lock);
David Howells214fda12006-03-25 03:06:36 -0800811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 return de;
813}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700814EXPORT_SYMBOL(d_find_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
816/*
817 * Try to kill dentries associated with this inode.
818 * WARNING: you must own a reference to inode.
819 */
820void d_prune_aliases(struct inode *inode)
821{
Domen Puncer0cdca3f2005-09-10 00:27:07 -0700822 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823restart:
Nick Piggin873feea2011-01-07 17:50:06 +1100824 spin_lock(&inode->i_lock);
Al Viroa7cca092014-10-26 19:19:16 -0400825 list_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 spin_lock(&dentry->d_lock);
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100827 if (!dentry->d_count) {
Nick Piggindc0474b2011-01-07 17:49:43 +1100828 __dget_dlock(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 __d_drop(dentry);
830 spin_unlock(&dentry->d_lock);
Nick Piggin873feea2011-01-07 17:50:06 +1100831 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 dput(dentry);
833 goto restart;
834 }
835 spin_unlock(&dentry->d_lock);
836 }
Nick Piggin873feea2011-01-07 17:50:06 +1100837 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700839EXPORT_SYMBOL(d_prune_aliases);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
841/*
Nick Piggin77812a12011-01-07 17:49:48 +1100842 * Try to throw away a dentry - free the inode, dput the parent.
843 * Requires dentry->d_lock is held, and dentry->d_count == 0.
844 * Releases dentry->d_lock.
Andrew Mortond702ccb2006-06-22 14:47:31 -0700845 *
Nick Piggin77812a12011-01-07 17:49:48 +1100846 * This may fail if locks cannot be acquired no problem, just try again.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 */
Nick Piggin77812a12011-01-07 17:49:48 +1100848static void try_prune_one_dentry(struct dentry *dentry)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200849 __releases(dentry->d_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850{
Nick Piggin77812a12011-01-07 17:49:48 +1100851 struct dentry *parent;
Miklos Szeredid52b9082007-05-08 00:23:46 -0700852
Nick Piggin77812a12011-01-07 17:49:48 +1100853 parent = dentry_kill(dentry, 0);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700854 /*
Nick Piggin77812a12011-01-07 17:49:48 +1100855 * If dentry_kill returns NULL, we have nothing more to do.
856 * if it returns the same dentry, trylocks failed. In either
857 * case, just loop again.
858 *
859 * Otherwise, we need to prune ancestors too. This is necessary
860 * to prevent quadratic behavior of shrink_dcache_parent(), but
861 * is also expected to be beneficial in reducing dentry cache
862 * fragmentation.
Miklos Szeredid52b9082007-05-08 00:23:46 -0700863 */
Nick Piggin77812a12011-01-07 17:49:48 +1100864 if (!parent)
865 return;
866 if (parent == dentry)
867 return;
868
869 /* Prune ancestors. */
870 dentry = parent;
Miklos Szeredid52b9082007-05-08 00:23:46 -0700871 while (dentry) {
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100872 spin_lock(&dentry->d_lock);
Nick Piggin89e60542011-01-07 17:49:45 +1100873 if (dentry->d_count > 1) {
874 dentry->d_count--;
875 spin_unlock(&dentry->d_lock);
876 return;
877 }
Nick Piggin77812a12011-01-07 17:49:48 +1100878 dentry = dentry_kill(dentry, 1);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880}
881
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400882static void shrink_dentry_list(struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883{
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700884 struct dentry *dentry;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700885
Nick Pigginec336792011-01-07 17:49:47 +1100886 rcu_read_lock();
887 for (;;) {
Nick Pigginec336792011-01-07 17:49:47 +1100888 dentry = list_entry_rcu(list->prev, struct dentry, d_lru);
889 if (&dentry->d_lru == list)
890 break; /* empty */
891 spin_lock(&dentry->d_lock);
892 if (dentry != list_entry(list->prev, struct dentry, d_lru)) {
893 spin_unlock(&dentry->d_lock);
Nick Piggin23044502011-01-07 17:49:31 +1100894 continue;
895 }
896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 /*
898 * We found an inuse dentry which was not removed from
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700899 * the LRU because of laziness during lookup. Do not free
900 * it - just keep it off the LRU list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 */
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100902 if (dentry->d_count) {
Nick Pigginec336792011-01-07 17:49:47 +1100903 dentry_lru_del(dentry);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700904 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 continue;
906 }
Nick Pigginec336792011-01-07 17:49:47 +1100907
Nick Pigginec336792011-01-07 17:49:47 +1100908 rcu_read_unlock();
Nick Piggin77812a12011-01-07 17:49:48 +1100909
910 try_prune_one_dentry(dentry);
911
Nick Pigginec336792011-01-07 17:49:47 +1100912 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 }
Nick Pigginec336792011-01-07 17:49:47 +1100914 rcu_read_unlock();
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400915}
916
917/**
Dave Chinnerb48f03b2011-08-23 18:56:24 +1000918 * prune_dcache_sb - shrink the dcache
919 * @sb: superblock
920 * @count: number of entries to try to free
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400921 *
Dave Chinnerb48f03b2011-08-23 18:56:24 +1000922 * Attempt to shrink the superblock dcache LRU by @count entries. This is
923 * done when we need more memory an called from the superblock shrinker
924 * function.
925 *
926 * This function may fail to free any resources if all the dentries are in
927 * use.
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400928 */
Dave Chinnerb48f03b2011-08-23 18:56:24 +1000929void prune_dcache_sb(struct super_block *sb, int count)
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400930{
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400931 struct dentry *dentry;
932 LIST_HEAD(referenced);
933 LIST_HEAD(tmp);
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400934
Nick Piggin23044502011-01-07 17:49:31 +1100935relock:
936 spin_lock(&dcache_lru_lock);
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400937 while (!list_empty(&sb->s_dentry_lru)) {
938 dentry = list_entry(sb->s_dentry_lru.prev,
939 struct dentry, d_lru);
940 BUG_ON(dentry->d_sb != sb);
941
Nick Piggin23044502011-01-07 17:49:31 +1100942 if (!spin_trylock(&dentry->d_lock)) {
943 spin_unlock(&dcache_lru_lock);
944 cpu_relax();
945 goto relock;
946 }
947
Dave Chinnerb48f03b2011-08-23 18:56:24 +1000948 if (dentry->d_flags & DCACHE_REFERENCED) {
Nick Piggin23044502011-01-07 17:49:31 +1100949 dentry->d_flags &= ~DCACHE_REFERENCED;
950 list_move(&dentry->d_lru, &referenced);
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400951 spin_unlock(&dentry->d_lock);
Nick Piggin23044502011-01-07 17:49:31 +1100952 } else {
953 list_move_tail(&dentry->d_lru, &tmp);
Miklos Szeredieaf5f902012-01-10 18:22:25 +0100954 dentry->d_flags |= DCACHE_SHRINK_LIST;
Nick Piggin23044502011-01-07 17:49:31 +1100955 spin_unlock(&dentry->d_lock);
Dave Chinnerb0d40c92011-07-08 14:14:42 +1000956 if (!--count)
Nick Piggin23044502011-01-07 17:49:31 +1100957 break;
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400958 }
Nick Pigginec336792011-01-07 17:49:47 +1100959 cond_resched_lock(&dcache_lru_lock);
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400960 }
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700961 if (!list_empty(&referenced))
962 list_splice(&referenced, &sb->s_dentry_lru);
Nick Piggin23044502011-01-07 17:49:31 +1100963 spin_unlock(&dcache_lru_lock);
Nick Pigginec336792011-01-07 17:49:47 +1100964
965 shrink_dentry_list(&tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966}
967
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700968/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 * shrink_dcache_sb - shrink dcache for a superblock
970 * @sb: superblock
971 *
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400972 * Shrink the dcache for the specified super block. This is used to free
973 * the dcache before unmounting a file system.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 */
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400975void shrink_dcache_sb(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400977 LIST_HEAD(tmp);
978
Nick Piggin23044502011-01-07 17:49:31 +1100979 spin_lock(&dcache_lru_lock);
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400980 while (!list_empty(&sb->s_dentry_lru)) {
981 list_splice_init(&sb->s_dentry_lru, &tmp);
Nick Pigginec336792011-01-07 17:49:47 +1100982 spin_unlock(&dcache_lru_lock);
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400983 shrink_dentry_list(&tmp);
Nick Pigginec336792011-01-07 17:49:47 +1100984 spin_lock(&dcache_lru_lock);
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400985 }
Nick Piggin23044502011-01-07 17:49:31 +1100986 spin_unlock(&dcache_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700988EXPORT_SYMBOL(shrink_dcache_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
990/*
David Howellsc636ebd2006-10-11 01:22:19 -0700991 * destroy a single subtree of dentries for unmount
992 * - see the comments on shrink_dcache_for_umount() for a description of the
993 * locking
994 */
995static void shrink_dcache_for_umount_subtree(struct dentry *dentry)
996{
997 struct dentry *parent;
998
999 BUG_ON(!IS_ROOT(dentry));
1000
David Howellsc636ebd2006-10-11 01:22:19 -07001001 for (;;) {
1002 /* descend to the first leaf in the current subtree */
David Howells43c1c9c2011-06-07 14:09:30 +01001003 while (!list_empty(&dentry->d_subdirs))
David Howellsc636ebd2006-10-11 01:22:19 -07001004 dentry = list_entry(dentry->d_subdirs.next,
Al Viroa7cca092014-10-26 19:19:16 -04001005 struct dentry, d_child);
David Howellsc636ebd2006-10-11 01:22:19 -07001006
1007 /* consume the dentries from this leaf up through its parents
1008 * until we find one with children or run out altogether */
1009 do {
1010 struct inode *inode;
1011
Sage Weilf0023bc2011-10-28 10:02:42 -07001012 /*
1013 * remove the dentry from the lru, and inform
1014 * the fs that this dentry is about to be
1015 * unhashed and destroyed.
1016 */
1017 dentry_lru_prune(dentry);
David Howells43c1c9c2011-06-07 14:09:30 +01001018 __d_shrink(dentry);
1019
Nick Pigginb7ab39f2011-01-07 17:49:32 +11001020 if (dentry->d_count != 0) {
David Howellsc636ebd2006-10-11 01:22:19 -07001021 printk(KERN_ERR
1022 "BUG: Dentry %p{i=%lx,n=%s}"
1023 " still in use (%d)"
1024 " [unmount of %s %s]\n",
1025 dentry,
1026 dentry->d_inode ?
1027 dentry->d_inode->i_ino : 0UL,
1028 dentry->d_name.name,
Nick Pigginb7ab39f2011-01-07 17:49:32 +11001029 dentry->d_count,
David Howellsc636ebd2006-10-11 01:22:19 -07001030 dentry->d_sb->s_type->name,
1031 dentry->d_sb->s_id);
1032 BUG();
1033 }
1034
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001035 if (IS_ROOT(dentry)) {
David Howellsc636ebd2006-10-11 01:22:19 -07001036 parent = NULL;
Al Viroa7cca092014-10-26 19:19:16 -04001037 list_del(&dentry->d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001038 } else {
OGAWA Hirofumi871c0062008-10-16 07:50:27 +09001039 parent = dentry->d_parent;
Nick Pigginb7ab39f2011-01-07 17:49:32 +11001040 parent->d_count--;
Al Viroa7cca092014-10-26 19:19:16 -04001041 list_del(&dentry->d_child);
OGAWA Hirofumi871c0062008-10-16 07:50:27 +09001042 }
David Howellsc636ebd2006-10-11 01:22:19 -07001043
David Howellsc636ebd2006-10-11 01:22:19 -07001044 inode = dentry->d_inode;
1045 if (inode) {
1046 dentry->d_inode = NULL;
Al Viroa7cca092014-10-26 19:19:16 -04001047 list_del_init(&dentry->d_u.d_alias);
David Howellsc636ebd2006-10-11 01:22:19 -07001048 if (dentry->d_op && dentry->d_op->d_iput)
1049 dentry->d_op->d_iput(dentry, inode);
1050 else
1051 iput(inode);
1052 }
1053
1054 d_free(dentry);
1055
1056 /* finished when we fall off the top of the tree,
1057 * otherwise we ascend to the parent and move to the
1058 * next sibling if there is one */
1059 if (!parent)
Christoph Hellwig312d3ca2010-10-10 05:36:23 -04001060 return;
David Howellsc636ebd2006-10-11 01:22:19 -07001061 dentry = parent;
David Howellsc636ebd2006-10-11 01:22:19 -07001062 } while (list_empty(&dentry->d_subdirs));
1063
1064 dentry = list_entry(dentry->d_subdirs.next,
Al Viroa7cca092014-10-26 19:19:16 -04001065 struct dentry, d_child);
David Howellsc636ebd2006-10-11 01:22:19 -07001066 }
1067}
1068
1069/*
1070 * destroy the dentries attached to a superblock on unmounting
Nick Pigginb5c84bf2011-01-07 17:49:38 +11001071 * - we don't need to use dentry->d_lock because:
David Howellsc636ebd2006-10-11 01:22:19 -07001072 * - the superblock is detached from all mountings and open files, so the
1073 * dentry trees will not be rearranged by the VFS
1074 * - s_umount is write-locked, so the memory pressure shrinker will ignore
1075 * any dentries belonging to this superblock that it comes across
1076 * - the filesystem itself is no longer permitted to rearrange the dentries
1077 * in this superblock
1078 */
1079void shrink_dcache_for_umount(struct super_block *sb)
1080{
1081 struct dentry *dentry;
1082
1083 if (down_read_trylock(&sb->s_umount))
1084 BUG();
1085
1086 dentry = sb->s_root;
1087 sb->s_root = NULL;
Nick Pigginb7ab39f2011-01-07 17:49:32 +11001088 dentry->d_count--;
David Howellsc636ebd2006-10-11 01:22:19 -07001089 shrink_dcache_for_umount_subtree(dentry);
1090
Nick Pigginceb5bdc2011-01-07 17:50:05 +11001091 while (!hlist_bl_empty(&sb->s_anon)) {
1092 dentry = hlist_bl_entry(hlist_bl_first(&sb->s_anon), struct dentry, d_hash);
David Howellsc636ebd2006-10-11 01:22:19 -07001093 shrink_dcache_for_umount_subtree(dentry);
1094 }
1095}
1096
Linus Torvaldsc826cb72011-03-15 15:29:21 -07001097
1098/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 * Search for at least 1 mount point in the dentry's subdirs.
1100 * We descend to the next level whenever the d_subdirs
1101 * list is non-empty and continue searching.
1102 */
1103
1104/**
1105 * have_submounts - check for mounts over a dentry
1106 * @parent: dentry to check.
1107 *
1108 * Return true if the parent or its subdirectories contain
1109 * a mount point
1110 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111int have_submounts(struct dentry *parent)
1112{
Nick Piggin949854d2011-01-07 17:49:37 +11001113 struct dentry *this_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 struct list_head *next;
Nick Piggin949854d2011-01-07 17:49:37 +11001115 unsigned seq;
Nick Piggin58db63d2011-01-07 17:49:39 +11001116 int locked = 0;
Nick Piggin949854d2011-01-07 17:49:37 +11001117
Nick Piggin949854d2011-01-07 17:49:37 +11001118 seq = read_seqbegin(&rename_lock);
Nick Piggin58db63d2011-01-07 17:49:39 +11001119again:
1120 this_parent = parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 if (d_mountpoint(parent))
1123 goto positive;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001124 spin_lock(&this_parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125repeat:
1126 next = this_parent->d_subdirs.next;
1127resume:
1128 while (next != &this_parent->d_subdirs) {
1129 struct list_head *tmp = next;
Al Viroa7cca092014-10-26 19:19:16 -04001130 struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 next = tmp->next;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001132
1133 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 /* Have we found a mount point ? */
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001135 if (d_mountpoint(dentry)) {
1136 spin_unlock(&dentry->d_lock);
1137 spin_unlock(&this_parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 goto positive;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 if (!list_empty(&dentry->d_subdirs)) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001141 spin_unlock(&this_parent->d_lock);
1142 spin_release(&dentry->d_lock.dep_map, 1, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 this_parent = dentry;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001144 spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 goto repeat;
1146 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001147 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 }
1149 /*
1150 * All done at this level ... ascend and resume the search.
1151 */
Al Viro01306a52014-10-26 19:31:10 -04001152 rcu_read_lock();
1153ascend:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 if (this_parent != parent) {
Linus Torvaldsc826cb72011-03-15 15:29:21 -07001155 struct dentry *child = this_parent;
Al Viro01306a52014-10-26 19:31:10 -04001156 this_parent = child->d_parent;
1157
1158 spin_unlock(&child->d_lock);
1159 spin_lock(&this_parent->d_lock);
1160
1161 /* might go back up the wrong parent if we have had a rename */
1162 if (!locked && read_seqretry(&rename_lock, seq))
Nick Piggin949854d2011-01-07 17:49:37 +11001163 goto rename_retry;
Al Viroa7cca092014-10-26 19:19:16 -04001164 next = child->d_child.next;
Al Viro01306a52014-10-26 19:31:10 -04001165 while (unlikely(child->d_flags & DCACHE_DENTRY_KILLED)) {
1166 if (next == &this_parent->d_subdirs)
1167 goto ascend;
1168 child = list_entry(next, struct dentry, d_child);
1169 next = next->next;
1170 }
1171 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 goto resume;
1173 }
Nick Piggin58db63d2011-01-07 17:49:39 +11001174 if (!locked && read_seqretry(&rename_lock, seq))
Nick Piggin949854d2011-01-07 17:49:37 +11001175 goto rename_retry;
Al Viro01306a52014-10-26 19:31:10 -04001176 spin_unlock(&this_parent->d_lock);
1177 rcu_read_unlock();
Nick Piggin58db63d2011-01-07 17:49:39 +11001178 if (locked)
1179 write_sequnlock(&rename_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 return 0; /* No mount points found in tree */
1181positive:
Nick Piggin58db63d2011-01-07 17:49:39 +11001182 if (!locked && read_seqretry(&rename_lock, seq))
Al Viro01306a52014-10-26 19:31:10 -04001183 goto rename_retry_unlocked;
Nick Piggin58db63d2011-01-07 17:49:39 +11001184 if (locked)
1185 write_sequnlock(&rename_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 return 1;
Nick Piggin58db63d2011-01-07 17:49:39 +11001187
1188rename_retry:
Al Viro01306a52014-10-26 19:31:10 -04001189 spin_unlock(&this_parent->d_lock);
1190 rcu_read_unlock();
Miklos Szeredidb8a9e22012-09-17 22:23:30 +02001191 if (locked)
1192 goto again;
Al Viro01306a52014-10-26 19:31:10 -04001193rename_retry_unlocked:
Nick Piggin58db63d2011-01-07 17:49:39 +11001194 locked = 1;
1195 write_seqlock(&rename_lock);
1196 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001198EXPORT_SYMBOL(have_submounts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
1200/*
1201 * Search the dentry child list for the specified parent,
1202 * and move any unused dentries to the end of the unused
1203 * list for prune_dcache(). We descend to the next level
1204 * whenever the d_subdirs list is non-empty and continue
1205 * searching.
1206 *
1207 * It returns zero iff there are no unused children,
1208 * otherwise it returns the number of children moved to
1209 * the end of the unused list. This may not be the total
1210 * number of unused children, because select_parent can
1211 * drop the lock and return early due to latency
1212 * constraints.
1213 */
Dave Chinnerb48f03b2011-08-23 18:56:24 +10001214static int select_parent(struct dentry *parent, struct list_head *dispose)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215{
Nick Piggin949854d2011-01-07 17:49:37 +11001216 struct dentry *this_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 struct list_head *next;
Nick Piggin949854d2011-01-07 17:49:37 +11001218 unsigned seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 int found = 0;
Nick Piggin58db63d2011-01-07 17:49:39 +11001220 int locked = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Nick Piggin949854d2011-01-07 17:49:37 +11001222 seq = read_seqbegin(&rename_lock);
Nick Piggin58db63d2011-01-07 17:49:39 +11001223again:
1224 this_parent = parent;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001225 spin_lock(&this_parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226repeat:
1227 next = this_parent->d_subdirs.next;
1228resume:
1229 while (next != &this_parent->d_subdirs) {
1230 struct list_head *tmp = next;
Al Viroa7cca092014-10-26 19:19:16 -04001231 struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 next = tmp->next;
1233
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001234 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Nick Piggin23044502011-01-07 17:49:31 +11001235
Dave Chinnerb48f03b2011-08-23 18:56:24 +10001236 /*
1237 * move only zero ref count dentries to the dispose list.
Miklos Szeredieaf5f902012-01-10 18:22:25 +01001238 *
1239 * Those which are presently on the shrink list, being processed
1240 * by shrink_dentry_list(), shouldn't be moved. Otherwise the
1241 * loop in shrink_dcache_parent() might not make any progress
1242 * and loop forever.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 */
Miklos Szeredieaf5f902012-01-10 18:22:25 +01001244 if (dentry->d_count) {
Christoph Hellwiga4633352010-10-10 05:36:26 -04001245 dentry_lru_del(dentry);
Miklos Szeredieaf5f902012-01-10 18:22:25 +01001246 } else if (!(dentry->d_flags & DCACHE_SHRINK_LIST)) {
1247 dentry_lru_move_list(dentry, dispose);
1248 dentry->d_flags |= DCACHE_SHRINK_LIST;
1249 found++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 /*
1252 * We can return to the caller if we have found some (this
1253 * ensures forward progress). We'll be coming back to find
1254 * the rest.
1255 */
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001256 if (found && need_resched()) {
1257 spin_unlock(&dentry->d_lock);
Al Viro01306a52014-10-26 19:31:10 -04001258 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 goto out;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
1262 /*
1263 * Descend a level if the d_subdirs list is non-empty.
1264 */
1265 if (!list_empty(&dentry->d_subdirs)) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001266 spin_unlock(&this_parent->d_lock);
1267 spin_release(&dentry->d_lock.dep_map, 1, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 this_parent = dentry;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001269 spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 goto repeat;
1271 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001272
1273 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 }
1275 /*
1276 * All done at this level ... ascend and resume the search.
1277 */
Al Viro01306a52014-10-26 19:31:10 -04001278 rcu_read_lock();
1279ascend:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 if (this_parent != parent) {
Linus Torvaldsc826cb72011-03-15 15:29:21 -07001281 struct dentry *child = this_parent;
Al Viro01306a52014-10-26 19:31:10 -04001282 this_parent = child->d_parent;
1283
1284 spin_unlock(&child->d_lock);
1285 spin_lock(&this_parent->d_lock);
1286
1287 /* might go back up the wrong parent if we have had a rename */
1288 if (!locked && read_seqretry(&rename_lock, seq))
Nick Piggin949854d2011-01-07 17:49:37 +11001289 goto rename_retry;
Al Viroe309b362015-05-28 23:09:19 -04001290 /* go into the first sibling still alive */
1291 do {
1292 next = child->d_child.next;
Al Viro01306a52014-10-26 19:31:10 -04001293 if (next == &this_parent->d_subdirs)
1294 goto ascend;
1295 child = list_entry(next, struct dentry, d_child);
Al Viroe309b362015-05-28 23:09:19 -04001296 } while (unlikely(child->d_flags & DCACHE_DENTRY_KILLED));
Al Viro01306a52014-10-26 19:31:10 -04001297 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 goto resume;
1299 }
1300out:
Nick Piggin58db63d2011-01-07 17:49:39 +11001301 if (!locked && read_seqretry(&rename_lock, seq))
Nick Piggin949854d2011-01-07 17:49:37 +11001302 goto rename_retry;
Al Viro01306a52014-10-26 19:31:10 -04001303 spin_unlock(&this_parent->d_lock);
1304 rcu_read_unlock();
Nick Piggin58db63d2011-01-07 17:49:39 +11001305 if (locked)
1306 write_sequnlock(&rename_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 return found;
Nick Piggin58db63d2011-01-07 17:49:39 +11001308
1309rename_retry:
Al Viro01306a52014-10-26 19:31:10 -04001310 spin_unlock(&this_parent->d_lock);
1311 rcu_read_unlock();
Nick Piggin58db63d2011-01-07 17:49:39 +11001312 if (found)
1313 return found;
Miklos Szeredidb8a9e22012-09-17 22:23:30 +02001314 if (locked)
1315 goto again;
Nick Piggin58db63d2011-01-07 17:49:39 +11001316 locked = 1;
1317 write_seqlock(&rename_lock);
1318 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319}
1320
1321/**
1322 * shrink_dcache_parent - prune dcache
1323 * @parent: parent of entries to prune
1324 *
1325 * Prune the dcache to remove unused children of the parent dentry.
1326 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327void shrink_dcache_parent(struct dentry * parent)
1328{
Dave Chinnerb48f03b2011-08-23 18:56:24 +10001329 LIST_HEAD(dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 int found;
1331
Greg Thelence8441f2013-04-30 15:26:48 -07001332 while ((found = select_parent(parent, &dispose)) != 0) {
Dave Chinnerb48f03b2011-08-23 18:56:24 +10001333 shrink_dentry_list(&dispose);
Greg Thelence8441f2013-04-30 15:26:48 -07001334 cond_resched();
1335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001337EXPORT_SYMBOL(shrink_dcache_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339/**
Al Viroa4464db2011-07-07 15:03:58 -04001340 * __d_alloc - allocate a dcache entry
1341 * @sb: filesystem it will belong to
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 * @name: qstr of the name
1343 *
1344 * Allocates a dentry. It returns %NULL if there is insufficient memory
1345 * available. On a success the dentry is returned. The name passed in is
1346 * copied and the copy passed in may be reused after this call.
1347 */
1348
Al Viroa4464db2011-07-07 15:03:58 -04001349struct dentry *__d_alloc(struct super_block *sb, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350{
1351 struct dentry *dentry;
1352 char *dname;
1353
Mel Gormane12ba742007-10-16 01:25:52 -07001354 dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 if (!dentry)
1356 return NULL;
1357
Linus Torvalds8c27a1e2012-05-21 16:14:04 -07001358 /*
1359 * We guarantee that the inline name is always NUL-terminated.
1360 * This way the memcpy() done by the name switching in rename
1361 * will still always have a NUL at the end, even if we might
1362 * be overwriting an internal NUL character
1363 */
1364 dentry->d_iname[DNAME_INLINE_LEN-1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 if (name->len > DNAME_INLINE_LEN-1) {
Al Viro443c3682014-09-29 14:54:27 -04001366 size_t size = offsetof(struct external_name, name[1]);
1367 struct external_name *p = kmalloc(size + name->len, GFP_KERNEL);
1368 if (!p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 kmem_cache_free(dentry_cache, dentry);
1370 return NULL;
1371 }
Al Viro443c3682014-09-29 14:54:27 -04001372 atomic_set(&p->u.count, 1);
1373 dname = p->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 } else {
1375 dname = dentry->d_iname;
1376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
1378 dentry->d_name.len = name->len;
1379 dentry->d_name.hash = name->hash;
1380 memcpy(dname, name->name, name->len);
1381 dname[name->len] = 0;
1382
Linus Torvalds8c27a1e2012-05-21 16:14:04 -07001383 /* Make sure we always see the terminating NUL character */
1384 smp_wmb();
1385 dentry->d_name.name = dname;
1386
Nick Pigginb7ab39f2011-01-07 17:49:32 +11001387 dentry->d_count = 1;
Linus Torvaldsdea36672011-04-24 07:58:46 -07001388 dentry->d_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 spin_lock_init(&dentry->d_lock);
Nick Piggin31e6b012011-01-07 17:49:52 +11001390 seqcount_init(&dentry->d_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 dentry->d_inode = NULL;
Al Viroa4464db2011-07-07 15:03:58 -04001392 dentry->d_parent = dentry;
1393 dentry->d_sb = sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 dentry->d_op = NULL;
1395 dentry->d_fsdata = NULL;
Nick Pigginceb5bdc2011-01-07 17:50:05 +11001396 INIT_HLIST_BL_NODE(&dentry->d_hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 INIT_LIST_HEAD(&dentry->d_lru);
1398 INIT_LIST_HEAD(&dentry->d_subdirs);
Al Viroa7cca092014-10-26 19:19:16 -04001399 INIT_LIST_HEAD(&dentry->d_u.d_alias);
1400 INIT_LIST_HEAD(&dentry->d_child);
Al Viroa4464db2011-07-07 15:03:58 -04001401 d_set_d_op(dentry, dentry->d_sb->s_d_op);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Nick Piggin3e880fb2011-01-07 17:49:19 +11001403 this_cpu_inc(nr_dentry);
Christoph Hellwig312d3ca2010-10-10 05:36:23 -04001404
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 return dentry;
1406}
Al Viroa4464db2011-07-07 15:03:58 -04001407
1408/**
1409 * d_alloc - allocate a dcache entry
1410 * @parent: parent of entry to allocate
1411 * @name: qstr of the name
1412 *
1413 * Allocates a dentry. It returns %NULL if there is insufficient memory
1414 * available. On a success the dentry is returned. The name passed in is
1415 * copied and the copy passed in may be reused after this call.
1416 */
1417struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
1418{
1419 struct dentry *dentry = __d_alloc(parent->d_sb, name);
1420 if (!dentry)
1421 return NULL;
1422
1423 spin_lock(&parent->d_lock);
1424 /*
1425 * don't need child lock because it is not subject
1426 * to concurrency here
1427 */
1428 __dget_dlock(parent);
1429 dentry->d_parent = parent;
Al Viroa7cca092014-10-26 19:19:16 -04001430 list_add(&dentry->d_child, &parent->d_subdirs);
Al Viroa4464db2011-07-07 15:03:58 -04001431 spin_unlock(&parent->d_lock);
1432
1433 return dentry;
1434}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001435EXPORT_SYMBOL(d_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
Nick Piggin4b936882011-01-07 17:50:07 +11001437struct dentry *d_alloc_pseudo(struct super_block *sb, const struct qstr *name)
1438{
Al Viroa4464db2011-07-07 15:03:58 -04001439 struct dentry *dentry = __d_alloc(sb, name);
1440 if (dentry)
Nick Piggin4b936882011-01-07 17:50:07 +11001441 dentry->d_flags |= DCACHE_DISCONNECTED;
Nick Piggin4b936882011-01-07 17:50:07 +11001442 return dentry;
1443}
1444EXPORT_SYMBOL(d_alloc_pseudo);
1445
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446struct dentry *d_alloc_name(struct dentry *parent, const char *name)
1447{
1448 struct qstr q;
1449
1450 q.name = name;
1451 q.len = strlen(name);
1452 q.hash = full_name_hash(q.name, q.len);
1453 return d_alloc(parent, &q);
1454}
H Hartley Sweetenef26ca92009-09-29 20:09:42 -04001455EXPORT_SYMBOL(d_alloc_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Nick Pigginfb045ad2011-01-07 17:49:55 +11001457void d_set_d_op(struct dentry *dentry, const struct dentry_operations *op)
1458{
Linus Torvalds6f7f7ca2011-01-14 13:26:18 -08001459 WARN_ON_ONCE(dentry->d_op);
1460 WARN_ON_ONCE(dentry->d_flags & (DCACHE_OP_HASH |
Nick Pigginfb045ad2011-01-07 17:49:55 +11001461 DCACHE_OP_COMPARE |
1462 DCACHE_OP_REVALIDATE |
1463 DCACHE_OP_DELETE ));
1464 dentry->d_op = op;
1465 if (!op)
1466 return;
1467 if (op->d_hash)
1468 dentry->d_flags |= DCACHE_OP_HASH;
1469 if (op->d_compare)
1470 dentry->d_flags |= DCACHE_OP_COMPARE;
1471 if (op->d_revalidate)
1472 dentry->d_flags |= DCACHE_OP_REVALIDATE;
1473 if (op->d_delete)
1474 dentry->d_flags |= DCACHE_OP_DELETE;
Sage Weilf0023bc2011-10-28 10:02:42 -07001475 if (op->d_prune)
1476 dentry->d_flags |= DCACHE_OP_PRUNE;
Nick Pigginfb045ad2011-01-07 17:49:55 +11001477
1478}
1479EXPORT_SYMBOL(d_set_d_op);
1480
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001481static void __d_instantiate(struct dentry *dentry, struct inode *inode)
1482{
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001483 spin_lock(&dentry->d_lock);
David Howells9875cf82011-01-14 18:45:21 +00001484 if (inode) {
1485 if (unlikely(IS_AUTOMOUNT(inode)))
1486 dentry->d_flags |= DCACHE_NEED_AUTOMOUNT;
Al Viroa7cca092014-10-26 19:19:16 -04001487 list_add(&dentry->d_u.d_alias, &inode->i_dentry);
David Howells9875cf82011-01-14 18:45:21 +00001488 }
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001489 dentry->d_inode = inode;
Nick Piggin31e6b012011-01-07 17:49:52 +11001490 dentry_rcuwalk_barrier(dentry);
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001491 spin_unlock(&dentry->d_lock);
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001492 fsnotify_d_instantiate(dentry, inode);
1493}
1494
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495/**
1496 * d_instantiate - fill in inode information for a dentry
1497 * @entry: dentry to complete
1498 * @inode: inode to attach to this dentry
1499 *
1500 * Fill in inode information in the entry.
1501 *
1502 * This turns negative dentries into productive full members
1503 * of society.
1504 *
1505 * NOTE! This assumes that the inode count has been incremented
1506 * (or otherwise set) by the caller to indicate that it is now
1507 * in use by the dcache.
1508 */
1509
1510void d_instantiate(struct dentry *entry, struct inode * inode)
1511{
Al Viroa7cca092014-10-26 19:19:16 -04001512 BUG_ON(!list_empty(&entry->d_u.d_alias));
Nick Piggin873feea2011-01-07 17:50:06 +11001513 if (inode)
1514 spin_lock(&inode->i_lock);
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001515 __d_instantiate(entry, inode);
Nick Piggin873feea2011-01-07 17:50:06 +11001516 if (inode)
1517 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 security_d_instantiate(entry, inode);
1519}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001520EXPORT_SYMBOL(d_instantiate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
1522/**
1523 * d_instantiate_unique - instantiate a non-aliased dentry
1524 * @entry: dentry to instantiate
1525 * @inode: inode to attach to this dentry
1526 *
1527 * Fill in inode information in the entry. On success, it returns NULL.
1528 * If an unhashed alias of "entry" already exists, then we return the
Oleg Drokine866cfa2006-01-09 20:52:51 -08001529 * aliased dentry instead and drop one reference to inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 *
1531 * Note that in order to avoid conflicts with rename() etc, the caller
1532 * had better be holding the parent directory semaphore.
Oleg Drokine866cfa2006-01-09 20:52:51 -08001533 *
1534 * This also assumes that the inode count has been incremented
1535 * (or otherwise set) by the caller to indicate that it is now
1536 * in use by the dcache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 */
David Howells770bfad2006-08-22 20:06:07 -04001538static struct dentry *__d_instantiate_unique(struct dentry *entry,
1539 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540{
1541 struct dentry *alias;
1542 int len = entry->d_name.len;
1543 const char *name = entry->d_name.name;
1544 unsigned int hash = entry->d_name.hash;
1545
David Howells770bfad2006-08-22 20:06:07 -04001546 if (!inode) {
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001547 __d_instantiate(entry, NULL);
David Howells770bfad2006-08-22 20:06:07 -04001548 return NULL;
1549 }
1550
Al Viroa7cca092014-10-26 19:19:16 -04001551 list_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
Nick Piggin9abca362011-01-07 17:49:36 +11001552 /*
1553 * Don't need alias->d_lock here, because aliases with
1554 * d_parent == entry->d_parent are not subject to name or
1555 * parent changes, because the parent inode i_mutex is held.
1556 */
Linus Torvaldsb7d3740d2012-05-04 14:59:14 -07001557 if (alias->d_name.hash != hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 continue;
1559 if (alias->d_parent != entry->d_parent)
1560 continue;
Linus Torvalds3ae62d92012-05-10 12:37:10 -07001561 if (alias->d_name.len != len)
1562 continue;
Linus Torvaldsb7d3740d2012-05-04 14:59:14 -07001563 if (dentry_cmp(alias, name, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 continue;
Nick Piggindc0474b2011-01-07 17:49:43 +11001565 __dget(alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 return alias;
1567 }
David Howells770bfad2006-08-22 20:06:07 -04001568
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001569 __d_instantiate(entry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 return NULL;
1571}
David Howells770bfad2006-08-22 20:06:07 -04001572
1573struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode)
1574{
1575 struct dentry *result;
1576
Al Viroa7cca092014-10-26 19:19:16 -04001577 BUG_ON(!list_empty(&entry->d_u.d_alias));
David Howells770bfad2006-08-22 20:06:07 -04001578
Nick Piggin873feea2011-01-07 17:50:06 +11001579 if (inode)
1580 spin_lock(&inode->i_lock);
David Howells770bfad2006-08-22 20:06:07 -04001581 result = __d_instantiate_unique(entry, inode);
Nick Piggin873feea2011-01-07 17:50:06 +11001582 if (inode)
1583 spin_unlock(&inode->i_lock);
David Howells770bfad2006-08-22 20:06:07 -04001584
1585 if (!result) {
1586 security_d_instantiate(entry, inode);
1587 return NULL;
1588 }
1589
1590 BUG_ON(!d_unhashed(result));
1591 iput(inode);
1592 return result;
1593}
1594
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595EXPORT_SYMBOL(d_instantiate_unique);
1596
Al Viroadc0e912012-01-08 16:49:21 -05001597struct dentry *d_make_root(struct inode *root_inode)
1598{
1599 struct dentry *res = NULL;
1600
1601 if (root_inode) {
Linus Torvalds6cd1e702012-05-10 13:14:12 -07001602 static const struct qstr name = QSTR_INIT("/", 1);
Al Viroadc0e912012-01-08 16:49:21 -05001603
1604 res = __d_alloc(root_inode->i_sb, &name);
1605 if (res)
1606 d_instantiate(res, root_inode);
1607 else
1608 iput(root_inode);
1609 }
1610 return res;
1611}
1612EXPORT_SYMBOL(d_make_root);
1613
J. Bruce Fieldsd891eed2011-01-18 15:45:09 -05001614static struct dentry * __d_find_any_alias(struct inode *inode)
1615{
1616 struct dentry *alias;
1617
1618 if (list_empty(&inode->i_dentry))
1619 return NULL;
Al Viroa7cca092014-10-26 19:19:16 -04001620 alias = list_first_entry(&inode->i_dentry, struct dentry, d_u.d_alias);
J. Bruce Fieldsd891eed2011-01-18 15:45:09 -05001621 __dget(alias);
1622 return alias;
1623}
1624
Sage Weil46f72b32012-01-10 09:04:37 -08001625/**
1626 * d_find_any_alias - find any alias for a given inode
1627 * @inode: inode to find an alias for
1628 *
1629 * If any aliases exist for the given inode, take and return a
1630 * reference for one of them. If no aliases exist, return %NULL.
1631 */
1632struct dentry *d_find_any_alias(struct inode *inode)
J. Bruce Fieldsd891eed2011-01-18 15:45:09 -05001633{
1634 struct dentry *de;
1635
1636 spin_lock(&inode->i_lock);
1637 de = __d_find_any_alias(inode);
1638 spin_unlock(&inode->i_lock);
1639 return de;
1640}
Sage Weil46f72b32012-01-10 09:04:37 -08001641EXPORT_SYMBOL(d_find_any_alias);
J. Bruce Fieldsd891eed2011-01-18 15:45:09 -05001642
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643/**
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001644 * d_obtain_alias - find or allocate a dentry for a given inode
1645 * @inode: inode to allocate the dentry for
1646 *
1647 * Obtain a dentry for an inode resulting from NFS filehandle conversion or
1648 * similar open by handle operations. The returned dentry may be anonymous,
1649 * or may have a full name (if the inode was already in the cache).
1650 *
1651 * When called on a directory inode, we must ensure that the inode only ever
1652 * has one dentry. If a dentry is found, that is returned instead of
1653 * allocating a new one.
1654 *
1655 * On successful return, the reference to the inode has been transferred
Christoph Hellwig44003722008-08-11 15:49:04 +02001656 * to the dentry. In case of an error the reference on the inode is released.
1657 * To make it easier to use in export operations a %NULL or IS_ERR inode may
1658 * be passed in and will be the error will be propagate to the return value,
1659 * with a %NULL @inode replaced by ERR_PTR(-ESTALE).
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001660 */
1661struct dentry *d_obtain_alias(struct inode *inode)
1662{
NeilBrown032409b2012-11-08 16:09:37 -08001663 static const struct qstr anonstring = { .name = "/", .len = 1 };
Christoph Hellwig9308a612008-08-11 15:49:12 +02001664 struct dentry *tmp;
1665 struct dentry *res;
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001666
1667 if (!inode)
Christoph Hellwig44003722008-08-11 15:49:04 +02001668 return ERR_PTR(-ESTALE);
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001669 if (IS_ERR(inode))
1670 return ERR_CAST(inode);
1671
J. Bruce Fieldsd891eed2011-01-18 15:45:09 -05001672 res = d_find_any_alias(inode);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001673 if (res)
1674 goto out_iput;
1675
Al Viroa4464db2011-07-07 15:03:58 -04001676 tmp = __d_alloc(inode->i_sb, &anonstring);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001677 if (!tmp) {
1678 res = ERR_PTR(-ENOMEM);
1679 goto out_iput;
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001680 }
Nick Pigginb5c84bf2011-01-07 17:49:38 +11001681
Nick Piggin873feea2011-01-07 17:50:06 +11001682 spin_lock(&inode->i_lock);
J. Bruce Fieldsd891eed2011-01-18 15:45:09 -05001683 res = __d_find_any_alias(inode);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001684 if (res) {
Nick Piggin873feea2011-01-07 17:50:06 +11001685 spin_unlock(&inode->i_lock);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001686 dput(tmp);
1687 goto out_iput;
1688 }
1689
1690 /* attach a disconnected dentry */
1691 spin_lock(&tmp->d_lock);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001692 tmp->d_inode = inode;
1693 tmp->d_flags |= DCACHE_DISCONNECTED;
Al Viroa7cca092014-10-26 19:19:16 -04001694 list_add(&tmp->d_u.d_alias, &inode->i_dentry);
Christoph Hellwig1879fd62011-04-25 14:01:36 -04001695 hlist_bl_lock(&tmp->d_sb->s_anon);
Nick Pigginceb5bdc2011-01-07 17:50:05 +11001696 hlist_bl_add_head(&tmp->d_hash, &tmp->d_sb->s_anon);
Christoph Hellwig1879fd62011-04-25 14:01:36 -04001697 hlist_bl_unlock(&tmp->d_sb->s_anon);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001698 spin_unlock(&tmp->d_lock);
Nick Piggin873feea2011-01-07 17:50:06 +11001699 spin_unlock(&inode->i_lock);
Josef Bacik24ff6662010-11-18 20:52:55 -05001700 security_d_instantiate(tmp, inode);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001701
Christoph Hellwig9308a612008-08-11 15:49:12 +02001702 return tmp;
1703
1704 out_iput:
Josef Bacik24ff6662010-11-18 20:52:55 -05001705 if (res && !IS_ERR(res))
1706 security_d_instantiate(res, inode);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001707 iput(inode);
1708 return res;
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001709}
Benny Halevyadc48722009-02-27 14:02:59 -08001710EXPORT_SYMBOL(d_obtain_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
1712/**
1713 * d_splice_alias - splice a disconnected dentry into the tree if one exists
1714 * @inode: the inode which may have a disconnected dentry
1715 * @dentry: a negative dentry which we want to point to the inode.
1716 *
1717 * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
1718 * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
1719 * and return it, else simply d_add the inode to the dentry and return NULL.
1720 *
1721 * This is needed in the lookup routine of any filesystem that is exportable
1722 * (via knfsd) so that we can build dcache paths to directories effectively.
1723 *
1724 * If a dentry was found and moved, then it is returned. Otherwise NULL
1725 * is returned. This matches the expected return value of ->lookup.
1726 *
1727 */
1728struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
1729{
1730 struct dentry *new = NULL;
1731
Al Viroa9049372011-07-08 21:20:11 -04001732 if (IS_ERR(inode))
1733 return ERR_CAST(inode);
1734
NeilBrown21c0d8f2006-10-04 02:16:16 -07001735 if (inode && S_ISDIR(inode->i_mode)) {
Nick Piggin873feea2011-01-07 17:50:06 +11001736 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 new = __d_find_alias(inode, 1);
1738 if (new) {
1739 BUG_ON(!(new->d_flags & DCACHE_DISCONNECTED));
Nick Piggin873feea2011-01-07 17:50:06 +11001740 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 security_d_instantiate(new, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 d_move(new, dentry);
1743 iput(inode);
1744 } else {
Nick Piggin873feea2011-01-07 17:50:06 +11001745 /* already taking inode->i_lock, so d_add() by hand */
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001746 __d_instantiate(dentry, inode);
Nick Piggin873feea2011-01-07 17:50:06 +11001747 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 security_d_instantiate(dentry, inode);
1749 d_rehash(dentry);
1750 }
1751 } else
1752 d_add(dentry, inode);
1753 return new;
1754}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001755EXPORT_SYMBOL(d_splice_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
Barry Naujok94035402008-05-21 16:50:46 +10001757/**
1758 * d_add_ci - lookup or allocate new dentry with case-exact name
1759 * @inode: the inode case-insensitive lookup has found
1760 * @dentry: the negative dentry that was passed to the parent's lookup func
1761 * @name: the case-exact name to be associated with the returned dentry
1762 *
1763 * This is to avoid filling the dcache with case-insensitive names to the
1764 * same inode, only the actual correct case is stored in the dcache for
1765 * case-insensitive filesystems.
1766 *
1767 * For a case-insensitive lookup match and if the the case-exact dentry
1768 * already exists in in the dcache, use it and return it.
1769 *
1770 * If no entry exists with the exact case name, allocate new dentry with
1771 * the exact case, and return the spliced entry.
1772 */
Christoph Hellwige45b5902008-08-07 23:49:07 +02001773struct dentry *d_add_ci(struct dentry *dentry, struct inode *inode,
Barry Naujok94035402008-05-21 16:50:46 +10001774 struct qstr *name)
1775{
1776 int error;
1777 struct dentry *found;
1778 struct dentry *new;
1779
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001780 /*
1781 * First check if a dentry matching the name already exists,
1782 * if not go ahead and create it now.
1783 */
Barry Naujok94035402008-05-21 16:50:46 +10001784 found = d_hash_and_lookup(dentry->d_parent, name);
Barry Naujok94035402008-05-21 16:50:46 +10001785 if (!found) {
1786 new = d_alloc(dentry->d_parent, name);
1787 if (!new) {
1788 error = -ENOMEM;
1789 goto err_out;
1790 }
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001791
Barry Naujok94035402008-05-21 16:50:46 +10001792 found = d_splice_alias(inode, new);
1793 if (found) {
1794 dput(new);
1795 return found;
1796 }
1797 return new;
1798 }
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001799
1800 /*
1801 * If a matching dentry exists, and it's not negative use it.
1802 *
1803 * Decrement the reference count to balance the iget() done
1804 * earlier on.
1805 */
Barry Naujok94035402008-05-21 16:50:46 +10001806 if (found->d_inode) {
1807 if (unlikely(found->d_inode != inode)) {
1808 /* This can't happen because bad inodes are unhashed. */
1809 BUG_ON(!is_bad_inode(inode));
1810 BUG_ON(!is_bad_inode(found->d_inode));
1811 }
Barry Naujok94035402008-05-21 16:50:46 +10001812 iput(inode);
1813 return found;
1814 }
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001815
Barry Naujok94035402008-05-21 16:50:46 +10001816 /*
Josef Bacik44396f42011-05-31 11:58:49 -04001817 * We are going to instantiate this dentry, unhash it and clear the
1818 * lookup flag so we can do that.
1819 */
1820 if (unlikely(d_need_lookup(found)))
1821 d_clear_need_lookup(found);
1822
1823 /*
Barry Naujok94035402008-05-21 16:50:46 +10001824 * Negative dentry: instantiate it unless the inode is a directory and
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001825 * already has a dentry.
Barry Naujok94035402008-05-21 16:50:46 +10001826 */
Al Viro4513d892011-07-17 10:52:14 -04001827 new = d_splice_alias(inode, found);
1828 if (new) {
1829 dput(found);
1830 found = new;
Barry Naujok94035402008-05-21 16:50:46 +10001831 }
Al Viro4513d892011-07-17 10:52:14 -04001832 return found;
Barry Naujok94035402008-05-21 16:50:46 +10001833
1834err_out:
1835 iput(inode);
1836 return ERR_PTR(error);
1837}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001838EXPORT_SYMBOL(d_add_ci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
Linus Torvaldsb7d3740d2012-05-04 14:59:14 -07001840/*
1841 * Do the slow-case of the dentry name compare.
1842 *
1843 * Unlike the dentry_cmp() function, we need to atomically
1844 * load the name, length and inode information, so that the
1845 * filesystem can rely on them, and can use the 'name' and
1846 * 'len' information without worrying about walking off the
1847 * end of memory etc.
1848 *
1849 * Thus the read_seqcount_retry() and the "duplicate" info
1850 * in arguments (the low-level filesystem should not look
1851 * at the dentry inode or name contents directly, since
1852 * rename can change them while we're in RCU mode).
1853 */
1854enum slow_d_compare {
1855 D_COMP_OK,
1856 D_COMP_NOMATCH,
1857 D_COMP_SEQRETRY,
1858};
1859
1860static noinline enum slow_d_compare slow_dentry_cmp(
1861 const struct dentry *parent,
1862 struct inode *inode,
1863 struct dentry *dentry,
1864 unsigned int seq,
1865 const struct qstr *name)
1866{
1867 int tlen = dentry->d_name.len;
1868 const char *tname = dentry->d_name.name;
1869 struct inode *i = dentry->d_inode;
1870
1871 if (read_seqcount_retry(&dentry->d_seq, seq)) {
1872 cpu_relax();
1873 return D_COMP_SEQRETRY;
1874 }
1875 if (parent->d_op->d_compare(parent, inode,
1876 dentry, i,
1877 tlen, tname, name))
1878 return D_COMP_NOMATCH;
1879 return D_COMP_OK;
1880}
1881
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882/**
Nick Piggin31e6b012011-01-07 17:49:52 +11001883 * __d_lookup_rcu - search for a dentry (racy, store-free)
1884 * @parent: parent dentry
1885 * @name: qstr of name we wish to find
Randy Dunlap1f1e6e52012-03-18 21:23:05 -07001886 * @seqp: returns d_seq value at the point where the dentry was found
Nick Piggin31e6b012011-01-07 17:49:52 +11001887 * @inode: returns dentry->d_inode when the inode was found valid.
1888 * Returns: dentry, or NULL
1889 *
1890 * __d_lookup_rcu is the dcache lookup function for rcu-walk name
1891 * resolution (store-free path walking) design described in
1892 * Documentation/filesystems/path-lookup.txt.
1893 *
1894 * This is not to be used outside core vfs.
1895 *
1896 * __d_lookup_rcu must only be used in rcu-walk mode, ie. with vfsmount lock
1897 * held, and rcu_read_lock held. The returned dentry must not be stored into
1898 * without taking d_lock and checking d_seq sequence count against @seq
1899 * returned here.
1900 *
1901 * A refcount may be taken on the found dentry with the __d_rcu_to_refcount
1902 * function.
1903 *
1904 * Alternatively, __d_lookup_rcu may be called again to look up the child of
1905 * the returned dentry, so long as its parent's seqlock is checked after the
1906 * child is looked up. Thus, an interlocking stepping of sequence lock checks
1907 * is formed, giving integrity down the path walk.
Linus Torvaldsb7d3740d2012-05-04 14:59:14 -07001908 *
1909 * NOTE! The caller *has* to check the resulting dentry against the sequence
1910 * number we've returned before using any of the resulting dentry state!
Nick Piggin31e6b012011-01-07 17:49:52 +11001911 */
Linus Torvalds8966be92012-03-02 14:23:30 -08001912struct dentry *__d_lookup_rcu(const struct dentry *parent,
1913 const struct qstr *name,
Linus Torvaldsb7d3740d2012-05-04 14:59:14 -07001914 unsigned *seqp, struct inode *inode)
Nick Piggin31e6b012011-01-07 17:49:52 +11001915{
Linus Torvalds6cd1e702012-05-10 13:14:12 -07001916 u64 hashlen = name->hash_len;
Nick Piggin31e6b012011-01-07 17:49:52 +11001917 const unsigned char *str = name->name;
Linus Torvalds6cd1e702012-05-10 13:14:12 -07001918 struct hlist_bl_head *b = d_hash(parent, hashlen_hash(hashlen));
Nick Pigginceb5bdc2011-01-07 17:50:05 +11001919 struct hlist_bl_node *node;
Nick Piggin31e6b012011-01-07 17:49:52 +11001920 struct dentry *dentry;
1921
1922 /*
1923 * Note: There is significant duplication with __d_lookup_rcu which is
1924 * required to prevent single threaded performance regressions
1925 * especially on architectures where smp_rmb (in seqcounts) are costly.
1926 * Keep the two functions in sync.
1927 */
1928
1929 /*
1930 * The hash list is protected using RCU.
1931 *
1932 * Carefully use d_seq when comparing a candidate dentry, to avoid
1933 * races with d_move().
1934 *
1935 * It is possible that concurrent renames can mess up our list
1936 * walk here and result in missing our dentry, resulting in the
1937 * false-negative result. d_lookup() protects against concurrent
1938 * renames using rename_lock seqlock.
1939 *
Namhyung Kimb0a4bb82011-01-22 15:31:32 +09001940 * See Documentation/filesystems/path-lookup.txt for more details.
Nick Piggin31e6b012011-01-07 17:49:52 +11001941 */
Linus Torvaldsb07ad992011-04-23 22:32:03 -07001942 hlist_bl_for_each_entry_rcu(dentry, node, b, d_hash) {
Linus Torvalds8966be92012-03-02 14:23:30 -08001943 unsigned seq;
Nick Piggin31e6b012011-01-07 17:49:52 +11001944
1945seqretry:
Linus Torvaldsb7d3740d2012-05-04 14:59:14 -07001946 /*
1947 * The dentry sequence count protects us from concurrent
1948 * renames, and thus protects inode, parent and name fields.
1949 *
1950 * The caller must perform a seqcount check in order
1951 * to do anything useful with the returned dentry,
1952 * including using the 'd_inode' pointer.
1953 *
1954 * NOTE! We do a "raw" seqcount_begin here. That means that
1955 * we don't wait for the sequence count to stabilize if it
1956 * is in the middle of a sequence change. If we do the slow
1957 * dentry compare, we will do seqretries until it is stable,
1958 * and if we end up with a successful lookup, we actually
1959 * want to exit RCU lookup anyway.
1960 */
1961 seq = raw_seqcount_begin(&dentry->d_seq);
Nick Piggin31e6b012011-01-07 17:49:52 +11001962 if (dentry->d_parent != parent)
1963 continue;
1964 if (d_unhashed(dentry))
1965 continue;
Linus Torvalds8966be92012-03-02 14:23:30 -08001966 *seqp = seq;
Linus Torvaldsb7d3740d2012-05-04 14:59:14 -07001967
1968 if (unlikely(parent->d_flags & DCACHE_OP_COMPARE)) {
Linus Torvalds6cd1e702012-05-10 13:14:12 -07001969 if (dentry->d_name.hash != hashlen_hash(hashlen))
1970 continue;
Linus Torvaldsb7d3740d2012-05-04 14:59:14 -07001971 switch (slow_dentry_cmp(parent, inode, dentry, seq, name)) {
1972 case D_COMP_OK:
1973 return dentry;
1974 case D_COMP_NOMATCH:
1975 continue;
1976 default:
1977 goto seqretry;
1978 }
1979 }
1980
Linus Torvalds6cd1e702012-05-10 13:14:12 -07001981 if (dentry->d_name.hash_len != hashlen)
Linus Torvalds3ae62d92012-05-10 12:37:10 -07001982 continue;
Linus Torvalds6cd1e702012-05-10 13:14:12 -07001983 if (!dentry_cmp(dentry, str, hashlen_len(hashlen)))
Linus Torvaldsb7d3740d2012-05-04 14:59:14 -07001984 return dentry;
Nick Piggin31e6b012011-01-07 17:49:52 +11001985 }
1986 return NULL;
1987}
1988
1989/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 * d_lookup - search for a dentry
1991 * @parent: parent dentry
1992 * @name: qstr of name we wish to find
Nick Pigginb04f7842010-08-18 04:37:34 +10001993 * Returns: dentry, or NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 *
Nick Pigginb04f7842010-08-18 04:37:34 +10001995 * d_lookup searches the children of the parent dentry for the name in
1996 * question. If the dentry is found its reference count is incremented and the
1997 * dentry is returned. The caller must use dput to free the entry when it has
1998 * finished using it. %NULL is returned if the dentry does not exist.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 */
Al Virod3af92f2013-01-24 18:29:34 -05002000struct dentry *d_lookup(const struct dentry *parent, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001{
Nick Piggin31e6b012011-01-07 17:49:52 +11002002 struct dentry *dentry;
Nick Piggin949854d2011-01-07 17:49:37 +11002003 unsigned seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004
2005 do {
2006 seq = read_seqbegin(&rename_lock);
2007 dentry = __d_lookup(parent, name);
2008 if (dentry)
2009 break;
2010 } while (read_seqretry(&rename_lock, seq));
2011 return dentry;
2012}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002013EXPORT_SYMBOL(d_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
Nick Piggin31e6b012011-01-07 17:49:52 +11002015/**
Nick Pigginb04f7842010-08-18 04:37:34 +10002016 * __d_lookup - search for a dentry (racy)
2017 * @parent: parent dentry
2018 * @name: qstr of name we wish to find
2019 * Returns: dentry, or NULL
2020 *
2021 * __d_lookup is like d_lookup, however it may (rarely) return a
2022 * false-negative result due to unrelated rename activity.
2023 *
2024 * __d_lookup is slightly faster by avoiding rename_lock read seqlock,
2025 * however it must be used carefully, eg. with a following d_lookup in
2026 * the case of failure.
2027 *
2028 * __d_lookup callers must be commented.
2029 */
Al Viro16b78332013-01-24 18:27:00 -05002030struct dentry *__d_lookup(const struct dentry *parent, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031{
2032 unsigned int len = name->len;
2033 unsigned int hash = name->hash;
2034 const unsigned char *str = name->name;
Linus Torvaldsb07ad992011-04-23 22:32:03 -07002035 struct hlist_bl_head *b = d_hash(parent, hash);
Nick Pigginceb5bdc2011-01-07 17:50:05 +11002036 struct hlist_bl_node *node;
Nick Piggin31e6b012011-01-07 17:49:52 +11002037 struct dentry *found = NULL;
Paul E. McKenney665a7582005-11-07 00:59:17 -08002038 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
Nick Pigginb04f7842010-08-18 04:37:34 +10002040 /*
Nick Piggin31e6b012011-01-07 17:49:52 +11002041 * Note: There is significant duplication with __d_lookup_rcu which is
2042 * required to prevent single threaded performance regressions
2043 * especially on architectures where smp_rmb (in seqcounts) are costly.
2044 * Keep the two functions in sync.
2045 */
2046
2047 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10002048 * The hash list is protected using RCU.
2049 *
2050 * Take d_lock when comparing a candidate dentry, to avoid races
2051 * with d_move().
2052 *
2053 * It is possible that concurrent renames can mess up our list
2054 * walk here and result in missing our dentry, resulting in the
2055 * false-negative result. d_lookup() protects against concurrent
2056 * renames using rename_lock seqlock.
2057 *
Namhyung Kimb0a4bb82011-01-22 15:31:32 +09002058 * See Documentation/filesystems/path-lookup.txt for more details.
Nick Pigginb04f7842010-08-18 04:37:34 +10002059 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 rcu_read_lock();
2061
Linus Torvaldsb07ad992011-04-23 22:32:03 -07002062 hlist_bl_for_each_entry_rcu(dentry, node, b, d_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 if (dentry->d_name.hash != hash)
2065 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
2067 spin_lock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 if (dentry->d_parent != parent)
2069 goto next;
Linus Torvaldsd0185c02008-09-29 07:42:57 -07002070 if (d_unhashed(dentry))
2071 goto next;
2072
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 /*
2074 * It is safe to compare names since d_move() cannot
2075 * change the qstr (protected by d_lock).
2076 */
Nick Pigginfb045ad2011-01-07 17:49:55 +11002077 if (parent->d_flags & DCACHE_OP_COMPARE) {
Linus Torvaldsb7d3740d2012-05-04 14:59:14 -07002078 int tlen = dentry->d_name.len;
2079 const char *tname = dentry->d_name.name;
Nick Piggin621e1552011-01-07 17:49:27 +11002080 if (parent->d_op->d_compare(parent, parent->d_inode,
2081 dentry, dentry->d_inode,
Nick Piggin31e6b012011-01-07 17:49:52 +11002082 tlen, tname, name))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 goto next;
2084 } else {
Linus Torvalds3ae62d92012-05-10 12:37:10 -07002085 if (dentry->d_name.len != len)
2086 goto next;
Linus Torvaldsb7d3740d2012-05-04 14:59:14 -07002087 if (dentry_cmp(dentry, str, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 goto next;
2089 }
2090
Nick Pigginb7ab39f2011-01-07 17:49:32 +11002091 dentry->d_count++;
Linus Torvaldsd0185c02008-09-29 07:42:57 -07002092 found = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 spin_unlock(&dentry->d_lock);
2094 break;
2095next:
2096 spin_unlock(&dentry->d_lock);
2097 }
2098 rcu_read_unlock();
2099
2100 return found;
2101}
2102
2103/**
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08002104 * d_hash_and_lookup - hash the qstr then search for a dentry
2105 * @dir: Directory to search in
2106 * @name: qstr of name we wish to find
2107 *
2108 * On hash failure or on lookup failure NULL is returned.
2109 */
2110struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name)
2111{
2112 struct dentry *dentry = NULL;
2113
2114 /*
2115 * Check for a fs-specific hash function. Note that we must
2116 * calculate the standard hash first, as the d_op->d_hash()
2117 * routine may choose to leave the hash value unchanged.
2118 */
2119 name->hash = full_name_hash(name->name, name->len);
Nick Pigginfb045ad2011-01-07 17:49:55 +11002120 if (dir->d_flags & DCACHE_OP_HASH) {
Nick Pigginb1e6a012011-01-07 17:49:28 +11002121 if (dir->d_op->d_hash(dir, dir->d_inode, name) < 0)
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08002122 goto out;
2123 }
2124 dentry = d_lookup(dir, name);
2125out:
2126 return dentry;
2127}
2128
2129/**
Nick Piggin786a5e12011-01-07 17:49:16 +11002130 * d_validate - verify dentry provided from insecure source (deprecated)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 * @dentry: The dentry alleged to be valid child of @dparent
Randy Dunlapff5fdb62011-01-22 20:16:06 -08002132 * @dparent: The parent dentry (known to be valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 *
2134 * An insecure source has sent us a dentry, here we verify it and dget() it.
2135 * This is used by ncpfs in its readdir implementation.
2136 * Zero is returned in the dentry is invalid.
Nick Piggin786a5e12011-01-07 17:49:16 +11002137 *
2138 * This function is slow for big directories, and deprecated, do not use it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 */
Nick Piggind3a23e12011-01-05 20:01:21 +11002140int d_validate(struct dentry *dentry, struct dentry *dparent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141{
Nick Piggin786a5e12011-01-07 17:49:16 +11002142 struct dentry *child;
Nick Piggind3a23e12011-01-05 20:01:21 +11002143
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002144 spin_lock(&dparent->d_lock);
Al Viroa7cca092014-10-26 19:19:16 -04002145 list_for_each_entry(child, &dparent->d_subdirs, d_child) {
Nick Piggin786a5e12011-01-07 17:49:16 +11002146 if (dentry == child) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002147 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Nick Piggindc0474b2011-01-07 17:49:43 +11002148 __dget_dlock(dentry);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002149 spin_unlock(&dentry->d_lock);
2150 spin_unlock(&dparent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 return 1;
2152 }
2153 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002154 spin_unlock(&dparent->d_lock);
Nick Piggin786a5e12011-01-07 17:49:16 +11002155
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 return 0;
2157}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002158EXPORT_SYMBOL(d_validate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
2160/*
2161 * When a file is deleted, we have two options:
2162 * - turn this dentry into a negative dentry
2163 * - unhash this dentry and free it.
2164 *
2165 * Usually, we want to just turn this into
2166 * a negative dentry, but if anybody else is
2167 * currently using the dentry or the inode
2168 * we can't do that and we fall back on removing
2169 * it from the hash queues and waiting for
2170 * it to be deleted later when it has no users
2171 */
2172
2173/**
2174 * d_delete - delete a dentry
2175 * @dentry: The dentry to delete
2176 *
2177 * Turn the dentry into a negative dentry if possible, otherwise
2178 * remove it from the hash queues so it can be deleted later
2179 */
2180
2181void d_delete(struct dentry * dentry)
2182{
Nick Piggin873feea2011-01-07 17:50:06 +11002183 struct inode *inode;
John McCutchan7a91bf72005-08-08 13:52:16 -04002184 int isdir = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 /*
2186 * Are we the only user?
2187 */
Nick Piggin357f8e62011-01-07 17:49:42 +11002188again:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 spin_lock(&dentry->d_lock);
Nick Piggin873feea2011-01-07 17:50:06 +11002190 inode = dentry->d_inode;
2191 isdir = S_ISDIR(inode->i_mode);
Nick Pigginb7ab39f2011-01-07 17:49:32 +11002192 if (dentry->d_count == 1) {
Nick Piggin873feea2011-01-07 17:50:06 +11002193 if (inode && !spin_trylock(&inode->i_lock)) {
Nick Piggin357f8e62011-01-07 17:49:42 +11002194 spin_unlock(&dentry->d_lock);
2195 cpu_relax();
2196 goto again;
2197 }
Al Viro13e3c5e2010-05-21 16:11:04 -04002198 dentry->d_flags &= ~DCACHE_CANT_MOUNT;
Nick Piggin31e6b012011-01-07 17:49:52 +11002199 dentry_unlink_inode(dentry);
John McCutchan7a91bf72005-08-08 13:52:16 -04002200 fsnotify_nameremove(dentry, isdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 return;
2202 }
2203
2204 if (!d_unhashed(dentry))
2205 __d_drop(dentry);
2206
2207 spin_unlock(&dentry->d_lock);
John McCutchan7a91bf72005-08-08 13:52:16 -04002208
2209 fsnotify_nameremove(dentry, isdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002211EXPORT_SYMBOL(d_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
Linus Torvaldsb07ad992011-04-23 22:32:03 -07002213static void __d_rehash(struct dentry * entry, struct hlist_bl_head *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214{
Nick Pigginceb5bdc2011-01-07 17:50:05 +11002215 BUG_ON(!d_unhashed(entry));
Christoph Hellwig1879fd62011-04-25 14:01:36 -04002216 hlist_bl_lock(b);
Linus Torvaldsdea36672011-04-24 07:58:46 -07002217 entry->d_flags |= DCACHE_RCUACCESS;
Linus Torvaldsb07ad992011-04-23 22:32:03 -07002218 hlist_bl_add_head_rcu(&entry->d_hash, b);
Christoph Hellwig1879fd62011-04-25 14:01:36 -04002219 hlist_bl_unlock(b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220}
2221
David Howells770bfad2006-08-22 20:06:07 -04002222static void _d_rehash(struct dentry * entry)
2223{
2224 __d_rehash(entry, d_hash(entry->d_parent, entry->d_name.hash));
2225}
2226
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227/**
2228 * d_rehash - add an entry back to the hash
2229 * @entry: dentry to add to the hash
2230 *
2231 * Adds a dentry to the hash according to its name.
2232 */
2233
2234void d_rehash(struct dentry * entry)
2235{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 spin_lock(&entry->d_lock);
David Howells770bfad2006-08-22 20:06:07 -04002237 _d_rehash(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 spin_unlock(&entry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002240EXPORT_SYMBOL(d_rehash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241
Nick Pigginfb2d5b82011-01-07 17:49:26 +11002242/**
2243 * dentry_update_name_case - update case insensitive dentry with a new name
2244 * @dentry: dentry to be updated
2245 * @name: new name
2246 *
2247 * Update a case insensitive dentry with new case of name.
2248 *
2249 * dentry must have been returned by d_lookup with name @name. Old and new
2250 * name lengths must match (ie. no d_compare which allows mismatched name
2251 * lengths).
2252 *
2253 * Parent inode i_mutex must be held over d_lookup and into this call (to
2254 * keep renames and concurrent inserts, and readdir(2) away).
2255 */
2256void dentry_update_name_case(struct dentry *dentry, struct qstr *name)
2257{
Linus Torvalds7ebfa572011-04-15 07:34:26 -07002258 BUG_ON(!mutex_is_locked(&dentry->d_parent->d_inode->i_mutex));
Nick Pigginfb2d5b82011-01-07 17:49:26 +11002259 BUG_ON(dentry->d_name.len != name->len); /* d_lookup gives this */
2260
Nick Pigginfb2d5b82011-01-07 17:49:26 +11002261 spin_lock(&dentry->d_lock);
Nick Piggin31e6b012011-01-07 17:49:52 +11002262 write_seqcount_begin(&dentry->d_seq);
Nick Pigginfb2d5b82011-01-07 17:49:26 +11002263 memcpy((unsigned char *)dentry->d_name.name, name->name, name->len);
Nick Piggin31e6b012011-01-07 17:49:52 +11002264 write_seqcount_end(&dentry->d_seq);
Nick Pigginfb2d5b82011-01-07 17:49:26 +11002265 spin_unlock(&dentry->d_lock);
Nick Pigginfb2d5b82011-01-07 17:49:26 +11002266}
2267EXPORT_SYMBOL(dentry_update_name_case);
2268
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269static void switch_names(struct dentry *dentry, struct dentry *target)
2270{
Al Viro443c3682014-09-29 14:54:27 -04002271 if (unlikely(dname_external(target))) {
2272 if (unlikely(dname_external(dentry))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 /*
2274 * Both external: swap the pointers
2275 */
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08002276 swap(target->d_name.name, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 } else {
2278 /*
2279 * dentry:internal, target:external. Steal target's
2280 * storage and make target internal.
2281 */
J. Bruce Fields321bcf92007-10-21 16:41:38 -07002282 memcpy(target->d_iname, dentry->d_name.name,
2283 dentry->d_name.len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 dentry->d_name.name = target->d_name.name;
2285 target->d_name.name = target->d_iname;
2286 }
2287 } else {
Al Viro443c3682014-09-29 14:54:27 -04002288 if (unlikely(dname_external(dentry))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 /*
2290 * dentry:external, target:internal. Give dentry's
2291 * storage to target and make dentry internal
2292 */
2293 memcpy(dentry->d_iname, target->d_name.name,
2294 target->d_name.len + 1);
2295 target->d_name.name = dentry->d_name.name;
2296 dentry->d_name.name = dentry->d_iname;
2297 } else {
2298 /*
2299 * Both are internal. Just copy target to dentry
2300 */
2301 memcpy(dentry->d_iname, target->d_name.name,
2302 target->d_name.len + 1);
Al Virodc711ca2008-11-03 15:03:50 -05002303 dentry->d_name.len = target->d_name.len;
2304 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 }
2306 }
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08002307 swap(dentry->d_name.len, target->d_name.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308}
2309
Al Viro443c3682014-09-29 14:54:27 -04002310static void copy_name(struct dentry *dentry, struct dentry *target)
2311{
2312 struct external_name *old_name = NULL;
2313 if (unlikely(dname_external(dentry)))
2314 old_name = external_name(dentry);
2315 if (unlikely(dname_external(target))) {
2316 atomic_inc(&external_name(target)->u.count);
2317 dentry->d_name = target->d_name;
2318 } else {
2319 memcpy(dentry->d_iname, target->d_name.name,
2320 target->d_name.len + 1);
2321 dentry->d_name.name = dentry->d_iname;
2322 dentry->d_name.len = target->d_name.len;
2323 }
2324 if (old_name && likely(atomic_dec_and_test(&old_name->u.count)))
2325 kfree_rcu(old_name, u.head);
2326}
2327
2328
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002329static void dentry_lock_for_move(struct dentry *dentry, struct dentry *target)
2330{
2331 /*
2332 * XXXX: do we really need to take target->d_lock?
2333 */
2334 if (IS_ROOT(dentry) || dentry->d_parent == target->d_parent)
2335 spin_lock(&target->d_parent->d_lock);
2336 else {
2337 if (d_ancestor(dentry->d_parent, target->d_parent)) {
2338 spin_lock(&dentry->d_parent->d_lock);
2339 spin_lock_nested(&target->d_parent->d_lock,
2340 DENTRY_D_LOCK_NESTED);
2341 } else {
2342 spin_lock(&target->d_parent->d_lock);
2343 spin_lock_nested(&dentry->d_parent->d_lock,
2344 DENTRY_D_LOCK_NESTED);
2345 }
2346 }
2347 if (target < dentry) {
2348 spin_lock_nested(&target->d_lock, 2);
2349 spin_lock_nested(&dentry->d_lock, 3);
2350 } else {
2351 spin_lock_nested(&dentry->d_lock, 2);
2352 spin_lock_nested(&target->d_lock, 3);
2353 }
2354}
2355
2356static void dentry_unlock_parents_for_move(struct dentry *dentry,
2357 struct dentry *target)
2358{
2359 if (target->d_parent != dentry->d_parent)
2360 spin_unlock(&dentry->d_parent->d_lock);
2361 if (target->d_parent != target)
2362 spin_unlock(&target->d_parent->d_lock);
2363}
2364
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365/*
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002366 * When switching names, the actual string doesn't strictly have to
2367 * be preserved in the target - because we're dropping the target
2368 * anyway. As such, we can just do a simple memcpy() to copy over
2369 * the new name before we switch.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 *
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002371 * Note that we have to be a lot more careful about getting the hash
2372 * switched - we have to switch the hash value properly even if it
2373 * then no longer matches the actual (corrupted) string of the target.
2374 * The hash value has to match the hash queue that the dentry is on..
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 */
Trond Myklebust9eaef272006-10-21 10:24:20 -07002376/*
Al Viro18367502011-07-12 21:42:24 -04002377 * __d_move - move a dentry
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 * @dentry: entry to move
2379 * @target: new dentry
2380 *
2381 * Update the dcache to reflect the move of a file name. Negative
Jeff Laytonc46c8872011-07-26 13:33:16 -04002382 * dcache entries should not be moved in this way. Caller must hold
2383 * rename_lock, the i_mutex of the source and target directories,
2384 * and the sb->s_vfs_rename_mutex if they differ. See lock_rename().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 */
Al Viro18367502011-07-12 21:42:24 -04002386static void __d_move(struct dentry * dentry, struct dentry * target)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 if (!dentry->d_inode)
2389 printk(KERN_WARNING "VFS: moving negative dcache entry\n");
2390
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002391 BUG_ON(d_ancestor(dentry, target));
2392 BUG_ON(d_ancestor(target, dentry));
2393
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002394 dentry_lock_for_move(dentry, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395
Nick Piggin31e6b012011-01-07 17:49:52 +11002396 write_seqcount_begin(&dentry->d_seq);
2397 write_seqcount_begin(&target->d_seq);
2398
Nick Pigginceb5bdc2011-01-07 17:50:05 +11002399 /* __d_drop does write_seqcount_barrier, but they're OK to nest. */
2400
2401 /*
2402 * Move the dentry to the target hash queue. Don't bother checking
2403 * for the same hash queue because of how unlikely it is.
2404 */
2405 __d_drop(dentry);
Nick Piggin789680d2011-01-07 17:49:30 +11002406 __d_rehash(dentry, d_hash(target->d_parent, target->d_name.hash));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407
2408 /* Unhash the target: dput() will then get rid of it */
2409 __d_drop(target);
2410
Al Viroa7cca092014-10-26 19:19:16 -04002411 list_del(&dentry->d_child);
2412 list_del(&target->d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413
2414 /* Switch the names.. */
Al Viro443c3682014-09-29 14:54:27 -04002415 copy_name(dentry, target);
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08002416 swap(dentry->d_name.hash, target->d_name.hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417
2418 /* ... and switch the parents */
2419 if (IS_ROOT(dentry)) {
2420 dentry->d_parent = target->d_parent;
2421 target->d_parent = target;
Al Viroa7cca092014-10-26 19:19:16 -04002422 INIT_LIST_HEAD(&target->d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 } else {
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08002424 swap(dentry->d_parent, target->d_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425
2426 /* And add them back to the (new) parent lists */
Al Viroa7cca092014-10-26 19:19:16 -04002427 list_add(&target->d_child, &target->d_parent->d_subdirs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 }
2429
Al Viroa7cca092014-10-26 19:19:16 -04002430 list_add(&dentry->d_child, &dentry->d_parent->d_subdirs);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002431
Nick Piggin31e6b012011-01-07 17:49:52 +11002432 write_seqcount_end(&target->d_seq);
2433 write_seqcount_end(&dentry->d_seq);
2434
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002435 dentry_unlock_parents_for_move(dentry, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 spin_unlock(&target->d_lock);
Nick Pigginc32ccd82006-03-25 03:07:09 -08002437 fsnotify_d_move(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 spin_unlock(&dentry->d_lock);
Al Viro18367502011-07-12 21:42:24 -04002439}
2440
2441/*
2442 * d_move - move a dentry
2443 * @dentry: entry to move
2444 * @target: new dentry
2445 *
2446 * Update the dcache to reflect the move of a file name. Negative
Jeff Laytonc46c8872011-07-26 13:33:16 -04002447 * dcache entries should not be moved in this way. See the locking
2448 * requirements for __d_move.
Al Viro18367502011-07-12 21:42:24 -04002449 */
2450void d_move(struct dentry *dentry, struct dentry *target)
2451{
2452 write_seqlock(&rename_lock);
2453 __d_move(dentry, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 write_sequnlock(&rename_lock);
Trond Myklebust9eaef272006-10-21 10:24:20 -07002455}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002456EXPORT_SYMBOL(d_move);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002458/**
2459 * d_ancestor - search for an ancestor
2460 * @p1: ancestor dentry
2461 * @p2: child dentry
2462 *
2463 * Returns the ancestor dentry of p2 which is a child of p1, if p1 is
2464 * an ancestor of p2, else NULL.
Trond Myklebust9eaef272006-10-21 10:24:20 -07002465 */
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002466struct dentry *d_ancestor(struct dentry *p1, struct dentry *p2)
Trond Myklebust9eaef272006-10-21 10:24:20 -07002467{
2468 struct dentry *p;
2469
OGAWA Hirofumi871c0062008-10-16 07:50:27 +09002470 for (p = p2; !IS_ROOT(p); p = p->d_parent) {
Trond Myklebust9eaef272006-10-21 10:24:20 -07002471 if (p->d_parent == p1)
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002472 return p;
Trond Myklebust9eaef272006-10-21 10:24:20 -07002473 }
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002474 return NULL;
Trond Myklebust9eaef272006-10-21 10:24:20 -07002475}
2476
2477/*
2478 * This helper attempts to cope with remotely renamed directories
2479 *
2480 * It assumes that the caller is already holding
Al Viro18367502011-07-12 21:42:24 -04002481 * dentry->d_parent->d_inode->i_mutex, inode->i_lock and rename_lock
Trond Myklebust9eaef272006-10-21 10:24:20 -07002482 *
2483 * Note: If ever the locking in lock_rename() changes, then please
2484 * remember to update this too...
Trond Myklebust9eaef272006-10-21 10:24:20 -07002485 */
Nick Piggin873feea2011-01-07 17:50:06 +11002486static struct dentry *__d_unalias(struct inode *inode,
2487 struct dentry *dentry, struct dentry *alias)
Trond Myklebust9eaef272006-10-21 10:24:20 -07002488{
2489 struct mutex *m1 = NULL, *m2 = NULL;
2490 struct dentry *ret;
2491
2492 /* If alias and dentry share a parent, then no extra locks required */
2493 if (alias->d_parent == dentry->d_parent)
2494 goto out_unalias;
2495
Trond Myklebust9eaef272006-10-21 10:24:20 -07002496 /* See lock_rename() */
2497 ret = ERR_PTR(-EBUSY);
2498 if (!mutex_trylock(&dentry->d_sb->s_vfs_rename_mutex))
2499 goto out_err;
2500 m1 = &dentry->d_sb->s_vfs_rename_mutex;
2501 if (!mutex_trylock(&alias->d_parent->d_inode->i_mutex))
2502 goto out_err;
2503 m2 = &alias->d_parent->d_inode->i_mutex;
2504out_unalias:
Al Viro18367502011-07-12 21:42:24 -04002505 __d_move(alias, dentry);
Trond Myklebust9eaef272006-10-21 10:24:20 -07002506 ret = alias;
2507out_err:
Nick Piggin873feea2011-01-07 17:50:06 +11002508 spin_unlock(&inode->i_lock);
Trond Myklebust9eaef272006-10-21 10:24:20 -07002509 if (m2)
2510 mutex_unlock(m2);
2511 if (m1)
2512 mutex_unlock(m1);
2513 return ret;
2514}
2515
2516/*
David Howells770bfad2006-08-22 20:06:07 -04002517 * Prepare an anonymous dentry for life in the superblock's dentry tree as a
2518 * named dentry in place of the dentry to be replaced.
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002519 * returns with anon->d_lock held!
David Howells770bfad2006-08-22 20:06:07 -04002520 */
2521static void __d_materialise_dentry(struct dentry *dentry, struct dentry *anon)
2522{
2523 struct dentry *dparent, *aparent;
2524
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002525 dentry_lock_for_move(anon, dentry);
David Howells770bfad2006-08-22 20:06:07 -04002526
Nick Piggin31e6b012011-01-07 17:49:52 +11002527 write_seqcount_begin(&dentry->d_seq);
2528 write_seqcount_begin(&anon->d_seq);
2529
David Howells770bfad2006-08-22 20:06:07 -04002530 dparent = dentry->d_parent;
2531 aparent = anon->d_parent;
2532
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002533 switch_names(dentry, anon);
2534 swap(dentry->d_name.hash, anon->d_name.hash);
2535
David Howells770bfad2006-08-22 20:06:07 -04002536 dentry->d_parent = (aparent == anon) ? dentry : aparent;
Al Viroa7cca092014-10-26 19:19:16 -04002537 list_del(&dentry->d_child);
David Howells770bfad2006-08-22 20:06:07 -04002538 if (!IS_ROOT(dentry))
Al Viroa7cca092014-10-26 19:19:16 -04002539 list_add(&dentry->d_child, &dentry->d_parent->d_subdirs);
David Howells770bfad2006-08-22 20:06:07 -04002540 else
Al Viroa7cca092014-10-26 19:19:16 -04002541 INIT_LIST_HEAD(&dentry->d_child);
David Howells770bfad2006-08-22 20:06:07 -04002542
2543 anon->d_parent = (dparent == dentry) ? anon : dparent;
Al Viroa7cca092014-10-26 19:19:16 -04002544 list_del(&anon->d_child);
David Howells770bfad2006-08-22 20:06:07 -04002545 if (!IS_ROOT(anon))
Al Viroa7cca092014-10-26 19:19:16 -04002546 list_add(&anon->d_child, &anon->d_parent->d_subdirs);
David Howells770bfad2006-08-22 20:06:07 -04002547 else
Al Viroa7cca092014-10-26 19:19:16 -04002548 INIT_LIST_HEAD(&anon->d_child);
David Howells770bfad2006-08-22 20:06:07 -04002549
Nick Piggin31e6b012011-01-07 17:49:52 +11002550 write_seqcount_end(&dentry->d_seq);
2551 write_seqcount_end(&anon->d_seq);
2552
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002553 dentry_unlock_parents_for_move(anon, dentry);
2554 spin_unlock(&dentry->d_lock);
2555
2556 /* anon->d_lock still locked, returns locked */
David Howells770bfad2006-08-22 20:06:07 -04002557 anon->d_flags &= ~DCACHE_DISCONNECTED;
2558}
2559
2560/**
2561 * d_materialise_unique - introduce an inode into the tree
2562 * @dentry: candidate dentry
2563 * @inode: inode to bind to the dentry, to which aliases may be attached
2564 *
2565 * Introduces an dentry into the tree, substituting an extant disconnected
Jeff Laytonc46c8872011-07-26 13:33:16 -04002566 * root directory alias in its place if there is one. Caller must hold the
2567 * i_mutex of the parent directory.
David Howells770bfad2006-08-22 20:06:07 -04002568 */
2569struct dentry *d_materialise_unique(struct dentry *dentry, struct inode *inode)
2570{
Trond Myklebust9eaef272006-10-21 10:24:20 -07002571 struct dentry *actual;
David Howells770bfad2006-08-22 20:06:07 -04002572
2573 BUG_ON(!d_unhashed(dentry));
2574
David Howells770bfad2006-08-22 20:06:07 -04002575 if (!inode) {
2576 actual = dentry;
OGAWA Hirofumi360da902008-10-16 07:50:28 +09002577 __d_instantiate(dentry, NULL);
Nick Piggin357f8e62011-01-07 17:49:42 +11002578 d_rehash(actual);
2579 goto out_nolock;
David Howells770bfad2006-08-22 20:06:07 -04002580 }
2581
Nick Piggin873feea2011-01-07 17:50:06 +11002582 spin_lock(&inode->i_lock);
Nick Piggin357f8e62011-01-07 17:49:42 +11002583
Trond Myklebust9eaef272006-10-21 10:24:20 -07002584 if (S_ISDIR(inode->i_mode)) {
2585 struct dentry *alias;
David Howells770bfad2006-08-22 20:06:07 -04002586
Trond Myklebust9eaef272006-10-21 10:24:20 -07002587 /* Does an aliased dentry already exist? */
2588 alias = __d_find_alias(inode, 0);
2589 if (alias) {
2590 actual = alias;
Al Viro18367502011-07-12 21:42:24 -04002591 write_seqlock(&rename_lock);
2592
2593 if (d_ancestor(alias, dentry)) {
2594 /* Check for loops */
2595 actual = ERR_PTR(-ELOOP);
Michel Lespinasseb18dafc2012-03-26 17:32:44 -07002596 spin_unlock(&inode->i_lock);
Al Viro18367502011-07-12 21:42:24 -04002597 } else if (IS_ROOT(alias)) {
2598 /* Is this an anonymous mountpoint that we
2599 * could splice into our tree? */
Trond Myklebust9eaef272006-10-21 10:24:20 -07002600 __d_materialise_dentry(dentry, alias);
Al Viro18367502011-07-12 21:42:24 -04002601 write_sequnlock(&rename_lock);
Trond Myklebust9eaef272006-10-21 10:24:20 -07002602 __d_drop(alias);
2603 goto found;
Al Viro18367502011-07-12 21:42:24 -04002604 } else {
2605 /* Nope, but we must(!) avoid directory
Michel Lespinasseb18dafc2012-03-26 17:32:44 -07002606 * aliasing. This drops inode->i_lock */
Al Viro18367502011-07-12 21:42:24 -04002607 actual = __d_unalias(inode, dentry, alias);
Trond Myklebust9eaef272006-10-21 10:24:20 -07002608 }
Al Viro18367502011-07-12 21:42:24 -04002609 write_sequnlock(&rename_lock);
David Howellsdd179942011-08-16 15:31:30 +01002610 if (IS_ERR(actual)) {
2611 if (PTR_ERR(actual) == -ELOOP)
2612 pr_warn_ratelimited(
2613 "VFS: Lookup of '%s' in %s %s"
2614 " would have caused loop\n",
2615 dentry->d_name.name,
2616 inode->i_sb->s_type->name,
2617 inode->i_sb->s_id);
Trond Myklebust9eaef272006-10-21 10:24:20 -07002618 dput(alias);
David Howellsdd179942011-08-16 15:31:30 +01002619 }
Trond Myklebust9eaef272006-10-21 10:24:20 -07002620 goto out_nolock;
2621 }
David Howells770bfad2006-08-22 20:06:07 -04002622 }
2623
2624 /* Add a unique reference */
2625 actual = __d_instantiate_unique(dentry, inode);
2626 if (!actual)
2627 actual = dentry;
Nick Piggin357f8e62011-01-07 17:49:42 +11002628 else
2629 BUG_ON(!d_unhashed(actual));
David Howells770bfad2006-08-22 20:06:07 -04002630
David Howells770bfad2006-08-22 20:06:07 -04002631 spin_lock(&actual->d_lock);
2632found:
2633 _d_rehash(actual);
2634 spin_unlock(&actual->d_lock);
Nick Piggin873feea2011-01-07 17:50:06 +11002635 spin_unlock(&inode->i_lock);
Trond Myklebust9eaef272006-10-21 10:24:20 -07002636out_nolock:
David Howells770bfad2006-08-22 20:06:07 -04002637 if (actual == dentry) {
2638 security_d_instantiate(dentry, inode);
2639 return NULL;
2640 }
2641
2642 iput(inode);
2643 return actual;
David Howells770bfad2006-08-22 20:06:07 -04002644}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002645EXPORT_SYMBOL_GPL(d_materialise_unique);
David Howells770bfad2006-08-22 20:06:07 -04002646
Miklos Szeredicdd16d02008-06-23 18:11:53 +02002647static int prepend(char **buffer, int *buflen, const char *str, int namelen)
Ram Pai6092d042008-03-27 13:06:20 +01002648{
2649 *buflen -= namelen;
2650 if (*buflen < 0)
2651 return -ENAMETOOLONG;
2652 *buffer -= namelen;
2653 memcpy(*buffer, str, namelen);
2654 return 0;
2655}
2656
Miklos Szeredicdd16d02008-06-23 18:11:53 +02002657static int prepend_name(char **buffer, int *buflen, struct qstr *name)
2658{
2659 return prepend(buffer, buflen, name->name, name->len);
2660}
2661
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662/**
Randy Dunlap208898c2010-11-18 15:02:49 -08002663 * prepend_path - Prepend path string to a buffer
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002664 * @path: the dentry/vfsmount to report
Al Viro02125a82011-12-05 08:43:34 -05002665 * @root: root vfsmnt/dentry
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002666 * @buffer: pointer to the end of the buffer
2667 * @buflen: pointer to buffer length
2668 *
Nick Piggin949854d2011-01-07 17:49:37 +11002669 * Caller holds the rename_lock.
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002670 */
Al Viro02125a82011-12-05 08:43:34 -05002671static int prepend_path(const struct path *path,
2672 const struct path *root,
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002673 char **buffer, int *buflen)
2674{
2675 struct dentry *dentry = path->dentry;
2676 struct vfsmount *vfsmnt = path->mnt;
Al Viro0714a532011-11-24 22:19:58 -05002677 struct mount *mnt = real_mount(vfsmnt);
Eric W. Biederman33e2de82015-08-15 13:36:12 -05002678 char *orig_buffer = *buffer;
2679 int orig_len = *buflen;
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002680 bool slash = false;
2681 int error = 0;
2682
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002683 while (dentry != root->dentry || vfsmnt != root->mnt) {
2684 struct dentry * parent;
2685
2686 if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
Eric W. Biederman33e2de82015-08-15 13:36:12 -05002687 /* Escaped? */
2688 if (dentry != vfsmnt->mnt_root) {
2689 *buffer = orig_buffer;
2690 *buflen = orig_len;
2691 slash = false;
2692 error = 3;
2693 goto global_root;
2694 }
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002695 /* Global root? */
Al Viro676da582011-11-24 21:47:05 -05002696 if (!mnt_has_parent(mnt))
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002697 goto global_root;
Al Viroa73324d2011-11-24 22:25:07 -05002698 dentry = mnt->mnt_mountpoint;
Al Viro0714a532011-11-24 22:19:58 -05002699 mnt = mnt->mnt_parent;
2700 vfsmnt = &mnt->mnt;
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002701 continue;
2702 }
2703 parent = dentry->d_parent;
2704 prefetch(parent);
Nick Piggin9abca362011-01-07 17:49:36 +11002705 spin_lock(&dentry->d_lock);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002706 error = prepend_name(buffer, buflen, &dentry->d_name);
Nick Piggin9abca362011-01-07 17:49:36 +11002707 spin_unlock(&dentry->d_lock);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002708 if (!error)
2709 error = prepend(buffer, buflen, "/", 1);
2710 if (error)
2711 break;
2712
2713 slash = true;
2714 dentry = parent;
2715 }
2716
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002717 if (!error && !slash)
2718 error = prepend(buffer, buflen, "/", 1);
2719
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002720 return error;
2721
2722global_root:
2723 /*
2724 * Filesystems needing to implement special "root names"
2725 * should do so with ->d_dname()
2726 */
2727 if (IS_ROOT(dentry) &&
2728 (dentry->d_name.len != 1 || dentry->d_name.name[0] != '/')) {
2729 WARN(1, "Root dentry has weird name <%.*s>\n",
2730 (int) dentry->d_name.len, dentry->d_name.name);
2731 }
Al Viro02125a82011-12-05 08:43:34 -05002732 if (!slash)
2733 error = prepend(buffer, buflen, "/", 1);
2734 if (!error)
Al Virod9eb5042012-06-09 00:59:08 -04002735 error = is_mounted(vfsmnt) ? 1 : 2;
Al Viro4d9597b2013-03-26 18:25:57 -04002736 return error;
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002737}
2738
2739/**
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +02002740 * __d_path - return the path of a dentry
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002741 * @path: the dentry/vfsmount to report
Al Viro02125a82011-12-05 08:43:34 -05002742 * @root: root vfsmnt/dentry
Randy Dunlapcd956a12010-08-14 13:05:31 -07002743 * @buf: buffer to return value in
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 * @buflen: buffer length
2745 *
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002746 * Convert a dentry into an ASCII path name.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 *
Arjan van de Ven52afeef2008-12-01 14:35:00 -08002748 * Returns a pointer into the buffer or an error code if the
2749 * path was too long.
Linus Torvalds552ce542007-02-13 12:08:18 -08002750 *
Christoph Hellwigbe148242010-10-10 05:36:21 -04002751 * "buflen" should be positive.
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002752 *
Al Viro02125a82011-12-05 08:43:34 -05002753 * If the path is not reachable from the supplied root, return %NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 */
Al Viro02125a82011-12-05 08:43:34 -05002755char *__d_path(const struct path *path,
2756 const struct path *root,
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002757 char *buf, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758{
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002759 char *res = buf + buflen;
2760 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002762 prepend(&res, &buflen, "\0", 1);
Andi Kleenf0769502012-05-08 13:32:02 +09302763 br_read_lock(&vfsmount_lock);
Nick Piggin949854d2011-01-07 17:49:37 +11002764 write_seqlock(&rename_lock);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002765 error = prepend_path(path, root, &res, &buflen);
Nick Piggin949854d2011-01-07 17:49:37 +11002766 write_sequnlock(&rename_lock);
Andi Kleenf0769502012-05-08 13:32:02 +09302767 br_read_unlock(&vfsmount_lock);
Christoph Hellwigbe148242010-10-10 05:36:21 -04002768
Al Viro02125a82011-12-05 08:43:34 -05002769 if (error < 0)
2770 return ERR_PTR(error);
2771 if (error > 0)
2772 return NULL;
2773 return res;
2774}
2775
2776char *d_absolute_path(const struct path *path,
2777 char *buf, int buflen)
2778{
2779 struct path root = {};
2780 char *res = buf + buflen;
2781 int error;
2782
2783 prepend(&res, &buflen, "\0", 1);
Andi Kleenf0769502012-05-08 13:32:02 +09302784 br_read_lock(&vfsmount_lock);
Al Viro02125a82011-12-05 08:43:34 -05002785 write_seqlock(&rename_lock);
2786 error = prepend_path(path, &root, &res, &buflen);
2787 write_sequnlock(&rename_lock);
Andi Kleenf0769502012-05-08 13:32:02 +09302788 br_read_unlock(&vfsmount_lock);
Al Viro02125a82011-12-05 08:43:34 -05002789
2790 if (error > 1)
2791 error = -EINVAL;
2792 if (error < 0)
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002793 return ERR_PTR(error);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002794 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795}
2796
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002797/*
2798 * same as __d_path but appends "(deleted)" for unlinked files.
2799 */
Al Viro02125a82011-12-05 08:43:34 -05002800static int path_with_deleted(const struct path *path,
2801 const struct path *root,
2802 char **buf, int *buflen)
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002803{
2804 prepend(buf, buflen, "\0", 1);
2805 if (d_unlinked(path->dentry)) {
2806 int error = prepend(buf, buflen, " (deleted)", 10);
2807 if (error)
2808 return error;
2809 }
2810
2811 return prepend_path(path, root, buf, buflen);
2812}
2813
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002814static int prepend_unreachable(char **buffer, int *buflen)
2815{
2816 return prepend(buffer, buflen, "(unreachable)", 13);
2817}
2818
Jan Bluncka03a8a702008-02-14 19:38:32 -08002819/**
2820 * d_path - return the path of a dentry
Jan Blunckcf28b482008-02-14 19:38:44 -08002821 * @path: path to report
Jan Bluncka03a8a702008-02-14 19:38:32 -08002822 * @buf: buffer to return value in
2823 * @buflen: buffer length
2824 *
2825 * Convert a dentry into an ASCII path name. If the entry has been deleted
2826 * the string " (deleted)" is appended. Note that this is ambiguous.
2827 *
Arjan van de Ven52afeef2008-12-01 14:35:00 -08002828 * Returns a pointer into the buffer or an error code if the path was
2829 * too long. Note: Callers should use the returned pointer, not the passed
2830 * in buffer, to use the name! The implementation often starts at an offset
2831 * into the buffer, and may leave 0 bytes at the start.
Jan Bluncka03a8a702008-02-14 19:38:32 -08002832 *
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +02002833 * "buflen" should be positive.
Jan Bluncka03a8a702008-02-14 19:38:32 -08002834 */
Jan Engelhardt20d4fdc2008-06-09 16:40:36 -07002835char *d_path(const struct path *path, char *buf, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836{
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002837 char *res = buf + buflen;
Jan Blunck6ac08c32008-02-14 19:34:38 -08002838 struct path root;
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002839 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840
Eric Dumazetc23fbb62007-05-08 00:26:18 -07002841 /*
2842 * We have various synthetic filesystems that never get mounted. On
2843 * these filesystems dentries are never used for lookup purposes, and
2844 * thus don't need to be hashed. They also don't need a name until a
2845 * user wants to identify the object in /proc/pid/fd/. The little hack
2846 * below allows us to generate a name for these objects on demand:
2847 */
Jan Blunckcf28b482008-02-14 19:38:44 -08002848 if (path->dentry->d_op && path->dentry->d_op->d_dname)
2849 return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
Eric Dumazetc23fbb62007-05-08 00:26:18 -07002850
Miklos Szeredif7ad3c62010-08-10 11:41:36 +02002851 get_fs_root(current->fs, &root);
Andi Kleenf0769502012-05-08 13:32:02 +09302852 br_read_lock(&vfsmount_lock);
Nick Piggin949854d2011-01-07 17:49:37 +11002853 write_seqlock(&rename_lock);
Al Viro02125a82011-12-05 08:43:34 -05002854 error = path_with_deleted(path, &root, &res, &buflen);
Al Viro4d9597b2013-03-26 18:25:57 -04002855 write_sequnlock(&rename_lock);
Andi Kleenf0769502012-05-08 13:32:02 +09302856 br_read_unlock(&vfsmount_lock);
Al Viro02125a82011-12-05 08:43:34 -05002857 if (error < 0)
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002858 res = ERR_PTR(error);
Jan Blunck6ac08c32008-02-14 19:34:38 -08002859 path_put(&root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 return res;
2861}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002862EXPORT_SYMBOL(d_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002864/**
2865 * d_path_with_unreachable - return the path of a dentry
2866 * @path: path to report
2867 * @buf: buffer to return value in
2868 * @buflen: buffer length
2869 *
2870 * The difference from d_path() is that this prepends "(unreachable)"
2871 * to paths which are unreachable from the current process' root.
2872 */
2873char *d_path_with_unreachable(const struct path *path, char *buf, int buflen)
2874{
2875 char *res = buf + buflen;
2876 struct path root;
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002877 int error;
2878
2879 if (path->dentry->d_op && path->dentry->d_op->d_dname)
2880 return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
2881
2882 get_fs_root(current->fs, &root);
Nick Piggin949854d2011-01-07 17:49:37 +11002883 write_seqlock(&rename_lock);
Al Viro02125a82011-12-05 08:43:34 -05002884 error = path_with_deleted(path, &root, &res, &buflen);
2885 if (error > 0)
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002886 error = prepend_unreachable(&res, &buflen);
Nick Piggin949854d2011-01-07 17:49:37 +11002887 write_sequnlock(&rename_lock);
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002888 path_put(&root);
2889 if (error)
2890 res = ERR_PTR(error);
2891
2892 return res;
2893}
2894
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895/*
Eric Dumazetc23fbb62007-05-08 00:26:18 -07002896 * Helper function for dentry_operations.d_dname() members
2897 */
2898char *dynamic_dname(struct dentry *dentry, char *buffer, int buflen,
2899 const char *fmt, ...)
2900{
2901 va_list args;
2902 char temp[64];
2903 int sz;
2904
2905 va_start(args, fmt);
2906 sz = vsnprintf(temp, sizeof(temp), fmt, args) + 1;
2907 va_end(args);
2908
2909 if (sz > sizeof(temp) || sz > buflen)
2910 return ERR_PTR(-ENAMETOOLONG);
2911
2912 buffer += buflen - sz;
2913 return memcpy(buffer, temp, sz);
2914}
2915
2916/*
Ram Pai6092d042008-03-27 13:06:20 +01002917 * Write full pathname from the root of the filesystem into the buffer.
2918 */
Nick Pigginec2447c2011-01-07 17:49:29 +11002919static char *__dentry_path(struct dentry *dentry, char *buf, int buflen)
Ram Pai6092d042008-03-27 13:06:20 +01002920{
2921 char *end = buf + buflen;
2922 char *retval;
2923
Ram Pai6092d042008-03-27 13:06:20 +01002924 prepend(&end, &buflen, "\0", 1);
Ram Pai6092d042008-03-27 13:06:20 +01002925 if (buflen < 1)
2926 goto Elong;
2927 /* Get '/' right */
2928 retval = end-1;
2929 *retval = '/';
2930
Miklos Szeredicdd16d02008-06-23 18:11:53 +02002931 while (!IS_ROOT(dentry)) {
2932 struct dentry *parent = dentry->d_parent;
Nick Piggin9abca362011-01-07 17:49:36 +11002933 int error;
Ram Pai6092d042008-03-27 13:06:20 +01002934
Ram Pai6092d042008-03-27 13:06:20 +01002935 prefetch(parent);
Nick Piggin9abca362011-01-07 17:49:36 +11002936 spin_lock(&dentry->d_lock);
2937 error = prepend_name(&end, &buflen, &dentry->d_name);
2938 spin_unlock(&dentry->d_lock);
2939 if (error != 0 || prepend(&end, &buflen, "/", 1) != 0)
Ram Pai6092d042008-03-27 13:06:20 +01002940 goto Elong;
2941
2942 retval = end;
2943 dentry = parent;
2944 }
Al Viroc1031352010-06-06 22:31:14 -04002945 return retval;
2946Elong:
2947 return ERR_PTR(-ENAMETOOLONG);
2948}
Nick Pigginec2447c2011-01-07 17:49:29 +11002949
2950char *dentry_path_raw(struct dentry *dentry, char *buf, int buflen)
2951{
2952 char *retval;
2953
Nick Piggin949854d2011-01-07 17:49:37 +11002954 write_seqlock(&rename_lock);
Nick Pigginec2447c2011-01-07 17:49:29 +11002955 retval = __dentry_path(dentry, buf, buflen);
Nick Piggin949854d2011-01-07 17:49:37 +11002956 write_sequnlock(&rename_lock);
Nick Pigginec2447c2011-01-07 17:49:29 +11002957
2958 return retval;
2959}
2960EXPORT_SYMBOL(dentry_path_raw);
Al Viroc1031352010-06-06 22:31:14 -04002961
2962char *dentry_path(struct dentry *dentry, char *buf, int buflen)
2963{
2964 char *p = NULL;
2965 char *retval;
2966
Nick Piggin949854d2011-01-07 17:49:37 +11002967 write_seqlock(&rename_lock);
Al Viroc1031352010-06-06 22:31:14 -04002968 if (d_unlinked(dentry)) {
2969 p = buf + buflen;
2970 if (prepend(&p, &buflen, "//deleted", 10) != 0)
2971 goto Elong;
2972 buflen++;
2973 }
2974 retval = __dentry_path(dentry, buf, buflen);
Nick Piggin949854d2011-01-07 17:49:37 +11002975 write_sequnlock(&rename_lock);
Al Viroc1031352010-06-06 22:31:14 -04002976 if (!IS_ERR(retval) && p)
2977 *p = '/'; /* restore '/' overriden with '\0' */
Ram Pai6092d042008-03-27 13:06:20 +01002978 return retval;
2979Elong:
Ram Pai6092d042008-03-27 13:06:20 +01002980 return ERR_PTR(-ENAMETOOLONG);
2981}
2982
2983/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 * NOTE! The user-level library version returns a
2985 * character pointer. The kernel system call just
2986 * returns the length of the buffer filled (which
2987 * includes the ending '\0' character), or a negative
2988 * error value. So libc would do something like
2989 *
2990 * char *getcwd(char * buf, size_t size)
2991 * {
2992 * int retval;
2993 *
2994 * retval = sys_getcwd(buf, size);
2995 * if (retval >= 0)
2996 * return buf;
2997 * errno = -retval;
2998 * return NULL;
2999 * }
3000 */
Heiko Carstens3cdad422009-01-14 14:14:22 +01003001SYSCALL_DEFINE2(getcwd, char __user *, buf, unsigned long, size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002{
Linus Torvalds552ce542007-02-13 12:08:18 -08003003 int error;
Jan Blunck6ac08c32008-02-14 19:34:38 -08003004 struct path pwd, root;
Linus Torvalds552ce542007-02-13 12:08:18 -08003005 char *page = (char *) __get_free_page(GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006
3007 if (!page)
3008 return -ENOMEM;
3009
Miklos Szeredif7ad3c62010-08-10 11:41:36 +02003010 get_fs_root_and_pwd(current->fs, &root, &pwd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011
Linus Torvalds552ce542007-02-13 12:08:18 -08003012 error = -ENOENT;
Andi Kleenf0769502012-05-08 13:32:02 +09303013 br_read_lock(&vfsmount_lock);
Nick Piggin949854d2011-01-07 17:49:37 +11003014 write_seqlock(&rename_lock);
Alexey Dobriyanf3da3922009-05-04 03:32:03 +04003015 if (!d_unlinked(pwd.dentry)) {
Linus Torvalds552ce542007-02-13 12:08:18 -08003016 unsigned long len;
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02003017 char *cwd = page + PAGE_SIZE;
3018 int buflen = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02003020 prepend(&cwd, &buflen, "\0", 1);
Al Viro02125a82011-12-05 08:43:34 -05003021 error = prepend_path(&pwd, &root, &cwd, &buflen);
Nick Piggin949854d2011-01-07 17:49:37 +11003022 write_sequnlock(&rename_lock);
Andi Kleenf0769502012-05-08 13:32:02 +09303023 br_read_unlock(&vfsmount_lock);
Linus Torvalds552ce542007-02-13 12:08:18 -08003024
Al Viro02125a82011-12-05 08:43:34 -05003025 if (error < 0)
Linus Torvalds552ce542007-02-13 12:08:18 -08003026 goto out;
3027
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02003028 /* Unreachable from current root */
Al Viro02125a82011-12-05 08:43:34 -05003029 if (error > 0) {
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02003030 error = prepend_unreachable(&cwd, &buflen);
3031 if (error)
3032 goto out;
3033 }
3034
Linus Torvalds552ce542007-02-13 12:08:18 -08003035 error = -ERANGE;
3036 len = PAGE_SIZE + page - cwd;
3037 if (len <= size) {
3038 error = len;
3039 if (copy_to_user(buf, cwd, len))
3040 error = -EFAULT;
3041 }
Nick Piggin949854d2011-01-07 17:49:37 +11003042 } else {
3043 write_sequnlock(&rename_lock);
Andi Kleenf0769502012-05-08 13:32:02 +09303044 br_read_unlock(&vfsmount_lock);
Nick Piggin949854d2011-01-07 17:49:37 +11003045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046
3047out:
Jan Blunck6ac08c32008-02-14 19:34:38 -08003048 path_put(&pwd);
3049 path_put(&root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 free_page((unsigned long) page);
3051 return error;
3052}
3053
3054/*
3055 * Test whether new_dentry is a subdirectory of old_dentry.
3056 *
3057 * Trivially implemented using the dcache structure
3058 */
3059
3060/**
3061 * is_subdir - is new dentry a subdirectory of old_dentry
3062 * @new_dentry: new dentry
3063 * @old_dentry: old dentry
3064 *
3065 * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
3066 * Returns 0 otherwise.
3067 * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
3068 */
3069
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09003070int is_subdir(struct dentry *new_dentry, struct dentry *old_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071{
3072 int result;
Nick Piggin949854d2011-01-07 17:49:37 +11003073 unsigned seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09003075 if (new_dentry == old_dentry)
3076 return 1;
3077
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09003078 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 /* for restarting inner loop in case of seq retry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 seq = read_seqbegin(&rename_lock);
Nick Piggin949854d2011-01-07 17:49:37 +11003081 /*
3082 * Need rcu_readlock to protect against the d_parent trashing
3083 * due to d_move
3084 */
3085 rcu_read_lock();
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09003086 if (d_ancestor(old_dentry, new_dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 result = 1;
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09003088 else
3089 result = 0;
Nick Piggin949854d2011-01-07 17:49:37 +11003090 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 } while (read_seqretry(&rename_lock, seq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092
3093 return result;
3094}
3095
3096void d_genocide(struct dentry *root)
3097{
Nick Piggin949854d2011-01-07 17:49:37 +11003098 struct dentry *this_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 struct list_head *next;
Nick Piggin949854d2011-01-07 17:49:37 +11003100 unsigned seq;
Nick Piggin58db63d2011-01-07 17:49:39 +11003101 int locked = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102
Nick Piggin949854d2011-01-07 17:49:37 +11003103 seq = read_seqbegin(&rename_lock);
Nick Piggin58db63d2011-01-07 17:49:39 +11003104again:
3105 this_parent = root;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11003106 spin_lock(&this_parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107repeat:
3108 next = this_parent->d_subdirs.next;
3109resume:
3110 while (next != &this_parent->d_subdirs) {
3111 struct list_head *tmp = next;
Al Viroa7cca092014-10-26 19:19:16 -04003112 struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 next = tmp->next;
Nick Piggin949854d2011-01-07 17:49:37 +11003114
Nick Pigginda502952011-01-07 17:49:33 +11003115 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
3116 if (d_unhashed(dentry) || !dentry->d_inode) {
3117 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 continue;
Nick Pigginda502952011-01-07 17:49:33 +11003119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 if (!list_empty(&dentry->d_subdirs)) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11003121 spin_unlock(&this_parent->d_lock);
3122 spin_release(&dentry->d_lock.dep_map, 1, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 this_parent = dentry;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11003124 spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 goto repeat;
3126 }
Nick Piggin949854d2011-01-07 17:49:37 +11003127 if (!(dentry->d_flags & DCACHE_GENOCIDE)) {
3128 dentry->d_flags |= DCACHE_GENOCIDE;
3129 dentry->d_count--;
3130 }
Nick Pigginb7ab39f2011-01-07 17:49:32 +11003131 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 }
Al Viro01306a52014-10-26 19:31:10 -04003133 rcu_read_lock();
3134ascend:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 if (this_parent != root) {
Linus Torvaldsc826cb72011-03-15 15:29:21 -07003136 struct dentry *child = this_parent;
Nick Piggin949854d2011-01-07 17:49:37 +11003137 if (!(this_parent->d_flags & DCACHE_GENOCIDE)) {
3138 this_parent->d_flags |= DCACHE_GENOCIDE;
3139 this_parent->d_count--;
3140 }
Al Viro01306a52014-10-26 19:31:10 -04003141 this_parent = child->d_parent;
3142
3143 spin_unlock(&child->d_lock);
3144 spin_lock(&this_parent->d_lock);
3145
3146 /* might go back up the wrong parent if we have had a rename */
3147 if (!locked && read_seqretry(&rename_lock, seq))
Nick Piggin949854d2011-01-07 17:49:37 +11003148 goto rename_retry;
Al Viroa7cca092014-10-26 19:19:16 -04003149 next = child->d_child.next;
Al Viro01306a52014-10-26 19:31:10 -04003150 while (unlikely(child->d_flags & DCACHE_DENTRY_KILLED)) {
3151 if (next == &this_parent->d_subdirs)
3152 goto ascend;
3153 child = list_entry(next, struct dentry, d_child);
3154 next = next->next;
3155 }
3156 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 goto resume;
3158 }
Nick Piggin58db63d2011-01-07 17:49:39 +11003159 if (!locked && read_seqretry(&rename_lock, seq))
Nick Piggin949854d2011-01-07 17:49:37 +11003160 goto rename_retry;
Al Viro01306a52014-10-26 19:31:10 -04003161 spin_unlock(&this_parent->d_lock);
3162 rcu_read_unlock();
Nick Piggin58db63d2011-01-07 17:49:39 +11003163 if (locked)
3164 write_sequnlock(&rename_lock);
3165 return;
3166
3167rename_retry:
Al Viro01306a52014-10-26 19:31:10 -04003168 spin_unlock(&this_parent->d_lock);
3169 rcu_read_unlock();
Miklos Szeredidb8a9e22012-09-17 22:23:30 +02003170 if (locked)
3171 goto again;
Nick Piggin58db63d2011-01-07 17:49:39 +11003172 locked = 1;
3173 write_seqlock(&rename_lock);
3174 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175}
3176
Al Virob0d54302013-06-07 01:20:27 -04003177void d_tmpfile(struct dentry *dentry, struct inode *inode)
3178{
3179 inode_dec_link_count(inode);
3180 BUG_ON(dentry->d_name.name != dentry->d_iname ||
3181 !list_empty(&dentry->d_u.d_alias) ||
3182 !d_unlinked(dentry));
3183 spin_lock(&dentry->d_parent->d_lock);
3184 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
3185 dentry->d_name.len = sprintf(dentry->d_iname, "#%llu",
3186 (unsigned long long)inode->i_ino);
3187 spin_unlock(&dentry->d_lock);
3188 spin_unlock(&dentry->d_parent->d_lock);
3189 d_instantiate(dentry, inode);
3190}
3191EXPORT_SYMBOL(d_tmpfile);
3192
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193/**
3194 * find_inode_number - check for dentry with name
3195 * @dir: directory to check
3196 * @name: Name to find.
3197 *
3198 * Check whether a dentry already exists for the given name,
3199 * and return the inode number if it has an inode. Otherwise
3200 * 0 is returned.
3201 *
3202 * This routine is used to post-process directory listings for
3203 * filesystems using synthetic inode numbers, and is necessary
3204 * to keep getcwd() working.
3205 */
3206
3207ino_t find_inode_number(struct dentry *dir, struct qstr *name)
3208{
3209 struct dentry * dentry;
3210 ino_t ino = 0;
3211
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08003212 dentry = d_hash_and_lookup(dir, name);
3213 if (dentry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 if (dentry->d_inode)
3215 ino = dentry->d_inode->i_ino;
3216 dput(dentry);
3217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 return ino;
3219}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07003220EXPORT_SYMBOL(find_inode_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221
3222static __initdata unsigned long dhash_entries;
3223static int __init set_dhash_entries(char *str)
3224{
3225 if (!str)
3226 return 0;
3227 dhash_entries = simple_strtoul(str, &str, 0);
3228 return 1;
3229}
3230__setup("dhash_entries=", set_dhash_entries);
3231
3232static void __init dcache_init_early(void)
3233{
Dimitri Sivanich074b8512012-02-08 12:39:07 -08003234 unsigned int loop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235
3236 /* If hashes are distributed across NUMA nodes, defer
3237 * hash allocation until vmalloc space is available.
3238 */
3239 if (hashdist)
3240 return;
3241
3242 dentry_hashtable =
3243 alloc_large_system_hash("Dentry cache",
Linus Torvaldsb07ad992011-04-23 22:32:03 -07003244 sizeof(struct hlist_bl_head),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 dhash_entries,
3246 13,
3247 HASH_EARLY,
3248 &d_hash_shift,
3249 &d_hash_mask,
3250 0);
3251
Dimitri Sivanich074b8512012-02-08 12:39:07 -08003252 for (loop = 0; loop < (1U << d_hash_shift); loop++)
Linus Torvaldsb07ad992011-04-23 22:32:03 -07003253 INIT_HLIST_BL_HEAD(dentry_hashtable + loop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254}
3255
Denis Cheng74bf17c2007-10-16 23:26:30 -07003256static void __init dcache_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257{
Dimitri Sivanich074b8512012-02-08 12:39:07 -08003258 unsigned int loop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259
3260 /*
3261 * A constructor could be added for stable state like the lists,
3262 * but it is probably not worth it because of the cache nature
3263 * of the dcache.
3264 */
Christoph Lameter0a31bd52007-05-06 14:49:57 -07003265 dentry_cache = KMEM_CACHE(dentry,
3266 SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|SLAB_MEM_SPREAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267
3268 /* Hash may have been set up in dcache_init_early */
3269 if (!hashdist)
3270 return;
3271
3272 dentry_hashtable =
3273 alloc_large_system_hash("Dentry cache",
Linus Torvaldsb07ad992011-04-23 22:32:03 -07003274 sizeof(struct hlist_bl_head),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 dhash_entries,
3276 13,
3277 0,
3278 &d_hash_shift,
3279 &d_hash_mask,
3280 0);
3281
Dimitri Sivanich074b8512012-02-08 12:39:07 -08003282 for (loop = 0; loop < (1U << d_hash_shift); loop++)
Linus Torvaldsb07ad992011-04-23 22:32:03 -07003283 INIT_HLIST_BL_HEAD(dentry_hashtable + loop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284}
3285
3286/* SLAB cache for __getname() consumers */
Christoph Lametere18b8902006-12-06 20:33:20 -08003287struct kmem_cache *names_cachep __read_mostly;
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07003288EXPORT_SYMBOL(names_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290EXPORT_SYMBOL(d_genocide);
3291
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292void __init vfs_caches_init_early(void)
3293{
3294 dcache_init_early();
3295 inode_init_early();
3296}
3297
3298void __init vfs_caches_init(unsigned long mempages)
3299{
3300 unsigned long reserve;
3301
3302 /* Base hash sizes on available memory, with a reserve equal to
3303 150% of current kernel size */
3304
3305 reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);
3306 mempages -= reserve;
3307
3308 names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09003309 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310
Denis Cheng74bf17c2007-10-16 23:26:30 -07003311 dcache_init();
3312 inode_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 files_init(mempages);
Denis Cheng74bf17c2007-10-16 23:26:30 -07003314 mnt_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 bdev_cache_init();
3316 chrdev_init();
3317}