From d3aa542a116268b4763bf76094bcc05deaee47c5 Mon Sep 17 00:00:00 2001
From: Sascha Wildner <saw@online.de>
Date: Tue, 14 Sep 2021 21:18:29 +0200
Subject: [PATCH] Various additions/changes related to
 fdatasync()/posix_fallocate().

---
 lib/libc/sys/Makefile.inc           |   4 +-
 lib/libc/sys/fsync.2                |  73 +++++++++++--
 lib/libc/sys/posix_fallocate.2      | 154 ++++++++++++++++++++++++++++
 lib/libc/sys/sigaction.2            |   4 +-
 lib/libpthread/pthread_testcancel.3 |   3 +-
 share/man/man9/Makefile             |   1 +
 share/man/man9/VOP_FSYNC.9          |  17 ++-
 sys/sys/vfsops.h                    |   2 +-
 test/stress/fsstress/global.h       |   4 -
 9 files changed, 243 insertions(+), 19 deletions(-)
 create mode 100644 lib/libc/sys/posix_fallocate.2

diff --git a/lib/libc/sys/Makefile.inc b/lib/libc/sys/Makefile.inc
index ea41cbb2c2f..79b4d2e55bb 100644
--- a/lib/libc/sys/Makefile.inc
+++ b/lib/libc/sys/Makefile.inc
@@ -84,7 +84,8 @@ MAN+=	_exit.2 accept.2 access.2 acct.2 adjtime.2 bind.2 \
 	msgctl.2 msgget.2 msgrcv.2 msgsnd.2 \
 	msync.2 munmap.2 nanosleep.2 \
 	nfssvc.2 ntp_adjtime.2 ntp_gettime.2 open.2 \
-	pathconf.2 pipe.2 poll.2 procctl.2 profil.2 pselect.2 ptrace.2 \
+	pathconf.2 pipe.2 poll.2 posix_fallocate.2 \
+	procctl.2 profil.2 pselect.2 ptrace.2 \
 	quotactl.2 \
 	read.2 readlink.2 reboot.2 recv.2 rename.2 revoke.2 rfork.2 rmdir.2 \
 	rtprio.2 __realpath.2 \
@@ -127,6 +128,7 @@ MLINKS+=extattr.2 extattr_delete_file.2 \
 MLINKS+=fhopen.2 fhstat.2 \
 	fhopen.2 fhstatfs.2 \
 	fhopen.2 fhstatvfs.2
+MLINKS+=fsync.2 fdatasync.2
 MLINKS+=getdirentries.2 _DIRENT_DIRSIZ.2 \
 	getdirentries.2 _DIRENT_NEXT.2 \
 	getdirentries.2 getdents.2
diff --git a/lib/libc/sys/fsync.2 b/lib/libc/sys/fsync.2
index f05b7d95be6..2a0f5afc818 100644
--- a/lib/libc/sys/fsync.2
+++ b/lib/libc/sys/fsync.2
@@ -1,5 +1,11 @@
 .\" Copyright (c) 1983, 1993
 .\"	The Regents of the University of California.  All rights reserved.
+.\" Copyright (c) 2016 The FreeBSD Foundation, Inc.
+.\" All rights reserved.
+.\"
+.\" Parts of this documentation were written by
+.\" Konstantin Belousov <kib@FreeBSD.org> under sponsorship
+.\" from the FreeBSD Foundation.
 .\"
 .\" Redistribution and use in source and binary forms, with or without
 .\" modification, are permitted provided that the following conditions
@@ -26,13 +32,12 @@
 .\" SUCH DAMAGE.
 .\"
 .\"     @(#)fsync.2	8.1 (Berkeley) 6/4/93
-.\" $FreeBSD: src/lib/libc/sys/fsync.2,v 1.6.2.8 2001/12/14 18:34:00 ru Exp $
-.\" $DragonFly: src/lib/libc/sys/fsync.2,v 1.2 2003/06/17 04:26:47 dillon Exp $
 .\"
-.Dd June 4, 1993
+.Dd September 14, 2021
 .Dt FSYNC 2
 .Os
 .Sh NAME
+.Nm fdatasync ,
 .Nm fsync
 .Nd "synchronise changes to a file"
 .Sh LIBRARY
@@ -40,41 +45,91 @@
 .Sh SYNOPSIS
 .In unistd.h
 .Ft int
