summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-09-27 17:56:32 -0300
committerGabriel Schneider <[email protected]>2026-09-27 18:18:09 -0300
commit5beb0167c3b9dc1a788e08b9c1652aaba8eb0a40 (patch)
treee17d2c4f87a99f7c42ff2b5b47d9f206e0b8c169
parent1c1b192d4ef59199a7196229d56901b1f6512678 (diff)
downloadcloud9-5beb0167c3b9dc1a788e08b9c1652aaba8eb0a40.tar.gz
cloud9-5beb0167c3b9dc1a788e08b9c1652aaba8eb0a40.zip
9ns: keep many 9P requests in flight, so a parked read no longer freezes the mount
9ns served one FUSE request at a time per 9P server, so a read the server parks (pardes's log, a pane's event) held up every other operation on the mount: with 'cat log' parked, 'cat status' took 3.5-4.5 s instead of ~1 ms. nine.zig: the session is shared by threads and multiplexes by tag the way Plan 9's mount driver does (devmnt.c mountio/mountmux/mntgate/ mntflushalloc). Each thread calls through its own nine.Caller (reply buffer, wake-up eventfd, interrupt source, ename). A call writes its T-message under wlock; the first waiter with nobody reading takes the reading baton, reads for everyone and hands each R-message to the caller its tag names, then passes the baton on. Tflush is per tag: a reply that beats the Rflush is honoured, the flushed tag is not reused before the Rflush, the baton is let go before the flush is written. With every tag taken a call waits for one (starved list) instead of failing. bridge.zig: one thread (the loop) still owns the inode/handle/qid tables and serves everything that touches them. READ and WRITE are resolved to their fid on the loop and handed to transfer workers (up to 31 per bridge, started on demand), which make the 9P calls on their own Caller and reply themselves. FUSE_INTERRUPT for a worker's request: still queued -> EINTR on the spot; in flight -> its unique down the worker's pipe -> Tflush of exactly that tag. Fids are clunked only on RELEASE. mntgen mounts get the same pool (their dispatcher routes interrupts to it); the mount interrupt pipe moved into Bridge. main.zig attaches through a Caller. Tests: two multiplexing tests in nine.zig (a parked read holds up only its caller and its interrupt flushes exactly its tag; with every tag parked a call waits for a tag), and a serve()-level test driving a fake /dev/fuse (a parked READ holds up neither READs nor GETATTR; INTERRUPT -> one Tflush for the parked tag -> EINTR; RELEASE). Also de-flaked the pre-existing 'short read' test (it raced its own server reply under load). Measured (own 9ns mount on the live pardes socket, read-only): with log parked, cat status 4498 ms before, 7 ms after. Tag window: client.zig gains ClientWith(n) (Client = ClientWith(16) as before; request/result types hoisted so all instantiations share them). 9ns uses ClientWith(32): 32 is fs.zig's default slot_capacity, so one 9ns mount can never have more parked on a cloud9 fs server than it parks, and never meets its "Resource temporarily unavailable" refusal. fs.zig is unchanged (its default is also the ESP32 board's, held to 24 KiB by its budget tests). Full window: the loop keeps its tag (metadata unaffected); the 32nd READ/WRITE waits in the pool queue (blocks, interruptible, never EAGAIN/EIO). More tests (bridge.zig, serve() over a fake /dev/fuse): one open is one fid (two opens of an inode read through distinct fids, concurrently, one of them parked); FLUSH on a handle with a parked read clunks nothing and the read later completes on that fid; only RELEASE clunks; a flushed READ whose reply beats the Rflush returns the data, not EINTR.
-rw-r--r--9ns/docs/DESIGN.md208
-rw-r--r--9ns/src/bridge.zig846
-rw-r--r--9ns/src/main.zig21
-rw-r--r--9ns/src/nine.zig1033
-rw-r--r--docs/design.md6
-rw-r--r--src/client.zig652
-rw-r--r--src/root.zig1
7 files changed, 2027 insertions, 740 deletions
diff --git a/9ns/docs/DESIGN.md b/9ns/docs/DESIGN.md
index 32c548c..ad59521 100644
--- a/9ns/docs/DESIGN.md
+++ b/9ns/docs/DESIGN.md
@@ -166,12 +166,13 @@ Threads and node ids:
* A **mount** is a posted name walked into: the socket to dial, its own
`nine.Session` and bridge state (inode table, handles — the ordinary
single-server translation, unchanged) once dialed, and a **worker
- thread** that owns all of it. The dispatcher makes a mount without
+ thread** that owns all of it (its transfer workers share the session). The dispatcher makes a mount without
touching the network and hands it the walk; the worker dials while
serving that first request. It pops copied requests off a queue and
- serves them one at a time, exactly the single-connection contract; the
- dispatcher keeps reading `/dev/fuse` meanwhile, so one slow server never
- blocks the other names. Replies go straight back on the FUSE fd (one
+ serves them one at a time, exactly the single-connection contract
+ (READ and WRITE handed on to that mount's transfer workers, see
+ *Concurrency* under `src/bridge.zig`); the dispatcher keeps reading
+ `/dev/fuse` meanwhile, so one slow server never blocks the other names. Replies go straight back on the FUSE fd (one
`writev` per reply; the kernel processes each write as one message).
The queue is a plain list under a mutex and condition rather than an
`std.Io.Queue` because an interrupt has to find and remove a request by
@@ -353,15 +354,17 @@ pub fn nameAfter(comptime T: type, req: Request) ![]const u8; // NUL-terminated
The request buffer must be ≥ `max_write + 4096`; 9ns uses 1 MiB + 4 KiB.
Requests with an unknown/unsupported opcode get `ENOSYS`.
-### `src/nine.zig` — synchronous 9P2000 session on a blocking fd
+### `src/nine.zig` — 9P2000 session on a blocking fd, shared by threads
-Thin, synchronous RPC layer over `cloud9.Client` (which is push/take,
-non-blocking-agnostic). One outstanding request at a time (the FUSE loop is
-single-threaded). Fids are allocated from a free list.
+Thin RPC layer over `cloud9.Client` (which is push/take,
+non-blocking-agnostic). Many requests may be in flight at once, each under
+its own tag; replies come back in any order. The connection is a `Session`;
+each thread that makes calls on it has a `Caller`. Fids are allocated from
+a free list (under the session's mutex).
```zig
pub const Address = union(enum) { unix: []const u8, tcp: struct { host: []const u8, port: u16 }, fd: i32 };
-/// Owner's hook into the reply wait (the bridge's FUSE fd); see "Interrupts" under bridge.zig.
+/// Owner's hook into the reply wait (the bridge's FUSE fd, or an interrupt pipe); see "Interrupts" under bridge.zig.
pub const Interrupt = struct {
ctx: *anyopaque,
watch: *const fn (ctx) i32, // fd to poll alongside the socket, or -1
@@ -370,53 +373,81 @@ pub const Interrupt = struct {
};
pub const Session = struct {
pub const Error = error{ Nine, Protocol, Io, Closed, Stopped, Interrupted, TooLarge, OutOfMemory };
- /// After error.Nine, `ename` holds the server's Rerror text (copied, bounded).
- ename: [256]u8, ename_len: usize,
msize: u32,
- stop_fd: i32 = -1, // readable → pending rpc fails with error.Stopped
- interrupt: ?Interrupt = null,
+ stop_fd: i32 = -1, // readable → every waiting call fails with error.Stopped
+ flush_grace_ms: i32 = 3000, // a Tflush unanswered this long wedges the session
+ wedged: bool, // declared gone: every call → error.Closed
pub fn connect(gpa: std.mem.Allocator, address: Address, msize: u32) !Session; // socket+connect, version
- pub fn deinit(s: *Session) void;
- pub fn attach(s: *Session, fid: u32, uname: []const u8, aname: []const u8) Error!cloud9.Qid;
+ pub fn deinit(s: *Session) void; // after every Caller is done
pub fn allocFid(s: *Session) u32;
pub fn freeFid(s: *Session, fid: u32) void;
- /// Generic RPC. Result slices borrow the input buffer until the next call.
- pub fn rpc(s: *Session, req: cloud9.Client.Request) Error!cloud9.Client.Result;
+ pub fn over(s: *Session) bool; // wedged, or the connection died
+};
+pub const Caller = struct {
+ session: *Session,
+ /// After error.Nine, `ename` holds the server's Rerror text (copied, bounded).
+ ename: [256]u8, ename_len: usize,
+ interrupt: ?Interrupt = null,
+ abort_on_cancel: bool = false, // handshake: a cancellation hangs up instead of flushing
+
+ pub fn init(s: *Session) !Caller; // msize reply buffer + wake-up eventfd
+ pub fn deinit(c: *Caller) void;
+ /// Generic RPC. Result slices borrow the caller's reply buffer until its next call.
+ pub fn rpc(c: *Caller, req: cloud9.Client.Request) Error!cloud9.Client.Result;
// Conveniences (all built on rpc):
- pub fn walk(s, fid: u32, newfid: u32, names: []const []const u8) Error!Walk; // Walk = { nwqid, wqid[16] }; partial walk → error.Nine with ename "not found"-ish
- pub fn clone(s, fid: u32) Error!u32; // allocFid + walk with no names
- pub fn open(s, fid: u32, mode: u8) Error!Open; // Open = { qid, iounit }
- pub fn create(s, fid: u32, name: []const u8, perm: u32, mode: u8) Error!Open;
- pub fn read(s, fid: u32, offset: u64, buf: []u8) Error!usize; // chunks by maxRead/iounit; stops at short read
- pub fn write(s, fid: u32, offset: u64, data: []const u8) Error!usize; // chunks; stops at short write
- pub fn stat(s, fid: u32) Error!cloud9.Stat; // strings borrow the input buffer
- pub fn wstat(s, fid: u32, st: cloud9.Stat) Error!void;
- pub fn clunk(s, fid: u32) Error!void; // frees the fid even on error
- pub fn remove(s, fid: u32) Error!void; // frees the fid even on error
- pub fn errno(s: *const Session) std.os.linux.E; // map ename → errno (see below)
+ pub fn version(c) Error!void;
+ pub fn attach(c, fid: u32, uname: []const u8, aname: []const u8) Error!cloud9.Qid;
+ pub fn walk(c, fid: u32, newfid: u32, names: []const []const u8) Error!Walk; // Walk = { nwqid, wqid[16] }; partial walk → error.Nine with ename "not found"-ish
+ pub fn clone(c, fid: u32) Error!u32; // allocFid + walk with no names
+ pub fn open(c, fid: u32, mode: u8) Error!Open; // Open = { qid, iounit }
+ pub fn create(c, fid: u32, name: []const u8, perm: u32, mode: u8) Error!Open;
+ pub fn read(c, fid: u32, offset: u64, buf: []u8) Error!usize; // chunks by maxRead/iounit; stops at short read
+ pub fn write(c, fid: u32, offset: u64, data: []const u8) Error!usize; // chunks; stops at short write
+ pub fn stat(c, fid: u32) Error!cloud9.Stat; // strings borrow the reply buffer
+ pub fn wstat(c, fid: u32, st: cloud9.Stat) Error!void;
+ pub fn clunk(c, fid: u32) Error!void; // frees the fid even on error
+ pub fn remove(c, fid: u32) Error!void; // frees the fid even on error
+ pub fn errno(c: *const Caller) std.os.linux.E; // map ename → errno (see below)
};
pub const dontcare = cloud9.Stat{ .type = 0xFFFF, .dev = 0xFFFF_FFFF, .qid = .{ .type = 0xFF, .version = 0xFFFF_FFFF, .path = 0xFFFF_FFFF_FFFF_FFFF }, .mode = 0xFFFF_FFFF, .atime = 0xFFFF_FFFF, .mtime = 0xFFFF_FFFF, .length = 0xFFFF_FFFF_FFFF_FFFF, .name = "", .uid = "", .gid = "", .muid = "" };
```
`connect`: for `.unix` and `.tcp` create a blocking `SOCK_STREAM|SOCK_CLOEXEC`
socket and connect (`TCP_NODELAY` on TCP); for `.fd` adopt it. Buffers of
-`msize` bytes for in/out are heap allocated. Then submit `.version`, drain
-output to the socket, read until `take()` yields the version result. The
-negotiated msize is `result.version.msize`; if the server answered
-`"unknown"`, fail with `error.Protocol`.
+`msize` bytes for in/out are heap allocated. Then a `version` rpc on a
+temporary caller. The negotiated msize is `result.version.msize`; if the
+server answered `"unknown"`, fail with `error.Protocol`.
-`rpc`: submit, write all of `client.output()` (calling `wrote`), then loop:
-`take()`; if null, poll the fd together with `stop_fd` and the interrupt
-source's descriptor; when the fd is readable, `read` into a temp buffer and
-`push` (push returns how much fit; the frame is at most msize so it always
-fits after a `take`). If the fd returns 0 → `error.Closed`. If the client
-dies → `error.Protocol`. `stop_fd` readable → `error.Stopped`. When the
-interrupt source asks for it, submit `.flush = .{ .oldtag = tag }` and keep
-waiting: the original reply → returned normally (the Rflush that follows is
-skipped by a later call); the Rflush first → `error.Interrupted`. A `.fail`
-result copies the ename and returns `error.Nine`. `read`/`write` chunk loops
-stop between chunks once the source reports `armed`, and turn an
+`rpc` multiplexes the way Plan 9's mount driver does
+(`kernel/devices/sys/devmnt.c` in principia-softwarica: `mountio`,
+`mountmux`, `mntgate`, `mntflushalloc`). Under the session's `wlock`
+(which serializes the client's output buffer and the socket's write side)
+and `mutex` (tags, input side, the waiter table) the call claims a tag,
+registers its waiter under it and writes the T-message; the write itself
+runs with only `wlock` held. Then it waits: the first waiter that finds
+nobody reading takes the **reading baton** (`m->rip`), reads the socket,
+`push`es and `take`s, copies each R-message into the caller its tag names
+(the client's input buffer belongs to whoever reads next) and pokes that
+caller's eventfd; when its own reply is in it lets go of the baton and
+pokes every waiter, one of which takes it over. Waiters without the baton
+poll their eventfd; all poll `stop_fd` (→ `error.Stopped`) and their own
+interrupt source. A socket EOF → `error.Closed`, a client protocol death →
+`error.Protocol`, for every waiter. When all `nine.max_tags` tags are
+taken a call waits on a starved list (reading if it gets the baton) until a
+reply frees one; nothing fails for lack of a tag.
+
+When the interrupt source asks for it the waiter first gives up the baton
+(a Tflush write that blocks must not stop everyone's replies, the Rflush
+among them, being read), then submits `.flush = .{ .oldtag = tag }` under a
+tag of its own and keeps waiting on both tags: the original reply first →
+it is honoured and returned (flush(5): a Twrite that completed changed
+state; the Rflush that follows is dropped); the Rflush first →
+`error.Interrupted`. The client does not reuse the flushed tag until the
+Rflush is in. A Tflush unanswered for `flush_grace_ms` wedges the session
+(every waiter → `error.Closed`). A `.fail` result copies the ename into the
+caller and returns `error.Nine`. `read`/`write` chunk loops (per call, per
+caller) stop between chunks once the source reports `armed`, and turn an
`Interrupted` chunk into a short count when earlier chunks moved data.
Rerror text → errno mapping (case-insensitive substring, in this order):
@@ -491,8 +522,8 @@ Op mapping (9P2000 has no symlinks, links, xattrs, locks, mknod):
| SETATTR | `stat` then `wstat` with a *dontcare* Stat: `FATTR_SIZE`→length; `FATTR_MODE`→`(old.mode & ~0o777) \| (mode & 0o777)`; `FATTR_MTIME`→mtime (`FATTR_MTIME_NOW` → now); `FATTR_ATIME` ignored; `FATTR_UID/GID` → `EPERM` unless unchanged; then `stat` again for the reply |
| OPEN | `clone(inode.fid)` then `open(newfid, mode)`; mode from `O_ACCMODE` (`oread/owrite/ordwr`), `O_TRUNC` → `otrunc`; reply `OpenOut{ fh, open_flags = FOPEN_DIRECT_IO }`; on failure clunk |
| OPENDIR | same with `oread`; `fh` with `dir = null` |
-| READ | `read(fh.fid, offset, buf[0..min(size, 1 MiB)])`; reply data |
-| WRITE | `write(fh.fid, offset, data)`; `WriteOut{ size = n }` |
+| READ | on a transfer worker: `read(fh.fid, offset, buf[0..min(size, 1 MiB)])`; reply data |
+| WRITE | on a transfer worker: `write(fh.fid, offset, data)`; `WriteOut{ size = n }` |
| READDIR | fill `Dirent`s from the `DirList` starting at `offset`, up to `size` bytes |
| RELEASE / RELEASEDIR | `clunk(fh.fid)`; free DirList |
| FLUSH / FSYNC / FSYNCDIR | ok (no-op) |
@@ -528,12 +559,46 @@ rather than poisoning the whole READDIR reply; `length` near 2^64 is clamped
to `i64` max; the errno of a failing 9P call is latched before any cleanup
clunk overwrites the session's ename.
+#### Concurrency (transfer workers)
+
+One thread, the loop, owns the inode, handle and qid tables and serves
+every request that reads or changes them, one at a time; lookups and
+forgets are reconciled there and nowhere else. READ and WRITE are the
+requests a synthetic server parks (a blocking `log`, `event` or `run`
+file), and they need no table beyond the handle → fid lookup, so the loop
+does that lookup and hands the transfer to a **transfer worker**
+(`Pool`/`Worker` in `bridge.zig`): a thread with its own `nine.Caller`,
+data buffer and interrupt pipe, which makes the 9P calls, chunks by
+msize/iounit itself and writes the reply to the FUSE fd. The session
+multiplexes their tags, so a parked read holds up only its own process:
+with pardes's `log` parked, `cat status` answers in milliseconds instead of
+waiting for the read. Workers start on demand, up to `max_workers =
+nine.max_tags - 1` per bridge (the loop keeps the last tag); a transfer
+that finds every worker parked waits in the queue for one, where an
+INTERRUPT answers it `EINTR` at once. Every queued transfer gets an idle
+worker of its own or a new one, so a transfer never waits behind a parked
+one while the pool has room.
+
+The fid a worker uses is the handle's, and is clunked only on RELEASE,
+which the kernel sends once no read or write on that open file is in
+progress (FLUSH, sent on every `close`, clunks nothing). Two processes
+reading or writing one open file concurrently now run concurrently on
+the server too; one process's write-then-read stays ordered because each
+syscall waits for its reply, and 9P promises nothing more.
+
+This is devmnt's model with the waiting given threads: in Plan 9 every
+process blocked in a read is its own waiter on the mount; a FUSE server
+gets the kernel's requests on one descriptor, so the waiting processes
+are represented by workers here.
+
#### Interrupts (FUSE_INTERRUPT → Tflush)
-One request at a time, but not deaf. `serve` puts the FUSE fd in `O_NONBLOCK`
-mode (every read follows a poll) and installs a `nine.Interrupt` source that
-`Session.rpc` polls together with the socket and `stop_fd` whenever a reply
-is outstanding, including the initial root stat:
+The loop waits on the server too (its own lookups, opens, stats), but it
+is not deaf meanwhile. `serve` puts the FUSE fd in `O_NONBLOCK` mode (every
+read follows a poll) and installs a `nine.Interrupt` source on the loop's
+caller that `Caller.rpc` polls together with `stop_fd` (and the socket,
+when it holds the baton) whenever a reply is outstanding, including the
+initial root stat:
* `onReadable` reads the request the kernel has ready into a second
8-aligned buffer of `request_buf_len` bytes (`spare_buf`). A
@@ -541,15 +606,21 @@ is outstanding, including the initial root stat:
(`cur_unique`) sets `interrupted` and returns true: `rpc` sends
`Tflush(oldtag)` and keeps waiting until either the original reply arrives
(the interrupt raced it: the result is returned as if nothing happened and
- the Rflush that follows is swallowed by a later call) or the Rflush does
+ the Rflush that follows is dropped by whoever reads it) or the Rflush does
(`error.Interrupted` → `EINTR`; a server that instead answers the flushed
request with an Rerror containing "interrupt", as Pardes does, lands on the
- same errno through the ename table). An INTERRUPT for any other unique is
- consumed and dropped (the kernel expects no reply). Any other request
+ same errno through the ename table). An INTERRUPT for a transfer worker's
+ request goes to that worker: still queued, it is answered `EINTR` on the
+ spot; in flight, its unique goes down the worker's interrupt pipe, and the
+ worker's session sends the Tflush the same way. An INTERRUPT for any
+ other unique is consumed and dropped (the kernel expects no reply); the
+ loop routes one it reads while idle the same way. Any other request
(FORGET, RELEASE, INIT during the root stat, a second process's LOOKUP) is
copied into a queue that `serve` dispatches, in arrival order, before it
- polls again; the fd stays watched throughout, so an INTERRUPT is never
- stuck behind a parked request (a copy that cannot be allocated answers
+ polls again (a READ or WRITE goes to a worker on the spot instead, and an
+ INTERRUPT for a request still in that queue answers it `EINTR`); the fd
+ stays watched throughout, so an INTERRUPT is never stuck behind a parked
+ request (a copy that cannot be allocated answers
`ENOMEM` on the spot).
* The INTERRUPT applies to `cur_unique` only and is consumed when read: the
clunks that unwind a half-done lookup/create/mkdir after an `Interrupted`
@@ -559,7 +630,7 @@ is outstanding, including the initial root stat:
* Multi-step operations fail with `EINTR` at whichever rpc was flushed and
release the fids they had allocated (`--debug` prints `fids=N` per
request; the interrupt suite checks it and the server-side count). The
- chunk loops (`Session.read/write`, `loadDir`) also stop between chunks
+ chunk loops (`Caller.read/write`, `loadDir`) also stop between chunks
while `armed`, so a reply that won the race cannot lead into another
blocking chunk: a read or write that already moved data returns the partial
count like read(2), one that moved nothing and a directory listing return
@@ -575,7 +646,32 @@ behind — so the reader gets `EINTR` and the session ends (the hostile
`never` mode; in single-connection mode that is the mount, in mntgen that
one name). A slow-but-alive server that cannot answer a flush inside its own
blocking read pays the same price; the alternative was a hang that only
-SIGKILL of 9ns could end.
+SIGKILL of 9ns could end. When a worker's flush wedges the session the
+loop notices at its next request (every call on the session then fails)
+and the mount ends there.
+
+Budgets: a session's 9P client is `cloud9.ClientWith(nine.max_tags)`, 32
+request tags plus one kept for flushes (cloud9's default `Client` has 16),
+so one mount has at most 32 requests on the server at once. 32 is the
+cloud9 file server engine's `slot_capacity` (`src/fs.zig`, what pardes
+serves with): the most reads, writes, opens and clunks it parks per
+connection, answering the one past it with Rerror "Resource temporarily
+unavailable" (which would reach the reader as `EIO`). A mount can
+therefore never meet that refusal. The two are raised together or not at
+all: the engine's default is also the ESP32 board's (its budget tests in
+`fs.zig` hold a default server to 24 KiB), so a bigger window is a
+per-server option (pardes's editor `fs.Options`) plus `nine.max_tags`,
+not a new default.
+
+When the window is full: the loop keeps a tag of its own, so lookups,
+opens, stats, listings and every other request the loop serves go on;
+31 parked transfers occupy all the workers, and a 32nd READ or WRITE,
+quick or not (9ns cannot tell a `status` read from a `log` read), waits
+in the pool queue until a parked one ends or is interrupted. It blocks;
+it is never refused (`EAGAIN`) or failed (`EIO`), and an INTERRUPT
+answers it `EINTR` at once. A worker whose flushed request was answered
+before its Rflush holds the flushed tag until that Rflush; a call that
+meanwhile finds no tag waits for one the same way.
### `src/ns.zig` — namespace and process plumbing
diff --git a/9ns/src/bridge.zig b/9ns/src/bridge.zig
index 327cde1..2226658 100644
--- a/9ns/src/bridge.zig
+++ b/9ns/src/bridge.zig
@@ -1,16 +1,29 @@
//! FUSE ↔ 9P2000 translation: the request loop that turns kernel FUSE requests
//! into synchronous 9P calls on a `nine.Session` and sends the replies back.
//!
-//! Everything here is single-threaded and one request at a time. State is three
-//! tables: inodes (nodeid → fid/qid, deduplicated by qid.path), open handles
-//! (fh → fid plus a cached directory listing), and the reverse qid map.
+//! State is three tables: inodes (nodeid → fid/qid, deduplicated by
+//! qid.path), open handles (fh → fid plus a cached directory listing), and
+//! the reverse qid map. One thread owns them, the loop, and serves every
+//! request that reads or changes them one at a time.
//!
-//! One request at a time does not mean deaf: while a 9P reply is outstanding
-//! the session polls the FUSE descriptor too (`nine.Interrupt`). A
-//! FUSE_INTERRUPT for the request being served becomes a Tflush, and if the
-//! server honours it the request fails with EINTR; anything else the kernel
-//! sends meanwhile is copied into a queue and served next, so the FUSE fd
-//! is always being read and no INTERRUPT waits behind a parked request.
+//! READ and WRITE are the exception: the loop resolves the handle to its fid
+//! and hands the transfer to a worker thread (`Pool`), which makes the 9P
+//! calls on its own `nine.Caller` and replies itself. The session multiplexes
+//! them by tag, so a read the server parks (a blocking `log` or `event`
+//! file) holds up only the process that issued it, not the mount. The
+//! workers touch no table; the fid they use stays bound until FUSE_RELEASE,
+//! which the kernel sends only once no read or write on that file is still
+//! in progress.
+//!
+//! Waiting on the server does not make the loop deaf: while one of its own
+//! 9P replies is outstanding the session polls the FUSE descriptor too
+//! (`nine.Interrupt`). A FUSE_INTERRUPT for the request being served becomes
+//! a Tflush, and if the server honours it the request fails with EINTR; one
+//! for a worker's request is passed to that worker, whose session does the
+//! same; READ and WRITE are handed to workers on the spot; anything else the
+//! kernel sends meanwhile is copied into a queue and served next, so the
+//! FUSE fd is always being read and no INTERRUPT waits behind a parked
+//! request.
const std = @import("std");
const cloud9 = @import("cloud9");
const post = cloud9.post;
@@ -88,10 +101,12 @@ pub fn serve(gpa: std.mem.Allocator, fuse_fd: i32, session: *nine.Session, root_
// (often zero) size and truncate reads: 9P sizes are authoritative and change
// under us. direct_io ignores the cached size, so the cache is safe only there.
if (!effective.direct_io) effective.attr_timeout_ns = 0;
+ var caller = try nine.Caller.init(session);
+ defer caller.deinit();
var b: Bridge = .{
.gpa = gpa,
.fuse_fd = fuse_fd,
- .nine = session,
+ .nine = &caller,
.opts = effective,
};
defer b.deinit();
@@ -111,8 +126,7 @@ pub fn serve(gpa: std.mem.Allocator, fuse_fd: i32, session: *nine.Session, root_
defer session.stop_fd = -1;
// And watch the FUSE fd meanwhile: INTERRUPTs become Tflush, other
// requests (INIT arrives during the root stat) wait in the stash.
- session.interrupt = b.interruptSource();
- defer session.interrupt = null;
+ caller.interrupt = b.interruptSource();
// Node 1 is the root; its qid comes from a stat so lookups resolving back to
// it (e.g. via a walk) dedupe onto node 1.
@@ -171,7 +185,8 @@ pub fn serve(gpa: std.mem.Allocator, fuse_fd: i32, session: *nine.Session, root_
const Bridge = struct {
gpa: std.mem.Allocator,
fuse_fd: i32,
- nine: *nine.Session,
+ /// This thread's side of the 9P session (a worker's bridge has its own).
+ nine: *nine.Caller,
opts: Options,
/// Node id of this bridge's root inode. `fuse.root_id` (1) in
/// single-connection mode; in mntgen mode the mount's `root_node`,
@@ -198,8 +213,15 @@ const Bridge = struct {
stash: std.ArrayList([]align(8) u8) = .empty,
/// `unique` of the FUSE request being served, if any (0 = none): the only
/// one an INTERRUPT may cancel. Atomic: in mntgen mode the dispatcher
- /// thread scans it to route FUSE_INTERRUPTs to the right server.
+ /// thread scans it to route FUSE_INTERRUPTs to the right server, and
+ /// the loop scans its workers' to route theirs.
cur_unique: std.atomic.Value(u64) = .init(0),
+ /// Where an INTERRUPT is delivered when this bridge's thread does not read
+ /// the FUSE fd itself (a worker, a mntgen mount): whoever does writes the
+ /// target's unique into `int_pipe[1]`, and `pipeSource` polls `[0]`.
+ int_pipe: [2]i32 = .{ -1, -1 },
+ /// The workers READ and WRITE run on (the table-owning bridge's only).
+ pool: Pool = .{},
/// An INTERRUPT for `cur_unique` was consumed: chunked loops stop early
/// even when the flushed reply won the race. Reset per request.
interrupted: bool = false,
@@ -219,6 +241,10 @@ const Bridge = struct {
last_err: linux.E = .IO,
fn deinit(b: *Bridge) void {
+ b.stopPool();
+ if (b.int_pipe[0] >= 0) _ = linux.close(b.int_pipe[0]);
+ if (b.int_pipe[1] >= 0) _ = linux.close(b.int_pipe[1]);
+ b.int_pipe = .{ -1, -1 };
var it = b.handles.valueIterator();
while (it.next()) |h| if (h.dir) |*d| d.deinit(b.gpa);
b.handles.deinit(b.gpa);
@@ -274,9 +300,30 @@ const Bridge = struct {
b.interrupted = true;
return true;
}
+ if (b.interruptWorker(in.unique)) return false;
+ // Waiting its turn in the stash: it has not started, and the
+ // kernel sends an INTERRUPT once, so it ends here.
+ for (b.stash.items, 0..) |raw, i| {
+ if (uniqueOf(raw) != in.unique or !wantsReply(raw)) continue;
+ _ = b.stash.orderedRemove(i);
+ b.gpa.free(raw);
+ b.trace("<- interrupt for unique={d} (stashed): answered EINTR", .{in.unique});
+ b.replyError(in.unique, .INTR) catch {};
+ return false;
+ }
b.trace("<- interrupt for unique={d} (not in flight; ignored)", .{in.unique});
return false;
}
+ // A transfer goes to a worker right away rather than wait behind
+ // the loop's own 9P reply.
+ if (h.op() == .read or h.op() == .write) {
+ if (b.ioFid(req)) |fid| {
+ if (b.offload(req, fid)) {
+ b.trace("<- {s} unique={d} handed to a worker while a 9P reply is outstanding", .{ opName(h.op()), h.unique });
+ return false;
+ }
+ } else |_| {}
+ }
b.trace("<- {s} unique={d} nodeid={d} stashed while a 9P reply is outstanding", .{ opName(h.op()), h.unique, h.nodeid });
const raw = b.spare_buf[0..h.len];
const copy = b.gpa.alignedAlloc(u8, .@"8", raw.len) catch {
@@ -307,7 +354,7 @@ const Bridge = struct {
fn dispatch(b: *Bridge, req: fuse.Request) !bool {
const h = req.header;
const op = h.op();
- b.trace("<- {s} unique={d} nodeid={d} len={d} (fids={d} inodes={d} handles={d})", .{ opName(op), h.unique, h.nodeid, h.len, b.nine.fidsInUse(), b.inodes.count(), b.handles.count() });
+ b.trace("<- {s} unique={d} nodeid={d} len={d} (fids={d} inodes={d} handles={d})", .{ opName(op), h.unique, h.nodeid, h.len, b.nine.session.fidsInUse(), b.inodes.count(), b.handles.count() });
const wants_reply = switch (op) {
.forget, .batch_forget, .interrupt => false,
else => true,
@@ -322,23 +369,8 @@ const Bridge = struct {
b.handle(req) catch |e| {
if (e == error.Stopped and b.fuse_gone) return false;
if (e == error.Stopped and b.fuse_fail != null) return b.fuse_fail.?;
- const code: linux.E = switch (e) {
- error.Nine => b.last_err,
- error.BadRequest => .INVAL,
- error.NoEntry => .NOENT,
- error.BadHandle => .BADF,
- error.Exdev => .XDEV,
- error.Perm => .PERM,
- error.NotSup => .NOSYS,
- error.OutOfMemory => .NOMEM,
- error.TooLarge => .NAMETOOLONG,
- error.BadDir => .IO,
- error.Closed, error.Io => if (b.stale_on_death) .STALE else .IO,
- error.Protocol, error.Stopped => .IO,
- error.Interrupted => .INTR,
- error.FuseIo => return error.FuseIo,
- };
- if (wants_reply) try b.replyError(h.unique, code);
+ if (e == error.FuseIo) return error.FuseIo;
+ if (wants_reply) try b.replyError(h.unique, b.errnoOf(e));
switch (e) {
error.Closed, error.Protocol, error.Io => return error.Closed,
error.Stopped => return false, // the child is gone; the mount is being torn down
@@ -346,12 +378,31 @@ const Bridge = struct {
}
};
// An EINTR that came from the server going mute (a Tflush unanswered
- // past its grace) is the last thing this session says: the
- // connection is gone with it.
- if (b.nine.wedged) return error.Closed;
+ // past its grace, here or on a worker) is the last thing this session
+ // says: the connection is gone with it.
+ if (b.nine.session.over()) return error.Closed;
return true;
}
+ /// The errno a failed request is answered with.
+ fn errnoOf(b: *const Bridge, e: HandlerError) linux.E {
+ return switch (e) {
+ error.Nine => b.last_err,
+ error.BadRequest => .INVAL,
+ error.NoEntry => .NOENT,
+ error.BadHandle => .BADF,
+ error.Exdev => .XDEV,
+ error.Perm => .PERM,
+ error.NotSup => .NOSYS,
+ error.OutOfMemory => .NOMEM,
+ error.TooLarge => .NAMETOOLONG,
+ error.BadDir => .IO,
+ error.Closed, error.Io => if (b.stale_on_death) .STALE else .IO,
+ error.Protocol, error.Stopped, error.FuseIo => .IO,
+ error.Interrupted => .INTR,
+ };
+ }
+
fn handle(b: *Bridge, req: fuse.Request) HandlerError!void {
const u = req.header.unique;
switch (req.header.op()) {
@@ -388,21 +439,11 @@ const Bridge = struct {
.setattr => try b.setattr(req),
.open => try b.openFile(req, false),
.opendir => try b.openFile(req, true),
- .read => {
- const in = try body(fuse.ReadIn, req);
- const h = b.handles.get(in.fh) orelse return error.BadHandle;
- const want: usize = @min(in.size, max_write);
- const n = try b.read(h.fid, in.offset, b.data_buf[0..want]);
- try b.reply(u, &.{b.data_buf[0..n]});
- },
- .write => {
- const in = try body(fuse.WriteIn, req);
- const h = b.handles.get(in.fh) orelse return error.BadHandle;
- const rest = req.body[@sizeOf(fuse.WriteIn)..];
- if (rest.len < in.size) return error.BadRequest;
- const n = try b.write(h.fid, in.offset, rest[0..in.size]);
- const out = fuse.WriteOut{ .size = @intCast(n) };
- try b.reply(u, &.{std.mem.asBytes(&out)});
+ .read, .write => {
+ const fid = try b.ioFid(req);
+ if (b.offload(req, fid)) return;
+ // No worker to be had: the loop does it itself, as it used to.
+ try b.transfer(req, fid);
},
.readdir => try b.readdir(req),
.release, .releasedir => {
@@ -453,7 +494,10 @@ const Bridge = struct {
const out = fuse.StatfsOut{ .st = .{ .bsize = 4096, .namelen = 255, .frsize = 4096 } };
try b.reply(u, &.{std.mem.asBytes(&out)});
},
- .interrupt => {},
+ .interrupt => {
+ const in = try body(fuse.InterruptIn, req);
+ if (!b.interruptWorker(in.unique)) b.trace(" interrupt for unique={d} (not in flight; ignored)", .{in.unique});
+ },
.destroy => unreachable, // handled in dispatch
.access => return error.NotSup,
else => return error.NotSup,
@@ -716,6 +760,226 @@ const Bridge = struct {
return list;
}
+ // -- READ and WRITE on workers ------------------------------------------------------
+
+ /// The fid behind a READ's or WRITE's file handle (loop thread: the
+ /// handle table is the loop's).
+ fn ioFid(b: *Bridge, req: fuse.Request) HandlerError!u32 {
+ const fh = switch (req.header.op()) {
+ .read => (try body(fuse.ReadIn, req)).fh,
+ .write => (try body(fuse.WriteIn, req)).fh,
+ else => unreachable,
+ };
+ const h = b.handles.get(fh) orelse return error.BadHandle;
+ return h.fid;
+ }
+
+ /// The 9P half of a READ or WRITE on `fid`, and the reply. Touches no
+ /// table, so it runs on a worker as well as on the loop.
+ fn transfer(b: *Bridge, req: fuse.Request, fid: u32) HandlerError!void {
+ const u = req.header.unique;
+ switch (req.header.op()) {
+ .read => {
+ const in = try body(fuse.ReadIn, req);
+ const want: usize = @min(in.size, max_write);
+ const n = try b.read(fid, in.offset, b.data_buf[0..want]);
+ try b.reply(u, &.{b.data_buf[0..n]});
+ },
+ .write => {
+ const in = try body(fuse.WriteIn, req);
+ const rest = req.body[@sizeOf(fuse.WriteIn)..];
+ if (rest.len < in.size) return error.BadRequest;
+ const n = try b.write(fid, in.offset, rest[0..in.size]);
+ const out = fuse.WriteOut{ .size = @intCast(n) };
+ try b.reply(u, &.{std.mem.asBytes(&out)});
+ },
+ else => unreachable,
+ }
+ }
+
+ /// Queues a READ or WRITE (copied: the request buffer is reused) for a
+ /// worker, starting one when every worker is taken and the pool is not
+ /// full. False when there is no worker to be had (no memory, no thread,
+ /// the pool stopping): the caller serves the request itself.
+ fn offload(b: *Bridge, req: fuse.Request, fid: u32) bool {
+ const raw = b.gpa.alignedAlloc(u8, .@"8", req.header.len) catch return false;
+ @memcpy(raw[0..@sizeOf(fuse.InHeader)], std.mem.asBytes(&req.header));
+ @memcpy(raw[@sizeOf(fuse.InHeader)..], req.body);
+ const io = nine.lockIo();
+ const p = &b.pool;
+ p.mutex.lockUncancelable(io);
+ defer p.mutex.unlock(io);
+ if (!p.stopping) {
+ // Every queued request needs an idle worker of its own, or it
+ // would wait behind a read that may park.
+ if (p.jobs.items.len + 1 > p.idle) b.addWorker();
+ if (p.count != 0) {
+ if (p.jobs.append(b.gpa, .{ .raw = raw, .fid = fid })) |_| {
+ p.cond.signal(io);
+ return true;
+ } else |_| {}
+ }
+ }
+ b.gpa.free(raw);
+ return false;
+ }
+
+ /// Starts a worker if the pool has room (pool mutex held). Best effort:
+ /// without one the queued request waits for a busy worker.
+ fn addWorker(b: *Bridge) void {
+ const p = &b.pool;
+ const slot = for (p.workers, 0..) |w, i| {
+ if (w == null) break i;
+ } else return;
+ const w = b.gpa.create(Worker) catch return;
+ w.* = .{
+ .owner = b,
+ .b = .{ .gpa = b.gpa, .fuse_fd = b.fuse_fd, .nine = &w.caller, .opts = b.opts, .stale_on_death = b.stale_on_death },
+ .caller = nine.Caller.init(b.nine.session) catch {
+ b.gpa.destroy(w);
+ return;
+ },
+ .thread = undefined,
+ };
+ // From here on `w.b.deinit` frees what the worker holds.
+ const ready = blk: {
+ w.b.data_buf = b.gpa.alloc(u8, max_write) catch break :blk false;
+ if (linux.errno(linux.pipe2(&w.b.int_pipe, .{ .CLOEXEC = true, .NONBLOCK = true })) != .SUCCESS) {
+ w.b.int_pipe = .{ -1, -1 };
+ break :blk false;
+ }
+ w.caller.interrupt = w.b.pipeSource();
+ w.thread = std.Thread.spawn(.{}, poolWorker, .{w}) catch break :blk false;
+ break :blk true;
+ };
+ if (!ready) {
+ w.caller.deinit();
+ w.b.deinit();
+ b.gpa.destroy(w);
+ return;
+ }
+ p.workers[slot] = w;
+ p.count += 1;
+ b.trace(" worker {d} started ({d} in the pool)", .{ slot, p.count });
+ }
+
+ /// An INTERRUPT for a request a worker holds. Still queued, it has not
+ /// started: it is taken out and answered EINTR here (the kernel sends an
+ /// INTERRUPT once). In flight, its unique goes down the worker's pipe and
+ /// the worker's session flushes the 9P request. False when no worker has
+ /// it. Safe from any thread (mntgen's dispatcher routes here too).
+ fn interruptWorker(b: *Bridge, target: u64) bool {
+ const io = nine.lockIo();
+ const p = &b.pool;
+ p.mutex.lockUncancelable(io);
+ for (p.jobs.items, 0..) |job, i| {
+ if (uniqueOf(job.raw) != target) continue;
+ _ = p.jobs.orderedRemove(i);
+ p.mutex.unlock(io);
+ b.trace(" interrupt for unique={d}: still queued for a worker; answered EINTR", .{target});
+ b.replyError(target, .INTR) catch {};
+ b.gpa.free(job.raw);
+ return true;
+ }
+ for (p.workers) |slot| {
+ const w = slot orelse continue;
+ if (w.b.cur_unique.load(.seq_cst) != target) continue;
+ // Under the pool mutex, which is where a worker takes a request
+ // on (sets cur_unique) and where its pipe is closed. A dropped
+ // packet (full pipe) only means one interrupt missed its window.
+ var packet: [8]u8 = undefined;
+ std.mem.writeInt(u64, &packet, target, .little);
+ _ = linux.write(w.b.int_pipe[1], &packet, packet.len);
+ p.mutex.unlock(io);
+ b.trace(" interrupt for unique={d}: forwarded to a worker", .{target});
+ return true;
+ }
+ p.mutex.unlock(io);
+ return false;
+ }
+
+ /// Stops and joins the workers. Requests still queued are answered as a
+ /// lost session's are; a worker parked in a 9P wait is released by
+ /// hanging up the socket (the program's exit releases it through
+ /// `stop_fd` anyway, but an unmount or DESTROY can come with the program
+ /// alive, and so can a mntgen mount's death). The session must still be
+ /// open.
+ fn stopPool(b: *Bridge) void {
+ const io = nine.lockIo();
+ const p = &b.pool;
+ p.mutex.lockUncancelable(io);
+ p.stopping = true;
+ for (p.jobs.items) |job| {
+ b.replyError(uniqueOf(job.raw), if (b.stale_on_death) .STALE else .IO) catch {};
+ b.gpa.free(job.raw);
+ }
+ p.jobs.clearAndFree(b.gpa);
+ var busy = false;
+ for (p.workers) |slot| {
+ if (slot) |w| busy = busy or w.b.cur_unique.load(.seq_cst) != 0;
+ }
+ p.mutex.unlock(io);
+ if (p.count == 0) return;
+ p.cond.broadcast(io);
+ if (busy) _ = linux.shutdown(b.nine.session.fd, linux.SHUT.RDWR);
+ for (&p.workers) |*slot| {
+ const w = slot.* orelse continue;
+ w.thread.join();
+ p.mutex.lockUncancelable(io);
+ slot.* = null;
+ p.count -= 1;
+ p.mutex.unlock(io);
+ w.caller.deinit();
+ w.b.deinit();
+ b.gpa.destroy(w);
+ }
+ }
+
+ // -- an interrupt pipe as the interrupt source ------------------------------------------
+ //
+ // For a bridge whose thread does not read /dev/fuse (a worker; a mntgen
+ // mount, whose dispatcher owns the fd): whoever reads the INTERRUPT
+ // writes the target's unique into `int_pipe[1]`.
+
+ fn pipeSource(b: *Bridge) nine.Interrupt {
+ return .{ .ctx = b, .watch = pipeWatch, .onReadable = pipeReadable, .armed = interruptArmed };
+ }
+
+ fn pipeWatch(ctx: *anyopaque) i32 {
+ const b: *Bridge = @ptrCast(@alignCast(ctx));
+ return b.int_pipe[0];
+ }
+
+ fn pipeReadable(ctx: *anyopaque) nine.Session.Error!bool {
+ const b: *Bridge = @ptrCast(@alignCast(ctx));
+ return b.pipeHit();
+ }
+
+ /// Drains the interrupt pipe; true when one of the packets named the
+ /// request in flight, which is then marked interrupted.
+ fn pipeHit(b: *Bridge) nine.Session.Error!bool {
+ var packet: [8]u8 = undefined;
+ var hit = false;
+ while (true) {
+ const rc = linux.read(b.int_pipe[0], &packet, packet.len);
+ switch (linux.errno(rc)) {
+ .SUCCESS => {
+ // Pipe writes of 8 bytes are atomic; a short read cannot happen.
+ const target = std.mem.readInt(u64, &packet, .little);
+ const cur = b.cur_unique.load(.seq_cst);
+ if (cur != 0 and target == cur) hit = true;
+ },
+ .INTR => continue,
+ .AGAIN => break, // drained
+ else => return error.Io,
+ }
+ }
+ if (!hit) return false;
+ b.trace("<- interrupt for unique={d} (in flight): cancelling, sending Tflush", .{b.cur_unique.load(.seq_cst)});
+ b.interrupted = true;
+ return true;
+ }
+
// -- 9P wrappers (tracing) -----------------------------------------------------------
fn stat(b: *Bridge, fid: u32) nine.Session.Error!cloud9.Stat {
@@ -740,9 +1004,9 @@ const Bridge = struct {
/// interruption the server may or may not have bound it (the Rflush
/// tells us only that no reply is coming), so it is clunked to be sure.
fn walkTo(b: *Bridge, fid: u32, names: []const []const u8) nine.Session.Error!u32 {
- const newfid = b.nine.allocFid();
+ const newfid = b.nine.session.allocFid();
_ = b.nine.walk(fid, newfid, names) catch |e| {
- if (e == error.Interrupted) b.clunkQuiet(newfid) else b.nine.freeFid(newfid);
+ if (e == error.Interrupted) b.clunkQuiet(newfid) else b.nine.session.freeFid(newfid);
return b.nineErr("walk", fid, e);
};
return newfid;
@@ -850,15 +1114,94 @@ const Bridge = struct {
};
// ===========================================================================
+// Workers: READ and WRITE off the loop
+// ===========================================================================
+//
+// Plan 9's mount driver needs no pool: every process blocked in a read is
+// its own waiter on the mount (devmnt.c `mountio`). A FUSE server receives
+// the kernel's requests on one descriptor instead, so the waiting has to be
+// given threads here. Only READ and WRITE get them: those are the requests
+// a synthetic server parks (a blocking `log`, `event` or `run` file) and
+// they touch no table, needing just the fid their handle resolved to on
+// the loop. Everything else stays on the loop, so the inode, handle and
+// qid tables, and the reconciliation of lookups with forgets, keep a
+// single owner.
+
+/// Workers per bridge. With the loop they make as many callers as the 9P
+/// client has request tags (`nine.max_tags`), so parked transfers cannot
+/// starve the loop's lookups of a tag. A transfer that finds every worker
+/// parked waits in the queue for one (interruptible there too).
+pub const max_workers: usize = nine.max_tags - 1;
+
+const Pool = struct {
+ /// Guards everything here, and each worker's move of a request from
+ /// the queue to its `cur_unique` and back (see `interruptWorker`).
+ mutex: std.Io.Mutex = .init,
+ cond: std.Io.Condition = .init,
+ /// Transfers waiting for a worker, in arrival order.
+ jobs: std.ArrayList(Job) = .empty,
+ workers: [max_workers]?*Worker = @splat(null),
+ count: usize = 0,
+ /// Workers waiting on `cond` for a job.
+ idle: usize = 0,
+ stopping: bool = false,
+};
+
+/// A READ or WRITE, copied out of the request buffer, and the fid its file
+/// handle named when the loop looked it up.
+const Job = struct { raw: []align(8) u8, fid: u32 };
+
+/// A worker thread and a bridge of its own that owns no table: its caller
+/// on the session, its data buffer, the request in flight, the errno of the
+/// last Rerror, its interrupt pipe.
+const Worker = struct {
+ owner: *Bridge,
+ b: Bridge,
+ caller: nine.Caller,
+ thread: std.Thread,
+};
+
+fn poolWorker(w: *Worker) void {
+ const io = nine.lockIo();
+ const p = &w.owner.pool;
+ p.mutex.lockUncancelable(io);
+ while (true) {
+ if (p.jobs.items.len != 0) {
+ const job = p.jobs.orderedRemove(0);
+ // In flight from the moment it leaves the queue, under the same
+ // lock: an INTERRUPT finds it in one place or the other.
+ w.b.cur_unique.store(uniqueOf(job.raw), .seq_cst);
+ p.mutex.unlock(io);
+ const req = requestOf(job.raw);
+ w.b.interrupted = false;
+ w.b.trace(" worker: {s} unique={d} fid={d}", .{ opName(req.header.op()), req.header.unique, job.fid });
+ w.b.transfer(req, job.fid) catch |e| {
+ if (e != error.FuseIo) w.b.replyError(req.header.unique, w.b.errnoOf(e)) catch {};
+ };
+ w.owner.gpa.free(job.raw);
+ p.mutex.lockUncancelable(io);
+ w.b.cur_unique.store(0, .seq_cst);
+ continue;
+ }
+ if (p.stopping) break;
+ p.idle += 1;
+ p.cond.waitUncancelable(io, &p.mutex);
+ p.idle -= 1;
+ }
+ p.mutex.unlock(io);
+}
+
+// ===========================================================================
// mntgen: many servers behind one FUSE mount
// ===========================================================================
//
// `9ns --mntgen` serves one FUSE mount whose synthetic root lists the posted
// 9P services in `$XDG_RUNTIME_DIR/9p` (cloud9.post's registry; no connection
// is made to list). A walk into a name makes a mount for it and hands the
-// walk to the mount's worker thread, which dials the server (the only
-// thread that ever talks to it) and then runs the ordinary bridge
-// translation above for everything under the name. The dispatcher thread
+// walk to the mount's worker thread, which dials the server and then runs
+// the ordinary bridge translation above for everything under the name
+// (READ and WRITE on that mount's own transfer workers, on the same
+// session). The dispatcher thread
// reads /dev/fuse, routes, and serves the registry's own directories; it
// never waits on a server, so one server that never answers holds up only
// the walks into its own name — and those, being ordinary requests on a
@@ -991,12 +1334,17 @@ const Mount = struct {
/// the worker under `mutex`, so the dispatcher can shut the socket down
/// at teardown; `root_attr` and the bridge's root inode exist with it.
session: ?nine.Session = null,
+ /// The worker's caller on `session` (the bridge's `nine`), made and
+ /// dropped with it.
+ caller: ?nine.Caller = null,
/// Attr of the server's 9P root from the dial-time stat: what a
/// LOOKUP of the name answers.
root_attr: fuse.Attr = undefined,
b: *Bridge,
- /// Guards `queue`, `stopping`, `session`'s existence, and the moment a
- /// request leaves the queue for `b.cur_unique` (see `routeInterrupt`).
+ /// Guards `queue`, `stopping`, `session`'s existence, the moment a
+ /// request leaves the queue for `b.cur_unique` (see `routeInterrupt`),
+ /// and `b.int_pipe`, where the dispatcher drops a FUSE_INTERRUPT's
+ /// target unique for the worker's session to poll.
mutex: std.Io.Mutex = .init,
cond: std.Io.Condition = .init,
queue: std.ArrayList([]align(8) u8) = .empty,
@@ -1005,9 +1353,6 @@ const Mount = struct {
dead: std.atomic.Value(bool) = .init(false),
/// Draining and exiting (child gone, FUSE device gone or DESTROY).
stopping: bool = false,
- /// The dispatcher drops a FUSE_INTERRUPT's target unique in here; the
- /// worker's session polls it while a 9P reply is outstanding.
- int_pipe: [2]i32,
thread: std.Thread,
fn sockPath(m: *const Mount) [:0]const u8 {
@@ -1117,9 +1462,8 @@ const Mntgen = struct {
m.queue.deinit(mg.gpa);
// A mount that died released all of this itself (`retire`).
if (!m.dead.load(.seq_cst)) {
- _ = linux.close(m.int_pipe[0]);
- _ = linux.close(m.int_pipe[1]);
- m.b.deinit();
+ m.b.deinit(); // its workers first: they use the session
+ if (m.caller) |*c| c.deinit();
if (m.session) |*sess| sess.deinit();
}
mg.gpa.free(m.name);
@@ -1356,7 +1700,7 @@ const Mntgen = struct {
return;
}
const in_flight = m.b.cur_unique.load(.seq_cst) == target;
- if (in_flight and m.int_pipe[1] >= 0) {
+ if (in_flight and m.b.int_pipe[1] >= 0) {
// Under the mutex, because the worker closes the pipe there
// when the mount dies. The pipe is small and nonblocking; a
// dropped packet only means one interrupt missed its window
@@ -1364,13 +1708,15 @@ const Mntgen = struct {
// exit ends the session through stop_fd regardless).
var packet: [8]u8 = undefined;
std.mem.writeInt(u64, &packet, target, .little);
- _ = linux.write(m.int_pipe[1], &packet, packet.len);
+ _ = linux.write(m.b.int_pipe[1], &packet, packet.len);
}
m.mutex.unlock(mg.io);
if (in_flight) {
mg.trace(" interrupt for unique={d}: forwarded to '{s}'", .{ target, m.name });
return;
}
+ // A READ or WRITE on one of the mount's transfer workers.
+ if (m.b.interruptWorker(target)) return;
}
mg.trace(" interrupt for unique={d} (not in flight; ignored)", .{target});
}
@@ -1750,14 +2096,10 @@ const Mntgen = struct {
errdefer b.deinit();
b.data_buf = try mg.gpa.alloc(u8, max_write);
- var pipes: [2]i32 = undefined;
- if (linux.errno(linux.pipe2(&pipes, .{ .CLOEXEC = true, .NONBLOCK = true })) != .SUCCESS) {
+ if (linux.errno(linux.pipe2(&b.int_pipe, .{ .CLOEXEC = true, .NONBLOCK = true })) != .SUCCESS) {
+ b.int_pipe = .{ -1, -1 };
return error.SystemResources;
}
- errdefer {
- _ = linux.close(pipes[0]);
- _ = linux.close(pipes[1]);
- }
const m = try mg.gpa.create(Mount);
errdefer mg.gpa.destroy(m);
@@ -1773,7 +2115,6 @@ const Mntgen = struct {
.entry_name = undefined, // a slice of `name`, set below
.stop_fd = mg.stop_fd,
.b = b,
- .int_pipe = pipes,
.thread = undefined,
};
errdefer mg.gpa.free(m.name);
@@ -1921,7 +2262,7 @@ const DialError = error{ NotPosted, Stale, Busy, SystemResources, Io } || nine.S
/// no session yet to flush a request out of (`abort_on_cancel`).
fn dial(m: *Mount) DialError!void {
const fd = try connectSocket(m);
- var fresh = nine.Session.dial(m.gpa, .{ .fd = fd }, m.mo.msize, m.stop_fd) catch |e| {
+ const fresh = nine.Session.dial(m.gpa, .{ .fd = fd }, m.mo.msize, m.stop_fd) catch |e| {
_ = linux.close(fd);
// With an `.fd` address the only thing left to fail is the buffers.
return switch (e) {
@@ -1929,18 +2270,19 @@ fn dial(m: *Mount) DialError!void {
else => error.Io,
};
};
- fresh.interrupt = mountInterrupt(m);
- fresh.abort_on_cancel = true;
m.mutex.lockUncancelable(m.io);
m.session = fresh;
m.mutex.unlock(m.io);
errdefer dropSession(m);
- const sess = &m.session.?;
- m.b.nine = sess;
- try sess.version();
- _ = try sess.attach(0, m.mo.uname, m.mo.aname);
- const st = try sess.stat(0);
- sess.abort_on_cancel = false;
+ m.caller = try nine.Caller.init(&m.session.?);
+ const c = &m.caller.?;
+ c.interrupt = m.b.pipeSource();
+ c.abort_on_cancel = true;
+ m.b.nine = c;
+ try c.version();
+ _ = try c.attach(0, m.mo.uname, m.mo.aname);
+ const st = try c.stat(0);
+ c.abort_on_cancel = false;
// The kernel-visible root of this server's subtree: the mount's root
// node id, fid 0, its qid from the stat. From here on the bridge is an
@@ -1957,6 +2299,8 @@ fn dial(m: *Mount) DialError!void {
/// Closes the session and its socket; the mount is back to having none.
fn dropSession(m: *Mount) void {
m.mutex.lockUncancelable(m.io);
+ if (m.caller) |*c| c.deinit();
+ m.caller = null;
if (m.session) |*sess| sess.deinit();
m.session = null;
m.mutex.unlock(m.io);
@@ -1973,11 +2317,12 @@ fn dropSession(m: *Mount) void {
fn retire(m: *Mount) void {
m.dead.store(true, .seq_cst);
retireEntry(m);
+ m.b.stopPool(); // before the session goes: the workers use it
dropSession(m);
m.mutex.lockUncancelable(m.io);
- _ = linux.close(m.int_pipe[0]);
- _ = linux.close(m.int_pipe[1]);
- m.int_pipe = .{ -1, -1 };
+ _ = linux.close(m.b.int_pipe[0]);
+ _ = linux.close(m.b.int_pipe[1]);
+ m.b.int_pipe = .{ -1, -1 };
m.mutex.unlock(m.io);
m.b.deinit();
}
@@ -2010,14 +2355,14 @@ fn connectSocket(m: *Mount) DialError!i32 {
if (waited >= connect_grace_ms) return error.Busy;
var pfds = [_]linux.pollfd{
.{ .fd = m.stop_fd, .events = linux.POLL.IN, .revents = 0 },
- .{ .fd = m.int_pipe[0], .events = linux.POLL.IN, .revents = 0 },
+ .{ .fd = m.b.int_pipe[0], .events = linux.POLL.IN, .revents = 0 },
};
switch (linux.errno(linux.poll(&pfds, pfds.len, connect_retry_ms))) {
.SUCCESS, .INTR, .AGAIN => {},
else => return error.Io,
}
if (pfds[0].revents != 0) return error.Stopped;
- if (pfds[1].revents != 0 and try interruptHit(m)) return error.Interrupted;
+ if (pfds[1].revents != 0 and try m.b.pipeHit()) return error.Interrupted;
waited += connect_retry_ms;
}
_ = linux.fcntl(fd, linux.F.SETFL, 0);
@@ -2059,60 +2404,6 @@ fn retireEntry(m: *Mount) void {
};
}
-// -- a mount's interrupt source -------------------------------------------------
-//
-// The worker's session polls `int_pipe[0]` while a 9P reply is outstanding
-// (and `connectSocket` while it waits on a full backlog); the dispatcher
-// writes the interrupted request's unique into it. Unlike the
-// single-connection source, no FUSE reading happens here — the dispatcher
-// owns /dev/fuse.
-
-fn mountInterrupt(m: *Mount) nine.Interrupt {
- return .{ .ctx = m, .watch = mountWatch, .onReadable = mountOnReadable, .armed = mountArmed };
-}
-
-fn mountWatch(ctx: *anyopaque) i32 {
- const m: *Mount = @ptrCast(@alignCast(ctx));
- return m.int_pipe[0];
-}
-
-fn mountOnReadable(ctx: *anyopaque) nine.Session.Error!bool {
- const m: *Mount = @ptrCast(@alignCast(ctx));
- return interruptHit(m);
-}
-
-/// Drains the interrupt pipe; true when one of the packets named the
-/// request in flight, which is then marked interrupted.
-fn interruptHit(m: *Mount) nine.Session.Error!bool {
- var packet: [8]u8 = undefined;
- var hit = false;
- while (true) {
- const rc = linux.read(m.int_pipe[0], &packet, packet.len);
- switch (linux.errno(rc)) {
- .SUCCESS => {
- // Pipe writes of 8 bytes are atomic; a short read cannot happen.
- const target = std.mem.readInt(u64, &packet, .little);
- const cur = m.b.cur_unique.load(.seq_cst);
- if (cur != 0 and target == cur) hit = true;
- },
- .INTR => continue,
- .AGAIN => break, // drained
- else => return error.Io,
- }
- }
- if (hit) {
- if (m.debug) std.debug.print("9ns: [{s}] interrupt for unique={d} (in flight): cancelling\n", .{ m.name, m.b.cur_unique.load(.seq_cst) });
- m.b.interrupted = true;
- return true;
- }
- return false;
-}
-
-fn mountArmed(ctx: *anyopaque) bool {
- const m: *Mount = @ptrCast(@alignCast(ctx));
- return m.b.interrupted;
-}
-
// -- pure helpers (unit-tested) ------------------------------------------------------------
/// Attr from a 9P Stat: DMDIR → S_IFDIR else S_IFREG, low 9 permission bits kept.
@@ -2391,7 +2682,7 @@ test "interrupt source: INTERRUPT in flight → Tflush, other INTERRUPTs ignored
var fs: nine.FakeServer = .{ .fd = -1, .msize = 8192, .file_len = 50, .hang_offset = 0, .on_flush = .rflush, .on_hang_inject = &pi };
var pair = try fs.start();
defer pair.close();
- const s = &pair.session;
+ const s = pair.caller;
var b: Bridge = .{ .gpa = testing.allocator, .fuse_fd = pi.read_end, .nine = s, .opts = .{ .uid = 0, .gid = 0 } };
defer b.deinit();
b.spare_buf = try testing.allocator.alignedAlloc(u8, .@"8", request_buf_len);
@@ -2414,7 +2705,7 @@ test "interrupt source: INTERRUPT in flight → Tflush, other INTERRUPTs ignored
b.interrupted = false;
b.cur_unique.store(8, .seq_cst);
try testing.expectEqual(@as(usize, 40), try b.read(1, 10, buf[0..40]));
- try testing.expectEqual(@as(usize, 0), s.client.pending());
+ try testing.expectEqual(@as(usize, 0), s.session.client.pending());
// A FORGET arriving during a wait is copied into the stash, and the fd
// stays watched: an INTERRUPT can still arrive behind it.
@@ -2508,3 +2799,272 @@ test "mntgen: ino_xor keeps two servers' identical qid.paths distinct" {
var b0: Bridge = .{ .gpa = testing.allocator, .fuse_fd = -1, .nine = undefined, .opts = .{ .uid = 0, .gid = 0 } };
try testing.expectEqual(inoFromPath(qid.path), b0.inoOf(1, qid));
}
+
+/// The kernel's end of a fake /dev/fuse: a SOCK_SEQPACKET pair keeps one
+/// request per read and one reply per write, like the device.
+const FakeKernel = struct {
+ fd: i32,
+ buf: [8192]u8 align(8) = undefined,
+
+ const Reply = struct { unique: u64, err: i32, payload: []const u8 };
+
+ fn send(k: *FakeKernel, op: fuse.Opcode, unique: u64, nodeid: u64, parts: []const []const u8) !void {
+ var len: usize = @sizeOf(fuse.InHeader);
+ for (parts) |p| len += p.len;
+ const header = fuse.InHeader{ .len = @intCast(len), .opcode = @intFromEnum(op), .unique = unique, .nodeid = nodeid, .uid = 0, .gid = 0, .pid = 0, .total_extlen = 0, .padding = 0 };
+ var wire: [512]u8 = undefined;
+ @memcpy(wire[0..@sizeOf(fuse.InHeader)], std.mem.asBytes(&header));
+ var at: usize = @sizeOf(fuse.InHeader);
+ for (parts) |p| {
+ @memcpy(wire[at..][0..p.len], p);
+ at += p.len;
+ }
+ if (linux.write(k.fd, &wire, len) != len) return error.Io;
+ }
+
+ /// The next reply, or error.Timeout after `ms` milliseconds without one.
+ fn recv(k: *FakeKernel, ms: i32) !Reply {
+ var pfd = [_]linux.pollfd{.{ .fd = k.fd, .events = linux.POLL.IN, .revents = 0 }};
+ if (linux.poll(&pfd, 1, ms) != 1) return error.Timeout;
+ const rc = linux.read(k.fd, &k.buf, k.buf.len);
+ if (linux.errno(rc) != .SUCCESS or rc < @sizeOf(fuse.OutHeader)) return error.Io;
+ const h = std.mem.bytesToValue(fuse.OutHeader, k.buf[0..@sizeOf(fuse.OutHeader)]);
+ return .{ .unique = h.unique, .err = h.@"error", .payload = k.buf[@sizeOf(fuse.OutHeader)..rc] };
+ }
+};
+
+const ServeThread = struct {
+ session: *nine.Session,
+ fuse_fd: i32,
+ stop_fd: i32,
+ result: anyerror!void = {},
+
+ fn run(t: *ServeThread) void {
+ t.result = serve(testing.allocator, t.fuse_fd, t.session, 0, t.stop_fd, .{ .uid = 0, .gid = 0 });
+ }
+};
+
+/// `serve` on its own thread, fed through a fake /dev/fuse, against a
+/// FakeServer; `lookupOpen` does the LOOKUP + OPEN a process's open(2) does.
+const ServeRig = struct {
+ pair: nine.FakeServer.Pair,
+ dev: [2]i32,
+ stop: [2]i32,
+ st: ServeThread,
+ thread: std.Thread,
+ k: FakeKernel,
+
+ fn start(r: *ServeRig, fs: *nine.FakeServer) !void {
+ r.pair = try fs.start();
+ errdefer r.pair.close();
+ try testing.expectEqual(linux.E.SUCCESS, linux.errno(linux.socketpair(linux.AF.UNIX, linux.SOCK.SEQPACKET | linux.SOCK.CLOEXEC, 0, &r.dev)));
+ try testing.expectEqual(linux.E.SUCCESS, linux.errno(linux.pipe2(&r.stop, .{ .CLOEXEC = true })));
+ r.st = .{ .session = r.pair.session, .fuse_fd = r.dev[0], .stop_fd = r.stop[0] };
+ r.thread = try std.Thread.spawn(.{}, ServeThread.run, .{&r.st});
+ r.k = .{ .fd = r.dev[1] };
+ }
+
+ /// Whatever happened, the loop must end: DESTROY, and stop_fd for a
+ /// loop stuck in a 9P wait (which is what head-of-line blocking looks
+ /// like). Then everything is closed.
+ fn stopAll(r: *ServeRig) void {
+ r.k.send(.destroy, 999, 0, &.{}) catch {};
+ _ = linux.write(r.stop[1], "x", 1);
+ r.thread.join();
+ for (r.dev ++ r.stop) |fd| _ = linux.close(fd);
+ r.pair.close();
+ }
+
+ fn lookupOpen(r: *ServeRig, unique: u64) !struct { node: u64, fh: u64 } {
+ try r.k.send(.lookup, unique, fuse.root_id, &.{"file\x00"});
+ var rep = try r.k.recv(3000);
+ try testing.expectEqual(@as(i32, 0), rep.err);
+ const node = std.mem.bytesToValue(fuse.EntryOut, rep.payload[0..@sizeOf(fuse.EntryOut)]).nodeid;
+ try r.k.send(.open, unique + 1, node, &.{std.mem.asBytes(&fuse.OpenIn{ .flags = 0, .open_flags = 0 })});
+ rep = try r.k.recv(3000);
+ try testing.expectEqual(@as(i32, 0), rep.err);
+ return .{ .node = node, .fh = std.mem.bytesToValue(fuse.OpenOut, rep.payload[0..@sizeOf(fuse.OpenOut)]).fh };
+ }
+
+ fn read(r: *ServeRig, unique: u64, node: u64, fh: u64, offset: u64, size: u32) !void {
+ const in = fuse.ReadIn{ .fh = fh, .offset = offset, .size = size, .read_flags = 0, .lock_owner = 0, .flags = 0, .padding = 0 };
+ try r.k.send(.read, unique, node, &.{std.mem.asBytes(&in)});
+ }
+
+ fn getattr(r: *ServeRig, unique: u64, node: u64) !void {
+ try r.k.send(.getattr, unique, node, &.{std.mem.asBytes(&fuse.GetattrIn{ .getattr_flags = 0, .dummy = 0, .fh = 0 })});
+ }
+
+ fn waitHung(fs: *nine.FakeServer, n: u32) !void {
+ var waited: usize = 0;
+ while (fs.hung_now.load(.seq_cst) < n and waited < 3000) : (waited += 1) {
+ const ts: linux.timespec = .{ .sec = 0, .nsec = std.time.ns_per_ms };
+ _ = linux.nanosleep(&ts, null);
+ }
+ try testing.expect(fs.hung_now.load(.seq_cst) >= n);
+ }
+};
+
+test "serve: a parked READ holds up neither other READs nor metadata; its INTERRUPT flushes it" {
+ var fs: nine.FakeServer = .{ .fd = -1, .msize = 8192, .file_len = 50, .hang_offset = 0, .on_flush = .rflush };
+ var rig: ServeRig = undefined;
+ try rig.start(&fs);
+ defer rig.stopAll();
+ const k = &rig.k;
+ const f = try rig.lookupOpen(1);
+
+ // The READ at offset 0 parks on the server.
+ try rig.read(10, f.node, f.fh, 0, 100);
+ try ServeRig.waitHung(&fs, 1);
+
+ // Meanwhile another READ on the same file and a GETATTR are answered,
+ // promptly and in any order; the parked READ is not.
+ for (0..20) |i| {
+ const u: u64 = 100 + 2 * i;
+ try rig.read(u, f.node, f.fh, 10, 40);
+ try rig.getattr(u + 1, f.node);
+ var seen: u2 = 0;
+ for (0..2) |_| {
+ const r = try k.recv(1000);
+ try testing.expectEqual(@as(i32, 0), r.err);
+ if (r.unique == u) {
+ try testing.expectEqual(@as(usize, 40), r.payload.len);
+ try testing.expectEqual(@as(u8, 10), r.payload[0]);
+ seen |= 1;
+ } else {
+ try testing.expectEqual(u + 1, r.unique);
+ const attr = std.mem.bytesToValue(fuse.AttrOut, r.payload[0..@sizeOf(fuse.AttrOut)]);
+ try testing.expectEqual(@as(u64, 50), attr.attr.size);
+ seen |= 2;
+ }
+ }
+ try testing.expectEqual(@as(u2, 3), seen);
+ }
+ try testing.expectEqual(@as(u32, 0), fs.flushes.load(.seq_cst));
+
+ // INTERRUPT for the parked READ: exactly one Tflush, for its tag, and
+ // the READ comes back EINTR.
+ const hung_tag = fs.hung_tag.load(.seq_cst);
+ try k.send(.interrupt, 11, 0, &.{std.mem.asBytes(&fuse.InterruptIn{ .unique = 10 })});
+ var r = try k.recv(3000);
+ try testing.expectEqual(@as(u64, 10), r.unique);
+ try testing.expectEqual(-@as(i32, @intFromEnum(linux.E.INTR)), r.err);
+ try testing.expectEqual(@as(u32, 1), fs.flushes.load(.seq_cst));
+ try testing.expectEqual(hung_tag, fs.flush_oldtag.load(.seq_cst));
+
+ // RELEASE clunks the fid; the mount carries on.
+ try k.send(.release, 12, f.node, &.{std.mem.asBytes(&fuse.ReleaseIn{ .fh = f.fh, .flags = 0, .release_flags = 0, .lock_owner = 0 })});
+ r = try k.recv(3000);
+ try testing.expectEqual(@as(u64, 12), r.unique);
+ try testing.expectEqual(@as(i32, 0), r.err);
+ try rig.getattr(13, f.node);
+ r = try k.recv(3000);
+ try testing.expectEqual(@as(i32, 0), r.err);
+ try testing.expectEqual(@as(usize, 0), rig.pair.session.client.pending());
+}
+
+test "serve: one open is one fid; FLUSH clunks nothing, and a READ parked on the handle completes on its fid" {
+ // pardes keeps state per open (a pty's or run's answer, a log cursor,
+ // an event reader): two opens of one file must be two fids, and each
+ // handle's reads must go to its own.
+ var fs: nine.FakeServer = .{ .fd = -1, .msize = 8192, .file_len = 50, .hang_offset = 0, .on_flush = .rflush, .fid_in_data = true, .release_on_stat = true };
+ var rig: ServeRig = undefined;
+ try rig.start(&fs);
+ defer rig.stopAll();
+ const k = &rig.k;
+ const a = try rig.lookupOpen(1);
+ const b = try rig.lookupOpen(3);
+ try testing.expectEqual(a.node, b.node); // one inode...
+ try testing.expect(a.fh != b.fh); // ...two opens
+
+ // Each handle reads through its own fid (the server stamps it in byte 0).
+ try rig.read(10, a.node, a.fh, 10, 40);
+ var r = try k.recv(3000);
+ try testing.expectEqual(@as(i32, 0), r.err);
+ const fid_a = r.payload[0];
+ try rig.read(11, b.node, b.fh, 10, 40);
+ r = try k.recv(3000);
+ try testing.expectEqual(@as(i32, 0), r.err);
+ const fid_b = r.payload[0];
+ try testing.expect(fid_a != fid_b);
+
+ // A read parks on `a`; `b` keeps answering from its own fid meanwhile,
+ // concurrently, as two processes would see it.
+ try rig.read(20, a.node, a.fh, 0, 100);
+ try ServeRig.waitHung(&fs, 1);
+ for (0..5) |i| {
+ try rig.read(30 + i, b.node, b.fh, 10, 40);
+ r = try k.recv(1000);
+ try testing.expectEqual(30 + i, r.unique);
+ try testing.expectEqual(fid_b, r.payload[0]);
+ }
+
+ // close(2) of `a` in another thread sends FLUSH while the read is still
+ // parked: it is answered and clunks nothing.
+ const clunks = fs.clunks.load(.seq_cst);
+ try k.send(.flush, 21, a.node, &.{std.mem.asBytes(&fuse.FlushIn{ .fh = a.fh, .unused = 0, .padding = 0, .lock_owner = 0 })});
+ r = try k.recv(3000);
+ try testing.expectEqual(@as(u64, 21), r.unique);
+ try testing.expectEqual(@as(i32, 0), r.err);
+ try testing.expectEqual(clunks, fs.clunks.load(.seq_cst));
+ try testing.expectEqual(@as(u32, 1), fs.hung_now.load(.seq_cst));
+
+ // The server answers the parked read (release_on_stat, triggered by a
+ // GETATTR): it arrives on `a`'s fid, which was never clunked.
+ try rig.getattr(22, a.node);
+ var seen: u2 = 0;
+ for (0..2) |_| {
+ r = try k.recv(3000);
+ try testing.expectEqual(@as(i32, 0), r.err);
+ if (r.unique == 20) {
+ try testing.expectEqual(@as(usize, 50), r.payload.len);
+ try testing.expectEqual(fid_a, r.payload[0]);
+ seen |= 1;
+ } else {
+ try testing.expectEqual(@as(u64, 22), r.unique);
+ seen |= 2;
+ }
+ }
+ try testing.expectEqual(@as(u2, 3), seen);
+
+ // Only RELEASE clunks: one per open.
+ for ([_]u64{ a.fh, b.fh }, 0..) |fh, i| {
+ try k.send(.release, 40 + i, a.node, &.{std.mem.asBytes(&fuse.ReleaseIn{ .fh = fh, .flags = 0, .release_flags = 0, .lock_owner = 0 })});
+ r = try k.recv(3000);
+ try testing.expectEqual(@as(i32, 0), r.err);
+ }
+ try testing.expectEqual(clunks + 2, fs.clunks.load(.seq_cst));
+}
+
+test "serve: a flushed READ whose reply beats the Rflush returns that reply, not EINTR (flush(5))" {
+ var fs: nine.FakeServer = .{ .fd = -1, .msize = 8192, .file_len = 50, .hang_offset = 0, .on_flush = .reply_then_rflush };
+ var rig: ServeRig = undefined;
+ try rig.start(&fs);
+ defer rig.stopAll();
+ const k = &rig.k;
+ const f = try rig.lookupOpen(1);
+ try rig.read(10, f.node, f.fh, 0, 100);
+ try ServeRig.waitHung(&fs, 1);
+
+ // The server answers the Tflush with the Rread first, then the Rflush:
+ // the read completed on the server, so its data is the answer.
+ const hung_tag = fs.hung_tag.load(.seq_cst);
+ try k.send(.interrupt, 11, 0, &.{std.mem.asBytes(&fuse.InterruptIn{ .unique = 10 })});
+ var r = try k.recv(3000);
+ try testing.expectEqual(@as(u64, 10), r.unique);
+ try testing.expectEqual(@as(i32, 0), r.err);
+ try testing.expectEqual(@as(usize, 50), r.payload.len);
+ for (r.payload, 0..) |byte, i| try testing.expectEqual(@as(u8, @truncate(i)), byte);
+ try testing.expectEqual(@as(u32, 1), fs.flushes.load(.seq_cst));
+ try testing.expectEqual(hung_tag, fs.flush_oldtag.load(.seq_cst));
+
+ // The late Rflush is dropped by whoever reads next; nothing is left
+ // pending and the mount carries on.
+ try rig.getattr(12, f.node);
+ r = try k.recv(3000);
+ try testing.expectEqual(@as(i32, 0), r.err);
+ try rig.read(13, f.node, f.fh, 10, 40);
+ r = try k.recv(3000);
+ try testing.expectEqual(@as(usize, 40), r.payload.len);
+ try testing.expectEqual(@as(usize, 0), rig.pair.session.client.pending());
+}
diff --git a/9ns/src/main.zig b/9ns/src/main.zig
index 386552f..eac0a5e 100644
--- a/9ns/src/main.zig
+++ b/9ns/src/main.zig
@@ -480,13 +480,20 @@ pub fn main(init: std.process.Init) !u8 {
defer session.deinit();
defer stopServer(server);
- _ = session.attach(0, uname, cfg.aname) catch |err| {
- switch (err) {
- error.Nine => std.debug.print("9ns: attach (uname={s}, aname='{s}'): {s}\n", .{ uname, cfg.aname, session.ename[0..session.ename_len] }),
- else => std.debug.print("9ns: attach: {t}\n", .{err}),
- }
- return own_failure;
- };
+ {
+ var caller = nine.Caller.init(&session) catch |err| {
+ std.debug.print("9ns: attach: {t}\n", .{err});
+ return own_failure;
+ };
+ defer caller.deinit();
+ _ = caller.attach(0, uname, cfg.aname) catch |err| {
+ switch (err) {
+ error.Nine => std.debug.print("9ns: attach (uname={s}, aname='{s}'): {s}\n", .{ uname, cfg.aname, caller.ename[0..caller.ename_len] }),
+ else => std.debug.print("9ns: attach: {t}\n", .{err}),
+ }
+ return own_failure;
+ };
+ }
if (cfg.debug) std.debug.print("9ns: attached to {s} (msize {d}), mounting on {s}\n", .{ describeAddress(address, &addr_buf), session.msize, mountpoint });
var child_pid: i32 = 0;
diff --git a/9ns/src/nine.zig b/9ns/src/nine.zig
index 6f1fe09..52a45ae 100644
--- a/9ns/src/nine.zig
+++ b/9ns/src/nine.zig
@@ -1,8 +1,18 @@
-//! Synchronous 9P2000 session over a blocking file descriptor.
+//! 9P2000 session over a blocking file descriptor, shared by several threads.
+//!
+//! A thin RPC layer over `cloud9.Client` (push/take, allocation-free). Many
+//! requests may be outstanding at once, each under its own tag, and their
+//! replies may come back in any order: a read the server parks (a blocking
+//! `log` or `event` file) holds up only the thread that asked for it.
+//!
+//! The multiplexing follows Plan 9's mount driver (`mountio`, `mountmux` and
+//! `mntgate` in kernel/devices/sys/devmnt.c). Each thread that makes calls
+//! owns a `Caller`. A call writes its T-message and then waits; the first
+//! waiter to find nobody reading the socket takes the reading baton and
+//! reads for everyone, handing each R-message to the waiter its tag names;
+//! when its own reply has come in it passes the baton on. There is no reader
+//! thread and no lock held across a blocking read.
//!
-//! A thin RPC layer over `cloud9.Client` (push/take, allocation-free). One request
-//! is outstanding at a time: the FUSE loop that drives this is single-threaded, so
-//! every call here blocks until its reply (or the connection's death) arrives.
//! Fids are handed out from a free list; fid 0 is reserved for the root.
const std = @import("std");
const cloud9 = @import("cloud9");
@@ -29,8 +39,29 @@ pub const dontcare = cloud9.Stat{
.muid = "",
};
-/// How the owner of a session (the FUSE bridge) gets a say while an rpc waits
-/// for its reply. `watch` names a descriptor to poll alongside the socket, or
+/// Requests one session keeps in flight at once (one more tag is kept for a
+/// Tflush). Every parked read a mount serves holds one, so the window is
+/// twice the 16 of cloud9's default client: 32, which is what cloud9's
+/// file server engine parks per connection (`fs.Options.slot_capacity`,
+/// the engine pardes serves with). A mount can therefore never have more
+/// requests parked on such a server than it will park, and never meets its
+/// refusal (Rerror "Resource temporarily unavailable" for the one past the
+/// cap). Raising this past 32 means raising the server's `slot_capacity`
+/// with it (for pardes, its editor `fs.Options`).
+pub const max_tags: usize = 32;
+pub const Client = cloud9.ClientWith(max_tags);
+
+/// The `Io` the locks here (and the bridge's) hand to `std.Io.Mutex` and
+/// `std.Io.Condition`. They only use it for futex waits and wakes, and the
+/// uncancelable paths used throughout ignore the instance and go straight to
+/// the futex syscall, which is thread-safe; no `Io` is threaded through the
+/// FUSE loop, so this one stands in.
+pub fn lockIo() std.Io {
+ return std.Io.Threaded.global_single_threaded.io();
+}
+
+/// How the owner of a caller (the FUSE bridge) gets a say while its rpc waits
+/// for a reply. `watch` names a descriptor to poll alongside the socket, or
/// -1 to poll nothing extra right now; when it becomes readable `onReadable`
/// consumes whatever is there and returns true if the request in flight should
/// be cancelled with a Tflush. `armed` reports whether such a cancellation was
@@ -44,6 +75,8 @@ pub const Interrupt = struct {
armed: *const fn (ctx: *anyopaque) bool,
};
+/// One connection: the socket, the protocol state, the fids, and the table
+/// of calls waiting for replies. Calls are made through a `Caller`.
pub const Session = struct {
pub const Error = error{ Nine, Protocol, Io, Closed, Stopped, Interrupted, TooLarge, OutOfMemory };
@@ -52,39 +85,52 @@ pub const Session = struct {
gpa: std.mem.Allocator,
fd: i32,
- client: cloud9.Client,
+ client: Client,
in_buf: []u8,
out_buf: []u8,
- /// After `error.Nine`, the server's Rerror text (copied, bounded).
- ename: [256]u8 = undefined,
- ename_len: usize = 0,
/// Negotiated maximum message size.
msize: u32,
next_fid: u32 = 1,
free_fids: std.ArrayList(u32) = .empty,
/// Per-fid iounit learned from open/create (0 = none); used to chunk read/write.
iounits: std.AutoHashMapUnmanaged(u32, u32) = .empty,
- /// Optional descriptor watched while waiting for a reply: when it becomes
- /// readable (the bridge's "child exited" pipe) the pending rpc fails with
+ /// Optional descriptor every waiting call watches: when it becomes
+ /// readable (the bridge's "child exited" pipe) the call fails with
/// `error.Stopped` instead of blocking on a server that never answers.
stop_fd: i32 = -1,
- /// Optional interrupt source (the bridge's FUSE descriptor) consulted while
- /// a reply is outstanding; see `Interrupt`.
- interrupt: ?Interrupt = null,
- /// While set, a cancellation ends the rpc at once with `error.Interrupted`
- /// and wedges the session, with no Tflush: for the handshake (version,
- /// attach, the root stat), where there is no session yet to flush a
- /// request out of, and the honest answer to "stop waiting" is to hang up.
- abort_on_cancel: bool = false,
/// Milliseconds a Tflush may go unanswered before the server is declared
- /// wedged: the rpc fails with `error.Interrupted` and the session with it.
+ /// wedged: the call fails with `error.Interrupted` and the session with it.
/// The protocol says a client waits for the Rflush; a server that has not
/// managed one in this long is not going to, and the process behind the
/// interrupt is unkillable until we stop waiting. 0 waits forever.
flush_grace_ms: i32 = 3000,
/// The server is gone as far as this session is concerned (see
- /// `abort_on_cancel`, `flush_grace_ms`); every rpc answers `error.Closed`.
+ /// `Caller.abort_on_cancel`, `flush_grace_ms`); every call answers
+ /// `error.Closed`, the ones already waiting included.
wedged: bool = false,
+ /// The connection itself failed (EOF, a socket error, a reply that
+ /// breaks the protocol): every call answers this.
+ dead: ?Error = null,
+
+ // -- multiplexing (devmnt's Mnt: `queue`, `rip`) ------------------------------------
+
+ /// Guards the client's tags and input side, the tables below, the fid
+ /// tables, `wedged` and `dead`. Never held across a blocking syscall.
+ mutex: std.Io.Mutex = .init,
+ /// Serializes transmission: the client's output buffer and the write side
+ /// of the socket. Taken before `mutex`, and never by the baton holder
+ /// (see `Waiter.cancel`), so a write that blocks because the server is
+ /// itself blocked writing replies cannot stop those replies being read.
+ wlock: std.Io.Mutex = .init,
+ /// The call waiting on each tag. A call that sent a Tflush sits under
+ /// both tags until one of them answers.
+ waiting: [max_tags + 1]?*Waiter = @splat(null),
+ /// The Tversion's waiter (it travels under NOTAG).
+ versioning: ?*Waiter = null,
+ /// Calls that found every tag taken, waiting for a reply to free one.
+ starved: ?*Waiter = null,
+ /// The waiter holding the reading baton, if any (devmnt's `m->rip`).
+ reader: ?*Waiter = null,
/// Connect to `address`, then negotiate the protocol version.
/// `msize` is the maximum message size to ask for (0 = the buffers' size).
@@ -99,7 +145,7 @@ pub const Session = struct {
/// returned session. An `.fd` address is the caller's to close on failure.
pub fn connectWatched(gpa: std.mem.Allocator, address: Address, msize: u32, stop_fd: i32) !Session {
var s = try dial(gpa, address, msize, stop_fd);
- s.version() catch |e| {
+ negotiate(&s) catch |e| {
s.freeBuffers();
if (address != .fd) _ = linux.close(s.fd);
return e;
@@ -107,6 +153,12 @@ pub const Session = struct {
return s;
}
+ fn negotiate(s: *Session) !void {
+ var c = try Caller.init(s);
+ defer c.deinit();
+ try c.version();
+ }
+
/// The transport and the buffers, no handshake: for a caller that wants
/// its interrupt source in place before `version()` (9ns's mntgen
/// worker, so a walk interrupted mid-dial can abandon the dial). Owns
@@ -132,13 +184,6 @@ pub const Session = struct {
};
}
- /// Negotiates the protocol version with the msize `dial` was given.
- pub fn version(s: *Session) Error!void {
- const r = try s.rpc(.{ .version = .{ .msize = s.msize } });
- if (!std.mem.eql(u8, r.version.version, "9P2000")) return error.Protocol;
- s.msize = r.version.msize;
- }
-
fn freeBuffers(s: *Session) void {
s.free_fids.deinit(s.gpa);
s.iounits.deinit(s.gpa);
@@ -147,19 +192,18 @@ pub const Session = struct {
}
/// Closes the descriptor and frees the buffers. Fids are not clunked.
+ /// Every `Caller` must be done with the session (and is best deinit first).
pub fn deinit(s: *Session) void {
_ = linux.close(s.fd);
s.freeBuffers();
s.* = undefined;
}
- pub fn attach(s: *Session, fid: u32, uname: []const u8, aname: []const u8) Error!cloud9.Qid {
- const r = try s.rpc(.{ .attach = .{ .fid = fid, .uname = uname, .aname = aname } });
- return r.attach;
- }
-
/// Fid 0 is never handed out: it belongs to the root attach.
pub fn allocFid(s: *Session) u32 {
+ const io = lockIo();
+ s.mutex.lockUncancelable(io);
+ defer s.mutex.unlock(io);
if (s.free_fids.pop()) |fid| return fid;
const fid = s.next_fid;
s.next_fid += 1;
@@ -167,179 +211,504 @@ pub const Session = struct {
}
/// Fids currently bound (excluding fid 0); a debugging aid for leak hunting.
- pub fn fidsInUse(s: *const Session) usize {
+ pub fn fidsInUse(s: *Session) usize {
+ const io = lockIo();
+ s.mutex.lockUncancelable(io);
+ defer s.mutex.unlock(io);
return (s.next_fid - 1) - s.free_fids.items.len;
}
pub fn freeFid(s: *Session, fid: u32) void {
+ const io = lockIo();
+ s.mutex.lockUncancelable(io);
+ defer s.mutex.unlock(io);
_ = s.iounits.remove(fid);
// If the free list cannot grow the fid is simply leaked; the counter keeps going.
s.free_fids.append(s.gpa, fid) catch {};
}
- /// Generic RPC. Result slices borrow the input buffer until the next call.
+ /// Whether the session was declared gone (`wedged`) or broke (`dead`):
+ /// nothing more will come of it.
+ pub fn over(s: *Session) bool {
+ const io = lockIo();
+ s.mutex.lockUncancelable(io);
+ defer s.mutex.unlock(io);
+ return s.wedged or s.dead != null;
+ }
+
+ // -- internals: every function below runs with `mutex` held -------------------------
+
+ /// Wakes a caller from its poll: its reply is in, the baton is free, a
+ /// tag was freed, or the session is over. Spurious pokes are harmless
+ /// (the waiter rechecks under the mutex), so the counter is never trusted.
+ fn poke(c: *Caller) void {
+ const one: u64 = 1;
+ _ = linux.write(c.wake, std.mem.asBytes(&one), 8);
+ }
+
+ /// Wakes every waiting call: the baton is free (devmnt's `mntgate`, which
+ /// wakes one; here all of them recheck, so a waiter that turns out to be
+ /// done or still writing its request cannot swallow the handoff), or the
+ /// session is over.
+ /// `me` is the caller doing it, which needs no poke (and a stale one
+ /// would only cost it a spurious wake-up later).
+ fn pokeAll(s: *Session, me: *Caller) void {
+ for (s.waiting) |slot| if (slot) |w| if (w.caller != me) poke(w.caller);
+ if (s.versioning) |w| if (w.caller != me) poke(w.caller);
+ var it = s.starved;
+ while (it) |w| : (it = w.next_starved) if (w.caller != me) poke(w.caller);
+ }
+
+ fn die(s: *Session, e: Error, me: *Caller) void {
+ if (s.dead == null) s.dead = e;
+ s.pokeAll(me);
+ }
+
+ fn wedge(s: *Session, me: *Caller) void {
+ s.wedged = true;
+ s.pokeAll(me);
+ }
+
+ /// Takes every complete R-message out of the client and hands each to the
+ /// call its tag names (devmnt's `mountmux`). The reply is copied into the
+ /// caller's own buffer: the client's input buffer belongs to whoever reads
+ /// next. A reply nobody waits for any more (its call gave up, or a Rflush
+ /// whose request was answered first) is dropped.
+ fn deliver(s: *Session, me: *Caller) void {
+ var freed = false;
+ while (s.client.take()) |d| {
+ freed = true;
+ const frame = s.client.in[0..s.client.frame];
+ if (d.op == .version) {
+ const w = s.versioning orelse continue;
+ s.versioning = null;
+ w.result = copyResult(d.result, frame, w.caller.reply);
+ w.done = true;
+ if (w.caller != me) poke(w.caller);
+ continue;
+ }
+ const w = s.waiting[d.tag] orelse continue;
+ s.waiting[d.tag] = null;
+ if (d.op == .flush) {
+ // The Rflush came first: the server dropped the request, and
+ // the client has released its tag with this reply.
+ w.flush_tag = null;
+ if (s.waiting[w.tag] == w) s.waiting[w.tag] = null;
+ if (!w.done) {
+ w.done = true;
+ w.flushed = true;
+ }
+ } else {
+ w.result = copyResult(d.result, frame, w.caller.reply);
+ w.done = true;
+ }
+ if (w.caller != me) poke(w.caller);
+ }
+ if (s.client.dead) s.die(error.Protocol, me);
+ if (freed) {
+ // A reply freed a tag: every starved call tries again.
+ while (s.starved) |w| {
+ s.starved = w.next_starved;
+ w.next_starved = null;
+ w.on_starved = false;
+ w.tag_freed = true;
+ if (w.caller != me) poke(w.caller);
+ }
+ }
+ }
+
+ /// Takes a waiter out of every table, and passes the baton on if it held it.
+ fn leave(s: *Session, w: *Waiter) void {
+ for (&s.waiting) |*slot| {
+ if (slot.* == w) slot.* = null;
+ }
+ if (s.versioning == w) s.versioning = null;
+ if (w.on_starved) {
+ var link = &s.starved;
+ while (link.*) |it| : (link = &it.next_starved) {
+ if (it == w) {
+ link.* = w.next_starved;
+ break;
+ }
+ }
+ w.on_starved = false;
+ w.next_starved = null;
+ }
+ if (s.reader == w) {
+ s.reader = null;
+ s.pokeAll(w.caller);
+ }
+ }
+
+ fn starve(s: *Session, w: *Waiter) void {
+ if (w.on_starved) return;
+ w.tag_freed = false;
+ w.on_starved = true;
+ w.next_starved = s.starved;
+ s.starved = w;
+ }
+
+ /// Claims a tag for `req`, registers `w` under it and writes the message.
+ /// When every tag is taken the call waits (reading, if it gets the baton)
+ /// until a reply frees one; a cancellation meanwhile ends it with
+ /// `error.Interrupted`, nothing having been sent.
+ fn transmit(s: *Session, w: *Waiter, req: cloud9.Client.Request) Error!void {
+ const io = lockIo();
+ const c = w.caller;
+ while (true) {
+ if (s.wedged) return error.Closed;
+ if (s.dead) |e| return e;
+ // Lock order: wlock, then mutex.
+ s.mutex.unlock(io);
+ s.wlock.lockUncancelable(io);
+ s.mutex.lockUncancelable(io);
+ const tag = s.client.submit(req) catch |e| {
+ s.wlock.unlock(io);
+ switch (e) {
+ error.NoTags => {
+ s.starve(w);
+ try s.await(w, .tag);
+ continue;
+ },
+ error.Handshake, error.Dead => return error.Protocol,
+ error.NoSpace, error.TooLarge => return error.TooLarge,
+ error.BadRequest => {
+ c.setEname("bad request");
+ return error.Nine;
+ },
+ }
+ };
+ w.tag = tag;
+ if (req == .version) s.versioning = w else s.waiting[tag] = w;
+ try s.send(c);
+ return;
+ }
+ }
+
+ /// Writes out what `submit` left in the client's output buffer. Entered
+ /// with `mutex` and `wlock` held; returns with `mutex` held and `wlock`
+ /// released. The socket write happens with `mutex` released, so the
+ /// reader keeps reading while it blocks.
+ fn send(s: *Session, c: *Caller) Error!void {
+ const io = lockIo();
+ s.mutex.unlock(io);
+ const sent = writeOut(s);
+ s.wlock.unlock(io);
+ s.mutex.lockUncancelable(io);
+ sent catch |e| {
+ s.die(e, c);
+ return e;
+ };
+ }
+
+ const Until = enum { reply, tag };
+
+ /// Waits until `w` is answered (`.reply`: its reply or its Rflush is in)
+ /// or a tag was freed (`.tag`). Takes the reading baton whenever nobody
+ /// holds it (devmnt's `mountio` gate), reads for everyone while holding
+ /// it, and lets go of it on the way out.
///
- /// While the reply is outstanding the socket is polled together with
- /// `stop_fd` (→ `error.Stopped`) and the interrupt source's descriptor. When
- /// the latter asks for a cancellation a Tflush for the request's tag goes out
- /// and the wait continues until either the original reply arrives (the flush
- /// lost the race; the result is returned as if nothing happened and the
- /// Rflush is swallowed by a later call) or the Rflush does (→
- /// `error.Interrupted`; the server has dropped the request), or neither
- /// within `flush_grace_ms` (→ `error.Interrupted`, and the session is
- /// wedged: the server stopped talking). Under `abort_on_cancel` the
- /// cancellation itself wedges the session, with nothing sent.
- pub fn rpc(s: *Session, req: cloud9.Client.Request) Error!cloud9.Client.Result {
- if (s.wedged) return error.Closed;
- s.ename_len = 0;
- const tag = s.client.submit(req) catch |e| switch (e) {
- error.NoTags, error.Handshake, error.Dead => return error.Protocol,
- error.NoSpace, error.TooLarge => return error.TooLarge,
- error.BadRequest => {
- s.setEname("bad request");
- return error.Nine;
- },
+ /// While waiting it polls, besides its own wake-up descriptor, `stop_fd`
+ /// (→ `error.Stopped`) and the caller's interrupt source. When the latter
+ /// asks for a cancellation a Tflush for the request's tag goes out and the
+ /// wait continues until either the original reply arrives (the flush lost
+ /// the race: it is honoured, and its result returned, as flush(5) says;
+ /// the later Rflush is dropped) or the Rflush does (the call ends in
+ /// `error.Interrupted`), or neither within `flush_grace_ms` (→
+ /// `error.Interrupted`, and the session is wedged: the server stopped
+ /// talking). Under `abort_on_cancel` the cancellation itself wedges the
+ /// session, with nothing sent.
+ fn await(s: *Session, w: *Waiter, until: Until) Error!void {
+ const io = lockIo();
+ const c = w.caller;
+ defer if (s.reader == w) {
+ s.reader = null;
+ s.pokeAll(c);
};
- try s.flush();
- var flush_tag: ?u16 = null;
- // Monotonic ms by which the Tflush must have been answered.
+ var cancel = false;
var flush_deadline: ?i64 = null;
var tmp: [64 * 1024]u8 = undefined;
while (true) {
- while (s.client.take()) |done| {
- if (done.tag == tag) {
- switch (done.result) {
- .fail => |ename| {
- s.setEname(ename);
- return error.Nine;
- },
- else => return done.result,
- }
+ switch (until) {
+ .reply => if (w.done) return,
+ .tag => if (w.tag_freed) return,
+ }
+ if (s.wedged) return error.Closed;
+ if (s.dead) |e| return e;
+ // (On the starved list the Tflush waits for a reply to free a tag.)
+ if (cancel and w.flush_tag == null and !w.on_starved and until == .reply) {
+ // devmnt's `mntflushalloc`: the flush is an rpc of its own.
+ // The baton goes first, so a write that blocks cannot stop
+ // everyone's replies (the Rflush among them) being read.
+ if (s.reader == w) {
+ s.reader = null;
+ s.pokeAll(c);
}
- if (flush_tag != null and done.tag == flush_tag.?) return error.Interrupted;
- // An Rflush for a flush whose original reply won the race in an
- // earlier call: the client has released both tags; nothing to do.
- if (done.op == .flush) continue;
- return error.Protocol;
+ try s.sendFlush(w);
+ continue;
}
- if (s.client.dead) return error.Protocol;
- // After take() returned null the previous frame is gone, so the free
- // space is at least what the pending frame still needs.
+ if (s.reader == null) s.reader = w;
+ const reading = s.reader == w;
+ // Only the baton holder touches the input side, so `room` holds
+ // across the unlocked read. After take() returned null the frame
+ // in progress still fits (a frame is at most msize bytes).
const room = s.client.in.len - s.client.in_len;
- if (room == 0) return error.Protocol;
+ if (reading and room == 0) {
+ s.die(error.Protocol, c);
+ continue;
+ }
const grace: i32 = if (flush_deadline) |d| @intCast(@max(d - nowMs(), 0)) else -1;
- switch (try s.wait(grace)) {
+ s.mutex.unlock(io);
+ var got: usize = 0;
+ const ready = c.wait(if (reading) s.fd else -1, grace);
+ const read_err: ?Error = if (ready) |r| (if (r == .socket) blk: {
+ got = readSocket(s.fd, tmp[0..@min(room, tmp.len)]) catch |e| break :blk e;
+ break :blk null;
+ } else null) else |_| null;
+ s.mutex.lockUncancelable(io);
+ switch (try ready) {
.socket => {
- const n = try readSocket(s.fd, tmp[0..@min(room, tmp.len)]);
- if (n == 0) return error.Closed;
- const pushed = s.client.push(tmp[0..n]);
- if (pushed != n) return error.Protocol;
+ if (read_err) |e| {
+ s.die(e, c);
+ continue;
+ }
+ if (got == 0) {
+ s.die(error.Closed, c);
+ continue;
+ }
+ if (s.client.push(tmp[0..got]) != got) {
+ s.die(error.Protocol, c);
+ continue;
+ }
+ s.deliver(c);
},
+ .wake => {},
.cancel => {
- if (s.abort_on_cancel) {
- s.wedged = true;
+ if (c.abort_on_cancel) {
+ s.wedge(c);
return error.Interrupted;
}
- if (flush_tag == null) {
- flush_tag = s.client.submit(.{ .flush = .{ .oldtag = tag } }) catch return error.Protocol;
- try s.flush();
+ // Nothing on the wire yet: nothing to flush.
+ if (until == .tag) return error.Interrupted;
+ if (!cancel) {
+ cancel = true;
if (s.flush_grace_ms != 0) flush_deadline = nowMs() + s.flush_grace_ms;
}
},
.timeout => {
- s.wedged = true;
+ s.wedge(c);
return error.Interrupted;
},
}
}
}
+ /// Sends the Tflush for `w`'s request, unless its reply beat us to it.
+ /// With no tag free it waits on the starved list: the next reply frees
+ /// one (the client keeps a tag aside for flushes, but several calls may
+ /// be flushing at once).
+ fn sendFlush(s: *Session, w: *Waiter) Error!void {
+ const io = lockIo();
+ s.mutex.unlock(io);
+ s.wlock.lockUncancelable(io);
+ s.mutex.lockUncancelable(io);
+ if (w.done) {
+ s.wlock.unlock(io);
+ return;
+ }
+ const tag = s.client.submit(.{ .flush = .{ .oldtag = w.tag } }) catch |e| {
+ s.wlock.unlock(io);
+ if (e != error.NoTags) return error.Protocol;
+ s.starve(w);
+ return;
+ };
+ w.flush_tag = tag;
+ s.waiting[tag] = w;
+ try s.send(w.caller);
+ }
+};
+
+/// One call in flight (devmnt's Mntrpc): lives on the calling thread's stack
+/// for the length of the rpc.
+const Waiter = struct {
+ caller: *Caller,
+ tag: u16 = cloud9.notag,
+ /// The Tflush sent for this call, until its Rflush (or the reply) is in.
+ flush_tag: ?u16 = null,
+ /// The reply (or, after a Tflush, the Rflush) came in.
+ done: bool = false,
+ /// It was the Rflush: the server dropped the request.
+ flushed: bool = false,
+ result: cloud9.Client.Result = undefined,
+ /// Starved-list link, and the flag a freed tag sets.
+ on_starved: bool = false,
+ next_starved: ?*Waiter = null,
+ tag_freed: bool = false,
+};
+
+/// One thread's side of a session: where its replies land, the descriptor
+/// that wakes it, its interrupt source and its last error. Every thread
+/// that makes calls on a session has its own; a Caller is used by one
+/// thread at a time.
+pub const Caller = struct {
+ session: *Session,
+ /// The reply to the latest call, copied out of the session's input
+ /// buffer (msize bytes): result slices borrow it until the next call.
+ reply: []u8,
+ /// eventfd poked by whoever has something for this caller.
+ wake: i32,
+ /// After `error.Nine`, the server's Rerror text (copied, bounded).
+ ename: [256]u8 = undefined,
+ ename_len: usize = 0,
+ /// Optional interrupt source consulted while a reply is outstanding;
+ /// see `Interrupt`.
+ interrupt: ?Interrupt = null,
+ /// While set, a cancellation ends the rpc at once with `error.Interrupted`
+ /// and wedges the session, with no Tflush: for the handshake (version,
+ /// attach, the root stat), where there is no session yet to flush a
+ /// request out of, and the honest answer to "stop waiting" is to hang up.
+ abort_on_cancel: bool = false,
+
+ pub fn init(s: *Session) !Caller {
+ const rc = linux.eventfd(0, linux.EFD.CLOEXEC | linux.EFD.NONBLOCK);
+ if (linux.errno(rc) != .SUCCESS) return error.SystemResources;
+ const wake: i32 = @intCast(rc);
+ errdefer _ = linux.close(wake);
+ return .{ .session = s, .reply = try s.gpa.alloc(u8, s.in_buf.len), .wake = wake };
+ }
+
+ pub fn deinit(c: *Caller) void {
+ c.session.gpa.free(c.reply);
+ _ = linux.close(c.wake);
+ c.* = undefined;
+ }
+
+ /// Generic RPC. Result slices borrow the caller's reply buffer until its
+ /// next call. See `Session.await` for how the wait ends.
+ pub fn rpc(c: *Caller, req: cloud9.Client.Request) Session.Error!cloud9.Client.Result {
+ const s = c.session;
+ const io = lockIo();
+ c.ename_len = 0;
+ var w: Waiter = .{ .caller = c };
+ s.mutex.lockUncancelable(io);
+ defer s.mutex.unlock(io);
+ defer s.leave(&w);
+ try s.transmit(&w, req);
+ try s.await(&w, .reply);
+ if (w.flushed) return error.Interrupted;
+ switch (w.result) {
+ .fail => |ename| {
+ c.setEname(ename);
+ return error.Nine;
+ },
+ else => return w.result,
+ }
+ }
+
+ /// Negotiates the protocol version with the msize `dial` was given.
+ pub fn version(c: *Caller) Session.Error!void {
+ const s = c.session;
+ const r = try c.rpc(.{ .version = .{ .msize = s.msize } });
+ if (!std.mem.eql(u8, r.version.version, "9P2000")) return error.Protocol;
+ s.msize = r.version.msize;
+ }
+
+ pub fn attach(c: *Caller, fid: u32, uname: []const u8, aname: []const u8) Session.Error!cloud9.Qid {
+ const r = try c.rpc(.{ .attach = .{ .fid = fid, .uname = uname, .aname = aname } });
+ return r.attach;
+ }
+
/// True when the interrupt source has asked for the operation in progress to
/// stop; consulted between the chunks of a read or write.
- pub fn interruptArmed(s: *const Session) bool {
- const i = s.interrupt orelse return false;
+ pub fn interruptArmed(c: *const Caller) bool {
+ const i = c.interrupt orelse return false;
return i.armed(i.ctx);
}
/// Walk `names` from `fid` to `newfid`. A partial walk leaves `newfid` unbound
/// (9P semantics) and reports `error.Nine` with ename "file does not exist".
- pub fn walk(s: *Session, fid: u32, newfid: u32, names: []const []const u8) Error!Walk {
- const r = try s.rpc(.{ .walk = .{ .fid = fid, .newfid = newfid, .names = names } });
+ pub fn walk(c: *Caller, fid: u32, newfid: u32, names: []const []const u8) Session.Error!Session.Walk {
+ const r = try c.rpc(.{ .walk = .{ .fid = fid, .newfid = newfid, .names = names } });
if (r.walk.nwqid < names.len) {
- s.setEname("file does not exist");
+ c.setEname("file does not exist");
return error.Nine;
}
return .{ .nwqid = r.walk.nwqid, .wqid = r.walk.wqid };
}
/// allocFid + zero-element walk. The fid is released again on failure.
- pub fn clone(s: *Session, fid: u32) Error!u32 {
- const newfid = s.allocFid();
- errdefer s.freeFid(newfid);
- _ = try s.walk(fid, newfid, &.{});
+ pub fn clone(c: *Caller, fid: u32) Session.Error!u32 {
+ const newfid = c.session.allocFid();
+ errdefer c.session.freeFid(newfid);
+ _ = try c.walk(fid, newfid, &.{});
return newfid;
}
- pub fn open(s: *Session, fid: u32, mode: u8) Error!Open {
- const r = try s.rpc(.{ .open = .{ .fid = fid, .mode = mode } });
- s.noteIounit(fid, r.open.iounit);
+ pub fn open(c: *Caller, fid: u32, mode: u8) Session.Error!Session.Open {
+ const r = try c.rpc(.{ .open = .{ .fid = fid, .mode = mode } });
+ c.noteIounit(fid, r.open.iounit);
return .{ .qid = r.open.qid, .iounit = r.open.iounit };
}
- pub fn create(s: *Session, fid: u32, name: []const u8, perm: u32, mode: u8) Error!Open {
- const r = try s.rpc(.{ .create = .{ .fid = fid, .name = name, .perm = perm, .mode = mode } });
- s.noteIounit(fid, r.create.iounit);
+ pub fn create(c: *Caller, fid: u32, name: []const u8, perm: u32, mode: u8) Session.Error!Session.Open {
+ const r = try c.rpc(.{ .create = .{ .fid = fid, .name = name, .perm = perm, .mode = mode } });
+ c.noteIounit(fid, r.create.iounit);
return .{ .qid = r.create.qid, .iounit = r.create.iounit };
}
/// Reads into `buf`, chunking by min(maxRead, iounit) and stopping at the first
/// short read. Returns the number of bytes read (0 at end of file).
- pub fn read(s: *Session, fid: u32, offset: u64, buf: []u8) Error!usize {
- return readWith(s, rpc, fid, offset, buf, s.chunk(fid));
+ pub fn read(c: *Caller, fid: u32, offset: u64, buf: []u8) Session.Error!usize {
+ return readWith(c, rpc, fid, offset, buf, c.chunk(fid, .read));
}
/// Writes `data`, chunking like `read` and stopping at the first short write.
- pub fn write(s: *Session, fid: u32, offset: u64, data: []const u8) Error!usize {
- return writeWith(s, rpc, fid, offset, data, s.chunkWrite(fid));
+ pub fn write(c: *Caller, fid: u32, offset: u64, data: []const u8) Session.Error!usize {
+ return writeWith(c, rpc, fid, offset, data, c.chunk(fid, .write));
}
- /// The returned Stat's strings (name/uid/gid/muid) borrow the session's input
- /// buffer: they are valid only until the next rpc. Copy what must outlive it.
- pub fn stat(s: *Session, fid: u32) Error!cloud9.Stat {
- const r = try s.rpc(.{ .stat = .{ .fid = fid } });
+ /// The returned Stat's strings (name/uid/gid/muid) borrow the caller's
+ /// reply buffer: they are valid only until its next rpc. Copy what must
+ /// outlive it.
+ pub fn stat(c: *Caller, fid: u32) Session.Error!cloud9.Stat {
+ const r = try c.rpc(.{ .stat = .{ .fid = fid } });
return r.stat;
}
- pub fn wstat(s: *Session, fid: u32, st: cloud9.Stat) Error!void {
- _ = try s.rpc(.{ .wstat = .{ .fid = fid, .stat = st } });
+ pub fn wstat(c: *Caller, fid: u32, st: cloud9.Stat) Session.Error!void {
+ _ = try c.rpc(.{ .wstat = .{ .fid = fid, .stat = st } });
}
/// Frees the fid locally even when the server reports an error.
- pub fn clunk(s: *Session, fid: u32) Error!void {
- defer s.freeFid(fid);
- _ = try s.rpc(.{ .clunk = .{ .fid = fid } });
+ pub fn clunk(c: *Caller, fid: u32) Session.Error!void {
+ defer c.session.freeFid(fid);
+ _ = try c.rpc(.{ .clunk = .{ .fid = fid } });
}
/// Frees the fid locally even when the server reports an error.
- pub fn remove(s: *Session, fid: u32) Error!void {
- defer s.freeFid(fid);
- _ = try s.rpc(.{ .remove = .{ .fid = fid } });
+ pub fn remove(c: *Caller, fid: u32) Session.Error!void {
+ defer c.session.freeFid(fid);
+ _ = try c.rpc(.{ .remove = .{ .fid = fid } });
}
/// Maps the last Rerror text to an errno (case-insensitive substring match).
- pub fn errno(s: *const Session) linux.E {
- return enameToErrno(s.ename[0..s.ename_len]);
+ pub fn errno(c: *const Caller) linux.E {
+ return enameToErrno(c.ename[0..c.ename_len]);
}
// -- internals --------------------------------------------------------------
- fn setEname(s: *Session, text: []const u8) void {
+ fn setEname(c: *Caller, text: []const u8) void {
const n = @min(text.len, 255);
- @memcpy(s.ename[0..n], text[0..n]);
- s.ename_len = n;
+ @memcpy(c.ename[0..n], text[0..n]);
+ c.ename_len = n;
}
- fn noteIounit(s: *Session, fid: u32, iounit: u32) void {
+ fn noteIounit(c: *Caller, fid: u32, iounit: u32) void {
+ const s = c.session;
+ const io = lockIo();
+ s.mutex.lockUncancelable(io);
+ defer s.mutex.unlock(io);
if (iounit == 0) {
_ = s.iounits.remove(fid);
} else {
@@ -347,39 +716,46 @@ pub const Session = struct {
}
}
- fn chunk(s: *Session, fid: u32) u32 {
- return chunkSize(s.client.maxRead(), s.iounits.get(fid) orelse 0);
- }
-
- fn chunkWrite(s: *Session, fid: u32) u32 {
- return chunkSize(s.client.maxWrite(), s.iounits.get(fid) orelse 0);
+ fn chunk(c: *Caller, fid: u32, dir: enum { read, write }) u32 {
+ const s = c.session;
+ const io = lockIo();
+ s.mutex.lockUncancelable(io);
+ defer s.mutex.unlock(io);
+ const max = switch (dir) {
+ .read => s.client.maxRead(),
+ .write => s.client.maxWrite(),
+ };
+ return chunkSize(max, s.iounits.get(fid) orelse 0);
}
- const Ready = enum { socket, cancel, timeout };
+ const Ready = enum { socket, wake, cancel, timeout };
- /// Blocks until the socket is readable (`.socket`), the interrupt source
- /// wants the request in flight cancelled (`.cancel`), `timeout_ms` passes
- /// with neither (`.timeout`; -1 waits forever), or `stop_fd` fires
- /// (`error.Stopped`). Anything the interrupt source consumes without asking
- /// for a cancellation simply resumes the wait.
- fn wait(s: *Session, timeout_ms: i32) Error!Ready {
+ /// Blocks until the socket is readable (`.socket`; `sock` is -1 when this
+ /// caller does not hold the baton), the caller is poked (`.wake`), the
+ /// interrupt source wants the request in flight cancelled (`.cancel`),
+ /// `timeout_ms` passes with none of that (`.timeout`; -1 waits forever),
+ /// or `stop_fd` fires (`error.Stopped`). Anything the interrupt source
+ /// consumes without asking for a cancellation simply resumes the wait.
+ fn wait(c: *Caller, sock: i32, timeout_ms: i32) Session.Error!Ready {
+ const stop_fd = c.session.stop_fd;
while (true) {
- var pfds: [3]linux.pollfd = undefined;
+ var pfds: [4]linux.pollfd = undefined;
var n: usize = 0;
- pfds[n] = .{ .fd = s.fd, .events = linux.POLL.IN, .revents = 0 };
+ pfds[n] = .{ .fd = sock, .events = linux.POLL.IN, .revents = 0 }; // -1: ignored by poll
+ n += 1;
+ pfds[n] = .{ .fd = c.wake, .events = linux.POLL.IN, .revents = 0 };
n += 1;
- const stop_at: ?usize = if (s.stop_fd >= 0) n else null;
+ const stop_at: ?usize = if (stop_fd >= 0) n else null;
if (stop_at != null) {
- pfds[n] = .{ .fd = s.stop_fd, .events = linux.POLL.IN, .revents = 0 };
+ pfds[n] = .{ .fd = stop_fd, .events = linux.POLL.IN, .revents = 0 };
n += 1;
}
- const ifd: i32 = if (s.interrupt) |i| i.watch(i.ctx) else -1;
+ const ifd: i32 = if (c.interrupt) |i| i.watch(i.ctx) else -1;
const int_at: ?usize = if (ifd >= 0) n else null;
if (int_at != null) {
pfds[n] = .{ .fd = ifd, .events = linux.POLL.IN, .revents = 0 };
n += 1;
}
- if (n == 1 and timeout_ms < 0) return .socket;
const prc = linux.poll(&pfds, @intCast(n), timeout_ms);
switch (linux.errno(prc)) {
.SUCCESS => {},
@@ -389,35 +765,69 @@ pub const Session = struct {
if (prc == 0) return .timeout;
// A reply that is already there wins over everything else.
if (pfds[0].revents != 0) return .socket;
+ if (pfds[1].revents != 0) {
+ var count: u64 = 0;
+ _ = linux.read(c.wake, std.mem.asBytes(&count), 8);
+ return .wake;
+ }
if (stop_at) |i| {
if (pfds[i].revents != 0) return error.Stopped;
}
if (int_at) |i| {
if (pfds[i].revents != 0) {
- const src = s.interrupt.?;
+ const src = c.interrupt.?;
if (try src.onReadable(src.ctx)) return .cancel;
}
}
}
}
+};
- /// Writes everything in the client's output buffer to the socket.
- fn flush(s: *Session) Error!void {
- while (s.client.output().len != 0) {
- const out = s.client.output();
- const rc = linux.write(s.fd, out.ptr, out.len);
- switch (linux.errno(rc)) {
- .SUCCESS => {
- if (rc == 0) return error.Closed;
- s.client.wrote(rc);
- },
- .INTR, .AGAIN => continue,
- .PIPE, .CONNRESET => return error.Closed,
- else => return error.Io,
- }
+/// A result whose slices point into `from` (the frame in the client's input
+/// buffer), with those slices moved to the same offsets in `to`, where the
+/// frame is copied.
+fn copyResult(r: cloud9.Client.Result, from: []const u8, to: []u8) cloud9.Client.Result {
+ @memcpy(to[0..from.len], from);
+ const move = struct {
+ fn f(slice: []const u8, src: []const u8, dst: []u8) []const u8 {
+ const lo = @intFromPtr(src.ptr);
+ const at = @intFromPtr(slice.ptr);
+ if (at < lo or at + slice.len > lo + src.len) return slice; // a literal, not the frame
+ return dst[at - lo ..][0..slice.len];
}
+ }.f;
+ var out = r;
+ switch (out) {
+ .fail => |*e| e.* = move(e.*, from, to),
+ .version => |*v| v.version = move(v.version, from, to),
+ .read => |*d| d.* = move(d.*, from, to),
+ .stat => |*st| {
+ st.name = move(st.name, from, to);
+ st.uid = move(st.uid, from, to);
+ st.gid = move(st.gid, from, to);
+ st.muid = move(st.muid, from, to);
+ },
+ else => {},
}
-};
+ return out;
+}
+
+/// Writes everything in the client's output buffer to the socket (with `wlock` held).
+fn writeOut(s: *Session) Session.Error!void {
+ while (s.client.output().len != 0) {
+ const out = s.client.output();
+ const rc = linux.write(s.fd, out.ptr, out.len);
+ switch (linux.errno(rc)) {
+ .SUCCESS => {
+ if (rc == 0) return error.Closed;
+ s.client.wrote(rc);
+ },
+ .INTR, .AGAIN => continue,
+ .PIPE, .CONNRESET => return error.Closed,
+ else => return error.Io,
+ }
+ }
+}
/// The monotonic clock in milliseconds: deadlines, not timestamps.
fn nowMs() i64 {
@@ -653,6 +1063,7 @@ test "ename → errno mapping" {
test "fid allocator recycles and never hands out 0" {
var s: Session = undefined;
s.gpa = testing.allocator;
+ s.mutex = .init;
s.next_fid = 1;
s.free_fids = .empty;
s.iounits = .empty;
@@ -787,15 +1198,15 @@ test "flush grace: a Tflush the server never answers wedges the session after th
var fs: FakeServer = .{ .fd = -1, .msize = 8192, .file_len = 50, .hang_offset = 0, .on_flush = .ignore, .on_hang_inject = &pi };
var pair = try fs.start();
defer pair.close();
- const s = &pair.session;
+ const s = pair.caller;
s.interrupt = pi.interface();
- s.flush_grace_ms = 100;
+ s.session.flush_grace_ms = 100;
// The read at offset 0 hangs; the injected INTERRUPT sends a Tflush; the
// server ignores it; the grace runs out.
var buf: [100]u8 = undefined;
try testing.expectError(error.Interrupted, s.read(1, 0, &buf));
try testing.expectEqual(@as(u32, 1), fs.flushes.load(.seq_cst));
- try testing.expect(s.wedged);
+ try testing.expect(s.session.wedged);
// From here on the session is closed for business, without another
// byte to the server.
try testing.expectError(error.Closed, s.read(1, 10, &buf));
@@ -811,12 +1222,12 @@ test "flush grace: zero waits for the Rflush, however late" {
var fs: FakeServer = .{ .fd = -1, .msize = 8192, .file_len = 50, .hang_offset = 0, .on_flush = .rflush, .on_hang_inject = &pi, .read_delay_ns = 0 };
var pair = try fs.start();
defer pair.close();
- const s = &pair.session;
+ const s = pair.caller;
s.interrupt = pi.interface();
- s.flush_grace_ms = 0;
+ s.session.flush_grace_ms = 0;
var buf: [100]u8 = undefined;
try testing.expectError(error.Interrupted, s.read(1, 0, &buf));
- try testing.expect(!s.wedged);
+ try testing.expect(!s.session.wedged);
// The session lives: the Rflush released the tag and reads go on.
try testing.expectEqual(@as(usize, 40), try s.read(1, 10, buf[0..40]));
}
@@ -827,12 +1238,12 @@ test "abort_on_cancel: a cancellation ends the rpc at once, sends no Tflush, wed
var fs: FakeServer = .{ .fd = -1, .msize = 8192, .file_len = 50, .hang_offset = 0, .on_flush = .rflush, .on_hang_inject = &pi };
var pair = try fs.start();
defer pair.close();
- const s = &pair.session;
+ const s = pair.caller;
s.interrupt = pi.interface();
s.abort_on_cancel = true;
var buf: [100]u8 = undefined;
try testing.expectError(error.Interrupted, s.read(1, 0, &buf));
- try testing.expect(s.wedged);
+ try testing.expect(s.session.wedged);
try testing.expectEqual(@as(u32, 0), fs.flushes.load(.seq_cst));
try testing.expectError(error.Closed, s.read(1, 10, &buf));
}
@@ -858,6 +1269,16 @@ pub const FakeServer = struct {
flush_oldtag: std.atomic.Value(u32) = .init(0xFFFF),
/// The tag of the hung Tread, for the test to compare with `flush_oldtag`.
hung_tag: std.atomic.Value(u32) = .init(0xFFFF),
+ /// How many Treads are hung right now (several callers can each park one).
+ hung_now: std.atomic.Value(u32) = .init(0),
+ /// A Tstat first answers every hung Tread (with data), then itself: lets a
+ /// test release a crowd of parked readers.
+ release_on_stat: bool = false,
+ /// The first byte of every Rread is the fid it was read on (low byte):
+ /// lets a test tell which fid, i.e. which open, a read went to.
+ fid_in_data: bool = false,
+ /// Tclunks seen.
+ clunks: std.atomic.Value(u32) = .init(0),
/// When set, the server injects that source's INTERRUPT the moment a read
/// hangs, so the cancellation provably arrives while the wait is on.
on_hang_inject: ?*PipeInterrupt = null,
@@ -881,7 +1302,8 @@ pub const FakeServer = struct {
var srv: cloud9.Server = .init(.{ .in = in, .out = out });
var tmp: [4096]u8 = undefined;
var data: [8192]u8 = undefined;
- var hung: ?struct { tag: u16, offset: u64, count: u32 } = null;
+ const Hung = struct { tag: u16, fid: u32, offset: u64, count: u32 };
+ var hung: [max_tags + 1]?Hung = @splat(null);
while (true) {
while (try srv.receive()) |req| {
const tag = req.tag;
@@ -905,36 +1327,50 @@ pub const FakeServer = struct {
try srv.reply(tag, .{ .rwalk = .{ .nwqid = n, .wqid = wq } });
}
},
- .tstat => try srv.reply(tag, .{ .rstat = .{ .stat = .{
- .type = 0,
- .dev = 0,
- .qid = file_qid,
- .mode = 0o644,
- .atime = 1,
- .mtime = 2,
- .length = fs.file_len,
- .name = "file",
- .uid = "u",
- .gid = "g",
- .muid = "u",
- } } }),
+ .tstat => {
+ if (fs.release_on_stat) for (&hung) |*slot| if (slot.*) |h| {
+ try srv.reply(h.tag, .{ .rread = .{ .data = fs.readData(&data, h.fid, h.offset, h.count) } });
+ slot.* = null;
+ _ = fs.hung_now.fetchSub(1, .seq_cst);
+ };
+ try srv.reply(tag, .{ .rstat = .{ .stat = .{
+ .type = 0,
+ .dev = 0,
+ .qid = file_qid,
+ .mode = 0o644,
+ .atime = 1,
+ .mtime = 2,
+ .length = fs.file_len,
+ .name = "file",
+ .uid = "u",
+ .gid = "g",
+ .muid = "u",
+ } } });
+ },
.topen => |m| try srv.reply(tag, .{ .ropen = .{ .qid = file_qid, .iounit = if (m.mode == cloud9.owrite) 700 else 0 } }),
.tread => |m| {
fs.max_read_count = @max(fs.max_read_count, m.count);
if (fs.hang_offset != null and fs.hang_offset.? == m.offset) {
- hung = .{ .tag = tag, .offset = m.offset, .count = m.count };
+ for (&hung) |*slot| if (slot.* == null) {
+ slot.* = .{ .tag = tag, .fid = m.fid, .offset = m.offset, .count = m.count };
+ break;
+ };
fs.hung_tag.store(tag, .seq_cst);
+ _ = fs.hung_now.fetchAdd(1, .seq_cst);
if (fs.on_hang_inject) |p| try p.inject(p.unique);
} else {
if (fs.read_delay_ns != 0) {
const ts: linux.timespec = .{ .sec = @intCast(fs.read_delay_ns / std.time.ns_per_s), .nsec = @intCast(fs.read_delay_ns % std.time.ns_per_s) };
_ = linux.nanosleep(&ts, null);
}
- try srv.reply(tag, .{ .rread = .{ .data = fs.fill(&data, m.offset, m.count) } });
+ try srv.reply(tag, .{ .rread = .{ .data = fs.readData(&data, m.fid, m.offset, m.count) } });
}
},
.twrite => |m| try srv.reply(tag, .{ .rwrite = .{ .count = @intCast(m.data.len) } }),
- .tclunk => try srv.reply(tag, .rclunk),
+ .tclunk => {
+ _ = fs.clunks.fetchAdd(1, .seq_cst);
+ try srv.reply(tag, .rclunk);
+ },
.tremove => try srv.reply(tag, .{ .rerror = .{ .ename = "permission denied" } }),
.twstat => try srv.reply(tag, .rwstat),
.tflush => |m| {
@@ -945,13 +1381,17 @@ pub const FakeServer = struct {
.hangup => return,
.ignore => {},
.rflush => {
- if (hung != null and hung.?.tag == m.oldtag) hung = null;
+ for (&hung) |*slot| if (slot.*) |h| if (h.tag == m.oldtag) {
+ slot.* = null;
+ _ = fs.hung_now.fetchSub(1, .seq_cst);
+ };
try srv.reply(tag, .rflush);
},
.reply_then_rflush => {
- if (hung) |h| if (h.tag == m.oldtag) {
- try srv.reply(h.tag, .{ .rread = .{ .data = fs.fill(&data, h.offset, h.count) } });
- hung = null;
+ for (&hung) |*slot| if (slot.*) |h| if (h.tag == m.oldtag) {
+ try srv.reply(h.tag, .{ .rread = .{ .data = fs.readData(&data, h.fid, h.offset, h.count) } });
+ slot.* = null;
+ _ = fs.hung_now.fetchSub(1, .seq_cst);
};
try srv.reply(tag, .rflush);
},
@@ -974,6 +1414,13 @@ pub const FakeServer = struct {
}
}
+ /// What a Tread on `fid` gets: `fill`, stamped with the fid under `fid_in_data`.
+ fn readData(fs: *const FakeServer, data: []u8, fid: u32, offset: u64, count: u32) []const u8 {
+ const got = fs.fill(data, offset, count);
+ if (fs.fid_in_data and got.len != 0) data[0] = @truncate(fid);
+ return got;
+ }
+
/// File contents: byte i == i & 0xff, `file_len` bytes long.
fn fill(fs: *const FakeServer, data: []u8, offset: u64, count: u32) []const u8 {
var n: usize = 0;
@@ -987,11 +1434,27 @@ pub const FakeServer = struct {
/// for reading) plus the server thread; `close` when done.
pub const Pair = struct {
server: *FakeServer,
- session: Session,
+ session: *Session,
+ /// The test thread's caller; `newCaller` makes more.
+ caller: *Caller,
thread: std.Thread,
+ pub fn newCaller(p: *Pair) !*Caller {
+ const c = try testing.allocator.create(Caller);
+ errdefer testing.allocator.destroy(c);
+ c.* = try Caller.init(p.session);
+ return c;
+ }
+
+ pub fn freeCaller(c: *Caller) void {
+ c.deinit();
+ testing.allocator.destroy(c);
+ }
+
pub fn close(p: *Pair) void {
+ freeCaller(p.caller);
p.session.deinit();
+ testing.allocator.destroy(p.session);
p.thread.join();
}
};
@@ -1001,13 +1464,19 @@ pub const FakeServer = struct {
if (linux.errno(linux.socketpair(linux.AF.UNIX, linux.SOCK.STREAM | linux.SOCK.CLOEXEC, 0, &fds)) != .SUCCESS) return error.Io;
fs.fd = fds[1];
const th = try std.Thread.spawn(.{}, FakeServer.run, .{fs});
- var s = try Session.connect(testing.allocator, .{ .fd = fds[0] }, fs.msize);
+ const s = try testing.allocator.create(Session);
+ errdefer testing.allocator.destroy(s);
+ s.* = try Session.connect(testing.allocator, .{ .fd = fds[0] }, fs.msize);
errdefer s.deinit();
- _ = try s.attach(0, "me", "");
+ var pair: Pair = .{ .server = fs, .session = s, .caller = undefined, .thread = th };
+ pair.caller = try pair.newCaller();
+ errdefer Pair.freeCaller(pair.caller);
+ const c = pair.caller;
+ _ = try c.attach(0, "me", "");
const fid = s.allocFid();
- _ = try s.walk(0, fid, &.{"file"});
- _ = try s.open(fid, cloud9.oread);
- return .{ .server = fs, .session = s, .thread = th };
+ _ = try c.walk(0, fid, &.{"file"});
+ _ = try c.open(fid, cloud9.oread);
+ return pair;
}
};
@@ -1082,7 +1551,7 @@ test "rpc wait loop: INTERRUPT → Tflush → Rflush → error.Interrupted; sess
var fs: FakeServer = .{ .fd = -1, .msize = 8192, .file_len = 50, .hang_offset = 0, .on_flush = .rflush };
var pair = try fs.start();
defer pair.close();
- const s = &pair.session;
+ const s = pair.caller;
var pi = try PipeInterrupt.init(77);
defer pi.deinit();
s.interrupt = pi.interface();
@@ -1108,14 +1577,14 @@ test "rpc wait loop: INTERRUPT → Tflush → Rflush → error.Interrupted; sess
const st = try s.stat(1);
try testing.expectEqual(@as(u64, 50), st.length);
try testing.expectEqual(@as(usize, 40), try s.read(1, 10, &buf));
- try testing.expectEqual(@as(usize, 0), s.client.pending());
+ try testing.expectEqual(@as(usize, 0), s.session.client.pending());
}
test "rpc wait loop: the reply beats the Rflush → data returned, stray Rflush swallowed" {
var fs: FakeServer = .{ .fd = -1, .msize = 8192, .file_len = 50, .hang_offset = 0, .on_flush = .reply_then_rflush };
var pair = try fs.start();
defer pair.close();
- const s = &pair.session;
+ const s = pair.caller;
var pi = try PipeInterrupt.init(5);
defer pi.deinit();
s.interrupt = pi.interface();
@@ -1134,7 +1603,7 @@ test "rpc wait loop: the reply beats the Rflush → data returned, stray Rflush
const st = try s.stat(1);
try testing.expectEqual(@as(u64, 50), st.length);
try testing.expectEqual(@as(usize, 40), try s.read(1, 10, &buf));
- try testing.expectEqual(@as(usize, 0), s.client.pending());
+ try testing.expectEqual(@as(usize, 0), s.session.client.pending());
}
test "rpc wait loop: a read interrupted after some data is a short read" {
@@ -1145,7 +1614,7 @@ test "rpc wait loop: a read interrupted after some data is a short read" {
var fs: FakeServer = .{ .fd = -1, .msize = 1024, .file_len = 5000, .hang_offset = 2026, .on_flush = .rflush, .on_hang_inject = &pi };
var pair = try fs.start();
defer pair.close();
- const s = &pair.session;
+ const s = pair.caller;
s.interrupt = pi.interface();
defer s.interrupt = null;
@@ -1154,16 +1623,20 @@ test "rpc wait loop: a read interrupted after some data is a short read" {
for (buf[0..2026], 0..) |b, i| try testing.expectEqual(@as(u8, @truncate(i)), b);
try testing.expect(pi.armed_flag);
try testing.expectEqual(@as(u32, 1), fs.flushes.load(.seq_cst));
- try testing.expectEqual(@as(usize, 0), s.client.pending());
+ try testing.expectEqual(@as(usize, 0), s.session.client.pending());
// An INTERRUPT already waiting when the read starts: the first chunk's
// reply races the flush and wins, the armed flag then stops the loop.
+ // (The delayed reply makes sure the INTERRUPT is seen first: with the
+ // reply already readable the wait takes it and the flush falls on the
+ // second chunk instead, which on a loaded machine happened.)
pi.armed_flag = false;
+ fs.read_delay_ns = 20 * std.time.ns_per_ms;
try pi.inject(9);
try testing.expectEqual(@as(usize, 1013), try s.read(1, 0, &buf));
// The stray Rflush is consumed by the next call.
_ = try s.stat(1);
- try testing.expectEqual(@as(usize, 0), s.client.pending());
+ try testing.expectEqual(@as(usize, 0), s.session.client.pending());
}
test "connectWatched: a silent server cannot pin the handshake past stop_fd" {
@@ -1216,18 +1689,21 @@ test "session against an in-process cloud9.Server" {
var fs: FakeServer = .{ .fd = fds[1], .msize = 8192, .file_len = 20_000 };
const th = try std.Thread.spawn(.{}, FakeServer.run, .{&fs});
- var s = try Session.connect(testing.allocator, .{ .fd = fds[0] }, 8192);
+ var session = try Session.connect(testing.allocator, .{ .fd = fds[0] }, 8192);
defer {
- s.deinit();
+ session.deinit();
th.join();
}
- try testing.expectEqual(@as(u32, 8192), s.msize);
+ try testing.expectEqual(@as(u32, 8192), session.msize);
+ var caller = try Caller.init(&session);
+ defer caller.deinit();
+ const s = &caller;
const root = try s.attach(0, "me", "");
try testing.expectEqual(FakeServer.dir_qid.path, root.path);
// Plain rpc + stat borrowing the input buffer.
- const fid = s.allocFid();
+ const fid = session.allocFid();
const w = try s.walk(0, fid, &.{"file"});
try testing.expectEqual(@as(u16, 1), w.nwqid);
try testing.expectEqual(FakeServer.file_qid.path, w.wqid[0].path);
@@ -1254,33 +1730,156 @@ test "session against an in-process cloud9.Server" {
try testing.expectEqual(@as(usize, 3000), try s.write(wfid, 0, buf[0..3000]));
// Partial walk → error.Nine with a "not exist" ename → ENOENT.
- const pfid = s.allocFid();
+ const pfid = session.allocFid();
try testing.expectError(error.Nine, s.walk(0, pfid, &.{ "dir", "nope" }));
try testing.expectEqual(linux.E.NOENT, s.errno());
try testing.expectEqualStrings("file does not exist", s.ename[0..s.ename_len]);
- s.freeFid(pfid);
+ session.freeFid(pfid);
// Server Rerror → error.Nine, ename copied, fid freed by remove even on error.
try testing.expectError(error.Nine, s.remove(wfid));
try testing.expectEqual(linux.E.ACCES, s.errno());
- try testing.expectEqual(wfid, s.allocFid()); // recycled
- s.freeFid(wfid);
+ try testing.expectEqual(wfid, session.allocFid()); // recycled
+ session.freeFid(wfid);
// Unsupported op → "not supported" → ENOTSUP; a plain wstat succeeds.
try testing.expectError(error.Nine, s.rpc(.{ .auth = .{ .afid = 5, .uname = "me" } }));
try testing.expectEqual(linux.E.OPNOTSUPP, s.errno());
try s.wstat(fid, dontcare);
try s.clunk(fid);
- try testing.expectEqual(fid, s.allocFid());
- s.freeFid(fid);
+ try testing.expectEqual(fid, session.allocFid());
+ session.freeFid(fid);
// A clone bound to a fid that then fails to walk must release the fid.
- const before = s.next_fid;
- const cfid = s.allocFid();
- s.freeFid(cfid);
+ const before = session.next_fid;
+ const cfid = session.allocFid();
+ session.freeFid(cfid);
try testing.expectError(error.Nine, s.walk(0, cfid, &.{"nope"}));
- try testing.expectEqual(before, s.next_fid);
+ try testing.expectEqual(before, session.next_fid);
// The server hanging up makes the pending rpc fail with error.Closed.
try testing.expectError(error.Closed, s.rpc(.{ .flush = .{ .oldtag = 0 } }));
}
+
+// -- multiplexing ----------------------------------------------------------------------
+
+/// Polls `cond` every millisecond for up to three seconds.
+fn eventually(cond: anytype) bool {
+ for (0..3000) |_| {
+ if (cond.check()) return true;
+ const ts: linux.timespec = .{ .sec = 0, .nsec = std.time.ns_per_ms };
+ _ = linux.nanosleep(&ts, null);
+ }
+ return cond.check();
+}
+
+const HungAtLeast = struct {
+ fs: *FakeServer,
+ n: u32,
+ fn check(h: HungAtLeast) bool {
+ return h.fs.hung_now.load(.seq_cst) >= h.n;
+ }
+};
+
+/// A caller reading `file` at offset 0 on its own thread (the offset the
+/// FakeServer parks), recording how the read ended.
+const ParkedRead = struct {
+ caller: *Caller,
+ thread: std.Thread = undefined,
+ result: Session.Error!usize = 0,
+ done: std.atomic.Value(bool) = .init(false),
+
+ fn start(p: *ParkedRead) !void {
+ p.thread = try std.Thread.spawn(.{}, run, .{p});
+ }
+
+ fn run(p: *ParkedRead) void {
+ var buf: [100]u8 = undefined;
+ p.result = p.caller.read(1, 0, &buf);
+ p.done.store(true, .release);
+ }
+};
+
+test "multiplexing: a parked read holds up only its own caller; its interrupt flushes exactly its tag" {
+ var fs: FakeServer = .{ .fd = -1, .msize = 8192, .file_len = 50, .hang_offset = 0, .on_flush = .rflush };
+ var pair = try fs.start();
+ defer pair.close();
+ var pi = try PipeInterrupt.init(21);
+ defer pi.deinit();
+ var parked: ParkedRead = .{ .caller = try pair.newCaller() };
+ defer FakeServer.Pair.freeCaller(parked.caller);
+ parked.caller.interrupt = pi.interface();
+ try parked.start();
+ try testing.expect(eventually(HungAtLeast{ .fs = &fs, .n = 1 }));
+
+ // The other caller's requests go through while the read is parked; which
+ // of the two threads holds the reading baton at any moment does not matter.
+ const s = pair.caller;
+ var buf: [100]u8 = undefined;
+ const t0 = nowMs();
+ for (0..50) |_| {
+ try testing.expectEqual(@as(usize, 40), try s.read(1, 10, buf[0..40]));
+ const st = try s.stat(1);
+ try testing.expectEqualStrings("file", st.name);
+ }
+ try testing.expect(nowMs() - t0 < 2000);
+ try testing.expect(!parked.done.load(.acquire));
+ try testing.expectEqual(@as(u32, 0), fs.flushes.load(.seq_cst));
+
+ // Interrupting the parked read sends one Tflush, for the parked tag, and
+ // the Rflush ends that read (and only it) in error.Interrupted.
+ const hung_tag = fs.hung_tag.load(.seq_cst);
+ try pi.inject(21);
+ parked.thread.join();
+ try testing.expectError(error.Interrupted, parked.result);
+ try testing.expectEqual(@as(u32, 1), fs.flushes.load(.seq_cst));
+ try testing.expectEqual(hung_tag, fs.flush_oldtag.load(.seq_cst));
+
+ // Both callers carry on; nothing is left pending.
+ try testing.expectEqual(@as(usize, 40), try s.read(1, 10, buf[0..40]));
+ try testing.expectEqual(@as(usize, 40), try parked.caller.read(1, 10, buf[0..40]));
+ try testing.expectEqual(@as(usize, 0), s.session.client.pending());
+}
+
+test "multiplexing: with every tag parked, a call waits for a tag instead of failing" {
+ // max_tags reads park (the client's whole request window); the next call
+ // finds no tag. Interrupting one parked read sends its Tflush on the tag
+ // the client keeps for flushes; the Rflush frees a tag, the waiting stat
+ // goes out, and (release_on_stat) the server answers the other reads.
+ var fs: FakeServer = .{ .fd = -1, .msize = 8192, .file_len = 50, .hang_offset = 0, .on_flush = .rflush, .release_on_stat = true };
+ var pair = try fs.start();
+ defer pair.close();
+ var pi = try PipeInterrupt.init(33);
+ defer pi.deinit();
+ var reads: [max_tags]ParkedRead = undefined;
+ for (&reads) |*r| r.* = .{ .caller = try pair.newCaller() };
+ defer for (&reads) |*r| FakeServer.Pair.freeCaller(r.caller);
+ reads[0].caller.interrupt = pi.interface();
+ for (&reads) |*r| try r.start();
+ try testing.expect(eventually(HungAtLeast{ .fs = &fs, .n = max_tags }));
+
+ const Stat = struct {
+ caller: *Caller,
+ result: Session.Error!u64 = 0,
+ done: std.atomic.Value(bool) = .init(false),
+ fn run(st: *@This()) void {
+ st.result = if (st.caller.stat(1)) |x| x.length else |e| e;
+ st.done.store(true, .release);
+ }
+ };
+ var stat: Stat = .{ .caller = pair.caller };
+ const th = try std.Thread.spawn(.{}, Stat.run, .{&stat});
+ // It is waiting for a tag, not failed.
+ const ts: linux.timespec = .{ .sec = 0, .nsec = 100 * std.time.ns_per_ms };
+ _ = linux.nanosleep(&ts, null);
+ try testing.expect(!stat.done.load(.acquire));
+
+ try pi.inject(33);
+ th.join();
+ try testing.expectEqual(@as(u64, 50), try stat.result);
+ for (&reads) |*r| r.thread.join();
+ try testing.expectError(error.Interrupted, reads[0].result);
+ for (reads[1..]) |r| try testing.expectEqual(@as(usize, 50), try r.result);
+ try testing.expectEqual(@as(u32, 1), fs.flushes.load(.seq_cst));
+ try testing.expectEqual(@as(usize, 0), pair.session.client.pending());
+}
diff --git a/docs/design.md b/docs/design.md
index e68530f..702315b 100644
--- a/docs/design.md
+++ b/docs/design.md
@@ -7,7 +7,11 @@ may already occupy their exact final position in the output frame, but arbitrary
input/output overlap is not supported. Stat uses the protocol's inner length;
Rstat and Twstat add and validate the outer length separately.
-`Client` supports 16 ordinary outstanding requests and one additional flush.
+`Client` supports 16 ordinary outstanding requests (`max_tags`) and one
+additional flush; `ClientWith(n)` is the same client with `n` ordinary tags
+(9ns uses 32, the `fs` engine's default `slot_capacity`, so its whole
+window can park on one connection without a read being refused), sharing
+its `Request`, `Result`, `Done` and `Options` types.
Replies can arrive out of order. Tags remain reserved while flushes are pending,
even when the original request has already completed. Multiple flushes and
flushing a flush are accounted for. A completed result's slices remain valid
diff --git a/src/client.zig b/src/client.zig
index 5b428dd..307cd7f 100644
--- a/src/client.zig
+++ b/src/client.zig
@@ -17,8 +17,6 @@ const frameLen = wire.frameLen;
const Decoded = wire.Decoded;
const totalLen = wire.encodedLen;
pub const msize_min: u32 = 24;
-pub const max_tags: usize = 16;
-
const twrite_header: usize = header_len + 4 + 8 + 4;
const rread_header: usize = header_len + 4;
@@ -26,7 +24,6 @@ const rread_header: usize = header_len + 4;
comptime {
assert(twrite_header == 23);
assert(rread_header == 11);
- assert(max_tags <= notag);
assert(msize_min > rread_header);
assert(msize_min > twrite_header);
}
@@ -40,353 +37,376 @@ pub const ClientError = error{
BadRequest,
};
-pub const Client = struct {
- in: []u8,
- out: []u8,
-
- in_len: usize = 0,
- frame: u32 = 0,
- out_len: usize = 0,
- out_off: usize = 0,
-
- msize: u32 = 0,
- asked: u32 = 0,
- versioning: bool = false,
- dead: bool = false,
+/// The request window of the default `Client`: ordinary tags in flight at
+/// once (one more is kept for a Tflush).
+pub const max_tags: usize = 16;
- tags: [max_tags + 1]Slot = @splat(.{}),
+pub const Client = ClientWith(max_tags);
- const Slot = struct {
- op: ?Op = null,
- count: u32 = 0,
- oldtag: u16 = notag,
- completed: bool = false,
- };
+pub const ClientOp = enum { version, auth, attach, flush, walk, open, create, read, write, clunk, remove, stat, wstat };
- pub const Op = enum { version, auth, attach, flush, walk, open, create, read, write, clunk, remove, stat, wstat };
+pub const ClientRequest = union(ClientOp) {
+ version: struct { msize: u32 = 0 },
+ auth: struct { afid: u32, uname: []const u8, aname: []const u8 = "" },
+ attach: struct { fid: u32, afid: u32 = nofid, uname: []const u8, aname: []const u8 = "" },
+ flush: struct { oldtag: u16 },
+ walk: struct { fid: u32, newfid: u32, names: []const []const u8 },
+ open: struct { fid: u32, mode: u8 },
+ create: struct { fid: u32, name: []const u8, perm: u32, mode: u8 },
+ read: struct { fid: u32, offset: u64, count: u32 },
+ write: struct { fid: u32, offset: u64, data: []const u8 },
+ clunk: struct { fid: u32 },
+ remove: struct { fid: u32 },
+ stat: struct { fid: u32 },
+ wstat: struct { fid: u32, stat: Stat },
+};
- pub const Request = union(Op) {
- version: struct { msize: u32 = 0 },
- auth: struct { afid: u32, uname: []const u8, aname: []const u8 = "" },
- attach: struct { fid: u32, afid: u32 = nofid, uname: []const u8, aname: []const u8 = "" },
- flush: struct { oldtag: u16 },
- walk: struct { fid: u32, newfid: u32, names: []const []const u8 },
- open: struct { fid: u32, mode: u8 },
- create: struct { fid: u32, name: []const u8, perm: u32, mode: u8 },
- read: struct { fid: u32, offset: u64, count: u32 },
- write: struct { fid: u32, offset: u64, data: []const u8 },
- clunk: struct { fid: u32 },
- remove: struct { fid: u32 },
- stat: struct { fid: u32 },
- wstat: struct { fid: u32, stat: Stat },
- };
+pub const ClientResult = union(enum) {
+ fail: []const u8,
+ version: struct { msize: u32, version: []const u8 },
+ auth: Qid,
+ flush: void,
+ create: struct { qid: Qid, iounit: u32 },
+ remove: void,
+ wstat: void,
+ attach: Qid,
+ walk: struct { nwqid: u16, wqid: [max_welem]Qid },
+ open: struct { qid: Qid, iounit: u32 },
+ read: []const u8,
+ write: u32,
+ clunk: void,
+ stat: Stat,
+};
- pub const Result = union(enum) {
- fail: []const u8,
- version: struct { msize: u32, version: []const u8 },
- auth: Qid,
- flush: void,
- create: struct { qid: Qid, iounit: u32 },
- remove: void,
- wstat: void,
- attach: Qid,
- walk: struct { nwqid: u16, wqid: [max_welem]Qid },
- open: struct { qid: Qid, iounit: u32 },
- read: []const u8,
- write: u32,
- clunk: void,
- stat: Stat,
- };
+pub const ClientDone = struct {
+ tag: u16,
+ op: ClientOp,
+ result: ClientResult,
+};
- pub const Done = struct {
- tag: u16,
- op: Op,
- result: Result,
- };
+pub const ClientOptions = struct {
+ in: []u8,
+ out: []u8,
+};
- pub const Options = struct {
+/// A client with `tag_count` ordinary request tags, plus one kept so that a
+/// full window can still be flushed. Every instantiation shares the request,
+/// result and option types above.
+pub fn ClientWith(comptime tag_count: usize) type {
+ comptime {
+ assert(tag_count >= 1);
+ assert(tag_count < notag);
+ }
+ return struct {
+ /// The ordinary request tags this client keeps in flight at most.
+ pub const window = tag_count;
in: []u8,
out: []u8,
- };
- pub fn init(opts: Options) Client {
- assert(opts.in.len >= msize_min);
- assert(opts.out.len >= msize_min);
- return .{ .in = opts.in, .out = opts.out };
- }
+ in_len: usize = 0,
+ frame: u32 = 0,
+ out_len: usize = 0,
+ out_off: usize = 0,
- pub fn hangup(c: *Client) void {
- c.dead = true;
- c.tags = @splat(.{});
- c.versioning = false;
- c.msize = 0;
- c.asked = 0;
- c.in_len = 0;
- c.frame = 0;
- c.out_len = 0;
- c.out_off = 0;
- }
+ msize: u32 = 0,
+ asked: u32 = 0,
+ versioning: bool = false,
+ dead: bool = false,
- pub fn push(c: *Client, bytes: []const u8) usize {
- if (c.dead) return 0;
- const n = @min(bytes.len, c.in.len - c.in_len);
- @memcpy(c.in[c.in_len..][0..n], bytes[0..n]);
- c.in_len += n;
- return n;
- }
+ tags: [tag_count + 1]Slot = @splat(.{}),
- pub fn output(c: *const Client) []const u8 {
- return c.out[c.out_off..c.out_len];
- }
+ const Slot = struct {
+ op: ?Op = null,
+ count: u32 = 0,
+ oldtag: u16 = notag,
+ completed: bool = false,
+ };
- pub fn wrote(c: *Client, n: usize) void {
- assert(n <= c.out_len - c.out_off);
- c.out_off += n;
- if (c.out_off == c.out_len) {
- c.out_off = 0;
+ pub const Op = ClientOp;
+ pub const Request = ClientRequest;
+ pub const Result = ClientResult;
+ pub const Done = ClientDone;
+ pub const Options = ClientOptions;
+
+ pub fn init(opts: Options) @This() {
+ assert(opts.in.len >= msize_min);
+ assert(opts.out.len >= msize_min);
+ return .{ .in = opts.in, .out = opts.out };
+ }
+
+ pub fn hangup(c: *@This()) void {
+ c.dead = true;
+ c.tags = @splat(.{});
+ c.versioning = false;
+ c.msize = 0;
+ c.asked = 0;
+ c.in_len = 0;
+ c.frame = 0;
c.out_len = 0;
+ c.out_off = 0;
}
- }
- fn compact(c: *Client) void {
- assert(c.out_off <= c.out_len);
- const n = c.out_len - c.out_off;
- std.mem.copyForwards(u8, c.out[0..n], c.out[c.out_off..c.out_len]);
- c.out_off = 0;
- c.out_len = n;
- }
+ pub fn push(c: *@This(), bytes: []const u8) usize {
+ if (c.dead) return 0;
+ const n = @min(bytes.len, c.in.len - c.in_len);
+ @memcpy(c.in[c.in_len..][0..n], bytes[0..n]);
+ c.in_len += n;
+ return n;
+ }
- fn dropFrame(c: *Client) void {
- assert(c.frame != 0);
- assert(c.frame <= c.in_len);
- const n = c.frame;
- std.mem.copyForwards(u8, c.in[0 .. c.in_len - n], c.in[n..c.in_len]);
- c.in_len -= n;
- c.frame = 0;
- }
+ pub fn output(c: *const @This()) []const u8 {
+ return c.out[c.out_off..c.out_len];
+ }
- pub fn maxRead(c: *const Client) u32 {
- if (c.msize == 0) return 0;
- return c.msize - @as(u32, @intCast(rread_header));
- }
+ pub fn wrote(c: *@This(), n: usize) void {
+ assert(n <= c.out_len - c.out_off);
+ c.out_off += n;
+ if (c.out_off == c.out_len) {
+ c.out_off = 0;
+ c.out_len = 0;
+ }
+ }
- pub fn maxWrite(c: *const Client) u32 {
- if (c.msize == 0) return 0;
- return c.msize - @as(u32, @intCast(twrite_header));
- }
+ fn compact(c: *@This()) void {
+ assert(c.out_off <= c.out_len);
+ const n = c.out_len - c.out_off;
+ std.mem.copyForwards(u8, c.out[0..n], c.out[c.out_off..c.out_len]);
+ c.out_off = 0;
+ c.out_len = n;
+ }
- pub fn pending(c: *const Client) usize {
- var n: usize = @intFromBool(c.versioning);
- for (c.tags) |t| n += @intFromBool(t.op != null);
- return n;
- }
+ fn dropFrame(c: *@This()) void {
+ assert(c.frame != 0);
+ assert(c.frame <= c.in_len);
+ const n = c.frame;
+ std.mem.copyForwards(u8, c.in[0 .. c.in_len - n], c.in[n..c.in_len]);
+ c.in_len -= n;
+ c.frame = 0;
+ }
- pub fn submit(c: *Client, req: Request) ClientError!u16 {
- if (c.dead) return error.Dead;
- if (req == .version) return c.beginVersion(req.version.msize);
- if (c.msize == 0 or c.versioning) return error.Handshake;
+ pub fn maxRead(c: *const @This()) u32 {
+ if (c.msize == 0) return 0;
+ return c.msize - @as(u32, @intCast(rread_header));
+ }
- const msg: Msg = switch (req) {
- .version => unreachable, // handled above
- .auth => |m| blk: {
- if (m.afid == nofid) return error.BadRequest;
- break :blk .{ .tauth = .{ .afid = m.afid, .uname = m.uname, .aname = m.aname } };
- },
- .flush => |m| blk: {
- if (m.oldtag == notag) return error.BadRequest;
- break :blk .{ .tflush = .{ .oldtag = m.oldtag } };
- },
- .create => |m| blk: {
- if (m.fid == nofid or m.name.len == 0) return error.BadRequest;
- if (std.mem.indexOfAny(u8, m.name, "/\x00") != null) return error.BadRequest;
- if (std.mem.eql(u8, m.name, ".") or std.mem.eql(u8, m.name, "..")) return error.BadRequest;
- break :blk .{ .tcreate = .{ .fid = m.fid, .name = m.name, .perm = m.perm, .mode = m.mode } };
- },
- .remove => |m| blk: {
- if (m.fid == nofid) return error.BadRequest;
- break :blk .{ .tremove = .{ .fid = m.fid } };
- },
- .wstat => |m| blk: {
- if (m.fid == nofid) return error.BadRequest;
- break :blk .{ .twstat = .{ .fid = m.fid, .stat = m.stat } };
- },
- .attach => |m| blk: {
- if (m.fid == nofid) return error.BadRequest;
- break :blk .{ .tattach = .{
- .fid = m.fid,
- .afid = m.afid,
- .uname = m.uname,
- .aname = m.aname,
- } };
- },
- .walk => |m| blk: {
- if (m.fid == nofid or m.newfid == nofid) return error.BadRequest;
- if (m.names.len > max_welem) return error.BadRequest;
- var w: [max_welem][]const u8 = @splat("");
- for (m.names, 0..) |n, i| {
- if (n.len == 0) return error.BadRequest;
- if (std.mem.indexOfAny(u8, n, "/\x00") != null) return error.BadRequest;
- w[i] = n;
- }
- break :blk .{ .twalk = .{
- .fid = m.fid,
- .newfid = m.newfid,
- .nwname = @intCast(m.names.len),
- .wname = w,
- } };
- },
- .open => |m| blk: {
- if (m.fid == nofid) return error.BadRequest;
- break :blk .{ .topen = .{ .fid = m.fid, .mode = m.mode } };
- },
- .read => |m| blk: {
- if (m.fid == nofid) return error.BadRequest;
- if (m.count > c.maxRead()) return error.TooLarge;
- break :blk .{ .tread = .{ .fid = m.fid, .offset = m.offset, .count = m.count } };
- },
- .write => |m| blk: {
- if (m.fid == nofid) return error.BadRequest;
- break :blk .{ .twrite = .{ .fid = m.fid, .offset = m.offset, .data = m.data } };
- },
- .clunk => |m| blk: {
- if (m.fid == nofid) return error.BadRequest;
- break :blk .{ .tclunk = .{ .fid = m.fid } };
- },
- .stat => |m| blk: {
- if (m.fid == nofid) return error.BadRequest;
- break :blk .{ .tstat = .{ .fid = m.fid } };
- },
- };
- const need = totalLen(msg) catch return error.TooLarge;
- if (need > c.msize) return error.TooLarge;
+ pub fn maxWrite(c: *const @This()) u32 {
+ if (c.msize == 0) return 0;
+ return c.msize - @as(u32, @intCast(twrite_header));
+ }
- const op = std.meta.activeTag(req);
- const tag = c.claim(op, if (req == .flush) req.flush.oldtag else notag) orelse return error.NoTags;
- errdefer c.tags[tag] = .{};
- try c.emit(tag, msg);
- switch (req) {
- .read => |m| c.tags[tag].count = m.count,
- .write => |m| c.tags[tag].count = @intCast(m.data.len),
- .walk => |m| c.tags[tag].count = @intCast(m.names.len),
- .flush => |m| {
- c.tags[tag].oldtag = m.oldtag;
- },
- else => {},
+ pub fn pending(c: *const @This()) usize {
+ var n: usize = @intFromBool(c.versioning);
+ for (c.tags) |t| n += @intFromBool(t.op != null);
+ return n;
}
- return tag;
- }
- fn beginVersion(c: *Client, want: u32) ClientError!u16 {
- if (c.pending() != 0) return error.Handshake;
- const cap: u32 = @intCast(@min(c.in.len, c.out.len, std.math.maxInt(u32)));
- const m = @min(if (want == 0) cap else want, cap);
- if (m < msize_min) return error.BadRequest;
- try c.emit(notag, .{ .tversion = .{ .msize = m, .version = "9P2000" } });
- c.msize = 0;
- c.asked = m;
- c.versioning = true;
- return notag;
- }
+ pub fn submit(c: *@This(), req: Request) ClientError!u16 {
+ if (c.dead) return error.Dead;
+ if (req == .version) return c.beginVersion(req.version.msize);
+ if (c.msize == 0 or c.versioning) return error.Handshake;
+
+ const msg: Msg = switch (req) {
+ .version => unreachable, // handled above
+ .auth => |m| blk: {
+ if (m.afid == nofid) return error.BadRequest;
+ break :blk .{ .tauth = .{ .afid = m.afid, .uname = m.uname, .aname = m.aname } };
+ },
+ .flush => |m| blk: {
+ if (m.oldtag == notag) return error.BadRequest;
+ break :blk .{ .tflush = .{ .oldtag = m.oldtag } };
+ },
+ .create => |m| blk: {
+ if (m.fid == nofid or m.name.len == 0) return error.BadRequest;
+ if (std.mem.indexOfAny(u8, m.name, "/\x00") != null) return error.BadRequest;
+ if (std.mem.eql(u8, m.name, ".") or std.mem.eql(u8, m.name, "..")) return error.BadRequest;
+ break :blk .{ .tcreate = .{ .fid = m.fid, .name = m.name, .perm = m.perm, .mode = m.mode } };
+ },
+ .remove => |m| blk: {
+ if (m.fid == nofid) return error.BadRequest;
+ break :blk .{ .tremove = .{ .fid = m.fid } };
+ },
+ .wstat => |m| blk: {
+ if (m.fid == nofid) return error.BadRequest;
+ break :blk .{ .twstat = .{ .fid = m.fid, .stat = m.stat } };
+ },
+ .attach => |m| blk: {
+ if (m.fid == nofid) return error.BadRequest;
+ break :blk .{ .tattach = .{
+ .fid = m.fid,
+ .afid = m.afid,
+ .uname = m.uname,
+ .aname = m.aname,
+ } };
+ },
+ .walk => |m| blk: {
+ if (m.fid == nofid or m.newfid == nofid) return error.BadRequest;
+ if (m.names.len > max_welem) return error.BadRequest;
+ var w: [max_welem][]const u8 = @splat("");
+ for (m.names, 0..) |n, i| {
+ if (n.len == 0) return error.BadRequest;
+ if (std.mem.indexOfAny(u8, n, "/\x00") != null) return error.BadRequest;
+ w[i] = n;
+ }
+ break :blk .{ .twalk = .{
+ .fid = m.fid,
+ .newfid = m.newfid,
+ .nwname = @intCast(m.names.len),
+ .wname = w,
+ } };
+ },
+ .open => |m| blk: {
+ if (m.fid == nofid) return error.BadRequest;
+ break :blk .{ .topen = .{ .fid = m.fid, .mode = m.mode } };
+ },
+ .read => |m| blk: {
+ if (m.fid == nofid) return error.BadRequest;
+ if (m.count > c.maxRead()) return error.TooLarge;
+ break :blk .{ .tread = .{ .fid = m.fid, .offset = m.offset, .count = m.count } };
+ },
+ .write => |m| blk: {
+ if (m.fid == nofid) return error.BadRequest;
+ break :blk .{ .twrite = .{ .fid = m.fid, .offset = m.offset, .data = m.data } };
+ },
+ .clunk => |m| blk: {
+ if (m.fid == nofid) return error.BadRequest;
+ break :blk .{ .tclunk = .{ .fid = m.fid } };
+ },
+ .stat => |m| blk: {
+ if (m.fid == nofid) return error.BadRequest;
+ break :blk .{ .tstat = .{ .fid = m.fid } };
+ },
+ };
+ const need = totalLen(msg) catch return error.TooLarge;
+ if (need > c.msize) return error.TooLarge;
- fn reserved(c: *const Client, tag: u16) bool {
- for (c.tags) |slot| {
- if (slot.op == .flush and !slot.completed and slot.oldtag == tag) return true;
+ const op = std.meta.activeTag(req);
+ const tag = c.claim(op, if (req == .flush) req.flush.oldtag else notag) orelse return error.NoTags;
+ errdefer c.tags[tag] = .{};
+ try c.emit(tag, msg);
+ switch (req) {
+ .read => |m| c.tags[tag].count = m.count,
+ .write => |m| c.tags[tag].count = @intCast(m.data.len),
+ .walk => |m| c.tags[tag].count = @intCast(m.names.len),
+ .flush => |m| {
+ c.tags[tag].oldtag = m.oldtag;
+ },
+ else => {},
+ }
+ return tag;
}
- return false;
- }
- fn claim(c: *Client, op: Op, avoid: u16) ?u16 {
- for (&c.tags, 0..) |*t, i| {
- // Keep one tag available to cancel a fully occupied request window.
- if (i >= max_tags and op != .flush) continue;
- if (t.op != null or i == avoid or c.reserved(@intCast(i))) continue;
- t.* = .{ .op = op };
- return @intCast(i);
+ fn beginVersion(c: *@This(), want: u32) ClientError!u16 {
+ if (c.pending() != 0) return error.Handshake;
+ const cap: u32 = @intCast(@min(c.in.len, c.out.len, std.math.maxInt(u32)));
+ const m = @min(if (want == 0) cap else want, cap);
+ if (m < msize_min) return error.BadRequest;
+ try c.emit(notag, .{ .tversion = .{ .msize = m, .version = "9P2000" } });
+ c.msize = 0;
+ c.asked = m;
+ c.versioning = true;
+ return notag;
}
- return null;
- }
- fn emit(c: *Client, tag: u16, msg: Msg) ClientError!void {
- if (c.out_off != 0) c.compact();
- const bytes = encode(msg, tag, c.out[c.out_len..]) catch return error.NoSpace;
- c.out_len += bytes.len;
- }
+ fn reserved(c: *const @This(), tag: u16) bool {
+ for (c.tags) |slot| {
+ if (slot.op == .flush and !slot.completed and slot.oldtag == tag) return true;
+ }
+ return false;
+ }
- pub fn take(c: *Client) ?Done {
- if (c.frame != 0) c.dropFrame();
- if (c.dead) return null;
- const len = frameLen(c.in[0..c.in_len]) orelse return null;
- if (len < header_len or len > c.in.len) return c.die();
- if (c.msize != 0 and len > c.msize) return c.die();
- if (len > c.in_len) return null;
- c.frame = len;
- const got = decode(c.in[0..len]) catch return c.die();
- return c.consume(got);
- }
+ fn claim(c: *@This(), op: Op, avoid: u16) ?u16 {
+ for (&c.tags, 0..) |*t, i| {
+ // Keep one tag available to cancel a fully occupied request window.
+ if (i >= tag_count and op != .flush) continue;
+ if (t.op != null or i == avoid or c.reserved(@intCast(i))) continue;
+ t.* = .{ .op = op };
+ return @intCast(i);
+ }
+ return null;
+ }
- fn die(c: *Client) ?Done {
- c.dead = true;
- return null;
- }
+ fn emit(c: *@This(), tag: u16, msg: Msg) ClientError!void {
+ if (c.out_off != 0) c.compact();
+ const bytes = encode(msg, tag, c.out[c.out_len..]) catch return error.NoSpace;
+ c.out_len += bytes.len;
+ }
- fn consume(c: *Client, got: Decoded) ?Done {
- if (isT(got.msg.msgType())) return c.die();
- if (got.msg == .rversion) return c.version(got);
- if (c.versioning or c.msize == 0) return c.die();
- if (got.tag >= c.tags.len) return c.die();
- const slot = &c.tags[got.tag];
- const op = slot.op orelse return c.die();
- if (slot.completed) return c.die();
- const result: Result = switch (got.msg) {
- .rerror => |m| if (op == .flush) return c.die() else .{ .fail = m.ename },
- .rauth => |m| if (op != .auth) return c.die() else .{ .auth = m.aqid },
- .rcreate => |m| if (op != .create) return c.die() else .{ .create = .{ .qid = m.qid, .iounit = m.iounit } },
- .rremove => if (op != .remove) return c.die() else .remove,
- .rwstat => if (op != .wstat) return c.die() else .wstat,
- .rflush => blk: {
- if (op != .flush) return c.die();
- if (slot.oldtag < c.tags.len) c.tags[slot.oldtag] = .{};
- break :blk .flush;
- },
- .rattach => |m| if (op != .attach) return c.die() else .{ .attach = m.qid },
- .rwalk => |m| if (op != .walk or m.nwqid > slot.count or (m.nwqid == 0 and slot.count != 0)) return c.die() else .{
- .walk = .{ .nwqid = m.nwqid, .wqid = m.wqid },
- },
- .ropen => |m| if (op != .open) return c.die() else .{
- .open = .{ .qid = m.qid, .iounit = m.iounit },
- },
- .rread => |m| blk: {
- if (op != .read) return c.die();
- if (m.data.len > slot.count) return c.die();
- break :blk .{ .read = m.data };
- },
- .rwrite => |m| if (op != .write or m.count > slot.count) return c.die() else .{ .write = m.count },
- .rclunk => if (op != .clunk) return c.die() else .clunk,
- .rstat => |m| if (op != .stat) return c.die() else .{ .stat = m.stat },
- else => return c.die(),
- };
- slot.completed = true;
- // Multiple flushes can reserve the same tag; flushing a flush can release
- // a reservation on a completed original operation.
- for (&c.tags, 0..) |*candidate, tag| {
- if (candidate.completed and !c.reserved(@intCast(tag))) candidate.* = .{};
+ pub fn take(c: *@This()) ?Done {
+ if (c.frame != 0) c.dropFrame();
+ if (c.dead) return null;
+ const len = frameLen(c.in[0..c.in_len]) orelse return null;
+ if (len < header_len or len > c.in.len) return c.die();
+ if (c.msize != 0 and len > c.msize) return c.die();
+ if (len > c.in_len) return null;
+ c.frame = len;
+ const got = decode(c.in[0..len]) catch return c.die();
+ return c.consume(got);
}
- return .{ .tag = got.tag, .op = op, .result = result };
- }
- fn version(c: *Client, got: Decoded) ?Done {
- if (!c.versioning) return c.die();
- if (got.tag != notag) return c.die();
- const m = got.msg.rversion;
- if (m.msize > c.asked or m.msize < msize_min) return c.die();
- c.versioning = false;
- if (std.mem.eql(u8, m.version, "9P2000")) {
- c.msize = m.msize;
- } else if (!std.mem.eql(u8, m.version, "unknown")) {
- return c.die();
+ fn die(c: *@This()) ?Done {
+ c.dead = true;
+ return null;
}
- return .{ .tag = notag, .op = .version, .result = .{
- .version = .{ .msize = m.msize, .version = m.version },
- } };
- }
-};
+
+ fn consume(c: *@This(), got: Decoded) ?Done {
+ if (isT(got.msg.msgType())) return c.die();
+ if (got.msg == .rversion) return c.version(got);
+ if (c.versioning or c.msize == 0) return c.die();
+ if (got.tag >= c.tags.len) return c.die();
+ const slot = &c.tags[got.tag];
+ const op = slot.op orelse return c.die();
+ if (slot.completed) return c.die();
+ const result: Result = switch (got.msg) {
+ .rerror => |m| if (op == .flush) return c.die() else .{ .fail = m.ename },
+ .rauth => |m| if (op != .auth) return c.die() else .{ .auth = m.aqid },
+ .rcreate => |m| if (op != .create) return c.die() else .{ .create = .{ .qid = m.qid, .iounit = m.iounit } },
+ .rremove => if (op != .remove) return c.die() else .remove,
+ .rwstat => if (op != .wstat) return c.die() else .wstat,
+ .rflush => blk: {
+ if (op != .flush) return c.die();
+ if (slot.oldtag < c.tags.len) c.tags[slot.oldtag] = .{};
+ break :blk .flush;
+ },
+ .rattach => |m| if (op != .attach) return c.die() else .{ .attach = m.qid },
+ .rwalk => |m| if (op != .walk or m.nwqid > slot.count or (m.nwqid == 0 and slot.count != 0)) return c.die() else .{
+ .walk = .{ .nwqid = m.nwqid, .wqid = m.wqid },
+ },
+ .ropen => |m| if (op != .open) return c.die() else .{
+ .open = .{ .qid = m.qid, .iounit = m.iounit },
+ },
+ .rread => |m| blk: {
+ if (op != .read) return c.die();
+ if (m.data.len > slot.count) return c.die();
+ break :blk .{ .read = m.data };
+ },
+ .rwrite => |m| if (op != .write or m.count > slot.count) return c.die() else .{ .write = m.count },
+ .rclunk => if (op != .clunk) return c.die() else .clunk,
+ .rstat => |m| if (op != .stat) return c.die() else .{ .stat = m.stat },
+ else => return c.die(),
+ };
+ slot.completed = true;
+ // Multiple flushes can reserve the same tag; flushing a flush can release
+ // a reservation on a completed original operation.
+ for (&c.tags, 0..) |*candidate, tag| {
+ if (candidate.completed and !c.reserved(@intCast(tag))) candidate.* = .{};
+ }
+ return .{ .tag = got.tag, .op = op, .result = result };
+ }
+
+ fn version(c: *@This(), got: Decoded) ?Done {
+ if (!c.versioning) return c.die();
+ if (got.tag != notag) return c.die();
+ const m = got.msg.rversion;
+ if (m.msize > c.asked or m.msize < msize_min) return c.die();
+ c.versioning = false;
+ if (std.mem.eql(u8, m.version, "9P2000")) {
+ c.msize = m.msize;
+ } else if (!std.mem.eql(u8, m.version, "unknown")) {
+ return c.die();
+ }
+ return .{ .tag = notag, .op = .version, .result = .{
+ .version = .{ .msize = m.msize, .version = m.version },
+ } };
+ }
+ };
+}
diff --git a/src/root.zig b/src/root.zig
index fb0bf5c..950fc26 100644
--- a/src/root.zig
+++ b/src/root.zig
@@ -33,6 +33,7 @@ pub const dmauth = wire.dmauth;
pub const dmtmp = wire.dmtmp;
pub const dmperm = wire.dmperm;
pub const Client = @import("client.zig").Client;
+pub const ClientWith = @import("client.zig").ClientWith;
pub const ClientError = @import("client.zig").ClientError;
pub const max_tags = @import("client.zig").max_tags;
pub const Server = @import("Server.zig");