Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions changelog.d/+dir-close-after-sync.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`fsx.moveAcross` closes a copied directory after syncing it and before the rename that publishes the copy, including each directory of a copied tree. `fsx.copyTo` and `moveAcross` close the parent directory after the sync that makes the new name durable. `atomic.fsyncDir` and the parent directory sync in `atomic.atomicWrite` check that close as well, as does the directory sync at the end of `logs.gzipInPlace`. A failed close is returned as `sys.Error`. A close on the way out of an earlier error is ignored and that errno is kept. Before this change those closes were ignored, so a directory sync that a network file system (NFS) reported lost only at the close was returned as success. Before the rename, a failure leaves the destination name as it was and removes the temporary path. After the rename, the published name keeps what the rename put there. A close that `EINTR` interrupts is not retried and is not an error, as `sys.result` treats `EINTR`. The closes go through `fsx.publish_ops`, `atomic.ops.close` and `logs.gzip_ops` in the same builds as the other replaceable calls.
31 changes: 22 additions & 9 deletions src/atomic.zig
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ const posix_access = "system.posix_acl_access";
/// filesystem's ACL model or a failing call, in a `replaceable` build only
/// (this library's tests, a tool's -Dtest-ops artifact). `created` runs on
/// the temporary file right after its O_EXCL creation, where a filesystem
/// applies an inherited ACL. `close` is that temporary file's close, before
/// the rename; the caller does not retry it.
/// applies an inherited ACL. `close` is the temporary file's close before
/// the rename, and the parent directory's close after its sync; the caller
/// does not retry either.
pub const Ops = struct {
flistxattr: *const fn (fd: sys.fd_t, buf: []u8) usize = realList,
fgetxattr: *const fn (fd: sys.fd_t, name: [:0]const u8, buf: []u8) usize = realGet,
Expand Down Expand Up @@ -453,12 +454,19 @@ pub fn fileMode(path: []const u8) u32 {
return 0o644 & ~currentUmask();
}

/// Open `path` read-only and fsync it. For a directory, that makes a
/// rename or a new name in it durable.
/// Open `path` read-only, fsync it and close it. For a directory, that
/// makes a rename or a new name in it durable. A failed close is returned
/// with `sys.last_errno` set. A close that EINTR interrupts is not retried
/// and is not an error, as `sys.result` treats EINTR. The close goes
/// through `ops.close`. On an earlier error the cleanup close is ignored
/// and that errno is kept.
pub fn fsyncDir(path: []const u8) sys.Error!void {
const fd = try sys.openat(fsx.cwd, path, .{}, 0);
defer sys.close(fd);
var open = true;
errdefer if (open) closeDiscarded(fd);
try sys.fsync(fd);
open = false;
try closeTemp(fd);
}

/// os.makedirs(path, exist_ok=True): a name that exists as something
Expand Down Expand Up @@ -595,9 +603,11 @@ pub const WriteOptions = struct {
/// points STEP.write, STEP.fsync and STEP.rename precede those
/// operations, and in a replaceable build can fail them (Faults.fails);
/// the destination is never unlinked first. The close goes through
/// `ops.close`. Everything it allocates from `a` is freed before it
/// returns, except the failure message in `why`. Call `why.deinit(a)`
/// after any error.
/// `ops.close`. After the rename the parent directory is synced and closed
/// through that same call. A failed close of the directory is reported
/// with no path, and the destination keeps the bytes the rename published.
/// Everything it allocates from `a` is freed before it returns, except the
/// failure message in `why`. Call `why.deinit(a)` after any error.
pub fn atomicWrite(a: Allocator, path: []const u8, data: []const u8, opts: WriteOptions, why: *Why) Error!void {
sys.last_errno = .SUCCESS;
const parent = if (std.fs.path.dirname(path)) |d| (if (d.len > 0) d else ".") else ".";
Expand Down Expand Up @@ -681,8 +691,11 @@ pub fn atomicWrite(a: Allocator, path: []const u8, data: []const u8, opts: Write
renamed = true;
// Opening the directory names it. Flushing the descriptor names no file.
const directory = sys.openat(fsx.cwd, parent, .{}, 0) catch |err| return failOs(a, why, err, parent, null);
defer sys.close(directory);
var dir_open = true;
defer if (dir_open) closeDiscarded(directory);
sys.fsync(directory) catch |err| return failOs(a, why, err, null, null);
dir_open = false;
closeTemp(directory) catch |err| return failOs(a, why, err, null, null);
}

/// atomic_write pinned to an open directory: replace `dir/name` with
Expand Down
95 changes: 94 additions & 1 deletion src/atomic_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2044,7 +2044,9 @@ fn expectPublishClose(dir_path: []const u8, dfd: fsx.fd_t, close_errno: linux.E,
try testing.expectEqualStrings("old\n", readFile(path));
} else try testing.expect(!exists(path));
} else try testing.expectEqualStrings("new\n", readFile(path));
try testing.expectEqual(@as(usize, 1), ncloses);
// atomicWrite also closes the parent directory after the rename.
const want_closes: usize = if (!fails and !c.at) 2 else 1;
try testing.expectEqual(want_closes, ncloses);
try testing.expectEqual(before, try test_support.fdCount(gpa));
try expectNoPublishTemp(gpa, dfd);
}
Expand Down Expand Up @@ -2081,3 +2083,94 @@ test "a failed fsync of an atomic write closes the temporary file once" {
defer sys.close(dfd);
try expectPublishClose(dir_path, dfd, .SUCCESS, true, .IO, error.Unexpected, true);
}

const DirCloseHook = struct {
var closes: usize = 0;
var errno: linux.E = .SUCCESS;
/// When set, only a directory descriptor is injected. A file close stays
/// the real call, so the parent close can fail on its own.
var dirs_only: bool = false;

fn arm(e: linux.E, directories_only: bool) void {
closes = 0;
errno = e;
dirs_only = directories_only;
atomic.ops.close = close;
}

fn close(fd: sys.fd_t) usize {
const is_dir = if (fsx.fstat(fd)) |st| st.isDir() else |_| false;
const rc = linux.close(fd);
if (dirs_only and !is_dir) return rc;
closes += 1;
if (errno == .SUCCESS or closes > 1) return rc;
return err(errno);
}
};

test "dir close: fsyncDir returns EIO and leaves the directory" {
begin();
defer end();
const dir_path = tmpdir();
defer cleanup(dir_path);
DirCloseHook.arm(.IO, false);
sys.last_errno = .SUCCESS;
try testing.expectError(error.Unexpected, atomic.fsyncDir(dir_path));
try testing.expectEqual(linux.E.IO, sys.last_errno);
try testing.expectEqual(@as(usize, 1), DirCloseHook.closes);
try testing.expect(exists(dir_path));
}

test "dir close: fsyncDir publishes on EINTR" {
begin();
defer end();
const dir_path = tmpdir();
defer cleanup(dir_path);
DirCloseHook.arm(.INTR, false);
try atomic.fsyncDir(dir_path);
try testing.expectEqual(@as(usize, 1), DirCloseHook.closes);
try testing.expect(exists(dir_path));
}

test "dir close: atomicWrite parent returns EIO and keeps the published bytes" {
begin();
defer end();
const gpa = testing.allocator;
const dir_path = tmpdir();
defer cleanup(dir_path);
const path = join(dir_path, "kept");
seed(path, true);
DirCloseHook.arm(.IO, true);
var why: atomic.Why = .{};
defer why.deinit(gpa);
sys.last_errno = .SUCCESS;
const before = try test_support.fdCount(gpa);
try testing.expectError(error.Unexpected, atomic.atomicWrite(gpa, path, "new\n", .{}, &why));
try testing.expectEqual(linux.E.IO, sys.last_errno);
try testing.expectEqualStrings(sys.describeErrno(.IO), why.msg);
try testing.expectEqual(@as(usize, 1), DirCloseHook.closes);
try testing.expectEqual(before, try test_support.fdCount(gpa));
try testing.expectEqualStrings("new\n", readFile(path));
const dfd = try fsx.openDir(fsx.cwd, dir_path, false);
defer sys.close(dfd);
try expectNoPublishTemp(gpa, dfd);
}

test "dir close: atomicWrite parent publishes on EINTR" {
begin();
defer end();
const gpa = testing.allocator;
const dir_path = tmpdir();
defer cleanup(dir_path);
const path = join(dir_path, "kept");
seed(path, true);
DirCloseHook.arm(.INTR, true);
var why: atomic.Why = .{};
defer why.deinit(gpa);
const before = try test_support.fdCount(gpa);
try atomic.atomicWrite(gpa, path, "new\n", .{}, &why);
try testing.expectEqualStrings("", why.msg);
try testing.expectEqual(@as(usize, 1), DirCloseHook.closes);
try testing.expectEqual(before, try test_support.fdCount(gpa));
try testing.expectEqualStrings("new\n", readFile(path));
}
Loading
Loading