btrfs: qgroup: rescan related message minor fixes
v3 cover btrfs: qgroup: rescan related message minor fixes
[CHANGELOG]
v3:
- Fix the @cancelled update
To handle the @stopped == false case better.
v2:
- Handle cancelled-after-finished case better
So cancelled is only update when the rescan is stopped. This means if the rescan is finished properly then cancelled before we commit everything, at least we still treat the rescan as finished.
This is pointed by sashiko, but I won't handle it any further, as it's a long existing cancel-rescan race. I don't think it's that important either.
- Reorder the error messages priority
Now the failure has the highest priority.
The first one is from Sashiko.
The second one is to make sure we output the rescan related message even if we failed to start a transaction (e.g. transaction aborted).
The final one is to fix a bug that rescan cancelled message is never shown.
But the final one is also prone to race between scan and cancel. I don't think there is any good way to really fix that yet nor is really caused by this patch.
So I'll mostly ignore future scan-cancel race related review from sashiko on the last patch.
Qu Wenruo (3):
btrfs: qgroup: handle the transaction commit error during rescan
btrfs: qgroup: ensure the qgroup rescan messages are always shown
btrfs: qgroup: show the "qgroup scan cancelled" message correctly
fs/btrfs/qgroup.c | 42 ++++++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 16 deletions(-)
Signature · 1 line
2.56.0
PATCH v3 1/3 btrfs: qgroup: handle the transaction commit error during rescan
After qgroup_rescan_leaf() return 1, meaning it has done the scanning, btrfs_qgroup_rescan_worker() will call btrfs_commit_transaction() to update the qgroup infos.
But if that btrfs_commit_transaction() failed, @ret is still 1, which will treat the rescan as ended successfully, clearing the INCONSISTENT flag.
Fix the error handling by also check the return value of btrfs_commit_transaction(). If that failed, update @ret to the error code so we treat the rescan as failed.
--- fs/btrfs/qgroup.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)
fs/btrfs/qgroup.c +8 -3
@@ -3928,10 +3928,15 @@ static void btrfs_qgroup_rescan_worker(struct btrfs_work *work) ret =qgroup_rescan_leaf (trans, path); did_leaf_rescans = true; -if (ret >0 ) -btrfs_commit_transaction (trans); -else +if (ret >0 ) { +int commit_ret; + + commit_ret =btrfs_commit_transaction (trans); +if (commit_ret <0 ) + ret = commit_ret; + }else {btrfs_end_transaction (trans); + } } out:
Signature · 1 line
2.56.0
Re: [PATCH v3 1/3] btrfs: qgroup: handle the transaction commit error during rescan
Quoted 2 lines · Qu Wenruo
After
qgroup_rescan_leaf()return 1, meaning it has done the scanning,
return -> returns
Quoted 8 lines
btrfs_qgroup_rescan_worker()will callbtrfs_commit_transaction()to update the qgroup infos.But if that
btrfs_commit_transaction()failed, @ret is still 1, which will treat the rescan as ended successfully, clearing the INCONSISTENT flag.Fix the error handling by also check the return value of
check -> checking
Looks good otherwise, so:
Thanks.
Quoted 36 lines
btrfs_commit_transaction(). If that failed, update @ret to the error code so we treat the rescan as failed.Signed-off-by Qu Wenruo <[email protected]>--- fs/btrfs/qgroup.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)fs/btrfs/qgroup.c +8
-4@@ -3928,10 +3928,15 @@ static void btrfs_qgroup_rescan_worker(struct btrfs_work *work) ret =qgroup_rescan_leaf (trans, path); did_leaf_rescans = true; -if (ret >0 ) -btrfs_commit_transaction (trans); -else +if (ret >0 ) { +int commit_ret; + + commit_ret =btrfs_commit_transaction (trans); +if (commit_ret <0 ) + ret = commit_ret; + }else {btrfs_end_transaction (trans); + } } out: --2.56.0
PATCH v3 2/3 btrfs: qgroup: ensure the qgroup rescan messages are always shown
In reply to Qu WenruoIn btrfs_qgroup_rescan_worker(), if by somehow the transaction is aborted halfway, later we can not start a new transaction.
This marks @trans variable always to be NULL, which later skips the qgroup scan related messages, this can be a little confusing for end users.
Fix it by only end the transaction if we have a valid trans handle, other than returning from btrfs_qgroup_rescan_worker().
--- fs/btrfs/qgroup.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
fs/btrfs/qgroup.c +2 -4
@@ -3986,10 +3986,8 @@ static void btrfs_qgroup_rescan_worker(struct btrfs_work *work)complete_all (&fs_info->qgroup_rescan_completion);mutex_unlock (&fs_info->qgroup_rescan_lock); -if (!trans) -return ; - -btrfs_end_transaction (trans); +if (trans) +btrfs_end_transaction (trans);if (stopped) {btrfs_info (fs_info,"qgroup scan paused" );
Signature · 1 line
2.56.0
Re: [PATCH v3 2/3] btrfs: qgroup: ensure the qgroup rescan messages are always shown
Quoted 2 lines · Qu Wenruo
In
btrfs_qgroup_rescan_worker(), if by somehow the transaction is
Delete "by".
Quoted 7 lines
aborted halfway, later we can not start a new transaction.
This marks @trans variable always to be NULL, which later skips the qgroup scan related messages, this can be a little confusing for end users.
Fix it by only end the transaction if we have a valid trans handle,
end -> ending
Thanks.
Quoted 28 lines
other than returning from
btrfs_qgroup_rescan_worker().Signed-off-by Qu Wenruo <[email protected]>--- fs/btrfs/qgroup.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)fs/btrfs/qgroup.c +2
-5@@ -3986,10 +3986,8 @@ static void btrfs_qgroup_rescan_worker(struct btrfs_work *work)complete_all (&fs_info->qgroup_rescan_completion);mutex_unlock (&fs_info->qgroup_rescan_lock); -if (!trans) -return ; - -btrfs_end_transaction (trans); +if (trans) +btrfs_end_transaction (trans);if (stopped) {btrfs_info (fs_info,"qgroup scan paused" ); --2.56.0
PATCH v3 3/3 btrfs: qgroup: show the "qgroup scan cancelled" message correctly
In reply to Qu WenruoThere are several reasons why we won't output the "qgroup scan cancelled" message:
- We always clear BTRFS_QGROUP_RUNTIME_BIT_CANCEL_RESCAN before the message check
So that test_bit() will never return true.
- @stopped has a higher priority
When a rescan is cancelled, it has a high chance to be caught by rescan_should_stop(), so in that case @stopped is also true.
Then when checking which message to output, @stopped has a higher priority, thus it shows the rescan as paused instead.
Fix the problem by:
- Save the previous CANCEL_RESCAN flag into @cancelled, and check that variable instead
And @cancelled is only updated if the rescan is stopped.
This is to handle finished-then-cancelled rescan a little better. This won't completely solve the cancel race with rescan though.
But the cancel-race is already there, not caused by this commit, and I don't think we should bother too much on the mostly cosmetic error
- Change the message priority so that cancelled has a higher priority The new priority list is:
- Error (ret < 0)
- Cancelled
- Stopped
- Finished
And we no longer go the complex "if () else if ()" way, instead each hit branch will return, so only one branch will hit.
--- fs/btrfs/qgroup.c | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-)
fs/btrfs/qgroup.c +16 -9
@@ -3900,6 +3900,7 @@ static void btrfs_qgroup_rescan_worker(struct btrfs_work *work)struct btrfs_path *path;struct btrfs_trans_handle *trans =NULL ;int ret =0 ; +bool cancelled = false;bool stopped = false;bool did_leaf_rescans = false;
@@ -3970,8 +3971,9 @@ static void btrfs_qgroup_rescan_worker(struct btrfs_work *work) }mutex_lock (&fs_info->qgroup_rescan_lock); -if (!stopped ||test_bit (BTRFS_QGROUP_RUNTIME_BIT_CANCEL_RESCAN , - &fs_info->qgroup_flags)) + cancelled = stopped &&test_bit (BTRFS_QGROUP_RUNTIME_BIT_CANCEL_RESCAN , + &fs_info->qgroup_flags); +if (!stopped || cancelled)clear_bit (BTRFS_QGROUP_STATUS_BIT_RESCAN , &fs_info->qgroup_flags);if (trans) {int ret2 =update_qgroup_status_item (trans);
@@ -3989,16 +3991,21 @@ static void btrfs_qgroup_rescan_worker(struct btrfs_work *work)if (trans)btrfs_end_transaction (trans); +if (ret <0 ) { +btrfs_err (fs_info,"qgroup scan failed with %pe" ,ERR_PTR (ret)); +return ; + } +if (cancelled) { +btrfs_info (fs_info,"qgroup scan cancelled" ); +return ; + }if (stopped) {btrfs_info (fs_info,"qgroup scan paused" ); - }else if (test_bit (BTRFS_QGROUP_RUNTIME_BIT_CANCEL_RESCAN , &fs_info->qgroup_flags)) { -btrfs_info (fs_info,"qgroup scan cancelled" ); - }else if (ret >=0 ) { -btrfs_info (fs_info,"qgroup scan completed%s" , - ret >0 ?" (inconsistency flag cleared)" :"" ); - }else { -btrfs_err (fs_info,"qgroup scan failed with %pe" ,ERR_PTR (ret)); +return ; } + +btrfs_info (fs_info,"qgroup scan completed%s" , + ret >0 ?" (inconsistency flag cleared)" :"" ); }/*
Signature · 1 line
2.56.0
Re: [PATCH v3 3/3] btrfs: qgroup: show the "qgroup scan cancelled" message correctly
Quoted 34 lines · Qu Wenruo
There are several reasons why we won't output the "qgroup scan cancelled" message:
- We always clear
BTRFS_QGROUP_RUNTIME_BIT_CANCEL_RESCANbefore the message check
So thattest_bit()will never return true.- @stopped has a higher priority
When a rescan is cancelled, it has a high chance to be caught byrescan_should_stop(), so in that case @stopped is also true.Then when checking which message to output, @stopped has a higher priority, thus it shows the rescan as paused instead.
Fix the problem by:
- Save the previous
CANCEL_RESCANflag into @cancelled, and check that variable instead
And @cancelled is only updated if the rescan is stopped.This is to handle finished-then-cancelled rescan a little better. This won't completely solve the cancel race with rescan though.
But the cancel-race is already there, not caused by this commit, and I don't think we should bother too much on the mostly cosmetic error
- Change the message priority so that cancelled has a higher priority The new priority list is:
- Error (ret < 0)
- Cancelled
- Stopped
- Finished
Quoted 4 lines
And we no longer go the complex "if () else if ()" way, instead each hit branch will return, so only one branch will hit.
Signed-off-by Qu Wenruo <[email protected]>
Thanks.
Quoted 61 lines
--- fs/btrfs/qgroup.c | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-)fs/btrfs/qgroup.c +16
-10@@ -3900,6 +3900,7 @@ static void btrfs_qgroup_rescan_worker(struct btrfs_work *work)struct btrfs_path *path;struct btrfs_trans_handle *trans =NULL ;int ret =0 ; +bool cancelled = false;bool stopped = false;bool did_leaf_rescans = false;@@ -3970,8 +3971,9 @@ static void btrfs_qgroup_rescan_worker(struct btrfs_work *work) }mutex_lock (&fs_info->qgroup_rescan_lock); -if (!stopped ||test_bit (BTRFS_QGROUP_RUNTIME_BIT_CANCEL_RESCAN , - &fs_info->qgroup_flags)) + cancelled = stopped &&test_bit (BTRFS_QGROUP_RUNTIME_BIT_CANCEL_RESCAN , + &fs_info->qgroup_flags); +if (!stopped || cancelled)clear_bit (BTRFS_QGROUP_STATUS_BIT_RESCAN , &fs_info->qgroup_flags);if (trans) {int ret2 =update_qgroup_status_item (trans);@@ -3989,16 +3991,21 @@ static void btrfs_qgroup_rescan_worker(struct btrfs_work *work)if (trans)btrfs_end_transaction (trans); +if (ret <0 ) { +btrfs_err (fs_info,"qgroup scan failed with %pe" ,ERR_PTR (ret)); +return ; + } +if (cancelled) { +btrfs_info (fs_info,"qgroup scan cancelled" ); +return ; + }if (stopped) {btrfs_info (fs_info,"qgroup scan paused" ); - }else if (test_bit (BTRFS_QGROUP_RUNTIME_BIT_CANCEL_RESCAN , &fs_info->qgroup_flags)) { -btrfs_info (fs_info,"qgroup scan cancelled" ); - }else if (ret >=0 ) { -btrfs_info (fs_info,"qgroup scan completed%s" , - ret >0 ?" (inconsistency flag cleared)" :"" ); - }else { -btrfs_err (fs_info,"qgroup scan failed with %pe" ,ERR_PTR (ret)); +return ; } + +btrfs_info (fs_info,"qgroup scan completed%s" , + ret >0 ?" (inconsistency flag cleared)" :"" ); }/* -- 2.56.0