Skip to contentSkip to conversation

tests: add blkdiscard offsets test

PATCH In review Federico Simoncelli 2 participants 5 messages Oct 23, 2014 – Oct 24, 2014 Util-Linux package

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 tests: add blkdiscard offsets test

---
 tests/commands.sh                 |  1 +
 tests/expected/blkdiscard/offsets | 16 ++++++++++
 tests/ts/blkdiscard/offsets       | 62 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 79 insertions(+)
 create mode 100644 tests/expected/blkdiscard/offsets
 create mode 100755 tests/ts/blkdiscard/offsets
tests/commands.sh +1 -0
index 4c7455e..e11bd2a 100644
@@ -29,6 +29,7 @@ TS_HELPER_SYSINFO="$top_builddir/test_sysinfo"
 # paths to commands
 TS_CMD_ADDPART=${TS_CMD_ADDPART:-"$top_builddir/addpart"}
 TS_CMD_DELPART=${TS_CMD_DELPART:-"$top_builddir/delpart"}
+TS_CMD_BLKDISCARD=${TS_CMD_BLKID-"$top_builddir/blkdiscard"}
 TS_CMD_BLKID=${TS_CMD_BLKID-"$top_builddir/blkid"}
 TS_CMD_CAL=${TS_CMD_CAL-"$top_builddir/cal"}
 TS_CMD_COLRM=${TS_CMD_COLRM:-"$top_builddir/colrm"}
tests/expected/blkdiscard/offsets new file +16 -0
new file mode 100644
index 0000000..766c39d
@@ -0,0 +1,16 @@
+create loop device from image
+testing offsets with full block size
+Discarded 10485760 bytes from the offset 0
+Discarded 10485248 bytes from the offset 512
+Discarded 10485248 bytes from the offset 512
+Discarded 10485248 bytes from the offset 512
+Discarded 10484736 bytes from the offset 1024
+testing offsets with specific length
+Discarded 5242880 bytes from the offset 0
+Discarded 5242880 bytes from the offset 0
+Discarded 5242880 bytes from the offset 0
+Discarded 5242880 bytes from the offset 512
+Discarded 5242880 bytes from the offset 512
+Discarded 5242880 bytes from the offset 512
+Discarded 5242880 bytes from the offset 1024
+detach loop device from image
tests/ts/blkdiscard/offsets new file +62 -0

62 lines not on this page · raw message

Signature · 1 line
1.9.3
#1 lore raw

PATCH 2/2 blkdiscard: fail on sector misalignment

---
 sys-utils/blkdiscard.c            | 10 +++++++---
 tests/expected/blkdiscard/offsets | 12 ++++++------
 2 files changed, 13 insertions(+), 9 deletions(-)
sys-utils/blkdiscard.c +7 -3
index 1eb2b285..76c72b8 100644
@@ -144,9 +144,9 @@ int main(int argc, char **argv)
 	if (ioctl(fd, BLKSSZGET, &secsize))
 		err(EXIT_FAILURE, _("%s: BLKSSZGET ioctl failed"), path);
 
-	/* align range to the sector size */
-	range[0] = (range[0] + secsize - 1) & ~(secsize - 1);
-	range[1] &= ~(secsize - 1);
+	/* check offset alignment to the sector size */
+	if (range[0] % secsize)
+		errx(EXIT_FAILURE, _("%s: offset %" PRIu64 " is not aligned"), path, range[0]);
 
 	/* is the range end behind the end of the device ?*/
 	if (range[0] > blksize)
@@ -155,6 +155,10 @@ int main(int argc, char **argv)
 	if (end < range[0] || end > blksize)
 		range[1] = blksize - range[0];
 
