diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c index 274318e9114eee4cdd95108d6fb309c16fdd4678..3495710c72b82a2a3780afd0b31709db05b859b4 100644 --- a/fs/btrfs/extent-tree.c +++ b/fs/btrfs/extent-tree.c @@ -4182,8 +4182,6 @@ static int __btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info, if (pin) pin_down_extent(cache, start, len, 1); else { - if (btrfs_test_opt(fs_info, DISCARD)) - ret = btrfs_discard_extent(fs_info, start, len, NULL); btrfs_add_free_space(cache, start, len); btrfs_free_reserved_bytes(cache, len, delalloc); trace_btrfs_reserved_extent_free(fs_info, start, len); diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c index c70baafb2a3920ced47005c3d60b11c07c89af88..c1cee6a01c35a828606281251df7879da4a3dc3d 100644 --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c @@ -3250,10 +3250,19 @@ static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent) if ((ret || !logical_len) && clear_reserved_extent && !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && - !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) + !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { + /* + * Discard the range before returning it back to the + * free space pool + */ + if (ret && btrfs_test_opt(fs_info, DISCARD)) + btrfs_discard_extent(fs_info, + ordered_extent->start, + ordered_extent->disk_len, NULL); btrfs_free_reserved_extent(fs_info, ordered_extent->start, ordered_extent->disk_len, 1); + } }