1FSYNC(2) Linux Programmer's Manual FSYNC(2)
2
3
4
6 fsync, fdatasync - synchronize a file's in-core state with storage
7 device
8
10 #include <unistd.h>
11
12 int fsync(int fd);
13
14 int fdatasync(int fd);
15
16 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
17
18 fsync():
19 Glibc 2.16 and later:
20 No feature test macros need be defined
21 Glibc up to and including 2.15:
22 _BSD_SOURCE || _XOPEN_SOURCE
23 || /* since glibc 2.8: */ _POSIX_C_SOURCE >= 200112L
24 fdatasync():
25 _POSIX_C_SOURCE >= 199309L || _XOPEN_SOURCE >= 500
26
28 fsync() transfers ("flushes") all modified in-core data of (i.e., modi‐
29 fied buffer cache pages for) the file referred to by the file descrip‐
30 tor fd to the disk device (or other permanent storage device) so that
31 all changed information can be retrieved even if the system crashes or
32 is rebooted. This includes writing through or flushing a disk cache if
33 present. The call blocks until the device reports that the transfer
34 has completed.
35
36 As well as flushing the file data, fsync() also flushes the metadata
37 information associated with the file (see inode(7)).
38
39 Calling fsync() does not necessarily ensure that the entry in the
40 directory containing the file has also reached disk. For that an
41 explicit fsync() on a file descriptor for the directory is also needed.
42
43 fdatasync() is similar to fsync(), but does not flush modified metadata
44 unless that metadata is needed in order to allow a subsequent data
45 retrieval to be correctly handled. For example, changes to st_atime or
46 st_mtime (respectively, time of last access and time of last modifica‐
47 tion; see inode(7)) do not require flushing because they are not neces‐
48 sary for a subsequent data read to be handled correctly. On the other
49 hand, a change to the file size (st_size, as made by say ftruncate(2)),
50 would require a metadata flush.
51
52 The aim of fdatasync() is to reduce disk activity for applications that
53 do not require all metadata to be synchronized with the disk.
54
56 On success, these system calls return zero. On error, -1 is returned,
57 and errno is set appropriately.
58
60 EBADF fd is not a valid open file descriptor.
61
62 EIO An error occurred during synchronization. This error may relate
63 to data written to some other file descriptor on the same file.
64 Since Linux 4.13, errors from write-back will be reported to all
65 file descriptors that might have written the data which trig‐
66 gered the error. Some filesystems (e.g., NFS) keep close track
67 of which data came through which file descriptor, and give more
68 precise reporting. Other filesystems (e.g., most local filesys‐
69 tems) will report errors to all file descriptors that were open
70 on the file when the error was recorded.
71
72 ENOSPC Disk space was exhausted while synchronizing.
73
74 EROFS, EINVAL
75 fd is bound to a special file (e.g., a pipe, FIFO, or socket)
76 which does not support synchronization.
77
78 ENOSPC, EDQUOT
79 fd is bound to a file on NFS or another filesystem which does
80 not allocate space at the time of a write(2) system call, and
81 some previous write failed due to insufficient storage space.
82
84 POSIX.1-2001, POSIX.1-2008, 4.3BSD.
85
86 On POSIX systems on which fdatasync() is available, _POSIX_SYNCHRO‐
87 NIZED_IO is defined in <unistd.h> to a value greater than 0. (See also
88 sysconf(3).)
89
91 On some UNIX systems (but not Linux), fd must be a writable file
92 descriptor.
93
94 In Linux 2.2 and earlier, fdatasync() is equivalent to fsync(), and so
95 has no performance advantage.
96
97 The fsync() implementations in older kernels and lesser used filesys‐
98 tems do not know how to flush disk caches. In these cases disk caches
99 need to be disabled using hdparm(8) or sdparm(8) to guarantee safe
100 operation.
101
103 sync(1), bdflush(2), open(2), posix_fadvise(2), pwritev(2), sync(2),
104 sync_file_range(2), fflush(3), fileno(3), hdparm(8), mount(8)
105
107 This page is part of release 5.07 of the Linux man-pages project. A
108 description of the project, information about reporting bugs, and the
109 latest version of this page, can be found at
110 https://www.kernel.org/doc/man-pages/.
111
112
113
114Linux 2020-06-09 FSYNC(2)