+.Fn fdatasync "int fd"
+.Ft int
 .Fn fsync "int fd"
 .Sh DESCRIPTION
-.Fn Fsync
-causes all modified data and attributes of
+The
+.Fn fsync
+system call
+causes all modified data and attributes of the file referenced by
+the file descriptor
 .Fa fd
 to be moved to a permanent storage device.
 This normally results in all in-core modified copies
 of buffers for the associated file to be written to a disk.
 .Pp
-.Fn Fsync
+The
+.Fn fdatasync
+system call causes all modified data of
+.Fa fd
+to be moved to a permanent storage device.
+Unlike
+.Fn fsync ,
+the system call does not guarantee that file attributes or
+metadata necessary to access the file are committed to the permanent storage.
+.Pp
+The
+.Fn fsync
+system call
 should be used by programs that require a file to be
 in a known state, for example, in building a simple transaction
 facility.
+If the file metadata has already been committed, using
+.Fn fdatasync
+can be more efficient than
+.Fn fsync .
+.Pp
+Both
+.Fn fdatasync
+and
+.Fn fsync
+calls are cancellation points.
 .Sh RETURN VALUES
-.Rv -std fsync
+.Rv -std fdatasync fsync
 .Sh ERRORS
 The
 .Fn fsync
-fails if:
+and
+.Fn fdatasync
+calls fail if:
 .Bl -tag -width Er
 .It Bq Er EBADF
+The
 .Fa fd
+argument
 is not a valid descriptor.
 .It Bq Er EINVAL
+The
 .Fa fd
+argument
 refers to a socket, not to a file.
 .It Bq Er EIO
 An I/O error occurred while reading from or writing to the file system.
+.\".It Bq Er EINTEGRITY
+.\"Corrupted data was detected while reading from the file system.
 .El
 .Sh SEE ALSO
+.Xr fsync 1 ,
 .Xr sync 2 ,
 .Xr syncer 4 ,
 .Xr sync 8
 .Sh HISTORY
 The
 .Fn fsync
-function call appeared in
+system call appeared in
 .Bx 4.2 .
