Bug 64875 - [libc] [patch] [request] add a system call: fdatasync()
Summary: [libc] [patch] [request] add a system call: fdatasync()
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: kern (show other bugs)
Version: Unspecified
Hardware: Any Any
: Normal Affects Only Me
Assignee: freebsd-standards (Nobody)
URL:
Keywords: patch
Depends on:
Blocks:
 
Reported: 2004-03-29 03:00 UTC by Kevin Lo
Modified: 2017-11-23 16:02 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Kevin Lo freebsd_committer freebsd_triage 2004-03-29 03:00:36 UTC
fdatasync()is part of realtime extensions in POSIX 1003.1.

DESCRIPTION
     The fdatasync() function forces  all  currently  queued  I/O
     operations  associated  with  the  file  indicated  by  file
     descriptor fildes to the synchronized I/O completion state.

How-To-Repeat: diff -ruN sys.orig/compat/freebsd32/syscalls.master sys/compat/freebsd32/syscalls.master
--- sys.orig/compat/freebsd32/syscalls.master	Fri Mar 26 11:18:23 2004
+++ sys/compat/freebsd32/syscalls.master	Fri Mar 26 11:36:30 2004
@@ -604,3 +604,4 @@
 439	UNIMPL	extattr_list_link
 440	UNIMPL	kse_switchin
 441	UNIMPL	ksem_timedwait
+442	UNIMPL	fdatasync
diff -ruN sys.orig/kern/syscalls.master sys/kern/syscalls.master
--- sys.orig/kern/syscalls.master	Fri Mar 26 11:18:59 2004
+++ sys/kern/syscalls.master	Fri Mar 26 11:36:49 2004
@@ -629,5 +629,6 @@
 440	MSTD	{ int kse_switchin(const struct __mcontext *mcp, \
 		    long val, long *loc); }
 441	MNOSTD	{ int ksem_timedwait(semid_t id, struct timespec *abstime); }
+442	STD	{ int fdatasync(int fd); }
 ; Please copy any additions and changes to the following compatability tables:
 ; sys/compat/freebsd32/syscalls.master
diff -ruN sys.orig/kern/vfs_syscalls.c sys/kern/vfs_syscalls.c
--- sys.orig/kern/vfs_syscalls.c	Fri Mar 26 11:19:00 2004
+++ sys/kern/vfs_syscalls.c	Fri Mar 26 14:00:14 2004
@@ -3122,6 +3122,48 @@
 }
 
 /*
+ * Sync the data of an open file.
+ */
+#ifndef _SYS_SYSPROTO_H_
+struct fdatasync_args {
+	int	fd;
+};
+#endif
+/* ARGSUSED */
+int
+fdatasync(td, uap)
+	struct thread *td;
+	struct fsync_args /* {
+		int fd;
+	} */ *uap;
+{
+	struct vnode *vp;
+	struct file *fp;
+	vm_object_t obj;
+	int error;
+
+	GIANT_REQUIRED;
+
+	if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
+		return (error);
+	if ((fp->f_flag & FWRITE) == 0) {
+		fdrop(fp, td);
+		return (EBADF);
+	}
+	vp = fp->f_vnode;
+	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
+	if (VOP_GETVOBJECT(vp, &obj) == 0) {
+		VM_OBJECT_LOCK(obj);
+		vm_object_page_clean(obj, 0, 0, 0);
+		VM_OBJECT_UNLOCK(obj);
+	}
+	error = VOP_FSYNC(vp, fp->f_cred, MNT_WAIT, td);
+	VOP_UNLOCK(vp, 0, td);
+	fdrop(fp, td);
+	return (error);
+}
+
+/*
  * Rename files.  Source and destination must either both be directories,
  * or both not be directories.  If target is a directory, it must be empty.
  */
