Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 56 additions & 3 deletions zig/src/core/direct_io.zig
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,23 @@ fn readAt(f: File, buf: Buffer, offset: u64, len: usize, ctx: *anyopaque, pread:
const want: usize = @intCast(offset + len - lo);
const whole = span(offset, len);
if (whole > buf.len) return error.BufferTooSmall;
var aligned = f.direct;
var got: usize = 0;
while (got < want) {
// A short O_DIRECT read is retried from the last 4 KiB boundary so the next pread stays aligned.
const at: usize = if (f.direct) std.mem.alignBackward(usize, got, alignment) else got;
const at: usize = if (aligned) std.mem.alignBackward(usize, got, alignment) else got;
const ask = whole - at;
const n = pread(ctx, f.fd, buf.ptr + at, ask, lo + @as(u64, @intCast(at)));
if (n < 0) {
if (std.c.errno(n) == .INTR) continue;
std.log.err("read of {d} bytes at {d} failed: {t}", .{ ask, lo + at, std.c.errno(n) });
const e = std.c.errno(n);
if (e == .INTR) continue;
// a file system that took the O_DIRECT open can still refuse its reads: the file goes through the cache
if (e == .INVAL and aligned) {
try buffered(f.fd);
aligned = false;
continue;
}
std.log.err("read of {d} bytes at {d} failed: {t}", .{ ask, lo + at, e });
return error.ReadFailed;
}
if (n == 0) return error.EndOfFile;
Expand All @@ -75,6 +83,13 @@ fn readAt(f: File, buf: Buffer, offset: u64, len: usize, ctx: *anyopaque, pread:
return buf[@intCast(offset - lo)..][0..len];
}

/// Clears O_DIRECT on `fd`, so this read and every later one through it use the page cache.
fn buffered(fd: std.c.fd_t) !void {
const direct_flag: c_int = @bitCast(@as(u32, @bitCast(std.c.O{ .DIRECT = true })));
const flags = std.c.fcntl(fd, std.c.F.GETFL);
if (flags < 0 or std.c.fcntl(fd, std.c.F.SETFL, flags & ~direct_flag) < 0) return error.ReadFailed;
}

test "span and fits" {
try std.testing.expectEqual(@as(usize, 4096), span(0, 1));
try std.testing.expectEqual(@as(usize, 8192), span(4095, 2));
Expand Down Expand Up @@ -104,6 +119,44 @@ test "direct reads return the same bytes as buffered reads" {
try std.testing.expectError(error.BufferTooSmall, f.read(buf, 1, 1 << 20));
}

/// Refuses its first read with EINVAL, as a file system that accepts an O_DIRECT open but not the read does.
const Refuse = struct {
refused: bool = false,

fn pread(ctx: *anyopaque, fd: std.c.fd_t, buf: [*]u8, len: usize, offset: u64) isize {
const r: *Refuse = @ptrCast(@alignCast(ctx));
if (!r.refused) {
r.refused = true;
std.c._errno().* = @backingInt(std.c.E.INVAL);
return -1;
}
return std.c.pread(fd, buf, len, @intCast(offset));
}
};

test "a direct read the file system refuses is read again through the page cache" {
if (builtin.os.tag != .linux) return error.SkipZigTest;
const gpa = std.testing.allocator;
var f = try File.open("/proc/self/exe");
defer f.close();
if (!f.direct) return error.SkipZigTest;
var plain: File = .{ .fd = std.c.open("/proc/self/exe", .{ .ACCMODE = .RDONLY, .CLOEXEC = true }), .direct = false };
try std.testing.expect(plain.fd >= 0);
defer plain.close();
const buf = try gpa.alignedAlloc(u8, .fromByteUnits(alignment), 1 << 20);
defer gpa.free(buf);
const want = try gpa.alloc(u8, 100000);
defer gpa.free(want);
try std.testing.expectEqual(@as(isize, @intCast(want.len)), std.c.pread(plain.fd, want.ptr, want.len, 12345));
var refuse: Refuse = .{};
try std.testing.expectEqualSlices(u8, want, try readAt(f, buf, 12345, want.len, &refuse, Refuse.pread));
try std.testing.expect(refuse.refused);
const direct_flag: c_int = @bitCast(@as(u32, @bitCast(std.c.O{ .DIRECT = true })));
try std.testing.expectEqual(@as(c_int, 0), std.c.fcntl(f.fd, std.c.F.GETFL) & direct_flag);
// later reads through the descriptor, now buffered, still return the file's bytes
try std.testing.expectEqualSlices(u8, want[1..][0..4095], try f.read(buf, 12346, 4095));
}

const Script = struct {
plan: []const usize,
offs: [8]u64 = undefined,
Expand Down
Loading