Btrfs: Fix extent_buffer usage when nodesize != leafsize

Signed-off-by: Chris Mason <chris.mason@oracle.com>
diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c
index 99a8b0f..2c569b4 100644
--- a/fs/btrfs/extent-tree.c
+++ b/fs/btrfs/extent-tree.c
@@ -1212,6 +1212,7 @@
 				u64 header_transid =
 					btrfs_header_generation(buf);
 				if (header_transid == transid) {
+					clean_tree_block(NULL, root, buf);
 					free_extent_buffer(buf);
 					return 1;
 				}
@@ -1249,7 +1250,6 @@
 	key.objectid = bytenr;
 	btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
 	key.offset = num_bytes;
-
 	path = btrfs_alloc_path();
 	if (!path)
 		return -ENOMEM;
@@ -1648,8 +1648,6 @@
 			       search_start, search_end, hint_byte, ins,
 			       trans->alloc_exclude_start,
 			       trans->alloc_exclude_nr, data);
-if (ret)
-printk("find free extent returns %d\n", ret);
 	BUG_ON(ret);
 	if (ret)
 		return ret;
@@ -1764,7 +1762,16 @@
 				  0, 0, 0);
 		return ERR_PTR(-ENOMEM);
 	}
+	btrfs_set_header_generation(buf, trans->transid);
+	clean_tree_block(trans, root, buf);
+	wait_on_tree_block_writeback(root, buf);
 	btrfs_set_buffer_uptodate(buf);
+
+	if (PageDirty(buf->first_page)) {
+		printk("page %lu dirty\n", buf->first_page->index);
+		WARN_ON(1);
+	}
+
 	set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
 			 buf->start + buf->len - 1, GFP_NOFS);
 	set_extent_bits(&BTRFS_I(root->fs_info->btree_inode)->extent_tree,