Skip to content

fs: resolve overlay symlinks across layers - #856

Merged
ewhauser merged 2 commits into
mainfrom
claude/interesting-pare-325ca4
Oct 19, 2026
Merged

ewhauser merged 2 commits into
mainfrom
claude/interesting-pare-325ca4

Conversation

@ewhauser

Copy link
Copy Markdown
Owner

Summary

OverlayFS, the backend behind --root sessions, treated every lookup as Lstat, so symlinks in the read-only lower layer were never followed:

  • Stat behaved like Lstat, so [ -d link ] and [ -f flink ] were false.
  • mkdir -p link/sub failed with "Not a directory".
  • echo new > flink copied the target's bytes into the upper layer at the link's path. That replaced the symlink with a 0777 regular file and left the real target unchanged.

Paths are now resolved component by component through the merged upper+lower view. Each link is read from the layer that currently provides it, so a link in one layer can point at an entry that exists only in the other.

Follows the final link Resolves parent directories only
Stat, Open/OpenFile, ReadDir, Realpath, Chdir, Chmod, Chtimes, Chown(follow=true) Lstat, Readlink, Remove, Rename, Link, Symlink, Mkfifo, Lchtimes, Chown(follow=false)

Writes and metadata changes through a link copy up and modify the resolved target, and the link stays a link. MkdirAll follows symlinked directory components. O_CREATE|O_EXCL fails on any existing link, including a dangling one. Parent resolution also fixes rm link/f (it used to tombstone the wrong path, so real/f stayed visible) and echo x > link/new (it used to create a link/ directory in the upper layer that hid the symlink).

Performance

Walking each component made every lookup re-resolve all of its path prefixes in the lower layer. A new lowerDirs cache stores lower-layer directory prefixes. This is safe because the lower layer is never written through the overlay. Deep Stat over a HostFS lower (ad-hoc benchmark, Apple M3 Max):

ns/op
before ~64k
uncached walk ~232k
this PR ~68k

With an in-memory lower layer, deep Stat goes from 1.2µs to 4.3µs. The extra cost is per-component upper Lstats, all in memory.

SPEC

Added two bullets under the backend boundary section of SPEC.md describing the overlay's symlink semantics.

Tests

  • fs/overlay_symlink_test.go: Stat/Lstat, cross-layer links in both directions, Realpath/ReadDir/Chdir, MkdirAll, write-through, create-through (dir link, dangling link), O_EXCL, Chmod/Chtimes/Chown, and Remove through a symlinked parent. All 7 tests fail on main.
  • internal/runtime: TestOverlayFollowsLowerSymlinksForShellReadsAndWrites, an end-to-end shell script under SymlinkFollow. On main it fails with mkdir: cannot create directory "/w/link": Not a directory and cat real/f prints orig.
  • go test ./fs/... ./internal/runtime/... passes, and make lint reports 0 issues.
  • make test: everything passes except scripts/bench-compare TestExpansionStressScenarioMatchesExpectedStdoutUnderBash, which also fails on unmodified main on this machine (it runs host bash 3.2).

Known pre-existing issue (not fixed here)

Chmod/Chown/Chtimes on a lower directory copy its whole subtree up from the raw lower layer and ignore tombstones. After rm d/a; chmod 700 d, d/a shows up again. This is a separate fix.

OverlayFS used Lstat semantics everywhere, so Stat did not follow links,
MkdirAll rejected symlinked components, and writing through a lower
symlink copied the target's bytes over the link instead of writing the
target. Resolve every path through the merged upper/lower view, reading
each link from the layer that provides it, and copy up the resolved
target on write and metadata changes.
@ewhauser
ewhauser enabled auto-merge (squash) October 19, 2026 04:05
@ewhauser
ewhauser merged commit 562b690 into main Oct 19, 2026
16 checks passed
@ewhauser
ewhauser deleted the claude/interesting-pare-325ca4 branch October 19, 2026 04:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant