1 .\" Copyright 1993 Rickard E. Faith (faith@cs.unc.edu) and
2 .\" and Copyright 2006 Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
6 .\" Modified 21 Aug 1994 by Michael Chastain <mec@shell.portal.com>:
7 .\" Removed note about old libc (pre-4.5.26) translating to 'sync'.
8 .\" Modified 15 Apr 1995 by Michael Chastain <mec@shell.portal.com>:
9 .\" Added `see also' section.
10 .\" Modified 13 Apr 1996 by Markus Kuhn <mskuhn@cip.informatik.uni-erlangen.de>
11 .\" Added remarks about fdatasync.
12 .\" Modified 31 Jan 1997 by Eric S. Raymond <esr@thyrsus.com>
13 .\" Modified 18 Apr 2001 by Andi Kleen
14 .\" Fix description to describe what it really does; add a few caveats.
15 .\" 2006-04-28, mtk, substantial rewrite of various parts.
16 .\" 2012-02-27 Various changes by Christoph Hellwig <hch@lst.de>
18 .TH fsync 2 (date) "Linux man-pages (unreleased)"
20 fsync, fdatasync \- synchronize a file's in-core state with storage device
23 .RI ( libc ", " \-lc )
26 .B #include <unistd.h>
28 .BI "int fsync(int " fd );
30 .BI "int fdatasync(int " fd );
34 Feature Test Macro Requirements for glibc (see
35 .BR feature_test_macros (7)):
41 No feature test macros need be defined
42 Glibc up to and including 2.15:
43 _BSD_SOURCE || _XOPEN_SOURCE
44 || /* Since glibc 2.8: */ _POSIX_C_SOURCE >= 200112L
49 _POSIX_C_SOURCE >= 199309L || _XOPEN_SOURCE >= 500
53 transfers ("flushes") all modified in-core data of
54 (i.e., modified buffer cache pages for) the
55 file referred to by the file descriptor
57 to the disk device (or other permanent storage device) so that all
58 changed information can be retrieved even if the system crashes or
60 This includes writing through or flushing a disk cache if present.
61 The call blocks until the device reports that the transfer has completed.
63 As well as flushing the file data,
65 also flushes the metadata information associated with the file (see
70 does not necessarily ensure
71 that the entry in the directory containing the file has also reached disk.
74 on a file descriptor for the directory is also needed.
79 but does not flush modified metadata unless that metadata
80 is needed in order to allow a subsequent data retrieval to be
82 For example, changes to
86 (respectively, time of last access and
87 time of last modification; see
89 do not require flushing because they are not necessary for
90 a subsequent data read to be handled correctly.
91 On the other hand, a change to the file size
95 would require a metadata flush.
99 is to reduce disk activity for applications that do not
100 require all metadata to be synchronized with the disk.
102 On success, these system calls return zero.
103 On error, \-1 is returned, and
105 is set to indicate the error.
110 is not a valid open file descriptor.
113 The function was interrupted by a signal; see
117 An error occurred during synchronization.
118 This error may relate to data written to some other file descriptor
121 .\" commit 088737f44bbf6378745f5b57b035e57ee3dc4750
122 errors from write-back will be reported to
123 all file descriptors that might have written the data which triggered
125 Some filesystems (e.g., NFS) keep close track of which data
126 came through which file descriptor, and give more precise reporting.
127 Other filesystems (e.g., most local filesystems) will report errors to
128 all file descriptors that were open on the file when the error was recorded.
131 Disk space was exhausted while synchronizing.
133 .BR EROFS ", " EINVAL
135 is bound to a special file (e.g., a pipe, FIFO, or socket)
136 which does not support synchronization.
138 .BR ENOSPC ", " EDQUOT
140 is bound to a file on NFS or another filesystem which does not allocate
141 space at the time of a
143 system call, and some previous write failed due to insufficient
146 POSIX.1-2001, POSIX.1-2008, 4.3BSD.
148 On POSIX systems on which
151 .B _POSIX_SYNCHRONIZED_IO
154 to a value greater than 0.
157 .\" POSIX.1-2001: It shall be defined to -1 or 0 or 200112L.
158 .\" -1: unavailable, 0: ask using sysconf().
159 .\" glibc defines them to 1.
161 On some UNIX systems (but not Linux),
167 In Linux 2.2 and earlier,
171 and so has no performance advantage.
175 implementations in older kernels and lesser used filesystems
176 do not know how to flush disk caches.
177 In these cases disk caches need to be disabled using
181 to guarantee safe operation.
186 .BR posix_fadvise (2),
189 .BR sync_file_range (2),