--- /dev/null	Mon Mar 29 09:44:00 2004
+++ lib/libc/sys/fdatasync.2	Mon Mar 29 09:47:49 2004
@@ -0,0 +1,86 @@
+.\" Copyright (c) 2004
+.\"	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. All advertising materials mentioning features or use of this software
+.\"    must display the following acknowledgement:
+.\"	This product includes software developed by the University of
+.\"	California, Berkeley and its contributors.
+.\" 4. 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.
+.\"
+.\" $FreeBSD$
+.\"
+.Dd March 27, 2004
+.Dt FDATASYNC 2
+.Os
+.Sh NAME
+.Nm fdatasync
+.Nd "synchronize the data of a file"
+.Sh LIBRARY
+.Lb libc
+.Sh SYNOPSIS
+.In unistd.h
+.Ft int
+.Fn fdatasync "int fd"
+.Sh DESCRIPTION
+The
+.Fn fdatasync
+system call
+shall force all currently queued I/O operations associated
+with the file indicated by file descriptor
+.Fa fd
+to the synchronized I/O completion state.
+.Pp
+The
+functionality shall be equivalent to 
+.Fn fsync
+with the exception that all I/O operations shall be completed as 
+defined for synchronized I/O data integrity completion.
+.Sh RETURN VALUES
+.Rv -std fdatasync
+.Sh ERRORS
+The
+.Fn fdatasync
+fails 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.
+.El
+.Sh SEE ALSO
+.Xr fsync 2
+.Sh HISTORY
+The
+.Fn fdatasync
+system call appeared in
+.Fx 5.2 .
Comment 1 Tilman Keskinoz freebsd_committer freebsd_triage 2004-09-06 14:56:20 UTC
Responsible Changed
From-To: freebsd-bugs->freebsd-standards

Over to standards Mailinglist
Comment 2 David Schultz freebsd_committer freebsd_triage 2005-04-19 05:07:48 UTC
On Sun, Mar 28, 2004, Kevin Lo wrote:
> >Number:         64875
> >Category:       kern
> >Synopsis:       Add a system call: fdatasync()
[...]
> >Description:
> fdatasync()is part of realtime extensions in POSIX 1003.1.
> 
> DESCRIPTION
>      The fdatasync() function forces  all  currently  queued  I/O
>      operations  associated  with  the  file  indicated  by  file
>      descriptor fildes to the synchronized I/O completion state.
[...]
> +int
> +fdatasync(td, uap)
> +	struct thread *td;
> +	struct fsync_args /* {
> +		int fd;
> +	} */ *uap;
> +{
> +	struct vnode *vp;
> +	struct file *fp;
> +	vm_object_t obj;
> +	int error;
> +
> +	GIANT_REQUIRED;
> +
> +	if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
> +		return (error);
> +	if ((fp->f_flag & FWRITE) == 0) {
> +		fdrop(fp, td);
> +		return (EBADF);
> +	}
> +	vp = fp->f_vnode;
> +	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
> +	if (VOP_GETVOBJECT(vp, &obj) == 0) {
> +		VM_OBJECT_LOCK(obj);
> +		vm_object_page_clean(obj, 0, 0, 0);
> +		VM_OBJECT_UNLOCK(obj);
> +	}
> +	error = VOP_FSYNC(vp, fp->f_cred, MNT_WAIT, td);
> +	VOP_UNLOCK(vp, 0, td);
> +	fdrop(fp, td);
> +	return (error);
> +}

This is a good start, but there are several problems with the patch.

- The above is basically the same as fsync().  The whole point of
  fdatasync() is to be more efficient than fsync() by not syncing
  the file metadata, so it's misleading to implement it as an alias
  for fsync().  I think a correct implementation could be achieved
  by omitting the VOP_FSYNC() call and passing the OBJPC_SYNC flag
  to vm_object_page_clean(), but Alan should review this.

  - Regarding the above, what's supposed to happen if the file size
    changes, or if the file was recently created, and its inode has
    not been written at all yet?  I'm not sure whether fdatasync()
    is supposed to omit all metadata updates or just `unimportant'
    ones like atime and mtime.  If my suggestion above isn't
    adequate, the VOP_FSYNC() interface may need to be extended.

- The proposed documentation for fdatasync() needs to be rewritten,
  for two reasons.  First, although we do have permission to copy
  text from POSIX, it's still plagiarism to do so without
  acknowledgement.  Second, the POSIX documentation is not very
  helpful in this case; what's the ``synchronized I/O completion
  state'' anyway?
Comment 3 Mark Linimon freebsd_committer freebsd_triage 2005-10-25 06:38:40 UTC
State Changed
From-To: open->suspended

Mark as 'suspended' since this does not seem as though it is being 
actively worked on.
Comment 4 Pawel Biernacki freebsd_committer freebsd_triage 2017-11-23 15:50:13 UTC
fdatasync() was added in r304287.
Comment 5 Pawel Biernacki freebsd_committer freebsd_triage 2017-11-23 15:51:53 UTC
Meant to be r304176 :-)