diff --git a/cmd/fusey/main.go b/cmd/fusey/main.go index 3df89c1..4290b0c 100644 --- a/cmd/fusey/main.go +++ b/cmd/fusey/main.go @@ -382,6 +382,9 @@ func runDaemon(daemonID, mountpoint string, asOf time.Time) { case <-ctx.Done(): return case <-ticker.C: + // IsDirty excludes atime-only changes, so a read-only + // workload no longer rewrites the full snapshot every + // interval. if idx.IsDirty() || idx.IsRemoteDirty() { if err := persistFn(ctx); err != nil { log.Printf("persist index: %v", err) diff --git a/internal/index/atime_parent_test.go b/internal/index/atime_parent_test.go new file mode 100644 index 0000000..b25059d --- /dev/null +++ b/internal/index/atime_parent_test.go @@ -0,0 +1,99 @@ +package index + +import "testing" + +// A read touches atime. That must not mark the index structurally dirty, +// because IsDirty is what triggers a full snapshot rewrite on every persist +// tick — a pure-read workload would otherwise rewrite the whole index every +// FUSEY_PERSIST_INTERVAL. +func TestAtimeDoesNotDirtyIndex(t *testing.T) { + idx := New(4096) + idx.MarkClean() + + idx.TouchAtime(RootIno, 12345) + + if idx.IsDirty() { + t.Error("IsDirty() must be false after an atime-only change") + } + if !idx.IsAtimeDirty() { + t.Error("IsAtimeDirty() must be true after TouchAtime") + } + + idx.MarkClean() + if idx.IsAtimeDirty() { + t.Error("MarkClean must clear atimeDirty") + } +} + +// Structural mutations must still set the dirty flag. +func TestStructuralChangeStillDirty(t *testing.T) { + idx := New(4096) + idx.MarkClean() + + if _, err := idx.MakeFile(RootIno, "f", 0o644, 0, 0, 0, now()); err != nil { + t.Fatal(err) + } + if !idx.IsDirty() { + t.Error("IsDirty() must be true after MakeFile") + } +} + +// The parent map must stay correct across every structural mutation that can +// change a child's parent, and must survive a snapshot round-trip. +func TestParentMapConsistency(t *testing.T) { + idx := New(4096) + n := now() + + d, err := idx.MakeDir(RootIno, "d", 0o755, 0, 0, n) + if err != nil { + t.Fatal(err) + } + f, err := idx.MakeFile(d, "f", 0o644, 0, 0, 0, n) + if err != nil { + t.Fatal(err) + } + if got := idx.GetParentIno(f); got != d { + t.Errorf("GetParentIno(f) = %d, want %d", got, d) + } + + // Renaming the directory itself keeps its parent at root. + if err := idx.Rename(RootIno, "d", RootIno, "d2", n); err != nil { + t.Fatal(err) + } + if got := idx.GetParentIno(d); got != RootIno { + t.Errorf("after rename GetParentIno(d) = %d, want %d", got, RootIno) + } + if got := idx.GetParentIno(f); got != d { + t.Errorf("child parent must not change when its parent is renamed, got %d want %d", got, d) + } + + // Moving a file to a different parent must update its parent pointer. + other, err := idx.MakeDir(RootIno, "other", 0o755, 0, 0, n) + if err != nil { + t.Fatal(err) + } + if err := idx.Rename(d, "f", other, "f", n); err != nil { + t.Fatal(err) + } + if got := idx.GetParentIno(f); got != other { + t.Errorf("after move GetParentIno(f) = %d, want %d", got, other) + } + + // Round-trip through the snapshot format rebuilds the map. + snap := idx.Snapshot() + idx2 := FromSnapshot(snap, 4096) + if got := idx2.GetParentIno(f); got != other { + t.Errorf("after round-trip GetParentIno(f) = %d, want %d", got, other) + } + if got := idx2.GetParentIno(d); got != RootIno { + t.Errorf("after round-trip GetParentIno(d) = %d, want %d", got, RootIno) + } + + // Removing an entry must drop the child's parent mapping. + if err := idx.RemoveDirEntry(other, "f", n); err != nil { + t.Fatal(err) + } + if _, ok := idx.Lookup(other, "f"); ok { + t.Error("entry should be gone after RemoveDirEntry") + } +} diff --git a/internal/index/index.go b/internal/index/index.go index 60c6d8d..38af279 100644 --- a/internal/index/index.go +++ b/internal/index/index.go @@ -27,11 +27,13 @@ type Index struct { extents map[uint64][]Extent symlinks map[uint64]string xattrs map[uint64]map[string]string + parent map[uint64]uint64 // childIno -> parentIno, derived from dirIndex nextIno uint64 blockSize int32 dirty bool remoteDirty bool // true when structural mutations have occurred since the last remote-store write + atimeDirty bool // true when only atime changed since the last snapshot } // New creates an empty Index with a root directory inode. @@ -53,6 +55,7 @@ func New(blockSize int32) *Index { extents: map[uint64][]Extent{}, symlinks: map[uint64]string{}, xattrs: map[uint64]map[string]string{}, + parent: map[uint64]uint64{}, nextIno: RootIno + 1, blockSize: blockSize, } @@ -64,6 +67,7 @@ func FromSnapshot(snap *Snapshot, blockSize int32) *Index { idx := &Index{ inodes: snap.Inodes, dirIndex: make(map[uint64]map[string]uint64), + parent: make(map[uint64]uint64), extents: snap.ExtentMap, symlinks: snap.SymlinkTargets, xattrs: snap.Xattrs, @@ -88,6 +92,7 @@ func FromSnapshot(snap *Snapshot, blockSize int32) *Index { idx.dirIndex[e.ParentIno] = map[string]uint64{} } idx.dirIndex[e.ParentIno][e.Name] = e.ChildIno + idx.parent[e.ChildIno] = e.ParentIno } // Ensure every directory inode has an entry in dirIndex. for ino, inode := range idx.inodes { @@ -151,11 +156,21 @@ func (idx *Index) IsDirty() bool { return idx.dirty } +// IsAtimeDirty reports whether only atime has changed since the last +// snapshot. Such changes can be deferred across several persist intervals: +// they cost a full index rewrite but only affect atime precision. +func (idx *Index) IsAtimeDirty() bool { + idx.mu.RLock() + defer idx.mu.RUnlock() + return idx.atimeDirty +} + // MarkClean resets the dirty flag (called after a successful persist). func (idx *Index) MarkClean() { idx.mu.Lock() defer idx.mu.Unlock() idx.dirty = false + idx.atimeDirty = false } // IsRemoteDirty reports whether structural mutations have occurred since the @@ -315,6 +330,7 @@ func (idx *Index) AddDirEntry(parentIno uint64, name string, childIno uint64, no return fmt.Errorf("entry %q already exists in directory %d", name, parentIno) } idx.dirIndex[parentIno][name] = childIno + idx.parent[childIno] = parentIno child.Nlink++ child.Ctime = now parent.Mtime = now @@ -365,6 +381,7 @@ func (idx *Index) MakeFile(parentIno uint64, name string, mode, uid, gid, rdev u } idx.extents[ino] = []Extent{} idx.dirIndex[parentIno][name] = ino + idx.parent[ino] = parentIno parent.Mtime = now parent.Ctime = now idx.dirty = true @@ -405,6 +422,7 @@ func (idx *Index) MakeDir(parentIno uint64, name string, mode, uid, gid uint32, } idx.dirIndex[ino] = map[string]uint64{} idx.dirIndex[parentIno][name] = ino + idx.parent[ino] = parentIno parent.Mtime = now parent.Ctime = now idx.dirty = true @@ -446,6 +464,7 @@ func (idx *Index) MakeSymlink(parentIno uint64, name, target string, uid, gid ui } idx.symlinks[ino] = target idx.dirIndex[parentIno][name] = ino + idx.parent[ino] = parentIno parent.Mtime = now parent.Ctime = now idx.dirty = true @@ -476,6 +495,7 @@ func (idx *Index) RemoveDirEntry(parentIno uint64, name string, now int64) error } delete(idx.dirIndex[parentIno], name) + delete(idx.parent, childIno) child.Nlink-- // For directories, the "." self-link accounts for nlink=1 with no // external references. Free the inode when no external links remain: @@ -529,11 +549,13 @@ func (idx *Index) Rename(srcParent uint64, srcName string, dstParent uint64, dst delete(idx.symlinks, dstChildIno) delete(idx.xattrs, dstChildIno) delete(idx.dirIndex, dstChildIno) + delete(idx.parent, dstChildIno) } } delete(idx.dirIndex[srcParent], srcName) idx.dirIndex[dstParent][dstName] = srcChildIno + idx.parent[srcChildIno] = dstParent srcChild.Ctime = now if parent := idx.inodes[srcParent]; parent != nil { parent.Mtime = now @@ -739,7 +761,7 @@ func (idx *Index) TouchAtime(ino uint64, now int64) { defer idx.mu.Unlock() if inode, ok := idx.inodes[ino]; ok { inode.Atime = now - idx.dirty = true + idx.atimeDirty = true } } @@ -751,12 +773,8 @@ func (idx *Index) GetParentIno(ino uint64) uint64 { if ino == RootIno { return RootIno } - for parentIno, children := range idx.dirIndex { - for _, childIno := range children { - if childIno == ino { - return parentIno - } - } + if p, ok := idx.parent[ino]; ok { + return p } return ino }