Files
seaweedfs/weed/server/filer_grpc_scope_rules.go
T
c96636ecd0 filer: fold chunk lists for scoped commits (#13281)
* filer: fold chunk lists for scoped commits

A filer serving a keep-only scoped token never folded chunk lists, and
refused any new chunk for an entry holding manifests, so scoped clients
kept their lists flat. On a store with a value size limit, such as
FoundationDB, a scoped file stopped fitting at about 1000 chunks.

The filer now folds scoped commits and reuses stored manifests as it does
for unscoped ones. Folding runs after the retained-data check, so the only
manifests a scoped commit stores are ones the filer wrote:

- a scoped request may keep a manifest stored at the target, sent as
  stored; any other manifest is refused, with or without a proof, and a
  stored manifest cannot be kept as a data chunk;
- the chunks held by the stored manifests count as stored at the target
  and join the identity map, so they may be retained flat but not under
  another spelling of their file id;
- those chunks are read under the handler's path lock, not at admission;
  admission still refuses a manifest the target does not store and leaves
  the checks that depend on the held chunks to the locked check. A
  manifest that cannot be read refuses the write as Unavailable. An update
  rechecking inside its store transaction reuses the chunks it already
  read, so it does no volume I/O there.

Appends to a folded entry are allowed under the same rules: an append
keeps every stored chunk, and the locked check refuses aliases of the
chunks the stored manifests hold.

* mount: explain why a scoped mount keeps chunk lists flat

A filer serving a scoped token now folds scoped commits itself and keeps
only the manifests it wrote. The mount's flat rule is unchanged; only the
reason given for it was out of date.

* filer: test a scoped truncate through a stored manifest

* filer: refuse manifest chunks in a scoped append

An append moves each chunk it carries to the end of the file. A manifest
cannot be moved that way: the chunks it holds keep their own offsets, so
the appended range would read no data and the manifest's bytes would still
read at their old place. Before this series a scoped request could carry no
manifest at all; now that a scoped commit may keep a stored manifest, an
append still takes data chunks only.

* filer: read only the stored manifests a scoped commit drops

The locked retained-data check read every stored manifest whenever a
scoped request had chunks, so each flush of a folded file and each append
cost one volume read per manifest under the path lock. The unscoped path
reads only the manifests a commit drops.

Read the same way for a keep-only scope: only the stored manifests the
request does not keep unchanged, and none for an append, which keeps every
stored chunk. A kept manifest keeps its chunks referenced, and garbage
comparisons match chunk identities, so another spelling of one of its
chunks cannot get the needle deleted; such a chunk now needs its own proof
like any new chunk. Truncating through a manifest and listing its chunks
flat still work, since both drop it.

* filer: find the dropped manifests of a scoped commit in one pass

Index the manifests the request keeps by file id instead of scanning the
request for each stored manifest, which compared and cloned chunks
quadratically in the number of manifests.

* filer: rewrap the scopeManifestReader comment

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

---------

Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-02 04:22:12 -08:00

186 lines
6.7 KiB
Go

package weed_server
// Rules applied to a scoped filer token on the filesystem gRPC service: which
// paths the token may use, and which chunk file ids and hard-link ids an entry
// it sends may reference. See filer_grpc_scope.go for how they are applied.
import (
"errors"
"fmt"
"math"
"path"
"reflect"
"strings"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
)
// scopeGrant is one path prefix a token may use, read-only or read-write.
type scopeGrant struct {
path string
readWrite bool
}
// scopeGrantsFromClaims turns the token's prefix lists into grants. Every
// prefix must be a clean absolute path: the filer stores the directory string
// it receives without cleaning, so an unclean grant could never match a stored
// path exactly and would only invite confusion about what it covers.
func scopeGrantsFromClaims(allowedPrefixes, readPrefixes []string) ([]scopeGrant, error) {
grants := make([]scopeGrant, 0, len(allowedPrefixes)+len(readPrefixes))
add := func(p string, readWrite bool) error {
if p == "" || p[0] != '/' || path.Clean(p) != p {
return fmt.Errorf("prefix %q is not a clean absolute path", p)
}
grants = append(grants, scopeGrant{path: p, readWrite: readWrite})
return nil
}
for _, p := range allowedPrefixes {
if err := add(p, true); err != nil {
return nil, err
}
}
for _, p := range readPrefixes {
if err := add(p, false); err != nil {
return nil, err
}
}
return grants, nil
}
var errScopePathNotCanonical = errors.New("path is not canonical")
// scopeFullPath joins a filer directory and entry name as the rules check
// them. The directory must be absolute and already clean, the name one clean
// segment or empty (the directory itself). Anything else is refused rather
// than normalised, because the filer would store it as received.
func scopeFullPath(dir, name string) (string, error) {
if dir == "" || dir[0] != '/' || path.Clean(dir) != dir {
return "", fmt.Errorf("%w: directory %q", errScopePathNotCanonical, dir)
}
switch {
case name == "":
return dir, nil
case name == "." || name == ".." || strings.Contains(name, "/"):
return "", fmt.Errorf("%w: name %q", errScopePathNotCanonical, name)
}
if dir == "/" {
return "/" + name, nil
}
return dir + "/" + name, nil
}
// scopePathAllowed reports whether the canonical path p equals a grant or lies
// under it; a mutation needs a read-write grant covering p.
func scopePathAllowed(grants []scopeGrant, p string, write bool) bool {
if p == "" || p[0] != '/' {
return false
}
for _, g := range grants {
under := g.path == "/" || p == g.path || strings.HasPrefix(p, g.path+"/")
if !under {
continue
}
if write && !g.readWrite {
continue
}
return true
}
return false
}
func scopeSplitPath(p string) (dir, name string) {
i := strings.LastIndex(p, "/")
if i <= 0 {
return "/", strings.TrimPrefix(p, "/")
}
return p[:i], p[i+1:]
}
// scopeProtectedPath reports whether a mutation can change the filer's shared
// storage configuration. Entry writes cannot replace or promote filer.conf to
// a directory; a rename or delete also cannot move or remove an ancestor.
func scopeProtectedPath(p string, descendants bool) bool {
config := scopeFilerConfDir + "/" + scopeFilerConfName
return p == config || strings.HasPrefix(p, config+"/") ||
(descendants && (p == "/" || strings.HasPrefix(config, p+"/")))
}
// scopeCheckEntryShape refuses indirect or missing chunk identities. Manifests
// contain additional file ids; scopeRetainsData admits only those the filer
// stored at the target entry.
func scopeCheckEntryShape(e *filer_pb.Entry) error {
if e == nil {
return errors.New("an entry is required")
}
if e.RemoteEntry != nil {
return errors.New("remote entries are not allowed")
}
if len(e.GetContent()) > 0 && len(e.GetChunks()) > 0 {
return errors.New("inline content and chunks cannot be combined")
}
if len(e.GetChunks()) > 0 && e.GetAttributes().GetFileSize() > math.MaxInt64 {
return errors.New("file size must fit a nonnegative signed offset")
}
if err := scopeCheckChunkRanges(e.GetChunks()); err != nil {
return err
}
for _, c := range e.GetChunks() {
if c == nil {
return errors.New("nil chunks are not allowed")
}
// Some readers use Fid directly. A suffixed FileId is not converted
// during serialization, so an unchecked Fid would survive alongside it.
if c.FileId != "" && c.Fid != nil {
structured := (&filer_pb.FileChunk{Fid: c.Fid}).GetFileIdString()
if c.FileId != structured {
return errors.New("chunk string and structured file ids must match")
}
}
if c.GetSourceFileId() != "" || c.GetSourceFid() != nil {
return fmt.Errorf("chunk %s: source file ids are not allowed", c.GetFileIdString())
}
if c.GetFileIdString() == "" {
return errors.New("chunk without a file id")
}
}
return nil
}
// scopeHardLinkMarker terminates every id filer.NewHardLinkId produces (16
// random bytes plus the marker), the only shape a client may introduce as a
// new id.
const scopeHardLinkMarker = '\x01'
func scopeLooksLikeNewHardLinkId(id []byte) bool {
return len(id) == 17 && id[16] == scopeHardLinkMarker
}
// scopeUnaryMethods binds each admitted RPC name to its request type. The path
// rules in unaryScoped are selected by that type; a new RPC reusing a request
// type cannot inherit permission accidentally. All absent methods are refused.
var scopeUnaryMethods = map[string]reflect.Type{
"AssignVolume": reflect.TypeOf((*filer_pb.AssignVolumeRequest)(nil)),
"LookupDirectoryEntry": reflect.TypeOf((*filer_pb.LookupDirectoryEntryRequest)(nil)),
"LookupDirectoryEntries": reflect.TypeOf((*filer_pb.LookupDirectoryEntriesRequest)(nil)),
"TouchAccessTime": reflect.TypeOf((*filer_pb.TouchAccessTimeRequest)(nil)),
"CreateEntry": reflect.TypeOf((*filer_pb.CreateEntryRequest)(nil)),
"UpdateEntry": reflect.TypeOf((*filer_pb.UpdateEntryRequest)(nil)),
"AppendToEntry": reflect.TypeOf((*filer_pb.AppendToEntryRequest)(nil)),
"DeleteEntry": reflect.TypeOf((*filer_pb.DeleteEntryRequest)(nil)),
"AtomicRenameEntry": reflect.TypeOf((*filer_pb.AtomicRenameEntryRequest)(nil)),
"PosixLock": reflect.TypeOf((*filer_pb.PosixLockRequest)(nil)),
"Ping": reflect.TypeOf((*filer_pb.PingRequest)(nil)),
"LookupVolume": reflect.TypeOf((*filer_pb.LookupVolumeRequest)(nil)),
"GetFilerConfiguration": reflect.TypeOf((*filer_pb.GetFilerConfigurationRequest)(nil)),
"Statistics": reflect.TypeOf((*filer_pb.StatisticsRequest)(nil)),
}
// Each admitted stream has a request/response rule in the stream interceptor.
var scopeStreamMethods = map[string]bool{
"ListEntries": true,
"StreamRenameEntry": true,
"StreamMutateEntry": true,
"SubscribeMetadata": true,
"SubscribeLocalMetadata": true,
}