diff options
Diffstat (limited to 'fs/btrfs/ordered-data.c')
| -rw-r--r-- | fs/btrfs/ordered-data.c | 128 |
1 files changed, 84 insertions, 44 deletions
diff --git a/fs/btrfs/ordered-data.c b/fs/btrfs/ordered-data.c index 1ddd728541ee..81369827e514 100644 --- a/fs/btrfs/ordered-data.c +++ b/fs/btrfs/ordered-data.c | |||
| @@ -24,6 +24,7 @@ | |||
| 24 | #include "transaction.h" | 24 | #include "transaction.h" |
| 25 | #include "btrfs_inode.h" | 25 | #include "btrfs_inode.h" |
| 26 | #include "extent_io.h" | 26 | #include "extent_io.h" |
| 27 | #include "disk-io.h" | ||
| 27 | 28 | ||
| 28 | static struct kmem_cache *btrfs_ordered_extent_cache; | 29 | static struct kmem_cache *btrfs_ordered_extent_cache; |
| 29 | 30 | ||
| @@ -184,6 +185,7 @@ static int __btrfs_add_ordered_extent(struct inode *inode, u64 file_offset, | |||
| 184 | u64 start, u64 len, u64 disk_len, | 185 | u64 start, u64 len, u64 disk_len, |
| 185 | int type, int dio, int compress_type) | 186 | int type, int dio, int compress_type) |
| 186 | { | 187 | { |
| 188 | struct btrfs_root *root = BTRFS_I(inode)->root; | ||
| 187 | struct btrfs_ordered_inode_tree *tree; | 189 | struct btrfs_ordered_inode_tree *tree; |
| 188 | struct rb_node *node; | 190 | struct rb_node *node; |
| 189 | struct btrfs_ordered_extent *entry; | 191 | struct btrfs_ordered_extent *entry; |
| @@ -227,10 +229,18 @@ static int __btrfs_add_ordered_extent(struct inode *inode, u64 file_offset, | |||
| 227 | ordered_data_tree_panic(inode, -EEXIST, file_offset); | 229 | ordered_data_tree_panic(inode, -EEXIST, file_offset); |
| 228 | spin_unlock_irq(&tree->lock); | 230 | spin_unlock_irq(&tree->lock); |
| 229 | 231 | ||
| 230 | spin_lock(&BTRFS_I(inode)->root->fs_info->ordered_extent_lock); | 232 | spin_lock(&root->ordered_extent_lock); |
| 231 | list_add_tail(&entry->root_extent_list, | 233 | list_add_tail(&entry->root_extent_list, |
| 232 | &BTRFS_I(inode)->root->fs_info->ordered_extents); | 234 | &root->ordered_extents); |
| 233 | spin_unlock(&BTRFS_I(inode)->root->fs_info->ordered_extent_lock); | 235 | root->nr_ordered_extents++; |
| 236 | if (root->nr_ordered_extents == 1) { | ||
| 237 | spin_lock(&root->fs_info->ordered_root_lock); | ||
| 238 | BUG_ON(!list_empty(&root->ordered_root)); | ||
| 239 | list_add_tail(&root->ordered_root, | ||
| 240 | &root->fs_info->ordered_roots); | ||
| 241 | spin_unlock(&root->fs_info->ordered_root_lock); | ||
| 242 | } | ||
| 243 | spin_unlock(&root->ordered_extent_lock); | ||
| 234 | 244 | ||
| 235 | return 0; | 245 | return 0; |
| 236 | } | 246 | } |
| @@ -516,8 +526,9 @@ void btrfs_remove_ordered_extent(struct inode *inode, | |||
| 516 | set_bit(BTRFS_ORDERED_COMPLETE, &entry->flags); | 526 | set_bit(BTRFS_ORDERED_COMPLETE, &entry->flags); |
| 517 | spin_unlock_irq(&tree->lock); | 527 | spin_unlock_irq(&tree->lock); |
| 518 | 528 | ||
| 519 | spin_lock(&root->fs_info->ordered_extent_lock); | 529 | spin_lock(&root->ordered_extent_lock); |
| 520 | list_del_init(&entry->root_extent_list); | 530 | list_del_init(&entry->root_extent_list); |
| 531 | root->nr_ordered_extents--; | ||
| 521 | 532 | ||
| 522 | trace_btrfs_ordered_extent_remove(inode, entry); | 533 | trace_btrfs_ordered_extent_remove(inode, entry); |
| 523 | 534 | ||
| @@ -530,7 +541,14 @@ void btrfs_remove_ordered_extent(struct inode *inode, | |||
| 530 | !mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY)) { | 541 | !mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY)) { |
| 531 | list_del_init(&BTRFS_I(inode)->ordered_operations); | 542 | list_del_init(&BTRFS_I(inode)->ordered_operations); |
| 532 | } | 543 | } |
| 533 | spin_unlock(&root->fs_info->ordered_extent_lock); | 544 | |
| 545 | if (!root->nr_ordered_extents) { | ||
| 546 | spin_lock(&root->fs_info->ordered_root_lock); | ||
| 547 | BUG_ON(list_empty(&root->ordered_root)); | ||
| 548 | list_del_init(&root->ordered_root); | ||
| 549 | spin_unlock(&root->fs_info->ordered_root_lock); | ||
| 550 | } | ||
| 551 | spin_unlock(&root->ordered_extent_lock); | ||
| 534 | wake_up(&entry->wait); | 552 | wake_up(&entry->wait); |
| 535 | } | 553 | } |
| 536 | 554 | ||
| @@ -550,7 +568,6 @@ static void btrfs_run_ordered_extent_work(struct btrfs_work *work) | |||
| 550 | void btrfs_wait_ordered_extents(struct btrfs_root *root, int delay_iput) | 568 | void btrfs_wait_ordered_extents(struct btrfs_root *root, int delay_iput) |
| 551 | { | 569 | { |
| 552 | struct list_head splice, works; | 570 | struct list_head splice, works; |
| 553 | struct list_head *cur; | ||
| 554 | struct btrfs_ordered_extent *ordered, *next; | 571 | struct btrfs_ordered_extent *ordered, *next; |
| 555 | struct inode *inode; | 572 | struct inode *inode; |
| 556 | 573 | ||
| @@ -558,35 +575,34 @@ void btrfs_wait_ordered_extents(struct btrfs_root *root, int delay_iput) | |||
| 558 | INIT_LIST_HEAD(&works); | 575 | INIT_LIST_HEAD(&works); |
| 559 | 576 | ||
| 560 | mutex_lock(&root->fs_info->ordered_operations_mutex); | 577 | mutex_lock(&root->fs_info->ordered_operations_mutex); |
| 561 | spin_lock(&root->fs_info->ordered_extent_lock); | 578 | spin_lock(&root->ordered_extent_lock); |
| 562 | list_splice_init(&root->fs_info->ordered_extents, &splice); | 579 | list_splice_init(&root->ordered_extents, &splice); |
| 563 | while (!list_empty(&splice)) { | 580 | while (!list_empty(&splice)) { |
| 564 | cur = splice.next; | 581 | ordered = list_first_entry(&splice, struct btrfs_ordered_extent, |
| 565 | ordered = list_entry(cur, struct btrfs_ordered_extent, | 582 | root_extent_list); |
| 566 | root_extent_list); | 583 | list_move_tail(&ordered->root_extent_list, |
| 567 | list_del_init(&ordered->root_extent_list); | 584 | &root->ordered_extents); |
| 568 | atomic_inc(&ordered->refs); | ||
| 569 | |||
| 570 | /* | 585 | /* |
| 571 | * the inode may be getting freed (in sys_unlink path). | 586 | * the inode may be getting freed (in sys_unlink path). |
| 572 | */ | 587 | */ |
| 573 | inode = igrab(ordered->inode); | 588 | inode = igrab(ordered->inode); |
| 589 | if (!inode) { | ||
| 590 | cond_resched_lock(&root->ordered_extent_lock); | ||
| 591 | continue; | ||
| 592 | } | ||
| 574 | 593 | ||
| 575 | spin_unlock(&root->fs_info->ordered_extent_lock); | 594 | atomic_inc(&ordered->refs); |
| 595 | spin_unlock(&root->ordered_extent_lock); | ||
| 576 | 596 | ||
| 577 | if (inode) { | 597 | ordered->flush_work.func = btrfs_run_ordered_extent_work; |
| 578 | ordered->flush_work.func = btrfs_run_ordered_extent_work; | 598 | list_add_tail(&ordered->work_list, &works); |
| 579 | list_add_tail(&ordered->work_list, &works); | 599 | btrfs_queue_worker(&root->fs_info->flush_workers, |
| 580 | btrfs_queue_worker(&root->fs_info->flush_workers, | 600 | &ordered->flush_work); |
| 581 | &ordered->flush_work); | ||
| 582 | } else { | ||
| 583 | btrfs_put_ordered_extent(ordered); | ||
| 584 | } | ||
| 585 | 601 | ||
| 586 | cond_resched(); | 602 | cond_resched(); |
| 587 | spin_lock(&root->fs_info->ordered_extent_lock); | 603 | spin_lock(&root->ordered_extent_lock); |
| 588 | } | 604 | } |
| 589 | spin_unlock(&root->fs_info->ordered_extent_lock); | 605 | spin_unlock(&root->ordered_extent_lock); |
| 590 | 606 | ||
| 591 | list_for_each_entry_safe(ordered, next, &works, work_list) { | 607 | list_for_each_entry_safe(ordered, next, &works, work_list) { |
| 592 | list_del_init(&ordered->work_list); | 608 | list_del_init(&ordered->work_list); |
| @@ -604,6 +620,33 @@ void btrfs_wait_ordered_extents(struct btrfs_root *root, int delay_iput) | |||
| 604 | mutex_unlock(&root->fs_info->ordered_operations_mutex); | 620 | mutex_unlock(&root->fs_info->ordered_operations_mutex); |
| 605 | } | 621 | } |
| 606 | 622 | ||
| 623 | void btrfs_wait_all_ordered_extents(struct btrfs_fs_info *fs_info, | ||
| 624 | int delay_iput) | ||
| 625 | { | ||
| 626 | struct btrfs_root *root; | ||
| 627 | struct list_head splice; | ||
| 628 | |||
| 629 | INIT_LIST_HEAD(&splice); | ||
| 630 | |||
| 631 | spin_lock(&fs_info->ordered_root_lock); | ||
| 632 | list_splice_init(&fs_info->ordered_roots, &splice); | ||
| 633 | while (!list_empty(&splice)) { | ||
| 634 | root = list_first_entry(&splice, struct btrfs_root, | ||
| 635 | ordered_root); | ||
| 636 | root = btrfs_grab_fs_root(root); | ||
| 637 | BUG_ON(!root); | ||
| 638 | list_move_tail(&root->ordered_root, | ||
| 639 | &fs_info->ordered_roots); | ||
| 640 | spin_unlock(&fs_info->ordered_root_lock); | ||
| 641 | |||
| 642 | btrfs_wait_ordered_extents(root, delay_iput); | ||
| 643 | btrfs_put_fs_root(root); | ||
| 644 | |||
| 645 | spin_lock(&fs_info->ordered_root_lock); | ||
| 646 | } | ||
| 647 | spin_unlock(&fs_info->ordered_root_lock); | ||
| 648 | } | ||
| 649 | |||
| 607 | /* | 650 | /* |
| 608 | * this is used during transaction commit to write all the inodes | 651 | * this is used during transaction commit to write all the inodes |
| 609 | * added to the ordered operation list. These files must be fully on | 652 | * added to the ordered operation list. These files must be fully on |
| @@ -629,7 +672,7 @@ int btrfs_run_ordered_operations(struct btrfs_trans_handle *trans, | |||
| 629 | INIT_LIST_HEAD(&works); | 672 | INIT_LIST_HEAD(&works); |
| 630 | 673 | ||
| 631 | mutex_lock(&root->fs_info->ordered_operations_mutex); | 674 | mutex_lock(&root->fs_info->ordered_operations_mutex); |
| 632 | spin_lock(&root->fs_info->ordered_extent_lock); | 675 | spin_lock(&root->fs_info->ordered_root_lock); |
| 633 | list_splice_init(&cur_trans->ordered_operations, &splice); | 676 | list_splice_init(&cur_trans->ordered_operations, &splice); |
| 634 | while (!list_empty(&splice)) { | 677 | while (!list_empty(&splice)) { |
| 635 | btrfs_inode = list_entry(splice.next, struct btrfs_inode, | 678 | btrfs_inode = list_entry(splice.next, struct btrfs_inode, |
| @@ -648,17 +691,17 @@ int btrfs_run_ordered_operations(struct btrfs_trans_handle *trans, | |||
| 648 | if (!wait) | 691 | if (!wait) |
| 649 | list_add_tail(&BTRFS_I(inode)->ordered_operations, | 692 | list_add_tail(&BTRFS_I(inode)->ordered_operations, |
| 650 | &cur_trans->ordered_operations); | 693 | &cur_trans->ordered_operations); |
| 651 | spin_unlock(&root->fs_info->ordered_extent_lock); | 694 | spin_unlock(&root->fs_info->ordered_root_lock); |
| 652 | 695 | ||
| 653 | work = btrfs_alloc_delalloc_work(inode, wait, 1); | 696 | work = btrfs_alloc_delalloc_work(inode, wait, 1); |
| 654 | if (!work) { | 697 | if (!work) { |
| 655 | spin_lock(&root->fs_info->ordered_extent_lock); | 698 | spin_lock(&root->fs_info->ordered_root_lock); |
| 656 | if (list_empty(&BTRFS_I(inode)->ordered_operations)) | 699 | if (list_empty(&BTRFS_I(inode)->ordered_operations)) |
| 657 | list_add_tail(&btrfs_inode->ordered_operations, | 700 | list_add_tail(&btrfs_inode->ordered_operations, |
| 658 | &splice); | 701 | &splice); |
| 659 | list_splice_tail(&splice, | 702 | list_splice_tail(&splice, |
| 660 | &cur_trans->ordered_operations); | 703 | &cur_trans->ordered_operations); |
| 661 | spin_unlock(&root->fs_info->ordered_extent_lock); | 704 | spin_unlock(&root->fs_info->ordered_root_lock); |
| 662 | ret = -ENOMEM; | 705 | ret = -ENOMEM; |
| 663 | goto out; | 706 | goto out; |
| 664 | } | 707 | } |
| @@ -667,9 +710,9 @@ int btrfs_run_ordered_operations(struct btrfs_trans_handle *trans, | |||
| 667 | &work->work); | 710 | &work->work); |
| 668 | 711 | ||
| 669 | cond_resched(); | 712 | cond_resched(); |
| 670 | spin_lock(&root->fs_info->ordered_extent_lock); | 713 | spin_lock(&root->fs_info->ordered_root_lock); |
| 671 | } | 714 | } |
| 672 | spin_unlock(&root->fs_info->ordered_extent_lock); | 715 | spin_unlock(&root->fs_info->ordered_root_lock); |
| 673 | out: | 716 | out: |
| 674 | list_for_each_entry_safe(work, next, &works, list) { | 717 | list_for_each_entry_safe(work, next, &works, list) { |
| 675 | list_del_init(&work->list); | 718 | list_del_init(&work->list); |
| @@ -989,7 +1032,6 @@ int btrfs_find_ordered_sum(struct inode *inode, u64 offset, u64 disk_bytenr, | |||
| 989 | u32 *sum, int len) | 1032 | u32 *sum, int len) |
| 990 | { | 1033 | { |
| 991 | struct btrfs_ordered_sum *ordered_sum; | 1034 | struct btrfs_ordered_sum *ordered_sum; |
| 992 | struct btrfs_sector_sum *sector_sums; | ||
| 993 | struct btrfs_ordered_extent *ordered; | 1035 | struct btrfs_ordered_extent *ordered; |
| 994 | struct btrfs_ordered_inode_tree *tree = &BTRFS_I(inode)->ordered_tree; | 1036 | struct btrfs_ordered_inode_tree *tree = &BTRFS_I(inode)->ordered_tree; |
| 995 | unsigned long num_sectors; | 1037 | unsigned long num_sectors; |
| @@ -1007,18 +1049,16 @@ int btrfs_find_ordered_sum(struct inode *inode, u64 offset, u64 disk_bytenr, | |||
| 1007 | disk_bytenr < ordered_sum->bytenr + ordered_sum->len) { | 1049 | disk_bytenr < ordered_sum->bytenr + ordered_sum->len) { |
| 1008 | i = (disk_bytenr - ordered_sum->bytenr) >> | 1050 | i = (disk_bytenr - ordered_sum->bytenr) >> |
| 1009 | inode->i_sb->s_blocksize_bits; | 1051 | inode->i_sb->s_blocksize_bits; |
| 1010 | sector_sums = ordered_sum->sums + i; | ||
| 1011 | num_sectors = ordered_sum->len >> | 1052 | num_sectors = ordered_sum->len >> |
| 1012 | inode->i_sb->s_blocksize_bits; | 1053 | inode->i_sb->s_blocksize_bits; |
| 1013 | for (; i < num_sectors; i++) { | 1054 | num_sectors = min_t(int, len - index, num_sectors - i); |
| 1014 | if (sector_sums[i].bytenr == disk_bytenr) { | 1055 | memcpy(sum + index, ordered_sum->sums + i, |
| 1015 | sum[index] = sector_sums[i].sum; | 1056 | num_sectors); |
| 1016 | index++; | 1057 | |
| 1017 | if (index == len) | 1058 | index += (int)num_sectors; |
| 1018 | goto out; | 1059 | if (index == len) |
| 1019 | disk_bytenr += sectorsize; | 1060 | goto out; |
| 1020 | } | 1061 | disk_bytenr += num_sectors * sectorsize; |
| 1021 | } | ||
| 1022 | } | 1062 | } |
| 1023 | } | 1063 | } |
| 1024 | out: | 1064 | out: |
| @@ -1055,12 +1095,12 @@ void btrfs_add_ordered_operation(struct btrfs_trans_handle *trans, | |||
| 1055 | if (last_mod < root->fs_info->last_trans_committed) | 1095 | if (last_mod < root->fs_info->last_trans_committed) |
| 1056 | return; | 1096 | return; |
| 1057 | 1097 | ||
| 1058 | spin_lock(&root->fs_info->ordered_extent_lock); | 1098 | spin_lock(&root->fs_info->ordered_root_lock); |
| 1059 | if (list_empty(&BTRFS_I(inode)->ordered_operations)) { | 1099 | if (list_empty(&BTRFS_I(inode)->ordered_operations)) { |
| 1060 | list_add_tail(&BTRFS_I(inode)->ordered_operations, | 1100 | list_add_tail(&BTRFS_I(inode)->ordered_operations, |
| 1061 | &cur_trans->ordered_operations); | 1101 | &cur_trans->ordered_operations); |
| 1062 | } | 1102 | } |
| 1063 | spin_unlock(&root->fs_info->ordered_extent_lock); | 1103 | spin_unlock(&root->fs_info->ordered_root_lock); |
| 1064 | } | 1104 | } |
| 1065 | 1105 | ||
| 1066 | int __init ordered_data_init(void) | 1106 | int __init ordered_data_init(void) |
