Skip to contentSkip to conversation

btrfs-progs: fi resize: fix false 0.00B sized output

PATCH Superseded Su Yue 3 participants 5 messages Apr 19, 2021 – Apr 20, 2021 Btrfs file system

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Su Yue

author
#0 lore raw

PATCH v2 1/1 btrfs-progs: fi resize: fix false 0.00B sized output

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

---
 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.

cmds/filesystem.c +10 -0
index 9e3cce687d6e..54d46470c53f 100644
@@ -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",
Signature · 1 line
2.30.1
#1 lore raw
Quoted 11 lines · Su Yue

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

Thanks, added to devel.

#2 lore raw
In reply to Su Yue
Quoted 31 lines · Su Yue

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

---
 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.

cmds/filesystem.c +3 -0
index 9e3cce687d6e..54d46470c53f 100644
@@ -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;

cmds/filesystem.c static int check_resize_args(const char *amount, const char *path) { · line 1173

Quoted 10 lines
cmds/filesystem.c +7 -0
+			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.

Thanks for the fix,
Boris

Quoted 3 lines
Signature · 1 line
2.30.1
#3 lore raw
Quoted 22 lines · Boris Burkov
Quoted 7 lines · Su Yue
cmds/filesystem.c +3 -0
@@ -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;

cmds/filesystem.c static int check_resize_args(const char *amount, const char *path) { · line 1173

Quoted 10 lines
cmds/filesystem.c +7 -0
+			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:

Quoted 9 lines
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
}

Right, this looks much better and shares a lot of with the code that follows the original fix.

author
#4 lore raw
In reply to Boris Burkov
Quoted 60 lines · Boris Burkov
Quoted 35 lines · Su Yue

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

---
 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.

cmds/filesystem.c +1 -0
index 9e3cce687d6e..54d46470c53f 100644
@@ -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) */

cmds/filesystem.c +3 -0
+		if (mod == 0) {
+			new_size = diff;
+			diff = max(old_size, new_size) - min(old_size, 

new_size);

cmds/filesystem.c static int check_resize_args(const char *amount, const char *path) {

Quoted 10 lines
cmds/filesystem.c +6 -0
+			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
}
Quoted 4 lines

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

Quoted 6 lines

Thanks for the fix,
Boris

Quoted 3 lines

--
2.30.1