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
17 fsync() transfers ("flushes") all modified in-core data of (i.e., modi‐
18 fied buffer cache pages for) the file referred to by the file descrip‐
19 tor fd to the disk device (or other permanent storage device) where
20 that file resides. The call blocks until the device reports that the
21 transfer has completed. It also flushes metadata information associ‐
22 ated with the file (see stat(2)).
23
24 Calling fsync() does not necessarily ensure that the entry in the
25 directory containing the file has also reached disk. For that an
26 explicit fsync() on a file descriptor for the directory is also needed.
27
28 fdatasync() is similar to fsync(), but does not flush modified metadata
29 unless that metadata is needed in order to allow a subsequent data
30 retrieval to be correctly handled. For example, changes to st_atime or
31 st_mtime (respectively, time of last access and time of last modifica‐
32 tion; see stat(2)) do not require flushing because they are not neces‐
33 sary for a subsequent data read to be handled correctly. On the other
34 hand, a change to the file size (st_size, as made by say ftruncate(2)),
35 would require a metadata flush.
36
37 The aim of fdatasync(2) is to reduce disk activity for applications
38 that do not require all metadata to be synchronised with the disk.
39
41 On success, zero is returned. On error, -1 is returned, and errno is
42 set appropriately.
43
45 EBADF fd is not a valid file descriptor open for writing.
46
47 EIO An error occurred during synchronization.
48
49 EROFS, EINVAL
50 fd is bound to a special file which does not support synchro‐
51 nization.
52
54 If the underlying hard disk has write caching enabled, then the data
55 may not really be on permanent storage when fsync() / fdatasync()
56 return.
57
58 When an ext2 file system is mounted with the sync option, directory
59 entries are also implicitly synced by fsync().
60
61 On kernels before 2.4, fsync() on big files can be inefficient. An
62 alternative might be to use the O_SYNC flag to open(2).
63
65 4.3BSD, POSIX.1-2001
66
68 bdflush(2), open(2), sync(2), sync_file_range(2), hdparm(8), mount(8),
69 sync(8), update(8)
70
71
72
73Linux 1.3.85 2006-04-28 FSYNC(2)