# K2146–K2150 Shutdown Storage Flush Contract (`shutdown_io.flush`) ## Purpose and Invariant - **Feature**: `shutdown_io.flush` (`K2146–K2150`) - **Invariant**: Completed storage flush and mere process/host quit are distinct events (`Tamamlanan flush ile süreç quit ayrıdır`). - **Scope**: Models ordered shutdown storage flushing (`Data -> Metadata -> Journal`) after write admission has quiesced, and refuses the clean-shutdown label whenever any flush stage fails with `EIO` (`5`) or `ETIMEDOUT` (`110`), is skipped out of order, or is claimed via a bare process quit. ## State Machine (`Phase`) | Phase | Code | Meaning | | --- | --- | --- | | `Idle` | `0` | Quiesced write gate confirmed; awaiting `Stage::Data` flush | | `DataFlushed` | `1` | Dirty data extents flushed (`flushed_stages |= 1`) | | `MetadataFlushed` | `2` | Inode/directory metadata flushed (`flushed_stages |= 2`) | | `JournalFlushed` | `3` | Journal commit barrier flushed (`flushed_stages |= 4`, `ALL_STAGES_MASK = 0b111`) | | `CleanReady` | `4` | All three stages flushed without fault and `clean_granted == true` | | `DegradedAbort` | `5` | Flush stage encountered `Fault::IoError` (`EIO = 5`) or `Fault::Timeout` (`ETIMEDOUT = 110`); clean shutdown label permanently withheld | ## Error Codes (`Error`) - `NotQuiesced = 1`: `FlushCoordinator::new(false)` refused before write quiesce completes. - `Order = 2`: Out-of-order or duplicate stage flush refused. - `InvalidFlush = 3`: Zero block count, zero journal sequence, zero digest, or invalid fault injection refused. - `IoError = 4`: Storage flush failed with `EIO` (`5`); transitions to `Phase::DegradedAbort`. - `Timeout = 5`: Storage flush timed out with `ETIMEDOUT` (`110`); transitions to `Phase::DegradedAbort`. - `ProcessQuitNotFlush = 6`: `claim_clean_shutdown(true)` refused because process/host termination is not a storage flush. - `CleanClaimRefused = 7`: `claim_clean_shutdown(false)` refused when any stage is missing or `fault_count != 0`. ## Storage adapter and kernel-owned session `os-shutdown-io/src/flush_io.rs` now issues backend writes and flush barriers in Data, Metadata, Journal order. It validates all LBAs, capacities and aliases before the first write, requires a fully drained `QuiesceGate`, and issues a private clean receipt only after all three barriers complete. EIO or timeout stops subsequent I/O and prevents reuse of the same coordinator. The backend retains responsibility for DMA ownership and reset after a timeout. `flush_session.rs` owns the receipt and accepts the kernel-assigned PID1 identity and generation. An unauthorized caller cannot submit I/O or claim a receipt. A claim before the transaction, a bare process quit, and a repeated flush attempt are refused. Device errors preserve their errno and permanently withhold the receipt for that session. A user cannot supply counts, digests, or a success flag to this interface. The native experiment runs a separate EL0 worker and PID1. The worker's unauthorized requests produce no disk commands. PID1 invokes actual writes and negotiated virtio FLUSH operations, then checks the receipt through SVC. Both programs continue executing after refusal. Each request exposes raw DMA descriptors, available/used rings and the request page before queue reuse. The driver retains its DMA lease after a timeout and releases it only after device status and QueueReady confirm reset. Positive cases also read the three sectors back; the stopped host checks the complete disposable disk. ## Required physical matrix One compiled Image must pass direct/UEFI boot, 256/512/1024 MiB RAM and 1/2/4 CPUs. Every one of those eighteen profiles runs three fresh-disk cases: completed Data/Metadata/Journal barriers, real metadata-write EIO, and a real journal FLUSH completion delayed beyond the guest deadline. The latter two must return EIO/ETIMEDOUT to EL0 and deny the clean receipt. Native SVC records must follow actual `svc #0` instructions in the embedded program binaries. Compilation, host fixtures and the older model-only guest do not satisfy this matrix. The complete matrix and source-bound local checks are required before K2150 guest acceptance. The storage receipt establishes this flush contract; whole-system device drain, second-boot persistence and platform poweroff have their own roadmap gates.