linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] Btrfs: always copy scrub arguments back to user space
@ 2020-01-16 11:29 fdmanana
  2020-01-16 11:59 ` Johannes Thumshirn
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: fdmanana @ 2020-01-16 11:29 UTC (permalink / raw)
  To: linux-btrfs

From: Filipe Manana <fdmanana@suse.com>

If scrub returns an error we are not copying back the scrub arguments
structure to user space. This prevents user space to know how much progress
scrub has done if an error happened - this includes -ECANCELED which is
returned when users ask for scrub to stop. A particular use case, which is
used in btrfs-progs, is to resume scrub after it is canceled, in that case
it relies on checking the progress from the scrub arguments structure and
then use that progress in a call to resume scrub.

So fix this by always copying the scrub arguments structure to user space,
overwriting the value returned to user space with -EFAULT only if copying
the structure failed to let user space know that either that copying did
not happen, and therefore the structure is stale, or it happened partially
and the structure is probably not valid and corrupt due to the partial
copy.

Reported-by: Graham Cobb <g.btrfs@cobb.uk.net>
Link: https://lore.kernel.org/linux-btrfs/d0a97688-78be-08de-ca7d-bcb4c7fb397e@cobb.uk.net/
Fixes: 06fe39ab15a6a4 ("Btrfs: do not overwrite scrub error with fault error in scrub ioctl")
Signed-off-by: Filipe Manana <fdmanana@suse.com>
---
 fs/btrfs/ioctl.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c
index 3a4bd5cd67fa..173758d86feb 100644
--- a/fs/btrfs/ioctl.c
+++ b/fs/btrfs/ioctl.c
@@ -4253,7 +4253,19 @@ static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
 			      &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
 			      0);
 
-	if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
+	/*
+	 * Copy scrub args to user space even if btrfs_scrub_dev() returned an
+	 * error. This is important as it allows user space to know how much
+	 * progress scrub has done. For example, if scrub is canceled we get
+	 * -ECANCELED from btrfs_scrub_dev() and return that error back to user
+	 * space. Later user space can inspect the progress from the structure
+	 * btrfs_ioctl_scrub_args and resume scrub from where it left off
+	 * previously (btrfs-progs does this).
+	 * If we fail to copy the btrfs_ioctl_scrub_args structure to user space
+	 * then return -EFAULT to signal the structure was not copied or it may
+	 * be corrupt and unreliable due to a partial copy.
+	 */
+	if (copy_to_user(arg, sa, sizeof(*sa)))
 		ret = -EFAULT;
 
 	if (!(sa->flags & BTRFS_SCRUB_READONLY))
-- 
2.11.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] Btrfs: always copy scrub arguments back to user space
  2020-01-16 11:29 [PATCH] Btrfs: always copy scrub arguments back to user space fdmanana
@ 2020-01-16 11:59 ` Johannes Thumshirn
  2020-01-16 12:00 ` Qu Wenruo
  2020-01-16 14:12 ` David Sterba
  2 siblings, 0 replies; 5+ messages in thread
From: Johannes Thumshirn @ 2020-01-16 11:59 UTC (permalink / raw)
  To: fdmanana, linux-btrfs

