diff options
| author | Gabriel Schneider <[email protected]> | 2026-10-31 06:28:48 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-10-31 06:28:48 -0300 |
| commit | a479792a1ab2b593642d7f648194bfa23ce01dba (patch) | |
| tree | 96ddee7d879a9084daa07db43b0b7910b072a785 /src/9p_io.zig | |
| parent | e179cc44fe5060c76a5b00c41e53f11209f5acd9 (diff) | |
| parent | 4c761893e5b3fdae422622cfa02104ab36633045 (diff) | |
| download | pardes-a479792a1ab2b593642d7f648194bfa23ce01dba.tar.gz pardes-a479792a1ab2b593642d7f648194bfa23ce01dba.zip | |
Merge the upstream daemon and replicated client/server refactor
Diffstat (limited to 'src/9p_io.zig')
| -rw-r--r-- | src/9p_io.zig | 918 |
1 files changed, 802 insertions, 116 deletions
diff --git a/src/9p_io.zig b/src/9p_io.zig index a089a7cf..377440bd 100644 --- a/src/9p_io.zig +++ b/src/9p_io.zig @@ -12,7 +12,7 @@ const quic = if (quic_enabled) @import("9p_quic.zig") else struct {}; const log = std.log.scoped(.ninep); pub const darwin = builtin.os.tag.isDarwin(); -pub const supported = builtin.os.tag == .linux or darwin; +pub const supported = builtin.os.tag == .linux or builtin.os.tag == .freebsd or darwin; pub const sun_path_len = @typeInfo(@FieldType(libc.sockaddr.un, "path")).array.len; pub fn setCloexec(fd: c_int) void { @@ -29,7 +29,7 @@ pub fn socketDir(buf: *[sun_path_len:0]u8) ?[:0]const u8 { pub const FileFacts = struct { mode: u32, uid: libc.uid_t }; pub fn statNoFollow(path: [:0]const u8) ?FileFacts { - if (comptime darwin) { + if (comptime builtin.os.tag != .linux) { var stat: libc.Stat = undefined; if (libc.fstatat(libc.AT.FDCWD, path, &stat, libc.AT.SYMLINK_NOFOLLOW) != 0) return null; return .{ .mode = stat.mode, .uid = stat.uid }; @@ -195,6 +195,9 @@ pub const Listener = struct { paused_ms: i64 = 0, path_buf: [sun_path_len]u8 = undefined, path_len: usize = 0, + /// The socket's lock (`claim`), held for the listener's life; -1 when none. + lock: c_int = -1, + lock_buf: [lock_path_len:0]u8 = undefined, /// The registry entry this editor posted /// (`$XDG_RUNTIME_DIR/9p/pardes/<name>`), zero when it did not. posted_buf: [sun_path_len]u8 = undefined, @@ -721,6 +724,12 @@ pub const Listener = struct { /// and only between the editor's steps: a step out in a syscall may be /// reading one of these entries. fn collectOs(l: *Listener) void { + // A request out in a host syscall may be using an entry's path, or + // made one that no fid names yet (a walk's next element): swept at + // the next quiet instead. Taken back after a step of its own, the + // editor's frame (tty pollFrame) came here with one out, and a walk + // of a directory just made was answered ENOENT. + if (pardes.turn.out != 0) return; const core = l.core; var i: usize = 0; while (i < core.fs.os_paths.items.len) { @@ -893,6 +902,25 @@ pub const Listener = struct { l.posted_len = 0; } + /// What the connections have still to send let go, 200 ms at most, + /// before they are cut (a quit, an Upgrade's exec: its write's answer). + pub fn flushOut(l: *Listener) void { + if (comptime !supported) return; + const deadline = nowMs() +| 200; + while (nowMs() < deadline) { + const pending = for (&l.runner.conns) |*conn| { + if (!conn.live()) continue; + conn.lock(); + const n = conn.engine.output().len; + conn.unlock(); + if (n != 0) break true; + } else false; + if (!pending) break; + Client.nap(1); + } + Client.nap(1); // the last bytes leave the stream's own buffer + } + pub fn deinit(l: *Listener, gpa: std.mem.Allocator) void { l.stopping.store(true, .release); l.unpostFromRegistry(); @@ -913,19 +941,7 @@ pub const Listener = struct { l.edit_watchers.cancel(l.io); // An answer given as the editor quit (a Kill written to /ctl) // may still be on its way out: let it go before the cut. - const deadline = nowMs() +| 200; - while (nowMs() < deadline) { - const pending = for (&l.runner.conns) |*conn| { - if (!conn.live()) continue; - conn.lock(); - const n = conn.engine.output().len; - conn.unlock(); - if (n != 0) break true; - } else false; - if (!pending) break; - Client.nap(1); - } - Client.nap(1); // the last bytes leave the stream's own buffer + l.flushOut(); l.runner.stop(); pardes.turn.wake(); } @@ -939,6 +955,8 @@ pub const Listener = struct { z[l.path_len] = 0; _ = libc.unlink(z[0..l.path_len :0]); } + // The lock after the socket: a socket there never lacks a held lock. + if (l.lock >= 0) release(l.lock, std.mem.sliceTo(&l.lock_buf, 0)); freeRunner(l.runner); gpa.destroy(l); } @@ -961,27 +979,32 @@ fn freeRunner(runner: *Runner) void { /// The socket this process listens on, kept where a fatal signal's handler /// can reach it without allocating (unlinkOwnSocket); empty when none. -var own_socket: [sun_path_len:0]u8 = undefined; +var own_socket: [lock_path_len:0]u8 = undefined; var own_socket_len: usize = 0; -/// Removes the socket this process listens on. Async-signal-safe (one -/// unlink): SIGTERM and SIGHUP call it before the signal's own death, so a -/// killed editor leaves no socket behind. +/// Removes the socket this process listens on, then its lock (the death +/// lets go of it). Async-signal-safe (two unlinks): SIGTERM and SIGHUP call +/// it before the signal's own death, so a killed editor leaves no socket +/// behind. pub fn unlinkOwnSocket() void { if (comptime !supported) return; const n = own_socket_len; if (n == 0) return; own_socket_len = 0; _ = libc.unlink(own_socket[0..n :0]); + own_socket[n..][0..lock_suffix.len].* = lock_suffix.*; + own_socket[n + lock_suffix.len] = 0; + _ = libc.unlink(own_socket[0 .. n + lock_suffix.len :0]); } -/// SIGTERM and SIGHUP with no handler of their own (a GUI's): the socket -/// goes, then the signal's own death. A host that handles them itself (the -/// terminal's Killed, the detached server's quit) removes it its own way. +/// SIGTERM, SIGHUP and SIGINT with no handler of their own (a GUI's, a +/// detached session's): the socket goes, then the signal's own death. A +/// host that handles them itself (the terminal's Killed, SDL's quit) +/// removes it its own way. fn armSocketCleanup() void { const sig = std.posix.SIG; const on: std.posix.Sigaction = .{ .handler = .{ .handler = onFatalSignal }, .mask = std.posix.sigemptyset(), .flags = std.posix.SA.RESETHAND }; - for ([_]std.posix.SIG{ sig.TERM, sig.HUP }) |s| { + for ([_]std.posix.SIG{ sig.TERM, sig.HUP, sig.INT }) |s| { var was: std.posix.Sigaction = undefined; std.posix.sigaction(s, null, &was); if (was.handler.handler == sig.DFL) std.posix.sigaction(s, &on, null); @@ -995,6 +1018,8 @@ pub var fatal_cleanup: ?*const fn () void = null; fn onFatalSignal(s: std.posix.SIG) callconv(.c) void { unlinkOwnSocket(); if (fatal_cleanup) |cleanup| cleanup(); + // Its shells go with it, the daemon's dups let go. + @import("daemon.zig").hangUp(); // SA_RESETHAND put the default back: the same signal, now fatal. _ = std.c.raise(s); } @@ -1024,10 +1049,76 @@ pub fn sweepDeadSockets(io: std.Io, dir: [:0]const u8) void { const facts = statNoFollow(path) orelse continue; if (facts.mode & 0o170000 != 0o140000 or facts.uid != libc.getuid()) continue; if (probe(path) == .live) continue; - _ = libc.unlink(path); + removeDead(path); } } +/// A socket's lock, `<socket>.lock` beside it: flocked by the socket's owner +/// from before it binds to after it unlinks (`claim`, `release`), so held +/// is alive and free is dead. A connect cannot say that: FreeBSD refuses one +/// to a listener whose queue is full exactly as to a socket nobody listens +/// on, and a starting owner's socket refuses until it listens. +const lock_suffix = ".lock"; +const lock_path_len = sun_path_len + lock_suffix.len; + +fn lockPath(buf: *[lock_path_len:0]u8, sock: []const u8) ?[:0]const u8 { + return std.fmt.bufPrintSentinel(buf, "{s}" ++ lock_suffix, .{sock}, 0) catch null; +} + +/// `sock`'s lock taken (its path in `buf`), made when it is missing: null +/// while someone else holds it. A probe holds it shared for a moment, so a +/// refusal is asked again for a while before it counts. One taken on a file +/// its owner unlinked as it let go is taken again on the one at the path. +fn claim(sock: []const u8, buf: *[lock_path_len:0]u8) ?c_int { + const path = lockPath(buf, sock) orelse return null; + var tries: u32 = 0; + while (tries < 50) : (tries += 1) { + const fd = libc.open(path, .{ .ACCMODE = .RDWR, .CREAT = true, .CLOEXEC = true }, @as(libc.mode_t, 0o600)); + if (fd < 0) return null; + if (libc.flock(fd, std.posix.LOCK.EX | std.posix.LOCK.NB) != 0) { + _ = libc.close(fd); + Client.nap(1); + continue; + } + const io = std.Io.Threaded.global_single_threaded.io(); + const held = (std.Io.File{ .handle = fd, .flags = .{ .nonblocking = false } }).stat(io) catch null; + const named = std.Io.Dir.cwd().statFile(io, path, .{ .follow_symlinks = false }) catch null; + if (held != null and named != null and held.?.inode == named.?.inode) return fd; + _ = libc.close(fd); + } + return null; +} + +/// A claim let go, its lock file removed while still held: nobody else +/// unlinks it, and a claim on the unlinked file is taken again (`claim`). +fn release(fd: c_int, path: [:0]const u8) void { + _ = libc.unlink(path); + _ = libc.close(fd); +} + +/// What `sock`'s lock says of its owner: held is `.live`, free `.stale`, and +/// none at all `.none`, a socket of a pardes from before the locks, which a +/// connect judges. Anything unclear is `.live`. +fn lockState(sock: []const u8) Probe { + var buf: [lock_path_len:0]u8 = undefined; + const path = lockPath(&buf, sock) orelse return .live; + const fd = libc.open(path, .{ .ACCMODE = .RDONLY, .CLOEXEC = true }, @as(libc.mode_t, 0)); + if (fd < 0) return if (libc.errno(fd) == .NOENT) .none else .live; + defer _ = libc.close(fd); + return if (libc.flock(fd, std.posix.LOCK.SH | std.posix.LOCK.NB) == 0) .stale else .live; +} + +/// A dead owner's socket removed, and its lock, under that lock: no new +/// owner can come between the judging and the unlink. Under the lock, an +/// owner that took it is gone, and a socket of a pardes from before the +/// locks still has its connect to answer. +fn removeDead(sock: [:0]const u8) void { + var buf: [lock_path_len:0]u8 = undefined; + const fd = claim(sock, &buf) orelse return; + if (connectProbe(sock) == .stale) _ = libc.unlink(sock); + release(fd, std.mem.sliceTo(&buf, 0)); +} + /// The names in `dir` that `keep` takes (all with none), into `names` as a length byte and /// the name each, until it is full; the bytes staged. They are staged /// before anything is unlinked, so a sweep never asks a directory to keep @@ -1105,6 +1196,15 @@ pub fn listen(io: std.Io, gpa: std.mem.Allocator, core: *pardes.Pardes, named: [ .greet_timeout_ms = Listener.greet_deadline_ms, }); const p = socketPath(&l.path_buf, dir, entry_name).?; + // The name's lock first, held from here to the end: whoever else holds + // it is alive, however its socket answers a connect. Held, a socket + // there is a dead owner's, or one from a pardes before the locks, which + // the connects below still ask. + l.lock = claim(p, &l.lock_buf) orelse { + log.warn("something is already listening on {s}", .{p}); + l.deinit(gpa); + return null; + }; // Listened on under a name of its own, then renamed into place: a client // that waits for the socket to appear finds it listening already. Bound // at its own name, it was there a moment before listen(2), and a connect @@ -1204,6 +1304,17 @@ pub fn listen(io: std.Io, gpa: std.mem.Allocator, core: *pardes.Pardes, named: [ const alive = transport.isListening; +/// Whether a live session listens under `name` already: a second +/// `--detach=<name>` is refused before anything is made. +pub fn taken(name: []const u8) bool { + if (comptime !supported) return false; + var dir_buf: [sun_path_len:0]u8 = undefined; + const dir = socketDir(&dir_buf) orelse return false; + var buf: [sun_path_len]u8 = undefined; + const p = socketPath(&buf, dir, name) orelse return false; + return probe(p) == .live; +} + /// What is at a socket path, in cloud9's three answers (`post.Probe`). /// Asked here with libc rather than through `cloud9.post.probe`, whose /// raw Linux syscalls darwin cannot compile, but the classification is @@ -1221,7 +1332,18 @@ const Probe = enum { none, stale, live }; /// `fcntl` instead. const probe_socket_kind: c_uint = libc.SOCK.STREAM | (if (darwin) 0 else libc.SOCK.NONBLOCK); +/// Whether whoever made the socket at `path` (or the one a registry entry +/// there names) is alive: its lock decides (`lockState`), and a connect +/// only for a socket that has none. fn probe(path: [:0]const u8) Probe { + var link_buf: [sun_path_len]u8 = undefined; + const n = libc.readlink(path, &link_buf, link_buf.len); + const sock = if (n > 0 and n < link_buf.len and link_buf[0] == '/') link_buf[0..@intCast(n)] else path; + const locked = lockState(sock); + return if (locked == .none) connectProbe(path) else locked; +} + +fn connectProbe(path: [:0]const u8) Probe { if (path.len + 1 > sun_path_len) return .live; // cannot ask; assume occupied var addr: libc.sockaddr.un = .{ .path = @splat(0) }; @memcpy(addr.path[0 .. path.len + 1], path[0 .. path.len + 1]); @@ -1261,7 +1383,8 @@ fn probe(path: [:0]const u8) Probe { /// the somebody. It is only ever a symlink that is followed or /// unlinked, because that is the only thing `postToRegistry` makes and /// anything else under a name belongs to whoever put it there; and only -/// a definite refusal counts as gone. The socket a stale entry points +/// a lock let go (or, with none, a definite refusal) counts as gone +/// (`probe`). The socket a stale entry points /// at goes too, but not before a stat agrees it is a socket of ours: a /// plain file answers a connect with the same refusal. /// Is this the kind of path this editor is allowed to delete? A registry @@ -1310,12 +1433,10 @@ fn sweepRegistry(io: std.Io, svc: [:0]const u8, sockets: []const u8) void { if (!ourSocket(target, sockets)) continue; const t = statNoFollow(target) orelse continue; if (t.mode & 0o170000 != 0o140000 or t.uid != libc.getuid()) continue; - // Ask again, immediately before deleting. The first probe was of the - // ENTRY and is by now several syscalls old; a socket that is bound but - // has not reached listen(2) yet answers ECONNREFUSED exactly like a - // dead one, and that window is every server's startup. - if (probe(target) != .stale) continue; - _ = libc.unlink(target); + // Judged again, under its lock: the first probe was of the ENTRY and + // is by now several syscalls old, and a new owner may have claimed + // the name since. + removeDead(target); } if (reaped != 0) log.info("reaped {d} stale registry entries under {s}", .{ reaped, svc }); } @@ -1414,21 +1535,42 @@ test "the registry sweep takes the dead entries and leaves everything else" { const outsider_sock = try std.fmt.bufPrintSentinel(&paths[5], "{s}/sweep-outsider-{d}.sock", .{ base, pid }, 0); var outsider_buf: [sun_path_len:0]u8 = undefined; const outsider = try std.fmt.bufPrintSentinel(&outsider_buf, "{s}/outsider", .{svc}, 0); + // Live, its lock held, but bound and not listening: a connect is + // refused, on every system, as FreeBSD refuses one to a live session + // whose queue is full. Its lock is what says it lives. + var starting_paths: [2][sun_path_len:0]u8 = undefined; + const starting_sock = try std.fmt.bufPrintSentinel(&starting_paths[0], "{s}/pardes-9p-sweepstarting-{d}.sock", .{ base, pid }, 0); + const starting = try std.fmt.bufPrintSentinel(&starting_paths[1], "{s}/starting", .{svc}, 0); + var locks: [3][lock_path_len:0]u8 = undefined; + const dead_lock = lockPath(&locks[2], dead_sock).?; defer { - for ([_][:0]const u8{ live_sock, dead_sock, live, dead, stranger, outsider_sock, outsider }) |p| _ = libc.unlink(p); + for ([_][:0]const u8{ live_sock, dead_sock, live, dead, stranger, outsider_sock, outsider, starting_sock, starting, dead_lock }) |p| _ = libc.unlink(p); _ = libc.rmdir(svc); } + const live_lock = claim(live_sock, &locks[0]) orelse return error.LockFailed; + defer release(live_lock, std.mem.sliceTo(&locks[0], 0)); const listening = bindSocket(live_sock); try testing.expect(listening >= 0); defer _ = libc.close(listening); try testing.expectEqual(@as(c_int, 0), libc.listen(listening, 1)); - // Bound and then dropped: the file stays, and nobody answers it — - // exactly what an aborted test leaves behind. + const starting_lock = claim(starting_sock, &locks[1]) orelse return error.LockFailed; + defer release(starting_lock, std.mem.sliceTo(&locks[1], 0)); + const bound = bindSocket(starting_sock); + try testing.expect(bound >= 0); + defer _ = libc.close(bound); + try testing.expectEqual(@as(c_int, 0), libc.symlink(starting_sock, starting)); + + // Bound and then dropped, its lock let go: the files stay, and nobody + // answers it -- exactly what a killed session leaves behind. const abandoned = bindSocket(dead_sock); try testing.expect(abandoned >= 0); _ = libc.close(abandoned); + const dropped = libc.open(dead_lock, .{ .ACCMODE = .RDWR, .CREAT = true, .CLOEXEC = true }, @as(libc.mode_t, 0o600)); + try testing.expect(dropped >= 0); + try testing.expectEqual(@as(c_int, 0), libc.flock(dropped, std.posix.LOCK.EX)); + _ = libc.close(dropped); try testing.expectEqual(@as(c_int, 0), libc.symlink(live_sock, live)); try testing.expectEqual(@as(c_int, 0), libc.symlink(dead_sock, dead)); @@ -1485,6 +1627,9 @@ test "the registry sweep takes the dead entries and leaves everything else" { try testing.expect(statNoFollow(dead) == null); try testing.expect(statNoFollow(dead_sock) == null); + try testing.expect(statNoFollow(dead_lock) == null); + try testing.expect(statNoFollow(starting) != null); + try testing.expect(statNoFollow(starting_sock) != null); try testing.expect(statNoFollow(live) != null); try testing.expect(statNoFollow(live_sock) != null); try testing.expect(statNoFollow(stranger) != null); @@ -1616,8 +1761,9 @@ test "a listening editor posts itself into the 9P registry and unposts on stop" // adopting the registry moves nothing, it only advertises. try testing.expect(statNoFollow(sock) != null); // It appeared already listening, renamed into place from a name of - // its own, which is gone. + // its own, which is gone; its lock held all the while. try testing.expect(alive(sock)); + try testing.expectEqual(Probe.live, lockState(sock)); var tmp_buf: [sun_path_len:0]u8 = undefined; try testing.expect(statNoFollow(try std.fmt.bufPrintSentinel(&tmp_buf, "{s}.{d}", .{ sock, libc.getpid() }, 0)) == null); // And the registry holds a symlink to it one directory down, so @@ -1627,10 +1773,20 @@ test "a listening editor posts itself into the 9P registry and unposts on stop" const n = libc.readlink(entry, &link, link.len); try testing.expect(n > 0); try testing.expectEqualStrings(sock, link[0..@intCast(n)]); + // A host path no fid names is swept, but never while a request is + // out in a syscall: it may be that request's, mid-walk. + try p.fs.os_paths.append(gpa, .{ .node = @import("fs.zig").os_node | 1, .path = try gpa.dupe(u8, "/tmp") }); + pardes.turn.out = 1; + l.collectOs(); + pardes.turn.out = 0; + try testing.expectEqual(@as(usize, 1), p.fs.os_paths.items.len); + l.collectOs(); + try testing.expectEqual(@as(usize, 0), p.fs.os_paths.items.len); } // Stopping takes the name back out, so the next editor of that name - // is not refused by its own corpse. + // is not refused by its own corpse; the lock goes with the socket. try testing.expect(libc.readlink(entry, &link, link.len) < 0); + try testing.expectEqual(Probe.none, lockState(sock)); } test "Unix TCP and QUIC share one listener through reads writes reconnects and reset" { @@ -1821,9 +1977,98 @@ test "a change waits while the editor is out mid-step, a read does not, and the pardes.turn.wake(); try testing.expect(writer.done.load(.acquire)); if (writer.failure) |err| return err; + // Its bytes are held until its open is let go (pane.zig `batchFor`), and + // the client lets it go without waiting: rested again until that lands, + // the body read only while the editor has the turn. + while (!std.mem.eql(u8, pane.file.?.content, "after\n") and Client.nowMs() < deadline) { + pardes.turn.rest(); + Client.nap(1); + pardes.turn.wake(); + } try testing.expectEqualStrings("after\n", pane.file.?.content); } +test "a mount's connection is kept between operations, and one the peer hung up is dialled again" { + if (comptime !supported) return error.SkipZigTest; + const gpa = testing.allocator; + var directory: [64:0]u8 = undefined; + _ = try std.fmt.bufPrintSentinel(&directory, "/tmp/pardes-kept-XXXXXX", .{}, 0); + if (mkdtemp(&directory) == null) return error.TempDirectoryFailed; + defer std.Io.Dir.cwd().deleteTree(testing.io, std.mem.sliceTo(&directory, 0)) catch |err| std.debug.print("deleteTree: {s}\n", .{@errorName(err)}); + const old_runtime = if (libc.getenv("XDG_RUNTIME_DIR")) |v| try gpa.dupeSentinel(u8, std.mem.span(v), 0) else null; + defer { + if (old_runtime) |v| { + _ = setenv("XDG_RUNTIME_DIR", v, 1); + gpa.free(v); + } else _ = @import("host_io.zig").testUnsetenv("XDG_RUNTIME_DIR"); + } + try testing.expectEqual(@as(c_int, 0), setenv("XDG_RUNTIME_DIR", &directory, 1)); + + const p = try pardes.Pardes.init(gpa, .{ .tty_only = true, .cols = 40, .rows = 12 }); + defer p.deinit(); + const pane = try p.setTestFile("before\n"); + while (p.nextEffect()) |_| {} + const l = listen(testing.io, gpa, p, "kept", "", null, null) orelse return error.ListenFailed; + defer { + l.reset(p); + l.deinit(gpa); + } + var body_buf: [64]u8 = undefined; + const body = try std.fmt.bufPrint(&body_buf, "/pane/{d}/body", .{pane.serial}); + const long = try gpa.alloc(u8, 2 * msize + 5); + defer gpa.free(long); + @memset(long, 'k'); + long[long.len - 1] = '\n'; + + const Worker = struct { + l: *Listener, + body: []const u8, + long: []const u8, + done: std.atomic.Value(bool) = .init(false), + failure: ?anyerror = null, + fn run(w: *@This()) void { + defer w.done.store(true, .release); + w.check() catch |err| { + w.failure = err; + }; + } + fn read(w: *@This(), want: []const u8) !void { + const got = try Client.readMount(testing.allocator, w.l.path(), w.body, w.body, limits.max_file_bytes); + defer testing.allocator.free(got); + try testing.expectEqualStrings(want, got); + } + fn check(w: *@This()) !void { + defer Client.forget(w.l.path()); + try w.read("before\n"); + try w.read("before\n"); + // A write longer than a message, and a miss, on the same one. + try Client.writeMount(testing.allocator, w.l.path(), w.body, w.long); + try w.read(w.long); + try testing.expectError(error.NotFound, Client.readMount(testing.allocator, w.l.path(), "/pane/no-such", "/pane/no-such", 64)); + try testing.expectEqual(@as(u64, 1), w.l.accepts.load(.acquire)); + // The peer hangs up (a Restore cuts its clients): the next + // operation dials again and is answered. + for (&w.l.runner.conns) |*conn| if (conn.live()) conn.close(); + const by = Client.nowMs() + Client.budget_ms; + while (w.l.runner.count() != 0 and Client.nowMs() < by) Client.nap(1); + try w.read(w.long); + try testing.expectEqual(@as(u64, 2), w.l.accepts.load(.acquire)); + } + }; + var worker: Worker = .{ .l = l, .body = body, .long = long }; + pardes.turn.rest(); + const thread = try std.Thread.spawn(.{}, Worker.run, .{&worker}); + const deadline = Client.nowMs() + 3 * Client.budget_ms; + while (!worker.done.load(.acquire) and Client.nowMs() < deadline) Client.nap(1); + thread.join(); + // Forgotten: the kept connection is closed, and its task sees it. + const settle_by = Client.nowMs() + Client.budget_ms; + while (l.runner.count() != 0 and Client.nowMs() < settle_by) Client.nap(1); + pardes.turn.wake(); + if (worker.failure) |err| return err; + try testing.expectEqual(@as(usize, 0), l.runner.count()); +} + test "a connection holds up to 128 reads, and the next is refused in words" { try testing.expectEqual(@as(usize, 128), ninep.max_held); if (comptime supported) try testing.expectEqualStrings("too many reads waiting: 128", Runner.Engine.e_waiting); @@ -2162,11 +2407,20 @@ pub const Client = struct { } pub fn readLimit(gpa: std.mem.Allocator, dial: []const u8, path: []const u8, display_path: []const u8, max_bytes: usize) ![]u8 { + return readWith(false, gpa, dial, path, display_path, max_bytes); + } + + /// readLimit through a mount: its connection kept for the next (`kept`). + pub fn readMount(gpa: std.mem.Allocator, dial: []const u8, path: []const u8, display_path: []const u8, max_bytes: usize) ![]u8 { + return readWith(true, gpa, dial, path, display_path, max_bytes); + } + + fn readWith(keep: bool, gpa: std.mem.Allocator, dial: []const u8, path: []const u8, display_path: []const u8, max_bytes: usize) ![]u8 { if (comptime !supported) return error.Unsupported; var names: [max_depth][]const u8 = undefined; const n = try elements(path, &names); var remote: RemoteError = .{}; - return fetchBytes(gpa, dial, names[0..n], &remote, null, display_path, @min(max_bytes, limits.max_file_bytes)) catch |err| { + return fetchBytes(gpa, dial, names[0..n], &remote, null, display_path, @min(max_bytes, limits.max_file_bytes), keep) catch |err| { if (err == error.Remote) said = remote; return err; }; @@ -2182,7 +2436,16 @@ pub const Client = struct { /// Whether a peer answers at `dial`: a version and attach, and its root /// read. Any answer from the peer, an error of its own included, is one. pub fn probe(gpa: std.mem.Allocator, dial: []const u8) !void { - const got = readLimit(gpa, dial, "", "", 0) catch |err| switch (err) { + return probeWith(false, gpa, dial); + } + + /// probe for a Mount: the connection kept as the mount's. + pub fn probeMount(gpa: std.mem.Allocator, dial: []const u8) !void { + return probeWith(true, gpa, dial); + } + + fn probeWith(keep: bool, gpa: std.mem.Allocator, dial: []const u8) !void { + const got = readWith(keep, gpa, dial, "", "", 0) catch |err| switch (err) { error.IsDirectory, error.NotFound, error.Remote, error.FileTooLarge => return, else => return err, }; @@ -2190,11 +2453,20 @@ pub const Client = struct { } pub fn write(gpa: std.mem.Allocator, dial: []const u8, path: []const u8, bytes: []const u8) !void { + return writeWith(false, gpa, dial, path, bytes); + } + + /// write through a mount: its connection kept for the next (`kept`). + pub fn writeMount(gpa: std.mem.Allocator, dial: []const u8, path: []const u8, bytes: []const u8) !void { + return writeWith(true, gpa, dial, path, bytes); + } + + fn writeWith(keep: bool, gpa: std.mem.Allocator, dial: []const u8, path: []const u8, bytes: []const u8) !void { if (comptime !supported) return error.Unsupported; var names: [max_depth][]const u8 = undefined; const n = try elements(path, &names); var remote: RemoteError = .{}; - const result = fetchBytes(gpa, dial, names[0..n], &remote, bytes, path, limits.max_file_bytes) catch |err| { + const result = fetchBytes(gpa, dial, names[0..n], &remote, bytes, path, limits.max_file_bytes, keep) catch |err| { if (err == error.Remote) said = remote; return err; }; @@ -2206,9 +2478,13 @@ pub const Client = struct { len: usize = 0, fn set(r: *RemoteError, msg: []const u8) error{Remote} { + r.keep(msg); + return error.Remote; + } + + fn keep(r: *RemoteError, msg: []const u8) void { r.len = @min(msg.len, r.buf.len); @memcpy(r.buf[0..r.len], msg[0..r.len]); - return error.Remote; } }; @@ -2262,6 +2538,18 @@ pub const Client = struct { in: [msize]u8 = undefined, out: [msize]u8 = undefined, stage: [msize]u8 = undefined, + /// Version and attach answered: fid 0 is the peer's root, and each + /// operation walks a fid of its own from it, so a kept connection + /// (`kept`) pays for neither again. + attached: bool = false, + root_qid: ninep.Qid = undefined, + next_fid: u32 = 1, + /// Tags whose answers nobody waits for (a clunk's): dropped by + /// whoever settles next. + unwaited: u32 = 0, + /// The dial a kept connection answers for. + dial_buf: [sun_path_len + 32]u8 = undefined, + dial_len: usize = 0, fn wait(s: *Session, events: i16) Error!void { while (true) { @@ -2314,7 +2602,13 @@ pub const Client = struct { while (true) { if (nowMs() >= s.deadline) return Error.Timeout; try s.flush(); - if (s.cl.take()) |done| return done; + if (s.cl.take()) |done| { + if (done.tag < 32 and s.unwaited & bit(done.tag) != 0) { + s.unwaited &= ~bit(done.tag); + continue; + } + return done; + } if (s.cl.dead) return Error.Botch; const room = s.cl.in.len - s.cl.in_len; if (room == 0) return Error.Botch; @@ -2352,39 +2646,290 @@ pub const Client = struct { } fn dropNoWait(s: *Session, fid: u32) void { - _ = s.cl.submit(.{ .clunk = .{ .fid = fid } }) catch return; + const tag = s.cl.submit(.{ .clunk = .{ .fid = fid } }) catch return; + s.unwaited |= bit(tag); s.flush() catch {}; } + + fn send(s: *Session, req: ninep.Client.Request) Error!u16 { + return s.cl.submit(req) catch Error.Botch; + } + + fn newFid(s: *Session) u32 { + const fid = s.next_fid; + s.next_fid = if (fid >= 1 << 20) 1 else fid + 1; + return fid; + } + + /// A new connection's Tversion and Tattach, sent with the first + /// operation's requests behind them in one flight (`land`). + fn greet(s: *Session, f: *Flight) Error!void { + if (s.attached) return; + _ = f.add(.version, try s.send(.{ .version = .{} })); + // ponytail: cloud9's client submits nothing while a Tversion is + // out, so its msize is taken as asked until the Rversion lands + // (`land` puts it back); only a pardes is dialled here, and every + // one offers ninep.msize. A Twrite in this flight stays under the + // least a listener offers (`flight_write`). The fix is cloud9's + // client taking requests while it versions. + s.cl.versioning = false; + s.cl.msize = s.cl.asked; + _ = f.add(.attach, try s.send(.{ .attach = .{ .fid = 0, .uname = uname } })); + } + + /// The walk to a new fid at `names` (two Twalks past 16 names, the + /// second from the fid to itself), from the root. + fn walkNew(s: *Session, f: *Flight, names: []const []const u8) Error!u32 { + const fid = s.newFid(); + const first = names[0..@min(names.len, ninep.max_welem)]; + f.add(.walk, try s.send(.{ .walk = .{ .fid = 0, .newfid = fid, .names = first } })).asked = @intCast(first.len); + if (names.len > first.len) + f.add(.walk, try s.send(.{ .walk = .{ .fid = fid, .newfid = fid, .names = names[first.len..] } })).asked = @intCast(names.len - first.len); + return fid; + } + + /// Sends the flight and takes every answer of it, a read's bytes + /// into `sink` as they come (each valid only until the next), so + /// that the connection is left with nothing out but its clunks. A + /// refused Tversion is Remote at once: nothing after it is answered. + fn land(s: *Session, f: *Flight, sink: anytype) Error!void { + if (f.get(.version) != null) s.cl.versioning = true; + while (f.open()) { + const done = try s.settle(); + const item = f.find(done.tag) orelse return Error.Botch; + item.done = true; + switch (done.result) { + .fail => |words| { + item.failed = true; + item.said.keep(words); + if (item.step == .version) return Error.Remote; + }, + .version => { + if (item.step != .version or s.cl.msize == 0) return Error.Botch; + }, + .attach => |qid| { + if (item.step != .attach) return Error.Botch; + s.root_qid = qid; + s.attached = true; + }, + .walk => |w| { + if (item.step != .walk) return Error.Botch; + item.n = w.nwqid; + if (w.nwqid > 0) item.qid = w.wqid[w.nwqid - 1]; + }, + .stat => |st| { + if (item.step != .stat) return Error.Botch; + item.n = st.length; + }, + .open => |o| { + if (item.step != .open) return Error.Botch; + item.qid = o.qid; + }, + .read => |data| { + if (item.step != .read) return Error.Botch; + item.n = data.len; + sink.take(item, data); + // A read is timed by its progress, as a long write + // is: each answer gives the rest the budget again, so + // a large file over a slow link is not cut off. + s.deadline = @max(s.deadline, nowMs() +| budget_ms); + }, + .write => |count| { + if (item.step != .write) return Error.Botch; + item.n = count; + }, + else => return Error.Botch, + } + } + } }; - /// Version, attach and a walk to `names`: the fid there and its qid. - fn walkTo(s: *Session, names: []const []const u8, remote: *RemoteError) !struct { fid: u32, qid: ninep.Qid } { - _ = try s.ask(.{ .version = .{} }, remote); - if (s.cl.msize == 0) return Error.Botch; + fn bit(tag: u16) u32 { + return @as(u32, 1) << @intCast(tag); + } - const root: u32 = 0; - var here = (try s.ask(.{ .attach = .{ .fid = root, .uname = uname } }, remote)).attach; - var cur: u32 = root; - var next: u32 = 1; + /// The requests of one flight, each its own tag, sent back to back: a + /// pardes takes one connection's requests in order (cloud9's engine + /// starts a request once the one before it is answered or parked), so + /// a walk, the open of its fid and the first read or write need no + /// answer between them, and an operation costs one round trip. + const Flight = struct { + items: [ninep.Client.window]Item = undefined, + len: usize = 0, - var i: usize = 0; - while (i < names.len) { - const n = @min(ninep.max_welem, names.len - i); - const w = (try s.ask(.{ .walk = .{ - .fid = cur, - .newfid = next, - .names = names[i..][0..n], - } }, remote)).walk; - if (w.nwqid != n) return Error.NotFound; - here = w.wqid[n - 1]; - if (cur != root) s.drop(cur); - cur = next; - next = if (next == 1) 2 else 1; - i += n; + const Step = enum { version, attach, walk, stat, open, read, write }; + const Item = struct { + step: Step, + tag: u16, + done: bool = false, + failed: bool = false, + said: RemoteError = .{}, + /// walk: names asked; read: its offset and count. + asked: u16 = 0, + off: u64 = 0, + count: u32 = 0, + /// walk: qids answered; stat: the length; read, write: bytes. + n: u64 = 0, + qid: ninep.Qid = undefined, + }; + + fn add(f: *Flight, step: Step, tag: u16) *Item { + f.items[f.len] = .{ .step = step, .tag = tag }; + f.len += 1; + return &f.items[f.len - 1]; + } + + fn find(f: *Flight, tag: u16) ?*Item { + for (f.items[0..f.len]) |*item| if (item.tag == tag and !item.done) return item; + return null; + } + + fn get(f: *Flight, step: Step) ?*Item { + for (f.items[0..f.len]) |*item| if (item.step == step) return item; + return null; + } + + fn open(f: *const Flight) bool { + for (f.items[0..f.len]) |item| if (!item.done) return true; + return false; + } + + /// The first refusal in the order asked: a version's or an attach's + /// words, a walk short of its names, an open's refusal. + fn check(f: *Flight, remote: *RemoteError) Error!void { + for (f.items[0..f.len]) |*item| switch (item.step) { + .version, .attach, .open => if (item.failed) return remote.set(item.said.buf[0..item.said.len]), + .walk => { + if (item.failed) return remote.set(item.said.buf[0..item.said.len]); + if (item.n != item.asked) return Error.NotFound; + }, + .stat, .read, .write => {}, + }; + } + + /// The walked file's qid: the last walk's, or the root's for none. + fn qid(f: *Flight, root: ninep.Qid) ninep.Qid { + var at = root; + for (f.items[0..f.len]) |item| if (item.step == .walk and item.n > 0) { + at = item.qid; + }; + return at; + } + + /// Whether the walk made the fid, so that it is owed a clunk. + fn made(f: *Flight) bool { + const first = f.get(.walk) orelse return false; + return !first.failed and first.n == first.asked; } - return .{ .fid = cur, .qid = here }; + }; + + /// A write in a new connection's first flight is sent before the msize + /// is known: no larger than the least a pardes listener offers. + const flight_write = ninep.min_msize - 23; + + /// Reads ahead this many Treads at once (the window, less a clunk + /// still out and one spare). + const read_ahead = ninep.Client.window - 2; + + /// Version and attach on a new connection, and a walk to `names`, in + /// one flight: the fid there and its qid. + fn walkTo(s: *Session, names: []const []const u8, remote: *RemoteError) !struct { fid: u32, qid: ninep.Qid } { + var f: Flight = .{}; + try s.greet(&f); + const fid = try s.walkNew(&f, names); + try landChecked(s, &f, NoSink{}, remote); + f.check(remote) catch |err| { + if (f.made()) s.dropNoWait(fid); + return err; + }; + return .{ .fid = fid, .qid = f.qid(s.root_qid) }; } + /// `land`, saying a refused Tversion in its own words: nothing else of + /// that flight is answered. + fn landChecked(s: *Session, f: *Flight, sink: anytype, remote: *RemoteError) Error!void { + s.land(f, sink) catch |err| { + if (err == Error.Remote) try f.check(remote); + return err; + }; + } + + const NoSink = struct { + fn take(_: NoSink, _: *Flight.Item, _: []const u8) void {} + }; + + /// A read's bytes taken in order: a file's as they are, a directory's + /// entries as rows of names. A read ahead that came back out of order, + /// or past one that came back short, is not taken: what it held is + /// read again from where the taken bytes end. + const Taker = struct { + out: *std.Io.Writer.Allocating, + display_path: []const u8, + limit: usize, + /// The flight with the walk, whose answer says what was reached: + /// it lands before any read (a walk never parks). + flight: *Flight, + s: *const Session, + /// Bytes taken from the start; a directory's next offset. + end: u64 = 0, + /// The read that ended at `end` came back short, or empty (the end). + short: bool = false, + empty: bool = false, + /// What taking said (too large, a bad entry): the operation's + /// answer once every read out has landed. + err: ?Error = null, + + fn take(t: *Taker, item: *Flight.Item, data: []const u8) void { + if (t.err != null or item.off != t.end or t.short) return; + t.put(data) catch |err| { + t.err = err; + return; + }; + t.end += data.len; + t.short = data.len < item.count; + t.empty = data.len == 0; + } + + fn directory(t: *const Taker) bool { + return t.flight.qid(t.s.root_qid).type & ninep.qtdir != 0; + } + + fn maxBytes(t: *const Taker) u64 { + return @min(t.limit, @as(usize, if (t.directory()) limits.max_stream_bytes else limits.max_file_bytes)); + } + + fn wireLimit(t: *const Taker) u64 { + return if (t.directory()) limits.max_stream_bytes else t.maxBytes(); + } + + fn put(t: *Taker, data: []const u8) Error!void { + if (t.end + data.len > t.wireLimit()) return Error.FileTooLarge; + if (t.directory()) { + var pos: usize = 0; + while (pos < data.len) { + if (data.len - pos < 2) return Error.Botch; + const len: usize = 2 + @as(usize, std.mem.readInt(u16, data[pos..][0..2], .little)); + if (len > data.len - pos) return Error.Botch; + const entry = ninep.Stat.decode(data[pos..][0..len]) catch return Error.Botch; + const display_dir = std.mem.trimEnd(u8, t.display_path, "/"); + const row_len = display_dir.len + entry.name.len + 2 + @as(usize, @intFromBool(entry.qid.type & ninep.qtdir != 0)); + if (row_len > t.maxBytes() - t.out.written().len) return Error.FileTooLarge; + t.out.writer.print("{s}/{s}", .{ display_dir, entry.name }) catch return Error.Botch; + if (entry.qid.type & ninep.qtdir != 0) t.out.writer.writeByte('/') catch return Error.Botch; + t.out.writer.writeByte('\n') catch return Error.Botch; + pos += len; + } + } else t.out.writer.writeAll(data) catch return Error.Botch; + } + }; + + /// One operation in as few round trips as its answers allow: the walk, + /// a read's stat, the open and the first read or write go in one flight + /// (behind a new connection's version and attach). A file of the + /// length its stat said is whole when a read comes back short at that + /// length; a longer one is read ahead, the Treads for the rest sent at + /// once. Anything else (a directory, a file of no stated length) reads + /// on until a read comes back empty, the only end 9P has. fn transact( s: *Session, names: []const []const u8, @@ -2393,23 +2938,52 @@ pub const Client = struct { write_bytes: ?[]const u8, read_limit: usize, ) !void { - const at = try walkTo(s, names, remote); - const cur = at.fid; - const here = at.qid; - defer s.dropNoWait(cur); - - const directory = here.type & ninep.qtdir != 0; - - const mode: u8 = if (write_bytes != null) ninep.owrite else ninep.oread; - const truncate = write_bytes != null and names.len > 0 and + const writing = write_bytes != null; + const mode: u8 = if (writing) ninep.owrite else ninep.oread; + const truncate = writing and names.len > 0 and (std.mem.eql(u8, names[0], "os") or std.mem.eql(u8, names[names.len - 1], "body")); - _ = try s.ask(.{ .open = .{ .fid = cur, .mode = mode | if (truncate) ninep.otrunc else 0 } }, remote); + const fresh = !s.attached; + var f: Flight = .{}; + try s.greet(&f); + const fid = try s.walkNew(&f, names); + if (!writing) _ = f.add(.stat, try s.send(.{ .stat = .{ .fid = fid } })); + _ = f.add(.open, try s.send(.{ .open = .{ .fid = fid, .mode = mode | if (truncate) ninep.otrunc else 0 } })); + const chunk_max: usize = if (fresh) flight_write else s.cl.maxWrite(); + const first: []const u8 = if (write_bytes) |bytes| bytes[0..@min(bytes.len, chunk_max)] else ""; + if (writing and first.len != 0) { + // Out of the client's buffer first (it holds one msize): still + // sent without waiting. + try s.flush(); + _ = f.add(.write, try s.send(.{ .write = .{ .fid = fid, .offset = 0, .data = first } })); + } + // A read's count before the msize is known is the asked msize's. + if (!writing) { + const item = f.add(.read, try s.send(.{ .read = .{ .fid = fid, .offset = 0, .count = s.cl.maxRead() } })); + item.count = s.cl.maxRead(); + } + var taker: Taker = .{ .out = out, .display_path = s.display_path, .limit = read_limit, .flight = &f, .s = s }; + try landChecked(s, &f, &taker, remote); + defer if (f.made()) s.dropNoWait(fid); + try f.check(remote); + if (taker.err) |err| return err; + const directory = taker.directory(); + if (write_bytes) |bytes| { if (directory) return Error.IsDirectory; var written: usize = 0; + if (f.get(.write)) |w| { + // Refused unopened, the open parked (a truncating one while + // the editor is out): sent again below, after it. + if (w.failed and !std.mem.eql(u8, w.said.buf[0..w.said.len], ninep.fs.e_bad_use)) return remote.set(w.said.buf[0..w.said.len]); + if (!w.failed) { + if (w.n == 0 or w.n > first.len) return Error.Botch; + written = w.n; + } + } while (written < bytes.len) { + s.deadline = nowMs() +| write_budget_ms; const chunk = bytes[written..][0..@min(bytes.len - written, s.cl.maxWrite())]; - const count = (try s.ask(.{ .write = .{ .fid = cur, .offset = written, .data = chunk } }, remote)).write; + const count = (try s.ask(.{ .write = .{ .fid = fid, .offset = written, .data = chunk } }, remote)).write; if (count == 0 or count > chunk.len) return Error.Botch; written += count; // A long write is timed by its progress: each answer gives @@ -2417,36 +2991,36 @@ pub const Client = struct { // in through one open, whole, however long it takes. The // budget is a write's: a server putting in tens of MiB it // held (pane.zig batchFor) answers that one write late. - s.deadline = nowMs() +| write_budget_ms; } return; } - const max_bytes: u64 = @min(read_limit, @as(usize, if (directory) limits.max_stream_bytes else limits.max_file_bytes)); - const wire_limit: u64 = if (directory) limits.max_stream_bytes else max_bytes; - var off: u64 = 0; + if (f.get(.read)) |r| if (r.failed and !std.mem.eql(u8, r.said.buf[0..r.said.len], ninep.fs.e_bad_use)) + return remote.set(r.said.buf[0..r.said.len]); + const length: u64 = if (f.get(.stat)) |st| (if (st.failed or directory) 0 else st.n) else 0; + const step: u64 = s.cl.maxRead(); while (true) { - const want: u32 = @intCast(@min(@as(u64, s.cl.maxRead()), wire_limit + 1 - off)); - const data = (try s.ask(.{ .read = .{ .fid = cur, .offset = off, .count = want } }, remote)).read; - if (data.len == 0) return; - if (off + data.len > wire_limit) return Error.FileTooLarge; - if (directory) { - var pos: usize = 0; - while (pos < data.len) { - if (data.len - pos < 2) return Error.Botch; - const len: usize = 2 + @as(usize, std.mem.readInt(u16, data[pos..][0..2], .little)); - if (len > data.len - pos) return Error.Botch; - const entry = ninep.Stat.decode(data[pos..][0..len]) catch return Error.Botch; - const display_dir = std.mem.trimEnd(u8, s.display_path, "/"); - const row_len = display_dir.len + entry.name.len + 2 + @as(usize, @intFromBool(entry.qid.type & ninep.qtdir != 0)); - if (row_len > max_bytes - out.written().len) return Error.FileTooLarge; - try out.writer.print("{s}/{s}", .{ display_dir, entry.name }); - if (entry.qid.type & ninep.qtdir != 0) try out.writer.writeByte('/'); - try out.writer.writeByte('\n'); - pos += len; - } - } else try out.writer.writeAll(data); - off += data.len; + if (taker.empty) return; + if (taker.short and length != 0 and taker.end == length) return; + // The rest of a file of known length at once; a directory, a + // file of none, or the end to be seen, a read at a time. + // ponytail: a file of no stated length is not read ahead. + const want: u64 = if (directory or taker.short or length <= taker.end) 1 else @min(read_ahead, (length - taker.end) / step + 1); + var batch: Flight = .{}; + var off = taker.end; + for (0..want) |_| { + if (off > taker.wireLimit()) break; + const count: u32 = @intCast(@min(step, taker.wireLimit() + 1 - off)); + const item = batch.add(.read, try s.send(.{ .read = .{ .fid = fid, .offset = off, .count = count } })); + item.off = off; + item.count = count; + off += count; + } + taker.short = false; + try s.land(&batch, &taker); + if (taker.err) |err| return err; + for (batch.items[0..batch.len]) |item| if (item.failed and item.off == taker.end and !taker.short) + return remote.set(item.said.buf[0..item.said.len]); } } @@ -2458,24 +3032,58 @@ pub const Client = struct { write_bytes: ?[]const u8, display_path: []const u8, read_limit: usize, + keep: bool, ) ![]u8 { if (comptime !supported) return Error.Dial; - const s = try startSession(gpa, dial, display_path); - defer endSession(gpa, s); + if (keep) if (kept.take(dial)) |s| { + if (fetchOn(s, gpa, names, remote, write_bytes, read_limit, display_path, true)) |bytes| return bytes else |err| switch (err) { + // Let go of by the peer since (its hangup still on the way): + // a read is asked again on a new connection. A write is not, + // as it may have been done. + error.Hangup, error.Botch => if (write_bytes != null) return err, + else => return err, + } + }; + return fetchOn(try startSession(dial, display_path), gpa, names, remote, write_bytes, read_limit, display_path, keep); + } + + fn fetchOn( + s: *Session, + gpa: std.mem.Allocator, + names: []const []const u8, + remote: *RemoteError, + write_bytes: ?[]const u8, + read_limit: usize, + display_path: []const u8, + keep: bool, + ) ![]u8 { + s.deadline = nowMs() +| budget_ms; + s.display_path = display_path; + var clean = false; + defer endSession(s, keep and clean); var out: std.Io.Writer.Allocating = .init(gpa); errdefer out.deinit(); - try transact(s, names, &out, remote, write_bytes, read_limit); + transact(s, names, &out, remote, write_bytes, read_limit) catch |err| { + // Refused with every answer in: the connection is as good as before. + clean = switch (err) { + error.Remote, error.NotFound, error.IsDirectory, error.FileTooLarge => true, + else => false, + }; + return err; + }; + clean = true; return out.toOwnedSlice(); } - /// A connected session at `dial` whose requests have the usual deadline. - fn startSession(gpa: std.mem.Allocator, dial: []const u8, display_path: []const u8) !*Session { + /// A new connection at `dial` whose requests have the usual deadline. + fn startSession(dial: []const u8, display_path: []const u8) !*Session { var sock_buf: [sun_path_len]u8 = undefined; const sock = try resolve(&sock_buf, dial); const deadline = nowMs() +| budget_ms; - const s = try gpa.create(Session); + // Not the caller's allocator: a kept one outlives the call (an arena's). + const s = try std.heap.page_allocator.create(Session); s.* = .{ .fd = -1, .deadline = deadline, .display_path = display_path }; - errdefer endSession(gpa, s); + errdefer endSession(s, false); if (sock == .quic) { if (comptime quic_enabled) { s.quic = quic.Connection.dial(sock.quic) catch return Error.Dial; @@ -2483,14 +3091,91 @@ pub const Client = struct { } else return Error.QuicUnavailable; } else s.fd = try connect(sock, deadline); s.cl = .init(.{ .in = &s.in, .out = &s.out }); + if (dial.len <= s.dial_buf.len) { + @memcpy(s.dial_buf[0..dial.len], dial); + s.dial_len = dial.len; + } return s; } - fn endSession(gpa: std.mem.Allocator, s: *Session) void { + fn endSession(s: *Session, keep: bool) void { + if (keep and s.attached and s.dial_len != 0 and !(quic_enabled and s.quic != null) and kept.put(s)) return; if (quic_enabled and s.quic != null) { s.quic.?.deinit(); } else if (s.fd >= 0) _ = libc.close(s.fd); - gpa.destroy(s); + std.heap.page_allocator.destroy(s); + } + + /// Connections kept open between operations, a few dials' (a mount's, + /// as Plan 9's mount keeps its channel): each costs the peer one of its + /// `max_conns` while kept, so only mounts and the Mount probe keep one. + const kept = struct { + const max = 8; + var lock: std.atomic.Mutex = .unlocked; + var slots: [max]?*Session = @splat(null); + + fn hold() void { + while (!lock.tryLock()) std.atomic.spinLoopHint(); + } + + /// The session kept for `dial`, out of the table, if it is still + /// there: what came while it waited (a clunk's answer) taken, and + /// a hangup or anything else seen as its end. + fn take(dial: []const u8) ?*Session { + hold(); + const s = for (&slots) |*slot| { + const got = slot.* orelse continue; + if (std.mem.eql(u8, got.dial_buf[0..got.dial_len], dial)) { + slot.* = null; + break got; + } + } else null; + lock.unlock(); + const found = s orelse return null; + if (usable(found)) return found; + endSession(found, false); + return null; + } + + fn usable(s: *Session) bool { + while (true) { + var fds = [1]libc.pollfd{.{ .fd = s.fd, .events = poll_in, .revents = 0 }}; + if (libc.poll(&fds, 1, 0) < 0) return false; + if (fds[0].revents == 0) return true; + if (fds[0].revents & poll_in == 0) return false; + const room = s.cl.in.len - s.cl.in_len; + if (room == 0) return false; + const got = (transport.read(s.fd, s.stage[0..@min(room, s.stage.len)]) catch return false) orelse return true; + if (got == 0) return false; + _ = s.cl.push(s.stage[0..got]); + while (s.cl.take()) |done| { + if (done.tag >= 32 or s.unwaited & bit(done.tag) == 0) return false; + s.unwaited &= ~bit(done.tag); + } + if (s.cl.dead) return false; + } + } + + fn put(s: *Session) bool { + hold(); + defer lock.unlock(); + for (&slots) |*slot| if (slot.* == null) { + slot.* = s; + return true; + }; + return false; + } + + /// Closes what is kept for `dial` (an Unmount). + fn forget(dial: []const u8) void { + while (take(dial)) |s| endSession(s, false); + } + }; + + /// Lets go of a connection kept for `dial`: its mount is gone. + pub fn forget(dial: []const u8) void { + if (comptime !supported) return; + kept.forget(dial); } /// Writes `request`, then `more`, to `path` and reads the answer on the @@ -2504,8 +3189,8 @@ pub const Client = struct { const n = try elements(path, &names); var remote: RemoteError = .{}; errdefer said = remote; - const s = try startSession(gpa, dial, path); - defer endSession(gpa, s); + const s = try startSession(dial, path); + defer endSession(s, false); const fid = (try walkTo(s, names[0..n], &remote)).fid; _ = try s.ask(.{ .open = .{ .fid = fid, .mode = ninep.ordwr } }, &remote); var written: usize = 0; @@ -2545,8 +3230,9 @@ pub const Client = struct { var names: [max_depth][]const u8 = undefined; const n = try elements(path, &names); var remote: RemoteError = .{}; - const s = try startSession(gpa, dial, path); - defer endSession(gpa, s); + _ = gpa; + const s = try startSession(dial, path); + defer endSession(s, false); const fid = (try walkTo(s, names[0..n], &remote)).fid; _ = try s.ask(.{ .open = .{ .fid = fid, .mode = ninep.ordwr } }, &remote); _ = try s.ask(.{ .write = .{ .fid = fid, .offset = 0, .data = first } }, &remote); @@ -2622,7 +3308,7 @@ pub const Client = struct { const before = nowMs(); try testing.expectError( Error.Dial, - fetchBytes(testing.allocator, "/tmp/pardes-9p-no-such-socket.sock", names[0..n], &remote, null, "/pane/1/body", limits.max_file_bytes), + fetchBytes(testing.allocator, "/tmp/pardes-9p-no-such-socket.sock", names[0..n], &remote, null, "/pane/1/body", limits.max_file_bytes, false), ); try testing.expect(nowMs() - before < budget_ms); } @@ -2631,7 +3317,7 @@ pub const Client = struct { const transport_bytes = if (quic_enabled) @sizeOf(?quic.Connection) else 0; try testing.expectEqual(@as(usize, msize), @as(usize, (Session{ .fd = -1, .deadline = 0, .display_path = "" }).in.len)); try testing.expect(transport_bytes <= 64); - try testing.expect(@sizeOf(Session) <= 3 * msize + @sizeOf(ninep.Client) + 128 + transport_bytes); + try testing.expect(@sizeOf(Session) <= 3 * msize + @sizeOf(ninep.Client) + 128 + @sizeOf(@FieldType(Session, "dial_buf")) + transport_bytes); try testing.expect(@sizeOf(ninep.Client) <= 512); } |
