interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / store / torn_slot.h

133 lines · 6196 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0
2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
18 * torn_slot.h -- torn-write-safe 4096-byte record over 512- or 4096-byte
19 * logical blocks (host reference, C, no outside library).
20 *
21 * Problem. A Store record is 4096 bytes (OS-0015). On a device whose
22 * logical block is 512 bytes, one record spans 8 blocks, and a power cut can
23 * persist any subset of those 8 blocks. On a 4096-byte device the unit is one
24 * block, but a device can still tear inside a block if it breaks its own
25 * contract. The Spark's SSD reports 512-byte logical and physical blocks and a
26 * 512-byte atomic write unit; ARCH-0021 decision 2 keeps it that way.
27 *
28 * Protocol (dual slot, commit sector last):
29 *
30 * region, in 4096-byte units: 0 = body A 1 = commit A
31 * 2 = body B 3 = commit B
32 *
33 * commit(record):
34 * target = the slot that does NOT hold the newest valid record
35 * 1. write record to body[target] (all blocks)
36 * 2. FLUSH (barrier)
37 * 3. write commit header to commit[target] (seq = newest + 1)
38 * 4. FLUSH (durability point)
39 *
40 * The commit header's non-zero bytes all lie in bytes 0..87 of the commit
41 * unit, which is inside logical block 0 for both 512 and 4096 geometry.
42 * It holds the sequence number, a SHA-256 over (slot, seq, all 4096 record
43 * bytes), and a SHA-256 over the header itself.
44 *
45 * recover():
46 * a slot is valid only if its header is well formed, its header digest
47 * verifies, bytes 88..4095 of the commit unit are zero, and the record
48 * digest recomputed from all 4096 body bytes matches. The newest valid
49 * slot wins. Nothing partial is ever returned. recover() never writes.
50 *
51 * Why it holds. commit() only ever writes the slot that is NOT the newest
52 * valid one, so the complete old record is never touched. A torn body fails
53 * the record digest (the old header's digest names the old body); a torn
54 * header fails the header digest; the flush in step 2 keeps the new header
55 * from becoming durable before the whole new body. The device must honour
56 * FLUSH (NVMe Flush with a volatile write cache, or no volatile cache); a
57 * device that acknowledges FLUSH without persisting is outside this model.
58 *
59 * Relationship to the Rust Store engine: OS-0015 already uses two
60 * superblock slots with a CRC. This file is a C reference for the 4096-byte
61 * record over non-atomic 512-byte blocks; it does not change the v1 format
62 * and is not linked into the kernel.
63 */
64#ifndef AIENOS_TORN_SLOT_H
65#define AIENOS_TORN_SLOT_H
66
67#include <stdint.h>
68
69#define TS_RECORD_BYTES 4096u
70#define TS_UNITS 4u /* body A, commit A, body B, commit B */
71#define TS_HEADER_BYTES 88u
72
73/* Result codes. */
74enum {
75 TS_OK = 0, /* a committed record was recovered */
76 TS_EMPTY = 1, /* no record was ever committed: slot B never written, slot A blank or torn first write */
77 TS_CORRUPT = -1, /* damage or a slot state that crashes cannot produce (see ts_recover) */
78 TS_CONFLICT = -2, /* two valid slots claim the same seq (media damage) */
79 TS_EIO = -3, /* the device reported an error */
80 TS_EGEOMETRY = -4, /* unsupported block size or region too small */
81 TS_ESTATE = -5, /* commit without a successful recover, or seq exhausted */
82};
83
84/* Device callbacks. lba is absolute; count is in logical blocks. Return 0 on
85 * success, nonzero on device error. flush must not return until every
86 * previously acknowledged write is persistent. */
87typedef struct {
88 void *ctx;
89 uint32_t block_size; /* reported logical block size: 512 or 4096 */
90 uint64_t block_count; /* blocks in the device */
91 int (*read)(void *ctx, uint64_t lba, uint32_t count, uint8_t *buf);
92 int (*write)(void *ctx, uint64_t lba, uint32_t count, const uint8_t *buf);
93 int (*flush)(void *ctx);
94} ts_device;
95
96typedef struct {
97 ts_device dev;
98 uint64_t base_lba;
99 uint32_t blocks_per_unit;
100 int known; /* recover() has run and the state is usable */
101 int newest_slot; /* -1 when no committed record */
102 uint64_t newest_seq;
103} ts_region;
104
105/* Per-slot diagnosis from the last recover(). */
106typedef enum {
107 TS_SLOT_BLANK = 0, /* commit unit is all zero (never committed) */
108 TS_SLOT_VALID = 1,
109 TS_SLOT_INVALID = 2, /* non-blank but fails a check */
110} ts_slot_state;
111
112/* Bind a region of TS_UNITS units starting at base_lba. The block size must
113 * be 512 or 4096, and the region must fit the device. Does not touch media. */
114int ts_open(ts_region *r, const ts_device *dev, uint64_t base_lba);
115
116/* Recover the newest committed record. On TS_OK, out receives all 4096
117 * bytes and *seq_out the sequence number. On TS_EMPTY, out is zeroed.
118 * slot_states (optional, 2 entries) receives the per-slot diagnosis.
119 * Never writes to the device. */
120int ts_recover(ts_region *r, uint8_t out[TS_RECORD_BYTES], uint64_t *seq_out,
121 ts_slot_state slot_states[2]);
122
123/* Commit a new record. Requires a prior ts_recover() that returned TS_OK or
124 * TS_EMPTY (fail closed on TS_CORRUPT or TS_CONFLICT). Returns TS_OK only
125 * after the final flush. On any error the region must be recovered again. */
126int ts_commit(ts_region *r, const uint8_t rec[TS_RECORD_BYTES]);
127
128/* Encode a commit header into a zeroed 4096-byte commit unit (exposed for
129 * tests). */
130void ts_encode_commit(uint8_t unit[TS_RECORD_BYTES], int slot, uint64_t seq,
131 const uint8_t rec[TS_RECORD_BYTES]);
132
133#endif /* AIENOS_TORN_SLOT_H */