# Name resolution

---

# Name resolution

_Peios Kernel / Internals / StrataFS / Name resolution_

> Find the Peios kernel articles about name resolution.

Use these articles to understand name resolution in the Peios kernel.

- [Lookup and type conflicts](</kernel/internals/stratafs/name-resolution/lookup-and-type-conflicts.md>)
- [Directory Merge](</kernel/internals/stratafs/name-resolution/directory-merge.md>)
- [Enumeration](</kernel/internals/stratafs/name-resolution/enumeration.md>)

---

# Lookup and type conflicts

_Peios Kernel / Internals / StrataFS / Name resolution_

> Resolving one name in one directory — the provider, ancestors, independence from the caller, and staging entries.

<a id="trm-4-3-1"></a>

Resolution is defined for one name in one directory. Every path
operation is a sequence of such resolutions, each independent of the
last.

<a id="trm-4-3-1-1"></a>

## The provider <a id="cite-resolution.provider-is-highest-stratum"></a>

To resolve a name in a stratafs directory, the corresponding directory
of each stratum is examined in precedence order, and the first stratum
holding an entry of that name is its **provider**. If no stratum holds
it, the resolution produces a negative dentry and the VFS reports
`ENOENT`.

Mechanically, each stratum's path string is joined with the relative
path of the name and walked in full, once per stratum. A walk that
succeeds sets that stratum's bit in a presence bitmap; a walk that
fails with `ENOENT` or `ENOTDIR` leaves it clear and the stratum is
skipped. Selecting the provider is then the trailing-zero count of the
bitmap, computed in `stratafs-core`.

Any other error from a stratum's walk — `EACCES`, `EIO`, `ELOOP`,
`ENAMETOOLONG`, `ESTALE` — is not treated as absence. It fails the
whole resolution, so a stratum that is unreadable for a reason other
than not being there masks the name entirely rather than being passed
over.

A joined stratum path, or a child relative path, that would exceed
`PATH_MAX` fails with `ENAMETOOLONG`.

<a id="trm-4-3-1-2"></a>

## Ancestors <a id="cite-resolution.provider-chosen-per-component"></a>

