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> | |
29 | #include <asm/uaccess.h> | |
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> |
613afbf8 | 35 | #include <linux/hardirq.h> |
ceb5bdc2 NP |
36 | #include <linux/bit_spinlock.h> |
37 | #include <linux/rculist_bl.h> | |
268bb0ce | 38 | #include <linux/prefetch.h> |
dd179946 | 39 | #include <linux/ratelimit.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: |
d76c446b | 46 | * - i_dentry, d_u.d_alias, d_inode of aliases |
ceb5bdc2 NP |
47 | * dcache_hash_bucket lock protects: |
48 | * - the dcache hash table | |
49 | * s_anon bl list spinlock protects: | |
50 | * - the s_anon list (see __d_drop) | |
23044507 NP |
51 | * dcache_lru_lock protects: |
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 | |
d76c446b | 61 | * - d_u.d_alias, d_inode |
789680d1 NP |
62 | * |
63 | * Ordering: | |
873feea0 | 64 | * dentry->d_inode->i_lock |
b5c84bf6 NP |
65 | * dentry->d_lock |
66 | * dcache_lru_lock | |
ceb5bdc2 NP |
67 | * dcache_hash_bucket lock |
68 | * s_anon 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 | ||
23044507 | 84 | static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_lru_lock); |
74c3cbe3 | 85 | __cacheline_aligned_in_smp DEFINE_SEQLOCK(rename_lock); |
1da177e4 | 86 | |
949854d0 | 87 | EXPORT_SYMBOL(rename_lock); |
1da177e4 | 88 | |
e18b890b | 89 | static struct kmem_cache *dentry_cache __read_mostly; |
1da177e4 | 90 | |
1da177e4 LT |
91 | /* |
92 | * This is the single most critical data structure when it comes | |
93 | * to the dcache: the hashtable for lookups. Somebody should try | |
94 | * to make this good - I've just made it work. | |
95 | * | |
96 | * This hash-function tries to avoid losing too many bits of hash | |
97 | * information, yet avoid using a prime hash-size or similar. | |
98 | */ | |
1da177e4 | 99 | |
fa3536cc ED |
100 | static unsigned int d_hash_mask __read_mostly; |
101 | static unsigned int d_hash_shift __read_mostly; | |
ceb5bdc2 | 102 | |
b07ad996 | 103 | static struct hlist_bl_head *dentry_hashtable __read_mostly; |
ceb5bdc2 | 104 | |
8966be90 | 105 | static inline struct hlist_bl_head *d_hash(const struct dentry *parent, |
6d7d1a0d | 106 | unsigned int hash) |
ceb5bdc2 | 107 | { |
6d7d1a0d | 108 | hash += (unsigned long) parent / L1_CACHE_BYTES; |
d4c96061 | 109 | return dentry_hashtable + hash_32(hash, d_hash_shift); |
ceb5bdc2 NP |
110 | } |
111 | ||
1da177e4 LT |
112 | /* Statistics gathering. */ |
113 | struct dentry_stat_t dentry_stat = { | |
114 | .age_limit = 45, | |
115 | }; | |
116 | ||
3e880fb5 | 117 | static DEFINE_PER_CPU(unsigned int, nr_dentry); |
312d3ca8 CH |
118 | |
119 | #if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS) | |
3e880fb5 NP |
120 | static int get_nr_dentry(void) |
121 | { | |
122 | int i; | |
123 | int sum = 0; | |
124 | for_each_possible_cpu(i) | |
125 | sum += per_cpu(nr_dentry, i); | |
126 | return sum < 0 ? 0 : sum; | |
127 | } | |
128 | ||
312d3ca8 CH |
129 | int proc_nr_dentry(ctl_table *table, int write, void __user *buffer, |
130 | size_t *lenp, loff_t *ppos) | |
131 | { | |
3e880fb5 | 132 | dentry_stat.nr_dentry = get_nr_dentry(); |
312d3ca8 CH |
133 | return proc_dointvec(table, write, buffer, lenp, ppos); |
134 | } | |
135 | #endif | |
136 | ||
5483f18e LT |
137 | /* |
138 | * Compare 2 name strings, return 0 if they match, otherwise non-zero. | |
139 | * The strings are both count bytes long, and count is non-zero. | |
140 | */ | |
e419b4cc LT |
141 | #ifdef CONFIG_DCACHE_WORD_ACCESS |
142 | ||
143 | #include <asm/word-at-a-time.h> | |
144 | /* | |
145 | * NOTE! 'cs' and 'scount' come from a dentry, so it has a | |
146 | * aligned allocation for this particular component. We don't | |
147 | * strictly need the load_unaligned_zeropad() safety, but it | |
148 | * doesn't hurt either. | |
149 | * | |
150 | * In contrast, 'ct' and 'tcount' can be from a pathname, and do | |
151 | * need the careful unaligned handling. | |
152 | */ | |
94753db5 | 153 | static inline int dentry_string_cmp(const unsigned char *cs, const unsigned char *ct, unsigned tcount) |
5483f18e | 154 | { |
bfcfaa77 | 155 | unsigned long a,b,mask; |
bfcfaa77 LT |
156 | |
157 | for (;;) { | |
12f8ad4b | 158 | a = *(unsigned long *)cs; |
e419b4cc | 159 | b = load_unaligned_zeropad(ct); |
bfcfaa77 LT |
160 | if (tcount < sizeof(unsigned long)) |
161 | break; | |
162 | if (unlikely(a != b)) | |
163 | return 1; | |
164 | cs += sizeof(unsigned long); | |
165 | ct += sizeof(unsigned long); | |
166 | tcount -= sizeof(unsigned long); | |
167 | if (!tcount) | |
168 | return 0; | |
169 | } | |
170 | mask = ~(~0ul << tcount*8); | |
171 | return unlikely(!!((a ^ b) & mask)); | |
e419b4cc LT |
172 | } |
173 | ||
bfcfaa77 | 174 | #else |
e419b4cc | 175 | |
94753db5 | 176 | static inline int dentry_string_cmp(const unsigned char *cs, const unsigned char *ct, unsigned tcount) |
e419b4cc | 177 | { |
5483f18e LT |
178 | do { |
179 | if (*cs != *ct) | |
180 | return 1; | |
181 | cs++; | |
182 | ct++; | |
183 | tcount--; | |
184 | } while (tcount); | |
185 | return 0; | |
186 | } | |
187 | ||
e419b4cc LT |
188 | #endif |
189 | ||
94753db5 LT |
190 | static inline int dentry_cmp(const struct dentry *dentry, const unsigned char *ct, unsigned tcount) |
191 | { | |
6326c71f | 192 | const unsigned char *cs; |
94753db5 LT |
193 | /* |
194 | * Be careful about RCU walk racing with rename: | |
195 | * use ACCESS_ONCE to fetch the name pointer. | |
196 | * | |
197 | * NOTE! Even if a rename will mean that the length | |
198 | * was not loaded atomically, we don't care. The | |
199 | * RCU walk will check the sequence count eventually, | |
200 | * and catch it. And we won't overrun the buffer, | |
201 | * because we're reading the name pointer atomically, | |
202 | * and a dentry name is guaranteed to be properly | |
203 | * terminated with a NUL byte. | |
204 | * | |
205 | * End result: even if 'len' is wrong, we'll exit | |
206 | * early because the data cannot match (there can | |
207 | * be no NUL in the ct/tcount data) | |
208 | */ | |
6326c71f LT |
209 | cs = ACCESS_ONCE(dentry->d_name.name); |
210 | smp_read_barrier_depends(); | |
211 | return dentry_string_cmp(cs, ct, tcount); | |
94753db5 LT |
212 | } |
213 | ||
9c82ab9c | 214 | static void __d_free(struct rcu_head *head) |
1da177e4 | 215 | { |
9c82ab9c CH |
216 | struct dentry *dentry = container_of(head, struct dentry, d_u.d_rcu); |
217 | ||
1da177e4 LT |
218 | if (dname_external(dentry)) |
219 | kfree(dentry->d_name.name); | |
220 | kmem_cache_free(dentry_cache, dentry); | |
221 | } | |
222 | ||
223 | /* | |
b5c84bf6 | 224 | * no locks, please. |
1da177e4 LT |
225 | */ |
226 | static void d_free(struct dentry *dentry) | |
227 | { | |
d76c446b | 228 | WARN_ON(!hlist_unhashed(&dentry->d_u.d_alias)); |
b7ab39f6 | 229 | BUG_ON(dentry->d_count); |
3e880fb5 | 230 | this_cpu_dec(nr_dentry); |
1da177e4 LT |
231 | if (dentry->d_op && dentry->d_op->d_release) |
232 | dentry->d_op->d_release(dentry); | |
312d3ca8 | 233 | |
dea3667b LT |
234 | /* if dentry was never visible to RCU, immediate free is OK */ |
235 | if (!(dentry->d_flags & DCACHE_RCUACCESS)) | |
9c82ab9c | 236 | __d_free(&dentry->d_u.d_rcu); |
b3423415 | 237 | else |
9c82ab9c | 238 | call_rcu(&dentry->d_u.d_rcu, __d_free); |
1da177e4 LT |
239 | } |
240 | ||
ae9cf59b AV |
241 | void take_dentry_name_snapshot(struct name_snapshot *name, struct dentry *dentry) |
242 | { | |
243 | spin_lock(&dentry->d_lock); | |
244 | if (unlikely(dname_external(dentry))) { | |
245 | u32 len; | |
246 | char *p; | |
247 | for (;;) { | |
248 | len = dentry->d_name.len; | |
249 | spin_unlock(&dentry->d_lock); | |
250 | p = kmalloc(len + 1, GFP_KERNEL | __GFP_NOFAIL); | |
251 | spin_lock(&dentry->d_lock); | |
252 | if (dentry->d_name.len <= len) | |
253 | break; | |
254 | kfree(p); | |
255 | } | |
256 | memcpy(p, dentry->d_name.name, dentry->d_name.len + 1); | |
257 | spin_unlock(&dentry->d_lock); | |
258 | name->name = p; | |
259 | } else { | |
260 | memcpy(name->inline_name, dentry->d_iname, DNAME_INLINE_LEN); | |
261 | spin_unlock(&dentry->d_lock); | |
262 | name->name = name->inline_name; | |
263 | } | |
264 | } | |
265 | EXPORT_SYMBOL(take_dentry_name_snapshot); | |
266 | void release_dentry_name_snapshot(struct name_snapshot *name) | |
267 | { | |
268 | if (unlikely(name->name != name->inline_name)) | |
269 | kfree(name->name); | |
270 | } | |
271 | EXPORT_SYMBOL(release_dentry_name_snapshot); | |
272 | ||
31e6b01f NP |
273 | /** |
274 | * dentry_rcuwalk_barrier - invalidate in-progress rcu-walk lookups | |
ff5fdb61 | 275 | * @dentry: the target dentry |
31e6b01f NP |
276 | * After this call, in-progress rcu-walk path lookup will fail. This |
277 | * should be called after unhashing, and after changing d_inode (if | |
278 | * the dentry has not already been unhashed). | |
279 | */ | |
280 | static inline void dentry_rcuwalk_barrier(struct dentry *dentry) | |
281 | { | |
282 | assert_spin_locked(&dentry->d_lock); | |
283 | /* Go through a barrier */ | |
284 | write_seqcount_barrier(&dentry->d_seq); | |
285 | } | |
286 | ||
1da177e4 LT |
287 | /* |
288 | * Release the dentry's inode, using the filesystem | |
31e6b01f NP |
289 | * d_iput() operation if defined. Dentry has no refcount |
290 | * and is unhashed. | |
1da177e4 | 291 | */ |
858119e1 | 292 | static void dentry_iput(struct dentry * dentry) |
31f3e0b3 | 293 | __releases(dentry->d_lock) |
873feea0 | 294 | __releases(dentry->d_inode->i_lock) |
1da177e4 LT |
295 | { |
296 | struct inode *inode = dentry->d_inode; | |
297 | if (inode) { | |
298 | dentry->d_inode = NULL; | |
d76c446b | 299 | hlist_del_init(&dentry->d_u.d_alias); |
1da177e4 | 300 | spin_unlock(&dentry->d_lock); |
873feea0 | 301 | spin_unlock(&inode->i_lock); |
f805fbda LT |
302 | if (!inode->i_nlink) |
303 | fsnotify_inoderemove(inode); | |
1da177e4 LT |
304 | if (dentry->d_op && dentry->d_op->d_iput) |
305 | dentry->d_op->d_iput(dentry, inode); | |
306 | else | |
307 | iput(inode); | |
308 | } else { | |
309 | spin_unlock(&dentry->d_lock); | |
1da177e4 LT |
310 | } |
311 | } | |
312 | ||
31e6b01f NP |
313 | /* |
314 | * Release the dentry's inode, using the filesystem | |
315 | * d_iput() operation if defined. dentry remains in-use. | |
316 | */ | |
317 | static void dentry_unlink_inode(struct dentry * dentry) | |
318 | __releases(dentry->d_lock) | |
873feea0 | 319 | __releases(dentry->d_inode->i_lock) |
31e6b01f NP |
320 | { |
321 | struct inode *inode = dentry->d_inode; | |
322 | dentry->d_inode = NULL; | |
d76c446b | 323 | hlist_del_init(&dentry->d_u.d_alias); |
31e6b01f NP |
324 | dentry_rcuwalk_barrier(dentry); |
325 | spin_unlock(&dentry->d_lock); | |
873feea0 | 326 | spin_unlock(&inode->i_lock); |
31e6b01f NP |
327 | if (!inode->i_nlink) |
328 | fsnotify_inoderemove(inode); | |
329 | if (dentry->d_op && dentry->d_op->d_iput) | |
330 | dentry->d_op->d_iput(dentry, inode); | |
331 | else | |
332 | iput(inode); | |
333 | } | |
334 | ||
da3bbdd4 | 335 | /* |
f0023bc6 | 336 | * dentry_lru_(add|del|prune|move_tail) must be called with d_lock held. |
da3bbdd4 KM |
337 | */ |
338 | static void dentry_lru_add(struct dentry *dentry) | |
339 | { | |
a4633357 | 340 | if (list_empty(&dentry->d_lru)) { |
23044507 | 341 | spin_lock(&dcache_lru_lock); |
a4633357 CH |
342 | list_add(&dentry->d_lru, &dentry->d_sb->s_dentry_lru); |
343 | dentry->d_sb->s_nr_dentry_unused++; | |
86c8749e | 344 | dentry_stat.nr_unused++; |
23044507 | 345 | spin_unlock(&dcache_lru_lock); |
a4633357 | 346 | } |
da3bbdd4 KM |
347 | } |
348 | ||
23044507 NP |
349 | static void __dentry_lru_del(struct dentry *dentry) |
350 | { | |
351 | list_del_init(&dentry->d_lru); | |
eaf5f907 | 352 | dentry->d_flags &= ~DCACHE_SHRINK_LIST; |
23044507 NP |
353 | dentry->d_sb->s_nr_dentry_unused--; |
354 | dentry_stat.nr_unused--; | |
355 | } | |
356 | ||
f0023bc6 SW |
357 | /* |
358 | * Remove a dentry with references from the LRU. | |
359 | */ | |
da3bbdd4 KM |
360 | static void dentry_lru_del(struct dentry *dentry) |
361 | { | |
362 | if (!list_empty(&dentry->d_lru)) { | |
23044507 NP |
363 | spin_lock(&dcache_lru_lock); |
364 | __dentry_lru_del(dentry); | |
365 | spin_unlock(&dcache_lru_lock); | |
da3bbdd4 KM |
366 | } |
367 | } | |
368 | ||
b48f03b3 | 369 | static void dentry_lru_move_list(struct dentry *dentry, struct list_head *list) |
da3bbdd4 | 370 | { |
23044507 | 371 | spin_lock(&dcache_lru_lock); |
a4633357 | 372 | if (list_empty(&dentry->d_lru)) { |
b48f03b3 | 373 | list_add_tail(&dentry->d_lru, list); |
a4633357 | 374 | dentry->d_sb->s_nr_dentry_unused++; |
86c8749e | 375 | dentry_stat.nr_unused++; |
a4633357 | 376 | } else { |
b48f03b3 | 377 | list_move_tail(&dentry->d_lru, list); |
da3bbdd4 | 378 | } |
23044507 | 379 | spin_unlock(&dcache_lru_lock); |
da3bbdd4 KM |
380 | } |
381 | ||
d52b9086 MS |
382 | /** |
383 | * d_kill - kill dentry and return parent | |
384 | * @dentry: dentry to kill | |
ff5fdb61 | 385 | * @parent: parent dentry |
d52b9086 | 386 | * |
31f3e0b3 | 387 | * The dentry must already be unhashed and removed from the LRU. |
d52b9086 MS |
388 | * |
389 | * If this is the root of the dentry tree, return NULL. | |
23044507 | 390 | * |
b5c84bf6 NP |
391 | * dentry->d_lock and parent->d_lock must be held by caller, and are dropped by |
392 | * d_kill. | |
d52b9086 | 393 | */ |
2fd6b7f5 | 394 | static struct dentry *d_kill(struct dentry *dentry, struct dentry *parent) |
31f3e0b3 | 395 | __releases(dentry->d_lock) |
2fd6b7f5 | 396 | __releases(parent->d_lock) |
873feea0 | 397 | __releases(dentry->d_inode->i_lock) |
d52b9086 | 398 | { |
b102b017 | 399 | __list_del_entry(&dentry->d_child); |
c83ce989 | 400 | /* |
b102b017 | 401 | * Inform ascending readers that we are no longer attached to the |
c83ce989 TM |
402 | * dentry tree |
403 | */ | |
b161dfa6 | 404 | dentry->d_flags |= DCACHE_DENTRY_KILLED; |
2fd6b7f5 NP |
405 | if (parent) |
406 | spin_unlock(&parent->d_lock); | |
d52b9086 | 407 | dentry_iput(dentry); |
b7ab39f6 NP |
408 | /* |
409 | * dentry_iput drops the locks, at which point nobody (except | |
410 | * transient RCU lookups) can reach this dentry. | |
411 | */ | |
d52b9086 | 412 | d_free(dentry); |
871c0067 | 413 | return parent; |
d52b9086 MS |
414 | } |
415 | ||
c6627c60 DH |
416 | /* |
417 | * Unhash a dentry without inserting an RCU walk barrier or checking that | |
418 | * dentry->d_lock is locked. The caller must take care of that, if | |
419 | * appropriate. | |
420 | */ | |
421 | static void __d_shrink(struct dentry *dentry) | |
422 | { | |
423 | if (!d_unhashed(dentry)) { | |
424 | struct hlist_bl_head *b; | |
425 | if (unlikely(dentry->d_flags & DCACHE_DISCONNECTED)) | |
426 | b = &dentry->d_sb->s_anon; | |
427 | else | |
428 | b = d_hash(dentry->d_parent, dentry->d_name.hash); | |
429 | ||
430 | hlist_bl_lock(b); | |
431 | __hlist_bl_del(&dentry->d_hash); | |
432 | dentry->d_hash.pprev = NULL; | |
433 | hlist_bl_unlock(b); | |
434 | } | |
435 | } | |
436 | ||
789680d1 NP |
437 | /** |
438 | * d_drop - drop a dentry | |
439 | * @dentry: dentry to drop | |
440 | * | |
441 | * d_drop() unhashes the entry from the parent dentry hashes, so that it won't | |
442 | * be found through a VFS lookup any more. Note that this is different from | |
443 | * deleting the dentry - d_delete will try to mark the dentry negative if | |
444 | * possible, giving a successful _negative_ lookup, while d_drop will | |
445 | * just make the cache lookup fail. | |
446 | * | |
447 | * d_drop() is used mainly for stuff that wants to invalidate a dentry for some | |
448 | * reason (NFS timeouts or autofs deletes). | |
449 | * | |
450 | * __d_drop requires dentry->d_lock. | |
451 | */ | |
452 | void __d_drop(struct dentry *dentry) | |
453 | { | |
dea3667b | 454 | if (!d_unhashed(dentry)) { |
c6627c60 | 455 | __d_shrink(dentry); |
dea3667b | 456 | dentry_rcuwalk_barrier(dentry); |
789680d1 NP |
457 | } |
458 | } | |
459 | EXPORT_SYMBOL(__d_drop); | |
460 | ||
461 | void d_drop(struct dentry *dentry) | |
462 | { | |
789680d1 NP |
463 | spin_lock(&dentry->d_lock); |
464 | __d_drop(dentry); | |
465 | spin_unlock(&dentry->d_lock); | |
789680d1 NP |
466 | } |
467 | EXPORT_SYMBOL(d_drop); | |
468 | ||
77812a1e NP |
469 | /* |
470 | * Finish off a dentry we've decided to kill. | |
471 | * dentry->d_lock must be held, returns with it unlocked. | |
472 | * If ref is non-zero, then decrement the refcount too. | |
473 | * Returns dentry requiring refcount drop, or NULL if we're done. | |
474 | */ | |
475 | static inline struct dentry *dentry_kill(struct dentry *dentry, int ref) | |
476 | __releases(dentry->d_lock) | |
477 | { | |
873feea0 | 478 | struct inode *inode; |
77812a1e NP |
479 | struct dentry *parent; |
480 | ||
873feea0 NP |
481 | inode = dentry->d_inode; |
482 | if (inode && !spin_trylock(&inode->i_lock)) { | |
77812a1e NP |
483 | relock: |
484 | spin_unlock(&dentry->d_lock); | |
485 | cpu_relax(); | |
486 | return dentry; /* try again with same dentry */ | |
487 | } | |
488 | if (IS_ROOT(dentry)) | |
489 | parent = NULL; | |
490 | else | |
491 | parent = dentry->d_parent; | |
492 | if (parent && !spin_trylock(&parent->d_lock)) { | |
873feea0 NP |
493 | if (inode) |
494 | spin_unlock(&inode->i_lock); | |
77812a1e NP |
495 | goto relock; |
496 | } | |
31e6b01f | 497 | |
77812a1e NP |
498 | if (ref) |
499 | dentry->d_count--; | |
f0023bc6 | 500 | /* |
f0023bc6 SW |
501 | * inform the fs via d_prune that this dentry is about to be |
502 | * unhashed and destroyed. | |
503 | */ | |
61572bb1 YZ |
504 | if (dentry->d_flags & DCACHE_OP_PRUNE) |
505 | dentry->d_op->d_prune(dentry); | |
506 | ||
507 | dentry_lru_del(dentry); | |
77812a1e NP |
508 | /* if it was on the hash then remove it */ |
509 | __d_drop(dentry); | |
510 | return d_kill(dentry, parent); | |
511 | } | |
512 | ||
1da177e4 LT |
513 | /* |
514 | * This is dput | |
515 | * | |
516 | * This is complicated by the fact that we do not want to put | |
517 | * dentries that are no longer on any hash chain on the unused | |
518 | * list: we'd much rather just get rid of them immediately. | |
519 | * | |
520 | * However, that implies that we have to traverse the dentry | |
521 | * tree upwards to the parents which might _also_ now be | |
522 | * scheduled for deletion (it may have been only waiting for | |
523 | * its last child to go away). | |
524 | * | |
525 | * This tail recursion is done by hand as we don't want to depend | |
526 | * on the compiler to always get this right (gcc generally doesn't). | |
527 | * Real recursion would eat up our stack space. | |
528 | */ | |
529 | ||
530 | /* | |
531 | * dput - release a dentry | |
532 | * @dentry: dentry to release | |
533 | * | |
534 | * Release a dentry. This will drop the usage count and if appropriate | |
535 | * call the dentry unlink method as well as removing it from the queues and | |
536 | * releasing its resources. If the parent dentries were scheduled for release | |
537 | * they too may now get deleted. | |
1da177e4 | 538 | */ |
1da177e4 LT |
539 | void dput(struct dentry *dentry) |
540 | { | |
541 | if (!dentry) | |
542 | return; | |
543 | ||
544 | repeat: | |
b7ab39f6 | 545 | if (dentry->d_count == 1) |
1da177e4 | 546 | might_sleep(); |
1da177e4 | 547 | spin_lock(&dentry->d_lock); |
61f3dee4 NP |
548 | BUG_ON(!dentry->d_count); |
549 | if (dentry->d_count > 1) { | |
550 | dentry->d_count--; | |
1da177e4 | 551 | spin_unlock(&dentry->d_lock); |
1da177e4 LT |
552 | return; |
553 | } | |
554 | ||
fb045adb | 555 | if (dentry->d_flags & DCACHE_OP_DELETE) { |
1da177e4 | 556 | if (dentry->d_op->d_delete(dentry)) |
61f3dee4 | 557 | goto kill_it; |
1da177e4 | 558 | } |
265ac902 | 559 | |
1da177e4 LT |
560 | /* Unreachable? Get rid of it */ |
561 | if (d_unhashed(dentry)) | |
562 | goto kill_it; | |
265ac902 | 563 | |
39e3c955 | 564 | dentry->d_flags |= DCACHE_REFERENCED; |
a4633357 | 565 | dentry_lru_add(dentry); |
265ac902 | 566 | |
61f3dee4 NP |
567 | dentry->d_count--; |
568 | spin_unlock(&dentry->d_lock); | |
1da177e4 LT |
569 | return; |
570 | ||
d52b9086 | 571 | kill_it: |
77812a1e | 572 | dentry = dentry_kill(dentry, 1); |
d52b9086 MS |
573 | if (dentry) |
574 | goto repeat; | |
1da177e4 | 575 | } |
ec4f8605 | 576 | EXPORT_SYMBOL(dput); |
1da177e4 LT |
577 | |
578 | /** | |
579 | * d_invalidate - invalidate a dentry | |
580 | * @dentry: dentry to invalidate | |
581 | * | |
582 | * Try to invalidate the dentry if it turns out to be | |
583 | * possible. If there are other dentries that can be | |
584 | * reached through this one we can't delete it and we | |
585 | * return -EBUSY. On success we return 0. | |
586 | * | |
587 | * no dcache lock. | |
588 | */ | |
589 | ||
590 | int d_invalidate(struct dentry * dentry) | |
591 | { | |
592 | /* | |
593 | * If it's already been dropped, return OK. | |
594 | */ | |
da502956 | 595 | spin_lock(&dentry->d_lock); |
1da177e4 | 596 | if (d_unhashed(dentry)) { |
da502956 | 597 | spin_unlock(&dentry->d_lock); |
1da177e4 LT |
598 | return 0; |
599 | } | |
600 | /* | |
601 | * Check whether to do a partial shrink_dcache | |
602 | * to get rid of unused child entries. | |
603 | */ | |
604 | if (!list_empty(&dentry->d_subdirs)) { | |
da502956 | 605 | spin_unlock(&dentry->d_lock); |
1da177e4 | 606 | shrink_dcache_parent(dentry); |
da502956 | 607 | spin_lock(&dentry->d_lock); |
1da177e4 LT |
608 | } |
609 | ||
610 | /* | |
611 | * Somebody else still using it? | |
612 | * | |
613 | * If it's a directory, we can't drop it | |
614 | * for fear of somebody re-populating it | |
615 | * with children (even though dropping it | |
616 | * would make it unreachable from the root, | |
617 | * we might still populate it if it was a | |
618 | * working directory or similar). | |
50e69630 AV |
619 | * We also need to leave mountpoints alone, |
620 | * directory or not. | |
1da177e4 | 621 | */ |
50e69630 AV |
622 | if (dentry->d_count > 1 && dentry->d_inode) { |
623 | if (S_ISDIR(dentry->d_inode->i_mode) || d_mountpoint(dentry)) { | |
1da177e4 | 624 | spin_unlock(&dentry->d_lock); |
1da177e4 LT |
625 | return -EBUSY; |
626 | } | |
627 | } | |
628 | ||
629 | __d_drop(dentry); | |
630 | spin_unlock(&dentry->d_lock); | |
1da177e4 LT |
631 | return 0; |
632 | } | |
ec4f8605 | 633 | EXPORT_SYMBOL(d_invalidate); |
1da177e4 | 634 | |
b5c84bf6 | 635 | /* This must be called with d_lock held */ |
dc0474be | 636 | static inline void __dget_dlock(struct dentry *dentry) |
23044507 | 637 | { |
b7ab39f6 | 638 | dentry->d_count++; |
23044507 NP |
639 | } |
640 | ||
dc0474be | 641 | static inline void __dget(struct dentry *dentry) |
1da177e4 | 642 | { |
23044507 | 643 | spin_lock(&dentry->d_lock); |
dc0474be | 644 | __dget_dlock(dentry); |
23044507 | 645 | spin_unlock(&dentry->d_lock); |
1da177e4 LT |
646 | } |
647 | ||
b7ab39f6 NP |
648 | struct dentry *dget_parent(struct dentry *dentry) |
649 | { | |
650 | struct dentry *ret; | |
651 | ||
652 | repeat: | |
a734eb45 NP |
653 | /* |
654 | * Don't need rcu_dereference because we re-check it was correct under | |
655 | * the lock. | |
656 | */ | |
657 | rcu_read_lock(); | |
b7ab39f6 | 658 | ret = dentry->d_parent; |
a734eb45 NP |
659 | spin_lock(&ret->d_lock); |
660 | if (unlikely(ret != dentry->d_parent)) { | |
661 | spin_unlock(&ret->d_lock); | |
662 | rcu_read_unlock(); | |
b7ab39f6 NP |
663 | goto repeat; |
664 | } | |
a734eb45 | 665 | rcu_read_unlock(); |
b7ab39f6 NP |
666 | BUG_ON(!ret->d_count); |
667 | ret->d_count++; | |
668 | spin_unlock(&ret->d_lock); | |
b7ab39f6 NP |
669 | return ret; |
670 | } | |
671 | EXPORT_SYMBOL(dget_parent); | |
672 | ||
1da177e4 LT |
673 | /** |
674 | * d_find_alias - grab a hashed alias of inode | |
675 | * @inode: inode in question | |
32ba9c3f LT |
676 | * @want_discon: flag, used by d_splice_alias, to request |
677 | * that only a DISCONNECTED alias be returned. | |
1da177e4 LT |
678 | * |
679 | * If inode has a hashed alias, or is a directory and has any alias, | |
680 | * acquire the reference to alias and return it. Otherwise return NULL. | |
681 | * Notice that if inode is a directory there can be only one alias and | |
682 | * it can be unhashed only if it has no children, or if it is the root | |
683 | * of a filesystem. | |
684 | * | |
21c0d8fd | 685 | * If the inode has an IS_ROOT, DCACHE_DISCONNECTED alias, then prefer |
32ba9c3f LT |
686 | * any other hashed alias over that one unless @want_discon is set, |
687 | * in which case only return an IS_ROOT, DCACHE_DISCONNECTED alias. | |
1da177e4 | 688 | */ |
32ba9c3f | 689 | static struct dentry *__d_find_alias(struct inode *inode, int want_discon) |
1da177e4 | 690 | { |
da502956 | 691 | struct dentry *alias, *discon_alias; |
1da177e4 | 692 | |
da502956 NP |
693 | again: |
694 | discon_alias = NULL; | |
d76c446b | 695 | hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) { |
da502956 | 696 | spin_lock(&alias->d_lock); |
1da177e4 | 697 | if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) { |
21c0d8fd | 698 | if (IS_ROOT(alias) && |
da502956 | 699 | (alias->d_flags & DCACHE_DISCONNECTED)) { |
1da177e4 | 700 | discon_alias = alias; |
32ba9c3f | 701 | } else if (!want_discon) { |
dc0474be | 702 | __dget_dlock(alias); |
da502956 NP |
703 | spin_unlock(&alias->d_lock); |
704 | return alias; | |
705 | } | |
706 | } | |
707 | spin_unlock(&alias->d_lock); | |
708 | } | |
709 | if (discon_alias) { | |
710 | alias = discon_alias; | |
711 | spin_lock(&alias->d_lock); | |
712 | if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) { | |
713 | if (IS_ROOT(alias) && | |
714 | (alias->d_flags & DCACHE_DISCONNECTED)) { | |
dc0474be | 715 | __dget_dlock(alias); |
da502956 | 716 | spin_unlock(&alias->d_lock); |
1da177e4 LT |
717 | return alias; |
718 | } | |
719 | } | |
da502956 NP |
720 | spin_unlock(&alias->d_lock); |
721 | goto again; | |
1da177e4 | 722 | } |
da502956 | 723 | return NULL; |
1da177e4 LT |
724 | } |
725 | ||
da502956 | 726 | struct dentry *d_find_alias(struct inode *inode) |
1da177e4 | 727 | { |
214fda1f DH |
728 | struct dentry *de = NULL; |
729 | ||
b3d9b7a3 | 730 | if (!hlist_empty(&inode->i_dentry)) { |
873feea0 | 731 | spin_lock(&inode->i_lock); |
32ba9c3f | 732 | de = __d_find_alias(inode, 0); |
873feea0 | 733 | spin_unlock(&inode->i_lock); |
214fda1f | 734 | } |
1da177e4 LT |
735 | return de; |
736 | } | |
ec4f8605 | 737 | EXPORT_SYMBOL(d_find_alias); |
1da177e4 LT |
738 | |
739 | /* | |
740 | * Try to kill dentries associated with this inode. | |
741 | * WARNING: you must own a reference to inode. | |
742 | */ | |
743 | void d_prune_aliases(struct inode *inode) | |
744 | { | |
0cdca3f9 | 745 | struct dentry *dentry; |
1da177e4 | 746 | restart: |
873feea0 | 747 | spin_lock(&inode->i_lock); |
d76c446b | 748 | hlist_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) { |
1da177e4 | 749 | spin_lock(&dentry->d_lock); |
b7ab39f6 | 750 | if (!dentry->d_count) { |
dc0474be | 751 | __dget_dlock(dentry); |
1da177e4 LT |
752 | __d_drop(dentry); |
753 | spin_unlock(&dentry->d_lock); | |
873feea0 | 754 | spin_unlock(&inode->i_lock); |
1da177e4 LT |
755 | dput(dentry); |
756 | goto restart; | |
757 | } | |
758 | spin_unlock(&dentry->d_lock); | |
759 | } | |
873feea0 | 760 | spin_unlock(&inode->i_lock); |
1da177e4 | 761 | } |
ec4f8605 | 762 | EXPORT_SYMBOL(d_prune_aliases); |
1da177e4 LT |
763 | |
764 | /* | |
77812a1e NP |
765 | * Try to throw away a dentry - free the inode, dput the parent. |
766 | * Requires dentry->d_lock is held, and dentry->d_count == 0. | |
767 | * Releases dentry->d_lock. | |
d702ccb3 | 768 | * |
77812a1e | 769 | * This may fail if locks cannot be acquired no problem, just try again. |
1da177e4 | 770 | */ |
77812a1e | 771 | static void try_prune_one_dentry(struct dentry *dentry) |
31f3e0b3 | 772 | __releases(dentry->d_lock) |
1da177e4 | 773 | { |
77812a1e | 774 | struct dentry *parent; |
d52b9086 | 775 | |
77812a1e | 776 | parent = dentry_kill(dentry, 0); |
d52b9086 | 777 | /* |
77812a1e NP |
778 | * If dentry_kill returns NULL, we have nothing more to do. |
779 | * if it returns the same dentry, trylocks failed. In either | |
780 | * case, just loop again. | |
781 | * | |
782 | * Otherwise, we need to prune ancestors too. This is necessary | |
783 | * to prevent quadratic behavior of shrink_dcache_parent(), but | |
784 | * is also expected to be beneficial in reducing dentry cache | |
785 | * fragmentation. | |
d52b9086 | 786 | */ |
77812a1e NP |
787 | if (!parent) |
788 | return; | |
789 | if (parent == dentry) | |
790 | return; | |
791 | ||
792 | /* Prune ancestors. */ | |
793 | dentry = parent; | |
d52b9086 | 794 | while (dentry) { |
b7ab39f6 | 795 | spin_lock(&dentry->d_lock); |
89e60548 NP |
796 | if (dentry->d_count > 1) { |
797 | dentry->d_count--; | |
798 | spin_unlock(&dentry->d_lock); | |
799 | return; | |
800 | } | |
77812a1e | 801 | dentry = dentry_kill(dentry, 1); |
d52b9086 | 802 | } |
1da177e4 LT |
803 | } |
804 | ||
3049cfe2 | 805 | static void shrink_dentry_list(struct list_head *list) |
1da177e4 | 806 | { |
da3bbdd4 | 807 | struct dentry *dentry; |
da3bbdd4 | 808 | |
ec33679d NP |
809 | rcu_read_lock(); |
810 | for (;;) { | |
ec33679d NP |
811 | dentry = list_entry_rcu(list->prev, struct dentry, d_lru); |
812 | if (&dentry->d_lru == list) | |
813 | break; /* empty */ | |
814 | spin_lock(&dentry->d_lock); | |
815 | if (dentry != list_entry(list->prev, struct dentry, d_lru)) { | |
816 | spin_unlock(&dentry->d_lock); | |
23044507 NP |
817 | continue; |
818 | } | |
819 | ||
1da177e4 LT |
820 | /* |
821 | * We found an inuse dentry which was not removed from | |
da3bbdd4 KM |
822 | * the LRU because of laziness during lookup. Do not free |
823 | * it - just keep it off the LRU list. | |
1da177e4 | 824 | */ |
b7ab39f6 | 825 | if (dentry->d_count) { |
ec33679d | 826 | dentry_lru_del(dentry); |
da3bbdd4 | 827 | spin_unlock(&dentry->d_lock); |
1da177e4 LT |
828 | continue; |
829 | } | |
ec33679d | 830 | |
ec33679d | 831 | rcu_read_unlock(); |
77812a1e NP |
832 | |
833 | try_prune_one_dentry(dentry); | |
834 | ||
ec33679d | 835 | rcu_read_lock(); |
da3bbdd4 | 836 | } |
ec33679d | 837 | rcu_read_unlock(); |
3049cfe2 CH |
838 | } |
839 | ||
840 | /** | |
b48f03b3 DC |
841 | * prune_dcache_sb - shrink the dcache |
842 | * @sb: superblock | |
843 | * @count: number of entries to try to free | |
844 | * | |
845 | * Attempt to shrink the superblock dcache LRU by @count entries. This is | |
846 | * done when we need more memory an called from the superblock shrinker | |
847 | * function. | |
3049cfe2 | 848 | * |
b48f03b3 DC |
849 | * This function may fail to free any resources if all the dentries are in |
850 | * use. | |
3049cfe2 | 851 | */ |
b48f03b3 | 852 | void prune_dcache_sb(struct super_block *sb, int count) |
3049cfe2 | 853 | { |
3049cfe2 CH |
854 | struct dentry *dentry; |
855 | LIST_HEAD(referenced); | |
856 | LIST_HEAD(tmp); | |
3049cfe2 | 857 | |
23044507 NP |
858 | relock: |
859 | spin_lock(&dcache_lru_lock); | |
3049cfe2 CH |
860 | while (!list_empty(&sb->s_dentry_lru)) { |
861 | dentry = list_entry(sb->s_dentry_lru.prev, | |
862 | struct dentry, d_lru); | |
863 | BUG_ON(dentry->d_sb != sb); | |
864 | ||
23044507 NP |
865 | if (!spin_trylock(&dentry->d_lock)) { |
866 | spin_unlock(&dcache_lru_lock); | |
867 | cpu_relax(); | |
868 | goto relock; | |
869 | } | |
870 | ||
b48f03b3 | 871 | if (dentry->d_flags & DCACHE_REFERENCED) { |
23044507 NP |
872 | dentry->d_flags &= ~DCACHE_REFERENCED; |
873 | list_move(&dentry->d_lru, &referenced); | |
3049cfe2 | 874 | spin_unlock(&dentry->d_lock); |
23044507 NP |
875 | } else { |
876 | list_move_tail(&dentry->d_lru, &tmp); | |
eaf5f907 | 877 | dentry->d_flags |= DCACHE_SHRINK_LIST; |
23044507 | 878 | spin_unlock(&dentry->d_lock); |
b0d40c92 | 879 | if (!--count) |
23044507 | 880 | break; |
3049cfe2 | 881 | } |
ec33679d | 882 | cond_resched_lock(&dcache_lru_lock); |
3049cfe2 | 883 | } |
da3bbdd4 KM |
884 | if (!list_empty(&referenced)) |
885 | list_splice(&referenced, &sb->s_dentry_lru); | |
23044507 | 886 | spin_unlock(&dcache_lru_lock); |
ec33679d NP |
887 | |
888 | shrink_dentry_list(&tmp); | |
da3bbdd4 KM |
889 | } |
890 | ||
1da177e4 LT |
891 | /** |
892 | * shrink_dcache_sb - shrink dcache for a superblock | |
893 | * @sb: superblock | |
894 | * | |
3049cfe2 CH |
895 | * Shrink the dcache for the specified super block. This is used to free |
896 | * the dcache before unmounting a file system. | |
1da177e4 | 897 | */ |
3049cfe2 | 898 | void shrink_dcache_sb(struct super_block *sb) |
1da177e4 | 899 | { |
3049cfe2 CH |
900 | LIST_HEAD(tmp); |
901 | ||
23044507 | 902 | spin_lock(&dcache_lru_lock); |
3049cfe2 CH |
903 | while (!list_empty(&sb->s_dentry_lru)) { |
904 | list_splice_init(&sb->s_dentry_lru, &tmp); | |
ec33679d | 905 | spin_unlock(&dcache_lru_lock); |
3049cfe2 | 906 | shrink_dentry_list(&tmp); |
ec33679d | 907 | spin_lock(&dcache_lru_lock); |
3049cfe2 | 908 | } |
23044507 | 909 | spin_unlock(&dcache_lru_lock); |
1da177e4 | 910 | } |
ec4f8605 | 911 | EXPORT_SYMBOL(shrink_dcache_sb); |
1da177e4 | 912 | |
c636ebdb DH |
913 | /* |
914 | * destroy a single subtree of dentries for unmount | |
915 | * - see the comments on shrink_dcache_for_umount() for a description of the | |
916 | * locking | |
917 | */ | |
918 | static void shrink_dcache_for_umount_subtree(struct dentry *dentry) | |
919 | { | |
920 | struct dentry *parent; | |
921 | ||
922 | BUG_ON(!IS_ROOT(dentry)); | |
923 | ||
c636ebdb DH |
924 | for (;;) { |
925 | /* descend to the first leaf in the current subtree */ | |
43c1c9cd | 926 | while (!list_empty(&dentry->d_subdirs)) |
c636ebdb | 927 | dentry = list_entry(dentry->d_subdirs.next, |
d76c446b | 928 | struct dentry, d_child); |
c636ebdb DH |
929 | |
930 | /* consume the dentries from this leaf up through its parents | |
931 | * until we find one with children or run out altogether */ | |
932 | do { | |
933 | struct inode *inode; | |
934 | ||
f0023bc6 | 935 | /* |
61572bb1 | 936 | * inform the fs that this dentry is about to be |
f0023bc6 SW |
937 | * unhashed and destroyed. |
938 | */ | |
61572bb1 YZ |
939 | if (dentry->d_flags & DCACHE_OP_PRUNE) |
940 | dentry->d_op->d_prune(dentry); | |
941 | ||
942 | dentry_lru_del(dentry); | |
43c1c9cd DH |
943 | __d_shrink(dentry); |
944 | ||
b7ab39f6 | 945 | if (dentry->d_count != 0) { |
c636ebdb DH |
946 | printk(KERN_ERR |
947 | "BUG: Dentry %p{i=%lx,n=%s}" | |
948 | " still in use (%d)" | |
949 | " [unmount of %s %s]\n", | |
950 | dentry, | |
951 | dentry->d_inode ? | |
952 | dentry->d_inode->i_ino : 0UL, | |
953 | dentry->d_name.name, | |
b7ab39f6 | 954 | dentry->d_count, |
c636ebdb DH |
955 | dentry->d_sb->s_type->name, |
956 | dentry->d_sb->s_id); | |
957 | BUG(); | |
958 | } | |
959 | ||
2fd6b7f5 | 960 | if (IS_ROOT(dentry)) { |
c636ebdb | 961 | parent = NULL; |
d76c446b | 962 | list_del(&dentry->d_child); |
2fd6b7f5 | 963 | } else { |
871c0067 | 964 | parent = dentry->d_parent; |
b7ab39f6 | 965 | parent->d_count--; |
d76c446b | 966 | list_del(&dentry->d_child); |
871c0067 | 967 | } |
c636ebdb | 968 | |
c636ebdb DH |
969 | inode = dentry->d_inode; |
970 | if (inode) { | |
971 | dentry->d_inode = NULL; | |
d76c446b | 972 | hlist_del_init(&dentry->d_u.d_alias); |
c636ebdb DH |
973 | if (dentry->d_op && dentry->d_op->d_iput) |
974 | dentry->d_op->d_iput(dentry, inode); | |
975 | else | |
976 | iput(inode); | |
977 | } | |
978 | ||
979 | d_free(dentry); | |
980 | ||
981 | /* finished when we fall off the top of the tree, | |
982 | * otherwise we ascend to the parent and move to the | |
983 | * next sibling if there is one */ | |
984 | if (!parent) | |
312d3ca8 | 985 | return; |
c636ebdb | 986 | dentry = parent; |
c636ebdb DH |
987 | } while (list_empty(&dentry->d_subdirs)); |
988 | ||
989 | dentry = list_entry(dentry->d_subdirs.next, | |
d76c446b | 990 | struct dentry, d_child); |
c636ebdb DH |
991 | } |
992 | } | |
993 | ||
994 | /* | |
995 | * destroy the dentries attached to a superblock on unmounting | |
b5c84bf6 | 996 | * - we don't need to use dentry->d_lock because: |
c636ebdb DH |
997 | * - the superblock is detached from all mountings and open files, so the |
998 | * dentry trees will not be rearranged by the VFS | |
999 | * - s_umount is write-locked, so the memory pressure shrinker will ignore | |
1000 | * any dentries belonging to this superblock that it comes across | |
1001 | * - the filesystem itself is no longer permitted to rearrange the dentries | |
1002 | * in this superblock | |
1003 | */ | |
1004 | void shrink_dcache_for_umount(struct super_block *sb) | |
1005 | { | |
1006 | struct dentry *dentry; | |
1007 | ||
1008 | if (down_read_trylock(&sb->s_umount)) | |
1009 | BUG(); | |
1010 | ||
1011 | dentry = sb->s_root; | |
1012 | sb->s_root = NULL; | |
b7ab39f6 | 1013 | dentry->d_count--; |
c636ebdb DH |
1014 | shrink_dcache_for_umount_subtree(dentry); |
1015 | ||
ceb5bdc2 NP |
1016 | while (!hlist_bl_empty(&sb->s_anon)) { |
1017 | dentry = hlist_bl_entry(hlist_bl_first(&sb->s_anon), struct dentry, d_hash); | |
c636ebdb DH |
1018 | shrink_dcache_for_umount_subtree(dentry); |
1019 | } | |
1020 | } | |
1021 | ||
1da177e4 LT |
1022 | /* |
1023 | * Search for at least 1 mount point in the dentry's subdirs. | |
1024 | * We descend to the next level whenever the d_subdirs | |
1025 | * list is non-empty and continue searching. | |
1026 | */ | |
1027 | ||
1028 | /** | |
1029 | * have_submounts - check for mounts over a dentry | |
1030 | * @parent: dentry to check. | |
1031 | * | |
1032 | * Return true if the parent or its subdirectories contain | |
1033 | * a mount point | |
1034 | */ | |
1da177e4 LT |
1035 | int have_submounts(struct dentry *parent) |
1036 | { | |
949854d0 | 1037 | struct dentry *this_parent; |
1da177e4 | 1038 | struct list_head *next; |
949854d0 | 1039 | unsigned seq; |
58db63d0 | 1040 | int locked = 0; |
949854d0 | 1041 | |
949854d0 | 1042 | seq = read_seqbegin(&rename_lock); |
58db63d0 NP |
1043 | again: |
1044 | this_parent = parent; | |
1da177e4 | 1045 | |
1da177e4 LT |
1046 | if (d_mountpoint(parent)) |
1047 | goto positive; | |
2fd6b7f5 | 1048 | spin_lock(&this_parent->d_lock); |
1da177e4 LT |
1049 | repeat: |
1050 | next = this_parent->d_subdirs.next; | |
1051 | resume: | |
1052 | while (next != &this_parent->d_subdirs) { | |
1053 | struct list_head *tmp = next; | |
d76c446b | 1054 | struct dentry *dentry = list_entry(tmp, struct dentry, d_child); |
1da177e4 | 1055 | next = tmp->next; |
2fd6b7f5 NP |
1056 | |
1057 | spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); | |
1da177e4 | 1058 | /* Have we found a mount point ? */ |
2fd6b7f5 NP |
1059 | if (d_mountpoint(dentry)) { |
1060 | spin_unlock(&dentry->d_lock); | |
1061 | spin_unlock(&this_parent->d_lock); | |
1da177e4 | 1062 | goto positive; |
2fd6b7f5 | 1063 | } |
1da177e4 | 1064 | if (!list_empty(&dentry->d_subdirs)) { |
2fd6b7f5 NP |
1065 | spin_unlock(&this_parent->d_lock); |
1066 | spin_release(&dentry->d_lock.dep_map, 1, _RET_IP_); | |
1da177e4 | 1067 | this_parent = dentry; |
2fd6b7f5 | 1068 | spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_); |
1da177e4 LT |
1069 | goto repeat; |
1070 | } | |
2fd6b7f5 | 1071 | spin_unlock(&dentry->d_lock); |
1da177e4 LT |
1072 | } |
1073 | /* | |
1074 | * All done at this level ... ascend and resume the search. | |
1075 | */ | |
b102b017 AV |
1076 | rcu_read_lock(); |
1077 | ascend: | |
1da177e4 | 1078 | if (this_parent != parent) { |
c826cb7d | 1079 | struct dentry *child = this_parent; |
b102b017 AV |
1080 | this_parent = child->d_parent; |
1081 | ||
1082 | spin_unlock(&child->d_lock); | |
1083 | spin_lock(&this_parent->d_lock); | |
1084 | ||
1085 | /* might go back up the wrong parent if we have had a rename. */ | |
1086 | if (!locked && read_seqretry(&rename_lock, seq)) | |
949854d0 | 1087 | goto rename_retry; |
d76c446b | 1088 | next = child->d_child.next; |
b102b017 AV |
1089 | while (unlikely(child->d_flags & DCACHE_DENTRY_KILLED)) { |
1090 | if (next == &this_parent->d_subdirs) | |
1091 | goto ascend; | |
1092 | child = list_entry(next, struct dentry, d_child); | |
1093 | next = next->next; | |
1094 | } | |
1095 | rcu_read_unlock(); | |
1da177e4 LT |
1096 | goto resume; |
1097 | } | |
58db63d0 | 1098 | if (!locked && read_seqretry(&rename_lock, seq)) |
949854d0 | 1099 | goto rename_retry; |
b102b017 AV |
1100 | spin_unlock(&this_parent->d_lock); |
1101 | rcu_read_unlock(); | |
58db63d0 NP |
1102 | if (locked) |
1103 | write_sequnlock(&rename_lock); | |
1da177e4 LT |
1104 | return 0; /* No mount points found in tree */ |
1105 | positive: | |
58db63d0 | 1106 | if (!locked && read_seqretry(&rename_lock, seq)) |
e2dbdc06 | 1107 | goto rename_retry_unlocked; |
58db63d0 NP |
1108 | if (locked) |
1109 | write_sequnlock(&rename_lock); | |
1da177e4 | 1110 | return 1; |
58db63d0 NP |
1111 | |
1112 | rename_retry: | |
b102b017 AV |
1113 | spin_unlock(&this_parent->d_lock); |
1114 | rcu_read_unlock(); | |
8110e16d MS |
1115 | if (locked) |
1116 | goto again; | |
e2dbdc06 | 1117 | rename_retry_unlocked: |
58db63d0 NP |
1118 | locked = 1; |
1119 | write_seqlock(&rename_lock); | |
1120 | goto again; | |
1da177e4 | 1121 | } |
ec4f8605 | 1122 | EXPORT_SYMBOL(have_submounts); |
1da177e4 LT |
1123 | |
1124 | /* | |
fd517909 | 1125 | * Search the dentry child list of the specified parent, |
1da177e4 LT |
1126 | * and move any unused dentries to the end of the unused |
1127 | * list for prune_dcache(). We descend to the next level | |
1128 | * whenever the d_subdirs list is non-empty and continue | |
1129 | * searching. | |
1130 | * | |
1131 | * It returns zero iff there are no unused children, | |
1132 | * otherwise it returns the number of children moved to | |
1133 | * the end of the unused list. This may not be the total | |
1134 | * number of unused children, because select_parent can | |
1135 | * drop the lock and return early due to latency | |
1136 | * constraints. | |
1137 | */ | |
b48f03b3 | 1138 | static int select_parent(struct dentry *parent, struct list_head *dispose) |
1da177e4 | 1139 | { |
949854d0 | 1140 | struct dentry *this_parent; |
1da177e4 | 1141 | struct list_head *next; |
949854d0 | 1142 | unsigned seq; |
1da177e4 | 1143 | int found = 0; |
58db63d0 | 1144 | int locked = 0; |
1da177e4 | 1145 | |
949854d0 | 1146 | seq = read_seqbegin(&rename_lock); |
58db63d0 NP |
1147 | again: |
1148 | this_parent = parent; | |
2fd6b7f5 | 1149 | spin_lock(&this_parent->d_lock); |
1da177e4 LT |
1150 | repeat: |
1151 | next = this_parent->d_subdirs.next; | |
1152 | resume: | |
1153 | while (next != &this_parent->d_subdirs) { | |
1154 | struct list_head *tmp = next; | |
d76c446b | 1155 | struct dentry *dentry = list_entry(tmp, struct dentry, d_child); |
1da177e4 LT |
1156 | next = tmp->next; |
1157 | ||
2fd6b7f5 | 1158 | spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); |
23044507 | 1159 | |
b48f03b3 DC |
1160 | /* |
1161 | * move only zero ref count dentries to the dispose list. | |
eaf5f907 MS |
1162 | * |
1163 | * Those which are presently on the shrink list, being processed | |
1164 | * by shrink_dentry_list(), shouldn't be moved. Otherwise the | |
1165 | * loop in shrink_dcache_parent() might not make any progress | |
1166 | * and loop forever. | |
1da177e4 | 1167 | */ |
eaf5f907 MS |
1168 | if (dentry->d_count) { |
1169 | dentry_lru_del(dentry); | |
1170 | } else if (!(dentry->d_flags & DCACHE_SHRINK_LIST)) { | |
b48f03b3 | 1171 | dentry_lru_move_list(dentry, dispose); |
eaf5f907 | 1172 | dentry->d_flags |= DCACHE_SHRINK_LIST; |
1da177e4 LT |
1173 | found++; |
1174 | } | |
1da177e4 LT |
1175 | /* |
1176 | * We can return to the caller if we have found some (this | |
1177 | * ensures forward progress). We'll be coming back to find | |
1178 | * the rest. | |
1179 | */ | |
2fd6b7f5 NP |
1180 | if (found && need_resched()) { |
1181 | spin_unlock(&dentry->d_lock); | |
e2dbdc06 | 1182 | rcu_read_lock(); |
1da177e4 | 1183 | goto out; |
2fd6b7f5 | 1184 | } |
1da177e4 LT |
1185 | |
1186 | /* | |
1187 | * Descend a level if the d_subdirs list is non-empty. | |
1188 | */ | |
1189 | if (!list_empty(&dentry->d_subdirs)) { | |
2fd6b7f5 NP |
1190 | spin_unlock(&this_parent->d_lock); |
1191 | spin_release(&dentry->d_lock.dep_map, 1, _RET_IP_); | |
1da177e4 | 1192 | this_parent = dentry; |
2fd6b7f5 | 1193 | spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_); |
1da177e4 LT |
1194 | goto repeat; |
1195 | } | |
2fd6b7f5 NP |
1196 | |
1197 | spin_unlock(&dentry->d_lock); | |
1da177e4 LT |
1198 | } |
1199 | /* | |
1200 | * All done at this level ... ascend and resume the search. | |
1201 | */ | |
b102b017 AV |
1202 | rcu_read_lock(); |
1203 | ascend: | |
1da177e4 | 1204 | if (this_parent != parent) { |
c826cb7d | 1205 | struct dentry *child = this_parent; |
b102b017 AV |
1206 | this_parent = child->d_parent; |
1207 | ||
1208 | spin_unlock(&child->d_lock); | |
1209 | spin_lock(&this_parent->d_lock); | |
1210 | ||
1211 | /* might go back up the wrong parent if we have had a rename. */ | |
1212 | if (!locked && read_seqretry(&rename_lock, seq)) | |
949854d0 | 1213 | goto rename_retry; |
d76c446b | 1214 | next = child->d_child.next; |
b102b017 AV |
1215 | while (unlikely(child->d_flags & DCACHE_DENTRY_KILLED)) { |
1216 | if (next == &this_parent->d_subdirs) | |
1217 | goto ascend; | |
1218 | child = list_entry(next, struct dentry, d_child); | |
1219 | next = next->next; | |
1220 | } | |
1221 | rcu_read_unlock(); | |
1da177e4 LT |
1222 | goto resume; |
1223 | } | |
1224 | out: | |
58db63d0 | 1225 | if (!locked && read_seqretry(&rename_lock, seq)) |
949854d0 | 1226 | goto rename_retry; |
b102b017 AV |
1227 | spin_unlock(&this_parent->d_lock); |
1228 | rcu_read_unlock(); | |
58db63d0 NP |
1229 | if (locked) |
1230 | write_sequnlock(&rename_lock); | |
1da177e4 | 1231 | return found; |
58db63d0 NP |
1232 | |
1233 | rename_retry: | |
b102b017 AV |
1234 | spin_unlock(&this_parent->d_lock); |
1235 | rcu_read_unlock(); | |
58db63d0 NP |
1236 | if (found) |
1237 | return found; | |
8110e16d MS |
1238 | if (locked) |
1239 | goto again; | |
58db63d0 NP |
1240 | locked = 1; |
1241 | write_seqlock(&rename_lock); | |
1242 | goto again; | |
1da177e4 LT |
1243 | } |
1244 | ||
1245 | /** | |
1246 | * shrink_dcache_parent - prune dcache | |
1247 | * @parent: parent of entries to prune | |
1248 | * | |
1249 | * Prune the dcache to remove unused children of the parent dentry. | |
1250 | */ | |
1da177e4 LT |
1251 | void shrink_dcache_parent(struct dentry * parent) |
1252 | { | |
b48f03b3 | 1253 | LIST_HEAD(dispose); |
1da177e4 LT |
1254 | int found; |
1255 | ||
421348f1 | 1256 | while ((found = select_parent(parent, &dispose)) != 0) { |
b48f03b3 | 1257 | shrink_dentry_list(&dispose); |
421348f1 GT |
1258 | cond_resched(); |
1259 | } | |
1da177e4 | 1260 | } |
ec4f8605 | 1261 | EXPORT_SYMBOL(shrink_dcache_parent); |
1da177e4 | 1262 | |
1da177e4 | 1263 | /** |
a4464dbc AV |
1264 | * __d_alloc - allocate a dcache entry |
1265 | * @sb: filesystem it will belong to | |
1da177e4 LT |
1266 | * @name: qstr of the name |
1267 | * | |
1268 | * Allocates a dentry. It returns %NULL if there is insufficient memory | |
1269 | * available. On a success the dentry is returned. The name passed in is | |
1270 | * copied and the copy passed in may be reused after this call. | |
1271 | */ | |
1272 | ||
a4464dbc | 1273 | struct dentry *__d_alloc(struct super_block *sb, const struct qstr *name) |
1da177e4 LT |
1274 | { |
1275 | struct dentry *dentry; | |
1276 | char *dname; | |
1277 | ||
e12ba74d | 1278 | dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL); |
1da177e4 LT |
1279 | if (!dentry) |
1280 | return NULL; | |
1281 | ||
6326c71f LT |
1282 | /* |
1283 | * We guarantee that the inline name is always NUL-terminated. | |
1284 | * This way the memcpy() done by the name switching in rename | |
1285 | * will still always have a NUL at the end, even if we might | |
1286 | * be overwriting an internal NUL character | |
1287 | */ | |
1288 | dentry->d_iname[DNAME_INLINE_LEN-1] = 0; | |
1da177e4 LT |
1289 | if (name->len > DNAME_INLINE_LEN-1) { |
1290 | dname = kmalloc(name->len + 1, GFP_KERNEL); | |
1291 | if (!dname) { | |
1292 | kmem_cache_free(dentry_cache, dentry); | |
1293 | return NULL; | |
1294 | } | |
1295 | } else { | |
1296 | dname = dentry->d_iname; | |
1297 | } | |
1da177e4 LT |
1298 | |
1299 | dentry->d_name.len = name->len; | |
1300 | dentry->d_name.hash = name->hash; | |
1301 | memcpy(dname, name->name, name->len); | |
1302 | dname[name->len] = 0; | |
1303 | ||
6326c71f LT |
1304 | /* Make sure we always see the terminating NUL character */ |
1305 | smp_wmb(); | |
1306 | dentry->d_name.name = dname; | |
1307 | ||
b7ab39f6 | 1308 | dentry->d_count = 1; |
dea3667b | 1309 | dentry->d_flags = 0; |
1da177e4 | 1310 | spin_lock_init(&dentry->d_lock); |
31e6b01f | 1311 | seqcount_init(&dentry->d_seq); |
1da177e4 | 1312 | dentry->d_inode = NULL; |
a4464dbc AV |
1313 | dentry->d_parent = dentry; |
1314 | dentry->d_sb = sb; | |
1da177e4 LT |
1315 | dentry->d_op = NULL; |
1316 | dentry->d_fsdata = NULL; | |
ceb5bdc2 | 1317 | INIT_HLIST_BL_NODE(&dentry->d_hash); |
1da177e4 LT |
1318 | INIT_LIST_HEAD(&dentry->d_lru); |
1319 | INIT_LIST_HEAD(&dentry->d_subdirs); | |
d76c446b AV |
1320 | INIT_HLIST_NODE(&dentry->d_u.d_alias); |
1321 | INIT_LIST_HEAD(&dentry->d_child); | |
a4464dbc | 1322 | d_set_d_op(dentry, dentry->d_sb->s_d_op); |
1da177e4 | 1323 | |
3e880fb5 | 1324 | this_cpu_inc(nr_dentry); |
312d3ca8 | 1325 | |
1da177e4 LT |
1326 | return dentry; |
1327 | } | |
a4464dbc AV |
1328 | |
1329 | /** | |
1330 | * d_alloc - allocate a dcache entry | |
1331 | * @parent: parent of entry to allocate | |
1332 | * @name: qstr of the name | |
1333 | * | |
1334 | * Allocates a dentry. It returns %NULL if there is insufficient memory | |
1335 | * available. On a success the dentry is returned. The name passed in is | |
1336 | * copied and the copy passed in may be reused after this call. | |
1337 | */ | |
1338 | struct dentry *d_alloc(struct dentry * parent, const struct qstr *name) | |
1339 | { | |
1340 | struct dentry *dentry = __d_alloc(parent->d_sb, name); | |
1341 | if (!dentry) | |
1342 | return NULL; | |
1343 | ||
1344 | spin_lock(&parent->d_lock); | |
1345 | /* | |
1346 | * don't need child lock because it is not subject | |
1347 | * to concurrency here | |
1348 | */ | |
1349 | __dget_dlock(parent); | |
1350 | dentry->d_parent = parent; | |
d76c446b | 1351 | list_add(&dentry->d_child, &parent->d_subdirs); |
a4464dbc AV |
1352 | spin_unlock(&parent->d_lock); |
1353 | ||
1354 | return dentry; | |
1355 | } | |
ec4f8605 | 1356 | EXPORT_SYMBOL(d_alloc); |
1da177e4 | 1357 | |
4b936885 NP |
1358 | struct dentry *d_alloc_pseudo(struct super_block *sb, const struct qstr *name) |
1359 | { | |
a4464dbc AV |
1360 | struct dentry *dentry = __d_alloc(sb, name); |
1361 | if (dentry) | |
4b936885 | 1362 | dentry->d_flags |= DCACHE_DISCONNECTED; |
4b936885 NP |
1363 | return dentry; |
1364 | } | |
1365 | EXPORT_SYMBOL(d_alloc_pseudo); | |
1366 | ||
1da177e4 LT |
1367 | struct dentry *d_alloc_name(struct dentry *parent, const char *name) |
1368 | { | |
1369 | struct qstr q; | |
1370 | ||
1371 | q.name = name; | |
1372 | q.len = strlen(name); | |
1373 | q.hash = full_name_hash(q.name, q.len); | |
1374 | return d_alloc(parent, &q); | |
1375 | } | |
ef26ca97 | 1376 | EXPORT_SYMBOL(d_alloc_name); |
1da177e4 | 1377 | |
fb045adb NP |
1378 | void d_set_d_op(struct dentry *dentry, const struct dentry_operations *op) |
1379 | { | |
6f7f7caa LT |
1380 | WARN_ON_ONCE(dentry->d_op); |
1381 | WARN_ON_ONCE(dentry->d_flags & (DCACHE_OP_HASH | | |
fb045adb NP |
1382 | DCACHE_OP_COMPARE | |
1383 | DCACHE_OP_REVALIDATE | | |
ecf3d1f1 | 1384 | DCACHE_OP_WEAK_REVALIDATE | |
fb045adb NP |
1385 | DCACHE_OP_DELETE )); |
1386 | dentry->d_op = op; | |
1387 | if (!op) | |
1388 | return; | |
1389 | if (op->d_hash) | |
1390 | dentry->d_flags |= DCACHE_OP_HASH; | |
1391 | if (op->d_compare) | |
1392 | dentry->d_flags |= DCACHE_OP_COMPARE; | |
1393 | if (op->d_revalidate) | |
1394 | dentry->d_flags |= DCACHE_OP_REVALIDATE; | |
ecf3d1f1 JL |
1395 | if (op->d_weak_revalidate) |
1396 | dentry->d_flags |= DCACHE_OP_WEAK_REVALIDATE; | |
fb045adb NP |
1397 | if (op->d_delete) |
1398 | dentry->d_flags |= DCACHE_OP_DELETE; | |
f0023bc6 SW |
1399 | if (op->d_prune) |
1400 | dentry->d_flags |= DCACHE_OP_PRUNE; | |
fb045adb NP |
1401 | |
1402 | } | |
1403 | EXPORT_SYMBOL(d_set_d_op); | |
1404 | ||
360da900 OH |
1405 | static void __d_instantiate(struct dentry *dentry, struct inode *inode) |
1406 | { | |
b23fb0a6 | 1407 | spin_lock(&dentry->d_lock); |
9875cf80 DH |
1408 | if (inode) { |
1409 | if (unlikely(IS_AUTOMOUNT(inode))) | |
1410 | dentry->d_flags |= DCACHE_NEED_AUTOMOUNT; | |
d76c446b | 1411 | hlist_add_head(&dentry->d_u.d_alias, &inode->i_dentry); |
9875cf80 | 1412 | } |
360da900 | 1413 | dentry->d_inode = inode; |
31e6b01f | 1414 | dentry_rcuwalk_barrier(dentry); |
b23fb0a6 | 1415 | spin_unlock(&dentry->d_lock); |
360da900 OH |
1416 | fsnotify_d_instantiate(dentry, inode); |
1417 | } | |
1418 | ||
1da177e4 LT |
1419 | /** |
1420 | * d_instantiate - fill in inode information for a dentry | |
1421 | * @entry: dentry to complete | |
1422 | * @inode: inode to attach to this dentry | |
1423 | * | |
1424 | * Fill in inode information in the entry. | |
1425 | * | |
1426 | * This turns negative dentries into productive full members | |
1427 | * of society. | |
1428 | * | |
1429 | * NOTE! This assumes that the inode count has been incremented | |
1430 | * (or otherwise set) by the caller to indicate that it is now | |
1431 | * in use by the dcache. | |
1432 | */ | |
1433 | ||
1434 | void d_instantiate(struct dentry *entry, struct inode * inode) | |
1435 | { | |
d76c446b | 1436 | BUG_ON(!hlist_unhashed(&entry->d_u.d_alias)); |
873feea0 NP |
1437 | if (inode) |
1438 | spin_lock(&inode->i_lock); | |
360da900 | 1439 | __d_instantiate(entry, inode); |
873feea0 NP |
1440 | if (inode) |
1441 | spin_unlock(&inode->i_lock); | |
1da177e4 LT |
1442 | security_d_instantiate(entry, inode); |
1443 | } | |
ec4f8605 | 1444 | EXPORT_SYMBOL(d_instantiate); |
1da177e4 LT |
1445 | |
1446 | /** | |
1447 | * d_instantiate_unique - instantiate a non-aliased dentry | |
1448 | * @entry: dentry to instantiate | |
1449 | * @inode: inode to attach to this dentry | |
1450 | * | |
1451 | * Fill in inode information in the entry. On success, it returns NULL. | |
1452 | * If an unhashed alias of "entry" already exists, then we return the | |
e866cfa9 | 1453 | * aliased dentry instead and drop one reference to inode. |
1da177e4 LT |
1454 | * |
1455 | * Note that in order to avoid conflicts with rename() etc, the caller | |
1456 | * had better be holding the parent directory semaphore. | |
e866cfa9 OD |
1457 | * |
1458 | * This also assumes that the inode count has been incremented | |
1459 | * (or otherwise set) by the caller to indicate that it is now | |
1460 | * in use by the dcache. | |
1da177e4 | 1461 | */ |
770bfad8 DH |
1462 | static struct dentry *__d_instantiate_unique(struct dentry *entry, |
1463 | struct inode *inode) | |
1da177e4 LT |
1464 | { |
1465 | struct dentry *alias; | |
1466 | int len = entry->d_name.len; | |
1467 | const char *name = entry->d_name.name; | |
1468 | unsigned int hash = entry->d_name.hash; | |
1469 | ||
770bfad8 | 1470 | if (!inode) { |
360da900 | 1471 | __d_instantiate(entry, NULL); |
770bfad8 DH |
1472 | return NULL; |
1473 | } | |
1474 | ||
d76c446b | 1475 | hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) { |
9abca360 NP |
1476 | /* |
1477 | * Don't need alias->d_lock here, because aliases with | |
1478 | * d_parent == entry->d_parent are not subject to name or | |
1479 | * parent changes, because the parent inode i_mutex is held. | |
1480 | */ | |
12f8ad4b | 1481 | if (alias->d_name.hash != hash) |
1da177e4 LT |
1482 | continue; |
1483 | if (alias->d_parent != entry->d_parent) | |
1484 | continue; | |
ee983e89 LT |
1485 | if (alias->d_name.len != len) |
1486 | continue; | |
12f8ad4b | 1487 | if (dentry_cmp(alias, name, len)) |
1da177e4 | 1488 | continue; |
dc0474be | 1489 | __dget(alias); |
1da177e4 LT |
1490 | return alias; |
1491 | } | |
770bfad8 | 1492 | |
360da900 | 1493 | __d_instantiate(entry, inode); |
1da177e4 LT |
1494 | return NULL; |
1495 | } | |
770bfad8 DH |
1496 | |
1497 | struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode) | |
1498 | { | |
1499 | struct dentry *result; | |
1500 | ||
d76c446b | 1501 | BUG_ON(!hlist_unhashed(&entry->d_u.d_alias)); |
770bfad8 | 1502 | |
873feea0 NP |
1503 | if (inode) |
1504 | spin_lock(&inode->i_lock); | |
770bfad8 | 1505 | result = __d_instantiate_unique(entry, inode); |
873feea0 NP |
1506 | if (inode) |
1507 | spin_unlock(&inode->i_lock); | |
770bfad8 DH |
1508 | |
1509 | if (!result) { | |
1510 | security_d_instantiate(entry, inode); | |
1511 | return NULL; | |
1512 | } | |
1513 | ||
1514 | BUG_ON(!d_unhashed(result)); | |
1515 | iput(inode); | |
1516 | return result; | |
1517 | } | |
1518 | ||
1da177e4 LT |
1519 | EXPORT_SYMBOL(d_instantiate_unique); |
1520 | ||
adc0e91a AV |
1521 | struct dentry *d_make_root(struct inode *root_inode) |
1522 | { | |
1523 | struct dentry *res = NULL; | |
1524 | ||
1525 | if (root_inode) { | |
26fe5750 | 1526 | static const struct qstr name = QSTR_INIT("/", 1); |
adc0e91a AV |
1527 | |
1528 | res = __d_alloc(root_inode->i_sb, &name); | |
1529 | if (res) | |
1530 | d_instantiate(res, root_inode); | |
1531 | else | |
1532 | iput(root_inode); | |
1533 | } | |
1534 | return res; | |
1535 | } | |
1536 | EXPORT_SYMBOL(d_make_root); | |
1537 | ||
d891eedb BF |
1538 | static struct dentry * __d_find_any_alias(struct inode *inode) |
1539 | { | |
1540 | struct dentry *alias; | |
1541 | ||
b3d9b7a3 | 1542 | if (hlist_empty(&inode->i_dentry)) |
d891eedb | 1543 | return NULL; |
d76c446b | 1544 | alias = hlist_entry(inode->i_dentry.first, struct dentry, d_u.d_alias); |
d891eedb BF |
1545 | __dget(alias); |
1546 | return alias; | |
1547 | } | |
1548 | ||
46f72b34 SW |
1549 | /** |
1550 | * d_find_any_alias - find any alias for a given inode | |
1551 | * @inode: inode to find an alias for | |
1552 | * | |
1553 | * If any aliases exist for the given inode, take and return a | |
1554 | * reference for one of them. If no aliases exist, return %NULL. | |
1555 | */ | |
1556 | struct dentry *d_find_any_alias(struct inode *inode) | |
d891eedb BF |
1557 | { |
1558 | struct dentry *de; | |
1559 | ||
1560 | spin_lock(&inode->i_lock); | |
1561 | de = __d_find_any_alias(inode); | |
1562 | spin_unlock(&inode->i_lock); | |
1563 | return de; | |
1564 | } | |
46f72b34 | 1565 | EXPORT_SYMBOL(d_find_any_alias); |
d891eedb | 1566 | |
4ea3ada2 CH |
1567 | /** |
1568 | * d_obtain_alias - find or allocate a dentry for a given inode | |
1569 | * @inode: inode to allocate the dentry for | |
1570 | * | |
1571 | * Obtain a dentry for an inode resulting from NFS filehandle conversion or | |
1572 | * similar open by handle operations. The returned dentry may be anonymous, | |
1573 | * or may have a full name (if the inode was already in the cache). | |
1574 | * | |
1575 | * When called on a directory inode, we must ensure that the inode only ever | |
1576 | * has one dentry. If a dentry is found, that is returned instead of | |
1577 | * allocating a new one. | |
1578 | * | |
1579 | * On successful return, the reference to the inode has been transferred | |
44003728 CH |
1580 | * to the dentry. In case of an error the reference on the inode is released. |
1581 | * To make it easier to use in export operations a %NULL or IS_ERR inode may | |
1582 | * be passed in and will be the error will be propagate to the return value, | |
1583 | * with a %NULL @inode replaced by ERR_PTR(-ESTALE). | |
4ea3ada2 CH |
1584 | */ |
1585 | struct dentry *d_obtain_alias(struct inode *inode) | |
1586 | { | |
b911a6bd | 1587 | static const struct qstr anonstring = QSTR_INIT("/", 1); |
9308a612 CH |
1588 | struct dentry *tmp; |
1589 | struct dentry *res; | |
4ea3ada2 CH |
1590 | |
1591 | if (!inode) | |
44003728 | 1592 | return ERR_PTR(-ESTALE); |
4ea3ada2 CH |
1593 | if (IS_ERR(inode)) |
1594 | return ERR_CAST(inode); | |
1595 | ||
d891eedb | 1596 | res = d_find_any_alias(inode); |
9308a612 CH |
1597 | if (res) |
1598 | goto out_iput; | |
1599 | ||
a4464dbc | 1600 | tmp = __d_alloc(inode->i_sb, &anonstring); |
9308a612 CH |
1601 | if (!tmp) { |
1602 | res = ERR_PTR(-ENOMEM); | |
1603 | goto out_iput; | |
4ea3ada2 | 1604 | } |
b5c84bf6 | 1605 | |
873feea0 | 1606 | spin_lock(&inode->i_lock); |
d891eedb | 1607 | res = __d_find_any_alias(inode); |
9308a612 | 1608 | if (res) { |
873feea0 | 1609 | spin_unlock(&inode->i_lock); |
9308a612 CH |
1610 | dput(tmp); |
1611 | goto out_iput; | |
1612 | } | |
1613 | ||
1614 | /* attach a disconnected dentry */ | |
1615 | spin_lock(&tmp->d_lock); | |
9308a612 CH |
1616 | tmp->d_inode = inode; |
1617 | tmp->d_flags |= DCACHE_DISCONNECTED; | |
d76c446b | 1618 | hlist_add_head(&tmp->d_u.d_alias, &inode->i_dentry); |
1879fd6a | 1619 | hlist_bl_lock(&tmp->d_sb->s_anon); |
ceb5bdc2 | 1620 | hlist_bl_add_head(&tmp->d_hash, &tmp->d_sb->s_anon); |
1879fd6a | 1621 | hlist_bl_unlock(&tmp->d_sb->s_anon); |
9308a612 | 1622 | spin_unlock(&tmp->d_lock); |
873feea0 | 1623 | spin_unlock(&inode->i_lock); |
24ff6663 | 1624 | security_d_instantiate(tmp, inode); |
9308a612 | 1625 | |
9308a612 CH |
1626 | return tmp; |
1627 | ||
1628 | out_iput: | |
24ff6663 JB |
1629 | if (res && !IS_ERR(res)) |
1630 | security_d_instantiate(res, inode); | |
9308a612 CH |
1631 | iput(inode); |
1632 | return res; | |
4ea3ada2 | 1633 | } |
adc48720 | 1634 | EXPORT_SYMBOL(d_obtain_alias); |
1da177e4 LT |
1635 | |
1636 | /** | |
1637 | * d_splice_alias - splice a disconnected dentry into the tree if one exists | |
1638 | * @inode: the inode which may have a disconnected dentry | |
1639 | * @dentry: a negative dentry which we want to point to the inode. | |
1640 | * | |
1641 | * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and | |
1642 | * DCACHE_DISCONNECTED), then d_move that in place of the given dentry | |
1643 | * and return it, else simply d_add the inode to the dentry and return NULL. | |
1644 | * | |
1645 | * This is needed in the lookup routine of any filesystem that is exportable | |
1646 | * (via knfsd) so that we can build dcache paths to directories effectively. | |
1647 | * | |
1648 | * If a dentry was found and moved, then it is returned. Otherwise NULL | |
1649 | * is returned. This matches the expected return value of ->lookup. | |
1650 | * | |
1651 | */ | |
1652 | struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry) | |
1653 | { | |
1654 | struct dentry *new = NULL; | |
1655 | ||
a9049376 AV |
1656 | if (IS_ERR(inode)) |
1657 | return ERR_CAST(inode); | |
1658 | ||
21c0d8fd | 1659 | if (inode && S_ISDIR(inode->i_mode)) { |
873feea0 | 1660 | spin_lock(&inode->i_lock); |
32ba9c3f | 1661 | new = __d_find_alias(inode, 1); |
1da177e4 | 1662 | if (new) { |
32ba9c3f | 1663 | BUG_ON(!(new->d_flags & DCACHE_DISCONNECTED)); |
873feea0 | 1664 | spin_unlock(&inode->i_lock); |
1da177e4 | 1665 | security_d_instantiate(new, inode); |
1da177e4 LT |
1666 | d_move(new, dentry); |
1667 | iput(inode); | |
1668 | } else { | |
873feea0 | 1669 | /* already taking inode->i_lock, so d_add() by hand */ |
360da900 | 1670 | __d_instantiate(dentry, inode); |
873feea0 | 1671 | spin_unlock(&inode->i_lock); |
1da177e4 LT |
1672 | security_d_instantiate(dentry, inode); |
1673 | d_rehash(dentry); | |
1674 | } | |
1675 | } else | |
1676 | d_add(dentry, inode); | |
1677 | return new; | |
1678 | } | |
ec4f8605 | 1679 | EXPORT_SYMBOL(d_splice_alias); |
1da177e4 | 1680 | |
9403540c BN |
1681 | /** |
1682 | * d_add_ci - lookup or allocate new dentry with case-exact name | |
1683 | * @inode: the inode case-insensitive lookup has found | |
1684 | * @dentry: the negative dentry that was passed to the parent's lookup func | |
1685 | * @name: the case-exact name to be associated with the returned dentry | |
1686 | * | |
1687 | * This is to avoid filling the dcache with case-insensitive names to the | |
1688 | * same inode, only the actual correct case is stored in the dcache for | |
1689 | * case-insensitive filesystems. | |
1690 | * | |
1691 | * For a case-insensitive lookup match and if the the case-exact dentry | |
1692 | * already exists in in the dcache, use it and return it. | |
1693 | * | |
1694 | * If no entry exists with the exact case name, allocate new dentry with | |
1695 | * the exact case, and return the spliced entry. | |
1696 | */ | |
e45b590b | 1697 | struct dentry *d_add_ci(struct dentry *dentry, struct inode *inode, |
9403540c BN |
1698 | struct qstr *name) |
1699 | { | |
9403540c BN |
1700 | struct dentry *found; |
1701 | struct dentry *new; | |
1702 | ||
b6520c81 CH |
1703 | /* |
1704 | * First check if a dentry matching the name already exists, | |
1705 | * if not go ahead and create it now. | |
1706 | */ | |
9403540c | 1707 | found = d_hash_and_lookup(dentry->d_parent, name); |
4f522a24 AV |
1708 | if (unlikely(IS_ERR(found))) |
1709 | goto err_out; | |
9403540c BN |
1710 | if (!found) { |
1711 | new = d_alloc(dentry->d_parent, name); | |
1712 | if (!new) { | |
4f522a24 | 1713 | found = ERR_PTR(-ENOMEM); |
9403540c BN |
1714 | goto err_out; |
1715 | } | |
b6520c81 | 1716 | |
9403540c BN |
1717 | found = d_splice_alias(inode, new); |
1718 | if (found) { | |
1719 | dput(new); | |
1720 | return found; | |
1721 | } | |
1722 | return new; | |
1723 | } | |
b6520c81 CH |
1724 | |
1725 | /* | |
1726 | * If a matching dentry exists, and it's not negative use it. | |
1727 | * | |
1728 | * Decrement the reference count to balance the iget() done | |
1729 | * earlier on. | |
1730 | */ | |
9403540c BN |
1731 | if (found->d_inode) { |
1732 | if (unlikely(found->d_inode != inode)) { | |
1733 | /* This can't happen because bad inodes are unhashed. */ | |
1734 | BUG_ON(!is_bad_inode(inode)); | |
1735 | BUG_ON(!is_bad_inode(found->d_inode)); | |
1736 | } | |
9403540c BN |
1737 | iput(inode); |
1738 | return found; | |
1739 | } | |
b6520c81 | 1740 | |
9403540c | 1741 | /* |
9403540c | 1742 | * Negative dentry: instantiate it unless the inode is a directory and |
b6520c81 | 1743 | * already has a dentry. |
9403540c | 1744 | */ |
4513d899 AV |
1745 | new = d_splice_alias(inode, found); |
1746 | if (new) { | |
1747 | dput(found); | |
1748 | found = new; | |
9403540c | 1749 | } |
4513d899 | 1750 | return found; |
9403540c BN |
1751 | |
1752 | err_out: | |
1753 | iput(inode); | |
4f522a24 | 1754 | return found; |
9403540c | 1755 | } |
ec4f8605 | 1756 | EXPORT_SYMBOL(d_add_ci); |
1da177e4 | 1757 | |
12f8ad4b LT |
1758 | /* |
1759 | * Do the slow-case of the dentry name compare. | |
1760 | * | |
1761 | * Unlike the dentry_cmp() function, we need to atomically | |
1762 | * load the name, length and inode information, so that the | |
1763 | * filesystem can rely on them, and can use the 'name' and | |
1764 | * 'len' information without worrying about walking off the | |
1765 | * end of memory etc. | |
1766 | * | |
1767 | * Thus the read_seqcount_retry() and the "duplicate" info | |
1768 | * in arguments (the low-level filesystem should not look | |
1769 | * at the dentry inode or name contents directly, since | |
1770 | * rename can change them while we're in RCU mode). | |
1771 | */ | |
1772 | enum slow_d_compare { | |
1773 | D_COMP_OK, | |
1774 | D_COMP_NOMATCH, | |
1775 | D_COMP_SEQRETRY, | |
1776 | }; | |
1777 | ||
1778 | static noinline enum slow_d_compare slow_dentry_cmp( | |
1779 | const struct dentry *parent, | |
1780 | struct inode *inode, | |
1781 | struct dentry *dentry, | |
1782 | unsigned int seq, | |
1783 | const struct qstr *name) | |
1784 | { | |
1785 | int tlen = dentry->d_name.len; | |
1786 | const char *tname = dentry->d_name.name; | |
1787 | struct inode *i = dentry->d_inode; | |
1788 | ||
1789 | if (read_seqcount_retry(&dentry->d_seq, seq)) { | |
1790 | cpu_relax(); | |
1791 | return D_COMP_SEQRETRY; | |
1792 | } | |
1793 | if (parent->d_op->d_compare(parent, inode, | |
1794 | dentry, i, | |
1795 | tlen, tname, name)) | |
1796 | return D_COMP_NOMATCH; | |
1797 | return D_COMP_OK; | |
1798 | } | |
1799 | ||
31e6b01f NP |
1800 | /** |
1801 | * __d_lookup_rcu - search for a dentry (racy, store-free) | |
1802 | * @parent: parent dentry | |
1803 | * @name: qstr of name we wish to find | |
1f1e6e52 | 1804 | * @seqp: returns d_seq value at the point where the dentry was found |
31e6b01f NP |
1805 | * @inode: returns dentry->d_inode when the inode was found valid. |
1806 | * Returns: dentry, or NULL | |
1807 | * | |
1808 | * __d_lookup_rcu is the dcache lookup function for rcu-walk name | |
1809 | * resolution (store-free path walking) design described in | |
1810 | * Documentation/filesystems/path-lookup.txt. | |
1811 | * | |
1812 | * This is not to be used outside core vfs. | |
1813 | * | |
1814 | * __d_lookup_rcu must only be used in rcu-walk mode, ie. with vfsmount lock | |
1815 | * held, and rcu_read_lock held. The returned dentry must not be stored into | |
1816 | * without taking d_lock and checking d_seq sequence count against @seq | |
1817 | * returned here. | |
1818 | * | |
1819 | * A refcount may be taken on the found dentry with the __d_rcu_to_refcount | |
1820 | * function. | |
1821 | * | |
1822 | * Alternatively, __d_lookup_rcu may be called again to look up the child of | |
1823 | * the returned dentry, so long as its parent's seqlock is checked after the | |
1824 | * child is looked up. Thus, an interlocking stepping of sequence lock checks | |
1825 | * is formed, giving integrity down the path walk. | |
12f8ad4b LT |
1826 | * |
1827 | * NOTE! The caller *has* to check the resulting dentry against the sequence | |
1828 | * number we've returned before using any of the resulting dentry state! | |
31e6b01f | 1829 | */ |
8966be90 LT |
1830 | struct dentry *__d_lookup_rcu(const struct dentry *parent, |
1831 | const struct qstr *name, | |
12f8ad4b | 1832 | unsigned *seqp, struct inode *inode) |
31e6b01f | 1833 | { |
26fe5750 | 1834 | u64 hashlen = name->hash_len; |
31e6b01f | 1835 | const unsigned char *str = name->name; |
26fe5750 | 1836 | struct hlist_bl_head *b = d_hash(parent, hashlen_hash(hashlen)); |
ceb5bdc2 | 1837 | struct hlist_bl_node *node; |
31e6b01f NP |
1838 | struct dentry *dentry; |
1839 | ||
1840 | /* | |
1841 | * Note: There is significant duplication with __d_lookup_rcu which is | |
1842 | * required to prevent single threaded performance regressions | |
1843 | * especially on architectures where smp_rmb (in seqcounts) are costly. | |
1844 | * Keep the two functions in sync. | |
1845 | */ | |
1846 | ||
1847 | /* | |
1848 | * The hash list is protected using RCU. | |
1849 | * | |
1850 | * Carefully use d_seq when comparing a candidate dentry, to avoid | |
1851 | * races with d_move(). | |
1852 | * | |
1853 | * It is possible that concurrent renames can mess up our list | |
1854 | * walk here and result in missing our dentry, resulting in the | |
1855 | * false-negative result. d_lookup() protects against concurrent | |
1856 | * renames using rename_lock seqlock. | |
1857 | * | |
b0a4bb83 | 1858 | * See Documentation/filesystems/path-lookup.txt for more details. |
31e6b01f | 1859 | */ |
b07ad996 | 1860 | hlist_bl_for_each_entry_rcu(dentry, node, b, d_hash) { |
8966be90 | 1861 | unsigned seq; |
31e6b01f | 1862 | |
31e6b01f | 1863 | seqretry: |
12f8ad4b LT |
1864 | /* |
1865 | * The dentry sequence count protects us from concurrent | |
1866 | * renames, and thus protects inode, parent and name fields. | |
1867 | * | |
1868 | * The caller must perform a seqcount check in order | |
1869 | * to do anything useful with the returned dentry, | |
1870 | * including using the 'd_inode' pointer. | |
1871 | * | |
1872 | * NOTE! We do a "raw" seqcount_begin here. That means that | |
1873 | * we don't wait for the sequence count to stabilize if it | |
1874 | * is in the middle of a sequence change. If we do the slow | |
1875 | * dentry compare, we will do seqretries until it is stable, | |
1876 | * and if we end up with a successful lookup, we actually | |
1877 | * want to exit RCU lookup anyway. | |
1878 | */ | |
1879 | seq = raw_seqcount_begin(&dentry->d_seq); | |
31e6b01f NP |
1880 | if (dentry->d_parent != parent) |
1881 | continue; | |
2e321806 LT |
1882 | if (d_unhashed(dentry)) |
1883 | continue; | |
12f8ad4b LT |
1884 | *seqp = seq; |
1885 | ||
830c0f0e | 1886 | if (unlikely(parent->d_flags & DCACHE_OP_COMPARE)) { |
26fe5750 LT |
1887 | if (dentry->d_name.hash != hashlen_hash(hashlen)) |
1888 | continue; | |
12f8ad4b LT |
1889 | switch (slow_dentry_cmp(parent, inode, dentry, seq, name)) { |
1890 | case D_COMP_OK: | |
1891 | return dentry; | |
1892 | case D_COMP_NOMATCH: | |
31e6b01f | 1893 | continue; |
12f8ad4b LT |
1894 | default: |
1895 | goto seqretry; | |
1896 | } | |
31e6b01f | 1897 | } |
12f8ad4b | 1898 | |
26fe5750 | 1899 | if (dentry->d_name.hash_len != hashlen) |
ee983e89 | 1900 | continue; |
26fe5750 | 1901 | if (!dentry_cmp(dentry, str, hashlen_len(hashlen))) |
12f8ad4b | 1902 | return dentry; |
31e6b01f NP |
1903 | } |
1904 | return NULL; | |
1905 | } | |
1906 | ||
1da177e4 LT |
1907 | /** |
1908 | * d_lookup - search for a dentry | |
1909 | * @parent: parent dentry | |
1910 | * @name: qstr of name we wish to find | |
b04f784e | 1911 | * Returns: dentry, or NULL |
1da177e4 | 1912 | * |
b04f784e NP |
1913 | * d_lookup searches the children of the parent dentry for the name in |
1914 | * question. If the dentry is found its reference count is incremented and the | |
1915 | * dentry is returned. The caller must use dput to free the entry when it has | |
1916 | * finished using it. %NULL is returned if the dentry does not exist. | |
1da177e4 | 1917 | */ |
da2d8455 | 1918 | struct dentry *d_lookup(const struct dentry *parent, const struct qstr *name) |
1da177e4 | 1919 | { |
31e6b01f | 1920 | struct dentry *dentry; |
949854d0 | 1921 | unsigned seq; |
1da177e4 LT |
1922 | |
1923 | do { | |
1924 | seq = read_seqbegin(&rename_lock); | |
1925 | dentry = __d_lookup(parent, name); | |
1926 | if (dentry) | |
1927 | break; | |
1928 | } while (read_seqretry(&rename_lock, seq)); | |
1929 | return dentry; | |
1930 | } | |
ec4f8605 | 1931 | EXPORT_SYMBOL(d_lookup); |
1da177e4 | 1932 | |
31e6b01f | 1933 | /** |
b04f784e NP |
1934 | * __d_lookup - search for a dentry (racy) |
1935 | * @parent: parent dentry | |
1936 | * @name: qstr of name we wish to find | |
1937 | * Returns: dentry, or NULL | |
1938 | * | |
1939 | * __d_lookup is like d_lookup, however it may (rarely) return a | |
1940 | * false-negative result due to unrelated rename activity. | |
1941 | * | |
1942 | * __d_lookup is slightly faster by avoiding rename_lock read seqlock, | |
1943 | * however it must be used carefully, eg. with a following d_lookup in | |
1944 | * the case of failure. | |
1945 | * | |
1946 | * __d_lookup callers must be commented. | |
1947 | */ | |
a713ca2a | 1948 | struct dentry *__d_lookup(const struct dentry *parent, const struct qstr *name) |
1da177e4 LT |
1949 | { |
1950 | unsigned int len = name->len; | |
1951 | unsigned int hash = name->hash; | |
1952 | const unsigned char *str = name->name; | |
b07ad996 | 1953 | struct hlist_bl_head *b = d_hash(parent, hash); |
ceb5bdc2 | 1954 | struct hlist_bl_node *node; |
31e6b01f | 1955 | struct dentry *found = NULL; |
665a7583 | 1956 | struct dentry *dentry; |
1da177e4 | 1957 | |
31e6b01f NP |
1958 | /* |
1959 | * Note: There is significant duplication with __d_lookup_rcu which is | |
1960 | * required to prevent single threaded performance regressions | |
1961 | * especially on architectures where smp_rmb (in seqcounts) are costly. | |
1962 | * Keep the two functions in sync. | |
1963 | */ | |
1964 | ||
b04f784e NP |
1965 | /* |
1966 | * The hash list is protected using RCU. | |
1967 | * | |
1968 | * Take d_lock when comparing a candidate dentry, to avoid races | |
1969 | * with d_move(). | |
1970 | * | |
1971 | * It is possible that concurrent renames can mess up our list | |
1972 | * walk here and result in missing our dentry, resulting in the | |
1973 | * false-negative result. d_lookup() protects against concurrent | |
1974 | * renames using rename_lock seqlock. | |
1975 | * | |
b0a4bb83 | 1976 | * See Documentation/filesystems/path-lookup.txt for more details. |
b04f784e | 1977 | */ |
1da177e4 LT |
1978 | rcu_read_lock(); |
1979 | ||
b07ad996 | 1980 | hlist_bl_for_each_entry_rcu(dentry, node, b, d_hash) { |
1da177e4 | 1981 | |
1da177e4 LT |
1982 | if (dentry->d_name.hash != hash) |
1983 | continue; | |
1da177e4 LT |
1984 | |
1985 | spin_lock(&dentry->d_lock); | |
1da177e4 LT |
1986 | if (dentry->d_parent != parent) |
1987 | goto next; | |
d0185c08 LT |
1988 | if (d_unhashed(dentry)) |
1989 | goto next; | |
1990 | ||
1da177e4 LT |
1991 | /* |
1992 | * It is safe to compare names since d_move() cannot | |
1993 | * change the qstr (protected by d_lock). | |
1994 | */ | |
fb045adb | 1995 | if (parent->d_flags & DCACHE_OP_COMPARE) { |
12f8ad4b LT |
1996 | int tlen = dentry->d_name.len; |
1997 | const char *tname = dentry->d_name.name; | |
621e155a NP |
1998 | if (parent->d_op->d_compare(parent, parent->d_inode, |
1999 | dentry, dentry->d_inode, | |
31e6b01f | 2000 | tlen, tname, name)) |
1da177e4 LT |
2001 | goto next; |
2002 | } else { | |
ee983e89 LT |
2003 | if (dentry->d_name.len != len) |
2004 | goto next; | |
12f8ad4b | 2005 | if (dentry_cmp(dentry, str, len)) |
1da177e4 LT |
2006 | goto next; |
2007 | } | |
2008 | ||
b7ab39f6 | 2009 | dentry->d_count++; |
d0185c08 | 2010 | found = dentry; |
1da177e4 LT |
2011 | spin_unlock(&dentry->d_lock); |
2012 | break; | |
2013 | next: | |
2014 | spin_unlock(&dentry->d_lock); | |
2015 | } | |
2016 | rcu_read_unlock(); | |
2017 | ||
2018 | return found; | |
2019 | } | |
2020 | ||
3e7e241f EB |
2021 | /** |
2022 | * d_hash_and_lookup - hash the qstr then search for a dentry | |
2023 | * @dir: Directory to search in | |
2024 | * @name: qstr of name we wish to find | |
2025 | * | |
4f522a24 | 2026 | * On lookup failure NULL is returned; on bad name - ERR_PTR(-error) |
3e7e241f EB |
2027 | */ |
2028 | struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name) | |
2029 | { | |
3e7e241f EB |
2030 | /* |
2031 | * Check for a fs-specific hash function. Note that we must | |
2032 | * calculate the standard hash first, as the d_op->d_hash() | |
2033 | * routine may choose to leave the hash value unchanged. | |
2034 | */ | |
2035 | name->hash = full_name_hash(name->name, name->len); | |
fb045adb | 2036 | if (dir->d_flags & DCACHE_OP_HASH) { |
4f522a24 AV |
2037 | int err = dir->d_op->d_hash(dir, dir->d_inode, name); |
2038 | if (unlikely(err < 0)) | |
2039 | return ERR_PTR(err); | |
3e7e241f | 2040 | } |
4f522a24 | 2041 | return d_lookup(dir, name); |
3e7e241f | 2042 | } |
4f522a24 | 2043 | EXPORT_SYMBOL(d_hash_and_lookup); |
3e7e241f | 2044 | |
1da177e4 | 2045 | /** |
786a5e15 | 2046 | * d_validate - verify dentry provided from insecure source (deprecated) |
1da177e4 | 2047 | * @dentry: The dentry alleged to be valid child of @dparent |
ff5fdb61 | 2048 | * @dparent: The parent dentry (known to be valid) |
1da177e4 LT |
2049 | * |
2050 | * An insecure source has sent us a dentry, here we verify it and dget() it. | |
2051 | * This is used by ncpfs in its readdir implementation. | |
2052 | * Zero is returned in the dentry is invalid. | |
786a5e15 NP |
2053 | * |
2054 | * This function is slow for big directories, and deprecated, do not use it. | |
1da177e4 | 2055 | */ |
d3a23e16 | 2056 | int d_validate(struct dentry *dentry, struct dentry *dparent) |
1da177e4 | 2057 | { |
786a5e15 | 2058 | struct dentry *child; |
d3a23e16 | 2059 | |
2fd6b7f5 | 2060 | spin_lock(&dparent->d_lock); |
d76c446b | 2061 | list_for_each_entry(child, &dparent->d_subdirs, d_child) { |
786a5e15 | 2062 | if (dentry == child) { |
2fd6b7f5 | 2063 | spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); |
dc0474be | 2064 | __dget_dlock(dentry); |
2fd6b7f5 NP |
2065 | spin_unlock(&dentry->d_lock); |
2066 | spin_unlock(&dparent->d_lock); | |
1da177e4 LT |
2067 | return 1; |
2068 | } | |
2069 | } | |
2fd6b7f5 | 2070 | spin_unlock(&dparent->d_lock); |
786a5e15 | 2071 | |
1da177e4 LT |
2072 | return 0; |
2073 | } | |
ec4f8605 | 2074 | EXPORT_SYMBOL(d_validate); |
1da177e4 LT |
2075 | |
2076 | /* | |
2077 | * When a file is deleted, we have two options: | |
2078 | * - turn this dentry into a negative dentry | |
2079 | * - unhash this dentry and free it. | |
2080 | * | |
2081 | * Usually, we want to just turn this into | |
2082 | * a negative dentry, but if anybody else is | |
2083 | * currently using the dentry or the inode | |
2084 | * we can't do that and we fall back on removing | |
2085 | * it from the hash queues and waiting for | |
2086 | * it to be deleted later when it has no users | |
2087 | */ | |
2088 | ||
2089 | /** | |
2090 | * d_delete - delete a dentry | |
2091 | * @dentry: The dentry to delete | |
2092 | * | |
2093 | * Turn the dentry into a negative dentry if possible, otherwise | |
2094 | * remove it from the hash queues so it can be deleted later | |
2095 | */ | |
2096 | ||
2097 | void d_delete(struct dentry * dentry) | |
2098 | { | |
873feea0 | 2099 | struct inode *inode; |
7a91bf7f | 2100 | int isdir = 0; |
1da177e4 LT |
2101 | /* |
2102 | * Are we the only user? | |
2103 | */ | |
357f8e65 | 2104 | again: |
1da177e4 | 2105 | spin_lock(&dentry->d_lock); |
873feea0 NP |
2106 | inode = dentry->d_inode; |
2107 | isdir = S_ISDIR(inode->i_mode); | |
b7ab39f6 | 2108 | if (dentry->d_count == 1) { |
1fe0c023 | 2109 | if (!spin_trylock(&inode->i_lock)) { |
357f8e65 NP |
2110 | spin_unlock(&dentry->d_lock); |
2111 | cpu_relax(); | |
2112 | goto again; | |
2113 | } | |
13e3c5e5 | 2114 | dentry->d_flags &= ~DCACHE_CANT_MOUNT; |
31e6b01f | 2115 | dentry_unlink_inode(dentry); |
7a91bf7f | 2116 | fsnotify_nameremove(dentry, isdir); |
1da177e4 LT |
2117 | return; |
2118 | } | |
2119 | ||
2120 | if (!d_unhashed(dentry)) | |
2121 | __d_drop(dentry); | |
2122 | ||
2123 | spin_unlock(&dentry->d_lock); | |
7a91bf7f JM |
2124 | |
2125 | fsnotify_nameremove(dentry, isdir); | |
1da177e4 | 2126 | } |
ec4f8605 | 2127 | EXPORT_SYMBOL(d_delete); |
1da177e4 | 2128 | |
b07ad996 | 2129 | static void __d_rehash(struct dentry * entry, struct hlist_bl_head *b) |
1da177e4 | 2130 | { |
ceb5bdc2 | 2131 | BUG_ON(!d_unhashed(entry)); |
1879fd6a | 2132 | hlist_bl_lock(b); |
dea3667b | 2133 | entry->d_flags |= DCACHE_RCUACCESS; |
b07ad996 | 2134 | hlist_bl_add_head_rcu(&entry->d_hash, b); |
1879fd6a | 2135 | hlist_bl_unlock(b); |
1da177e4 LT |
2136 | } |
2137 | ||
770bfad8 DH |
2138 | static void _d_rehash(struct dentry * entry) |
2139 | { | |
2140 | __d_rehash(entry, d_hash(entry->d_parent, entry->d_name.hash)); | |
2141 | } | |
2142 | ||
1da177e4 LT |
2143 | /** |
2144 | * d_rehash - add an entry back to the hash | |
2145 | * @entry: dentry to add to the hash | |
2146 | * | |
2147 | * Adds a dentry to the hash according to its name. | |
2148 | */ | |
2149 | ||
2150 | void d_rehash(struct dentry * entry) | |
2151 | { | |
1da177e4 | 2152 | spin_lock(&entry->d_lock); |
770bfad8 | 2153 | _d_rehash(entry); |
1da177e4 | 2154 | spin_unlock(&entry->d_lock); |
1da177e4 | 2155 | } |
ec4f8605 | 2156 | EXPORT_SYMBOL(d_rehash); |
1da177e4 | 2157 | |
fb2d5b86 NP |
2158 | /** |
2159 | * dentry_update_name_case - update case insensitive dentry with a new name | |
2160 | * @dentry: dentry to be updated | |
2161 | * @name: new name | |
2162 | * | |
2163 | * Update a case insensitive dentry with new case of name. | |
2164 | * | |
2165 | * dentry must have been returned by d_lookup with name @name. Old and new | |
2166 | * name lengths must match (ie. no d_compare which allows mismatched name | |
2167 | * lengths). | |
2168 | * | |
2169 | * Parent inode i_mutex must be held over d_lookup and into this call (to | |
2170 | * keep renames and concurrent inserts, and readdir(2) away). | |
2171 | */ | |
2172 | void dentry_update_name_case(struct dentry *dentry, struct qstr *name) | |
2173 | { | |
7ebfa57f | 2174 | BUG_ON(!mutex_is_locked(&dentry->d_parent->d_inode->i_mutex)); |
fb2d5b86 NP |
2175 | BUG_ON(dentry->d_name.len != name->len); /* d_lookup gives this */ |
2176 | ||
fb2d5b86 | 2177 | spin_lock(&dentry->d_lock); |
31e6b01f | 2178 | write_seqcount_begin(&dentry->d_seq); |
fb2d5b86 | 2179 | memcpy((unsigned char *)dentry->d_name.name, name->name, name->len); |
31e6b01f | 2180 | write_seqcount_end(&dentry->d_seq); |
fb2d5b86 | 2181 | spin_unlock(&dentry->d_lock); |
fb2d5b86 NP |
2182 | } |
2183 | EXPORT_SYMBOL(dentry_update_name_case); | |
2184 | ||
1da177e4 LT |
2185 | static void switch_names(struct dentry *dentry, struct dentry *target) |
2186 | { | |
2187 | if (dname_external(target)) { | |
2188 | if (dname_external(dentry)) { | |
2189 | /* | |
2190 | * Both external: swap the pointers | |
2191 | */ | |
9a8d5bb4 | 2192 | swap(target->d_name.name, dentry->d_name.name); |
1da177e4 LT |
2193 | } else { |
2194 | /* | |
2195 | * dentry:internal, target:external. Steal target's | |
2196 | * storage and make target internal. | |
2197 | */ | |
321bcf92 BF |
2198 | memcpy(target->d_iname, dentry->d_name.name, |
2199 | dentry->d_name.len + 1); | |
1da177e4 LT |
2200 | dentry->d_name.name = target->d_name.name; |
2201 | target->d_name.name = target->d_iname; | |
2202 | } | |
2203 | } else { | |
2204 | if (dname_external(dentry)) { | |
2205 | /* | |
2206 | * dentry:external, target:internal. Give dentry's | |
2207 | * storage to target and make dentry internal | |
2208 | */ | |
2209 | memcpy(dentry->d_iname, target->d_name.name, | |
2210 | target->d_name.len + 1); | |
2211 | target->d_name.name = dentry->d_name.name; | |
2212 | dentry->d_name.name = dentry->d_iname; | |
2213 | } else { | |
2214 | /* | |
2215 | * Both are internal. Just copy target to dentry | |
2216 | */ | |
2217 | memcpy(dentry->d_iname, target->d_name.name, | |
2218 | target->d_name.len + 1); | |
dc711ca3 AV |
2219 | dentry->d_name.len = target->d_name.len; |
2220 | return; | |
1da177e4 LT |
2221 | } |
2222 | } | |
9a8d5bb4 | 2223 | swap(dentry->d_name.len, target->d_name.len); |
1da177e4 LT |
2224 | } |
2225 | ||
2fd6b7f5 NP |
2226 | static void dentry_lock_for_move(struct dentry *dentry, struct dentry *target) |
2227 | { | |
2228 | /* | |
2229 | * XXXX: do we really need to take target->d_lock? | |
2230 | */ | |
2231 | if (IS_ROOT(dentry) || dentry->d_parent == target->d_parent) | |
2232 | spin_lock(&target->d_parent->d_lock); | |
2233 | else { | |
2234 | if (d_ancestor(dentry->d_parent, target->d_parent)) { | |
2235 | spin_lock(&dentry->d_parent->d_lock); | |
2236 | spin_lock_nested(&target->d_parent->d_lock, | |
2237 | DENTRY_D_LOCK_NESTED); | |
2238 | } else { | |
2239 | spin_lock(&target->d_parent->d_lock); | |
2240 | spin_lock_nested(&dentry->d_parent->d_lock, | |
2241 | DENTRY_D_LOCK_NESTED); | |
2242 | } | |
2243 | } | |
2244 | if (target < dentry) { | |
2245 | spin_lock_nested(&target->d_lock, 2); | |
2246 | spin_lock_nested(&dentry->d_lock, 3); | |
2247 | } else { | |
2248 | spin_lock_nested(&dentry->d_lock, 2); | |
2249 | spin_lock_nested(&target->d_lock, 3); | |
2250 | } | |
2251 | } | |
2252 | ||
2253 | static void dentry_unlock_parents_for_move(struct dentry *dentry, | |
2254 | struct dentry *target) | |
2255 | { | |
2256 | if (target->d_parent != dentry->d_parent) | |
2257 | spin_unlock(&dentry->d_parent->d_lock); | |
2258 | if (target->d_parent != target) | |
2259 | spin_unlock(&target->d_parent->d_lock); | |
2260 | } | |
2261 | ||
1da177e4 | 2262 | /* |
2fd6b7f5 NP |
2263 | * When switching names, the actual string doesn't strictly have to |
2264 | * be preserved in the target - because we're dropping the target | |
2265 | * anyway. As such, we can just do a simple memcpy() to copy over | |
2266 | * the new name before we switch. | |
2267 | * | |
2268 | * Note that we have to be a lot more careful about getting the hash | |
2269 | * switched - we have to switch the hash value properly even if it | |
2270 | * then no longer matches the actual (corrupted) string of the target. | |
2271 | * The hash value has to match the hash queue that the dentry is on.. | |
1da177e4 | 2272 | */ |
9eaef27b | 2273 | /* |
18367501 | 2274 | * __d_move - move a dentry |
1da177e4 LT |
2275 | * @dentry: entry to move |
2276 | * @target: new dentry | |
2277 | * | |
2278 | * Update the dcache to reflect the move of a file name. Negative | |
c46c8877 JL |
2279 | * dcache entries should not be moved in this way. Caller must hold |
2280 | * rename_lock, the i_mutex of the source and target directories, | |
2281 | * and the sb->s_vfs_rename_mutex if they differ. See lock_rename(). | |
1da177e4 | 2282 | */ |
18367501 | 2283 | static void __d_move(struct dentry * dentry, struct dentry * target) |
1da177e4 | 2284 | { |
1da177e4 LT |
2285 | if (!dentry->d_inode) |
2286 | printk(KERN_WARNING "VFS: moving negative dcache entry\n"); | |
2287 | ||
2fd6b7f5 NP |
2288 | BUG_ON(d_ancestor(dentry, target)); |
2289 | BUG_ON(d_ancestor(target, dentry)); | |
2290 | ||
2fd6b7f5 | 2291 | dentry_lock_for_move(dentry, target); |
1da177e4 | 2292 | |
31e6b01f NP |
2293 | write_seqcount_begin(&dentry->d_seq); |
2294 | write_seqcount_begin(&target->d_seq); | |
2295 | ||
ceb5bdc2 NP |
2296 | /* __d_drop does write_seqcount_barrier, but they're OK to nest. */ |
2297 | ||
2298 | /* | |
2299 | * Move the dentry to the target hash queue. Don't bother checking | |
2300 | * for the same hash queue because of how unlikely it is. | |
2301 | */ | |
2302 | __d_drop(dentry); | |
789680d1 | 2303 | __d_rehash(dentry, d_hash(target->d_parent, target->d_name.hash)); |
1da177e4 LT |
2304 | |
2305 | /* Unhash the target: dput() will then get rid of it */ | |
2306 | __d_drop(target); | |
2307 | ||
d76c446b AV |
2308 | list_del(&dentry->d_child); |
2309 | list_del(&target->d_child); | |
1da177e4 LT |
2310 | |
2311 | /* Switch the names.. */ | |
2312 | switch_names(dentry, target); | |
9a8d5bb4 | 2313 | swap(dentry->d_name.hash, target->d_name.hash); |
1da177e4 LT |
2314 | |
2315 | /* ... and switch the parents */ | |
2316 | if (IS_ROOT(dentry)) { | |
2317 | dentry->d_parent = target->d_parent; | |
2318 | target->d_parent = target; | |
d76c446b | 2319 | INIT_LIST_HEAD(&target->d_child); |
1da177e4 | 2320 | } else { |
9a8d5bb4 | 2321 | swap(dentry->d_parent, target->d_parent); |
1da177e4 LT |
2322 | |
2323 | /* And add them back to the (new) parent lists */ | |
d76c446b | 2324 | list_add(&target->d_child, &target->d_parent->d_subdirs); |
1da177e4 LT |
2325 | } |
2326 | ||
d76c446b | 2327 | list_add(&dentry->d_child, &dentry->d_parent->d_subdirs); |
2fd6b7f5 | 2328 | |
31e6b01f NP |
2329 | write_seqcount_end(&target->d_seq); |
2330 | write_seqcount_end(&dentry->d_seq); | |
2331 | ||
2fd6b7f5 | 2332 | dentry_unlock_parents_for_move(dentry, target); |
1da177e4 | 2333 | spin_unlock(&target->d_lock); |
c32ccd87 | 2334 | fsnotify_d_move(dentry); |
1da177e4 | 2335 | spin_unlock(&dentry->d_lock); |
18367501 AV |
2336 | } |
2337 | ||
2338 | /* | |
2339 | * d_move - move a dentry | |
2340 | * @dentry: entry to move | |
2341 | * @target: new dentry | |
2342 | * | |
2343 | * Update the dcache to reflect the move of a file name. Negative | |
c46c8877 JL |
2344 | * dcache entries should not be moved in this way. See the locking |
2345 | * requirements for __d_move. | |
18367501 AV |
2346 | */ |
2347 | void d_move(struct dentry *dentry, struct dentry *target) | |
2348 | { | |
2349 | write_seqlock(&rename_lock); | |
2350 | __d_move(dentry, target); | |
1da177e4 | 2351 | write_sequnlock(&rename_lock); |
9eaef27b | 2352 | } |
ec4f8605 | 2353 | EXPORT_SYMBOL(d_move); |
1da177e4 | 2354 | |
e2761a11 OH |
2355 | /** |
2356 | * d_ancestor - search for an ancestor | |
2357 | * @p1: ancestor dentry | |
2358 | * @p2: child dentry | |
2359 | * | |
2360 | * Returns the ancestor dentry of p2 which is a child of p1, if p1 is | |
2361 | * an ancestor of p2, else NULL. | |
9eaef27b | 2362 | */ |
e2761a11 | 2363 | struct dentry *d_ancestor(struct dentry *p1, struct dentry *p2) |
9eaef27b TM |
2364 | { |
2365 | struct dentry *p; | |
2366 | ||
871c0067 | 2367 | for (p = p2; !IS_ROOT(p); p = p->d_parent) { |
9eaef27b | 2368 | if (p->d_parent == p1) |
e2761a11 | 2369 | return p; |
9eaef27b | 2370 | } |
e2761a11 | 2371 | return NULL; |
9eaef27b TM |
2372 | } |
2373 | ||
2374 | /* | |
2375 | * This helper attempts to cope with remotely renamed directories | |
2376 | * | |
2377 | * It assumes that the caller is already holding | |
18367501 | 2378 | * dentry->d_parent->d_inode->i_mutex, inode->i_lock and rename_lock |
9eaef27b TM |
2379 | * |
2380 | * Note: If ever the locking in lock_rename() changes, then please | |
2381 | * remember to update this too... | |
9eaef27b | 2382 | */ |
873feea0 NP |
2383 | static struct dentry *__d_unalias(struct inode *inode, |
2384 | struct dentry *dentry, struct dentry *alias) | |
9eaef27b TM |
2385 | { |
2386 | struct mutex *m1 = NULL, *m2 = NULL; | |
ee3efa91 | 2387 | struct dentry *ret = ERR_PTR(-EBUSY); |
9eaef27b TM |
2388 | |
2389 | /* If alias and dentry share a parent, then no extra locks required */ | |
2390 | if (alias->d_parent == dentry->d_parent) | |
2391 | goto out_unalias; | |
2392 | ||
9eaef27b | 2393 | /* See lock_rename() */ |
9eaef27b TM |
2394 | if (!mutex_trylock(&dentry->d_sb->s_vfs_rename_mutex)) |
2395 | goto out_err; | |
2396 | m1 = &dentry->d_sb->s_vfs_rename_mutex; | |
2397 | if (!mutex_trylock(&alias->d_parent->d_inode->i_mutex)) | |
2398 | goto out_err; | |
2399 | m2 = &alias->d_parent->d_inode->i_mutex; | |
2400 | out_unalias: | |
ee3efa91 AV |
2401 | if (likely(!d_mountpoint(alias))) { |
2402 | __d_move(alias, dentry); | |
2403 | ret = alias; | |
2404 | } | |
9eaef27b | 2405 | out_err: |
873feea0 | 2406 | spin_unlock(&inode->i_lock); |
9eaef27b TM |
2407 | if (m2) |
2408 | mutex_unlock(m2); | |
2409 | if (m1) | |
2410 | mutex_unlock(m1); | |
2411 | return ret; | |
2412 | } | |
2413 | ||
770bfad8 DH |
2414 | /* |
2415 | * Prepare an anonymous dentry for life in the superblock's dentry tree as a | |
2416 | * named dentry in place of the dentry to be replaced. | |
2fd6b7f5 | 2417 | * returns with anon->d_lock held! |
770bfad8 DH |
2418 | */ |
2419 | static void __d_materialise_dentry(struct dentry *dentry, struct dentry *anon) | |
2420 | { | |
740da42e | 2421 | struct dentry *dparent; |
770bfad8 | 2422 | |
2fd6b7f5 | 2423 | dentry_lock_for_move(anon, dentry); |
770bfad8 | 2424 | |
31e6b01f NP |
2425 | write_seqcount_begin(&dentry->d_seq); |
2426 | write_seqcount_begin(&anon->d_seq); | |
2427 | ||
770bfad8 | 2428 | dparent = dentry->d_parent; |
770bfad8 | 2429 | |
2fd6b7f5 NP |
2430 | switch_names(dentry, anon); |
2431 | swap(dentry->d_name.hash, anon->d_name.hash); | |
2432 | ||
740da42e | 2433 | dentry->d_parent = dentry; |
d76c446b | 2434 | list_del_init(&dentry->d_child); |
740da42e | 2435 | anon->d_parent = dparent; |
c2ed8c8e AV |
2436 | if (likely(!d_unhashed(anon))) { |
2437 | hlist_bl_lock(&anon->d_sb->s_anon); | |
2438 | __hlist_bl_del(&anon->d_hash); | |
2439 | anon->d_hash.pprev = NULL; | |
2440 | hlist_bl_unlock(&anon->d_sb->s_anon); | |
2441 | } | |
d76c446b | 2442 | list_move(&anon->d_child, &dparent->d_subdirs); |
770bfad8 | 2443 | |
31e6b01f NP |
2444 | write_seqcount_end(&dentry->d_seq); |
2445 | write_seqcount_end(&anon->d_seq); | |
2446 | ||
2fd6b7f5 NP |
2447 | dentry_unlock_parents_for_move(anon, dentry); |
2448 | spin_unlock(&dentry->d_lock); | |
2449 | ||
2450 | /* anon->d_lock still locked, returns locked */ | |
770bfad8 DH |
2451 | anon->d_flags &= ~DCACHE_DISCONNECTED; |
2452 | } | |
2453 | ||
2454 | /** | |
2455 | * d_materialise_unique - introduce an inode into the tree | |
2456 | * @dentry: candidate dentry | |
2457 | * @inode: inode to bind to the dentry, to which aliases may be attached | |
2458 | * | |
2459 | * Introduces an dentry into the tree, substituting an extant disconnected | |
c46c8877 JL |
2460 | * root directory alias in its place if there is one. Caller must hold the |
2461 | * i_mutex of the parent directory. | |
770bfad8 DH |
2462 | */ |
2463 | struct dentry *d_materialise_unique(struct dentry *dentry, struct inode *inode) | |
2464 | { | |
9eaef27b | 2465 | struct dentry *actual; |
770bfad8 DH |
2466 | |
2467 | BUG_ON(!d_unhashed(dentry)); | |
2468 | ||
770bfad8 DH |
2469 | if (!inode) { |
2470 | actual = dentry; | |
360da900 | 2471 | __d_instantiate(dentry, NULL); |
357f8e65 NP |
2472 | d_rehash(actual); |
2473 | goto out_nolock; | |
770bfad8 DH |
2474 | } |
2475 | ||
873feea0 | 2476 | spin_lock(&inode->i_lock); |
357f8e65 | 2477 | |
9eaef27b TM |
2478 | if (S_ISDIR(inode->i_mode)) { |
2479 | struct dentry *alias; | |
2480 | ||
2481 | /* Does an aliased dentry already exist? */ | |
32ba9c3f | 2482 | alias = __d_find_alias(inode, 0); |
9eaef27b TM |
2483 | if (alias) { |
2484 | actual = alias; | |
18367501 AV |
2485 | write_seqlock(&rename_lock); |
2486 | ||
2487 | if (d_ancestor(alias, dentry)) { | |
2488 | /* Check for loops */ | |
2489 | actual = ERR_PTR(-ELOOP); | |
b18dafc8 | 2490 | spin_unlock(&inode->i_lock); |
18367501 AV |
2491 | } else if (IS_ROOT(alias)) { |
2492 | /* Is this an anonymous mountpoint that we | |
2493 | * could splice into our tree? */ | |
9eaef27b | 2494 | __d_materialise_dentry(dentry, alias); |
18367501 | 2495 | write_sequnlock(&rename_lock); |
9eaef27b | 2496 | goto found; |
18367501 AV |
2497 | } else { |
2498 | /* Nope, but we must(!) avoid directory | |
b18dafc8 | 2499 | * aliasing. This drops inode->i_lock */ |
18367501 | 2500 | actual = __d_unalias(inode, dentry, alias); |
9eaef27b | 2501 | } |
18367501 | 2502 | write_sequnlock(&rename_lock); |
dd179946 DH |
2503 | if (IS_ERR(actual)) { |
2504 | if (PTR_ERR(actual) == -ELOOP) | |
2505 | pr_warn_ratelimited( | |
2506 | "VFS: Lookup of '%s' in %s %s" | |
2507 | " would have caused loop\n", | |
2508 | dentry->d_name.name, | |
2509 | inode->i_sb->s_type->name, | |
2510 | inode->i_sb->s_id); | |
9eaef27b | 2511 | dput(alias); |
dd179946 | 2512 | } |
9eaef27b TM |
2513 | goto out_nolock; |
2514 | } | |
770bfad8 DH |
2515 | } |
2516 | ||
2517 | /* Add a unique reference */ | |
2518 | actual = __d_instantiate_unique(dentry, inode); | |
2519 | if (!actual) | |
2520 | actual = dentry; | |
357f8e65 NP |
2521 | else |
2522 | BUG_ON(!d_unhashed(actual)); | |
770bfad8 | 2523 | |
770bfad8 DH |
2524 | spin_lock(&actual->d_lock); |
2525 | found: | |
2526 | _d_rehash(actual); | |
2527 | spin_unlock(&actual->d_lock); | |
873feea0 | 2528 | spin_unlock(&inode->i_lock); |
9eaef27b | 2529 | out_nolock: |
770bfad8 DH |
2530 | if (actual == dentry) { |
2531 | security_d_instantiate(dentry, inode); | |
2532 | return NULL; | |
2533 | } | |
2534 | ||
2535 | iput(inode); | |
2536 | return actual; | |
770bfad8 | 2537 | } |
ec4f8605 | 2538 | EXPORT_SYMBOL_GPL(d_materialise_unique); |
770bfad8 | 2539 | |
cdd16d02 | 2540 | static int prepend(char **buffer, int *buflen, const char *str, int namelen) |
6092d048 RP |
2541 | { |
2542 | *buflen -= namelen; | |
2543 | if (*buflen < 0) | |
2544 | return -ENAMETOOLONG; | |
2545 | *buffer -= namelen; | |
2546 | memcpy(*buffer, str, namelen); | |
2547 | return 0; | |
2548 | } | |
2549 | ||
cdd16d02 MS |
2550 | static int prepend_name(char **buffer, int *buflen, struct qstr *name) |
2551 | { | |
2552 | return prepend(buffer, buflen, name->name, name->len); | |
2553 | } | |
2554 | ||
1da177e4 | 2555 | /** |
208898c1 | 2556 | * prepend_path - Prepend path string to a buffer |
9d1bc601 | 2557 | * @path: the dentry/vfsmount to report |
02125a82 | 2558 | * @root: root vfsmnt/dentry |
f2eb6575 MS |
2559 | * @buffer: pointer to the end of the buffer |
2560 | * @buflen: pointer to buffer length | |
552ce544 | 2561 | * |
949854d0 | 2562 | * Caller holds the rename_lock. |
1da177e4 | 2563 | */ |
02125a82 AV |
2564 | static int prepend_path(const struct path *path, |
2565 | const struct path *root, | |
f2eb6575 | 2566 | char **buffer, int *buflen) |
1da177e4 | 2567 | { |
9d1bc601 MS |
2568 | struct dentry *dentry = path->dentry; |
2569 | struct vfsmount *vfsmnt = path->mnt; | |
0714a533 | 2570 | struct mount *mnt = real_mount(vfsmnt); |
3c2a0909 S |
2571 | char *orig_buffer = *buffer; |
2572 | int orig_len = *buflen; | |
f2eb6575 MS |
2573 | bool slash = false; |
2574 | int error = 0; | |
6092d048 | 2575 | |
f2eb6575 | 2576 | while (dentry != root->dentry || vfsmnt != root->mnt) { |
1da177e4 LT |
2577 | struct dentry * parent; |
2578 | ||
1da177e4 | 2579 | if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) { |
3c2a0909 S |
2580 | /* Escaped? */ |
2581 | if (dentry != vfsmnt->mnt_root) { | |
2582 | *buffer = orig_buffer; | |
2583 | *buflen = orig_len; | |
2584 | slash = false; | |
2585 | error = 3; | |
2586 | goto global_root; | |
2587 | } | |
552ce544 | 2588 | /* Global root? */ |
676da58d | 2589 | if (!mnt_has_parent(mnt)) |
1da177e4 | 2590 | goto global_root; |
a73324da | 2591 | dentry = mnt->mnt_mountpoint; |
0714a533 AV |
2592 | mnt = mnt->mnt_parent; |
2593 | vfsmnt = &mnt->mnt; | |
1da177e4 LT |
2594 | continue; |
2595 | } | |
2596 | parent = dentry->d_parent; | |
2597 | prefetch(parent); | |
9abca360 | 2598 | spin_lock(&dentry->d_lock); |
f2eb6575 | 2599 | error = prepend_name(buffer, buflen, &dentry->d_name); |
9abca360 | 2600 | spin_unlock(&dentry->d_lock); |
f2eb6575 MS |
2601 | if (!error) |
2602 | error = prepend(buffer, buflen, "/", 1); | |
2603 | if (error) | |
2604 | break; | |
2605 | ||
2606 | slash = true; | |
1da177e4 LT |
2607 | dentry = parent; |
2608 | } | |
2609 | ||
f2eb6575 MS |
2610 | if (!error && !slash) |
2611 | error = prepend(buffer, buflen, "/", 1); | |
2612 | ||
f2eb6575 | 2613 | return error; |
1da177e4 LT |
2614 | |
2615 | global_root: | |
98dc568b MS |
2616 | /* |
2617 | * Filesystems needing to implement special "root names" | |
2618 | * should do so with ->d_dname() | |
2619 | */ | |
2620 | if (IS_ROOT(dentry) && | |
2621 | (dentry->d_name.len != 1 || dentry->d_name.name[0] != '/')) { | |
2622 | WARN(1, "Root dentry has weird name <%.*s>\n", | |
2623 | (int) dentry->d_name.len, dentry->d_name.name); | |
2624 | } | |
02125a82 AV |
2625 | if (!slash) |
2626 | error = prepend(buffer, buflen, "/", 1); | |
2627 | if (!error) | |
f7a99c5b | 2628 | error = is_mounted(vfsmnt) ? 1 : 2; |
7ea600b5 | 2629 | return error; |
f2eb6575 | 2630 | } |
be285c71 | 2631 | |
f2eb6575 MS |
2632 | /** |
2633 | * __d_path - return the path of a dentry | |
2634 | * @path: the dentry/vfsmount to report | |
02125a82 | 2635 | * @root: root vfsmnt/dentry |
cd956a1c | 2636 | * @buf: buffer to return value in |
f2eb6575 MS |
2637 | * @buflen: buffer length |
2638 | * | |
ffd1f4ed | 2639 | * Convert a dentry into an ASCII path name. |
f2eb6575 MS |
2640 | * |
2641 | * Returns a pointer into the buffer or an error code if the | |
2642 | * path was too long. | |
2643 | * | |
be148247 | 2644 | * "buflen" should be positive. |
f2eb6575 | 2645 | * |
02125a82 | 2646 | * If the path is not reachable from the supplied root, return %NULL. |
f2eb6575 | 2647 | */ |
02125a82 AV |
2648 | char *__d_path(const struct path *path, |
2649 | const struct path *root, | |
f2eb6575 MS |
2650 | char *buf, int buflen) |
2651 | { | |
2652 | char *res = buf + buflen; | |
2653 | int error; | |
2654 | ||
2655 | prepend(&res, &buflen, "\0", 1); | |
7ea600b5 | 2656 | br_read_lock(&vfsmount_lock); |
949854d0 | 2657 | write_seqlock(&rename_lock); |
f2eb6575 | 2658 | error = prepend_path(path, root, &res, &buflen); |
949854d0 | 2659 | write_sequnlock(&rename_lock); |
7ea600b5 | 2660 | br_read_unlock(&vfsmount_lock); |
be148247 | 2661 | |
02125a82 AV |
2662 | if (error < 0) |
2663 | return ERR_PTR(error); | |
2664 | if (error > 0) | |
2665 | return NULL; | |
2666 | return res; | |
2667 | } | |
2668 | ||
2669 | char *d_absolute_path(const struct path *path, | |
2670 | char *buf, int buflen) | |
2671 | { | |
2672 | struct path root = {}; | |
2673 | char *res = buf + buflen; | |
2674 | int error; | |
2675 | ||
2676 | prepend(&res, &buflen, "\0", 1); | |
7ea600b5 | 2677 | br_read_lock(&vfsmount_lock); |
02125a82 AV |
2678 | write_seqlock(&rename_lock); |
2679 | error = prepend_path(path, &root, &res, &buflen); | |
2680 | write_sequnlock(&rename_lock); | |
7ea600b5 | 2681 | br_read_unlock(&vfsmount_lock); |
02125a82 AV |
2682 | |
2683 | if (error > 1) | |
2684 | error = -EINVAL; | |
2685 | if (error < 0) | |
f2eb6575 | 2686 | return ERR_PTR(error); |
f2eb6575 | 2687 | return res; |
1da177e4 LT |
2688 | } |
2689 | ||
ffd1f4ed MS |
2690 | /* |
2691 | * same as __d_path but appends "(deleted)" for unlinked files. | |
2692 | */ | |
02125a82 AV |
2693 | static int path_with_deleted(const struct path *path, |
2694 | const struct path *root, | |
2695 | char **buf, int *buflen) | |
ffd1f4ed MS |
2696 | { |
2697 | prepend(buf, buflen, "\0", 1); | |
2698 | if (d_unlinked(path->dentry)) { | |
2699 | int error = prepend(buf, buflen, " (deleted)", 10); | |
2700 | if (error) | |
2701 | return error; | |
2702 | } | |
2703 | ||
2704 | return prepend_path(path, root, buf, buflen); | |
2705 | } | |
2706 | ||
8df9d1a4 MS |
2707 | static int prepend_unreachable(char **buffer, int *buflen) |
2708 | { | |
2709 | return prepend(buffer, buflen, "(unreachable)", 13); | |
2710 | } | |
2711 | ||
a03a8a70 JB |
2712 | /** |
2713 | * d_path - return the path of a dentry | |
cf28b486 | 2714 | * @path: path to report |
a03a8a70 JB |
2715 | * @buf: buffer to return value in |
2716 | * @buflen: buffer length | |
2717 | * | |
2718 | * Convert a dentry into an ASCII path name. If the entry has been deleted | |
2719 | * the string " (deleted)" is appended. Note that this is ambiguous. | |
2720 | * | |
52afeefb AV |
2721 | * Returns a pointer into the buffer or an error code if the path was |
2722 | * too long. Note: Callers should use the returned pointer, not the passed | |
2723 | * in buffer, to use the name! The implementation often starts at an offset | |
2724 | * into the buffer, and may leave 0 bytes at the start. | |
a03a8a70 | 2725 | * |
31f3e0b3 | 2726 | * "buflen" should be positive. |
a03a8a70 | 2727 | */ |
20d4fdc1 | 2728 | char *d_path(const struct path *path, char *buf, int buflen) |
1da177e4 | 2729 | { |
ffd1f4ed | 2730 | char *res = buf + buflen; |
6ac08c39 | 2731 | struct path root; |
ffd1f4ed | 2732 | int error; |
1da177e4 | 2733 | |
c23fbb6b ED |
2734 | /* |
2735 | * We have various synthetic filesystems that never get mounted. On | |
2736 | * these filesystems dentries are never used for lookup purposes, and | |
2737 | * thus don't need to be hashed. They also don't need a name until a | |
2738 | * user wants to identify the object in /proc/pid/fd/. The little hack | |
2739 | * below allows us to generate a name for these objects on demand: | |
3717eee3 EB |
2740 | * |
2741 | * Some pseudo inodes are mountable. When they are mounted | |
2742 | * path->dentry == path->mnt->mnt_root. In that case don't call d_dname | |
2743 | * and instead have d_path return the mounted path. | |
c23fbb6b | 2744 | */ |
3717eee3 EB |
2745 | if (path->dentry->d_op && path->dentry->d_op->d_dname && |
2746 | (!IS_ROOT(path->dentry) || path->dentry != path->mnt->mnt_root)) | |
cf28b486 | 2747 | return path->dentry->d_op->d_dname(path->dentry, buf, buflen); |
c23fbb6b | 2748 | |
f7ad3c6b | 2749 | get_fs_root(current->fs, &root); |
7ea600b5 | 2750 | br_read_lock(&vfsmount_lock); |
949854d0 | 2751 | write_seqlock(&rename_lock); |
02125a82 | 2752 | error = path_with_deleted(path, &root, &res, &buflen); |
7ea600b5 AV |
2753 | write_sequnlock(&rename_lock); |
2754 | br_read_unlock(&vfsmount_lock); | |
02125a82 | 2755 | if (error < 0) |
ffd1f4ed | 2756 | res = ERR_PTR(error); |
6ac08c39 | 2757 | path_put(&root); |
1da177e4 LT |
2758 | return res; |
2759 | } | |
ec4f8605 | 2760 | EXPORT_SYMBOL(d_path); |
1da177e4 | 2761 | |
c23fbb6b ED |
2762 | /* |
2763 | * Helper function for dentry_operations.d_dname() members | |
2764 | */ | |
2765 | char *dynamic_dname(struct dentry *dentry, char *buffer, int buflen, | |
2766 | const char *fmt, ...) | |
2767 | { | |
2768 | va_list args; | |
2769 | char temp[64]; | |
2770 | int sz; | |
2771 | ||
2772 | va_start(args, fmt); | |
2773 | sz = vsnprintf(temp, sizeof(temp), fmt, args) + 1; | |
2774 | va_end(args); | |
2775 | ||
2776 | if (sz > sizeof(temp) || sz > buflen) | |
2777 | return ERR_PTR(-ENAMETOOLONG); | |
2778 | ||
2779 | buffer += buflen - sz; | |
2780 | return memcpy(buffer, temp, sz); | |
2781 | } | |
2782 | ||
ad4c3cc4 AV |
2783 | char *simple_dname(struct dentry *dentry, char *buffer, int buflen) |
2784 | { | |
2785 | char *end = buffer + buflen; | |
2786 | /* these dentries are never renamed, so d_lock is not needed */ | |
2787 | if (prepend(&end, &buflen, " (deleted)", 11) || | |
2788 | prepend_name(&end, &buflen, &dentry->d_name) || | |
2789 | prepend(&end, &buflen, "/", 1)) | |
2790 | end = ERR_PTR(-ENAMETOOLONG); | |
2791 | return end; | |
2792 | } | |
2793 | ||
6092d048 RP |
2794 | /* |
2795 | * Write full pathname from the root of the filesystem into the buffer. | |
2796 | */ | |
ec2447c2 | 2797 | static char *__dentry_path(struct dentry *dentry, char *buf, int buflen) |
6092d048 RP |
2798 | { |
2799 | char *end = buf + buflen; | |
2800 | char *retval; | |
2801 | ||
6092d048 | 2802 | prepend(&end, &buflen, "\0", 1); |
6092d048 RP |
2803 | if (buflen < 1) |
2804 | goto Elong; | |
2805 | /* Get '/' right */ | |
2806 | retval = end-1; | |
2807 | *retval = '/'; | |
2808 | ||
cdd16d02 MS |
2809 | while (!IS_ROOT(dentry)) { |
2810 | struct dentry *parent = dentry->d_parent; | |
9abca360 | 2811 | int error; |
6092d048 | 2812 | |
6092d048 | 2813 | prefetch(parent); |
9abca360 NP |
2814 | spin_lock(&dentry->d_lock); |
2815 | error = prepend_name(&end, &buflen, &dentry->d_name); | |
2816 | spin_unlock(&dentry->d_lock); | |
2817 | if (error != 0 || prepend(&end, &buflen, "/", 1) != 0) | |
6092d048 RP |
2818 | goto Elong; |
2819 | ||
2820 | retval = end; | |
2821 | dentry = parent; | |
2822 | } | |
c103135c AV |
2823 | return retval; |
2824 | Elong: | |
2825 | return ERR_PTR(-ENAMETOOLONG); | |
2826 | } | |
ec2447c2 NP |
2827 | |
2828 | char *dentry_path_raw(struct dentry *dentry, char *buf, int buflen) | |
2829 | { | |
2830 | char *retval; | |
2831 | ||
949854d0 | 2832 | write_seqlock(&rename_lock); |
ec2447c2 | 2833 | retval = __dentry_path(dentry, buf, buflen); |
949854d0 | 2834 | write_sequnlock(&rename_lock); |
ec2447c2 NP |
2835 | |
2836 | return retval; | |
2837 | } | |
2838 | EXPORT_SYMBOL(dentry_path_raw); | |
c103135c AV |
2839 | |
2840 | char *dentry_path(struct dentry *dentry, char *buf, int buflen) | |
2841 | { | |
2842 | char *p = NULL; | |
2843 | char *retval; | |
2844 | ||
949854d0 | 2845 | write_seqlock(&rename_lock); |
c103135c AV |
2846 | if (d_unlinked(dentry)) { |
2847 | p = buf + buflen; | |
2848 | if (prepend(&p, &buflen, "//deleted", 10) != 0) | |
2849 | goto Elong; | |
2850 | buflen++; | |
2851 | } | |
2852 | retval = __dentry_path(dentry, buf, buflen); | |
949854d0 | 2853 | write_sequnlock(&rename_lock); |
c103135c AV |
2854 | if (!IS_ERR(retval) && p) |
2855 | *p = '/'; /* restore '/' overriden with '\0' */ | |
6092d048 RP |
2856 | return retval; |
2857 | Elong: | |
6092d048 RP |
2858 | return ERR_PTR(-ENAMETOOLONG); |
2859 | } | |
2860 | ||
1da177e4 LT |
2861 | /* |
2862 | * NOTE! The user-level library version returns a | |
2863 | * character pointer. The kernel system call just | |
2864 | * returns the length of the buffer filled (which | |
2865 | * includes the ending '\0' character), or a negative | |
2866 | * error value. So libc would do something like | |
2867 | * | |
2868 | * char *getcwd(char * buf, size_t size) | |
2869 | * { | |
2870 | * int retval; | |
2871 | * | |
2872 | * retval = sys_getcwd(buf, size); | |
2873 | * if (retval >= 0) | |
2874 | * return buf; | |
2875 | * errno = -retval; | |
2876 | * return NULL; | |
2877 | * } | |
2878 | */ | |
3cdad428 | 2879 | SYSCALL_DEFINE2(getcwd, char __user *, buf, unsigned long, size) |
1da177e4 | 2880 | { |
552ce544 | 2881 | int error; |
6ac08c39 | 2882 | struct path pwd, root; |
552ce544 | 2883 | char *page = (char *) __get_free_page(GFP_USER); |
1da177e4 LT |
2884 | |
2885 | if (!page) | |
2886 | return -ENOMEM; | |
2887 | ||
f7ad3c6b | 2888 | get_fs_root_and_pwd(current->fs, &root, &pwd); |
1da177e4 | 2889 | |
552ce544 | 2890 | error = -ENOENT; |
7ea600b5 | 2891 | br_read_lock(&vfsmount_lock); |
949854d0 | 2892 | write_seqlock(&rename_lock); |
f3da392e | 2893 | if (!d_unlinked(pwd.dentry)) { |
552ce544 | 2894 | unsigned long len; |
8df9d1a4 MS |
2895 | char *cwd = page + PAGE_SIZE; |
2896 | int buflen = PAGE_SIZE; | |
1da177e4 | 2897 | |
8df9d1a4 | 2898 | prepend(&cwd, &buflen, "\0", 1); |
02125a82 | 2899 | error = prepend_path(&pwd, &root, &cwd, &buflen); |
949854d0 | 2900 | write_sequnlock(&rename_lock); |
7ea600b5 | 2901 | br_read_unlock(&vfsmount_lock); |
552ce544 | 2902 | |
02125a82 | 2903 | if (error < 0) |
552ce544 LT |
2904 | goto out; |
2905 | ||
8df9d1a4 | 2906 | /* Unreachable from current root */ |
02125a82 | 2907 | if (error > 0) { |
8df9d1a4 MS |
2908 | error = prepend_unreachable(&cwd, &buflen); |
2909 | if (error) | |
2910 | goto out; | |
2911 | } | |
2912 | ||
552ce544 LT |
2913 | error = -ERANGE; |
2914 | len = PAGE_SIZE + page - cwd; | |
2915 | if (len <= size) { | |
2916 | error = len; | |
2917 | if (copy_to_user(buf, cwd, len)) | |
2918 | error = -EFAULT; | |
2919 | } | |
949854d0 NP |
2920 | } else { |
2921 | write_sequnlock(&rename_lock); | |
7ea600b5 | 2922 | br_read_unlock(&vfsmount_lock); |
949854d0 | 2923 | } |
1da177e4 LT |
2924 | |
2925 | out: | |
6ac08c39 JB |
2926 | path_put(&pwd); |
2927 | path_put(&root); | |
1da177e4 LT |
2928 | free_page((unsigned long) page); |
2929 | return error; | |
2930 | } | |
2931 | ||
2932 | /* | |
2933 | * Test whether new_dentry is a subdirectory of old_dentry. | |
2934 | * | |
2935 | * Trivially implemented using the dcache structure | |
2936 | */ | |
2937 | ||
2938 | /** | |
2939 | * is_subdir - is new dentry a subdirectory of old_dentry | |
2940 | * @new_dentry: new dentry | |
2941 | * @old_dentry: old dentry | |
2942 | * | |
2943 | * Returns 1 if new_dentry is a subdirectory of the parent (at any depth). | |
2944 | * Returns 0 otherwise. | |
2945 | * Caller must ensure that "new_dentry" is pinned before calling is_subdir() | |
2946 | */ | |
2947 | ||
e2761a11 | 2948 | int is_subdir(struct dentry *new_dentry, struct dentry *old_dentry) |
1da177e4 LT |
2949 | { |
2950 | int result; | |
949854d0 | 2951 | unsigned seq; |
1da177e4 | 2952 | |
e2761a11 OH |
2953 | if (new_dentry == old_dentry) |
2954 | return 1; | |
2955 | ||
e2761a11 | 2956 | do { |
1da177e4 | 2957 | /* for restarting inner loop in case of seq retry */ |
1da177e4 | 2958 | seq = read_seqbegin(&rename_lock); |
949854d0 NP |
2959 | /* |
2960 | * Need rcu_readlock to protect against the d_parent trashing | |
2961 | * due to d_move | |
2962 | */ | |
2963 | rcu_read_lock(); | |
e2761a11 | 2964 | if (d_ancestor(old_dentry, new_dentry)) |
1da177e4 | 2965 | result = 1; |
e2761a11 OH |
2966 | else |
2967 | result = 0; | |
949854d0 | 2968 | rcu_read_unlock(); |
1da177e4 | 2969 | } while (read_seqretry(&rename_lock, seq)); |
1da177e4 LT |
2970 | |
2971 | return result; | |
2972 | } | |
2973 | ||
2974 | void d_genocide(struct dentry *root) | |
2975 | { | |
949854d0 | 2976 | struct dentry *this_parent; |
1da177e4 | 2977 | struct list_head *next; |
949854d0 | 2978 | unsigned seq; |
58db63d0 | 2979 | int locked = 0; |
1da177e4 | 2980 | |
949854d0 | 2981 | seq = read_seqbegin(&rename_lock); |
58db63d0 NP |
2982 | again: |
2983 | this_parent = root; | |
2fd6b7f5 | 2984 | spin_lock(&this_parent->d_lock); |
1da177e4 LT |
2985 | repeat: |
2986 | next = this_parent->d_subdirs.next; | |
2987 | resume: | |
2988 | while (next != &this_parent->d_subdirs) { | |
2989 | struct list_head *tmp = next; | |
d76c446b | 2990 | struct dentry *dentry = list_entry(tmp, struct dentry, d_child); |
1da177e4 | 2991 | next = tmp->next; |
949854d0 | 2992 | |
da502956 NP |
2993 | spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); |
2994 | if (d_unhashed(dentry) || !dentry->d_inode) { | |
2995 | spin_unlock(&dentry->d_lock); | |
1da177e4 | 2996 | continue; |
da502956 | 2997 | } |
1da177e4 | 2998 | if (!list_empty(&dentry->d_subdirs)) { |
2fd6b7f5 NP |
2999 | spin_unlock(&this_parent->d_lock); |
3000 | spin_release(&dentry->d_lock.dep_map, 1, _RET_IP_); | |
1da177e4 | 3001 | this_parent = dentry; |
2fd6b7f5 | 3002 | spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_); |
1da177e4 LT |
3003 | goto repeat; |
3004 | } | |
949854d0 NP |
3005 | if (!(dentry->d_flags & DCACHE_GENOCIDE)) { |
3006 | dentry->d_flags |= DCACHE_GENOCIDE; | |
3007 | dentry->d_count--; | |
3008 | } | |
b7ab39f6 | 3009 | spin_unlock(&dentry->d_lock); |
1da177e4 | 3010 | } |
b102b017 AV |
3011 | rcu_read_lock(); |
3012 | ascend: | |
1da177e4 | 3013 | if (this_parent != root) { |
c826cb7d | 3014 | struct dentry *child = this_parent; |
949854d0 NP |
3015 | if (!(this_parent->d_flags & DCACHE_GENOCIDE)) { |
3016 | this_parent->d_flags |= DCACHE_GENOCIDE; | |
3017 | this_parent->d_count--; | |
3018 | } | |
b102b017 AV |
3019 | this_parent = child->d_parent; |
3020 | ||
3021 | spin_unlock(&child->d_lock); | |
3022 | spin_lock(&this_parent->d_lock); | |
3023 | ||
3024 | /* might go back up the wrong parent if we have had a rename. */ | |
3025 | if (!locked && read_seqretry(&rename_lock, seq)) | |
949854d0 | 3026 | goto rename_retry; |
d76c446b | 3027 | next = child->d_child.next; |
b102b017 AV |
3028 | while (unlikely(child->d_flags & DCACHE_DENTRY_KILLED)) { |
3029 | if (next == &this_parent->d_subdirs) | |
3030 | goto ascend; | |
3031 | child = list_entry(next, struct dentry, d_child); | |
3032 | next = next->next; | |
3033 | } | |
3034 | rcu_read_unlock(); | |
1da177e4 LT |
3035 | goto resume; |
3036 | } | |
58db63d0 | 3037 | if (!locked && read_seqretry(&rename_lock, seq)) |
949854d0 | 3038 | goto rename_retry; |
b102b017 AV |
3039 | spin_unlock(&this_parent->d_lock); |
3040 | rcu_read_unlock(); | |
58db63d0 NP |
3041 | if (locked) |
3042 | write_sequnlock(&rename_lock); | |
3043 | return; | |
3044 | ||
3045 | rename_retry: | |
b102b017 AV |
3046 | spin_unlock(&this_parent->d_lock); |
3047 | rcu_read_unlock(); | |
8110e16d MS |
3048 | if (locked) |
3049 | goto again; | |
58db63d0 NP |
3050 | locked = 1; |
3051 | write_seqlock(&rename_lock); | |
3052 | goto again; | |
1da177e4 LT |
3053 | } |
3054 | ||
3055 | /** | |
3056 | * find_inode_number - check for dentry with name | |
3057 | * @dir: directory to check | |
3058 | * @name: Name to find. | |
3059 | * | |
3060 | * Check whether a dentry already exists for the given name, | |
3061 | * and return the inode number if it has an inode. Otherwise | |
3062 | * 0 is returned. | |
3063 | * | |
3064 | * This routine is used to post-process directory listings for | |
3065 | * filesystems using synthetic inode numbers, and is necessary | |
3066 | * to keep getcwd() working. | |
3067 | */ | |
3068 | ||
3069 | ino_t find_inode_number(struct dentry *dir, struct qstr *name) | |
3070 | { | |
3071 | struct dentry * dentry; | |
3072 | ino_t ino = 0; | |
3073 | ||
3e7e241f | 3074 | dentry = d_hash_and_lookup(dir, name); |
4f522a24 | 3075 | if (!IS_ERR_OR_NULL(dentry)) { |
1da177e4 LT |
3076 | if (dentry->d_inode) |
3077 | ino = dentry->d_inode->i_ino; | |
3078 | dput(dentry); | |
3079 | } | |
1da177e4 LT |
3080 | return ino; |
3081 | } | |
ec4f8605 | 3082 | EXPORT_SYMBOL(find_inode_number); |
1da177e4 LT |
3083 | |
3084 | static __initdata unsigned long dhash_entries; | |
3085 | static int __init set_dhash_entries(char *str) | |
3086 | { | |
3087 | if (!str) | |
3088 | return 0; | |
3089 | dhash_entries = simple_strtoul(str, &str, 0); | |
3090 | return 1; | |
3091 | } | |
3092 | __setup("dhash_entries=", set_dhash_entries); | |
3093 | ||
3094 | static void __init dcache_init_early(void) | |
3095 | { | |
074b8517 | 3096 | unsigned int loop; |
1da177e4 LT |
3097 | |
3098 | /* If hashes are distributed across NUMA nodes, defer | |
3099 | * hash allocation until vmalloc space is available. | |
3100 | */ | |
3101 | if (hashdist) | |
3102 | return; | |
3103 | ||
3104 | dentry_hashtable = | |
3105 | alloc_large_system_hash("Dentry cache", | |
b07ad996 | 3106 | sizeof(struct hlist_bl_head), |
1da177e4 LT |
3107 | dhash_entries, |
3108 | 13, | |
3109 | HASH_EARLY, | |
3110 | &d_hash_shift, | |
3111 | &d_hash_mask, | |
31fe62b9 | 3112 | 0, |
1da177e4 LT |
3113 | 0); |
3114 | ||
074b8517 | 3115 | for (loop = 0; loop < (1U << d_hash_shift); loop++) |
b07ad996 | 3116 | INIT_HLIST_BL_HEAD(dentry_hashtable + loop); |
1da177e4 LT |
3117 | } |
3118 | ||
74bf17cf | 3119 | static void __init dcache_init(void) |
1da177e4 | 3120 | { |
074b8517 | 3121 | unsigned int loop; |
1da177e4 LT |
3122 | |
3123 | /* | |
3124 | * A constructor could be added for stable state like the lists, | |
3125 | * but it is probably not worth it because of the cache nature | |
3126 | * of the dcache. | |
3127 | */ | |
0a31bd5f CL |
3128 | dentry_cache = KMEM_CACHE(dentry, |
3129 | SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|SLAB_MEM_SPREAD); | |
1da177e4 LT |
3130 | |
3131 | /* Hash may have been set up in dcache_init_early */ | |
3132 | if (!hashdist) | |
3133 | return; | |
3134 | ||
3135 | dentry_hashtable = | |
3136 | alloc_large_system_hash("Dentry cache", | |
b07ad996 | 3137 | sizeof(struct hlist_bl_head), |
1da177e4 LT |
3138 | dhash_entries, |
3139 | 13, | |
3140 | 0, | |
3141 | &d_hash_shift, | |
3142 | &d_hash_mask, | |
31fe62b9 | 3143 | 0, |
1da177e4 LT |
3144 | 0); |
3145 | ||
074b8517 | 3146 | for (loop = 0; loop < (1U << d_hash_shift); loop++) |
b07ad996 | 3147 | INIT_HLIST_BL_HEAD(dentry_hashtable + loop); |
1da177e4 LT |
3148 | } |
3149 | ||
3150 | /* SLAB cache for __getname() consumers */ | |
e18b890b | 3151 | struct kmem_cache *names_cachep __read_mostly; |
ec4f8605 | 3152 | EXPORT_SYMBOL(names_cachep); |
1da177e4 | 3153 | |
1da177e4 LT |
3154 | EXPORT_SYMBOL(d_genocide); |
3155 | ||
1da177e4 LT |
3156 | void __init vfs_caches_init_early(void) |
3157 | { | |
3158 | dcache_init_early(); | |
3159 | inode_init_early(); | |
3160 | } | |
3161 | ||
3162 | void __init vfs_caches_init(unsigned long mempages) | |
3163 | { | |
3164 | unsigned long reserve; | |
3165 | ||
3166 | /* Base hash sizes on available memory, with a reserve equal to | |
3167 | 150% of current kernel size */ | |
3168 | ||
3169 | reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1); | |
3170 | mempages -= reserve; | |
3171 | ||
3172 | names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0, | |
20c2df83 | 3173 | SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL); |
1da177e4 | 3174 | |
74bf17cf DC |
3175 | dcache_init(); |
3176 | inode_init(); | |
1da177e4 | 3177 | files_init(mempages); |
74bf17cf | 3178 | mnt_init(); |
1da177e4 LT |
3179 | bdev_cache_init(); |
3180 | chrdev_init(); | |
3181 | } |