Looks good,
Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
    
    


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] Btrfs: always copy scrub arguments back to user space
  2020-01-16 11:29 [PATCH] Btrfs: always copy scrub arguments back to user space fdmanana
  2020-01-16 11:59 ` Johannes Thumshirn
@ 2020-01-16 12:00 ` Qu Wenruo
  2020-01-16 14:12 ` David Sterba
  2 siblings, 0 replies; 5+ messages in thread
From: Qu Wenruo @ 2020-01-16 12:00 UTC (permalink / raw)
  To: fdmanana, linux-btrfs


[-- Attachment #1.1: Type: text/plain, Size: 2698 bytes --]



On 2020/1/16 下午7:29, fdmanana@kernel.org wrote:
> From: Filipe Manana <fdmanana@suse.com>
> 
> If scrub returns an error we are not copying back the scrub arguments
> structure to user space. This prevents user space to know how much progress
> scrub has done if an error happened - this includes -ECANCELED which is
> returned when users ask for scrub to stop. A particular use case, which is
> used in btrfs-progs, is to resume scrub after it is canceled, in that case
> it relies on checking the progress from the scrub arguments structure and
> then use that progress in a call to resume scrub.
> 
> So fix this by always copying the scrub arguments structure to user space,
> overwriting the value returned to user space with -EFAULT only if copying
> the structure failed to let user space know that either that copying did
> not happen, and therefore the structure is stale, or it happened partially
> and the structure is probably not valid and corrupt due to the partial
> copy.
> 
> Reported-by: Graham Cobb <g.btrfs@cobb.uk.net>
> Link: https://lore.kernel.org/linux-btrfs/d0a97688-78be-08de-ca7d-bcb4c7fb397e@cobb.uk.net/
> Fixes: 06fe39ab15a6a4 ("Btrfs: do not overwrite scrub error with fault error in scrub ioctl")
> Signed-off-by: Filipe Manana <fdmanana@suse.com>

Reviewed-by: Qu Wenruo <wqu@suse.com>

Thanks,
Qu

> ---
>  fs/btrfs/ioctl.c | 14 +++++++++++++-
>  1 file changed, 13 insertions(+), 1 deletion(-)
> 
> diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c
> index 3a4bd5cd67fa..173758d86feb 100644
> --- a/fs/btrfs/ioctl.c
> +++ b/fs/btrfs/ioctl.c
> @@ -4253,7 +4253,19 @@ static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
>  			      &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
>  			      0);
>  
> -	if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
> +	/*
> +	 * Copy scrub args to user space even if btrfs_scrub_dev() returned an
> +	 * error. This is important as it allows user space to know how much
> +	 * progress scrub has done. For example, if scrub is canceled we get
> +	 * -ECANCELED from btrfs_scrub_dev() and return that error back to user
> +	 * space. Later user space can inspect the progress from the structure
> +	 * btrfs_ioctl_scrub_args and resume scrub from where it left off
> +	 * previously (btrfs-progs does this).
> +	 * If we fail to copy the btrfs_ioctl_scrub_args structure to user space
> +	 * then return -EFAULT to signal the structure was not copied or it may
> +	 * be corrupt and unreliable due to a partial copy.
> +	 */
> +	if (copy_to_user(arg, sa, sizeof(*sa)))
>  		ret = -EFAULT;
>  
>  	if (!(sa->flags & BTRFS_SCRUB_READONLY))
> 


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] Btrfs: always copy scrub arguments back to user space
  2020-01-16 11:29 [PATCH] Btrfs: always copy scrub arguments back to user space fdmanana
  2020-01-16 11:59 ` Johannes Thumshirn
  2020-01-16 12:00 ` Qu Wenruo
@ 2020-01-16 14:12 ` David Sterba
  2020-01-17 14:24   ` Graham Cobb
  2 siblings, 1 reply; 5+ messages in thread
From: David Sterba @ 2020-01-16 14:12 UTC (permalink / raw)
  To: fdmanana; +Cc: linux-btrfs

On Thu, Jan 16, 2020 at 11:29:20AM +0000, fdmanana@kernel.org wrote:
> From: Filipe Manana <fdmanana@suse.com>
> 
> If scrub returns an error we are not copying back the scrub arguments
> structure to user space. This prevents user space to know how much progress
> scrub has done if an error happened - this includes -ECANCELED which is
> returned when users ask for scrub to stop. A particular use case, which is
> used in btrfs-progs, is to resume scrub after it is canceled, in that case
> it relies on checking the progress from the scrub arguments structure and
> then use that progress in a call to resume scrub.
> 
> So fix this by always copying the scrub arguments structure to user space,
> overwriting the value returned to user space with -EFAULT only if copying
> the structure failed to let user space know that either that copying did
> not happen, and therefore the structure is stale, or it happened partially
> and the structure is probably not valid and corrupt due to the partial
> copy.
> 
> Reported-by: Graham Cobb <g.btrfs@cobb.uk.net>
> Link: https://lore.kernel.org/linux-btrfs/d0a97688-78be-08de-ca7d-bcb4c7fb397e@cobb.uk.net/
> Fixes: 06fe39ab15a6a4 ("Btrfs: do not overwrite scrub error with fault error in scrub ioctl")
> Signed-off-by: Filipe Manana <fdmanana@suse.com>

Added to 5.5-rc queue, thanks.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] Btrfs: always copy scrub arguments back to user space
  2020-01-16 14:12 ` David Sterba
@ 2020-01-17 14:24   ` Graham Cobb
  0 siblings, 0 replies; 5+ messages in thread
From: Graham Cobb @ 2020-01-17 14:24 UTC (permalink / raw)
  To: dsterba, fdmanana, linux-btrfs

On 16/01/2020 14:12, David Sterba wrote:
> On Thu, Jan 16, 2020 at 11:29:20AM +0000, fdmanana@kernel.org wrote:
>> From: Filipe Manana <fdmanana@suse.com>
>>
>> If scrub returns an error we are not copying back the scrub arguments
>> structure to user space. This prevents user space to know how much progress
>> scrub has done if an error happened - this includes -ECANCELED which is
>> returned when users ask for scrub to stop. A particular use case, which is
>> used in btrfs-progs, is to resume scrub after it is canceled, in that case
>> it relies on checking the progress from the scrub arguments structure and
>> then use that progress in a call to resume scrub.
>>
>> So fix this by always copying the scrub arguments structure to user space,
>> overwriting the value returned to user space with -EFAULT only if copying
>> the structure failed to let user space know that either that copying did
>> not happen, and therefore the structure is stale, or it happened partially
>> and the structure is probably not valid and corrupt due to the partial
>> copy.
>>
>> Reported-by: Graham Cobb <g.btrfs@cobb.uk.net>
>> Link: https://lore.kernel.org/linux-btrfs/d0a97688-78be-08de-ca7d-bcb4c7fb397e@cobb.uk.net/
>> Fixes: 06fe39ab15a6a4 ("Btrfs: do not overwrite scrub error with fault error in scrub ioctl")
>> Signed-off-by: Filipe Manana <fdmanana@suse.com>
> 
> Added to 5.5-rc queue, thanks.

Just to let you know... As promised I have built a Debian Testing kernel
(5.3) with this patch applied. My btrfs-scrub-slowly script has now run
successfully to completion with 32 cancel/resume cycles (runs for 30
mins then takes 10 min break).

If it is useful, feel free to add:

Tested-by: Graham Cobb <g.btrfs@cobb.uk.net>

Thanks Felipe!



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2020-01-17 14:24 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-01-16 11:29 [PATCH] Btrfs: always copy scrub arguments back to user space fdmanana
2020-01-16 11:59 ` Johannes Thumshirn
2020-01-16 12:00 ` Qu Wenruo
2020-01-16 14:12 ` David Sterba
2020-01-17 14:24   ` Graham Cobb

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).