Skip to content

Commit

Permalink
Btrfs: Fix chunk allocation when some devices don't have enough room …
Browse files Browse the repository at this point in the history
…for stripes

Signed-off-by: Chris Mason <chris.mason@oracle.com>
  • Loading branch information
Chris Mason committed Sep 25, 2008
1 parent 9b3f68b commit a40a90a
Showing 1 changed file with 29 additions and 16 deletions.
45 changes: 29 additions & 16 deletions fs/btrfs/volumes.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -683,51 +684,56 @@ 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);
if (num_stripes < 4)
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);
do_div(calc_size, stripe_len);
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;

Expand All @@ -746,27 +752,34 @@ int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,

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;
goto 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,
Expand Down

0 comments on commit a40a90a

Please sign in to comment.