Expand description
Durable atomic file / directory operations for checkpoint-class writes.
The tmp-file + rename pattern is atomic only if both the file data and the containing directory entry reach stable storage in the correct order. On ext4 / XFS the rename metadata op can reach disk before the data pages backing the tmp file — a power loss between the write and the next checkpoint then leaves a correctly-named file containing zeros.
atomic_write_fsync is the single helper all checkpoint-class writers
go through so the ordering (write → sync_data → rename → fsync_dir) is
enforced in one place. atomic_swap_dirs_fsync does the same for
directory-level swaps (rename old-dir → backup, rename new-dir → old-dir).
read_checkpoint_dontneed pairs with the write helper on the read side:
checkpoint bytes are consumed once (deserialized into the in-memory index)
and then superseded. Leaving them in the page cache wastes memory needed
by hot workloads.
Functions§
- atomic_
swap_ dirs_ fsync - Atomically swap a directory:
rename(live, backup); rename(staged, live), fsyncing the parent directory once both renames have completed. - atomic_
write_ fsync - Atomically write
bytestodstvia atmpfile with full durability. - fsync_
directory - Fsync a directory to ensure file creation/deletion metadata is durable.
- read_
checkpoint_ dontneed - Read a checkpoint file and advise the kernel to drop its pages from the page cache.