fs: factor inode disposal

We have a couple of places that dispose of inodes. factor the
disposal into evict() to isolate this code and make it simpler to
peel away the inode_lock from the code.

While doing this, change the logic flow in iput_final() to separate
the different cases that need to be handled to make the transitions
the inode goes through more obvious.

Signed-off-by: Dave Chinner <dchinner@redhat.com>
Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>

authored by Dave Chinner and committed by Al Viro b2b2af8e 250df6ed

+42 -64
+42 -64
fs/inode.c
··· 423 EXPORT_SYMBOL(__insert_inode_hash); 424 425 /** 426 - * __remove_inode_hash - remove an inode from the hash 427 - * @inode: inode to unhash 428 - * 429 - * Remove an inode from the superblock. 430 - */ 431 - static void __remove_inode_hash(struct inode *inode) 432 - { 433 - hlist_del_init(&inode->i_hash); 434 - } 435 - 436 - /** 437 * remove_inode_hash - remove an inode from the hash 438 * @inode: inode to unhash 439 * ··· 451 } 452 EXPORT_SYMBOL(end_writeback); 453 454 static void evict(struct inode *inode) 455 { 456 const struct super_operations *op = inode->i_sb->s_op; 457 458 if (op->evict_inode) { 459 op->evict_inode(inode); ··· 487 bd_forget(inode); 488 if (S_ISCHR(inode->i_mode) && inode->i_cdev) 489 cd_forget(inode); 490 } 491 492 /* ··· 514 list_del_init(&inode->i_lru); 515 516 evict(inode); 517 - 518 - spin_lock(&inode_lock); 519 - __remove_inode_hash(inode); 520 - __inode_sb_list_del(inode); 521 - spin_unlock(&inode_lock); 522 - 523 - spin_lock(&inode->i_lock); 524 - wake_up_bit(&inode->i_state, __I_NEW); 525 - spin_unlock(&inode->i_lock); 526 - destroy_inode(inode); 527 } 528 } 529 ··· 546 if (!(inode->i_state & (I_DIRTY | I_SYNC))) 547 inodes_stat.nr_unused--; 548 spin_unlock(&inode->i_lock); 549 - 550 - /* 551 - * Move the inode off the IO lists and LRU once I_FREEING is 552 - * set so that it won't get moved back on there if it is dirty. 553 - */ 554 list_move(&inode->i_lru, &dispose); 555 - list_del_init(&inode->i_wb_list); 556 } 557 spin_unlock(&inode_lock); 558 ··· 599 if (!(inode->i_state & (I_DIRTY | I_SYNC))) 600 inodes_stat.nr_unused--; 601 spin_unlock(&inode->i_lock); 602 - 603 - /* 604 - * Move the inode off the IO lists and LRU once I_FREEING is 605 - * set so that it won't get moved back on there if it is dirty. 606 - */ 607 list_move(&inode->i_lru, &dispose); 608 - list_del_init(&inode->i_wb_list); 609 } 610 spin_unlock(&inode_lock); 611 ··· 696 inode->i_state |= I_FREEING; 697 spin_unlock(&inode->i_lock); 698 699 - /* 700 - * Move the inode off the IO lists and LRU once I_FREEING is 701 - * set so that it won't get moved back on there if it is dirty. 702 - */ 703 list_move(&inode->i_lru, &freeable); 704 - list_del_init(&inode->i_wb_list); 705 inodes_stat.nr_unused--; 706 } 707 if (current_is_kswapd()) ··· 1426 else 1427 drop = generic_drop_inode(inode); 1428 1429 if (!drop) { 1430 - if (sb->s_flags & MS_ACTIVE) { 1431 - inode->i_state |= I_REFERENCED; 1432 - if (!(inode->i_state & (I_DIRTY|I_SYNC))) { 1433 - inode_lru_list_add(inode); 1434 - } 1435 - spin_unlock(&inode->i_lock); 1436 - spin_unlock(&inode_lock); 1437 - return; 1438 - } 1439 inode->i_state |= I_WILL_FREE; 1440 spin_unlock(&inode->i_lock); 1441 spin_unlock(&inode_lock); ··· 1444 spin_lock(&inode->i_lock); 1445 WARN_ON(inode->i_state & I_NEW); 1446 inode->i_state &= ~I_WILL_FREE; 1447 - __remove_inode_hash(inode); 1448 } 1449 1450 inode->i_state |= I_FREEING; 1451 - spin_unlock(&inode->i_lock); 1452 - 1453 - /* 1454 - * Move the inode off the IO lists and LRU once I_FREEING is 1455 - * set so that it won't get moved back on there if it is dirty. 1456 - */ 1457 inode_lru_list_del(inode); 1458 - list_del_init(&inode->i_wb_list); 1459 - 1460 - __inode_sb_list_del(inode); 1461 - spin_unlock(&inode_lock); 1462 - evict(inode); 1463 - remove_inode_hash(inode); 1464 - spin_lock(&inode->i_lock); 1465 - wake_up_bit(&inode->i_state, __I_NEW); 1466 - BUG_ON(inode->i_state != (I_FREEING | I_CLEAR)); 1467 spin_unlock(&inode->i_lock); 1468 - destroy_inode(inode); 1469 } 1470 1471 /**
··· 423 EXPORT_SYMBOL(__insert_inode_hash); 424 425 /** 426 * remove_inode_hash - remove an inode from the hash 427 * @inode: inode to unhash 428 * ··· 462 } 463 EXPORT_SYMBOL(end_writeback); 464 465 + /* 466 + * Free the inode passed in, removing it from the lists it is still connected 467 + * to. We remove any pages still attached to the inode and wait for any IO that 468 + * is still in progress before finally destroying the inode. 469 + * 470 + * An inode must already be marked I_FREEING so that we avoid the inode being 471 + * moved back onto lists if we race with other code that manipulates the lists 472 + * (e.g. writeback_single_inode). The caller is responsible for setting this. 473 + * 474 + * An inode must already be removed from the LRU list before being evicted from 475 + * the cache. This should occur atomically with setting the I_FREEING state 476 + * flag, so no inodes here should ever be on the LRU when being evicted. 477 + */ 478 static void evict(struct inode *inode) 479 { 480 const struct super_operations *op = inode->i_sb->s_op; 481 + 482 + BUG_ON(!(inode->i_state & I_FREEING)); 483 + BUG_ON(!list_empty(&inode->i_lru)); 484 + 485 + spin_lock(&inode_lock); 486 + list_del_init(&inode->i_wb_list); 487 + __inode_sb_list_del(inode); 488 + spin_unlock(&inode_lock); 489 490 if (op->evict_inode) { 491 op->evict_inode(inode); ··· 477 bd_forget(inode); 478 if (S_ISCHR(inode->i_mode) && inode->i_cdev) 479 cd_forget(inode); 480 + 481 + remove_inode_hash(inode); 482 + 483 + spin_lock(&inode->i_lock); 484 + wake_up_bit(&inode->i_state, __I_NEW); 485 + BUG_ON(inode->i_state != (I_FREEING | I_CLEAR)); 486 + spin_unlock(&inode->i_lock); 487 + 488 + destroy_inode(inode); 489 } 490 491 /* ··· 495 list_del_init(&inode->i_lru); 496 497 evict(inode); 498 } 499 } 500 ··· 537 if (!(inode->i_state & (I_DIRTY | I_SYNC))) 538 inodes_stat.nr_unused--; 539 spin_unlock(&inode->i_lock); 540 list_move(&inode->i_lru, &dispose); 541 } 542 spin_unlock(&inode_lock); 543 ··· 596 if (!(inode->i_state & (I_DIRTY | I_SYNC))) 597 inodes_stat.nr_unused--; 598 spin_unlock(&inode->i_lock); 599 list_move(&inode->i_lru, &dispose); 600 } 601 spin_unlock(&inode_lock); 602 ··· 699 inode->i_state |= I_FREEING; 700 spin_unlock(&inode->i_lock); 701 702 list_move(&inode->i_lru, &freeable); 703 inodes_stat.nr_unused--; 704 } 705 if (current_is_kswapd()) ··· 1434 else 1435 drop = generic_drop_inode(inode); 1436 1437 + if (!drop && (sb->s_flags & MS_ACTIVE)) { 1438 + inode->i_state |= I_REFERENCED; 1439 + if (!(inode->i_state & (I_DIRTY|I_SYNC))) 1440 + inode_lru_list_add(inode); 1441 + spin_unlock(&inode->i_lock); 1442 + spin_unlock(&inode_lock); 1443 + return; 1444 + } 1445 + 1446 if (!drop) { 1447 inode->i_state |= I_WILL_FREE; 1448 spin_unlock(&inode->i_lock); 1449 spin_unlock(&inode_lock); ··· 1452 spin_lock(&inode->i_lock); 1453 WARN_ON(inode->i_state & I_NEW); 1454 inode->i_state &= ~I_WILL_FREE; 1455 } 1456 1457 inode->i_state |= I_FREEING; 1458 inode_lru_list_del(inode); 1459 spin_unlock(&inode->i_lock); 1460 + spin_unlock(&inode_lock); 1461 + 1462 + evict(inode); 1463 } 1464 1465 /**