# 5.7 Locks and Leases

_Peios / Developing for Peios / SDK Reference / file.h — File Security_

> NT byte-range locks enforced for every process, and leases that let a program cache a file until someone else needs it.

## 5.7.1 Byte-range locks

```c
int peios_file_lock(int fd, uint32_t op, uint64_t offset, uint64_t length,
                    uint32_t flags, uint64_t cookie);
```

An NT **byte-range lock** reserves part of a file for one open. Unlike
Linux's `fcntl` and `flock` locks, which programs can ignore, these are
**enforced**: a read or write that conflicts with another open's lock fails
with `EAGAIN`, whichever program issued it.

`op` is one of:

| Operation | Meaning |
|---|---|
| `KACS_LOCK_EXCLUSIVE` | Keep every other open from reading or writing `length` bytes from `offset`. |
| `KACS_LOCK_SHARED` | Keep everyone, this open included, from writing the range. |
| `KACS_LOCK_UNLOCK` | Release a lock this open holds on exactly that range; `ENOLCK` if it holds none. |
| `KACS_LOCK_CANCEL` | Cancel the waiting request named by `cookie`. |

A conflicting request waits for the range to come free, unless `flags` carries
`KACS_LOCK_FAIL_IMMEDIATELY`, when it fails with `EAGAIN`. Give a waiting
request a `cookie` so that another thread can cancel it. Locks are released
when the open closes.

A lock blocks other opens' reads and writes only if the open that took it
could write the file. A reader's lock still conflicts with other locks, so
cooperating readers can coordinate, but it cannot stop anyone's I/O. Memory
mappings are not covered.

## 5.7.2 Leases

A **lease** lets a program cache a file — its contents, its pending writes, or
an open handle to it — while nobody else needs it. When another operation
would make the cached state wrong, the kernel **breaks** the lease: it tells
the holder to flush and give up some of its state, and the operation waits
until the holder acknowledges. A file server holds leases for its clients and
relays the breaks to them.

```c
int     peios_lease_manager_create(uint32_t flags, uint32_t timeout_ms);
int     peios_file_lease(int fd, int manager_fd, const uint8_t key[16],
                         uint32_t *state, uint32_t flags);
ssize_t peios_lease_manager_read(int manager_fd, struct kacs_lease_break *out, size_t count);
```

A **lease manager** is a descriptor that receives the break notices for the
leases held under it. `flags` takes `KACS_LEASE_MANAGER_CLOEXEC` and
`_NONBLOCK`. `timeout_ms` bounds how long a break waits for acknowledgment
before the kernel completes it itself; zero gives the default of 35 seconds.

`peios_file_lease` requests a lease on `fd`'s file, held under the manager and
a 16-byte **key** that identifies the holder. `*state` is the state asked for
— any of `KACS_LEASE_READ`, `_WRITE`, and `_HANDLE` — and on return the
state granted, which can be less. Opens made with the same manager and key,
through [`KACS_OPEN_LEASE_KEY`](/peios/developing-for-peios/sdk-reference/sdk-files/opening-a-file.md), do not
break it.

`peios_lease_manager_read` reads up to `count` break notices and returns how
many it read. Each names the lease by key, file, current state and the state
it is being broken to, with the `reason` (`KACS_LEASE_BREAK_*`) and an `epoch`
that counts the lease's breaks. Notices are never dropped, and the manager
descriptor is pollable. Answer a notice with `peios_file_lease`, `flags` =
`KACS_LEASE_ACKNOWLEDGE`, and a state no greater than the notice's new state,
through an open of the same file under the same key; the waiting operation
then proceeds.

## 5.7.3 See also

- **[Byte-Range Locks](/kernel/internals/kacs/file-security/byte-range-locks.md)**
  and **[Leases](/kernel/internals/kacs/file-security/leases.md)** — the kernel's
  rules, including what breaks a lease.
