Start of man-pages-5.14: updating Changes and Changes.old
[man-pages.git] / man2 / fsync.2
blob0f070ed2c7a887fb1cad292ef615f4571a11c18a
1 .\" Copyright 1993 Rickard E. Faith (faith@cs.unc.edu) and
2 .\" and Copyright 2006 Michael Kerrisk <mtk.manpages@gmail.com>
3 .\"
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
8 .\"
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
13 .\"
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein.  The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
20 .\" professionally.
21 .\"
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
24 .\" %%%LICENSE_END
25 .\"
26 .\" Modified 21 Aug 1994 by Michael Chastain <mec@shell.portal.com>:
27 .\"   Removed note about old libc (pre-4.5.26) translating to 'sync'.
28 .\" Modified 15 Apr 1995 by Michael Chastain <mec@shell.portal.com>:
29 .\"   Added `see also' section.
30 .\" Modified 13 Apr 1996 by Markus Kuhn <mskuhn@cip.informatik.uni-erlangen.de>
31 .\"   Added remarks about fdatasync.
32 .\" Modified 31 Jan 1997 by Eric S. Raymond <esr@thyrsus.com>
33 .\" Modified 18 Apr 2001 by Andi Kleen
34 .\"   Fix description to describe what it really does; add a few caveats.
35 .\" 2006-04-28, mtk, substantial rewrite of various parts.
36 .\" 2012-02-27 Various changes by Christoph Hellwig <hch@lst.de>
37 .\"
38 .TH FSYNC 2 2021-03-22 "Linux" "Linux Programmer's Manual"
39 .SH NAME
40 fsync, fdatasync \- synchronize a file's in-core state with storage device
41 .SH SYNOPSIS
42 .nf
43 .B #include <unistd.h>
44 .PP
45 .BI "int fsync(int " fd );
46 .PP
47 .BI "int fdatasync(int " fd );
48 .fi
49 .PP
50 .RS -4
51 Feature Test Macro Requirements for glibc (see
52 .BR feature_test_macros (7)):
53 .RE
54 .PP
55 .nf
56 .BR fsync ():
57     Glibc 2.16 and later:
58         No feature test macros need be defined
59     Glibc up to and including 2.15:
60         _BSD_SOURCE || _XOPEN_SOURCE
61             || /* Since glibc 2.8: */ _POSIX_C_SOURCE >= 200112L
62 .fi
63 .PP
64 .BR fdatasync ():
65 .nf
66     _POSIX_C_SOURCE >= 199309L || _XOPEN_SOURCE >= 500
67 .fi
68 .SH DESCRIPTION
69 .BR fsync ()
70 transfers ("flushes") all modified in-core data of
71 (i.e., modified buffer cache pages for) the
72 file referred to by the file descriptor
73 .I fd
74 to the disk device (or other permanent storage device) so that all
75 changed information can be retrieved even if the system crashes or
76 is rebooted.
77 This includes writing through or flushing a disk cache if present.
78 The call blocks until the device reports that the transfer has completed.
79 .PP
80 As well as flushing the file data,
81 .BR fsync ()
82 also flushes the metadata information associated with the file (see
83 .BR inode (7)).
84 .PP
85 Calling
86 .BR fsync ()
87 does not necessarily ensure
88 that the entry in the directory containing the file has also reached disk.
89 For that an explicit
90 .BR fsync ()
91 on a file descriptor for the directory is also needed.
92 .PP
93 .BR fdatasync ()
94 is similar to
95 .BR fsync (),
96 but does not flush modified metadata unless that metadata
97 is needed in order to allow a subsequent data retrieval to be
98 correctly handled.
99 For example, changes to
100 .I st_atime
102 .I st_mtime
103 (respectively, time of last access and
104 time of last modification; see
105 .BR inode (7))
106 do not require flushing because they are not necessary for
107 a subsequent data read to be handled correctly.
108 On the other hand, a change to the file size
109 .RI ( st_size ,
110 as made by say
111 .BR ftruncate (2)),
112 would require a metadata flush.
114 The aim of
115 .BR fdatasync ()
116 is to reduce disk activity for applications that do not
117 require all metadata to be synchronized with the disk.
118 .SH RETURN VALUE
119 On success, these system calls return zero.
120 On error, \-1 is returned, and
121 .I errno
122 is set to indicate the error.
123 .SH ERRORS
125 .B EBADF
126 .I fd
127 is not a valid open file descriptor.
129 .B EIO
130 An error occurred during synchronization.
131 This error may relate to data written to some other file descriptor
132 on the same file.
133 Since Linux 4.13,
134 .\" commit 088737f44bbf6378745f5b57b035e57ee3dc4750
135 errors from write-back will be reported to
136 all file descriptors that might have written the data which triggered
137 the error.
138 Some filesystems (e.g., NFS) keep close track of which data
139 came through which file descriptor, and give more precise reporting.
140 Other filesystems (e.g., most local filesystems) will report errors to
141 all file descriptors that were open on the file when the error was recorded.
143 .B ENOSPC
144 Disk space was exhausted while synchronizing.
146 .BR EROFS ", " EINVAL
147 .I fd
148 is bound to a special file (e.g., a pipe, FIFO, or socket)
149 which does not support synchronization.
151 .BR ENOSPC ", " EDQUOT
152 .I fd
153 is bound to a file on NFS or another filesystem which does not allocate
154 space at the time of a
155 .BR write (2)
156 system call, and some previous write failed due to insufficient
157 storage space.
158 .SH CONFORMING TO
159 POSIX.1-2001, POSIX.1-2008, 4.3BSD.
161 On POSIX systems on which
162 .BR fdatasync ()
163 is available,
164 .B _POSIX_SYNCHRONIZED_IO
165 is defined in
166 .I <unistd.h>
167 to a value greater than 0.
168 (See also
169 .BR sysconf (3).)
170 .\" POSIX.1-2001: It shall be defined to -1 or 0 or 200112L.
171 .\" -1: unavailable, 0: ask using sysconf().
172 .\" glibc defines them to 1.
173 .SH NOTES
174 On some UNIX systems (but not Linux),
175 .I fd
176 must be a
177 .I writable
178 file descriptor.
180 In Linux 2.2 and earlier,
181 .BR fdatasync ()
182 is equivalent to
183 .BR fsync (),
184 and so has no performance advantage.
187 .BR fsync ()
188 implementations in older kernels and lesser used filesystems
189 do not know how to flush disk caches.
190 In these cases disk caches need to be disabled using
191 .BR hdparm (8)
193 .BR sdparm (8)
194 to guarantee safe operation.
195 .SH SEE ALSO
196 .BR sync (1),
197 .BR bdflush (2),
198 .BR open (2),
199 .BR posix_fadvise (2),
200 .BR pwritev (2),
201 .BR sync (2),
202 .BR sync_file_range (2),
203 .BR fflush (3),
204 .BR fileno (3),
205 .BR hdparm (8),
206 .BR mount (8)