fix: reject mixed manifests and share segmented roots with master and usage

This commit is contained in:
pingqiu committed 2026-11-15 18:56:14 -08:00
1 parent 701a304257
commit ae907d6ea6
4 files changed
+84 -6

No files matched your search

@@ -281,8 +281,9 @@ type ForkResult struct {
// Catalog reads and writes the control-plane entries under one chunk base.
type Catalog struct {
f Filer
base string
blobs Blobs // optional immutable-object reader; SWFS supplies it directly
f Filer
base string
// OpDeadline bounds a snapshot or fork (DefaultOpDeadline when zero).
OpDeadline time.Duration
now func() time.Time
@@ -300,7 +301,8 @@ func NewCatalog(f Filer, base string) (*Catalog, error) {
if !path.IsAbs(base) || path.Clean(base) != base || base == "/" {
return nil, fmt.Errorf("chunkstore: chunk base %q must be a clean absolute directory other than /", base)
}
return &Catalog{f: f, base: base, now: time.Now}, nil
blobs, _ := f.(Blobs)
return &Catalog{f: f, blobs: blobs, base: base, now: time.Now}, nil
}
// SetClock replaces the catalog's clock (tests).
@@ -503,6 +503,22 @@ func decodeHeadDoc(p string, content []byte) (storage.ChunkHead, error) {
if err := decodeManifestJSON(content, &doc); err != nil {
return fail("decode: %v", err)
}
var fields map[string]json.RawMessage
if err := json.Unmarshal(content, &fields); err != nil {
return fail("decode representation: %v", err)
}
if doc.Format == 1 {
for _, name := range []string{"segments", "chunk_bytes", "chunk_count"} {
if _, present := fields[name]; present {
return fail("flat HEAD contains segment field %s", name)
}
}
}
if doc.Format == 2 {
if _, present := fields["chunks"]; present {
return fail("segmented HEAD contains flat chunks field")
}
}
if doc.Format != headFormat && doc.Format != 2 {
return fail("format %d not supported (want %d)", doc.Format, headFormat)
}
@@ -565,3 +581,9 @@ func NewSWFSChunkRemote(c *SWFS, root string) (*ChunkRemote, error) {
}
var _ storage.ChunkRemote = (*ChunkRemote)(nil)
// DecodeHeadDocument validates a persisted flat or segmented root without
// fetching data. Control-plane readers share the same representation checks.
func DecodeHeadDocument(name string, body []byte) (storage.ChunkHead, error) {
return decodeHeadDoc(name, body)
}
@@ -12,7 +12,8 @@ import (
)
// Space accounting (claude05's rulings, 2026-11-15 22:53Z). Everything is
// computed from manifests and filer listings; no chunk data is read.
// computed from manifests and filer listings; segment objects are verified,
// but data chunks are not read.
//
// A chunk's bytes are ChunkBlocks x BlockSize, the last chunk clipped to
// NumBlocks (ChunkRef carries no size).
@@ -99,6 +100,16 @@ func (c *Catalog) Usage(ctx context.Context, vol string) (VolumeUsage, error) {
return VolumeUsage{}, err
}
head, err = c.usageHead(ctx, vol, head)
if err != nil {
return VolumeUsage{}, err
}
for i := range snaps {
snaps[i].head, err = c.usageHead(ctx, vol, snaps[i].head)
if err != nil {
return VolumeUsage{}, err
}
}
// holders counts, per chunk identity (owner volume + key), how many
// HEADs and live snapshots of any volume reference it; size is each
// identity's byte size as this volume's geometry indexes it.
@@ -138,6 +149,10 @@ func (c *Catalog) Usage(ctx context.Context, vol string) (VolumeUsage, error) {
return VolumeUsage{}, err
}
if has {
oh, err = c.usageHead(ctx, other, oh)
if err != nil {
return VolumeUsage{}, err
}
addHolder(other, oh, false)
}
osnaps, err := c.allSnapshots(ctx, other)
@@ -145,7 +160,11 @@ func (c *Catalog) Usage(ctx context.Context, vol string) (VolumeUsage, error) {
return VolumeUsage{}, err
}
for _, s := range osnaps {
addHolder(other, s.Head(), false)
otherHead, err := c.usageHead(ctx, other, s.Head())
if err != nil {
return VolumeUsage{}, err
}
addHolder(other, otherHead, false)
}
}
for _, s := range snaps {
@@ -218,3 +237,34 @@ func chunkIdentity(vol, key string) string {
}
return vol + "\x00" + key
}
func (c *Catalog) usageHead(ctx context.Context, vol string, h storage.ChunkHead) (storage.ChunkHead, error) {
if h.Segments == nil {
return h, nil
}
var blobs Blobs = c.blobs
if blobs == nil {
return h, errors.New("chunkstore: usage segment reader unavailable")
}
r, err := NewChunkRemote(c.f, blobs, path.Join(c.base, vol))
if err != nil {
return h, err
}
resolved, err := r.resolveManifest(ctx, h)
if err != nil {
return h, err
}
v, ok, err := c.ReadVolume(ctx, vol)
if err != nil {
return h, err
}
if !ok || h.ChunkBytes != uint64(v.Geometry.ChunkBlocks)*uint64(v.Geometry.BlockSize) {
return h, errors.New("chunkstore: usage manifest/volume geometry mismatch")
}
for idx := range resolved.Chunks {
if idx >= (v.Geometry.NumBlocks+uint64(v.Geometry.ChunkBlocks)-1)/uint64(v.Geometry.ChunkBlocks) {
return h, errors.New("chunkstore: usage chunk outside volume")
}
}
return resolved, nil
}
@@ -11,6 +11,7 @@ import (
"time"
"github.com/seaweedfs/seaweed-block/core/authority"
"github.com/seaweedfs/seaweed-block/core/frontend/chunkstore"
)
// NewWorkspaceHEADReader reads inline HEAD content from the configured filer
@@ -63,7 +64,10 @@ func NewWorkspaceHEADReader(base string) (func(context.Context, string) (authori
if err := json.Unmarshal(raw, &doc); err != nil {
return authority.WorkspaceHead{}, err
}
if doc.Format != 1 || doc.Gen == 0 || doc.Epoch == 0 || doc.Holder == "" || doc.LegacyDetached != nil || (doc.State != "" && doc.State != "active" && doc.State != "detached") {
if _, err := chunkstore.DecodeHeadDocument("workspace/"+volume, raw); err != nil {
return authority.WorkspaceHead{}, err
}
if (doc.Format != 1 && doc.Format != 2) || doc.Gen == 0 || doc.Epoch == 0 || doc.Holder == "" || doc.LegacyDetached != nil || (doc.State != "" && doc.State != "active" && doc.State != "detached") {
return authority.WorkspaceHead{}, fmt.Errorf("invalid HEAD identity/state")
}
// Missing state is the legacy active representation. Only a final-cut