aboutsummaryrefslogtreecommitdiffstats
path: root/fs/btrfs/extent-tree.c
diff options
context:
space:
mode:
authorIngo Molnar <mingo@kernel.org>2016-09-15 02:24:53 -0400
committerIngo Molnar <mingo@kernel.org>2016-09-15 02:24:53 -0400
commitd4b80afbba49e968623330f1336da8c724da8aad (patch)
treea9478bd77d8b001a6a7119328d34e9666d7bfe93 /fs/btrfs/extent-tree.c
parentfcd709ef20a9d83bdb7524d27cd6719dac8690a0 (diff)
parent4cea8776571b18db7485930cb422faa739580c8c (diff)
Merge branch 'linus' into x86/asm, to pick up recent fixes
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Diffstat (limited to 'fs/btrfs/extent-tree.c')
-rw-r--r--fs/btrfs/extent-tree.c214
1 files changed, 104 insertions, 110 deletions
diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c
index 61b494e8e604..38c2df84cabd 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:
@@ -4907,11 +4901,6 @@ btrfs_calc_reclaim_metadata_size(struct btrfs_root *root,
4907 u64 expected; 4901 u64 expected;
4908 u64 to_reclaim = 0; 4902 u64 to_reclaim = 0;
4909 4903
4910 to_reclaim = min_t(u64, num_online_cpus() * SZ_1M, SZ_16M);
4911 if (can_overcommit(root, space_info, to_reclaim,
4912 BTRFS_RESERVE_FLUSH_ALL))
4913 return 0;
4914
4915 list_for_each_entry(ticket, &space_info->tickets, list) 4904 list_for_each_entry(ticket, &space_info->tickets, list)
4916 to_reclaim += ticket->bytes; 4905 to_reclaim += ticket->bytes;
4917 list_for_each_entry(ticket, &space_info->priority_tickets, list) 4906 list_for_each_entry(ticket, &space_info->priority_tickets, list)
@@ -4919,6 +4908,11 @@ btrfs_calc_reclaim_metadata_size(struct btrfs_root *root,
4919 if (to_reclaim) 4908 if (to_reclaim)
4920 return to_reclaim; 4909 return to_reclaim;
4921 4910
4911 to_reclaim = min_t(u64, num_online_cpus() * SZ_1M, SZ_16M);
4912 if (can_overcommit(root, space_info, to_reclaim,
4913 BTRFS_RESERVE_FLUSH_ALL))
4914 return 0;
4915
4922 used = space_info->bytes_used + space_info->bytes_reserved + 4916 used = space_info->bytes_used + space_info->bytes_reserved +
4923 space_info->bytes_pinned + space_info->bytes_readonly + 4917 space_info->bytes_pinned + space_info->bytes_readonly +
4924 space_info->bytes_may_use; 4918 space_info->bytes_may_use;
@@ -4972,12 +4966,12 @@ static void wake_all_tickets(struct list_head *head)
4972 */ 4966 */
4973static void btrfs_async_reclaim_metadata_space(struct work_struct *work) 4967static void btrfs_async_reclaim_metadata_space(struct work_struct *work)
4974{ 4968{
4975 struct reserve_ticket *last_ticket = NULL;
4976 struct btrfs_fs_info *fs_info; 4969 struct btrfs_fs_info *fs_info;
4977 struct btrfs_space_info *space_info; 4970 struct btrfs_space_info *space_info;
4978 u64 to_reclaim; 4971 u64 to_reclaim;
4979 int flush_state; 4972 int flush_state;
4980 int commit_cycles = 0; 4973 int commit_cycles = 0;
4974 u64 last_tickets_id;
4981 4975
4982 fs_info = container_of(work, struct btrfs_fs_info, async_reclaim_work); 4976 fs_info = container_of(work, struct btrfs_fs_info, async_reclaim_work);
4983 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA); 4977 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
@@ -4990,8 +4984,7 @@ static void btrfs_async_reclaim_metadata_space(struct work_struct *work)
4990 spin_unlock(&space_info->lock); 4984 spin_unlock(&space_info->lock);
4991 return; 4985 return;
4992 } 4986 }
4993 last_ticket = list_first_entry(&space_info->tickets, 4987 last_tickets_id = space_info->tickets_id;
4994 struct reserve_ticket, list);
4995 spin_unlock(&space_info->lock); 4988 spin_unlock(&space_info->lock);
4996 4989
4997 flush_state = FLUSH_DELAYED_ITEMS_NR; 4990 flush_state = FLUSH_DELAYED_ITEMS_NR;
@@ -5011,10 +5004,10 @@ static void btrfs_async_reclaim_metadata_space(struct work_struct *work)
5011 space_info); 5004 space_info);
5012 ticket = list_first_entry(&space_info->tickets, 5005 ticket = list_first_entry(&space_info->tickets,
5013 struct reserve_ticket, list); 5006 struct reserve_ticket, list);
5014 if (last_ticket == ticket) { 5007 if (last_tickets_id == space_info->tickets_id) {
5015 flush_state++; 5008 flush_state++;
5016 } else { 5009 } else {
5017 last_ticket = ticket; 5010 last_tickets_id = space_info->tickets_id;
5018 flush_state = FLUSH_DELAYED_ITEMS_NR; 5011 flush_state = FLUSH_DELAYED_ITEMS_NR;
5019 if (commit_cycles) 5012 if (commit_cycles)
5020 commit_cycles--; 5013 commit_cycles--;
@@ -5390,6 +5383,7 @@ again:
5390 list_del_init(&ticket->list); 5383 list_del_init(&ticket->list);
5391 num_bytes -= ticket->bytes; 5384 num_bytes -= ticket->bytes;
5392 ticket->bytes = 0; 5385 ticket->bytes = 0;
5386 space_info->tickets_id++;
5393 wake_up(&ticket->wait); 5387 wake_up(&ticket->wait);
5394 } else { 5388 } else {
5395 ticket->bytes -= num_bytes; 5389 ticket->bytes -= num_bytes;
@@ -5432,6 +5426,7 @@ again:
5432 num_bytes -= ticket->bytes; 5426 num_bytes -= ticket->bytes;
5433 space_info->bytes_may_use += ticket->bytes; 5427 space_info->bytes_may_use += ticket->bytes;
5434 ticket->bytes = 0; 5428 ticket->bytes = 0;
5429 space_info->tickets_id++;
5435 wake_up(&ticket->wait); 5430 wake_up(&ticket->wait);
5436 } else { 5431 } else {
5437 trace_btrfs_space_reservation(fs_info, "space_info", 5432 trace_btrfs_space_reservation(fs_info, "space_info",
@@ -6497,19 +6492,15 @@ void btrfs_wait_block_group_reservations(struct btrfs_block_group_cache *bg)
6497} 6492}
6498 6493
6499/** 6494/**
6500 * btrfs_update_reserved_bytes - update the block_group and space info counters 6495 * btrfs_add_reserved_bytes - update the block_group and space info counters
6501 * @cache: The cache we are manipulating 6496 * @cache: The cache we are manipulating
6497 * @ram_bytes: The number of bytes of file content, and will be same to
6498 * @num_bytes except for the compress path.
6502 * @num_bytes: The number of bytes in question 6499 * @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 6500 * @delalloc: The blocks are allocated for the delalloc write
6505 * 6501 *
6506 * This is called by the allocator when it reserves space, or by somebody who is 6502 * 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 6503 * 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 6504 * 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 6505 * 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 6506 * allocating less disk space for the amount of data we have reserved in the
@@ -6519,44 +6510,63 @@ void btrfs_wait_block_group_reservations(struct btrfs_block_group_cache *bg)
6519 * make the reservation and return -EAGAIN, otherwise this function always 6510 * make the reservation and return -EAGAIN, otherwise this function always
6520 * succeeds. 6511 * succeeds.
6521 */ 6512 */
6522static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache, 6513static int btrfs_add_reserved_bytes(struct btrfs_block_group_cache *cache,
6523 u64 num_bytes, int reserve, int delalloc) 6514 u64 ram_bytes, u64 num_bytes, int delalloc)
6524{ 6515{
6525 struct btrfs_space_info *space_info = cache->space_info; 6516 struct btrfs_space_info *space_info = cache->space_info;
6526 int ret = 0; 6517 int ret = 0;
6527 6518
6528 spin_lock(&space_info->lock); 6519 spin_lock(&space_info->lock);
6529 spin_lock(&cache->lock); 6520 spin_lock(&cache->lock);
6530 if (reserve != RESERVE_FREE) { 6521 if (cache->ro) {
6531 if (cache->ro) { 6522 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 { 6523 } else {
6547 if (cache->ro) 6524 cache->reserved += num_bytes;
6548 space_info->bytes_readonly += num_bytes; 6525 space_info->bytes_reserved += num_bytes;
6549 cache->reserved -= num_bytes;
6550 space_info->bytes_reserved -= num_bytes;
6551 6526
6527 trace_btrfs_space_reservation(cache->fs_info,
6528 "space_info", space_info->flags,
6529 ram_bytes, 0);
6530 space_info->bytes_may_use -= ram_bytes;
6552 if (delalloc) 6531 if (delalloc)
6553 cache->delalloc_bytes -= num_bytes; 6532 cache->delalloc_bytes += num_bytes;
6554 } 6533 }
6555 spin_unlock(&cache->lock); 6534 spin_unlock(&cache->lock);
6556 spin_unlock(&space_info->lock); 6535 spin_unlock(&space_info->lock);
6557 return ret; 6536 return ret;
6558} 6537}
6559 6538
6539/**
6540 * btrfs_free_reserved_bytes - update the block_group and space info counters
6541 * @cache: The cache we are manipulating
6542 * @num_bytes: The number of bytes in question
6543 * @delalloc: The blocks are allocated for the delalloc write
6544 *
6545 * This is called by somebody who is freeing space that was never actually used
6546 * on disk. For example if you reserve some space for a new leaf in transaction
6547 * A and before transaction A commits you free that leaf, you call this with
6548 * reserve set to 0 in order to clear the reservation.
6549 */
6550
6551static int btrfs_free_reserved_bytes(struct btrfs_block_group_cache *cache,
6552 u64 num_bytes, int delalloc)
6553{
6554 struct btrfs_space_info *space_info = cache->space_info;
6555 int ret = 0;
6556
6557 spin_lock(&space_info->lock);
6558 spin_lock(&cache->lock);
6559 if (cache->ro)
6560 space_info->bytes_readonly += num_bytes;
6561 cache->reserved -= num_bytes;
6562 space_info->bytes_reserved -= num_bytes;
6563
6564 if (delalloc)
6565 cache->delalloc_bytes -= num_bytes;
6566 spin_unlock(&cache->lock);
6567 spin_unlock(&space_info->lock);
6568 return ret;
6569}
6560void btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans, 6570void btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
6561 struct btrfs_root *root) 6571 struct btrfs_root *root)
6562{ 6572{
@@ -7191,7 +7201,7 @@ void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
7191 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)); 7201 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
7192 7202
7193 btrfs_add_free_space(cache, buf->start, buf->len); 7203 btrfs_add_free_space(cache, buf->start, buf->len);
7194 btrfs_update_reserved_bytes(cache, buf->len, RESERVE_FREE, 0); 7204 btrfs_free_reserved_bytes(cache, buf->len, 0);
7195 btrfs_put_block_group(cache); 7205 btrfs_put_block_group(cache);
7196 trace_btrfs_reserved_extent_free(root, buf->start, buf->len); 7206 trace_btrfs_reserved_extent_free(root, buf->start, buf->len);
7197 pin = 0; 7207 pin = 0;
@@ -7416,9 +7426,9 @@ btrfs_release_block_group(struct btrfs_block_group_cache *cache,
7416 * the free space extent currently. 7426 * the free space extent currently.
7417 */ 7427 */
7418static noinline int find_free_extent(struct btrfs_root *orig_root, 7428static noinline int find_free_extent(struct btrfs_root *orig_root,
7419 u64 num_bytes, u64 empty_size, 7429 u64 ram_bytes, u64 num_bytes, u64 empty_size,
7420 u64 hint_byte, struct btrfs_key *ins, 7430 u64 hint_byte, struct btrfs_key *ins,
7421 u64 flags, int delalloc) 7431 u64 flags, int delalloc)
7422{ 7432{
7423 int ret = 0; 7433 int ret = 0;
7424 struct btrfs_root *root = orig_root->fs_info->extent_root; 7434 struct btrfs_root *root = orig_root->fs_info->extent_root;
@@ -7430,8 +7440,6 @@ static noinline int find_free_extent(struct btrfs_root *orig_root,
7430 struct btrfs_space_info *space_info; 7440 struct btrfs_space_info *space_info;
7431 int loop = 0; 7441 int loop = 0;
7432 int index = __get_raid_index(flags); 7442 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; 7443 bool failed_cluster_refill = false;
7436 bool failed_alloc = false; 7444 bool failed_alloc = false;
7437 bool use_cluster = true; 7445 bool use_cluster = true;
@@ -7763,8 +7771,8 @@ checks:
7763 search_start - offset); 7771 search_start - offset);
7764 BUG_ON(offset > search_start); 7772 BUG_ON(offset > search_start);
7765 7773
7766 ret = btrfs_update_reserved_bytes(block_group, num_bytes, 7774 ret = btrfs_add_reserved_bytes(block_group, ram_bytes,
7767 alloc_type, delalloc); 7775 num_bytes, delalloc);
7768 if (ret == -EAGAIN) { 7776 if (ret == -EAGAIN) {
7769 btrfs_add_free_space(block_group, offset, num_bytes); 7777 btrfs_add_free_space(block_group, offset, num_bytes);
7770 goto loop; 7778 goto loop;
@@ -7936,7 +7944,7 @@ again:
7936 up_read(&info->groups_sem); 7944 up_read(&info->groups_sem);
7937} 7945}
7938 7946
7939int btrfs_reserve_extent(struct btrfs_root *root, 7947int btrfs_reserve_extent(struct btrfs_root *root, u64 ram_bytes,
7940 u64 num_bytes, u64 min_alloc_size, 7948 u64 num_bytes, u64 min_alloc_size,
7941 u64 empty_size, u64 hint_byte, 7949 u64 empty_size, u64 hint_byte,
7942 struct btrfs_key *ins, int is_data, int delalloc) 7950 struct btrfs_key *ins, int is_data, int delalloc)
@@ -7948,8 +7956,8 @@ int btrfs_reserve_extent(struct btrfs_root *root,
7948 flags = btrfs_get_alloc_profile(root, is_data); 7956 flags = btrfs_get_alloc_profile(root, is_data);
7949again: 7957again:
7950 WARN_ON(num_bytes < root->sectorsize); 7958 WARN_ON(num_bytes < root->sectorsize);
7951 ret = find_free_extent(root, num_bytes, empty_size, hint_byte, ins, 7959 ret = find_free_extent(root, ram_bytes, num_bytes, empty_size,
7952 flags, delalloc); 7960 hint_byte, ins, flags, delalloc);
7953 if (!ret && !is_data) { 7961 if (!ret && !is_data) {
7954 btrfs_dec_block_group_reservations(root->fs_info, 7962 btrfs_dec_block_group_reservations(root->fs_info,
7955 ins->objectid); 7963 ins->objectid);
@@ -7958,6 +7966,7 @@ again:
7958 num_bytes = min(num_bytes >> 1, ins->offset); 7966 num_bytes = min(num_bytes >> 1, ins->offset);
7959 num_bytes = round_down(num_bytes, root->sectorsize); 7967 num_bytes = round_down(num_bytes, root->sectorsize);
7960 num_bytes = max(num_bytes, min_alloc_size); 7968 num_bytes = max(num_bytes, min_alloc_size);
7969 ram_bytes = num_bytes;
7961 if (num_bytes == min_alloc_size) 7970 if (num_bytes == min_alloc_size)
7962 final_tried = true; 7971 final_tried = true;
7963 goto again; 7972 goto again;
@@ -7995,7 +8004,7 @@ static int __btrfs_free_reserved_extent(struct btrfs_root *root,
7995 if (btrfs_test_opt(root->fs_info, DISCARD)) 8004 if (btrfs_test_opt(root->fs_info, DISCARD))
7996 ret = btrfs_discard_extent(root, start, len, NULL); 8005 ret = btrfs_discard_extent(root, start, len, NULL);
7997 btrfs_add_free_space(cache, start, len); 8006 btrfs_add_free_space(cache, start, len);
7998 btrfs_update_reserved_bytes(cache, len, RESERVE_FREE, delalloc); 8007 btrfs_free_reserved_bytes(cache, len, delalloc);
7999 trace_btrfs_reserved_extent_free(root, start, len); 8008 trace_btrfs_reserved_extent_free(root, start, len);
8000 } 8009 }
8001 8010
@@ -8208,6 +8217,7 @@ int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
8208{ 8217{
8209 int ret; 8218 int ret;
8210 struct btrfs_block_group_cache *block_group; 8219 struct btrfs_block_group_cache *block_group;
8220 struct btrfs_space_info *space_info;
8211 8221
8212 /* 8222 /*
8213 * Mixed block groups will exclude before processing the log so we only 8223 * Mixed block groups will exclude before processing the log so we only
@@ -8223,9 +8233,14 @@ int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
8223 if (!block_group) 8233 if (!block_group)
8224 return -EINVAL; 8234 return -EINVAL;
8225 8235
8226 ret = btrfs_update_reserved_bytes(block_group, ins->offset, 8236 space_info = block_group->space_info;
8227 RESERVE_ALLOC_NO_ACCOUNT, 0); 8237 spin_lock(&space_info->lock);
8228 BUG_ON(ret); /* logic error */ 8238 spin_lock(&block_group->lock);
8239 space_info->bytes_reserved += ins->offset;
8240 block_group->reserved += ins->offset;
8241 spin_unlock(&block_group->lock);
8242 spin_unlock(&space_info->lock);
8243
8229 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid, 8244 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
8230 0, owner, offset, ins, 1); 8245 0, owner, offset, ins, 1);
8231 btrfs_put_block_group(block_group); 8246 btrfs_put_block_group(block_group);
@@ -8368,7 +8383,7 @@ struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans,
8368 if (IS_ERR(block_rsv)) 8383 if (IS_ERR(block_rsv))
8369 return ERR_CAST(block_rsv); 8384 return ERR_CAST(block_rsv);
8370 8385
8371 ret = btrfs_reserve_extent(root, blocksize, blocksize, 8386 ret = btrfs_reserve_extent(root, blocksize, blocksize, blocksize,
8372 empty_size, hint, &ins, 0, 0); 8387 empty_size, hint, &ins, 0, 0);
8373 if (ret) 8388 if (ret)
8374 goto out_unuse; 8389 goto out_unuse;
@@ -8521,35 +8536,6 @@ reada:
8521 wc->reada_slot = slot; 8536 wc->reada_slot = slot;
8522} 8537}
8523 8538
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, 8539static int account_leaf_items(struct btrfs_trans_handle *trans,
8554 struct btrfs_root *root, 8540 struct btrfs_root *root,
8555 struct extent_buffer *eb) 8541 struct extent_buffer *eb)
@@ -8583,7 +8569,8 @@ static int account_leaf_items(struct btrfs_trans_handle *trans,
8583 8569
8584 num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi); 8570 num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi);
8585 8571
8586 ret = record_one_subtree_extent(trans, root, bytenr, num_bytes); 8572 ret = btrfs_qgroup_insert_dirty_extent(trans, root->fs_info,
8573 bytenr, num_bytes, GFP_NOFS);
8587 if (ret) 8574 if (ret)
8588 return ret; 8575 return ret;
8589 } 8576 }
@@ -8732,8 +8719,9 @@ walk_down:
8732 btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK); 8719 btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
8733 path->locks[level] = BTRFS_READ_LOCK_BLOCKING; 8720 path->locks[level] = BTRFS_READ_LOCK_BLOCKING;
8734 8721
8735 ret = record_one_subtree_extent(trans, root, child_bytenr, 8722 ret = btrfs_qgroup_insert_dirty_extent(trans,
8736 root->nodesize); 8723 root->fs_info, child_bytenr,
8724 root->nodesize, GFP_NOFS);
8737 if (ret) 8725 if (ret)
8738 goto out; 8726 goto out;
8739 } 8727 }
@@ -9906,6 +9894,7 @@ static int find_first_block_group(struct btrfs_root *root,
9906 } else { 9894 } else {
9907 ret = 0; 9895 ret = 0;
9908 } 9896 }
9897 free_extent_map(em);
9909 goto out; 9898 goto out;
9910 } 9899 }
9911 path->slots[0]++; 9900 path->slots[0]++;
@@ -9942,6 +9931,7 @@ void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
9942 block_group->iref = 0; 9931 block_group->iref = 0;
9943 block_group->inode = NULL; 9932 block_group->inode = NULL;
9944 spin_unlock(&block_group->lock); 9933 spin_unlock(&block_group->lock);
9934 ASSERT(block_group->io_ctl.inode == NULL);
9945 iput(inode); 9935 iput(inode);
9946 last = block_group->key.objectid + block_group->key.offset; 9936 last = block_group->key.objectid + block_group->key.offset;
9947 btrfs_put_block_group(block_group); 9937 btrfs_put_block_group(block_group);
@@ -9999,6 +9989,10 @@ int btrfs_free_block_groups(struct btrfs_fs_info *info)
9999 free_excluded_extents(info->extent_root, block_group); 9989 free_excluded_extents(info->extent_root, block_group);
10000 9990
10001 btrfs_remove_free_space_cache(block_group); 9991 btrfs_remove_free_space_cache(block_group);
9992 ASSERT(list_empty(&block_group->dirty_list));
9993 ASSERT(list_empty(&block_group->io_list));
9994 ASSERT(list_empty(&block_group->bg_list));
9995 ASSERT(atomic_read(&block_group->count) == 1);
10002 btrfs_put_block_group(block_group); 9996 btrfs_put_block_group(block_group);
10003 9997
10004 spin_lock(&info->block_group_cache_lock); 9998 spin_lock(&info->block_group_cache_lock);