Skip to main content
Be Bitwise

APFS & macOS Storage

macOS & BSD Internals · Operating Systems track

8 min read

In this module

Apple's Modern File System

APFS landed in 2017 to replace HFS+ across every Apple platform. Apple drew it up explicitly for flash, with copy-on-write at the core, plus snapshots, shared-pool space management, native encryption, and instant clones. Pulling APFS apart shows what a file system looks like when it's designed from scratch for flash rather than extended from an early-1990s on-disk format, as ext4 and NTFS were.

Copy-on-Write Architecture

APFS uses copy-on-write (COW) for every scrap of metadata, and can extend it to file data too. When something changes, the new version gets written to a fresh location, then the parent pointer flips atomically over to it:

Metadata never gets overwritten in place. That single rule pays off in three ways:

  1. Crash consistency: pull the plug mid-write and you'll come back up with either the old superblock or the new one. There's no torn intermediate state to clean up.
  2. Snapshots: hold on to an old superblock and everything it points to stays alive. Free.
  3. Little for fsck to do: the on-disk state is consistent by construction, so there's no journal to replay after a crash. fsck_apfs still exists (Disk Utility's First Aid runs it) for catching corruption and bugs.
COW vs Journaling

ext4 and NTFS use journaling: write changes to a log first, then apply them. If a crash occurs, replay the log. APFS uses COW instead: write new data to new locations, then atomically update the pointer. COW eliminates the need for a journal (no double-write overhead) and makes snapshots essentially free. The trade-off is potential fragmentation over time, as data is never updated in place.

Container and Volume Architecture

APFS layers storage in two tiers: containers on the outside, volumes inside.

Containers

A container takes a whole partition (or sometimes the entire disk) and owns the underlying physical storage:

Space Sharing

Every volume inside a container draws from the same free-space pool:

text
Container: 500 GB total
├── macOS System: 25 GB used
├── macOS Data: 200 GB used
├── Preboot: 0.5 GB used
├── Recovery: 1 GB used
├── VM: 4 GB used
└── Free: 269.5 GB (shared by ALL volumes)

There are no fixed volume sizes. If the Data volume wants more room, it just stretches into the shared pool. Delete a few gigs off the system volume and that space immediately becomes available everywhere. The whole "I need to resize my partition" headache goes away.

Snapshots

Snapshots freeze a volume's state at a particular moment:

Take a snapshot called before-update while the volume holds files A, B and C. Then edit block 3 of A (the write lands in new block 9), delete B and create D. Here's who points at what afterwards:

Snapshot and live volume sharing blocks after a few changes

Blocks with two arrows cost nothing extra. The snapshot's real price is the blocks only it points at: 3, 4 and 5. Delete the snapshot and exactly those three go back to the free pool, which is why deleting B didn't free any space while the snapshot existed.

Taking a snapshot is essentially free — the kernel just pins the current superblock — and the space cost only grows as blocks diverge from the snapshot's view of the world.

This is the trick Time Machine uses for local backups. Before any OS update, macOS quietly snapshots the system volume so a rollback is one command away if the install goes sideways.

Clones

Cloning is instant duplication — both files share their data blocks until one of them changes:

bash
# Clone a file (instant, no data copied)
cp -c largefile.iso largefile_copy.iso
# Or using APFS clone API
clonefile("largefile.iso", "largefile_copy.iso", 0);
text
After cloning:
Original: metadata → [block 1] [block 2] [block 3] ... [block N]
Clone: metadata → [block 1] [block 2] [block 3] ... [block N]
↑ same physical blocks, shared
After modifying block 2 in the clone:
Original: metadata → [block 1] [block 2] [block 3] ... [block N]
Clone: metadata → [block 1] [block 2'] [block 3] ... [block N]
↑ new block, only this one copied

macOS leans on cloning constantly:

  • Xcode clones build products to avoid copying everything
  • "Duplicate" in Finder clones when it can
  • APFS-aware backup tools clone to dodge copying unchanged data altogether

You can watch two of those three behaviours on any Unix file system. The runner makes a hard link and a plain copy of one file, then writes through the link. Predict which of the other two files sees the change before you run it.

Try itRun it, then write through copy.txt instead of hard.txt and predict which files change this time.
C (GCC 9.2.0)

An APFS clone would sit between the two: its own inode like copy.txt, so a write through it leaves the original alone, but sharing the original's data blocks until that write happens, so creating it costs almost nothing.

Key Concept
Clone vs Copy vs Hard Link

A clone creates a new file with independent metadata that shares data blocks (COW semantics - changes diverge). A copy creates a fully independent file with its own data blocks (slow, doubles space). A hard link creates a new directory entry pointing to the same inode/metadata (changes are always shared, cannot link directories). Clones offer the best of both worlds: instant creation like hard links, but independent behaviour like copies.

Encryption

APFS bakes in encryption at several granularities:

text
Encryption modes:
├── No encryption
├── Single-key encryption (one key for entire volume)
├── Multi-key encryption
│ ├── Per-file key for file contents
│ ├── Per-volume key for metadata
│ └── Container key for container metadata
└── FileVault uses multi-key mode

Files can each carry their own encryption key, wrapped by the volume key, which is itself wrapped by the user's password (or, on Apple Silicon, a key locked inside the Secure Enclave). That nested wrapping unlocks a few useful tricks:

  • Instant secure erase: throw away the top-level key and the data is unreachable forever
  • Per-user encryption: every account gets its own key chain
  • Class-based encryption on iOS: some data unwraps before unlock, some only after

Comparing File Systems

FeatureAPFSext4NTFS
COWYes (all metadata)NoNo
SnapshotsNative, efficientVia LVMVia VSS (Volume Shadow Copy)
ClonesNative, instantNo (reflink in btrfs/XFS)No
Space sharingYes (container level)NoNo
Native encryptionPer-file multi-keydm-crypt (block level)BitLocker (volume) + EFS (file)
Crash consistencyCOW (no journal needed)JournalJournal ($LogFile)
CompressionYes (transparent, per-file, inherited from HFS+)NoYes (per-file)
Max file size8 EB16 TB16 EB
Optimised forFlash/SSDHDD and SSDHDD and SSD

Sealed System Volume

Big Sur introduced the Sealed System Volume (SSV). The system volume is verified end-to-end via a SHA-256 Merkle tree:

text
Each file's contents has a SHA-256 hash, stored in its metadata
Metadata nodes hash their children, forming a tree
The root hash (the "seal") is signed by Apple
Any modification to any system file invalidates the tree
Boot process:
1. Verify root hash signature
2. As file data is read, check it against its stored hash
3. Any mismatch = system has been tampered with

Even with SIP turned off, you can't touch a system file without breaking the seal. The system volume is mounted read-only, and an overlay scheme called firmlinks weaves it together with the writable data volume so the user-visible filesystem still looks like one tree.

APFS pulls together features that older file systems only get to via bolted-on tooling — LVM for snapshots, dm-crypt for encryption. What lifts it above a spec checklist is how tightly it wires into the rest of the Apple stack: Secure Enclave for key custody, Time Machine and FileVault for higher-level workflows, SSV for system integrity. The result is a file system designed end-to-end for one platform, with no seams showing.