+The
+.Fn fdatasync
+system call appeared in
+.Fx 11.1
+and was ported to
+.Dx 6.1 .
+.\".Sh BUGS
+.\"The
+.\".Fn fdatasync
+.\"system call currently does not guarantee that enqueued
+.\".Xr aio 4
+.\"requests for the file referenced by
+.\".Fa fd
+.\"are completed before the syscall returns.
diff --git a/lib/libc/sys/posix_fallocate.2 b/lib/libc/sys/posix_fallocate.2
new file mode 100644
index 00000000000..55b0c3bbc65
--- /dev/null
+++ b/lib/libc/sys/posix_fallocate.2
@@ -0,0 +1,154 @@
+.\" Copyright (c) 1980, 1991, 1993
+.\"	The Regents of the University of California.  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 University 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 REGENTS 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 REGENTS 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.
+.\"
+.\"     @(#)open.2	8.2 (Berkeley) 11/16/93
+.\" $FreeBSD$
+.\"
+.Dd September 14, 2021
+.Dt POSIX_FALLOCATE 2
+.Os
+.Sh NAME
+.Nm posix_fallocate
+.Nd pre-allocate storage for a range in a file
+.Sh LIBRARY
+.Lb libc
+.Sh SYNOPSIS
+.In fcntl.h
+.Ft int
+.Fn posix_fallocate "int fd" "off_t offset" "off_t len"
+.Sh DESCRIPTION
+Required storage for the range
+.Fa offset
+to
+.Fa offset +
+.Fa len
+in the file referenced by
+.Fa fd
+is guaranteed to be allocated upon successful return.
+That is, if
+.Fn posix_fallocate
+returns successfully, subsequent writes to the specified file data
+will not fail due to lack of free space on the file system storage
+media.
+Any existing file data in the specified range is unmodified.
+If
+.Fa offset +
+.Fa len
+is beyond the current file size, then
+.Fn posix_fallocate
+will adjust the file size to
+.Fa offset +
+.Fa len .
+Otherwise, the file size will not be changed.
+.Pp
+Space allocated by
+.Fn posix_fallocate
+will be freed by a successful call to
+.Xr creat 2
+or
+.Xr open 2
+that truncates the size of the file.
+Space allocated via
+.Fn posix_fallocate
+may be freed by a successful call to
+.Xr ftruncate 2
+that reduces the file size to a size smaller than
+.Fa offset +
+.Fa len .
+.Sh RETURN VALUES
+If successful,
+.Fn posix_fallocate
+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 EBADF
+The
+.Fa fd
+argument references a file that was opened without write permission.
+.It Bq Er EFBIG
+The value of
+.Fa offset +
+.Fa len
+is greater than the maximum file size.
+.It Bq Er EINTR
+A signal was caught during execution.
+.It Bq Er EINVAL
+The
+.Fa len
+argument was less than or equal to zero, the
+.Fa offset
+argument was less than zero,
+or the operation is not supported by the file system.
+.It Bq Er EIO
+An I/O error occurred while reading from or writing to a file system.
+.\".It Bq Er EINTEGRITY
+.\"Corrupted data was detected while reading from the file system.
+.It Bq Er ENODEV
+The
+.Fa fd
+argument does not refer to a file that supports
+.Nm .
+.It Bq Er ENOSPC
+There is insufficient free space remaining on the file system storage
+media.
+.\".It Bq Er ENOTCAPABLE
+.\"The file descriptor
+.\".Fa fd
+.\"has insufficient rights.
+.It Bq Er ESPIPE
+The
+.Fa fd
+argument is associated with a pipe or FIFO.
+.El
+.Sh SEE ALSO
+.Xr creat 2 ,
+.Xr ftruncate 2 ,
+.Xr open 2 ,
+.Xr unlink 2
+.Sh STANDARDS
+The
+.Fn posix_fallocate
+system call conforms to
+.St -p1003.1-2004 .
+.Sh HISTORY
+The
+.Fn posix_fallocate
+function appeared in
+.Fx 9.0
+and was ported to
+.Dx 6.1 .
+.Sh AUTHORS
+.Fn posix_fallocate
+and this manual page were initially written by
+.An Matthew Fleming Aq Mt mdf@FreeBSD.org .
diff --git a/lib/libc/sys/sigaction.2 b/lib/libc/sys/sigaction.2
index 701946075a8..acdfda4f1fe 100644
--- a/lib/libc/sys/sigaction.2
+++ b/lib/libc/sys/sigaction.2
@@ -28,7 +28,7 @@
 .\"	From: @(#)sigaction.2	8.2 (Berkeley) 4/3/94
 .\" $FreeBSD: src/lib/libc/sys/sigaction.2,v 1.22.2.10 2002/12/29 16:35:34 schweikh Exp $
 .\"
-.Dd July 20, 2016
+.Dd September 14, 2021
 .Dt SIGACTION 2
 .Os
 .Sh NAME
@@ -462,7 +462,7 @@ Realtime Interfaces:
 .Fn sigpause ,
 .\".Fn timer_getoverrun ,
 .Fn aio_return ,
-.\".Fn fdatasync ,
+.Fn fdatasync ,
 .\".Fn sigqueue ,
 .\".Fn timer_gettime ,
 .Fn aio_suspend ,
diff --git a/lib/libpthread/pthread_testcancel.3 b/lib/libpthread/pthread_testcancel.3
index 7f0771e84eb..f541dd8d18f 100644
--- a/lib/libpthread/pthread_testcancel.3
+++ b/lib/libpthread/pthread_testcancel.3
@@ -1,5 +1,5 @@
 .\" $FreeBSD: src/share/man/man3/pthread_testcancel.3,v 1.15 2007/10/22 10:08:01 ru Exp $
-.Dd January 17, 2021
+.Dd September 14, 2021
 .Dt PTHREAD_TESTCANCEL 3
 .Os
 .Sh NAME
@@ -103,6 +103,7 @@ functions:
 .Fn close ,
 .Fn creat ,
 .Fn fcntl ,
+.Fn fdatasync ,
 .Fn fsync ,
 .Fn msync ,
 .Fn nanosleep ,
diff --git a/share/man/man9/Makefile b/share/man/man9/Makefile
index 674b3f83695..877db05143d 100644
--- a/share/man/man9/Makefile
+++ b/share/man/man9/Makefile
@@ -1068,6 +1068,7 @@ MLINKS+=vn_lock.9 vn_islocked.9 \
 	vn_lock.9 vn_unlock.9
 MLINKS+=VOP_ATTRIB.9 VOP_GETATTR.9 \
 	VOP_ATTRIB.9 VOP_SETATTR.9
