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(): _BSD_SOURCE || _XOPEN_SOURCE
19 || /* since glibc 2.8: */ _POSIX_C_SOURCE >= 200112L
20 fdatasync(): _POSIX_C_SOURCE >= 199309L || _XOPEN_SOURCE >= 500
21
23 fsync() transfers ("flushes") all modified in-core data of (i.e., modi‐
24 fied buffer cache pages for) the file referred to by the file descrip‐
25 tor fd to the disk device (or other permanent storage device) where
26 that file resides. The call blocks until the device reports that the
27 transfer has completed. It also flushes metadata information associ‐
28 ated with the file (see stat(2)).
29
30 Calling fsync() does not necessarily ensure that the entry in the
31 directory containing the file has also reached disk. For that an
32 explicit fsync() on a file descriptor for the directory is also needed.
33
34 fdatasync() is similar to fsync(), but does not flush modified metadata
35 unless that metadata is needed in order to allow a subsequent data
36 retrieval to be correctly handled. For example, changes to st_atime or
37 st_mtime (respectively, time of last access and time of last modifica‐
38 tion; see stat(2)) do not require flushing because they are not neces‐
39 sary for a subsequent data read to be handled correctly. On the other
40 hand, a change to the file size (st_size, as made by say ftruncate(2)),
41 would require a metadata flush.
42
43 The aim of fdatasync() is to reduce disk activity for applications that
44 do not require all metadata to be synchronized with the disk.
45
47 On success, these system calls return zero. On error, -1 is returned,
48 and errno is set appropriately.
49
51 EBADF fd is not a valid file descriptor open for writing.
52
53 EIO An error occurred during synchronization.
54
55 EROFS, EINVAL
56 fd is bound to a special file which does not support synchro‐
57 nization.
58
60 4.3BSD, POSIX.1-2001.
61
63 On POSIX systems on which fdatasync() is available, _POSIX_SYNCHRO‐
64 NIZED_IO is defined in <unistd.h> to a value greater than 0. (See also
65 sysconf(3).)
66
68 Applications that access databases or log files often write a tiny data
69 fragment (e.g., one line in a log file) and then call fsync() immedi‐
70 ately in order to ensure that the written data is physically stored on
71 the harddisk. Unfortunately, fsync() will always initiate two write
72 operations: one for the newly written data and another one in order to
73 update the modification time stored in the inode. If the modification
74 time is not a part of the transaction concept fdatasync() can be used
75 to avoid unnecessary inode disk write operations.
76
77 If the underlying hard disk has write caching enabled, then the data
78 may not really be on permanent storage when fsync() / fdatasync()
79 return.
80
81 When an ext2 file system is mounted with the sync option, directory
82 entries are also implicitly synced by fsync().
83
84 On kernels before 2.4, fsync() on big files can be inefficient. An
85 alternative might be to use the O_SYNC flag to open(2).
86
87 In Linux 2.2 and earlier, fdatasync() is equivalent to fsync(), and so
88 has no performance advantage.
89
91 bdflush(2), open(2), sync(2), sync_file_range(2), hdparm(8), mount(8),
92 sync(8), update(8)
93
95 This page is part of release 3.22 of the Linux man-pages project. A
96 description of the project, and information about reporting bugs, can
97 be found at http://www.kernel.org/doc/man-pages/.
98
99
100
101Linux 2008-11-07 FSYNC(2)