Lesson 6 of 9 · 26m
Filesystems and Storage Semantics That Production Depends On
Paths, inodes, journaling, fsync, mounts, permissions, and why file operations are transactional only if you make them so.
The filesystem looks simple — create, write, rename, delete — until a power loss, disk stall, or log rotation exposes what those operations actually promise. Most applications do not need database theory, but they do need to respect filesystem semantics. Data loss bugs are usually not exotic. They are missing fsyncs, unsafe rewrites, wrong assumptions about atomicity, or a full filesystem.
Files are names over inodes
$ ls -li /var/log/nginx/access.log
393545 -rw-r----- 1 www-data adm 183221 Nov 29 22:10 /var/log/nginx/access.logA directory entry maps a name to an inode. Hard links can point multiple names at the same inode. Unlinking a name does not necessarily delete data: if a process still holds the file open, the inode persists until the final descriptor closes. That is how a service keeps writing to a deleted log file while disk space appears mysteriously consumed.
Run lsof +L1 to find deleted-but-open files, then restart or signal the process to reopen logs. rm alone does not release the space.
Writes are cached; durability requires fsync
write(fd, data, len); /* copies into kernel/page cache */
fsync(fd); /* asks storage to make it durable */write() success means the kernel accepted the bytes, not that the disk has them. A crash can lose cached data. fsync() is expensive because it forces ordering and hardware flushes, but configuration files, state checkpoints, and write-ahead logs need it when durability matters.
Safe file replacement is a pattern, not an improvisation:
write tmpfile
fsync tmpfile
mv tmpfile final
fsync containing directoryThe final directory fsync is the step everyone forgets. Without it, the rename itself may not be durable after a crash.
Rename is atomic within one filesystem
rename(2) atomically replaces one directory entry with another on the same filesystem. It is the foundation of safe deployment and config reloads. It is not atomic across filesystems — there it becomes copy plus unlink or fails with EXDEV. This is why /tmp location matters when applications write temporary files for atomic rename.
Full filesystems fail sideways
df -h /
df -i /Byte capacity and inode capacity are separate. Millions of tiny files can exhaust inodes while bytes remain. Applications then see ENOSPC even though df -h looks acceptable. Design rotation and cleanup for both dimensions.
Permissions also compose along the whole path. A file can be readable while its parent directory is not searchable. Execute (x) on a directory means traversal, not execution of the directory itself.
I/O wait means the machine is waiting, not calculating
%iowait is time CPUs had no runnable work because threads were blocked on storage. It is a clue, not a complete measurement. Better evidence comes from iostat -x 1: await latency, queue size, utilization, and throughput. NVMe can show high utilization with low latency; network filesystems can stall at low utilization. Interpret device behavior before reaching for application rewrites.
Field notes
- NFS and object-storage-backed filesystems have different consistency semantics. Do not assume POSIX behavior because a path exists.
O_DIRECTbypasses page cache but brings alignment and performance constraints; use it only when you know why.- Log rotation must coordinate with the writer: copy-truncate can lose lines, while create-and-signal depends on the application reopening correctly.
- Journaling protects filesystem structure, not necessarily your application payload. Those are different durability guarantees.
Was this lesson helpful?