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 <evansr@google.com>
Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Rob Norris <rob.norris@klarasystems.com>
Closes #17843
This commit is contained in:
Rob Norris 2025-10-15 09:57:35 +11:00 committed by GitHub
parent d1e1b80ffe
commit 6ae99d2692
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -47,25 +47,34 @@
#endif #endif
static void static void
seek_data(int fd, off_t offset, off_t expected) seek_expect(int fd, off_t offset, int whence, off_t expect_offset)
{ {
off_t data_offset = lseek(fd, offset, SEEK_DATA); errno = 0;
if (data_offset != expected) { off_t seek_offset = lseek(fd, offset, whence);
fprintf(stderr, "lseek(fd, %d, SEEK_DATA) = %d (expected %d)\n", if (seek_offset == expect_offset)
(int)offset, (int)data_offset, (int)expected); return;
exit(2);
} int err = errno;
fprintf(stderr, "lseek(fd, %ld, SEEK_%s) = %ld (expected %ld)",
offset, (whence == SEEK_DATA ? "DATA" : "HOLE"),
seek_offset, expect_offset);
if (err != 0)
fprintf(stderr, " (errno %d [%s])\n", err, strerror(err));
else
fputc('\n', stderr);
exit(2);
} }
static void static inline void
seek_data(int fd, off_t offset, off_t expected)
{
seek_expect(fd, offset, SEEK_DATA, expected);
}
static inline void
seek_hole(int fd, off_t offset, off_t expected) seek_hole(int fd, off_t offset, off_t expected)
{ {
off_t hole_offset = lseek(fd, offset, SEEK_HOLE); seek_expect(fd, offset, SEEK_HOLE, expected);
if (hole_offset != expected) {
fprintf(stderr, "lseek(fd, %d, SEEK_HOLE) = %d (expected %d)\n",
(int)offset, (int)hole_offset, (int)expected);
exit(2);
}
} }
int int