Files
seaweedfs/weed/mount/weedfs_file_lock.go
T
Chris LuandDevin 0ea818a44f mount: drain OFD locks when a file description is released (#12117)
* mount: drain OFD locks when a file description is released

The kernel scopes OFD fcntl locks to the open file description but
reports no lock_owner for them at RELEASE — it only sends one for flock
holders — so an OFD write lock taken on a file handle outlived the
holder and blocked every later open of that file (e.g. etcd failing to
reopen its WAL after a restart). The mount now records the owner of each
granted non-flock lock under its file handle and releases those owners
on RELEASE, which also covers any POSIX locks left over when FLUSH could
not run.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* mount: give every open its own fh value over the shared handle

AcquireFileHandle reuses one FileHandle per inode, so every open reported
the same fh to the kernel. A RELEASE for one description could then not
be told apart from a sibling still holding the inode open — draining
lock owners recorded under the shared fh dropped locks belonging to
descriptions that stay open.

Each open now registers its own fh value in fh2inode, all resolving to
the shared FileHandle, and RELEASE removes exactly the id that is
closing. This also stops a duplicated RELEASE on one id from
double-decrementing the open count.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* mount: forget flushed lock owners so a later release cannot re-drop locks

POSIX lock owners are process-scoped: Flush releases every lock the
process holds on the inode, but the owner stayed recorded under each
open's fh for the Release-time drain. If the process took a fresh lock
through another open before an old description finished closing, the
stale record made that RELEASE drop the new lock too.

Flush now drops the released owner from every recorded open of the
inode, so the Release drain only ever fires for owners whose locks were
never flushed.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* mount: keep close-time lock owner records when the release fails

A routed POSIX owner release that errors keeps the flush hint for a
retry, but Flush also dropped the owner from every open's release
records unconditionally — so the final Release skipped the retry and
the owner filer's lock was stranded until lease reaping. Only forget
the owner once its locks were actually released.

---------

Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-10-26 22:02:13 -07:00

92 lines
2.8 KiB
Go

package mount
import (
"github.com/seaweedfs/go-fuse/v2/fuse"
)
// GetLk queries for a conflicting lock on the file.
// If a conflict exists, the conflicting lock is returned in out.
// If no conflict, out.Lk.Typ is set to F_UNLCK.
func (wfs *WFS) GetLk(cancel <-chan struct{}, in *fuse.LkIn, out *fuse.LkOut) fuse.Status {
if wfs.crossMountLocks() {
return wfs.routedGetLk(cancel, in, out)
}
proposed := lockRange{
Start: in.Lk.Start,
End: in.Lk.End,
Typ: in.Lk.Typ,
Owner: in.Owner,
Pid: in.Lk.Pid,
IsFlock: in.LkFlags&fuse.FUSE_LK_FLOCK != 0,
}
wfs.posixLocks.GetLk(in.NodeId, proposed, out)
return fuse.OK
}
// SetLk sets or clears a POSIX lock (non-blocking).
// Returns EAGAIN if the lock conflicts with an existing lock from another owner.
func (wfs *WFS) SetLk(cancel <-chan struct{}, in *fuse.LkIn) fuse.Status {
var status fuse.Status
if wfs.crossMountLocks() {
status = wfs.routedSetLk(cancel, in)
} else {
lk := lockRange{
Start: in.Lk.Start,
End: in.Lk.End,
Typ: in.Lk.Typ,
Owner: in.Owner,
Pid: in.Lk.Pid,
IsFlock: in.LkFlags&fuse.FUSE_LK_FLOCK != 0,
}
status = wfs.posixLocks.SetLk(in.NodeId, lk)
}
wfs.noteFhLockOwner(in, status)
return status
}
// SetLkw sets a POSIX lock (blocking).
// Waits until the lock can be acquired or the request is cancelled.
func (wfs *WFS) SetLkw(cancel <-chan struct{}, in *fuse.LkIn) fuse.Status {
var status fuse.Status
if wfs.crossMountLocks() {
status = wfs.routedSetLkw(cancel, in)
} else {
lk := lockRange{
Start: in.Lk.Start,
End: in.Lk.End,
Typ: in.Lk.Typ,
Owner: in.Owner,
Pid: in.Lk.Pid,
IsFlock: in.LkFlags&fuse.FUSE_LK_FLOCK != 0,
}
if lk.Typ == f_UNLCK {
status = wfs.posixLocks.SetLk(in.NodeId, lk)
} else {
status = wfs.posixLocks.SetLkw(in.NodeId, lk, cancel)
}
}
wfs.noteFhLockOwner(in, status)
return status
}
// noteFhLockOwner remembers a granted non-flock lock owner under the open it
// was taken on, so Release can drain every lock the file description held —
// the kernel sends no cleanup notice for OFD locks.
func (wfs *WFS) noteFhLockOwner(in *fuse.LkIn, status fuse.Status) {
if status != fuse.OK || in.Lk.Typ == f_UNLCK || in.LkFlags&fuse.FUSE_LK_FLOCK != 0 {
return
}
wfs.fhLocks.add(in.Fh, in.Owner)
}
// forgetLockOwner drops a lock owner from every recorded open of the inode.
// Flush calls it only after the owner's process-wide locks were released, so
// keeping the record would let a later Release of an unrelated open drop
// locks the process acquired in between — and a failed release keeps its
// record so Release can retry.
func (wfs *WFS) forgetLockOwner(inode, owner uint64) {
for _, openFh := range wfs.fhMap.openFhIds(inode) {
wfs.fhLocks.remove(uint64(openFh), owner)
}
}