# Mutation

---

# Mutation

_Peios Kernel / Internals / StrataFS / Mutation_

> Find the Peios kernel articles about mutation.

Use these articles to understand mutation in the Peios kernel.

- [Deferred deletion](</kernel/internals/stratafs/mutation/deferred-deletion.md>)
- [Write Routing](</kernel/internals/stratafs/mutation/write-routing.md>)
- [Copy-Up](</kernel/internals/stratafs/mutation/copy-up.md>)
- [Creation](</kernel/internals/stratafs/mutation/creation.md>)
- [Removal](</kernel/internals/stratafs/mutation/removal.md>)
- [Rename](</kernel/internals/stratafs/mutation/rename.md>)
- [Links](</kernel/internals/stratafs/mutation/links.md>)
- [Locking](</kernel/internals/stratafs/mutation/locking.md>)
- [Durability and Accounting](</kernel/internals/stratafs/mutation/durability-and-accounting.md>)

---

# Deferred deletion

_Peios Kernel / Internals / StrataFS / Mutation_

> Deferred deletion: behaviour and implementation of the Peios kernel.

<a id="trm-3-9-7-5"></a>

## Deferred deletion <a id="cite-facs.stratafs-copy-up.deferred-deletion"></a>

A delete-pending mark is authorized when it is set, and the setting
open's token is recorded with it ([Sharing and Deletion](</kernel/internals/kacs/file-security/sharing-and-deletion.md#trm-3-9-10-3>)). At the
last close there is no re-authorization against the closing task. <a id="cite-facs.stratafs-copy-up.close-no-reauth"></a> Instead a synchronous, non-nesting internal
deletion scope is armed for the outer path the marking open named, captured
when the mark was set, and StrataFS binds
it once to the exact provider parent, dentry and inode selected from
the descriptor's settled provider. <a id="cite-facs.stratafs-copy-up.deletion-scope-binding"></a> Only the corresponding outer and
lower unlink calls match, and the scope is cleared on every return.

The mechanism never turns an ordinary close into deletion authority,
never admits a different provider entry, and never permits unlinking
an entry that no longer names the descriptor's inode. <a id="cite-facs.stratafs-copy-up.deletion-identity-check"></a> If the original
entry has disappeared or changed identity, the deletion is already
complete and no exemption is used.

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

## Deferred deletion <a id="cite-create.deferred-delete-targets-resolved-object"></a>

