blob: 49461353ac37bbebb68e9ba6d9a10fbfeffd6040 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * fs/dcache.c
4 *
5 * Complete reimplementation
6 * (C) 1997 Thomas Schoebel-Theuer,
7 * with heavy changes by Linus Torvalds
8 */
9
10/*
11 * Notes on the allocation strategy:
12 *
13 * The dcache is a master of the icache - whenever a dcache entry
14 * exists, the inode will always exist. "iput()" is done either when
15 * the dcache entry is deleted or garbage collected.
16 */
17
Al Viro7a5cf792018-03-05 19:15:50 -050018#include <linux/ratelimit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/string.h>
20#include <linux/mm.h>
21#include <linux/fs.h>
Eric Biggers0bf3d5c12019-03-20 11:39:11 -070022#include <linux/fscrypt.h>
John McCutchan7a91bf72005-08-08 13:52:16 -040023#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/slab.h>
25#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/hash.h>
27#include <linux/cache.h>
Paul Gortmaker630d9c42011-11-16 23:57:37 -050028#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/security.h>
30#include <linux/seqlock.h>
Mike Rapoport57c8a662018-10-30 15:09:49 -070031#include <linux/memblock.h>
Nick Pigginceb5bdc2011-01-07 17:50:05 +110032#include <linux/bit_spinlock.h>
33#include <linux/rculist_bl.h>
Dave Chinnerf6041562013-08-28 10:18:00 +100034#include <linux/list_lru.h>
David Howells07f3f052006-09-30 20:52:18 +020035#include "internal.h"
Al Virob2dba1a2011-11-23 19:26:23 -050036#include "mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Nick Piggin789680d2011-01-07 17:49:30 +110038/*
39 * Usage:
Nick Piggin873feea2011-01-07 17:50:06 +110040 * dcache->d_inode->i_lock protects:
Al Viro946e51f2014-10-26 19:19:16 -040041 * - i_dentry, d_u.d_alias, d_inode of aliases
Nick Pigginceb5bdc2011-01-07 17:50:05 +110042 * dcache_hash_bucket lock protects:
43 * - the dcache hash table
NeilBrownf1ee6162017-12-21 09:45:40 +110044 * s_roots bl list spinlock protects:
45 * - the s_roots list (see __d_drop)
Dave Chinner19156842013-08-28 10:17:55 +100046 * dentry->d_sb->s_dentry_lru_lock protects:
Nick Piggin23044502011-01-07 17:49:31 +110047 * - the dcache lru lists and counters
48 * d_lock protects:
49 * - d_flags
50 * - d_name
51 * - d_lru
Nick Pigginb7ab39f2011-01-07 17:49:32 +110052 * - d_count
Nick Pigginda502952011-01-07 17:49:33 +110053 * - d_unhashed()
Nick Piggin2fd6b7f2011-01-07 17:49:34 +110054 * - d_parent and d_subdirs
55 * - childrens' d_child and d_parent
Al Viro946e51f2014-10-26 19:19:16 -040056 * - d_u.d_alias, d_inode
Nick Piggin789680d2011-01-07 17:49:30 +110057 *
58 * Ordering:
Nick Piggin873feea2011-01-07 17:50:06 +110059 * dentry->d_inode->i_lock
Nick Pigginb5c84bf2011-01-07 17:49:38 +110060 * dentry->d_lock
Dave Chinner19156842013-08-28 10:17:55 +100061 * dentry->d_sb->s_dentry_lru_lock
Nick Pigginceb5bdc2011-01-07 17:50:05 +110062 * dcache_hash_bucket lock
NeilBrownf1ee6162017-12-21 09:45:40 +110063 * s_roots lock
Nick Piggin789680d2011-01-07 17:49:30 +110064 *
Nick Pigginda502952011-01-07 17:49:33 +110065 * If there is an ancestor relationship:
66 * dentry->d_parent->...->d_parent->d_lock
67 * ...
68 * dentry->d_parent->d_lock
69 * dentry->d_lock
70 *
71 * If no ancestor relationship:
Al Viro076515f2018-03-10 23:15:52 -050072 * arbitrary, since it's serialized on rename_lock
Nick Piggin789680d2011-01-07 17:49:30 +110073 */
Eric Dumazetfa3536c2006-03-26 01:37:24 -080074int sysctl_vfs_cache_pressure __read_mostly = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075EXPORT_SYMBOL_GPL(sysctl_vfs_cache_pressure);
76
Al Viro74c3cbe2007-07-22 08:04:18 -040077__cacheline_aligned_in_smp DEFINE_SEQLOCK(rename_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Nick Piggin949854d2011-01-07 17:49:37 +110079EXPORT_SYMBOL(rename_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Christoph Lametere18b8902006-12-06 20:33:20 -080081static struct kmem_cache *dentry_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
David Howellscdf01222017-07-04 17:25:22 +010083const struct qstr empty_name = QSTR_INIT("", 0);
84EXPORT_SYMBOL(empty_name);
85const struct qstr slash_name = QSTR_INIT("/", 1);
86EXPORT_SYMBOL(slash_name);
Al Viro80e5d1ff52021-04-15 19:46:50 -040087const struct qstr dotdot_name = QSTR_INIT("..", 2);
88EXPORT_SYMBOL(dotdot_name);
David Howellscdf01222017-07-04 17:25:22 +010089
Linus Torvalds1da177e2005-04-16 15:20:36 -070090/*
91 * This is the single most critical data structure when it comes
92 * to the dcache: the hashtable for lookups. Somebody should try
93 * to make this good - I've just made it work.
94 *
95 * This hash-function tries to avoid losing too many bits of hash
96 * information, yet avoid using a prime hash-size or similar.
97 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Eric Dumazetfa3536c2006-03-26 01:37:24 -080099static unsigned int d_hash_shift __read_mostly;
Nick Pigginceb5bdc2011-01-07 17:50:05 +1100100
Linus Torvaldsb07ad992011-04-23 22:32:03 -0700101static struct hlist_bl_head *dentry_hashtable __read_mostly;
Nick Pigginceb5bdc2011-01-07 17:50:05 +1100102
Linus Torvalds8387ff22016-06-10 07:51:30 -0700103static inline struct hlist_bl_head *d_hash(unsigned int hash)
Nick Pigginceb5bdc2011-01-07 17:50:05 +1100104{
Alexey Dobriyan854d3e62017-11-20 18:05:07 +0300105 return dentry_hashtable + (hash >> d_hash_shift);
Nick Pigginceb5bdc2011-01-07 17:50:05 +1100106}
107
Al Viro94bdd652016-04-15 02:42:04 -0400108#define IN_LOOKUP_SHIFT 10
109static struct hlist_bl_head in_lookup_hashtable[1 << IN_LOOKUP_SHIFT];
110
111static inline struct hlist_bl_head *in_lookup_hash(const struct dentry *parent,
112 unsigned int hash)
113{
114 hash += (unsigned long) parent / L1_CACHE_BYTES;
115 return in_lookup_hashtable + hash_32(hash, IN_LOOKUP_SHIFT);
116}
117
Luis Chamberlainc8c0c232022-01-21 22:12:59 -0800118struct dentry_stat_t {
119 long nr_dentry;
120 long nr_unused;
121 long age_limit; /* age in seconds */
122 long want_pages; /* pages requested by system */
123 long nr_negative; /* # of unused negative dentries */
124 long dummy; /* Reserved for future use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125};
126
Glauber Costa3942c072013-08-28 10:17:53 +1000127static DEFINE_PER_CPU(long, nr_dentry);
Dave Chinner62d36c72013-08-28 10:17:54 +1000128static DEFINE_PER_CPU(long, nr_dentry_unused);
Waiman Longaf0c9af2019-01-30 13:52:38 -0500129static DEFINE_PER_CPU(long, nr_dentry_negative);
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400130
131#if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS)
Luis Chamberlainc8c0c232022-01-21 22:12:59 -0800132/* Statistics gathering. */
133static struct dentry_stat_t dentry_stat = {
134 .age_limit = 45,
135};
Dave Chinner62d36c72013-08-28 10:17:54 +1000136
137/*
138 * Here we resort to our own counters instead of using generic per-cpu counters
139 * for consistency with what the vfs inode code does. We are expected to harvest
140 * better code and performance by having our own specialized counters.
141 *
142 * Please note that the loop is done over all possible CPUs, not over all online
143 * CPUs. The reason for this is that we don't want to play games with CPUs going
144 * on and off. If one of them goes off, we will just keep their counters.
145 *
146 * glommer: See cffbc8a for details, and if you ever intend to change this,
147 * please update all vfs counters to match.
148 */
Glauber Costa3942c072013-08-28 10:17:53 +1000149static long get_nr_dentry(void)
Nick Piggin3e880fb2011-01-07 17:49:19 +1100150{
151 int i;
Glauber Costa3942c072013-08-28 10:17:53 +1000152 long sum = 0;
Nick Piggin3e880fb2011-01-07 17:49:19 +1100153 for_each_possible_cpu(i)
154 sum += per_cpu(nr_dentry, i);
155 return sum < 0 ? 0 : sum;
156}
157
Dave Chinner62d36c72013-08-28 10:17:54 +1000158static long get_nr_dentry_unused(void)
159{
160 int i;
161 long sum = 0;
162 for_each_possible_cpu(i)
163 sum += per_cpu(nr_dentry_unused, i);
164 return sum < 0 ? 0 : sum;
165}
166
Waiman Longaf0c9af2019-01-30 13:52:38 -0500167static long get_nr_dentry_negative(void)
168{
169 int i;
170 long sum = 0;
171
172 for_each_possible_cpu(i)
173 sum += per_cpu(nr_dentry_negative, i);
174 return sum < 0 ? 0 : sum;
175}
176
Luis Chamberlainc8c0c232022-01-21 22:12:59 -0800177static int proc_nr_dentry(struct ctl_table *table, int write, void *buffer,
178 size_t *lenp, loff_t *ppos)
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400179{
Nick Piggin3e880fb2011-01-07 17:49:19 +1100180 dentry_stat.nr_dentry = get_nr_dentry();
Dave Chinner62d36c72013-08-28 10:17:54 +1000181 dentry_stat.nr_unused = get_nr_dentry_unused();
Waiman Longaf0c9af2019-01-30 13:52:38 -0500182 dentry_stat.nr_negative = get_nr_dentry_negative();
Glauber Costa3942c072013-08-28 10:17:53 +1000183 return proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400184}
Luis Chamberlainc8c0c232022-01-21 22:12:59 -0800185
186static struct ctl_table fs_dcache_sysctls[] = {
187 {
188 .procname = "dentry-state",
189 .data = &dentry_stat,
190 .maxlen = 6*sizeof(long),
191 .mode = 0444,
192 .proc_handler = proc_nr_dentry,
193 },
194 { }
195};
196
197static int __init init_fs_dcache_sysctls(void)
198{
199 register_sysctl_init("fs", fs_dcache_sysctls);
200 return 0;
201}
202fs_initcall(init_fs_dcache_sysctls);
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400203#endif
204
Linus Torvalds5483f182012-03-04 15:51:42 -0800205/*
206 * Compare 2 name strings, return 0 if they match, otherwise non-zero.
207 * The strings are both count bytes long, and count is non-zero.
208 */
Linus Torvaldse419b4c2012-05-03 10:16:43 -0700209#ifdef CONFIG_DCACHE_WORD_ACCESS
210
211#include <asm/word-at-a-time.h>
212/*
213 * NOTE! 'cs' and 'scount' come from a dentry, so it has a
214 * aligned allocation for this particular component. We don't
215 * strictly need the load_unaligned_zeropad() safety, but it
216 * doesn't hurt either.
217 *
218 * In contrast, 'ct' and 'tcount' can be from a pathname, and do
219 * need the careful unaligned handling.
220 */
Linus Torvalds94753db52012-05-10 12:19:19 -0700221static inline int dentry_string_cmp(const unsigned char *cs, const unsigned char *ct, unsigned tcount)
Linus Torvalds5483f182012-03-04 15:51:42 -0800222{
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -0800223 unsigned long a,b,mask;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -0800224
225 for (;;) {
Andrey Ryabininbfe7aa62018-02-01 21:00:51 +0300226 a = read_word_at_a_time(cs);
Linus Torvaldse419b4c2012-05-03 10:16:43 -0700227 b = load_unaligned_zeropad(ct);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -0800228 if (tcount < sizeof(unsigned long))
229 break;
230 if (unlikely(a != b))
231 return 1;
232 cs += sizeof(unsigned long);
233 ct += sizeof(unsigned long);
234 tcount -= sizeof(unsigned long);
235 if (!tcount)
236 return 0;
237 }
Will Deacona5c21dc2013-12-12 17:40:21 +0000238 mask = bytemask_from_count(tcount);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -0800239 return unlikely(!!((a ^ b) & mask));
Linus Torvaldse419b4c2012-05-03 10:16:43 -0700240}
241
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -0800242#else
Linus Torvaldse419b4c2012-05-03 10:16:43 -0700243
Linus Torvalds94753db52012-05-10 12:19:19 -0700244static inline int dentry_string_cmp(const unsigned char *cs, const unsigned char *ct, unsigned tcount)
Linus Torvaldse419b4c2012-05-03 10:16:43 -0700245{
Linus Torvalds5483f182012-03-04 15:51:42 -0800246 do {
247 if (*cs != *ct)
248 return 1;
249 cs++;
250 ct++;
251 tcount--;
252 } while (tcount);
253 return 0;
254}
255
Linus Torvaldse419b4c2012-05-03 10:16:43 -0700256#endif
257
Linus Torvalds94753db52012-05-10 12:19:19 -0700258static inline int dentry_cmp(const struct dentry *dentry, const unsigned char *ct, unsigned tcount)
259{
Linus Torvalds94753db52012-05-10 12:19:19 -0700260 /*
261 * Be careful about RCU walk racing with rename:
Will Deacon506458e2017-10-24 11:22:48 +0100262 * use 'READ_ONCE' to fetch the name pointer.
Linus Torvalds94753db52012-05-10 12:19:19 -0700263 *
264 * NOTE! Even if a rename will mean that the length
265 * was not loaded atomically, we don't care. The
266 * RCU walk will check the sequence count eventually,
267 * and catch it. And we won't overrun the buffer,
268 * because we're reading the name pointer atomically,
269 * and a dentry name is guaranteed to be properly
270 * terminated with a NUL byte.
271 *
272 * End result: even if 'len' is wrong, we'll exit
273 * early because the data cannot match (there can
274 * be no NUL in the ct/tcount data)
275 */
Will Deacon506458e2017-10-24 11:22:48 +0100276 const unsigned char *cs = READ_ONCE(dentry->d_name.name);
He Kuangae0a8432016-03-26 09:12:10 +0000277
Linus Torvalds6326c71f2012-05-21 16:14:04 -0700278 return dentry_string_cmp(cs, ct, tcount);
Linus Torvalds94753db52012-05-10 12:19:19 -0700279}
280
Al Viro8d85b482014-09-29 14:54:27 -0400281struct external_name {
282 union {
283 atomic_t count;
284 struct rcu_head head;
285 } u;
286 unsigned char name[];
287};
288
289static inline struct external_name *external_name(struct dentry *dentry)
290{
291 return container_of(dentry->d_name.name, struct external_name, name[0]);
292}
293
Christoph Hellwig9c82ab92010-10-10 05:36:22 -0400294static void __d_free(struct rcu_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
Christoph Hellwig9c82ab92010-10-10 05:36:22 -0400296 struct dentry *dentry = container_of(head, struct dentry, d_u.d_rcu);
297
Al Viro8d85b482014-09-29 14:54:27 -0400298 kmem_cache_free(dentry_cache, dentry);
299}
300
301static void __d_free_external(struct rcu_head *head)
302{
303 struct dentry *dentry = container_of(head, struct dentry, d_u.d_rcu);
Vlastimil Babka2e03b4b2018-10-26 15:05:41 -0700304 kfree(external_name(dentry));
Roman Gushchinf1782c92018-04-10 16:27:44 -0700305 kmem_cache_free(dentry_cache, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
Al Viro810bb172014-10-12 12:45:37 -0400308static inline int dname_external(const struct dentry *dentry)
309{
310 return dentry->d_name.name != dentry->d_iname;
311}
312
Al Viro49d31c22017-07-07 14:51:19 -0400313void take_dentry_name_snapshot(struct name_snapshot *name, struct dentry *dentry)
314{
315 spin_lock(&dentry->d_lock);
Al Viro230c6402019-04-26 13:07:27 -0400316 name->name = dentry->d_name;
Al Viro49d31c22017-07-07 14:51:19 -0400317 if (unlikely(dname_external(dentry))) {
Al Viro230c6402019-04-26 13:07:27 -0400318 atomic_inc(&external_name(dentry)->u.count);
Al Viro49d31c22017-07-07 14:51:19 -0400319 } else {
Tetsuo Handa6cd00a02018-08-17 15:44:34 -0700320 memcpy(name->inline_name, dentry->d_iname,
321 dentry->d_name.len + 1);
Al Viro230c6402019-04-26 13:07:27 -0400322 name->name.name = name->inline_name;
Al Viro49d31c22017-07-07 14:51:19 -0400323 }
Al Viro230c6402019-04-26 13:07:27 -0400324 spin_unlock(&dentry->d_lock);
Al Viro49d31c22017-07-07 14:51:19 -0400325}
326EXPORT_SYMBOL(take_dentry_name_snapshot);
327
328void release_dentry_name_snapshot(struct name_snapshot *name)
329{
Al Viro230c6402019-04-26 13:07:27 -0400330 if (unlikely(name->name.name != name->inline_name)) {
Al Viro49d31c22017-07-07 14:51:19 -0400331 struct external_name *p;
Al Viro230c6402019-04-26 13:07:27 -0400332 p = container_of(name->name.name, struct external_name, name[0]);
Al Viro49d31c22017-07-07 14:51:19 -0400333 if (unlikely(atomic_dec_and_test(&p->u.count)))
Vlastimil Babka2e03b4b2018-10-26 15:05:41 -0700334 kfree_rcu(p, u.head);
Al Viro49d31c22017-07-07 14:51:19 -0400335 }
336}
337EXPORT_SYMBOL(release_dentry_name_snapshot);
338
David Howells4bf46a22015-03-05 14:09:22 +0000339static inline void __d_set_inode_and_type(struct dentry *dentry,
340 struct inode *inode,
341 unsigned type_flags)
342{
343 unsigned flags;
344
345 dentry->d_inode = inode;
David Howells4bf46a22015-03-05 14:09:22 +0000346 flags = READ_ONCE(dentry->d_flags);
347 flags &= ~(DCACHE_ENTRY_TYPE | DCACHE_FALLTHRU);
348 flags |= type_flags;
Al Viro2fa6b1e2019-11-12 16:13:06 -0500349 smp_store_release(&dentry->d_flags, flags);
David Howells4bf46a22015-03-05 14:09:22 +0000350}
351
David Howells4bf46a22015-03-05 14:09:22 +0000352static inline void __d_clear_type_and_inode(struct dentry *dentry)
353{
354 unsigned flags = READ_ONCE(dentry->d_flags);
355
356 flags &= ~(DCACHE_ENTRY_TYPE | DCACHE_FALLTHRU);
357 WRITE_ONCE(dentry->d_flags, flags);
David Howells4bf46a22015-03-05 14:09:22 +0000358 dentry->d_inode = NULL;
Brian Fosterf6f6fdc2024-07-03 08:13:01 -0400359 /*
360 * The negative counter only tracks dentries on the LRU. Don't inc if
361 * d_lru is on another list.
362 */
363 if ((flags & (DCACHE_LRU_LIST|DCACHE_SHRINK_LIST)) == DCACHE_LRU_LIST)
Waiman Longaf0c9af2019-01-30 13:52:38 -0500364 this_cpu_inc(nr_dentry_negative);
David Howells4bf46a22015-03-05 14:09:22 +0000365}
366
Al Virob4f03542014-04-29 23:40:14 -0400367static void dentry_free(struct dentry *dentry)
368{
Al Viro946e51f2014-10-26 19:19:16 -0400369 WARN_ON(!hlist_unhashed(&dentry->d_u.d_alias));
Al Viro8d85b482014-09-29 14:54:27 -0400370 if (unlikely(dname_external(dentry))) {
371 struct external_name *p = external_name(dentry);
372 if (likely(atomic_dec_and_test(&p->u.count))) {
373 call_rcu(&dentry->d_u.d_rcu, __d_free_external);
374 return;
375 }
376 }
Al Virob4f03542014-04-29 23:40:14 -0400377 /* if dentry was never visible to RCU, immediate free is OK */
Al Viro5467a682019-03-15 22:23:19 -0400378 if (dentry->d_flags & DCACHE_NORCU)
Al Virob4f03542014-04-29 23:40:14 -0400379 __d_free(&dentry->d_u.d_rcu);
380 else
381 call_rcu(&dentry->d_u.d_rcu, __d_free);
382}
383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384/*
385 * Release the dentry's inode, using the filesystem
Al Viro550dce02016-05-29 20:13:30 -0400386 * d_iput() operation if defined.
Nick Piggin31e6b012011-01-07 17:49:52 +1100387 */
388static void dentry_unlink_inode(struct dentry * dentry)
389 __releases(dentry->d_lock)
Nick Piggin873feea2011-01-07 17:50:06 +1100390 __releases(dentry->d_inode->i_lock)
Nick Piggin31e6b012011-01-07 17:49:52 +1100391{
392 struct inode *inode = dentry->d_inode;
Al Viroa528aca2016-02-29 12:12:46 -0500393
Al Viro4c0d7cd2018-08-09 10:15:54 -0400394 raw_write_seqcount_begin(&dentry->d_seq);
David Howells4bf46a22015-03-05 14:09:22 +0000395 __d_clear_type_and_inode(dentry);
Al Viro946e51f2014-10-26 19:19:16 -0400396 hlist_del_init(&dentry->d_u.d_alias);
Al Viro4c0d7cd2018-08-09 10:15:54 -0400397 raw_write_seqcount_end(&dentry->d_seq);
Nick Piggin31e6b012011-01-07 17:49:52 +1100398 spin_unlock(&dentry->d_lock);
Nick Piggin873feea2011-01-07 17:50:06 +1100399 spin_unlock(&inode->i_lock);
Nick Piggin31e6b012011-01-07 17:49:52 +1100400 if (!inode->i_nlink)
401 fsnotify_inoderemove(inode);
402 if (dentry->d_op && dentry->d_op->d_iput)
403 dentry->d_op->d_iput(dentry, inode);
404 else
405 iput(inode);
406}
407
408/*
Linus Torvalds89dc77b2013-09-13 22:55:10 -0400409 * The DCACHE_LRU_LIST bit is set whenever the 'd_lru' entry
410 * is in use - which includes both the "real" per-superblock
411 * LRU list _and_ the DCACHE_SHRINK_LIST use.
412 *
413 * The DCACHE_SHRINK_LIST bit is set whenever the dentry is
414 * on the shrink list (ie not on the superblock LRU list).
415 *
416 * The per-cpu "nr_dentry_unused" counters are updated with
417 * the DCACHE_LRU_LIST bit.
418 *
Waiman Longaf0c9af2019-01-30 13:52:38 -0500419 * The per-cpu "nr_dentry_negative" counters are only updated
420 * when deleted from or added to the per-superblock LRU list, not
421 * from/to the shrink list. That is to avoid an unneeded dec/inc
422 * pair when moving from LRU to shrink list in select_collect().
423 *
Linus Torvalds89dc77b2013-09-13 22:55:10 -0400424 * These helper functions make sure we always follow the
425 * rules. d_lock must be held by the caller.
426 */
427#define D_FLAG_VERIFY(dentry,x) WARN_ON_ONCE(((dentry)->d_flags & (DCACHE_LRU_LIST | DCACHE_SHRINK_LIST)) != (x))
428static void d_lru_add(struct dentry *dentry)
429{
430 D_FLAG_VERIFY(dentry, 0);
431 dentry->d_flags |= DCACHE_LRU_LIST;
432 this_cpu_inc(nr_dentry_unused);
Waiman Longaf0c9af2019-01-30 13:52:38 -0500433 if (d_is_negative(dentry))
434 this_cpu_inc(nr_dentry_negative);
Linus Torvalds89dc77b2013-09-13 22:55:10 -0400435 WARN_ON_ONCE(!list_lru_add(&dentry->d_sb->s_dentry_lru, &dentry->d_lru));
436}
437
438static void d_lru_del(struct dentry *dentry)
439{
440 D_FLAG_VERIFY(dentry, DCACHE_LRU_LIST);
441 dentry->d_flags &= ~DCACHE_LRU_LIST;
442 this_cpu_dec(nr_dentry_unused);
Waiman Longaf0c9af2019-01-30 13:52:38 -0500443 if (d_is_negative(dentry))
444 this_cpu_dec(nr_dentry_negative);
Linus Torvalds89dc77b2013-09-13 22:55:10 -0400445 WARN_ON_ONCE(!list_lru_del(&dentry->d_sb->s_dentry_lru, &dentry->d_lru));
446}
447
448static void d_shrink_del(struct dentry *dentry)
449{
450 D_FLAG_VERIFY(dentry, DCACHE_SHRINK_LIST | DCACHE_LRU_LIST);
451 list_del_init(&dentry->d_lru);
452 dentry->d_flags &= ~(DCACHE_SHRINK_LIST | DCACHE_LRU_LIST);
453 this_cpu_dec(nr_dentry_unused);
454}
455
456static void d_shrink_add(struct dentry *dentry, struct list_head *list)
457{
458 D_FLAG_VERIFY(dentry, 0);
459 list_add(&dentry->d_lru, list);
460 dentry->d_flags |= DCACHE_SHRINK_LIST | DCACHE_LRU_LIST;
461 this_cpu_inc(nr_dentry_unused);
462}
463
464/*
465 * These can only be called under the global LRU lock, ie during the
466 * callback for freeing the LRU list. "isolate" removes it from the
467 * LRU lists entirely, while shrink_move moves it to the indicated
468 * private list.
469 */
Vladimir Davydov3f97b162015-02-12 14:59:35 -0800470static void d_lru_isolate(struct list_lru_one *lru, struct dentry *dentry)
Linus Torvalds89dc77b2013-09-13 22:55:10 -0400471{
472 D_FLAG_VERIFY(dentry, DCACHE_LRU_LIST);
473 dentry->d_flags &= ~DCACHE_LRU_LIST;
474 this_cpu_dec(nr_dentry_unused);
Waiman Longaf0c9af2019-01-30 13:52:38 -0500475 if (d_is_negative(dentry))
476 this_cpu_dec(nr_dentry_negative);
Vladimir Davydov3f97b162015-02-12 14:59:35 -0800477 list_lru_isolate(lru, &dentry->d_lru);
Linus Torvalds89dc77b2013-09-13 22:55:10 -0400478}
479
Vladimir Davydov3f97b162015-02-12 14:59:35 -0800480static void d_lru_shrink_move(struct list_lru_one *lru, struct dentry *dentry,
481 struct list_head *list)
Linus Torvalds89dc77b2013-09-13 22:55:10 -0400482{
483 D_FLAG_VERIFY(dentry, DCACHE_LRU_LIST);
484 dentry->d_flags |= DCACHE_SHRINK_LIST;
Waiman Longaf0c9af2019-01-30 13:52:38 -0500485 if (d_is_negative(dentry))
486 this_cpu_dec(nr_dentry_negative);
Vladimir Davydov3f97b162015-02-12 14:59:35 -0800487 list_lru_isolate_move(lru, &dentry->d_lru, list);
Linus Torvalds89dc77b2013-09-13 22:55:10 -0400488}
489
NeilBrown61647822017-11-10 15:45:41 +1100490static void ___d_drop(struct dentry *dentry)
Nick Piggin789680d2011-01-07 17:49:30 +1100491{
Al Viro0632a9a2018-03-07 00:49:10 -0500492 struct hlist_bl_head *b;
493 /*
494 * Hashed dentries are normally on the dentry hashtable,
495 * with the exception of those newly allocated by
496 * d_obtain_root, which are always IS_ROOT:
497 */
498 if (unlikely(IS_ROOT(dentry)))
499 b = &dentry->d_sb->s_roots;
500 else
501 b = d_hash(dentry->d_name.hash);
Al Virob61625d2013-10-04 11:09:01 -0400502
Al Viro0632a9a2018-03-07 00:49:10 -0500503 hlist_bl_lock(b);
504 __hlist_bl_del(&dentry->d_hash);
505 hlist_bl_unlock(b);
Nick Piggin789680d2011-01-07 17:49:30 +1100506}
NeilBrown61647822017-11-10 15:45:41 +1100507
508void __d_drop(struct dentry *dentry)
509{
Al Viro0632a9a2018-03-07 00:49:10 -0500510 if (!d_unhashed(dentry)) {
511 ___d_drop(dentry);
512 dentry->d_hash.pprev = NULL;
513 write_seqcount_invalidate(&dentry->d_seq);
514 }
NeilBrown61647822017-11-10 15:45:41 +1100515}
Nick Piggin789680d2011-01-07 17:49:30 +1100516EXPORT_SYMBOL(__d_drop);
517
Mauro Carvalho Chehab961f3c82021-01-14 09:04:39 +0100518/**
519 * d_drop - drop a dentry
520 * @dentry: dentry to drop
521 *
522 * d_drop() unhashes the entry from the parent dentry hashes, so that it won't
523 * be found through a VFS lookup any more. Note that this is different from
524 * deleting the dentry - d_delete will try to mark the dentry negative if
525 * possible, giving a successful _negative_ lookup, while d_drop will
526 * just make the cache lookup fail.
527 *
528 * d_drop() is used mainly for stuff that wants to invalidate a dentry for some
529 * reason (NFS timeouts or autofs deletes).
530 *
531 * __d_drop requires dentry->d_lock
532 *
533 * ___d_drop doesn't mark dentry as "unhashed"
534 * (dentry->d_hash.pprev will be LIST_POISON2, not NULL).
535 */
Nick Piggin789680d2011-01-07 17:49:30 +1100536void d_drop(struct dentry *dentry)
537{
Nick Piggin789680d2011-01-07 17:49:30 +1100538 spin_lock(&dentry->d_lock);
539 __d_drop(dentry);
540 spin_unlock(&dentry->d_lock);
Nick Piggin789680d2011-01-07 17:49:30 +1100541}
542EXPORT_SYMBOL(d_drop);
543
Al Viroba65dc52016-06-10 11:32:47 -0400544static inline void dentry_unlist(struct dentry *dentry, struct dentry *parent)
545{
546 struct dentry *next;
547 /*
548 * Inform d_walk() and shrink_dentry_list() that we are no longer
549 * attached to the dentry tree
550 */
551 dentry->d_flags |= DCACHE_DENTRY_KILLED;
552 if (unlikely(list_empty(&dentry->d_child)))
553 return;
554 __list_del_entry(&dentry->d_child);
555 /*
556 * Cursors can move around the list of children. While we'd been
557 * a normal list member, it didn't matter - ->d_child.next would've
558 * been updated. However, from now on it won't be and for the
559 * things like d_walk() it might end up with a nasty surprise.
560 * Normally d_walk() doesn't care about cursors moving around -
561 * ->d_lock on parent prevents that and since a cursor has no children
562 * of its own, we get through it without ever unlocking the parent.
563 * There is one exception, though - if we ascend from a child that
564 * gets killed as soon as we unlock it, the next sibling is found
565 * using the value left in its ->d_child.next. And if _that_
566 * pointed to a cursor, and cursor got moved (e.g. by lseek())
567 * before d_walk() regains parent->d_lock, we'll end up skipping
568 * everything the cursor had been moved past.
569 *
570 * Solution: make sure that the pointer left behind in ->d_child.next
571 * points to something that won't be moving around. I.e. skip the
572 * cursors.
573 */
574 while (dentry->d_child.next != &parent->d_subdirs) {
575 next = list_entry(dentry->d_child.next, struct dentry, d_child);
576 if (likely(!(next->d_flags & DCACHE_DENTRY_CURSOR)))
577 break;
578 dentry->d_child.next = next->d_child.next;
579 }
580}
581
Al Viroe55fd012014-05-28 13:51:12 -0400582static void __dentry_kill(struct dentry *dentry)
Nick Piggin77812a12011-01-07 17:49:48 +1100583{
Al Viro41edf2782014-05-01 10:30:00 -0400584 struct dentry *parent = NULL;
585 bool can_free = true;
Al Viro41edf2782014-05-01 10:30:00 -0400586 if (!IS_ROOT(dentry))
Nick Piggin77812a12011-01-07 17:49:48 +1100587 parent = dentry->d_parent;
Nick Piggin31e6b012011-01-07 17:49:52 +1100588
Linus Torvalds0d984392013-09-08 13:46:52 -0700589 /*
590 * The dentry is now unrecoverably dead to the world.
591 */
592 lockref_mark_dead(&dentry->d_lockref);
593
Sage Weilf0023bc2011-10-28 10:02:42 -0700594 /*
Sage Weilf0023bc2011-10-28 10:02:42 -0700595 * inform the fs via d_prune that this dentry is about to be
596 * unhashed and destroyed.
597 */
Al Viro29266202014-05-30 11:39:02 -0400598 if (dentry->d_flags & DCACHE_OP_PRUNE)
Yan, Zheng61572bb2013-04-15 14:13:21 +0800599 dentry->d_op->d_prune(dentry);
600
Al Viro01b60352014-04-29 23:42:52 -0400601 if (dentry->d_flags & DCACHE_LRU_LIST) {
602 if (!(dentry->d_flags & DCACHE_SHRINK_LIST))
603 d_lru_del(dentry);
Al Viro01b60352014-04-29 23:42:52 -0400604 }
Nick Piggin77812a12011-01-07 17:49:48 +1100605 /* if it was on the hash then remove it */
606 __d_drop(dentry);
Al Viroba65dc52016-06-10 11:32:47 -0400607 dentry_unlist(dentry, parent);
Al Viro03b3b882014-04-29 15:45:28 -0400608 if (parent)
609 spin_unlock(&parent->d_lock);
Al Viro550dce02016-05-29 20:13:30 -0400610 if (dentry->d_inode)
611 dentry_unlink_inode(dentry);
612 else
613 spin_unlock(&dentry->d_lock);
Al Viro03b3b882014-04-29 15:45:28 -0400614 this_cpu_dec(nr_dentry);
615 if (dentry->d_op && dentry->d_op->d_release)
616 dentry->d_op->d_release(dentry);
617
Al Viro41edf2782014-05-01 10:30:00 -0400618 spin_lock(&dentry->d_lock);
619 if (dentry->d_flags & DCACHE_SHRINK_LIST) {
620 dentry->d_flags |= DCACHE_MAY_FREE;
621 can_free = false;
622 }
623 spin_unlock(&dentry->d_lock);
Al Viro41edf2782014-05-01 10:30:00 -0400624 if (likely(can_free))
625 dentry_free(dentry);
Al Viro9c5f1d32018-04-15 18:28:48 -0400626 cond_resched();
Al Viroe55fd012014-05-28 13:51:12 -0400627}
628
Al Viro8b987a42018-02-23 22:11:34 -0500629static struct dentry *__lock_parent(struct dentry *dentry)
Al Viro046b9612014-05-29 08:54:52 -0400630{
Al Viro8b987a42018-02-23 22:11:34 -0500631 struct dentry *parent;
Al Viro046b9612014-05-29 08:54:52 -0400632 rcu_read_lock();
Al Viroc2338f22014-06-12 00:29:13 -0400633 spin_unlock(&dentry->d_lock);
Al Viro046b9612014-05-29 08:54:52 -0400634again:
Mark Rutland66702eb2017-10-23 14:07:14 -0700635 parent = READ_ONCE(dentry->d_parent);
Al Viro046b9612014-05-29 08:54:52 -0400636 spin_lock(&parent->d_lock);
637 /*
638 * We can't blindly lock dentry until we are sure
639 * that we won't violate the locking order.
640 * Any changes of dentry->d_parent must have
641 * been done with parent->d_lock held, so
642 * spin_lock() above is enough of a barrier
643 * for checking if it's still our child.
644 */
645 if (unlikely(parent != dentry->d_parent)) {
646 spin_unlock(&parent->d_lock);
647 goto again;
648 }
Al Viro3b821402018-02-23 20:47:17 -0500649 rcu_read_unlock();
Al Viro65d8eb52018-02-23 22:07:35 -0500650 if (parent != dentry)
651 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
652 else
653 parent = NULL;
Al Viro046b9612014-05-29 08:54:52 -0400654 return parent;
655}
656
Al Viro8b987a42018-02-23 22:11:34 -0500657static inline struct dentry *lock_parent(struct dentry *dentry)
658{
659 struct dentry *parent = dentry->d_parent;
660 if (IS_ROOT(dentry))
661 return NULL;
662 if (likely(spin_trylock(&parent->d_lock)))
663 return parent;
664 return __lock_parent(dentry);
665}
666
Al Viroa3385792018-02-23 21:07:24 -0500667static inline bool retain_dentry(struct dentry *dentry)
668{
669 WARN_ON(d_in_lookup(dentry));
670
671 /* Unreachable? Get rid of it */
672 if (unlikely(d_unhashed(dentry)))
673 return false;
674
675 if (unlikely(dentry->d_flags & DCACHE_DISCONNECTED))
676 return false;
677
678 if (unlikely(dentry->d_flags & DCACHE_OP_DELETE)) {
679 if (dentry->d_op->d_delete(dentry))
680 return false;
681 }
Ira Weiny2c567af2020-04-30 07:41:37 -0700682
683 if (unlikely(dentry->d_flags & DCACHE_DONTCACHE))
684 return false;
685
Al Viro62d99562018-03-06 21:37:31 -0500686 /* retain; LRU fodder */
687 dentry->d_lockref.count--;
688 if (unlikely(!(dentry->d_flags & DCACHE_LRU_LIST)))
689 d_lru_add(dentry);
690 else if (unlikely(!(dentry->d_flags & DCACHE_REFERENCED)))
691 dentry->d_flags |= DCACHE_REFERENCED;
Al Viroa3385792018-02-23 21:07:24 -0500692 return true;
693}
694
Ira Weiny2c567af2020-04-30 07:41:37 -0700695void d_mark_dontcache(struct inode *inode)
696{
697 struct dentry *de;
698
699 spin_lock(&inode->i_lock);
700 hlist_for_each_entry(de, &inode->i_dentry, d_u.d_alias) {
701 spin_lock(&de->d_lock);
702 de->d_flags |= DCACHE_DONTCACHE;
703 spin_unlock(&de->d_lock);
704 }
705 inode->i_state |= I_DONTCACHE;
706 spin_unlock(&inode->i_lock);
707}
708EXPORT_SYMBOL(d_mark_dontcache);
709
Linus Torvalds360f5472015-01-09 15:19:03 -0800710/*
John Ognessc1d0c1a2018-02-23 00:50:21 +0100711 * Finish off a dentry we've decided to kill.
712 * dentry->d_lock must be held, returns with it unlocked.
713 * Returns dentry requiring refcount drop, or NULL if we're done.
714 */
715static struct dentry *dentry_kill(struct dentry *dentry)
716 __releases(dentry->d_lock)
717{
718 struct inode *inode = dentry->d_inode;
719 struct dentry *parent = NULL;
720
721 if (inode && unlikely(!spin_trylock(&inode->i_lock)))
Al Virof657a662018-02-23 21:25:42 -0500722 goto slow_positive;
John Ognessc1d0c1a2018-02-23 00:50:21 +0100723
724 if (!IS_ROOT(dentry)) {
725 parent = dentry->d_parent;
726 if (unlikely(!spin_trylock(&parent->d_lock))) {
Al Virof657a662018-02-23 21:25:42 -0500727 parent = __lock_parent(dentry);
728 if (likely(inode || !dentry->d_inode))
729 goto got_locks;
730 /* negative that became positive */
731 if (parent)
732 spin_unlock(&parent->d_lock);
733 inode = dentry->d_inode;
734 goto slow_positive;
John Ognessc1d0c1a2018-02-23 00:50:21 +0100735 }
736 }
John Ognessc1d0c1a2018-02-23 00:50:21 +0100737 __dentry_kill(dentry);
738 return parent;
739
Al Virof657a662018-02-23 21:25:42 -0500740slow_positive:
John Ognessc1d0c1a2018-02-23 00:50:21 +0100741 spin_unlock(&dentry->d_lock);
Al Virof657a662018-02-23 21:25:42 -0500742 spin_lock(&inode->i_lock);
743 spin_lock(&dentry->d_lock);
744 parent = lock_parent(dentry);
745got_locks:
746 if (unlikely(dentry->d_lockref.count != 1)) {
747 dentry->d_lockref.count--;
748 } else if (likely(!retain_dentry(dentry))) {
749 __dentry_kill(dentry);
750 return parent;
751 }
752 /* we are keeping it, after all */
753 if (inode)
754 spin_unlock(&inode->i_lock);
755 if (parent)
756 spin_unlock(&parent->d_lock);
757 spin_unlock(&dentry->d_lock);
758 return NULL;
John Ognessc1d0c1a2018-02-23 00:50:21 +0100759}
760
761/*
Linus Torvalds360f5472015-01-09 15:19:03 -0800762 * Try to do a lockless dput(), and return whether that was successful.
763 *
764 * If unsuccessful, we return false, having already taken the dentry lock.
765 *
766 * The caller needs to hold the RCU read lock, so that the dentry is
767 * guaranteed to stay around even if the refcount goes down to zero!
768 */
769static inline bool fast_dput(struct dentry *dentry)
770{
771 int ret;
772 unsigned int d_flags;
773
774 /*
775 * If we have a d_op->d_delete() operation, we sould not
Al Viro75a6f822015-07-08 02:42:38 +0100776 * let the dentry count go to zero, so use "put_or_lock".
Linus Torvalds360f5472015-01-09 15:19:03 -0800777 */
778 if (unlikely(dentry->d_flags & DCACHE_OP_DELETE))
779 return lockref_put_or_lock(&dentry->d_lockref);
780
781 /*
782 * .. otherwise, we can try to just decrement the
783 * lockref optimistically.
784 */
785 ret = lockref_put_return(&dentry->d_lockref);
786
787 /*
788 * If the lockref_put_return() failed due to the lock being held
789 * by somebody else, the fast path has failed. We will need to
790 * get the lock, and then check the count again.
791 */
792 if (unlikely(ret < 0)) {
793 spin_lock(&dentry->d_lock);
Al Viroc4cb4282023-11-01 01:08:54 -0400794 if (WARN_ON_ONCE(dentry->d_lockref.count <= 0)) {
Linus Torvalds360f5472015-01-09 15:19:03 -0800795 spin_unlock(&dentry->d_lock);
Gustavo A. R. Silva79644102018-08-01 19:39:05 -0500796 return true;
Linus Torvalds360f5472015-01-09 15:19:03 -0800797 }
Al Viroc4cb4282023-11-01 01:08:54 -0400798 dentry->d_lockref.count--;
799 goto locked;
Linus Torvalds360f5472015-01-09 15:19:03 -0800800 }
801
802 /*
803 * If we weren't the last ref, we're done.
804 */
805 if (ret)
Gustavo A. R. Silva79644102018-08-01 19:39:05 -0500806 return true;
Linus Torvalds360f5472015-01-09 15:19:03 -0800807
808 /*
809 * Careful, careful. The reference count went down
810 * to zero, but we don't hold the dentry lock, so
811 * somebody else could get it again, and do another
812 * dput(), and we need to not race with that.
813 *
814 * However, there is a very special and common case
815 * where we don't care, because there is nothing to
816 * do: the dentry is still hashed, it does not have
817 * a 'delete' op, and it's referenced and already on
818 * the LRU list.
819 *
820 * NOTE! Since we aren't locked, these values are
821 * not "stable". However, it is sufficient that at
822 * some point after we dropped the reference the
823 * dentry was hashed and the flags had the proper
824 * value. Other dentry users may have re-gotten
825 * a reference to the dentry and change that, but
826 * our work is done - we can leave the dentry
827 * around with a zero refcount.
Hao Li77573fa2020-12-08 10:10:50 +0800828 *
829 * Nevertheless, there are two cases that we should kill
830 * the dentry anyway.
831 * 1. free disconnected dentries as soon as their refcount
832 * reached zero.
833 * 2. free dentries if they should not be cached.
Linus Torvalds360f5472015-01-09 15:19:03 -0800834 */
835 smp_rmb();
Mark Rutland66702eb2017-10-23 14:07:14 -0700836 d_flags = READ_ONCE(dentry->d_flags);
Hao Li77573fa2020-12-08 10:10:50 +0800837 d_flags &= DCACHE_REFERENCED | DCACHE_LRU_LIST |
838 DCACHE_DISCONNECTED | DCACHE_DONTCACHE;
Linus Torvalds360f5472015-01-09 15:19:03 -0800839
840 /* Nothing to do? Dropping the reference was all we needed? */
841 if (d_flags == (DCACHE_REFERENCED | DCACHE_LRU_LIST) && !d_unhashed(dentry))
Gustavo A. R. Silva79644102018-08-01 19:39:05 -0500842 return true;
Linus Torvalds360f5472015-01-09 15:19:03 -0800843
844 /*
845 * Not the fast normal case? Get the lock. We've already decremented
846 * the refcount, but we'll need to re-check the situation after
847 * getting the lock.
848 */
849 spin_lock(&dentry->d_lock);
850
851 /*
852 * Did somebody else grab a reference to it in the meantime, and
853 * we're no longer the last user after all? Alternatively, somebody
854 * else could have killed it and marked it dead. Either way, we
855 * don't need to do anything else.
856 */
Al Viroc4cb4282023-11-01 01:08:54 -0400857locked:
Linus Torvalds360f5472015-01-09 15:19:03 -0800858 if (dentry->d_lockref.count) {
859 spin_unlock(&dentry->d_lock);
Gustavo A. R. Silva79644102018-08-01 19:39:05 -0500860 return true;
Linus Torvalds360f5472015-01-09 15:19:03 -0800861 }
862
863 /*
864 * Re-get the reference we optimistically dropped. We hold the
865 * lock, and we just tested that it was zero, so we can just
866 * set it to 1.
867 */
868 dentry->d_lockref.count = 1;
Gustavo A. R. Silva79644102018-08-01 19:39:05 -0500869 return false;
Linus Torvalds360f5472015-01-09 15:19:03 -0800870}
871
872
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873/*
874 * This is dput
875 *
876 * This is complicated by the fact that we do not want to put
877 * dentries that are no longer on any hash chain on the unused
878 * list: we'd much rather just get rid of them immediately.
879 *
880 * However, that implies that we have to traverse the dentry
881 * tree upwards to the parents which might _also_ now be
882 * scheduled for deletion (it may have been only waiting for
883 * its last child to go away).
884 *
885 * This tail recursion is done by hand as we don't want to depend
886 * on the compiler to always get this right (gcc generally doesn't).
887 * Real recursion would eat up our stack space.
888 */
889
890/*
891 * dput - release a dentry
892 * @dentry: dentry to release
893 *
894 * Release a dentry. This will drop the usage count and if appropriate
895 * call the dentry unlink method as well as removing it from the queues and
896 * releasing its resources. If the parent dentries were scheduled for release
897 * they too may now get deleted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899void dput(struct dentry *dentry)
900{
Al Viro1088a642018-04-15 18:31:03 -0400901 while (dentry) {
902 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Al Viro1088a642018-04-15 18:31:03 -0400904 rcu_read_lock();
905 if (likely(fast_dput(dentry))) {
906 rcu_read_unlock();
907 return;
908 }
Wei Fang47be6182016-07-06 11:32:20 +0800909
Al Viro1088a642018-04-15 18:31:03 -0400910 /* Slow case: now with the dentry lock held */
Linus Torvalds360f5472015-01-09 15:19:03 -0800911 rcu_read_unlock();
Linus Torvalds360f5472015-01-09 15:19:03 -0800912
Al Viro1088a642018-04-15 18:31:03 -0400913 if (likely(retain_dentry(dentry))) {
914 spin_unlock(&dentry->d_lock);
915 return;
916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
Al Viro1088a642018-04-15 18:31:03 -0400918 dentry = dentry_kill(dentry);
Wei Fang47be6182016-07-06 11:32:20 +0800919 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700921EXPORT_SYMBOL(dput);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Al Viro9bdebc22019-06-29 18:31:24 -0400923static void __dput_to_list(struct dentry *dentry, struct list_head *list)
924__must_hold(&dentry->d_lock)
925{
926 if (dentry->d_flags & DCACHE_SHRINK_LIST) {
927 /* let the owner of the list it's on deal with it */
928 --dentry->d_lockref.count;
929 } else {
930 if (dentry->d_flags & DCACHE_LRU_LIST)
931 d_lru_del(dentry);
932 if (!--dentry->d_lockref.count)
933 d_shrink_add(dentry, list);
934 }
935}
936
937void dput_to_list(struct dentry *dentry, struct list_head *list)
938{
939 rcu_read_lock();
940 if (likely(fast_dput(dentry))) {
941 rcu_read_unlock();
942 return;
943 }
944 rcu_read_unlock();
945 if (!retain_dentry(dentry))
946 __dput_to_list(dentry, list);
947 spin_unlock(&dentry->d_lock);
948}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100950/* This must be called with d_lock held */
Nick Piggindc0474b2011-01-07 17:49:43 +1100951static inline void __dget_dlock(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952{
Waiman Long98474232013-08-28 18:24:59 -0700953 dentry->d_lockref.count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954}
955
Nick Piggindc0474b2011-01-07 17:49:43 +1100956static inline void __dget(struct dentry *dentry)
Nick Piggin23044502011-01-07 17:49:31 +1100957{
Waiman Long98474232013-08-28 18:24:59 -0700958 lockref_get(&dentry->d_lockref);
Nick Piggin23044502011-01-07 17:49:31 +1100959}
960
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100961struct dentry *dget_parent(struct dentry *dentry)
962{
Waiman Longdf3d0bb2013-09-02 11:29:22 -0700963 int gotref;
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100964 struct dentry *ret;
Al Viroe8400932019-10-31 01:43:31 -0400965 unsigned seq;
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100966
Waiman Longdf3d0bb2013-09-02 11:29:22 -0700967 /*
968 * Do optimistic parent lookup without any
969 * locking.
970 */
971 rcu_read_lock();
Al Viroe8400932019-10-31 01:43:31 -0400972 seq = raw_seqcount_begin(&dentry->d_seq);
Mark Rutland66702eb2017-10-23 14:07:14 -0700973 ret = READ_ONCE(dentry->d_parent);
Waiman Longdf3d0bb2013-09-02 11:29:22 -0700974 gotref = lockref_get_not_zero(&ret->d_lockref);
975 rcu_read_unlock();
976 if (likely(gotref)) {
Al Viroe8400932019-10-31 01:43:31 -0400977 if (!read_seqcount_retry(&dentry->d_seq, seq))
Waiman Longdf3d0bb2013-09-02 11:29:22 -0700978 return ret;
979 dput(ret);
980 }
981
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100982repeat:
Nick Piggina734eb42011-01-07 17:49:44 +1100983 /*
984 * Don't need rcu_dereference because we re-check it was correct under
985 * the lock.
986 */
987 rcu_read_lock();
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100988 ret = dentry->d_parent;
Nick Piggina734eb42011-01-07 17:49:44 +1100989 spin_lock(&ret->d_lock);
990 if (unlikely(ret != dentry->d_parent)) {
991 spin_unlock(&ret->d_lock);
992 rcu_read_unlock();
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100993 goto repeat;
994 }
Nick Piggina734eb42011-01-07 17:49:44 +1100995 rcu_read_unlock();
Waiman Long98474232013-08-28 18:24:59 -0700996 BUG_ON(!ret->d_lockref.count);
997 ret->d_lockref.count++;
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100998 spin_unlock(&ret->d_lock);
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100999 return ret;
1000}
1001EXPORT_SYMBOL(dget_parent);
1002
Al Viro61fec492018-04-25 10:52:25 -04001003static struct dentry * __d_find_any_alias(struct inode *inode)
1004{
1005 struct dentry *alias;
1006
1007 if (hlist_empty(&inode->i_dentry))
1008 return NULL;
1009 alias = hlist_entry(inode->i_dentry.first, struct dentry, d_u.d_alias);
1010 __dget(alias);
1011 return alias;
1012}
1013
1014/**
1015 * d_find_any_alias - find any alias for a given inode
1016 * @inode: inode to find an alias for
1017 *
1018 * If any aliases exist for the given inode, take and return a
1019 * reference for one of them. If no aliases exist, return %NULL.
1020 */
1021struct dentry *d_find_any_alias(struct inode *inode)
1022{
1023 struct dentry *de;
1024
1025 spin_lock(&inode->i_lock);
1026 de = __d_find_any_alias(inode);
1027 spin_unlock(&inode->i_lock);
1028 return de;
1029}
1030EXPORT_SYMBOL(d_find_any_alias);
1031
J. Bruce Fields52ed46f2014-01-16 11:15:51 -05001032static struct dentry *__d_find_alias(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
Al Viro61fec492018-04-25 10:52:25 -04001034 struct dentry *alias;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
Al Viro61fec492018-04-25 10:52:25 -04001036 if (S_ISDIR(inode->i_mode))
1037 return __d_find_any_alias(inode);
1038
Al Viro946e51f2014-10-26 19:19:16 -04001039 hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
Nick Pigginda502952011-01-07 17:49:33 +11001040 spin_lock(&alias->d_lock);
Al Viro61fec492018-04-25 10:52:25 -04001041 if (!d_unhashed(alias)) {
J. Bruce Fields8d80d7d2014-01-16 17:17:31 -05001042 __dget_dlock(alias);
1043 spin_unlock(&alias->d_lock);
1044 return alias;
Nick Pigginda502952011-01-07 17:49:33 +11001045 }
1046 spin_unlock(&alias->d_lock);
Nick Pigginda502952011-01-07 17:49:33 +11001047 }
1048 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049}
1050
Mauro Carvalho Chehab961f3c82021-01-14 09:04:39 +01001051/**
1052 * d_find_alias - grab a hashed alias of inode
1053 * @inode: inode in question
1054 *
1055 * If inode has a hashed alias, or is a directory and has any alias,
1056 * acquire the reference to alias and return it. Otherwise return NULL.
1057 * Notice that if inode is a directory there can be only one alias and
1058 * it can be unhashed only if it has no children, or if it is the root
1059 * of a filesystem, or if the directory was renamed and d_revalidate
1060 * was the first vfs operation to notice.
1061 *
1062 * If the inode has an IS_ROOT, DCACHE_DISCONNECTED alias, then prefer
1063 * any other hashed alias over that one.
1064 */
Nick Pigginda502952011-01-07 17:49:33 +11001065struct dentry *d_find_alias(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066{
David Howells214fda12006-03-25 03:06:36 -08001067 struct dentry *de = NULL;
1068
Al Virob3d9b7a2012-06-09 13:51:19 -04001069 if (!hlist_empty(&inode->i_dentry)) {
Nick Piggin873feea2011-01-07 17:50:06 +11001070 spin_lock(&inode->i_lock);
J. Bruce Fields52ed46f2014-01-16 11:15:51 -05001071 de = __d_find_alias(inode);
Nick Piggin873feea2011-01-07 17:50:06 +11001072 spin_unlock(&inode->i_lock);
David Howells214fda12006-03-25 03:06:36 -08001073 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 return de;
1075}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001076EXPORT_SYMBOL(d_find_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
1078/*
Al Virobca585d2021-01-05 14:13:52 -05001079 * Caller MUST be holding rcu_read_lock() and be guaranteed
1080 * that inode won't get freed until rcu_read_unlock().
1081 */
1082struct dentry *d_find_alias_rcu(struct inode *inode)
1083{
1084 struct hlist_head *l = &inode->i_dentry;
1085 struct dentry *de = NULL;
1086
1087 spin_lock(&inode->i_lock);
1088 // ->i_dentry and ->i_rcu are colocated, but the latter won't be
1089 // used without having I_FREEING set, which means no aliases left
1090 if (likely(!(inode->i_state & I_FREEING) && !hlist_empty(l))) {
1091 if (S_ISDIR(inode->i_mode)) {
1092 de = hlist_entry(l->first, struct dentry, d_u.d_alias);
1093 } else {
1094 hlist_for_each_entry(de, l, d_u.d_alias)
1095 if (!d_unhashed(de))
1096 break;
1097 }
1098 }
1099 spin_unlock(&inode->i_lock);
1100 return de;
1101}
1102
1103/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 * Try to kill dentries associated with this inode.
1105 * WARNING: you must own a reference to inode.
1106 */
1107void d_prune_aliases(struct inode *inode)
1108{
Domen Puncer0cdca3f2005-09-10 00:27:07 -07001109 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110restart:
Nick Piggin873feea2011-01-07 17:50:06 +11001111 spin_lock(&inode->i_lock);
Al Viro946e51f2014-10-26 19:19:16 -04001112 hlist_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 spin_lock(&dentry->d_lock);
Waiman Long98474232013-08-28 18:24:59 -07001114 if (!dentry->d_lockref.count) {
Al Viro29355c32014-05-30 11:25:30 -04001115 struct dentry *parent = lock_parent(dentry);
1116 if (likely(!dentry->d_lockref.count)) {
1117 __dentry_kill(dentry);
Yan, Zheng4a7795d2014-11-19 15:50:34 +08001118 dput(parent);
Al Viro29355c32014-05-30 11:25:30 -04001119 goto restart;
1120 }
1121 if (parent)
1122 spin_unlock(&parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 }
1124 spin_unlock(&dentry->d_lock);
1125 }
Nick Piggin873feea2011-01-07 17:50:06 +11001126 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001128EXPORT_SYMBOL(d_prune_aliases);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
Al Viro3b3f09f2018-02-23 21:54:18 -05001130/*
1131 * Lock a dentry from shrink list.
John Ogness8f04da22018-02-23 00:50:24 +01001132 * Called under rcu_read_lock() and dentry->d_lock; the former
1133 * guarantees that nothing we access will be freed under us.
Al Viro3b3f09f2018-02-23 21:54:18 -05001134 * Note that dentry is *not* protected from concurrent dentry_kill(),
John Ogness8f04da22018-02-23 00:50:24 +01001135 * d_delete(), etc.
1136 *
Al Viro3b3f09f2018-02-23 21:54:18 -05001137 * Return false if dentry has been disrupted or grabbed, leaving
1138 * the caller to kick it off-list. Otherwise, return true and have
1139 * that dentry's inode and parent both locked.
1140 */
1141static bool shrink_lock_dentry(struct dentry *dentry)
1142{
1143 struct inode *inode;
1144 struct dentry *parent;
1145
1146 if (dentry->d_lockref.count)
1147 return false;
1148
1149 inode = dentry->d_inode;
1150 if (inode && unlikely(!spin_trylock(&inode->i_lock))) {
Al Viro3b3f09f2018-02-23 21:54:18 -05001151 spin_unlock(&dentry->d_lock);
1152 spin_lock(&inode->i_lock);
1153 spin_lock(&dentry->d_lock);
1154 if (unlikely(dentry->d_lockref.count))
1155 goto out;
1156 /* changed inode means that somebody had grabbed it */
1157 if (unlikely(inode != dentry->d_inode))
1158 goto out;
Al Viro3b3f09f2018-02-23 21:54:18 -05001159 }
1160
1161 parent = dentry->d_parent;
1162 if (IS_ROOT(dentry) || likely(spin_trylock(&parent->d_lock)))
1163 return true;
1164
Al Viro3b3f09f2018-02-23 21:54:18 -05001165 spin_unlock(&dentry->d_lock);
Al Viro3b3f09f2018-02-23 21:54:18 -05001166 spin_lock(&parent->d_lock);
1167 if (unlikely(parent != dentry->d_parent)) {
1168 spin_unlock(&parent->d_lock);
1169 spin_lock(&dentry->d_lock);
1170 goto out;
1171 }
1172 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
John Ogness8f04da22018-02-23 00:50:24 +01001173 if (likely(!dentry->d_lockref.count))
Al Viro3b3f09f2018-02-23 21:54:18 -05001174 return true;
Al Viro3b3f09f2018-02-23 21:54:18 -05001175 spin_unlock(&parent->d_lock);
1176out:
1177 if (inode)
1178 spin_unlock(&inode->i_lock);
Al Viro3b3f09f2018-02-23 21:54:18 -05001179 return false;
1180}
1181
Al Viro9bdebc22019-06-29 18:31:24 -04001182void shrink_dentry_list(struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183{
Miklos Szeredi60942f2f2014-05-02 15:38:39 -04001184 while (!list_empty(list)) {
Al Viro3b3f09f2018-02-23 21:54:18 -05001185 struct dentry *dentry, *parent;
Al Viro3b3f09f2018-02-23 21:54:18 -05001186
Miklos Szeredi60942f2f2014-05-02 15:38:39 -04001187 dentry = list_entry(list->prev, struct dentry, d_lru);
Nick Pigginec336792011-01-07 17:49:47 +11001188 spin_lock(&dentry->d_lock);
John Ogness8f04da22018-02-23 00:50:24 +01001189 rcu_read_lock();
Al Viro3b3f09f2018-02-23 21:54:18 -05001190 if (!shrink_lock_dentry(dentry)) {
1191 bool can_free = false;
John Ogness8f04da22018-02-23 00:50:24 +01001192 rcu_read_unlock();
Al Viro3b3f09f2018-02-23 21:54:18 -05001193 d_shrink_del(dentry);
1194 if (dentry->d_lockref.count < 0)
1195 can_free = dentry->d_flags & DCACHE_MAY_FREE;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -07001196 spin_unlock(&dentry->d_lock);
Al Viro64fd72e2014-05-28 09:48:44 -04001197 if (can_free)
1198 dentry_free(dentry);
1199 continue;
1200 }
John Ogness8f04da22018-02-23 00:50:24 +01001201 rcu_read_unlock();
Al Viro3b3f09f2018-02-23 21:54:18 -05001202 d_shrink_del(dentry);
1203 parent = dentry->d_parent;
Al Viro9bdebc22019-06-29 18:31:24 -04001204 if (parent != dentry)
1205 __dput_to_list(parent, list);
Al Viroff2fde92014-05-28 13:59:13 -04001206 __dentry_kill(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 }
Christoph Hellwig3049cfe2010-10-10 05:36:25 -04001208}
1209
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001210static enum lru_status dentry_lru_isolate(struct list_head *item,
1211 struct list_lru_one *lru, spinlock_t *lru_lock, void *arg)
Dave Chinnerf6041562013-08-28 10:18:00 +10001212{
1213 struct list_head *freeable = arg;
1214 struct dentry *dentry = container_of(item, struct dentry, d_lru);
1215
1216
1217 /*
1218 * we are inverting the lru lock/dentry->d_lock here,
1219 * so use a trylock. If we fail to get the lock, just skip
1220 * it
1221 */
1222 if (!spin_trylock(&dentry->d_lock))
1223 return LRU_SKIP;
1224
1225 /*
1226 * Referenced dentries are still in use. If they have active
1227 * counts, just remove them from the LRU. Otherwise give them
1228 * another pass through the LRU.
1229 */
1230 if (dentry->d_lockref.count) {
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001231 d_lru_isolate(lru, dentry);
Dave Chinnerf6041562013-08-28 10:18:00 +10001232 spin_unlock(&dentry->d_lock);
1233 return LRU_REMOVED;
1234 }
1235
1236 if (dentry->d_flags & DCACHE_REFERENCED) {
1237 dentry->d_flags &= ~DCACHE_REFERENCED;
1238 spin_unlock(&dentry->d_lock);
1239
1240 /*
1241 * The list move itself will be made by the common LRU code. At
1242 * this point, we've dropped the dentry->d_lock but keep the
1243 * lru lock. This is safe to do, since every list movement is
1244 * protected by the lru lock even if both locks are held.
1245 *
1246 * This is guaranteed by the fact that all LRU management
1247 * functions are intermediated by the LRU API calls like
1248 * list_lru_add and list_lru_del. List movement in this file
1249 * only ever occur through this functions or through callbacks
1250 * like this one, that are called from the LRU API.
1251 *
1252 * The only exceptions to this are functions like
1253 * shrink_dentry_list, and code that first checks for the
1254 * DCACHE_SHRINK_LIST flag. Those are guaranteed to be
1255 * operating only with stack provided lists after they are
1256 * properly isolated from the main list. It is thus, always a
1257 * local access.
1258 */
1259 return LRU_ROTATE;
1260 }
1261
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001262 d_lru_shrink_move(lru, dentry, freeable);
Dave Chinnerf6041562013-08-28 10:18:00 +10001263 spin_unlock(&dentry->d_lock);
1264
1265 return LRU_REMOVED;
1266}
1267
Christoph Hellwig3049cfe2010-10-10 05:36:25 -04001268/**
Dave Chinnerb48f03b32011-08-23 18:56:24 +10001269 * prune_dcache_sb - shrink the dcache
1270 * @sb: superblock
Vladimir Davydov503c3582015-02-12 14:58:47 -08001271 * @sc: shrink control, passed to list_lru_shrink_walk()
Christoph Hellwig3049cfe2010-10-10 05:36:25 -04001272 *
Vladimir Davydov503c3582015-02-12 14:58:47 -08001273 * Attempt to shrink the superblock dcache LRU by @sc->nr_to_scan entries. This
1274 * is done when we need more memory and called from the superblock shrinker
Dave Chinnerb48f03b32011-08-23 18:56:24 +10001275 * function.
1276 *
1277 * This function may fail to free any resources if all the dentries are in
1278 * use.
Christoph Hellwig3049cfe2010-10-10 05:36:25 -04001279 */
Vladimir Davydov503c3582015-02-12 14:58:47 -08001280long prune_dcache_sb(struct super_block *sb, struct shrink_control *sc)
Christoph Hellwig3049cfe2010-10-10 05:36:25 -04001281{
Dave Chinnerf6041562013-08-28 10:18:00 +10001282 LIST_HEAD(dispose);
1283 long freed;
Christoph Hellwig3049cfe2010-10-10 05:36:25 -04001284
Vladimir Davydov503c3582015-02-12 14:58:47 -08001285 freed = list_lru_shrink_walk(&sb->s_dentry_lru, sc,
1286 dentry_lru_isolate, &dispose);
Dave Chinnerf6041562013-08-28 10:18:00 +10001287 shrink_dentry_list(&dispose);
Dave Chinner0a234c62013-08-28 10:17:57 +10001288 return freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289}
1290
Glauber Costa4e717f52013-08-28 10:18:03 +10001291static enum lru_status dentry_lru_isolate_shrink(struct list_head *item,
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001292 struct list_lru_one *lru, spinlock_t *lru_lock, void *arg)
Dave Chinnerdd1f6b22013-08-28 10:17:55 +10001293{
Glauber Costa4e717f52013-08-28 10:18:03 +10001294 struct list_head *freeable = arg;
1295 struct dentry *dentry = container_of(item, struct dentry, d_lru);
Dave Chinnerdd1f6b22013-08-28 10:17:55 +10001296
Glauber Costa4e717f52013-08-28 10:18:03 +10001297 /*
1298 * we are inverting the lru lock/dentry->d_lock here,
1299 * so use a trylock. If we fail to get the lock, just skip
1300 * it
1301 */
1302 if (!spin_trylock(&dentry->d_lock))
1303 return LRU_SKIP;
1304
Vladimir Davydov3f97b162015-02-12 14:59:35 -08001305 d_lru_shrink_move(lru, dentry, freeable);
Glauber Costa4e717f52013-08-28 10:18:03 +10001306 spin_unlock(&dentry->d_lock);
1307
1308 return LRU_REMOVED;
Dave Chinnerdd1f6b22013-08-28 10:17:55 +10001309}
1310
Glauber Costa4e717f52013-08-28 10:18:03 +10001311
Kentaro Makitada3bbdd2008-07-23 21:27:13 -07001312/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 * shrink_dcache_sb - shrink dcache for a superblock
1314 * @sb: superblock
1315 *
Christoph Hellwig3049cfe2010-10-10 05:36:25 -04001316 * Shrink the dcache for the specified super block. This is used to free
1317 * the dcache before unmounting a file system.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 */
Christoph Hellwig3049cfe2010-10-10 05:36:25 -04001319void shrink_dcache_sb(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320{
Glauber Costa4e717f52013-08-28 10:18:03 +10001321 do {
1322 LIST_HEAD(dispose);
1323
Waiman Long1dbd4492019-01-30 13:52:36 -05001324 list_lru_walk(&sb->s_dentry_lru,
Sahitya Tummalab17c0702017-07-10 15:50:00 -07001325 dentry_lru_isolate_shrink, &dispose, 1024);
Glauber Costa4e717f52013-08-28 10:18:03 +10001326 shrink_dentry_list(&dispose);
Sahitya Tummalab17c0702017-07-10 15:50:00 -07001327 } while (list_lru_count(&sb->s_dentry_lru) > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001329EXPORT_SYMBOL(shrink_dcache_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331/**
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001332 * enum d_walk_ret - action to talke during tree walk
1333 * @D_WALK_CONTINUE: contrinue walk
1334 * @D_WALK_QUIT: quit walk
1335 * @D_WALK_NORETRY: quit when retry is needed
1336 * @D_WALK_SKIP: skip this dentry and its children
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 */
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001338enum d_walk_ret {
1339 D_WALK_CONTINUE,
1340 D_WALK_QUIT,
1341 D_WALK_NORETRY,
1342 D_WALK_SKIP,
1343};
1344
1345/**
1346 * d_walk - walk the dentry tree
1347 * @parent: start of walk
1348 * @data: data passed to @enter() and @finish()
1349 * @enter: callback when first entering the dentry
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001350 *
Al Viro3a8e3612018-04-15 18:27:23 -04001351 * The @enter() callbacks are called with d_lock held.
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001352 */
1353static void d_walk(struct dentry *parent, void *data,
Al Viro3a8e3612018-04-15 18:27:23 -04001354 enum d_walk_ret (*enter)(void *, struct dentry *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355{
Nick Piggin949854d2011-01-07 17:49:37 +11001356 struct dentry *this_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 struct list_head *next;
Al Viro48f5ec22013-09-09 15:22:25 -04001358 unsigned seq = 0;
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001359 enum d_walk_ret ret;
1360 bool retry = true;
Nick Piggin949854d2011-01-07 17:49:37 +11001361
Nick Piggin58db63d2011-01-07 17:49:39 +11001362again:
Al Viro48f5ec22013-09-09 15:22:25 -04001363 read_seqbegin_or_lock(&rename_lock, &seq);
Nick Piggin58db63d2011-01-07 17:49:39 +11001364 this_parent = parent;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001365 spin_lock(&this_parent->d_lock);
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001366
1367 ret = enter(data, this_parent);
1368 switch (ret) {
1369 case D_WALK_CONTINUE:
1370 break;
1371 case D_WALK_QUIT:
1372 case D_WALK_SKIP:
1373 goto out_unlock;
1374 case D_WALK_NORETRY:
1375 retry = false;
1376 break;
1377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378repeat:
1379 next = this_parent->d_subdirs.next;
1380resume:
1381 while (next != &this_parent->d_subdirs) {
1382 struct list_head *tmp = next;
Al Viro946e51f2014-10-26 19:19:16 -04001383 struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 next = tmp->next;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001385
Al Viroba65dc52016-06-10 11:32:47 -04001386 if (unlikely(dentry->d_flags & DCACHE_DENTRY_CURSOR))
1387 continue;
1388
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001389 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001390
1391 ret = enter(data, dentry);
1392 switch (ret) {
1393 case D_WALK_CONTINUE:
1394 break;
1395 case D_WALK_QUIT:
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001396 spin_unlock(&dentry->d_lock);
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001397 goto out_unlock;
1398 case D_WALK_NORETRY:
1399 retry = false;
1400 break;
1401 case D_WALK_SKIP:
1402 spin_unlock(&dentry->d_lock);
1403 continue;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001404 }
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001405
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 if (!list_empty(&dentry->d_subdirs)) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001407 spin_unlock(&this_parent->d_lock);
Qian Cai5facae42019-09-19 12:09:40 -04001408 spin_release(&dentry->d_lock.dep_map, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 this_parent = dentry;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001410 spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 goto repeat;
1412 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001413 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 }
1415 /*
1416 * All done at this level ... ascend and resume the search.
1417 */
Al Viroca5358e2014-10-26 19:31:10 -04001418 rcu_read_lock();
1419ascend:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 if (this_parent != parent) {
Linus Torvaldsc826cb72011-03-15 15:29:21 -07001421 struct dentry *child = this_parent;
Al Viro31dec132013-10-25 17:04:27 -04001422 this_parent = child->d_parent;
1423
Al Viro31dec132013-10-25 17:04:27 -04001424 spin_unlock(&child->d_lock);
1425 spin_lock(&this_parent->d_lock);
1426
Al Viroca5358e2014-10-26 19:31:10 -04001427 /* might go back up the wrong parent if we have had a rename. */
1428 if (need_seqretry(&rename_lock, seq))
Nick Piggin949854d2011-01-07 17:49:37 +11001429 goto rename_retry;
Al Viro21591842015-05-28 23:09:19 -04001430 /* go into the first sibling still alive */
1431 do {
1432 next = child->d_child.next;
Al Viroca5358e2014-10-26 19:31:10 -04001433 if (next == &this_parent->d_subdirs)
1434 goto ascend;
1435 child = list_entry(next, struct dentry, d_child);
Al Viro21591842015-05-28 23:09:19 -04001436 } while (unlikely(child->d_flags & DCACHE_DENTRY_KILLED));
Al Viro31dec132013-10-25 17:04:27 -04001437 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 goto resume;
1439 }
Al Viroca5358e2014-10-26 19:31:10 -04001440 if (need_seqretry(&rename_lock, seq))
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001441 goto rename_retry;
Al Viroca5358e2014-10-26 19:31:10 -04001442 rcu_read_unlock();
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001443
1444out_unlock:
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001445 spin_unlock(&this_parent->d_lock);
Al Viro48f5ec22013-09-09 15:22:25 -04001446 done_seqretry(&rename_lock, seq);
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001447 return;
Nick Piggin58db63d2011-01-07 17:49:39 +11001448
1449rename_retry:
Al Viroca5358e2014-10-26 19:31:10 -04001450 spin_unlock(&this_parent->d_lock);
1451 rcu_read_unlock();
1452 BUG_ON(seq & 1);
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001453 if (!retry)
1454 return;
Al Viro48f5ec22013-09-09 15:22:25 -04001455 seq = 1;
Nick Piggin58db63d2011-01-07 17:49:39 +11001456 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457}
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001458
Ian Kent01619492016-11-24 08:03:41 +11001459struct check_mount {
1460 struct vfsmount *mnt;
1461 unsigned int mounted;
1462};
1463
1464static enum d_walk_ret path_check_mount(void *data, struct dentry *dentry)
1465{
1466 struct check_mount *info = data;
1467 struct path path = { .mnt = info->mnt, .dentry = dentry };
1468
1469 if (likely(!d_mountpoint(dentry)))
1470 return D_WALK_CONTINUE;
1471 if (__path_is_mountpoint(&path)) {
1472 info->mounted = 1;
1473 return D_WALK_QUIT;
1474 }
1475 return D_WALK_CONTINUE;
1476}
1477
1478/**
1479 * path_has_submounts - check for mounts over a dentry in the
1480 * current namespace.
1481 * @parent: path to check.
1482 *
1483 * Return true if the parent or its subdirectories contain
1484 * a mount point in the current namespace.
1485 */
1486int path_has_submounts(const struct path *parent)
1487{
1488 struct check_mount data = { .mnt = parent->mnt, .mounted = 0 };
1489
1490 read_seqlock_excl(&mount_lock);
Al Viro3a8e3612018-04-15 18:27:23 -04001491 d_walk(parent->dentry, &data, path_check_mount);
Ian Kent01619492016-11-24 08:03:41 +11001492 read_sequnlock_excl(&mount_lock);
1493
1494 return data.mounted;
1495}
1496EXPORT_SYMBOL(path_has_submounts);
1497
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498/*
Miklos Szeredieed81002013-09-05 14:39:11 +02001499 * Called by mount code to set a mountpoint and check if the mountpoint is
1500 * reachable (e.g. NFS can unhash a directory dentry and then the complete
1501 * subtree can become unreachable).
1502 *
Eric W. Biederman1ffe46d2014-02-13 09:39:37 -08001503 * Only one of d_invalidate() and d_set_mounted() must succeed. For
Miklos Szeredieed81002013-09-05 14:39:11 +02001504 * this reason take rename_lock and d_lock on dentry and ancestors.
1505 */
1506int d_set_mounted(struct dentry *dentry)
1507{
1508 struct dentry *p;
1509 int ret = -ENOENT;
1510 write_seqlock(&rename_lock);
1511 for (p = dentry->d_parent; !IS_ROOT(p); p = p->d_parent) {
Eric W. Biederman1ffe46d2014-02-13 09:39:37 -08001512 /* Need exclusion wrt. d_invalidate() */
Miklos Szeredieed81002013-09-05 14:39:11 +02001513 spin_lock(&p->d_lock);
1514 if (unlikely(d_unhashed(p))) {
1515 spin_unlock(&p->d_lock);
1516 goto out;
1517 }
1518 spin_unlock(&p->d_lock);
1519 }
1520 spin_lock(&dentry->d_lock);
1521 if (!d_unlinked(dentry)) {
Eric W. Biederman3895dbf2017-01-03 14:18:43 +13001522 ret = -EBUSY;
1523 if (!d_mountpoint(dentry)) {
1524 dentry->d_flags |= DCACHE_MOUNTED;
1525 ret = 0;
1526 }
Miklos Szeredieed81002013-09-05 14:39:11 +02001527 }
1528 spin_unlock(&dentry->d_lock);
1529out:
1530 write_sequnlock(&rename_lock);
1531 return ret;
1532}
1533
1534/*
J. Bruce Fieldsfd517902012-09-18 16:35:51 -04001535 * Search the dentry child list of the specified parent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 * and move any unused dentries to the end of the unused
1537 * list for prune_dcache(). We descend to the next level
1538 * whenever the d_subdirs list is non-empty and continue
1539 * searching.
1540 *
1541 * It returns zero iff there are no unused children,
1542 * otherwise it returns the number of children moved to
1543 * the end of the unused list. This may not be the total
1544 * number of unused children, because select_parent can
1545 * drop the lock and return early due to latency
1546 * constraints.
1547 */
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001548
1549struct select_data {
1550 struct dentry *start;
Al Viro9bdebc22019-06-29 18:31:24 -04001551 union {
1552 long found;
1553 struct dentry *victim;
1554 };
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001555 struct list_head dispose;
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001556};
1557
1558static enum d_walk_ret select_collect(void *_data, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559{
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001560 struct select_data *data = _data;
1561 enum d_walk_ret ret = D_WALK_CONTINUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001563 if (data->start == dentry)
1564 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
Al Virofe915222014-05-03 00:02:25 -04001566 if (dentry->d_flags & DCACHE_SHRINK_LIST) {
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001567 data->found++;
Al Virofe915222014-05-03 00:02:25 -04001568 } else {
1569 if (dentry->d_flags & DCACHE_LRU_LIST)
1570 d_lru_del(dentry);
1571 if (!dentry->d_lockref.count) {
1572 d_shrink_add(dentry, &data->dispose);
1573 data->found++;
1574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 }
1576 /*
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001577 * We can return to the caller if we have found some (this
1578 * ensures forward progress). We'll be coming back to find
1579 * the rest.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 */
Al Virofe915222014-05-03 00:02:25 -04001581 if (!list_empty(&data->dispose))
1582 ret = need_resched() ? D_WALK_QUIT : D_WALK_NORETRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583out:
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001584 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585}
1586
Al Viro9bdebc22019-06-29 18:31:24 -04001587static enum d_walk_ret select_collect2(void *_data, struct dentry *dentry)
1588{
1589 struct select_data *data = _data;
1590 enum d_walk_ret ret = D_WALK_CONTINUE;
1591
1592 if (data->start == dentry)
1593 goto out;
1594
1595 if (dentry->d_flags & DCACHE_SHRINK_LIST) {
1596 if (!dentry->d_lockref.count) {
1597 rcu_read_lock();
1598 data->victim = dentry;
1599 return D_WALK_QUIT;
1600 }
1601 } else {
1602 if (dentry->d_flags & DCACHE_LRU_LIST)
1603 d_lru_del(dentry);
1604 if (!dentry->d_lockref.count)
1605 d_shrink_add(dentry, &data->dispose);
1606 }
1607 /*
1608 * We can return to the caller if we have found some (this
1609 * ensures forward progress). We'll be coming back to find
1610 * the rest.
1611 */
1612 if (!list_empty(&data->dispose))
1613 ret = need_resched() ? D_WALK_QUIT : D_WALK_NORETRY;
1614out:
1615 return ret;
1616}
1617
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618/**
1619 * shrink_dcache_parent - prune dcache
1620 * @parent: parent of entries to prune
1621 *
1622 * Prune the dcache to remove unused children of the parent dentry.
1623 */
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001624void shrink_dcache_parent(struct dentry *parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625{
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001626 for (;;) {
Al Viro9bdebc22019-06-29 18:31:24 -04001627 struct select_data data = {.start = parent};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001629 INIT_LIST_HEAD(&data.dispose);
Al Viro3a8e3612018-04-15 18:27:23 -04001630 d_walk(parent, &data, select_collect);
Al Viro4fb48872018-04-19 23:58:48 -04001631
1632 if (!list_empty(&data.dispose)) {
1633 shrink_dentry_list(&data.dispose);
1634 continue;
1635 }
1636
1637 cond_resched();
Miklos Szeredidb14fc32013-09-05 11:44:35 +02001638 if (!data.found)
1639 break;
Al Viro9bdebc22019-06-29 18:31:24 -04001640 data.victim = NULL;
1641 d_walk(parent, &data, select_collect2);
1642 if (data.victim) {
1643 struct dentry *parent;
1644 spin_lock(&data.victim->d_lock);
1645 if (!shrink_lock_dentry(data.victim)) {
1646 spin_unlock(&data.victim->d_lock);
1647 rcu_read_unlock();
1648 } else {
1649 rcu_read_unlock();
1650 parent = data.victim->d_parent;
1651 if (parent != data.victim)
1652 __dput_to_list(parent, &data.dispose);
1653 __dentry_kill(data.victim);
1654 }
1655 }
1656 if (!list_empty(&data.dispose))
1657 shrink_dentry_list(&data.dispose);
Greg Thelen421348f2013-04-30 15:26:48 -07001658 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001660EXPORT_SYMBOL(shrink_dcache_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
Al Viro9c8c10e2014-05-02 20:36:10 -04001662static enum d_walk_ret umount_check(void *_data, struct dentry *dentry)
Al Viro42c32602013-11-08 12:31:16 -05001663{
Al Viro9c8c10e2014-05-02 20:36:10 -04001664 /* it has busy descendents; complain about those instead */
1665 if (!list_empty(&dentry->d_subdirs))
1666 return D_WALK_CONTINUE;
Al Viro42c32602013-11-08 12:31:16 -05001667
Al Viro9c8c10e2014-05-02 20:36:10 -04001668 /* root with refcount 1 is fine */
1669 if (dentry == _data && dentry->d_lockref.count == 1)
1670 return D_WALK_CONTINUE;
1671
1672 printk(KERN_ERR "BUG: Dentry %p{i=%lx,n=%pd} "
1673 " still in use (%d) [unmount of %s %s]\n",
Al Viro42c32602013-11-08 12:31:16 -05001674 dentry,
1675 dentry->d_inode ?
1676 dentry->d_inode->i_ino : 0UL,
Al Viro9c8c10e2014-05-02 20:36:10 -04001677 dentry,
Al Viro42c32602013-11-08 12:31:16 -05001678 dentry->d_lockref.count,
1679 dentry->d_sb->s_type->name,
1680 dentry->d_sb->s_id);
Al Viro9c8c10e2014-05-02 20:36:10 -04001681 WARN_ON(1);
1682 return D_WALK_CONTINUE;
1683}
1684
1685static void do_one_tree(struct dentry *dentry)
1686{
1687 shrink_dcache_parent(dentry);
Al Viro3a8e3612018-04-15 18:27:23 -04001688 d_walk(dentry, dentry, umount_check);
Al Viro9c8c10e2014-05-02 20:36:10 -04001689 d_drop(dentry);
1690 dput(dentry);
Al Viro42c32602013-11-08 12:31:16 -05001691}
1692
1693/*
1694 * destroy the dentries attached to a superblock on unmounting
1695 */
1696void shrink_dcache_for_umount(struct super_block *sb)
1697{
1698 struct dentry *dentry;
1699
Al Viro9c8c10e2014-05-02 20:36:10 -04001700 WARN(down_read_trylock(&sb->s_umount), "s_umount should've been locked");
Al Viro42c32602013-11-08 12:31:16 -05001701
1702 dentry = sb->s_root;
1703 sb->s_root = NULL;
Al Viro9c8c10e2014-05-02 20:36:10 -04001704 do_one_tree(dentry);
Al Viro42c32602013-11-08 12:31:16 -05001705
NeilBrownf1ee6162017-12-21 09:45:40 +11001706 while (!hlist_bl_empty(&sb->s_roots)) {
1707 dentry = dget(hlist_bl_entry(hlist_bl_first(&sb->s_roots), struct dentry, d_hash));
Al Viro9c8c10e2014-05-02 20:36:10 -04001708 do_one_tree(dentry);
Al Viro42c32602013-11-08 12:31:16 -05001709 }
1710}
1711
Al Viroff17fa52018-04-15 18:21:47 -04001712static enum d_walk_ret find_submount(void *_data, struct dentry *dentry)
Miklos Szeredi848ac1142013-09-05 11:44:36 +02001713{
Al Viroff17fa52018-04-15 18:21:47 -04001714 struct dentry **victim = _data;
Miklos Szeredi848ac1142013-09-05 11:44:36 +02001715 if (d_mountpoint(dentry)) {
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07001716 __dget_dlock(dentry);
Al Viroff17fa52018-04-15 18:21:47 -04001717 *victim = dentry;
Miklos Szeredi848ac1142013-09-05 11:44:36 +02001718 return D_WALK_QUIT;
1719 }
Al Viroff17fa52018-04-15 18:21:47 -04001720 return D_WALK_CONTINUE;
Miklos Szeredi848ac1142013-09-05 11:44:36 +02001721}
1722
1723/**
Eric W. Biederman1ffe46d2014-02-13 09:39:37 -08001724 * d_invalidate - detach submounts, prune dcache, and drop
1725 * @dentry: dentry to invalidate (aka detach, prune and drop)
Miklos Szeredi848ac1142013-09-05 11:44:36 +02001726 */
Eric W. Biederman5542aa22014-02-13 09:46:25 -08001727void d_invalidate(struct dentry *dentry)
Miklos Szeredi848ac1142013-09-05 11:44:36 +02001728{
Al Viroff17fa52018-04-15 18:21:47 -04001729 bool had_submounts = false;
Eric W. Biederman1ffe46d2014-02-13 09:39:37 -08001730 spin_lock(&dentry->d_lock);
1731 if (d_unhashed(dentry)) {
1732 spin_unlock(&dentry->d_lock);
Eric W. Biederman5542aa22014-02-13 09:46:25 -08001733 return;
Eric W. Biederman1ffe46d2014-02-13 09:39:37 -08001734 }
Al Viroff17fa52018-04-15 18:21:47 -04001735 __d_drop(dentry);
Eric W. Biederman1ffe46d2014-02-13 09:39:37 -08001736 spin_unlock(&dentry->d_lock);
1737
Miklos Szeredi848ac1142013-09-05 11:44:36 +02001738 /* Negative dentries can be dropped without further checks */
Al Viroff17fa52018-04-15 18:21:47 -04001739 if (!dentry->d_inode)
Eric W. Biederman5542aa22014-02-13 09:46:25 -08001740 return;
Miklos Szeredi848ac1142013-09-05 11:44:36 +02001741
Al Viroff17fa52018-04-15 18:21:47 -04001742 shrink_dcache_parent(dentry);
Miklos Szeredi848ac1142013-09-05 11:44:36 +02001743 for (;;) {
Al Viroff17fa52018-04-15 18:21:47 -04001744 struct dentry *victim = NULL;
Al Viro3a8e3612018-04-15 18:27:23 -04001745 d_walk(dentry, &victim, find_submount);
Al Viroff17fa52018-04-15 18:21:47 -04001746 if (!victim) {
1747 if (had_submounts)
1748 shrink_dcache_parent(dentry);
Al Viro81be24d2017-06-03 07:20:09 +01001749 return;
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07001750 }
Al Viroff17fa52018-04-15 18:21:47 -04001751 had_submounts = true;
1752 detach_mounts(victim);
1753 dput(victim);
Miklos Szeredi848ac1142013-09-05 11:44:36 +02001754 }
Miklos Szeredi848ac1142013-09-05 11:44:36 +02001755}
Eric W. Biederman1ffe46d2014-02-13 09:39:37 -08001756EXPORT_SYMBOL(d_invalidate);
Miklos Szeredi848ac1142013-09-05 11:44:36 +02001757
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758/**
Al Viroa4464db2011-07-07 15:03:58 -04001759 * __d_alloc - allocate a dcache entry
1760 * @sb: filesystem it will belong to
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 * @name: qstr of the name
1762 *
1763 * Allocates a dentry. It returns %NULL if there is insufficient memory
1764 * available. On a success the dentry is returned. The name passed in is
1765 * copied and the copy passed in may be reused after this call.
1766 */
1767
Al Viro5c8b0df2019-10-25 14:08:24 -04001768static struct dentry *__d_alloc(struct super_block *sb, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769{
1770 struct dentry *dentry;
1771 char *dname;
Miklos Szeredi285b1022016-06-28 11:47:32 +02001772 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
Muchun Songf53bf7112022-03-22 14:41:09 -07001774 dentry = kmem_cache_alloc_lru(dentry_cache, &sb->s_dentry_lru,
1775 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 if (!dentry)
1777 return NULL;
1778
Linus Torvalds6326c71f2012-05-21 16:14:04 -07001779 /*
1780 * We guarantee that the inline name is always NUL-terminated.
1781 * This way the memcpy() done by the name switching in rename
1782 * will still always have a NUL at the end, even if we might
1783 * be overwriting an internal NUL character
1784 */
1785 dentry->d_iname[DNAME_INLINE_LEN-1] = 0;
Al Viro798434b2016-03-24 20:38:43 -04001786 if (unlikely(!name)) {
David Howellscdf01222017-07-04 17:25:22 +01001787 name = &slash_name;
Al Viro798434b2016-03-24 20:38:43 -04001788 dname = dentry->d_iname;
1789 } else if (name->len > DNAME_INLINE_LEN-1) {
Al Viro8d85b482014-09-29 14:54:27 -04001790 size_t size = offsetof(struct external_name, name[1]);
Vlastimil Babka2e03b4b2018-10-26 15:05:41 -07001791 struct external_name *p = kmalloc(size + name->len,
1792 GFP_KERNEL_ACCOUNT |
1793 __GFP_RECLAIMABLE);
1794 if (!p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 kmem_cache_free(dentry_cache, dentry);
1796 return NULL;
1797 }
Vlastimil Babka2e03b4b2018-10-26 15:05:41 -07001798 atomic_set(&p->u.count, 1);
1799 dname = p->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 } else {
1801 dname = dentry->d_iname;
1802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
1804 dentry->d_name.len = name->len;
1805 dentry->d_name.hash = name->hash;
1806 memcpy(dname, name->name, name->len);
1807 dname[name->len] = 0;
1808
Linus Torvalds6326c71f2012-05-21 16:14:04 -07001809 /* Make sure we always see the terminating NUL character */
Paul E. McKenney7088efa2017-10-09 10:04:27 -07001810 smp_store_release(&dentry->d_name.name, dname); /* ^^^ */
Linus Torvalds6326c71f2012-05-21 16:14:04 -07001811
Waiman Long98474232013-08-28 18:24:59 -07001812 dentry->d_lockref.count = 1;
Linus Torvaldsdea36672011-04-24 07:58:46 -07001813 dentry->d_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 spin_lock_init(&dentry->d_lock);
Ahmed S. Darwish26475372020-07-20 17:55:24 +02001815 seqcount_spinlock_init(&dentry->d_seq, &dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 dentry->d_inode = NULL;
Al Viroa4464db2011-07-07 15:03:58 -04001817 dentry->d_parent = dentry;
1818 dentry->d_sb = sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 dentry->d_op = NULL;
1820 dentry->d_fsdata = NULL;
Nick Pigginceb5bdc2011-01-07 17:50:05 +11001821 INIT_HLIST_BL_NODE(&dentry->d_hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 INIT_LIST_HEAD(&dentry->d_lru);
1823 INIT_LIST_HEAD(&dentry->d_subdirs);
Al Viro946e51f2014-10-26 19:19:16 -04001824 INIT_HLIST_NODE(&dentry->d_u.d_alias);
1825 INIT_LIST_HEAD(&dentry->d_child);
Al Viroa4464db2011-07-07 15:03:58 -04001826 d_set_d_op(dentry, dentry->d_sb->s_d_op);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
Miklos Szeredi285b1022016-06-28 11:47:32 +02001828 if (dentry->d_op && dentry->d_op->d_init) {
1829 err = dentry->d_op->d_init(dentry);
1830 if (err) {
1831 if (dname_external(dentry))
1832 kfree(external_name(dentry));
1833 kmem_cache_free(dentry_cache, dentry);
1834 return NULL;
1835 }
1836 }
1837
Nick Piggin3e880fb2011-01-07 17:49:19 +11001838 this_cpu_inc(nr_dentry);
Christoph Hellwig312d3ca2010-10-10 05:36:23 -04001839
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 return dentry;
1841}
Al Viroa4464db2011-07-07 15:03:58 -04001842
1843/**
1844 * d_alloc - allocate a dcache entry
1845 * @parent: parent of entry to allocate
1846 * @name: qstr of the name
1847 *
1848 * Allocates a dentry. It returns %NULL if there is insufficient memory
1849 * available. On a success the dentry is returned. The name passed in is
1850 * copied and the copy passed in may be reused after this call.
1851 */
1852struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
1853{
1854 struct dentry *dentry = __d_alloc(parent->d_sb, name);
1855 if (!dentry)
1856 return NULL;
Al Viroa4464db2011-07-07 15:03:58 -04001857 spin_lock(&parent->d_lock);
1858 /*
1859 * don't need child lock because it is not subject
1860 * to concurrency here
1861 */
1862 __dget_dlock(parent);
1863 dentry->d_parent = parent;
Al Viro946e51f2014-10-26 19:19:16 -04001864 list_add(&dentry->d_child, &parent->d_subdirs);
Al Viroa4464db2011-07-07 15:03:58 -04001865 spin_unlock(&parent->d_lock);
1866
1867 return dentry;
1868}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001869EXPORT_SYMBOL(d_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
Miklos Szeredif9c34672018-01-19 11:39:52 +01001871struct dentry *d_alloc_anon(struct super_block *sb)
1872{
1873 return __d_alloc(sb, NULL);
1874}
1875EXPORT_SYMBOL(d_alloc_anon);
1876
Al Viroba65dc52016-06-10 11:32:47 -04001877struct dentry *d_alloc_cursor(struct dentry * parent)
1878{
Miklos Szeredif9c34672018-01-19 11:39:52 +01001879 struct dentry *dentry = d_alloc_anon(parent->d_sb);
Al Viroba65dc52016-06-10 11:32:47 -04001880 if (dentry) {
Al Viro5467a682019-03-15 22:23:19 -04001881 dentry->d_flags |= DCACHE_DENTRY_CURSOR;
Al Viroba65dc52016-06-10 11:32:47 -04001882 dentry->d_parent = dget(parent);
1883 }
1884 return dentry;
1885}
1886
J. Bruce Fieldse1a24bb2012-06-29 16:20:47 -04001887/**
1888 * d_alloc_pseudo - allocate a dentry (for lookup-less filesystems)
1889 * @sb: the superblock
1890 * @name: qstr of the name
1891 *
1892 * For a filesystem that just pins its dentries in memory and never
1893 * performs lookups at all, return an unhashed IS_ROOT dentry.
Al Viro5467a682019-03-15 22:23:19 -04001894 * This is used for pipes, sockets et.al. - the stuff that should
1895 * never be anyone's children or parents. Unlike all other
1896 * dentries, these will not have RCU delay between dropping the
1897 * last reference and freeing them.
Al Viroab1152d2019-03-15 22:58:11 -04001898 *
1899 * The only user is alloc_file_pseudo() and that's what should
1900 * be considered a public interface. Don't use directly.
J. Bruce Fieldse1a24bb2012-06-29 16:20:47 -04001901 */
Nick Piggin4b936882011-01-07 17:50:07 +11001902struct dentry *d_alloc_pseudo(struct super_block *sb, const struct qstr *name)
1903{
Al Viro5467a682019-03-15 22:23:19 -04001904 struct dentry *dentry = __d_alloc(sb, name);
1905 if (likely(dentry))
1906 dentry->d_flags |= DCACHE_NORCU;
1907 return dentry;
Nick Piggin4b936882011-01-07 17:50:07 +11001908}
Nick Piggin4b936882011-01-07 17:50:07 +11001909
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910struct dentry *d_alloc_name(struct dentry *parent, const char *name)
1911{
1912 struct qstr q;
1913
1914 q.name = name;
Linus Torvalds8387ff22016-06-10 07:51:30 -07001915 q.hash_len = hashlen_string(parent, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 return d_alloc(parent, &q);
1917}
H Hartley Sweetenef26ca92009-09-29 20:09:42 -04001918EXPORT_SYMBOL(d_alloc_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
Nick Pigginfb045ad2011-01-07 17:49:55 +11001920void d_set_d_op(struct dentry *dentry, const struct dentry_operations *op)
1921{
Linus Torvalds6f7f7ca2011-01-14 13:26:18 -08001922 WARN_ON_ONCE(dentry->d_op);
1923 WARN_ON_ONCE(dentry->d_flags & (DCACHE_OP_HASH |
Nick Pigginfb045ad2011-01-07 17:49:55 +11001924 DCACHE_OP_COMPARE |
1925 DCACHE_OP_REVALIDATE |
Jeff Laytonecf3d1f2013-02-20 11:19:05 -05001926 DCACHE_OP_WEAK_REVALIDATE |
David Howells4bacc9c2015-06-18 14:32:31 +01001927 DCACHE_OP_DELETE |
Miklos Szeredid101a122016-03-26 16:14:37 -04001928 DCACHE_OP_REAL));
Nick Pigginfb045ad2011-01-07 17:49:55 +11001929 dentry->d_op = op;
1930 if (!op)
1931 return;
1932 if (op->d_hash)
1933 dentry->d_flags |= DCACHE_OP_HASH;
1934 if (op->d_compare)
1935 dentry->d_flags |= DCACHE_OP_COMPARE;
1936 if (op->d_revalidate)
1937 dentry->d_flags |= DCACHE_OP_REVALIDATE;
Jeff Laytonecf3d1f2013-02-20 11:19:05 -05001938 if (op->d_weak_revalidate)
1939 dentry->d_flags |= DCACHE_OP_WEAK_REVALIDATE;
Nick Pigginfb045ad2011-01-07 17:49:55 +11001940 if (op->d_delete)
1941 dentry->d_flags |= DCACHE_OP_DELETE;
Sage Weilf0023bc2011-10-28 10:02:42 -07001942 if (op->d_prune)
1943 dentry->d_flags |= DCACHE_OP_PRUNE;
Miklos Szeredid101a122016-03-26 16:14:37 -04001944 if (op->d_real)
1945 dentry->d_flags |= DCACHE_OP_REAL;
Nick Pigginfb045ad2011-01-07 17:49:55 +11001946
1947}
1948EXPORT_SYMBOL(d_set_d_op);
1949
David Howellsdf1a0852015-01-29 12:02:28 +00001950
1951/*
1952 * d_set_fallthru - Mark a dentry as falling through to a lower layer
1953 * @dentry - The dentry to mark
1954 *
1955 * Mark a dentry as falling through to the lower layer (as set with
1956 * d_pin_lower()). This flag may be recorded on the medium.
1957 */
1958void d_set_fallthru(struct dentry *dentry)
1959{
1960 spin_lock(&dentry->d_lock);
1961 dentry->d_flags |= DCACHE_FALLTHRU;
1962 spin_unlock(&dentry->d_lock);
1963}
1964EXPORT_SYMBOL(d_set_fallthru);
1965
David Howellsb18825a2013-09-12 19:22:53 +01001966static unsigned d_flags_for_inode(struct inode *inode)
1967{
David Howells44bdb5e2015-01-29 12:02:29 +00001968 unsigned add_flags = DCACHE_REGULAR_TYPE;
David Howellsb18825a2013-09-12 19:22:53 +01001969
1970 if (!inode)
1971 return DCACHE_MISS_TYPE;
1972
1973 if (S_ISDIR(inode->i_mode)) {
1974 add_flags = DCACHE_DIRECTORY_TYPE;
1975 if (unlikely(!(inode->i_opflags & IOP_LOOKUP))) {
1976 if (unlikely(!inode->i_op->lookup))
1977 add_flags = DCACHE_AUTODIR_TYPE;
1978 else
1979 inode->i_opflags |= IOP_LOOKUP;
1980 }
David Howells44bdb5e2015-01-29 12:02:29 +00001981 goto type_determined;
David Howellsb18825a2013-09-12 19:22:53 +01001982 }
1983
David Howells44bdb5e2015-01-29 12:02:29 +00001984 if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) {
Al Viro6b255392015-11-17 10:20:54 -05001985 if (unlikely(inode->i_op->get_link)) {
David Howells44bdb5e2015-01-29 12:02:29 +00001986 add_flags = DCACHE_SYMLINK_TYPE;
1987 goto type_determined;
1988 }
1989 inode->i_opflags |= IOP_NOFOLLOW;
1990 }
1991
1992 if (unlikely(!S_ISREG(inode->i_mode)))
1993 add_flags = DCACHE_SPECIAL_TYPE;
1994
1995type_determined:
David Howellsb18825a2013-09-12 19:22:53 +01001996 if (unlikely(IS_AUTOMOUNT(inode)))
1997 add_flags |= DCACHE_NEED_AUTOMOUNT;
1998 return add_flags;
1999}
2000
OGAWA Hirofumi360da902008-10-16 07:50:28 +09002001static void __d_instantiate(struct dentry *dentry, struct inode *inode)
2002{
David Howellsb18825a2013-09-12 19:22:53 +01002003 unsigned add_flags = d_flags_for_inode(inode);
Al Viro85c7f812016-04-14 19:52:13 -04002004 WARN_ON(d_in_lookup(dentry));
David Howellsb18825a2013-09-12 19:22:53 +01002005
Nick Pigginb23fb0a2011-01-07 17:49:35 +11002006 spin_lock(&dentry->d_lock);
Waiman Longaf0c9af2019-01-30 13:52:38 -05002007 /*
Brian Fosterf6f6fdc2024-07-03 08:13:01 -04002008 * The negative counter only tracks dentries on the LRU. Don't dec if
2009 * d_lru is on another list.
Waiman Longaf0c9af2019-01-30 13:52:38 -05002010 */
Brian Fosterf6f6fdc2024-07-03 08:13:01 -04002011 if ((dentry->d_flags &
2012 (DCACHE_LRU_LIST|DCACHE_SHRINK_LIST)) == DCACHE_LRU_LIST)
Waiman Longaf0c9af2019-01-30 13:52:38 -05002013 this_cpu_dec(nr_dentry_negative);
Al Virode689f52016-03-09 18:05:42 -05002014 hlist_add_head(&dentry->d_u.d_alias, &inode->i_dentry);
Al Viroa528aca2016-02-29 12:12:46 -05002015 raw_write_seqcount_begin(&dentry->d_seq);
David Howells4bf46a22015-03-05 14:09:22 +00002016 __d_set_inode_and_type(dentry, inode, add_flags);
Al Viroa528aca2016-02-29 12:12:46 -05002017 raw_write_seqcount_end(&dentry->d_seq);
Al Viroaffda482016-05-29 18:35:12 -04002018 fsnotify_update_flags(dentry);
Nick Pigginb23fb0a2011-01-07 17:49:35 +11002019 spin_unlock(&dentry->d_lock);
OGAWA Hirofumi360da902008-10-16 07:50:28 +09002020}
2021
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022/**
2023 * d_instantiate - fill in inode information for a dentry
2024 * @entry: dentry to complete
2025 * @inode: inode to attach to this dentry
2026 *
2027 * Fill in inode information in the entry.
2028 *
2029 * This turns negative dentries into productive full members
2030 * of society.
2031 *
2032 * NOTE! This assumes that the inode count has been incremented
2033 * (or otherwise set) by the caller to indicate that it is now
2034 * in use by the dcache.
2035 */
2036
2037void d_instantiate(struct dentry *entry, struct inode * inode)
2038{
Al Viro946e51f2014-10-26 19:19:16 -04002039 BUG_ON(!hlist_unhashed(&entry->d_u.d_alias));
Al Virode689f52016-03-09 18:05:42 -05002040 if (inode) {
Al Virob9680912016-04-11 00:53:26 -04002041 security_d_instantiate(entry, inode);
Nick Piggin873feea2011-01-07 17:50:06 +11002042 spin_lock(&inode->i_lock);
Al Virode689f52016-03-09 18:05:42 -05002043 __d_instantiate(entry, inode);
Nick Piggin873feea2011-01-07 17:50:06 +11002044 spin_unlock(&inode->i_lock);
Al Virode689f52016-03-09 18:05:42 -05002045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002047EXPORT_SYMBOL(d_instantiate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048
Al Viro1e2e5472018-05-04 08:23:01 -04002049/*
2050 * This should be equivalent to d_instantiate() + unlock_new_inode(),
2051 * with lockdep-related part of unlock_new_inode() done before
2052 * anything else. Use that instead of open-coding d_instantiate()/
2053 * unlock_new_inode() combinations.
2054 */
2055void d_instantiate_new(struct dentry *entry, struct inode *inode)
2056{
2057 BUG_ON(!hlist_unhashed(&entry->d_u.d_alias));
2058 BUG_ON(!inode);
2059 lockdep_annotate_inode_mutex_key(inode);
2060 security_d_instantiate(entry, inode);
2061 spin_lock(&inode->i_lock);
2062 __d_instantiate(entry, inode);
2063 WARN_ON(!(inode->i_state & I_NEW));
Al Viroc2b6d622018-06-28 15:53:17 -04002064 inode->i_state &= ~I_NEW & ~I_CREATING;
Al Viro1e2e5472018-05-04 08:23:01 -04002065 smp_mb();
2066 wake_up_bit(&inode->i_state, __I_NEW);
2067 spin_unlock(&inode->i_lock);
2068}
2069EXPORT_SYMBOL(d_instantiate_new);
2070
Al Viroadc0e91a2012-01-08 16:49:21 -05002071struct dentry *d_make_root(struct inode *root_inode)
2072{
2073 struct dentry *res = NULL;
2074
2075 if (root_inode) {
Miklos Szeredif9c34672018-01-19 11:39:52 +01002076 res = d_alloc_anon(root_inode->i_sb);
Al Viro5467a682019-03-15 22:23:19 -04002077 if (res)
Al Viroadc0e91a2012-01-08 16:49:21 -05002078 d_instantiate(res, root_inode);
Al Viro5467a682019-03-15 22:23:19 -04002079 else
Al Viroadc0e91a2012-01-08 16:49:21 -05002080 iput(root_inode);
2081 }
2082 return res;
2083}
2084EXPORT_SYMBOL(d_make_root);
2085
Miklos Szeredif9c34672018-01-19 11:39:52 +01002086static struct dentry *__d_instantiate_anon(struct dentry *dentry,
2087 struct inode *inode,
2088 bool disconnected)
2089{
2090 struct dentry *res;
2091 unsigned add_flags;
2092
2093 security_d_instantiate(dentry, inode);
2094 spin_lock(&inode->i_lock);
2095 res = __d_find_any_alias(inode);
2096 if (res) {
2097 spin_unlock(&inode->i_lock);
2098 dput(dentry);
2099 goto out_iput;
2100 }
2101
2102 /* attach a disconnected dentry */
2103 add_flags = d_flags_for_inode(inode);
2104
2105 if (disconnected)
2106 add_flags |= DCACHE_DISCONNECTED;
2107
2108 spin_lock(&dentry->d_lock);
2109 __d_set_inode_and_type(dentry, inode, add_flags);
2110 hlist_add_head(&dentry->d_u.d_alias, &inode->i_dentry);
Linus Torvalds139351f2018-02-05 13:05:20 -08002111 if (!disconnected) {
2112 hlist_bl_lock(&dentry->d_sb->s_roots);
2113 hlist_bl_add_head(&dentry->d_hash, &dentry->d_sb->s_roots);
2114 hlist_bl_unlock(&dentry->d_sb->s_roots);
2115 }
Miklos Szeredif9c34672018-01-19 11:39:52 +01002116 spin_unlock(&dentry->d_lock);
2117 spin_unlock(&inode->i_lock);
2118
2119 return dentry;
2120
2121 out_iput:
2122 iput(inode);
2123 return res;
2124}
2125
2126struct dentry *d_instantiate_anon(struct dentry *dentry, struct inode *inode)
2127{
2128 return __d_instantiate_anon(dentry, inode, true);
2129}
2130EXPORT_SYMBOL(d_instantiate_anon);
2131
2132static struct dentry *__d_obtain_alias(struct inode *inode, bool disconnected)
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02002133{
Christoph Hellwig9308a612008-08-11 15:49:12 +02002134 struct dentry *tmp;
2135 struct dentry *res;
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02002136
2137 if (!inode)
Christoph Hellwig44003722008-08-11 15:49:04 +02002138 return ERR_PTR(-ESTALE);
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02002139 if (IS_ERR(inode))
2140 return ERR_CAST(inode);
2141
J. Bruce Fieldsd891eed2011-01-18 15:45:09 -05002142 res = d_find_any_alias(inode);
Christoph Hellwig9308a612008-08-11 15:49:12 +02002143 if (res)
2144 goto out_iput;
2145
Miklos Szeredif9c34672018-01-19 11:39:52 +01002146 tmp = d_alloc_anon(inode->i_sb);
Christoph Hellwig9308a612008-08-11 15:49:12 +02002147 if (!tmp) {
2148 res = ERR_PTR(-ENOMEM);
2149 goto out_iput;
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02002150 }
Nick Pigginb5c84bf2011-01-07 17:49:38 +11002151
Miklos Szeredif9c34672018-01-19 11:39:52 +01002152 return __d_instantiate_anon(tmp, inode, disconnected);
Christoph Hellwig9308a612008-08-11 15:49:12 +02002153
Miklos Szeredif9c34672018-01-19 11:39:52 +01002154out_iput:
Christoph Hellwig9308a612008-08-11 15:49:12 +02002155 iput(inode);
2156 return res;
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02002157}
J. Bruce Fields1a0a3972014-02-14 17:35:37 -05002158
2159/**
2160 * d_obtain_alias - find or allocate a DISCONNECTED dentry for a given inode
2161 * @inode: inode to allocate the dentry for
2162 *
2163 * Obtain a dentry for an inode resulting from NFS filehandle conversion or
2164 * similar open by handle operations. The returned dentry may be anonymous,
2165 * or may have a full name (if the inode was already in the cache).
2166 *
2167 * When called on a directory inode, we must ensure that the inode only ever
2168 * has one dentry. If a dentry is found, that is returned instead of
2169 * allocating a new one.
2170 *
2171 * On successful return, the reference to the inode has been transferred
2172 * to the dentry. In case of an error the reference on the inode is released.
2173 * To make it easier to use in export operations a %NULL or IS_ERR inode may
2174 * be passed in and the error will be propagated to the return value,
2175 * with a %NULL @inode replaced by ERR_PTR(-ESTALE).
2176 */
2177struct dentry *d_obtain_alias(struct inode *inode)
2178{
Miklos Szeredif9c34672018-01-19 11:39:52 +01002179 return __d_obtain_alias(inode, true);
J. Bruce Fields1a0a3972014-02-14 17:35:37 -05002180}
Benny Halevyadc48722009-02-27 14:02:59 -08002181EXPORT_SYMBOL(d_obtain_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182
2183/**
J. Bruce Fields1a0a3972014-02-14 17:35:37 -05002184 * d_obtain_root - find or allocate a dentry for a given inode
2185 * @inode: inode to allocate the dentry for
2186 *
2187 * Obtain an IS_ROOT dentry for the root of a filesystem.
2188 *
2189 * We must ensure that directory inodes only ever have one dentry. If a
2190 * dentry is found, that is returned instead of allocating a new one.
2191 *
2192 * On successful return, the reference to the inode has been transferred
2193 * to the dentry. In case of an error the reference on the inode is
2194 * released. A %NULL or IS_ERR inode may be passed in and will be the
2195 * error will be propagate to the return value, with a %NULL @inode
2196 * replaced by ERR_PTR(-ESTALE).
2197 */
2198struct dentry *d_obtain_root(struct inode *inode)
2199{
Miklos Szeredif9c34672018-01-19 11:39:52 +01002200 return __d_obtain_alias(inode, false);
J. Bruce Fields1a0a3972014-02-14 17:35:37 -05002201}
2202EXPORT_SYMBOL(d_obtain_root);
2203
2204/**
Barry Naujok94035402008-05-21 16:50:46 +10002205 * d_add_ci - lookup or allocate new dentry with case-exact name
2206 * @inode: the inode case-insensitive lookup has found
2207 * @dentry: the negative dentry that was passed to the parent's lookup func
2208 * @name: the case-exact name to be associated with the returned dentry
2209 *
2210 * This is to avoid filling the dcache with case-insensitive names to the
2211 * same inode, only the actual correct case is stored in the dcache for
2212 * case-insensitive filesystems.
2213 *
Randy Dunlap3d742d42021-02-24 12:00:48 -08002214 * For a case-insensitive lookup match and if the case-exact dentry
2215 * already exists in the dcache, use it and return it.
Barry Naujok94035402008-05-21 16:50:46 +10002216 *
2217 * If no entry exists with the exact case name, allocate new dentry with
2218 * the exact case, and return the spliced entry.
2219 */
Christoph Hellwige45b5902008-08-07 23:49:07 +02002220struct dentry *d_add_ci(struct dentry *dentry, struct inode *inode,
Barry Naujok94035402008-05-21 16:50:46 +10002221 struct qstr *name)
2222{
Al Virod9171b92016-04-15 03:33:13 -04002223 struct dentry *found, *res;
Barry Naujok94035402008-05-21 16:50:46 +10002224
Christoph Hellwigb6520c82009-01-05 19:10:37 +01002225 /*
2226 * First check if a dentry matching the name already exists,
2227 * if not go ahead and create it now.
2228 */
Barry Naujok94035402008-05-21 16:50:46 +10002229 found = d_hash_and_lookup(dentry->d_parent, name);
Al Virod9171b92016-04-15 03:33:13 -04002230 if (found) {
2231 iput(inode);
2232 return found;
Barry Naujok94035402008-05-21 16:50:46 +10002233 }
Al Virod9171b92016-04-15 03:33:13 -04002234 if (d_in_lookup(dentry)) {
2235 found = d_alloc_parallel(dentry->d_parent, name,
2236 dentry->d_wait);
2237 if (IS_ERR(found) || !d_in_lookup(found)) {
2238 iput(inode);
2239 return found;
2240 }
2241 } else {
2242 found = d_alloc(dentry->d_parent, name);
2243 if (!found) {
2244 iput(inode);
2245 return ERR_PTR(-ENOMEM);
2246 }
2247 }
2248 res = d_splice_alias(inode, found);
2249 if (res) {
Al Viro40a3cb02022-07-30 00:29:05 -04002250 d_lookup_done(found);
Al Virod9171b92016-04-15 03:33:13 -04002251 dput(found);
2252 return res;
2253 }
Al Viro4f522a22013-02-11 23:20:37 -05002254 return found;
Barry Naujok94035402008-05-21 16:50:46 +10002255}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002256EXPORT_SYMBOL(d_add_ci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257
Xiubo Li4f48d5d2022-05-16 11:23:19 +08002258/**
2259 * d_same_name - compare dentry name with case-exact name
2260 * @parent: parent dentry
2261 * @dentry: the negative dentry that was passed to the parent's lookup func
2262 * @name: the case-exact name to be associated with the returned dentry
2263 *
2264 * Return: true if names are same, or false
2265 */
2266bool d_same_name(const struct dentry *dentry, const struct dentry *parent,
2267 const struct qstr *name)
Linus Torvalds12f8ad42012-05-04 14:59:14 -07002268{
Al Virod4c91a82016-06-25 23:33:49 -04002269 if (likely(!(parent->d_flags & DCACHE_OP_COMPARE))) {
2270 if (dentry->d_name.len != name->len)
2271 return false;
2272 return dentry_cmp(dentry, name->name, name->len) == 0;
Linus Torvalds12f8ad42012-05-04 14:59:14 -07002273 }
Al Viro6fa67e72016-07-31 16:37:25 -04002274 return parent->d_op->d_compare(dentry,
Al Virod4c91a82016-06-25 23:33:49 -04002275 dentry->d_name.len, dentry->d_name.name,
2276 name) == 0;
Linus Torvalds12f8ad42012-05-04 14:59:14 -07002277}
Xiubo Li4f48d5d2022-05-16 11:23:19 +08002278EXPORT_SYMBOL_GPL(d_same_name);
Linus Torvalds12f8ad42012-05-04 14:59:14 -07002279
Linus Torvaldsae2a8232022-08-11 15:29:46 -07002280/*
2281 * This is __d_lookup_rcu() when the parent dentry has
2282 * DCACHE_OP_COMPARE, which makes things much nastier.
2283 */
2284static noinline struct dentry *__d_lookup_rcu_op_compare(
2285 const struct dentry *parent,
2286 const struct qstr *name,
2287 unsigned *seqp)
2288{
2289 u64 hashlen = name->hash_len;
2290 struct hlist_bl_head *b = d_hash(hashlen_hash(hashlen));
2291 struct hlist_bl_node *node;
2292 struct dentry *dentry;
2293
2294 hlist_bl_for_each_entry_rcu(dentry, node, b, d_hash) {
2295 int tlen;
2296 const char *tname;
2297 unsigned seq;
2298
2299seqretry:
2300 seq = raw_seqcount_begin(&dentry->d_seq);
2301 if (dentry->d_parent != parent)
2302 continue;
2303 if (d_unhashed(dentry))
2304 continue;
2305 if (dentry->d_name.hash != hashlen_hash(hashlen))
2306 continue;
2307 tlen = dentry->d_name.len;
2308 tname = dentry->d_name.name;
2309 /* we want a consistent (name,len) pair */
2310 if (read_seqcount_retry(&dentry->d_seq, seq)) {
2311 cpu_relax();
2312 goto seqretry;
2313 }
2314 if (parent->d_op->d_compare(dentry, tlen, tname, name) != 0)
2315 continue;
2316 *seqp = seq;
2317 return dentry;
2318 }
2319 return NULL;
2320}
2321
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322/**
Nick Piggin31e6b012011-01-07 17:49:52 +11002323 * __d_lookup_rcu - search for a dentry (racy, store-free)
2324 * @parent: parent dentry
2325 * @name: qstr of name we wish to find
Randy Dunlap1f1e6e52012-03-18 21:23:05 -07002326 * @seqp: returns d_seq value at the point where the dentry was found
Nick Piggin31e6b012011-01-07 17:49:52 +11002327 * Returns: dentry, or NULL
2328 *
2329 * __d_lookup_rcu is the dcache lookup function for rcu-walk name
2330 * resolution (store-free path walking) design described in
2331 * Documentation/filesystems/path-lookup.txt.
2332 *
2333 * This is not to be used outside core vfs.
2334 *
2335 * __d_lookup_rcu must only be used in rcu-walk mode, ie. with vfsmount lock
2336 * held, and rcu_read_lock held. The returned dentry must not be stored into
2337 * without taking d_lock and checking d_seq sequence count against @seq
2338 * returned here.
2339 *
Linus Torvalds15570082013-09-02 11:38:06 -07002340 * A refcount may be taken on the found dentry with the d_rcu_to_refcount
Nick Piggin31e6b012011-01-07 17:49:52 +11002341 * function.
2342 *
2343 * Alternatively, __d_lookup_rcu may be called again to look up the child of
2344 * the returned dentry, so long as its parent's seqlock is checked after the
2345 * child is looked up. Thus, an interlocking stepping of sequence lock checks
2346 * is formed, giving integrity down the path walk.
Linus Torvalds12f8ad42012-05-04 14:59:14 -07002347 *
2348 * NOTE! The caller *has* to check the resulting dentry against the sequence
2349 * number we've returned before using any of the resulting dentry state!
Nick Piggin31e6b012011-01-07 17:49:52 +11002350 */
Linus Torvalds8966be92012-03-02 14:23:30 -08002351struct dentry *__d_lookup_rcu(const struct dentry *parent,
2352 const struct qstr *name,
Linus Torvaldsda53be12013-05-21 15:22:44 -07002353 unsigned *seqp)
Nick Piggin31e6b012011-01-07 17:49:52 +11002354{
Linus Torvalds26fe5752012-05-10 13:14:12 -07002355 u64 hashlen = name->hash_len;
Nick Piggin31e6b012011-01-07 17:49:52 +11002356 const unsigned char *str = name->name;
Linus Torvalds8387ff22016-06-10 07:51:30 -07002357 struct hlist_bl_head *b = d_hash(hashlen_hash(hashlen));
Nick Pigginceb5bdc2011-01-07 17:50:05 +11002358 struct hlist_bl_node *node;
Nick Piggin31e6b012011-01-07 17:49:52 +11002359 struct dentry *dentry;
2360
2361 /*
2362 * Note: There is significant duplication with __d_lookup_rcu which is
2363 * required to prevent single threaded performance regressions
2364 * especially on architectures where smp_rmb (in seqcounts) are costly.
2365 * Keep the two functions in sync.
2366 */
2367
Linus Torvaldsae2a8232022-08-11 15:29:46 -07002368 if (unlikely(parent->d_flags & DCACHE_OP_COMPARE))
2369 return __d_lookup_rcu_op_compare(parent, name, seqp);
2370
Nick Piggin31e6b012011-01-07 17:49:52 +11002371 /*
2372 * The hash list is protected using RCU.
2373 *
2374 * Carefully use d_seq when comparing a candidate dentry, to avoid
2375 * races with d_move().
2376 *
2377 * It is possible that concurrent renames can mess up our list
2378 * walk here and result in missing our dentry, resulting in the
2379 * false-negative result. d_lookup() protects against concurrent
2380 * renames using rename_lock seqlock.
2381 *
Namhyung Kimb0a4bb82011-01-22 15:31:32 +09002382 * See Documentation/filesystems/path-lookup.txt for more details.
Nick Piggin31e6b012011-01-07 17:49:52 +11002383 */
Linus Torvaldsb07ad992011-04-23 22:32:03 -07002384 hlist_bl_for_each_entry_rcu(dentry, node, b, d_hash) {
Linus Torvalds8966be92012-03-02 14:23:30 -08002385 unsigned seq;
Nick Piggin31e6b012011-01-07 17:49:52 +11002386
Linus Torvalds12f8ad42012-05-04 14:59:14 -07002387 /*
2388 * The dentry sequence count protects us from concurrent
Linus Torvaldsda53be12013-05-21 15:22:44 -07002389 * renames, and thus protects parent and name fields.
Linus Torvalds12f8ad42012-05-04 14:59:14 -07002390 *
2391 * The caller must perform a seqcount check in order
Linus Torvaldsda53be12013-05-21 15:22:44 -07002392 * to do anything useful with the returned dentry.
Linus Torvalds12f8ad42012-05-04 14:59:14 -07002393 *
2394 * NOTE! We do a "raw" seqcount_begin here. That means that
2395 * we don't wait for the sequence count to stabilize if it
2396 * is in the middle of a sequence change. If we do the slow
2397 * dentry compare, we will do seqretries until it is stable,
2398 * and if we end up with a successful lookup, we actually
2399 * want to exit RCU lookup anyway.
Al Virod4c91a82016-06-25 23:33:49 -04002400 *
2401 * Note that raw_seqcount_begin still *does* smp_rmb(), so
2402 * we are still guaranteed NUL-termination of ->d_name.name.
Linus Torvalds12f8ad42012-05-04 14:59:14 -07002403 */
2404 seq = raw_seqcount_begin(&dentry->d_seq);
Nick Piggin31e6b012011-01-07 17:49:52 +11002405 if (dentry->d_parent != parent)
2406 continue;
Linus Torvalds2e321802012-05-21 18:48:10 -07002407 if (d_unhashed(dentry))
2408 continue;
Linus Torvaldsae2a8232022-08-11 15:29:46 -07002409 if (dentry->d_name.hash_len != hashlen)
2410 continue;
2411 if (dentry_cmp(dentry, str, hashlen_len(hashlen)) != 0)
2412 continue;
Linus Torvaldsda53be12013-05-21 15:22:44 -07002413 *seqp = seq;
Al Virod4c91a82016-06-25 23:33:49 -04002414 return dentry;
Nick Piggin31e6b012011-01-07 17:49:52 +11002415 }
2416 return NULL;
2417}
2418
2419/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 * d_lookup - search for a dentry
2421 * @parent: parent dentry
2422 * @name: qstr of name we wish to find
Nick Pigginb04f7842010-08-18 04:37:34 +10002423 * Returns: dentry, or NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 *
Nick Pigginb04f7842010-08-18 04:37:34 +10002425 * d_lookup searches the children of the parent dentry for the name in
2426 * question. If the dentry is found its reference count is incremented and the
2427 * dentry is returned. The caller must use dput to free the entry when it has
2428 * finished using it. %NULL is returned if the dentry does not exist.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 */
Al Viroda2d8452013-01-24 18:29:34 -05002430struct dentry *d_lookup(const struct dentry *parent, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431{
Nick Piggin31e6b012011-01-07 17:49:52 +11002432 struct dentry *dentry;
Nick Piggin949854d2011-01-07 17:49:37 +11002433 unsigned seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434
Daeseok Younb8314f92014-08-11 11:46:53 +09002435 do {
2436 seq = read_seqbegin(&rename_lock);
2437 dentry = __d_lookup(parent, name);
2438 if (dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 break;
2440 } while (read_seqretry(&rename_lock, seq));
2441 return dentry;
2442}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002443EXPORT_SYMBOL(d_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444
Nick Piggin31e6b012011-01-07 17:49:52 +11002445/**
Nick Pigginb04f7842010-08-18 04:37:34 +10002446 * __d_lookup - search for a dentry (racy)
2447 * @parent: parent dentry
2448 * @name: qstr of name we wish to find
2449 * Returns: dentry, or NULL
2450 *
2451 * __d_lookup is like d_lookup, however it may (rarely) return a
2452 * false-negative result due to unrelated rename activity.
2453 *
2454 * __d_lookup is slightly faster by avoiding rename_lock read seqlock,
2455 * however it must be used carefully, eg. with a following d_lookup in
2456 * the case of failure.
2457 *
2458 * __d_lookup callers must be commented.
2459 */
Al Viroa713ca2a2013-01-24 18:27:00 -05002460struct dentry *__d_lookup(const struct dentry *parent, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 unsigned int hash = name->hash;
Linus Torvalds8387ff22016-06-10 07:51:30 -07002463 struct hlist_bl_head *b = d_hash(hash);
Nick Pigginceb5bdc2011-01-07 17:50:05 +11002464 struct hlist_bl_node *node;
Nick Piggin31e6b012011-01-07 17:49:52 +11002465 struct dentry *found = NULL;
Paul E. McKenney665a7582005-11-07 00:59:17 -08002466 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467
Nick Pigginb04f7842010-08-18 04:37:34 +10002468 /*
Nick Piggin31e6b012011-01-07 17:49:52 +11002469 * Note: There is significant duplication with __d_lookup_rcu which is
2470 * required to prevent single threaded performance regressions
2471 * especially on architectures where smp_rmb (in seqcounts) are costly.
2472 * Keep the two functions in sync.
2473 */
2474
2475 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10002476 * The hash list is protected using RCU.
2477 *
2478 * Take d_lock when comparing a candidate dentry, to avoid races
2479 * with d_move().
2480 *
2481 * It is possible that concurrent renames can mess up our list
2482 * walk here and result in missing our dentry, resulting in the
2483 * false-negative result. d_lookup() protects against concurrent
2484 * renames using rename_lock seqlock.
2485 *
Namhyung Kimb0a4bb82011-01-22 15:31:32 +09002486 * See Documentation/filesystems/path-lookup.txt for more details.
Nick Pigginb04f7842010-08-18 04:37:34 +10002487 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 rcu_read_lock();
2489
Linus Torvaldsb07ad992011-04-23 22:32:03 -07002490 hlist_bl_for_each_entry_rcu(dentry, node, b, d_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 if (dentry->d_name.hash != hash)
2493 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494
2495 spin_lock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 if (dentry->d_parent != parent)
2497 goto next;
Linus Torvaldsd0185c02008-09-29 07:42:57 -07002498 if (d_unhashed(dentry))
2499 goto next;
2500
Al Virod4c91a82016-06-25 23:33:49 -04002501 if (!d_same_name(dentry, parent, name))
2502 goto next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503
Waiman Long98474232013-08-28 18:24:59 -07002504 dentry->d_lockref.count++;
Linus Torvaldsd0185c02008-09-29 07:42:57 -07002505 found = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 spin_unlock(&dentry->d_lock);
2507 break;
2508next:
2509 spin_unlock(&dentry->d_lock);
2510 }
2511 rcu_read_unlock();
2512
2513 return found;
2514}
2515
2516/**
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08002517 * d_hash_and_lookup - hash the qstr then search for a dentry
2518 * @dir: Directory to search in
2519 * @name: qstr of name we wish to find
2520 *
Al Viro4f522a22013-02-11 23:20:37 -05002521 * On lookup failure NULL is returned; on bad name - ERR_PTR(-error)
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08002522 */
2523struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name)
2524{
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08002525 /*
2526 * Check for a fs-specific hash function. Note that we must
2527 * calculate the standard hash first, as the d_op->d_hash()
2528 * routine may choose to leave the hash value unchanged.
2529 */
Linus Torvalds8387ff22016-06-10 07:51:30 -07002530 name->hash = full_name_hash(dir, name->name, name->len);
Nick Pigginfb045ad2011-01-07 17:49:55 +11002531 if (dir->d_flags & DCACHE_OP_HASH) {
Linus Torvaldsda53be12013-05-21 15:22:44 -07002532 int err = dir->d_op->d_hash(dir, name);
Al Viro4f522a22013-02-11 23:20:37 -05002533 if (unlikely(err < 0))
2534 return ERR_PTR(err);
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08002535 }
Al Viro4f522a22013-02-11 23:20:37 -05002536 return d_lookup(dir, name);
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08002537}
Al Viro4f522a22013-02-11 23:20:37 -05002538EXPORT_SYMBOL(d_hash_and_lookup);
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08002539
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540/*
2541 * When a file is deleted, we have two options:
2542 * - turn this dentry into a negative dentry
2543 * - unhash this dentry and free it.
2544 *
2545 * Usually, we want to just turn this into
2546 * a negative dentry, but if anybody else is
2547 * currently using the dentry or the inode
2548 * we can't do that and we fall back on removing
2549 * it from the hash queues and waiting for
2550 * it to be deleted later when it has no users
2551 */
2552
2553/**
2554 * d_delete - delete a dentry
2555 * @dentry: The dentry to delete
2556 *
2557 * Turn the dentry into a negative dentry if possible, otherwise
2558 * remove it from the hash queues so it can be deleted later
2559 */
2560
2561void d_delete(struct dentry * dentry)
2562{
Al Viroc19457f2018-02-23 21:02:31 -05002563 struct inode *inode = dentry->d_inode;
Al Viroc19457f2018-02-23 21:02:31 -05002564
2565 spin_lock(&inode->i_lock);
2566 spin_lock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 /*
2568 * Are we the only user?
2569 */
Waiman Long98474232013-08-28 18:24:59 -07002570 if (dentry->d_lockref.count == 1) {
Al Viro13e3c5e2010-05-21 16:11:04 -04002571 dentry->d_flags &= ~DCACHE_CANT_MOUNT;
Nick Piggin31e6b012011-01-07 17:49:52 +11002572 dentry_unlink_inode(dentry);
Al Viroc19457f2018-02-23 21:02:31 -05002573 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 __d_drop(dentry);
Al Viroc19457f2018-02-23 21:02:31 -05002575 spin_unlock(&dentry->d_lock);
2576 spin_unlock(&inode->i_lock);
2577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002579EXPORT_SYMBOL(d_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580
Al Viro15d3c582016-07-29 17:45:21 -04002581static void __d_rehash(struct dentry *entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582{
Al Viro15d3c582016-07-29 17:45:21 -04002583 struct hlist_bl_head *b = d_hash(entry->d_name.hash);
NeilBrown61647822017-11-10 15:45:41 +11002584
Christoph Hellwig1879fd62011-04-25 14:01:36 -04002585 hlist_bl_lock(b);
Linus Torvaldsb07ad992011-04-23 22:32:03 -07002586 hlist_bl_add_head_rcu(&entry->d_hash, b);
Christoph Hellwig1879fd62011-04-25 14:01:36 -04002587 hlist_bl_unlock(b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588}
2589
2590/**
2591 * d_rehash - add an entry back to the hash
2592 * @entry: dentry to add to the hash
2593 *
2594 * Adds a dentry to the hash according to its name.
2595 */
2596
2597void d_rehash(struct dentry * entry)
2598{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 spin_lock(&entry->d_lock);
Al Viro15d3c582016-07-29 17:45:21 -04002600 __d_rehash(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 spin_unlock(&entry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002603EXPORT_SYMBOL(d_rehash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604
Al Viro84e710d2016-04-15 00:58:55 -04002605static inline unsigned start_dir_add(struct inode *dir)
2606{
Thomas Gleixner93f6d4e2022-08-25 18:41:25 +02002607 preempt_disable_nested();
Al Viro84e710d2016-04-15 00:58:55 -04002608 for (;;) {
2609 unsigned n = dir->i_dir_seq;
2610 if (!(n & 1) && cmpxchg(&dir->i_dir_seq, n, n + 1) == n)
2611 return n;
2612 cpu_relax();
2613 }
2614}
2615
Sebastian Andrzej Siewior50417d22022-07-27 13:49:04 +02002616static inline void end_dir_add(struct inode *dir, unsigned int n,
2617 wait_queue_head_t *d_wait)
Al Viro84e710d2016-04-15 00:58:55 -04002618{
2619 smp_store_release(&dir->i_dir_seq, n + 2);
Thomas Gleixner93f6d4e2022-08-25 18:41:25 +02002620 preempt_enable_nested();
Sebastian Andrzej Siewior50417d22022-07-27 13:49:04 +02002621 wake_up_all(d_wait);
Al Viro84e710d2016-04-15 00:58:55 -04002622}
2623
Al Virod9171b92016-04-15 03:33:13 -04002624static void d_wait_lookup(struct dentry *dentry)
2625{
2626 if (d_in_lookup(dentry)) {
2627 DECLARE_WAITQUEUE(wait, current);
2628 add_wait_queue(dentry->d_wait, &wait);
2629 do {
2630 set_current_state(TASK_UNINTERRUPTIBLE);
2631 spin_unlock(&dentry->d_lock);
2632 schedule();
2633 spin_lock(&dentry->d_lock);
2634 } while (d_in_lookup(dentry));
2635 }
2636}
2637
Al Viro94bdd652016-04-15 02:42:04 -04002638struct dentry *d_alloc_parallel(struct dentry *parent,
Al Virod9171b92016-04-15 03:33:13 -04002639 const struct qstr *name,
2640 wait_queue_head_t *wq)
Al Viro94bdd652016-04-15 02:42:04 -04002641{
Al Viro94bdd652016-04-15 02:42:04 -04002642 unsigned int hash = name->hash;
Al Viro94bdd652016-04-15 02:42:04 -04002643 struct hlist_bl_head *b = in_lookup_hash(parent, hash);
2644 struct hlist_bl_node *node;
2645 struct dentry *new = d_alloc(parent, name);
2646 struct dentry *dentry;
2647 unsigned seq, r_seq, d_seq;
2648
2649 if (unlikely(!new))
2650 return ERR_PTR(-ENOMEM);
2651
2652retry:
2653 rcu_read_lock();
Will Deacon015555f2018-02-19 14:55:54 +00002654 seq = smp_load_acquire(&parent->d_inode->i_dir_seq);
Al Viro94bdd652016-04-15 02:42:04 -04002655 r_seq = read_seqbegin(&rename_lock);
2656 dentry = __d_lookup_rcu(parent, name, &d_seq);
2657 if (unlikely(dentry)) {
2658 if (!lockref_get_not_dead(&dentry->d_lockref)) {
2659 rcu_read_unlock();
2660 goto retry;
2661 }
2662 if (read_seqcount_retry(&dentry->d_seq, d_seq)) {
2663 rcu_read_unlock();
2664 dput(dentry);
2665 goto retry;
2666 }
2667 rcu_read_unlock();
2668 dput(new);
2669 return dentry;
2670 }
2671 if (unlikely(read_seqretry(&rename_lock, r_seq))) {
2672 rcu_read_unlock();
2673 goto retry;
2674 }
Will Deacon015555f2018-02-19 14:55:54 +00002675
2676 if (unlikely(seq & 1)) {
2677 rcu_read_unlock();
2678 goto retry;
2679 }
2680
Al Viro94bdd652016-04-15 02:42:04 -04002681 hlist_bl_lock(b);
Will Deacon8cc07c82018-02-19 14:55:55 +00002682 if (unlikely(READ_ONCE(parent->d_inode->i_dir_seq) != seq)) {
Al Viro94bdd652016-04-15 02:42:04 -04002683 hlist_bl_unlock(b);
2684 rcu_read_unlock();
2685 goto retry;
2686 }
Al Viro94bdd652016-04-15 02:42:04 -04002687 /*
2688 * No changes for the parent since the beginning of d_lookup().
2689 * Since all removals from the chain happen with hlist_bl_lock(),
2690 * any potential in-lookup matches are going to stay here until
2691 * we unlock the chain. All fields are stable in everything
2692 * we encounter.
2693 */
2694 hlist_bl_for_each_entry(dentry, node, b, d_u.d_in_lookup_hash) {
2695 if (dentry->d_name.hash != hash)
2696 continue;
2697 if (dentry->d_parent != parent)
2698 continue;
Al Virod4c91a82016-06-25 23:33:49 -04002699 if (!d_same_name(dentry, parent, name))
2700 continue;
Al Viro94bdd652016-04-15 02:42:04 -04002701 hlist_bl_unlock(b);
Al Viroe7d6ef92016-06-20 01:35:59 -04002702 /* now we can try to grab a reference */
2703 if (!lockref_get_not_dead(&dentry->d_lockref)) {
2704 rcu_read_unlock();
2705 goto retry;
2706 }
2707
2708 rcu_read_unlock();
2709 /*
2710 * somebody is likely to be still doing lookup for it;
2711 * wait for them to finish
2712 */
Al Virod9171b92016-04-15 03:33:13 -04002713 spin_lock(&dentry->d_lock);
2714 d_wait_lookup(dentry);
2715 /*
2716 * it's not in-lookup anymore; in principle we should repeat
2717 * everything from dcache lookup, but it's likely to be what
2718 * d_lookup() would've found anyway. If it is, just return it;
2719 * otherwise we really have to repeat the whole thing.
2720 */
2721 if (unlikely(dentry->d_name.hash != hash))
2722 goto mismatch;
2723 if (unlikely(dentry->d_parent != parent))
2724 goto mismatch;
2725 if (unlikely(d_unhashed(dentry)))
2726 goto mismatch;
Al Virod4c91a82016-06-25 23:33:49 -04002727 if (unlikely(!d_same_name(dentry, parent, name)))
2728 goto mismatch;
Al Virod9171b92016-04-15 03:33:13 -04002729 /* OK, it *is* a hashed match; return it */
2730 spin_unlock(&dentry->d_lock);
Al Viro94bdd652016-04-15 02:42:04 -04002731 dput(new);
2732 return dentry;
2733 }
Al Viroe7d6ef92016-06-20 01:35:59 -04002734 rcu_read_unlock();
Al Viro94bdd652016-04-15 02:42:04 -04002735 /* we can't take ->d_lock here; it's OK, though. */
2736 new->d_flags |= DCACHE_PAR_LOOKUP;
Al Virod9171b92016-04-15 03:33:13 -04002737 new->d_wait = wq;
Al Viro94bdd652016-04-15 02:42:04 -04002738 hlist_bl_add_head_rcu(&new->d_u.d_in_lookup_hash, b);
2739 hlist_bl_unlock(b);
2740 return new;
Al Virod9171b92016-04-15 03:33:13 -04002741mismatch:
2742 spin_unlock(&dentry->d_lock);
2743 dput(dentry);
2744 goto retry;
Al Viro94bdd652016-04-15 02:42:04 -04002745}
2746EXPORT_SYMBOL(d_alloc_parallel);
2747
Sebastian Andrzej Siewior45f78b02022-07-27 13:49:03 +02002748/*
2749 * - Unhash the dentry
2750 * - Retrieve and clear the waitqueue head in dentry
2751 * - Return the waitqueue head
2752 */
2753static wait_queue_head_t *__d_lookup_unhash(struct dentry *dentry)
Al Viro85c7f812016-04-14 19:52:13 -04002754{
Sebastian Andrzej Siewior45f78b02022-07-27 13:49:03 +02002755 wait_queue_head_t *d_wait;
2756 struct hlist_bl_head *b;
2757
2758 lockdep_assert_held(&dentry->d_lock);
2759
2760 b = in_lookup_hash(dentry->d_parent, dentry->d_name.hash);
Al Viro94bdd652016-04-15 02:42:04 -04002761 hlist_bl_lock(b);
Al Viro85c7f812016-04-14 19:52:13 -04002762 dentry->d_flags &= ~DCACHE_PAR_LOOKUP;
Al Viro94bdd652016-04-15 02:42:04 -04002763 __hlist_bl_del(&dentry->d_u.d_in_lookup_hash);
Sebastian Andrzej Siewior45f78b02022-07-27 13:49:03 +02002764 d_wait = dentry->d_wait;
Al Virod9171b92016-04-15 03:33:13 -04002765 dentry->d_wait = NULL;
Al Viro94bdd652016-04-15 02:42:04 -04002766 hlist_bl_unlock(b);
2767 INIT_HLIST_NODE(&dentry->d_u.d_alias);
Al Virod9171b92016-04-15 03:33:13 -04002768 INIT_LIST_HEAD(&dentry->d_lru);
Sebastian Andrzej Siewior45f78b02022-07-27 13:49:03 +02002769 return d_wait;
Al Viro85c7f812016-04-14 19:52:13 -04002770}
Sebastian Andrzej Siewior45f78b02022-07-27 13:49:03 +02002771
2772void __d_lookup_unhash_wake(struct dentry *dentry)
2773{
2774 spin_lock(&dentry->d_lock);
2775 wake_up_all(__d_lookup_unhash(dentry));
2776 spin_unlock(&dentry->d_lock);
2777}
2778EXPORT_SYMBOL(__d_lookup_unhash_wake);
Al Viroed782b52016-03-09 19:52:39 -05002779
2780/* inode->i_lock held if inode is non-NULL */
2781
2782static inline void __d_add(struct dentry *dentry, struct inode *inode)
2783{
Sebastian Andrzej Siewior45f78b02022-07-27 13:49:03 +02002784 wait_queue_head_t *d_wait;
Al Viro84e710d2016-04-15 00:58:55 -04002785 struct inode *dir = NULL;
2786 unsigned n;
Al Viro0568d702016-04-14 19:40:56 -04002787 spin_lock(&dentry->d_lock);
Al Viro84e710d2016-04-15 00:58:55 -04002788 if (unlikely(d_in_lookup(dentry))) {
2789 dir = dentry->d_parent->d_inode;
2790 n = start_dir_add(dir);
Sebastian Andrzej Siewior45f78b02022-07-27 13:49:03 +02002791 d_wait = __d_lookup_unhash(dentry);
Al Viro84e710d2016-04-15 00:58:55 -04002792 }
Al Viroed782b52016-03-09 19:52:39 -05002793 if (inode) {
Al Viro0568d702016-04-14 19:40:56 -04002794 unsigned add_flags = d_flags_for_inode(inode);
2795 hlist_add_head(&dentry->d_u.d_alias, &inode->i_dentry);
2796 raw_write_seqcount_begin(&dentry->d_seq);
2797 __d_set_inode_and_type(dentry, inode, add_flags);
2798 raw_write_seqcount_end(&dentry->d_seq);
Al Viroaffda482016-05-29 18:35:12 -04002799 fsnotify_update_flags(dentry);
Al Viroed782b52016-03-09 19:52:39 -05002800 }
Al Viro15d3c582016-07-29 17:45:21 -04002801 __d_rehash(dentry);
Al Viro84e710d2016-04-15 00:58:55 -04002802 if (dir)
Sebastian Andrzej Siewior50417d22022-07-27 13:49:04 +02002803 end_dir_add(dir, n, d_wait);
Al Viro0568d702016-04-14 19:40:56 -04002804 spin_unlock(&dentry->d_lock);
2805 if (inode)
2806 spin_unlock(&inode->i_lock);
Al Viroed782b52016-03-09 19:52:39 -05002807}
2808
Nick Pigginfb2d5b82011-01-07 17:49:26 +11002809/**
Al Viro34d0d192016-03-08 21:01:03 -05002810 * d_add - add dentry to hash queues
2811 * @entry: dentry to add
2812 * @inode: The inode to attach to this dentry
2813 *
2814 * This adds the entry to the hash queues and initializes @inode.
2815 * The entry was actually filled in earlier during d_alloc().
2816 */
2817
2818void d_add(struct dentry *entry, struct inode *inode)
2819{
Al Virob9680912016-04-11 00:53:26 -04002820 if (inode) {
2821 security_d_instantiate(entry, inode);
Al Viroed782b52016-03-09 19:52:39 -05002822 spin_lock(&inode->i_lock);
Al Virob9680912016-04-11 00:53:26 -04002823 }
Al Viroed782b52016-03-09 19:52:39 -05002824 __d_add(entry, inode);
Al Viro34d0d192016-03-08 21:01:03 -05002825}
2826EXPORT_SYMBOL(d_add);
2827
2828/**
Al Viro668d0cd2016-03-08 12:44:17 -05002829 * d_exact_alias - find and hash an exact unhashed alias
2830 * @entry: dentry to add
2831 * @inode: The inode to go with this dentry
2832 *
2833 * If an unhashed dentry with the same name/parent and desired
2834 * inode already exists, hash and return it. Otherwise, return
2835 * NULL.
2836 *
2837 * Parent directory should be locked.
2838 */
2839struct dentry *d_exact_alias(struct dentry *entry, struct inode *inode)
2840{
2841 struct dentry *alias;
Al Viro668d0cd2016-03-08 12:44:17 -05002842 unsigned int hash = entry->d_name.hash;
2843
2844 spin_lock(&inode->i_lock);
2845 hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
2846 /*
2847 * Don't need alias->d_lock here, because aliases with
2848 * d_parent == entry->d_parent are not subject to name or
2849 * parent changes, because the parent inode i_mutex is held.
2850 */
2851 if (alias->d_name.hash != hash)
2852 continue;
2853 if (alias->d_parent != entry->d_parent)
2854 continue;
Al Virod4c91a82016-06-25 23:33:49 -04002855 if (!d_same_name(alias, entry->d_parent, &entry->d_name))
Al Viro668d0cd2016-03-08 12:44:17 -05002856 continue;
2857 spin_lock(&alias->d_lock);
2858 if (!d_unhashed(alias)) {
2859 spin_unlock(&alias->d_lock);
2860 alias = NULL;
2861 } else {
2862 __dget_dlock(alias);
Al Viro15d3c582016-07-29 17:45:21 -04002863 __d_rehash(alias);
Al Viro668d0cd2016-03-08 12:44:17 -05002864 spin_unlock(&alias->d_lock);
2865 }
2866 spin_unlock(&inode->i_lock);
2867 return alias;
2868 }
2869 spin_unlock(&inode->i_lock);
2870 return NULL;
2871}
2872EXPORT_SYMBOL(d_exact_alias);
2873
Al Viro8d85b482014-09-29 14:54:27 -04002874static void swap_names(struct dentry *dentry, struct dentry *target)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875{
Al Viro8d85b482014-09-29 14:54:27 -04002876 if (unlikely(dname_external(target))) {
2877 if (unlikely(dname_external(dentry))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 /*
2879 * Both external: swap the pointers
2880 */
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08002881 swap(target->d_name.name, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 } else {
2883 /*
2884 * dentry:internal, target:external. Steal target's
2885 * storage and make target internal.
2886 */
J. Bruce Fields321bcf92007-10-21 16:41:38 -07002887 memcpy(target->d_iname, dentry->d_name.name,
2888 dentry->d_name.len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 dentry->d_name.name = target->d_name.name;
2890 target->d_name.name = target->d_iname;
2891 }
2892 } else {
Al Viro8d85b482014-09-29 14:54:27 -04002893 if (unlikely(dname_external(dentry))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 /*
2895 * dentry:external, target:internal. Give dentry's
2896 * storage to target and make dentry internal
2897 */
2898 memcpy(dentry->d_iname, target->d_name.name,
2899 target->d_name.len + 1);
2900 target->d_name.name = dentry->d_name.name;
2901 dentry->d_name.name = dentry->d_iname;
2902 } else {
2903 /*
Miklos Szeredida1ce062014-04-01 17:08:43 +02002904 * Both are internal.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 */
Miklos Szeredida1ce062014-04-01 17:08:43 +02002906 unsigned int i;
2907 BUILD_BUG_ON(!IS_ALIGNED(DNAME_INLINE_LEN, sizeof(long)));
2908 for (i = 0; i < DNAME_INLINE_LEN / sizeof(long); i++) {
2909 swap(((long *) &dentry->d_iname)[i],
2910 ((long *) &target->d_iname)[i]);
2911 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 }
2913 }
Linus Torvaldsa28ddb82014-09-24 12:27:39 -07002914 swap(dentry->d_name.hash_len, target->d_name.hash_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915}
2916
Al Viro8d85b482014-09-29 14:54:27 -04002917static void copy_name(struct dentry *dentry, struct dentry *target)
2918{
2919 struct external_name *old_name = NULL;
2920 if (unlikely(dname_external(dentry)))
2921 old_name = external_name(dentry);
2922 if (unlikely(dname_external(target))) {
2923 atomic_inc(&external_name(target)->u.count);
2924 dentry->d_name = target->d_name;
2925 } else {
2926 memcpy(dentry->d_iname, target->d_name.name,
2927 target->d_name.len + 1);
2928 dentry->d_name.name = dentry->d_iname;
2929 dentry->d_name.hash_len = target->d_name.hash_len;
2930 }
2931 if (old_name && likely(atomic_dec_and_test(&old_name->u.count)))
Vlastimil Babka2e03b4b2018-10-26 15:05:41 -07002932 kfree_rcu(old_name, u.head);
Al Viro8d85b482014-09-29 14:54:27 -04002933}
2934
Trond Myklebust9eaef272006-10-21 10:24:20 -07002935/*
Al Viro18367502011-07-12 21:42:24 -04002936 * __d_move - move a dentry
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 * @dentry: entry to move
2938 * @target: new dentry
Miklos Szeredida1ce062014-04-01 17:08:43 +02002939 * @exchange: exchange the two dentries
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 *
2941 * Update the dcache to reflect the move of a file name. Negative
Jeff Laytonc46c8872011-07-26 13:33:16 -04002942 * dcache entries should not be moved in this way. Caller must hold
2943 * rename_lock, the i_mutex of the source and target directories,
2944 * and the sb->s_vfs_rename_mutex if they differ. See lock_rename().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 */
Miklos Szeredida1ce062014-04-01 17:08:43 +02002946static void __d_move(struct dentry *dentry, struct dentry *target,
2947 bool exchange)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948{
Al Viro42177002018-03-11 15:15:46 -04002949 struct dentry *old_parent, *p;
Sebastian Andrzej Siewior45f78b02022-07-27 13:49:03 +02002950 wait_queue_head_t *d_wait;
Al Viro84e710d2016-04-15 00:58:55 -04002951 struct inode *dir = NULL;
2952 unsigned n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953
Al Viro42177002018-03-11 15:15:46 -04002954 WARN_ON(!dentry->d_inode);
2955 if (WARN_ON(dentry == target))
2956 return;
2957
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002958 BUG_ON(d_ancestor(target, dentry));
Al Viro42177002018-03-11 15:15:46 -04002959 old_parent = dentry->d_parent;
2960 p = d_ancestor(old_parent, target);
2961 if (IS_ROOT(dentry)) {
2962 BUG_ON(p);
2963 spin_lock(&target->d_parent->d_lock);
2964 } else if (!p) {
2965 /* target is not a descendent of dentry->d_parent */
2966 spin_lock(&target->d_parent->d_lock);
2967 spin_lock_nested(&old_parent->d_lock, DENTRY_D_LOCK_NESTED);
2968 } else {
2969 BUG_ON(p == dentry);
2970 spin_lock(&old_parent->d_lock);
2971 if (p != target)
2972 spin_lock_nested(&target->d_parent->d_lock,
2973 DENTRY_D_LOCK_NESTED);
2974 }
2975 spin_lock_nested(&dentry->d_lock, 2);
2976 spin_lock_nested(&target->d_lock, 3);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002977
Al Viro84e710d2016-04-15 00:58:55 -04002978 if (unlikely(d_in_lookup(target))) {
2979 dir = target->d_parent->d_inode;
2980 n = start_dir_add(dir);
Sebastian Andrzej Siewior45f78b02022-07-27 13:49:03 +02002981 d_wait = __d_lookup_unhash(target);
Al Viro84e710d2016-04-15 00:58:55 -04002982 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983
Nick Piggin31e6b012011-01-07 17:49:52 +11002984 write_seqcount_begin(&dentry->d_seq);
John Stultz1ca7d672013-10-07 15:51:59 -07002985 write_seqcount_begin_nested(&target->d_seq, DENTRY_D_LOCK_NESTED);
Nick Piggin31e6b012011-01-07 17:49:52 +11002986
Al Viro15d3c582016-07-29 17:45:21 -04002987 /* unhash both */
Al Viro0632a9a2018-03-07 00:49:10 -05002988 if (!d_unhashed(dentry))
2989 ___d_drop(dentry);
2990 if (!d_unhashed(target))
2991 ___d_drop(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992
Al Viro63cf4272014-09-26 23:06:14 -04002993 /* ... and switch them in the tree */
Al Viro076515f2018-03-10 23:15:52 -05002994 dentry->d_parent = target->d_parent;
2995 if (!exchange) {
2996 copy_name(dentry, target);
2997 target->d_hash.pprev = NULL;
2998 dentry->d_parent->d_lockref.count++;
Al Viro5467a682019-03-15 22:23:19 -04002999 if (dentry != old_parent) /* wasn't IS_ROOT */
Al Viro076515f2018-03-10 23:15:52 -05003000 WARN_ON(!--old_parent->d_lockref.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 } else {
Al Viro076515f2018-03-10 23:15:52 -05003002 target->d_parent = old_parent;
3003 swap_names(dentry, target);
Al Viro946e51f2014-10-26 19:19:16 -04003004 list_move(&target->d_child, &target->d_parent->d_subdirs);
Al Viro076515f2018-03-10 23:15:52 -05003005 __d_rehash(target);
3006 fsnotify_update_flags(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 }
Al Viro076515f2018-03-10 23:15:52 -05003008 list_move(&dentry->d_child, &dentry->d_parent->d_subdirs);
3009 __d_rehash(dentry);
3010 fsnotify_update_flags(dentry);
Eric Biggers0bf3d5c12019-03-20 11:39:11 -07003011 fscrypt_handle_d_move(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012
Nick Piggin31e6b012011-01-07 17:49:52 +11003013 write_seqcount_end(&target->d_seq);
3014 write_seqcount_end(&dentry->d_seq);
3015
Al Viro84e710d2016-04-15 00:58:55 -04003016 if (dir)
Sebastian Andrzej Siewior50417d22022-07-27 13:49:04 +02003017 end_dir_add(dir, n, d_wait);
Al Viro076515f2018-03-10 23:15:52 -05003018
3019 if (dentry->d_parent != old_parent)
3020 spin_unlock(&dentry->d_parent->d_lock);
3021 if (dentry != old_parent)
3022 spin_unlock(&old_parent->d_lock);
3023 spin_unlock(&target->d_lock);
3024 spin_unlock(&dentry->d_lock);
Al Viro18367502011-07-12 21:42:24 -04003025}
3026
3027/*
3028 * d_move - move a dentry
3029 * @dentry: entry to move
3030 * @target: new dentry
3031 *
3032 * Update the dcache to reflect the move of a file name. Negative
Jeff Laytonc46c8872011-07-26 13:33:16 -04003033 * dcache entries should not be moved in this way. See the locking
3034 * requirements for __d_move.
Al Viro18367502011-07-12 21:42:24 -04003035 */
3036void d_move(struct dentry *dentry, struct dentry *target)
3037{
3038 write_seqlock(&rename_lock);
Miklos Szeredida1ce062014-04-01 17:08:43 +02003039 __d_move(dentry, target, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 write_sequnlock(&rename_lock);
Trond Myklebust9eaef272006-10-21 10:24:20 -07003041}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07003042EXPORT_SYMBOL(d_move);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043
Miklos Szeredida1ce062014-04-01 17:08:43 +02003044/*
3045 * d_exchange - exchange two dentries
3046 * @dentry1: first dentry
3047 * @dentry2: second dentry
3048 */
3049void d_exchange(struct dentry *dentry1, struct dentry *dentry2)
3050{
3051 write_seqlock(&rename_lock);
3052
3053 WARN_ON(!dentry1->d_inode);
3054 WARN_ON(!dentry2->d_inode);
3055 WARN_ON(IS_ROOT(dentry1));
3056 WARN_ON(IS_ROOT(dentry2));
3057
3058 __d_move(dentry1, dentry2, true);
3059
3060 write_sequnlock(&rename_lock);
3061}
3062
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09003063/**
3064 * d_ancestor - search for an ancestor
3065 * @p1: ancestor dentry
3066 * @p2: child dentry
3067 *
3068 * Returns the ancestor dentry of p2 which is a child of p1, if p1 is
3069 * an ancestor of p2, else NULL.
Trond Myklebust9eaef272006-10-21 10:24:20 -07003070 */
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09003071struct dentry *d_ancestor(struct dentry *p1, struct dentry *p2)
Trond Myklebust9eaef272006-10-21 10:24:20 -07003072{
3073 struct dentry *p;
3074
OGAWA Hirofumi871c0062008-10-16 07:50:27 +09003075 for (p = p2; !IS_ROOT(p); p = p->d_parent) {
Trond Myklebust9eaef272006-10-21 10:24:20 -07003076 if (p->d_parent == p1)
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09003077 return p;
Trond Myklebust9eaef272006-10-21 10:24:20 -07003078 }
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09003079 return NULL;
Trond Myklebust9eaef272006-10-21 10:24:20 -07003080}
3081
3082/*
3083 * This helper attempts to cope with remotely renamed directories
3084 *
3085 * It assumes that the caller is already holding
Eric W. Biedermana03e2832015-08-15 13:36:41 -05003086 * dentry->d_parent->d_inode->i_mutex, and rename_lock
Trond Myklebust9eaef272006-10-21 10:24:20 -07003087 *
3088 * Note: If ever the locking in lock_rename() changes, then please
3089 * remember to update this too...
Trond Myklebust9eaef272006-10-21 10:24:20 -07003090 */
Al Virob5ae6b12014-10-12 22:16:02 -04003091static int __d_unalias(struct inode *inode,
Nick Piggin873feea2011-01-07 17:50:06 +11003092 struct dentry *dentry, struct dentry *alias)
Trond Myklebust9eaef272006-10-21 10:24:20 -07003093{
Al Viro9902af72016-04-15 15:08:36 -04003094 struct mutex *m1 = NULL;
3095 struct rw_semaphore *m2 = NULL;
J. Bruce Fields3d330dc2015-02-10 10:55:53 -05003096 int ret = -ESTALE;
Trond Myklebust9eaef272006-10-21 10:24:20 -07003097
3098 /* If alias and dentry share a parent, then no extra locks required */
3099 if (alias->d_parent == dentry->d_parent)
3100 goto out_unalias;
3101
Trond Myklebust9eaef272006-10-21 10:24:20 -07003102 /* See lock_rename() */
Trond Myklebust9eaef272006-10-21 10:24:20 -07003103 if (!mutex_trylock(&dentry->d_sb->s_vfs_rename_mutex))
3104 goto out_err;
3105 m1 = &dentry->d_sb->s_vfs_rename_mutex;
Al Viro9902af72016-04-15 15:08:36 -04003106 if (!inode_trylock_shared(alias->d_parent->d_inode))
Trond Myklebust9eaef272006-10-21 10:24:20 -07003107 goto out_err;
Al Viro9902af72016-04-15 15:08:36 -04003108 m2 = &alias->d_parent->d_inode->i_rwsem;
Trond Myklebust9eaef272006-10-21 10:24:20 -07003109out_unalias:
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07003110 __d_move(alias, dentry, false);
Al Virob5ae6b12014-10-12 22:16:02 -04003111 ret = 0;
Trond Myklebust9eaef272006-10-21 10:24:20 -07003112out_err:
Trond Myklebust9eaef272006-10-21 10:24:20 -07003113 if (m2)
Al Viro9902af72016-04-15 15:08:36 -04003114 up_read(m2);
Trond Myklebust9eaef272006-10-21 10:24:20 -07003115 if (m1)
3116 mutex_unlock(m1);
3117 return ret;
3118}
3119
David Howells770bfad2006-08-22 20:06:07 -04003120/**
J. Bruce Fields3f70bd52014-02-18 14:11:26 -05003121 * d_splice_alias - splice a disconnected dentry into the tree if one exists
3122 * @inode: the inode which may have a disconnected dentry
3123 * @dentry: a negative dentry which we want to point to the inode.
3124 *
J. Bruce Fieldsda093a92014-02-17 18:03:57 -05003125 * If inode is a directory and has an IS_ROOT alias, then d_move that in
3126 * place of the given dentry and return it, else simply d_add the inode
3127 * to the dentry and return NULL.
J. Bruce Fields3f70bd52014-02-18 14:11:26 -05003128 *
J. Bruce Fields908790f2014-02-17 17:58:42 -05003129 * If a non-IS_ROOT directory is found, the filesystem is corrupt, and
3130 * we should error out: directories can't have multiple aliases.
3131 *
J. Bruce Fields3f70bd52014-02-18 14:11:26 -05003132 * This is needed in the lookup routine of any filesystem that is exportable
3133 * (via knfsd) so that we can build dcache paths to directories effectively.
3134 *
3135 * If a dentry was found and moved, then it is returned. Otherwise NULL
3136 * is returned. This matches the expected return value of ->lookup.
3137 *
3138 * Cluster filesystems may call this function with a negative, hashed dentry.
3139 * In that case, we know that the inode will be a regular file, and also this
3140 * will only occur during atomic_open. So we need to check for the dentry
3141 * being already hashed only in the final case.
3142 */
3143struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
3144{
J. Bruce Fields3f70bd52014-02-18 14:11:26 -05003145 if (IS_ERR(inode))
3146 return ERR_CAST(inode);
3147
David Howells770bfad2006-08-22 20:06:07 -04003148 BUG_ON(!d_unhashed(dentry));
3149
Al Virode689f52016-03-09 18:05:42 -05003150 if (!inode)
Al Virob5ae6b12014-10-12 22:16:02 -04003151 goto out;
Al Virode689f52016-03-09 18:05:42 -05003152
Al Virob9680912016-04-11 00:53:26 -04003153 security_d_instantiate(dentry, inode);
Nick Piggin873feea2011-01-07 17:50:06 +11003154 spin_lock(&inode->i_lock);
Trond Myklebust9eaef272006-10-21 10:24:20 -07003155 if (S_ISDIR(inode->i_mode)) {
Al Virob5ae6b12014-10-12 22:16:02 -04003156 struct dentry *new = __d_find_any_alias(inode);
3157 if (unlikely(new)) {
Eric W. Biedermana03e2832015-08-15 13:36:41 -05003158 /* The reference to new ensures it remains an alias */
3159 spin_unlock(&inode->i_lock);
Al Viro18367502011-07-12 21:42:24 -04003160 write_seqlock(&rename_lock);
Al Virob5ae6b12014-10-12 22:16:02 -04003161 if (unlikely(d_ancestor(new, dentry))) {
Al Viro18367502011-07-12 21:42:24 -04003162 write_sequnlock(&rename_lock);
Al Virob5ae6b12014-10-12 22:16:02 -04003163 dput(new);
3164 new = ERR_PTR(-ELOOP);
3165 pr_warn_ratelimited(
3166 "VFS: Lookup of '%s' in %s %s"
3167 " would have caused loop\n",
3168 dentry->d_name.name,
3169 inode->i_sb->s_type->name,
3170 inode->i_sb->s_id);
3171 } else if (!IS_ROOT(new)) {
Al Viro076515f2018-03-10 23:15:52 -05003172 struct dentry *old_parent = dget(new->d_parent);
Al Virob5ae6b12014-10-12 22:16:02 -04003173 int err = __d_unalias(inode, dentry, new);
3174 write_sequnlock(&rename_lock);
3175 if (err) {
3176 dput(new);
3177 new = ERR_PTR(err);
3178 }
Al Viro076515f2018-03-10 23:15:52 -05003179 dput(old_parent);
Al Viro18367502011-07-12 21:42:24 -04003180 } else {
Al Virob5ae6b12014-10-12 22:16:02 -04003181 __d_move(new, dentry, false);
3182 write_sequnlock(&rename_lock);
Trond Myklebust9eaef272006-10-21 10:24:20 -07003183 }
Al Virob5ae6b12014-10-12 22:16:02 -04003184 iput(inode);
3185 return new;
Trond Myklebust9eaef272006-10-21 10:24:20 -07003186 }
David Howells770bfad2006-08-22 20:06:07 -04003187 }
Al Virob5ae6b12014-10-12 22:16:02 -04003188out:
Al Viroed782b52016-03-09 19:52:39 -05003189 __d_add(dentry, inode);
Al Virob5ae6b12014-10-12 22:16:02 -04003190 return NULL;
David Howells770bfad2006-08-22 20:06:07 -04003191}
Al Virob5ae6b12014-10-12 22:16:02 -04003192EXPORT_SYMBOL(d_splice_alias);
David Howells770bfad2006-08-22 20:06:07 -04003193
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194/*
3195 * Test whether new_dentry is a subdirectory of old_dentry.
3196 *
3197 * Trivially implemented using the dcache structure
3198 */
3199
3200/**
3201 * is_subdir - is new dentry a subdirectory of old_dentry
3202 * @new_dentry: new dentry
3203 * @old_dentry: old dentry
3204 *
Yaowei Baia6e57872015-11-17 14:40:11 +08003205 * Returns true if new_dentry is a subdirectory of the parent (at any depth).
3206 * Returns false otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
3208 */
3209
Yaowei Baia6e57872015-11-17 14:40:11 +08003210bool is_subdir(struct dentry *new_dentry, struct dentry *old_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211{
Christian Brauner9dc97802024-07-02 21:03:26 +02003212 bool subdir;
Nick Piggin949854d2011-01-07 17:49:37 +11003213 unsigned seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09003215 if (new_dentry == old_dentry)
Yaowei Baia6e57872015-11-17 14:40:11 +08003216 return true;
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09003217
Christian Brauner9dc97802024-07-02 21:03:26 +02003218 /* Access d_parent under rcu as d_move() may change it. */
3219 rcu_read_lock();
3220 seq = read_seqbegin(&rename_lock);
3221 subdir = d_ancestor(old_dentry, new_dentry);
3222 /* Try lockless once... */
3223 if (read_seqretry(&rename_lock, seq)) {
3224 /* ...else acquire lock for progress even on deep chains. */
3225 read_seqlock_excl(&rename_lock);
3226 subdir = d_ancestor(old_dentry, new_dentry);
3227 read_sequnlock_excl(&rename_lock);
3228 }
3229 rcu_read_unlock();
3230 return subdir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231}
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +02003232EXPORT_SYMBOL(is_subdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233
Miklos Szeredidb14fc32013-09-05 11:44:35 +02003234static enum d_walk_ret d_genocide_kill(void *data, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235{
Miklos Szeredidb14fc32013-09-05 11:44:35 +02003236 struct dentry *root = data;
3237 if (dentry != root) {
3238 if (d_unhashed(dentry) || !dentry->d_inode)
3239 return D_WALK_SKIP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240
Miklos Szeredi01ddc4e2013-09-05 11:44:34 +02003241 if (!(dentry->d_flags & DCACHE_GENOCIDE)) {
3242 dentry->d_flags |= DCACHE_GENOCIDE;
3243 dentry->d_lockref.count--;
3244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 }
Miklos Szeredidb14fc32013-09-05 11:44:35 +02003246 return D_WALK_CONTINUE;
3247}
Nick Piggin58db63d2011-01-07 17:49:39 +11003248
Miklos Szeredidb14fc32013-09-05 11:44:35 +02003249void d_genocide(struct dentry *parent)
3250{
Al Viro3a8e3612018-04-15 18:27:23 -04003251 d_walk(parent, parent, d_genocide_kill);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252}
3253
Al Virocbd4a5b2018-03-29 15:08:21 -04003254EXPORT_SYMBOL(d_genocide);
3255
Miklos Szeredi863f1442022-09-24 07:00:00 +02003256void d_tmpfile(struct file *file, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257{
Miklos Szeredi863f1442022-09-24 07:00:00 +02003258 struct dentry *dentry = file->f_path.dentry;
3259
Al Viro60545d02013-06-07 01:20:27 -04003260 inode_dec_link_count(inode);
3261 BUG_ON(dentry->d_name.name != dentry->d_iname ||
Al Viro946e51f2014-10-26 19:19:16 -04003262 !hlist_unhashed(&dentry->d_u.d_alias) ||
Al Viro60545d02013-06-07 01:20:27 -04003263 !d_unlinked(dentry));
3264 spin_lock(&dentry->d_parent->d_lock);
3265 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
3266 dentry->d_name.len = sprintf(dentry->d_iname, "#%llu",
3267 (unsigned long long)inode->i_ino);
3268 spin_unlock(&dentry->d_lock);
3269 spin_unlock(&dentry->d_parent->d_lock);
3270 d_instantiate(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271}
Al Viro60545d02013-06-07 01:20:27 -04003272EXPORT_SYMBOL(d_tmpfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273
3274static __initdata unsigned long dhash_entries;
3275static int __init set_dhash_entries(char *str)
3276{
3277 if (!str)
3278 return 0;
3279 dhash_entries = simple_strtoul(str, &str, 0);
3280 return 1;
3281}
3282__setup("dhash_entries=", set_dhash_entries);
3283
3284static void __init dcache_init_early(void)
3285{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 /* If hashes are distributed across NUMA nodes, defer
3287 * hash allocation until vmalloc space is available.
3288 */
3289 if (hashdist)
3290 return;
3291
3292 dentry_hashtable =
3293 alloc_large_system_hash("Dentry cache",
Linus Torvaldsb07ad992011-04-23 22:32:03 -07003294 sizeof(struct hlist_bl_head),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 dhash_entries,
3296 13,
Pavel Tatashin3d375d72017-07-06 15:39:11 -07003297 HASH_EARLY | HASH_ZERO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 &d_hash_shift,
Alexey Dobriyanb35d7862017-11-20 18:05:52 +03003299 NULL,
Tim Bird31fe62b2012-05-23 13:33:35 +00003300 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 0);
Alexey Dobriyan854d3e62017-11-20 18:05:07 +03003302 d_hash_shift = 32 - d_hash_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303}
3304
Denis Cheng74bf17c2007-10-16 23:26:30 -07003305static void __init dcache_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306{
Pavel Tatashin3d375d72017-07-06 15:39:11 -07003307 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 * A constructor could be added for stable state like the lists,
3309 * but it is probably not worth it because of the cache nature
Pavel Tatashin3d375d72017-07-06 15:39:11 -07003310 * of the dcache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 */
David Windsor80344262017-06-10 22:50:44 -04003312 dentry_cache = KMEM_CACHE_USERCOPY(dentry,
3313 SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|SLAB_MEM_SPREAD|SLAB_ACCOUNT,
3314 d_iname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315
3316 /* Hash may have been set up in dcache_init_early */
3317 if (!hashdist)
3318 return;
3319
3320 dentry_hashtable =
3321 alloc_large_system_hash("Dentry cache",
Linus Torvaldsb07ad992011-04-23 22:32:03 -07003322 sizeof(struct hlist_bl_head),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 dhash_entries,
3324 13,
Pavel Tatashin3d375d72017-07-06 15:39:11 -07003325 HASH_ZERO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 &d_hash_shift,
Alexey Dobriyanb35d7862017-11-20 18:05:52 +03003327 NULL,
Tim Bird31fe62b2012-05-23 13:33:35 +00003328 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 0);
Alexey Dobriyan854d3e62017-11-20 18:05:07 +03003330 d_hash_shift = 32 - d_hash_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331}
3332
3333/* SLAB cache for __getname() consumers */
Christoph Lametere18b8902006-12-06 20:33:20 -08003334struct kmem_cache *names_cachep __read_mostly;
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07003335EXPORT_SYMBOL(names_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337void __init vfs_caches_init_early(void)
3338{
Sebastian Andrzej Siewior69163632017-06-27 18:19:11 +02003339 int i;
3340
3341 for (i = 0; i < ARRAY_SIZE(in_lookup_hashtable); i++)
3342 INIT_HLIST_BL_HEAD(&in_lookup_hashtable[i]);
3343
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 dcache_init_early();
3345 inode_init_early();
3346}
3347
Mel Gorman4248b0d2015-08-06 15:46:20 -07003348void __init vfs_caches_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349{
David Windsor6a9b8822017-06-10 22:50:30 -04003350 names_cachep = kmem_cache_create_usercopy("names_cache", PATH_MAX, 0,
3351 SLAB_HWCACHE_ALIGN|SLAB_PANIC, 0, PATH_MAX, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352
Denis Cheng74bf17c2007-10-16 23:26:30 -07003353 dcache_init();
3354 inode_init();
Mel Gorman4248b0d2015-08-06 15:46:20 -07003355 files_init();
3356 files_maxfiles_init();
Denis Cheng74bf17c2007-10-16 23:26:30 -07003357 mnt_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 bdev_cache_init();
3359 chrdev_init();
3360}