[PATCH 17/23] btrfs: use the same helper for data and metadata reservations

Josef Bacik
2020-07-08T14:00:56Z

Now that data reservations follow the same pattern as metadata
reservations we can simply rename __reserve_metadata_bytes to
__reserve_bytes and use that helper for data reservations.

Things to keep in mind, btrfs_can_overcommit() returns 0 for data,
because we can never overcommit. We also will never pass in FLUSH_ALL
for data, so we'll simply be added to the priority list and go straight
into handle_reserve_ticket.

Reviewed-by: Nikolay Borisov <nborisov@suse.com>
Tested-by: Nikolay Borisov <nborisov@suse.com>
Signed-off-by: Josef Bacik <josef@toxicpanda.com>
---
fs/btrfs/space-info.c | 49 +++++++++++++------------------------------
1 file changed, 14 insertions(+), 35 deletions(-)

diff --git a/fs/btrfs/space-info.c b/fs/btrfs/space-info.c
index afdc774cebe9..605be9f4fcac 100644
--- a/fs/btrfs/space-info.c
+++ b/fs/btrfs/space-info.c
@@ -1252,10 +1252,9 @@ static inline bool is_normal_flushing(enum btrfs_reserve_flush_enum flush)
* regain reservations will be made and this will fail if there is not enough
* space already.
*/
-static int __reserve_metadata_bytes(struct btrfs_fs_info *fs_info,
- struct btrfs_space_info *space_info,
- u64 orig_bytes,
- enum btrfs_reserve_flush_enum flush)
+static int __reserve_bytes(struct btrfs_fs_info *fs_info,
+ struct btrfs_space_info *space_info, u64 orig_bytes,
+ enum btrfs_reserve_flush_enum flush)
{
struct reserve_ticket ticket;
u64 used;
@@ -1367,8 +1366,8 @@ int btrfs_reserve_metadata_bytes(struct btrfs_root *root,
struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
int ret;

- ret = __reserve_metadata_bytes(fs_info, block_rsv->space_info,
- orig_bytes, flush);
+ ret = __reserve_bytes(fs_info, block_rsv->space_info, orig_bytes,
+ flush);
if (ret == -ENOSPC &&
unlikely(root->orphan_cleanup_state == ORPHAN_CLEANUP_STARTED)) {
if (block_rsv != global_rsv &&
@@ -1400,38 +1399,18 @@ int btrfs_reserve_data_bytes(struct btrfs_fs_info *fs_info, u64 bytes,
enum btrfs_reserve_flush_enum flush)
{
struct btrfs_space_info *data_sinfo = fs_info->data_sinfo;
- u64 used;
- int ret = -ENOSPC;
- bool pending_tickets;
+ int ret;

+ ASSERT(flush == BTRFS_RESERVE_FLUSH_DATA ||
+ flush == BTRFS_RESERVE_FLUSH_FREE_SPACE_INODE);
ASSERT(!current->journal_info || flush != BTRFS_RESERVE_FLUSH_DATA);

- spin_lock(&data_sinfo->lock);
- used = btrfs_space_info_used(data_sinfo, true);
- pending_tickets = !list_empty(&data_sinfo->tickets) ||
- !list_empty(&data_sinfo->priority_tickets);
-
- if (pending_tickets ||
- used + bytes > data_sinfo->total_bytes) {
- struct reserve_ticket ticket;
-
- init_waitqueue_head(&ticket.wait);
- ticket.bytes = bytes;
- ticket.error = 0;
- list_add_tail(&ticket.list, &data_sinfo->priority_tickets);
- data_sinfo->reclaim_size += bytes;
- spin_unlock(&data_sinfo->lock);
-
- ret = handle_reserve_ticket(fs_info, data_sinfo, &ticket,
- flush);
- } else {
- btrfs_space_info_update_bytes_may_use(fs_info, data_sinfo, bytes);
- ret = 0;
- spin_unlock(&data_sinfo->lock);
- }
- if (ret)
- trace_btrfs_space_reservation(fs_info,
- "space_info:enospc",
+ ret = __reserve_bytes(fs_info, data_sinfo, bytes, flush);
+ if (ret == -ENOSPC) {
+ trace_btrfs_space_reservation(fs_info, "space_info:enospc",
data_sinfo->flags, bytes, 1);
+ if (btrfs_test_opt(fs_info, ENOSPC_DEBUG))
+ btrfs_dump_space_info(fs_info, data_sinfo, bytes, 0);
+ }
return ret;
}
--
2.24.1