# Strata and mounting

---

# Strata and mounting

_Peios Kernel / Internals / StrataFS / Strata and mounting_

> Find the Peios kernel articles about strata and mounting.

Use these articles to understand strata and mounting in the Peios kernel.

- [The Stratum Stack](</kernel/internals/stratafs/strata-and-mounting/the-stratum-stack.md>)
- [Mount Admission](</kernel/internals/stratafs/strata-and-mounting/mount-admission.md>)
- [Absent Strata](</kernel/internals/stratafs/strata-and-mounting/absent-strata.md>)

---

# The Stratum Stack

_Peios Kernel / Internals / StrataFS / Strata and mounting_

> A mount is its stratum stack — an ordered, non-empty list fixed for the mount's life — plus the resolution context and the create stratum.

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

A mount is defined by its stratum stack: an ordered, non-empty list of
strata, highest-precedence first. The stack is fixed for the life of
the mount. Nothing reorders it, and precedence never varies by path, by
caller, or by operation. <a id="cite-strata.stack-fixed-and-ordered"></a>

Index 0 is the highest-precedence stratum. That convention runs all the
way through: strata are stored in a fixed array in mount-option order,
presence across the stack is a `u64` bitmap indexed by the same
position, and selecting the provider is the trailing-zero count of that
bitmap — the lowest set index, which is the highest-precedence stratum
that holds the name.

The array is `STRATAFS_MAX_STRATA` entries, which is **16**. A stack
longer than that is refused at parse time. <a id="cite-strata.max-sixteen"></a> Absence never renumbers
anything: an absent stratum simply has its bit clear, keeping its slot
and its precedence.

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

## A stratum is a path <a id="cite-strata.no-cached-resolution"></a>

What is stored for each stratum is a string and a flag word — nothing
else. No reference is held on a stratum's directory, and no resolution
result is retained across operations. Every time a name is resolved,
the stratum's path string is joined with the relative path of the name
and walked from scratch.

This is what makes a stratum follow a wholesale replacement. A package
transaction that renames the old tree aside and the new tree into place
leaves the original directory object intact and referenced by anything
that held it; a stratum defined as that object would go on serving the
replaced tree. Defined as a path, it follows.

