# 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.
