1fsync(2) System Calls Manual fsync(2)
2
3
4
6 fsync, fdatasync - synchronize a file's in-core state with storage de‐
7 vice
8
10 Standard C library (libc, -lc)
11
13 #include <unistd.h>
14
15 int fsync(int fd);
16
17 int fdatasync(int fd);
18
19 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
20
21 fsync():
22 glibc 2.16 and later:
23 No feature test macros need be defined
24 glibc up to and including 2.15:
25 _BSD_SOURCE || _XOPEN_SOURCE
26 || /* Since glibc 2.8: */ _POSIX_C_SOURCE >= 200112L
27
28 fdatasync():
29 _POSIX_C_SOURCE >= 199309L || _XOPEN_SOURCE >= 500
30
32 fsync() transfers ("flushes") all modified in-core data of (i.e., modi‐
33 fied buffer cache pages for) the file referred to by the file descrip‐
34 tor fd to the disk device (or other permanent storage device) so that
35 all changed information can be retrieved even if the system crashes or
36 is rebooted. This includes writing through or flushing a disk cache if
37 present. The call blocks until the device reports that the transfer
38 has completed.
39
40 As well as flushing the file data, fsync() also flushes the metadata
41 information associated with the file (see inode(7)).
42
43 Calling fsync() does not necessarily ensure that the entry in the di‐
44 rectory containing the file has also reached disk. For that an ex‐
45 plicit fsync() on a file descriptor for the directory is also needed.
46
47 fdatasync() is similar to fsync(), but does not flush modified metadata
48 unless that metadata is needed in order to allow a subsequent data re‐
49 trieval to be correctly handled. For example, changes to st_atime or
50 st_mtime (respectively, time of last access and time of last modifica‐
51 tion; see inode(7)) do not require flushing because they are not neces‐
52 sary for a subsequent data read to be handled correctly. On the other
53 hand, a change to the file size (st_size, as made by say ftruncate(2)),
54 would require a metadata flush.
55
56 The aim of fdatasync() is to reduce disk activity for applications that
57 do not require all metadata to be synchronized with the disk.
58
60 On success, these system calls return zero. On error, -1 is returned,
61 and errno is set to indicate the error.
62
64 EBADF fd is not a valid open file descriptor.
65
66 EINTR The function was interrupted by a signal; see signal(7).
67
68 EIO An error occurred during synchronization. This error may relate
69 to data written to some other file descriptor on the same file.
70 Since Linux 4.13, errors from write-back will be reported to all