Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/inode.c |
| 3 | * |
| 4 | * (C) 1997 Linus Torvalds |
| 5 | */ |
| 6 | |
| 7 | #include <linux/config.h> |
| 8 | #include <linux/fs.h> |
| 9 | #include <linux/mm.h> |
| 10 | #include <linux/dcache.h> |
| 11 | #include <linux/init.h> |
| 12 | #include <linux/quotaops.h> |
| 13 | #include <linux/slab.h> |
| 14 | #include <linux/writeback.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/backing-dev.h> |
| 17 | #include <linux/wait.h> |
| 18 | #include <linux/hash.h> |
| 19 | #include <linux/swap.h> |
| 20 | #include <linux/security.h> |
| 21 | #include <linux/pagemap.h> |
| 22 | #include <linux/cdev.h> |
| 23 | #include <linux/bootmem.h> |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 24 | #include <linux/inotify.h> |
Christoph Hellwig | fc33a7b | 2006-01-09 20:52:17 -0800 | [diff] [blame] | 25 | #include <linux/mount.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
| 27 | /* |
| 28 | * This is needed for the following functions: |
| 29 | * - inode_has_buffers |
| 30 | * - invalidate_inode_buffers |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | * - invalidate_bdev |
| 32 | * |
| 33 | * FIXME: remove all knowledge of the buffer layer from this file |
| 34 | */ |
| 35 | #include <linux/buffer_head.h> |
| 36 | |
| 37 | /* |
| 38 | * New inode.c implementation. |
| 39 | * |
| 40 | * This implementation has the basic premise of trying |
| 41 | * to be extremely low-overhead and SMP-safe, yet be |
| 42 | * simple enough to be "obviously correct". |
| 43 | * |
| 44 | * Famous last words. |
| 45 | */ |
| 46 | |
| 47 | /* inode dynamic allocation 1999, Andrea Arcangeli <andrea@suse.de> */ |
| 48 | |
| 49 | /* #define INODE_PARANOIA 1 */ |
| 50 | /* #define INODE_DEBUG 1 */ |
| 51 | |
| 52 | /* |
| 53 | * Inode lookup is no longer as critical as it used to be: |
| 54 | * most of the lookups are going to be through the dcache. |
| 55 | */ |
| 56 | #define I_HASHBITS i_hash_shift |
| 57 | #define I_HASHMASK i_hash_mask |
| 58 | |
| 59 | static unsigned int i_hash_mask; |
| 60 | static unsigned int i_hash_shift; |
| 61 | |
| 62 | /* |
| 63 | * Each inode can be on two separate lists. One is |
| 64 | * the hash list of the inode, used for lookups. The |
| 65 | * other linked list is the "type" list: |
| 66 | * "in_use" - valid inode, i_count > 0, i_nlink > 0 |
| 67 | * "dirty" - as "in_use" but also dirty |
| 68 | * "unused" - valid inode, i_count = 0 |
| 69 | * |
| 70 | * A "dirty" list is maintained for each super block, |
| 71 | * allowing for low-overhead inode sync() operations. |
| 72 | */ |
| 73 | |
| 74 | LIST_HEAD(inode_in_use); |
| 75 | LIST_HEAD(inode_unused); |
| 76 | static struct hlist_head *inode_hashtable; |
| 77 | |
| 78 | /* |
| 79 | * A simple spinlock to protect the list manipulations. |
| 80 | * |
| 81 | * NOTE! You also have to own the lock if you change |
| 82 | * the i_state of an inode while it is in use.. |
| 83 | */ |
| 84 | DEFINE_SPINLOCK(inode_lock); |
| 85 | |
| 86 | /* |
| 87 | * iprune_sem provides exclusion between the kswapd or try_to_free_pages |
| 88 | * icache shrinking path, and the umount path. Without this exclusion, |
| 89 | * by the time prune_icache calls iput for the inode whose pages it has |
| 90 | * been invalidating, or by the time it calls clear_inode & destroy_inode |
| 91 | * from its final dispose_list, the struct super_block they refer to |
| 92 | * (for inode->i_sb->s_op) may already have been freed and reused. |
| 93 | */ |
| 94 | DECLARE_MUTEX(iprune_sem); |
| 95 | |
| 96 | /* |
| 97 | * Statistics gathering.. |
| 98 | */ |
| 99 | struct inodes_stat_t inodes_stat; |
| 100 | |
| 101 | static kmem_cache_t * inode_cachep; |
| 102 | |
| 103 | static struct inode *alloc_inode(struct super_block *sb) |
| 104 | { |
| 105 | static struct address_space_operations empty_aops; |
| 106 | static struct inode_operations empty_iops; |
| 107 | static struct file_operations empty_fops; |
| 108 | struct inode *inode; |
| 109 | |
| 110 | if (sb->s_op->alloc_inode) |
| 111 | inode = sb->s_op->alloc_inode(sb); |
| 112 | else |
| 113 | inode = (struct inode *) kmem_cache_alloc(inode_cachep, SLAB_KERNEL); |
| 114 | |
| 115 | if (inode) { |
| 116 | struct address_space * const mapping = &inode->i_data; |
| 117 | |
| 118 | inode->i_sb = sb; |
| 119 | inode->i_blkbits = sb->s_blocksize_bits; |
| 120 | inode->i_flags = 0; |
| 121 | atomic_set(&inode->i_count, 1); |
| 122 | inode->i_op = &empty_iops; |
| 123 | inode->i_fop = &empty_fops; |
| 124 | inode->i_nlink = 1; |
| 125 | atomic_set(&inode->i_writecount, 0); |
| 126 | inode->i_size = 0; |
| 127 | inode->i_blocks = 0; |
| 128 | inode->i_bytes = 0; |
| 129 | inode->i_generation = 0; |
| 130 | #ifdef CONFIG_QUOTA |
| 131 | memset(&inode->i_dquot, 0, sizeof(inode->i_dquot)); |
| 132 | #endif |
| 133 | inode->i_pipe = NULL; |
| 134 | inode->i_bdev = NULL; |
| 135 | inode->i_cdev = NULL; |
| 136 | inode->i_rdev = 0; |
| 137 | inode->i_security = NULL; |
| 138 | inode->dirtied_when = 0; |
| 139 | if (security_inode_alloc(inode)) { |
| 140 | if (inode->i_sb->s_op->destroy_inode) |
| 141 | inode->i_sb->s_op->destroy_inode(inode); |
| 142 | else |
| 143 | kmem_cache_free(inode_cachep, (inode)); |
| 144 | return NULL; |
| 145 | } |
| 146 | |
| 147 | mapping->a_ops = &empty_aops; |
| 148 | mapping->host = inode; |
| 149 | mapping->flags = 0; |
| 150 | mapping_set_gfp_mask(mapping, GFP_HIGHUSER); |
| 151 | mapping->assoc_mapping = NULL; |
| 152 | mapping->backing_dev_info = &default_backing_dev_info; |
| 153 | |
| 154 | /* |
| 155 | * If the block_device provides a backing_dev_info for client |
| 156 | * inodes then use that. Otherwise the inode share the bdev's |
| 157 | * backing_dev_info. |
| 158 | */ |
| 159 | if (sb->s_bdev) { |
| 160 | struct backing_dev_info *bdi; |
| 161 | |
| 162 | bdi = sb->s_bdev->bd_inode_backing_dev_info; |
| 163 | if (!bdi) |
| 164 | bdi = sb->s_bdev->bd_inode->i_mapping->backing_dev_info; |
| 165 | mapping->backing_dev_info = bdi; |
| 166 | } |
| 167 | memset(&inode->u, 0, sizeof(inode->u)); |
| 168 | inode->i_mapping = mapping; |
| 169 | } |
| 170 | return inode; |
| 171 | } |
| 172 | |
| 173 | void destroy_inode(struct inode *inode) |
| 174 | { |
| 175 | if (inode_has_buffers(inode)) |
| 176 | BUG(); |
| 177 | security_inode_free(inode); |
| 178 | if (inode->i_sb->s_op->destroy_inode) |
| 179 | inode->i_sb->s_op->destroy_inode(inode); |
| 180 | else |
| 181 | kmem_cache_free(inode_cachep, (inode)); |
| 182 | } |
| 183 | |
| 184 | |
| 185 | /* |
| 186 | * These are initializations that only need to be done |
| 187 | * once, because the fields are idempotent across use |
| 188 | * of the inode, so let the slab aware of that. |
| 189 | */ |
| 190 | void inode_init_once(struct inode *inode) |
| 191 | { |
| 192 | memset(inode, 0, sizeof(*inode)); |
| 193 | INIT_HLIST_NODE(&inode->i_hash); |
| 194 | INIT_LIST_HEAD(&inode->i_dentry); |
| 195 | INIT_LIST_HEAD(&inode->i_devices); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 196 | mutex_init(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | init_rwsem(&inode->i_alloc_sem); |
| 198 | INIT_RADIX_TREE(&inode->i_data.page_tree, GFP_ATOMIC); |
| 199 | rwlock_init(&inode->i_data.tree_lock); |
| 200 | spin_lock_init(&inode->i_data.i_mmap_lock); |
| 201 | INIT_LIST_HEAD(&inode->i_data.private_list); |
| 202 | spin_lock_init(&inode->i_data.private_lock); |
| 203 | INIT_RAW_PRIO_TREE_ROOT(&inode->i_data.i_mmap); |
| 204 | INIT_LIST_HEAD(&inode->i_data.i_mmap_nonlinear); |
| 205 | spin_lock_init(&inode->i_lock); |
| 206 | i_size_ordered_init(inode); |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 207 | #ifdef CONFIG_INOTIFY |
| 208 | INIT_LIST_HEAD(&inode->inotify_watches); |
| 209 | sema_init(&inode->inotify_sem, 1); |
| 210 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | EXPORT_SYMBOL(inode_init_once); |
| 214 | |
| 215 | static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags) |
| 216 | { |
| 217 | struct inode * inode = (struct inode *) foo; |
| 218 | |
| 219 | if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) == |
| 220 | SLAB_CTOR_CONSTRUCTOR) |
| 221 | inode_init_once(inode); |
| 222 | } |
| 223 | |
| 224 | /* |
| 225 | * inode_lock must be held |
| 226 | */ |
| 227 | void __iget(struct inode * inode) |
| 228 | { |
| 229 | if (atomic_read(&inode->i_count)) { |
| 230 | atomic_inc(&inode->i_count); |
| 231 | return; |
| 232 | } |
| 233 | atomic_inc(&inode->i_count); |
| 234 | if (!(inode->i_state & (I_DIRTY|I_LOCK))) |
| 235 | list_move(&inode->i_list, &inode_in_use); |
| 236 | inodes_stat.nr_unused--; |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * clear_inode - clear an inode |
| 241 | * @inode: inode to clear |
| 242 | * |
| 243 | * This is called by the filesystem to tell us |
| 244 | * that the inode is no longer useful. We just |
| 245 | * terminate it with extreme prejudice. |
| 246 | */ |
| 247 | void clear_inode(struct inode *inode) |
| 248 | { |
| 249 | might_sleep(); |
| 250 | invalidate_inode_buffers(inode); |
| 251 | |
| 252 | if (inode->i_data.nrpages) |
| 253 | BUG(); |
| 254 | if (!(inode->i_state & I_FREEING)) |
| 255 | BUG(); |
| 256 | if (inode->i_state & I_CLEAR) |
| 257 | BUG(); |
| 258 | wait_on_inode(inode); |
| 259 | DQUOT_DROP(inode); |
| 260 | if (inode->i_sb && inode->i_sb->s_op->clear_inode) |
| 261 | inode->i_sb->s_op->clear_inode(inode); |
| 262 | if (inode->i_bdev) |
| 263 | bd_forget(inode); |
| 264 | if (inode->i_cdev) |
| 265 | cd_forget(inode); |
| 266 | inode->i_state = I_CLEAR; |
| 267 | } |
| 268 | |
| 269 | EXPORT_SYMBOL(clear_inode); |
| 270 | |
| 271 | /* |
| 272 | * dispose_list - dispose of the contents of a local list |
| 273 | * @head: the head of the list to free |
| 274 | * |
| 275 | * Dispose-list gets a local list with local inodes in it, so it doesn't |
| 276 | * need to worry about list corruption and SMP locks. |
| 277 | */ |
| 278 | static void dispose_list(struct list_head *head) |
| 279 | { |
| 280 | int nr_disposed = 0; |
| 281 | |
| 282 | while (!list_empty(head)) { |
| 283 | struct inode *inode; |
| 284 | |
| 285 | inode = list_entry(head->next, struct inode, i_list); |
| 286 | list_del(&inode->i_list); |
| 287 | |
| 288 | if (inode->i_data.nrpages) |
| 289 | truncate_inode_pages(&inode->i_data, 0); |
| 290 | clear_inode(inode); |
Artem B. Bityuckiy | 4120db4 | 2005-07-12 13:58:12 -0700 | [diff] [blame] | 291 | |
| 292 | spin_lock(&inode_lock); |
| 293 | hlist_del_init(&inode->i_hash); |
| 294 | list_del_init(&inode->i_sb_list); |
| 295 | spin_unlock(&inode_lock); |
| 296 | |
| 297 | wake_up_inode(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | destroy_inode(inode); |
| 299 | nr_disposed++; |
| 300 | } |
| 301 | spin_lock(&inode_lock); |
| 302 | inodes_stat.nr_inodes -= nr_disposed; |
| 303 | spin_unlock(&inode_lock); |
| 304 | } |
| 305 | |
| 306 | /* |
| 307 | * Invalidate all inodes for a device. |
| 308 | */ |
| 309 | static int invalidate_list(struct list_head *head, struct list_head *dispose) |
| 310 | { |
| 311 | struct list_head *next; |
| 312 | int busy = 0, count = 0; |
| 313 | |
| 314 | next = head->next; |
| 315 | for (;;) { |
| 316 | struct list_head * tmp = next; |
| 317 | struct inode * inode; |
| 318 | |
| 319 | /* |
| 320 | * We can reschedule here without worrying about the list's |
| 321 | * consistency because the per-sb list of inodes must not |
| 322 | * change during umount anymore, and because iprune_sem keeps |
| 323 | * shrink_icache_memory() away. |
| 324 | */ |
| 325 | cond_resched_lock(&inode_lock); |
| 326 | |
| 327 | next = next->next; |
| 328 | if (tmp == head) |
| 329 | break; |
| 330 | inode = list_entry(tmp, struct inode, i_sb_list); |
| 331 | invalidate_inode_buffers(inode); |
| 332 | if (!atomic_read(&inode->i_count)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | list_move(&inode->i_list, dispose); |
| 334 | inode->i_state |= I_FREEING; |
| 335 | count++; |
| 336 | continue; |
| 337 | } |
| 338 | busy = 1; |
| 339 | } |
| 340 | /* only unused inodes may be cached with i_count zero */ |
| 341 | inodes_stat.nr_unused -= count; |
| 342 | return busy; |
| 343 | } |
| 344 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | /** |
| 346 | * invalidate_inodes - discard the inodes on a device |
| 347 | * @sb: superblock |
| 348 | * |
| 349 | * Discard all of the inodes for a given superblock. If the discard |
| 350 | * fails because there are busy inodes then a non zero value is returned. |
| 351 | * If the discard is successful all the inodes have been discarded. |
| 352 | */ |
| 353 | int invalidate_inodes(struct super_block * sb) |
| 354 | { |
| 355 | int busy; |
| 356 | LIST_HEAD(throw_away); |
| 357 | |
| 358 | down(&iprune_sem); |
| 359 | spin_lock(&inode_lock); |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 360 | inotify_unmount_inodes(&sb->s_inodes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | busy = invalidate_list(&sb->s_inodes, &throw_away); |
| 362 | spin_unlock(&inode_lock); |
| 363 | |
| 364 | dispose_list(&throw_away); |
| 365 | up(&iprune_sem); |
| 366 | |
| 367 | return busy; |
| 368 | } |
| 369 | |
| 370 | EXPORT_SYMBOL(invalidate_inodes); |
| 371 | |
Christoph Hellwig | 2ef4163 | 2005-05-05 16:15:59 -0700 | [diff] [blame] | 372 | int __invalidate_device(struct block_device *bdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | { |
Christoph Hellwig | 2ef4163 | 2005-05-05 16:15:59 -0700 | [diff] [blame] | 374 | struct super_block *sb = get_super(bdev); |
| 375 | int res = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | if (sb) { |
| 378 | /* |
| 379 | * no need to lock the super, get_super holds the |
| 380 | * read semaphore so the filesystem cannot go away |
| 381 | * under us (->put_super runs with the write lock |
| 382 | * hold). |
| 383 | */ |
| 384 | shrink_dcache_sb(sb); |
| 385 | res = invalidate_inodes(sb); |
| 386 | drop_super(sb); |
| 387 | } |
| 388 | invalidate_bdev(bdev, 0); |
| 389 | return res; |
| 390 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | EXPORT_SYMBOL(__invalidate_device); |
| 392 | |
| 393 | static int can_unuse(struct inode *inode) |
| 394 | { |
| 395 | if (inode->i_state) |
| 396 | return 0; |
| 397 | if (inode_has_buffers(inode)) |
| 398 | return 0; |
| 399 | if (atomic_read(&inode->i_count)) |
| 400 | return 0; |
| 401 | if (inode->i_data.nrpages) |
| 402 | return 0; |
| 403 | return 1; |
| 404 | } |
| 405 | |
| 406 | /* |
| 407 | * Scan `goal' inodes on the unused list for freeable ones. They are moved to |
| 408 | * a temporary list and then are freed outside inode_lock by dispose_list(). |
| 409 | * |
| 410 | * Any inodes which are pinned purely because of attached pagecache have their |
| 411 | * pagecache removed. We expect the final iput() on that inode to add it to |
| 412 | * the front of the inode_unused list. So look for it there and if the |
| 413 | * inode is still freeable, proceed. The right inode is found 99.9% of the |
| 414 | * time in testing on a 4-way. |
| 415 | * |
| 416 | * If the inode has metadata buffers attached to mapping->private_list then |
| 417 | * try to remove them. |
| 418 | */ |
| 419 | static void prune_icache(int nr_to_scan) |
| 420 | { |
| 421 | LIST_HEAD(freeable); |
| 422 | int nr_pruned = 0; |
| 423 | int nr_scanned; |
| 424 | unsigned long reap = 0; |
| 425 | |
| 426 | down(&iprune_sem); |
| 427 | spin_lock(&inode_lock); |
| 428 | for (nr_scanned = 0; nr_scanned < nr_to_scan; nr_scanned++) { |
| 429 | struct inode *inode; |
| 430 | |
| 431 | if (list_empty(&inode_unused)) |
| 432 | break; |
| 433 | |
| 434 | inode = list_entry(inode_unused.prev, struct inode, i_list); |
| 435 | |
| 436 | if (inode->i_state || atomic_read(&inode->i_count)) { |
| 437 | list_move(&inode->i_list, &inode_unused); |
| 438 | continue; |
| 439 | } |
| 440 | if (inode_has_buffers(inode) || inode->i_data.nrpages) { |
| 441 | __iget(inode); |
| 442 | spin_unlock(&inode_lock); |
| 443 | if (remove_inode_buffers(inode)) |
| 444 | reap += invalidate_inode_pages(&inode->i_data); |
| 445 | iput(inode); |
| 446 | spin_lock(&inode_lock); |
| 447 | |
| 448 | if (inode != list_entry(inode_unused.next, |
| 449 | struct inode, i_list)) |
| 450 | continue; /* wrong inode or list_empty */ |
| 451 | if (!can_unuse(inode)) |
| 452 | continue; |
| 453 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | list_move(&inode->i_list, &freeable); |
| 455 | inode->i_state |= I_FREEING; |
| 456 | nr_pruned++; |
| 457 | } |
| 458 | inodes_stat.nr_unused -= nr_pruned; |
| 459 | spin_unlock(&inode_lock); |
| 460 | |
| 461 | dispose_list(&freeable); |
| 462 | up(&iprune_sem); |
| 463 | |
| 464 | if (current_is_kswapd()) |
| 465 | mod_page_state(kswapd_inodesteal, reap); |
| 466 | else |
| 467 | mod_page_state(pginodesteal, reap); |
| 468 | } |
| 469 | |
| 470 | /* |
| 471 | * shrink_icache_memory() will attempt to reclaim some unused inodes. Here, |
| 472 | * "unused" means that no dentries are referring to the inodes: the files are |
| 473 | * not open and the dcache references to those inodes have already been |
| 474 | * reclaimed. |
| 475 | * |
| 476 | * This function is passed the number of inodes to scan, and it returns the |
| 477 | * total number of remaining possibly-reclaimable inodes. |
| 478 | */ |
Al Viro | 27496a8 | 2005-10-21 03:20:48 -0400 | [diff] [blame] | 479 | static int shrink_icache_memory(int nr, gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | { |
| 481 | if (nr) { |
| 482 | /* |
| 483 | * Nasty deadlock avoidance. We may hold various FS locks, |
| 484 | * and we don't want to recurse into the FS that called us |
| 485 | * in clear_inode() and friends.. |
| 486 | */ |
| 487 | if (!(gfp_mask & __GFP_FS)) |
| 488 | return -1; |
| 489 | prune_icache(nr); |
| 490 | } |
| 491 | return (inodes_stat.nr_unused / 100) * sysctl_vfs_cache_pressure; |
| 492 | } |
| 493 | |
| 494 | static void __wait_on_freeing_inode(struct inode *inode); |
| 495 | /* |
| 496 | * Called with the inode lock held. |
| 497 | * NOTE: we are not increasing the inode-refcount, you must call __iget() |
| 498 | * by hand after calling find_inode now! This simplifies iunique and won't |
| 499 | * add any additional branch in the common code. |
| 500 | */ |
| 501 | static struct inode * find_inode(struct super_block * sb, struct hlist_head *head, int (*test)(struct inode *, void *), void *data) |
| 502 | { |
| 503 | struct hlist_node *node; |
| 504 | struct inode * inode = NULL; |
| 505 | |
| 506 | repeat: |
| 507 | hlist_for_each (node, head) { |
| 508 | inode = hlist_entry(node, struct inode, i_hash); |
| 509 | if (inode->i_sb != sb) |
| 510 | continue; |
| 511 | if (!test(inode, data)) |
| 512 | continue; |
Alexander Viro | 991114c | 2005-06-23 00:09:01 -0700 | [diff] [blame] | 513 | if (inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | __wait_on_freeing_inode(inode); |
| 515 | goto repeat; |
| 516 | } |
| 517 | break; |
| 518 | } |
| 519 | return node ? inode : NULL; |
| 520 | } |
| 521 | |
| 522 | /* |
| 523 | * find_inode_fast is the fast path version of find_inode, see the comment at |
| 524 | * iget_locked for details. |
| 525 | */ |
| 526 | static struct inode * find_inode_fast(struct super_block * sb, struct hlist_head *head, unsigned long ino) |
| 527 | { |
| 528 | struct hlist_node *node; |
| 529 | struct inode * inode = NULL; |
| 530 | |
| 531 | repeat: |
| 532 | hlist_for_each (node, head) { |
| 533 | inode = hlist_entry(node, struct inode, i_hash); |
| 534 | if (inode->i_ino != ino) |
| 535 | continue; |
| 536 | if (inode->i_sb != sb) |
| 537 | continue; |
Alexander Viro | 991114c | 2005-06-23 00:09:01 -0700 | [diff] [blame] | 538 | if (inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | __wait_on_freeing_inode(inode); |
| 540 | goto repeat; |
| 541 | } |
| 542 | break; |
| 543 | } |
| 544 | return node ? inode : NULL; |
| 545 | } |
| 546 | |
| 547 | /** |
| 548 | * new_inode - obtain an inode |
| 549 | * @sb: superblock |
| 550 | * |
| 551 | * Allocates a new inode for given superblock. |
| 552 | */ |
| 553 | struct inode *new_inode(struct super_block *sb) |
| 554 | { |
| 555 | static unsigned long last_ino; |
| 556 | struct inode * inode; |
| 557 | |
| 558 | spin_lock_prefetch(&inode_lock); |
| 559 | |
| 560 | inode = alloc_inode(sb); |
| 561 | if (inode) { |
| 562 | spin_lock(&inode_lock); |
| 563 | inodes_stat.nr_inodes++; |
| 564 | list_add(&inode->i_list, &inode_in_use); |
| 565 | list_add(&inode->i_sb_list, &sb->s_inodes); |
| 566 | inode->i_ino = ++last_ino; |
| 567 | inode->i_state = 0; |
| 568 | spin_unlock(&inode_lock); |
| 569 | } |
| 570 | return inode; |
| 571 | } |
| 572 | |
| 573 | EXPORT_SYMBOL(new_inode); |
| 574 | |
| 575 | void unlock_new_inode(struct inode *inode) |
| 576 | { |
| 577 | /* |
| 578 | * This is special! We do not need the spinlock |
| 579 | * when clearing I_LOCK, because we're guaranteed |
| 580 | * that nobody else tries to do anything about the |
| 581 | * state of the inode when it is locked, as we |
| 582 | * just created it (so there can be no old holders |
| 583 | * that haven't tested I_LOCK). |
| 584 | */ |
| 585 | inode->i_state &= ~(I_LOCK|I_NEW); |
| 586 | wake_up_inode(inode); |
| 587 | } |
| 588 | |
| 589 | EXPORT_SYMBOL(unlock_new_inode); |
| 590 | |
| 591 | /* |
| 592 | * This is called without the inode lock held.. Be careful. |
| 593 | * |
| 594 | * We no longer cache the sb_flags in i_flags - see fs.h |
| 595 | * -- rmk@arm.uk.linux.org |
| 596 | */ |
| 597 | static struct inode * get_new_inode(struct super_block *sb, struct hlist_head *head, int (*test)(struct inode *, void *), int (*set)(struct inode *, void *), void *data) |
| 598 | { |
| 599 | struct inode * inode; |
| 600 | |
| 601 | inode = alloc_inode(sb); |
| 602 | if (inode) { |
| 603 | struct inode * old; |
| 604 | |
| 605 | spin_lock(&inode_lock); |
| 606 | /* We released the lock, so.. */ |
| 607 | old = find_inode(sb, head, test, data); |
| 608 | if (!old) { |
| 609 | if (set(inode, data)) |
| 610 | goto set_failed; |
| 611 | |
| 612 | inodes_stat.nr_inodes++; |
| 613 | list_add(&inode->i_list, &inode_in_use); |
| 614 | list_add(&inode->i_sb_list, &sb->s_inodes); |
| 615 | hlist_add_head(&inode->i_hash, head); |
| 616 | inode->i_state = I_LOCK|I_NEW; |
| 617 | spin_unlock(&inode_lock); |
| 618 | |
| 619 | /* Return the locked inode with I_NEW set, the |
| 620 | * caller is responsible for filling in the contents |
| 621 | */ |
| 622 | return inode; |
| 623 | } |
| 624 | |
| 625 | /* |
| 626 | * Uhhuh, somebody else created the same inode under |
| 627 | * us. Use the old inode instead of the one we just |
| 628 | * allocated. |
| 629 | */ |
| 630 | __iget(old); |
| 631 | spin_unlock(&inode_lock); |
| 632 | destroy_inode(inode); |
| 633 | inode = old; |
| 634 | wait_on_inode(inode); |
| 635 | } |
| 636 | return inode; |
| 637 | |
| 638 | set_failed: |
| 639 | spin_unlock(&inode_lock); |
| 640 | destroy_inode(inode); |
| 641 | return NULL; |
| 642 | } |
| 643 | |
| 644 | /* |
| 645 | * get_new_inode_fast is the fast path version of get_new_inode, see the |
| 646 | * comment at iget_locked for details. |
| 647 | */ |
| 648 | static struct inode * get_new_inode_fast(struct super_block *sb, struct hlist_head *head, unsigned long ino) |
| 649 | { |
| 650 | struct inode * inode; |
| 651 | |
| 652 | inode = alloc_inode(sb); |
| 653 | if (inode) { |
| 654 | struct inode * old; |
| 655 | |
| 656 | spin_lock(&inode_lock); |
| 657 | /* We released the lock, so.. */ |
| 658 | old = find_inode_fast(sb, head, ino); |
| 659 | if (!old) { |
| 660 | inode->i_ino = ino; |
| 661 | inodes_stat.nr_inodes++; |
| 662 | list_add(&inode->i_list, &inode_in_use); |
| 663 | list_add(&inode->i_sb_list, &sb->s_inodes); |
| 664 | hlist_add_head(&inode->i_hash, head); |
| 665 | inode->i_state = I_LOCK|I_NEW; |
| 666 | spin_unlock(&inode_lock); |
| 667 | |
| 668 | /* Return the locked inode with I_NEW set, the |
| 669 | * caller is responsible for filling in the contents |
| 670 | */ |
| 671 | return inode; |
| 672 | } |
| 673 | |
| 674 | /* |
| 675 | * Uhhuh, somebody else created the same inode under |
| 676 | * us. Use the old inode instead of the one we just |
| 677 | * allocated. |
| 678 | */ |
| 679 | __iget(old); |
| 680 | spin_unlock(&inode_lock); |
| 681 | destroy_inode(inode); |
| 682 | inode = old; |
| 683 | wait_on_inode(inode); |
| 684 | } |
| 685 | return inode; |
| 686 | } |
| 687 | |
| 688 | static inline unsigned long hash(struct super_block *sb, unsigned long hashval) |
| 689 | { |
| 690 | unsigned long tmp; |
| 691 | |
| 692 | tmp = (hashval * (unsigned long)sb) ^ (GOLDEN_RATIO_PRIME + hashval) / |
| 693 | L1_CACHE_BYTES; |
| 694 | tmp = tmp ^ ((tmp ^ GOLDEN_RATIO_PRIME) >> I_HASHBITS); |
| 695 | return tmp & I_HASHMASK; |
| 696 | } |
| 697 | |
| 698 | /** |
| 699 | * iunique - get a unique inode number |
| 700 | * @sb: superblock |
| 701 | * @max_reserved: highest reserved inode number |
| 702 | * |
| 703 | * Obtain an inode number that is unique on the system for a given |
| 704 | * superblock. This is used by file systems that have no natural |
| 705 | * permanent inode numbering system. An inode number is returned that |
| 706 | * is higher than the reserved limit but unique. |
| 707 | * |
| 708 | * BUGS: |
| 709 | * With a large number of inodes live on the file system this function |
| 710 | * currently becomes quite slow. |
| 711 | */ |
| 712 | ino_t iunique(struct super_block *sb, ino_t max_reserved) |
| 713 | { |
| 714 | static ino_t counter; |
| 715 | struct inode *inode; |
| 716 | struct hlist_head * head; |
| 717 | ino_t res; |
| 718 | spin_lock(&inode_lock); |
| 719 | retry: |
| 720 | if (counter > max_reserved) { |
| 721 | head = inode_hashtable + hash(sb,counter); |
| 722 | res = counter++; |
| 723 | inode = find_inode_fast(sb, head, res); |
| 724 | if (!inode) { |
| 725 | spin_unlock(&inode_lock); |
| 726 | return res; |
| 727 | } |
| 728 | } else { |
| 729 | counter = max_reserved + 1; |
| 730 | } |
| 731 | goto retry; |
| 732 | |
| 733 | } |
| 734 | |
| 735 | EXPORT_SYMBOL(iunique); |
| 736 | |
| 737 | struct inode *igrab(struct inode *inode) |
| 738 | { |
| 739 | spin_lock(&inode_lock); |
Alexander Viro | 991114c | 2005-06-23 00:09:01 -0700 | [diff] [blame] | 740 | if (!(inode->i_state & (I_FREEING|I_WILL_FREE))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | __iget(inode); |
| 742 | else |
| 743 | /* |
| 744 | * Handle the case where s_op->clear_inode is not been |
| 745 | * called yet, and somebody is calling igrab |
| 746 | * while the inode is getting freed. |
| 747 | */ |
| 748 | inode = NULL; |
| 749 | spin_unlock(&inode_lock); |
| 750 | return inode; |
| 751 | } |
| 752 | |
| 753 | EXPORT_SYMBOL(igrab); |
| 754 | |
| 755 | /** |
| 756 | * ifind - internal function, you want ilookup5() or iget5(). |
| 757 | * @sb: super block of file system to search |
| 758 | * @head: the head of the list to search |
| 759 | * @test: callback used for comparisons between inodes |
| 760 | * @data: opaque data pointer to pass to @test |
Anton Altaparmakov | 88bd512 | 2005-07-13 01:10:44 -0700 | [diff] [blame] | 761 | * @wait: if true wait for the inode to be unlocked, if false do not |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | * |
| 763 | * ifind() searches for the inode specified by @data in the inode |
| 764 | * cache. This is a generalized version of ifind_fast() for file systems where |
| 765 | * the inode number is not sufficient for unique identification of an inode. |
| 766 | * |
| 767 | * If the inode is in the cache, the inode is returned with an incremented |
| 768 | * reference count. |
| 769 | * |
| 770 | * Otherwise NULL is returned. |
| 771 | * |
| 772 | * Note, @test is called with the inode_lock held, so can't sleep. |
| 773 | */ |
Matt Mackall | 5d2bea4 | 2006-01-08 01:05:21 -0800 | [diff] [blame] | 774 | static struct inode *ifind(struct super_block *sb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | struct hlist_head *head, int (*test)(struct inode *, void *), |
Anton Altaparmakov | 88bd512 | 2005-07-13 01:10:44 -0700 | [diff] [blame] | 776 | void *data, const int wait) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | { |
| 778 | struct inode *inode; |
| 779 | |
| 780 | spin_lock(&inode_lock); |
| 781 | inode = find_inode(sb, head, test, data); |
| 782 | if (inode) { |
| 783 | __iget(inode); |
| 784 | spin_unlock(&inode_lock); |
Anton Altaparmakov | 88bd512 | 2005-07-13 01:10:44 -0700 | [diff] [blame] | 785 | if (likely(wait)) |
| 786 | wait_on_inode(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | return inode; |
| 788 | } |
| 789 | spin_unlock(&inode_lock); |
| 790 | return NULL; |
| 791 | } |
| 792 | |
| 793 | /** |
| 794 | * ifind_fast - internal function, you want ilookup() or iget(). |
| 795 | * @sb: super block of file system to search |
| 796 | * @head: head of the list to search |
| 797 | * @ino: inode number to search for |
| 798 | * |
| 799 | * ifind_fast() searches for the inode @ino in the inode cache. This is for |
| 800 | * file systems where the inode number is sufficient for unique identification |
| 801 | * of an inode. |
| 802 | * |
| 803 | * If the inode is in the cache, the inode is returned with an incremented |
| 804 | * reference count. |
| 805 | * |
| 806 | * Otherwise NULL is returned. |
| 807 | */ |
Matt Mackall | 5d2bea4 | 2006-01-08 01:05:21 -0800 | [diff] [blame] | 808 | static struct inode *ifind_fast(struct super_block *sb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | struct hlist_head *head, unsigned long ino) |
| 810 | { |
| 811 | struct inode *inode; |
| 812 | |
| 813 | spin_lock(&inode_lock); |
| 814 | inode = find_inode_fast(sb, head, ino); |
| 815 | if (inode) { |
| 816 | __iget(inode); |
| 817 | spin_unlock(&inode_lock); |
| 818 | wait_on_inode(inode); |
| 819 | return inode; |
| 820 | } |
| 821 | spin_unlock(&inode_lock); |
| 822 | return NULL; |
| 823 | } |
| 824 | |
| 825 | /** |
Anton Altaparmakov | 88bd512 | 2005-07-13 01:10:44 -0700 | [diff] [blame] | 826 | * ilookup5_nowait - search for an inode in the inode cache |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | * @sb: super block of file system to search |
| 828 | * @hashval: hash value (usually inode number) to search for |
| 829 | * @test: callback used for comparisons between inodes |
| 830 | * @data: opaque data pointer to pass to @test |
| 831 | * |
| 832 | * ilookup5() uses ifind() to search for the inode specified by @hashval and |
| 833 | * @data in the inode cache. This is a generalized version of ilookup() for |
| 834 | * file systems where the inode number is not sufficient for unique |
| 835 | * identification of an inode. |
| 836 | * |
| 837 | * If the inode is in the cache, the inode is returned with an incremented |
Anton Altaparmakov | 88bd512 | 2005-07-13 01:10:44 -0700 | [diff] [blame] | 838 | * reference count. Note, the inode lock is not waited upon so you have to be |
| 839 | * very careful what you do with the returned inode. You probably should be |
| 840 | * using ilookup5() instead. |
| 841 | * |
| 842 | * Otherwise NULL is returned. |
| 843 | * |
| 844 | * Note, @test is called with the inode_lock held, so can't sleep. |
| 845 | */ |
| 846 | struct inode *ilookup5_nowait(struct super_block *sb, unsigned long hashval, |
| 847 | int (*test)(struct inode *, void *), void *data) |
| 848 | { |
| 849 | struct hlist_head *head = inode_hashtable + hash(sb, hashval); |
| 850 | |
| 851 | return ifind(sb, head, test, data, 0); |
| 852 | } |
| 853 | |
| 854 | EXPORT_SYMBOL(ilookup5_nowait); |
| 855 | |
| 856 | /** |
| 857 | * ilookup5 - search for an inode in the inode cache |
| 858 | * @sb: super block of file system to search |
| 859 | * @hashval: hash value (usually inode number) to search for |
| 860 | * @test: callback used for comparisons between inodes |
| 861 | * @data: opaque data pointer to pass to @test |
| 862 | * |
| 863 | * ilookup5() uses ifind() to search for the inode specified by @hashval and |
| 864 | * @data in the inode cache. This is a generalized version of ilookup() for |
| 865 | * file systems where the inode number is not sufficient for unique |
| 866 | * identification of an inode. |
| 867 | * |
| 868 | * If the inode is in the cache, the inode lock is waited upon and the inode is |
| 869 | * returned with an incremented reference count. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | * |
| 871 | * Otherwise NULL is returned. |
| 872 | * |
| 873 | * Note, @test is called with the inode_lock held, so can't sleep. |
| 874 | */ |
| 875 | struct inode *ilookup5(struct super_block *sb, unsigned long hashval, |
| 876 | int (*test)(struct inode *, void *), void *data) |
| 877 | { |
| 878 | struct hlist_head *head = inode_hashtable + hash(sb, hashval); |
| 879 | |
Anton Altaparmakov | 88bd512 | 2005-07-13 01:10:44 -0700 | [diff] [blame] | 880 | return ifind(sb, head, test, data, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | EXPORT_SYMBOL(ilookup5); |
| 884 | |
| 885 | /** |
| 886 | * ilookup - search for an inode in the inode cache |
| 887 | * @sb: super block of file system to search |
| 888 | * @ino: inode number to search for |
| 889 | * |
| 890 | * ilookup() uses ifind_fast() to search for the inode @ino in the inode cache. |
| 891 | * This is for file systems where the inode number is sufficient for unique |
| 892 | * identification of an inode. |
| 893 | * |
| 894 | * If the inode is in the cache, the inode is returned with an incremented |
| 895 | * reference count. |
| 896 | * |
| 897 | * Otherwise NULL is returned. |
| 898 | */ |
| 899 | struct inode *ilookup(struct super_block *sb, unsigned long ino) |
| 900 | { |
| 901 | struct hlist_head *head = inode_hashtable + hash(sb, ino); |
| 902 | |
| 903 | return ifind_fast(sb, head, ino); |
| 904 | } |
| 905 | |
| 906 | EXPORT_SYMBOL(ilookup); |
| 907 | |
| 908 | /** |
| 909 | * iget5_locked - obtain an inode from a mounted file system |
| 910 | * @sb: super block of file system |
| 911 | * @hashval: hash value (usually inode number) to get |
| 912 | * @test: callback used for comparisons between inodes |
| 913 | * @set: callback used to initialize a new struct inode |
| 914 | * @data: opaque data pointer to pass to @test and @set |
| 915 | * |
| 916 | * This is iget() without the read_inode() portion of get_new_inode(). |
| 917 | * |
| 918 | * iget5_locked() uses ifind() to search for the inode specified by @hashval |
| 919 | * and @data in the inode cache and if present it is returned with an increased |
| 920 | * reference count. This is a generalized version of iget_locked() for file |
| 921 | * systems where the inode number is not sufficient for unique identification |
| 922 | * of an inode. |
| 923 | * |
| 924 | * If the inode is not in cache, get_new_inode() is called to allocate a new |
| 925 | * inode and this is returned locked, hashed, and with the I_NEW flag set. The |
| 926 | * file system gets to fill it in before unlocking it via unlock_new_inode(). |
| 927 | * |
| 928 | * Note both @test and @set are called with the inode_lock held, so can't sleep. |
| 929 | */ |
| 930 | struct inode *iget5_locked(struct super_block *sb, unsigned long hashval, |
| 931 | int (*test)(struct inode *, void *), |
| 932 | int (*set)(struct inode *, void *), void *data) |
| 933 | { |
| 934 | struct hlist_head *head = inode_hashtable + hash(sb, hashval); |
| 935 | struct inode *inode; |
| 936 | |
Anton Altaparmakov | 88bd512 | 2005-07-13 01:10:44 -0700 | [diff] [blame] | 937 | inode = ifind(sb, head, test, data, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | if (inode) |
| 939 | return inode; |
| 940 | /* |
| 941 | * get_new_inode() will do the right thing, re-trying the search |
| 942 | * in case it had to block at any point. |
| 943 | */ |
| 944 | return get_new_inode(sb, head, test, set, data); |
| 945 | } |
| 946 | |
| 947 | EXPORT_SYMBOL(iget5_locked); |
| 948 | |
| 949 | /** |
| 950 | * iget_locked - obtain an inode from a mounted file system |
| 951 | * @sb: super block of file system |
| 952 | * @ino: inode number to get |
| 953 | * |
| 954 | * This is iget() without the read_inode() portion of get_new_inode_fast(). |
| 955 | * |
| 956 | * iget_locked() uses ifind_fast() to search for the inode specified by @ino in |
| 957 | * the inode cache and if present it is returned with an increased reference |
| 958 | * count. This is for file systems where the inode number is sufficient for |
| 959 | * unique identification of an inode. |
| 960 | * |
| 961 | * If the inode is not in cache, get_new_inode_fast() is called to allocate a |
| 962 | * new inode and this is returned locked, hashed, and with the I_NEW flag set. |
| 963 | * The file system gets to fill it in before unlocking it via |
| 964 | * unlock_new_inode(). |
| 965 | */ |
| 966 | struct inode *iget_locked(struct super_block *sb, unsigned long ino) |
| 967 | { |
| 968 | struct hlist_head *head = inode_hashtable + hash(sb, ino); |
| 969 | struct inode *inode; |
| 970 | |
| 971 | inode = ifind_fast(sb, head, ino); |
| 972 | if (inode) |
| 973 | return inode; |
| 974 | /* |
| 975 | * get_new_inode_fast() will do the right thing, re-trying the search |
| 976 | * in case it had to block at any point. |
| 977 | */ |
| 978 | return get_new_inode_fast(sb, head, ino); |
| 979 | } |
| 980 | |
| 981 | EXPORT_SYMBOL(iget_locked); |
| 982 | |
| 983 | /** |
| 984 | * __insert_inode_hash - hash an inode |
| 985 | * @inode: unhashed inode |
| 986 | * @hashval: unsigned long value used to locate this object in the |
| 987 | * inode_hashtable. |
| 988 | * |
| 989 | * Add an inode to the inode hash for this superblock. |
| 990 | */ |
| 991 | void __insert_inode_hash(struct inode *inode, unsigned long hashval) |
| 992 | { |
| 993 | struct hlist_head *head = inode_hashtable + hash(inode->i_sb, hashval); |
| 994 | spin_lock(&inode_lock); |
| 995 | hlist_add_head(&inode->i_hash, head); |
| 996 | spin_unlock(&inode_lock); |
| 997 | } |
| 998 | |
| 999 | EXPORT_SYMBOL(__insert_inode_hash); |
| 1000 | |
| 1001 | /** |
| 1002 | * remove_inode_hash - remove an inode from the hash |
| 1003 | * @inode: inode to unhash |
| 1004 | * |
| 1005 | * Remove an inode from the superblock. |
| 1006 | */ |
| 1007 | void remove_inode_hash(struct inode *inode) |
| 1008 | { |
| 1009 | spin_lock(&inode_lock); |
| 1010 | hlist_del_init(&inode->i_hash); |
| 1011 | spin_unlock(&inode_lock); |
| 1012 | } |
| 1013 | |
| 1014 | EXPORT_SYMBOL(remove_inode_hash); |
| 1015 | |
| 1016 | /* |
| 1017 | * Tell the filesystem that this inode is no longer of any interest and should |
| 1018 | * be completely destroyed. |
| 1019 | * |
| 1020 | * We leave the inode in the inode hash table until *after* the filesystem's |
| 1021 | * ->delete_inode completes. This ensures that an iget (such as nfsd might |
| 1022 | * instigate) will always find up-to-date information either in the hash or on |
| 1023 | * disk. |
| 1024 | * |
| 1025 | * I_FREEING is set so that no-one will take a new reference to the inode while |
| 1026 | * it is being deleted. |
| 1027 | */ |
| 1028 | void generic_delete_inode(struct inode *inode) |
| 1029 | { |
| 1030 | struct super_operations *op = inode->i_sb->s_op; |
| 1031 | |
| 1032 | list_del_init(&inode->i_list); |
| 1033 | list_del_init(&inode->i_sb_list); |
| 1034 | inode->i_state|=I_FREEING; |
| 1035 | inodes_stat.nr_inodes--; |
| 1036 | spin_unlock(&inode_lock); |
| 1037 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | security_inode_delete(inode); |
| 1039 | |
| 1040 | if (op->delete_inode) { |
| 1041 | void (*delete)(struct inode *) = op->delete_inode; |
| 1042 | if (!is_bad_inode(inode)) |
| 1043 | DQUOT_INIT(inode); |
Mark Fasheh | e85b565 | 2005-09-09 13:01:29 -0700 | [diff] [blame] | 1044 | /* Filesystems implementing their own |
| 1045 | * s_op->delete_inode are required to call |
| 1046 | * truncate_inode_pages and clear_inode() |
| 1047 | * internally */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | delete(inode); |
Mark Fasheh | e85b565 | 2005-09-09 13:01:29 -0700 | [diff] [blame] | 1049 | } else { |
| 1050 | truncate_inode_pages(&inode->i_data, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | clear_inode(inode); |
Mark Fasheh | e85b565 | 2005-09-09 13:01:29 -0700 | [diff] [blame] | 1052 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | spin_lock(&inode_lock); |
| 1054 | hlist_del_init(&inode->i_hash); |
| 1055 | spin_unlock(&inode_lock); |
| 1056 | wake_up_inode(inode); |
| 1057 | if (inode->i_state != I_CLEAR) |
| 1058 | BUG(); |
| 1059 | destroy_inode(inode); |
| 1060 | } |
| 1061 | |
| 1062 | EXPORT_SYMBOL(generic_delete_inode); |
| 1063 | |
| 1064 | static void generic_forget_inode(struct inode *inode) |
| 1065 | { |
| 1066 | struct super_block *sb = inode->i_sb; |
| 1067 | |
| 1068 | if (!hlist_unhashed(&inode->i_hash)) { |
| 1069 | if (!(inode->i_state & (I_DIRTY|I_LOCK))) |
| 1070 | list_move(&inode->i_list, &inode_unused); |
| 1071 | inodes_stat.nr_unused++; |
Alexander Viro | 991114c | 2005-06-23 00:09:01 -0700 | [diff] [blame] | 1072 | if (!sb || (sb->s_flags & MS_ACTIVE)) { |
| 1073 | spin_unlock(&inode_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | return; |
Alexander Viro | 991114c | 2005-06-23 00:09:01 -0700 | [diff] [blame] | 1075 | } |
| 1076 | inode->i_state |= I_WILL_FREE; |
| 1077 | spin_unlock(&inode_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | write_inode_now(inode, 1); |
| 1079 | spin_lock(&inode_lock); |
Alexander Viro | 991114c | 2005-06-23 00:09:01 -0700 | [diff] [blame] | 1080 | inode->i_state &= ~I_WILL_FREE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1081 | inodes_stat.nr_unused--; |
| 1082 | hlist_del_init(&inode->i_hash); |
| 1083 | } |
| 1084 | list_del_init(&inode->i_list); |
| 1085 | list_del_init(&inode->i_sb_list); |
Alexander Viro | 991114c | 2005-06-23 00:09:01 -0700 | [diff] [blame] | 1086 | inode->i_state |= I_FREEING; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | inodes_stat.nr_inodes--; |
| 1088 | spin_unlock(&inode_lock); |
| 1089 | if (inode->i_data.nrpages) |
| 1090 | truncate_inode_pages(&inode->i_data, 0); |
| 1091 | clear_inode(inode); |
Andrea Arcangeli | 7f04c26 | 2005-10-30 15:03:05 -0800 | [diff] [blame] | 1092 | wake_up_inode(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | destroy_inode(inode); |
| 1094 | } |
| 1095 | |
| 1096 | /* |
| 1097 | * Normal UNIX filesystem behaviour: delete the |
| 1098 | * inode when the usage count drops to zero, and |
| 1099 | * i_nlink is zero. |
| 1100 | */ |
Mark Fasheh | cb2c023 | 2005-07-07 17:56:03 -0700 | [diff] [blame] | 1101 | void generic_drop_inode(struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1102 | { |
| 1103 | if (!inode->i_nlink) |
| 1104 | generic_delete_inode(inode); |
| 1105 | else |
| 1106 | generic_forget_inode(inode); |
| 1107 | } |
| 1108 | |
Mark Fasheh | cb2c023 | 2005-07-07 17:56:03 -0700 | [diff] [blame] | 1109 | EXPORT_SYMBOL_GPL(generic_drop_inode); |
| 1110 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1111 | /* |
| 1112 | * Called when we're dropping the last reference |
| 1113 | * to an inode. |
| 1114 | * |
| 1115 | * Call the FS "drop()" function, defaulting to |
| 1116 | * the legacy UNIX filesystem behaviour.. |
| 1117 | * |
| 1118 | * NOTE! NOTE! NOTE! We're called with the inode lock |
| 1119 | * held, and the drop function is supposed to release |
| 1120 | * the lock! |
| 1121 | */ |
| 1122 | static inline void iput_final(struct inode *inode) |
| 1123 | { |
| 1124 | struct super_operations *op = inode->i_sb->s_op; |
| 1125 | void (*drop)(struct inode *) = generic_drop_inode; |
| 1126 | |
| 1127 | if (op && op->drop_inode) |
| 1128 | drop = op->drop_inode; |
| 1129 | drop(inode); |
| 1130 | } |
| 1131 | |
| 1132 | /** |
| 1133 | * iput - put an inode |
| 1134 | * @inode: inode to put |
| 1135 | * |
| 1136 | * Puts an inode, dropping its usage count. If the inode use count hits |
| 1137 | * zero, the inode is then freed and may also be destroyed. |
| 1138 | * |
| 1139 | * Consequently, iput() can sleep. |
| 1140 | */ |
| 1141 | void iput(struct inode *inode) |
| 1142 | { |
| 1143 | if (inode) { |
| 1144 | struct super_operations *op = inode->i_sb->s_op; |
| 1145 | |
| 1146 | BUG_ON(inode->i_state == I_CLEAR); |
| 1147 | |
| 1148 | if (op && op->put_inode) |
| 1149 | op->put_inode(inode); |
| 1150 | |
| 1151 | if (atomic_dec_and_lock(&inode->i_count, &inode_lock)) |
| 1152 | iput_final(inode); |
| 1153 | } |
| 1154 | } |
| 1155 | |
| 1156 | EXPORT_SYMBOL(iput); |
| 1157 | |
| 1158 | /** |
| 1159 | * bmap - find a block number in a file |
| 1160 | * @inode: inode of file |
| 1161 | * @block: block to find |
| 1162 | * |
| 1163 | * Returns the block number on the device holding the inode that |
| 1164 | * is the disk block number for the block of the file requested. |
| 1165 | * That is, asked for block 4 of inode 1 the function will return the |
| 1166 | * disk block relative to the disk start that holds that block of the |
| 1167 | * file. |
| 1168 | */ |
| 1169 | sector_t bmap(struct inode * inode, sector_t block) |
| 1170 | { |
| 1171 | sector_t res = 0; |
| 1172 | if (inode->i_mapping->a_ops->bmap) |
| 1173 | res = inode->i_mapping->a_ops->bmap(inode->i_mapping, block); |
| 1174 | return res; |
| 1175 | } |
| 1176 | |
| 1177 | EXPORT_SYMBOL(bmap); |
| 1178 | |
| 1179 | /** |
Christoph Hellwig | 869243a | 2006-01-09 20:52:03 -0800 | [diff] [blame] | 1180 | * touch_atime - update the access time |
| 1181 | * @mnt: mount the inode is accessed on |
Martin Waitz | 7045f37 | 2006-02-01 03:06:57 -0800 | [diff] [blame] | 1182 | * @dentry: dentry accessed |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1183 | * |
| 1184 | * Update the accessed time on an inode and mark it for writeback. |
| 1185 | * This function automatically handles read only file systems and media, |
| 1186 | * as well as the "noatime" flag and inode specific "noatime" markers. |
| 1187 | */ |
Christoph Hellwig | 869243a | 2006-01-09 20:52:03 -0800 | [diff] [blame] | 1188 | void touch_atime(struct vfsmount *mnt, struct dentry *dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1189 | { |
Christoph Hellwig | 869243a | 2006-01-09 20:52:03 -0800 | [diff] [blame] | 1190 | struct inode *inode = dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1191 | struct timespec now; |
| 1192 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1193 | if (IS_RDONLY(inode)) |
| 1194 | return; |
| 1195 | |
Christoph Hellwig | fc33a7b | 2006-01-09 20:52:17 -0800 | [diff] [blame] | 1196 | if ((inode->i_flags & S_NOATIME) || |
| 1197 | (inode->i_sb->s_flags & MS_NOATIME) || |
| 1198 | ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode))) |
| 1199 | return; |
| 1200 | |
| 1201 | /* |
| 1202 | * We may have a NULL vfsmount when coming from NFSD |
| 1203 | */ |
| 1204 | if (mnt && |
| 1205 | ((mnt->mnt_flags & MNT_NOATIME) || |
| 1206 | ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode)))) |
| 1207 | return; |
| 1208 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1209 | now = current_fs_time(inode->i_sb); |
| 1210 | if (!timespec_equal(&inode->i_atime, &now)) { |
| 1211 | inode->i_atime = now; |
| 1212 | mark_inode_dirty_sync(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1213 | } |
| 1214 | } |
| 1215 | |
Christoph Hellwig | 869243a | 2006-01-09 20:52:03 -0800 | [diff] [blame] | 1216 | EXPORT_SYMBOL(touch_atime); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1217 | |
| 1218 | /** |
Christoph Hellwig | 870f481 | 2006-01-09 20:52:01 -0800 | [diff] [blame] | 1219 | * file_update_time - update mtime and ctime time |
| 1220 | * @file: file accessed |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | * |
Christoph Hellwig | 870f481 | 2006-01-09 20:52:01 -0800 | [diff] [blame] | 1222 | * Update the mtime and ctime members of an inode and mark the inode |
| 1223 | * for writeback. Note that this function is meant exclusively for |
| 1224 | * usage in the file write path of filesystems, and filesystems may |
| 1225 | * choose to explicitly ignore update via this function with the |
| 1226 | * S_NOCTIME inode flag, e.g. for network filesystem where these |
| 1227 | * timestamps are handled by the server. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | */ |
| 1229 | |
Christoph Hellwig | 870f481 | 2006-01-09 20:52:01 -0800 | [diff] [blame] | 1230 | void file_update_time(struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | { |
Christoph Hellwig | 870f481 | 2006-01-09 20:52:01 -0800 | [diff] [blame] | 1232 | struct inode *inode = file->f_dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | struct timespec now; |
| 1234 | int sync_it = 0; |
| 1235 | |
| 1236 | if (IS_NOCMTIME(inode)) |
| 1237 | return; |
| 1238 | if (IS_RDONLY(inode)) |
| 1239 | return; |
| 1240 | |
| 1241 | now = current_fs_time(inode->i_sb); |
| 1242 | if (!timespec_equal(&inode->i_mtime, &now)) |
| 1243 | sync_it = 1; |
| 1244 | inode->i_mtime = now; |
| 1245 | |
Christoph Hellwig | 870f481 | 2006-01-09 20:52:01 -0800 | [diff] [blame] | 1246 | if (!timespec_equal(&inode->i_ctime, &now)) |
| 1247 | sync_it = 1; |
| 1248 | inode->i_ctime = now; |
| 1249 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1250 | if (sync_it) |
| 1251 | mark_inode_dirty_sync(inode); |
| 1252 | } |
| 1253 | |
Christoph Hellwig | 870f481 | 2006-01-09 20:52:01 -0800 | [diff] [blame] | 1254 | EXPORT_SYMBOL(file_update_time); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1255 | |
| 1256 | int inode_needs_sync(struct inode *inode) |
| 1257 | { |
| 1258 | if (IS_SYNC(inode)) |
| 1259 | return 1; |
| 1260 | if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode)) |
| 1261 | return 1; |
| 1262 | return 0; |
| 1263 | } |
| 1264 | |
| 1265 | EXPORT_SYMBOL(inode_needs_sync); |
| 1266 | |
| 1267 | /* |
| 1268 | * Quota functions that want to walk the inode lists.. |
| 1269 | */ |
| 1270 | #ifdef CONFIG_QUOTA |
| 1271 | |
| 1272 | /* Function back in dquot.c */ |
| 1273 | int remove_inode_dquot_ref(struct inode *, int, struct list_head *); |
| 1274 | |
| 1275 | void remove_dquot_ref(struct super_block *sb, int type, |
| 1276 | struct list_head *tofree_head) |
| 1277 | { |
| 1278 | struct inode *inode; |
| 1279 | |
| 1280 | if (!sb->dq_op) |
| 1281 | return; /* nothing to do */ |
| 1282 | spin_lock(&inode_lock); /* This lock is for inodes code */ |
| 1283 | |
| 1284 | /* |
| 1285 | * We don't have to lock against quota code - test IS_QUOTAINIT is |
| 1286 | * just for speedup... |
| 1287 | */ |
| 1288 | list_for_each_entry(inode, &sb->s_inodes, i_sb_list) |
| 1289 | if (!IS_NOQUOTA(inode)) |
| 1290 | remove_inode_dquot_ref(inode, type, tofree_head); |
| 1291 | |
| 1292 | spin_unlock(&inode_lock); |
| 1293 | } |
| 1294 | |
| 1295 | #endif |
| 1296 | |
| 1297 | int inode_wait(void *word) |
| 1298 | { |
| 1299 | schedule(); |
| 1300 | return 0; |
| 1301 | } |
| 1302 | |
| 1303 | /* |
Miklos Szeredi | 168a9fd | 2005-07-12 13:58:10 -0700 | [diff] [blame] | 1304 | * If we try to find an inode in the inode hash while it is being |
| 1305 | * deleted, we have to wait until the filesystem completes its |
| 1306 | * deletion before reporting that it isn't found. This function waits |
| 1307 | * until the deletion _might_ have completed. Callers are responsible |
| 1308 | * to recheck inode state. |
| 1309 | * |
| 1310 | * It doesn't matter if I_LOCK is not set initially, a call to |
| 1311 | * wake_up_inode() after removing from the hash list will DTRT. |
| 1312 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1313 | * This is called with inode_lock held. |
| 1314 | */ |
| 1315 | static void __wait_on_freeing_inode(struct inode *inode) |
| 1316 | { |
| 1317 | wait_queue_head_t *wq; |
| 1318 | DEFINE_WAIT_BIT(wait, &inode->i_state, __I_LOCK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1319 | wq = bit_waitqueue(&inode->i_state, __I_LOCK); |
| 1320 | prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE); |
| 1321 | spin_unlock(&inode_lock); |
| 1322 | schedule(); |
| 1323 | finish_wait(wq, &wait.wait); |
| 1324 | spin_lock(&inode_lock); |
| 1325 | } |
| 1326 | |
| 1327 | void wake_up_inode(struct inode *inode) |
| 1328 | { |
| 1329 | /* |
| 1330 | * Prevent speculative execution through spin_unlock(&inode_lock); |
| 1331 | */ |
| 1332 | smp_mb(); |
| 1333 | wake_up_bit(&inode->i_state, __I_LOCK); |
| 1334 | } |
| 1335 | |
| 1336 | static __initdata unsigned long ihash_entries; |
| 1337 | static int __init set_ihash_entries(char *str) |
| 1338 | { |
| 1339 | if (!str) |
| 1340 | return 0; |
| 1341 | ihash_entries = simple_strtoul(str, &str, 0); |
| 1342 | return 1; |
| 1343 | } |
| 1344 | __setup("ihash_entries=", set_ihash_entries); |
| 1345 | |
| 1346 | /* |
| 1347 | * Initialize the waitqueues and inode hash table. |
| 1348 | */ |
| 1349 | void __init inode_init_early(void) |
| 1350 | { |
| 1351 | int loop; |
| 1352 | |
| 1353 | /* If hashes are distributed across NUMA nodes, defer |
| 1354 | * hash allocation until vmalloc space is available. |
| 1355 | */ |
| 1356 | if (hashdist) |
| 1357 | return; |
| 1358 | |
| 1359 | inode_hashtable = |
| 1360 | alloc_large_system_hash("Inode-cache", |
| 1361 | sizeof(struct hlist_head), |
| 1362 | ihash_entries, |
| 1363 | 14, |
| 1364 | HASH_EARLY, |
| 1365 | &i_hash_shift, |
| 1366 | &i_hash_mask, |
| 1367 | 0); |
| 1368 | |
| 1369 | for (loop = 0; loop < (1 << i_hash_shift); loop++) |
| 1370 | INIT_HLIST_HEAD(&inode_hashtable[loop]); |
| 1371 | } |
| 1372 | |
| 1373 | void __init inode_init(unsigned long mempages) |
| 1374 | { |
| 1375 | int loop; |
| 1376 | |
| 1377 | /* inode slab cache */ |
| 1378 | inode_cachep = kmem_cache_create("inode_cache", sizeof(struct inode), |
Andrea Arcangeli | e422fd2 | 2005-05-05 16:15:04 -0700 | [diff] [blame] | 1379 | 0, SLAB_RECLAIM_ACCOUNT|SLAB_PANIC, init_once, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1380 | set_shrinker(DEFAULT_SEEKS, shrink_icache_memory); |
| 1381 | |
| 1382 | /* Hash may have been set up in inode_init_early */ |
| 1383 | if (!hashdist) |
| 1384 | return; |
| 1385 | |
| 1386 | inode_hashtable = |
| 1387 | alloc_large_system_hash("Inode-cache", |
| 1388 | sizeof(struct hlist_head), |
| 1389 | ihash_entries, |
| 1390 | 14, |
| 1391 | 0, |
| 1392 | &i_hash_shift, |
| 1393 | &i_hash_mask, |
| 1394 | 0); |
| 1395 | |
| 1396 | for (loop = 0; loop < (1 << i_hash_shift); loop++) |
| 1397 | INIT_HLIST_HEAD(&inode_hashtable[loop]); |
| 1398 | } |
| 1399 | |
| 1400 | void init_special_inode(struct inode *inode, umode_t mode, dev_t rdev) |
| 1401 | { |
| 1402 | inode->i_mode = mode; |
| 1403 | if (S_ISCHR(mode)) { |
| 1404 | inode->i_fop = &def_chr_fops; |
| 1405 | inode->i_rdev = rdev; |
| 1406 | } else if (S_ISBLK(mode)) { |
| 1407 | inode->i_fop = &def_blk_fops; |
| 1408 | inode->i_rdev = rdev; |
| 1409 | } else if (S_ISFIFO(mode)) |
| 1410 | inode->i_fop = &def_fifo_fops; |
| 1411 | else if (S_ISSOCK(mode)) |
| 1412 | inode->i_fop = &bad_sock_fops; |
| 1413 | else |
| 1414 | printk(KERN_DEBUG "init_special_inode: bogus i_mode (%o)\n", |
| 1415 | mode); |
| 1416 | } |
| 1417 | EXPORT_SYMBOL(init_special_inode); |