Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 2 Feb 2023 20:10:21 GMT
From:      Warner Losh <imp@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org
Subject:   git: 2e1edd04eb01 - main - kboot: For hostfs, return better errors from read, where possible.
Message-ID:  <202302022010.312KALBP044884@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch main has been updated by imp:

URL: https://cgit.FreeBSD.org/src/commit/?id=2e1edd04eb01bac15628185686cbf615d80de7de

commit 2e1edd04eb01bac15628185686cbf615d80de7de
Author:     Warner Losh <imp@FreeBSD.org>
AuthorDate: 2023-02-02 20:06:24 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2023-02-02 20:06:31 +0000

    kboot: For hostfs, return better errors from read, where possible.
    
    Translate the Linux error return from read to a FreeBSD errno. We use a
    simplified translation: 1-34 are the same between the systems, so any of
    those will be returned directly. All other errno map to EINVAL. This
    will suffice for some code that reads /dev/mem in producing the right
    diagnostic.
    
    A fully generalized version is much harder. Linux has a number of errno
    that don't translate well and has architecture dependent
    encodings. Avoid this mess with a simple macro for now. Add comment
    explaining why we use the simple method we do.
    
    Sponsored by:           Netflix
    Reviewed by:            kevans, andrew
    Differential Revision:  https://reviews.freebsd.org/D38265
---
 stand/kboot/host_syscall.h | 10 ++++++++++
 stand/kboot/hostfs.c       |  5 ++---
 2 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/stand/kboot/host_syscall.h b/stand/kboot/host_syscall.h
index 38f0fb04aa95..75394afcde6c 100644
--- a/stand/kboot/host_syscall.h
+++ b/stand/kboot/host_syscall.h
@@ -206,4 +206,14 @@ ssize_t host_write(int fd, const void *buf, size_t nbyte);
 	host_mmap(0, size, HOST_PROT_READ | HOST_PROT_WRITE, \
 	    HOST_MAP_PRIVATE | HOST_MAP_ANONYMOUS, -1, 0);
 
+/*
+ * Translate Linux errno to FreeBSD errno. The two system have idenitcal errors
+ * for 1-34. After that, they differ. Linux also has errno that don't map
+ * exactly to FreeBSD's errno, plus the Linux errno are arch dependent >
+ * 34. Since we just need to do this for simple cases, use the simple mapping
+ * function where -1 to -34 are translated to 1 to 34 and all others are EINVAL.
+ * Pass the linux return value, which will be the -errno.
+ */
+#define host_to_stand_errno(e)	((-e) > 34 ? EINVAL : (-e))
+
 #endif
diff --git a/stand/kboot/hostfs.c b/stand/kboot/hostfs.c
index 08f532999abe..02afa885d833 100644
--- a/stand/kboot/hostfs.c
+++ b/stand/kboot/hostfs.c
@@ -112,9 +112,8 @@ hostfs_read(struct open_file *f, void *start, size_t size, size_t *resid)
 	ssize_t sz;
 
 	sz = host_read(hf->hf_fd, start, size);
-	if (sz < 0) {
-		return (EINVAL);
-	}
+	if (sz < 0)
+		return (host_to_stand_errno(sz));
 	*resid = size - sz;
 
 	return (0);



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202302022010.312KALBP044884>