From e4135b9668bbbfc58780d319280822b50c1bab18 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Tue, 17 Nov 2026 18:13:18 -0800 Subject: [PATCH] fix(sw-block): refuse bad migration geometry before any write (row 13b P1, P2s) codex01 review of 7c6a29d8: - P1: ValidateMigrationV3 lacked the checked extent end, so an aligned geometry whose extent end overflows passed, the backup and the map were written, and only the slot validation refused. The geometry check now applies the same bounds (volume, extent, block, the checked WAL and extent ends, the file end within MaxInt64), and the tool runs it, and a file-size bound on the v2 WAL, before the WAL is read or anything is written. - P2: the scan allocated the v2 WAL size from the superblock before any bound; it is now bounded by the file size first, with no wrapping sum. - P2: a rerun that found the v2 chunk bitmap already renamed skipped the directory fsync an earlier run may have failed; it now fsyncs it. Controls: an overflowing extent end, a WAL past the file end and a WAL size whose sums wrap are each refused with no byte written; a retirement whose directory fsync failed is made durable by the rerun. Mutants: no checked file end, no file-size bound, the rerun skipping the fsync. The WAL-region overflow check alone is equivalent: the file-size bound also refuses that case. Co-Authored-By: Claude Opus 5.5 --- seaweed-block/cmd/sw-block/migrate.go | 41 +++++++++--- seaweed-block/cmd/sw-block/migrate_test.go | 74 ++++++++++++++++++++++ seaweed-block/core/storage/migrate_v3.go | 11 ++++ 3 files changed, 117 insertions(+), 9 deletions(-) diff --git a/seaweed-block/cmd/sw-block/migrate.go b/seaweed-block/cmd/sw-block/migrate.go index f9b908cce1..99cb9cbd44 100644 --- a/seaweed-block/cmd/sw-block/migrate.go +++ b/seaweed-block/cmd/sw-block/migrate.go @@ -30,6 +30,10 @@ var errMigrateNeitherSlot = errors.New("migrate-v3: no valid v2 superblock or v3 // its name; returning an error stops the tool there (a simulated crash). var migrateStepHook func(step string) error +// syncDirHook, when set by a test, runs at every directory fsync; an error +// fails that fsync. +var syncDirHook func(path string) error + func runMigrateV3(args []string, stdout, stderr io.Writer) int { fs := flag.NewFlagSet("migrate-v3", flag.ContinueOnError) fs.SetOutput(stderr) @@ -96,6 +100,23 @@ func migrateV3(path string, chunkBlocks uint64, out io.Writer) error { } func migrateFromV2(f *os.File, path string, sb v2legacy.Superblock, chunkBlocks uint64, out io.Writer) error { + src := storage.MigrationSourceV3{ + UUID: sb.UUID, VolumeSize: sb.VolumeSize, ExtentSize: sb.ExtentSize, BlockSize: sb.BlockSize, + OldWALOffset: sb.WALOffset, OldWALSize: sb.WALSize, CreatedAt: sb.CreatedAt, + CheckpointLSN: sb.WALCheckpointLSN, ChunkBlocks: chunkBlocks, + } + // Precondition 4 first (read-only): the geometry, including the checked + // extent end, refuses before the WAL is read or anything is written. + if err := storage.ValidateMigrationV3(src); err != nil { + return err + } + fi, err := f.Stat() + if err != nil { + return err + } + if size := uint64(fi.Size()); sb.WALOffset > size || sb.WALSize > size-sb.WALOffset { // no wrapping sum + return fmt.Errorf("%w: the v2 WAL [%d, %d) extends past the file end %d", storage.ErrMigrateGeometry, sb.WALOffset, sb.WALOffset+sb.WALSize, fi.Size()) + } // Step 1: precondition 3, read-only and before any write. if sb.WALHead != sb.WALTail { return fmt.Errorf("%w (WALHead %d != WALTail %d)", errMigrateNotClean, sb.WALHead, sb.WALTail) @@ -107,14 +128,6 @@ func migrateFromV2(f *os.File, path string, sb v2legacy.Superblock, chunkBlocks if found := v2legacy.ScanClean(wal, sb.WALCheckpointLSN); len(found) > 0 { return fmt.Errorf("%w (%d unflushed record(s); first at WAL offset %d: %s)", errMigrateNotClean, len(found), found[0].Offset, found[0].Reason) } - src := storage.MigrationSourceV3{ - UUID: sb.UUID, VolumeSize: sb.VolumeSize, ExtentSize: sb.ExtentSize, BlockSize: sb.BlockSize, - OldWALOffset: sb.WALOffset, OldWALSize: sb.WALSize, CreatedAt: sb.CreatedAt, - CheckpointLSN: sb.WALCheckpointLSN, ChunkBlocks: chunkBlocks, - } - if err := storage.ValidateMigrationV3(src); err != nil { // precondition 4 - return err - } if err := step("scanned"); err != nil { return err } @@ -220,7 +233,12 @@ func writeFileAtomic(name string, b []byte) error { func retireC11(path string) error { err := os.Rename(path+".chunkbitmap", path+".v2-chunkbitmap.bak") if errors.Is(err, os.ErrNotExist) { - return nil + if _, serr := os.Stat(path + ".v2-chunkbitmap.bak"); serr != nil { + return nil // there was no v2 bitmap + } + // An earlier run renamed it; its directory fsync may have failed, so + // make the rename durable now (codex01 row 13b P2). + return syncDirOf(path) } if err != nil { return fmt.Errorf("migrate-v3: retire the v2 chunk bitmap: %w", err) @@ -231,6 +249,11 @@ func retireC11(path string) error { // syncDirOf fsyncs the directory holding path. A directory cannot be // fsynced on Windows; there it is a no-op, as in the provider. func syncDirOf(path string) error { + if h := syncDirHook; h != nil { + if err := h(path); err != nil { + return err + } + } if runtime.GOOS == "windows" { return nil } diff --git a/seaweed-block/cmd/sw-block/migrate_test.go b/seaweed-block/cmd/sw-block/migrate_test.go index f9f62b200a..e7b2ef3bfd 100644 --- a/seaweed-block/cmd/sw-block/migrate_test.go +++ b/seaweed-block/cmd/sw-block/migrate_test.go @@ -389,3 +389,77 @@ func damageSlotA(t *testing.T, path string) { t.Fatal(err) } } + +func patchSuperblock(t *testing.T, path string, off int64, v uint64) { + t.Helper() + f, err := os.OpenFile(path, os.O_RDWR, 0) + if err != nil { + t.Fatal(err) + } + defer f.Close() + var b [8]byte + for i := range b { + b[i] = byte(v >> (8 * i)) + } + if _, err := f.WriteAt(b[:], off); err != nil { + t.Fatal(err) + } +} + +// codex01 row 13b P1 and P2: geometry the v3 slot validation would refuse +// is refused before ANY write (no backup, no map, the volume unchanged): +// an aligned VolumeSize whose extent end overflows, and a v2 WAL size that +// extends past the file end (refused before the scan allocates it). +// Mutants: no checked extent end in ValidateMigrationV3, no file-size +// bound before the WAL read. +func TestMigrateV3BadGeometryRefusedBeforeAnyWrite(t *testing.T) { + for name, patch := range map[string][2]uint64{ + "extent end overflows": {24, 1 << 63}, // VolumeSize, block aligned + "WAL past the file end": {48, 1<<45 + 4096}, // WALSize, v3 size aligned + "WAL region wraps": {48, ^uint64(0) - 4095}, // WALSize 2^64-4096: every sum wraps + } { + t.Run(name, func(t *testing.T) { + path := v2Volume(t, true) + patchSuperblock(t, path, int64(patch[0]), patch[1]) + snap := snapshotFiles(t, path) + code, msg := migrate(t, path) + if code == 0 || !strings.Contains(msg, "cannot be migrated in place") { + t.Fatalf("exit %d: %s", code, msg) + } + if !sameFiles(snap, snapshotFiles(t, path)) { + t.Fatal("a geometry refusal wrote a backup, a map or volume bytes") + } + }) + } +} + +// codex01 row 13b P2: an earlier run renamed the v2 chunk bitmap but its +// directory fsync failed; the rerun (source gone) still fsyncs the +// directory so the rename is durable. Mutant: treating the missing source +// as done without the fsync fails here. +func TestMigrateV3RetirementRerunFsyncsTheDirectory(t *testing.T) { + path := v2Volume(t, true) + if err := os.WriteFile(path+".chunkbitmap", []byte("c11"), 0o644); err != nil { + t.Fatal(err) + } + failed := false + syncDirHook = func(string) error { + if _, err := os.Stat(path + ".v2-chunkbitmap.bak"); err == nil && !failed { + failed = true + return errors.New("injected directory fsync failure") + } + return nil + } + defer func() { syncDirHook = nil }() + if code, _ := migrate(t, path); code == 0 { + t.Fatal("the injected retirement fsync failure did not fail the tool") + } + calls := 0 + syncDirHook = func(string) error { calls++; return nil } + if code, msg := migrate(t, path); code != 0 { + t.Fatalf("rerun: exit %d: %s", code, msg) + } + if calls == 0 { + t.Fatal("the rerun did not fsync the directory of the earlier rename") + } +} diff --git a/seaweed-block/core/storage/migrate_v3.go b/seaweed-block/core/storage/migrate_v3.go index 32d14b0f44..16ebb3964d 100644 --- a/seaweed-block/core/storage/migrate_v3.go +++ b/seaweed-block/core/storage/migrate_v3.go @@ -5,6 +5,7 @@ import ( "crypto/sha256" "errors" "fmt" + "math" "os" ) @@ -72,6 +73,16 @@ func migrationDescriptorV3(src MigrationSourceV3) (descriptorV3, error) { if src.CheckpointLSN > counterMaxV3 { return descriptorV3{}, fmt.Errorf("%w: checkpoint %d", ErrMigrateGeometry, src.CheckpointLSN) } + // The checked extent end (codex01 row 13b P1): the same bounds the slot + // validation applies, here, before any backup or sidecar write. + if src.VolumeSize == 0 || src.ExtentSize == 0 || src.VolumeSize%uint64(src.BlockSize) != 0 { + return descriptorV3{}, fmt.Errorf("%w: volume %d, extent %d, block %d", ErrMigrateGeometry, src.VolumeSize, src.ExtentSize, src.BlockSize) + } + if end, ok := checkedAddV3(walOffsetV3, walSize); !ok { + return descriptorV3{}, fmt.Errorf("%w: the WAL region overflows", ErrMigrateGeometry) + } else if fileEnd, ok := checkedAddV3(end, src.VolumeSize); !ok || fileEnd > math.MaxInt64 { + return descriptorV3{}, fmt.Errorf("%w: the extent region overflows", ErrMigrateGeometry) + } d := descriptorV3{ ImplKind: ImplKindWALStore, ImplVersion: walStoreImplVersion3, UUID: src.UUID, VolumeSize: src.VolumeSize, ExtentSize: src.ExtentSize, BlockSize: src.BlockSize,