1FSYNC(2)                   Linux Programmer's Manual                  FSYNC(2)
2
3
4

NAME

6       fsync,  fdatasync  -  synchronize  a  file's in-core state with storage
7       device
8

SYNOPSIS

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

DESCRIPTION

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)  so  that
26       all  changed information can be retrieved even after the system crashed
27       or was rebooted.  This includes writing  through  or  flushing  a  disk
28       cache  if  present.   The call blocks until the device reports that the
29       transfer has completed.  It also flushes metadata  information  associ‐
30       ated with the file (see stat(2)).
31
32       Calling  fsync()  does  not  necessarily  ensure  that the entry in the
33       directory containing the file has  also  reached  disk.   For  that  an
34       explicit fsync() on a file descriptor for the directory is also needed.
35
36       fdatasync() is similar to fsync(), but does not flush modified metadata
37       unless that metadata is needed in order  to  allow  a  subsequent  data
38       retrieval to be correctly handled.  For example, changes to st_atime or
39       st_mtime (respectively, time of last access and time of last  modifica‐
40       tion;  see stat(2)) do not require flushing because they are not neces‐
41       sary for a subsequent data read to be handled correctly.  On the  other
42       hand, a change to the file size (st_size, as made by say ftruncate(2)),
43       would require a metadata flush.
44
45       The aim of fdatasync() is to reduce disk activity for applications that
46       do not require all metadata to be synchronized with the disk.
47

RETURN VALUE

49       On  success, these system calls return zero.  On error, -1 is returned,
50       and errno is set appropriately.
51

ERRORS

53       EBADF  fd is not a valid open file descriptor.
54
55       EIO    An error occurred during synchronization.
56
57       EROFS, EINVAL
58              fd is bound to a special file which does  not  support  synchro‐
59              nization.
60

CONFORMING TO

62       4.3BSD, POSIX.1-2001.
63

AVAILABILITY

65       On  POSIX  systems  on  which fdatasync() is available, _POSIX_SYNCHRO‐
66       NIZED_IO is defined in <unistd.h> to a value greater than 0.  (See also
67       sysconf(3).)
68

NOTES

70       On  some  UNIX  systems  (but  not  Linux),  fd must be a writable file
71       descriptor.
72
73       In Linux 2.2 and earlier, fdatasync() is equivalent to fsync(), and  so
74       has no performance advantage.
75
76       The  fsync()  implementations in older kernels and lesser used filesys‐
77       tems does not know how to flush  disk  caches.   In  these  cases  disk
78       caches  need  to  be disabled using hdparm(8) or sdparm(8) to guarantee
79       safe operation.
80

SEE ALSO

82       bdflush(2), open(2), sync(2), sync_file_range(2), hdparm(8),  mount(8),
83       sync(8), update(8)
84

COLOPHON

86       This  page  is  part of release 3.53 of the Linux man-pages project.  A
87       description of the project, and information about reporting  bugs,  can
88       be found at http://www.kernel.org/doc/man-pages/.
89
90
91
92Linux                             2012-02-27                          FSYNC(2)
Impressum