Skip to contentSkip to conversation

generic/587: fix rounding error in quota/stat block comparison

PATCH Superseded Darrick J. Wong 2 participants 4 messages Mar 18, 2020 – Mar 19, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 generic/587: fix rounding error in quota/stat block comparison

From: Darrick J. Wong <[email protected]>

It turns out that repquota (which reports in units of 1k blocks) reports rounded up numbers when the fs blocksize is 512 bytes. However, xfs_io stat always reports block counts in units of 512 bytes. If the number of (512b) file blocks is not an even number, the "$3 / 2" expression will round down, causing the test to fail. Round up to the nearest 1k to match repquota's behavior.

Reported-by [email protected]
Fixes 6b04ed05456fc6c ("generic: test unwritten extent conversion extent mapping quota accounting")
---
 tests/generic/587 |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tests/generic/587 +2 -1
index 7b07d07d..2ffa367d 100755
@@ -57,7 +57,8 @@ check_quota_accounting()
 {
 	$XFS_IO_PROG -c stat $testfile > $tmp.out
 	cat $tmp.out >> $seqres.full
-	local stat_blocks=$(grep 'stat.blocks' $tmp.out | awk '{print $3 / 2}')
+	local stat_blocks=$(grep 'stat.blocks' $tmp.out | \
+		awk '{printf("%d\n", ($3 + 1) / 2);}')
 
 	_report_quota_blocks $SCRATCH_MNT > $tmp.out
 	cat $tmp.out >> $seqres.full
Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

It turns out that repquota (which reports in units of 1k blocks) reports rounded up numbers when the fs blocksize is 512 bytes. However, xfs_io stat always reports block counts in units of 512 bytes. If the number of (512b) file blocks is not an even number, the "$3 / 2" expression will round down, causing the test to fail. Round up to the nearest 1k to match repquota's behavior.

Quoted 4 lines
Reported-by [email protected]
Fixes 6b04ed05456fc6c ("generic: test unwritten extent conversion extent mapping quota accounting")

---

I've tested this patch. It's good to me. If we can add a simple comment before doing "($3 + 1)", to explain why we need a "blocks + 1" at here, that would be better for others read this code.

Quoted 18 lines
 tests/generic/587 |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tests/generic/587 +2 -1
index 7b07d07d..2ffa367d 100755
@@ -57,7 +57,8 @@ check_quota_accounting()
 {
 	$XFS_IO_PROG -c stat $testfile > $tmp.out
 	cat $tmp.out >> $seqres.full
-	local stat_blocks=$(grep 'stat.blocks' $tmp.out | awk '{print $3 / 2}')
+	local stat_blocks=$(grep 'stat.blocks' $tmp.out | \
+		awk '{printf("%d\n", ($3 + 1) / 2);}')
 
 	_report_quota_blocks $SCRATCH_MNT > $tmp.out
 	cat $tmp.out >> $seqres.full
#2 lore raw

PATCH v2 1/1 generic/587: fix rounding error in quota/stat block comparison

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

It turns out that repquota (which reports in units of 1k blocks) reports rounded up numbers when the fs blocksize is 512 bytes. However, xfs_io stat always reports block counts in units of 512 bytes. If the number of (512b) file blocks is not an even number, the "$3 / 2" expression will round down, causing the test to fail. Round up to the nearest 1k to match repquota's behavior.

Reported-by [email protected]
Fixes 6b04ed05456fc6c ("generic: test unwritten extent conversion extent mapping quota accounting")

---
v2: improve the comments to explain exactly what we're doing and why

---
 tests/generic/587 |   10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
tests/generic/587 +7 -3
index 7b07d07d..3e58a302 100755
@@ -51,13 +51,17 @@ ENDL
 
 # Make sure that the quota blocks accounting for qa_user on the scratch fs
 # matches the stat blocks counter for the only file on the scratch fs that
-# is owned by qa_user.  Note that stat reports in units of 512b blocks whereas
-# repquota reports in units of 1k blocks.
+# is owned by qa_user.
 check_quota_accounting()
 {
+	# repquota rounds the raw numbers up to the nearest 1k when reporting
+	# space usage.  xfs_io stat always reports space usage in 512b units,
+	# so use an awk script to round this number up to the nearest 1k, just
+	# like repquota does.
 	$XFS_IO_PROG -c stat $testfile > $tmp.out
 	cat $tmp.out >> $seqres.full
-	local stat_blocks=$(grep 'stat.blocks' $tmp.out | awk '{print $3 / 2}')
+	local stat_blocks=$(grep 'stat.blocks' $tmp.out | \
+		awk '{printf("%d\n", ($3 + 1) / 2);}')
 
 	_report_quota_blocks $SCRATCH_MNT > $tmp.out
 	cat $tmp.out >> $seqres.full
#3 lore raw
Quoted 25 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

It turns out that repquota (which reports in units of 1k blocks) reports rounded up numbers when the fs blocksize is 512 bytes. However, xfs_io stat always reports block counts in units of 512 bytes. If the number of (512b) file blocks is not an even number, the "$3 / 2" expression will round down, causing the test to fail. Round up to the nearest 1k to match repquota's behavior.

Reported-by [email protected]
Fixes 6b04ed05456fc6c ("generic: test unwritten extent conversion extent mapping quota accounting")

---
v2: improve the comments to explain exactly what we're doing and why

---
 tests/generic/587 |   10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
tests/generic/587 +0 -0
index 7b07d07d..3e58a302 100755
@@ -51,13 +51,17 @@ ENDL
 
 # Make sure that the quota blocks accounting for qa_user on the scratch fs

tests/generic/587 ENDL · line 60

Quoted 10 lines
tests/generic/587 +5 -2
 # matches the stat blocks counter for the only file on the scratch fs that
-# is owned by qa_user.  Note that stat reports in units of 512b blocks whereas
-# repquota reports in units of 1k blocks.
+# is owned by qa_user.
 check_quota_accounting()
 {
+	# repquota rounds the raw numbers up to the nearest 1k when reporting
+	# space usage.  xfs_io stat always reports space usage in 512b units,
+	# so use an awk script to round this number up to the nearest 1k, just
+	# like repquota does.

Yeah, it's better to have a comment to explain why we need a "+1" at here. The V2 looks good to me too.

Quoted 9 lines
tests/generic/587 +2 -1
 	$XFS_IO_PROG -c stat $testfile > $tmp.out
 	cat $tmp.out >> $seqres.full
-	local stat_blocks=$(grep 'stat.blocks' $tmp.out | awk '{print $3 / 2}')
+	local stat_blocks=$(grep 'stat.blocks' $tmp.out | \
+		awk '{printf("%d\n", ($3 + 1) / 2);}')
 
 	_report_quota_blocks $SCRATCH_MNT > $tmp.out
 	cat $tmp.out >> $seqres.full