Files
Ayush RanjanandgVisor bot 6b2855f537 gofer: Add full xattr support.
- gofer client: Added an xattr cache on the inode struct. In case the gofer
  mount being used is exclusive (cachedMetadataAuthoritative() == true), we
  cache positive and negative xattr queries.
- Subsequently, we now hold metadataMu for the entire duration of xattr
  operations. This is important to avoid stale updates in the xattr cache due
  to concurrent xattr operations. This is the same approach we take for
  maintaining other inode metadata in the inode struct.
- Added support for setxattr/removexattr/listxattr in runsc/fsgofer and gofer
  client (when using directfs). Consequently the seccomp filters for sandbox
  and gofer processes were expanded to allow these syscalls.
- Added xattr syscall tests to //test/syscalls/BUILD so they are run on
  BuildKite on presubmits. (Also fixed up some tests.)

A note about performance:

The default rootfs configuration for runsc enables rootfs overlay. In that, the
gofer mount is an exclusive mount on the lower layer. fsimpl/overlay issues a
lot of getxattr calls on the lower layer to check for opaque directory xattr.
However, as before, the "trusted" xattr namespace is not supported. So all
those calls continue to fail with EOPNOTSUPP. So this should not regress rootfs
overlay performance.

When using an overlay where gofer is the upper layer or when "userxattr" mount
option is in effect, fsimpl/overlay will similarly issue a lot of getxattr
calls using the "user" xattr namespace. This will be allowed by
checkXattrPermissions(). To optimize the common case where directories are not
opaque, we specially track `overlayOpaqueNegative` in `xattrCache`. This avoids
a map allocation during xattrCache.addNegative().

Updates #12475
Updates #12828

PiperOrigin-RevId: 915097116
2026-05-13 15:42:32 -07:00

106 lines
2.8 KiB
Go

// Copyright 2026 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package gofer
import "slices"
// xattrOverlayOpaque is the name of the overlayfs opaque xattr. Note that
// gofer does not support the trusted xattr namespace.
const xattrOverlayOpaque = "user.overlay.opaque"
type xattrCache struct {
// values caches existing extended attributes.
values map[string]string
// negatives tracks xattrs known not to exist (linuxerr.ENODATA).
negatives map[string]struct{}
// overlayOpaqueNegative tracks if "user.overlay.opaque" is known not to
// exist (linuxerr.ENODATA). This avoids allocating xattrsNegative map in
// the common case.
overlayOpaqueNegative bool
// list caches the full list of xattr names. If nil, the list is not cached.
// If non-nil but empty, the cache is authoritatively empty.
list []string
}
func (c *xattrCache) get(name string) (val string, negative bool, found bool) {
if cachedVal, ok := c.values[name]; ok {
found = true
val = cachedVal
return
}
if name == xattrOverlayOpaque && c.overlayOpaqueNegative {
found = true
negative = true
return
}
if _, ok := c.negatives[name]; ok {
found = true
negative = true
return
}
return
}
func (c *xattrCache) add(name string, value string) {
if c.values == nil {
c.values = make(map[string]string)
}
c.values[name] = value
if name == xattrOverlayOpaque {
c.overlayOpaqueNegative = false
}
delete(c.negatives, name)
if c.list != nil {
if !slices.Contains(c.list, name) {
c.list = append(c.list, name)
}
}
}
func (c *xattrCache) addNegative(name string) {
delete(c.values, name)
if name == xattrOverlayOpaque {
c.overlayOpaqueNegative = true
} else {
if c.negatives == nil {
c.negatives = make(map[string]struct{})
}
c.negatives[name] = struct{}{}
}
if c.list != nil {
c.list = slices.DeleteFunc(c.list, func(n string) bool { return n == name })
}
}
func (c *xattrCache) getList() ([]string, bool) {
if c.list == nil {
return nil, false
}
// Return a copy to prevent the caller from modifying the cached list.
return append([]string(nil), c.list...), true
}
func (c *xattrCache) setList(names []string) {
if len(names) == 0 {
c.list = []string{}
} else {
// Save a copy to prevent the caller from modifying the cached list.
c.list = append([]string(nil), names...)
}
}