From 2629101b7ca3d9a0b7b879e425ee6f55ae4274a8 Mon Sep 17 00:00:00 2001
From: Sascha Wildner <saw@online.de>
Date: Fri, 26 Apr 2019 13:35:52 +0200
Subject: [PATCH] Dummy posix_fadvise()

---
 lib/libc/stdlib/Makefile.inc    |   7 +-
 lib/libc/stdlib/Symbol.map      |   1 +
 lib/libc/stdlib/posix_fadvise.3 | 146 ++++++++++++++++++++++++++++++++
 lib/libc/stdlib/posix_fadvise.c |  51 +++++++++++
 lib/liblzma/config.h            |   2 +-
 sys/sys/fcntl.h                 |  14 ++-
 6 files changed, 216 insertions(+), 5 deletions(-)
 create mode 100644 lib/libc/stdlib/posix_fadvise.3
 create mode 100644 lib/libc/stdlib/posix_fadvise.c

diff --git a/lib/libc/stdlib/Makefile.inc b/lib/libc/stdlib/Makefile.inc
index d47dd3be17..b6236c6e6f 100644
--- a/lib/libc/stdlib/Makefile.inc
+++ b/lib/libc/stdlib/Makefile.inc
@@ -13,7 +13,7 @@ MISRCS+=a64l.c abort.c abs.c atexit.c \
 	getenv.c getopt.c getopt_long.c \
 	getsubopt.c hcreate.c heapsort.c imaxabs.c imaxdiv.c \
 	insque.c l64a.c labs.c ldiv.c llabs.c lldiv.c lsearch.c \
-	merge.c ptsname.c qsort.c qsort_r.c quick_exit.c \
+	merge.c posix_fadvise.c ptsname.c qsort.c qsort_r.c quick_exit.c \
 	radixsort.c rand.c random.c \
 	reallocarray.c reallocf.c realpath.c recallocarray.c remque.c \
 	strfmon.c strsuftoll.c strtoimax.c \
@@ -41,8 +41,9 @@ MAN+=	a64l.3 abort.3 abs.3 alloca.3 atexit.3 atof.3 atoi.3 atol.3 \
 	bsearch.3 \
 	div.3 exit.3 getenv.3 getopt.3 getopt_long.3 getsubopt.3 \
 	hcreate.3 imaxabs.3 imaxdiv.3 insque.3 labs.3 ldiv.3 llabs.3 lldiv.3 \
-	lsearch.3 malloc.3 memory.3 posix_memalign.3 posix_openpt.3 \
-	ptsname.3 qsort.3 quick_exit.3 \
+	lsearch.3 malloc.3 memory.3 \
+	posix_fadvise.3 posix_memalign.3 posix_openpt.3 ptsname.3 \
+	qsort.3 quick_exit.3 \
 	radixsort.3 rand.3 random.3 realpath.3 reallocarray.3 \
 	strfmon.3 strsuftoll.3 strtod.3 strtol.3 strtonum.3 strtoul.3 system.3 \
 	tsearch.3
diff --git a/lib/libc/stdlib/Symbol.map b/lib/libc/stdlib/Symbol.map
index c064e320e4..e93cba1210 100644
--- a/lib/libc/stdlib/Symbol.map
+++ b/lib/libc/stdlib/Symbol.map
@@ -120,6 +120,7 @@ DF506.0 {
     __aligned_alloc;
     __cxa_thread_atexit_impl;
     freezero;
+    posix_fadvise;
     reallocarray;
     recallocarray;
 };
