summaryrefslogtreecommitdiff
path: root/src/fs.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-09-27 20:46:28 -0300
committerGabriel Schneider <[email protected]>2026-09-27 20:55:19 -0300
commitcca47d63c3ba774cb4de6f76603ba6edc5b475fe (patch)
treea0e7c45ba191155be232240dbf53c9ae34f209ba /src/fs.zig
parent82d8152cd15ea3ddd238caec2072bc2a1327a578 (diff)
downloadcloud9-cca47d63c3ba774cb4de6f76603ba6edc5b475fe.tar.gz
cloud9-cca47d63c3ba774cb4de6f76603ba6edc5b475fe.zip
fs: a backend can keep a parked request by ticket and answer it later, and a clunk interrupts what is parked on its fid
A backend holding a read that has nothing yet had to find its park by walking the engine's slots and matching a tag. Now Conn.hold() answers .again and returns a Ticket (slot and that park's sequence number); Conn.waiting() says whether that very park still waits, and Conn.answerWith() answers it with what a producer makes, asking the producer only while it waits and under the same lock, the way lib9p lets a server keep a Req and respond() later, so a consuming answer is never spent on a flushed request. The sequence number counts on across the connections a runner slot serves (initIn keeps it), so a ticket of one never names a park of the next. A clunk or remove fails everything parked on its fid with EINTR first, so no slot outlives its fid to be retried with a released handle or answered to a fid of the same number walked anew; retry's check for a vanished fid is gone with it. 9ns maps acme's "shut down" to EIO. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Diffstat (limited to 'src/fs.zig')
-rw-r--r--src/fs.zig133
1 files changed, 114 insertions, 19 deletions
diff --git a/src/fs.zig b/src/fs.zig
index 2ab619a..9e7a25a 100644
--- a/src/fs.zig
+++ b/src/fs.zig
@@ -178,6 +178,13 @@ fn noPayload(comptime Payload: type) Payload {
/// or readdir travel beside it as the `bytes` argument of `reply()`.
pub const Reply = ReplyWith(void);
+/// A request the backend answered `.again`, kept to be answered later, the
+/// way lib9p lets a server keep a Req and respond() when it has something:
+/// the slot it parked in and that park's sequence number, which nothing
+/// parked there later will carry. Good on the connection that issued it,
+/// until that connection hangs up.
+pub const Ticket = struct { slot: u16, seq: u64 };
+
/// The errno values a backend may answer with; `errString` names each.
pub const E = struct {
pub const PERM: u16 = 1;
@@ -474,7 +481,10 @@ pub fn Server(comptime Backend: type, comptime opts: Options) type {
/// bump-allocated and nothing reads a slot at or past `high_water`,
/// so those slots are left unwritten: a large `fid_capacity` then
/// costs memory only for the fids a client actually holds, where
- /// `init` writes the whole table on every connection.
+ /// `init` writes the whole table on every connection. `seq` carries
+ /// over, so an engine set up again for its next connection never
+ /// repeats a backend tag or a `Ticket` of the last one: set it once
+ /// (to 0) before the first `initIn`, as `init` does.
pub fn initIn(s: *Self, o: InitOptions) void {
assert(o.in.len >= msize_min);
assert(o.out.len >= 2 * msize_min);
@@ -486,7 +496,6 @@ pub fn Server(comptime Backend: type, comptime opts: Options) type {
if (!opts.fid_index) s.fids = @splat(.{});
s.slots = @splat(.{});
s.job = .{};
- s.seq = 0;
s.nfids = 0;
s.norphans = 0;
s.index = @splat(no_slot);
@@ -728,17 +737,9 @@ pub fn Server(comptime Backend: type, comptime opts: Options) type {
return null;
}
const sl = &s.slots[i];
- if (sl.holdsJob()) {
- // Its fid may have been clunked while it waited: then there
- // is nobody to answer, and asking the backend would make it
- // do the work -- open a handle, make an object -- for no one.
- if (s.findFid(sl.fid) == null) {
- s.fail(sl.tag, e_unknown_fid);
- sl.* = .{};
- return s.retry();
- }
- return s.resumeJob(i);
- }
+ // A clunk of its fid while it waited took it out (`clunk`), so
+ // nothing here is asked again for no one.
+ if (sl.holdsJob()) return s.resumeJob(i);
sl.retried = true;
sl.parked = false;
return sl.req;
@@ -787,6 +788,34 @@ pub fn Server(comptime Backend: type, comptime opts: Options) type {
if (s.findSlot(r.tag)) |i| return s.slotReply(i, r, bytes);
}
+ /// The ticket of the request `req_tag` while it waits parked: right
+ /// after the backend answered it `.again`.
+ pub fn ticket(s: *const Self, req_tag: u64) ?Ticket {
+ for (&s.slots, 0..) |*sl, i| {
+ if (sl.used and sl.parked and sl.req.tag == req_tag) return .{ .slot = @intCast(i), .seq = sl.seq };
+ }
+ return null;
+ }
+
+ /// Whether the request `t` names still waits parked: not flushed,
+ /// its fid not clunked, the connection not hung up, and not out
+ /// being asked again by `retry()` (whose asking answers it instead).
+ pub fn waiting(s: *const Self, t: Ticket) bool {
+ if (t.slot >= s.slots.len) return false;
+ const sl = &s.slots[t.slot];
+ return sl.used and sl.parked and sl.seq == t.seq;
+ }
+
+ /// Answers the request `t` names, as `reply()` would; false, with
+ /// nothing sent, once it no longer waits. A backend whose answer
+ /// costs something to make (a record taken off a queue) asks
+ /// `waiting()` first, in the same hold of the engine.
+ pub fn answer(s: *Self, t: Ticket, r: *const Backend.Reply, bytes: []const u8) bool {
+ if (!s.waiting(t)) return false;
+ s.slotReply(t.slot, r, bytes);
+ return true;
+ }
+
fn orphan(s: *Self) ?Backend.Req {
if (s.norphans == 0) return null;
for (s.touched(), 0..) |*f, i| {
@@ -964,6 +993,14 @@ pub fn Server(comptime Backend: type, comptime opts: Options) type {
fn clunk(s: *Self, tag: u16, fid: u32, kind: Kind) void {
assert(kind == .clunk or kind == .remove);
const i = s.findFid(fid) orelse return s.fail(tag, e_unknown_fid);
+ // What is parked on the fid has nobody left to answer for: it is
+ // interrupted now, so that no slot outlives its fid, to be
+ // retried with a handle the backend has let go of or answered
+ // after a fid of the same number is walked anew.
+ for (&s.slots) |*sl| if (sl.used and sl.fid == fid) {
+ s.fail(sl.tag, e_interrupted);
+ sl.* = .{};
+ };
if (s.fids[i].open or refs or (kind == .remove and features.remove)) {
s.job = .{ .kind = kind, .tag = tag, .fid = fid };
return;
@@ -2266,22 +2303,23 @@ test "fs server: a parked open, truncate and clunk complete on retry, and a walk
try testing.expectEqualStrings(e_again, got.msg.rerror.ename);
h.fsys.park_lookups = false;
- // A parked open whose fid is clunked meanwhile is answered for the
- // clunk's sake and never asked again: the backend would otherwise open
- // a handle for nobody.
+ // A parked open whose fid is clunked meanwhile is interrupted by the
+ // clunk and never asked again: the backend would otherwise open a
+ // handle for nobody.
_ = try h.walkTo(16, 5, &.{ "1", "tag" });
h.fsys.park_opens = true;
try h.send(17, .{ .topen = .{ .fid = 5, .mode = oread } });
try h.quiet();
try h.send(18, .{ .tclunk = .{ .fid = 5 } });
got = try h.reap();
+ try testing.expectEqual(@as(u16, 17), got.tag);
+ try testing.expectEqualStrings(e_interrupted, got.msg.rerror.ename);
+ got = try h.reap();
try testing.expect(got.msg == .rclunk);
h.fsys.park_opens = false;
const asked = h.fsys.calls;
h.pump();
- got = try h.reap();
- try testing.expectEqual(@as(u16, 17), got.tag);
- try testing.expectEqualStrings(e_unknown_fid, got.msg.rerror.ename);
+ try h.quiet();
try testing.expectEqual(asked, h.fsys.calls);
// A flush reaches a parked job the way it reaches a parked read.
@@ -2354,6 +2392,62 @@ test "fs server: Tflush answers the original first and the Rflush second" {
try h.quiet();
}
+test "fs server: a ticket answers only the park it was given for" {
+ var h: Harness = .{};
+ try h.handshake(4096);
+ _ = try h.walkTo(5, 1, &.{ "1", "event" });
+ try h.send(6, .{ .topen = .{ .fid = 1, .mode = oread } });
+ _ = try h.reap();
+ const parked = struct {
+ /// The ticket of the one read parked now, taken by its backend tag.
+ fn ticket(hh: *Harness) !Ticket {
+ for (hh.srv.slots) |sl| if (sl.used and sl.parked) return hh.srv.ticket(sl.req.tag).?;
+ return error.NothingParked;
+ }
+ };
+
+ // Parked, the read can be answered later through its ticket, once.
+ try h.send(7, .{ .tread = .{ .fid = 1, .offset = 0, .count = 4096 } });
+ try h.quiet();
+ const t = try parked.ticket(&h);
+ try testing.expect(h.srv.waiting(t));
+ try testing.expect(h.srv.answer(t, &.{ .tag = 0 }, "held\n"));
+ var got = try h.reap();
+ try testing.expectEqual(@as(u16, 7), got.tag);
+ try testing.expectEqualStrings("held\n", got.msg.rread.data);
+ try testing.expect(!h.srv.answer(t, &.{ .tag = 0 }, "twice\n"));
+ try h.quiet();
+
+ // Flushed, and the tag used again for a read of another fid parked in
+ // the same slot: the old ticket answers neither.
+ try h.send(7, .{ .tread = .{ .fid = 1, .offset = 0, .count = 4096 } });
+ const old = try parked.ticket(&h);
+ try h.send(8, .{ .tflush = .{ .oldtag = 7 } });
+ _ = try h.reap();
+ _ = try h.reap();
+ _ = try h.walkTo(9, 2, &.{ "1", "event" });
+ try h.send(10, .{ .topen = .{ .fid = 2, .mode = oread } });
+ _ = try h.reap();
+ try h.send(7, .{ .tread = .{ .fid = 2, .offset = 0, .count = 4096 } });
+ const new = try parked.ticket(&h);
+ try testing.expectEqual(old.slot, new.slot);
+ try testing.expect(!h.srv.waiting(old));
+ try testing.expect(!h.srv.answer(old, &.{ .tag = 0 }, "misrouted\n"));
+ try h.quiet();
+
+ // Clunking a fid interrupts what is parked on it, and its ticket dies.
+ try h.send(11, .{ .tclunk = .{ .fid = 2 } });
+ got = try h.reap();
+ try testing.expectEqual(@as(u16, 7), got.tag);
+ try testing.expectEqualStrings(e_interrupted, got.msg.rerror.ename);
+ got = try h.reap();
+ try testing.expectEqual(@as(u16, 11), got.tag);
+ try testing.expect(got.msg == .rclunk);
+ try testing.expect(!h.srv.waiting(new));
+ for (h.srv.slots) |sl| try testing.expect(!sl.used);
+ try h.quiet();
+}
+
test "fs server: the fid and permission refusals, each in a string Linux knows" {
var h: Harness = .{};
try h.handshake(4096);
@@ -3400,6 +3494,7 @@ test "fs server: an engine set up in place never writes the fid slots it has not
defer testing.allocator.destroy(r);
r.* = .{ .fsys = .{} };
@memset(std.mem.asBytes(&r.srv), 0xA5);
+ r.srv.seq = 0; // the one field a runner writes when it is made
r.srv.initIn(.{ .in = &r.in, .out = &r.out, .root = 1, .seed = 0x4242 });
_ = try r.call(notag, .{ .tversion = .{ .msize = 4096, .version = "9P2000" } });
_ = try r.call(0, .{ .tattach = .{ .fid = 0, .afid = nofid, .uname = "goblin", .aname = "" } });