Files
seaweedfs/weed/mount/weedfs_xattr.go
T
120d786818 mount: update a directory's times once, without retries (#13443)
* mount: update a directory's times once, without retries

touchDirMtimeCtime sent its UpdateEntry through saveEntry's retries, 1, 2
and 4 s apart. Every create, link, rename and removal in the directory
waits for that update and ignores how it went. In a directory many
writers share, the update of its entry conflicts in the filer store with
theirs (TiKV reports "write conflict"), and the call waited out the
backoffs: in one directory about 200 jobs shared, three mounts logged 3646
such retries in two hours, each holding a create or a rename for up to
about 7 s.

saveEntry takes a bestEffort flag: such an update is tried once and its
failure logged at V(1). touchDirMtimeCtime sets it; setattr and the xattr
calls keep their retries.

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

* mount: shorten the directory times comments

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>
2026-12-05 11:11:43 -08:00

331 lines
8.8 KiB
Go

//go:build !freebsd
package mount
import (
"runtime"
"strings"
"syscall"
"github.com/seaweedfs/go-fuse/v2/fuse"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/util"
)
const (
// https://man7.org/linux/man-pages/man7/xattr.7.html#:~:text=The%20VFS%20imposes%20limitations%20that,in%20listxattr(2)).
MAX_XATTR_NAME_SIZE = 255
MAX_XATTR_VALUE_SIZE = 65536
XATTR_PREFIX = "xattr-" // same as filer
)
// GetXAttr reads an extended attribute, and should return the
// number of bytes. If the buffer is too small, return ERANGE,
// with the required buffer size.
func (wfs *WFS) GetXAttr(cancel <-chan struct{}, header *fuse.InHeader, attr string, dest []byte) (size uint32, code fuse.Status) {
if wfs.option.DisableXAttr {
return 0, fuse.Status(syscall.ENOTSUP)
}
//validate attr name
if len(attr) > MAX_XATTR_NAME_SIZE {
if runtime.GOOS == "darwin" {
return 0, fuse.EPERM
} else {
return 0, fuse.ERANGE
}
}
if len(attr) == 0 {
return 0, fuse.EINVAL
}
ctx, cancelFunc := ctxFromCancel(cancel)
defer cancelFunc()
_, fh, entry, status := wfs.maybeReadEntry(ctx, header.NodeId)
if status != fuse.OK {
return 0, status
}
if entry == nil {
return 0, fuse.ENOENT
}
// An open handle's Extended map is shared with the writers of its
// entry, so the value is copied out while the entry lock is held.
unlock, entry := lockExtendedForRead(fh, entry)
defer unlock()
if entry.Extended == nil {
return 0, fuse.ENOATTR
}
data, found := entry.Extended[XATTR_PREFIX+attr]
if !found {
return 0, fuse.ENOATTR
}
if len(dest) < len(data) {
return uint32(len(data)), fuse.ERANGE
}
copy(dest, data)
return uint32(len(data)), fuse.OK
}
// lockExtendedForRead returns the entry whose Extended map the caller may
// read until it calls unlock. An open handle's entry is changed in place by
// setxattr, removexattr, truncate to zero and the metadata merges, all under
// the LockedEntry lock, and SetEntry can swap the entry since maybeReadEntry
// returned it, so the read takes that lock and the handle's current entry.
// Any other entry is a private copy.
func lockExtendedForRead(fh *FileHandle, entry *filer_pb.Entry) (unlock func(), current *filer_pb.Entry) {
if fh == nil {
return func() {}, entry
}
fh.entry.RLock()
return fh.entry.RUnlock, fh.entry.Entry
}
// SetXAttr writes an extended attribute.
// https://man7.org/linux/man-pages/man2/setxattr.2.html
//
// By default (i.e., flags is zero), the extended attribute will be
// created if it does not exist, or the value will be replaced if
// the attribute already exists. To modify these semantics, one of
// the following values can be specified in flags:
//
// XATTR_CREATE
// Perform a pure create, which fails if the named attribute
// exists already.
//
// XATTR_REPLACE
// Perform a pure replace operation, which fails if the named
// attribute does not already exist.
func (wfs *WFS) SetXAttr(cancel <-chan struct{}, input *fuse.SetXAttrIn, attr string, data []byte) fuse.Status {
if wfs.option.DisableXAttr {
return fuse.Status(syscall.ENOTSUP)
}
if wfs.IsOverQuotaWithUncommitted() {
return fuse.Status(syscall.ENOSPC)
}
//validate attr name
if len(attr) > MAX_XATTR_NAME_SIZE {
if runtime.GOOS == "darwin" {
return fuse.EPERM
} else {
return fuse.ERANGE
}
}
if len(attr) == 0 {
return fuse.EINVAL
}
//validate attr value
if len(data) > MAX_XATTR_VALUE_SIZE {
if runtime.GOOS == "darwin" {
return fuse.Status(syscall.E2BIG)
} else {
return fuse.ERANGE
}
}
ctx, cancelFunc := ctxFromCancel(cancel)
defer cancelFunc()
path, fh, entry, status := wfs.maybeReadEntry(ctx, input.NodeId)
if status != fuse.OK {
return status
}
if entry == nil {
return fuse.ENOENT
}
if path != "" {
var unlock func()
if path, fh, entry, unlock, status = wfs.lockNodePublication(ctx, input.NodeId, path, fh, entry); status != fuse.OK {
return status
}
defer unlock()
if entry == nil {
return fuse.ENOENT
}
}
if snapshot, ok := wfs.attributeEntrySnapshot(fh, entry); ok && path != "" {
_, code := wfs.saveEntryAttributes(ctx, path, snapshot, 0, func(next *filer_pb.Entry) fuse.Status {
return applyXAttr(next, input.Flags, attr, data)
}, func(live, acked *filer_pb.Entry) {
if live.Extended == nil {
live.Extended = make(map[string][]byte)
}
live.Extended[XATTR_PREFIX+attr] = append([]byte(nil), acked.Extended[XATTR_PREFIX+attr]...)
})
return code
}
if fh != nil {
active := wfs.fhLockTable.AcquireLock("xattr", fh.fh, util.ExclusiveLock)
defer wfs.fhLockTable.ReleaseLock(fh.fh, active)
if wfs.shuttingDown.Load() {
return fuse.EIO
}
fh.entryLock.Lock()
defer fh.entryLock.Unlock()
// Readers take only the LockedEntry lock. Re-read the entry under
// it in case SetEntry swapped the pointer since maybeReadEntry.
fh.entry.Lock()
defer fh.entry.Unlock()
entry = fh.entry.Entry
}
if code := applyXAttr(entry, input.Flags, attr, data); code != fuse.OK {
return code
}
if fh != nil {
fh.dirtyMetadata = true
return fuse.OK
}
if path == "" {
// removed while open: the remembered entry is all there is to update
wfs.rememberRemovedDir(input.NodeId, entry)
return fuse.OK
}
return wfs.saveEntry(path, entry, false)
}
// ListXAttr lists extended attributes as '\0' delimited byte
// slice, and return the number of bytes. If the buffer is too
// small, return ERANGE, with the required buffer size.
func (wfs *WFS) ListXAttr(cancel <-chan struct{}, header *fuse.InHeader, dest []byte) (n uint32, code fuse.Status) {
if wfs.option.DisableXAttr {
return 0, fuse.Status(syscall.ENOTSUP)
}
ctx, cancelFunc := ctxFromCancel(cancel)
defer cancelFunc()
_, fh, entry, status := wfs.maybeReadEntry(ctx, header.NodeId)
if status != fuse.OK {
return 0, status
}
if entry == nil {
return 0, fuse.ENOENT
}
var data []byte
func() {
unlock, live := lockExtendedForRead(fh, entry)
defer unlock()
for k := range live.Extended {
if strings.HasPrefix(k, XATTR_PREFIX) {
data = append(data, k[len(XATTR_PREFIX):]...)
data = append(data, 0)
}
}
}()
if len(dest) < len(data) {
return uint32(len(data)), fuse.ERANGE
}
copy(dest, data)
return uint32(len(data)), fuse.OK
}
// RemoveXAttr removes an extended attribute.
func (wfs *WFS) RemoveXAttr(cancel <-chan struct{}, header *fuse.InHeader, attr string) fuse.Status {
if wfs.option.DisableXAttr {
return fuse.Status(syscall.ENOTSUP)
}
if len(attr) == 0 {
return fuse.EINVAL
}
ctx, cancelFunc := ctxFromCancel(cancel)
defer cancelFunc()
path, fh, entry, status := wfs.maybeReadEntry(ctx, header.NodeId)
if status != fuse.OK {
return status
}
if entry == nil {
return fuse.OK
}
if path != "" {
var unlock func()
if path, fh, entry, unlock, status = wfs.lockNodePublication(ctx, header.NodeId, path, fh, entry); status != fuse.OK {
return status
}
defer unlock()
if entry == nil {
return fuse.OK
}
}
if snapshot, ok := wfs.attributeEntrySnapshot(fh, entry); ok && path != "" {
_, code := wfs.saveEntryAttributes(ctx, path, snapshot, 0, func(next *filer_pb.Entry) fuse.Status {
return removeXAttr(next, attr)
}, func(live, _ *filer_pb.Entry) {
delete(live.Extended, XATTR_PREFIX+attr)
})
return code
}
if fh != nil {
active := wfs.fhLockTable.AcquireLock("xattr", fh.fh, util.ExclusiveLock)
defer wfs.fhLockTable.ReleaseLock(fh.fh, active)
if wfs.shuttingDown.Load() {
return fuse.EIO
}
fh.entryLock.Lock()
defer fh.entryLock.Unlock()
// Readers take only the LockedEntry lock. Re-read the entry under
// it in case SetEntry swapped the pointer since maybeReadEntry.
fh.entry.Lock()
defer fh.entry.Unlock()
entry = fh.entry.Entry
}
if code := removeXAttr(entry, attr); code != fuse.OK {
return code
}
if fh != nil {
fh.dirtyMetadata = true
return fuse.OK
}
if path == "" {
// removed while open: the remembered entry is all there is to update
wfs.rememberRemovedDir(header.NodeId, entry)
return fuse.OK
}
return wfs.saveEntry(path, entry, false)
}
func removeXAttr(entry *filer_pb.Entry, attr string) fuse.Status {
if _, found := entry.Extended[XATTR_PREFIX+attr]; !found {
return fuse.ENOATTR
}
delete(entry.Extended, XATTR_PREFIX+attr)
return fuse.OK
}
func applyXAttr(entry *filer_pb.Entry, flags uint32, attr string, data []byte) fuse.Status {
if entry.Extended == nil {
entry.Extended = make(map[string][]byte)
}
_, exists := entry.Extended[XATTR_PREFIX+attr]
switch flags {
case xattr_CREATE:
if exists {
return fuse.Status(syscall.EEXIST)
}
case xattr_REPLACE:
if !exists {
return fuse.ENODATA
}
}
// data aliases the FUSE request's pooled input buffer, which is
// recycled once this handler returns. Copy before storing so a
// later request reusing the buffer cannot corrupt the value.
entry.Extended[XATTR_PREFIX+attr] = append([]byte(nil), data...)
return fuse.OK
}