From 5beb0167c3b9dc1a788e08b9c1652aaba8eb0a40 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Sun, 27 Sep 2026 17:56:32 -0300 Subject: 9ns: keep many 9P requests in flight, so a parked read no longer freezes the mount 9ns served one FUSE request at a time per 9P server, so a read the server parks (pardes's log, a pane's event) held up every other operation on the mount: with 'cat log' parked, 'cat status' took 3.5-4.5 s instead of ~1 ms. nine.zig: the session is shared by threads and multiplexes by tag the way Plan 9's mount driver does (devmnt.c mountio/mountmux/mntgate/ mntflushalloc). Each thread calls through its own nine.Caller (reply buffer, wake-up eventfd, interrupt source, ename). A call writes its T-message under wlock; the first waiter with nobody reading takes the reading baton, reads for everyone and hands each R-message to the caller its tag names, then passes the baton on. Tflush is per tag: a reply that beats the Rflush is honoured, the flushed tag is not reused before the Rflush, the baton is let go before the flush is written. With every tag taken a call waits for one (starved list) instead of failing. bridge.zig: one thread (the loop) still owns the inode/handle/qid tables and serves everything that touches them. READ and WRITE are resolved to their fid on the loop and handed to transfer workers (up to 31 per bridge, started on demand), which make the 9P calls on their own Caller and reply themselves. FUSE_INTERRUPT for a worker's request: still queued -> EINTR on the spot; in flight -> its unique down the worker's pipe -> Tflush of exactly that tag. Fids are clunked only on RELEASE. mntgen mounts get the same pool (their dispatcher routes interrupts to it); the mount interrupt pipe moved into Bridge. main.zig attaches through a Caller. Tests: two multiplexing tests in nine.zig (a parked read holds up only its caller and its interrupt flushes exactly its tag; with every tag parked a call waits for a tag), and a serve()-level test driving a fake /dev/fuse (a parked READ holds up neither READs nor GETATTR; INTERRUPT -> one Tflush for the parked tag -> EINTR; RELEASE). Also de-flaked the pre-existing 'short read' test (it raced its own server reply under load). Measured (own 9ns mount on the live pardes socket, read-only): with log parked, cat status 4498 ms before, 7 ms after. Tag window: client.zig gains ClientWith(n) (Client = ClientWith(16) as before; request/result types hoisted so all instantiations share them). 9ns uses ClientWith(32): 32 is fs.zig's default slot_capacity, so one 9ns mount can never have more parked on a cloud9 fs server than it parks, and never meets its "Resource temporarily unavailable" refusal. fs.zig is unchanged (its default is also the ESP32 board's, held to 24 KiB by its budget tests). Full window: the loop keeps its tag (metadata unaffected); the 32nd READ/WRITE waits in the pool queue (blocks, interruptible, never EAGAIN/EIO). More tests (bridge.zig, serve() over a fake /dev/fuse): one open is one fid (two opens of an inode read through distinct fids, concurrently, one of them parked); FLUSH on a handle with a parked read clunks nothing and the read later completes on that fid; only RELEASE clunks; a flushed READ whose reply beats the Rflush returns the data, not EINTR. --- src/client.zig | 658 +++++++++++++++++++++++++++++---------------------------- src/root.zig | 1 + 2 files changed, 340 insertions(+), 319 deletions(-) (limited to 'src') diff --git a/src/client.zig b/src/client.zig index 5b428dd..307cd7f 100644 --- a/src/client.zig +++ b/src/client.zig @@ -17,8 +17,6 @@ const frameLen = wire.frameLen; const Decoded = wire.Decoded; const totalLen = wire.encodedLen; pub const msize_min: u32 = 24; -pub const max_tags: usize = 16; - const twrite_header: usize = header_len + 4 + 8 + 4; const rread_header: usize = header_len + 4; @@ -26,7 +24,6 @@ const rread_header: usize = header_len + 4; comptime { assert(twrite_header == 23); assert(rread_header == 11); - assert(max_tags <= notag); assert(msize_min > rread_header); assert(msize_min > twrite_header); } @@ -40,353 +37,376 @@ pub const ClientError = error{ BadRequest, }; -pub const Client = struct { +/// The request window of the default `Client`: ordinary tags in flight at +/// once (one more is kept for a Tflush). +pub const max_tags: usize = 16; + +pub const Client = ClientWith(max_tags); + +pub const ClientOp = enum { version, auth, attach, flush, walk, open, create, read, write, clunk, remove, stat, wstat }; + +pub const ClientRequest = union(ClientOp) { + version: struct { msize: u32 = 0 }, + auth: struct { afid: u32, uname: []const u8, aname: []const u8 = "" }, + attach: struct { fid: u32, afid: u32 = nofid, uname: []const u8, aname: []const u8 = "" }, + flush: struct { oldtag: u16 }, + walk: struct { fid: u32, newfid: u32, names: []const []const u8 }, + open: struct { fid: u32, mode: u8 }, + create: struct { fid: u32, name: []const u8, perm: u32, mode: u8 }, + read: struct { fid: u32, offset: u64, count: u32 }, + write: struct { fid: u32, offset: u64, data: []const u8 }, + clunk: struct { fid: u32 }, + remove: struct { fid: u32 }, + stat: struct { fid: u32 }, + wstat: struct { fid: u32, stat: Stat }, +}; + +pub const ClientResult = union(enum) { + fail: []const u8, + version: struct { msize: u32, version: []const u8 }, + auth: Qid, + flush: void, + create: struct { qid: Qid, iounit: u32 }, + remove: void, + wstat: void, + attach: Qid, + walk: struct { nwqid: u16, wqid: [max_welem]Qid }, + open: struct { qid: Qid, iounit: u32 }, + read: []const u8, + write: u32, + clunk: void, + stat: Stat, +}; + +pub const ClientDone = struct { + tag: u16, + op: ClientOp, + result: ClientResult, +}; + +pub const ClientOptions = struct { in: []u8, out: []u8, +}; - in_len: usize = 0, - frame: u32 = 0, - out_len: usize = 0, - out_off: usize = 0, - - msize: u32 = 0, - asked: u32 = 0, - versioning: bool = false, - dead: bool = false, +/// A client with `tag_count` ordinary request tags, plus one kept so that a +/// full window can still be flushed. Every instantiation shares the request, +/// result and option types above. +pub fn ClientWith(comptime tag_count: usize) type { + comptime { + assert(tag_count >= 1); + assert(tag_count < notag); + } + return struct { + /// The ordinary request tags this client keeps in flight at most. + pub const window = tag_count; + in: []u8, + out: []u8, - tags: [max_tags + 1]Slot = @splat(.{}), + in_len: usize = 0, + frame: u32 = 0, + out_len: usize = 0, + out_off: usize = 0, - const Slot = struct { - op: ?Op = null, - count: u32 = 0, - oldtag: u16 = notag, - completed: bool = false, - }; + msize: u32 = 0, + asked: u32 = 0, + versioning: bool = false, + dead: bool = false, - pub const Op = enum { version, auth, attach, flush, walk, open, create, read, write, clunk, remove, stat, wstat }; - - pub const Request = union(Op) { - version: struct { msize: u32 = 0 }, - auth: struct { afid: u32, uname: []const u8, aname: []const u8 = "" }, - attach: struct { fid: u32, afid: u32 = nofid, uname: []const u8, aname: []const u8 = "" }, - flush: struct { oldtag: u16 }, - walk: struct { fid: u32, newfid: u32, names: []const []const u8 }, - open: struct { fid: u32, mode: u8 }, - create: struct { fid: u32, name: []const u8, perm: u32, mode: u8 }, - read: struct { fid: u32, offset: u64, count: u32 }, - write: struct { fid: u32, offset: u64, data: []const u8 }, - clunk: struct { fid: u32 }, - remove: struct { fid: u32 }, - stat: struct { fid: u32 }, - wstat: struct { fid: u32, stat: Stat }, - }; + tags: [tag_count + 1]Slot = @splat(.{}), - pub const Result = union(enum) { - fail: []const u8, - version: struct { msize: u32, version: []const u8 }, - auth: Qid, - flush: void, - create: struct { qid: Qid, iounit: u32 }, - remove: void, - wstat: void, - attach: Qid, - walk: struct { nwqid: u16, wqid: [max_welem]Qid }, - open: struct { qid: Qid, iounit: u32 }, - read: []const u8, - write: u32, - clunk: void, - stat: Stat, - }; + const Slot = struct { + op: ?Op = null, + count: u32 = 0, + oldtag: u16 = notag, + completed: bool = false, + }; - pub const Done = struct { - tag: u16, - op: Op, - result: Result, - }; + pub const Op = ClientOp; + pub const Request = ClientRequest; + pub const Result = ClientResult; + pub const Done = ClientDone; + pub const Options = ClientOptions; - pub const Options = struct { - in: []u8, - out: []u8, - }; + pub fn init(opts: Options) @This() { + assert(opts.in.len >= msize_min); + assert(opts.out.len >= msize_min); + return .{ .in = opts.in, .out = opts.out }; + } - pub fn init(opts: Options) Client { - assert(opts.in.len >= msize_min); - assert(opts.out.len >= msize_min); - return .{ .in = opts.in, .out = opts.out }; - } + pub fn hangup(c: *@This()) void { + c.dead = true; + c.tags = @splat(.{}); + c.versioning = false; + c.msize = 0; + c.asked = 0; + c.in_len = 0; + c.frame = 0; + c.out_len = 0; + c.out_off = 0; + } - pub fn hangup(c: *Client) void { - c.dead = true; - c.tags = @splat(.{}); - c.versioning = false; - c.msize = 0; - c.asked = 0; - c.in_len = 0; - c.frame = 0; - c.out_len = 0; - c.out_off = 0; - } + pub fn push(c: *@This(), bytes: []const u8) usize { + if (c.dead) return 0; + const n = @min(bytes.len, c.in.len - c.in_len); + @memcpy(c.in[c.in_len..][0..n], bytes[0..n]); + c.in_len += n; + return n; + } - pub fn push(c: *Client, bytes: []const u8) usize { - if (c.dead) return 0; - const n = @min(bytes.len, c.in.len - c.in_len); - @memcpy(c.in[c.in_len..][0..n], bytes[0..n]); - c.in_len += n; - return n; - } + pub fn output(c: *const @This()) []const u8 { + return c.out[c.out_off..c.out_len]; + } - pub fn output(c: *const Client) []const u8 { - return c.out[c.out_off..c.out_len]; - } + pub fn wrote(c: *@This(), n: usize) void { + assert(n <= c.out_len - c.out_off); + c.out_off += n; + if (c.out_off == c.out_len) { + c.out_off = 0; + c.out_len = 0; + } + } - pub fn wrote(c: *Client, n: usize) void { - assert(n <= c.out_len - c.out_off); - c.out_off += n; - if (c.out_off == c.out_len) { + fn compact(c: *@This()) void { + assert(c.out_off <= c.out_len); + const n = c.out_len - c.out_off; + std.mem.copyForwards(u8, c.out[0..n], c.out[c.out_off..c.out_len]); c.out_off = 0; - c.out_len = 0; + c.out_len = n; } - } - fn compact(c: *Client) void { - assert(c.out_off <= c.out_len); - const n = c.out_len - c.out_off; - std.mem.copyForwards(u8, c.out[0..n], c.out[c.out_off..c.out_len]); - c.out_off = 0; - c.out_len = n; - } - - fn dropFrame(c: *Client) void { - assert(c.frame != 0); - assert(c.frame <= c.in_len); - const n = c.frame; - std.mem.copyForwards(u8, c.in[0 .. c.in_len - n], c.in[n..c.in_len]); - c.in_len -= n; - c.frame = 0; - } + fn dropFrame(c: *@This()) void { + assert(c.frame != 0); + assert(c.frame <= c.in_len); + const n = c.frame; + std.mem.copyForwards(u8, c.in[0 .. c.in_len - n], c.in[n..c.in_len]); + c.in_len -= n; + c.frame = 0; + } - pub fn maxRead(c: *const Client) u32 { - if (c.msize == 0) return 0; - return c.msize - @as(u32, @intCast(rread_header)); - } + pub fn maxRead(c: *const @This()) u32 { + if (c.msize == 0) return 0; + return c.msize - @as(u32, @intCast(rread_header)); + } - pub fn maxWrite(c: *const Client) u32 { - if (c.msize == 0) return 0; - return c.msize - @as(u32, @intCast(twrite_header)); - } + pub fn maxWrite(c: *const @This()) u32 { + if (c.msize == 0) return 0; + return c.msize - @as(u32, @intCast(twrite_header)); + } - pub fn pending(c: *const Client) usize { - var n: usize = @intFromBool(c.versioning); - for (c.tags) |t| n += @intFromBool(t.op != null); - return n; - } + pub fn pending(c: *const @This()) usize { + var n: usize = @intFromBool(c.versioning); + for (c.tags) |t| n += @intFromBool(t.op != null); + return n; + } - pub fn submit(c: *Client, req: Request) ClientError!u16 { - if (c.dead) return error.Dead; - if (req == .version) return c.beginVersion(req.version.msize); - if (c.msize == 0 or c.versioning) return error.Handshake; - - const msg: Msg = switch (req) { - .version => unreachable, // handled above - .auth => |m| blk: { - if (m.afid == nofid) return error.BadRequest; - break :blk .{ .tauth = .{ .afid = m.afid, .uname = m.uname, .aname = m.aname } }; - }, - .flush => |m| blk: { - if (m.oldtag == notag) return error.BadRequest; - break :blk .{ .tflush = .{ .oldtag = m.oldtag } }; - }, - .create => |m| blk: { - if (m.fid == nofid or m.name.len == 0) return error.BadRequest; - if (std.mem.indexOfAny(u8, m.name, "/\x00") != null) return error.BadRequest; - if (std.mem.eql(u8, m.name, ".") or std.mem.eql(u8, m.name, "..")) return error.BadRequest; - break :blk .{ .tcreate = .{ .fid = m.fid, .name = m.name, .perm = m.perm, .mode = m.mode } }; - }, - .remove => |m| blk: { - if (m.fid == nofid) return error.BadRequest; - break :blk .{ .tremove = .{ .fid = m.fid } }; - }, - .wstat => |m| blk: { - if (m.fid == nofid) return error.BadRequest; - break :blk .{ .twstat = .{ .fid = m.fid, .stat = m.stat } }; - }, - .attach => |m| blk: { - if (m.fid == nofid) return error.BadRequest; - break :blk .{ .tattach = .{ - .fid = m.fid, - .afid = m.afid, - .uname = m.uname, - .aname = m.aname, - } }; - }, - .walk => |m| blk: { - if (m.fid == nofid or m.newfid == nofid) return error.BadRequest; - if (m.names.len > max_welem) return error.BadRequest; - var w: [max_welem][]const u8 = @splat(""); - for (m.names, 0..) |n, i| { - if (n.len == 0) return error.BadRequest; - if (std.mem.indexOfAny(u8, n, "/\x00") != null) return error.BadRequest; - w[i] = n; - } - break :blk .{ .twalk = .{ - .fid = m.fid, - .newfid = m.newfid, - .nwname = @intCast(m.names.len), - .wname = w, - } }; - }, - .open => |m| blk: { - if (m.fid == nofid) return error.BadRequest; - break :blk .{ .topen = .{ .fid = m.fid, .mode = m.mode } }; - }, - .read => |m| blk: { - if (m.fid == nofid) return error.BadRequest; - if (m.count > c.maxRead()) return error.TooLarge; - break :blk .{ .tread = .{ .fid = m.fid, .offset = m.offset, .count = m.count } }; - }, - .write => |m| blk: { - if (m.fid == nofid) return error.BadRequest; - break :blk .{ .twrite = .{ .fid = m.fid, .offset = m.offset, .data = m.data } }; - }, - .clunk => |m| blk: { - if (m.fid == nofid) return error.BadRequest; - break :blk .{ .tclunk = .{ .fid = m.fid } }; - }, - .stat => |m| blk: { - if (m.fid == nofid) return error.BadRequest; - break :blk .{ .tstat = .{ .fid = m.fid } }; - }, - }; - const need = totalLen(msg) catch return error.TooLarge; - if (need > c.msize) return error.TooLarge; - - const op = std.meta.activeTag(req); - const tag = c.claim(op, if (req == .flush) req.flush.oldtag else notag) orelse return error.NoTags; - errdefer c.tags[tag] = .{}; - try c.emit(tag, msg); - switch (req) { - .read => |m| c.tags[tag].count = m.count, - .write => |m| c.tags[tag].count = @intCast(m.data.len), - .walk => |m| c.tags[tag].count = @intCast(m.names.len), - .flush => |m| { - c.tags[tag].oldtag = m.oldtag; - }, - else => {}, + pub fn submit(c: *@This(), req: Request) ClientError!u16 { + if (c.dead) return error.Dead; + if (req == .version) return c.beginVersion(req.version.msize); + if (c.msize == 0 or c.versioning) return error.Handshake; + + const msg: Msg = switch (req) { + .version => unreachable, // handled above + .auth => |m| blk: { + if (m.afid == nofid) return error.BadRequest; + break :blk .{ .tauth = .{ .afid = m.afid, .uname = m.uname, .aname = m.aname } }; + }, + .flush => |m| blk: { + if (m.oldtag == notag) return error.BadRequest; + break :blk .{ .tflush = .{ .oldtag = m.oldtag } }; + }, + .create => |m| blk: { + if (m.fid == nofid or m.name.len == 0) return error.BadRequest; + if (std.mem.indexOfAny(u8, m.name, "/\x00") != null) return error.BadRequest; + if (std.mem.eql(u8, m.name, ".") or std.mem.eql(u8, m.name, "..")) return error.BadRequest; + break :blk .{ .tcreate = .{ .fid = m.fid, .name = m.name, .perm = m.perm, .mode = m.mode } }; + }, + .remove => |m| blk: { + if (m.fid == nofid) return error.BadRequest; + break :blk .{ .tremove = .{ .fid = m.fid } }; + }, + .wstat => |m| blk: { + if (m.fid == nofid) return error.BadRequest; + break :blk .{ .twstat = .{ .fid = m.fid, .stat = m.stat } }; + }, + .attach => |m| blk: { + if (m.fid == nofid) return error.BadRequest; + break :blk .{ .tattach = .{ + .fid = m.fid, + .afid = m.afid, + .uname = m.uname, + .aname = m.aname, + } }; + }, + .walk => |m| blk: { + if (m.fid == nofid or m.newfid == nofid) return error.BadRequest; + if (m.names.len > max_welem) return error.BadRequest; + var w: [max_welem][]const u8 = @splat(""); + for (m.names, 0..) |n, i| { + if (n.len == 0) return error.BadRequest; + if (std.mem.indexOfAny(u8, n, "/\x00") != null) return error.BadRequest; + w[i] = n; + } + break :blk .{ .twalk = .{ + .fid = m.fid, + .newfid = m.newfid, + .nwname = @intCast(m.names.len), + .wname = w, + } }; + }, + .open => |m| blk: { + if (m.fid == nofid) return error.BadRequest; + break :blk .{ .topen = .{ .fid = m.fid, .mode = m.mode } }; + }, + .read => |m| blk: { + if (m.fid == nofid) return error.BadRequest; + if (m.count > c.maxRead()) return error.TooLarge; + break :blk .{ .tread = .{ .fid = m.fid, .offset = m.offset, .count = m.count } }; + }, + .write => |m| blk: { + if (m.fid == nofid) return error.BadRequest; + break :blk .{ .twrite = .{ .fid = m.fid, .offset = m.offset, .data = m.data } }; + }, + .clunk => |m| blk: { + if (m.fid == nofid) return error.BadRequest; + break :blk .{ .tclunk = .{ .fid = m.fid } }; + }, + .stat => |m| blk: { + if (m.fid == nofid) return error.BadRequest; + break :blk .{ .tstat = .{ .fid = m.fid } }; + }, + }; + const need = totalLen(msg) catch return error.TooLarge; + if (need > c.msize) return error.TooLarge; + + const op = std.meta.activeTag(req); + const tag = c.claim(op, if (req == .flush) req.flush.oldtag else notag) orelse return error.NoTags; + errdefer c.tags[tag] = .{}; + try c.emit(tag, msg); + switch (req) { + .read => |m| c.tags[tag].count = m.count, + .write => |m| c.tags[tag].count = @intCast(m.data.len), + .walk => |m| c.tags[tag].count = @intCast(m.names.len), + .flush => |m| { + c.tags[tag].oldtag = m.oldtag; + }, + else => {}, + } + return tag; } - return tag; - } - fn beginVersion(c: *Client, want: u32) ClientError!u16 { - if (c.pending() != 0) return error.Handshake; - const cap: u32 = @intCast(@min(c.in.len, c.out.len, std.math.maxInt(u32))); - const m = @min(if (want == 0) cap else want, cap); - if (m < msize_min) return error.BadRequest; - try c.emit(notag, .{ .tversion = .{ .msize = m, .version = "9P2000" } }); - c.msize = 0; - c.asked = m; - c.versioning = true; - return notag; - } + fn beginVersion(c: *@This(), want: u32) ClientError!u16 { + if (c.pending() != 0) return error.Handshake; + const cap: u32 = @intCast(@min(c.in.len, c.out.len, std.math.maxInt(u32))); + const m = @min(if (want == 0) cap else want, cap); + if (m < msize_min) return error.BadRequest; + try c.emit(notag, .{ .tversion = .{ .msize = m, .version = "9P2000" } }); + c.msize = 0; + c.asked = m; + c.versioning = true; + return notag; + } - fn reserved(c: *const Client, tag: u16) bool { - for (c.tags) |slot| { - if (slot.op == .flush and !slot.completed and slot.oldtag == tag) return true; + fn reserved(c: *const @This(), tag: u16) bool { + for (c.tags) |slot| { + if (slot.op == .flush and !slot.completed and slot.oldtag == tag) return true; + } + return false; } - return false; - } - fn claim(c: *Client, op: Op, avoid: u16) ?u16 { - for (&c.tags, 0..) |*t, i| { - // Keep one tag available to cancel a fully occupied request window. - if (i >= max_tags and op != .flush) continue; - if (t.op != null or i == avoid or c.reserved(@intCast(i))) continue; - t.* = .{ .op = op }; - return @intCast(i); + fn claim(c: *@This(), op: Op, avoid: u16) ?u16 { + for (&c.tags, 0..) |*t, i| { + // Keep one tag available to cancel a fully occupied request window. + if (i >= tag_count and op != .flush) continue; + if (t.op != null or i == avoid or c.reserved(@intCast(i))) continue; + t.* = .{ .op = op }; + return @intCast(i); + } + return null; } - return null; - } - fn emit(c: *Client, tag: u16, msg: Msg) ClientError!void { - if (c.out_off != 0) c.compact(); - const bytes = encode(msg, tag, c.out[c.out_len..]) catch return error.NoSpace; - c.out_len += bytes.len; - } + fn emit(c: *@This(), tag: u16, msg: Msg) ClientError!void { + if (c.out_off != 0) c.compact(); + const bytes = encode(msg, tag, c.out[c.out_len..]) catch return error.NoSpace; + c.out_len += bytes.len; + } - pub fn take(c: *Client) ?Done { - if (c.frame != 0) c.dropFrame(); - if (c.dead) return null; - const len = frameLen(c.in[0..c.in_len]) orelse return null; - if (len < header_len or len > c.in.len) return c.die(); - if (c.msize != 0 and len > c.msize) return c.die(); - if (len > c.in_len) return null; - c.frame = len; - const got = decode(c.in[0..len]) catch return c.die(); - return c.consume(got); - } + pub fn take(c: *@This()) ?Done { + if (c.frame != 0) c.dropFrame(); + if (c.dead) return null; + const len = frameLen(c.in[0..c.in_len]) orelse return null; + if (len < header_len or len > c.in.len) return c.die(); + if (c.msize != 0 and len > c.msize) return c.die(); + if (len > c.in_len) return null; + c.frame = len; + const got = decode(c.in[0..len]) catch return c.die(); + return c.consume(got); + } - fn die(c: *Client) ?Done { - c.dead = true; - return null; - } + fn die(c: *@This()) ?Done { + c.dead = true; + return null; + } - fn consume(c: *Client, got: Decoded) ?Done { - if (isT(got.msg.msgType())) return c.die(); - if (got.msg == .rversion) return c.version(got); - if (c.versioning or c.msize == 0) return c.die(); - if (got.tag >= c.tags.len) return c.die(); - const slot = &c.tags[got.tag]; - const op = slot.op orelse return c.die(); - if (slot.completed) return c.die(); - const result: Result = switch (got.msg) { - .rerror => |m| if (op == .flush) return c.die() else .{ .fail = m.ename }, - .rauth => |m| if (op != .auth) return c.die() else .{ .auth = m.aqid }, - .rcreate => |m| if (op != .create) return c.die() else .{ .create = .{ .qid = m.qid, .iounit = m.iounit } }, - .rremove => if (op != .remove) return c.die() else .remove, - .rwstat => if (op != .wstat) return c.die() else .wstat, - .rflush => blk: { - if (op != .flush) return c.die(); - if (slot.oldtag < c.tags.len) c.tags[slot.oldtag] = .{}; - break :blk .flush; - }, - .rattach => |m| if (op != .attach) return c.die() else .{ .attach = m.qid }, - .rwalk => |m| if (op != .walk or m.nwqid > slot.count or (m.nwqid == 0 and slot.count != 0)) return c.die() else .{ - .walk = .{ .nwqid = m.nwqid, .wqid = m.wqid }, - }, - .ropen => |m| if (op != .open) return c.die() else .{ - .open = .{ .qid = m.qid, .iounit = m.iounit }, - }, - .rread => |m| blk: { - if (op != .read) return c.die(); - if (m.data.len > slot.count) return c.die(); - break :blk .{ .read = m.data }; - }, - .rwrite => |m| if (op != .write or m.count > slot.count) return c.die() else .{ .write = m.count }, - .rclunk => if (op != .clunk) return c.die() else .clunk, - .rstat => |m| if (op != .stat) return c.die() else .{ .stat = m.stat }, - else => return c.die(), - }; - slot.completed = true; - // Multiple flushes can reserve the same tag; flushing a flush can release - // a reservation on a completed original operation. - for (&c.tags, 0..) |*candidate, tag| { - if (candidate.completed and !c.reserved(@intCast(tag))) candidate.* = .{}; + fn consume(c: *@This(), got: Decoded) ?Done { + if (isT(got.msg.msgType())) return c.die(); + if (got.msg == .rversion) return c.version(got); + if (c.versioning or c.msize == 0) return c.die(); + if (got.tag >= c.tags.len) return c.die(); + const slot = &c.tags[got.tag]; + const op = slot.op orelse return c.die(); + if (slot.completed) return c.die(); + const result: Result = switch (got.msg) { + .rerror => |m| if (op == .flush) return c.die() else .{ .fail = m.ename }, + .rauth => |m| if (op != .auth) return c.die() else .{ .auth = m.aqid }, + .rcreate => |m| if (op != .create) return c.die() else .{ .create = .{ .qid = m.qid, .iounit = m.iounit } }, + .rremove => if (op != .remove) return c.die() else .remove, + .rwstat => if (op != .wstat) return c.die() else .wstat, + .rflush => blk: { + if (op != .flush) return c.die(); + if (slot.oldtag < c.tags.len) c.tags[slot.oldtag] = .{}; + break :blk .flush; + }, + .rattach => |m| if (op != .attach) return c.die() else .{ .attach = m.qid }, + .rwalk => |m| if (op != .walk or m.nwqid > slot.count or (m.nwqid == 0 and slot.count != 0)) return c.die() else .{ + .walk = .{ .nwqid = m.nwqid, .wqid = m.wqid }, + }, + .ropen => |m| if (op != .open) return c.die() else .{ + .open = .{ .qid = m.qid, .iounit = m.iounit }, + }, + .rread => |m| blk: { + if (op != .read) return c.die(); + if (m.data.len > slot.count) return c.die(); + break :blk .{ .read = m.data }; + }, + .rwrite => |m| if (op != .write or m.count > slot.count) return c.die() else .{ .write = m.count }, + .rclunk => if (op != .clunk) return c.die() else .clunk, + .rstat => |m| if (op != .stat) return c.die() else .{ .stat = m.stat }, + else => return c.die(), + }; + slot.completed = true; + // Multiple flushes can reserve the same tag; flushing a flush can release + // a reservation on a completed original operation. + for (&c.tags, 0..) |*candidate, tag| { + if (candidate.completed and !c.reserved(@intCast(tag))) candidate.* = .{}; + } + return .{ .tag = got.tag, .op = op, .result = result }; } - return .{ .tag = got.tag, .op = op, .result = result }; - } - fn version(c: *Client, got: Decoded) ?Done { - if (!c.versioning) return c.die(); - if (got.tag != notag) return c.die(); - const m = got.msg.rversion; - if (m.msize > c.asked or m.msize < msize_min) return c.die(); - c.versioning = false; - if (std.mem.eql(u8, m.version, "9P2000")) { - c.msize = m.msize; - } else if (!std.mem.eql(u8, m.version, "unknown")) { - return c.die(); + fn version(c: *@This(), got: Decoded) ?Done { + if (!c.versioning) return c.die(); + if (got.tag != notag) return c.die(); + const m = got.msg.rversion; + if (m.msize > c.asked or m.msize < msize_min) return c.die(); + c.versioning = false; + if (std.mem.eql(u8, m.version, "9P2000")) { + c.msize = m.msize; + } else if (!std.mem.eql(u8, m.version, "unknown")) { + return c.die(); + } + return .{ .tag = notag, .op = .version, .result = .{ + .version = .{ .msize = m.msize, .version = m.version }, + } }; } - return .{ .tag = notag, .op = .version, .result = .{ - .version = .{ .msize = m.msize, .version = m.version }, - } }; - } -}; + }; +} diff --git a/src/root.zig b/src/root.zig index fb0bf5c..950fc26 100644 --- a/src/root.zig +++ b/src/root.zig @@ -33,6 +33,7 @@ pub const dmauth = wire.dmauth; pub const dmtmp = wire.dmtmp; pub const dmperm = wire.dmperm; pub const Client = @import("client.zig").Client; +pub const ClientWith = @import("client.zig").ClientWith; pub const ClientError = @import("client.zig").ClientError; pub const max_tags = @import("client.zig").max_tags; pub const Server = @import("Server.zig"); -- cgit v1.3