diff options
Diffstat (limited to '9ns/src/nine.zig')
| -rw-r--r-- | 9ns/src/nine.zig | 1033 |
1 files changed, 816 insertions, 217 deletions
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()); +} |
