blob: af8501489af59e3f432f2f64c035f01f4d32112b [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>
David Howells07f3f052006-09-30 20:52:18 +020040#include "internal.h"
Al Virob2dba1a2011-11-23 19:26:23 -050041#include "mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Nick Piggin789680d2011-01-07 17:49:30 +110043/*
44 * Usage:
Nick Piggin873feea2011-01-07 17:50:06 +110045 * dcache->d_inode->i_lock protects:
Al Viro946e51f2014-10-26 19:19:16 -040046 * - i_dentry, d_u.d_alias, d_inode of aliases
Nick Pigginceb5bdc2011-01-07 17:50:05 +110047 * dcache_hash_bucket lock protects:
48 * - the dcache hash table
NeilBrownf1ee6162017-12-21 09:45:40 +110049 * s_roots bl list spinlock protects:
50 * - the s_roots list (see __d_drop)
Dave Chinner19156842013-08-28 10:17:55 +100051 * dentry->d_sb->s_dentry_lru_lock protects:
Nick Piggin23044502011-01-07 17:49:31 +110052 * - the dcache lru lists and counters
53 * d_lock protects:
54 * - d_flags
55 * - d_name
56 * - d_lru
Nick Pigginb7ab39f2011-01-07 17:49:32 +110057 * - d_count
Nick Pigginda502952011-01-07 17:49:33 +110058 * - d_unhashed()
Nick Piggin2fd6b7f2011-01-07 17:49:34 +110059 * - d_parent and d_subdirs
60 * - childrens' d_child and d_parent
Al Viro946e51f2014-10-26 19:19:16 -040061 * - d_u.d_alias, d_inode
Nick Piggin789680d2011-01-07 17:49:30 +110062 *
63 * Ordering:
Nick Piggin873feea2011-01-07 17:50:06 +110064 * dentry->d_inode->i_lock
Nick Pigginb5c84bf2011-01-07 17:49:38 +110065 * dentry->d_lock
Dave Chinner19156842013-08-28 10:17:55 +100066 * dentry->d_sb->s_dentry_lru_lock
Nick Pigginceb5bdc2011-01-07 17:50:05 +110067 * dcache_hash_bucket lock
NeilBrownf1ee6162017-12-21 09:45:40 +110068 * s_roots lock
Nick Piggin789680d2011-01-07 17:49:30 +110069 *
Nick Pigginda502952011-01-07 17:49:33 +110070 * If there is an ancestor relationship:
71 * dentry->d_parent->...->d_parent->d_lock
72 * ...
73 * dentry->d_parent->d_lock
74 * dentry->d_lock
75 *
76 * If no ancestor relationship:
Nick Piggin789680d2011-01-07 17:49:30 +110077 * if (dentry1 < dentry2)
78 * dentry1->d_lock
79 * dentry2->d_lock
80 */
Eric Dumazetfa3536c2006-03-26 01:37:24 -080081int sysctl_vfs_cache_pressure __read_mostly = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082EXPORT_SYMBOL_GPL(sysctl_vfs_cache_pressure);
83
Al Viro74c3cbe2007-07-22 08:04:18 -040084__cacheline_aligned_in_smp DEFINE_SEQLOCK(rename_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Nick Piggin949854d2011-01-07 17:49:37 +110086EXPORT_SYMBOL(rename_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Christoph Lametere18b8902006-12-06 20:33:20 -080088static struct kmem_cache *dentry_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
David Howellscdf01222017-07-04 17:25:22 +010090const struct qstr empty_name = QSTR_INIT("", 0);
91EXPORT_SYMBOL(empty_name);
92const struct qstr slash_name = QSTR_INIT("/", 1);
93EXPORT_SYMBOL(slash_name);
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095/*
96 * This is the single most critical data structure when it comes
97 * to the dcache: the hashtable for lookups. Somebody should try
98 * to make this good - I've just made it work.
99 *
100 * This hash-function tries to avoid losing too many bits of hash
101 * information, yet avoid using a prime hash-size or similar.
102 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Eric Dumazetfa3536c2006-03-26 01:37:24 -0800104static unsigned int d_hash_shift __read_mostly;
Nick Pigginceb5bdc2011-01-07 17:50:05 +1100105
Linus Torvaldsb07ad992011-04-23 22:32:03 -0700106static struct hlist_bl_head *dentry_hashtable __read_mostly;
Nick Pigginceb5bdc2011-01-07 17:50:05 +1100107
Linus Torvalds8387ff22016-06-10 07:51:30 -0700108static inline struct hlist_bl_head *d_hash(unsigned int hash)
Nick Pigginceb5bdc2011-01-07 17:50:05 +1100109{
Alexey Dobriyan854d3e62017-11-20 18:05:07 +0300110 return dentry_hashtable + (hash >> d_hash_shift);
Nick Pigginceb5bdc2011-01-07 17:50:05 +1100111}
112
Al Viro94bdd652016-04-15 02:42:04 -0400113#define IN_LOOKUP_SHIFT 10
114static struct hlist_bl_head in_lookup_hashtable[1 << IN_LOOKUP_SHIFT];
115
116static inline struct hlist_bl_head *in_lookup_hash(const struct dentry *parent,
117 unsigned int hash)
118{
119 hash += (unsigned long) parent / L1_CACHE_BYTES;
120 return in_lookup_hashtable + hash_32(hash, IN_LOOKUP_SHIFT);
121}
122
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124/* Statistics gathering. */
125struct dentry_stat_t dentry_stat = {
126 .age_limit = 45,
127};
128
Glauber Costa3942c072013-08-28 10:17:53 +1000129static DEFINE_PER_CPU(long, nr_dentry);
Dave Chinner62d36c72013-08-28 10:17:54 +1000130static DEFINE_PER_CPU(long, nr_dentry_unused);
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400131
132#if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS)
Dave Chinner62d36c72013-08-28 10:17:54 +1000133
134/*
135 * Here we resort to our own counters instead of using generic per-cpu counters
136 * for consistency with what the vfs inode code does. We are expected to harvest
137 * better code and performance by having our own specialized counters.
138 *
139 * Please note that the loop is done over all possible CPUs, not over all online
140 * CPUs. The reason for this is that we don't want to play games with CPUs going
141 * on and off. If one of them goes off, we will just keep their counters.
142 *
143 * glommer: See cffbc8a for details, and if you ever intend to change this,
144 * please update all vfs counters to match.
145 */
Glauber Costa3942c072013-08-28 10:17:53 +1000146static long get_nr_dentry(void)
Nick Piggin3e880fb2011-01-07 17:49:19 +1100147{
148 int i;
Glauber Costa3942c072013-08-28 10:17:53 +1000149 long sum = 0;
Nick Piggin3e880fb2011-01-07 17:49:19 +1100150 for_each_possible_cpu(i)
151 sum += per_cpu(nr_dentry, i);
152 return sum < 0 ? 0 : sum;
153}
154
Dave Chinner62d36c72013-08-28 10:17:54 +1000155static long get_nr_dentry_unused(void)
156{
157 int i;
158 long sum = 0;
159 for_each_possible_cpu(i)
160 sum += per_cpu(nr_dentry_unused, i);
161 return sum < 0 ? 0 : sum;
162}
163
Joe Perches1f7e0612014-06-06 14:38:05 -0700164int proc_nr_dentry(struct ctl_table *table, int write, void __user *buffer,
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400165 size_t *lenp, loff_t *ppos)
166{
Nick Piggin3e880fb2011-01-07 17:49:19 +1100167 dentry_stat.nr_dentry = get_nr_dentry();
Dave Chinner62d36c72013-08-28 10:17:54 +1000168 dentry_stat.nr_unused = get_nr_dentry_unused();
Glauber Costa3942c072013-08-28 10:17:53 +1000169 return proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400170}
171#endif
172
Linus Torvalds5483f182012-03-04 15:51:42 -0800173/*
174 * Compare 2 name strings, return 0 if they match, otherwise non-zero.
175 * The strings are both count bytes long, and count is non-zero.
176 */
Linus Torvaldse419b4c2012-05-03 10:16:43 -0700177#ifdef CONFIG_DCACHE_WORD_ACCESS
178
179#include <asm/word-at-a-time.h>
180/*
181 * NOTE! 'cs' and 'scount' come from a dentry, so it has a
182 * aligned allocation for this particular component. We don't
183 * strictly need the load_unaligned_zeropad() safety, but it
184 * doesn't hurt either.
185 *
186 * In contrast, 'ct' and 'tcount' can be from a pathname, and do
187 * need the careful unaligned handling.
188 */
Linus Torvalds94753db52012-05-10 12:19:19 -0700189static inline int dentry_string_cmp(const unsigned char *cs, const unsigned char *ct, unsigned tcount)
Linus Torvalds5483f182012-03-04 15:51:42 -0800190{
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -0800191 unsigned long a,b,mask;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -0800192
193 for (;;) {
Andrey Ryabininbfe7aa62018-02-01 21:00:51 +0300194 a = read_word_at_a_time(cs);
Linus Torvaldse419b4c2012-05-03 10:16:43 -0700195 b = load_unaligned_zeropad(ct);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -0800196 if (tcount < sizeof(unsigned long))
197 break;
198 if (unlikely(a != b))
199 return 1;
200 cs += sizeof(unsigned long);
201 ct += sizeof(unsigned long);
202 tcount -= sizeof(unsigned long);
203 if (!tcount)
204 return 0;
205 }
Will Deacona5c21dc2013-12-12 17:40:21 +0000206 mask = bytemask_from_count(tcount);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -0800207 return unlikely(!!((a ^ b) & mask));
Linus Torvaldse419b4c2012-05-03 10:16:43 -0700208}
209
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -0800210#else
Linus Torvaldse419b4c2012-05-03 10:16:43 -0700211
Linus Torvalds94753db52012-05-10 12:19:19 -0700212static inline int dentry_string_cmp(const unsigned char *cs, const unsigned char *ct, unsigned tcount)
Linus Torvaldse419b4c2012-05-03 10:16:43 -0700213{
Linus Torvalds5483f182012-03-04 15:51:42 -0800214 do {
215 if (*cs != *ct)
216 return 1;
217 cs++;
218 ct++;
219 tcount--;
220 } while (tcount);
221 return 0;
222}
223
Linus Torvaldse419b4c2012-05-03 10:16:43 -0700224#endif
225
Linus Torvalds94753db52012-05-10 12:19:19 -0700226static inline int dentry_cmp(const struct dentry *dentry, const unsigned char *ct, unsigned tcount)
227{
Linus Torvalds94753db52012-05-10 12:19:19 -0700228 /*
229 * Be careful about RCU walk racing with rename:
Will Deacon506458e2017-10-24 11:22:48 +0100230 * use 'READ_ONCE' to fetch the name pointer.
Linus Torvalds94753db52012-05-10 12:19:19 -0700231 *
232 * NOTE! Even if a rename will mean that the length
233 * was not loaded atomically, we don't care. The
234 * RCU walk will check the sequence count eventually,
235 * and catch it. And we won't overrun the buffer,
236 * because we're reading the name pointer atomically,
237 * and a dentry name is guaranteed to be properly
238 * terminated with a NUL byte.
239 *
240 * End result: even if 'len' is wrong, we'll exit
241 * early because the data cannot match (there can
242 * be no NUL in the ct/tcount data)
243 */
Will Deacon506458e2017-10-24 11:22:48 +0100244 const unsigned char *cs = READ_ONCE(dentry->d_name.name);
He Kuangae0a8432016-03-26 09:12:10 +0000245
Linus Torvalds6326c71f2012-05-21 16:14:04 -0700246 return dentry_string_cmp(cs, ct, tcount);
Linus Torvalds94753db52012-05-10 12:19:19 -0700247}
248
Al Viro8d85b482014-09-29 14:54:27 -0400249struct external_name {
250 union {
251 atomic_t count;
252 struct rcu_head head;
253 } u;
254 unsigned char name[];
255};
256
257static inline struct external_name *external_name(struct dentry *dentry)
258{
259 return container_of(dentry->d_name.name, struct external_name, name[0]);
260}
261
Christoph Hellwig9c82ab92010-10-10 05:36:22 -0400262static void __d_free(struct rcu_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
Christoph Hellwig9c82ab92010-10-10 05:36:22 -0400264 struct dentry *dentry = container_of(head, struct dentry, d_u.d_rcu);
265
Al Viro8d85b482014-09-29 14:54:27 -0400266 kmem_cache_free(dentry_cache, dentry);
267}
268
269static void __d_free_external(struct rcu_head *head)
270{
271 struct dentry *dentry = container_of(head, struct dentry, d_u.d_rcu);
Al Viro8d85b482014-09-29 14:54:27 -0400272 kfree(external_name(dentry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 kmem_cache_free(dentry_cache, dentry);
274}
275
Al Viro810bb172014-10-12 12:45:37 -0400276static inline int dname_external(const struct dentry *dentry)
277{
278 return dentry->d_name.name != dentry->d_iname;
279}
280
Al Viro49d31c22017-07-07 14:51:19 -0400281void take_dentry_name_snapshot(struct name_snapshot *name, struct dentry *dentry)
282{
283 spin_lock(&dentry->d_lock);
284 if (unlikely(dname_external(dentry))) {
285 struct external_name *p = external_name(dentry);
286 atomic_inc(&p->u.count);
287 spin_unlock(&dentry->d_lock);
288 name->name = p->name;
289 } else {
290 memcpy(name->inline_name, dentry->d_iname, DNAME_INLINE_LEN);
291 spin_unlock(&dentry->d_lock);
292 name->name = name->inline_name;
293 }
294}
295EXPORT_SYMBOL(take_dentry_name_snapshot);
296
297void release_dentry_name_snapshot(struct name_snapshot *name)
298{
299 if (unlikely(name->name != name->inline_name)) {
300 struct external_name *p;
301 p = container_of(name->name, struct external_name, name[0]);
302 if (unlikely(atomic_dec_and_test(&p->u.count)))
303 kfree_rcu(p, u.head);
304 }
305}
306EXPORT_SYMBOL(release_dentry_name_snapshot);
307
David Howells4bf46a22015-03-05 14:09:22 +0000308static inline void __d_set_inode_and_type(struct dentry *dentry,
309 struct inode *inode,
310 unsigned type_flags)
311{
312 unsigned flags;
313
314 dentry->d_inode = inode;
David Howells4bf46a22015-03-05 14:09:22 +0000315 flags = READ_ONCE(dentry->d_flags);
316 flags &= ~(DCACHE_ENTRY_TYPE | DCACHE_FALLTHRU);
317 flags |= type_flags;
318 WRITE_ONCE(dentry->d_flags, flags);
319}
320
David Howells4bf46a22015-03-05 14:09:22 +0000321static inline void __d_clear_type_and_inode(struct dentry *dentry)
322{
323 unsigned flags = READ_ONCE(dentry->d_flags);
324
325 flags &= ~(DCACHE_ENTRY_TYPE | DCACHE_FALLTHRU);
326 WRITE_ONCE(dentry->d_flags, flags);
David Howells4bf46a22015-03-05 14:09:22 +0000327 dentry->d_inode = NULL;
328}
329
Al Virob4f03542014-04-29 23:40:14 -0400330static void dentry_free(struct dentry *dentry)
331{
Al Viro946e51f2014-10-26 19:19:16 -0400332 WARN_ON(!hlist_unhashed(&dentry->d_u.d_alias));
Al Viro8d85b482014-09-29 14:54:27 -0400333 if (unlikely(dname_external(dentry))) {
334 struct external_name *p = external_name(dentry);
335 if (likely(atomic_dec_and_test(&p->u.count))) {
336 call_rcu(&dentry->d_u.d_rcu, __d_free_external);
337 return;
338 }
339 }
Al Virob4f03542014-04-29 23:40:14 -0400340 /* if dentry was never visible to RCU, immediate free is OK */
341 if (!(dentry->d_flags & DCACHE_RCUACCESS))
342 __d_free(&dentry->d_u.d_rcu);
343 else
344 call_rcu(&dentry->d_u.d_rcu, __d_free);
345}
346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347/*
348 * Release the dentry's inode, using the filesystem
Al Viro550dce02016-05-29 20:13:30 -0400349 * d_iput() operation if defined.
Nick Piggin31e6b012011-01-07 17:49:52 +1100350 */
351static void dentry_unlink_inode(struct dentry * dentry)
352 __releases(dentry->d_lock)
Nick Piggin873feea2011-01-07 17:50:06 +1100353 __releases(dentry->d_inode->i_lock)
Nick Piggin31e6b012011-01-07 17:49:52 +1100354{
355 struct inode *inode = dentry->d_inode;
Al Viro550dce02016-05-29 20:13:30 -0400356 bool hashed = !d_unhashed(dentry);
Al Viroa528aca2016-02-29 12:12:46 -0500357
Al Viro550dce02016-05-29 20:13:30 -0400358 if (hashed)
359 raw_write_seqcount_begin(&dentry->d_seq);
David Howells4bf46a22015-03-05 14:09:22 +0000360 __d_clear_type_and_inode(dentry);
Al Viro946e51f2014-10-26 19:19:16 -0400361 hlist_del_init(&dentry->d_u.d_alias);
Al Viro550dce02016-05-29 20:13:30 -0400362 if (hashed)
363 raw_write_seqcount_end(&dentry->d_seq);
Nick Piggin31e6b012011-01-07 17:49:52 +1100364 spin_unlock(&dentry->d_lock);
Nick Piggin873feea2011-01-07 17:50:06 +1100365 spin_unlock(&inode->i_lock);
Nick Piggin31e6b012011-01-07 17:49:52 +1100366 if (!inode->i_nlink)
367 fsnotify_inoderemove(inode);
368 if (dentry->d_op && dentry->d_op->d_iput)
369 dentry->d_op->d_iput(dentry, inode);
370 else
371 iput(inode);
372}
373
374/*
Linus Torvalds89dc77b2013-09-13 22:55:10 -0400375 * The DCACHE_LRU_LIST bit is set whenever the 'd_lru' entry
376 * is in use - which includes both the "real" per-superblock
377 * LRU list _and_ the DCACHE_SHRINK_LIST use.
378 *
379 * The DCACHE_SHRINK_LIST bit is set whenever the dentry is
380 * on the shrink list (ie not on the superblock LRU list).
381 *
382 * The per-cpu "nr_dentry_unused" counters are updated with
383 * the DCACHE_LRU_LIST bit.
384 *
385 * These helper functions make sure we always follow the
386 * rules. d_lock must be held by the caller.
387 */
388#define D_FLAG_VERIFY(dentry,x) WARN_ON_ONCE(((dentry)->d_flags & (DCACHE_LRU_LIST | DCACHE_SHRINK_LIST)) != (x))
389static void d_lru_add(struct dentry *dentry)
390{
391 D_FLAG_VERIFY(dentry, 0);
392 dentry->d_flags |= DCACHE_LRU_LIST;
393 this_cpu_inc(nr_dentry_unused);
394 WARN_ON_ONCE(!list_lru_add(&dentry->d_sb->s_dentry_lru, &dentry->d_lru));
395}
396
397static void d_lru_del(struct dentry *dentry)
398{
399 D_FLAG_VERIFY(dentry, DCACHE_LRU_LIST);
400 dentry->d_flags &= ~DCACHE_LRU_LIST;
401 this_cpu_dec(nr_dentry_unused);
402 WARN_ON_ONCE(!list_lru_del(&dentry->d_sb->s_dentry_lru, &dentry->d_lru));
403}
404
405static void d_shrink_del(struct dentry *dentry)
406{
407 D_FLAG_VERIFY(dentry, DCACHE_SHRINK_LIST | DCACHE_LRU_LIST);
408 list_del_init(&dentry->d_lru);
409 dentry->d_flags &= ~(DCACHE_SHRINK_LIST | DCACHE_LRU_LIST);
410 this_cpu_dec(nr_dentry_unused);
411}
412
413static void d_shrink_add(struct dentry *dentry, struct list_head *list)
414{
415 D_FLAG_VERIFY(dentry, 0);
416 list_add(&dentry->d_lru, list);
417 dentry->d_flags |= DCACHE_SHRINK_LIST | DCACHE_LRU_LIST;
418 this_cpu_inc(nr_dentry_unused);
419}
420
421/*
422 * These can only be called under the global LRU lock, ie during the
423 * callback for freeing the LRU list. "isolate" removes it from the
424 * LRU lists entirely, while shrink_move moves it to the indicated
425 * private list.
426 */
Vladimir Davydov3f97b162015-02-12 14:59:35 -0800427static void d_lru_isolate(struct list_lru_one *lru, struct dentry *dentry)
Linus Torvalds89dc77b2013-09-13 22:55:10 -0400428{
429 D_FLAG_VERIFY(dentry, DCACHE_LRU_LIST);
430 dentry->d_flags &= ~DCACHE_LRU_LIST;
431 this_cpu_dec(nr_dentry_unused);
Vladimir Davydov3f97b162015-02-12 14:59:35 -0800432 list_lru_isolate(lru, &dentry->d_lru);
Linus Torvalds89dc77b2013-09-13 22:55:10 -0400433}
434
Vladimir Davydov3f97b162015-02-12 14:59:35 -0800435static void d_lru_shrink_move(struct list_lru_one *lru, struct dentry *dentry,
436 struct list_head *list)
Linus Torvalds89dc77b2013-09-13 22:55:10 -0400437{
438 D_FLAG_VERIFY(dentry, DCACHE_LRU_LIST);
439 dentry->d_flags |= DCACHE_SHRINK_LIST;
Vladimir Davydov3f97b162015-02-12 14:59:35 -0800440 list_lru_isolate_move(lru, &dentry->d_lru, list);
Linus Torvalds89dc77b2013-09-13 22:55:10 -0400441}
442
443/*
Dave Chinnerf6041562013-08-28 10:18:00 +1000444 * dentry_lru_(add|del)_list) must be called with d_lock held.
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700445 */
446static void dentry_lru_add(struct dentry *dentry)
447{
Linus Torvalds89dc77b2013-09-13 22:55:10 -0400448 if (unlikely(!(dentry->d_flags & DCACHE_LRU_LIST)))
449 d_lru_add(dentry);
Josef Bacik563f4002017-04-18 16:04:17 -0400450 else if (unlikely(!(dentry->d_flags & DCACHE_REFERENCED)))
451 dentry->d_flags |= DCACHE_REFERENCED;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700452}
453
Miklos Szeredid52b9082007-05-08 00:23:46 -0700454/**
Nick Piggin789680d2011-01-07 17:49:30 +1100455 * d_drop - drop a dentry
456 * @dentry: dentry to drop
457 *
458 * d_drop() unhashes the entry from the parent dentry hashes, so that it won't
459 * be found through a VFS lookup any more. Note that this is different from
460 * deleting the dentry - d_delete will try to mark the dentry negative if
461 * possible, giving a successful _negative_ lookup, while d_drop will
462 * just make the cache lookup fail.
463 *
464 * d_drop() is used mainly for stuff that wants to invalidate a dentry for some
465 * reason (NFS timeouts or autofs deletes).
466 *
NeilBrown61647822017-11-10 15:45:41 +1100467 * __d_drop requires dentry->d_lock
468 * ___d_drop doesn't mark dentry as "unhashed"
469 * (dentry->d_hash.pprev will be LIST_POISON2, not NULL).
Nick Piggin789680d2011-01-07 17:49:30 +1100470 */
NeilBrown61647822017-11-10 15:45:41 +1100471static void ___d_drop(struct dentry *dentry)
Nick Piggin789680d2011-01-07 17:49:30 +1100472{
Al Viro0632a9a2018-03-07 00:49:10 -0500473 struct hlist_bl_head *b;
474 /*
475 * Hashed dentries are normally on the dentry hashtable,
476 * with the exception of those newly allocated by
477 * d_obtain_root, which are always IS_ROOT:
478 */
479 if (unlikely(IS_ROOT(dentry)))
480 b = &dentry->d_sb->s_roots;
481 else
482 b = d_hash(dentry->d_name.hash);
Al Virob61625d2013-10-04 11:09:01 -0400483
Al Viro0632a9a2018-03-07 00:49:10 -0500484 hlist_bl_lock(b);
485 __hlist_bl_del(&dentry->d_hash);
486 hlist_bl_unlock(b);
Nick Piggin789680d2011-01-07 17:49:30 +1100487}
NeilBrown61647822017-11-10 15:45:41 +1100488
489void __d_drop(struct dentry *dentry)
490{
Al Viro0632a9a2018-03-07 00:49:10 -0500491 if (!d_unhashed(dentry)) {
492 ___d_drop(dentry);
493 dentry->d_hash.pprev = NULL;
494 write_seqcount_invalidate(&dentry->d_seq);
495 }
NeilBrown61647822017-11-10 15:45:41 +1100496}
Nick Piggin789680d2011-01-07 17:49:30 +1100497EXPORT_SYMBOL(__d_drop);
498
499void d_drop(struct dentry *dentry)
500{
Nick Piggin789680d2011-01-07 17:49:30 +1100501 spin_lock(&dentry->d_lock);
502 __d_drop(dentry);
503 spin_unlock(&dentry->d_lock);
Nick Piggin789680d2011-01-07 17:49:30 +1100504}
505EXPORT_SYMBOL(d_drop);
506
Al Viroba65dc52016-06-10 11:32:47 -0400507static inline void dentry_unlist(struct dentry *dentry, struct dentry *parent)
508{
509 struct dentry *next;
510 /*
511 * Inform d_walk() and shrink_dentry_list() that we are no longer
512 * attached to the dentry tree
513 */
514 dentry->d_flags |= DCACHE_DENTRY_KILLED;
515 if (unlikely(list_empty(&dentry->d_child)))
516 return;
517 __list_del_entry(&dentry->d_child);
518 /*
519 * Cursors can move around the list of children. While we'd been
520 * a normal list member, it didn't matter - ->d_child.next would've
521 * been updated. However, from now on it won't be and for the
522 * things like d_walk() it might end up with a nasty surprise.
523 * Normally d_walk() doesn't care about cursors moving around -
524 * ->d_lock on parent prevents that and since a cursor has no children
525 * of its own, we get through it without ever unlocking the parent.
526 * There is one exception, though - if we ascend from a child that
527 * gets killed as soon as we unlock it, the next sibling is found
528 * using the value left in its ->d_child.next. And if _that_
529 * pointed to a cursor, and cursor got moved (e.g. by lseek())
530 * before d_walk() regains parent->d_lock, we'll end up skipping
531 * everything the cursor had been moved past.
532 *
533 * Solution: make sure that the pointer left behind in ->d_child.next
534 * points to something that won't be moving around. I.e. skip the
535 * cursors.
536 */
537 while (dentry->d_child.next != &parent->d_subdirs) {
538 next = list_entry(dentry->d_child.next, struct dentry, d_child);
539 if (likely(!(next->d_flags & DCACHE_DENTRY_CURSOR)))
540 break;
541 dentry->d_child.next = next->d_child.next;
542 }
543}
544
Al Viroe55fd012014-05-28 13:51:12 -0400545static void __dentry_kill(struct dentry *dentry)
Nick Piggin77812a12011-01-07 17:49:48 +1100546{
Al Viro41edf2782014-05-01 10:30:00 -0400547 struct dentry *parent = NULL;
548 bool can_free = true;
Al Viro41edf2782014-05-01 10:30:00 -0400549 if (!IS_ROOT(dentry))
Nick Piggin77812a12011-01-07 17:49:48 +1100550 parent = dentry->d_parent;
Nick Piggin31e6b012011-01-07 17:49:52 +1100551
Linus Torvalds0d984392013-09-08 13:46:52 -0700552 /*
553 * The dentry is now unrecoverably dead to the world.
554 */
555 lockref_mark_dead(&dentry->d_lockref);
556
Sage Weilf0023bc2011-10-28 10:02:42 -0700557 /*
Sage Weilf0023bc2011-10-28 10:02:42 -0700558 * inform the fs via d_prune that this dentry is about to be
559 * unhashed and destroyed.
560 */
Al Viro29266202014-05-30 11:39:02 -0400561 if (dentry->d_flags & DCACHE_OP_PRUNE)
Yan, Zheng61572bb2013-04-15 14:13:21 +0800562 dentry->d_op->d_prune(dentry);
563
Al Viro01b60352014-04-29 23:42:52 -0400564 if (dentry->d_flags & DCACHE_LRU_LIST) {
565 if (!(dentry->d_flags & DCACHE_SHRINK_LIST))
566 d_lru_del(dentry);
Al Viro01b60352014-04-29 23:42:52 -0400567 }
Nick Piggin77812a12011-01-07 17:49:48 +1100568 /* if it was on the hash then remove it */
569 __d_drop(dentry);
Al Viroba65dc52016-06-10 11:32:47 -0400570 dentry_unlist(dentry, parent);
Al Viro03b3b882014-04-29 15:45:28 -0400571 if (parent)
572 spin_unlock(&parent->d_lock);
Al Viro550dce02016-05-29 20:13:30 -0400573 if (dentry->d_inode)
574 dentry_unlink_inode(dentry);
575 else
576 spin_unlock(&dentry->d_lock);
Al Viro03b3b882014-04-29 15:45:28 -0400577 this_cpu_dec(nr_dentry);
578 if (dentry->d_op && dentry->d_op->d_release)
579 dentry->d_op->d_release(dentry);
580
Al Viro41edf2782014-05-01 10:30:00 -0400581 spin_lock(&dentry->d_lock);
582 if (dentry->d_flags & DCACHE_SHRINK_LIST) {
583 dentry->d_flags |= DCACHE_MAY_FREE;
584 can_free = false;
585 }
586 spin_unlock(&dentry->d_lock);
Al Viro41edf2782014-05-01 10:30:00 -0400587 if (likely(can_free))
588 dentry_free(dentry);
Al Viroe55fd012014-05-28 13:51:12 -0400589}
590
Al Viro046b9612014-05-29 08:54:52 -0400591static inline struct dentry *lock_parent(struct dentry *dentry)
592{
593 struct dentry *parent = dentry->d_parent;
594 if (IS_ROOT(dentry))
595 return NULL;
Linus Torvalds360f5472015-01-09 15:19:03 -0800596 if (unlikely(dentry->d_lockref.count < 0))
Al Viroc2338f22014-06-12 00:29:13 -0400597 return NULL;
Al Viro046b9612014-05-29 08:54:52 -0400598 if (likely(spin_trylock(&parent->d_lock)))
599 return parent;
Al Viro046b9612014-05-29 08:54:52 -0400600 rcu_read_lock();
Al Viroc2338f22014-06-12 00:29:13 -0400601 spin_unlock(&dentry->d_lock);
Al Viro046b9612014-05-29 08:54:52 -0400602again:
Mark Rutland66702eb2017-10-23 14:07:14 -0700603 parent = READ_ONCE(dentry->d_parent);
Al Viro046b9612014-05-29 08:54:52 -0400604 spin_lock(&parent->d_lock);
605 /*
606 * We can't blindly lock dentry until we are sure
607 * that we won't violate the locking order.
608 * Any changes of dentry->d_parent must have
609 * been done with parent->d_lock held, so
610 * spin_lock() above is enough of a barrier
611 * for checking if it's still our child.
612 */
613 if (unlikely(parent != dentry->d_parent)) {
614 spin_unlock(&parent->d_lock);
615 goto again;
616 }
Al Viro3b821402018-02-23 20:47:17 -0500617 if (parent != dentry) {
Linus Torvalds9f126002014-05-31 09:13:21 -0700618 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Al Viro3b821402018-02-23 20:47:17 -0500619 if (unlikely(dentry->d_lockref.count < 0)) {
620 spin_unlock(&parent->d_lock);
621 parent = NULL;
622 }
623 } else {
Al Viro046b9612014-05-29 08:54:52 -0400624 parent = NULL;
Al Viro3b821402018-02-23 20:47:17 -0500625 }
626 rcu_read_unlock();
Al Viro046b9612014-05-29 08:54:52 -0400627 return parent;
628}
629
Linus Torvalds360f5472015-01-09 15:19:03 -0800630/*
John Ognessc1d0c1a2018-02-23 00:50:21 +0100631 * Finish off a dentry we've decided to kill.
632 * dentry->d_lock must be held, returns with it unlocked.
633 * Returns dentry requiring refcount drop, or NULL if we're done.
634 */
635static struct dentry *dentry_kill(struct dentry *dentry)
636 __releases(dentry->d_lock)
637{
638 struct inode *inode = dentry->d_inode;
639 struct dentry *parent = NULL;
640
641 if (inode && unlikely(!spin_trylock(&inode->i_lock)))
642 goto failed;
643
644 if (!IS_ROOT(dentry)) {
645 parent = dentry->d_parent;
646 if (unlikely(!spin_trylock(&parent->d_lock))) {
647 if (inode)
648 spin_unlock(&inode->i_lock);
649 goto failed;
650 }
651 }
652
653 __dentry_kill(dentry);
654 return parent;
655
656failed:
657 spin_unlock(&dentry->d_lock);
658 return dentry; /* try again with same dentry */
659}
660
661/*
Linus Torvalds360f5472015-01-09 15:19:03 -0800662 * Try to do a lockless dput(), and return whether that was successful.
663 *
664 * If unsuccessful, we return false, having already taken the dentry lock.
665 *
666 * The caller needs to hold the RCU read lock, so that the dentry is
667 * guaranteed to stay around even if the refcount goes down to zero!
668 */
669static inline bool fast_dput(struct dentry *dentry)
670{
671 int ret;
672 unsigned int d_flags;
673
674 /*
675 * If we have a d_op->d_delete() operation, we sould not
Al Viro75a6f822015-07-08 02:42:38 +0100676 * let the dentry count go to zero, so use "put_or_lock".
Linus Torvalds360f5472015-01-09 15:19:03 -0800677 */
678 if (unlikely(dentry->d_flags & DCACHE_OP_DELETE))
679 return lockref_put_or_lock(&dentry->d_lockref);
680
681 /*
682 * .. otherwise, we can try to just decrement the
683 * lockref optimistically.
684 */
685 ret = lockref_put_return(&dentry->d_lockref);
686
687 /*
688 * If the lockref_put_return() failed due to the lock being held
689 * by somebody else, the fast path has failed. We will need to
690 * get the lock, and then check the count again.
691 */
692 if (unlikely(ret < 0)) {
693 spin_lock(&dentry->d_lock);
694 if (dentry->d_lockref.count > 1) {
695 dentry->d_lockref.count--;
696 spin_unlock(&dentry->d_lock);
697 return 1;
698 }
699 return 0;
700 }
701
702 /*
703 * If we weren't the last ref, we're done.
704 */
705 if (ret)
706 return 1;
707
708 /*
709 * Careful, careful. The reference count went down
710 * to zero, but we don't hold the dentry lock, so
711 * somebody else could get it again, and do another
712 * dput(), and we need to not race with that.
713 *
714 * However, there is a very special and common case
715 * where we don't care, because there is nothing to
716 * do: the dentry is still hashed, it does not have
717 * a 'delete' op, and it's referenced and already on
718 * the LRU list.
719 *
720 * NOTE! Since we aren't locked, these values are
721 * not "stable". However, it is sufficient that at
722 * some point after we dropped the reference the
723 * dentry was hashed and the flags had the proper
724 * value. Other dentry users may have re-gotten
725 * a reference to the dentry and change that, but
726 * our work is done - we can leave the dentry
727 * around with a zero refcount.
728 */
729 smp_rmb();
Mark Rutland66702eb2017-10-23 14:07:14 -0700730 d_flags = READ_ONCE(dentry->d_flags);
Al Viro75a6f822015-07-08 02:42:38 +0100731 d_flags &= DCACHE_REFERENCED | DCACHE_LRU_LIST | DCACHE_DISCONNECTED;
Linus Torvalds360f5472015-01-09 15:19:03 -0800732
733 /* Nothing to do? Dropping the reference was all we needed? */
734 if (d_flags == (DCACHE_REFERENCED | DCACHE_LRU_LIST) && !d_unhashed(dentry))
735 return 1;
736
737 /*
738 * Not the fast normal case? Get the lock. We've already decremented
739 * the refcount, but we'll need to re-check the situation after
740 * getting the lock.
741 */
742 spin_lock(&dentry->d_lock);
743
744 /*
745 * Did somebody else grab a reference to it in the meantime, and
746 * we're no longer the last user after all? Alternatively, somebody
747 * else could have killed it and marked it dead. Either way, we
748 * don't need to do anything else.
749 */
750 if (dentry->d_lockref.count) {
751 spin_unlock(&dentry->d_lock);
752 return 1;
753 }
754
755 /*
756 * Re-get the reference we optimistically dropped. We hold the
757 * lock, and we just tested that it was zero, so we can just
758 * set it to 1.
759 */
760 dentry->d_lockref.count = 1;
761 return 0;
762}
763
764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765/*
766 * This is dput
767 *
768 * This is complicated by the fact that we do not want to put
769 * dentries that are no longer on any hash chain on the unused
770 * list: we'd much rather just get rid of them immediately.
771 *
772 * However, that implies that we have to traverse the dentry
773 * tree upwards to the parents which might _also_ now be
774 * scheduled for deletion (it may have been only waiting for
775 * its last child to go away).
776 *
777 * This tail recursion is done by hand as we don't want to depend
778 * on the compiler to always get this right (gcc generally doesn't).
779 * Real recursion would eat up our stack space.
780 */
781
782/*
783 * dput - release a dentry
784 * @dentry: dentry to release
785 *
786 * Release a dentry. This will drop the usage count and if appropriate
787 * call the dentry unlink method as well as removing it from the queues and
788 * releasing its resources. If the parent dentries were scheduled for release
789 * they too may now get deleted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791void dput(struct dentry *dentry)
792{
Linus Torvalds8aab6a22013-09-08 13:26:18 -0700793 if (unlikely(!dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 return;
795
796repeat:
Wei Fang47be6182016-07-06 11:32:20 +0800797 might_sleep();
798
Linus Torvalds360f5472015-01-09 15:19:03 -0800799 rcu_read_lock();
800 if (likely(fast_dput(dentry))) {
801 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 return;
Linus Torvalds360f5472015-01-09 15:19:03 -0800803 }
804
805 /* Slow case: now with the dentry lock held */
806 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Al Viro85c7f812016-04-14 19:52:13 -0400808 WARN_ON(d_in_lookup(dentry));
809
Linus Torvalds8aab6a22013-09-08 13:26:18 -0700810 /* Unreachable? Get rid of it */
811 if (unlikely(d_unhashed(dentry)))
812 goto kill_it;
813
Al Viro75a6f822015-07-08 02:42:38 +0100814 if (unlikely(dentry->d_flags & DCACHE_DISCONNECTED))
815 goto kill_it;
816
Linus Torvalds8aab6a22013-09-08 13:26:18 -0700817 if (unlikely(dentry->d_flags & DCACHE_OP_DELETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 if (dentry->d_op->d_delete(dentry))
Nick Piggin61f3dee2011-01-07 17:49:40 +1100819 goto kill_it;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 }
Nick Piggin265ac902010-10-10 05:36:24 -0400821
Christoph Hellwiga4633352010-10-10 05:36:26 -0400822 dentry_lru_add(dentry);
Nick Piggin265ac902010-10-10 05:36:24 -0400823
Waiman Long98474232013-08-28 18:24:59 -0700824 dentry->d_lockref.count--;
Nick Piggin61f3dee2011-01-07 17:49:40 +1100825 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 return;
827
Miklos Szeredid52b9082007-05-08 00:23:46 -0700828kill_it:
Al Viro8cbf74d2014-05-29 09:18:26 -0400829 dentry = dentry_kill(dentry);
Wei Fang47be6182016-07-06 11:32:20 +0800830 if (dentry) {
831 cond_resched();
Miklos Szeredid52b9082007-05-08 00:23:46 -0700832 goto repeat;
Wei Fang47be6182016-07-06 11:32:20 +0800833 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700835EXPORT_SYMBOL(dput);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100838/* This must be called with d_lock held */
Nick Piggindc0474b2011-01-07 17:49:43 +1100839static inline void __dget_dlock(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
Waiman Long98474232013-08-28 18:24:59 -0700841 dentry->d_lockref.count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842}
843
Nick Piggindc0474b2011-01-07 17:49:43 +1100844static inline void __dget(struct dentry *dentry)
Nick Piggin23044502011-01-07 17:49:31 +1100845{
Waiman Long98474232013-08-28 18:24:59 -0700846 lockref_get(&dentry->d_lockref);
Nick Piggin23044502011-01-07 17:49:31 +1100847}
848
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100849struct dentry *dget_parent(struct dentry *dentry)
850{
Waiman Longdf3d0bb2013-09-02 11:29:22 -0700851 int gotref;
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100852 struct dentry *ret;
853
Waiman Longdf3d0bb2013-09-02 11:29:22 -0700854 /*
855 * Do optimistic parent lookup without any
856 * locking.
857 */
858 rcu_read_lock();
Mark Rutland66702eb2017-10-23 14:07:14 -0700859 ret = READ_ONCE(dentry->d_parent);
Waiman Longdf3d0bb2013-09-02 11:29:22 -0700860 gotref = lockref_get_not_zero(&ret->d_lockref);
861 rcu_read_unlock();
862 if (likely(gotref)) {
Mark Rutland66702eb2017-10-23 14:07:14 -0700863 if (likely(ret == READ_ONCE(dentry->d_parent)))
Waiman Longdf3d0bb2013-09-02 11:29:22 -0700864 return ret;
865 dput(ret);
866 }
867
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100868repeat:
Nick Piggina734eb42011-01-07 17:49:44 +1100869 /*
870 * Don't need rcu_dereference because we re-check it was correct under
871 * the lock.
872 */
873 rcu_read_lock();
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100874 ret = dentry->d_parent;
Nick Piggina734eb42011-01-07 17:49:44 +1100875 spin_lock(&ret->d_lock);
876 if (unlikely(ret != dentry->d_parent)) {
877 spin_unlock(&ret->d_lock);
878 rcu_read_unlock();
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100879 goto repeat;
880 }
Nick Piggina734eb42011-01-07 17:49:44 +1100881 rcu_read_unlock();
Waiman Long98474232013-08-28 18:24:59 -0700882 BUG_ON(!ret->d_lockref.count);
883 ret->d_lockref.count++;
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100884 spin_unlock(&ret->d_lock);
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100885 return ret;
886}
887EXPORT_SYMBOL(dget_parent);
888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889/**
890 * d_find_alias - grab a hashed alias of inode
891 * @inode: inode in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 *
893 * If inode has a hashed alias, or is a directory and has any alias,
894 * acquire the reference to alias and return it. Otherwise return NULL.
895 * Notice that if inode is a directory there can be only one alias and
896 * it can be unhashed only if it has no children, or if it is the root
Eric W. Biederman3ccb3542014-02-12 16:08:06 -0800897 * of a filesystem, or if the directory was renamed and d_revalidate
898 * was the first vfs operation to notice.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 *
NeilBrown21c0d8f2006-10-04 02:16:16 -0700900 * If the inode has an IS_ROOT, DCACHE_DISCONNECTED alias, then prefer
J. Bruce Fields52ed46f2014-01-16 11:15:51 -0500901 * any other hashed alias over that one.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 */
J. Bruce Fields52ed46f2014-01-16 11:15:51 -0500903static struct dentry *__d_find_alias(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
Nick Pigginda502952011-01-07 17:49:33 +1100905 struct dentry *alias, *discon_alias;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Nick Pigginda502952011-01-07 17:49:33 +1100907again:
908 discon_alias = NULL;
Al Viro946e51f2014-10-26 19:19:16 -0400909 hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
Nick Pigginda502952011-01-07 17:49:33 +1100910 spin_lock(&alias->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
NeilBrown21c0d8f2006-10-04 02:16:16 -0700912 if (IS_ROOT(alias) &&
Nick Pigginda502952011-01-07 17:49:33 +1100913 (alias->d_flags & DCACHE_DISCONNECTED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 discon_alias = alias;
J. Bruce Fields52ed46f2014-01-16 11:15:51 -0500915 } else {
Nick Piggindc0474b2011-01-07 17:49:43 +1100916 __dget_dlock(alias);
Nick Pigginda502952011-01-07 17:49:33 +1100917 spin_unlock(&alias->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 return alias;
919 }
920 }
Nick Pigginda502952011-01-07 17:49:33 +1100921 spin_unlock(&alias->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 }
Nick Pigginda502952011-01-07 17:49:33 +1100923 if (discon_alias) {
924 alias = discon_alias;
925 spin_lock(&alias->d_lock);
926 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
J. Bruce Fields8d80d7d2014-01-16 17:17:31 -0500927 __dget_dlock(alias);
928 spin_unlock(&alias->d_lock);
929 return alias;
Nick Pigginda502952011-01-07 17:49:33 +1100930 }
931 spin_unlock(&alias->d_lock);
932 goto again;
933 }
934 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935}
936
Nick Pigginda502952011-01-07 17:49:33 +1100937struct dentry *d_find_alias(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938{
David Howells214fda12006-03-25 03:06:36 -0800939 struct dentry *de = NULL;
940
Al Virob3d9b7a2012-06-09 13:51:19 -0400941 if (!hlist_empty(&inode->i_dentry)) {
Nick Piggin873feea2011-01-07 17:50:06 +1100942 spin_lock(&inode->i_lock);
J. Bruce Fields52ed46f2014-01-16 11:15:51 -0500943 de = __d_find_alias(inode);
Nick Piggin873feea2011-01-07 17:50:06 +1100944 spin_unlock(&inode->i_lock);
David Howells214fda12006-03-25 03:06:36 -0800945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 return de;
947}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700948EXPORT_SYMBOL(d_find_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
950/*
951 * Try to kill dentries associated with this inode.
952 * WARNING: you must own a reference to inode.
953 */
954void d_prune_aliases(struct inode *inode)
955{
Domen Puncer0cdca3f2005-09-10 00:27:07 -0700956 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957restart:
Nick Piggin873feea2011-01-07 17:50:06 +1100958 spin_lock(&inode->i_lock);
Al Viro946e51f2014-10-26 19:19:16 -0400959 hlist_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 spin_lock(&dentry->d_lock);
Waiman Long98474232013-08-28 18:24:59 -0700961 if (!dentry->d_lockref.count) {
Al Viro29355c32014-05-30 11:25:30 -0400962 struct dentry *parent = lock_parent(dentry);
963 if (likely(!dentry->d_lockref.count)) {
964 __dentry_kill(dentry);
Yan, Zheng4a7795d2014-11-19 15:50:34 +0800965 dput(parent);
Al Viro29355c32014-05-30 11:25:30 -0400966 goto restart;
967 }
968 if (parent)
969 spin_unlock(&parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 }
971 spin_unlock(&dentry->d_lock);
972 }
Nick Piggin873feea2011-01-07 17:50:06 +1100973 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700975EXPORT_SYMBOL(d_prune_aliases);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Al Viro3b3f09f2018-02-23 21:54:18 -0500977/*
978 * Lock a dentry from shrink list.
979 * Note that dentry is *not* protected from concurrent dentry_kill(),
980 * d_delete(), etc. It is protected from freeing (by the fact of
981 * being on a shrink list), but everything else is fair game.
982 * Return false if dentry has been disrupted or grabbed, leaving
983 * the caller to kick it off-list. Otherwise, return true and have
984 * that dentry's inode and parent both locked.
985 */
986static bool shrink_lock_dentry(struct dentry *dentry)
987{
988 struct inode *inode;
989 struct dentry *parent;
990
991 if (dentry->d_lockref.count)
992 return false;
993
994 inode = dentry->d_inode;
995 if (inode && unlikely(!spin_trylock(&inode->i_lock))) {
996 rcu_read_lock(); /* to protect inode */
997 spin_unlock(&dentry->d_lock);
998 spin_lock(&inode->i_lock);
999 spin_lock(&dentry->d_lock);
1000 if (unlikely(dentry->d_lockref.count))
1001 goto out;
1002 /* changed inode means that somebody had grabbed it */
1003 if (unlikely(inode != dentry->d_inode))
1004 goto out;
1005 rcu_read_unlock();
1006 }
1007
1008 parent = dentry->d_parent;
1009 if (IS_ROOT(dentry) || likely(spin_trylock(&parent->d_lock)))
1010 return true;
1011
1012 rcu_read_lock(); /* to protect parent */
1013 spin_unlock(&dentry->d_lock);
1014 parent = READ_ONCE(dentry->d_parent);
1015 spin_lock(&parent->d_lock);
1016 if (unlikely(parent != dentry->d_parent)) {
1017 spin_unlock(&parent->d_lock);
1018 spin_lock(&dentry->d_lock);
1019 goto out;
1020 }
1021 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
1022 if (likely(!dentry->d_lockref.count)) {
1023 rcu_read_unlock();
1024 return true;
1025 }
1026 spin_unlock(&parent->d_lock);
1027out:
1028 if (inode)
1029 spin_unlock(&inode->i_lock);
1030 rcu_read_unlock();
1031 return false;
1032}
1033
Christoph Hellwig3049cfe2010-10-10 05:36:25 -04001034static void shrink_dentry_list(struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035{
Miklos Szeredi60942f22014-05-02 15:38:39 -04001036 while (!list_empty(list)) {
Al Viro3b3f09f2018-02-23 21:54:18 -05001037 struct dentry *dentry, *parent;
Al Viroff2fde92014-05-28 13:59:13 -04001038 struct inode *inode;
Al Viro3b3f09f2018-02-23 21:54:18 -05001039
Miklos Szeredi60942f22014-05-02 15:38:39 -04001040 dentry = list_entry(list->prev, struct dentry, d_lru);
Nick Pigginec336792011-01-07 17:49:47 +11001041 spin_lock(&dentry->d_lock);
Al Viro3b3f09f2018-02-23 21:54:18 -05001042 if (!shrink_lock_dentry(dentry)) {
1043 bool can_free = false;
1044 d_shrink_del(dentry);
1045 if (dentry->d_lockref.count < 0)
1046 can_free = dentry->d_flags & DCACHE_MAY_FREE;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -07001047 spin_unlock(&dentry->d_lock);
Al Viro64fd72e2014-05-28 09:48:44 -04001048 if (can_free)
1049 dentry_free(dentry);
1050 continue;
1051 }
Al Viro3b3f09f2018-02-23 21:54:18 -05001052 d_shrink_del(dentry);
1053 parent = dentry->d_parent;
Al Viroff2fde92014-05-28 13:59:13 -04001054 __dentry_kill(dentry);
Al Viro3b3f09f2018-02-23 21:54:18 -05001055 if (parent == dentry)
1056 continue;
Al Viro5c47e6d2014-04-29 16:13:18 -04001057 /*
1058 * We need to prune ancestors too. This is necessary to prevent
1059 * quadratic behavior of shrink_dcache_parent(), but is also
1060 * expected to be beneficial in reducing dentry cache
1061 * fragmentation.
1062 */
1063 dentry = parent;
Al Virob2b80192014-05-29 09:11:45 -04001064 while (dentry && !lockref_put_or_lock(&dentry->d_lockref)) {
1065 parent = lock_parent(dentry);
1066 if (dentry->d_lockref.count != 1) {
1067 dentry->d_lockref.count--;
1068 spin_unlock(&dentry->d_lock);
1069 if (parent)
1070 spin_unlock(&parent->d_lock);
1071 break;
1072 }
1073 inode = dentry->d_inode; /* can't be NULL */
1074 if (unlikely(!spin_trylock(&inode->i_lock))) {
1075 spin_unlock(&dentry->d_lock);
1076 if (parent)
1077 spin_unlock(&parent->d_lock);
1078 cpu_relax();
1079 continue;
1080 }
1081 __dentry_kill(dentry);
1082 dentry = parent;
1083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 }
Christoph Hellwig3049cfe2010-10-10 05:36:25 -04001085}
1086
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001087static enum lru_status dentry_lru_isolate(struct list_head *item,
1088 struct list_lru_one *lru, spinlock_t *lru_lock, void *arg)
Dave Chinnerf6041562013-08-28 10:18:00 +10001089{
1090 struct list_head *freeable = arg;
1091 struct dentry *dentry = container_of(item, struct dentry, d_lru);
1092
1093
1094 /*
1095 * we are inverting the lru lock/dentry->d_lock here,
1096 * so use a trylock. If we fail to get the lock, just skip
1097 * it
1098 */
1099 if (!spin_trylock(&dentry->d_lock))
1100 return LRU_SKIP;
1101
1102 /*
1103 * Referenced dentries are still in use. If they have active
1104 * counts, just remove them from the LRU. Otherwise give them
1105 * another pass through the LRU.
1106 */
1107 if (dentry->d_lockref.count) {
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001108 d_lru_isolate(lru, dentry);
Dave Chinnerf6041562013-08-28 10:18:00 +10001109 spin_unlock(&dentry->d_lock);
1110 return LRU_REMOVED;
1111 }
1112
1113 if (dentry->d_flags & DCACHE_REFERENCED) {
1114 dentry->d_flags &= ~DCACHE_REFERENCED;
1115 spin_unlock(&dentry->d_lock);
1116
1117 /*
1118 * The list move itself will be made by the common LRU code. At
1119 * this point, we've dropped the dentry->d_lock but keep the
1120 * lru lock. This is safe to do, since every list movement is
1121 * protected by the lru lock even if both locks are held.
1122 *
1123 * This is guaranteed by the fact that all LRU management
1124 * functions are intermediated by the LRU API calls like
1125 * list_lru_add and list_lru_del. List movement in this file
1126 * only ever occur through this functions or through callbacks
1127 * like this one, that are called from the LRU API.
1128 *
1129 * The only exceptions to this are functions like
1130 * shrink_dentry_list, and code that first checks for the
1131 * DCACHE_SHRINK_LIST flag. Those are guaranteed to be
1132 * operating only with stack provided lists after they are
1133 * properly isolated from the main list. It is thus, always a
1134 * local access.
1135 */
1136 return LRU_ROTATE;
1137 }
1138
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001139 d_lru_shrink_move(lru, dentry, freeable);
Dave Chinnerf6041562013-08-28 10:18:00 +10001140 spin_unlock(&dentry->d_lock);
1141
1142 return LRU_REMOVED;
1143}
1144
Christoph Hellwig3049cfe2010-10-10 05:36:25 -04001145/**
Dave Chinnerb48f03b2011-08-23 18:56:24 +10001146 * prune_dcache_sb - shrink the dcache
1147 * @sb: superblock
Vladimir Davydov503c3582015-02-12 14:58:47 -08001148 * @sc: shrink control, passed to list_lru_shrink_walk()
Christoph Hellwig3049cfe2010-10-10 05:36:25 -04001149 *
Vladimir Davydov503c3582015-02-12 14:58:47 -08001150 * Attempt to shrink the superblock dcache LRU by @sc->nr_to_scan entries. This
1151 * is done when we need more memory and called from the superblock shrinker
Dave Chinnerb48f03b2011-08-23 18:56:24 +10001152 * function.
1153 *
1154 * This function may fail to free any resources if all the dentries are in
1155 * use.
Christoph Hellwig3049cfe2010-10-10 05:36:25 -04001156 */
Vladimir Davydov503c3582015-02-12 14:58:47 -08001157long prune_dcache_sb(struct super_block *sb, struct shrink_control *sc)
Christoph Hellwig3049cfe2010-10-10 05:36:25 -04001158{
Dave Chinnerf6041562013-08-28 10:18:00 +10001159 LIST_HEAD(dispose);
1160 long freed;
Christoph Hellwig3049cfe2010-10-10 05:36:25 -04001161
Vladimir Davydov503c3582015-02-12 14:58:47 -08001162 freed = list_lru_shrink_walk(&sb->s_dentry_lru, sc,
1163 dentry_lru_isolate, &dispose);
Dave Chinnerf6041562013-08-28 10:18:00 +10001164 shrink_dentry_list(&dispose);
Dave Chinner0a234c62013-08-28 10:17:57 +10001165 return freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166}
1167
Glauber Costa4e717f52013-08-28 10:18:03 +10001168static enum lru_status dentry_lru_isolate_shrink(struct list_head *item,
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001169 struct list_lru_one *lru, spinlock_t *lru_lock, void *arg)
Dave Chinnerdd1f6b22013-08-28 10:17:55 +10001170{
Glauber Costa4e717f52013-08-28 10:18:03 +10001171 struct list_head *freeable = arg;
1172 struct dentry *dentry = container_of(item, struct dentry, d_lru);
Dave Chinnerdd1f6b22013-08-28 10:17:55 +10001173
Glauber Costa4e717f52013-08-28 10:18:03 +10001174 /*
1175 * we are inverting the lru lock/dentry->d_lock here,
1176 * so use a trylock. If we fail to get the lock, just skip
1177 * it
1178 */
1179 if (!spin_trylock(&dentry->d_lock))
1180 return LRU_SKIP;
1181
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001182 d_lru_shrink_move(lru, dentry, freeable);
Glauber Costa4e717f52013-08-28 10:18:03 +10001183 spin_unlock(&dentry->d_lock);
1184
1185 return LRU_REMOVED;
Dave Chinnerdd1f6b22013-08-28 10:17:55 +10001186}
1187
Glauber Costa4e717f52013-08-28 10:18:03 +10001188
Kentaro Makitada3bbdd2008-07-23 21:27:13 -07001189/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 * shrink_dcache_sb - shrink dcache for a superblock
1191 * @sb: superblock
1192 *
Christoph Hellwig3049cfe2010-10-10 05:36:25 -04001193 * Shrink the dcache for the specified super block. This is used to free
1194 * the dcache before unmounting a file system.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 */
Christoph Hellwig3049cfe2010-10-10 05:36:25 -04001196void shrink_dcache_sb(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197{
Glauber Costa4e717f52013-08-28 10:18:03 +10001198 long freed;
Christoph Hellwig3049cfe2010-10-10 05:36:25 -04001199
Glauber Costa4e717f52013-08-28 10:18:03 +10001200 do {
1201 LIST_HEAD(dispose);
1202
1203 freed = list_lru_walk(&sb->s_dentry_lru,
Sahitya Tummalab17c0702017-07-10 15:50:00 -07001204 dentry_lru_isolate_shrink, &dispose, 1024);
Glauber Costa4e717f52013-08-28 10:18:03 +10001205
1206 this_cpu_sub(nr_dentry_unused, freed);
1207 shrink_dentry_list(&dispose);
Sahitya Tummalab17c0702017-07-10 15:50:00 -07001208 cond_resched();
1209 } while (list_lru_count(&sb->s_dentry_lru) > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001211EXPORT_SYMBOL(shrink_dcache_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213/**
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001214 * enum d_walk_ret - action to talke during tree walk
1215 * @D_WALK_CONTINUE: contrinue walk
1216 * @D_WALK_QUIT: quit walk
1217 * @D_WALK_NORETRY: quit when retry is needed
1218 * @D_WALK_SKIP: skip this dentry and its children
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 */
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001220enum d_walk_ret {
1221 D_WALK_CONTINUE,
1222 D_WALK_QUIT,
1223 D_WALK_NORETRY,
1224 D_WALK_SKIP,
1225};
1226
1227/**
1228 * d_walk - walk the dentry tree
1229 * @parent: start of walk
1230 * @data: data passed to @enter() and @finish()
1231 * @enter: callback when first entering the dentry
1232 * @finish: callback when successfully finished the walk
1233 *
1234 * The @enter() and @finish() callbacks are called with d_lock held.
1235 */
1236static void d_walk(struct dentry *parent, void *data,
1237 enum d_walk_ret (*enter)(void *, struct dentry *),
1238 void (*finish)(void *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239{
Nick Piggin949854d2011-01-07 17:49:37 +11001240 struct dentry *this_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 struct list_head *next;
Al Viro48f5ec22013-09-09 15:22:25 -04001242 unsigned seq = 0;
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001243 enum d_walk_ret ret;
1244 bool retry = true;
Nick Piggin949854d2011-01-07 17:49:37 +11001245
Nick Piggin58db63d2011-01-07 17:49:39 +11001246again:
Al Viro48f5ec22013-09-09 15:22:25 -04001247 read_seqbegin_or_lock(&rename_lock, &seq);
Nick Piggin58db63d2011-01-07 17:49:39 +11001248 this_parent = parent;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001249 spin_lock(&this_parent->d_lock);
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001250
1251 ret = enter(data, this_parent);
1252 switch (ret) {
1253 case D_WALK_CONTINUE:
1254 break;
1255 case D_WALK_QUIT:
1256 case D_WALK_SKIP:
1257 goto out_unlock;
1258 case D_WALK_NORETRY:
1259 retry = false;
1260 break;
1261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262repeat:
1263 next = this_parent->d_subdirs.next;
1264resume:
1265 while (next != &this_parent->d_subdirs) {
1266 struct list_head *tmp = next;
Al Viro946e51f2014-10-26 19:19:16 -04001267 struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 next = tmp->next;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001269
Al Viroba65dc52016-06-10 11:32:47 -04001270 if (unlikely(dentry->d_flags & DCACHE_DENTRY_CURSOR))
1271 continue;
1272
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001273 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001274
1275 ret = enter(data, dentry);
1276 switch (ret) {
1277 case D_WALK_CONTINUE:
1278 break;
1279 case D_WALK_QUIT:
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001280 spin_unlock(&dentry->d_lock);
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001281 goto out_unlock;
1282 case D_WALK_NORETRY:
1283 retry = false;
1284 break;
1285 case D_WALK_SKIP:
1286 spin_unlock(&dentry->d_lock);
1287 continue;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001288 }
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001289
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 if (!list_empty(&dentry->d_subdirs)) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001291 spin_unlock(&this_parent->d_lock);
1292 spin_release(&dentry->d_lock.dep_map, 1, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 this_parent = dentry;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001294 spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 goto repeat;
1296 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001297 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 }
1299 /*
1300 * All done at this level ... ascend and resume the search.
1301 */
Al Viroca5358e2014-10-26 19:31:10 -04001302 rcu_read_lock();
1303ascend:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 if (this_parent != parent) {
Linus Torvaldsc826cb72011-03-15 15:29:21 -07001305 struct dentry *child = this_parent;
Al Viro31dec132013-10-25 17:04:27 -04001306 this_parent = child->d_parent;
1307
Al Viro31dec132013-10-25 17:04:27 -04001308 spin_unlock(&child->d_lock);
1309 spin_lock(&this_parent->d_lock);
1310
Al Viroca5358e2014-10-26 19:31:10 -04001311 /* might go back up the wrong parent if we have had a rename. */
1312 if (need_seqretry(&rename_lock, seq))
Nick Piggin949854d2011-01-07 17:49:37 +11001313 goto rename_retry;
Al Viro21591842015-05-28 23:09:19 -04001314 /* go into the first sibling still alive */
1315 do {
1316 next = child->d_child.next;
Al Viroca5358e2014-10-26 19:31:10 -04001317 if (next == &this_parent->d_subdirs)
1318 goto ascend;
1319 child = list_entry(next, struct dentry, d_child);
Al Viro21591842015-05-28 23:09:19 -04001320 } while (unlikely(child->d_flags & DCACHE_DENTRY_KILLED));
Al Viro31dec132013-10-25 17:04:27 -04001321 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 goto resume;
1323 }
Al Viroca5358e2014-10-26 19:31:10 -04001324 if (need_seqretry(&rename_lock, seq))
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001325 goto rename_retry;
Al Viroca5358e2014-10-26 19:31:10 -04001326 rcu_read_unlock();
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001327 if (finish)
1328 finish(data);
1329
1330out_unlock:
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001331 spin_unlock(&this_parent->d_lock);
Al Viro48f5ec22013-09-09 15:22:25 -04001332 done_seqretry(&rename_lock, seq);
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001333 return;
Nick Piggin58db63d2011-01-07 17:49:39 +11001334
1335rename_retry:
Al Viroca5358e2014-10-26 19:31:10 -04001336 spin_unlock(&this_parent->d_lock);
1337 rcu_read_unlock();
1338 BUG_ON(seq & 1);
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001339 if (!retry)
1340 return;
Al Viro48f5ec22013-09-09 15:22:25 -04001341 seq = 1;
Nick Piggin58db63d2011-01-07 17:49:39 +11001342 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343}
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001344
Ian Kent01619492016-11-24 08:03:41 +11001345struct check_mount {
1346 struct vfsmount *mnt;
1347 unsigned int mounted;
1348};
1349
1350static enum d_walk_ret path_check_mount(void *data, struct dentry *dentry)
1351{
1352 struct check_mount *info = data;
1353 struct path path = { .mnt = info->mnt, .dentry = dentry };
1354
1355 if (likely(!d_mountpoint(dentry)))
1356 return D_WALK_CONTINUE;
1357 if (__path_is_mountpoint(&path)) {
1358 info->mounted = 1;
1359 return D_WALK_QUIT;
1360 }
1361 return D_WALK_CONTINUE;
1362}
1363
1364/**
1365 * path_has_submounts - check for mounts over a dentry in the
1366 * current namespace.
1367 * @parent: path to check.
1368 *
1369 * Return true if the parent or its subdirectories contain
1370 * a mount point in the current namespace.
1371 */
1372int path_has_submounts(const struct path *parent)
1373{
1374 struct check_mount data = { .mnt = parent->mnt, .mounted = 0 };
1375
1376 read_seqlock_excl(&mount_lock);
1377 d_walk(parent->dentry, &data, path_check_mount, NULL);
1378 read_sequnlock_excl(&mount_lock);
1379
1380 return data.mounted;
1381}
1382EXPORT_SYMBOL(path_has_submounts);
1383
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384/*
Miklos Szeredieed81002013-09-05 14:39:11 +02001385 * Called by mount code to set a mountpoint and check if the mountpoint is
1386 * reachable (e.g. NFS can unhash a directory dentry and then the complete
1387 * subtree can become unreachable).
1388 *
Eric W. Biederman1ffe46d2014-02-13 09:39:37 -08001389 * Only one of d_invalidate() and d_set_mounted() must succeed. For
Miklos Szeredieed81002013-09-05 14:39:11 +02001390 * this reason take rename_lock and d_lock on dentry and ancestors.
1391 */
1392int d_set_mounted(struct dentry *dentry)
1393{
1394 struct dentry *p;
1395 int ret = -ENOENT;
1396 write_seqlock(&rename_lock);
1397 for (p = dentry->d_parent; !IS_ROOT(p); p = p->d_parent) {
Eric W. Biederman1ffe46d2014-02-13 09:39:37 -08001398 /* Need exclusion wrt. d_invalidate() */
Miklos Szeredieed81002013-09-05 14:39:11 +02001399 spin_lock(&p->d_lock);
1400 if (unlikely(d_unhashed(p))) {
1401 spin_unlock(&p->d_lock);
1402 goto out;
1403 }
1404 spin_unlock(&p->d_lock);
1405 }
1406 spin_lock(&dentry->d_lock);
1407 if (!d_unlinked(dentry)) {
Eric W. Biederman3895dbf2017-01-03 14:18:43 +13001408 ret = -EBUSY;
1409 if (!d_mountpoint(dentry)) {
1410 dentry->d_flags |= DCACHE_MOUNTED;
1411 ret = 0;
1412 }
Miklos Szeredieed81002013-09-05 14:39:11 +02001413 }
1414 spin_unlock(&dentry->d_lock);
1415out:
1416 write_sequnlock(&rename_lock);
1417 return ret;
1418}
1419
1420/*
J. Bruce Fieldsfd517902012-09-18 16:35:51 -04001421 * Search the dentry child list of the specified parent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 * and move any unused dentries to the end of the unused
1423 * list for prune_dcache(). We descend to the next level
1424 * whenever the d_subdirs list is non-empty and continue
1425 * searching.
1426 *
1427 * It returns zero iff there are no unused children,
1428 * otherwise it returns the number of children moved to
1429 * the end of the unused list. This may not be the total
1430 * number of unused children, because select_parent can
1431 * drop the lock and return early due to latency
1432 * constraints.
1433 */
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001434
1435struct select_data {
1436 struct dentry *start;
1437 struct list_head dispose;
1438 int found;
1439};
1440
1441static enum d_walk_ret select_collect(void *_data, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442{
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001443 struct select_data *data = _data;
1444 enum d_walk_ret ret = D_WALK_CONTINUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001446 if (data->start == dentry)
1447 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Al Virofe915222014-05-03 00:02:25 -04001449 if (dentry->d_flags & DCACHE_SHRINK_LIST) {
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001450 data->found++;
Al Virofe915222014-05-03 00:02:25 -04001451 } else {
1452 if (dentry->d_flags & DCACHE_LRU_LIST)
1453 d_lru_del(dentry);
1454 if (!dentry->d_lockref.count) {
1455 d_shrink_add(dentry, &data->dispose);
1456 data->found++;
1457 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 }
1459 /*
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001460 * We can return to the caller if we have found some (this
1461 * ensures forward progress). We'll be coming back to find
1462 * the rest.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 */
Al Virofe915222014-05-03 00:02:25 -04001464 if (!list_empty(&data->dispose))
1465 ret = need_resched() ? D_WALK_QUIT : D_WALK_NORETRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466out:
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001467 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468}
1469
1470/**
1471 * shrink_dcache_parent - prune dcache
1472 * @parent: parent of entries to prune
1473 *
1474 * Prune the dcache to remove unused children of the parent dentry.
1475 */
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001476void shrink_dcache_parent(struct dentry *parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477{
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001478 for (;;) {
1479 struct select_data data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001481 INIT_LIST_HEAD(&data.dispose);
1482 data.start = parent;
1483 data.found = 0;
1484
1485 d_walk(parent, &data, select_collect, NULL);
1486 if (!data.found)
1487 break;
1488
1489 shrink_dentry_list(&data.dispose);
Greg Thelen421348f2013-04-30 15:26:48 -07001490 cond_resched();
1491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001493EXPORT_SYMBOL(shrink_dcache_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
Al Viro9c8c10e2014-05-02 20:36:10 -04001495static enum d_walk_ret umount_check(void *_data, struct dentry *dentry)
Al Viro42c32602013-11-08 12:31:16 -05001496{
Al Viro9c8c10e2014-05-02 20:36:10 -04001497 /* it has busy descendents; complain about those instead */
1498 if (!list_empty(&dentry->d_subdirs))
1499 return D_WALK_CONTINUE;
Al Viro42c32602013-11-08 12:31:16 -05001500
Al Viro9c8c10e2014-05-02 20:36:10 -04001501 /* root with refcount 1 is fine */
1502 if (dentry == _data && dentry->d_lockref.count == 1)
1503 return D_WALK_CONTINUE;
1504
1505 printk(KERN_ERR "BUG: Dentry %p{i=%lx,n=%pd} "
1506 " still in use (%d) [unmount of %s %s]\n",
Al Viro42c32602013-11-08 12:31:16 -05001507 dentry,
1508 dentry->d_inode ?
1509 dentry->d_inode->i_ino : 0UL,
Al Viro9c8c10e2014-05-02 20:36:10 -04001510 dentry,
Al Viro42c32602013-11-08 12:31:16 -05001511 dentry->d_lockref.count,
1512 dentry->d_sb->s_type->name,
1513 dentry->d_sb->s_id);
Al Viro9c8c10e2014-05-02 20:36:10 -04001514 WARN_ON(1);
1515 return D_WALK_CONTINUE;
1516}
1517
1518static void do_one_tree(struct dentry *dentry)
1519{
1520 shrink_dcache_parent(dentry);
1521 d_walk(dentry, dentry, umount_check, NULL);
1522 d_drop(dentry);
1523 dput(dentry);
Al Viro42c32602013-11-08 12:31:16 -05001524}
1525
1526/*
1527 * destroy the dentries attached to a superblock on unmounting
1528 */
1529void shrink_dcache_for_umount(struct super_block *sb)
1530{
1531 struct dentry *dentry;
1532
Al Viro9c8c10e2014-05-02 20:36:10 -04001533 WARN(down_read_trylock(&sb->s_umount), "s_umount should've been locked");
Al Viro42c32602013-11-08 12:31:16 -05001534
1535 dentry = sb->s_root;
1536 sb->s_root = NULL;
Al Viro9c8c10e2014-05-02 20:36:10 -04001537 do_one_tree(dentry);
Al Viro42c32602013-11-08 12:31:16 -05001538
NeilBrownf1ee6162017-12-21 09:45:40 +11001539 while (!hlist_bl_empty(&sb->s_roots)) {
1540 dentry = dget(hlist_bl_entry(hlist_bl_first(&sb->s_roots), struct dentry, d_hash));
Al Viro9c8c10e2014-05-02 20:36:10 -04001541 do_one_tree(dentry);
Al Viro42c32602013-11-08 12:31:16 -05001542 }
1543}
1544
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07001545struct detach_data {
1546 struct select_data select;
1547 struct dentry *mountpoint;
1548};
1549static enum d_walk_ret detach_and_collect(void *_data, struct dentry *dentry)
Miklos Szeredi848ac1142013-09-05 11:44:36 +02001550{
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07001551 struct detach_data *data = _data;
Miklos Szeredi848ac1142013-09-05 11:44:36 +02001552
1553 if (d_mountpoint(dentry)) {
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07001554 __dget_dlock(dentry);
1555 data->mountpoint = dentry;
Miklos Szeredi848ac1142013-09-05 11:44:36 +02001556 return D_WALK_QUIT;
1557 }
1558
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07001559 return select_collect(&data->select, dentry);
Miklos Szeredi848ac1142013-09-05 11:44:36 +02001560}
1561
1562static void check_and_drop(void *_data)
1563{
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07001564 struct detach_data *data = _data;
Miklos Szeredi848ac1142013-09-05 11:44:36 +02001565
Al Viro81be24d2017-06-03 07:20:09 +01001566 if (!data->mountpoint && list_empty(&data->select.dispose))
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07001567 __d_drop(data->select.start);
Miklos Szeredi848ac1142013-09-05 11:44:36 +02001568}
1569
1570/**
Eric W. Biederman1ffe46d2014-02-13 09:39:37 -08001571 * d_invalidate - detach submounts, prune dcache, and drop
1572 * @dentry: dentry to invalidate (aka detach, prune and drop)
1573 *
Eric W. Biederman1ffe46d2014-02-13 09:39:37 -08001574 * no dcache lock.
Miklos Szeredi848ac1142013-09-05 11:44:36 +02001575 *
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07001576 * The final d_drop is done as an atomic operation relative to
1577 * rename_lock ensuring there are no races with d_set_mounted. This
1578 * ensures there are no unhashed dentries on the path to a mountpoint.
Miklos Szeredi848ac1142013-09-05 11:44:36 +02001579 */
Eric W. Biederman5542aa22014-02-13 09:46:25 -08001580void d_invalidate(struct dentry *dentry)
Miklos Szeredi848ac1142013-09-05 11:44:36 +02001581{
Eric W. Biederman1ffe46d2014-02-13 09:39:37 -08001582 /*
1583 * If it's already been dropped, return OK.
1584 */
1585 spin_lock(&dentry->d_lock);
1586 if (d_unhashed(dentry)) {
1587 spin_unlock(&dentry->d_lock);
Eric W. Biederman5542aa22014-02-13 09:46:25 -08001588 return;
Eric W. Biederman1ffe46d2014-02-13 09:39:37 -08001589 }
1590 spin_unlock(&dentry->d_lock);
1591
Miklos Szeredi848ac1142013-09-05 11:44:36 +02001592 /* Negative dentries can be dropped without further checks */
1593 if (!dentry->d_inode) {
1594 d_drop(dentry);
Eric W. Biederman5542aa22014-02-13 09:46:25 -08001595 return;
Miklos Szeredi848ac1142013-09-05 11:44:36 +02001596 }
1597
1598 for (;;) {
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07001599 struct detach_data data;
Miklos Szeredi848ac1142013-09-05 11:44:36 +02001600
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07001601 data.mountpoint = NULL;
1602 INIT_LIST_HEAD(&data.select.dispose);
1603 data.select.start = dentry;
1604 data.select.found = 0;
Miklos Szeredi848ac1142013-09-05 11:44:36 +02001605
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07001606 d_walk(dentry, &data, detach_and_collect, check_and_drop);
Miklos Szeredi848ac1142013-09-05 11:44:36 +02001607
Al Viro81be24d2017-06-03 07:20:09 +01001608 if (!list_empty(&data.select.dispose))
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07001609 shrink_dentry_list(&data.select.dispose);
Al Viro81be24d2017-06-03 07:20:09 +01001610 else if (!data.mountpoint)
1611 return;
Miklos Szeredi848ac1142013-09-05 11:44:36 +02001612
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07001613 if (data.mountpoint) {
1614 detach_mounts(data.mountpoint);
1615 dput(data.mountpoint);
1616 }
Miklos Szeredi848ac1142013-09-05 11:44:36 +02001617 cond_resched();
1618 }
Miklos Szeredi848ac1142013-09-05 11:44:36 +02001619}
Eric W. Biederman1ffe46d2014-02-13 09:39:37 -08001620EXPORT_SYMBOL(d_invalidate);
Miklos Szeredi848ac1142013-09-05 11:44:36 +02001621
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622/**
Al Viroa4464db2011-07-07 15:03:58 -04001623 * __d_alloc - allocate a dcache entry
1624 * @sb: filesystem it will belong to
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 * @name: qstr of the name
1626 *
1627 * Allocates a dentry. It returns %NULL if there is insufficient memory
1628 * available. On a success the dentry is returned. The name passed in is
1629 * copied and the copy passed in may be reused after this call.
1630 */
1631
Al Viroa4464db2011-07-07 15:03:58 -04001632struct dentry *__d_alloc(struct super_block *sb, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633{
1634 struct dentry *dentry;
1635 char *dname;
Miklos Szeredi285b1022016-06-28 11:47:32 +02001636 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
Mel Gormane12ba742007-10-16 01:25:52 -07001638 dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 if (!dentry)
1640 return NULL;
1641
Linus Torvalds6326c71f2012-05-21 16:14:04 -07001642 /*
1643 * We guarantee that the inline name is always NUL-terminated.
1644 * This way the memcpy() done by the name switching in rename
1645 * will still always have a NUL at the end, even if we might
1646 * be overwriting an internal NUL character
1647 */
1648 dentry->d_iname[DNAME_INLINE_LEN-1] = 0;
Al Viro798434b2016-03-24 20:38:43 -04001649 if (unlikely(!name)) {
David Howellscdf01222017-07-04 17:25:22 +01001650 name = &slash_name;
Al Viro798434b2016-03-24 20:38:43 -04001651 dname = dentry->d_iname;
1652 } else if (name->len > DNAME_INLINE_LEN-1) {
Al Viro8d85b482014-09-29 14:54:27 -04001653 size_t size = offsetof(struct external_name, name[1]);
Vladimir Davydov5d097052016-01-14 15:18:21 -08001654 struct external_name *p = kmalloc(size + name->len,
1655 GFP_KERNEL_ACCOUNT);
Al Viro8d85b482014-09-29 14:54:27 -04001656 if (!p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 kmem_cache_free(dentry_cache, dentry);
1658 return NULL;
1659 }
Al Viro8d85b482014-09-29 14:54:27 -04001660 atomic_set(&p->u.count, 1);
1661 dname = p->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 } else {
1663 dname = dentry->d_iname;
1664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
1666 dentry->d_name.len = name->len;
1667 dentry->d_name.hash = name->hash;
1668 memcpy(dname, name->name, name->len);
1669 dname[name->len] = 0;
1670
Linus Torvalds6326c71f2012-05-21 16:14:04 -07001671 /* Make sure we always see the terminating NUL character */
Paul E. McKenney7088efa2017-10-09 10:04:27 -07001672 smp_store_release(&dentry->d_name.name, dname); /* ^^^ */
Linus Torvalds6326c71f2012-05-21 16:14:04 -07001673
Waiman Long98474232013-08-28 18:24:59 -07001674 dentry->d_lockref.count = 1;
Linus Torvaldsdea36672011-04-24 07:58:46 -07001675 dentry->d_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 spin_lock_init(&dentry->d_lock);
Nick Piggin31e6b012011-01-07 17:49:52 +11001677 seqcount_init(&dentry->d_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 dentry->d_inode = NULL;
Al Viroa4464db2011-07-07 15:03:58 -04001679 dentry->d_parent = dentry;
1680 dentry->d_sb = sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 dentry->d_op = NULL;
1682 dentry->d_fsdata = NULL;
Nick Pigginceb5bdc2011-01-07 17:50:05 +11001683 INIT_HLIST_BL_NODE(&dentry->d_hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 INIT_LIST_HEAD(&dentry->d_lru);
1685 INIT_LIST_HEAD(&dentry->d_subdirs);
Al Viro946e51f2014-10-26 19:19:16 -04001686 INIT_HLIST_NODE(&dentry->d_u.d_alias);
1687 INIT_LIST_HEAD(&dentry->d_child);
Al Viroa4464db2011-07-07 15:03:58 -04001688 d_set_d_op(dentry, dentry->d_sb->s_d_op);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689
Miklos Szeredi285b1022016-06-28 11:47:32 +02001690 if (dentry->d_op && dentry->d_op->d_init) {
1691 err = dentry->d_op->d_init(dentry);
1692 if (err) {
1693 if (dname_external(dentry))
1694 kfree(external_name(dentry));
1695 kmem_cache_free(dentry_cache, dentry);
1696 return NULL;
1697 }
1698 }
1699
Nick Piggin3e880fb2011-01-07 17:49:19 +11001700 this_cpu_inc(nr_dentry);
Christoph Hellwig312d3ca2010-10-10 05:36:23 -04001701
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 return dentry;
1703}
Al Viroa4464db2011-07-07 15:03:58 -04001704
1705/**
1706 * d_alloc - allocate a dcache entry
1707 * @parent: parent of entry to allocate
1708 * @name: qstr of the name
1709 *
1710 * Allocates a dentry. It returns %NULL if there is insufficient memory
1711 * available. On a success the dentry is returned. The name passed in is
1712 * copied and the copy passed in may be reused after this call.
1713 */
1714struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
1715{
1716 struct dentry *dentry = __d_alloc(parent->d_sb, name);
1717 if (!dentry)
1718 return NULL;
Al Viro3d56c252016-06-07 21:26:55 -04001719 dentry->d_flags |= DCACHE_RCUACCESS;
Al Viroa4464db2011-07-07 15:03:58 -04001720 spin_lock(&parent->d_lock);
1721 /*
1722 * don't need child lock because it is not subject
1723 * to concurrency here
1724 */
1725 __dget_dlock(parent);
1726 dentry->d_parent = parent;
Al Viro946e51f2014-10-26 19:19:16 -04001727 list_add(&dentry->d_child, &parent->d_subdirs);
Al Viroa4464db2011-07-07 15:03:58 -04001728 spin_unlock(&parent->d_lock);
1729
1730 return dentry;
1731}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001732EXPORT_SYMBOL(d_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
Miklos Szeredif9c34672018-01-19 11:39:52 +01001734struct dentry *d_alloc_anon(struct super_block *sb)
1735{
1736 return __d_alloc(sb, NULL);
1737}
1738EXPORT_SYMBOL(d_alloc_anon);
1739
Al Viroba65dc52016-06-10 11:32:47 -04001740struct dentry *d_alloc_cursor(struct dentry * parent)
1741{
Miklos Szeredif9c34672018-01-19 11:39:52 +01001742 struct dentry *dentry = d_alloc_anon(parent->d_sb);
Al Viroba65dc52016-06-10 11:32:47 -04001743 if (dentry) {
1744 dentry->d_flags |= DCACHE_RCUACCESS | DCACHE_DENTRY_CURSOR;
1745 dentry->d_parent = dget(parent);
1746 }
1747 return dentry;
1748}
1749
J. Bruce Fieldse1a24bb2012-06-29 16:20:47 -04001750/**
1751 * d_alloc_pseudo - allocate a dentry (for lookup-less filesystems)
1752 * @sb: the superblock
1753 * @name: qstr of the name
1754 *
1755 * For a filesystem that just pins its dentries in memory and never
1756 * performs lookups at all, return an unhashed IS_ROOT dentry.
1757 */
Nick Piggin4b936882011-01-07 17:50:07 +11001758struct dentry *d_alloc_pseudo(struct super_block *sb, const struct qstr *name)
1759{
J. Bruce Fieldse1a24bb2012-06-29 16:20:47 -04001760 return __d_alloc(sb, name);
Nick Piggin4b936882011-01-07 17:50:07 +11001761}
1762EXPORT_SYMBOL(d_alloc_pseudo);
1763
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764struct dentry *d_alloc_name(struct dentry *parent, const char *name)
1765{
1766 struct qstr q;
1767
1768 q.name = name;
Linus Torvalds8387ff22016-06-10 07:51:30 -07001769 q.hash_len = hashlen_string(parent, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 return d_alloc(parent, &q);
1771}
H Hartley Sweetenef26ca92009-09-29 20:09:42 -04001772EXPORT_SYMBOL(d_alloc_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
Nick Pigginfb045ad2011-01-07 17:49:55 +11001774void d_set_d_op(struct dentry *dentry, const struct dentry_operations *op)
1775{
Linus Torvalds6f7f7ca2011-01-14 13:26:18 -08001776 WARN_ON_ONCE(dentry->d_op);
1777 WARN_ON_ONCE(dentry->d_flags & (DCACHE_OP_HASH |
Nick Pigginfb045ad2011-01-07 17:49:55 +11001778 DCACHE_OP_COMPARE |
1779 DCACHE_OP_REVALIDATE |
Jeff Laytonecf3d1f2013-02-20 11:19:05 -05001780 DCACHE_OP_WEAK_REVALIDATE |
David Howells4bacc9c2015-06-18 14:32:31 +01001781 DCACHE_OP_DELETE |
Miklos Szeredid101a122016-03-26 16:14:37 -04001782 DCACHE_OP_REAL));
Nick Pigginfb045ad2011-01-07 17:49:55 +11001783 dentry->d_op = op;
1784 if (!op)
1785 return;
1786 if (op->d_hash)
1787 dentry->d_flags |= DCACHE_OP_HASH;
1788 if (op->d_compare)
1789 dentry->d_flags |= DCACHE_OP_COMPARE;
1790 if (op->d_revalidate)
1791 dentry->d_flags |= DCACHE_OP_REVALIDATE;
Jeff Laytonecf3d1f2013-02-20 11:19:05 -05001792 if (op->d_weak_revalidate)
1793 dentry->d_flags |= DCACHE_OP_WEAK_REVALIDATE;
Nick Pigginfb045ad2011-01-07 17:49:55 +11001794 if (op->d_delete)
1795 dentry->d_flags |= DCACHE_OP_DELETE;
Sage Weilf0023bc2011-10-28 10:02:42 -07001796 if (op->d_prune)
1797 dentry->d_flags |= DCACHE_OP_PRUNE;
Miklos Szeredid101a122016-03-26 16:14:37 -04001798 if (op->d_real)
1799 dentry->d_flags |= DCACHE_OP_REAL;
Nick Pigginfb045ad2011-01-07 17:49:55 +11001800
1801}
1802EXPORT_SYMBOL(d_set_d_op);
1803
David Howellsdf1a0852015-01-29 12:02:28 +00001804
1805/*
1806 * d_set_fallthru - Mark a dentry as falling through to a lower layer
1807 * @dentry - The dentry to mark
1808 *
1809 * Mark a dentry as falling through to the lower layer (as set with
1810 * d_pin_lower()). This flag may be recorded on the medium.
1811 */
1812void d_set_fallthru(struct dentry *dentry)
1813{
1814 spin_lock(&dentry->d_lock);
1815 dentry->d_flags |= DCACHE_FALLTHRU;
1816 spin_unlock(&dentry->d_lock);
1817}
1818EXPORT_SYMBOL(d_set_fallthru);
1819
David Howellsb18825a2013-09-12 19:22:53 +01001820static unsigned d_flags_for_inode(struct inode *inode)
1821{
David Howells44bdb5e2015-01-29 12:02:29 +00001822 unsigned add_flags = DCACHE_REGULAR_TYPE;
David Howellsb18825a2013-09-12 19:22:53 +01001823
1824 if (!inode)
1825 return DCACHE_MISS_TYPE;
1826
1827 if (S_ISDIR(inode->i_mode)) {
1828 add_flags = DCACHE_DIRECTORY_TYPE;
1829 if (unlikely(!(inode->i_opflags & IOP_LOOKUP))) {
1830 if (unlikely(!inode->i_op->lookup))
1831 add_flags = DCACHE_AUTODIR_TYPE;
1832 else
1833 inode->i_opflags |= IOP_LOOKUP;
1834 }
David Howells44bdb5e2015-01-29 12:02:29 +00001835 goto type_determined;
David Howellsb18825a2013-09-12 19:22:53 +01001836 }
1837
David Howells44bdb5e2015-01-29 12:02:29 +00001838 if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) {
Al Viro6b255392015-11-17 10:20:54 -05001839 if (unlikely(inode->i_op->get_link)) {
David Howells44bdb5e2015-01-29 12:02:29 +00001840 add_flags = DCACHE_SYMLINK_TYPE;
1841 goto type_determined;
1842 }
1843 inode->i_opflags |= IOP_NOFOLLOW;
1844 }
1845
1846 if (unlikely(!S_ISREG(inode->i_mode)))
1847 add_flags = DCACHE_SPECIAL_TYPE;
1848
1849type_determined:
David Howellsb18825a2013-09-12 19:22:53 +01001850 if (unlikely(IS_AUTOMOUNT(inode)))
1851 add_flags |= DCACHE_NEED_AUTOMOUNT;
1852 return add_flags;
1853}
1854
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001855static void __d_instantiate(struct dentry *dentry, struct inode *inode)
1856{
David Howellsb18825a2013-09-12 19:22:53 +01001857 unsigned add_flags = d_flags_for_inode(inode);
Al Viro85c7f812016-04-14 19:52:13 -04001858 WARN_ON(d_in_lookup(dentry));
David Howellsb18825a2013-09-12 19:22:53 +01001859
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001860 spin_lock(&dentry->d_lock);
Al Virode689f52016-03-09 18:05:42 -05001861 hlist_add_head(&dentry->d_u.d_alias, &inode->i_dentry);
Al Viroa528aca2016-02-29 12:12:46 -05001862 raw_write_seqcount_begin(&dentry->d_seq);
David Howells4bf46a22015-03-05 14:09:22 +00001863 __d_set_inode_and_type(dentry, inode, add_flags);
Al Viroa528aca2016-02-29 12:12:46 -05001864 raw_write_seqcount_end(&dentry->d_seq);
Al Viroaffda482016-05-29 18:35:12 -04001865 fsnotify_update_flags(dentry);
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001866 spin_unlock(&dentry->d_lock);
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001867}
1868
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869/**
1870 * d_instantiate - fill in inode information for a dentry
1871 * @entry: dentry to complete
1872 * @inode: inode to attach to this dentry
1873 *
1874 * Fill in inode information in the entry.
1875 *
1876 * This turns negative dentries into productive full members
1877 * of society.
1878 *
1879 * NOTE! This assumes that the inode count has been incremented
1880 * (or otherwise set) by the caller to indicate that it is now
1881 * in use by the dcache.
1882 */
1883
1884void d_instantiate(struct dentry *entry, struct inode * inode)
1885{
Al Viro946e51f2014-10-26 19:19:16 -04001886 BUG_ON(!hlist_unhashed(&entry->d_u.d_alias));
Al Virode689f52016-03-09 18:05:42 -05001887 if (inode) {
Al Virob9680912016-04-11 00:53:26 -04001888 security_d_instantiate(entry, inode);
Nick Piggin873feea2011-01-07 17:50:06 +11001889 spin_lock(&inode->i_lock);
Al Virode689f52016-03-09 18:05:42 -05001890 __d_instantiate(entry, inode);
Nick Piggin873feea2011-01-07 17:50:06 +11001891 spin_unlock(&inode->i_lock);
Al Virode689f52016-03-09 18:05:42 -05001892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001894EXPORT_SYMBOL(d_instantiate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
1896/**
Miklos Szeredib70a80e2013-10-01 16:44:54 +02001897 * d_instantiate_no_diralias - instantiate a non-aliased dentry
1898 * @entry: dentry to complete
1899 * @inode: inode to attach to this dentry
1900 *
1901 * Fill in inode information in the entry. If a directory alias is found, then
1902 * return an error (and drop inode). Together with d_materialise_unique() this
1903 * guarantees that a directory inode may never have more than one alias.
1904 */
1905int d_instantiate_no_diralias(struct dentry *entry, struct inode *inode)
1906{
Al Viro946e51f2014-10-26 19:19:16 -04001907 BUG_ON(!hlist_unhashed(&entry->d_u.d_alias));
Miklos Szeredib70a80e2013-10-01 16:44:54 +02001908
Al Virob9680912016-04-11 00:53:26 -04001909 security_d_instantiate(entry, inode);
Miklos Szeredib70a80e2013-10-01 16:44:54 +02001910 spin_lock(&inode->i_lock);
1911 if (S_ISDIR(inode->i_mode) && !hlist_empty(&inode->i_dentry)) {
1912 spin_unlock(&inode->i_lock);
1913 iput(inode);
1914 return -EBUSY;
1915 }
1916 __d_instantiate(entry, inode);
1917 spin_unlock(&inode->i_lock);
Miklos Szeredib70a80e2013-10-01 16:44:54 +02001918
1919 return 0;
1920}
1921EXPORT_SYMBOL(d_instantiate_no_diralias);
1922
Al Viroadc0e91a2012-01-08 16:49:21 -05001923struct dentry *d_make_root(struct inode *root_inode)
1924{
1925 struct dentry *res = NULL;
1926
1927 if (root_inode) {
Miklos Szeredif9c34672018-01-19 11:39:52 +01001928 res = d_alloc_anon(root_inode->i_sb);
Al Viroadc0e91a2012-01-08 16:49:21 -05001929 if (res)
1930 d_instantiate(res, root_inode);
1931 else
1932 iput(root_inode);
1933 }
1934 return res;
1935}
1936EXPORT_SYMBOL(d_make_root);
1937
J. Bruce Fieldsd891eed2011-01-18 15:45:09 -05001938static struct dentry * __d_find_any_alias(struct inode *inode)
1939{
1940 struct dentry *alias;
1941
Al Virob3d9b7a2012-06-09 13:51:19 -04001942 if (hlist_empty(&inode->i_dentry))
J. Bruce Fieldsd891eed2011-01-18 15:45:09 -05001943 return NULL;
Al Viro946e51f2014-10-26 19:19:16 -04001944 alias = hlist_entry(inode->i_dentry.first, struct dentry, d_u.d_alias);
J. Bruce Fieldsd891eed2011-01-18 15:45:09 -05001945 __dget(alias);
1946 return alias;
1947}
1948
Sage Weil46f72b32012-01-10 09:04:37 -08001949/**
1950 * d_find_any_alias - find any alias for a given inode
1951 * @inode: inode to find an alias for
1952 *
1953 * If any aliases exist for the given inode, take and return a
1954 * reference for one of them. If no aliases exist, return %NULL.
1955 */
1956struct dentry *d_find_any_alias(struct inode *inode)
J. Bruce Fieldsd891eed2011-01-18 15:45:09 -05001957{
1958 struct dentry *de;
1959
1960 spin_lock(&inode->i_lock);
1961 de = __d_find_any_alias(inode);
1962 spin_unlock(&inode->i_lock);
1963 return de;
1964}
Sage Weil46f72b32012-01-10 09:04:37 -08001965EXPORT_SYMBOL(d_find_any_alias);
J. Bruce Fieldsd891eed2011-01-18 15:45:09 -05001966
Miklos Szeredif9c34672018-01-19 11:39:52 +01001967static struct dentry *__d_instantiate_anon(struct dentry *dentry,
1968 struct inode *inode,
1969 bool disconnected)
1970{
1971 struct dentry *res;
1972 unsigned add_flags;
1973
1974 security_d_instantiate(dentry, inode);
1975 spin_lock(&inode->i_lock);
1976 res = __d_find_any_alias(inode);
1977 if (res) {
1978 spin_unlock(&inode->i_lock);
1979 dput(dentry);
1980 goto out_iput;
1981 }
1982
1983 /* attach a disconnected dentry */
1984 add_flags = d_flags_for_inode(inode);
1985
1986 if (disconnected)
1987 add_flags |= DCACHE_DISCONNECTED;
1988
1989 spin_lock(&dentry->d_lock);
1990 __d_set_inode_and_type(dentry, inode, add_flags);
1991 hlist_add_head(&dentry->d_u.d_alias, &inode->i_dentry);
Linus Torvalds139351f2018-02-05 13:05:20 -08001992 if (!disconnected) {
1993 hlist_bl_lock(&dentry->d_sb->s_roots);
1994 hlist_bl_add_head(&dentry->d_hash, &dentry->d_sb->s_roots);
1995 hlist_bl_unlock(&dentry->d_sb->s_roots);
1996 }
Miklos Szeredif9c34672018-01-19 11:39:52 +01001997 spin_unlock(&dentry->d_lock);
1998 spin_unlock(&inode->i_lock);
1999
2000 return dentry;
2001
2002 out_iput:
2003 iput(inode);
2004 return res;
2005}
2006
2007struct dentry *d_instantiate_anon(struct dentry *dentry, struct inode *inode)
2008{
2009 return __d_instantiate_anon(dentry, inode, true);
2010}
2011EXPORT_SYMBOL(d_instantiate_anon);
2012
2013static struct dentry *__d_obtain_alias(struct inode *inode, bool disconnected)
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02002014{
Christoph Hellwig9308a612008-08-11 15:49:12 +02002015 struct dentry *tmp;
2016 struct dentry *res;
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02002017
2018 if (!inode)
Christoph Hellwig44003722008-08-11 15:49:04 +02002019 return ERR_PTR(-ESTALE);
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02002020 if (IS_ERR(inode))
2021 return ERR_CAST(inode);
2022
J. Bruce Fieldsd891eed2011-01-18 15:45:09 -05002023 res = d_find_any_alias(inode);
Christoph Hellwig9308a612008-08-11 15:49:12 +02002024 if (res)
2025 goto out_iput;
2026
Miklos Szeredif9c34672018-01-19 11:39:52 +01002027 tmp = d_alloc_anon(inode->i_sb);
Christoph Hellwig9308a612008-08-11 15:49:12 +02002028 if (!tmp) {
2029 res = ERR_PTR(-ENOMEM);
2030 goto out_iput;
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02002031 }
Nick Pigginb5c84bf2011-01-07 17:49:38 +11002032
Miklos Szeredif9c34672018-01-19 11:39:52 +01002033 return __d_instantiate_anon(tmp, inode, disconnected);
Christoph Hellwig9308a612008-08-11 15:49:12 +02002034
Miklos Szeredif9c34672018-01-19 11:39:52 +01002035out_iput:
Christoph Hellwig9308a612008-08-11 15:49:12 +02002036 iput(inode);
2037 return res;
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02002038}
J. Bruce Fields1a0a3972014-02-14 17:35:37 -05002039
2040/**
2041 * d_obtain_alias - find or allocate a DISCONNECTED dentry for a given inode
2042 * @inode: inode to allocate the dentry for
2043 *
2044 * Obtain a dentry for an inode resulting from NFS filehandle conversion or
2045 * similar open by handle operations. The returned dentry may be anonymous,
2046 * or may have a full name (if the inode was already in the cache).
2047 *
2048 * When called on a directory inode, we must ensure that the inode only ever
2049 * has one dentry. If a dentry is found, that is returned instead of
2050 * allocating a new one.
2051 *
2052 * On successful return, the reference to the inode has been transferred
2053 * to the dentry. In case of an error the reference on the inode is released.
2054 * To make it easier to use in export operations a %NULL or IS_ERR inode may
2055 * be passed in and the error will be propagated to the return value,
2056 * with a %NULL @inode replaced by ERR_PTR(-ESTALE).
2057 */
2058struct dentry *d_obtain_alias(struct inode *inode)
2059{
Miklos Szeredif9c34672018-01-19 11:39:52 +01002060 return __d_obtain_alias(inode, true);
J. Bruce Fields1a0a3972014-02-14 17:35:37 -05002061}
Benny Halevyadc48722009-02-27 14:02:59 -08002062EXPORT_SYMBOL(d_obtain_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
2064/**
J. Bruce Fields1a0a3972014-02-14 17:35:37 -05002065 * d_obtain_root - find or allocate a dentry for a given inode
2066 * @inode: inode to allocate the dentry for
2067 *
2068 * Obtain an IS_ROOT dentry for the root of a filesystem.
2069 *
2070 * We must ensure that directory inodes only ever have one dentry. If a
2071 * dentry is found, that is returned instead of allocating a new one.
2072 *
2073 * On successful return, the reference to the inode has been transferred
2074 * to the dentry. In case of an error the reference on the inode is
2075 * released. A %NULL or IS_ERR inode may be passed in and will be the
2076 * error will be propagate to the return value, with a %NULL @inode
2077 * replaced by ERR_PTR(-ESTALE).
2078 */
2079struct dentry *d_obtain_root(struct inode *inode)
2080{
Miklos Szeredif9c34672018-01-19 11:39:52 +01002081 return __d_obtain_alias(inode, false);
J. Bruce Fields1a0a3972014-02-14 17:35:37 -05002082}
2083EXPORT_SYMBOL(d_obtain_root);
2084
2085/**
Barry Naujok94035402008-05-21 16:50:46 +10002086 * d_add_ci - lookup or allocate new dentry with case-exact name
2087 * @inode: the inode case-insensitive lookup has found
2088 * @dentry: the negative dentry that was passed to the parent's lookup func
2089 * @name: the case-exact name to be associated with the returned dentry
2090 *
2091 * This is to avoid filling the dcache with case-insensitive names to the
2092 * same inode, only the actual correct case is stored in the dcache for
2093 * case-insensitive filesystems.
2094 *
2095 * For a case-insensitive lookup match and if the the case-exact dentry
2096 * already exists in in the dcache, use it and return it.
2097 *
2098 * If no entry exists with the exact case name, allocate new dentry with
2099 * the exact case, and return the spliced entry.
2100 */
Christoph Hellwige45b5902008-08-07 23:49:07 +02002101struct dentry *d_add_ci(struct dentry *dentry, struct inode *inode,
Barry Naujok94035402008-05-21 16:50:46 +10002102 struct qstr *name)
2103{
Al Virod9171b92016-04-15 03:33:13 -04002104 struct dentry *found, *res;
Barry Naujok94035402008-05-21 16:50:46 +10002105
Christoph Hellwigb6520c82009-01-05 19:10:37 +01002106 /*
2107 * First check if a dentry matching the name already exists,
2108 * if not go ahead and create it now.
2109 */
Barry Naujok94035402008-05-21 16:50:46 +10002110 found = d_hash_and_lookup(dentry->d_parent, name);
Al Virod9171b92016-04-15 03:33:13 -04002111 if (found) {
2112 iput(inode);
2113 return found;
Barry Naujok94035402008-05-21 16:50:46 +10002114 }
Al Virod9171b92016-04-15 03:33:13 -04002115 if (d_in_lookup(dentry)) {
2116 found = d_alloc_parallel(dentry->d_parent, name,
2117 dentry->d_wait);
2118 if (IS_ERR(found) || !d_in_lookup(found)) {
2119 iput(inode);
2120 return found;
2121 }
2122 } else {
2123 found = d_alloc(dentry->d_parent, name);
2124 if (!found) {
2125 iput(inode);
2126 return ERR_PTR(-ENOMEM);
2127 }
2128 }
2129 res = d_splice_alias(inode, found);
2130 if (res) {
2131 dput(found);
2132 return res;
2133 }
Al Viro4f522a22013-02-11 23:20:37 -05002134 return found;
Barry Naujok94035402008-05-21 16:50:46 +10002135}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002136EXPORT_SYMBOL(d_add_ci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137
Linus Torvalds12f8ad42012-05-04 14:59:14 -07002138
Al Virod4c91a82016-06-25 23:33:49 -04002139static inline bool d_same_name(const struct dentry *dentry,
2140 const struct dentry *parent,
2141 const struct qstr *name)
Linus Torvalds12f8ad42012-05-04 14:59:14 -07002142{
Al Virod4c91a82016-06-25 23:33:49 -04002143 if (likely(!(parent->d_flags & DCACHE_OP_COMPARE))) {
2144 if (dentry->d_name.len != name->len)
2145 return false;
2146 return dentry_cmp(dentry, name->name, name->len) == 0;
Linus Torvalds12f8ad42012-05-04 14:59:14 -07002147 }
Al Viro6fa67e72016-07-31 16:37:25 -04002148 return parent->d_op->d_compare(dentry,
Al Virod4c91a82016-06-25 23:33:49 -04002149 dentry->d_name.len, dentry->d_name.name,
2150 name) == 0;
Linus Torvalds12f8ad42012-05-04 14:59:14 -07002151}
2152
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153/**
Nick Piggin31e6b012011-01-07 17:49:52 +11002154 * __d_lookup_rcu - search for a dentry (racy, store-free)
2155 * @parent: parent dentry
2156 * @name: qstr of name we wish to find
Randy Dunlap1f1e6e52012-03-18 21:23:05 -07002157 * @seqp: returns d_seq value at the point where the dentry was found
Nick Piggin31e6b012011-01-07 17:49:52 +11002158 * Returns: dentry, or NULL
2159 *
2160 * __d_lookup_rcu is the dcache lookup function for rcu-walk name
2161 * resolution (store-free path walking) design described in
2162 * Documentation/filesystems/path-lookup.txt.
2163 *
2164 * This is not to be used outside core vfs.
2165 *
2166 * __d_lookup_rcu must only be used in rcu-walk mode, ie. with vfsmount lock
2167 * held, and rcu_read_lock held. The returned dentry must not be stored into
2168 * without taking d_lock and checking d_seq sequence count against @seq
2169 * returned here.
2170 *
Linus Torvalds15570082013-09-02 11:38:06 -07002171 * A refcount may be taken on the found dentry with the d_rcu_to_refcount
Nick Piggin31e6b012011-01-07 17:49:52 +11002172 * function.
2173 *
2174 * Alternatively, __d_lookup_rcu may be called again to look up the child of
2175 * the returned dentry, so long as its parent's seqlock is checked after the
2176 * child is looked up. Thus, an interlocking stepping of sequence lock checks
2177 * is formed, giving integrity down the path walk.
Linus Torvalds12f8ad42012-05-04 14:59:14 -07002178 *
2179 * NOTE! The caller *has* to check the resulting dentry against the sequence
2180 * number we've returned before using any of the resulting dentry state!
Nick Piggin31e6b012011-01-07 17:49:52 +11002181 */
Linus Torvalds8966be92012-03-02 14:23:30 -08002182struct dentry *__d_lookup_rcu(const struct dentry *parent,
2183 const struct qstr *name,
Linus Torvaldsda53be12013-05-21 15:22:44 -07002184 unsigned *seqp)
Nick Piggin31e6b012011-01-07 17:49:52 +11002185{
Linus Torvalds26fe5752012-05-10 13:14:12 -07002186 u64 hashlen = name->hash_len;
Nick Piggin31e6b012011-01-07 17:49:52 +11002187 const unsigned char *str = name->name;
Linus Torvalds8387ff22016-06-10 07:51:30 -07002188 struct hlist_bl_head *b = d_hash(hashlen_hash(hashlen));
Nick Pigginceb5bdc2011-01-07 17:50:05 +11002189 struct hlist_bl_node *node;
Nick Piggin31e6b012011-01-07 17:49:52 +11002190 struct dentry *dentry;
2191
2192 /*
2193 * Note: There is significant duplication with __d_lookup_rcu which is
2194 * required to prevent single threaded performance regressions
2195 * especially on architectures where smp_rmb (in seqcounts) are costly.
2196 * Keep the two functions in sync.
2197 */
2198
2199 /*
2200 * The hash list is protected using RCU.
2201 *
2202 * Carefully use d_seq when comparing a candidate dentry, to avoid
2203 * races with d_move().
2204 *
2205 * It is possible that concurrent renames can mess up our list
2206 * walk here and result in missing our dentry, resulting in the
2207 * false-negative result. d_lookup() protects against concurrent
2208 * renames using rename_lock seqlock.
2209 *
Namhyung Kimb0a4bb82011-01-22 15:31:32 +09002210 * See Documentation/filesystems/path-lookup.txt for more details.
Nick Piggin31e6b012011-01-07 17:49:52 +11002211 */
Linus Torvaldsb07ad992011-04-23 22:32:03 -07002212 hlist_bl_for_each_entry_rcu(dentry, node, b, d_hash) {
Linus Torvalds8966be92012-03-02 14:23:30 -08002213 unsigned seq;
Nick Piggin31e6b012011-01-07 17:49:52 +11002214
Nick Piggin31e6b012011-01-07 17:49:52 +11002215seqretry:
Linus Torvalds12f8ad42012-05-04 14:59:14 -07002216 /*
2217 * The dentry sequence count protects us from concurrent
Linus Torvaldsda53be12013-05-21 15:22:44 -07002218 * renames, and thus protects parent and name fields.
Linus Torvalds12f8ad42012-05-04 14:59:14 -07002219 *
2220 * The caller must perform a seqcount check in order
Linus Torvaldsda53be12013-05-21 15:22:44 -07002221 * to do anything useful with the returned dentry.
Linus Torvalds12f8ad42012-05-04 14:59:14 -07002222 *
2223 * NOTE! We do a "raw" seqcount_begin here. That means that
2224 * we don't wait for the sequence count to stabilize if it
2225 * is in the middle of a sequence change. If we do the slow
2226 * dentry compare, we will do seqretries until it is stable,
2227 * and if we end up with a successful lookup, we actually
2228 * want to exit RCU lookup anyway.
Al Virod4c91a82016-06-25 23:33:49 -04002229 *
2230 * Note that raw_seqcount_begin still *does* smp_rmb(), so
2231 * we are still guaranteed NUL-termination of ->d_name.name.
Linus Torvalds12f8ad42012-05-04 14:59:14 -07002232 */
2233 seq = raw_seqcount_begin(&dentry->d_seq);
Nick Piggin31e6b012011-01-07 17:49:52 +11002234 if (dentry->d_parent != parent)
2235 continue;
Linus Torvalds2e321802012-05-21 18:48:10 -07002236 if (d_unhashed(dentry))
2237 continue;
Linus Torvalds12f8ad42012-05-04 14:59:14 -07002238
2239 if (unlikely(parent->d_flags & DCACHE_OP_COMPARE)) {
Al Virod4c91a82016-06-25 23:33:49 -04002240 int tlen;
2241 const char *tname;
Linus Torvalds26fe5752012-05-10 13:14:12 -07002242 if (dentry->d_name.hash != hashlen_hash(hashlen))
2243 continue;
Al Virod4c91a82016-06-25 23:33:49 -04002244 tlen = dentry->d_name.len;
2245 tname = dentry->d_name.name;
2246 /* we want a consistent (name,len) pair */
2247 if (read_seqcount_retry(&dentry->d_seq, seq)) {
2248 cpu_relax();
Linus Torvalds12f8ad42012-05-04 14:59:14 -07002249 goto seqretry;
2250 }
Al Viro6fa67e72016-07-31 16:37:25 -04002251 if (parent->d_op->d_compare(dentry,
Al Virod4c91a82016-06-25 23:33:49 -04002252 tlen, tname, name) != 0)
2253 continue;
2254 } else {
2255 if (dentry->d_name.hash_len != hashlen)
2256 continue;
2257 if (dentry_cmp(dentry, str, hashlen_len(hashlen)) != 0)
2258 continue;
Linus Torvalds12f8ad42012-05-04 14:59:14 -07002259 }
Linus Torvaldsda53be12013-05-21 15:22:44 -07002260 *seqp = seq;
Al Virod4c91a82016-06-25 23:33:49 -04002261 return dentry;
Nick Piggin31e6b012011-01-07 17:49:52 +11002262 }
2263 return NULL;
2264}
2265
2266/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 * d_lookup - search for a dentry
2268 * @parent: parent dentry
2269 * @name: qstr of name we wish to find
Nick Pigginb04f7842010-08-18 04:37:34 +10002270 * Returns: dentry, or NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 *
Nick Pigginb04f7842010-08-18 04:37:34 +10002272 * d_lookup searches the children of the parent dentry for the name in
2273 * question. If the dentry is found its reference count is incremented and the
2274 * dentry is returned. The caller must use dput to free the entry when it has
2275 * finished using it. %NULL is returned if the dentry does not exist.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 */
Al Viroda2d8452013-01-24 18:29:34 -05002277struct dentry *d_lookup(const struct dentry *parent, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278{
Nick Piggin31e6b012011-01-07 17:49:52 +11002279 struct dentry *dentry;
Nick Piggin949854d2011-01-07 17:49:37 +11002280 unsigned seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281
Daeseok Younb8314f92014-08-11 11:46:53 +09002282 do {
2283 seq = read_seqbegin(&rename_lock);
2284 dentry = __d_lookup(parent, name);
2285 if (dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 break;
2287 } while (read_seqretry(&rename_lock, seq));
2288 return dentry;
2289}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002290EXPORT_SYMBOL(d_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
Nick Piggin31e6b012011-01-07 17:49:52 +11002292/**
Nick Pigginb04f7842010-08-18 04:37:34 +10002293 * __d_lookup - search for a dentry (racy)
2294 * @parent: parent dentry
2295 * @name: qstr of name we wish to find
2296 * Returns: dentry, or NULL
2297 *
2298 * __d_lookup is like d_lookup, however it may (rarely) return a
2299 * false-negative result due to unrelated rename activity.
2300 *
2301 * __d_lookup is slightly faster by avoiding rename_lock read seqlock,
2302 * however it must be used carefully, eg. with a following d_lookup in
2303 * the case of failure.
2304 *
2305 * __d_lookup callers must be commented.
2306 */
Al Viroa713ca2a2013-01-24 18:27:00 -05002307struct dentry *__d_lookup(const struct dentry *parent, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 unsigned int hash = name->hash;
Linus Torvalds8387ff22016-06-10 07:51:30 -07002310 struct hlist_bl_head *b = d_hash(hash);
Nick Pigginceb5bdc2011-01-07 17:50:05 +11002311 struct hlist_bl_node *node;
Nick Piggin31e6b012011-01-07 17:49:52 +11002312 struct dentry *found = NULL;
Paul E. McKenney665a7582005-11-07 00:59:17 -08002313 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314
Nick Pigginb04f7842010-08-18 04:37:34 +10002315 /*
Nick Piggin31e6b012011-01-07 17:49:52 +11002316 * Note: There is significant duplication with __d_lookup_rcu which is
2317 * required to prevent single threaded performance regressions
2318 * especially on architectures where smp_rmb (in seqcounts) are costly.
2319 * Keep the two functions in sync.
2320 */
2321
2322 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10002323 * The hash list is protected using RCU.
2324 *
2325 * Take d_lock when comparing a candidate dentry, to avoid races
2326 * with d_move().
2327 *
2328 * It is possible that concurrent renames can mess up our list
2329 * walk here and result in missing our dentry, resulting in the
2330 * false-negative result. d_lookup() protects against concurrent
2331 * renames using rename_lock seqlock.
2332 *
Namhyung Kimb0a4bb82011-01-22 15:31:32 +09002333 * See Documentation/filesystems/path-lookup.txt for more details.
Nick Pigginb04f7842010-08-18 04:37:34 +10002334 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 rcu_read_lock();
2336
Linus Torvaldsb07ad992011-04-23 22:32:03 -07002337 hlist_bl_for_each_entry_rcu(dentry, node, b, d_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 if (dentry->d_name.hash != hash)
2340 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341
2342 spin_lock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 if (dentry->d_parent != parent)
2344 goto next;
Linus Torvaldsd0185c02008-09-29 07:42:57 -07002345 if (d_unhashed(dentry))
2346 goto next;
2347
Al Virod4c91a82016-06-25 23:33:49 -04002348 if (!d_same_name(dentry, parent, name))
2349 goto next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350
Waiman Long98474232013-08-28 18:24:59 -07002351 dentry->d_lockref.count++;
Linus Torvaldsd0185c02008-09-29 07:42:57 -07002352 found = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 spin_unlock(&dentry->d_lock);
2354 break;
2355next:
2356 spin_unlock(&dentry->d_lock);
2357 }
2358 rcu_read_unlock();
2359
2360 return found;
2361}
2362
2363/**
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08002364 * d_hash_and_lookup - hash the qstr then search for a dentry
2365 * @dir: Directory to search in
2366 * @name: qstr of name we wish to find
2367 *
Al Viro4f522a22013-02-11 23:20:37 -05002368 * On lookup failure NULL is returned; on bad name - ERR_PTR(-error)
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08002369 */
2370struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name)
2371{
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08002372 /*
2373 * Check for a fs-specific hash function. Note that we must
2374 * calculate the standard hash first, as the d_op->d_hash()
2375 * routine may choose to leave the hash value unchanged.
2376 */
Linus Torvalds8387ff22016-06-10 07:51:30 -07002377 name->hash = full_name_hash(dir, name->name, name->len);
Nick Pigginfb045ad2011-01-07 17:49:55 +11002378 if (dir->d_flags & DCACHE_OP_HASH) {
Linus Torvaldsda53be12013-05-21 15:22:44 -07002379 int err = dir->d_op->d_hash(dir, name);
Al Viro4f522a22013-02-11 23:20:37 -05002380 if (unlikely(err < 0))
2381 return ERR_PTR(err);
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08002382 }
Al Viro4f522a22013-02-11 23:20:37 -05002383 return d_lookup(dir, name);
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08002384}
Al Viro4f522a22013-02-11 23:20:37 -05002385EXPORT_SYMBOL(d_hash_and_lookup);
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08002386
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387/*
2388 * When a file is deleted, we have two options:
2389 * - turn this dentry into a negative dentry
2390 * - unhash this dentry and free it.
2391 *
2392 * Usually, we want to just turn this into
2393 * a negative dentry, but if anybody else is
2394 * currently using the dentry or the inode
2395 * we can't do that and we fall back on removing
2396 * it from the hash queues and waiting for
2397 * it to be deleted later when it has no users
2398 */
2399
2400/**
2401 * d_delete - delete a dentry
2402 * @dentry: The dentry to delete
2403 *
2404 * Turn the dentry into a negative dentry if possible, otherwise
2405 * remove it from the hash queues so it can be deleted later
2406 */
2407
2408void d_delete(struct dentry * dentry)
2409{
Al Viroc19457f2018-02-23 21:02:31 -05002410 struct inode *inode = dentry->d_inode;
2411 int isdir = d_is_dir(dentry);
2412
2413 spin_lock(&inode->i_lock);
2414 spin_lock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 /*
2416 * Are we the only user?
2417 */
Waiman Long98474232013-08-28 18:24:59 -07002418 if (dentry->d_lockref.count == 1) {
Al Viro13e3c5e2010-05-21 16:11:04 -04002419 dentry->d_flags &= ~DCACHE_CANT_MOUNT;
Nick Piggin31e6b012011-01-07 17:49:52 +11002420 dentry_unlink_inode(dentry);
Al Viroc19457f2018-02-23 21:02:31 -05002421 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 __d_drop(dentry);
Al Viroc19457f2018-02-23 21:02:31 -05002423 spin_unlock(&dentry->d_lock);
2424 spin_unlock(&inode->i_lock);
2425 }
John McCutchan7a91bf72005-08-08 13:52:16 -04002426 fsnotify_nameremove(dentry, isdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002428EXPORT_SYMBOL(d_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429
Al Viro15d3c582016-07-29 17:45:21 -04002430static void __d_rehash(struct dentry *entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431{
Al Viro15d3c582016-07-29 17:45:21 -04002432 struct hlist_bl_head *b = d_hash(entry->d_name.hash);
NeilBrown61647822017-11-10 15:45:41 +11002433
Christoph Hellwig1879fd62011-04-25 14:01:36 -04002434 hlist_bl_lock(b);
Linus Torvaldsb07ad992011-04-23 22:32:03 -07002435 hlist_bl_add_head_rcu(&entry->d_hash, b);
Christoph Hellwig1879fd62011-04-25 14:01:36 -04002436 hlist_bl_unlock(b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437}
2438
2439/**
2440 * d_rehash - add an entry back to the hash
2441 * @entry: dentry to add to the hash
2442 *
2443 * Adds a dentry to the hash according to its name.
2444 */
2445
2446void d_rehash(struct dentry * entry)
2447{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 spin_lock(&entry->d_lock);
Al Viro15d3c582016-07-29 17:45:21 -04002449 __d_rehash(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 spin_unlock(&entry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002452EXPORT_SYMBOL(d_rehash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453
Al Viro84e710d2016-04-15 00:58:55 -04002454static inline unsigned start_dir_add(struct inode *dir)
2455{
2456
2457 for (;;) {
2458 unsigned n = dir->i_dir_seq;
2459 if (!(n & 1) && cmpxchg(&dir->i_dir_seq, n, n + 1) == n)
2460 return n;
2461 cpu_relax();
2462 }
2463}
2464
2465static inline void end_dir_add(struct inode *dir, unsigned n)
2466{
2467 smp_store_release(&dir->i_dir_seq, n + 2);
2468}
2469
Al Virod9171b92016-04-15 03:33:13 -04002470static void d_wait_lookup(struct dentry *dentry)
2471{
2472 if (d_in_lookup(dentry)) {
2473 DECLARE_WAITQUEUE(wait, current);
2474 add_wait_queue(dentry->d_wait, &wait);
2475 do {
2476 set_current_state(TASK_UNINTERRUPTIBLE);
2477 spin_unlock(&dentry->d_lock);
2478 schedule();
2479 spin_lock(&dentry->d_lock);
2480 } while (d_in_lookup(dentry));
2481 }
2482}
2483
Al Viro94bdd652016-04-15 02:42:04 -04002484struct dentry *d_alloc_parallel(struct dentry *parent,
Al Virod9171b92016-04-15 03:33:13 -04002485 const struct qstr *name,
2486 wait_queue_head_t *wq)
Al Viro94bdd652016-04-15 02:42:04 -04002487{
Al Viro94bdd652016-04-15 02:42:04 -04002488 unsigned int hash = name->hash;
Al Viro94bdd652016-04-15 02:42:04 -04002489 struct hlist_bl_head *b = in_lookup_hash(parent, hash);
2490 struct hlist_bl_node *node;
2491 struct dentry *new = d_alloc(parent, name);
2492 struct dentry *dentry;
2493 unsigned seq, r_seq, d_seq;
2494
2495 if (unlikely(!new))
2496 return ERR_PTR(-ENOMEM);
2497
2498retry:
2499 rcu_read_lock();
Will Deacon015555f2018-02-19 14:55:54 +00002500 seq = smp_load_acquire(&parent->d_inode->i_dir_seq);
Al Viro94bdd652016-04-15 02:42:04 -04002501 r_seq = read_seqbegin(&rename_lock);
2502 dentry = __d_lookup_rcu(parent, name, &d_seq);
2503 if (unlikely(dentry)) {
2504 if (!lockref_get_not_dead(&dentry->d_lockref)) {
2505 rcu_read_unlock();
2506 goto retry;
2507 }
2508 if (read_seqcount_retry(&dentry->d_seq, d_seq)) {
2509 rcu_read_unlock();
2510 dput(dentry);
2511 goto retry;
2512 }
2513 rcu_read_unlock();
2514 dput(new);
2515 return dentry;
2516 }
2517 if (unlikely(read_seqretry(&rename_lock, r_seq))) {
2518 rcu_read_unlock();
2519 goto retry;
2520 }
Will Deacon015555f2018-02-19 14:55:54 +00002521
2522 if (unlikely(seq & 1)) {
2523 rcu_read_unlock();
2524 goto retry;
2525 }
2526
Al Viro94bdd652016-04-15 02:42:04 -04002527 hlist_bl_lock(b);
Will Deacon8cc07c82018-02-19 14:55:55 +00002528 if (unlikely(READ_ONCE(parent->d_inode->i_dir_seq) != seq)) {
Al Viro94bdd652016-04-15 02:42:04 -04002529 hlist_bl_unlock(b);
2530 rcu_read_unlock();
2531 goto retry;
2532 }
Al Viro94bdd652016-04-15 02:42:04 -04002533 /*
2534 * No changes for the parent since the beginning of d_lookup().
2535 * Since all removals from the chain happen with hlist_bl_lock(),
2536 * any potential in-lookup matches are going to stay here until
2537 * we unlock the chain. All fields are stable in everything
2538 * we encounter.
2539 */
2540 hlist_bl_for_each_entry(dentry, node, b, d_u.d_in_lookup_hash) {
2541 if (dentry->d_name.hash != hash)
2542 continue;
2543 if (dentry->d_parent != parent)
2544 continue;
Al Virod4c91a82016-06-25 23:33:49 -04002545 if (!d_same_name(dentry, parent, name))
2546 continue;
Al Viro94bdd652016-04-15 02:42:04 -04002547 hlist_bl_unlock(b);
Al Viroe7d6ef92016-06-20 01:35:59 -04002548 /* now we can try to grab a reference */
2549 if (!lockref_get_not_dead(&dentry->d_lockref)) {
2550 rcu_read_unlock();
2551 goto retry;
2552 }
2553
2554 rcu_read_unlock();
2555 /*
2556 * somebody is likely to be still doing lookup for it;
2557 * wait for them to finish
2558 */
Al Virod9171b92016-04-15 03:33:13 -04002559 spin_lock(&dentry->d_lock);
2560 d_wait_lookup(dentry);
2561 /*
2562 * it's not in-lookup anymore; in principle we should repeat
2563 * everything from dcache lookup, but it's likely to be what
2564 * d_lookup() would've found anyway. If it is, just return it;
2565 * otherwise we really have to repeat the whole thing.
2566 */
2567 if (unlikely(dentry->d_name.hash != hash))
2568 goto mismatch;
2569 if (unlikely(dentry->d_parent != parent))
2570 goto mismatch;
2571 if (unlikely(d_unhashed(dentry)))
2572 goto mismatch;
Al Virod4c91a82016-06-25 23:33:49 -04002573 if (unlikely(!d_same_name(dentry, parent, name)))
2574 goto mismatch;
Al Virod9171b92016-04-15 03:33:13 -04002575 /* OK, it *is* a hashed match; return it */
2576 spin_unlock(&dentry->d_lock);
Al Viro94bdd652016-04-15 02:42:04 -04002577 dput(new);
2578 return dentry;
2579 }
Al Viroe7d6ef92016-06-20 01:35:59 -04002580 rcu_read_unlock();
Al Viro94bdd652016-04-15 02:42:04 -04002581 /* we can't take ->d_lock here; it's OK, though. */
2582 new->d_flags |= DCACHE_PAR_LOOKUP;
Al Virod9171b92016-04-15 03:33:13 -04002583 new->d_wait = wq;
Al Viro94bdd652016-04-15 02:42:04 -04002584 hlist_bl_add_head_rcu(&new->d_u.d_in_lookup_hash, b);
2585 hlist_bl_unlock(b);
2586 return new;
Al Virod9171b92016-04-15 03:33:13 -04002587mismatch:
2588 spin_unlock(&dentry->d_lock);
2589 dput(dentry);
2590 goto retry;
Al Viro94bdd652016-04-15 02:42:04 -04002591}
2592EXPORT_SYMBOL(d_alloc_parallel);
2593
Al Viro85c7f812016-04-14 19:52:13 -04002594void __d_lookup_done(struct dentry *dentry)
2595{
Al Viro94bdd652016-04-15 02:42:04 -04002596 struct hlist_bl_head *b = in_lookup_hash(dentry->d_parent,
2597 dentry->d_name.hash);
2598 hlist_bl_lock(b);
Al Viro85c7f812016-04-14 19:52:13 -04002599 dentry->d_flags &= ~DCACHE_PAR_LOOKUP;
Al Viro94bdd652016-04-15 02:42:04 -04002600 __hlist_bl_del(&dentry->d_u.d_in_lookup_hash);
Al Virod9171b92016-04-15 03:33:13 -04002601 wake_up_all(dentry->d_wait);
2602 dentry->d_wait = NULL;
Al Viro94bdd652016-04-15 02:42:04 -04002603 hlist_bl_unlock(b);
2604 INIT_HLIST_NODE(&dentry->d_u.d_alias);
Al Virod9171b92016-04-15 03:33:13 -04002605 INIT_LIST_HEAD(&dentry->d_lru);
Al Viro85c7f812016-04-14 19:52:13 -04002606}
2607EXPORT_SYMBOL(__d_lookup_done);
Al Viroed782b52016-03-09 19:52:39 -05002608
2609/* inode->i_lock held if inode is non-NULL */
2610
2611static inline void __d_add(struct dentry *dentry, struct inode *inode)
2612{
Al Viro84e710d2016-04-15 00:58:55 -04002613 struct inode *dir = NULL;
2614 unsigned n;
Al Viro0568d702016-04-14 19:40:56 -04002615 spin_lock(&dentry->d_lock);
Al Viro84e710d2016-04-15 00:58:55 -04002616 if (unlikely(d_in_lookup(dentry))) {
2617 dir = dentry->d_parent->d_inode;
2618 n = start_dir_add(dir);
Al Viro85c7f812016-04-14 19:52:13 -04002619 __d_lookup_done(dentry);
Al Viro84e710d2016-04-15 00:58:55 -04002620 }
Al Viroed782b52016-03-09 19:52:39 -05002621 if (inode) {
Al Viro0568d702016-04-14 19:40:56 -04002622 unsigned add_flags = d_flags_for_inode(inode);
2623 hlist_add_head(&dentry->d_u.d_alias, &inode->i_dentry);
2624 raw_write_seqcount_begin(&dentry->d_seq);
2625 __d_set_inode_and_type(dentry, inode, add_flags);
2626 raw_write_seqcount_end(&dentry->d_seq);
Al Viroaffda482016-05-29 18:35:12 -04002627 fsnotify_update_flags(dentry);
Al Viroed782b52016-03-09 19:52:39 -05002628 }
Al Viro15d3c582016-07-29 17:45:21 -04002629 __d_rehash(dentry);
Al Viro84e710d2016-04-15 00:58:55 -04002630 if (dir)
2631 end_dir_add(dir, n);
Al Viro0568d702016-04-14 19:40:56 -04002632 spin_unlock(&dentry->d_lock);
2633 if (inode)
2634 spin_unlock(&inode->i_lock);
Al Viroed782b52016-03-09 19:52:39 -05002635}
2636
Nick Pigginfb2d5b82011-01-07 17:49:26 +11002637/**
Al Viro34d0d192016-03-08 21:01:03 -05002638 * d_add - add dentry to hash queues
2639 * @entry: dentry to add
2640 * @inode: The inode to attach to this dentry
2641 *
2642 * This adds the entry to the hash queues and initializes @inode.
2643 * The entry was actually filled in earlier during d_alloc().
2644 */
2645
2646void d_add(struct dentry *entry, struct inode *inode)
2647{
Al Virob9680912016-04-11 00:53:26 -04002648 if (inode) {
2649 security_d_instantiate(entry, inode);
Al Viroed782b52016-03-09 19:52:39 -05002650 spin_lock(&inode->i_lock);
Al Virob9680912016-04-11 00:53:26 -04002651 }
Al Viroed782b52016-03-09 19:52:39 -05002652 __d_add(entry, inode);
Al Viro34d0d192016-03-08 21:01:03 -05002653}
2654EXPORT_SYMBOL(d_add);
2655
2656/**
Al Viro668d0cd2016-03-08 12:44:17 -05002657 * d_exact_alias - find and hash an exact unhashed alias
2658 * @entry: dentry to add
2659 * @inode: The inode to go with this dentry
2660 *
2661 * If an unhashed dentry with the same name/parent and desired
2662 * inode already exists, hash and return it. Otherwise, return
2663 * NULL.
2664 *
2665 * Parent directory should be locked.
2666 */
2667struct dentry *d_exact_alias(struct dentry *entry, struct inode *inode)
2668{
2669 struct dentry *alias;
Al Viro668d0cd2016-03-08 12:44:17 -05002670 unsigned int hash = entry->d_name.hash;
2671
2672 spin_lock(&inode->i_lock);
2673 hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
2674 /*
2675 * Don't need alias->d_lock here, because aliases with
2676 * d_parent == entry->d_parent are not subject to name or
2677 * parent changes, because the parent inode i_mutex is held.
2678 */
2679 if (alias->d_name.hash != hash)
2680 continue;
2681 if (alias->d_parent != entry->d_parent)
2682 continue;
Al Virod4c91a82016-06-25 23:33:49 -04002683 if (!d_same_name(alias, entry->d_parent, &entry->d_name))
Al Viro668d0cd2016-03-08 12:44:17 -05002684 continue;
2685 spin_lock(&alias->d_lock);
2686 if (!d_unhashed(alias)) {
2687 spin_unlock(&alias->d_lock);
2688 alias = NULL;
2689 } else {
2690 __dget_dlock(alias);
Al Viro15d3c582016-07-29 17:45:21 -04002691 __d_rehash(alias);
Al Viro668d0cd2016-03-08 12:44:17 -05002692 spin_unlock(&alias->d_lock);
2693 }
2694 spin_unlock(&inode->i_lock);
2695 return alias;
2696 }
2697 spin_unlock(&inode->i_lock);
2698 return NULL;
2699}
2700EXPORT_SYMBOL(d_exact_alias);
2701
2702/**
Nick Pigginfb2d5b82011-01-07 17:49:26 +11002703 * dentry_update_name_case - update case insensitive dentry with a new name
2704 * @dentry: dentry to be updated
2705 * @name: new name
2706 *
2707 * Update a case insensitive dentry with new case of name.
2708 *
2709 * dentry must have been returned by d_lookup with name @name. Old and new
2710 * name lengths must match (ie. no d_compare which allows mismatched name
2711 * lengths).
2712 *
2713 * Parent inode i_mutex must be held over d_lookup and into this call (to
2714 * keep renames and concurrent inserts, and readdir(2) away).
2715 */
Al Viro9aba36d2016-07-20 22:28:45 -04002716void dentry_update_name_case(struct dentry *dentry, const struct qstr *name)
Nick Pigginfb2d5b82011-01-07 17:49:26 +11002717{
Al Viro59551022016-01-22 15:40:57 -05002718 BUG_ON(!inode_is_locked(dentry->d_parent->d_inode));
Nick Pigginfb2d5b82011-01-07 17:49:26 +11002719 BUG_ON(dentry->d_name.len != name->len); /* d_lookup gives this */
2720
Nick Pigginfb2d5b82011-01-07 17:49:26 +11002721 spin_lock(&dentry->d_lock);
Nick Piggin31e6b012011-01-07 17:49:52 +11002722 write_seqcount_begin(&dentry->d_seq);
Nick Pigginfb2d5b82011-01-07 17:49:26 +11002723 memcpy((unsigned char *)dentry->d_name.name, name->name, name->len);
Nick Piggin31e6b012011-01-07 17:49:52 +11002724 write_seqcount_end(&dentry->d_seq);
Nick Pigginfb2d5b82011-01-07 17:49:26 +11002725 spin_unlock(&dentry->d_lock);
Nick Pigginfb2d5b82011-01-07 17:49:26 +11002726}
2727EXPORT_SYMBOL(dentry_update_name_case);
2728
Al Viro8d85b482014-09-29 14:54:27 -04002729static void swap_names(struct dentry *dentry, struct dentry *target)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730{
Al Viro8d85b482014-09-29 14:54:27 -04002731 if (unlikely(dname_external(target))) {
2732 if (unlikely(dname_external(dentry))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 /*
2734 * Both external: swap the pointers
2735 */
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08002736 swap(target->d_name.name, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 } else {
2738 /*
2739 * dentry:internal, target:external. Steal target's
2740 * storage and make target internal.
2741 */
J. Bruce Fields321bcf92007-10-21 16:41:38 -07002742 memcpy(target->d_iname, dentry->d_name.name,
2743 dentry->d_name.len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 dentry->d_name.name = target->d_name.name;
2745 target->d_name.name = target->d_iname;
2746 }
2747 } else {
Al Viro8d85b482014-09-29 14:54:27 -04002748 if (unlikely(dname_external(dentry))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 /*
2750 * dentry:external, target:internal. Give dentry's
2751 * storage to target and make dentry internal
2752 */
2753 memcpy(dentry->d_iname, target->d_name.name,
2754 target->d_name.len + 1);
2755 target->d_name.name = dentry->d_name.name;
2756 dentry->d_name.name = dentry->d_iname;
2757 } else {
2758 /*
Miklos Szeredida1ce062014-04-01 17:08:43 +02002759 * Both are internal.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 */
Miklos Szeredida1ce062014-04-01 17:08:43 +02002761 unsigned int i;
2762 BUILD_BUG_ON(!IS_ALIGNED(DNAME_INLINE_LEN, sizeof(long)));
2763 for (i = 0; i < DNAME_INLINE_LEN / sizeof(long); i++) {
2764 swap(((long *) &dentry->d_iname)[i],
2765 ((long *) &target->d_iname)[i]);
2766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 }
2768 }
Linus Torvaldsa28ddb82014-09-24 12:27:39 -07002769 swap(dentry->d_name.hash_len, target->d_name.hash_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770}
2771
Al Viro8d85b482014-09-29 14:54:27 -04002772static void copy_name(struct dentry *dentry, struct dentry *target)
2773{
2774 struct external_name *old_name = NULL;
2775 if (unlikely(dname_external(dentry)))
2776 old_name = external_name(dentry);
2777 if (unlikely(dname_external(target))) {
2778 atomic_inc(&external_name(target)->u.count);
2779 dentry->d_name = target->d_name;
2780 } else {
2781 memcpy(dentry->d_iname, target->d_name.name,
2782 target->d_name.len + 1);
2783 dentry->d_name.name = dentry->d_iname;
2784 dentry->d_name.hash_len = target->d_name.hash_len;
2785 }
2786 if (old_name && likely(atomic_dec_and_test(&old_name->u.count)))
2787 kfree_rcu(old_name, u.head);
2788}
2789
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002790static void dentry_lock_for_move(struct dentry *dentry, struct dentry *target)
2791{
2792 /*
2793 * XXXX: do we really need to take target->d_lock?
2794 */
2795 if (IS_ROOT(dentry) || dentry->d_parent == target->d_parent)
2796 spin_lock(&target->d_parent->d_lock);
2797 else {
2798 if (d_ancestor(dentry->d_parent, target->d_parent)) {
2799 spin_lock(&dentry->d_parent->d_lock);
2800 spin_lock_nested(&target->d_parent->d_lock,
2801 DENTRY_D_LOCK_NESTED);
2802 } else {
2803 spin_lock(&target->d_parent->d_lock);
2804 spin_lock_nested(&dentry->d_parent->d_lock,
2805 DENTRY_D_LOCK_NESTED);
2806 }
2807 }
2808 if (target < dentry) {
2809 spin_lock_nested(&target->d_lock, 2);
2810 spin_lock_nested(&dentry->d_lock, 3);
2811 } else {
2812 spin_lock_nested(&dentry->d_lock, 2);
2813 spin_lock_nested(&target->d_lock, 3);
2814 }
2815}
2816
Al Viro986c01942014-09-26 23:11:15 -04002817static void dentry_unlock_for_move(struct dentry *dentry, struct dentry *target)
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002818{
2819 if (target->d_parent != dentry->d_parent)
2820 spin_unlock(&dentry->d_parent->d_lock);
2821 if (target->d_parent != target)
2822 spin_unlock(&target->d_parent->d_lock);
Al Viro986c01942014-09-26 23:11:15 -04002823 spin_unlock(&target->d_lock);
2824 spin_unlock(&dentry->d_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002825}
2826
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827/*
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002828 * When switching names, the actual string doesn't strictly have to
2829 * be preserved in the target - because we're dropping the target
2830 * anyway. As such, we can just do a simple memcpy() to copy over
Mikhail Efremovd2fa4a82014-09-24 22:14:33 +04002831 * the new name before we switch, unless we are going to rehash
2832 * it. Note that if we *do* unhash the target, we are not allowed
2833 * to rehash it without giving it a new name/hash key - whether
2834 * we swap or overwrite the names here, resulting name won't match
2835 * the reality in filesystem; it's only there for d_path() purposes.
2836 * Note that all of this is happening under rename_lock, so the
2837 * any hash lookup seeing it in the middle of manipulations will
2838 * be discarded anyway. So we do not care what happens to the hash
2839 * key in that case.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 */
Trond Myklebust9eaef272006-10-21 10:24:20 -07002841/*
Al Viro18367502011-07-12 21:42:24 -04002842 * __d_move - move a dentry
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 * @dentry: entry to move
2844 * @target: new dentry
Miklos Szeredida1ce062014-04-01 17:08:43 +02002845 * @exchange: exchange the two dentries
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 *
2847 * Update the dcache to reflect the move of a file name. Negative
Jeff Laytonc46c8872011-07-26 13:33:16 -04002848 * dcache entries should not be moved in this way. Caller must hold
2849 * rename_lock, the i_mutex of the source and target directories,
2850 * and the sb->s_vfs_rename_mutex if they differ. See lock_rename().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 */
Miklos Szeredida1ce062014-04-01 17:08:43 +02002852static void __d_move(struct dentry *dentry, struct dentry *target,
2853 bool exchange)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854{
Al Viro84e710d2016-04-15 00:58:55 -04002855 struct inode *dir = NULL;
2856 unsigned n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 if (!dentry->d_inode)
2858 printk(KERN_WARNING "VFS: moving negative dcache entry\n");
2859
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002860 BUG_ON(d_ancestor(dentry, target));
2861 BUG_ON(d_ancestor(target, dentry));
2862
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002863 dentry_lock_for_move(dentry, target);
Al Viro84e710d2016-04-15 00:58:55 -04002864 if (unlikely(d_in_lookup(target))) {
2865 dir = target->d_parent->d_inode;
2866 n = start_dir_add(dir);
Al Viro85c7f812016-04-14 19:52:13 -04002867 __d_lookup_done(target);
Al Viro84e710d2016-04-15 00:58:55 -04002868 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869
Nick Piggin31e6b012011-01-07 17:49:52 +11002870 write_seqcount_begin(&dentry->d_seq);
John Stultz1ca7d672013-10-07 15:51:59 -07002871 write_seqcount_begin_nested(&target->d_seq, DENTRY_D_LOCK_NESTED);
Nick Piggin31e6b012011-01-07 17:49:52 +11002872
Al Viro15d3c582016-07-29 17:45:21 -04002873 /* unhash both */
Al Viro0632a9a2018-03-07 00:49:10 -05002874 if (!d_unhashed(dentry))
2875 ___d_drop(dentry);
2876 if (!d_unhashed(target))
2877 ___d_drop(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 /* Switch the names.. */
Al Viro8d85b482014-09-29 14:54:27 -04002880 if (exchange)
2881 swap_names(dentry, target);
2882 else
2883 copy_name(dentry, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884
Al Viro15d3c582016-07-29 17:45:21 -04002885 /* rehash in new place(s) */
2886 __d_rehash(dentry);
2887 if (exchange)
2888 __d_rehash(target);
NeilBrown61647822017-11-10 15:45:41 +11002889 else
2890 target->d_hash.pprev = NULL;
Al Viro15d3c582016-07-29 17:45:21 -04002891
Al Viro63cf4272014-09-26 23:06:14 -04002892 /* ... and switch them in the tree */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 if (IS_ROOT(dentry)) {
Al Viro63cf4272014-09-26 23:06:14 -04002894 /* splicing a tree */
Al Viro3d56c252016-06-07 21:26:55 -04002895 dentry->d_flags |= DCACHE_RCUACCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 dentry->d_parent = target->d_parent;
2897 target->d_parent = target;
Al Viro946e51f2014-10-26 19:19:16 -04002898 list_del_init(&target->d_child);
2899 list_move(&dentry->d_child, &dentry->d_parent->d_subdirs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 } else {
Al Viro63cf4272014-09-26 23:06:14 -04002901 /* swapping two dentries */
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08002902 swap(dentry->d_parent, target->d_parent);
Al Viro946e51f2014-10-26 19:19:16 -04002903 list_move(&target->d_child, &target->d_parent->d_subdirs);
2904 list_move(&dentry->d_child, &dentry->d_parent->d_subdirs);
Al Viro63cf4272014-09-26 23:06:14 -04002905 if (exchange)
Al Viroaffda482016-05-29 18:35:12 -04002906 fsnotify_update_flags(target);
2907 fsnotify_update_flags(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 }
2909
Nick Piggin31e6b012011-01-07 17:49:52 +11002910 write_seqcount_end(&target->d_seq);
2911 write_seqcount_end(&dentry->d_seq);
2912
Al Viro84e710d2016-04-15 00:58:55 -04002913 if (dir)
2914 end_dir_add(dir, n);
Al Viro986c01942014-09-26 23:11:15 -04002915 dentry_unlock_for_move(dentry, target);
Al Viro18367502011-07-12 21:42:24 -04002916}
2917
2918/*
2919 * d_move - move a dentry
2920 * @dentry: entry to move
2921 * @target: new dentry
2922 *
2923 * Update the dcache to reflect the move of a file name. Negative
Jeff Laytonc46c8872011-07-26 13:33:16 -04002924 * dcache entries should not be moved in this way. See the locking
2925 * requirements for __d_move.
Al Viro18367502011-07-12 21:42:24 -04002926 */
2927void d_move(struct dentry *dentry, struct dentry *target)
2928{
2929 write_seqlock(&rename_lock);
Miklos Szeredida1ce062014-04-01 17:08:43 +02002930 __d_move(dentry, target, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 write_sequnlock(&rename_lock);
Trond Myklebust9eaef272006-10-21 10:24:20 -07002932}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002933EXPORT_SYMBOL(d_move);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934
Miklos Szeredida1ce062014-04-01 17:08:43 +02002935/*
2936 * d_exchange - exchange two dentries
2937 * @dentry1: first dentry
2938 * @dentry2: second dentry
2939 */
2940void d_exchange(struct dentry *dentry1, struct dentry *dentry2)
2941{
2942 write_seqlock(&rename_lock);
2943
2944 WARN_ON(!dentry1->d_inode);
2945 WARN_ON(!dentry2->d_inode);
2946 WARN_ON(IS_ROOT(dentry1));
2947 WARN_ON(IS_ROOT(dentry2));
2948
2949 __d_move(dentry1, dentry2, true);
2950
2951 write_sequnlock(&rename_lock);
2952}
2953
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002954/**
2955 * d_ancestor - search for an ancestor
2956 * @p1: ancestor dentry
2957 * @p2: child dentry
2958 *
2959 * Returns the ancestor dentry of p2 which is a child of p1, if p1 is
2960 * an ancestor of p2, else NULL.
Trond Myklebust9eaef272006-10-21 10:24:20 -07002961 */
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002962struct dentry *d_ancestor(struct dentry *p1, struct dentry *p2)
Trond Myklebust9eaef272006-10-21 10:24:20 -07002963{
2964 struct dentry *p;
2965
OGAWA Hirofumi871c0062008-10-16 07:50:27 +09002966 for (p = p2; !IS_ROOT(p); p = p->d_parent) {
Trond Myklebust9eaef272006-10-21 10:24:20 -07002967 if (p->d_parent == p1)
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002968 return p;
Trond Myklebust9eaef272006-10-21 10:24:20 -07002969 }
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002970 return NULL;
Trond Myklebust9eaef272006-10-21 10:24:20 -07002971}
2972
2973/*
2974 * This helper attempts to cope with remotely renamed directories
2975 *
2976 * It assumes that the caller is already holding
Eric W. Biedermana03e2832015-08-15 13:36:41 -05002977 * dentry->d_parent->d_inode->i_mutex, and rename_lock
Trond Myklebust9eaef272006-10-21 10:24:20 -07002978 *
2979 * Note: If ever the locking in lock_rename() changes, then please
2980 * remember to update this too...
Trond Myklebust9eaef272006-10-21 10:24:20 -07002981 */
Al Virob5ae6b12014-10-12 22:16:02 -04002982static int __d_unalias(struct inode *inode,
Nick Piggin873feea2011-01-07 17:50:06 +11002983 struct dentry *dentry, struct dentry *alias)
Trond Myklebust9eaef272006-10-21 10:24:20 -07002984{
Al Viro9902af72016-04-15 15:08:36 -04002985 struct mutex *m1 = NULL;
2986 struct rw_semaphore *m2 = NULL;
J. Bruce Fields3d330dc2015-02-10 10:55:53 -05002987 int ret = -ESTALE;
Trond Myklebust9eaef272006-10-21 10:24:20 -07002988
2989 /* If alias and dentry share a parent, then no extra locks required */
2990 if (alias->d_parent == dentry->d_parent)
2991 goto out_unalias;
2992
Trond Myklebust9eaef272006-10-21 10:24:20 -07002993 /* See lock_rename() */
Trond Myklebust9eaef272006-10-21 10:24:20 -07002994 if (!mutex_trylock(&dentry->d_sb->s_vfs_rename_mutex))
2995 goto out_err;
2996 m1 = &dentry->d_sb->s_vfs_rename_mutex;
Al Viro9902af72016-04-15 15:08:36 -04002997 if (!inode_trylock_shared(alias->d_parent->d_inode))
Trond Myklebust9eaef272006-10-21 10:24:20 -07002998 goto out_err;
Al Viro9902af72016-04-15 15:08:36 -04002999 m2 = &alias->d_parent->d_inode->i_rwsem;
Trond Myklebust9eaef272006-10-21 10:24:20 -07003000out_unalias:
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07003001 __d_move(alias, dentry, false);
Al Virob5ae6b12014-10-12 22:16:02 -04003002 ret = 0;
Trond Myklebust9eaef272006-10-21 10:24:20 -07003003out_err:
Trond Myklebust9eaef272006-10-21 10:24:20 -07003004 if (m2)
Al Viro9902af72016-04-15 15:08:36 -04003005 up_read(m2);
Trond Myklebust9eaef272006-10-21 10:24:20 -07003006 if (m1)
3007 mutex_unlock(m1);
3008 return ret;
3009}
3010
David Howells770bfad2006-08-22 20:06:07 -04003011/**
J. Bruce Fields3f70bd52014-02-18 14:11:26 -05003012 * d_splice_alias - splice a disconnected dentry into the tree if one exists
3013 * @inode: the inode which may have a disconnected dentry
3014 * @dentry: a negative dentry which we want to point to the inode.
3015 *
J. Bruce Fieldsda093a92014-02-17 18:03:57 -05003016 * If inode is a directory and has an IS_ROOT alias, then d_move that in
3017 * place of the given dentry and return it, else simply d_add the inode
3018 * to the dentry and return NULL.
J. Bruce Fields3f70bd52014-02-18 14:11:26 -05003019 *
J. Bruce Fields908790f2014-02-17 17:58:42 -05003020 * If a non-IS_ROOT directory is found, the filesystem is corrupt, and
3021 * we should error out: directories can't have multiple aliases.
3022 *
J. Bruce Fields3f70bd52014-02-18 14:11:26 -05003023 * This is needed in the lookup routine of any filesystem that is exportable
3024 * (via knfsd) so that we can build dcache paths to directories effectively.
3025 *
3026 * If a dentry was found and moved, then it is returned. Otherwise NULL
3027 * is returned. This matches the expected return value of ->lookup.
3028 *
3029 * Cluster filesystems may call this function with a negative, hashed dentry.
3030 * In that case, we know that the inode will be a regular file, and also this
3031 * will only occur during atomic_open. So we need to check for the dentry
3032 * being already hashed only in the final case.
3033 */
3034struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
3035{
J. Bruce Fields3f70bd52014-02-18 14:11:26 -05003036 if (IS_ERR(inode))
3037 return ERR_CAST(inode);
3038
David Howells770bfad2006-08-22 20:06:07 -04003039 BUG_ON(!d_unhashed(dentry));
3040
Al Virode689f52016-03-09 18:05:42 -05003041 if (!inode)
Al Virob5ae6b12014-10-12 22:16:02 -04003042 goto out;
Al Virode689f52016-03-09 18:05:42 -05003043
Al Virob9680912016-04-11 00:53:26 -04003044 security_d_instantiate(dentry, inode);
Nick Piggin873feea2011-01-07 17:50:06 +11003045 spin_lock(&inode->i_lock);
Trond Myklebust9eaef272006-10-21 10:24:20 -07003046 if (S_ISDIR(inode->i_mode)) {
Al Virob5ae6b12014-10-12 22:16:02 -04003047 struct dentry *new = __d_find_any_alias(inode);
3048 if (unlikely(new)) {
Eric W. Biedermana03e2832015-08-15 13:36:41 -05003049 /* The reference to new ensures it remains an alias */
3050 spin_unlock(&inode->i_lock);
Al Viro18367502011-07-12 21:42:24 -04003051 write_seqlock(&rename_lock);
Al Virob5ae6b12014-10-12 22:16:02 -04003052 if (unlikely(d_ancestor(new, dentry))) {
Al Viro18367502011-07-12 21:42:24 -04003053 write_sequnlock(&rename_lock);
Al Virob5ae6b12014-10-12 22:16:02 -04003054 dput(new);
3055 new = ERR_PTR(-ELOOP);
3056 pr_warn_ratelimited(
3057 "VFS: Lookup of '%s' in %s %s"
3058 " would have caused loop\n",
3059 dentry->d_name.name,
3060 inode->i_sb->s_type->name,
3061 inode->i_sb->s_id);
3062 } else if (!IS_ROOT(new)) {
3063 int err = __d_unalias(inode, dentry, new);
3064 write_sequnlock(&rename_lock);
3065 if (err) {
3066 dput(new);
3067 new = ERR_PTR(err);
3068 }
Al Viro18367502011-07-12 21:42:24 -04003069 } else {
Al Virob5ae6b12014-10-12 22:16:02 -04003070 __d_move(new, dentry, false);
3071 write_sequnlock(&rename_lock);
Trond Myklebust9eaef272006-10-21 10:24:20 -07003072 }
Al Virob5ae6b12014-10-12 22:16:02 -04003073 iput(inode);
3074 return new;
Trond Myklebust9eaef272006-10-21 10:24:20 -07003075 }
David Howells770bfad2006-08-22 20:06:07 -04003076 }
Al Virob5ae6b12014-10-12 22:16:02 -04003077out:
Al Viroed782b52016-03-09 19:52:39 -05003078 __d_add(dentry, inode);
Al Virob5ae6b12014-10-12 22:16:02 -04003079 return NULL;
David Howells770bfad2006-08-22 20:06:07 -04003080}
Al Virob5ae6b12014-10-12 22:16:02 -04003081EXPORT_SYMBOL(d_splice_alias);
David Howells770bfad2006-08-22 20:06:07 -04003082
Miklos Szeredicdd16d02008-06-23 18:11:53 +02003083static int prepend(char **buffer, int *buflen, const char *str, int namelen)
Ram Pai6092d042008-03-27 13:06:20 +01003084{
3085 *buflen -= namelen;
3086 if (*buflen < 0)
3087 return -ENAMETOOLONG;
3088 *buffer -= namelen;
3089 memcpy(*buffer, str, namelen);
3090 return 0;
3091}
3092
Waiman Long232d2d62013-09-09 12:18:13 -04003093/**
3094 * prepend_name - prepend a pathname in front of current buffer pointer
Waiman Long18129972013-09-12 10:55:35 -04003095 * @buffer: buffer pointer
3096 * @buflen: allocated length of the buffer
3097 * @name: name string and length qstr structure
Waiman Long232d2d62013-09-09 12:18:13 -04003098 *
Mark Rutland66702eb2017-10-23 14:07:14 -07003099 * With RCU path tracing, it may race with d_move(). Use READ_ONCE() to
Waiman Long232d2d62013-09-09 12:18:13 -04003100 * make sure that either the old or the new name pointer and length are
3101 * fetched. However, there may be mismatch between length and pointer.
3102 * The length cannot be trusted, we need to copy it byte-by-byte until
3103 * the length is reached or a null byte is found. It also prepends "/" at
3104 * the beginning of the name. The sequence number check at the caller will
3105 * retry it again when a d_move() does happen. So any garbage in the buffer
3106 * due to mismatched pointer and length will be discarded.
Al Viro6d13f692014-09-29 14:46:30 -04003107 *
Paul E. McKenney7088efa2017-10-09 10:04:27 -07003108 * Load acquire is needed to make sure that we see that terminating NUL.
Waiman Long232d2d62013-09-09 12:18:13 -04003109 */
Al Viro9aba36d2016-07-20 22:28:45 -04003110static int prepend_name(char **buffer, int *buflen, const struct qstr *name)
Miklos Szeredicdd16d02008-06-23 18:11:53 +02003111{
Paul E. McKenney7088efa2017-10-09 10:04:27 -07003112 const char *dname = smp_load_acquire(&name->name); /* ^^^ */
Mark Rutland66702eb2017-10-23 14:07:14 -07003113 u32 dlen = READ_ONCE(name->len);
Waiman Long232d2d62013-09-09 12:18:13 -04003114 char *p;
3115
Waiman Long232d2d62013-09-09 12:18:13 -04003116 *buflen -= dlen + 1;
Al Viroe8251962014-03-23 00:28:40 -04003117 if (*buflen < 0)
3118 return -ENAMETOOLONG;
Waiman Long232d2d62013-09-09 12:18:13 -04003119 p = *buffer -= dlen + 1;
3120 *p++ = '/';
3121 while (dlen--) {
3122 char c = *dname++;
3123 if (!c)
3124 break;
3125 *p++ = c;
3126 }
3127 return 0;
Miklos Szeredicdd16d02008-06-23 18:11:53 +02003128}
3129
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130/**
Randy Dunlap208898c2010-11-18 15:02:49 -08003131 * prepend_path - Prepend path string to a buffer
Miklos Szeredif2eb6572010-08-10 11:41:39 +02003132 * @path: the dentry/vfsmount to report
Al Viro02125a82011-12-05 08:43:34 -05003133 * @root: root vfsmnt/dentry
Miklos Szeredif2eb6572010-08-10 11:41:39 +02003134 * @buffer: pointer to the end of the buffer
3135 * @buflen: pointer to buffer length
3136 *
Waiman Long18129972013-09-12 10:55:35 -04003137 * The function will first try to write out the pathname without taking any
3138 * lock other than the RCU read lock to make sure that dentries won't go away.
3139 * It only checks the sequence number of the global rename_lock as any change
3140 * in the dentry's d_seq will be preceded by changes in the rename_lock
3141 * sequence number. If the sequence number had been changed, it will restart
3142 * the whole pathname back-tracing sequence again by taking the rename_lock.
3143 * In this case, there is no need to take the RCU read lock as the recursive
3144 * parent pointer references will keep the dentry chain alive as long as no
3145 * rename operation is performed.
Miklos Szeredif2eb6572010-08-10 11:41:39 +02003146 */
Al Viro02125a82011-12-05 08:43:34 -05003147static int prepend_path(const struct path *path,
3148 const struct path *root,
Miklos Szeredif2eb6572010-08-10 11:41:39 +02003149 char **buffer, int *buflen)
3150{
Al Viroede4ceb2013-11-13 07:45:40 -05003151 struct dentry *dentry;
3152 struct vfsmount *vfsmnt;
3153 struct mount *mnt;
Miklos Szeredif2eb6572010-08-10 11:41:39 +02003154 int error = 0;
Al Viro48a066e2013-09-29 22:06:07 -04003155 unsigned seq, m_seq = 0;
Waiman Long232d2d62013-09-09 12:18:13 -04003156 char *bptr;
3157 int blen;
Miklos Szeredif2eb6572010-08-10 11:41:39 +02003158
Al Viro48f5ec22013-09-09 15:22:25 -04003159 rcu_read_lock();
Al Viro48a066e2013-09-29 22:06:07 -04003160restart_mnt:
3161 read_seqbegin_or_lock(&mount_lock, &m_seq);
3162 seq = 0;
Li Zhong4ec6c2a2013-11-13 15:21:51 +08003163 rcu_read_lock();
Waiman Long232d2d62013-09-09 12:18:13 -04003164restart:
3165 bptr = *buffer;
3166 blen = *buflen;
Al Viro48a066e2013-09-29 22:06:07 -04003167 error = 0;
Al Viroede4ceb2013-11-13 07:45:40 -05003168 dentry = path->dentry;
3169 vfsmnt = path->mnt;
3170 mnt = real_mount(vfsmnt);
Waiman Long232d2d62013-09-09 12:18:13 -04003171 read_seqbegin_or_lock(&rename_lock, &seq);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02003172 while (dentry != root->dentry || vfsmnt != root->mnt) {
3173 struct dentry * parent;
3174
3175 if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
Mark Rutland66702eb2017-10-23 14:07:14 -07003176 struct mount *parent = READ_ONCE(mnt->mnt_parent);
Eric W. Biedermancde93be2015-08-15 13:36:12 -05003177 /* Escaped? */
3178 if (dentry != vfsmnt->mnt_root) {
3179 bptr = *buffer;
3180 blen = *buflen;
3181 error = 3;
3182 break;
3183 }
Miklos Szeredif2eb6572010-08-10 11:41:39 +02003184 /* Global root? */
Al Viro48a066e2013-09-29 22:06:07 -04003185 if (mnt != parent) {
Mark Rutland66702eb2017-10-23 14:07:14 -07003186 dentry = READ_ONCE(mnt->mnt_mountpoint);
Al Viro48a066e2013-09-29 22:06:07 -04003187 mnt = parent;
Waiman Long232d2d62013-09-09 12:18:13 -04003188 vfsmnt = &mnt->mnt;
3189 continue;
3190 }
Waiman Long232d2d62013-09-09 12:18:13 -04003191 if (!error)
3192 error = is_mounted(vfsmnt) ? 1 : 2;
3193 break;
Miklos Szeredif2eb6572010-08-10 11:41:39 +02003194 }
3195 parent = dentry->d_parent;
3196 prefetch(parent);
Waiman Long232d2d62013-09-09 12:18:13 -04003197 error = prepend_name(&bptr, &blen, &dentry->d_name);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02003198 if (error)
3199 break;
3200
Miklos Szeredif2eb6572010-08-10 11:41:39 +02003201 dentry = parent;
3202 }
Al Viro48f5ec22013-09-09 15:22:25 -04003203 if (!(seq & 1))
3204 rcu_read_unlock();
3205 if (need_seqretry(&rename_lock, seq)) {
3206 seq = 1;
Waiman Long232d2d62013-09-09 12:18:13 -04003207 goto restart;
Miklos Szeredif2eb6572010-08-10 11:41:39 +02003208 }
Al Viro48f5ec22013-09-09 15:22:25 -04003209 done_seqretry(&rename_lock, seq);
Li Zhong4ec6c2a2013-11-13 15:21:51 +08003210
3211 if (!(m_seq & 1))
3212 rcu_read_unlock();
Al Viro48a066e2013-09-29 22:06:07 -04003213 if (need_seqretry(&mount_lock, m_seq)) {
3214 m_seq = 1;
3215 goto restart_mnt;
3216 }
3217 done_seqretry(&mount_lock, m_seq);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02003218
Waiman Long232d2d62013-09-09 12:18:13 -04003219 if (error >= 0 && bptr == *buffer) {
3220 if (--blen < 0)
3221 error = -ENAMETOOLONG;
3222 else
3223 *--bptr = '/';
Miklos Szeredif2eb6572010-08-10 11:41:39 +02003224 }
Waiman Long232d2d62013-09-09 12:18:13 -04003225 *buffer = bptr;
3226 *buflen = blen;
Al Viro7ea600b2013-03-26 18:25:57 -04003227 return error;
Miklos Szeredif2eb6572010-08-10 11:41:39 +02003228}
3229
3230/**
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +02003231 * __d_path - return the path of a dentry
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01003232 * @path: the dentry/vfsmount to report
Al Viro02125a82011-12-05 08:43:34 -05003233 * @root: root vfsmnt/dentry
Randy Dunlapcd956a12010-08-14 13:05:31 -07003234 * @buf: buffer to return value in
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 * @buflen: buffer length
3236 *
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02003237 * Convert a dentry into an ASCII path name.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 *
Arjan van de Ven52afeef2008-12-01 14:35:00 -08003239 * Returns a pointer into the buffer or an error code if the
3240 * path was too long.
Linus Torvalds552ce542007-02-13 12:08:18 -08003241 *
Christoph Hellwigbe148242010-10-10 05:36:21 -04003242 * "buflen" should be positive.
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01003243 *
Al Viro02125a82011-12-05 08:43:34 -05003244 * If the path is not reachable from the supplied root, return %NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 */
Al Viro02125a82011-12-05 08:43:34 -05003246char *__d_path(const struct path *path,
3247 const struct path *root,
Miklos Szeredif2eb6572010-08-10 11:41:39 +02003248 char *buf, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249{
Miklos Szeredif2eb6572010-08-10 11:41:39 +02003250 char *res = buf + buflen;
3251 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252
Miklos Szeredif2eb6572010-08-10 11:41:39 +02003253 prepend(&res, &buflen, "\0", 1);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02003254 error = prepend_path(path, root, &res, &buflen);
Christoph Hellwigbe148242010-10-10 05:36:21 -04003255
Al Viro02125a82011-12-05 08:43:34 -05003256 if (error < 0)
3257 return ERR_PTR(error);
3258 if (error > 0)
3259 return NULL;
3260 return res;
3261}
3262
3263char *d_absolute_path(const struct path *path,
3264 char *buf, int buflen)
3265{
3266 struct path root = {};
3267 char *res = buf + buflen;
3268 int error;
3269
3270 prepend(&res, &buflen, "\0", 1);
Al Viro02125a82011-12-05 08:43:34 -05003271 error = prepend_path(path, &root, &res, &buflen);
Al Viro02125a82011-12-05 08:43:34 -05003272
3273 if (error > 1)
3274 error = -EINVAL;
3275 if (error < 0)
Miklos Szeredif2eb6572010-08-10 11:41:39 +02003276 return ERR_PTR(error);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02003277 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278}
3279
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02003280/*
3281 * same as __d_path but appends "(deleted)" for unlinked files.
3282 */
Al Viro02125a82011-12-05 08:43:34 -05003283static int path_with_deleted(const struct path *path,
3284 const struct path *root,
3285 char **buf, int *buflen)
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02003286{
3287 prepend(buf, buflen, "\0", 1);
3288 if (d_unlinked(path->dentry)) {
3289 int error = prepend(buf, buflen, " (deleted)", 10);
3290 if (error)
3291 return error;
3292 }
3293
3294 return prepend_path(path, root, buf, buflen);
3295}
3296
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02003297static int prepend_unreachable(char **buffer, int *buflen)
3298{
3299 return prepend(buffer, buflen, "(unreachable)", 13);
3300}
3301
Linus Torvalds68f0d9d2013-09-12 13:24:55 -07003302static void get_fs_root_rcu(struct fs_struct *fs, struct path *root)
3303{
3304 unsigned seq;
3305
3306 do {
3307 seq = read_seqcount_begin(&fs->seq);
3308 *root = fs->root;
3309 } while (read_seqcount_retry(&fs->seq, seq));
3310}
3311
Jan Bluncka03a8a702008-02-14 19:38:32 -08003312/**
3313 * d_path - return the path of a dentry
Jan Blunckcf28b482008-02-14 19:38:44 -08003314 * @path: path to report
Jan Bluncka03a8a702008-02-14 19:38:32 -08003315 * @buf: buffer to return value in
3316 * @buflen: buffer length
3317 *
3318 * Convert a dentry into an ASCII path name. If the entry has been deleted
3319 * the string " (deleted)" is appended. Note that this is ambiguous.
3320 *
Arjan van de Ven52afeef2008-12-01 14:35:00 -08003321 * Returns a pointer into the buffer or an error code if the path was
3322 * too long. Note: Callers should use the returned pointer, not the passed
3323 * in buffer, to use the name! The implementation often starts at an offset
3324 * into the buffer, and may leave 0 bytes at the start.
Jan Bluncka03a8a702008-02-14 19:38:32 -08003325 *
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +02003326 * "buflen" should be positive.
Jan Bluncka03a8a702008-02-14 19:38:32 -08003327 */
Jan Engelhardt20d4fdc2008-06-09 16:40:36 -07003328char *d_path(const struct path *path, char *buf, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329{
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02003330 char *res = buf + buflen;
Jan Blunck6ac08c32008-02-14 19:34:38 -08003331 struct path root;
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02003332 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333
Eric Dumazetc23fbb62007-05-08 00:26:18 -07003334 /*
3335 * We have various synthetic filesystems that never get mounted. On
3336 * these filesystems dentries are never used for lookup purposes, and
3337 * thus don't need to be hashed. They also don't need a name until a
3338 * user wants to identify the object in /proc/pid/fd/. The little hack
3339 * below allows us to generate a name for these objects on demand:
Eric W. Biedermanf48cfdd2013-11-08 16:31:29 -08003340 *
3341 * Some pseudo inodes are mountable. When they are mounted
3342 * path->dentry == path->mnt->mnt_root. In that case don't call d_dname
3343 * and instead have d_path return the mounted path.
Eric Dumazetc23fbb62007-05-08 00:26:18 -07003344 */
Eric W. Biedermanf48cfdd2013-11-08 16:31:29 -08003345 if (path->dentry->d_op && path->dentry->d_op->d_dname &&
3346 (!IS_ROOT(path->dentry) || path->dentry != path->mnt->mnt_root))
Jan Blunckcf28b482008-02-14 19:38:44 -08003347 return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
Eric Dumazetc23fbb62007-05-08 00:26:18 -07003348
Linus Torvalds68f0d9d2013-09-12 13:24:55 -07003349 rcu_read_lock();
3350 get_fs_root_rcu(current->fs, &root);
Al Viro02125a82011-12-05 08:43:34 -05003351 error = path_with_deleted(path, &root, &res, &buflen);
Linus Torvalds68f0d9d2013-09-12 13:24:55 -07003352 rcu_read_unlock();
3353
Al Viro02125a82011-12-05 08:43:34 -05003354 if (error < 0)
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02003355 res = ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 return res;
3357}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07003358EXPORT_SYMBOL(d_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359
3360/*
Eric Dumazetc23fbb62007-05-08 00:26:18 -07003361 * Helper function for dentry_operations.d_dname() members
3362 */
3363char *dynamic_dname(struct dentry *dentry, char *buffer, int buflen,
3364 const char *fmt, ...)
3365{
3366 va_list args;
3367 char temp[64];
3368 int sz;
3369
3370 va_start(args, fmt);
3371 sz = vsnprintf(temp, sizeof(temp), fmt, args) + 1;
3372 va_end(args);
3373
3374 if (sz > sizeof(temp) || sz > buflen)
3375 return ERR_PTR(-ENAMETOOLONG);
3376
3377 buffer += buflen - sz;
3378 return memcpy(buffer, temp, sz);
3379}
3380
Al Viro118b2302013-08-24 12:08:17 -04003381char *simple_dname(struct dentry *dentry, char *buffer, int buflen)
3382{
3383 char *end = buffer + buflen;
3384 /* these dentries are never renamed, so d_lock is not needed */
3385 if (prepend(&end, &buflen, " (deleted)", 11) ||
Waiman Long232d2d62013-09-09 12:18:13 -04003386 prepend(&end, &buflen, dentry->d_name.name, dentry->d_name.len) ||
Al Viro118b2302013-08-24 12:08:17 -04003387 prepend(&end, &buflen, "/", 1))
3388 end = ERR_PTR(-ENAMETOOLONG);
Waiman Long232d2d62013-09-09 12:18:13 -04003389 return end;
Al Viro118b2302013-08-24 12:08:17 -04003390}
David Herrmann31bbe162014-01-03 14:09:47 +01003391EXPORT_SYMBOL(simple_dname);
Al Viro118b2302013-08-24 12:08:17 -04003392
Eric Dumazetc23fbb62007-05-08 00:26:18 -07003393/*
Ram Pai6092d042008-03-27 13:06:20 +01003394 * Write full pathname from the root of the filesystem into the buffer.
3395 */
Al Virof6500802014-01-26 12:37:55 -05003396static char *__dentry_path(struct dentry *d, char *buf, int buflen)
Ram Pai6092d042008-03-27 13:06:20 +01003397{
Al Virof6500802014-01-26 12:37:55 -05003398 struct dentry *dentry;
Waiman Long232d2d62013-09-09 12:18:13 -04003399 char *end, *retval;
3400 int len, seq = 0;
3401 int error = 0;
Ram Pai6092d042008-03-27 13:06:20 +01003402
Al Virof6500802014-01-26 12:37:55 -05003403 if (buflen < 2)
3404 goto Elong;
3405
Al Viro48f5ec22013-09-09 15:22:25 -04003406 rcu_read_lock();
Waiman Long232d2d62013-09-09 12:18:13 -04003407restart:
Al Virof6500802014-01-26 12:37:55 -05003408 dentry = d;
Waiman Long232d2d62013-09-09 12:18:13 -04003409 end = buf + buflen;
3410 len = buflen;
3411 prepend(&end, &len, "\0", 1);
Ram Pai6092d042008-03-27 13:06:20 +01003412 /* Get '/' right */
3413 retval = end-1;
3414 *retval = '/';
Waiman Long232d2d62013-09-09 12:18:13 -04003415 read_seqbegin_or_lock(&rename_lock, &seq);
Miklos Szeredicdd16d02008-06-23 18:11:53 +02003416 while (!IS_ROOT(dentry)) {
3417 struct dentry *parent = dentry->d_parent;
Ram Pai6092d042008-03-27 13:06:20 +01003418
Ram Pai6092d042008-03-27 13:06:20 +01003419 prefetch(parent);
Waiman Long232d2d62013-09-09 12:18:13 -04003420 error = prepend_name(&end, &len, &dentry->d_name);
3421 if (error)
3422 break;
Ram Pai6092d042008-03-27 13:06:20 +01003423
3424 retval = end;
3425 dentry = parent;
3426 }
Al Viro48f5ec22013-09-09 15:22:25 -04003427 if (!(seq & 1))
3428 rcu_read_unlock();
3429 if (need_seqretry(&rename_lock, seq)) {
3430 seq = 1;
Waiman Long232d2d62013-09-09 12:18:13 -04003431 goto restart;
Al Viro48f5ec22013-09-09 15:22:25 -04003432 }
3433 done_seqretry(&rename_lock, seq);
Waiman Long232d2d62013-09-09 12:18:13 -04003434 if (error)
3435 goto Elong;
Al Viroc1031352010-06-06 22:31:14 -04003436 return retval;
3437Elong:
3438 return ERR_PTR(-ENAMETOOLONG);
3439}
Nick Pigginec2447c2011-01-07 17:49:29 +11003440
3441char *dentry_path_raw(struct dentry *dentry, char *buf, int buflen)
3442{
Waiman Long232d2d62013-09-09 12:18:13 -04003443 return __dentry_path(dentry, buf, buflen);
Nick Pigginec2447c2011-01-07 17:49:29 +11003444}
3445EXPORT_SYMBOL(dentry_path_raw);
Al Viroc1031352010-06-06 22:31:14 -04003446
3447char *dentry_path(struct dentry *dentry, char *buf, int buflen)
3448{
3449 char *p = NULL;
3450 char *retval;
3451
Al Viroc1031352010-06-06 22:31:14 -04003452 if (d_unlinked(dentry)) {
3453 p = buf + buflen;
3454 if (prepend(&p, &buflen, "//deleted", 10) != 0)
3455 goto Elong;
3456 buflen++;
3457 }
3458 retval = __dentry_path(dentry, buf, buflen);
Al Viroc1031352010-06-06 22:31:14 -04003459 if (!IS_ERR(retval) && p)
3460 *p = '/'; /* restore '/' overriden with '\0' */
Ram Pai6092d042008-03-27 13:06:20 +01003461 return retval;
3462Elong:
Ram Pai6092d042008-03-27 13:06:20 +01003463 return ERR_PTR(-ENAMETOOLONG);
3464}
3465
Linus Torvalds8b19e342013-09-12 10:35:47 -07003466static void get_fs_root_and_pwd_rcu(struct fs_struct *fs, struct path *root,
3467 struct path *pwd)
Linus Torvalds57624822013-09-12 10:12:47 -07003468{
Linus Torvalds8b19e342013-09-12 10:35:47 -07003469 unsigned seq;
3470
3471 do {
3472 seq = read_seqcount_begin(&fs->seq);
3473 *root = fs->root;
3474 *pwd = fs->pwd;
3475 } while (read_seqcount_retry(&fs->seq, seq));
Linus Torvalds57624822013-09-12 10:12:47 -07003476}
3477
Ram Pai6092d042008-03-27 13:06:20 +01003478/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 * NOTE! The user-level library version returns a
3480 * character pointer. The kernel system call just
3481 * returns the length of the buffer filled (which
3482 * includes the ending '\0' character), or a negative
3483 * error value. So libc would do something like
3484 *
3485 * char *getcwd(char * buf, size_t size)
3486 * {
3487 * int retval;
3488 *
3489 * retval = sys_getcwd(buf, size);
3490 * if (retval >= 0)
3491 * return buf;
3492 * errno = -retval;
3493 * return NULL;
3494 * }
3495 */
Heiko Carstens3cdad422009-01-14 14:14:22 +01003496SYSCALL_DEFINE2(getcwd, char __user *, buf, unsigned long, size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497{
Linus Torvalds552ce542007-02-13 12:08:18 -08003498 int error;
Jan Blunck6ac08c32008-02-14 19:34:38 -08003499 struct path pwd, root;
Linus Torvalds3272c542013-09-12 12:40:15 -07003500 char *page = __getname();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501
3502 if (!page)
3503 return -ENOMEM;
3504
Linus Torvalds8b19e342013-09-12 10:35:47 -07003505 rcu_read_lock();
3506 get_fs_root_and_pwd_rcu(current->fs, &root, &pwd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507
Linus Torvalds552ce542007-02-13 12:08:18 -08003508 error = -ENOENT;
Alexey Dobriyanf3da3922009-05-04 03:32:03 +04003509 if (!d_unlinked(pwd.dentry)) {
Linus Torvalds552ce542007-02-13 12:08:18 -08003510 unsigned long len;
Linus Torvalds3272c542013-09-12 12:40:15 -07003511 char *cwd = page + PATH_MAX;
3512 int buflen = PATH_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02003514 prepend(&cwd, &buflen, "\0", 1);
Al Viro02125a82011-12-05 08:43:34 -05003515 error = prepend_path(&pwd, &root, &cwd, &buflen);
Linus Torvaldsff812d72013-09-12 11:57:01 -07003516 rcu_read_unlock();
Linus Torvalds552ce542007-02-13 12:08:18 -08003517
Al Viro02125a82011-12-05 08:43:34 -05003518 if (error < 0)
Linus Torvalds552ce542007-02-13 12:08:18 -08003519 goto out;
3520
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02003521 /* Unreachable from current root */
Al Viro02125a82011-12-05 08:43:34 -05003522 if (error > 0) {
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02003523 error = prepend_unreachable(&cwd, &buflen);
3524 if (error)
3525 goto out;
3526 }
3527
Linus Torvalds552ce542007-02-13 12:08:18 -08003528 error = -ERANGE;
Linus Torvalds3272c542013-09-12 12:40:15 -07003529 len = PATH_MAX + page - cwd;
Linus Torvalds552ce542007-02-13 12:08:18 -08003530 if (len <= size) {
3531 error = len;
3532 if (copy_to_user(buf, cwd, len))
3533 error = -EFAULT;
3534 }
Nick Piggin949854d2011-01-07 17:49:37 +11003535 } else {
Linus Torvaldsff812d72013-09-12 11:57:01 -07003536 rcu_read_unlock();
Nick Piggin949854d2011-01-07 17:49:37 +11003537 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538
3539out:
Linus Torvalds3272c542013-09-12 12:40:15 -07003540 __putname(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 return error;
3542}
3543
3544/*
3545 * Test whether new_dentry is a subdirectory of old_dentry.
3546 *
3547 * Trivially implemented using the dcache structure
3548 */
3549
3550/**
3551 * is_subdir - is new dentry a subdirectory of old_dentry
3552 * @new_dentry: new dentry
3553 * @old_dentry: old dentry
3554 *
Yaowei Baia6e57872015-11-17 14:40:11 +08003555 * Returns true if new_dentry is a subdirectory of the parent (at any depth).
3556 * Returns false otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
3558 */
3559
Yaowei Baia6e57872015-11-17 14:40:11 +08003560bool is_subdir(struct dentry *new_dentry, struct dentry *old_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561{
Yaowei Baia6e57872015-11-17 14:40:11 +08003562 bool result;
Nick Piggin949854d2011-01-07 17:49:37 +11003563 unsigned seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09003565 if (new_dentry == old_dentry)
Yaowei Baia6e57872015-11-17 14:40:11 +08003566 return true;
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09003567
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09003568 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 /* for restarting inner loop in case of seq retry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 seq = read_seqbegin(&rename_lock);
Nick Piggin949854d2011-01-07 17:49:37 +11003571 /*
3572 * Need rcu_readlock to protect against the d_parent trashing
3573 * due to d_move
3574 */
3575 rcu_read_lock();
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09003576 if (d_ancestor(old_dentry, new_dentry))
Yaowei Baia6e57872015-11-17 14:40:11 +08003577 result = true;
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09003578 else
Yaowei Baia6e57872015-11-17 14:40:11 +08003579 result = false;
Nick Piggin949854d2011-01-07 17:49:37 +11003580 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 } while (read_seqretry(&rename_lock, seq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582
3583 return result;
3584}
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +02003585EXPORT_SYMBOL(is_subdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586
Miklos Szeredidb14fc32013-09-05 11:44:35 +02003587static enum d_walk_ret d_genocide_kill(void *data, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588{
Miklos Szeredidb14fc32013-09-05 11:44:35 +02003589 struct dentry *root = data;
3590 if (dentry != root) {
3591 if (d_unhashed(dentry) || !dentry->d_inode)
3592 return D_WALK_SKIP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593
Miklos Szeredi01ddc4e2013-09-05 11:44:34 +02003594 if (!(dentry->d_flags & DCACHE_GENOCIDE)) {
3595 dentry->d_flags |= DCACHE_GENOCIDE;
3596 dentry->d_lockref.count--;
3597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 }
Miklos Szeredidb14fc32013-09-05 11:44:35 +02003599 return D_WALK_CONTINUE;
3600}
Nick Piggin58db63d2011-01-07 17:49:39 +11003601
Miklos Szeredidb14fc32013-09-05 11:44:35 +02003602void d_genocide(struct dentry *parent)
3603{
3604 d_walk(parent, parent, d_genocide_kill, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605}
3606
Al Viro60545d02013-06-07 01:20:27 -04003607void d_tmpfile(struct dentry *dentry, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608{
Al Viro60545d02013-06-07 01:20:27 -04003609 inode_dec_link_count(inode);
3610 BUG_ON(dentry->d_name.name != dentry->d_iname ||
Al Viro946e51f2014-10-26 19:19:16 -04003611 !hlist_unhashed(&dentry->d_u.d_alias) ||
Al Viro60545d02013-06-07 01:20:27 -04003612 !d_unlinked(dentry));
3613 spin_lock(&dentry->d_parent->d_lock);
3614 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
3615 dentry->d_name.len = sprintf(dentry->d_iname, "#%llu",
3616 (unsigned long long)inode->i_ino);
3617 spin_unlock(&dentry->d_lock);
3618 spin_unlock(&dentry->d_parent->d_lock);
3619 d_instantiate(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620}
Al Viro60545d02013-06-07 01:20:27 -04003621EXPORT_SYMBOL(d_tmpfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622
3623static __initdata unsigned long dhash_entries;
3624static int __init set_dhash_entries(char *str)
3625{
3626 if (!str)
3627 return 0;
3628 dhash_entries = simple_strtoul(str, &str, 0);
3629 return 1;
3630}
3631__setup("dhash_entries=", set_dhash_entries);
3632
3633static void __init dcache_init_early(void)
3634{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 /* If hashes are distributed across NUMA nodes, defer
3636 * hash allocation until vmalloc space is available.
3637 */
3638 if (hashdist)
3639 return;
3640
3641 dentry_hashtable =
3642 alloc_large_system_hash("Dentry cache",
Linus Torvaldsb07ad992011-04-23 22:32:03 -07003643 sizeof(struct hlist_bl_head),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 dhash_entries,
3645 13,
Pavel Tatashin3d375d72017-07-06 15:39:11 -07003646 HASH_EARLY | HASH_ZERO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 &d_hash_shift,
Alexey Dobriyanb35d7862017-11-20 18:05:52 +03003648 NULL,
Tim Bird31fe62b2012-05-23 13:33:35 +00003649 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 0);
Alexey Dobriyan854d3e62017-11-20 18:05:07 +03003651 d_hash_shift = 32 - d_hash_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652}
3653
Denis Cheng74bf17c2007-10-16 23:26:30 -07003654static void __init dcache_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655{
Pavel Tatashin3d375d72017-07-06 15:39:11 -07003656 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 * A constructor could be added for stable state like the lists,
3658 * but it is probably not worth it because of the cache nature
Pavel Tatashin3d375d72017-07-06 15:39:11 -07003659 * of the dcache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 */
David Windsor80344262017-06-10 22:50:44 -04003661 dentry_cache = KMEM_CACHE_USERCOPY(dentry,
3662 SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|SLAB_MEM_SPREAD|SLAB_ACCOUNT,
3663 d_iname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664
3665 /* Hash may have been set up in dcache_init_early */
3666 if (!hashdist)
3667 return;
3668
3669 dentry_hashtable =
3670 alloc_large_system_hash("Dentry cache",
Linus Torvaldsb07ad992011-04-23 22:32:03 -07003671 sizeof(struct hlist_bl_head),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 dhash_entries,
3673 13,
Pavel Tatashin3d375d72017-07-06 15:39:11 -07003674 HASH_ZERO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 &d_hash_shift,
Alexey Dobriyanb35d7862017-11-20 18:05:52 +03003676 NULL,
Tim Bird31fe62b2012-05-23 13:33:35 +00003677 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 0);
Alexey Dobriyan854d3e62017-11-20 18:05:07 +03003679 d_hash_shift = 32 - d_hash_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680}
3681
3682/* SLAB cache for __getname() consumers */
Christoph Lametere18b8902006-12-06 20:33:20 -08003683struct kmem_cache *names_cachep __read_mostly;
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07003684EXPORT_SYMBOL(names_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686EXPORT_SYMBOL(d_genocide);
3687
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688void __init vfs_caches_init_early(void)
3689{
Sebastian Andrzej Siewior69163632017-06-27 18:19:11 +02003690 int i;
3691
3692 for (i = 0; i < ARRAY_SIZE(in_lookup_hashtable); i++)
3693 INIT_HLIST_BL_HEAD(&in_lookup_hashtable[i]);
3694
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695 dcache_init_early();
3696 inode_init_early();
3697}
3698
Mel Gorman4248b0d2015-08-06 15:46:20 -07003699void __init vfs_caches_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700{
David Windsor6a9b8822017-06-10 22:50:30 -04003701 names_cachep = kmem_cache_create_usercopy("names_cache", PATH_MAX, 0,
3702 SLAB_HWCACHE_ALIGN|SLAB_PANIC, 0, PATH_MAX, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703
Denis Cheng74bf17c2007-10-16 23:26:30 -07003704 dcache_init();
3705 inode_init();
Mel Gorman4248b0d2015-08-06 15:46:20 -07003706 files_init();
3707 files_maxfiles_init();
Denis Cheng74bf17c2007-10-16 23:26:30 -07003708 mnt_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 bdev_cache_init();
3710 chrdev_init();
3711}