linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Su Yue <l@damenly.su>
To: Boris Burkov <boris@bur.io>
Cc: 20210419124541.148269-1-l@damenly.su,
	linux-btrfs@vger.kernel.org,
	Chris Murphy <lists@colorremedies.com>
Subject: Re: [PATCH v2] btrfs-progs: fi resize: fix false 0.00B sized output
Date: Tue, 20 Apr 2021 12:20:44 +0800	[thread overview]
Message-ID: <8s5da8cy.fsf@damenly.su> (raw)
In-Reply-To: <YH25FlnQ4nHg57bm@zen>


On Tue 20 Apr 2021 at 01:08, Boris Burkov <boris@bur.io> wrote:

> On Mon, Apr 19, 2021 at 09:05:49PM +0800, Su Yue wrote:
>> Resize to nums without sign prefix makes false output:
>>  btrfs fi resize 1:150g /srv/extra
>> Resize device id 1 (/dev/sdb1) from 298.09GiB to 0.00B
>>
>> The resize operation would take effect though.
>>
>> Fix it by handling the case if mod is 0 in check_resize_args().
>>
>> Issue: #307
>> Reported-by: Chris Murphy <lists@colorremedies.com>
>> Signed-off-by: Su Yue <l@damenly.su>
>> ---
>>  cmds/filesystem.c | 10 ++++++++++
>>  1 file changed, 10 insertions(+)
>>
>> ---
>> Changelog:
>>     v2:
>>         Calculate u64 diff using max() and min().
>>         Calculate mod by comparing new and old size.
>> diff --git a/cmds/filesystem.c b/cmds/filesystem.c
>> index 9e3cce687d6e..54d46470c53f 100644
>> --- a/cmds/filesystem.c
>> +++ b/cmds/filesystem.c
>> @@ -1158,6 +1158,16 @@ static int check_resize_args(const char 
>> *amount, const char *path) {
>>  		}
>>  		old_size = di_args[dev_idx].total_bytes;
>>
>> +		/* For target sizes without '+'/'-' sign prefix(e.g. 
>> 1:150g) */
>> +		if (mod == 0) {
>> +			new_size = diff;
>> +			diff = max(old_size, new_size) - min(old_size, 
>> new_size);
>> +			if (new_size > old_size)
>> +				mod = 1;
>> +			else if (new_size < old_size)
>> +				mod = -1;
>> +		}
>> +
>>  		if (mod < 0) {
>>  			if (diff > old_size) {
>>  				error("current size is %s which is smaller 
>>  than %s",
>
> This fix seems correct to me, but it feels a tiny bit 
> over-complicated.
> Personally, I think it would be cleaner to do something like:
>
> if (mod == 0) {
> 	new_size = diff;
> } else if (mod < 0) {
> 	// >0 check
> 	new_size = old_size - diff
> } else {
> 	// overflow check
> 	new_size = old_size + diff
> }
>
> At this point, new_size is set correctly in all cases and we can 
> do the
> print. I tested this version on some simple cases, and it seems 
> to work
> ok as well.
>
Right. The first fix when I saw the code was same with yours.
Now I relaized that I was over thinking about boundary checks and
falling through checks are meaningless if mod is 0.
Just to clarify that my fix is wrong:

       if (mod == 0) {
            new_size = diff;
           diff = max(old_size, new_size) - min(old_size, 
           new_size);
           if (new_size > old_size)
               mod = 1;
           else if (new_size < old_size)
               mod = -1;
       } /* falls through to check diff */

      if (mod < 0) {  // new_size < old_size, diff = old_size - 
      new_size
                     // so diff < new_size
           if (diff > old_size) // can't happen, if mod is 0 
           before.
                  ...
      } else if (mod > 0) {
        // new_size > old_size so diff = new_size(u64) - old_size,
           if (diff > ULLONG_MAX - old_size) // can't happen even 
           new_size is
                                             // ULLONG_MAX if mod 
                                             is 0 before
              ...
      }


Thanks for your suggestion, will send v3.
--
Su

> Thanks for the fix,
> Boris
>
>> --
>> 2.30.1
>>

      parent reply	other threads:[~2021-04-20  4:47 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-19 13:05 [PATCH v2] btrfs-progs: fi resize: fix false 0.00B sized output Su Yue
2021-04-19 17:03 ` David Sterba
2021-04-19 17:08 ` Boris Burkov
2021-04-19 17:28   ` David Sterba
2021-04-20  4:20   ` Su Yue [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8s5da8cy.fsf@damenly.su \
    --to=l@damenly.su \
    --cc=20210419124541.148269-1-l@damenly.su \
    --cc=boris@bur.io \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=lists@colorremedies.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).