bcache: Add struct bset_sort_state

More disentangling bset.c from the rest of the bcache code - soon, the
sorting routines won't have any dependencies on any outside structs.

Signed-off-by: Kent Overstreet <kmo@daterainc.com>
diff --git a/drivers/md/bcache/btree.c b/drivers/md/bcache/btree.c
index e1e36e7..78ba0b6 100644
--- a/drivers/md/bcache/btree.c
+++ b/drivers/md/bcache/btree.c
@@ -263,7 +263,7 @@
 		if (i->seq == b->sets[0].data->seq)
 			goto err;
 
-	bch_btree_sort_and_fix_extents(b, iter);
+	bch_btree_sort_and_fix_extents(b, iter, &b->c->sort);
 
 	i = b->sets[0].data;
 	err = "short btree key";
@@ -476,7 +476,11 @@
 	atomic_long_add(set_blocks(i, b->c) * b->c->sb.block_size,
 			&PTR_CACHE(b->c, &b->key, 0)->btree_sectors_written);
 
-	bch_btree_sort_lazy(b);
+	/* If not a leaf node, always sort */
+	if (b->level && b->nsets)
+		bch_btree_sort(b, &b->c->sort);
+	else
+		bch_btree_sort_lazy(b, &b->c->sort);
 
 	/*
 	 * do verify if there was more than one set initially (i.e. we did a
@@ -1125,8 +1129,10 @@
 static struct btree *btree_node_alloc_replacement(struct btree *b, bool wait)
 {
 	struct btree *n = bch_btree_node_alloc(b->c, b->level, wait);
-	if (!IS_ERR_OR_NULL(n))
-		bch_btree_sort_into(b, n);
+	if (!IS_ERR_OR_NULL(n)) {
+		bch_btree_sort_into(b, n, &b->c->sort);
+		bkey_copy_key(&n->key, &b->key);
+	}
 
 	return n;
 }