From 2ae9f36cb0911d0606345c223c0e05708311f379 Mon Sep 17 00:00:00 2001 From: tofarr Date: Tue, 1 Dec 2026 13:27:04 +0000 Subject: [PATCH] fix(fuse): support fsync on directories fsync(2) on a directory returned ENOTSUP: go-fuse answers FSYNCDIR with ENOTSUP unless the node (or directory handle) implements Fsync/Fsyncdir. The OpenHands SDK's atomic_write_text fsyncs the parent directory after the rename and treats any OSError as fatal, so every conversation start on a fusey mount failed with "OSError: [Errno 95] Operation not supported". Implement NodeFsyncer on Node. go-fuse routes both FSYNC (preferred over FileFsyncer) and the FSYNCDIR fallback to it, so the shared sync helper now serves files and directories: flush the active chunk and persist the index, which is also what makes renamed directory entries durable. Co-authored-by: openhands --- internal/fuse/fs.go | 28 ++++++---- internal/fuse/fs_test.go | 107 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 125 insertions(+), 10 deletions(-) diff --git a/internal/fuse/fs.go b/internal/fuse/fs.go index aec4a30..c8f8c25 100644 --- a/internal/fuse/fs.go +++ b/internal/fuse/fs.go @@ -109,6 +109,7 @@ var _ fs.NodeGetxattrer = (*Node)(nil) var _ fs.NodeSetxattrer = (*Node)(nil) var _ fs.NodeListxattrer = (*Node)(nil) var _ fs.NodeRemovexattrer = (*Node)(nil) +var _ fs.NodeFsyncer = (*Node)(nil) func (n *Node) Getattr(ctx context.Context, fh fs.FileHandle, out *gofuse.AttrOut) syscall.Errno { inode, ok := n.f.idx.GetInode(n.ino) @@ -460,7 +461,6 @@ type FileHandle struct { var _ fs.FileReader = (*FileHandle)(nil) var _ fs.FileWriter = (*FileHandle)(nil) var _ fs.FileFlusher = (*FileHandle)(nil) -var _ fs.FileFsyncer = (*FileHandle)(nil) func (fh *FileHandle) Read(ctx context.Context, dest []byte, off int64) (gofuse.ReadResult, syscall.Errno) { inode, ok := fh.f.idx.GetInode(fh.ino) @@ -533,34 +533,42 @@ func (fh *FileHandle) Flush(ctx context.Context) syscall.Errno { return 0 } -// Fsync makes the file's data durable: +// sync makes all written data and the index durable: // 1. Flush the in-memory active chunk to the backing store so all written // bytes are retrievable without relying on the process staying alive. // 2. Persist the index snapshot to cacheDir so the file-to-extent mapping -// survives a restart. +// and directory entries survive a restart. // -// Both operations are idempotent; calling Fsync multiple times is safe. -func (fh *FileHandle) Fsync(ctx context.Context, flags uint32) syscall.Errno { - if err := fh.f.cs.FlushActive(ctx); err != nil { +// Both operations are idempotent; calling sync multiple times is safe. +func (f *Fusey) sync(ctx context.Context) syscall.Errno { + if err := f.cs.FlushActive(ctx); err != nil { return syscall.EIO } - if fh.f.cacheDir != "" { + if f.cacheDir != "" { // Save the underlying index. FUSE holds an FSIndex interface; // both *index.Index and *journal.JournaledIndex have the Save // method on the inner index, so we unwrap the wrapper if needed. var raw *index.Index - if w, ok := fh.f.idx.(interface{ Inner() *index.Index }); ok { + if w, ok := f.idx.(interface{ Inner() *index.Index }); ok { raw = w.Inner() } else { - raw = fh.f.idx.(*index.Index) + raw = f.idx.(*index.Index) } - if err := index.Save(raw, fh.f.cacheDir); err != nil { + if err := index.Save(raw, f.cacheDir); err != nil { return syscall.EIO } } return 0 } +// Fsync serves fsync(2) on files and directories. go-fuse prefers NodeFsyncer +// over a handle's FileFsyncer, and falls back to it for FSYNCDIR, which would +// otherwise return ENOTSUP. Callers such as the SDK's atomic_write_text fsync +// the parent directory after a rename and treat that error as fatal. +func (n *Node) Fsync(ctx context.Context, fh fs.FileHandle, flags uint32) syscall.Errno { + return n.f.sync(ctx) +} + // --- helpers --- func fillAttr(inode *index.Inode, attr *gofuse.Attr) { diff --git a/internal/fuse/fs_test.go b/internal/fuse/fs_test.go index 0619ff2..fb6de79 100644 --- a/internal/fuse/fs_test.go +++ b/internal/fuse/fs_test.go @@ -4,6 +4,7 @@ package fuse import ( "bytes" + "context" "os" "path/filepath" "syscall" @@ -446,3 +447,109 @@ func TestFsync(t *testing.T) { } f2.Close() } + +// TestFsyncDirectory verifies that fsync(2) on a directory succeeds (go-fuse +// answers FSYNCDIR with ENOTSUP unless the node implements Fsync) and that it +// makes directory entries durable by persisting the index snapshot. +func TestFsyncDirectory(t *testing.T) { + mnt, cacheDir, cleanup := mountFS(t) + defer cleanup() + + if err := os.WriteFile(filepath.Join(mnt, "entry.txt"), []byte("x"), 0o644); err != nil { + t.Fatal(err) + } + + dir, err := os.Open(mnt) + if err != nil { + t.Fatal(err) + } + defer dir.Close() + if err := dir.Sync(); err != nil { + t.Fatalf("fsync(directory): %v", err) + } + + loaded, err := index.Load(cacheDir, 4096) + if err != nil { + t.Fatalf("load persisted index: %v", err) + } + if _, ok := loaded.Lookup(index.RootIno, "entry.txt"); !ok { + t.Error("entry.txt missing from persisted index after directory fsync") + } +} + +// TestAtomicWriteSequence replays the syscalls of the SDK's atomic_write_text +// (temp file, fchmod, fsync, rename, fsync of the parent directory), which +// failed with ENOTSUP on the directory fsync. +func TestAtomicWriteSequence(t *testing.T) { + mnt, _, cleanup := mountFS(t) + defer cleanup() + + sub := filepath.Join(mnt, "conversations") + if err := os.Mkdir(sub, 0o755); err != nil { + t.Fatal(err) + } + tmp := filepath.Join(sub, ".base_state.json.abc123.tmp") + final := filepath.Join(sub, "base_state.json") + + f, err := os.OpenFile(tmp, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0o600) + if err != nil { + t.Fatal(err) + } + if err := f.Chmod(0o600); err != nil { + t.Fatalf("fchmod: %v", err) + } + if _, err := f.WriteString(`{"ok":true}`); err != nil { + t.Fatal(err) + } + if err := f.Sync(); err != nil { + t.Fatalf("fsync(file): %v", err) + } + f.Close() + if err := os.Rename(tmp, final); err != nil { + t.Fatalf("rename: %v", err) + } + + dir, err := os.Open(sub) + if err != nil { + t.Fatal(err) + } + defer dir.Close() + if err := dir.Sync(); err != nil { + t.Fatalf("fsync(parent directory): %v", err) + } + + got, err := os.ReadFile(final) + if err != nil { + t.Fatal(err) + } + if string(got) != `{"ok":true}` { + t.Errorf("content after atomic write: got %q", got) + } +} + +// TestNodeFsyncWithoutMount exercises Node.Fsync directly, so the durability +// behavior is covered on machines without /dev/fuse (where the mounted tests skip). +func TestNodeFsyncWithoutMount(t *testing.T) { + local, err := chunks.NewLocalStore(t.TempDir()) + if err != nil { + t.Fatal(err) + } + idx := index.New(4096) + cacheDir := t.TempDir() + f := New(idx, chunks.NewChunkStore(local, 64*1024*1024), 1<<30, cacheDir, false) + + if _, err := idx.MakeDir(index.RootIno, "sub", 0o755, 0, 0, time.Now().UnixNano()); err != nil { + t.Fatal(err) + } + if errno := f.Root().Fsync(context.Background(), nil, 0); errno != 0 { + t.Fatalf("Node.Fsync: %v", errno) + } + + loaded, err := index.Load(cacheDir, 4096) + if err != nil { + t.Fatalf("load persisted index: %v", err) + } + if _, ok := loaded.Lookup(index.RootIno, "sub"); !ok { + t.Error("directory entry missing from persisted index after Node.Fsync") + } +}