+	/* check length alignment to the sector size */
+	if (range[1] % secsize)
+		errx(EXIT_FAILURE, _("%s: length %" PRIu64 " is not aligned"), path, range[1]);
+
 	if (secure) {
 		if (ioctl(fd, BLKSECDISCARD, &range))
 			err(EXIT_FAILURE, _("%s: BLKSECDISCARD ioctl failed"), path);
tests/expected/blkdiscard/offsets +6 -6
index 766c39d..abedb08 100644
@@ -1,16 +1,16 @@
 create loop device from image
 testing offsets with full block size
 Discarded 10485760 bytes from the offset 0
-Discarded 10485248 bytes from the offset 512
-Discarded 10485248 bytes from the offset 512
+blkdiscard: offset 1 is not aligned
+blkdiscard: offset 511 is not aligned
 Discarded 10485248 bytes from the offset 512
 Discarded 10484736 bytes from the offset 1024
 testing offsets with specific length
 Discarded 5242880 bytes from the offset 0
-Discarded 5242880 bytes from the offset 0
-Discarded 5242880 bytes from the offset 0
-Discarded 5242880 bytes from the offset 512
-Discarded 5242880 bytes from the offset 512
+blkdiscard: length 5242881 is not aligned
+blkdiscard: length 5243391 is not aligned
+blkdiscard: offset 1 is not aligned
+blkdiscard: offset 511 is not aligned
 Discarded 5242880 bytes from the offset 512
 Discarded 5242880 bytes from the offset 1024
 detach loop device from image
Signature · 1 line
1.9.3
#2 lore raw

Re: [PATCH 2/2] blkdiscard: fail on sector misalignment

Quoted 30 lines · Federico Simoncelli

Date: Thu, 23 Oct 2014 15:34:36 +0000
From: Federico Simoncelli <[email protected]>
To: [email protected]
Cc: [email protected], [email protected],
Federico Simoncelli <[email protected]>
Subject: [PATCH 2/2] blkdiscard: fail on sector misalignment

---
 sys-utils/blkdiscard.c            | 10 +++++++---
 tests/expected/blkdiscard/offsets | 12 ++++++------
 2 files changed, 13 insertions(+), 9 deletions(-)
sys-utils/blkdiscard.c +3 -3
index 1eb2b285..76c72b8 100644
@@ -144,9 +144,9 @@ int main(int argc, char **argv)
 	if (ioctl(fd, BLKSSZGET, &secsize))
 		err(EXIT_FAILURE, _("%s: BLKSSZGET ioctl failed"), path);
 
-	/* align range to the sector size */
-	range[0] = (range[0] + secsize - 1) & ~(secsize - 1);
-	range[1] &= ~(secsize - 1);
+	/* check offset alignment to the sector size */
+	if (range[0] % secsize)
+		errx(EXIT_FAILURE, _("%s: offset %" PRIu64 " is not aligned"), path, range[0]);
 
 	/* is the range end behind the end of the device ?*/
 	if (range[0] > blksize)

sys-utils/blkdiscard.c int main(int argc, char **argv) · line 161

Quoted 7 lines
sys-utils/blkdiscard.c +4 -0
@@ -155,6 +155,10 @@ int main(int argc, char **argv)
 	if (end < range[0] || end > blksize)
 		range[1] = blksize - range[0];
 
+	/* check length alignment to the sector size */
+	if (range[1] % secsize)
+		errx(EXIT_FAILURE, _("%s: length %" PRIu64 " is not aligned"), path, range[1]);
+

Looks good, however remember that the tool is intended to be used by the end user, can we be a bit more helpful ? We already know the secsize it should be aligned to, so we can print that as well.

However it just occurred to me that aligning it to secsize is not enough. It should be aligned to discard_granularity.

Thanks!
-Lukas

Quoted 31 lines
sys-utils/blkdiscard.c +0 -0
 	if (secure) {
 		if (ioctl(fd, BLKSECDISCARD, &range))
 			err(EXIT_FAILURE, _("%s: BLKSECDISCARD ioctl failed"), path);
tests/expected/blkdiscard/offsets +6 -6
index 766c39d..abedb08 100644
@@ -1,16 +1,16 @@
 create loop device from image
 testing offsets with full block size
 Discarded 10485760 bytes from the offset 0
-Discarded 10485248 bytes from the offset 512
-Discarded 10485248 bytes from the offset 512
+blkdiscard: offset 1 is not aligned
+blkdiscard: offset 511 is not aligned
 Discarded 10485248 bytes from the offset 512
 Discarded 10484736 bytes from the offset 1024
 testing offsets with specific length
 Discarded 5242880 bytes from the offset 0
-Discarded 5242880 bytes from the offset 0
-Discarded 5242880 bytes from the offset 0
-Discarded 5242880 bytes from the offset 512
-Discarded 5242880 bytes from the offset 512
+blkdiscard: length 5242881 is not aligned
+blkdiscard: length 5243391 is not aligned
+blkdiscard: offset 1 is not aligned
+blkdiscard: offset 511 is not aligned
 Discarded 5242880 bytes from the offset 512
 Discarded 5242880 bytes from the offset 1024
 detach loop device from image
#3 lore raw

Re: [PATCH 2/2] blkdiscard: fail on sector misalignment

----- Original Message -----

Quoted 42 lines

From: "Lukáš Czerner" <[email protected]>
To: "Federico Simoncelli" <[email protected]>
Cc: [email protected], [email protected]
Sent: Friday, October 24, 2014 11:37:33 AM
Subject: Re: [PATCH 2/2] blkdiscard: fail on sector misalignment

Quoted 34 lines · Federico Simoncelli

Date: Thu, 23 Oct 2014 15:34:36 +0000
From: Federico Simoncelli <[email protected]>
To: [email protected]
Cc: [email protected], [email protected],
Federico Simoncelli <[email protected]>
Subject: [PATCH 2/2] blkdiscard: fail on sector misalignment

---
 sys-utils/blkdiscard.c            | 10 +++++++---
 tests/expected/blkdiscard/offsets | 12 ++++++------
 2 files changed, 13 insertions(+), 9 deletions(-)
sys-utils/blkdiscard.c +3 -3
index 1eb2b285..76c72b8 100644
@@ -144,9 +144,9 @@ int main(int argc, char **argv)
 	if (ioctl(fd, BLKSSZGET, &secsize))
 		err(EXIT_FAILURE, _("%s: BLKSSZGET ioctl failed"), path);
 
-	/* align range to the sector size */
-	range[0] = (range[0] + secsize - 1) & ~(secsize - 1);
-	range[1] &= ~(secsize - 1);
+	/* check offset alignment to the sector size */
+	if (range[0] % secsize)
+		errx(EXIT_FAILURE, _("%s: offset %" PRIu64 " is not aligned"), path,
 range[0]);
 
 	/* is the range end behind the end of the device ?*/
sys-utils/blkdiscard.c +0 -0
 	if (range[0] > blksize)
@@ -155,6 +155,10 @@ int main(int argc, char **argv)
 	if (end < range[0] || end > blksize)
 		range[1] = blksize - range[0];
 
Quoted 9 lines

sys-utils/blkdiscard.c int main(int argc, char **argv) · line 161

Quoted 5 lines
sys-utils/blkdiscard.c +4 -0
+	/* check length alignment to the sector size */
+	if (range[1] % secsize)
+		errx(EXIT_FAILURE, _("%s: length %" PRIu64 " is not aligned"), path,
 range[1]);
+

Looks good, however remember that the tool is intended to be used by the end user, can we be a bit more helpful ? We already know the secsize it should be aligned to, so we can print that as well.

I will update the error message.

Quoted 2 lines

However it just occurred to me that aligning it to secsize is not enough. It should be aligned to discard_granularity.

Aligning to discard_granularity is an optimization, on the other hand not aligning to secsize would result in the ioctl failing (EINVAL).

Signature · 1 line
Federico
#4 lore raw

Re: [PATCH 2/2] blkdiscard: fail on sector misalignment

Quoted 59 lines · Federico Simoncelli

Date: Fri, 24 Oct 2014 05:47:04 -0400 (EDT)
From: Federico Simoncelli <[email protected]>
To: Lukáš Czerner <[email protected]>
Cc: [email protected], [email protected]
Subject: Re: [PATCH 2/2] blkdiscard: fail on sector misalignment

----- Original Message -----

Quoted 42 lines

From: "Lukáš Czerner" <[email protected]>
To: "Federico Simoncelli" <[email protected]>
Cc: [email protected], [email protected]
Sent: Friday, October 24, 2014 11:37:33 AM
Subject: Re: [PATCH 2/2] blkdiscard: fail on sector misalignment

Quoted 34 lines · Federico Simoncelli

Date: Thu, 23 Oct 2014 15:34:36 +0000
From: Federico Simoncelli <[email protected]>
To: [email protected]
Cc: [email protected], [email protected],
Federico Simoncelli <[email protected]>
Subject: [PATCH 2/2] blkdiscard: fail on sector misalignment

---
 sys-utils/blkdiscard.c            | 10 +++++++---
 tests/expected/blkdiscard/offsets | 12 ++++++------
 2 files changed, 13 insertions(+), 9 deletions(-)
sys-utils/blkdiscard.c +3 -3
index 1eb2b285..76c72b8 100644
@@ -144,9 +144,9 @@ int main(int argc, char **argv)
 	if (ioctl(fd, BLKSSZGET, &secsize))
 		err(EXIT_FAILURE, _("%s: BLKSSZGET ioctl failed"), path);
 
-	/* align range to the sector size */
-	range[0] = (range[0] + secsize - 1) & ~(secsize - 1);
-	range[1] &= ~(secsize - 1);
+	/* check offset alignment to the sector size */
+	if (range[0] % secsize)
+		errx(EXIT_FAILURE, _("%s: offset %" PRIu64 " is not aligned"), path,
 range[0]);
 
 	/* is the range end behind the end of the device ?*/
sys-utils/blkdiscard.c +0 -0
 	if (range[0] > blksize)
@@ -155,6 +155,10 @@ int main(int argc, char **argv)
 	if (end < range[0] || end > blksize)
 		range[1] = blksize - range[0];
 
Quoted 9 lines

sys-utils/blkdiscard.c int main(int argc, char **argv) · line 161

Quoted 5 lines
sys-utils/blkdiscard.c +4 -0
+	/* check length alignment to the sector size */
+	if (range[1] % secsize)
+		errx(EXIT_FAILURE, _("%s: length %" PRIu64 " is not aligned"), path,
 range[1]);
+

Looks good, however remember that the tool is intended to be used by the end user, can we be a bit more helpful ? We already know the secsize it should be aligned to, so we can print that as well.

Quoted 7 lines

I will update the error message.

Quoted 2 lines

However it just occurred to me that aligning it to secsize is not enough. It should be aligned to discard_granularity.

Aligning to discard_granularity is an optimization, on the other hand not aligning to secsize would result in the ioctl failing (EINVAL).

Fair enough, but the unaligned part will be ignored and not 'discarded'. But I guess that it really does not matter.

-Lukas

Quoted 2 lines