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