]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * fs/dcache.c | |
3 | * | |
4 | * Complete reimplementation | |
5 | * (C) 1997 Thomas Schoebel-Theuer, | |
6 | * with heavy changes by Linus Torvalds | |
7 | */ | |
8 | ||
9 | /* | |
10 | * Notes on the allocation strategy: | |
11 | * | |
12 | * The dcache is a master of the icache - whenever a dcache entry | |
13 | * exists, the inode will always exist. "iput()" is done either when | |
14 | * the dcache entry is deleted or garbage collected. | |
15 | */ | |
16 | ||
1da177e4 LT |
17 | #include <linux/syscalls.h> |
18 | #include <linux/string.h> | |
19 | #include <linux/mm.h> | |
20 | #include <linux/fs.h> | |
7a91bf7f | 21 | #include <linux/fsnotify.h> |
1da177e4 LT |
22 | #include <linux/slab.h> |
23 | #include <linux/init.h> | |
1da177e4 LT |
24 | #include <linux/hash.h> |
25 | #include <linux/cache.h> | |
630d9c47 | 26 | #include <linux/export.h> |
1da177e4 LT |
27 | #include <linux/mount.h> |
28 | #include <linux/file.h> | |
7c0f6ba6 | 29 | #include <linux/uaccess.h> |
1da177e4 LT |
30 | #include <linux/security.h> |
31 | #include <linux/seqlock.h> | |
32 | #include <linux/swap.h> | |
33 | #include <linux/bootmem.h> | |
5ad4e53b | 34 | #include <linux/fs_struct.h> |
ceb5bdc2 NP |
35 | #include <linux/bit_spinlock.h> |
36 | #include <linux/rculist_bl.h> | |
268bb0ce | 37 | #include <linux/prefetch.h> |
dd179946 | 38 | #include <linux/ratelimit.h> |
f6041567 | 39 | #include <linux/list_lru.h> |
07f3f05c | 40 | #include "internal.h" |
b2dba1af | 41 | #include "mount.h" |
1da177e4 | 42 | |
789680d1 NP |
43 | /* |
44 | * Usage: | |
873feea0 | 45 | * dcache->d_inode->i_lock protects: |
946e51f2 | 46 | * - i_dentry, d_u.d_alias, d_inode of aliases |
ceb5bdc2 NP |
47 | * dcache_hash_bucket lock protects: |
48 | * - the dcache hash table | |
f1ee6162 N |
49 | * s_roots bl list spinlock protects: |
50 | * - the s_roots list (see __d_drop) | |
19156840 | 51 | * dentry->d_sb->s_dentry_lru_lock protects: |
23044507 NP |
52 | * - the dcache lru lists and counters |
53 | * d_lock protects: | |
54 | * - d_flags | |
55 | * - d_name | |
56 | * - d_lru | |
b7ab39f6 | 57 | * - d_count |
da502956 | 58 | * - d_unhashed() |
2fd6b7f5 NP |
59 | * - d_parent and d_subdirs |
60 | * - childrens' d_child and d_parent | |
946e51f2 | 61 | * - d_u.d_alias, d_inode |
789680d1 NP |
62 | * |
63 | * Ordering: | |
873feea0 | 64 | * dentry->d_inode->i_lock |
b5c84bf6 | 65 | * dentry->d_lock |
19156840 | 66 | * dentry->d_sb->s_dentry_lru_lock |
ceb5bdc2 | 67 | * dcache_hash_bucket lock |
f1ee6162 | 68 | * s_roots lock |
789680d1 | 69 | * |
da502956 NP |
70 | * If there is an ancestor relationship: |
71 | * dentry->d_parent->...->d_parent->d_lock | |
72 | * ... | |
73 | * dentry->d_parent->d_lock | |
74 | * dentry->d_lock | |
75 | * | |
76 | * If no ancestor relationship: | |
789680d1 NP |
77 | * if (dentry1 < dentry2) |
78 | * dentry1->d_lock | |
79 | * dentry2->d_lock | |
80 | */ | |
fa3536cc | 81 | int sysctl_vfs_cache_pressure __read_mostly = 100; |
1da177e4 LT |
82 | EXPORT_SYMBOL_GPL(sysctl_vfs_cache_pressure); |
83 | ||
74c3cbe3 | 84 | __cacheline_aligned_in_smp DEFINE_SEQLOCK(rename_lock); |
1da177e4 | 85 | |
949854d0 | 86 | EXPORT_SYMBOL(rename_lock); |
1da177e4 | 87 | |
e18b890b | 88 | static struct kmem_cache *dentry_cache __read_mostly; |
1da177e4 | 89 | |
cdf01226 DH |
90 | const struct qstr empty_name = QSTR_INIT("", 0); |
91 | EXPORT_SYMBOL(empty_name); | |
92 | const struct qstr slash_name = QSTR_INIT("/", 1); | |
93 | EXPORT_SYMBOL(slash_name); | |
94 | ||
1da177e4 LT |
95 | /* |
96 | * This is the single most critical data structure when it comes | |
97 | * to the dcache: the hashtable for lookups. Somebody should try | |
98 | * to make this good - I've just made it work. | |
99 | * | |
100 | * This hash-function tries to avoid losing too many bits of hash | |
101 | * information, yet avoid using a prime hash-size or similar. | |
102 | */ | |
1da177e4 | 103 | |
fa3536cc | 104 | static unsigned int d_hash_shift __read_mostly; |
ceb5bdc2 | 105 | |
b07ad996 | 106 | static struct hlist_bl_head *dentry_hashtable __read_mostly; |
ceb5bdc2 | 107 | |
8387ff25 | 108 | static inline struct hlist_bl_head *d_hash(unsigned int hash) |
ceb5bdc2 | 109 | { |
854d3e63 | 110 | return dentry_hashtable + (hash >> d_hash_shift); |
ceb5bdc2 NP |
111 | } |
112 | ||
94bdd655 AV |
113 | #define IN_LOOKUP_SHIFT 10 |
114 | static struct hlist_bl_head in_lookup_hashtable[1 << IN_LOOKUP_SHIFT]; | |
115 | ||
116 | static inline struct hlist_bl_head *in_lookup_hash(const struct dentry *parent, | |
117 | unsigned int hash) | |
118 | { | |
119 | hash += (unsigned long) parent / L1_CACHE_BYTES; | |
120 | return in_lookup_hashtable + hash_32(hash, IN_LOOKUP_SHIFT); | |
121 | } | |
122 | ||
123 | ||
1da177e4 LT |
124 | /* Statistics gathering. */ |
125 | struct dentry_stat_t dentry_stat = { | |
126 | .age_limit = 45, | |
127 | }; | |
128 | ||
3942c07c | 129 | static DEFINE_PER_CPU(long, nr_dentry); |
62d36c77 | 130 | static DEFINE_PER_CPU(long, nr_dentry_unused); |
312d3ca8 CH |
131 | |
132 | #if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS) | |
62d36c77 DC |
133 | |
134 | /* | |
135 | * Here we resort to our own counters instead of using generic per-cpu counters | |
136 | * for consistency with what the vfs inode code does. We are expected to harvest | |
137 | * better code and performance by having our own specialized counters. | |
138 | * | |
139 | * Please note that the loop is done over all possible CPUs, not over all online | |
140 | * CPUs. The reason for this is that we don't want to play games with CPUs going | |
141 | * on and off. If one of them goes off, we will just keep their counters. | |
142 | * | |
143 | * glommer: See cffbc8a for details, and if you ever intend to change this, | |
144 | * please update all vfs counters to match. | |
145 | */ | |
3942c07c | 146 | static long get_nr_dentry(void) |
3e880fb5 NP |
147 | { |
148 | int i; | |
3942c07c | 149 | long sum = 0; |
3e880fb5 NP |
150 | for_each_possible_cpu(i) |
151 | sum += per_cpu(nr_dentry, i); | |
152 | return sum < 0 ? 0 : sum; | |
153 | } | |
154 | ||
62d36c77 DC |
155 | static long get_nr_dentry_unused(void) |
156 | { | |
157 | int i; | |
158 | long sum = 0; | |
159 | for_each_possible_cpu(i) | |
160 | sum += per_cpu(nr_dentry_unused, i); | |
161 | return sum < 0 ? 0 : sum; | |
162 | } | |
163 | ||
1f7e0616 | 164 | int proc_nr_dentry(struct ctl_table *table, int write, void __user *buffer, |
312d3ca8 CH |
165 | size_t *lenp, loff_t *ppos) |
166 | { | |
3e880fb5 | 167 | dentry_stat.nr_dentry = get_nr_dentry(); |
62d36c77 | 168 | dentry_stat.nr_unused = get_nr_dentry_unused(); |
3942c07c | 169 | return proc_doulongvec_minmax(table, write, buffer, lenp, ppos); |
312d3ca8 CH |
170 | } |
171 | #endif | |
172 | ||
5483f18e LT |
173 | /* |
174 | * Compare 2 name strings, return 0 if they match, otherwise non-zero. | |
175 | * The strings are both count bytes long, and count is non-zero. | |
176 | */ | |
e419b4cc LT |
177 | #ifdef CONFIG_DCACHE_WORD_ACCESS |
178 | ||
179 | #include <asm/word-at-a-time.h> | |
180 | /* | |
181 | * NOTE! 'cs' and 'scount' come from a dentry, so it has a | |
182 | * aligned allocation for this particular component. We don't | |
183 | * strictly need the load_unaligned_zeropad() safety, but it | |
184 | * doesn't hurt either. | |
185 | * | |
186 | * In contrast, 'ct' and 'tcount' can be from a pathname, and do | |
187 | * need the careful unaligned handling. | |
188 | */ | |
94753db5 | 189 | static inline int dentry_string_cmp(const unsigned char *cs, const unsigned char *ct, unsigned tcount) |
5483f18e | 190 | { |
bfcfaa77 | 191 | unsigned long a,b,mask; |
bfcfaa77 LT |
192 | |
193 | for (;;) { | |
bfe7aa6c | 194 | a = read_word_at_a_time(cs); |
e419b4cc | 195 | b = load_unaligned_zeropad(ct); |
bfcfaa77 LT |
196 | if (tcount < sizeof(unsigned long)) |
197 | break; | |
198 | if (unlikely(a != b)) | |
199 | return 1; | |
200 | cs += sizeof(unsigned long); | |
201 | ct += sizeof(unsigned long); | |
202 | tcount -= sizeof(unsigned long); | |
203 | if (!tcount) | |
204 | return 0; | |
205 | } | |
a5c21dce | 206 | mask = bytemask_from_count(tcount); |
bfcfaa77 | 207 | return unlikely(!!((a ^ b) & mask)); |
e419b4cc LT |
208 | } |
209 | ||
bfcfaa77 | 210 | #else |
e419b4cc | 211 | |
94753db5 | 212 | static inline int dentry_string_cmp(const unsigned char *cs, const unsigned char *ct, unsigned tcount) |
e419b4cc | 213 | { |
5483f18e LT |
214 | do { |
215 | if (*cs != *ct) | |
216 | return 1; | |
217 | cs++; | |
218 | ct++; | |
219 | tcount--; | |
220 | } while (tcount); | |
221 | return 0; | |
222 | } | |
223 | ||
e419b4cc LT |
224 | #endif |
225 | ||
94753db5 LT |
226 | static inline int dentry_cmp(const struct dentry *dentry, const unsigned char *ct, unsigned tcount) |
227 | { | |
94753db5 LT |
228 | /* |
229 | * Be careful about RCU walk racing with rename: | |
506458ef | 230 | * use 'READ_ONCE' to fetch the name pointer. |
94753db5 LT |
231 | * |
232 | * NOTE! Even if a rename will mean that the length | |
233 | * was not loaded atomically, we don't care. The | |
234 | * RCU walk will check the sequence count eventually, | |
235 | * and catch it. And we won't overrun the buffer, | |
236 | * because we're reading the name pointer atomically, | |
237 | * and a dentry name is guaranteed to be properly | |
238 | * terminated with a NUL byte. | |
239 | * | |
240 | * End result: even if 'len' is wrong, we'll exit | |
241 | * early because the data cannot match (there can | |
242 | * be no NUL in the ct/tcount data) | |
243 | */ | |
506458ef | 244 | const unsigned char *cs = READ_ONCE(dentry->d_name.name); |
ae0a843c | 245 | |
6326c71f | 246 | return dentry_string_cmp(cs, ct, tcount); |
94753db5 LT |
247 | } |
248 | ||
8d85b484 AV |
249 | struct external_name { |
250 | union { | |
251 | atomic_t count; | |
252 | struct rcu_head head; | |
253 | } u; | |
254 | unsigned char name[]; | |
255 | }; | |
256 | ||
257 | static inline struct external_name *external_name(struct dentry *dentry) | |
258 | { | |
259 | return container_of(dentry->d_name.name, struct external_name, name[0]); | |
260 | } | |
261 | ||
9c82ab9c | 262 | static void __d_free(struct rcu_head *head) |
1da177e4 | 263 | { |
9c82ab9c CH |
264 | struct dentry *dentry = container_of(head, struct dentry, d_u.d_rcu); |
265 | ||
8d85b484 AV |
266 | kmem_cache_free(dentry_cache, dentry); |
267 | } | |
268 | ||
269 | static void __d_free_external(struct rcu_head *head) | |
270 | { | |
271 | struct dentry *dentry = container_of(head, struct dentry, d_u.d_rcu); | |
8d85b484 | 272 | kfree(external_name(dentry)); |
1da177e4 LT |
273 | kmem_cache_free(dentry_cache, dentry); |
274 | } | |
275 | ||
810bb172 AV |
276 | static inline int dname_external(const struct dentry *dentry) |
277 | { | |
278 | return dentry->d_name.name != dentry->d_iname; | |
279 | } | |
280 | ||
49d31c2f AV |
281 | void take_dentry_name_snapshot(struct name_snapshot *name, struct dentry *dentry) |
282 | { | |
283 | spin_lock(&dentry->d_lock); | |
284 | if (unlikely(dname_external(dentry))) { | |
285 | struct external_name *p = external_name(dentry); | |
286 | atomic_inc(&p->u.count); | |
287 | spin_unlock(&dentry->d_lock); | |
288 | name->name = p->name; | |
289 | } else { | |
290 | memcpy(name->inline_name, dentry->d_iname, DNAME_INLINE_LEN); | |
291 | spin_unlock(&dentry->d_lock); | |
292 | name->name = name->inline_name; | |
293 | } | |
294 | } | |
295 | EXPORT_SYMBOL(take_dentry_name_snapshot); | |
296 | ||
297 | void release_dentry_name_snapshot(struct name_snapshot *name) | |
298 | { | |
299 | if (unlikely(name->name != name->inline_name)) { | |
300 | struct external_name *p; | |
301 | p = container_of(name->name, struct external_name, name[0]); | |
302 | if (unlikely(atomic_dec_and_test(&p->u.count))) | |
303 | kfree_rcu(p, u.head); | |
304 | } | |
305 | } | |
306 | EXPORT_SYMBOL(release_dentry_name_snapshot); | |
307 | ||
4bf46a27 DH |
308 | static inline void __d_set_inode_and_type(struct dentry *dentry, |
309 | struct inode *inode, | |
310 | unsigned type_flags) | |
311 | { | |
312 | unsigned flags; | |
313 | ||
314 | dentry->d_inode = inode; | |
4bf46a27 DH |
315 | flags = READ_ONCE(dentry->d_flags); |
316 | flags &= ~(DCACHE_ENTRY_TYPE | DCACHE_FALLTHRU); | |
317 | flags |= type_flags; | |
318 | WRITE_ONCE(dentry->d_flags, flags); | |
319 | } | |
320 | ||
4bf46a27 DH |
321 | static inline void __d_clear_type_and_inode(struct dentry *dentry) |
322 | { | |
323 | unsigned flags = READ_ONCE(dentry->d_flags); | |
324 | ||
325 | flags &= ~(DCACHE_ENTRY_TYPE | DCACHE_FALLTHRU); | |
326 | WRITE_ONCE(dentry->d_flags, flags); | |
4bf46a27 DH |
327 | dentry->d_inode = NULL; |
328 | } | |
329 | ||
b4f0354e AV |
330 | static void dentry_free(struct dentry *dentry) |
331 | { | |
946e51f2 | 332 | WARN_ON(!hlist_unhashed(&dentry->d_u.d_alias)); |
8d85b484 AV |
333 | if (unlikely(dname_external(dentry))) { |
334 | struct external_name *p = external_name(dentry); | |
335 | if (likely(atomic_dec_and_test(&p->u.count))) { | |
336 | call_rcu(&dentry->d_u.d_rcu, __d_free_external); | |
337 | return; | |
338 | } | |
339 | } | |
b4f0354e AV |
340 | /* if dentry was never visible to RCU, immediate free is OK */ |
341 | if (!(dentry->d_flags & DCACHE_RCUACCESS)) | |
342 | __d_free(&dentry->d_u.d_rcu); | |
343 | else | |
344 | call_rcu(&dentry->d_u.d_rcu, __d_free); | |
345 | } | |
346 | ||
1da177e4 LT |
347 | /* |
348 | * Release the dentry's inode, using the filesystem | |
550dce01 | 349 | * d_iput() operation if defined. |
31e6b01f NP |
350 | */ |
351 | static void dentry_unlink_inode(struct dentry * dentry) | |
352 | __releases(dentry->d_lock) | |
873feea0 | 353 | __releases(dentry->d_inode->i_lock) |
31e6b01f NP |
354 | { |
355 | struct inode *inode = dentry->d_inode; | |
550dce01 | 356 | bool hashed = !d_unhashed(dentry); |
a528aca7 | 357 | |
550dce01 AV |
358 | if (hashed) |
359 | raw_write_seqcount_begin(&dentry->d_seq); | |
4bf46a27 | 360 | __d_clear_type_and_inode(dentry); |
946e51f2 | 361 | hlist_del_init(&dentry->d_u.d_alias); |
550dce01 AV |
362 | if (hashed) |
363 | raw_write_seqcount_end(&dentry->d_seq); | |
31e6b01f | 364 | spin_unlock(&dentry->d_lock); |
873feea0 | 365 | spin_unlock(&inode->i_lock); |
31e6b01f NP |
366 | if (!inode->i_nlink) |
367 | fsnotify_inoderemove(inode); | |
368 | if (dentry->d_op && dentry->d_op->d_iput) | |
369 | dentry->d_op->d_iput(dentry, inode); | |
370 | else | |
371 | iput(inode); | |
372 | } | |
373 | ||
89dc77bc LT |
374 | /* |
375 | * The DCACHE_LRU_LIST bit is set whenever the 'd_lru' entry | |
376 | * is in use - which includes both the "real" per-superblock | |
377 | * LRU list _and_ the DCACHE_SHRINK_LIST use. | |
378 | * | |
379 | * The DCACHE_SHRINK_LIST bit is set whenever the dentry is | |
380 | * on the shrink list (ie not on the superblock LRU list). | |
381 | * | |
382 | * The per-cpu "nr_dentry_unused" counters are updated with | |
383 | * the DCACHE_LRU_LIST bit. | |
384 | * | |
385 | * These helper functions make sure we always follow the | |
386 | * rules. d_lock must be held by the caller. | |
387 | */ | |
388 | #define D_FLAG_VERIFY(dentry,x) WARN_ON_ONCE(((dentry)->d_flags & (DCACHE_LRU_LIST | DCACHE_SHRINK_LIST)) != (x)) | |
389 | static void d_lru_add(struct dentry *dentry) | |
390 | { | |
391 | D_FLAG_VERIFY(dentry, 0); | |
392 | dentry->d_flags |= DCACHE_LRU_LIST; | |
393 | this_cpu_inc(nr_dentry_unused); | |
394 | WARN_ON_ONCE(!list_lru_add(&dentry->d_sb->s_dentry_lru, &dentry->d_lru)); | |
395 | } | |
396 | ||
397 | static void d_lru_del(struct dentry *dentry) | |
398 | { | |
399 | D_FLAG_VERIFY(dentry, DCACHE_LRU_LIST); | |
400 | dentry->d_flags &= ~DCACHE_LRU_LIST; | |
401 | this_cpu_dec(nr_dentry_unused); | |
402 | WARN_ON_ONCE(!list_lru_del(&dentry->d_sb->s_dentry_lru, &dentry->d_lru)); | |
403 | } | |
404 | ||
405 | static void d_shrink_del(struct dentry *dentry) | |
406 | { | |
407 | D_FLAG_VERIFY(dentry, DCACHE_SHRINK_LIST | DCACHE_LRU_LIST); | |
408 | list_del_init(&dentry->d_lru); | |
409 | dentry->d_flags &= ~(DCACHE_SHRINK_LIST | DCACHE_LRU_LIST); | |
410 | this_cpu_dec(nr_dentry_unused); | |
411 | } | |
412 | ||
413 | static void d_shrink_add(struct dentry *dentry, struct list_head *list) | |
414 | { | |
415 | D_FLAG_VERIFY(dentry, 0); | |
416 | list_add(&dentry->d_lru, list); | |
417 | dentry->d_flags |= DCACHE_SHRINK_LIST | DCACHE_LRU_LIST; | |
418 | this_cpu_inc(nr_dentry_unused); | |
419 | } | |
420 | ||
421 | /* | |
422 | * These can only be called under the global LRU lock, ie during the | |
423 | * callback for freeing the LRU list. "isolate" removes it from the | |
424 | * LRU lists entirely, while shrink_move moves it to the indicated | |
425 | * private list. | |
426 | */ | |
3f97b163 | 427 | static void d_lru_isolate(struct list_lru_one *lru, struct dentry *dentry) |
89dc77bc LT |
428 | { |
429 | D_FLAG_VERIFY(dentry, DCACHE_LRU_LIST); | |
430 | dentry->d_flags &= ~DCACHE_LRU_LIST; | |
431 | this_cpu_dec(nr_dentry_unused); | |
3f97b163 | 432 | list_lru_isolate(lru, &dentry->d_lru); |
89dc77bc LT |
433 | } |
434 | ||
3f97b163 VD |
435 | static void d_lru_shrink_move(struct list_lru_one *lru, struct dentry *dentry, |
436 | struct list_head *list) | |
89dc77bc LT |
437 | { |
438 | D_FLAG_VERIFY(dentry, DCACHE_LRU_LIST); | |
439 | dentry->d_flags |= DCACHE_SHRINK_LIST; | |
3f97b163 | 440 | list_lru_isolate_move(lru, &dentry->d_lru, list); |
89dc77bc LT |
441 | } |
442 | ||
789680d1 NP |
443 | /** |
444 | * d_drop - drop a dentry | |
445 | * @dentry: dentry to drop | |
446 | * | |
447 | * d_drop() unhashes the entry from the parent dentry hashes, so that it won't | |
448 | * be found through a VFS lookup any more. Note that this is different from | |
449 | * deleting the dentry - d_delete will try to mark the dentry negative if | |
450 | * possible, giving a successful _negative_ lookup, while d_drop will | |
451 | * just make the cache lookup fail. | |
452 | * | |
453 | * d_drop() is used mainly for stuff that wants to invalidate a dentry for some | |
454 | * reason (NFS timeouts or autofs deletes). | |
455 | * | |
61647823 N |
456 | * __d_drop requires dentry->d_lock |
457 | * ___d_drop doesn't mark dentry as "unhashed" | |
458 | * (dentry->d_hash.pprev will be LIST_POISON2, not NULL). | |
789680d1 | 459 | */ |
61647823 | 460 | static void ___d_drop(struct dentry *dentry) |
789680d1 | 461 | { |
0632a9ac AV |
462 | struct hlist_bl_head *b; |
463 | /* | |
464 | * Hashed dentries are normally on the dentry hashtable, | |
465 | * with the exception of those newly allocated by | |
466 | * d_obtain_root, which are always IS_ROOT: | |
467 | */ | |
468 | if (unlikely(IS_ROOT(dentry))) | |
469 | b = &dentry->d_sb->s_roots; | |
470 | else | |
471 | b = d_hash(dentry->d_name.hash); | |
b61625d2 | 472 | |
0632a9ac AV |
473 | hlist_bl_lock(b); |
474 | __hlist_bl_del(&dentry->d_hash); | |
475 | hlist_bl_unlock(b); | |
789680d1 | 476 | } |
61647823 N |
477 | |
478 | void __d_drop(struct dentry *dentry) | |
479 | { | |
0632a9ac AV |
480 | if (!d_unhashed(dentry)) { |
481 | ___d_drop(dentry); | |
482 | dentry->d_hash.pprev = NULL; | |
483 | write_seqcount_invalidate(&dentry->d_seq); | |
484 | } | |
61647823 | 485 | } |
789680d1 NP |
486 | EXPORT_SYMBOL(__d_drop); |
487 | ||
488 | void d_drop(struct dentry *dentry) | |
489 | { | |
789680d1 NP |
490 | spin_lock(&dentry->d_lock); |
491 | __d_drop(dentry); | |
492 | spin_unlock(&dentry->d_lock); | |
789680d1 NP |
493 | } |
494 | EXPORT_SYMBOL(d_drop); | |
495 | ||
ba65dc5e AV |
496 | static inline void dentry_unlist(struct dentry *dentry, struct dentry *parent) |
497 | { | |
498 | struct dentry *next; | |
499 | /* | |
500 | * Inform d_walk() and shrink_dentry_list() that we are no longer | |
501 | * attached to the dentry tree | |
502 | */ | |
503 | dentry->d_flags |= DCACHE_DENTRY_KILLED; | |
504 | if (unlikely(list_empty(&dentry->d_child))) | |
505 | return; | |
506 | __list_del_entry(&dentry->d_child); | |
507 | /* | |
508 | * Cursors can move around the list of children. While we'd been | |
509 | * a normal list member, it didn't matter - ->d_child.next would've | |
510 | * been updated. However, from now on it won't be and for the | |
511 | * things like d_walk() it might end up with a nasty surprise. | |
512 | * Normally d_walk() doesn't care about cursors moving around - | |
513 | * ->d_lock on parent prevents that and since a cursor has no children | |
514 | * of its own, we get through it without ever unlocking the parent. | |
515 | * There is one exception, though - if we ascend from a child that | |
516 | * gets killed as soon as we unlock it, the next sibling is found | |
517 | * using the value left in its ->d_child.next. And if _that_ | |
518 | * pointed to a cursor, and cursor got moved (e.g. by lseek()) | |
519 | * before d_walk() regains parent->d_lock, we'll end up skipping | |
520 | * everything the cursor had been moved past. | |
521 | * | |
522 | * Solution: make sure that the pointer left behind in ->d_child.next | |
523 | * points to something that won't be moving around. I.e. skip the | |
524 | * cursors. | |
525 | */ | |
526 | while (dentry->d_child.next != &parent->d_subdirs) { | |
527 | next = list_entry(dentry->d_child.next, struct dentry, d_child); | |
528 | if (likely(!(next->d_flags & DCACHE_DENTRY_CURSOR))) | |
529 | break; | |
530 | dentry->d_child.next = next->d_child.next; | |
531 | } | |
532 | } | |
533 | ||
e55fd011 | 534 | static void __dentry_kill(struct dentry *dentry) |
77812a1e | 535 | { |
41edf278 AV |
536 | struct dentry *parent = NULL; |
537 | bool can_free = true; | |
41edf278 | 538 | if (!IS_ROOT(dentry)) |
77812a1e | 539 | parent = dentry->d_parent; |
31e6b01f | 540 | |
0d98439e LT |
541 | /* |
542 | * The dentry is now unrecoverably dead to the world. | |
543 | */ | |
544 | lockref_mark_dead(&dentry->d_lockref); | |
545 | ||
f0023bc6 | 546 | /* |
f0023bc6 SW |
547 | * inform the fs via d_prune that this dentry is about to be |
548 | * unhashed and destroyed. | |
549 | */ | |
29266201 | 550 | if (dentry->d_flags & DCACHE_OP_PRUNE) |
61572bb1 YZ |
551 | dentry->d_op->d_prune(dentry); |
552 | ||
01b60351 AV |
553 | if (dentry->d_flags & DCACHE_LRU_LIST) { |
554 | if (!(dentry->d_flags & DCACHE_SHRINK_LIST)) | |
555 | d_lru_del(dentry); | |
01b60351 | 556 | } |
77812a1e NP |
557 | /* if it was on the hash then remove it */ |
558 | __d_drop(dentry); | |
ba65dc5e | 559 | dentry_unlist(dentry, parent); |
03b3b889 AV |
560 | if (parent) |
561 | spin_unlock(&parent->d_lock); | |
550dce01 AV |
562 | if (dentry->d_inode) |
563 | dentry_unlink_inode(dentry); | |
564 | else | |
565 | spin_unlock(&dentry->d_lock); | |
03b3b889 AV |
566 | this_cpu_dec(nr_dentry); |
567 | if (dentry->d_op && dentry->d_op->d_release) | |
568 | dentry->d_op->d_release(dentry); | |
569 | ||
41edf278 AV |
570 | spin_lock(&dentry->d_lock); |
571 | if (dentry->d_flags & DCACHE_SHRINK_LIST) { | |
572 | dentry->d_flags |= DCACHE_MAY_FREE; | |
573 | can_free = false; | |
574 | } | |
575 | spin_unlock(&dentry->d_lock); | |
41edf278 AV |
576 | if (likely(can_free)) |
577 | dentry_free(dentry); | |
e55fd011 AV |
578 | } |
579 | ||
8b987a46 | 580 | static struct dentry *__lock_parent(struct dentry *dentry) |
046b961b | 581 | { |
8b987a46 | 582 | struct dentry *parent; |
046b961b | 583 | rcu_read_lock(); |
c2338f2d | 584 | spin_unlock(&dentry->d_lock); |
046b961b | 585 | again: |
66702eb5 | 586 | parent = READ_ONCE(dentry->d_parent); |
046b961b AV |
587 | spin_lock(&parent->d_lock); |
588 | /* | |
589 | * We can't blindly lock dentry until we are sure | |
590 | * that we won't violate the locking order. | |
591 | * Any changes of dentry->d_parent must have | |
592 | * been done with parent->d_lock held, so | |
593 | * spin_lock() above is enough of a barrier | |
594 | * for checking if it's still our child. | |
595 | */ | |
596 | if (unlikely(parent != dentry->d_parent)) { | |
597 | spin_unlock(&parent->d_lock); | |
598 | goto again; | |
599 | } | |
65d8eb5a AV |
600 | rcu_read_unlock(); |
601 | if (parent != dentry) | |
9f12600f | 602 | spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); |
65d8eb5a | 603 | else |
046b961b AV |
604 | parent = NULL; |
605 | return parent; | |
606 | } | |
607 | ||
8b987a46 AV |
608 | static inline struct dentry *lock_parent(struct dentry *dentry) |
609 | { | |
610 | struct dentry *parent = dentry->d_parent; | |
611 | if (IS_ROOT(dentry)) | |
612 | return NULL; | |
613 | if (likely(spin_trylock(&parent->d_lock))) | |
614 | return parent; | |
615 | return __lock_parent(dentry); | |
616 | } | |
617 | ||
a338579f AV |
618 | static inline bool retain_dentry(struct dentry *dentry) |
619 | { | |
620 | WARN_ON(d_in_lookup(dentry)); | |
621 | ||
622 | /* Unreachable? Get rid of it */ | |
623 | if (unlikely(d_unhashed(dentry))) | |
624 | return false; | |
625 | ||
626 | if (unlikely(dentry->d_flags & DCACHE_DISCONNECTED)) | |
627 | return false; | |
628 | ||
629 | if (unlikely(dentry->d_flags & DCACHE_OP_DELETE)) { | |
630 | if (dentry->d_op->d_delete(dentry)) | |
631 | return false; | |
632 | } | |
62d9956c AV |
633 | /* retain; LRU fodder */ |
634 | dentry->d_lockref.count--; | |
635 | if (unlikely(!(dentry->d_flags & DCACHE_LRU_LIST))) | |
636 | d_lru_add(dentry); | |
637 | else if (unlikely(!(dentry->d_flags & DCACHE_REFERENCED))) | |
638 | dentry->d_flags |= DCACHE_REFERENCED; | |
a338579f AV |
639 | return true; |
640 | } | |
641 | ||
c1d0c1a2 JO |
642 | /* |
643 | * Finish off a dentry we've decided to kill. | |
644 | * dentry->d_lock must be held, returns with it unlocked. | |
645 | * Returns dentry requiring refcount drop, or NULL if we're done. | |
646 | */ | |
647 | static struct dentry *dentry_kill(struct dentry *dentry) | |
648 | __releases(dentry->d_lock) | |
649 | { | |
650 | struct inode *inode = dentry->d_inode; | |
651 | struct dentry *parent = NULL; | |
652 | ||
653 | if (inode && unlikely(!spin_trylock(&inode->i_lock))) | |
654 | goto failed; | |
655 | ||
656 | if (!IS_ROOT(dentry)) { | |
657 | parent = dentry->d_parent; | |
658 | if (unlikely(!spin_trylock(&parent->d_lock))) { | |
659 | if (inode) | |
660 | spin_unlock(&inode->i_lock); | |
661 | goto failed; | |
662 | } | |
663 | } | |
664 | ||
665 | __dentry_kill(dentry); | |
666 | return parent; | |
667 | ||
668 | failed: | |
669 | spin_unlock(&dentry->d_lock); | |
670 | return dentry; /* try again with same dentry */ | |
671 | } | |
672 | ||
360f5479 LT |
673 | /* |
674 | * Try to do a lockless dput(), and return whether that was successful. | |
675 | * | |
676 | * If unsuccessful, we return false, having already taken the dentry lock. | |
677 | * | |
678 | * The caller needs to hold the RCU read lock, so that the dentry is | |
679 | * guaranteed to stay around even if the refcount goes down to zero! | |
680 | */ | |
681 | static inline bool fast_dput(struct dentry *dentry) | |
682 | { | |
683 | int ret; | |
684 | unsigned int d_flags; | |
685 | ||
686 | /* | |
687 | * If we have a d_op->d_delete() operation, we sould not | |
75a6f82a | 688 | * let the dentry count go to zero, so use "put_or_lock". |
360f5479 LT |
689 | */ |
690 | if (unlikely(dentry->d_flags & DCACHE_OP_DELETE)) | |
691 | return lockref_put_or_lock(&dentry->d_lockref); | |
692 | ||
693 | /* | |
694 | * .. otherwise, we can try to just decrement the | |
695 | * lockref optimistically. | |
696 | */ | |
697 | ret = lockref_put_return(&dentry->d_lockref); | |
698 | ||
699 | /* | |
700 | * If the lockref_put_return() failed due to the lock being held | |
701 | * by somebody else, the fast path has failed. We will need to | |
702 | * get the lock, and then check the count again. | |
703 | */ | |
704 | if (unlikely(ret < 0)) { | |
705 | spin_lock(&dentry->d_lock); | |
706 | if (dentry->d_lockref.count > 1) { | |
707 | dentry->d_lockref.count--; | |
708 | spin_unlock(&dentry->d_lock); | |
709 | return 1; | |
710 | } | |
711 | return 0; | |
712 | } | |
713 | ||
714 | /* | |
715 | * If we weren't the last ref, we're done. | |
716 | */ | |
717 | if (ret) | |
718 | return 1; | |
719 | ||
720 | /* | |
721 | * Careful, careful. The reference count went down | |
722 | * to zero, but we don't hold the dentry lock, so | |
723 | * somebody else could get it again, and do another | |
724 | * dput(), and we need to not race with that. | |
725 | * | |
726 | * However, there is a very special and common case | |
727 | * where we don't care, because there is nothing to | |
728 | * do: the dentry is still hashed, it does not have | |
729 | * a 'delete' op, and it's referenced and already on | |
730 | * the LRU list. | |
731 | * | |
732 | * NOTE! Since we aren't locked, these values are | |
733 | * not "stable". However, it is sufficient that at | |
734 | * some point after we dropped the reference the | |
735 | * dentry was hashed and the flags had the proper | |
736 | * value. Other dentry users may have re-gotten | |
737 | * a reference to the dentry and change that, but | |
738 | * our work is done - we can leave the dentry | |
739 | * around with a zero refcount. | |
740 | */ | |
741 | smp_rmb(); | |
66702eb5 | 742 | d_flags = READ_ONCE(dentry->d_flags); |
75a6f82a | 743 | d_flags &= DCACHE_REFERENCED | DCACHE_LRU_LIST | DCACHE_DISCONNECTED; |
360f5479 LT |
744 | |
745 | /* Nothing to do? Dropping the reference was all we needed? */ | |
746 | if (d_flags == (DCACHE_REFERENCED | DCACHE_LRU_LIST) && !d_unhashed(dentry)) | |
747 | return 1; | |
748 | ||
749 | /* | |
750 | * Not the fast normal case? Get the lock. We've already decremented | |
751 | * the refcount, but we'll need to re-check the situation after | |
752 | * getting the lock. | |
753 | */ | |
754 | spin_lock(&dentry->d_lock); | |
755 | ||
756 | /* | |
757 | * Did somebody else grab a reference to it in the meantime, and | |
758 | * we're no longer the last user after all? Alternatively, somebody | |
759 | * else could have killed it and marked it dead. Either way, we | |
760 | * don't need to do anything else. | |
761 | */ | |
762 | if (dentry->d_lockref.count) { | |
763 | spin_unlock(&dentry->d_lock); | |
764 | return 1; | |
765 | } | |
766 | ||
767 | /* | |
768 | * Re-get the reference we optimistically dropped. We hold the | |
769 | * lock, and we just tested that it was zero, so we can just | |
770 | * set it to 1. | |
771 | */ | |
772 | dentry->d_lockref.count = 1; | |
773 | return 0; | |
774 | } | |
775 | ||
776 | ||
1da177e4 LT |
777 | /* |
778 | * This is dput | |
779 | * | |
780 | * This is complicated by the fact that we do not want to put | |
781 | * dentries that are no longer on any hash chain on the unused | |
782 | * list: we'd much rather just get rid of them immediately. | |
783 | * | |
784 | * However, that implies that we have to traverse the dentry | |
785 | * tree upwards to the parents which might _also_ now be | |
786 | * scheduled for deletion (it may have been only waiting for | |
787 | * its last child to go away). | |
788 | * | |
789 | * This tail recursion is done by hand as we don't want to depend | |
790 | * on the compiler to always get this right (gcc generally doesn't). | |
791 | * Real recursion would eat up our stack space. | |
792 | */ | |
793 | ||
794 | /* | |
795 | * dput - release a dentry | |
796 | * @dentry: dentry to release | |
797 | * | |
798 | * Release a dentry. This will drop the usage count and if appropriate | |
799 | * call the dentry unlink method as well as removing it from the queues and | |
800 | * releasing its resources. If the parent dentries were scheduled for release | |
801 | * they too may now get deleted. | |
1da177e4 | 802 | */ |
1da177e4 LT |
803 | void dput(struct dentry *dentry) |
804 | { | |
8aab6a27 | 805 | if (unlikely(!dentry)) |
1da177e4 LT |
806 | return; |
807 | ||
808 | repeat: | |
47be6184 WF |
809 | might_sleep(); |
810 | ||
360f5479 LT |
811 | rcu_read_lock(); |
812 | if (likely(fast_dput(dentry))) { | |
813 | rcu_read_unlock(); | |
1da177e4 | 814 | return; |
360f5479 LT |
815 | } |
816 | ||
817 | /* Slow case: now with the dentry lock held */ | |
818 | rcu_read_unlock(); | |
1da177e4 | 819 | |
a338579f | 820 | if (likely(retain_dentry(dentry))) { |
a338579f AV |
821 | spin_unlock(&dentry->d_lock); |
822 | return; | |
1da177e4 | 823 | } |
265ac902 | 824 | |
8cbf74da | 825 | dentry = dentry_kill(dentry); |
47be6184 WF |
826 | if (dentry) { |
827 | cond_resched(); | |
d52b9086 | 828 | goto repeat; |
47be6184 | 829 | } |
1da177e4 | 830 | } |
ec4f8605 | 831 | EXPORT_SYMBOL(dput); |
1da177e4 | 832 | |
1da177e4 | 833 | |
b5c84bf6 | 834 | /* This must be called with d_lock held */ |
dc0474be | 835 | static inline void __dget_dlock(struct dentry *dentry) |
23044507 | 836 | { |
98474236 | 837 | dentry->d_lockref.count++; |
23044507 NP |
838 | } |
839 | ||
dc0474be | 840 | static inline void __dget(struct dentry *dentry) |
1da177e4 | 841 | { |
98474236 | 842 | lockref_get(&dentry->d_lockref); |
1da177e4 LT |
843 | } |
844 | ||
b7ab39f6 NP |
845 | struct dentry *dget_parent(struct dentry *dentry) |
846 | { | |
df3d0bbc | 847 | int gotref; |
b7ab39f6 NP |
848 | struct dentry *ret; |
849 | ||
df3d0bbc WL |
850 | /* |
851 | * Do optimistic parent lookup without any | |
852 | * locking. | |
853 | */ | |
854 | rcu_read_lock(); | |
66702eb5 | 855 | ret = READ_ONCE(dentry->d_parent); |
df3d0bbc WL |
856 | gotref = lockref_get_not_zero(&ret->d_lockref); |
857 | rcu_read_unlock(); | |
858 | if (likely(gotref)) { | |
66702eb5 | 859 | if (likely(ret == READ_ONCE(dentry->d_parent))) |
df3d0bbc WL |
860 | return ret; |
861 | dput(ret); | |
862 | } | |
863 | ||
b7ab39f6 | 864 | repeat: |
a734eb45 NP |
865 | /* |
866 | * Don't need rcu_dereference because we re-check it was correct under | |
867 | * the lock. | |
868 | */ | |
869 | rcu_read_lock(); | |
b7ab39f6 | 870 | ret = dentry->d_parent; |
a734eb45 NP |
871 | spin_lock(&ret->d_lock); |
872 | if (unlikely(ret != dentry->d_parent)) { | |
873 | spin_unlock(&ret->d_lock); | |
874 | rcu_read_unlock(); | |
b7ab39f6 NP |
875 | goto repeat; |
876 | } | |
a734eb45 | 877 | rcu_read_unlock(); |
98474236 WL |
878 | BUG_ON(!ret->d_lockref.count); |
879 | ret->d_lockref.count++; | |
b7ab39f6 | 880 | spin_unlock(&ret->d_lock); |
b7ab39f6 NP |
881 | return ret; |
882 | } | |
883 | EXPORT_SYMBOL(dget_parent); | |
884 | ||
1da177e4 LT |
885 | /** |
886 | * d_find_alias - grab a hashed alias of inode | |
887 | * @inode: inode in question | |
1da177e4 LT |
888 | * |
889 | * If inode has a hashed alias, or is a directory and has any alias, | |
890 | * acquire the reference to alias and return it. Otherwise return NULL. | |
891 | * Notice that if inode is a directory there can be only one alias and | |
892 | * it can be unhashed only if it has no children, or if it is the root | |
3ccb354d EB |
893 | * of a filesystem, or if the directory was renamed and d_revalidate |
894 | * was the first vfs operation to notice. | |
1da177e4 | 895 | * |
21c0d8fd | 896 | * If the inode has an IS_ROOT, DCACHE_DISCONNECTED alias, then prefer |
52ed46f0 | 897 | * any other hashed alias over that one. |
1da177e4 | 898 | */ |
52ed46f0 | 899 | static struct dentry *__d_find_alias(struct inode *inode) |
1da177e4 | 900 | { |
da502956 | 901 | struct dentry *alias, *discon_alias; |
1da177e4 | 902 | |
da502956 NP |
903 | again: |
904 | discon_alias = NULL; | |
946e51f2 | 905 | hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) { |
da502956 | 906 | spin_lock(&alias->d_lock); |
1da177e4 | 907 | if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) { |
21c0d8fd | 908 | if (IS_ROOT(alias) && |
da502956 | 909 | (alias->d_flags & DCACHE_DISCONNECTED)) { |
1da177e4 | 910 | discon_alias = alias; |
52ed46f0 | 911 | } else { |
dc0474be | 912 | __dget_dlock(alias); |
da502956 NP |
913 | spin_unlock(&alias->d_lock); |
914 | return alias; | |
915 | } | |
916 | } | |
917 | spin_unlock(&alias->d_lock); | |
918 | } | |
919 | if (discon_alias) { | |
920 | alias = discon_alias; | |
921 | spin_lock(&alias->d_lock); | |
922 | if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) { | |
8d80d7da BF |
923 | __dget_dlock(alias); |
924 | spin_unlock(&alias->d_lock); | |
925 | return alias; | |
1da177e4 | 926 | } |
da502956 NP |
927 | spin_unlock(&alias->d_lock); |
928 | goto again; | |
1da177e4 | 929 | } |
da502956 | 930 | return NULL; |
1da177e4 LT |
931 | } |
932 | ||
da502956 | 933 | struct dentry *d_find_alias(struct inode *inode) |
1da177e4 | 934 | { |
214fda1f DH |
935 | struct dentry *de = NULL; |
936 | ||
b3d9b7a3 | 937 | if (!hlist_empty(&inode->i_dentry)) { |
873feea0 | 938 | spin_lock(&inode->i_lock); |
52ed46f0 | 939 | de = __d_find_alias(inode); |
873feea0 | 940 | spin_unlock(&inode->i_lock); |
214fda1f | 941 | } |
1da177e4 LT |
942 | return de; |
943 | } | |
ec4f8605 | 944 | EXPORT_SYMBOL(d_find_alias); |
1da177e4 LT |
945 | |
946 | /* | |
947 | * Try to kill dentries associated with this inode. | |
948 | * WARNING: you must own a reference to inode. | |
949 | */ | |
950 | void d_prune_aliases(struct inode *inode) | |
951 | { | |
0cdca3f9 | 952 | struct dentry *dentry; |
1da177e4 | 953 | restart: |
873feea0 | 954 | spin_lock(&inode->i_lock); |
946e51f2 | 955 | hlist_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) { |
1da177e4 | 956 | spin_lock(&dentry->d_lock); |
98474236 | 957 | if (!dentry->d_lockref.count) { |
29355c39 AV |
958 | struct dentry *parent = lock_parent(dentry); |
959 | if (likely(!dentry->d_lockref.count)) { | |
960 | __dentry_kill(dentry); | |
4a7795d3 | 961 | dput(parent); |
29355c39 AV |
962 | goto restart; |
963 | } | |
964 | if (parent) | |
965 | spin_unlock(&parent->d_lock); | |
1da177e4 LT |
966 | } |
967 | spin_unlock(&dentry->d_lock); | |
968 | } | |
873feea0 | 969 | spin_unlock(&inode->i_lock); |
1da177e4 | 970 | } |
ec4f8605 | 971 | EXPORT_SYMBOL(d_prune_aliases); |
1da177e4 | 972 | |
3b3f09f4 AV |
973 | /* |
974 | * Lock a dentry from shrink list. | |
975 | * Note that dentry is *not* protected from concurrent dentry_kill(), | |
976 | * d_delete(), etc. It is protected from freeing (by the fact of | |
977 | * being on a shrink list), but everything else is fair game. | |
978 | * Return false if dentry has been disrupted or grabbed, leaving | |
979 | * the caller to kick it off-list. Otherwise, return true and have | |
980 | * that dentry's inode and parent both locked. | |
981 | */ | |
982 | static bool shrink_lock_dentry(struct dentry *dentry) | |
1da177e4 | 983 | { |
3b3f09f4 AV |
984 | struct inode *inode; |
985 | struct dentry *parent; | |
da3bbdd4 | 986 | |
3b3f09f4 AV |
987 | if (dentry->d_lockref.count) |
988 | return false; | |
989 | ||
990 | inode = dentry->d_inode; | |
991 | if (inode && unlikely(!spin_trylock(&inode->i_lock))) { | |
992 | rcu_read_lock(); /* to protect inode */ | |
993 | spin_unlock(&dentry->d_lock); | |
994 | spin_lock(&inode->i_lock); | |
ec33679d | 995 | spin_lock(&dentry->d_lock); |
3b3f09f4 AV |
996 | if (unlikely(dentry->d_lockref.count)) |
997 | goto out; | |
998 | /* changed inode means that somebody had grabbed it */ | |
999 | if (unlikely(inode != dentry->d_inode)) | |
1000 | goto out; | |
1001 | rcu_read_unlock(); | |
1002 | } | |
046b961b | 1003 | |
3b3f09f4 AV |
1004 | parent = dentry->d_parent; |
1005 | if (IS_ROOT(dentry) || likely(spin_trylock(&parent->d_lock))) | |
1006 | return true; | |
dd1f6b2e | 1007 | |
3b3f09f4 AV |
1008 | rcu_read_lock(); /* to protect parent */ |
1009 | spin_unlock(&dentry->d_lock); | |
1010 | parent = READ_ONCE(dentry->d_parent); | |
1011 | spin_lock(&parent->d_lock); | |
1012 | if (unlikely(parent != dentry->d_parent)) { | |
1013 | spin_unlock(&parent->d_lock); | |
1014 | spin_lock(&dentry->d_lock); | |
1015 | goto out; | |
1016 | } | |
1017 | spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); | |
1018 | if (likely(!dentry->d_lockref.count)) { | |
1019 | rcu_read_unlock(); | |
1020 | return true; | |
1021 | } | |
1022 | spin_unlock(&parent->d_lock); | |
1023 | out: | |
1024 | if (inode) | |
1025 | spin_unlock(&inode->i_lock); | |
1026 | rcu_read_unlock(); | |
1027 | return false; | |
1028 | } | |
77812a1e | 1029 | |
3b3f09f4 AV |
1030 | static void shrink_dentry_list(struct list_head *list) |
1031 | { | |
1032 | while (!list_empty(list)) { | |
1033 | struct dentry *dentry, *parent; | |
1034 | struct inode *inode; | |
64fd72e0 | 1035 | |
3b3f09f4 AV |
1036 | dentry = list_entry(list->prev, struct dentry, d_lru); |
1037 | spin_lock(&dentry->d_lock); | |
1038 | if (!shrink_lock_dentry(dentry)) { | |
1039 | bool can_free = false; | |
1040 | d_shrink_del(dentry); | |
1041 | if (dentry->d_lockref.count < 0) | |
1042 | can_free = dentry->d_flags & DCACHE_MAY_FREE; | |
64fd72e0 AV |
1043 | spin_unlock(&dentry->d_lock); |
1044 | if (can_free) | |
1045 | dentry_free(dentry); | |
1046 | continue; | |
1047 | } | |
3b3f09f4 AV |
1048 | d_shrink_del(dentry); |
1049 | parent = dentry->d_parent; | |
ff2fde99 | 1050 | __dentry_kill(dentry); |
3b3f09f4 AV |
1051 | if (parent == dentry) |
1052 | continue; | |
5c47e6d0 AV |
1053 | /* |
1054 | * We need to prune ancestors too. This is necessary to prevent | |
1055 | * quadratic behavior of shrink_dcache_parent(), but is also | |
1056 | * expected to be beneficial in reducing dentry cache | |
1057 | * fragmentation. | |
1058 | */ | |
1059 | dentry = parent; | |
b2b80195 AV |
1060 | while (dentry && !lockref_put_or_lock(&dentry->d_lockref)) { |
1061 | parent = lock_parent(dentry); | |
1062 | if (dentry->d_lockref.count != 1) { | |
1063 | dentry->d_lockref.count--; | |
1064 | spin_unlock(&dentry->d_lock); | |
1065 | if (parent) | |
1066 | spin_unlock(&parent->d_lock); | |
1067 | break; | |
1068 | } | |
1069 | inode = dentry->d_inode; /* can't be NULL */ | |
1070 | if (unlikely(!spin_trylock(&inode->i_lock))) { | |
1071 | spin_unlock(&dentry->d_lock); | |
1072 | if (parent) | |
1073 | spin_unlock(&parent->d_lock); | |
1074 | cpu_relax(); | |
1075 | continue; | |
1076 | } | |
1077 | __dentry_kill(dentry); | |
1078 | dentry = parent; | |
1079 | } | |
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 | |
1121 | * list_lru_add and list_lru_del. List movement in this file | |
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 | long freed; |
1195 | ||
1196 | do { | |
1197 | LIST_HEAD(dispose); | |
1198 | ||
1199 | freed = list_lru_walk(&sb->s_dentry_lru, | |
b17c070f | 1200 | dentry_lru_isolate_shrink, &dispose, 1024); |
3049cfe2 | 1201 | |
4e717f5c GC |
1202 | this_cpu_sub(nr_dentry_unused, freed); |
1203 | shrink_dentry_list(&dispose); | |
b17c070f ST |
1204 | cond_resched(); |
1205 | } while (list_lru_count(&sb->s_dentry_lru) > 0); | |
1da177e4 | 1206 | } |
ec4f8605 | 1207 | EXPORT_SYMBOL(shrink_dcache_sb); |
1da177e4 | 1208 | |
db14fc3a MS |
1209 | /** |
1210 | * enum d_walk_ret - action to talke during tree walk | |
1211 | * @D_WALK_CONTINUE: contrinue walk | |
1212 | * @D_WALK_QUIT: quit walk | |
1213 | * @D_WALK_NORETRY: quit when retry is needed | |
1214 | * @D_WALK_SKIP: skip this dentry and its children | |
1215 | */ | |
1216 | enum d_walk_ret { | |
1217 | D_WALK_CONTINUE, | |
1218 | D_WALK_QUIT, | |
1219 | D_WALK_NORETRY, | |
1220 | D_WALK_SKIP, | |
1221 | }; | |
c826cb7d | 1222 | |
1da177e4 | 1223 | /** |
db14fc3a MS |
1224 | * d_walk - walk the dentry tree |
1225 | * @parent: start of walk | |
1226 | * @data: data passed to @enter() and @finish() | |
1227 | * @enter: callback when first entering the dentry | |
1228 | * @finish: callback when successfully finished the walk | |
1da177e4 | 1229 | * |
db14fc3a | 1230 | * The @enter() and @finish() callbacks are called with d_lock held. |
1da177e4 | 1231 | */ |
db14fc3a MS |
1232 | static void d_walk(struct dentry *parent, void *data, |
1233 | enum d_walk_ret (*enter)(void *, struct dentry *), | |
1234 | void (*finish)(void *)) | |
1da177e4 | 1235 | { |
949854d0 | 1236 | struct dentry *this_parent; |
1da177e4 | 1237 | struct list_head *next; |
48f5ec21 | 1238 | unsigned seq = 0; |
db14fc3a MS |
1239 | enum d_walk_ret ret; |
1240 | bool retry = true; | |
949854d0 | 1241 | |
58db63d0 | 1242 | again: |
48f5ec21 | 1243 | read_seqbegin_or_lock(&rename_lock, &seq); |
58db63d0 | 1244 | this_parent = parent; |
2fd6b7f5 | 1245 | spin_lock(&this_parent->d_lock); |
db14fc3a MS |
1246 | |
1247 | ret = enter(data, this_parent); | |
1248 | switch (ret) { | |
1249 | case D_WALK_CONTINUE: | |
1250 | break; | |
1251 | case D_WALK_QUIT: | |
1252 | case D_WALK_SKIP: | |
1253 | goto out_unlock; | |
1254 | case D_WALK_NORETRY: | |
1255 | retry = false; | |
1256 | break; | |
1257 | } | |
1da177e4 LT |
1258 | repeat: |
1259 | next = this_parent->d_subdirs.next; | |
1260 | resume: | |
1261 | while (next != &this_parent->d_subdirs) { | |
1262 | struct list_head *tmp = next; | |
946e51f2 | 1263 | struct dentry *dentry = list_entry(tmp, struct dentry, d_child); |
1da177e4 | 1264 | next = tmp->next; |
2fd6b7f5 | 1265 | |
ba65dc5e AV |
1266 | if (unlikely(dentry->d_flags & DCACHE_DENTRY_CURSOR)) |
1267 | continue; | |
1268 | ||
2fd6b7f5 | 1269 | spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); |
db14fc3a MS |
1270 | |
1271 | ret = enter(data, dentry); | |
1272 | switch (ret) { | |
1273 | case D_WALK_CONTINUE: | |
1274 | break; | |
1275 | case D_WALK_QUIT: | |
2fd6b7f5 | 1276 | spin_unlock(&dentry->d_lock); |
db14fc3a MS |
1277 | goto out_unlock; |
1278 | case D_WALK_NORETRY: | |
1279 | retry = false; | |
1280 | break; | |
1281 | case D_WALK_SKIP: | |
1282 | spin_unlock(&dentry->d_lock); | |
1283 | continue; | |
2fd6b7f5 | 1284 | } |
db14fc3a | 1285 | |
1da177e4 | 1286 | if (!list_empty(&dentry->d_subdirs)) { |
2fd6b7f5 NP |
1287 | spin_unlock(&this_parent->d_lock); |
1288 | spin_release(&dentry->d_lock.dep_map, 1, _RET_IP_); | |
1da177e4 | 1289 | this_parent = dentry; |
2fd6b7f5 | 1290 | spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_); |
1da177e4 LT |
1291 | goto repeat; |
1292 | } | |
2fd6b7f5 | 1293 | spin_unlock(&dentry->d_lock); |
1da177e4 LT |
1294 | } |
1295 | /* | |
1296 | * All done at this level ... ascend and resume the search. | |
1297 | */ | |
ca5358ef AV |
1298 | rcu_read_lock(); |
1299 | ascend: | |
1da177e4 | 1300 | if (this_parent != parent) { |
c826cb7d | 1301 | struct dentry *child = this_parent; |
31dec132 AV |
1302 | this_parent = child->d_parent; |
1303 | ||
31dec132 AV |
1304 | spin_unlock(&child->d_lock); |
1305 | spin_lock(&this_parent->d_lock); | |
1306 | ||
ca5358ef AV |
1307 | /* might go back up the wrong parent if we have had a rename. */ |
1308 | if (need_seqretry(&rename_lock, seq)) | |
949854d0 | 1309 | goto rename_retry; |
2159184e AV |
1310 | /* go into the first sibling still alive */ |
1311 | do { | |
1312 | next = child->d_child.next; | |
ca5358ef AV |
1313 | if (next == &this_parent->d_subdirs) |
1314 | goto ascend; | |
1315 | child = list_entry(next, struct dentry, d_child); | |
2159184e | 1316 | } while (unlikely(child->d_flags & DCACHE_DENTRY_KILLED)); |
31dec132 | 1317 | rcu_read_unlock(); |
1da177e4 LT |
1318 | goto resume; |
1319 | } | |
ca5358ef | 1320 | if (need_seqretry(&rename_lock, seq)) |
949854d0 | 1321 | goto rename_retry; |
ca5358ef | 1322 | rcu_read_unlock(); |
db14fc3a MS |
1323 | if (finish) |
1324 | finish(data); | |
1325 | ||
1326 | out_unlock: | |
1327 | spin_unlock(&this_parent->d_lock); | |
48f5ec21 | 1328 | done_seqretry(&rename_lock, seq); |
db14fc3a | 1329 | return; |
58db63d0 NP |
1330 | |
1331 | rename_retry: | |
ca5358ef AV |
1332 | spin_unlock(&this_parent->d_lock); |
1333 | rcu_read_unlock(); | |
1334 | BUG_ON(seq & 1); | |
db14fc3a MS |
1335 | if (!retry) |
1336 | return; | |
48f5ec21 | 1337 | seq = 1; |
58db63d0 | 1338 | goto again; |
1da177e4 | 1339 | } |
db14fc3a | 1340 | |
01619491 IK |
1341 | struct check_mount { |
1342 | struct vfsmount *mnt; | |
1343 | unsigned int mounted; | |
1344 | }; | |
1345 | ||
1346 | static enum d_walk_ret path_check_mount(void *data, struct dentry *dentry) | |
1347 | { | |
1348 | struct check_mount *info = data; | |
1349 | struct path path = { .mnt = info->mnt, .dentry = dentry }; | |
1350 | ||
1351 | if (likely(!d_mountpoint(dentry))) | |
1352 | return D_WALK_CONTINUE; | |
1353 | if (__path_is_mountpoint(&path)) { | |
1354 | info->mounted = 1; | |
1355 | return D_WALK_QUIT; | |
1356 | } | |
1357 | return D_WALK_CONTINUE; | |
1358 | } | |
1359 | ||
1360 | /** | |
1361 | * path_has_submounts - check for mounts over a dentry in the | |
1362 | * current namespace. | |
1363 | * @parent: path to check. | |
1364 | * | |
1365 | * Return true if the parent or its subdirectories contain | |
1366 | * a mount point in the current namespace. | |
1367 | */ | |
1368 | int path_has_submounts(const struct path *parent) | |
1369 | { | |
1370 | struct check_mount data = { .mnt = parent->mnt, .mounted = 0 }; | |
1371 | ||
1372 | read_seqlock_excl(&mount_lock); | |
1373 | d_walk(parent->dentry, &data, path_check_mount, NULL); | |
1374 | read_sequnlock_excl(&mount_lock); | |
1375 | ||
1376 | return data.mounted; | |
1377 | } | |
1378 | EXPORT_SYMBOL(path_has_submounts); | |
1379 | ||
eed81007 MS |
1380 | /* |
1381 | * Called by mount code to set a mountpoint and check if the mountpoint is | |
1382 | * reachable (e.g. NFS can unhash a directory dentry and then the complete | |
1383 | * subtree can become unreachable). | |
1384 | * | |
1ffe46d1 | 1385 | * Only one of d_invalidate() and d_set_mounted() must succeed. For |
eed81007 MS |
1386 | * this reason take rename_lock and d_lock on dentry and ancestors. |
1387 | */ | |
1388 | int d_set_mounted(struct dentry *dentry) | |
1389 | { | |
1390 | struct dentry *p; | |
1391 | int ret = -ENOENT; | |
1392 | write_seqlock(&rename_lock); | |
1393 | for (p = dentry->d_parent; !IS_ROOT(p); p = p->d_parent) { | |
1ffe46d1 | 1394 | /* Need exclusion wrt. d_invalidate() */ |
eed81007 MS |
1395 | spin_lock(&p->d_lock); |
1396 | if (unlikely(d_unhashed(p))) { | |
1397 | spin_unlock(&p->d_lock); | |
1398 | goto out; | |
1399 | } | |
1400 | spin_unlock(&p->d_lock); | |
1401 | } | |
1402 | spin_lock(&dentry->d_lock); | |
1403 | if (!d_unlinked(dentry)) { | |
3895dbf8 EB |
1404 | ret = -EBUSY; |
1405 | if (!d_mountpoint(dentry)) { | |
1406 | dentry->d_flags |= DCACHE_MOUNTED; | |
1407 | ret = 0; | |
1408 | } | |
eed81007 MS |
1409 | } |
1410 | spin_unlock(&dentry->d_lock); | |
1411 | out: | |
1412 | write_sequnlock(&rename_lock); | |
1413 | return ret; | |
1414 | } | |
1415 | ||
1da177e4 | 1416 | /* |
fd517909 | 1417 | * Search the dentry child list of the specified parent, |
1da177e4 LT |
1418 | * and move any unused dentries to the end of the unused |
1419 | * list for prune_dcache(). We descend to the next level | |
1420 | * whenever the d_subdirs list is non-empty and continue | |
1421 | * searching. | |
1422 | * | |
1423 | * It returns zero iff there are no unused children, | |
1424 | * otherwise it returns the number of children moved to | |
1425 | * the end of the unused list. This may not be the total | |
1426 | * number of unused children, because select_parent can | |
1427 | * drop the lock and return early due to latency | |
1428 | * constraints. | |
1429 | */ | |
1da177e4 | 1430 | |
db14fc3a MS |
1431 | struct select_data { |
1432 | struct dentry *start; | |
1433 | struct list_head dispose; | |
1434 | int found; | |
1435 | }; | |
23044507 | 1436 | |
db14fc3a MS |
1437 | static enum d_walk_ret select_collect(void *_data, struct dentry *dentry) |
1438 | { | |
1439 | struct select_data *data = _data; | |
1440 | enum d_walk_ret ret = D_WALK_CONTINUE; | |
1da177e4 | 1441 | |
db14fc3a MS |
1442 | if (data->start == dentry) |
1443 | goto out; | |
2fd6b7f5 | 1444 | |
fe91522a | 1445 | if (dentry->d_flags & DCACHE_SHRINK_LIST) { |
db14fc3a | 1446 | data->found++; |
fe91522a AV |
1447 | } else { |
1448 | if (dentry->d_flags & DCACHE_LRU_LIST) | |
1449 | d_lru_del(dentry); | |
1450 | if (!dentry->d_lockref.count) { | |
1451 | d_shrink_add(dentry, &data->dispose); | |
1452 | data->found++; | |
1453 | } | |
1da177e4 | 1454 | } |
db14fc3a MS |
1455 | /* |
1456 | * We can return to the caller if we have found some (this | |
1457 | * ensures forward progress). We'll be coming back to find | |
1458 | * the rest. | |
1459 | */ | |
fe91522a AV |
1460 | if (!list_empty(&data->dispose)) |
1461 | ret = need_resched() ? D_WALK_QUIT : D_WALK_NORETRY; | |
1da177e4 | 1462 | out: |
db14fc3a | 1463 | return ret; |
1da177e4 LT |
1464 | } |
1465 | ||
1466 | /** | |
1467 | * shrink_dcache_parent - prune dcache | |
1468 | * @parent: parent of entries to prune | |
1469 | * | |
1470 | * Prune the dcache to remove unused children of the parent dentry. | |
1471 | */ | |
db14fc3a | 1472 | void shrink_dcache_parent(struct dentry *parent) |
1da177e4 | 1473 | { |
db14fc3a MS |
1474 | for (;;) { |
1475 | struct select_data data; | |
1da177e4 | 1476 | |
db14fc3a MS |
1477 | INIT_LIST_HEAD(&data.dispose); |
1478 | data.start = parent; | |
1479 | data.found = 0; | |
1480 | ||
1481 | d_walk(parent, &data, select_collect, NULL); | |
1482 | if (!data.found) | |
1483 | break; | |
1484 | ||
1485 | shrink_dentry_list(&data.dispose); | |
421348f1 GT |
1486 | cond_resched(); |
1487 | } | |
1da177e4 | 1488 | } |
ec4f8605 | 1489 | EXPORT_SYMBOL(shrink_dcache_parent); |
1da177e4 | 1490 | |
9c8c10e2 | 1491 | static enum d_walk_ret umount_check(void *_data, struct dentry *dentry) |
42c32608 | 1492 | { |
9c8c10e2 AV |
1493 | /* it has busy descendents; complain about those instead */ |
1494 | if (!list_empty(&dentry->d_subdirs)) | |
1495 | return D_WALK_CONTINUE; | |
42c32608 | 1496 | |
9c8c10e2 AV |
1497 | /* root with refcount 1 is fine */ |
1498 | if (dentry == _data && dentry->d_lockref.count == 1) | |
1499 | return D_WALK_CONTINUE; | |
1500 | ||
1501 | printk(KERN_ERR "BUG: Dentry %p{i=%lx,n=%pd} " | |
1502 | " still in use (%d) [unmount of %s %s]\n", | |
42c32608 AV |
1503 | dentry, |
1504 | dentry->d_inode ? | |
1505 | dentry->d_inode->i_ino : 0UL, | |
9c8c10e2 | 1506 | dentry, |
42c32608 AV |
1507 | dentry->d_lockref.count, |
1508 | dentry->d_sb->s_type->name, | |
1509 | dentry->d_sb->s_id); | |
9c8c10e2 AV |
1510 | WARN_ON(1); |
1511 | return D_WALK_CONTINUE; | |
1512 | } | |
1513 | ||
1514 | static void do_one_tree(struct dentry *dentry) | |
1515 | { | |
1516 | shrink_dcache_parent(dentry); | |
1517 | d_walk(dentry, dentry, umount_check, NULL); | |
1518 | d_drop(dentry); | |
1519 | dput(dentry); | |
42c32608 AV |
1520 | } |
1521 | ||
1522 | /* | |
1523 | * destroy the dentries attached to a superblock on unmounting | |
1524 | */ | |
1525 | void shrink_dcache_for_umount(struct super_block *sb) | |
1526 | { | |
1527 | struct dentry *dentry; | |
1528 | ||
9c8c10e2 | 1529 | WARN(down_read_trylock(&sb->s_umount), "s_umount should've been locked"); |
42c32608 AV |
1530 | |
1531 | dentry = sb->s_root; | |
1532 | sb->s_root = NULL; | |
9c8c10e2 | 1533 | do_one_tree(dentry); |
42c32608 | 1534 | |
f1ee6162 N |
1535 | while (!hlist_bl_empty(&sb->s_roots)) { |
1536 | dentry = dget(hlist_bl_entry(hlist_bl_first(&sb->s_roots), struct dentry, d_hash)); | |
9c8c10e2 | 1537 | do_one_tree(dentry); |
42c32608 AV |
1538 | } |
1539 | } | |
1540 | ||
8ed936b5 EB |
1541 | struct detach_data { |
1542 | struct select_data select; | |
1543 | struct dentry *mountpoint; | |
1544 | }; | |
1545 | static enum d_walk_ret detach_and_collect(void *_data, struct dentry *dentry) | |
848ac114 | 1546 | { |
8ed936b5 | 1547 | struct detach_data *data = _data; |
848ac114 MS |
1548 | |
1549 | if (d_mountpoint(dentry)) { | |
8ed936b5 EB |
1550 | __dget_dlock(dentry); |
1551 | data->mountpoint = dentry; | |
848ac114 MS |
1552 | return D_WALK_QUIT; |
1553 | } | |
1554 | ||
8ed936b5 | 1555 | return select_collect(&data->select, dentry); |
848ac114 MS |
1556 | } |
1557 | ||
1558 | static void check_and_drop(void *_data) | |
1559 | { | |
8ed936b5 | 1560 | struct detach_data *data = _data; |
848ac114 | 1561 | |
81be24d2 | 1562 | if (!data->mountpoint && list_empty(&data->select.dispose)) |
8ed936b5 | 1563 | __d_drop(data->select.start); |
848ac114 MS |
1564 | } |
1565 | ||
1566 | /** | |
1ffe46d1 EB |
1567 | * d_invalidate - detach submounts, prune dcache, and drop |
1568 | * @dentry: dentry to invalidate (aka detach, prune and drop) | |
1569 | * | |
1ffe46d1 | 1570 | * no dcache lock. |
848ac114 | 1571 | * |
8ed936b5 EB |
1572 | * The final d_drop is done as an atomic operation relative to |
1573 | * rename_lock ensuring there are no races with d_set_mounted. This | |
1574 | * ensures there are no unhashed dentries on the path to a mountpoint. | |
848ac114 | 1575 | */ |
5542aa2f | 1576 | void d_invalidate(struct dentry *dentry) |
848ac114 | 1577 | { |
1ffe46d1 EB |
1578 | /* |
1579 | * If it's already been dropped, return OK. | |
1580 | */ | |
1581 | spin_lock(&dentry->d_lock); | |
1582 | if (d_unhashed(dentry)) { | |
1583 | spin_unlock(&dentry->d_lock); | |
5542aa2f | 1584 | return; |
1ffe46d1 EB |
1585 | } |
1586 | spin_unlock(&dentry->d_lock); | |
1587 | ||
848ac114 MS |
1588 | /* Negative dentries can be dropped without further checks */ |
1589 | if (!dentry->d_inode) { | |
1590 | d_drop(dentry); | |
5542aa2f | 1591 | return; |
848ac114 MS |
1592 | } |
1593 | ||
1594 | for (;;) { | |
8ed936b5 | 1595 | struct detach_data data; |
848ac114 | 1596 | |
8ed936b5 EB |
1597 | data.mountpoint = NULL; |
1598 | INIT_LIST_HEAD(&data.select.dispose); | |
1599 | data.select.start = dentry; | |
1600 | data.select.found = 0; | |
1601 | ||
1602 | d_walk(dentry, &data, detach_and_collect, check_and_drop); | |
848ac114 | 1603 | |
81be24d2 | 1604 | if (!list_empty(&data.select.dispose)) |
8ed936b5 | 1605 | shrink_dentry_list(&data.select.dispose); |
81be24d2 AV |
1606 | else if (!data.mountpoint) |
1607 | return; | |
848ac114 | 1608 | |
8ed936b5 EB |
1609 | if (data.mountpoint) { |
1610 | detach_mounts(data.mountpoint); | |
1611 | dput(data.mountpoint); | |
1612 | } | |
848ac114 MS |
1613 | cond_resched(); |
1614 | } | |
848ac114 | 1615 | } |
1ffe46d1 | 1616 | EXPORT_SYMBOL(d_invalidate); |
848ac114 | 1617 | |
1da177e4 | 1618 | /** |
a4464dbc AV |
1619 | * __d_alloc - allocate a dcache entry |
1620 | * @sb: filesystem it will belong to | |
1da177e4 LT |
1621 | * @name: qstr of the name |
1622 | * | |
1623 | * Allocates a dentry. It returns %NULL if there is insufficient memory | |
1624 | * available. On a success the dentry is returned. The name passed in is | |
1625 | * copied and the copy passed in may be reused after this call. | |
1626 | */ | |
1627 | ||
a4464dbc | 1628 | struct dentry *__d_alloc(struct super_block *sb, const struct qstr *name) |
1da177e4 LT |
1629 | { |
1630 | struct dentry *dentry; | |
1631 | char *dname; | |
285b102d | 1632 | int err; |
1da177e4 | 1633 | |
e12ba74d | 1634 | dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL); |
1da177e4 LT |
1635 | if (!dentry) |
1636 | return NULL; | |
1637 | ||
6326c71f LT |
1638 | /* |
1639 | * We guarantee that the inline name is always NUL-terminated. | |
1640 | * This way the memcpy() done by the name switching in rename | |
1641 | * will still always have a NUL at the end, even if we might | |
1642 | * be overwriting an internal NUL character | |
1643 | */ | |
1644 | dentry->d_iname[DNAME_INLINE_LEN-1] = 0; | |
798434bd | 1645 | if (unlikely(!name)) { |
cdf01226 | 1646 | name = &slash_name; |
798434bd AV |
1647 | dname = dentry->d_iname; |
1648 | } else if (name->len > DNAME_INLINE_LEN-1) { | |
8d85b484 | 1649 | size_t size = offsetof(struct external_name, name[1]); |
5d097056 VD |
1650 | struct external_name *p = kmalloc(size + name->len, |
1651 | GFP_KERNEL_ACCOUNT); | |
8d85b484 | 1652 | if (!p) { |
1da177e4 LT |
1653 | kmem_cache_free(dentry_cache, dentry); |
1654 | return NULL; | |
1655 | } | |
8d85b484 AV |
1656 | atomic_set(&p->u.count, 1); |
1657 | dname = p->name; | |
1da177e4 LT |
1658 | } else { |
1659 | dname = dentry->d_iname; | |
1660 | } | |
1da177e4 LT |
1661 | |
1662 | dentry->d_name.len = name->len; | |
1663 | dentry->d_name.hash = name->hash; | |
1664 | memcpy(dname, name->name, name->len); | |
1665 | dname[name->len] = 0; | |
1666 | ||
6326c71f | 1667 | /* Make sure we always see the terminating NUL character */ |
7088efa9 | 1668 | smp_store_release(&dentry->d_name.name, dname); /* ^^^ */ |
6326c71f | 1669 | |
98474236 | 1670 | dentry->d_lockref.count = 1; |
dea3667b | 1671 | dentry->d_flags = 0; |
1da177e4 | 1672 | spin_lock_init(&dentry->d_lock); |
31e6b01f | 1673 | seqcount_init(&dentry->d_seq); |
1da177e4 | 1674 | dentry->d_inode = NULL; |
a4464dbc AV |
1675 | dentry->d_parent = dentry; |
1676 | dentry->d_sb = sb; | |
1da177e4 LT |
1677 | dentry->d_op = NULL; |
1678 | dentry->d_fsdata = NULL; | |
ceb5bdc2 | 1679 | INIT_HLIST_BL_NODE(&dentry->d_hash); |
1da177e4 LT |
1680 | INIT_LIST_HEAD(&dentry->d_lru); |
1681 | INIT_LIST_HEAD(&dentry->d_subdirs); | |
946e51f2 AV |
1682 | INIT_HLIST_NODE(&dentry->d_u.d_alias); |
1683 | INIT_LIST_HEAD(&dentry->d_child); | |
a4464dbc | 1684 | d_set_d_op(dentry, dentry->d_sb->s_d_op); |
1da177e4 | 1685 | |
285b102d MS |
1686 | if (dentry->d_op && dentry->d_op->d_init) { |
1687 | err = dentry->d_op->d_init(dentry); | |
1688 | if (err) { | |
1689 | if (dname_external(dentry)) | |
1690 | kfree(external_name(dentry)); | |
1691 | kmem_cache_free(dentry_cache, dentry); | |
1692 | return NULL; | |
1693 | } | |
1694 | } | |
1695 | ||
3e880fb5 | 1696 | this_cpu_inc(nr_dentry); |
312d3ca8 | 1697 | |
1da177e4 LT |
1698 | return dentry; |
1699 | } | |
a4464dbc AV |
1700 | |
1701 | /** | |
1702 | * d_alloc - allocate a dcache entry | |
1703 | * @parent: parent of entry to allocate | |
1704 | * @name: qstr of the name | |
1705 | * | |
1706 | * Allocates a dentry. It returns %NULL if there is insufficient memory | |
1707 | * available. On a success the dentry is returned. The name passed in is | |
1708 | * copied and the copy passed in may be reused after this call. | |
1709 | */ | |
1710 | struct dentry *d_alloc(struct dentry * parent, const struct qstr *name) | |
1711 | { | |
1712 | struct dentry *dentry = __d_alloc(parent->d_sb, name); | |
1713 | if (!dentry) | |
1714 | return NULL; | |
3d56c25e | 1715 | dentry->d_flags |= DCACHE_RCUACCESS; |
a4464dbc AV |
1716 | spin_lock(&parent->d_lock); |
1717 | /* | |
1718 | * don't need child lock because it is not subject | |
1719 | * to concurrency here | |
1720 | */ | |
1721 | __dget_dlock(parent); | |
1722 | dentry->d_parent = parent; | |
946e51f2 | 1723 | list_add(&dentry->d_child, &parent->d_subdirs); |
a4464dbc AV |
1724 | spin_unlock(&parent->d_lock); |
1725 | ||
1726 | return dentry; | |
1727 | } | |
ec4f8605 | 1728 | EXPORT_SYMBOL(d_alloc); |
1da177e4 | 1729 | |
f9c34674 MS |
1730 | struct dentry *d_alloc_anon(struct super_block *sb) |
1731 | { | |
1732 | return __d_alloc(sb, NULL); | |
1733 | } | |
1734 | EXPORT_SYMBOL(d_alloc_anon); | |
1735 | ||
ba65dc5e AV |
1736 | struct dentry *d_alloc_cursor(struct dentry * parent) |
1737 | { | |
f9c34674 | 1738 | struct dentry *dentry = d_alloc_anon(parent->d_sb); |
ba65dc5e AV |
1739 | if (dentry) { |
1740 | dentry->d_flags |= DCACHE_RCUACCESS | DCACHE_DENTRY_CURSOR; | |
1741 | dentry->d_parent = dget(parent); | |
1742 | } | |
1743 | return dentry; | |
1744 | } | |
1745 | ||
e1a24bb0 BF |
1746 | /** |
1747 | * d_alloc_pseudo - allocate a dentry (for lookup-less filesystems) | |
1748 | * @sb: the superblock | |
1749 | * @name: qstr of the name | |
1750 | * | |
1751 | * For a filesystem that just pins its dentries in memory and never | |
1752 | * performs lookups at all, return an unhashed IS_ROOT dentry. | |
1753 | */ | |
4b936885 NP |
1754 | struct dentry *d_alloc_pseudo(struct super_block *sb, const struct qstr *name) |
1755 | { | |
e1a24bb0 | 1756 | return __d_alloc(sb, name); |
4b936885 NP |
1757 | } |
1758 | EXPORT_SYMBOL(d_alloc_pseudo); | |
1759 | ||
1da177e4 LT |
1760 | struct dentry *d_alloc_name(struct dentry *parent, const char *name) |
1761 | { | |
1762 | struct qstr q; | |
1763 | ||
1764 | q.name = name; | |
8387ff25 | 1765 | q.hash_len = hashlen_string(parent, name); |
1da177e4 LT |
1766 | return d_alloc(parent, &q); |
1767 | } | |
ef26ca97 | 1768 | EXPORT_SYMBOL(d_alloc_name); |
1da177e4 | 1769 | |
fb045adb NP |
1770 | void d_set_d_op(struct dentry *dentry, const struct dentry_operations *op) |
1771 | { | |
6f7f7caa LT |
1772 | WARN_ON_ONCE(dentry->d_op); |
1773 | WARN_ON_ONCE(dentry->d_flags & (DCACHE_OP_HASH | | |
fb045adb NP |
1774 | DCACHE_OP_COMPARE | |
1775 | DCACHE_OP_REVALIDATE | | |
ecf3d1f1 | 1776 | DCACHE_OP_WEAK_REVALIDATE | |
4bacc9c9 | 1777 | DCACHE_OP_DELETE | |
d101a125 | 1778 | DCACHE_OP_REAL)); |
fb045adb NP |
1779 | dentry->d_op = op; |
1780 | if (!op) | |
1781 | return; | |
1782 | if (op->d_hash) | |
1783 | dentry->d_flags |= DCACHE_OP_HASH; | |
1784 | if (op->d_compare) | |
1785 | dentry->d_flags |= DCACHE_OP_COMPARE; | |
1786 | if (op->d_revalidate) | |
1787 | dentry->d_flags |= DCACHE_OP_REVALIDATE; | |
ecf3d1f1 JL |
1788 | if (op->d_weak_revalidate) |
1789 | dentry->d_flags |= DCACHE_OP_WEAK_REVALIDATE; | |
fb045adb NP |
1790 | if (op->d_delete) |
1791 | dentry->d_flags |= DCACHE_OP_DELETE; | |
f0023bc6 SW |
1792 | if (op->d_prune) |
1793 | dentry->d_flags |= DCACHE_OP_PRUNE; | |
d101a125 MS |
1794 | if (op->d_real) |
1795 | dentry->d_flags |= DCACHE_OP_REAL; | |
fb045adb NP |
1796 | |
1797 | } | |
1798 | EXPORT_SYMBOL(d_set_d_op); | |
1799 | ||
df1a085a DH |
1800 | |
1801 | /* | |
1802 | * d_set_fallthru - Mark a dentry as falling through to a lower layer | |
1803 | * @dentry - The dentry to mark | |
1804 | * | |
1805 | * Mark a dentry as falling through to the lower layer (as set with | |
1806 | * d_pin_lower()). This flag may be recorded on the medium. | |
1807 | */ | |
1808 | void d_set_fallthru(struct dentry *dentry) | |
1809 | { | |
1810 | spin_lock(&dentry->d_lock); | |
1811 | dentry->d_flags |= DCACHE_FALLTHRU; | |
1812 | spin_unlock(&dentry->d_lock); | |
1813 | } | |
1814 | EXPORT_SYMBOL(d_set_fallthru); | |
1815 | ||
b18825a7 DH |
1816 | static unsigned d_flags_for_inode(struct inode *inode) |
1817 | { | |
44bdb5e5 | 1818 | unsigned add_flags = DCACHE_REGULAR_TYPE; |
b18825a7 DH |
1819 | |
1820 | if (!inode) | |
1821 | return DCACHE_MISS_TYPE; | |
1822 | ||
1823 | if (S_ISDIR(inode->i_mode)) { | |
1824 | add_flags = DCACHE_DIRECTORY_TYPE; | |
1825 | if (unlikely(!(inode->i_opflags & IOP_LOOKUP))) { | |
1826 | if (unlikely(!inode->i_op->lookup)) | |
1827 | add_flags = DCACHE_AUTODIR_TYPE; | |
1828 | else | |
1829 | inode->i_opflags |= IOP_LOOKUP; | |
1830 | } | |
44bdb5e5 DH |
1831 | goto type_determined; |
1832 | } | |
1833 | ||
1834 | if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) { | |
6b255391 | 1835 | if (unlikely(inode->i_op->get_link)) { |
b18825a7 | 1836 | add_flags = DCACHE_SYMLINK_TYPE; |
44bdb5e5 DH |
1837 | goto type_determined; |
1838 | } | |
1839 | inode->i_opflags |= IOP_NOFOLLOW; | |
b18825a7 DH |
1840 | } |
1841 | ||
44bdb5e5 DH |
1842 | if (unlikely(!S_ISREG(inode->i_mode))) |
1843 | add_flags = DCACHE_SPECIAL_TYPE; | |
1844 | ||
1845 | type_determined: | |
b18825a7 DH |
1846 | if (unlikely(IS_AUTOMOUNT(inode))) |
1847 | add_flags |= DCACHE_NEED_AUTOMOUNT; | |
1848 | return add_flags; | |
1849 | } | |
1850 | ||
360da900 OH |
1851 | static void __d_instantiate(struct dentry *dentry, struct inode *inode) |
1852 | { | |
b18825a7 | 1853 | unsigned add_flags = d_flags_for_inode(inode); |
85c7f810 | 1854 | WARN_ON(d_in_lookup(dentry)); |
b18825a7 | 1855 | |
b23fb0a6 | 1856 | spin_lock(&dentry->d_lock); |
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 | |
b70a80e7 MS |
1892 | /** |
1893 | * d_instantiate_no_diralias - instantiate a non-aliased dentry | |
1894 | * @entry: dentry to complete | |
1895 | * @inode: inode to attach to this dentry | |
1896 | * | |
1897 | * Fill in inode information in the entry. If a directory alias is found, then | |
1898 | * return an error (and drop inode). Together with d_materialise_unique() this | |
1899 | * guarantees that a directory inode may never have more than one alias. | |
1900 | */ | |
1901 | int d_instantiate_no_diralias(struct dentry *entry, struct inode *inode) | |
1902 | { | |
946e51f2 | 1903 | BUG_ON(!hlist_unhashed(&entry->d_u.d_alias)); |
b70a80e7 | 1904 | |
b9680917 | 1905 | security_d_instantiate(entry, inode); |
b70a80e7 MS |
1906 | spin_lock(&inode->i_lock); |
1907 | if (S_ISDIR(inode->i_mode) && !hlist_empty(&inode->i_dentry)) { | |
1908 | spin_unlock(&inode->i_lock); | |
1909 | iput(inode); | |
1910 | return -EBUSY; | |
1911 | } | |
1912 | __d_instantiate(entry, inode); | |
1913 | spin_unlock(&inode->i_lock); | |
b70a80e7 MS |
1914 | |
1915 | return 0; | |
1916 | } | |
1917 | EXPORT_SYMBOL(d_instantiate_no_diralias); | |
1918 | ||
adc0e91a AV |
1919 | struct dentry *d_make_root(struct inode *root_inode) |
1920 | { | |
1921 | struct dentry *res = NULL; | |
1922 | ||
1923 | if (root_inode) { | |
f9c34674 | 1924 | res = d_alloc_anon(root_inode->i_sb); |
adc0e91a AV |
1925 | if (res) |
1926 | d_instantiate(res, root_inode); | |
1927 | else | |
1928 | iput(root_inode); | |
1929 | } | |
1930 | return res; | |
1931 | } | |
1932 | EXPORT_SYMBOL(d_make_root); | |
1933 | ||
d891eedb BF |
1934 | static struct dentry * __d_find_any_alias(struct inode *inode) |
1935 | { | |
1936 | struct dentry *alias; | |
1937 | ||
b3d9b7a3 | 1938 | if (hlist_empty(&inode->i_dentry)) |
d891eedb | 1939 | return NULL; |
946e51f2 | 1940 | alias = hlist_entry(inode->i_dentry.first, struct dentry, d_u.d_alias); |
d891eedb BF |
1941 | __dget(alias); |
1942 | return alias; | |
1943 | } | |
1944 | ||
46f72b34 SW |
1945 | /** |
1946 | * d_find_any_alias - find any alias for a given inode | |
1947 | * @inode: inode to find an alias for | |
1948 | * | |
1949 | * If any aliases exist for the given inode, take and return a | |
1950 | * reference for one of them. If no aliases exist, return %NULL. | |
1951 | */ | |
1952 | struct dentry *d_find_any_alias(struct inode *inode) | |
d891eedb BF |
1953 | { |
1954 | struct dentry *de; | |
1955 | ||
1956 | spin_lock(&inode->i_lock); | |
1957 | de = __d_find_any_alias(inode); | |
1958 | spin_unlock(&inode->i_lock); | |
1959 | return de; | |
1960 | } | |
46f72b34 | 1961 | EXPORT_SYMBOL(d_find_any_alias); |
d891eedb | 1962 | |
f9c34674 MS |
1963 | static struct dentry *__d_instantiate_anon(struct dentry *dentry, |
1964 | struct inode *inode, | |
1965 | bool disconnected) | |
4ea3ada2 | 1966 | { |
9308a612 | 1967 | struct dentry *res; |
b18825a7 | 1968 | unsigned add_flags; |
4ea3ada2 | 1969 | |
f9c34674 | 1970 | security_d_instantiate(dentry, inode); |
873feea0 | 1971 | spin_lock(&inode->i_lock); |
d891eedb | 1972 | res = __d_find_any_alias(inode); |
9308a612 | 1973 | if (res) { |
873feea0 | 1974 | spin_unlock(&inode->i_lock); |
f9c34674 | 1975 | dput(dentry); |
9308a612 CH |
1976 | goto out_iput; |
1977 | } | |
1978 | ||
1979 | /* attach a disconnected dentry */ | |
1a0a397e BF |
1980 | add_flags = d_flags_for_inode(inode); |
1981 | ||
1982 | if (disconnected) | |
1983 | add_flags |= DCACHE_DISCONNECTED; | |
b18825a7 | 1984 | |
f9c34674 MS |
1985 | spin_lock(&dentry->d_lock); |
1986 | __d_set_inode_and_type(dentry, inode, add_flags); | |
1987 | hlist_add_head(&dentry->d_u.d_alias, &inode->i_dentry); | |
f1ee6162 | 1988 | if (!disconnected) { |
139351f1 LT |
1989 | hlist_bl_lock(&dentry->d_sb->s_roots); |
1990 | hlist_bl_add_head(&dentry->d_hash, &dentry->d_sb->s_roots); | |
1991 | hlist_bl_unlock(&dentry->d_sb->s_roots); | |
f1ee6162 | 1992 | } |
f9c34674 | 1993 | spin_unlock(&dentry->d_lock); |
873feea0 | 1994 | spin_unlock(&inode->i_lock); |
9308a612 | 1995 | |
f9c34674 | 1996 | return dentry; |
9308a612 CH |
1997 | |
1998 | out_iput: | |
1999 | iput(inode); | |
2000 | return res; | |
4ea3ada2 | 2001 | } |
1a0a397e | 2002 | |
f9c34674 MS |
2003 | struct dentry *d_instantiate_anon(struct dentry *dentry, struct inode *inode) |
2004 | { | |
2005 | return __d_instantiate_anon(dentry, inode, true); | |
2006 | } | |
2007 | EXPORT_SYMBOL(d_instantiate_anon); | |
2008 | ||
2009 | static struct dentry *__d_obtain_alias(struct inode *inode, bool disconnected) | |
2010 | { | |
2011 | struct dentry *tmp; | |
2012 | struct dentry *res; | |
2013 | ||
2014 | if (!inode) | |
2015 | return ERR_PTR(-ESTALE); | |
2016 | if (IS_ERR(inode)) | |
2017 | return ERR_CAST(inode); | |
2018 | ||
2019 | res = d_find_any_alias(inode); | |
2020 | if (res) | |
2021 | goto out_iput; | |
2022 | ||
2023 | tmp = d_alloc_anon(inode->i_sb); | |
2024 | if (!tmp) { | |
2025 | res = ERR_PTR(-ENOMEM); | |
2026 | goto out_iput; | |
2027 | } | |
2028 | ||
2029 | return __d_instantiate_anon(tmp, inode, disconnected); | |
2030 | ||
2031 | out_iput: | |
2032 | iput(inode); | |
2033 | return res; | |
2034 | } | |
2035 | ||
1a0a397e BF |
2036 | /** |
2037 | * d_obtain_alias - find or allocate a DISCONNECTED dentry for a given inode | |
2038 | * @inode: inode to allocate the dentry for | |
2039 | * | |
2040 | * Obtain a dentry for an inode resulting from NFS filehandle conversion or | |
2041 | * similar open by handle operations. The returned dentry may be anonymous, | |
2042 | * or may have a full name (if the inode was already in the cache). | |
2043 | * | |
2044 | * When called on a directory inode, we must ensure that the inode only ever | |
2045 | * has one dentry. If a dentry is found, that is returned instead of | |
2046 | * allocating a new one. | |
2047 | * | |
2048 | * On successful return, the reference to the inode has been transferred | |
2049 | * to the dentry. In case of an error the reference on the inode is released. | |
2050 | * To make it easier to use in export operations a %NULL or IS_ERR inode may | |
2051 | * be passed in and the error will be propagated to the return value, | |
2052 | * with a %NULL @inode replaced by ERR_PTR(-ESTALE). | |
2053 | */ | |
2054 | struct dentry *d_obtain_alias(struct inode *inode) | |
2055 | { | |
f9c34674 | 2056 | return __d_obtain_alias(inode, true); |
1a0a397e | 2057 | } |
adc48720 | 2058 | EXPORT_SYMBOL(d_obtain_alias); |
1da177e4 | 2059 | |
1a0a397e BF |
2060 | /** |
2061 | * d_obtain_root - find or allocate a dentry for a given inode | |
2062 | * @inode: inode to allocate the dentry for | |
2063 | * | |
2064 | * Obtain an IS_ROOT dentry for the root of a filesystem. | |
2065 | * | |
2066 | * We must ensure that directory inodes only ever have one dentry. If a | |
2067 | * dentry is found, that is returned instead of allocating a new one. | |
2068 | * | |
2069 | * On successful return, the reference to the inode has been transferred | |
2070 | * to the dentry. In case of an error the reference on the inode is | |
2071 | * released. A %NULL or IS_ERR inode may be passed in and will be the | |
2072 | * error will be propagate to the return value, with a %NULL @inode | |
2073 | * replaced by ERR_PTR(-ESTALE). | |
2074 | */ | |
2075 | struct dentry *d_obtain_root(struct inode *inode) | |
2076 | { | |
f9c34674 | 2077 | return __d_obtain_alias(inode, false); |
1a0a397e BF |
2078 | } |
2079 | EXPORT_SYMBOL(d_obtain_root); | |
2080 | ||
9403540c BN |
2081 | /** |
2082 | * d_add_ci - lookup or allocate new dentry with case-exact name | |
2083 | * @inode: the inode case-insensitive lookup has found | |
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 | * This is to avoid filling the dcache with case-insensitive names to the | |
2088 | * same inode, only the actual correct case is stored in the dcache for | |
2089 | * case-insensitive filesystems. | |
2090 | * | |
2091 | * For a case-insensitive lookup match and if the the case-exact dentry | |
2092 | * already exists in in the dcache, use it and return it. | |
2093 | * | |
2094 | * If no entry exists with the exact case name, allocate new dentry with | |
2095 | * the exact case, and return the spliced entry. | |
2096 | */ | |
e45b590b | 2097 | struct dentry *d_add_ci(struct dentry *dentry, struct inode *inode, |
9403540c BN |
2098 | struct qstr *name) |
2099 | { | |
d9171b93 | 2100 | struct dentry *found, *res; |
9403540c | 2101 | |
b6520c81 CH |
2102 | /* |
2103 | * First check if a dentry matching the name already exists, | |
2104 | * if not go ahead and create it now. | |
2105 | */ | |
9403540c | 2106 | found = d_hash_and_lookup(dentry->d_parent, name); |
d9171b93 AV |
2107 | if (found) { |
2108 | iput(inode); | |
2109 | return found; | |
2110 | } | |
2111 | if (d_in_lookup(dentry)) { | |
2112 | found = d_alloc_parallel(dentry->d_parent, name, | |
2113 | dentry->d_wait); | |
2114 | if (IS_ERR(found) || !d_in_lookup(found)) { | |
2115 | iput(inode); | |
2116 | return found; | |
9403540c | 2117 | } |
d9171b93 AV |
2118 | } else { |
2119 | found = d_alloc(dentry->d_parent, name); | |
2120 | if (!found) { | |
2121 | iput(inode); | |
2122 | return ERR_PTR(-ENOMEM); | |
2123 | } | |
2124 | } | |
2125 | res = d_splice_alias(inode, found); | |
2126 | if (res) { | |
2127 | dput(found); | |
2128 | return res; | |
9403540c | 2129 | } |
4f522a24 | 2130 | return found; |
9403540c | 2131 | } |
ec4f8605 | 2132 | EXPORT_SYMBOL(d_add_ci); |
1da177e4 | 2133 | |
12f8ad4b | 2134 | |
d4c91a8f AV |
2135 | static inline bool d_same_name(const struct dentry *dentry, |
2136 | const struct dentry *parent, | |
2137 | const struct qstr *name) | |
12f8ad4b | 2138 | { |
d4c91a8f AV |
2139 | if (likely(!(parent->d_flags & DCACHE_OP_COMPARE))) { |
2140 | if (dentry->d_name.len != name->len) | |
2141 | return false; | |
2142 | return dentry_cmp(dentry, name->name, name->len) == 0; | |
12f8ad4b | 2143 | } |
6fa67e70 | 2144 | return parent->d_op->d_compare(dentry, |
d4c91a8f AV |
2145 | dentry->d_name.len, dentry->d_name.name, |
2146 | name) == 0; | |
12f8ad4b LT |
2147 | } |
2148 | ||
31e6b01f NP |
2149 | /** |
2150 | * __d_lookup_rcu - search for a dentry (racy, store-free) | |
2151 | * @parent: parent dentry | |
2152 | * @name: qstr of name we wish to find | |
1f1e6e52 | 2153 | * @seqp: returns d_seq value at the point where the dentry was found |
31e6b01f NP |
2154 | * Returns: dentry, or NULL |
2155 | * | |
2156 | * __d_lookup_rcu is the dcache lookup function for rcu-walk name | |
2157 | * resolution (store-free path walking) design described in | |
2158 | * Documentation/filesystems/path-lookup.txt. | |
2159 | * | |
2160 | * This is not to be used outside core vfs. | |
2161 | * | |
2162 | * __d_lookup_rcu must only be used in rcu-walk mode, ie. with vfsmount lock | |
2163 | * held, and rcu_read_lock held. The returned dentry must not be stored into | |
2164 | * without taking d_lock and checking d_seq sequence count against @seq | |
2165 | * returned here. | |
2166 | * | |
15570086 | 2167 | * A refcount may be taken on the found dentry with the d_rcu_to_refcount |
31e6b01f NP |
2168 | * function. |
2169 | * | |
2170 | * Alternatively, __d_lookup_rcu may be called again to look up the child of | |
2171 | * the returned dentry, so long as its parent's seqlock is checked after the | |
2172 | * child is looked up. Thus, an interlocking stepping of sequence lock checks | |
2173 | * is formed, giving integrity down the path walk. | |
12f8ad4b LT |
2174 | * |
2175 | * NOTE! The caller *has* to check the resulting dentry against the sequence | |
2176 | * number we've returned before using any of the resulting dentry state! | |
31e6b01f | 2177 | */ |
8966be90 LT |
2178 | struct dentry *__d_lookup_rcu(const struct dentry *parent, |
2179 | const struct qstr *name, | |
da53be12 | 2180 | unsigned *seqp) |
31e6b01f | 2181 | { |
26fe5750 | 2182 | u64 hashlen = name->hash_len; |
31e6b01f | 2183 | const unsigned char *str = name->name; |
8387ff25 | 2184 | struct hlist_bl_head *b = d_hash(hashlen_hash(hashlen)); |
ceb5bdc2 | 2185 | struct hlist_bl_node *node; |
31e6b01f NP |
2186 | struct dentry *dentry; |
2187 | ||
2188 | /* | |
2189 | * Note: There is significant duplication with __d_lookup_rcu which is | |
2190 | * required to prevent single threaded performance regressions | |
2191 | * especially on architectures where smp_rmb (in seqcounts) are costly. | |
2192 | * Keep the two functions in sync. | |
2193 | */ | |
2194 | ||
2195 | /* | |
2196 | * The hash list is protected using RCU. | |
2197 | * | |
2198 | * Carefully use d_seq when comparing a candidate dentry, to avoid | |
2199 | * races with d_move(). | |
2200 | * | |
2201 | * It is possible that concurrent renames can mess up our list | |
2202 | * walk here and result in missing our dentry, resulting in the | |
2203 | * false-negative result. d_lookup() protects against concurrent | |
2204 | * renames using rename_lock seqlock. | |
2205 | * | |
b0a4bb83 | 2206 | * See Documentation/filesystems/path-lookup.txt for more details. |
31e6b01f | 2207 | */ |
b07ad996 | 2208 | hlist_bl_for_each_entry_rcu(dentry, node, b, d_hash) { |
8966be90 | 2209 | unsigned seq; |
31e6b01f | 2210 | |
31e6b01f | 2211 | seqretry: |
12f8ad4b LT |
2212 | /* |
2213 | * The dentry sequence count protects us from concurrent | |
da53be12 | 2214 | * renames, and thus protects parent and name fields. |
12f8ad4b LT |
2215 | * |
2216 | * The caller must perform a seqcount check in order | |
da53be12 | 2217 | * to do anything useful with the returned dentry. |
12f8ad4b LT |
2218 | * |
2219 | * NOTE! We do a "raw" seqcount_begin here. That means that | |
2220 | * we don't wait for the sequence count to stabilize if it | |
2221 | * is in the middle of a sequence change. If we do the slow | |
2222 | * dentry compare, we will do seqretries until it is stable, | |
2223 | * and if we end up with a successful lookup, we actually | |
2224 | * want to exit RCU lookup anyway. | |
d4c91a8f AV |
2225 | * |
2226 | * Note that raw_seqcount_begin still *does* smp_rmb(), so | |
2227 | * we are still guaranteed NUL-termination of ->d_name.name. | |
12f8ad4b LT |
2228 | */ |
2229 | seq = raw_seqcount_begin(&dentry->d_seq); | |
31e6b01f NP |
2230 | if (dentry->d_parent != parent) |
2231 | continue; | |
2e321806 LT |
2232 | if (d_unhashed(dentry)) |
2233 | continue; | |
12f8ad4b | 2234 | |
830c0f0e | 2235 | if (unlikely(parent->d_flags & DCACHE_OP_COMPARE)) { |
d4c91a8f AV |
2236 | int tlen; |
2237 | const char *tname; | |
26fe5750 LT |
2238 | if (dentry->d_name.hash != hashlen_hash(hashlen)) |
2239 | continue; | |
d4c91a8f AV |
2240 | tlen = dentry->d_name.len; |
2241 | tname = dentry->d_name.name; | |
2242 | /* we want a consistent (name,len) pair */ | |
2243 | if (read_seqcount_retry(&dentry->d_seq, seq)) { | |
2244 | cpu_relax(); | |
12f8ad4b LT |
2245 | goto seqretry; |
2246 | } | |
6fa67e70 | 2247 | if (parent->d_op->d_compare(dentry, |
d4c91a8f AV |
2248 | tlen, tname, name) != 0) |
2249 | continue; | |
2250 | } else { | |
2251 | if (dentry->d_name.hash_len != hashlen) | |
2252 | continue; | |
2253 | if (dentry_cmp(dentry, str, hashlen_len(hashlen)) != 0) | |
2254 | continue; | |
31e6b01f | 2255 | } |
da53be12 | 2256 | *seqp = seq; |
d4c91a8f | 2257 | return dentry; |
31e6b01f NP |
2258 | } |
2259 | return NULL; | |
2260 | } | |
2261 | ||
1da177e4 LT |
2262 | /** |
2263 | * d_lookup - search for a dentry | |
2264 | * @parent: parent dentry | |
2265 | * @name: qstr of name we wish to find | |
b04f784e | 2266 | * Returns: dentry, or NULL |
1da177e4 | 2267 | * |
b04f784e NP |
2268 | * d_lookup searches the children of the parent dentry for the name in |
2269 | * question. If the dentry is found its reference count is incremented and the | |
2270 | * dentry is returned. The caller must use dput to free the entry when it has | |
2271 | * finished using it. %NULL is returned if the dentry does not exist. | |
1da177e4 | 2272 | */ |
da2d8455 | 2273 | struct dentry *d_lookup(const struct dentry *parent, const struct qstr *name) |
1da177e4 | 2274 | { |
31e6b01f | 2275 | struct dentry *dentry; |
949854d0 | 2276 | unsigned seq; |
1da177e4 | 2277 | |
b8314f93 DY |
2278 | do { |
2279 | seq = read_seqbegin(&rename_lock); | |
2280 | dentry = __d_lookup(parent, name); | |
2281 | if (dentry) | |
1da177e4 LT |
2282 | break; |
2283 | } while (read_seqretry(&rename_lock, seq)); | |
2284 | return dentry; | |
2285 | } | |
ec4f8605 | 2286 | EXPORT_SYMBOL(d_lookup); |
1da177e4 | 2287 | |
31e6b01f | 2288 | /** |
b04f784e NP |
2289 | * __d_lookup - search for a dentry (racy) |
2290 | * @parent: parent dentry | |
2291 | * @name: qstr of name we wish to find | |
2292 | * Returns: dentry, or NULL | |
2293 | * | |
2294 | * __d_lookup is like d_lookup, however it may (rarely) return a | |
2295 | * false-negative result due to unrelated rename activity. | |
2296 | * | |
2297 | * __d_lookup is slightly faster by avoiding rename_lock read seqlock, | |
2298 | * however it must be used carefully, eg. with a following d_lookup in | |
2299 | * the case of failure. | |
2300 | * | |
2301 | * __d_lookup callers must be commented. | |
2302 | */ | |
a713ca2a | 2303 | struct dentry *__d_lookup(const struct dentry *parent, const struct qstr *name) |
1da177e4 | 2304 | { |
1da177e4 | 2305 | unsigned int hash = name->hash; |
8387ff25 | 2306 | struct hlist_bl_head *b = d_hash(hash); |
ceb5bdc2 | 2307 | struct hlist_bl_node *node; |
31e6b01f | 2308 | struct dentry *found = NULL; |
665a7583 | 2309 | struct dentry *dentry; |
1da177e4 | 2310 | |
31e6b01f NP |
2311 | /* |
2312 | * Note: There is significant duplication with __d_lookup_rcu which is | |
2313 | * required to prevent single threaded performance regressions | |
2314 | * especially on architectures where smp_rmb (in seqcounts) are costly. | |
2315 | * Keep the two functions in sync. | |
2316 | */ | |
2317 | ||
b04f784e NP |
2318 | /* |
2319 | * The hash list is protected using RCU. | |
2320 | * | |
2321 | * Take d_lock when comparing a candidate dentry, to avoid races | |
2322 | * with d_move(). | |
2323 | * | |
2324 | * It is possible that concurrent renames can mess up our list | |
2325 | * walk here and result in missing our dentry, resulting in the | |
2326 | * false-negative result. d_lookup() protects against concurrent | |
2327 | * renames using rename_lock seqlock. | |
2328 | * | |
b0a4bb83 | 2329 | * See Documentation/filesystems/path-lookup.txt for more details. |
b04f784e | 2330 | */ |
1da177e4 LT |
2331 | rcu_read_lock(); |
2332 | ||
b07ad996 | 2333 | hlist_bl_for_each_entry_rcu(dentry, node, b, d_hash) { |
1da177e4 | 2334 | |
1da177e4 LT |
2335 | if (dentry->d_name.hash != hash) |
2336 | continue; | |
1da177e4 LT |
2337 | |
2338 | spin_lock(&dentry->d_lock); | |
1da177e4 LT |
2339 | if (dentry->d_parent != parent) |
2340 | goto next; | |
d0185c08 LT |
2341 | if (d_unhashed(dentry)) |
2342 | goto next; | |
2343 | ||
d4c91a8f AV |
2344 | if (!d_same_name(dentry, parent, name)) |
2345 | goto next; | |
1da177e4 | 2346 | |
98474236 | 2347 | dentry->d_lockref.count++; |
d0185c08 | 2348 | found = dentry; |
1da177e4 LT |
2349 | spin_unlock(&dentry->d_lock); |
2350 | break; | |
2351 | next: | |
2352 | spin_unlock(&dentry->d_lock); | |
2353 | } | |
2354 | rcu_read_unlock(); | |
2355 | ||
2356 | return found; | |
2357 | } | |
2358 | ||
3e7e241f EB |
2359 | /** |
2360 | * d_hash_and_lookup - hash the qstr then search for a dentry | |
2361 | * @dir: Directory to search in | |
2362 | * @name: qstr of name we wish to find | |
2363 | * | |
4f522a24 | 2364 | * On lookup failure NULL is returned; on bad name - ERR_PTR(-error) |
3e7e241f EB |
2365 | */ |
2366 | struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name) | |
2367 | { | |
3e7e241f EB |
2368 | /* |
2369 | * Check for a fs-specific hash function. Note that we must | |
2370 | * calculate the standard hash first, as the d_op->d_hash() | |
2371 | * routine may choose to leave the hash value unchanged. | |
2372 | */ | |
8387ff25 | 2373 | name->hash = full_name_hash(dir, name->name, name->len); |
fb045adb | 2374 | if (dir->d_flags & DCACHE_OP_HASH) { |
da53be12 | 2375 | int err = dir->d_op->d_hash(dir, name); |
4f522a24 AV |
2376 | if (unlikely(err < 0)) |
2377 | return ERR_PTR(err); | |
3e7e241f | 2378 | } |
4f522a24 | 2379 | return d_lookup(dir, name); |
3e7e241f | 2380 | } |
4f522a24 | 2381 | EXPORT_SYMBOL(d_hash_and_lookup); |
3e7e241f | 2382 | |
1da177e4 LT |
2383 | /* |
2384 | * When a file is deleted, we have two options: | |
2385 | * - turn this dentry into a negative dentry | |
2386 | * - unhash this dentry and free it. | |
2387 | * | |
2388 | * Usually, we want to just turn this into | |
2389 | * a negative dentry, but if anybody else is | |
2390 | * currently using the dentry or the inode | |
2391 | * we can't do that and we fall back on removing | |
2392 | * it from the hash queues and waiting for | |
2393 | * it to be deleted later when it has no users | |
2394 | */ | |
2395 | ||
2396 | /** | |
2397 | * d_delete - delete a dentry | |
2398 | * @dentry: The dentry to delete | |
2399 | * | |
2400 | * Turn the dentry into a negative dentry if possible, otherwise | |
2401 | * remove it from the hash queues so it can be deleted later | |
2402 | */ | |
2403 | ||
2404 | void d_delete(struct dentry * dentry) | |
2405 | { | |
c19457f0 AV |
2406 | struct inode *inode = dentry->d_inode; |
2407 | int isdir = d_is_dir(dentry); | |
2408 | ||
2409 | spin_lock(&inode->i_lock); | |
2410 | spin_lock(&dentry->d_lock); | |
1da177e4 LT |
2411 | /* |
2412 | * Are we the only user? | |
2413 | */ | |
98474236 | 2414 | if (dentry->d_lockref.count == 1) { |
13e3c5e5 | 2415 | dentry->d_flags &= ~DCACHE_CANT_MOUNT; |
31e6b01f | 2416 | dentry_unlink_inode(dentry); |
c19457f0 | 2417 | } else { |
1da177e4 | 2418 | __d_drop(dentry); |
c19457f0 AV |
2419 | spin_unlock(&dentry->d_lock); |
2420 | spin_unlock(&inode->i_lock); | |
2421 | } | |
7a91bf7f | 2422 | fsnotify_nameremove(dentry, isdir); |
1da177e4 | 2423 | } |
ec4f8605 | 2424 | EXPORT_SYMBOL(d_delete); |
1da177e4 | 2425 | |
15d3c589 | 2426 | static void __d_rehash(struct dentry *entry) |
1da177e4 | 2427 | { |
15d3c589 | 2428 | struct hlist_bl_head *b = d_hash(entry->d_name.hash); |
61647823 | 2429 | |
1879fd6a | 2430 | hlist_bl_lock(b); |
b07ad996 | 2431 | hlist_bl_add_head_rcu(&entry->d_hash, b); |
1879fd6a | 2432 | hlist_bl_unlock(b); |
1da177e4 LT |
2433 | } |
2434 | ||
2435 | /** | |
2436 | * d_rehash - add an entry back to the hash | |
2437 | * @entry: dentry to add to the hash | |
2438 | * | |
2439 | * Adds a dentry to the hash according to its name. | |
2440 | */ | |
2441 | ||
2442 | void d_rehash(struct dentry * entry) | |
2443 | { | |
1da177e4 | 2444 | spin_lock(&entry->d_lock); |
15d3c589 | 2445 | __d_rehash(entry); |
1da177e4 | 2446 | spin_unlock(&entry->d_lock); |
1da177e4 | 2447 | } |
ec4f8605 | 2448 | EXPORT_SYMBOL(d_rehash); |
1da177e4 | 2449 | |
84e710da AV |
2450 | static inline unsigned start_dir_add(struct inode *dir) |
2451 | { | |
2452 | ||
2453 | for (;;) { | |
2454 | unsigned n = dir->i_dir_seq; | |
2455 | if (!(n & 1) && cmpxchg(&dir->i_dir_seq, n, n + 1) == n) | |
2456 | return n; | |
2457 | cpu_relax(); | |
2458 | } | |
2459 | } | |
2460 | ||
2461 | static inline void end_dir_add(struct inode *dir, unsigned n) | |
2462 | { | |
2463 | smp_store_release(&dir->i_dir_seq, n + 2); | |
2464 | } | |
2465 | ||
d9171b93 AV |
2466 | static void d_wait_lookup(struct dentry *dentry) |
2467 | { | |
2468 | if (d_in_lookup(dentry)) { | |
2469 | DECLARE_WAITQUEUE(wait, current); | |
2470 | add_wait_queue(dentry->d_wait, &wait); | |
2471 | do { | |
2472 | set_current_state(TASK_UNINTERRUPTIBLE); | |
2473 | spin_unlock(&dentry->d_lock); | |
2474 | schedule(); | |
2475 | spin_lock(&dentry->d_lock); | |
2476 | } while (d_in_lookup(dentry)); | |
2477 | } | |
2478 | } | |
2479 | ||
94bdd655 | 2480 | struct dentry *d_alloc_parallel(struct dentry *parent, |
d9171b93 AV |
2481 | const struct qstr *name, |
2482 | wait_queue_head_t *wq) | |
94bdd655 | 2483 | { |
94bdd655 | 2484 | unsigned int hash = name->hash; |
94bdd655 AV |
2485 | struct hlist_bl_head *b = in_lookup_hash(parent, hash); |
2486 | struct hlist_bl_node *node; | |
2487 | struct dentry *new = d_alloc(parent, name); | |
2488 | struct dentry *dentry; | |
2489 | unsigned seq, r_seq, d_seq; | |
2490 | ||
2491 | if (unlikely(!new)) | |
2492 | return ERR_PTR(-ENOMEM); | |
2493 | ||
2494 | retry: | |
2495 | rcu_read_lock(); | |
015555fd | 2496 | seq = smp_load_acquire(&parent->d_inode->i_dir_seq); |
94bdd655 AV |
2497 | r_seq = read_seqbegin(&rename_lock); |
2498 | dentry = __d_lookup_rcu(parent, name, &d_seq); | |
2499 | if (unlikely(dentry)) { | |
2500 | if (!lockref_get_not_dead(&dentry->d_lockref)) { | |
2501 | rcu_read_unlock(); | |
2502 | goto retry; | |
2503 | } | |
2504 | if (read_seqcount_retry(&dentry->d_seq, d_seq)) { | |
2505 | rcu_read_unlock(); | |
2506 | dput(dentry); | |
2507 | goto retry; | |
2508 | } | |
2509 | rcu_read_unlock(); | |
2510 | dput(new); | |
2511 | return dentry; | |
2512 | } | |
2513 | if (unlikely(read_seqretry(&rename_lock, r_seq))) { | |
2514 | rcu_read_unlock(); | |
2515 | goto retry; | |
2516 | } | |
015555fd WD |
2517 | |
2518 | if (unlikely(seq & 1)) { | |
2519 | rcu_read_unlock(); | |
2520 | goto retry; | |
2521 | } | |
2522 | ||
94bdd655 | 2523 | hlist_bl_lock(b); |
8cc07c80 | 2524 | if (unlikely(READ_ONCE(parent->d_inode->i_dir_seq) != seq)) { |
94bdd655 AV |
2525 | hlist_bl_unlock(b); |
2526 | rcu_read_unlock(); | |
2527 | goto retry; | |
2528 | } | |
94bdd655 AV |
2529 | /* |
2530 | * No changes for the parent since the beginning of d_lookup(). | |
2531 | * Since all removals from the chain happen with hlist_bl_lock(), | |
2532 | * any potential in-lookup matches are going to stay here until | |
2533 | * we unlock the chain. All fields are stable in everything | |
2534 | * we encounter. | |
2535 | */ | |
2536 | hlist_bl_for_each_entry(dentry, node, b, d_u.d_in_lookup_hash) { | |
2537 | if (dentry->d_name.hash != hash) | |
2538 | continue; | |
2539 | if (dentry->d_parent != parent) | |
2540 | continue; | |
d4c91a8f AV |
2541 | if (!d_same_name(dentry, parent, name)) |
2542 | continue; | |
94bdd655 | 2543 | hlist_bl_unlock(b); |
e7d6ef97 AV |
2544 | /* now we can try to grab a reference */ |
2545 | if (!lockref_get_not_dead(&dentry->d_lockref)) { | |
2546 | rcu_read_unlock(); | |
2547 | goto retry; | |
2548 | } | |
2549 | ||
2550 | rcu_read_unlock(); | |
2551 | /* | |
2552 | * somebody is likely to be still doing lookup for it; | |
2553 | * wait for them to finish | |
2554 | */ | |
d9171b93 AV |
2555 | spin_lock(&dentry->d_lock); |
2556 | d_wait_lookup(dentry); | |
2557 | /* | |
2558 | * it's not in-lookup anymore; in principle we should repeat | |
2559 | * everything from dcache lookup, but it's likely to be what | |
2560 | * d_lookup() would've found anyway. If it is, just return it; | |
2561 | * otherwise we really have to repeat the whole thing. | |
2562 | */ | |
2563 | if (unlikely(dentry->d_name.hash != hash)) | |
2564 | goto mismatch; | |
2565 | if (unlikely(dentry->d_parent != parent)) | |
2566 | goto mismatch; | |
2567 | if (unlikely(d_unhashed(dentry))) | |
2568 | goto mismatch; | |
d4c91a8f AV |
2569 | if (unlikely(!d_same_name(dentry, parent, name))) |
2570 | goto mismatch; | |
d9171b93 AV |
2571 | /* OK, it *is* a hashed match; return it */ |
2572 | spin_unlock(&dentry->d_lock); | |
94bdd655 AV |
2573 | dput(new); |
2574 | return dentry; | |
2575 | } | |
e7d6ef97 | 2576 | rcu_read_unlock(); |
94bdd655 AV |
2577 | /* we can't take ->d_lock here; it's OK, though. */ |
2578 | new->d_flags |= DCACHE_PAR_LOOKUP; | |
d9171b93 | 2579 | new->d_wait = wq; |
94bdd655 AV |
2580 | hlist_bl_add_head_rcu(&new->d_u.d_in_lookup_hash, b); |
2581 | hlist_bl_unlock(b); | |
2582 | return new; | |
d9171b93 AV |
2583 | mismatch: |
2584 | spin_unlock(&dentry->d_lock); | |
2585 | dput(dentry); | |
2586 | goto retry; | |
94bdd655 AV |
2587 | } |
2588 | EXPORT_SYMBOL(d_alloc_parallel); | |
2589 | ||
85c7f810 AV |
2590 | void __d_lookup_done(struct dentry *dentry) |
2591 | { | |
94bdd655 AV |
2592 | struct hlist_bl_head *b = in_lookup_hash(dentry->d_parent, |
2593 | dentry->d_name.hash); | |
2594 | hlist_bl_lock(b); | |
85c7f810 | 2595 | dentry->d_flags &= ~DCACHE_PAR_LOOKUP; |
94bdd655 | 2596 | __hlist_bl_del(&dentry->d_u.d_in_lookup_hash); |
d9171b93 AV |
2597 | wake_up_all(dentry->d_wait); |
2598 | dentry->d_wait = NULL; | |
94bdd655 AV |
2599 | hlist_bl_unlock(b); |
2600 | INIT_HLIST_NODE(&dentry->d_u.d_alias); | |
d9171b93 | 2601 | INIT_LIST_HEAD(&dentry->d_lru); |
85c7f810 AV |
2602 | } |
2603 | EXPORT_SYMBOL(__d_lookup_done); | |
ed782b5a AV |
2604 | |
2605 | /* inode->i_lock held if inode is non-NULL */ | |
2606 | ||
2607 | static inline void __d_add(struct dentry *dentry, struct inode *inode) | |
2608 | { | |
84e710da AV |
2609 | struct inode *dir = NULL; |
2610 | unsigned n; | |
0568d705 | 2611 | spin_lock(&dentry->d_lock); |
84e710da AV |
2612 | if (unlikely(d_in_lookup(dentry))) { |
2613 | dir = dentry->d_parent->d_inode; | |
2614 | n = start_dir_add(dir); | |
85c7f810 | 2615 | __d_lookup_done(dentry); |
84e710da | 2616 | } |
ed782b5a | 2617 | if (inode) { |
0568d705 AV |
2618 | unsigned add_flags = d_flags_for_inode(inode); |
2619 | hlist_add_head(&dentry->d_u.d_alias, &inode->i_dentry); | |
2620 | raw_write_seqcount_begin(&dentry->d_seq); | |
2621 | __d_set_inode_and_type(dentry, inode, add_flags); | |
2622 | raw_write_seqcount_end(&dentry->d_seq); | |
affda484 | 2623 | fsnotify_update_flags(dentry); |
ed782b5a | 2624 | } |
15d3c589 | 2625 | __d_rehash(dentry); |
84e710da AV |
2626 | if (dir) |
2627 | end_dir_add(dir, n); | |
0568d705 AV |
2628 | spin_unlock(&dentry->d_lock); |
2629 | if (inode) | |
2630 | spin_unlock(&inode->i_lock); | |
ed782b5a AV |
2631 | } |
2632 | ||
34d0d19d AV |
2633 | /** |
2634 | * d_add - add dentry to hash queues | |
2635 | * @entry: dentry to add | |
2636 | * @inode: The inode to attach to this dentry | |
2637 | * | |
2638 | * This adds the entry to the hash queues and initializes @inode. | |
2639 | * The entry was actually filled in earlier during d_alloc(). | |
2640 | */ | |
2641 | ||
2642 | void d_add(struct dentry *entry, struct inode *inode) | |
2643 | { | |
b9680917 AV |
2644 | if (inode) { |
2645 | security_d_instantiate(entry, inode); | |
ed782b5a | 2646 | spin_lock(&inode->i_lock); |
b9680917 | 2647 | } |
ed782b5a | 2648 | __d_add(entry, inode); |
34d0d19d AV |
2649 | } |
2650 | EXPORT_SYMBOL(d_add); | |
2651 | ||
668d0cd5 AV |
2652 | /** |
2653 | * d_exact_alias - find and hash an exact unhashed alias | |
2654 | * @entry: dentry to add | |
2655 | * @inode: The inode to go with this dentry | |
2656 | * | |
2657 | * If an unhashed dentry with the same name/parent and desired | |
2658 | * inode already exists, hash and return it. Otherwise, return | |
2659 | * NULL. | |
2660 | * | |
2661 | * Parent directory should be locked. | |
2662 | */ | |
2663 | struct dentry *d_exact_alias(struct dentry *entry, struct inode *inode) | |
2664 | { | |
2665 | struct dentry *alias; | |
668d0cd5 AV |
2666 | unsigned int hash = entry->d_name.hash; |
2667 | ||
2668 | spin_lock(&inode->i_lock); | |
2669 | hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) { | |
2670 | /* | |
2671 | * Don't need alias->d_lock here, because aliases with | |
2672 | * d_parent == entry->d_parent are not subject to name or | |
2673 | * parent changes, because the parent inode i_mutex is held. | |
2674 | */ | |
2675 | if (alias->d_name.hash != hash) | |
2676 | continue; | |
2677 | if (alias->d_parent != entry->d_parent) | |
2678 | continue; | |
d4c91a8f | 2679 | if (!d_same_name(alias, entry->d_parent, &entry->d_name)) |
668d0cd5 AV |
2680 | continue; |
2681 | spin_lock(&alias->d_lock); | |
2682 | if (!d_unhashed(alias)) { | |
2683 | spin_unlock(&alias->d_lock); | |
2684 | alias = NULL; | |
2685 | } else { | |
2686 | __dget_dlock(alias); | |
15d3c589 | 2687 | __d_rehash(alias); |
668d0cd5 AV |
2688 | spin_unlock(&alias->d_lock); |
2689 | } | |
2690 | spin_unlock(&inode->i_lock); | |
2691 | return alias; | |
2692 | } | |
2693 | spin_unlock(&inode->i_lock); | |
2694 | return NULL; | |
2695 | } | |
2696 | EXPORT_SYMBOL(d_exact_alias); | |
2697 | ||
fb2d5b86 NP |
2698 | /** |
2699 | * dentry_update_name_case - update case insensitive dentry with a new name | |
2700 | * @dentry: dentry to be updated | |
2701 | * @name: new name | |
2702 | * | |
2703 | * Update a case insensitive dentry with new case of name. | |
2704 | * | |
2705 | * dentry must have been returned by d_lookup with name @name. Old and new | |
2706 | * name lengths must match (ie. no d_compare which allows mismatched name | |
2707 | * lengths). | |
2708 | * | |
2709 | * Parent inode i_mutex must be held over d_lookup and into this call (to | |
2710 | * keep renames and concurrent inserts, and readdir(2) away). | |
2711 | */ | |
9aba36de | 2712 | void dentry_update_name_case(struct dentry *dentry, const struct qstr *name) |
fb2d5b86 | 2713 | { |
5955102c | 2714 | BUG_ON(!inode_is_locked(dentry->d_parent->d_inode)); |
fb2d5b86 NP |
2715 | BUG_ON(dentry->d_name.len != name->len); /* d_lookup gives this */ |
2716 | ||
fb2d5b86 | 2717 | spin_lock(&dentry->d_lock); |
31e6b01f | 2718 | write_seqcount_begin(&dentry->d_seq); |
fb2d5b86 | 2719 | memcpy((unsigned char *)dentry->d_name.name, name->name, name->len); |
31e6b01f | 2720 | write_seqcount_end(&dentry->d_seq); |
fb2d5b86 | 2721 | spin_unlock(&dentry->d_lock); |
fb2d5b86 NP |
2722 | } |
2723 | EXPORT_SYMBOL(dentry_update_name_case); | |
2724 | ||
8d85b484 | 2725 | static void swap_names(struct dentry *dentry, struct dentry *target) |
1da177e4 | 2726 | { |
8d85b484 AV |
2727 | if (unlikely(dname_external(target))) { |
2728 | if (unlikely(dname_external(dentry))) { | |
1da177e4 LT |
2729 | /* |
2730 | * Both external: swap the pointers | |
2731 | */ | |
9a8d5bb4 | 2732 | swap(target->d_name.name, dentry->d_name.name); |
1da177e4 LT |
2733 | } else { |
2734 | /* | |
2735 | * dentry:internal, target:external. Steal target's | |
2736 | * storage and make target internal. | |
2737 | */ | |
321bcf92 BF |
2738 | memcpy(target->d_iname, dentry->d_name.name, |
2739 | dentry->d_name.len + 1); | |
1da177e4 LT |
2740 | dentry->d_name.name = target->d_name.name; |
2741 | target->d_name.name = target->d_iname; | |
2742 | } | |
2743 | } else { | |
8d85b484 | 2744 | if (unlikely(dname_external(dentry))) { |
1da177e4 LT |
2745 | /* |
2746 | * dentry:external, target:internal. Give dentry's | |
2747 | * storage to target and make dentry internal | |
2748 | */ | |
2749 | memcpy(dentry->d_iname, target->d_name.name, | |
2750 | target->d_name.len + 1); | |
2751 | target->d_name.name = dentry->d_name.name; | |
2752 | dentry->d_name.name = dentry->d_iname; | |
2753 | } else { | |
2754 | /* | |
da1ce067 | 2755 | * Both are internal. |
1da177e4 | 2756 | */ |
da1ce067 MS |
2757 | unsigned int i; |
2758 | BUILD_BUG_ON(!IS_ALIGNED(DNAME_INLINE_LEN, sizeof(long))); | |
2759 | for (i = 0; i < DNAME_INLINE_LEN / sizeof(long); i++) { | |
2760 | swap(((long *) &dentry->d_iname)[i], | |
2761 | ((long *) &target->d_iname)[i]); | |
2762 | } | |
1da177e4 LT |
2763 | } |
2764 | } | |
a28ddb87 | 2765 | swap(dentry->d_name.hash_len, target->d_name.hash_len); |
1da177e4 LT |
2766 | } |
2767 | ||
8d85b484 AV |
2768 | static void copy_name(struct dentry *dentry, struct dentry *target) |
2769 | { | |
2770 | struct external_name *old_name = NULL; | |
2771 | if (unlikely(dname_external(dentry))) | |
2772 | old_name = external_name(dentry); | |
2773 | if (unlikely(dname_external(target))) { | |
2774 | atomic_inc(&external_name(target)->u.count); | |
2775 | dentry->d_name = target->d_name; | |
2776 | } else { | |
2777 | memcpy(dentry->d_iname, target->d_name.name, | |
2778 | target->d_name.len + 1); | |
2779 | dentry->d_name.name = dentry->d_iname; | |
2780 | dentry->d_name.hash_len = target->d_name.hash_len; | |
2781 | } | |
2782 | if (old_name && likely(atomic_dec_and_test(&old_name->u.count))) | |
2783 | kfree_rcu(old_name, u.head); | |
2784 | } | |
2785 | ||
2fd6b7f5 NP |
2786 | static void dentry_lock_for_move(struct dentry *dentry, struct dentry *target) |
2787 | { | |
2788 | /* | |
2789 | * XXXX: do we really need to take target->d_lock? | |
2790 | */ | |
2791 | if (IS_ROOT(dentry) || dentry->d_parent == target->d_parent) | |
2792 | spin_lock(&target->d_parent->d_lock); | |
2793 | else { | |
2794 | if (d_ancestor(dentry->d_parent, target->d_parent)) { | |
2795 | spin_lock(&dentry->d_parent->d_lock); | |
2796 | spin_lock_nested(&target->d_parent->d_lock, | |
2797 | DENTRY_D_LOCK_NESTED); | |
2798 | } else { | |
2799 | spin_lock(&target->d_parent->d_lock); | |
2800 | spin_lock_nested(&dentry->d_parent->d_lock, | |
2801 | DENTRY_D_LOCK_NESTED); | |
2802 | } | |
2803 | } | |
2804 | if (target < dentry) { | |
2805 | spin_lock_nested(&target->d_lock, 2); | |
2806 | spin_lock_nested(&dentry->d_lock, 3); | |
2807 | } else { | |
2808 | spin_lock_nested(&dentry->d_lock, 2); | |
2809 | spin_lock_nested(&target->d_lock, 3); | |
2810 | } | |
2811 | } | |
2812 | ||
986c0194 | 2813 | static void dentry_unlock_for_move(struct dentry *dentry, struct dentry *target) |
2fd6b7f5 NP |
2814 | { |
2815 | if (target->d_parent != dentry->d_parent) | |
2816 | spin_unlock(&dentry->d_parent->d_lock); | |
2817 | if (target->d_parent != target) | |
2818 | spin_unlock(&target->d_parent->d_lock); | |
986c0194 AV |
2819 | spin_unlock(&target->d_lock); |
2820 | spin_unlock(&dentry->d_lock); | |
2fd6b7f5 NP |
2821 | } |
2822 | ||
1da177e4 | 2823 | /* |
2fd6b7f5 NP |
2824 | * When switching names, the actual string doesn't strictly have to |
2825 | * be preserved in the target - because we're dropping the target | |
2826 | * anyway. As such, we can just do a simple memcpy() to copy over | |
d2fa4a84 ME |
2827 | * the new name before we switch, unless we are going to rehash |
2828 | * it. Note that if we *do* unhash the target, we are not allowed | |
2829 | * to rehash it without giving it a new name/hash key - whether | |
2830 | * we swap or overwrite the names here, resulting name won't match | |
2831 | * the reality in filesystem; it's only there for d_path() purposes. | |
2832 | * Note that all of this is happening under rename_lock, so the | |
2833 | * any hash lookup seeing it in the middle of manipulations will | |
2834 | * be discarded anyway. So we do not care what happens to the hash | |
2835 | * key in that case. | |
1da177e4 | 2836 | */ |
9eaef27b | 2837 | /* |
18367501 | 2838 | * __d_move - move a dentry |
1da177e4 LT |
2839 | * @dentry: entry to move |
2840 | * @target: new dentry | |
da1ce067 | 2841 | * @exchange: exchange the two dentries |
1da177e4 LT |
2842 | * |
2843 | * Update the dcache to reflect the move of a file name. Negative | |
c46c8877 JL |
2844 | * dcache entries should not be moved in this way. Caller must hold |
2845 | * rename_lock, the i_mutex of the source and target directories, | |
2846 | * and the sb->s_vfs_rename_mutex if they differ. See lock_rename(). | |
1da177e4 | 2847 | */ |
da1ce067 MS |
2848 | static void __d_move(struct dentry *dentry, struct dentry *target, |
2849 | bool exchange) | |
1da177e4 | 2850 | { |
84e710da AV |
2851 | struct inode *dir = NULL; |
2852 | unsigned n; | |
1da177e4 LT |
2853 | if (!dentry->d_inode) |
2854 | printk(KERN_WARNING "VFS: moving negative dcache entry\n"); | |
2855 | ||
2fd6b7f5 NP |
2856 | BUG_ON(d_ancestor(dentry, target)); |
2857 | BUG_ON(d_ancestor(target, dentry)); | |
2858 | ||
2fd6b7f5 | 2859 | dentry_lock_for_move(dentry, target); |
84e710da AV |
2860 | if (unlikely(d_in_lookup(target))) { |
2861 | dir = target->d_parent->d_inode; | |
2862 | n = start_dir_add(dir); | |
85c7f810 | 2863 | __d_lookup_done(target); |
84e710da | 2864 | } |
1da177e4 | 2865 | |
31e6b01f | 2866 | write_seqcount_begin(&dentry->d_seq); |
1ca7d67c | 2867 | write_seqcount_begin_nested(&target->d_seq, DENTRY_D_LOCK_NESTED); |
31e6b01f | 2868 | |
15d3c589 | 2869 | /* unhash both */ |
0632a9ac AV |
2870 | if (!d_unhashed(dentry)) |
2871 | ___d_drop(dentry); | |
2872 | if (!d_unhashed(target)) | |
2873 | ___d_drop(target); | |
1da177e4 | 2874 | |
1da177e4 | 2875 | /* Switch the names.. */ |
8d85b484 AV |
2876 | if (exchange) |
2877 | swap_names(dentry, target); | |
2878 | else | |
2879 | copy_name(dentry, target); | |
1da177e4 | 2880 | |
15d3c589 AV |
2881 | /* rehash in new place(s) */ |
2882 | __d_rehash(dentry); | |
2883 | if (exchange) | |
2884 | __d_rehash(target); | |
61647823 N |
2885 | else |
2886 | target->d_hash.pprev = NULL; | |
15d3c589 | 2887 | |
63cf427a | 2888 | /* ... and switch them in the tree */ |
1da177e4 | 2889 | if (IS_ROOT(dentry)) { |
63cf427a | 2890 | /* splicing a tree */ |
3d56c25e | 2891 | dentry->d_flags |= DCACHE_RCUACCESS; |
1da177e4 LT |
2892 | dentry->d_parent = target->d_parent; |
2893 | target->d_parent = target; | |
946e51f2 AV |
2894 | list_del_init(&target->d_child); |
2895 | list_move(&dentry->d_child, &dentry->d_parent->d_subdirs); | |
1da177e4 | 2896 | } else { |
63cf427a | 2897 | /* swapping two dentries */ |
9a8d5bb4 | 2898 | swap(dentry->d_parent, target->d_parent); |
946e51f2 AV |
2899 | list_move(&target->d_child, &target->d_parent->d_subdirs); |
2900 | list_move(&dentry->d_child, &dentry->d_parent->d_subdirs); | |
63cf427a | 2901 | if (exchange) |
affda484 AV |
2902 | fsnotify_update_flags(target); |
2903 | fsnotify_update_flags(dentry); | |
1da177e4 LT |
2904 | } |
2905 | ||
31e6b01f NP |
2906 | write_seqcount_end(&target->d_seq); |
2907 | write_seqcount_end(&dentry->d_seq); | |
2908 | ||
84e710da AV |
2909 | if (dir) |
2910 | end_dir_add(dir, n); | |
986c0194 | 2911 | dentry_unlock_for_move(dentry, target); |
18367501 AV |
2912 | } |
2913 | ||
2914 | /* | |
2915 | * d_move - move a dentry | |
2916 | * @dentry: entry to move | |
2917 | * @target: new dentry | |
2918 | * | |
2919 | * Update the dcache to reflect the move of a file name. Negative | |
c46c8877 JL |
2920 | * dcache entries should not be moved in this way. See the locking |
2921 | * requirements for __d_move. | |
18367501 AV |
2922 | */ |
2923 | void d_move(struct dentry *dentry, struct dentry *target) | |
2924 | { | |
2925 | write_seqlock(&rename_lock); | |
da1ce067 | 2926 | __d_move(dentry, target, false); |
1da177e4 | 2927 | write_sequnlock(&rename_lock); |
9eaef27b | 2928 | } |
ec4f8605 | 2929 | EXPORT_SYMBOL(d_move); |
1da177e4 | 2930 | |
da1ce067 MS |
2931 | /* |
2932 | * d_exchange - exchange two dentries | |
2933 | * @dentry1: first dentry | |
2934 | * @dentry2: second dentry | |
2935 | */ | |
2936 | void d_exchange(struct dentry *dentry1, struct dentry *dentry2) | |
2937 | { | |
2938 | write_seqlock(&rename_lock); | |
2939 | ||
2940 | WARN_ON(!dentry1->d_inode); | |
2941 | WARN_ON(!dentry2->d_inode); | |
2942 | WARN_ON(IS_ROOT(dentry1)); | |
2943 | WARN_ON(IS_ROOT(dentry2)); | |
2944 | ||
2945 | __d_move(dentry1, dentry2, true); | |
2946 | ||
2947 | write_sequnlock(&rename_lock); | |
2948 | } | |
2949 | ||
e2761a11 OH |
2950 | /** |
2951 | * d_ancestor - search for an ancestor | |
2952 | * @p1: ancestor dentry | |
2953 | * @p2: child dentry | |
2954 | * | |
2955 | * Returns the ancestor dentry of p2 which is a child of p1, if p1 is | |
2956 | * an ancestor of p2, else NULL. | |
9eaef27b | 2957 | */ |
e2761a11 | 2958 | struct dentry *d_ancestor(struct dentry *p1, struct dentry *p2) |
9eaef27b TM |
2959 | { |
2960 | struct dentry *p; | |
2961 | ||
871c0067 | 2962 | for (p = p2; !IS_ROOT(p); p = p->d_parent) { |
9eaef27b | 2963 | if (p->d_parent == p1) |
e2761a11 | 2964 | return p; |
9eaef27b | 2965 | } |
e2761a11 | 2966 | return NULL; |
9eaef27b TM |
2967 | } |
2968 | ||
2969 | /* | |
2970 | * This helper attempts to cope with remotely renamed directories | |
2971 | * | |
2972 | * It assumes that the caller is already holding | |
a03e283b | 2973 | * dentry->d_parent->d_inode->i_mutex, and rename_lock |
9eaef27b TM |
2974 | * |
2975 | * Note: If ever the locking in lock_rename() changes, then please | |
2976 | * remember to update this too... | |
9eaef27b | 2977 | */ |
b5ae6b15 | 2978 | static int __d_unalias(struct inode *inode, |
873feea0 | 2979 | struct dentry *dentry, struct dentry *alias) |
9eaef27b | 2980 | { |
9902af79 AV |
2981 | struct mutex *m1 = NULL; |
2982 | struct rw_semaphore *m2 = NULL; | |
3d330dc1 | 2983 | int ret = -ESTALE; |
9eaef27b TM |
2984 | |
2985 | /* If alias and dentry share a parent, then no extra locks required */ | |
2986 | if (alias->d_parent == dentry->d_parent) | |
2987 | goto out_unalias; | |
2988 | ||
9eaef27b | 2989 | /* See lock_rename() */ |
9eaef27b TM |
2990 | if (!mutex_trylock(&dentry->d_sb->s_vfs_rename_mutex)) |
2991 | goto out_err; | |
2992 | m1 = &dentry->d_sb->s_vfs_rename_mutex; | |
9902af79 | 2993 | if (!inode_trylock_shared(alias->d_parent->d_inode)) |
9eaef27b | 2994 | goto out_err; |
9902af79 | 2995 | m2 = &alias->d_parent->d_inode->i_rwsem; |
9eaef27b | 2996 | out_unalias: |
8ed936b5 | 2997 | __d_move(alias, dentry, false); |
b5ae6b15 | 2998 | ret = 0; |
9eaef27b | 2999 | out_err: |
9eaef27b | 3000 | if (m2) |
9902af79 | 3001 | up_read(m2); |
9eaef27b TM |
3002 | if (m1) |
3003 | mutex_unlock(m1); | |
3004 | return ret; | |
3005 | } | |
3006 | ||
3f70bd51 BF |
3007 | /** |
3008 | * d_splice_alias - splice a disconnected dentry into the tree if one exists | |
3009 | * @inode: the inode which may have a disconnected dentry | |
3010 | * @dentry: a negative dentry which we want to point to the inode. | |
3011 | * | |
da093a9b BF |
3012 | * If inode is a directory and has an IS_ROOT alias, then d_move that in |
3013 | * place of the given dentry and return it, else simply d_add the inode | |
3014 | * to the dentry and return NULL. | |
3f70bd51 | 3015 | * |
908790fa BF |
3016 | * If a non-IS_ROOT directory is found, the filesystem is corrupt, and |
3017 | * we should error out: directories can't have multiple aliases. | |
3018 | * | |
3f70bd51 BF |
3019 | * This is needed in the lookup routine of any filesystem that is exportable |
3020 | * (via knfsd) so that we can build dcache paths to directories effectively. | |
3021 | * | |
3022 | * If a dentry was found and moved, then it is returned. Otherwise NULL | |
3023 | * is returned. This matches the expected return value of ->lookup. | |
3024 | * | |
3025 | * Cluster filesystems may call this function with a negative, hashed dentry. | |
3026 | * In that case, we know that the inode will be a regular file, and also this | |
3027 | * will only occur during atomic_open. So we need to check for the dentry | |
3028 | * being already hashed only in the final case. | |
3029 | */ | |
3030 | struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry) | |
3031 | { | |
3f70bd51 BF |
3032 | if (IS_ERR(inode)) |
3033 | return ERR_CAST(inode); | |
3034 | ||
770bfad8 DH |
3035 | BUG_ON(!d_unhashed(dentry)); |
3036 | ||
de689f5e | 3037 | if (!inode) |
b5ae6b15 | 3038 | goto out; |
de689f5e | 3039 | |
b9680917 | 3040 | security_d_instantiate(dentry, inode); |
873feea0 | 3041 | spin_lock(&inode->i_lock); |
9eaef27b | 3042 | if (S_ISDIR(inode->i_mode)) { |
b5ae6b15 AV |
3043 | struct dentry *new = __d_find_any_alias(inode); |
3044 | if (unlikely(new)) { | |
a03e283b EB |
3045 | /* The reference to new ensures it remains an alias */ |
3046 | spin_unlock(&inode->i_lock); | |
18367501 | 3047 | write_seqlock(&rename_lock); |
b5ae6b15 AV |
3048 | if (unlikely(d_ancestor(new, dentry))) { |
3049 | write_sequnlock(&rename_lock); | |
b5ae6b15 AV |
3050 | dput(new); |
3051 | new = ERR_PTR(-ELOOP); | |
3052 | pr_warn_ratelimited( | |
3053 | "VFS: Lookup of '%s' in %s %s" | |
3054 | " would have caused loop\n", | |
3055 | dentry->d_name.name, | |
3056 | inode->i_sb->s_type->name, | |
3057 | inode->i_sb->s_id); | |
3058 | } else if (!IS_ROOT(new)) { | |
3059 | int err = __d_unalias(inode, dentry, new); | |
18367501 | 3060 | write_sequnlock(&rename_lock); |
b5ae6b15 AV |
3061 | if (err) { |
3062 | dput(new); | |
3063 | new = ERR_PTR(err); | |
3064 | } | |
18367501 | 3065 | } else { |
b5ae6b15 AV |
3066 | __d_move(new, dentry, false); |
3067 | write_sequnlock(&rename_lock); | |
dd179946 | 3068 | } |
b5ae6b15 AV |
3069 | iput(inode); |
3070 | return new; | |
9eaef27b | 3071 | } |
770bfad8 | 3072 | } |
b5ae6b15 | 3073 | out: |
ed782b5a | 3074 | __d_add(dentry, inode); |
b5ae6b15 | 3075 | return NULL; |
770bfad8 | 3076 | } |
b5ae6b15 | 3077 | EXPORT_SYMBOL(d_splice_alias); |
770bfad8 | 3078 | |
cdd16d02 | 3079 | static int prepend(char **buffer, int *buflen, const char *str, int namelen) |
6092d048 RP |
3080 | { |
3081 | *buflen -= namelen; | |
3082 | if (*buflen < 0) | |
3083 | return -ENAMETOOLONG; | |
3084 | *buffer -= namelen; | |
3085 | memcpy(*buffer, str, namelen); | |
3086 | return 0; | |
3087 | } | |
3088 | ||
232d2d60 WL |
3089 | /** |
3090 | * prepend_name - prepend a pathname in front of current buffer pointer | |
18129977 WL |
3091 | * @buffer: buffer pointer |
3092 | * @buflen: allocated length of the buffer | |
3093 | * @name: name string and length qstr structure | |
232d2d60 | 3094 | * |
66702eb5 | 3095 | * With RCU path tracing, it may race with d_move(). Use READ_ONCE() to |
232d2d60 WL |
3096 | * make sure that either the old or the new name pointer and length are |
3097 | * fetched. However, there may be mismatch between length and pointer. | |
3098 | * The length cannot be trusted, we need to copy it byte-by-byte until | |
3099 | * the length is reached or a null byte is found. It also prepends "/" at | |
3100 | * the beginning of the name. The sequence number check at the caller will | |
3101 | * retry it again when a d_move() does happen. So any garbage in the buffer | |
3102 | * due to mismatched pointer and length will be discarded. | |
6d13f694 | 3103 | * |
7088efa9 | 3104 | * Load acquire is needed to make sure that we see that terminating NUL. |
232d2d60 | 3105 | */ |
9aba36de | 3106 | static int prepend_name(char **buffer, int *buflen, const struct qstr *name) |
cdd16d02 | 3107 | { |
7088efa9 | 3108 | const char *dname = smp_load_acquire(&name->name); /* ^^^ */ |
66702eb5 | 3109 | u32 dlen = READ_ONCE(name->len); |
232d2d60 WL |
3110 | char *p; |
3111 | ||
232d2d60 | 3112 | *buflen -= dlen + 1; |
e825196d AV |
3113 | if (*buflen < 0) |
3114 | return -ENAMETOOLONG; | |
232d2d60 WL |
3115 | p = *buffer -= dlen + 1; |
3116 | *p++ = '/'; | |
3117 | while (dlen--) { | |
3118 | char c = *dname++; | |
3119 | if (!c) | |
3120 | break; | |
3121 | *p++ = c; | |
3122 | } | |
3123 | return 0; | |
cdd16d02 MS |
3124 | } |
3125 | ||
1da177e4 | 3126 | /** |
208898c1 | 3127 | * prepend_path - Prepend path string to a buffer |
9d1bc601 | 3128 | * @path: the dentry/vfsmount to report |
02125a82 | 3129 | * @root: root vfsmnt/dentry |
f2eb6575 MS |
3130 | * @buffer: pointer to the end of the buffer |
3131 | * @buflen: pointer to buffer length | |
552ce544 | 3132 | * |
18129977 WL |
3133 | * The function will first try to write out the pathname without taking any |
3134 | * lock other than the RCU read lock to make sure that dentries won't go away. | |
3135 | * It only checks the sequence number of the global rename_lock as any change | |
3136 | * in the dentry's d_seq will be preceded by changes in the rename_lock | |
3137 | * sequence number. If the sequence number had been changed, it will restart | |
3138 | * the whole pathname back-tracing sequence again by taking the rename_lock. | |
3139 | * In this case, there is no need to take the RCU read lock as the recursive | |
3140 | * parent pointer references will keep the dentry chain alive as long as no | |
3141 | * rename operation is performed. | |
1da177e4 | 3142 | */ |
02125a82 AV |
3143 | static int prepend_path(const struct path *path, |
3144 | const struct path *root, | |
f2eb6575 | 3145 | char **buffer, int *buflen) |
1da177e4 | 3146 | { |
ede4cebc AV |
3147 | struct dentry *dentry; |
3148 | struct vfsmount *vfsmnt; | |
3149 | struct mount *mnt; | |
f2eb6575 | 3150 | int error = 0; |
48a066e7 | 3151 | unsigned seq, m_seq = 0; |
232d2d60 WL |
3152 | char *bptr; |
3153 | int blen; | |
6092d048 | 3154 | |
48f5ec21 | 3155 | rcu_read_lock(); |
48a066e7 AV |
3156 | restart_mnt: |
3157 | read_seqbegin_or_lock(&mount_lock, &m_seq); | |
3158 | seq = 0; | |
4ec6c2ae | 3159 | rcu_read_lock(); |
232d2d60 WL |
3160 | restart: |
3161 | bptr = *buffer; | |
3162 | blen = *buflen; | |
48a066e7 | 3163 | error = 0; |
ede4cebc AV |
3164 | dentry = path->dentry; |
3165 | vfsmnt = path->mnt; | |
3166 | mnt = real_mount(vfsmnt); | |
232d2d60 | 3167 | read_seqbegin_or_lock(&rename_lock, &seq); |
f2eb6575 | 3168 | while (dentry != root->dentry || vfsmnt != root->mnt) { |
1da177e4 LT |
3169 | struct dentry * parent; |
3170 | ||
1da177e4 | 3171 | if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) { |
66702eb5 | 3172 | struct mount *parent = READ_ONCE(mnt->mnt_parent); |
cde93be4 EB |
3173 | /* Escaped? */ |
3174 | if (dentry != vfsmnt->mnt_root) { | |
3175 | bptr = *buffer; | |
3176 | blen = *buflen; | |
3177 | error = 3; | |
3178 | break; | |
3179 | } | |
552ce544 | 3180 | /* Global root? */ |
48a066e7 | 3181 | if (mnt != parent) { |
66702eb5 | 3182 | dentry = READ_ONCE(mnt->mnt_mountpoint); |
48a066e7 | 3183 | mnt = parent; |
232d2d60 WL |
3184 | vfsmnt = &mnt->mnt; |
3185 | continue; | |
3186 | } | |
232d2d60 WL |
3187 | if (!error) |
3188 | error = is_mounted(vfsmnt) ? 1 : 2; | |
3189 | break; | |
1da177e4 LT |
3190 | } |
3191 | parent = dentry->d_parent; | |
3192 | prefetch(parent); | |
232d2d60 | 3193 | error = prepend_name(&bptr, &blen, &dentry->d_name); |
f2eb6575 MS |
3194 | if (error) |
3195 | break; | |
3196 | ||
1da177e4 LT |
3197 | dentry = parent; |
3198 | } | |
48f5ec21 AV |
3199 | if (!(seq & 1)) |
3200 | rcu_read_unlock(); | |
3201 | if (need_seqretry(&rename_lock, seq)) { | |
3202 | seq = 1; | |
232d2d60 | 3203 | goto restart; |
48f5ec21 AV |
3204 | } |
3205 | done_seqretry(&rename_lock, seq); | |
4ec6c2ae LZ |
3206 | |
3207 | if (!(m_seq & 1)) | |
3208 | rcu_read_unlock(); | |
48a066e7 AV |
3209 | if (need_seqretry(&mount_lock, m_seq)) { |
3210 | m_seq = 1; | |
3211 | goto restart_mnt; | |
3212 | } | |
3213 | done_seqretry(&mount_lock, m_seq); | |
1da177e4 | 3214 | |
232d2d60 WL |
3215 | if (error >= 0 && bptr == *buffer) { |
3216 | if (--blen < 0) | |
3217 | error = -ENAMETOOLONG; | |
3218 | else | |
3219 | *--bptr = '/'; | |
3220 | } | |
3221 | *buffer = bptr; | |
3222 | *buflen = blen; | |
7ea600b5 | 3223 | return error; |
f2eb6575 | 3224 | } |
be285c71 | 3225 | |
f2eb6575 MS |
3226 | /** |
3227 | * __d_path - return the path of a dentry | |
3228 | * @path: the dentry/vfsmount to report | |
02125a82 | 3229 | * @root: root vfsmnt/dentry |
cd956a1c | 3230 | * @buf: buffer to return value in |
f2eb6575 MS |
3231 | * @buflen: buffer length |
3232 | * | |
ffd1f4ed | 3233 | * Convert a dentry into an ASCII path name. |
f2eb6575 MS |
3234 | * |
3235 | * Returns a pointer into the buffer or an error code if the | |
3236 | * path was too long. | |
3237 | * | |
be148247 | 3238 | * "buflen" should be positive. |
f2eb6575 | 3239 | * |
02125a82 | 3240 | * If the path is not reachable from the supplied root, return %NULL. |
f2eb6575 | 3241 | */ |
02125a82 AV |
3242 | char *__d_path(const struct path *path, |
3243 | const struct path *root, | |
f2eb6575 MS |
3244 | char *buf, int buflen) |
3245 | { | |
3246 | char *res = buf + buflen; | |
3247 | int error; | |
3248 | ||
3249 | prepend(&res, &buflen, "\0", 1); | |
f2eb6575 | 3250 | error = prepend_path(path, root, &res, &buflen); |
be148247 | 3251 | |
02125a82 AV |
3252 | if (error < 0) |
3253 | return ERR_PTR(error); | |
3254 | if (error > 0) | |
3255 | return NULL; | |
3256 | return res; | |
3257 | } | |
3258 | ||
3259 | char *d_absolute_path(const struct path *path, | |
3260 | char *buf, int buflen) | |
3261 | { | |
3262 | struct path root = {}; | |
3263 | char *res = buf + buflen; | |
3264 | int error; | |
3265 | ||
3266 | prepend(&res, &buflen, "\0", 1); | |
02125a82 | 3267 | error = prepend_path(path, &root, &res, &buflen); |
02125a82 AV |
3268 | |
3269 | if (error > 1) | |
3270 | error = -EINVAL; | |
3271 | if (error < 0) | |
f2eb6575 | 3272 | return ERR_PTR(error); |
f2eb6575 | 3273 | return res; |
1da177e4 LT |
3274 | } |
3275 | ||
ffd1f4ed MS |
3276 | /* |
3277 | * same as __d_path but appends "(deleted)" for unlinked files. | |
3278 | */ | |
02125a82 AV |
3279 | static int path_with_deleted(const struct path *path, |
3280 | const struct path *root, | |
3281 | char **buf, int *buflen) | |
ffd1f4ed MS |
3282 | { |
3283 | prepend(buf, buflen, "\0", 1); | |
3284 | if (d_unlinked(path->dentry)) { | |
3285 | int error = prepend(buf, buflen, " (deleted)", 10); | |
3286 | if (error) | |
3287 | return error; | |
3288 | } | |
3289 | ||
3290 | return prepend_path(path, root, buf, buflen); | |
3291 | } | |
3292 | ||
8df9d1a4 MS |
3293 | static int prepend_unreachable(char **buffer, int *buflen) |
3294 | { | |
3295 | return prepend(buffer, buflen, "(unreachable)", 13); | |
3296 | } | |
3297 | ||
68f0d9d9 LT |
3298 | static void get_fs_root_rcu(struct fs_struct *fs, struct path *root) |
3299 | { | |
3300 | unsigned seq; | |
3301 | ||
3302 | do { | |
3303 | seq = read_seqcount_begin(&fs->seq); | |
3304 | *root = fs->root; | |
3305 | } while (read_seqcount_retry(&fs->seq, seq)); | |
3306 | } | |
3307 | ||
a03a8a70 JB |
3308 | /** |
3309 | * d_path - return the path of a dentry | |
cf28b486 | 3310 | * @path: path to report |
a03a8a70 JB |
3311 | * @buf: buffer to return value in |
3312 | * @buflen: buffer length | |
3313 | * | |
3314 | * Convert a dentry into an ASCII path name. If the entry has been deleted | |
3315 | * the string " (deleted)" is appended. Note that this is ambiguous. | |
3316 | * | |
52afeefb AV |
3317 | * Returns a pointer into the buffer or an error code if the path was |
3318 | * too long. Note: Callers should use the returned pointer, not the passed | |
3319 | * in buffer, to use the name! The implementation often starts at an offset | |
3320 | * into the buffer, and may leave 0 bytes at the start. | |
a03a8a70 | 3321 | * |
31f3e0b3 | 3322 | * "buflen" should be positive. |
a03a8a70 | 3323 | */ |
20d4fdc1 | 3324 | char *d_path(const struct path *path, char *buf, int buflen) |
1da177e4 | 3325 | { |
ffd1f4ed | 3326 | char *res = buf + buflen; |
6ac08c39 | 3327 | struct path root; |
ffd1f4ed | 3328 | int error; |
1da177e4 | 3329 | |
c23fbb6b ED |
3330 | /* |
3331 | * We have various synthetic filesystems that never get mounted. On | |
3332 | * these filesystems dentries are never used for lookup purposes, and | |
3333 | * thus don't need to be hashed. They also don't need a name until a | |
3334 | * user wants to identify the object in /proc/pid/fd/. The little hack | |
3335 | * below allows us to generate a name for these objects on demand: | |
f48cfddc EB |
3336 | * |
3337 | * Some pseudo inodes are mountable. When they are mounted | |
3338 | * path->dentry == path->mnt->mnt_root. In that case don't call d_dname | |
3339 | * and instead have d_path return the mounted path. | |
c23fbb6b | 3340 | */ |
f48cfddc EB |
3341 | if (path->dentry->d_op && path->dentry->d_op->d_dname && |
3342 | (!IS_ROOT(path->dentry) || path->dentry != path->mnt->mnt_root)) | |
cf28b486 | 3343 | return path->dentry->d_op->d_dname(path->dentry, buf, buflen); |
c23fbb6b | 3344 | |
68f0d9d9 LT |
3345 | rcu_read_lock(); |
3346 | get_fs_root_rcu(current->fs, &root); | |
02125a82 | 3347 | error = path_with_deleted(path, &root, &res, &buflen); |
68f0d9d9 LT |
3348 | rcu_read_unlock(); |
3349 | ||
02125a82 | 3350 | if (error < 0) |
ffd1f4ed | 3351 | res = ERR_PTR(error); |
1da177e4 LT |
3352 | return res; |
3353 | } | |
ec4f8605 | 3354 | EXPORT_SYMBOL(d_path); |
1da177e4 | 3355 | |
c23fbb6b ED |
3356 | /* |
3357 | * Helper function for dentry_operations.d_dname() members | |
3358 | */ | |
3359 | char *dynamic_dname(struct dentry *dentry, char *buffer, int buflen, | |
3360 | const char *fmt, ...) | |
3361 | { | |
3362 | va_list args; | |
3363 | char temp[64]; | |
3364 | int sz; | |
3365 | ||
3366 | va_start(args, fmt); | |
3367 | sz = vsnprintf(temp, sizeof(temp), fmt, args) + 1; | |
3368 | va_end(args); | |
3369 | ||
3370 | if (sz > sizeof(temp) || sz > buflen) | |
3371 | return ERR_PTR(-ENAMETOOLONG); | |
3372 | ||
3373 | buffer += buflen - sz; | |
3374 | return memcpy(buffer, temp, sz); | |
3375 | } | |
3376 | ||
118b2302 AV |
3377 | char *simple_dname(struct dentry *dentry, char *buffer, int buflen) |
3378 | { | |
3379 | char *end = buffer + buflen; | |
3380 | /* these dentries are never renamed, so d_lock is not needed */ | |
3381 | if (prepend(&end, &buflen, " (deleted)", 11) || | |
232d2d60 | 3382 | prepend(&end, &buflen, dentry->d_name.name, dentry->d_name.len) || |
118b2302 AV |
3383 | prepend(&end, &buflen, "/", 1)) |
3384 | end = ERR_PTR(-ENAMETOOLONG); | |
232d2d60 | 3385 | return end; |
118b2302 | 3386 | } |
31bbe16f | 3387 | EXPORT_SYMBOL(simple_dname); |
118b2302 | 3388 | |
6092d048 RP |
3389 | /* |
3390 | * Write full pathname from the root of the filesystem into the buffer. | |
3391 | */ | |
f6500801 | 3392 | static char *__dentry_path(struct dentry *d, char *buf, int buflen) |
6092d048 | 3393 | { |
f6500801 | 3394 | struct dentry *dentry; |
232d2d60 WL |
3395 | char *end, *retval; |
3396 | int len, seq = 0; | |
3397 | int error = 0; | |
6092d048 | 3398 | |
f6500801 AV |
3399 | if (buflen < 2) |
3400 | goto Elong; | |
3401 | ||
48f5ec21 | 3402 | rcu_read_lock(); |
232d2d60 | 3403 | restart: |
f6500801 | 3404 | dentry = d; |
232d2d60 WL |
3405 | end = buf + buflen; |
3406 | len = buflen; | |
3407 | prepend(&end, &len, "\0", 1); | |
6092d048 RP |
3408 | /* Get '/' right */ |
3409 | retval = end-1; | |
3410 | *retval = '/'; | |
232d2d60 | 3411 | read_seqbegin_or_lock(&rename_lock, &seq); |
cdd16d02 MS |
3412 | while (!IS_ROOT(dentry)) { |
3413 | struct dentry *parent = dentry->d_parent; | |
6092d048 | 3414 | |
6092d048 | 3415 | prefetch(parent); |
232d2d60 WL |
3416 | error = prepend_name(&end, &len, &dentry->d_name); |
3417 | if (error) | |
3418 | break; | |
6092d048 RP |
3419 | |
3420 | retval = end; | |
3421 | dentry = parent; | |
3422 | } | |
48f5ec21 AV |
3423 | if (!(seq & 1)) |
3424 | rcu_read_unlock(); | |
3425 | if (need_seqretry(&rename_lock, seq)) { | |
3426 | seq = 1; | |
232d2d60 | 3427 | goto restart; |
48f5ec21 AV |
3428 | } |
3429 | done_seqretry(&rename_lock, seq); | |
232d2d60 WL |
3430 | if (error) |
3431 | goto Elong; | |
c103135c AV |
3432 | return retval; |
3433 | Elong: | |
3434 | return ERR_PTR(-ENAMETOOLONG); | |
3435 | } | |
ec2447c2 NP |
3436 | |
3437 | char *dentry_path_raw(struct dentry *dentry, char *buf, int buflen) | |
3438 | { | |
232d2d60 | 3439 | return __dentry_path(dentry, buf, buflen); |
ec2447c2 NP |
3440 | } |
3441 | EXPORT_SYMBOL(dentry_path_raw); | |
c103135c AV |
3442 | |
3443 | char *dentry_path(struct dentry *dentry, char *buf, int buflen) | |
3444 | { | |
3445 | char *p = NULL; | |
3446 | char *retval; | |
3447 | ||
c103135c AV |
3448 | if (d_unlinked(dentry)) { |
3449 | p = buf + buflen; | |
3450 | if (prepend(&p, &buflen, "//deleted", 10) != 0) | |
3451 | goto Elong; | |
3452 | buflen++; | |
3453 | } | |
3454 | retval = __dentry_path(dentry, buf, buflen); | |
c103135c AV |
3455 | if (!IS_ERR(retval) && p) |
3456 | *p = '/'; /* restore '/' overriden with '\0' */ | |
6092d048 RP |
3457 | return retval; |
3458 | Elong: | |
6092d048 RP |
3459 | return ERR_PTR(-ENAMETOOLONG); |
3460 | } | |
3461 | ||
8b19e341 LT |
3462 | static void get_fs_root_and_pwd_rcu(struct fs_struct *fs, struct path *root, |
3463 | struct path *pwd) | |
5762482f | 3464 | { |
8b19e341 LT |
3465 | unsigned seq; |
3466 | ||
3467 | do { | |
3468 | seq = read_seqcount_begin(&fs->seq); | |
3469 | *root = fs->root; | |
3470 | *pwd = fs->pwd; | |
3471 | } while (read_seqcount_retry(&fs->seq, seq)); | |
5762482f LT |
3472 | } |
3473 | ||
1da177e4 LT |
3474 | /* |
3475 | * NOTE! The user-level library version returns a | |
3476 | * character pointer. The kernel system call just | |
3477 | * returns the length of the buffer filled (which | |
3478 | * includes the ending '\0' character), or a negative | |
3479 | * error value. So libc would do something like | |
3480 | * | |
3481 | * char *getcwd(char * buf, size_t size) | |
3482 | * { | |
3483 | * int retval; | |
3484 | * | |
3485 | * retval = sys_getcwd(buf, size); | |
3486 | * if (retval >= 0) | |
3487 | * return buf; | |
3488 | * errno = -retval; | |
3489 | * return NULL; | |
3490 | * } | |
3491 | */ | |
3cdad428 | 3492 | SYSCALL_DEFINE2(getcwd, char __user *, buf, unsigned long, size) |
1da177e4 | 3493 | { |
552ce544 | 3494 | int error; |
6ac08c39 | 3495 | struct path pwd, root; |
3272c544 | 3496 | char *page = __getname(); |
1da177e4 LT |
3497 | |
3498 | if (!page) | |
3499 | return -ENOMEM; | |
3500 | ||
8b19e341 LT |
3501 | rcu_read_lock(); |
3502 | get_fs_root_and_pwd_rcu(current->fs, &root, &pwd); | |
1da177e4 | 3503 | |
552ce544 | 3504 | error = -ENOENT; |
f3da392e | 3505 | if (!d_unlinked(pwd.dentry)) { |
552ce544 | 3506 | unsigned long len; |
3272c544 LT |
3507 | char *cwd = page + PATH_MAX; |
3508 | int buflen = PATH_MAX; | |
1da177e4 | 3509 | |
8df9d1a4 | 3510 | prepend(&cwd, &buflen, "\0", 1); |
02125a82 | 3511 | error = prepend_path(&pwd, &root, &cwd, &buflen); |
ff812d72 | 3512 | rcu_read_unlock(); |
552ce544 | 3513 | |
02125a82 | 3514 | if (error < 0) |
552ce544 LT |
3515 | goto out; |
3516 | ||
8df9d1a4 | 3517 | /* Unreachable from current root */ |
02125a82 | 3518 | if (error > 0) { |
8df9d1a4 MS |
3519 | error = prepend_unreachable(&cwd, &buflen); |
3520 | if (error) | |
3521 | goto out; | |
3522 | } | |
3523 | ||
552ce544 | 3524 | error = -ERANGE; |
3272c544 | 3525 | len = PATH_MAX + page - cwd; |
552ce544 LT |
3526 | if (len <= size) { |
3527 | error = len; | |
3528 | if (copy_to_user(buf, cwd, len)) | |
3529 | error = -EFAULT; | |
3530 | } | |
949854d0 | 3531 | } else { |
ff812d72 | 3532 | rcu_read_unlock(); |
949854d0 | 3533 | } |
1da177e4 LT |
3534 | |
3535 | out: | |
3272c544 | 3536 | __putname(page); |
1da177e4 LT |
3537 | return error; |
3538 | } | |
3539 | ||
3540 | /* | |
3541 | * Test whether new_dentry is a subdirectory of old_dentry. | |
3542 | * | |
3543 | * Trivially implemented using the dcache structure | |
3544 | */ | |
3545 | ||
3546 | /** | |
3547 | * is_subdir - is new dentry a subdirectory of old_dentry | |
3548 | * @new_dentry: new dentry | |
3549 | * @old_dentry: old dentry | |
3550 | * | |
a6e5787f YB |
3551 | * Returns true if new_dentry is a subdirectory of the parent (at any depth). |
3552 | * Returns false otherwise. | |
1da177e4 LT |
3553 | * Caller must ensure that "new_dentry" is pinned before calling is_subdir() |
3554 | */ | |
3555 | ||
a6e5787f | 3556 | bool is_subdir(struct dentry *new_dentry, struct dentry *old_dentry) |
1da177e4 | 3557 | { |
a6e5787f | 3558 | bool result; |
949854d0 | 3559 | unsigned seq; |
1da177e4 | 3560 | |
e2761a11 | 3561 | if (new_dentry == old_dentry) |
a6e5787f | 3562 | return true; |
e2761a11 | 3563 | |
e2761a11 | 3564 | do { |
1da177e4 | 3565 | /* for restarting inner loop in case of seq retry */ |
1da177e4 | 3566 | seq = read_seqbegin(&rename_lock); |
949854d0 NP |
3567 | /* |
3568 | * Need rcu_readlock to protect against the d_parent trashing | |
3569 | * due to d_move | |
3570 | */ | |
3571 | rcu_read_lock(); | |
e2761a11 | 3572 | if (d_ancestor(old_dentry, new_dentry)) |
a6e5787f | 3573 | result = true; |
e2761a11 | 3574 | else |
a6e5787f | 3575 | result = false; |
949854d0 | 3576 | rcu_read_unlock(); |
1da177e4 | 3577 | } while (read_seqretry(&rename_lock, seq)); |
1da177e4 LT |
3578 | |
3579 | return result; | |
3580 | } | |
e8f9e5b7 | 3581 | EXPORT_SYMBOL(is_subdir); |
1da177e4 | 3582 | |
db14fc3a | 3583 | static enum d_walk_ret d_genocide_kill(void *data, struct dentry *dentry) |
1da177e4 | 3584 | { |
db14fc3a MS |
3585 | struct dentry *root = data; |
3586 | if (dentry != root) { | |
3587 | if (d_unhashed(dentry) || !dentry->d_inode) | |
3588 | return D_WALK_SKIP; | |
1da177e4 | 3589 | |
01ddc4ed MS |
3590 | if (!(dentry->d_flags & DCACHE_GENOCIDE)) { |
3591 | dentry->d_flags |= DCACHE_GENOCIDE; | |
3592 | dentry->d_lockref.count--; | |
3593 | } | |
1da177e4 | 3594 | } |
db14fc3a MS |
3595 | return D_WALK_CONTINUE; |
3596 | } | |
58db63d0 | 3597 | |
db14fc3a MS |
3598 | void d_genocide(struct dentry *parent) |
3599 | { | |
3600 | d_walk(parent, parent, d_genocide_kill, NULL); | |
1da177e4 LT |
3601 | } |
3602 | ||
60545d0d | 3603 | void d_tmpfile(struct dentry *dentry, struct inode *inode) |
1da177e4 | 3604 | { |
60545d0d AV |
3605 | inode_dec_link_count(inode); |
3606 | BUG_ON(dentry->d_name.name != dentry->d_iname || | |
946e51f2 | 3607 | !hlist_unhashed(&dentry->d_u.d_alias) || |
60545d0d AV |
3608 | !d_unlinked(dentry)); |
3609 | spin_lock(&dentry->d_parent->d_lock); | |
3610 | spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); | |
3611 | dentry->d_name.len = sprintf(dentry->d_iname, "#%llu", | |
3612 | (unsigned long long)inode->i_ino); | |
3613 | spin_unlock(&dentry->d_lock); | |
3614 | spin_unlock(&dentry->d_parent->d_lock); | |
3615 | d_instantiate(dentry, inode); | |
1da177e4 | 3616 | } |
60545d0d | 3617 | EXPORT_SYMBOL(d_tmpfile); |
1da177e4 LT |
3618 | |
3619 | static __initdata unsigned long dhash_entries; | |
3620 | static int __init set_dhash_entries(char *str) | |
3621 | { | |
3622 | if (!str) | |
3623 | return 0; | |
3624 | dhash_entries = simple_strtoul(str, &str, 0); | |
3625 | return 1; | |
3626 | } | |
3627 | __setup("dhash_entries=", set_dhash_entries); | |
3628 | ||
3629 | static void __init dcache_init_early(void) | |
3630 | { | |
1da177e4 LT |
3631 | /* If hashes are distributed across NUMA nodes, defer |
3632 | * hash allocation until vmalloc space is available. | |
3633 | */ | |
3634 | if (hashdist) | |
3635 | return; | |
3636 | ||
3637 | dentry_hashtable = | |
3638 | alloc_large_system_hash("Dentry cache", | |
b07ad996 | 3639 | sizeof(struct hlist_bl_head), |
1da177e4 LT |
3640 | dhash_entries, |
3641 | 13, | |
3d375d78 | 3642 | HASH_EARLY | HASH_ZERO, |
1da177e4 | 3643 | &d_hash_shift, |
b35d786b | 3644 | NULL, |
31fe62b9 | 3645 | 0, |
1da177e4 | 3646 | 0); |
854d3e63 | 3647 | d_hash_shift = 32 - d_hash_shift; |
1da177e4 LT |
3648 | } |
3649 | ||
74bf17cf | 3650 | static void __init dcache_init(void) |
1da177e4 | 3651 | { |
3d375d78 | 3652 | /* |
1da177e4 LT |
3653 | * A constructor could be added for stable state like the lists, |
3654 | * but it is probably not worth it because of the cache nature | |
3d375d78 | 3655 | * of the dcache. |
1da177e4 | 3656 | */ |
80344266 DW |
3657 | dentry_cache = KMEM_CACHE_USERCOPY(dentry, |
3658 | SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|SLAB_MEM_SPREAD|SLAB_ACCOUNT, | |
3659 | d_iname); | |
1da177e4 LT |
3660 | |
3661 | /* Hash may have been set up in dcache_init_early */ | |
3662 | if (!hashdist) | |
3663 | return; | |
3664 | ||
3665 | dentry_hashtable = | |
3666 | alloc_large_system_hash("Dentry cache", | |
b07ad996 | 3667 | sizeof(struct hlist_bl_head), |
1da177e4 LT |
3668 | dhash_entries, |
3669 | 13, | |
3d375d78 | 3670 | HASH_ZERO, |
1da177e4 | 3671 | &d_hash_shift, |
b35d786b | 3672 | NULL, |
31fe62b9 | 3673 | 0, |
1da177e4 | 3674 | 0); |
854d3e63 | 3675 | d_hash_shift = 32 - d_hash_shift; |
1da177e4 LT |
3676 | } |
3677 | ||
3678 | /* SLAB cache for __getname() consumers */ | |
e18b890b | 3679 | struct kmem_cache *names_cachep __read_mostly; |
ec4f8605 | 3680 | EXPORT_SYMBOL(names_cachep); |
1da177e4 | 3681 | |
1da177e4 LT |
3682 | EXPORT_SYMBOL(d_genocide); |
3683 | ||
1da177e4 LT |
3684 | void __init vfs_caches_init_early(void) |
3685 | { | |
6916363f SAS |
3686 | int i; |
3687 | ||
3688 | for (i = 0; i < ARRAY_SIZE(in_lookup_hashtable); i++) | |
3689 | INIT_HLIST_BL_HEAD(&in_lookup_hashtable[i]); | |
3690 | ||
1da177e4 LT |
3691 | dcache_init_early(); |
3692 | inode_init_early(); | |
3693 | } | |
3694 | ||
4248b0da | 3695 | void __init vfs_caches_init(void) |
1da177e4 | 3696 | { |
6a9b8820 DW |
3697 | names_cachep = kmem_cache_create_usercopy("names_cache", PATH_MAX, 0, |
3698 | SLAB_HWCACHE_ALIGN|SLAB_PANIC, 0, PATH_MAX, NULL); | |
1da177e4 | 3699 | |
74bf17cf DC |
3700 | dcache_init(); |
3701 | inode_init(); | |
4248b0da MG |
3702 | files_init(); |
3703 | files_maxfiles_init(); | |
74bf17cf | 3704 | mnt_init(); |
1da177e4 LT |
3705 | bdev_cache_init(); |
3706 | chrdev_init(); | |
3707 | } |