From: Bart Van Assche <bvanassche@acm.org>
To: Shin'ichiro Kawasaki <shinichiro.kawasaki@wdc.com>
Cc: Damien Le Moal <dlemoal@kernel.org>,
linux-block@vger.kernel.org, Bart Van Assche <bvanassche@acm.org>,
Keith Busch <kbusch@kernel.org>
Subject: [PATCH blktests] src/dio-offsets.c: Fix err() usage
Date: Thu, 26 Mar 2026 10:36:32 -0700 [thread overview]
Message-ID: <20260326173632.3259254-1-bvanassche@acm.org> (raw)
If the dio-offsets program detects data corruption, it reports the
following message:
dio-offsets: test_unaligned_vectors: data corruption: Success
The "Success" part in this message is confusing and is reported because
the err() macro is used incorrectly. errno must be set before err() is
used instead of passing an error number as first argument. Fix usage of
the err() macro as follows:
- Change the first argument into EXIT_FAILURE (1). According to POSIX,
exit codes 1 - 125 mean failure and > 128 means that a program was
terminated by a signal. Hence, exit with code 1 instead of -1 if
ioctl() fails.
- Use the err_errno() macro to set the error code instead of passing an
error code as first argument to err().
Cc: Keith Busch <kbusch@kernel.org>
Signed-off-by: Bart Van Assche <bvanassche@acm.org>
---
src/dio-offsets.c | 99 +++++++++++++++++++++++++++--------------------
1 file changed, 56 insertions(+), 43 deletions(-)
diff --git a/src/dio-offsets.c b/src/dio-offsets.c
index 4d9c71c83798..9fc7b9247b99 100644
--- a/src/dio-offsets.c
+++ b/src/dio-offsets.c
@@ -26,6 +26,11 @@
#include <sys/mount.h>
#define power_of_2(x) ((x) && !((x) & ((x) - 1)))
+#define err_errno(exit_code, ...) \
+ do { \
+ errno = (exit_code); \
+ err(EXIT_FAILURE, __VA_ARGS__); \
+ } while (0)
static unsigned long logical_block_size;
static unsigned long dma_alignment;
@@ -42,7 +47,7 @@ static void init_args(char **argv)
{
test_fd = open(argv[1], O_RDWR | O_TRUNC | O_DIRECT);
if (test_fd < 0)
- err(errno, "%s: failed to open %s", __func__, argv[1]);
+ err(EXIT_FAILURE, "%s: failed to open %s", __func__, argv[1]);
max_segments = strtoul(argv[2], NULL, 0);
max_bytes = strtoul(argv[3], NULL, 0) * 1024;
@@ -54,18 +59,18 @@ static void init_args(char **argv)
!power_of_2(dma_alignment) ||
!power_of_2(logical_block_size)) {
errno = EINVAL;
- err(1, "%s: bad parameters", __func__);
+ err(EXIT_FAILURE, "%s: bad parameters", __func__);
}
if (virt_boundary > 1 && virt_boundary < logical_block_size) {
errno = EINVAL;
- err(1, "%s: virt_boundary:%lu logical_block_size:%lu", __func__,
+ err(EXIT_FAILURE, "%s: virt_boundary:%lu logical_block_size:%lu", __func__,
virt_boundary, logical_block_size);
}
if (dma_alignment > logical_block_size) {
errno = EINVAL;
- err(1, "%s: dma_alignment:%lu logical_block_size:%lu", __func__,
+ err(EXIT_FAILURE, "%s: dma_alignment:%lu logical_block_size:%lu", __func__,
dma_alignment, logical_block_size);
}
@@ -87,7 +92,7 @@ static void init_buffers()
buf_size = max_bytes * max_segments / 2;
if (buf_size < logical_block_size * max_segments)
- err(EINVAL, "%s: logical block size is too big", __func__);
+ err_errno(EINVAL, "%s: logical block size is too big", __func__);
if (buf_size < logical_block_size * 1024 * 4)
buf_size = logical_block_size * 1024 * 4;
@@ -97,26 +102,27 @@ static void init_buffers()
ret = ioctl(test_fd, BLKGETSIZE64, &dev_bytes);
if (ret < 0)
- err(ret, "%s: ioctl BLKGETSIZE64 failed", __func__);
+ err(EXIT_FAILURE, "%s: ioctl BLKGETSIZE64 failed", __func__);
if (dev_bytes < buf_size)
buf_size = dev_bytes;
ret = posix_memalign((void **)&in_buf, pagesize, buf_size);
if (ret)
- err(EINVAL, "%s: failed to allocate in-buf", __func__);
+ err_errno(EINVAL, "%s: failed to allocate in-buf", __func__);
ret = posix_memalign((void **)&out_buf, pagesize, buf_size);
if (ret)
- err(EINVAL, "%s: failed to allocate out-buf", __func__);
+ err_errno(EINVAL, "%s: failed to allocate out-buf", __func__);
fd = open("/dev/urandom", O_RDONLY);
if (fd < 0)
- err(EINVAL, "%s: failed to open urandom", __func__);
+ err_errno(EINVAL, "%s: failed to open urandom", __func__);
ret = read(fd, out_buf, buf_size);
if (ret < 0)
- err(EINVAL, "%s: failed to randomize output buffer", __func__);
+ err_errno(EINVAL, "%s: failed to randomize output buffer",
+ __func__);
close(fd);
}
@@ -143,11 +149,11 @@ static void test_full_size_aligned()
memset(in_buf, 0, buf_size);
ret = pwrite(test_fd, out_buf, buf_size, 0);
if (ret < 0)
- err(errno, "%s: failed to write buf", __func__);
+ err(EXIT_FAILURE, "%s: failed to write buf", __func__);
ret = pread(test_fd, in_buf, buf_size, 0);
if (ret < 0)
- err(errno, "%s: failed to read buf", __func__);
+ err(EXIT_FAILURE, "%s: failed to read buf", __func__);
compare(out_buf, in_buf, buf_size);
}
@@ -164,11 +170,11 @@ static void test_dma_aligned()
memset(in_buf, 0, buf_size);
ret = pwrite(test_fd, out_buf + dma_alignment, max_bytes, 0);
if (ret < 0)
- err(errno, "%s: failed to write buf", __func__);
+ err(EXIT_FAILURE, "%s: failed to write buf", __func__);
ret = pread(test_fd, in_buf + dma_alignment, max_bytes, 0);
if (ret < 0)
- err(errno, "%s: failed to read buf", __func__);
+ err(EXIT_FAILURE, "%s: failed to read buf", __func__);
compare(out_buf + dma_alignment, in_buf + dma_alignment, max_bytes);
}
@@ -194,7 +200,7 @@ static void test_page_aligned_vectors()
ret = pwritev(test_fd, iov, vecs, 0);
if (ret < 0)
- err(errno, "%s: failed to write buf", __func__);
+ err(EXIT_FAILURE, "%s: failed to write buf", __func__);
for (i = 0; i < vecs; i++) {
offset = logical_block_size * i * 4;
@@ -204,7 +210,7 @@ static void test_page_aligned_vectors()
ret = preadv(test_fd, iov, vecs, 0);
if (ret < 0)
- err(errno, "%s: failed to read buf", __func__);
+ err(EXIT_FAILURE, "%s: failed to read buf", __func__);
for (i = 0; i < vecs; i++) {
offset = logical_block_size * i * 4;
@@ -234,7 +240,7 @@ static void test_dma_aligned_vectors()
ret = pwritev(test_fd, iov, vecs, 0);
if (ret < 0)
- err(errno, "%s: failed to write buf", __func__);
+ err(EXIT_FAILURE, "%s: failed to write buf", __func__);
for (i = 0; i < vecs; i++) {
offset = logical_block_size * i * 8 + dma_alignment * (i + 1);
@@ -244,7 +250,7 @@ static void test_dma_aligned_vectors()
ret = preadv(test_fd, iov, vecs, 0);
if (ret < 0)
- err(errno, "%s: failed to read buf", __func__);
+ err(EXIT_FAILURE, "%s: failed to read buf", __func__);
for (i = 0; i < vecs; i++) {
offset = logical_block_size * i * 8 + dma_alignment * (i + 1);
@@ -295,7 +301,7 @@ static void test_unaligned_page_vectors()
if (ret < 0) {
if (should_fail)
return;
- err(errno, "%s: failed to write buf", __func__);
+ err(EXIT_FAILURE, "%s: failed to write buf", __func__);
}
i = 0;
@@ -318,7 +324,7 @@ static void test_unaligned_page_vectors()
ret = preadv(test_fd, iov, vecs, 0);
if (ret < 0)
- err(errno, "%s: failed to read buf", __func__);
+ err(EXIT_FAILURE, "%s: failed to read buf", __func__);
i = 0;
offset = pagesize - (logical_block_size / 4);
@@ -367,7 +373,7 @@ static void test_unaligned_vectors()
ret = preadv(test_fd, iov, vecs, 0);
if (ret < 0)
- err(errno, "%s: failed to read buf", __func__);
+ err(EXIT_FAILURE, "%s: failed to read buf", __func__);
for (i = 0; i < vecs; i++) {
offset = logical_block_size * i * 8;
@@ -400,8 +406,9 @@ static void test_invalid_starting_addr()
if (ret < 0)
return;
- err(ENOTSUP, "%s: write buf unexpectedly succeeded with NULL address ret:%d",
- __func__, ret);
+ err_errno(ENOTSUP,
+ "%s: write buf unexpectedly succeeded with NULL address ret:%d",
+ __func__, ret);
}
/*
@@ -436,8 +443,9 @@ static void test_invalid_middle_addr()
if (ret < 0)
return;
- err(ENOTSUP, "%s: write buf unexpectedly succeeded with NULL address ret:%d",
- __func__, ret);
+ err_errno(ENOTSUP,
+ "%s: write buf unexpectedly succeeded with NULL address ret:%d",
+ __func__, ret);
}
/*
@@ -458,16 +466,17 @@ static void test_invalid_dma_alignment()
if (ret < 0) {
if (should_fail)
return;
- err(errno, "%s: failed to write buf", __func__);
+ err(EXIT_FAILURE, "%s: failed to write buf", __func__);
}
if (should_fail)
- err(ENOTSUP, "%s: write buf unexpectedly succeeded with invalid DMA offset address, ret:%d",
- __func__, ret);
+ err_errno(ENOTSUP,
+ "%s: write buf unexpectedly succeeded with invalid DMA offset address, ret:%d",
+ __func__, ret);
ret = pread(test_fd, in_buf + offset, size, 0);
if (ret < 0)
- err(errno, "%s: failed to read buf", __func__);
+ err(EXIT_FAILURE, "%s: failed to read buf", __func__);
compare(out_buf + offset, in_buf + offset, size);
}
@@ -506,11 +515,12 @@ static void test_invalid_dma_vector_alignment()
if (ret < 0) {
if (should_fail)
return;
- err(errno, "%s: failed to write buf", __func__);
+ err(EXIT_FAILURE, "%s: failed to write buf", __func__);
}
if (should_fail)
- err(ENOTSUP, "%s: write buf unexpectedly succeeded with invalid DMA offset address ret:%d",
- __func__, ret);
+ err_errno(ENOTSUP,
+ "%s: write buf unexpectedly succeeded with invalid DMA offset address ret:%d",
+ __func__, ret);
iov[0].iov_base = in_buf;
iov[0].iov_len = max_bytes;
@@ -529,7 +539,7 @@ static void test_invalid_dma_vector_alignment()
ret = preadv(test_fd, iov, vecs, 0);
if (ret < 0)
- err(errno, "%s: failed to read buf", __func__);
+ err(EXIT_FAILURE, "%s: failed to read buf", __func__);
compare(out_buf, in_buf, max_bytes);
compare(out_buf + max_bytes * 2, in_buf + max_bytes * 2, max_bytes);
@@ -568,12 +578,13 @@ static void test_max_vector_limits()
if (ret < 0) {
if (should_fail)
return;
- err(errno, "%s: failed to write buf", __func__);
+ err(EXIT_FAILURE, "%s: failed to write buf", __func__);
}
if (should_fail)
- err(ENOTSUP, "%s: write buf unexpectedly succeeded with excess vectors ret:%d",
- __func__, ret);
+ err_errno(ENOTSUP,
+ "%s: write buf unexpectedly succeeded with excess vectors ret:%d",
+ __func__, ret);
for (i = 0; i < vecs; i++) {
offset = i * iov_size * 2;
@@ -583,7 +594,7 @@ static void test_max_vector_limits()
ret = preadv(test_fd, iov, vecs, 0);
if (ret < 0)
- err(errno, "%s: failed to read buf", __func__);
+ err(EXIT_FAILURE, "%s: failed to read buf", __func__);
for (i = 0; i < vecs; i++) {
offset = i * iov_size * 2;
@@ -622,8 +633,9 @@ static void test_invalid_dma_vector_alignment_large()
if (ret < 0)
return;
- err(ENOTSUP, "%s: write buf unexpectedly succeeded with NULL address ret:%d",
- __func__, ret);
+ err_errno(ENOTSUP,
+ "%s: write buf unexpectedly succeeded with NULL address ret:%d",
+ __func__, ret);
}
/*
@@ -656,12 +668,13 @@ static void test_invalid_dma_vector_length()
if (ret < 0) {
if (should_fail)
return;
- err(errno, "%s: failed to write buf", __func__);
+ err(EXIT_FAILURE, "%s: failed to write buf", __func__);
}
if (should_fail)
- err(ENOTSUP, "%s: write buf unexpectedly succeeded with invalid DMA offset address ret:%d",
- __func__, ret);
+ err_errno(ENOTSUP,
+ "%s: write buf unexpectedly succeeded with invalid DMA offset address ret:%d",
+ __func__, ret);
iov[0].iov_base = in_buf;
iov[0].iov_len = max_bytes * 2 - max_bytes / 2;
@@ -677,7 +690,7 @@ static void test_invalid_dma_vector_length()
ret = pwritev(test_fd, iov, vecs, 0);
if (ret < 0)
- err(errno, "%s: failed to read buf", __func__);
+ err(EXIT_FAILURE, "%s: failed to read buf", __func__);
compare(out_buf, in_buf, iov[0].iov_len);
compare(out_buf + max_bytes * 4, in_buf + max_bytes * 4, iov[1].iov_len);
next reply other threads:[~2026-03-26 17:36 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-26 17:36 Bart Van Assche [this message]
2026-03-26 17:42 ` [PATCH blktests] src/dio-offsets.c: Fix err() usage Keith Busch
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=20260326173632.3259254-1-bvanassche@acm.org \
--to=bvanassche@acm.org \
--cc=dlemoal@kernel.org \
--cc=kbusch@kernel.org \
--cc=linux-block@vger.kernel.org \
--cc=shinichiro.kawasaki@wdc.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