Files
seaweedfs/weed/mount/weedfs_xattr_test.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

63 lines
2.1 KiB
Go

//go:build !freebsd
package mount
import (
"testing"
"github.com/seaweedfs/go-fuse/v2/fuse"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/util"
)
// TestSetXAttrCopiesValueBuffer reproduces the bug from
// https://github.com/seaweedfs/seaweedfs/discussions/9275 where `cp -a`
// of a file with an extended attribute leaves the destination with a
// corrupted value. The root cause is that go-fuse hands SetXAttr a
// `data` slice that aliases the per-request input buffer; when the
// buffer is returned to the pool and reused for another FUSE request,
// any reference still held by entry.Extended sees garbage.
//
// The bug only manifested when the file was open during setxattr (the
// open-fh path defers persistence to flush). We simulate that by
// mutating the caller-supplied buffer after SetXAttr returns.
func TestSetXAttrCopiesValueBuffer(t *testing.T) {
wfs := newCopyRangeTestWFS()
path := util.FullPath("/aaa.txt")
inode := wfs.inodeToPath.Lookup(path, 1, false, false, 0, true)
fh, _, _ := wfs.fhMap.AcquireFileHandle(wfs, inode, &filer_pb.Entry{
Name: "aaa.txt",
Attributes: &filer_pb.FuseAttributes{
FileMode: 0100644,
Inode: inode,
},
}, 0, 0)
fh.RememberPath(path)
// Caller buffer aliases a pool that the kernel will overwrite on
// the very next request. SetXAttr must defensively copy.
buf := []byte("test,in")
status := wfs.SetXAttr(make(chan struct{}), &fuse.SetXAttrIn{
InHeader: fuse.InHeader{NodeId: inode},
}, "user.xtags", buf)
if status != fuse.OK {
t.Fatalf("SetXAttr status = %v, want OK", status)
}
// Simulate the request buffer being recycled and reused for an
// unrelated payload.
for i := range buf {
buf[i] = 0xff
}
dest := make([]byte, 64)
n, status := wfs.GetXAttr(make(chan struct{}), &fuse.InHeader{NodeId: inode}, "user.xtags", dest)
if status != fuse.OK {
t.Fatalf("GetXAttr status = %v, want OK", status)
}
if got := string(dest[:n]); got != "test,in" {
t.Fatalf("GetXAttr value = %q, want %q (buffer aliasing leaked into stored xattr)", got, "test,in")
}
}