diff options
Diffstat (limited to 'fs/btrfs/extent_io.c')
-rw-r--r-- | fs/btrfs/extent_io.c | 134 |
1 files changed, 91 insertions, 43 deletions
diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c index 0cb88f8146ea..96577e8bf9fd 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,11 +379,14 @@ 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 | } |
368 | if (bits & EXTENT_DIRTY) | ||
369 | tree->dirty_bytes += end - start + 1; | ||
370 | state->start = start; | 382 | state->start = start; |
371 | state->end = end; | 383 | state->end = end; |
372 | set_state_cb(tree, state, bits); | 384 | ret = set_state_cb(tree, state, bits); |
385 | if (ret) | ||
386 | return ret; | ||
387 | |||
388 | if (bits & EXTENT_DIRTY) | ||
389 | tree->dirty_bytes += end - start + 1; | ||
373 | state->state |= bits; | 390 | state->state |= bits; |
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) { |
@@ -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; |
@@ -431,7 +460,8 @@ static int clear_state_bit(struct extent_io_tree *tree, | |||
431 | struct extent_state *state, int bits, int wake, | 460 | struct extent_state *state, int bits, int wake, |
432 | int delete) | 461 | int delete) |
433 | { | 462 | { |
434 | int ret = state->state & bits; | 463 | int bits_to_clear = bits & ~EXTENT_DO_ACCOUNTING; |
464 | int ret = state->state & bits_to_clear; | ||
435 | 465 | ||
436 | if ((bits & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) { | 466 | if ((bits & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) { |
437 | u64 range = state->end - state->start + 1; | 467 | u64 range = state->end - state->start + 1; |
@@ -439,7 +469,7 @@ static int clear_state_bit(struct extent_io_tree *tree, | |||
439 | tree->dirty_bytes -= range; | 469 | tree->dirty_bytes -= range; |
440 | } | 470 | } |
441 | clear_state_cb(tree, state, bits); | 471 | clear_state_cb(tree, state, bits); |
442 | state->state &= ~bits; | 472 | state->state &= ~bits_to_clear; |
443 | if (wake) | 473 | if (wake) |
444 | wake_up(&state->wq); | 474 | wake_up(&state->wq); |
445 | if (delete || state->state == 0) { | 475 | if (delete || state->state == 0) { |
@@ -542,8 +572,8 @@ hit_next: | |||
542 | if (err) | 572 | if (err) |
543 | goto out; | 573 | goto out; |
544 | if (state->end <= end) { | 574 | if (state->end <= end) { |
545 | set |= clear_state_bit(tree, state, bits, | 575 | set |= clear_state_bit(tree, state, bits, wake, |
546 | wake, delete); | 576 | delete); |
547 | if (last_end == (u64)-1) | 577 | if (last_end == (u64)-1) |
548 | goto out; | 578 | goto out; |
549 | start = last_end + 1; | 579 | start = last_end + 1; |
@@ -561,12 +591,11 @@ hit_next: | |||
561 | prealloc = alloc_extent_state(GFP_ATOMIC); | 591 | prealloc = alloc_extent_state(GFP_ATOMIC); |
562 | err = split_state(tree, state, prealloc, end + 1); | 592 | err = split_state(tree, state, prealloc, end + 1); |
563 | BUG_ON(err == -EEXIST); | 593 | BUG_ON(err == -EEXIST); |
564 | |||
565 | if (wake) | 594 | if (wake) |
566 | wake_up(&state->wq); | 595 | wake_up(&state->wq); |
567 | 596 | ||
568 | set |= clear_state_bit(tree, prealloc, bits, | 597 | set |= clear_state_bit(tree, prealloc, bits, wake, delete); |
569 | wake, delete); | 598 | |
570 | prealloc = NULL; | 599 | prealloc = NULL; |
571 | goto out; | 600 | goto out; |
572 | } | 601 | } |
@@ -667,16 +696,23 @@ out: | |||
667 | return 0; | 696 | return 0; |
668 | } | 697 | } |
669 | 698 | ||
670 | static void set_state_bits(struct extent_io_tree *tree, | 699 | static int set_state_bits(struct extent_io_tree *tree, |
671 | struct extent_state *state, | 700 | struct extent_state *state, |
672 | int bits) | 701 | int bits) |
673 | { | 702 | { |
703 | int ret; | ||
704 | |||
705 | ret = set_state_cb(tree, state, bits); | ||
706 | if (ret) | ||
707 | return ret; | ||
708 | |||
674 | if ((bits & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) { | 709 | if ((bits & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) { |
675 | u64 range = state->end - state->start + 1; | 710 | u64 range = state->end - state->start + 1; |
676 | tree->dirty_bytes += range; | 711 | tree->dirty_bytes += range; |
677 | } | 712 | } |
678 | set_state_cb(tree, state, bits); | ||
679 | state->state |= bits; | 713 | state->state |= bits; |
714 | |||
715 | return 0; | ||
680 | } | 716 | } |
681 | 717 | ||
682 | static void cache_state(struct extent_state *state, | 718 | static void cache_state(struct extent_state *state, |
@@ -758,7 +794,10 @@ hit_next: | |||
758 | goto out; | 794 | goto out; |
759 | } | 795 | } |
760 | 796 | ||
761 | set_state_bits(tree, state, bits); | 797 | err = set_state_bits(tree, state, bits); |
798 | if (err) | ||
799 | goto out; | ||
800 | |||
762 | cache_state(state, cached_state); | 801 | cache_state(state, cached_state); |
763 | merge_state(tree, state); | 802 | merge_state(tree, state); |
764 | if (last_end == (u64)-1) | 803 | if (last_end == (u64)-1) |
@@ -805,7 +844,9 @@ hit_next: | |||
805 | if (err) | 844 | if (err) |
806 | goto out; | 845 | goto out; |
807 | if (state->end <= end) { | 846 | if (state->end <= end) { |
808 | set_state_bits(tree, state, bits); | 847 | err = set_state_bits(tree, state, bits); |
848 | if (err) | ||
849 | goto out; | ||
809 | cache_state(state, cached_state); | 850 | cache_state(state, cached_state); |
810 | merge_state(tree, state); | 851 | merge_state(tree, state); |
811 | if (last_end == (u64)-1) | 852 | if (last_end == (u64)-1) |
@@ -829,11 +870,13 @@ hit_next: | |||
829 | this_end = last_start - 1; | 870 | this_end = last_start - 1; |
830 | err = insert_state(tree, prealloc, start, this_end, | 871 | err = insert_state(tree, prealloc, start, this_end, |
831 | bits); | 872 | bits); |
832 | cache_state(prealloc, cached_state); | ||
833 | prealloc = NULL; | ||
834 | BUG_ON(err == -EEXIST); | 873 | BUG_ON(err == -EEXIST); |
835 | if (err) | 874 | if (err) { |
875 | prealloc = NULL; | ||
836 | goto out; | 876 | goto out; |
877 | } | ||
878 | cache_state(prealloc, cached_state); | ||
879 | prealloc = NULL; | ||
837 | start = this_end + 1; | 880 | start = this_end + 1; |
838 | goto search_again; | 881 | goto search_again; |
839 | } | 882 | } |
@@ -852,7 +895,11 @@ hit_next: | |||
852 | err = split_state(tree, state, prealloc, end + 1); | 895 | err = split_state(tree, state, prealloc, end + 1); |
853 | BUG_ON(err == -EEXIST); | 896 | BUG_ON(err == -EEXIST); |
854 | 897 | ||
855 | set_state_bits(tree, prealloc, bits); | 898 | err = set_state_bits(tree, prealloc, bits); |
899 | if (err) { | ||
900 | prealloc = NULL; | ||
901 | goto out; | ||
902 | } | ||
856 | cache_state(prealloc, cached_state); | 903 | cache_state(prealloc, cached_state); |
857 | merge_state(tree, prealloc); | 904 | merge_state(tree, prealloc); |
858 | prealloc = NULL; | 905 | prealloc = NULL; |
@@ -910,7 +957,8 @@ int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end, | |||
910 | gfp_t mask) | 957 | gfp_t mask) |
911 | { | 958 | { |
912 | return clear_extent_bit(tree, start, end, | 959 | return clear_extent_bit(tree, start, end, |
913 | EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0, | 960 | EXTENT_DIRTY | EXTENT_DELALLOC | |
961 | EXTENT_DO_ACCOUNTING, 0, 0, | ||
914 | NULL, mask); | 962 | NULL, mask); |
915 | } | 963 | } |
916 | 964 | ||
@@ -1355,12 +1403,7 @@ out_failed: | |||
1355 | int extent_clear_unlock_delalloc(struct inode *inode, | 1403 | int extent_clear_unlock_delalloc(struct inode *inode, |
1356 | struct extent_io_tree *tree, | 1404 | struct extent_io_tree *tree, |
1357 | u64 start, u64 end, struct page *locked_page, | 1405 | u64 start, u64 end, struct page *locked_page, |
1358 | int unlock_pages, | 1406 | unsigned long op) |
1359 | int clear_unlock, | ||
1360 | int clear_delalloc, int clear_dirty, | ||
1361 | int set_writeback, | ||
1362 | int end_writeback, | ||
1363 | int set_private2) | ||
1364 | { | 1407 | { |
1365 | int ret; | 1408 | int ret; |
1366 | struct page *pages[16]; | 1409 | struct page *pages[16]; |
@@ -1370,17 +1413,21 @@ int extent_clear_unlock_delalloc(struct inode *inode, | |||
1370 | int i; | 1413 | int i; |
1371 | int clear_bits = 0; | 1414 | int clear_bits = 0; |
1372 | 1415 | ||
1373 | if (clear_unlock) | 1416 | if (op & EXTENT_CLEAR_UNLOCK) |
1374 | clear_bits |= EXTENT_LOCKED; | 1417 | clear_bits |= EXTENT_LOCKED; |
1375 | if (clear_dirty) | 1418 | if (op & EXTENT_CLEAR_DIRTY) |
1376 | clear_bits |= EXTENT_DIRTY; | 1419 | clear_bits |= EXTENT_DIRTY; |
1377 | 1420 | ||
1378 | if (clear_delalloc) | 1421 | if (op & EXTENT_CLEAR_DELALLOC) |
1379 | clear_bits |= EXTENT_DELALLOC; | 1422 | clear_bits |= EXTENT_DELALLOC; |
1380 | 1423 | ||
1424 | if (op & EXTENT_CLEAR_ACCOUNTING) | ||
1425 | clear_bits |= EXTENT_DO_ACCOUNTING; | ||
1426 | |||
1381 | clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS); | 1427 | clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS); |
1382 | if (!(unlock_pages || clear_dirty || set_writeback || end_writeback || | 1428 | if (!(op & (EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY | |
1383 | set_private2)) | 1429 | EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK | |
1430 | EXTENT_SET_PRIVATE2))) | ||
1384 | return 0; | 1431 | return 0; |
1385 | 1432 | ||
1386 | while (nr_pages > 0) { | 1433 | while (nr_pages > 0) { |
@@ -1389,20 +1436,20 @@ int extent_clear_unlock_delalloc(struct inode *inode, | |||
1389 | nr_pages, ARRAY_SIZE(pages)), pages); | 1436 | nr_pages, ARRAY_SIZE(pages)), pages); |
1390 | for (i = 0; i < ret; i++) { | 1437 | for (i = 0; i < ret; i++) { |
1391 | 1438 | ||
1392 | if (set_private2) | 1439 | if (op & EXTENT_SET_PRIVATE2) |
1393 | SetPagePrivate2(pages[i]); | 1440 | SetPagePrivate2(pages[i]); |
1394 | 1441 | ||
1395 | if (pages[i] == locked_page) { | 1442 | if (pages[i] == locked_page) { |
1396 | page_cache_release(pages[i]); | 1443 | page_cache_release(pages[i]); |
1397 | continue; | 1444 | continue; |
1398 | } | 1445 | } |
1399 | if (clear_dirty) | 1446 | if (op & EXTENT_CLEAR_DIRTY) |
1400 | clear_page_dirty_for_io(pages[i]); | 1447 | clear_page_dirty_for_io(pages[i]); |
1401 | if (set_writeback) | 1448 | if (op & EXTENT_SET_WRITEBACK) |
1402 | set_page_writeback(pages[i]); | 1449 | set_page_writeback(pages[i]); |
1403 | if (end_writeback) | 1450 | if (op & EXTENT_END_WRITEBACK) |
1404 | end_page_writeback(pages[i]); | 1451 | end_page_writeback(pages[i]); |
1405 | if (unlock_pages) | 1452 | if (op & EXTENT_CLEAR_UNLOCK_PAGE) |
1406 | unlock_page(pages[i]); | 1453 | unlock_page(pages[i]); |
1407 | page_cache_release(pages[i]); | 1454 | page_cache_release(pages[i]); |
1408 | } | 1455 | } |
@@ -2668,7 +2715,8 @@ int extent_invalidatepage(struct extent_io_tree *tree, | |||
2668 | lock_extent(tree, start, end, GFP_NOFS); | 2715 | lock_extent(tree, start, end, GFP_NOFS); |
2669 | wait_on_page_writeback(page); | 2716 | wait_on_page_writeback(page); |
2670 | clear_extent_bit(tree, start, end, | 2717 | clear_extent_bit(tree, start, end, |
2671 | EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC, | 2718 | EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC | |
2719 | EXTENT_DO_ACCOUNTING, | ||
2672 | 1, 1, NULL, GFP_NOFS); | 2720 | 1, 1, NULL, GFP_NOFS); |
2673 | return 0; | 2721 | return 0; |
2674 | } | 2722 | } |