Commit | Line | Data |
---|---|---|
457c8996 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
1da177e4 LT |
2 | /* |
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 | ||
7a5cf791 | 18 | #include <linux/ratelimit.h> |
1da177e4 LT |
19 | #include <linux/string.h> |
20 | #include <linux/mm.h> | |
21 | #include <linux/fs.h> | |
0bf3d5c1 | 22 | #include <linux/fscrypt.h> |
7a91bf7f | 23 | #include <linux/fsnotify.h> |
1da177e4 LT |
24 | #include <linux/slab.h> |
25 | #include <linux/init.h> | |
1da177e4 LT |
26 | #include <linux/hash.h> |
27 | #include <linux/cache.h> | |
630d9c47 | 28 | #include <linux/export.h> |
1da177e4 LT |
29 | #include <linux/security.h> |
30 | #include <linux/seqlock.h> | |
57c8a661 | 31 | #include <linux/memblock.h> |
ceb5bdc2 NP |
32 | #include <linux/bit_spinlock.h> |
33 | #include <linux/rculist_bl.h> | |
f6041567 | 34 | #include <linux/list_lru.h> |
07f3f05c | 35 | #include "internal.h" |
b2dba1af | 36 | #include "mount.h" |
1da177e4 | 37 | |
e7829855 LT |
38 | #include <asm/runtime-const.h> |
39 | ||
789680d1 NP |
40 | /* |
41 | * Usage: | |
873feea0 | 42 | * dcache->d_inode->i_lock protects: |
946e51f2 | 43 | * - i_dentry, d_u.d_alias, d_inode of aliases |
ceb5bdc2 NP |
44 | * dcache_hash_bucket lock protects: |
45 | * - the dcache hash table | |
f1ee6162 N |
46 | * s_roots bl list spinlock protects: |
47 | * - the s_roots list (see __d_drop) | |
19156840 | 48 | * dentry->d_sb->s_dentry_lru_lock protects: |
23044507 NP |
49 | * - the dcache lru lists and counters |
50 | * d_lock protects: | |
51 | * - d_flags | |
52 | * - d_name | |
53 | * - d_lru | |
b7ab39f6 | 54 | * - d_count |
da502956 | 55 | * - d_unhashed() |
da549bdd AV |
56 | * - d_parent and d_chilren |
57 | * - childrens' d_sib and d_parent | |
946e51f2 | 58 | * - d_u.d_alias, d_inode |
789680d1 NP |
59 | * |
60 | * Ordering: | |
873feea0 | 61 | * dentry->d_inode->i_lock |
b5c84bf6 | 62 | * dentry->d_lock |
19156840 | 63 | * dentry->d_sb->s_dentry_lru_lock |
ceb5bdc2 | 64 | * dcache_hash_bucket lock |
f1ee6162 | 65 | * s_roots lock |
789680d1 | 66 | * |
da502956 NP |
67 | * If there is an ancestor relationship: |
68 | * dentry->d_parent->...->d_parent->d_lock | |
69 | * ... | |
70 | * dentry->d_parent->d_lock | |
71 | * dentry->d_lock | |
72 | * | |
73 | * If no ancestor relationship: | |
076515fc | 74 | * arbitrary, since it's serialized on rename_lock |
789680d1 | 75 | */ |
52e66823 | 76 | static int sysctl_vfs_cache_pressure __read_mostly = 100; |
e7b9cea7 | 77 | static int sysctl_vfs_cache_pressure_denom __read_mostly = 100; |
52e66823 KY |
78 | |
79 | unsigned long vfs_pressure_ratio(unsigned long val) | |
80 | { | |
e7b9cea7 | 81 | return mult_frac(val, sysctl_vfs_cache_pressure, sysctl_vfs_cache_pressure_denom); |
52e66823 KY |
82 | } |
83 | EXPORT_SYMBOL_GPL(vfs_pressure_ratio); | |
1da177e4 | 84 | |
74c3cbe3 | 85 | __cacheline_aligned_in_smp DEFINE_SEQLOCK(rename_lock); |
1da177e4 | 86 | |
949854d0 | 87 | EXPORT_SYMBOL(rename_lock); |
1da177e4 | 88 | |
68279f9c | 89 | static struct kmem_cache *dentry_cache __ro_after_init; |
1da177e4 | 90 | |
cdf01226 DH |
91 | const struct qstr empty_name = QSTR_INIT("", 0); |
92 | EXPORT_SYMBOL(empty_name); | |
93 | const struct qstr slash_name = QSTR_INIT("/", 1); | |
94 | EXPORT_SYMBOL(slash_name); | |
80e5d1ff AV |
95 | const struct qstr dotdot_name = QSTR_INIT("..", 2); |
96 | EXPORT_SYMBOL(dotdot_name); | |
cdf01226 | 97 | |
1da177e4 LT |
98 | /* |
99 | * This is the single most critical data structure when it comes | |
100 | * to the dcache: the hashtable for lookups. Somebody should try | |
101 | * to make this good - I've just made it work. | |
102 | * | |
103 | * This hash-function tries to avoid losing too many bits of hash | |
104 | * information, yet avoid using a prime hash-size or similar. | |
04c8abae SB |
105 | * |
106 | * Marking the variables "used" ensures that the compiler doesn't | |
107 | * optimize them away completely on architectures with runtime | |
108 | * constant infrastructure, this allows debuggers to see their | |
109 | * values. But updating these values has no effect on those arches. | |
1da177e4 | 110 | */ |
1da177e4 | 111 | |
04c8abae | 112 | static unsigned int d_hash_shift __ro_after_init __used; |
ceb5bdc2 | 113 | |
04c8abae | 114 | static struct hlist_bl_head *dentry_hashtable __ro_after_init __used; |
ceb5bdc2 | 115 | |
e60cc611 | 116 | static inline struct hlist_bl_head *d_hash(unsigned long hashlen) |
ceb5bdc2 | 117 | { |
e7829855 LT |
118 | return runtime_const_ptr(dentry_hashtable) + |
119 | runtime_const_shift_right_32(hashlen, d_hash_shift); | |
ceb5bdc2 NP |
120 | } |
121 | ||
94bdd655 AV |
122 | #define IN_LOOKUP_SHIFT 10 |
123 | static struct hlist_bl_head in_lookup_hashtable[1 << IN_LOOKUP_SHIFT]; | |
124 | ||
125 | static inline struct hlist_bl_head *in_lookup_hash(const struct dentry *parent, | |
126 | unsigned int hash) | |
127 | { | |
128 | hash += (unsigned long) parent / L1_CACHE_BYTES; | |
129 | return in_lookup_hashtable + hash_32(hash, IN_LOOKUP_SHIFT); | |
130 | } | |
131 | ||
c8c0c239 LC |
132 | struct dentry_stat_t { |
133 | long nr_dentry; | |
134 | long nr_unused; | |
135 | long age_limit; /* age in seconds */ | |
136 | long want_pages; /* pages requested by system */ | |
137 | long nr_negative; /* # of unused negative dentries */ | |
138 | long dummy; /* Reserved for future use */ | |
1da177e4 LT |
139 | }; |
140 | ||
3942c07c | 141 | static DEFINE_PER_CPU(long, nr_dentry); |
62d36c77 | 142 | static DEFINE_PER_CPU(long, nr_dentry_unused); |
af0c9af1 | 143 | static DEFINE_PER_CPU(long, nr_dentry_negative); |
e6957c99 | 144 | static int dentry_negative_policy; |
312d3ca8 CH |
145 | |
146 | #if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS) | |
c8c0c239 LC |
147 | /* Statistics gathering. */ |
148 | static struct dentry_stat_t dentry_stat = { | |
149 | .age_limit = 45, | |
150 | }; | |
62d36c77 DC |
151 | |
152 | /* | |
153 | * Here we resort to our own counters instead of using generic per-cpu counters | |
154 | * for consistency with what the vfs inode code does. We are expected to harvest | |
155 | * better code and performance by having our own specialized counters. | |
156 | * | |
157 | * Please note that the loop is done over all possible CPUs, not over all online | |
158 | * CPUs. The reason for this is that we don't want to play games with CPUs going | |
159 | * on and off. If one of them goes off, we will just keep their counters. | |
160 | * | |
161 | * glommer: See cffbc8a for details, and if you ever intend to change this, | |
162 | * please update all vfs counters to match. | |
163 | */ | |
3942c07c | 164 | static long get_nr_dentry(void) |
3e880fb5 NP |
165 | { |
166 | int i; | |
3942c07c | 167 | long sum = 0; |
3e880fb5 NP |
168 | for_each_possible_cpu(i) |
169 | sum += per_cpu(nr_dentry, i); | |
170 | return sum < 0 ? 0 : sum; | |
171 | } | |
172 | ||
62d36c77 DC |
173 | static long get_nr_dentry_unused(void) |
174 | { | |
175 | int i; | |
176 | long sum = 0; | |
177 | for_each_possible_cpu(i) | |
178 | sum += per_cpu(nr_dentry_unused, i); | |
179 | return sum < 0 ? 0 : sum; | |
180 | } | |
181 | ||
af0c9af1 WL |
182 | static long get_nr_dentry_negative(void) |
183 | { | |
184 | int i; | |
185 | long sum = 0; | |
186 | ||
187 | for_each_possible_cpu(i) | |
188 | sum += per_cpu(nr_dentry_negative, i); | |
189 | return sum < 0 ? 0 : sum; | |
190 | } | |
191 | ||
78eb4ea2 | 192 | static int proc_nr_dentry(const struct ctl_table *table, int write, void *buffer, |
c8c0c239 | 193 | size_t *lenp, loff_t *ppos) |
312d3ca8 | 194 | { |
3e880fb5 | 195 | dentry_stat.nr_dentry = get_nr_dentry(); |
62d36c77 | 196 | dentry_stat.nr_unused = get_nr_dentry_unused(); |
af0c9af1 | 197 | dentry_stat.nr_negative = get_nr_dentry_negative(); |
3942c07c | 198 | return proc_doulongvec_minmax(table, write, buffer, lenp, ppos); |
312d3ca8 | 199 | } |
c8c0c239 | 200 | |
1751f872 | 201 | static const struct ctl_table fs_dcache_sysctls[] = { |
c8c0c239 LC |
202 | { |
203 | .procname = "dentry-state", | |
204 | .data = &dentry_stat, | |
205 | .maxlen = 6*sizeof(long), | |
206 | .mode = 0444, | |
207 | .proc_handler = proc_nr_dentry, | |
208 | }, | |
e6957c99 YS |
209 | { |
210 | .procname = "dentry-negative", | |
211 | .data = &dentry_negative_policy, | |
212 | .maxlen = sizeof(dentry_negative_policy), | |
213 | .mode = 0644, | |
214 | .proc_handler = proc_dointvec_minmax, | |
215 | .extra1 = SYSCTL_ZERO, | |
216 | .extra2 = SYSCTL_ONE, | |
217 | }, | |
c8c0c239 LC |
218 | }; |
219 | ||
52e66823 KY |
220 | static const struct ctl_table vm_dcache_sysctls[] = { |
221 | { | |
222 | .procname = "vfs_cache_pressure", | |
223 | .data = &sysctl_vfs_cache_pressure, | |
224 | .maxlen = sizeof(sysctl_vfs_cache_pressure), | |
225 | .mode = 0644, | |
226 | .proc_handler = proc_dointvec_minmax, | |
227 | .extra1 = SYSCTL_ZERO, | |
228 | }, | |
e7b9cea7 YS |
229 | { |
230 | .procname = "vfs_cache_pressure_denom", | |
231 | .data = &sysctl_vfs_cache_pressure_denom, | |
232 | .maxlen = sizeof(sysctl_vfs_cache_pressure_denom), | |
233 | .mode = 0644, | |
234 | .proc_handler = proc_dointvec_minmax, | |
235 | .extra1 = SYSCTL_ONE_HUNDRED, | |
236 | }, | |
52e66823 KY |
237 | }; |
238 | ||
c8c0c239 LC |
239 | static int __init init_fs_dcache_sysctls(void) |
240 | { | |
52e66823 | 241 | register_sysctl_init("vm", vm_dcache_sysctls); |
c8c0c239 LC |
242 | register_sysctl_init("fs", fs_dcache_sysctls); |
243 | return 0; | |
244 | } | |
245 | fs_initcall(init_fs_dcache_sysctls); | |
312d3ca8 CH |
246 | #endif |
247 | ||
5483f18e LT |
248 | /* |
249 | * Compare 2 name strings, return 0 if they match, otherwise non-zero. | |
250 | * The strings are both count bytes long, and count is non-zero. | |
251 | */ | |
e419b4cc LT |
252 | #ifdef CONFIG_DCACHE_WORD_ACCESS |
253 | ||
254 | #include <asm/word-at-a-time.h> | |
255 | /* | |
256 | * NOTE! 'cs' and 'scount' come from a dentry, so it has a | |
257 | * aligned allocation for this particular component. We don't | |
258 | * strictly need the load_unaligned_zeropad() safety, but it | |
259 | * doesn't hurt either. | |
260 | * | |
261 | * In contrast, 'ct' and 'tcount' can be from a pathname, and do | |
262 | * need the careful unaligned handling. | |
263 | */ | |
94753db5 | 264 | static inline int dentry_string_cmp(const unsigned char *cs, const unsigned char *ct, unsigned tcount) |
5483f18e | 265 | { |
bfcfaa77 | 266 | unsigned long a,b,mask; |
bfcfaa77 LT |
267 | |
268 | for (;;) { | |
bfe7aa6c | 269 | a = read_word_at_a_time(cs); |
e419b4cc | 270 | b = load_unaligned_zeropad(ct); |
bfcfaa77 LT |
271 | if (tcount < sizeof(unsigned long)) |
272 | break; | |
273 | if (unlikely(a != b)) | |
274 | return 1; | |
275 | cs += sizeof(unsigned long); | |
276 | ct += sizeof(unsigned long); | |
277 | tcount -= sizeof(unsigned long); | |
278 | if (!tcount) | |
279 | return 0; | |
280 | } | |
a5c21dce | 281 | mask = bytemask_from_count(tcount); |
bfcfaa77 | 282 | return unlikely(!!((a ^ b) & mask)); |
e419b4cc LT |
283 | } |
284 | ||
bfcfaa77 | 285 | #else |
e419b4cc | 286 | |
94753db5 | 287 | static inline int dentry_string_cmp(const unsigned char *cs, const unsigned char *ct, unsigned tcount) |
e419b4cc | 288 | { |
5483f18e LT |
289 | do { |
290 | if (*cs != *ct) | |
291 | return 1; | |
292 | cs++; | |
293 | ct++; | |
294 | tcount--; | |
295 | } while (tcount); | |
296 | return 0; | |
297 | } | |
298 | ||
e419b4cc LT |
299 | #endif |
300 | ||
94753db5 LT |
301 | static inline int dentry_cmp(const struct dentry *dentry, const unsigned char *ct, unsigned tcount) |
302 | { | |
94753db5 LT |
303 | /* |
304 | * Be careful about RCU walk racing with rename: | |
506458ef | 305 | * use 'READ_ONCE' to fetch the name pointer. |
94753db5 LT |
306 | * |
307 | * NOTE! Even if a rename will mean that the length | |
308 | * was not loaded atomically, we don't care. The | |
309 | * RCU walk will check the sequence count eventually, | |
310 | * and catch it. And we won't overrun the buffer, | |
311 | * because we're reading the name pointer atomically, | |
312 | * and a dentry name is guaranteed to be properly | |
313 | * terminated with a NUL byte. | |
314 | * | |
315 | * End result: even if 'len' is wrong, we'll exit | |
316 | * early because the data cannot match (there can | |
317 | * be no NUL in the ct/tcount data) | |
318 | */ | |
506458ef | 319 | const unsigned char *cs = READ_ONCE(dentry->d_name.name); |
ae0a843c | 320 | |
6326c71f | 321 | return dentry_string_cmp(cs, ct, tcount); |
94753db5 LT |
322 | } |
323 | ||
95a4ccbb AV |
324 | /* |
325 | * long names are allocated separately from dentry and never modified. | |
326 | * Refcounted, freeing is RCU-delayed. See take_dentry_name_snapshot() | |
327 | * for the reason why ->count and ->head can't be combined into a union. | |
328 | * dentry_string_cmp() relies upon ->name[] being word-aligned. | |
329 | */ | |
8d85b484 | 330 | struct external_name { |
95a4ccbb AV |
331 | atomic_t count; |
332 | struct rcu_head head; | |
333 | unsigned char name[] __aligned(sizeof(unsigned long)); | |
8d85b484 AV |
334 | }; |
335 | ||
336 | static inline struct external_name *external_name(struct dentry *dentry) | |
337 | { | |
338 | return container_of(dentry->d_name.name, struct external_name, name[0]); | |
339 | } | |
340 | ||
9c82ab9c | 341 | static void __d_free(struct rcu_head *head) |
1da177e4 | 342 | { |
9c82ab9c CH |
343 | struct dentry *dentry = container_of(head, struct dentry, d_u.d_rcu); |
344 | ||
8d85b484 AV |
345 | kmem_cache_free(dentry_cache, dentry); |
346 | } | |
347 | ||
348 | static void __d_free_external(struct rcu_head *head) | |
349 | { | |
350 | struct dentry *dentry = container_of(head, struct dentry, d_u.d_rcu); | |
2e03b4bc | 351 | kfree(external_name(dentry)); |
f1782c9b | 352 | kmem_cache_free(dentry_cache, dentry); |
1da177e4 LT |
353 | } |
354 | ||
810bb172 AV |
355 | static inline int dname_external(const struct dentry *dentry) |
356 | { | |
58cf9c38 | 357 | return dentry->d_name.name != dentry->d_shortname.string; |
810bb172 AV |
358 | } |
359 | ||
49d31c2f AV |
360 | void take_dentry_name_snapshot(struct name_snapshot *name, struct dentry *dentry) |
361 | { | |
1c9be84a AV |
362 | unsigned seq; |
363 | const unsigned char *s; | |
364 | ||
365 | rcu_read_lock(); | |
366 | retry: | |
367 | seq = read_seqcount_begin(&dentry->d_seq); | |
368 | s = READ_ONCE(dentry->d_name.name); | |
369 | name->name.hash_len = dentry->d_name.hash_len; | |
370 | name->name.name = name->inline_name.string; | |
371 | if (likely(s == dentry->d_shortname.string)) { | |
58cf9c38 | 372 | name->inline_name = dentry->d_shortname; |
49d31c2f | 373 | } else { |
1c9be84a AV |
374 | struct external_name *p; |
375 | p = container_of(s, struct external_name, name[0]); | |
376 | // get a valid reference | |
95a4ccbb | 377 | if (unlikely(!atomic_inc_not_zero(&p->count))) |
1c9be84a AV |
378 | goto retry; |
379 | name->name.name = s; | |
49d31c2f | 380 | } |
1c9be84a AV |
381 | if (read_seqcount_retry(&dentry->d_seq, seq)) { |
382 | release_dentry_name_snapshot(name); | |
383 | goto retry; | |
49d31c2f | 384 | } |
1c9be84a | 385 | rcu_read_unlock(); |
49d31c2f AV |
386 | } |
387 | EXPORT_SYMBOL(take_dentry_name_snapshot); | |
388 | ||
389 | void release_dentry_name_snapshot(struct name_snapshot *name) | |
390 | { | |
58cf9c38 | 391 | if (unlikely(name->name.name != name->inline_name.string)) { |
49d31c2f | 392 | struct external_name *p; |
230c6402 | 393 | p = container_of(name->name.name, struct external_name, name[0]); |
95a4ccbb AV |
394 | if (unlikely(atomic_dec_and_test(&p->count))) |
395 | kfree_rcu(p, head); | |
49d31c2f AV |
396 | } |
397 | } | |
398 | EXPORT_SYMBOL(release_dentry_name_snapshot); | |
399 | ||
4bf46a27 DH |
400 | static inline void __d_set_inode_and_type(struct dentry *dentry, |
401 | struct inode *inode, | |
402 | unsigned type_flags) | |
403 | { | |
404 | unsigned flags; | |
405 | ||
406 | dentry->d_inode = inode; | |
4bf46a27 | 407 | flags = READ_ONCE(dentry->d_flags); |
8219cb58 | 408 | flags &= ~DCACHE_ENTRY_TYPE; |
4bf46a27 | 409 | flags |= type_flags; |
2fa6b1e0 | 410 | smp_store_release(&dentry->d_flags, flags); |
4bf46a27 DH |
411 | } |
412 | ||
4bf46a27 DH |
413 | static inline void __d_clear_type_and_inode(struct dentry *dentry) |
414 | { | |
415 | unsigned flags = READ_ONCE(dentry->d_flags); | |
416 | ||
8219cb58 | 417 | flags &= ~DCACHE_ENTRY_TYPE; |
4bf46a27 | 418 | WRITE_ONCE(dentry->d_flags, flags); |
4bf46a27 | 419 | dentry->d_inode = NULL; |
aabfe57e BF |
420 | /* |
421 | * The negative counter only tracks dentries on the LRU. Don't inc if | |
422 | * d_lru is on another list. | |
423 | */ | |
424 | if ((flags & (DCACHE_LRU_LIST|DCACHE_SHRINK_LIST)) == DCACHE_LRU_LIST) | |
af0c9af1 | 425 | this_cpu_inc(nr_dentry_negative); |
4bf46a27 DH |
426 | } |
427 | ||
b4f0354e AV |
428 | static void dentry_free(struct dentry *dentry) |
429 | { | |
946e51f2 | 430 | WARN_ON(!hlist_unhashed(&dentry->d_u.d_alias)); |
8d85b484 AV |
431 | if (unlikely(dname_external(dentry))) { |
432 | struct external_name *p = external_name(dentry); | |
95a4ccbb | 433 | if (likely(atomic_dec_and_test(&p->count))) { |
8d85b484 AV |
434 | call_rcu(&dentry->d_u.d_rcu, __d_free_external); |
435 | return; | |
436 | } | |
437 | } | |
b4f0354e | 438 | /* if dentry was never visible to RCU, immediate free is OK */ |
5467a68c | 439 | if (dentry->d_flags & DCACHE_NORCU) |
b4f0354e AV |
440 | __d_free(&dentry->d_u.d_rcu); |
441 | else | |
442 | call_rcu(&dentry->d_u.d_rcu, __d_free); | |
443 | } | |
444 | ||
1da177e4 LT |
445 | /* |
446 | * Release the dentry's inode, using the filesystem | |
550dce01 | 447 | * d_iput() operation if defined. |
31e6b01f NP |
448 | */ |
449 | static void dentry_unlink_inode(struct dentry * dentry) | |
450 | __releases(dentry->d_lock) | |
873feea0 | 451 | __releases(dentry->d_inode->i_lock) |
31e6b01f NP |
452 | { |
453 | struct inode *inode = dentry->d_inode; | |
a528aca7 | 454 | |
4c0d7cd5 | 455 | raw_write_seqcount_begin(&dentry->d_seq); |
4bf46a27 | 456 | __d_clear_type_and_inode(dentry); |
946e51f2 | 457 | hlist_del_init(&dentry->d_u.d_alias); |
4c0d7cd5 | 458 | raw_write_seqcount_end(&dentry->d_seq); |
31e6b01f | 459 | spin_unlock(&dentry->d_lock); |
873feea0 | 460 | spin_unlock(&inode->i_lock); |
31e6b01f NP |
461 | if (!inode->i_nlink) |
462 | fsnotify_inoderemove(inode); | |
463 | if (dentry->d_op && dentry->d_op->d_iput) | |
464 | dentry->d_op->d_iput(dentry, inode); | |
465 | else | |
466 | iput(inode); | |
467 | } | |
468 | ||
89dc77bc LT |
469 | /* |
470 | * The DCACHE_LRU_LIST bit is set whenever the 'd_lru' entry | |
471 | * is in use - which includes both the "real" per-superblock | |
472 | * LRU list _and_ the DCACHE_SHRINK_LIST use. | |
473 | * | |
474 | * The DCACHE_SHRINK_LIST bit is set whenever the dentry is | |
475 | * on the shrink list (ie not on the superblock LRU list). | |
476 | * | |
477 | * The per-cpu "nr_dentry_unused" counters are updated with | |
478 | * the DCACHE_LRU_LIST bit. | |
479 | * | |
af0c9af1 WL |
480 | * The per-cpu "nr_dentry_negative" counters are only updated |
481 | * when deleted from or added to the per-superblock LRU list, not | |
482 | * from/to the shrink list. That is to avoid an unneeded dec/inc | |
483 | * pair when moving from LRU to shrink list in select_collect(). | |
484 | * | |
89dc77bc LT |
485 | * These helper functions make sure we always follow the |
486 | * rules. d_lock must be held by the caller. | |
487 | */ | |
488 | #define D_FLAG_VERIFY(dentry,x) WARN_ON_ONCE(((dentry)->d_flags & (DCACHE_LRU_LIST | DCACHE_SHRINK_LIST)) != (x)) | |
489 | static void d_lru_add(struct dentry *dentry) | |
490 | { | |
491 | D_FLAG_VERIFY(dentry, 0); | |
492 | dentry->d_flags |= DCACHE_LRU_LIST; | |
493 | this_cpu_inc(nr_dentry_unused); | |
af0c9af1 WL |
494 | if (d_is_negative(dentry)) |
495 | this_cpu_inc(nr_dentry_negative); | |
0a97c01c NP |
496 | WARN_ON_ONCE(!list_lru_add_obj( |
497 | &dentry->d_sb->s_dentry_lru, &dentry->d_lru)); | |
89dc77bc LT |
498 | } |
499 | ||
500 | static void d_lru_del(struct dentry *dentry) | |
501 | { | |
502 | D_FLAG_VERIFY(dentry, DCACHE_LRU_LIST); | |
503 | dentry->d_flags &= ~DCACHE_LRU_LIST; | |
504 | this_cpu_dec(nr_dentry_unused); | |
af0c9af1 WL |
505 | if (d_is_negative(dentry)) |
506 | this_cpu_dec(nr_dentry_negative); | |
0a97c01c NP |
507 | WARN_ON_ONCE(!list_lru_del_obj( |
508 | &dentry->d_sb->s_dentry_lru, &dentry->d_lru)); | |
89dc77bc LT |
509 | } |
510 | ||
511 | static void d_shrink_del(struct dentry *dentry) | |
512 | { | |
513 | D_FLAG_VERIFY(dentry, DCACHE_SHRINK_LIST | DCACHE_LRU_LIST); | |
514 | list_del_init(&dentry->d_lru); | |
515 | dentry->d_flags &= ~(DCACHE_SHRINK_LIST | DCACHE_LRU_LIST); | |
516 | this_cpu_dec(nr_dentry_unused); | |
517 | } | |
518 | ||
519 | static void d_shrink_add(struct dentry *dentry, struct list_head *list) | |
520 | { | |
521 | D_FLAG_VERIFY(dentry, 0); | |
522 | list_add(&dentry->d_lru, list); | |
523 | dentry->d_flags |= DCACHE_SHRINK_LIST | DCACHE_LRU_LIST; | |
524 | this_cpu_inc(nr_dentry_unused); | |
525 | } | |
526 | ||
527 | /* | |
528 | * These can only be called under the global LRU lock, ie during the | |
529 | * callback for freeing the LRU list. "isolate" removes it from the | |
530 | * LRU lists entirely, while shrink_move moves it to the indicated | |
531 | * private list. | |
532 | */ | |
3f97b163 | 533 | static void d_lru_isolate(struct list_lru_one *lru, struct dentry *dentry) |
89dc77bc LT |
534 | { |
535 | D_FLAG_VERIFY(dentry, DCACHE_LRU_LIST); | |
536 | dentry->d_flags &= ~DCACHE_LRU_LIST; | |
537 | this_cpu_dec(nr_dentry_unused); | |
af0c9af1 WL |
538 | if (d_is_negative(dentry)) |
539 | this_cpu_dec(nr_dentry_negative); | |
3f97b163 | 540 | list_lru_isolate(lru, &dentry->d_lru); |
89dc77bc LT |
541 | } |
542 | ||
3f97b163 VD |
543 | static void d_lru_shrink_move(struct list_lru_one *lru, struct dentry *dentry, |
544 | struct list_head *list) | |
89dc77bc LT |
545 | { |
546 | D_FLAG_VERIFY(dentry, DCACHE_LRU_LIST); | |
547 | dentry->d_flags |= DCACHE_SHRINK_LIST; | |
af0c9af1 WL |
548 | if (d_is_negative(dentry)) |
549 | this_cpu_dec(nr_dentry_negative); | |
3f97b163 | 550 | list_lru_isolate_move(lru, &dentry->d_lru, list); |
89dc77bc LT |
551 | } |
552 | ||
61647823 | 553 | static void ___d_drop(struct dentry *dentry) |
789680d1 | 554 | { |
0632a9ac AV |
555 | struct hlist_bl_head *b; |
556 | /* | |
557 | * Hashed dentries are normally on the dentry hashtable, | |
558 | * with the exception of those newly allocated by | |
559 | * d_obtain_root, which are always IS_ROOT: | |
560 | */ | |
561 | if (unlikely(IS_ROOT(dentry))) | |
562 | b = &dentry->d_sb->s_roots; | |
563 | else | |
564 | b = d_hash(dentry->d_name.hash); | |
b61625d2 | 565 | |
0632a9ac AV |
566 | hlist_bl_lock(b); |
567 | __hlist_bl_del(&dentry->d_hash); | |
568 | hlist_bl_unlock(b); | |
789680d1 | 569 | } |
61647823 N |
570 | |
571 | void __d_drop(struct dentry *dentry) | |
572 | { | |
0632a9ac AV |
573 | if (!d_unhashed(dentry)) { |
574 | ___d_drop(dentry); | |
575 | dentry->d_hash.pprev = NULL; | |
576 | write_seqcount_invalidate(&dentry->d_seq); | |
577 | } | |
61647823 | 578 | } |
789680d1 NP |
579 | EXPORT_SYMBOL(__d_drop); |
580 | ||
961f3c89 MCC |
581 | /** |
582 | * d_drop - drop a dentry | |
583 | * @dentry: dentry to drop | |
584 | * | |
585 | * d_drop() unhashes the entry from the parent dentry hashes, so that it won't | |
586 | * be found through a VFS lookup any more. Note that this is different from | |
587 | * deleting the dentry - d_delete will try to mark the dentry negative if | |
588 | * possible, giving a successful _negative_ lookup, while d_drop will | |
589 | * just make the cache lookup fail. | |
590 | * | |
591 | * d_drop() is used mainly for stuff that wants to invalidate a dentry for some | |
592 | * reason (NFS timeouts or autofs deletes). | |
593 | * | |
594 | * __d_drop requires dentry->d_lock | |
595 | * | |
596 | * ___d_drop doesn't mark dentry as "unhashed" | |
597 | * (dentry->d_hash.pprev will be LIST_POISON2, not NULL). | |
598 | */ | |
789680d1 NP |
599 | void d_drop(struct dentry *dentry) |
600 | { | |
789680d1 NP |
601 | spin_lock(&dentry->d_lock); |
602 | __d_drop(dentry); | |
603 | spin_unlock(&dentry->d_lock); | |
789680d1 NP |
604 | } |
605 | EXPORT_SYMBOL(d_drop); | |
606 | ||
da549bdd | 607 | static inline void dentry_unlist(struct dentry *dentry) |
ba65dc5e AV |
608 | { |
609 | struct dentry *next; | |
610 | /* | |
611 | * Inform d_walk() and shrink_dentry_list() that we are no longer | |
612 | * attached to the dentry tree | |
613 | */ | |
614 | dentry->d_flags |= DCACHE_DENTRY_KILLED; | |
da549bdd | 615 | if (unlikely(hlist_unhashed(&dentry->d_sib))) |
ba65dc5e | 616 | return; |
da549bdd | 617 | __hlist_del(&dentry->d_sib); |
ba65dc5e AV |
618 | /* |
619 | * Cursors can move around the list of children. While we'd been | |
da549bdd | 620 | * a normal list member, it didn't matter - ->d_sib.next would've |
ba65dc5e AV |
621 | * been updated. However, from now on it won't be and for the |
622 | * things like d_walk() it might end up with a nasty surprise. | |
623 | * Normally d_walk() doesn't care about cursors moving around - | |
624 | * ->d_lock on parent prevents that and since a cursor has no children | |
625 | * of its own, we get through it without ever unlocking the parent. | |
626 | * There is one exception, though - if we ascend from a child that | |
627 | * gets killed as soon as we unlock it, the next sibling is found | |
da549bdd | 628 | * using the value left in its ->d_sib.next. And if _that_ |
ba65dc5e AV |
629 | * pointed to a cursor, and cursor got moved (e.g. by lseek()) |
630 | * before d_walk() regains parent->d_lock, we'll end up skipping | |
631 | * everything the cursor had been moved past. | |
632 | * | |
da549bdd | 633 | * Solution: make sure that the pointer left behind in ->d_sib.next |
ba65dc5e AV |
634 | * points to something that won't be moving around. I.e. skip the |
635 | * cursors. | |
636 | */ | |
da549bdd AV |
637 | while (dentry->d_sib.next) { |
638 | next = hlist_entry(dentry->d_sib.next, struct dentry, d_sib); | |
ba65dc5e AV |
639 | if (likely(!(next->d_flags & DCACHE_DENTRY_CURSOR))) |
640 | break; | |
da549bdd | 641 | dentry->d_sib.next = next->d_sib.next; |
ba65dc5e AV |
642 | } |
643 | } | |
644 | ||
1c18edd1 | 645 | static struct dentry *__dentry_kill(struct dentry *dentry) |
77812a1e | 646 | { |
41edf278 AV |
647 | struct dentry *parent = NULL; |
648 | bool can_free = true; | |
31e6b01f | 649 | |
0d98439e LT |
650 | /* |
651 | * The dentry is now unrecoverably dead to the world. | |
652 | */ | |
653 | lockref_mark_dead(&dentry->d_lockref); | |
654 | ||
f0023bc6 | 655 | /* |
f0023bc6 SW |
656 | * inform the fs via d_prune that this dentry is about to be |
657 | * unhashed and destroyed. | |
658 | */ | |
29266201 | 659 | if (dentry->d_flags & DCACHE_OP_PRUNE) |
61572bb1 YZ |
660 | dentry->d_op->d_prune(dentry); |
661 | ||
01b60351 AV |
662 | if (dentry->d_flags & DCACHE_LRU_LIST) { |
663 | if (!(dentry->d_flags & DCACHE_SHRINK_LIST)) | |
664 | d_lru_del(dentry); | |
01b60351 | 665 | } |
77812a1e NP |
666 | /* if it was on the hash then remove it */ |
667 | __d_drop(dentry); | |
550dce01 AV |
668 | if (dentry->d_inode) |
669 | dentry_unlink_inode(dentry); | |
670 | else | |
671 | spin_unlock(&dentry->d_lock); | |
03b3b889 AV |
672 | this_cpu_dec(nr_dentry); |
673 | if (dentry->d_op && dentry->d_op->d_release) | |
674 | dentry->d_op->d_release(dentry); | |
675 | ||
1c18edd1 AV |
676 | cond_resched(); |
677 | /* now that it's negative, ->d_parent is stable */ | |
678 | if (!IS_ROOT(dentry)) { | |
679 | parent = dentry->d_parent; | |
680 | spin_lock(&parent->d_lock); | |
41edf278 | 681 | } |
1c18edd1 AV |
682 | spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); |
683 | dentry_unlist(dentry); | |
1b327b5a | 684 | if (dentry->d_flags & DCACHE_SHRINK_LIST) |
41edf278 | 685 | can_free = false; |
41edf278 | 686 | spin_unlock(&dentry->d_lock); |
41edf278 AV |
687 | if (likely(can_free)) |
688 | dentry_free(dentry); | |
1c18edd1 | 689 | if (parent && --parent->d_lockref.count) { |
046b961b | 690 | spin_unlock(&parent->d_lock); |
1c18edd1 | 691 | return NULL; |
046b961b | 692 | } |
046b961b AV |
693 | return parent; |
694 | } | |
695 | ||
339e9e13 AV |
696 | /* |
697 | * Lock a dentry for feeding it to __dentry_kill(). | |
698 | * Called under rcu_read_lock() and dentry->d_lock; the former | |
699 | * guarantees that nothing we access will be freed under us. | |
700 | * Note that dentry is *not* protected from concurrent dentry_kill(), | |
701 | * d_delete(), etc. | |
702 | * | |
703 | * Return false if dentry is busy. Otherwise, return true and have | |
1c18edd1 | 704 | * that dentry's inode locked. |
339e9e13 AV |
705 | */ |
706 | ||
707 | static bool lock_for_kill(struct dentry *dentry) | |
8b987a46 | 708 | { |
339e9e13 | 709 | struct inode *inode = dentry->d_inode; |
339e9e13 AV |
710 | |
711 | if (unlikely(dentry->d_lockref.count)) | |
712 | return false; | |
713 | ||
1c18edd1 | 714 | if (!inode || likely(spin_trylock(&inode->i_lock))) |
339e9e13 AV |
715 | return true; |
716 | ||
1c18edd1 AV |
717 | do { |
718 | spin_unlock(&dentry->d_lock); | |
719 | spin_lock(&inode->i_lock); | |
720 | spin_lock(&dentry->d_lock); | |
339e9e13 AV |
721 | if (likely(inode == dentry->d_inode)) |
722 | break; | |
1c18edd1 | 723 | spin_unlock(&inode->i_lock); |
339e9e13 | 724 | inode = dentry->d_inode; |
1c18edd1 | 725 | } while (inode); |
339e9e13 AV |
726 | if (likely(!dentry->d_lockref.count)) |
727 | return true; | |
728 | if (inode) | |
729 | spin_unlock(&inode->i_lock); | |
339e9e13 | 730 | return false; |
8b987a46 AV |
731 | } |
732 | ||
6367b491 AV |
733 | /* |
734 | * Decide if dentry is worth retaining. Usually this is called with dentry | |
735 | * locked; if not locked, we are more limited and might not be able to tell | |
736 | * without a lock. False in this case means "punt to locked path and recheck". | |
737 | * | |
738 | * In case we aren't locked, these predicates are not "stable". However, it is | |
739 | * sufficient that at some point after we dropped the reference the dentry was | |
740 | * hashed and the flags had the proper value. Other dentry users may have | |
741 | * re-gotten a reference to the dentry and change that, but our work is done - | |
742 | * we can leave the dentry around with a zero refcount. | |
743 | */ | |
744 | static inline bool retain_dentry(struct dentry *dentry, bool locked) | |
a338579f | 745 | { |
6367b491 | 746 | unsigned int d_flags; |
a338579f | 747 | |
6367b491 AV |
748 | smp_rmb(); |
749 | d_flags = READ_ONCE(dentry->d_flags); | |
a338579f | 750 | |
6367b491 | 751 | // Unreachable? Nobody would be able to look it up, no point retaining |
a338579f AV |
752 | if (unlikely(d_unhashed(dentry))) |
753 | return false; | |
754 | ||
6367b491 AV |
755 | // Same if it's disconnected |
756 | if (unlikely(d_flags & DCACHE_DISCONNECTED)) | |
a338579f AV |
757 | return false; |
758 | ||
6367b491 AV |
759 | // ->d_delete() might tell us not to bother, but that requires |
760 | // ->d_lock; can't decide without it | |
761 | if (unlikely(d_flags & DCACHE_OP_DELETE)) { | |
762 | if (!locked || dentry->d_op->d_delete(dentry)) | |
a338579f AV |
763 | return false; |
764 | } | |
2c567af4 | 765 | |
6367b491 AV |
766 | // Explicitly told not to bother |
767 | if (unlikely(d_flags & DCACHE_DONTCACHE)) | |
2c567af4 IW |
768 | return false; |
769 | ||
6367b491 AV |
770 | // At this point it looks like we ought to keep it. We also might |
771 | // need to do something - put it on LRU if it wasn't there already | |
772 | // and mark it referenced if it was on LRU, but not marked yet. | |
773 | // Unfortunately, both actions require ->d_lock, so in lockless | |
774 | // case we'd have to punt rather than doing those. | |
775 | if (unlikely(!(d_flags & DCACHE_LRU_LIST))) { | |
776 | if (!locked) | |
777 | return false; | |
62d9956c | 778 | d_lru_add(dentry); |
6367b491 AV |
779 | } else if (unlikely(!(d_flags & DCACHE_REFERENCED))) { |
780 | if (!locked) | |
781 | return false; | |
62d9956c | 782 | dentry->d_flags |= DCACHE_REFERENCED; |
6367b491 | 783 | } |
a338579f AV |
784 | return true; |
785 | } | |
786 | ||
2c567af4 IW |
787 | void d_mark_dontcache(struct inode *inode) |
788 | { | |
789 | struct dentry *de; | |
790 | ||
791 | spin_lock(&inode->i_lock); | |
792 | hlist_for_each_entry(de, &inode->i_dentry, d_u.d_alias) { | |
793 | spin_lock(&de->d_lock); | |
794 | de->d_flags |= DCACHE_DONTCACHE; | |
795 | spin_unlock(&de->d_lock); | |
796 | } | |
797 | inode->i_state |= I_DONTCACHE; | |
798 | spin_unlock(&inode->i_lock); | |
799 | } | |
800 | EXPORT_SYMBOL(d_mark_dontcache); | |
801 | ||
360f5479 LT |
802 | /* |
803 | * Try to do a lockless dput(), and return whether that was successful. | |
804 | * | |
805 | * If unsuccessful, we return false, having already taken the dentry lock. | |
f05441c7 AV |
806 | * In that case refcount is guaranteed to be zero and we have already |
807 | * decided that it's not worth keeping around. | |
360f5479 LT |
808 | * |
809 | * The caller needs to hold the RCU read lock, so that the dentry is | |
810 | * guaranteed to stay around even if the refcount goes down to zero! | |
811 | */ | |
812 | static inline bool fast_dput(struct dentry *dentry) | |
813 | { | |
814 | int ret; | |
360f5479 LT |
815 | |
816 | /* | |
15220fbf | 817 | * try to decrement the lockref optimistically. |
360f5479 LT |
818 | */ |
819 | ret = lockref_put_return(&dentry->d_lockref); | |
820 | ||
821 | /* | |
822 | * If the lockref_put_return() failed due to the lock being held | |
823 | * by somebody else, the fast path has failed. We will need to | |
824 | * get the lock, and then check the count again. | |
825 | */ | |
826 | if (unlikely(ret < 0)) { | |
827 | spin_lock(&dentry->d_lock); | |
504e08ce | 828 | if (WARN_ON_ONCE(dentry->d_lockref.count <= 0)) { |
360f5479 | 829 | spin_unlock(&dentry->d_lock); |
7964410f | 830 | return true; |
360f5479 | 831 | } |
504e08ce AV |
832 | dentry->d_lockref.count--; |
833 | goto locked; | |
360f5479 LT |
834 | } |
835 | ||
836 | /* | |
837 | * If we weren't the last ref, we're done. | |
838 | */ | |
839 | if (ret) | |
7964410f | 840 | return true; |
360f5479 LT |
841 | |
842 | /* | |
6367b491 AV |
843 | * Can we decide that decrement of refcount is all we needed without |
844 | * taking the lock? There's a very common case when it's all we need - | |
845 | * dentry looks like it ought to be retained and there's nothing else | |
846 | * to do. | |
360f5479 | 847 | */ |
6367b491 | 848 | if (retain_dentry(dentry, false)) |
7964410f | 849 | return true; |
360f5479 LT |
850 | |
851 | /* | |
6367b491 AV |
852 | * Either not worth retaining or we can't tell without the lock. |
853 | * Get the lock, then. We've already decremented the refcount to 0, | |
854 | * but we'll need to re-check the situation after getting the lock. | |
360f5479 LT |
855 | */ |
856 | spin_lock(&dentry->d_lock); | |
857 | ||
858 | /* | |
859 | * Did somebody else grab a reference to it in the meantime, and | |
860 | * we're no longer the last user after all? Alternatively, somebody | |
861 | * else could have killed it and marked it dead. Either way, we | |
862 | * don't need to do anything else. | |
863 | */ | |
504e08ce | 864 | locked: |
6367b491 | 865 | if (dentry->d_lockref.count || retain_dentry(dentry, true)) { |
360f5479 | 866 | spin_unlock(&dentry->d_lock); |
7964410f | 867 | return true; |
360f5479 | 868 | } |
7964410f | 869 | return false; |
360f5479 LT |
870 | } |
871 | ||
872 | ||
1da177e4 LT |
873 | /* |
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. | |
1da177e4 | 898 | */ |
1da177e4 LT |
899 | void dput(struct dentry *dentry) |
900 | { | |
1c18edd1 AV |
901 | if (!dentry) |
902 | return; | |
903 | might_sleep(); | |
904 | rcu_read_lock(); | |
905 | if (likely(fast_dput(dentry))) { | |
360f5479 | 906 | rcu_read_unlock(); |
1c18edd1 AV |
907 | return; |
908 | } | |
909 | while (lock_for_kill(dentry)) { | |
910 | rcu_read_unlock(); | |
911 | dentry = __dentry_kill(dentry); | |
912 | if (!dentry) | |
1088a640 | 913 | return; |
6367b491 | 914 | if (retain_dentry(dentry, true)) { |
1088a640 AV |
915 | spin_unlock(&dentry->d_lock); |
916 | return; | |
917 | } | |
1c18edd1 | 918 | rcu_read_lock(); |
47be6184 | 919 | } |
1c18edd1 AV |
920 | rcu_read_unlock(); |
921 | spin_unlock(&dentry->d_lock); | |
1da177e4 | 922 | } |
ec4f8605 | 923 | EXPORT_SYMBOL(dput); |
1da177e4 | 924 | |
6511f6be | 925 | static void to_shrink_list(struct dentry *dentry, struct list_head *list) |
9bdebc2b AV |
926 | __must_hold(&dentry->d_lock) |
927 | { | |
6511f6be | 928 | if (!(dentry->d_flags & DCACHE_SHRINK_LIST)) { |
9bdebc2b AV |
929 | if (dentry->d_flags & DCACHE_LRU_LIST) |
930 | d_lru_del(dentry); | |
c2e5e29f | 931 | d_shrink_add(dentry, list); |
9bdebc2b AV |
932 | } |
933 | } | |
934 | ||
935 | void dput_to_list(struct dentry *dentry, struct list_head *list) | |
936 | { | |
937 | rcu_read_lock(); | |
938 | if (likely(fast_dput(dentry))) { | |
939 | rcu_read_unlock(); | |
940 | return; | |
941 | } | |
942 | rcu_read_unlock(); | |
f05441c7 | 943 | to_shrink_list(dentry, list); |
9bdebc2b AV |
944 | spin_unlock(&dentry->d_lock); |
945 | } | |
1da177e4 | 946 | |
b7ab39f6 NP |
947 | struct dentry *dget_parent(struct dentry *dentry) |
948 | { | |
df3d0bbc | 949 | int gotref; |
b7ab39f6 | 950 | struct dentry *ret; |
e8400933 | 951 | unsigned seq; |
b7ab39f6 | 952 | |
df3d0bbc WL |
953 | /* |
954 | * Do optimistic parent lookup without any | |
955 | * locking. | |
956 | */ | |
957 | rcu_read_lock(); | |
e8400933 | 958 | seq = raw_seqcount_begin(&dentry->d_seq); |
66702eb5 | 959 | ret = READ_ONCE(dentry->d_parent); |
df3d0bbc WL |
960 | gotref = lockref_get_not_zero(&ret->d_lockref); |
961 | rcu_read_unlock(); | |
962 | if (likely(gotref)) { | |
e8400933 | 963 | if (!read_seqcount_retry(&dentry->d_seq, seq)) |
df3d0bbc WL |
964 | return ret; |
965 | dput(ret); | |
966 | } | |
967 | ||
b7ab39f6 | 968 | repeat: |
a734eb45 NP |
969 | /* |
970 | * Don't need rcu_dereference because we re-check it was correct under | |
971 | * the lock. | |
972 | */ | |
973 | rcu_read_lock(); | |
b7ab39f6 | 974 | ret = dentry->d_parent; |
a734eb45 NP |
975 | spin_lock(&ret->d_lock); |
976 | if (unlikely(ret != dentry->d_parent)) { | |
977 | spin_unlock(&ret->d_lock); | |
978 | rcu_read_unlock(); | |
b7ab39f6 NP |
979 | goto repeat; |
980 | } | |
a734eb45 | 981 | rcu_read_unlock(); |
98474236 WL |
982 | BUG_ON(!ret->d_lockref.count); |
983 | ret->d_lockref.count++; | |
b7ab39f6 | 984 | spin_unlock(&ret->d_lock); |
b7ab39f6 NP |
985 | return ret; |
986 | } | |
987 | EXPORT_SYMBOL(dget_parent); | |
988 | ||
61fec493 AV |
989 | static struct dentry * __d_find_any_alias(struct inode *inode) |
990 | { | |
991 | struct dentry *alias; | |
992 | ||
993 | if (hlist_empty(&inode->i_dentry)) | |
994 | return NULL; | |
995 | alias = hlist_entry(inode->i_dentry.first, struct dentry, d_u.d_alias); | |
6d73c9ce | 996 | lockref_get(&alias->d_lockref); |
61fec493 AV |
997 | return alias; |
998 | } | |
999 | ||
1000 | /** | |
1001 | * d_find_any_alias - find any alias for a given inode | |
1002 | * @inode: inode to find an alias for | |
1003 | * | |
1004 | * If any aliases exist for the given inode, take and return a | |
1005 | * reference for one of them. If no aliases exist, return %NULL. | |
1006 | */ | |
1007 | struct dentry *d_find_any_alias(struct inode *inode) | |
1008 | { | |
1009 | struct dentry *de; | |
1010 | ||
1011 | spin_lock(&inode->i_lock); | |
1012 | de = __d_find_any_alias(inode); | |
1013 | spin_unlock(&inode->i_lock); | |
1014 | return de; | |
1015 | } | |
1016 | EXPORT_SYMBOL(d_find_any_alias); | |
1017 | ||
52ed46f0 | 1018 | static struct dentry *__d_find_alias(struct inode *inode) |
1da177e4 | 1019 | { |
61fec493 AV |
1020 | struct dentry *alias; |
1021 | ||
1022 | if (S_ISDIR(inode->i_mode)) | |
1023 | return __d_find_any_alias(inode); | |
1da177e4 | 1024 | |
946e51f2 | 1025 | hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) { |
da502956 | 1026 | spin_lock(&alias->d_lock); |
61fec493 | 1027 | if (!d_unhashed(alias)) { |
1b6ae9f6 | 1028 | dget_dlock(alias); |
8d80d7da BF |
1029 | spin_unlock(&alias->d_lock); |
1030 | return alias; | |
1da177e4 | 1031 | } |
da502956 | 1032 | spin_unlock(&alias->d_lock); |
1da177e4 | 1033 | } |
da502956 | 1034 | return NULL; |
1da177e4 LT |
1035 | } |
1036 | ||
961f3c89 MCC |
1037 | /** |
1038 | * d_find_alias - grab a hashed alias of inode | |
1039 | * @inode: inode in question | |
1040 | * | |
1041 | * If inode has a hashed alias, or is a directory and has any alias, | |
1042 | * acquire the reference to alias and return it. Otherwise return NULL. | |
1043 | * Notice that if inode is a directory there can be only one alias and | |
1044 | * it can be unhashed only if it has no children, or if it is the root | |
1045 | * of a filesystem, or if the directory was renamed and d_revalidate | |
1046 | * was the first vfs operation to notice. | |
1047 | * | |
1048 | * If the inode has an IS_ROOT, DCACHE_DISCONNECTED alias, then prefer | |
1049 | * any other hashed alias over that one. | |
1050 | */ | |
da502956 | 1051 | struct dentry *d_find_alias(struct inode *inode) |
1da177e4 | 1052 | { |
214fda1f DH |
1053 | struct dentry *de = NULL; |
1054 | ||
b3d9b7a3 | 1055 | if (!hlist_empty(&inode->i_dentry)) { |
873feea0 | 1056 | spin_lock(&inode->i_lock); |
52ed46f0 | 1057 | de = __d_find_alias(inode); |
873feea0 | 1058 | spin_unlock(&inode->i_lock); |
214fda1f | 1059 | } |
1da177e4 LT |
1060 | return de; |
1061 | } | |
ec4f8605 | 1062 | EXPORT_SYMBOL(d_find_alias); |
1da177e4 | 1063 | |
bca585d2 AV |
1064 | /* |
1065 | * Caller MUST be holding rcu_read_lock() and be guaranteed | |
1066 | * that inode won't get freed until rcu_read_unlock(). | |
1067 | */ | |
1068 | struct dentry *d_find_alias_rcu(struct inode *inode) | |
1069 | { | |
1070 | struct hlist_head *l = &inode->i_dentry; | |
1071 | struct dentry *de = NULL; | |
1072 | ||
1073 | spin_lock(&inode->i_lock); | |
1074 | // ->i_dentry and ->i_rcu are colocated, but the latter won't be | |
1075 | // used without having I_FREEING set, which means no aliases left | |
1076 | if (likely(!(inode->i_state & I_FREEING) && !hlist_empty(l))) { | |
1077 | if (S_ISDIR(inode->i_mode)) { | |
1078 | de = hlist_entry(l->first, struct dentry, d_u.d_alias); | |
1079 | } else { | |
1080 | hlist_for_each_entry(de, l, d_u.d_alias) | |
1081 | if (!d_unhashed(de)) | |
1082 | break; | |
1083 | } | |
1084 | } | |
1085 | spin_unlock(&inode->i_lock); | |
1086 | return de; | |
1087 | } | |
1088 | ||
1da177e4 LT |
1089 | /* |
1090 | * Try to kill dentries associated with this inode. | |
1091 | * WARNING: you must own a reference to inode. | |
1092 | */ | |
1093 | void d_prune_aliases(struct inode *inode) | |
1094 | { | |
b4cc0734 | 1095 | LIST_HEAD(dispose); |
0cdca3f9 | 1096 | struct dentry *dentry; |
b4cc0734 | 1097 | |
873feea0 | 1098 | spin_lock(&inode->i_lock); |
946e51f2 | 1099 | hlist_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) { |
1da177e4 | 1100 | spin_lock(&dentry->d_lock); |
b4cc0734 AV |
1101 | if (!dentry->d_lockref.count) |
1102 | to_shrink_list(dentry, &dispose); | |
1da177e4 LT |
1103 | spin_unlock(&dentry->d_lock); |
1104 | } | |
873feea0 | 1105 | spin_unlock(&inode->i_lock); |
b4cc0734 | 1106 | shrink_dentry_list(&dispose); |
1da177e4 | 1107 | } |
ec4f8605 | 1108 | EXPORT_SYMBOL(d_prune_aliases); |
1da177e4 | 1109 | |
1c18edd1 | 1110 | static inline void shrink_kill(struct dentry *victim) |
1da177e4 | 1111 | { |
1c18edd1 AV |
1112 | do { |
1113 | rcu_read_unlock(); | |
1114 | victim = __dentry_kill(victim); | |
1115 | rcu_read_lock(); | |
1116 | } while (victim && lock_for_kill(victim)); | |
1117 | rcu_read_unlock(); | |
1118 | if (victim) | |
1119 | spin_unlock(&victim->d_lock); | |
3b3f09f4 | 1120 | } |
77812a1e | 1121 | |
9bdebc2b | 1122 | void shrink_dentry_list(struct list_head *list) |
3b3f09f4 AV |
1123 | { |
1124 | while (!list_empty(list)) { | |
3fcf5356 | 1125 | struct dentry *dentry; |
64fd72e0 | 1126 | |
3b3f09f4 AV |
1127 | dentry = list_entry(list->prev, struct dentry, d_lru); |
1128 | spin_lock(&dentry->d_lock); | |
8f04da2a | 1129 | rcu_read_lock(); |
339e9e13 | 1130 | if (!lock_for_kill(dentry)) { |
cd9f84f3 | 1131 | bool can_free; |
8f04da2a | 1132 | rcu_read_unlock(); |
3b3f09f4 | 1133 | d_shrink_del(dentry); |
1b327b5a | 1134 | can_free = dentry->d_flags & DCACHE_DENTRY_KILLED; |
64fd72e0 AV |
1135 | spin_unlock(&dentry->d_lock); |
1136 | if (can_free) | |
1137 | dentry_free(dentry); | |
1138 | continue; | |
1139 | } | |
3b3f09f4 | 1140 | d_shrink_del(dentry); |
1c18edd1 | 1141 | shrink_kill(dentry); |
da3bbdd4 | 1142 | } |
3049cfe2 CH |
1143 | } |
1144 | ||
3f97b163 | 1145 | static enum lru_status dentry_lru_isolate(struct list_head *item, |
da0c0251 | 1146 | struct list_lru_one *lru, void *arg) |
f6041567 DC |
1147 | { |
1148 | struct list_head *freeable = arg; | |
1149 | struct dentry *dentry = container_of(item, struct dentry, d_lru); | |
1150 | ||
1151 | ||
1152 | /* | |
1153 | * we are inverting the lru lock/dentry->d_lock here, | |
1154 | * so use a trylock. If we fail to get the lock, just skip | |
1155 | * it | |
1156 | */ | |
1157 | if (!spin_trylock(&dentry->d_lock)) | |
1158 | return LRU_SKIP; | |
1159 | ||
1160 | /* | |
1161 | * Referenced dentries are still in use. If they have active | |
1162 | * counts, just remove them from the LRU. Otherwise give them | |
1163 | * another pass through the LRU. | |
1164 | */ | |
1165 | if (dentry->d_lockref.count) { | |
3f97b163 | 1166 | d_lru_isolate(lru, dentry); |
f6041567 DC |
1167 | spin_unlock(&dentry->d_lock); |
1168 | return LRU_REMOVED; | |
1169 | } | |
1170 | ||
1171 | if (dentry->d_flags & DCACHE_REFERENCED) { | |
1172 | dentry->d_flags &= ~DCACHE_REFERENCED; | |
1173 | spin_unlock(&dentry->d_lock); | |
1174 | ||
1175 | /* | |
1176 | * The list move itself will be made by the common LRU code. At | |
1177 | * this point, we've dropped the dentry->d_lock but keep the | |
1178 | * lru lock. This is safe to do, since every list movement is | |
1179 | * protected by the lru lock even if both locks are held. | |
1180 | * | |
1181 | * This is guaranteed by the fact that all LRU management | |
1182 | * functions are intermediated by the LRU API calls like | |
0a97c01c | 1183 | * list_lru_add_obj and list_lru_del_obj. List movement in this file |
f6041567 DC |
1184 | * only ever occur through this functions or through callbacks |
1185 | * like this one, that are called from the LRU API. | |
1186 | * | |
1187 | * The only exceptions to this are functions like | |
1188 | * shrink_dentry_list, and code that first checks for the | |
1189 | * DCACHE_SHRINK_LIST flag. Those are guaranteed to be | |
1190 | * operating only with stack provided lists after they are | |
1191 | * properly isolated from the main list. It is thus, always a | |
1192 | * local access. | |
1193 | */ | |
1194 | return LRU_ROTATE; | |
1195 | } | |
1196 | ||
3f97b163 | 1197 | d_lru_shrink_move(lru, dentry, freeable); |
f6041567 DC |
1198 | spin_unlock(&dentry->d_lock); |
1199 | ||
1200 | return LRU_REMOVED; | |
1201 | } | |
1202 | ||
3049cfe2 | 1203 | /** |
b48f03b3 DC |
1204 | * prune_dcache_sb - shrink the dcache |
1205 | * @sb: superblock | |
503c358c | 1206 | * @sc: shrink control, passed to list_lru_shrink_walk() |
b48f03b3 | 1207 | * |
503c358c VD |
1208 | * Attempt to shrink the superblock dcache LRU by @sc->nr_to_scan entries. This |
1209 | * is done when we need more memory and called from the superblock shrinker | |
b48f03b3 | 1210 | * function. |
3049cfe2 | 1211 | * |
b48f03b3 DC |
1212 | * This function may fail to free any resources if all the dentries are in |
1213 | * use. | |
3049cfe2 | 1214 | */ |
503c358c | 1215 | long prune_dcache_sb(struct super_block *sb, struct shrink_control *sc) |
3049cfe2 | 1216 | { |
f6041567 DC |
1217 | LIST_HEAD(dispose); |
1218 | long freed; | |
3049cfe2 | 1219 | |
503c358c VD |
1220 | freed = list_lru_shrink_walk(&sb->s_dentry_lru, sc, |
1221 | dentry_lru_isolate, &dispose); | |
f6041567 | 1222 | shrink_dentry_list(&dispose); |
0a234c6d | 1223 | return freed; |
da3bbdd4 | 1224 | } |
23044507 | 1225 | |
4e717f5c | 1226 | static enum lru_status dentry_lru_isolate_shrink(struct list_head *item, |
da0c0251 | 1227 | struct list_lru_one *lru, void *arg) |
dd1f6b2e | 1228 | { |
4e717f5c GC |
1229 | struct list_head *freeable = arg; |
1230 | struct dentry *dentry = container_of(item, struct dentry, d_lru); | |
dd1f6b2e | 1231 | |
4e717f5c GC |
1232 | /* |
1233 | * we are inverting the lru lock/dentry->d_lock here, | |
1234 | * so use a trylock. If we fail to get the lock, just skip | |
1235 | * it | |
1236 | */ | |
1237 | if (!spin_trylock(&dentry->d_lock)) | |
1238 | return LRU_SKIP; | |
1239 | ||
3f97b163 | 1240 | d_lru_shrink_move(lru, dentry, freeable); |
4e717f5c | 1241 | spin_unlock(&dentry->d_lock); |
ec33679d | 1242 | |
4e717f5c | 1243 | return LRU_REMOVED; |
da3bbdd4 KM |
1244 | } |
1245 | ||
4e717f5c | 1246 | |
1da177e4 LT |
1247 | /** |
1248 | * shrink_dcache_sb - shrink dcache for a superblock | |
1249 | * @sb: superblock | |
1250 | * | |
3049cfe2 CH |
1251 | * Shrink the dcache for the specified super block. This is used to free |
1252 | * the dcache before unmounting a file system. | |
1da177e4 | 1253 | */ |
3049cfe2 | 1254 | void shrink_dcache_sb(struct super_block *sb) |
1da177e4 | 1255 | { |
4e717f5c GC |
1256 | do { |
1257 | LIST_HEAD(dispose); | |
1258 | ||
1dbd449c | 1259 | list_lru_walk(&sb->s_dentry_lru, |
b17c070f | 1260 | dentry_lru_isolate_shrink, &dispose, 1024); |
4e717f5c | 1261 | shrink_dentry_list(&dispose); |
b17c070f | 1262 | } while (list_lru_count(&sb->s_dentry_lru) > 0); |
1da177e4 | 1263 | } |
ec4f8605 | 1264 | EXPORT_SYMBOL(shrink_dcache_sb); |
1da177e4 | 1265 | |
db14fc3a MS |
1266 | /** |
1267 | * enum d_walk_ret - action to talke during tree walk | |
1268 | * @D_WALK_CONTINUE: contrinue walk | |
1269 | * @D_WALK_QUIT: quit walk | |
1270 | * @D_WALK_NORETRY: quit when retry is needed | |
1271 | * @D_WALK_SKIP: skip this dentry and its children | |
1272 | */ | |
1273 | enum d_walk_ret { | |
1274 | D_WALK_CONTINUE, | |
1275 | D_WALK_QUIT, | |
1276 | D_WALK_NORETRY, | |
1277 | D_WALK_SKIP, | |
1278 | }; | |
c826cb7d | 1279 | |
1da177e4 | 1280 | /** |
db14fc3a MS |
1281 | * d_walk - walk the dentry tree |
1282 | * @parent: start of walk | |
1283 | * @data: data passed to @enter() and @finish() | |
1284 | * @enter: callback when first entering the dentry | |
1da177e4 | 1285 | * |
3a8e3611 | 1286 | * The @enter() callbacks are called with d_lock held. |
1da177e4 | 1287 | */ |
db14fc3a | 1288 | static void d_walk(struct dentry *parent, void *data, |
3a8e3611 | 1289 | enum d_walk_ret (*enter)(void *, struct dentry *)) |
1da177e4 | 1290 | { |
da549bdd | 1291 | struct dentry *this_parent, *dentry; |
48f5ec21 | 1292 | unsigned seq = 0; |
db14fc3a MS |
1293 | enum d_walk_ret ret; |
1294 | bool retry = true; | |
949854d0 | 1295 | |
58db63d0 | 1296 | again: |
48f5ec21 | 1297 | read_seqbegin_or_lock(&rename_lock, &seq); |
58db63d0 | 1298 | this_parent = parent; |
2fd6b7f5 | 1299 | spin_lock(&this_parent->d_lock); |
db14fc3a MS |
1300 | |
1301 | ret = enter(data, this_parent); | |
1302 | switch (ret) { | |
1303 | case D_WALK_CONTINUE: | |
1304 | break; | |
1305 | case D_WALK_QUIT: | |
1306 | case D_WALK_SKIP: | |
1307 | goto out_unlock; | |
1308 | case D_WALK_NORETRY: | |
1309 | retry = false; | |
1310 | break; | |
1311 | } | |
1da177e4 | 1312 | repeat: |
da549bdd | 1313 | dentry = d_first_child(this_parent); |
1da177e4 | 1314 | resume: |
da549bdd | 1315 | hlist_for_each_entry_from(dentry, d_sib) { |
ba65dc5e AV |
1316 | if (unlikely(dentry->d_flags & DCACHE_DENTRY_CURSOR)) |
1317 | continue; | |
1318 | ||
2fd6b7f5 | 1319 | spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); |
db14fc3a MS |
1320 | |
1321 | ret = enter(data, dentry); | |
1322 | switch (ret) { | |
1323 | case D_WALK_CONTINUE: | |
1324 | break; | |
1325 | case D_WALK_QUIT: | |
2fd6b7f5 | 1326 | spin_unlock(&dentry->d_lock); |
db14fc3a MS |
1327 | goto out_unlock; |
1328 | case D_WALK_NORETRY: | |
1329 | retry = false; | |
1330 | break; | |
1331 | case D_WALK_SKIP: | |
1332 | spin_unlock(&dentry->d_lock); | |
1333 | continue; | |
2fd6b7f5 | 1334 | } |
db14fc3a | 1335 | |
da549bdd | 1336 | if (!hlist_empty(&dentry->d_children)) { |
2fd6b7f5 | 1337 | spin_unlock(&this_parent->d_lock); |
5facae4f | 1338 | spin_release(&dentry->d_lock.dep_map, _RET_IP_); |
1da177e4 | 1339 | this_parent = dentry; |
2fd6b7f5 | 1340 | spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_); |
1da177e4 LT |
1341 | goto repeat; |
1342 | } | |
2fd6b7f5 | 1343 | spin_unlock(&dentry->d_lock); |
1da177e4 LT |
1344 | } |
1345 | /* | |
1346 | * All done at this level ... ascend and resume the search. | |
1347 | */ | |
ca5358ef AV |
1348 | rcu_read_lock(); |
1349 | ascend: | |
1da177e4 | 1350 | if (this_parent != parent) { |
da549bdd AV |
1351 | dentry = this_parent; |
1352 | this_parent = dentry->d_parent; | |
31dec132 | 1353 | |
da549bdd | 1354 | spin_unlock(&dentry->d_lock); |
31dec132 AV |
1355 | spin_lock(&this_parent->d_lock); |
1356 | ||
ca5358ef AV |
1357 | /* might go back up the wrong parent if we have had a rename. */ |
1358 | if (need_seqretry(&rename_lock, seq)) | |
949854d0 | 1359 | goto rename_retry; |
2159184e | 1360 | /* go into the first sibling still alive */ |
da549bdd AV |
1361 | hlist_for_each_entry_continue(dentry, d_sib) { |
1362 | if (likely(!(dentry->d_flags & DCACHE_DENTRY_KILLED))) { | |
1363 | rcu_read_unlock(); | |
1364 | goto resume; | |
1365 | } | |
1366 | } | |
1367 | goto ascend; | |
1da177e4 | 1368 | } |
ca5358ef | 1369 | if (need_seqretry(&rename_lock, seq)) |
949854d0 | 1370 | goto rename_retry; |
ca5358ef | 1371 | rcu_read_unlock(); |
db14fc3a MS |
1372 | |
1373 | out_unlock: | |
1374 | spin_unlock(&this_parent->d_lock); | |
48f5ec21 | 1375 | done_seqretry(&rename_lock, seq); |
db14fc3a | 1376 | return; |
58db63d0 NP |
1377 | |
1378 | rename_retry: | |
ca5358ef AV |
1379 | spin_unlock(&this_parent->d_lock); |
1380 | rcu_read_unlock(); | |
1381 | BUG_ON(seq & 1); | |
db14fc3a MS |
1382 | if (!retry) |
1383 | return; | |
48f5ec21 | 1384 | seq = 1; |
58db63d0 | 1385 | goto again; |
1da177e4 | 1386 | } |
db14fc3a | 1387 | |
01619491 IK |
1388 | struct check_mount { |
1389 | struct vfsmount *mnt; | |
1390 | unsigned int mounted; | |
1391 | }; | |
1392 | ||
1393 | static enum d_walk_ret path_check_mount(void *data, struct dentry *dentry) | |
1394 | { | |
1395 | struct check_mount *info = data; | |
1396 | struct path path = { .mnt = info->mnt, .dentry = dentry }; | |
1397 | ||
1398 | if (likely(!d_mountpoint(dentry))) | |
1399 | return D_WALK_CONTINUE; | |
1400 | if (__path_is_mountpoint(&path)) { | |
1401 | info->mounted = 1; | |
1402 | return D_WALK_QUIT; | |
1403 | } | |
1404 | return D_WALK_CONTINUE; | |
1405 | } | |
1406 | ||
1407 | /** | |
1408 | * path_has_submounts - check for mounts over a dentry in the | |
1409 | * current namespace. | |
1410 | * @parent: path to check. | |
1411 | * | |
1412 | * Return true if the parent or its subdirectories contain | |
1413 | * a mount point in the current namespace. | |
1414 | */ | |
1415 | int path_has_submounts(const struct path *parent) | |
1416 | { | |
1417 | struct check_mount data = { .mnt = parent->mnt, .mounted = 0 }; | |
1418 | ||
1419 | read_seqlock_excl(&mount_lock); | |
3a8e3611 | 1420 | d_walk(parent->dentry, &data, path_check_mount); |
01619491 IK |
1421 | read_sequnlock_excl(&mount_lock); |
1422 | ||
1423 | return data.mounted; | |
1424 | } | |
1425 | EXPORT_SYMBOL(path_has_submounts); | |
1426 | ||
eed81007 MS |
1427 | /* |
1428 | * Called by mount code to set a mountpoint and check if the mountpoint is | |
1429 | * reachable (e.g. NFS can unhash a directory dentry and then the complete | |
1430 | * subtree can become unreachable). | |
1431 | * | |
1ffe46d1 | 1432 | * Only one of d_invalidate() and d_set_mounted() must succeed. For |
eed81007 MS |
1433 | * this reason take rename_lock and d_lock on dentry and ancestors. |
1434 | */ | |
1435 | int d_set_mounted(struct dentry *dentry) | |
1436 | { | |
1437 | struct dentry *p; | |
1438 | int ret = -ENOENT; | |
1439 | write_seqlock(&rename_lock); | |
1440 | for (p = dentry->d_parent; !IS_ROOT(p); p = p->d_parent) { | |
1ffe46d1 | 1441 | /* Need exclusion wrt. d_invalidate() */ |
eed81007 MS |
1442 | spin_lock(&p->d_lock); |
1443 | if (unlikely(d_unhashed(p))) { | |
1444 | spin_unlock(&p->d_lock); | |
1445 | goto out; | |
1446 | } | |
1447 | spin_unlock(&p->d_lock); | |
1448 | } | |
1449 | spin_lock(&dentry->d_lock); | |
1450 | if (!d_unlinked(dentry)) { | |
3895dbf8 EB |
1451 | ret = -EBUSY; |
1452 | if (!d_mountpoint(dentry)) { | |
1453 | dentry->d_flags |= DCACHE_MOUNTED; | |
1454 | ret = 0; | |
1455 | } | |
eed81007 MS |
1456 | } |
1457 | spin_unlock(&dentry->d_lock); | |
1458 | out: | |
1459 | write_sequnlock(&rename_lock); | |
1460 | return ret; | |
1461 | } | |
1462 | ||
1da177e4 | 1463 | /* |
fd517909 | 1464 | * Search the dentry child list of the specified parent, |
1da177e4 LT |
1465 | * and move any unused dentries to the end of the unused |
1466 | * list for prune_dcache(). We descend to the next level | |
da549bdd | 1467 | * whenever the d_children list is non-empty and continue |
1da177e4 LT |
1468 | * searching. |
1469 | * | |
1470 | * It returns zero iff there are no unused children, | |
1471 | * otherwise it returns the number of children moved to | |
1472 | * the end of the unused list. This may not be the total | |
1473 | * number of unused children, because select_parent can | |
1474 | * drop the lock and return early due to latency | |
1475 | * constraints. | |
1476 | */ | |
1da177e4 | 1477 | |
db14fc3a MS |
1478 | struct select_data { |
1479 | struct dentry *start; | |
9bdebc2b AV |
1480 | union { |
1481 | long found; | |
1482 | struct dentry *victim; | |
1483 | }; | |
db14fc3a | 1484 | struct list_head dispose; |
db14fc3a | 1485 | }; |
23044507 | 1486 | |
db14fc3a MS |
1487 | static enum d_walk_ret select_collect(void *_data, struct dentry *dentry) |
1488 | { | |
1489 | struct select_data *data = _data; | |
1490 | enum d_walk_ret ret = D_WALK_CONTINUE; | |
1da177e4 | 1491 | |
db14fc3a MS |
1492 | if (data->start == dentry) |
1493 | goto out; | |
2fd6b7f5 | 1494 | |
fe91522a | 1495 | if (dentry->d_flags & DCACHE_SHRINK_LIST) { |
db14fc3a | 1496 | data->found++; |
f5c8a8a4 AV |
1497 | } else if (!dentry->d_lockref.count) { |
1498 | to_shrink_list(dentry, &data->dispose); | |
1499 | data->found++; | |
1c18edd1 AV |
1500 | } else if (dentry->d_lockref.count < 0) { |
1501 | data->found++; | |
1da177e4 | 1502 | } |
db14fc3a MS |
1503 | /* |
1504 | * We can return to the caller if we have found some (this | |
1505 | * ensures forward progress). We'll be coming back to find | |
1506 | * the rest. | |
1507 | */ | |
fe91522a AV |
1508 | if (!list_empty(&data->dispose)) |
1509 | ret = need_resched() ? D_WALK_QUIT : D_WALK_NORETRY; | |
1da177e4 | 1510 | out: |
db14fc3a | 1511 | return ret; |
1da177e4 LT |
1512 | } |
1513 | ||
9bdebc2b AV |
1514 | static enum d_walk_ret select_collect2(void *_data, struct dentry *dentry) |
1515 | { | |
1516 | struct select_data *data = _data; | |
1517 | enum d_walk_ret ret = D_WALK_CONTINUE; | |
1518 | ||
1519 | if (data->start == dentry) | |
1520 | goto out; | |
1521 | ||
f5c8a8a4 AV |
1522 | if (!dentry->d_lockref.count) { |
1523 | if (dentry->d_flags & DCACHE_SHRINK_LIST) { | |
9bdebc2b AV |
1524 | rcu_read_lock(); |
1525 | data->victim = dentry; | |
1526 | return D_WALK_QUIT; | |
1527 | } | |
f5c8a8a4 | 1528 | to_shrink_list(dentry, &data->dispose); |
9bdebc2b AV |
1529 | } |
1530 | /* | |
1531 | * We can return to the caller if we have found some (this | |
1532 | * ensures forward progress). We'll be coming back to find | |
1533 | * the rest. | |
1534 | */ | |
1535 | if (!list_empty(&data->dispose)) | |
1536 | ret = need_resched() ? D_WALK_QUIT : D_WALK_NORETRY; | |
1537 | out: | |
1538 | return ret; | |
1539 | } | |
1540 | ||
1da177e4 LT |
1541 | /** |
1542 | * shrink_dcache_parent - prune dcache | |
1543 | * @parent: parent of entries to prune | |
1544 | * | |
1545 | * Prune the dcache to remove unused children of the parent dentry. | |
1546 | */ | |
db14fc3a | 1547 | void shrink_dcache_parent(struct dentry *parent) |
1da177e4 | 1548 | { |
db14fc3a | 1549 | for (;;) { |
9bdebc2b | 1550 | struct select_data data = {.start = parent}; |
1da177e4 | 1551 | |
db14fc3a | 1552 | INIT_LIST_HEAD(&data.dispose); |
3a8e3611 | 1553 | d_walk(parent, &data, select_collect); |
4fb48871 AV |
1554 | |
1555 | if (!list_empty(&data.dispose)) { | |
1556 | shrink_dentry_list(&data.dispose); | |
1557 | continue; | |
1558 | } | |
1559 | ||
1560 | cond_resched(); | |
db14fc3a MS |
1561 | if (!data.found) |
1562 | break; | |
9bdebc2b AV |
1563 | data.victim = NULL; |
1564 | d_walk(parent, &data, select_collect2); | |
1565 | if (data.victim) { | |
9bdebc2b | 1566 | spin_lock(&data.victim->d_lock); |
339e9e13 | 1567 | if (!lock_for_kill(data.victim)) { |
9bdebc2b AV |
1568 | spin_unlock(&data.victim->d_lock); |
1569 | rcu_read_unlock(); | |
1570 | } else { | |
1c18edd1 | 1571 | shrink_kill(data.victim); |
9bdebc2b AV |
1572 | } |
1573 | } | |
1574 | if (!list_empty(&data.dispose)) | |
1575 | shrink_dentry_list(&data.dispose); | |
421348f1 | 1576 | } |
1da177e4 | 1577 | } |
ec4f8605 | 1578 | EXPORT_SYMBOL(shrink_dcache_parent); |
1da177e4 | 1579 | |
9c8c10e2 | 1580 | static enum d_walk_ret umount_check(void *_data, struct dentry *dentry) |
42c32608 | 1581 | { |
9c8c10e2 | 1582 | /* it has busy descendents; complain about those instead */ |
da549bdd | 1583 | if (!hlist_empty(&dentry->d_children)) |
9c8c10e2 | 1584 | return D_WALK_CONTINUE; |
42c32608 | 1585 | |
9c8c10e2 AV |
1586 | /* root with refcount 1 is fine */ |
1587 | if (dentry == _data && dentry->d_lockref.count == 1) | |
1588 | return D_WALK_CONTINUE; | |
1589 | ||
8c8e7dba | 1590 | WARN(1, "BUG: Dentry %p{i=%lx,n=%pd} " |
9c8c10e2 | 1591 | " still in use (%d) [unmount of %s %s]\n", |
42c32608 AV |
1592 | dentry, |
1593 | dentry->d_inode ? | |
1594 | dentry->d_inode->i_ino : 0UL, | |
9c8c10e2 | 1595 | dentry, |
42c32608 AV |
1596 | dentry->d_lockref.count, |
1597 | dentry->d_sb->s_type->name, | |
1598 | dentry->d_sb->s_id); | |
9c8c10e2 AV |
1599 | return D_WALK_CONTINUE; |
1600 | } | |
1601 | ||
1602 | static void do_one_tree(struct dentry *dentry) | |
1603 | { | |
1604 | shrink_dcache_parent(dentry); | |
3a8e3611 | 1605 | d_walk(dentry, dentry, umount_check); |
9c8c10e2 AV |
1606 | d_drop(dentry); |
1607 | dput(dentry); | |
42c32608 AV |
1608 | } |
1609 | ||
1610 | /* | |
1611 | * destroy the dentries attached to a superblock on unmounting | |
1612 | */ | |
1613 | void shrink_dcache_for_umount(struct super_block *sb) | |
1614 | { | |
1615 | struct dentry *dentry; | |
1616 | ||
54018131 | 1617 | rwsem_assert_held_write(&sb->s_umount); |
42c32608 AV |
1618 | |
1619 | dentry = sb->s_root; | |
1620 | sb->s_root = NULL; | |
9c8c10e2 | 1621 | do_one_tree(dentry); |
42c32608 | 1622 | |
f1ee6162 N |
1623 | while (!hlist_bl_empty(&sb->s_roots)) { |
1624 | dentry = dget(hlist_bl_entry(hlist_bl_first(&sb->s_roots), struct dentry, d_hash)); | |
9c8c10e2 | 1625 | do_one_tree(dentry); |
42c32608 AV |
1626 | } |
1627 | } | |
1628 | ||
ff17fa56 | 1629 | static enum d_walk_ret find_submount(void *_data, struct dentry *dentry) |
848ac114 | 1630 | { |
ff17fa56 | 1631 | struct dentry **victim = _data; |
848ac114 | 1632 | if (d_mountpoint(dentry)) { |
1b6ae9f6 | 1633 | *victim = dget_dlock(dentry); |
848ac114 MS |
1634 | return D_WALK_QUIT; |
1635 | } | |
ff17fa56 | 1636 | return D_WALK_CONTINUE; |
848ac114 MS |
1637 | } |
1638 | ||
1639 | /** | |
1ffe46d1 EB |
1640 | * d_invalidate - detach submounts, prune dcache, and drop |
1641 | * @dentry: dentry to invalidate (aka detach, prune and drop) | |
848ac114 | 1642 | */ |
5542aa2f | 1643 | void d_invalidate(struct dentry *dentry) |
848ac114 | 1644 | { |
ff17fa56 | 1645 | bool had_submounts = false; |
1ffe46d1 EB |
1646 | spin_lock(&dentry->d_lock); |
1647 | if (d_unhashed(dentry)) { | |
1648 | spin_unlock(&dentry->d_lock); | |
5542aa2f | 1649 | return; |
1ffe46d1 | 1650 | } |
ff17fa56 | 1651 | __d_drop(dentry); |
1ffe46d1 EB |
1652 | spin_unlock(&dentry->d_lock); |
1653 | ||
848ac114 | 1654 | /* Negative dentries can be dropped without further checks */ |
ff17fa56 | 1655 | if (!dentry->d_inode) |
5542aa2f | 1656 | return; |
848ac114 | 1657 | |
ff17fa56 | 1658 | shrink_dcache_parent(dentry); |
848ac114 | 1659 | for (;;) { |
ff17fa56 | 1660 | struct dentry *victim = NULL; |
3a8e3611 | 1661 | d_walk(dentry, &victim, find_submount); |
ff17fa56 AV |
1662 | if (!victim) { |
1663 | if (had_submounts) | |
1664 | shrink_dcache_parent(dentry); | |
81be24d2 | 1665 | return; |
8ed936b5 | 1666 | } |
ff17fa56 AV |
1667 | had_submounts = true; |
1668 | detach_mounts(victim); | |
1669 | dput(victim); | |
848ac114 | 1670 | } |
848ac114 | 1671 | } |
1ffe46d1 | 1672 | EXPORT_SYMBOL(d_invalidate); |
848ac114 | 1673 | |
1da177e4 | 1674 | /** |
a4464dbc AV |
1675 | * __d_alloc - allocate a dcache entry |
1676 | * @sb: filesystem it will belong to | |
1da177e4 LT |
1677 | * @name: qstr of the name |
1678 | * | |
1679 | * Allocates a dentry. It returns %NULL if there is insufficient memory | |
1680 | * available. On a success the dentry is returned. The name passed in is | |
1681 | * copied and the copy passed in may be reused after this call. | |
1682 | */ | |
1683 | ||
5c8b0dfc | 1684 | static struct dentry *__d_alloc(struct super_block *sb, const struct qstr *name) |
1da177e4 LT |
1685 | { |
1686 | struct dentry *dentry; | |
1687 | char *dname; | |
285b102d | 1688 | int err; |
1da177e4 | 1689 | |
f53bf711 MS |
1690 | dentry = kmem_cache_alloc_lru(dentry_cache, &sb->s_dentry_lru, |
1691 | GFP_KERNEL); | |
1da177e4 LT |
1692 | if (!dentry) |
1693 | return NULL; | |
1694 | ||
6326c71f LT |
1695 | /* |
1696 | * We guarantee that the inline name is always NUL-terminated. | |
1697 | * This way the memcpy() done by the name switching in rename | |
1698 | * will still always have a NUL at the end, even if we might | |
1699 | * be overwriting an internal NUL character | |
1700 | */ | |
58cf9c38 | 1701 | dentry->d_shortname.string[DNAME_INLINE_LEN-1] = 0; |
798434bd | 1702 | if (unlikely(!name)) { |
cdf01226 | 1703 | name = &slash_name; |
58cf9c38 | 1704 | dname = dentry->d_shortname.string; |
798434bd | 1705 | } else if (name->len > DNAME_INLINE_LEN-1) { |
8d85b484 | 1706 | size_t size = offsetof(struct external_name, name[1]); |
2e03b4bc VB |
1707 | struct external_name *p = kmalloc(size + name->len, |
1708 | GFP_KERNEL_ACCOUNT | | |
1709 | __GFP_RECLAIMABLE); | |
1710 | if (!p) { | |
1da177e4 LT |
1711 | kmem_cache_free(dentry_cache, dentry); |
1712 | return NULL; | |
1713 | } | |
95a4ccbb | 1714 | atomic_set(&p->count, 1); |
2e03b4bc | 1715 | dname = p->name; |
1da177e4 | 1716 | } else { |
58cf9c38 | 1717 | dname = dentry->d_shortname.string; |
1da177e4 | 1718 | } |
1da177e4 LT |
1719 | |
1720 | dentry->d_name.len = name->len; | |
1721 | dentry->d_name.hash = name->hash; | |
1722 | memcpy(dname, name->name, name->len); | |
1723 | dname[name->len] = 0; | |
1724 | ||
6326c71f | 1725 | /* Make sure we always see the terminating NUL character */ |
7088efa9 | 1726 | smp_store_release(&dentry->d_name.name, dname); /* ^^^ */ |
6326c71f | 1727 | |
dea3667b | 1728 | dentry->d_flags = 0; |
bb504b4d | 1729 | lockref_init(&dentry->d_lockref); |
26475371 | 1730 | seqcount_spinlock_init(&dentry->d_seq, &dentry->d_lock); |
1da177e4 | 1731 | dentry->d_inode = NULL; |
a4464dbc AV |
1732 | dentry->d_parent = dentry; |
1733 | dentry->d_sb = sb; | |
1da177e4 LT |
1734 | dentry->d_op = NULL; |
1735 | dentry->d_fsdata = NULL; | |
ceb5bdc2 | 1736 | INIT_HLIST_BL_NODE(&dentry->d_hash); |
1da177e4 | 1737 | INIT_LIST_HEAD(&dentry->d_lru); |
da549bdd | 1738 | INIT_HLIST_HEAD(&dentry->d_children); |
946e51f2 | 1739 | INIT_HLIST_NODE(&dentry->d_u.d_alias); |
da549bdd | 1740 | INIT_HLIST_NODE(&dentry->d_sib); |
a4464dbc | 1741 | d_set_d_op(dentry, dentry->d_sb->s_d_op); |
1da177e4 | 1742 | |
285b102d MS |
1743 | if (dentry->d_op && dentry->d_op->d_init) { |
1744 | err = dentry->d_op->d_init(dentry); | |
1745 | if (err) { | |
1746 | if (dname_external(dentry)) | |
1747 | kfree(external_name(dentry)); | |
1748 | kmem_cache_free(dentry_cache, dentry); | |
1749 | return NULL; | |
1750 | } | |
1751 | } | |
1752 | ||
3e880fb5 | 1753 | this_cpu_inc(nr_dentry); |
312d3ca8 | 1754 | |
1da177e4 LT |
1755 | return dentry; |
1756 | } | |
a4464dbc AV |
1757 | |
1758 | /** | |
1759 | * d_alloc - allocate a dcache entry | |
1760 | * @parent: parent of entry to allocate | |
1761 | * @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 | struct dentry *d_alloc(struct dentry * parent, const struct qstr *name) | |
1768 | { | |
1769 | struct dentry *dentry = __d_alloc(parent->d_sb, name); | |
1770 | if (!dentry) | |
1771 | return NULL; | |
a4464dbc AV |
1772 | spin_lock(&parent->d_lock); |
1773 | /* | |
1774 | * don't need child lock because it is not subject | |
1775 | * to concurrency here | |
1776 | */ | |
1b6ae9f6 | 1777 | dentry->d_parent = dget_dlock(parent); |
da549bdd | 1778 | hlist_add_head(&dentry->d_sib, &parent->d_children); |
a4464dbc AV |
1779 | spin_unlock(&parent->d_lock); |
1780 | ||
1781 | return dentry; | |
1782 | } | |
ec4f8605 | 1783 | EXPORT_SYMBOL(d_alloc); |
1da177e4 | 1784 | |
f9c34674 MS |
1785 | struct dentry *d_alloc_anon(struct super_block *sb) |
1786 | { | |
1787 | return __d_alloc(sb, NULL); | |
1788 | } | |
1789 | EXPORT_SYMBOL(d_alloc_anon); | |
1790 | ||
ba65dc5e AV |
1791 | struct dentry *d_alloc_cursor(struct dentry * parent) |
1792 | { | |
f9c34674 | 1793 | struct dentry *dentry = d_alloc_anon(parent->d_sb); |
ba65dc5e | 1794 | if (dentry) { |
5467a68c | 1795 | dentry->d_flags |= DCACHE_DENTRY_CURSOR; |
ba65dc5e AV |
1796 | dentry->d_parent = dget(parent); |
1797 | } | |
1798 | return dentry; | |
1799 | } | |
1800 | ||
e1a24bb0 BF |
1801 | /** |
1802 | * d_alloc_pseudo - allocate a dentry (for lookup-less filesystems) | |
1803 | * @sb: the superblock | |
1804 | * @name: qstr of the name | |
1805 | * | |
1806 | * For a filesystem that just pins its dentries in memory and never | |
1807 | * performs lookups at all, return an unhashed IS_ROOT dentry. | |
5467a68c AV |
1808 | * This is used for pipes, sockets et.al. - the stuff that should |
1809 | * never be anyone's children or parents. Unlike all other | |
1810 | * dentries, these will not have RCU delay between dropping the | |
1811 | * last reference and freeing them. | |
ab1152dd AV |
1812 | * |
1813 | * The only user is alloc_file_pseudo() and that's what should | |
1814 | * be considered a public interface. Don't use directly. | |
e1a24bb0 | 1815 | */ |
4b936885 NP |
1816 | struct dentry *d_alloc_pseudo(struct super_block *sb, const struct qstr *name) |
1817 | { | |
9024b4c9 AV |
1818 | static const struct dentry_operations anon_ops = { |
1819 | .d_dname = simple_dname | |
1820 | }; | |
5467a68c | 1821 | struct dentry *dentry = __d_alloc(sb, name); |
9024b4c9 | 1822 | if (likely(dentry)) { |
5467a68c | 1823 | dentry->d_flags |= DCACHE_NORCU; |
9024b4c9 AV |
1824 | if (!sb->s_d_op) |
1825 | d_set_d_op(dentry, &anon_ops); | |
1826 | } | |
5467a68c | 1827 | return dentry; |
4b936885 | 1828 | } |
4b936885 | 1829 | |
1da177e4 LT |
1830 | struct dentry *d_alloc_name(struct dentry *parent, const char *name) |
1831 | { | |
1832 | struct qstr q; | |
1833 | ||
1834 | q.name = name; | |
8387ff25 | 1835 | q.hash_len = hashlen_string(parent, name); |
1da177e4 LT |
1836 | return d_alloc(parent, &q); |
1837 | } | |
ef26ca97 | 1838 | EXPORT_SYMBOL(d_alloc_name); |
1da177e4 | 1839 | |
fb045adb NP |
1840 | void d_set_d_op(struct dentry *dentry, const struct dentry_operations *op) |
1841 | { | |
6f7f7caa LT |
1842 | WARN_ON_ONCE(dentry->d_op); |
1843 | WARN_ON_ONCE(dentry->d_flags & (DCACHE_OP_HASH | | |
fb045adb NP |
1844 | DCACHE_OP_COMPARE | |
1845 | DCACHE_OP_REVALIDATE | | |
ecf3d1f1 | 1846 | DCACHE_OP_WEAK_REVALIDATE | |
4bacc9c9 | 1847 | DCACHE_OP_DELETE | |
d101a125 | 1848 | DCACHE_OP_REAL)); |
fb045adb NP |
1849 | dentry->d_op = op; |
1850 | if (!op) | |
1851 | return; | |
1852 | if (op->d_hash) | |
1853 | dentry->d_flags |= DCACHE_OP_HASH; | |
1854 | if (op->d_compare) | |
1855 | dentry->d_flags |= DCACHE_OP_COMPARE; | |
1856 | if (op->d_revalidate) | |
1857 | dentry->d_flags |= DCACHE_OP_REVALIDATE; | |
ecf3d1f1 JL |
1858 | if (op->d_weak_revalidate) |
1859 | dentry->d_flags |= DCACHE_OP_WEAK_REVALIDATE; | |
fb045adb NP |
1860 | if (op->d_delete) |
1861 | dentry->d_flags |= DCACHE_OP_DELETE; | |
f0023bc6 SW |
1862 | if (op->d_prune) |
1863 | dentry->d_flags |= DCACHE_OP_PRUNE; | |
d101a125 MS |
1864 | if (op->d_real) |
1865 | dentry->d_flags |= DCACHE_OP_REAL; | |
fb045adb NP |
1866 | |
1867 | } | |
1868 | EXPORT_SYMBOL(d_set_d_op); | |
1869 | ||
b18825a7 DH |
1870 | static unsigned d_flags_for_inode(struct inode *inode) |
1871 | { | |
44bdb5e5 | 1872 | unsigned add_flags = DCACHE_REGULAR_TYPE; |
b18825a7 DH |
1873 | |
1874 | if (!inode) | |
1875 | return DCACHE_MISS_TYPE; | |
1876 | ||
1877 | if (S_ISDIR(inode->i_mode)) { | |
1878 | add_flags = DCACHE_DIRECTORY_TYPE; | |
1879 | if (unlikely(!(inode->i_opflags & IOP_LOOKUP))) { | |
1880 | if (unlikely(!inode->i_op->lookup)) | |
1881 | add_flags = DCACHE_AUTODIR_TYPE; | |
1882 | else | |
1883 | inode->i_opflags |= IOP_LOOKUP; | |
1884 | } | |
44bdb5e5 DH |
1885 | goto type_determined; |
1886 | } | |
1887 | ||
1888 | if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) { | |
6b255391 | 1889 | if (unlikely(inode->i_op->get_link)) { |
b18825a7 | 1890 | add_flags = DCACHE_SYMLINK_TYPE; |
44bdb5e5 DH |
1891 | goto type_determined; |
1892 | } | |
1893 | inode->i_opflags |= IOP_NOFOLLOW; | |
b18825a7 DH |
1894 | } |
1895 | ||
44bdb5e5 DH |
1896 | if (unlikely(!S_ISREG(inode->i_mode))) |
1897 | add_flags = DCACHE_SPECIAL_TYPE; | |
1898 | ||
1899 | type_determined: | |
b18825a7 DH |
1900 | if (unlikely(IS_AUTOMOUNT(inode))) |
1901 | add_flags |= DCACHE_NEED_AUTOMOUNT; | |
1902 | return add_flags; | |
1903 | } | |
1904 | ||
360da900 OH |
1905 | static void __d_instantiate(struct dentry *dentry, struct inode *inode) |
1906 | { | |
b18825a7 | 1907 | unsigned add_flags = d_flags_for_inode(inode); |
85c7f810 | 1908 | WARN_ON(d_in_lookup(dentry)); |
b18825a7 | 1909 | |
b23fb0a6 | 1910 | spin_lock(&dentry->d_lock); |
af0c9af1 | 1911 | /* |
aabfe57e BF |
1912 | * The negative counter only tracks dentries on the LRU. Don't dec if |
1913 | * d_lru is on another list. | |
af0c9af1 | 1914 | */ |
aabfe57e BF |
1915 | if ((dentry->d_flags & |
1916 | (DCACHE_LRU_LIST|DCACHE_SHRINK_LIST)) == DCACHE_LRU_LIST) | |
af0c9af1 | 1917 | this_cpu_dec(nr_dentry_negative); |
de689f5e | 1918 | hlist_add_head(&dentry->d_u.d_alias, &inode->i_dentry); |
a528aca7 | 1919 | raw_write_seqcount_begin(&dentry->d_seq); |
4bf46a27 | 1920 | __d_set_inode_and_type(dentry, inode, add_flags); |
a528aca7 | 1921 | raw_write_seqcount_end(&dentry->d_seq); |
affda484 | 1922 | fsnotify_update_flags(dentry); |
b23fb0a6 | 1923 | spin_unlock(&dentry->d_lock); |
360da900 OH |
1924 | } |
1925 | ||
1da177e4 LT |
1926 | /** |
1927 | * d_instantiate - fill in inode information for a dentry | |
1928 | * @entry: dentry to complete | |
1929 | * @inode: inode to attach to this dentry | |
1930 | * | |
1931 | * Fill in inode information in the entry. | |
1932 | * | |
1933 | * This turns negative dentries into productive full members | |
1934 | * of society. | |
1935 | * | |
1936 | * NOTE! This assumes that the inode count has been incremented | |
1937 | * (or otherwise set) by the caller to indicate that it is now | |
1938 | * in use by the dcache. | |
1939 | */ | |
1940 | ||
1941 | void d_instantiate(struct dentry *entry, struct inode * inode) | |
1942 | { | |
946e51f2 | 1943 | BUG_ON(!hlist_unhashed(&entry->d_u.d_alias)); |
de689f5e | 1944 | if (inode) { |
b9680917 | 1945 | security_d_instantiate(entry, inode); |
873feea0 | 1946 | spin_lock(&inode->i_lock); |
de689f5e | 1947 | __d_instantiate(entry, inode); |
873feea0 | 1948 | spin_unlock(&inode->i_lock); |
de689f5e | 1949 | } |
1da177e4 | 1950 | } |
ec4f8605 | 1951 | EXPORT_SYMBOL(d_instantiate); |
1da177e4 | 1952 | |
1e2e547a AV |
1953 | /* |
1954 | * This should be equivalent to d_instantiate() + unlock_new_inode(), | |
1955 | * with lockdep-related part of unlock_new_inode() done before | |
1956 | * anything else. Use that instead of open-coding d_instantiate()/ | |
1957 | * unlock_new_inode() combinations. | |
1958 | */ | |
1959 | void d_instantiate_new(struct dentry *entry, struct inode *inode) | |
1960 | { | |
1961 | BUG_ON(!hlist_unhashed(&entry->d_u.d_alias)); | |
1962 | BUG_ON(!inode); | |
1963 | lockdep_annotate_inode_mutex_key(inode); | |
1964 | security_d_instantiate(entry, inode); | |
1965 | spin_lock(&inode->i_lock); | |
1966 | __d_instantiate(entry, inode); | |
1967 | WARN_ON(!(inode->i_state & I_NEW)); | |
c2b6d621 | 1968 | inode->i_state &= ~I_NEW & ~I_CREATING; |
0fe340a9 CB |
1969 | /* |
1970 | * Pairs with the barrier in prepare_to_wait_event() to make sure | |
1971 | * ___wait_var_event() either sees the bit cleared or | |
1972 | * waitqueue_active() check in wake_up_var() sees the waiter. | |
1973 | */ | |
1e2e547a | 1974 | smp_mb(); |
0fe340a9 | 1975 | inode_wake_up_bit(inode, __I_NEW); |
1e2e547a AV |
1976 | spin_unlock(&inode->i_lock); |
1977 | } | |
1978 | EXPORT_SYMBOL(d_instantiate_new); | |
1979 | ||
adc0e91a AV |
1980 | struct dentry *d_make_root(struct inode *root_inode) |
1981 | { | |
1982 | struct dentry *res = NULL; | |
1983 | ||
1984 | if (root_inode) { | |
f9c34674 | 1985 | res = d_alloc_anon(root_inode->i_sb); |
5467a68c | 1986 | if (res) |
adc0e91a | 1987 | d_instantiate(res, root_inode); |
5467a68c | 1988 | else |
adc0e91a AV |
1989 | iput(root_inode); |
1990 | } | |
1991 | return res; | |
1992 | } | |
1993 | EXPORT_SYMBOL(d_make_root); | |
1994 | ||
f9c34674 MS |
1995 | static struct dentry *__d_obtain_alias(struct inode *inode, bool disconnected) |
1996 | { | |
f2824db1 AV |
1997 | struct super_block *sb; |
1998 | struct dentry *new, *res; | |
f9c34674 MS |
1999 | |
2000 | if (!inode) | |
2001 | return ERR_PTR(-ESTALE); | |
2002 | if (IS_ERR(inode)) | |
2003 | return ERR_CAST(inode); | |
2004 | ||
f2824db1 AV |
2005 | sb = inode->i_sb; |
2006 | ||
2007 | res = d_find_any_alias(inode); /* existing alias? */ | |
f9c34674 | 2008 | if (res) |
f2824db1 | 2009 | goto out; |
f9c34674 | 2010 | |
f2824db1 AV |
2011 | new = d_alloc_anon(sb); |
2012 | if (!new) { | |
f9c34674 | 2013 | res = ERR_PTR(-ENOMEM); |
f2824db1 | 2014 | goto out; |
f9c34674 MS |
2015 | } |
2016 | ||
f2824db1 AV |
2017 | security_d_instantiate(new, inode); |
2018 | spin_lock(&inode->i_lock); | |
2019 | res = __d_find_any_alias(inode); /* recheck under lock */ | |
2020 | if (likely(!res)) { /* still no alias, attach a disconnected dentry */ | |
2021 | unsigned add_flags = d_flags_for_inode(inode); | |
2022 | ||
2023 | if (disconnected) | |
2024 | add_flags |= DCACHE_DISCONNECTED; | |
f9c34674 | 2025 | |
f2824db1 AV |
2026 | spin_lock(&new->d_lock); |
2027 | __d_set_inode_and_type(new, inode, add_flags); | |
2028 | hlist_add_head(&new->d_u.d_alias, &inode->i_dentry); | |
2029 | if (!disconnected) { | |
2030 | hlist_bl_lock(&sb->s_roots); | |
2031 | hlist_bl_add_head(&new->d_hash, &sb->s_roots); | |
2032 | hlist_bl_unlock(&sb->s_roots); | |
2033 | } | |
2034 | spin_unlock(&new->d_lock); | |
2035 | spin_unlock(&inode->i_lock); | |
2036 | inode = NULL; /* consumed by new->d_inode */ | |
2037 | res = new; | |
2038 | } else { | |
2039 | spin_unlock(&inode->i_lock); | |
2040 | dput(new); | |
2041 | } | |
f9c34674 | 2042 | |
f2824db1 | 2043 | out: |
f9c34674 MS |
2044 | iput(inode); |
2045 | return res; | |
2046 | } | |
2047 | ||
1a0a397e BF |
2048 | /** |
2049 | * d_obtain_alias - find or allocate a DISCONNECTED dentry for a given inode | |
2050 | * @inode: inode to allocate the dentry for | |
2051 | * | |
2052 | * Obtain a dentry for an inode resulting from NFS filehandle conversion or | |
2053 | * similar open by handle operations. The returned dentry may be anonymous, | |
2054 | * or may have a full name (if the inode was already in the cache). | |
2055 | * | |
2056 | * When called on a directory inode, we must ensure that the inode only ever | |
2057 | * has one dentry. If a dentry is found, that is returned instead of | |
2058 | * allocating a new one. | |
2059 | * | |
2060 | * On successful return, the reference to the inode has been transferred | |
2061 | * to the dentry. In case of an error the reference on the inode is released. | |
2062 | * To make it easier to use in export operations a %NULL or IS_ERR inode may | |
2063 | * be passed in and the error will be propagated to the return value, | |
2064 | * with a %NULL @inode replaced by ERR_PTR(-ESTALE). | |
2065 | */ | |
2066 | struct dentry *d_obtain_alias(struct inode *inode) | |
2067 | { | |
f9c34674 | 2068 | return __d_obtain_alias(inode, true); |
1a0a397e | 2069 | } |
adc48720 | 2070 | EXPORT_SYMBOL(d_obtain_alias); |
1da177e4 | 2071 | |
1a0a397e BF |
2072 | /** |
2073 | * d_obtain_root - find or allocate a dentry for a given inode | |
2074 | * @inode: inode to allocate the dentry for | |
2075 | * | |
2076 | * Obtain an IS_ROOT dentry for the root of a filesystem. | |
2077 | * | |
2078 | * We must ensure that directory inodes only ever have one dentry. If a | |
2079 | * dentry is found, that is returned instead of allocating a new one. | |
2080 | * | |
2081 | * On successful return, the reference to the inode has been transferred | |
2082 | * to the dentry. In case of an error the reference on the inode is | |
2083 | * released. A %NULL or IS_ERR inode may be passed in and will be the | |
2084 | * error will be propagate to the return value, with a %NULL @inode | |
2085 | * replaced by ERR_PTR(-ESTALE). | |
2086 | */ | |
2087 | struct dentry *d_obtain_root(struct inode *inode) | |
2088 | { | |
f9c34674 | 2089 | return __d_obtain_alias(inode, false); |
1a0a397e BF |
2090 | } |
2091 | EXPORT_SYMBOL(d_obtain_root); | |
2092 | ||
9403540c BN |
2093 | /** |
2094 | * d_add_ci - lookup or allocate new dentry with case-exact name | |
9403540c | 2095 | * @dentry: the negative dentry that was passed to the parent's lookup func |
1e756248 | 2096 | * @inode: the inode case-insensitive lookup has found |
9403540c BN |
2097 | * @name: the case-exact name to be associated with the returned dentry |
2098 | * | |
2099 | * This is to avoid filling the dcache with case-insensitive names to the | |
2100 | * same inode, only the actual correct case is stored in the dcache for | |
2101 | * case-insensitive filesystems. | |
2102 | * | |
3d742d4b RD |
2103 | * For a case-insensitive lookup match and if the case-exact dentry |
2104 | * already exists in the dcache, use it and return it. | |
9403540c BN |
2105 | * |
2106 | * If no entry exists with the exact case name, allocate new dentry with | |
2107 | * the exact case, and return the spliced entry. | |
2108 | */ | |
e45b590b | 2109 | struct dentry *d_add_ci(struct dentry *dentry, struct inode *inode, |
9403540c BN |
2110 | struct qstr *name) |
2111 | { | |
d9171b93 | 2112 | struct dentry *found, *res; |
9403540c | 2113 | |
b6520c81 CH |
2114 | /* |
2115 | * First check if a dentry matching the name already exists, | |
2116 | * if not go ahead and create it now. | |
2117 | */ | |
9403540c | 2118 | found = d_hash_and_lookup(dentry->d_parent, name); |
d9171b93 AV |
2119 | if (found) { |
2120 | iput(inode); | |
2121 | return found; | |
2122 | } | |
2123 | if (d_in_lookup(dentry)) { | |
2124 | found = d_alloc_parallel(dentry->d_parent, name, | |
2125 | dentry->d_wait); | |
2126 | if (IS_ERR(found) || !d_in_lookup(found)) { | |
2127 | iput(inode); | |
2128 | return found; | |
9403540c | 2129 | } |
d9171b93 AV |
2130 | } else { |
2131 | found = d_alloc(dentry->d_parent, name); | |
2132 | if (!found) { | |
2133 | iput(inode); | |
2134 | return ERR_PTR(-ENOMEM); | |
2135 | } | |
2136 | } | |
2137 | res = d_splice_alias(inode, found); | |
2138 | if (res) { | |
40a3cb0d | 2139 | d_lookup_done(found); |
d9171b93 AV |
2140 | dput(found); |
2141 | return res; | |
9403540c | 2142 | } |
4f522a24 | 2143 | return found; |
9403540c | 2144 | } |
ec4f8605 | 2145 | EXPORT_SYMBOL(d_add_ci); |
1da177e4 | 2146 | |
4f48d5da XL |
2147 | /** |
2148 | * d_same_name - compare dentry name with case-exact name | |
4f48d5da | 2149 | * @dentry: the negative dentry that was passed to the parent's lookup func |
1e756248 | 2150 | * @parent: parent dentry |
4f48d5da XL |
2151 | * @name: the case-exact name to be associated with the returned dentry |
2152 | * | |
2153 | * Return: true if names are same, or false | |
2154 | */ | |
2155 | bool d_same_name(const struct dentry *dentry, const struct dentry *parent, | |
2156 | const struct qstr *name) | |
12f8ad4b | 2157 | { |
d4c91a8f AV |
2158 | if (likely(!(parent->d_flags & DCACHE_OP_COMPARE))) { |
2159 | if (dentry->d_name.len != name->len) | |
2160 | return false; | |
2161 | return dentry_cmp(dentry, name->name, name->len) == 0; | |
12f8ad4b | 2162 | } |
6fa67e70 | 2163 | return parent->d_op->d_compare(dentry, |
d4c91a8f AV |
2164 | dentry->d_name.len, dentry->d_name.name, |
2165 | name) == 0; | |
12f8ad4b | 2166 | } |
4f48d5da | 2167 | EXPORT_SYMBOL_GPL(d_same_name); |
12f8ad4b | 2168 | |
ae2a8236 LT |
2169 | /* |
2170 | * This is __d_lookup_rcu() when the parent dentry has | |
2171 | * DCACHE_OP_COMPARE, which makes things much nastier. | |
2172 | */ | |
2173 | static noinline struct dentry *__d_lookup_rcu_op_compare( | |
2174 | const struct dentry *parent, | |
2175 | const struct qstr *name, | |
2176 | unsigned *seqp) | |
2177 | { | |
2178 | u64 hashlen = name->hash_len; | |
e60cc611 | 2179 | struct hlist_bl_head *b = d_hash(hashlen); |
ae2a8236 LT |
2180 | struct hlist_bl_node *node; |
2181 | struct dentry *dentry; | |
2182 | ||
2183 | hlist_bl_for_each_entry_rcu(dentry, node, b, d_hash) { | |
2184 | int tlen; | |
2185 | const char *tname; | |
2186 | unsigned seq; | |
2187 | ||
2188 | seqretry: | |
2189 | seq = raw_seqcount_begin(&dentry->d_seq); | |
2190 | if (dentry->d_parent != parent) | |
2191 | continue; | |
2192 | if (d_unhashed(dentry)) | |
2193 | continue; | |
2194 | if (dentry->d_name.hash != hashlen_hash(hashlen)) | |
2195 | continue; | |
2196 | tlen = dentry->d_name.len; | |
2197 | tname = dentry->d_name.name; | |
2198 | /* we want a consistent (name,len) pair */ | |
2199 | if (read_seqcount_retry(&dentry->d_seq, seq)) { | |
2200 | cpu_relax(); | |
2201 | goto seqretry; | |
2202 | } | |
2203 | if (parent->d_op->d_compare(dentry, tlen, tname, name) != 0) | |
2204 | continue; | |
2205 | *seqp = seq; | |
2206 | return dentry; | |
2207 | } | |
2208 | return NULL; | |
2209 | } | |
2210 | ||
31e6b01f NP |
2211 | /** |
2212 | * __d_lookup_rcu - search for a dentry (racy, store-free) | |
2213 | * @parent: parent dentry | |
2214 | * @name: qstr of name we wish to find | |
1f1e6e52 | 2215 | * @seqp: returns d_seq value at the point where the dentry was found |
31e6b01f NP |
2216 | * Returns: dentry, or NULL |
2217 | * | |
2218 | * __d_lookup_rcu is the dcache lookup function for rcu-walk name | |
2219 | * resolution (store-free path walking) design described in | |
2220 | * Documentation/filesystems/path-lookup.txt. | |
2221 | * | |
2222 | * This is not to be used outside core vfs. | |
2223 | * | |
2224 | * __d_lookup_rcu must only be used in rcu-walk mode, ie. with vfsmount lock | |
2225 | * held, and rcu_read_lock held. The returned dentry must not be stored into | |
2226 | * without taking d_lock and checking d_seq sequence count against @seq | |
2227 | * returned here. | |
2228 | * | |
31e6b01f NP |
2229 | * Alternatively, __d_lookup_rcu may be called again to look up the child of |
2230 | * the returned dentry, so long as its parent's seqlock is checked after the | |
2231 | * child is looked up. Thus, an interlocking stepping of sequence lock checks | |
2232 | * is formed, giving integrity down the path walk. | |
12f8ad4b LT |
2233 | * |
2234 | * NOTE! The caller *has* to check the resulting dentry against the sequence | |
2235 | * number we've returned before using any of the resulting dentry state! | |
31e6b01f | 2236 | */ |
8966be90 LT |
2237 | struct dentry *__d_lookup_rcu(const struct dentry *parent, |
2238 | const struct qstr *name, | |
da53be12 | 2239 | unsigned *seqp) |
31e6b01f | 2240 | { |
26fe5750 | 2241 | u64 hashlen = name->hash_len; |
31e6b01f | 2242 | const unsigned char *str = name->name; |
e60cc611 | 2243 | struct hlist_bl_head *b = d_hash(hashlen); |
ceb5bdc2 | 2244 | struct hlist_bl_node *node; |
31e6b01f NP |
2245 | struct dentry *dentry; |
2246 | ||
2247 | /* | |
2248 | * Note: There is significant duplication with __d_lookup_rcu which is | |
2249 | * required to prevent single threaded performance regressions | |
2250 | * especially on architectures where smp_rmb (in seqcounts) are costly. | |
2251 | * Keep the two functions in sync. | |
2252 | */ | |
2253 | ||
ae2a8236 LT |
2254 | if (unlikely(parent->d_flags & DCACHE_OP_COMPARE)) |
2255 | return __d_lookup_rcu_op_compare(parent, name, seqp); | |
2256 | ||
31e6b01f NP |
2257 | /* |
2258 | * The hash list is protected using RCU. | |
2259 | * | |
2260 | * Carefully use d_seq when comparing a candidate dentry, to avoid | |
2261 | * races with d_move(). | |
2262 | * | |
2263 | * It is possible that concurrent renames can mess up our list | |
2264 | * walk here and result in missing our dentry, resulting in the | |
2265 | * false-negative result. d_lookup() protects against concurrent | |
2266 | * renames using rename_lock seqlock. | |
2267 | * | |
b0a4bb83 | 2268 | * See Documentation/filesystems/path-lookup.txt for more details. |
31e6b01f | 2269 | */ |
b07ad996 | 2270 | hlist_bl_for_each_entry_rcu(dentry, node, b, d_hash) { |
8966be90 | 2271 | unsigned seq; |
31e6b01f | 2272 | |
12f8ad4b LT |
2273 | /* |
2274 | * The dentry sequence count protects us from concurrent | |
da53be12 | 2275 | * renames, and thus protects parent and name fields. |
12f8ad4b LT |
2276 | * |
2277 | * The caller must perform a seqcount check in order | |
da53be12 | 2278 | * to do anything useful with the returned dentry. |
12f8ad4b LT |
2279 | * |
2280 | * NOTE! We do a "raw" seqcount_begin here. That means that | |
2281 | * we don't wait for the sequence count to stabilize if it | |
2282 | * is in the middle of a sequence change. If we do the slow | |
2283 | * dentry compare, we will do seqretries until it is stable, | |
2284 | * and if we end up with a successful lookup, we actually | |
2285 | * want to exit RCU lookup anyway. | |
d4c91a8f AV |
2286 | * |
2287 | * Note that raw_seqcount_begin still *does* smp_rmb(), so | |
2288 | * we are still guaranteed NUL-termination of ->d_name.name. | |
12f8ad4b LT |
2289 | */ |
2290 | seq = raw_seqcount_begin(&dentry->d_seq); | |
31e6b01f NP |
2291 | if (dentry->d_parent != parent) |
2292 | continue; | |
2e321806 LT |
2293 | if (d_unhashed(dentry)) |
2294 | continue; | |
ae2a8236 LT |
2295 | if (dentry->d_name.hash_len != hashlen) |
2296 | continue; | |
2297 | if (dentry_cmp(dentry, str, hashlen_len(hashlen)) != 0) | |
2298 | continue; | |
da53be12 | 2299 | *seqp = seq; |
d4c91a8f | 2300 | return dentry; |
31e6b01f NP |
2301 | } |
2302 | return NULL; | |
2303 | } | |
2304 | ||
1da177e4 LT |
2305 | /** |
2306 | * d_lookup - search for a dentry | |
2307 | * @parent: parent dentry | |
2308 | * @name: qstr of name we wish to find | |
b04f784e | 2309 | * Returns: dentry, or NULL |
1da177e4 | 2310 | * |
b04f784e NP |
2311 | * d_lookup searches the children of the parent dentry for the name in |
2312 | * question. If the dentry is found its reference count is incremented and the | |
2313 | * dentry is returned. The caller must use dput to free the entry when it has | |
2314 | * finished using it. %NULL is returned if the dentry does not exist. | |
1da177e4 | 2315 | */ |
da2d8455 | 2316 | struct dentry *d_lookup(const struct dentry *parent, const struct qstr *name) |
1da177e4 | 2317 | { |
31e6b01f | 2318 | struct dentry *dentry; |
949854d0 | 2319 | unsigned seq; |
1da177e4 | 2320 | |
b8314f93 DY |
2321 | do { |
2322 | seq = read_seqbegin(&rename_lock); | |
2323 | dentry = __d_lookup(parent, name); | |
2324 | if (dentry) | |
1da177e4 LT |
2325 | break; |
2326 | } while (read_seqretry(&rename_lock, seq)); | |
2327 | return dentry; | |
2328 | } | |
ec4f8605 | 2329 | EXPORT_SYMBOL(d_lookup); |
1da177e4 | 2330 | |
31e6b01f | 2331 | /** |
b04f784e NP |
2332 | * __d_lookup - search for a dentry (racy) |
2333 | * @parent: parent dentry | |
2334 | * @name: qstr of name we wish to find | |
2335 | * Returns: dentry, or NULL | |
2336 | * | |
2337 | * __d_lookup is like d_lookup, however it may (rarely) return a | |
2338 | * false-negative result due to unrelated rename activity. | |
2339 | * | |
2340 | * __d_lookup is slightly faster by avoiding rename_lock read seqlock, | |
2341 | * however it must be used carefully, eg. with a following d_lookup in | |
2342 | * the case of failure. | |
2343 | * | |
2344 | * __d_lookup callers must be commented. | |
2345 | */ | |
a713ca2a | 2346 | struct dentry *__d_lookup(const struct dentry *parent, const struct qstr *name) |
1da177e4 | 2347 | { |
1da177e4 | 2348 | unsigned int hash = name->hash; |
8387ff25 | 2349 | struct hlist_bl_head *b = d_hash(hash); |
ceb5bdc2 | 2350 | struct hlist_bl_node *node; |
31e6b01f | 2351 | struct dentry *found = NULL; |
665a7583 | 2352 | struct dentry *dentry; |
1da177e4 | 2353 | |
31e6b01f NP |
2354 | /* |
2355 | * Note: There is significant duplication with __d_lookup_rcu which is | |
2356 | * required to prevent single threaded performance regressions | |
2357 | * especially on architectures where smp_rmb (in seqcounts) are costly. | |
2358 | * Keep the two functions in sync. | |
2359 | */ | |
2360 | ||
b04f784e NP |
2361 | /* |
2362 | * The hash list is protected using RCU. | |
2363 | * | |
2364 | * Take d_lock when comparing a candidate dentry, to avoid races | |
2365 | * with d_move(). | |
2366 | * | |
2367 | * It is possible that concurrent renames can mess up our list | |
2368 | * walk here and result in missing our dentry, resulting in the | |
2369 | * false-negative result. d_lookup() protects against concurrent | |
2370 | * renames using rename_lock seqlock. | |
2371 | * | |
b0a4bb83 | 2372 | * See Documentation/filesystems/path-lookup.txt for more details. |
b04f784e | 2373 | */ |
1da177e4 LT |
2374 | rcu_read_lock(); |
2375 | ||
b07ad996 | 2376 | hlist_bl_for_each_entry_rcu(dentry, node, b, d_hash) { |
1da177e4 | 2377 | |
1da177e4 LT |
2378 | if (dentry->d_name.hash != hash) |
2379 | continue; | |
1da177e4 LT |
2380 | |
2381 | spin_lock(&dentry->d_lock); | |
1da177e4 LT |
2382 | if (dentry->d_parent != parent) |
2383 | goto next; | |
d0185c08 LT |
2384 | if (d_unhashed(dentry)) |
2385 | goto next; | |
2386 | ||
d4c91a8f AV |
2387 | if (!d_same_name(dentry, parent, name)) |
2388 | goto next; | |
1da177e4 | 2389 | |
98474236 | 2390 | dentry->d_lockref.count++; |
d0185c08 | 2391 | found = dentry; |
1da177e4 LT |
2392 | spin_unlock(&dentry->d_lock); |
2393 | break; | |
2394 | next: | |
2395 | spin_unlock(&dentry->d_lock); | |
2396 | } | |
2397 | rcu_read_unlock(); | |
2398 | ||
2399 | return found; | |
2400 | } | |
2401 | ||
3e7e241f EB |
2402 | /** |
2403 | * d_hash_and_lookup - hash the qstr then search for a dentry | |
2404 | * @dir: Directory to search in | |
2405 | * @name: qstr of name we wish to find | |
2406 | * | |
4f522a24 | 2407 | * On lookup failure NULL is returned; on bad name - ERR_PTR(-error) |
3e7e241f EB |
2408 | */ |
2409 | struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name) | |
2410 | { | |
3e7e241f EB |
2411 | /* |
2412 | * Check for a fs-specific hash function. Note that we must | |
2413 | * calculate the standard hash first, as the d_op->d_hash() | |
2414 | * routine may choose to leave the hash value unchanged. | |
2415 | */ | |
8387ff25 | 2416 | name->hash = full_name_hash(dir, name->name, name->len); |
fb045adb | 2417 | if (dir->d_flags & DCACHE_OP_HASH) { |
da53be12 | 2418 | int err = dir->d_op->d_hash(dir, name); |
4f522a24 AV |
2419 | if (unlikely(err < 0)) |
2420 | return ERR_PTR(err); | |
3e7e241f | 2421 | } |
4f522a24 | 2422 | return d_lookup(dir, name); |
3e7e241f EB |
2423 | } |
2424 | ||
1da177e4 LT |
2425 | /* |
2426 | * When a file is deleted, we have two options: | |
2427 | * - turn this dentry into a negative dentry | |
2428 | * - unhash this dentry and free it. | |
2429 | * | |
2430 | * Usually, we want to just turn this into | |
4a4be1ad LT |
2431 | * a negative dentry, but if anybody else is |
2432 | * currently using the dentry or the inode | |
2433 | * we can't do that and we fall back on removing | |
2434 | * it from the hash queues and waiting for | |
2435 | * it to be deleted later when it has no users | |
1da177e4 LT |
2436 | */ |
2437 | ||
2438 | /** | |
2439 | * d_delete - delete a dentry | |
2440 | * @dentry: The dentry to delete | |
2441 | * | |
4a4be1ad LT |
2442 | * Turn the dentry into a negative dentry if possible, otherwise |
2443 | * remove it from the hash queues so it can be deleted later | |
1da177e4 LT |
2444 | */ |
2445 | ||
2446 | void d_delete(struct dentry * dentry) | |
2447 | { | |
c19457f0 | 2448 | struct inode *inode = dentry->d_inode; |
c19457f0 AV |
2449 | |
2450 | spin_lock(&inode->i_lock); | |
2451 | spin_lock(&dentry->d_lock); | |
1da177e4 LT |
2452 | /* |
2453 | * Are we the only user? | |
2454 | */ | |
98474236 | 2455 | if (dentry->d_lockref.count == 1) { |
e6957c99 YS |
2456 | if (dentry_negative_policy) |
2457 | __d_drop(dentry); | |
13e3c5e5 | 2458 | dentry->d_flags &= ~DCACHE_CANT_MOUNT; |
31e6b01f | 2459 | dentry_unlink_inode(dentry); |
c19457f0 | 2460 | } else { |
4a4be1ad | 2461 | __d_drop(dentry); |
c19457f0 AV |
2462 | spin_unlock(&dentry->d_lock); |
2463 | spin_unlock(&inode->i_lock); | |
2464 | } | |
1da177e4 | 2465 | } |
ec4f8605 | 2466 | EXPORT_SYMBOL(d_delete); |
1da177e4 | 2467 | |
15d3c589 | 2468 | static void __d_rehash(struct dentry *entry) |
1da177e4 | 2469 | { |
15d3c589 | 2470 | struct hlist_bl_head *b = d_hash(entry->d_name.hash); |
61647823 | 2471 | |
1879fd6a | 2472 | hlist_bl_lock(b); |
b07ad996 | 2473 | hlist_bl_add_head_rcu(&entry->d_hash, b); |
1879fd6a | 2474 | hlist_bl_unlock(b); |
1da177e4 LT |
2475 | } |
2476 | ||
2477 | /** | |
2478 | * d_rehash - add an entry back to the hash | |
2479 | * @entry: dentry to add to the hash | |
2480 | * | |
2481 | * Adds a dentry to the hash according to its name. | |
2482 | */ | |
2483 | ||
2484 | void d_rehash(struct dentry * entry) | |
2485 | { | |
1da177e4 | 2486 | spin_lock(&entry->d_lock); |
15d3c589 | 2487 | __d_rehash(entry); |
1da177e4 | 2488 | spin_unlock(&entry->d_lock); |
1da177e4 | 2489 | } |
ec4f8605 | 2490 | EXPORT_SYMBOL(d_rehash); |
1da177e4 | 2491 | |
84e710da AV |
2492 | static inline unsigned start_dir_add(struct inode *dir) |
2493 | { | |
93f6d4e1 | 2494 | preempt_disable_nested(); |
84e710da AV |
2495 | for (;;) { |
2496 | unsigned n = dir->i_dir_seq; | |
2497 | if (!(n & 1) && cmpxchg(&dir->i_dir_seq, n, n + 1) == n) | |
2498 | return n; | |
2499 | cpu_relax(); | |
2500 | } | |
2501 | } | |
2502 | ||
50417d22 SAS |
2503 | static inline void end_dir_add(struct inode *dir, unsigned int n, |
2504 | wait_queue_head_t *d_wait) | |
84e710da AV |
2505 | { |
2506 | smp_store_release(&dir->i_dir_seq, n + 2); | |
93f6d4e1 | 2507 | preempt_enable_nested(); |
008a746a MG |
2508 | if (wq_has_sleeper(d_wait)) |
2509 | wake_up_all(d_wait); | |
84e710da AV |
2510 | } |
2511 | ||
d9171b93 AV |
2512 | static void d_wait_lookup(struct dentry *dentry) |
2513 | { | |
2514 | if (d_in_lookup(dentry)) { | |
2515 | DECLARE_WAITQUEUE(wait, current); | |
2516 | add_wait_queue(dentry->d_wait, &wait); | |
2517 | do { | |
2518 | set_current_state(TASK_UNINTERRUPTIBLE); | |
2519 | spin_unlock(&dentry->d_lock); | |
2520 | schedule(); | |
2521 | spin_lock(&dentry->d_lock); | |
2522 | } while (d_in_lookup(dentry)); | |
2523 | } | |
2524 | } | |
2525 | ||
94bdd655 | 2526 | struct dentry *d_alloc_parallel(struct dentry *parent, |
d9171b93 AV |
2527 | const struct qstr *name, |
2528 | wait_queue_head_t *wq) | |
94bdd655 | 2529 | { |
94bdd655 | 2530 | unsigned int hash = name->hash; |
94bdd655 AV |
2531 | struct hlist_bl_head *b = in_lookup_hash(parent, hash); |
2532 | struct hlist_bl_node *node; | |
2533 | struct dentry *new = d_alloc(parent, name); | |
2534 | struct dentry *dentry; | |
2535 | unsigned seq, r_seq, d_seq; | |
2536 | ||
2537 | if (unlikely(!new)) | |
2538 | return ERR_PTR(-ENOMEM); | |
2539 | ||
2540 | retry: | |
2541 | rcu_read_lock(); | |
015555fd | 2542 | seq = smp_load_acquire(&parent->d_inode->i_dir_seq); |
94bdd655 AV |
2543 | r_seq = read_seqbegin(&rename_lock); |
2544 | dentry = __d_lookup_rcu(parent, name, &d_seq); | |
2545 | if (unlikely(dentry)) { | |
2546 | if (!lockref_get_not_dead(&dentry->d_lockref)) { | |
2547 | rcu_read_unlock(); | |
2548 | goto retry; | |
2549 | } | |
2550 | if (read_seqcount_retry(&dentry->d_seq, d_seq)) { | |
2551 | rcu_read_unlock(); | |
2552 | dput(dentry); | |
2553 | goto retry; | |
2554 | } | |
2555 | rcu_read_unlock(); | |
2556 | dput(new); | |
2557 | return dentry; | |
2558 | } | |
2559 | if (unlikely(read_seqretry(&rename_lock, r_seq))) { | |
2560 | rcu_read_unlock(); | |
2561 | goto retry; | |
2562 | } | |
015555fd WD |
2563 | |
2564 | if (unlikely(seq & 1)) { | |
2565 | rcu_read_unlock(); | |
2566 | goto retry; | |
2567 | } | |
2568 | ||
94bdd655 | 2569 | hlist_bl_lock(b); |
8cc07c80 | 2570 | if (unlikely(READ_ONCE(parent->d_inode->i_dir_seq) != seq)) { |
94bdd655 AV |
2571 | hlist_bl_unlock(b); |
2572 | rcu_read_unlock(); | |
2573 | goto retry; | |
2574 | } | |
94bdd655 AV |
2575 | /* |
2576 | * No changes for the parent since the beginning of d_lookup(). | |
2577 | * Since all removals from the chain happen with hlist_bl_lock(), | |
2578 | * any potential in-lookup matches are going to stay here until | |
2579 | * we unlock the chain. All fields are stable in everything | |
2580 | * we encounter. | |
2581 | */ | |
2582 | hlist_bl_for_each_entry(dentry, node, b, d_u.d_in_lookup_hash) { | |
2583 | if (dentry->d_name.hash != hash) | |
2584 | continue; | |
2585 | if (dentry->d_parent != parent) | |
2586 | continue; | |
d4c91a8f AV |
2587 | if (!d_same_name(dentry, parent, name)) |
2588 | continue; | |
94bdd655 | 2589 | hlist_bl_unlock(b); |
e7d6ef97 AV |
2590 | /* now we can try to grab a reference */ |
2591 | if (!lockref_get_not_dead(&dentry->d_lockref)) { | |
2592 | rcu_read_unlock(); | |
2593 | goto retry; | |
2594 | } | |
2595 | ||
2596 | rcu_read_unlock(); | |
2597 | /* | |
2598 | * somebody is likely to be still doing lookup for it; | |
2599 | * wait for them to finish | |
2600 | */ | |
d9171b93 AV |
2601 | spin_lock(&dentry->d_lock); |
2602 | d_wait_lookup(dentry); | |
2603 | /* | |
2604 | * it's not in-lookup anymore; in principle we should repeat | |
2605 | * everything from dcache lookup, but it's likely to be what | |
2606 | * d_lookup() would've found anyway. If it is, just return it; | |
2607 | * otherwise we really have to repeat the whole thing. | |
2608 | */ | |
2609 | if (unlikely(dentry->d_name.hash != hash)) | |
2610 | goto mismatch; | |
2611 | if (unlikely(dentry->d_parent != parent)) | |
2612 | goto mismatch; | |
2613 | if (unlikely(d_unhashed(dentry))) | |
2614 | goto mismatch; | |
d4c91a8f AV |
2615 | if (unlikely(!d_same_name(dentry, parent, name))) |
2616 | goto mismatch; | |
d9171b93 AV |
2617 | /* OK, it *is* a hashed match; return it */ |
2618 | spin_unlock(&dentry->d_lock); | |
94bdd655 AV |
2619 | dput(new); |
2620 | return dentry; | |
2621 | } | |
e7d6ef97 | 2622 | rcu_read_unlock(); |
94bdd655 AV |
2623 | /* we can't take ->d_lock here; it's OK, though. */ |
2624 | new->d_flags |= DCACHE_PAR_LOOKUP; | |
d9171b93 | 2625 | new->d_wait = wq; |
f9f677c5 | 2626 | hlist_bl_add_head(&new->d_u.d_in_lookup_hash, b); |
94bdd655 AV |
2627 | hlist_bl_unlock(b); |
2628 | return new; | |
d9171b93 AV |
2629 | mismatch: |
2630 | spin_unlock(&dentry->d_lock); | |
2631 | dput(dentry); | |
2632 | goto retry; | |
94bdd655 AV |
2633 | } |
2634 | EXPORT_SYMBOL(d_alloc_parallel); | |
2635 | ||
45f78b0a SAS |
2636 | /* |
2637 | * - Unhash the dentry | |
2638 | * - Retrieve and clear the waitqueue head in dentry | |
2639 | * - Return the waitqueue head | |
2640 | */ | |
2641 | static wait_queue_head_t *__d_lookup_unhash(struct dentry *dentry) | |
85c7f810 | 2642 | { |
45f78b0a SAS |
2643 | wait_queue_head_t *d_wait; |
2644 | struct hlist_bl_head *b; | |
2645 | ||
2646 | lockdep_assert_held(&dentry->d_lock); | |
2647 | ||
2648 | b = in_lookup_hash(dentry->d_parent, dentry->d_name.hash); | |
94bdd655 | 2649 | hlist_bl_lock(b); |
85c7f810 | 2650 | dentry->d_flags &= ~DCACHE_PAR_LOOKUP; |
94bdd655 | 2651 | __hlist_bl_del(&dentry->d_u.d_in_lookup_hash); |
45f78b0a | 2652 | d_wait = dentry->d_wait; |
d9171b93 | 2653 | dentry->d_wait = NULL; |
94bdd655 AV |
2654 | hlist_bl_unlock(b); |
2655 | INIT_HLIST_NODE(&dentry->d_u.d_alias); | |
d9171b93 | 2656 | INIT_LIST_HEAD(&dentry->d_lru); |
45f78b0a SAS |
2657 | return d_wait; |
2658 | } | |
2659 | ||
2660 | void __d_lookup_unhash_wake(struct dentry *dentry) | |
2661 | { | |
2662 | spin_lock(&dentry->d_lock); | |
2663 | wake_up_all(__d_lookup_unhash(dentry)); | |
2664 | spin_unlock(&dentry->d_lock); | |
85c7f810 | 2665 | } |
45f78b0a | 2666 | EXPORT_SYMBOL(__d_lookup_unhash_wake); |
ed782b5a AV |
2667 | |
2668 | /* inode->i_lock held if inode is non-NULL */ | |
2669 | ||
2670 | static inline void __d_add(struct dentry *dentry, struct inode *inode) | |
2671 | { | |
45f78b0a | 2672 | wait_queue_head_t *d_wait; |
84e710da AV |
2673 | struct inode *dir = NULL; |
2674 | unsigned n; | |
0568d705 | 2675 | spin_lock(&dentry->d_lock); |
84e710da AV |
2676 | if (unlikely(d_in_lookup(dentry))) { |
2677 | dir = dentry->d_parent->d_inode; | |
2678 | n = start_dir_add(dir); | |
45f78b0a | 2679 | d_wait = __d_lookup_unhash(dentry); |
84e710da | 2680 | } |
ed782b5a | 2681 | if (inode) { |
0568d705 AV |
2682 | unsigned add_flags = d_flags_for_inode(inode); |
2683 | hlist_add_head(&dentry->d_u.d_alias, &inode->i_dentry); | |
2684 | raw_write_seqcount_begin(&dentry->d_seq); | |
2685 | __d_set_inode_and_type(dentry, inode, add_flags); | |
2686 | raw_write_seqcount_end(&dentry->d_seq); | |
affda484 | 2687 | fsnotify_update_flags(dentry); |
ed782b5a | 2688 | } |
15d3c589 | 2689 | __d_rehash(dentry); |
84e710da | 2690 | if (dir) |
50417d22 | 2691 | end_dir_add(dir, n, d_wait); |
0568d705 AV |
2692 | spin_unlock(&dentry->d_lock); |
2693 | if (inode) | |
2694 | spin_unlock(&inode->i_lock); | |
ed782b5a AV |
2695 | } |
2696 | ||
34d0d19d AV |
2697 | /** |
2698 | * d_add - add dentry to hash queues | |
2699 | * @entry: dentry to add | |
2700 | * @inode: The inode to attach to this dentry | |
2701 | * | |
2702 | * This adds the entry to the hash queues and initializes @inode. | |
2703 | * The entry was actually filled in earlier during d_alloc(). | |
2704 | */ | |
2705 | ||
2706 | void d_add(struct dentry *entry, struct inode *inode) | |
2707 | { | |
b9680917 AV |
2708 | if (inode) { |
2709 | security_d_instantiate(entry, inode); | |
ed782b5a | 2710 | spin_lock(&inode->i_lock); |
b9680917 | 2711 | } |
ed782b5a | 2712 | __d_add(entry, inode); |
34d0d19d AV |
2713 | } |
2714 | EXPORT_SYMBOL(d_add); | |
2715 | ||
8d85b484 | 2716 | static void swap_names(struct dentry *dentry, struct dentry *target) |
1da177e4 | 2717 | { |
8d85b484 AV |
2718 | if (unlikely(dname_external(target))) { |
2719 | if (unlikely(dname_external(dentry))) { | |
1da177e4 LT |
2720 | /* |
2721 | * Both external: swap the pointers | |
2722 | */ | |
9a8d5bb4 | 2723 | swap(target->d_name.name, dentry->d_name.name); |
1da177e4 LT |
2724 | } else { |
2725 | /* | |
2726 | * dentry:internal, target:external. Steal target's | |
2727 | * storage and make target internal. | |
2728 | */ | |
2729 | dentry->d_name.name = target->d_name.name; | |
58cf9c38 AV |
2730 | target->d_shortname = dentry->d_shortname; |
2731 | target->d_name.name = target->d_shortname.string; | |
1da177e4 LT |
2732 | } |
2733 | } else { | |
8d85b484 | 2734 | if (unlikely(dname_external(dentry))) { |
1da177e4 LT |
2735 | /* |
2736 | * dentry:external, target:internal. Give dentry's | |
2737 | * storage to target and make dentry internal | |
2738 | */ | |
1da177e4 | 2739 | target->d_name.name = dentry->d_name.name; |
58cf9c38 AV |
2740 | dentry->d_shortname = target->d_shortname; |
2741 | dentry->d_name.name = dentry->d_shortname.string; | |
1da177e4 LT |
2742 | } else { |
2743 | /* | |
da1ce067 | 2744 | * Both are internal. |
1da177e4 | 2745 | */ |
58cf9c38 AV |
2746 | for (int i = 0; i < DNAME_INLINE_WORDS; i++) |
2747 | swap(dentry->d_shortname.words[i], | |
2748 | target->d_shortname.words[i]); | |
1da177e4 LT |
2749 | } |
2750 | } | |
a28ddb87 | 2751 | swap(dentry->d_name.hash_len, target->d_name.hash_len); |
1da177e4 LT |
2752 | } |
2753 | ||
8d85b484 AV |
2754 | static void copy_name(struct dentry *dentry, struct dentry *target) |
2755 | { | |
2756 | struct external_name *old_name = NULL; | |
2757 | if (unlikely(dname_external(dentry))) | |
2758 | old_name = external_name(dentry); | |
2759 | if (unlikely(dname_external(target))) { | |
95a4ccbb | 2760 | atomic_inc(&external_name(target)->count); |
8d85b484 AV |
2761 | dentry->d_name = target->d_name; |
2762 | } else { | |
58cf9c38 AV |
2763 | dentry->d_shortname = target->d_shortname; |
2764 | dentry->d_name.name = dentry->d_shortname.string; | |
8d85b484 AV |
2765 | dentry->d_name.hash_len = target->d_name.hash_len; |
2766 | } | |
95a4ccbb AV |
2767 | if (old_name && likely(atomic_dec_and_test(&old_name->count))) |
2768 | kfree_rcu(old_name, head); | |
8d85b484 AV |
2769 | } |
2770 | ||
9eaef27b | 2771 | /* |
18367501 | 2772 | * __d_move - move a dentry |
1da177e4 LT |
2773 | * @dentry: entry to move |
2774 | * @target: new dentry | |
da1ce067 | 2775 | * @exchange: exchange the two dentries |
1da177e4 LT |
2776 | * |
2777 | * Update the dcache to reflect the move of a file name. Negative | |
c46c8877 JL |
2778 | * dcache entries should not be moved in this way. Caller must hold |
2779 | * rename_lock, the i_mutex of the source and target directories, | |
2780 | * and the sb->s_vfs_rename_mutex if they differ. See lock_rename(). | |
1da177e4 | 2781 | */ |
da1ce067 MS |
2782 | static void __d_move(struct dentry *dentry, struct dentry *target, |
2783 | bool exchange) | |
1da177e4 | 2784 | { |
42177007 | 2785 | struct dentry *old_parent, *p; |
45f78b0a | 2786 | wait_queue_head_t *d_wait; |
84e710da AV |
2787 | struct inode *dir = NULL; |
2788 | unsigned n; | |
1da177e4 | 2789 | |
42177007 AV |
2790 | WARN_ON(!dentry->d_inode); |
2791 | if (WARN_ON(dentry == target)) | |
2792 | return; | |
2793 | ||
2fd6b7f5 | 2794 | BUG_ON(d_ancestor(target, dentry)); |
42177007 AV |
2795 | old_parent = dentry->d_parent; |
2796 | p = d_ancestor(old_parent, target); | |
2797 | if (IS_ROOT(dentry)) { | |
2798 | BUG_ON(p); | |
2799 | spin_lock(&target->d_parent->d_lock); | |
2800 | } else if (!p) { | |
2801 | /* target is not a descendent of dentry->d_parent */ | |
2802 | spin_lock(&target->d_parent->d_lock); | |
2803 | spin_lock_nested(&old_parent->d_lock, DENTRY_D_LOCK_NESTED); | |
2804 | } else { | |
2805 | BUG_ON(p == dentry); | |
2806 | spin_lock(&old_parent->d_lock); | |
2807 | if (p != target) | |
2808 | spin_lock_nested(&target->d_parent->d_lock, | |
2809 | DENTRY_D_LOCK_NESTED); | |
2810 | } | |
2811 | spin_lock_nested(&dentry->d_lock, 2); | |
2812 | spin_lock_nested(&target->d_lock, 3); | |
2fd6b7f5 | 2813 | |
84e710da AV |
2814 | if (unlikely(d_in_lookup(target))) { |
2815 | dir = target->d_parent->d_inode; | |
2816 | n = start_dir_add(dir); | |
45f78b0a | 2817 | d_wait = __d_lookup_unhash(target); |
84e710da | 2818 | } |
1da177e4 | 2819 | |
31e6b01f | 2820 | write_seqcount_begin(&dentry->d_seq); |
1ca7d67c | 2821 | write_seqcount_begin_nested(&target->d_seq, DENTRY_D_LOCK_NESTED); |
31e6b01f | 2822 | |
15d3c589 | 2823 | /* unhash both */ |
0632a9ac AV |
2824 | if (!d_unhashed(dentry)) |
2825 | ___d_drop(dentry); | |
2826 | if (!d_unhashed(target)) | |
2827 | ___d_drop(target); | |
1da177e4 | 2828 | |
076515fc AV |
2829 | /* ... and switch them in the tree */ |
2830 | dentry->d_parent = target->d_parent; | |
2831 | if (!exchange) { | |
8d85b484 | 2832 | copy_name(dentry, target); |
61647823 | 2833 | target->d_hash.pprev = NULL; |
076515fc | 2834 | dentry->d_parent->d_lockref.count++; |
5467a68c | 2835 | if (dentry != old_parent) /* wasn't IS_ROOT */ |
076515fc | 2836 | WARN_ON(!--old_parent->d_lockref.count); |
1da177e4 | 2837 | } else { |
076515fc AV |
2838 | target->d_parent = old_parent; |
2839 | swap_names(dentry, target); | |
da549bdd AV |
2840 | if (!hlist_unhashed(&target->d_sib)) |
2841 | __hlist_del(&target->d_sib); | |
2842 | hlist_add_head(&target->d_sib, &target->d_parent->d_children); | |
076515fc AV |
2843 | __d_rehash(target); |
2844 | fsnotify_update_flags(target); | |
1da177e4 | 2845 | } |
da549bdd AV |
2846 | if (!hlist_unhashed(&dentry->d_sib)) |
2847 | __hlist_del(&dentry->d_sib); | |
2848 | hlist_add_head(&dentry->d_sib, &dentry->d_parent->d_children); | |
076515fc AV |
2849 | __d_rehash(dentry); |
2850 | fsnotify_update_flags(dentry); | |
0bf3d5c1 | 2851 | fscrypt_handle_d_move(dentry); |
1da177e4 | 2852 | |
31e6b01f NP |
2853 | write_seqcount_end(&target->d_seq); |
2854 | write_seqcount_end(&dentry->d_seq); | |
2855 | ||
84e710da | 2856 | if (dir) |
50417d22 | 2857 | end_dir_add(dir, n, d_wait); |
076515fc AV |
2858 | |
2859 | if (dentry->d_parent != old_parent) | |
2860 | spin_unlock(&dentry->d_parent->d_lock); | |
2861 | if (dentry != old_parent) | |
2862 | spin_unlock(&old_parent->d_lock); | |
2863 | spin_unlock(&target->d_lock); | |
2864 | spin_unlock(&dentry->d_lock); | |
18367501 AV |
2865 | } |
2866 | ||
2867 | /* | |
2868 | * d_move - move a dentry | |
2869 | * @dentry: entry to move | |
2870 | * @target: new dentry | |
2871 | * | |
2872 | * Update the dcache to reflect the move of a file name. Negative | |
c46c8877 JL |
2873 | * dcache entries should not be moved in this way. See the locking |
2874 | * requirements for __d_move. | |
18367501 AV |
2875 | */ |
2876 | void d_move(struct dentry *dentry, struct dentry *target) | |
2877 | { | |
2878 | write_seqlock(&rename_lock); | |
da1ce067 | 2879 | __d_move(dentry, target, false); |
1da177e4 | 2880 | write_sequnlock(&rename_lock); |
9eaef27b | 2881 | } |
ec4f8605 | 2882 | EXPORT_SYMBOL(d_move); |
1da177e4 | 2883 | |
da1ce067 MS |
2884 | /* |
2885 | * d_exchange - exchange two dentries | |
2886 | * @dentry1: first dentry | |
2887 | * @dentry2: second dentry | |
2888 | */ | |
2889 | void d_exchange(struct dentry *dentry1, struct dentry *dentry2) | |
2890 | { | |
2891 | write_seqlock(&rename_lock); | |
2892 | ||
2893 | WARN_ON(!dentry1->d_inode); | |
2894 | WARN_ON(!dentry2->d_inode); | |
2895 | WARN_ON(IS_ROOT(dentry1)); | |
2896 | WARN_ON(IS_ROOT(dentry2)); | |
2897 | ||
2898 | __d_move(dentry1, dentry2, true); | |
2899 | ||
2900 | write_sequnlock(&rename_lock); | |
2901 | } | |
2902 | ||
e2761a11 OH |
2903 | /** |
2904 | * d_ancestor - search for an ancestor | |
2905 | * @p1: ancestor dentry | |
2906 | * @p2: child dentry | |
2907 | * | |
2908 | * Returns the ancestor dentry of p2 which is a child of p1, if p1 is | |
2909 | * an ancestor of p2, else NULL. | |
9eaef27b | 2910 | */ |
e2761a11 | 2911 | struct dentry *d_ancestor(struct dentry *p1, struct dentry *p2) |
9eaef27b TM |
2912 | { |
2913 | struct dentry *p; | |
2914 | ||
871c0067 | 2915 | for (p = p2; !IS_ROOT(p); p = p->d_parent) { |
9eaef27b | 2916 | if (p->d_parent == p1) |
e2761a11 | 2917 | return p; |
9eaef27b | 2918 | } |
e2761a11 | 2919 | return NULL; |
9eaef27b TM |
2920 | } |
2921 | ||
2922 | /* | |
2923 | * This helper attempts to cope with remotely renamed directories | |
2924 | * | |
2925 | * It assumes that the caller is already holding | |
a03e283b | 2926 | * dentry->d_parent->d_inode->i_mutex, and rename_lock |
9eaef27b TM |
2927 | * |
2928 | * Note: If ever the locking in lock_rename() changes, then please | |
2929 | * remember to update this too... | |
9eaef27b | 2930 | */ |
ef69f050 | 2931 | static int __d_unalias(struct dentry *dentry, struct dentry *alias) |
9eaef27b | 2932 | { |
9902af79 AV |
2933 | struct mutex *m1 = NULL; |
2934 | struct rw_semaphore *m2 = NULL; | |
3d330dc1 | 2935 | int ret = -ESTALE; |
9eaef27b TM |
2936 | |
2937 | /* If alias and dentry share a parent, then no extra locks required */ | |
2938 | if (alias->d_parent == dentry->d_parent) | |
2939 | goto out_unalias; | |
2940 | ||
9eaef27b | 2941 | /* See lock_rename() */ |
9eaef27b TM |
2942 | if (!mutex_trylock(&dentry->d_sb->s_vfs_rename_mutex)) |
2943 | goto out_err; | |
2944 | m1 = &dentry->d_sb->s_vfs_rename_mutex; | |
9902af79 | 2945 | if (!inode_trylock_shared(alias->d_parent->d_inode)) |
9eaef27b | 2946 | goto out_err; |
9902af79 | 2947 | m2 = &alias->d_parent->d_inode->i_rwsem; |
9eaef27b | 2948 | out_unalias: |
902e09c8 | 2949 | if (alias->d_op && alias->d_op->d_unalias_trylock && |
30d61efe AV |
2950 | !alias->d_op->d_unalias_trylock(alias)) |
2951 | goto out_err; | |
8ed936b5 | 2952 | __d_move(alias, dentry, false); |
902e09c8 | 2953 | if (alias->d_op && alias->d_op->d_unalias_unlock) |
30d61efe | 2954 | alias->d_op->d_unalias_unlock(alias); |
b5ae6b15 | 2955 | ret = 0; |
9eaef27b | 2956 | out_err: |
9eaef27b | 2957 | if (m2) |
9902af79 | 2958 | up_read(m2); |
9eaef27b TM |
2959 | if (m1) |
2960 | mutex_unlock(m1); | |
2961 | return ret; | |
2962 | } | |
2963 | ||
3f70bd51 BF |
2964 | /** |
2965 | * d_splice_alias - splice a disconnected dentry into the tree if one exists | |
2966 | * @inode: the inode which may have a disconnected dentry | |
2967 | * @dentry: a negative dentry which we want to point to the inode. | |
2968 | * | |
da093a9b BF |
2969 | * If inode is a directory and has an IS_ROOT alias, then d_move that in |
2970 | * place of the given dentry and return it, else simply d_add the inode | |
2971 | * to the dentry and return NULL. | |
3f70bd51 | 2972 | * |
908790fa BF |
2973 | * If a non-IS_ROOT directory is found, the filesystem is corrupt, and |
2974 | * we should error out: directories can't have multiple aliases. | |
2975 | * | |
3f70bd51 BF |
2976 | * This is needed in the lookup routine of any filesystem that is exportable |
2977 | * (via knfsd) so that we can build dcache paths to directories effectively. | |
2978 | * | |
2979 | * If a dentry was found and moved, then it is returned. Otherwise NULL | |
2980 | * is returned. This matches the expected return value of ->lookup. | |
2981 | * | |
2982 | * Cluster filesystems may call this function with a negative, hashed dentry. | |
2983 | * In that case, we know that the inode will be a regular file, and also this | |
2984 | * will only occur during atomic_open. So we need to check for the dentry | |
2985 | * being already hashed only in the final case. | |
2986 | */ | |
2987 | struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry) | |
2988 | { | |
3f70bd51 BF |
2989 | if (IS_ERR(inode)) |
2990 | return ERR_CAST(inode); | |
2991 | ||
770bfad8 DH |
2992 | BUG_ON(!d_unhashed(dentry)); |
2993 | ||
de689f5e | 2994 | if (!inode) |
b5ae6b15 | 2995 | goto out; |
de689f5e | 2996 | |
b9680917 | 2997 | security_d_instantiate(dentry, inode); |
873feea0 | 2998 | spin_lock(&inode->i_lock); |
9eaef27b | 2999 | if (S_ISDIR(inode->i_mode)) { |
b5ae6b15 AV |
3000 | struct dentry *new = __d_find_any_alias(inode); |
3001 | if (unlikely(new)) { | |
a03e283b EB |
3002 | /* The reference to new ensures it remains an alias */ |
3003 | spin_unlock(&inode->i_lock); | |
18367501 | 3004 | write_seqlock(&rename_lock); |
b5ae6b15 AV |
3005 | if (unlikely(d_ancestor(new, dentry))) { |
3006 | write_sequnlock(&rename_lock); | |
b5ae6b15 AV |
3007 | dput(new); |
3008 | new = ERR_PTR(-ELOOP); | |
3009 | pr_warn_ratelimited( | |
3010 | "VFS: Lookup of '%s' in %s %s" | |
3011 | " would have caused loop\n", | |
3012 | dentry->d_name.name, | |
3013 | inode->i_sb->s_type->name, | |
3014 | inode->i_sb->s_id); | |
3015 | } else if (!IS_ROOT(new)) { | |
076515fc | 3016 | struct dentry *old_parent = dget(new->d_parent); |
ef69f050 | 3017 | int err = __d_unalias(dentry, new); |
18367501 | 3018 | write_sequnlock(&rename_lock); |
b5ae6b15 AV |
3019 | if (err) { |
3020 | dput(new); | |
3021 | new = ERR_PTR(err); | |
3022 | } | |
076515fc | 3023 | dput(old_parent); |
18367501 | 3024 | } else { |
b5ae6b15 AV |
3025 | __d_move(new, dentry, false); |
3026 | write_sequnlock(&rename_lock); | |
dd179946 | 3027 | } |
b5ae6b15 AV |
3028 | iput(inode); |
3029 | return new; | |
9eaef27b | 3030 | } |
770bfad8 | 3031 | } |
b5ae6b15 | 3032 | out: |
ed782b5a | 3033 | __d_add(dentry, inode); |
b5ae6b15 | 3034 | return NULL; |
770bfad8 | 3035 | } |
b5ae6b15 | 3036 | EXPORT_SYMBOL(d_splice_alias); |
770bfad8 | 3037 | |
1da177e4 LT |
3038 | /* |
3039 | * Test whether new_dentry is a subdirectory of old_dentry. | |
3040 | * | |
3041 | * Trivially implemented using the dcache structure | |
3042 | */ | |
3043 | ||
3044 | /** | |
3045 | * is_subdir - is new dentry a subdirectory of old_dentry | |
3046 | * @new_dentry: new dentry | |
3047 | * @old_dentry: old dentry | |
3048 | * | |
a6e5787f YB |
3049 | * Returns true if new_dentry is a subdirectory of the parent (at any depth). |
3050 | * Returns false otherwise. | |
1da177e4 LT |
3051 | * Caller must ensure that "new_dentry" is pinned before calling is_subdir() |
3052 | */ | |
3053 | ||
a6e5787f | 3054 | bool is_subdir(struct dentry *new_dentry, struct dentry *old_dentry) |
1da177e4 | 3055 | { |
391b59b0 | 3056 | bool subdir; |
949854d0 | 3057 | unsigned seq; |
1da177e4 | 3058 | |
e2761a11 | 3059 | if (new_dentry == old_dentry) |
a6e5787f | 3060 | return true; |
e2761a11 | 3061 | |
391b59b0 CB |
3062 | /* Access d_parent under rcu as d_move() may change it. */ |
3063 | rcu_read_lock(); | |
3064 | seq = read_seqbegin(&rename_lock); | |
3065 | subdir = d_ancestor(old_dentry, new_dentry); | |
3066 | /* Try lockless once... */ | |
3067 | if (read_seqretry(&rename_lock, seq)) { | |
3068 | /* ...else acquire lock for progress even on deep chains. */ | |
3069 | read_seqlock_excl(&rename_lock); | |
3070 | subdir = d_ancestor(old_dentry, new_dentry); | |
3071 | read_sequnlock_excl(&rename_lock); | |
3072 | } | |
3073 | rcu_read_unlock(); | |
3074 | return subdir; | |
1da177e4 | 3075 | } |
e8f9e5b7 | 3076 | EXPORT_SYMBOL(is_subdir); |
1da177e4 | 3077 | |
db14fc3a | 3078 | static enum d_walk_ret d_genocide_kill(void *data, struct dentry *dentry) |
1da177e4 | 3079 | { |
db14fc3a MS |
3080 | struct dentry *root = data; |
3081 | if (dentry != root) { | |
3082 | if (d_unhashed(dentry) || !dentry->d_inode) | |
3083 | return D_WALK_SKIP; | |
1da177e4 | 3084 | |
7e4a205f AV |
3085 | if (!(dentry->d_flags & DCACHE_GENOCIDE)) { |
3086 | dentry->d_flags |= DCACHE_GENOCIDE; | |
3087 | dentry->d_lockref.count--; | |
3088 | } | |
1da177e4 | 3089 | } |
db14fc3a MS |
3090 | return D_WALK_CONTINUE; |
3091 | } | |
58db63d0 | 3092 | |
db14fc3a MS |
3093 | void d_genocide(struct dentry *parent) |
3094 | { | |
3a8e3611 | 3095 | d_walk(parent, parent, d_genocide_kill); |
1da177e4 LT |
3096 | } |
3097 | ||
771eb4fe | 3098 | void d_mark_tmpfile(struct file *file, struct inode *inode) |
1da177e4 | 3099 | { |
863f144f MS |
3100 | struct dentry *dentry = file->f_path.dentry; |
3101 | ||
58cf9c38 | 3102 | BUG_ON(dname_external(dentry) || |
946e51f2 | 3103 | !hlist_unhashed(&dentry->d_u.d_alias) || |
60545d0d AV |
3104 | !d_unlinked(dentry)); |
3105 | spin_lock(&dentry->d_parent->d_lock); | |
3106 | spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); | |
58cf9c38 | 3107 | dentry->d_name.len = sprintf(dentry->d_shortname.string, "#%llu", |
60545d0d AV |
3108 | (unsigned long long)inode->i_ino); |
3109 | spin_unlock(&dentry->d_lock); | |
3110 | spin_unlock(&dentry->d_parent->d_lock); | |
771eb4fe KO |
3111 | } |
3112 | EXPORT_SYMBOL(d_mark_tmpfile); | |
3113 | ||
3114 | void d_tmpfile(struct file *file, struct inode *inode) | |
3115 | { | |
3116 | struct dentry *dentry = file->f_path.dentry; | |
3117 | ||
3118 | inode_dec_link_count(inode); | |
3119 | d_mark_tmpfile(file, inode); | |
60545d0d | 3120 | d_instantiate(dentry, inode); |
1da177e4 | 3121 | } |
60545d0d | 3122 | EXPORT_SYMBOL(d_tmpfile); |
1da177e4 | 3123 | |
f378ec4e MG |
3124 | /* |
3125 | * Obtain inode number of the parent dentry. | |
3126 | */ | |
3127 | ino_t d_parent_ino(struct dentry *dentry) | |
3128 | { | |
3129 | struct dentry *parent; | |
3130 | struct inode *iparent; | |
3131 | unsigned seq; | |
3132 | ino_t ret; | |
3133 | ||
3134 | scoped_guard(rcu) { | |
3135 | seq = raw_seqcount_begin(&dentry->d_seq); | |
3136 | parent = READ_ONCE(dentry->d_parent); | |
3137 | iparent = d_inode_rcu(parent); | |
3138 | if (likely(iparent)) { | |
3139 | ret = iparent->i_ino; | |
3140 | if (!read_seqcount_retry(&dentry->d_seq, seq)) | |
3141 | return ret; | |
3142 | } | |
3143 | } | |
3144 | ||
3145 | spin_lock(&dentry->d_lock); | |
3146 | ret = dentry->d_parent->d_inode->i_ino; | |
3147 | spin_unlock(&dentry->d_lock); | |
3148 | return ret; | |
3149 | } | |
3150 | EXPORT_SYMBOL(d_parent_ino); | |
3151 | ||
1da177e4 LT |
3152 | static __initdata unsigned long dhash_entries; |
3153 | static int __init set_dhash_entries(char *str) | |
3154 | { | |
3155 | if (!str) | |
3156 | return 0; | |
3157 | dhash_entries = simple_strtoul(str, &str, 0); | |
3158 | return 1; | |
3159 | } | |
3160 | __setup("dhash_entries=", set_dhash_entries); | |
3161 | ||
3162 | static void __init dcache_init_early(void) | |
3163 | { | |
1da177e4 LT |
3164 | /* If hashes are distributed across NUMA nodes, defer |
3165 | * hash allocation until vmalloc space is available. | |
3166 | */ | |
3167 | if (hashdist) | |
3168 | return; | |
3169 | ||
3170 | dentry_hashtable = | |
3171 | alloc_large_system_hash("Dentry cache", | |
b07ad996 | 3172 | sizeof(struct hlist_bl_head), |
1da177e4 LT |
3173 | dhash_entries, |
3174 | 13, | |
3d375d78 | 3175 | HASH_EARLY | HASH_ZERO, |
1da177e4 | 3176 | &d_hash_shift, |
b35d786b | 3177 | NULL, |
31fe62b9 | 3178 | 0, |
1da177e4 | 3179 | 0); |
854d3e63 | 3180 | d_hash_shift = 32 - d_hash_shift; |
e7829855 LT |
3181 | |
3182 | runtime_const_init(shift, d_hash_shift); | |
3183 | runtime_const_init(ptr, dentry_hashtable); | |
1da177e4 LT |
3184 | } |
3185 | ||
74bf17cf | 3186 | static void __init dcache_init(void) |
1da177e4 | 3187 | { |
3d375d78 | 3188 | /* |
1da177e4 LT |
3189 | * A constructor could be added for stable state like the lists, |
3190 | * but it is probably not worth it because of the cache nature | |
3d375d78 | 3191 | * of the dcache. |
1da177e4 | 3192 | */ |
80344266 | 3193 | dentry_cache = KMEM_CACHE_USERCOPY(dentry, |
c997d683 | 3194 | SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|SLAB_ACCOUNT, |
58cf9c38 | 3195 | d_shortname.string); |
1da177e4 LT |
3196 | |
3197 | /* Hash may have been set up in dcache_init_early */ | |
3198 | if (!hashdist) | |
3199 | return; | |
3200 | ||
3201 | dentry_hashtable = | |
3202 | alloc_large_system_hash("Dentry cache", | |
b07ad996 | 3203 | sizeof(struct hlist_bl_head), |
1da177e4 LT |
3204 | dhash_entries, |
3205 | 13, | |
3d375d78 | 3206 | HASH_ZERO, |
1da177e4 | 3207 | &d_hash_shift, |
b35d786b | 3208 | NULL, |
31fe62b9 | 3209 | 0, |
1da177e4 | 3210 | 0); |
854d3e63 | 3211 | d_hash_shift = 32 - d_hash_shift; |
e7829855 LT |
3212 | |
3213 | runtime_const_init(shift, d_hash_shift); | |
3214 | runtime_const_init(ptr, dentry_hashtable); | |
1da177e4 LT |
3215 | } |
3216 | ||
3217 | /* SLAB cache for __getname() consumers */ | |
68279f9c | 3218 | struct kmem_cache *names_cachep __ro_after_init; |
ec4f8605 | 3219 | EXPORT_SYMBOL(names_cachep); |
1da177e4 | 3220 | |
1da177e4 LT |
3221 | void __init vfs_caches_init_early(void) |
3222 | { | |
6916363f SAS |
3223 | int i; |
3224 | ||
3225 | for (i = 0; i < ARRAY_SIZE(in_lookup_hashtable); i++) | |
3226 | INIT_HLIST_BL_HEAD(&in_lookup_hashtable[i]); | |
3227 | ||
1da177e4 LT |
3228 | dcache_init_early(); |
3229 | inode_init_early(); | |
3230 | } | |
3231 | ||
4248b0da | 3232 | void __init vfs_caches_init(void) |
1da177e4 | 3233 | { |
6a9b8820 DW |
3234 | names_cachep = kmem_cache_create_usercopy("names_cache", PATH_MAX, 0, |
3235 | SLAB_HWCACHE_ALIGN|SLAB_PANIC, 0, PATH_MAX, NULL); | |
1da177e4 | 3236 | |
74bf17cf DC |
3237 | dcache_init(); |
3238 | inode_init(); | |
4248b0da MG |
3239 | files_init(); |
3240 | files_maxfiles_init(); | |
74bf17cf | 3241 | mnt_init(); |
1da177e4 LT |
3242 | bdev_cache_init(); |
3243 | chrdev_init(); | |
3244 | } |