From bd1f24a09bfd79fbc404d9aa93dbd84940ccc76b Mon Sep 17 00:00:00 2001 From: MapleWhiteZZZ <421700801@qq.com> Date: Thu, 26 Nov 2026 09:28:56 +0800 Subject: [PATCH] mount: carry a file's inline content into the name a hard link adds (#13008) * mount: carry a file's inline content into the name a hard link adds A small file the filer keeps inside its entry (-saveToFilerLimit, or ?saveInside=true on the upload) has content and no chunks. To link it a mount updates the source and creates the new name, and the filer writes the link's shared record from each of the two. The create carried the source's attributes, chunks and extended attributes but not its content, so the record was left with a size and nothing behind it: both names read zeros from then on, through the mounts and through the filer. Co-Authored-By: Claude Fable 5.2 * mount: carry a remote-only file's RemoteEntry into the name a hard link adds A file under a remote mount that is not cached yet has no chunks and no content; its RemoteEntry is what sends a read to fetch the object. The create of the new name left it out, so the hard link's shared record lost it and both names read zeros. Co-Authored-By: Claude Opus 5.5 * mount: cache a remote-only file before a hard link gives it a new name The filer resolves a remote object from the name a read goes through, and the new name has no object of its own, so a read through it failed until the source name had been read. The link now has the filer cache the object first, so every name shares its local chunks. Co-Authored-By: Claude Opus 5.5 * mount: cache a linked remote-only file through its open handle The cache moved the stored entry off the base of a handle open on the source, so the handle refused the link change and its next flush failed. A handle with buffered writes now publishes them first, and the cache goes through the handle as a read does, which moves its base along. Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: MapleWhiteZZZ <18653045+MapleWhiteZZZ@users.noreply.github.com> Co-authored-by: Claude Fable 5.2 Co-authored-by: Chris Lu --- weed/mount/weedfs_file_mkrm_test.go | 1 + weed/mount/weedfs_link.go | 66 +++++++++++++ weed/mount/weedfs_link_inline_test.go | 106 +++++++++++++++++++++ weed/mount/weedfs_link_open_handle_test.go | 41 ++++++++ 4 files changed, 214 insertions(+) create mode 100644 weed/mount/weedfs_link_inline_test.go diff --git a/weed/mount/weedfs_file_mkrm_test.go b/weed/mount/weedfs_file_mkrm_test.go index 2542cdd746..137efcf6ac 100644 --- a/weed/mount/weedfs_file_mkrm_test.go +++ b/weed/mount/weedfs_file_mkrm_test.go @@ -27,6 +27,7 @@ type createEntryTestServer struct { lastUID uint32 lastGID uint32 lastMode uint32 + cachedRemote *filer_pb.Entry } type createEntrySnapshot struct { diff --git a/weed/mount/weedfs_link.go b/weed/mount/weedfs_link.go index 6f6a308a23..93ff5eb0d6 100644 --- a/weed/mount/weedfs_link.go +++ b/weed/mount/weedfs_link.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "errors" + "fmt" "syscall" "time" @@ -104,6 +105,42 @@ func (wfs *WFS) Link(cancel <-chan struct{}, in *fuse.LinkIn, name string, out * oldEntry = fresh } + // A new name resolves the remote object from its own path, so the names + // have to share local chunks before the link. + if oldEntry.IsInRemoteOnly() { + // An open handle publishes its buffered writes, then caches as a read + // does, so its base moves with the stored entry. + if fh, ok := wfs.fhMap.FindFileHandle(sourceInode); ok { + if fh.dirtyMetadata || fh.dirtyPages.HasWrites() { + if flushStatus := wfs.doFlush(context.Background(), fh, in.Uid, in.Gid, false, metadataFlushTimeout); flushStatus != fuse.OK { + return flushStatus + } + } + fhLock := wfs.fhLockTable.AcquireLock("Link", fh.fh, util.SharedLock) + fh.entryLock.RLock() + entry := fh.GetEntry() + entry.RLock() + remoteOnly := entry.Entry.IsInRemoteOnly() + entry.RUnlock() + var err error + if remoteOnly { + err = fh.downloadRemoteEntry(ctx, entry) + } + fh.entryLock.RUnlock() + wfs.fhLockTable.ReleaseLock(fh.fh, fhLock) + if err != nil { + glog.V(0).Infof("Link %v: cache remote object: %v", oldEntryPath, err) + return fuse.EIO + } + } + cached, err := wfs.cacheRemoteOnlyEntry(ctx, oldEntryPath) + if err != nil { + glog.V(0).Infof("Link %v: cache remote object: %v", oldEntryPath, err) + return fuse.EIO + } + oldEntry = cached + } + // update old file to hardlink mode origHardLinkId := oldEntry.HardLinkId origHardLinkCounter := oldEntry.HardLinkCounter @@ -134,6 +171,8 @@ func (wfs *WFS) Link(cancel <-chan struct{}, in *fuse.LinkIn, name string, out * IsDirectory: false, Attributes: oldEntry.Attributes, Chunks: oldEntry.GetChunks(), + Content: oldEntry.Content, + RemoteEntry: oldEntry.RemoteEntry, Extended: oldEntry.Extended, HardLinkId: oldEntry.HardLinkId, HardLinkCounter: oldEntry.HardLinkCounter, @@ -226,6 +265,33 @@ func (wfs *WFS) Link(cancel <-chan struct{}, in *fuse.LinkIn, name string, out * return fuse.OK } +// cacheRemoteOnlyEntry has the filer download the remote object of the entry +// at path and returns the entry, in local ids, with its local chunks. +func (wfs *WFS) cacheRemoteOnlyEntry(ctx context.Context, path util.FullPath) (*filer_pb.Entry, error) { + dir, name := path.DirAndName() + var cached *filer_pb.Entry + err := wfs.withFilerClient(ctx, false, 0, func(ctx context.Context, client filer_pb.SeaweedFilerClient) error { + resp, err := client.CacheRemoteObjectToLocalCluster(ctx, &filer_pb.CacheRemoteObjectToLocalClusterRequest{ + Directory: dir, + Name: name, + }) + if err != nil { + return err + } + cached = resp.GetEntry() + return nil + }) + if err != nil { + return nil, err + } + if cached == nil || cached.IsInRemoteOnly() { + return nil, fmt.Errorf("%s is still remote-only", path) + } + cached = proto.Clone(cached).(*filer_pb.Entry) + wfs.mapPbIdFromFilerToLocal(cached) + return cached, nil +} + // adoptLinkChange moves the handle past a hard link change this mount made, // stored being the entry the filer now holds at the name the handle flushes // through, in filer ids. A base that differs from it in more than the link diff --git a/weed/mount/weedfs_link_inline_test.go b/weed/mount/weedfs_link_inline_test.go new file mode 100644 index 0000000000..9ffcf84e5e --- /dev/null +++ b/weed/mount/weedfs_link_inline_test.go @@ -0,0 +1,106 @@ +package mount + +import ( + "bytes" + "context" + "testing" + + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" + "google.golang.org/protobuf/proto" + + "github.com/seaweedfs/seaweedfs/weed/filer" + "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb" + "github.com/seaweedfs/seaweedfs/weed/util" +) + +// A small file the filer keeps inside its entry (-saveToFilerLimit, or +// ?saveInside=true on the upload) has content and no chunks. The filer writes +// a hard link's shared record from each write of one of its names, so the +// create of the new name has to carry the content as the update of the source +// does. Left out, the record lost it, and both names read zeros from then on. +func TestLinkCarriesInlineContentToTheNewName(t *testing.T) { + wfs, _ := newCreateTestWFS(t) + + content := []byte("kept inside the entry\n") + source := &filer_pb.Entry{ + Name: "inline.txt", + Content: content, + Attributes: &filer_pb.FuseAttributes{ + FileMode: 0o644, + FileSize: uint64(len(content)), + Crtime: 1, + Mtime: 1, + Uid: 99, + Gid: 100, + }, + } + if err := wfs.metaCache.InsertEntry(context.Background(), filer.FromPbEntry("/", source), 0); err != nil { + t.Fatalf("InsertEntry: %v", err) + } + sourcePath := util.FullPath("/inline.txt") + sourceInode := wfs.inodeToPath.Lookup(sourcePath, source.Attributes.Crtime, false, false, 0, true) + + doLink(t, wfs, sourceInode, "hardlink.txt") + + // The test filer returns no event, so the mount caches each name as it + // sent it, and its cache keeps one record for a hard link's names as the + // filer does: a create without the content empties it for both. + for _, path := range []util.FullPath{sourcePath, "/hardlink.txt"} { + if got := cachedLink(t, wfs, path).Content; !bytes.Equal(got, content) { + t.Fatalf("%s has content %q after the link, want %q", path, got, content) + } + } +} + +// A file under a remote mount that is not cached yet has no chunks and no +// content, and its remote object is keyed by its own path, which the new name +// does not resolve to. The link caches it first, so both names share local +// chunks and keep the RemoteEntry. +func TestLinkCachesARemoteOnlyFileForTheNewName(t *testing.T) { + wfs, server := newCreateTestWFS(t) + + remote := &filer_pb.RemoteEntry{StorageName: "cloud1", RemoteMtime: 1, RemoteSize: 4096, RemoteETag: "etag"} + attributes := &filer_pb.FuseAttributes{ + FileMode: 0o644, + FileSize: uint64(remote.RemoteSize), + Crtime: 1, + Mtime: 1, + Uid: 99, + Gid: 100, + } + source := &filer_pb.Entry{Name: "remote.bin", RemoteEntry: remote, Attributes: attributes} + if err := wfs.metaCache.InsertEntry(context.Background(), filer.FromPbEntry("/", source), 0); err != nil { + t.Fatalf("InsertEntry: %v", err) + } + chunk := &filer_pb.FileChunk{FileId: "3,01637037d6", Size: uint64(remote.RemoteSize), ModifiedTsNs: 1} + server.cachedRemote = &filer_pb.Entry{ + Name: source.Name, + RemoteEntry: remote, + Attributes: proto.Clone(attributes).(*filer_pb.FuseAttributes), + Chunks: []*filer_pb.FileChunk{chunk}, + } + sourcePath := util.FullPath("/remote.bin") + sourceInode := wfs.inodeToPath.Lookup(sourcePath, source.Attributes.Crtime, false, false, 0, true) + + doLink(t, wfs, sourceInode, "hardlink.bin") + + for _, path := range []util.FullPath{sourcePath, "/hardlink.bin"} { + got := cachedLink(t, wfs, path) + if !proto.Equal(got.Remote, remote) { + t.Fatalf("%s has remote entry %v after the link, want %v", path, got.Remote, remote) + } + if len(got.GetChunks()) != 1 || got.GetChunks()[0].GetFileIdString() != chunk.GetFileIdString() { + t.Fatalf("%s has chunks %v after the link, want the cached %v", path, got.GetChunks(), chunk) + } + } +} + +func (s *createEntryTestServer) CacheRemoteObjectToLocalCluster(ctx context.Context, req *filer_pb.CacheRemoteObjectToLocalClusterRequest) (*filer_pb.CacheRemoteObjectToLocalClusterResponse, error) { + s.mu.Lock() + defer s.mu.Unlock() + if s.cachedRemote == nil { + return nil, status.Errorf(codes.NotFound, "%s/%s", req.GetDirectory(), req.GetName()) + } + return &filer_pb.CacheRemoteObjectToLocalClusterResponse{Entry: proto.Clone(s.cachedRemote).(*filer_pb.Entry)}, nil +} diff --git a/weed/mount/weedfs_link_open_handle_test.go b/weed/mount/weedfs_link_open_handle_test.go index b887bb154b..968185ce46 100644 --- a/weed/mount/weedfs_link_open_handle_test.go +++ b/weed/mount/weedfs_link_open_handle_test.go @@ -532,3 +532,44 @@ func TestAdoptLinkChangeSkipsOlderVersion(t *testing.T) { t.Fatalf("a link change from another filer's clock was not adopted: %v", got) } } + +// CacheRemoteObjectToLocalCluster gives a remote-only entry one local chunk. +func (s *hardLinkFiler) CacheRemoteObjectToLocalCluster(ctx context.Context, r *filer_pb.CacheRemoteObjectToLocalClusterRequest) (*filer_pb.CacheRemoteObjectToLocalClusterResponse, error) { + s.mu.Lock() + defer s.mu.Unlock() + entry := s.resolve(r.Name) + if entry == nil { + return nil, status.Error(codes.NotFound, r.Name) + } + if entry.IsInRemoteOnly() { + entry.Chunks = []*filer_pb.FileChunk{linkHandleChunk(1, 0)} + entry = s.store(entry) + } + return &filer_pb.CacheRemoteObjectToLocalClusterResponse{Entry: entry, LogTsNs: entry.CommitTsNs}, nil +} + +// A remote-only file open on a handle gets a second name. The link caches the +// file through the handle, so a write through it still publishes. +func TestLinkCachesARemoteOnlyFileThroughItsOpenHandle(t *testing.T) { + wfs := newInvalidateTestWFS(t) + wfs.option.FilerMountRootPath = "/" + fake := &hardLinkFiler{records: map[string]*filer_pb.Entry{}, blobs: map[string]*filer_pb.Entry{}} + startFakeFiler(t, wfs, fake) + stored := fake.store(&filer_pb.Entry{ + Name: "tmp", + RemoteEntry: &filer_pb.RemoteEntry{StorageName: "cloud1", RemoteMtime: 1, RemoteSize: 100}, + Attributes: &filer_pb.FuseAttributes{Inode: 42, FileMode: 0o600, Uid: 2000, Gid: 20, FileSize: 100, Crtime: 1, Mtime: 1, Ctime: 1}, + }) + inode := wfs.inodeToPath.Lookup("/tmp", 1, false, false, 42, true) + wfs.mapPbIdFromFilerToLocal(stored) + fh, _, _, _ := wfs.fhMap.AcquireFileHandle(wfs, inode, stored, stored.CommitTsNs, 0) + t.Cleanup(func() { fh.dirtyPages.Destroy() }) + fh.RememberPath("/tmp") + + linkInRoot(t, wfs, inode, "final") + if code := appendAndFlush(t, wfs, fh, 2); code != fuse.OK { + t.Fatalf("flush after link: %v", code) + } + wantStored(t, fake, "tmp", 2, 2) + wantStored(t, fake, "final", 2, 2) +}