1*84d9c625SLionel Sambuc.\" $NetBSD: fsync.2,v 1.18 2013/09/22 10:02:05 apb Exp $ 22fe8fb19SBen Gras.\" 32fe8fb19SBen Gras.\" Copyright (c) 1983, 1993 42fe8fb19SBen Gras.\" The Regents of the University of California. All rights reserved. 52fe8fb19SBen Gras.\" 62fe8fb19SBen Gras.\" Redistribution and use in source and binary forms, with or without 72fe8fb19SBen Gras.\" modification, are permitted provided that the following conditions 82fe8fb19SBen Gras.\" are met: 92fe8fb19SBen Gras.\" 1. Redistributions of source code must retain the above copyright 102fe8fb19SBen Gras.\" notice, this list of conditions and the following disclaimer. 112fe8fb19SBen Gras.\" 2. Redistributions in binary form must reproduce the above copyright 122fe8fb19SBen Gras.\" notice, this list of conditions and the following disclaimer in the 132fe8fb19SBen Gras.\" documentation and/or other materials provided with the distribution. 142fe8fb19SBen Gras.\" 3. Neither the name of the University nor the names of its contributors 152fe8fb19SBen Gras.\" may be used to endorse or promote products derived from this software 162fe8fb19SBen Gras.\" without specific prior written permission. 172fe8fb19SBen Gras.\" 182fe8fb19SBen Gras.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 192fe8fb19SBen Gras.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 202fe8fb19SBen Gras.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 212fe8fb19SBen Gras.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 222fe8fb19SBen Gras.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 232fe8fb19SBen Gras.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 242fe8fb19SBen Gras.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 252fe8fb19SBen Gras.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 262fe8fb19SBen Gras.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 272fe8fb19SBen Gras.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 282fe8fb19SBen Gras.\" SUCH DAMAGE. 292fe8fb19SBen Gras.\" 302fe8fb19SBen Gras.\" @(#)fsync.2 8.1 (Berkeley) 6/4/93 312fe8fb19SBen Gras.\" 32*84d9c625SLionel Sambuc.Dd September 22, 2013 332fe8fb19SBen Gras.Dt FSYNC 2 342fe8fb19SBen Gras.Os 352fe8fb19SBen Gras.Sh NAME 362fe8fb19SBen Gras.Nm fsync , 372fe8fb19SBen Gras.Nm fsync_range 382fe8fb19SBen Gras.Nd "synchronize a file's in-core state with that on disk" 392fe8fb19SBen Gras.Sh LIBRARY 402fe8fb19SBen Gras.Lb libc 412fe8fb19SBen Gras.Sh SYNOPSIS 422fe8fb19SBen Gras.In unistd.h 432fe8fb19SBen Gras.Ft int 442fe8fb19SBen Gras.Fn fsync "int fd" 452fe8fb19SBen Gras.Ft int 462fe8fb19SBen Gras.Fn fsync_range "int fd" "int how" "off_t start" "off_t length" 472fe8fb19SBen Gras.Sh DESCRIPTION 482fe8fb19SBen Gras.Fn fsync 492fe8fb19SBen Grascauses all modified data and attributes of 502fe8fb19SBen Gras.Fa fd 51*84d9c625SLionel Sambucto be written to a permanent storage device. 522fe8fb19SBen GrasThis normally results in all in-core modified copies 532fe8fb19SBen Grasof buffers for the associated file to be written to a disk. 542fe8fb19SBen Gras.Pp 55*84d9c625SLionel Sambuc.Fn fsync_range 56*84d9c625SLionel Sambucis similar, but provides control over the region of the file 57*84d9c625SLionel Sambucto be synchronized, and the method of synchronization. 58*84d9c625SLionel Sambuc.Pp 59*84d9c625SLionel SambucThese functions should be used by programs that require a file to be 602fe8fb19SBen Grasin a known state, for example, in building a simple transaction 612fe8fb19SBen Grasfacility. 622fe8fb19SBen Gras.Pp 63*84d9c625SLionel SambucNote that writing the data to a permanent storage device 64*84d9c625SLionel Sambucdoes not necessarily write the data to permanent storage media 65*84d9c625SLionel Sambucwithin that device; 66*84d9c625SLionel Sambucfor example, after writing data to a disk device, the data might 67*84d9c625SLionel Sambucreside in a cache within the device, but not yet on 68*84d9c625SLionel Sambucmore permanent storage within the device. 69*84d9c625SLionel SambucNeither 70*84d9c625SLionel Sambuc.Fn fsync 71*84d9c625SLionel Sambucnor the default behavior of 72*84d9c625SLionel Sambuc.Fn fsync_range 73*84d9c625SLionel Sambuc(without the 74*84d9c625SLionel Sambuc.Dv FDISKSYNC 75*84d9c625SLionel Sambucflag) 76*84d9c625SLionel Sambucwill flush disk caches, 77*84d9c625SLionel Sambucbecause they assume that storage devices are able to ensure that 78*84d9c625SLionel Sambuccompleted writes are transferred to media some time between the 79*84d9c625SLionel Sambucwrite and a power failure or system crash. 80*84d9c625SLionel Sambuc.Pp 812fe8fb19SBen Gras.Fn fsync_range 822fe8fb19SBen Grascauses all modified data starting at 832fe8fb19SBen Gras.Fa start 842fe8fb19SBen Grasfor length 852fe8fb19SBen Gras.Fa length 862fe8fb19SBen Grasof 872fe8fb19SBen Gras.Fa fd 88*84d9c625SLionel Sambucto be written to a permanent storage device. 892fe8fb19SBen GrasIf the 902fe8fb19SBen Gras.Fa length 912fe8fb19SBen Grasparameter is zero, 922fe8fb19SBen Gras.Fn fsync_range 932fe8fb19SBen Graswill synchronize all of the file data. 94*84d9c625SLionel Sambuc.Pp 95*84d9c625SLionel Sambuc.Fn fsync_range 96*84d9c625SLionel Sambuctakes a 97*84d9c625SLionel Sambuc.Fa how 98*84d9c625SLionel Sambucparameter which contains one or more of the following flags: 99*84d9c625SLionel Sambuc.Bl -tag -width FDATASYNC -offset indent 100*84d9c625SLionel Sambuc.It Dv FDATASYNC 101*84d9c625SLionel SambucSynchronize the file data and sufficient meta-data to retrieve the 102*84d9c625SLionel Sambucdata for the specified range. 103*84d9c625SLionel SambucThis is equivalent to 104*84d9c625SLionel Sambuc.Xr fdatasync 2 105*84d9c625SLionel Sambucon the specified range. 106*84d9c625SLionel Sambuc.It Dv FFILESYNC 107*84d9c625SLionel SambucSynchronize all modified file data and meta-data for the specified range. 108*84d9c625SLionel SambucThis is equivalent to 109*84d9c625SLionel Sambuc.Xr fsync 2 110*84d9c625SLionel Sambucon the specified range. 111*84d9c625SLionel Sambuc.It Dv FDISKSYNC 112*84d9c625SLionel SambucRequest the destination device to ensure that the relevant data 113*84d9c625SLionel Sambucand meta-data is flushed from any cache to permanent storage media. 114*84d9c625SLionel SambucIn the present implementation, the entire cache on the affected device will 115*84d9c625SLionel Sambucbe flushed, and this may have a significant impact on performance. 116*84d9c625SLionel Sambuc.El 117*84d9c625SLionel Sambuc.Pp 118*84d9c625SLionel SambucThe 119*84d9c625SLionel Sambuc.Dv FDATASYNC 120*84d9c625SLionel Sambucand 121*84d9c625SLionel Sambuc.Dv FFILESYNC 122*84d9c625SLionel Sambucflags are mutually exclusive. 123*84d9c625SLionel SambucEither of those flags may be combined with the 124*84d9c625SLionel Sambuc.Dv FDISKSYNC 125*84d9c625SLionel Sambucflag. 126*84d9c625SLionel Sambuc.Pp 127*84d9c625SLionel SambucNote that 128*84d9c625SLionel Sambuc.Fn fsync_range 129*84d9c625SLionel Sambucrequires that the file 130*84d9c625SLionel Sambuc.Fa fd 131*84d9c625SLionel Sambucmust be open for writing, whereas 132*84d9c625SLionel Sambuc.Fn fsync 133*84d9c625SLionel Sambucdoes not. 1342fe8fb19SBen Gras.Sh RETURN VALUES 1352fe8fb19SBen GrasA 0 value is returned on success. 1362fe8fb19SBen GrasA \-1 value indicates an error. 1372fe8fb19SBen Gras.Sh ERRORS 1382fe8fb19SBen Gras.Fn fsync 1392fe8fb19SBen Grasor 1402fe8fb19SBen Gras.Fn fsync_range 1412fe8fb19SBen Grasfail if: 1422fe8fb19SBen Gras.Bl -tag -width Er 1432fe8fb19SBen Gras.It Bq Er EBADF 1442fe8fb19SBen Gras.Fa fd 1452fe8fb19SBen Grasis not a valid descriptor. 1462fe8fb19SBen Gras.It Bq Er EINVAL 1472fe8fb19SBen Gras.Fa fd 1482fe8fb19SBen Grasrefers to a socket, not to a file. 1492fe8fb19SBen Gras.It Bq Er EIO 1502fe8fb19SBen GrasAn I/O error occurred while reading from or writing to the file system. 1512fe8fb19SBen Gras.El 1522fe8fb19SBen Gras.Pp 1532fe8fb19SBen GrasAdditionally, 1542fe8fb19SBen Gras.Fn fsync_range 1552fe8fb19SBen Grasfails if: 1562fe8fb19SBen Gras.Bl -tag -width Er 1572fe8fb19SBen Gras.It Bq Er EBADF 1582fe8fb19SBen Gras.Fa fd 1592fe8fb19SBen Grasis not open for writing. 1602fe8fb19SBen Gras.It Bq Er EINVAL 1612fe8fb19SBen Gras.Fa start 1622fe8fb19SBen Gras+ 1632fe8fb19SBen Gras.Fa length 1642fe8fb19SBen Grasis less than 1652fe8fb19SBen Gras.Fa start . 1662fe8fb19SBen Gras.El 1672fe8fb19SBen Gras.Sh NOTES 1682fe8fb19SBen GrasFor optimal efficiency, the 1692fe8fb19SBen Gras.Fn fsync_range 1702fe8fb19SBen Grascall requires that the file system containing the file referenced by 1712fe8fb19SBen Gras.Fa fd 1722fe8fb19SBen Grassupport partial synchronization of file data. 1732fe8fb19SBen GrasFor file systems which do 1742fe8fb19SBen Grasnot support partial synchronization, the entire file will be synchronized 1752fe8fb19SBen Grasand the call will be the equivalent of calling 1762fe8fb19SBen Gras.Fn fsync . 1772fe8fb19SBen Gras.Sh SEE ALSO 178*84d9c625SLionel Sambuc.Xr fdatasync 2 , 1792fe8fb19SBen Gras.Xr sync 2 , 1802fe8fb19SBen Gras.Xr sync 8 1812fe8fb19SBen Gras.Sh HISTORY 1822fe8fb19SBen GrasThe 1832fe8fb19SBen Gras.Fn fsync 1842fe8fb19SBen Grasfunction call appeared in 1852fe8fb19SBen Gras.Bx 4.2 . 1862fe8fb19SBen Gras.Pp 1872fe8fb19SBen GrasThe 1882fe8fb19SBen Gras.Fn fsync_range 1892fe8fb19SBen Grasfunction call first appeared in 1902fe8fb19SBen Gras.Nx 2.0 1912fe8fb19SBen Grasand is modeled after the function available in AIX. 192