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