Files
seaweedfs/weed/mount/weedfs_dir_lookup.go
T
Chris LuandClaude Opus 5.5 0872953b81 mount: resolve NFS file handles across a weed mount restart (-nfsExport) (#13020)
* mount: persist a created file's entry at create time under -nfsExport

A file created over NFS gets a handle at create time. With creation
deferred to flush, a mount that stops before the flush leaves no entry for
the next mount to resolve that handle to.

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

* mount: resolve NFS file handles of files an earlier mount had open

With -nfsExport, the mount records on the filer the path of each file it
has open, keyed by inode, and drops the record a minute after the last
close. A "." lookup on a node id the mount does not know reads that
record, so the NFS server can reopen its files after the mount restarts or
another mount takes over. A failed read answers EIO rather than ESTALE.

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

* mount: record NFS handles at open, not on every path refresh

A writeback close flushes after the last release, and the flush refreshes
the handle's path. Recording there renewed the record of a file no longer
open, so it was never removed. Opens and creates now record, and a path
refresh only moves an existing record.

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

* mount: leave writeback creates deferred under -nfsExport

With writeback caching an eager create runs as an async filer RPC that an
unlink does not wait for, so a quick create+unlink could bring the name
back. Writeback mounts already accept losing unflushed files on a crash.

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

* mount: retain a recorded handle path only once it matches the file

A path read from the filer record was retained before the lookup checked
it, so a stale one kept answering ESTALE without reading the record again.
It is now retained right before LookupHandle, and never over a forgotten
inode or path the table already holds.

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

* mount: count NFS handle opens, keep other mounts' records, retry writes

A reopen that attached a new handle before the close registered its
release left a removal pending for an open file; opens are now counted.
Records carry the writing mount's signature, so a delayed removal leaves
a record another mount wrote since. A failed write is retried while the
inode's state is unchanged, and the put runs under the attempt context.

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

* mount: keep one NFS handle registration per mount in an inode's record

Two mounts with the same file open shared one record, so the one that
closed last removed the path the other still had open. Each mount now
replaces or drops only its own registration, and a lookup uses the newest.

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

* mount: name a mount's NFS handle registrations by host and mount directory

A restarted mount drew a new random signature, so it never replaced or
removed the registrations its predecessor left for files it had open.

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

* mount: take turns on an NFS handle record through the lock manager under -dlm

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

* mount: retry an NFS handle record update when its lock is not granted

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

* mount: renew an NFS handle record's lock for the whole update

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

* mount: resolve an NFS handle record to the newest path under the mount root

Another mount rooted elsewhere could hold the newest registration of a
hard-linked inode, hiding this mount's own valid path.

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

* mount: register an NFS handle at the path the mount knows now

A rename could move the inode after an opener read its path but before
the new handle was registered, so the record kept the old path; a stale
opener could also move an existing record back.

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

* mount: keep no NFS handle records with -filer.tokenFile

The filer refuses KvGet and KvPut to scoped tokens, so the records would
only fail: log at startup instead, and keep the in-process handle support.

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

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 12:31:17 -08:00

142 lines
4.1 KiB
Go

package mount
import (
"context"
"syscall"
"unicode/utf8"
"github.com/seaweedfs/go-fuse/v2/fuse"
"github.com/seaweedfs/seaweedfs/weed/filer"
"github.com/seaweedfs/seaweedfs/weed/glog"
"github.com/seaweedfs/seaweedfs/weed/util"
)
// Lookup is called by the kernel when the VFS wants to know
// about a file inside a directory. Many lookup calls can
// occur in parallel, but only one call happens for each (dir,
// name) pair.
func (wfs *WFS) Lookup(cancel <-chan struct{}, header *fuse.InHeader, name string, out *fuse.EntryOut) (code fuse.Status) {
if !utf8.ValidString(name) {
return fuse.ENOENT
}
var s fuse.Status
if name, s = checkName(name); s != fuse.OK {
return s
}
ctx, cancelFunc := ctxFromCancel(cancel)
defer cancelFunc()
var fullFilePath util.FullPath
var possibleInode uint64
if name == "." || name == ".." {
if fullFilePath, possibleInode, code = wfs.resolveDotName(ctx, header.NodeId, name); code != fuse.OK {
return code
}
if fullFilePath == util.FullPath(wfs.option.FilerMountRootPath) {
wfs.outputRootEntry(out)
return fuse.OK
}
} else {
dirPath, code := wfs.inodeToPath.GetPath(header.NodeId)
if code != fuse.OK {
return code
}
fullFilePath = dirPath.Child(name)
}
// Use shared lookup logic that checks cache first, then filer if needed
localEntry, _, status := wfs.lookupEntry(ctx, fullFilePath)
if status != fuse.OK {
if status == fuse.ENOENT && name == "." {
return fuse.Status(syscall.ESTALE)
}
return status
}
inode := possibleInode
if name == "." {
if !sameFile(inode, fullFilePath, localEntry) {
return fuse.Status(syscall.ESTALE)
}
wfs.inodeToPath.RememberForgotten(inode, fullFilePath)
if !wfs.inodeToPath.LookupHandle(fullFilePath, inode, localEntry.IsDirectory()) {
return fuse.Status(syscall.ESTALE)
}
} else {
inode = wfs.inodeToPath.Lookup(fullFilePath, localEntry.Crtime.Unix(), localEntry.IsDirectory(), len(localEntry.HardLinkId) > 0, localEntry.Inode, true)
}
if fh, found := wfs.fhMap.FindFileHandle(inode); found {
fh.entryLock.RLock()
if entry := fh.GetEntry().GetEntry(); entry != nil {
glog.V(4).Infof("lookup opened file %s size %d", fullFilePath, filer.FileSize(entry))
dir, _ := fullFilePath.DirAndName()
localEntry = filer.FromPbEntry(dir, entry)
}
fh.entryLock.RUnlock()
}
wfs.outputFilerEntry(out, inode, localEntry)
if localEntry.IsDirectory() {
wfs.applyInMemoryDirMtime(&out.Attr, inode)
if wfs.option.PosixDirNlink {
wfs.applyDirNlink(&out.Attr, fullFilePath)
}
}
return fuse.OK
}
// resolveDotName answers the lookups the kernel sends to decode a file
// handle: "." on the handle's node id, which it may have forgotten, or which
// an earlier mount had open, and ".." for a directory's parent. "." keeps the
// handle's node id, or fails ESTALE when its path now belongs to another inode.
func (wfs *WFS) resolveDotName(ctx context.Context, nodeId uint64, name string) (util.FullPath, uint64, fuse.Status) {
path, code := wfs.inodeToPath.GetPath(nodeId)
if code != fuse.OK {
forgotten, found := wfs.inodeToPath.GetForgottenPath(nodeId)
if !found {
if forgotten, code = wfs.exportedHandles.GetPath(ctx, nodeId, util.FullPath(wfs.option.FilerMountRootPath)); code != fuse.OK {
return "", 0, code
}
}
if inode, known := wfs.inodeToPath.GetInode(forgotten); known && inode != nodeId {
return "", 0, fuse.Status(syscall.ESTALE)
}
path = forgotten
}
if name == "." {
return path, nodeId, fuse.OK
}
if path == util.FullPath(wfs.option.FilerMountRootPath) {
return path, 0, fuse.OK
}
dir, _ := path.DirAndName()
return util.FullPath(dir), 0, fuse.OK
}
func (wfs *WFS) outputRootEntry(out *fuse.EntryOut) {
wfs.inodeToPath.IncrementNlookup(1)
var attr fuse.AttrOut
wfs.setRootAttr(&attr)
out.NodeId = 1
out.Generation = 1
out.EntryValid = wfs.entryValidSec
out.AttrValid = attr.AttrValid
out.Attr = attr.Attr
}
// sameFile reports whether entry, found at path, is the file inode names, and
// not one created at that path since.
func sameFile(inode uint64, path util.FullPath, entry *filer.Entry) bool {
if entry.Inode != 0 {
return entry.Inode == inode
}
return path.AsInode(entry.Crtime.Unix()) == inode
}