1fsync(2)                      System Calls Manual                     fsync(2)
2
3
4

NAME

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

LIBRARY

10       Standard C library (libc, -lc)
11

SYNOPSIS

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

DESCRIPTION

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

RETURN VALUE

60       On  success, these system calls return zero.  On error, -1 is returned,
61       and errno is set to indicate the error.
62

ERRORS

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
71              file  descriptors  that  might have written the data which trig‐
72              gered the error.  Some filesystems (e.g., NFS) keep close  track
73              of  which data came through which file descriptor, and give more
74              precise reporting.  Other filesystems (e.g., most local filesys‐
75              tems)  will report errors to all file descriptors that were open
76              on the file when the error was recorded.
77
78       ENOSPC Disk space was exhausted while synchronizing.
79
80       EROFS, EINVAL
81              fd is bound to a special file (e.g., a pipe,  FIFO,  or  socket)
82              which does not support synchronization.
83
84       ENOSPC, EDQUOT
85              fd  is  bound  to a file on NFS or another filesystem which does
86              not allocate space at the time of a write(2)  system  call,  and
87              some previous write failed due to insufficient storage space.
88

VERSIONS

90       On  POSIX  systems  on  which fdatasync() is available, _POSIX_SYNCHRO‐
91       NIZED_IO is defined in <unistd.h> to a value greater than 0.  (See also
92       sysconf(3).)
93
94       On  some  UNIX  systems (but not Linux), fd must be a writable file de‐
95       scriptor.
96

STANDARDS

98       POSIX.1-2008.
99

HISTORY

101       POSIX.1-2001, 4.3BSD.
102
103       In Linux 2.2 and earlier, fdatasync() is equivalent to fsync(), and  so
104       has no performance advantage.
105
106       The  fsync()  implementations in older kernels and lesser used filesys‐
107       tems do not know how to flush disk caches.  In these cases disk  caches
108       need  to be disabled using hdparm(8) or sdparm(8) to guarantee safe op‐
109       eration.
110

SEE ALSO

112       sync(1), bdflush(2), open(2),  posix_fadvise(2),  pwritev(2),  sync(2),
113       sync_file_range(2), fflush(3), fileno(3), hdparm(8), mount(8)
114
115
116
117Linux man-pages 6.04              2023-03-30                          fsync(2)
Impressum