diff --git a/lib/libc/stdlib/posix_fadvise.3 b/lib/libc/stdlib/posix_fadvise.3
new file mode 100644
index 0000000000..fdefec569c
--- /dev/null
+++ b/lib/libc/stdlib/posix_fadvise.3
@@ -0,0 +1,146 @@
+.\"
+.\" Copyright (c) 2019 The DragonFly Project.  All rights reserved.
+.\"
+.\" 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.
+.\" 3. Neither the name of The DragonFly Project nor the names of its
+.\"    contributors may be used to endorse or promote products derived
+.\"    from this software without specific, prior written permission.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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
+.\" COPYRIGHT HOLDERS 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.
+.\"
+.Dd February 7, 2019
+.Dt POSIX_FADVISE 3
+.Os
+.Sh NAME
+.Nm posix_fadvise
+.Nd give advice about use of file data
+.Sh LIBRARY
+.Lb libc
+.Sh SYNOPSIS
+.In fcntl.h
+.Ft int
+.Fn posix_fadvise "int fd" "off_t offset" "off_t len" "int advice"
+.Sh DESCRIPTION
+.Em Note
+that this implementation is a no-op.
+It only does some failure checks.
+Any
+.Fa advice
+has no effect on the system's data access behavior.
+.Pp
+The
+.Fn posix_fadvise
+function
+allows a process to describe to the system its data access behavior for an
+open file descriptor
+.Fa fd .
+The advice covers the data starting at offset
+.Fa offset
+and continuing for
+.Fa len
+bytes.
+If
+.Fa len
+is zero,
+all data from
+.Fa offset
+to the end of the file is covered.
+.Pp
+The behavior is specified by the
+.Fa advice
+parameter and may be one of:
+.Bl -tag -width POSIX_FADV_SEQUENTIAL
+.It Dv POSIX_FADV_NORMAL
+Tells the system to revert to the default data access behavior.
+.It Dv POSIX_FADV_RANDOM
+Is a hint that file data will be accessed randomly,
+and prefetching is likely not advantageous.
+.It Dv POSIX_FADV_SEQUENTIAL
+Tells the system that file data will be accessed sequentially.
+This currently does nothing as the default behavior uses heuristics to
+detect sequential behavior.
+.It Dv POSIX_FADV_WILLNEED
+Tells the system that the specified data will be accessed in the near future.
+The system may initiate an asynchronous read of the data if it is not already
+present in memory.
+.It Dv POSIX_FADV_DONTNEED
+Tells the system that the specified data will not be accessed in the near
+future.
+The system may decrease the in-memory priority of clean data within the
+specified range and future access to this data may require a read operation.
+.It Dv POSIX_FADV_NOREUSE
+Tells the system that the specified data will only be accessed once and
+then not reused.
+The system may decrease the in-memory priority of data once it has been
+read or written.
+Future access to this data may require a read operation.
+.El
+.Sh RETURN VALUES
+If successful,
+.Fn posix_fadvise
+returns zero.
+It returns an error on failure, without setting
+.Va errno .
+.Sh ERRORS
+Possible failure conditions:
+.Bl -tag -width Er
+.It Bq Er EBADF
+The
+.Fa fd
+argument is not a valid file descriptor.
+.It Bq Er EINVAL
+The
+.Fa advice
+argument is not valid.
+.It Bq Er EINVAL
+The
+.Fa offset
+or
+.Fa len
+arguments are negative,
+or
+.Fa offset
++
+.Fa len
+is greater than the maximum file size.
+.It Bq Er ENODEV
+The
+.Fa fd
+argument does not refer to a regular file.
+.It Bq Er ESPIPE
+The
+.Fa fd
+argument is associated with a pipe or FIFO.
+.El
+.Sh SEE ALSO
+.Xr madvise 2
+.Sh STANDARDS
+The
+.Fn posix_fadvise
+interface conforms to
+.St -p1003.1-2001 .
+.Sh HISTORY
+The
+.Fn posix_fadvise
+function first appeared in
+.Dx 5.5 .
diff --git a/lib/libc/stdlib/posix_fadvise.c b/lib/libc/stdlib/posix_fadvise.c
new file mode 100644
index 0000000000..8c6505089a
--- /dev/null
+++ b/lib/libc/stdlib/posix_fadvise.c
@@ -0,0 +1,51 @@
+/*-
+ * Copyright (c) 2019 The DragonFly Project.  All rights reserved.
+ *
+ * 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.
+ * 3. Neither the name of The DragonFly Project nor the names of its
+ *    contributors may be used to endorse or promote products derived
+ *   from this software without specific, prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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
+ * COPYRIGHT HOLDERS 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 <errno.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <machine/limits.h>
+
+int
+posix_fadvise(int fd, off_t offset, off_t len, int advice)
+{
+	struct stat buf;
+	int ret;
+
+	if ((offset < 0 || len < 0 || offset > OFF_MAX - len) ||
+	    (advice < POSIX_FADV_NORMAL || advice > POSIX_FADV_NOREUSE))
+		return (EINVAL);
+	if ((ret = fstat(fd, &buf)) != 0)
+		return (ret);
+	if (S_ISFIFO(buf.st_mode))
+		return (ESPIPE);
+	return (0);
+}
diff --git a/lib/liblzma/config.h b/lib/liblzma/config.h
index e345f14943..1be1f336fd 100644
--- a/lib/liblzma/config.h
+++ b/lib/liblzma/config.h
@@ -191,7 +191,7 @@
 #define HAVE_OPTRESET 1
 
 /* Define to 1 if you have the `posix_fadvise' function. */
-/* #undef HAVE_POSIX_FADVISE */
+#define HAVE_POSIX_FADVISE 1
 
 /* Define to 1 if you have the `pthread_condattr_setclock' function. */
 /* #undef HAVE_PTHREAD_CONDATTR_SETCLOCK */
diff --git a/sys/sys/fcntl.h b/sys/sys/fcntl.h
index dab780a827..788584bd8e 100644
--- a/sys/sys/fcntl.h
+++ b/sys/sys/fcntl.h
@@ -310,6 +310,18 @@ union fcntl_dat {
 #define	S_IXOTH	0000001			/* X for other */
 #endif
 
+#if __POSIX_VISIBLE >= 200112
+/*
+ * Advice to be applied to the file data
+ */
+#define	POSIX_FADV_NORMAL	0	/* no advice */
+#define	POSIX_FADV_SEQUENTIAL	1	/* expect sequential access */
+#define	POSIX_FADV_RANDOM	2	/* expect random access */
+#define	POSIX_FADV_WILLNEED	3	/* expect access in the near future */
+#define	POSIX_FADV_DONTNEED	4	/* expect no access in the near future */
+#define	POSIX_FADV_NOREUSE	5	/* expect access only once */
+#endif
+
 #if !defined(_KERNEL) || defined(_KERNEL_VIRTUAL)
 __BEGIN_DECLS
 int	open(const char *, int, ...);
@@ -321,9 +333,9 @@ int	fcntl(int, int, ...);
 #if __BSD_VISIBLE
 int	flock(int, int);
 #endif /* __BSD_VISIBLE */
-#if 0 /* XXX missing */
 #if __POSIX_VISIBLE >= 200112
 int	posix_fadvise(int, off_t, off_t, int);
+#if 0 /* XXX missing */
 int	posix_fallocate(int, off_t, off_t);
 #endif
 #endif
-- 
2.21.0

