aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJeff Mahoney <jeffm@suse.com>2012-03-12 11:03:00 -0400
committerDavid Sterba <dsterba@suse.cz>2012-03-22 06:52:54 -0400
commit79787eaab46121d4713ed03c8fc63b9ec3eaec76 (patch)
treeee6b17d0811ee54ab74a03aa4e0bb92769d2f12a
parent49b25e0540904be0bf558b84475c69d72e4de66e (diff)
btrfs: replace many BUG_ONs with proper error handling
btrfs currently handles most errors with BUG_ON. This patch is a work-in- progress but aims to handle most errors other than internal logic errors and ENOMEM more gracefully. This iteration prevents most crashes but can run into lockups with the page lock on occasion when the timing "works out." Signed-off-by: Jeff Mahoney <jeffm@suse.com>
-rw-r--r--fs/btrfs/compression.c24
-rw-r--r--fs/btrfs/ctree.c19
-rw-r--r--fs/btrfs/delayed-inode.c27
-rw-r--r--fs/btrfs/dir-item.c1
-rw-r--r--fs/btrfs/disk-io.c116
-rw-r--r--fs/btrfs/export.c2
-rw-r--r--fs/btrfs/extent-tree.c316
-rw-r--r--fs/btrfs/extent_io.c40
-rw-r--r--fs/btrfs/file-item.c13
-rw-r--r--fs/btrfs/file.c45
-rw-r--r--fs/btrfs/free-space-cache.c13
-rw-r--r--fs/btrfs/inode-item.c1
-rw-r--r--fs/btrfs/inode-map.c19
-rw-r--r--fs/btrfs/inode.c378
-rw-r--r--fs/btrfs/ioctl.c79
-rw-r--r--fs/btrfs/orphan.c2
-rw-r--r--fs/btrfs/relocation.c35
-rw-r--r--fs/btrfs/root-tree.c12
-rw-r--r--fs/btrfs/scrub.c3
-rw-r--r--fs/btrfs/super.c2
-rw-r--r--fs/btrfs/transaction.c36
-rw-r--r--fs/btrfs/tree-log.c79
-rw-r--r--fs/btrfs/volumes.c103
23 files changed, 980 insertions, 385 deletions
diff --git a/fs/btrfs/compression.c b/fs/btrfs/compression.c
index cfd158dccdec..d11afa67c7d8 100644
--- a/fs/btrfs/compression.c
+++ b/fs/btrfs/compression.c
@@ -391,16 +391,16 @@ int btrfs_submit_compressed_write(struct inode *inode, u64 start,
391 */ 391 */
392 atomic_inc(&cb->pending_bios); 392 atomic_inc(&cb->pending_bios);
393 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0); 393 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
394 BUG_ON(ret); 394 BUG_ON(ret); /* -ENOMEM */
395 395
396 if (!skip_sum) { 396 if (!skip_sum) {
397 ret = btrfs_csum_one_bio(root, inode, bio, 397 ret = btrfs_csum_one_bio(root, inode, bio,
398 start, 1); 398 start, 1);
399 BUG_ON(ret); 399 BUG_ON(ret); /* -ENOMEM */
400 } 400 }
401 401
402 ret = btrfs_map_bio(root, WRITE, bio, 0, 1); 402 ret = btrfs_map_bio(root, WRITE, bio, 0, 1);
403 BUG_ON(ret); 403 BUG_ON(ret); /* -ENOMEM */
404 404
405 bio_put(bio); 405 bio_put(bio);
406 406
@@ -420,15 +420,15 @@ int btrfs_submit_compressed_write(struct inode *inode, u64 start,
420 bio_get(bio); 420 bio_get(bio);
421 421
422 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0); 422 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
423 BUG_ON(ret); 423 BUG_ON(ret); /* -ENOMEM */
424 424
425 if (!skip_sum) { 425 if (!skip_sum) {
426 ret = btrfs_csum_one_bio(root, inode, bio, start, 1); 426 ret = btrfs_csum_one_bio(root, inode, bio, start, 1);
427 BUG_ON(ret); 427 BUG_ON(ret); /* -ENOMEM */
428 } 428 }
429 429
430 ret = btrfs_map_bio(root, WRITE, bio, 0, 1); 430 ret = btrfs_map_bio(root, WRITE, bio, 0, 1);
431 BUG_ON(ret); 431 BUG_ON(ret); /* -ENOMEM */
432 432
433 bio_put(bio); 433 bio_put(bio);
434 return 0; 434 return 0;
@@ -661,7 +661,7 @@ int btrfs_submit_compressed_read(struct inode *inode, struct bio *bio,
661 bio_get(comp_bio); 661 bio_get(comp_bio);
662 662
663 ret = btrfs_bio_wq_end_io(root->fs_info, comp_bio, 0); 663 ret = btrfs_bio_wq_end_io(root->fs_info, comp_bio, 0);
664 BUG_ON(ret); 664 BUG_ON(ret); /* -ENOMEM */
665 665
666 /* 666 /*
667 * inc the count before we submit the bio so 667 * inc the count before we submit the bio so
@@ -674,14 +674,14 @@ int btrfs_submit_compressed_read(struct inode *inode, struct bio *bio,
674 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) { 674 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
675 ret = btrfs_lookup_bio_sums(root, inode, 675 ret = btrfs_lookup_bio_sums(root, inode,
676 comp_bio, sums); 676 comp_bio, sums);
677 BUG_ON(ret); 677 BUG_ON(ret); /* -ENOMEM */
678 } 678 }
679 sums += (comp_bio->bi_size + root->sectorsize - 1) / 679 sums += (comp_bio->bi_size + root->sectorsize - 1) /
680 root->sectorsize; 680 root->sectorsize;
681 681
682 ret = btrfs_map_bio(root, READ, comp_bio, 682 ret = btrfs_map_bio(root, READ, comp_bio,
683 mirror_num, 0); 683 mirror_num, 0);
684 BUG_ON(ret); 684 BUG_ON(ret); /* -ENOMEM */
685 685
686 bio_put(comp_bio); 686 bio_put(comp_bio);
687 687
@@ -697,15 +697,15 @@ int btrfs_submit_compressed_read(struct inode *inode, struct bio *bio,
697 bio_get(comp_bio); 697 bio_get(comp_bio);
698 698
699 ret = btrfs_bio_wq_end_io(root->fs_info, comp_bio, 0); 699 ret = btrfs_bio_wq_end_io(root->fs_info, comp_bio, 0);
700 BUG_ON(ret); 700 BUG_ON(ret); /* -ENOMEM */
701 701
702 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) { 702 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
703 ret = btrfs_lookup_bio_sums(root, inode, comp_bio, sums); 703 ret = btrfs_lookup_bio_sums(root, inode, comp_bio, sums);
704 BUG_ON(ret); 704 BUG_ON(ret); /* -ENOMEM */
705 } 705 }
706 706
707 ret = btrfs_map_bio(root, READ, comp_bio, mirror_num, 0); 707 ret = btrfs_map_bio(root, READ, comp_bio, mirror_num, 0);
708 BUG_ON(ret); 708 BUG_ON(ret); /* -ENOMEM */
709 709
710 bio_put(comp_bio); 710 bio_put(comp_bio);
711 return 0; 711 return 0;
diff --git a/fs/btrfs/ctree.c b/fs/btrfs/ctree.c
index 651a26a6c651..e697afd18159 100644
--- a/fs/btrfs/ctree.c
+++ b/fs/btrfs/ctree.c
@@ -356,14 +356,14 @@ static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
356 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) && 356 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
357 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) { 357 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
358 ret = btrfs_inc_ref(trans, root, buf, 1, 1); 358 ret = btrfs_inc_ref(trans, root, buf, 1, 1);
359 BUG_ON(ret); 359 BUG_ON(ret); /* -ENOMEM */
360 360
361 if (root->root_key.objectid == 361 if (root->root_key.objectid ==
362 BTRFS_TREE_RELOC_OBJECTID) { 362 BTRFS_TREE_RELOC_OBJECTID) {
363 ret = btrfs_dec_ref(trans, root, buf, 0, 1); 363 ret = btrfs_dec_ref(trans, root, buf, 0, 1);
364 BUG_ON(ret); 364 BUG_ON(ret); /* -ENOMEM */
365 ret = btrfs_inc_ref(trans, root, cow, 1, 1); 365 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
366 BUG_ON(ret); 366 BUG_ON(ret); /* -ENOMEM */
367 } 367 }
368 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF; 368 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
369 } else { 369 } else {
@@ -373,7 +373,7 @@ static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
373 ret = btrfs_inc_ref(trans, root, cow, 1, 1); 373 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
374 else 374 else
375 ret = btrfs_inc_ref(trans, root, cow, 0, 1); 375 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
376 BUG_ON(ret); 376 BUG_ON(ret); /* -ENOMEM */
377 } 377 }
378 if (new_flags != 0) { 378 if (new_flags != 0) {
379 ret = btrfs_set_disk_extent_flags(trans, root, 379 ret = btrfs_set_disk_extent_flags(trans, root,
@@ -390,9 +390,9 @@ static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
390 ret = btrfs_inc_ref(trans, root, cow, 1, 1); 390 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
391 else 391 else
392 ret = btrfs_inc_ref(trans, root, cow, 0, 1); 392 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
393 BUG_ON(ret); 393 BUG_ON(ret); /* -ENOMEM */
394 ret = btrfs_dec_ref(trans, root, buf, 1, 1); 394 ret = btrfs_dec_ref(trans, root, buf, 1, 1);
395 BUG_ON(ret); 395 BUG_ON(ret); /* -ENOMEM */
396 } 396 }
397 clean_tree_block(trans, root, buf); 397 clean_tree_block(trans, root, buf);
398 *last_ref = 1; 398 *last_ref = 1;
@@ -475,7 +475,7 @@ static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
475 475
476 ret = update_ref_for_cow(trans, root, buf, cow, &last_ref); 476 ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
477 if (ret) { 477 if (ret) {
478 btrfs_std_error(root->fs_info, ret); 478 btrfs_abort_transaction(trans, root, ret);
479 return ret; 479 return ret;
480 } 480 }
481 481
@@ -2713,7 +2713,8 @@ static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
2713 path->nodes[1], slot - 1, &left); 2713 path->nodes[1], slot - 1, &left);
2714 if (ret) { 2714 if (ret) {
2715 /* we hit -ENOSPC, but it isn't fatal here */ 2715 /* we hit -ENOSPC, but it isn't fatal here */
2716 ret = 1; 2716 if (ret == -ENOSPC)
2717 ret = 1;
2717 goto out; 2718 goto out;
2718 } 2719 }
2719 2720
@@ -4017,7 +4018,7 @@ find_next_key:
4017 } 4018 }
4018 btrfs_set_path_blocking(path); 4019 btrfs_set_path_blocking(path);
4019 cur = read_node_slot(root, cur, slot); 4020 cur = read_node_slot(root, cur, slot);
4020 BUG_ON(!cur); 4021 BUG_ON(!cur); /* -ENOMEM */
4021 4022
4022 btrfs_tree_read_lock(cur); 4023 btrfs_tree_read_lock(cur);
4023 4024
diff --git a/fs/btrfs/delayed-inode.c b/fs/btrfs/delayed-inode.c
index 6829590d0fbb..03e3748d84d0 100644
--- a/fs/btrfs/delayed-inode.c
+++ b/fs/btrfs/delayed-inode.c
@@ -115,6 +115,7 @@ static struct btrfs_delayed_node *btrfs_get_delayed_node(struct inode *inode)
115 return NULL; 115 return NULL;
116} 116}
117 117
118/* Will return either the node or PTR_ERR(-ENOMEM) */
118static struct btrfs_delayed_node *btrfs_get_or_create_delayed_node( 119static struct btrfs_delayed_node *btrfs_get_or_create_delayed_node(
119 struct inode *inode) 120 struct inode *inode)
120{ 121{
@@ -1106,16 +1107,25 @@ static int btrfs_update_delayed_inode(struct btrfs_trans_handle *trans,
1106 return 0; 1107 return 0;
1107} 1108}
1108 1109
1109/* Called when committing the transaction. */ 1110/*
1111 * Called when committing the transaction.
1112 * Returns 0 on success.
1113 * Returns < 0 on error and returns with an aborted transaction with any
1114 * outstanding delayed items cleaned up.
1115 */
1110int btrfs_run_delayed_items(struct btrfs_trans_handle *trans, 1116int btrfs_run_delayed_items(struct btrfs_trans_handle *trans,
1111 struct btrfs_root *root) 1117 struct btrfs_root *root)
1112{ 1118{
1119 struct btrfs_root *curr_root = root;
1113 struct btrfs_delayed_root *delayed_root; 1120 struct btrfs_delayed_root *delayed_root;
1114 struct btrfs_delayed_node *curr_node, *prev_node; 1121 struct btrfs_delayed_node *curr_node, *prev_node;
1115 struct btrfs_path *path; 1122 struct btrfs_path *path;
1116 struct btrfs_block_rsv *block_rsv; 1123 struct btrfs_block_rsv *block_rsv;
1117 int ret = 0; 1124 int ret = 0;
1118 1125
1126 if (trans->aborted)
1127 return -EIO;
1128
1119 path = btrfs_alloc_path(); 1129 path = btrfs_alloc_path();
1120 if (!path) 1130 if (!path)
1121 return -ENOMEM; 1131 return -ENOMEM;
@@ -1128,17 +1138,18 @@ int btrfs_run_delayed_items(struct btrfs_trans_handle *trans,
1128 1138
1129 curr_node = btrfs_first_delayed_node(delayed_root); 1139 curr_node = btrfs_first_delayed_node(delayed_root);
1130 while (curr_node) { 1140 while (curr_node) {
1131 root = curr_node->root; 1141 curr_root = curr_node->root;
1132 ret = btrfs_insert_delayed_items(trans, path, root, 1142 ret = btrfs_insert_delayed_items(trans, path, curr_root,
1133 curr_node); 1143 curr_node);
1134 if (!ret) 1144 if (!ret)
1135 ret = btrfs_delete_delayed_items(trans, path, root, 1145 ret = btrfs_delete_delayed_items(trans, path,
1136 curr_node); 1146 curr_root, curr_node);
1137 if (!ret) 1147 if (!ret)
1138 ret = btrfs_update_delayed_inode(trans, root, path, 1148 ret = btrfs_update_delayed_inode(trans, curr_root,
1139 curr_node); 1149 path, curr_node);
1140 if (ret) { 1150 if (ret) {
1141 btrfs_release_delayed_node(curr_node); 1151 btrfs_release_delayed_node(curr_node);
1152 btrfs_abort_transaction(trans, root, ret);
1142 break; 1153 break;
1143 } 1154 }
1144 1155
@@ -1149,6 +1160,7 @@ int btrfs_run_delayed_items(struct btrfs_trans_handle *trans,
1149 1160
1150 btrfs_free_path(path); 1161 btrfs_free_path(path);
1151 trans->block_rsv = block_rsv; 1162 trans->block_rsv = block_rsv;
1163
1152 return ret; 1164 return ret;
1153} 1165}
1154 1166
@@ -1369,6 +1381,7 @@ void btrfs_balance_delayed_items(struct btrfs_root *root)
1369 btrfs_wq_run_delayed_node(delayed_root, root, 0); 1381 btrfs_wq_run_delayed_node(delayed_root, root, 0);
1370} 1382}
1371 1383
1384/* Will return 0 or -ENOMEM */
1372int btrfs_insert_delayed_dir_index(struct btrfs_trans_handle *trans, 1385int btrfs_insert_delayed_dir_index(struct btrfs_trans_handle *trans,
1373 struct btrfs_root *root, const char *name, 1386 struct btrfs_root *root, const char *name,
1374 int name_len, struct inode *dir, 1387 int name_len, struct inode *dir,
diff --git a/fs/btrfs/dir-item.c b/fs/btrfs/dir-item.c
index 76743308bd9f..c1a074d0696f 100644
--- a/fs/btrfs/dir-item.c
+++ b/fs/btrfs/dir-item.c
@@ -115,6 +115,7 @@ int btrfs_insert_xattr_item(struct btrfs_trans_handle *trans,
115 * 'location' is the key to stuff into the directory item, 'type' is the 115 * 'location' is the key to stuff into the directory item, 'type' is the
116 * type of the inode we're pointing to, and 'index' is the sequence number 116 * type of the inode we're pointing to, and 'index' is the sequence number
117 * to use for the second index (if one is created). 117 * to use for the second index (if one is created).
118 * Will return 0 or -ENOMEM
118 */ 119 */
119int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root 120int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
120 *root, const char *name, int name_len, 121 *root, const char *name, int name_len,
diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
index 6297a030ac50..16a0cada26c2 100644
--- a/fs/btrfs/disk-io.c
+++ b/fs/btrfs/disk-io.c
@@ -98,6 +98,7 @@ struct async_submit_bio {
98 */ 98 */
99 u64 bio_offset; 99 u64 bio_offset;
100 struct btrfs_work work; 100 struct btrfs_work work;
101 int error;
101}; 102};
102 103
103/* 104/*
@@ -405,7 +406,7 @@ static int csum_dirty_buffer(struct btrfs_root *root, struct page *page)
405 u64 found_start; 406 u64 found_start;
406 unsigned long len; 407 unsigned long len;
407 struct extent_buffer *eb; 408 struct extent_buffer *eb;
408 int ret; 409 int ret = -EIO;
409 410
410 tree = &BTRFS_I(page->mapping->host)->io_tree; 411 tree = &BTRFS_I(page->mapping->host)->io_tree;
411 412
@@ -423,13 +424,20 @@ static int csum_dirty_buffer(struct btrfs_root *root, struct page *page)
423 eb = alloc_extent_buffer(tree, start, len, page); 424 eb = alloc_extent_buffer(tree, start, len, page);
424 if (eb == NULL) { 425 if (eb == NULL) {
425 WARN_ON(1); 426 WARN_ON(1);
427 ret = -ENOMEM;
426 goto out; 428 goto out;
427 } 429 }
428 ret = btree_read_extent_buffer_pages(root, eb, start + PAGE_CACHE_SIZE, 430 ret = btree_read_extent_buffer_pages(root, eb, start + PAGE_CACHE_SIZE,
429 btrfs_header_generation(eb)); 431 btrfs_header_generation(eb));
430 BUG_ON(ret); 432 if (ret) {
433 btrfs_printk(root->fs_info, KERN_WARNING
434 "Failed to checksum dirty buffer @ %llu[%lu]\n",
435 start, len);
436 goto err;
437 }
431 WARN_ON(!btrfs_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN)); 438 WARN_ON(!btrfs_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN));
432 439
440 ret = -EIO;
433 found_start = btrfs_header_bytenr(eb); 441 found_start = btrfs_header_bytenr(eb);
434 if (found_start != start) { 442 if (found_start != start) {
435 WARN_ON(1); 443 WARN_ON(1);
@@ -444,10 +452,11 @@ static int csum_dirty_buffer(struct btrfs_root *root, struct page *page)
444 goto err; 452 goto err;
445 } 453 }
446 csum_tree_block(root, eb, 0); 454 csum_tree_block(root, eb, 0);
455 ret = 0;
447err: 456err:
448 free_extent_buffer(eb); 457 free_extent_buffer(eb);
449out: 458out:
450 return 0; 459 return ret;
451} 460}
452 461
453static int check_tree_block_fsid(struct btrfs_root *root, 462static int check_tree_block_fsid(struct btrfs_root *root,
@@ -718,11 +727,14 @@ unsigned long btrfs_async_submit_limit(struct btrfs_fs_info *info)
718static void run_one_async_start(struct btrfs_work *work) 727static void run_one_async_start(struct btrfs_work *work)
719{ 728{
720 struct async_submit_bio *async; 729 struct async_submit_bio *async;
730 int ret;
721 731
722 async = container_of(work, struct async_submit_bio, work); 732 async = container_of(work, struct async_submit_bio, work);
723 async->submit_bio_start(async->inode, async->rw, async->bio, 733 ret = async->submit_bio_start(async->inode, async->rw, async->bio,
724 async->mirror_num, async->bio_flags, 734 async->mirror_num, async->bio_flags,
725 async->bio_offset); 735 async->bio_offset);
736 if (ret)
737 async->error = ret;
726} 738}
727 739
728static void run_one_async_done(struct btrfs_work *work) 740static void run_one_async_done(struct btrfs_work *work)
@@ -743,6 +755,12 @@ static void run_one_async_done(struct btrfs_work *work)
743 waitqueue_active(&fs_info->async_submit_wait)) 755 waitqueue_active(&fs_info->async_submit_wait))
744 wake_up(&fs_info->async_submit_wait); 756 wake_up(&fs_info->async_submit_wait);
745 757
758 /* If an error occured we just want to clean up the bio and move on */
759 if (async->error) {
760 bio_endio(async->bio, async->error);
761 return;
762 }
763
746 async->submit_bio_done(async->inode, async->rw, async->bio, 764 async->submit_bio_done(async->inode, async->rw, async->bio,
747 async->mirror_num, async->bio_flags, 765 async->mirror_num, async->bio_flags,
748 async->bio_offset); 766 async->bio_offset);
@@ -784,6 +802,8 @@ int btrfs_wq_submit_bio(struct btrfs_fs_info *fs_info, struct inode *inode,
784 async->bio_flags = bio_flags; 802 async->bio_flags = bio_flags;
785 async->bio_offset = bio_offset; 803 async->bio_offset = bio_offset;
786 804
805 async->error = 0;
806
787 atomic_inc(&fs_info->nr_async_submits); 807 atomic_inc(&fs_info->nr_async_submits);
788 808
789 if (rw & REQ_SYNC) 809 if (rw & REQ_SYNC)
@@ -805,15 +825,18 @@ static int btree_csum_one_bio(struct bio *bio)
805 struct bio_vec *bvec = bio->bi_io_vec; 825 struct bio_vec *bvec = bio->bi_io_vec;
806 int bio_index = 0; 826 int bio_index = 0;
807 struct btrfs_root *root; 827 struct btrfs_root *root;
828 int ret = 0;
808 829
809 WARN_ON(bio->bi_vcnt <= 0); 830 WARN_ON(bio->bi_vcnt <= 0);
810 while (bio_index < bio->bi_vcnt) { 831 while (bio_index < bio->bi_vcnt) {
811 root = BTRFS_I(bvec->bv_page->mapping->host)->root; 832 root = BTRFS_I(bvec->bv_page->mapping->host)->root;
812 csum_dirty_buffer(root, bvec->bv_page); 833 ret = csum_dirty_buffer(root, bvec->bv_page);
834 if (ret)
835 break;
813 bio_index++; 836 bio_index++;
814 bvec++; 837 bvec++;
815 } 838 }
816 return 0; 839 return ret;
817} 840}
818 841
819static int __btree_submit_bio_start(struct inode *inode, int rw, 842static int __btree_submit_bio_start(struct inode *inode, int rw,
@@ -825,8 +848,7 @@ static int __btree_submit_bio_start(struct inode *inode, int rw,
825 * when we're called for a write, we're already in the async 848 * when we're called for a write, we're already in the async
826 * submission context. Just jump into btrfs_map_bio 849 * submission context. Just jump into btrfs_map_bio
827 */ 850 */
828 btree_csum_one_bio(bio); 851 return btree_csum_one_bio(bio);
829 return 0;
830} 852}
831 853
832static int __btree_submit_bio_done(struct inode *inode, int rw, struct bio *bio, 854static int __btree_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
@@ -1381,7 +1403,7 @@ struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_root *tree_root,
1381 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item), 1403 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
1382 blocksize, generation); 1404 blocksize, generation);
1383 root->commit_root = btrfs_root_node(root); 1405 root->commit_root = btrfs_root_node(root);
1384 BUG_ON(!root->node); 1406 BUG_ON(!root->node); /* -ENOMEM */
1385out: 1407out:
1386 if (location->objectid != BTRFS_TREE_LOG_OBJECTID) { 1408 if (location->objectid != BTRFS_TREE_LOG_OBJECTID) {
1387 root->ref_cows = 1; 1409 root->ref_cows = 1;
@@ -1618,7 +1640,6 @@ static int transaction_kthread(void *arg)
1618 u64 transid; 1640 u64 transid;
1619 unsigned long now; 1641 unsigned long now;
1620 unsigned long delay; 1642 unsigned long delay;
1621 int ret;
1622 1643
1623 do { 1644 do {
1624 delay = HZ * 30; 1645 delay = HZ * 30;
@@ -1642,11 +1663,12 @@ static int transaction_kthread(void *arg)
1642 transid = cur->transid; 1663 transid = cur->transid;
1643 spin_unlock(&root->fs_info->trans_lock); 1664 spin_unlock(&root->fs_info->trans_lock);
1644 1665
1666 /* If the file system is aborted, this will always fail. */
1645 trans = btrfs_join_transaction(root); 1667 trans = btrfs_join_transaction(root);
1646 BUG_ON(IS_ERR(trans)); 1668 if (IS_ERR(trans))
1669 goto sleep;
1647 if (transid == trans->transid) { 1670 if (transid == trans->transid) {
1648 ret = btrfs_commit_transaction(trans, root); 1671 btrfs_commit_transaction(trans, root);
1649 BUG_ON(ret);
1650 } else { 1672 } else {
1651 btrfs_end_transaction(trans, root); 1673 btrfs_end_transaction(trans, root);
1652 } 1674 }
@@ -2289,7 +2311,7 @@ int open_ctree(struct super_block *sb,
2289 chunk_root->node = read_tree_block(chunk_root, 2311 chunk_root->node = read_tree_block(chunk_root,
2290 btrfs_super_chunk_root(disk_super), 2312 btrfs_super_chunk_root(disk_super),
2291 blocksize, generation); 2313 blocksize, generation);
2292 BUG_ON(!chunk_root->node); 2314 BUG_ON(!chunk_root->node); /* -ENOMEM */
2293 if (!test_bit(EXTENT_BUFFER_UPTODATE, &chunk_root->node->bflags)) { 2315 if (!test_bit(EXTENT_BUFFER_UPTODATE, &chunk_root->node->bflags)) {
2294 printk(KERN_WARNING "btrfs: failed to read chunk root on %s\n", 2316 printk(KERN_WARNING "btrfs: failed to read chunk root on %s\n",
2295 sb->s_id); 2317 sb->s_id);
@@ -2429,21 +2451,31 @@ retry_root_backup:
2429 log_tree_root->node = read_tree_block(tree_root, bytenr, 2451 log_tree_root->node = read_tree_block(tree_root, bytenr,
2430 blocksize, 2452 blocksize,
2431 generation + 1); 2453 generation + 1);
2454 /* returns with log_tree_root freed on success */
2432 ret = btrfs_recover_log_trees(log_tree_root); 2455 ret = btrfs_recover_log_trees(log_tree_root);
2433 BUG_ON(ret); 2456 if (ret) {
2457 btrfs_error(tree_root->fs_info, ret,
2458 "Failed to recover log tree");
2459 free_extent_buffer(log_tree_root->node);
2460 kfree(log_tree_root);
2461 goto fail_trans_kthread;
2462 }
2434 2463
2435 if (sb->s_flags & MS_RDONLY) { 2464 if (sb->s_flags & MS_RDONLY) {
2436 ret = btrfs_commit_super(tree_root); 2465 ret = btrfs_commit_super(tree_root);
2437 BUG_ON(ret); 2466 if (ret)
2467 goto fail_trans_kthread;
2438 } 2468 }
2439 } 2469 }
2440 2470
2441 ret = btrfs_find_orphan_roots(tree_root); 2471 ret = btrfs_find_orphan_roots(tree_root);
2442 BUG_ON(ret); 2472 if (ret)
2473 goto fail_trans_kthread;
2443 2474
2444 if (!(sb->s_flags & MS_RDONLY)) { 2475 if (!(sb->s_flags & MS_RDONLY)) {
2445 ret = btrfs_cleanup_fs_roots(fs_info); 2476 ret = btrfs_cleanup_fs_roots(fs_info);
2446 BUG_ON(ret); 2477 if (ret) {
2478 }
2447 2479
2448 ret = btrfs_recover_relocation(tree_root); 2480 ret = btrfs_recover_relocation(tree_root);
2449 if (ret < 0) { 2481 if (ret < 0) {
@@ -2863,6 +2895,8 @@ int write_all_supers(struct btrfs_root *root, int max_mirrors)
2863 if (total_errors > max_errors) { 2895 if (total_errors > max_errors) {
2864 printk(KERN_ERR "btrfs: %d errors while writing supers\n", 2896 printk(KERN_ERR "btrfs: %d errors while writing supers\n",
2865 total_errors); 2897 total_errors);
2898
2899 /* This shouldn't happen. FUA is masked off if unsupported */
2866 BUG(); 2900 BUG();
2867 } 2901 }
2868 2902
@@ -2879,9 +2913,9 @@ int write_all_supers(struct btrfs_root *root, int max_mirrors)
2879 } 2913 }
2880 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); 2914 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2881 if (total_errors > max_errors) { 2915 if (total_errors > max_errors) {
2882 printk(KERN_ERR "btrfs: %d errors while writing supers\n", 2916 btrfs_error(root->fs_info, -EIO,
2883 total_errors); 2917 "%d errors while writing supers", total_errors);
2884 BUG(); 2918 return -EIO;
2885 } 2919 }
2886 return 0; 2920 return 0;
2887} 2921}
@@ -3014,14 +3048,21 @@ int btrfs_commit_super(struct btrfs_root *root)
3014 if (IS_ERR(trans)) 3048 if (IS_ERR(trans))
3015 return PTR_ERR(trans); 3049 return PTR_ERR(trans);
3016 ret = btrfs_commit_transaction(trans, root); 3050 ret = btrfs_commit_transaction(trans, root);
3017 BUG_ON(ret); 3051 if (ret)
3052 return ret;
3018 /* run commit again to drop the original snapshot */ 3053 /* run commit again to drop the original snapshot */
3019 trans = btrfs_join_transaction(root); 3054 trans = btrfs_join_transaction(root);
3020 if (IS_ERR(trans)) 3055 if (IS_ERR(trans))
3021 return PTR_ERR(trans); 3056 return PTR_ERR(trans);
3022 btrfs_commit_transaction(trans, root); 3057 ret = btrfs_commit_transaction(trans, root);
3058 if (ret)
3059 return ret;
3023 ret = btrfs_write_and_wait_transaction(NULL, root); 3060 ret = btrfs_write_and_wait_transaction(NULL, root);
3024 BUG_ON(ret); 3061 if (ret) {
3062 btrfs_error(root->fs_info, ret,
3063 "Failed to sync btree inode to disk.");
3064 return ret;
3065 }
3025 3066
3026 ret = write_ctree_super(NULL, root, 0); 3067 ret = write_ctree_super(NULL, root, 0);
3027 return ret; 3068 return ret;
@@ -3366,8 +3407,8 @@ static void btrfs_destroy_ordered_extents(struct btrfs_root *root)
3366 spin_unlock(&root->fs_info->ordered_extent_lock); 3407 spin_unlock(&root->fs_info->ordered_extent_lock);
3367} 3408}
3368 3409
3369static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, 3410int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans,
3370 struct btrfs_root *root) 3411 struct btrfs_root *root)
3371{ 3412{
3372 struct rb_node *node; 3413 struct rb_node *node;
3373 struct btrfs_delayed_ref_root *delayed_refs; 3414 struct btrfs_delayed_ref_root *delayed_refs;
@@ -3376,6 +3417,7 @@ static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans,
3376 3417
3377 delayed_refs = &trans->delayed_refs; 3418 delayed_refs = &trans->delayed_refs;
3378 3419
3420again:
3379 spin_lock(&delayed_refs->lock); 3421 spin_lock(&delayed_refs->lock);
3380 if (delayed_refs->num_entries == 0) { 3422 if (delayed_refs->num_entries == 0) {
3381 spin_unlock(&delayed_refs->lock); 3423 spin_unlock(&delayed_refs->lock);
@@ -3397,6 +3439,7 @@ static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans,
3397 struct btrfs_delayed_ref_head *head; 3439 struct btrfs_delayed_ref_head *head;
3398 3440
3399 head = btrfs_delayed_node_to_head(ref); 3441 head = btrfs_delayed_node_to_head(ref);
3442 spin_unlock(&delayed_refs->lock);
3400 mutex_lock(&head->mutex); 3443 mutex_lock(&head->mutex);
3401 kfree(head->extent_op); 3444 kfree(head->extent_op);
3402 delayed_refs->num_heads--; 3445 delayed_refs->num_heads--;
@@ -3404,8 +3447,9 @@ static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans,
3404 delayed_refs->num_heads_ready--; 3447 delayed_refs->num_heads_ready--;
3405 list_del_init(&head->cluster); 3448 list_del_init(&head->cluster);
3406 mutex_unlock(&head->mutex); 3449 mutex_unlock(&head->mutex);
3450 btrfs_put_delayed_ref(ref);
3451 goto again;
3407 } 3452 }
3408
3409 spin_unlock(&delayed_refs->lock); 3453 spin_unlock(&delayed_refs->lock);
3410 btrfs_put_delayed_ref(ref); 3454 btrfs_put_delayed_ref(ref);
3411 3455
@@ -3649,6 +3693,17 @@ int btrfs_cleanup_transaction(struct btrfs_root *root)
3649 return 0; 3693 return 0;
3650} 3694}
3651 3695
3696static int btree_writepage_io_failed_hook(struct bio *bio, struct page *page,
3697 u64 start, u64 end,
3698 struct extent_state *state)
3699{
3700 struct super_block *sb = page->mapping->host->i_sb;
3701 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
3702 btrfs_error(fs_info, -EIO,
3703 "Error occured while writing out btree at %llu", start);
3704 return -EIO;
3705}
3706
3652static struct extent_io_ops btree_extent_io_ops = { 3707static struct extent_io_ops btree_extent_io_ops = {
3653 .write_cache_pages_lock_hook = btree_lock_page_hook, 3708 .write_cache_pages_lock_hook = btree_lock_page_hook,
3654 .readpage_end_io_hook = btree_readpage_end_io_hook, 3709 .readpage_end_io_hook = btree_readpage_end_io_hook,
@@ -3656,4 +3711,5 @@ static struct extent_io_ops btree_extent_io_ops = {
3656 .submit_bio_hook = btree_submit_bio_hook, 3711 .submit_bio_hook = btree_submit_bio_hook,
3657 /* note we're sharing with inode.c for the merge bio hook */ 3712 /* note we're sharing with inode.c for the merge bio hook */
3658 .merge_bio_hook = btrfs_merge_bio_hook, 3713 .merge_bio_hook = btrfs_merge_bio_hook,
3714 .writepage_io_failed_hook = btree_writepage_io_failed_hook,
3659}; 3715};
diff --git a/fs/btrfs/export.c b/fs/btrfs/export.c
index 5f77166fd01c..e887ee62b6d4 100644
--- a/fs/btrfs/export.c
+++ b/fs/btrfs/export.c
@@ -193,7 +193,7 @@ static struct dentry *btrfs_get_parent(struct dentry *child)
193 if (ret < 0) 193 if (ret < 0)
194 goto fail; 194 goto fail;
195 195
196 BUG_ON(ret == 0); 196 BUG_ON(ret == 0); /* Key with offset of -1 found */
197 if (path->slots[0] == 0) { 197 if (path->slots[0] == 0) {
198 ret = -ENOENT; 198 ret = -ENOENT;
199 goto fail; 199 goto fail;
diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c
index cd6f8ae0a78d..4b3f1eedced0 100644
--- a/fs/btrfs/extent-tree.c
+++ b/fs/btrfs/extent-tree.c
@@ -245,7 +245,7 @@ static int exclude_super_stripes(struct btrfs_root *root,
245 cache->bytes_super += stripe_len; 245 cache->bytes_super += stripe_len;
246 ret = add_excluded_extent(root, cache->key.objectid, 246 ret = add_excluded_extent(root, cache->key.objectid,
247 stripe_len); 247 stripe_len);
248 BUG_ON(ret); 248 BUG_ON(ret); /* -ENOMEM */
249 } 249 }
250 250
251 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) { 251 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
@@ -253,13 +253,13 @@ static int exclude_super_stripes(struct btrfs_root *root,
253 ret = btrfs_rmap_block(&root->fs_info->mapping_tree, 253 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
254 cache->key.objectid, bytenr, 254 cache->key.objectid, bytenr,
255 0, &logical, &nr, &stripe_len); 255 0, &logical, &nr, &stripe_len);
256 BUG_ON(ret); 256 BUG_ON(ret); /* -ENOMEM */
257 257
258 while (nr--) { 258 while (nr--) {
259 cache->bytes_super += stripe_len; 259 cache->bytes_super += stripe_len;
260 ret = add_excluded_extent(root, logical[nr], 260 ret = add_excluded_extent(root, logical[nr],
261 stripe_len); 261 stripe_len);
262 BUG_ON(ret); 262 BUG_ON(ret); /* -ENOMEM */
263 } 263 }
264 264
265 kfree(logical); 265 kfree(logical);
@@ -321,7 +321,7 @@ static u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
321 total_added += size; 321 total_added += size;
322 ret = btrfs_add_free_space(block_group, start, 322 ret = btrfs_add_free_space(block_group, start,
323 size); 323 size);
324 BUG_ON(ret); 324 BUG_ON(ret); /* -ENOMEM or logic error */
325 start = extent_end + 1; 325 start = extent_end + 1;
326 } else { 326 } else {
327 break; 327 break;
@@ -332,7 +332,7 @@ static u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
332 size = end - start; 332 size = end - start;
333 total_added += size; 333 total_added += size;
334 ret = btrfs_add_free_space(block_group, start, size); 334 ret = btrfs_add_free_space(block_group, start, size);
335 BUG_ON(ret); 335 BUG_ON(ret); /* -ENOMEM or logic error */
336 } 336 }
337 337
338 return total_added; 338 return total_added;
@@ -474,7 +474,8 @@ static int cache_block_group(struct btrfs_block_group_cache *cache,
474 int ret = 0; 474 int ret = 0;
475 475
476 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS); 476 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS);
477 BUG_ON(!caching_ctl); 477 if (!caching_ctl)
478 return -ENOMEM;
478 479
479 INIT_LIST_HEAD(&caching_ctl->list); 480 INIT_LIST_HEAD(&caching_ctl->list);
480 mutex_init(&caching_ctl->mutex); 481 mutex_init(&caching_ctl->mutex);
@@ -982,7 +983,7 @@ static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
982 ret = btrfs_next_leaf(root, path); 983 ret = btrfs_next_leaf(root, path);
983 if (ret < 0) 984 if (ret < 0)
984 return ret; 985 return ret;
985 BUG_ON(ret > 0); 986 BUG_ON(ret > 0); /* Corruption */
986 leaf = path->nodes[0]; 987 leaf = path->nodes[0];
987 } 988 }
988 btrfs_item_key_to_cpu(leaf, &found_key, 989 btrfs_item_key_to_cpu(leaf, &found_key,
@@ -1008,7 +1009,7 @@ static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
1008 new_size + extra_size, 1); 1009 new_size + extra_size, 1);
1009 if (ret < 0) 1010 if (ret < 0)
1010 return ret; 1011 return ret;
1011 BUG_ON(ret); 1012 BUG_ON(ret); /* Corruption */
1012 1013
1013 btrfs_extend_item(trans, root, path, new_size); 1014 btrfs_extend_item(trans, root, path, new_size);
1014 1015
@@ -1478,7 +1479,11 @@ int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1478 err = ret; 1479 err = ret;
1479 goto out; 1480 goto out;
1480 } 1481 }
1481 BUG_ON(ret); 1482 if (ret && !insert) {
1483 err = -ENOENT;
1484 goto out;
1485 }
1486 BUG_ON(ret); /* Corruption */
1482 1487
1483 leaf = path->nodes[0]; 1488 leaf = path->nodes[0];
1484 item_size = btrfs_item_size_nr(leaf, path->slots[0]); 1489 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
@@ -1831,6 +1836,7 @@ static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
1831 /* Tell the block device(s) that the sectors can be discarded */ 1836 /* Tell the block device(s) that the sectors can be discarded */
1832 ret = btrfs_map_block(&root->fs_info->mapping_tree, REQ_DISCARD, 1837 ret = btrfs_map_block(&root->fs_info->mapping_tree, REQ_DISCARD,
1833 bytenr, &num_bytes, &bbio, 0); 1838 bytenr, &num_bytes, &bbio, 0);
1839 /* Error condition is -ENOMEM */
1834 if (!ret) { 1840 if (!ret) {
1835 struct btrfs_bio_stripe *stripe = bbio->stripes; 1841 struct btrfs_bio_stripe *stripe = bbio->stripes;
1836 int i; 1842 int i;
@@ -1846,7 +1852,7 @@ static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
1846 if (!ret) 1852 if (!ret)
1847 discarded_bytes += stripe->length; 1853 discarded_bytes += stripe->length;
1848 else if (ret != -EOPNOTSUPP) 1854 else if (ret != -EOPNOTSUPP)
1849 break; 1855 break; /* Logic errors or -ENOMEM, or -EIO but I don't know how that could happen JDM */
1850 1856
1851 /* 1857 /*
1852 * Just in case we get back EOPNOTSUPP for some reason, 1858 * Just in case we get back EOPNOTSUPP for some reason,
@@ -1865,6 +1871,7 @@ static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
1865 return ret; 1871 return ret;
1866} 1872}
1867 1873
1874/* Can return -ENOMEM */
1868int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, 1875int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1869 struct btrfs_root *root, 1876 struct btrfs_root *root,
1870 u64 bytenr, u64 num_bytes, u64 parent, 1877 u64 bytenr, u64 num_bytes, u64 parent,
@@ -1940,7 +1947,8 @@ static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1940 ret = insert_extent_backref(trans, root->fs_info->extent_root, 1947 ret = insert_extent_backref(trans, root->fs_info->extent_root,
1941 path, bytenr, parent, root_objectid, 1948 path, bytenr, parent, root_objectid,
1942 owner, offset, refs_to_add); 1949 owner, offset, refs_to_add);
1943 BUG_ON(ret); 1950 if (ret)
1951 btrfs_abort_transaction(trans, root, ret);
1944out: 1952out:
1945 btrfs_free_path(path); 1953 btrfs_free_path(path);
1946 return err; 1954 return err;
@@ -2027,6 +2035,9 @@ static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
2027 int ret; 2035 int ret;
2028 int err = 0; 2036 int err = 0;
2029 2037
2038 if (trans->aborted)
2039 return 0;
2040
2030 path = btrfs_alloc_path(); 2041 path = btrfs_alloc_path();
2031 if (!path) 2042 if (!path)
2032 return -ENOMEM; 2043 return -ENOMEM;
@@ -2124,7 +2135,11 @@ static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
2124 struct btrfs_delayed_extent_op *extent_op, 2135 struct btrfs_delayed_extent_op *extent_op,
2125 int insert_reserved) 2136 int insert_reserved)
2126{ 2137{
2127 int ret; 2138 int ret = 0;
2139
2140 if (trans->aborted)
2141 return 0;
2142
2128 if (btrfs_delayed_ref_is_head(node)) { 2143 if (btrfs_delayed_ref_is_head(node)) {
2129 struct btrfs_delayed_ref_head *head; 2144 struct btrfs_delayed_ref_head *head;
2130 /* 2145 /*
@@ -2142,11 +2157,10 @@ static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
2142 ret = btrfs_del_csums(trans, root, 2157 ret = btrfs_del_csums(trans, root,
2143 node->bytenr, 2158 node->bytenr,
2144 node->num_bytes); 2159 node->num_bytes);
2145 BUG_ON(ret);
2146 } 2160 }
2147 } 2161 }
2148 mutex_unlock(&head->mutex); 2162 mutex_unlock(&head->mutex);
2149 return 0; 2163 return ret;
2150 } 2164 }
2151 2165
2152 if (node->type == BTRFS_TREE_BLOCK_REF_KEY || 2166 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
@@ -2193,6 +2207,10 @@ again:
2193 return NULL; 2207 return NULL;
2194} 2208}
2195 2209
2210/*
2211 * Returns 0 on success or if called with an already aborted transaction.
2212 * Returns -ENOMEM or -EIO on failure and will abort the transaction.
2213 */
2196static noinline int run_clustered_refs(struct btrfs_trans_handle *trans, 2214static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
2197 struct btrfs_root *root, 2215 struct btrfs_root *root,
2198 struct list_head *cluster) 2216 struct list_head *cluster)
@@ -2281,9 +2299,13 @@ static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
2281 2299
2282 ret = run_delayed_extent_op(trans, root, 2300 ret = run_delayed_extent_op(trans, root,
2283 ref, extent_op); 2301 ref, extent_op);
2284 BUG_ON(ret);
2285 kfree(extent_op); 2302 kfree(extent_op);
2286 2303
2304 if (ret) {
2305 printk(KERN_DEBUG "btrfs: run_delayed_extent_op returned %d\n", ret);
2306 return ret;
2307 }
2308
2287 goto next; 2309 goto next;
2288 } 2310 }
2289 2311
@@ -2304,11 +2326,16 @@ static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
2304 2326
2305 ret = run_one_delayed_ref(trans, root, ref, extent_op, 2327 ret = run_one_delayed_ref(trans, root, ref, extent_op,
2306 must_insert_reserved); 2328 must_insert_reserved);
2307 BUG_ON(ret);
2308 2329
2309 btrfs_put_delayed_ref(ref); 2330 btrfs_put_delayed_ref(ref);
2310 kfree(extent_op); 2331 kfree(extent_op);
2311 count++; 2332 count++;
2333
2334 if (ret) {
2335 printk(KERN_DEBUG "btrfs: run_one_delayed_ref returned %d\n", ret);
2336 return ret;
2337 }
2338
2312next: 2339next:
2313 do_chunk_alloc(trans, root->fs_info->extent_root, 2340 do_chunk_alloc(trans, root->fs_info->extent_root,
2314 2 * 1024 * 1024, 2341 2 * 1024 * 1024,
@@ -2343,6 +2370,9 @@ static void wait_for_more_refs(struct btrfs_delayed_ref_root *delayed_refs,
2343 * 0, which means to process everything in the tree at the start 2370 * 0, which means to process everything in the tree at the start
2344 * of the run (but not newly added entries), or it can be some target 2371 * of the run (but not newly added entries), or it can be some target
2345 * number you'd like to process. 2372 * number you'd like to process.
2373 *
2374 * Returns 0 on success or if called with an aborted transaction
2375 * Returns <0 on error and aborts the transaction
2346 */ 2376 */
2347int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, 2377int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2348 struct btrfs_root *root, unsigned long count) 2378 struct btrfs_root *root, unsigned long count)
@@ -2358,6 +2388,10 @@ int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2358 unsigned long num_refs = 0; 2388 unsigned long num_refs = 0;
2359 int consider_waiting; 2389 int consider_waiting;
2360 2390
2391 /* We'll clean this up in btrfs_cleanup_transaction */
2392 if (trans->aborted)
2393 return 0;
2394
2361 if (root == root->fs_info->extent_root) 2395 if (root == root->fs_info->extent_root)
2362 root = root->fs_info->tree_root; 2396 root = root->fs_info->tree_root;
2363 2397
@@ -2415,7 +2449,11 @@ again:
2415 } 2449 }
2416 2450
2417 ret = run_clustered_refs(trans, root, &cluster); 2451 ret = run_clustered_refs(trans, root, &cluster);
2418 BUG_ON(ret < 0); 2452 if (ret < 0) {
2453 spin_unlock(&delayed_refs->lock);
2454 btrfs_abort_transaction(trans, root, ret);
2455 return ret;
2456 }
2419 2457
2420 count -= min_t(unsigned long, ret, count); 2458 count -= min_t(unsigned long, ret, count);
2421 2459
@@ -2580,7 +2618,7 @@ static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
2580 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); 2618 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2581 if (ret < 0) 2619 if (ret < 0)
2582 goto out; 2620 goto out;
2583 BUG_ON(ret == 0); 2621 BUG_ON(ret == 0); /* Corruption */
2584 2622
2585 ret = -ENOENT; 2623 ret = -ENOENT;
2586 if (path->slots[0] == 0) 2624 if (path->slots[0] == 0)
@@ -2734,7 +2772,6 @@ static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
2734 } 2772 }
2735 return 0; 2773 return 0;
2736fail: 2774fail:
2737 BUG();
2738 return ret; 2775 return ret;
2739} 2776}
2740 2777
@@ -2763,7 +2800,7 @@ static int write_one_cache_group(struct btrfs_trans_handle *trans,
2763 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1); 2800 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
2764 if (ret < 0) 2801 if (ret < 0)
2765 goto fail; 2802 goto fail;
2766 BUG_ON(ret); 2803 BUG_ON(ret); /* Corruption */
2767 2804
2768 leaf = path->nodes[0]; 2805 leaf = path->nodes[0];
2769 bi = btrfs_item_ptr_offset(leaf, path->slots[0]); 2806 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
@@ -2771,8 +2808,10 @@ static int write_one_cache_group(struct btrfs_trans_handle *trans,
2771 btrfs_mark_buffer_dirty(leaf); 2808 btrfs_mark_buffer_dirty(leaf);
2772 btrfs_release_path(path); 2809 btrfs_release_path(path);
2773fail: 2810fail:
2774 if (ret) 2811 if (ret) {
2812 btrfs_abort_transaction(trans, root, ret);
2775 return ret; 2813 return ret;
2814 }
2776 return 0; 2815 return 0;
2777 2816
2778} 2817}
@@ -2945,7 +2984,8 @@ again:
2945 if (last == 0) { 2984 if (last == 0) {
2946 err = btrfs_run_delayed_refs(trans, root, 2985 err = btrfs_run_delayed_refs(trans, root,
2947 (unsigned long)-1); 2986 (unsigned long)-1);
2948 BUG_ON(err); 2987 if (err) /* File system offline */
2988 goto out;
2949 } 2989 }
2950 2990
2951 cache = btrfs_lookup_first_block_group(root->fs_info, last); 2991 cache = btrfs_lookup_first_block_group(root->fs_info, last);
@@ -2972,7 +3012,9 @@ again:
2972 last = cache->key.objectid + cache->key.offset; 3012 last = cache->key.objectid + cache->key.offset;
2973 3013
2974 err = write_one_cache_group(trans, root, path, cache); 3014 err = write_one_cache_group(trans, root, path, cache);
2975 BUG_ON(err); 3015 if (err) /* File system offline */
3016 goto out;
3017
2976 btrfs_put_block_group(cache); 3018 btrfs_put_block_group(cache);
2977 } 3019 }
2978 3020
@@ -2985,7 +3027,8 @@ again:
2985 if (last == 0) { 3027 if (last == 0) {
2986 err = btrfs_run_delayed_refs(trans, root, 3028 err = btrfs_run_delayed_refs(trans, root,
2987 (unsigned long)-1); 3029 (unsigned long)-1);
2988 BUG_ON(err); 3030 if (err) /* File system offline */
3031 goto out;
2989 } 3032 }
2990 3033
2991 cache = btrfs_lookup_first_block_group(root->fs_info, last); 3034 cache = btrfs_lookup_first_block_group(root->fs_info, last);
@@ -3010,20 +3053,21 @@ again:
3010 continue; 3053 continue;
3011 } 3054 }
3012 3055
3013 btrfs_write_out_cache(root, trans, cache, path); 3056 err = btrfs_write_out_cache(root, trans, cache, path);
3014 3057
3015 /* 3058 /*
3016 * If we didn't have an error then the cache state is still 3059 * If we didn't have an error then the cache state is still
3017 * NEED_WRITE, so we can set it to WRITTEN. 3060 * NEED_WRITE, so we can set it to WRITTEN.
3018 */ 3061 */
3019 if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE) 3062 if (!err && cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
3020 cache->disk_cache_state = BTRFS_DC_WRITTEN; 3063 cache->disk_cache_state = BTRFS_DC_WRITTEN;
3021 last = cache->key.objectid + cache->key.offset; 3064 last = cache->key.objectid + cache->key.offset;
3022 btrfs_put_block_group(cache); 3065 btrfs_put_block_group(cache);
3023 } 3066 }
3067out:
3024 3068
3025 btrfs_free_path(path); 3069 btrfs_free_path(path);
3026 return 0; 3070 return err;
3027} 3071}
3028 3072
3029int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr) 3073int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
@@ -3407,9 +3451,9 @@ static int do_chunk_alloc(struct btrfs_trans_handle *trans,
3407 if (!space_info) { 3451 if (!space_info) {
3408 ret = update_space_info(extent_root->fs_info, flags, 3452 ret = update_space_info(extent_root->fs_info, flags,
3409 0, 0, &space_info); 3453 0, 0, &space_info);
3410 BUG_ON(ret); 3454 BUG_ON(ret); /* -ENOMEM */
3411 } 3455 }
3412 BUG_ON(!space_info); 3456 BUG_ON(!space_info); /* Logic error */
3413 3457
3414again: 3458again:
3415 spin_lock(&space_info->lock); 3459 spin_lock(&space_info->lock);
@@ -3674,8 +3718,10 @@ again:
3674 ret = wait_event_interruptible(space_info->wait, 3718 ret = wait_event_interruptible(space_info->wait,
3675 !space_info->flush); 3719 !space_info->flush);
3676 /* Must have been interrupted, return */ 3720 /* Must have been interrupted, return */
3677 if (ret) 3721 if (ret) {
3722 printk(KERN_DEBUG "btrfs: %s returning -EINTR\n", __func__);
3678 return -EINTR; 3723 return -EINTR;
3724 }
3679 3725
3680 spin_lock(&space_info->lock); 3726 spin_lock(&space_info->lock);
3681 } 3727 }
@@ -3832,8 +3878,9 @@ out:
3832 return ret; 3878 return ret;
3833} 3879}
3834 3880
3835static struct btrfs_block_rsv *get_block_rsv(struct btrfs_trans_handle *trans, 3881static struct btrfs_block_rsv *get_block_rsv(
3836 struct btrfs_root *root) 3882 const struct btrfs_trans_handle *trans,
3883 const struct btrfs_root *root)
3837{ 3884{
3838 struct btrfs_block_rsv *block_rsv = NULL; 3885 struct btrfs_block_rsv *block_rsv = NULL;
3839 3886
@@ -4200,6 +4247,7 @@ void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
4200 trans->bytes_reserved = 0; 4247 trans->bytes_reserved = 0;
4201} 4248}
4202 4249
4250/* Can only return 0 or -ENOSPC */
4203int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans, 4251int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
4204 struct inode *inode) 4252 struct inode *inode)
4205{ 4253{
@@ -4536,7 +4584,7 @@ static int update_block_group(struct btrfs_trans_handle *trans,
4536 while (total) { 4584 while (total) {
4537 cache = btrfs_lookup_block_group(info, bytenr); 4585 cache = btrfs_lookup_block_group(info, bytenr);
4538 if (!cache) 4586 if (!cache)
4539 return -1; 4587 return -ENOENT;
4540 if (cache->flags & (BTRFS_BLOCK_GROUP_DUP | 4588 if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
4541 BTRFS_BLOCK_GROUP_RAID1 | 4589 BTRFS_BLOCK_GROUP_RAID1 |
4542 BTRFS_BLOCK_GROUP_RAID10)) 4590 BTRFS_BLOCK_GROUP_RAID10))
@@ -4639,7 +4687,7 @@ int btrfs_pin_extent(struct btrfs_root *root,
4639 struct btrfs_block_group_cache *cache; 4687 struct btrfs_block_group_cache *cache;
4640 4688
4641 cache = btrfs_lookup_block_group(root->fs_info, bytenr); 4689 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
4642 BUG_ON(!cache); 4690 BUG_ON(!cache); /* Logic error */
4643 4691
4644 pin_down_extent(root, cache, bytenr, num_bytes, reserved); 4692 pin_down_extent(root, cache, bytenr, num_bytes, reserved);
4645 4693
@@ -4657,7 +4705,7 @@ int btrfs_pin_extent_for_log_replay(struct btrfs_trans_handle *trans,
4657 struct btrfs_block_group_cache *cache; 4705 struct btrfs_block_group_cache *cache;
4658 4706
4659 cache = btrfs_lookup_block_group(root->fs_info, bytenr); 4707 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
4660 BUG_ON(!cache); 4708 BUG_ON(!cache); /* Logic error */
4661 4709
4662 /* 4710 /*
4663 * pull in the free space cache (if any) so that our pin 4711 * pull in the free space cache (if any) so that our pin
@@ -4702,6 +4750,7 @@ static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
4702{ 4750{
4703 struct btrfs_space_info *space_info = cache->space_info; 4751 struct btrfs_space_info *space_info = cache->space_info;
4704 int ret = 0; 4752 int ret = 0;
4753
4705 spin_lock(&space_info->lock); 4754 spin_lock(&space_info->lock);
4706 spin_lock(&cache->lock); 4755 spin_lock(&cache->lock);
4707 if (reserve != RESERVE_FREE) { 4756 if (reserve != RESERVE_FREE) {
@@ -4774,7 +4823,7 @@ static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
4774 if (cache) 4823 if (cache)
4775 btrfs_put_block_group(cache); 4824 btrfs_put_block_group(cache);
4776 cache = btrfs_lookup_block_group(fs_info, start); 4825 cache = btrfs_lookup_block_group(fs_info, start);
4777 BUG_ON(!cache); 4826 BUG_ON(!cache); /* Logic error */
4778 } 4827 }
4779 4828
4780 len = cache->key.objectid + cache->key.offset - start; 4829 len = cache->key.objectid + cache->key.offset - start;
@@ -4811,6 +4860,9 @@ int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
4811 u64 end; 4860 u64 end;
4812 int ret; 4861 int ret;
4813 4862
4863 if (trans->aborted)
4864 return 0;
4865
4814 if (fs_info->pinned_extents == &fs_info->freed_extents[0]) 4866 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
4815 unpin = &fs_info->freed_extents[1]; 4867 unpin = &fs_info->freed_extents[1];
4816 else 4868 else
@@ -4896,7 +4948,8 @@ static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
4896 ret = remove_extent_backref(trans, extent_root, path, 4948 ret = remove_extent_backref(trans, extent_root, path,
4897 NULL, refs_to_drop, 4949 NULL, refs_to_drop,
4898 is_data); 4950 is_data);
4899 BUG_ON(ret); 4951 if (ret)
4952 goto abort;
4900 btrfs_release_path(path); 4953 btrfs_release_path(path);
4901 path->leave_spinning = 1; 4954 path->leave_spinning = 1;
4902 4955
@@ -4914,10 +4967,11 @@ static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
4914 btrfs_print_leaf(extent_root, 4967 btrfs_print_leaf(extent_root,
4915 path->nodes[0]); 4968 path->nodes[0]);
4916 } 4969 }
4917 BUG_ON(ret); 4970 if (ret < 0)
4971 goto abort;
4918 extent_slot = path->slots[0]; 4972 extent_slot = path->slots[0];
4919 } 4973 }
4920 } else { 4974 } else if (ret == -ENOENT) {
4921 btrfs_print_leaf(extent_root, path->nodes[0]); 4975 btrfs_print_leaf(extent_root, path->nodes[0]);
4922 WARN_ON(1); 4976 WARN_ON(1);
4923 printk(KERN_ERR "btrfs unable to find ref byte nr %llu " 4977 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
@@ -4927,6 +4981,8 @@ static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
4927 (unsigned long long)root_objectid, 4981 (unsigned long long)root_objectid,
4928 (unsigned long long)owner_objectid, 4982 (unsigned long long)owner_objectid,
4929 (unsigned long long)owner_offset); 4983 (unsigned long long)owner_offset);
4984 } else {
4985 goto abort;
4930 } 4986 }
4931 4987
4932 leaf = path->nodes[0]; 4988 leaf = path->nodes[0];
@@ -4936,7 +4992,8 @@ static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
4936 BUG_ON(found_extent || extent_slot != path->slots[0]); 4992 BUG_ON(found_extent || extent_slot != path->slots[0]);
4937 ret = convert_extent_item_v0(trans, extent_root, path, 4993 ret = convert_extent_item_v0(trans, extent_root, path,
4938 owner_objectid, 0); 4994 owner_objectid, 0);
4939 BUG_ON(ret < 0); 4995 if (ret < 0)
4996 goto abort;
4940 4997
4941 btrfs_release_path(path); 4998 btrfs_release_path(path);
4942 path->leave_spinning = 1; 4999 path->leave_spinning = 1;
@@ -4953,7 +5010,8 @@ static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
4953 (unsigned long long)bytenr); 5010 (unsigned long long)bytenr);
4954 btrfs_print_leaf(extent_root, path->nodes[0]); 5011 btrfs_print_leaf(extent_root, path->nodes[0]);
4955 } 5012 }
4956 BUG_ON(ret); 5013 if (ret < 0)
5014 goto abort;
4957 extent_slot = path->slots[0]; 5015 extent_slot = path->slots[0];
4958 leaf = path->nodes[0]; 5016 leaf = path->nodes[0];
4959 item_size = btrfs_item_size_nr(leaf, extent_slot); 5017 item_size = btrfs_item_size_nr(leaf, extent_slot);
@@ -4990,7 +5048,8 @@ static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
4990 ret = remove_extent_backref(trans, extent_root, path, 5048 ret = remove_extent_backref(trans, extent_root, path,
4991 iref, refs_to_drop, 5049 iref, refs_to_drop,
4992 is_data); 5050 is_data);
4993 BUG_ON(ret); 5051 if (ret)
5052 goto abort;
4994 } 5053 }
4995 } else { 5054 } else {
4996 if (found_extent) { 5055 if (found_extent) {
@@ -5007,12 +5066,14 @@ static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
5007 5066
5008 ret = btrfs_del_items(trans, extent_root, path, path->slots[0], 5067 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
5009 num_to_del); 5068 num_to_del);
5010 BUG_ON(ret); 5069 if (ret)
5070 goto abort;
5011 btrfs_release_path(path); 5071 btrfs_release_path(path);
5012 5072
5013 if (is_data) { 5073 if (is_data) {
5014 ret = btrfs_del_csums(trans, root, bytenr, num_bytes); 5074 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
5015 BUG_ON(ret); 5075 if (ret)
5076 goto abort;
5016 } else { 5077 } else {
5017 invalidate_mapping_pages(info->btree_inode->i_mapping, 5078 invalidate_mapping_pages(info->btree_inode->i_mapping,
5018 bytenr >> PAGE_CACHE_SHIFT, 5079 bytenr >> PAGE_CACHE_SHIFT,
@@ -5020,10 +5081,16 @@ static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
5020 } 5081 }
5021 5082
5022 ret = update_block_group(trans, root, bytenr, num_bytes, 0); 5083 ret = update_block_group(trans, root, bytenr, num_bytes, 0);
5023 BUG_ON(ret); 5084 if (ret)
5085 goto abort;
5024 } 5086 }
5087out:
5025 btrfs_free_path(path); 5088 btrfs_free_path(path);
5026 return ret; 5089 return ret;
5090
5091abort:
5092 btrfs_abort_transaction(trans, extent_root, ret);
5093 goto out;
5027} 5094}
5028 5095
5029/* 5096/*
@@ -5119,7 +5186,7 @@ void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
5119 parent, root->root_key.objectid, 5186 parent, root->root_key.objectid,
5120 btrfs_header_level(buf), 5187 btrfs_header_level(buf),
5121 BTRFS_DROP_DELAYED_REF, NULL, for_cow); 5188 BTRFS_DROP_DELAYED_REF, NULL, for_cow);
5122 BUG_ON(ret); 5189 BUG_ON(ret); /* -ENOMEM */
5123 } 5190 }
5124 5191
5125 if (!last_ref) 5192 if (!last_ref)
@@ -5153,6 +5220,7 @@ out:
5153 btrfs_put_block_group(cache); 5220 btrfs_put_block_group(cache);
5154} 5221}
5155 5222
5223/* Can return -ENOMEM */
5156int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root *root, 5224int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root *root,
5157 u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid, 5225 u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid,
5158 u64 owner, u64 offset, int for_cow) 5226 u64 owner, u64 offset, int for_cow)
@@ -5174,14 +5242,12 @@ int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root *root,
5174 num_bytes, 5242 num_bytes,
5175 parent, root_objectid, (int)owner, 5243 parent, root_objectid, (int)owner,
5176 BTRFS_DROP_DELAYED_REF, NULL, for_cow); 5244 BTRFS_DROP_DELAYED_REF, NULL, for_cow);
5177 BUG_ON(ret);
5178 } else { 5245 } else {
5179 ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr, 5246 ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
5180 num_bytes, 5247 num_bytes,
5181 parent, root_objectid, owner, 5248 parent, root_objectid, owner,
5182 offset, BTRFS_DROP_DELAYED_REF, 5249 offset, BTRFS_DROP_DELAYED_REF,
5183 NULL, for_cow); 5250 NULL, for_cow);
5184 BUG_ON(ret);
5185 } 5251 }
5186 return ret; 5252 return ret;
5187} 5253}
@@ -5418,6 +5484,7 @@ have_block_group:
5418 found_uncached_bg = true; 5484 found_uncached_bg = true;
5419 ret = cache_block_group(block_group, trans, 5485 ret = cache_block_group(block_group, trans,
5420 orig_root, 1); 5486 orig_root, 1);
5487 BUG_ON(ret < 0); /* -ENOMEM */
5421 if (block_group->cached == BTRFS_CACHE_FINISHED) 5488 if (block_group->cached == BTRFS_CACHE_FINISHED)
5422 goto alloc; 5489 goto alloc;
5423 5490
@@ -5439,7 +5506,7 @@ have_block_group:
5439 if (loop > LOOP_FIND_IDEAL) { 5506 if (loop > LOOP_FIND_IDEAL) {
5440 ret = cache_block_group(block_group, trans, 5507 ret = cache_block_group(block_group, trans,
5441 orig_root, 0); 5508 orig_root, 0);
5442 BUG_ON(ret); 5509 BUG_ON(ret); /* -ENOMEM */
5443 } 5510 }
5444 5511
5445 /* 5512 /*
@@ -5712,6 +5779,11 @@ loop:
5712 ret = do_chunk_alloc(trans, root, num_bytes + 5779 ret = do_chunk_alloc(trans, root, num_bytes +
5713 2 * 1024 * 1024, data, 5780 2 * 1024 * 1024, data,
5714 CHUNK_ALLOC_LIMITED); 5781 CHUNK_ALLOC_LIMITED);
5782 if (ret < 0) {
5783 btrfs_abort_transaction(trans,
5784 root, ret);
5785 goto out;
5786 }
5715 allowed_chunk_alloc = 0; 5787 allowed_chunk_alloc = 0;
5716 if (ret == 1) 5788 if (ret == 1)
5717 done_chunk_alloc = 1; 5789 done_chunk_alloc = 1;
@@ -5740,6 +5812,7 @@ loop:
5740 } else if (ins->objectid) { 5812 } else if (ins->objectid) {
5741 ret = 0; 5813 ret = 0;
5742 } 5814 }
5815out:
5743 5816
5744 return ret; 5817 return ret;
5745} 5818}
@@ -5806,10 +5879,15 @@ again:
5806 * the only place that sets empty_size is btrfs_realloc_node, which 5879 * the only place that sets empty_size is btrfs_realloc_node, which
5807 * is not called recursively on allocations 5880 * is not called recursively on allocations
5808 */ 5881 */
5809 if (empty_size || root->ref_cows) 5882 if (empty_size || root->ref_cows) {
5810 ret = do_chunk_alloc(trans, root->fs_info->extent_root, 5883 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
5811 num_bytes + 2 * 1024 * 1024, data, 5884 num_bytes + 2 * 1024 * 1024, data,
5812 CHUNK_ALLOC_NO_FORCE); 5885 CHUNK_ALLOC_NO_FORCE);
5886 if (ret < 0 && ret != -ENOSPC) {
5887 btrfs_abort_transaction(trans, root, ret);
5888 return ret;
5889 }
5890 }
5813 5891
5814 WARN_ON(num_bytes < root->sectorsize); 5892 WARN_ON(num_bytes < root->sectorsize);
5815 ret = find_free_extent(trans, root, num_bytes, empty_size, 5893 ret = find_free_extent(trans, root, num_bytes, empty_size,
@@ -5821,8 +5899,12 @@ again:
5821 num_bytes = num_bytes >> 1; 5899 num_bytes = num_bytes >> 1;
5822 num_bytes = num_bytes & ~(root->sectorsize - 1); 5900 num_bytes = num_bytes & ~(root->sectorsize - 1);
5823 num_bytes = max(num_bytes, min_alloc_size); 5901 num_bytes = max(num_bytes, min_alloc_size);
5824 do_chunk_alloc(trans, root->fs_info->extent_root, 5902 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
5825 num_bytes, data, CHUNK_ALLOC_FORCE); 5903 num_bytes, data, CHUNK_ALLOC_FORCE);
5904 if (ret < 0 && ret != -ENOSPC) {
5905 btrfs_abort_transaction(trans, root, ret);
5906 return ret;
5907 }
5826 if (num_bytes == min_alloc_size) 5908 if (num_bytes == min_alloc_size)
5827 final_tried = true; 5909 final_tried = true;
5828 goto again; 5910 goto again;
@@ -5913,7 +5995,10 @@ static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
5913 path->leave_spinning = 1; 5995 path->leave_spinning = 1;
5914 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, 5996 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
5915 ins, size); 5997 ins, size);
5916 BUG_ON(ret); 5998 if (ret) {
5999 btrfs_free_path(path);
6000 return ret;
6001 }
5917 6002
5918 leaf = path->nodes[0]; 6003 leaf = path->nodes[0];
5919 extent_item = btrfs_item_ptr(leaf, path->slots[0], 6004 extent_item = btrfs_item_ptr(leaf, path->slots[0],
@@ -5943,7 +6028,7 @@ static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
5943 btrfs_free_path(path); 6028 btrfs_free_path(path);
5944 6029
5945 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1); 6030 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
5946 if (ret) { 6031 if (ret) { /* -ENOENT, logic error */
5947 printk(KERN_ERR "btrfs update block group failed for %llu " 6032 printk(KERN_ERR "btrfs update block group failed for %llu "
5948 "%llu\n", (unsigned long long)ins->objectid, 6033 "%llu\n", (unsigned long long)ins->objectid,
5949 (unsigned long long)ins->offset); 6034 (unsigned long long)ins->offset);
@@ -5974,7 +6059,10 @@ static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
5974 path->leave_spinning = 1; 6059 path->leave_spinning = 1;
5975 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, 6060 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
5976 ins, size); 6061 ins, size);
5977 BUG_ON(ret); 6062 if (ret) {
6063 btrfs_free_path(path);
6064 return ret;
6065 }
5978 6066
5979 leaf = path->nodes[0]; 6067 leaf = path->nodes[0];
5980 extent_item = btrfs_item_ptr(leaf, path->slots[0], 6068 extent_item = btrfs_item_ptr(leaf, path->slots[0],
@@ -6004,7 +6092,7 @@ static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
6004 btrfs_free_path(path); 6092 btrfs_free_path(path);
6005 6093
6006 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1); 6094 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
6007 if (ret) { 6095 if (ret) { /* -ENOENT, logic error */
6008 printk(KERN_ERR "btrfs update block group failed for %llu " 6096 printk(KERN_ERR "btrfs update block group failed for %llu "
6009 "%llu\n", (unsigned long long)ins->objectid, 6097 "%llu\n", (unsigned long long)ins->objectid,
6010 (unsigned long long)ins->offset); 6098 (unsigned long long)ins->offset);
@@ -6052,28 +6140,28 @@ int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
6052 if (!caching_ctl) { 6140 if (!caching_ctl) {
6053 BUG_ON(!block_group_cache_done(block_group)); 6141 BUG_ON(!block_group_cache_done(block_group));
6054 ret = btrfs_remove_free_space(block_group, start, num_bytes); 6142 ret = btrfs_remove_free_space(block_group, start, num_bytes);
6055 BUG_ON(ret); 6143 BUG_ON(ret); /* -ENOMEM */
6056 } else { 6144 } else {
6057 mutex_lock(&caching_ctl->mutex); 6145 mutex_lock(&caching_ctl->mutex);
6058 6146
6059 if (start >= caching_ctl->progress) { 6147 if (start >= caching_ctl->progress) {
6060 ret = add_excluded_extent(root, start, num_bytes); 6148 ret = add_excluded_extent(root, start, num_bytes);
6061 BUG_ON(ret); 6149 BUG_ON(ret); /* -ENOMEM */
6062 } else if (start + num_bytes <= caching_ctl->progress) { 6150 } else if (start + num_bytes <= caching_ctl->progress) {
6063 ret = btrfs_remove_free_space(block_group, 6151 ret = btrfs_remove_free_space(block_group,
6064 start, num_bytes); 6152 start, num_bytes);
6065 BUG_ON(ret); 6153 BUG_ON(ret); /* -ENOMEM */
6066 } else { 6154 } else {
6067 num_bytes = caching_ctl->progress - start; 6155 num_bytes = caching_ctl->progress - start;
6068 ret = btrfs_remove_free_space(block_group, 6156 ret = btrfs_remove_free_space(block_group,
6069 start, num_bytes); 6157 start, num_bytes);
6070 BUG_ON(ret); 6158 BUG_ON(ret); /* -ENOMEM */
6071 6159
6072 start = caching_ctl->progress; 6160 start = caching_ctl->progress;
6073 num_bytes = ins->objectid + ins->offset - 6161 num_bytes = ins->objectid + ins->offset -
6074 caching_ctl->progress; 6162 caching_ctl->progress;
6075 ret = add_excluded_extent(root, start, num_bytes); 6163 ret = add_excluded_extent(root, start, num_bytes);
6076 BUG_ON(ret); 6164 BUG_ON(ret); /* -ENOMEM */
6077 } 6165 }
6078 6166
6079 mutex_unlock(&caching_ctl->mutex); 6167 mutex_unlock(&caching_ctl->mutex);
@@ -6082,7 +6170,7 @@ int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
6082 6170
6083 ret = btrfs_update_reserved_bytes(block_group, ins->offset, 6171 ret = btrfs_update_reserved_bytes(block_group, ins->offset,
6084 RESERVE_ALLOC_NO_ACCOUNT); 6172 RESERVE_ALLOC_NO_ACCOUNT);
6085 BUG_ON(ret); 6173 BUG_ON(ret); /* logic error */
6086 btrfs_put_block_group(block_group); 6174 btrfs_put_block_group(block_group);
6087 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid, 6175 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
6088 0, owner, offset, ins, 1); 6176 0, owner, offset, ins, 1);
@@ -6218,7 +6306,7 @@ struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
6218 6306
6219 buf = btrfs_init_new_buffer(trans, root, ins.objectid, 6307 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
6220 blocksize, level); 6308 blocksize, level);
6221 BUG_ON(IS_ERR(buf)); 6309 BUG_ON(IS_ERR(buf)); /* -ENOMEM */
6222 6310
6223 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) { 6311 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
6224 if (parent == 0) 6312 if (parent == 0)
@@ -6230,7 +6318,7 @@ struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
6230 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) { 6318 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
6231 struct btrfs_delayed_extent_op *extent_op; 6319 struct btrfs_delayed_extent_op *extent_op;
6232 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS); 6320 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
6233 BUG_ON(!extent_op); 6321 BUG_ON(!extent_op); /* -ENOMEM */
6234 if (key) 6322 if (key)
6235 memcpy(&extent_op->key, key, sizeof(extent_op->key)); 6323 memcpy(&extent_op->key, key, sizeof(extent_op->key));
6236 else 6324 else
@@ -6245,7 +6333,7 @@ struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
6245 ins.offset, parent, root_objectid, 6333 ins.offset, parent, root_objectid,
6246 level, BTRFS_ADD_DELAYED_EXTENT, 6334 level, BTRFS_ADD_DELAYED_EXTENT,
6247 extent_op, for_cow); 6335 extent_op, for_cow);
6248 BUG_ON(ret); 6336 BUG_ON(ret); /* -ENOMEM */
6249 } 6337 }
6250 return buf; 6338 return buf;
6251} 6339}
@@ -6315,7 +6403,9 @@ static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
6315 /* We don't lock the tree block, it's OK to be racy here */ 6403 /* We don't lock the tree block, it's OK to be racy here */
6316 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize, 6404 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
6317 &refs, &flags); 6405 &refs, &flags);
6318 BUG_ON(ret); 6406 /* We don't care about errors in readahead. */
6407 if (ret < 0)
6408 continue;
6319 BUG_ON(refs == 0); 6409 BUG_ON(refs == 0);
6320 6410
6321 if (wc->stage == DROP_REFERENCE) { 6411 if (wc->stage == DROP_REFERENCE) {
@@ -6382,7 +6472,9 @@ static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
6382 eb->start, eb->len, 6472 eb->start, eb->len,
6383 &wc->refs[level], 6473 &wc->refs[level],
6384 &wc->flags[level]); 6474 &wc->flags[level]);
6385 BUG_ON(ret); 6475 BUG_ON(ret == -ENOMEM);
6476 if (ret)
6477 return ret;
6386 BUG_ON(wc->refs[level] == 0); 6478 BUG_ON(wc->refs[level] == 0);
6387 } 6479 }
6388 6480
@@ -6401,12 +6493,12 @@ static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
6401 if (!(wc->flags[level] & flag)) { 6493 if (!(wc->flags[level] & flag)) {
6402 BUG_ON(!path->locks[level]); 6494 BUG_ON(!path->locks[level]);
6403 ret = btrfs_inc_ref(trans, root, eb, 1, wc->for_reloc); 6495 ret = btrfs_inc_ref(trans, root, eb, 1, wc->for_reloc);
6404 BUG_ON(ret); 6496 BUG_ON(ret); /* -ENOMEM */
6405 ret = btrfs_dec_ref(trans, root, eb, 0, wc->for_reloc); 6497 ret = btrfs_dec_ref(trans, root, eb, 0, wc->for_reloc);
6406 BUG_ON(ret); 6498 BUG_ON(ret); /* -ENOMEM */
6407 ret = btrfs_set_disk_extent_flags(trans, root, eb->start, 6499 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
6408 eb->len, flag, 0); 6500 eb->len, flag, 0);
6409 BUG_ON(ret); 6501 BUG_ON(ret); /* -ENOMEM */
6410 wc->flags[level] |= flag; 6502 wc->flags[level] |= flag;
6411 } 6503 }
6412 6504
@@ -6478,7 +6570,11 @@ static noinline int do_walk_down(struct btrfs_trans_handle *trans,
6478 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize, 6570 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
6479 &wc->refs[level - 1], 6571 &wc->refs[level - 1],
6480 &wc->flags[level - 1]); 6572 &wc->flags[level - 1]);
6481 BUG_ON(ret); 6573 if (ret < 0) {
6574 btrfs_tree_unlock(next);
6575 return ret;
6576 }
6577
6482 BUG_ON(wc->refs[level - 1] == 0); 6578 BUG_ON(wc->refs[level - 1] == 0);
6483 *lookup_info = 0; 6579 *lookup_info = 0;
6484 6580
@@ -6547,7 +6643,7 @@ skip:
6547 6643
6548 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent, 6644 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
6549 root->root_key.objectid, level - 1, 0, 0); 6645 root->root_key.objectid, level - 1, 0, 0);
6550 BUG_ON(ret); 6646 BUG_ON(ret); /* -ENOMEM */
6551 } 6647 }
6552 btrfs_tree_unlock(next); 6648 btrfs_tree_unlock(next);
6553 free_extent_buffer(next); 6649 free_extent_buffer(next);
@@ -6605,7 +6701,10 @@ static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
6605 eb->start, eb->len, 6701 eb->start, eb->len,
6606 &wc->refs[level], 6702 &wc->refs[level],
6607 &wc->flags[level]); 6703 &wc->flags[level]);
6608 BUG_ON(ret); 6704 if (ret < 0) {
6705 btrfs_tree_unlock_rw(eb, path->locks[level]);
6706 return ret;
6707 }
6609 BUG_ON(wc->refs[level] == 0); 6708 BUG_ON(wc->refs[level] == 0);
6610 if (wc->refs[level] == 1) { 6709 if (wc->refs[level] == 1) {
6611 btrfs_tree_unlock_rw(eb, path->locks[level]); 6710 btrfs_tree_unlock_rw(eb, path->locks[level]);
@@ -6625,7 +6724,7 @@ static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
6625 else 6724 else
6626 ret = btrfs_dec_ref(trans, root, eb, 0, 6725 ret = btrfs_dec_ref(trans, root, eb, 0,
6627 wc->for_reloc); 6726 wc->for_reloc);
6628 BUG_ON(ret); 6727 BUG_ON(ret); /* -ENOMEM */
6629 } 6728 }
6630 /* make block locked assertion in clean_tree_block happy */ 6729 /* make block locked assertion in clean_tree_block happy */
6631 if (!path->locks[level] && 6730 if (!path->locks[level] &&
@@ -6762,7 +6861,10 @@ int btrfs_drop_snapshot(struct btrfs_root *root,
6762 } 6861 }
6763 6862
6764 trans = btrfs_start_transaction(tree_root, 0); 6863 trans = btrfs_start_transaction(tree_root, 0);
6765 BUG_ON(IS_ERR(trans)); 6864 if (IS_ERR(trans)) {
6865 err = PTR_ERR(trans);
6866 goto out_free;
6867 }
6766 6868
6767 if (block_rsv) 6869 if (block_rsv)
6768 trans->block_rsv = block_rsv; 6870 trans->block_rsv = block_rsv;
@@ -6787,7 +6889,7 @@ int btrfs_drop_snapshot(struct btrfs_root *root,
6787 path->lowest_level = 0; 6889 path->lowest_level = 0;
6788 if (ret < 0) { 6890 if (ret < 0) {
6789 err = ret; 6891 err = ret;
6790 goto out_free; 6892 goto out_end_trans;
6791 } 6893 }
6792 WARN_ON(ret > 0); 6894 WARN_ON(ret > 0);
6793 6895
@@ -6807,7 +6909,10 @@ int btrfs_drop_snapshot(struct btrfs_root *root,
6807 path->nodes[level]->len, 6909 path->nodes[level]->len,
6808 &wc->refs[level], 6910 &wc->refs[level],
6809 &wc->flags[level]); 6911 &wc->flags[level]);
6810 BUG_ON(ret); 6912 if (ret < 0) {
6913 err = ret;
6914 goto out_end_trans;
6915 }
6811 BUG_ON(wc->refs[level] == 0); 6916 BUG_ON(wc->refs[level] == 0);
6812 6917
6813 if (level == root_item->drop_level) 6918 if (level == root_item->drop_level)
@@ -6858,26 +6963,40 @@ int btrfs_drop_snapshot(struct btrfs_root *root,
6858 ret = btrfs_update_root(trans, tree_root, 6963 ret = btrfs_update_root(trans, tree_root,
6859 &root->root_key, 6964 &root->root_key,
6860 root_item); 6965 root_item);
6861 BUG_ON(ret); 6966 if (ret) {
6967 btrfs_abort_transaction(trans, tree_root, ret);
6968 err = ret;
6969 goto out_end_trans;
6970 }
6862 6971
6863 btrfs_end_transaction_throttle(trans, tree_root); 6972 btrfs_end_transaction_throttle(trans, tree_root);
6864 trans = btrfs_start_transaction(tree_root, 0); 6973 trans = btrfs_start_transaction(tree_root, 0);
6865 BUG_ON(IS_ERR(trans)); 6974 if (IS_ERR(trans)) {
6975 err = PTR_ERR(trans);
6976 goto out_free;
6977 }
6866 if (block_rsv) 6978 if (block_rsv)
6867 trans->block_rsv = block_rsv; 6979 trans->block_rsv = block_rsv;
6868 } 6980 }
6869 } 6981 }
6870 btrfs_release_path(path); 6982 btrfs_release_path(path);
6871 BUG_ON(err); 6983 if (err)
6984 goto out_end_trans;
6872 6985
6873 ret = btrfs_del_root(trans, tree_root, &root->root_key); 6986 ret = btrfs_del_root(trans, tree_root, &root->root_key);
6874 BUG_ON(ret); 6987 if (ret) {
6988 btrfs_abort_transaction(trans, tree_root, ret);
6989 goto out_end_trans;
6990 }
6875 6991
6876 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) { 6992 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
6877 ret = btrfs_find_last_root(tree_root, root->root_key.objectid, 6993 ret = btrfs_find_last_root(tree_root, root->root_key.objectid,
6878 NULL, NULL); 6994 NULL, NULL);
6879 BUG_ON(ret < 0); 6995 if (ret < 0) {
6880 if (ret > 0) { 6996 btrfs_abort_transaction(trans, tree_root, ret);
6997 err = ret;
6998 goto out_end_trans;
6999 } else if (ret > 0) {
6881 /* if we fail to delete the orphan item this time 7000 /* if we fail to delete the orphan item this time
6882 * around, it'll get picked up the next time. 7001 * around, it'll get picked up the next time.
6883 * 7002 *
@@ -6895,8 +7014,9 @@ int btrfs_drop_snapshot(struct btrfs_root *root,
6895 free_extent_buffer(root->commit_root); 7014 free_extent_buffer(root->commit_root);
6896 kfree(root); 7015 kfree(root);
6897 } 7016 }
6898out_free: 7017out_end_trans:
6899 btrfs_end_transaction_throttle(trans, tree_root); 7018 btrfs_end_transaction_throttle(trans, tree_root);
7019out_free:
6900 kfree(wc); 7020 kfree(wc);
6901 btrfs_free_path(path); 7021 btrfs_free_path(path);
6902out: 7022out:
@@ -7099,12 +7219,16 @@ int btrfs_set_block_group_ro(struct btrfs_root *root,
7099 BUG_ON(cache->ro); 7219 BUG_ON(cache->ro);
7100 7220
7101 trans = btrfs_join_transaction(root); 7221 trans = btrfs_join_transaction(root);
7102 BUG_ON(IS_ERR(trans)); 7222 if (IS_ERR(trans))
7223 return PTR_ERR(trans);
7103 7224
7104 alloc_flags = update_block_group_flags(root, cache->flags); 7225 alloc_flags = update_block_group_flags(root, cache->flags);
7105 if (alloc_flags != cache->flags) 7226 if (alloc_flags != cache->flags) {
7106 do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags, 7227 ret = do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags,
7107 CHUNK_ALLOC_FORCE); 7228 CHUNK_ALLOC_FORCE);
7229 if (ret < 0)
7230 goto out;
7231 }
7108 7232
7109 ret = set_block_group_ro(cache, 0); 7233 ret = set_block_group_ro(cache, 0);
7110 if (!ret) 7234 if (!ret)
@@ -7567,7 +7691,7 @@ int btrfs_read_block_groups(struct btrfs_root *root)
7567 ret = update_space_info(info, cache->flags, found_key.offset, 7691 ret = update_space_info(info, cache->flags, found_key.offset,
7568 btrfs_block_group_used(&cache->item), 7692 btrfs_block_group_used(&cache->item),
7569 &space_info); 7693 &space_info);
7570 BUG_ON(ret); 7694 BUG_ON(ret); /* -ENOMEM */
7571 cache->space_info = space_info; 7695 cache->space_info = space_info;
7572 spin_lock(&cache->space_info->lock); 7696 spin_lock(&cache->space_info->lock);
7573 cache->space_info->bytes_readonly += cache->bytes_super; 7697 cache->space_info->bytes_readonly += cache->bytes_super;
@@ -7576,7 +7700,7 @@ int btrfs_read_block_groups(struct btrfs_root *root)
7576 __link_block_group(space_info, cache); 7700 __link_block_group(space_info, cache);
7577 7701
7578 ret = btrfs_add_block_group_cache(root->fs_info, cache); 7702 ret = btrfs_add_block_group_cache(root->fs_info, cache);
7579 BUG_ON(ret); 7703 BUG_ON(ret); /* Logic error */
7580 7704
7581 set_avail_alloc_bits(root->fs_info, cache->flags); 7705 set_avail_alloc_bits(root->fs_info, cache->flags);
7582 if (btrfs_chunk_readonly(root, cache->key.objectid)) 7706 if (btrfs_chunk_readonly(root, cache->key.objectid))
@@ -7658,7 +7782,7 @@ int btrfs_make_block_group(struct btrfs_trans_handle *trans,
7658 7782
7659 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used, 7783 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
7660 &cache->space_info); 7784 &cache->space_info);
7661 BUG_ON(ret); 7785 BUG_ON(ret); /* -ENOMEM */
7662 update_global_block_rsv(root->fs_info); 7786 update_global_block_rsv(root->fs_info);
7663 7787
7664 spin_lock(&cache->space_info->lock); 7788 spin_lock(&cache->space_info->lock);
@@ -7668,11 +7792,14 @@ int btrfs_make_block_group(struct btrfs_trans_handle *trans,
7668 __link_block_group(cache->space_info, cache); 7792 __link_block_group(cache->space_info, cache);
7669 7793
7670 ret = btrfs_add_block_group_cache(root->fs_info, cache); 7794 ret = btrfs_add_block_group_cache(root->fs_info, cache);
7671 BUG_ON(ret); 7795 BUG_ON(ret); /* Logic error */
7672 7796
7673 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item, 7797 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
7674 sizeof(cache->item)); 7798 sizeof(cache->item));
7675 BUG_ON(ret); 7799 if (ret) {
7800 btrfs_abort_transaction(trans, extent_root, ret);
7801 return ret;
7802 }
7676 7803
7677 set_avail_alloc_bits(extent_root->fs_info, type); 7804 set_avail_alloc_bits(extent_root->fs_info, type);
7678 7805
@@ -7753,7 +7880,10 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
7753 inode = lookup_free_space_inode(tree_root, block_group, path); 7880 inode = lookup_free_space_inode(tree_root, block_group, path);
7754 if (!IS_ERR(inode)) { 7881 if (!IS_ERR(inode)) {
7755 ret = btrfs_orphan_add(trans, inode); 7882 ret = btrfs_orphan_add(trans, inode);
7756 BUG_ON(ret); 7883 if (ret) {
7884 btrfs_add_delayed_iput(inode);
7885 goto out;
7886 }
7757 clear_nlink(inode); 7887 clear_nlink(inode);
7758 /* One for the block groups ref */ 7888 /* One for the block groups ref */
7759 spin_lock(&block_group->lock); 7889 spin_lock(&block_group->lock);
diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c
index ffa7cc3370c7..4c3ce7a0a7a4 100644
--- a/fs/btrfs/extent_io.c
+++ b/fs/btrfs/extent_io.c
@@ -1244,7 +1244,7 @@ static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
1244 1244
1245 while (index <= end_index) { 1245 while (index <= end_index) {
1246 page = find_get_page(tree->mapping, index); 1246 page = find_get_page(tree->mapping, index);
1247 BUG_ON(!page); 1247 BUG_ON(!page); /* Pages should be in the extent_io_tree */
1248 set_page_writeback(page); 1248 set_page_writeback(page);
1249 page_cache_release(page); 1249 page_cache_release(page);
1250 index++; 1250 index++;
@@ -1523,7 +1523,7 @@ again:
1523 goto out_failed; 1523 goto out_failed;
1524 } 1524 }
1525 } 1525 }
1526 BUG_ON(ret); 1526 BUG_ON(ret); /* Only valid values are 0 and -EAGAIN */
1527 1527
1528 /* step three, lock the state bits for the whole range */ 1528 /* step three, lock the state bits for the whole range */
1529 lock_extent_bits(tree, delalloc_start, delalloc_end, 0, &cached_state); 1529 lock_extent_bits(tree, delalloc_start, delalloc_end, 0, &cached_state);
@@ -2200,7 +2200,6 @@ int end_extent_writepage(struct page *page, int err, u64 start, u64 end)
2200 /* Writeback already completed */ 2200 /* Writeback already completed */
2201 if (ret == 0) 2201 if (ret == 0)
2202 return 1; 2202 return 1;
2203 BUG_ON(ret < 0);
2204 } 2203 }
2205 2204
2206 if (!uptodate) { 2205 if (!uptodate) {
@@ -2353,7 +2352,6 @@ error_handled:
2353 if (ret == 0) 2352 if (ret == 0)
2354 goto error_handled; 2353 goto error_handled;
2355 } 2354 }
2356 BUG_ON(ret < 0);
2357 } 2355 }
2358 2356
2359 if (uptodate) { 2357 if (uptodate) {
@@ -2405,6 +2403,10 @@ btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
2405 return bio; 2403 return bio;
2406} 2404}
2407 2405
2406/*
2407 * Since writes are async, they will only return -ENOMEM.
2408 * Reads can return the full range of I/O error conditions.
2409 */
2408static int __must_check submit_one_bio(int rw, struct bio *bio, 2410static int __must_check submit_one_bio(int rw, struct bio *bio,
2409 int mirror_num, unsigned long bio_flags) 2411 int mirror_num, unsigned long bio_flags)
2410{ 2412{
@@ -2477,7 +2479,8 @@ static int submit_extent_page(int rw, struct extent_io_tree *tree,
2477 bio_add_page(bio, page, page_size, offset) < page_size) { 2479 bio_add_page(bio, page, page_size, offset) < page_size) {
2478 ret = submit_one_bio(rw, bio, mirror_num, 2480 ret = submit_one_bio(rw, bio, mirror_num,
2479 prev_bio_flags); 2481 prev_bio_flags);
2480 BUG_ON(ret < 0); 2482 if (ret < 0)
2483 return ret;
2481 bio = NULL; 2484 bio = NULL;
2482 } else { 2485 } else {
2483 return 0; 2486 return 0;
@@ -2498,10 +2501,8 @@ static int submit_extent_page(int rw, struct extent_io_tree *tree,
2498 2501
2499 if (bio_ret) 2502 if (bio_ret)
2500 *bio_ret = bio; 2503 *bio_ret = bio;
2501 else { 2504 else
2502 ret = submit_one_bio(rw, bio, mirror_num, bio_flags); 2505 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
2503 BUG_ON(ret < 0);
2504 }
2505 2506
2506 return ret; 2507 return ret;
2507} 2508}
@@ -2525,6 +2526,7 @@ static void set_page_extent_head(struct page *page, unsigned long len)
2525 * basic readpage implementation. Locked extent state structs are inserted 2526 * basic readpage implementation. Locked extent state structs are inserted
2526 * into the tree that are removed when the IO is done (by the end_io 2527 * into the tree that are removed when the IO is done (by the end_io
2527 * handlers) 2528 * handlers)
2529 * XXX JDM: This needs looking at to ensure proper page locking
2528 */ 2530 */
2529static int __extent_read_full_page(struct extent_io_tree *tree, 2531static int __extent_read_full_page(struct extent_io_tree *tree,
2530 struct page *page, 2532 struct page *page,
@@ -2687,6 +2689,7 @@ static int __extent_read_full_page(struct extent_io_tree *tree,
2687 end_bio_extent_readpage, mirror_num, 2689 end_bio_extent_readpage, mirror_num,
2688 *bio_flags, 2690 *bio_flags,
2689 this_bio_flag); 2691 this_bio_flag);
2692 BUG_ON(ret == -ENOMEM);
2690 nr++; 2693 nr++;
2691 *bio_flags = this_bio_flag; 2694 *bio_flags = this_bio_flag;
2692 } 2695 }
@@ -2713,10 +2716,8 @@ int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
2713 2716
2714 ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num, 2717 ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
2715 &bio_flags); 2718 &bio_flags);
2716 if (bio) { 2719 if (bio)
2717 ret = submit_one_bio(READ, bio, mirror_num, bio_flags); 2720 ret = submit_one_bio(READ, bio, mirror_num, bio_flags);
2718 BUG_ON(ret < 0);
2719 }
2720 return ret; 2721 return ret;
2721} 2722}
2722 2723
@@ -2830,7 +2831,11 @@ static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2830 delalloc_end, 2831 delalloc_end,
2831 &page_started, 2832 &page_started,
2832 &nr_written); 2833 &nr_written);
2833 BUG_ON(ret); 2834 /* File system has been set read-only */
2835 if (ret) {
2836 SetPageError(page);
2837 goto done;
2838 }
2834 /* 2839 /*
2835 * delalloc_end is already one less than the total 2840 * delalloc_end is already one less than the total
2836 * length, so we don't subtract one from 2841 * length, so we don't subtract one from
@@ -3141,7 +3146,7 @@ static void flush_epd_write_bio(struct extent_page_data *epd)
3141 rw = WRITE_SYNC; 3146 rw = WRITE_SYNC;
3142 3147
3143 ret = submit_one_bio(rw, epd->bio, 0, 0); 3148 ret = submit_one_bio(rw, epd->bio, 0, 0);
3144 BUG_ON(ret < 0); 3149 BUG_ON(ret < 0); /* -ENOMEM */
3145 epd->bio = NULL; 3150 epd->bio = NULL;
3146 } 3151 }
3147} 3152}
@@ -3257,10 +3262,8 @@ int extent_readpages(struct extent_io_tree *tree,
3257 page_cache_release(page); 3262 page_cache_release(page);
3258 } 3263 }
3259 BUG_ON(!list_empty(pages)); 3264 BUG_ON(!list_empty(pages));
3260 if (bio) { 3265 if (bio)
3261 int ret = submit_one_bio(READ, bio, 0, bio_flags); 3266 return submit_one_bio(READ, bio, 0, bio_flags);
3262 BUG_ON(ret < 0);
3263 }
3264 return 0; 3267 return 0;
3265} 3268}
3266 3269
@@ -4090,7 +4093,8 @@ int read_extent_buffer_pages(struct extent_io_tree *tree,
4090 4093
4091 if (bio) { 4094 if (bio) {
4092 err = submit_one_bio(READ, bio, mirror_num, bio_flags); 4095 err = submit_one_bio(READ, bio, mirror_num, bio_flags);
4093 BUG_ON(err < 0); 4096 if (err)
4097 return err;
4094 } 4098 }
4095 4099
4096 if (ret || wait != WAIT_COMPLETE) 4100 if (ret || wait != WAIT_COMPLETE)
diff --git a/fs/btrfs/file-item.c b/fs/btrfs/file-item.c
index 89af104c7569..a14dbca5974e 100644
--- a/fs/btrfs/file-item.c
+++ b/fs/btrfs/file-item.c
@@ -59,7 +59,7 @@ int btrfs_insert_file_extent(struct btrfs_trans_handle *trans,
59 sizeof(*item)); 59 sizeof(*item));
60 if (ret < 0) 60 if (ret < 0)
61 goto out; 61 goto out;
62 BUG_ON(ret); 62 BUG_ON(ret); /* Can't happen */
63 leaf = path->nodes[0]; 63 leaf = path->nodes[0];
64 item = btrfs_item_ptr(leaf, path->slots[0], 64 item = btrfs_item_ptr(leaf, path->slots[0],
65 struct btrfs_file_extent_item); 65 struct btrfs_file_extent_item);
@@ -431,7 +431,7 @@ int btrfs_csum_one_bio(struct btrfs_root *root, struct inode *inode,
431 offset = page_offset(bvec->bv_page) + bvec->bv_offset; 431 offset = page_offset(bvec->bv_page) + bvec->bv_offset;
432 432
433 ordered = btrfs_lookup_ordered_extent(inode, offset); 433 ordered = btrfs_lookup_ordered_extent(inode, offset);
434 BUG_ON(!ordered); 434 BUG_ON(!ordered); /* Logic error */
435 sums->bytenr = ordered->start; 435 sums->bytenr = ordered->start;
436 436
437 while (bio_index < bio->bi_vcnt) { 437 while (bio_index < bio->bi_vcnt) {
@@ -450,11 +450,11 @@ int btrfs_csum_one_bio(struct btrfs_root *root, struct inode *inode,
450 450
451 sums = kzalloc(btrfs_ordered_sum_size(root, bytes_left), 451 sums = kzalloc(btrfs_ordered_sum_size(root, bytes_left),
452 GFP_NOFS); 452 GFP_NOFS);
453 BUG_ON(!sums); 453 BUG_ON(!sums); /* -ENOMEM */
454 sector_sum = sums->sums; 454 sector_sum = sums->sums;
455 sums->len = bytes_left; 455 sums->len = bytes_left;
456 ordered = btrfs_lookup_ordered_extent(inode, offset); 456 ordered = btrfs_lookup_ordered_extent(inode, offset);
457 BUG_ON(!ordered); 457 BUG_ON(!ordered); /* Logic error */
458 sums->bytenr = ordered->start; 458 sums->bytenr = ordered->start;
459 } 459 }
460 460
@@ -643,7 +643,10 @@ int btrfs_del_csums(struct btrfs_trans_handle *trans,
643 * item changed size or key 643 * item changed size or key
644 */ 644 */
645 ret = btrfs_split_item(trans, root, path, &key, offset); 645 ret = btrfs_split_item(trans, root, path, &key, offset);
646 BUG_ON(ret && ret != -EAGAIN); 646 if (ret && ret != -EAGAIN) {
647 btrfs_abort_transaction(trans, root, ret);
648 goto out;
649 }
647 650
648 key.offset = end_byte - 1; 651 key.offset = end_byte - 1;
649 } else { 652 } else {
diff --git a/fs/btrfs/file.c b/fs/btrfs/file.c
index 0eb80cc4ec81..d83260d7498f 100644
--- a/fs/btrfs/file.c
+++ b/fs/btrfs/file.c
@@ -452,7 +452,7 @@ int btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end,
452 split = alloc_extent_map(); 452 split = alloc_extent_map();
453 if (!split2) 453 if (!split2)
454 split2 = alloc_extent_map(); 454 split2 = alloc_extent_map();
455 BUG_ON(!split || !split2); 455 BUG_ON(!split || !split2); /* -ENOMEM */
456 456
457 write_lock(&em_tree->lock); 457 write_lock(&em_tree->lock);
458 em = lookup_extent_mapping(em_tree, start, len); 458 em = lookup_extent_mapping(em_tree, start, len);
@@ -494,7 +494,7 @@ int btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end,
494 split->flags = flags; 494 split->flags = flags;
495 split->compress_type = em->compress_type; 495 split->compress_type = em->compress_type;
496 ret = add_extent_mapping(em_tree, split); 496 ret = add_extent_mapping(em_tree, split);
497 BUG_ON(ret); 497 BUG_ON(ret); /* Logic error */
498 free_extent_map(split); 498 free_extent_map(split);
499 split = split2; 499 split = split2;
500 split2 = NULL; 500 split2 = NULL;
@@ -520,7 +520,7 @@ int btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end,
520 } 520 }
521 521
522 ret = add_extent_mapping(em_tree, split); 522 ret = add_extent_mapping(em_tree, split);
523 BUG_ON(ret); 523 BUG_ON(ret); /* Logic error */
524 free_extent_map(split); 524 free_extent_map(split);
525 split = NULL; 525 split = NULL;
526 } 526 }
@@ -679,7 +679,7 @@ next_slot:
679 root->root_key.objectid, 679 root->root_key.objectid,
680 new_key.objectid, 680 new_key.objectid,
681 start - extent_offset, 0); 681 start - extent_offset, 0);
682 BUG_ON(ret); 682 BUG_ON(ret); /* -ENOMEM */
683 *hint_byte = disk_bytenr; 683 *hint_byte = disk_bytenr;
684 } 684 }
685 key.offset = start; 685 key.offset = start;
@@ -754,7 +754,7 @@ next_slot:
754 root->root_key.objectid, 754 root->root_key.objectid,
755 key.objectid, key.offset - 755 key.objectid, key.offset -
756 extent_offset, 0); 756 extent_offset, 0);
757 BUG_ON(ret); 757 BUG_ON(ret); /* -ENOMEM */
758 inode_sub_bytes(inode, 758 inode_sub_bytes(inode,
759 extent_end - key.offset); 759 extent_end - key.offset);
760 *hint_byte = disk_bytenr; 760 *hint_byte = disk_bytenr;
@@ -770,7 +770,10 @@ next_slot:
770 770
771 ret = btrfs_del_items(trans, root, path, del_slot, 771 ret = btrfs_del_items(trans, root, path, del_slot,
772 del_nr); 772 del_nr);
773 BUG_ON(ret); 773 if (ret) {
774 btrfs_abort_transaction(trans, root, ret);
775 goto out;
776 }
774 777
775 del_nr = 0; 778 del_nr = 0;
776 del_slot = 0; 779 del_slot = 0;
@@ -782,11 +785,13 @@ next_slot:
782 BUG_ON(1); 785 BUG_ON(1);
783 } 786 }
784 787
785 if (del_nr > 0) { 788 if (!ret && del_nr > 0) {
786 ret = btrfs_del_items(trans, root, path, del_slot, del_nr); 789 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
787 BUG_ON(ret); 790 if (ret)
791 btrfs_abort_transaction(trans, root, ret);
788 } 792 }
789 793
794out:
790 btrfs_free_path(path); 795 btrfs_free_path(path);
791 return ret; 796 return ret;
792} 797}
@@ -944,7 +949,10 @@ again:
944 btrfs_release_path(path); 949 btrfs_release_path(path);
945 goto again; 950 goto again;
946 } 951 }
947 BUG_ON(ret < 0); 952 if (ret < 0) {
953 btrfs_abort_transaction(trans, root, ret);
954 goto out;
955 }
948 956
949 leaf = path->nodes[0]; 957 leaf = path->nodes[0];
950 fi = btrfs_item_ptr(leaf, path->slots[0] - 1, 958 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
@@ -963,7 +971,7 @@ again:
963 ret = btrfs_inc_extent_ref(trans, root, bytenr, num_bytes, 0, 971 ret = btrfs_inc_extent_ref(trans, root, bytenr, num_bytes, 0,
964 root->root_key.objectid, 972 root->root_key.objectid,
965 ino, orig_offset, 0); 973 ino, orig_offset, 0);
966 BUG_ON(ret); 974 BUG_ON(ret); /* -ENOMEM */
967 975
968 if (split == start) { 976 if (split == start) {
969 key.offset = start; 977 key.offset = start;
@@ -990,7 +998,7 @@ again:
990 ret = btrfs_free_extent(trans, root, bytenr, num_bytes, 998 ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
991 0, root->root_key.objectid, 999 0, root->root_key.objectid,
992 ino, orig_offset, 0); 1000 ino, orig_offset, 0);
993 BUG_ON(ret); 1001 BUG_ON(ret); /* -ENOMEM */
994 } 1002 }
995 other_start = 0; 1003 other_start = 0;
996 other_end = start; 1004 other_end = start;
@@ -1007,7 +1015,7 @@ again:
1007 ret = btrfs_free_extent(trans, root, bytenr, num_bytes, 1015 ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
1008 0, root->root_key.objectid, 1016 0, root->root_key.objectid,
1009 ino, orig_offset, 0); 1017 ino, orig_offset, 0);
1010 BUG_ON(ret); 1018 BUG_ON(ret); /* -ENOMEM */
1011 } 1019 }
1012 if (del_nr == 0) { 1020 if (del_nr == 0) {
1013 fi = btrfs_item_ptr(leaf, path->slots[0], 1021 fi = btrfs_item_ptr(leaf, path->slots[0],
@@ -1025,7 +1033,10 @@ again:
1025 btrfs_mark_buffer_dirty(leaf); 1033 btrfs_mark_buffer_dirty(leaf);
1026 1034
1027 ret = btrfs_del_items(trans, root, path, del_slot, del_nr); 1035 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
1028 BUG_ON(ret); 1036 if (ret < 0) {
1037 btrfs_abort_transaction(trans, root, ret);
1038 goto out;
1039 }
1029 } 1040 }
1030out: 1041out:
1031 btrfs_free_path(path); 1042 btrfs_free_path(path);
@@ -1666,7 +1677,13 @@ static long btrfs_fallocate(struct file *file, int mode,
1666 1677
1667 em = btrfs_get_extent(inode, NULL, 0, cur_offset, 1678 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
1668 alloc_end - cur_offset, 0); 1679 alloc_end - cur_offset, 0);
1669 BUG_ON(IS_ERR_OR_NULL(em)); 1680 if (IS_ERR_OR_NULL(em)) {
1681 if (!em)
1682 ret = -ENOMEM;
1683 else
1684 ret = PTR_ERR(em);
1685 break;
1686 }
1670 last_byte = min(extent_map_end(em), alloc_end); 1687 last_byte = min(extent_map_end(em), alloc_end);
1671 actual_end = min_t(u64, extent_map_end(em), offset + len); 1688 actual_end = min_t(u64, extent_map_end(em), offset + len);
1672 last_byte = (last_byte + mask) & ~mask; 1689 last_byte = (last_byte + mask) & ~mask;
diff --git a/fs/btrfs/free-space-cache.c b/fs/btrfs/free-space-cache.c
index ecbcc8711a03..054707ed5791 100644
--- a/fs/btrfs/free-space-cache.c
+++ b/fs/btrfs/free-space-cache.c
@@ -230,11 +230,13 @@ int btrfs_truncate_free_space_cache(struct btrfs_root *root,
230 230
231 if (ret) { 231 if (ret) {
232 trans->block_rsv = rsv; 232 trans->block_rsv = rsv;
233 WARN_ON(1); 233 btrfs_abort_transaction(trans, root, ret);
234 return ret; 234 return ret;
235 } 235 }
236 236
237 ret = btrfs_update_inode(trans, root, inode); 237 ret = btrfs_update_inode(trans, root, inode);
238 if (ret)
239 btrfs_abort_transaction(trans, root, ret);
238 trans->block_rsv = rsv; 240 trans->block_rsv = rsv;
239 241
240 return ret; 242 return ret;
@@ -1948,14 +1950,14 @@ again:
1948 */ 1950 */
1949 ret = btrfs_add_free_space(block_group, old_start, 1951 ret = btrfs_add_free_space(block_group, old_start,
1950 offset - old_start); 1952 offset - old_start);
1951 WARN_ON(ret); 1953 WARN_ON(ret); /* -ENOMEM */
1952 goto out; 1954 goto out;
1953 } 1955 }
1954 1956
1955 ret = remove_from_bitmap(ctl, info, &offset, &bytes); 1957 ret = remove_from_bitmap(ctl, info, &offset, &bytes);
1956 if (ret == -EAGAIN) 1958 if (ret == -EAGAIN)
1957 goto again; 1959 goto again;
1958 BUG_ON(ret); 1960 BUG_ON(ret); /* logic error */
1959out_lock: 1961out_lock:
1960 spin_unlock(&ctl->tree_lock); 1962 spin_unlock(&ctl->tree_lock);
1961out: 1963out:
@@ -2346,7 +2348,7 @@ again:
2346 rb_erase(&entry->offset_index, &ctl->free_space_offset); 2348 rb_erase(&entry->offset_index, &ctl->free_space_offset);
2347 ret = tree_insert_offset(&cluster->root, entry->offset, 2349 ret = tree_insert_offset(&cluster->root, entry->offset,
2348 &entry->offset_index, 1); 2350 &entry->offset_index, 1);
2349 BUG_ON(ret); 2351 BUG_ON(ret); /* -EEXIST; Logic error */
2350 2352
2351 trace_btrfs_setup_cluster(block_group, cluster, 2353 trace_btrfs_setup_cluster(block_group, cluster,
2352 total_found * block_group->sectorsize, 1); 2354 total_found * block_group->sectorsize, 1);
@@ -2439,7 +2441,7 @@ setup_cluster_no_bitmap(struct btrfs_block_group_cache *block_group,
2439 ret = tree_insert_offset(&cluster->root, entry->offset, 2441 ret = tree_insert_offset(&cluster->root, entry->offset,
2440 &entry->offset_index, 0); 2442 &entry->offset_index, 0);
2441 total_size += entry->bytes; 2443 total_size += entry->bytes;
2442 BUG_ON(ret); 2444 BUG_ON(ret); /* -EEXIST; Logic error */
2443 } while (node && entry != last); 2445 } while (node && entry != last);
2444 2446
2445 cluster->max_size = max_extent; 2447 cluster->max_size = max_extent;
@@ -2830,6 +2832,7 @@ u64 btrfs_find_ino_for_alloc(struct btrfs_root *fs_root)
2830 int ret; 2832 int ret;
2831 2833
2832 ret = search_bitmap(ctl, entry, &offset, &count); 2834 ret = search_bitmap(ctl, entry, &offset, &count);
2835 /* Logic error; Should be empty if it can't find anything */
2833 BUG_ON(ret); 2836 BUG_ON(ret);
2834 2837
2835 ino = offset; 2838 ino = offset;
diff --git a/fs/btrfs/inode-item.c b/fs/btrfs/inode-item.c
index c0792409bb6a..7ec58bd7c50d 100644
--- a/fs/btrfs/inode-item.c
+++ b/fs/btrfs/inode-item.c
@@ -135,6 +135,7 @@ out:
135 return ret; 135 return ret;
136} 136}
137 137
138/* Will return 0, -ENOMEM, -EMLINK, or -EEXIST or anything from the CoW path */
138int btrfs_insert_inode_ref(struct btrfs_trans_handle *trans, 139int btrfs_insert_inode_ref(struct btrfs_trans_handle *trans,
139 struct btrfs_root *root, 140 struct btrfs_root *root,
140 const char *name, int name_len, 141 const char *name, int name_len,
diff --git a/fs/btrfs/inode-map.c b/fs/btrfs/inode-map.c
index ee15d88b33d2..7ca46e6e11ae 100644
--- a/fs/btrfs/inode-map.c
+++ b/fs/btrfs/inode-map.c
@@ -178,7 +178,7 @@ static void start_caching(struct btrfs_root *root)
178 178
179 tsk = kthread_run(caching_kthread, root, "btrfs-ino-cache-%llu\n", 179 tsk = kthread_run(caching_kthread, root, "btrfs-ino-cache-%llu\n",
180 root->root_key.objectid); 180 root->root_key.objectid);
181 BUG_ON(IS_ERR(tsk)); 181 BUG_ON(IS_ERR(tsk)); /* -ENOMEM */
182} 182}
183 183
184int btrfs_find_free_ino(struct btrfs_root *root, u64 *objectid) 184int btrfs_find_free_ino(struct btrfs_root *root, u64 *objectid)
@@ -271,7 +271,7 @@ void btrfs_unpin_free_ino(struct btrfs_root *root)
271 break; 271 break;
272 272
273 info = rb_entry(n, struct btrfs_free_space, offset_index); 273 info = rb_entry(n, struct btrfs_free_space, offset_index);
274 BUG_ON(info->bitmap); 274 BUG_ON(info->bitmap); /* Logic error */
275 275
276 if (info->offset > root->cache_progress) 276 if (info->offset > root->cache_progress)
277 goto free; 277 goto free;
@@ -443,13 +443,13 @@ int btrfs_save_ino_cache(struct btrfs_root *root,
443 trans->bytes_reserved, 1); 443 trans->bytes_reserved, 1);
444again: 444again:
445 inode = lookup_free_ino_inode(root, path); 445 inode = lookup_free_ino_inode(root, path);
446 if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) { 446 if (IS_ERR(inode) && (PTR_ERR(inode) != -ENOENT || retry)) {
447 ret = PTR_ERR(inode); 447 ret = PTR_ERR(inode);
448 goto out_release; 448 goto out_release;
449 } 449 }
450 450
451 if (IS_ERR(inode)) { 451 if (IS_ERR(inode)) {
452 BUG_ON(retry); 452 BUG_ON(retry); /* Logic error */
453 retry = true; 453 retry = true;
454 454
455 ret = create_free_ino_inode(root, trans, path); 455 ret = create_free_ino_inode(root, trans, path);
@@ -460,12 +460,17 @@ again:
460 460
461 BTRFS_I(inode)->generation = 0; 461 BTRFS_I(inode)->generation = 0;
462 ret = btrfs_update_inode(trans, root, inode); 462 ret = btrfs_update_inode(trans, root, inode);
463 WARN_ON(ret); 463 if (ret) {
464 btrfs_abort_transaction(trans, root, ret);
465 goto out_put;
466 }
464 467
465 if (i_size_read(inode) > 0) { 468 if (i_size_read(inode) > 0) {
466 ret = btrfs_truncate_free_space_cache(root, trans, path, inode); 469 ret = btrfs_truncate_free_space_cache(root, trans, path, inode);
467 if (ret) 470 if (ret) {
471 btrfs_abort_transaction(trans, root, ret);
468 goto out_put; 472 goto out_put;
473 }
469 } 474 }
470 475
471 spin_lock(&root->cache_lock); 476 spin_lock(&root->cache_lock);
@@ -532,7 +537,7 @@ static int btrfs_find_highest_objectid(struct btrfs_root *root, u64 *objectid)
532 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0); 537 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
533 if (ret < 0) 538 if (ret < 0)
534 goto error; 539 goto error;
535 BUG_ON(ret == 0); 540 BUG_ON(ret == 0); /* Corruption */
536 if (path->slots[0] > 0) { 541 if (path->slots[0] > 0) {
537 slot = path->slots[0] - 1; 542 slot = path->slots[0] - 1;
538 l = path->nodes[0]; 543 l = path->nodes[0];
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 41132339e2ea..d6420cca9c8d 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -150,7 +150,6 @@ static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
150 inode_add_bytes(inode, size); 150 inode_add_bytes(inode, size);
151 ret = btrfs_insert_empty_item(trans, root, path, &key, 151 ret = btrfs_insert_empty_item(trans, root, path, &key,
152 datasize); 152 datasize);
153 BUG_ON(ret);
154 if (ret) { 153 if (ret) {
155 err = ret; 154 err = ret;
156 goto fail; 155 goto fail;
@@ -206,9 +205,9 @@ static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
206 * could end up racing with unlink. 205 * could end up racing with unlink.
207 */ 206 */
208 BTRFS_I(inode)->disk_i_size = inode->i_size; 207 BTRFS_I(inode)->disk_i_size = inode->i_size;
209 btrfs_update_inode(trans, root, inode); 208 ret = btrfs_update_inode(trans, root, inode);
210 209
211 return 0; 210 return ret;
212fail: 211fail:
213 btrfs_free_path(path); 212 btrfs_free_path(path);
214 return err; 213 return err;
@@ -250,14 +249,18 @@ static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
250 249
251 ret = btrfs_drop_extents(trans, inode, start, aligned_end, 250 ret = btrfs_drop_extents(trans, inode, start, aligned_end,
252 &hint_byte, 1); 251 &hint_byte, 1);
253 BUG_ON(ret); 252 if (ret)
253 return ret;
254 254
255 if (isize > actual_end) 255 if (isize > actual_end)
256 inline_len = min_t(u64, isize, actual_end); 256 inline_len = min_t(u64, isize, actual_end);
257 ret = insert_inline_extent(trans, root, inode, start, 257 ret = insert_inline_extent(trans, root, inode, start,
258 inline_len, compressed_size, 258 inline_len, compressed_size,
259 compress_type, compressed_pages); 259 compress_type, compressed_pages);
260 BUG_ON(ret); 260 if (ret) {
261 btrfs_abort_transaction(trans, root, ret);
262 return ret;
263 }
261 btrfs_delalloc_release_metadata(inode, end + 1 - start); 264 btrfs_delalloc_release_metadata(inode, end + 1 - start);
262 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0); 265 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
263 return 0; 266 return 0;
@@ -293,7 +296,7 @@ static noinline int add_async_extent(struct async_cow *cow,
293 struct async_extent *async_extent; 296 struct async_extent *async_extent;
294 297
295 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS); 298 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
296 BUG_ON(!async_extent); 299 BUG_ON(!async_extent); /* -ENOMEM */
297 async_extent->start = start; 300 async_extent->start = start;
298 async_extent->ram_size = ram_size; 301 async_extent->ram_size = ram_size;
299 async_extent->compressed_size = compressed_size; 302 async_extent->compressed_size = compressed_size;
@@ -433,7 +436,11 @@ again:
433cont: 436cont:
434 if (start == 0) { 437 if (start == 0) {
435 trans = btrfs_join_transaction(root); 438 trans = btrfs_join_transaction(root);
436 BUG_ON(IS_ERR(trans)); 439 if (IS_ERR(trans)) {
440 ret = PTR_ERR(trans);
441 trans = NULL;
442 goto cleanup_and_out;
443 }
437 trans->block_rsv = &root->fs_info->delalloc_block_rsv; 444 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
438 445
439 /* lets try to make an inline extent */ 446 /* lets try to make an inline extent */
@@ -450,11 +457,11 @@ cont:
450 total_compressed, 457 total_compressed,
451 compress_type, pages); 458 compress_type, pages);
452 } 459 }
453 if (ret == 0) { 460 if (ret <= 0) {
454 /* 461 /*
455 * inline extent creation worked, we don't need 462 * inline extent creation worked or returned error,
456 * to create any more async work items. Unlock 463 * we don't need to create any more async work items.
457 * and free up our temp pages. 464 * Unlock and free up our temp pages.
458 */ 465 */
459 extent_clear_unlock_delalloc(inode, 466 extent_clear_unlock_delalloc(inode,
460 &BTRFS_I(inode)->io_tree, 467 &BTRFS_I(inode)->io_tree,
@@ -547,7 +554,7 @@ cleanup_and_bail_uncompressed:
547 } 554 }
548 555
549out: 556out:
550 return 0; 557 return ret;
551 558
552free_pages_out: 559free_pages_out:
553 for (i = 0; i < nr_pages_ret; i++) { 560 for (i = 0; i < nr_pages_ret; i++) {
@@ -557,6 +564,20 @@ free_pages_out:
557 kfree(pages); 564 kfree(pages);
558 565
559 goto out; 566 goto out;
567
568cleanup_and_out:
569 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
570 start, end, NULL,
571 EXTENT_CLEAR_UNLOCK_PAGE |
572 EXTENT_CLEAR_DIRTY |
573 EXTENT_CLEAR_DELALLOC |
574 EXTENT_SET_WRITEBACK |
575 EXTENT_END_WRITEBACK);
576 if (!trans || IS_ERR(trans))
577 btrfs_error(root->fs_info, ret, "Failed to join transaction");
578 else
579 btrfs_abort_transaction(trans, root, ret);
580 goto free_pages_out;
560} 581}
561 582
562/* 583/*
@@ -606,6 +627,8 @@ retry:
606 async_extent->ram_size - 1, 627 async_extent->ram_size - 1,
607 &page_started, &nr_written, 0); 628 &page_started, &nr_written, 0);
608 629
630 /* JDM XXX */
631
609 /* 632 /*
610 * if page_started, cow_file_range inserted an 633 * if page_started, cow_file_range inserted an
611 * inline extent and took care of all the unlocking 634 * inline extent and took care of all the unlocking
@@ -628,14 +651,19 @@ retry:
628 async_extent->start + async_extent->ram_size - 1); 651 async_extent->start + async_extent->ram_size - 1);
629 652
630 trans = btrfs_join_transaction(root); 653 trans = btrfs_join_transaction(root);
631 BUG_ON(IS_ERR(trans)); 654 if (IS_ERR(trans)) {
632 trans->block_rsv = &root->fs_info->delalloc_block_rsv; 655 ret = PTR_ERR(trans);
633 ret = btrfs_reserve_extent(trans, root, 656 } else {
657 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
658 ret = btrfs_reserve_extent(trans, root,
634 async_extent->compressed_size, 659 async_extent->compressed_size,
635 async_extent->compressed_size, 660 async_extent->compressed_size,
636 0, alloc_hint, 661 0, alloc_hint,
637 (u64)-1, &ins, 1); 662 (u64)-1, &ins, 1);
638 btrfs_end_transaction(trans, root); 663 if (ret)
664 btrfs_abort_transaction(trans, root, ret);
665 btrfs_end_transaction(trans, root);
666 }
639 667
640 if (ret) { 668 if (ret) {
641 int i; 669 int i;
@@ -649,7 +677,9 @@ retry:
649 unlock_extent(io_tree, async_extent->start, 677 unlock_extent(io_tree, async_extent->start,
650 async_extent->start + 678 async_extent->start +
651 async_extent->ram_size - 1); 679 async_extent->ram_size - 1);
652 goto retry; 680 if (ret == -ENOSPC)
681 goto retry;
682 goto out_free; /* JDM: Requeue? */
653 } 683 }
654 684
655 /* 685 /*
@@ -661,7 +691,7 @@ retry:
661 async_extent->ram_size - 1, 0); 691 async_extent->ram_size - 1, 0);
662 692
663 em = alloc_extent_map(); 693 em = alloc_extent_map();
664 BUG_ON(!em); 694 BUG_ON(!em); /* -ENOMEM */
665 em->start = async_extent->start; 695 em->start = async_extent->start;
666 em->len = async_extent->ram_size; 696 em->len = async_extent->ram_size;
667 em->orig_start = em->start; 697 em->orig_start = em->start;
@@ -693,7 +723,7 @@ retry:
693 ins.offset, 723 ins.offset,
694 BTRFS_ORDERED_COMPRESSED, 724 BTRFS_ORDERED_COMPRESSED,
695 async_extent->compress_type); 725 async_extent->compress_type);
696 BUG_ON(ret); 726 BUG_ON(ret); /* -ENOMEM */
697 727
698 /* 728 /*
699 * clear dirty, set writeback and unlock the pages. 729 * clear dirty, set writeback and unlock the pages.
@@ -715,13 +745,17 @@ retry:
715 ins.offset, async_extent->pages, 745 ins.offset, async_extent->pages,
716 async_extent->nr_pages); 746 async_extent->nr_pages);
717 747
718 BUG_ON(ret); 748 BUG_ON(ret); /* -ENOMEM */
719 alloc_hint = ins.objectid + ins.offset; 749 alloc_hint = ins.objectid + ins.offset;
720 kfree(async_extent); 750 kfree(async_extent);
721 cond_resched(); 751 cond_resched();
722 } 752 }
723 753 ret = 0;
724 return 0; 754out:
755 return ret;
756out_free:
757 kfree(async_extent);
758 goto out;
725} 759}
726 760
727static u64 get_extent_allocation_hint(struct inode *inode, u64 start, 761static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
@@ -790,7 +824,18 @@ static noinline int cow_file_range(struct inode *inode,
790 824
791 BUG_ON(btrfs_is_free_space_inode(root, inode)); 825 BUG_ON(btrfs_is_free_space_inode(root, inode));
792 trans = btrfs_join_transaction(root); 826 trans = btrfs_join_transaction(root);
793 BUG_ON(IS_ERR(trans)); 827 if (IS_ERR(trans)) {
828 extent_clear_unlock_delalloc(inode,
829 &BTRFS_I(inode)->io_tree,
830 start, end, NULL,
831 EXTENT_CLEAR_UNLOCK_PAGE |
832 EXTENT_CLEAR_UNLOCK |
833 EXTENT_CLEAR_DELALLOC |
834 EXTENT_CLEAR_DIRTY |
835 EXTENT_SET_WRITEBACK |
836 EXTENT_END_WRITEBACK);
837 return PTR_ERR(trans);
838 }
794 trans->block_rsv = &root->fs_info->delalloc_block_rsv; 839 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
795 840
796 num_bytes = (end - start + blocksize) & ~(blocksize - 1); 841 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
@@ -820,8 +865,10 @@ static noinline int cow_file_range(struct inode *inode,
820 *nr_written = *nr_written + 865 *nr_written = *nr_written +
821 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE; 866 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
822 *page_started = 1; 867 *page_started = 1;
823 ret = 0;
824 goto out; 868 goto out;
869 } else if (ret < 0) {
870 btrfs_abort_transaction(trans, root, ret);
871 goto out_unlock;
825 } 872 }
826 } 873 }
827 874
@@ -838,10 +885,13 @@ static noinline int cow_file_range(struct inode *inode,
838 ret = btrfs_reserve_extent(trans, root, cur_alloc_size, 885 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
839 root->sectorsize, 0, alloc_hint, 886 root->sectorsize, 0, alloc_hint,
840 (u64)-1, &ins, 1); 887 (u64)-1, &ins, 1);
841 BUG_ON(ret); 888 if (ret < 0) {
889 btrfs_abort_transaction(trans, root, ret);
890 goto out_unlock;
891 }
842 892
843 em = alloc_extent_map(); 893 em = alloc_extent_map();
844 BUG_ON(!em); 894 BUG_ON(!em); /* -ENOMEM */
845 em->start = start; 895 em->start = start;
846 em->orig_start = em->start; 896 em->orig_start = em->start;
847 ram_size = ins.offset; 897 ram_size = ins.offset;
@@ -867,13 +917,16 @@ static noinline int cow_file_range(struct inode *inode,
867 cur_alloc_size = ins.offset; 917 cur_alloc_size = ins.offset;
868 ret = btrfs_add_ordered_extent(inode, start, ins.objectid, 918 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
869 ram_size, cur_alloc_size, 0); 919 ram_size, cur_alloc_size, 0);
870 BUG_ON(ret); 920 BUG_ON(ret); /* -ENOMEM */
871 921
872 if (root->root_key.objectid == 922 if (root->root_key.objectid ==
873 BTRFS_DATA_RELOC_TREE_OBJECTID) { 923 BTRFS_DATA_RELOC_TREE_OBJECTID) {
874 ret = btrfs_reloc_clone_csums(inode, start, 924 ret = btrfs_reloc_clone_csums(inode, start,
875 cur_alloc_size); 925 cur_alloc_size);
876 BUG_ON(ret); 926 if (ret) {
927 btrfs_abort_transaction(trans, root, ret);
928 goto out_unlock;
929 }
877 } 930 }
878 931
879 if (disk_num_bytes < cur_alloc_size) 932 if (disk_num_bytes < cur_alloc_size)
@@ -898,11 +951,23 @@ static noinline int cow_file_range(struct inode *inode,
898 alloc_hint = ins.objectid + ins.offset; 951 alloc_hint = ins.objectid + ins.offset;
899 start += cur_alloc_size; 952 start += cur_alloc_size;
900 } 953 }
901out:
902 ret = 0; 954 ret = 0;
955out:
903 btrfs_end_transaction(trans, root); 956 btrfs_end_transaction(trans, root);
904 957
905 return ret; 958 return ret;
959out_unlock:
960 extent_clear_unlock_delalloc(inode,
961 &BTRFS_I(inode)->io_tree,
962 start, end, NULL,
963 EXTENT_CLEAR_UNLOCK_PAGE |
964 EXTENT_CLEAR_UNLOCK |
965 EXTENT_CLEAR_DELALLOC |
966 EXTENT_CLEAR_DIRTY |
967 EXTENT_SET_WRITEBACK |
968 EXTENT_END_WRITEBACK);
969
970 goto out;
906} 971}
907 972
908/* 973/*
@@ -968,7 +1033,7 @@ static int cow_file_range_async(struct inode *inode, struct page *locked_page,
968 1, 0, NULL, GFP_NOFS); 1033 1, 0, NULL, GFP_NOFS);
969 while (start < end) { 1034 while (start < end) {
970 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS); 1035 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
971 BUG_ON(!async_cow); 1036 BUG_ON(!async_cow); /* -ENOMEM */
972 async_cow->inode = inode; 1037 async_cow->inode = inode;
973 async_cow->root = root; 1038 async_cow->root = root;
974 async_cow->locked_page = locked_page; 1039 async_cow->locked_page = locked_page;
@@ -1059,7 +1124,7 @@ static noinline int run_delalloc_nocow(struct inode *inode,
1059 u64 disk_bytenr; 1124 u64 disk_bytenr;
1060 u64 num_bytes; 1125 u64 num_bytes;
1061 int extent_type; 1126 int extent_type;
1062 int ret; 1127 int ret, err;
1063 int type; 1128 int type;
1064 int nocow; 1129 int nocow;
1065 int check_prev = 1; 1130 int check_prev = 1;
@@ -1077,7 +1142,11 @@ static noinline int run_delalloc_nocow(struct inode *inode,
1077 else 1142 else
1078 trans = btrfs_join_transaction(root); 1143 trans = btrfs_join_transaction(root);
1079 1144
1080 BUG_ON(IS_ERR(trans)); 1145 if (IS_ERR(trans)) {
1146 btrfs_free_path(path);
1147 return PTR_ERR(trans);
1148 }
1149
1081 trans->block_rsv = &root->fs_info->delalloc_block_rsv; 1150 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1082 1151
1083 cow_start = (u64)-1; 1152 cow_start = (u64)-1;
@@ -1085,7 +1154,10 @@ static noinline int run_delalloc_nocow(struct inode *inode,
1085 while (1) { 1154 while (1) {
1086 ret = btrfs_lookup_file_extent(trans, root, path, ino, 1155 ret = btrfs_lookup_file_extent(trans, root, path, ino,
1087 cur_offset, 0); 1156 cur_offset, 0);
1088 BUG_ON(ret < 0); 1157 if (ret < 0) {
1158 btrfs_abort_transaction(trans, root, ret);
1159 goto error;
1160 }
1089 if (ret > 0 && path->slots[0] > 0 && check_prev) { 1161 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1090 leaf = path->nodes[0]; 1162 leaf = path->nodes[0];
1091 btrfs_item_key_to_cpu(leaf, &found_key, 1163 btrfs_item_key_to_cpu(leaf, &found_key,
@@ -1099,8 +1171,10 @@ next_slot:
1099 leaf = path->nodes[0]; 1171 leaf = path->nodes[0];
1100 if (path->slots[0] >= btrfs_header_nritems(leaf)) { 1172 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1101 ret = btrfs_next_leaf(root, path); 1173 ret = btrfs_next_leaf(root, path);
1102 if (ret < 0) 1174 if (ret < 0) {
1103 BUG_ON(1); 1175 btrfs_abort_transaction(trans, root, ret);
1176 goto error;
1177 }
1104 if (ret > 0) 1178 if (ret > 0)
1105 break; 1179 break;
1106 leaf = path->nodes[0]; 1180 leaf = path->nodes[0];
@@ -1188,7 +1262,10 @@ out_check:
1188 ret = cow_file_range(inode, locked_page, cow_start, 1262 ret = cow_file_range(inode, locked_page, cow_start,
1189 found_key.offset - 1, page_started, 1263 found_key.offset - 1, page_started,
1190 nr_written, 1); 1264 nr_written, 1);
1191 BUG_ON(ret); 1265 if (ret) {
1266 btrfs_abort_transaction(trans, root, ret);
1267 goto error;
1268 }
1192 cow_start = (u64)-1; 1269 cow_start = (u64)-1;
1193 } 1270 }
1194 1271
@@ -1197,7 +1274,7 @@ out_check:
1197 struct extent_map_tree *em_tree; 1274 struct extent_map_tree *em_tree;
1198 em_tree = &BTRFS_I(inode)->extent_tree; 1275 em_tree = &BTRFS_I(inode)->extent_tree;
1199 em = alloc_extent_map(); 1276 em = alloc_extent_map();
1200 BUG_ON(!em); 1277 BUG_ON(!em); /* -ENOMEM */
1201 em->start = cur_offset; 1278 em->start = cur_offset;
1202 em->orig_start = em->start; 1279 em->orig_start = em->start;
1203 em->len = num_bytes; 1280 em->len = num_bytes;
@@ -1223,13 +1300,16 @@ out_check:
1223 1300
1224 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr, 1301 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
1225 num_bytes, num_bytes, type); 1302 num_bytes, num_bytes, type);
1226 BUG_ON(ret); 1303 BUG_ON(ret); /* -ENOMEM */
1227 1304
1228 if (root->root_key.objectid == 1305 if (root->root_key.objectid ==
1229 BTRFS_DATA_RELOC_TREE_OBJECTID) { 1306 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1230 ret = btrfs_reloc_clone_csums(inode, cur_offset, 1307 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1231 num_bytes); 1308 num_bytes);
1232 BUG_ON(ret); 1309 if (ret) {
1310 btrfs_abort_transaction(trans, root, ret);
1311 goto error;
1312 }
1233 } 1313 }
1234 1314
1235 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, 1315 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
@@ -1248,18 +1328,23 @@ out_check:
1248 if (cow_start != (u64)-1) { 1328 if (cow_start != (u64)-1) {
1249 ret = cow_file_range(inode, locked_page, cow_start, end, 1329 ret = cow_file_range(inode, locked_page, cow_start, end,
1250 page_started, nr_written, 1); 1330 page_started, nr_written, 1);
1251 BUG_ON(ret); 1331 if (ret) {
1332 btrfs_abort_transaction(trans, root, ret);
1333 goto error;
1334 }
1252 } 1335 }
1253 1336
1337error:
1254 if (nolock) { 1338 if (nolock) {
1255 ret = btrfs_end_transaction_nolock(trans, root); 1339 err = btrfs_end_transaction_nolock(trans, root);
1256 BUG_ON(ret);
1257 } else { 1340 } else {
1258 ret = btrfs_end_transaction(trans, root); 1341 err = btrfs_end_transaction(trans, root);
1259 BUG_ON(ret);
1260 } 1342 }
1343 if (!ret)
1344 ret = err;
1345
1261 btrfs_free_path(path); 1346 btrfs_free_path(path);
1262 return 0; 1347 return ret;
1263} 1348}
1264 1349
1265/* 1350/*
@@ -1448,7 +1533,7 @@ static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1448 int ret = 0; 1533 int ret = 0;
1449 1534
1450 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); 1535 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1451 BUG_ON(ret); 1536 BUG_ON(ret); /* -ENOMEM */
1452 return 0; 1537 return 0;
1453} 1538}
1454 1539
@@ -1677,13 +1762,15 @@ static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1677 */ 1762 */
1678 ret = btrfs_drop_extents(trans, inode, file_pos, file_pos + num_bytes, 1763 ret = btrfs_drop_extents(trans, inode, file_pos, file_pos + num_bytes,
1679 &hint, 0); 1764 &hint, 0);
1680 BUG_ON(ret); 1765 if (ret)
1766 goto out;
1681 1767
1682 ins.objectid = btrfs_ino(inode); 1768 ins.objectid = btrfs_ino(inode);
1683 ins.offset = file_pos; 1769 ins.offset = file_pos;
1684 ins.type = BTRFS_EXTENT_DATA_KEY; 1770 ins.type = BTRFS_EXTENT_DATA_KEY;
1685 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi)); 1771 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
1686 BUG_ON(ret); 1772 if (ret)
1773 goto out;
1687 leaf = path->nodes[0]; 1774 leaf = path->nodes[0];
1688 fi = btrfs_item_ptr(leaf, path->slots[0], 1775 fi = btrfs_item_ptr(leaf, path->slots[0],
1689 struct btrfs_file_extent_item); 1776 struct btrfs_file_extent_item);
@@ -1711,10 +1798,10 @@ static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1711 ret = btrfs_alloc_reserved_file_extent(trans, root, 1798 ret = btrfs_alloc_reserved_file_extent(trans, root,
1712 root->root_key.objectid, 1799 root->root_key.objectid,
1713 btrfs_ino(inode), file_pos, &ins); 1800 btrfs_ino(inode), file_pos, &ins);
1714 BUG_ON(ret); 1801out:
1715 btrfs_free_path(path); 1802 btrfs_free_path(path);
1716 1803
1717 return 0; 1804 return ret;
1718} 1805}
1719 1806
1720/* 1807/*
@@ -1742,22 +1829,24 @@ static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
1742 end - start + 1); 1829 end - start + 1);
1743 if (!ret) 1830 if (!ret)
1744 return 0; 1831 return 0;
1745 BUG_ON(!ordered_extent); 1832 BUG_ON(!ordered_extent); /* Logic error */
1746 1833
1747 nolock = btrfs_is_free_space_inode(root, inode); 1834 nolock = btrfs_is_free_space_inode(root, inode);
1748 1835
1749 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { 1836 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
1750 BUG_ON(!list_empty(&ordered_extent->list)); 1837 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
1751 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent); 1838 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1752 if (!ret) { 1839 if (!ret) {
1753 if (nolock) 1840 if (nolock)
1754 trans = btrfs_join_transaction_nolock(root); 1841 trans = btrfs_join_transaction_nolock(root);
1755 else 1842 else
1756 trans = btrfs_join_transaction(root); 1843 trans = btrfs_join_transaction(root);
1757 BUG_ON(IS_ERR(trans)); 1844 if (IS_ERR(trans))
1845 return PTR_ERR(trans);
1758 trans->block_rsv = &root->fs_info->delalloc_block_rsv; 1846 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1759 ret = btrfs_update_inode_fallback(trans, root, inode); 1847 ret = btrfs_update_inode_fallback(trans, root, inode);
1760 BUG_ON(ret); 1848 if (ret) /* -ENOMEM or corruption */
1849 btrfs_abort_transaction(trans, root, ret);
1761 } 1850 }
1762 goto out; 1851 goto out;
1763 } 1852 }
@@ -1770,7 +1859,11 @@ static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
1770 trans = btrfs_join_transaction_nolock(root); 1859 trans = btrfs_join_transaction_nolock(root);
1771 else 1860 else
1772 trans = btrfs_join_transaction(root); 1861 trans = btrfs_join_transaction(root);
1773 BUG_ON(IS_ERR(trans)); 1862 if (IS_ERR(trans)) {
1863 ret = PTR_ERR(trans);
1864 trans = NULL;
1865 goto out_unlock;
1866 }
1774 trans->block_rsv = &root->fs_info->delalloc_block_rsv; 1867 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1775 1868
1776 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) 1869 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
@@ -1781,7 +1874,6 @@ static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
1781 ordered_extent->file_offset, 1874 ordered_extent->file_offset,
1782 ordered_extent->file_offset + 1875 ordered_extent->file_offset +
1783 ordered_extent->len); 1876 ordered_extent->len);
1784 BUG_ON(ret);
1785 } else { 1877 } else {
1786 BUG_ON(root == root->fs_info->tree_root); 1878 BUG_ON(root == root->fs_info->tree_root);
1787 ret = insert_reserved_file_extent(trans, inode, 1879 ret = insert_reserved_file_extent(trans, inode,
@@ -1795,11 +1887,14 @@ static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
1795 unpin_extent_cache(&BTRFS_I(inode)->extent_tree, 1887 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
1796 ordered_extent->file_offset, 1888 ordered_extent->file_offset,
1797 ordered_extent->len); 1889 ordered_extent->len);
1798 BUG_ON(ret);
1799 } 1890 }
1800 unlock_extent_cached(io_tree, ordered_extent->file_offset, 1891 unlock_extent_cached(io_tree, ordered_extent->file_offset,
1801 ordered_extent->file_offset + 1892 ordered_extent->file_offset +
1802 ordered_extent->len - 1, &cached_state, GFP_NOFS); 1893 ordered_extent->len - 1, &cached_state, GFP_NOFS);
1894 if (ret < 0) {
1895 btrfs_abort_transaction(trans, root, ret);
1896 goto out;
1897 }
1803 1898
1804 add_pending_csums(trans, inode, ordered_extent->file_offset, 1899 add_pending_csums(trans, inode, ordered_extent->file_offset,
1805 &ordered_extent->list); 1900 &ordered_extent->list);
@@ -1807,7 +1902,10 @@ static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
1807 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent); 1902 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1808 if (!ret || !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { 1903 if (!ret || !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
1809 ret = btrfs_update_inode_fallback(trans, root, inode); 1904 ret = btrfs_update_inode_fallback(trans, root, inode);
1810 BUG_ON(ret); 1905 if (ret) { /* -ENOMEM or corruption */
1906 btrfs_abort_transaction(trans, root, ret);
1907 goto out;
1908 }
1811 } 1909 }
1812 ret = 0; 1910 ret = 0;
1813out: 1911out:
@@ -1826,6 +1924,11 @@ out:
1826 btrfs_put_ordered_extent(ordered_extent); 1924 btrfs_put_ordered_extent(ordered_extent);
1827 1925
1828 return 0; 1926 return 0;
1927out_unlock:
1928 unlock_extent_cached(io_tree, ordered_extent->file_offset,
1929 ordered_extent->file_offset +
1930 ordered_extent->len - 1, &cached_state, GFP_NOFS);
1931 goto out;
1829} 1932}
1830 1933
1831static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end, 1934static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
@@ -1907,6 +2010,8 @@ struct delayed_iput {
1907 struct inode *inode; 2010 struct inode *inode;
1908}; 2011};
1909 2012
2013/* JDM: If this is fs-wide, why can't we add a pointer to
2014 * btrfs_inode instead and avoid the allocation? */
1910void btrfs_add_delayed_iput(struct inode *inode) 2015void btrfs_add_delayed_iput(struct inode *inode)
1911{ 2016{
1912 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; 2017 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
@@ -2053,20 +2158,27 @@ int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2053 /* grab metadata reservation from transaction handle */ 2158 /* grab metadata reservation from transaction handle */
2054 if (reserve) { 2159 if (reserve) {
2055 ret = btrfs_orphan_reserve_metadata(trans, inode); 2160 ret = btrfs_orphan_reserve_metadata(trans, inode);
2056 BUG_ON(ret); 2161 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
2057 } 2162 }
2058 2163
2059 /* insert an orphan item to track this unlinked/truncated file */ 2164 /* insert an orphan item to track this unlinked/truncated file */
2060 if (insert >= 1) { 2165 if (insert >= 1) {
2061 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode)); 2166 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
2062 BUG_ON(ret && ret != -EEXIST); 2167 if (ret && ret != -EEXIST) {
2168 btrfs_abort_transaction(trans, root, ret);
2169 return ret;
2170 }
2171 ret = 0;
2063 } 2172 }
2064 2173
2065 /* insert an orphan item to track subvolume contains orphan files */ 2174 /* insert an orphan item to track subvolume contains orphan files */
2066 if (insert >= 2) { 2175 if (insert >= 2) {
2067 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root, 2176 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2068 root->root_key.objectid); 2177 root->root_key.objectid);
2069 BUG_ON(ret); 2178 if (ret && ret != -EEXIST) {
2179 btrfs_abort_transaction(trans, root, ret);
2180 return ret;
2181 }
2070 } 2182 }
2071 return 0; 2183 return 0;
2072} 2184}
@@ -2096,7 +2208,7 @@ int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
2096 2208
2097 if (trans && delete_item) { 2209 if (trans && delete_item) {
2098 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode)); 2210 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
2099 BUG_ON(ret); 2211 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
2100 } 2212 }
2101 2213
2102 if (release_rsv) 2214 if (release_rsv)
@@ -2230,7 +2342,7 @@ int btrfs_orphan_cleanup(struct btrfs_root *root)
2230 } 2342 }
2231 ret = btrfs_del_orphan_item(trans, root, 2343 ret = btrfs_del_orphan_item(trans, root,
2232 found_key.objectid); 2344 found_key.objectid);
2233 BUG_ON(ret); 2345 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
2234 btrfs_end_transaction(trans, root); 2346 btrfs_end_transaction(trans, root);
2235 continue; 2347 continue;
2236 } 2348 }
@@ -2612,16 +2724,22 @@ static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2612 printk(KERN_INFO "btrfs failed to delete reference to %.*s, " 2724 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
2613 "inode %llu parent %llu\n", name_len, name, 2725 "inode %llu parent %llu\n", name_len, name,
2614 (unsigned long long)ino, (unsigned long long)dir_ino); 2726 (unsigned long long)ino, (unsigned long long)dir_ino);
2727 btrfs_abort_transaction(trans, root, ret);
2615 goto err; 2728 goto err;
2616 } 2729 }
2617 2730
2618 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index); 2731 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
2619 if (ret) 2732 if (ret) {
2733 btrfs_abort_transaction(trans, root, ret);
2620 goto err; 2734 goto err;
2735 }
2621 2736
2622 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, 2737 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
2623 inode, dir_ino); 2738 inode, dir_ino);
2624 BUG_ON(ret != 0 && ret != -ENOENT); 2739 if (ret != 0 && ret != -ENOENT) {
2740 btrfs_abort_transaction(trans, root, ret);
2741 goto err;
2742 }
2625 2743
2626 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, 2744 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2627 dir, index); 2745 dir, index);
@@ -2779,7 +2897,7 @@ static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2779 err = ret; 2897 err = ret;
2780 goto out; 2898 goto out;
2781 } 2899 }
2782 BUG_ON(ret == 0); 2900 BUG_ON(ret == 0); /* Corruption */
2783 if (check_path_shared(root, path)) 2901 if (check_path_shared(root, path))
2784 goto out; 2902 goto out;
2785 btrfs_release_path(path); 2903 btrfs_release_path(path);
@@ -2812,7 +2930,7 @@ static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2812 err = PTR_ERR(ref); 2930 err = PTR_ERR(ref);
2813 goto out; 2931 goto out;
2814 } 2932 }
2815 BUG_ON(!ref); 2933 BUG_ON(!ref); /* Logic error */
2816 if (check_path_shared(root, path)) 2934 if (check_path_shared(root, path))
2817 goto out; 2935 goto out;
2818 index = btrfs_inode_ref_index(path->nodes[0], ref); 2936 index = btrfs_inode_ref_index(path->nodes[0], ref);
@@ -2919,23 +3037,42 @@ int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
2919 3037
2920 di = btrfs_lookup_dir_item(trans, root, path, dir_ino, 3038 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
2921 name, name_len, -1); 3039 name, name_len, -1);
2922 BUG_ON(IS_ERR_OR_NULL(di)); 3040 if (IS_ERR_OR_NULL(di)) {
3041 if (!di)
3042 ret = -ENOENT;
3043 else
3044 ret = PTR_ERR(di);
3045 goto out;
3046 }
2923 3047
2924 leaf = path->nodes[0]; 3048 leaf = path->nodes[0];
2925 btrfs_dir_item_key_to_cpu(leaf, di, &key); 3049 btrfs_dir_item_key_to_cpu(leaf, di, &key);
2926 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); 3050 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
2927 ret = btrfs_delete_one_dir_name(trans, root, path, di); 3051 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2928 BUG_ON(ret); 3052 if (ret) {
3053 btrfs_abort_transaction(trans, root, ret);
3054 goto out;
3055 }
2929 btrfs_release_path(path); 3056 btrfs_release_path(path);
2930 3057
2931 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root, 3058 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
2932 objectid, root->root_key.objectid, 3059 objectid, root->root_key.objectid,
2933 dir_ino, &index, name, name_len); 3060 dir_ino, &index, name, name_len);
2934 if (ret < 0) { 3061 if (ret < 0) {
2935 BUG_ON(ret != -ENOENT); 3062 if (ret != -ENOENT) {
3063 btrfs_abort_transaction(trans, root, ret);
3064 goto out;
3065 }
2936 di = btrfs_search_dir_index_item(root, path, dir_ino, 3066 di = btrfs_search_dir_index_item(root, path, dir_ino,
2937 name, name_len); 3067 name, name_len);
2938 BUG_ON(IS_ERR_OR_NULL(di)); 3068 if (IS_ERR_OR_NULL(di)) {
3069 if (!di)
3070 ret = -ENOENT;
3071 else
3072 ret = PTR_ERR(di);
3073 btrfs_abort_transaction(trans, root, ret);
3074 goto out;
3075 }
2939 3076
2940 leaf = path->nodes[0]; 3077 leaf = path->nodes[0];
2941 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); 3078 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
@@ -2945,15 +3082,19 @@ int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
2945 btrfs_release_path(path); 3082 btrfs_release_path(path);
2946 3083
2947 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index); 3084 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
2948 BUG_ON(ret); 3085 if (ret) {
3086 btrfs_abort_transaction(trans, root, ret);
3087 goto out;
3088 }
2949 3089
2950 btrfs_i_size_write(dir, dir->i_size - name_len * 2); 3090 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2951 dir->i_mtime = dir->i_ctime = CURRENT_TIME; 3091 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2952 ret = btrfs_update_inode(trans, root, dir); 3092 ret = btrfs_update_inode(trans, root, dir);
2953 BUG_ON(ret); 3093 if (ret)
2954 3094 btrfs_abort_transaction(trans, root, ret);
3095out:
2955 btrfs_free_path(path); 3096 btrfs_free_path(path);
2956 return 0; 3097 return ret;
2957} 3098}
2958 3099
2959static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) 3100static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
@@ -3212,7 +3353,11 @@ delete:
3212 ret = btrfs_del_items(trans, root, path, 3353 ret = btrfs_del_items(trans, root, path,
3213 pending_del_slot, 3354 pending_del_slot,
3214 pending_del_nr); 3355 pending_del_nr);
3215 BUG_ON(ret); 3356 if (ret) {
3357 btrfs_abort_transaction(trans,
3358 root, ret);
3359 goto error;
3360 }
3216 pending_del_nr = 0; 3361 pending_del_nr = 0;
3217 } 3362 }
3218 btrfs_release_path(path); 3363 btrfs_release_path(path);
@@ -3225,8 +3370,10 @@ out:
3225 if (pending_del_nr) { 3370 if (pending_del_nr) {
3226 ret = btrfs_del_items(trans, root, path, pending_del_slot, 3371 ret = btrfs_del_items(trans, root, path, pending_del_slot,
3227 pending_del_nr); 3372 pending_del_nr);
3228 BUG_ON(ret); 3373 if (ret)
3374 btrfs_abort_transaction(trans, root, ret);
3229 } 3375 }
3376error:
3230 btrfs_free_path(path); 3377 btrfs_free_path(path);
3231 return err; 3378 return err;
3232} 3379}
@@ -3373,7 +3520,10 @@ int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
3373 while (1) { 3520 while (1) {
3374 em = btrfs_get_extent(inode, NULL, 0, cur_offset, 3521 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
3375 block_end - cur_offset, 0); 3522 block_end - cur_offset, 0);
3376 BUG_ON(IS_ERR_OR_NULL(em)); 3523 if (IS_ERR(em)) {
3524 err = PTR_ERR(em);
3525 break;
3526 }
3377 last_byte = min(extent_map_end(em), block_end); 3527 last_byte = min(extent_map_end(em), block_end);
3378 last_byte = (last_byte + mask) & ~mask; 3528 last_byte = (last_byte + mask) & ~mask;
3379 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { 3529 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
@@ -3390,7 +3540,7 @@ int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
3390 cur_offset + hole_size, 3540 cur_offset + hole_size,
3391 &hint_byte, 1); 3541 &hint_byte, 1);
3392 if (err) { 3542 if (err) {
3393 btrfs_update_inode(trans, root, inode); 3543 btrfs_abort_transaction(trans, root, err);
3394 btrfs_end_transaction(trans, root); 3544 btrfs_end_transaction(trans, root);
3395 break; 3545 break;
3396 } 3546 }
@@ -3400,7 +3550,7 @@ int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
3400 0, hole_size, 0, hole_size, 3550 0, hole_size, 0, hole_size,
3401 0, 0, 0); 3551 0, 0, 0);
3402 if (err) { 3552 if (err) {
3403 btrfs_update_inode(trans, root, inode); 3553 btrfs_abort_transaction(trans, root, err);
3404 btrfs_end_transaction(trans, root); 3554 btrfs_end_transaction(trans, root);
3405 break; 3555 break;
3406 } 3556 }
@@ -4581,18 +4731,26 @@ int btrfs_add_link(struct btrfs_trans_handle *trans,
4581 parent_ino, index); 4731 parent_ino, index);
4582 } 4732 }
4583 4733
4584 if (ret == 0) { 4734 /* Nothing to clean up yet */
4585 ret = btrfs_insert_dir_item(trans, root, name, name_len, 4735 if (ret)
4586 parent_inode, &key, 4736 return ret;
4587 btrfs_inode_type(inode), index);
4588 if (ret)
4589 goto fail_dir_item;
4590 4737
4591 btrfs_i_size_write(parent_inode, parent_inode->i_size + 4738 ret = btrfs_insert_dir_item(trans, root, name, name_len,
4592 name_len * 2); 4739 parent_inode, &key,
4593 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME; 4740 btrfs_inode_type(inode), index);
4594 ret = btrfs_update_inode(trans, root, parent_inode); 4741 if (ret == -EEXIST)
4742 goto fail_dir_item;
4743 else if (ret) {
4744 btrfs_abort_transaction(trans, root, ret);
4745 return ret;
4595 } 4746 }
4747
4748 btrfs_i_size_write(parent_inode, parent_inode->i_size +
4749 name_len * 2);
4750 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
4751 ret = btrfs_update_inode(trans, root, parent_inode);
4752 if (ret)
4753 btrfs_abort_transaction(trans, root, ret);
4596 return ret; 4754 return ret;
4597 4755
4598fail_dir_item: 4756fail_dir_item:
@@ -4806,7 +4964,8 @@ static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
4806 } else { 4964 } else {
4807 struct dentry *parent = dentry->d_parent; 4965 struct dentry *parent = dentry->d_parent;
4808 err = btrfs_update_inode(trans, root, inode); 4966 err = btrfs_update_inode(trans, root, inode);
4809 BUG_ON(err); 4967 if (err)
4968 goto fail;
4810 d_instantiate(dentry, inode); 4969 d_instantiate(dentry, inode);
4811 btrfs_log_new_name(trans, inode, NULL, parent); 4970 btrfs_log_new_name(trans, inode, NULL, parent);
4812 } 4971 }
@@ -5137,7 +5296,7 @@ again:
5137 ret = uncompress_inline(path, inode, page, 5296 ret = uncompress_inline(path, inode, page,
5138 pg_offset, 5297 pg_offset,
5139 extent_offset, item); 5298 extent_offset, item);
5140 BUG_ON(ret); 5299 BUG_ON(ret); /* -ENOMEM */
5141 } else { 5300 } else {
5142 map = kmap(page); 5301 map = kmap(page);
5143 read_extent_buffer(leaf, map + pg_offset, ptr, 5302 read_extent_buffer(leaf, map + pg_offset, ptr,
@@ -5252,6 +5411,7 @@ out:
5252 free_extent_map(em); 5411 free_extent_map(em);
5253 return ERR_PTR(err); 5412 return ERR_PTR(err);
5254 } 5413 }
5414 BUG_ON(!em); /* Error is always set */
5255 return em; 5415 return em;
5256} 5416}
5257 5417
@@ -5868,7 +6028,7 @@ static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
5868 int ret; 6028 int ret;
5869 struct btrfs_root *root = BTRFS_I(inode)->root; 6029 struct btrfs_root *root = BTRFS_I(inode)->root;
5870 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1); 6030 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
5871 BUG_ON(ret); 6031 BUG_ON(ret); /* -ENOMEM */
5872 return 0; 6032 return 0;
5873} 6033}
5874 6034
@@ -7068,7 +7228,10 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7068 if (!ret) 7228 if (!ret)
7069 ret = btrfs_update_inode(trans, root, old_inode); 7229 ret = btrfs_update_inode(trans, root, old_inode);
7070 } 7230 }
7071 BUG_ON(ret); 7231 if (ret) {
7232 btrfs_abort_transaction(trans, root, ret);
7233 goto out_fail;
7234 }
7072 7235
7073 if (new_inode) { 7236 if (new_inode) {
7074 new_inode->i_ctime = CURRENT_TIME; 7237 new_inode->i_ctime = CURRENT_TIME;
@@ -7086,11 +7249,14 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7086 new_dentry->d_name.name, 7249 new_dentry->d_name.name,
7087 new_dentry->d_name.len); 7250 new_dentry->d_name.len);
7088 } 7251 }
7089 BUG_ON(ret); 7252 if (!ret && new_inode->i_nlink == 0) {
7090 if (new_inode->i_nlink == 0) {
7091 ret = btrfs_orphan_add(trans, new_dentry->d_inode); 7253 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
7092 BUG_ON(ret); 7254 BUG_ON(ret);
7093 } 7255 }
7256 if (ret) {
7257 btrfs_abort_transaction(trans, root, ret);
7258 goto out_fail;
7259 }
7094 } 7260 }
7095 7261
7096 fixup_inode_flags(new_dir, old_inode); 7262 fixup_inode_flags(new_dir, old_inode);
@@ -7098,7 +7264,10 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7098 ret = btrfs_add_link(trans, new_dir, old_inode, 7264 ret = btrfs_add_link(trans, new_dir, old_inode,
7099 new_dentry->d_name.name, 7265 new_dentry->d_name.name,
7100 new_dentry->d_name.len, 0, index); 7266 new_dentry->d_name.len, 0, index);
7101 BUG_ON(ret); 7267 if (ret) {
7268 btrfs_abort_transaction(trans, root, ret);
7269 goto out_fail;
7270 }
7102 7271
7103 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) { 7272 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
7104 struct dentry *parent = new_dentry->d_parent; 7273 struct dentry *parent = new_dentry->d_parent;
@@ -7323,7 +7492,12 @@ static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
7323 ins.offset, ins.offset, 7492 ins.offset, ins.offset,
7324 ins.offset, 0, 0, 0, 7493 ins.offset, 0, 0, 0,
7325 BTRFS_FILE_EXTENT_PREALLOC); 7494 BTRFS_FILE_EXTENT_PREALLOC);
7326 BUG_ON(ret); 7495 if (ret) {
7496 btrfs_abort_transaction(trans, root, ret);
7497 if (own_trans)
7498 btrfs_end_transaction(trans, root);
7499 break;
7500 }
7327 btrfs_drop_extent_cache(inode, cur_offset, 7501 btrfs_drop_extent_cache(inode, cur_offset,
7328 cur_offset + ins.offset -1, 0); 7502 cur_offset + ins.offset -1, 0);
7329 7503
@@ -7345,7 +7519,13 @@ static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
7345 } 7519 }
7346 7520
7347 ret = btrfs_update_inode(trans, root, inode); 7521 ret = btrfs_update_inode(trans, root, inode);
7348 BUG_ON(ret); 7522
7523 if (ret) {
7524 btrfs_abort_transaction(trans, root, ret);
7525 if (own_trans)
7526 btrfs_end_transaction(trans, root);
7527 break;
7528 }
7349 7529
7350 if (own_trans) 7530 if (own_trans)
7351 btrfs_end_transaction(trans, root); 7531 btrfs_end_transaction(trans, root);
diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c
index de25e4255aeb..205809200714 100644
--- a/fs/btrfs/ioctl.c
+++ b/fs/btrfs/ioctl.c
@@ -425,13 +425,18 @@ static noinline int create_subvol(struct btrfs_root *root,
425 425
426 key.offset = (u64)-1; 426 key.offset = (u64)-1;
427 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key); 427 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
428 BUG_ON(IS_ERR(new_root)); 428 if (IS_ERR(new_root)) {
429 btrfs_abort_transaction(trans, root, PTR_ERR(new_root));
430 ret = PTR_ERR(new_root);
431 goto fail;
432 }
429 433
430 btrfs_record_root_in_trans(trans, new_root); 434 btrfs_record_root_in_trans(trans, new_root);
431 435
432 ret = btrfs_create_subvol_root(trans, new_root, new_dirid); 436 ret = btrfs_create_subvol_root(trans, new_root, new_dirid);
433 if (ret) { 437 if (ret) {
434 /* We potentially lose an unused inode item here */ 438 /* We potentially lose an unused inode item here */
439 btrfs_abort_transaction(trans, root, ret);
435 goto fail; 440 goto fail;
436 } 441 }
437 442
@@ -439,13 +444,18 @@ static noinline int create_subvol(struct btrfs_root *root,
439 * insert the directory item 444 * insert the directory item
440 */ 445 */
441 ret = btrfs_set_inode_index(dir, &index); 446 ret = btrfs_set_inode_index(dir, &index);
442 BUG_ON(ret); 447 if (ret) {
448 btrfs_abort_transaction(trans, root, ret);
449 goto fail;
450 }
443 451
444 ret = btrfs_insert_dir_item(trans, root, 452 ret = btrfs_insert_dir_item(trans, root,
445 name, namelen, dir, &key, 453 name, namelen, dir, &key,
446 BTRFS_FT_DIR, index); 454 BTRFS_FT_DIR, index);
447 if (ret) 455 if (ret) {
456 btrfs_abort_transaction(trans, root, ret);
448 goto fail; 457 goto fail;
458 }
449 459
450 btrfs_i_size_write(dir, dir->i_size + namelen * 2); 460 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
451 ret = btrfs_update_inode(trans, root, dir); 461 ret = btrfs_update_inode(trans, root, dir);
@@ -1970,7 +1980,11 @@ static noinline int btrfs_ioctl_snap_destroy(struct file *file,
1970 dest->root_key.objectid, 1980 dest->root_key.objectid,
1971 dentry->d_name.name, 1981 dentry->d_name.name,
1972 dentry->d_name.len); 1982 dentry->d_name.len);
1973 BUG_ON(ret); 1983 if (ret) {
1984 err = ret;
1985 btrfs_abort_transaction(trans, root, ret);
1986 goto out_end_trans;
1987 }
1974 1988
1975 btrfs_record_root_in_trans(trans, dest); 1989 btrfs_record_root_in_trans(trans, dest);
1976 1990
@@ -1983,11 +1997,16 @@ static noinline int btrfs_ioctl_snap_destroy(struct file *file,
1983 ret = btrfs_insert_orphan_item(trans, 1997 ret = btrfs_insert_orphan_item(trans,
1984 root->fs_info->tree_root, 1998 root->fs_info->tree_root,
1985 dest->root_key.objectid); 1999 dest->root_key.objectid);
1986 BUG_ON(ret); 2000 if (ret) {
2001 btrfs_abort_transaction(trans, root, ret);
2002 err = ret;
2003 goto out_end_trans;
2004 }
1987 } 2005 }
1988 2006out_end_trans:
1989 ret = btrfs_end_transaction(trans, root); 2007 ret = btrfs_end_transaction(trans, root);
1990 BUG_ON(ret); 2008 if (ret && !err)
2009 err = ret;
1991 inode->i_flags |= S_DEAD; 2010 inode->i_flags |= S_DEAD;
1992out_up_write: 2011out_up_write:
1993 up_write(&root->fs_info->subvol_sem); 2012 up_write(&root->fs_info->subvol_sem);
@@ -2451,11 +2470,21 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
2451 new_key.offset, 2470 new_key.offset,
2452 new_key.offset + datal, 2471 new_key.offset + datal,
2453 &hint_byte, 1); 2472 &hint_byte, 1);
2454 BUG_ON(ret); 2473 if (ret) {
2474 btrfs_abort_transaction(trans, root,
2475 ret);
2476 btrfs_end_transaction(trans, root);
2477 goto out;
2478 }
2455 2479
2456 ret = btrfs_insert_empty_item(trans, root, path, 2480 ret = btrfs_insert_empty_item(trans, root, path,
2457 &new_key, size); 2481 &new_key, size);
2458 BUG_ON(ret); 2482 if (ret) {
2483 btrfs_abort_transaction(trans, root,
2484 ret);
2485 btrfs_end_transaction(trans, root);
2486 goto out;
2487 }
2459 2488
2460 leaf = path->nodes[0]; 2489 leaf = path->nodes[0];
2461 slot = path->slots[0]; 2490 slot = path->slots[0];
@@ -2482,7 +2511,15 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
2482 btrfs_ino(inode), 2511 btrfs_ino(inode),
2483 new_key.offset - datao, 2512 new_key.offset - datao,
2484 0); 2513 0);
2485 BUG_ON(ret); 2514 if (ret) {
2515 btrfs_abort_transaction(trans,
2516 root,
2517 ret);
2518 btrfs_end_transaction(trans,
2519 root);
2520 goto out;
2521
2522 }
2486 } 2523 }
2487 } else if (type == BTRFS_FILE_EXTENT_INLINE) { 2524 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2488 u64 skip = 0; 2525 u64 skip = 0;
@@ -2507,11 +2544,21 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
2507 new_key.offset, 2544 new_key.offset,
2508 new_key.offset + datal, 2545 new_key.offset + datal,
2509 &hint_byte, 1); 2546 &hint_byte, 1);
2510 BUG_ON(ret); 2547 if (ret) {
2548 btrfs_abort_transaction(trans, root,
2549 ret);
2550 btrfs_end_transaction(trans, root);
2551 goto out;
2552 }
2511 2553
2512 ret = btrfs_insert_empty_item(trans, root, path, 2554 ret = btrfs_insert_empty_item(trans, root, path,
2513 &new_key, size); 2555 &new_key, size);
2514 BUG_ON(ret); 2556 if (ret) {
2557 btrfs_abort_transaction(trans, root,
2558 ret);
2559 btrfs_end_transaction(trans, root);
2560 goto out;
2561 }
2515 2562
2516 if (skip) { 2563 if (skip) {
2517 u32 start = 2564 u32 start =
@@ -2545,8 +2592,12 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
2545 btrfs_i_size_write(inode, endoff); 2592 btrfs_i_size_write(inode, endoff);
2546 2593
2547 ret = btrfs_update_inode(trans, root, inode); 2594 ret = btrfs_update_inode(trans, root, inode);
2548 BUG_ON(ret); 2595 if (ret) {
2549 btrfs_end_transaction(trans, root); 2596 btrfs_abort_transaction(trans, root, ret);
2597 btrfs_end_transaction(trans, root);
2598 goto out;
2599 }
2600 ret = btrfs_end_transaction(trans, root);
2550 } 2601 }
2551next: 2602next:
2552 btrfs_release_path(path); 2603 btrfs_release_path(path);
diff --git a/fs/btrfs/orphan.c b/fs/btrfs/orphan.c
index f8be250963a0..24cad1695af7 100644
--- a/fs/btrfs/orphan.c
+++ b/fs/btrfs/orphan.c
@@ -58,7 +58,7 @@ int btrfs_del_orphan_item(struct btrfs_trans_handle *trans,
58 ret = btrfs_search_slot(trans, root, &key, path, -1, 1); 58 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
59 if (ret < 0) 59 if (ret < 0)
60 goto out; 60 goto out;
61 if (ret) { 61 if (ret) { /* JDM: Really? */
62 ret = -ENOENT; 62 ret = -ENOENT;
63 goto out; 63 goto out;
64 } 64 }
diff --git a/fs/btrfs/relocation.c b/fs/btrfs/relocation.c
index cba7a0bf3667..017281dbb2a7 100644
--- a/fs/btrfs/relocation.c
+++ b/fs/btrfs/relocation.c
@@ -4102,10 +4102,11 @@ out:
4102static noinline_for_stack int mark_garbage_root(struct btrfs_root *root) 4102static noinline_for_stack int mark_garbage_root(struct btrfs_root *root)
4103{ 4103{
4104 struct btrfs_trans_handle *trans; 4104 struct btrfs_trans_handle *trans;
4105 int ret; 4105 int ret, err;
4106 4106
4107 trans = btrfs_start_transaction(root->fs_info->tree_root, 0); 4107 trans = btrfs_start_transaction(root->fs_info->tree_root, 0);
4108 BUG_ON(IS_ERR(trans)); 4108 if (IS_ERR(trans))
4109 return PTR_ERR(trans);
4109 4110
4110 memset(&root->root_item.drop_progress, 0, 4111 memset(&root->root_item.drop_progress, 0,
4111 sizeof(root->root_item.drop_progress)); 4112 sizeof(root->root_item.drop_progress));
@@ -4113,11 +4114,11 @@ static noinline_for_stack int mark_garbage_root(struct btrfs_root *root)
4113 btrfs_set_root_refs(&root->root_item, 0); 4114 btrfs_set_root_refs(&root->root_item, 0);
4114 ret = btrfs_update_root(trans, root->fs_info->tree_root, 4115 ret = btrfs_update_root(trans, root->fs_info->tree_root,
4115 &root->root_key, &root->root_item); 4116 &root->root_key, &root->root_item);
4116 BUG_ON(ret);
4117 4117
4118 ret = btrfs_end_transaction(trans, root->fs_info->tree_root); 4118 err = btrfs_end_transaction(trans, root->fs_info->tree_root);
4119 BUG_ON(ret); 4119 if (err)
4120 return 0; 4120 return err;
4121 return ret;
4121} 4122}
4122 4123
4123/* 4124/*
@@ -4185,7 +4186,11 @@ int btrfs_recover_relocation(struct btrfs_root *root)
4185 err = ret; 4186 err = ret;
4186 goto out; 4187 goto out;
4187 } 4188 }
4188 mark_garbage_root(reloc_root); 4189 ret = mark_garbage_root(reloc_root);
4190 if (ret < 0) {
4191 err = ret;
4192 goto out;
4193 }
4189 } 4194 }
4190 } 4195 }
4191 4196
@@ -4231,14 +4236,19 @@ int btrfs_recover_relocation(struct btrfs_root *root)
4231 4236
4232 fs_root = read_fs_root(root->fs_info, 4237 fs_root = read_fs_root(root->fs_info,
4233 reloc_root->root_key.offset); 4238 reloc_root->root_key.offset);
4234 BUG_ON(IS_ERR(fs_root)); 4239 if (IS_ERR(fs_root)) {
4240 err = PTR_ERR(fs_root);
4241 goto out_free;
4242 }
4235 4243
4236 err = __add_reloc_root(reloc_root); 4244 err = __add_reloc_root(reloc_root);
4237 BUG_ON(err < 0); 4245 BUG_ON(err < 0); /* -ENOMEM or logic error */
4238 fs_root->reloc_root = reloc_root; 4246 fs_root->reloc_root = reloc_root;
4239 } 4247 }
4240 4248
4241 btrfs_commit_transaction(trans, rc->extent_root); 4249 err = btrfs_commit_transaction(trans, rc->extent_root);
4250 if (err)
4251 goto out_free;
4242 4252
4243 merge_reloc_roots(rc); 4253 merge_reloc_roots(rc);
4244 4254
@@ -4248,7 +4258,7 @@ int btrfs_recover_relocation(struct btrfs_root *root)
4248 if (IS_ERR(trans)) 4258 if (IS_ERR(trans))
4249 err = PTR_ERR(trans); 4259 err = PTR_ERR(trans);
4250 else 4260 else
4251 btrfs_commit_transaction(trans, rc->extent_root); 4261 err = btrfs_commit_transaction(trans, rc->extent_root);
4252out_free: 4262out_free:
4253 kfree(rc); 4263 kfree(rc);
4254out: 4264out:
@@ -4297,6 +4307,8 @@ int btrfs_reloc_clone_csums(struct inode *inode, u64 file_pos, u64 len)
4297 disk_bytenr = file_pos + BTRFS_I(inode)->index_cnt; 4307 disk_bytenr = file_pos + BTRFS_I(inode)->index_cnt;
4298 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, disk_bytenr, 4308 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, disk_bytenr,
4299 disk_bytenr + len - 1, &list, 0); 4309 disk_bytenr + len - 1, &list, 0);
4310 if (ret)
4311 goto out;
4300 4312
4301 while (!list_empty(&list)) { 4313 while (!list_empty(&list)) {
4302 sums = list_entry(list.next, struct btrfs_ordered_sum, list); 4314 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
@@ -4314,6 +4326,7 @@ int btrfs_reloc_clone_csums(struct inode *inode, u64 file_pos, u64 len)
4314 4326
4315 btrfs_add_ordered_sum(inode, ordered, sums); 4327 btrfs_add_ordered_sum(inode, ordered, sums);
4316 } 4328 }
4329out:
4317 btrfs_put_ordered_extent(ordered); 4330 btrfs_put_ordered_extent(ordered);
4318 return ret; 4331 return ret;
4319} 4332}
diff --git a/fs/btrfs/root-tree.c b/fs/btrfs/root-tree.c
index 1486cf9de1da..24fb8ce4e071 100644
--- a/fs/btrfs/root-tree.c
+++ b/fs/btrfs/root-tree.c
@@ -97,8 +97,10 @@ int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
97 return -ENOMEM; 97 return -ENOMEM;
98 98
99 ret = btrfs_search_slot(trans, root, key, path, 0, 1); 99 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
100 if (ret < 0) 100 if (ret < 0) {
101 btrfs_abort_transaction(trans, root, ret);
101 goto out; 102 goto out;
103 }
102 104
103 if (ret != 0) { 105 if (ret != 0) {
104 btrfs_print_leaf(root, path->nodes[0]); 106 btrfs_print_leaf(root, path->nodes[0]);
@@ -383,6 +385,8 @@ int btrfs_find_root_ref(struct btrfs_root *tree_root,
383 * 385 *
384 * For a back ref the root_id is the id of the subvol or snapshot and 386 * For a back ref the root_id is the id of the subvol or snapshot and
385 * ref_id is the id of the tree referencing it. 387 * ref_id is the id of the tree referencing it.
388 *
389 * Will return 0, -ENOMEM, or anything from the CoW path
386 */ 390 */
387int btrfs_add_root_ref(struct btrfs_trans_handle *trans, 391int btrfs_add_root_ref(struct btrfs_trans_handle *trans,
388 struct btrfs_root *tree_root, 392 struct btrfs_root *tree_root,
@@ -406,7 +410,11 @@ int btrfs_add_root_ref(struct btrfs_trans_handle *trans,
406again: 410again:
407 ret = btrfs_insert_empty_item(trans, tree_root, path, &key, 411 ret = btrfs_insert_empty_item(trans, tree_root, path, &key,
408 sizeof(*ref) + name_len); 412 sizeof(*ref) + name_len);
409 BUG_ON(ret); 413 if (ret) {
414 btrfs_abort_transaction(trans, tree_root, ret);
415 btrfs_free_path(path);
416 return ret;
417 }
410 418
411 leaf = path->nodes[0]; 419 leaf = path->nodes[0];
412 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); 420 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
diff --git a/fs/btrfs/scrub.c b/fs/btrfs/scrub.c
index 794cbb52f308..0209d8a9ae39 100644
--- a/fs/btrfs/scrub.c
+++ b/fs/btrfs/scrub.c
@@ -1505,6 +1505,9 @@ static noinline_for_stack int scrub_supers(struct scrub_dev *sdev)
1505 struct btrfs_device *device = sdev->dev; 1505 struct btrfs_device *device = sdev->dev;
1506 struct btrfs_root *root = device->dev_root; 1506 struct btrfs_root *root = device->dev_root;
1507 1507
1508 if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR)
1509 return -EIO;
1510
1508 gen = root->fs_info->last_trans_committed; 1511 gen = root->fs_info->last_trans_committed;
1509 1512
1510 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) { 1513 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c
index 0517bd70b04c..9db64165123a 100644
--- a/fs/btrfs/super.c
+++ b/fs/btrfs/super.c
@@ -216,7 +216,7 @@ void __btrfs_abort_transaction(struct btrfs_trans_handle *trans,
216 struct btrfs_root *root, const char *function, 216 struct btrfs_root *root, const char *function,
217 unsigned int line, int errno) 217 unsigned int line, int errno)
218{ 218{
219 WARN_ON_ONCE(1); 219 WARN_ONCE(1, KERN_DEBUG "btrfs: Transaction aborted");
220 trans->aborted = errno; 220 trans->aborted = errno;
221 /* Nothing used. The other threads that have joined this 221 /* Nothing used. The other threads that have joined this
222 * transaction may be able to continue. */ 222 * transaction may be able to continue. */
diff --git a/fs/btrfs/transaction.c b/fs/btrfs/transaction.c
index 5a4999aa8fef..63f835aa9788 100644
--- a/fs/btrfs/transaction.c
+++ b/fs/btrfs/transaction.c
@@ -949,18 +949,19 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
949 dentry->d_name.name, dentry->d_name.len, 949 dentry->d_name.name, dentry->d_name.len,
950 parent_inode, &key, 950 parent_inode, &key,
951 BTRFS_FT_DIR, index); 951 BTRFS_FT_DIR, index);
952 if (ret) { 952 if (ret == -EEXIST) {
953 pending->error = -EEXIST; 953 pending->error = -EEXIST;
954 dput(parent); 954 dput(parent);
955 goto fail; 955 goto fail;
956 } else if (ret) 956 } else if (ret) {
957 goto abort_trans; 957 goto abort_trans_dput;
958 }
958 959
959 btrfs_i_size_write(parent_inode, parent_inode->i_size + 960 btrfs_i_size_write(parent_inode, parent_inode->i_size +
960 dentry->d_name.len * 2); 961 dentry->d_name.len * 2);
961 ret = btrfs_update_inode(trans, parent_root, parent_inode); 962 ret = btrfs_update_inode(trans, parent_root, parent_inode);
962 if (ret) 963 if (ret)
963 goto abort_trans; 964 goto abort_trans_dput;
964 965
965 /* 966 /*
966 * pull in the delayed directory update 967 * pull in the delayed directory update
@@ -969,8 +970,10 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
969 * snapshot 970 * snapshot
970 */ 971 */
971 ret = btrfs_run_delayed_items(trans, root); 972 ret = btrfs_run_delayed_items(trans, root);
972 if (ret) /* Transaction aborted */ 973 if (ret) { /* Transaction aborted */
974 dput(parent);
973 goto fail; 975 goto fail;
976 }
974 977
975 record_root_in_trans(trans, root); 978 record_root_in_trans(trans, root);
976 btrfs_set_root_last_snapshot(&root->root_item, trans->transid); 979 btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
@@ -986,17 +989,20 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
986 989
987 old = btrfs_lock_root_node(root); 990 old = btrfs_lock_root_node(root);
988 ret = btrfs_cow_block(trans, root, old, NULL, 0, &old); 991 ret = btrfs_cow_block(trans, root, old, NULL, 0, &old);
989 if (ret) 992 if (ret) {
990 goto abort_trans; 993 btrfs_tree_unlock(old);
994 free_extent_buffer(old);
995 goto abort_trans_dput;
996 }
991 997
992 btrfs_set_lock_blocking(old); 998 btrfs_set_lock_blocking(old);
993 999
994 ret = btrfs_copy_root(trans, root, old, &tmp, objectid); 1000 ret = btrfs_copy_root(trans, root, old, &tmp, objectid);
995 if (ret) 1001 /* clean up in any case */
996 goto abort_trans;
997
998 btrfs_tree_unlock(old); 1002 btrfs_tree_unlock(old);
999 free_extent_buffer(old); 1003 free_extent_buffer(old);
1004 if (ret)
1005 goto abort_trans_dput;
1000 1006
1001 /* see comments in should_cow_block() */ 1007 /* see comments in should_cow_block() */
1002 root->force_cow = 1; 1008 root->force_cow = 1;
@@ -1009,7 +1015,7 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
1009 btrfs_tree_unlock(tmp); 1015 btrfs_tree_unlock(tmp);
1010 free_extent_buffer(tmp); 1016 free_extent_buffer(tmp);
1011 if (ret) 1017 if (ret)
1012 goto abort_trans; 1018 goto abort_trans_dput;
1013 1019
1014 /* 1020 /*
1015 * insert root back/forward references 1021 * insert root back/forward references
@@ -1018,14 +1024,16 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
1018 parent_root->root_key.objectid, 1024 parent_root->root_key.objectid,
1019 btrfs_ino(parent_inode), index, 1025 btrfs_ino(parent_inode), index,
1020 dentry->d_name.name, dentry->d_name.len); 1026 dentry->d_name.name, dentry->d_name.len);
1027 dput(parent);
1021 if (ret) 1028 if (ret)
1022 goto fail; 1029 goto fail;
1023 dput(parent);
1024 1030
1025 key.offset = (u64)-1; 1031 key.offset = (u64)-1;
1026 pending->snap = btrfs_read_fs_root_no_name(root->fs_info, &key); 1032 pending->snap = btrfs_read_fs_root_no_name(root->fs_info, &key);
1027 if (IS_ERR(pending->snap)) 1033 if (IS_ERR(pending->snap)) {
1034 ret = PTR_ERR(pending->snap);
1028 goto abort_trans; 1035 goto abort_trans;
1036 }
1029 1037
1030 ret = btrfs_reloc_post_snapshot(trans, pending); 1038 ret = btrfs_reloc_post_snapshot(trans, pending);
1031 if (ret) 1039 if (ret)
@@ -1037,6 +1045,8 @@ fail:
1037 btrfs_block_rsv_release(root, &pending->block_rsv, (u64)-1); 1045 btrfs_block_rsv_release(root, &pending->block_rsv, (u64)-1);
1038 return ret; 1046 return ret;
1039 1047
1048abort_trans_dput:
1049 dput(parent);
1040abort_trans: 1050abort_trans:
1041 btrfs_abort_transaction(trans, root, ret); 1051 btrfs_abort_transaction(trans, root, ret);
1042 goto fail; 1052 goto fail;
diff --git a/fs/btrfs/tree-log.c b/fs/btrfs/tree-log.c
index 37b52b8c6727..d017283ae6f5 100644
--- a/fs/btrfs/tree-log.c
+++ b/fs/btrfs/tree-log.c
@@ -1761,7 +1761,7 @@ static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
1761 BTRFS_TREE_LOG_OBJECTID); 1761 BTRFS_TREE_LOG_OBJECTID);
1762 ret = btrfs_free_and_pin_reserved_extent(root, 1762 ret = btrfs_free_and_pin_reserved_extent(root,
1763 bytenr, blocksize); 1763 bytenr, blocksize);
1764 BUG_ON(ret); 1764 BUG_ON(ret); /* -ENOMEM or logic errors */
1765 } 1765 }
1766 free_extent_buffer(next); 1766 free_extent_buffer(next);
1767 continue; 1767 continue;
@@ -1869,20 +1869,26 @@ static int walk_log_tree(struct btrfs_trans_handle *trans,
1869 wret = walk_down_log_tree(trans, log, path, &level, wc); 1869 wret = walk_down_log_tree(trans, log, path, &level, wc);
1870 if (wret > 0) 1870 if (wret > 0)
1871 break; 1871 break;
1872 if (wret < 0) 1872 if (wret < 0) {
1873 ret = wret; 1873 ret = wret;
1874 goto out;
1875 }
1874 1876
1875 wret = walk_up_log_tree(trans, log, path, &level, wc); 1877 wret = walk_up_log_tree(trans, log, path, &level, wc);
1876 if (wret > 0) 1878 if (wret > 0)
1877 break; 1879 break;
1878 if (wret < 0) 1880 if (wret < 0) {
1879 ret = wret; 1881 ret = wret;
1882 goto out;
1883 }
1880 } 1884 }
1881 1885
1882 /* was the root node processed? if not, catch it here */ 1886 /* was the root node processed? if not, catch it here */
1883 if (path->nodes[orig_level]) { 1887 if (path->nodes[orig_level]) {
1884 wc->process_func(log, path->nodes[orig_level], wc, 1888 ret = wc->process_func(log, path->nodes[orig_level], wc,
1885 btrfs_header_generation(path->nodes[orig_level])); 1889 btrfs_header_generation(path->nodes[orig_level]));
1890 if (ret)
1891 goto out;
1886 if (wc->free) { 1892 if (wc->free) {
1887 struct extent_buffer *next; 1893 struct extent_buffer *next;
1888 1894
@@ -1898,10 +1904,11 @@ static int walk_log_tree(struct btrfs_trans_handle *trans,
1898 BTRFS_TREE_LOG_OBJECTID); 1904 BTRFS_TREE_LOG_OBJECTID);
1899 ret = btrfs_free_and_pin_reserved_extent(log, next->start, 1905 ret = btrfs_free_and_pin_reserved_extent(log, next->start,
1900 next->len); 1906 next->len);
1901 BUG_ON(ret); 1907 BUG_ON(ret); /* -ENOMEM or logic errors */
1902 } 1908 }
1903 } 1909 }
1904 1910
1911out:
1905 for (i = 0; i <= orig_level; i++) { 1912 for (i = 0; i <= orig_level; i++) {
1906 if (path->nodes[i]) { 1913 if (path->nodes[i]) {
1907 free_extent_buffer(path->nodes[i]); 1914 free_extent_buffer(path->nodes[i]);
@@ -2043,7 +2050,11 @@ int btrfs_sync_log(struct btrfs_trans_handle *trans,
2043 * wait for them until later. 2050 * wait for them until later.
2044 */ 2051 */
2045 ret = btrfs_write_marked_extents(log, &log->dirty_log_pages, mark); 2052 ret = btrfs_write_marked_extents(log, &log->dirty_log_pages, mark);
2046 BUG_ON(ret); 2053 if (ret) {
2054 btrfs_abort_transaction(trans, root, ret);
2055 mutex_unlock(&root->log_mutex);
2056 goto out;
2057 }
2047 2058
2048 btrfs_set_root_node(&log->root_item, log->node); 2059 btrfs_set_root_node(&log->root_item, log->node);
2049 2060
@@ -2074,7 +2085,11 @@ int btrfs_sync_log(struct btrfs_trans_handle *trans,
2074 } 2085 }
2075 2086
2076 if (ret) { 2087 if (ret) {
2077 BUG_ON(ret != -ENOSPC); 2088 if (ret != -ENOSPC) {
2089 btrfs_abort_transaction(trans, root, ret);
2090 mutex_unlock(&log_root_tree->log_mutex);
2091 goto out;
2092 }
2078 root->fs_info->last_trans_log_full_commit = trans->transid; 2093 root->fs_info->last_trans_log_full_commit = trans->transid;
2079 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark); 2094 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
2080 mutex_unlock(&log_root_tree->log_mutex); 2095 mutex_unlock(&log_root_tree->log_mutex);
@@ -2114,7 +2129,11 @@ int btrfs_sync_log(struct btrfs_trans_handle *trans,
2114 ret = btrfs_write_and_wait_marked_extents(log_root_tree, 2129 ret = btrfs_write_and_wait_marked_extents(log_root_tree,
2115 &log_root_tree->dirty_log_pages, 2130 &log_root_tree->dirty_log_pages,
2116 EXTENT_DIRTY | EXTENT_NEW); 2131 EXTENT_DIRTY | EXTENT_NEW);
2117 BUG_ON(ret); 2132 if (ret) {
2133 btrfs_abort_transaction(trans, root, ret);
2134 mutex_unlock(&log_root_tree->log_mutex);
2135 goto out_wake_log_root;
2136 }
2118 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark); 2137 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
2119 2138
2120 btrfs_set_super_log_root(root->fs_info->super_for_commit, 2139 btrfs_set_super_log_root(root->fs_info->super_for_commit,
@@ -2323,7 +2342,9 @@ out_unlock:
2323 if (ret == -ENOSPC) { 2342 if (ret == -ENOSPC) {
2324 root->fs_info->last_trans_log_full_commit = trans->transid; 2343 root->fs_info->last_trans_log_full_commit = trans->transid;
2325 ret = 0; 2344 ret = 0;
2326 } 2345 } else if (ret < 0)
2346 btrfs_abort_transaction(trans, root, ret);
2347
2327 btrfs_end_log_trans(root); 2348 btrfs_end_log_trans(root);
2328 2349
2329 return err; 2350 return err;
@@ -2354,7 +2375,8 @@ int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
2354 if (ret == -ENOSPC) { 2375 if (ret == -ENOSPC) {
2355 root->fs_info->last_trans_log_full_commit = trans->transid; 2376 root->fs_info->last_trans_log_full_commit = trans->transid;
2356 ret = 0; 2377 ret = 0;
2357 } 2378 } else if (ret < 0 && ret != -ENOENT)
2379 btrfs_abort_transaction(trans, root, ret);
2358 btrfs_end_log_trans(root); 2380 btrfs_end_log_trans(root);
2359 2381
2360 return ret; 2382 return ret;
@@ -3166,13 +3188,20 @@ int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
3166 fs_info->log_root_recovering = 1; 3188 fs_info->log_root_recovering = 1;
3167 3189
3168 trans = btrfs_start_transaction(fs_info->tree_root, 0); 3190 trans = btrfs_start_transaction(fs_info->tree_root, 0);
3169 BUG_ON(IS_ERR(trans)); 3191 if (IS_ERR(trans)) {
3192 ret = PTR_ERR(trans);
3193 goto error;
3194 }
3170 3195
3171 wc.trans = trans; 3196 wc.trans = trans;
3172 wc.pin = 1; 3197 wc.pin = 1;
3173 3198
3174 ret = walk_log_tree(trans, log_root_tree, &wc); 3199 ret = walk_log_tree(trans, log_root_tree, &wc);
3175 BUG_ON(ret); 3200 if (ret) {
3201 btrfs_error(fs_info, ret, "Failed to pin buffers while "
3202 "recovering log root tree.");
3203 goto error;
3204 }
3176 3205
3177again: 3206again:
3178 key.objectid = BTRFS_TREE_LOG_OBJECTID; 3207 key.objectid = BTRFS_TREE_LOG_OBJECTID;
@@ -3181,8 +3210,12 @@ again:
3181 3210
3182 while (1) { 3211 while (1) {
3183 ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0); 3212 ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
3184 if (ret < 0) 3213
3185 break; 3214 if (ret < 0) {
3215 btrfs_error(fs_info, ret,
3216 "Couldn't find tree log root.");
3217 goto error;
3218 }
3186 if (ret > 0) { 3219 if (ret > 0) {
3187 if (path->slots[0] == 0) 3220 if (path->slots[0] == 0)
3188 break; 3221 break;
@@ -3196,14 +3229,24 @@ again:
3196 3229
3197 log = btrfs_read_fs_root_no_radix(log_root_tree, 3230 log = btrfs_read_fs_root_no_radix(log_root_tree,
3198 &found_key); 3231 &found_key);
3199 BUG_ON(IS_ERR(log)); 3232 if (IS_ERR(log)) {
3233 ret = PTR_ERR(log);
3234 btrfs_error(fs_info, ret,
3235 "Couldn't read tree log root.");
3236 goto error;
3237 }
3200 3238
3201 tmp_key.objectid = found_key.offset; 3239 tmp_key.objectid = found_key.offset;
3202 tmp_key.type = BTRFS_ROOT_ITEM_KEY; 3240 tmp_key.type = BTRFS_ROOT_ITEM_KEY;
3203 tmp_key.offset = (u64)-1; 3241 tmp_key.offset = (u64)-1;
3204 3242
3205 wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key); 3243 wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
3206 BUG_ON(IS_ERR_OR_NULL(wc.replay_dest)); 3244 if (IS_ERR(wc.replay_dest)) {
3245 ret = PTR_ERR(wc.replay_dest);
3246 btrfs_error(fs_info, ret, "Couldn't read target root "
3247 "for tree log recovery.");
3248 goto error;
3249 }
3207 3250
3208 wc.replay_dest->log_root = log; 3251 wc.replay_dest->log_root = log;
3209 btrfs_record_root_in_trans(trans, wc.replay_dest); 3252 btrfs_record_root_in_trans(trans, wc.replay_dest);
@@ -3251,6 +3294,10 @@ again:
3251 3294
3252 kfree(log_root_tree); 3295 kfree(log_root_tree);
3253 return 0; 3296 return 0;
3297
3298error:
3299 btrfs_free_path(path);
3300 return ret;
3254} 3301}
3255 3302
3256/* 3303/*
diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
index 394bf15ea18c..57305e88ea82 100644
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
@@ -549,10 +549,10 @@ static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
549 fs_devices->num_can_discard--; 549 fs_devices->num_can_discard--;
550 550
551 new_device = kmalloc(sizeof(*new_device), GFP_NOFS); 551 new_device = kmalloc(sizeof(*new_device), GFP_NOFS);
552 BUG_ON(!new_device); 552 BUG_ON(!new_device); /* -ENOMEM */
553 memcpy(new_device, device, sizeof(*new_device)); 553 memcpy(new_device, device, sizeof(*new_device));
554 new_device->name = kstrdup(device->name, GFP_NOFS); 554 new_device->name = kstrdup(device->name, GFP_NOFS);
555 BUG_ON(device->name && !new_device->name); 555 BUG_ON(device->name && !new_device->name); /* -ENOMEM */
556 new_device->bdev = NULL; 556 new_device->bdev = NULL;
557 new_device->writeable = 0; 557 new_device->writeable = 0;
558 new_device->in_fs_metadata = 0; 558 new_device->in_fs_metadata = 0;
@@ -1036,8 +1036,10 @@ again:
1036 leaf = path->nodes[0]; 1036 leaf = path->nodes[0];
1037 extent = btrfs_item_ptr(leaf, path->slots[0], 1037 extent = btrfs_item_ptr(leaf, path->slots[0],
1038 struct btrfs_dev_extent); 1038 struct btrfs_dev_extent);
1039 } else {
1040 btrfs_error(root->fs_info, ret, "Slot search failed");
1041 goto out;
1039 } 1042 }
1040 BUG_ON(ret);
1041 1043
1042 if (device->bytes_used > 0) { 1044 if (device->bytes_used > 0) {
1043 u64 len = btrfs_dev_extent_length(leaf, extent); 1045 u64 len = btrfs_dev_extent_length(leaf, extent);
@@ -1047,7 +1049,10 @@ again:
1047 spin_unlock(&root->fs_info->free_chunk_lock); 1049 spin_unlock(&root->fs_info->free_chunk_lock);
1048 } 1050 }
1049 ret = btrfs_del_item(trans, root, path); 1051 ret = btrfs_del_item(trans, root, path);
1050 1052 if (ret) {
1053 btrfs_error(root->fs_info, ret,
1054 "Failed to remove dev extent item");
1055 }
1051out: 1056out:
1052 btrfs_free_path(path); 1057 btrfs_free_path(path);
1053 return ret; 1058 return ret;
@@ -1117,7 +1122,7 @@ static noinline int find_next_chunk(struct btrfs_root *root,
1117 if (ret < 0) 1122 if (ret < 0)
1118 goto error; 1123 goto error;
1119 1124
1120 BUG_ON(ret == 0); 1125 BUG_ON(ret == 0); /* Corruption */
1121 1126
1122 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY); 1127 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
1123 if (ret) { 1128 if (ret) {
@@ -1161,7 +1166,7 @@ static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
1161 if (ret < 0) 1166 if (ret < 0)
1162 goto error; 1167 goto error;
1163 1168
1164 BUG_ON(ret == 0); 1169 BUG_ON(ret == 0); /* Corruption */
1165 1170
1166 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID, 1171 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1167 BTRFS_DEV_ITEM_KEY); 1172 BTRFS_DEV_ITEM_KEY);
@@ -1595,7 +1600,7 @@ next_slot:
1595 (unsigned long)btrfs_device_fsid(dev_item), 1600 (unsigned long)btrfs_device_fsid(dev_item),
1596 BTRFS_UUID_SIZE); 1601 BTRFS_UUID_SIZE);
1597 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid); 1602 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1598 BUG_ON(!device); 1603 BUG_ON(!device); /* Logic error */
1599 1604
1600 if (device->fs_devices->seeding) { 1605 if (device->fs_devices->seeding) {
1601 btrfs_set_device_generation(leaf, dev_item, 1606 btrfs_set_device_generation(leaf, dev_item,
@@ -1705,7 +1710,7 @@ int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1705 if (seeding_dev) { 1710 if (seeding_dev) {
1706 sb->s_flags &= ~MS_RDONLY; 1711 sb->s_flags &= ~MS_RDONLY;
1707 ret = btrfs_prepare_sprout(root); 1712 ret = btrfs_prepare_sprout(root);
1708 BUG_ON(ret); 1713 BUG_ON(ret); /* -ENOMEM */
1709 } 1714 }
1710 1715
1711 device->fs_devices = root->fs_info->fs_devices; 1716 device->fs_devices = root->fs_info->fs_devices;
@@ -1743,11 +1748,15 @@ int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1743 1748
1744 if (seeding_dev) { 1749 if (seeding_dev) {
1745 ret = init_first_rw_device(trans, root, device); 1750 ret = init_first_rw_device(trans, root, device);
1746 BUG_ON(ret); 1751 if (ret)
1752 goto error_trans;
1747 ret = btrfs_finish_sprout(trans, root); 1753 ret = btrfs_finish_sprout(trans, root);
1748 BUG_ON(ret); 1754 if (ret)
1755 goto error_trans;
1749 } else { 1756 } else {
1750 ret = btrfs_add_device(trans, root, device); 1757 ret = btrfs_add_device(trans, root, device);
1758 if (ret)
1759 goto error_trans;
1751 } 1760 }
1752 1761
1753 /* 1762 /*
@@ -1757,17 +1766,31 @@ int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1757 btrfs_clear_space_info_full(root->fs_info); 1766 btrfs_clear_space_info_full(root->fs_info);
1758 1767
1759 unlock_chunks(root); 1768 unlock_chunks(root);
1760 btrfs_commit_transaction(trans, root); 1769 ret = btrfs_commit_transaction(trans, root);
1761 1770
1762 if (seeding_dev) { 1771 if (seeding_dev) {
1763 mutex_unlock(&uuid_mutex); 1772 mutex_unlock(&uuid_mutex);
1764 up_write(&sb->s_umount); 1773 up_write(&sb->s_umount);
1765 1774
1775 if (ret) /* transaction commit */
1776 return ret;
1777
1766 ret = btrfs_relocate_sys_chunks(root); 1778 ret = btrfs_relocate_sys_chunks(root);
1767 BUG_ON(ret); 1779 if (ret < 0)
1780 btrfs_error(root->fs_info, ret,
1781 "Failed to relocate sys chunks after "
1782 "device initialization. This can be fixed "
1783 "using the \"btrfs balance\" command.");
1768 } 1784 }
1769 1785
1770 return ret; 1786 return ret;
1787
1788error_trans:
1789 unlock_chunks(root);
1790 btrfs_abort_transaction(trans, root, ret);
1791 btrfs_end_transaction(trans, root);
1792 kfree(device->name);
1793 kfree(device);
1771error: 1794error:
1772 blkdev_put(bdev, FMODE_EXCL); 1795 blkdev_put(bdev, FMODE_EXCL);
1773 if (seeding_dev) { 1796 if (seeding_dev) {
@@ -1875,10 +1898,20 @@ static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1875 key.type = BTRFS_CHUNK_ITEM_KEY; 1898 key.type = BTRFS_CHUNK_ITEM_KEY;
1876 1899
1877 ret = btrfs_search_slot(trans, root, &key, path, -1, 1); 1900 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1878 BUG_ON(ret); 1901 if (ret < 0)
1902 goto out;
1903 else if (ret > 0) { /* Logic error or corruption */
1904 btrfs_error(root->fs_info, -ENOENT,
1905 "Failed lookup while freeing chunk.");
1906 ret = -ENOENT;
1907 goto out;
1908 }
1879 1909
1880 ret = btrfs_del_item(trans, root, path); 1910 ret = btrfs_del_item(trans, root, path);
1881 1911 if (ret < 0)
1912 btrfs_error(root->fs_info, ret,
1913 "Failed to delete chunk item.");
1914out:
1882 btrfs_free_path(path); 1915 btrfs_free_path(path);
1883 return ret; 1916 return ret;
1884} 1917}
@@ -2040,7 +2073,7 @@ again:
2040 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); 2073 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2041 if (ret < 0) 2074 if (ret < 0)
2042 goto error; 2075 goto error;
2043 BUG_ON(ret == 0); 2076 BUG_ON(ret == 0); /* Corruption */
2044 2077
2045 ret = btrfs_previous_item(chunk_root, path, key.objectid, 2078 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2046 key.type); 2079 key.type);
@@ -3334,7 +3367,8 @@ static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3334 ret = btrfs_make_block_group(trans, extent_root, 0, type, 3367 ret = btrfs_make_block_group(trans, extent_root, 0, type,
3335 BTRFS_FIRST_CHUNK_TREE_OBJECTID, 3368 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
3336 start, num_bytes); 3369 start, num_bytes);
3337 BUG_ON(ret); 3370 if (ret)
3371 goto error;
3338 3372
3339 for (i = 0; i < map->num_stripes; ++i) { 3373 for (i = 0; i < map->num_stripes; ++i) {
3340 struct btrfs_device *device; 3374 struct btrfs_device *device;
@@ -3347,7 +3381,10 @@ static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3347 info->chunk_root->root_key.objectid, 3381 info->chunk_root->root_key.objectid,
3348 BTRFS_FIRST_CHUNK_TREE_OBJECTID, 3382 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
3349 start, dev_offset, stripe_size); 3383 start, dev_offset, stripe_size);
3350 BUG_ON(ret); 3384 if (ret) {
3385 btrfs_abort_transaction(trans, extent_root, ret);
3386 goto error;
3387 }
3351 } 3388 }
3352 3389
3353 kfree(devices_info); 3390 kfree(devices_info);
@@ -3465,7 +3502,8 @@ int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3465 3502
3466 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset, 3503 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
3467 chunk_size, stripe_size); 3504 chunk_size, stripe_size);
3468 BUG_ON(ret); 3505 if (ret)
3506 return ret;
3469 return 0; 3507 return 0;
3470} 3508}
3471 3509
@@ -3497,7 +3535,8 @@ static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
3497 3535
3498 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size, 3536 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
3499 &stripe_size, chunk_offset, alloc_profile); 3537 &stripe_size, chunk_offset, alloc_profile);
3500 BUG_ON(ret); 3538 if (ret)
3539 return ret;
3501 3540
3502 sys_chunk_offset = chunk_offset + chunk_size; 3541 sys_chunk_offset = chunk_offset + chunk_size;
3503 3542
@@ -3508,10 +3547,12 @@ static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
3508 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map, 3547 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
3509 &sys_chunk_size, &sys_stripe_size, 3548 &sys_chunk_size, &sys_stripe_size,
3510 sys_chunk_offset, alloc_profile); 3549 sys_chunk_offset, alloc_profile);
3511 BUG_ON(ret); 3550 if (ret)
3551 goto abort;
3512 3552
3513 ret = btrfs_add_device(trans, fs_info->chunk_root, device); 3553 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
3514 BUG_ON(ret); 3554 if (ret)
3555 goto abort;
3515 3556
3516 /* 3557 /*
3517 * Modifying chunk tree needs allocating new blocks from both 3558 * Modifying chunk tree needs allocating new blocks from both
@@ -3521,13 +3562,20 @@ static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
3521 */ 3562 */
3522 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset, 3563 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
3523 chunk_size, stripe_size); 3564 chunk_size, stripe_size);
3524 BUG_ON(ret); 3565 if (ret)
3566 goto abort;
3525 3567
3526 ret = __finish_chunk_alloc(trans, extent_root, sys_map, 3568 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
3527 sys_chunk_offset, sys_chunk_size, 3569 sys_chunk_offset, sys_chunk_size,
3528 sys_stripe_size); 3570 sys_stripe_size);
3529 BUG_ON(ret); 3571 if (ret)
3572 goto abort;
3573
3530 return 0; 3574 return 0;
3575
3576abort:
3577 btrfs_abort_transaction(trans, root, ret);
3578 return ret;
3531} 3579}
3532 3580
3533int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset) 3581int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
@@ -3878,7 +3926,7 @@ int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
3878 do_div(length, map->num_stripes); 3926 do_div(length, map->num_stripes);
3879 3927
3880 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS); 3928 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
3881 BUG_ON(!buf); 3929 BUG_ON(!buf); /* -ENOMEM */
3882 3930
3883 for (i = 0; i < map->num_stripes; i++) { 3931 for (i = 0; i < map->num_stripes; i++) {
3884 if (devid && map->stripes[i].dev->devid != devid) 3932 if (devid && map->stripes[i].dev->devid != devid)
@@ -4039,7 +4087,8 @@ int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
4039 4087
4040 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &bbio, 4088 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &bbio,
4041 mirror_num); 4089 mirror_num);
4042 BUG_ON(ret); 4090 if (ret) /* -ENOMEM */
4091 return ret;
4043 4092
4044 total_devs = bbio->num_stripes; 4093 total_devs = bbio->num_stripes;
4045 if (map_length < length) { 4094 if (map_length < length) {
@@ -4058,7 +4107,7 @@ int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
4058 while (dev_nr < total_devs) { 4107 while (dev_nr < total_devs) {
4059 if (dev_nr < total_devs - 1) { 4108 if (dev_nr < total_devs - 1) {
4060 bio = bio_clone(first_bio, GFP_NOFS); 4109 bio = bio_clone(first_bio, GFP_NOFS);
4061 BUG_ON(!bio); 4110 BUG_ON(!bio); /* -ENOMEM */
4062 } else { 4111 } else {
4063 bio = first_bio; 4112 bio = first_bio;
4064 } 4113 }
@@ -4212,7 +4261,7 @@ static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
4212 write_lock(&map_tree->map_tree.lock); 4261 write_lock(&map_tree->map_tree.lock);
4213 ret = add_extent_mapping(&map_tree->map_tree, em); 4262 ret = add_extent_mapping(&map_tree->map_tree, em);
4214 write_unlock(&map_tree->map_tree.lock); 4263 write_unlock(&map_tree->map_tree.lock);
4215 BUG_ON(ret); 4264 BUG_ON(ret); /* Tree corruption */
4216 free_extent_map(em); 4265 free_extent_map(em);
4217 4266
4218 return 0; 4267 return 0;