mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-06 02:57:37 +01:00
* mount: keep delayed events about an earlier file off the next file at its name A writer that publishes a status file by writing NNNN.json.tmp-PID, fsyncing it and renaming it over NNNN.json recreates the tmp name every few seconds. Without -writebackCache a mount applies its own events from the metadata subscription after the local apply (the events of a rename stream carry the response time, not the log position, so the dedup ring does not match them), and their invalidations reach whatever file sits at the path by then. With about 200 such writers in one directory, two of these events failed a round of the next file: - the rename of round k moved round k+1's tmp file to NNNN.json. Its handle published under that name and was refused (EIO); the retry wrote into NNNN.json, and the rename of the tmp name failed with ENOENT. - the create of round k became the entry and base of round k+1's file, and every flush of the round was refused (EIO). Both hit the new file in the window in which Create had put its handle in the map but not yet marked it an unpublished create: it did that afterwards, holding no lock, so the event found a clean handle with a base and took it for a published file. On master the second event, which carries the inode number the creating mount stored, makes the handle give the name up instead (replacedByAnotherFile) and marks it deleted: its writes are never published, and close returns success. - AcquireCreatedFileHandle puts a new handle in the state the create leaves it in before the handle is in the map. An existing handle, whose inode number outlived an earlier file, is installed under its lock. - An invalidation carries which file its event is about: the inode number the file's creator stored and its creation time (hard links excluded). An open handle ignores a rename of another file, and any other event about an earlier one (another number, created before the file held). A later file at the name still reaches the handle. - MovePath takes the inode number a rename event reports. A source that maps to another inode, while the table knows the renamed file by that number, holds another file and stays. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * mount: judge another file by its creation time only for this mount's own events A peer's file can take the name with an older creation time: renamed in from another name, or created on a host whose clock runs behind. Dropping its event left the name on the replaced file's inode. A peer's rename can also name a file by a stored inode number another file here holds, when two mounts made the same path within one second, and was refused. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * mount: a delete of another file at the name leaves the open handle alone Like a rename, a delete names the file it removed. A peer's delayed delete of an earlier file at the name, logged by a filer other than the one behind the handle's version, marked the held file deleted and dropped its writes. 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>
694 lines
25 KiB
Go
694 lines
25 KiB
Go
package mount
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"syscall"
|
|
"time"
|
|
"unicode/utf8"
|
|
|
|
"github.com/seaweedfs/go-fuse/v2/fuse"
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/cluster"
|
|
"github.com/seaweedfs/seaweedfs/weed/cluster/lock_manager"
|
|
"github.com/seaweedfs/seaweedfs/weed/filer"
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
|
)
|
|
|
|
/**
|
|
* Create and open a file
|
|
*
|
|
* If the file does not exist, first create it with the specified
|
|
* mode, and then open it.
|
|
*
|
|
* If this method is not implemented or under Linux kernel
|
|
* versions earlier than 2.6.15, the mknod() and open() methods
|
|
* will be called instead.
|
|
*/
|
|
func (wfs *WFS) Create(cancel <-chan struct{}, in *fuse.CreateIn, name string, out *fuse.CreateOut) (code fuse.Status) {
|
|
var s fuse.Status
|
|
if name, s = checkName(name); s != fuse.OK {
|
|
return s
|
|
}
|
|
|
|
dirFullPath, code := wfs.inodeToPath.GetPath(in.NodeId)
|
|
if code != fuse.OK {
|
|
return code
|
|
}
|
|
|
|
entryFullPath := dirFullPath.Child(name)
|
|
var inode uint64
|
|
|
|
ctx, cancelFunc := ctxFromCancel(cancel)
|
|
defer cancelFunc()
|
|
newEntry, _, code := wfs.maybeLoadEntry(ctx, entryFullPath)
|
|
if code == fuse.OK {
|
|
return wfs.openExistingForCreate(ctx, in, entryFullPath, newEntry, out)
|
|
}
|
|
if code != fuse.ENOENT {
|
|
return code
|
|
}
|
|
|
|
// An exclusive create goes to the filer at open, so a name another mount
|
|
// holds fails here rather than at close. Writeback assumes one writer.
|
|
deferCreate := !wfs.option.EagerFilerCreate && (in.Flags&syscall.O_EXCL == 0 || wfs.option.WritebackCache)
|
|
|
|
// Acquire the DLM lock before the filer create: a lock failure after an
|
|
// eager create would return EAGAIN while the file stays persisted.
|
|
var dlmLock *cluster.LiveLock
|
|
if wfs.lockClient != nil {
|
|
owner := fmt.Sprintf("mount-%d", wfs.signature)
|
|
dlmLock = wfs.lockClient.NewBlockingLongLivedLock(
|
|
string(entryFullPath), owner, lock_manager.LiveLockTTL,
|
|
)
|
|
if dlmLock == nil {
|
|
return fuse.Status(syscall.EAGAIN)
|
|
}
|
|
// A deferred create does not ask the filer, so find a file the
|
|
// previous lock holder created while this create waited.
|
|
if deferCreate {
|
|
if peerEntry, _, err := wfs.lookupFilerEntry(ctx, entryFullPath); err == nil {
|
|
dlmLock.Stop()
|
|
return wfs.openExistingForCreate(ctx, in, entryFullPath, peerEntry, out)
|
|
}
|
|
}
|
|
}
|
|
|
|
inode, newEntry, code = wfs.createRegularFile(ctx, dirFullPath, name, in.Mode, in.Uid, in.Gid, 0, deferCreate, true)
|
|
if code == fuse.Status(syscall.EEXIST) && in.Flags&syscall.O_EXCL == 0 {
|
|
// Race: another process created the file between our check and create.
|
|
// Reopen the winner's entry; AcquireHandle takes its own lock.
|
|
if dlmLock != nil {
|
|
dlmLock.Stop()
|
|
}
|
|
newEntry, _, code = wfs.maybeLoadEntry(ctx, entryFullPath)
|
|
if code != fuse.OK {
|
|
return code
|
|
}
|
|
return wfs.openExistingForCreate(ctx, in, entryFullPath, newEntry, out)
|
|
} else if code != fuse.OK {
|
|
if dlmLock != nil {
|
|
dlmLock.Stop()
|
|
}
|
|
return code
|
|
} else {
|
|
inode = wfs.inodeToPath.Lookup(entryFullPath, newEntry.Attributes.Crtime, false, false, inode, true)
|
|
}
|
|
|
|
wfs.outputPbEntry(&out.EntryOut, inode, newEntry)
|
|
|
|
// For deferred creates, bypass AcquireHandle (which calls maybeReadEntry
|
|
// and would fail since the entry is not yet on the filer or in the meta cache).
|
|
// We already have the entry from createRegularFile, so create the handle directly.
|
|
// A deferred create is dirty, so its filer create happens on Flush even if
|
|
// the file is closed without any writes; an eager create has already
|
|
// persisted the entry, so its handle starts clean. A new handle is in that
|
|
// state before anything can find it.
|
|
var fileHandle *FileHandle
|
|
var openFh FileHandleId
|
|
var existed, draining bool
|
|
for {
|
|
fileHandle, openFh, existed, draining = wfs.fhMap.AcquireCreatedFileHandle(wfs, inode, newEntry, deferCreate, in.Flags&syscall.O_APPEND != 0)
|
|
if !draining {
|
|
break
|
|
}
|
|
// A prior incarnation's deferred flush is still draining on this
|
|
// inode; wait for it to release the handle before attaching ours.
|
|
if !wfs.waitForPendingAsyncFlush(ctx, inode) {
|
|
return fuse.EINTR
|
|
}
|
|
}
|
|
createHandleVisible(fileHandle)
|
|
if existed {
|
|
// A create is authoritative for the entry it just made, so it takes
|
|
// effect even on a handle that outlived a previous incarnation.
|
|
// Installed under the handle's lock, as events install theirs.
|
|
fhActiveLock := wfs.fhLockTable.AcquireLock("Create", fileHandle.fh, util.ExclusiveLock)
|
|
fileHandle.SetEntry(newEntry)
|
|
fileHandle.installCreated(newEntry, deferCreate)
|
|
wfs.fhLockTable.ReleaseLock(fileHandle.fh, fhActiveLock)
|
|
}
|
|
fileHandle.RememberPath(entryFullPath)
|
|
if !existed {
|
|
wfs.exportedHandles.AcquireHandle(inode, entryFullPath)
|
|
}
|
|
|
|
// Create bypasses AcquireHandle, so attach the lock acquired above.
|
|
// A surviving handle may already hold one; ours is redundant then.
|
|
if dlmLock != nil {
|
|
if fileHandle.dlmLock == nil {
|
|
fileHandle.dlmLock = dlmLock
|
|
glog.V(1).Infof("DLM lock acquired for new file %s", entryFullPath)
|
|
} else {
|
|
dlmLock.Stop()
|
|
}
|
|
}
|
|
|
|
out.Fh = uint64(openFh)
|
|
out.OpenFlags = 0
|
|
|
|
return fuse.OK
|
|
}
|
|
|
|
// createHandleVisible runs as soon as Create's handle can be found by other
|
|
// threads. A hook for tests.
|
|
var createHandleVisible = func(fh *FileHandle) {}
|
|
|
|
// openExistingForCreate answers a create whose name already exists: an
|
|
// exclusive create fails, any other opens the entry, truncating it if asked.
|
|
func (wfs *WFS) openExistingForCreate(ctx context.Context, in *fuse.CreateIn, entryFullPath util.FullPath, entry *filer_pb.Entry, out *fuse.CreateOut) fuse.Status {
|
|
if entry == nil || entry.Attributes == nil {
|
|
return fuse.EIO
|
|
}
|
|
if in.Flags&syscall.O_EXCL != 0 {
|
|
glog.V(0).Infof("Create O_EXCL %s: already exists (uid=%d gid=%d mode=%o)",
|
|
entryFullPath, entry.Attributes.Uid, entry.Attributes.Gid, entry.Attributes.FileMode)
|
|
return fuse.Status(syscall.EEXIST)
|
|
}
|
|
inode := wfs.inodeToPath.Lookup(entryFullPath, entry.Attributes.Crtime, false, len(entry.HardLinkId) > 0, entry.Attributes.Inode, true)
|
|
fileHandle, openFh, status := wfs.AcquireHandle(ctx, inode, in.Flags, in.Uid, in.Gid)
|
|
if status != fuse.OK {
|
|
return status
|
|
}
|
|
if in.Flags&syscall.O_TRUNC != 0 && in.Flags&fuse.O_ANYWRITE != 0 {
|
|
if code := wfs.truncateEntry(ctx, entryFullPath, entry); code != fuse.OK {
|
|
wfs.ReleaseHandle(openFh)
|
|
return code
|
|
}
|
|
entry = fileHandle.GetEntry().GetEntry()
|
|
}
|
|
wfs.outputPbEntry(&out.EntryOut, inode, entry)
|
|
out.Fh = uint64(openFh)
|
|
out.OpenFlags = 0
|
|
wfs.reportOpenPath(in.Flags, entryFullPath, entry)
|
|
return fuse.OK
|
|
}
|
|
|
|
/** Create a file node
|
|
*
|
|
* This is called for creation of all non-directory, non-symlink
|
|
* nodes. If the filesystem defines a create() method, then for
|
|
* regular files that will be called instead.
|
|
*/
|
|
func (wfs *WFS) Mknod(cancel <-chan struct{}, in *fuse.MknodIn, name string, out *fuse.EntryOut) (code fuse.Status) {
|
|
|
|
var s fuse.Status
|
|
if name, s = checkName(name); s != fuse.OK {
|
|
return s
|
|
}
|
|
|
|
dirFullPath, code := wfs.inodeToPath.GetPath(in.NodeId)
|
|
if code != fuse.OK {
|
|
return
|
|
}
|
|
|
|
ctx, cancelFunc := ctxFromCancel(cancel)
|
|
defer cancelFunc()
|
|
inode, newEntry, code := wfs.createRegularFile(ctx, dirFullPath, name, in.Mode, in.Uid, in.Gid, in.Rdev, false, false)
|
|
if code != fuse.OK {
|
|
return code
|
|
}
|
|
|
|
// this is to increase nlookup counter
|
|
entryFullPath := dirFullPath.Child(name)
|
|
inode = wfs.inodeToPath.Lookup(entryFullPath, newEntry.Attributes.Crtime, false, false, inode, true)
|
|
|
|
wfs.outputPbEntry(out, inode, newEntry)
|
|
|
|
return fuse.OK
|
|
|
|
}
|
|
|
|
/** Remove a file */
|
|
func (wfs *WFS) Unlink(cancel <-chan struct{}, header *fuse.InHeader, name string) (code fuse.Status) {
|
|
|
|
if !utf8.ValidString(name) {
|
|
return fuse.ENOENT
|
|
}
|
|
|
|
dirFullPath, code := wfs.inodeToPath.GetPath(header.NodeId)
|
|
if code != fuse.OK {
|
|
if code == fuse.ENOENT {
|
|
return fuse.OK
|
|
}
|
|
return code
|
|
}
|
|
entryFullPath := dirFullPath.Child(name)
|
|
|
|
ctx, cancelFunc := ctxFromCancel(cancel)
|
|
defer cancelFunc()
|
|
entry, _, code := wfs.maybeLoadEntry(ctx, entryFullPath)
|
|
if code != fuse.OK {
|
|
if code == fuse.ENOENT {
|
|
return fuse.OK
|
|
}
|
|
return code
|
|
}
|
|
|
|
if wormEnforced, _ := wfs.wormEnforcedForEntry(entryFullPath, entry); wormEnforced {
|
|
return fuse.EPERM
|
|
}
|
|
|
|
// For hard-linked files, serialize all concurrent mutations on the
|
|
// same HardLinkId so the filer-side blob decrement and the mount-side
|
|
// sibling cache sync are observed atomically by other unlinkers.
|
|
// Without this, two concurrent unlinks on different links of the same
|
|
// file both read the same pre-decrement counter and both stamp their
|
|
// siblings to counter-1, leaving the cache one higher than the blob.
|
|
// Re-load the entry under the lock so we see any sibling update a
|
|
// prior holder just applied.
|
|
if entry != nil && len(entry.HardLinkId) > 0 {
|
|
hlKey := string(entry.HardLinkId)
|
|
lock := wfs.hardLinkLockTable.AcquireLock("unlink", hlKey, util.ExclusiveLock)
|
|
defer wfs.hardLinkLockTable.ReleaseLock(hlKey, lock)
|
|
// If another thread unlinked the entry while we waited for the
|
|
// lock, the file is already gone — return OK like the initial
|
|
// maybeLoadEntry path above. Do not fall back to the stale
|
|
// pre-lock snapshot: proceeding with its HardLinkCounter would
|
|
// reintroduce the stale-base update the lock is meant to prevent.
|
|
fresh, _, freshCode := wfs.maybeLoadEntry(ctx, entryFullPath)
|
|
if freshCode == fuse.ENOENT {
|
|
return fuse.OK
|
|
}
|
|
if freshCode != fuse.OK {
|
|
return freshCode
|
|
}
|
|
entry = fresh
|
|
}
|
|
|
|
// POSIX: enforce sticky bit on the parent directory.
|
|
if dirEntry, _, dirCode := wfs.maybeLoadEntry(ctx, dirFullPath); dirCode != fuse.OK {
|
|
return dirCode
|
|
} else if dirEntry != nil && dirEntry.Attributes != nil {
|
|
targetUid := uint32(0)
|
|
if entry != nil && entry.Attributes != nil {
|
|
targetUid = entry.Attributes.Uid
|
|
}
|
|
if code := checkStickyBit(dirEntry.Attributes.FileMode, dirEntry.Attributes.Uid, targetUid, header.Uid); code != fuse.OK {
|
|
return code
|
|
}
|
|
}
|
|
|
|
isHardLink := entry != nil && entry.Attributes != nil && len(entry.HardLinkId) > 0 && entry.HardLinkCounter > 1
|
|
var markedInode, deleteEpoch uint64
|
|
if inode, found := wfs.inodeToPath.GetInode(entryFullPath); found {
|
|
markedInode = inode
|
|
} else if entry != nil && entry.Attributes != nil && entry.Attributes.Inode != 0 {
|
|
markedInode = entry.Attributes.Inode
|
|
}
|
|
if markedInode != 0 {
|
|
if isHardLink {
|
|
// Another link keeps the content, so a queued close publishes it.
|
|
wfs.waitForPendingAsyncFlush(context.Background(), markedInode)
|
|
}
|
|
}
|
|
|
|
// first, ensure the filer store can correctly delete
|
|
glog.V(3).Infof("remove file: %v", entryFullPath)
|
|
// Always let the filer decide whether to delete chunks based on its authoritative data.
|
|
// The filer has the correct hard link count and will only delete chunks when appropriate.
|
|
deleteReq := &filer_pb.DeleteEntryRequest{
|
|
// See weedfs_dir_mkrm.go Mkdir for why Directory is sanitized.
|
|
Directory: dirFullPath.Sanitized(),
|
|
Name: name,
|
|
IsDeleteData: true,
|
|
Signatures: []int32{wfs.signature},
|
|
}
|
|
// The handle's flushes wait for the filer's answer under its flush lock
|
|
// and are marked only once the entry is gone: a flush skipped as unlinked
|
|
// before a delete the filer then refused loses its writes, and a close
|
|
// takes the handle with it.
|
|
markDeleted, unlockHandle := wfs.lockHandleForDelete(markedInode)
|
|
resp, err := wfs.streamDeleteEntry(context.Background(), deleteReq)
|
|
if err != nil {
|
|
unlockHandle()
|
|
glog.V(0).Infof("remove %s: %v", entryFullPath, err)
|
|
return fuse.EIO
|
|
}
|
|
deleteEpoch = markDeleted()
|
|
unlockHandle()
|
|
// A queued close that was waiting for the answer sees the mark; let it
|
|
// finish before the name is given up.
|
|
wfs.waitForPendingAsyncFlush(context.Background(), markedInode)
|
|
|
|
var event *filer_pb.SubscribeMetadataResponse
|
|
if resp != nil && resp.MetadataEvent != nil {
|
|
event = resp.MetadataEvent
|
|
} else {
|
|
event = metadataDeleteEvent(string(dirFullPath), name, false)
|
|
}
|
|
if applyErr := wfs.applyLocalMetadataEvent(context.Background(), event); applyErr != nil {
|
|
glog.Warningf("unlink %s: best-effort metadata apply failed: %v", entryFullPath, applyErr)
|
|
wfs.inodeToPath.InvalidateChildrenCache(dirFullPath)
|
|
}
|
|
wfs.inodeToPath.TouchDirectory(dirFullPath)
|
|
wfs.touchDirMtimeCtimeBest(dirFullPath)
|
|
|
|
// For hard-linked files, the filer's DeleteHardLink decremented the
|
|
// shared blob, but the sibling link entries in the local metacache
|
|
// still carry the pre-unlink HardLinkCounter, and the kernel has
|
|
// cached attrs on the shared inode. Resolve the shared inode before
|
|
// removing the path from inodeToPath, then propagate the decrement
|
|
// to every sibling cache entry and invalidate the kernel attr cache
|
|
// so subsequent lstats on other links see the new nlink — pjdfstest
|
|
// link/00.t asserts this after `unlink n0` leaves n1/n2 behind.
|
|
var sharedInode uint64
|
|
if isHardLink {
|
|
sharedInode = entry.Attributes.Inode
|
|
if sharedInode == 0 {
|
|
if resolved, found := wfs.inodeToPath.GetInode(entryFullPath); found {
|
|
sharedInode = resolved
|
|
}
|
|
}
|
|
}
|
|
|
|
wfs.inodeToPath.RemovePath(entryFullPath, nil)
|
|
|
|
if isHardLink && sharedInode != 0 {
|
|
decremented := proto.Clone(entry).(*filer_pb.Entry)
|
|
decremented.HardLinkCounter = entry.HardLinkCounter - 1
|
|
now := time.Now()
|
|
decremented.Attributes.Ctime = now.Unix()
|
|
decremented.Attributes.CtimeNs = int32(now.Nanosecond())
|
|
wfs.syncHardLinkSiblings(ctx, sharedInode, decremented, event.GetTsNs(), entryFullPath)
|
|
}
|
|
|
|
// Another link still names the inode, so its open handle keeps flushing
|
|
// through that name, over the count and ctime the filer stored there.
|
|
if isHardLink && markedInode != 0 {
|
|
if survivor, status := wfs.inodeToPath.GetPath(markedInode); status == fuse.OK {
|
|
if fh, found := wfs.fhMap.FindFileHandle(markedInode); found {
|
|
lock := wfs.fhLockTable.AcquireLock("Unlink", fh.fh, util.ExclusiveLock)
|
|
if err := wfs.adoptStoredLink(ctx, fh, survivor, entry.HardLinkId); err != nil {
|
|
glog.V(1).Infof("unlink %s: read %s again at the next flush: %v", entryFullPath, survivor, err)
|
|
}
|
|
wfs.fhLockTable.ReleaseLock(fh.fh, lock)
|
|
}
|
|
wfs.clearHandleDeleted(markedInode, deleteEpoch)
|
|
}
|
|
}
|
|
|
|
return fuse.OK
|
|
|
|
}
|
|
|
|
func (wfs *WFS) createRegularFile(ctx context.Context, dirFullPath util.FullPath, name string, mode uint32, uid, gid, rdev uint32, deferFilerCreate bool, skipExistenceCheck bool) (inode uint64, newEntry *filer_pb.Entry, code fuse.Status) {
|
|
if wfs.IsOverQuotaWithUncommitted() {
|
|
return 0, nil, fuse.Status(syscall.ENOSPC)
|
|
}
|
|
|
|
// Load the parent directory to validate it exists (the create RPC sets
|
|
// SkipCheckParentDirectory, so this is the only parent check). With
|
|
// default_permissions the kernel already verified write+search on it before
|
|
// Create/Mknod, so skip only the mode-bit check and its group lookup.
|
|
parentEntry, _, parentStatus := wfs.maybeLoadEntry(ctx, dirFullPath)
|
|
if parentStatus != fuse.OK {
|
|
return 0, nil, parentStatus
|
|
}
|
|
if parentEntry == nil || parentEntry.Attributes == nil {
|
|
return 0, nil, fuse.EIO
|
|
}
|
|
if !wfs.option.DefaultPermissions {
|
|
if !hasAccess(uid, gid, parentEntry.Attributes.Uid, parentEntry.Attributes.Gid, parentEntry.Attributes.FileMode, fuse.W_OK|fuse.X_OK) {
|
|
return 0, nil, fuse.Status(syscall.EACCES)
|
|
}
|
|
}
|
|
|
|
entryFullPath := dirFullPath.Child(name)
|
|
if !skipExistenceCheck {
|
|
if _, _, status := wfs.maybeLoadEntry(ctx, entryFullPath); status == fuse.OK {
|
|
return 0, nil, fuse.Status(syscall.EEXIST)
|
|
} else if status != fuse.ENOENT {
|
|
return 0, nil, status
|
|
}
|
|
}
|
|
fileMode := toOsFileMode(mode)
|
|
now := time.Now().Unix()
|
|
inode = wfs.inodeToPath.AllocateInode(entryFullPath, now)
|
|
|
|
newEntry = &filer_pb.Entry{
|
|
Name: name,
|
|
IsDirectory: false,
|
|
Attributes: &filer_pb.FuseAttributes{
|
|
Mtime: now,
|
|
Crtime: now,
|
|
Ctime: now,
|
|
FileMode: uint32(fileMode),
|
|
Uid: uid,
|
|
Gid: gid,
|
|
TtlSec: wfs.option.TtlSec,
|
|
Rdev: rdev,
|
|
Inode: inode,
|
|
},
|
|
}
|
|
|
|
if deferFilerCreate || wfs.option.WritebackCache {
|
|
// Insert a local placeholder into the metadata cache so that
|
|
// maybeLoadEntry() can find the file immediately (e.g., duplicate-
|
|
// create checks, stat, readdir).
|
|
// We use InsertEntry directly instead of applyLocalMetadataEvent to avoid
|
|
// triggering directory hot-threshold eviction that would wipe the entry.
|
|
cachedEntry := proto.Clone(newEntry).(*filer_pb.Entry)
|
|
wfs.mapPbIdFromLocalToFiler(cachedEntry)
|
|
if insertErr := wfs.metaCache.InsertEntry(context.Background(), filer.FromPbEntry(string(dirFullPath), cachedEntry), 0); insertErr != nil {
|
|
glog.Warningf("createFile %s: insert local entry: %v", entryFullPath, insertErr)
|
|
}
|
|
wfs.inodeToPath.TouchDirectory(dirFullPath)
|
|
wfs.touchDirMtimeCtimeBest(dirFullPath)
|
|
|
|
if deferFilerCreate {
|
|
// Fully deferred: the caller (Create) will build a file handle
|
|
// directly from newEntry. The actual filer entry is created by
|
|
// flushMetadataToFiler on close.
|
|
glog.V(3).Infof("createFile %s: deferred to flush", entryFullPath)
|
|
} else {
|
|
// Async create: Mknod with writeback caching. The node is
|
|
// visible locally; fire the filer RPC in the background.
|
|
wfs.asyncCreateEntry(dirFullPath, newEntry)
|
|
glog.V(3).Infof("createFile %s: async create", entryFullPath)
|
|
}
|
|
return inode, newEntry, fuse.OK
|
|
}
|
|
|
|
wfs.mapPbIdFromLocalToFiler(newEntry)
|
|
defer wfs.mapPbIdFromFilerToLocal(newEntry)
|
|
|
|
request := &filer_pb.CreateEntryRequest{
|
|
Directory: string(dirFullPath),
|
|
Entry: newEntry,
|
|
Signatures: []int32{wfs.signature},
|
|
SkipCheckParentDirectory: true,
|
|
// Without OExcl the filer stores a duplicate as an update of the
|
|
// existing file, so a name another mount holds would be replaced.
|
|
OExcl: true,
|
|
}
|
|
|
|
glog.V(1).Infof("createFile: %s", entryFullPath)
|
|
resp, err := wfs.streamCreateEntry(context.Background(), request)
|
|
if err != nil {
|
|
glog.V(0).Infof("createFile %s: %v", entryFullPath, err)
|
|
} else {
|
|
event := resp.GetMetadataEvent()
|
|
if event == nil {
|
|
event = metadataCreateEvent(string(dirFullPath), newEntry)
|
|
}
|
|
if applyErr := wfs.applyLocalMetadataEvent(context.Background(), event); applyErr != nil {
|
|
glog.Warningf("createFile %s: best-effort metadata apply failed: %v", entryFullPath, applyErr)
|
|
wfs.inodeToPath.InvalidateChildrenCache(dirFullPath)
|
|
}
|
|
wfs.inodeToPath.TouchDirectory(dirFullPath)
|
|
wfs.touchDirMtimeCtimeBest(dirFullPath)
|
|
}
|
|
|
|
glog.V(3).Infof("createFile %s: %v", entryFullPath, err)
|
|
|
|
if err != nil {
|
|
if errors.Is(err, filer_pb.ErrEntryAlreadyExists) {
|
|
// See Mkdir: the next lookup must fetch the winner's entry.
|
|
wfs.inodeToPath.InvalidateChildrenCache(dirFullPath)
|
|
return 0, nil, fuse.Status(syscall.EEXIST)
|
|
}
|
|
return 0, nil, grpcErrorToFuseStatus(err)
|
|
}
|
|
|
|
return inode, wfs.ackedBase(resp, newEntry), fuse.OK
|
|
}
|
|
|
|
// markHandleDeleted flags the inode's open handle so its flushes stop writing
|
|
// the entry back, and reports which mark this was. Taken and released under
|
|
// the handle's flush lock: a flush already holding it finishes before the
|
|
// caller's delete runs, and any later flush sees the flag; setting the flag
|
|
// bare raced the flush's own check and resurrected the entry right after the
|
|
// delete.
|
|
func (wfs *WFS) markHandleDeleted(inode uint64) uint64 {
|
|
fh, found := wfs.fhMap.FindFileHandle(inode)
|
|
if !found {
|
|
return 0
|
|
}
|
|
fhActiveLock := wfs.fhLockTable.AcquireLock("markHandleDeleted", fh.fh, util.ExclusiveLock)
|
|
fh.isDeleted = true
|
|
fh.deleteEpoch++
|
|
epoch := fh.deleteEpoch
|
|
wfs.fhLockTable.ReleaseLock(fh.fh, fhActiveLock)
|
|
return epoch
|
|
}
|
|
|
|
// handleDeleted reads the unlink mark under the handle's lock, which Unlink
|
|
// sets it under while the delete is in flight.
|
|
func (wfs *WFS) handleDeleted(fh *FileHandle) bool {
|
|
lock := wfs.fhLockTable.AcquireLock("handleDeleted", fh.fh, util.SharedLock)
|
|
defer wfs.fhLockTable.ReleaseLock(fh.fh, lock)
|
|
return fh.isDeleted
|
|
}
|
|
|
|
// lockHandleForDelete holds the flush lock of the inode's open handle over a
|
|
// delete whose outcome is not known yet. mark flags the handle as
|
|
// markHandleDeleted does, without taking the lock again.
|
|
func (wfs *WFS) lockHandleForDelete(inode uint64) (mark func() uint64, unlock func()) {
|
|
fh, found := wfs.fhMap.FindFileHandle(inode)
|
|
if !found {
|
|
return func() uint64 { return 0 }, func() {}
|
|
}
|
|
fhActiveLock := wfs.fhLockTable.AcquireLock("Unlink", fh.fh, util.ExclusiveLock)
|
|
return func() uint64 {
|
|
fh.isDeleted = true
|
|
fh.deleteEpoch++
|
|
return fh.deleteEpoch
|
|
}, func() {
|
|
wfs.fhLockTable.ReleaseLock(fh.fh, fhActiveLock)
|
|
}
|
|
}
|
|
|
|
// markHandleReplaced marks the handle of a file another mount's rename
|
|
// replaced, so its flush does not put the old content back over what took
|
|
// the name. A file this mount created and has not published was never on the
|
|
// filer, so the rename replaced nothing of it: its handle keeps its writes,
|
|
// but the name is no longer its own. Its publication is refused, so the
|
|
// writes are reported or retained instead of landing later at a name that
|
|
// was unlinked or whose directory moved since.
|
|
func (wfs *WFS) markHandleReplaced(inode uint64) {
|
|
fh, found := wfs.fhMap.FindFileHandle(inode)
|
|
if !found {
|
|
return
|
|
}
|
|
fhActiveLock := wfs.fhLockTable.AcquireLock("markHandleReplaced", fh.fh, util.ExclusiveLock)
|
|
defer wfs.fhLockTable.ReleaseLock(fh.fh, fhActiveLock)
|
|
if fh.unpublishedCreate() {
|
|
fh.nameTaken = true
|
|
return
|
|
}
|
|
fh.isDeleted = true
|
|
fh.deleteEpoch++
|
|
}
|
|
|
|
// clearHandleDeleted lifts a mark whose delete did not go through. Only the
|
|
// mark epoch names: an unlink that raised the flag after it owns the entry's
|
|
// fate and keeps it.
|
|
func (wfs *WFS) clearHandleDeleted(inode uint64, epoch uint64) {
|
|
if epoch == 0 {
|
|
return
|
|
}
|
|
fh, found := wfs.fhMap.FindFileHandle(inode)
|
|
if !found {
|
|
return
|
|
}
|
|
fhActiveLock := wfs.fhLockTable.AcquireLock("clearHandleDeleted", fh.fh, util.ExclusiveLock)
|
|
if fh.deleteEpoch == epoch {
|
|
fh.isDeleted = false
|
|
}
|
|
wfs.fhLockTable.ReleaseLock(fh.fh, fhActiveLock)
|
|
}
|
|
|
|
// asyncCreateEntry sends a CreateEntry RPC to the filer in the background.
|
|
// The entry is already in the local meta cache; this persists it to the filer.
|
|
// Used by Mknod with writeback caching — the node is visible locally right away.
|
|
//
|
|
// If the filer RPC fails after retries, the local cache entry is removed so the
|
|
// phantom file does not persist across cache invalidation or mount restart.
|
|
func (wfs *WFS) asyncCreateEntry(dirFullPath util.FullPath, entry *filer_pb.Entry) {
|
|
// Clone so the goroutine has its own copy for uid/gid mapping.
|
|
requestEntry := proto.Clone(entry).(*filer_pb.Entry)
|
|
dir := string(dirFullPath)
|
|
entryPath := dirFullPath.Child(entry.Name)
|
|
wfs.asyncCreates.Add(1)
|
|
go func() {
|
|
defer wfs.asyncCreates.Add(-1)
|
|
wfs.mapPbIdFromLocalToFiler(requestEntry)
|
|
request := &filer_pb.CreateEntryRequest{
|
|
Directory: dir,
|
|
Entry: requestEntry,
|
|
Signatures: []int32{wfs.signature},
|
|
SkipCheckParentDirectory: true,
|
|
}
|
|
err := retryMetadataFlush(context.Background(), func() error {
|
|
resp, createErr := wfs.streamCreateEntry(context.Background(), request)
|
|
if createErr != nil {
|
|
return createErr
|
|
}
|
|
event := resp.GetMetadataEvent()
|
|
if event == nil {
|
|
event = metadataCreateEvent(dir, requestEntry)
|
|
}
|
|
if applyErr := wfs.applyLocalMetadataEvent(context.Background(), event); applyErr != nil {
|
|
glog.Warningf("async createFile %s: metadata apply: %v", entryPath, applyErr)
|
|
wfs.inodeToPath.InvalidateChildrenCache(dirFullPath)
|
|
}
|
|
return nil
|
|
}, func(nextAttempt, totalAttempts int, backoff time.Duration, err error) {
|
|
glog.Warningf("async createFile %s: retrying (attempt %d/%d) after %v: %v",
|
|
entryPath, nextAttempt, totalAttempts, backoff, err)
|
|
})
|
|
if err != nil {
|
|
glog.Errorf("async createFile %s: failed after retries: %v — removing local entry", entryPath, err)
|
|
wfs.metaCache.DeleteEntry(context.Background(), entryPath)
|
|
wfs.inodeToPath.InvalidateChildrenCache(dirFullPath)
|
|
}
|
|
}()
|
|
}
|
|
|
|
// 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
|
|
}
|
|
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
|
|
next.Attributes.Md5 = nil
|
|
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
|
|
}
|
|
|
|
if inode, found := wfs.inodeToPath.GetInode(entryFullPath); found {
|
|
wfs.invalidateOpenMtimeCache(inode)
|
|
if fh, fhFound := wfs.fhMap.FindFileHandle(inode); fhFound {
|
|
fhActiveLock := fh.wfs.fhLockTable.AcquireLock("truncateEntry", fh.fh, util.ExclusiveLock)
|
|
fh.ResetDirtyPages()
|
|
fh.SetEntry(truncated)
|
|
fh.setAuthoritativeBase(proto.Clone(truncated).(*filer_pb.Entry))
|
|
fh.wfs.fhLockTable.ReleaseLock(fh.fh, fhActiveLock)
|
|
}
|
|
}
|
|
|
|
return fuse.OK
|
|
}
|