From a03d1649108510cda5951e918e561d7cae43d6b0 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Wed, 30 Sep 2026 22:50:41 -0300 Subject: 9ns: a file whose stat length is 0 opens direct and nonseekable, so a splice or a cached size never ends a stream early A server's stream (pardes's log) or generated view stats 0: its length is no length. Opened with the page cache or a kernel file offset of its own, a splicing reader (coreutils cat into a pipe) could read it from the wrong offset and take the size as its end. Such a file is now opened FOPEN_DIRECT_IO | FOPEN_NONSEEKABLE whatever --no-direct-io says; every other file keeps the option's flags. Co-Authored-By: Claude Opus 5.5 --- 9ns/src/bridge.zig | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) (limited to '9ns/src') diff --git a/9ns/src/bridge.zig b/9ns/src/bridge.zig index 2226658..07bd5d8 100644 --- a/9ns/src/bridge.zig +++ b/9ns/src/bridge.zig @@ -628,14 +628,25 @@ const Bridge = struct { b.clunkQuiet(fid); return e; }; + // A file whose stat length is 0 is a stream or a view made by each + // read (a log, a screen): no page cache, no size taken as its end, + // and no offset of the kernel's own, so a splice (cat) reads it from + // where the open stands, as read(2) does. + const stream = !is_dir and if (b.stat(fid)) |st| st.length == 0 else |_| false; const fh = try b.newHandle(fid, req.header.nodeid); const out = fuse.OpenOut{ .fh = fh, - .open_flags = if (!is_dir and b.opts.direct_io) fuse.FOPEN_DIRECT_IO else 0, + .open_flags = openFlags(is_dir, stream, b.opts.direct_io), }; try b.reply(req.header.unique, &.{std.mem.asBytes(&out)}); } + fn openFlags(is_dir: bool, stream: bool, direct_io: bool) u32 { + if (is_dir) return 0; + if (stream) return fuse.FOPEN_DIRECT_IO | fuse.FOPEN_NONSEEKABLE; + return if (direct_io) fuse.FOPEN_DIRECT_IO else 0; + } + fn newHandle(b: *Bridge, fid: u32, nodeid: u64) HandlerError!u64 { const fh = b.next_fh; b.handles.put(b.gpa, fh, .{ .fid = fid, .nodeid = nodeid, .dir = null }) catch |e| { @@ -2568,6 +2579,14 @@ test "attr mapping: DMDIR → S_IFDIR|perm, length → size/blocks" { try testing.expectEqual(@as(u64, 2), attrFromStat(testStat("f", 0o600, 513, 4), 4, 0, 0).blocks); } +test "a file whose stat length is 0 opens direct and nonseekable, whatever the cache option" { + for ([_]bool{ false, true }) |direct_io| { + try std.testing.expectEqual(fuse.FOPEN_DIRECT_IO | fuse.FOPEN_NONSEEKABLE, Bridge.openFlags(false, true, direct_io)); + try std.testing.expectEqual(@as(u32, if (direct_io) fuse.FOPEN_DIRECT_IO else 0), Bridge.openFlags(false, false, direct_io)); + try std.testing.expectEqual(@as(u32, 0), Bridge.openFlags(true, true, direct_io)); + } +} + test "open flag → 9P mode mapping" { const rdonly: u32 = @bitCast(linux.O{ .ACCMODE = .RDONLY }); const wronly: u32 = @bitCast(linux.O{ .ACCMODE = .WRONLY }); -- cgit v1.3