From bd9e9a37c2fde2b169889ba3c8cb3ceead994e43 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Sun, 29 Nov 2026 17:44:06 -0800 Subject: [PATCH] mount: send an open's truncate to the write owner as a conditional update (#13113) An open with O_TRUNC over an existing file stored the empty entry with an unconditional UpdateEntry. The filer only routes conditional writes to the path's write owner, so the truncate landed on the filer the mount talks to, which deleted the chunks it dropped, while a peer's conditional chmod could still commit at the owner over the entry it had not yet seen truncated. The owner's write then brought the deleted chunks back into the entry. The truncate now goes through saveEntryAttributes: guarded by the entry the open found, routed to the owner, and replayed on the owner's entry when refused. Co-authored-by: Claude Opus 5.5 --- weed/mount/weedfs_attr_cas_test.go | 33 ++++++++++++++++++++++ weed/mount/weedfs_file_mkrm.go | 43 ++++++++++++++++------------- weed/mount/weedfs_file_mkrm_test.go | 2 +- 3 files changed, 58 insertions(+), 20 deletions(-) diff --git a/weed/mount/weedfs_attr_cas_test.go b/weed/mount/weedfs_attr_cas_test.go index f654334777..9baab213b7 100644 --- a/weed/mount/weedfs_attr_cas_test.go +++ b/weed/mount/weedfs_attr_cas_test.go @@ -31,6 +31,7 @@ type attributeCASFiler struct { mu sync.Mutex entry *filer_pb.Entry updates, conflicts int + unconditional int beforeUpdate func(int, *filer_pb.Entry) afterCommit func() updateErr error @@ -56,6 +57,9 @@ func (s *attributeCASFiler) UpdateEntry(ctx context.Context, req *filer_pb.Updat if s.beforeUpdate != nil { s.beforeUpdate(s.updates, s.entry) } + if len(req.GetCondition().GetClauses()) == 0 { + s.unconditional++ + } if clauses := req.GetCondition().GetClauses(); len(clauses) > 0 && !proto.Equal(clauses[0].GetExpectedEntry(), s.entry) { s.conflicts++ return nil, status.Error(codes.FailedPrecondition, "entry changed") @@ -121,6 +125,35 @@ func TestAttributeOnlyConflictOutcomes(t *testing.T) { } } +// An open with O_TRUNC over a cached entry another mount has changed since +// truncates the owner's entry: the stale copy is refused, never written. +func TestTruncateEntryReplaysOnOwnerEntry(t *testing.T) { + wfs := newInvalidateTestWFS(t) + cached := attributeAppendEntry(1) + stored := attributeAppendEntry(1) + stored.Attributes.FileMode = 0640 + fake := &attributeCASFiler{entry: stored} + startFakeFiler(t, wfs, fake) + path := util.FullPath("/dir/file") + wfs.inodeToPath.Lookup(util.FullPath("/dir"), 1, true, false, 0, false) + wfs.inodeToPath.Lookup(path, 1, false, false, 42, true) + + if code := wfs.truncateEntry(context.Background(), path, cached); code != fuse.OK { + t.Fatalf("truncateEntry: %v", code) + } + fake.mu.Lock() + defer fake.mu.Unlock() + if fake.unconditional != 0 || fake.conflicts != 1 || fake.updates != 2 { + t.Fatalf("updates=%d conflicts=%d unconditional=%d, want 2, 1, 0", fake.updates, fake.conflicts, fake.unconditional) + } + if len(fake.entry.Chunks) != 0 || fake.entry.Attributes.FileSize != 0 { + t.Fatalf("stored chunks=%d size=%d, want an empty file", len(fake.entry.Chunks), fake.entry.Attributes.FileSize) + } + if fake.entry.Attributes.FileMode != 0640 { + t.Fatalf("stored mode=%o, want the peer's 640", fake.entry.Attributes.FileMode) + } +} + func (s *attributeCASFiler) CreateEntry(ctx context.Context, req *filer_pb.CreateEntryRequest) (*filer_pb.CreateEntryResponse, error) { s.mu.Lock() defer s.mu.Unlock() diff --git a/weed/mount/weedfs_file_mkrm.go b/weed/mount/weedfs_file_mkrm.go index 7a30cb0a2c..3178515b2d 100644 --- a/weed/mount/weedfs_file_mkrm.go +++ b/weed/mount/weedfs_file_mkrm.go @@ -169,7 +169,7 @@ func (wfs *WFS) openExistingForCreate(ctx context.Context, in *fuse.CreateIn, en return status } if in.Flags&syscall.O_TRUNC != 0 && in.Flags&fuse.O_ANYWRITE != 0 { - if code := wfs.truncateEntry(entryFullPath, entry); code != fuse.OK { + if code := wfs.truncateEntry(ctx, entryFullPath, entry); code != fuse.OK { wfs.ReleaseHandle(openFh) return code } @@ -614,25 +614,30 @@ func (wfs *WFS) asyncCreateEntry(dirFullPath util.FullPath, entry *filer_pb.Entr }() } -func (wfs *WFS) truncateEntry(entryFullPath util.FullPath, entry *filer_pb.Entry) fuse.Status { +// truncateEntry empties the file as a conditional update: like an attribute +// change it goes to the entry's write owner and, refused, is replayed on the +// owner's entry. Written unconditionally to another filer, it dropped chunks +// that a write already accepted by the owner put back. +func (wfs *WFS) truncateEntry(ctx context.Context, entryFullPath util.FullPath, entry *filer_pb.Entry) fuse.Status { if entry == nil { return fuse.EIO } - if entry.Attributes == nil { - entry.Attributes = &filer_pb.FuseAttributes{} - } - - entry.Content = nil - entry.Chunks = nil - entry.Attributes.FileSize = 0 - clearS3Encryption(entry) - truncNow := time.Now() - entry.Attributes.Mtime = truncNow.Unix() - entry.Attributes.MtimeNs = int32(truncNow.Nanosecond()) - entry.Attributes.Ctime = truncNow.Unix() - entry.Attributes.CtimeNs = int32(truncNow.Nanosecond()) - - if code := wfs.saveEntry(entryFullPath, entry); code != fuse.OK { + truncated, code := wfs.saveEntryAttributes(ctx, entryFullPath, entry, 0, func(next *filer_pb.Entry) fuse.Status { + if next.Attributes == nil { + next.Attributes = &filer_pb.FuseAttributes{} + } + next.Content = nil + next.Chunks = nil + next.Attributes.FileSize = 0 + clearS3Encryption(next) + truncNow := time.Now() + next.Attributes.Mtime = truncNow.Unix() + next.Attributes.MtimeNs = int32(truncNow.Nanosecond()) + next.Attributes.Ctime = truncNow.Unix() + next.Attributes.CtimeNs = int32(truncNow.Nanosecond()) + return fuse.OK + }, func(live, acked *filer_pb.Entry) {}) + if code != fuse.OK { return code } @@ -641,8 +646,8 @@ func (wfs *WFS) truncateEntry(entryFullPath util.FullPath, entry *filer_pb.Entry if fh, fhFound := wfs.fhMap.FindFileHandle(inode); fhFound { fhActiveLock := fh.wfs.fhLockTable.AcquireLock("truncateEntry", fh.fh, util.ExclusiveLock) fh.ResetDirtyPages() - fh.SetEntry(entry) - fh.setAuthoritativeBase(proto.Clone(entry).(*filer_pb.Entry)) + fh.SetEntry(truncated) + fh.setAuthoritativeBase(proto.Clone(truncated).(*filer_pb.Entry)) fh.wfs.fhLockTable.ReleaseLock(fh.fh, fhActiveLock) } } diff --git a/weed/mount/weedfs_file_mkrm_test.go b/weed/mount/weedfs_file_mkrm_test.go index 77f28c4564..3837851243 100644 --- a/weed/mount/weedfs_file_mkrm_test.go +++ b/weed/mount/weedfs_file_mkrm_test.go @@ -303,7 +303,7 @@ func TestTruncateEntryClearsDirtyPagesForOpenHandle(t *testing.T) { }, } - if status := wfs.truncateEntry(fullPath, truncatedEntry); status != fuse.OK { + if status := wfs.truncateEntry(context.Background(), fullPath, truncatedEntry); status != fuse.OK { t.Fatalf("truncateEntry status = %v, want OK", status) } if fh.dirtyPages == oldDirtyPages {