]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/fsync.2
clock_getres.2, fsync.2, mlock.2, mmap.2, msync.2, INFINITY.3, cexp2.3, clog2.3,...
[thirdparty/man-pages.git] / man2 / fsync.2
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 2019-03-06 "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 .B #include <unistd.h>
43 .PP
44 .BI "int fsync(int " fd );
45 .PP
46 .BI "int fdatasync(int " fd );
47 .PP
48 .in -4n
49 Feature Test Macro Requirements for glibc (see
50 .BR feature_test_macros (7)):
51 .in
52 .PP
53 .BR fsync ():
54 Glibc 2.16 and later:
55 No feature test macros need be defined
56 Glibc up to and including 2.15:
57 _BSD_SOURCE || _XOPEN_SOURCE
58 || /* since glibc 2.8: */ _POSIX_C_SOURCE\ >=\ 200112L
59 .br
60 .BR fdatasync ():
61 _POSIX_C_SOURCE\ >=\ 199309L || _XOPEN_SOURCE\ >=\ 500
62 .SH DESCRIPTION
63 .BR fsync ()
64 transfers ("flushes") all modified in-core data of
65 (i.e., modified buffer cache pages for) the
66 file referred to by the file descriptor
67 .I fd
68 to the disk device (or other permanent storage device) so that all
69 changed information can be retrieved even if the system crashes or
70 is rebooted.
71 This includes writing through or flushing a disk cache if present.
72 The call blocks until the device reports that the transfer has completed.
73 .PP
74 As well as flushing the file data,
75 .BR fsync ()
76 also flushes the metadata information associated with the file (see
77 .BR inode (7)).
78 .PP
79 Calling
80 .BR fsync ()
81 does not necessarily ensure
82 that the entry in the directory containing the file has also reached disk.
83 For that an explicit
84 .BR fsync ()
85 on a file descriptor for the directory is also needed.
86 .PP
87 .BR fdatasync ()
88 is similar to
89 .BR fsync (),
90 but does not flush modified metadata unless that metadata
91 is needed in order to allow a subsequent data retrieval to be
92 correctly handled.
93 For example, changes to
94 .I st_atime
95 or
96 .I st_mtime
97 (respectively, time of last access and
98 time of last modification; see
99 .BR inode (7))
100 do not require flushing because they are not necessary for
101 a subsequent data read to be handled correctly.
102 On the other hand, a change to the file size
103 .RI ( st_size ,
104 as made by say
105 .BR ftruncate (2)),
106 would require a metadata flush.
107 .PP
108 The aim of
109 .BR fdatasync ()
110 is to reduce disk activity for applications that do not
111 require all metadata to be synchronized with the disk.
112 .SH RETURN VALUE
113 On success, these system calls return zero.
114 On error, \-1 is returned, and
115 .I errno
116 is set appropriately.
117 .SH ERRORS
118 .TP
119 .B EBADF
120 .I fd
121 is not a valid open file descriptor.
122 .TP
123 .B EIO
124 An error occurred during synchronization.
125 This error may relate to data written to some other file descriptor
126 on the same file.
127 Since Linux 4.13,
128 .\" commit 088737f44bbf6378745f5b57b035e57ee3dc4750
129 errors from write-back will be reported to
130 all file descriptors that might have written the data which triggered
131 the error.
132 Some filesystems (e.g., NFS) keep close track of which data
133 came through which file descriptor, and give more precise reporting.
134 Other filesystems (e.g., most local filesystems) will report errors to
135 all file descriptors that were open on the file when the error was recorded.
136 .TP
137 .B ENOSPC
138 Disk space was exhausted while synchronizing.
139 .TP
140 .BR EROFS ", " EINVAL
141 .I fd
142 is bound to a special file (e.g., a pipe, FIFO, or socket)
143 which does not support synchronization.
144 .TP
145 .BR ENOSPC ", " EDQUOT
146 .I fd
147 is bound to a file on NFS or another filesystem which does not allocate
148 space at the time of a
149 .BR write (2)
150 system call, and some previous write failed due to insufficient
151 storage space.
152 .SH CONFORMING TO
153 POSIX.1-2001, POSIX.1-2008, 4.3BSD.
154 .PP
155 On POSIX systems on which
156 .BR fdatasync ()
157 is available,
158 .B _POSIX_SYNCHRONIZED_IO
159 is defined in
160 .I <unistd.h>
161 to a value greater than 0.
162 (See also
163 .BR sysconf (3).)
164 .\" POSIX.1-2001: It shall be defined to -1 or 0 or 200112L.
165 .\" -1: unavailable, 0: ask using sysconf().
166 .\" glibc defines them to 1.
167 .SH NOTES
168 On some UNIX systems (but not Linux),
169 .I fd
170 must be a
171 .I writable
172 file descriptor.
173 .PP
174 In Linux 2.2 and earlier,
175 .BR fdatasync ()
176 is equivalent to
177 .BR fsync (),
178 and so has no performance advantage.
179 .PP
180 The
181 .BR fsync ()
182 implementations in older kernels and lesser used filesystems
183 do not know how to flush disk caches.
184 In these cases disk caches need to be disabled using
185 .BR hdparm (8)
186 or
187 .BR sdparm (8)
188 to guarantee safe operation.
189 .SH SEE ALSO
190 .BR sync (1),
191 .BR bdflush (2),
192 .BR open (2),
193 .BR posix_fadvise (2),
194 .BR pwritev (2),
195 .BR sync (2),
196 .BR sync_file_range (2),
197 .BR fflush (3),
198 .BR fileno (3),
199 .BR hdparm (8),
200 .BR mount (8)