Saving data safely
The page cache
Disk is slow and RAM is fast. So the OS keeps file data you used lately in RAM, in the page cache. Read the same file again and it comes from RAM, not the disk. The OS uses spare RAM this way and gives it back when programs need it.
Buffered writes
A write usually lands in the page cache and returns at once. The OS copies it to disk a few seconds later. This is a buffered write, and data not yet on disk is dirty. It is fast, but a power cut before then loses the write, though the program thought it was done.
fsync
fsync makes the OS write a file's dirty data to the disk and wait until the disk says it is stored. After fsync returns, the data survives a crash. It is slow, so programs group many writes and then call fsync once.
Crash in the middle
Adding a block to a file updates several places on disk: the data block, the file's inode, and the list of free blocks. The disk writes them one at a time. A crash in between leaves them disagreeing. An old fix, fsck, scans the whole disk after a crash, which can take hours on a big disk.
Journaling
A journaling file system first writes the planned changes to a log on disk, the journal, followed by a commit mark. Only then does it write them to their real places. After a crash, it replays committed changes and throws away ones with no commit. Recovery is fast, but each change is written twice.
Remember
- The page cache keeps file data in RAM for fast repeat reads.
writeusually only reaches RAM; a crash can lose it.fsyncwaits until the data is on disk; it is slow.- One change means several disk writes; a crash can split them.
- A journal plus a commit mark lets the OS finish or drop a change.