Commit d1b8b94a authored by Qu Wenruo's avatar Qu Wenruo Committed by David Sterba

btrfs: qgroup: Cleanup btrfs_qgroup_prepare_account_extents function

Quite a lot of qgroup corruption happens due to wrong time of calling
btrfs_qgroup_prepare_account_extents().

Since the safest time is to call it just before
btrfs_qgroup_account_extents(), there is no need to separate these 2
functions.

Merging them will make code cleaner and less bug prone.
Signed-off-by: default avatarQu Wenruo <quwenruo@cn.fujitsu.com>
[ changelog and comment adjustments ]
Signed-off-by: default avatarDavid Sterba <dsterba@suse.com>
parent 5edfd9fd
...@@ -1406,38 +1406,6 @@ int btrfs_limit_qgroup(struct btrfs_trans_handle *trans, ...@@ -1406,38 +1406,6 @@ int btrfs_limit_qgroup(struct btrfs_trans_handle *trans,
return ret; return ret;
} }
int btrfs_qgroup_prepare_account_extents(struct btrfs_trans_handle *trans,
struct btrfs_fs_info *fs_info)
{
struct btrfs_qgroup_extent_record *record;
struct btrfs_delayed_ref_root *delayed_refs;
struct rb_node *node;
u64 qgroup_to_skip;
int ret = 0;
delayed_refs = &trans->transaction->delayed_refs;
qgroup_to_skip = delayed_refs->qgroup_to_skip;
/*
* No need to do lock, since this function will only be called in
* btrfs_commit_transaction().
*/
node = rb_first(&delayed_refs->dirty_extent_root);
while (node) {
record = rb_entry(node, struct btrfs_qgroup_extent_record,
node);
if (WARN_ON(!record->old_roots))
ret = btrfs_find_all_roots(NULL, fs_info,
record->bytenr, 0, &record->old_roots);
if (ret < 0)
break;
if (qgroup_to_skip)
ulist_del(record->old_roots, qgroup_to_skip, 0);
node = rb_next(node);
}
return ret;
}
int btrfs_qgroup_trace_extent_nolock(struct btrfs_fs_info *fs_info, int btrfs_qgroup_trace_extent_nolock(struct btrfs_fs_info *fs_info,
struct btrfs_delayed_ref_root *delayed_refs, struct btrfs_delayed_ref_root *delayed_refs,
struct btrfs_qgroup_extent_record *record) struct btrfs_qgroup_extent_record *record)
...@@ -2056,6 +2024,19 @@ int btrfs_qgroup_account_extents(struct btrfs_trans_handle *trans, ...@@ -2056,6 +2024,19 @@ int btrfs_qgroup_account_extents(struct btrfs_trans_handle *trans,
trace_btrfs_qgroup_account_extents(fs_info, record); trace_btrfs_qgroup_account_extents(fs_info, record);
if (!ret) { if (!ret) {
/*
* Old roots should be searched when inserting qgroup
* extent record
*/
if (WARN_ON(!record->old_roots)) {
/* Search commit root to find old_roots */
ret = btrfs_find_all_roots(NULL, fs_info,
record->bytenr, 0,
&record->old_roots);
if (ret < 0)
goto cleanup;
}
/* /*
* Use SEQ_LAST as time_seq to do special search, which * Use SEQ_LAST as time_seq to do special search, which
* doesn't lock tree or delayed_refs and search current * doesn't lock tree or delayed_refs and search current
...@@ -2065,8 +2046,11 @@ int btrfs_qgroup_account_extents(struct btrfs_trans_handle *trans, ...@@ -2065,8 +2046,11 @@ int btrfs_qgroup_account_extents(struct btrfs_trans_handle *trans,
record->bytenr, SEQ_LAST, &new_roots); record->bytenr, SEQ_LAST, &new_roots);
if (ret < 0) if (ret < 0)
goto cleanup; goto cleanup;
if (qgroup_to_skip) if (qgroup_to_skip) {
ulist_del(new_roots, qgroup_to_skip, 0); ulist_del(new_roots, qgroup_to_skip, 0);
ulist_del(record->old_roots, qgroup_to_skip,
0);
}
ret = btrfs_qgroup_account_extent(trans, fs_info, ret = btrfs_qgroup_account_extent(trans, fs_info,
record->bytenr, record->num_bytes, record->bytenr, record->num_bytes,
record->old_roots, new_roots); record->old_roots, new_roots);
......
...@@ -134,8 +134,7 @@ int btrfs_limit_qgroup(struct btrfs_trans_handle *trans, ...@@ -134,8 +134,7 @@ int btrfs_limit_qgroup(struct btrfs_trans_handle *trans,
int btrfs_read_qgroup_config(struct btrfs_fs_info *fs_info); int btrfs_read_qgroup_config(struct btrfs_fs_info *fs_info);
void btrfs_free_qgroup_config(struct btrfs_fs_info *fs_info); void btrfs_free_qgroup_config(struct btrfs_fs_info *fs_info);
struct btrfs_delayed_extent_op; struct btrfs_delayed_extent_op;
int btrfs_qgroup_prepare_account_extents(struct btrfs_trans_handle *trans,
struct btrfs_fs_info *fs_info);
/* /*
* Inform qgroup to trace one dirty extent, its info is recorded in @record. * Inform qgroup to trace one dirty extent, its info is recorded in @record.
* So qgroup can account it at transaction committing time. * So qgroup can account it at transaction committing time.
......
...@@ -1374,9 +1374,6 @@ static int qgroup_account_snapshot(struct btrfs_trans_handle *trans, ...@@ -1374,9 +1374,6 @@ static int qgroup_account_snapshot(struct btrfs_trans_handle *trans,
ret = commit_fs_roots(trans, fs_info); ret = commit_fs_roots(trans, fs_info);
if (ret) if (ret)
goto out; goto out;
ret = btrfs_qgroup_prepare_account_extents(trans, fs_info);
if (ret < 0)
goto out;
ret = btrfs_qgroup_account_extents(trans, fs_info); ret = btrfs_qgroup_account_extents(trans, fs_info);
if (ret < 0) if (ret < 0)
goto out; goto out;
...@@ -2180,13 +2177,6 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans) ...@@ -2180,13 +2177,6 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans)
goto scrub_continue; goto scrub_continue;
} }
ret = btrfs_qgroup_prepare_account_extents(trans, fs_info);
if (ret) {
mutex_unlock(&fs_info->tree_log_mutex);
mutex_unlock(&fs_info->reloc_mutex);
goto scrub_continue;
}
/* /*
* Since fs roots are all committed, we can get a quite accurate * Since fs roots are all committed, we can get a quite accurate
* new_roots. So let's do quota accounting. * new_roots. So let's do quota accounting.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment