Btrfs: Fix chunk allocation when some devices don't have enough room for stripes
authorChris Mason <chris.mason@oracle.com>
Fri, 18 Apr 2008 15:55:51 +0000 (11:55 -0400)
committerChris Mason <chris.mason@oracle.com>
Thu, 25 Sep 2008 15:04:02 +0000 (11:04 -0400)
Signed-off-by: Chris Mason <chris.mason@oracle.com>
fs/btrfs/volumes.c

index e3ddd7f..fe5b009 100644 (file)
@@ -664,7 +664,7 @@ int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
        struct extent_map_tree *em_tree;
        struct map_lookup *map;
        struct extent_map *em;
-       int min_chunk_size = 8 * 1024 * 1024;
+       int min_stripe_size = 1 * 1024 * 1024;
        u64 physical;
        u64 calc_size = 1024 * 1024 * 1024;
        u64 max_chunk_size = calc_size;
@@ -673,6 +673,7 @@ int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
        u64 max_avail = 0;
        u64 percent_max;
        int num_stripes = 1;
+       int min_stripes = 1;
        int sub_stripes = 0;
        int looped = 0;
        int ret;
@@ -683,15 +684,20 @@ int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
        if (list_empty(dev_list))
                return -ENOSPC;
 
-       if (type & (BTRFS_BLOCK_GROUP_RAID0))
+       if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
                num_stripes = btrfs_super_num_devices(&info->super_copy);
-       if (type & (BTRFS_BLOCK_GROUP_DUP))
+               min_stripes = 2;
+       }
+       if (type & (BTRFS_BLOCK_GROUP_DUP)) {
                num_stripes = 2;
+               min_stripes = 2;
+       }
        if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
                num_stripes = min_t(u64, 2,
                                  btrfs_super_num_devices(&info->super_copy));
                if (num_stripes < 2)
                        return -ENOSPC;
+               min_stripes = 2;
        }
        if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
                num_stripes = btrfs_super_num_devices(&info->super_copy);
@@ -699,22 +705,26 @@ int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
                        return -ENOSPC;
                num_stripes &= ~(u32)1;
                sub_stripes = 2;
+               min_stripes = 4;
        }
 
        if (type & BTRFS_BLOCK_GROUP_DATA) {
                max_chunk_size = 10 * calc_size;
-               min_chunk_size = 256 * 1024 * 1024;
+               min_stripe_size = 64 * 1024 * 1024;
        } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
                max_chunk_size = 4 * calc_size;
-               min_chunk_size = 64 * 1024 * 1024;
-       } else {
-               min_chunk_size = 32 * 1024 * 1024;
+               min_stripe_size = 32 * 1024 * 1024;
+       } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
+               calc_size = 8 * 1024 * 1024;
+               max_chunk_size = calc_size * 2;
+               min_stripe_size = 1 * 1024 * 1024;
        }
 
        /* we don't want a chunk larger than 10% of the FS */
        percent_max = div_factor(btrfs_super_total_bytes(&info->super_copy), 1);
        max_chunk_size = min(percent_max, max_chunk_size);
 
+again:
        if (calc_size * num_stripes > max_chunk_size) {
                calc_size = max_chunk_size;
                do_div(calc_size, num_stripes);
@@ -722,12 +732,8 @@ int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
                calc_size *= stripe_len;
        }
        /* we don't want tiny stripes */
-       *num_bytes = chunk_bytes_by_type(type, calc_size,
-                                        num_stripes, sub_stripes);
-       calc_size = max_t(u64, chunk_bytes_by_type(type, min_chunk_size,
-                         num_stripes, sub_stripes), calc_size);
+       calc_size = max_t(u64, min_stripe_size, calc_size);
 
-again:
        do_div(calc_size, stripe_len);
        calc_size *= stripe_len;
 
@@ -746,19 +752,27 @@ again:
 
                avail = device->total_bytes - device->bytes_used;
                cur = cur->next;
-               if (avail > max_avail)
-                       max_avail = avail;
                if (avail >= min_free) {
                        list_move_tail(&device->dev_list, &private_devs);
                        index++;
                        if (type & BTRFS_BLOCK_GROUP_DUP)
                                index++;
-               }
+               } else if (avail > max_avail)
+                       max_avail = avail;
                if (cur == dev_list)
                        break;
        }
        if (index < num_stripes) {
                list_splice(&private_devs, dev_list);
+               if (index >= min_stripes) {
+                       num_stripes = index;
+                       if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
+                               num_stripes /= sub_stripes;
+                               num_stripes *= sub_stripes;
+                       }
+                       looped = 1;
+                       goto again;
+               }
                if (!looped && max_avail > 0) {
                        looped = 1;
                        calc_size = max_avail;
@@ -766,7 +780,6 @@ again:
                }
                return -ENOSPC;
        }
-
        key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
        key.type = BTRFS_CHUNK_ITEM_KEY;
        ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,