Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/native/store/store_engine.c # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / native / store / store_engine.c` 630 lines ยท 23820 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 * store_engine.c -- Store v1 mount and transaction engine, C twin of 19 * crates/aienos-kernel/src/store/engine.rs. See store_engine.h. 20 * 21 * One deliberate difference, recorded in README.md: while planning a 22 * transaction, Rust binary-searches a vector that already has the new 23 * entries appended (unsorted tail), so an already-stored object requested 24 * after a new one can be missed and the transaction then fails with 25 * Format(DuplicateObject). This engine searches only the sorted committed 26 * catalog, so such a transaction deduplicates as ADR 0015 intends. The 27 * written bytes are identical whenever Rust succeeds. 28 */ 29 #include "store_engine.h" 30 31 enum { RE_OK = 0, RE_IO = 1, RE_GRAPH = 2 }; 32 33 const char *st_strerror(int e) 34 { 35 switch (e) { 36 case ST_M_IO: return "MountError::Io"; 37 case ST_M_UNFORMATTED: return "MountError::Unformatted"; 38 case ST_M_FOREIGN: return "MountError::ForeignOrUnknown"; 39 case ST_M_UNSUPPORTED_VERSION: return "MountError::UnsupportedVersion"; 40 case ST_M_CORRUPT_RECOVERY_REQUIRED: return "MountError::CorruptRecoveryRequired"; 41 case ST_M_CONFLICTING_ROOTS: return "MountError::ConflictingRoots"; 42 case ST_M_INCONSISTENT_HISTORY: return "MountError::InconsistentHistory"; 43 case ST_E_IO: return "StoreError::Io"; 44 case ST_E_CORRUPT: return "StoreError::Corrupt"; 45 case ST_E_READ_ONLY_DEGRADED: return "StoreError::ReadOnlyDegraded"; 46 case ST_E_DUPLICATE_OBJECT_CONFLICT: return "StoreError::DuplicateObjectConflict"; 47 case ST_E_NO_SPACE: return "StoreError::NoSpace"; 48 case ST_E_CATALOG_FULL: return "StoreError::CatalogFull"; 49 case ST_E_GENERATION_EXHAUSTED: return "StoreError::GenerationExhausted"; 50 case ST_E_TRANSACTION_LIMIT: return "StoreError::TransactionLimit"; 51 case ST_E_INVALID_OBJECT: return "StoreError::InvalidObject"; 52 case ST_E_NEEDS_REOPEN: return "StoreError::NeedsReopen"; 53 case ST_E_ARG: return "Arg"; 54 case ST_E_GEOMETRY: return "Geometry"; 55 default: return sv1_strerror(e); 56 } 57 } 58 59 const char *st_checkpoint_name(int cp) 60 { 61 switch (cp) { 62 case ST_CP_BEFORE_FIRST_WRITE: return "before_first_write"; 63 case ST_CP_AFTER_PAYLOAD_OBJECTS: return "after_payload_objects"; 64 case ST_CP_AFTER_CATALOG: return "after_catalog"; 65 case ST_CP_AFTER_COMMIT_RECORD: return "after_commit_record"; 66 case ST_CP_AFTER_FIRST_FLUSH: return "after_first_flush"; 67 case ST_CP_AFTER_INACTIVE_SUPERBLOCK: return "after_inactive_superblock"; 68 case ST_CP_AFTER_FINAL_FLUSH: return "after_final_flush"; 69 default: return "unknown"; 70 } 71 } 72 73 static int dev_read(const st_dev *d, uint64_t unit, uint8_t *buf) 74 { 75 return d->read_unit(d->ctx, unit, buf); 76 } 77 static int dev_write(const st_dev *d, uint64_t unit, const uint8_t *buf) 78 { 79 return d->write_unit(d->ctx, unit, buf); 80 } 81 82 /* read_application_object, streamed one unit at a time through ws->unit. 83 * Like Rust, every unit is read first in order (a read failure is RE_IO 84 * whatever the bytes are), and only then is the extent judged. If out is 85 * set the semantic bytes are copied there (caller checked the size). If cmp 86 * is set, *differs reports whether the semantic bytes differ from cmp. */ 87 static int read_app_object(const st_dev *d, st_workspace *ws, const sv1_entry *e, uint8_t *out, 88 const uint8_t *cmp, int *differs) 89 { 90 sha256_ctx ctx; 91 int graph = 0; 92 uint32_t units; 93 if (sv1_unit_count(e->byte_length, &units) || units != e->unit_count) 94 graph = 1; 95 if (!graph && sv1_object_id_begin(&ctx, e->kind, e->version, e->byte_length)) 96 graph = 1; 97 if (differs) 98 *differs = 0; 99 for (uint32_t i = 0; i < e->unit_count; i++) { 100 if (dev_read(d, e->first_unit + i, ws->unit)) 101 return RE_IO; 102 if (graph) 103 continue; 104 uint64_t start = (uint64_t)i * SV1_UNIT; 105 uint64_t take = e->byte_length > start ? e->byte_length - start : 0; 106 if (take > SV1_UNIT) 107 take = SV1_UNIT; 108 sha256_update(&ctx, ws->unit, (size_t)take); 109 if (!sv1_all_zero(ws->unit + take, SV1_UNIT - (size_t)take)) graph = 1; /* GUARD:object-padding */ 110 if (out) 111 sv1_copy(out + start, ws->unit, (size_t)take); 112 if (cmp && differs && !sv1_equal(cmp + start, ws->unit, (size_t)take)) 113 *differs = 1; 114 } 115 if (graph) 116 return RE_GRAPH; 117 uint8_t id[32]; 118 sha256_final(&ctx, id); 119 if (!sv1_equal(id, e->object_id, 32)) return RE_GRAPH; /* GUARD:object-hash */ 120 return RE_OK; 121 } 122 123 static int validate_root(const st_dev *d, st_workspace *ws, const sv1_superblock *sb, 124 uint32_t cat_index, st_root *root) 125 { 126 if (sb->region_units != d->region_units) 127 return RE_GRAPH; 128 if (dev_read(d, sb->commit_record_unit, ws->unit)) 129 return RE_IO; 130 if (!sv1_all_zero(ws->unit + SV1_COMMIT_BYTES, SV1_UNIT - SV1_COMMIT_BYTES)) return RE_GRAPH; /* GUARD:commit-unit-padding */ 131 uint8_t cid[32]; 132 if (sv1_object_id(SV1_KIND_COMMIT, SV1_VERSION, ws->unit, SV1_COMMIT_BYTES, cid)) 133 return RE_GRAPH; 134 /* Rust order; not a mutation guard: sb-commit-match refuses the same case */ 135 if (!sv1_equal(cid, sb->commit_record_id, 32)) 136 return RE_GRAPH; 137 sv1_commit c; 138 if (sv1_commit_decode(ws->unit, SV1_COMMIT_BYTES, &c)) 139 return RE_GRAPH; 140 if (sv1_superblock_validate_commit(sb, cid, &c)) 141 return RE_GRAPH; 142 /* commit decode bounds the catalog to SV1_MAX_CATALOG_UNITS units */ 143 for (uint32_t i = 0; i < c.catalog_unit_count; i++) 144 if (dev_read(d, c.catalog_first_unit + i, ws->catbuf + (size_t)i * SV1_UNIT)) 145 return RE_IO; 146 if (sv1_validate_extent(c.catalog_id, SV1_KIND_CATALOG, SV1_VERSION, c.catalog_byte_length, 147 ws->catbuf, (size_t)c.catalog_unit_count * SV1_UNIT)) 148 return RE_GRAPH; 149 sv1_entry *entries = ws->cat[cat_index]; 150 uint32_t n; 151 if (sv1_catalog_decode(ws->catbuf, (size_t)c.catalog_byte_length, entries, ST_CAT_CAP, &n)) 152 return RE_GRAPH; 153 if (sv1_commit_validates_catalog(&c, c.catalog_id, entries, n)) 154 return RE_GRAPH; 155 if (sv1_catalog_validate_extents(entries, n, c.region_units, c.catalog_first_unit, 156 c.committed_high_water, ws->ext)) 157 return RE_GRAPH; 158 for (uint32_t i = 0; i < n; i++) { 159 int r = read_app_object(d, ws, &entries[i], (uint8_t *)0, (const uint8_t *)0, (int *)0); 160 if (r) 161 return r; 162 } 163 root->sb = *sb; 164 root->commit = c; 165 sv1_copy(root->commit_id, cid, 32); 166 root->cat_index = cat_index; 167 root->n = n; 168 return RE_OK; 169 } 170 171 static int verify_append_transition(st_workspace *ws, const st_root *older, const st_root *newer) 172 { 173 const sv1_entry *o = ws->cat[older->cat_index]; 174 const sv1_entry *nw = ws->cat[newer->cat_index]; 175 for (uint32_t i = 0; i < older->n; i++) { 176 long j = sv1_catalog_find(nw, newer->n, o[i].object_id); 177 /* one line: with j < 0 the second test would read nw[-1] */ 178 if (j < 0 || !sv1_entry_equal(&nw[j], &o[i])) return ST_M_INCONSISTENT_HISTORY; /* GUARD:append-keeps-old */ 179 } 180 size_t k = 0; 181 for (uint32_t i = 0; i < newer->n; i++) { 182 long j = sv1_catalog_find(o, older->n, nw[i].object_id); 183 if (j >= 0) { 184 if (!sv1_entry_equal(&o[j], &nw[i])) 185 return ST_M_INCONSISTENT_HISTORY; 186 continue; 187 } 188 ws->ext[k].first = nw[i].first_unit; 189 ws->ext[k].end = nw[i].unit_count; 190 k++; 191 } 192 sv1_sort_extents(ws->ext, k); 193 uint64_t next = older->commit.committed_high_water; 194 for (size_t i = 0; i < k; i++) { 195 if (ws->ext[i].first != next) return ST_M_INCONSISTENT_HISTORY; /* GUARD:append-contiguous */ 196 if (next > UINT64_MAX - ws->ext[i].end) 197 return ST_M_INCONSISTENT_HISTORY; 198 next += ws->ext[i].end; 199 } 200 if (next != newer->commit.catalog_first_unit) return ST_M_INCONSISTENT_HISTORY; /* GUARD:append-ends-at-catalog */ 201 return 0; 202 } 203 204 static int select_two(st_workspace *ws, const st_root *a, const st_root *b, st_root *out) 205 { 206 if (a->commit.generation == b->commit.generation) { 207 if (!sv1_superblock_equivalent(&a->sb, &b->sb)) return ST_M_CONFLICTING_ROOTS; /* GUARD:same-gen-equivalent */ 208 *out = *a; 209 return 0; 210 } 211 const st_root *older = a->commit.generation < b->commit.generation ? a : b; 212 const st_root *newer = older == a ? b : a; 213 /* Rust parity; not a mutation guard: the predecessor check below also 214 * requires newer generation == older generation + 1 */ 215 if (older->commit.generation == UINT64_MAX || 216 older->commit.generation + 1 != newer->commit.generation) 217 return ST_M_INCONSISTENT_HISTORY; 218 if (!sv1_commit_identifies_predecessor(&newer->commit, &older->commit, older->commit_id)) return ST_M_INCONSISTENT_HISTORY; /* GUARD:predecessor */ 219 int r = verify_append_transition(ws, older, newer); 220 if (r) 221 return r; 222 *out = *newer; 223 return 0; 224 } 225 226 static int unsupported_sb(const uint8_t *b, int magic, int crc_ok) 227 { 228 return magic && crc_ok && 229 (sv1_get16(b + 8) != 1 || sv1_get16(b + 10) != 0 || !sv1_all_zero(b + 12, 16)); 230 } 231 232 static const uint8_t SB_MAGIC[8] = {'A', 'I', 'E', 'N', 'S', 'T', 'R', '1'}; 233 234 int st_open(st_store *s, const st_dev *dev, st_workspace *ws) 235 { 236 if (!s || !dev || !ws) 237 return ST_E_ARG; 238 /* No state from an earlier open (or an uninitialised caller struct) may 239 * survive: every successful return below sets state and peer itself. */ 240 s->state = -1; 241 s->peer = -1; 242 s->poisoned = 1; 243 uint64_t units = dev->region_units; 244 if (units < 4 || units > SV1_MAX_REGION_UNITS) 245 return ST_M_CORRUPT_RECOVERY_REQUIRED; 246 uint8_t *raw[2] = {ws->unit, ws->unit2}; 247 /* validate_root reuses ws->unit, so slot 0's bytes move to plan_sb. */ 248 raw[0] = ws->plan_sb; 249 if (dev_read(dev, 0, raw[0])) 250 return ST_M_IO; 251 if (dev_read(dev, 1, raw[1])) 252 return ST_M_IO; 253 int zero[2], magic[2], crc_ok[2], unsup[2]; 254 uint64_t gen_of[2]; 255 for (int i = 0; i < 2; i++) { 256 zero[i] = sv1_all_zero(raw[i], SV1_UNIT); 257 magic[i] = sv1_equal(raw[i], SB_MAGIC, 8); 258 crc_ok[i] = sv1_superblock_crc_ok(raw[i]); 259 unsup[i] = unsupported_sb(raw[i], magic[i], crc_ok[i]); 260 gen_of[i] = sv1_get64(raw[i] + 56); 261 } 262 if (zero[0] && zero[1]) 263 return ST_M_UNFORMATTED; 264 if (!magic[0] && !magic[1]) 265 return ST_M_FOREIGN; 266 for (int i = 0; i < 2; i++) { 267 if (crc_ok[i] && unsup[i]) return ST_M_UNSUPPORTED_VERSION; /* GUARD:unsupported-root */ 268 } 269 270 sv1_superblock sbs[2]; 271 int decoded[2] = {0, 0}, malformed[2] = {0, 0}, crc_supported[2] = {0, 0}; 272 for (int i = 0; i < 2; i++) { 273 if (zero[i]) 274 continue; 275 if (!magic[i] || !crc_ok[i] || unsup[i]) { 276 malformed[i] = 1; 277 continue; 278 } 279 crc_supported[i] = 1; 280 if (sv1_superblock_decode(raw[i], SV1_UNIT, (uint32_t)i, &sbs[i]) || 281 sbs[i].region_units != units) { 282 malformed[i] = 1; 283 continue; 284 } 285 decoded[i] = 1; 286 } 287 st_root roots[2]; 288 int have[2] = {0, 0}, graph_bad[2] = {0, 0}; 289 for (int i = 0; i < 2; i++) { 290 if (!decoded[i]) 291 continue; 292 int r = validate_root(dev, ws, &sbs[i], (uint32_t)i, &roots[i]); 293 if (r == RE_IO) 294 return ST_M_IO; 295 if (r == RE_GRAPH) 296 graph_bad[i] = 1; 297 else 298 have[i] = 1; 299 } 300 301 s->dev = *dev; 302 s->ws = ws; 303 s->poisoned = 0; 304 if (have[0] && have[1]) { 305 int r = select_two(ws, &roots[0], &roots[1], &s->root); 306 if (r) 307 return r; 308 s->state = ST_VALID; 309 s->peer = ST_PEER_VALID; 310 return 0; 311 } 312 if (have[0] || have[1]) { 313 int good = have[0] ? 0 : 1; 314 int other = 1 - good; 315 s->root = roots[good]; 316 if (zero[other]) { 317 s->state = ST_VALID; 318 s->peer = ST_PEER_ZERO; 319 return 0; 320 } 321 if (malformed[other]) { 322 if (unsup[other] && crc_ok[other]) 323 return ST_M_UNSUPPORTED_VERSION; 324 s->state = ST_DEGRADED_RECOVERY; /* GUARD:malformed-peer-degrades */ 325 s->peer = ST_PEER_MALFORMED; 326 return 0; 327 } 328 if (graph_bad[other] && crc_supported[other]) { 329 if (gen_of[other] > roots[good].sb.generation) { 330 s->state = ST_DEGRADED_RECOVERY; /* GUARD:newer-bad-peer-degrades */ 331 s->peer = ST_PEER_GRAPH_BAD_NEWER; 332 return 0; 333 } 334 if (gen_of[other] < roots[good].sb.generation) { 335 s->state = ST_VALID; 336 s->peer = ST_PEER_GRAPH_BAD_OLDER; 337 return 0; 338 } 339 return ST_M_CORRUPT_RECOVERY_REQUIRED; 340 } 341 return ST_M_CORRUPT_RECOVERY_REQUIRED; 342 } 343 return ST_M_CORRUPT_RECOVERY_REQUIRED; 344 } 345 346 static int write_object(const st_dev *d, st_workspace *ws, uint64_t first, uint32_t units, 347 const uint8_t *bytes, uint64_t len) 348 { 349 for (uint32_t i = 0; i < units; i++) { 350 uint64_t start = (uint64_t)i * SV1_UNIT; 351 uint64_t take = len > start ? len - start : 0; 352 if (take > SV1_UNIT) 353 take = SV1_UNIT; 354 sv1_zero(ws->unit, SV1_UNIT); 355 sv1_copy(ws->unit, bytes + start, (size_t)take); 356 if (dev_write(d, first + i, ws->unit)) 357 return ST_E_IO; 358 } 359 return 0; 360 } 361 362 typedef struct { 363 size_t nreq; 364 size_t cat_len; 365 uint32_t cat_units; 366 uint64_t catalog_first; 367 uint64_t commit_unit; 368 st_root root; 369 } st_plan; 370 371 static int preflight(st_store *s, const st_object *objects, size_t n, st_plan *p) 372 { 373 st_workspace *ws = s->ws; 374 if (n > SV1_MAX_TRANSACTION_OBJECTS) return ST_E_TRANSACTION_LIMIT; /* GUARD:tx-object-limit */ 375 size_t k = 0; 376 for (size_t i = 0; i < n; i++) { 377 const st_object *o = &objects[i]; 378 uint8_t id[32]; 379 int r = sv1_object_id(o->kind, o->version, o->bytes, o->len, id); 380 if (r) 381 return r; 382 uint32_t units; 383 r = sv1_unit_count(o->len, &units); 384 if (r) 385 return r; 386 size_t j; 387 for (j = 0; j < k; j++) 388 if (sv1_equal(ws->req[j].id, id, 32)) 389 break; 390 if (j < k) { 391 const st_req *x = &ws->req[j]; 392 int same = x->kind == o->kind && x->version == o->version && x->len == o->len && 393 sv1_equal(x->bytes, o->bytes, (size_t)o->len); 394 /* Rust parity; not a mutation guard: equal ObjectIds with different 395 * kind, version or bytes need a SHA-256 collision */ 396 if (!same) 397 return ST_E_DUPLICATE_OBJECT_CONFLICT; 398 continue; 399 } 400 st_req *x = &ws->req[k++]; 401 sv1_copy(x->id, id, 32); 402 x->kind = o->kind; 403 x->version = o->version; 404 x->bytes = o->bytes; 405 x->len = o->len; 406 x->units = units; 407 x->first_unit = 0; 408 x->append = 0; 409 } 410 411 const sv1_entry *old = ws->cat[s->root.cat_index]; 412 uint32_t on = s->root.n; 413 uint32_t spare = 1 - s->root.cat_index; 414 sv1_entry *e = ws->cat[spare]; 415 for (uint32_t i = 0; i < on; i++) 416 e[i] = old[i]; 417 size_t m = on; 418 uint64_t app_units = 0; 419 uint64_t next = s->root.commit.committed_high_water; 420 for (size_t i = 0; i < k; i++) { 421 st_req *x = &ws->req[i]; 422 long j = sv1_catalog_find(old, on, x->id); 423 if (j >= 0) { 424 const sv1_entry *oe = &old[j]; 425 int same = oe->kind == x->kind && oe->version == x->version && 426 oe->byte_length == x->len && oe->unit_count == x->units; 427 if (!same) return ST_E_DUPLICATE_OBJECT_CONFLICT; /* GUARD:dedup-descriptor */ 428 int differs = 0; 429 int r = read_app_object(&s->dev, ws, oe, (uint8_t *)0, x->bytes, &differs); 430 if (r == RE_IO) 431 return ST_E_IO; 432 if (r == RE_GRAPH) return ST_E_CORRUPT; /* GUARD:dedup-verify */ 433 if (differs) 434 return ST_E_DUPLICATE_OBJECT_CONFLICT; 435 continue; 436 } 437 if (app_units > UINT64_MAX - x->units) 438 return ST_E_NO_SPACE; 439 app_units += x->units; 440 sv1_entry *ne = &e[m++]; 441 sv1_copy(ne->object_id, x->id, 32); 442 ne->kind = x->kind; 443 ne->version = x->version; 444 ne->first_unit = next; 445 ne->byte_length = x->len; 446 ne->unit_count = x->units; 447 ne->flags = 0; 448 x->first_unit = next; 449 x->append = 1; 450 if (next > UINT64_MAX - x->units) 451 return ST_E_NO_SPACE; 452 next += x->units; 453 } 454 sv1_sort_entries(e, m); 455 if (m > SV1_MAX_CATALOG_ENTRIES) return ST_E_CATALOG_FULL; /* GUARD:catalog-full */ 456 if (app_units > SV1_MAX_TRANSACTION_UNITS) return ST_E_TRANSACTION_LIMIT; /* GUARD:tx-unit-limit */ 457 size_t cat_len; 458 int r = sv1_catalog_encode(e, m, ws->catbuf, sizeof ws->catbuf, &cat_len); 459 if (r) 460 return r; 461 uint32_t cat_units; 462 r = sv1_unit_count(cat_len, &cat_units); 463 if (r) 464 return r; 465 uint64_t catalog_first = next; 466 if (catalog_first > UINT64_MAX - cat_units) 467 return ST_E_NO_SPACE; 468 uint64_t commit_unit = catalog_first + cat_units; 469 if (commit_unit == UINT64_MAX) 470 return ST_E_NO_SPACE; 471 uint64_t hw = commit_unit + 1; 472 if (hw > s->dev.region_units || hw > SV1_MAX_REGION_UNITS) return ST_E_NO_SPACE; /* GUARD:no-space */ 473 if (s->root.commit.generation == UINT64_MAX) return ST_E_GENERATION_EXHAUSTED; /* GUARD:generation-exhausted */ 474 uint64_t gen = s->root.commit.generation + 1; 475 476 st_root *nr = &p->root; 477 sv1_commit *c = &nr->commit; 478 sv1_zero(c, sizeof *c); 479 sv1_copy(c->store_uuid, s->root.commit.store_uuid, 16); 480 c->region_units = s->root.commit.region_units; 481 c->generation = gen; 482 c->previous_generation = s->root.commit.generation; 483 sv1_copy(c->previous_commit_id, s->root.commit_id, 32); 484 sv1_copy(c->previous_catalog_id, s->root.commit.catalog_id, 32); 485 r = sv1_object_id(SV1_KIND_CATALOG, SV1_VERSION, ws->catbuf, cat_len, c->catalog_id); 486 if (r) 487 return r; 488 c->catalog_first_unit = catalog_first; 489 c->catalog_byte_length = cat_len; 490 c->catalog_unit_count = cat_units; 491 c->catalog_entry_count = (uint32_t)m; 492 c->committed_high_water = hw; 493 sv1_zero(ws->plan_commit, SV1_UNIT); 494 r = sv1_commit_encode(c, ws->plan_commit); 495 if (r) 496 return r; 497 r = sv1_object_id(SV1_KIND_COMMIT, SV1_VERSION, ws->plan_commit, SV1_COMMIT_BYTES, 498 nr->commit_id); 499 if (r) 500 return r; 501 sv1_superblock *sb = &nr->sb; 502 sv1_zero(sb, sizeof *sb); 503 sv1_copy(sb->store_uuid, c->store_uuid, 16); 504 sb->slot_id = 1 - s->root.sb.slot_id; 505 sb->region_units = c->region_units; 506 sb->generation = gen; 507 sv1_copy(sb->commit_record_id, nr->commit_id, 32); 508 sb->commit_record_unit = commit_unit; 509 sv1_copy(sb->catalog_id, c->catalog_id, 32); 510 sb->catalog_first_unit = catalog_first; 511 sb->catalog_byte_length = c->catalog_byte_length; 512 sb->catalog_unit_count = cat_units; 513 sb->catalog_entry_count = c->catalog_entry_count; 514 sb->committed_high_water = hw; 515 r = sv1_superblock_encode(sb, ws->plan_sb); 516 if (r) 517 return r; 518 nr->cat_index = spare; 519 nr->n = (uint32_t)m; 520 p->nreq = k; 521 p->cat_len = cat_len; 522 p->cat_units = cat_units; 523 p->catalog_first = catalog_first; 524 p->commit_unit = commit_unit; 525 return 0; 526 } 527 528 static void hook_at(st_hook hook, void *arg, int cp) 529 { 530 if (hook) 531 hook(arg, cp); 532 } 533 534 int st_transact(st_store *s, const st_object *objects, size_t n, st_hook hook, void *arg) 535 { 536 if (!s || (n && !objects)) 537 return ST_E_ARG; 538 if (s->poisoned) return ST_E_NEEDS_REOPEN; /* GUARD:poisoned */ 539 if (s->state == ST_DEGRADED_RECOVERY) return ST_E_READ_ONLY_DEGRADED; /* GUARD:degraded-read-only */ 540 st_plan p; 541 int r = preflight(s, objects, n, &p); 542 if (r) 543 return r; 544 st_workspace *ws = s->ws; 545 hook_at(hook, arg, ST_CP_BEFORE_FIRST_WRITE); 546 for (size_t i = 0; i < p.nreq; i++) { 547 st_req *x = &ws->req[i]; 548 if (!x->append) 549 continue; 550 if (write_object(&s->dev, ws, x->first_unit, x->units, x->bytes, x->len)) { 551 s->poisoned = 1; 552 return ST_E_IO; 553 } 554 } 555 hook_at(hook, arg, ST_CP_AFTER_PAYLOAD_OBJECTS); 556 if (write_object(&s->dev, ws, p.catalog_first, p.cat_units, ws->catbuf, p.cat_len)) { 557 s->poisoned = 1; 558 return ST_E_IO; 559 } 560 hook_at(hook, arg, ST_CP_AFTER_CATALOG); 561 if (dev_write(&s->dev, p.commit_unit, ws->plan_commit)) { 562 s->poisoned = 1; 563 return ST_E_IO; 564 } 565 hook_at(hook, arg, ST_CP_AFTER_COMMIT_RECORD); 566 /* CK_TEST_STORE_MUTANT_SKIP_ROOT_FLUSH: TEST-ONLY mutant of the C kernel 567 * crash gate (native/kernel make full CK_TEST_STORE_CRASH=1 568 * CK_TEST_STORE_CRASH_MUTANT=skip_root_flush); never defined otherwise. */ 569 #ifndef CK_TEST_STORE_MUTANT_SKIP_ROOT_FLUSH 570 if (s->dev.flush(s->dev.ctx)) { 571 s->poisoned = 1; 572 return ST_E_IO; 573 } 574 #endif 575 hook_at(hook, arg, ST_CP_AFTER_FIRST_FLUSH); 576 if (dev_write(&s->dev, p.root.sb.slot_id, ws->plan_sb)) { 577 s->poisoned = 1; 578 return ST_E_IO; 579 } 580 hook_at(hook, arg, ST_CP_AFTER_INACTIVE_SUPERBLOCK); 581 if (s->dev.flush(s->dev.ctx)) { 582 s->poisoned = 1; 583 return ST_E_IO; 584 } 585 hook_at(hook, arg, ST_CP_AFTER_FINAL_FLUSH); 586 s->root = p.root; 587 s->peer = ST_PEER_VALID; 588 return 0; 589 } 590 591 int st_read_object(st_store *s, const uint8_t id[32], uint8_t *out, size_t cap, size_t *len) 592 { 593 if (!s || !id || !len) 594 return ST_E_ARG; 595 *len = 0; 596 const sv1_entry *cat = s->ws->cat[s->root.cat_index]; 597 long j = sv1_catalog_find(cat, s->root.n, id); 598 if (j < 0) 599 return ST_E_INVALID_OBJECT; 600 const sv1_entry *e = &cat[j]; 601 if (!out || e->byte_length > cap) 602 return ST_E_ARG; 603 int r = read_app_object(&s->dev, s->ws, e, out, (const uint8_t *)0, (int *)0); 604 if (r) { 605 sv1_zero(out, cap); 606 return r == RE_IO ? ST_E_IO : ST_E_CORRUPT; 607 } 608 *len = (size_t)e->byte_length; 609 return 0; 610 } 611 612 int st_format(const st_dev *dev, const uint8_t uuid[16]) 613 { 614 static const uint8_t zero_unit[SV1_UNIT]; 615 uint8_t g[4][SV1_UNIT]; 616 if (!dev || !uuid) 617 return ST_E_ARG; 618 int r = sv1_genesis_units(uuid, dev->region_units, g); 619 if (r) 620 return r; 621 /* Clear both slots first so a cut never leaves an old root pointing at 622 * the new units; then the genesis graph; then superblock A. */ 623 if (dev_write(dev, 0, zero_unit) || dev_write(dev, 1, zero_unit) || dev->flush(dev->ctx)) 624 return ST_E_IO; 625 if (dev_write(dev, 2, g[2]) || dev_write(dev, 3, g[3]) || dev->flush(dev->ctx)) 626 return ST_E_IO; 627 if (dev_write(dev, 0, g[0]) || dev->flush(dev->ctx)) 628 return ST_E_IO; 629 return 0; 630 }