The cost is that every lookup does the walk. [Revalidation](</kernel/internals/stratafs/coherency/model-and-revalidation.md#trm-4-4-2>) covers what that
means in practice, because the implementation does not cache
resolutions at all.

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

## The resolution context <a id="cite-mount.resolution-context-pinned"></a>

Stratum paths are joined absolute and walked from a root captured when
the mount was created — the creating process's own filesystem root —
under credentials captured at the same moment. Both are pinned for the
life of the mount and released only when the superblock is freed.

The credential override matters as much as the root. A stratum path is
resolved with the mounter's credentials, not with those of whatever
process later touches the mount, so a caller in a different mount
namespace cannot shift what a stratum denotes, and the paths reported
by the origin attribute ([The Origin Attribute](</kernel/internals/stratafs/start-here/the-origin-attribute.md#trm-4-7>)) always name the same things.

Symbolic links and mounts along a stratum's path are followed as they
stand at the moment of resolution, since the walk is an ordinary
`filename_lookup` with no restricting flags. A mount established inside
a stratum is therefore part of that stratum's tree, and one stratum can
span several filesystems — which [Inode Identity and Lifecycle](</kernel/internals/stratafs/coherency/inode-identity-and-lifecycle.md#trm-4-4-3>) has to account for when
deriving inode numbers.

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

## What a stratum's filesystem must provide <a id="cite-strata.mount-time-checks"></a>

Nothing beyond ordinary directory and file operations. stratafs probes
no capability at mount time: it checks only that each stratum path
resolves, names a directory, is not a duplicate of another stratum, and
does not push the composed stack past the kernel's maximum stacking
depth. There is no test for a usable directory version value, because
nothing in the implementation would use one.

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

## Flags <a id="cite-strata.stack-errors-are-einval"></a>

Each stratum carries zero or more flags, declared with it in the mount
options and stored as a bit field.

| Flag | Bit | Meaning |
|---|---|---|
| `create` | `STRATAFS_F_CREATE` | This stratum receives newly created objects and is the destination of copy-up. |
| `ro` | `STRATAFS_F_RO` | stratafs does not modify this stratum. |
| `am` | `STRATAFS_F_AM` | This stratum's directory may be absent. |

The stack-wide rules are decided in Rust, in `stratafs-core`: the stack
must be non-empty, must not exceed 16 strata, must contain no
unrecognised flag bit, must not carry `create` twice, and must not
carry `create` and `ro` on one stratum. The crate distinguishes five
error cases, but the C boundary collapses all of them to `EINVAL`, so
the distinction is not observable to a caller.

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

### `create` <a id="cite-strata.create-designates-creation-and-copy-up-destination"></a>

At most one stratum carries `create`, and a stack may carry none, in
which case `create_index` is `-1` and every creation and every copy-up
is refused with `EROFS`. Modification of an object provided by a
stratum that accepts modification is unaffected, because routing tests
the provider before it consults the create stratum at all ([Write Routing](</kernel/internals/stratafs/mutation/write-routing.md#trm-4-5-1>)).

`create` does not mean "writable", and it is not the only stratum
stratafs writes to. It designates where objects that do not yet exist
in any stratum are created, and where an object is copied when its
provider will not accept a modification. A stratum carrying neither
`create` nor `ro` is modified in place, and any number of such strata
may sit above the create stratum, below it, or both.

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

### `ro` <a id="cite-strata.ro-is-independent-of-filesystem"></a>

`ro` is a stratafs-level assertion, independent of whether the
stratum's filesystem is itself read-only. It is one of three terms in
the predicate that decides whether a stratum **accepts modification**
of an object it provides:

- the stratum does not carry `ro`;
- the provider's mount is not read-only;
- the provider's inode is not marked immutable.

The predicate takes only the superblock, the stratum index, and the
provider path. It reads no credentials, consults no security
descriptor, and calls into KACS not at all — which is what stops a
caller who has been *refused* write access from provoking a copy-up
([Write Routing](</kernel/internals/stratafs/mutation/write-routing.md#trm-4-5-1>)).

Note the third term is specifically the immutable inode flag. A file
that is unwritable by its mode bits is not excluded by this predicate;
the write is routed in place and the underlying filesystem refuses it.

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

### `am` <a id="cite-strata.am-governs-mount-time-only"></a>

Without `am`, a stratum's directory must exist when the mount is
created, and a mount whose stratum directory is absent fails with
`ENOENT`. With `am`, an absent directory is accepted at mount time.

The flag governs mount time only. At runtime the resolver never
consults it: a stratum whose directory has gone is skipped exactly the
same way whether or not it carries `am`. [Absent Strata](</kernel/internals/stratafs/strata-and-mounting/absent-strata.md#trm-4-2-4>) covers what absence
means once the mount is live.

---

# Mount Admission

_Peios Kernel / Internals / StrataFS / Strata and mounting_

> What a caller must be entitled to and what a configuration must satisfy — evaluation order, loop detection, and mount immutability.

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

A stratafs mount is configured entirely at mount time. This section
covers what the caller must be entitled to, the conditions a
configuration has to satisfy, and the order in which those conditions
are evaluated.

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

## Entitlement <a id="cite-mount.create-requires-init-userns-cap"></a>

Establishing a mount with no create stratum takes no privilege of its
own. The filesystem type carries `FS_USERNS_MOUNT`, and stratafs is on
the allowlist of types a mount namespace's descriptor may admit
([A Private Mount Table](</kernel/internals/kacs/mount-namespaces/a-private-mount-table.md#trm-3-13-1>)), so a caller may establish one in a mount namespace it created
without the mount privilege; what the caller needs beyond that is only
the access that resolving the stratum paths already requires, which
falls out of the resolution itself. A user namespace by itself confers
nothing here, as everywhere under KACS.

A stack that carries `create` is different. Copy-up is authorised by
the outer handle and deliberately requires no add-entry right on the
create stratum ([Checks on Merged Directories](</kernel/internals/stratafs/security/checks-on-merged-directories.md#trm-4-6-2>)), so the mount configuration carries authority
to materialise names in a real directory outside the mount. The caller
establishing such a stack must be operating in the **initial** user
namespace and hold `CAP_SYS_ADMIN` there; anything else fails with
`EPERM`.

The test is made in `get_tree`, before the tree is built and therefore
before any stratum path is resolved, and it is stricter than requiring
the capability alone: the credential's own user namespace must *be* the
initial one, not merely a namespace in which the capability resolves.
This is the closure the KACS copy-up context depends on. The check is
made once, when the immutable stack is established, rather than at each
copy-up, so descriptor delegation cannot reintroduce authorisation
against the acting task, and no reconfiguration can re-supply the
strata list.

There is no explicit per-stratum entitlement check in the mount path.
Both halves come out of the ordinary machinery: traverse rights are
enforced by the path walk, which runs under the mounting caller's
credentials, and the right to read a stratum directory's attributes is
enforced by using the security-checking `vfs_getattr` rather than its
unchecked variant, which reaches KACS's `getattr` hook and demands
`FILE_READ_ATTRIBUTES`.

One seam is worth recording: the path walk uses the credentials
captured on the filesystem context, while `vfs_getattr` runs outside
that override and so uses the acting task's. For a mount established
the ordinary way the two are the same task. For one established with
`fsopen` and `fsconfig` from different tasks they need not be.

Nothing re-checks entitlement if a stratum directory later appears.
That is sound because every access through the mount is checked against
the providing object in any case ([Access Check Delegation](</kernel/internals/stratafs/security/access-check-delegation.md#trm-4-6-1>)), so a caller who mounts a
stratum they cannot read still cannot read it.

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

## Validity

| Condition | Error |
|---|---|
| The stratum stack is empty | `EINVAL` <a id="cite-mount.admit.empty-stack-einval"></a> |
| The stack exceeds 16 strata | `EINVAL` <a id="cite-mount.admit.too-many-strata-einval"></a> |
| More than one stratum carries `create` | `EINVAL` <a id="cite-mount.admit.multiple-create-einval"></a> |
| A stratum carries both `create` and `ro` | `EINVAL` <a id="cite-mount.admit.create-with-ro-einval"></a> |
| The same directory appears as more than one stratum | `EINVAL` <a id="cite-mount.admit.duplicate-stratum-einval"></a> |
| A malformed `strata=` value ([Mount Options](</kernel/administration/stratafs/mount-options.md#trm-4-2-2>)) | `EINVAL` <a id="cite-mount.admit.malformed-value-einval"></a> |
| A create-bearing stack from outside the initial user namespace, or without `CAP_SYS_ADMIN` there | `EPERM` <a id="cite-mount.admit.create-requires-privilege-eperm"></a> |
| A stratum's path names something other than a directory | `ENOTDIR` <a id="cite-mount.admit.not-a-directory-enotdir"></a> |
| A stratum's directory is absent and the stratum does not carry `am` | `ENOENT` <a id="cite-mount.admit.absent-without-am-enoent"></a> |
| The composed stack reaches the kernel's maximum stacking depth | `ELOOP` <a id="cite-mount.admit.stacking-depth-eloop"></a> |
| A stratum lies within the mount point, or within another stratafs mount whose strata include this mount point | `ELOOP` <a id="cite-mount.admit.recursive-stratum-eloop"></a> |
| Sixteen consecutive collisions allocating a mount cookie | `EAGAIN` <a id="cite-mount.admit.cookie-exhaustion-eagain"></a> |

Two strata are the same directory when they resolve to the same
directory object, not merely when their paths are equal as strings: the
comparison is on resolved inodes, so two paths reaching one directory
through different symbolic links or bind mounts are caught. Absent
strata are skipped and never compared. <a id="cite-mount.duplicate-strata-by-inode"></a>

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

## Evaluation order <a id="cite-mount.admission-evaluation-order"></a>

The conditions that depend on nothing but the option string are decided
first, during parsing and stack-wide validation, and are reported
whatever the caller's access. The `EPERM` admission test comes next, in
`get_tree`, still before any path is touched. Only then are the strata
resolved.

The purpose of that ordering is to stop the validity conditions being
an oracle: a caller with no right to traverse a directory should not be
able to name it as a stratum and learn from the errno whether it exists
and whether it is a directory.

The path walk and the attribute read both run under the caller's
credentials, so a path the caller cannot resolve returns `EACCES` from
the walk itself, and the `EACCES` is propagated unchanged.

The check is stack-wide because it runs in two passes. The first
resolves and stats every stratum; an `EACCES` from any of them is the
answer, whatever an earlier stratum reported. Only when every stratum
has passed entitlement does the second pass apply the type, duplicate
and depth conditions. A caller who names a readable non-directory first
and an unreadable path second is told `EACCES`, not `ENOTDIR`.
<a id="cite-mount.entitlement-is-decided-in-two-passes"></a>

The mount-point loop condition is evaluated in a different call
entirely, after the tree has been built, and so always follows every
entitlement check.

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

## Loops <a id="cite-mount.loop-detected-at-mount"></a>

A stratum inside the mount point is detected directly. The indirect
case — a stratum inside *another* stratafs mount whose own strata
include this mount point — is detected by recursing into any stratum
whose superblock carries the stratafs magic, bounded by a visited-
superblock set and by the kernel's maximum stacking depth. This runs
through a Peios-added `super_operations` hook, `validate_mountpoint`,
wired into the new-mount, bind and move-mount paths by the patch series;
it is not an upstream interface.

That check cannot bind after the fact: a mount established within a
stratum, or a bind mount of this mount into one of its own strata, can
create a cycle later. Resolution is guarded separately, and differently
— not by a depth counter but by a per-task, per-superblock re-entrancy
list. A task that is already resolving in a superblock and re-enters it
gets `ELOOP` immediately, so a cycle spanning any number of stratafs
mounts terminates the moment it returns to one it is already inside.

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

## Immutability and mount identity <a id="cite-mount.strata-shared-between-mounts"></a>

The stack is fixed for the life of the mount; changing one is expressed
by unmounting and mounting again ([Mount Options](</kernel/administration/stratafs/mount-options.md#trm-4-2-2>)).

Two stratafs mounts may name the same directory as a stratum. Each
resolves independently and neither is aware of the other; where both
have a create stratum in common they mutate the same objects, with the
same result as any two writers of one directory. There is no registry
of stratum paths to make them aware of each other.

There is a registry of *mounts*, but it exists for a different purpose.
Each mount draws a random non-zero cookie and inserts itself into a
global table, retrying up to sixteen times on collision; a second
random non-zero cookie is drawn once per boot. The pair identifies
which live mount owns a staging entry, so that a mount sharing a create
stratum can distinguish another mount's copy-up in flight from an
orphan left by a crash ([Copy-Up](</kernel/internals/stratafs/mutation/copy-up.md#trm-4-5-2>)).

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

## An all-absent stack <a id="cite-mount.all-absent-stack-is-legal"></a>

A mount succeeds even when no stratum root is present at all — a stack
whose only strata are absent `am` strata is legal. The root inode is
constructed with no provider, and reports mode `S_IFDIR` with no
permission bits.

Such a root still carries a security descriptor, since an object with
none would refuse every access check made on the mount root and the
provider-less root could never be observed. stratafs synthesises it:
owned by the mounter, with read and traverse for everyone, and nothing
more — there is nothing beneath such a root to write. The moment a
stratum root appears the root's descriptor is that root's own, and
when it goes again the synthesised one returns; KACS never caches a
StrataFS root's descriptor, so no access is decided against a root that
has since gained or lost its provider. <a id="cite-mount.all-absent-root-serves-a-synthesised-descriptor"></a>

---

# Absent Strata

_Peios Kernel / Internals / StrataFS / Strata and mounting_

> A stratum's directory may not exist — what happens while it is absent, and what appearing, disappearing and reappearing do.

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

A stratum's directory may not exist. It may be absent when the mount is
created — which requires `am` ([Mount Admission](</kernel/internals/stratafs/strata-and-mounting/mount-admission.md#trm-4-2-3>)) — or exist at mount time and be
removed while the mount is live, which nothing prevents for any
stratum.

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

## While absent <a id="cite-strata.absent-contributes-nothing"></a>

An absent stratum holds no names, participates in no merged directory,
and contributes no entries to any enumeration. It keeps its position
and its precedence; only its presence bit is clear.

The mechanism is a single test in the resolver. Resolving one stratum
either succeeds, or fails with `ENOENT` or `ENOTDIR`, in which case the
stratum is skipped and its bit is left clear. Any other error —
`EACCES`, `EIO`, `ELOOP`, `ENAMETOOLONG`, `ESTALE` — is not treated as
absence and fails the whole resolution instead. So a stratum that is
unreadable for a reason other than not being there masks the name for
every stratum, rather than being passed over.

An absent create stratum additionally causes every operation that would
create or copy up to fail with `EROFS`. The create stratum's root is
re-resolved at the point of use and its `ENOENT` is mapped to `EROFS`
by each of the paths that needs it — the copy-up parent walk, the
creation authorisation pre-check, and the routing decision, which
computes create-stratum presence live and requires the result to be a
directory. stratafs does not create the stratum's own directory to
satisfy such an operation: establishing that directory, with the
security descriptor it should have, belongs to whatever provisions the
system, and a mount that minted it would be choosing that descriptor.

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

## Appearing and disappearing <a id="cite-strata.appearance-observed-on-next-lookup"></a>

Neither event is detected. Both are simply observed, because there is
nothing to invalidate.

A stratum's directory is never held; only its path string is. Every
resolution walks that string afresh, so a directory that comes into
existence is picked up by the very next lookup, and one that is
removed, renamed away, or replaced by another directory is picked up
just as immediately — the walk finds nothing, or finds the replacement.

The specification describes this in terms of a version tuple recorded
over the nearest existing ancestor of an absent stratum's path, and an
identity comparison for a stratum that disappears. Neither exists in
the implementation, and neither is needed: they are the machinery for
knowing when a *cached* resolution has gone stale, and nothing is
cached. [Revalidation](</kernel/internals/stratafs/coherency/model-and-revalidation.md#trm-4-4-2>) covers the trade that represents.

The `am` flag is not consulted at runtime at all. It governs whether
the mount is allowed to be established with the directory missing, and
nothing more; a stratum without it that vanishes afterwards behaves
exactly like one with it.

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

## Reappearance <a id="cite-strata.reappearance-preserves-inode-number"></a>

A stratum that disappears and reappears is the same stratum in the same
stack position, and nothing about the old directory is remembered.
Resolutions are recomputed against whatever the new directory holds.

Provider identity does survive the gap, though it is not resolution
memory. The derived inode number ([Inode Identity and Lifecycle](</kernel/internals/stratafs/coherency/inode-identity-and-lifecycle.md#trm-4-4-3>)) identifies the provider's
filesystem and inode number. If the same underlying object is reached
again — because the directory was renamed away and back rather than
replaced — it receives the same number it had before, without a
mount-lifetime pin on ordinary provider inodes. Only identities that
cannot be derived use the pinned fallback map. That is exactly what
the identity rule requires: equal numbers for one provider object,
whatever path reached it.
