fsync(2)

๐Ÿ’– fsync(2) โ€” the bestie cut ๐Ÿ’–

okay so this is a page from the System Calls Manual, and today's main characters are fsync and fdatasync โ€” they sync a file's in-core state with the storage device. no cap, that's the whole vibe. ๐Ÿฆ‹


๐ŸŒธ NAME ๐ŸŒธ

fsync, fdatasync โ€” synchronize a file's in-core state with storage device โœจ

๐Ÿ“š LIBRARY ๐Ÿ“š

Standard C library (libc, -lc) ๐Ÿ’…

โœจ SYNOPSIS โœจ

#include <unistd.h>

int fsync(int fd);

int fdatasync(int fd);

๐ŸŽ€ Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

fsync():

  • glibc 2.16 and later: no feature test macros need be defined ๐Ÿ’–
  • glibc up to and including 2.15:
_BSD_SOURCE || _XOPEN_SOURCE
    || /* Since glibc 2.8: */ _POSIX_C_SOURCE >= 200112L

fdatasync():

_POSIX_C_SOURCE >= 199309L || _XOPEN_SOURCE >= 500

๐Ÿ’œ DESCRIPTION ๐Ÿ’œ

okay bestie, listen. fsync() transfers โ€” like, literally "flushes" โ€” all the modified in-core data of the file referred to by the file descriptor fd (that's the modified buffer cache pages, for the record) to the disk device, or whatever other permanent storage device you've got going on, so that every single changed bit of information can be retrieved even if the system crashes or gets rebooted. ๐Ÿ˜ญ

And yes, this includes writing through or flushing a disk cache if there is one. the way that this call just blocks until the device reports the transfer has completed?? main character behavior, honestly. ๐Ÿ‘‘

As well as flushing the file data, fsync() also flushes the metadata information associated with the file (peep inode(7)).

โš ๏ธ heads up though โ€” calling fsync() does not necessarily ensure that the entry in the directory containing the file has also reached disk. for that, you need an explicit fsync() on a file descriptor for the directory too. two separate calls, bestie, don't skip the second one. ๐Ÿ’€

fdatasync() is similar to fsync(), but it does not flush modified metadata unless that metadata is needed in order to allow a subsequent data retrieval to be correctly handled. so like โ€” changes to st_atime or st_mtime (that's time of last access and time of last modification, respectively; see inode(7)) don't require flushing, because they're not necessary for a subsequent data read to be handled correctly. ๐Ÿ“

but on the other hand?? a change to the file size (st_size, as made by, say, ftruncate(2)) would require a metadata flush. so it's giving "it depends". ๐Ÿ’…

The aim of fdatasync() is to reduce disk activity for applications that don't require all metadata to be synchronized with the disk. efficiency queen. โœจ


๐Ÿซถ RETURN VALUE ๐Ÿซถ

On success, these system calls return zero. ๐ŸŽ‰

On error, -1 is returned, and errno is set to indicate the error. ๐Ÿ™ˆ

๐Ÿ’€ ERRORS ๐Ÿ’€

  • EBADF โ€” fd is not a valid open file descriptor. not a vibe.
  • EINTR โ€” The function was interrupted by a signal; see signal(7). ๐Ÿ˜ค
  • EIO โ€” An error occurred during synchronization. This error may relate to data written to some other file descriptor on the same file. Since Linux 4.13, errors from write-back will be reported to all file descriptors that might have written the data which triggered the error. Some filesystems (e.g., NFS) keep close track of which data came through which file descriptor, and give more precise reporting. Other filesystems (e.g., most local filesystems) will report errors to all file descriptors that were open on the file when the error was recorded. so it's giving "who did this" and sometimes the answer is "everyone". ๐Ÿ’€
  • ENOSPC โ€” Disk space was exhausted while synchronizing.
  • EROFS โ€” (listed with no description attached, just vibes)
  • EINVAL โ€” fd is bound to a special file (e.g., a pipe, FIFO, or socket) which does not support synchronization.
  • ENOSPC โ€” (yeah it's listed again, and again with no description โ€” we don't question the ancestors)
  • EDQUOT โ€” fd is bound to a file on NFS or another filesystem which does not allocate space at the time of a write(2) system call, and some previous write failed due to insufficient storage space.

๐ŸŒˆ VERSIONS ๐ŸŒˆ

On POSIX systems on which fdatasync() is available, _POSIX_SYNCHRONIZED_IO is defined in <unistd.h> to a value greater than 0. (See also sysconf(3).) โญ

โญ STANDARDS โญ

POSIX.1-2024. iconic, certified, ratified. ๐Ÿ‘‘

๐Ÿ“œ HISTORY ๐Ÿ“œ

fsync()

  • 4.2BSD, SUSv1, POSIX.1-1996.

fdatasync()

  • POSIX.1-1996.

In Linux 2.2 and earlier, fdatasync() is equivalent to fsync(), and so has no performance advantage. so if you're time-traveling, don't bother. ๐Ÿ•ฐ๏ธ

The fsync() implementations in older kernels and lesser used filesystems do not know how to flush disk caches. In these cases disk caches need to be disabled using hdparm(8) or sdparm(8) to guarantee safe operation. โš ๏ธ

Under AT&T UNIX System V Release 4 fd needs to be opened for writing. This is by itself incompatible with the original BSD interface and forbidden by POSIX, but nevertheless survives in HP-UX and AIX. the way that this is just still around?? heritage. ๐Ÿ™ˆ

๐Ÿ‘‘ SEE ALSO ๐Ÿ‘‘

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


and that's the whole page, bestie!! ๐ŸŒˆ๐Ÿ’–โœจ this one came from Linux man-pages 6.20, dated 2026-02-08 โ€” screenshot it, cite it, tell your friends. now go flush something. ๐Ÿ’…๐Ÿฆ‹๐ŸŽ€

๐Ÿ“ฆ where this page comes from
  • arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • ๐Ÿ“œ the page's license: Linux-man-pages-copyleft
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms