aboutsummaryrefslogtreecommitdiffstats
path: root/fs/btrfs/ordered-data.c
diff options
context:
space:
mode:
authorTejun Heo <tj@kernel.org>2010-01-04 19:17:33 -0500
committerTejun Heo <tj@kernel.org>2010-01-04 19:17:33 -0500
commit32032df6c2f6c9c6b2ada2ce42322231824f70c2 (patch)
treeb1ce838a37044bb38dfc128e2116ca35630e629a /fs/btrfs/ordered-data.c
parent22b737f4c75197372d64afc6ed1bccd58c00e549 (diff)
parentc5974b835a909ff15c3b7e6cf6789b5eb919f419 (diff)
Merge branch 'master' into percpu
Conflicts: arch/powerpc/platforms/pseries/hvCall.S include/linux/percpu.h
Diffstat (limited to 'fs/btrfs/ordered-data.c')
-rw-r--r--fs/btrfs/ordered-data.c121
1 files changed, 94 insertions, 27 deletions
diff --git a/fs/btrfs/ordered-data.c b/fs/btrfs/ordered-data.c
index 897fba835f89..b10a49d4bc6a 100644
--- a/fs/btrfs/ordered-data.c
+++ b/fs/btrfs/ordered-data.c
@@ -291,21 +291,27 @@ int btrfs_put_ordered_extent(struct btrfs_ordered_extent *entry)
291 291
292/* 292/*
293 * remove an ordered extent from the tree. No references are dropped 293 * remove an ordered extent from the tree. No references are dropped
294 * but, anyone waiting on this extent is woken up. 294 * and you must wake_up entry->wait. You must hold the tree mutex
295 * while you call this function.
295 */ 296 */
296int btrfs_remove_ordered_extent(struct inode *inode, 297static int __btrfs_remove_ordered_extent(struct inode *inode,
297 struct btrfs_ordered_extent *entry) 298 struct btrfs_ordered_extent *entry)
298{ 299{
299 struct btrfs_ordered_inode_tree *tree; 300 struct btrfs_ordered_inode_tree *tree;
300 struct rb_node *node; 301 struct rb_node *node;
301 302
302 tree = &BTRFS_I(inode)->ordered_tree; 303 tree = &BTRFS_I(inode)->ordered_tree;
303 mutex_lock(&tree->mutex);
304 node = &entry->rb_node; 304 node = &entry->rb_node;
305 rb_erase(node, &tree->tree); 305 rb_erase(node, &tree->tree);
306 tree->last = NULL; 306 tree->last = NULL;
307 set_bit(BTRFS_ORDERED_COMPLETE, &entry->flags); 307 set_bit(BTRFS_ORDERED_COMPLETE, &entry->flags);
308 308
309 spin_lock(&BTRFS_I(inode)->accounting_lock);
310 BTRFS_I(inode)->outstanding_extents--;
311 spin_unlock(&BTRFS_I(inode)->accounting_lock);
312 btrfs_unreserve_metadata_for_delalloc(BTRFS_I(inode)->root,
313 inode, 1);
314
309 spin_lock(&BTRFS_I(inode)->root->fs_info->ordered_extent_lock); 315 spin_lock(&BTRFS_I(inode)->root->fs_info->ordered_extent_lock);
310 list_del_init(&entry->root_extent_list); 316 list_del_init(&entry->root_extent_list);
311 317
@@ -320,16 +326,34 @@ int btrfs_remove_ordered_extent(struct inode *inode,
320 } 326 }
321 spin_unlock(&BTRFS_I(inode)->root->fs_info->ordered_extent_lock); 327 spin_unlock(&BTRFS_I(inode)->root->fs_info->ordered_extent_lock);
322 328
329 return 0;
330}
331
332/*
333 * remove an ordered extent from the tree. No references are dropped
334 * but any waiters are woken.
335 */
336int btrfs_remove_ordered_extent(struct inode *inode,
337 struct btrfs_ordered_extent *entry)
338{
339 struct btrfs_ordered_inode_tree *tree;
340 int ret;
341
342 tree = &BTRFS_I(inode)->ordered_tree;
343 mutex_lock(&tree->mutex);
344 ret = __btrfs_remove_ordered_extent(inode, entry);
323 mutex_unlock(&tree->mutex); 345 mutex_unlock(&tree->mutex);
324 wake_up(&entry->wait); 346 wake_up(&entry->wait);
325 return 0; 347
348 return ret;
326} 349}
327 350
328/* 351/*
329 * wait for all the ordered extents in a root. This is done when balancing 352 * wait for all the ordered extents in a root. This is done when balancing
330 * space between drives. 353 * space between drives.
331 */ 354 */
332int btrfs_wait_ordered_extents(struct btrfs_root *root, int nocow_only) 355int btrfs_wait_ordered_extents(struct btrfs_root *root,
356 int nocow_only, int delay_iput)
333{ 357{
334 struct list_head splice; 358 struct list_head splice;
335 struct list_head *cur; 359 struct list_head *cur;
@@ -366,7 +390,10 @@ int btrfs_wait_ordered_extents(struct btrfs_root *root, int nocow_only)
366 if (inode) { 390 if (inode) {
367 btrfs_start_ordered_extent(inode, ordered, 1); 391 btrfs_start_ordered_extent(inode, ordered, 1);
368 btrfs_put_ordered_extent(ordered); 392 btrfs_put_ordered_extent(ordered);
369 iput(inode); 393 if (delay_iput)
394 btrfs_add_delayed_iput(inode);
395 else
396 iput(inode);
370 } else { 397 } else {
371 btrfs_put_ordered_extent(ordered); 398 btrfs_put_ordered_extent(ordered);
372 } 399 }
@@ -424,7 +451,7 @@ again:
424 btrfs_wait_ordered_range(inode, 0, (u64)-1); 451 btrfs_wait_ordered_range(inode, 0, (u64)-1);
425 else 452 else
426 filemap_flush(inode->i_mapping); 453 filemap_flush(inode->i_mapping);
427 iput(inode); 454 btrfs_add_delayed_iput(inode);
428 } 455 }
429 456
430 cond_resched(); 457 cond_resched();
@@ -583,7 +610,7 @@ out:
583 * After an extent is done, call this to conditionally update the on disk 610 * After an extent is done, call this to conditionally update the on disk
584 * i_size. i_size is updated to cover any fully written part of the file. 611 * i_size. i_size is updated to cover any fully written part of the file.
585 */ 612 */
586int btrfs_ordered_update_i_size(struct inode *inode, 613int btrfs_ordered_update_i_size(struct inode *inode, u64 offset,
587 struct btrfs_ordered_extent *ordered) 614 struct btrfs_ordered_extent *ordered)
588{ 615{
589 struct btrfs_ordered_inode_tree *tree = &BTRFS_I(inode)->ordered_tree; 616 struct btrfs_ordered_inode_tree *tree = &BTRFS_I(inode)->ordered_tree;
@@ -591,18 +618,30 @@ int btrfs_ordered_update_i_size(struct inode *inode,
591 u64 disk_i_size; 618 u64 disk_i_size;
592 u64 new_i_size; 619 u64 new_i_size;
593 u64 i_size_test; 620 u64 i_size_test;
621 u64 i_size = i_size_read(inode);
594 struct rb_node *node; 622 struct rb_node *node;
623 struct rb_node *prev = NULL;
595 struct btrfs_ordered_extent *test; 624 struct btrfs_ordered_extent *test;
625 int ret = 1;
626
627 if (ordered)
628 offset = entry_end(ordered);
596 629
597 mutex_lock(&tree->mutex); 630 mutex_lock(&tree->mutex);
598 disk_i_size = BTRFS_I(inode)->disk_i_size; 631 disk_i_size = BTRFS_I(inode)->disk_i_size;
599 632
633 /* truncate file */
634 if (disk_i_size > i_size) {
635 BTRFS_I(inode)->disk_i_size = i_size;
636 ret = 0;
637 goto out;
638 }
639
600 /* 640 /*
601 * if the disk i_size is already at the inode->i_size, or 641 * if the disk i_size is already at the inode->i_size, or
602 * this ordered extent is inside the disk i_size, we're done 642 * this ordered extent is inside the disk i_size, we're done
603 */ 643 */
604 if (disk_i_size >= inode->i_size || 644 if (disk_i_size == i_size || offset <= disk_i_size) {
605 ordered->file_offset + ordered->len <= disk_i_size) {
606 goto out; 645 goto out;
607 } 646 }
608 647
@@ -610,8 +649,7 @@ int btrfs_ordered_update_i_size(struct inode *inode,
610 * we can't update the disk_isize if there are delalloc bytes 649 * we can't update the disk_isize if there are delalloc bytes
611 * between disk_i_size and this ordered extent 650 * between disk_i_size and this ordered extent
612 */ 651 */
613 if (test_range_bit(io_tree, disk_i_size, 652 if (test_range_bit(io_tree, disk_i_size, offset - 1,
614 ordered->file_offset + ordered->len - 1,
615 EXTENT_DELALLOC, 0, NULL)) { 653 EXTENT_DELALLOC, 0, NULL)) {
616 goto out; 654 goto out;
617 } 655 }
@@ -620,20 +658,32 @@ int btrfs_ordered_update_i_size(struct inode *inode,
620 * if we find an ordered extent then we can't update disk i_size 658 * if we find an ordered extent then we can't update disk i_size
621 * yet 659 * yet
622 */ 660 */
623 node = &ordered->rb_node; 661 if (ordered) {
624 while (1) { 662 node = rb_prev(&ordered->rb_node);
625 node = rb_prev(node); 663 } else {
626 if (!node) 664 prev = tree_search(tree, offset);
627 break; 665 /*
666 * we insert file extents without involving ordered struct,
667 * so there should be no ordered struct cover this offset
668 */
669 if (prev) {
670 test = rb_entry(prev, struct btrfs_ordered_extent,
671 rb_node);
672 BUG_ON(offset_in_entry(test, offset));
673 }
674 node = prev;
675 }
676 while (node) {
628 test = rb_entry(node, struct btrfs_ordered_extent, rb_node); 677 test = rb_entry(node, struct btrfs_ordered_extent, rb_node);
629 if (test->file_offset + test->len <= disk_i_size) 678 if (test->file_offset + test->len <= disk_i_size)
630 break; 679 break;
631 if (test->file_offset >= inode->i_size) 680 if (test->file_offset >= i_size)
632 break; 681 break;
633 if (test->file_offset >= disk_i_size) 682 if (test->file_offset >= disk_i_size)
634 goto out; 683 goto out;
684 node = rb_prev(node);
635 } 685 }
636 new_i_size = min_t(u64, entry_end(ordered), i_size_read(inode)); 686 new_i_size = min_t(u64, offset, i_size);
637 687
638 /* 688 /*
639 * at this point, we know we can safely update i_size to at least 689 * at this point, we know we can safely update i_size to at least
@@ -641,7 +691,14 @@ int btrfs_ordered_update_i_size(struct inode *inode,
641 * walk forward and see if ios from higher up in the file have 691 * walk forward and see if ios from higher up in the file have
642 * finished. 692 * finished.
643 */ 693 */
644 node = rb_next(&ordered->rb_node); 694 if (ordered) {
695 node = rb_next(&ordered->rb_node);
696 } else {
697 if (prev)
698 node = rb_next(prev);
699 else
700 node = rb_first(&tree->tree);
701 }
645 i_size_test = 0; 702 i_size_test = 0;
646 if (node) { 703 if (node) {
647 /* 704 /*
@@ -649,10 +706,10 @@ int btrfs_ordered_update_i_size(struct inode *inode,
649 * between our ordered extent and the next one. 706 * between our ordered extent and the next one.
650 */ 707 */
651 test = rb_entry(node, struct btrfs_ordered_extent, rb_node); 708 test = rb_entry(node, struct btrfs_ordered_extent, rb_node);
652 if (test->file_offset > entry_end(ordered)) 709 if (test->file_offset > offset)
653 i_size_test = test->file_offset; 710 i_size_test = test->file_offset;
654 } else { 711 } else {
655 i_size_test = i_size_read(inode); 712 i_size_test = i_size;
656 } 713 }
657 714
658 /* 715 /*
@@ -661,15 +718,25 @@ int btrfs_ordered_update_i_size(struct inode *inode,
661 * are no delalloc bytes in this area, it is safe to update 718 * are no delalloc bytes in this area, it is safe to update
662 * disk_i_size to the end of the region. 719 * disk_i_size to the end of the region.
663 */ 720 */
664 if (i_size_test > entry_end(ordered) && 721 if (i_size_test > offset &&
665 !test_range_bit(io_tree, entry_end(ordered), i_size_test - 1, 722 !test_range_bit(io_tree, offset, i_size_test - 1,
666 EXTENT_DELALLOC, 0, NULL)) { 723 EXTENT_DELALLOC, 0, NULL)) {
667 new_i_size = min_t(u64, i_size_test, i_size_read(inode)); 724 new_i_size = min_t(u64, i_size_test, i_size);
668 } 725 }
669 BTRFS_I(inode)->disk_i_size = new_i_size; 726 BTRFS_I(inode)->disk_i_size = new_i_size;
727 ret = 0;
670out: 728out:
729 /*
730 * we need to remove the ordered extent with the tree lock held
731 * so that other people calling this function don't find our fully
732 * processed ordered entry and skip updating the i_size
733 */
734 if (ordered)
735 __btrfs_remove_ordered_extent(inode, ordered);
671 mutex_unlock(&tree->mutex); 736 mutex_unlock(&tree->mutex);
672 return 0; 737 if (ordered)
738 wake_up(&ordered->wait);
739 return ret;
673} 740}
674 741
675/* 742/*