Btrfs: Add a mount option to control worker thread pool size

mount -o thread_pool_size changes the default, which is
min(num_cpus + 2, 8).  Larger thread pools would make more sense on
very large disk arrays.

This mount option controls the max size of each thread pool.  There
are multiple thread pools, so the total worker count will be larger
than the mount option.

Signed-off-by: Chris Mason <chris.mason@oracle.com>
diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
index 98ff4fb..c6a710a 100644
--- a/fs/btrfs/disk-io.c
+++ b/fs/btrfs/disk-io.c
@@ -1117,6 +1117,7 @@
 					      GFP_NOFS);
 	int ret;
 	int err = -EINVAL;
+
 	struct btrfs_super_block *disk_super;
 
 	if (!extent_root || !tree_root || !fs_info) {
@@ -1148,6 +1149,7 @@
 	fs_info->btree_inode = new_inode(sb);
 	fs_info->btree_inode->i_ino = 1;
 	fs_info->btree_inode->i_nlink = 1;
+	fs_info->thread_pool_size = min(num_online_cpus() + 2, 8);
 
 	sb->s_blocksize = 4096;
 	sb->s_blocksize_bits = blksize_bits(4096);
@@ -1195,19 +1197,6 @@
 	mutex_init(&fs_info->trans_mutex);
 	mutex_init(&fs_info->fs_mutex);
 
-	/* we need to start all the end_io workers up front because the
-	 * queue work function gets called at interrupt time.  The endio
-	 * workers don't normally start IO, so some number of them <= the
-	 * number of cpus is fine.  They handle checksumming after a read.
-	 *
-	 * The other worker threads do start IO, so the max is larger than
-	 * the number of CPUs.  FIXME, tune this for huge machines
-	 */
-	btrfs_init_workers(&fs_info->workers, num_online_cpus() * 2);
-	btrfs_init_workers(&fs_info->endio_workers, num_online_cpus());
-	btrfs_start_workers(&fs_info->workers, 1);
-	btrfs_start_workers(&fs_info->endio_workers, num_online_cpus());
-
 #if 0
 	ret = add_hasher(fs_info, "crc32c");
 	if (ret) {
@@ -1238,6 +1227,17 @@
 	if (err)
 		goto fail_sb_buffer;
 
+	/*
+	 * we need to start all the end_io workers up front because the
+	 * queue work function gets called at interrupt time, and so it
+	 * cannot dynamically grow.
+	 */
+	btrfs_init_workers(&fs_info->workers, fs_info->thread_pool_size);
+	btrfs_init_workers(&fs_info->endio_workers, fs_info->thread_pool_size);
+	btrfs_start_workers(&fs_info->workers, 1);
+	btrfs_start_workers(&fs_info->endio_workers, fs_info->thread_pool_size);
+
+
 	err = -EINVAL;
 	if (btrfs_super_num_devices(disk_super) > fs_devices->open_devices) {
 		printk("Btrfs: wanted %llu devices, but found %llu\n",
@@ -1341,10 +1341,10 @@
 	mutex_unlock(&fs_info->fs_mutex);
 fail_sb_buffer:
 	extent_io_tree_empty_lru(&BTRFS_I(fs_info->btree_inode)->io_tree);
-fail_iput:
-	iput(fs_info->btree_inode);
 	btrfs_stop_workers(&fs_info->workers);
 	btrfs_stop_workers(&fs_info->endio_workers);
+fail_iput:
+	iput(fs_info->btree_inode);
 fail:
 	btrfs_close_devices(fs_info->fs_devices);
 	btrfs_mapping_tree_free(&fs_info->mapping_tree);