From e84c3f9adaee8e562ed02e64ea50262889e1efa6 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Tue, 1 Dec 2026 13:22:56 -0800 Subject: [PATCH] mount, filer: a content change clears the stored content MD5 (#13238) * mount: a content change clears the stored content MD5 Attributes.Md5 is the whole content's MD5: the S3 and WebDAV ETags and WebDAV's oc:checksums report it. A write, a size change, a grown fallocate, an O_TRUNC open or a copy_file_range into the file kept it, so those reported the old content's MD5 and an ETag that did not change. Co-Authored-By: Claude Opus 5.5 * filer: AppendToEntry clears the stored content MD5 Co-Authored-By: Claude Opus 5.5 * mount: copy_file_range clears the MD5 once it buffers bytes A cancelled copy or a later read error returned before the clear, leaving the bytes already buffered under the old MD5. Co-Authored-By: Claude Opus 5.5 * filer: an empty AppendToEntry keeps the content MD5 Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 --- weed/mount/weedfs_attr.go | 3 + weed/mount/weedfs_content_md5_test.go | 112 ++++++++++++++++++ weed/mount/weedfs_file_copy_range.go | 1 + weed/mount/weedfs_file_fallocate.go | 1 + weed/mount/weedfs_file_mkrm.go | 1 + weed/mount/weedfs_file_write.go | 1 + weed/server/filer_grpc_server.go | 3 + ..._grpc_server_append_representation_test.go | 33 ++++++ 8 files changed, 155 insertions(+) create mode 100644 weed/mount/weedfs_content_md5_test.go diff --git a/weed/mount/weedfs_attr.go b/weed/mount/weedfs_attr.go index 8f7c1ddd78..c453cf6b48 100644 --- a/weed/mount/weedfs_attr.go +++ b/weed/mount/weedfs_attr.go @@ -208,6 +208,9 @@ func (wfs *WFS) SetAttr(cancel <-chan struct{}, input *fuse.SetAttrIn, out *fuse truncNow := time.Now() entry.Attributes.Mtime = truncNow.Unix() entry.Attributes.MtimeNs = int32(truncNow.Nanosecond()) + if size != oldFileSize { + entry.Attributes.Md5 = nil + } entry.Attributes.FileSize = size if size == 0 { clearS3Encryption(entry) diff --git a/weed/mount/weedfs_content_md5_test.go b/weed/mount/weedfs_content_md5_test.go new file mode 100644 index 0000000000..5e25b1d6cb --- /dev/null +++ b/weed/mount/weedfs_content_md5_test.go @@ -0,0 +1,112 @@ +//go:build !freebsd + +package mount + +import ( + "context" + "testing" + + "github.com/seaweedfs/go-fuse/v2/fuse" + "github.com/stretchr/testify/require" + "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" +) + +var storedContentMd5 = []byte("0123456789abcdef") + +func openHandleWithMd5(t *testing.T, fileSize uint64) (*WFS, *FileHandle) { + t.Helper() + wfs, fh := newOpenFileHandle(t, fileSize) + t.Cleanup(fh.dirtyPages.Destroy) + fh.GetEntry().GetEntry().Attributes.Md5 = storedContentMd5 + return wfs, fh +} + +// The filer reports Attributes.Md5 as the content's MD5 (S3 and WebDAV +// ETags, oc:checksums): a change of the bytes through the mount drops it. +func TestContentChangeClearsMd5(t *testing.T) { + t.Run("write", func(t *testing.T) { + wfs, fh := openHandleWithMd5(t, 10) + in := &fuse.WriteIn{Fh: uint64(fh.fh), Offset: 2, Size: 3} + _, status := wfs.Write(nil, in, []byte("abc")) + require.Equal(t, fuse.OK, status) + require.Nil(t, fh.GetEntry().GetEntry().Attributes.Md5) + }) + t.Run("fallocate past the end", func(t *testing.T) { + wfs, fh := openHandleWithMd5(t, 10) + require.Equal(t, fuse.OK, wfs.Fallocate(nil, &fuse.FallocateIn{Fh: uint64(fh.fh), Offset: 4096, Length: 4096})) + require.Nil(t, fh.GetEntry().GetEntry().Attributes.Md5) + }) + t.Run("fallocate inside the file", func(t *testing.T) { + wfs, fh := openHandleWithMd5(t, 10) + require.Equal(t, fuse.OK, wfs.Fallocate(nil, &fuse.FallocateIn{Fh: uint64(fh.fh), Offset: 0, Length: 1})) + require.Equal(t, storedContentMd5, fh.GetEntry().GetEntry().Attributes.Md5) + }) + t.Run("copy_file_range into the file", func(t *testing.T) { + wfs, dst := openHandleWithMd5(t, 10) + srcInode := wfs.inodeToPath.Lookup(util.FullPath("/src.txt"), 1, false, false, 0, true) + src, _, _, _ := wfs.fhMap.AcquireFileHandle(wfs, srcInode, &filer_pb.Entry{ + Name: "src.txt", + Attributes: &filer_pb.FuseAttributes{FileMode: 0100644, FileSize: 5, Inode: srcInode}, + Content: []byte("hello"), + }, 0, 0) + _, status := wfs.CopyFileRange(make(chan struct{}), &fuse.CopyFileRangeIn{ + FhIn: uint64(src.fh), FhOut: uint64(dst.fh), OffIn: 1, OffOut: 0, Len: 4, + }) + require.Equal(t, fuse.OK, status) + require.Nil(t, dst.GetEntry().GetEntry().Attributes.Md5) + }) +} + +func TestTruncateClearsMd5(t *testing.T) { + for _, tc := range []struct { + name string + size uint64 + keepMd5 bool + oTrunc bool + }{ + {name: "shorter", size: 300}, + {name: "longer", size: 4096}, + {name: "same size", size: 768, keepMd5: true}, + {name: "open with O_TRUNC", size: 0, oTrunc: true}, + } { + t.Run(tc.name, func(t *testing.T) { + wfs := newInvalidateTestWFS(t) + current := attributeAppendEntry(3) + current.Attributes.Md5 = storedContentMd5 + fake := &attributeCASFiler{entry: proto.Clone(current).(*filer_pb.Entry)} + 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) + wfs.inodeToPath.MarkChildrenCached(util.FullPath("/dir")) + require.NoError(t, wfs.metaCache.InsertEntry(context.Background(), filer.FromPbEntry("/dir", current), 1)) + local := proto.Clone(current).(*filer_pb.Entry) + wfs.mapPbIdFromFilerToLocal(local) + fh, _, _, _ := wfs.fhMap.AcquireFileHandle(wfs, 42, local, 0, 0) + fh.baseEntry.Store(proto.Clone(local).(*filer_pb.Entry)) + fh.RememberPath(path) + t.Cleanup(fh.dirtyPages.Destroy) + + if tc.oTrunc { + require.Equal(t, fuse.OK, wfs.truncateEntry(context.Background(), path, proto.Clone(current).(*filer_pb.Entry))) + } else { + in := &fuse.SetAttrIn{} + in.NodeId, in.Valid, in.Size = 42, fuse.FATTR_SIZE, tc.size + require.Equal(t, fuse.OK, wfs.SetAttr(nil, in, &fuse.AttrOut{})) + } + + fake.mu.Lock() + defer fake.mu.Unlock() + require.Equal(t, tc.size, fake.entry.Attributes.FileSize) + if tc.keepMd5 { + require.Equal(t, storedContentMd5, fake.entry.Attributes.Md5) + } else { + require.Nil(t, fake.entry.Attributes.Md5) + } + }) + } +} diff --git a/weed/mount/weedfs_file_copy_range.go b/weed/mount/weedfs_file_copy_range.go index 4fcb343b51..94ba86b3ce 100644 --- a/weed/mount/weedfs_file_copy_range.go +++ b/weed/mount/weedfs_file_copy_range.go @@ -195,6 +195,7 @@ func (wfs *WFS) CopyFileRange(cancel <-chan struct{}, in *fuse.CopyFileRangeIn) fhOut.pageWrites.Add(1) totalCopied += buffered fhOut.dirtyContent = true + fhOut.entry.Attributes.Md5 = nil } if err != nil { glog.Errorf("AddPage error: %v", err) diff --git a/weed/mount/weedfs_file_fallocate.go b/weed/mount/weedfs_file_fallocate.go index 3ccf86ad83..eb15733fb2 100644 --- a/weed/mount/weedfs_file_fallocate.go +++ b/weed/mount/weedfs_file_fallocate.go @@ -62,6 +62,7 @@ func (wfs *WFS) Fallocate(cancel <-chan struct{}, in *fuse.FallocateIn) (code fu return writeErrorToFuseStatus(err) } entry.Attributes.FileSize = newFileSize + entry.Attributes.Md5 = nil // The writes that fill the range will not grow the file, so they charge // nothing; the growth is counted here or the quota never sees it. wfs.AddUncommittedBytes(int64(newFileSize - oldFileSize)) diff --git a/weed/mount/weedfs_file_mkrm.go b/weed/mount/weedfs_file_mkrm.go index a44f60787c..5d2ce64a2a 100644 --- a/weed/mount/weedfs_file_mkrm.go +++ b/weed/mount/weedfs_file_mkrm.go @@ -660,6 +660,7 @@ func (wfs *WFS) truncateEntry(ctx context.Context, entryFullPath util.FullPath, next.Content = nil next.Chunks = nil next.Attributes.FileSize = 0 + next.Attributes.Md5 = nil clearS3Encryption(next) truncNow := time.Now() next.Attributes.Mtime = truncNow.Unix() diff --git a/weed/mount/weedfs_file_write.go b/weed/mount/weedfs_file_write.go index 6bc3db22fa..08694d11a7 100644 --- a/weed/mount/weedfs_file_write.go +++ b/weed/mount/weedfs_file_write.go @@ -99,6 +99,7 @@ func (wfs *WFS) Write(cancel <-chan struct{}, in *fuse.WriteIn, data []byte) (wr oldFileSize := int64(entry.Attributes.FileSize) newFileSize := max(offset+int64(len(data)), oldFileSize) entry.Attributes.FileSize = uint64(newFileSize) + entry.Attributes.Md5 = nil // POSIX: writes update mtime and ctime. Stamp the entry now so the // flush path persists the write time rather than overwriting any diff --git a/weed/server/filer_grpc_server.go b/weed/server/filer_grpc_server.go index c0a7ae4f17..849f5937bd 100644 --- a/weed/server/filer_grpc_server.go +++ b/weed/server/filer_grpc_server.go @@ -1379,6 +1379,9 @@ func (fs *FilerServer) AppendToEntry(ctx context.Context, req *filer_pb.AppendTo } entry.Chunks = append(entry.GetChunks(), req.Chunks...) + if len(req.Chunks) > 0 { + entry.Md5 = nil + } so, err := fs.applyStorageDefaultsToEntry(ctx, entry) if err != nil { glog.WarningfCtx(ctx, "applyStorageDefaultsToEntry: %v", err) diff --git a/weed/server/filer_grpc_server_append_representation_test.go b/weed/server/filer_grpc_server_append_representation_test.go index 75d87f9040..694d47f931 100644 --- a/weed/server/filer_grpc_server_append_representation_test.go +++ b/weed/server/filer_grpc_server_append_representation_test.go @@ -383,3 +383,36 @@ func TestAppendToEntryAppendsAfterSparseTail(t *testing.T) { require.NoError(t, stream(&got)) require.Equal(t, []byte("old-\x00\x00\x00\x00\x00\x00new-"), got.Bytes(), "appended bytes must follow the sparse tail") } + +func TestAppendToEntryClearsContentMd5(t *testing.T) { + md5 := []byte("0123456789abcdef") + for _, tc := range []struct { + name string + req *filer_pb.AppendToEntryRequest + keepMd5 bool + }{ + {name: "append", req: appendRepresentationRequest("/data", "append.txt")}, + {name: "empty append", req: &filer_pb.AppendToEntryRequest{Directory: "/data", EntryName: "append.txt"}, keepMd5: true}, + } { + t.Run(tc.name, func(t *testing.T) { + fs, store := newAppendLockServer(t) + close(store.release) + entry := newFileEntry(appendRepresentationPath, 1) + entry.Chunks = []*filer_pb.FileChunk{{FileId: "1,01637037d6", Size: 4}} + entry.FileSize = 4 + entry.Md5 = md5 + store.entries[appendRepresentationPath] = entry + + _, err := fs.AppendToEntry(context.Background(), tc.req) + require.NoError(t, err) + + stored, err := fs.filer.FindEntry(context.Background(), appendRepresentationPath) + require.NoError(t, err) + if tc.keepMd5 { + require.Equal(t, md5, stored.Md5) + } else { + require.Nil(t, stored.Md5, "the MD5 of the content before the append") + } + }) + } +}