Add btrfs_end_transaction_throttle to force writers to wait for pending commits

The existing throttle mechanism was often not sufficient to prevent
new writers from coming in and making a given transaction run forever.
This adds an explicit wait at the end of most operations so they will
allow the current transaction to close.

There is no wait inside file_write, inode updates, or cow filling, all which
have different deadlock possibilities.

This is a temporary measure until better asynchronous commit support is
added.  This code leads to stalls as it waits for data=ordered
writeback, and it really needs to be fixed.

Signed-off-by: Chris Mason <chris.mason@oracle.com>
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index cf27b59..bbba335 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -855,10 +855,9 @@
 		btrfs_del_ordered_inode(inode, 1);
 	}
 
-	btrfs_end_transaction(trans, root);
+	btrfs_end_transaction_throttle(trans, root);
 fail:
 	btrfs_btree_balance_dirty(root, nr);
-	btrfs_throttle(root);
 	return ret;
 }
 
@@ -889,10 +888,9 @@
 	}
 
 	nr = trans->blocks_used;
-	ret = btrfs_end_transaction(trans, root);
+	ret = btrfs_end_transaction_throttle(trans, root);
 fail:
 	btrfs_btree_balance_dirty(root, nr);
-	btrfs_throttle(root);
 
 	if (ret && !err)
 		err = ret;
@@ -1871,14 +1869,13 @@
 	btrfs_update_inode_block_group(trans, dir);
 out_unlock:
 	nr = trans->blocks_used;
-	btrfs_end_transaction(trans, root);
+	btrfs_end_transaction_throttle(trans, root);
 fail:
 	if (drop_inode) {
 		inode_dec_link_count(inode);
 		iput(inode);
 	}
 	btrfs_btree_balance_dirty(root, nr);
-	btrfs_throttle(root);
 	return err;
 }
 
@@ -1936,14 +1933,13 @@
 	btrfs_update_inode_block_group(trans, dir);
 out_unlock:
 	nr = trans->blocks_used;
-	btrfs_end_transaction(trans, root);
+	btrfs_end_transaction_throttle(trans, root);
 fail:
 	if (drop_inode) {
 		inode_dec_link_count(inode);
 		iput(inode);
 	}
 	btrfs_btree_balance_dirty(root, nr);
-	btrfs_throttle(root);
 	return err;
 }
 
@@ -1985,14 +1981,13 @@
 		drop_inode = 1;
 
 	nr = trans->blocks_used;
-	btrfs_end_transaction(trans, root);
+	btrfs_end_transaction_throttle(trans, root);
 fail:
 	if (drop_inode) {
 		inode_dec_link_count(inode);
 		iput(inode);
 	}
 	btrfs_btree_balance_dirty(root, nr);
-	btrfs_throttle(root);
 	return err;
 }
 
@@ -2055,13 +2050,12 @@
 
 out_fail:
 	nr = trans->blocks_used;
-	btrfs_end_transaction(trans, root);
+	btrfs_end_transaction_throttle(trans, root);
 
 out_unlock:
 	if (drop_on_err)
 		iput(inode);
 	btrfs_btree_balance_dirty(root, nr);
-	btrfs_throttle(root);
 	return err;
 }
 
@@ -2587,10 +2581,9 @@
 	btrfs_update_inode(trans, root, inode);
 	nr = trans->blocks_used;
 
-	ret = btrfs_end_transaction(trans, root);
+	ret = btrfs_end_transaction_throttle(trans, root);
 	BUG_ON(ret);
 	btrfs_btree_balance_dirty(root, nr);
-	btrfs_throttle(root);
 }
 
 /*
@@ -2912,14 +2905,13 @@
 
 out_unlock:
 	nr = trans->blocks_used;
-	btrfs_end_transaction(trans, root);
+	btrfs_end_transaction_throttle(trans, root);
 out_fail:
 	if (drop_inode) {
 		inode_dec_link_count(inode);
 		iput(inode);
 	}
 	btrfs_btree_balance_dirty(root, nr);
-	btrfs_throttle(root);
 	return err;
 }