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