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