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); | |
6eac3f93 VA |
481 | /* |
482 | * Insert dentry at the head of the list as inserting at the | |
483 | * tail leads to a cycle. | |
0feae5c4 | 484 | */ |
6eac3f93 VA |
485 | list_add(&dentry->d_lru, &dentry_unused); |
486 | dentry_stat.nr_unused++; | |
1da177e4 LT |
487 | } |
488 | spin_unlock(&dcache_lock); | |
489 | } | |
490 | ||
491 | /* | |
492 | * Shrink the dcache for the specified super block. | |
493 | * This allows us to unmount a device without disturbing | |
494 | * the dcache for the other devices. | |
495 | * | |
496 | * This implementation makes just two traversals of the | |
497 | * unused list. On the first pass we move the selected | |
498 | * dentries to the most recent end, and on the second | |
499 | * pass we free them. The second pass must restart after | |
500 | * each dput(), but since the target dentries are all at | |
501 | * the end, it's really just a single traversal. | |
502 | */ | |
503 | ||
504 | /** | |
505 | * shrink_dcache_sb - shrink dcache for a superblock | |
506 | * @sb: superblock | |
507 | * | |
508 | * Shrink the dcache for the specified super block. This | |
509 | * is used to free the dcache before unmounting a file | |
510 | * system | |
511 | */ | |
512 | ||
513 | void shrink_dcache_sb(struct super_block * sb) | |
514 | { | |
515 | struct list_head *tmp, *next; | |
516 | struct dentry *dentry; | |
517 | ||
518 | /* | |
519 | * Pass one ... move the dentries for the specified | |
520 | * superblock to the most recent end of the unused list. | |
521 | */ | |
522 | spin_lock(&dcache_lock); | |
0cdca3f9 | 523 | list_for_each_safe(tmp, next, &dentry_unused) { |
1da177e4 LT |
524 | dentry = list_entry(tmp, struct dentry, d_lru); |
525 | if (dentry->d_sb != sb) | |
526 | continue; | |
1bfba4e8 | 527 | list_move(tmp, &dentry_unused); |
1da177e4 LT |
528 | } |
529 | ||
530 | /* | |
531 | * Pass two ... free the dentries for this superblock. | |
532 | */ | |
533 | repeat: | |
0cdca3f9 | 534 | list_for_each_safe(tmp, next, &dentry_unused) { |
1da177e4 LT |
535 | dentry = list_entry(tmp, struct dentry, d_lru); |
536 | if (dentry->d_sb != sb) | |
537 | continue; | |
538 | dentry_stat.nr_unused--; | |
539 | list_del_init(tmp); | |
540 | spin_lock(&dentry->d_lock); | |
541 | if (atomic_read(&dentry->d_count)) { | |
542 | spin_unlock(&dentry->d_lock); | |
543 | continue; | |
544 | } | |
545 | prune_one_dentry(dentry); | |
2ab13460 | 546 | cond_resched_lock(&dcache_lock); |
1da177e4 LT |
547 | goto repeat; |
548 | } | |
549 | spin_unlock(&dcache_lock); | |
550 | } | |
551 | ||
c636ebdb DH |
552 | /* |
553 | * destroy a single subtree of dentries for unmount | |
554 | * - see the comments on shrink_dcache_for_umount() for a description of the | |
555 | * locking | |
556 | */ | |
557 | static void shrink_dcache_for_umount_subtree(struct dentry *dentry) | |
558 | { | |
559 | struct dentry *parent; | |
f8713576 | 560 | unsigned detached = 0; |
c636ebdb DH |
561 | |
562 | BUG_ON(!IS_ROOT(dentry)); | |
563 | ||
564 | /* detach this root from the system */ | |
565 | spin_lock(&dcache_lock); | |
566 | if (!list_empty(&dentry->d_lru)) { | |
567 | dentry_stat.nr_unused--; | |
568 | list_del_init(&dentry->d_lru); | |
569 | } | |
570 | __d_drop(dentry); | |
571 | spin_unlock(&dcache_lock); | |
572 | ||
573 | for (;;) { | |
574 | /* descend to the first leaf in the current subtree */ | |
575 | while (!list_empty(&dentry->d_subdirs)) { | |
576 | struct dentry *loop; | |
577 | ||
578 | /* this is a branch with children - detach all of them | |
579 | * from the system in one go */ | |
580 | spin_lock(&dcache_lock); | |
581 | list_for_each_entry(loop, &dentry->d_subdirs, | |
582 | d_u.d_child) { | |
583 | if (!list_empty(&loop->d_lru)) { | |
584 | dentry_stat.nr_unused--; | |
585 | list_del_init(&loop->d_lru); | |
586 | } | |
587 | ||
588 | __d_drop(loop); | |
589 | cond_resched_lock(&dcache_lock); | |
590 | } | |
591 | spin_unlock(&dcache_lock); | |
592 | ||
593 | /* move to the first child */ | |
594 | dentry = list_entry(dentry->d_subdirs.next, | |
595 | struct dentry, d_u.d_child); | |
596 | } | |
597 | ||
598 | /* consume the dentries from this leaf up through its parents | |
599 | * until we find one with children or run out altogether */ | |
600 | do { | |
601 | struct inode *inode; | |
602 | ||
603 | if (atomic_read(&dentry->d_count) != 0) { | |
604 | printk(KERN_ERR | |
605 | "BUG: Dentry %p{i=%lx,n=%s}" | |
606 | " still in use (%d)" | |
607 | " [unmount of %s %s]\n", | |
608 | dentry, | |
609 | dentry->d_inode ? | |
610 | dentry->d_inode->i_ino : 0UL, | |
611 | dentry->d_name.name, | |
612 | atomic_read(&dentry->d_count), | |
613 | dentry->d_sb->s_type->name, | |
614 | dentry->d_sb->s_id); | |
615 | BUG(); | |
616 | } | |
617 | ||
618 | parent = dentry->d_parent; | |
619 | if (parent == dentry) | |
620 | parent = NULL; | |
621 | else | |
622 | atomic_dec(&parent->d_count); | |
623 | ||
624 | list_del(&dentry->d_u.d_child); | |
f8713576 | 625 | detached++; |
c636ebdb DH |
626 | |
627 | inode = dentry->d_inode; | |
628 | if (inode) { | |
629 | dentry->d_inode = NULL; | |
630 | list_del_init(&dentry->d_alias); | |
631 | if (dentry->d_op && dentry->d_op->d_iput) | |
632 | dentry->d_op->d_iput(dentry, inode); | |
633 | else | |
634 | iput(inode); | |
635 | } | |
636 | ||
637 | d_free(dentry); | |
638 | ||
639 | /* finished when we fall off the top of the tree, | |
640 | * otherwise we ascend to the parent and move to the | |
641 | * next sibling if there is one */ | |
642 | if (!parent) | |
f8713576 | 643 | goto out; |
c636ebdb DH |
644 | |
645 | dentry = parent; | |
646 | ||
647 | } while (list_empty(&dentry->d_subdirs)); | |
648 | ||
649 | dentry = list_entry(dentry->d_subdirs.next, | |
650 | struct dentry, d_u.d_child); | |
651 | } | |
f8713576 DH |
652 | out: |
653 | /* several dentries were freed, need to correct nr_dentry */ | |
654 | spin_lock(&dcache_lock); | |
655 | dentry_stat.nr_dentry -= detached; | |
656 | spin_unlock(&dcache_lock); | |
c636ebdb DH |
657 | } |
658 | ||
659 | /* | |
660 | * destroy the dentries attached to a superblock on unmounting | |
661 | * - we don't need to use dentry->d_lock, and only need dcache_lock when | |
662 | * removing the dentry from the system lists and hashes because: | |
663 | * - the superblock is detached from all mountings and open files, so the | |
664 | * dentry trees will not be rearranged by the VFS | |
665 | * - s_umount is write-locked, so the memory pressure shrinker will ignore | |
666 | * any dentries belonging to this superblock that it comes across | |
667 | * - the filesystem itself is no longer permitted to rearrange the dentries | |
668 | * in this superblock | |
669 | */ | |
670 | void shrink_dcache_for_umount(struct super_block *sb) | |
671 | { | |
672 | struct dentry *dentry; | |
673 | ||
674 | if (down_read_trylock(&sb->s_umount)) | |
675 | BUG(); | |
676 | ||
677 | dentry = sb->s_root; | |
678 | sb->s_root = NULL; | |
679 | atomic_dec(&dentry->d_count); | |
680 | shrink_dcache_for_umount_subtree(dentry); | |
681 | ||
682 | while (!hlist_empty(&sb->s_anon)) { | |
683 | dentry = hlist_entry(sb->s_anon.first, struct dentry, d_hash); | |
684 | shrink_dcache_for_umount_subtree(dentry); | |
685 | } | |
686 | } | |
687 | ||
1da177e4 LT |
688 | /* |
689 | * Search for at least 1 mount point in the dentry's subdirs. | |
690 | * We descend to the next level whenever the d_subdirs | |
691 | * list is non-empty and continue searching. | |
692 | */ | |
693 | ||
694 | /** | |
695 | * have_submounts - check for mounts over a dentry | |
696 | * @parent: dentry to check. | |
697 | * | |
698 | * Return true if the parent or its subdirectories contain | |
699 | * a mount point | |
700 | */ | |
701 | ||
702 | int have_submounts(struct dentry *parent) | |
703 | { | |
704 | struct dentry *this_parent = parent; | |
705 | struct list_head *next; | |
706 | ||
707 | spin_lock(&dcache_lock); | |
708 | if (d_mountpoint(parent)) | |
709 | goto positive; | |
710 | repeat: | |
711 | next = this_parent->d_subdirs.next; | |
712 | resume: | |
713 | while (next != &this_parent->d_subdirs) { | |
714 | struct list_head *tmp = next; | |
5160ee6f | 715 | struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child); |
1da177e4 LT |
716 | next = tmp->next; |
717 | /* Have we found a mount point ? */ | |
718 | if (d_mountpoint(dentry)) | |
719 | goto positive; | |
720 | if (!list_empty(&dentry->d_subdirs)) { | |
721 | this_parent = dentry; | |
722 | goto repeat; | |
723 | } | |
724 | } | |
725 | /* | |
726 | * All done at this level ... ascend and resume the search. | |
727 | */ | |
728 | if (this_parent != parent) { | |
5160ee6f | 729 | next = this_parent->d_u.d_child.next; |
1da177e4 LT |
730 | this_parent = this_parent->d_parent; |
731 | goto resume; | |
732 | } | |
733 | spin_unlock(&dcache_lock); | |
734 | return 0; /* No mount points found in tree */ | |
735 | positive: | |
736 | spin_unlock(&dcache_lock); | |
737 | return 1; | |
738 | } | |
739 | ||
740 | /* | |
741 | * Search the dentry child list for the specified parent, | |
742 | * and move any unused dentries to the end of the unused | |
743 | * list for prune_dcache(). We descend to the next level | |
744 | * whenever the d_subdirs list is non-empty and continue | |
745 | * searching. | |
746 | * | |
747 | * It returns zero iff there are no unused children, | |
748 | * otherwise it returns the number of children moved to | |
749 | * the end of the unused list. This may not be the total | |
750 | * number of unused children, because select_parent can | |
751 | * drop the lock and return early due to latency | |
752 | * constraints. | |
753 | */ | |
754 | static int select_parent(struct dentry * parent) | |
755 | { | |
756 | struct dentry *this_parent = parent; | |
757 | struct list_head *next; | |
758 | int found = 0; | |
759 | ||
760 | spin_lock(&dcache_lock); | |
761 | repeat: | |
762 | next = this_parent->d_subdirs.next; | |
763 | resume: | |
764 | while (next != &this_parent->d_subdirs) { | |
765 | struct list_head *tmp = next; | |
5160ee6f | 766 | struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child); |
1da177e4 LT |
767 | next = tmp->next; |
768 | ||
769 | if (!list_empty(&dentry->d_lru)) { | |
770 | dentry_stat.nr_unused--; | |
771 | list_del_init(&dentry->d_lru); | |
772 | } | |
773 | /* | |
774 | * move only zero ref count dentries to the end | |
775 | * of the unused list for prune_dcache | |
776 | */ | |
777 | if (!atomic_read(&dentry->d_count)) { | |
8e13059a | 778 | list_add_tail(&dentry->d_lru, &dentry_unused); |
1da177e4 LT |
779 | dentry_stat.nr_unused++; |
780 | found++; | |
781 | } | |
782 | ||
783 | /* | |
784 | * We can return to the caller if we have found some (this | |
785 | * ensures forward progress). We'll be coming back to find | |
786 | * the rest. | |
787 | */ | |
788 | if (found && need_resched()) | |
789 | goto out; | |
790 | ||
791 | /* | |
792 | * Descend a level if the d_subdirs list is non-empty. | |
793 | */ | |
794 | if (!list_empty(&dentry->d_subdirs)) { | |
795 | this_parent = dentry; | |
1da177e4 LT |
796 | goto repeat; |
797 | } | |
798 | } | |
799 | /* | |
800 | * All done at this level ... ascend and resume the search. | |
801 | */ | |
802 | if (this_parent != parent) { | |
5160ee6f | 803 | next = this_parent->d_u.d_child.next; |
1da177e4 | 804 | this_parent = this_parent->d_parent; |
1da177e4 LT |
805 | goto resume; |
806 | } | |
807 | out: | |
808 | spin_unlock(&dcache_lock); | |
809 | return found; | |
810 | } | |
811 | ||
812 | /** | |
813 | * shrink_dcache_parent - prune dcache | |
814 | * @parent: parent of entries to prune | |
815 | * | |
816 | * Prune the dcache to remove unused children of the parent dentry. | |
817 | */ | |
818 | ||
819 | void shrink_dcache_parent(struct dentry * parent) | |
820 | { | |
821 | int found; | |
822 | ||
823 | while ((found = select_parent(parent)) != 0) | |
0feae5c4 | 824 | prune_dcache(found, parent->d_sb); |
1da177e4 LT |
825 | } |
826 | ||
1da177e4 LT |
827 | /* |
828 | * Scan `nr' dentries and return the number which remain. | |
829 | * | |
830 | * We need to avoid reentering the filesystem if the caller is performing a | |
831 | * GFP_NOFS allocation attempt. One example deadlock is: | |
832 | * | |
833 | * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache-> | |
834 | * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->put_inode-> | |
835 | * ext2_discard_prealloc->ext2_free_blocks->lock_super->DEADLOCK. | |
836 | * | |
837 | * In this case we return -1 to tell the caller that we baled. | |
838 | */ | |
27496a8c | 839 | static int shrink_dcache_memory(int nr, gfp_t gfp_mask) |
1da177e4 LT |
840 | { |
841 | if (nr) { | |
842 | if (!(gfp_mask & __GFP_FS)) | |
843 | return -1; | |
0feae5c4 | 844 | prune_dcache(nr, NULL); |
1da177e4 LT |
845 | } |
846 | return (dentry_stat.nr_unused / 100) * sysctl_vfs_cache_pressure; | |
847 | } | |
848 | ||
849 | /** | |
850 | * d_alloc - allocate a dcache entry | |
851 | * @parent: parent of entry to allocate | |
852 | * @name: qstr of the name | |
853 | * | |
854 | * Allocates a dentry. It returns %NULL if there is insufficient memory | |
855 | * available. On a success the dentry is returned. The name passed in is | |
856 | * copied and the copy passed in may be reused after this call. | |
857 | */ | |
858 | ||
859 | struct dentry *d_alloc(struct dentry * parent, const struct qstr *name) | |
860 | { | |
861 | struct dentry *dentry; | |
862 | char *dname; | |
863 | ||
864 | dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL); | |
865 | if (!dentry) | |
866 | return NULL; | |
867 | ||
868 | if (name->len > DNAME_INLINE_LEN-1) { | |
869 | dname = kmalloc(name->len + 1, GFP_KERNEL); | |
870 | if (!dname) { | |
871 | kmem_cache_free(dentry_cache, dentry); | |
872 | return NULL; | |
873 | } | |
874 | } else { | |
875 | dname = dentry->d_iname; | |
876 | } | |
877 | dentry->d_name.name = dname; | |
878 | ||
879 | dentry->d_name.len = name->len; | |
880 | dentry->d_name.hash = name->hash; | |
881 | memcpy(dname, name->name, name->len); | |
882 | dname[name->len] = 0; | |
883 | ||
884 | atomic_set(&dentry->d_count, 1); | |
885 | dentry->d_flags = DCACHE_UNHASHED; | |
886 | spin_lock_init(&dentry->d_lock); | |
887 | dentry->d_inode = NULL; | |
888 | dentry->d_parent = NULL; | |
889 | dentry->d_sb = NULL; | |
890 | dentry->d_op = NULL; | |
891 | dentry->d_fsdata = NULL; | |
892 | dentry->d_mounted = 0; | |
47ba87e0 | 893 | #ifdef CONFIG_PROFILING |
1da177e4 | 894 | dentry->d_cookie = NULL; |
47ba87e0 | 895 | #endif |
1da177e4 LT |
896 | INIT_HLIST_NODE(&dentry->d_hash); |
897 | INIT_LIST_HEAD(&dentry->d_lru); | |
898 | INIT_LIST_HEAD(&dentry->d_subdirs); | |
899 | INIT_LIST_HEAD(&dentry->d_alias); | |
900 | ||
901 | if (parent) { | |
902 | dentry->d_parent = dget(parent); | |
903 | dentry->d_sb = parent->d_sb; | |
904 | } else { | |
5160ee6f | 905 | INIT_LIST_HEAD(&dentry->d_u.d_child); |
1da177e4 LT |
906 | } |
907 | ||
908 | spin_lock(&dcache_lock); | |
909 | if (parent) | |
5160ee6f | 910 | list_add(&dentry->d_u.d_child, &parent->d_subdirs); |
1da177e4 LT |
911 | dentry_stat.nr_dentry++; |
912 | spin_unlock(&dcache_lock); | |
913 | ||
914 | return dentry; | |
915 | } | |
916 | ||
917 | struct dentry *d_alloc_name(struct dentry *parent, const char *name) | |
918 | { | |
919 | struct qstr q; | |
920 | ||
921 | q.name = name; | |
922 | q.len = strlen(name); | |
923 | q.hash = full_name_hash(q.name, q.len); | |
924 | return d_alloc(parent, &q); | |
925 | } | |
926 | ||
927 | /** | |
928 | * d_instantiate - fill in inode information for a dentry | |
929 | * @entry: dentry to complete | |
930 | * @inode: inode to attach to this dentry | |
931 | * | |
932 | * Fill in inode information in the entry. | |
933 | * | |
934 | * This turns negative dentries into productive full members | |
935 | * of society. | |
936 | * | |
937 | * NOTE! This assumes that the inode count has been incremented | |
938 | * (or otherwise set) by the caller to indicate that it is now | |
939 | * in use by the dcache. | |
940 | */ | |
941 | ||
942 | void d_instantiate(struct dentry *entry, struct inode * inode) | |
943 | { | |
28133c7b | 944 | BUG_ON(!list_empty(&entry->d_alias)); |
1da177e4 LT |
945 | spin_lock(&dcache_lock); |
946 | if (inode) | |
947 | list_add(&entry->d_alias, &inode->i_dentry); | |
948 | entry->d_inode = inode; | |
c32ccd87 | 949 | fsnotify_d_instantiate(entry, inode); |
1da177e4 LT |
950 | spin_unlock(&dcache_lock); |
951 | security_d_instantiate(entry, inode); | |
952 | } | |
953 | ||
954 | /** | |
955 | * d_instantiate_unique - instantiate a non-aliased dentry | |
956 | * @entry: dentry to instantiate | |
957 | * @inode: inode to attach to this dentry | |
958 | * | |
959 | * Fill in inode information in the entry. On success, it returns NULL. | |
960 | * If an unhashed alias of "entry" already exists, then we return the | |
e866cfa9 | 961 | * aliased dentry instead and drop one reference to inode. |
1da177e4 LT |
962 | * |
963 | * Note that in order to avoid conflicts with rename() etc, the caller | |
964 | * had better be holding the parent directory semaphore. | |
e866cfa9 OD |
965 | * |
966 | * This also assumes that the inode count has been incremented | |
967 | * (or otherwise set) by the caller to indicate that it is now | |
968 | * in use by the dcache. | |
1da177e4 | 969 | */ |
770bfad8 DH |
970 | static struct dentry *__d_instantiate_unique(struct dentry *entry, |
971 | struct inode *inode) | |
1da177e4 LT |
972 | { |
973 | struct dentry *alias; | |
974 | int len = entry->d_name.len; | |
975 | const char *name = entry->d_name.name; | |
976 | unsigned int hash = entry->d_name.hash; | |
977 | ||
770bfad8 DH |
978 | if (!inode) { |
979 | entry->d_inode = NULL; | |
980 | return NULL; | |
981 | } | |
982 | ||
1da177e4 LT |
983 | list_for_each_entry(alias, &inode->i_dentry, d_alias) { |
984 | struct qstr *qstr = &alias->d_name; | |
985 | ||
986 | if (qstr->hash != hash) | |
987 | continue; | |
988 | if (alias->d_parent != entry->d_parent) | |
989 | continue; | |
990 | if (qstr->len != len) | |
991 | continue; | |
992 | if (memcmp(qstr->name, name, len)) | |
993 | continue; | |
994 | dget_locked(alias); | |
1da177e4 LT |
995 | return alias; |
996 | } | |
770bfad8 | 997 | |
1da177e4 | 998 | list_add(&entry->d_alias, &inode->i_dentry); |
1da177e4 | 999 | entry->d_inode = inode; |
c32ccd87 | 1000 | fsnotify_d_instantiate(entry, inode); |
1da177e4 LT |
1001 | return NULL; |
1002 | } | |
770bfad8 DH |
1003 | |
1004 | struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode) | |
1005 | { | |
1006 | struct dentry *result; | |
1007 | ||
1008 | BUG_ON(!list_empty(&entry->d_alias)); | |
1009 | ||
1010 | spin_lock(&dcache_lock); | |
1011 | result = __d_instantiate_unique(entry, inode); | |
1012 | spin_unlock(&dcache_lock); | |
1013 | ||
1014 | if (!result) { | |
1015 | security_d_instantiate(entry, inode); | |
1016 | return NULL; | |
1017 | } | |
1018 | ||
1019 | BUG_ON(!d_unhashed(result)); | |
1020 | iput(inode); | |
1021 | return result; | |
1022 | } | |
1023 | ||
1da177e4 LT |
1024 | EXPORT_SYMBOL(d_instantiate_unique); |
1025 | ||
1026 | /** | |
1027 | * d_alloc_root - allocate root dentry | |
1028 | * @root_inode: inode to allocate the root for | |
1029 | * | |
1030 | * Allocate a root ("/") dentry for the inode given. The inode is | |
1031 | * instantiated and returned. %NULL is returned if there is insufficient | |
1032 | * memory or the inode passed is %NULL. | |
1033 | */ | |
1034 | ||
1035 | struct dentry * d_alloc_root(struct inode * root_inode) | |
1036 | { | |
1037 | struct dentry *res = NULL; | |
1038 | ||
1039 | if (root_inode) { | |
1040 | static const struct qstr name = { .name = "/", .len = 1 }; | |
1041 | ||
1042 | res = d_alloc(NULL, &name); | |
1043 | if (res) { | |
1044 | res->d_sb = root_inode->i_sb; | |
1045 | res->d_parent = res; | |
1046 | d_instantiate(res, root_inode); | |
1047 | } | |
1048 | } | |
1049 | return res; | |
1050 | } | |
1051 | ||
1052 | static inline struct hlist_head *d_hash(struct dentry *parent, | |
1053 | unsigned long hash) | |
1054 | { | |
1055 | hash += ((unsigned long) parent ^ GOLDEN_RATIO_PRIME) / L1_CACHE_BYTES; | |
1056 | hash = hash ^ ((hash ^ GOLDEN_RATIO_PRIME) >> D_HASHBITS); | |
1057 | return dentry_hashtable + (hash & D_HASHMASK); | |
1058 | } | |
1059 | ||
1060 | /** | |
1061 | * d_alloc_anon - allocate an anonymous dentry | |
1062 | * @inode: inode to allocate the dentry for | |
1063 | * | |
1064 | * This is similar to d_alloc_root. It is used by filesystems when | |
1065 | * creating a dentry for a given inode, often in the process of | |
1066 | * mapping a filehandle to a dentry. The returned dentry may be | |
1067 | * anonymous, or may have a full name (if the inode was already | |
1068 | * in the cache). The file system may need to make further | |
1069 | * efforts to connect this dentry into the dcache properly. | |
1070 | * | |
1071 | * When called on a directory inode, we must ensure that | |
1072 | * the inode only ever has one dentry. If a dentry is | |
1073 | * found, that is returned instead of allocating a new one. | |
1074 | * | |
1075 | * On successful return, the reference to the inode has been transferred | |
1076 | * to the dentry. If %NULL is returned (indicating kmalloc failure), | |
1077 | * the reference on the inode has not been released. | |
1078 | */ | |
1079 | ||
1080 | struct dentry * d_alloc_anon(struct inode *inode) | |
1081 | { | |
1082 | static const struct qstr anonstring = { .name = "" }; | |
1083 | struct dentry *tmp; | |
1084 | struct dentry *res; | |
1085 | ||
1086 | if ((res = d_find_alias(inode))) { | |
1087 | iput(inode); | |
1088 | return res; | |
1089 | } | |
1090 | ||
1091 | tmp = d_alloc(NULL, &anonstring); | |
1092 | if (!tmp) | |
1093 | return NULL; | |
1094 | ||
1095 | tmp->d_parent = tmp; /* make sure dput doesn't croak */ | |
1096 | ||
1097 | spin_lock(&dcache_lock); | |
1098 | res = __d_find_alias(inode, 0); | |
1099 | if (!res) { | |
1100 | /* attach a disconnected dentry */ | |
1101 | res = tmp; | |
1102 | tmp = NULL; | |
1103 | spin_lock(&res->d_lock); | |
1104 | res->d_sb = inode->i_sb; | |
1105 | res->d_parent = res; | |
1106 | res->d_inode = inode; | |
1107 | res->d_flags |= DCACHE_DISCONNECTED; | |
1108 | res->d_flags &= ~DCACHE_UNHASHED; | |
1109 | list_add(&res->d_alias, &inode->i_dentry); | |
1110 | hlist_add_head(&res->d_hash, &inode->i_sb->s_anon); | |
1111 | spin_unlock(&res->d_lock); | |
1112 | ||
1113 | inode = NULL; /* don't drop reference */ | |
1114 | } | |
1115 | spin_unlock(&dcache_lock); | |
1116 | ||
1117 | if (inode) | |
1118 | iput(inode); | |
1119 | if (tmp) | |
1120 | dput(tmp); | |
1121 | return res; | |
1122 | } | |
1123 | ||
1124 | ||
1125 | /** | |
1126 | * d_splice_alias - splice a disconnected dentry into the tree if one exists | |
1127 | * @inode: the inode which may have a disconnected dentry | |
1128 | * @dentry: a negative dentry which we want to point to the inode. | |
1129 | * | |
1130 | * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and | |
1131 | * DCACHE_DISCONNECTED), then d_move that in place of the given dentry | |
1132 | * and return it, else simply d_add the inode to the dentry and return NULL. | |
1133 | * | |
1134 | * This is needed in the lookup routine of any filesystem that is exportable | |
1135 | * (via knfsd) so that we can build dcache paths to directories effectively. | |
1136 | * | |
1137 | * If a dentry was found and moved, then it is returned. Otherwise NULL | |
1138 | * is returned. This matches the expected return value of ->lookup. | |
1139 | * | |
1140 | */ | |
1141 | struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry) | |
1142 | { | |
1143 | struct dentry *new = NULL; | |
1144 | ||
21c0d8fd | 1145 | if (inode && S_ISDIR(inode->i_mode)) { |
1da177e4 LT |
1146 | spin_lock(&dcache_lock); |
1147 | new = __d_find_alias(inode, 1); | |
1148 | if (new) { | |
1149 | BUG_ON(!(new->d_flags & DCACHE_DISCONNECTED)); | |
c32ccd87 | 1150 | fsnotify_d_instantiate(new, inode); |
1da177e4 LT |
1151 | spin_unlock(&dcache_lock); |
1152 | security_d_instantiate(new, inode); | |
1153 | d_rehash(dentry); | |
1154 | d_move(new, dentry); | |
1155 | iput(inode); | |
1156 | } else { | |
1157 | /* d_instantiate takes dcache_lock, so we do it by hand */ | |
1158 | list_add(&dentry->d_alias, &inode->i_dentry); | |
1159 | dentry->d_inode = inode; | |
c32ccd87 | 1160 | fsnotify_d_instantiate(dentry, inode); |
1da177e4 LT |
1161 | spin_unlock(&dcache_lock); |
1162 | security_d_instantiate(dentry, inode); | |
1163 | d_rehash(dentry); | |
1164 | } | |
1165 | } else | |
1166 | d_add(dentry, inode); | |
1167 | return new; | |
1168 | } | |
1169 | ||
1170 | ||
1171 | /** | |
1172 | * d_lookup - search for a dentry | |
1173 | * @parent: parent dentry | |
1174 | * @name: qstr of name we wish to find | |
1175 | * | |
1176 | * Searches the children of the parent dentry for the name in question. If | |
1177 | * the dentry is found its reference count is incremented and the dentry | |
1178 | * is returned. The caller must use d_put to free the entry when it has | |
1179 | * finished using it. %NULL is returned on failure. | |
1180 | * | |
1181 | * __d_lookup is dcache_lock free. The hash list is protected using RCU. | |
1182 | * Memory barriers are used while updating and doing lockless traversal. | |
1183 | * To avoid races with d_move while rename is happening, d_lock is used. | |
1184 | * | |
1185 | * Overflows in memcmp(), while d_move, are avoided by keeping the length | |
1186 | * and name pointer in one structure pointed by d_qstr. | |
1187 | * | |
1188 | * rcu_read_lock() and rcu_read_unlock() are used to disable preemption while | |
1189 | * lookup is going on. | |
1190 | * | |
1191 | * dentry_unused list is not updated even if lookup finds the required dentry | |
1192 | * in there. It is updated in places such as prune_dcache, shrink_dcache_sb, | |
1193 | * select_parent and __dget_locked. This laziness saves lookup from dcache_lock | |
1194 | * acquisition. | |
1195 | * | |
1196 | * d_lookup() is protected against the concurrent renames in some unrelated | |
1197 | * directory using the seqlockt_t rename_lock. | |
1198 | */ | |
1199 | ||
1200 | struct dentry * d_lookup(struct dentry * parent, struct qstr * name) | |
1201 | { | |
1202 | struct dentry * dentry = NULL; | |
1203 | unsigned long seq; | |
1204 | ||
1205 | do { | |
1206 | seq = read_seqbegin(&rename_lock); | |
1207 | dentry = __d_lookup(parent, name); | |
1208 | if (dentry) | |
1209 | break; | |
1210 | } while (read_seqretry(&rename_lock, seq)); | |
1211 | return dentry; | |
1212 | } | |
1213 | ||
1214 | struct dentry * __d_lookup(struct dentry * parent, struct qstr * name) | |
1215 | { | |
1216 | unsigned int len = name->len; | |
1217 | unsigned int hash = name->hash; | |
1218 | const unsigned char *str = name->name; | |
1219 | struct hlist_head *head = d_hash(parent,hash); | |
1220 | struct dentry *found = NULL; | |
1221 | struct hlist_node *node; | |
665a7583 | 1222 | struct dentry *dentry; |
1da177e4 LT |
1223 | |
1224 | rcu_read_lock(); | |
1225 | ||
665a7583 | 1226 | hlist_for_each_entry_rcu(dentry, node, head, d_hash) { |
1da177e4 LT |
1227 | struct qstr *qstr; |
1228 | ||
1da177e4 LT |
1229 | if (dentry->d_name.hash != hash) |
1230 | continue; | |
1231 | if (dentry->d_parent != parent) | |
1232 | continue; | |
1233 | ||
1234 | spin_lock(&dentry->d_lock); | |
1235 | ||
1236 | /* | |
1237 | * Recheck the dentry after taking the lock - d_move may have | |
1238 | * changed things. Don't bother checking the hash because we're | |
1239 | * about to compare the whole name anyway. | |
1240 | */ | |
1241 | if (dentry->d_parent != parent) | |
1242 | goto next; | |
1243 | ||
1244 | /* | |
1245 | * It is safe to compare names since d_move() cannot | |
1246 | * change the qstr (protected by d_lock). | |
1247 | */ | |
1248 | qstr = &dentry->d_name; | |
1249 | if (parent->d_op && parent->d_op->d_compare) { | |
1250 | if (parent->d_op->d_compare(parent, qstr, name)) | |
1251 | goto next; | |
1252 | } else { | |
1253 | if (qstr->len != len) | |
1254 | goto next; | |
1255 | if (memcmp(qstr->name, str, len)) | |
1256 | goto next; | |
1257 | } | |
1258 | ||
1259 | if (!d_unhashed(dentry)) { | |
1260 | atomic_inc(&dentry->d_count); | |
1261 | found = dentry; | |
1262 | } | |
1263 | spin_unlock(&dentry->d_lock); | |
1264 | break; | |
1265 | next: | |
1266 | spin_unlock(&dentry->d_lock); | |
1267 | } | |
1268 | rcu_read_unlock(); | |
1269 | ||
1270 | return found; | |
1271 | } | |
1272 | ||
3e7e241f EB |
1273 | /** |
1274 | * d_hash_and_lookup - hash the qstr then search for a dentry | |
1275 | * @dir: Directory to search in | |
1276 | * @name: qstr of name we wish to find | |
1277 | * | |
1278 | * On hash failure or on lookup failure NULL is returned. | |
1279 | */ | |
1280 | struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name) | |
1281 | { | |
1282 | struct dentry *dentry = NULL; | |
1283 | ||
1284 | /* | |
1285 | * Check for a fs-specific hash function. Note that we must | |
1286 | * calculate the standard hash first, as the d_op->d_hash() | |
1287 | * routine may choose to leave the hash value unchanged. | |
1288 | */ | |
1289 | name->hash = full_name_hash(name->name, name->len); | |
1290 | if (dir->d_op && dir->d_op->d_hash) { | |
1291 | if (dir->d_op->d_hash(dir, name) < 0) | |
1292 | goto out; | |
1293 | } | |
1294 | dentry = d_lookup(dir, name); | |
1295 | out: | |
1296 | return dentry; | |
1297 | } | |
1298 | ||
1da177e4 LT |
1299 | /** |
1300 | * d_validate - verify dentry provided from insecure source | |
1301 | * @dentry: The dentry alleged to be valid child of @dparent | |
1302 | * @dparent: The parent dentry (known to be valid) | |
1303 | * @hash: Hash of the dentry | |
1304 | * @len: Length of the name | |
1305 | * | |
1306 | * An insecure source has sent us a dentry, here we verify it and dget() it. | |
1307 | * This is used by ncpfs in its readdir implementation. | |
1308 | * Zero is returned in the dentry is invalid. | |
1309 | */ | |
1310 | ||
1311 | int d_validate(struct dentry *dentry, struct dentry *dparent) | |
1312 | { | |
1313 | struct hlist_head *base; | |
1314 | struct hlist_node *lhp; | |
1315 | ||
1316 | /* Check whether the ptr might be valid at all.. */ | |
1317 | if (!kmem_ptr_validate(dentry_cache, dentry)) | |
1318 | goto out; | |
1319 | ||
1320 | if (dentry->d_parent != dparent) | |
1321 | goto out; | |
1322 | ||
1323 | spin_lock(&dcache_lock); | |
1324 | base = d_hash(dparent, dentry->d_name.hash); | |
1325 | hlist_for_each(lhp,base) { | |
665a7583 | 1326 | /* hlist_for_each_entry_rcu() not required for d_hash list |
1da177e4 LT |
1327 | * as it is parsed under dcache_lock |
1328 | */ | |
1329 | if (dentry == hlist_entry(lhp, struct dentry, d_hash)) { | |
1330 | __dget_locked(dentry); | |
1331 | spin_unlock(&dcache_lock); | |
1332 | return 1; | |
1333 | } | |
1334 | } | |
1335 | spin_unlock(&dcache_lock); | |
1336 | out: | |
1337 | return 0; | |
1338 | } | |
1339 | ||
1340 | /* | |
1341 | * When a file is deleted, we have two options: | |
1342 | * - turn this dentry into a negative dentry | |
1343 | * - unhash this dentry and free it. | |
1344 | * | |
1345 | * Usually, we want to just turn this into | |
1346 | * a negative dentry, but if anybody else is | |
1347 | * currently using the dentry or the inode | |
1348 | * we can't do that and we fall back on removing | |
1349 | * it from the hash queues and waiting for | |
1350 | * it to be deleted later when it has no users | |
1351 | */ | |
1352 | ||
1353 | /** | |
1354 | * d_delete - delete a dentry | |
1355 | * @dentry: The dentry to delete | |
1356 | * | |
1357 | * Turn the dentry into a negative dentry if possible, otherwise | |
1358 | * remove it from the hash queues so it can be deleted later | |
1359 | */ | |
1360 | ||
1361 | void d_delete(struct dentry * dentry) | |
1362 | { | |
7a91bf7f | 1363 | int isdir = 0; |
1da177e4 LT |
1364 | /* |
1365 | * Are we the only user? | |
1366 | */ | |
1367 | spin_lock(&dcache_lock); | |
1368 | spin_lock(&dentry->d_lock); | |
7a91bf7f | 1369 | isdir = S_ISDIR(dentry->d_inode->i_mode); |
1da177e4 LT |
1370 | if (atomic_read(&dentry->d_count) == 1) { |
1371 | dentry_iput(dentry); | |
7a91bf7f | 1372 | fsnotify_nameremove(dentry, isdir); |
7a2bd3f7 AG |
1373 | |
1374 | /* remove this and other inotify debug checks after 2.6.18 */ | |
1375 | dentry->d_flags &= ~DCACHE_INOTIFY_PARENT_WATCHED; | |
1da177e4 LT |
1376 | return; |
1377 | } | |
1378 | ||
1379 | if (!d_unhashed(dentry)) | |
1380 | __d_drop(dentry); | |
1381 | ||
1382 | spin_unlock(&dentry->d_lock); | |
1383 | spin_unlock(&dcache_lock); | |
7a91bf7f JM |
1384 | |
1385 | fsnotify_nameremove(dentry, isdir); | |
1da177e4 LT |
1386 | } |
1387 | ||
1388 | static void __d_rehash(struct dentry * entry, struct hlist_head *list) | |
1389 | { | |
1390 | ||
1391 | entry->d_flags &= ~DCACHE_UNHASHED; | |
1392 | hlist_add_head_rcu(&entry->d_hash, list); | |
1393 | } | |
1394 | ||
770bfad8 DH |
1395 | static void _d_rehash(struct dentry * entry) |
1396 | { | |
1397 | __d_rehash(entry, d_hash(entry->d_parent, entry->d_name.hash)); | |
1398 | } | |
1399 | ||
1da177e4 LT |
1400 | /** |
1401 | * d_rehash - add an entry back to the hash | |
1402 | * @entry: dentry to add to the hash | |
1403 | * | |
1404 | * Adds a dentry to the hash according to its name. | |
1405 | */ | |
1406 | ||
1407 | void d_rehash(struct dentry * entry) | |
1408 | { | |
1da177e4 LT |
1409 | spin_lock(&dcache_lock); |
1410 | spin_lock(&entry->d_lock); | |
770bfad8 | 1411 | _d_rehash(entry); |
1da177e4 LT |
1412 | spin_unlock(&entry->d_lock); |
1413 | spin_unlock(&dcache_lock); | |
1414 | } | |
1415 | ||
1416 | #define do_switch(x,y) do { \ | |
1417 | __typeof__ (x) __tmp = x; \ | |
1418 | x = y; y = __tmp; } while (0) | |
1419 | ||
1420 | /* | |
1421 | * When switching names, the actual string doesn't strictly have to | |
1422 | * be preserved in the target - because we're dropping the target | |
1423 | * anyway. As such, we can just do a simple memcpy() to copy over | |
1424 | * the new name before we switch. | |
1425 | * | |
1426 | * Note that we have to be a lot more careful about getting the hash | |
1427 | * switched - we have to switch the hash value properly even if it | |
1428 | * then no longer matches the actual (corrupted) string of the target. | |
1429 | * The hash value has to match the hash queue that the dentry is on.. | |
1430 | */ | |
1431 | static void switch_names(struct dentry *dentry, struct dentry *target) | |
1432 | { | |
1433 | if (dname_external(target)) { | |
1434 | if (dname_external(dentry)) { | |
1435 | /* | |
1436 | * Both external: swap the pointers | |
1437 | */ | |
1438 | do_switch(target->d_name.name, dentry->d_name.name); | |
1439 | } else { | |
1440 | /* | |
1441 | * dentry:internal, target:external. Steal target's | |
1442 | * storage and make target internal. | |
1443 | */ | |
1444 | dentry->d_name.name = target->d_name.name; | |
1445 | target->d_name.name = target->d_iname; | |
1446 | } | |
1447 | } else { | |
1448 | if (dname_external(dentry)) { | |
1449 | /* | |
1450 | * dentry:external, target:internal. Give dentry's | |
1451 | * storage to target and make dentry internal | |
1452 | */ | |
1453 | memcpy(dentry->d_iname, target->d_name.name, | |
1454 | target->d_name.len + 1); | |
1455 | target->d_name.name = dentry->d_name.name; | |
1456 | dentry->d_name.name = dentry->d_iname; | |
1457 | } else { | |
1458 | /* | |
1459 | * Both are internal. Just copy target to dentry | |
1460 | */ | |
1461 | memcpy(dentry->d_iname, target->d_name.name, | |
1462 | target->d_name.len + 1); | |
1463 | } | |
1464 | } | |
1465 | } | |
1466 | ||
1467 | /* | |
1468 | * We cannibalize "target" when moving dentry on top of it, | |
1469 | * because it's going to be thrown away anyway. We could be more | |
1470 | * polite about it, though. | |
1471 | * | |
1472 | * This forceful removal will result in ugly /proc output if | |
1473 | * somebody holds a file open that got deleted due to a rename. | |
1474 | * We could be nicer about the deleted file, and let it show | |
1475 | * up under the name it got deleted rather than the name that | |
1476 | * deleted it. | |
1477 | */ | |
1478 | ||
9eaef27b TM |
1479 | /* |
1480 | * d_move_locked - move a dentry | |
1da177e4 LT |
1481 | * @dentry: entry to move |
1482 | * @target: new dentry | |
1483 | * | |
1484 | * Update the dcache to reflect the move of a file name. Negative | |
1485 | * dcache entries should not be moved in this way. | |
1486 | */ | |
9eaef27b | 1487 | static void d_move_locked(struct dentry * dentry, struct dentry * target) |
1da177e4 LT |
1488 | { |
1489 | struct hlist_head *list; | |
1490 | ||
1491 | if (!dentry->d_inode) | |
1492 | printk(KERN_WARNING "VFS: moving negative dcache entry\n"); | |
1493 | ||
1da177e4 LT |
1494 | write_seqlock(&rename_lock); |
1495 | /* | |
1496 | * XXXX: do we really need to take target->d_lock? | |
1497 | */ | |
1498 | if (target < dentry) { | |
1499 | spin_lock(&target->d_lock); | |
a90b9c05 | 1500 | spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); |
1da177e4 LT |
1501 | } else { |
1502 | spin_lock(&dentry->d_lock); | |
a90b9c05 | 1503 | spin_lock_nested(&target->d_lock, DENTRY_D_LOCK_NESTED); |
1da177e4 LT |
1504 | } |
1505 | ||
1506 | /* Move the dentry to the target hash queue, if on different bucket */ | |
1507 | if (dentry->d_flags & DCACHE_UNHASHED) | |
1508 | goto already_unhashed; | |
1509 | ||
1510 | hlist_del_rcu(&dentry->d_hash); | |
1511 | ||
1512 | already_unhashed: | |
1513 | list = d_hash(target->d_parent, target->d_name.hash); | |
1514 | __d_rehash(dentry, list); | |
1515 | ||
1516 | /* Unhash the target: dput() will then get rid of it */ | |
1517 | __d_drop(target); | |
1518 | ||
5160ee6f ED |
1519 | list_del(&dentry->d_u.d_child); |
1520 | list_del(&target->d_u.d_child); | |
1da177e4 LT |
1521 | |
1522 | /* Switch the names.. */ | |
1523 | switch_names(dentry, target); | |
1524 | do_switch(dentry->d_name.len, target->d_name.len); | |
1525 | do_switch(dentry->d_name.hash, target->d_name.hash); | |
1526 | ||
1527 | /* ... and switch the parents */ | |
1528 | if (IS_ROOT(dentry)) { | |
1529 | dentry->d_parent = target->d_parent; | |
1530 | target->d_parent = target; | |
5160ee6f | 1531 | INIT_LIST_HEAD(&target->d_u.d_child); |
1da177e4 LT |
1532 | } else { |
1533 | do_switch(dentry->d_parent, target->d_parent); | |
1534 | ||
1535 | /* And add them back to the (new) parent lists */ | |
5160ee6f | 1536 | list_add(&target->d_u.d_child, &target->d_parent->d_subdirs); |
1da177e4 LT |
1537 | } |
1538 | ||
5160ee6f | 1539 | list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs); |
1da177e4 | 1540 | spin_unlock(&target->d_lock); |
c32ccd87 | 1541 | fsnotify_d_move(dentry); |
1da177e4 LT |
1542 | spin_unlock(&dentry->d_lock); |
1543 | write_sequnlock(&rename_lock); | |
9eaef27b TM |
1544 | } |
1545 | ||
1546 | /** | |
1547 | * d_move - move a dentry | |
1548 | * @dentry: entry to move | |
1549 | * @target: new dentry | |
1550 | * | |
1551 | * Update the dcache to reflect the move of a file name. Negative | |
1552 | * dcache entries should not be moved in this way. | |
1553 | */ | |
1554 | ||
1555 | void d_move(struct dentry * dentry, struct dentry * target) | |
1556 | { | |
1557 | spin_lock(&dcache_lock); | |
1558 | d_move_locked(dentry, target); | |
1da177e4 LT |
1559 | spin_unlock(&dcache_lock); |
1560 | } | |
1561 | ||
9eaef27b TM |
1562 | /* |
1563 | * Helper that returns 1 if p1 is a parent of p2, else 0 | |
1564 | */ | |
1565 | static int d_isparent(struct dentry *p1, struct dentry *p2) | |
1566 | { | |
1567 | struct dentry *p; | |
1568 | ||
1569 | for (p = p2; p->d_parent != p; p = p->d_parent) { | |
1570 | if (p->d_parent == p1) | |
1571 | return 1; | |
1572 | } | |
1573 | return 0; | |
1574 | } | |
1575 | ||
1576 | /* | |
1577 | * This helper attempts to cope with remotely renamed directories | |
1578 | * | |
1579 | * It assumes that the caller is already holding | |
1580 | * dentry->d_parent->d_inode->i_mutex and the dcache_lock | |
1581 | * | |
1582 | * Note: If ever the locking in lock_rename() changes, then please | |
1583 | * remember to update this too... | |
1584 | * | |
1585 | * On return, dcache_lock will have been unlocked. | |
1586 | */ | |
1587 | static struct dentry *__d_unalias(struct dentry *dentry, struct dentry *alias) | |
1588 | { | |
1589 | struct mutex *m1 = NULL, *m2 = NULL; | |
1590 | struct dentry *ret; | |
1591 | ||
1592 | /* If alias and dentry share a parent, then no extra locks required */ | |
1593 | if (alias->d_parent == dentry->d_parent) | |
1594 | goto out_unalias; | |
1595 | ||
1596 | /* Check for loops */ | |
1597 | ret = ERR_PTR(-ELOOP); | |
1598 | if (d_isparent(alias, dentry)) | |
1599 | goto out_err; | |
1600 | ||
1601 | /* See lock_rename() */ | |
1602 | ret = ERR_PTR(-EBUSY); | |
1603 | if (!mutex_trylock(&dentry->d_sb->s_vfs_rename_mutex)) | |
1604 | goto out_err; | |
1605 | m1 = &dentry->d_sb->s_vfs_rename_mutex; | |
1606 | if (!mutex_trylock(&alias->d_parent->d_inode->i_mutex)) | |
1607 | goto out_err; | |
1608 | m2 = &alias->d_parent->d_inode->i_mutex; | |
1609 | out_unalias: | |
1610 | d_move_locked(alias, dentry); | |
1611 | ret = alias; | |
1612 | out_err: | |
1613 | spin_unlock(&dcache_lock); | |
1614 | if (m2) | |
1615 | mutex_unlock(m2); | |
1616 | if (m1) | |
1617 | mutex_unlock(m1); | |
1618 | return ret; | |
1619 | } | |
1620 | ||
770bfad8 DH |
1621 | /* |
1622 | * Prepare an anonymous dentry for life in the superblock's dentry tree as a | |
1623 | * named dentry in place of the dentry to be replaced. | |
1624 | */ | |
1625 | static void __d_materialise_dentry(struct dentry *dentry, struct dentry *anon) | |
1626 | { | |
1627 | struct dentry *dparent, *aparent; | |
1628 | ||
1629 | switch_names(dentry, anon); | |
1630 | do_switch(dentry->d_name.len, anon->d_name.len); | |
1631 | do_switch(dentry->d_name.hash, anon->d_name.hash); | |
1632 | ||
1633 | dparent = dentry->d_parent; | |
1634 | aparent = anon->d_parent; | |
1635 | ||
1636 | dentry->d_parent = (aparent == anon) ? dentry : aparent; | |
1637 | list_del(&dentry->d_u.d_child); | |
1638 | if (!IS_ROOT(dentry)) | |
1639 | list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs); | |
1640 | else | |
1641 | INIT_LIST_HEAD(&dentry->d_u.d_child); | |
1642 | ||
1643 | anon->d_parent = (dparent == dentry) ? anon : dparent; | |
1644 | list_del(&anon->d_u.d_child); | |
1645 | if (!IS_ROOT(anon)) | |
1646 | list_add(&anon->d_u.d_child, &anon->d_parent->d_subdirs); | |
1647 | else | |
1648 | INIT_LIST_HEAD(&anon->d_u.d_child); | |
1649 | ||
1650 | anon->d_flags &= ~DCACHE_DISCONNECTED; | |
1651 | } | |
1652 | ||
1653 | /** | |
1654 | * d_materialise_unique - introduce an inode into the tree | |
1655 | * @dentry: candidate dentry | |
1656 | * @inode: inode to bind to the dentry, to which aliases may be attached | |
1657 | * | |
1658 | * Introduces an dentry into the tree, substituting an extant disconnected | |
1659 | * root directory alias in its place if there is one | |
1660 | */ | |
1661 | struct dentry *d_materialise_unique(struct dentry *dentry, struct inode *inode) | |
1662 | { | |
9eaef27b | 1663 | struct dentry *actual; |
770bfad8 DH |
1664 | |
1665 | BUG_ON(!d_unhashed(dentry)); | |
1666 | ||
1667 | spin_lock(&dcache_lock); | |
1668 | ||
1669 | if (!inode) { | |
1670 | actual = dentry; | |
1671 | dentry->d_inode = NULL; | |
1672 | goto found_lock; | |
1673 | } | |
1674 | ||
9eaef27b TM |
1675 | if (S_ISDIR(inode->i_mode)) { |
1676 | struct dentry *alias; | |
1677 | ||
1678 | /* Does an aliased dentry already exist? */ | |
1679 | alias = __d_find_alias(inode, 0); | |
1680 | if (alias) { | |
1681 | actual = alias; | |
1682 | /* Is this an anonymous mountpoint that we could splice | |
1683 | * into our tree? */ | |
1684 | if (IS_ROOT(alias)) { | |
1685 | spin_lock(&alias->d_lock); | |
1686 | __d_materialise_dentry(dentry, alias); | |
1687 | __d_drop(alias); | |
1688 | goto found; | |
1689 | } | |
1690 | /* Nope, but we must(!) avoid directory aliasing */ | |
1691 | actual = __d_unalias(dentry, alias); | |
1692 | if (IS_ERR(actual)) | |
1693 | dput(alias); | |
1694 | goto out_nolock; | |
1695 | } | |
770bfad8 DH |
1696 | } |
1697 | ||
1698 | /* Add a unique reference */ | |
1699 | actual = __d_instantiate_unique(dentry, inode); | |
1700 | if (!actual) | |
1701 | actual = dentry; | |
1702 | else if (unlikely(!d_unhashed(actual))) | |
1703 | goto shouldnt_be_hashed; | |
1704 | ||
1705 | found_lock: | |
1706 | spin_lock(&actual->d_lock); | |
1707 | found: | |
1708 | _d_rehash(actual); | |
1709 | spin_unlock(&actual->d_lock); | |
1710 | spin_unlock(&dcache_lock); | |
9eaef27b | 1711 | out_nolock: |
770bfad8 DH |
1712 | if (actual == dentry) { |
1713 | security_d_instantiate(dentry, inode); | |
1714 | return NULL; | |
1715 | } | |
1716 | ||
1717 | iput(inode); | |
1718 | return actual; | |
1719 | ||
770bfad8 DH |
1720 | shouldnt_be_hashed: |
1721 | spin_unlock(&dcache_lock); | |
1722 | BUG(); | |
1723 | goto shouldnt_be_hashed; | |
1724 | } | |
1725 | ||
1da177e4 LT |
1726 | /** |
1727 | * d_path - return the path of a dentry | |
1728 | * @dentry: dentry to report | |
1729 | * @vfsmnt: vfsmnt to which the dentry belongs | |
1730 | * @root: root dentry | |
1731 | * @rootmnt: vfsmnt to which the root dentry belongs | |
1732 | * @buffer: buffer to return value in | |
1733 | * @buflen: buffer length | |
1734 | * | |
1735 | * Convert a dentry into an ASCII path name. If the entry has been deleted | |
1736 | * the string " (deleted)" is appended. Note that this is ambiguous. | |
1737 | * | |
1738 | * Returns the buffer or an error code if the path was too long. | |
1739 | * | |
1740 | * "buflen" should be positive. Caller holds the dcache_lock. | |
1741 | */ | |
1742 | static char * __d_path( struct dentry *dentry, struct vfsmount *vfsmnt, | |
1743 | struct dentry *root, struct vfsmount *rootmnt, | |
1744 | char *buffer, int buflen) | |
1745 | { | |
1746 | char * end = buffer+buflen; | |
1747 | char * retval; | |
1748 | int namelen; | |
1749 | ||
1750 | *--end = '\0'; | |
1751 | buflen--; | |
1752 | if (!IS_ROOT(dentry) && d_unhashed(dentry)) { | |
1753 | buflen -= 10; | |
1754 | end -= 10; | |
1755 | if (buflen < 0) | |
1756 | goto Elong; | |
1757 | memcpy(end, " (deleted)", 10); | |
1758 | } | |
1759 | ||
1760 | if (buflen < 1) | |
1761 | goto Elong; | |
1762 | /* Get '/' right */ | |
1763 | retval = end-1; | |
1764 | *retval = '/'; | |
1765 | ||
1766 | for (;;) { | |
1767 | struct dentry * parent; | |
1768 | ||
1769 | if (dentry == root && vfsmnt == rootmnt) | |
1770 | break; | |
1771 | if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) { | |
1772 | /* Global root? */ | |
1773 | spin_lock(&vfsmount_lock); | |
1774 | if (vfsmnt->mnt_parent == vfsmnt) { | |
1775 | spin_unlock(&vfsmount_lock); | |
1776 | goto global_root; | |
1777 | } | |
1778 | dentry = vfsmnt->mnt_mountpoint; | |
1779 | vfsmnt = vfsmnt->mnt_parent; | |
1780 | spin_unlock(&vfsmount_lock); | |
1781 | continue; | |
1782 | } | |
1783 | parent = dentry->d_parent; | |
1784 | prefetch(parent); | |
1785 | namelen = dentry->d_name.len; | |
1786 | buflen -= namelen + 1; | |
1787 | if (buflen < 0) | |
1788 | goto Elong; | |
1789 | end -= namelen; | |
1790 | memcpy(end, dentry->d_name.name, namelen); | |
1791 | *--end = '/'; | |
1792 | retval = end; | |
1793 | dentry = parent; | |
1794 | } | |
1795 | ||
1796 | return retval; | |
1797 | ||
1798 | global_root: | |
1799 | namelen = dentry->d_name.len; | |
1800 | buflen -= namelen; | |
1801 | if (buflen < 0) | |
1802 | goto Elong; | |
1803 | retval -= namelen-1; /* hit the slash */ | |
1804 | memcpy(retval, dentry->d_name.name, namelen); | |
1805 | return retval; | |
1806 | Elong: | |
1807 | return ERR_PTR(-ENAMETOOLONG); | |
1808 | } | |
1809 | ||
1810 | /* write full pathname into buffer and return start of pathname */ | |
1811 | char * d_path(struct dentry *dentry, struct vfsmount *vfsmnt, | |
1812 | char *buf, int buflen) | |
1813 | { | |
1814 | char *res; | |
1815 | struct vfsmount *rootmnt; | |
1816 | struct dentry *root; | |
1817 | ||
1818 | read_lock(¤t->fs->lock); | |
1819 | rootmnt = mntget(current->fs->rootmnt); | |
1820 | root = dget(current->fs->root); | |
1821 | read_unlock(¤t->fs->lock); | |
1822 | spin_lock(&dcache_lock); | |
1823 | res = __d_path(dentry, vfsmnt, root, rootmnt, buf, buflen); | |
1824 | spin_unlock(&dcache_lock); | |
1825 | dput(root); | |
1826 | mntput(rootmnt); | |
1827 | return res; | |
1828 | } | |
1829 | ||
1830 | /* | |
1831 | * NOTE! The user-level library version returns a | |
1832 | * character pointer. The kernel system call just | |
1833 | * returns the length of the buffer filled (which | |
1834 | * includes the ending '\0' character), or a negative | |
1835 | * error value. So libc would do something like | |
1836 | * | |
1837 | * char *getcwd(char * buf, size_t size) | |
1838 | * { | |
1839 | * int retval; | |
1840 | * | |
1841 | * retval = sys_getcwd(buf, size); | |
1842 | * if (retval >= 0) | |
1843 | * return buf; | |
1844 | * errno = -retval; | |
1845 | * return NULL; | |
1846 | * } | |
1847 | */ | |
1848 | asmlinkage long sys_getcwd(char __user *buf, unsigned long size) | |
1849 | { | |
1850 | int error; | |
1851 | struct vfsmount *pwdmnt, *rootmnt; | |
1852 | struct dentry *pwd, *root; | |
1853 | char *page = (char *) __get_free_page(GFP_USER); | |
1854 | ||
1855 | if (!page) | |
1856 | return -ENOMEM; | |
1857 | ||
1858 | read_lock(¤t->fs->lock); | |
1859 | pwdmnt = mntget(current->fs->pwdmnt); | |
1860 | pwd = dget(current->fs->pwd); | |
1861 | rootmnt = mntget(current->fs->rootmnt); | |
1862 | root = dget(current->fs->root); | |
1863 | read_unlock(¤t->fs->lock); | |
1864 | ||
1865 | error = -ENOENT; | |
1866 | /* Has the current directory has been unlinked? */ | |
1867 | spin_lock(&dcache_lock); | |
1868 | if (pwd->d_parent == pwd || !d_unhashed(pwd)) { | |
1869 | unsigned long len; | |
1870 | char * cwd; | |
1871 | ||
1872 | cwd = __d_path(pwd, pwdmnt, root, rootmnt, page, PAGE_SIZE); | |
1873 | spin_unlock(&dcache_lock); | |
1874 | ||
1875 | error = PTR_ERR(cwd); | |
1876 | if (IS_ERR(cwd)) | |
1877 | goto out; | |
1878 | ||
1879 | error = -ERANGE; | |
1880 | len = PAGE_SIZE + page - cwd; | |
1881 | if (len <= size) { | |
1882 | error = len; | |
1883 | if (copy_to_user(buf, cwd, len)) | |
1884 | error = -EFAULT; | |
1885 | } | |
1886 | } else | |
1887 | spin_unlock(&dcache_lock); | |
1888 | ||
1889 | out: | |
1890 | dput(pwd); | |
1891 | mntput(pwdmnt); | |
1892 | dput(root); | |
1893 | mntput(rootmnt); | |
1894 | free_page((unsigned long) page); | |
1895 | return error; | |
1896 | } | |
1897 | ||
1898 | /* | |
1899 | * Test whether new_dentry is a subdirectory of old_dentry. | |
1900 | * | |
1901 | * Trivially implemented using the dcache structure | |
1902 | */ | |
1903 | ||
1904 | /** | |
1905 | * is_subdir - is new dentry a subdirectory of old_dentry | |
1906 | * @new_dentry: new dentry | |
1907 | * @old_dentry: old dentry | |
1908 | * | |
1909 | * Returns 1 if new_dentry is a subdirectory of the parent (at any depth). | |
1910 | * Returns 0 otherwise. | |
1911 | * Caller must ensure that "new_dentry" is pinned before calling is_subdir() | |
1912 | */ | |
1913 | ||
1914 | int is_subdir(struct dentry * new_dentry, struct dentry * old_dentry) | |
1915 | { | |
1916 | int result; | |
1917 | struct dentry * saved = new_dentry; | |
1918 | unsigned long seq; | |
1919 | ||
1920 | /* need rcu_readlock to protect against the d_parent trashing due to | |
1921 | * d_move | |
1922 | */ | |
1923 | rcu_read_lock(); | |
1924 | do { | |
1925 | /* for restarting inner loop in case of seq retry */ | |
1926 | new_dentry = saved; | |
1927 | result = 0; | |
1928 | seq = read_seqbegin(&rename_lock); | |
1929 | for (;;) { | |
1930 | if (new_dentry != old_dentry) { | |
1931 | struct dentry * parent = new_dentry->d_parent; | |
1932 | if (parent == new_dentry) | |
1933 | break; | |
1934 | new_dentry = parent; | |
1935 | continue; | |
1936 | } | |
1937 | result = 1; | |
1938 | break; | |
1939 | } | |
1940 | } while (read_seqretry(&rename_lock, seq)); | |
1941 | rcu_read_unlock(); | |
1942 | ||
1943 | return result; | |
1944 | } | |
1945 | ||
1946 | void d_genocide(struct dentry *root) | |
1947 | { | |
1948 | struct dentry *this_parent = root; | |
1949 | struct list_head *next; | |
1950 | ||
1951 | spin_lock(&dcache_lock); | |
1952 | repeat: | |
1953 | next = this_parent->d_subdirs.next; | |
1954 | resume: | |
1955 | while (next != &this_parent->d_subdirs) { | |
1956 | struct list_head *tmp = next; | |
5160ee6f | 1957 | struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child); |
1da177e4 LT |
1958 | next = tmp->next; |
1959 | if (d_unhashed(dentry)||!dentry->d_inode) | |
1960 | continue; | |
1961 | if (!list_empty(&dentry->d_subdirs)) { | |
1962 | this_parent = dentry; | |
1963 | goto repeat; | |
1964 | } | |
1965 | atomic_dec(&dentry->d_count); | |
1966 | } | |
1967 | if (this_parent != root) { | |
5160ee6f | 1968 | next = this_parent->d_u.d_child.next; |
1da177e4 LT |
1969 | atomic_dec(&this_parent->d_count); |
1970 | this_parent = this_parent->d_parent; | |
1971 | goto resume; | |
1972 | } | |
1973 | spin_unlock(&dcache_lock); | |
1974 | } | |
1975 | ||
1976 | /** | |
1977 | * find_inode_number - check for dentry with name | |
1978 | * @dir: directory to check | |
1979 | * @name: Name to find. | |
1980 | * | |
1981 | * Check whether a dentry already exists for the given name, | |
1982 | * and return the inode number if it has an inode. Otherwise | |
1983 | * 0 is returned. | |
1984 | * | |
1985 | * This routine is used to post-process directory listings for | |
1986 | * filesystems using synthetic inode numbers, and is necessary | |
1987 | * to keep getcwd() working. | |
1988 | */ | |
1989 | ||
1990 | ino_t find_inode_number(struct dentry *dir, struct qstr *name) | |
1991 | { | |
1992 | struct dentry * dentry; | |
1993 | ino_t ino = 0; | |
1994 | ||
3e7e241f EB |
1995 | dentry = d_hash_and_lookup(dir, name); |
1996 | if (dentry) { | |
1da177e4 LT |
1997 | if (dentry->d_inode) |
1998 | ino = dentry->d_inode->i_ino; | |
1999 | dput(dentry); | |
2000 | } | |
1da177e4 LT |
2001 | return ino; |
2002 | } | |
2003 | ||
2004 | static __initdata unsigned long dhash_entries; | |
2005 | static int __init set_dhash_entries(char *str) | |
2006 | { | |
2007 | if (!str) | |
2008 | return 0; | |
2009 | dhash_entries = simple_strtoul(str, &str, 0); | |
2010 | return 1; | |
2011 | } | |
2012 | __setup("dhash_entries=", set_dhash_entries); | |
2013 | ||
2014 | static void __init dcache_init_early(void) | |
2015 | { | |
2016 | int loop; | |
2017 | ||
2018 | /* If hashes are distributed across NUMA nodes, defer | |
2019 | * hash allocation until vmalloc space is available. | |
2020 | */ | |
2021 | if (hashdist) | |
2022 | return; | |
2023 | ||
2024 | dentry_hashtable = | |
2025 | alloc_large_system_hash("Dentry cache", | |
2026 | sizeof(struct hlist_head), | |
2027 | dhash_entries, | |
2028 | 13, | |
2029 | HASH_EARLY, | |
2030 | &d_hash_shift, | |
2031 | &d_hash_mask, | |
2032 | 0); | |
2033 | ||
2034 | for (loop = 0; loop < (1 << d_hash_shift); loop++) | |
2035 | INIT_HLIST_HEAD(&dentry_hashtable[loop]); | |
2036 | } | |
2037 | ||
2038 | static void __init dcache_init(unsigned long mempages) | |
2039 | { | |
2040 | int loop; | |
2041 | ||
2042 | /* | |
2043 | * A constructor could be added for stable state like the lists, | |
2044 | * but it is probably not worth it because of the cache nature | |
2045 | * of the dcache. | |
2046 | */ | |
2047 | dentry_cache = kmem_cache_create("dentry_cache", | |
2048 | sizeof(struct dentry), | |
2049 | 0, | |
b0196009 PJ |
2050 | (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC| |
2051 | SLAB_MEM_SPREAD), | |
1da177e4 LT |
2052 | NULL, NULL); |
2053 | ||
2054 | set_shrinker(DEFAULT_SEEKS, shrink_dcache_memory); | |
2055 | ||
2056 | /* Hash may have been set up in dcache_init_early */ | |
2057 | if (!hashdist) | |
2058 | return; | |
2059 | ||
2060 | dentry_hashtable = | |
2061 | alloc_large_system_hash("Dentry cache", | |
2062 | sizeof(struct hlist_head), | |
2063 | dhash_entries, | |
2064 | 13, | |
2065 | 0, | |
2066 | &d_hash_shift, | |
2067 | &d_hash_mask, | |
2068 | 0); | |
2069 | ||
2070 | for (loop = 0; loop < (1 << d_hash_shift); loop++) | |
2071 | INIT_HLIST_HEAD(&dentry_hashtable[loop]); | |
2072 | } | |
2073 | ||
2074 | /* SLAB cache for __getname() consumers */ | |
fa3536cc | 2075 | kmem_cache_t *names_cachep __read_mostly; |
1da177e4 LT |
2076 | |
2077 | /* SLAB cache for file structures */ | |
fa3536cc | 2078 | kmem_cache_t *filp_cachep __read_mostly; |
1da177e4 LT |
2079 | |
2080 | EXPORT_SYMBOL(d_genocide); | |
2081 | ||
1da177e4 LT |
2082 | void __init vfs_caches_init_early(void) |
2083 | { | |
2084 | dcache_init_early(); | |
2085 | inode_init_early(); | |
2086 | } | |
2087 | ||
2088 | void __init vfs_caches_init(unsigned long mempages) | |
2089 | { | |
2090 | unsigned long reserve; | |
2091 | ||
2092 | /* Base hash sizes on available memory, with a reserve equal to | |
2093 | 150% of current kernel size */ | |
2094 | ||
2095 | reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1); | |
2096 | mempages -= reserve; | |
2097 | ||
2098 | names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0, | |
2099 | SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL); | |
2100 | ||
2101 | filp_cachep = kmem_cache_create("filp", sizeof(struct file), 0, | |
529bf6be | 2102 | SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL); |
1da177e4 LT |
2103 | |
2104 | dcache_init(mempages); | |
2105 | inode_init(mempages); | |
2106 | files_init(mempages); | |
2107 | mnt_init(mempages); | |
2108 | bdev_cache_init(); | |
2109 | chrdev_init(); | |
2110 | } | |
2111 | ||
2112 | EXPORT_SYMBOL(d_alloc); | |
2113 | EXPORT_SYMBOL(d_alloc_anon); | |
2114 | EXPORT_SYMBOL(d_alloc_root); | |
2115 | EXPORT_SYMBOL(d_delete); | |
2116 | EXPORT_SYMBOL(d_find_alias); | |
2117 | EXPORT_SYMBOL(d_instantiate); | |
2118 | EXPORT_SYMBOL(d_invalidate); | |
2119 | EXPORT_SYMBOL(d_lookup); | |
2120 | EXPORT_SYMBOL(d_move); | |
770bfad8 | 2121 | EXPORT_SYMBOL_GPL(d_materialise_unique); |
1da177e4 LT |
2122 | EXPORT_SYMBOL(d_path); |
2123 | EXPORT_SYMBOL(d_prune_aliases); | |
2124 | EXPORT_SYMBOL(d_rehash); | |
2125 | EXPORT_SYMBOL(d_splice_alias); | |
2126 | EXPORT_SYMBOL(d_validate); | |
2127 | EXPORT_SYMBOL(dget_locked); | |
2128 | EXPORT_SYMBOL(dput); | |
2129 | EXPORT_SYMBOL(find_inode_number); | |
2130 | EXPORT_SYMBOL(have_submounts); | |
2131 | EXPORT_SYMBOL(names_cachep); | |
2132 | EXPORT_SYMBOL(shrink_dcache_parent); | |
2133 | EXPORT_SYMBOL(shrink_dcache_sb); |