diff options
Diffstat (limited to '9ns/src')
| -rw-r--r-- | 9ns/src/bridge.zig | 846 | ||||
| -rw-r--r-- | 9ns/src/main.zig | 21 | ||||
| -rw-r--r-- | 9ns/src/nine.zig | 1033 |
3 files changed, 1533 insertions, 367 deletions
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()); +} |
