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