Wed Apr 22 18:18:26 2020 UTC ()
Pull up following revision(s) (requested by gdt in ticket #1534):

	sys/kern/vfs_syscalls.c: revision 1.544
	lib/libc/sys/fdatasync.2: revision 1.17

Relax fdatasync restriction that fd be writable

The restriction that a fd passed to fdatasync(2) must be writable was
added in 2003 in order to comply with POSIX.  Since then, POSIX has
removed that requirement, and POSIX-valid programs have been therefore
encountering errors on NetBSD.

Patch by Paul Ripke after discussion on netbsd-users.  Issue
discovered with pkgsrc/databases/mongodb3 as used by pkgsrc/net/unifi.


(martin)
diff -r1.16 -r1.16.56.1 src/lib/libc/sys/fdatasync.2
diff -r1.516 -r1.516.2.1 src/sys/kern/vfs_syscalls.c

cvs diff -r1.16 -r1.16.56.1 src/lib/libc/sys/fdatasync.2 (expand / switch to unified diff)

--- src/lib/libc/sys/fdatasync.2 2008/04/30 13:10:51 1.16
+++ src/lib/libc/sys/fdatasync.2 2020/04/22 18:18:26 1.16.56.1
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1.\" $NetBSD: fdatasync.2,v 1.16 2008/04/30 13:10:51 martin Exp $ 1.\" $NetBSD: fdatasync.2,v 1.16.56.1 2020/04/22 18:18:26 martin Exp $
2.\" 2.\"
3.\" Copyright (c) 1998 The NetBSD Foundation, Inc. 3.\" Copyright (c) 1998 The NetBSD Foundation, Inc.
4.\" All rights reserved. 4.\" All rights reserved.
5.\" 5.\"
6.\" This code is derived from software contributed to The NetBSD Foundation 6.\" This code is derived from software contributed to The NetBSD Foundation
7.\" by Klaus Klein. 7.\" by Klaus Klein.
8.\" 8.\"
9.\" Redistribution and use in source and binary forms, with or without 9.\" Redistribution and use in source and binary forms, with or without
10.\" modification, are permitted provided that the following conditions 10.\" modification, are permitted provided that the following conditions
11.\" are met: 11.\" are met:
12.\" 1. Redistributions of source code must retain the above copyright 12.\" 1. Redistributions of source code must retain the above copyright
13.\" notice, this list of conditions and the following disclaimer. 13.\" notice, this list of conditions and the following disclaimer.
14.\" 2. Redistributions in binary form must reproduce the above copyright 14.\" 2. Redistributions in binary form must reproduce the above copyright
@@ -58,39 +58,39 @@ synchronized I/O data integrity completi @@ -58,39 +58,39 @@ synchronized I/O data integrity completi
58.Sh RETURN VALUES 58.Sh RETURN VALUES
59A value of 0 is returned on success. 59A value of 0 is returned on success.
60Otherwise, a value \-1 is returned and 60Otherwise, a value \-1 is returned and
61.Va errno 61.Va errno
62is set to indicate the error. 62is set to indicate the error.
63.Sh ERRORS 63.Sh ERRORS
64The 64The
65.Fn fdatasync 65.Fn fdatasync
66function will fail if: 66function will fail if:
67.Bl -tag -width Er 67.Bl -tag -width Er
68.It Bq Er EBADF 68.It Bq Er EBADF
69The 69The
70.Fa fd 70.Fa fd
71argument is not a valid file descriptor open for writing. 71argument is not a valid file descriptor.
72.It Bq Er EINVAL 72.It Bq Er EINVAL
73This implementation does not support synchronized I/O for this file. 73This implementation does not support synchronized I/O for this file.
74.It Bq Er ENOSYS 74.It Bq Er ENOSYS
75The 75The
76.Fn fdatasync 76.Fn fdatasync
77function is not supported by this implementation. 77function is not supported by this implementation.
78.El 78.El
79.Pp 79.Pp
80In the event that any of the I/O operations to be performed fail, 80In the event that any of the I/O operations to be performed fail,
81.Fn fdatasync 81.Fn fdatasync
82returns the error conditions defined for 82returns the error conditions defined for
83.Xr read 2 83.Xr read 2
84and 84and
85.Xr write 2 , 85.Xr write 2 ,
86and outstanding I/O operations are not guaranteed to have been completed. 86and outstanding I/O operations are not guaranteed to have been completed.
87.Sh SEE ALSO 87.Sh SEE ALSO
88.Xr fsync 2 , 88.Xr fsync 2 ,
89.Xr open 2 , 89.Xr open 2 ,
90.Xr read 2 , 90.Xr read 2 ,
91.Xr write 2 91.Xr write 2
92.Sh STANDARDS 92.Sh STANDARDS
93The 93The
94.Fn fdatasync 94.Fn fdatasync
95function conforms to 95function conforms to
96.St -p1003.1b-93 . 96.St -p1003.1-2008 .

cvs diff -r1.516 -r1.516.2.1 src/sys/kern/vfs_syscalls.c (expand / switch to unified diff)

--- src/sys/kern/vfs_syscalls.c 2017/06/01 02:45:13 1.516
+++ src/sys/kern/vfs_syscalls.c 2020/04/22 18:18:26 1.516.2.1
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: vfs_syscalls.c,v 1.516 2017/06/01 02:45:13 chs Exp $ */ 1/* $NetBSD: vfs_syscalls.c,v 1.516.2.1 2020/04/22 18:18:26 martin Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc. 4 * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to The NetBSD Foundation 7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Doran. 8 * by Andrew Doran.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
@@ -60,27 +60,27 @@ @@ -60,27 +60,27 @@
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * SUCH DAMAGE. 63 * SUCH DAMAGE.
64 * 64 *
65 * @(#)vfs_syscalls.c 8.42 (Berkeley) 7/31/95 65 * @(#)vfs_syscalls.c 8.42 (Berkeley) 7/31/95
66 */ 66 */
67 67
68/* 68/*
69 * Virtual File System System Calls 69 * Virtual File System System Calls
70 */ 70 */
71 71
72#include <sys/cdefs.h> 72#include <sys/cdefs.h>
73__KERNEL_RCSID(0, "$NetBSD: vfs_syscalls.c,v 1.516 2017/06/01 02:45:13 chs Exp $"); 73__KERNEL_RCSID(0, "$NetBSD: vfs_syscalls.c,v 1.516.2.1 2020/04/22 18:18:26 martin Exp $");
74 74
75#ifdef _KERNEL_OPT 75#ifdef _KERNEL_OPT
76#include "opt_fileassoc.h" 76#include "opt_fileassoc.h"
77#include "veriexec.h" 77#include "veriexec.h"
78#endif 78#endif
79 79
80#include <sys/param.h> 80#include <sys/param.h>
81#include <sys/systm.h> 81#include <sys/systm.h>
82#include <sys/namei.h> 82#include <sys/namei.h>
83#include <sys/filedesc.h> 83#include <sys/filedesc.h>
84#include <sys/kernel.h> 84#include <sys/kernel.h>
85#include <sys/file.h> 85#include <sys/file.h>
86#include <sys/fcntl.h> 86#include <sys/fcntl.h>
@@ -3994,28 +3994,27 @@ sys_fsync(struct lwp *l, const struct sy @@ -3994,28 +3994,27 @@ sys_fsync(struct lwp *l, const struct sy
3994 return (error); 3994 return (error);
3995 vp = fp->f_vnode; 3995 vp = fp->f_vnode;
3996 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 3996 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3997 error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT, 0, 0); 3997 error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT, 0, 0);
3998 VOP_UNLOCK(vp); 3998 VOP_UNLOCK(vp);
3999 fd_putfile(SCARG(uap, fd)); 3999 fd_putfile(SCARG(uap, fd));
4000 return (error); 4000 return (error);
4001} 4001}
4002 4002
4003/* 4003/*
4004 * Sync a range of file data. API modeled after that found in AIX. 4004 * Sync a range of file data. API modeled after that found in AIX.
4005 * 4005 *
4006 * FDATASYNC indicates that we need only save enough metadata to be able 4006 * FDATASYNC indicates that we need only save enough metadata to be able
4007 * to re-read the written data. Note we duplicate AIX's requirement that 4007 * to re-read the written data.
4008 * the file be open for writing. 
4009 */ 4008 */
4010/* ARGSUSED */ 4009/* ARGSUSED */
4011int 4010int
4012sys_fsync_range(struct lwp *l, const struct sys_fsync_range_args *uap, register_t *retval) 4011sys_fsync_range(struct lwp *l, const struct sys_fsync_range_args *uap, register_t *retval)
4013{ 4012{
4014 /* { 4013 /* {
4015 syscallarg(int) fd; 4014 syscallarg(int) fd;
4016 syscallarg(int) flags; 4015 syscallarg(int) flags;
4017 syscallarg(off_t) start; 4016 syscallarg(off_t) start;
4018 syscallarg(off_t) length; 4017 syscallarg(off_t) length;
4019 } */ 4018 } */
4020 struct vnode *vp; 4019 struct vnode *vp;
4021 file_t *fp; 4020 file_t *fp;
@@ -4076,30 +4075,26 @@ out: @@ -4076,30 +4075,26 @@ out:
4076int 4075int
4077sys_fdatasync(struct lwp *l, const struct sys_fdatasync_args *uap, register_t *retval) 4076sys_fdatasync(struct lwp *l, const struct sys_fdatasync_args *uap, register_t *retval)
4078{ 4077{
4079 /* { 4078 /* {
4080 syscallarg(int) fd; 4079 syscallarg(int) fd;
4081 } */ 4080 } */
4082 struct vnode *vp; 4081 struct vnode *vp;
4083 file_t *fp; 4082 file_t *fp;
4084 int error; 4083 int error;
4085 4084
4086 /* fd_getvnode() will use the descriptor for us */ 4085 /* fd_getvnode() will use the descriptor for us */
4087 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0) 4086 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
4088 return (error); 4087 return (error);
4089 if ((fp->f_flag & FWRITE) == 0) { 
4090 fd_putfile(SCARG(uap, fd)); 
4091 return (EBADF); 
4092 } 
4093 vp = fp->f_vnode; 4088 vp = fp->f_vnode;
4094 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 4089 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4095 error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT|FSYNC_DATAONLY, 0, 0); 4090 error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT|FSYNC_DATAONLY, 0, 0);
4096 VOP_UNLOCK(vp); 4091 VOP_UNLOCK(vp);
4097 fd_putfile(SCARG(uap, fd)); 4092 fd_putfile(SCARG(uap, fd));
4098 return (error); 4093 return (error);
4099} 4094}
4100 4095
4101/* 4096/*
4102 * Rename files, (standard) BSD semantics frontend. 4097 * Rename files, (standard) BSD semantics frontend.
4103 */ 4098 */
4104/* ARGSUSED */ 4099/* ARGSUSED */
4105int 4100int