Changing one block of a file raises two questions: where does the replacement go, and what happens if the machine crashes before the update finishes? Copy-on-write (CoW) writes the replacement to a new location, then commits metadata that makes it reachable. The previous committed version stays intact while the replacement is prepared.
This page follows filesystem CoW in Btrfs and ZFS. The same sharing-until-modification idea also appears in virtual memory, but memory-page faults and filesystem commits use different mechanisms.
The Traditional Problem: In-Place Updates
An in-place update overwrites the block holding the old data. Without additional protection, a crash during that write can leave a partial replacement, and the old version is no longer available there.
That does not mean every in-place filesystem leaves its metadata inconsistent. Ext4 uses a journal to recover committed metadata operations; its default mode does not journal ordinary file contents. Filesystem consistency and application-data durability are separate guarantees. The kernel's ext4 journal documentation explains that boundary.
The Copy-on-Write Solution
CoW changes the update sequence:
- Allocate a new block for the changed data.
- Write the replacement without overwriting the old block.
- Write new metadata that points to the replacement, continuing toward the root.
- Persist the required data and metadata, then commit a recoverable new root.
- Reclaim old blocks only when no retained version needs them and reclamation is safe.
The root commit chooses a complete tree. In ZFS, writes are grouped into transaction groups, and recovery selects a valid committed uberblock. This is a filesystem protocol with ordering and recovery rules, not an arbitrary pointer assignment that makes every application write atomic. OpenZFS describes the write path and transaction groups.
How Copy-on-Write Works: Interactive Exploration
Step through Simple Write to watch block 101 become 5280. Then try Instant Snapshots to see why old B remains allocated, or Atomic Operations to follow the new data blocks before and after their metadata becomes reachable.
Follow the references
When does the committed file start pointing at the new data?
Start with three referenced blocks
The file points to blocks 100, 101 and 102. We will change only its middle block, B.
Block 100
Data A
Current
refs: 1
Block 101
Data B
Current
refs: 1
Block 102
Data C
Current
refs: 1
Allocated data 12 KiB4 KiB per block; metadata excluded
Key CoW Concepts
1. Write-Anywhere Allocation
A new location separates preparation from commitment. It need not be physically adjacent to the original block, and the whole file does not need to be copied.
Before: current file → [100: A, 101: B, 102: C]
Prepare: write modified B to block 5280
After: current file → [100: A, 5280: B', 102: C]
A and C are unchanged. Only B and the metadata needed to reach its replacement are rewritten.
2. Metadata Updates Are Key
The replacement data alone does not change the committed tree. New parent blocks must reach it, and the root commit must happen after those blocks are durable. A crash before that commit leaves recovery using the old tree; a completed commit lets recovery select the new one.
This model assumes a working commit protocol and storage that honors the required flushes. CoW does not itself detect silent corruption or repair damaged data. Checksums detect many errors, and repair needs a valid alternative copy. Btrfs documents the conditions for repair.
3. Reference Counting
The explorer counts logical references from the displayed file and snapshot. Real filesystems can track sharing through tree nodes and extents instead of updating a literal counter on every data block when a snapshot is created.
After a snapshot, before modification:
Current file → [100, 101, 102]
Snapshot → [100, 101, 102]
Block 101: two logical references
After the current file changes B:
Current file → [100, 6000, 102]
Snapshot → [100, 101, 102]
Block 101: one reference, from the snapshot
Block 6000: one reference, from the current file
Delete the snapshot:
Block 101: one → zero references; eligible for reclamation
Blocks 100, 102: two → one reference; still needed
Block 6000: remains referenced by the current file
Snapshots: Sharing the Old State
A snapshot retains access to an existing tree without copying all its file data. That makes creation inexpensive relative to a full copy, but not free: metadata must be recorded, and retained old blocks consume space as the current filesystem changes. Btrfs snapshots operate on subvolumes and share extents with their source. See the Btrfs snapshot model.
Snapshot Space Efficiency
Consider an authored example with 100 GiB of data. Ignore metadata, compression, redundancy and allocation rounding. After taking a snapshot, replace exactly 10 GiB of blocks in the current version:
| Data category | Physical space |
|---|---|
| Unchanged blocks shared by both versions | 90 GiB |
| Old blocks retained by the snapshot | 10 GiB |
| Replacement blocks used by the current version | 10 GiB |
| Total unique data | 110 GiB |
Each version still reads 100 GiB. Adding those logical sizes would double-count the shared 90 GiB. Deleting the snapshot can release its unique 10 GiB once no other references remain. A snapshot on the same storage is not an independent backup.
Atomic Operations
The tree view shows two changes prepared together and made reachable by one filesystem commit. This explains why recovery can find a complete committed tree; it does not promise that two separate application writes form one transaction.
Example: Rename Directory
A directory rename can update several metadata records. A journaled filesystem can protect that operation with a journal transaction; a CoW filesystem can commit replacement metadata. It is incorrect to infer that a journaled rename necessarily corrupts the filesystem if interrupted. Crash durability also depends on the application's synchronization and the filesystem's guarantees.
Example: Database Transaction
A database still needs its own transaction and recovery protocol. Its writes may span filesystem commits, and its invariants are not visible to the filesystem. CoW does not replace database logging, appropriate synchronization, or application-aware snapshot procedures.
ZFS also has an intent log for synchronous operations. “CoW” and “logging” are not mutually exclusive designs; they can serve different recovery needs. OpenZFS distinguishes transaction-group commits from synchronous-write logging.
CoW in Different Filesystems
| Filesystem | How CoW is used |
|---|---|
| Btrfs | CoW metadata and, by default, file data; subvolume snapshots and reflinked files share extents. |
| ZFS | CoW data and metadata, transaction-group commits, dataset snapshots and writable clones. |
| APFS | CoW metadata, snapshots and file clones that initially share storage. Apple's filesystem guide describes these features. |
A reflink gives a new file its own identity while initially sharing data with its source. A full data copy is a different operation. On a supporting filesystem, cp --reflink=always source destination requests a reflink and fails if it cannot be made.
Performance Implications
CoW avoids copying unchanged file data when creating snapshots or reflinks. Its cost appears on modification: replacement data and metadata must be allocated and written, and older versions may remain pinned by snapshots.
Advantages
- Shared copies: snapshots and reflinks reuse existing data blocks.
- Preserved committed state: new writes can be prepared without overwriting the previous committed tree.
- Allocation flexibility: the filesystem can choose free locations for new blocks.
Challenges
- Write amplification: a small change can rewrite larger allocation units and parent metadata.
- Fragmentation: repeated changes can scatter a file's blocks; CoW does not eliminate seeks on a hard disk.
- Space pressure: retained versions restrict what can be freed, and allocation becomes harder as available space shrinks.
Optimization Tips
Measure the actual workload before changing policy. Record sizes, compression, snapshot retention and device layout affect the result. Free-space headroom is useful, but there is no universal 80% performance cliff. SSD wear leveling is handled by the device; CoW is not a guarantee of even wear.
Btrfs defragmentation can break extent sharing and increase space use. ZFS recordsize should match the workload's access pattern, rather than being set to one fixed value for every database. Btrfs describes defragmentation's sharing costs, and ZFS documents dataset properties.
Space Reclamation
Deleting a file or snapshot removes references. Blocks remain allocated if another snapshot, clone or file still needs them, and cleanup can be deferred. Inspect filesystem accounting rather than assuming the sum of file sizes equals physical use.
# Inspect Btrfs allocation and snapshots.
btrfs filesystem usage /mnt
btrfs subvolume list -s /mnt
# Inspect ZFS dataset and snapshot space.
zfs list -o name,used,available,referenced
zfs list -t snapshot -o name,used,referenced
Deleting snapshots is a retention decision: it releases only blocks that no other version still references. In Btrfs, snapshot deletion includes background cleanup. The subvolume manual describes deletion and reclamation.
When CoW Hurts: Workload-Specific Choices
Random overwrites in databases and VM images can make CoW costs noticeable. They do not justify a blanket instruction to disable CoW.
Btrfs NOCOW
For a deliberately chosen workload, setting chattr +C on an empty directory makes newly created files inherit NOCOW. This permits in-place writes where blocks are exclusively owned. It also disables data checksumming and compression for those files. Existing populated files are not converted by simply changing their parent directory. Btrfs documents NOCOW's trade-offs.
Snapshots still preserve shared extents: a subsequent write must copy a shared extent before it can be overwritten privately. NOCOW therefore does not mean that snapshots disappear or that no write can ever use CoW. The kernel's NOCOW check explicitly preserves shared extents.
ZFS Remains Copy-on-Write
Neither copies=1 nor compression=off disables ZFS CoW. copies controls additional data copies, and compression controls how data is encoded. Tune dataset properties for a measured workload while retaining the application's durability requirements. The ZFS property reference defines both settings.
CoW vs Journaling
| Question | CoW tree commits | Metadata journaling |
|---|---|---|
| How is a committed metadata state recovered? | Select a valid committed root and its tree. | Replay committed journal records as needed. |
| How is old data retained? | Keep snapshot or clone references to shared blocks. | Use a separate versioning or snapshot facility. |
| What work does an update add? | New blocks and changed metadata paths. | Journal records and later metadata writes. |
| Are arbitrary application transactions automatic? | No. | No. |
| Does the mechanism alone detect or repair corruption? | No; checksums and valid copies are separate mechanisms. | No; guarantees depend on the filesystem and configuration. |
Best Practices
Keep enough space for new writes and the old versions your retention policy preserves. Monitor physical allocation, test workload-specific settings, and use scrubbing where supported to verify stored checksums. Keep independent backups and retain the database or application's own recovery protocol.
CoW's central rule is simple: prepare the new blocks, commit the references that reach them, and reclaim the old blocks only when they are no longer needed. Snapshots follow directly from keeping some of those references alive.
Related concepts
Follow a filesystem transaction from journal to checkpoint. Explore crash recovery, ext4 journal modes, and the limits of data durability.
Trace how Btrfs shares extents, creates snapshots, distributes copies, compresses writes, and repairs checksum errors when a good replica exists.
Explore ext4, the default Linux filesystem with journaling, extents, and proven reliability. Learn how ext4 protects your data.
Learn FAT32 and exFAT filesystems for cross-platform USB drives and SD cards. Understand file size limits and compatibility.
Explore Linux filesystems through interactive visuals. Learn VFS, compare ext4 vs Btrfs vs ZFS, and understand file operations.
Understand Linux inodes - the metadata structures behind every file. Learn about hard links, soft links, and inode limits.
