Copy-on-Write (CoW) Filesystems
Overview
Copy-on-Write (CoW) is a storage management technique where existing data blocks are never mutated in place. When data is modified:
- The filesystem allocates new unallocated blocks on disk.
- The modified data is written to these new blocks.
- The parent metadata pointers (e.g., uberblock, merkle tree nodes) are updated atomically to point to the new location.
- The old blocks are marked free only if not referenced by an active snapshot.
Key Advantages of CoW
- Crash Consistency & Atomic Updates: No half-written or corrupted metadata states on unexpected power failure. Eliminates the need for traditional journal recovery (
fsck). - Instant, Zero-Cost Snapshots: Taking a snapshot simply preserves the current root pointer; no data is duplicated until blocks are modified.
- Efficient Deduplication & Clones: Multiple datasets or VM images can share common base blocks indefinitely.
Trade-offs & Considerations
- Write Amplification: Small random writes cause parent metadata tree nodes to be rewritten all the way up to the root.
- Fragmentation: Free-space fragmentation can increase over time with random write workloads (e.g., databases or VM disk images).
- Out-of-Space Deadlocks: CoW systems require free space to delete files (because modifying the directory tree requires new writes).
Major CoW Filesystems
- ZFS (OpenZFS)
- Btrfs
- APFS (Apple File System)
- Bcachefs