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