public inbox for util-linux@vger.kernel.org
 help / color / mirror / Atom feed
From: Federico Simoncelli <fsimonce@redhat.com>
To: util-linux@vger.kernel.org
Cc: kzak@redhat.com, lczerner@redhat.com,
	Federico Simoncelli <fsimonce@redhat.com>
Subject: [PATCH 2/2] blkdiscard: fail on sector misalignment
Date: Thu, 23 Oct 2014 15:34:36 +0000	[thread overview]
Message-ID: <1414078476-11007-2-git-send-email-fsimonce@redhat.com> (raw)
In-Reply-To: <1414078476-11007-1-git-send-email-fsimonce@redhat.com>

---
 sys-utils/blkdiscard.c            | 10 +++++++---
 tests/expected/blkdiscard/offsets | 12 ++++++------
 2 files changed, 13 insertions(+), 9 deletions(-)

diff --git a/sys-utils/blkdiscard.c b/sys-utils/blkdiscard.c
index 1eb2b285..76c72b8 100644
--- a/sys-utils/blkdiscard.c
+++ b/sys-utils/blkdiscard.c
@@ -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);
diff --git a/tests/expected/blkdiscard/offsets b/tests/expected/blkdiscard/offsets
index 766c39d..abedb08 100644
--- a/tests/expected/blkdiscard/offsets
+++ b/tests/expected/blkdiscard/offsets
@@ -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
-- 
1.9.3


  reply	other threads:[~2014-10-23 15:34 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-10-23 15:34 [PATCH 1/2] tests: add blkdiscard offsets test Federico Simoncelli
2014-10-23 15:34 ` Federico Simoncelli [this message]
2014-10-24  9:37   ` [PATCH 2/2] blkdiscard: fail on sector misalignment Lukáš Czerner
2014-10-24  9:47     ` Federico Simoncelli
2014-10-24  9:50       ` Lukáš Czerner

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=1414078476-11007-2-git-send-email-fsimonce@redhat.com \
    --to=fsimonce@redhat.com \
    --cc=kzak@redhat.com \
    --cc=lczerner@redhat.com \
    --cc=util-linux@vger.kernel.org \
    /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