// K2386-K2390: configuration.persistence — Config atomik kalıcılık // Invariant: Ayar ya eski ya yeni geçerli kayıt olur // Positive: Geçici diskte write rename // Counterexample: Commit sırasında süreç ölümü // Acceptance: Yeni boot geçerli bir sürüm seçer pub const CONFIGURATION_PERSISTENCE_SCHEMA: &str = "turkos.os.configuration.persistence.v1"; pub const CONFIGURATION_PERSISTENCE_INVARIANT: &str = "Ayar ya eski ya yeni geçerli kayıt olur"; pub const CONFIGURATION_PERSISTENCE_POSITIVE: &str = "Geçici diskte write rename"; pub const CONFIGURATION_PERSISTENCE_COUNTEREXAMPLE: &str = "Commit sırasında süreç ölümü"; pub const CONFIGURATION_PERSISTENCE_ACCEPTANCE: &str = "Yeni boot geçerli bir sürüm seçer"; pub const EXPECTED_SCRATCH_WRITE_RENAME_OPS: u32 = 2; pub const EXPECTED_VALID_BOOT_GENERATIONS: u32 = 2; pub const EXPECTED_ATOMIC_COMMIT_WITNESSES: u32 = 2; pub const EXPECTED_TORN_CONFIG_RECORDS: u32 = 0; #[repr(u8)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum ConfigurationPersistenceError { ProcessKilledDuringCommit = 1, NeitherOldNorNewValidRecord = 2, NextBootSelectedInvalidVersion = 3, ScratchWriteRenameOpsMismatch = 4, ValidBootGenerationsMismatch = 5, AtomicCommitWitnessesMismatch = 6, ZeroPersistenceEpoch = 7, NonMonotonicPersistenceEpoch = 8, ZeroPersistenceRecordDigest = 9, UnboundConfigurationPersistenceDigest = 10, AlreadyCommitted = 11, } #[repr(C)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct ConfigurationPersistenceRequest { pub process_killed_during_commit: bool, pub neither_old_nor_new_valid_record: bool, pub next_boot_selects_valid_version: bool, pub scratch_write_rename_ops: u32, pub valid_boot_generations: u32, pub atomic_commit_witnesses: u32, pub persistence_epoch: u64, pub previous_persistence_epoch: u64, pub persistence_record_digest: u64, pub persistence_binding_digest: u64, } #[repr(C)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct ConfigurationPersistenceReceipt { pub scratch_write_rename_ops: u32, pub valid_boot_generations: u32, pub atomic_commit_witnesses: u32, pub torn_config_records: u32, pub next_boot_selects_valid_version: bool, pub persistence_epoch: u64, pub persistence_record_digest: u64, pub receipt_digest: u64, } pub const fn compute_configuration_persistence_binding_digest( scratch_write_rename_ops: u32, valid_boot_generations: u32, atomic_commit_witnesses: u32, torn_config_records: u32, persistence_epoch: u64, persistence_record_digest: u64, ) -> u64 { let mut h: u64 = 0xcbf2_9ce4_8422_2325; h = (h ^ 0x2390_4346_5052_5354).wrapping_mul(0x0000_0100_0000_01b3); h = (h ^ (scratch_write_rename_ops as u64)).wrapping_mul(0x0000_0100_0000_01b3); h = (h ^ (valid_boot_generations as u64)).wrapping_mul(0x0000_0100_0000_01b3); h = (h ^ (atomic_commit_witnesses as u64)).wrapping_mul(0x0000_0100_0000_01b3); h = (h ^ (torn_config_records as u64)).wrapping_mul(0x0000_0100_0000_01b3); h = (h ^ persistence_epoch).wrapping_mul(0x0000_0100_0000_01b3); h = (h ^ persistence_record_digest).wrapping_mul(0x0000_0100_0000_01b3); h } pub struct ConfigurationPersistenceCoordinator { committed: bool, committed_total: u64, rejected_total: u64, atomic_persistence_refusals: u64, persistence_accounting_refusals: u64, committed_receipt: Option, } impl ConfigurationPersistenceCoordinator { pub const fn new() -> Self { Self { committed: false, committed_total: 0, rejected_total: 0, atomic_persistence_refusals: 0, persistence_accounting_refusals: 0, committed_receipt: None, } } pub fn is_committed(&self) -> bool { self.committed } pub fn committed_total(&self) -> u64 { self.committed_total } pub fn rejected_total(&self) -> u64 { self.rejected_total } pub fn atomic_persistence_refusals(&self) -> u64 { self.atomic_persistence_refusals } pub fn persistence_accounting_refusals(&self) -> u64 { self.persistence_accounting_refusals } pub fn committed_receipt(&self) -> Option { self.committed_receipt } pub fn commit_atomic_config_persistence( &mut self, req: &ConfigurationPersistenceRequest, ) -> Result { if self.committed { self.rejected_total += 1; return Err(ConfigurationPersistenceError::AlreadyCommitted); } if req.process_killed_during_commit { self.rejected_total += 1; self.atomic_persistence_refusals += 1; return Err(ConfigurationPersistenceError::ProcessKilledDuringCommit); } if req.neither_old_nor_new_valid_record { self.rejected_total += 1; self.atomic_persistence_refusals += 1; return Err(ConfigurationPersistenceError::NeitherOldNorNewValidRecord); } if !req.next_boot_selects_valid_version { self.rejected_total += 1; self.atomic_persistence_refusals += 1; return Err(ConfigurationPersistenceError::NextBootSelectedInvalidVersion); } if req.scratch_write_rename_ops != EXPECTED_SCRATCH_WRITE_RENAME_OPS { self.rejected_total += 1; self.persistence_accounting_refusals += 1; return Err(ConfigurationPersistenceError::ScratchWriteRenameOpsMismatch); } if req.valid_boot_generations != EXPECTED_VALID_BOOT_GENERATIONS { self.rejected_total += 1; self.persistence_accounting_refusals += 1; return Err(ConfigurationPersistenceError::ValidBootGenerationsMismatch); } if req.atomic_commit_witnesses != EXPECTED_ATOMIC_COMMIT_WITNESSES { self.rejected_total += 1; self.persistence_accounting_refusals += 1; return Err(ConfigurationPersistenceError::AtomicCommitWitnessesMismatch); } if req.persistence_epoch == 0 { self.rejected_total += 1; return Err(ConfigurationPersistenceError::ZeroPersistenceEpoch); } if req.persistence_epoch <= req.previous_persistence_epoch { self.rejected_total += 1; return Err(ConfigurationPersistenceError::NonMonotonicPersistenceEpoch); } if req.persistence_record_digest == 0 { self.rejected_total += 1; return Err(ConfigurationPersistenceError::ZeroPersistenceRecordDigest); } let expected_binding = compute_configuration_persistence_binding_digest( req.scratch_write_rename_ops, req.valid_boot_generations, req.atomic_commit_witnesses, EXPECTED_TORN_CONFIG_RECORDS, req.persistence_epoch, req.persistence_record_digest, ); if req.persistence_binding_digest != expected_binding { self.rejected_total += 1; self.persistence_accounting_refusals += 1; return Err(ConfigurationPersistenceError::UnboundConfigurationPersistenceDigest); } self.committed = true; self.committed_total += 1; let receipt = ConfigurationPersistenceReceipt { scratch_write_rename_ops: req.scratch_write_rename_ops, valid_boot_generations: req.valid_boot_generations, atomic_commit_witnesses: req.atomic_commit_witnesses, torn_config_records: EXPECTED_TORN_CONFIG_RECORDS, next_boot_selects_valid_version: true, persistence_epoch: req.persistence_epoch, persistence_record_digest: req.persistence_record_digest, receipt_digest: expected_binding ^ 0x2390_ACE0_5052_5354, }; self.committed_receipt = Some(receipt); Ok(receipt) } }