Btrfs: Add mount -o ssd, which includes optimizations for seek free storage

Signed-off-by: Chris Mason <chris.mason@oracle.com>
diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c
index 2c569b4..b69a466 100644
--- a/fs/btrfs/extent-tree.c
+++ b/fs/btrfs/extent-tree.c
@@ -1431,6 +1431,19 @@
 		data = BTRFS_BLOCK_GROUP_MIXED;
 	}
 
+	/* for SSD, cluster allocations together as much as possible */
+	if (btrfs_test_opt(root, SSD)) {
+		if (!data) {
+			if (root->fs_info->last_alloc)
+				hint_byte = root->fs_info->last_alloc;
+			else {
+				hint_byte = hint_byte &
+					~((u64)BTRFS_BLOCK_GROUP_SIZE - 1);
+				empty_size += 16 * 1024 * 1024;
+			}
+		}
+	}
+
 	search_end = min(search_end,
 			 btrfs_super_total_bytes(&info->super_copy));
 	if (hint_byte) {
@@ -1456,6 +1469,19 @@
 	}
 	search_start = find_search_start(root, &block_group, search_start,
 					 total_needed, data);
+
+	if (!data && btrfs_test_opt(root, SSD) && info->last_alloc &&
+	    search_start != info->last_alloc) {
+		info->last_alloc = 0;
+		if (!empty_size) {
+			empty_size += 16 * 1024 * 1024;
+			total_needed += empty_size;
+		}
+		search_start = find_search_start(root, &block_group,
+						 search_start, total_needed,
+						 data);
+	}
+
 	search_start = stripe_align(root, search_start);
 	cached_start = search_start;
 	btrfs_init_path(path);
@@ -1610,6 +1636,8 @@
 error:
 	btrfs_release_path(root, path);
 	btrfs_free_path(path);
+	if (btrfs_test_opt(root, SSD) && !ret && !data)
+		info->last_alloc = ins->objectid + ins->offset;
 	return ret;
 }
 /*
@@ -1778,7 +1806,8 @@
 			buf->start, buf->start + buf->len - 1,
 			EXTENT_CSUM, GFP_NOFS);
 	buf->flags |= EXTENT_CSUM;
-	btrfs_set_buffer_defrag(buf);
+	if (!btrfs_test_opt(root, SSD))
+		btrfs_set_buffer_defrag(buf);
 	trans->blocks_used++;
 	return buf;
 }