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

NAME

6       fsync,  fdatasync - synchronize a file's in-core state with storage de‐
7       vice
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():
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
25       fdatasync():
26           _POSIX_C_SOURCE >= 199309L || _XOPEN_SOURCE >= 500
27

DESCRIPTION

29       fsync() transfers ("flushes") all modified in-core data of (i.e., modi‐
30       fied  buffer cache pages for) the file referred to by the file descrip‐
31       tor fd to the disk device (or other permanent storage device)  so  that
32       all  changed information can be retrieved even if the system crashes or
33       is rebooted.  This includes writing through or flushing a disk cache if
34       present.   The  call  blocks until the device reports that the transfer
35       has completed.
36
37       As well as flushing the file data, fsync() also  flushes  the  metadata
38       information associated with the file (see inode(7)).
39
40       Calling  fsync()  does not necessarily ensure that the entry in the di‐
41       rectory containing the file has also reached disk.   For  that  an  ex‐
42       plicit fsync() on a file descriptor for the directory is also needed.
43
44       fdatasync() is similar to fsync(), but does not flush modified metadata
45       unless that metadata is needed in order to allow a subsequent data  re‐
46       trieval  to  be correctly handled.  For example, changes to st_atime or
47       st_mtime (respectively, time of last access and time of last  modifica‐
48       tion; see inode(7)) do not require flushing because they are not neces‐
49       sary for a subsequent data read to be handled correctly.  On the  other
50       hand, a change to the file size (st_size, as made by say ftruncate(2)),
51       would require a metadata flush.
52
53       The aim of fdatasync() is to reduce disk activity for applications that
54       do not require all metadata to be synchronized with the disk.
55

RETURN VALUE

57       On  success, these system calls return zero.  On error, -1 is returned,
58       and errno is set to indicate the error.
59

ERRORS

61       EBADF  fd is not a valid open file descriptor.
62
63       EIO    An error occurred during synchronization.  This error may relate
64              to  data written to some other file descriptor on the same file.
65              Since Linux 4.13, errors from write-back will be reported to all
66              file  descriptors  that  might have written the data which trig‐
67              gered the error.  Some filesystems (e.g., NFS) keep close  track
68              of  which data came through which file descriptor, and give more
69              precise reporting.  Other filesystems (e.g., most local filesys‐
70              tems)  will report errors to all file descriptors that were open
71              on the file when the error was recorded.
72
73       ENOSPC Disk space was exhausted while synchronizing.
74
75       EROFS, EINVAL
76              fd is bound to a special file (e.g., a pipe,  FIFO,  or  socket)
77              which does not support synchronization.
78
79       ENOSPC, EDQUOT
80              fd  is  bound  to a file on NFS or another filesystem which does
81              not allocate space at the time of a write(2)  system  call,  and
82              some previous write failed due to insufficient storage space.
83

CONFORMING TO

85       POSIX.1-2001, POSIX.1-2008, 4.3BSD.
86
87       On  POSIX  systems  on  which fdatasync() is available, _POSIX_SYNCHRO‐
88       NIZED_IO is defined in <unistd.h> to a value greater than 0.  (See also
89       sysconf(3).)
90

NOTES

92       On  some  UNIX  systems (but not Linux), fd must be a writable file de‐
93       scriptor.
94
95       In Linux 2.2 and earlier, fdatasync() is equivalent to fsync(), and  so
96       has no performance advantage.
97
98       The  fsync()  implementations in older kernels and lesser used filesys‐
99       tems do not know how to flush disk caches.  In these cases disk  caches
100       need  to be disabled using hdparm(8) or sdparm(8) to guarantee safe op‐
101       eration.
102

SEE ALSO

104       sync(1), bdflush(2), open(2),  posix_fadvise(2),  pwritev(2),  sync(2),
105       sync_file_range(2), fflush(3), fileno(3), hdparm(8), mount(8)
106

COLOPHON

108       This  page  is  part of release 5.12 of the Linux man-pages project.  A
109       description of the project, information about reporting bugs,  and  the
110       latest     version     of     this    page,    can    be    found    at
111       https://www.kernel.org/doc/man-pages/.
112
113
114
115Linux                             2021-03-22                          FSYNC(2)
Impressum