diff options
Diffstat (limited to 'fs/btrfs/tree-log.c')
-rw-r--r-- | fs/btrfs/tree-log.c | 438 |
1 files changed, 201 insertions, 237 deletions
diff --git a/fs/btrfs/tree-log.c b/fs/btrfs/tree-log.c index 451fad96ecd1..c276ac9a0ec3 100644 --- a/fs/btrfs/tree-log.c +++ b/fs/btrfs/tree-log.c | |||
@@ -277,17 +277,19 @@ static int process_one_buffer(struct btrfs_root *log, | |||
277 | struct extent_buffer *eb, | 277 | struct extent_buffer *eb, |
278 | struct walk_control *wc, u64 gen) | 278 | struct walk_control *wc, u64 gen) |
279 | { | 279 | { |
280 | int ret = 0; | ||
281 | |||
280 | if (wc->pin) | 282 | if (wc->pin) |
281 | btrfs_pin_extent_for_log_replay(log->fs_info->extent_root, | 283 | ret = btrfs_pin_extent_for_log_replay(log->fs_info->extent_root, |
282 | eb->start, eb->len); | 284 | eb->start, eb->len); |
283 | 285 | ||
284 | if (btrfs_buffer_uptodate(eb, gen, 0)) { | 286 | if (!ret && btrfs_buffer_uptodate(eb, gen, 0)) { |
285 | if (wc->write) | 287 | if (wc->write) |
286 | btrfs_write_tree_block(eb); | 288 | btrfs_write_tree_block(eb); |
287 | if (wc->wait) | 289 | if (wc->wait) |
288 | btrfs_wait_tree_block_writeback(eb); | 290 | btrfs_wait_tree_block_writeback(eb); |
289 | } | 291 | } |
290 | return 0; | 292 | return ret; |
291 | } | 293 | } |
292 | 294 | ||
293 | /* | 295 | /* |
@@ -317,6 +319,7 @@ static noinline int overwrite_item(struct btrfs_trans_handle *trans, | |||
317 | unsigned long src_ptr; | 319 | unsigned long src_ptr; |
318 | unsigned long dst_ptr; | 320 | unsigned long dst_ptr; |
319 | int overwrite_root = 0; | 321 | int overwrite_root = 0; |
322 | bool inode_item = key->type == BTRFS_INODE_ITEM_KEY; | ||
320 | 323 | ||
321 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) | 324 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) |
322 | overwrite_root = 1; | 325 | overwrite_root = 1; |
@@ -326,6 +329,9 @@ static noinline int overwrite_item(struct btrfs_trans_handle *trans, | |||
326 | 329 | ||
327 | /* look for the key in the destination tree */ | 330 | /* look for the key in the destination tree */ |
328 | ret = btrfs_search_slot(NULL, root, key, path, 0, 0); | 331 | ret = btrfs_search_slot(NULL, root, key, path, 0, 0); |
332 | if (ret < 0) | ||
333 | return ret; | ||
334 | |||
329 | if (ret == 0) { | 335 | if (ret == 0) { |
330 | char *src_copy; | 336 | char *src_copy; |
331 | char *dst_copy; | 337 | char *dst_copy; |
@@ -367,6 +373,30 @@ static noinline int overwrite_item(struct btrfs_trans_handle *trans, | |||
367 | return 0; | 373 | return 0; |
368 | } | 374 | } |
369 | 375 | ||
376 | /* | ||
377 | * We need to load the old nbytes into the inode so when we | ||
378 | * replay the extents we've logged we get the right nbytes. | ||
379 | */ | ||
380 | if (inode_item) { | ||
381 | struct btrfs_inode_item *item; | ||
382 | u64 nbytes; | ||
383 | |||
384 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], | ||
385 | struct btrfs_inode_item); | ||
386 | nbytes = btrfs_inode_nbytes(path->nodes[0], item); | ||
387 | item = btrfs_item_ptr(eb, slot, | ||
388 | struct btrfs_inode_item); | ||
389 | btrfs_set_inode_nbytes(eb, item, nbytes); | ||
390 | } | ||
391 | } else if (inode_item) { | ||
392 | struct btrfs_inode_item *item; | ||
393 | |||
394 | /* | ||
395 | * New inode, set nbytes to 0 so that the nbytes comes out | ||
396 | * properly when we replay the extents. | ||
397 | */ | ||
398 | item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item); | ||
399 | btrfs_set_inode_nbytes(eb, item, 0); | ||
370 | } | 400 | } |
371 | insert: | 401 | insert: |
372 | btrfs_release_path(path); | 402 | btrfs_release_path(path); |
@@ -380,9 +410,9 @@ insert: | |||
380 | found_size = btrfs_item_size_nr(path->nodes[0], | 410 | found_size = btrfs_item_size_nr(path->nodes[0], |
381 | path->slots[0]); | 411 | path->slots[0]); |
382 | if (found_size > item_size) | 412 | if (found_size > item_size) |
383 | btrfs_truncate_item(trans, root, path, item_size, 1); | 413 | btrfs_truncate_item(root, path, item_size, 1); |
384 | else if (found_size < item_size) | 414 | else if (found_size < item_size) |
385 | btrfs_extend_item(trans, root, path, | 415 | btrfs_extend_item(root, path, |
386 | item_size - found_size); | 416 | item_size - found_size); |
387 | } else if (ret) { | 417 | } else if (ret) { |
388 | return ret; | 418 | return ret; |
@@ -486,7 +516,7 @@ static noinline int replay_one_extent(struct btrfs_trans_handle *trans, | |||
486 | int found_type; | 516 | int found_type; |
487 | u64 extent_end; | 517 | u64 extent_end; |
488 | u64 start = key->offset; | 518 | u64 start = key->offset; |
489 | u64 saved_nbytes; | 519 | u64 nbytes = 0; |
490 | struct btrfs_file_extent_item *item; | 520 | struct btrfs_file_extent_item *item; |
491 | struct inode *inode = NULL; | 521 | struct inode *inode = NULL; |
492 | unsigned long size; | 522 | unsigned long size; |
@@ -496,10 +526,19 @@ static noinline int replay_one_extent(struct btrfs_trans_handle *trans, | |||
496 | found_type = btrfs_file_extent_type(eb, item); | 526 | found_type = btrfs_file_extent_type(eb, item); |
497 | 527 | ||
498 | if (found_type == BTRFS_FILE_EXTENT_REG || | 528 | if (found_type == BTRFS_FILE_EXTENT_REG || |
499 | found_type == BTRFS_FILE_EXTENT_PREALLOC) | 529 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
500 | extent_end = start + btrfs_file_extent_num_bytes(eb, item); | 530 | nbytes = btrfs_file_extent_num_bytes(eb, item); |
501 | else if (found_type == BTRFS_FILE_EXTENT_INLINE) { | 531 | extent_end = start + nbytes; |
532 | |||
533 | /* | ||
534 | * We don't add to the inodes nbytes if we are prealloc or a | ||
535 | * hole. | ||
536 | */ | ||
537 | if (btrfs_file_extent_disk_bytenr(eb, item) == 0) | ||
538 | nbytes = 0; | ||
539 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { | ||
502 | size = btrfs_file_extent_inline_len(eb, item); | 540 | size = btrfs_file_extent_inline_len(eb, item); |
541 | nbytes = btrfs_file_extent_ram_bytes(eb, item); | ||
503 | extent_end = ALIGN(start + size, root->sectorsize); | 542 | extent_end = ALIGN(start + size, root->sectorsize); |
504 | } else { | 543 | } else { |
505 | ret = 0; | 544 | ret = 0; |
@@ -548,10 +587,10 @@ static noinline int replay_one_extent(struct btrfs_trans_handle *trans, | |||
548 | } | 587 | } |
549 | btrfs_release_path(path); | 588 | btrfs_release_path(path); |
550 | 589 | ||
551 | saved_nbytes = inode_get_bytes(inode); | ||
552 | /* drop any overlapping extents */ | 590 | /* drop any overlapping extents */ |
553 | ret = btrfs_drop_extents(trans, root, inode, start, extent_end, 1); | 591 | ret = btrfs_drop_extents(trans, root, inode, start, extent_end, 1); |
554 | BUG_ON(ret); | 592 | if (ret) |
593 | goto out; | ||
555 | 594 | ||
556 | if (found_type == BTRFS_FILE_EXTENT_REG || | 595 | if (found_type == BTRFS_FILE_EXTENT_REG || |
557 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { | 596 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
@@ -561,7 +600,8 @@ static noinline int replay_one_extent(struct btrfs_trans_handle *trans, | |||
561 | 600 | ||
562 | ret = btrfs_insert_empty_item(trans, root, path, key, | 601 | ret = btrfs_insert_empty_item(trans, root, path, key, |
563 | sizeof(*item)); | 602 | sizeof(*item)); |
564 | BUG_ON(ret); | 603 | if (ret) |
604 | goto out; | ||
565 | dest_offset = btrfs_item_ptr_offset(path->nodes[0], | 605 | dest_offset = btrfs_item_ptr_offset(path->nodes[0], |
566 | path->slots[0]); | 606 | path->slots[0]); |
567 | copy_extent_buffer(path->nodes[0], eb, dest_offset, | 607 | copy_extent_buffer(path->nodes[0], eb, dest_offset, |
@@ -587,7 +627,8 @@ static noinline int replay_one_extent(struct btrfs_trans_handle *trans, | |||
587 | ins.objectid, ins.offset, | 627 | ins.objectid, ins.offset, |
588 | 0, root->root_key.objectid, | 628 | 0, root->root_key.objectid, |
589 | key->objectid, offset, 0); | 629 | key->objectid, offset, 0); |
590 | BUG_ON(ret); | 630 | if (ret) |
631 | goto out; | ||
591 | } else { | 632 | } else { |
592 | /* | 633 | /* |
593 | * insert the extent pointer in the extent | 634 | * insert the extent pointer in the extent |
@@ -596,7 +637,8 @@ static noinline int replay_one_extent(struct btrfs_trans_handle *trans, | |||
596 | ret = btrfs_alloc_logged_file_extent(trans, | 637 | ret = btrfs_alloc_logged_file_extent(trans, |
597 | root, root->root_key.objectid, | 638 | root, root->root_key.objectid, |
598 | key->objectid, offset, &ins); | 639 | key->objectid, offset, &ins); |
599 | BUG_ON(ret); | 640 | if (ret) |
641 | goto out; | ||
600 | } | 642 | } |
601 | btrfs_release_path(path); | 643 | btrfs_release_path(path); |
602 | 644 | ||
@@ -613,29 +655,33 @@ static noinline int replay_one_extent(struct btrfs_trans_handle *trans, | |||
613 | ret = btrfs_lookup_csums_range(root->log_root, | 655 | ret = btrfs_lookup_csums_range(root->log_root, |
614 | csum_start, csum_end - 1, | 656 | csum_start, csum_end - 1, |
615 | &ordered_sums, 0); | 657 | &ordered_sums, 0); |
616 | BUG_ON(ret); | 658 | if (ret) |
659 | goto out; | ||
617 | while (!list_empty(&ordered_sums)) { | 660 | while (!list_empty(&ordered_sums)) { |
618 | struct btrfs_ordered_sum *sums; | 661 | struct btrfs_ordered_sum *sums; |
619 | sums = list_entry(ordered_sums.next, | 662 | sums = list_entry(ordered_sums.next, |
620 | struct btrfs_ordered_sum, | 663 | struct btrfs_ordered_sum, |
621 | list); | 664 | list); |
622 | ret = btrfs_csum_file_blocks(trans, | 665 | if (!ret) |
666 | ret = btrfs_csum_file_blocks(trans, | ||
623 | root->fs_info->csum_root, | 667 | root->fs_info->csum_root, |
624 | sums); | 668 | sums); |
625 | BUG_ON(ret); | ||
626 | list_del(&sums->list); | 669 | list_del(&sums->list); |
627 | kfree(sums); | 670 | kfree(sums); |
628 | } | 671 | } |
672 | if (ret) | ||
673 | goto out; | ||
629 | } else { | 674 | } else { |
630 | btrfs_release_path(path); | 675 | btrfs_release_path(path); |
631 | } | 676 | } |
632 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { | 677 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
633 | /* inline extents are easy, we just overwrite them */ | 678 | /* inline extents are easy, we just overwrite them */ |
634 | ret = overwrite_item(trans, root, path, eb, slot, key); | 679 | ret = overwrite_item(trans, root, path, eb, slot, key); |
635 | BUG_ON(ret); | 680 | if (ret) |
681 | goto out; | ||
636 | } | 682 | } |
637 | 683 | ||
638 | inode_set_bytes(inode, saved_nbytes); | 684 | inode_add_bytes(inode, nbytes); |
639 | ret = btrfs_update_inode(trans, root, inode); | 685 | ret = btrfs_update_inode(trans, root, inode); |
640 | out: | 686 | out: |
641 | if (inode) | 687 | if (inode) |
@@ -677,20 +723,21 @@ static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans, | |||
677 | 723 | ||
678 | inode = read_one_inode(root, location.objectid); | 724 | inode = read_one_inode(root, location.objectid); |
679 | if (!inode) { | 725 | if (!inode) { |
680 | kfree(name); | 726 | ret = -EIO; |
681 | return -EIO; | 727 | goto out; |
682 | } | 728 | } |
683 | 729 | ||
684 | ret = link_to_fixup_dir(trans, root, path, location.objectid); | 730 | ret = link_to_fixup_dir(trans, root, path, location.objectid); |
685 | BUG_ON(ret); | 731 | if (ret) |
732 | goto out; | ||
686 | 733 | ||
687 | ret = btrfs_unlink_inode(trans, root, dir, inode, name, name_len); | 734 | ret = btrfs_unlink_inode(trans, root, dir, inode, name, name_len); |
688 | BUG_ON(ret); | 735 | if (ret) |
736 | goto out; | ||
737 | btrfs_run_delayed_items(trans, root); | ||
738 | out: | ||
689 | kfree(name); | 739 | kfree(name); |
690 | |||
691 | iput(inode); | 740 | iput(inode); |
692 | |||
693 | btrfs_run_delayed_items(trans, root); | ||
694 | return ret; | 741 | return ret; |
695 | } | 742 | } |
696 | 743 | ||
@@ -843,7 +890,8 @@ again: | |||
843 | victim_name_len = btrfs_inode_ref_name_len(leaf, | 890 | victim_name_len = btrfs_inode_ref_name_len(leaf, |
844 | victim_ref); | 891 | victim_ref); |
845 | victim_name = kmalloc(victim_name_len, GFP_NOFS); | 892 | victim_name = kmalloc(victim_name_len, GFP_NOFS); |
846 | BUG_ON(!victim_name); | 893 | if (!victim_name) |
894 | return -ENOMEM; | ||
847 | 895 | ||
848 | read_extent_buffer(leaf, victim_name, | 896 | read_extent_buffer(leaf, victim_name, |
849 | (unsigned long)(victim_ref + 1), | 897 | (unsigned long)(victim_ref + 1), |
@@ -859,9 +907,10 @@ again: | |||
859 | ret = btrfs_unlink_inode(trans, root, dir, | 907 | ret = btrfs_unlink_inode(trans, root, dir, |
860 | inode, victim_name, | 908 | inode, victim_name, |
861 | victim_name_len); | 909 | victim_name_len); |
862 | BUG_ON(ret); | ||
863 | btrfs_run_delayed_items(trans, root); | ||
864 | kfree(victim_name); | 910 | kfree(victim_name); |
911 | if (ret) | ||
912 | return ret; | ||
913 | btrfs_run_delayed_items(trans, root); | ||
865 | *search_done = 1; | 914 | *search_done = 1; |
866 | goto again; | 915 | goto again; |
867 | } | 916 | } |
@@ -869,7 +918,6 @@ again: | |||
869 | 918 | ||
870 | ptr = (unsigned long)(victim_ref + 1) + victim_name_len; | 919 | ptr = (unsigned long)(victim_ref + 1) + victim_name_len; |
871 | } | 920 | } |
872 | BUG_ON(ret); | ||
873 | 921 | ||
874 | /* | 922 | /* |
875 | * NOTE: we have searched root tree and checked the | 923 | * NOTE: we have searched root tree and checked the |
@@ -903,6 +951,8 @@ again: | |||
903 | goto next; | 951 | goto next; |
904 | 952 | ||
905 | victim_name = kmalloc(victim_name_len, GFP_NOFS); | 953 | victim_name = kmalloc(victim_name_len, GFP_NOFS); |
954 | if (!victim_name) | ||
955 | return -ENOMEM; | ||
906 | read_extent_buffer(leaf, victim_name, (unsigned long)&extref->name, | 956 | read_extent_buffer(leaf, victim_name, (unsigned long)&extref->name, |
907 | victim_name_len); | 957 | victim_name_len); |
908 | 958 | ||
@@ -929,14 +979,16 @@ again: | |||
929 | victim_name_len); | 979 | victim_name_len); |
930 | btrfs_run_delayed_items(trans, root); | 980 | btrfs_run_delayed_items(trans, root); |
931 | } | 981 | } |
932 | BUG_ON(ret); | ||
933 | iput(victim_parent); | 982 | iput(victim_parent); |
934 | kfree(victim_name); | 983 | kfree(victim_name); |
984 | if (ret) | ||
985 | return ret; | ||
935 | *search_done = 1; | 986 | *search_done = 1; |
936 | goto again; | 987 | goto again; |
937 | } | 988 | } |
938 | kfree(victim_name); | 989 | kfree(victim_name); |
939 | BUG_ON(ret); | 990 | if (ret) |
991 | return ret; | ||
940 | next: | 992 | next: |
941 | cur_offset += victim_name_len + sizeof(*extref); | 993 | cur_offset += victim_name_len + sizeof(*extref); |
942 | } | 994 | } |
@@ -949,7 +1001,8 @@ next: | |||
949 | ref_index, name, namelen, 0); | 1001 | ref_index, name, namelen, 0); |
950 | if (di && !IS_ERR(di)) { | 1002 | if (di && !IS_ERR(di)) { |
951 | ret = drop_one_dir_item(trans, root, path, dir, di); | 1003 | ret = drop_one_dir_item(trans, root, path, dir, di); |
952 | BUG_ON(ret); | 1004 | if (ret) |
1005 | return ret; | ||
953 | } | 1006 | } |
954 | btrfs_release_path(path); | 1007 | btrfs_release_path(path); |
955 | 1008 | ||
@@ -958,7 +1011,8 @@ next: | |||
958 | name, namelen, 0); | 1011 | name, namelen, 0); |
959 | if (di && !IS_ERR(di)) { | 1012 | if (di && !IS_ERR(di)) { |
960 | ret = drop_one_dir_item(trans, root, path, dir, di); | 1013 | ret = drop_one_dir_item(trans, root, path, dir, di); |
961 | BUG_ON(ret); | 1014 | if (ret) |
1015 | return ret; | ||
962 | } | 1016 | } |
963 | btrfs_release_path(path); | 1017 | btrfs_release_path(path); |
964 | 1018 | ||
@@ -1103,15 +1157,19 @@ static noinline int add_inode_ref(struct btrfs_trans_handle *trans, | |||
1103 | parent_objectid, | 1157 | parent_objectid, |
1104 | ref_index, name, namelen, | 1158 | ref_index, name, namelen, |
1105 | &search_done); | 1159 | &search_done); |
1106 | if (ret == 1) | 1160 | if (ret == 1) { |
1161 | ret = 0; | ||
1162 | goto out; | ||
1163 | } | ||
1164 | if (ret) | ||
1107 | goto out; | 1165 | goto out; |
1108 | BUG_ON(ret); | ||
1109 | } | 1166 | } |
1110 | 1167 | ||
1111 | /* insert our name */ | 1168 | /* insert our name */ |
1112 | ret = btrfs_add_link(trans, dir, inode, name, namelen, | 1169 | ret = btrfs_add_link(trans, dir, inode, name, namelen, |
1113 | 0, ref_index); | 1170 | 0, ref_index); |
1114 | BUG_ON(ret); | 1171 | if (ret) |
1172 | goto out; | ||
1115 | 1173 | ||
1116 | btrfs_update_inode(trans, root, inode); | 1174 | btrfs_update_inode(trans, root, inode); |
1117 | } | 1175 | } |
@@ -1126,13 +1184,11 @@ static noinline int add_inode_ref(struct btrfs_trans_handle *trans, | |||
1126 | 1184 | ||
1127 | /* finally write the back reference in the inode */ | 1185 | /* finally write the back reference in the inode */ |
1128 | ret = overwrite_item(trans, root, path, eb, slot, key); | 1186 | ret = overwrite_item(trans, root, path, eb, slot, key); |
1129 | BUG_ON(ret); | ||
1130 | |||
1131 | out: | 1187 | out: |
1132 | btrfs_release_path(path); | 1188 | btrfs_release_path(path); |
1133 | iput(dir); | 1189 | iput(dir); |
1134 | iput(inode); | 1190 | iput(inode); |
1135 | return 0; | 1191 | return ret; |
1136 | } | 1192 | } |
1137 | 1193 | ||
1138 | static int insert_orphan_item(struct btrfs_trans_handle *trans, | 1194 | static int insert_orphan_item(struct btrfs_trans_handle *trans, |
@@ -1290,10 +1346,10 @@ static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans, | |||
1290 | if (S_ISDIR(inode->i_mode)) { | 1346 | if (S_ISDIR(inode->i_mode)) { |
1291 | ret = replay_dir_deletes(trans, root, NULL, path, | 1347 | ret = replay_dir_deletes(trans, root, NULL, path, |
1292 | ino, 1); | 1348 | ino, 1); |
1293 | BUG_ON(ret); | 1349 | if (ret) |
1350 | goto out; | ||
1294 | } | 1351 | } |
1295 | ret = insert_orphan_item(trans, root, ino); | 1352 | ret = insert_orphan_item(trans, root, ino); |
1296 | BUG_ON(ret); | ||
1297 | } | 1353 | } |
1298 | 1354 | ||
1299 | out: | 1355 | out: |
@@ -1338,9 +1394,9 @@ static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans, | |||
1338 | return -EIO; | 1394 | return -EIO; |
1339 | 1395 | ||
1340 | ret = fixup_inode_link_count(trans, root, inode); | 1396 | ret = fixup_inode_link_count(trans, root, inode); |
1341 | BUG_ON(ret); | ||
1342 | |||
1343 | iput(inode); | 1397 | iput(inode); |
1398 | if (ret) | ||
1399 | goto out; | ||
1344 | 1400 | ||
1345 | /* | 1401 | /* |
1346 | * fixup on a directory may create new entries, | 1402 | * fixup on a directory may create new entries, |
@@ -1390,7 +1446,7 @@ static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans, | |||
1390 | } else if (ret == -EEXIST) { | 1446 | } else if (ret == -EEXIST) { |
1391 | ret = 0; | 1447 | ret = 0; |
1392 | } else { | 1448 | } else { |
1393 | BUG(); | 1449 | BUG(); /* Logic Error */ |
1394 | } | 1450 | } |
1395 | iput(inode); | 1451 | iput(inode); |
1396 | 1452 | ||
@@ -1459,7 +1515,7 @@ static noinline int replay_one_name(struct btrfs_trans_handle *trans, | |||
1459 | struct inode *dir; | 1515 | struct inode *dir; |
1460 | u8 log_type; | 1516 | u8 log_type; |
1461 | int exists; | 1517 | int exists; |
1462 | int ret; | 1518 | int ret = 0; |
1463 | 1519 | ||
1464 | dir = read_one_inode(root, key->objectid); | 1520 | dir = read_one_inode(root, key->objectid); |
1465 | if (!dir) | 1521 | if (!dir) |
@@ -1491,7 +1547,9 @@ static noinline int replay_one_name(struct btrfs_trans_handle *trans, | |||
1491 | key->offset, name, | 1547 | key->offset, name, |
1492 | name_len, 1); | 1548 | name_len, 1); |
1493 | } else { | 1549 | } else { |
1494 | BUG(); | 1550 | /* Corruption */ |
1551 | ret = -EINVAL; | ||
1552 | goto out; | ||
1495 | } | 1553 | } |
1496 | if (IS_ERR_OR_NULL(dst_di)) { | 1554 | if (IS_ERR_OR_NULL(dst_di)) { |
1497 | /* we need a sequence number to insert, so we only | 1555 | /* we need a sequence number to insert, so we only |
@@ -1519,7 +1577,8 @@ static noinline int replay_one_name(struct btrfs_trans_handle *trans, | |||
1519 | goto out; | 1577 | goto out; |
1520 | 1578 | ||
1521 | ret = drop_one_dir_item(trans, root, path, dir, dst_di); | 1579 | ret = drop_one_dir_item(trans, root, path, dir, dst_di); |
1522 | BUG_ON(ret); | 1580 | if (ret) |
1581 | goto out; | ||
1523 | 1582 | ||
1524 | if (key->type == BTRFS_DIR_INDEX_KEY) | 1583 | if (key->type == BTRFS_DIR_INDEX_KEY) |
1525 | goto insert; | 1584 | goto insert; |
@@ -1527,14 +1586,15 @@ out: | |||
1527 | btrfs_release_path(path); | 1586 | btrfs_release_path(path); |
1528 | kfree(name); | 1587 | kfree(name); |
1529 | iput(dir); | 1588 | iput(dir); |
1530 | return 0; | 1589 | return ret; |
1531 | 1590 | ||
1532 | insert: | 1591 | insert: |
1533 | btrfs_release_path(path); | 1592 | btrfs_release_path(path); |
1534 | ret = insert_one_name(trans, root, path, key->objectid, key->offset, | 1593 | ret = insert_one_name(trans, root, path, key->objectid, key->offset, |
1535 | name, name_len, log_type, &log_key); | 1594 | name, name_len, log_type, &log_key); |
1536 | 1595 | if (ret && ret != -ENOENT) | |
1537 | BUG_ON(ret && ret != -ENOENT); | 1596 | goto out; |
1597 | ret = 0; | ||
1538 | goto out; | 1598 | goto out; |
1539 | } | 1599 | } |
1540 | 1600 | ||
@@ -1565,7 +1625,8 @@ static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans, | |||
1565 | return -EIO; | 1625 | return -EIO; |
1566 | name_len = btrfs_dir_name_len(eb, di); | 1626 | name_len = btrfs_dir_name_len(eb, di); |
1567 | ret = replay_one_name(trans, root, path, eb, di, key); | 1627 | ret = replay_one_name(trans, root, path, eb, di, key); |
1568 | BUG_ON(ret); | 1628 | if (ret) |
1629 | return ret; | ||
1569 | ptr = (unsigned long)(di + 1); | 1630 | ptr = (unsigned long)(di + 1); |
1570 | ptr += name_len; | 1631 | ptr += name_len; |
1571 | } | 1632 | } |
@@ -1726,16 +1787,21 @@ again: | |||
1726 | 1787 | ||
1727 | ret = link_to_fixup_dir(trans, root, | 1788 | ret = link_to_fixup_dir(trans, root, |
1728 | path, location.objectid); | 1789 | path, location.objectid); |
1729 | BUG_ON(ret); | 1790 | if (ret) { |
1791 | kfree(name); | ||
1792 | iput(inode); | ||
1793 | goto out; | ||
1794 | } | ||
1795 | |||
1730 | btrfs_inc_nlink(inode); | 1796 | btrfs_inc_nlink(inode); |
1731 | ret = btrfs_unlink_inode(trans, root, dir, inode, | 1797 | ret = btrfs_unlink_inode(trans, root, dir, inode, |
1732 | name, name_len); | 1798 | name, name_len); |
1733 | BUG_ON(ret); | 1799 | if (!ret) |
1734 | 1800 | btrfs_run_delayed_items(trans, root); | |
1735 | btrfs_run_delayed_items(trans, root); | ||
1736 | |||
1737 | kfree(name); | 1801 | kfree(name); |
1738 | iput(inode); | 1802 | iput(inode); |
1803 | if (ret) | ||
1804 | goto out; | ||
1739 | 1805 | ||
1740 | /* there might still be more names under this key | 1806 | /* there might still be more names under this key |
1741 | * check and repeat if required | 1807 | * check and repeat if required |
@@ -1839,7 +1905,8 @@ again: | |||
1839 | ret = check_item_in_log(trans, root, log, path, | 1905 | ret = check_item_in_log(trans, root, log, path, |
1840 | log_path, dir, | 1906 | log_path, dir, |
1841 | &found_key); | 1907 | &found_key); |
1842 | BUG_ON(ret); | 1908 | if (ret) |
1909 | goto out; | ||
1843 | if (found_key.offset == (u64)-1) | 1910 | if (found_key.offset == (u64)-1) |
1844 | break; | 1911 | break; |
1845 | dir_key.offset = found_key.offset + 1; | 1912 | dir_key.offset = found_key.offset + 1; |
@@ -1916,11 +1983,13 @@ static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb, | |||
1916 | if (S_ISDIR(mode)) { | 1983 | if (S_ISDIR(mode)) { |
1917 | ret = replay_dir_deletes(wc->trans, | 1984 | ret = replay_dir_deletes(wc->trans, |
1918 | root, log, path, key.objectid, 0); | 1985 | root, log, path, key.objectid, 0); |
1919 | BUG_ON(ret); | 1986 | if (ret) |
1987 | break; | ||
1920 | } | 1988 | } |
1921 | ret = overwrite_item(wc->trans, root, path, | 1989 | ret = overwrite_item(wc->trans, root, path, |
1922 | eb, i, &key); | 1990 | eb, i, &key); |
1923 | BUG_ON(ret); | 1991 | if (ret) |
1992 | break; | ||
1924 | 1993 | ||
1925 | /* for regular files, make sure corresponding | 1994 | /* for regular files, make sure corresponding |
1926 | * orhpan item exist. extents past the new EOF | 1995 | * orhpan item exist. extents past the new EOF |
@@ -1929,12 +1998,14 @@ static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb, | |||
1929 | if (S_ISREG(mode)) { | 1998 | if (S_ISREG(mode)) { |
1930 | ret = insert_orphan_item(wc->trans, root, | 1999 | ret = insert_orphan_item(wc->trans, root, |
1931 | key.objectid); | 2000 | key.objectid); |
1932 | BUG_ON(ret); | 2001 | if (ret) |
2002 | break; | ||
1933 | } | 2003 | } |
1934 | 2004 | ||
1935 | ret = link_to_fixup_dir(wc->trans, root, | 2005 | ret = link_to_fixup_dir(wc->trans, root, |
1936 | path, key.objectid); | 2006 | path, key.objectid); |
1937 | BUG_ON(ret); | 2007 | if (ret) |
2008 | break; | ||
1938 | } | 2009 | } |
1939 | if (wc->stage < LOG_WALK_REPLAY_ALL) | 2010 | if (wc->stage < LOG_WALK_REPLAY_ALL) |
1940 | continue; | 2011 | continue; |
@@ -1943,28 +2014,35 @@ static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb, | |||
1943 | if (key.type == BTRFS_XATTR_ITEM_KEY) { | 2014 | if (key.type == BTRFS_XATTR_ITEM_KEY) { |
1944 | ret = overwrite_item(wc->trans, root, path, | 2015 | ret = overwrite_item(wc->trans, root, path, |
1945 | eb, i, &key); | 2016 | eb, i, &key); |
1946 | BUG_ON(ret); | 2017 | if (ret) |
2018 | break; | ||
1947 | } else if (key.type == BTRFS_INODE_REF_KEY) { | 2019 | } else if (key.type == BTRFS_INODE_REF_KEY) { |
1948 | ret = add_inode_ref(wc->trans, root, log, path, | 2020 | ret = add_inode_ref(wc->trans, root, log, path, |
1949 | eb, i, &key); | 2021 | eb, i, &key); |
1950 | BUG_ON(ret && ret != -ENOENT); | 2022 | if (ret && ret != -ENOENT) |
2023 | break; | ||
2024 | ret = 0; | ||
1951 | } else if (key.type == BTRFS_INODE_EXTREF_KEY) { | 2025 | } else if (key.type == BTRFS_INODE_EXTREF_KEY) { |
1952 | ret = add_inode_ref(wc->trans, root, log, path, | 2026 | ret = add_inode_ref(wc->trans, root, log, path, |
1953 | eb, i, &key); | 2027 | eb, i, &key); |
1954 | BUG_ON(ret && ret != -ENOENT); | 2028 | if (ret && ret != -ENOENT) |
2029 | break; | ||
2030 | ret = 0; | ||
1955 | } else if (key.type == BTRFS_EXTENT_DATA_KEY) { | 2031 | } else if (key.type == BTRFS_EXTENT_DATA_KEY) { |
1956 | ret = replay_one_extent(wc->trans, root, path, | 2032 | ret = replay_one_extent(wc->trans, root, path, |
1957 | eb, i, &key); | 2033 | eb, i, &key); |
1958 | BUG_ON(ret); | 2034 | if (ret) |
2035 | break; | ||
1959 | } else if (key.type == BTRFS_DIR_ITEM_KEY || | 2036 | } else if (key.type == BTRFS_DIR_ITEM_KEY || |
1960 | key.type == BTRFS_DIR_INDEX_KEY) { | 2037 | key.type == BTRFS_DIR_INDEX_KEY) { |
1961 | ret = replay_one_dir_item(wc->trans, root, path, | 2038 | ret = replay_one_dir_item(wc->trans, root, path, |
1962 | eb, i, &key); | 2039 | eb, i, &key); |
1963 | BUG_ON(ret); | 2040 | if (ret) |
2041 | break; | ||
1964 | } | 2042 | } |
1965 | } | 2043 | } |
1966 | btrfs_free_path(path); | 2044 | btrfs_free_path(path); |
1967 | return 0; | 2045 | return ret; |
1968 | } | 2046 | } |
1969 | 2047 | ||
1970 | static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans, | 2048 | static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans, |
@@ -2009,8 +2087,10 @@ static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans, | |||
2009 | 2087 | ||
2010 | if (*level == 1) { | 2088 | if (*level == 1) { |
2011 | ret = wc->process_func(root, next, wc, ptr_gen); | 2089 | ret = wc->process_func(root, next, wc, ptr_gen); |
2012 | if (ret) | 2090 | if (ret) { |
2091 | free_extent_buffer(next); | ||
2013 | return ret; | 2092 | return ret; |
2093 | } | ||
2014 | 2094 | ||
2015 | path->slots[*level]++; | 2095 | path->slots[*level]++; |
2016 | if (wc->free) { | 2096 | if (wc->free) { |
@@ -2030,7 +2110,10 @@ static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans, | |||
2030 | BTRFS_TREE_LOG_OBJECTID); | 2110 | BTRFS_TREE_LOG_OBJECTID); |
2031 | ret = btrfs_free_and_pin_reserved_extent(root, | 2111 | ret = btrfs_free_and_pin_reserved_extent(root, |
2032 | bytenr, blocksize); | 2112 | bytenr, blocksize); |
2033 | BUG_ON(ret); /* -ENOMEM or logic errors */ | 2113 | if (ret) { |
2114 | free_extent_buffer(next); | ||
2115 | return ret; | ||
2116 | } | ||
2034 | } | 2117 | } |
2035 | free_extent_buffer(next); | 2118 | free_extent_buffer(next); |
2036 | continue; | 2119 | continue; |
@@ -2103,7 +2186,8 @@ static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans, | |||
2103 | ret = btrfs_free_and_pin_reserved_extent(root, | 2186 | ret = btrfs_free_and_pin_reserved_extent(root, |
2104 | path->nodes[*level]->start, | 2187 | path->nodes[*level]->start, |
2105 | path->nodes[*level]->len); | 2188 | path->nodes[*level]->len); |
2106 | BUG_ON(ret); | 2189 | if (ret) |
2190 | return ret; | ||
2107 | } | 2191 | } |
2108 | free_extent_buffer(path->nodes[*level]); | 2192 | free_extent_buffer(path->nodes[*level]); |
2109 | path->nodes[*level] = NULL; | 2193 | path->nodes[*level] = NULL; |
@@ -2125,7 +2209,6 @@ static int walk_log_tree(struct btrfs_trans_handle *trans, | |||
2125 | int wret; | 2209 | int wret; |
2126 | int level; | 2210 | int level; |
2127 | struct btrfs_path *path; | 2211 | struct btrfs_path *path; |
2128 | int i; | ||
2129 | int orig_level; | 2212 | int orig_level; |
2130 | 2213 | ||
2131 | path = btrfs_alloc_path(); | 2214 | path = btrfs_alloc_path(); |
@@ -2177,17 +2260,12 @@ static int walk_log_tree(struct btrfs_trans_handle *trans, | |||
2177 | BTRFS_TREE_LOG_OBJECTID); | 2260 | BTRFS_TREE_LOG_OBJECTID); |
2178 | ret = btrfs_free_and_pin_reserved_extent(log, next->start, | 2261 | ret = btrfs_free_and_pin_reserved_extent(log, next->start, |
2179 | next->len); | 2262 | next->len); |
2180 | BUG_ON(ret); /* -ENOMEM or logic errors */ | 2263 | if (ret) |
2264 | goto out; | ||
2181 | } | 2265 | } |
2182 | } | 2266 | } |
2183 | 2267 | ||
2184 | out: | 2268 | out: |
2185 | for (i = 0; i <= orig_level; i++) { | ||
2186 | if (path->nodes[i]) { | ||
2187 | free_extent_buffer(path->nodes[i]); | ||
2188 | path->nodes[i] = NULL; | ||
2189 | } | ||
2190 | } | ||
2191 | btrfs_free_path(path); | 2269 | btrfs_free_path(path); |
2192 | return ret; | 2270 | return ret; |
2193 | } | 2271 | } |
@@ -2471,7 +2549,10 @@ static void free_log_tree(struct btrfs_trans_handle *trans, | |||
2471 | 2549 | ||
2472 | if (trans) { | 2550 | if (trans) { |
2473 | ret = walk_log_tree(trans, log, &wc); | 2551 | ret = walk_log_tree(trans, log, &wc); |
2474 | BUG_ON(ret); | 2552 | |
2553 | /* I don't think this can happen but just in case */ | ||
2554 | if (ret) | ||
2555 | btrfs_abort_transaction(trans, log, ret); | ||
2475 | } | 2556 | } |
2476 | 2557 | ||
2477 | while (1) { | 2558 | while (1) { |
@@ -2579,7 +2660,10 @@ int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans, | |||
2579 | if (di) { | 2660 | if (di) { |
2580 | ret = btrfs_delete_one_dir_name(trans, log, path, di); | 2661 | ret = btrfs_delete_one_dir_name(trans, log, path, di); |
2581 | bytes_del += name_len; | 2662 | bytes_del += name_len; |
2582 | BUG_ON(ret); | 2663 | if (ret) { |
2664 | err = ret; | ||
2665 | goto fail; | ||
2666 | } | ||
2583 | } | 2667 | } |
2584 | btrfs_release_path(path); | 2668 | btrfs_release_path(path); |
2585 | di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino, | 2669 | di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino, |
@@ -2591,7 +2675,10 @@ int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans, | |||
2591 | if (di) { | 2675 | if (di) { |
2592 | ret = btrfs_delete_one_dir_name(trans, log, path, di); | 2676 | ret = btrfs_delete_one_dir_name(trans, log, path, di); |
2593 | bytes_del += name_len; | 2677 | bytes_del += name_len; |
2594 | BUG_ON(ret); | 2678 | if (ret) { |
2679 | err = ret; | ||
2680 | goto fail; | ||
2681 | } | ||
2595 | } | 2682 | } |
2596 | 2683 | ||
2597 | /* update the directory size in the log to reflect the names | 2684 | /* update the directory size in the log to reflect the names |
@@ -2930,7 +3017,7 @@ static int drop_objectid_items(struct btrfs_trans_handle *trans, | |||
2930 | 3017 | ||
2931 | while (1) { | 3018 | while (1) { |
2932 | ret = btrfs_search_slot(trans, log, &key, path, -1, 1); | 3019 | ret = btrfs_search_slot(trans, log, &key, path, -1, 1); |
2933 | BUG_ON(ret == 0); | 3020 | BUG_ON(ret == 0); /* Logic error */ |
2934 | if (ret < 0) | 3021 | if (ret < 0) |
2935 | break; | 3022 | break; |
2936 | 3023 | ||
@@ -3133,7 +3220,11 @@ static noinline int copy_items(struct btrfs_trans_handle *trans, | |||
3133 | log->fs_info->csum_root, | 3220 | log->fs_info->csum_root, |
3134 | ds + cs, ds + cs + cl - 1, | 3221 | ds + cs, ds + cs + cl - 1, |
3135 | &ordered_sums, 0); | 3222 | &ordered_sums, 0); |
3136 | BUG_ON(ret); | 3223 | if (ret) { |
3224 | btrfs_release_path(dst_path); | ||
3225 | kfree(ins_data); | ||
3226 | return ret; | ||
3227 | } | ||
3137 | } | 3228 | } |
3138 | } | 3229 | } |
3139 | } | 3230 | } |
@@ -3173,115 +3264,6 @@ static int extent_cmp(void *priv, struct list_head *a, struct list_head *b) | |||
3173 | return 0; | 3264 | return 0; |
3174 | } | 3265 | } |
3175 | 3266 | ||
3176 | static int drop_adjacent_extents(struct btrfs_trans_handle *trans, | ||
3177 | struct btrfs_root *root, struct inode *inode, | ||
3178 | struct extent_map *em, | ||
3179 | struct btrfs_path *path) | ||
3180 | { | ||
3181 | struct btrfs_file_extent_item *fi; | ||
3182 | struct extent_buffer *leaf; | ||
3183 | struct btrfs_key key, new_key; | ||
3184 | struct btrfs_map_token token; | ||
3185 | u64 extent_end; | ||
3186 | u64 extent_offset = 0; | ||
3187 | int extent_type; | ||
3188 | int del_slot = 0; | ||
3189 | int del_nr = 0; | ||
3190 | int ret = 0; | ||
3191 | |||
3192 | while (1) { | ||
3193 | btrfs_init_map_token(&token); | ||
3194 | leaf = path->nodes[0]; | ||
3195 | path->slots[0]++; | ||
3196 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { | ||
3197 | if (del_nr) { | ||
3198 | ret = btrfs_del_items(trans, root, path, | ||
3199 | del_slot, del_nr); | ||
3200 | if (ret) | ||
3201 | return ret; | ||
3202 | del_nr = 0; | ||
3203 | } | ||
3204 | |||
3205 | ret = btrfs_next_leaf_write(trans, root, path, 1); | ||
3206 | if (ret < 0) | ||
3207 | return ret; | ||
3208 | if (ret > 0) | ||
3209 | return 0; | ||
3210 | leaf = path->nodes[0]; | ||
3211 | } | ||
3212 | |||
3213 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | ||
3214 | if (key.objectid != btrfs_ino(inode) || | ||
3215 | key.type != BTRFS_EXTENT_DATA_KEY || | ||
3216 | key.offset >= em->start + em->len) | ||
3217 | break; | ||
3218 | |||
3219 | fi = btrfs_item_ptr(leaf, path->slots[0], | ||
3220 | struct btrfs_file_extent_item); | ||
3221 | extent_type = btrfs_token_file_extent_type(leaf, fi, &token); | ||
3222 | if (extent_type == BTRFS_FILE_EXTENT_REG || | ||
3223 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { | ||
3224 | extent_offset = btrfs_token_file_extent_offset(leaf, | ||
3225 | fi, &token); | ||
3226 | extent_end = key.offset + | ||
3227 | btrfs_token_file_extent_num_bytes(leaf, fi, | ||
3228 | &token); | ||
3229 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { | ||
3230 | extent_end = key.offset + | ||
3231 | btrfs_file_extent_inline_len(leaf, fi); | ||
3232 | } else { | ||
3233 | BUG(); | ||
3234 | } | ||
3235 | |||
3236 | if (extent_end <= em->len + em->start) { | ||
3237 | if (!del_nr) { | ||
3238 | del_slot = path->slots[0]; | ||
3239 | } | ||
3240 | del_nr++; | ||
3241 | continue; | ||
3242 | } | ||
3243 | |||
3244 | /* | ||
3245 | * Ok so we'll ignore previous items if we log a new extent, | ||
3246 | * which can lead to overlapping extents, so if we have an | ||
3247 | * existing extent we want to adjust we _have_ to check the next | ||
3248 | * guy to make sure we even need this extent anymore, this keeps | ||
3249 | * us from panicing in set_item_key_safe. | ||
3250 | */ | ||
3251 | if (path->slots[0] < btrfs_header_nritems(leaf) - 1) { | ||
3252 | struct btrfs_key tmp_key; | ||
3253 | |||
3254 | btrfs_item_key_to_cpu(leaf, &tmp_key, | ||
3255 | path->slots[0] + 1); | ||
3256 | if (tmp_key.objectid == btrfs_ino(inode) && | ||
3257 | tmp_key.type == BTRFS_EXTENT_DATA_KEY && | ||
3258 | tmp_key.offset <= em->start + em->len) { | ||
3259 | if (!del_nr) | ||
3260 | del_slot = path->slots[0]; | ||
3261 | del_nr++; | ||
3262 | continue; | ||
3263 | } | ||
3264 | } | ||
3265 | |||
3266 | BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE); | ||
3267 | memcpy(&new_key, &key, sizeof(new_key)); | ||
3268 | new_key.offset = em->start + em->len; | ||
3269 | btrfs_set_item_key_safe(trans, root, path, &new_key); | ||
3270 | extent_offset += em->start + em->len - key.offset; | ||
3271 | btrfs_set_token_file_extent_offset(leaf, fi, extent_offset, | ||
3272 | &token); | ||
3273 | btrfs_set_token_file_extent_num_bytes(leaf, fi, extent_end - | ||
3274 | (em->start + em->len), | ||
3275 | &token); | ||
3276 | btrfs_mark_buffer_dirty(leaf); | ||
3277 | } | ||
3278 | |||
3279 | if (del_nr) | ||
3280 | ret = btrfs_del_items(trans, root, path, del_slot, del_nr); | ||
3281 | |||
3282 | return ret; | ||
3283 | } | ||
3284 | |||
3285 | static int log_one_extent(struct btrfs_trans_handle *trans, | 3267 | static int log_one_extent(struct btrfs_trans_handle *trans, |
3286 | struct inode *inode, struct btrfs_root *root, | 3268 | struct inode *inode, struct btrfs_root *root, |
3287 | struct extent_map *em, struct btrfs_path *path) | 3269 | struct extent_map *em, struct btrfs_path *path) |
@@ -3303,39 +3285,24 @@ static int log_one_extent(struct btrfs_trans_handle *trans, | |||
3303 | int index = log->log_transid % 2; | 3285 | int index = log->log_transid % 2; |
3304 | bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; | 3286 | bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
3305 | 3287 | ||
3306 | insert: | 3288 | ret = __btrfs_drop_extents(trans, log, inode, path, em->start, |
3289 | em->start + em->len, NULL, 0); | ||
3290 | if (ret) | ||
3291 | return ret; | ||
3292 | |||
3307 | INIT_LIST_HEAD(&ordered_sums); | 3293 | INIT_LIST_HEAD(&ordered_sums); |
3308 | btrfs_init_map_token(&token); | 3294 | btrfs_init_map_token(&token); |
3309 | key.objectid = btrfs_ino(inode); | 3295 | key.objectid = btrfs_ino(inode); |
3310 | key.type = BTRFS_EXTENT_DATA_KEY; | 3296 | key.type = BTRFS_EXTENT_DATA_KEY; |
3311 | key.offset = em->start; | 3297 | key.offset = em->start; |
3312 | path->really_keep_locks = 1; | ||
3313 | 3298 | ||
3314 | ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*fi)); | 3299 | ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*fi)); |
3315 | if (ret && ret != -EEXIST) { | 3300 | if (ret) |
3316 | path->really_keep_locks = 0; | ||
3317 | return ret; | 3301 | return ret; |
3318 | } | ||
3319 | leaf = path->nodes[0]; | 3302 | leaf = path->nodes[0]; |
3320 | fi = btrfs_item_ptr(leaf, path->slots[0], | 3303 | fi = btrfs_item_ptr(leaf, path->slots[0], |
3321 | struct btrfs_file_extent_item); | 3304 | struct btrfs_file_extent_item); |
3322 | 3305 | ||
3323 | /* | ||
3324 | * If we are overwriting an inline extent with a real one then we need | ||
3325 | * to just delete the inline extent as it may not be large enough to | ||
3326 | * have the entire file_extent_item. | ||
3327 | */ | ||
3328 | if (ret && btrfs_token_file_extent_type(leaf, fi, &token) == | ||
3329 | BTRFS_FILE_EXTENT_INLINE) { | ||
3330 | ret = btrfs_del_item(trans, log, path); | ||
3331 | btrfs_release_path(path); | ||
3332 | if (ret) { | ||
3333 | path->really_keep_locks = 0; | ||
3334 | return ret; | ||
3335 | } | ||
3336 | goto insert; | ||
3337 | } | ||
3338 | |||
3339 | btrfs_set_token_file_extent_generation(leaf, fi, em->generation, | 3306 | btrfs_set_token_file_extent_generation(leaf, fi, em->generation, |
3340 | &token); | 3307 | &token); |
3341 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { | 3308 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { |
@@ -3374,22 +3341,14 @@ insert: | |||
3374 | em->start - em->orig_start, | 3341 | em->start - em->orig_start, |
3375 | &token); | 3342 | &token); |
3376 | btrfs_set_token_file_extent_num_bytes(leaf, fi, em->len, &token); | 3343 | btrfs_set_token_file_extent_num_bytes(leaf, fi, em->len, &token); |
3377 | btrfs_set_token_file_extent_ram_bytes(leaf, fi, em->len, &token); | 3344 | btrfs_set_token_file_extent_ram_bytes(leaf, fi, em->ram_bytes, &token); |
3378 | btrfs_set_token_file_extent_compression(leaf, fi, em->compress_type, | 3345 | btrfs_set_token_file_extent_compression(leaf, fi, em->compress_type, |
3379 | &token); | 3346 | &token); |
3380 | btrfs_set_token_file_extent_encryption(leaf, fi, 0, &token); | 3347 | btrfs_set_token_file_extent_encryption(leaf, fi, 0, &token); |
3381 | btrfs_set_token_file_extent_other_encoding(leaf, fi, 0, &token); | 3348 | btrfs_set_token_file_extent_other_encoding(leaf, fi, 0, &token); |
3382 | btrfs_mark_buffer_dirty(leaf); | 3349 | btrfs_mark_buffer_dirty(leaf); |
3383 | 3350 | ||
3384 | /* | ||
3385 | * Have to check the extent to the right of us to make sure it doesn't | ||
3386 | * fall in our current range. We're ok if the previous extent is in our | ||
3387 | * range since the recovery stuff will run us in key order and thus just | ||
3388 | * drop the part we overwrote. | ||
3389 | */ | ||
3390 | ret = drop_adjacent_extents(trans, log, inode, em, path); | ||
3391 | btrfs_release_path(path); | 3351 | btrfs_release_path(path); |
3392 | path->really_keep_locks = 0; | ||
3393 | if (ret) { | 3352 | if (ret) { |
3394 | return ret; | 3353 | return ret; |
3395 | } | 3354 | } |
@@ -3614,8 +3573,6 @@ static int btrfs_log_inode(struct btrfs_trans_handle *trans, | |||
3614 | bool fast_search = false; | 3573 | bool fast_search = false; |
3615 | u64 ino = btrfs_ino(inode); | 3574 | u64 ino = btrfs_ino(inode); |
3616 | 3575 | ||
3617 | log = root->log_root; | ||
3618 | |||
3619 | path = btrfs_alloc_path(); | 3576 | path = btrfs_alloc_path(); |
3620 | if (!path) | 3577 | if (!path) |
3621 | return -ENOMEM; | 3578 | return -ENOMEM; |
@@ -3882,9 +3839,9 @@ out: | |||
3882 | * only logging is done of any parent directories that are older than | 3839 | * only logging is done of any parent directories that are older than |
3883 | * the last committed transaction | 3840 | * the last committed transaction |
3884 | */ | 3841 | */ |
3885 | int btrfs_log_inode_parent(struct btrfs_trans_handle *trans, | 3842 | static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans, |
3886 | struct btrfs_root *root, struct inode *inode, | 3843 | struct btrfs_root *root, struct inode *inode, |
3887 | struct dentry *parent, int exists_only) | 3844 | struct dentry *parent, int exists_only) |
3888 | { | 3845 | { |
3889 | int inode_only = exists_only ? LOG_INODE_EXISTS : LOG_INODE_ALL; | 3846 | int inode_only = exists_only ? LOG_INODE_EXISTS : LOG_INODE_ALL; |
3890 | struct super_block *sb; | 3847 | struct super_block *sb; |
@@ -4075,6 +4032,9 @@ again: | |||
4075 | wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key); | 4032 | wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key); |
4076 | if (IS_ERR(wc.replay_dest)) { | 4033 | if (IS_ERR(wc.replay_dest)) { |
4077 | ret = PTR_ERR(wc.replay_dest); | 4034 | ret = PTR_ERR(wc.replay_dest); |
4035 | free_extent_buffer(log->node); | ||
4036 | free_extent_buffer(log->commit_root); | ||
4037 | kfree(log); | ||
4078 | btrfs_error(fs_info, ret, "Couldn't read target root " | 4038 | btrfs_error(fs_info, ret, "Couldn't read target root " |
4079 | "for tree log recovery."); | 4039 | "for tree log recovery."); |
4080 | goto error; | 4040 | goto error; |
@@ -4083,12 +4043,10 @@ again: | |||
4083 | wc.replay_dest->log_root = log; | 4043 | wc.replay_dest->log_root = log; |
4084 | btrfs_record_root_in_trans(trans, wc.replay_dest); | 4044 | btrfs_record_root_in_trans(trans, wc.replay_dest); |
4085 | ret = walk_log_tree(trans, log, &wc); | 4045 | ret = walk_log_tree(trans, log, &wc); |
4086 | BUG_ON(ret); | ||
4087 | 4046 | ||
4088 | if (wc.stage == LOG_WALK_REPLAY_ALL) { | 4047 | if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) { |
4089 | ret = fixup_inode_link_counts(trans, wc.replay_dest, | 4048 | ret = fixup_inode_link_counts(trans, wc.replay_dest, |
4090 | path); | 4049 | path); |
4091 | BUG_ON(ret); | ||
4092 | } | 4050 | } |
4093 | 4051 | ||
4094 | key.offset = found_key.offset - 1; | 4052 | key.offset = found_key.offset - 1; |
@@ -4097,6 +4055,9 @@ again: | |||
4097 | free_extent_buffer(log->commit_root); | 4055 | free_extent_buffer(log->commit_root); |
4098 | kfree(log); | 4056 | kfree(log); |
4099 | 4057 | ||
4058 | if (ret) | ||
4059 | goto error; | ||
4060 | |||
4100 | if (found_key.offset == 0) | 4061 | if (found_key.offset == 0) |
4101 | break; | 4062 | break; |
4102 | } | 4063 | } |
@@ -4117,17 +4078,20 @@ again: | |||
4117 | 4078 | ||
4118 | btrfs_free_path(path); | 4079 | btrfs_free_path(path); |
4119 | 4080 | ||
4081 | /* step 4: commit the transaction, which also unpins the blocks */ | ||
4082 | ret = btrfs_commit_transaction(trans, fs_info->tree_root); | ||
4083 | if (ret) | ||
4084 | return ret; | ||
4085 | |||
4120 | free_extent_buffer(log_root_tree->node); | 4086 | free_extent_buffer(log_root_tree->node); |
4121 | log_root_tree->log_root = NULL; | 4087 | log_root_tree->log_root = NULL; |
4122 | fs_info->log_root_recovering = 0; | 4088 | fs_info->log_root_recovering = 0; |
4123 | |||
4124 | /* step 4: commit the transaction, which also unpins the blocks */ | ||
4125 | btrfs_commit_transaction(trans, fs_info->tree_root); | ||
4126 | |||
4127 | kfree(log_root_tree); | 4089 | kfree(log_root_tree); |
4128 | return 0; | ||
4129 | 4090 | ||
4091 | return 0; | ||
4130 | error: | 4092 | error: |
4093 | if (wc.trans) | ||
4094 | btrfs_end_transaction(wc.trans, fs_info->tree_root); | ||
4131 | btrfs_free_path(path); | 4095 | btrfs_free_path(path); |
4132 | return ret; | 4096 | return ret; |
4133 | } | 4097 | } |