Btrfs: Add btree locking to the tree defragmentation code

The online btree defragger is simplified and rewritten to use
standard btree searches instead of a walk up / down mechanism.

Signed-off-by: Chris Mason <chris.mason@oracle.com>
diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c
index 89cc4f6..a9b3a25 100644
--- a/fs/btrfs/extent-tree.c
+++ b/fs/btrfs/extent-tree.c
@@ -2201,6 +2201,7 @@
 {
 	mutex_unlock(&root->fs_info->alloc_mutex);
 	lookup_extent_ref(NULL, root, start, len, refs);
+	cond_resched();
 	mutex_lock(&root->fs_info->alloc_mutex);
 	return lookup_extent_ref(NULL, root, start, len, refs);
 }
@@ -2280,6 +2281,7 @@
 
 			next = read_tree_block(root, bytenr, blocksize,
 					       ptr_gen);
+			cond_resched();
 			mutex_lock(&root->fs_info->alloc_mutex);
 
 			/* we've dropped the lock, double check */
@@ -2329,6 +2331,7 @@
 	*level += 1;
 	BUG_ON(ret);
 	mutex_unlock(&root->fs_info->alloc_mutex);
+	cond_resched();
 	return 0;
 }
 
@@ -2448,6 +2451,10 @@
 			break;
 		if (wret < 0)
 			ret = wret;
+		if (trans->transaction->in_commit) {
+			ret = -EAGAIN;
+			break;
+		}
 	}
 	for (i = 0; i <= orig_level; i++) {
 		if (path->nodes[i]) {