Definition
fsync is a system call, a request from a program to the operating system kernel, defined by POSIX (Portable Operating System Interface) [389]. Given an open file, fsync writes the file’s modified data and metadata from kernel memory to the storage device and returns once the device reports them stored. Directory durability §8.2 is the separate property that a directory’s entries, which link names to files, have reached storage. An fsync of a file does not necessarily make durable the entry naming it; that needs an fsync of the directory.
The idea
A program creates a new file report, writes 120 bytes, calls fsync on the file and gets success. The power fails a second later. After the restart, the directory holds no entry named report. The 120 bytes may sit on the disk, but no name leads to them, so to every program the file does not exist. The fsync did its job; the program asked for too little.
A file and its name live in different places. The contents, and the metadata needed to read them back (size, location of the data blocks), belong to the file’s own record, its inode. The name belongs to the parent directory, itself a file whose contents are a list of entries. Creating, renaming or deleting a file changes the directory, not the file, and the kernel caches each of the two and writes each to the device on its own schedule. In a library, a durable file is a book on the shelf and a durable directory entry is its catalog card; a book without a card cannot be found. The picture stops at one point: some file systems happen to file the card whenever they shelve the book, but that is not part of the fsync contract, so a program cannot count on it.
How it works
The write path.
writecopies bytes into the page cache, kernel memory that holds recently used file data, and returns. The bytes are visible to every process but exist only in RAM.The kernel writes modified pages to the device later; on Linux, by default, once they have been modified for about 30 seconds.
The device may first hold the data in a volatile write cache, its own memory that loses its content without power.
The synchronization calls.
fsync(fd)writes the file’s modified data and metadata, makes the device write through or flush its cache, and returns 0 when all of that has completed.fdatasync(fd)does the same but skips metadata not needed to read the data back, such as the modification time.fsync on a descriptor for a directory, obtained by opening the directory for reading, makes the directory’s entries durable: names created, renamed or removed in it.
The open flags
O_SYNCandO_DSYNCmake everywritebehave as if followed byfsyncorfdatasync.
The Linux manual page for fsync states the point of this entry: calling fsync does not necessarily ensure that the entry in the directory containing the file has also reached the disk; that needs an explicit fsync on a descriptor for the directory.
Crash-safe replacement. The usual protocol replaces a file name so that a crash leaves either the complete old version or the complete new one:
Write the new contents to a temporary file in the same directory, for example
name.tmp; the same directory keeps both names on one file system, whichrenameneeds.fsync the temporary file and check the result. The new contents are durable, though perhaps not yet reachable by name.
Call
renamefromname.tmptoname. POSIX makes this replacement atomic for other processes: one that opensnamegets the old file or the new one, never neither.Open the directory, fsync it, and check the result. The new entry is now durable.
Only now report success.
A newly created directory adds one more step: its own entry lives in its parent, so the parent must be synchronized too.
Errors. Every one of these calls can fail, and a failed fsync is serious. On Linux, after a write to the device fails, the kernel may report the error once and discard the unwritten data, so a second fsync that returns success proves nothing. The developers of the PostgreSQL database found this in 2018, and PostgreSQL now stops with an error instead of retrying [390]. Treat a failed fsync as lost data: rewrite from a copy the program still holds, or fail.
Worked example
The directory /data contains config, inode 11, holding 100 bytes of old settings. A program replaces it with 120 bytes of new settings through config.tmp, inode 12, using the protocol above. For each last completed step before a power failure, the list gives what the directory may hold after the restart, assuming the device honors flush requests.
Writing
config.tmp:configis the old inode 11;config.tmpmay be missing, empty or partial. Acceptable.fsync of
config.tmp:configis still inode 11;config.tmpis complete, or missing because its own entry was never made durable. Acceptable.rename:
confignames inode 11 or inode 12. Both are complete versions, but success must not have been reported yet, or a reported update may vanish.fsync of the directory:
confignames inode 12 with the 120 new bytes.Variant without step 2:
configmay name inode 12 while its 120 bytes never reached the device, so the file is empty or partial. Not acceptable.
Publishing the new version needs two durable facts: inode 12 holds the 120 bytes, and the entry config names inode 12. Step 2 establishes the first fact, step 4 the second. The question’s case is a brand-new file with no old version: the program writes report, fsyncs it and stops there. If the power fails before the directory’s new entry reaches the device, the restarted system has no report. The data and the inode were durable; the name never was. A file system check after the restart may free the nameless inode, or place it in a recovery directory such as lost+found.
Stated precisely
Call information durable when it survives a crash or power failure and is found again after the restart. Let be the statement that the file’s contents are durable and the statement that the directory entry binding the name to that file is durable. Publishing a file safely requires , where means and; if the directory itself is new, its own entry in its parent must be durable too, and so on upward. A successful fsync of the file establishes . A successful fsync of the directory, made after the operation that created or renamed the entry, establishes . Neither call establishes the other’s fact.
For a replacement, must hold before the rename, because once the rename can reach the device, a crash may leave the name pointing at contents that are not durable. This is a write ordering §8.20 requirement: the contents reach the device before the new entry does. The guarantees assume: (1) the device has stored the data when it reports a flush complete, which some devices with a volatile cache and no protection against power loss do not; (2) the operating system passes the flush to the device, which fsync on Apple’s macOS does not, and there the fcntl command F_FULLFSYNC is needed; (3) every call returned success; (4) the file system implements POSIX semantics for rename and fsync, which some network file systems weaken.
Limits and common mistakes
Durability is not atomicity §8.3. rename is atomic for running processes; whether a crash during it leaves the old entry or the new one is up to the file system, and the protocol makes both outcomes acceptable.
A successful write means the bytes reached the page cache, nothing more, and a successful close does not flush either. Synchronizing only the directory can leave a durable name pointing to an empty or partial file.
Some file systems happen to make a new entry durable together with the file, so a test on one machine may never show the missing name while another file system, mount option or kernel version shows it. Portable code synchronizes the directory.
fsync covers one file, and a directory fsync one directory. An update that must change several files together needs a larger protocol, such as write-ahead logging §8.12, which records the intended changes durably before making them.
Durable is not authentic: fsync stores whatever bytes the program wrote and says nothing about who produced them. Test the protocol with power-loss injection §8.38, cutting power at many points; an orderly shutdown flushes every cache and hides the problem.
Questions answered
Why can a durably written file still disappear after a crash?
Because a file’s contents and its name are stored in different places, and fsync of the file makes only the contents durable. The name is an entry in the parent directory, which the operating system caches and writes to the storage device separately. If the power fails after the file’s fsync but before the directory’s new entry reaches the device, the restarted system finds no name that leads to the file. The file has disappeared although its data was written. The Linux manual page for fsync warns of exactly this.
The fix is to also call fsync on the directory, through a descriptor obtained by opening the directory, after creating or renaming the file, and to report success only after both calls succeed. To replace an existing file: write a temporary file in the same directory, fsync it, rename it over the old name, then fsync the directory. Check every call’s result, and treat a failed fsync as lost data rather than retrying it.
Related entries: Atomicity (§8.3), Linearization point (§8.6), Transaction (§8.7), Immutable evidence (§13.12), Durability (§8.2), Crash consistency (§8.4), Write ordering (§8.20), Power-loss injection (§8.38). Further reading: [50, 140, 389, 390].