aboutsummaryrefslogtreecommitdiffstats
path: root/fs/btrfs/extent-tree.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/btrfs/extent-tree.c')
-rw-r--r--fs/btrfs/extent-tree.c185
1 files changed, 86 insertions, 99 deletions
diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c
index 61b494e8e604..0450dc410533 100644
--- a/fs/btrfs/extent-tree.c
+++ b/fs/btrfs/extent-tree.c
@@ -60,21 +60,6 @@ enum {
60 CHUNK_ALLOC_FORCE = 2, 60 CHUNK_ALLOC_FORCE = 2,
61}; 61};
62 62
63/*
64 * Control how reservations are dealt with.
65 *
66 * RESERVE_FREE - freeing a reservation.
67 * RESERVE_ALLOC - allocating space and we need to update bytes_may_use for
68 * ENOSPC accounting
69 * RESERVE_ALLOC_NO_ACCOUNT - allocating space and we should not update
70 * bytes_may_use as the ENOSPC accounting is done elsewhere
71 */
72enum {
73 RESERVE_FREE = 0,
74 RESERVE_ALLOC = 1,
75 RESERVE_ALLOC_NO_ACCOUNT = 2,
76};
77
78static int update_block_group(struct btrfs_trans_handle *trans, 63static int update_block_group(struct btrfs_trans_handle *trans,
79 struct btrfs_root *root, u64 bytenr, 64 struct btrfs_root *root, u64 bytenr,
80 u64 num_bytes, int alloc); 65 u64 num_bytes, int alloc);
@@ -104,9 +89,10 @@ static int find_next_key(struct btrfs_path *path, int level,
104 struct btrfs_key *key); 89 struct btrfs_key *key);
105static void dump_space_info(struct btrfs_space_info *info, u64 bytes, 90static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
106 int dump_block_groups); 91 int dump_block_groups);
107static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache, 92static int btrfs_add_reserved_bytes(struct btrfs_block_group_cache *cache,
108 u64 num_bytes, int reserve, 93 u64 ram_bytes, u64 num_bytes, int delalloc);
109 int delalloc); 94static int btrfs_free_reserved_bytes(struct btrfs_block_group_cache *cache,
95 u64 num_bytes, int delalloc);
110static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv, 96static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
111 u64 num_bytes); 97 u64 num_bytes);
112int btrfs_pin_extent(struct btrfs_root *root, 98int btrfs_pin_extent(struct btrfs_root *root,
@@ -3501,7 +3487,6 @@ again:
3501 dcs = BTRFS_DC_SETUP; 3487 dcs = BTRFS_DC_SETUP;
3502 else if (ret == -ENOSPC) 3488 else if (ret == -ENOSPC)
3503 set_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags); 3489 set_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags);
3504 btrfs_free_reserved_data_space(inode, 0, num_pages);
3505 3490
3506out_put: 3491out_put:
3507 iput(inode); 3492 iput(inode);
@@ -4472,6 +4457,15 @@ void check_system_chunk(struct btrfs_trans_handle *trans,
4472 } 4457 }
4473} 4458}
4474 4459
4460/*
4461 * If force is CHUNK_ALLOC_FORCE:
4462 * - return 1 if it successfully allocates a chunk,
4463 * - return errors including -ENOSPC otherwise.
4464 * If force is NOT CHUNK_ALLOC_FORCE:
4465 * - return 0 if it doesn't need to allocate a new chunk,
4466 * - return 1 if it successfully allocates a chunk,
4467 * - return errors including -ENOSPC otherwise.
4468 */
4475static int do_chunk_alloc(struct btrfs_trans_handle *trans, 4469static int do_chunk_alloc(struct btrfs_trans_handle *trans,
4476 struct btrfs_root *extent_root, u64 flags, int force) 4470 struct btrfs_root *extent_root, u64 flags, int force)
4477{ 4471{
@@ -4882,7 +4876,7 @@ static int flush_space(struct btrfs_root *root,
4882 btrfs_get_alloc_profile(root, 0), 4876 btrfs_get_alloc_profile(root, 0),
4883 CHUNK_ALLOC_NO_FORCE); 4877 CHUNK_ALLOC_NO_FORCE);
4884 btrfs_end_transaction(trans, root); 4878 btrfs_end_transaction(trans, root);
4885 if (ret == -ENOSPC) 4879 if (ret > 0 || ret == -ENOSPC)
4886 ret = 0; 4880 ret = 0;
4887 break; 4881 break;
4888 case COMMIT_TRANS: 4882 case COMMIT_TRANS:
@@ -6497,19 +6491,15 @@ void btrfs_wait_block_group_reservations(struct btrfs_block_group_cache *bg)
6497} 6491}
6498 6492
6499/** 6493/**
6500 * btrfs_update_reserved_bytes - update the block_group and space info counters 6494 * btrfs_add_reserved_bytes - update the block_group and space info counters
6501 * @cache: The cache we are manipulating 6495 * @cache: The cache we are manipulating
6496 * @ram_bytes: The number of bytes of file content, and will be same to
6497 * @num_bytes except for the compress path.
6502 * @num_bytes: The number of bytes in question 6498 * @num_bytes: The number of bytes in question
6503 * @reserve: One of the reservation enums
6504 * @delalloc: The blocks are allocated for the delalloc write 6499 * @delalloc: The blocks are allocated for the delalloc write
6505 * 6500 *
6506 * This is called by the allocator when it reserves space, or by somebody who is 6501 * This is called by the allocator when it reserves space. Metadata
6507 * freeing space that was never actually used on disk. For example if you 6502 * reservations should be called with RESERVE_ALLOC so we do the proper
6508 * reserve some space for a new leaf in transaction A and before transaction A
6509 * commits you free that leaf, you call this with reserve set to 0 in order to
6510 * clear the reservation.
6511 *
6512 * Metadata reservations should be called with RESERVE_ALLOC so we do the proper
6513 * ENOSPC accounting. For data we handle the reservation through clearing the 6503 * ENOSPC accounting. For data we handle the reservation through clearing the
6514 * delalloc bits in the io_tree. We have to do this since we could end up 6504 * delalloc bits in the io_tree. We have to do this since we could end up
6515 * allocating less disk space for the amount of data we have reserved in the 6505 * allocating less disk space for the amount of data we have reserved in the
@@ -6519,44 +6509,63 @@ void btrfs_wait_block_group_reservations(struct btrfs_block_group_cache *bg)
6519 * make the reservation and return -EAGAIN, otherwise this function always 6509 * make the reservation and return -EAGAIN, otherwise this function always
6520 * succeeds. 6510 * succeeds.
6521 */ 6511 */
6522static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache, 6512static int btrfs_add_reserved_bytes(struct btrfs_block_group_cache *cache,
6523 u64 num_bytes, int reserve, int delalloc) 6513 u64 ram_bytes, u64 num_bytes, int delalloc)
6524{ 6514{
6525 struct btrfs_space_info *space_info = cache->space_info; 6515 struct btrfs_space_info *space_info = cache->space_info;
6526 int ret = 0; 6516 int ret = 0;
6527 6517
6528 spin_lock(&space_info->lock); 6518 spin_lock(&space_info->lock);
6529 spin_lock(&cache->lock); 6519 spin_lock(&cache->lock);
6530 if (reserve != RESERVE_FREE) { 6520 if (cache->ro) {
6531 if (cache->ro) { 6521 ret = -EAGAIN;
6532 ret = -EAGAIN;
6533 } else {
6534 cache->reserved += num_bytes;
6535 space_info->bytes_reserved += num_bytes;
6536 if (reserve == RESERVE_ALLOC) {
6537 trace_btrfs_space_reservation(cache->fs_info,
6538 "space_info", space_info->flags,
6539 num_bytes, 0);
6540 space_info->bytes_may_use -= num_bytes;
6541 }
6542
6543 if (delalloc)
6544 cache->delalloc_bytes += num_bytes;
6545 }
6546 } else { 6522 } else {
6547 if (cache->ro) 6523 cache->reserved += num_bytes;
6548 space_info->bytes_readonly += num_bytes; 6524 space_info->bytes_reserved += num_bytes;
6549 cache->reserved -= num_bytes;
6550 space_info->bytes_reserved -= num_bytes;
6551 6525
6526 trace_btrfs_space_reservation(cache->fs_info,
6527 "space_info", space_info->flags,
6528 ram_bytes, 0);
6529 space_info->bytes_may_use -= ram_bytes;
6552 if (delalloc) 6530 if (delalloc)
6553 cache->delalloc_bytes -= num_bytes; 6531 cache->delalloc_bytes += num_bytes;
6554 } 6532 }
6555 spin_unlock(&cache->lock); 6533 spin_unlock(&cache->lock);
6556 spin_unlock(&space_info->lock); 6534 spin_unlock(&space_info->lock);
6557 return ret; 6535 return ret;
6558} 6536}
6559 6537
6538/**
6539 * btrfs_free_reserved_bytes - update the block_group and space info counters
6540 * @cache: The cache we are manipulating
6541 * @num_bytes: The number of bytes in question
6542 * @delalloc: The blocks are allocated for the delalloc write
6543 *
6544 * This is called by somebody who is freeing space that was never actually used
6545 * on disk. For example if you reserve some space for a new leaf in transaction
6546 * A and before transaction A commits you free that leaf, you call this with
6547 * reserve set to 0 in order to clear the reservation.
6548 */
6549
6550static int btrfs_free_reserved_bytes(struct btrfs_block_group_cache *cache,
6551 u64 num_bytes, int delalloc)
6552{
6553 struct btrfs_space_info *space_info = cache->space_info;
6554 int ret = 0;
6555
6556 spin_lock(&space_info->lock);
6557 spin_lock(&cache->lock);
6558 if (cache->ro)
6559 space_info->bytes_readonly += num_bytes;
6560 cache->reserved -= num_bytes;
6561 space_info->bytes_reserved -= num_bytes;
6562
6563 if (delalloc)
6564 cache->delalloc_bytes -= num_bytes;
6565 spin_unlock(&cache->lock);
6566 spin_unlock(&space_info->lock);
6567 return ret;
6568}
6560void btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans, 6569void btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
6561 struct btrfs_root *root) 6570 struct btrfs_root *root)
6562{ 6571{
@@ -7191,7 +7200,7 @@ void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
7191 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)); 7200 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
7192 7201
7193 btrfs_add_free_space(cache, buf->start, buf->len); 7202 btrfs_add_free_space(cache, buf->start, buf->len);
7194 btrfs_update_reserved_bytes(cache, buf->len, RESERVE_FREE, 0); 7203 btrfs_free_reserved_bytes(cache, buf->len, 0);
7195 btrfs_put_block_group(cache); 7204 btrfs_put_block_group(cache);
7196 trace_btrfs_reserved_extent_free(root, buf->start, buf->len); 7205 trace_btrfs_reserved_extent_free(root, buf->start, buf->len);
7197 pin = 0; 7206 pin = 0;
@@ -7416,9 +7425,9 @@ btrfs_release_block_group(struct btrfs_block_group_cache *cache,
7416 * the free space extent currently. 7425 * the free space extent currently.
7417 */ 7426 */
7418static noinline int find_free_extent(struct btrfs_root *orig_root, 7427static noinline int find_free_extent(struct btrfs_root *orig_root,
7419 u64 num_bytes, u64 empty_size, 7428 u64 ram_bytes, u64 num_bytes, u64 empty_size,
7420 u64 hint_byte, struct btrfs_key *ins, 7429 u64 hint_byte, struct btrfs_key *ins,
7421 u64 flags, int delalloc) 7430 u64 flags, int delalloc)
7422{ 7431{
7423 int ret = 0; 7432 int ret = 0;
7424 struct btrfs_root *root = orig_root->fs_info->extent_root; 7433 struct btrfs_root *root = orig_root->fs_info->extent_root;
@@ -7430,8 +7439,6 @@ static noinline int find_free_extent(struct btrfs_root *orig_root,
7430 struct btrfs_space_info *space_info; 7439 struct btrfs_space_info *space_info;
7431 int loop = 0; 7440 int loop = 0;
7432 int index = __get_raid_index(flags); 7441 int index = __get_raid_index(flags);
7433 int alloc_type = (flags & BTRFS_BLOCK_GROUP_DATA) ?
7434 RESERVE_ALLOC_NO_ACCOUNT : RESERVE_ALLOC;
7435 bool failed_cluster_refill = false; 7442 bool failed_cluster_refill = false;
7436 bool failed_alloc = false; 7443 bool failed_alloc = false;
7437 bool use_cluster = true; 7444 bool use_cluster = true;
@@ -7763,8 +7770,8 @@ checks:
7763 search_start - offset); 7770 search_start - offset);
7764 BUG_ON(offset > search_start); 7771 BUG_ON(offset > search_start);
7765 7772
7766 ret = btrfs_update_reserved_bytes(block_group, num_bytes, 7773 ret = btrfs_add_reserved_bytes(block_group, ram_bytes,
7767 alloc_type, delalloc); 7774 num_bytes, delalloc);
7768 if (ret == -EAGAIN) { 7775 if (ret == -EAGAIN) {
7769 btrfs_add_free_space(block_group, offset, num_bytes); 7776 btrfs_add_free_space(block_group, offset, num_bytes);
7770 goto loop; 7777 goto loop;
@@ -7936,7 +7943,7 @@ again:
7936 up_read(&info->groups_sem); 7943 up_read(&info->groups_sem);
7937} 7944}
7938 7945
7939int btrfs_reserve_extent(struct btrfs_root *root, 7946int btrfs_reserve_extent(struct btrfs_root *root, u64 ram_bytes,
7940 u64 num_bytes, u64 min_alloc_size, 7947 u64 num_bytes, u64 min_alloc_size,
7941 u64 empty_size, u64 hint_byte, 7948 u64 empty_size, u64 hint_byte,
7942 struct btrfs_key *ins, int is_data, int delalloc) 7949 struct btrfs_key *ins, int is_data, int delalloc)
@@ -7948,8 +7955,8 @@ int btrfs_reserve_extent(struct btrfs_root *root,
7948 flags = btrfs_get_alloc_profile(root, is_data); 7955 flags = btrfs_get_alloc_profile(root, is_data);
7949again: 7956again:
7950 WARN_ON(num_bytes < root->sectorsize); 7957 WARN_ON(num_bytes < root->sectorsize);
7951 ret = find_free_extent(root, num_bytes, empty_size, hint_byte, ins, 7958 ret = find_free_extent(root, ram_bytes, num_bytes, empty_size,
7952 flags, delalloc); 7959 hint_byte, ins, flags, delalloc);
7953 if (!ret && !is_data) { 7960 if (!ret && !is_data) {
7954 btrfs_dec_block_group_reservations(root->fs_info, 7961 btrfs_dec_block_group_reservations(root->fs_info,
7955 ins->objectid); 7962 ins->objectid);
@@ -7958,6 +7965,7 @@ again:
7958 num_bytes = min(num_bytes >> 1, ins->offset); 7965 num_bytes = min(num_bytes >> 1, ins->offset);
7959 num_bytes = round_down(num_bytes, root->sectorsize); 7966 num_bytes = round_down(num_bytes, root->sectorsize);
7960 num_bytes = max(num_bytes, min_alloc_size); 7967 num_bytes = max(num_bytes, min_alloc_size);
7968 ram_bytes = num_bytes;
7961 if (num_bytes == min_alloc_size) 7969 if (num_bytes == min_alloc_size)
7962 final_tried = true; 7970 final_tried = true;
7963 goto again; 7971 goto again;
@@ -7995,7 +8003,7 @@ static int __btrfs_free_reserved_extent(struct btrfs_root *root,
7995 if (btrfs_test_opt(root->fs_info, DISCARD)) 8003 if (btrfs_test_opt(root->fs_info, DISCARD))
7996 ret = btrfs_discard_extent(root, start, len, NULL); 8004 ret = btrfs_discard_extent(root, start, len, NULL);
7997 btrfs_add_free_space(cache, start, len); 8005 btrfs_add_free_space(cache, start, len);
7998 btrfs_update_reserved_bytes(cache, len, RESERVE_FREE, delalloc); 8006 btrfs_free_reserved_bytes(cache, len, delalloc);
7999 trace_btrfs_reserved_extent_free(root, start, len); 8007 trace_btrfs_reserved_extent_free(root, start, len);
8000 } 8008 }
8001 8009
@@ -8223,8 +8231,8 @@ int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
8223 if (!block_group) 8231 if (!block_group)
8224 return -EINVAL; 8232 return -EINVAL;
8225 8233
8226 ret = btrfs_update_reserved_bytes(block_group, ins->offset, 8234 ret = btrfs_add_reserved_bytes(block_group, ins->offset,
8227 RESERVE_ALLOC_NO_ACCOUNT, 0); 8235 ins->offset, 0);
8228 BUG_ON(ret); /* logic error */ 8236 BUG_ON(ret); /* logic error */
8229 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid, 8237 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
8230 0, owner, offset, ins, 1); 8238 0, owner, offset, ins, 1);
@@ -8368,7 +8376,7 @@ struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans,
8368 if (IS_ERR(block_rsv)) 8376 if (IS_ERR(block_rsv))
8369 return ERR_CAST(block_rsv); 8377 return ERR_CAST(block_rsv);
8370 8378
8371 ret = btrfs_reserve_extent(root, blocksize, blocksize, 8379 ret = btrfs_reserve_extent(root, blocksize, blocksize, blocksize,
8372 empty_size, hint, &ins, 0, 0); 8380 empty_size, hint, &ins, 0, 0);
8373 if (ret) 8381 if (ret)
8374 goto out_unuse; 8382 goto out_unuse;
@@ -8521,35 +8529,6 @@ reada:
8521 wc->reada_slot = slot; 8529 wc->reada_slot = slot;
8522} 8530}
8523 8531
8524/*
8525 * These may not be seen by the usual inc/dec ref code so we have to
8526 * add them here.
8527 */
8528static int record_one_subtree_extent(struct btrfs_trans_handle *trans,
8529 struct btrfs_root *root, u64 bytenr,
8530 u64 num_bytes)
8531{
8532 struct btrfs_qgroup_extent_record *qrecord;
8533 struct btrfs_delayed_ref_root *delayed_refs;
8534
8535 qrecord = kmalloc(sizeof(*qrecord), GFP_NOFS);
8536 if (!qrecord)
8537 return -ENOMEM;
8538
8539 qrecord->bytenr = bytenr;
8540 qrecord->num_bytes = num_bytes;
8541 qrecord->old_roots = NULL;
8542
8543 delayed_refs = &trans->transaction->delayed_refs;
8544 spin_lock(&delayed_refs->lock);
8545 if (btrfs_qgroup_insert_dirty_extent(trans->fs_info,
8546 delayed_refs, qrecord))
8547 kfree(qrecord);
8548 spin_unlock(&delayed_refs->lock);
8549
8550 return 0;
8551}
8552
8553static int account_leaf_items(struct btrfs_trans_handle *trans, 8532static int account_leaf_items(struct btrfs_trans_handle *trans,
8554 struct btrfs_root *root, 8533 struct btrfs_root *root,
8555 struct extent_buffer *eb) 8534 struct extent_buffer *eb)
@@ -8583,7 +8562,8 @@ static int account_leaf_items(struct btrfs_trans_handle *trans,
8583 8562
8584 num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi); 8563 num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi);
8585 8564
8586 ret = record_one_subtree_extent(trans, root, bytenr, num_bytes); 8565 ret = btrfs_qgroup_insert_dirty_extent(trans, root->fs_info,
8566 bytenr, num_bytes, GFP_NOFS);
8587 if (ret) 8567 if (ret)
8588 return ret; 8568 return ret;
8589 } 8569 }
@@ -8732,8 +8712,9 @@ walk_down:
8732 btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK); 8712 btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
8733 path->locks[level] = BTRFS_READ_LOCK_BLOCKING; 8713 path->locks[level] = BTRFS_READ_LOCK_BLOCKING;
8734 8714
8735 ret = record_one_subtree_extent(trans, root, child_bytenr, 8715 ret = btrfs_qgroup_insert_dirty_extent(trans,
8736 root->nodesize); 8716 root->fs_info, child_bytenr,
8717 root->nodesize, GFP_NOFS);
8737 if (ret) 8718 if (ret)
8738 goto out; 8719 goto out;
8739 } 8720 }
@@ -9906,6 +9887,7 @@ static int find_first_block_group(struct btrfs_root *root,
9906 } else { 9887 } else {
9907 ret = 0; 9888 ret = 0;
9908 } 9889 }
9890 free_extent_map(em);
9909 goto out; 9891 goto out;
9910 } 9892 }
9911 path->slots[0]++; 9893 path->slots[0]++;
@@ -9942,6 +9924,7 @@ void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
9942 block_group->iref = 0; 9924 block_group->iref = 0;
9943 block_group->inode = NULL; 9925 block_group->inode = NULL;
9944 spin_unlock(&block_group->lock); 9926 spin_unlock(&block_group->lock);
9927 ASSERT(block_group->io_ctl.inode == NULL);
9945 iput(inode); 9928 iput(inode);
9946 last = block_group->key.objectid + block_group->key.offset; 9929 last = block_group->key.objectid + block_group->key.offset;
9947 btrfs_put_block_group(block_group); 9930 btrfs_put_block_group(block_group);
@@ -9999,6 +9982,10 @@ int btrfs_free_block_groups(struct btrfs_fs_info *info)
9999 free_excluded_extents(info->extent_root, block_group); 9982 free_excluded_extents(info->extent_root, block_group);
10000 9983
10001 btrfs_remove_free_space_cache(block_group); 9984 btrfs_remove_free_space_cache(block_group);
9985 ASSERT(list_empty(&block_group->dirty_list));
9986 ASSERT(list_empty(&block_group->io_list));
9987 ASSERT(list_empty(&block_group->bg_list));
9988 ASSERT(atomic_read(&block_group->count) == 1);
10002 btrfs_put_block_group(block_group); 9989 btrfs_put_block_group(block_group);
10003 9990
10004 spin_lock(&info->block_group_cache_lock); 9991 spin_lock(&info->block_group_cache_lock);