signal.7: Since Linux 3.8, read(2) on an inotify FD is restartable with SA_RESTART
[man-pages.git] / man2 / fsync.2
blobb482727551a8a54a64e6497a3de6945573517b5e
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 2017-05-03 "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 .sp
44 .BI "int fsync(int " fd );
45 .sp
46 .BI "int fdatasync(int " fd );
47 .sp
48 .in -4n
49 Feature Test Macro Requirements for glibc (see
50 .BR feature_test_macros (7)):
51 .in
52 .sp
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 after the system crashed or
70 was 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 It also flushes metadata information associated with the file (see
74 .BR inode (7)).
76 Calling
77 .BR fsync ()
78 does not necessarily ensure
79 that the entry in the directory containing the file has also reached disk.
80 For that an explicit
81 .BR fsync ()
82 on a file descriptor for the directory is also needed.
84 .BR fdatasync ()
85 is similar to
86 .BR fsync (),
87 but does not flush modified metadata unless that metadata
88 is needed in order to allow a subsequent data retrieval to be
89 correctly handled.
90 For example, changes to
91 .I st_atime
93 .I st_mtime
94 (respectively, time of last access and
95 time of last modification; see
96 .BR inode (7))
97 do not require flushing because they are not necessary for
98 a subsequent data read to be handled correctly.
99 On the other hand, a change to the file size
100 .RI ( st_size ,
101 as made by say
102 .BR ftruncate (2)),
103 would require a metadata flush.
105 The aim of
106 .BR fdatasync ()
107 is to reduce disk activity for applications that do not
108 require all metadata to be synchronized with the disk.
109 .SH RETURN VALUE
110 On success, these system calls return zero.
111 On error, \-1 is returned, and
112 .I errno
113 is set appropriately.
114 .SH ERRORS
116 .B EBADF
117 .I fd
118 is not a valid open file descriptor.
120 .B EIO
121 An error occurred during synchronization.
123 .BR EROFS ", " EINVAL
124 .I fd
125 is bound to a special file (e.g., a pipe, FIFO, or socket)
126 which does not support synchronization.
127 .SH CONFORMING TO
128 POSIX.1-2001, POSIX.1-2008, 4.3BSD.
129 .SH AVAILABILITY
130 On POSIX systems on which
131 .BR fdatasync ()
132 is available,
133 .B _POSIX_SYNCHRONIZED_IO
134 is defined in
135 .I <unistd.h>
136 to a value greater than 0.
137 (See also
138 .BR sysconf (3).)
139 .\" POSIX.1-2001: It shall be defined to -1 or 0 or 200112L.
140 .\" -1: unavailable, 0: ask using sysconf().
141 .\" glibc defines them to 1.
142 .SH NOTES
143 On some UNIX systems (but not Linux),
144 .I fd
145 must be a
146 .I writable
147 file descriptor.
149 In Linux 2.2 and earlier,
150 .BR fdatasync ()
151 is equivalent to
152 .BR fsync (),
153 and so has no performance advantage.
156 .BR fsync ()
157 implementations in older kernels and lesser used filesystems
158 does not know how to flush disk caches.
159 In these cases disk caches need to be disabled using
160 .BR hdparm (8)
162 .BR sdparm (8)
163 to guarantee safe operation.
164 .SH SEE ALSO
165 .BR sync (1),
166 .BR bdflush (2),
167 .BR open (2),
168 .BR posix_fadvise (2),
169 .BR pwritev (2),
170 .BR sync (2),
171 .BR sync_file_range (2),
172 .BR fflush (3),
173 .BR fileno (3),
174 .BR hdparm (8),
175 .BR mount (8)