diff --git a/seaweed-block/core/frontend/durable/one_process_per_volume_test.go b/seaweed-block/core/frontend/durable/one_process_per_volume_test.go index 55af4a8d40..98b3329fe1 100644 --- a/seaweed-block/core/frontend/durable/one_process_per_volume_test.go +++ b/seaweed-block/core/frontend/durable/one_process_per_volume_test.go @@ -98,6 +98,25 @@ func TestHostVolumeLockDirectoryCreatedOrRefused(t *testing.T) { } } +// A lock path that exists but is not a directory refuses, as before the +// lock directory was laid out by the provider. +func TestHostVolumeLockRefusesNonDirectoryLockPath(t *testing.T) { + locks := filepath.Join(t.TempDir(), "locks") + if err := os.WriteFile(locks, nil, 0o600); err != nil { + t.Fatal(err) + } + p, err := durable.NewDurableProvider(durable.ProviderConfig{ + Impl: durable.ImplWALStore, StorageRoot: t.TempDir(), BlockSize: 4096, NumBlocks: pcBlocks, + HostVolumeLock: true, VolumeLockDirTestOnly: locks, + }, v1View()) + if err == nil { + _ = p.Close() + } + if !errors.Is(err, durable.ErrVolumeLockDirUnavailable) { + t.Fatalf("a regular file as the lock directory was accepted: %v", err) + } +} + // A lock file this process cannot write (one another user created, as the // root ublk unit does) is still taken: the lock opens it read-only. func TestHostVolumeLockTakesReadOnlyLockFile(t *testing.T) { diff --git a/seaweed-block/core/frontend/durable/provider.go b/seaweed-block/core/frontend/durable/provider.go index eb391db3d8..e99ada32b2 100644 --- a/seaweed-block/core/frontend/durable/provider.go +++ b/seaweed-block/core/frontend/durable/provider.go @@ -405,7 +405,7 @@ func NewDurableProvider(cfg ProviderConfig, view frontend.ProjectionView) (*Dura if cfg.VolumeLockDirTestOnly == "" { cfg.VolumeLockDirTestOnly = HostVolumeLockDir } - if err := os.MkdirAll(cfg.VolumeLockDirTestOnly, 0o700); err != nil { + if err := makeVolumeLockDir(cfg.VolumeLockDirTestOnly); err != nil { return nil, fmt.Errorf("%w %s: %w; %s", ErrVolumeLockDirUnavailable, cfg.VolumeLockDirTestOnly, err, volumeLockCure) } } @@ -560,7 +560,40 @@ var ErrVolumeLockDirUnavailable = errors.New("durable: host volume lock director // volumeLockCure names the operator's cure. A directory or lock file a root // process created is root-owned: an unprivileged unit then cannot use it. -const volumeLockCure = "install the tmpfiles.d entry (deploy/systemd/sw-block.tmpfiles.conf), or chown the directory and its lock files to this process's user" +const volumeLockCure = "install the tmpfiles.d entry (deploy/systemd/sw-block.tmpfiles.conf), or make its parent directory traversable (0755) and chown the directory and its lock files to this process's user" + +// makeVolumeLockDir lays out a missing lock directory as the tmpfiles.d +// entry does: its parent 0755 (traversable by the unit's user even when root +// created it) and the directory itself 0700. Only a directory this call +// creates is chmod'ed (the umask cannot narrow it); one that exists, or that +// another process creates meanwhile, keeps its mode. An existing lock path +// that is not a directory refuses. +func makeVolumeLockDir(dir string) error { + parent := filepath.Dir(dir) + if err := os.MkdirAll(filepath.Dir(parent), 0o755); err != nil { + return err + } + if err := os.Mkdir(parent, 0o755); err == nil { + if err := os.Chmod(parent, 0o755); err != nil { + return err + } + } else if !errors.Is(err, os.ErrExist) { + return err + } + if err := os.Mkdir(dir, 0o700); err == nil { + return os.Chmod(dir, 0o700) + } else if !errors.Is(err, os.ErrExist) { + return err + } + fi, err := os.Lstat(dir) + if err != nil { + return err + } + if !fi.IsDir() { + return fmt.Errorf("%s exists and is not a directory", dir) + } + return nil +} // lockVolumeOnHost takes the volume's host lock (nil without // HostVolumeLock). A second live process for the volume on this host gets diff --git a/seaweed-block/core/frontend/durable/volume_lock_parent_mode_test.go b/seaweed-block/core/frontend/durable/volume_lock_parent_mode_test.go new file mode 100644 index 0000000000..ba85f454d9 --- /dev/null +++ b/seaweed-block/core/frontend/durable/volume_lock_parent_mode_test.go @@ -0,0 +1,88 @@ +//go:build unix + +package durable_test + +import ( + "os" + "path/filepath" + "syscall" + "testing" + + "github.com/seaweedfs/seaweed-block/core/frontend/durable" +) + +// V1-SPEC r2.45 (1), as the tmpfiles.d entry lays it out: a lock directory +// the provider creates gets a traversable parent (0755) and is itself 0700, +// whatever the umask. A 0700 parent created by root would keep the unit's +// user out even after the locks directory is chowned to it. +func TestHostVolumeLockCreatesParent0755AndLocks0700(t *testing.T) { + old := syscall.Umask(0o077) // a strict umask must not narrow the parent + defer syscall.Umask(old) + parent := filepath.Join(t.TempDir(), "sw-block") + locks := filepath.Join(parent, "locks") + p, err := durable.NewDurableProvider(durable.ProviderConfig{ + Impl: durable.ImplWALStore, StorageRoot: t.TempDir(), BlockSize: 4096, NumBlocks: pcBlocks, + HostVolumeLock: true, VolumeLockDirTestOnly: locks, + }, v1View()) + if err != nil { + t.Fatal(err) + } + _ = p.Close() + for path, want := range map[string]os.FileMode{parent: 0o755, locks: 0o700} { + fi, err := os.Stat(path) + if err != nil { + t.Fatal(err) + } + if got := fi.Mode().Perm(); got != want { + t.Fatalf("%s created %v, want %v", path, got, want) + } + } +} + +// newLockProvider opens a provider whose host lock directory is locks. +func newLockProvider(t *testing.T, locks string) { + t.Helper() + p, err := durable.NewDurableProvider(durable.ProviderConfig{ + Impl: durable.ImplWALStore, StorageRoot: t.TempDir(), BlockSize: 4096, NumBlocks: pcBlocks, + HostVolumeLock: true, VolumeLockDirTestOnly: locks, + }, v1View()) + if err != nil { + t.Fatal(err) + } + _ = p.Close() +} + +func wantPerm(t *testing.T, path string, want os.FileMode) { + t.Helper() + fi, err := os.Stat(path) + if err != nil { + t.Fatal(err) + } + if got := fi.Mode().Perm(); got != want { + t.Fatalf("%s is %v, want %v", path, got, want) + } +} + +// Directories the provider did not create keep their modes: an existing +// parent with a missing lock directory, and both existing. +func TestHostVolumeLockKeepsExistingDirectoryModes(t *testing.T) { + parent := filepath.Join(t.TempDir(), "sw-block") + if err := os.Mkdir(parent, 0o711); err != nil { + t.Fatal(err) + } + if err := os.Chmod(parent, 0o711); err != nil { + t.Fatal(err) + } + locks := filepath.Join(parent, "locks") + newLockProvider(t, locks) + wantPerm(t, parent, 0o711) + wantPerm(t, locks, 0o700) + + if err := os.Chmod(locks, 0o750); err != nil { + t.Fatal(err) + } + newLockProvider(t, locks) + wantPerm(t, parent, 0o711) + wantPerm(t, locks, 0o750) +} + diff --git a/seaweed-block/docs/design/invariants.md b/seaweed-block/docs/design/invariants.md index 0b6255aeac..7831af1871 100644 --- a/seaweed-block/docs/design/invariants.md +++ b/seaweed-block/docs/design/invariants.md @@ -467,7 +467,7 @@ Tests: `TestSnapshotSchedule_SlotsAreOffsetPerVolume`, `_FailureIsTypedAndBacksO ## Part B. Invariant ledger -314 rows: 243 ACTIVE, 68 STUB, 3 ARCHIVED; 15 kept from the internal ledger, 299 new. +315 rows: 244 ACTIVE, 68 STUB, 3 ARCHIVED; 15 kept from the internal ledger, 300 new. The "Machine / transition" column refers to Part A. Notes under a table record what a row does not cover and, for kept rows, what was narrowed from the internal ledger. @@ -717,7 +717,7 @@ Notes: | `INV-WS-CAS-UNKNOWN-001` | An unknown HEAD CAS outcome is settled by re-reading HEAD, never by a blind retry; until it is settled the proposal stays pending and its intents stay. | M5 CAS → Pending | `core/storage/chunk_layer.go` `captureLocked`, `resolvePending`; `core/storage/chunk_restore.go` `ClaimChunkHead` | `TestChunkRemote_UncertainCASIsNotPrecondition` (`core/frontend/chunkstore/chunk_remote_test.go`); `TestChunkLayer_C6_UnknownNotAppliedNeverClaimsSuccess` (`core/storage/chunk_layer_contract_test.go`); `TestClaimChunkHeadSettlesUnknownWithoutReplay` (`core/storage/chunk_restore_test.go`); `TestChunkIntent_UncertainCASKeepsIntentUntilSettled` (`core/storage/chunk_layer_intent_test.go`) | — | ACTIVE | | `INV-WS-CLAIM-BEFORE-SERVE-001` | A new holder claims HEAD at its master-issued epoch before I/O opens; a HEAD with a higher epoch, or another holder at the same epoch, fences the claim. | M4 Claiming → Holder \| Fenced | `core/storage/chunk_restore.go` `ClaimChunkHead`; `core/frontend/durable/provider_chunk.go` `newChunkLayer` | `TestClaimChunkHeadRefusesStaleAuthorityAndAdmissionDrift` (`core/storage/chunk_restore_test.go`); `TestRestoreSeedPrecedesTakeoverCAS` (`core/frontend/durable/provider_seed_order_test.go`); `TestWorkspaceMoveRestoresLazilyAndFencesOldHost` (`core/host/master/workspace_restore_test.go`) | `WorkspaceTakeover` `SeedMatchesTakeover` | ACTIVE | | `INV-WS-STALE-LOCAL-ROOT-001` | An existing local root whose HEAD another holder has published, or whose local durable LSN is below HEAD's `remote_lsn` (a rolled-back copy), is not served or cut; the attach fails closed with `ErrChunkStaleLocalRoot`. | M4 Claiming → Refused | `core/frontend/durable/provider_chunk.go` `newChunkLayer` | `TestWALStoreChunk_MoveBackOntoAnOldRootIsRefused`, `TestWALStoreChunk_MoveBackRefusedOnDeferredAttach` (`core/frontend/durable/provider_chunk_moveback_test.go`); `TestLocalReopenRejectsForeignClaimImage` (`core/frontend/durable/provider_reopen_belt_test.go`); `TestRolledBackCopyBelowHeadLSNIsRefused` (`core/frontend/durable/rolled_back_copy_test.go`); `TestCutPublishesOnlyLocallyDurableLSN` (`core/frontend/durable/cut_durable_before_cas_test.go`) | — | ACTIVE | -| `INV-WS-ONE-PROCESS-PER-HOST-001` | One host runs at most one live blockvolume per volume: each takes the volume's exclusive host lock before its store opens and holds it until the store closes; a second is refused before it opens anything, whatever its `--durable-root`. | M4 (start) → Attaching, Refused | `core/frontend/durable/provider.go` `DurableProvider.lockVolumeOnHost` | `TestOneLiveProcessPerVolumePerHost`, `TestHostVolumeLockDirectoryCreatedOrRefused`, `TestHostVolumeLockTakesReadOnlyLockFile` (`core/frontend/durable/one_process_per_volume_test.go`); `TestVolumeLockDirTestOnlyRefusedUnderSystemd` (`cmd/blockvolume/volume_lock_flag_test.go`) | — | ACTIVE | +| `INV-WS-ONE-PROCESS-PER-HOST-001` | One host runs at most one live blockvolume per volume: each takes the volume's exclusive host lock before its store opens and holds it until the store closes; a second is refused before it opens anything, whatever its `--durable-root`. | M4 (start) → Attaching, Refused | `core/frontend/durable/provider.go` `DurableProvider.lockVolumeOnHost` | `TestOneLiveProcessPerVolumePerHost`, `TestHostVolumeLockDirectoryCreatedOrRefused`, `TestHostVolumeLockRefusesNonDirectoryLockPath`, `TestHostVolumeLockTakesReadOnlyLockFile` (`core/frontend/durable/one_process_per_volume_test.go`); `TestHostVolumeLockCreatesParent0755AndLocks0700`, `TestHostVolumeLockKeepsExistingDirectoryModes` (`core/frontend/durable/volume_lock_parent_mode_test.go`); `TestVolumeLockDirTestOnlyRefusedUnderSystemd` (`cmd/blockvolume/volume_lock_flag_test.go`) | — | ACTIVE | | `INV-WS-SINGLE-FILER-001` | A chunk volume starts only when `--chunk-filer` is the only filer of its group: a master listing another filer or several refuses at once, whichever master answers first; a filer no master lists within 60 s is refused. A startup census, not a fence: a filer added to the group later is not detected. | M4 (startup) → Refused | `core/frontend/chunkstore/single_filer.go` `CheckSingleFiler`; `cmd/blockvolume/chunk_remote.go` `newChunkWiring`; `cmd/sw-block/chunk.go` `refuseUnlessSingleFiler` | `TestCheckSingleFilerRefusesAnotherOrSeveralFilers`, `TestCheckSingleFilerFailsClosed`, `TestCheckSingleFilerFavourableThenStalledRefuses`, `TestIsTargetComparesScopedAddresses`, `TestCheckSingleFilerPasses` (`core/frontend/chunkstore/single_filer_test.go`), `TestChunkWiringRefusesAMultiFilerGroup` (`cmd/blockvolume/single_filer_test.go`), `TestOpsChunk_WritingCommandsRunTheSingleFilerCheck` (`cmd/sw-block/chunk_start_check_test.go`), `TestOpsChunk_VerifyRunsTheSingleFilerCheck` (`cmd/sw-block/chunk_verify_test.go`) | — | ACTIVE | | `INV-WS-LEASE-REGRANT-001` | Re-grant after expiry: same holder and epoch only, no move pending or issued, not revoking, store certain, one durable write, and only once max(horizon, deadline) + skew bound has passed; the new process claims HEAD with a new generation before any I/O, so no older incarnation writes or CASes after. | M2 lease: Expired → Granted | `core/authority/workspace_lease_view.go` `regrantWorkspaceLease`; `core/authority/workspace.go` `forcedMoveBarrierLocked`; `core/storage/chunk_restore.go` `ClaimChunkHead` | `TestWorkspaceLeaseExpiredSameHolderRegrantIsDurable`, `TestWorkspaceLeaseRegrantWaitsForFullSkew`, `TestWorkspaceLeaseRegrantDefeatsAlreadyPassedForcedPreflight`, `TestWorkspaceLeaseForcedIssueDefeatsOldHolderRegrant`, `TestWorkspaceLeaseRegrantWaitsForDurabilityAndLatchesFailure` (`core/authority/workspace_regrant_test.go`); `TestWorkspaceLeaseRegrantPreservesLocalLogThroughFirstCut`, `TestWorkspaceLeaseRegrantCannotUnsealSameEpoch`, `TestWorkspaceLeaseRegrantEmptyRootClaimsOnlyHEAD` (`core/authority/workspace_regrant_storage_test.go`) | `WorkspaceRegrant` `NoDualServingInc`, `RegrantNoLoss`, `DiskCoversMemory` | ACTIVE | | `INV-WS-LEASE-001` | A host admits frontend I/O and cuts only under an unexpired lease for its exact `{volume, replica, epoch}`; a fenced guard never reopens, even on a late renewal reply. | M4 lease guard: Valid → Fenced | `core/host/volume/workspace_lease.go` `permits`, `Refresh` | `TestWorkspaceLeaseExpiryFencesWithoutTimerAndCannotReopen`, `TestWorkspaceDelayedRenewalCannotUndoExpiry` (`core/host/volume/workspace_lease_test.go`); `TestWorkspaceHostRequiresLeaseBeforeFrontendAndCut` (`core/host/volume/workspace_runtime_test.go`); `TestWorkspacePausedGranteeRefusedAtAttach` (`core/host/master/workspace_paused_grantee_test.go`) | `WorkspaceTakeover` `NoDualServing` | ACTIVE | diff --git a/seaweed-block/docs/releases/one-process-per-volume.md b/seaweed-block/docs/releases/one-process-per-volume.md index e2604748c5..93ed89b56b 100644 --- a/seaweed-block/docs/releases/one-process-per-volume.md +++ b/seaweed-block/docs/releases/one-process-per-volume.md @@ -7,7 +7,8 @@ the same host is refused before it opens anything, whatever its `--durable-root`. The lock is released when the process exits, including on a crash or kill. -blockvolume creates the directory (`0700`) when it is missing and it may, and +blockvolume creates the directory when it is missing and it may, laid out as +the tmpfiles.d entry does (`/run/sw-block` `0755`, `/run/sw-block/locks` `0700`), and refuses to start when it cannot create or open the directory or a volume's lock file. The shipped units cannot create it (`ProtectSystem=strict`), so install the tmpfiles.d entry shipped in [`deploy/systemd/sw-block.tmpfiles.conf`](../../deploy/systemd/sw-block.tmpfiles.conf): @@ -22,11 +23,12 @@ boot. Both shipped units allow writes to it (`ReadWritePaths=-/run/sw-block/locks`). Without it, or when it is not usable, blockvolume exits at start with `host volume lock directory unavailable ... install the tmpfiles.d entry (deploy/systemd/sw-block.tmpfiles.conf), -or chown the directory and its lock files to this process's user`. A -directory created by a root process (the root ublk unit, or blockvolume run -by hand as root) is root-owned, and an unprivileged unit on that host then -cannot use it: install the tmpfiles.d entry, or chown the directory to the -unit's user. Lock files are opened read-only, so a lock file another user +or make its parent directory traversable (0755) and chown the directory and +its lock files to this process's user`. A directory created by a root +process (the root ublk unit, or blockvolume run by hand as root) is +root-owned, and an unprivileged unit on that host then cannot use it: +install the tmpfiles.d entry, or chown the locks directory to the unit's +user and make sure `/run/sw-block` is `0755`. Lock files are opened read-only, so a lock file another user created stays lockable. ## A rolled-back copy of a root is refused