From a5877b3d628a0d4f0a9f5e98426ea6dcc681c0f6 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Fri, 4 Dec 2026 01:31:21 -0800 Subject: [PATCH] mount: refuse copy, fallocate and handle attribute changes during the shutdown flush copy_file_range, fallocate, and setattr/xattr on an open handle also leave their result only in the handle. One that lands after the shutdown flush has passed its handle was acknowledged and then lost at exit, as writes were before. Like Write, they now return EIO once the flush starts. Co-Authored-By: Claude Opus 5.5 --- weed/mount/weedfs.go | 3 +- weed/mount/weedfs_attr.go | 3 ++ weed/mount/weedfs_file_copy_range.go | 3 ++ weed/mount/weedfs_file_fallocate.go | 3 ++ .../weedfs_metadata_flush_shutdown_test.go | 28 +++++++++++++++++-- weed/mount/weedfs_xattr.go | 6 ++++ 6 files changed, 42 insertions(+), 4 deletions(-) diff --git a/weed/mount/weedfs.go b/weed/mount/weedfs.go index b95959a169..ed902d0ea8 100644 --- a/weed/mount/weedfs.go +++ b/weed/mount/weedfs.go @@ -237,7 +237,8 @@ type WFS struct { // asyncFlushWg tracks pending background flush work items for writebackCache mode. // Must be waited on before unmount cleanup to prevent data loss. asyncFlushWg sync.WaitGroup - // shuttingDown refuses writes once the shutdown flush has begun. + // shuttingDown refuses changes an open handle would hold in memory once + // the shutdown flush has begun. shuttingDown atomic.Bool // metaFollowWg tracks the metadata subscription goroutine and diff --git a/weed/mount/weedfs_attr.go b/weed/mount/weedfs_attr.go index c453cf6b48..33e826b03e 100644 --- a/weed/mount/weedfs_attr.go +++ b/weed/mount/weedfs_attr.go @@ -121,6 +121,9 @@ func (wfs *WFS) SetAttr(cancel <-chan struct{}, input *fuse.SetAttrIn, out *fuse } active := wfs.fhLockTable.AcquireLock("SetAttr", fh.fh, util.ExclusiveLock) defer wfs.fhLockTable.ReleaseLock(fh.fh, active) + if wfs.shuttingDown.Load() { + return fuse.EIO + } fh.entryLock.Lock() defer fh.entryLock.Unlock() // entry is the handle's shared LockedEntry.Entry. Async upload workers diff --git a/weed/mount/weedfs_file_copy_range.go b/weed/mount/weedfs_file_copy_range.go index 94ba86b3ce..1603ef323a 100644 --- a/weed/mount/weedfs_file_copy_range.go +++ b/weed/mount/weedfs_file_copy_range.go @@ -95,6 +95,9 @@ func (wfs *WFS) CopyFileRange(cancel <-chan struct{}, in *fuse.CopyFileRangeIn) // lock source and target file handles unlockHandles := lockCopyFileRangeHandles(fhIn, fhOut) defer unlockHandles() + if wfs.shuttingDown.Load() { + return 0, fuse.EIO + } if fhOut.entry == nil { return 0, fuse.ENOENT diff --git a/weed/mount/weedfs_file_fallocate.go b/weed/mount/weedfs_file_fallocate.go index eb15733fb2..ed84e08c4a 100644 --- a/weed/mount/weedfs_file_fallocate.go +++ b/weed/mount/weedfs_file_fallocate.go @@ -32,6 +32,9 @@ func (wfs *WFS) Fallocate(cancel <-chan struct{}, in *fuse.FallocateIn) (code fu fhActiveLock := fh.wfs.fhLockTable.AcquireLock("Fallocate", fh.fh, util.ExclusiveLock) defer fh.wfs.fhLockTable.ReleaseLock(fh.fh, fhActiveLock) + if wfs.shuttingDown.Load() { + return fuse.EIO + } entry := fh.GetEntry().GetEntry() if entry == nil { diff --git a/weed/mount/weedfs_metadata_flush_shutdown_test.go b/weed/mount/weedfs_metadata_flush_shutdown_test.go index b252a734c4..8611497552 100644 --- a/weed/mount/weedfs_metadata_flush_shutdown_test.go +++ b/weed/mount/weedfs_metadata_flush_shutdown_test.go @@ -7,7 +7,7 @@ import ( ) // Shutdown uploads the bytes an open file still buffers and commits them, then -// refuses further writes instead of acknowledging data it cannot keep. +// refuses further changes instead of acknowledging data it cannot keep. func TestFlushOpenFilesUploadsBufferedWrites(t *testing.T) { cluster := newProofCluster() _, addr := newProofFiler(t, cluster, newVolumeStub(t)) @@ -23,7 +23,29 @@ func TestFlushOpenFilesUploadsBufferedWrites(t *testing.T) { if fh.dirtyMetadata { t.Fatal("handle still dirty after the shutdown flush") } - if _, code := wfs.Write(nil, &fuse.WriteIn{Fh: uint64(fh.fh), Size: 1}, []byte("x")); code != fuse.EIO { - t.Fatalf("write after the shutdown flush = %v, want EIO", code) + wfs.copyBufferPool.New = func() any { return make([]byte, 1024) } + in := fuse.InHeader{NodeId: fh.inode} + for name, change := range map[string]func() fuse.Status{ + "write": func() fuse.Status { + _, code := wfs.Write(nil, &fuse.WriteIn{Fh: uint64(fh.fh), Size: 1}, []byte("x")) + return code + }, + "copy_file_range": func() fuse.Status { + _, code := wfs.CopyFileRange(nil, &fuse.CopyFileRangeIn{FhIn: uint64(fh.fh), OffIn: 1 << 20, FhOut: uint64(fh.fh), Len: 1}) + return code + }, + "fallocate": func() fuse.Status { + return wfs.Fallocate(nil, &fuse.FallocateIn{Fh: uint64(fh.fh), Length: 1 << 20}) + }, + "truncate": func() fuse.Status { + return wfs.SetAttr(nil, &fuse.SetAttrIn{SetAttrInCommon: fuse.SetAttrInCommon{InHeader: in, Valid: fuse.FATTR_SIZE | fuse.FATTR_FH, Fh: uint64(fh.fh), Size: 1}}, &fuse.AttrOut{}) + }, + } { + if code := change(); code != fuse.EIO { + t.Errorf("%s after the shutdown flush = %v, want EIO", name, code) + } + } + if fh.dirtyMetadata { + t.Fatal("a refused change left the handle dirty") } } diff --git a/weed/mount/weedfs_xattr.go b/weed/mount/weedfs_xattr.go index af1e45906e..7ff1667a2b 100644 --- a/weed/mount/weedfs_xattr.go +++ b/weed/mount/weedfs_xattr.go @@ -160,6 +160,9 @@ func (wfs *WFS) SetXAttr(cancel <-chan struct{}, input *fuse.SetXAttrIn, attr st if fh != nil { active := wfs.fhLockTable.AcquireLock("xattr", fh.fh, util.ExclusiveLock) defer wfs.fhLockTable.ReleaseLock(fh.fh, active) + if wfs.shuttingDown.Load() { + return fuse.EIO + } fh.entryLock.Lock() defer fh.entryLock.Unlock() // Readers take only the LockedEntry lock. Re-read the entry under @@ -265,6 +268,9 @@ func (wfs *WFS) RemoveXAttr(cancel <-chan struct{}, header *fuse.InHeader, attr if fh != nil { active := wfs.fhLockTable.AcquireLock("xattr", fh.fh, util.ExclusiveLock) defer wfs.fhLockTable.ReleaseLock(fh.fh, active) + if wfs.shuttingDown.Load() { + return fuse.EIO + } fh.entryLock.Lock() defer fh.entryLock.Unlock() // Readers take only the LockedEntry lock. Re-read the entry under