Skip to content

Commit 96c3f43

Browse files
Josef BacikChris Mason
authored andcommitted
Btrfs: flush delayed inodes if we're short on space
Those crazy gentoo guys have been complaining about ENOSPC errors on their portage volumes. This is because doing things like untar tends to create lots of new files which will soak up all the reservation space in the delayed inodes. Usually this gets papered over by the fact that we will try and commit the transaction, however if this happens in the wrong spot or we choose not to commit the transaction you will be screwed. So add the ability to expclitly flush delayed inodes to free up space. Please test this out guys to make sure it works since as usual I cannot reproduce. Thanks, Signed-off-by: Josef Bacik <jbacik@fusionio.com>
1 parent b27f7c0 commit 96c3f43

3 files changed

Lines changed: 83 additions & 38 deletions

File tree

‎fs/btrfs/delayed-inode.c‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,15 +1113,16 @@ static int btrfs_update_delayed_inode(struct btrfs_trans_handle *trans,
11131113
* Returns < 0 on error and returns with an aborted transaction with any
11141114
* outstanding delayed items cleaned up.
11151115
*/
1116-
int btrfs_run_delayed_items(struct btrfs_trans_handle *trans,
1117-
struct btrfs_root *root)
1116+
static int __btrfs_run_delayed_items(struct btrfs_trans_handle *trans,
1117+
struct btrfs_root *root, int nr)
11181118
{
11191119
struct btrfs_root *curr_root = root;
11201120
struct btrfs_delayed_root *delayed_root;
11211121
struct btrfs_delayed_node *curr_node, *prev_node;
11221122
struct btrfs_path *path;
11231123
struct btrfs_block_rsv *block_rsv;
11241124
int ret = 0;
1125+
bool count = (nr > 0);
11251126

11261127
if (trans->aborted)
11271128
return -EIO;
@@ -1137,7 +1138,7 @@ int btrfs_run_delayed_items(struct btrfs_trans_handle *trans,
11371138
delayed_root = btrfs_get_delayed_root(root);
11381139

11391140
curr_node = btrfs_first_delayed_node(delayed_root);
1140-
while (curr_node) {
1141+
while (curr_node && (!count || (count && nr--))) {
11411142
curr_root = curr_node->root;
11421143
ret = btrfs_insert_delayed_items(trans, path, curr_root,
11431144
curr_node);
@@ -1149,6 +1150,7 @@ int btrfs_run_delayed_items(struct btrfs_trans_handle *trans,
11491150
path, curr_node);
11501151
if (ret) {
11511152
btrfs_release_delayed_node(curr_node);
1153+
curr_node = NULL;
11521154
btrfs_abort_transaction(trans, root, ret);
11531155
break;
11541156
}
@@ -1158,12 +1160,26 @@ int btrfs_run_delayed_items(struct btrfs_trans_handle *trans,
11581160
btrfs_release_delayed_node(prev_node);
11591161
}
11601162

1163+
if (curr_node)
1164+
btrfs_release_delayed_node(curr_node);
11611165
btrfs_free_path(path);
11621166
trans->block_rsv = block_rsv;
11631167

11641168
return ret;
11651169
}
11661170

1171+
int btrfs_run_delayed_items(struct btrfs_trans_handle *trans,
1172+
struct btrfs_root *root)
1173+
{
1174+
return __btrfs_run_delayed_items(trans, root, -1);
1175+
}
1176+
1177+
int btrfs_run_delayed_items_nr(struct btrfs_trans_handle *trans,
1178+
struct btrfs_root *root, int nr)
1179+
{
1180+
return __btrfs_run_delayed_items(trans, root, nr);
1181+
}
1182+
11671183
static int __btrfs_commit_inode_delayed_items(struct btrfs_trans_handle *trans,
11681184
struct btrfs_delayed_node *node)
11691185
{

‎fs/btrfs/delayed-inode.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ int btrfs_inode_delayed_dir_index_count(struct inode *inode);
107107

108108
int btrfs_run_delayed_items(struct btrfs_trans_handle *trans,
109109
struct btrfs_root *root);
110+
int btrfs_run_delayed_items_nr(struct btrfs_trans_handle *trans,
111+
struct btrfs_root *root, int nr);
110112

111113
void btrfs_balance_delayed_items(struct btrfs_root *root);
112114

‎fs/btrfs/extent-tree.c‎

Lines changed: 62 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3728,6 +3728,60 @@ static int may_commit_transaction(struct btrfs_root *root,
37283728
return btrfs_commit_transaction(trans, root);
37293729
}
37303730

3731+
enum flush_state {
3732+
FLUSH_DELALLOC = 1,
3733+
FLUSH_DELALLOC_WAIT = 2,
3734+
FLUSH_DELAYED_ITEMS_NR = 3,
3735+
FLUSH_DELAYED_ITEMS = 4,
3736+
COMMIT_TRANS = 5,
3737+
};
3738+
3739+
static int flush_space(struct btrfs_root *root,
3740+
struct btrfs_space_info *space_info, u64 num_bytes,
3741+
u64 orig_bytes, int state)
3742+
{
3743+
struct btrfs_trans_handle *trans;
3744+
int nr;
3745+
int ret;
3746+
3747+
switch (state) {
3748+
case FLUSH_DELALLOC:
3749+
case FLUSH_DELALLOC_WAIT:
3750+
ret = shrink_delalloc(root, num_bytes,
3751+
state == FLUSH_DELALLOC_WAIT);
3752+
if (ret > 0)
3753+
ret = 0;
3754+
break;
3755+
case FLUSH_DELAYED_ITEMS_NR:
3756+
case FLUSH_DELAYED_ITEMS:
3757+
if (state == FLUSH_DELAYED_ITEMS_NR) {
3758+
u64 bytes = btrfs_calc_trans_metadata_size(root, 1);
3759+
3760+
nr = (int)div64_u64(num_bytes, bytes);
3761+
if (!nr)
3762+
nr = 1;
3763+
nr *= 2;
3764+
} else {
3765+
nr = -1;
3766+
}
3767+
trans = btrfs_join_transaction(root);
3768+
if (IS_ERR(trans)) {
3769+
ret = PTR_ERR(trans);
3770+
break;
3771+
}
3772+
ret = btrfs_run_delayed_items_nr(trans, root, nr);
3773+
btrfs_end_transaction(trans, root);
3774+
break;
3775+
case COMMIT_TRANS:
3776+
ret = may_commit_transaction(root, space_info, orig_bytes, 0);
3777+
break;
3778+
default:
3779+
ret = -ENOSPC;
3780+
break;
3781+
}
3782+
3783+
return ret;
3784+
}
37313785
/**
37323786
* reserve_metadata_bytes - try to reserve bytes from the block_rsv's space
37333787
* @root - the root we're allocating for
@@ -3749,11 +3803,10 @@ static int reserve_metadata_bytes(struct btrfs_root *root,
37493803
struct btrfs_space_info *space_info = block_rsv->space_info;
37503804
u64 used;
37513805
u64 num_bytes = orig_bytes;
3752-
int retries = 0;
3806+
int flush_state = FLUSH_DELALLOC;
37533807
int ret = 0;
3754-
bool committed = false;
37553808
bool flushing = false;
3756-
bool wait_ordered = false;
3809+
bool committed = false;
37573810

37583811
again:
37593812
ret = 0;
@@ -3812,9 +3865,8 @@ static int reserve_metadata_bytes(struct btrfs_root *root,
38123865
* amount plus the amount of bytes that we need for this
38133866
* reservation.
38143867
*/
3815-
wait_ordered = true;
38163868
num_bytes = used - space_info->total_bytes +
3817-
(orig_bytes * (retries + 1));
3869+
(orig_bytes * 2);
38183870
}
38193871

38203872
if (ret) {
@@ -3867,8 +3919,6 @@ static int reserve_metadata_bytes(struct btrfs_root *root,
38673919
trace_btrfs_space_reservation(root->fs_info,
38683920
"space_info", space_info->flags, orig_bytes, 1);
38693921
ret = 0;
3870-
} else {
3871-
wait_ordered = true;
38723922
}
38733923
}
38743924

@@ -3887,36 +3937,13 @@ static int reserve_metadata_bytes(struct btrfs_root *root,
38873937
if (!ret || !flush)
38883938
goto out;
38893939

3890-
/*
3891-
* We do synchronous shrinking since we don't actually unreserve
3892-
* metadata until after the IO is completed.
3893-
*/
3894-
ret = shrink_delalloc(root, num_bytes, wait_ordered);
3895-
if (ret < 0)
3896-
goto out;
3897-
3898-
ret = 0;
3899-
3900-
/*
3901-
* So if we were overcommitted it's possible that somebody else flushed
3902-
* out enough space and we simply didn't have enough space to reclaim,
3903-
* so go back around and try again.
3904-
*/
3905-
if (retries < 2) {
3906-
wait_ordered = true;
3907-
retries++;
3940+
ret = flush_space(root, space_info, num_bytes, orig_bytes,
3941+
flush_state);
3942+
flush_state++;
3943+
if (!ret)
39083944
goto again;
3909-
}
3910-
3911-
ret = -ENOSPC;
3912-
if (committed)
3913-
goto out;
3914-
3915-
ret = may_commit_transaction(root, space_info, orig_bytes, 0);
3916-
if (!ret) {
3917-
committed = true;
3945+
else if (flush_state <= COMMIT_TRANS)
39183946
goto again;
3919-
}
39203947

39213948
out:
39223949
if (flushing) {

0 commit comments

Comments
 (0)