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