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