A request to delete an object when the last descriptor to it is closed
applies to the object the descriptor resolved to, not to whatever
provides that name at close time. Because removal ([Removal](</kernel/internals/stratafs/mutation/removal.md#trm-4-5-4>)) is defined
over the current provider of a name, the deferred case has its own
path.

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

### An entry already gone <a id="cite-create.deferred-delete-already-gone-succeeds"></a>

When the deletion is attempted, stratafs locates the entry at the path
the descriptor was opened against, in the stratum that provided it at
that time — the descriptor's private dentry carries both — and resolves
the parent in that same stratum. Four conditions end the attempt
quietly, reporting success because the deletion is already complete:
the parent is gone, the name is gone, the name now identifies a
different inode, or the unlink raced.

The merged parent can disappear or become a non-directory before the
VFS reaches stratafs’s unlink handler. An `ENOENT` or `ENOTDIR` from
that earlier check is also a successful no-op, but only after a lookup
in the descriptor’s original providing stratum confirms that its
recorded name is absent or identifies a different inode. A higher
stratum masking a still-live backing target does not satisfy that
condition: the deletion still fails and is audited. Other errors keep
their ordinary failure and audit behavior.

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

### The original stratum must accept removal <a id="cite-create.deferred-delete-requires-writable-stratum"></a>

That stratum must accept modification, or the deletion fails with
`EROFS`. Then the entry is removed, and no other. In particular, where
another caller's copy-up has published a new object at that name in a
higher stratum, that object is not the one being deleted and is left
alone.

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

### Failures are audited <a id="cite-create.deferred-delete-audits-failure"></a>

Because the attempt has no caller to report to, any failure is audited
— and for a deferred deletion, *every* non-zero result is audited, not
only the arrangement errors [Audit](</kernel/internals/stratafs/security/audit.md#trm-4-6-5>) covers for ordinary refusals. The
object is left in place.

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

### Authority at deletion time <a id="cite-create.deferred-delete-checked-against-the-providing-stratum"></a>

The right to delete the entry is checked against the directory of the
**stratum where the entry lives**, not against the merged parent, and
it is checked at deletion time rather than only at arm.

Arm time could not do it. The authorizer there holds a `struct file`,
and which stratum provides the entry is stratafs-internal — the
provider index and the parent's relative path are not visible to KACS.
stratafs computes that directory before removing anything, so the check
happens there, where the answer exists.

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

### The requesting token outlives the arm <a id="cite-create.deferred-delete-token-outlives-the-arm"></a>

The token is the one that requested the deferred deletion, not the
credentials of whoever closes the descriptor. That requires it to
outlive the arm: the file's blob takes a reference when armed and
releases it in `->file_release`, on both the armed and unarmed exits.
The file is the thing that was armed, so its lifetime is the right
one — arm and close may be far apart and in different tasks.

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

### Supported object types <a id="cite-create.deferred-delete-non-directories"></a>

Deferred deletion is restricted to non-directories, and at arm time to
regular files on a managed mount.

## Related explanations

- [Creation](</kernel/internals/stratafs/mutation/creation.md>)

---

# Write Routing

_Peios Kernel / Internals / StrataFS / Mutation_

> Which single stratum a modification is performed against, when that decision is made, and what shared writable mappings do to it.

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

An operation that modifies an existing object is performed against
exactly one stratum. This section covers which, and when the decision
is made.

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

## Accepting modification <a id="cite-write.accepts-modification"></a>

A stratum **accepts modification** of an object it provides when all
three of the following hold:

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

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

### Independence from the caller’s rights <a id="cite-write.routing-does-not-use-caller-rights"></a>

The predicate is a property of the stratum and the object alone. It
takes the superblock, the stratum index and the provider path, and
nothing else — no credentials, no security descriptor, no access
check.

That restriction is load-bearing. Were the predicate to take the
caller's rights into account, a caller *refused* write access by the
provider's descriptor could still provoke a copy-up: the write would
fail, but the copy would have been published, and from then on the
merged path would resolve to a snapshot the provider's legitimate
writer could no longer update. A caller with no write access at all
could freeze any file in the mount.

Note the third term is the immutable inode flag specifically, not
unwritability in general. A file that is unwritable by its mode bits is
routed in place and refused by the underlying filesystem.

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

## The rule

The decision itself is `route_existing` in `stratafs-core`, which takes
the provider index, whether it accepts modification, the create index
and whether the create stratum is present, whether the object is of a
copyable type, and whether the stratafs mount itself is read-only. It
selects in-place modification, copy-up, or read-only through the following
ordered checks.

1. If the **mount** is read-only, the route is read-only. This term
   short-circuits everything else. <a id="cite-write.read-only-mount-short-circuits"></a>
2. If the provider accepts modification, the operation is performed
   against the provider's object. <a id="cite-write.in-place-when-provider-accepts"></a>
3. Otherwise, if the object is copyable, a create stratum exists, is
   present, and has **strictly higher precedence than the provider**,
   the object is copied up and the operation performed against the
   copy. <a id="cite-write.copy-up-when-create-outranks"></a>
4. Otherwise the route is read-only and the operation fails with
   `EROFS`. <a id="cite-write.erofs-when-no-writable-stratum"></a>

The strict `create_index < provider` comparison is what stops a
modification being placed where something already present would shadow
it. Where a high-precedence stratum provides a name it will not accept
a write for, there is no lower stratum that can take the write without
the result vanishing behind the provider, so `EROFS` is the honest
answer — reported at the moment of the write rather than discovered
later.

Where the name is held by no stratum, the operation is a creation and
[Creation](</kernel/internals/stratafs/mutation/creation.md#trm-4-5-3>) applies instead.

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

## When the decision is made

Routing happens as part of a modifying operation, not an ordinary open.
Most mutating entry points call `route_existing` afresh against their
selected provider. Shared write-capable mappings prepare the route before
the memory mapping lock is taken and recheck the selected provider in the
mapping callback. Opening a descriptor does not reserve a writable route.
<a id="cite-write.routed-per-operation"></a>

| Operation | Routes |
|---|---|
| Writing or appending | Yes <a id="cite-write.routes.write"></a> |
| Truncating, or any other `setattr` | Yes <a id="cite-write.routes.setattr"></a> |
| Changing mode, owner, timestamps, or the security descriptor | Yes <a id="cite-write.routes.metadata"></a> |
| Setting or removing an extended attribute | Yes <a id="cite-write.routes.xattr"></a> |
| `fallocate` | Yes <a id="cite-write.routes.fallocate"></a> |
| `splice` into the file | Yes <a id="cite-write.routes.splice"></a> |
| `copy_file_range` and `remap_file_range` | Yes <a id="cite-write.routes.copy-range"></a> |
| Establishing a shared writable mapping | Yes <a id="cite-write.routes.shared-mapping"></a> |
| Reading contents, attributes, or extended attributes | No <a id="cite-write.routes.read-does-not"></a> |
| Opening, for any access | No <a id="cite-write.routes.open-does-not"></a> |
| Taking or releasing a lock, or a lease | No <a id="cite-write.routes.lock-does-not"></a> |

The security descriptor and the system access control list are both
reached as extended attributes, so both route through the `setxattr`
path like any other attribute; there is no descriptor-specific code in
stratafs at all. <a id="cite-write.descriptor-routes-as-xattr"></a>

An open is not itself a routing trigger. `open` computes a route, but
uses it only to decide the flags of the backing open — a non-in-place
route downgrades the provider open to read-only and strips `O_TRUNC`,
deferring rather than deciding. <a id="cite-write.open-defers-routing"></a> The one case in which an open acts is
`O_TRUNC` on a regular file: that is a modification, so it routes, and
copies up or fails with `EROFS` there and then. The truncation itself
is applied afterwards by the VFS through `setattr`, which routes again
and lands on the copy. <a id="cite-write.o-trunc-routes-at-open"></a>

Writes to regular files use the open file description's current
`O_APPEND` setting, including changes made with `F_SETFL` after open or
copy-up. Clearing the flag restores writes at the selected offset and
allows `splice` and range-copy operations to use their explicit offsets.
The provider's append-only restrictions and flag checks still apply.

Routing at operation time rather than at open is what makes the rule
implementable. A filesystem cannot see what an open asked for in
access-mask terms — the caller's descriptor is stamped by KACS before
the filesystem's own open method runs, and its mask lives in a private
blob a filesystem cannot reach. Nor does it need to: the access check
has already happened by the time an operation reaches stratafs, so a
modifying operation arriving here is one its caller was entitled to
perform, and routing it is a decision about strata alone.

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

## Shared writable mappings <a id="cite-write.mmap-routes-on-maywrite"></a>

Establishing a shared mapping that can become writable routes before any
bytes are written. Stores through the mapping can then modify its backing
object without another filesystem operation that could perform copy-up.

Preparation applies to regular files mapped with `MAP_SHARED` or
`MAP_SHARED_VALIDATE` through a readable, writable open file description.
This includes `PROT_READ`: the shared mapping retains `VM_MAYWRITE` and can
later gain write access through `mprotect`. Private mappings and shared
mappings that cannot gain write access do not trigger preparation.
Ordinary writable opens remain lazy; opening alone does not copy up a file.

The existing KACS mapping checks run before preparation. Preparation
follows direct StrataFS providers and the selected backing file of FUSE
passthrough mappings. It completes routing and any required copy-up for
each StrataFS layer before the VFS acquires `mmap_lock`. It grants no
additional access, and the backing file's mapping checks still apply.

The mapping callback pins the selected provider and forwards only an
in-place route. If a route change after preparation could require another
copy-up, the mapping fails with `EAGAIN`. Retry the whole `mmap`
call so that preparation runs again. Definitive read-only conditions still
fail with `EROFS`.

A completed copy-up remains published if a later mapping check or provider
callback fails. The same applies if an earlier layer completes copy-up
before preparation fails in a deeper layer. Preparation does not roll back
copies that another operation could already have observed.

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

## What a copy-up does to an open descriptor <a id="cite-write.copying-descriptor-follows-copy"></a>

A copy-up performed for one descriptor changes which object provides
the name. That descriptor refers to the copy from then on: it keeps the
inode it was opened against while the inode's backing object becomes
the copy ([Inode Identity and Lifecycle](</kernel/internals/stratafs/coherency/inode-identity-and-lifecycle.md#trm-4-4-3>)), and its backing file is replaced.

Every other descriptor already open against the original continues to
refer to the original, and any fresh resolution of the path yields a
new inode standing for the copy. The pre-copy-up file is not closed —
it is retained so that locks and leases taken before the copy-up keep
working ([Locking](</kernel/internals/stratafs/mutation/locking.md#trm-4-5-7>)).

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

## Special files <a id="cite-write.special-files-never-copied-up"></a>

A FIFO, socket, or device node is opened and written without the
filesystem object being modified: what is written passes to a pipe, a
socket, or a driver, not to the object's contents. Writing to such an
object therefore does not route, and such an object is never copied up
— every routing site gates on the object being a regular file, and the
copyable flag excludes anything that is not a regular file, directory
or symlink.

Copying up a FIFO would sever it: a reader holding the original and a
writer that arrived after the copy would hold two unrelated pipes. A
device node survives copying only by the accident that the copy names
the same device.

Opens, reads and writes are forwarded to the provider whether or not it
accepts modification, since the write-mode downgrade and the `O_TRUNC`
refusal both apply to regular files only. Operations that modify the
object itself — its mode, its descriptor, its extended attributes — do
route, and where the provider does not accept modification they fail
with `EROFS`, because the copy-up branch cannot apply to an object that
is never copied up.

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

## `ioctl` <a id="cite-write.ioctl-regular-file-refused"></a>

`ioctl` on a regular file is refused unconditionally with `ENOTTY`, and
its compat form with `ENOIOCTLCMD`. Stored-file `ioctl`s can mutate
data and would need command-by-command routing, which is not
implemented; refusing is the conservative stand-in. `ioctl` on a
non-regular file is forwarded to the provider.

---

# Copy-Up

_Peios Kernel / Internals / StrataFS / Mutation_

> Replicating an object from its provider into the create stratum — parents, what is replicated, staging and atomicity.

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

Copy-up replicates an object from its provider stratum into the create
stratum, at the same path relative to the mount root, so that a
modification can be applied without modifying the provider. <a id="cite-copy-up.replicates-into-create-stratum"></a>

Every step runs inside a KACS copy-up context, which exempts the
mechanics from caller authorisation without granting anything. [The StrataFS Copy-Up Context](</kernel/internals/stratafs/security/kacs-admission-and-object-phases.md#trm-3-9-7>)
describes that context in full; [Descriptors on Copy-Up](</kernel/internals/stratafs/security/copy-up-descriptors.md#trm-4-6-3>) covers the stratafs side of the
bargain.

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

## Parents <a id="cite-copy-up.materialises-missing-parents"></a>

Where the create stratum does not hold the directories containing the
object's path, they are created first, walking the relative path
component by component from the create-stratum root downwards. Each is
created as an empty directory with the mode of the corresponding
**merged provider** directory, and with that directory's security
descriptor, installed by KACS during the create phase rather than
inherited.

Contents are not copied. The directories exist to hold the copied
object; the entries they hold in lower strata continue to be reached by
merging.

Where the create stratum holds one of those components as something
other than a directory, the copy-up fails with `ENOTDIR` and the
operation requiring it fails. The blocking entry is not removed or
replaced — there is no `unlink`, `rmdir` or `rename` anywhere on the
parent-materialisation path.

Materialised parents receive a mode and a descriptor, and nothing else:
no extended attributes and no timestamp preservation.

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

## What is replicated

| Provider type | Result in the create stratum |
|---|---|
| Regular file | A regular file with the same contents and mode <a id="cite-copy-up.result.regular-file"></a> |
| Symbolic link | A symbolic link with the same target <a id="cite-copy-up.result.symlink"></a> |
| Directory | An empty directory with the same mode; contents are not copied <a id="cite-copy-up.result.directory"></a> |

A device node, FIFO, or socket is never copied up. Anything that is not
a regular file, directory or symlink is refused with `EROFS` before a
copy-up begins. <a id="cite-copy-up.non-copyable-refused"></a>

The security descriptor is carried by KACS rather than replicated as an
attribute ([Descriptors on Copy-Up](</kernel/internals/stratafs/security/copy-up-descriptors.md#trm-4-6-3>)). Every other extended attribute is copied, with
three exclusions: the canonical descriptor attribute, anything in the
`system.stratafs.` namespace, and the staging marker attribute. Each
is copied with `XATTR_CREATE`, and `security.capability` goes through a
dedicated KACS entry point rather than a raw write. <a id="cite-copy-up.xattrs-copied-with-exclusions"></a>

No attribute is silently discarded. Any per-attribute failure aborts
the copy-up, as does a listing that fails or exceeds `XATTR_LIST_MAX`;
in every case the error reported is `EIO`, whatever the underlying one
was. <a id="cite-copy-up.xattr-failure-aborts-with-eio"></a>

Modification timestamps are preserved for every type, symbolic links
included. <a id="cite-copy-up.mtime-preserved"></a> The mode is carried across too, except for a symbolic
link, whose mode is fixed by the VFS and not the caller's to set. <a id="cite-copy-up.mode-preserved-except-symlinks"></a>
Access and change times are not preserved for any type. <a id="cite-copy-up.atime-ctime-not-preserved"></a>

**Ownership is preserved.** The staged object is created with the
calling task's credentials, so it begins owned by the caller; where
that differs from the source's uid or gid, a second `notify_change`
sets both to the source's. <a id="cite-copy-up.posix-ownership-preserved"></a> The KACS security descriptor, including
its owner SID, is preserved as well ([Descriptors on Copy-Up](</kernel/internals/stratafs/security/copy-up-descriptors.md#trm-4-6-3>)), so both notions of owner
name the object's owner rather than whoever provoked the copy.

The chown runs under the **mount's own credential** — the same
resolution context stratafs uses to reach providers the caller cannot
([The Stratum Stack](</kernel/internals/stratafs/strata-and-mounting/the-stratum-stack.md#trm-4-2-1>)) — because setting another uid needs authority the caller does
not have. <a id="cite-copy-up.ownership-chown-runs-under-resolution-credential"></a> It is a separate change from the mode and timestamp
above, which are the caller's to set and stay that way.

Because disk quota keys on the POSIX owner, a copy is accounted to the
owner of the object it was copied from and not to the caller. <a id="cite-copy-up.accounted-to-preserved-owner"></a> That is
what makes [Checks on Merged Directories](</kernel/internals/stratafs/security/checks-on-merged-directories.md#trm-4-6-2>)'s exemption sound: a caller who may write a file in a
stratum that will not accept modification can cause an entry to appear
in the create stratum without holding rights over it, gaining neither
access nor space.

Hard links are not preserved: an object with several links in its
stratum is copied up as a single independent object, and the other
links continue to refer to the original. <a id="cite-copy-up.hard-links-not-preserved"></a>

Contents are copied through a 64 KiB buffer, one read and one write per
iteration, with the inner write loop retrying short writes. Where the
copy-up was provoked by a path rather than a descriptor, the source is
reopened for each chunk; the staged file is reopened for each chunk
either way.

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

## The source must still be the provider <a id="cite-copy-up.source-must-still-be-provider"></a>

Before beginning a copy-up on behalf of a descriptor, the object the
descriptor refers to is verified still to be the provider of that name,
comparing both the path and the provider inode. Where it is not, the
copy-up is not performed and the operation fails with `ESTALE`.

The verification is repeated immediately before publication, under the
create directory's lock, and publication itself uses an operation that
fails if the target name already exists: linking an anonymous object
into place, or a `RENAME_NOREPLACE`. Both additionally test the target
dentry explicitly, and an `EEXIST` from either is normalised to
`ESTALE`.

That is decisive for the case that matters. A competing copy-up
publishes into the same create-stratum directory, where the
filesystem's own atomicity applies, so exactly one of two racing
copy-ups succeeds and the other fails `ESTALE`.

It cannot extend further, and nothing tries to. Strata may be on
different filesystems, and stratafs can neither lock them together nor
inspect them at one instant. A change in some *other* stratum after the
second verification — a direct writer creating the name in a
higher-precedence stratum, say — is an ordinary concurrent structural
change, visible to the next resolution, and is neither an error nor
detected.

This is not a rare case. Every descriptor other than the one that
caused a copy-up still refers to the original object, so a second
descriptor opened before the copy-up meets this rule the first time it
writes. A caller therefore has to be prepared for a write to fail
`ESTALE` on a descriptor that was valid when it was opened and has done
nothing wrong. [Failure Modes](</kernel/internals/stratafs/start-here/failure-modes.md#trm-4-8>) records it.

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

## Staging and atomicity <a id="cite-copy-up.never-partially-observable"></a>

A copy-up is never observable in a partial state. All content,
attribute and metadata work happens on an object that is not reachable
through the mount, and publication is a single step.

Two arrangements are used, chosen by type:

- A **regular file** is staged as an anonymous object on the create
  stratum's filesystem — a kernel tmpfile — and linked into place when
  complete. No reader can reach it.
- A **directory or symlink** is staged under a name in the create
  stratum, since neither has an anonymous form, and published by a
  no-replace rename.

A staged name is `.stratafs-stage-` followed by the mount cookie and a
per-stage identifier, in a 64-byte buffer, with up to eight retries on
collision. It is excluded from resolution and from enumeration for the
mount that owns it, and removed if the copy-up fails. The exclusion is
local: a second stratafs mount sharing the create stratum, and every
direct reader of that directory, sees an ordinary entry containing a
partial copy.

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

### Identifying staging entries <a id="cite-copy-up.orphan-recovery-checks-marker"></a>

A staged name alone is not proof of ownership, so each staged object
also carries a marker in the extended attribute
`security.peios.stratafs_staging`. The marker is a 24-byte
little-endian structure: a magic of `0x53544731` — ASCII `STG1` — a
version, its own size, a per-boot cookie, and the cookie of the mount
that created it.

Recovery of an orphan is therefore precise. A staged entry whose marker
is valid and whose owning mount is not live is removed; one belonging to
a live mount is left alone, and so is one whose marker is missing,
short, or carries the wrong magic, version or size. That matters
because two mounts may share a create stratum, and an unqualified
cleanup would have each new mount destroy the other's copy-up in
flight.

The scan runs in batches of 128 names, with resume, and is triggered
from five places: mounting, opening a directory, looking up a name
beginning with the staging prefix, copying up into a directory, and the
emptiness test of [Removal](</kernel/internals/stratafs/mutation/removal.md#trm-4-5-4>) before it scans the create stratum's participating
directory, after the caller's traverse/list access has been checked.

The fifth exists because the other four only reach directories somebody
visits. An orphan in a directory that is never opened, looked up, or
copied into would otherwise block `rmdir`: a dead mount's stages are not
in the current mount's in-flight list and would be counted as ordinary
entries. Recovery before that scan reaches the affected directory
without recursively visiting every directory at mount time. It also
means the create directory is scanned for recovery when its merged
emptiness is tested, even if it ultimately proves nonempty.

The marker is removed after publication. A failure to remove it is only
warned about, leaving the marker on the published copy until a later
lookup cleans it.

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

## Failed copy-up and rollback <a id="cite-copy-up.failure-leaves-no-entry"></a>

Where a copy-up fails for any reason, the create stratum is left with
no new entry at the target path and the operation fails. Nothing falls
back to a weaker replication: an object whose descriptor or extended
attributes could not be preserved is never published. A published copy
whose descriptor handoff then fails is rolled back.

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

## Concurrent modification <a id="cite-copy-up.no-source-snapshot"></a>

The provider may be modified while it is being copied, by a writer that
does not know stratafs exists.

Copy-up reads the provider as any reader would, with no locking of the
source and no snapshot, and offers no stronger consistency than an
ordinary read of that object. Where the provider is modified during the
copy, the copy may contain bytes from more than one state of the
source, exactly as a concurrent `read` of the same object may.

Nothing detects that and restarts — there is no retry loop, no
generation check. Equally, nothing blocks waiting for a quiescent
source, and nothing fails a copy-up merely because the source is being
written.

What *is* guaranteed is that the published object never appears in a
partial state, because staging and publication are properties of the
create stratum's filesystem, which stratafs does control.

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

### Independence after publication <a id="cite-copy-up.published-copy-independent"></a>

Once published, the copy is independent of its source. Subsequent
modifications to the object it was copied from are not reflected in it,
and are not visible through the mount for as long as the copy provides
the name.

---

# Creation

_Peios Kernel / Internals / StrataFS / Mutation_

> Creation: behaviour and implementation of the Peios kernel.

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

Creating a name held by no participating stratum places it in the
create stratum. <a id="cite-create.lands-in-create-stratum"></a>

1. If there is no create stratum, or it is absent, the operation fails
   with `EROFS`. <a id="cite-create.erofs-without-create-stratum"></a>
2. Any directories of the create stratum containing the name's path
   that do not exist are materialised as [Copy-Up](</kernel/internals/stratafs/mutation/copy-up.md#trm-4-5-2>) requires for copy-up
   parents. Where the create stratum holds one of those components as
   something other than a directory, the operation fails with
   `ENOTDIR`; the blocking entry is not removed or replaced. <a id="cite-create.parent-conflict-enotdir"></a>
3. The name is created there.

All six kinds of creation — regular file, directory, symbolic link,
device node, FIFO, socket — go through one helper and one path.

The `ENOTDIR` case arises because creation routes positionally, into
the create stratum's subdirectory at the same path whether or not it
exists ([Directory Merge](</kernel/internals/stratafs/name-resolution/directory-merge.md#trm-4-3-2>)). Where the create stratum holds that path as a file,
and a higher-precedence stratum provides it as a directory, the merged
directory exists and is reachable while its create-stratum counterpart
is blocked.

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

## The security descriptor <a id="cite-create.descriptor-inherited-from-parent"></a>

A created object has no provider to inherit from, so its descriptor is
established by the ordinary creation semantics of the create stratum's
filesystem — by inheritance from the directory it is created in,
exactly as if it had been created there directly. The create is an
ordinary `vfs_create`, `vfs_mkdir`, `vfs_symlink` or `vfs_mknod`
against the real create-stratum parent, with the KACS creation decision
bound to that same parent.

This is cleanly separated from copy-up inside KACS: the copy-up branch
of the inode security initialisation is taken first and short-circuits
the inheritance builder entirely. Ordinary creation in a create stratum
inherits; copy-up preserves.

Where the creating interface lets the caller supply a descriptor — the
native open path does — it is honoured rather than replaced by an
inherited one. That is entirely KACS's doing; stratafs has no
descriptor parameter and cannot express it. All stratafs contributes is
re-anchoring the pending native create request onto the create-stratum
parent.

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

## Dispositions that delete <a id="cite-create.supersede-is-removal-then-creation"></a>

A creating interface may offer a disposition that replaces an existing
object rather than opening it. In Peios that is the supersede
disposition of the native open path: KACS creates a temporary file
through the mount, opens it, and renames it onto the target, at which
point stratafs diverts the rename into a dedicated supersede path.

Such a disposition requires both removal and creation authority. This
does not prescribe their publication order. Before anything is created,
the removal is validated: the
target's provider must accept modification, or the operation fails with
`EROFS`. Before anything is removed, the replacement is checked for
reachability: if any stratum strictly above the create stratum, other
than the provider being removed, holds the name, the operation fails
with `EROFS`.

Without that guard the disposition could report success while leaving
the caller's new object invisible. The removal takes the name out of
the stratum that provided it; the creation puts the replacement in the
create stratum; and if some stratum between the two also holds the
name, it now outranks the replacement.

The creation half is treated as a creation throughout. It is never
reclassified as a modification of a newly-surfaced provider and never
copies one up: the caller asked for a new object, and its descriptor is
derived as above.

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

### Implementation restrictions

Three restrictions are implementation choices rather than consequences
of the model:

1. Supersede applies to regular files only, and anything else is refused
   with `EOPNOTSUPP`. <a id="cite-create.supersede-regular-files-only"></a>
2. The source must be in the create stratum, and source and destination
   must share a parent. <a id="cite-create.supersede-source-and-parent-restrictions"></a>
3. Across distinct strata the two halves are **not** atomic. The staged
   file is renamed into place in the create stratum **before** the old
   provider is unlinked. Between those operations both backing names
   exist: ordinary precedence selects the replacement if the create
   stratum is higher, or retains the original until its removal if it is
   higher. A failure to publish leaves the original untouched, rather
   than exposing a still-lower provider. <a id="cite-create.supersede-is-not-atomic"></a>

   Write access to the original provider is probed before publication,
   then released while the replacement is published. It is reacquired
   for removal; a provider remounted read-only in between therefore fails
   with `EROFS` after publication. The two backing mounts never have
   simultaneous freeze-protection holds, even when they share a superblock.

   A later removal failure is returned to the caller and recorded as
   `supersede-removal`. Native-open cleanup then attempts to remove the
   newly published object, following its final name rather than its old
   temporary name. If the original still has its name, successful
   cleanup restores its visibility; concurrent provider changes are
   preserved. Cleanup is itself a filesystem operation and can fail;
   in that case both entries can remain, with ordinary precedence
   deciding which is visible. A crash between publication and removal
   can likewise leave both. This ordering narrows the destructive
   failure window; it does not promise a transaction or crash recovery.

   When both objects are in the create stratum, replacement uses one
   backing-filesystem rename and needs no separate provider unlink.

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

## Exclusive creation <a id="cite-create.exclusive-eexist-from-any-stratum"></a>

Where creation is requested exclusively, the name must not exist in
**any** participating stratum, and a name provided by a lower stratum
causes `EEXIST` even though the create stratum does not hold it.

Nothing in stratafs implements this, and nothing needs to. The merged
lookup instantiates a positive dentry whenever any stratum holds the
name, and the VFS refuses `O_EXCL` on a dentry it did not create. The
`excl` argument stratafs receives is ignored. There is a race backstop
in the create path — a positive dentry appearing in the create stratum
yields `EEXIST` — but it sees only that one stratum.

Exclusive creation asks whether the name is free, and through this
mount it is not: a caller that created it anyway would find their
object shadowing a file they did not know was there, or masking a whole
subtree.

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

## Non-exclusive creation over a shadowed name <a id="cite-create.shadowed-non-exclusive-is-open"></a>

Where creation is not exclusive and a lower stratum provides the name,
the merged dentry is positive, so the VFS never calls the create path
at all. The operation is an open, and [Write Routing](</kernel/internals/stratafs/mutation/write-routing.md#trm-4-5-1>) routes it.

Where the provider is a regular file that does not accept modification
and the create stratum has higher precedence, the result is a copy-up
followed by the requested modification, including truncation where
`O_TRUNC` was requested. `O_TRUNC` is stripped from the backing open on
a non-in-place route precisely so that the copy-up source is not
destroyed before it is read.

Where the provider is a FIFO, socket, or device node, no copy-up occurs
and the open is forwarded to the provider. Such an open succeeds
whether or not the provider accepts modification: opening a special
file does not modify it.

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

## Unnamed files <a id="cite-create.unnamed-file-in-create-stratum"></a>

A file may be created without a name, for later linking into place. In
a merged directory this is supported, and the file is created on the
create stratum's filesystem, recorded with the create stratum's index
and marked unnamed.

Where the mount has no create stratum, or it is absent, the operation
fails with `EROFS`. Parent materialisation applies as for a named
creation: the create stratum's counterpart of the directory the
operation named is materialised, and the new file's descriptor is
derived by inheritance from it, because that directory is what the
descriptor must come from. Linking such a file into the mount is
governed by [Links](</kernel/internals/stratafs/mutation/links.md#trm-4-5-6>).

---

# Removal

_Peios Kernel / Internals / StrataFS / Mutation_

> stratafs has no whiteouts, so removal can only remove an entry that is really there — and what removal therefore never does.

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

stratafs has no whiteouts and cannot record that a name should be
absent. Removal can therefore only remove an entry that is actually
there, in a stratum it may write to.

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

## Unlinking <a id="cite-remove.unlink-in-provider"></a>

To remove a non-directory name from a merged directory: if the provider
accepts modification ([Write Routing](</kernel/internals/stratafs/mutation/write-routing.md#trm-4-5-1>)), the entry is removed from the provider;
otherwise the operation fails with `EROFS`. The parent is resolved in
the provider's stratum, and the unlink is performed there.

The provider need not be the create stratum. Any stratum that accepts
modification may have an entry removed from it, by the same rule that
allows an object it provides to be modified in place.

Where a lower-precedence stratum also holds the name, that entry
becomes the provider once the higher one is removed, and the name
remains visible, now resolving to the lower stratum's object. That is
not an error and is not reported as one: the removal succeeded, and the
entry it removed is gone. The dentry is dropped on success, so the next
lookup finds the lower entry.

Removing an object that a lower stratum also provides is how a
modification is undone. Where a file was copied up in order to be
edited, removing it through the mount discards the edit and restores
the original, which remained untouched in its own stratum throughout.
Callers expecting POSIX removal will find the name still present
afterwards; [Failure Modes](</kernel/internals/stratafs/start-here/failure-modes.md#trm-4-8>) records this as an intended divergence.

Refusal because the provider does not accept modification is `EROFS`,
with one exception: where the provider's inode is immutable it is
`EPERM`. The outer inode carries the provider's inode flags, so the VFS
refuses the removal before stratafs's own `EROFS` test is reached — and
`EPERM` is what every filesystem returns for an immutable file, so the
distinction is the useful one rather than an accident worth papering
over. The `ro` flag and a read-only provider mount both still produce
`EROFS`.

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

## Removing directories <a id="cite-remove.rmdir-merged-emptiness"></a>

The same rule applies, with one additional condition: the **merged**
directory must be empty. A directory is empty for this purpose only if
no participating stratum holds any entry within it, so a directory that
is empty in the provider but not in another participant fails with
`ENOTEMPTY`.

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

### Access checks precede the emptiness scan <a id="cite-remove.access-before-emptiness"></a>

Because determining this reads the contents of every participating
stratum, the caller must hold traverse and list rights on each of them.
That check completes over all participants before any of them is
enumerated, so a refusal yields `EACCES` without disclosing whether the
directory was empty. Without that ordering the emptiness test would be
a disclosure channel: a caller who may not enumerate a protected
participant could learn whether it contains anything by attempting
`rmdir` and distinguishing `ENOTEMPTY` from success.

The emptiness scan filters this mount's in-flight staging entries, as
enumeration does. That does not authorise removing a live stage: the
provider can still refuse the physical removal with `ENOTEMPTY`.

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

### Orphan recovery before inspection <a id="cite-remove.rmdir-staging-recovery"></a>

After the traverse/list checks complete, the emptiness test runs orphan
recovery on the create stratum's participating directory **before**
enumerating its contents ([Copy-Up](</kernel/internals/stratafs/mutation/copy-up.md#trm-4-5-2>)). A dead mount's entries are not in the
current mount's in-flight list, so waiting until that list has filtered
them would reject the directory before recovery could run. A directory
containing only recoverable orphan files or empty orphan directories can
therefore be removed without first opening or listing it.

Recovery requires the staging prefix, a valid marker, and an owner that
is no longer live. Missing or malformed markers, ordinary published
names, and stages belonging to a live mount remain untouched; they can
still make removal fail with `ENOTEMPTY`. The scan is local to the
participating create directory, not a recursive walk of the stratum.

Where the directory is removed and lower strata hold the same name as a
directory, the name remains visible as a merged directory of the
remaining strata — which, by the emptiness condition, is empty.

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

## What removal never does <a id="cite-remove.touches-only-the-provider"></a>

Removal touches exactly one stratum, the provider's. The other strata
are opened read-only for the emptiness scan and nothing else. No entry,
marker, or object is created in any stratum to suppress a lower entry;
no whiteout machinery exists anywhere in the filesystem.

Success is never reported for a name whose provider entry was not
removed. The one place zero is returned without a removal is the
deferred path of [Creation](</kernel/internals/stratafs/mutation/creation.md#trm-4-5-3>), where the entry the descriptor named is
already gone and the deletion is genuinely complete.

---

# Rename

_Peios Kernel / Internals / StrataFS / Mutation_

> Rename both removes and creates, so the source is bound by the removal constraint — atomic replacement, flags and RENAME_NOREPLACE ordering.

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

Rename both removes a name and creates one. Because stratafs cannot
make a name disappear ([Removal](</kernel/internals/stratafs/mutation/removal.md#trm-4-5-4>)), the constraint on the source is the
same as for removal, and the constraint on the destination follows from
the read-after-write direction of [Write Routing](</kernel/internals/stratafs/mutation/write-routing.md#trm-4-5-1>).

For a rename of a source to a destination within one mount, letting `P`
be the source's provider:

1. `P` must accept modification. Otherwise `EROFS` — the source cannot
   be removed, so the rename would leave it still visible and amount to
   a copy. <a id="cite-rename.source-provider-must-be-writable"></a>
2. The destination must not be provided by a stratum of **higher**
   precedence than `P`. Otherwise `EROFS` — the renamed object would be
   shadowed at its destination and unreadable through the path it was
   renamed to. <a id="cite-rename.destination-not-higher-precedence"></a>
3. `P` must hold the directory containing the destination. Otherwise
   `EXDEV`. That directory is not created to satisfy the condition,
   whether or not `P` is the create stratum: parent materialisation
   exists to receive a copy-up, and a rename is not one. <a id="cite-rename.destination-parent-in-provider"></a>
4. Where the source is a directory, the merged directory must contain
   no entries provided by a stratum other than `P`. Otherwise `EXDEV`,
   since those entries cannot move with it. Determining this reads
   every participating stratum, so traverse and list rights are
   required on each, checked before any enumeration begins; where that
   is refused the rename fails with `EACCES` without disclosing whether
   other strata contributed. <a id="cite-rename.directory-source-must-not-strand-entries"></a>
5. Where the destination is provided, its type must match the source's.
   A non-directory onto a directory provider fails with `EISDIR`; a
   directory onto anything else fails with `ENOTDIR`. Only the
   provider's type is compared, since a lower stratum's entry of a
   different type is masked and contributes no inode. <a id="cite-rename.destination-type-must-match"></a>
6. Where the destination's provider is a directory, that merged
   directory must contain no entries at all — the same merged-emptiness
   test `rmdir` uses, with the same access requirement. Otherwise
   `ENOTEMPTY`. <a id="cite-rename.destination-directory-must-be-empty"></a>
7. The rename is performed within `P`: both parents are resolved at the
   provider's index, and a single `vfs_rename` is issued there. <a id="cite-rename.performed-within-the-provider"></a>

Where the destination is provided by a stratum of lower precedence than
`P` and both are non-directories, the rename succeeds and the renamed
object shadows it. Where the destination is held by `P` itself, it is
replaced, as on any filesystem. <a id="cite-rename.lower-destination-is-shadowed"></a>

Where the renamed object and the destination are both directories, the
result is not a shadowing: by [Directory Merge](</kernel/internals/stratafs/name-resolution/directory-merge.md#trm-4-3-2>) the renamed directory merges with
the lower strata's directories of that name. <a id="cite-rename.directory-destination-merges"></a> Condition 6 is what keeps
that tolerable — those directories are empty, so the merged result is
the renamed directory's own contents.

`P` need not be the create stratum. A rename is performed in whichever
stratum provides the source, provided that stratum accepts
modification. A rename whose source and destination are in different
mounts fails with `EXDEV`, refused by the VFS before stratafs is
consulted, and stratafs additionally refuses one spanning two mounts
inside the provider stratum. <a id="cite-rename.cross-mount-exdev"></a>

The immutable-provider caveat of [Removal](</kernel/internals/stratafs/mutation/removal.md#trm-4-5-4>) applies to condition 1 as well:
an immutable source yields `EPERM` from the VFS rather than `EROFS`. <a id="cite-rename.immutable-source-eperm"></a>

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

## Replacing atomically <a id="cite-rename.replace-is-atomic"></a>

A rename onto an existing destination is the operation by which most
software replaces a file safely, and it works through a stratafs mount
for a destination provided by a lower stratum. That case is permitted
by condition 2: the destination is shadowed by the renamed object
rather than removed, so no whiteout is required, and the replacement is
a single `vfs_rename` within one directory pair of one stratum — atomic
on the filesystem holding `P`.

Where the source of such a rename is a temporary file the caller
created in the same directory, [Creation](</kernel/internals/stratafs/mutation/creation.md#trm-4-5-3>) placed it in the create stratum,
which never carries `ro`. Conditions 1 and 3 are then satisfied.

The contrast with the supersede disposition ([Creation](</kernel/internals/stratafs/mutation/creation.md#trm-4-5-3>)) is worth noticing:
that spans two strata and is not atomic.

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

## Flags

Conditions 1 and 4 concern whether the source can be moved at all, and
apply under every flag. Conditions 2, 3, 5 and 6 concern the
destination.

| Flag | Behaviour |
|---|---|
| `RENAME_NOREPLACE` | The destination must not be held by **any** participating stratum, not merely by `P`. Conditions 2, 5 and 6 are not evaluated, since each presupposes a destination that exists; where any stratum holds the destination the rename fails with `EEXIST`. <a id="cite-rename.noreplace-checks-every-stratum"></a> |
| `RENAME_EXCHANGE` | Both names must be provided by the **same** stratum, and that stratum must accept modification; otherwise `EROFS`. Conditions 1 and 4 apply to both names. Conditions 2, 5 and 6 are not evaluated: an exchange swaps two names that both already exist, so neither type matching nor emptiness is required of either, exactly as on any filesystem. Condition 3 is subsumed. Where either name is a directory, no stratum other than the providing one may hold entries under either name; otherwise `EXDEV`. <a id="cite-rename.exchange-needs-one-stratum"></a> |
| `RENAME_WHITEOUT` | Fails with `EINVAL`, checked before everything else. stratafs has no whiteouts and cannot represent one. <a id="cite-rename.whiteout-flag-einval"></a> |

Exempting `RENAME_EXCHANGE` from conditions 5 and 6 is what leaves the
flag usable. Its ordinary purpose is to swap two populated directories
atomically, which condition 6 would refuse outright and condition 5
would refuse whenever the two differ in type. The stranded-entries
condition is the only destination constraint an exchange genuinely
needs, because it is the only one that arises from the names spanning
strata rather than from what the names hold.

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

### The `RENAME_NOREPLACE` ordering <a id="cite-rename.noreplace-eexist-takes-precedence"></a>

A `RENAME_NOREPLACE` whose destination is held by any participating
stratum fails with `EEXIST`, and that `EEXIST` takes precedence over
conditions 1, 3 and 4. It is not stratafs that decides this. For
`RENAME_NOREPLACE` the VFS looks the destination up with `LOOKUP_EXCL`
and returns `EEXIST` for a positive dentry before `vfs_rename` runs at
all, and stratafs's merged lookup makes the dentry positive whenever any
stratum holds the name. `namei.c` is unpatched, so there is no point
inside `->rename` from which the order could be changed.

stratafs nonetheless evaluates conditions 1, 3 and 4 first, and the code
says so. That ordering is reachable only as a race backstop — where the
destination appeared between the VFS lookup and the rename — so a caller
should not expect `EROFS`, `EXDEV` or `EACCES` from a `RENAME_NOREPLACE`
whose destination already existed.

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

## Other behaviour

Unknown `flags` bits are neither rejected nor masked; they pass through
to the VFS. <a id="cite-rename.unknown-flags-pass-through"></a> Where the two provider-level names resolve to one inode the
rename is a no-op returning success. <a id="cite-rename.same-inode-is-a-noop"></a> A dentry whose private state is
missing, or whose provider identity no longer matches, yields `ESTALE`. <a id="cite-rename.stale-dentry-estale"></a>
That refusal is per attempt, and the VFS heals it: `ESTALE` from a
rename makes the syscall retry once with a fresh walk, whose rebuilt
dentry carries the current provider identity, so the caller usually
observes the retry's outcome — a rename of the object now at the name —
rather than the errno. The refusal itself is recorded at the
`stratafs:stratafs_rename_stale` tracepoint, with the walk-time and
found inode numbers.
Because the filesystem sets `FS_RENAME_DOES_D_MOVE`, stratafs performs
the `d_move` or `d_exchange` itself, together with swapping the
dentries' recorded relative paths.

---

# Links

_Peios Kernel / Internals / StrataFS / Mutation_

> Hard links within one mount, linking an unnamed file, and how symbolic links are created.

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

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

## Hard links

To create a hard link from an existing name to a new name within one
mount, letting `P` be the source's provider:

1. `P` must accept modification. Otherwise `EXDEV`. <a id="cite-link.source-provider-must-be-writable"></a>
2. The destination must not be held by any participating stratum.
   Otherwise `EEXIST`. <a id="cite-link.destination-must-not-be-held"></a>
3. `P` must hold the directory containing the destination. Otherwise
   `EXDEV`. That directory is not created to satisfy the condition,
   whether or not `P` is the create stratum. <a id="cite-link.destination-parent-in-provider"></a>
4. The link is created in `P`. <a id="cite-link.created-in-the-provider"></a>

`P` need not be the create stratum: a link is made in whichever stratum
provides the source, since that is the only stratum in which the two
names can share an object. Condition 2 ensures the new link is not
shadowed at its destination, since no stratum — above `P` or below it —
holds that name.

Condition 2 is enforced by the VFS rather than by stratafs for a named
source: the link path looks the destination up with `LOOKUP_EXCL`, and
stratafs's merged lookup makes the dentry positive whenever any stratum
holds the name. stratafs's own test covers only the provider stratum
and is a race backstop. For an unnamed source it does check every
stratum explicitly.

A link request is never satisfied by copying the source up. <a id="cite-link.never-copies-up"></a> The result
would be a link to the copy rather than to the object named by the
source, so the two names would not share an object — which is the whole
of what a hard link is for. Refusing is the only correct answer.

`EXDEV` is used rather than `EROFS` because it is the error callers
already handle when a link cannot be made between two locations, and
because it is accurate: the link would have to span two strata, which
through this mount are two filesystems.

Where installing the outer inode fails after the lower link succeeded,
the link is rolled back; a failed rollback is audited. <a id="cite-link.rolled-back-on-install-failure"></a>

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

## Linking an unnamed file <a id="cite-link.unnamed-goes-to-create-stratum"></a>

An unnamed file created under [Creation](</kernel/internals/stratafs/mutation/creation.md#trm-4-5-3>) is linked into the mount by the
rules for creation, not by those above: it has no provider, so
conditions 1 and 3 have no subject.

The link is created in the create stratum — a source recorded with any
other index is refused with `EXDEV` — and the operation follows [Creation](</kernel/internals/stratafs/mutation/creation.md#trm-4-5-3>):
the name must not be held by any participating stratum, parent
directories are materialised in the create stratum, and a create
stratum that is absent or does not exist fails with `EROFS`.

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

### Unnamed files cannot cross mounts <a id="cite-link.unnamed-cross-mount-exdev"></a>

Linking an unnamed file into a different mount fails with `EXDEV`,
checked both by superblock and by mount, and by the VFS as well.

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

## Symbolic links <a id="cite-link.symlink-target-verbatim"></a>

Creating a symbolic link is an ordinary creation and follows [Creation](</kernel/internals/stratafs/mutation/creation.md#trm-4-5-3>):
the link is created in the create stratum, with a security descriptor
established by inheritance there.

A symbolic link's target is stored and returned verbatim. The target
string is passed through untouched on creation, and on read the outer
inode's `get_link` calls the provider inode's own and returns its
result unmodified. No rewriting exists anywhere: a target naming a path
inside a stratum is not rewritten to name the corresponding path inside
the mount, nor the reverse.

Resolution of a symbolic link found in a stratum follows [Lookup](</kernel/internals/stratafs/name-resolution/lookup-and-type-conflicts.md#trm-4-3-1>). The
raw string goes back to the VFS, which interprets it as any filesystem's
target is interpreted, so an absolute target resolves from the process's
root and may re-enter this mount, a different stratafs mount, or none.
A link created directly in a stratum by that stratum's owner is
followed as written; the rule constrains only what stratafs itself
does, which is nothing.

---

# Locking

_Peios Kernel / Internals / StrataFS / Mutation_

> Advisory locks across a stack of independently writable directories — the rule, a changing provider, the retired provider, and leases.

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

Advisory file locks — whole-file and record locks alike — exist so that
several writers of one file can coordinate. A stratafs mount is
established over directories that have their own writers, so a lock
taken through the mount and a lock taken directly on the same object
must be the same lock.

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

## The rule <a id="cite-lock.taken-on-the-provider-object"></a>

A lock taken through a stratafs mount is held on the provider's object,
in the same lock space as a lock taken on that object by any other
path. Both the POSIX and the `flock` paths retarget the request onto
the descriptor's provider file, so the lock lands on the provider
inode's own lock context.

stratafs maintains no lock space of its own. There is no lock list, no
fallback onto the outer inode, and no per-inode lock state. Two callers
that lock the same provider object — one through the mount, one through
the stratum directly, or two through different stratafs mounts sharing
that stratum — contend with each other. Open-file-description locks are
re-owned onto the provider file so that their per-description semantics
are preserved.

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

### Locking does not copy up <a id="cite-lock.no-copy-up-and-facs-rights-still-apply"></a>

Taking a lock does not modify an object, so it does not route ([Write Routing](</kernel/internals/stratafs/mutation/write-routing.md#trm-4-5-1>))
and cannot itself cause a copy-up. FACS still checks the descriptor's
granted rights ([Use-Time Semantics](</kernel/internals/kacs/file-security/use-time-semantics.md#trm-3-9-4>)): a shared lock requires `FILE_READ_DATA`, and an
exclusive lock requires `FILE_WRITE_DATA` or `FILE_APPEND_DATA`. A
read-only descriptor can therefore carry a shared lock, but not an
exclusive lock without a write grant.

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

## Locks and a changing provider <a id="cite-lock.not-transferred-on-provider-change"></a>

A lock is held on the object a descriptor resolved to. That object may
cease to be the provider afterwards — because a higher-precedence
stratum gains the name, because the object is removed from its stratum,
or because a copy-up produced a new object.

In every such case the lock remains held on the object it was taken on.
It is not transferred, and it does not begin to guard the new provider.

**Two callers may therefore hold exclusive locks on one merged path
without contending**, whenever they opened it either side of a change of
provider. Neither is wrong about the object it locked; they locked
different objects, and each lock is honoured by everything else holding
that object. The merged path is what stopped naming one thing. [Failure Modes](</kernel/internals/stratafs/start-here/failure-modes.md#trm-4-8>)
records it.

A caller that must be sure it holds a lock on the current provider has
to reopen and re-take it, which is the same discipline required of
anything that locks a path another process may replace by rename. The
exposure here is that a copy-up is a replacement the caller did not
perform and cannot see.

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

## The retired provider

Copy-up does not close the file it copied from. The pre-copy-up
provider file is moved aside into the descriptor's private state
specifically to keep locks and leases taken before the copy-up alive,
and the fan-out that follows is invisible to the specification but
visible in behaviour:

- A POSIX or `flock` **unlock** is applied to both the retired file and
  the copy; a non-unlock request goes only to the copy. <a id="cite-lock.unlock-reaches-the-retired-file"></a>
- A lease release is applied to both. A lease *acquisition* is
  redirected to the retired file when that file already holds a lease
  of the same flavour. <a id="cite-lock.lease-acquisition-redirected"></a>
- Querying a lease returns the stronger of the two files' lease types,
  ordering write above read above none. <a id="cite-lock.lease-query-returns-the-stronger"></a>
- Closing the descriptor runs the underlying flush and removes POSIX
  locks on both files, and releasing it breaks leases on both, using
  the outer file as the owner identity. <a id="cite-lock.close-clears-both-files"></a>

All of it is serialised by the descriptor's mutation lock.

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

## Leases and mandatory locking <a id="cite-lock.leases-are-the-providers"></a>

Leases are established on the provider's object, in that object's own
lock space, and every result is the provider's own, returned unchanged.
stratafs neither adds to nor removes from whatever semantics the
provider's filesystem gives them, and never reports a lock as
established where the provider's filesystem refused it.

Mandatory locking has no subject here: the platform does not offer it,
and stratafs contains nothing that would obstruct it. The outer inode
copies the provider's inode flags wholesale.

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

## Internal locks

The specification names no internal lock and fixes no acquisition
order. The implementation's hierarchy, outermost first:

1. The per-open-file **mutation lock**, taken by every operation that
   may copy up.
2. The per-outer-inode **rebind lock**, taken inside it whenever an
   inode's provider is swapped.
3. The dentry lock or the inode lock, taken inside the rebind lock and
   never overlapping each other.

The superblock's identity lock and staging lock are each taken alone.
`copy_file_range` and `remap_file_range` deliberately do not nest the
two files' mutation locks: the input file's is taken, a reference
grabbed, and released before the output file's is taken.

StrataFS assigns inode and per-file lockdep classes during initialization,
before publication. The inode's `i_rwsem` and the file's mutation lock use
classes based on the mount's admitted stacking depth. Directory and
non-directory inodes use separate classes.

Dynamic path resolution can reach a different depth, so these classes do
not guarantee descending acquisition order. Lockdep still records
dependencies and detects reverse-order cycles across depths.

Reads and writes select and retain a reference to their backing file
under the mutation lock. Writes finish routing, any copy-up, and append
flag synchronization before releasing it. The data transfer runs without
the mutation lock because faults on user buffers can acquire the memory
mapping lock. An operation finishes against its selected provider even if a
concurrent operation copies up the descriptor. Writes that complete
successfully before forwarding returns refresh the outer inode from its
current provider under the mutation lock.

Shared write-capable mapping preparation follows the backing chain through
StrataFS and FUSE passthrough. It takes each StrataFS layer's mutation lock
before the VFS acquires `mmap_lock`, then releases it before preparing the
next layer. Each forwarding step uses the credentials that its backing
mapping would use.

StrataFS does not take the mutation lock, resolve paths, perform copy-up,
or acquire provider freeze protection in its mapping callback.

A separate leaf spinlock protects the backing file and matching provider
index. Copy-up publishes this pair while holding the mutation lock. The
mapping callback takes a reference to the selected file under the leaf
lock, then releases the lock before checking the route, auditing a refusal,
or forwarding to the backing filesystem. The reference preserves identity
and lifetime, not the provider's read-only or immutable state. A changed
route can require the caller to retry the whole mapping operation.

For mutations the ordering is: the KACS decision, then parent
materialisation, then write access on the target mount, then the
parent's directory lock through the VFS's create, remove or rename
helpers. Refusal auditing takes the dentry lock with an atomic
allocation first and falls back to the rebind lock only if that fails,
so the two are never nested.

Backing-filesystem freeze protection is held for one mutation stage at a
time. Regular-file copy-up releases its creation-stage mount write hold
while copying contents: reads and writes keep their ordinary permission
and pre-content notification checks, and each write acquires its own
freeze protection. It reacquires the mount write hold before copying
metadata or publishing the anonymous stage; failure to reacquire aborts
the copy without publishing it. This permits a pending filesystem freeze
to complete between stages without a writer waiting on itself.
<a id="cite-lock.copy-up-content-without-outer-writer"></a>

Supersede releases the create mount's write hold before acquiring
the original provider's hold for removal. Rename's merged-destination
emptiness scan, which can recover orphan staging objects, runs without a
backing write hold retained by rename; recovery acquires its own. An early
write-access probe preserves the read-only refusal, and the mutation
reacquires write access before taking its backing directory locks.

---

# Durability and Accounting

_Peios Kernel / Internals / StrataFS / Mutation_

> stratafs holds no storage, so durability and accounting belong to the providers — except where a merged directory needs a rule of its own.

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

stratafs holds no storage, so durability is the providers' and the
accounting is theirs too. Two operations nonetheless need a rule,
because a merged directory has no single provider to forward to.

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

## Synchronising an object <a id="cite-durability.fsync-follows-the-descriptor"></a>

Synchronising a non-directory is forwarded to the object the descriptor
resolved to — the descriptor's own provider file — and reports what
that object's filesystem reports. No path re-resolution happens, so it
is never forwarded to whichever object currently provides the path.
Where the two differ, the data the caller wrote is on the object it
opened, and synchronising anything else would report success while
leaving that data unsynchronised.

Where a copy-up has occurred through this descriptor, the descriptor's
object is the copy, and it is the copy that is synchronised. The
retired pre-copy-up file ([Locking](</kernel/internals/stratafs/mutation/locking.md#trm-4-5-7>)) is not.

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

## Synchronising a merged directory <a id="cite-durability.dirsync-covers-every-stratum-at-call-time"></a>

Synchronising a merged directory synchronises the corresponding
directory in **every** stratum of the mount that holds it at the time
of the call, and fails if any of them fails. The loop continues past a
failure, so every stratum is still attempted, and the first error is
what is returned.

The set is evaluated when the operation runs, not when the descriptor
was opened: the directory is re-resolved across all strata rather than
read from the participant set settled at open ([Enumeration](</kernel/internals/stratafs/name-resolution/enumeration.md#trm-4-3-4>)). Both halves of
that matter:

- Evaluating at call time catches a directory the create stratum did
  not hold when the descriptor was opened and does now — which is
  exactly what happens when a file is created through that descriptor
  and [Creation](</kernel/internals/stratafs/mutation/creation.md#trm-4-5-3>) materialises its parent.
- Covering every stratum rather than the provider alone is required by
  the atomic-replace pattern of [Rename](</kernel/internals/stratafs/mutation/rename.md#trm-4-5-5>), whose rename is performed in
  the stratum that provided the *source*, which need not be the stratum
  providing the merged directory.

Durability is a question about what is on disk now, which is why this
is the one place a merged directory is treated as its current set of
real directories rather than as the thing a descriptor was opened
against.

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

## Freezing <a id="cite-durability.freeze-eopnotsupp"></a>

A stratafs mount has no storage to quiesce. Freezing returns
`EOPNOTSUPP` and propagates nothing to any stratum's filesystem; no
unfreeze, freeze-super or thaw-super operation is registered at all.
Freezing the filesystem a stratum lives on is done through that
filesystem, and affects the merged view as it affects any other reader
of that stratum.

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

## Storage accounting

Storage consumed by an object created through the mount, or copied up
into the create stratum, is consumed on the create stratum's filesystem
and accounted there. <a id="cite-durability.storage-charged-to-create-stratum"></a>

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

### The principal charged by quota <a id="cite-durability.copy-up-accounted-to-preserved-owner"></a>

Disk quota keys on the POSIX owner, and copy-up preserves it ([Copy-Up](</kernel/internals/stratafs/mutation/copy-up.md#trm-4-5-2>)):
the staged object is created with the calling task's credentials, and
the ownership is then set to the source's under the mount's own
credential. <a id="cite-durability.copy-up-preserves-posix-owner"></a> A copy is therefore accounted to the owner of the object
it was copied from, not to the caller who caused it.

The KACS security descriptor, including its owner SID ([Descriptors on Copy-Up](</kernel/internals/stratafs/security/copy-up-descriptors.md#trm-4-6-3>)), is
preserved alongside it. <a id="cite-durability.copy-up-preserves-owner-sid"></a> The two notions of owner agree, and both
name the object rather than whoever provoked the copy — which is what
[Checks on Merged Directories](</kernel/internals/stratafs/security/checks-on-merged-directories.md#trm-4-6-2>) relies on when it permits a caller to cause a copy into a
directory they hold no rights over.

Ownership is what accounting follows; the caller who caused the copy is
recorded in the audit record of [Audit](</kernel/internals/stratafs/security/audit.md#trm-4-6-5>) instead.
