diff options
Diffstat (limited to 'fs/btrfs/extent_io.c')
-rw-r--r-- | fs/btrfs/extent_io.c | 416 |
1 files changed, 264 insertions, 152 deletions
diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c index 68260180f587..de1793ba004a 100644 --- a/fs/btrfs/extent_io.c +++ b/fs/btrfs/extent_io.c | |||
@@ -280,6 +280,14 @@ static struct extent_buffer *buffer_search(struct extent_io_tree *tree, | |||
280 | return NULL; | 280 | return NULL; |
281 | } | 281 | } |
282 | 282 | ||
283 | static void merge_cb(struct extent_io_tree *tree, struct extent_state *new, | ||
284 | struct extent_state *other) | ||
285 | { | ||
286 | if (tree->ops && tree->ops->merge_extent_hook) | ||
287 | tree->ops->merge_extent_hook(tree->mapping->host, new, | ||
288 | other); | ||
289 | } | ||
290 | |||
283 | /* | 291 | /* |
284 | * utility function to look for merge candidates inside a given range. | 292 | * utility function to look for merge candidates inside a given range. |
285 | * Any extents with matching state are merged together into a single | 293 | * Any extents with matching state are merged together into a single |
@@ -303,6 +311,7 @@ static int merge_state(struct extent_io_tree *tree, | |||
303 | other = rb_entry(other_node, struct extent_state, rb_node); | 311 | other = rb_entry(other_node, struct extent_state, rb_node); |
304 | if (other->end == state->start - 1 && | 312 | if (other->end == state->start - 1 && |
305 | other->state == state->state) { | 313 | other->state == state->state) { |
314 | merge_cb(tree, state, other); | ||
306 | state->start = other->start; | 315 | state->start = other->start; |
307 | other->tree = NULL; | 316 | other->tree = NULL; |
308 | rb_erase(&other->rb_node, &tree->state); | 317 | rb_erase(&other->rb_node, &tree->state); |
@@ -314,33 +323,37 @@ static int merge_state(struct extent_io_tree *tree, | |||
314 | other = rb_entry(other_node, struct extent_state, rb_node); | 323 | other = rb_entry(other_node, struct extent_state, rb_node); |
315 | if (other->start == state->end + 1 && | 324 | if (other->start == state->end + 1 && |
316 | other->state == state->state) { | 325 | other->state == state->state) { |
326 | merge_cb(tree, state, other); | ||
317 | other->start = state->start; | 327 | other->start = state->start; |
318 | state->tree = NULL; | 328 | state->tree = NULL; |
319 | rb_erase(&state->rb_node, &tree->state); | 329 | rb_erase(&state->rb_node, &tree->state); |
320 | free_extent_state(state); | 330 | free_extent_state(state); |
331 | state = NULL; | ||
321 | } | 332 | } |
322 | } | 333 | } |
334 | |||
323 | return 0; | 335 | return 0; |
324 | } | 336 | } |
325 | 337 | ||
326 | static void set_state_cb(struct extent_io_tree *tree, | 338 | static int set_state_cb(struct extent_io_tree *tree, |
327 | struct extent_state *state, | 339 | struct extent_state *state, |
328 | unsigned long bits) | 340 | unsigned long bits) |
329 | { | 341 | { |
330 | if (tree->ops && tree->ops->set_bit_hook) { | 342 | if (tree->ops && tree->ops->set_bit_hook) { |
331 | tree->ops->set_bit_hook(tree->mapping->host, state->start, | 343 | return tree->ops->set_bit_hook(tree->mapping->host, |
332 | state->end, state->state, bits); | 344 | state->start, state->end, |
345 | state->state, bits); | ||
333 | } | 346 | } |
347 | |||
348 | return 0; | ||
334 | } | 349 | } |
335 | 350 | ||
336 | static void clear_state_cb(struct extent_io_tree *tree, | 351 | static void clear_state_cb(struct extent_io_tree *tree, |
337 | struct extent_state *state, | 352 | struct extent_state *state, |
338 | unsigned long bits) | 353 | unsigned long bits) |
339 | { | 354 | { |
340 | if (tree->ops && tree->ops->clear_bit_hook) { | 355 | if (tree->ops && tree->ops->clear_bit_hook) |
341 | tree->ops->clear_bit_hook(tree->mapping->host, state->start, | 356 | tree->ops->clear_bit_hook(tree->mapping->host, state, bits); |
342 | state->end, state->state, bits); | ||
343 | } | ||
344 | } | 357 | } |
345 | 358 | ||
346 | /* | 359 | /* |
@@ -358,6 +371,7 @@ static int insert_state(struct extent_io_tree *tree, | |||
358 | int bits) | 371 | int bits) |
359 | { | 372 | { |
360 | struct rb_node *node; | 373 | struct rb_node *node; |
374 | int ret; | ||
361 | 375 | ||
362 | if (end < start) { | 376 | if (end < start) { |
363 | printk(KERN_ERR "btrfs end < start %llu %llu\n", | 377 | printk(KERN_ERR "btrfs end < start %llu %llu\n", |
@@ -365,12 +379,15 @@ static int insert_state(struct extent_io_tree *tree, | |||
365 | (unsigned long long)start); | 379 | (unsigned long long)start); |
366 | WARN_ON(1); | 380 | WARN_ON(1); |
367 | } | 381 | } |
382 | state->start = start; | ||
383 | state->end = end; | ||
384 | ret = set_state_cb(tree, state, bits); | ||
385 | if (ret) | ||
386 | return ret; | ||
387 | |||
368 | if (bits & EXTENT_DIRTY) | 388 | if (bits & EXTENT_DIRTY) |
369 | tree->dirty_bytes += end - start + 1; | 389 | tree->dirty_bytes += end - start + 1; |
370 | set_state_cb(tree, state, bits); | ||
371 | state->state |= bits; | 390 | state->state |= bits; |
372 | state->start = start; | ||
373 | state->end = end; | ||
374 | node = tree_insert(&tree->state, end, &state->rb_node); | 391 | node = tree_insert(&tree->state, end, &state->rb_node); |
375 | if (node) { | 392 | if (node) { |
376 | struct extent_state *found; | 393 | struct extent_state *found; |
@@ -387,6 +404,15 @@ static int insert_state(struct extent_io_tree *tree, | |||
387 | return 0; | 404 | return 0; |
388 | } | 405 | } |
389 | 406 | ||
407 | static int split_cb(struct extent_io_tree *tree, struct extent_state *orig, | ||
408 | u64 split) | ||
409 | { | ||
410 | if (tree->ops && tree->ops->split_extent_hook) | ||
411 | return tree->ops->split_extent_hook(tree->mapping->host, | ||
412 | orig, split); | ||
413 | return 0; | ||
414 | } | ||
415 | |||
390 | /* | 416 | /* |
391 | * split a given extent state struct in two, inserting the preallocated | 417 | * split a given extent state struct in two, inserting the preallocated |
392 | * struct 'prealloc' as the newly created second half. 'split' indicates an | 418 | * struct 'prealloc' as the newly created second half. 'split' indicates an |
@@ -405,6 +431,9 @@ static int split_state(struct extent_io_tree *tree, struct extent_state *orig, | |||
405 | struct extent_state *prealloc, u64 split) | 431 | struct extent_state *prealloc, u64 split) |
406 | { | 432 | { |
407 | struct rb_node *node; | 433 | struct rb_node *node; |
434 | |||
435 | split_cb(tree, orig, split); | ||
436 | |||
408 | prealloc->start = orig->start; | 437 | prealloc->start = orig->start; |
409 | prealloc->end = split - 1; | 438 | prealloc->end = split - 1; |
410 | prealloc->state = orig->state; | 439 | prealloc->state = orig->state; |
@@ -471,10 +500,14 @@ static int clear_state_bit(struct extent_io_tree *tree, | |||
471 | * bits were already set, or zero if none of the bits were already set. | 500 | * bits were already set, or zero if none of the bits were already set. |
472 | */ | 501 | */ |
473 | int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, | 502 | int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, |
474 | int bits, int wake, int delete, gfp_t mask) | 503 | int bits, int wake, int delete, |
504 | struct extent_state **cached_state, | ||
505 | gfp_t mask) | ||
475 | { | 506 | { |
476 | struct extent_state *state; | 507 | struct extent_state *state; |
508 | struct extent_state *cached; | ||
477 | struct extent_state *prealloc = NULL; | 509 | struct extent_state *prealloc = NULL; |
510 | struct rb_node *next_node; | ||
478 | struct rb_node *node; | 511 | struct rb_node *node; |
479 | u64 last_end; | 512 | u64 last_end; |
480 | int err; | 513 | int err; |
@@ -488,6 +521,17 @@ again: | |||
488 | } | 521 | } |
489 | 522 | ||
490 | spin_lock(&tree->lock); | 523 | spin_lock(&tree->lock); |
524 | if (cached_state) { | ||
525 | cached = *cached_state; | ||
526 | *cached_state = NULL; | ||
527 | cached_state = NULL; | ||
528 | if (cached && cached->tree && cached->start == start) { | ||
529 | atomic_dec(&cached->refs); | ||
530 | state = cached; | ||
531 | goto hit_next; | ||
532 | } | ||
533 | free_extent_state(cached); | ||
534 | } | ||
491 | /* | 535 | /* |
492 | * this search will find the extents that end after | 536 | * this search will find the extents that end after |
493 | * our range starts | 537 | * our range starts |
@@ -496,6 +540,7 @@ again: | |||
496 | if (!node) | 540 | if (!node) |
497 | goto out; | 541 | goto out; |
498 | state = rb_entry(node, struct extent_state, rb_node); | 542 | state = rb_entry(node, struct extent_state, rb_node); |
543 | hit_next: | ||
499 | if (state->start > end) | 544 | if (state->start > end) |
500 | goto out; | 545 | goto out; |
501 | WARN_ON(state->end < start); | 546 | WARN_ON(state->end < start); |
@@ -526,13 +571,11 @@ again: | |||
526 | if (err) | 571 | if (err) |
527 | goto out; | 572 | goto out; |
528 | if (state->end <= end) { | 573 | if (state->end <= end) { |
529 | set |= clear_state_bit(tree, state, bits, | 574 | set |= clear_state_bit(tree, state, bits, wake, |
530 | wake, delete); | 575 | delete); |
531 | if (last_end == (u64)-1) | 576 | if (last_end == (u64)-1) |
532 | goto out; | 577 | goto out; |
533 | start = last_end + 1; | 578 | start = last_end + 1; |
534 | } else { | ||
535 | start = state->start; | ||
536 | } | 579 | } |
537 | goto search_again; | 580 | goto search_again; |
538 | } | 581 | } |
@@ -547,19 +590,30 @@ again: | |||
547 | prealloc = alloc_extent_state(GFP_ATOMIC); | 590 | prealloc = alloc_extent_state(GFP_ATOMIC); |
548 | err = split_state(tree, state, prealloc, end + 1); | 591 | err = split_state(tree, state, prealloc, end + 1); |
549 | BUG_ON(err == -EEXIST); | 592 | BUG_ON(err == -EEXIST); |
550 | |||
551 | if (wake) | 593 | if (wake) |
552 | wake_up(&state->wq); | 594 | wake_up(&state->wq); |
553 | set |= clear_state_bit(tree, prealloc, bits, | 595 | |
554 | wake, delete); | 596 | set |= clear_state_bit(tree, prealloc, bits, wake, delete); |
597 | |||
555 | prealloc = NULL; | 598 | prealloc = NULL; |
556 | goto out; | 599 | goto out; |
557 | } | 600 | } |
558 | 601 | ||
602 | if (state->end < end && prealloc && !need_resched()) | ||
603 | next_node = rb_next(&state->rb_node); | ||
604 | else | ||
605 | next_node = NULL; | ||
606 | |||
559 | set |= clear_state_bit(tree, state, bits, wake, delete); | 607 | set |= clear_state_bit(tree, state, bits, wake, delete); |
560 | if (last_end == (u64)-1) | 608 | if (last_end == (u64)-1) |
561 | goto out; | 609 | goto out; |
562 | start = last_end + 1; | 610 | start = last_end + 1; |
611 | if (start <= end && next_node) { | ||
612 | state = rb_entry(next_node, struct extent_state, | ||
613 | rb_node); | ||
614 | if (state->start == start) | ||
615 | goto hit_next; | ||
616 | } | ||
563 | goto search_again; | 617 | goto search_again; |
564 | 618 | ||
565 | out: | 619 | out: |
@@ -641,40 +695,59 @@ out: | |||
641 | return 0; | 695 | return 0; |
642 | } | 696 | } |
643 | 697 | ||
644 | static void set_state_bits(struct extent_io_tree *tree, | 698 | static int set_state_bits(struct extent_io_tree *tree, |
645 | struct extent_state *state, | 699 | struct extent_state *state, |
646 | int bits) | 700 | int bits) |
647 | { | 701 | { |
702 | int ret; | ||
703 | |||
704 | ret = set_state_cb(tree, state, bits); | ||
705 | if (ret) | ||
706 | return ret; | ||
707 | |||
648 | if ((bits & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) { | 708 | if ((bits & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) { |
649 | u64 range = state->end - state->start + 1; | 709 | u64 range = state->end - state->start + 1; |
650 | tree->dirty_bytes += range; | 710 | tree->dirty_bytes += range; |
651 | } | 711 | } |
652 | set_state_cb(tree, state, bits); | ||
653 | state->state |= bits; | 712 | state->state |= bits; |
713 | |||
714 | return 0; | ||
715 | } | ||
716 | |||
717 | static void cache_state(struct extent_state *state, | ||
718 | struct extent_state **cached_ptr) | ||
719 | { | ||
720 | if (cached_ptr && !(*cached_ptr)) { | ||
721 | if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY)) { | ||
722 | *cached_ptr = state; | ||
723 | atomic_inc(&state->refs); | ||
724 | } | ||
725 | } | ||
654 | } | 726 | } |
655 | 727 | ||
656 | /* | 728 | /* |
657 | * set some bits on a range in the tree. This may require allocations | 729 | * set some bits on a range in the tree. This may require allocations or |
658 | * or sleeping, so the gfp mask is used to indicate what is allowed. | 730 | * sleeping, so the gfp mask is used to indicate what is allowed. |
659 | * | 731 | * |
660 | * If 'exclusive' == 1, this will fail with -EEXIST if some part of the | 732 | * If any of the exclusive bits are set, this will fail with -EEXIST if some |
661 | * range already has the desired bits set. The start of the existing | 733 | * part of the range already has the desired bits set. The start of the |
662 | * range is returned in failed_start in this case. | 734 | * existing range is returned in failed_start in this case. |
663 | * | 735 | * |
664 | * [start, end] is inclusive | 736 | * [start, end] is inclusive This takes the tree lock. |
665 | * This takes the tree lock. | ||
666 | */ | 737 | */ |
738 | |||
667 | static int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, | 739 | static int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, |
668 | int bits, int exclusive, u64 *failed_start, | 740 | int bits, int exclusive_bits, u64 *failed_start, |
741 | struct extent_state **cached_state, | ||
669 | gfp_t mask) | 742 | gfp_t mask) |
670 | { | 743 | { |
671 | struct extent_state *state; | 744 | struct extent_state *state; |
672 | struct extent_state *prealloc = NULL; | 745 | struct extent_state *prealloc = NULL; |
673 | struct rb_node *node; | 746 | struct rb_node *node; |
674 | int err = 0; | 747 | int err = 0; |
675 | int set; | ||
676 | u64 last_start; | 748 | u64 last_start; |
677 | u64 last_end; | 749 | u64 last_end; |
750 | |||
678 | again: | 751 | again: |
679 | if (!prealloc && (mask & __GFP_WAIT)) { | 752 | if (!prealloc && (mask & __GFP_WAIT)) { |
680 | prealloc = alloc_extent_state(mask); | 753 | prealloc = alloc_extent_state(mask); |
@@ -683,6 +756,13 @@ again: | |||
683 | } | 756 | } |
684 | 757 | ||
685 | spin_lock(&tree->lock); | 758 | spin_lock(&tree->lock); |
759 | if (cached_state && *cached_state) { | ||
760 | state = *cached_state; | ||
761 | if (state->start == start && state->tree) { | ||
762 | node = &state->rb_node; | ||
763 | goto hit_next; | ||
764 | } | ||
765 | } | ||
686 | /* | 766 | /* |
687 | * this search will find all the extents that end after | 767 | * this search will find all the extents that end after |
688 | * our range starts. | 768 | * our range starts. |
@@ -694,8 +774,8 @@ again: | |||
694 | BUG_ON(err == -EEXIST); | 774 | BUG_ON(err == -EEXIST); |
695 | goto out; | 775 | goto out; |
696 | } | 776 | } |
697 | |||
698 | state = rb_entry(node, struct extent_state, rb_node); | 777 | state = rb_entry(node, struct extent_state, rb_node); |
778 | hit_next: | ||
699 | last_start = state->start; | 779 | last_start = state->start; |
700 | last_end = state->end; | 780 | last_end = state->end; |
701 | 781 | ||
@@ -706,17 +786,32 @@ again: | |||
706 | * Just lock what we found and keep going | 786 | * Just lock what we found and keep going |
707 | */ | 787 | */ |
708 | if (state->start == start && state->end <= end) { | 788 | if (state->start == start && state->end <= end) { |
709 | set = state->state & bits; | 789 | struct rb_node *next_node; |
710 | if (set && exclusive) { | 790 | if (state->state & exclusive_bits) { |
711 | *failed_start = state->start; | 791 | *failed_start = state->start; |
712 | err = -EEXIST; | 792 | err = -EEXIST; |
713 | goto out; | 793 | goto out; |
714 | } | 794 | } |
715 | set_state_bits(tree, state, bits); | 795 | |
796 | err = set_state_bits(tree, state, bits); | ||
797 | if (err) | ||
798 | goto out; | ||
799 | |||
800 | cache_state(state, cached_state); | ||
716 | merge_state(tree, state); | 801 | merge_state(tree, state); |
717 | if (last_end == (u64)-1) | 802 | if (last_end == (u64)-1) |
718 | goto out; | 803 | goto out; |
804 | |||
719 | start = last_end + 1; | 805 | start = last_end + 1; |
806 | if (start < end && prealloc && !need_resched()) { | ||
807 | next_node = rb_next(node); | ||
808 | if (next_node) { | ||
809 | state = rb_entry(next_node, struct extent_state, | ||
810 | rb_node); | ||
811 | if (state->start == start) | ||
812 | goto hit_next; | ||
813 | } | ||
814 | } | ||
720 | goto search_again; | 815 | goto search_again; |
721 | } | 816 | } |
722 | 817 | ||
@@ -737,8 +832,7 @@ again: | |||
737 | * desired bit on it. | 832 | * desired bit on it. |
738 | */ | 833 | */ |
739 | if (state->start < start) { | 834 | if (state->start < start) { |
740 | set = state->state & bits; | 835 | if (state->state & exclusive_bits) { |
741 | if (exclusive && set) { | ||
742 | *failed_start = start; | 836 | *failed_start = start; |
743 | err = -EEXIST; | 837 | err = -EEXIST; |
744 | goto out; | 838 | goto out; |
@@ -749,13 +843,14 @@ again: | |||
749 | if (err) | 843 | if (err) |
750 | goto out; | 844 | goto out; |
751 | if (state->end <= end) { | 845 | if (state->end <= end) { |
752 | set_state_bits(tree, state, bits); | 846 | err = set_state_bits(tree, state, bits); |
847 | if (err) | ||
848 | goto out; | ||
849 | cache_state(state, cached_state); | ||
753 | merge_state(tree, state); | 850 | merge_state(tree, state); |
754 | if (last_end == (u64)-1) | 851 | if (last_end == (u64)-1) |
755 | goto out; | 852 | goto out; |
756 | start = last_end + 1; | 853 | start = last_end + 1; |
757 | } else { | ||
758 | start = state->start; | ||
759 | } | 854 | } |
760 | goto search_again; | 855 | goto search_again; |
761 | } | 856 | } |
@@ -774,10 +869,13 @@ again: | |||
774 | this_end = last_start - 1; | 869 | this_end = last_start - 1; |
775 | err = insert_state(tree, prealloc, start, this_end, | 870 | err = insert_state(tree, prealloc, start, this_end, |
776 | bits); | 871 | bits); |
777 | prealloc = NULL; | ||
778 | BUG_ON(err == -EEXIST); | 872 | BUG_ON(err == -EEXIST); |
779 | if (err) | 873 | if (err) { |
874 | prealloc = NULL; | ||
780 | goto out; | 875 | goto out; |
876 | } | ||
877 | cache_state(prealloc, cached_state); | ||
878 | prealloc = NULL; | ||
781 | start = this_end + 1; | 879 | start = this_end + 1; |
782 | goto search_again; | 880 | goto search_again; |
783 | } | 881 | } |
@@ -788,8 +886,7 @@ again: | |||
788 | * on the first half | 886 | * on the first half |
789 | */ | 887 | */ |
790 | if (state->start <= end && state->end > end) { | 888 | if (state->start <= end && state->end > end) { |
791 | set = state->state & bits; | 889 | if (state->state & exclusive_bits) { |
792 | if (exclusive && set) { | ||
793 | *failed_start = start; | 890 | *failed_start = start; |
794 | err = -EEXIST; | 891 | err = -EEXIST; |
795 | goto out; | 892 | goto out; |
@@ -797,7 +894,12 @@ again: | |||
797 | err = split_state(tree, state, prealloc, end + 1); | 894 | err = split_state(tree, state, prealloc, end + 1); |
798 | BUG_ON(err == -EEXIST); | 895 | BUG_ON(err == -EEXIST); |
799 | 896 | ||
800 | set_state_bits(tree, prealloc, bits); | 897 | err = set_state_bits(tree, prealloc, bits); |
898 | if (err) { | ||
899 | prealloc = NULL; | ||
900 | goto out; | ||
901 | } | ||
902 | cache_state(prealloc, cached_state); | ||
801 | merge_state(tree, prealloc); | 903 | merge_state(tree, prealloc); |
802 | prealloc = NULL; | 904 | prealloc = NULL; |
803 | goto out; | 905 | goto out; |
@@ -826,86 +928,64 @@ int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end, | |||
826 | gfp_t mask) | 928 | gfp_t mask) |
827 | { | 929 | { |
828 | return set_extent_bit(tree, start, end, EXTENT_DIRTY, 0, NULL, | 930 | return set_extent_bit(tree, start, end, EXTENT_DIRTY, 0, NULL, |
829 | mask); | 931 | NULL, mask); |
830 | } | ||
831 | |||
832 | int set_extent_ordered(struct extent_io_tree *tree, u64 start, u64 end, | ||
833 | gfp_t mask) | ||
834 | { | ||
835 | return set_extent_bit(tree, start, end, EXTENT_ORDERED, 0, NULL, mask); | ||
836 | } | 932 | } |
837 | 933 | ||
838 | int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end, | 934 | int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end, |
839 | int bits, gfp_t mask) | 935 | int bits, gfp_t mask) |
840 | { | 936 | { |
841 | return set_extent_bit(tree, start, end, bits, 0, NULL, | 937 | return set_extent_bit(tree, start, end, bits, 0, NULL, |
842 | mask); | 938 | NULL, mask); |
843 | } | 939 | } |
844 | 940 | ||
845 | int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end, | 941 | int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end, |
846 | int bits, gfp_t mask) | 942 | int bits, gfp_t mask) |
847 | { | 943 | { |
848 | return clear_extent_bit(tree, start, end, bits, 0, 0, mask); | 944 | return clear_extent_bit(tree, start, end, bits, 0, 0, NULL, mask); |
849 | } | 945 | } |
850 | 946 | ||
851 | int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end, | 947 | int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end, |
852 | gfp_t mask) | 948 | gfp_t mask) |
853 | { | 949 | { |
854 | return set_extent_bit(tree, start, end, | 950 | return set_extent_bit(tree, start, end, |
855 | EXTENT_DELALLOC | EXTENT_DIRTY, | 951 | EXTENT_DELALLOC | EXTENT_DIRTY | EXTENT_UPTODATE, |
856 | 0, NULL, mask); | 952 | 0, NULL, NULL, mask); |
857 | } | 953 | } |
858 | 954 | ||
859 | int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end, | 955 | int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end, |
860 | gfp_t mask) | 956 | gfp_t mask) |
861 | { | 957 | { |
862 | return clear_extent_bit(tree, start, end, | 958 | return clear_extent_bit(tree, start, end, |
863 | EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0, mask); | 959 | EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0, |
864 | } | 960 | NULL, mask); |
865 | |||
866 | int clear_extent_ordered(struct extent_io_tree *tree, u64 start, u64 end, | ||
867 | gfp_t mask) | ||
868 | { | ||
869 | return clear_extent_bit(tree, start, end, EXTENT_ORDERED, 1, 0, mask); | ||
870 | } | 961 | } |
871 | 962 | ||
872 | int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end, | 963 | int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end, |
873 | gfp_t mask) | 964 | gfp_t mask) |
874 | { | 965 | { |
875 | return set_extent_bit(tree, start, end, EXTENT_NEW, 0, NULL, | 966 | return set_extent_bit(tree, start, end, EXTENT_NEW, 0, NULL, |
876 | mask); | 967 | NULL, mask); |
877 | } | 968 | } |
878 | 969 | ||
879 | static int clear_extent_new(struct extent_io_tree *tree, u64 start, u64 end, | 970 | static int clear_extent_new(struct extent_io_tree *tree, u64 start, u64 end, |
880 | gfp_t mask) | 971 | gfp_t mask) |
881 | { | 972 | { |
882 | return clear_extent_bit(tree, start, end, EXTENT_NEW, 0, 0, mask); | 973 | return clear_extent_bit(tree, start, end, EXTENT_NEW, 0, 0, |
974 | NULL, mask); | ||
883 | } | 975 | } |
884 | 976 | ||
885 | int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end, | 977 | int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end, |
886 | gfp_t mask) | 978 | gfp_t mask) |
887 | { | 979 | { |
888 | return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, NULL, | 980 | return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, NULL, |
889 | mask); | 981 | NULL, mask); |
890 | } | 982 | } |
891 | 983 | ||
892 | static int clear_extent_uptodate(struct extent_io_tree *tree, u64 start, | 984 | static int clear_extent_uptodate(struct extent_io_tree *tree, u64 start, |
893 | u64 end, gfp_t mask) | 985 | u64 end, gfp_t mask) |
894 | { | 986 | { |
895 | return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0, mask); | 987 | return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0, |
896 | } | 988 | NULL, mask); |
897 | |||
898 | static int set_extent_writeback(struct extent_io_tree *tree, u64 start, u64 end, | ||
899 | gfp_t mask) | ||
900 | { | ||
901 | return set_extent_bit(tree, start, end, EXTENT_WRITEBACK, | ||
902 | 0, NULL, mask); | ||
903 | } | ||
904 | |||
905 | static int clear_extent_writeback(struct extent_io_tree *tree, u64 start, | ||
906 | u64 end, gfp_t mask) | ||
907 | { | ||
908 | return clear_extent_bit(tree, start, end, EXTENT_WRITEBACK, 1, 0, mask); | ||
909 | } | 989 | } |
910 | 990 | ||
911 | int wait_on_extent_writeback(struct extent_io_tree *tree, u64 start, u64 end) | 991 | int wait_on_extent_writeback(struct extent_io_tree *tree, u64 start, u64 end) |
@@ -917,13 +997,15 @@ int wait_on_extent_writeback(struct extent_io_tree *tree, u64 start, u64 end) | |||
917 | * either insert or lock state struct between start and end use mask to tell | 997 | * either insert or lock state struct between start and end use mask to tell |
918 | * us if waiting is desired. | 998 | * us if waiting is desired. |
919 | */ | 999 | */ |
920 | int lock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask) | 1000 | int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end, |
1001 | int bits, struct extent_state **cached_state, gfp_t mask) | ||
921 | { | 1002 | { |
922 | int err; | 1003 | int err; |
923 | u64 failed_start; | 1004 | u64 failed_start; |
924 | while (1) { | 1005 | while (1) { |
925 | err = set_extent_bit(tree, start, end, EXTENT_LOCKED, 1, | 1006 | err = set_extent_bit(tree, start, end, EXTENT_LOCKED | bits, |
926 | &failed_start, mask); | 1007 | EXTENT_LOCKED, &failed_start, |
1008 | cached_state, mask); | ||
927 | if (err == -EEXIST && (mask & __GFP_WAIT)) { | 1009 | if (err == -EEXIST && (mask & __GFP_WAIT)) { |
928 | wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED); | 1010 | wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED); |
929 | start = failed_start; | 1011 | start = failed_start; |
@@ -935,27 +1017,40 @@ int lock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask) | |||
935 | return err; | 1017 | return err; |
936 | } | 1018 | } |
937 | 1019 | ||
1020 | int lock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask) | ||
1021 | { | ||
1022 | return lock_extent_bits(tree, start, end, 0, NULL, mask); | ||
1023 | } | ||
1024 | |||
938 | int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end, | 1025 | int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end, |
939 | gfp_t mask) | 1026 | gfp_t mask) |
940 | { | 1027 | { |
941 | int err; | 1028 | int err; |
942 | u64 failed_start; | 1029 | u64 failed_start; |
943 | 1030 | ||
944 | err = set_extent_bit(tree, start, end, EXTENT_LOCKED, 1, | 1031 | err = set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED, |
945 | &failed_start, mask); | 1032 | &failed_start, NULL, mask); |
946 | if (err == -EEXIST) { | 1033 | if (err == -EEXIST) { |
947 | if (failed_start > start) | 1034 | if (failed_start > start) |
948 | clear_extent_bit(tree, start, failed_start - 1, | 1035 | clear_extent_bit(tree, start, failed_start - 1, |
949 | EXTENT_LOCKED, 1, 0, mask); | 1036 | EXTENT_LOCKED, 1, 0, NULL, mask); |
950 | return 0; | 1037 | return 0; |
951 | } | 1038 | } |
952 | return 1; | 1039 | return 1; |
953 | } | 1040 | } |
954 | 1041 | ||
1042 | int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end, | ||
1043 | struct extent_state **cached, gfp_t mask) | ||
1044 | { | ||
1045 | return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached, | ||
1046 | mask); | ||
1047 | } | ||
1048 | |||
955 | int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end, | 1049 | int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end, |
956 | gfp_t mask) | 1050 | gfp_t mask) |
957 | { | 1051 | { |
958 | return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, mask); | 1052 | return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL, |
1053 | mask); | ||
959 | } | 1054 | } |
960 | 1055 | ||
961 | /* | 1056 | /* |
@@ -974,7 +1069,6 @@ int set_range_dirty(struct extent_io_tree *tree, u64 start, u64 end) | |||
974 | page_cache_release(page); | 1069 | page_cache_release(page); |
975 | index++; | 1070 | index++; |
976 | } | 1071 | } |
977 | set_extent_dirty(tree, start, end, GFP_NOFS); | ||
978 | return 0; | 1072 | return 0; |
979 | } | 1073 | } |
980 | 1074 | ||
@@ -994,7 +1088,6 @@ static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end) | |||
994 | page_cache_release(page); | 1088 | page_cache_release(page); |
995 | index++; | 1089 | index++; |
996 | } | 1090 | } |
997 | set_extent_writeback(tree, start, end, GFP_NOFS); | ||
998 | return 0; | 1091 | return 0; |
999 | } | 1092 | } |
1000 | 1093 | ||
@@ -1232,6 +1325,7 @@ static noinline u64 find_lock_delalloc_range(struct inode *inode, | |||
1232 | u64 delalloc_start; | 1325 | u64 delalloc_start; |
1233 | u64 delalloc_end; | 1326 | u64 delalloc_end; |
1234 | u64 found; | 1327 | u64 found; |
1328 | struct extent_state *cached_state = NULL; | ||
1235 | int ret; | 1329 | int ret; |
1236 | int loops = 0; | 1330 | int loops = 0; |
1237 | 1331 | ||
@@ -1269,6 +1363,7 @@ again: | |||
1269 | /* some of the pages are gone, lets avoid looping by | 1363 | /* some of the pages are gone, lets avoid looping by |
1270 | * shortening the size of the delalloc range we're searching | 1364 | * shortening the size of the delalloc range we're searching |
1271 | */ | 1365 | */ |
1366 | free_extent_state(cached_state); | ||
1272 | if (!loops) { | 1367 | if (!loops) { |
1273 | unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1); | 1368 | unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1); |
1274 | max_bytes = PAGE_CACHE_SIZE - offset; | 1369 | max_bytes = PAGE_CACHE_SIZE - offset; |
@@ -1282,18 +1377,21 @@ again: | |||
1282 | BUG_ON(ret); | 1377 | BUG_ON(ret); |
1283 | 1378 | ||
1284 | /* step three, lock the state bits for the whole range */ | 1379 | /* step three, lock the state bits for the whole range */ |
1285 | lock_extent(tree, delalloc_start, delalloc_end, GFP_NOFS); | 1380 | lock_extent_bits(tree, delalloc_start, delalloc_end, |
1381 | 0, &cached_state, GFP_NOFS); | ||
1286 | 1382 | ||
1287 | /* then test to make sure it is all still delalloc */ | 1383 | /* then test to make sure it is all still delalloc */ |
1288 | ret = test_range_bit(tree, delalloc_start, delalloc_end, | 1384 | ret = test_range_bit(tree, delalloc_start, delalloc_end, |
1289 | EXTENT_DELALLOC, 1); | 1385 | EXTENT_DELALLOC, 1, cached_state); |
1290 | if (!ret) { | 1386 | if (!ret) { |
1291 | unlock_extent(tree, delalloc_start, delalloc_end, GFP_NOFS); | 1387 | unlock_extent_cached(tree, delalloc_start, delalloc_end, |
1388 | &cached_state, GFP_NOFS); | ||
1292 | __unlock_for_delalloc(inode, locked_page, | 1389 | __unlock_for_delalloc(inode, locked_page, |
1293 | delalloc_start, delalloc_end); | 1390 | delalloc_start, delalloc_end); |
1294 | cond_resched(); | 1391 | cond_resched(); |
1295 | goto again; | 1392 | goto again; |
1296 | } | 1393 | } |
1394 | free_extent_state(cached_state); | ||
1297 | *start = delalloc_start; | 1395 | *start = delalloc_start; |
1298 | *end = delalloc_end; | 1396 | *end = delalloc_end; |
1299 | out_failed: | 1397 | out_failed: |
@@ -1307,7 +1405,8 @@ int extent_clear_unlock_delalloc(struct inode *inode, | |||
1307 | int clear_unlock, | 1405 | int clear_unlock, |
1308 | int clear_delalloc, int clear_dirty, | 1406 | int clear_delalloc, int clear_dirty, |
1309 | int set_writeback, | 1407 | int set_writeback, |
1310 | int end_writeback) | 1408 | int end_writeback, |
1409 | int set_private2) | ||
1311 | { | 1410 | { |
1312 | int ret; | 1411 | int ret; |
1313 | struct page *pages[16]; | 1412 | struct page *pages[16]; |
@@ -1325,8 +1424,9 @@ int extent_clear_unlock_delalloc(struct inode *inode, | |||
1325 | if (clear_delalloc) | 1424 | if (clear_delalloc) |
1326 | clear_bits |= EXTENT_DELALLOC; | 1425 | clear_bits |= EXTENT_DELALLOC; |
1327 | 1426 | ||
1328 | clear_extent_bit(tree, start, end, clear_bits, 1, 0, GFP_NOFS); | 1427 | clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS); |
1329 | if (!(unlock_pages || clear_dirty || set_writeback || end_writeback)) | 1428 | if (!(unlock_pages || clear_dirty || set_writeback || end_writeback || |
1429 | set_private2)) | ||
1330 | return 0; | 1430 | return 0; |
1331 | 1431 | ||
1332 | while (nr_pages > 0) { | 1432 | while (nr_pages > 0) { |
@@ -1334,6 +1434,10 @@ int extent_clear_unlock_delalloc(struct inode *inode, | |||
1334 | min_t(unsigned long, | 1434 | min_t(unsigned long, |
1335 | nr_pages, ARRAY_SIZE(pages)), pages); | 1435 | nr_pages, ARRAY_SIZE(pages)), pages); |
1336 | for (i = 0; i < ret; i++) { | 1436 | for (i = 0; i < ret; i++) { |
1437 | |||
1438 | if (set_private2) | ||
1439 | SetPagePrivate2(pages[i]); | ||
1440 | |||
1337 | if (pages[i] == locked_page) { | 1441 | if (pages[i] == locked_page) { |
1338 | page_cache_release(pages[i]); | 1442 | page_cache_release(pages[i]); |
1339 | continue; | 1443 | continue; |
@@ -1476,14 +1580,17 @@ out: | |||
1476 | * range is found set. | 1580 | * range is found set. |
1477 | */ | 1581 | */ |
1478 | int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end, | 1582 | int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end, |
1479 | int bits, int filled) | 1583 | int bits, int filled, struct extent_state *cached) |
1480 | { | 1584 | { |
1481 | struct extent_state *state = NULL; | 1585 | struct extent_state *state = NULL; |
1482 | struct rb_node *node; | 1586 | struct rb_node *node; |
1483 | int bitset = 0; | 1587 | int bitset = 0; |
1484 | 1588 | ||
1485 | spin_lock(&tree->lock); | 1589 | spin_lock(&tree->lock); |
1486 | node = tree_search(tree, start); | 1590 | if (cached && cached->tree && cached->start == start) |
1591 | node = &cached->rb_node; | ||
1592 | else | ||
1593 | node = tree_search(tree, start); | ||
1487 | while (node && start <= end) { | 1594 | while (node && start <= end) { |
1488 | state = rb_entry(node, struct extent_state, rb_node); | 1595 | state = rb_entry(node, struct extent_state, rb_node); |
1489 | 1596 | ||
@@ -1503,6 +1610,10 @@ int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end, | |||
1503 | bitset = 0; | 1610 | bitset = 0; |
1504 | break; | 1611 | break; |
1505 | } | 1612 | } |
1613 | |||
1614 | if (state->end == (u64)-1) | ||
1615 | break; | ||
1616 | |||
1506 | start = state->end + 1; | 1617 | start = state->end + 1; |
1507 | if (start > end) | 1618 | if (start > end) |
1508 | break; | 1619 | break; |
@@ -1526,7 +1637,7 @@ static int check_page_uptodate(struct extent_io_tree *tree, | |||
1526 | { | 1637 | { |
1527 | u64 start = (u64)page->index << PAGE_CACHE_SHIFT; | 1638 | u64 start = (u64)page->index << PAGE_CACHE_SHIFT; |
1528 | u64 end = start + PAGE_CACHE_SIZE - 1; | 1639 | u64 end = start + PAGE_CACHE_SIZE - 1; |
1529 | if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1)) | 1640 | if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL)) |
1530 | SetPageUptodate(page); | 1641 | SetPageUptodate(page); |
1531 | return 0; | 1642 | return 0; |
1532 | } | 1643 | } |
@@ -1540,7 +1651,7 @@ static int check_page_locked(struct extent_io_tree *tree, | |||
1540 | { | 1651 | { |
1541 | u64 start = (u64)page->index << PAGE_CACHE_SHIFT; | 1652 | u64 start = (u64)page->index << PAGE_CACHE_SHIFT; |
1542 | u64 end = start + PAGE_CACHE_SIZE - 1; | 1653 | u64 end = start + PAGE_CACHE_SIZE - 1; |
1543 | if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0)) | 1654 | if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL)) |
1544 | unlock_page(page); | 1655 | unlock_page(page); |
1545 | return 0; | 1656 | return 0; |
1546 | } | 1657 | } |
@@ -1552,10 +1663,7 @@ static int check_page_locked(struct extent_io_tree *tree, | |||
1552 | static int check_page_writeback(struct extent_io_tree *tree, | 1663 | static int check_page_writeback(struct extent_io_tree *tree, |
1553 | struct page *page) | 1664 | struct page *page) |
1554 | { | 1665 | { |
1555 | u64 start = (u64)page->index << PAGE_CACHE_SHIFT; | 1666 | end_page_writeback(page); |
1556 | u64 end = start + PAGE_CACHE_SIZE - 1; | ||
1557 | if (!test_range_bit(tree, start, end, EXTENT_WRITEBACK, 0)) | ||
1558 | end_page_writeback(page); | ||
1559 | return 0; | 1667 | return 0; |
1560 | } | 1668 | } |
1561 | 1669 | ||
@@ -1613,13 +1721,11 @@ static void end_bio_extent_writepage(struct bio *bio, int err) | |||
1613 | } | 1721 | } |
1614 | 1722 | ||
1615 | if (!uptodate) { | 1723 | if (!uptodate) { |
1616 | clear_extent_uptodate(tree, start, end, GFP_ATOMIC); | 1724 | clear_extent_uptodate(tree, start, end, GFP_NOFS); |
1617 | ClearPageUptodate(page); | 1725 | ClearPageUptodate(page); |
1618 | SetPageError(page); | 1726 | SetPageError(page); |
1619 | } | 1727 | } |
1620 | 1728 | ||
1621 | clear_extent_writeback(tree, start, end, GFP_ATOMIC); | ||
1622 | |||
1623 | if (whole_page) | 1729 | if (whole_page) |
1624 | end_page_writeback(page); | 1730 | end_page_writeback(page); |
1625 | else | 1731 | else |
@@ -1983,7 +2089,8 @@ static int __extent_read_full_page(struct extent_io_tree *tree, | |||
1983 | continue; | 2089 | continue; |
1984 | } | 2090 | } |
1985 | /* the get_extent function already copied into the page */ | 2091 | /* the get_extent function already copied into the page */ |
1986 | if (test_range_bit(tree, cur, cur_end, EXTENT_UPTODATE, 1)) { | 2092 | if (test_range_bit(tree, cur, cur_end, |
2093 | EXTENT_UPTODATE, 1, NULL)) { | ||
1987 | check_page_uptodate(tree, page); | 2094 | check_page_uptodate(tree, page); |
1988 | unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS); | 2095 | unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS); |
1989 | cur = cur + iosize; | 2096 | cur = cur + iosize; |
@@ -2078,6 +2185,7 @@ static int __extent_writepage(struct page *page, struct writeback_control *wbc, | |||
2078 | u64 iosize; | 2185 | u64 iosize; |
2079 | u64 unlock_start; | 2186 | u64 unlock_start; |
2080 | sector_t sector; | 2187 | sector_t sector; |
2188 | struct extent_state *cached_state = NULL; | ||
2081 | struct extent_map *em; | 2189 | struct extent_map *em; |
2082 | struct block_device *bdev; | 2190 | struct block_device *bdev; |
2083 | int ret; | 2191 | int ret; |
@@ -2124,6 +2232,7 @@ static int __extent_writepage(struct page *page, struct writeback_control *wbc, | |||
2124 | delalloc_end = 0; | 2232 | delalloc_end = 0; |
2125 | page_started = 0; | 2233 | page_started = 0; |
2126 | if (!epd->extent_locked) { | 2234 | if (!epd->extent_locked) { |
2235 | u64 delalloc_to_write = 0; | ||
2127 | /* | 2236 | /* |
2128 | * make sure the wbc mapping index is at least updated | 2237 | * make sure the wbc mapping index is at least updated |
2129 | * to this page. | 2238 | * to this page. |
@@ -2143,8 +2252,24 @@ static int __extent_writepage(struct page *page, struct writeback_control *wbc, | |||
2143 | tree->ops->fill_delalloc(inode, page, delalloc_start, | 2252 | tree->ops->fill_delalloc(inode, page, delalloc_start, |
2144 | delalloc_end, &page_started, | 2253 | delalloc_end, &page_started, |
2145 | &nr_written); | 2254 | &nr_written); |
2255 | /* | ||
2256 | * delalloc_end is already one less than the total | ||
2257 | * length, so we don't subtract one from | ||
2258 | * PAGE_CACHE_SIZE | ||
2259 | */ | ||
2260 | delalloc_to_write += (delalloc_end - delalloc_start + | ||
2261 | PAGE_CACHE_SIZE) >> | ||
2262 | PAGE_CACHE_SHIFT; | ||
2146 | delalloc_start = delalloc_end + 1; | 2263 | delalloc_start = delalloc_end + 1; |
2147 | } | 2264 | } |
2265 | if (wbc->nr_to_write < delalloc_to_write) { | ||
2266 | int thresh = 8192; | ||
2267 | |||
2268 | if (delalloc_to_write < thresh * 2) | ||
2269 | thresh = delalloc_to_write; | ||
2270 | wbc->nr_to_write = min_t(u64, delalloc_to_write, | ||
2271 | thresh); | ||
2272 | } | ||
2148 | 2273 | ||
2149 | /* did the fill delalloc function already unlock and start | 2274 | /* did the fill delalloc function already unlock and start |
2150 | * the IO? | 2275 | * the IO? |
@@ -2160,15 +2285,10 @@ static int __extent_writepage(struct page *page, struct writeback_control *wbc, | |||
2160 | goto done_unlocked; | 2285 | goto done_unlocked; |
2161 | } | 2286 | } |
2162 | } | 2287 | } |
2163 | lock_extent(tree, start, page_end, GFP_NOFS); | ||
2164 | |||
2165 | unlock_start = start; | ||
2166 | |||
2167 | if (tree->ops && tree->ops->writepage_start_hook) { | 2288 | if (tree->ops && tree->ops->writepage_start_hook) { |
2168 | ret = tree->ops->writepage_start_hook(page, start, | 2289 | ret = tree->ops->writepage_start_hook(page, start, |
2169 | page_end); | 2290 | page_end); |
2170 | if (ret == -EAGAIN) { | 2291 | if (ret == -EAGAIN) { |
2171 | unlock_extent(tree, start, page_end, GFP_NOFS); | ||
2172 | redirty_page_for_writepage(wbc, page); | 2292 | redirty_page_for_writepage(wbc, page); |
2173 | update_nr_written(page, wbc, nr_written); | 2293 | update_nr_written(page, wbc, nr_written); |
2174 | unlock_page(page); | 2294 | unlock_page(page); |
@@ -2184,12 +2304,7 @@ static int __extent_writepage(struct page *page, struct writeback_control *wbc, | |||
2184 | update_nr_written(page, wbc, nr_written + 1); | 2304 | update_nr_written(page, wbc, nr_written + 1); |
2185 | 2305 | ||
2186 | end = page_end; | 2306 | end = page_end; |
2187 | if (test_range_bit(tree, start, page_end, EXTENT_DELALLOC, 0)) | ||
2188 | printk(KERN_ERR "btrfs delalloc bits after lock_extent\n"); | ||
2189 | |||
2190 | if (last_byte <= start) { | 2307 | if (last_byte <= start) { |
2191 | clear_extent_dirty(tree, start, page_end, GFP_NOFS); | ||
2192 | unlock_extent(tree, start, page_end, GFP_NOFS); | ||
2193 | if (tree->ops && tree->ops->writepage_end_io_hook) | 2308 | if (tree->ops && tree->ops->writepage_end_io_hook) |
2194 | tree->ops->writepage_end_io_hook(page, start, | 2309 | tree->ops->writepage_end_io_hook(page, start, |
2195 | page_end, NULL, 1); | 2310 | page_end, NULL, 1); |
@@ -2197,13 +2312,10 @@ static int __extent_writepage(struct page *page, struct writeback_control *wbc, | |||
2197 | goto done; | 2312 | goto done; |
2198 | } | 2313 | } |
2199 | 2314 | ||
2200 | set_extent_uptodate(tree, start, page_end, GFP_NOFS); | ||
2201 | blocksize = inode->i_sb->s_blocksize; | 2315 | blocksize = inode->i_sb->s_blocksize; |
2202 | 2316 | ||
2203 | while (cur <= end) { | 2317 | while (cur <= end) { |
2204 | if (cur >= last_byte) { | 2318 | if (cur >= last_byte) { |
2205 | clear_extent_dirty(tree, cur, page_end, GFP_NOFS); | ||
2206 | unlock_extent(tree, unlock_start, page_end, GFP_NOFS); | ||
2207 | if (tree->ops && tree->ops->writepage_end_io_hook) | 2319 | if (tree->ops && tree->ops->writepage_end_io_hook) |
2208 | tree->ops->writepage_end_io_hook(page, cur, | 2320 | tree->ops->writepage_end_io_hook(page, cur, |
2209 | page_end, NULL, 1); | 2321 | page_end, NULL, 1); |
@@ -2235,12 +2347,6 @@ static int __extent_writepage(struct page *page, struct writeback_control *wbc, | |||
2235 | */ | 2347 | */ |
2236 | if (compressed || block_start == EXTENT_MAP_HOLE || | 2348 | if (compressed || block_start == EXTENT_MAP_HOLE || |
2237 | block_start == EXTENT_MAP_INLINE) { | 2349 | block_start == EXTENT_MAP_INLINE) { |
2238 | clear_extent_dirty(tree, cur, | ||
2239 | cur + iosize - 1, GFP_NOFS); | ||
2240 | |||
2241 | unlock_extent(tree, unlock_start, cur + iosize - 1, | ||
2242 | GFP_NOFS); | ||
2243 | |||
2244 | /* | 2350 | /* |
2245 | * end_io notification does not happen here for | 2351 | * end_io notification does not happen here for |
2246 | * compressed extents | 2352 | * compressed extents |
@@ -2265,13 +2371,12 @@ static int __extent_writepage(struct page *page, struct writeback_control *wbc, | |||
2265 | } | 2371 | } |
2266 | /* leave this out until we have a page_mkwrite call */ | 2372 | /* leave this out until we have a page_mkwrite call */ |
2267 | if (0 && !test_range_bit(tree, cur, cur + iosize - 1, | 2373 | if (0 && !test_range_bit(tree, cur, cur + iosize - 1, |
2268 | EXTENT_DIRTY, 0)) { | 2374 | EXTENT_DIRTY, 0, NULL)) { |
2269 | cur = cur + iosize; | 2375 | cur = cur + iosize; |
2270 | pg_offset += iosize; | 2376 | pg_offset += iosize; |
2271 | continue; | 2377 | continue; |
2272 | } | 2378 | } |
2273 | 2379 | ||
2274 | clear_extent_dirty(tree, cur, cur + iosize - 1, GFP_NOFS); | ||
2275 | if (tree->ops && tree->ops->writepage_io_hook) { | 2380 | if (tree->ops && tree->ops->writepage_io_hook) { |
2276 | ret = tree->ops->writepage_io_hook(page, cur, | 2381 | ret = tree->ops->writepage_io_hook(page, cur, |
2277 | cur + iosize - 1); | 2382 | cur + iosize - 1); |
@@ -2309,12 +2414,12 @@ done: | |||
2309 | set_page_writeback(page); | 2414 | set_page_writeback(page); |
2310 | end_page_writeback(page); | 2415 | end_page_writeback(page); |
2311 | } | 2416 | } |
2312 | if (unlock_start <= page_end) | ||
2313 | unlock_extent(tree, unlock_start, page_end, GFP_NOFS); | ||
2314 | unlock_page(page); | 2417 | unlock_page(page); |
2315 | 2418 | ||
2316 | done_unlocked: | 2419 | done_unlocked: |
2317 | 2420 | ||
2421 | /* drop our reference on any cached states */ | ||
2422 | free_extent_state(cached_state); | ||
2318 | return 0; | 2423 | return 0; |
2319 | } | 2424 | } |
2320 | 2425 | ||
@@ -2339,9 +2444,9 @@ static int extent_write_cache_pages(struct extent_io_tree *tree, | |||
2339 | writepage_t writepage, void *data, | 2444 | writepage_t writepage, void *data, |
2340 | void (*flush_fn)(void *)) | 2445 | void (*flush_fn)(void *)) |
2341 | { | 2446 | { |
2342 | struct backing_dev_info *bdi = mapping->backing_dev_info; | ||
2343 | int ret = 0; | 2447 | int ret = 0; |
2344 | int done = 0; | 2448 | int done = 0; |
2449 | int nr_to_write_done = 0; | ||
2345 | struct pagevec pvec; | 2450 | struct pagevec pvec; |
2346 | int nr_pages; | 2451 | int nr_pages; |
2347 | pgoff_t index; | 2452 | pgoff_t index; |
@@ -2361,7 +2466,7 @@ static int extent_write_cache_pages(struct extent_io_tree *tree, | |||
2361 | scanned = 1; | 2466 | scanned = 1; |
2362 | } | 2467 | } |
2363 | retry: | 2468 | retry: |
2364 | while (!done && (index <= end) && | 2469 | while (!done && !nr_to_write_done && (index <= end) && |
2365 | (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, | 2470 | (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, |
2366 | PAGECACHE_TAG_DIRTY, min(end - index, | 2471 | PAGECACHE_TAG_DIRTY, min(end - index, |
2367 | (pgoff_t)PAGEVEC_SIZE-1) + 1))) { | 2472 | (pgoff_t)PAGEVEC_SIZE-1) + 1))) { |
@@ -2412,12 +2517,15 @@ retry: | |||
2412 | unlock_page(page); | 2517 | unlock_page(page); |
2413 | ret = 0; | 2518 | ret = 0; |
2414 | } | 2519 | } |
2415 | if (ret || wbc->nr_to_write <= 0) | 2520 | if (ret) |
2416 | done = 1; | ||
2417 | if (wbc->nonblocking && bdi_write_congested(bdi)) { | ||
2418 | wbc->encountered_congestion = 1; | ||
2419 | done = 1; | 2521 | done = 1; |
2420 | } | 2522 | |
2523 | /* | ||
2524 | * the filesystem may choose to bump up nr_to_write. | ||
2525 | * We have to make sure to honor the new nr_to_write | ||
2526 | * at any time | ||
2527 | */ | ||
2528 | nr_to_write_done = wbc->nr_to_write <= 0; | ||
2421 | } | 2529 | } |
2422 | pagevec_release(&pvec); | 2530 | pagevec_release(&pvec); |
2423 | cond_resched(); | 2531 | cond_resched(); |
@@ -2604,10 +2712,10 @@ int extent_invalidatepage(struct extent_io_tree *tree, | |||
2604 | return 0; | 2712 | return 0; |
2605 | 2713 | ||
2606 | lock_extent(tree, start, end, GFP_NOFS); | 2714 | lock_extent(tree, start, end, GFP_NOFS); |
2607 | wait_on_extent_writeback(tree, start, end); | 2715 | wait_on_page_writeback(page); |
2608 | clear_extent_bit(tree, start, end, | 2716 | clear_extent_bit(tree, start, end, |
2609 | EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC, | 2717 | EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC, |
2610 | 1, 1, GFP_NOFS); | 2718 | 1, 1, NULL, GFP_NOFS); |
2611 | return 0; | 2719 | return 0; |
2612 | } | 2720 | } |
2613 | 2721 | ||
@@ -2687,7 +2795,7 @@ int extent_prepare_write(struct extent_io_tree *tree, | |||
2687 | !isnew && !PageUptodate(page) && | 2795 | !isnew && !PageUptodate(page) && |
2688 | (block_off_end > to || block_off_start < from) && | 2796 | (block_off_end > to || block_off_start < from) && |
2689 | !test_range_bit(tree, block_start, cur_end, | 2797 | !test_range_bit(tree, block_start, cur_end, |
2690 | EXTENT_UPTODATE, 1)) { | 2798 | EXTENT_UPTODATE, 1, NULL)) { |
2691 | u64 sector; | 2799 | u64 sector; |
2692 | u64 extent_offset = block_start - em->start; | 2800 | u64 extent_offset = block_start - em->start; |
2693 | size_t iosize; | 2801 | size_t iosize; |
@@ -2701,7 +2809,7 @@ int extent_prepare_write(struct extent_io_tree *tree, | |||
2701 | */ | 2809 | */ |
2702 | set_extent_bit(tree, block_start, | 2810 | set_extent_bit(tree, block_start, |
2703 | block_start + iosize - 1, | 2811 | block_start + iosize - 1, |
2704 | EXTENT_LOCKED, 0, NULL, GFP_NOFS); | 2812 | EXTENT_LOCKED, 0, NULL, NULL, GFP_NOFS); |
2705 | ret = submit_extent_page(READ, tree, page, | 2813 | ret = submit_extent_page(READ, tree, page, |
2706 | sector, iosize, page_offset, em->bdev, | 2814 | sector, iosize, page_offset, em->bdev, |
2707 | NULL, 1, | 2815 | NULL, 1, |
@@ -2742,13 +2850,18 @@ int try_release_extent_state(struct extent_map_tree *map, | |||
2742 | int ret = 1; | 2850 | int ret = 1; |
2743 | 2851 | ||
2744 | if (test_range_bit(tree, start, end, | 2852 | if (test_range_bit(tree, start, end, |
2745 | EXTENT_IOBITS | EXTENT_ORDERED, 0)) | 2853 | EXTENT_IOBITS, 0, NULL)) |
2746 | ret = 0; | 2854 | ret = 0; |
2747 | else { | 2855 | else { |
2748 | if ((mask & GFP_NOFS) == GFP_NOFS) | 2856 | if ((mask & GFP_NOFS) == GFP_NOFS) |
2749 | mask = GFP_NOFS; | 2857 | mask = GFP_NOFS; |
2750 | clear_extent_bit(tree, start, end, EXTENT_UPTODATE, | 2858 | /* |
2751 | 1, 1, mask); | 2859 | * at this point we can safely clear everything except the |
2860 | * locked bit and the nodatasum bit | ||
2861 | */ | ||
2862 | clear_extent_bit(tree, start, end, | ||
2863 | ~(EXTENT_LOCKED | EXTENT_NODATASUM), | ||
2864 | 0, 0, NULL, mask); | ||
2752 | } | 2865 | } |
2753 | return ret; | 2866 | return ret; |
2754 | } | 2867 | } |
@@ -2771,29 +2884,28 @@ int try_release_extent_mapping(struct extent_map_tree *map, | |||
2771 | u64 len; | 2884 | u64 len; |
2772 | while (start <= end) { | 2885 | while (start <= end) { |
2773 | len = end - start + 1; | 2886 | len = end - start + 1; |
2774 | spin_lock(&map->lock); | 2887 | write_lock(&map->lock); |
2775 | em = lookup_extent_mapping(map, start, len); | 2888 | em = lookup_extent_mapping(map, start, len); |
2776 | if (!em || IS_ERR(em)) { | 2889 | if (!em || IS_ERR(em)) { |
2777 | spin_unlock(&map->lock); | 2890 | write_unlock(&map->lock); |
2778 | break; | 2891 | break; |
2779 | } | 2892 | } |
2780 | if (test_bit(EXTENT_FLAG_PINNED, &em->flags) || | 2893 | if (test_bit(EXTENT_FLAG_PINNED, &em->flags) || |
2781 | em->start != start) { | 2894 | em->start != start) { |
2782 | spin_unlock(&map->lock); | 2895 | write_unlock(&map->lock); |
2783 | free_extent_map(em); | 2896 | free_extent_map(em); |
2784 | break; | 2897 | break; |
2785 | } | 2898 | } |
2786 | if (!test_range_bit(tree, em->start, | 2899 | if (!test_range_bit(tree, em->start, |
2787 | extent_map_end(em) - 1, | 2900 | extent_map_end(em) - 1, |
2788 | EXTENT_LOCKED | EXTENT_WRITEBACK | | 2901 | EXTENT_LOCKED | EXTENT_WRITEBACK, |
2789 | EXTENT_ORDERED, | 2902 | 0, NULL)) { |
2790 | 0)) { | ||
2791 | remove_extent_mapping(map, em); | 2903 | remove_extent_mapping(map, em); |
2792 | /* once for the rb tree */ | 2904 | /* once for the rb tree */ |
2793 | free_extent_map(em); | 2905 | free_extent_map(em); |
2794 | } | 2906 | } |
2795 | start = extent_map_end(em); | 2907 | start = extent_map_end(em); |
2796 | spin_unlock(&map->lock); | 2908 | write_unlock(&map->lock); |
2797 | 2909 | ||
2798 | /* once for us */ | 2910 | /* once for us */ |
2799 | free_extent_map(em); | 2911 | free_extent_map(em); |
@@ -3203,7 +3315,7 @@ int extent_range_uptodate(struct extent_io_tree *tree, | |||
3203 | int uptodate; | 3315 | int uptodate; |
3204 | unsigned long index; | 3316 | unsigned long index; |
3205 | 3317 | ||
3206 | ret = test_range_bit(tree, start, end, EXTENT_UPTODATE, 1); | 3318 | ret = test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL); |
3207 | if (ret) | 3319 | if (ret) |
3208 | return 1; | 3320 | return 1; |
3209 | while (start <= end) { | 3321 | while (start <= end) { |
@@ -3233,7 +3345,7 @@ int extent_buffer_uptodate(struct extent_io_tree *tree, | |||
3233 | return 1; | 3345 | return 1; |
3234 | 3346 | ||
3235 | ret = test_range_bit(tree, eb->start, eb->start + eb->len - 1, | 3347 | ret = test_range_bit(tree, eb->start, eb->start + eb->len - 1, |
3236 | EXTENT_UPTODATE, 1); | 3348 | EXTENT_UPTODATE, 1, NULL); |
3237 | if (ret) | 3349 | if (ret) |
3238 | return ret; | 3350 | return ret; |
3239 | 3351 | ||
@@ -3269,7 +3381,7 @@ int read_extent_buffer_pages(struct extent_io_tree *tree, | |||
3269 | return 0; | 3381 | return 0; |
3270 | 3382 | ||
3271 | if (test_range_bit(tree, eb->start, eb->start + eb->len - 1, | 3383 | if (test_range_bit(tree, eb->start, eb->start + eb->len - 1, |
3272 | EXTENT_UPTODATE, 1)) { | 3384 | EXTENT_UPTODATE, 1, NULL)) { |
3273 | return 0; | 3385 | return 0; |
3274 | } | 3386 | } |
3275 | 3387 | ||