aboutsummaryrefslogtreecommitdiffstats
path: root/fs/btrfs/volumes.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/btrfs/volumes.c')
-rw-r--r--fs/btrfs/volumes.c207
1 files changed, 127 insertions, 80 deletions
diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
index f2a4cc79da61..f8e2943101a1 100644
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
@@ -366,6 +366,14 @@ static noinline int device_list_add(const char *path,
366 } 366 }
367 INIT_LIST_HEAD(&device->dev_alloc_list); 367 INIT_LIST_HEAD(&device->dev_alloc_list);
368 368
369 /* init readahead state */
370 spin_lock_init(&device->reada_lock);
371 device->reada_curr_zone = NULL;
372 atomic_set(&device->reada_in_flight, 0);
373 device->reada_next = 0;
374 INIT_RADIX_TREE(&device->reada_zones, GFP_NOFS & ~__GFP_WAIT);
375 INIT_RADIX_TREE(&device->reada_extents, GFP_NOFS & ~__GFP_WAIT);
376
369 mutex_lock(&fs_devices->device_list_mutex); 377 mutex_lock(&fs_devices->device_list_mutex);
370 list_add_rcu(&device->dev_list, &fs_devices->devices); 378 list_add_rcu(&device->dev_list, &fs_devices->devices);
371 mutex_unlock(&fs_devices->device_list_mutex); 379 mutex_unlock(&fs_devices->device_list_mutex);
@@ -597,10 +605,8 @@ static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
597 set_blocksize(bdev, 4096); 605 set_blocksize(bdev, 4096);
598 606
599 bh = btrfs_read_dev_super(bdev); 607 bh = btrfs_read_dev_super(bdev);
600 if (!bh) { 608 if (!bh)
601 ret = -EINVAL;
602 goto error_close; 609 goto error_close;
603 }
604 610
605 disk_super = (struct btrfs_super_block *)bh->b_data; 611 disk_super = (struct btrfs_super_block *)bh->b_data;
606 devid = btrfs_stack_device_id(&disk_super->dev_item); 612 devid = btrfs_stack_device_id(&disk_super->dev_item);
@@ -655,7 +661,7 @@ error:
655 continue; 661 continue;
656 } 662 }
657 if (fs_devices->open_devices == 0) { 663 if (fs_devices->open_devices == 0) {
658 ret = -EIO; 664 ret = -EINVAL;
659 goto out; 665 goto out;
660 } 666 }
661 fs_devices->seeding = seeding; 667 fs_devices->seeding = seeding;
@@ -1013,8 +1019,13 @@ static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
1013 } 1019 }
1014 BUG_ON(ret); 1020 BUG_ON(ret);
1015 1021
1016 if (device->bytes_used > 0) 1022 if (device->bytes_used > 0) {
1017 device->bytes_used -= btrfs_dev_extent_length(leaf, extent); 1023 u64 len = btrfs_dev_extent_length(leaf, extent);
1024 device->bytes_used -= len;
1025 spin_lock(&root->fs_info->free_chunk_lock);
1026 root->fs_info->free_chunk_space += len;
1027 spin_unlock(&root->fs_info->free_chunk_lock);
1028 }
1018 ret = btrfs_del_item(trans, root, path); 1029 ret = btrfs_del_item(trans, root, path);
1019 1030
1020out: 1031out:
@@ -1356,6 +1367,11 @@ int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1356 if (ret) 1367 if (ret)
1357 goto error_undo; 1368 goto error_undo;
1358 1369
1370 spin_lock(&root->fs_info->free_chunk_lock);
1371 root->fs_info->free_chunk_space = device->total_bytes -
1372 device->bytes_used;
1373 spin_unlock(&root->fs_info->free_chunk_lock);
1374
1359 device->in_fs_metadata = 0; 1375 device->in_fs_metadata = 0;
1360 btrfs_scrub_cancel_dev(root, device); 1376 btrfs_scrub_cancel_dev(root, device);
1361 1377
@@ -1387,8 +1403,8 @@ int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1387 call_rcu(&device->rcu, free_device); 1403 call_rcu(&device->rcu, free_device);
1388 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); 1404 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1389 1405
1390 num_devices = btrfs_super_num_devices(&root->fs_info->super_copy) - 1; 1406 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1391 btrfs_set_super_num_devices(&root->fs_info->super_copy, num_devices); 1407 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
1392 1408
1393 if (cur_devices->open_devices == 0) { 1409 if (cur_devices->open_devices == 0) {
1394 struct btrfs_fs_devices *fs_devices; 1410 struct btrfs_fs_devices *fs_devices;
@@ -1450,7 +1466,7 @@ static int btrfs_prepare_sprout(struct btrfs_trans_handle *trans,
1450 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; 1466 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1451 struct btrfs_fs_devices *old_devices; 1467 struct btrfs_fs_devices *old_devices;
1452 struct btrfs_fs_devices *seed_devices; 1468 struct btrfs_fs_devices *seed_devices;
1453 struct btrfs_super_block *disk_super = &root->fs_info->super_copy; 1469 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
1454 struct btrfs_device *device; 1470 struct btrfs_device *device;
1455 u64 super_flags; 1471 u64 super_flags;
1456 1472
@@ -1691,15 +1707,19 @@ int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1691 root->fs_info->fs_devices->num_can_discard++; 1707 root->fs_info->fs_devices->num_can_discard++;
1692 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes; 1708 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1693 1709
1710 spin_lock(&root->fs_info->free_chunk_lock);
1711 root->fs_info->free_chunk_space += device->total_bytes;
1712 spin_unlock(&root->fs_info->free_chunk_lock);
1713
1694 if (!blk_queue_nonrot(bdev_get_queue(bdev))) 1714 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
1695 root->fs_info->fs_devices->rotating = 1; 1715 root->fs_info->fs_devices->rotating = 1;
1696 1716
1697 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy); 1717 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
1698 btrfs_set_super_total_bytes(&root->fs_info->super_copy, 1718 btrfs_set_super_total_bytes(root->fs_info->super_copy,
1699 total_bytes + device->total_bytes); 1719 total_bytes + device->total_bytes);
1700 1720
1701 total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy); 1721 total_bytes = btrfs_super_num_devices(root->fs_info->super_copy);
1702 btrfs_set_super_num_devices(&root->fs_info->super_copy, 1722 btrfs_set_super_num_devices(root->fs_info->super_copy,
1703 total_bytes + 1); 1723 total_bytes + 1);
1704 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); 1724 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1705 1725
@@ -1790,7 +1810,7 @@ static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
1790 struct btrfs_device *device, u64 new_size) 1810 struct btrfs_device *device, u64 new_size)
1791{ 1811{
1792 struct btrfs_super_block *super_copy = 1812 struct btrfs_super_block *super_copy =
1793 &device->dev_root->fs_info->super_copy; 1813 device->dev_root->fs_info->super_copy;
1794 u64 old_total = btrfs_super_total_bytes(super_copy); 1814 u64 old_total = btrfs_super_total_bytes(super_copy);
1795 u64 diff = new_size - device->total_bytes; 1815 u64 diff = new_size - device->total_bytes;
1796 1816
@@ -1849,7 +1869,7 @@ static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1849static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64 1869static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
1850 chunk_offset) 1870 chunk_offset)
1851{ 1871{
1852 struct btrfs_super_block *super_copy = &root->fs_info->super_copy; 1872 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
1853 struct btrfs_disk_key *disk_key; 1873 struct btrfs_disk_key *disk_key;
1854 struct btrfs_chunk *chunk; 1874 struct btrfs_chunk *chunk;
1855 u8 *ptr; 1875 u8 *ptr;
@@ -2175,7 +2195,7 @@ int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
2175 bool retried = false; 2195 bool retried = false;
2176 struct extent_buffer *l; 2196 struct extent_buffer *l;
2177 struct btrfs_key key; 2197 struct btrfs_key key;
2178 struct btrfs_super_block *super_copy = &root->fs_info->super_copy; 2198 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
2179 u64 old_total = btrfs_super_total_bytes(super_copy); 2199 u64 old_total = btrfs_super_total_bytes(super_copy);
2180 u64 old_size = device->total_bytes; 2200 u64 old_size = device->total_bytes;
2181 u64 diff = device->total_bytes - new_size; 2201 u64 diff = device->total_bytes - new_size;
@@ -2192,8 +2212,12 @@ int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
2192 lock_chunks(root); 2212 lock_chunks(root);
2193 2213
2194 device->total_bytes = new_size; 2214 device->total_bytes = new_size;
2195 if (device->writeable) 2215 if (device->writeable) {
2196 device->fs_devices->total_rw_bytes -= diff; 2216 device->fs_devices->total_rw_bytes -= diff;
2217 spin_lock(&root->fs_info->free_chunk_lock);
2218 root->fs_info->free_chunk_space -= diff;
2219 spin_unlock(&root->fs_info->free_chunk_lock);
2220 }
2197 unlock_chunks(root); 2221 unlock_chunks(root);
2198 2222
2199again: 2223again:
@@ -2257,6 +2281,9 @@ again:
2257 device->total_bytes = old_size; 2281 device->total_bytes = old_size;
2258 if (device->writeable) 2282 if (device->writeable)
2259 device->fs_devices->total_rw_bytes += diff; 2283 device->fs_devices->total_rw_bytes += diff;
2284 spin_lock(&root->fs_info->free_chunk_lock);
2285 root->fs_info->free_chunk_space += diff;
2286 spin_unlock(&root->fs_info->free_chunk_lock);
2260 unlock_chunks(root); 2287 unlock_chunks(root);
2261 goto done; 2288 goto done;
2262 } 2289 }
@@ -2292,7 +2319,7 @@ static int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
2292 struct btrfs_key *key, 2319 struct btrfs_key *key,
2293 struct btrfs_chunk *chunk, int item_size) 2320 struct btrfs_chunk *chunk, int item_size)
2294{ 2321{
2295 struct btrfs_super_block *super_copy = &root->fs_info->super_copy; 2322 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
2296 struct btrfs_disk_key disk_key; 2323 struct btrfs_disk_key disk_key;
2297 u32 array_size; 2324 u32 array_size;
2298 u8 *ptr; 2325 u8 *ptr;
@@ -2615,6 +2642,11 @@ static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
2615 index++; 2642 index++;
2616 } 2643 }
2617 2644
2645 spin_lock(&extent_root->fs_info->free_chunk_lock);
2646 extent_root->fs_info->free_chunk_space -= (stripe_size *
2647 map->num_stripes);
2648 spin_unlock(&extent_root->fs_info->free_chunk_lock);
2649
2618 index = 0; 2650 index = 0;
2619 stripe = &chunk->stripe; 2651 stripe = &chunk->stripe;
2620 while (index < map->num_stripes) { 2652 while (index < map->num_stripes) {
@@ -2848,7 +2880,7 @@ static int find_live_mirror(struct map_lookup *map, int first, int num,
2848 2880
2849static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw, 2881static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2850 u64 logical, u64 *length, 2882 u64 logical, u64 *length,
2851 struct btrfs_multi_bio **multi_ret, 2883 struct btrfs_bio **bbio_ret,
2852 int mirror_num) 2884 int mirror_num)
2853{ 2885{
2854 struct extent_map *em; 2886 struct extent_map *em;
@@ -2866,18 +2898,18 @@ static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2866 int i; 2898 int i;
2867 int num_stripes; 2899 int num_stripes;
2868 int max_errors = 0; 2900 int max_errors = 0;
2869 struct btrfs_multi_bio *multi = NULL; 2901 struct btrfs_bio *bbio = NULL;
2870 2902
2871 if (multi_ret && !(rw & (REQ_WRITE | REQ_DISCARD))) 2903 if (bbio_ret && !(rw & (REQ_WRITE | REQ_DISCARD)))
2872 stripes_allocated = 1; 2904 stripes_allocated = 1;
2873again: 2905again:
2874 if (multi_ret) { 2906 if (bbio_ret) {
2875 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated), 2907 bbio = kzalloc(btrfs_bio_size(stripes_allocated),
2876 GFP_NOFS); 2908 GFP_NOFS);
2877 if (!multi) 2909 if (!bbio)
2878 return -ENOMEM; 2910 return -ENOMEM;
2879 2911
2880 atomic_set(&multi->error, 0); 2912 atomic_set(&bbio->error, 0);
2881 } 2913 }
2882 2914
2883 read_lock(&em_tree->lock); 2915 read_lock(&em_tree->lock);
@@ -2898,7 +2930,7 @@ again:
2898 if (mirror_num > map->num_stripes) 2930 if (mirror_num > map->num_stripes)
2899 mirror_num = 0; 2931 mirror_num = 0;
2900 2932
2901 /* if our multi bio struct is too small, back off and try again */ 2933 /* if our btrfs_bio struct is too small, back off and try again */
2902 if (rw & REQ_WRITE) { 2934 if (rw & REQ_WRITE) {
2903 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 | 2935 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
2904 BTRFS_BLOCK_GROUP_DUP)) { 2936 BTRFS_BLOCK_GROUP_DUP)) {
@@ -2917,11 +2949,11 @@ again:
2917 stripes_required = map->num_stripes; 2949 stripes_required = map->num_stripes;
2918 } 2950 }
2919 } 2951 }
2920 if (multi_ret && (rw & (REQ_WRITE | REQ_DISCARD)) && 2952 if (bbio_ret && (rw & (REQ_WRITE | REQ_DISCARD)) &&
2921 stripes_allocated < stripes_required) { 2953 stripes_allocated < stripes_required) {
2922 stripes_allocated = map->num_stripes; 2954 stripes_allocated = map->num_stripes;
2923 free_extent_map(em); 2955 free_extent_map(em);
2924 kfree(multi); 2956 kfree(bbio);
2925 goto again; 2957 goto again;
2926 } 2958 }
2927 stripe_nr = offset; 2959 stripe_nr = offset;
@@ -2950,7 +2982,7 @@ again:
2950 *length = em->len - offset; 2982 *length = em->len - offset;
2951 } 2983 }
2952 2984
2953 if (!multi_ret) 2985 if (!bbio_ret)
2954 goto out; 2986 goto out;
2955 2987
2956 num_stripes = 1; 2988 num_stripes = 1;
@@ -2975,13 +3007,17 @@ again:
2975 stripe_index = find_live_mirror(map, 0, 3007 stripe_index = find_live_mirror(map, 0,
2976 map->num_stripes, 3008 map->num_stripes,
2977 current->pid % map->num_stripes); 3009 current->pid % map->num_stripes);
3010 mirror_num = stripe_index + 1;
2978 } 3011 }
2979 3012
2980 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) { 3013 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
2981 if (rw & (REQ_WRITE | REQ_DISCARD)) 3014 if (rw & (REQ_WRITE | REQ_DISCARD)) {
2982 num_stripes = map->num_stripes; 3015 num_stripes = map->num_stripes;
2983 else if (mirror_num) 3016 } else if (mirror_num) {
2984 stripe_index = mirror_num - 1; 3017 stripe_index = mirror_num - 1;
3018 } else {
3019 mirror_num = 1;
3020 }
2985 3021
2986 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) { 3022 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2987 int factor = map->num_stripes / map->sub_stripes; 3023 int factor = map->num_stripes / map->sub_stripes;
@@ -3001,6 +3037,7 @@ again:
3001 stripe_index = find_live_mirror(map, stripe_index, 3037 stripe_index = find_live_mirror(map, stripe_index,
3002 map->sub_stripes, stripe_index + 3038 map->sub_stripes, stripe_index +
3003 current->pid % map->sub_stripes); 3039 current->pid % map->sub_stripes);
3040 mirror_num = stripe_index + 1;
3004 } 3041 }
3005 } else { 3042 } else {
3006 /* 3043 /*
@@ -3009,15 +3046,16 @@ again:
3009 * stripe_index is the number of our device in the stripe array 3046 * stripe_index is the number of our device in the stripe array
3010 */ 3047 */
3011 stripe_index = do_div(stripe_nr, map->num_stripes); 3048 stripe_index = do_div(stripe_nr, map->num_stripes);
3049 mirror_num = stripe_index + 1;
3012 } 3050 }
3013 BUG_ON(stripe_index >= map->num_stripes); 3051 BUG_ON(stripe_index >= map->num_stripes);
3014 3052
3015 if (rw & REQ_DISCARD) { 3053 if (rw & REQ_DISCARD) {
3016 for (i = 0; i < num_stripes; i++) { 3054 for (i = 0; i < num_stripes; i++) {
3017 multi->stripes[i].physical = 3055 bbio->stripes[i].physical =
3018 map->stripes[stripe_index].physical + 3056 map->stripes[stripe_index].physical +
3019 stripe_offset + stripe_nr * map->stripe_len; 3057 stripe_offset + stripe_nr * map->stripe_len;
3020 multi->stripes[i].dev = map->stripes[stripe_index].dev; 3058 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
3021 3059
3022 if (map->type & BTRFS_BLOCK_GROUP_RAID0) { 3060 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3023 u64 stripes; 3061 u64 stripes;
@@ -3038,16 +3076,16 @@ again:
3038 } 3076 }
3039 stripes = stripe_nr_end - 1 - j; 3077 stripes = stripe_nr_end - 1 - j;
3040 do_div(stripes, map->num_stripes); 3078 do_div(stripes, map->num_stripes);
3041 multi->stripes[i].length = map->stripe_len * 3079 bbio->stripes[i].length = map->stripe_len *
3042 (stripes - stripe_nr + 1); 3080 (stripes - stripe_nr + 1);
3043 3081
3044 if (i == 0) { 3082 if (i == 0) {
3045 multi->stripes[i].length -= 3083 bbio->stripes[i].length -=
3046 stripe_offset; 3084 stripe_offset;
3047 stripe_offset = 0; 3085 stripe_offset = 0;
3048 } 3086 }
3049 if (stripe_index == last_stripe) 3087 if (stripe_index == last_stripe)
3050 multi->stripes[i].length -= 3088 bbio->stripes[i].length -=
3051 stripe_end_offset; 3089 stripe_end_offset;
3052 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) { 3090 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3053 u64 stripes; 3091 u64 stripes;
@@ -3072,11 +3110,11 @@ again:
3072 } 3110 }
3073 stripes = stripe_nr_end - 1 - j; 3111 stripes = stripe_nr_end - 1 - j;
3074 do_div(stripes, factor); 3112 do_div(stripes, factor);
3075 multi->stripes[i].length = map->stripe_len * 3113 bbio->stripes[i].length = map->stripe_len *
3076 (stripes - stripe_nr + 1); 3114 (stripes - stripe_nr + 1);
3077 3115
3078 if (i < map->sub_stripes) { 3116 if (i < map->sub_stripes) {
3079 multi->stripes[i].length -= 3117 bbio->stripes[i].length -=
3080 stripe_offset; 3118 stripe_offset;
3081 if (i == map->sub_stripes - 1) 3119 if (i == map->sub_stripes - 1)
3082 stripe_offset = 0; 3120 stripe_offset = 0;
@@ -3084,11 +3122,11 @@ again:
3084 if (stripe_index >= last_stripe && 3122 if (stripe_index >= last_stripe &&
3085 stripe_index <= (last_stripe + 3123 stripe_index <= (last_stripe +
3086 map->sub_stripes - 1)) { 3124 map->sub_stripes - 1)) {
3087 multi->stripes[i].length -= 3125 bbio->stripes[i].length -=
3088 stripe_end_offset; 3126 stripe_end_offset;
3089 } 3127 }
3090 } else 3128 } else
3091 multi->stripes[i].length = *length; 3129 bbio->stripes[i].length = *length;
3092 3130
3093 stripe_index++; 3131 stripe_index++;
3094 if (stripe_index == map->num_stripes) { 3132 if (stripe_index == map->num_stripes) {
@@ -3099,19 +3137,20 @@ again:
3099 } 3137 }
3100 } else { 3138 } else {
3101 for (i = 0; i < num_stripes; i++) { 3139 for (i = 0; i < num_stripes; i++) {
3102 multi->stripes[i].physical = 3140 bbio->stripes[i].physical =
3103 map->stripes[stripe_index].physical + 3141 map->stripes[stripe_index].physical +
3104 stripe_offset + 3142 stripe_offset +
3105 stripe_nr * map->stripe_len; 3143 stripe_nr * map->stripe_len;
3106 multi->stripes[i].dev = 3144 bbio->stripes[i].dev =
3107 map->stripes[stripe_index].dev; 3145 map->stripes[stripe_index].dev;
3108 stripe_index++; 3146 stripe_index++;
3109 } 3147 }
3110 } 3148 }
3111 if (multi_ret) { 3149 if (bbio_ret) {
3112 *multi_ret = multi; 3150 *bbio_ret = bbio;
3113 multi->num_stripes = num_stripes; 3151 bbio->num_stripes = num_stripes;
3114 multi->max_errors = max_errors; 3152 bbio->max_errors = max_errors;
3153 bbio->mirror_num = mirror_num;
3115 } 3154 }
3116out: 3155out:
3117 free_extent_map(em); 3156 free_extent_map(em);
@@ -3120,9 +3159,9 @@ out:
3120 3159
3121int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw, 3160int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3122 u64 logical, u64 *length, 3161 u64 logical, u64 *length,
3123 struct btrfs_multi_bio **multi_ret, int mirror_num) 3162 struct btrfs_bio **bbio_ret, int mirror_num)
3124{ 3163{
3125 return __btrfs_map_block(map_tree, rw, logical, length, multi_ret, 3164 return __btrfs_map_block(map_tree, rw, logical, length, bbio_ret,
3126 mirror_num); 3165 mirror_num);
3127} 3166}
3128 3167
@@ -3191,28 +3230,30 @@ int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
3191 return 0; 3230 return 0;
3192} 3231}
3193 3232
3194static void end_bio_multi_stripe(struct bio *bio, int err) 3233static void btrfs_end_bio(struct bio *bio, int err)
3195{ 3234{
3196 struct btrfs_multi_bio *multi = bio->bi_private; 3235 struct btrfs_bio *bbio = bio->bi_private;
3197 int is_orig_bio = 0; 3236 int is_orig_bio = 0;
3198 3237
3199 if (err) 3238 if (err)
3200 atomic_inc(&multi->error); 3239 atomic_inc(&bbio->error);
3201 3240
3202 if (bio == multi->orig_bio) 3241 if (bio == bbio->orig_bio)
3203 is_orig_bio = 1; 3242 is_orig_bio = 1;
3204 3243
3205 if (atomic_dec_and_test(&multi->stripes_pending)) { 3244 if (atomic_dec_and_test(&bbio->stripes_pending)) {
3206 if (!is_orig_bio) { 3245 if (!is_orig_bio) {
3207 bio_put(bio); 3246 bio_put(bio);
3208 bio = multi->orig_bio; 3247 bio = bbio->orig_bio;
3209 } 3248 }
3210 bio->bi_private = multi->private; 3249 bio->bi_private = bbio->private;
3211 bio->bi_end_io = multi->end_io; 3250 bio->bi_end_io = bbio->end_io;
3251 bio->bi_bdev = (struct block_device *)
3252 (unsigned long)bbio->mirror_num;
3212 /* only send an error to the higher layers if it is 3253 /* only send an error to the higher layers if it is
3213 * beyond the tolerance of the multi-bio 3254 * beyond the tolerance of the multi-bio
3214 */ 3255 */
3215 if (atomic_read(&multi->error) > multi->max_errors) { 3256 if (atomic_read(&bbio->error) > bbio->max_errors) {
3216 err = -EIO; 3257 err = -EIO;
3217 } else if (err) { 3258 } else if (err) {
3218 /* 3259 /*
@@ -3222,7 +3263,7 @@ static void end_bio_multi_stripe(struct bio *bio, int err)
3222 set_bit(BIO_UPTODATE, &bio->bi_flags); 3263 set_bit(BIO_UPTODATE, &bio->bi_flags);
3223 err = 0; 3264 err = 0;
3224 } 3265 }
3225 kfree(multi); 3266 kfree(bbio);
3226 3267
3227 bio_endio(bio, err); 3268 bio_endio(bio, err);
3228 } else if (!is_orig_bio) { 3269 } else if (!is_orig_bio) {
@@ -3302,20 +3343,20 @@ int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
3302 u64 logical = (u64)bio->bi_sector << 9; 3343 u64 logical = (u64)bio->bi_sector << 9;
3303 u64 length = 0; 3344 u64 length = 0;
3304 u64 map_length; 3345 u64 map_length;
3305 struct btrfs_multi_bio *multi = NULL;
3306 int ret; 3346 int ret;
3307 int dev_nr = 0; 3347 int dev_nr = 0;
3308 int total_devs = 1; 3348 int total_devs = 1;
3349 struct btrfs_bio *bbio = NULL;
3309 3350
3310 length = bio->bi_size; 3351 length = bio->bi_size;
3311 map_tree = &root->fs_info->mapping_tree; 3352 map_tree = &root->fs_info->mapping_tree;
3312 map_length = length; 3353 map_length = length;
3313 3354
3314 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi, 3355 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &bbio,
3315 mirror_num); 3356 mirror_num);
3316 BUG_ON(ret); 3357 BUG_ON(ret);
3317 3358
3318 total_devs = multi->num_stripes; 3359 total_devs = bbio->num_stripes;
3319 if (map_length < length) { 3360 if (map_length < length) {
3320 printk(KERN_CRIT "mapping failed logical %llu bio len %llu " 3361 printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
3321 "len %llu\n", (unsigned long long)logical, 3362 "len %llu\n", (unsigned long long)logical,
@@ -3323,25 +3364,28 @@ int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
3323 (unsigned long long)map_length); 3364 (unsigned long long)map_length);
3324 BUG(); 3365 BUG();
3325 } 3366 }
3326 multi->end_io = first_bio->bi_end_io; 3367
3327 multi->private = first_bio->bi_private; 3368 bbio->orig_bio = first_bio;
3328 multi->orig_bio = first_bio; 3369 bbio->private = first_bio->bi_private;
3329 atomic_set(&multi->stripes_pending, multi->num_stripes); 3370 bbio->end_io = first_bio->bi_end_io;
3371 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
3330 3372
3331 while (dev_nr < total_devs) { 3373 while (dev_nr < total_devs) {
3332 if (total_devs > 1) { 3374 if (dev_nr < total_devs - 1) {
3333 if (dev_nr < total_devs - 1) { 3375 bio = bio_clone(first_bio, GFP_NOFS);
3334 bio = bio_clone(first_bio, GFP_NOFS); 3376 BUG_ON(!bio);
3335 BUG_ON(!bio); 3377 } else {
3336 } else { 3378 bio = first_bio;
3337 bio = first_bio;
3338 }
3339 bio->bi_private = multi;
3340 bio->bi_end_io = end_bio_multi_stripe;
3341 } 3379 }
3342 bio->bi_sector = multi->stripes[dev_nr].physical >> 9; 3380 bio->bi_private = bbio;
3343 dev = multi->stripes[dev_nr].dev; 3381 bio->bi_end_io = btrfs_end_bio;
3382 bio->bi_sector = bbio->stripes[dev_nr].physical >> 9;
3383 dev = bbio->stripes[dev_nr].dev;
3344 if (dev && dev->bdev && (rw != WRITE || dev->writeable)) { 3384 if (dev && dev->bdev && (rw != WRITE || dev->writeable)) {
3385 pr_debug("btrfs_map_bio: rw %d, secor=%llu, dev=%lu "
3386 "(%s id %llu), size=%u\n", rw,
3387 (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
3388 dev->name, dev->devid, bio->bi_size);
3345 bio->bi_bdev = dev->bdev; 3389 bio->bi_bdev = dev->bdev;
3346 if (async_submit) 3390 if (async_submit)
3347 schedule_bio(root, dev, rw, bio); 3391 schedule_bio(root, dev, rw, bio);
@@ -3354,8 +3398,6 @@ int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
3354 } 3398 }
3355 dev_nr++; 3399 dev_nr++;
3356 } 3400 }
3357 if (total_devs == 1)
3358 kfree(multi);
3359 return 0; 3401 return 0;
3360} 3402}
3361 3403
@@ -3616,15 +3658,20 @@ static int read_one_dev(struct btrfs_root *root,
3616 fill_device_from_item(leaf, dev_item, device); 3658 fill_device_from_item(leaf, dev_item, device);
3617 device->dev_root = root->fs_info->dev_root; 3659 device->dev_root = root->fs_info->dev_root;
3618 device->in_fs_metadata = 1; 3660 device->in_fs_metadata = 1;
3619 if (device->writeable) 3661 if (device->writeable) {
3620 device->fs_devices->total_rw_bytes += device->total_bytes; 3662 device->fs_devices->total_rw_bytes += device->total_bytes;
3663 spin_lock(&root->fs_info->free_chunk_lock);
3664 root->fs_info->free_chunk_space += device->total_bytes -
3665 device->bytes_used;
3666 spin_unlock(&root->fs_info->free_chunk_lock);
3667 }
3621 ret = 0; 3668 ret = 0;
3622 return ret; 3669 return ret;
3623} 3670}
3624 3671
3625int btrfs_read_sys_array(struct btrfs_root *root) 3672int btrfs_read_sys_array(struct btrfs_root *root)
3626{ 3673{
3627 struct btrfs_super_block *super_copy = &root->fs_info->super_copy; 3674 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
3628 struct extent_buffer *sb; 3675 struct extent_buffer *sb;
3629 struct btrfs_disk_key *disk_key; 3676 struct btrfs_disk_key *disk_key;
3630 struct btrfs_chunk *chunk; 3677 struct btrfs_chunk *chunk;