1.\" $NetBSD: fsync.2,v 1.18 2013/09/22 10:02:05 apb Exp $ 2.\" 3.\" Copyright (c) 1983, 1993 4.\" The Regents of the University of California. All rights reserved. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this list of conditions and the following disclaimer. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice, this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 3. Neither the name of the University nor the names of its contributors 15.\" may be used to endorse or promote products derived from this software 16.\" without specific prior written permission. 17.\" 18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28.\" SUCH DAMAGE. 29.\" 30.\" @(#)fsync.2 8.1 (Berkeley) 6/4/93 31.\" 32.Dd September 22, 2013 33.Dt FSYNC 2 34.Os 35.Sh NAME 36.Nm fsync , 37.Nm fsync_range 38.Nd "synchronize a file's in-core state with that on disk" 39.Sh LIBRARY 40.Lb libc 41.Sh SYNOPSIS 42.In unistd.h 43.Ft int 44.Fn fsync "int fd" 45.Ft int 46.Fn fsync_range "int fd" "int how" "off_t start" "off_t length" 47.Sh DESCRIPTION 48.Fn fsync 49causes all modified data and attributes of 50.Fa fd 51to be written to a permanent storage device. 52This normally results in all in-core modified copies 53of buffers for the associated file to be written to a disk. 54.Pp 55.Fn fsync_range 56is similar, but provides control over the region of the file 57to be synchronized, and the method of synchronization. 58.Pp 59These functions should be used by programs that require a file to be 60in a known state, for example, in building a simple transaction 61facility. 62.Pp 63Note that writing the data to a permanent storage device 64does not necessarily write the data to permanent storage media 65within that device; 66for example, after writing data to a disk device, the data might 67reside in a cache within the device, but not yet on 68more permanent storage within the device. 69Neither 70.Fn fsync 71nor the default behavior of 72.Fn fsync_range 73(without the 74.Dv FDISKSYNC 75flag) 76will flush disk caches, 77because they assume that storage devices are able to ensure that 78completed writes are transferred to media some time between the 79write and a power failure or system crash. 80.Pp 81.Fn fsync_range 82causes all modified data starting at 83.Fa start 84for length 85.Fa length 86of 87.Fa fd 88to be written to a permanent storage device. 89If the 90.Fa length 91parameter is zero, 92.Fn fsync_range 93will synchronize all of the file data. 94.Pp 95.Fn fsync_range 96takes a 97.Fa how 98parameter which contains one or more of the following flags: 99.Bl -tag -width FDATASYNC -offset indent 100.It Dv FDATASYNC 101Synchronize the file data and sufficient meta-data to retrieve the 102data for the specified range. 103This is equivalent to 104.Xr fdatasync 2 105on the specified range. 106.It Dv FFILESYNC 107Synchronize all modified file data and meta-data for the specified range. 108This is equivalent to 109.Xr fsync 2 110on the specified range. 111.It Dv FDISKSYNC 112Request the destination device to ensure that the relevant data 113and meta-data is flushed from any cache to permanent storage media. 114In the present implementation, the entire cache on the affected device will 115be flushed, and this may have a significant impact on performance. 116.El 117.Pp 118The 119.Dv FDATASYNC 120and 121.Dv FFILESYNC 122flags are mutually exclusive. 123Either of those flags may be combined with the 124.Dv FDISKSYNC 125flag. 126.Pp 127Note that 128.Fn fsync_range 129requires that the file 130.Fa fd 131must be open for writing, whereas 132.Fn fsync 133does not. 134.Sh RETURN VALUES 135A 0 value is returned on success. 136A \-1 value indicates an error. 137.Sh ERRORS 138.Fn fsync 139or 140.Fn fsync_range 141fail if: 142.Bl -tag -width Er 143.It Bq Er EBADF 144.Fa fd 145is not a valid descriptor. 146.It Bq Er EINVAL 147.Fa fd 148refers to a socket, not to a file. 149.It Bq Er EIO 150An I/O error occurred while reading from or writing to the file system. 151.El 152.Pp 153Additionally, 154.Fn fsync_range 155fails if: 156.Bl -tag -width Er 157.It Bq Er EBADF 158.Fa fd 159is not open for writing. 160.It Bq Er EINVAL 161.Fa start 162+ 163.Fa length 164is less than 165.Fa start . 166.El 167.Sh NOTES 168For optimal efficiency, the 169.Fn fsync_range 170call requires that the file system containing the file referenced by 171.Fa fd 172support partial synchronization of file data. 173For file systems which do 174not support partial synchronization, the entire file will be synchronized 175and the call will be the equivalent of calling 176.Fn fsync . 177.Sh SEE ALSO 178.Xr fdatasync 2 , 179.Xr sync 2 , 180.Xr sync 8 181.Sh HISTORY 182The 183.Fn fsync 184function call appeared in 185.Bx 4.2 . 186.Pp 187The 188.Fn fsync_range 189function call first appeared in 190.Nx 2.0 191and is modeled after the function available in AIX. 192