Skip to content

Commit 1956417

Browse files
robntonyhutter
authored andcommitted
mmap_seek: print error code and text on failure
If lseek() returns an unexpected error, it's useful to know the error code to help connect it to the trouble spot inside the module. Since the two seek functions should be basically identical, lift them into a single generic function. Sponsored-by: Klara, Inc. Sponsored-by: Wasabi Technology, Inc. Reviewed-by: Robert Evans <[email protected]> Reviewed-by: Brian Behlendorf <[email protected]> Signed-off-by: Rob Norris <[email protected]> Closes openzfs#17843
1 parent 3378a32 commit 1956417

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

tests/zfs-tests/cmd/mmap_seek.c

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,25 +47,34 @@
4747
#endif
4848

4949
static void
50+
seek_expect(int fd, off_t offset, int whence, off_t expect_offset)
51+
{
52+
errno = 0;
53+
off_t seek_offset = lseek(fd, offset, whence);
54+
if (seek_offset == expect_offset)
55+
return;
56+
57+
int err = errno;
58+
fprintf(stderr, "lseek(fd, %ld, SEEK_%s) = %ld (expected %ld)",
59+
offset, (whence == SEEK_DATA ? "DATA" : "HOLE"),
60+
seek_offset, expect_offset);
61+
if (err != 0)
62+
fprintf(stderr, " (errno %d [%s])\n", err, strerror(err));
63+
else
64+
fputc('\n', stderr);
65+
exit(2);
66+
}
67+
68+
static inline void
5069
seek_data(int fd, off_t offset, off_t expected)
5170
{
52-
off_t data_offset = lseek(fd, offset, SEEK_DATA);
53-
if (data_offset != expected) {
54-
fprintf(stderr, "lseek(fd, %d, SEEK_DATA) = %d (expected %d)\n",
55-
(int)offset, (int)data_offset, (int)expected);
56-
exit(2);
57-
}
71+
seek_expect(fd, offset, SEEK_DATA, expected);
5872
}
5973

60-
static void
74+
static inline void
6175
seek_hole(int fd, off_t offset, off_t expected)
6276
{
63-
off_t hole_offset = lseek(fd, offset, SEEK_HOLE);
64-
if (hole_offset != expected) {
65-
fprintf(stderr, "lseek(fd, %d, SEEK_HOLE) = %d (expected %d)\n",
66-
(int)offset, (int)hole_offset, (int)expected);
67-
exit(2);
68-
}
77+
seek_expect(fd, offset, SEEK_HOLE, expected);
6978
}
7079

7180
int

0 commit comments

Comments
 (0)