+MLINKS+=VOP_FSYNC.9 VOP_FDATASYNC.9
 MLINKS+=VOP_GETPAGES.9 VOP_PUTPAGES.9
 MLINKS+=VOP_INACTIVE.9 VOP_RECLAIM.9
 MLINKS+=VOP_OLD_CREATE.9 VOP_OLD_MKDIR.9 \
diff --git a/share/man/man9/VOP_FSYNC.9 b/share/man/man9/VOP_FSYNC.9
index f03902cb5a1..49b514d4e12 100644
--- a/share/man/man9/VOP_FSYNC.9
+++ b/share/man/man9/VOP_FSYNC.9
@@ -26,16 +26,19 @@
 .\"
 .\" $FreeBSD: src/share/man/man9/VOP_FSYNC.9,v 1.6.2.2 2001/12/17 11:30:18 ru Exp $
 .\"
-.Dd October 8, 2014
+.Dd September 14, 2021
 .Dt VOP_FSYNC 9
 .Os
 .Sh NAME
+.Nm VOP_FDATASYNC ,
 .Nm VOP_FSYNC
 .Nd flush filesystem buffers for a file
 .Sh SYNOPSIS
 .In sys/param.h
 .In sys/vnode.h
 .Ft int
+.Fn VOP_FDATASYNC "struct vnode *vp" "int waitfor" "int flags"
+.Ft int
 .Fn VOP_FSYNC "struct vnode *vp" "int waitfor" "int flags"
 .Sh DESCRIPTION
 This call flushes any dirty filesystem buffers for the file.
@@ -80,6 +83,18 @@ call.
 The only supported flag at the moment is
 .Dv VOP_FSYNC_SYSCALL
 which indicates the file synchronization was requested from userland.
+.Pp
+.Fn VOP_FDATASYNC
+is similar, but it does not require that all of the file's metadata be flushed.
+It only requires that the file's data be recoverable after a crash.
+That implies that the data itself must be flushed to disk, as well as some
+metadata such as the file's size but not necessarily its attributes.
+.Fn VOP_FDATASYNC
+should always wait for I/O to complete, as if called with
+.Dv MNT_WAIT .
+.Fn VOP_FDATASYNC
+is used to implement
+.Xr fdatasync 2 .
 .Sh RETURN VALUES
 Zero is returned if the call is successful, otherwise an appropriate
 error code is returned.
diff --git a/sys/sys/vfsops.h b/sys/sys/vfsops.h
index 5b4a6cc2b7e..f3ad0419139 100644
--- a/sys/sys/vfsops.h
+++ b/sys/sys/vfsops.h
@@ -1075,7 +1075,7 @@ extern struct syslink_desc vop_nrename_desc;
 	VOP_FSYNC_FP(vp, waitfor, flags, NULL)
 #define VOP_FDATASYNC_FP(vp, waitfor, flags, fp)	\
 	vop_fdatasync(*(vp)->v_ops, vp, waitfor, flags, fp) /* FUSE*/
-#define VOP_FDATASYNC(vp, waitfor, flags, fp)	\
+#define VOP_FDATASYNC(vp, waitfor, flags)		\
 	VOP_FDATASYNC_FP(*(vp)->v_ops, vp, waitfor, flags, NULL)
 #define VOP_READDIR_FP(vp, uio, cred, eofflag, ncookies, cookies, fp)	\
 	vop_readdir(*(vp)->v_ops, vp, uio, cred, eofflag, ncookies, cookies, fp) /* FUSE */
diff --git a/test/stress/fsstress/global.h b/test/stress/fsstress/global.h
index 1d89cc49c54..28b70ee0f47 100644
--- a/test/stress/fsstress/global.h
+++ b/test/stress/fsstress/global.h
@@ -29,9 +29,6 @@
  * 
  * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
  */
-/*
- * $DragonFly: src/test/stress/fsstress/global.h,v 1.2 2007/05/12 21:46:49 swildner Exp $
- */
  
 #ifndef GLOBAL_H
 #define GLOBAL_H
@@ -81,7 +78,6 @@ typedef off_t	off64_t;
 #define ftruncate64	ftruncate
 #define truncate64	truncate
 #define readdir64	readdir
-#define fdatasync	fsync
 
 static __inline
 void *
-- 
2.32.0

