git: 37e1449d29c8 - stable/13 - loader: Add preload operation to fs_ops

From: Emmanuel Vadot <manu_at_FreeBSD.org>
Date: Thu, 13 Jan 2022 09:49:05 UTC
The branch stable/13 has been updated by manu:

URL: https://cgit.FreeBSD.org/src/commit/?id=37e1449d29c847bbbb5120774b2256362a2aabdd

commit 37e1449d29c847bbbb5120774b2256362a2aabdd
Author:     Emmanuel Vadot <manu@FreeBSD.org>
AuthorDate: 2021-12-10 09:33:43 +0000
Commit:     Emmanuel Vadot <manu@FreeBSD.org>
CommitDate: 2022-01-13 07:58:31 +0000

    loader: Add preload operation to fs_ops
    
    When we load an ELF file (kernel or module) we do seek(2) a lot to
    parse/load the different sections of the ELF file.
    Protocol like TFTP suffers a lot from this as there is no resume or
    a way to start the tranfer from a specified offset in the file.
    fs_preload is added to help those protocol.
    Call preload just after opening the ELF file that we need to load so
    the underlying method can cache the hole file and then read/lseek operations
    are faster.
    
    Reviewed by:    imp
    MFC after:      2 weeks
    Sponsored by:   Beckhoff Automation GmbH & Co. KG
    Differential Revision:  https://reviews.freebsd.org/D33409
    
    (cherry picked from commit c25d9aff4607e7f1c378b0b7853e49c07c0af91a)
---
 stand/common/load_elf.c |  1 +
 stand/libsa/Makefile    |  2 +-
 stand/libsa/preload.c   | 45 +++++++++++++++++++++++++++++++++++++++++++++
 stand/libsa/stand.h     |  2 ++
 4 files changed, 49 insertions(+), 1 deletion(-)

diff --git a/stand/common/load_elf.c b/stand/common/load_elf.c
index a213b34970f0..c75567b4a560 100644
--- a/stand/common/load_elf.c
+++ b/stand/common/load_elf.c
@@ -238,6 +238,7 @@ __elfN(load_elf_header)(char *filename, elf_file_t ef)
 		close(ef->fd);
 		return (ENOMEM);
 	}
+	preload(ef->fd);
 #ifdef LOADER_VERIEXEC_VECTX
 	{
 		int verror;
diff --git a/stand/libsa/Makefile b/stand/libsa/Makefile
index 6fe145d681db..6ac1d900104e 100644
--- a/stand/libsa/Makefile
+++ b/stand/libsa/Makefile
@@ -132,7 +132,7 @@ CLEANFILES+= ${SAFE_INCS} ${STAND_H_INC} ${OTHER_INC}
 
 # io routines
 SRCS+=	closeall.c dev.c ioctl.c nullfs.c stat.c mount.c \
-	fstat.c close.c lseek.c open.c read.c write.c readdir.c
+	fstat.c close.c lseek.c open.c read.c write.c readdir.c preload.c
 
 # SMBios routines
 SRCS+=	smbios.c
diff --git a/stand/libsa/preload.c b/stand/libsa/preload.c
new file mode 100644
index 000000000000..3d41101dc861
--- /dev/null
+++ b/stand/libsa/preload.c
@@ -0,0 +1,45 @@
+/*-
+ * Copyright (c) 2021 Beckhoff Automation GmbH & Co. KG
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/param.h>
+#include "stand.h"
+
+void
+preload(int fd)
+{
+	struct open_file *f;
+
+	f = fd2open_file(fd);
+	if (f == NULL) {
+		errno = EBADF;
+		return;
+	}
+	if (f->f_ops->fo_preload)
+		(f->f_ops->fo_preload)(f);
+}
diff --git a/stand/libsa/stand.h b/stand/libsa/stand.h
index ab0961b77086..172b54c33b30 100644
--- a/stand/libsa/stand.h
+++ b/stand/libsa/stand.h
@@ -111,6 +111,7 @@ struct fs_ops {
     off_t	(*fo_seek)(struct open_file *f, off_t offset, int where);
     int		(*fo_stat)(struct open_file *f, struct stat *sb);
     int		(*fo_readdir)(struct open_file *f, struct dirent *d);
+    int		(*fo_preload)(struct open_file *f);
     int		(*fo_mount)(const char *, const char *, void **);
     int		(*fo_unmount)(const char *, void *);
 };
@@ -300,6 +301,7 @@ extern void	closeall(void);
 extern ssize_t	read(int, void *, size_t);
 extern ssize_t	write(int, const void *, size_t);
 extern struct	dirent *readdirfd(int);
+extern void	preload(int);
 
 extern void	srandom(unsigned int);
 extern long	random(void);