Files
seaweedfs/weed/mount/weedfs_file_write.go
T
Chris LuandClaude Opus 5.5 bb697650b4 mount: flush open files before shutdown (#13373)
* mount: flush open files before shutdown

On SIGTERM the mount unmounted and exited without uploading the buffered
writes of files still open, so a pod deletion dropped data the kernel had
already acknowledged. The interrupt hook now flushes every dirty open handle
like fsync, inside the existing 30s shutdown bound.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mount: refuse writes once the shutdown flush starts

A write admitted after its handle was flushed was acknowledged and then
lost when the process exited. The shutdown flush now sets a flag that Write
checks under the handle lock, takes every handle lock once to wait out the
writes already admitted, and reports whether every flush succeeded. The test
now buffers real bytes and checks they are uploaded and committed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mount: wait for each handle inside the shutdown flush workers

Waiting for every handle lock before flushing any file let one slow handle
use up the shutdown bound for all the others. Each worker now waits for its
own handle and flushes it if dirty.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* 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 <noreply@anthropic.com>

* mount: leave the meta cache open when the shutdown flush times out

A flush still running after the 30s bound holds its handle's lock.
Closing the meta cache drains queued invalidations, and one for that
handle waits on the same lock, so the hook blocked past its bound
until the flush gave up. Skip the close in that case, as the write
cache removal already is; the process exits right after the hooks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mount: keep the read cache dir when the shutdown flush times out

By default the write cache lives inside the read cache dir, so removing
the read cache on the timeout path deleted the swap files that the still
running flushes read, along with the meta store left open for them. Remove
both dirs only after the flushes drain. The meta store is recreated on
the next start.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mount: keep a close's writeback deferral through the shutdown flush

A writeback close marks the handle for the background drain. If the
shutdown flush started on that handle between the close's flush and its
release, it cleared the mark, so the release took the immediate path:
it fenced the handle's publication and destroyed its pages while the
shutdown flush still uploaded them. Once shutdown starts, a synchronous
flush leaves the mark, and the release drains as usual.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-04 10:24:02 -08:00

157 lines
4.7 KiB
Go

package mount
import (
"net/http"
"syscall"
"time"
"github.com/seaweedfs/go-fuse/v2/fuse"
"github.com/seaweedfs/seaweedfs/weed/glog"
"github.com/seaweedfs/seaweedfs/weed/util"
)
// isAppendWrite honors current F_SETFL flags; adapters without flags use the open.
func (wfs *WFS) isAppendWrite(in *fuse.WriteIn) bool {
if in.Flags != 0 {
return in.Flags&syscall.O_APPEND != 0
}
return wfs.fhMap.isAppendHandle(FileHandleId(in.Fh))
}
/**
* Write data
*
* Write should return exactly the number of bytes requested
* except on error. An exception to this is when the file has
* been opened in 'direct_io' mode, in which case the return value
* of the write system call will reflect the return value of this
* operation.
*
* Unless FUSE_CAP_HANDLE_KILLPRIV is disabled, this method is
* expected to reset the setuid and setgid bits.
*
* fi->fh will contain the value set by the open method, or will
* be undefined if the open method didn't set any value.
*
* Valid replies:
* fuse_reply_write
* fuse_reply_err
*
* @param req request handle
* @param ino the inode number
* @param buf data to write
* @param size number of bytes to write
* @param off offset to write to
* @param fi file information
*/
func (wfs *WFS) Write(cancel <-chan struct{}, in *fuse.WriteIn, data []byte) (written uint32, code fuse.Status) {
// Check quota including uncommitted writes for real-time enforcement
if wfs.IsOverQuotaWithUncommitted() {
return 0, fuse.Status(syscall.ENOSPC)
}
fh := wfs.GetHandle(FileHandleId(in.Fh))
if fh == nil {
return 0, fuse.ENOENT
}
fh.dirtyPages.writerPattern.MonitorWriteAt(int64(in.Offset), int(in.Size))
tsNs := time.Now().UnixNano()
fhActiveLock := fh.wfs.fhLockTable.AcquireLock("Write", fh.fh, util.ExclusiveLock)
defer fh.wfs.fhLockTable.ReleaseLock(fh.fh, fhActiveLock)
if wfs.shuttingDown.Load() {
return 0, fuse.EIO
}
entry := fh.GetEntry()
if entry == nil {
return 0, fuse.OK
}
// The bytes the filer kept inside the entry, as a write before this one.
if err := fh.bufferInlineContent(entry.Entry, tsNs); err != nil {
glog.Errorf("%v", err)
return 0, writeErrorToFuseStatus(err)
}
// Size and times cover only the bytes buffered, or the next flush
// publishes a hole where the data should be. A partly buffered write
// is reported as a short write, or as an error to kernel writeback,
// which ignores the count.
offset := int64(in.Offset)
buffered, err := fh.dirtyPages.AddPage(offset, data, fh.dirtyPages.writerPattern.IsSequentialMode(), tsNs)
if err != nil {
glog.Errorf("AddPage error: %v", err)
if buffered == 0 {
return 0, writeErrorToFuseStatus(err)
}
data = data[:buffered]
}
fh.pageWrites.Add(1)
// Only an append past all locally owned bytes needs the stored EOF guard.
if in.Offset > entry.Attributes.FileSize && wfs.isAppendWrite(in) {
if !fh.pendingAppend || in.Offset < fh.pendingAppendOffset {
fh.pendingAppendOffset = in.Offset
}
fh.pendingAppend = true
}
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
// user-set mtime (e.g., utimes/touch -m -d) at flush time.
writeNow := time.Unix(0, tsNs)
entry.Attributes.Mtime = writeNow.Unix()
entry.Attributes.MtimeNs = int32(writeNow.Nanosecond())
entry.Attributes.Ctime = writeNow.Unix()
entry.Attributes.CtimeNs = int32(writeNow.Nanosecond())
// Track uncommitted bytes for real-time quota enforcement.
// Only count the new bytes being added beyond the current file size.
if newFileSize > oldFileSize {
wfs.AddUncommittedBytes(newFileSize - oldFileSize)
}
written = uint32(len(data))
if offset == 0 {
// detect mime type
fh.contentType = http.DetectContentType(data)
}
fh.dirtyMetadata = true
fh.dirtyContent = true
// Invalidate the mtime cache so the next Open will not set FOPEN_KEEP_CACHE.
wfs.invalidateOpenMtimeCache(in.NodeId)
// POSIX: a write by a caller other than root clears the SUID and SGID
// bits. For an unknown caller the kernel decides: it clears the bits
// itself before a buffered write, and flags a direct I/O write by a
// caller without CAP_FSETID.
if in.Uid == unknownCallerId {
if in.WriteFlags&fuse.WRITE_KILL_SUIDGID != 0 {
entry.Attributes.FileMode &^= 0o6000
}
} else if in.Uid != 0 {
entry.Attributes.FileMode &^= 0o6000
}
if IsDebugFileReadWrite {
// print("+")
fh.mirrorFile.WriteAt(data, offset)
}
if err != nil && in.WriteFlags&fuse.WRITE_CACHE != 0 {
return 0, writeErrorToFuseStatus(err)
}
return written, fuse.OK
}