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> | |
24 | #include <linux/smp_lock.h> | |
25 | #include <linux/hash.h> | |
26 | #include <linux/cache.h> | |
27 | #include <linux/module.h> | |
28 | #include <linux/mount.h> | |
29 | #include <linux/file.h> | |
30 | #include <asm/uaccess.h> | |
31 | #include <linux/security.h> | |
32 | #include <linux/seqlock.h> | |
33 | #include <linux/swap.h> | |
34 | #include <linux/bootmem.h> | |
07f3f05c | 35 | #include "internal.h" |
1da177e4 | 36 | |
1da177e4 | 37 | |
fa3536cc | 38 | int sysctl_vfs_cache_pressure __read_mostly = 100; |
1da177e4 LT |
39 | EXPORT_SYMBOL_GPL(sysctl_vfs_cache_pressure); |
40 | ||
41 | __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_lock); | |
e4d91918 | 42 | static __cacheline_aligned_in_smp DEFINE_SEQLOCK(rename_lock); |
1da177e4 LT |
43 | |
44 | EXPORT_SYMBOL(dcache_lock); | |
45 | ||
fa3536cc | 46 | static kmem_cache_t *dentry_cache __read_mostly; |
1da177e4 LT |
47 | |
48 | #define DNAME_INLINE_LEN (sizeof(struct dentry)-offsetof(struct dentry,d_iname)) | |
49 | ||
50 | /* | |
51 | * This is the single most critical data structure when it comes | |
52 | * to the dcache: the hashtable for lookups. Somebody should try | |
53 | * to make this good - I've just made it work. | |
54 | * | |
55 | * This hash-function tries to avoid losing too many bits of hash | |
56 | * information, yet avoid using a prime hash-size or similar. | |
57 | */ | |
58 | #define D_HASHBITS d_hash_shift | |
59 | #define D_HASHMASK d_hash_mask | |
60 | ||
fa3536cc ED |
61 | static unsigned int d_hash_mask __read_mostly; |
62 | static unsigned int d_hash_shift __read_mostly; | |
63 | static struct hlist_head *dentry_hashtable __read_mostly; | |
1da177e4 LT |
64 | static LIST_HEAD(dentry_unused); |
65 | ||
66 | /* Statistics gathering. */ | |
67 | struct dentry_stat_t dentry_stat = { | |
68 | .age_limit = 45, | |
69 | }; | |
70 | ||
71 | static void d_callback(struct rcu_head *head) | |
72 | { | |
5160ee6f | 73 | struct dentry * dentry = container_of(head, struct dentry, d_u.d_rcu); |
1da177e4 LT |
74 | |
75 | if (dname_external(dentry)) | |
76 | kfree(dentry->d_name.name); | |
77 | kmem_cache_free(dentry_cache, dentry); | |
78 | } | |
79 | ||
80 | /* | |
81 | * no dcache_lock, please. The caller must decrement dentry_stat.nr_dentry | |
82 | * inside dcache_lock. | |
83 | */ | |
84 | static void d_free(struct dentry *dentry) | |
85 | { | |
86 | if (dentry->d_op && dentry->d_op->d_release) | |
87 | dentry->d_op->d_release(dentry); | |
5160ee6f | 88 | call_rcu(&dentry->d_u.d_rcu, d_callback); |
1da177e4 LT |
89 | } |
90 | ||
91 | /* | |
92 | * Release the dentry's inode, using the filesystem | |
93 | * d_iput() operation if defined. | |
94 | * Called with dcache_lock and per dentry lock held, drops both. | |
95 | */ | |
858119e1 | 96 | static void dentry_iput(struct dentry * dentry) |
1da177e4 LT |
97 | { |
98 | struct inode *inode = dentry->d_inode; | |
99 | if (inode) { | |
100 | dentry->d_inode = NULL; | |
101 | list_del_init(&dentry->d_alias); | |
102 | spin_unlock(&dentry->d_lock); | |
103 | spin_unlock(&dcache_lock); | |
f805fbda LT |
104 | if (!inode->i_nlink) |
105 | fsnotify_inoderemove(inode); | |
1da177e4 LT |
106 | if (dentry->d_op && dentry->d_op->d_iput) |
107 | dentry->d_op->d_iput(dentry, inode); | |
108 | else | |
109 | iput(inode); | |
110 | } else { | |
111 | spin_unlock(&dentry->d_lock); | |
112 | spin_unlock(&dcache_lock); | |
113 | } | |
114 | } | |
115 | ||
116 | /* | |
117 | * This is dput | |
118 | * | |
119 | * This is complicated by the fact that we do not want to put | |
120 | * dentries that are no longer on any hash chain on the unused | |
121 | * list: we'd much rather just get rid of them immediately. | |
122 | * | |
123 | * However, that implies that we have to traverse the dentry | |
124 | * tree upwards to the parents which might _also_ now be | |
125 | * scheduled for deletion (it may have been only waiting for | |
126 | * its last child to go away). | |
127 | * | |
128 | * This tail recursion is done by hand as we don't want to depend | |
129 | * on the compiler to always get this right (gcc generally doesn't). | |
130 | * Real recursion would eat up our stack space. | |
131 | */ | |
132 | ||
133 | /* | |
134 | * dput - release a dentry | |
135 | * @dentry: dentry to release | |
136 | * | |
137 | * Release a dentry. This will drop the usage count and if appropriate | |
138 | * call the dentry unlink method as well as removing it from the queues and | |
139 | * releasing its resources. If the parent dentries were scheduled for release | |
140 | * they too may now get deleted. | |
141 | * | |
142 | * no dcache lock, please. | |
143 | */ | |
144 | ||
145 | void dput(struct dentry *dentry) | |
146 | { | |
147 | if (!dentry) | |
148 | return; | |
149 | ||
150 | repeat: | |
151 | if (atomic_read(&dentry->d_count) == 1) | |
152 | might_sleep(); | |
153 | if (!atomic_dec_and_lock(&dentry->d_count, &dcache_lock)) | |
154 | return; | |
155 | ||
156 | spin_lock(&dentry->d_lock); | |
157 | if (atomic_read(&dentry->d_count)) { | |
158 | spin_unlock(&dentry->d_lock); | |
159 | spin_unlock(&dcache_lock); | |
160 | return; | |
161 | } | |
162 | ||
163 | /* | |
164 | * AV: ->d_delete() is _NOT_ allowed to block now. | |
165 | */ | |
166 | if (dentry->d_op && dentry->d_op->d_delete) { | |
167 | if (dentry->d_op->d_delete(dentry)) | |
168 | goto unhash_it; | |
169 | } | |
170 | /* Unreachable? Get rid of it */ | |
171 | if (d_unhashed(dentry)) | |
172 | goto kill_it; | |
173 | if (list_empty(&dentry->d_lru)) { | |
174 | dentry->d_flags |= DCACHE_REFERENCED; | |
175 | list_add(&dentry->d_lru, &dentry_unused); | |
176 | dentry_stat.nr_unused++; | |
177 | } | |
178 | spin_unlock(&dentry->d_lock); | |
179 | spin_unlock(&dcache_lock); | |
180 | return; | |
181 | ||
182 | unhash_it: | |
183 | __d_drop(dentry); | |
184 | ||
185 | kill_it: { | |
186 | struct dentry *parent; | |
187 | ||
188 | /* If dentry was on d_lru list | |
189 | * delete it from there | |
190 | */ | |
191 | if (!list_empty(&dentry->d_lru)) { | |
192 | list_del(&dentry->d_lru); | |
193 | dentry_stat.nr_unused--; | |
194 | } | |
5160ee6f | 195 | list_del(&dentry->d_u.d_child); |
1da177e4 LT |
196 | dentry_stat.nr_dentry--; /* For d_free, below */ |
197 | /*drops the locks, at that point nobody can reach this dentry */ | |
198 | dentry_iput(dentry); | |
199 | parent = dentry->d_parent; | |
200 | d_free(dentry); | |
201 | if (dentry == parent) | |
202 | return; | |
203 | dentry = parent; | |
204 | goto repeat; | |
205 | } | |
206 | } | |
207 | ||
208 | /** | |
209 | * d_invalidate - invalidate a dentry | |
210 | * @dentry: dentry to invalidate | |
211 | * | |
212 | * Try to invalidate the dentry if it turns out to be | |
213 | * possible. If there are other dentries that can be | |
214 | * reached through this one we can't delete it and we | |
215 | * return -EBUSY. On success we return 0. | |
216 | * | |
217 | * no dcache lock. | |
218 | */ | |
219 | ||
220 | int d_invalidate(struct dentry * dentry) | |
221 | { | |
222 | /* | |
223 | * If it's already been dropped, return OK. | |
224 | */ | |
225 | spin_lock(&dcache_lock); | |
226 | if (d_unhashed(dentry)) { | |
227 | spin_unlock(&dcache_lock); | |
228 | return 0; | |
229 | } | |
230 | /* | |
231 | * Check whether to do a partial shrink_dcache | |
232 | * to get rid of unused child entries. | |
233 | */ | |
234 | if (!list_empty(&dentry->d_subdirs)) { | |
235 | spin_unlock(&dcache_lock); | |
236 | shrink_dcache_parent(dentry); | |
237 | spin_lock(&dcache_lock); | |
238 | } | |
239 | ||
240 | /* | |
241 | * Somebody else still using it? | |
242 | * | |
243 | * If it's a directory, we can't drop it | |
244 | * for fear of somebody re-populating it | |
245 | * with children (even though dropping it | |
246 | * would make it unreachable from the root, | |
247 | * we might still populate it if it was a | |
248 | * working directory or similar). | |
249 | */ | |
250 | spin_lock(&dentry->d_lock); | |
251 | if (atomic_read(&dentry->d_count) > 1) { | |
252 | if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) { | |
253 | spin_unlock(&dentry->d_lock); | |
254 | spin_unlock(&dcache_lock); | |
255 | return -EBUSY; | |
256 | } | |
257 | } | |
258 | ||
259 | __d_drop(dentry); | |
260 | spin_unlock(&dentry->d_lock); | |
261 | spin_unlock(&dcache_lock); | |
262 | return 0; | |
263 | } | |
264 | ||
265 | /* This should be called _only_ with dcache_lock held */ | |
266 | ||
267 | static inline struct dentry * __dget_locked(struct dentry *dentry) | |
268 | { | |
269 | atomic_inc(&dentry->d_count); | |
270 | if (!list_empty(&dentry->d_lru)) { | |
271 | dentry_stat.nr_unused--; | |
272 | list_del_init(&dentry->d_lru); | |
273 | } | |
274 | return dentry; | |
275 | } | |
276 | ||
277 | struct dentry * dget_locked(struct dentry *dentry) | |
278 | { | |
279 | return __dget_locked(dentry); | |
280 | } | |
281 | ||
282 | /** | |
283 | * d_find_alias - grab a hashed alias of inode | |
284 | * @inode: inode in question | |
285 | * @want_discon: flag, used by d_splice_alias, to request | |
286 | * that only a DISCONNECTED alias be returned. | |
287 | * | |
288 | * If inode has a hashed alias, or is a directory and has any alias, | |
289 | * acquire the reference to alias and return it. Otherwise return NULL. | |
290 | * Notice that if inode is a directory there can be only one alias and | |
291 | * it can be unhashed only if it has no children, or if it is the root | |
292 | * of a filesystem. | |
293 | * | |
21c0d8fd | 294 | * If the inode has an IS_ROOT, DCACHE_DISCONNECTED alias, then prefer |
1da177e4 | 295 | * any other hashed alias over that one unless @want_discon is set, |
21c0d8fd | 296 | * in which case only return an IS_ROOT, DCACHE_DISCONNECTED alias. |
1da177e4 LT |
297 | */ |
298 | ||
299 | static struct dentry * __d_find_alias(struct inode *inode, int want_discon) | |
300 | { | |
301 | struct list_head *head, *next, *tmp; | |
302 | struct dentry *alias, *discon_alias=NULL; | |
303 | ||
304 | head = &inode->i_dentry; | |
305 | next = inode->i_dentry.next; | |
306 | while (next != head) { | |
307 | tmp = next; | |
308 | next = tmp->next; | |
309 | prefetch(next); | |
310 | alias = list_entry(tmp, struct dentry, d_alias); | |
311 | if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) { | |
21c0d8fd N |
312 | if (IS_ROOT(alias) && |
313 | (alias->d_flags & DCACHE_DISCONNECTED)) | |
1da177e4 LT |
314 | discon_alias = alias; |
315 | else if (!want_discon) { | |
316 | __dget_locked(alias); | |
317 | return alias; | |
318 | } | |
319 | } | |
320 | } | |
321 | if (discon_alias) | |
322 | __dget_locked(discon_alias); | |
323 | return discon_alias; | |
324 | } | |
325 | ||
326 | struct dentry * d_find_alias(struct inode *inode) | |
327 | { | |
214fda1f DH |
328 | struct dentry *de = NULL; |
329 | ||
330 | if (!list_empty(&inode->i_dentry)) { | |
331 | spin_lock(&dcache_lock); | |
332 | de = __d_find_alias(inode, 0); | |
333 | spin_unlock(&dcache_lock); | |
334 | } | |
1da177e4 LT |
335 | return de; |
336 | } | |
337 | ||
338 | /* | |
339 | * Try to kill dentries associated with this inode. | |
340 | * WARNING: you must own a reference to inode. | |
341 | */ | |
342 | void d_prune_aliases(struct inode *inode) | |
343 | { | |
0cdca3f9 | 344 | struct dentry *dentry; |
1da177e4 LT |
345 | restart: |
346 | spin_lock(&dcache_lock); | |
0cdca3f9 | 347 | list_for_each_entry(dentry, &inode->i_dentry, d_alias) { |
1da177e4 LT |
348 | spin_lock(&dentry->d_lock); |
349 | if (!atomic_read(&dentry->d_count)) { | |
350 | __dget_locked(dentry); | |
351 | __d_drop(dentry); | |
352 | spin_unlock(&dentry->d_lock); | |
353 | spin_unlock(&dcache_lock); | |
354 | dput(dentry); | |
355 | goto restart; | |
356 | } | |
357 | spin_unlock(&dentry->d_lock); | |
358 | } | |
359 | spin_unlock(&dcache_lock); | |
360 | } | |
361 | ||
362 | /* | |
d702ccb3 AM |
363 | * Throw away a dentry - free the inode, dput the parent. This requires that |
364 | * the LRU list has already been removed. | |
365 | * | |
1da177e4 | 366 | * Called with dcache_lock, drops it and then regains. |
d702ccb3 | 367 | * Called with dentry->d_lock held, drops it. |
1da177e4 | 368 | */ |
d702ccb3 | 369 | static void prune_one_dentry(struct dentry * dentry) |
1da177e4 LT |
370 | { |
371 | struct dentry * parent; | |
372 | ||
373 | __d_drop(dentry); | |
5160ee6f | 374 | list_del(&dentry->d_u.d_child); |
1da177e4 LT |
375 | dentry_stat.nr_dentry--; /* For d_free, below */ |
376 | dentry_iput(dentry); | |
377 | parent = dentry->d_parent; | |
378 | d_free(dentry); | |
379 | if (parent != dentry) | |
380 | dput(parent); | |
381 | spin_lock(&dcache_lock); | |
382 | } | |
383 | ||
384 | /** | |
385 | * prune_dcache - shrink the dcache | |
386 | * @count: number of entries to try and free | |
0feae5c4 N |
387 | * @sb: if given, ignore dentries for other superblocks |
388 | * which are being unmounted. | |
1da177e4 LT |
389 | * |
390 | * Shrink the dcache. This is done when we need | |
391 | * more memory, or simply when we need to unmount | |
392 | * something (at which point we need to unuse | |
393 | * all dentries). | |
394 | * | |
395 | * This function may fail to free any resources if | |
396 | * all the dentries are in use. | |
397 | */ | |
398 | ||
0feae5c4 | 399 | static void prune_dcache(int count, struct super_block *sb) |
1da177e4 LT |
400 | { |
401 | spin_lock(&dcache_lock); | |
402 | for (; count ; count--) { | |
403 | struct dentry *dentry; | |
404 | struct list_head *tmp; | |
0feae5c4 | 405 | struct rw_semaphore *s_umount; |
1da177e4 LT |
406 | |
407 | cond_resched_lock(&dcache_lock); | |
408 | ||
409 | tmp = dentry_unused.prev; | |
f58a1ebb | 410 | if (sb) { |
0feae5c4 N |
411 | /* Try to find a dentry for this sb, but don't try |
412 | * too hard, if they aren't near the tail they will | |
413 | * be moved down again soon | |
414 | */ | |
415 | int skip = count; | |
416 | while (skip && tmp != &dentry_unused && | |
417 | list_entry(tmp, struct dentry, d_lru)->d_sb != sb) { | |
418 | skip--; | |
419 | tmp = tmp->prev; | |
420 | } | |
421 | } | |
1da177e4 LT |
422 | if (tmp == &dentry_unused) |
423 | break; | |
424 | list_del_init(tmp); | |
425 | prefetch(dentry_unused.prev); | |
426 | dentry_stat.nr_unused--; | |
427 | dentry = list_entry(tmp, struct dentry, d_lru); | |
428 | ||
429 | spin_lock(&dentry->d_lock); | |
430 | /* | |
431 | * We found an inuse dentry which was not removed from | |
432 | * dentry_unused because of laziness during lookup. Do not free | |
433 | * it - just keep it off the dentry_unused list. | |
434 | */ | |
435 | if (atomic_read(&dentry->d_count)) { | |
436 | spin_unlock(&dentry->d_lock); | |
437 | continue; | |
438 | } | |
439 | /* If the dentry was recently referenced, don't free it. */ | |
440 | if (dentry->d_flags & DCACHE_REFERENCED) { | |
441 | dentry->d_flags &= ~DCACHE_REFERENCED; | |
442 | list_add(&dentry->d_lru, &dentry_unused); | |
443 | dentry_stat.nr_unused++; | |
444 | spin_unlock(&dentry->d_lock); | |
445 | continue; | |
446 | } | |
0feae5c4 N |
447 | /* |
448 | * If the dentry is not DCACHED_REFERENCED, it is time | |
449 | * to remove it from the dcache, provided the super block is | |
450 | * NULL (which means we are trying to reclaim memory) | |
451 | * or this dentry belongs to the same super block that | |
452 | * we want to shrink. | |
453 | */ | |
454 | /* | |
455 | * If this dentry is for "my" filesystem, then I can prune it | |
456 | * without taking the s_umount lock (I already hold it). | |
457 | */ | |
458 | if (sb && dentry->d_sb == sb) { | |
459 | prune_one_dentry(dentry); | |
460 | continue; | |
461 | } | |
462 | /* | |
463 | * ...otherwise we need to be sure this filesystem isn't being | |
464 | * unmounted, otherwise we could race with | |
465 | * generic_shutdown_super(), and end up holding a reference to | |
466 | * an inode while the filesystem is unmounted. | |
467 | * So we try to get s_umount, and make sure s_root isn't NULL. | |
468 | * (Take a local copy of s_umount to avoid a use-after-free of | |
469 | * `dentry'). | |
470 | */ | |
471 | s_umount = &dentry->d_sb->s_umount; | |
472 | if (down_read_trylock(s_umount)) { | |
473 | if (dentry->d_sb->s_root != NULL) { | |
474 | prune_one_dentry(dentry); | |
475 | up_read(s_umount); | |
476 | continue; | |
477 | } | |
478 | up_read(s_umount); | |
479 | } | |
480 | spin_unlock(&dentry->d_lock); | |
481 | /* Cannot remove the first dentry, and it isn't appropriate | |
482 | * to move it to the head of the list, so give up, and try | |
483 | * later | |
484 | */ | |
485 | break; | |
1da177e4 LT |
486 | } |
487 | spin_unlock(&dcache_lock); | |
488 | } | |
489 | ||
490 | /* | |
491 | * Shrink the dcache for the specified super block. | |
492 | * This allows us to unmount a device without disturbing | |
493 | * the dcache for the other devices. | |
494 | * | |
495 | * This implementation makes just two traversals of the | |
496 | * unused list. On the first pass we move the selected | |
497 | * dentries to the most recent end, and on the second | |
498 | * pass we free them. The second pass must restart after | |
499 | * each dput(), but since the target dentries are all at | |
500 | * the end, it's really just a single traversal. | |
501 | */ | |
502 | ||
503 | /** | |
504 | * shrink_dcache_sb - shrink dcache for a superblock | |
505 | * @sb: superblock | |
506 | * | |
507 | * Shrink the dcache for the specified super block. This | |
508 | * is used to free the dcache before unmounting a file | |
509 | * system | |
510 | */ | |
511 | ||
512 | void shrink_dcache_sb(struct super_block * sb) | |
513 | { | |
514 | struct list_head *tmp, *next; | |
515 | struct dentry *dentry; | |
516 | ||
517 | /* | |
518 | * Pass one ... move the dentries for the specified | |
519 | * superblock to the most recent end of the unused list. | |
520 | */ | |
521 | spin_lock(&dcache_lock); | |
0cdca3f9 | 522 | list_for_each_safe(tmp, next, &dentry_unused) { |
1da177e4 LT |
523 | dentry = list_entry(tmp, struct dentry, d_lru); |
524 | if (dentry->d_sb != sb) | |
525 | continue; | |
1bfba4e8 | 526 | list_move(tmp, &dentry_unused); |
1da177e4 LT |
527 | } |
528 | ||
529 | /* | |
530 | * Pass two ... free the dentries for this superblock. | |
531 | */ | |
532 | repeat: | |
0cdca3f9 | 533 | list_for_each_safe(tmp, next, &dentry_unused) { |
1da177e4 LT |
534 | dentry = list_entry(tmp, struct dentry, d_lru); |
535 | if (dentry->d_sb != sb) | |
536 | continue; | |
537 | dentry_stat.nr_unused--; | |
538 | list_del_init(tmp); | |
539 | spin_lock(&dentry->d_lock); | |
540 | if (atomic_read(&dentry->d_count)) { | |
541 | spin_unlock(&dentry->d_lock); | |
542 | continue; | |
543 | } | |
544 | prune_one_dentry(dentry); | |
2ab13460 | 545 | cond_resched_lock(&dcache_lock); |
1da177e4 LT |
546 | goto repeat; |
547 | } | |
548 | spin_unlock(&dcache_lock); | |
549 | } | |
550 | ||
551 | /* | |
552 | * Search for at least 1 mount point in the dentry's subdirs. | |
553 | * We descend to the next level whenever the d_subdirs | |
554 | * list is non-empty and continue searching. | |
555 | */ | |
556 | ||
557 | /** | |
558 | * have_submounts - check for mounts over a dentry | |
559 | * @parent: dentry to check. | |
560 | * | |
561 | * Return true if the parent or its subdirectories contain | |
562 | * a mount point | |
563 | */ | |
564 | ||
565 | int have_submounts(struct dentry *parent) | |
566 | { | |
567 | struct dentry *this_parent = parent; | |
568 | struct list_head *next; | |
569 | ||
570 | spin_lock(&dcache_lock); | |
571 | if (d_mountpoint(parent)) | |
572 | goto positive; | |
573 | repeat: | |
574 | next = this_parent->d_subdirs.next; | |
575 | resume: | |
576 | while (next != &this_parent->d_subdirs) { | |
577 | struct list_head *tmp = next; | |
5160ee6f | 578 | struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child); |
1da177e4 LT |
579 | next = tmp->next; |
580 | /* Have we found a mount point ? */ | |
581 | if (d_mountpoint(dentry)) | |
582 | goto positive; | |
583 | if (!list_empty(&dentry->d_subdirs)) { | |
584 | this_parent = dentry; | |
585 | goto repeat; | |
586 | } | |
587 | } | |
588 | /* | |
589 | * All done at this level ... ascend and resume the search. | |
590 | */ | |
591 | if (this_parent != parent) { | |
5160ee6f | 592 | next = this_parent->d_u.d_child.next; |
1da177e4 LT |
593 | this_parent = this_parent->d_parent; |
594 | goto resume; | |
595 | } | |
596 | spin_unlock(&dcache_lock); | |
597 | return 0; /* No mount points found in tree */ | |
598 | positive: | |
599 | spin_unlock(&dcache_lock); | |
600 | return 1; | |
601 | } | |
602 | ||
603 | /* | |
604 | * Search the dentry child list for the specified parent, | |
605 | * and move any unused dentries to the end of the unused | |
606 | * list for prune_dcache(). We descend to the next level | |
607 | * whenever the d_subdirs list is non-empty and continue | |
608 | * searching. | |
609 | * | |
610 | * It returns zero iff there are no unused children, | |
611 | * otherwise it returns the number of children moved to | |
612 | * the end of the unused list. This may not be the total | |
613 | * number of unused children, because select_parent can | |
614 | * drop the lock and return early due to latency | |
615 | * constraints. | |
616 | */ | |
617 | static int select_parent(struct dentry * parent) | |
618 | { | |
619 | struct dentry *this_parent = parent; | |
620 | struct list_head *next; | |
621 | int found = 0; | |
622 | ||
623 | spin_lock(&dcache_lock); | |
624 | repeat: | |
625 | next = this_parent->d_subdirs.next; | |
626 | resume: | |
627 | while (next != &this_parent->d_subdirs) { | |
628 | struct list_head *tmp = next; | |
5160ee6f | 629 | struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child); |
1da177e4 LT |
630 | next = tmp->next; |
631 | ||
632 | if (!list_empty(&dentry->d_lru)) { | |
633 | dentry_stat.nr_unused--; | |
634 | list_del_init(&dentry->d_lru); | |
635 | } | |
636 | /* | |
637 | * move only zero ref count dentries to the end | |
638 | * of the unused list for prune_dcache | |
639 | */ | |
640 | if (!atomic_read(&dentry->d_count)) { | |
8e13059a | 641 | list_add_tail(&dentry->d_lru, &dentry_unused); |
1da177e4 LT |
642 | dentry_stat.nr_unused++; |
643 | found++; | |
644 | } | |
645 | ||
646 | /* | |
647 | * We can return to the caller if we have found some (this | |
648 | * ensures forward progress). We'll be coming back to find | |
649 | * the rest. | |
650 | */ | |
651 | if (found && need_resched()) | |
652 | goto out; | |
653 | ||
654 | /* | |
655 | * Descend a level if the d_subdirs list is non-empty. | |
656 | */ | |
657 | if (!list_empty(&dentry->d_subdirs)) { | |
658 | this_parent = dentry; | |
1da177e4 LT |
659 | goto repeat; |
660 | } | |
661 | } | |
662 | /* | |
663 | * All done at this level ... ascend and resume the search. | |
664 | */ | |
665 | if (this_parent != parent) { | |
5160ee6f | 666 | next = this_parent->d_u.d_child.next; |
1da177e4 | 667 | this_parent = this_parent->d_parent; |
1da177e4 LT |
668 | goto resume; |
669 | } | |
670 | out: | |
671 | spin_unlock(&dcache_lock); | |
672 | return found; | |
673 | } | |
674 | ||
675 | /** | |
676 | * shrink_dcache_parent - prune dcache | |
677 | * @parent: parent of entries to prune | |
678 | * | |
679 | * Prune the dcache to remove unused children of the parent dentry. | |
680 | */ | |
681 | ||
682 | void shrink_dcache_parent(struct dentry * parent) | |
683 | { | |
684 | int found; | |
685 | ||
686 | while ((found = select_parent(parent)) != 0) | |
0feae5c4 | 687 | prune_dcache(found, parent->d_sb); |
1da177e4 LT |
688 | } |
689 | ||
1da177e4 LT |
690 | /* |
691 | * Scan `nr' dentries and return the number which remain. | |
692 | * | |
693 | * We need to avoid reentering the filesystem if the caller is performing a | |
694 | * GFP_NOFS allocation attempt. One example deadlock is: | |
695 | * | |
696 | * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache-> | |
697 | * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->put_inode-> | |
698 | * ext2_discard_prealloc->ext2_free_blocks->lock_super->DEADLOCK. | |
699 | * | |
700 | * In this case we return -1 to tell the caller that we baled. | |
701 | */ | |
27496a8c | 702 | static int shrink_dcache_memory(int nr, gfp_t gfp_mask) |
1da177e4 LT |
703 | { |
704 | if (nr) { | |
705 | if (!(gfp_mask & __GFP_FS)) | |
706 | return -1; | |
0feae5c4 | 707 | prune_dcache(nr, NULL); |
1da177e4 LT |
708 | } |
709 | return (dentry_stat.nr_unused / 100) * sysctl_vfs_cache_pressure; | |
710 | } | |
711 | ||
712 | /** | |
713 | * d_alloc - allocate a dcache entry | |
714 | * @parent: parent of entry to allocate | |
715 | * @name: qstr of the name | |
716 | * | |
717 | * Allocates a dentry. It returns %NULL if there is insufficient memory | |
718 | * available. On a success the dentry is returned. The name passed in is | |
719 | * copied and the copy passed in may be reused after this call. | |
720 | */ | |
721 | ||
722 | struct dentry *d_alloc(struct dentry * parent, const struct qstr *name) | |
723 | { | |
724 | struct dentry *dentry; | |
725 | char *dname; | |
726 | ||
727 | dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL); | |
728 | if (!dentry) | |
729 | return NULL; | |
730 | ||
731 | if (name->len > DNAME_INLINE_LEN-1) { | |
732 | dname = kmalloc(name->len + 1, GFP_KERNEL); | |
733 | if (!dname) { | |
734 | kmem_cache_free(dentry_cache, dentry); | |
735 | return NULL; | |
736 | } | |
737 | } else { | |
738 | dname = dentry->d_iname; | |
739 | } | |
740 | dentry->d_name.name = dname; | |
741 | ||
742 | dentry->d_name.len = name->len; | |
743 | dentry->d_name.hash = name->hash; | |
744 | memcpy(dname, name->name, name->len); | |
745 | dname[name->len] = 0; | |
746 | ||
747 | atomic_set(&dentry->d_count, 1); | |
748 | dentry->d_flags = DCACHE_UNHASHED; | |
749 | spin_lock_init(&dentry->d_lock); | |
750 | dentry->d_inode = NULL; | |
751 | dentry->d_parent = NULL; | |
752 | dentry->d_sb = NULL; | |
753 | dentry->d_op = NULL; | |
754 | dentry->d_fsdata = NULL; | |
755 | dentry->d_mounted = 0; | |
47ba87e0 | 756 | #ifdef CONFIG_PROFILING |
1da177e4 | 757 | dentry->d_cookie = NULL; |
47ba87e0 | 758 | #endif |
1da177e4 LT |
759 | INIT_HLIST_NODE(&dentry->d_hash); |
760 | INIT_LIST_HEAD(&dentry->d_lru); | |
761 | INIT_LIST_HEAD(&dentry->d_subdirs); | |
762 | INIT_LIST_HEAD(&dentry->d_alias); | |
763 | ||
764 | if (parent) { | |
765 | dentry->d_parent = dget(parent); | |
766 | dentry->d_sb = parent->d_sb; | |
767 | } else { | |
5160ee6f | 768 | INIT_LIST_HEAD(&dentry->d_u.d_child); |
1da177e4 LT |
769 | } |
770 | ||
771 | spin_lock(&dcache_lock); | |
772 | if (parent) | |
5160ee6f | 773 | list_add(&dentry->d_u.d_child, &parent->d_subdirs); |
1da177e4 LT |
774 | dentry_stat.nr_dentry++; |
775 | spin_unlock(&dcache_lock); | |
776 | ||
777 | return dentry; | |
778 | } | |
779 | ||
780 | struct dentry *d_alloc_name(struct dentry *parent, const char *name) | |
781 | { | |
782 | struct qstr q; | |
783 | ||
784 | q.name = name; | |
785 | q.len = strlen(name); | |
786 | q.hash = full_name_hash(q.name, q.len); | |
787 | return d_alloc(parent, &q); | |
788 | } | |
789 | ||
790 | /** | |
791 | * d_instantiate - fill in inode information for a dentry | |
792 | * @entry: dentry to complete | |
793 | * @inode: inode to attach to this dentry | |
794 | * | |
795 | * Fill in inode information in the entry. | |
796 | * | |
797 | * This turns negative dentries into productive full members | |
798 | * of society. | |
799 | * | |
800 | * NOTE! This assumes that the inode count has been incremented | |
801 | * (or otherwise set) by the caller to indicate that it is now | |
802 | * in use by the dcache. | |
803 | */ | |
804 | ||
805 | void d_instantiate(struct dentry *entry, struct inode * inode) | |
806 | { | |
28133c7b | 807 | BUG_ON(!list_empty(&entry->d_alias)); |
1da177e4 LT |
808 | spin_lock(&dcache_lock); |
809 | if (inode) | |
810 | list_add(&entry->d_alias, &inode->i_dentry); | |
811 | entry->d_inode = inode; | |
c32ccd87 | 812 | fsnotify_d_instantiate(entry, inode); |
1da177e4 LT |
813 | spin_unlock(&dcache_lock); |
814 | security_d_instantiate(entry, inode); | |
815 | } | |
816 | ||
817 | /** | |
818 | * d_instantiate_unique - instantiate a non-aliased dentry | |
819 | * @entry: dentry to instantiate | |
820 | * @inode: inode to attach to this dentry | |
821 | * | |
822 | * Fill in inode information in the entry. On success, it returns NULL. | |
823 | * If an unhashed alias of "entry" already exists, then we return the | |
e866cfa9 | 824 | * aliased dentry instead and drop one reference to inode. |
1da177e4 LT |
825 | * |
826 | * Note that in order to avoid conflicts with rename() etc, the caller | |
827 | * had better be holding the parent directory semaphore. | |
e866cfa9 OD |
828 | * |
829 | * This also assumes that the inode count has been incremented | |
830 | * (or otherwise set) by the caller to indicate that it is now | |
831 | * in use by the dcache. | |
1da177e4 | 832 | */ |
770bfad8 DH |
833 | static struct dentry *__d_instantiate_unique(struct dentry *entry, |
834 | struct inode *inode) | |
1da177e4 LT |
835 | { |
836 | struct dentry *alias; | |
837 | int len = entry->d_name.len; | |
838 | const char *name = entry->d_name.name; | |
839 | unsigned int hash = entry->d_name.hash; | |
840 | ||
770bfad8 DH |
841 | if (!inode) { |
842 | entry->d_inode = NULL; | |
843 | return NULL; | |
844 | } | |
845 | ||
1da177e4 LT |
846 | list_for_each_entry(alias, &inode->i_dentry, d_alias) { |
847 | struct qstr *qstr = &alias->d_name; | |
848 | ||
849 | if (qstr->hash != hash) | |
850 | continue; | |
851 | if (alias->d_parent != entry->d_parent) | |
852 | continue; | |
853 | if (qstr->len != len) | |
854 | continue; | |
855 | if (memcmp(qstr->name, name, len)) | |
856 | continue; | |
857 | dget_locked(alias); | |
1da177e4 LT |
858 | return alias; |
859 | } | |
770bfad8 | 860 | |
1da177e4 | 861 | list_add(&entry->d_alias, &inode->i_dentry); |
1da177e4 | 862 | entry->d_inode = inode; |
c32ccd87 | 863 | fsnotify_d_instantiate(entry, inode); |
1da177e4 LT |
864 | return NULL; |
865 | } | |
770bfad8 DH |
866 | |
867 | struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode) | |
868 | { | |
869 | struct dentry *result; | |
870 | ||
871 | BUG_ON(!list_empty(&entry->d_alias)); | |
872 | ||
873 | spin_lock(&dcache_lock); | |
874 | result = __d_instantiate_unique(entry, inode); | |
875 | spin_unlock(&dcache_lock); | |
876 | ||
877 | if (!result) { | |
878 | security_d_instantiate(entry, inode); | |
879 | return NULL; | |
880 | } | |
881 | ||
882 | BUG_ON(!d_unhashed(result)); | |
883 | iput(inode); | |
884 | return result; | |
885 | } | |
886 | ||
1da177e4 LT |
887 | EXPORT_SYMBOL(d_instantiate_unique); |
888 | ||
889 | /** | |
890 | * d_alloc_root - allocate root dentry | |
891 | * @root_inode: inode to allocate the root for | |
892 | * | |
893 | * Allocate a root ("/") dentry for the inode given. The inode is | |
894 | * instantiated and returned. %NULL is returned if there is insufficient | |
895 | * memory or the inode passed is %NULL. | |
896 | */ | |
897 | ||
898 | struct dentry * d_alloc_root(struct inode * root_inode) | |
899 | { | |
900 | struct dentry *res = NULL; | |
901 | ||
902 | if (root_inode) { | |
903 | static const struct qstr name = { .name = "/", .len = 1 }; | |
904 | ||
905 | res = d_alloc(NULL, &name); | |
906 | if (res) { | |
907 | res->d_sb = root_inode->i_sb; | |
908 | res->d_parent = res; | |
909 | d_instantiate(res, root_inode); | |
910 | } | |
911 | } | |
912 | return res; | |
913 | } | |
914 | ||
915 | static inline struct hlist_head *d_hash(struct dentry *parent, | |
916 | unsigned long hash) | |
917 | { | |
918 | hash += ((unsigned long) parent ^ GOLDEN_RATIO_PRIME) / L1_CACHE_BYTES; | |
919 | hash = hash ^ ((hash ^ GOLDEN_RATIO_PRIME) >> D_HASHBITS); | |
920 | return dentry_hashtable + (hash & D_HASHMASK); | |
921 | } | |
922 | ||
923 | /** | |
924 | * d_alloc_anon - allocate an anonymous dentry | |
925 | * @inode: inode to allocate the dentry for | |
926 | * | |
927 | * This is similar to d_alloc_root. It is used by filesystems when | |
928 | * creating a dentry for a given inode, often in the process of | |
929 | * mapping a filehandle to a dentry. The returned dentry may be | |
930 | * anonymous, or may have a full name (if the inode was already | |
931 | * in the cache). The file system may need to make further | |
932 | * efforts to connect this dentry into the dcache properly. | |
933 | * | |
934 | * When called on a directory inode, we must ensure that | |
935 | * the inode only ever has one dentry. If a dentry is | |
936 | * found, that is returned instead of allocating a new one. | |
937 | * | |
938 | * On successful return, the reference to the inode has been transferred | |
939 | * to the dentry. If %NULL is returned (indicating kmalloc failure), | |
940 | * the reference on the inode has not been released. | |
941 | */ | |
942 | ||
943 | struct dentry * d_alloc_anon(struct inode *inode) | |
944 | { | |
945 | static const struct qstr anonstring = { .name = "" }; | |
946 | struct dentry *tmp; | |
947 | struct dentry *res; | |
948 | ||
949 | if ((res = d_find_alias(inode))) { | |
950 | iput(inode); | |
951 | return res; | |
952 | } | |
953 | ||
954 | tmp = d_alloc(NULL, &anonstring); | |
955 | if (!tmp) | |
956 | return NULL; | |
957 | ||
958 | tmp->d_parent = tmp; /* make sure dput doesn't croak */ | |
959 | ||
960 | spin_lock(&dcache_lock); | |
961 | res = __d_find_alias(inode, 0); | |
962 | if (!res) { | |
963 | /* attach a disconnected dentry */ | |
964 | res = tmp; | |
965 | tmp = NULL; | |
966 | spin_lock(&res->d_lock); | |
967 | res->d_sb = inode->i_sb; | |
968 | res->d_parent = res; | |
969 | res->d_inode = inode; | |
970 | res->d_flags |= DCACHE_DISCONNECTED; | |
971 | res->d_flags &= ~DCACHE_UNHASHED; | |
972 | list_add(&res->d_alias, &inode->i_dentry); | |
973 | hlist_add_head(&res->d_hash, &inode->i_sb->s_anon); | |
974 | spin_unlock(&res->d_lock); | |
975 | ||
976 | inode = NULL; /* don't drop reference */ | |
977 | } | |
978 | spin_unlock(&dcache_lock); | |
979 | ||
980 | if (inode) | |
981 | iput(inode); | |
982 | if (tmp) | |
983 | dput(tmp); | |
984 | return res; | |
985 | } | |
986 | ||
987 | ||
988 | /** | |
989 | * d_splice_alias - splice a disconnected dentry into the tree if one exists | |
990 | * @inode: the inode which may have a disconnected dentry | |
991 | * @dentry: a negative dentry which we want to point to the inode. | |
992 | * | |
993 | * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and | |
994 | * DCACHE_DISCONNECTED), then d_move that in place of the given dentry | |
995 | * and return it, else simply d_add the inode to the dentry and return NULL. | |
996 | * | |
997 | * This is needed in the lookup routine of any filesystem that is exportable | |
998 | * (via knfsd) so that we can build dcache paths to directories effectively. | |
999 | * | |
1000 | * If a dentry was found and moved, then it is returned. Otherwise NULL | |
1001 | * is returned. This matches the expected return value of ->lookup. | |
1002 | * | |
1003 | */ | |
1004 | struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry) | |
1005 | { | |
1006 | struct dentry *new = NULL; | |
1007 | ||
21c0d8fd | 1008 | if (inode && S_ISDIR(inode->i_mode)) { |
1da177e4 LT |
1009 | spin_lock(&dcache_lock); |
1010 | new = __d_find_alias(inode, 1); | |
1011 | if (new) { | |
1012 | BUG_ON(!(new->d_flags & DCACHE_DISCONNECTED)); | |
c32ccd87 | 1013 | fsnotify_d_instantiate(new, inode); |
1da177e4 LT |
1014 | spin_unlock(&dcache_lock); |
1015 | security_d_instantiate(new, inode); | |
1016 | d_rehash(dentry); | |
1017 | d_move(new, dentry); | |
1018 | iput(inode); | |
1019 | } else { | |
1020 | /* d_instantiate takes dcache_lock, so we do it by hand */ | |
1021 | list_add(&dentry->d_alias, &inode->i_dentry); | |
1022 | dentry->d_inode = inode; | |
c32ccd87 | 1023 | fsnotify_d_instantiate(dentry, inode); |
1da177e4 LT |
1024 | spin_unlock(&dcache_lock); |
1025 | security_d_instantiate(dentry, inode); | |
1026 | d_rehash(dentry); | |
1027 | } | |
1028 | } else | |
1029 | d_add(dentry, inode); | |
1030 | return new; | |
1031 | } | |
1032 | ||
1033 | ||
1034 | /** | |
1035 | * d_lookup - search for a dentry | |
1036 | * @parent: parent dentry | |
1037 | * @name: qstr of name we wish to find | |
1038 | * | |
1039 | * Searches the children of the parent dentry for the name in question. If | |
1040 | * the dentry is found its reference count is incremented and the dentry | |
1041 | * is returned. The caller must use d_put to free the entry when it has | |
1042 | * finished using it. %NULL is returned on failure. | |
1043 | * | |
1044 | * __d_lookup is dcache_lock free. The hash list is protected using RCU. | |
1045 | * Memory barriers are used while updating and doing lockless traversal. | |
1046 | * To avoid races with d_move while rename is happening, d_lock is used. | |
1047 | * | |
1048 | * Overflows in memcmp(), while d_move, are avoided by keeping the length | |
1049 | * and name pointer in one structure pointed by d_qstr. | |
1050 | * | |
1051 | * rcu_read_lock() and rcu_read_unlock() are used to disable preemption while | |
1052 | * lookup is going on. | |
1053 | * | |
1054 | * dentry_unused list is not updated even if lookup finds the required dentry | |
1055 | * in there. It is updated in places such as prune_dcache, shrink_dcache_sb, | |
1056 | * select_parent and __dget_locked. This laziness saves lookup from dcache_lock | |
1057 | * acquisition. | |
1058 | * | |
1059 | * d_lookup() is protected against the concurrent renames in some unrelated | |
1060 | * directory using the seqlockt_t rename_lock. | |
1061 | */ | |
1062 | ||
1063 | struct dentry * d_lookup(struct dentry * parent, struct qstr * name) | |
1064 | { | |
1065 | struct dentry * dentry = NULL; | |
1066 | unsigned long seq; | |
1067 | ||
1068 | do { | |
1069 | seq = read_seqbegin(&rename_lock); | |
1070 | dentry = __d_lookup(parent, name); | |
1071 | if (dentry) | |
1072 | break; | |
1073 | } while (read_seqretry(&rename_lock, seq)); | |
1074 | return dentry; | |
1075 | } | |
1076 | ||
1077 | struct dentry * __d_lookup(struct dentry * parent, struct qstr * name) | |
1078 | { | |
1079 | unsigned int len = name->len; | |
1080 | unsigned int hash = name->hash; | |
1081 | const unsigned char *str = name->name; | |
1082 | struct hlist_head *head = d_hash(parent,hash); | |
1083 | struct dentry *found = NULL; | |
1084 | struct hlist_node *node; | |
665a7583 | 1085 | struct dentry *dentry; |
1da177e4 LT |
1086 | |
1087 | rcu_read_lock(); | |
1088 | ||
665a7583 | 1089 | hlist_for_each_entry_rcu(dentry, node, head, d_hash) { |
1da177e4 LT |
1090 | struct qstr *qstr; |
1091 | ||
1da177e4 LT |
1092 | if (dentry->d_name.hash != hash) |
1093 | continue; | |
1094 | if (dentry->d_parent != parent) | |
1095 | continue; | |
1096 | ||
1097 | spin_lock(&dentry->d_lock); | |
1098 | ||
1099 | /* | |
1100 | * Recheck the dentry after taking the lock - d_move may have | |
1101 | * changed things. Don't bother checking the hash because we're | |
1102 | * about to compare the whole name anyway. | |
1103 | */ | |
1104 | if (dentry->d_parent != parent) | |
1105 | goto next; | |
1106 | ||
1107 | /* | |
1108 | * It is safe to compare names since d_move() cannot | |
1109 | * change the qstr (protected by d_lock). | |
1110 | */ | |
1111 | qstr = &dentry->d_name; | |
1112 | if (parent->d_op && parent->d_op->d_compare) { | |
1113 | if (parent->d_op->d_compare(parent, qstr, name)) | |
1114 | goto next; | |
1115 | } else { | |
1116 | if (qstr->len != len) | |
1117 | goto next; | |
1118 | if (memcmp(qstr->name, str, len)) | |
1119 | goto next; | |
1120 | } | |
1121 | ||
1122 | if (!d_unhashed(dentry)) { | |
1123 | atomic_inc(&dentry->d_count); | |
1124 | found = dentry; | |
1125 | } | |
1126 | spin_unlock(&dentry->d_lock); | |
1127 | break; | |
1128 | next: | |
1129 | spin_unlock(&dentry->d_lock); | |
1130 | } | |
1131 | rcu_read_unlock(); | |
1132 | ||
1133 | return found; | |
1134 | } | |
1135 | ||
3e7e241f EB |
1136 | /** |
1137 | * d_hash_and_lookup - hash the qstr then search for a dentry | |
1138 | * @dir: Directory to search in | |
1139 | * @name: qstr of name we wish to find | |
1140 | * | |
1141 | * On hash failure or on lookup failure NULL is returned. | |
1142 | */ | |
1143 | struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name) | |
1144 | { | |
1145 | struct dentry *dentry = NULL; | |
1146 | ||
1147 | /* | |
1148 | * Check for a fs-specific hash function. Note that we must | |
1149 | * calculate the standard hash first, as the d_op->d_hash() | |
1150 | * routine may choose to leave the hash value unchanged. | |
1151 | */ | |
1152 | name->hash = full_name_hash(name->name, name->len); | |
1153 | if (dir->d_op && dir->d_op->d_hash) { | |
1154 | if (dir->d_op->d_hash(dir, name) < 0) | |
1155 | goto out; | |
1156 | } | |
1157 | dentry = d_lookup(dir, name); | |
1158 | out: | |
1159 | return dentry; | |
1160 | } | |
1161 | ||
1da177e4 LT |
1162 | /** |
1163 | * d_validate - verify dentry provided from insecure source | |
1164 | * @dentry: The dentry alleged to be valid child of @dparent | |
1165 | * @dparent: The parent dentry (known to be valid) | |
1166 | * @hash: Hash of the dentry | |
1167 | * @len: Length of the name | |
1168 | * | |
1169 | * An insecure source has sent us a dentry, here we verify it and dget() it. | |
1170 | * This is used by ncpfs in its readdir implementation. | |
1171 | * Zero is returned in the dentry is invalid. | |
1172 | */ | |
1173 | ||
1174 | int d_validate(struct dentry *dentry, struct dentry *dparent) | |
1175 | { | |
1176 | struct hlist_head *base; | |
1177 | struct hlist_node *lhp; | |
1178 | ||
1179 | /* Check whether the ptr might be valid at all.. */ | |
1180 | if (!kmem_ptr_validate(dentry_cache, dentry)) | |
1181 | goto out; | |
1182 | ||
1183 | if (dentry->d_parent != dparent) | |
1184 | goto out; | |
1185 | ||
1186 | spin_lock(&dcache_lock); | |
1187 | base = d_hash(dparent, dentry->d_name.hash); | |
1188 | hlist_for_each(lhp,base) { | |
665a7583 | 1189 | /* hlist_for_each_entry_rcu() not required for d_hash list |
1da177e4 LT |
1190 | * as it is parsed under dcache_lock |
1191 | */ | |
1192 | if (dentry == hlist_entry(lhp, struct dentry, d_hash)) { | |
1193 | __dget_locked(dentry); | |
1194 | spin_unlock(&dcache_lock); | |
1195 | return 1; | |
1196 | } | |
1197 | } | |
1198 | spin_unlock(&dcache_lock); | |
1199 | out: | |
1200 | return 0; | |
1201 | } | |
1202 | ||
1203 | /* | |
1204 | * When a file is deleted, we have two options: | |
1205 | * - turn this dentry into a negative dentry | |
1206 | * - unhash this dentry and free it. | |
1207 | * | |
1208 | * Usually, we want to just turn this into | |
1209 | * a negative dentry, but if anybody else is | |
1210 | * currently using the dentry or the inode | |
1211 | * we can't do that and we fall back on removing | |
1212 | * it from the hash queues and waiting for | |
1213 | * it to be deleted later when it has no users | |
1214 | */ | |
1215 | ||
1216 | /** | |
1217 | * d_delete - delete a dentry | |
1218 | * @dentry: The dentry to delete | |
1219 | * | |
1220 | * Turn the dentry into a negative dentry if possible, otherwise | |
1221 | * remove it from the hash queues so it can be deleted later | |
1222 | */ | |
1223 | ||
1224 | void d_delete(struct dentry * dentry) | |
1225 | { | |
7a91bf7f | 1226 | int isdir = 0; |
1da177e4 LT |
1227 | /* |
1228 | * Are we the only user? | |
1229 | */ | |
1230 | spin_lock(&dcache_lock); | |
1231 | spin_lock(&dentry->d_lock); | |
7a91bf7f | 1232 | isdir = S_ISDIR(dentry->d_inode->i_mode); |
1da177e4 LT |
1233 | if (atomic_read(&dentry->d_count) == 1) { |
1234 | dentry_iput(dentry); | |
7a91bf7f | 1235 | fsnotify_nameremove(dentry, isdir); |
7a2bd3f7 AG |
1236 | |
1237 | /* remove this and other inotify debug checks after 2.6.18 */ | |
1238 | dentry->d_flags &= ~DCACHE_INOTIFY_PARENT_WATCHED; | |
1da177e4 LT |
1239 | return; |
1240 | } | |
1241 | ||
1242 | if (!d_unhashed(dentry)) | |
1243 | __d_drop(dentry); | |
1244 | ||
1245 | spin_unlock(&dentry->d_lock); | |
1246 | spin_unlock(&dcache_lock); | |
7a91bf7f JM |
1247 | |
1248 | fsnotify_nameremove(dentry, isdir); | |
1da177e4 LT |
1249 | } |
1250 | ||
1251 | static void __d_rehash(struct dentry * entry, struct hlist_head *list) | |
1252 | { | |
1253 | ||
1254 | entry->d_flags &= ~DCACHE_UNHASHED; | |
1255 | hlist_add_head_rcu(&entry->d_hash, list); | |
1256 | } | |
1257 | ||
770bfad8 DH |
1258 | static void _d_rehash(struct dentry * entry) |
1259 | { | |
1260 | __d_rehash(entry, d_hash(entry->d_parent, entry->d_name.hash)); | |
1261 | } | |
1262 | ||
1da177e4 LT |
1263 | /** |
1264 | * d_rehash - add an entry back to the hash | |
1265 | * @entry: dentry to add to the hash | |
1266 | * | |
1267 | * Adds a dentry to the hash according to its name. | |
1268 | */ | |
1269 | ||
1270 | void d_rehash(struct dentry * entry) | |
1271 | { | |
1da177e4 LT |
1272 | spin_lock(&dcache_lock); |
1273 | spin_lock(&entry->d_lock); | |
770bfad8 | 1274 | _d_rehash(entry); |
1da177e4 LT |
1275 | spin_unlock(&entry->d_lock); |
1276 | spin_unlock(&dcache_lock); | |
1277 | } | |
1278 | ||
1279 | #define do_switch(x,y) do { \ | |
1280 | __typeof__ (x) __tmp = x; \ | |
1281 | x = y; y = __tmp; } while (0) | |
1282 | ||
1283 | /* | |
1284 | * When switching names, the actual string doesn't strictly have to | |
1285 | * be preserved in the target - because we're dropping the target | |
1286 | * anyway. As such, we can just do a simple memcpy() to copy over | |
1287 | * the new name before we switch. | |
1288 | * | |
1289 | * Note that we have to be a lot more careful about getting the hash | |
1290 | * switched - we have to switch the hash value properly even if it | |
1291 | * then no longer matches the actual (corrupted) string of the target. | |
1292 | * The hash value has to match the hash queue that the dentry is on.. | |
1293 | */ | |
1294 | static void switch_names(struct dentry *dentry, struct dentry *target) | |
1295 | { | |
1296 | if (dname_external(target)) { | |
1297 | if (dname_external(dentry)) { | |
1298 | /* | |
1299 | * Both external: swap the pointers | |
1300 | */ | |
1301 | do_switch(target->d_name.name, dentry->d_name.name); | |
1302 | } else { | |
1303 | /* | |
1304 | * dentry:internal, target:external. Steal target's | |
1305 | * storage and make target internal. | |
1306 | */ | |
1307 | dentry->d_name.name = target->d_name.name; | |
1308 | target->d_name.name = target->d_iname; | |
1309 | } | |
1310 | } else { | |
1311 | if (dname_external(dentry)) { | |
1312 | /* | |
1313 | * dentry:external, target:internal. Give dentry's | |
1314 | * storage to target and make dentry internal | |
1315 | */ | |
1316 | memcpy(dentry->d_iname, target->d_name.name, | |
1317 | target->d_name.len + 1); | |
1318 | target->d_name.name = dentry->d_name.name; | |
1319 | dentry->d_name.name = dentry->d_iname; | |
1320 | } else { | |
1321 | /* | |
1322 | * Both are internal. Just copy target to dentry | |
1323 | */ | |
1324 | memcpy(dentry->d_iname, target->d_name.name, | |
1325 | target->d_name.len + 1); | |
1326 | } | |
1327 | } | |
1328 | } | |
1329 | ||
1330 | /* | |
1331 | * We cannibalize "target" when moving dentry on top of it, | |
1332 | * because it's going to be thrown away anyway. We could be more | |
1333 | * polite about it, though. | |
1334 | * | |
1335 | * This forceful removal will result in ugly /proc output if | |
1336 | * somebody holds a file open that got deleted due to a rename. | |
1337 | * We could be nicer about the deleted file, and let it show | |
1338 | * up under the name it got deleted rather than the name that | |
1339 | * deleted it. | |
1340 | */ | |
1341 | ||
1342 | /** | |
1343 | * d_move - move a dentry | |
1344 | * @dentry: entry to move | |
1345 | * @target: new dentry | |
1346 | * | |
1347 | * Update the dcache to reflect the move of a file name. Negative | |
1348 | * dcache entries should not be moved in this way. | |
1349 | */ | |
1350 | ||
1351 | void d_move(struct dentry * dentry, struct dentry * target) | |
1352 | { | |
1353 | struct hlist_head *list; | |
1354 | ||
1355 | if (!dentry->d_inode) | |
1356 | printk(KERN_WARNING "VFS: moving negative dcache entry\n"); | |
1357 | ||
1358 | spin_lock(&dcache_lock); | |
1359 | write_seqlock(&rename_lock); | |
1360 | /* | |
1361 | * XXXX: do we really need to take target->d_lock? | |
1362 | */ | |
1363 | if (target < dentry) { | |
1364 | spin_lock(&target->d_lock); | |
a90b9c05 | 1365 | spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); |
1da177e4 LT |
1366 | } else { |
1367 | spin_lock(&dentry->d_lock); | |
a90b9c05 | 1368 | spin_lock_nested(&target->d_lock, DENTRY_D_LOCK_NESTED); |
1da177e4 LT |
1369 | } |
1370 | ||
1371 | /* Move the dentry to the target hash queue, if on different bucket */ | |
1372 | if (dentry->d_flags & DCACHE_UNHASHED) | |
1373 | goto already_unhashed; | |
1374 | ||
1375 | hlist_del_rcu(&dentry->d_hash); | |
1376 | ||
1377 | already_unhashed: | |
1378 | list = d_hash(target->d_parent, target->d_name.hash); | |
1379 | __d_rehash(dentry, list); | |
1380 | ||
1381 | /* Unhash the target: dput() will then get rid of it */ | |
1382 | __d_drop(target); | |
1383 | ||
5160ee6f ED |
1384 | list_del(&dentry->d_u.d_child); |
1385 | list_del(&target->d_u.d_child); | |
1da177e4 LT |
1386 | |
1387 | /* Switch the names.. */ | |
1388 | switch_names(dentry, target); | |
1389 | do_switch(dentry->d_name.len, target->d_name.len); | |
1390 | do_switch(dentry->d_name.hash, target->d_name.hash); | |
1391 | ||
1392 | /* ... and switch the parents */ | |
1393 | if (IS_ROOT(dentry)) { | |
1394 | dentry->d_parent = target->d_parent; | |
1395 | target->d_parent = target; | |
5160ee6f | 1396 | INIT_LIST_HEAD(&target->d_u.d_child); |
1da177e4 LT |
1397 | } else { |
1398 | do_switch(dentry->d_parent, target->d_parent); | |
1399 | ||
1400 | /* And add them back to the (new) parent lists */ | |
5160ee6f | 1401 | list_add(&target->d_u.d_child, &target->d_parent->d_subdirs); |
1da177e4 LT |
1402 | } |
1403 | ||
5160ee6f | 1404 | list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs); |
1da177e4 | 1405 | spin_unlock(&target->d_lock); |
c32ccd87 | 1406 | fsnotify_d_move(dentry); |
1da177e4 LT |
1407 | spin_unlock(&dentry->d_lock); |
1408 | write_sequnlock(&rename_lock); | |
1409 | spin_unlock(&dcache_lock); | |
1410 | } | |
1411 | ||
770bfad8 DH |
1412 | /* |
1413 | * Prepare an anonymous dentry for life in the superblock's dentry tree as a | |
1414 | * named dentry in place of the dentry to be replaced. | |
1415 | */ | |
1416 | static void __d_materialise_dentry(struct dentry *dentry, struct dentry *anon) | |
1417 | { | |
1418 | struct dentry *dparent, *aparent; | |
1419 | ||
1420 | switch_names(dentry, anon); | |
1421 | do_switch(dentry->d_name.len, anon->d_name.len); | |
1422 | do_switch(dentry->d_name.hash, anon->d_name.hash); | |
1423 | ||
1424 | dparent = dentry->d_parent; | |
1425 | aparent = anon->d_parent; | |
1426 | ||
1427 | dentry->d_parent = (aparent == anon) ? dentry : aparent; | |
1428 | list_del(&dentry->d_u.d_child); | |
1429 | if (!IS_ROOT(dentry)) | |
1430 | list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs); | |
1431 | else | |
1432 | INIT_LIST_HEAD(&dentry->d_u.d_child); | |
1433 | ||
1434 | anon->d_parent = (dparent == dentry) ? anon : dparent; | |
1435 | list_del(&anon->d_u.d_child); | |
1436 | if (!IS_ROOT(anon)) | |
1437 | list_add(&anon->d_u.d_child, &anon->d_parent->d_subdirs); | |
1438 | else | |
1439 | INIT_LIST_HEAD(&anon->d_u.d_child); | |
1440 | ||
1441 | anon->d_flags &= ~DCACHE_DISCONNECTED; | |
1442 | } | |
1443 | ||
1444 | /** | |
1445 | * d_materialise_unique - introduce an inode into the tree | |
1446 | * @dentry: candidate dentry | |
1447 | * @inode: inode to bind to the dentry, to which aliases may be attached | |
1448 | * | |
1449 | * Introduces an dentry into the tree, substituting an extant disconnected | |
1450 | * root directory alias in its place if there is one | |
1451 | */ | |
1452 | struct dentry *d_materialise_unique(struct dentry *dentry, struct inode *inode) | |
1453 | { | |
1454 | struct dentry *alias, *actual; | |
1455 | ||
1456 | BUG_ON(!d_unhashed(dentry)); | |
1457 | ||
1458 | spin_lock(&dcache_lock); | |
1459 | ||
1460 | if (!inode) { | |
1461 | actual = dentry; | |
1462 | dentry->d_inode = NULL; | |
1463 | goto found_lock; | |
1464 | } | |
1465 | ||
1466 | /* See if a disconnected directory already exists as an anonymous root | |
1467 | * that we should splice into the tree instead */ | |
1468 | if (S_ISDIR(inode->i_mode) && (alias = __d_find_alias(inode, 1))) { | |
1469 | spin_lock(&alias->d_lock); | |
1470 | ||
1471 | /* Is this a mountpoint that we could splice into our tree? */ | |
1472 | if (IS_ROOT(alias)) | |
1473 | goto connect_mountpoint; | |
1474 | ||
1475 | if (alias->d_name.len == dentry->d_name.len && | |
1476 | alias->d_parent == dentry->d_parent && | |
1477 | memcmp(alias->d_name.name, | |
1478 | dentry->d_name.name, | |
1479 | dentry->d_name.len) == 0) | |
1480 | goto replace_with_alias; | |
1481 | ||
1482 | spin_unlock(&alias->d_lock); | |
1483 | ||
1484 | /* Doh! Seem to be aliasing directories for some reason... */ | |
1485 | dput(alias); | |
1486 | } | |
1487 | ||
1488 | /* Add a unique reference */ | |
1489 | actual = __d_instantiate_unique(dentry, inode); | |
1490 | if (!actual) | |
1491 | actual = dentry; | |
1492 | else if (unlikely(!d_unhashed(actual))) | |
1493 | goto shouldnt_be_hashed; | |
1494 | ||
1495 | found_lock: | |
1496 | spin_lock(&actual->d_lock); | |
1497 | found: | |
1498 | _d_rehash(actual); | |
1499 | spin_unlock(&actual->d_lock); | |
1500 | spin_unlock(&dcache_lock); | |
1501 | ||
1502 | if (actual == dentry) { | |
1503 | security_d_instantiate(dentry, inode); | |
1504 | return NULL; | |
1505 | } | |
1506 | ||
1507 | iput(inode); | |
1508 | return actual; | |
1509 | ||
1510 | /* Convert the anonymous/root alias into an ordinary dentry */ | |
1511 | connect_mountpoint: | |
1512 | __d_materialise_dentry(dentry, alias); | |
1513 | ||
1514 | /* Replace the candidate dentry with the alias in the tree */ | |
1515 | replace_with_alias: | |
1516 | __d_drop(alias); | |
1517 | actual = alias; | |
1518 | goto found; | |
1519 | ||
1520 | shouldnt_be_hashed: | |
1521 | spin_unlock(&dcache_lock); | |
1522 | BUG(); | |
1523 | goto shouldnt_be_hashed; | |
1524 | } | |
1525 | ||
1da177e4 LT |
1526 | /** |
1527 | * d_path - return the path of a dentry | |
1528 | * @dentry: dentry to report | |
1529 | * @vfsmnt: vfsmnt to which the dentry belongs | |
1530 | * @root: root dentry | |
1531 | * @rootmnt: vfsmnt to which the root dentry belongs | |
1532 | * @buffer: buffer to return value in | |
1533 | * @buflen: buffer length | |
1534 | * | |
1535 | * Convert a dentry into an ASCII path name. If the entry has been deleted | |
1536 | * the string " (deleted)" is appended. Note that this is ambiguous. | |
1537 | * | |
1538 | * Returns the buffer or an error code if the path was too long. | |
1539 | * | |
1540 | * "buflen" should be positive. Caller holds the dcache_lock. | |
1541 | */ | |
1542 | static char * __d_path( struct dentry *dentry, struct vfsmount *vfsmnt, | |
1543 | struct dentry *root, struct vfsmount *rootmnt, | |
1544 | char *buffer, int buflen) | |
1545 | { | |
1546 | char * end = buffer+buflen; | |
1547 | char * retval; | |
1548 | int namelen; | |
1549 | ||
1550 | *--end = '\0'; | |
1551 | buflen--; | |
1552 | if (!IS_ROOT(dentry) && d_unhashed(dentry)) { | |
1553 | buflen -= 10; | |
1554 | end -= 10; | |
1555 | if (buflen < 0) | |
1556 | goto Elong; | |
1557 | memcpy(end, " (deleted)", 10); | |
1558 | } | |
1559 | ||
1560 | if (buflen < 1) | |
1561 | goto Elong; | |
1562 | /* Get '/' right */ | |
1563 | retval = end-1; | |
1564 | *retval = '/'; | |
1565 | ||
1566 | for (;;) { | |
1567 | struct dentry * parent; | |
1568 | ||
1569 | if (dentry == root && vfsmnt == rootmnt) | |
1570 | break; | |
1571 | if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) { | |
1572 | /* Global root? */ | |
1573 | spin_lock(&vfsmount_lock); | |
1574 | if (vfsmnt->mnt_parent == vfsmnt) { | |
1575 | spin_unlock(&vfsmount_lock); | |
1576 | goto global_root; | |
1577 | } | |
1578 | dentry = vfsmnt->mnt_mountpoint; | |
1579 | vfsmnt = vfsmnt->mnt_parent; | |
1580 | spin_unlock(&vfsmount_lock); | |
1581 | continue; | |
1582 | } | |
1583 | parent = dentry->d_parent; | |
1584 | prefetch(parent); | |
1585 | namelen = dentry->d_name.len; | |
1586 | buflen -= namelen + 1; | |
1587 | if (buflen < 0) | |
1588 | goto Elong; | |
1589 | end -= namelen; | |
1590 | memcpy(end, dentry->d_name.name, namelen); | |
1591 | *--end = '/'; | |
1592 | retval = end; | |
1593 | dentry = parent; | |
1594 | } | |
1595 | ||
1596 | return retval; | |
1597 | ||
1598 | global_root: | |
1599 | namelen = dentry->d_name.len; | |
1600 | buflen -= namelen; | |
1601 | if (buflen < 0) | |
1602 | goto Elong; | |
1603 | retval -= namelen-1; /* hit the slash */ | |
1604 | memcpy(retval, dentry->d_name.name, namelen); | |
1605 | return retval; | |
1606 | Elong: | |
1607 | return ERR_PTR(-ENAMETOOLONG); | |
1608 | } | |
1609 | ||
1610 | /* write full pathname into buffer and return start of pathname */ | |
1611 | char * d_path(struct dentry *dentry, struct vfsmount *vfsmnt, | |
1612 | char *buf, int buflen) | |
1613 | { | |
1614 | char *res; | |
1615 | struct vfsmount *rootmnt; | |
1616 | struct dentry *root; | |
1617 | ||
1618 | read_lock(¤t->fs->lock); | |
1619 | rootmnt = mntget(current->fs->rootmnt); | |
1620 | root = dget(current->fs->root); | |
1621 | read_unlock(¤t->fs->lock); | |
1622 | spin_lock(&dcache_lock); | |
1623 | res = __d_path(dentry, vfsmnt, root, rootmnt, buf, buflen); | |
1624 | spin_unlock(&dcache_lock); | |
1625 | dput(root); | |
1626 | mntput(rootmnt); | |
1627 | return res; | |
1628 | } | |
1629 | ||
1630 | /* | |
1631 | * NOTE! The user-level library version returns a | |
1632 | * character pointer. The kernel system call just | |
1633 | * returns the length of the buffer filled (which | |
1634 | * includes the ending '\0' character), or a negative | |
1635 | * error value. So libc would do something like | |
1636 | * | |
1637 | * char *getcwd(char * buf, size_t size) | |
1638 | * { | |
1639 | * int retval; | |
1640 | * | |
1641 | * retval = sys_getcwd(buf, size); | |
1642 | * if (retval >= 0) | |
1643 | * return buf; | |
1644 | * errno = -retval; | |
1645 | * return NULL; | |
1646 | * } | |
1647 | */ | |
1648 | asmlinkage long sys_getcwd(char __user *buf, unsigned long size) | |
1649 | { | |
1650 | int error; | |
1651 | struct vfsmount *pwdmnt, *rootmnt; | |
1652 | struct dentry *pwd, *root; | |
1653 | char *page = (char *) __get_free_page(GFP_USER); | |
1654 | ||
1655 | if (!page) | |
1656 | return -ENOMEM; | |
1657 | ||
1658 | read_lock(¤t->fs->lock); | |
1659 | pwdmnt = mntget(current->fs->pwdmnt); | |
1660 | pwd = dget(current->fs->pwd); | |
1661 | rootmnt = mntget(current->fs->rootmnt); | |
1662 | root = dget(current->fs->root); | |
1663 | read_unlock(¤t->fs->lock); | |
1664 | ||
1665 | error = -ENOENT; | |
1666 | /* Has the current directory has been unlinked? */ | |
1667 | spin_lock(&dcache_lock); | |
1668 | if (pwd->d_parent == pwd || !d_unhashed(pwd)) { | |
1669 | unsigned long len; | |
1670 | char * cwd; | |
1671 | ||
1672 | cwd = __d_path(pwd, pwdmnt, root, rootmnt, page, PAGE_SIZE); | |
1673 | spin_unlock(&dcache_lock); | |
1674 | ||
1675 | error = PTR_ERR(cwd); | |
1676 | if (IS_ERR(cwd)) | |
1677 | goto out; | |
1678 | ||
1679 | error = -ERANGE; | |
1680 | len = PAGE_SIZE + page - cwd; | |
1681 | if (len <= size) { | |
1682 | error = len; | |
1683 | if (copy_to_user(buf, cwd, len)) | |
1684 | error = -EFAULT; | |
1685 | } | |
1686 | } else | |
1687 | spin_unlock(&dcache_lock); | |
1688 | ||
1689 | out: | |
1690 | dput(pwd); | |
1691 | mntput(pwdmnt); | |
1692 | dput(root); | |
1693 | mntput(rootmnt); | |
1694 | free_page((unsigned long) page); | |
1695 | return error; | |
1696 | } | |
1697 | ||
1698 | /* | |
1699 | * Test whether new_dentry is a subdirectory of old_dentry. | |
1700 | * | |
1701 | * Trivially implemented using the dcache structure | |
1702 | */ | |
1703 | ||
1704 | /** | |
1705 | * is_subdir - is new dentry a subdirectory of old_dentry | |
1706 | * @new_dentry: new dentry | |
1707 | * @old_dentry: old dentry | |
1708 | * | |
1709 | * Returns 1 if new_dentry is a subdirectory of the parent (at any depth). | |
1710 | * Returns 0 otherwise. | |
1711 | * Caller must ensure that "new_dentry" is pinned before calling is_subdir() | |
1712 | */ | |
1713 | ||
1714 | int is_subdir(struct dentry * new_dentry, struct dentry * old_dentry) | |
1715 | { | |
1716 | int result; | |
1717 | struct dentry * saved = new_dentry; | |
1718 | unsigned long seq; | |
1719 | ||
1720 | /* need rcu_readlock to protect against the d_parent trashing due to | |
1721 | * d_move | |
1722 | */ | |
1723 | rcu_read_lock(); | |
1724 | do { | |
1725 | /* for restarting inner loop in case of seq retry */ | |
1726 | new_dentry = saved; | |
1727 | result = 0; | |
1728 | seq = read_seqbegin(&rename_lock); | |
1729 | for (;;) { | |
1730 | if (new_dentry != old_dentry) { | |
1731 | struct dentry * parent = new_dentry->d_parent; | |
1732 | if (parent == new_dentry) | |
1733 | break; | |
1734 | new_dentry = parent; | |
1735 | continue; | |
1736 | } | |
1737 | result = 1; | |
1738 | break; | |
1739 | } | |
1740 | } while (read_seqretry(&rename_lock, seq)); | |
1741 | rcu_read_unlock(); | |
1742 | ||
1743 | return result; | |
1744 | } | |
1745 | ||
1746 | void d_genocide(struct dentry *root) | |
1747 | { | |
1748 | struct dentry *this_parent = root; | |
1749 | struct list_head *next; | |
1750 | ||
1751 | spin_lock(&dcache_lock); | |
1752 | repeat: | |
1753 | next = this_parent->d_subdirs.next; | |
1754 | resume: | |
1755 | while (next != &this_parent->d_subdirs) { | |
1756 | struct list_head *tmp = next; | |
5160ee6f | 1757 | struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child); |
1da177e4 LT |
1758 | next = tmp->next; |
1759 | if (d_unhashed(dentry)||!dentry->d_inode) | |
1760 | continue; | |
1761 | if (!list_empty(&dentry->d_subdirs)) { | |
1762 | this_parent = dentry; | |
1763 | goto repeat; | |
1764 | } | |
1765 | atomic_dec(&dentry->d_count); | |
1766 | } | |
1767 | if (this_parent != root) { | |
5160ee6f | 1768 | next = this_parent->d_u.d_child.next; |
1da177e4 LT |
1769 | atomic_dec(&this_parent->d_count); |
1770 | this_parent = this_parent->d_parent; | |
1771 | goto resume; | |
1772 | } | |
1773 | spin_unlock(&dcache_lock); | |
1774 | } | |
1775 | ||
1776 | /** | |
1777 | * find_inode_number - check for dentry with name | |
1778 | * @dir: directory to check | |
1779 | * @name: Name to find. | |
1780 | * | |
1781 | * Check whether a dentry already exists for the given name, | |
1782 | * and return the inode number if it has an inode. Otherwise | |
1783 | * 0 is returned. | |
1784 | * | |
1785 | * This routine is used to post-process directory listings for | |
1786 | * filesystems using synthetic inode numbers, and is necessary | |
1787 | * to keep getcwd() working. | |
1788 | */ | |
1789 | ||
1790 | ino_t find_inode_number(struct dentry *dir, struct qstr *name) | |
1791 | { | |
1792 | struct dentry * dentry; | |
1793 | ino_t ino = 0; | |
1794 | ||
3e7e241f EB |
1795 | dentry = d_hash_and_lookup(dir, name); |
1796 | if (dentry) { | |
1da177e4 LT |
1797 | if (dentry->d_inode) |
1798 | ino = dentry->d_inode->i_ino; | |
1799 | dput(dentry); | |
1800 | } | |
1da177e4 LT |
1801 | return ino; |
1802 | } | |
1803 | ||
1804 | static __initdata unsigned long dhash_entries; | |
1805 | static int __init set_dhash_entries(char *str) | |
1806 | { | |
1807 | if (!str) | |
1808 | return 0; | |
1809 | dhash_entries = simple_strtoul(str, &str, 0); | |
1810 | return 1; | |
1811 | } | |
1812 | __setup("dhash_entries=", set_dhash_entries); | |
1813 | ||
1814 | static void __init dcache_init_early(void) | |
1815 | { | |
1816 | int loop; | |
1817 | ||
1818 | /* If hashes are distributed across NUMA nodes, defer | |
1819 | * hash allocation until vmalloc space is available. | |
1820 | */ | |
1821 | if (hashdist) | |
1822 | return; | |
1823 | ||
1824 | dentry_hashtable = | |
1825 | alloc_large_system_hash("Dentry cache", | |
1826 | sizeof(struct hlist_head), | |
1827 | dhash_entries, | |
1828 | 13, | |
1829 | HASH_EARLY, | |
1830 | &d_hash_shift, | |
1831 | &d_hash_mask, | |
1832 | 0); | |
1833 | ||
1834 | for (loop = 0; loop < (1 << d_hash_shift); loop++) | |
1835 | INIT_HLIST_HEAD(&dentry_hashtable[loop]); | |
1836 | } | |
1837 | ||
1838 | static void __init dcache_init(unsigned long mempages) | |
1839 | { | |
1840 | int loop; | |
1841 | ||
1842 | /* | |
1843 | * A constructor could be added for stable state like the lists, | |
1844 | * but it is probably not worth it because of the cache nature | |
1845 | * of the dcache. | |
1846 | */ | |
1847 | dentry_cache = kmem_cache_create("dentry_cache", | |
1848 | sizeof(struct dentry), | |
1849 | 0, | |
b0196009 PJ |
1850 | (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC| |
1851 | SLAB_MEM_SPREAD), | |
1da177e4 LT |
1852 | NULL, NULL); |
1853 | ||
1854 | set_shrinker(DEFAULT_SEEKS, shrink_dcache_memory); | |
1855 | ||
1856 | /* Hash may have been set up in dcache_init_early */ | |
1857 | if (!hashdist) | |
1858 | return; | |
1859 | ||
1860 | dentry_hashtable = | |
1861 | alloc_large_system_hash("Dentry cache", | |
1862 | sizeof(struct hlist_head), | |
1863 | dhash_entries, | |
1864 | 13, | |
1865 | 0, | |
1866 | &d_hash_shift, | |
1867 | &d_hash_mask, | |
1868 | 0); | |
1869 | ||
1870 | for (loop = 0; loop < (1 << d_hash_shift); loop++) | |
1871 | INIT_HLIST_HEAD(&dentry_hashtable[loop]); | |
1872 | } | |
1873 | ||
1874 | /* SLAB cache for __getname() consumers */ | |
fa3536cc | 1875 | kmem_cache_t *names_cachep __read_mostly; |
1da177e4 LT |
1876 | |
1877 | /* SLAB cache for file structures */ | |
fa3536cc | 1878 | kmem_cache_t *filp_cachep __read_mostly; |
1da177e4 LT |
1879 | |
1880 | EXPORT_SYMBOL(d_genocide); | |
1881 | ||
1da177e4 LT |
1882 | void __init vfs_caches_init_early(void) |
1883 | { | |
1884 | dcache_init_early(); | |
1885 | inode_init_early(); | |
1886 | } | |
1887 | ||
1888 | void __init vfs_caches_init(unsigned long mempages) | |
1889 | { | |
1890 | unsigned long reserve; | |
1891 | ||
1892 | /* Base hash sizes on available memory, with a reserve equal to | |
1893 | 150% of current kernel size */ | |
1894 | ||
1895 | reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1); | |
1896 | mempages -= reserve; | |
1897 | ||
1898 | names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0, | |
1899 | SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL); | |
1900 | ||
1901 | filp_cachep = kmem_cache_create("filp", sizeof(struct file), 0, | |
529bf6be | 1902 | SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL); |
1da177e4 LT |
1903 | |
1904 | dcache_init(mempages); | |
1905 | inode_init(mempages); | |
1906 | files_init(mempages); | |
1907 | mnt_init(mempages); | |
1908 | bdev_cache_init(); | |
1909 | chrdev_init(); | |
1910 | } | |
1911 | ||
1912 | EXPORT_SYMBOL(d_alloc); | |
1913 | EXPORT_SYMBOL(d_alloc_anon); | |
1914 | EXPORT_SYMBOL(d_alloc_root); | |
1915 | EXPORT_SYMBOL(d_delete); | |
1916 | EXPORT_SYMBOL(d_find_alias); | |
1917 | EXPORT_SYMBOL(d_instantiate); | |
1918 | EXPORT_SYMBOL(d_invalidate); | |
1919 | EXPORT_SYMBOL(d_lookup); | |
1920 | EXPORT_SYMBOL(d_move); | |
770bfad8 | 1921 | EXPORT_SYMBOL_GPL(d_materialise_unique); |
1da177e4 LT |
1922 | EXPORT_SYMBOL(d_path); |
1923 | EXPORT_SYMBOL(d_prune_aliases); | |
1924 | EXPORT_SYMBOL(d_rehash); | |
1925 | EXPORT_SYMBOL(d_splice_alias); | |
1926 | EXPORT_SYMBOL(d_validate); | |
1927 | EXPORT_SYMBOL(dget_locked); | |
1928 | EXPORT_SYMBOL(dput); | |
1929 | EXPORT_SYMBOL(find_inode_number); | |
1930 | EXPORT_SYMBOL(have_submounts); | |
1931 | EXPORT_SYMBOL(names_cachep); | |
1932 | EXPORT_SYMBOL(shrink_dcache_parent); | |
1933 | EXPORT_SYMBOL(shrink_dcache_sb); |