Skip to content

Commit 4e06bdd

Browse files
Josef Bacikchrismason-xx
authored andcommitted
Btrfs: try committing transaction before returning ENOSPC
This fixes a problem where we could return -ENOSPC when we may actually have plenty of space, the space is just pinned. Instead of returning -ENOSPC immediately, commit the transaction first and then try and do the allocation again. This patch also does chunk allocation for metadata if we pass the 80% threshold for metadata space. This will help with stack usage since the chunk allocation will happen early on, instead of when the allocation is happening. Signed-off-by: Josef Bacik <jbacik@redhat.com>
1 parent 6a63209 commit 4e06bdd

1 file changed

Lines changed: 47 additions & 10 deletions

File tree

‎fs/btrfs/extent-tree.c‎

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2017,26 +2017,49 @@ int btrfs_check_metadata_free_space(struct btrfs_root *root)
20172017
struct btrfs_fs_info *info = root->fs_info;
20182018
struct btrfs_space_info *meta_sinfo;
20192019
u64 alloc_target, thresh;
2020+
int committed = 0, ret;
20202021

20212022
/* get the space info for where the metadata will live */
20222023
alloc_target = btrfs_get_alloc_profile(root, 0);
20232024
meta_sinfo = __find_space_info(info, alloc_target);
20242025

2025-
/*
2026-
* if the metadata area isn't maxed out then there is no sense in
2027-
* checking how much is used, since we can always allocate a new chunk
2028-
*/
2029-
if (!meta_sinfo->full)
2030-
return 0;
2031-
2026+
again:
20322027
spin_lock(&meta_sinfo->lock);
2033-
thresh = meta_sinfo->total_bytes * 95;
2028+
if (!meta_sinfo->full)
2029+
thresh = meta_sinfo->total_bytes * 80;
2030+
else
2031+
thresh = meta_sinfo->total_bytes * 95;
20342032

20352033
do_div(thresh, 100);
20362034

20372035
if (meta_sinfo->bytes_used + meta_sinfo->bytes_reserved +
20382036
meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly > thresh) {
2037+
struct btrfs_trans_handle *trans;
2038+
if (!meta_sinfo->full) {
2039+
meta_sinfo->force_alloc = 1;
2040+
spin_unlock(&meta_sinfo->lock);
2041+
2042+
trans = btrfs_start_transaction(root, 1);
2043+
if (!trans)
2044+
return -ENOMEM;
2045+
2046+
ret = do_chunk_alloc(trans, root->fs_info->extent_root,
2047+
2 * 1024 * 1024, alloc_target, 0);
2048+
btrfs_end_transaction(trans, root);
2049+
goto again;
2050+
}
20392051
spin_unlock(&meta_sinfo->lock);
2052+
2053+
if (!committed) {
2054+
committed = 1;
2055+
trans = btrfs_join_transaction(root, 1);
2056+
if (!trans)
2057+
return -ENOMEM;
2058+
ret = btrfs_commit_transaction(trans, root);
2059+
if (ret)
2060+
return ret;
2061+
goto again;
2062+
}
20402063
return -ENOSPC;
20412064
}
20422065
spin_unlock(&meta_sinfo->lock);
@@ -2052,7 +2075,7 @@ int btrfs_check_data_free_space(struct btrfs_root *root, struct inode *inode,
20522075
u64 bytes)
20532076
{
20542077
struct btrfs_space_info *data_sinfo;
2055-
int ret = 0;
2078+
int ret = 0, committed = 0;
20562079

20572080
/* make sure bytes are sectorsize aligned */
20582081
bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
@@ -2065,13 +2088,14 @@ int btrfs_check_data_free_space(struct btrfs_root *root, struct inode *inode,
20652088
data_sinfo->bytes_delalloc - data_sinfo->bytes_reserved -
20662089
data_sinfo->bytes_pinned - data_sinfo->bytes_readonly -
20672090
data_sinfo->bytes_may_use < bytes) {
2091+
struct btrfs_trans_handle *trans;
2092+
20682093
/*
20692094
* if we don't have enough free bytes in this space then we need
20702095
* to alloc a new chunk.
20712096
*/
20722097
if (!data_sinfo->full) {
20732098
u64 alloc_target;
2074-
struct btrfs_trans_handle *trans;
20752099

20762100
data_sinfo->force_alloc = 1;
20772101
spin_unlock(&data_sinfo->lock);
@@ -2090,6 +2114,19 @@ int btrfs_check_data_free_space(struct btrfs_root *root, struct inode *inode,
20902114
goto again;
20912115
}
20922116
spin_unlock(&data_sinfo->lock);
2117+
2118+
/* commit the current transaction and try again */
2119+
if (!committed) {
2120+
committed = 1;
2121+
trans = btrfs_join_transaction(root, 1);
2122+
if (!trans)
2123+
return -ENOMEM;
2124+
ret = btrfs_commit_transaction(trans, root);
2125+
if (ret)
2126+
return ret;
2127+
goto again;
2128+
}
2129+
20932130
printk(KERN_ERR "no space left, need %llu, %llu delalloc bytes"
20942131
", %llu bytes_used, %llu bytes_reserved, "
20952132
"%llu bytes_pinned, %llu bytes_readonly, %llu may use"

0 commit comments

Comments
 (0)