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