Files
seaweedfs/weed/mount/weedfs_link.go
T
a1764358cf filer, mount: keep a hard-linked file to one link id and count its names on the filer (#13006)
* filer, mount: keep a hard-linked file to one link id and count its names on the filer

A hard link's bookkeeping was done by the client. To link a file a mount
reads its entry and sends two writes, an update of the source and a create
of the new name, both with a link id and a count of the file's names. Two
clients that link one file at the same moment each worked from the entry
as it was before the other's writes:

- With no link id on the entry yet, each made one up. The second update
  moved the source to its id, the store took the name off the first, and
  the first client's new name was left alone on it: one file's chunks
  under two link ids.
- With a link id there, both read the same count and sent it plus one.
  The record counted one name fewer than the file had. Any write through
  a name carried the client's copy of the count and replaced the record
  the same way.

Either way the delete of the last counted name deleted the record and the
chunks while another name still read them.

The filer's own store now counts the names of a hard-linked file itself
(countLink): one more when the name written did not hold the link before,
the same when it did or when a rename moves it there (WithMovedLink), one
for a record that starts. The count sent is not used, and the entry gets
the count stored, so the response and the event carry it. The lookup of
the name, the count, the record and the name are stored as one change of
the record (changeLinkRecord), which DeleteHardLink goes through as well:
under a lock per link id, and in a transaction where the store refuses a
commit over a change made since it began, run again when it does. A
wrapper around a copy of some entries, such as a mount's metadata cache,
sees only some of the names and stores the count it is given.

UpdateEntry refuses to give a name that holds a hard link another link id
that no file has yet (refuseSecondHardLink). The mount sends a file's
first link's update as a unary RPC, which keeps that refusal apart from
an I/O failure, reads the entry again and links under the id the file has
by now.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

* filer: take a replicated update's link id without refusing it

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

* filer: look a path-specific name up outside the record's transaction

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

* filer: check the leveldb open and close it in the hard link test

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

* mount: send a refused first link again from the entry as stored

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

* mount: keep a first link's refusal over the next filer's error

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

* filer: count a hard link's names next to the clients' count instead of in its place

The filer's count replaced the one clients send, which lost two things
that count did by being worked out ahead:

- A link's first write, the update of the source, carries the count with
  the new name in it already. While the second write is on its way that
  kept a delete of the source from being taken for the delete of the
  file's last name (lastLinkDropped goes by the count it read), and the
  chunks from being deleted under the name about to come. Counted only
  when it arrived, the new name was stored with chunks that were gone:
  104 of 300 rounds of a mount linking a file while another deletes it.
- A write of a name the store refused had counted the name already, and
  sent again counted it once more.

The filer now keeps its count of the names in a key of its own next to
the record (linkNames), written once the name is stored, and the record
gets whichever is higher: the count the client sent or the names counted.
It is never lower than what the client alone would have left, and never
lower than the names the file has. A record from before has its count
recorded as the names when it is first written again.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

* mount: send a refused first link again with the file's inline content

The second attempt builds the new name's create from the entry as the
filer has it by now: attributes, chunks and extended attributes. A file
the filer keeps inside its entry has its bytes in Content, and the create
is what the filer writes the link's shared record from, so without them
the record lost the file's content.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

* filer: store a path-specific name before the record's lock is let go

A second create of the same new name on this filer took the lock between
the first one's count and its name write, missed the name, and counted it
again: the file's record and chunks then outlived its last name.

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

* filer: keep a link's names key out of the keys of a directory's entries

The key began with the link's random id. One that began as the root's
entry keys do in a store keyed by path put the names' 4 bytes in the
root's listing, and it failed to decode: listing / and the etcd scan
under fsck failed.

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

* filer: record a hard link's name before it is stored, and take it off if it is not

The names counted for a hard link were recorded once the name was stored.
On a store without transactions a failure of that write left the name
stored and not counted, and the write sent again found the name there and
did not count it either: the names recorded stayed one below the names
stored, the side a file is deleted under a name from.

They are recorded first now, inside the change of the record that counted
them, and a name the store then does not take is counted off again
(storeCountedName). A failure halfway leaves them one above the names
stored, and the file leaked. A name in a path-specific store is recorded
with the record as well, and counted off in a change of its own.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

* filer: keep a hard link's name counted when a failed write stored it

A write whose deadline passes after the store took the name returns an
error, and counting the name off then left the names one below the names
stored. A later write with the count from before the link lowered the
record to them, and the file was deleted under a name it still had. Read
the name back first, and keep it counted where that cannot be told.

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

* mount: carry the stored RemoteEntry into the name a retried first link adds

The retry rebuilds the new name from the entry the file has once another
client linked it, but kept the RemoteEntry of the entry first read.

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

---------

Co-authored-by: MapleWhiteZZZ <18653045+MapleWhiteZZZ@users.noreply.github.com>
Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com>
2026-11-25 17:54:56 -08:00

495 lines
19 KiB
Go

package mount
import (
"bytes"
"context"
"errors"
"fmt"
"syscall"
"time"
"github.com/seaweedfs/go-fuse/v2/fuse"
"google.golang.org/grpc/codes"
grpcstatus "google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
"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"
)
/*
What is an inode?
If the file is an hardlinked file:
use the hardlink id as inode
Otherwise:
use the file path as inode
When creating a link:
use the original file inode
*/
/** Create a hard link to a file */
func (wfs *WFS) Link(cancel <-chan struct{}, in *fuse.LinkIn, name string, out *fuse.EntryOut) (code fuse.Status) {
if wfs.IsOverQuotaWithUncommitted() {
return fuse.Status(syscall.ENOSPC)
}
var s fuse.Status
if name, s = checkName(name); s != fuse.OK {
return s
}
newParentPath, code := wfs.inodeToPath.GetPath(in.NodeId)
if code != fuse.OK {
return
}
oldEntryPath, code := wfs.inodeToPath.GetPath(in.Oldnodeid)
if code != fuse.OK {
return
}
oldParentPath, _ := oldEntryPath.DirAndName()
// The new link has to be reported with the node id the kernel already holds
// for the source, which is what makes the two names share one file.
// oldEntry.Attributes.Inode is not a substitute. It is a mount-runtime
// number that only entries created through a mount carry: entries written
// by the S3 API, WebDAV or a direct filer call persist inode 0, and the
// kernel rejects a LINK reply with NodeId 0 as EIO (invalid_nodeid). It is
// also the wrong key for inodeToPath, which is keyed by the numbers handed
// to the kernel rather than by the persisted attribute; Lookup's collision
// probe can move the two apart even for an entry that does carry one.
sourceInode := in.Oldnodeid
ctx, cancelFunc := ctxFromCancel(cancel)
defer cancelFunc()
// A queued close may not have created or updated the source on the filer.
if !wfs.waitForPendingAsyncFlush(ctx, sourceInode) {
return fuse.EINTR
}
oldEntry, _, status := wfs.maybeLoadEntry(ctx, oldEntryPath)
if status != fuse.OK {
return status
}
// hardlink is not allowed in WORM mode
if wormEnforced, _ := wfs.wormEnforcedForEntry(oldEntryPath, oldEntry); wormEnforced {
return fuse.EPERM
}
// If the source is already a hard link, serialize on its HardLinkId
// so concurrent Link/Unlink operations on different siblings cannot
// both compute a new counter from a stale base. Re-load the entry
// under the lock to pick up any prior holder's sibling update.
if len(oldEntry.HardLinkId) > 0 {
hlKey := string(oldEntry.HardLinkId)
lock := wfs.hardLinkLockTable.AcquireLock("link", hlKey, util.ExclusiveLock)
defer wfs.hardLinkLockTable.ReleaseLock(hlKey, lock)
// Under the lock, re-resolve the source alias from the inode.
// A concurrent Unlink that held this same lock may have removed
// the specific alias we picked pre-lock even though other
// sibling hard links for the same inode are still around;
// GetPath(Oldnodeid) returns whichever alias is still active.
refreshedPath, refreshedStatus := wfs.inodeToPath.GetPath(in.Oldnodeid)
if refreshedStatus != fuse.OK {
return refreshedStatus
}
oldEntryPath = refreshedPath
oldParentPath, _ = oldEntryPath.DirAndName()
// Do not fall back to the pre-lock snapshot: if every alias
// was deleted while we waited, abort instead of deriving the
// next counter from a stale entry.
fresh, _, freshStatus := wfs.maybeLoadEntry(ctx, oldEntryPath)
if freshStatus != fuse.OK {
return freshStatus
}
oldEntry = fresh
}
// A new name resolves the remote object from its own path, so the names
// have to share local chunks before the link.
if oldEntry.IsInRemoteOnly() {
// An open handle publishes its buffered writes, then caches as a read
// does, so its base moves with the stored entry.
if fh, ok := wfs.fhMap.FindFileHandle(sourceInode); ok {
if fh.dirtyMetadata || fh.dirtyPages.HasWrites() {
if flushStatus := wfs.doFlush(context.Background(), fh, in.Uid, in.Gid, false, metadataFlushTimeout); flushStatus != fuse.OK {
return flushStatus
}
}
fhLock := wfs.fhLockTable.AcquireLock("Link", fh.fh, util.SharedLock)
fh.entryLock.RLock()
entry := fh.GetEntry()
entry.RLock()
remoteOnly := entry.Entry.IsInRemoteOnly()
entry.RUnlock()
var err error
if remoteOnly {
err = fh.downloadRemoteEntry(ctx, entry)
}
fh.entryLock.RUnlock()
wfs.fhLockTable.ReleaseLock(fh.fh, fhLock)
if err != nil {
glog.V(0).Infof("Link %v: cache remote object: %v", oldEntryPath, err)
return fuse.EIO
}
}
cached, err := wfs.cacheRemoteOnlyEntry(ctx, oldEntryPath)
if err != nil {
glog.V(0).Infof("Link %v: cache remote object: %v", oldEntryPath, err)
return fuse.EIO
}
oldEntry = cached
}
// update old file to hardlink mode
origHardLinkId := oldEntry.HardLinkId
origHardLinkCounter := oldEntry.HardLinkCounter
if len(oldEntry.HardLinkId) == 0 {
oldEntry.HardLinkId = filer.NewHardLinkId()
oldEntry.HardLinkCounter = 1
glog.V(4).Infof("Link: new HardLinkId %x for %s", oldEntry.HardLinkId, oldEntryPath)
}
oldEntry.HardLinkCounter++
glog.V(4).Infof("Link: %s -> %s/%s HardLinkId %x counter=%d",
oldEntryPath, newParentPath, name, oldEntry.HardLinkId, oldEntry.HardLinkCounter)
updateOldEntryRequest := &filer_pb.UpdateEntryRequest{
Directory: oldParentPath,
Entry: oldEntry,
Signatures: []int32{wfs.signature},
}
// CreateLink 1.2 : update new file to hardlink mode
linkNow := time.Now()
oldEntry.Attributes.Mtime = linkNow.Unix()
oldEntry.Attributes.MtimeNs = int32(linkNow.Nanosecond())
oldEntry.Attributes.Ctime = linkNow.Unix()
oldEntry.Attributes.CtimeNs = int32(linkNow.Nanosecond())
request := &filer_pb.CreateEntryRequest{
Directory: string(newParentPath),
Entry: &filer_pb.Entry{
Name: name,
IsDirectory: false,
Attributes: oldEntry.Attributes,
Chunks: oldEntry.GetChunks(),
Content: oldEntry.Content,
RemoteEntry: oldEntry.RemoteEntry,
Extended: oldEntry.Extended,
HardLinkId: oldEntry.HardLinkId,
HardLinkCounter: oldEntry.HardLinkCounter,
},
Signatures: []int32{wfs.signature},
SkipCheckParentDirectory: true,
}
// apply changes to the filer, and also apply to local metaCache
wfs.mapPbIdFromLocalToFiler(request.Entry)
var updateResp *filer_pb.UpdateEntryResponse
var err error
if len(origHardLinkId) > 0 {
updateResp, err = wfs.streamUpdateEntry(context.Background(), updateOldEntryRequest)
} else {
// The file's first link, under a link id made up here. Another client
// may have linked the file since the entry was read, and the filer
// refuses the file a second id. The stream reduces that refusal to
// EIO; the unary RPC keeps it apart from an I/O failure.
update := func() error {
return wfs.withFilerClient(context.Background(), false, wfs.filerRPCWait(), func(ctx context.Context, client filer_pb.SeaweedFilerClient) error {
var updateErr error
updateResp, updateErr = client.UpdateEntry(ctx, updateOldEntryRequest)
if grpcstatus.Code(updateErr) == codes.FailedPrecondition {
// the filer's verdict: no other filer's error replaces it
return errors.Join(filer_pb.ErrPreconditionFailed, updateErr)
}
return updateErr
})
}
if err = update(); errors.Is(err, filer_pb.ErrPreconditionFailed) {
// link under the id the file has by now
stored, _, _, getErr := filer_pb.GetEntry(ctx, wfs, oldEntryPath)
if getErr == nil && stored != nil && len(stored.HardLinkId) > 0 && stored.Attributes != nil {
glog.V(1).Infof("Link: %s is hard link %x already, not %x", oldEntryPath, stored.HardLinkId, oldEntry.HardLinkId)
// a rollback puts back what the file has now
origHardLinkId, origHardLinkCounter = stored.HardLinkId, stored.HardLinkCounter
// both writes carry the file as stored now, not as first read
oldEntry = stored
oldEntry.HardLinkCounter++
oldEntry.Attributes.Mtime, oldEntry.Attributes.MtimeNs = linkNow.Unix(), int32(linkNow.Nanosecond())
oldEntry.Attributes.Ctime, oldEntry.Attributes.CtimeNs = linkNow.Unix(), int32(linkNow.Nanosecond())
updateOldEntryRequest.Entry = oldEntry
request.Entry.Attributes, request.Entry.Chunks, request.Entry.Extended = oldEntry.Attributes, oldEntry.GetChunks(), oldEntry.Extended
request.Entry.Content, request.Entry.RemoteEntry = oldEntry.Content, oldEntry.RemoteEntry
request.Entry.HardLinkId, request.Entry.HardLinkCounter = oldEntry.HardLinkId, oldEntry.HardLinkCounter
err = update()
}
}
}
if err == nil {
updateEvent := updateResp.GetMetadataEvent()
if updateEvent == nil {
updateEvent = metadataUpdateEvent(oldParentPath, updateOldEntryRequest.Entry)
if updateEvent != nil {
updateEvent.TsNs = ackVersionTsNs(updateResp)
}
}
if applyErr := wfs.applyLocalMetadataEvent(context.Background(), updateEvent); applyErr != nil {
glog.Warningf("link %s: best-effort metadata apply failed: %v", oldEntryPath, applyErr)
wfs.inodeToPath.InvalidateChildrenCache(util.FullPath(oldParentPath))
}
}
var linkVersionTsNs int64
var linkSignature int32
var linkedEntry *filer_pb.Entry
if err == nil {
var createResp *filer_pb.CreateEntryResponse
createResp, err = wfs.streamCreateEntry(context.Background(), request)
if err != nil {
// Rollback: restore original HardLinkId/Counter on the source entry
oldEntry.HardLinkId = origHardLinkId
oldEntry.HardLinkCounter = origHardLinkCounter
rollbackReq := &filer_pb.UpdateEntryRequest{
Directory: oldParentPath,
Entry: oldEntry,
Signatures: []int32{wfs.signature},
}
if _, rollbackErr := wfs.streamUpdateEntry(context.Background(), rollbackReq); rollbackErr != nil {
glog.Warningf("link rollback %s: %v", oldEntryPath, rollbackErr)
}
} else {
linkVersionTsNs = ackVersionTsNs(createResp)
linkSignature = createResp.GetLogSignature()
createEvent := createResp.GetMetadataEvent()
if createEvent == nil {
createEvent = metadataCreateEvent(string(newParentPath), request.Entry)
if createEvent != nil {
createEvent.TsNs = linkVersionTsNs
}
}
if stored := createEvent.GetEventNotification().GetNewEntry(); stored != nil {
linkedEntry = proto.Clone(stored).(*filer_pb.Entry)
linkedEntry.Name = oldEntry.Name
}
if applyErr := wfs.applyLocalMetadataEvent(context.Background(), createEvent); applyErr != nil {
glog.Warningf("link %s: best-effort metadata apply failed: %v", newParentPath.Child(name), applyErr)
wfs.inodeToPath.InvalidateChildrenCache(newParentPath)
}
wfs.touchDirMtimeCtimeBest(newParentPath)
}
}
newEntryPath := newParentPath.Child(name)
// Map back to local uid/gid before writing attributes to the kernel.
wfs.mapPbIdFromFilerToLocal(request.Entry)
if err != nil {
glog.V(0).Infof("Link %v -> %s: %v", oldEntryPath, newEntryPath, err)
return entryCreateErrorToFuseStatus(err)
}
wfs.inodeToPath.AddPath(sourceInode, newEntryPath)
if fh, found := wfs.fhMap.FindFileHandle(sourceInode); found {
fh.adoptLinkChange(linkedEntry, linkVersionTsNs, linkSignature)
}
// Propagate the new HardLinkCounter to sibling cache entries and
// invalidate the kernel's inode attr cache. Without this, `stat` on any
// existing sibling link (other than the source we just wrote) returns
// the old nlink from the local metacache — pjdfstest link/00.t catches
// this after `link n1 n2` when it stats n0.
wfs.syncHardLinkSiblings(ctx, sourceInode, oldEntry, linkVersionTsNs, oldEntryPath, newEntryPath)
wfs.outputPbEntry(out, sourceInode, request.Entry)
return fuse.OK
}
// cacheRemoteOnlyEntry has the filer download the remote object of the entry
// at path and returns the entry, in local ids, with its local chunks.
func (wfs *WFS) cacheRemoteOnlyEntry(ctx context.Context, path util.FullPath) (*filer_pb.Entry, error) {
dir, name := path.DirAndName()
var cached *filer_pb.Entry
err := wfs.withFilerClient(ctx, false, 0, func(ctx context.Context, client filer_pb.SeaweedFilerClient) error {
resp, err := client.CacheRemoteObjectToLocalCluster(ctx, &filer_pb.CacheRemoteObjectToLocalClusterRequest{
Directory: dir,
Name: name,
})
if err != nil {
return err
}
cached = resp.GetEntry()
return nil
})
if err != nil {
return nil, err
}
if cached == nil || cached.IsInRemoteOnly() {
return nil, fmt.Errorf("%s is still remote-only", path)
}
cached = proto.Clone(cached).(*filer_pb.Entry)
wfs.mapPbIdFromFilerToLocal(cached)
return cached, nil
}
// adoptLinkChange moves the handle past a hard link change this mount made,
// stored being the entry the filer now holds at the name the handle flushes
// through, in filer ids. A base that differs from it in more than the link
// fields and times is left alone, so a peer's write is still refused. Pending
// content stays pending.
func (fh *FileHandle) adoptLinkChange(stored *filer_pb.Entry, versionTsNs int64, signature int32) {
fhActiveLock := fh.wfs.fhLockTable.AcquireLock("adoptLinkChange", fh.fh, util.ExclusiveLock)
defer fh.wfs.fhLockTable.ReleaseLock(fh.fh, fhActiveLock)
fh.adoptLinkChangeLocked(stored, versionTsNs, signature)
}
func (fh *FileHandle) adoptLinkChangeLocked(stored *filer_pb.Entry, versionTsNs int64, signature int32) {
if stored == nil {
return
}
handleSignature := fh.entryVersionSignature.Load()
provablyOtherClock := signature != 0 && handleSignature != 0 && signature != handleSignature
if versionTsNs != 0 && !provablyOtherClock && versionTsNs <= fh.entryVersionTsNs.Load() {
return
}
if base := fh.baseEntry.Load(); base != nil && !fh.wfs.sameApartFromLink(stored, base) {
return
}
linked := proto.Clone(stored).(*filer_pb.Entry)
fh.wfs.mapPbIdFromFilerToLocal(linked)
if fh.dirtyMetadata {
fh.UpdateEntry(func(entry *filer_pb.Entry) {
entry.Name = linked.Name
entry.HardLinkId, entry.HardLinkCounter = linked.HardLinkId, linked.HardLinkCounter
if a, b := entry.Attributes, linked.Attributes; a != nil && b != nil && (b.Ctime > a.Ctime || b.Ctime == a.Ctime && b.CtimeNs > a.CtimeNs) {
a.Ctime, a.CtimeNs = b.Ctime, b.CtimeNs
}
})
} else {
fh.SetEntry(proto.Clone(linked).(*filer_pb.Entry))
}
fh.setAuthoritativeBase(linked)
fh.advanceEntryVersion(versionTsNs, signature)
}
// adoptStoredLink reads the entry stored at path and adopts it as a change of
// hardLinkId. A failed read, or a path already gone, leaves the id on the
// handle, so its next flush reads again instead of publishing over a base the
// change made stale. The caller holds the handle's lock.
func (wfs *WFS) adoptStoredLink(ctx context.Context, fh *FileHandle, path util.FullPath, hardLinkId []byte) error {
stored, tsNs, signature, err := filer_pb.GetEntry(ctx, wfs, path)
if err != nil && !errors.Is(err, filer_pb.ErrNotFound) {
fh.linkRefresh = hardLinkId
return err
}
if stored == nil {
fh.linkRefresh = hardLinkId
return nil
}
fh.linkRefresh = nil
if bytes.Equal(stored.GetHardLinkId(), hardLinkId) {
fh.adoptLinkChangeLocked(stored, tsNs, signature)
}
return nil
}
// sameApartFromLink reports whether stored, in filer ids, still holds base
// apart from what a link or unlink rewrites: the link fields, the times, the
// name, the commit stamp and the derived size.
func (wfs *WFS) sameApartFromLink(stored, base *filer_pb.Entry) bool {
current, observed := proto.Clone(stored).(*filer_pb.Entry), proto.Clone(base).(*filer_pb.Entry)
wfs.mapPbIdFromFilerToLocal(current)
for _, e := range []*filer_pb.Entry{current, observed} {
e.Name, e.CommitTsNs = "", 0
e.HardLinkId, e.HardLinkCounter = nil, 0
if e.Attributes != nil {
e.Attributes.Mtime, e.Attributes.MtimeNs = 0, 0
e.Attributes.Ctime, e.Attributes.CtimeNs = 0, 0
e.Attributes.FileSize = filer.FileSize(e)
}
filer_pb.BeforeEntrySerialization(e.Chunks)
}
return proto.Equal(current, observed)
}
// syncHardLinkSiblings refreshes every sibling link of the given inode from
// the filer, so their cached HardLinkCounter (and any other fields) reflect
// the mutation. `authoritativeEntry` identifies the hard-link group;
// `versionTsNs` is the log position the mutation committed at — the fetch
// runs after the commit, so the fetched state already covers it, and the
// recorded position is the higher of the two. `skipPaths` are the link paths
// already updated by the caller (typically the source and/or the newly
// created/removed link).
func (wfs *WFS) syncHardLinkSiblings(ctx context.Context, inode uint64, authoritativeEntry *filer_pb.Entry, versionTsNs int64, skipPaths ...util.FullPath) {
if authoritativeEntry == nil || len(authoritativeEntry.HardLinkId) == 0 {
return
}
paths := wfs.inodeToPath.GetAllPaths(inode)
if len(paths) == 0 {
return
}
skip := make(map[util.FullPath]struct{}, len(skipPaths))
for _, p := range skipPaths {
skip[p] = struct{}{}
}
// One overall deadline for the sequential sibling lookups: they run
// under the caller's exclusive hard-link lock, so a slow filer must not
// stall every other link operation on this group.
ctx, cancel := context.WithTimeout(ctx, metadataFlushTimeout)
defer cancel()
for _, p := range paths {
if _, skipped := skip[p]; skipped {
continue
}
sibling, siblingTsNs, err := wfs.metaCache.FindEntry(ctx, p)
if err != nil || sibling == nil {
continue
}
// Refresh the sibling from the filer rather than stamping the cached
// copy: the cache may predate an in-flight event of its own, and
// claiming this mutation's version for that stale content would
// fence the pending event out at the version gate, leaving old
// chunks and attributes until a refresh.
pbEntry, entryTsNs, _, fetchErr := filer_pb.GetEntry(ctx, wfs, p)
if fetchErr == nil {
// A path the filer no longer knows is on its way out — leave the
// cache alone. Only touch entries that genuinely share the same
// hard-link id: inodeToPath's shared-inode invariant should
// already guarantee this, but a mismatch can occur transiently
// (e.g. a rename replaced one of the paths).
if pbEntry == nil || !bytes.Equal(pbEntry.HardLinkId, authoritativeEntry.HardLinkId) {
continue
}
if entryTsNs < versionTsNs {
entryTsNs = versionTsNs
}
dir, _ := p.DirAndName()
if err := wfs.metaCache.UpdateEntryChecked(ctx, filer.FromPbEntry(dir, pbEntry), entryTsNs); err != nil {
glog.V(4).Infof("syncHardLinkSiblings update %s: %v", p, err)
}
continue
}
// The fetch failed: patch the counter in place so the sibling is
// not left stale, but claim only the version its content already
// holds — a newer in-flight event still lands and corrects it,
// while a replayed older delete stays fenced instead of removing
// a live link.
if !bytes.Equal(sibling.HardLinkId, authoritativeEntry.HardLinkId) {
continue
}
sibling.HardLinkCounter = authoritativeEntry.HardLinkCounter
if authoritativeEntry.Attributes != nil {
sibling.Attr.Ctime = time.Unix(authoritativeEntry.Attributes.Ctime, int64(authoritativeEntry.Attributes.CtimeNs))
}
if err := wfs.metaCache.UpdateEntry(ctx, sibling, siblingTsNs); err != nil {
glog.V(4).Infof("syncHardLinkSiblings update %s: %v", p, err)
}
}
// Note: we deliberately do NOT call fuseServer.InodeNotify here. That
// call would be made from the FUSE Link request handler goroutine, and
// writes onto the same /dev/fuse fd that the kernel is still waiting to
// read the Link reply from — causing a self-notify deadlock. The kernel
// will re-stat siblings once its attr-cache TTL expires.
}