Btrfs: Add readahead to the online shrinker, and a mount -o alloc_start= for testing

Signed-off-by: Chris Mason <chris.mason@oracle.com>
diff --git a/fs/btrfs/ctree.c b/fs/btrfs/ctree.c
index 2b3ffa7..8fa92a2 100644
--- a/fs/btrfs/ctree.c
+++ b/fs/btrfs/ctree.c
@@ -2654,9 +2654,9 @@
  */
 int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
 {
+	u64 bytenr;
 	int slot;
 	int level = 1;
-	u64 bytenr;
 	struct extent_buffer *c;
 	struct extent_buffer *next = NULL;
 
@@ -2687,11 +2687,14 @@
 		level--;
 		c = path->nodes[level];
 		free_extent_buffer(c);
+		slot = btrfs_header_nritems(next);
+		if (slot != 0)
+			slot--;
 		path->nodes[level] = next;
-		path->slots[level] = 0;
+		path->slots[level] = slot;
 		if (!level)
 			break;
-		next = read_tree_block(root, btrfs_node_blockptr(next, 0),
+		next = read_tree_block(root, btrfs_node_blockptr(next, slot),
 				       btrfs_level_size(root, level - 1));
 	}
 	return 0;
diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h
index b83a1be..d6e5c19 100644
--- a/fs/btrfs/ctree.h
+++ b/fs/btrfs/ctree.h
@@ -324,6 +324,7 @@
 	u64 last_trans_committed;
 	unsigned long mount_opt;
 	u64 max_extent;
+	u64 alloc_start;
 	struct btrfs_transaction *running_transaction;
 	struct btrfs_super_block super_copy;
 	struct extent_buffer *sb_buffer;
diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c
index a421069..d6243c2 100644
--- a/fs/btrfs/extent-tree.c
+++ b/fs/btrfs/extent-tree.c
@@ -1630,11 +1630,11 @@
 	struct btrfs_path *path;
 
 	btrfs_set_stack_extent_refs(&extent_item, 1);
-#if 0
-	new_hint = max(hint_byte, 16ULL * 1024 * 1024 * 1024);
+
+	new_hint = max(hint_byte, root->fs_info->alloc_start);
 	if (new_hint < btrfs_super_total_bytes(&info->super_copy))
 		hint_byte = new_hint;
-#endif
+
 	WARN_ON(num_bytes < root->sectorsize);
 	ret = find_free_extent(trans, root, num_bytes, empty_size,
 			       search_start, search_end, hint_byte, ins,
@@ -2239,7 +2239,7 @@
 		free_extent_buffer(eb);
 
 		path->lowest_level = level;
-		path->reada = 0;
+		path->reada = 2;
 		ret = btrfs_search_slot(trans, found_root, &found_key, path,
 					0, 1);
 		path->lowest_level = 0;
@@ -2372,6 +2372,7 @@
 	block_group_cache = &info->block_group_cache;
 	path = btrfs_alloc_path();
 	root = root->fs_info->extent_root;
+	path->reada = 2;
 
 again:
 	total_found = 0;
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 3c7bf3b..d6de469 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -2058,7 +2058,7 @@
 
 	mutex_lock(&root->fs_info->fs_mutex);
 	ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
-	mutex_lock(&root->fs_info->fs_mutex);
+	mutex_unlock(&root->fs_info->fs_mutex);
 	if (ret)
 		goto out;
 
diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c
index 3bbd7f6..f1b56eb 100644
--- a/fs/btrfs/super.c
+++ b/fs/btrfs/super.c
@@ -63,7 +63,8 @@
 }
 
 enum {
-	Opt_subvol, Opt_nodatasum, Opt_nodatacow, Opt_max_extent, Opt_err,
+	Opt_subvol, Opt_nodatasum, Opt_nodatacow, Opt_max_extent,
+	Opt_alloc_start, Opt_err,
 };
 
 static match_table_t tokens = {
@@ -71,6 +72,7 @@
 	{Opt_nodatasum, "nodatasum"},
 	{Opt_nodatacow, "nodatacow"},
 	{Opt_max_extent, "max_extent=%s"},
+	{Opt_alloc_start, "alloc_start=%s"},
 	{Opt_err, NULL}
 };
 
@@ -162,6 +164,18 @@
 				}
 			}
 			break;
+		case Opt_alloc_start:
+			if (info) {
+				char *num = match_strdup(&args[0]);
+				if (num) {
+					info->alloc_start =
+						btrfs_parse_size(num);
+					kfree(num);
+					printk("btrfs: allocations start at "
+					       "%Lu\n", info->alloc_start);
+				}
+			}
+			break;
 		default:
 			break;
 		}