apfs-extent-xray
Zero-CGo, zero-sudo APFS physical extent inspector and sparse hole puncher built with x/apfs.
What It Demonstrates
- Physical Extent Inspection (
apfs.Extents / F_LOG2PHYS_EXT) & Sharing (apfs.SharedExtentBytes): Creates a 2 MiB file, clones it via apfs.CloneFile, and proves 100% of physical disk blocks are shared.
- Copy-on-Write Extent Divergence: Mutates a single 4 KiB page at offset
0x100000, syncs, and renders an ASCII extent map showing the exact diverged physical extent (16 KiB on Apple Silicon's 16 KiB VM pages) while surrounding extents remain shared.
- Sparse Hole Punching (
apfs.PunchHole / F_PUNCHHOLE): Punches a 1 MiB sparse hole in the clone, compares st_blocks before vs after (2048 KiB -> 1024 KiB on disk), and verifies the punched range reads back as zeros while logical file size remains 2 MiB.
Usage
CGO_ENABLED=0 go run ./examples/kernel/apfs-extent-xray
Measured Output (Apple M4 Max, CGO_ENABLED=0)
apfs-extent-xray (temp=/var/folders/kj/qbc32fzd2pv6ngnsysy8vpmw0000gn/T/apfs-extent-xray-3098367313)
[1] After apfs.CloneFile (before mutation)
orig [0]: logical 0x000000..0x200000 (2048 KiB) -> phys 0x057b48639000 [SHARED]
clone[0]: logical 0x000000..0x200000 (2048 KiB) -> phys 0x057b48639000 [SHARED]
shared physical bytes: 2097152 / 2097152 (100.0%)
extent map (64x32KiB): [================================================================] (= shared, D diverged, . hole)
[2] After mutating 4 KiB page at logical offset 0x100000
clone[0]: logical 0x000000..0x100000 (1024 KiB) -> phys 0x057b48639000 [SHARED]
clone[1]: logical 0x100000..0x104000 ( 16 KiB) -> phys 0x058912a57000 [DIVERGED]
clone[2]: logical 0x104000..0x200000 (1008 KiB) -> phys 0x057b4873d000 [SHARED]
shared physical bytes: 2080768 / 2097152 (99.2%), diverged: 16 KiB
extent map (64x32KiB): [================================D===============================] (= shared, D diverged, . hole)
[3] After apfs.PunchHole [0x0, +1024 KiB)
clone[0]: logical 0x100000..0x104000 ( 16 KiB) -> phys 0x058912a57000 [DIVERGED]
clone[1]: logical 0x104000..0x200000 (1008 KiB) -> phys 0x057b4873d000 [SHARED]
st_blocks: 4096 (2048 KiB) -> 2048 (1024 KiB) on disk
logical size unchanged: 2048 KiB; punched range reads all zeros: true
extent map (64x32KiB): [................................D===============================] (= shared, D diverged, . hole)