Resolution does not consult a parent's provider to find a child's. A
name's provider is chosen afresh across all strata, so if `/a` is
provided by stratum 2, `/a/b` may still be provided by stratum 1 —
provided stratum 1 also holds `/a` as a directory and the two therefore
merge ([Directory Merge](</kernel/internals/stratafs/name-resolution/directory-merge.md#trm-4-3-2>)).

What resolution does consult is whether any ancestor of the path is
masked. Before resolving the final component, every proper prefix of
the relative path is resolved across all strata and its merged provider
computed; a prefix whose provider is not a directory aborts the whole
resolution with `ENOTDIR`. That is what makes masking total ([Type Conflicts](</kernel/internals/stratafs/name-resolution/lookup-and-type-conflicts.md#trm-4-3-3>)),
and it is why a lookup costs one full walk per stratum for the name
itself plus one merged resolution per path component above it.

<a id="trm-4-3-1-3"></a>

## Independence from the caller <a id="cite-resolution.independent-of-caller"></a>

Resolution runs under the credentials captured at mount, against the
root captured at mount, and takes no operation argument. It does not
depend on the calling token, on what the caller is trying to do, or on
whether the operation will ultimately be permitted.

A name whose provider the caller may not access therefore resolves
normally and is then refused. It does not fall through to a lower
stratum — which would let a caller's rights change which file they
read, a considerably worse property than a denial.

<a id="trm-4-3-1-4"></a>

## Reaching the object <a id="cite-resolution.non-directory-forwarded"></a>

Once a name resolves to a non-directory provider, the object is the
provider's object and stratafs does not interpose on its contents. The
outer inode takes the provider's mode and, from it, the operations
tables for a regular file, symlink or special file; reads, writes,
mappings, splices, locks and `ioctl`s are forwarded to a backing file
opened on the provider.

Symbolic links are forwarded rather than followed. The outer inode's
`get_link` calls the provider inode's own and returns the raw target
verbatim; stratafs deliberately does not use `vfs_get_link`, which
would demand a read right the caller need not hold to traverse a link.
The VFS then interprets the target in the caller's own namespace, so an
absolute target resolves from the process's root and may re-enter this
mount, another stratafs mount, or none.

Where a mount is established at a path within a stratum, stratafs
follows it: the stratum walk is an ordinary `filename_lookup` with no
flag restricting it to one filesystem, and everything downstream
operates on the inner mount it returns.

<a id="trm-4-3-1-5"></a>

## Staging entries <a id="cite-resolution.staged-names-hidden"></a>

One class of name is invisible to resolution. While a copy-up is in
flight, its staged object may exist under a name in the create stratum;
that name is dropped from resolution for the mount that owns it, so an
incomplete copy is never reachable through the merged view ([Copy-Up](</kernel/internals/stratafs/mutation/copy-up.md#trm-4-5-2>)).
The suppression applies only to the create stratum, and only within the
owning mount — a second stratafs mount sharing that directory, and any
direct reader of it, sees an ordinary entry.

<a id="trm-4-3-1-5-1"></a>

### Recovery triggered by lookup <a id="cite-resolution.staging-prefix-triggers-recovery"></a>

Staged names begin with `.stratafs-stage-`, and a lookup of any name
with that prefix triggers a recovery scan of the create-stratum parent
before resolving. A resolution can therefore have the side effect of
removing orphaned staging entries from the create stratum.

<a id="trm-4-3-1-6"></a>

## Recursion <a id="cite-resolution.reentrant-superblock-eloop"></a>

A task that is already resolving inside a superblock and re-enters the
same superblock fails immediately with `ELOOP`. The guard is a global
list of task-and-superblock pairs, not a depth counter, so a cycle
formed after mount — a stratafs mount established inside one of its own
strata, or bind-mounted into one — terminates the moment resolution
returns to a mount it is already inside.

<a id="trm-4-3-3"></a>

Two strata may hold the same name with different types — a directory in
one, a regular file in another. The provider's type is the type of the
resolved object, and the outer inode is given the provider's mode and
the operations tables that follow from it. <a id="cite-resolution.conflict-provider-type-wins"></a>

- Where the provider is a **directory**, lower-precedence strata that
  hold the name as a directory participate in a merged directory
  ([Directory Merge](</kernel/internals/stratafs/name-resolution/directory-merge.md#trm-4-3-2>)). Lower-precedence strata that hold the name as anything else
  are masked. <a id="cite-resolution.conflict-directory-merges-only-directories"></a>
- Where the provider is **not** a directory, every lower-precedence
  entry of that name is masked, whatever its type. <a id="cite-resolution.non-directory-masks-lower"></a> Only the provider's
  path is retained on the dentry; the other strata's references are
  released as soon as the provider is chosen.

<a id="trm-4-3-3-1"></a>

## Masking is total <a id="cite-resolution.masking-hides-whole-subtree"></a>

A masked entry is unreachable through the mount. Where the masked entry
is a directory, its entire subtree is unreachable: no path beneath the
masked name resolves, regardless of what the masked directory contains
and regardless of whether some other stratum holds part of that
subtree.

This is enforced by the ancestor pass described in [Lookup](</kernel/internals/stratafs/name-resolution/lookup-and-type-conflicts.md#trm-4-3-1>). Each proper
prefix of a relative path is resolved across every stratum and its
merged provider computed; a prefix whose merged provider is not a
directory returns `ENOTDIR` before the final component is considered.
Because the test uses the merged answer rather than a per-stratum one,
another stratum holding the subtree cannot rescue it.

A regular file at `/x` in a high-precedence stratum therefore hides a
whole `/x/…` tree in a lower one. That is severe, and deliberate: the
alternative — resolving `/x` as a file but `/x/y` through the masked
directory — would make a path's meaning depend on how far along it the
caller looked.

<a id="trm-4-3-3-1-1"></a>

### Masking leaves the strata unchanged <a id="cite-resolution.masking-does-not-modify-strata"></a>

Masking modifies nothing. Resolution and enumeration are read-only
throughout, no marker is written to any stratum, and the masked entry
remains present and unchanged in its own stratum, reachable by any path
that does not traverse the mount.

<a id="trm-4-3-3-2"></a>

## Stability <a id="cite-resolution.conflict-operation-fails-against-provider"></a>

Type conflicts resolve identically for every caller and every
operation, because provider selection is a pure function of the
presence bitmap and consults neither.

An operation that would only be valid against the masked type does not
cause the masked entry to be selected. It fails against the provider
instead, with whatever error that type produces — typically `ENOTDIR`
from the ancestor pass, or `EISDIR` raised by the generic VFS against
an outer inode carrying the provider's mode.

---

# Directory Merge

_Peios Kernel / Internals / StrataFS / Name resolution_

> When lower strata hold the same name as a directory the result is a merged directory — participation, its create stratum, and symlinks.

<a id="trm-4-3-2"></a>

Where a name's provider is a directory, and lower-precedence strata
hold the same name as a directory, the resolved object is a **merged
directory**.

<a id="trm-4-3-2-1"></a>

## Participation <a id="cite-resolution.merge-participants"></a>

The strata participating in a merged directory are, in precedence
order, those whose corresponding directory both exists and is a
directory. A stratum holding the name as a non-directory does not
participate and is masked entirely ([Type Conflicts](</kernel/internals/stratafs/name-resolution/lookup-and-type-conflicts.md#trm-4-3-3>)); a stratum not holding the
name simply does not participate.

Every consumer of a merged directory applies the same two-part filter —
the presence bit is set, and the resolved dentry is a directory — in a
single ascending loop over stratum indices with a `continue` for
non-participants. Nothing compacts or re-sorts, so relative precedence
within a merged directory is always the relative precedence in the
stack.

Merging is recursive, and it is recomputed rather than cached: there is
no merged-directory object anywhere. Each lookup and each directory
open rebuilds the full per-stratum path set from the relative path
string, so any child that is a directory in more than one stratum
merges at its own level by the same rule.

<a id="trm-4-3-2-1-1"></a>

### Participation at the mount root <a id="cite-resolution.root-participation"></a>

The root of a mount is a merged directory whose participants are the
mount's strata. The root has one special case: where the ordinary
provider rule would pick a stratum root that is present but not a
directory, the root instead takes the first stratum that is both
present and a directory, so an absent or non-directory stratum root
cannot change the synthetic root's type. A mount whose stratum roots
are all absent still has a root inode, with no provider and no
permission bits.

<a id="trm-4-3-2-2"></a>

## The create stratum of a merged directory <a id="cite-resolution.merge-create-stratum-is-positional"></a>

A merged directory's create stratum is the correspondingly-named
subdirectory of the mount's create stratum, at the same path relative
to the mount root — whether or not that subdirectory currently exists.
It follows that a merged directory has a create stratum even when the
mount's create stratum holds no part of that path, and creation still
routes there.

The derivation is positional and mount-wide: `create_index` is a single
integer on the superblock, fixed at mount, and every creation and
copy-up site reads it directly. Nothing re-derives it from which strata
happen to participate.

That matters because the alternative — taking the create stratum to be
the highest-precedence participating writable directory — would make
the destination of a write depend on which directories happened to
exist, so creating a file in a subdirectory could land in a different
stratum from creating one beside it.

Where the create stratum's counterpart of a merged directory does not
exist, the path is materialised on demand, from the mount root
downwards, at the point an operation first needs it ([Copy-Up](</kernel/internals/stratafs/mutation/copy-up.md#trm-4-5-2>)). The
authorisation for creating into it is evaluated against the descriptor
that directory will carry once materialised, which is the corresponding
provider directory's ([Checks on Merged Directories](</kernel/internals/stratafs/security/checks-on-merged-directories.md#trm-4-6-2>)).

<a id="trm-4-3-2-3"></a>

## Symbolic links and participation <a id="cite-resolution.merge-participant-set-resolves-like-lookup"></a>

Two resolution entry points disagree about the final component of a
stratum path, and the difference is visible.

Ordinary lookup resolves without following the final component, so a
symbolic link at a name resolves to the link itself and the VFS follows
it. Every site that builds the participant set resolves the same way —
the set itself, the emptiness scan, the foreign-entry scan, the
permission check and directory `fsync`.

A symlink is a non-directory, so a stratum holding a name as one is
masked entirely, exactly as [Type Conflicts](</kernel/internals/stratafs/name-resolution/lookup-and-type-conflicts.md#trm-4-3-3>) has it: it does not participate,
contributes no entries to the merged listing or to emptiness tests, and
masks whatever the strata below hold at that name. A dangling symlink
is the same, and for the same reason. <a id="cite-resolution.symlink-masks-like-any-non-directory"></a>

The two views agreeing is what closes a confused-deputy shape. Were the
participant set to follow the final component while lookup did not, a
stratum owner who replaced a directory with a symlink would change
which real directory contributed entries to another stratum's merged
view, and the permission check would run against a target the mounter
never named.

---

# Enumeration

_Peios Kernel / Internals / StrataFS / Name resolution_

> The union of names across participating strata, each appearing once — captured at open, with consistency and position rules.

<a id="trm-4-3-4"></a>

Enumerating a merged directory yields the union of the names held by
its participating strata, with each distinct name appearing exactly
once. <a id="cite-resolution.merged-directory-is-union"></a>

<a id="trm-4-3-4-1"></a>

## Capture at open

The whole listing is built when the directory is opened, and the
enumeration is served from that capture for the life of the descriptor.
Each participant is opened and iterated in ascending stratum order, and
its entries are appended to one list. <a id="cite-enumerate.captured-at-open"></a>

Deduplication is global rather than per-stratum: before an entry is
recorded, the whole accumulated list is scanned for the same name, and
a match causes the entry to be dropped. Because participants are
visited highest-precedence first, the entry that survives is always the
provider's. <a id="cite-enumerate.duplicate-name-resolves-to-provider"></a> `.` and `..` are dropped from every participant and
synthesised once. <a id="cite-enumerate.dot-entries-synthesised-once"></a>

Each surviving entry carries the name, its length, the directory entry
type reported by the providing participant, and an inode number
obtained by looking the child up in that same participant and deriving
its identity, with allocation only as a fallback ([Inode Identity and Lifecycle](</kernel/internals/stratafs/coherency/inode-identity-and-lifecycle.md#trm-4-4-3>)), so `getdents` and
`stat` agree. <a id="cite-enumerate.entry-inode-matches-stat"></a> The final component is not followed during that lookup,
so a symlink entry reports its own identity rather than its target's. <a id="cite-enumerate.symlink-entry-reports-itself"></a>

Two details of that: an entry that vanishes between the participant's
own `readdir` and the follow-up lookup is silently dropped, which is
ordinary provider behaviour; and a participant filesystem that reports
`DT_UNKNOWN` has that propagated unchanged, even though the child path
is in hand and the real type could be derived. <a id="cite-enumerate.dt-unknown-propagated"></a>

Shadowed entries are neither reported nor otherwise detectable. No
second record is ever allocated, so nothing about them survives into
the listing, and the entry count reflects distinct names only. <a id="cite-enumerate.shadowed-entries-invisible"></a>

The order in which names are reported is stratum-ascending and then
each stratum's own `readdir` order — deterministic for one capture, and
not otherwise specified. <a id="cite-enumerate.order-stratum-then-readdir"></a>

The implementation uses a temporary, per-open name hash while capturing
entries, then an immutable position index for enumeration. Growing the
hash does not reorder entries or change which provider wins a duplicate.
The hash buckets are released before the open succeeds. Capture remains
eager under the opener's authority; the index does not defer any part of
that authorisation to a later reader or descriptor recipient.

Deduplication takes expected linear work in the number of names offered
by the participants. Resuming enumeration or seeking to an ordinal does
not scan earlier entries again. Provider enumeration, child lookup and
access checks still contribute their own costs. Each independent open
retains its own names and position index, so memory use still grows with
the number of distinct captured names and simultaneously open file
descriptions. These are implementation properties, not time or memory
limits promised to callers.

<a id="trm-4-3-4-2"></a>

## Consistency <a id="cite-enumerate.changes-after-open-invisible"></a>

There is exactly one capture per descriptor. Nothing appends to the
list after the directory is opened, and nothing re-captures — a
rewind replays the original capture, since the directory uses the
generic `llseek`. A change to any participating stratum after the open
is therefore invisible to that descriptor for its lifetime.

What that bounds is only what stratafs itself contributes. Each
participating directory is enumerated by its own filesystem, with
whatever consistency that filesystem offers its own callers, and
participants are read sequentially with no cross-stratum lock or
barrier. A merged listing is no better than the listings it is
assembled from, and nothing claims otherwise.

<a id="trm-4-3-4-3"></a>

## The settled participant set <a id="cite-enumerate.participant-set-settled-at-open"></a>

For the purpose of enumeration, the participating set is settled when
the directory is opened and does not change for the life of the
descriptor. What is settled is the set of participating **directory
objects**, not the set of stratum positions: the descriptor holds a
`struct path` reference on each participant, pinned until release, and
nothing re-resolves those positions by path. A participant that has
since been removed and replaced by another directory at the same path
is a different object and contributes nothing.

The settled set has exactly three consumers: the enumeration itself,
the access check performed when the directory is opened ([Checks on Merged Directories](</kernel/internals/stratafs/security/checks-on-merged-directories.md#trm-4-6-2>)), and
the origin attribute read through that descriptor ([The Origin Attribute](</kernel/internals/stratafs/start-here/the-origin-attribute.md#trm-4-7>)). It extends to
nothing else. Resolving a name relative to the descriptor — opening,
removing, renaming, linking — is an ordinary live resolution under
[Lookup](</kernel/internals/stratafs/name-resolution/lookup-and-type-conflicts.md#trm-4-3-1>), performed against the strata as they are at that moment.

So a descriptor opened before a stratum began to hold the directory
will not list that stratum's names, but `openat` through the same
descriptor will resolve them. The two answers differ deliberately:
enumeration is a bulk disclosure whose authorisation was decided once,
when the descriptor was opened; a resolution is a fresh operation that
carries its own check.

Freezing the participant set is what keeps that open-time check
meaningful. Were a later re-read free to admit a stratum that joined
afterwards, its names would reach the caller without its directory's
descriptor ever having been consulted — and a stratum owner could make
a directory participate precisely to expose it. A caller that wants the
current participant set reopens.

<a id="trm-4-3-4-4"></a>

## Positions <a id="cite-enumerate.offsets-are-plain-ordinals"></a>

Offsets 0 and 1 are `.` and `..`. Offset `2 + k` is the k-th element of
the captured list — a plain ordinal. The offset each participant
filesystem supplies is discarded: no provider cookie, no stratum index,
and no name hash is encoded.

A position is therefore meaningful only within one open file
description. On close and reopen — and so across a remount or a reboot —
the capture is rebuilt from each stratum's current contents and current
`readdir` order, and ordinal `2 + k` may name a different entry or
none. `telldir` and `seekdir` across descriptors are unreliable on a
stratafs directory, as is NFS re-export of one.

<a id="trm-4-3-4-5"></a>

## Access <a id="cite-enumerate.requires-rights-on-every-participant"></a>

Enumeration requires traverse and list rights on **every** participating
directory, checked before the capture is built. The check returns on
the first refusal, and a refusal aborts the open entirely, so no
partial listing covering only the readable strata can be produced.
