Files
seaweedfs/weed/mount/wfs_save_attrs.go
T
b4f5b1a354 mount: hold a rename's names against this mount's own publications (#12889)
* mount: hold a rename's names against this mount's own publications

A rename of an open file can fail with EIO on a filer store with optimistic
transactions (TiKV). Rename publishes the handle's deferred create first. A
FLUSH of the same handle (a forked child closing the inherited fd, say)
that looked at the dirty flag before that publication waits on the locks,
then publishes the identical entry again while the filer moves the file,
and TiKV refuses the rename's commit on the source key ("write conflict
... reason:Optimistic"). safetensors checkpoints hit this: they write
.tmpXXXXXX and rename it while the fd is still open, and our training jobs
fork a subprocess every second.

- flushMetadataToFiler checks dirtyMetadata again under its locks and
  publishes nothing for a handle another flush has already published.
- Rename holds the publication locks of both names, in path order, across
  the filer call and the local move. A flush, chmod/utimes or xattr change
  of either name through this mount waits for it, then publishes under the
  name the rename left: lockFlushTarget (content flushes, now also the
  periodic flush) and lockNodePublication (attribute changes) resolve the
  name again after the wait. A failed rename restores the target's handle
  before the locks go.

Tests: weedfs_rename_publish_test.go, with a filer that records every
write made while a rename is in flight. On master six of the seven fail,
the incident among them (the deferred create published twice); the one
that passes guards against deadlock between renames. The chmod stress run
fails on master with ENOENT. TestFlushMergeThroughChunkProxy marks its
handle dirty before it flushes: doFlush only flushes a handle with changes,
and the clean handle it used would now publish nothing.

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

* mount: give up on an attribute change whose node keeps moving

lockNodePublication went ahead on its fourth wait even when the node had
been renamed again meanwhile, and the change went to a name the node no
longer had, without that name's lock. It now returns EAGAIN then, as the
flush path already refuses after its fourth try. The stress tests accept
EAGAIN from a chmod racing back-to-back renames.

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

* mount: test the chmod that gives up on a file that keeps moving

The chmod waits for a name's lock that the test holds shared, so a
shared try fails exactly while the chmod is queued for it; the test
moves the file each time, four times, and expects EAGAIN with nothing
written.

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

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com>
2026-11-22 10:11:42 -08:00

189 lines
7.9 KiB
Go

package mount
import (
"context"
"errors"
"syscall"
"github.com/seaweedfs/go-fuse/v2/fuse"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/util"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
)
// A clean open handle must not become a dirty whole-entry write merely from
// chmod or setxattr. Pending content writes and deferred creates still use the
// handle's existing flush path, which owns their unsaved data.
func (wfs *WFS) attributeEntrySnapshot(fh *FileHandle, entry *filer_pb.Entry) (*filer_pb.Entry, bool) {
if fh == nil {
return entry, true
}
// Match Read: the handle's active lock precedes entryLock.
lock := wfs.fhLockTable.AcquireLock("attributeEntrySnapshot", fh.fh, util.SharedLock)
defer wfs.fhLockTable.ReleaseLock(fh.fh, lock)
fh.entryLock.Lock()
defer fh.entryLock.Unlock()
if fh.dirtyMetadata {
return nil, false
}
fh.entry.RLock()
defer fh.entry.RUnlock()
return proto.Clone(fh.entry.Entry).(*filer_pb.Entry), true
}
// saveEntryAttributes retries only an entry comparison miss, always replaying
// the syscall's attribute change on the new entry. In particular, the cached
// chunks, size, inline content and unrelated attributes never win a conflict.
func (wfs *WFS) saveEntryAttributes(ctx context.Context, path util.FullPath, base *filer_pb.Entry, idFields uint32, change func(*filer_pb.Entry) fuse.Status, retain func(live, acked *filer_pb.Entry)) (*filer_pb.Entry, fuse.Status) {
// Like the other metadata mutations, ignore a FUSE interrupt: the owner
// may already have applied the update the caller would be told failed.
ctx, cancel := context.WithTimeout(context.WithoutCancel(ctx), metadataFlushTimeout)
defer cancel()
dir, _ := path.DirAndName()
inode := base.GetAttributes().GetInode()
localUid, localGid := base.GetAttributes().GetUid(), base.GetAttributes().GetGid()
// The snapshot has local ids only. Where the id mapping is not one-to-one
// the filer ids derived from them can miss; the re-read supplies the real ones.
filerUid, filerGid := wfs.option.UidGidMapper.LocalToFiler(localUid, localGid)
for attempt := 0; attempt < 3; attempt++ {
if attempt > 0 {
// Bypass the mount cache: it supplied the snapshot just refused.
entry, _, err := wfs.lookupOwnerEntry(ctx, path)
if errors.Is(err, filer_pb.ErrNotFound) {
return nil, fuse.ENOENT
}
if err != nil {
return nil, grpcErrorToFuseStatus(err)
}
base = entry
filerUid, filerGid = base.GetAttributes().GetUid(), base.GetAttributes().GetGid()
wfs.mapPbIdFromFilerToLocal(base)
if inode != 0 && base.GetAttributes().GetInode() != inode {
return nil, fuse.Status(syscall.ESTALE)
}
}
next := proto.Clone(base).(*filer_pb.Entry)
if code := change(next); code != fuse.OK {
return nil, code
}
// Whole-entry clones are as large as the chunk list; switch ids in place.
// Only fields absent from the syscall keep the filer's raw ids.
// Equal local values still express an explicit chown/chgrp intent.
toFiler := func(uid, gid uint32) (uint32, uint32) {
mappedUid, mappedGid := wfs.option.UidGidMapper.LocalToFiler(uid, gid)
if idFields&fuse.FATTR_UID == 0 {
mappedUid = filerUid
}
if idFields&fuse.FATTR_GID == 0 {
mappedGid = filerGid
}
return mappedUid, mappedGid
}
restoreBase := mapIdsToFiler(base, func(_, _ uint32) (uint32, uint32) { return filerUid, filerGid })
restoreNext := mapIdsToFiler(next, toFiler)
req := &filer_pb.UpdateEntryRequest{Directory: dir, Entry: next, Condition: &filer_pb.WriteCondition{Clauses: []*filer_pb.WriteCondition_Clause{{Kind: filer_pb.WriteCondition_IF_ENTRY_EQUAL, ExpectedEntry: base}}}, Signatures: []int32{wfs.signature}}
var resp *filer_pb.UpdateEntryResponse
// The stream currently reduces FailedPrecondition to EIO. Use the unary
// RPC so a comparison miss remains distinguishable from an I/O failure.
err := wfs.withFilerClient(ctx, false, wfs.filerRPCWait(), func(ctx context.Context, client filer_pb.SeaweedFilerClient) error {
var err error
resp, err = client.UpdateEntry(ctx, req)
return err
})
restoreBase()
if status.Code(err) == codes.FailedPrecondition {
continue
}
if err != nil {
return nil, grpcErrorToFuseStatus(err)
}
wfs.applySavedEntry(path, next, resp)
restoreNext()
wfs.retainAcknowledgedAttributes(path, next, retain)
return next, fuse.OK
}
return nil, fuse.Status(syscall.EAGAIN)
}
// mapIdsToFiler maps entry's uid/gid to the filer's and returns a func that
// puts the local ids back exactly; mapping back can differ when the ids collide.
func mapIdsToFiler(entry *filer_pb.Entry, toFiler func(uid, gid uint32) (uint32, uint32)) (restore func()) {
attr := entry.GetAttributes()
if attr == nil {
return func() {}
}
uid, gid := attr.Uid, attr.Gid
attr.Uid, attr.Gid = toFiler(uid, gid)
return func() { attr.Uid, attr.Gid = uid, gid }
}
// A background WRITE may dirty the handle while the attribute RPC is in
// flight. installAckedEntry intentionally leaves its content alone. Retain
// only the acknowledged attribute delta there, so the next content flush
// cannot undo a successful syscall. Do not recheck CREATE/REPLACE or WORM:
// those preconditions were already validated by the acknowledged mutation.
func (wfs *WFS) retainAcknowledgedAttributes(path util.FullPath, acked *filer_pb.Entry, retain func(live, acked *filer_pb.Entry)) {
inode, found := wfs.inodeToPath.GetInode(path)
if !found {
return
}
fh, found := wfs.fhMap.FindFileHandle(inode)
if !found {
return
}
// Match Read: the handle's active lock precedes entryLock.
lock := wfs.fhLockTable.AcquireLock("retainAcknowledgedAttributes", fh.fh, util.ExclusiveLock)
defer wfs.fhLockTable.ReleaseLock(fh.fh, lock)
fh.entryLock.Lock()
defer fh.entryLock.Unlock()
if !fh.dirtyMetadata || fh.isDeleted {
return
}
fh.entry.Lock()
defer fh.entry.Unlock()
live := fh.entry.Entry
if acked.GetAttributes().GetInode() != 0 && live.GetAttributes().GetInode() != acked.GetAttributes().GetInode() {
return
}
retain(live, acked)
}
// A path lock covers the owner RPC through local installation/retention, and
// whole-entry metadata publication. WRITE and chunk upload remain independent.
// Keying by path also covers a handle opened while the RPC is in flight.
func (wfs *WFS) metadataPublicationTable() *util.LockTable[util.FullPath] {
wfs.metadataPublicationOnce.Do(func() { wfs.metadataPublicationLocks = util.NewLockTable[util.FullPath]() })
return wfs.metadataPublicationLocks
}
func (wfs *WFS) lockMetadataPublication(path util.FullPath) func() {
table := wfs.metadataPublicationTable()
lock := table.AcquireLock("metadata publication", path, util.ExclusiveLock)
return func() { table.ReleaseLock(path, lock) }
}
// lockNodePublication takes the publication lock of the node's entry, which
// maybeReadEntry found at path. A rename holds the locks of both its names
// until it has moved the node here, so an attribute change that waited for
// one finds the node under its new name, or under none when the rename
// replaced it: it reads the node again and locks the name it has now instead
// of writing the old one. A node renamed again each of four times it waited
// gets EAGAIN rather than a write to a name it no longer has.
func (wfs *WFS) lockNodePublication(ctx context.Context, nodeId uint64, path util.FullPath, fh *FileHandle, entry *filer_pb.Entry) (util.FullPath, *FileHandle, *filer_pb.Entry, func(), fuse.Status) {
for attempt := 0; ; attempt++ {
unlock := wfs.lockMetadataPublication(path)
if now, st := wfs.inodeToPath.GetPath(nodeId); st == fuse.OK && now == path {
return path, fh, entry, unlock, fuse.OK
}
unlock()
if attempt == 3 {
return path, fh, entry, func() {}, fuse.Status(syscall.EAGAIN)
}
var st fuse.Status
if path, fh, entry, st = wfs.maybeReadEntry(ctx, nodeId); st != fuse.OK || entry == nil || path == "" {
return path, fh, entry, func() {}, st
}
}
}