blob: d93872d425d6ffb206adc410d2f3667f8b89c801 [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>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080029#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#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>
Nick Pigginceb5bdc2011-01-07 17:50:05 +110035#include <linux/bit_spinlock.h>
36#include <linux/rculist_bl.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -070037#include <linux/prefetch.h>
David Howellsdd179942011-08-16 15:31:30 +010038#include <linux/ratelimit.h>
Dave Chinnerf6041562013-08-28 10:18:00 +100039#include <linux/list_lru.h>
Andrey Ryabinindf4c0e32015-02-13 14:39:45 -080040#include <linux/kasan.h>
41
David Howells07f3f05c2006-09-30 20:52:18 +020042#include "internal.h"
Al Virob2dba1a2011-11-23 19:26:23 -050043#include "mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Nick Piggin789680d2011-01-07 17:49:30 +110045/*
46 * Usage:
Nick Piggin873feea2011-01-07 17:50:06 +110047 * dcache->d_inode->i_lock protects:
Al Viro946e51f2014-10-26 19:19:16 -040048 * - i_dentry, d_u.d_alias, d_inode of aliases
Nick Pigginceb5bdc2011-01-07 17:50:05 +110049 * dcache_hash_bucket lock protects:
50 * - the dcache hash table
NeilBrownf1ee6162017-12-21 09:45:40 +110051 * s_roots bl list spinlock protects:
52 * - the s_roots list (see __d_drop)
Dave Chinner19156842013-08-28 10:17:55 +100053 * dentry->d_sb->s_dentry_lru_lock protects:
Nick Piggin23044502011-01-07 17:49:31 +110054 * - the dcache lru lists and counters
55 * d_lock protects:
56 * - d_flags
57 * - d_name
58 * - d_lru
Nick Pigginb7ab39f2011-01-07 17:49:32 +110059 * - d_count
Nick Pigginda502952011-01-07 17:49:33 +110060 * - d_unhashed()
Nick Piggin2fd6b7f2011-01-07 17:49:34 +110061 * - d_parent and d_subdirs
62 * - childrens' d_child and d_parent
Al Viro946e51f2014-10-26 19:19:16 -040063 * - d_u.d_alias, d_inode
Nick Piggin789680d2011-01-07 17:49:30 +110064 *
65 * Ordering:
Nick Piggin873feea2011-01-07 17:50:06 +110066 * dentry->d_inode->i_lock
Nick Pigginb5c84bf2011-01-07 17:49:38 +110067 * dentry->d_lock
Dave Chinner19156842013-08-28 10:17:55 +100068 * dentry->d_sb->s_dentry_lru_lock
Nick Pigginceb5bdc2011-01-07 17:50:05 +110069 * dcache_hash_bucket lock
NeilBrownf1ee6162017-12-21 09:45:40 +110070 * s_roots lock
Nick Piggin789680d2011-01-07 17:49:30 +110071 *
Nick Pigginda502952011-01-07 17:49:33 +110072 * If there is an ancestor relationship:
73 * dentry->d_parent->...->d_parent->d_lock
74 * ...
75 * dentry->d_parent->d_lock
76 * dentry->d_lock
77 *
78 * If no ancestor relationship:
Nick Piggin789680d2011-01-07 17:49:30 +110079 * if (dentry1 < dentry2)
80 * dentry1->d_lock
81 * dentry2->d_lock
82 */
Eric Dumazetfa3536c2006-03-26 01:37:24 -080083int sysctl_vfs_cache_pressure __read_mostly = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084EXPORT_SYMBOL_GPL(sysctl_vfs_cache_pressure);
85
Al Viro74c3cbe2007-07-22 08:04:18 -040086__cacheline_aligned_in_smp DEFINE_SEQLOCK(rename_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Nick Piggin949854d2011-01-07 17:49:37 +110088EXPORT_SYMBOL(rename_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Christoph Lametere18b8902006-12-06 20:33:20 -080090static struct kmem_cache *dentry_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
David Howellscdf01222017-07-04 17:25:22 +010092const struct qstr empty_name = QSTR_INIT("", 0);
93EXPORT_SYMBOL(empty_name);
94const struct qstr slash_name = QSTR_INIT("/", 1);
95EXPORT_SYMBOL(slash_name);
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097/*
98 * This is the single most critical data structure when it comes
99 * to the dcache: the hashtable for lookups. Somebody should try
100 * to make this good - I've just made it work.
101 *
102 * This hash-function tries to avoid losing too many bits of hash
103 * information, yet avoid using a prime hash-size or similar.
104 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Eric Dumazetfa3536c2006-03-26 01:37:24 -0800106static unsigned int d_hash_shift __read_mostly;
Nick Pigginceb5bdc2011-01-07 17:50:05 +1100107
Linus Torvaldsb07ad992011-04-23 22:32:03 -0700108static struct hlist_bl_head *dentry_hashtable __read_mostly;
Nick Pigginceb5bdc2011-01-07 17:50:05 +1100109
Linus Torvalds8387ff22016-06-10 07:51:30 -0700110static inline struct hlist_bl_head *d_hash(unsigned int hash)
Nick Pigginceb5bdc2011-01-07 17:50:05 +1100111{
Alexey Dobriyan854d3e62017-11-20 18:05:07 +0300112 return dentry_hashtable + (hash >> d_hash_shift);
Nick Pigginceb5bdc2011-01-07 17:50:05 +1100113}
114
Al Viro94bdd652016-04-15 02:42:04 -0400115#define IN_LOOKUP_SHIFT 10
116static struct hlist_bl_head in_lookup_hashtable[1 << IN_LOOKUP_SHIFT];
117
118static inline struct hlist_bl_head *in_lookup_hash(const struct dentry *parent,
119 unsigned int hash)
120{
121 hash += (unsigned long) parent / L1_CACHE_BYTES;
122 return in_lookup_hashtable + hash_32(hash, IN_LOOKUP_SHIFT);
123}
124
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126/* Statistics gathering. */
127struct dentry_stat_t dentry_stat = {
128 .age_limit = 45,
129};
130
Glauber Costa3942c072013-08-28 10:17:53 +1000131static DEFINE_PER_CPU(long, nr_dentry);
Dave Chinner62d36c72013-08-28 10:17:54 +1000132static DEFINE_PER_CPU(long, nr_dentry_unused);
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400133
134#if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS)
Dave Chinner62d36c72013-08-28 10:17:54 +1000135
136/*
137 * Here we resort to our own counters instead of using generic per-cpu counters
138 * for consistency with what the vfs inode code does. We are expected to harvest
139 * better code and performance by having our own specialized counters.
140 *
141 * Please note that the loop is done over all possible CPUs, not over all online
142 * CPUs. The reason for this is that we don't want to play games with CPUs going
143 * on and off. If one of them goes off, we will just keep their counters.
144 *
145 * glommer: See cffbc8a for details, and if you ever intend to change this,
146 * please update all vfs counters to match.
147 */
Glauber Costa3942c072013-08-28 10:17:53 +1000148static long get_nr_dentry(void)
Nick Piggin3e880fb2011-01-07 17:49:19 +1100149{
150 int i;
Glauber Costa3942c072013-08-28 10:17:53 +1000151 long sum = 0;
Nick Piggin3e880fb2011-01-07 17:49:19 +1100152 for_each_possible_cpu(i)
153 sum += per_cpu(nr_dentry, i);
154 return sum < 0 ? 0 : sum;
155}
156
Dave Chinner62d36c72013-08-28 10:17:54 +1000157static long get_nr_dentry_unused(void)
158{
159 int i;
160 long sum = 0;
161 for_each_possible_cpu(i)
162 sum += per_cpu(nr_dentry_unused, i);
163 return sum < 0 ? 0 : sum;
164}
165
Joe Perches1f7e0612014-06-06 14:38:05 -0700166int proc_nr_dentry(struct ctl_table *table, int write, void __user *buffer,
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400167 size_t *lenp, loff_t *ppos)
168{
Nick Piggin3e880fb2011-01-07 17:49:19 +1100169 dentry_stat.nr_dentry = get_nr_dentry();
Dave Chinner62d36c72013-08-28 10:17:54 +1000170 dentry_stat.nr_unused = get_nr_dentry_unused();
Glauber Costa3942c072013-08-28 10:17:53 +1000171 return proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400172}
173#endif
174
Linus Torvalds5483f182012-03-04 15:51:42 -0800175/*
176 * Compare 2 name strings, return 0 if they match, otherwise non-zero.
177 * The strings are both count bytes long, and count is non-zero.
178 */
Linus Torvaldse419b4c2012-05-03 10:16:43 -0700179#ifdef CONFIG_DCACHE_WORD_ACCESS
180
181#include <asm/word-at-a-time.h>
182/*
183 * NOTE! 'cs' and 'scount' come from a dentry, so it has a
184 * aligned allocation for this particular component. We don't
185 * strictly need the load_unaligned_zeropad() safety, but it
186 * doesn't hurt either.
187 *
188 * In contrast, 'ct' and 'tcount' can be from a pathname, and do
189 * need the careful unaligned handling.
190 */
Linus Torvalds94753db52012-05-10 12:19:19 -0700191static inline int dentry_string_cmp(const unsigned char *cs, const unsigned char *ct, unsigned tcount)
Linus Torvalds5483f182012-03-04 15:51:42 -0800192{
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -0800193 unsigned long a,b,mask;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -0800194
195 for (;;) {
Linus Torvalds12f8ad42012-05-04 14:59:14 -0700196 a = *(unsigned long *)cs;
Linus Torvaldse419b4c2012-05-03 10:16:43 -0700197 b = load_unaligned_zeropad(ct);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -0800198 if (tcount < sizeof(unsigned long))
199 break;
200 if (unlikely(a != b))
201 return 1;
202 cs += sizeof(unsigned long);
203 ct += sizeof(unsigned long);
204 tcount -= sizeof(unsigned long);
205 if (!tcount)
206 return 0;
207 }
Will Deacona5c21dc2013-12-12 17:40:21 +0000208 mask = bytemask_from_count(tcount);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -0800209 return unlikely(!!((a ^ b) & mask));
Linus Torvaldse419b4c2012-05-03 10:16:43 -0700210}
211
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -0800212#else
Linus Torvaldse419b4c2012-05-03 10:16:43 -0700213
Linus Torvalds94753db52012-05-10 12:19:19 -0700214static inline int dentry_string_cmp(const unsigned char *cs, const unsigned char *ct, unsigned tcount)
Linus Torvaldse419b4c2012-05-03 10:16:43 -0700215{
Linus Torvalds5483f182012-03-04 15:51:42 -0800216 do {
217 if (*cs != *ct)
218 return 1;
219 cs++;
220 ct++;
221 tcount--;
222 } while (tcount);
223 return 0;
224}
225
Linus Torvaldse419b4c2012-05-03 10:16:43 -0700226#endif
227
Linus Torvalds94753db52012-05-10 12:19:19 -0700228static inline int dentry_cmp(const struct dentry *dentry, const unsigned char *ct, unsigned tcount)
229{
Linus Torvalds94753db52012-05-10 12:19:19 -0700230 /*
231 * Be careful about RCU walk racing with rename:
Will Deacon506458e2017-10-24 11:22:48 +0100232 * use 'READ_ONCE' to fetch the name pointer.
Linus Torvalds94753db52012-05-10 12:19:19 -0700233 *
234 * NOTE! Even if a rename will mean that the length
235 * was not loaded atomically, we don't care. The
236 * RCU walk will check the sequence count eventually,
237 * and catch it. And we won't overrun the buffer,
238 * because we're reading the name pointer atomically,
239 * and a dentry name is guaranteed to be properly
240 * terminated with a NUL byte.
241 *
242 * End result: even if 'len' is wrong, we'll exit
243 * early because the data cannot match (there can
244 * be no NUL in the ct/tcount data)
245 */
Will Deacon506458e2017-10-24 11:22:48 +0100246 const unsigned char *cs = READ_ONCE(dentry->d_name.name);
He Kuangae0a8432016-03-26 09:12:10 +0000247
Linus Torvalds6326c712012-05-21 16:14:04 -0700248 return dentry_string_cmp(cs, ct, tcount);
Linus Torvalds94753db52012-05-10 12:19:19 -0700249}
250
Al Viro8d85b482014-09-29 14:54:27 -0400251struct external_name {
252 union {
253 atomic_t count;
254 struct rcu_head head;
255 } u;
256 unsigned char name[];
257};
258
259static inline struct external_name *external_name(struct dentry *dentry)
260{
261 return container_of(dentry->d_name.name, struct external_name, name[0]);
262}
263
Christoph Hellwig9c82ab92010-10-10 05:36:22 -0400264static void __d_free(struct rcu_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Christoph Hellwig9c82ab92010-10-10 05:36:22 -0400266 struct dentry *dentry = container_of(head, struct dentry, d_u.d_rcu);
267
Al Viro8d85b482014-09-29 14:54:27 -0400268 kmem_cache_free(dentry_cache, dentry);
269}
270
271static void __d_free_external(struct rcu_head *head)
272{
273 struct dentry *dentry = container_of(head, struct dentry, d_u.d_rcu);
Al Viro8d85b482014-09-29 14:54:27 -0400274 kfree(external_name(dentry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 kmem_cache_free(dentry_cache, dentry);
276}
277
Al Viro810bb172014-10-12 12:45:37 -0400278static inline int dname_external(const struct dentry *dentry)
279{
280 return dentry->d_name.name != dentry->d_iname;
281}
282
Al Viro49d31c22017-07-07 14:51:19 -0400283void take_dentry_name_snapshot(struct name_snapshot *name, struct dentry *dentry)
284{
285 spin_lock(&dentry->d_lock);
286 if (unlikely(dname_external(dentry))) {
287 struct external_name *p = external_name(dentry);
288 atomic_inc(&p->u.count);
289 spin_unlock(&dentry->d_lock);
290 name->name = p->name;
291 } else {
292 memcpy(name->inline_name, dentry->d_iname, DNAME_INLINE_LEN);
293 spin_unlock(&dentry->d_lock);
294 name->name = name->inline_name;
295 }
296}
297EXPORT_SYMBOL(take_dentry_name_snapshot);
298
299void release_dentry_name_snapshot(struct name_snapshot *name)
300{
301 if (unlikely(name->name != name->inline_name)) {
302 struct external_name *p;
303 p = container_of(name->name, struct external_name, name[0]);
304 if (unlikely(atomic_dec_and_test(&p->u.count)))
305 kfree_rcu(p, u.head);
306 }
307}
308EXPORT_SYMBOL(release_dentry_name_snapshot);
309
David Howells4bf46a22015-03-05 14:09:22 +0000310static inline void __d_set_inode_and_type(struct dentry *dentry,
311 struct inode *inode,
312 unsigned type_flags)
313{
314 unsigned flags;
315
316 dentry->d_inode = inode;
David Howells4bf46a22015-03-05 14:09:22 +0000317 flags = READ_ONCE(dentry->d_flags);
318 flags &= ~(DCACHE_ENTRY_TYPE | DCACHE_FALLTHRU);
319 flags |= type_flags;
320 WRITE_ONCE(dentry->d_flags, flags);
321}
322
David Howells4bf46a22015-03-05 14:09:22 +0000323static inline void __d_clear_type_and_inode(struct dentry *dentry)
324{
325 unsigned flags = READ_ONCE(dentry->d_flags);
326
327 flags &= ~(DCACHE_ENTRY_TYPE | DCACHE_FALLTHRU);
328 WRITE_ONCE(dentry->d_flags, flags);
David Howells4bf46a22015-03-05 14:09:22 +0000329 dentry->d_inode = NULL;
330}
331
Al Virob4f03542014-04-29 23:40:14 -0400332static void dentry_free(struct dentry *dentry)
333{
Al Viro946e51f2014-10-26 19:19:16 -0400334 WARN_ON(!hlist_unhashed(&dentry->d_u.d_alias));
Al Viro8d85b482014-09-29 14:54:27 -0400335 if (unlikely(dname_external(dentry))) {
336 struct external_name *p = external_name(dentry);
337 if (likely(atomic_dec_and_test(&p->u.count))) {
338 call_rcu(&dentry->d_u.d_rcu, __d_free_external);
339 return;
340 }
341 }
Al Virob4f03542014-04-29 23:40:14 -0400342 /* if dentry was never visible to RCU, immediate free is OK */
343 if (!(dentry->d_flags & DCACHE_RCUACCESS))
344 __d_free(&dentry->d_u.d_rcu);
345 else
346 call_rcu(&dentry->d_u.d_rcu, __d_free);
347}
348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349/*
350 * Release the dentry's inode, using the filesystem
Al Viro550dce02016-05-29 20:13:30 -0400351 * d_iput() operation if defined.
Nick Piggin31e6b012011-01-07 17:49:52 +1100352 */
353static void dentry_unlink_inode(struct dentry * dentry)
354 __releases(dentry->d_lock)
Nick Piggin873feea2011-01-07 17:50:06 +1100355 __releases(dentry->d_inode->i_lock)
Nick Piggin31e6b012011-01-07 17:49:52 +1100356{
357 struct inode *inode = dentry->d_inode;
Al Viro550dce02016-05-29 20:13:30 -0400358 bool hashed = !d_unhashed(dentry);
Al Viroa528aca2016-02-29 12:12:46 -0500359
Al Viro550dce02016-05-29 20:13:30 -0400360 if (hashed)
361 raw_write_seqcount_begin(&dentry->d_seq);
David Howells4bf46a22015-03-05 14:09:22 +0000362 __d_clear_type_and_inode(dentry);
Al Viro946e51f2014-10-26 19:19:16 -0400363 hlist_del_init(&dentry->d_u.d_alias);
Al Viro550dce02016-05-29 20:13:30 -0400364 if (hashed)
365 raw_write_seqcount_end(&dentry->d_seq);
Nick Piggin31e6b012011-01-07 17:49:52 +1100366 spin_unlock(&dentry->d_lock);
Nick Piggin873feea2011-01-07 17:50:06 +1100367 spin_unlock(&inode->i_lock);
Nick Piggin31e6b012011-01-07 17:49:52 +1100368 if (!inode->i_nlink)
369 fsnotify_inoderemove(inode);
370 if (dentry->d_op && dentry->d_op->d_iput)
371 dentry->d_op->d_iput(dentry, inode);
372 else
373 iput(inode);
374}
375
376/*
Linus Torvalds89dc77b2013-09-13 22:55:10 -0400377 * The DCACHE_LRU_LIST bit is set whenever the 'd_lru' entry
378 * is in use - which includes both the "real" per-superblock
379 * LRU list _and_ the DCACHE_SHRINK_LIST use.
380 *
381 * The DCACHE_SHRINK_LIST bit is set whenever the dentry is
382 * on the shrink list (ie not on the superblock LRU list).
383 *
384 * The per-cpu "nr_dentry_unused" counters are updated with
385 * the DCACHE_LRU_LIST bit.
386 *
387 * These helper functions make sure we always follow the
388 * rules. d_lock must be held by the caller.
389 */
390#define D_FLAG_VERIFY(dentry,x) WARN_ON_ONCE(((dentry)->d_flags & (DCACHE_LRU_LIST | DCACHE_SHRINK_LIST)) != (x))
391static void d_lru_add(struct dentry *dentry)
392{
393 D_FLAG_VERIFY(dentry, 0);
394 dentry->d_flags |= DCACHE_LRU_LIST;
395 this_cpu_inc(nr_dentry_unused);
396 WARN_ON_ONCE(!list_lru_add(&dentry->d_sb->s_dentry_lru, &dentry->d_lru));
397}
398
399static void d_lru_del(struct dentry *dentry)
400{
401 D_FLAG_VERIFY(dentry, DCACHE_LRU_LIST);
402 dentry->d_flags &= ~DCACHE_LRU_LIST;
403 this_cpu_dec(nr_dentry_unused);
404 WARN_ON_ONCE(!list_lru_del(&dentry->d_sb->s_dentry_lru, &dentry->d_lru));
405}
406
407static void d_shrink_del(struct dentry *dentry)
408{
409 D_FLAG_VERIFY(dentry, DCACHE_SHRINK_LIST | DCACHE_LRU_LIST);
410 list_del_init(&dentry->d_lru);
411 dentry->d_flags &= ~(DCACHE_SHRINK_LIST | DCACHE_LRU_LIST);
412 this_cpu_dec(nr_dentry_unused);
413}
414
415static void d_shrink_add(struct dentry *dentry, struct list_head *list)
416{
417 D_FLAG_VERIFY(dentry, 0);
418 list_add(&dentry->d_lru, list);
419 dentry->d_flags |= DCACHE_SHRINK_LIST | DCACHE_LRU_LIST;
420 this_cpu_inc(nr_dentry_unused);
421}
422
423/*
424 * These can only be called under the global LRU lock, ie during the
425 * callback for freeing the LRU list. "isolate" removes it from the
426 * LRU lists entirely, while shrink_move moves it to the indicated
427 * private list.
428 */
Vladimir Davydov3f97b162015-02-12 14:59:35 -0800429static void d_lru_isolate(struct list_lru_one *lru, struct dentry *dentry)
Linus Torvalds89dc77b2013-09-13 22:55:10 -0400430{
431 D_FLAG_VERIFY(dentry, DCACHE_LRU_LIST);
432 dentry->d_flags &= ~DCACHE_LRU_LIST;
433 this_cpu_dec(nr_dentry_unused);
Vladimir Davydov3f97b162015-02-12 14:59:35 -0800434 list_lru_isolate(lru, &dentry->d_lru);
Linus Torvalds89dc77b2013-09-13 22:55:10 -0400435}
436
Vladimir Davydov3f97b162015-02-12 14:59:35 -0800437static void d_lru_shrink_move(struct list_lru_one *lru, struct dentry *dentry,
438 struct list_head *list)
Linus Torvalds89dc77b2013-09-13 22:55:10 -0400439{
440 D_FLAG_VERIFY(dentry, DCACHE_LRU_LIST);
441 dentry->d_flags |= DCACHE_SHRINK_LIST;
Vladimir Davydov3f97b162015-02-12 14:59:35 -0800442 list_lru_isolate_move(lru, &dentry->d_lru, list);
Linus Torvalds89dc77b2013-09-13 22:55:10 -0400443}
444
445/*
Dave Chinnerf6041562013-08-28 10:18:00 +1000446 * dentry_lru_(add|del)_list) must be called with d_lock held.
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700447 */
448static void dentry_lru_add(struct dentry *dentry)
449{
Linus Torvalds89dc77b2013-09-13 22:55:10 -0400450 if (unlikely(!(dentry->d_flags & DCACHE_LRU_LIST)))
451 d_lru_add(dentry);
Josef Bacik563f4002017-04-18 16:04:17 -0400452 else if (unlikely(!(dentry->d_flags & DCACHE_REFERENCED)))
453 dentry->d_flags |= DCACHE_REFERENCED;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700454}
455
Miklos Szeredid52b9082007-05-08 00:23:46 -0700456/**
Nick Piggin789680d2011-01-07 17:49:30 +1100457 * d_drop - drop a dentry
458 * @dentry: dentry to drop
459 *
460 * d_drop() unhashes the entry from the parent dentry hashes, so that it won't
461 * be found through a VFS lookup any more. Note that this is different from
462 * deleting the dentry - d_delete will try to mark the dentry negative if
463 * possible, giving a successful _negative_ lookup, while d_drop will
464 * just make the cache lookup fail.
465 *
466 * d_drop() is used mainly for stuff that wants to invalidate a dentry for some
467 * reason (NFS timeouts or autofs deletes).
468 *
NeilBrown61647822017-11-10 15:45:41 +1100469 * __d_drop requires dentry->d_lock
470 * ___d_drop doesn't mark dentry as "unhashed"
471 * (dentry->d_hash.pprev will be LIST_POISON2, not NULL).
Nick Piggin789680d2011-01-07 17:49:30 +1100472 */
NeilBrown61647822017-11-10 15:45:41 +1100473static void ___d_drop(struct dentry *dentry)
Nick Piggin789680d2011-01-07 17:49:30 +1100474{
Linus Torvaldsdea36672011-04-24 07:58:46 -0700475 if (!d_unhashed(dentry)) {
Al Virob61625d2013-10-04 11:09:01 -0400476 struct hlist_bl_head *b;
J. Bruce Fields7632e462012-06-28 12:10:55 -0400477 /*
478 * Hashed dentries are normally on the dentry hashtable,
479 * with the exception of those newly allocated by
NeilBrownf1ee6162017-12-21 09:45:40 +1100480 * d_obtain_root, which are always IS_ROOT:
J. Bruce Fields7632e462012-06-28 12:10:55 -0400481 */
482 if (unlikely(IS_ROOT(dentry)))
NeilBrownf1ee6162017-12-21 09:45:40 +1100483 b = &dentry->d_sb->s_roots;
Al Virob61625d2013-10-04 11:09:01 -0400484 else
Linus Torvalds8387ff22016-06-10 07:51:30 -0700485 b = d_hash(dentry->d_name.hash);
Al Virob61625d2013-10-04 11:09:01 -0400486
487 hlist_bl_lock(b);
488 __hlist_bl_del(&dentry->d_hash);
Al Virob61625d2013-10-04 11:09:01 -0400489 hlist_bl_unlock(b);
Al Virod6141462016-07-28 13:05:50 -0400490 /* After this call, in-progress rcu-walk path lookup will fail. */
491 write_seqcount_invalidate(&dentry->d_seq);
Nick Piggin789680d2011-01-07 17:49:30 +1100492 }
493}
NeilBrown61647822017-11-10 15:45:41 +1100494
495void __d_drop(struct dentry *dentry)
496{
497 ___d_drop(dentry);
498 dentry->d_hash.pprev = NULL;
499}
Nick Piggin789680d2011-01-07 17:49:30 +1100500EXPORT_SYMBOL(__d_drop);
501
502void d_drop(struct dentry *dentry)
503{
Nick Piggin789680d2011-01-07 17:49:30 +1100504 spin_lock(&dentry->d_lock);
505 __d_drop(dentry);
506 spin_unlock(&dentry->d_lock);
Nick Piggin789680d2011-01-07 17:49:30 +1100507}
508EXPORT_SYMBOL(d_drop);
509
Al Viroba65dc52016-06-10 11:32:47 -0400510static inline void dentry_unlist(struct dentry *dentry, struct dentry *parent)
511{
512 struct dentry *next;
513 /*
514 * Inform d_walk() and shrink_dentry_list() that we are no longer
515 * attached to the dentry tree
516 */
517 dentry->d_flags |= DCACHE_DENTRY_KILLED;
518 if (unlikely(list_empty(&dentry->d_child)))
519 return;
520 __list_del_entry(&dentry->d_child);
521 /*
522 * Cursors can move around the list of children. While we'd been
523 * a normal list member, it didn't matter - ->d_child.next would've
524 * been updated. However, from now on it won't be and for the
525 * things like d_walk() it might end up with a nasty surprise.
526 * Normally d_walk() doesn't care about cursors moving around -
527 * ->d_lock on parent prevents that and since a cursor has no children
528 * of its own, we get through it without ever unlocking the parent.
529 * There is one exception, though - if we ascend from a child that
530 * gets killed as soon as we unlock it, the next sibling is found
531 * using the value left in its ->d_child.next. And if _that_
532 * pointed to a cursor, and cursor got moved (e.g. by lseek())
533 * before d_walk() regains parent->d_lock, we'll end up skipping
534 * everything the cursor had been moved past.
535 *
536 * Solution: make sure that the pointer left behind in ->d_child.next
537 * points to something that won't be moving around. I.e. skip the
538 * cursors.
539 */
540 while (dentry->d_child.next != &parent->d_subdirs) {
541 next = list_entry(dentry->d_child.next, struct dentry, d_child);
542 if (likely(!(next->d_flags & DCACHE_DENTRY_CURSOR)))
543 break;
544 dentry->d_child.next = next->d_child.next;
545 }
546}
547
Al Viroe55fd012014-05-28 13:51:12 -0400548static void __dentry_kill(struct dentry *dentry)
Nick Piggin77812a12011-01-07 17:49:48 +1100549{
Al Viro41edf272014-05-01 10:30:00 -0400550 struct dentry *parent = NULL;
551 bool can_free = true;
Al Viro41edf272014-05-01 10:30:00 -0400552 if (!IS_ROOT(dentry))
Nick Piggin77812a12011-01-07 17:49:48 +1100553 parent = dentry->d_parent;
Nick Piggin31e6b012011-01-07 17:49:52 +1100554
Linus Torvalds0d984392013-09-08 13:46:52 -0700555 /*
556 * The dentry is now unrecoverably dead to the world.
557 */
558 lockref_mark_dead(&dentry->d_lockref);
559
Sage Weilf0023bc2011-10-28 10:02:42 -0700560 /*
Sage Weilf0023bc2011-10-28 10:02:42 -0700561 * inform the fs via d_prune that this dentry is about to be
562 * unhashed and destroyed.
563 */
Al Viro29266202014-05-30 11:39:02 -0400564 if (dentry->d_flags & DCACHE_OP_PRUNE)
Yan, Zheng61572bb2013-04-15 14:13:21 +0800565 dentry->d_op->d_prune(dentry);
566
Al Viro01b60352014-04-29 23:42:52 -0400567 if (dentry->d_flags & DCACHE_LRU_LIST) {
568 if (!(dentry->d_flags & DCACHE_SHRINK_LIST))
569 d_lru_del(dentry);
Al Viro01b60352014-04-29 23:42:52 -0400570 }
Nick Piggin77812a12011-01-07 17:49:48 +1100571 /* if it was on the hash then remove it */
572 __d_drop(dentry);
Al Viroba65dc52016-06-10 11:32:47 -0400573 dentry_unlist(dentry, parent);
Al Viro03b3b882014-04-29 15:45:28 -0400574 if (parent)
575 spin_unlock(&parent->d_lock);
Al Viro550dce02016-05-29 20:13:30 -0400576 if (dentry->d_inode)
577 dentry_unlink_inode(dentry);
578 else
579 spin_unlock(&dentry->d_lock);
Al Viro03b3b882014-04-29 15:45:28 -0400580 this_cpu_dec(nr_dentry);
581 if (dentry->d_op && dentry->d_op->d_release)
582 dentry->d_op->d_release(dentry);
583
Al Viro41edf272014-05-01 10:30:00 -0400584 spin_lock(&dentry->d_lock);
585 if (dentry->d_flags & DCACHE_SHRINK_LIST) {
586 dentry->d_flags |= DCACHE_MAY_FREE;
587 can_free = false;
588 }
589 spin_unlock(&dentry->d_lock);
Al Viro41edf272014-05-01 10:30:00 -0400590 if (likely(can_free))
591 dentry_free(dentry);
Al Viroe55fd012014-05-28 13:51:12 -0400592}
593
594/*
595 * Finish off a dentry we've decided to kill.
596 * dentry->d_lock must be held, returns with it unlocked.
597 * If ref is non-zero, then decrement the refcount too.
598 * Returns dentry requiring refcount drop, or NULL if we're done.
599 */
Al Viro8cbf74d2014-05-29 09:18:26 -0400600static struct dentry *dentry_kill(struct dentry *dentry)
Al Viroe55fd012014-05-28 13:51:12 -0400601 __releases(dentry->d_lock)
602{
603 struct inode *inode = dentry->d_inode;
604 struct dentry *parent = NULL;
605
606 if (inode && unlikely(!spin_trylock(&inode->i_lock)))
607 goto failed;
608
609 if (!IS_ROOT(dentry)) {
610 parent = dentry->d_parent;
611 if (unlikely(!spin_trylock(&parent->d_lock))) {
612 if (inode)
613 spin_unlock(&inode->i_lock);
614 goto failed;
615 }
616 }
617
618 __dentry_kill(dentry);
Al Viro03b3b882014-04-29 15:45:28 -0400619 return parent;
Al Viroe55fd012014-05-28 13:51:12 -0400620
621failed:
Al Viro8cbf74d2014-05-29 09:18:26 -0400622 spin_unlock(&dentry->d_lock);
Al Viroe55fd012014-05-28 13:51:12 -0400623 return dentry; /* try again with same dentry */
Nick Piggin77812a12011-01-07 17:49:48 +1100624}
625
Al Viro046b9612014-05-29 08:54:52 -0400626static inline struct dentry *lock_parent(struct dentry *dentry)
627{
628 struct dentry *parent = dentry->d_parent;
629 if (IS_ROOT(dentry))
630 return NULL;
Linus Torvalds360f5472015-01-09 15:19:03 -0800631 if (unlikely(dentry->d_lockref.count < 0))
Al Viroc2338f22014-06-12 00:29:13 -0400632 return NULL;
Al Viro046b9612014-05-29 08:54:52 -0400633 if (likely(spin_trylock(&parent->d_lock)))
634 return parent;
Al Viro046b9612014-05-29 08:54:52 -0400635 rcu_read_lock();
Al Viroc2338f22014-06-12 00:29:13 -0400636 spin_unlock(&dentry->d_lock);
Al Viro046b9612014-05-29 08:54:52 -0400637again:
Mark Rutland66702eb2017-10-23 14:07:14 -0700638 parent = READ_ONCE(dentry->d_parent);
Al Viro046b9612014-05-29 08:54:52 -0400639 spin_lock(&parent->d_lock);
640 /*
641 * We can't blindly lock dentry until we are sure
642 * that we won't violate the locking order.
643 * Any changes of dentry->d_parent must have
644 * been done with parent->d_lock held, so
645 * spin_lock() above is enough of a barrier
646 * for checking if it's still our child.
647 */
648 if (unlikely(parent != dentry->d_parent)) {
649 spin_unlock(&parent->d_lock);
650 goto again;
651 }
652 rcu_read_unlock();
653 if (parent != dentry)
Linus Torvalds9f126002014-05-31 09:13:21 -0700654 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Al Viro046b9612014-05-29 08:54:52 -0400655 else
656 parent = NULL;
657 return parent;
658}
659
Linus Torvalds360f5472015-01-09 15:19:03 -0800660/*
661 * Try to do a lockless dput(), and return whether that was successful.
662 *
663 * If unsuccessful, we return false, having already taken the dentry lock.
664 *
665 * The caller needs to hold the RCU read lock, so that the dentry is
666 * guaranteed to stay around even if the refcount goes down to zero!
667 */
668static inline bool fast_dput(struct dentry *dentry)
669{
670 int ret;
671 unsigned int d_flags;
672
673 /*
674 * If we have a d_op->d_delete() operation, we sould not
Al Viro75a6f822015-07-08 02:42:38 +0100675 * let the dentry count go to zero, so use "put_or_lock".
Linus Torvalds360f5472015-01-09 15:19:03 -0800676 */
677 if (unlikely(dentry->d_flags & DCACHE_OP_DELETE))
678 return lockref_put_or_lock(&dentry->d_lockref);
679
680 /*
681 * .. otherwise, we can try to just decrement the
682 * lockref optimistically.
683 */
684 ret = lockref_put_return(&dentry->d_lockref);
685
686 /*
687 * If the lockref_put_return() failed due to the lock being held
688 * by somebody else, the fast path has failed. We will need to
689 * get the lock, and then check the count again.
690 */
691 if (unlikely(ret < 0)) {
692 spin_lock(&dentry->d_lock);
693 if (dentry->d_lockref.count > 1) {
694 dentry->d_lockref.count--;
695 spin_unlock(&dentry->d_lock);
696 return 1;
697 }
698 return 0;
699 }
700
701 /*
702 * If we weren't the last ref, we're done.
703 */
704 if (ret)
705 return 1;
706
707 /*
708 * Careful, careful. The reference count went down
709 * to zero, but we don't hold the dentry lock, so
710 * somebody else could get it again, and do another
711 * dput(), and we need to not race with that.
712 *
713 * However, there is a very special and common case
714 * where we don't care, because there is nothing to
715 * do: the dentry is still hashed, it does not have
716 * a 'delete' op, and it's referenced and already on
717 * the LRU list.
718 *
719 * NOTE! Since we aren't locked, these values are
720 * not "stable". However, it is sufficient that at
721 * some point after we dropped the reference the
722 * dentry was hashed and the flags had the proper
723 * value. Other dentry users may have re-gotten
724 * a reference to the dentry and change that, but
725 * our work is done - we can leave the dentry
726 * around with a zero refcount.
727 */
728 smp_rmb();
Mark Rutland66702eb2017-10-23 14:07:14 -0700729 d_flags = READ_ONCE(dentry->d_flags);
Al Viro75a6f822015-07-08 02:42:38 +0100730 d_flags &= DCACHE_REFERENCED | DCACHE_LRU_LIST | DCACHE_DISCONNECTED;
Linus Torvalds360f5472015-01-09 15:19:03 -0800731
732 /* Nothing to do? Dropping the reference was all we needed? */
733 if (d_flags == (DCACHE_REFERENCED | DCACHE_LRU_LIST) && !d_unhashed(dentry))
734 return 1;
735
736 /*
737 * Not the fast normal case? Get the lock. We've already decremented
738 * the refcount, but we'll need to re-check the situation after
739 * getting the lock.
740 */
741 spin_lock(&dentry->d_lock);
742
743 /*
744 * Did somebody else grab a reference to it in the meantime, and
745 * we're no longer the last user after all? Alternatively, somebody
746 * else could have killed it and marked it dead. Either way, we
747 * don't need to do anything else.
748 */
749 if (dentry->d_lockref.count) {
750 spin_unlock(&dentry->d_lock);
751 return 1;
752 }
753
754 /*
755 * Re-get the reference we optimistically dropped. We hold the
756 * lock, and we just tested that it was zero, so we can just
757 * set it to 1.
758 */
759 dentry->d_lockref.count = 1;
760 return 0;
761}
762
763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764/*
765 * This is dput
766 *
767 * This is complicated by the fact that we do not want to put
768 * dentries that are no longer on any hash chain on the unused
769 * list: we'd much rather just get rid of them immediately.
770 *
771 * However, that implies that we have to traverse the dentry
772 * tree upwards to the parents which might _also_ now be
773 * scheduled for deletion (it may have been only waiting for
774 * its last child to go away).
775 *
776 * This tail recursion is done by hand as we don't want to depend
777 * on the compiler to always get this right (gcc generally doesn't).
778 * Real recursion would eat up our stack space.
779 */
780
781/*
782 * dput - release a dentry
783 * @dentry: dentry to release
784 *
785 * Release a dentry. This will drop the usage count and if appropriate
786 * call the dentry unlink method as well as removing it from the queues and
787 * releasing its resources. If the parent dentries were scheduled for release
788 * they too may now get deleted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790void dput(struct dentry *dentry)
791{
Linus Torvalds8aab6a22013-09-08 13:26:18 -0700792 if (unlikely(!dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 return;
794
795repeat:
Wei Fang47be6182016-07-06 11:32:20 +0800796 might_sleep();
797
Linus Torvalds360f5472015-01-09 15:19:03 -0800798 rcu_read_lock();
799 if (likely(fast_dput(dentry))) {
800 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 return;
Linus Torvalds360f5472015-01-09 15:19:03 -0800802 }
803
804 /* Slow case: now with the dentry lock held */
805 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Al Viro85c7f812016-04-14 19:52:13 -0400807 WARN_ON(d_in_lookup(dentry));
808
Linus Torvalds8aab6a22013-09-08 13:26:18 -0700809 /* Unreachable? Get rid of it */
810 if (unlikely(d_unhashed(dentry)))
811 goto kill_it;
812
Al Viro75a6f822015-07-08 02:42:38 +0100813 if (unlikely(dentry->d_flags & DCACHE_DISCONNECTED))
814 goto kill_it;
815
Linus Torvalds8aab6a22013-09-08 13:26:18 -0700816 if (unlikely(dentry->d_flags & DCACHE_OP_DELETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 if (dentry->d_op->d_delete(dentry))
Nick Piggin61f3dee2011-01-07 17:49:40 +1100818 goto kill_it;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 }
Nick Piggin265ac902010-10-10 05:36:24 -0400820
Christoph Hellwiga4633352010-10-10 05:36:26 -0400821 dentry_lru_add(dentry);
Nick Piggin265ac902010-10-10 05:36:24 -0400822
Waiman Long98474232013-08-28 18:24:59 -0700823 dentry->d_lockref.count--;
Nick Piggin61f3dee2011-01-07 17:49:40 +1100824 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 return;
826
Miklos Szeredid52b9082007-05-08 00:23:46 -0700827kill_it:
Al Viro8cbf74d2014-05-29 09:18:26 -0400828 dentry = dentry_kill(dentry);
Wei Fang47be6182016-07-06 11:32:20 +0800829 if (dentry) {
830 cond_resched();
Miklos Szeredid52b9082007-05-08 00:23:46 -0700831 goto repeat;
Wei Fang47be6182016-07-06 11:32:20 +0800832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700834EXPORT_SYMBOL(dput);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100837/* This must be called with d_lock held */
Nick Piggindc0474b2011-01-07 17:49:43 +1100838static inline void __dget_dlock(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
Waiman Long98474232013-08-28 18:24:59 -0700840 dentry->d_lockref.count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841}
842
Nick Piggindc0474b2011-01-07 17:49:43 +1100843static inline void __dget(struct dentry *dentry)
Nick Piggin23044502011-01-07 17:49:31 +1100844{
Waiman Long98474232013-08-28 18:24:59 -0700845 lockref_get(&dentry->d_lockref);
Nick Piggin23044502011-01-07 17:49:31 +1100846}
847
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100848struct dentry *dget_parent(struct dentry *dentry)
849{
Waiman Longdf3d0bb2013-09-02 11:29:22 -0700850 int gotref;
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100851 struct dentry *ret;
852
Waiman Longdf3d0bb2013-09-02 11:29:22 -0700853 /*
854 * Do optimistic parent lookup without any
855 * locking.
856 */
857 rcu_read_lock();
Mark Rutland66702eb2017-10-23 14:07:14 -0700858 ret = READ_ONCE(dentry->d_parent);
Waiman Longdf3d0bb2013-09-02 11:29:22 -0700859 gotref = lockref_get_not_zero(&ret->d_lockref);
860 rcu_read_unlock();
861 if (likely(gotref)) {
Mark Rutland66702eb2017-10-23 14:07:14 -0700862 if (likely(ret == READ_ONCE(dentry->d_parent)))
Waiman Longdf3d0bb2013-09-02 11:29:22 -0700863 return ret;
864 dput(ret);
865 }
866
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100867repeat:
Nick Piggina734eb42011-01-07 17:49:44 +1100868 /*
869 * Don't need rcu_dereference because we re-check it was correct under
870 * the lock.
871 */
872 rcu_read_lock();
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100873 ret = dentry->d_parent;
Nick Piggina734eb42011-01-07 17:49:44 +1100874 spin_lock(&ret->d_lock);
875 if (unlikely(ret != dentry->d_parent)) {
876 spin_unlock(&ret->d_lock);
877 rcu_read_unlock();
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100878 goto repeat;
879 }
Nick Piggina734eb42011-01-07 17:49:44 +1100880 rcu_read_unlock();
Waiman Long98474232013-08-28 18:24:59 -0700881 BUG_ON(!ret->d_lockref.count);
882 ret->d_lockref.count++;
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100883 spin_unlock(&ret->d_lock);
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100884 return ret;
885}
886EXPORT_SYMBOL(dget_parent);
887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888/**
889 * d_find_alias - grab a hashed alias of inode
890 * @inode: inode in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 *
892 * If inode has a hashed alias, or is a directory and has any alias,
893 * acquire the reference to alias and return it. Otherwise return NULL.
894 * Notice that if inode is a directory there can be only one alias and
895 * it can be unhashed only if it has no children, or if it is the root
Eric W. Biederman3ccb3542014-02-12 16:08:06 -0800896 * of a filesystem, or if the directory was renamed and d_revalidate
897 * was the first vfs operation to notice.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 *
NeilBrown21c0d8f2006-10-04 02:16:16 -0700899 * If the inode has an IS_ROOT, DCACHE_DISCONNECTED alias, then prefer
J. Bruce Fields52ed46f2014-01-16 11:15:51 -0500900 * any other hashed alias over that one.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 */
J. Bruce Fields52ed46f2014-01-16 11:15:51 -0500902static struct dentry *__d_find_alias(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
Nick Pigginda502952011-01-07 17:49:33 +1100904 struct dentry *alias, *discon_alias;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Nick Pigginda502952011-01-07 17:49:33 +1100906again:
907 discon_alias = NULL;
Al Viro946e51f2014-10-26 19:19:16 -0400908 hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
Nick Pigginda502952011-01-07 17:49:33 +1100909 spin_lock(&alias->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
NeilBrown21c0d8f2006-10-04 02:16:16 -0700911 if (IS_ROOT(alias) &&
Nick Pigginda502952011-01-07 17:49:33 +1100912 (alias->d_flags & DCACHE_DISCONNECTED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 discon_alias = alias;
J. Bruce Fields52ed46f2014-01-16 11:15:51 -0500914 } else {
Nick Piggindc0474b2011-01-07 17:49:43 +1100915 __dget_dlock(alias);
Nick Pigginda502952011-01-07 17:49:33 +1100916 spin_unlock(&alias->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 return alias;
918 }
919 }
Nick Pigginda502952011-01-07 17:49:33 +1100920 spin_unlock(&alias->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 }
Nick Pigginda502952011-01-07 17:49:33 +1100922 if (discon_alias) {
923 alias = discon_alias;
924 spin_lock(&alias->d_lock);
925 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
J. Bruce Fields8d80d7d2014-01-16 17:17:31 -0500926 __dget_dlock(alias);
927 spin_unlock(&alias->d_lock);
928 return alias;
Nick Pigginda502952011-01-07 17:49:33 +1100929 }
930 spin_unlock(&alias->d_lock);
931 goto again;
932 }
933 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934}
935
Nick Pigginda502952011-01-07 17:49:33 +1100936struct dentry *d_find_alias(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
David Howells214fda12006-03-25 03:06:36 -0800938 struct dentry *de = NULL;
939
Al Virob3d9b7a2012-06-09 13:51:19 -0400940 if (!hlist_empty(&inode->i_dentry)) {
Nick Piggin873feea2011-01-07 17:50:06 +1100941 spin_lock(&inode->i_lock);
J. Bruce Fields52ed46f2014-01-16 11:15:51 -0500942 de = __d_find_alias(inode);
Nick Piggin873feea2011-01-07 17:50:06 +1100943 spin_unlock(&inode->i_lock);
David Howells214fda12006-03-25 03:06:36 -0800944 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 return de;
946}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700947EXPORT_SYMBOL(d_find_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
949/*
950 * Try to kill dentries associated with this inode.
951 * WARNING: you must own a reference to inode.
952 */
953void d_prune_aliases(struct inode *inode)
954{
Domen Puncer0cdca3f2005-09-10 00:27:07 -0700955 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956restart:
Nick Piggin873feea2011-01-07 17:50:06 +1100957 spin_lock(&inode->i_lock);
Al Viro946e51f2014-10-26 19:19:16 -0400958 hlist_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 spin_lock(&dentry->d_lock);
Waiman Long98474232013-08-28 18:24:59 -0700960 if (!dentry->d_lockref.count) {
Al Viro29355c32014-05-30 11:25:30 -0400961 struct dentry *parent = lock_parent(dentry);
962 if (likely(!dentry->d_lockref.count)) {
963 __dentry_kill(dentry);
Yan, Zheng4a7795d2014-11-19 15:50:34 +0800964 dput(parent);
Al Viro29355c32014-05-30 11:25:30 -0400965 goto restart;
966 }
967 if (parent)
968 spin_unlock(&parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 }
970 spin_unlock(&dentry->d_lock);
971 }
Nick Piggin873feea2011-01-07 17:50:06 +1100972 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700974EXPORT_SYMBOL(d_prune_aliases);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400976static void shrink_dentry_list(struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977{
Al Viro5c47e6d2014-04-29 16:13:18 -0400978 struct dentry *dentry, *parent;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700979
Miklos Szeredi60942f22014-05-02 15:38:39 -0400980 while (!list_empty(list)) {
Al Viroff2fde92014-05-28 13:59:13 -0400981 struct inode *inode;
Miklos Szeredi60942f22014-05-02 15:38:39 -0400982 dentry = list_entry(list->prev, struct dentry, d_lru);
Nick Pigginec336792011-01-07 17:49:47 +1100983 spin_lock(&dentry->d_lock);
Al Viro046b9612014-05-29 08:54:52 -0400984 parent = lock_parent(dentry);
985
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 /*
Dave Chinnerdd1f6b22013-08-28 10:17:55 +1000987 * The dispose list is isolated and dentries are not accounted
988 * to the LRU here, so we can simply remove it from the list
989 * here regardless of whether it is referenced or not.
990 */
Linus Torvalds89dc77b2013-09-13 22:55:10 -0400991 d_shrink_del(dentry);
Dave Chinnerdd1f6b22013-08-28 10:17:55 +1000992
993 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 * We found an inuse dentry which was not removed from
Dave Chinnerdd1f6b22013-08-28 10:17:55 +1000995 * the LRU because of laziness during lookup. Do not free it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 */
Linus Torvalds360f5472015-01-09 15:19:03 -0800997 if (dentry->d_lockref.count > 0) {
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700998 spin_unlock(&dentry->d_lock);
Al Viro046b9612014-05-29 08:54:52 -0400999 if (parent)
1000 spin_unlock(&parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 continue;
1002 }
Nick Piggin77812a12011-01-07 17:49:48 +11001003
Al Viro64fd72e2014-05-28 09:48:44 -04001004
1005 if (unlikely(dentry->d_flags & DCACHE_DENTRY_KILLED)) {
1006 bool can_free = dentry->d_flags & DCACHE_MAY_FREE;
1007 spin_unlock(&dentry->d_lock);
Al Viro046b9612014-05-29 08:54:52 -04001008 if (parent)
1009 spin_unlock(&parent->d_lock);
Al Viro64fd72e2014-05-28 09:48:44 -04001010 if (can_free)
1011 dentry_free(dentry);
1012 continue;
1013 }
1014
Al Viroff2fde92014-05-28 13:59:13 -04001015 inode = dentry->d_inode;
1016 if (inode && unlikely(!spin_trylock(&inode->i_lock))) {
Linus Torvalds89dc77b2013-09-13 22:55:10 -04001017 d_shrink_add(dentry, list);
Dave Chinnerdd1f6b22013-08-28 10:17:55 +10001018 spin_unlock(&dentry->d_lock);
Al Viro046b9612014-05-29 08:54:52 -04001019 if (parent)
1020 spin_unlock(&parent->d_lock);
Al Viro5c47e6d2014-04-29 16:13:18 -04001021 continue;
Dave Chinnerdd1f6b22013-08-28 10:17:55 +10001022 }
Al Viroff2fde92014-05-28 13:59:13 -04001023
Al Viroff2fde92014-05-28 13:59:13 -04001024 __dentry_kill(dentry);
Al Viro046b9612014-05-29 08:54:52 -04001025
Al Viro5c47e6d2014-04-29 16:13:18 -04001026 /*
1027 * We need to prune ancestors too. This is necessary to prevent
1028 * quadratic behavior of shrink_dcache_parent(), but is also
1029 * expected to be beneficial in reducing dentry cache
1030 * fragmentation.
1031 */
1032 dentry = parent;
Al Virob2b80192014-05-29 09:11:45 -04001033 while (dentry && !lockref_put_or_lock(&dentry->d_lockref)) {
1034 parent = lock_parent(dentry);
1035 if (dentry->d_lockref.count != 1) {
1036 dentry->d_lockref.count--;
1037 spin_unlock(&dentry->d_lock);
1038 if (parent)
1039 spin_unlock(&parent->d_lock);
1040 break;
1041 }
1042 inode = dentry->d_inode; /* can't be NULL */
1043 if (unlikely(!spin_trylock(&inode->i_lock))) {
1044 spin_unlock(&dentry->d_lock);
1045 if (parent)
1046 spin_unlock(&parent->d_lock);
1047 cpu_relax();
1048 continue;
1049 }
1050 __dentry_kill(dentry);
1051 dentry = parent;
1052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 }
Christoph Hellwig3049cfe2010-10-10 05:36:25 -04001054}
1055
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001056static enum lru_status dentry_lru_isolate(struct list_head *item,
1057 struct list_lru_one *lru, spinlock_t *lru_lock, void *arg)
Dave Chinnerf6041562013-08-28 10:18:00 +10001058{
1059 struct list_head *freeable = arg;
1060 struct dentry *dentry = container_of(item, struct dentry, d_lru);
1061
1062
1063 /*
1064 * we are inverting the lru lock/dentry->d_lock here,
1065 * so use a trylock. If we fail to get the lock, just skip
1066 * it
1067 */
1068 if (!spin_trylock(&dentry->d_lock))
1069 return LRU_SKIP;
1070
1071 /*
1072 * Referenced dentries are still in use. If they have active
1073 * counts, just remove them from the LRU. Otherwise give them
1074 * another pass through the LRU.
1075 */
1076 if (dentry->d_lockref.count) {
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001077 d_lru_isolate(lru, dentry);
Dave Chinnerf6041562013-08-28 10:18:00 +10001078 spin_unlock(&dentry->d_lock);
1079 return LRU_REMOVED;
1080 }
1081
1082 if (dentry->d_flags & DCACHE_REFERENCED) {
1083 dentry->d_flags &= ~DCACHE_REFERENCED;
1084 spin_unlock(&dentry->d_lock);
1085
1086 /*
1087 * The list move itself will be made by the common LRU code. At
1088 * this point, we've dropped the dentry->d_lock but keep the
1089 * lru lock. This is safe to do, since every list movement is
1090 * protected by the lru lock even if both locks are held.
1091 *
1092 * This is guaranteed by the fact that all LRU management
1093 * functions are intermediated by the LRU API calls like
1094 * list_lru_add and list_lru_del. List movement in this file
1095 * only ever occur through this functions or through callbacks
1096 * like this one, that are called from the LRU API.
1097 *
1098 * The only exceptions to this are functions like
1099 * shrink_dentry_list, and code that first checks for the
1100 * DCACHE_SHRINK_LIST flag. Those are guaranteed to be
1101 * operating only with stack provided lists after they are
1102 * properly isolated from the main list. It is thus, always a
1103 * local access.
1104 */
1105 return LRU_ROTATE;
1106 }
1107
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001108 d_lru_shrink_move(lru, dentry, freeable);
Dave Chinnerf6041562013-08-28 10:18:00 +10001109 spin_unlock(&dentry->d_lock);
1110
1111 return LRU_REMOVED;
1112}
1113
Christoph Hellwig3049cfe2010-10-10 05:36:25 -04001114/**
Dave Chinnerb48f03b2011-08-23 18:56:24 +10001115 * prune_dcache_sb - shrink the dcache
1116 * @sb: superblock
Vladimir Davydov503c3582015-02-12 14:58:47 -08001117 * @sc: shrink control, passed to list_lru_shrink_walk()
Christoph Hellwig3049cfe2010-10-10 05:36:25 -04001118 *
Vladimir Davydov503c3582015-02-12 14:58:47 -08001119 * Attempt to shrink the superblock dcache LRU by @sc->nr_to_scan entries. This
1120 * is done when we need more memory and called from the superblock shrinker
Dave Chinnerb48f03b2011-08-23 18:56:24 +10001121 * function.
1122 *
1123 * This function may fail to free any resources if all the dentries are in
1124 * use.
Christoph Hellwig3049cfe2010-10-10 05:36:25 -04001125 */
Vladimir Davydov503c3582015-02-12 14:58:47 -08001126long prune_dcache_sb(struct super_block *sb, struct shrink_control *sc)
Christoph Hellwig3049cfe2010-10-10 05:36:25 -04001127{
Dave Chinnerf6041562013-08-28 10:18:00 +10001128 LIST_HEAD(dispose);
1129 long freed;
Christoph Hellwig3049cfe2010-10-10 05:36:25 -04001130
Vladimir Davydov503c3582015-02-12 14:58:47 -08001131 freed = list_lru_shrink_walk(&sb->s_dentry_lru, sc,
1132 dentry_lru_isolate, &dispose);
Dave Chinnerf6041562013-08-28 10:18:00 +10001133 shrink_dentry_list(&dispose);
Dave Chinner0a234c62013-08-28 10:17:57 +10001134 return freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135}
1136
Glauber Costa4e717f52013-08-28 10:18:03 +10001137static enum lru_status dentry_lru_isolate_shrink(struct list_head *item,
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001138 struct list_lru_one *lru, spinlock_t *lru_lock, void *arg)
Dave Chinnerdd1f6b22013-08-28 10:17:55 +10001139{
Glauber Costa4e717f52013-08-28 10:18:03 +10001140 struct list_head *freeable = arg;
1141 struct dentry *dentry = container_of(item, struct dentry, d_lru);
Dave Chinnerdd1f6b22013-08-28 10:17:55 +10001142
Glauber Costa4e717f52013-08-28 10:18:03 +10001143 /*
1144 * we are inverting the lru lock/dentry->d_lock here,
1145 * so use a trylock. If we fail to get the lock, just skip
1146 * it
1147 */
1148 if (!spin_trylock(&dentry->d_lock))
1149 return LRU_SKIP;
1150
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001151 d_lru_shrink_move(lru, dentry, freeable);
Glauber Costa4e717f52013-08-28 10:18:03 +10001152 spin_unlock(&dentry->d_lock);
1153
1154 return LRU_REMOVED;
Dave Chinnerdd1f6b22013-08-28 10:17:55 +10001155}
1156
Glauber Costa4e717f52013-08-28 10:18:03 +10001157
Kentaro Makitada3bbdd2008-07-23 21:27:13 -07001158/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 * shrink_dcache_sb - shrink dcache for a superblock
1160 * @sb: superblock
1161 *
Christoph Hellwig3049cfe2010-10-10 05:36:25 -04001162 * Shrink the dcache for the specified super block. This is used to free
1163 * the dcache before unmounting a file system.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 */
Christoph Hellwig3049cfe2010-10-10 05:36:25 -04001165void shrink_dcache_sb(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166{
Glauber Costa4e717f52013-08-28 10:18:03 +10001167 long freed;
Christoph Hellwig3049cfe2010-10-10 05:36:25 -04001168
Glauber Costa4e717f52013-08-28 10:18:03 +10001169 do {
1170 LIST_HEAD(dispose);
1171
1172 freed = list_lru_walk(&sb->s_dentry_lru,
Sahitya Tummalab17c0702017-07-10 15:50:00 -07001173 dentry_lru_isolate_shrink, &dispose, 1024);
Glauber Costa4e717f52013-08-28 10:18:03 +10001174
1175 this_cpu_sub(nr_dentry_unused, freed);
1176 shrink_dentry_list(&dispose);
Sahitya Tummalab17c0702017-07-10 15:50:00 -07001177 cond_resched();
1178 } while (list_lru_count(&sb->s_dentry_lru) > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001180EXPORT_SYMBOL(shrink_dcache_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182/**
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001183 * enum d_walk_ret - action to talke during tree walk
1184 * @D_WALK_CONTINUE: contrinue walk
1185 * @D_WALK_QUIT: quit walk
1186 * @D_WALK_NORETRY: quit when retry is needed
1187 * @D_WALK_SKIP: skip this dentry and its children
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 */
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001189enum d_walk_ret {
1190 D_WALK_CONTINUE,
1191 D_WALK_QUIT,
1192 D_WALK_NORETRY,
1193 D_WALK_SKIP,
1194};
1195
1196/**
1197 * d_walk - walk the dentry tree
1198 * @parent: start of walk
1199 * @data: data passed to @enter() and @finish()
1200 * @enter: callback when first entering the dentry
1201 * @finish: callback when successfully finished the walk
1202 *
1203 * The @enter() and @finish() callbacks are called with d_lock held.
1204 */
1205static void d_walk(struct dentry *parent, void *data,
1206 enum d_walk_ret (*enter)(void *, struct dentry *),
1207 void (*finish)(void *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208{
Nick Piggin949854d2011-01-07 17:49:37 +11001209 struct dentry *this_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 struct list_head *next;
Al Viro48f5ec22013-09-09 15:22:25 -04001211 unsigned seq = 0;
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001212 enum d_walk_ret ret;
1213 bool retry = true;
Nick Piggin949854d2011-01-07 17:49:37 +11001214
Nick Piggin58db63d2011-01-07 17:49:39 +11001215again:
Al Viro48f5ec22013-09-09 15:22:25 -04001216 read_seqbegin_or_lock(&rename_lock, &seq);
Nick Piggin58db63d2011-01-07 17:49:39 +11001217 this_parent = parent;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001218 spin_lock(&this_parent->d_lock);
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001219
1220 ret = enter(data, this_parent);
1221 switch (ret) {
1222 case D_WALK_CONTINUE:
1223 break;
1224 case D_WALK_QUIT:
1225 case D_WALK_SKIP:
1226 goto out_unlock;
1227 case D_WALK_NORETRY:
1228 retry = false;
1229 break;
1230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231repeat:
1232 next = this_parent->d_subdirs.next;
1233resume:
1234 while (next != &this_parent->d_subdirs) {
1235 struct list_head *tmp = next;
Al Viro946e51f2014-10-26 19:19:16 -04001236 struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 next = tmp->next;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001238
Al Viroba65dc52016-06-10 11:32:47 -04001239 if (unlikely(dentry->d_flags & DCACHE_DENTRY_CURSOR))
1240 continue;
1241
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001242 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001243
1244 ret = enter(data, dentry);
1245 switch (ret) {
1246 case D_WALK_CONTINUE:
1247 break;
1248 case D_WALK_QUIT:
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001249 spin_unlock(&dentry->d_lock);
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001250 goto out_unlock;
1251 case D_WALK_NORETRY:
1252 retry = false;
1253 break;
1254 case D_WALK_SKIP:
1255 spin_unlock(&dentry->d_lock);
1256 continue;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001257 }
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001258
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 if (!list_empty(&dentry->d_subdirs)) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001260 spin_unlock(&this_parent->d_lock);
1261 spin_release(&dentry->d_lock.dep_map, 1, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 this_parent = dentry;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001263 spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 goto repeat;
1265 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001266 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 }
1268 /*
1269 * All done at this level ... ascend and resume the search.
1270 */
Al Viroca5358e2014-10-26 19:31:10 -04001271 rcu_read_lock();
1272ascend:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 if (this_parent != parent) {
Linus Torvaldsc826cb72011-03-15 15:29:21 -07001274 struct dentry *child = this_parent;
Al Viro31dec132013-10-25 17:04:27 -04001275 this_parent = child->d_parent;
1276
Al Viro31dec132013-10-25 17:04:27 -04001277 spin_unlock(&child->d_lock);
1278 spin_lock(&this_parent->d_lock);
1279
Al Viroca5358e2014-10-26 19:31:10 -04001280 /* might go back up the wrong parent if we have had a rename. */
1281 if (need_seqretry(&rename_lock, seq))
Nick Piggin949854d2011-01-07 17:49:37 +11001282 goto rename_retry;
Al Viro21591842015-05-28 23:09:19 -04001283 /* go into the first sibling still alive */
1284 do {
1285 next = child->d_child.next;
Al Viroca5358e2014-10-26 19:31:10 -04001286 if (next == &this_parent->d_subdirs)
1287 goto ascend;
1288 child = list_entry(next, struct dentry, d_child);
Al Viro21591842015-05-28 23:09:19 -04001289 } while (unlikely(child->d_flags & DCACHE_DENTRY_KILLED));
Al Viro31dec132013-10-25 17:04:27 -04001290 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 goto resume;
1292 }
Al Viroca5358e2014-10-26 19:31:10 -04001293 if (need_seqretry(&rename_lock, seq))
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001294 goto rename_retry;
Al Viroca5358e2014-10-26 19:31:10 -04001295 rcu_read_unlock();
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001296 if (finish)
1297 finish(data);
1298
1299out_unlock:
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001300 spin_unlock(&this_parent->d_lock);
Al Viro48f5ec22013-09-09 15:22:25 -04001301 done_seqretry(&rename_lock, seq);
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001302 return;
Nick Piggin58db63d2011-01-07 17:49:39 +11001303
1304rename_retry:
Al Viroca5358e2014-10-26 19:31:10 -04001305 spin_unlock(&this_parent->d_lock);
1306 rcu_read_unlock();
1307 BUG_ON(seq & 1);
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001308 if (!retry)
1309 return;
Al Viro48f5ec22013-09-09 15:22:25 -04001310 seq = 1;
Nick Piggin58db63d2011-01-07 17:49:39 +11001311 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312}
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001313
Ian Kent01619492016-11-24 08:03:41 +11001314struct check_mount {
1315 struct vfsmount *mnt;
1316 unsigned int mounted;
1317};
1318
1319static enum d_walk_ret path_check_mount(void *data, struct dentry *dentry)
1320{
1321 struct check_mount *info = data;
1322 struct path path = { .mnt = info->mnt, .dentry = dentry };
1323
1324 if (likely(!d_mountpoint(dentry)))
1325 return D_WALK_CONTINUE;
1326 if (__path_is_mountpoint(&path)) {
1327 info->mounted = 1;
1328 return D_WALK_QUIT;
1329 }
1330 return D_WALK_CONTINUE;
1331}
1332
1333/**
1334 * path_has_submounts - check for mounts over a dentry in the
1335 * current namespace.
1336 * @parent: path to check.
1337 *
1338 * Return true if the parent or its subdirectories contain
1339 * a mount point in the current namespace.
1340 */
1341int path_has_submounts(const struct path *parent)
1342{
1343 struct check_mount data = { .mnt = parent->mnt, .mounted = 0 };
1344
1345 read_seqlock_excl(&mount_lock);
1346 d_walk(parent->dentry, &data, path_check_mount, NULL);
1347 read_sequnlock_excl(&mount_lock);
1348
1349 return data.mounted;
1350}
1351EXPORT_SYMBOL(path_has_submounts);
1352
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353/*
Miklos Szeredieed81002013-09-05 14:39:11 +02001354 * Called by mount code to set a mountpoint and check if the mountpoint is
1355 * reachable (e.g. NFS can unhash a directory dentry and then the complete
1356 * subtree can become unreachable).
1357 *
Eric W. Biederman1ffe46d2014-02-13 09:39:37 -08001358 * Only one of d_invalidate() and d_set_mounted() must succeed. For
Miklos Szeredieed81002013-09-05 14:39:11 +02001359 * this reason take rename_lock and d_lock on dentry and ancestors.
1360 */
1361int d_set_mounted(struct dentry *dentry)
1362{
1363 struct dentry *p;
1364 int ret = -ENOENT;
1365 write_seqlock(&rename_lock);
1366 for (p = dentry->d_parent; !IS_ROOT(p); p = p->d_parent) {
Eric W. Biederman1ffe46d2014-02-13 09:39:37 -08001367 /* Need exclusion wrt. d_invalidate() */
Miklos Szeredieed81002013-09-05 14:39:11 +02001368 spin_lock(&p->d_lock);
1369 if (unlikely(d_unhashed(p))) {
1370 spin_unlock(&p->d_lock);
1371 goto out;
1372 }
1373 spin_unlock(&p->d_lock);
1374 }
1375 spin_lock(&dentry->d_lock);
1376 if (!d_unlinked(dentry)) {
Eric W. Biederman3895dbf2017-01-03 14:18:43 +13001377 ret = -EBUSY;
1378 if (!d_mountpoint(dentry)) {
1379 dentry->d_flags |= DCACHE_MOUNTED;
1380 ret = 0;
1381 }
Miklos Szeredieed81002013-09-05 14:39:11 +02001382 }
1383 spin_unlock(&dentry->d_lock);
1384out:
1385 write_sequnlock(&rename_lock);
1386 return ret;
1387}
1388
1389/*
J. Bruce Fieldsfd517902012-09-18 16:35:51 -04001390 * Search the dentry child list of the specified parent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 * and move any unused dentries to the end of the unused
1392 * list for prune_dcache(). We descend to the next level
1393 * whenever the d_subdirs list is non-empty and continue
1394 * searching.
1395 *
1396 * It returns zero iff there are no unused children,
1397 * otherwise it returns the number of children moved to
1398 * the end of the unused list. This may not be the total
1399 * number of unused children, because select_parent can
1400 * drop the lock and return early due to latency
1401 * constraints.
1402 */
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001403
1404struct select_data {
1405 struct dentry *start;
1406 struct list_head dispose;
1407 int found;
1408};
1409
1410static enum d_walk_ret select_collect(void *_data, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411{
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001412 struct select_data *data = _data;
1413 enum d_walk_ret ret = D_WALK_CONTINUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001415 if (data->start == dentry)
1416 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
Al Virofe915222014-05-03 00:02:25 -04001418 if (dentry->d_flags & DCACHE_SHRINK_LIST) {
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001419 data->found++;
Al Virofe915222014-05-03 00:02:25 -04001420 } else {
1421 if (dentry->d_flags & DCACHE_LRU_LIST)
1422 d_lru_del(dentry);
1423 if (!dentry->d_lockref.count) {
1424 d_shrink_add(dentry, &data->dispose);
1425 data->found++;
1426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 }
1428 /*
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001429 * We can return to the caller if we have found some (this
1430 * ensures forward progress). We'll be coming back to find
1431 * the rest.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 */
Al Virofe915222014-05-03 00:02:25 -04001433 if (!list_empty(&data->dispose))
1434 ret = need_resched() ? D_WALK_QUIT : D_WALK_NORETRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435out:
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001436 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437}
1438
1439/**
1440 * shrink_dcache_parent - prune dcache
1441 * @parent: parent of entries to prune
1442 *
1443 * Prune the dcache to remove unused children of the parent dentry.
1444 */
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001445void shrink_dcache_parent(struct dentry *parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446{
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001447 for (;;) {
1448 struct select_data data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001450 INIT_LIST_HEAD(&data.dispose);
1451 data.start = parent;
1452 data.found = 0;
1453
1454 d_walk(parent, &data, select_collect, NULL);
1455 if (!data.found)
1456 break;
1457
1458 shrink_dentry_list(&data.dispose);
Greg Thelen421348f2013-04-30 15:26:48 -07001459 cond_resched();
1460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001462EXPORT_SYMBOL(shrink_dcache_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
Al Viro9c8c10e2014-05-02 20:36:10 -04001464static enum d_walk_ret umount_check(void *_data, struct dentry *dentry)
Al Viro42c32602013-11-08 12:31:16 -05001465{
Al Viro9c8c10e2014-05-02 20:36:10 -04001466 /* it has busy descendents; complain about those instead */
1467 if (!list_empty(&dentry->d_subdirs))
1468 return D_WALK_CONTINUE;
Al Viro42c32602013-11-08 12:31:16 -05001469
Al Viro9c8c10e2014-05-02 20:36:10 -04001470 /* root with refcount 1 is fine */
1471 if (dentry == _data && dentry->d_lockref.count == 1)
1472 return D_WALK_CONTINUE;
1473
1474 printk(KERN_ERR "BUG: Dentry %p{i=%lx,n=%pd} "
1475 " still in use (%d) [unmount of %s %s]\n",
Al Viro42c32602013-11-08 12:31:16 -05001476 dentry,
1477 dentry->d_inode ?
1478 dentry->d_inode->i_ino : 0UL,
Al Viro9c8c10e2014-05-02 20:36:10 -04001479 dentry,
Al Viro42c32602013-11-08 12:31:16 -05001480 dentry->d_lockref.count,
1481 dentry->d_sb->s_type->name,
1482 dentry->d_sb->s_id);
Al Viro9c8c10e2014-05-02 20:36:10 -04001483 WARN_ON(1);
1484 return D_WALK_CONTINUE;
1485}
1486
1487static void do_one_tree(struct dentry *dentry)
1488{
1489 shrink_dcache_parent(dentry);
1490 d_walk(dentry, dentry, umount_check, NULL);
1491 d_drop(dentry);
1492 dput(dentry);
Al Viro42c32602013-11-08 12:31:16 -05001493}
1494
1495/*
1496 * destroy the dentries attached to a superblock on unmounting
1497 */
1498void shrink_dcache_for_umount(struct super_block *sb)
1499{
1500 struct dentry *dentry;
1501
Al Viro9c8c10e2014-05-02 20:36:10 -04001502 WARN(down_read_trylock(&sb->s_umount), "s_umount should've been locked");
Al Viro42c32602013-11-08 12:31:16 -05001503
1504 dentry = sb->s_root;
1505 sb->s_root = NULL;
Al Viro9c8c10e2014-05-02 20:36:10 -04001506 do_one_tree(dentry);
Al Viro42c32602013-11-08 12:31:16 -05001507
NeilBrownf1ee6162017-12-21 09:45:40 +11001508 while (!hlist_bl_empty(&sb->s_roots)) {
1509 dentry = dget(hlist_bl_entry(hlist_bl_first(&sb->s_roots), struct dentry, d_hash));
Al Viro9c8c10e2014-05-02 20:36:10 -04001510 do_one_tree(dentry);
Al Viro42c32602013-11-08 12:31:16 -05001511 }
1512}
1513
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07001514struct detach_data {
1515 struct select_data select;
1516 struct dentry *mountpoint;
1517};
1518static enum d_walk_ret detach_and_collect(void *_data, struct dentry *dentry)
Miklos Szeredi848ac1142013-09-05 11:44:36 +02001519{
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07001520 struct detach_data *data = _data;
Miklos Szeredi848ac1142013-09-05 11:44:36 +02001521
1522 if (d_mountpoint(dentry)) {
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07001523 __dget_dlock(dentry);
1524 data->mountpoint = dentry;
Miklos Szeredi848ac1142013-09-05 11:44:36 +02001525 return D_WALK_QUIT;
1526 }
1527
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07001528 return select_collect(&data->select, dentry);
Miklos Szeredi848ac1142013-09-05 11:44:36 +02001529}
1530
1531static void check_and_drop(void *_data)
1532{
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07001533 struct detach_data *data = _data;
Miklos Szeredi848ac1142013-09-05 11:44:36 +02001534
Al Viro81be24d2017-06-03 07:20:09 +01001535 if (!data->mountpoint && list_empty(&data->select.dispose))
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07001536 __d_drop(data->select.start);
Miklos Szeredi848ac1142013-09-05 11:44:36 +02001537}
1538
1539/**
Eric W. Biederman1ffe46d2014-02-13 09:39:37 -08001540 * d_invalidate - detach submounts, prune dcache, and drop
1541 * @dentry: dentry to invalidate (aka detach, prune and drop)
1542 *
Eric W. Biederman1ffe46d2014-02-13 09:39:37 -08001543 * no dcache lock.
Miklos Szeredi848ac1142013-09-05 11:44:36 +02001544 *
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07001545 * The final d_drop is done as an atomic operation relative to
1546 * rename_lock ensuring there are no races with d_set_mounted. This
1547 * ensures there are no unhashed dentries on the path to a mountpoint.
Miklos Szeredi848ac1142013-09-05 11:44:36 +02001548 */
Eric W. Biederman5542aa22014-02-13 09:46:25 -08001549void d_invalidate(struct dentry *dentry)
Miklos Szeredi848ac1142013-09-05 11:44:36 +02001550{
Eric W. Biederman1ffe46d2014-02-13 09:39:37 -08001551 /*
1552 * If it's already been dropped, return OK.
1553 */
1554 spin_lock(&dentry->d_lock);
1555 if (d_unhashed(dentry)) {
1556 spin_unlock(&dentry->d_lock);
Eric W. Biederman5542aa22014-02-13 09:46:25 -08001557 return;
Eric W. Biederman1ffe46d2014-02-13 09:39:37 -08001558 }
1559 spin_unlock(&dentry->d_lock);
1560
Miklos Szeredi848ac1142013-09-05 11:44:36 +02001561 /* Negative dentries can be dropped without further checks */
1562 if (!dentry->d_inode) {
1563 d_drop(dentry);
Eric W. Biederman5542aa22014-02-13 09:46:25 -08001564 return;
Miklos Szeredi848ac1142013-09-05 11:44:36 +02001565 }
1566
1567 for (;;) {
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07001568 struct detach_data data;
Miklos Szeredi848ac1142013-09-05 11:44:36 +02001569
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07001570 data.mountpoint = NULL;
1571 INIT_LIST_HEAD(&data.select.dispose);
1572 data.select.start = dentry;
1573 data.select.found = 0;
Miklos Szeredi848ac1142013-09-05 11:44:36 +02001574
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07001575 d_walk(dentry, &data, detach_and_collect, check_and_drop);
Miklos Szeredi848ac1142013-09-05 11:44:36 +02001576
Al Viro81be24d2017-06-03 07:20:09 +01001577 if (!list_empty(&data.select.dispose))
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07001578 shrink_dentry_list(&data.select.dispose);
Al Viro81be24d2017-06-03 07:20:09 +01001579 else if (!data.mountpoint)
1580 return;
Miklos Szeredi848ac1142013-09-05 11:44:36 +02001581
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07001582 if (data.mountpoint) {
1583 detach_mounts(data.mountpoint);
1584 dput(data.mountpoint);
1585 }
Miklos Szeredi848ac1142013-09-05 11:44:36 +02001586 cond_resched();
1587 }
Miklos Szeredi848ac1142013-09-05 11:44:36 +02001588}
Eric W. Biederman1ffe46d2014-02-13 09:39:37 -08001589EXPORT_SYMBOL(d_invalidate);
Miklos Szeredi848ac1142013-09-05 11:44:36 +02001590
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591/**
Al Viroa4464db2011-07-07 15:03:58 -04001592 * __d_alloc - allocate a dcache entry
1593 * @sb: filesystem it will belong to
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 * @name: qstr of the name
1595 *
1596 * Allocates a dentry. It returns %NULL if there is insufficient memory
1597 * available. On a success the dentry is returned. The name passed in is
1598 * copied and the copy passed in may be reused after this call.
1599 */
1600
Al Viroa4464db2011-07-07 15:03:58 -04001601struct dentry *__d_alloc(struct super_block *sb, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602{
1603 struct dentry *dentry;
1604 char *dname;
Miklos Szeredi285b1022016-06-28 11:47:32 +02001605 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
Mel Gormane12ba742007-10-16 01:25:52 -07001607 dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 if (!dentry)
1609 return NULL;
1610
Linus Torvalds6326c712012-05-21 16:14:04 -07001611 /*
1612 * We guarantee that the inline name is always NUL-terminated.
1613 * This way the memcpy() done by the name switching in rename
1614 * will still always have a NUL at the end, even if we might
1615 * be overwriting an internal NUL character
1616 */
1617 dentry->d_iname[DNAME_INLINE_LEN-1] = 0;
Al Viro798434b2016-03-24 20:38:43 -04001618 if (unlikely(!name)) {
David Howellscdf01222017-07-04 17:25:22 +01001619 name = &slash_name;
Al Viro798434b2016-03-24 20:38:43 -04001620 dname = dentry->d_iname;
1621 } else if (name->len > DNAME_INLINE_LEN-1) {
Al Viro8d85b482014-09-29 14:54:27 -04001622 size_t size = offsetof(struct external_name, name[1]);
Vladimir Davydov5d097052016-01-14 15:18:21 -08001623 struct external_name *p = kmalloc(size + name->len,
1624 GFP_KERNEL_ACCOUNT);
Al Viro8d85b482014-09-29 14:54:27 -04001625 if (!p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 kmem_cache_free(dentry_cache, dentry);
1627 return NULL;
1628 }
Al Viro8d85b482014-09-29 14:54:27 -04001629 atomic_set(&p->u.count, 1);
1630 dname = p->name;
Andrey Ryabinindf4c0e32015-02-13 14:39:45 -08001631 if (IS_ENABLED(CONFIG_DCACHE_WORD_ACCESS))
1632 kasan_unpoison_shadow(dname,
1633 round_up(name->len + 1, sizeof(unsigned long)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 } else {
1635 dname = dentry->d_iname;
1636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
1638 dentry->d_name.len = name->len;
1639 dentry->d_name.hash = name->hash;
1640 memcpy(dname, name->name, name->len);
1641 dname[name->len] = 0;
1642
Linus Torvalds6326c712012-05-21 16:14:04 -07001643 /* Make sure we always see the terminating NUL character */
Paul E. McKenney7088efa2017-10-09 10:04:27 -07001644 smp_store_release(&dentry->d_name.name, dname); /* ^^^ */
Linus Torvalds6326c712012-05-21 16:14:04 -07001645
Waiman Long98474232013-08-28 18:24:59 -07001646 dentry->d_lockref.count = 1;
Linus Torvaldsdea36672011-04-24 07:58:46 -07001647 dentry->d_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 spin_lock_init(&dentry->d_lock);
Nick Piggin31e6b012011-01-07 17:49:52 +11001649 seqcount_init(&dentry->d_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 dentry->d_inode = NULL;
Al Viroa4464db2011-07-07 15:03:58 -04001651 dentry->d_parent = dentry;
1652 dentry->d_sb = sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 dentry->d_op = NULL;
1654 dentry->d_fsdata = NULL;
Nick Pigginceb5bdc2011-01-07 17:50:05 +11001655 INIT_HLIST_BL_NODE(&dentry->d_hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 INIT_LIST_HEAD(&dentry->d_lru);
1657 INIT_LIST_HEAD(&dentry->d_subdirs);
Al Viro946e51f2014-10-26 19:19:16 -04001658 INIT_HLIST_NODE(&dentry->d_u.d_alias);
1659 INIT_LIST_HEAD(&dentry->d_child);
Al Viroa4464db2011-07-07 15:03:58 -04001660 d_set_d_op(dentry, dentry->d_sb->s_d_op);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
Miklos Szeredi285b1022016-06-28 11:47:32 +02001662 if (dentry->d_op && dentry->d_op->d_init) {
1663 err = dentry->d_op->d_init(dentry);
1664 if (err) {
1665 if (dname_external(dentry))
1666 kfree(external_name(dentry));
1667 kmem_cache_free(dentry_cache, dentry);
1668 return NULL;
1669 }
1670 }
1671
Nick Piggin3e880fb2011-01-07 17:49:19 +11001672 this_cpu_inc(nr_dentry);
Christoph Hellwig312d3ca2010-10-10 05:36:23 -04001673
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 return dentry;
1675}
Al Viroa4464db2011-07-07 15:03:58 -04001676
1677/**
1678 * d_alloc - allocate a dcache entry
1679 * @parent: parent of entry to allocate
1680 * @name: qstr of the name
1681 *
1682 * Allocates a dentry. It returns %NULL if there is insufficient memory
1683 * available. On a success the dentry is returned. The name passed in is
1684 * copied and the copy passed in may be reused after this call.
1685 */
1686struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
1687{
1688 struct dentry *dentry = __d_alloc(parent->d_sb, name);
1689 if (!dentry)
1690 return NULL;
Al Viro3d56c252016-06-07 21:26:55 -04001691 dentry->d_flags |= DCACHE_RCUACCESS;
Al Viroa4464db2011-07-07 15:03:58 -04001692 spin_lock(&parent->d_lock);
1693 /*
1694 * don't need child lock because it is not subject
1695 * to concurrency here
1696 */
1697 __dget_dlock(parent);
1698 dentry->d_parent = parent;
Al Viro946e51f2014-10-26 19:19:16 -04001699 list_add(&dentry->d_child, &parent->d_subdirs);
Al Viroa4464db2011-07-07 15:03:58 -04001700 spin_unlock(&parent->d_lock);
1701
1702 return dentry;
1703}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001704EXPORT_SYMBOL(d_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
Al Viroba65dc52016-06-10 11:32:47 -04001706struct dentry *d_alloc_cursor(struct dentry * parent)
1707{
1708 struct dentry *dentry = __d_alloc(parent->d_sb, NULL);
1709 if (dentry) {
1710 dentry->d_flags |= DCACHE_RCUACCESS | DCACHE_DENTRY_CURSOR;
1711 dentry->d_parent = dget(parent);
1712 }
1713 return dentry;
1714}
1715
J. Bruce Fieldse1a24bb2012-06-29 16:20:47 -04001716/**
1717 * d_alloc_pseudo - allocate a dentry (for lookup-less filesystems)
1718 * @sb: the superblock
1719 * @name: qstr of the name
1720 *
1721 * For a filesystem that just pins its dentries in memory and never
1722 * performs lookups at all, return an unhashed IS_ROOT dentry.
1723 */
Nick Piggin4b936882011-01-07 17:50:07 +11001724struct dentry *d_alloc_pseudo(struct super_block *sb, const struct qstr *name)
1725{
J. Bruce Fieldse1a24bb2012-06-29 16:20:47 -04001726 return __d_alloc(sb, name);
Nick Piggin4b936882011-01-07 17:50:07 +11001727}
1728EXPORT_SYMBOL(d_alloc_pseudo);
1729
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730struct dentry *d_alloc_name(struct dentry *parent, const char *name)
1731{
1732 struct qstr q;
1733
1734 q.name = name;
Linus Torvalds8387ff22016-06-10 07:51:30 -07001735 q.hash_len = hashlen_string(parent, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 return d_alloc(parent, &q);
1737}
H Hartley Sweetenef26ca92009-09-29 20:09:42 -04001738EXPORT_SYMBOL(d_alloc_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
Nick Pigginfb045ad2011-01-07 17:49:55 +11001740void d_set_d_op(struct dentry *dentry, const struct dentry_operations *op)
1741{
Linus Torvalds6f7f7ca2011-01-14 13:26:18 -08001742 WARN_ON_ONCE(dentry->d_op);
1743 WARN_ON_ONCE(dentry->d_flags & (DCACHE_OP_HASH |
Nick Pigginfb045ad2011-01-07 17:49:55 +11001744 DCACHE_OP_COMPARE |
1745 DCACHE_OP_REVALIDATE |
Jeff Laytonecf3d1f2013-02-20 11:19:05 -05001746 DCACHE_OP_WEAK_REVALIDATE |
David Howells4bacc9c2015-06-18 14:32:31 +01001747 DCACHE_OP_DELETE |
Miklos Szeredid101a122016-03-26 16:14:37 -04001748 DCACHE_OP_REAL));
Nick Pigginfb045ad2011-01-07 17:49:55 +11001749 dentry->d_op = op;
1750 if (!op)
1751 return;
1752 if (op->d_hash)
1753 dentry->d_flags |= DCACHE_OP_HASH;
1754 if (op->d_compare)
1755 dentry->d_flags |= DCACHE_OP_COMPARE;
1756 if (op->d_revalidate)
1757 dentry->d_flags |= DCACHE_OP_REVALIDATE;
Jeff Laytonecf3d1f2013-02-20 11:19:05 -05001758 if (op->d_weak_revalidate)
1759 dentry->d_flags |= DCACHE_OP_WEAK_REVALIDATE;
Nick Pigginfb045ad2011-01-07 17:49:55 +11001760 if (op->d_delete)
1761 dentry->d_flags |= DCACHE_OP_DELETE;
Sage Weilf0023bc2011-10-28 10:02:42 -07001762 if (op->d_prune)
1763 dentry->d_flags |= DCACHE_OP_PRUNE;
Miklos Szeredid101a122016-03-26 16:14:37 -04001764 if (op->d_real)
1765 dentry->d_flags |= DCACHE_OP_REAL;
Nick Pigginfb045ad2011-01-07 17:49:55 +11001766
1767}
1768EXPORT_SYMBOL(d_set_d_op);
1769
David Howellsdf1a0852015-01-29 12:02:28 +00001770
1771/*
1772 * d_set_fallthru - Mark a dentry as falling through to a lower layer
1773 * @dentry - The dentry to mark
1774 *
1775 * Mark a dentry as falling through to the lower layer (as set with
1776 * d_pin_lower()). This flag may be recorded on the medium.
1777 */
1778void d_set_fallthru(struct dentry *dentry)
1779{
1780 spin_lock(&dentry->d_lock);
1781 dentry->d_flags |= DCACHE_FALLTHRU;
1782 spin_unlock(&dentry->d_lock);
1783}
1784EXPORT_SYMBOL(d_set_fallthru);
1785
David Howellsb18825a2013-09-12 19:22:53 +01001786static unsigned d_flags_for_inode(struct inode *inode)
1787{
David Howells44bdb5e2015-01-29 12:02:29 +00001788 unsigned add_flags = DCACHE_REGULAR_TYPE;
David Howellsb18825a2013-09-12 19:22:53 +01001789
1790 if (!inode)
1791 return DCACHE_MISS_TYPE;
1792
1793 if (S_ISDIR(inode->i_mode)) {
1794 add_flags = DCACHE_DIRECTORY_TYPE;
1795 if (unlikely(!(inode->i_opflags & IOP_LOOKUP))) {
1796 if (unlikely(!inode->i_op->lookup))
1797 add_flags = DCACHE_AUTODIR_TYPE;
1798 else
1799 inode->i_opflags |= IOP_LOOKUP;
1800 }
David Howells44bdb5e2015-01-29 12:02:29 +00001801 goto type_determined;
David Howellsb18825a2013-09-12 19:22:53 +01001802 }
1803
David Howells44bdb5e2015-01-29 12:02:29 +00001804 if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) {
Al Viro6b255392015-11-17 10:20:54 -05001805 if (unlikely(inode->i_op->get_link)) {
David Howells44bdb5e2015-01-29 12:02:29 +00001806 add_flags = DCACHE_SYMLINK_TYPE;
1807 goto type_determined;
1808 }
1809 inode->i_opflags |= IOP_NOFOLLOW;
1810 }
1811
1812 if (unlikely(!S_ISREG(inode->i_mode)))
1813 add_flags = DCACHE_SPECIAL_TYPE;
1814
1815type_determined:
David Howellsb18825a2013-09-12 19:22:53 +01001816 if (unlikely(IS_AUTOMOUNT(inode)))
1817 add_flags |= DCACHE_NEED_AUTOMOUNT;
1818 return add_flags;
1819}
1820
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001821static void __d_instantiate(struct dentry *dentry, struct inode *inode)
1822{
David Howellsb18825a2013-09-12 19:22:53 +01001823 unsigned add_flags = d_flags_for_inode(inode);
Al Viro85c7f812016-04-14 19:52:13 -04001824 WARN_ON(d_in_lookup(dentry));
David Howellsb18825a2013-09-12 19:22:53 +01001825
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001826 spin_lock(&dentry->d_lock);
Al Virode689f52016-03-09 18:05:42 -05001827 hlist_add_head(&dentry->d_u.d_alias, &inode->i_dentry);
Al Viroa528aca2016-02-29 12:12:46 -05001828 raw_write_seqcount_begin(&dentry->d_seq);
David Howells4bf46a22015-03-05 14:09:22 +00001829 __d_set_inode_and_type(dentry, inode, add_flags);
Al Viroa528aca2016-02-29 12:12:46 -05001830 raw_write_seqcount_end(&dentry->d_seq);
Al Viroaffda482016-05-29 18:35:12 -04001831 fsnotify_update_flags(dentry);
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001832 spin_unlock(&dentry->d_lock);
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001833}
1834
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835/**
1836 * d_instantiate - fill in inode information for a dentry
1837 * @entry: dentry to complete
1838 * @inode: inode to attach to this dentry
1839 *
1840 * Fill in inode information in the entry.
1841 *
1842 * This turns negative dentries into productive full members
1843 * of society.
1844 *
1845 * NOTE! This assumes that the inode count has been incremented
1846 * (or otherwise set) by the caller to indicate that it is now
1847 * in use by the dcache.
1848 */
1849
1850void d_instantiate(struct dentry *entry, struct inode * inode)
1851{
Al Viro946e51f2014-10-26 19:19:16 -04001852 BUG_ON(!hlist_unhashed(&entry->d_u.d_alias));
Al Virode689f52016-03-09 18:05:42 -05001853 if (inode) {
Al Virob9680912016-04-11 00:53:26 -04001854 security_d_instantiate(entry, inode);
Nick Piggin873feea2011-01-07 17:50:06 +11001855 spin_lock(&inode->i_lock);
Al Virode689f52016-03-09 18:05:42 -05001856 __d_instantiate(entry, inode);
Nick Piggin873feea2011-01-07 17:50:06 +11001857 spin_unlock(&inode->i_lock);
Al Virode689f52016-03-09 18:05:42 -05001858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001860EXPORT_SYMBOL(d_instantiate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
1862/**
Miklos Szeredib70a80e2013-10-01 16:44:54 +02001863 * d_instantiate_no_diralias - instantiate a non-aliased dentry
1864 * @entry: dentry to complete
1865 * @inode: inode to attach to this dentry
1866 *
1867 * Fill in inode information in the entry. If a directory alias is found, then
1868 * return an error (and drop inode). Together with d_materialise_unique() this
1869 * guarantees that a directory inode may never have more than one alias.
1870 */
1871int d_instantiate_no_diralias(struct dentry *entry, struct inode *inode)
1872{
Al Viro946e51f2014-10-26 19:19:16 -04001873 BUG_ON(!hlist_unhashed(&entry->d_u.d_alias));
Miklos Szeredib70a80e2013-10-01 16:44:54 +02001874
Al Virob9680912016-04-11 00:53:26 -04001875 security_d_instantiate(entry, inode);
Miklos Szeredib70a80e2013-10-01 16:44:54 +02001876 spin_lock(&inode->i_lock);
1877 if (S_ISDIR(inode->i_mode) && !hlist_empty(&inode->i_dentry)) {
1878 spin_unlock(&inode->i_lock);
1879 iput(inode);
1880 return -EBUSY;
1881 }
1882 __d_instantiate(entry, inode);
1883 spin_unlock(&inode->i_lock);
Miklos Szeredib70a80e2013-10-01 16:44:54 +02001884
1885 return 0;
1886}
1887EXPORT_SYMBOL(d_instantiate_no_diralias);
1888
Al Viroadc0e912012-01-08 16:49:21 -05001889struct dentry *d_make_root(struct inode *root_inode)
1890{
1891 struct dentry *res = NULL;
1892
1893 if (root_inode) {
Al Viro798434b2016-03-24 20:38:43 -04001894 res = __d_alloc(root_inode->i_sb, NULL);
Al Viroadc0e912012-01-08 16:49:21 -05001895 if (res)
1896 d_instantiate(res, root_inode);
1897 else
1898 iput(root_inode);
1899 }
1900 return res;
1901}
1902EXPORT_SYMBOL(d_make_root);
1903
J. Bruce Fieldsd891eed2011-01-18 15:45:09 -05001904static struct dentry * __d_find_any_alias(struct inode *inode)
1905{
1906 struct dentry *alias;
1907
Al Virob3d9b7a2012-06-09 13:51:19 -04001908 if (hlist_empty(&inode->i_dentry))
J. Bruce Fieldsd891eed2011-01-18 15:45:09 -05001909 return NULL;
Al Viro946e51f2014-10-26 19:19:16 -04001910 alias = hlist_entry(inode->i_dentry.first, struct dentry, d_u.d_alias);
J. Bruce Fieldsd891eed2011-01-18 15:45:09 -05001911 __dget(alias);
1912 return alias;
1913}
1914
Sage Weil46f72b32012-01-10 09:04:37 -08001915/**
1916 * d_find_any_alias - find any alias for a given inode
1917 * @inode: inode to find an alias for
1918 *
1919 * If any aliases exist for the given inode, take and return a
1920 * reference for one of them. If no aliases exist, return %NULL.
1921 */
1922struct dentry *d_find_any_alias(struct inode *inode)
J. Bruce Fieldsd891eed2011-01-18 15:45:09 -05001923{
1924 struct dentry *de;
1925
1926 spin_lock(&inode->i_lock);
1927 de = __d_find_any_alias(inode);
1928 spin_unlock(&inode->i_lock);
1929 return de;
1930}
Sage Weil46f72b32012-01-10 09:04:37 -08001931EXPORT_SYMBOL(d_find_any_alias);
J. Bruce Fieldsd891eed2011-01-18 15:45:09 -05001932
Fengguang Wu49c7dd22014-07-31 17:59:02 -04001933static struct dentry *__d_obtain_alias(struct inode *inode, int disconnected)
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001934{
Christoph Hellwig9308a612008-08-11 15:49:12 +02001935 struct dentry *tmp;
1936 struct dentry *res;
David Howellsb18825a2013-09-12 19:22:53 +01001937 unsigned add_flags;
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001938
1939 if (!inode)
Christoph Hellwig44003722008-08-11 15:49:04 +02001940 return ERR_PTR(-ESTALE);
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001941 if (IS_ERR(inode))
1942 return ERR_CAST(inode);
1943
J. Bruce Fieldsd891eed2011-01-18 15:45:09 -05001944 res = d_find_any_alias(inode);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001945 if (res)
1946 goto out_iput;
1947
Al Viro798434b2016-03-24 20:38:43 -04001948 tmp = __d_alloc(inode->i_sb, NULL);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001949 if (!tmp) {
1950 res = ERR_PTR(-ENOMEM);
1951 goto out_iput;
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001952 }
Nick Pigginb5c84bf2011-01-07 17:49:38 +11001953
Al Virob9680912016-04-11 00:53:26 -04001954 security_d_instantiate(tmp, inode);
Nick Piggin873feea2011-01-07 17:50:06 +11001955 spin_lock(&inode->i_lock);
J. Bruce Fieldsd891eed2011-01-18 15:45:09 -05001956 res = __d_find_any_alias(inode);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001957 if (res) {
Nick Piggin873feea2011-01-07 17:50:06 +11001958 spin_unlock(&inode->i_lock);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001959 dput(tmp);
1960 goto out_iput;
1961 }
1962
1963 /* attach a disconnected dentry */
J. Bruce Fields1a0a3972014-02-14 17:35:37 -05001964 add_flags = d_flags_for_inode(inode);
1965
1966 if (disconnected)
1967 add_flags |= DCACHE_DISCONNECTED;
David Howellsb18825a2013-09-12 19:22:53 +01001968
Christoph Hellwig9308a612008-08-11 15:49:12 +02001969 spin_lock(&tmp->d_lock);
David Howells4bf46a22015-03-05 14:09:22 +00001970 __d_set_inode_and_type(tmp, inode, add_flags);
Al Viro946e51f2014-10-26 19:19:16 -04001971 hlist_add_head(&tmp->d_u.d_alias, &inode->i_dentry);
NeilBrownf1ee6162017-12-21 09:45:40 +11001972 if (!disconnected) {
1973 hlist_bl_lock(&tmp->d_sb->s_roots);
1974 hlist_bl_add_head(&tmp->d_hash, &tmp->d_sb->s_roots);
1975 hlist_bl_unlock(&tmp->d_sb->s_roots);
1976 }
Christoph Hellwig9308a612008-08-11 15:49:12 +02001977 spin_unlock(&tmp->d_lock);
Nick Piggin873feea2011-01-07 17:50:06 +11001978 spin_unlock(&inode->i_lock);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001979
Christoph Hellwig9308a612008-08-11 15:49:12 +02001980 return tmp;
1981
1982 out_iput:
1983 iput(inode);
1984 return res;
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001985}
J. Bruce Fields1a0a3972014-02-14 17:35:37 -05001986
1987/**
1988 * d_obtain_alias - find or allocate a DISCONNECTED dentry for a given inode
1989 * @inode: inode to allocate the dentry for
1990 *
1991 * Obtain a dentry for an inode resulting from NFS filehandle conversion or
1992 * similar open by handle operations. The returned dentry may be anonymous,
1993 * or may have a full name (if the inode was already in the cache).
1994 *
1995 * When called on a directory inode, we must ensure that the inode only ever
1996 * has one dentry. If a dentry is found, that is returned instead of
1997 * allocating a new one.
1998 *
1999 * On successful return, the reference to the inode has been transferred
2000 * to the dentry. In case of an error the reference on the inode is released.
2001 * To make it easier to use in export operations a %NULL or IS_ERR inode may
2002 * be passed in and the error will be propagated to the return value,
2003 * with a %NULL @inode replaced by ERR_PTR(-ESTALE).
2004 */
2005struct dentry *d_obtain_alias(struct inode *inode)
2006{
2007 return __d_obtain_alias(inode, 1);
2008}
Benny Halevyadc48722009-02-27 14:02:59 -08002009EXPORT_SYMBOL(d_obtain_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
2011/**
J. Bruce Fields1a0a3972014-02-14 17:35:37 -05002012 * d_obtain_root - find or allocate a dentry for a given inode
2013 * @inode: inode to allocate the dentry for
2014 *
2015 * Obtain an IS_ROOT dentry for the root of a filesystem.
2016 *
2017 * We must ensure that directory inodes only ever have one dentry. If a
2018 * dentry is found, that is returned instead of allocating a new one.
2019 *
2020 * On successful return, the reference to the inode has been transferred
2021 * to the dentry. In case of an error the reference on the inode is
2022 * released. A %NULL or IS_ERR inode may be passed in and will be the
2023 * error will be propagate to the return value, with a %NULL @inode
2024 * replaced by ERR_PTR(-ESTALE).
2025 */
2026struct dentry *d_obtain_root(struct inode *inode)
2027{
2028 return __d_obtain_alias(inode, 0);
2029}
2030EXPORT_SYMBOL(d_obtain_root);
2031
2032/**
Barry Naujok94035402008-05-21 16:50:46 +10002033 * d_add_ci - lookup or allocate new dentry with case-exact name
2034 * @inode: the inode case-insensitive lookup has found
2035 * @dentry: the negative dentry that was passed to the parent's lookup func
2036 * @name: the case-exact name to be associated with the returned dentry
2037 *
2038 * This is to avoid filling the dcache with case-insensitive names to the
2039 * same inode, only the actual correct case is stored in the dcache for
2040 * case-insensitive filesystems.
2041 *
2042 * For a case-insensitive lookup match and if the the case-exact dentry
2043 * already exists in in the dcache, use it and return it.
2044 *
2045 * If no entry exists with the exact case name, allocate new dentry with
2046 * the exact case, and return the spliced entry.
2047 */
Christoph Hellwige45b5902008-08-07 23:49:07 +02002048struct dentry *d_add_ci(struct dentry *dentry, struct inode *inode,
Barry Naujok94035402008-05-21 16:50:46 +10002049 struct qstr *name)
2050{
Al Virod9171b92016-04-15 03:33:13 -04002051 struct dentry *found, *res;
Barry Naujok94035402008-05-21 16:50:46 +10002052
Christoph Hellwigb6520c82009-01-05 19:10:37 +01002053 /*
2054 * First check if a dentry matching the name already exists,
2055 * if not go ahead and create it now.
2056 */
Barry Naujok94035402008-05-21 16:50:46 +10002057 found = d_hash_and_lookup(dentry->d_parent, name);
Al Virod9171b92016-04-15 03:33:13 -04002058 if (found) {
2059 iput(inode);
2060 return found;
Barry Naujok94035402008-05-21 16:50:46 +10002061 }
Al Virod9171b92016-04-15 03:33:13 -04002062 if (d_in_lookup(dentry)) {
2063 found = d_alloc_parallel(dentry->d_parent, name,
2064 dentry->d_wait);
2065 if (IS_ERR(found) || !d_in_lookup(found)) {
2066 iput(inode);
2067 return found;
2068 }
2069 } else {
2070 found = d_alloc(dentry->d_parent, name);
2071 if (!found) {
2072 iput(inode);
2073 return ERR_PTR(-ENOMEM);
2074 }
2075 }
2076 res = d_splice_alias(inode, found);
2077 if (res) {
2078 dput(found);
2079 return res;
2080 }
Al Viro4f522a22013-02-11 23:20:37 -05002081 return found;
Barry Naujok94035402008-05-21 16:50:46 +10002082}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002083EXPORT_SYMBOL(d_add_ci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
Linus Torvalds12f8ad42012-05-04 14:59:14 -07002085
Al Virod4c91a82016-06-25 23:33:49 -04002086static inline bool d_same_name(const struct dentry *dentry,
2087 const struct dentry *parent,
2088 const struct qstr *name)
Linus Torvalds12f8ad42012-05-04 14:59:14 -07002089{
Al Virod4c91a82016-06-25 23:33:49 -04002090 if (likely(!(parent->d_flags & DCACHE_OP_COMPARE))) {
2091 if (dentry->d_name.len != name->len)
2092 return false;
2093 return dentry_cmp(dentry, name->name, name->len) == 0;
Linus Torvalds12f8ad42012-05-04 14:59:14 -07002094 }
Al Viro6fa67e72016-07-31 16:37:25 -04002095 return parent->d_op->d_compare(dentry,
Al Virod4c91a82016-06-25 23:33:49 -04002096 dentry->d_name.len, dentry->d_name.name,
2097 name) == 0;
Linus Torvalds12f8ad42012-05-04 14:59:14 -07002098}
2099
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100/**
Nick Piggin31e6b012011-01-07 17:49:52 +11002101 * __d_lookup_rcu - search for a dentry (racy, store-free)
2102 * @parent: parent dentry
2103 * @name: qstr of name we wish to find
Randy Dunlap1f1e6e52012-03-18 21:23:05 -07002104 * @seqp: returns d_seq value at the point where the dentry was found
Nick Piggin31e6b012011-01-07 17:49:52 +11002105 * Returns: dentry, or NULL
2106 *
2107 * __d_lookup_rcu is the dcache lookup function for rcu-walk name
2108 * resolution (store-free path walking) design described in
2109 * Documentation/filesystems/path-lookup.txt.
2110 *
2111 * This is not to be used outside core vfs.
2112 *
2113 * __d_lookup_rcu must only be used in rcu-walk mode, ie. with vfsmount lock
2114 * held, and rcu_read_lock held. The returned dentry must not be stored into
2115 * without taking d_lock and checking d_seq sequence count against @seq
2116 * returned here.
2117 *
Linus Torvalds15570082013-09-02 11:38:06 -07002118 * A refcount may be taken on the found dentry with the d_rcu_to_refcount
Nick Piggin31e6b012011-01-07 17:49:52 +11002119 * function.
2120 *
2121 * Alternatively, __d_lookup_rcu may be called again to look up the child of
2122 * the returned dentry, so long as its parent's seqlock is checked after the
2123 * child is looked up. Thus, an interlocking stepping of sequence lock checks
2124 * is formed, giving integrity down the path walk.
Linus Torvalds12f8ad42012-05-04 14:59:14 -07002125 *
2126 * NOTE! The caller *has* to check the resulting dentry against the sequence
2127 * number we've returned before using any of the resulting dentry state!
Nick Piggin31e6b012011-01-07 17:49:52 +11002128 */
Linus Torvalds8966be92012-03-02 14:23:30 -08002129struct dentry *__d_lookup_rcu(const struct dentry *parent,
2130 const struct qstr *name,
Linus Torvaldsda53be12013-05-21 15:22:44 -07002131 unsigned *seqp)
Nick Piggin31e6b012011-01-07 17:49:52 +11002132{
Linus Torvalds26fe5752012-05-10 13:14:12 -07002133 u64 hashlen = name->hash_len;
Nick Piggin31e6b012011-01-07 17:49:52 +11002134 const unsigned char *str = name->name;
Linus Torvalds8387ff22016-06-10 07:51:30 -07002135 struct hlist_bl_head *b = d_hash(hashlen_hash(hashlen));
Nick Pigginceb5bdc2011-01-07 17:50:05 +11002136 struct hlist_bl_node *node;
Nick Piggin31e6b012011-01-07 17:49:52 +11002137 struct dentry *dentry;
2138
2139 /*
2140 * Note: There is significant duplication with __d_lookup_rcu which is
2141 * required to prevent single threaded performance regressions
2142 * especially on architectures where smp_rmb (in seqcounts) are costly.
2143 * Keep the two functions in sync.
2144 */
2145
2146 /*
2147 * The hash list is protected using RCU.
2148 *
2149 * Carefully use d_seq when comparing a candidate dentry, to avoid
2150 * races with d_move().
2151 *
2152 * It is possible that concurrent renames can mess up our list
2153 * walk here and result in missing our dentry, resulting in the
2154 * false-negative result. d_lookup() protects against concurrent
2155 * renames using rename_lock seqlock.
2156 *
Namhyung Kimb0a4bb82011-01-22 15:31:32 +09002157 * See Documentation/filesystems/path-lookup.txt for more details.
Nick Piggin31e6b012011-01-07 17:49:52 +11002158 */
Linus Torvaldsb07ad992011-04-23 22:32:03 -07002159 hlist_bl_for_each_entry_rcu(dentry, node, b, d_hash) {
Linus Torvalds8966be92012-03-02 14:23:30 -08002160 unsigned seq;
Nick Piggin31e6b012011-01-07 17:49:52 +11002161
Nick Piggin31e6b012011-01-07 17:49:52 +11002162seqretry:
Linus Torvalds12f8ad42012-05-04 14:59:14 -07002163 /*
2164 * The dentry sequence count protects us from concurrent
Linus Torvaldsda53be12013-05-21 15:22:44 -07002165 * renames, and thus protects parent and name fields.
Linus Torvalds12f8ad42012-05-04 14:59:14 -07002166 *
2167 * The caller must perform a seqcount check in order
Linus Torvaldsda53be12013-05-21 15:22:44 -07002168 * to do anything useful with the returned dentry.
Linus Torvalds12f8ad42012-05-04 14:59:14 -07002169 *
2170 * NOTE! We do a "raw" seqcount_begin here. That means that
2171 * we don't wait for the sequence count to stabilize if it
2172 * is in the middle of a sequence change. If we do the slow
2173 * dentry compare, we will do seqretries until it is stable,
2174 * and if we end up with a successful lookup, we actually
2175 * want to exit RCU lookup anyway.
Al Virod4c91a82016-06-25 23:33:49 -04002176 *
2177 * Note that raw_seqcount_begin still *does* smp_rmb(), so
2178 * we are still guaranteed NUL-termination of ->d_name.name.
Linus Torvalds12f8ad42012-05-04 14:59:14 -07002179 */
2180 seq = raw_seqcount_begin(&dentry->d_seq);
Nick Piggin31e6b012011-01-07 17:49:52 +11002181 if (dentry->d_parent != parent)
2182 continue;
Linus Torvalds2e321802012-05-21 18:48:10 -07002183 if (d_unhashed(dentry))
2184 continue;
Linus Torvalds12f8ad42012-05-04 14:59:14 -07002185
2186 if (unlikely(parent->d_flags & DCACHE_OP_COMPARE)) {
Al Virod4c91a82016-06-25 23:33:49 -04002187 int tlen;
2188 const char *tname;
Linus Torvalds26fe5752012-05-10 13:14:12 -07002189 if (dentry->d_name.hash != hashlen_hash(hashlen))
2190 continue;
Al Virod4c91a82016-06-25 23:33:49 -04002191 tlen = dentry->d_name.len;
2192 tname = dentry->d_name.name;
2193 /* we want a consistent (name,len) pair */
2194 if (read_seqcount_retry(&dentry->d_seq, seq)) {
2195 cpu_relax();
Linus Torvalds12f8ad42012-05-04 14:59:14 -07002196 goto seqretry;
2197 }
Al Viro6fa67e72016-07-31 16:37:25 -04002198 if (parent->d_op->d_compare(dentry,
Al Virod4c91a82016-06-25 23:33:49 -04002199 tlen, tname, name) != 0)
2200 continue;
2201 } else {
2202 if (dentry->d_name.hash_len != hashlen)
2203 continue;
2204 if (dentry_cmp(dentry, str, hashlen_len(hashlen)) != 0)
2205 continue;
Linus Torvalds12f8ad42012-05-04 14:59:14 -07002206 }
Linus Torvaldsda53be12013-05-21 15:22:44 -07002207 *seqp = seq;
Al Virod4c91a82016-06-25 23:33:49 -04002208 return dentry;
Nick Piggin31e6b012011-01-07 17:49:52 +11002209 }
2210 return NULL;
2211}
2212
2213/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 * d_lookup - search for a dentry
2215 * @parent: parent dentry
2216 * @name: qstr of name we wish to find
Nick Pigginb04f7842010-08-18 04:37:34 +10002217 * Returns: dentry, or NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 *
Nick Pigginb04f7842010-08-18 04:37:34 +10002219 * d_lookup searches the children of the parent dentry for the name in
2220 * question. If the dentry is found its reference count is incremented and the
2221 * dentry is returned. The caller must use dput to free the entry when it has
2222 * finished using it. %NULL is returned if the dentry does not exist.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 */
Al Viroda2d8452013-01-24 18:29:34 -05002224struct dentry *d_lookup(const struct dentry *parent, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225{
Nick Piggin31e6b012011-01-07 17:49:52 +11002226 struct dentry *dentry;
Nick Piggin949854d2011-01-07 17:49:37 +11002227 unsigned seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
Daeseok Younb8314f92014-08-11 11:46:53 +09002229 do {
2230 seq = read_seqbegin(&rename_lock);
2231 dentry = __d_lookup(parent, name);
2232 if (dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 break;
2234 } while (read_seqretry(&rename_lock, seq));
2235 return dentry;
2236}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002237EXPORT_SYMBOL(d_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238
Nick Piggin31e6b012011-01-07 17:49:52 +11002239/**
Nick Pigginb04f7842010-08-18 04:37:34 +10002240 * __d_lookup - search for a dentry (racy)
2241 * @parent: parent dentry
2242 * @name: qstr of name we wish to find
2243 * Returns: dentry, or NULL
2244 *
2245 * __d_lookup is like d_lookup, however it may (rarely) return a
2246 * false-negative result due to unrelated rename activity.
2247 *
2248 * __d_lookup is slightly faster by avoiding rename_lock read seqlock,
2249 * however it must be used carefully, eg. with a following d_lookup in
2250 * the case of failure.
2251 *
2252 * __d_lookup callers must be commented.
2253 */
Al Viroa713ca22013-01-24 18:27:00 -05002254struct dentry *__d_lookup(const struct dentry *parent, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 unsigned int hash = name->hash;
Linus Torvalds8387ff22016-06-10 07:51:30 -07002257 struct hlist_bl_head *b = d_hash(hash);
Nick Pigginceb5bdc2011-01-07 17:50:05 +11002258 struct hlist_bl_node *node;
Nick Piggin31e6b012011-01-07 17:49:52 +11002259 struct dentry *found = NULL;
Paul E. McKenney665a7582005-11-07 00:59:17 -08002260 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261
Nick Pigginb04f7842010-08-18 04:37:34 +10002262 /*
Nick Piggin31e6b012011-01-07 17:49:52 +11002263 * Note: There is significant duplication with __d_lookup_rcu which is
2264 * required to prevent single threaded performance regressions
2265 * especially on architectures where smp_rmb (in seqcounts) are costly.
2266 * Keep the two functions in sync.
2267 */
2268
2269 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10002270 * The hash list is protected using RCU.
2271 *
2272 * Take d_lock when comparing a candidate dentry, to avoid races
2273 * with d_move().
2274 *
2275 * It is possible that concurrent renames can mess up our list
2276 * walk here and result in missing our dentry, resulting in the
2277 * false-negative result. d_lookup() protects against concurrent
2278 * renames using rename_lock seqlock.
2279 *
Namhyung Kimb0a4bb82011-01-22 15:31:32 +09002280 * See Documentation/filesystems/path-lookup.txt for more details.
Nick Pigginb04f7842010-08-18 04:37:34 +10002281 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 rcu_read_lock();
2283
Linus Torvaldsb07ad992011-04-23 22:32:03 -07002284 hlist_bl_for_each_entry_rcu(dentry, node, b, d_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 if (dentry->d_name.hash != hash)
2287 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
2289 spin_lock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 if (dentry->d_parent != parent)
2291 goto next;
Linus Torvaldsd0185c02008-09-29 07:42:57 -07002292 if (d_unhashed(dentry))
2293 goto next;
2294
Al Virod4c91a82016-06-25 23:33:49 -04002295 if (!d_same_name(dentry, parent, name))
2296 goto next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
Waiman Long98474232013-08-28 18:24:59 -07002298 dentry->d_lockref.count++;
Linus Torvaldsd0185c02008-09-29 07:42:57 -07002299 found = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 spin_unlock(&dentry->d_lock);
2301 break;
2302next:
2303 spin_unlock(&dentry->d_lock);
2304 }
2305 rcu_read_unlock();
2306
2307 return found;
2308}
2309
2310/**
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08002311 * d_hash_and_lookup - hash the qstr then search for a dentry
2312 * @dir: Directory to search in
2313 * @name: qstr of name we wish to find
2314 *
Al Viro4f522a22013-02-11 23:20:37 -05002315 * On lookup failure NULL is returned; on bad name - ERR_PTR(-error)
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08002316 */
2317struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name)
2318{
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08002319 /*
2320 * Check for a fs-specific hash function. Note that we must
2321 * calculate the standard hash first, as the d_op->d_hash()
2322 * routine may choose to leave the hash value unchanged.
2323 */
Linus Torvalds8387ff22016-06-10 07:51:30 -07002324 name->hash = full_name_hash(dir, name->name, name->len);
Nick Pigginfb045ad2011-01-07 17:49:55 +11002325 if (dir->d_flags & DCACHE_OP_HASH) {
Linus Torvaldsda53be12013-05-21 15:22:44 -07002326 int err = dir->d_op->d_hash(dir, name);
Al Viro4f522a22013-02-11 23:20:37 -05002327 if (unlikely(err < 0))
2328 return ERR_PTR(err);
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08002329 }
Al Viro4f522a22013-02-11 23:20:37 -05002330 return d_lookup(dir, name);
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08002331}
Al Viro4f522a22013-02-11 23:20:37 -05002332EXPORT_SYMBOL(d_hash_and_lookup);
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08002333
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334/*
2335 * When a file is deleted, we have two options:
2336 * - turn this dentry into a negative dentry
2337 * - unhash this dentry and free it.
2338 *
2339 * Usually, we want to just turn this into
2340 * a negative dentry, but if anybody else is
2341 * currently using the dentry or the inode
2342 * we can't do that and we fall back on removing
2343 * it from the hash queues and waiting for
2344 * it to be deleted later when it has no users
2345 */
2346
2347/**
2348 * d_delete - delete a dentry
2349 * @dentry: The dentry to delete
2350 *
2351 * Turn the dentry into a negative dentry if possible, otherwise
2352 * remove it from the hash queues so it can be deleted later
2353 */
2354
2355void d_delete(struct dentry * dentry)
2356{
Nick Piggin873feea2011-01-07 17:50:06 +11002357 struct inode *inode;
John McCutchan7a91bf72005-08-08 13:52:16 -04002358 int isdir = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 /*
2360 * Are we the only user?
2361 */
Nick Piggin357f8e62011-01-07 17:49:42 +11002362again:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 spin_lock(&dentry->d_lock);
Nick Piggin873feea2011-01-07 17:50:06 +11002364 inode = dentry->d_inode;
2365 isdir = S_ISDIR(inode->i_mode);
Waiman Long98474232013-08-28 18:24:59 -07002366 if (dentry->d_lockref.count == 1) {
Alan Cox1fe0c022012-09-19 15:49:51 +01002367 if (!spin_trylock(&inode->i_lock)) {
Nick Piggin357f8e62011-01-07 17:49:42 +11002368 spin_unlock(&dentry->d_lock);
2369 cpu_relax();
2370 goto again;
2371 }
Al Viro13e3c5e2010-05-21 16:11:04 -04002372 dentry->d_flags &= ~DCACHE_CANT_MOUNT;
Nick Piggin31e6b012011-01-07 17:49:52 +11002373 dentry_unlink_inode(dentry);
John McCutchan7a91bf72005-08-08 13:52:16 -04002374 fsnotify_nameremove(dentry, isdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 return;
2376 }
2377
2378 if (!d_unhashed(dentry))
2379 __d_drop(dentry);
2380
2381 spin_unlock(&dentry->d_lock);
John McCutchan7a91bf72005-08-08 13:52:16 -04002382
2383 fsnotify_nameremove(dentry, isdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002385EXPORT_SYMBOL(d_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386
Al Viro15d3c582016-07-29 17:45:21 -04002387static void __d_rehash(struct dentry *entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388{
Al Viro15d3c582016-07-29 17:45:21 -04002389 struct hlist_bl_head *b = d_hash(entry->d_name.hash);
NeilBrown61647822017-11-10 15:45:41 +11002390
Christoph Hellwig1879fd62011-04-25 14:01:36 -04002391 hlist_bl_lock(b);
Linus Torvaldsb07ad992011-04-23 22:32:03 -07002392 hlist_bl_add_head_rcu(&entry->d_hash, b);
Christoph Hellwig1879fd62011-04-25 14:01:36 -04002393 hlist_bl_unlock(b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394}
2395
2396/**
2397 * d_rehash - add an entry back to the hash
2398 * @entry: dentry to add to the hash
2399 *
2400 * Adds a dentry to the hash according to its name.
2401 */
2402
2403void d_rehash(struct dentry * entry)
2404{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 spin_lock(&entry->d_lock);
Al Viro15d3c582016-07-29 17:45:21 -04002406 __d_rehash(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 spin_unlock(&entry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002409EXPORT_SYMBOL(d_rehash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410
Al Viro84e710d2016-04-15 00:58:55 -04002411static inline unsigned start_dir_add(struct inode *dir)
2412{
2413
2414 for (;;) {
2415 unsigned n = dir->i_dir_seq;
2416 if (!(n & 1) && cmpxchg(&dir->i_dir_seq, n, n + 1) == n)
2417 return n;
2418 cpu_relax();
2419 }
2420}
2421
2422static inline void end_dir_add(struct inode *dir, unsigned n)
2423{
2424 smp_store_release(&dir->i_dir_seq, n + 2);
2425}
2426
Al Virod9171b92016-04-15 03:33:13 -04002427static void d_wait_lookup(struct dentry *dentry)
2428{
2429 if (d_in_lookup(dentry)) {
2430 DECLARE_WAITQUEUE(wait, current);
2431 add_wait_queue(dentry->d_wait, &wait);
2432 do {
2433 set_current_state(TASK_UNINTERRUPTIBLE);
2434 spin_unlock(&dentry->d_lock);
2435 schedule();
2436 spin_lock(&dentry->d_lock);
2437 } while (d_in_lookup(dentry));
2438 }
2439}
2440
Al Viro94bdd652016-04-15 02:42:04 -04002441struct dentry *d_alloc_parallel(struct dentry *parent,
Al Virod9171b92016-04-15 03:33:13 -04002442 const struct qstr *name,
2443 wait_queue_head_t *wq)
Al Viro94bdd652016-04-15 02:42:04 -04002444{
Al Viro94bdd652016-04-15 02:42:04 -04002445 unsigned int hash = name->hash;
Al Viro94bdd652016-04-15 02:42:04 -04002446 struct hlist_bl_head *b = in_lookup_hash(parent, hash);
2447 struct hlist_bl_node *node;
2448 struct dentry *new = d_alloc(parent, name);
2449 struct dentry *dentry;
2450 unsigned seq, r_seq, d_seq;
2451
2452 if (unlikely(!new))
2453 return ERR_PTR(-ENOMEM);
2454
2455retry:
2456 rcu_read_lock();
2457 seq = smp_load_acquire(&parent->d_inode->i_dir_seq) & ~1;
2458 r_seq = read_seqbegin(&rename_lock);
2459 dentry = __d_lookup_rcu(parent, name, &d_seq);
2460 if (unlikely(dentry)) {
2461 if (!lockref_get_not_dead(&dentry->d_lockref)) {
2462 rcu_read_unlock();
2463 goto retry;
2464 }
2465 if (read_seqcount_retry(&dentry->d_seq, d_seq)) {
2466 rcu_read_unlock();
2467 dput(dentry);
2468 goto retry;
2469 }
2470 rcu_read_unlock();
2471 dput(new);
2472 return dentry;
2473 }
2474 if (unlikely(read_seqretry(&rename_lock, r_seq))) {
2475 rcu_read_unlock();
2476 goto retry;
2477 }
2478 hlist_bl_lock(b);
2479 if (unlikely(parent->d_inode->i_dir_seq != seq)) {
2480 hlist_bl_unlock(b);
2481 rcu_read_unlock();
2482 goto retry;
2483 }
Al Viro94bdd652016-04-15 02:42:04 -04002484 /*
2485 * No changes for the parent since the beginning of d_lookup().
2486 * Since all removals from the chain happen with hlist_bl_lock(),
2487 * any potential in-lookup matches are going to stay here until
2488 * we unlock the chain. All fields are stable in everything
2489 * we encounter.
2490 */
2491 hlist_bl_for_each_entry(dentry, node, b, d_u.d_in_lookup_hash) {
2492 if (dentry->d_name.hash != hash)
2493 continue;
2494 if (dentry->d_parent != parent)
2495 continue;
Al Virod4c91a82016-06-25 23:33:49 -04002496 if (!d_same_name(dentry, parent, name))
2497 continue;
Al Viro94bdd652016-04-15 02:42:04 -04002498 hlist_bl_unlock(b);
Al Viroe7d6ef92016-06-20 01:35:59 -04002499 /* now we can try to grab a reference */
2500 if (!lockref_get_not_dead(&dentry->d_lockref)) {
2501 rcu_read_unlock();
2502 goto retry;
2503 }
2504
2505 rcu_read_unlock();
2506 /*
2507 * somebody is likely to be still doing lookup for it;
2508 * wait for them to finish
2509 */
Al Virod9171b92016-04-15 03:33:13 -04002510 spin_lock(&dentry->d_lock);
2511 d_wait_lookup(dentry);
2512 /*
2513 * it's not in-lookup anymore; in principle we should repeat
2514 * everything from dcache lookup, but it's likely to be what
2515 * d_lookup() would've found anyway. If it is, just return it;
2516 * otherwise we really have to repeat the whole thing.
2517 */
2518 if (unlikely(dentry->d_name.hash != hash))
2519 goto mismatch;
2520 if (unlikely(dentry->d_parent != parent))
2521 goto mismatch;
2522 if (unlikely(d_unhashed(dentry)))
2523 goto mismatch;
Al Virod4c91a82016-06-25 23:33:49 -04002524 if (unlikely(!d_same_name(dentry, parent, name)))
2525 goto mismatch;
Al Virod9171b92016-04-15 03:33:13 -04002526 /* OK, it *is* a hashed match; return it */
2527 spin_unlock(&dentry->d_lock);
Al Viro94bdd652016-04-15 02:42:04 -04002528 dput(new);
2529 return dentry;
2530 }
Al Viroe7d6ef92016-06-20 01:35:59 -04002531 rcu_read_unlock();
Al Viro94bdd652016-04-15 02:42:04 -04002532 /* we can't take ->d_lock here; it's OK, though. */
2533 new->d_flags |= DCACHE_PAR_LOOKUP;
Al Virod9171b92016-04-15 03:33:13 -04002534 new->d_wait = wq;
Al Viro94bdd652016-04-15 02:42:04 -04002535 hlist_bl_add_head_rcu(&new->d_u.d_in_lookup_hash, b);
2536 hlist_bl_unlock(b);
2537 return new;
Al Virod9171b92016-04-15 03:33:13 -04002538mismatch:
2539 spin_unlock(&dentry->d_lock);
2540 dput(dentry);
2541 goto retry;
Al Viro94bdd652016-04-15 02:42:04 -04002542}
2543EXPORT_SYMBOL(d_alloc_parallel);
2544
Al Viro85c7f812016-04-14 19:52:13 -04002545void __d_lookup_done(struct dentry *dentry)
2546{
Al Viro94bdd652016-04-15 02:42:04 -04002547 struct hlist_bl_head *b = in_lookup_hash(dentry->d_parent,
2548 dentry->d_name.hash);
2549 hlist_bl_lock(b);
Al Viro85c7f812016-04-14 19:52:13 -04002550 dentry->d_flags &= ~DCACHE_PAR_LOOKUP;
Al Viro94bdd652016-04-15 02:42:04 -04002551 __hlist_bl_del(&dentry->d_u.d_in_lookup_hash);
Al Virod9171b92016-04-15 03:33:13 -04002552 wake_up_all(dentry->d_wait);
2553 dentry->d_wait = NULL;
Al Viro94bdd652016-04-15 02:42:04 -04002554 hlist_bl_unlock(b);
2555 INIT_HLIST_NODE(&dentry->d_u.d_alias);
Al Virod9171b92016-04-15 03:33:13 -04002556 INIT_LIST_HEAD(&dentry->d_lru);
Al Viro85c7f812016-04-14 19:52:13 -04002557}
2558EXPORT_SYMBOL(__d_lookup_done);
Al Viroed782b52016-03-09 19:52:39 -05002559
2560/* inode->i_lock held if inode is non-NULL */
2561
2562static inline void __d_add(struct dentry *dentry, struct inode *inode)
2563{
Al Viro84e710d2016-04-15 00:58:55 -04002564 struct inode *dir = NULL;
2565 unsigned n;
Al Viro0568d702016-04-14 19:40:56 -04002566 spin_lock(&dentry->d_lock);
Al Viro84e710d2016-04-15 00:58:55 -04002567 if (unlikely(d_in_lookup(dentry))) {
2568 dir = dentry->d_parent->d_inode;
2569 n = start_dir_add(dir);
Al Viro85c7f812016-04-14 19:52:13 -04002570 __d_lookup_done(dentry);
Al Viro84e710d2016-04-15 00:58:55 -04002571 }
Al Viroed782b52016-03-09 19:52:39 -05002572 if (inode) {
Al Viro0568d702016-04-14 19:40:56 -04002573 unsigned add_flags = d_flags_for_inode(inode);
2574 hlist_add_head(&dentry->d_u.d_alias, &inode->i_dentry);
2575 raw_write_seqcount_begin(&dentry->d_seq);
2576 __d_set_inode_and_type(dentry, inode, add_flags);
2577 raw_write_seqcount_end(&dentry->d_seq);
Al Viroaffda482016-05-29 18:35:12 -04002578 fsnotify_update_flags(dentry);
Al Viroed782b52016-03-09 19:52:39 -05002579 }
Al Viro15d3c582016-07-29 17:45:21 -04002580 __d_rehash(dentry);
Al Viro84e710d2016-04-15 00:58:55 -04002581 if (dir)
2582 end_dir_add(dir, n);
Al Viro0568d702016-04-14 19:40:56 -04002583 spin_unlock(&dentry->d_lock);
2584 if (inode)
2585 spin_unlock(&inode->i_lock);
Al Viroed782b52016-03-09 19:52:39 -05002586}
2587
Nick Pigginfb2d5b82011-01-07 17:49:26 +11002588/**
Al Viro34d0d192016-03-08 21:01:03 -05002589 * d_add - add dentry to hash queues
2590 * @entry: dentry to add
2591 * @inode: The inode to attach to this dentry
2592 *
2593 * This adds the entry to the hash queues and initializes @inode.
2594 * The entry was actually filled in earlier during d_alloc().
2595 */
2596
2597void d_add(struct dentry *entry, struct inode *inode)
2598{
Al Virob9680912016-04-11 00:53:26 -04002599 if (inode) {
2600 security_d_instantiate(entry, inode);
Al Viroed782b52016-03-09 19:52:39 -05002601 spin_lock(&inode->i_lock);
Al Virob9680912016-04-11 00:53:26 -04002602 }
Al Viroed782b52016-03-09 19:52:39 -05002603 __d_add(entry, inode);
Al Viro34d0d192016-03-08 21:01:03 -05002604}
2605EXPORT_SYMBOL(d_add);
2606
2607/**
Al Viro668d0cd2016-03-08 12:44:17 -05002608 * d_exact_alias - find and hash an exact unhashed alias
2609 * @entry: dentry to add
2610 * @inode: The inode to go with this dentry
2611 *
2612 * If an unhashed dentry with the same name/parent and desired
2613 * inode already exists, hash and return it. Otherwise, return
2614 * NULL.
2615 *
2616 * Parent directory should be locked.
2617 */
2618struct dentry *d_exact_alias(struct dentry *entry, struct inode *inode)
2619{
2620 struct dentry *alias;
Al Viro668d0cd2016-03-08 12:44:17 -05002621 unsigned int hash = entry->d_name.hash;
2622
2623 spin_lock(&inode->i_lock);
2624 hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
2625 /*
2626 * Don't need alias->d_lock here, because aliases with
2627 * d_parent == entry->d_parent are not subject to name or
2628 * parent changes, because the parent inode i_mutex is held.
2629 */
2630 if (alias->d_name.hash != hash)
2631 continue;
2632 if (alias->d_parent != entry->d_parent)
2633 continue;
Al Virod4c91a82016-06-25 23:33:49 -04002634 if (!d_same_name(alias, entry->d_parent, &entry->d_name))
Al Viro668d0cd2016-03-08 12:44:17 -05002635 continue;
2636 spin_lock(&alias->d_lock);
2637 if (!d_unhashed(alias)) {
2638 spin_unlock(&alias->d_lock);
2639 alias = NULL;
2640 } else {
2641 __dget_dlock(alias);
Al Viro15d3c582016-07-29 17:45:21 -04002642 __d_rehash(alias);
Al Viro668d0cd2016-03-08 12:44:17 -05002643 spin_unlock(&alias->d_lock);
2644 }
2645 spin_unlock(&inode->i_lock);
2646 return alias;
2647 }
2648 spin_unlock(&inode->i_lock);
2649 return NULL;
2650}
2651EXPORT_SYMBOL(d_exact_alias);
2652
2653/**
Nick Pigginfb2d5b82011-01-07 17:49:26 +11002654 * dentry_update_name_case - update case insensitive dentry with a new name
2655 * @dentry: dentry to be updated
2656 * @name: new name
2657 *
2658 * Update a case insensitive dentry with new case of name.
2659 *
2660 * dentry must have been returned by d_lookup with name @name. Old and new
2661 * name lengths must match (ie. no d_compare which allows mismatched name
2662 * lengths).
2663 *
2664 * Parent inode i_mutex must be held over d_lookup and into this call (to
2665 * keep renames and concurrent inserts, and readdir(2) away).
2666 */
Al Viro9aba36d2016-07-20 22:28:45 -04002667void dentry_update_name_case(struct dentry *dentry, const struct qstr *name)
Nick Pigginfb2d5b82011-01-07 17:49:26 +11002668{
Al Viro59551022016-01-22 15:40:57 -05002669 BUG_ON(!inode_is_locked(dentry->d_parent->d_inode));
Nick Pigginfb2d5b82011-01-07 17:49:26 +11002670 BUG_ON(dentry->d_name.len != name->len); /* d_lookup gives this */
2671
Nick Pigginfb2d5b82011-01-07 17:49:26 +11002672 spin_lock(&dentry->d_lock);
Nick Piggin31e6b012011-01-07 17:49:52 +11002673 write_seqcount_begin(&dentry->d_seq);
Nick Pigginfb2d5b82011-01-07 17:49:26 +11002674 memcpy((unsigned char *)dentry->d_name.name, name->name, name->len);
Nick Piggin31e6b012011-01-07 17:49:52 +11002675 write_seqcount_end(&dentry->d_seq);
Nick Pigginfb2d5b82011-01-07 17:49:26 +11002676 spin_unlock(&dentry->d_lock);
Nick Pigginfb2d5b82011-01-07 17:49:26 +11002677}
2678EXPORT_SYMBOL(dentry_update_name_case);
2679
Al Viro8d85b482014-09-29 14:54:27 -04002680static void swap_names(struct dentry *dentry, struct dentry *target)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681{
Al Viro8d85b482014-09-29 14:54:27 -04002682 if (unlikely(dname_external(target))) {
2683 if (unlikely(dname_external(dentry))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 /*
2685 * Both external: swap the pointers
2686 */
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08002687 swap(target->d_name.name, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 } else {
2689 /*
2690 * dentry:internal, target:external. Steal target's
2691 * storage and make target internal.
2692 */
J. Bruce Fields321bcf92007-10-21 16:41:38 -07002693 memcpy(target->d_iname, dentry->d_name.name,
2694 dentry->d_name.len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 dentry->d_name.name = target->d_name.name;
2696 target->d_name.name = target->d_iname;
2697 }
2698 } else {
Al Viro8d85b482014-09-29 14:54:27 -04002699 if (unlikely(dname_external(dentry))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 /*
2701 * dentry:external, target:internal. Give dentry's
2702 * storage to target and make dentry internal
2703 */
2704 memcpy(dentry->d_iname, target->d_name.name,
2705 target->d_name.len + 1);
2706 target->d_name.name = dentry->d_name.name;
2707 dentry->d_name.name = dentry->d_iname;
2708 } else {
2709 /*
Miklos Szeredida1ce062014-04-01 17:08:43 +02002710 * Both are internal.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 */
Miklos Szeredida1ce062014-04-01 17:08:43 +02002712 unsigned int i;
2713 BUILD_BUG_ON(!IS_ALIGNED(DNAME_INLINE_LEN, sizeof(long)));
2714 for (i = 0; i < DNAME_INLINE_LEN / sizeof(long); i++) {
2715 swap(((long *) &dentry->d_iname)[i],
2716 ((long *) &target->d_iname)[i]);
2717 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 }
2719 }
Linus Torvaldsa28ddb82014-09-24 12:27:39 -07002720 swap(dentry->d_name.hash_len, target->d_name.hash_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721}
2722
Al Viro8d85b482014-09-29 14:54:27 -04002723static void copy_name(struct dentry *dentry, struct dentry *target)
2724{
2725 struct external_name *old_name = NULL;
2726 if (unlikely(dname_external(dentry)))
2727 old_name = external_name(dentry);
2728 if (unlikely(dname_external(target))) {
2729 atomic_inc(&external_name(target)->u.count);
2730 dentry->d_name = target->d_name;
2731 } else {
2732 memcpy(dentry->d_iname, target->d_name.name,
2733 target->d_name.len + 1);
2734 dentry->d_name.name = dentry->d_iname;
2735 dentry->d_name.hash_len = target->d_name.hash_len;
2736 }
2737 if (old_name && likely(atomic_dec_and_test(&old_name->u.count)))
2738 kfree_rcu(old_name, u.head);
2739}
2740
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002741static void dentry_lock_for_move(struct dentry *dentry, struct dentry *target)
2742{
2743 /*
2744 * XXXX: do we really need to take target->d_lock?
2745 */
2746 if (IS_ROOT(dentry) || dentry->d_parent == target->d_parent)
2747 spin_lock(&target->d_parent->d_lock);
2748 else {
2749 if (d_ancestor(dentry->d_parent, target->d_parent)) {
2750 spin_lock(&dentry->d_parent->d_lock);
2751 spin_lock_nested(&target->d_parent->d_lock,
2752 DENTRY_D_LOCK_NESTED);
2753 } else {
2754 spin_lock(&target->d_parent->d_lock);
2755 spin_lock_nested(&dentry->d_parent->d_lock,
2756 DENTRY_D_LOCK_NESTED);
2757 }
2758 }
2759 if (target < dentry) {
2760 spin_lock_nested(&target->d_lock, 2);
2761 spin_lock_nested(&dentry->d_lock, 3);
2762 } else {
2763 spin_lock_nested(&dentry->d_lock, 2);
2764 spin_lock_nested(&target->d_lock, 3);
2765 }
2766}
2767
Al Viro986c0192014-09-26 23:11:15 -04002768static void dentry_unlock_for_move(struct dentry *dentry, struct dentry *target)
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002769{
2770 if (target->d_parent != dentry->d_parent)
2771 spin_unlock(&dentry->d_parent->d_lock);
2772 if (target->d_parent != target)
2773 spin_unlock(&target->d_parent->d_lock);
Al Viro986c0192014-09-26 23:11:15 -04002774 spin_unlock(&target->d_lock);
2775 spin_unlock(&dentry->d_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002776}
2777
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778/*
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002779 * When switching names, the actual string doesn't strictly have to
2780 * be preserved in the target - because we're dropping the target
2781 * anyway. As such, we can just do a simple memcpy() to copy over
Mikhail Efremovd2fa4a82014-09-24 22:14:33 +04002782 * the new name before we switch, unless we are going to rehash
2783 * it. Note that if we *do* unhash the target, we are not allowed
2784 * to rehash it without giving it a new name/hash key - whether
2785 * we swap or overwrite the names here, resulting name won't match
2786 * the reality in filesystem; it's only there for d_path() purposes.
2787 * Note that all of this is happening under rename_lock, so the
2788 * any hash lookup seeing it in the middle of manipulations will
2789 * be discarded anyway. So we do not care what happens to the hash
2790 * key in that case.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 */
Trond Myklebust9eaef272006-10-21 10:24:20 -07002792/*
Al Viro18367502011-07-12 21:42:24 -04002793 * __d_move - move a dentry
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 * @dentry: entry to move
2795 * @target: new dentry
Miklos Szeredida1ce062014-04-01 17:08:43 +02002796 * @exchange: exchange the two dentries
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 *
2798 * Update the dcache to reflect the move of a file name. Negative
Jeff Laytonc46c8872011-07-26 13:33:16 -04002799 * dcache entries should not be moved in this way. Caller must hold
2800 * rename_lock, the i_mutex of the source and target directories,
2801 * and the sb->s_vfs_rename_mutex if they differ. See lock_rename().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 */
Miklos Szeredida1ce062014-04-01 17:08:43 +02002803static void __d_move(struct dentry *dentry, struct dentry *target,
2804 bool exchange)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805{
Al Viro84e710d2016-04-15 00:58:55 -04002806 struct inode *dir = NULL;
2807 unsigned n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 if (!dentry->d_inode)
2809 printk(KERN_WARNING "VFS: moving negative dcache entry\n");
2810
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002811 BUG_ON(d_ancestor(dentry, target));
2812 BUG_ON(d_ancestor(target, dentry));
2813
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002814 dentry_lock_for_move(dentry, target);
Al Viro84e710d2016-04-15 00:58:55 -04002815 if (unlikely(d_in_lookup(target))) {
2816 dir = target->d_parent->d_inode;
2817 n = start_dir_add(dir);
Al Viro85c7f812016-04-14 19:52:13 -04002818 __d_lookup_done(target);
Al Viro84e710d2016-04-15 00:58:55 -04002819 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820
Nick Piggin31e6b012011-01-07 17:49:52 +11002821 write_seqcount_begin(&dentry->d_seq);
John Stultz1ca7d672013-10-07 15:51:59 -07002822 write_seqcount_begin_nested(&target->d_seq, DENTRY_D_LOCK_NESTED);
Nick Piggin31e6b012011-01-07 17:49:52 +11002823
Al Viro15d3c582016-07-29 17:45:21 -04002824 /* unhash both */
NeilBrown61647822017-11-10 15:45:41 +11002825 /* ___d_drop does write_seqcount_barrier, but they're OK to nest. */
2826 ___d_drop(dentry);
2827 ___d_drop(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 /* Switch the names.. */
Al Viro8d85b482014-09-29 14:54:27 -04002830 if (exchange)
2831 swap_names(dentry, target);
2832 else
2833 copy_name(dentry, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834
Al Viro15d3c582016-07-29 17:45:21 -04002835 /* rehash in new place(s) */
2836 __d_rehash(dentry);
2837 if (exchange)
2838 __d_rehash(target);
NeilBrown61647822017-11-10 15:45:41 +11002839 else
2840 target->d_hash.pprev = NULL;
Al Viro15d3c582016-07-29 17:45:21 -04002841
Al Viro63cf4272014-09-26 23:06:14 -04002842 /* ... and switch them in the tree */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 if (IS_ROOT(dentry)) {
Al Viro63cf4272014-09-26 23:06:14 -04002844 /* splicing a tree */
Al Viro3d56c252016-06-07 21:26:55 -04002845 dentry->d_flags |= DCACHE_RCUACCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 dentry->d_parent = target->d_parent;
2847 target->d_parent = target;
Al Viro946e51f2014-10-26 19:19:16 -04002848 list_del_init(&target->d_child);
2849 list_move(&dentry->d_child, &dentry->d_parent->d_subdirs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 } else {
Al Viro63cf4272014-09-26 23:06:14 -04002851 /* swapping two dentries */
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08002852 swap(dentry->d_parent, target->d_parent);
Al Viro946e51f2014-10-26 19:19:16 -04002853 list_move(&target->d_child, &target->d_parent->d_subdirs);
2854 list_move(&dentry->d_child, &dentry->d_parent->d_subdirs);
Al Viro63cf4272014-09-26 23:06:14 -04002855 if (exchange)
Al Viroaffda482016-05-29 18:35:12 -04002856 fsnotify_update_flags(target);
2857 fsnotify_update_flags(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 }
2859
Nick Piggin31e6b012011-01-07 17:49:52 +11002860 write_seqcount_end(&target->d_seq);
2861 write_seqcount_end(&dentry->d_seq);
2862
Al Viro84e710d2016-04-15 00:58:55 -04002863 if (dir)
2864 end_dir_add(dir, n);
Al Viro986c0192014-09-26 23:11:15 -04002865 dentry_unlock_for_move(dentry, target);
Al Viro18367502011-07-12 21:42:24 -04002866}
2867
2868/*
2869 * d_move - move a dentry
2870 * @dentry: entry to move
2871 * @target: new dentry
2872 *
2873 * Update the dcache to reflect the move of a file name. Negative
Jeff Laytonc46c8872011-07-26 13:33:16 -04002874 * dcache entries should not be moved in this way. See the locking
2875 * requirements for __d_move.
Al Viro18367502011-07-12 21:42:24 -04002876 */
2877void d_move(struct dentry *dentry, struct dentry *target)
2878{
2879 write_seqlock(&rename_lock);
Miklos Szeredida1ce062014-04-01 17:08:43 +02002880 __d_move(dentry, target, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 write_sequnlock(&rename_lock);
Trond Myklebust9eaef272006-10-21 10:24:20 -07002882}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002883EXPORT_SYMBOL(d_move);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884
Miklos Szeredida1ce062014-04-01 17:08:43 +02002885/*
2886 * d_exchange - exchange two dentries
2887 * @dentry1: first dentry
2888 * @dentry2: second dentry
2889 */
2890void d_exchange(struct dentry *dentry1, struct dentry *dentry2)
2891{
2892 write_seqlock(&rename_lock);
2893
2894 WARN_ON(!dentry1->d_inode);
2895 WARN_ON(!dentry2->d_inode);
2896 WARN_ON(IS_ROOT(dentry1));
2897 WARN_ON(IS_ROOT(dentry2));
2898
2899 __d_move(dentry1, dentry2, true);
2900
2901 write_sequnlock(&rename_lock);
2902}
2903
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002904/**
2905 * d_ancestor - search for an ancestor
2906 * @p1: ancestor dentry
2907 * @p2: child dentry
2908 *
2909 * Returns the ancestor dentry of p2 which is a child of p1, if p1 is
2910 * an ancestor of p2, else NULL.
Trond Myklebust9eaef272006-10-21 10:24:20 -07002911 */
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002912struct dentry *d_ancestor(struct dentry *p1, struct dentry *p2)
Trond Myklebust9eaef272006-10-21 10:24:20 -07002913{
2914 struct dentry *p;
2915
OGAWA Hirofumi871c0062008-10-16 07:50:27 +09002916 for (p = p2; !IS_ROOT(p); p = p->d_parent) {
Trond Myklebust9eaef272006-10-21 10:24:20 -07002917 if (p->d_parent == p1)
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002918 return p;
Trond Myklebust9eaef272006-10-21 10:24:20 -07002919 }
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002920 return NULL;
Trond Myklebust9eaef272006-10-21 10:24:20 -07002921}
2922
2923/*
2924 * This helper attempts to cope with remotely renamed directories
2925 *
2926 * It assumes that the caller is already holding
Eric W. Biedermana03e2832015-08-15 13:36:41 -05002927 * dentry->d_parent->d_inode->i_mutex, and rename_lock
Trond Myklebust9eaef272006-10-21 10:24:20 -07002928 *
2929 * Note: If ever the locking in lock_rename() changes, then please
2930 * remember to update this too...
Trond Myklebust9eaef272006-10-21 10:24:20 -07002931 */
Al Virob5ae6b12014-10-12 22:16:02 -04002932static int __d_unalias(struct inode *inode,
Nick Piggin873feea2011-01-07 17:50:06 +11002933 struct dentry *dentry, struct dentry *alias)
Trond Myklebust9eaef272006-10-21 10:24:20 -07002934{
Al Viro9902af72016-04-15 15:08:36 -04002935 struct mutex *m1 = NULL;
2936 struct rw_semaphore *m2 = NULL;
J. Bruce Fields3d330dc2015-02-10 10:55:53 -05002937 int ret = -ESTALE;
Trond Myklebust9eaef272006-10-21 10:24:20 -07002938
2939 /* If alias and dentry share a parent, then no extra locks required */
2940 if (alias->d_parent == dentry->d_parent)
2941 goto out_unalias;
2942
Trond Myklebust9eaef272006-10-21 10:24:20 -07002943 /* See lock_rename() */
Trond Myklebust9eaef272006-10-21 10:24:20 -07002944 if (!mutex_trylock(&dentry->d_sb->s_vfs_rename_mutex))
2945 goto out_err;
2946 m1 = &dentry->d_sb->s_vfs_rename_mutex;
Al Viro9902af72016-04-15 15:08:36 -04002947 if (!inode_trylock_shared(alias->d_parent->d_inode))
Trond Myklebust9eaef272006-10-21 10:24:20 -07002948 goto out_err;
Al Viro9902af72016-04-15 15:08:36 -04002949 m2 = &alias->d_parent->d_inode->i_rwsem;
Trond Myklebust9eaef272006-10-21 10:24:20 -07002950out_unalias:
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07002951 __d_move(alias, dentry, false);
Al Virob5ae6b12014-10-12 22:16:02 -04002952 ret = 0;
Trond Myklebust9eaef272006-10-21 10:24:20 -07002953out_err:
Trond Myklebust9eaef272006-10-21 10:24:20 -07002954 if (m2)
Al Viro9902af72016-04-15 15:08:36 -04002955 up_read(m2);
Trond Myklebust9eaef272006-10-21 10:24:20 -07002956 if (m1)
2957 mutex_unlock(m1);
2958 return ret;
2959}
2960
David Howells770bfad2006-08-22 20:06:07 -04002961/**
J. Bruce Fields3f70bd52014-02-18 14:11:26 -05002962 * d_splice_alias - splice a disconnected dentry into the tree if one exists
2963 * @inode: the inode which may have a disconnected dentry
2964 * @dentry: a negative dentry which we want to point to the inode.
2965 *
J. Bruce Fieldsda093a92014-02-17 18:03:57 -05002966 * If inode is a directory and has an IS_ROOT alias, then d_move that in
2967 * place of the given dentry and return it, else simply d_add the inode
2968 * to the dentry and return NULL.
J. Bruce Fields3f70bd52014-02-18 14:11:26 -05002969 *
J. Bruce Fields908790f2014-02-17 17:58:42 -05002970 * If a non-IS_ROOT directory is found, the filesystem is corrupt, and
2971 * we should error out: directories can't have multiple aliases.
2972 *
J. Bruce Fields3f70bd52014-02-18 14:11:26 -05002973 * This is needed in the lookup routine of any filesystem that is exportable
2974 * (via knfsd) so that we can build dcache paths to directories effectively.
2975 *
2976 * If a dentry was found and moved, then it is returned. Otherwise NULL
2977 * is returned. This matches the expected return value of ->lookup.
2978 *
2979 * Cluster filesystems may call this function with a negative, hashed dentry.
2980 * In that case, we know that the inode will be a regular file, and also this
2981 * will only occur during atomic_open. So we need to check for the dentry
2982 * being already hashed only in the final case.
2983 */
2984struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
2985{
J. Bruce Fields3f70bd52014-02-18 14:11:26 -05002986 if (IS_ERR(inode))
2987 return ERR_CAST(inode);
2988
David Howells770bfad2006-08-22 20:06:07 -04002989 BUG_ON(!d_unhashed(dentry));
2990
Al Virode689f52016-03-09 18:05:42 -05002991 if (!inode)
Al Virob5ae6b12014-10-12 22:16:02 -04002992 goto out;
Al Virode689f52016-03-09 18:05:42 -05002993
Al Virob9680912016-04-11 00:53:26 -04002994 security_d_instantiate(dentry, inode);
Nick Piggin873feea2011-01-07 17:50:06 +11002995 spin_lock(&inode->i_lock);
Trond Myklebust9eaef272006-10-21 10:24:20 -07002996 if (S_ISDIR(inode->i_mode)) {
Al Virob5ae6b12014-10-12 22:16:02 -04002997 struct dentry *new = __d_find_any_alias(inode);
2998 if (unlikely(new)) {
Eric W. Biedermana03e2832015-08-15 13:36:41 -05002999 /* The reference to new ensures it remains an alias */
3000 spin_unlock(&inode->i_lock);
Al Viro18367502011-07-12 21:42:24 -04003001 write_seqlock(&rename_lock);
Al Virob5ae6b12014-10-12 22:16:02 -04003002 if (unlikely(d_ancestor(new, dentry))) {
Al Viro18367502011-07-12 21:42:24 -04003003 write_sequnlock(&rename_lock);
Al Virob5ae6b12014-10-12 22:16:02 -04003004 dput(new);
3005 new = ERR_PTR(-ELOOP);
3006 pr_warn_ratelimited(
3007 "VFS: Lookup of '%s' in %s %s"
3008 " would have caused loop\n",
3009 dentry->d_name.name,
3010 inode->i_sb->s_type->name,
3011 inode->i_sb->s_id);
3012 } else if (!IS_ROOT(new)) {
3013 int err = __d_unalias(inode, dentry, new);
3014 write_sequnlock(&rename_lock);
3015 if (err) {
3016 dput(new);
3017 new = ERR_PTR(err);
3018 }
Al Viro18367502011-07-12 21:42:24 -04003019 } else {
Al Virob5ae6b12014-10-12 22:16:02 -04003020 __d_move(new, dentry, false);
3021 write_sequnlock(&rename_lock);
Trond Myklebust9eaef272006-10-21 10:24:20 -07003022 }
Al Virob5ae6b12014-10-12 22:16:02 -04003023 iput(inode);
3024 return new;
Trond Myklebust9eaef272006-10-21 10:24:20 -07003025 }
David Howells770bfad2006-08-22 20:06:07 -04003026 }
Al Virob5ae6b12014-10-12 22:16:02 -04003027out:
Al Viroed782b52016-03-09 19:52:39 -05003028 __d_add(dentry, inode);
Al Virob5ae6b12014-10-12 22:16:02 -04003029 return NULL;
David Howells770bfad2006-08-22 20:06:07 -04003030}
Al Virob5ae6b12014-10-12 22:16:02 -04003031EXPORT_SYMBOL(d_splice_alias);
David Howells770bfad2006-08-22 20:06:07 -04003032
Miklos Szeredicdd16d02008-06-23 18:11:53 +02003033static int prepend(char **buffer, int *buflen, const char *str, int namelen)
Ram Pai6092d042008-03-27 13:06:20 +01003034{
3035 *buflen -= namelen;
3036 if (*buflen < 0)
3037 return -ENAMETOOLONG;
3038 *buffer -= namelen;
3039 memcpy(*buffer, str, namelen);
3040 return 0;
3041}
3042
Waiman Long232d2d62013-09-09 12:18:13 -04003043/**
3044 * prepend_name - prepend a pathname in front of current buffer pointer
Waiman Long18129972013-09-12 10:55:35 -04003045 * @buffer: buffer pointer
3046 * @buflen: allocated length of the buffer
3047 * @name: name string and length qstr structure
Waiman Long232d2d62013-09-09 12:18:13 -04003048 *
Mark Rutland66702eb2017-10-23 14:07:14 -07003049 * With RCU path tracing, it may race with d_move(). Use READ_ONCE() to
Waiman Long232d2d62013-09-09 12:18:13 -04003050 * make sure that either the old or the new name pointer and length are
3051 * fetched. However, there may be mismatch between length and pointer.
3052 * The length cannot be trusted, we need to copy it byte-by-byte until
3053 * the length is reached or a null byte is found. It also prepends "/" at
3054 * the beginning of the name. The sequence number check at the caller will
3055 * retry it again when a d_move() does happen. So any garbage in the buffer
3056 * due to mismatched pointer and length will be discarded.
Al Viro6d13f692014-09-29 14:46:30 -04003057 *
Paul E. McKenney7088efa2017-10-09 10:04:27 -07003058 * Load acquire is needed to make sure that we see that terminating NUL.
Waiman Long232d2d62013-09-09 12:18:13 -04003059 */
Al Viro9aba36d2016-07-20 22:28:45 -04003060static int prepend_name(char **buffer, int *buflen, const struct qstr *name)
Miklos Szeredicdd16d02008-06-23 18:11:53 +02003061{
Paul E. McKenney7088efa2017-10-09 10:04:27 -07003062 const char *dname = smp_load_acquire(&name->name); /* ^^^ */
Mark Rutland66702eb2017-10-23 14:07:14 -07003063 u32 dlen = READ_ONCE(name->len);
Waiman Long232d2d62013-09-09 12:18:13 -04003064 char *p;
3065
Waiman Long232d2d62013-09-09 12:18:13 -04003066 *buflen -= dlen + 1;
Al Viroe8251962014-03-23 00:28:40 -04003067 if (*buflen < 0)
3068 return -ENAMETOOLONG;
Waiman Long232d2d62013-09-09 12:18:13 -04003069 p = *buffer -= dlen + 1;
3070 *p++ = '/';
3071 while (dlen--) {
3072 char c = *dname++;
3073 if (!c)
3074 break;
3075 *p++ = c;
3076 }
3077 return 0;
Miklos Szeredicdd16d02008-06-23 18:11:53 +02003078}
3079
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080/**
Randy Dunlap208898c2010-11-18 15:02:49 -08003081 * prepend_path - Prepend path string to a buffer
Miklos Szeredif2eb6572010-08-10 11:41:39 +02003082 * @path: the dentry/vfsmount to report
Al Viro02125a82011-12-05 08:43:34 -05003083 * @root: root vfsmnt/dentry
Miklos Szeredif2eb6572010-08-10 11:41:39 +02003084 * @buffer: pointer to the end of the buffer
3085 * @buflen: pointer to buffer length
3086 *
Waiman Long18129972013-09-12 10:55:35 -04003087 * The function will first try to write out the pathname without taking any
3088 * lock other than the RCU read lock to make sure that dentries won't go away.
3089 * It only checks the sequence number of the global rename_lock as any change
3090 * in the dentry's d_seq will be preceded by changes in the rename_lock
3091 * sequence number. If the sequence number had been changed, it will restart
3092 * the whole pathname back-tracing sequence again by taking the rename_lock.
3093 * In this case, there is no need to take the RCU read lock as the recursive
3094 * parent pointer references will keep the dentry chain alive as long as no
3095 * rename operation is performed.
Miklos Szeredif2eb6572010-08-10 11:41:39 +02003096 */
Al Viro02125a82011-12-05 08:43:34 -05003097static int prepend_path(const struct path *path,
3098 const struct path *root,
Miklos Szeredif2eb6572010-08-10 11:41:39 +02003099 char **buffer, int *buflen)
3100{
Al Viroede4ceb2013-11-13 07:45:40 -05003101 struct dentry *dentry;
3102 struct vfsmount *vfsmnt;
3103 struct mount *mnt;
Miklos Szeredif2eb6572010-08-10 11:41:39 +02003104 int error = 0;
Al Viro48a066e2013-09-29 22:06:07 -04003105 unsigned seq, m_seq = 0;
Waiman Long232d2d62013-09-09 12:18:13 -04003106 char *bptr;
3107 int blen;
Miklos Szeredif2eb6572010-08-10 11:41:39 +02003108
Al Viro48f5ec22013-09-09 15:22:25 -04003109 rcu_read_lock();
Al Viro48a066e2013-09-29 22:06:07 -04003110restart_mnt:
3111 read_seqbegin_or_lock(&mount_lock, &m_seq);
3112 seq = 0;
Li Zhong4ec6c2a2013-11-13 15:21:51 +08003113 rcu_read_lock();
Waiman Long232d2d62013-09-09 12:18:13 -04003114restart:
3115 bptr = *buffer;
3116 blen = *buflen;
Al Viro48a066e2013-09-29 22:06:07 -04003117 error = 0;
Al Viroede4ceb2013-11-13 07:45:40 -05003118 dentry = path->dentry;
3119 vfsmnt = path->mnt;
3120 mnt = real_mount(vfsmnt);
Waiman Long232d2d62013-09-09 12:18:13 -04003121 read_seqbegin_or_lock(&rename_lock, &seq);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02003122 while (dentry != root->dentry || vfsmnt != root->mnt) {
3123 struct dentry * parent;
3124
3125 if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
Mark Rutland66702eb2017-10-23 14:07:14 -07003126 struct mount *parent = READ_ONCE(mnt->mnt_parent);
Eric W. Biedermancde93be2015-08-15 13:36:12 -05003127 /* Escaped? */
3128 if (dentry != vfsmnt->mnt_root) {
3129 bptr = *buffer;
3130 blen = *buflen;
3131 error = 3;
3132 break;
3133 }
Miklos Szeredif2eb6572010-08-10 11:41:39 +02003134 /* Global root? */
Al Viro48a066e2013-09-29 22:06:07 -04003135 if (mnt != parent) {
Mark Rutland66702eb2017-10-23 14:07:14 -07003136 dentry = READ_ONCE(mnt->mnt_mountpoint);
Al Viro48a066e2013-09-29 22:06:07 -04003137 mnt = parent;
Waiman Long232d2d62013-09-09 12:18:13 -04003138 vfsmnt = &mnt->mnt;
3139 continue;
3140 }
Waiman Long232d2d62013-09-09 12:18:13 -04003141 if (!error)
3142 error = is_mounted(vfsmnt) ? 1 : 2;
3143 break;
Miklos Szeredif2eb6572010-08-10 11:41:39 +02003144 }
3145 parent = dentry->d_parent;
3146 prefetch(parent);
Waiman Long232d2d62013-09-09 12:18:13 -04003147 error = prepend_name(&bptr, &blen, &dentry->d_name);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02003148 if (error)
3149 break;
3150
Miklos Szeredif2eb6572010-08-10 11:41:39 +02003151 dentry = parent;
3152 }
Al Viro48f5ec22013-09-09 15:22:25 -04003153 if (!(seq & 1))
3154 rcu_read_unlock();
3155 if (need_seqretry(&rename_lock, seq)) {
3156 seq = 1;
Waiman Long232d2d62013-09-09 12:18:13 -04003157 goto restart;
Miklos Szeredif2eb6572010-08-10 11:41:39 +02003158 }
Al Viro48f5ec22013-09-09 15:22:25 -04003159 done_seqretry(&rename_lock, seq);
Li Zhong4ec6c2a2013-11-13 15:21:51 +08003160
3161 if (!(m_seq & 1))
3162 rcu_read_unlock();
Al Viro48a066e2013-09-29 22:06:07 -04003163 if (need_seqretry(&mount_lock, m_seq)) {
3164 m_seq = 1;
3165 goto restart_mnt;
3166 }
3167 done_seqretry(&mount_lock, m_seq);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02003168
Waiman Long232d2d62013-09-09 12:18:13 -04003169 if (error >= 0 && bptr == *buffer) {
3170 if (--blen < 0)
3171 error = -ENAMETOOLONG;
3172 else
3173 *--bptr = '/';
Miklos Szeredif2eb6572010-08-10 11:41:39 +02003174 }
Waiman Long232d2d62013-09-09 12:18:13 -04003175 *buffer = bptr;
3176 *buflen = blen;
Al Viro7ea600b2013-03-26 18:25:57 -04003177 return error;
Miklos Szeredif2eb6572010-08-10 11:41:39 +02003178}
3179
3180/**
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +02003181 * __d_path - return the path of a dentry
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01003182 * @path: the dentry/vfsmount to report
Al Viro02125a82011-12-05 08:43:34 -05003183 * @root: root vfsmnt/dentry
Randy Dunlapcd956a12010-08-14 13:05:31 -07003184 * @buf: buffer to return value in
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 * @buflen: buffer length
3186 *
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02003187 * Convert a dentry into an ASCII path name.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 *
Arjan van de Ven52afeef2008-12-01 14:35:00 -08003189 * Returns a pointer into the buffer or an error code if the
3190 * path was too long.
Linus Torvalds552ce542007-02-13 12:08:18 -08003191 *
Christoph Hellwigbe148242010-10-10 05:36:21 -04003192 * "buflen" should be positive.
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01003193 *
Al Viro02125a82011-12-05 08:43:34 -05003194 * If the path is not reachable from the supplied root, return %NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 */
Al Viro02125a82011-12-05 08:43:34 -05003196char *__d_path(const struct path *path,
3197 const struct path *root,
Miklos Szeredif2eb6572010-08-10 11:41:39 +02003198 char *buf, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199{
Miklos Szeredif2eb6572010-08-10 11:41:39 +02003200 char *res = buf + buflen;
3201 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202
Miklos Szeredif2eb6572010-08-10 11:41:39 +02003203 prepend(&res, &buflen, "\0", 1);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02003204 error = prepend_path(path, root, &res, &buflen);
Christoph Hellwigbe148242010-10-10 05:36:21 -04003205
Al Viro02125a82011-12-05 08:43:34 -05003206 if (error < 0)
3207 return ERR_PTR(error);
3208 if (error > 0)
3209 return NULL;
3210 return res;
3211}
3212
3213char *d_absolute_path(const struct path *path,
3214 char *buf, int buflen)
3215{
3216 struct path root = {};
3217 char *res = buf + buflen;
3218 int error;
3219
3220 prepend(&res, &buflen, "\0", 1);
Al Viro02125a82011-12-05 08:43:34 -05003221 error = prepend_path(path, &root, &res, &buflen);
Al Viro02125a82011-12-05 08:43:34 -05003222
3223 if (error > 1)
3224 error = -EINVAL;
3225 if (error < 0)
Miklos Szeredif2eb6572010-08-10 11:41:39 +02003226 return ERR_PTR(error);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02003227 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228}
3229
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02003230/*
3231 * same as __d_path but appends "(deleted)" for unlinked files.
3232 */
Al Viro02125a82011-12-05 08:43:34 -05003233static int path_with_deleted(const struct path *path,
3234 const struct path *root,
3235 char **buf, int *buflen)
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02003236{
3237 prepend(buf, buflen, "\0", 1);
3238 if (d_unlinked(path->dentry)) {
3239 int error = prepend(buf, buflen, " (deleted)", 10);
3240 if (error)
3241 return error;
3242 }
3243
3244 return prepend_path(path, root, buf, buflen);
3245}
3246
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02003247static int prepend_unreachable(char **buffer, int *buflen)
3248{
3249 return prepend(buffer, buflen, "(unreachable)", 13);
3250}
3251
Linus Torvalds68f0d9d2013-09-12 13:24:55 -07003252static void get_fs_root_rcu(struct fs_struct *fs, struct path *root)
3253{
3254 unsigned seq;
3255
3256 do {
3257 seq = read_seqcount_begin(&fs->seq);
3258 *root = fs->root;
3259 } while (read_seqcount_retry(&fs->seq, seq));
3260}
3261
Jan Bluncka03a8a702008-02-14 19:38:32 -08003262/**
3263 * d_path - return the path of a dentry
Jan Blunckcf28b482008-02-14 19:38:44 -08003264 * @path: path to report
Jan Bluncka03a8a702008-02-14 19:38:32 -08003265 * @buf: buffer to return value in
3266 * @buflen: buffer length
3267 *
3268 * Convert a dentry into an ASCII path name. If the entry has been deleted
3269 * the string " (deleted)" is appended. Note that this is ambiguous.
3270 *
Arjan van de Ven52afeef2008-12-01 14:35:00 -08003271 * Returns a pointer into the buffer or an error code if the path was
3272 * too long. Note: Callers should use the returned pointer, not the passed
3273 * in buffer, to use the name! The implementation often starts at an offset
3274 * into the buffer, and may leave 0 bytes at the start.
Jan Bluncka03a8a702008-02-14 19:38:32 -08003275 *
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +02003276 * "buflen" should be positive.
Jan Bluncka03a8a702008-02-14 19:38:32 -08003277 */
Jan Engelhardt20d4fdc2008-06-09 16:40:36 -07003278char *d_path(const struct path *path, char *buf, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279{
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02003280 char *res = buf + buflen;
Jan Blunck6ac08c32008-02-14 19:34:38 -08003281 struct path root;
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02003282 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283
Eric Dumazetc23fbb62007-05-08 00:26:18 -07003284 /*
3285 * We have various synthetic filesystems that never get mounted. On
3286 * these filesystems dentries are never used for lookup purposes, and
3287 * thus don't need to be hashed. They also don't need a name until a
3288 * user wants to identify the object in /proc/pid/fd/. The little hack
3289 * below allows us to generate a name for these objects on demand:
Eric W. Biedermanf48cfdd2013-11-08 16:31:29 -08003290 *
3291 * Some pseudo inodes are mountable. When they are mounted
3292 * path->dentry == path->mnt->mnt_root. In that case don't call d_dname
3293 * and instead have d_path return the mounted path.
Eric Dumazetc23fbb62007-05-08 00:26:18 -07003294 */
Eric W. Biedermanf48cfdd2013-11-08 16:31:29 -08003295 if (path->dentry->d_op && path->dentry->d_op->d_dname &&
3296 (!IS_ROOT(path->dentry) || path->dentry != path->mnt->mnt_root))
Jan Blunckcf28b482008-02-14 19:38:44 -08003297 return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
Eric Dumazetc23fbb62007-05-08 00:26:18 -07003298
Linus Torvalds68f0d9d2013-09-12 13:24:55 -07003299 rcu_read_lock();
3300 get_fs_root_rcu(current->fs, &root);
Al Viro02125a82011-12-05 08:43:34 -05003301 error = path_with_deleted(path, &root, &res, &buflen);
Linus Torvalds68f0d9d2013-09-12 13:24:55 -07003302 rcu_read_unlock();
3303
Al Viro02125a82011-12-05 08:43:34 -05003304 if (error < 0)
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02003305 res = ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 return res;
3307}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07003308EXPORT_SYMBOL(d_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309
3310/*
Eric Dumazetc23fbb62007-05-08 00:26:18 -07003311 * Helper function for dentry_operations.d_dname() members
3312 */
3313char *dynamic_dname(struct dentry *dentry, char *buffer, int buflen,
3314 const char *fmt, ...)
3315{
3316 va_list args;
3317 char temp[64];
3318 int sz;
3319
3320 va_start(args, fmt);
3321 sz = vsnprintf(temp, sizeof(temp), fmt, args) + 1;
3322 va_end(args);
3323
3324 if (sz > sizeof(temp) || sz > buflen)
3325 return ERR_PTR(-ENAMETOOLONG);
3326
3327 buffer += buflen - sz;
3328 return memcpy(buffer, temp, sz);
3329}
3330
Al Viro118b2302013-08-24 12:08:17 -04003331char *simple_dname(struct dentry *dentry, char *buffer, int buflen)
3332{
3333 char *end = buffer + buflen;
3334 /* these dentries are never renamed, so d_lock is not needed */
3335 if (prepend(&end, &buflen, " (deleted)", 11) ||
Waiman Long232d2d62013-09-09 12:18:13 -04003336 prepend(&end, &buflen, dentry->d_name.name, dentry->d_name.len) ||
Al Viro118b2302013-08-24 12:08:17 -04003337 prepend(&end, &buflen, "/", 1))
3338 end = ERR_PTR(-ENAMETOOLONG);
Waiman Long232d2d62013-09-09 12:18:13 -04003339 return end;
Al Viro118b2302013-08-24 12:08:17 -04003340}
David Herrmann31bbe162014-01-03 14:09:47 +01003341EXPORT_SYMBOL(simple_dname);
Al Viro118b2302013-08-24 12:08:17 -04003342
Eric Dumazetc23fbb62007-05-08 00:26:18 -07003343/*
Ram Pai6092d042008-03-27 13:06:20 +01003344 * Write full pathname from the root of the filesystem into the buffer.
3345 */
Al Virof6500802014-01-26 12:37:55 -05003346static char *__dentry_path(struct dentry *d, char *buf, int buflen)
Ram Pai6092d042008-03-27 13:06:20 +01003347{
Al Virof6500802014-01-26 12:37:55 -05003348 struct dentry *dentry;
Waiman Long232d2d62013-09-09 12:18:13 -04003349 char *end, *retval;
3350 int len, seq = 0;
3351 int error = 0;
Ram Pai6092d042008-03-27 13:06:20 +01003352
Al Virof6500802014-01-26 12:37:55 -05003353 if (buflen < 2)
3354 goto Elong;
3355
Al Viro48f5ec22013-09-09 15:22:25 -04003356 rcu_read_lock();
Waiman Long232d2d62013-09-09 12:18:13 -04003357restart:
Al Virof6500802014-01-26 12:37:55 -05003358 dentry = d;
Waiman Long232d2d62013-09-09 12:18:13 -04003359 end = buf + buflen;
3360 len = buflen;
3361 prepend(&end, &len, "\0", 1);
Ram Pai6092d042008-03-27 13:06:20 +01003362 /* Get '/' right */
3363 retval = end-1;
3364 *retval = '/';
Waiman Long232d2d62013-09-09 12:18:13 -04003365 read_seqbegin_or_lock(&rename_lock, &seq);
Miklos Szeredicdd16d02008-06-23 18:11:53 +02003366 while (!IS_ROOT(dentry)) {
3367 struct dentry *parent = dentry->d_parent;
Ram Pai6092d042008-03-27 13:06:20 +01003368
Ram Pai6092d042008-03-27 13:06:20 +01003369 prefetch(parent);
Waiman Long232d2d62013-09-09 12:18:13 -04003370 error = prepend_name(&end, &len, &dentry->d_name);
3371 if (error)
3372 break;
Ram Pai6092d042008-03-27 13:06:20 +01003373
3374 retval = end;
3375 dentry = parent;
3376 }
Al Viro48f5ec22013-09-09 15:22:25 -04003377 if (!(seq & 1))
3378 rcu_read_unlock();
3379 if (need_seqretry(&rename_lock, seq)) {
3380 seq = 1;
Waiman Long232d2d62013-09-09 12:18:13 -04003381 goto restart;
Al Viro48f5ec22013-09-09 15:22:25 -04003382 }
3383 done_seqretry(&rename_lock, seq);
Waiman Long232d2d62013-09-09 12:18:13 -04003384 if (error)
3385 goto Elong;
Al Viroc1031352010-06-06 22:31:14 -04003386 return retval;
3387Elong:
3388 return ERR_PTR(-ENAMETOOLONG);
3389}
Nick Pigginec2447c2011-01-07 17:49:29 +11003390
3391char *dentry_path_raw(struct dentry *dentry, char *buf, int buflen)
3392{
Waiman Long232d2d62013-09-09 12:18:13 -04003393 return __dentry_path(dentry, buf, buflen);
Nick Pigginec2447c2011-01-07 17:49:29 +11003394}
3395EXPORT_SYMBOL(dentry_path_raw);
Al Viroc1031352010-06-06 22:31:14 -04003396
3397char *dentry_path(struct dentry *dentry, char *buf, int buflen)
3398{
3399 char *p = NULL;
3400 char *retval;
3401
Al Viroc1031352010-06-06 22:31:14 -04003402 if (d_unlinked(dentry)) {
3403 p = buf + buflen;
3404 if (prepend(&p, &buflen, "//deleted", 10) != 0)
3405 goto Elong;
3406 buflen++;
3407 }
3408 retval = __dentry_path(dentry, buf, buflen);
Al Viroc1031352010-06-06 22:31:14 -04003409 if (!IS_ERR(retval) && p)
3410 *p = '/'; /* restore '/' overriden with '\0' */
Ram Pai6092d042008-03-27 13:06:20 +01003411 return retval;
3412Elong:
Ram Pai6092d042008-03-27 13:06:20 +01003413 return ERR_PTR(-ENAMETOOLONG);
3414}
3415
Linus Torvalds8b19e342013-09-12 10:35:47 -07003416static void get_fs_root_and_pwd_rcu(struct fs_struct *fs, struct path *root,
3417 struct path *pwd)
Linus Torvalds57624822013-09-12 10:12:47 -07003418{
Linus Torvalds8b19e342013-09-12 10:35:47 -07003419 unsigned seq;
3420
3421 do {
3422 seq = read_seqcount_begin(&fs->seq);
3423 *root = fs->root;
3424 *pwd = fs->pwd;
3425 } while (read_seqcount_retry(&fs->seq, seq));
Linus Torvalds57624822013-09-12 10:12:47 -07003426}
3427
Ram Pai6092d042008-03-27 13:06:20 +01003428/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 * NOTE! The user-level library version returns a
3430 * character pointer. The kernel system call just
3431 * returns the length of the buffer filled (which
3432 * includes the ending '\0' character), or a negative
3433 * error value. So libc would do something like
3434 *
3435 * char *getcwd(char * buf, size_t size)
3436 * {
3437 * int retval;
3438 *
3439 * retval = sys_getcwd(buf, size);
3440 * if (retval >= 0)
3441 * return buf;
3442 * errno = -retval;
3443 * return NULL;
3444 * }
3445 */
Heiko Carstens3cdad422009-01-14 14:14:22 +01003446SYSCALL_DEFINE2(getcwd, char __user *, buf, unsigned long, size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447{
Linus Torvalds552ce542007-02-13 12:08:18 -08003448 int error;
Jan Blunck6ac08c32008-02-14 19:34:38 -08003449 struct path pwd, root;
Linus Torvalds3272c542013-09-12 12:40:15 -07003450 char *page = __getname();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451
3452 if (!page)
3453 return -ENOMEM;
3454
Linus Torvalds8b19e342013-09-12 10:35:47 -07003455 rcu_read_lock();
3456 get_fs_root_and_pwd_rcu(current->fs, &root, &pwd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457
Linus Torvalds552ce542007-02-13 12:08:18 -08003458 error = -ENOENT;
Alexey Dobriyanf3da3922009-05-04 03:32:03 +04003459 if (!d_unlinked(pwd.dentry)) {
Linus Torvalds552ce542007-02-13 12:08:18 -08003460 unsigned long len;
Linus Torvalds3272c542013-09-12 12:40:15 -07003461 char *cwd = page + PATH_MAX;
3462 int buflen = PATH_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02003464 prepend(&cwd, &buflen, "\0", 1);
Al Viro02125a82011-12-05 08:43:34 -05003465 error = prepend_path(&pwd, &root, &cwd, &buflen);
Linus Torvaldsff812d72013-09-12 11:57:01 -07003466 rcu_read_unlock();
Linus Torvalds552ce542007-02-13 12:08:18 -08003467
Al Viro02125a82011-12-05 08:43:34 -05003468 if (error < 0)
Linus Torvalds552ce542007-02-13 12:08:18 -08003469 goto out;
3470
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02003471 /* Unreachable from current root */
Al Viro02125a82011-12-05 08:43:34 -05003472 if (error > 0) {
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02003473 error = prepend_unreachable(&cwd, &buflen);
3474 if (error)
3475 goto out;
3476 }
3477
Linus Torvalds552ce542007-02-13 12:08:18 -08003478 error = -ERANGE;
Linus Torvalds3272c542013-09-12 12:40:15 -07003479 len = PATH_MAX + page - cwd;
Linus Torvalds552ce542007-02-13 12:08:18 -08003480 if (len <= size) {
3481 error = len;
3482 if (copy_to_user(buf, cwd, len))
3483 error = -EFAULT;
3484 }
Nick Piggin949854d2011-01-07 17:49:37 +11003485 } else {
Linus Torvaldsff812d72013-09-12 11:57:01 -07003486 rcu_read_unlock();
Nick Piggin949854d2011-01-07 17:49:37 +11003487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488
3489out:
Linus Torvalds3272c542013-09-12 12:40:15 -07003490 __putname(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 return error;
3492}
3493
3494/*
3495 * Test whether new_dentry is a subdirectory of old_dentry.
3496 *
3497 * Trivially implemented using the dcache structure
3498 */
3499
3500/**
3501 * is_subdir - is new dentry a subdirectory of old_dentry
3502 * @new_dentry: new dentry
3503 * @old_dentry: old dentry
3504 *
Yaowei Baia6e57872015-11-17 14:40:11 +08003505 * Returns true if new_dentry is a subdirectory of the parent (at any depth).
3506 * Returns false otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
3508 */
3509
Yaowei Baia6e57872015-11-17 14:40:11 +08003510bool is_subdir(struct dentry *new_dentry, struct dentry *old_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511{
Yaowei Baia6e57872015-11-17 14:40:11 +08003512 bool result;
Nick Piggin949854d2011-01-07 17:49:37 +11003513 unsigned seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09003515 if (new_dentry == old_dentry)
Yaowei Baia6e57872015-11-17 14:40:11 +08003516 return true;
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09003517
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09003518 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 /* for restarting inner loop in case of seq retry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 seq = read_seqbegin(&rename_lock);
Nick Piggin949854d2011-01-07 17:49:37 +11003521 /*
3522 * Need rcu_readlock to protect against the d_parent trashing
3523 * due to d_move
3524 */
3525 rcu_read_lock();
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09003526 if (d_ancestor(old_dentry, new_dentry))
Yaowei Baia6e57872015-11-17 14:40:11 +08003527 result = true;
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09003528 else
Yaowei Baia6e57872015-11-17 14:40:11 +08003529 result = false;
Nick Piggin949854d2011-01-07 17:49:37 +11003530 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 } while (read_seqretry(&rename_lock, seq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532
3533 return result;
3534}
3535
Miklos Szeredidb14fc32013-09-05 11:44:35 +02003536static enum d_walk_ret d_genocide_kill(void *data, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537{
Miklos Szeredidb14fc32013-09-05 11:44:35 +02003538 struct dentry *root = data;
3539 if (dentry != root) {
3540 if (d_unhashed(dentry) || !dentry->d_inode)
3541 return D_WALK_SKIP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542
Miklos Szeredi01ddc4e2013-09-05 11:44:34 +02003543 if (!(dentry->d_flags & DCACHE_GENOCIDE)) {
3544 dentry->d_flags |= DCACHE_GENOCIDE;
3545 dentry->d_lockref.count--;
3546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 }
Miklos Szeredidb14fc32013-09-05 11:44:35 +02003548 return D_WALK_CONTINUE;
3549}
Nick Piggin58db63d2011-01-07 17:49:39 +11003550
Miklos Szeredidb14fc32013-09-05 11:44:35 +02003551void d_genocide(struct dentry *parent)
3552{
3553 d_walk(parent, parent, d_genocide_kill, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554}
3555
Al Viro60545d02013-06-07 01:20:27 -04003556void d_tmpfile(struct dentry *dentry, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557{
Al Viro60545d02013-06-07 01:20:27 -04003558 inode_dec_link_count(inode);
3559 BUG_ON(dentry->d_name.name != dentry->d_iname ||
Al Viro946e51f2014-10-26 19:19:16 -04003560 !hlist_unhashed(&dentry->d_u.d_alias) ||
Al Viro60545d02013-06-07 01:20:27 -04003561 !d_unlinked(dentry));
3562 spin_lock(&dentry->d_parent->d_lock);
3563 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
3564 dentry->d_name.len = sprintf(dentry->d_iname, "#%llu",
3565 (unsigned long long)inode->i_ino);
3566 spin_unlock(&dentry->d_lock);
3567 spin_unlock(&dentry->d_parent->d_lock);
3568 d_instantiate(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569}
Al Viro60545d02013-06-07 01:20:27 -04003570EXPORT_SYMBOL(d_tmpfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571
3572static __initdata unsigned long dhash_entries;
3573static int __init set_dhash_entries(char *str)
3574{
3575 if (!str)
3576 return 0;
3577 dhash_entries = simple_strtoul(str, &str, 0);
3578 return 1;
3579}
3580__setup("dhash_entries=", set_dhash_entries);
3581
3582static void __init dcache_init_early(void)
3583{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 /* If hashes are distributed across NUMA nodes, defer
3585 * hash allocation until vmalloc space is available.
3586 */
3587 if (hashdist)
3588 return;
3589
3590 dentry_hashtable =
3591 alloc_large_system_hash("Dentry cache",
Linus Torvaldsb07ad992011-04-23 22:32:03 -07003592 sizeof(struct hlist_bl_head),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 dhash_entries,
3594 13,
Pavel Tatashin3d375d72017-07-06 15:39:11 -07003595 HASH_EARLY | HASH_ZERO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 &d_hash_shift,
Alexey Dobriyanb35d7862017-11-20 18:05:52 +03003597 NULL,
Tim Bird31fe62b2012-05-23 13:33:35 +00003598 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599 0);
Alexey Dobriyan854d3e62017-11-20 18:05:07 +03003600 d_hash_shift = 32 - d_hash_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601}
3602
Denis Cheng74bf17c2007-10-16 23:26:30 -07003603static void __init dcache_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604{
Pavel Tatashin3d375d72017-07-06 15:39:11 -07003605 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606 * A constructor could be added for stable state like the lists,
3607 * but it is probably not worth it because of the cache nature
Pavel Tatashin3d375d72017-07-06 15:39:11 -07003608 * of the dcache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 */
Christoph Lameter0a31bd52007-05-06 14:49:57 -07003610 dentry_cache = KMEM_CACHE(dentry,
Vladimir Davydov5d097052016-01-14 15:18:21 -08003611 SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|SLAB_MEM_SPREAD|SLAB_ACCOUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612
3613 /* Hash may have been set up in dcache_init_early */
3614 if (!hashdist)
3615 return;
3616
3617 dentry_hashtable =
3618 alloc_large_system_hash("Dentry cache",
Linus Torvaldsb07ad992011-04-23 22:32:03 -07003619 sizeof(struct hlist_bl_head),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 dhash_entries,
3621 13,
Pavel Tatashin3d375d72017-07-06 15:39:11 -07003622 HASH_ZERO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 &d_hash_shift,
Alexey Dobriyanb35d7862017-11-20 18:05:52 +03003624 NULL,
Tim Bird31fe62b2012-05-23 13:33:35 +00003625 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626 0);
Alexey Dobriyan854d3e62017-11-20 18:05:07 +03003627 d_hash_shift = 32 - d_hash_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628}
3629
3630/* SLAB cache for __getname() consumers */
Christoph Lametere18b8902006-12-06 20:33:20 -08003631struct kmem_cache *names_cachep __read_mostly;
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07003632EXPORT_SYMBOL(names_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634EXPORT_SYMBOL(d_genocide);
3635
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636void __init vfs_caches_init_early(void)
3637{
Sebastian Andrzej Siewior69163632017-06-27 18:19:11 +02003638 int i;
3639
3640 for (i = 0; i < ARRAY_SIZE(in_lookup_hashtable); i++)
3641 INIT_HLIST_BL_HEAD(&in_lookup_hashtable[i]);
3642
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643 dcache_init_early();
3644 inode_init_early();
3645}
3646
Mel Gorman4248b0d2015-08-06 15:46:20 -07003647void __init vfs_caches_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649 names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09003650 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651
Denis Cheng74bf17c2007-10-16 23:26:30 -07003652 dcache_init();
3653 inode_init();
Mel Gorman4248b0d2015-08-06 15:46:20 -07003654 files_init();
3655 files_maxfiles_init();
Denis Cheng74bf17c2007-10-16 23:26:30 -07003656 mnt_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 bdev_cache_init();
3658 chrdev_init();
3659}