diff --git a/weed/mount/inode_to_path.go b/weed/mount/inode_to_path.go index 055a82cc14..74eb1fc568 100644 --- a/weed/mount/inode_to_path.go +++ b/weed/mount/inode_to_path.go @@ -629,6 +629,32 @@ func (i *InodeToPath) RemovePath(path util.FullPath, onStillReferenced func(inod delete(i.wasCachedDirs, path) } +// RemoveAlias drops a name only while its inode keeps another one, returning +// the inode's primary path afterwards. A sole name stays for a delayed rename +// to move. +func (i *InodeToPath) RemoveAlias(path util.FullPath) (util.FullPath, bool) { + i.Lock() + defer i.Unlock() + inode, found := i.path2inode[path] + if !found { + return "", false + } + ie := i.inode2path[inode] + if ie == nil { + return "", false + } + survives := false + for _, p := range ie.appendPaths(nil) { + survives = survives || p != path + } + if !survives { + return "", false + } + delete(i.path2inode, path) + ie.removeOnePath(path) + return ie.path, true +} + func (i *InodeToPath) removePathFromInode2Path(inode uint64, path util.FullPath) { ie, found := i.inode2path[inode] if !found { diff --git a/weed/mount/inode_to_path_test.go b/weed/mount/inode_to_path_test.go index c502838b46..5d6e3367af 100644 --- a/weed/mount/inode_to_path_test.go +++ b/weed/mount/inode_to_path_test.go @@ -230,3 +230,18 @@ func TestLookupRecordsHardLinkNames(t *testing.T) { t.Fatal("the final forget left /b mapped to a released inode") } } + +func TestRemoveAliasKeepsSoleName(t *testing.T) { + table := NewInodeToPath(util.FullPath("/"), 0) + inode := table.Lookup("/a", 1, false, true, 42, true) + if _, ok := table.RemoveAlias("/a"); ok || !table.HasPath("/a") { + t.Fatal("removed the only name") + } + table.AddPath(inode, "/b") + if survivor, ok := table.RemoveAlias("/a"); !ok || survivor != "/b" || table.HasPath("/a") { + t.Fatalf("RemoveAlias(/a) = %q, %v, want /b", survivor, ok) + } + if _, ok := table.RemoveAlias("/b"); ok || !table.HasPath("/b") { + t.Fatal("removed the surviving name") + } +} diff --git a/weed/mount/weedfs.go b/weed/mount/weedfs.go index b50907731c..bc39caaf86 100644 --- a/weed/mount/weedfs.go +++ b/weed/mount/weedfs.go @@ -966,6 +966,12 @@ func (wfs *WFS) onEntryInvalidation(invalidation meta_cache.EntryInvalidation) { handled, replacedInode := wfs.invalidateOpenFileHandle(invalidation) if !handled && invalidation.RenamedTo != "" { _, replacedInode = wfs.inodeToPath.MovePath(invalidation.Path, invalidation.RenamedTo) + } else if !handled && invalidation.Deleted && len(invalidation.LiveHardLinkId) > 0 { + // Otherwise the next open by nodeid resolves to the deleted name. A + // newer stored entry means something has taken the name since. + if _, _, newer := wfs.cachedEntrySince(invalidation.Path, invalidation.TsNs); !newer { + wfs.inodeToPath.RemoveAlias(invalidation.Path) + } } // A rename over an existing file destroys it, so its handle must not flush // the old content back over what took the name. Marked from here, holding @@ -1063,12 +1069,9 @@ func (wfs *WFS) invalidateOpenFileHandle(invalidation meta_cache.EntryInvalidati // hit there is a stale leftover — use the event entry instead. var candidate *filer_pb.Entry candidateTsNs := eventTsNs - dir, _ := filePath.DirAndName() - if wfs.metaCache.IsDirectoryCached(util.FullPath(dir)) { - if storeEntry, storeVersionTsNs, findErr := wfs.metaCache.FindEntry(context.Background(), filePath); findErr == nil && storeEntry != nil && storeVersionTsNs >= eventTsNs { - candidate = storeEntry.ToProtoEntry() - candidateTsNs = storeVersionTsNs - } + if storeEntry, storeVersionTsNs, found := wfs.cachedEntrySince(filePath, eventTsNs); found { + candidate = storeEntry.ToProtoEntry() + candidateTsNs = storeVersionTsNs } if candidate == nil && eventEntry != nil { candidate = proto.Clone(eventEntry).(*filer_pb.Entry) @@ -1089,7 +1092,7 @@ func (wfs *WFS) invalidateOpenFileHandle(invalidation meta_cache.EntryInvalidati if invalidation.Deleted { // The next flush reads the survivor's stored entry before // publishing, so the version stays where it was. - if len(invalidation.LiveHardLinkId) > 0 && wfs.followSurvivingLink(fh, inode, filePath) { + if len(invalidation.LiveHardLinkId) > 0 && wfs.followSurvivingLink(fh, filePath) { fh.linkRefresh = invalidation.LiveHardLinkId return } @@ -1137,22 +1140,29 @@ func (wfs *WFS) invalidateOpenFileHandle(invalidation meta_cache.EntryInvalidati return } +// cachedEntrySince returns the meta cache's entry at path when its directory is +// cached and the entry is no older than tsNs. +func (wfs *WFS) cachedEntrySince(path util.FullPath, tsNs int64) (*filer.Entry, int64, bool) { + dir, _ := path.DirAndName() + if !wfs.metaCache.IsDirectoryCached(util.FullPath(dir)) { + return nil, 0, false + } + entry, versionTsNs, err := wfs.metaCache.FindEntry(context.Background(), path) + if err != nil || entry == nil || versionTsNs < tsNs { + return nil, 0, false + } + return entry, versionTsNs, true +} + // followSurvivingLink drops a hard link name another client deleted and // keeps the handle on a name of the inode this mount still knows, reporting // false when it knows none. -func (wfs *WFS) followSurvivingLink(fh *FileHandle, inode uint64, removed util.FullPath) bool { - survives := false - for _, p := range wfs.inodeToPath.GetAllPaths(inode) { - survives = survives || p != removed - } - if !survives { - return false - } - wfs.inodeToPath.RemovePath(removed, nil) - if survivor, status := wfs.inodeToPath.GetPath(inode); status == fuse.OK { +func (wfs *WFS) followSurvivingLink(fh *FileHandle, removed util.FullPath) bool { + survivor, ok := wfs.inodeToPath.RemoveAlias(removed) + if ok { fh.RememberPath(survivor) } - return true + return ok } func (wfs *WFS) LookupFn() wdclient.LookupFileIdFunctionType { diff --git a/weed/mount/weedfs_peer_unlink_test.go b/weed/mount/weedfs_peer_unlink_test.go index be63c7abde..62d18b51e8 100644 --- a/weed/mount/weedfs_peer_unlink_test.go +++ b/weed/mount/weedfs_peer_unlink_test.go @@ -4,9 +4,11 @@ package mount import ( "context" + "os" "testing" "github.com/seaweedfs/go-fuse/v2/fuse" + "github.com/seaweedfs/seaweedfs/weed/filer" "github.com/seaweedfs/seaweedfs/weed/mount/meta_cache" "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb" ) @@ -106,6 +108,68 @@ func TestPeerUnlinkedAliasWithUnknownSurvivorStops(t *testing.T) { } } +// The kernel keeps the nodeid of a closed hard link after a peer unlinks the +// name it was opened by, and the next open must reach the surviving name. +func TestPeerUnlinkedAliasReopensClosedInode(t *testing.T) { + for _, removed := range []string{"tmp", "final"} { + t.Run(removed, func(t *testing.T) { + wfs, fake, fh, inode := openLinkTestFile(t) + wfs.openMtimeCache = map[uint64][2]int64{} + linkInRoot(t, wfs, inode, "final") + wfs.ReleaseHandle(fh.fh) + if _, found := wfs.fhMap.FindFileHandle(inode); found { + t.Fatal("handle still open") + } + peerUnlink(t, wfs, fake, removed) + survivor := map[string]string{"tmp": "/final", "final": "/tmp"}[removed] + in := &fuse.OpenIn{InHeader: fuse.InHeader{NodeId: inode, Caller: fuse.Caller{Owner: fuse.Owner{Uid: 1000, Gid: 20}}}, Flags: uint32(os.O_RDONLY)} + out := &fuse.OpenOut{} + if code := wfs.Open(make(chan struct{}), in, out); code != fuse.OK { + t.Fatalf("open after the peer unlinked %s: %v", removed, code) + } + reopened := wfs.GetHandle(FileHandleId(out.Fh)) + t.Cleanup(func() { wfs.ReleaseHandle(reopened.fh) }) + if got := reopened.FullPath(); string(got) != survivor { + t.Fatalf("reopened at %s, want %s", got, survivor) + } + }) + } +} + +// This mount renames another name of the closed file onto the one a peer +// unlinked before the peer's delete arrives, so that name is live again. +func TestDelayedPeerUnlinkKeepsRenamedOntoName(t *testing.T) { + wfs := newInvalidateTestWFS(t) + wfs.inodeToPath.MarkChildrenCached("/") + ctx := context.Background() + link := func(name string) *filer_pb.Entry { + return &filer_pb.Entry{Name: name, HardLinkId: []byte("link"), HardLinkCounter: 3, Attributes: &filer_pb.FuseAttributes{Inode: 42, FileMode: 0o600}} + } + for _, name := range []string{"tmp", "final", "third"} { + if err := wfs.metaCache.InsertEntry(ctx, filer.FromPbEntry("/", link(name)), 1); err != nil { + t.Fatalf("insert %s: %v", name, err) + } + } + inode := wfs.inodeToPath.Lookup("/tmp", 1, false, true, 42, true) + wfs.inodeToPath.AddPath(inode, "/final") + wfs.inodeToPath.AddPath(inode, "/third") + + rename := &filer_pb.SubscribeMetadataResponse{Directory: "/", TsNs: 20, EventNotification: &filer_pb.EventNotification{OldEntry: link("final"), NewEntry: link("tmp"), NewParentPath: "/"}} + if err := wfs.applyLocalMetadataEvent(ctx, rename); err != nil { + t.Fatalf("apply local rename: %v", err) + } + wfs.inodeToPath.MovePath("/final", "/tmp") + del := &filer_pb.SubscribeMetadataResponse{Directory: "/", TsNs: 10, EventNotification: &filer_pb.EventNotification{OldEntry: link("tmp")}} + if err := wfs.metaCache.ApplyMetadataResponse(ctx, del, meta_cache.SubscriberMetadataResponseApplyOptions); err != nil { + t.Fatalf("apply peer delete: %v", err) + } + wfs.metaCache.WaitForEntryInvalidations() + + if got, found := wfs.inodeToPath.GetInode("/tmp"); !found || got != inode { + t.Fatalf("/tmp maps to %d (%v), want %d", got, found, inode) + } +} + // A peer removes one name, then this mount unlinks the last one. func TestPeerThenLocalUnlinkKeepsHandleDeleted(t *testing.T) { wfs, fake, fh, inode := openLinkTestFile(t)