aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--fs/btrfs/ctree.h2
-rw-r--r--fs/btrfs/extent-tree.c56
-rw-r--r--fs/btrfs/extent_io.h1
-rw-r--r--fs/btrfs/file.c26
-rw-r--r--fs/btrfs/inode-map.c3
-rw-r--r--fs/btrfs/inode.c37
-rw-r--r--fs/btrfs/relocation.c11
7 files changed, 73 insertions, 63 deletions
diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h
index acc6850a118f..09cdff0d58e8 100644
--- a/fs/btrfs/ctree.h
+++ b/fs/btrfs/ctree.h
@@ -2579,7 +2579,7 @@ int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
2579 struct btrfs_root *root, 2579 struct btrfs_root *root,
2580 u64 root_objectid, u64 owner, u64 offset, 2580 u64 root_objectid, u64 owner, u64 offset,
2581 struct btrfs_key *ins); 2581 struct btrfs_key *ins);
2582int btrfs_reserve_extent(struct btrfs_root *root, u64 num_bytes, 2582int btrfs_reserve_extent(struct btrfs_root *root, u64 ram_bytes, u64 num_bytes,
2583 u64 min_alloc_size, u64 empty_size, u64 hint_byte, 2583 u64 min_alloc_size, u64 empty_size, u64 hint_byte,
2584 struct btrfs_key *ins, int is_data, int delalloc); 2584 struct btrfs_key *ins, int is_data, int delalloc);
2585int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, 2585int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c
index f1121db65878..133c93b0a0f1 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);
@@ -105,7 +90,7 @@ static int find_next_key(struct btrfs_path *path, int level,
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_add_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, int delalloc); 93 u64 ram_bytes, u64 num_bytes, int delalloc);
109static int btrfs_free_reserved_bytes(struct btrfs_block_group_cache *cache, 94static int btrfs_free_reserved_bytes(struct btrfs_block_group_cache *cache,
110 u64 num_bytes, int delalloc); 95 u64 num_bytes, int delalloc);
111static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv, 96static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
@@ -3502,7 +3487,6 @@ again:
3502 dcs = BTRFS_DC_SETUP; 3487 dcs = BTRFS_DC_SETUP;
3503 else if (ret == -ENOSPC) 3488 else if (ret == -ENOSPC)
3504 set_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags); 3489 set_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags);
3505 btrfs_free_reserved_data_space(inode, 0, num_pages);
3506 3490
3507out_put: 3491out_put:
3508 iput(inode); 3492 iput(inode);
@@ -6500,8 +6484,9 @@ void btrfs_wait_block_group_reservations(struct btrfs_block_group_cache *bg)
6500/** 6484/**
6501 * btrfs_add_reserved_bytes - update the block_group and space info counters 6485 * btrfs_add_reserved_bytes - update the block_group and space info counters
6502 * @cache: The cache we are manipulating 6486 * @cache: The cache we are manipulating
6487 * @ram_bytes: The number of bytes of file content, and will be same to
6488 * @num_bytes except for the compress path.
6503 * @num_bytes: The number of bytes in question 6489 * @num_bytes: The number of bytes in question
6504 * @reserve: One of the reservation enums
6505 * @delalloc: The blocks are allocated for the delalloc write 6490 * @delalloc: The blocks are allocated for the delalloc write
6506 * 6491 *
6507 * This is called by the allocator when it reserves space. Metadata 6492 * This is called by the allocator when it reserves space. Metadata
@@ -6516,7 +6501,7 @@ void btrfs_wait_block_group_reservations(struct btrfs_block_group_cache *bg)
6516 * succeeds. 6501 * succeeds.
6517 */ 6502 */
6518static int btrfs_add_reserved_bytes(struct btrfs_block_group_cache *cache, 6503static int btrfs_add_reserved_bytes(struct btrfs_block_group_cache *cache,
6519 u64 num_bytes, int reserve, int delalloc) 6504 u64 ram_bytes, u64 num_bytes, int delalloc)
6520{ 6505{
6521 struct btrfs_space_info *space_info = cache->space_info; 6506 struct btrfs_space_info *space_info = cache->space_info;
6522 int ret = 0; 6507 int ret = 0;
@@ -6528,13 +6513,11 @@ static int btrfs_add_reserved_bytes(struct btrfs_block_group_cache *cache,
6528 } else { 6513 } else {
6529 cache->reserved += num_bytes; 6514 cache->reserved += num_bytes;
6530 space_info->bytes_reserved += num_bytes; 6515 space_info->bytes_reserved += num_bytes;
6531 if (reserve == RESERVE_ALLOC) {
6532 trace_btrfs_space_reservation(cache->fs_info,
6533 "space_info", space_info->flags,
6534 num_bytes, 0);
6535 space_info->bytes_may_use -= num_bytes;
6536 }
6537 6516
6517 trace_btrfs_space_reservation(cache->fs_info,
6518 "space_info", space_info->flags,
6519 ram_bytes, 0);
6520 space_info->bytes_may_use -= ram_bytes;
6538 if (delalloc) 6521 if (delalloc)
6539 cache->delalloc_bytes += num_bytes; 6522 cache->delalloc_bytes += num_bytes;
6540 } 6523 }
@@ -7433,9 +7416,9 @@ btrfs_release_block_group(struct btrfs_block_group_cache *cache,
7433 * the free space extent currently. 7416 * the free space extent currently.
7434 */ 7417 */
7435static noinline int find_free_extent(struct btrfs_root *orig_root, 7418static noinline int find_free_extent(struct btrfs_root *orig_root,
7436 u64 num_bytes, u64 empty_size, 7419 u64 ram_bytes, u64 num_bytes, u64 empty_size,
7437 u64 hint_byte, struct btrfs_key *ins, 7420 u64 hint_byte, struct btrfs_key *ins,
7438 u64 flags, int delalloc) 7421 u64 flags, int delalloc)
7439{ 7422{
7440 int ret = 0; 7423 int ret = 0;
7441 struct btrfs_root *root = orig_root->fs_info->extent_root; 7424 struct btrfs_root *root = orig_root->fs_info->extent_root;
@@ -7447,8 +7430,6 @@ static noinline int find_free_extent(struct btrfs_root *orig_root,
7447 struct btrfs_space_info *space_info; 7430 struct btrfs_space_info *space_info;
7448 int loop = 0; 7431 int loop = 0;
7449 int index = __get_raid_index(flags); 7432 int index = __get_raid_index(flags);
7450 int alloc_type = (flags & BTRFS_BLOCK_GROUP_DATA) ?
7451 RESERVE_ALLOC_NO_ACCOUNT : RESERVE_ALLOC;
7452 bool failed_cluster_refill = false; 7433 bool failed_cluster_refill = false;
7453 bool failed_alloc = false; 7434 bool failed_alloc = false;
7454 bool use_cluster = true; 7435 bool use_cluster = true;
@@ -7780,8 +7761,8 @@ checks:
7780 search_start - offset); 7761 search_start - offset);
7781 BUG_ON(offset > search_start); 7762 BUG_ON(offset > search_start);
7782 7763
7783 ret = btrfs_add_reserved_bytes(block_group, num_bytes, 7764 ret = btrfs_add_reserved_bytes(block_group, ram_bytes,
7784 alloc_type, delalloc); 7765 num_bytes, delalloc);
7785 if (ret == -EAGAIN) { 7766 if (ret == -EAGAIN) {
7786 btrfs_add_free_space(block_group, offset, num_bytes); 7767 btrfs_add_free_space(block_group, offset, num_bytes);
7787 goto loop; 7768 goto loop;
@@ -7953,7 +7934,7 @@ again:
7953 up_read(&info->groups_sem); 7934 up_read(&info->groups_sem);
7954} 7935}
7955 7936
7956int btrfs_reserve_extent(struct btrfs_root *root, 7937int btrfs_reserve_extent(struct btrfs_root *root, u64 ram_bytes,
7957 u64 num_bytes, u64 min_alloc_size, 7938 u64 num_bytes, u64 min_alloc_size,
7958 u64 empty_size, u64 hint_byte, 7939 u64 empty_size, u64 hint_byte,
7959 struct btrfs_key *ins, int is_data, int delalloc) 7940 struct btrfs_key *ins, int is_data, int delalloc)
@@ -7965,8 +7946,8 @@ int btrfs_reserve_extent(struct btrfs_root *root,
7965 flags = btrfs_get_alloc_profile(root, is_data); 7946 flags = btrfs_get_alloc_profile(root, is_data);
7966again: 7947again:
7967 WARN_ON(num_bytes < root->sectorsize); 7948 WARN_ON(num_bytes < root->sectorsize);
7968 ret = find_free_extent(root, num_bytes, empty_size, hint_byte, ins, 7949 ret = find_free_extent(root, ram_bytes, num_bytes, empty_size,
7969 flags, delalloc); 7950 hint_byte, ins, flags, delalloc);
7970 if (!ret && !is_data) { 7951 if (!ret && !is_data) {
7971 btrfs_dec_block_group_reservations(root->fs_info, 7952 btrfs_dec_block_group_reservations(root->fs_info,
7972 ins->objectid); 7953 ins->objectid);
@@ -7975,6 +7956,7 @@ again:
7975 num_bytes = min(num_bytes >> 1, ins->offset); 7956 num_bytes = min(num_bytes >> 1, ins->offset);
7976 num_bytes = round_down(num_bytes, root->sectorsize); 7957 num_bytes = round_down(num_bytes, root->sectorsize);
7977 num_bytes = max(num_bytes, min_alloc_size); 7958 num_bytes = max(num_bytes, min_alloc_size);
7959 ram_bytes = num_bytes;
7978 if (num_bytes == min_alloc_size) 7960 if (num_bytes == min_alloc_size)
7979 final_tried = true; 7961 final_tried = true;
7980 goto again; 7962 goto again;
@@ -8241,7 +8223,7 @@ int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
8241 return -EINVAL; 8223 return -EINVAL;
8242 8224
8243 ret = btrfs_add_reserved_bytes(block_group, ins->offset, 8225 ret = btrfs_add_reserved_bytes(block_group, ins->offset,
8244 RESERVE_ALLOC_NO_ACCOUNT, 0); 8226 ins->offset, 0);
8245 BUG_ON(ret); /* logic error */ 8227 BUG_ON(ret); /* logic error */
8246 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid, 8228 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
8247 0, owner, offset, ins, 1); 8229 0, owner, offset, ins, 1);
@@ -8385,7 +8367,7 @@ struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans,
8385 if (IS_ERR(block_rsv)) 8367 if (IS_ERR(block_rsv))
8386 return ERR_CAST(block_rsv); 8368 return ERR_CAST(block_rsv);
8387 8369
8388 ret = btrfs_reserve_extent(root, blocksize, blocksize, 8370 ret = btrfs_reserve_extent(root, blocksize, blocksize, blocksize,
8389 empty_size, hint, &ins, 0, 0); 8371 empty_size, hint, &ins, 0, 0);
8390 if (ret) 8372 if (ret)
8391 goto out_unuse; 8373 goto out_unuse;
diff --git a/fs/btrfs/extent_io.h b/fs/btrfs/extent_io.h
index c0c1c4fef6ce..b52ca5db01cb 100644
--- a/fs/btrfs/extent_io.h
+++ b/fs/btrfs/extent_io.h
@@ -20,6 +20,7 @@
20#define EXTENT_DAMAGED (1U << 14) 20#define EXTENT_DAMAGED (1U << 14)
21#define EXTENT_NORESERVE (1U << 15) 21#define EXTENT_NORESERVE (1U << 15)
22#define EXTENT_QGROUP_RESERVED (1U << 16) 22#define EXTENT_QGROUP_RESERVED (1U << 16)
23#define EXTENT_CLEAR_DATA_RESV (1U << 17)
23#define EXTENT_IOBITS (EXTENT_LOCKED | EXTENT_WRITEBACK) 24#define EXTENT_IOBITS (EXTENT_LOCKED | EXTENT_WRITEBACK)
24#define EXTENT_CTLBITS (EXTENT_DO_ACCOUNTING | EXTENT_FIRST_DELALLOC) 25#define EXTENT_CTLBITS (EXTENT_DO_ACCOUNTING | EXTENT_FIRST_DELALLOC)
25 26
diff --git a/fs/btrfs/file.c b/fs/btrfs/file.c
index 5842423f8f47..3391f2adf0c8 100644
--- a/fs/btrfs/file.c
+++ b/fs/btrfs/file.c
@@ -2675,6 +2675,7 @@ static long btrfs_fallocate(struct file *file, int mode,
2675 2675
2676 alloc_start = round_down(offset, blocksize); 2676 alloc_start = round_down(offset, blocksize);
2677 alloc_end = round_up(offset + len, blocksize); 2677 alloc_end = round_up(offset + len, blocksize);
2678 cur_offset = alloc_start;
2678 2679
2679 /* Make sure we aren't being give some crap mode */ 2680 /* Make sure we aren't being give some crap mode */
2680 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE)) 2681 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
@@ -2767,7 +2768,6 @@ static long btrfs_fallocate(struct file *file, int mode,
2767 2768
2768 /* First, check if we exceed the qgroup limit */ 2769 /* First, check if we exceed the qgroup limit */
2769 INIT_LIST_HEAD(&reserve_list); 2770 INIT_LIST_HEAD(&reserve_list);
2770 cur_offset = alloc_start;
2771 while (1) { 2771 while (1) {
2772 em = btrfs_get_extent(inode, NULL, 0, cur_offset, 2772 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
2773 alloc_end - cur_offset, 0); 2773 alloc_end - cur_offset, 0);
@@ -2794,6 +2794,14 @@ static long btrfs_fallocate(struct file *file, int mode,
2794 last_byte - cur_offset); 2794 last_byte - cur_offset);
2795 if (ret < 0) 2795 if (ret < 0)
2796 break; 2796 break;
2797 } else {
2798 /*
2799 * Do not need to reserve unwritten extent for this
2800 * range, free reserved data space first, otherwise
2801 * it'll result in false ENOSPC error.
2802 */
2803 btrfs_free_reserved_data_space(inode, cur_offset,
2804 last_byte - cur_offset);
2797 } 2805 }
2798 free_extent_map(em); 2806 free_extent_map(em);
2799 cur_offset = last_byte; 2807 cur_offset = last_byte;
@@ -2811,6 +2819,9 @@ static long btrfs_fallocate(struct file *file, int mode,
2811 range->start, 2819 range->start,
2812 range->len, 1 << inode->i_blkbits, 2820 range->len, 1 << inode->i_blkbits,
2813 offset + len, &alloc_hint); 2821 offset + len, &alloc_hint);
2822 else
2823 btrfs_free_reserved_data_space(inode, range->start,
2824 range->len);
2814 list_del(&range->list); 2825 list_del(&range->list);
2815 kfree(range); 2826 kfree(range);
2816 } 2827 }
@@ -2845,18 +2856,11 @@ out_unlock:
2845 unlock_extent_cached(&BTRFS_I(inode)->io_tree, alloc_start, locked_end, 2856 unlock_extent_cached(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
2846 &cached_state, GFP_KERNEL); 2857 &cached_state, GFP_KERNEL);
2847out: 2858out:
2848 /*
2849 * As we waited the extent range, the data_rsv_map must be empty
2850 * in the range, as written data range will be released from it.
2851 * And for prealloacted extent, it will also be released when
2852 * its metadata is written.
2853 * So this is completely used as cleanup.
2854 */
2855 btrfs_qgroup_free_data(inode, alloc_start, alloc_end - alloc_start);
2856 inode_unlock(inode); 2859 inode_unlock(inode);
2857 /* Let go of our reservation. */ 2860 /* Let go of our reservation. */
2858 btrfs_free_reserved_data_space(inode, alloc_start, 2861 if (ret != 0)
2859 alloc_end - alloc_start); 2862 btrfs_free_reserved_data_space(inode, alloc_start,
2863 alloc_end - cur_offset);
2860 return ret; 2864 return ret;
2861} 2865}
2862 2866
diff --git a/fs/btrfs/inode-map.c b/fs/btrfs/inode-map.c
index aa6fabaee72e..359ee861b5a4 100644
--- a/fs/btrfs/inode-map.c
+++ b/fs/btrfs/inode-map.c
@@ -495,10 +495,9 @@ again:
495 ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, prealloc, 495 ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, prealloc,
496 prealloc, prealloc, &alloc_hint); 496 prealloc, prealloc, &alloc_hint);
497 if (ret) { 497 if (ret) {
498 btrfs_delalloc_release_space(inode, 0, prealloc); 498 btrfs_delalloc_release_metadata(inode, prealloc);
499 goto out_put; 499 goto out_put;
500 } 500 }
501 btrfs_free_reserved_data_space(inode, 0, prealloc);
502 501
503 ret = btrfs_write_out_ino_cache(root, trans, path, inode); 502 ret = btrfs_write_out_ino_cache(root, trans, path, inode);
504out_put: 503out_put:
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 29636624a427..bcea20015d9a 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -566,6 +566,8 @@ cont:
566 PAGE_SET_WRITEBACK | 566 PAGE_SET_WRITEBACK |
567 page_error_op | 567 page_error_op |
568 PAGE_END_WRITEBACK); 568 PAGE_END_WRITEBACK);
569 btrfs_free_reserved_data_space_noquota(inode, start,
570 end - start + 1);
569 goto free_pages_out; 571 goto free_pages_out;
570 } 572 }
571 } 573 }
@@ -742,7 +744,7 @@ retry:
742 lock_extent(io_tree, async_extent->start, 744 lock_extent(io_tree, async_extent->start,
743 async_extent->start + async_extent->ram_size - 1); 745 async_extent->start + async_extent->ram_size - 1);
744 746
745 ret = btrfs_reserve_extent(root, 747 ret = btrfs_reserve_extent(root, async_extent->ram_size,
746 async_extent->compressed_size, 748 async_extent->compressed_size,
747 async_extent->compressed_size, 749 async_extent->compressed_size,
748 0, alloc_hint, &ins, 1, 1); 750 0, alloc_hint, &ins, 1, 1);
@@ -969,7 +971,8 @@ static noinline int cow_file_range(struct inode *inode,
969 EXTENT_DEFRAG, PAGE_UNLOCK | 971 EXTENT_DEFRAG, PAGE_UNLOCK |
970 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK | 972 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
971 PAGE_END_WRITEBACK); 973 PAGE_END_WRITEBACK);
972 974 btrfs_free_reserved_data_space_noquota(inode, start,
975 end - start + 1);
973 *nr_written = *nr_written + 976 *nr_written = *nr_written +
974 (end - start + PAGE_SIZE) / PAGE_SIZE; 977 (end - start + PAGE_SIZE) / PAGE_SIZE;
975 *page_started = 1; 978 *page_started = 1;
@@ -989,7 +992,7 @@ static noinline int cow_file_range(struct inode *inode,
989 unsigned long op; 992 unsigned long op;
990 993
991 cur_alloc_size = disk_num_bytes; 994 cur_alloc_size = disk_num_bytes;
992 ret = btrfs_reserve_extent(root, cur_alloc_size, 995 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
993 root->sectorsize, 0, alloc_hint, 996 root->sectorsize, 0, alloc_hint,
994 &ins, 1, 1); 997 &ins, 1, 1);
995 if (ret < 0) 998 if (ret < 0)
@@ -1489,8 +1492,10 @@ out_check:
1489 extent_clear_unlock_delalloc(inode, cur_offset, 1492 extent_clear_unlock_delalloc(inode, cur_offset,
1490 cur_offset + num_bytes - 1, 1493 cur_offset + num_bytes - 1,
1491 locked_page, EXTENT_LOCKED | 1494 locked_page, EXTENT_LOCKED |
1492 EXTENT_DELALLOC, PAGE_UNLOCK | 1495 EXTENT_DELALLOC |
1493 PAGE_SET_PRIVATE2); 1496 EXTENT_CLEAR_DATA_RESV,
1497 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1498
1494 if (!nolock && nocow) 1499 if (!nolock && nocow)
1495 btrfs_end_write_no_snapshoting(root); 1500 btrfs_end_write_no_snapshoting(root);
1496 cur_offset = extent_end; 1501 cur_offset = extent_end;
@@ -1807,7 +1812,9 @@ static void btrfs_clear_bit_hook(struct inode *inode,
1807 return; 1812 return;
1808 1813
1809 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID 1814 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
1810 && do_list && !(state->state & EXTENT_NORESERVE)) 1815 && do_list && !(state->state & EXTENT_NORESERVE)
1816 && (*bits & (EXTENT_DO_ACCOUNTING |
1817 EXTENT_CLEAR_DATA_RESV)))
1811 btrfs_free_reserved_data_space_noquota(inode, 1818 btrfs_free_reserved_data_space_noquota(inode,
1812 state->start, len); 1819 state->start, len);
1813 1820
@@ -7252,7 +7259,7 @@ static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7252 int ret; 7259 int ret;
7253 7260
7254 alloc_hint = get_extent_allocation_hint(inode, start, len); 7261 alloc_hint = get_extent_allocation_hint(inode, start, len);
7255 ret = btrfs_reserve_extent(root, len, root->sectorsize, 0, 7262 ret = btrfs_reserve_extent(root, len, len, root->sectorsize, 0,
7256 alloc_hint, &ins, 1, 1); 7263 alloc_hint, &ins, 1, 1);
7257 if (ret) 7264 if (ret)
7258 return ERR_PTR(ret); 7265 return ERR_PTR(ret);
@@ -7752,6 +7759,13 @@ static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7752 ret = PTR_ERR(em2); 7759 ret = PTR_ERR(em2);
7753 goto unlock_err; 7760 goto unlock_err;
7754 } 7761 }
7762 /*
7763 * For inode marked NODATACOW or extent marked PREALLOC,
7764 * use the existing or preallocated extent, so does not
7765 * need to adjust btrfs_space_info's bytes_may_use.
7766 */
7767 btrfs_free_reserved_data_space_noquota(inode,
7768 start, len);
7755 goto unlock; 7769 goto unlock;
7756 } 7770 }
7757 } 7771 }
@@ -7786,7 +7800,6 @@ unlock:
7786 i_size_write(inode, start + len); 7800 i_size_write(inode, start + len);
7787 7801
7788 adjust_dio_outstanding_extents(inode, dio_data, len); 7802 adjust_dio_outstanding_extents(inode, dio_data, len);
7789 btrfs_free_reserved_data_space(inode, start, len);
7790 WARN_ON(dio_data->reserve < len); 7803 WARN_ON(dio_data->reserve < len);
7791 dio_data->reserve -= len; 7804 dio_data->reserve -= len;
7792 dio_data->unsubmitted_oe_range_end = start + len; 7805 dio_data->unsubmitted_oe_range_end = start + len;
@@ -10306,6 +10319,7 @@ static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10306 u64 last_alloc = (u64)-1; 10319 u64 last_alloc = (u64)-1;
10307 int ret = 0; 10320 int ret = 0;
10308 bool own_trans = true; 10321 bool own_trans = true;
10322 u64 end = start + num_bytes - 1;
10309 10323
10310 if (trans) 10324 if (trans)
10311 own_trans = false; 10325 own_trans = false;
@@ -10327,8 +10341,8 @@ static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10327 * sized chunks. 10341 * sized chunks.
10328 */ 10342 */
10329 cur_bytes = min(cur_bytes, last_alloc); 10343 cur_bytes = min(cur_bytes, last_alloc);
10330 ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0, 10344 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10331 *alloc_hint, &ins, 1, 0); 10345 min_size, 0, *alloc_hint, &ins, 1, 0);
10332 if (ret) { 10346 if (ret) {
10333 if (own_trans) 10347 if (own_trans)
10334 btrfs_end_transaction(trans, root); 10348 btrfs_end_transaction(trans, root);
@@ -10414,6 +10428,9 @@ next:
10414 if (own_trans) 10428 if (own_trans)
10415 btrfs_end_transaction(trans, root); 10429 btrfs_end_transaction(trans, root);
10416 } 10430 }
10431 if (cur_offset < end)
10432 btrfs_free_reserved_data_space(inode, cur_offset,
10433 end - cur_offset + 1);
10417 return ret; 10434 return ret;
10418} 10435}
10419 10436
diff --git a/fs/btrfs/relocation.c b/fs/btrfs/relocation.c
index 71b4b70f56b9..8a2c2a07987b 100644
--- a/fs/btrfs/relocation.c
+++ b/fs/btrfs/relocation.c
@@ -3040,6 +3040,7 @@ int prealloc_file_extent_cluster(struct inode *inode,
3040 int ret = 0; 3040 int ret = 0;
3041 u64 prealloc_start = cluster->start - offset; 3041 u64 prealloc_start = cluster->start - offset;
3042 u64 prealloc_end = cluster->end - offset; 3042 u64 prealloc_end = cluster->end - offset;
3043 u64 cur_offset;
3043 3044
3044 BUG_ON(cluster->start != cluster->boundary[0]); 3045 BUG_ON(cluster->start != cluster->boundary[0]);
3045 inode_lock(inode); 3046 inode_lock(inode);
@@ -3049,6 +3050,7 @@ int prealloc_file_extent_cluster(struct inode *inode,
3049 if (ret) 3050 if (ret)
3050 goto out; 3051 goto out;
3051 3052
3053 cur_offset = prealloc_start;
3052 while (nr < cluster->nr) { 3054 while (nr < cluster->nr) {
3053 start = cluster->boundary[nr] - offset; 3055 start = cluster->boundary[nr] - offset;
3054 if (nr + 1 < cluster->nr) 3056 if (nr + 1 < cluster->nr)
@@ -3058,16 +3060,21 @@ int prealloc_file_extent_cluster(struct inode *inode,
3058 3060
3059 lock_extent(&BTRFS_I(inode)->io_tree, start, end); 3061 lock_extent(&BTRFS_I(inode)->io_tree, start, end);
3060 num_bytes = end + 1 - start; 3062 num_bytes = end + 1 - start;
3063 if (cur_offset < start)
3064 btrfs_free_reserved_data_space(inode, cur_offset,
3065 start - cur_offset);
3061 ret = btrfs_prealloc_file_range(inode, 0, start, 3066 ret = btrfs_prealloc_file_range(inode, 0, start,
3062 num_bytes, num_bytes, 3067 num_bytes, num_bytes,
3063 end + 1, &alloc_hint); 3068 end + 1, &alloc_hint);
3069 cur_offset = end + 1;
3064 unlock_extent(&BTRFS_I(inode)->io_tree, start, end); 3070 unlock_extent(&BTRFS_I(inode)->io_tree, start, end);
3065 if (ret) 3071 if (ret)
3066 break; 3072 break;
3067 nr++; 3073 nr++;
3068 } 3074 }
3069 btrfs_free_reserved_data_space(inode, prealloc_start, 3075 if (cur_offset < prealloc_end)
3070 prealloc_end + 1 - prealloc_start); 3076 btrfs_free_reserved_data_space(inode, cur_offset,
3077 prealloc_end + 1 - cur_offset);
3071out: 3078out:
3072 inode_unlock(inode); 3079 inode_unlock(inode);
3073 return ret; 3080 return ret;