Btrfs: crash recovery fixes

Signed-off-by: Chris Mason <chris.mason@oracle.com>
diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c
index 01dc305..14b9326 100644
--- a/fs/btrfs/extent-tree.c
+++ b/fs/btrfs/extent-tree.c
@@ -523,6 +523,7 @@
 	}
 	return 0;
 fail:
+	WARN_ON(1);
 	for (i =0; i < faili; i++) {
 		if (leaf) {
 			u64 disk_blocknr;
@@ -572,7 +573,7 @@
 	bi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
 			    struct btrfs_block_group_item);
 	memcpy(bi, &cache->item, sizeof(*bi));
-	mark_buffer_dirty(path->nodes[0]);
+	btrfs_mark_buffer_dirty(path->nodes[0]);
 	btrfs_release_path(extent_root, path);
 fail:
 	finish_current_insert(trans, extent_root);
@@ -739,8 +740,30 @@
 	return ret;
 }
 
-int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
-			       btrfs_root *root)
+int btrfs_copy_pinned(struct btrfs_root *root, struct radix_tree_root *copy)
+{
+	unsigned long gang[8];
+	u64 last = 0;
+	struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
+	int ret;
+	int i;
+
+	while(1) {
+		ret = find_first_radix_bit(pinned_radix, gang, last,
+					   ARRAY_SIZE(gang));
+		if (!ret)
+			break;
+		for (i = 0 ; i < ret; i++) {
+			set_radix_bit(copy, gang[i]);
+			last = gang[i] + 1;
+		}
+	}
+	return 0;
+}
+
+int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
+			       struct btrfs_root *root,
+			       struct radix_tree_root *unpin_radix)
 {
 	unsigned long gang[8];
 	struct inode *btree_inode = root->fs_info->btree_inode;
@@ -752,7 +775,7 @@
 	struct radix_tree_root *extent_radix = &root->fs_info->extent_map_radix;
 
 	while(1) {
-		ret = find_first_radix_bit(pinned_radix, gang, 0,
+		ret = find_first_radix_bit(unpin_radix, gang, 0,
 					   ARRAY_SIZE(gang));
 		if (!ret)
 			break;
@@ -760,6 +783,7 @@
 			first = gang[0];
 		for (i = 0; i < ret; i++) {
 			clear_radix_bit(pinned_radix, gang[i]);
+			clear_radix_bit(unpin_radix, gang[i]);
 			block_group = btrfs_lookup_block_group(root->fs_info,
 							       gang[i]);
 			if (block_group) {
@@ -1309,6 +1333,7 @@
 	if (data) {
 		ret = find_free_extent(trans, root, 0, 0,
 				       search_end, 0, &prealloc_key, 0, 0, 0);
+		BUG_ON(ret);
 		if (ret)
 			return ret;
 		exclude_nr = info->extent_tree_prealloc_nr;
@@ -1319,6 +1344,7 @@
 	ret = find_free_extent(trans, root, num_blocks, search_start,
 			       search_end, hint_block, ins,
 			       exclude_start, exclude_nr, data);
+	BUG_ON(ret);
 	if (ret)
 		return ret;
 
@@ -1334,10 +1360,12 @@
 	if (!data) {
 		exclude_start = ins->objectid;
 		exclude_nr = ins->offset;
+		hint_block = exclude_start + exclude_nr;
 		ret = find_free_extent(trans, root, 0, search_start,
 				       search_end, hint_block,
 				       &prealloc_key, exclude_start,
 				       exclude_nr, 0);
+		BUG_ON(ret);
 		if (ret)
 			return ret;
 	}
@@ -1348,6 +1376,7 @@
 	ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
 				sizeof(extent_item));
 
+	BUG_ON(ret);
 	finish_current_insert(trans, extent_root);
 	pending_ret = del_pending_extents(trans, extent_root);
 	if (ret) {