omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / src / tensor / omega_tensor_store.c
| 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 | * M20 OMEGA_TENSOR crash-safe storage lifetime. See omega_tensor_store.h. |
| 19 | * Commit pattern (temp write, fsync, rename, fsync directory; phased crash |
| 20 | * points behind a test-only hook that calls _exit) follows |
| 21 | * src/runtime/rx_generation.c; nothing from src/runtime is linked. |
| 22 | */ |
| 23 | #include "omega_tensor_store.h" |
| 24 | #include "omega_api_decl.h" |
| 25 | #include "omega_native.h" |
| 26 | |
| 27 | #include <dirent.h> |
| 28 | #include <errno.h> |
| 29 | #include <fcntl.h> |
| 30 | #include <stdio.h> |
| 31 | #include <stdlib.h> |
| 32 | #include <string.h> |
| 33 | #include <sys/file.h> |
| 34 | #include <sys/stat.h> |
| 35 | #include <unistd.h> |
| 36 | |
| 37 | #include "sha256.h" |
| 38 | |
| 39 | #define JRN_MAGIC "OTSJRN1" /* 7 chars + NUL = 8 bytes */ |
| 40 | #define REC_MAGIC "OTSREC1" |
| 41 | #define JRN_VERSION 1u |
| 42 | #define HDR_BYTES 56u /* magic 8, version 4, capacity 4, count 8, sum 32 */ |
| 43 | #define HDR_SUM 24u |
| 44 | #define REC_BYTES 200u |
| 45 | #define REC_SUM 168u |
| 46 | #define REC_PUT 1u |
| 47 | #define REC_RELEASE 2u |
| 48 | #define PAYLOAD_TMP "payload.tmp" |
| 49 | #define JOURNAL_TMP "journal.tmp" |
| 50 | #define JOURNAL "journal" |
| 51 | #define PATH_MAX_LEN 600 |
| 52 | |
| 53 | typedef struct { |
| 54 | bool live; |
| 55 | uint64_t gen; |
| 56 | OmegaDType dtype; |
| 57 | uint32_t rank; |
| 58 | uint64_t shape[OMEGA_TENSOR_MAX_RANK]; |
| 59 | uint8_t value_id[32]; |
| 60 | uint8_t payload_sha[32]; |
| 61 | } StoreSlot; |
| 62 | |
| 63 | typedef struct { |
| 64 | uint32_t kind; |
| 65 | uint32_t slot; |
| 66 | uint64_t gen; |
| 67 | uint64_t seq; |
| 68 | uint32_t dtype; |
| 69 | uint32_t rank; |
| 70 | uint64_t shape[OMEGA_TENSOR_MAX_RANK]; |
| 71 | uint8_t value_id[32]; |
| 72 | uint8_t payload_sha[32]; |
| 73 | } Record; |
| 74 | |
| 75 | struct OmegaTensorStore { |
| 76 | char dir[PATH_MAX_LEN - 100]; |
| 77 | int lock_fd; |
| 78 | uint32_t cap; |
| 79 | StoreSlot *slots; |
| 80 | uint8_t *jbuf; /* journal file image: header + records */ |
| 81 | size_t jlen, jcap; |
| 82 | uint64_t count; /* records in jbuf */ |
| 83 | OmegaTensorCtx *vctx; /* private context used to recompute value ids */ |
| 84 | int crash_step; |
| 85 | }; |
| 86 | |
| 87 | /* ---- crash points (test builds only) ---------------------------------- */ |
| 88 | |
| 89 | #ifdef OMEGA_TENSOR_TEST_HOOKS |
| 90 | #define CRASH_POINT(st, step) do { if ((st)->crash_step == (step)) _exit(86); } while (0) |
| 91 | int omega_tensor_store_test_set_crash_step(OmegaTensorStore *st, int step) { |
| 92 | if (!st || step < OMEGA_TSTORE_CRASH_NONE || step >= OMEGA_TSTORE_CRASH_COUNT) |
| 93 | return OMEGA_TENSOR_ERR_BAD_ARGS; |
| 94 | st->crash_step = step; |
| 95 | return OMEGA_TENSOR_OK; |
| 96 | } |
| 97 | #else |
| 98 | #define CRASH_POINT(st, step) ((void)(st), (void)(step)) |
| 99 | #endif |
| 100 | |
| 101 | /* ---- files ------------------------------------------------------------- */ |
| 102 | |
| 103 | static int path_join(char *out, size_t n, const char *a, const char *b) { |
| 104 | int w = snprintf(out, n, "%s/%s", a, b); |
| 105 | return (w < 0 || (size_t)w >= n) ? -1 : 0; |
| 106 | } |
| 107 | |
| 108 | static int write_full(int fd, const uint8_t *buf, size_t n) { |
| 109 | size_t off = 0; |
| 110 | while (off < n) { |
| 111 | ssize_t w = write(fd, buf + off, n - off); |
| 112 | if (w < 0) { |
| 113 | if (errno == EINTR) continue; |
| 114 | return -1; |
| 115 | } |
| 116 | off += (size_t)w; |
| 117 | } |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | static int fsync_path(const char *path) { |
| 122 | int fd = open(path, O_RDONLY); |
| 123 | if (fd < 0) return -1; |
| 124 | int rc = fsync(fd) == 0 ? 0 : -1; |
| 125 | close(fd); |
| 126 | return rc; |
| 127 | } |
| 128 | |
| 129 | /* Reads at most cap bytes of path into buf; *n = bytes read. A file longer |
| 130 | * than cap reads as cap bytes plus *longer = true. */ |
| 131 | static int read_upto(const char *path, uint8_t *buf, size_t cap, size_t *n, bool *longer) { |
| 132 | *n = 0; |
| 133 | *longer = false; |
| 134 | int fd = open(path, O_RDONLY); |
| 135 | if (fd < 0) return -1; |
| 136 | for (;;) { |
| 137 | uint8_t extra; |
| 138 | uint8_t *dst = *n < cap ? buf + *n : &extra; |
| 139 | size_t want = *n < cap ? cap - *n : 1; |
| 140 | ssize_t r = read(fd, dst, want); |
| 141 | if (r < 0) { |
| 142 | if (errno == EINTR) continue; |
| 143 | close(fd); |
| 144 | return -1; |
| 145 | } |
| 146 | if (r == 0) break; |
| 147 | if (*n >= cap) { *longer = true; break; } |
| 148 | *n += (size_t)r; |
| 149 | } |
| 150 | close(fd); |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | /* |
| 155 | * Durable replace of <dir>/<name> with buf: write a temp file, fsync it, |
| 156 | * rename it over the final name, fsync the directory. step0 is the first of |
| 157 | * four crash points (PARTIAL, WRITTEN, RENAMED, SYNCED). |
| 158 | */ |
| 159 | static int commit_file(OmegaTensorStore *st, const char *tmp_name, const char *name, |
| 160 | const uint8_t *buf, size_t n, int step0) { |
| 161 | char tmp[PATH_MAX_LEN], final[PATH_MAX_LEN]; |
| 162 | if (path_join(tmp, sizeof tmp, st->dir, tmp_name) || path_join(final, sizeof final, st->dir, name)) |
| 163 | return OMEGA_TSTORE_ERR_IO; |
| 164 | const char *wpath = tmp; /* MUT:TSTORE_RENAME_BEFORE_WRITE (final name only after the bytes are durable) */ |
| 165 | int fd = open(wpath, O_CREAT | O_TRUNC | O_WRONLY, 0644); |
| 166 | if (fd < 0) return OMEGA_TSTORE_ERR_IO; |
| 167 | #ifdef OMEGA_TENSOR_TEST_HOOKS |
| 168 | if (st->crash_step == step0) { |
| 169 | size_t half = n / 2 ? n / 2 : (n ? 1 : 0); |
| 170 | if (write_full(fd, buf, half) == 0) (void)fsync(fd); |
| 171 | close(fd); |
| 172 | _exit(86); |
| 173 | } |
| 174 | #endif |
| 175 | if (write_full(fd, buf, n) != 0 || fsync(fd) != 0) { |
| 176 | close(fd); |
| 177 | return OMEGA_TSTORE_ERR_IO; |
| 178 | } |
| 179 | if (close(fd) != 0) return OMEGA_TSTORE_ERR_IO; |
| 180 | CRASH_POINT(st, step0 + 1); |
| 181 | if (rename(wpath, final) != 0) return OMEGA_TSTORE_ERR_IO; |
| 182 | CRASH_POINT(st, step0 + 2); |
| 183 | if (fsync_path(st->dir) != 0) return OMEGA_TSTORE_ERR_IO; |
| 184 | CRASH_POINT(st, step0 + 3); |
| 185 | return OMEGA_TENSOR_OK; |
| 186 | } |
| 187 | |
| 188 | static void hex_name(const uint8_t id[32], char out[69]) { |
| 189 | tstore_hex_name(id, (uint8_t *)out); |
| 190 | out[68] = '\0'; |
| 191 | } |
| 192 | |
| 193 | /* A payload file name: 64 lowercase hex digits + ".bin". */ |
| 194 | static bool is_payload_name(const char *s) { |
| 195 | return tstore_is_payload_name((const uint8_t *)s, strlen(s)) != 0; |
| 196 | } |
| 197 | |
| 198 | /* ---- shapes and payload bytes ----------------------------------------- */ |
| 199 | |
| 200 | /* Preserve the caller's file-integrity error for an invalid shape. */ |
| 201 | static int shape_elems(uint32_t dtype, uint32_t rank, const uint64_t *shape, |
| 202 | int invalid, uint64_t *elems) { |
| 203 | if (omega_dtype_size((OmegaDType)dtype) == 0 || rank > OMEGA_TENSOR_MAX_RANK) return invalid; |
| 204 | if (!rank) { *elems = 1; return OMEGA_TENSOR_OK; } |
| 205 | uint64_t result; |
| 206 | if (omega2_tensor_shape_size(&result, shape, rank, elems, 1) != OMEGA_OK) |
| 207 | return OMEGA_TENSOR_ERR_CAPACITY; |
| 208 | return result ? invalid : OMEGA_TENSOR_OK; |
| 209 | } |
| 210 | |
| 211 | /* Every NaN becomes the canonical quiet NaN of its dtype (same rule as the |
| 212 | * value id), so a value id names exactly one payload byte string. */ |
| 213 | static void canonicalize(OmegaDType dt, uint8_t *buf, uint64_t elems) { |
| 214 | if (!elems) return; |
| 215 | tensor_value_bytes(buf, (size_t)elems * omega_dtype_size(dt), (uint64_t)dt, 1); |
| 216 | } |
| 217 | |
| 218 | /* Value id of a row-major payload, recomputed through the tensor layer. */ |
| 219 | static int payload_value_id(OmegaTensorStore *st, const StoreSlot *sl, const uint8_t *buf, |
| 220 | uint8_t vid[32]) { |
| 221 | OmegaTensor t; |
| 222 | int rc = omega_tensor_from_data(st->vctx, sl->dtype, sl->rank, sl->shape, buf, &t); |
| 223 | if (rc) return rc; |
| 224 | rc = omega_tensor_value_id(st->vctx, t, vid); |
| 225 | int rr = omega_tensor_release(st->vctx, t); |
| 226 | return rc ? rc : rr; |
| 227 | } |
| 228 | |
| 229 | /* |
| 230 | * Reads and verifies the payload of a live slot. On success *out (if not |
| 231 | * NULL) receives a malloc'd buffer of exactly the expected size. |
| 232 | */ |
| 233 | static int load_payload(OmegaTensorStore *st, const StoreSlot *sl, uint8_t **out) { |
| 234 | uint64_t elems; |
| 235 | int shape_rc = shape_elems((uint32_t)sl->dtype, sl->rank, sl->shape, OMEGA_TSTORE_ERR_CORRUPT, &elems); |
| 236 | if (shape_rc) return shape_rc; |
| 237 | size_t expect = (size_t)elems * omega_dtype_size(sl->dtype); |
| 238 | char name[69], path[PATH_MAX_LEN]; |
| 239 | hex_name(sl->value_id, name); |
| 240 | if (path_join(path, sizeof path, st->dir, name)) return OMEGA_TSTORE_ERR_IO; |
| 241 | uint8_t *buf = calloc(1, expect); |
| 242 | if (!buf) return OMEGA_TENSOR_ERR_CAPACITY; |
| 243 | size_t n; |
| 244 | bool longer; |
| 245 | if (read_upto(path, buf, expect, &n, &longer) != 0) { |
| 246 | int err = errno; |
| 247 | free(buf); |
| 248 | return err == ENOENT ? OMEGA_TSTORE_ERR_CORRUPT : OMEGA_TSTORE_ERR_IO; |
| 249 | } |
| 250 | /* The payload digest covers the bytes actually read; a short file keeps |
| 251 | * zeros past its end for the value id recomputation. */ |
| 252 | uint8_t dig[32], vid[32]; |
| 253 | sha256_hash(buf, n, dig); |
| 254 | int rc = payload_value_id(st, sl, buf, vid); |
| 255 | if (rc) { free(buf); return rc; } |
| 256 | if (longer || n != expect) { free(buf); return OMEGA_TSTORE_ERR_CORRUPT; } |
| 257 | if (memcmp(dig, sl->payload_sha, 32) != 0 || memcmp(vid, sl->value_id, 32) != 0) { /* MUT:TSTORE_HASH_VERIFY */ |
| 258 | free(buf); |
| 259 | return OMEGA_TSTORE_ERR_CORRUPT; |
| 260 | } |
| 261 | if (out) *out = buf; |
| 262 | else free(buf); |
| 263 | return OMEGA_TENSOR_OK; |
| 264 | } |
| 265 | |
| 266 | /* ---- journal encoding -------------------------------------------------- */ |
| 267 | |
| 268 | static void encode_header(uint32_t cap, uint64_t count, uint8_t out[HDR_BYTES]) { |
| 269 | tstore_encode_header(cap, count, out); |
| 270 | sha256_hash(out, HDR_SUM, out + HDR_SUM); |
| 271 | } |
| 272 | |
| 273 | static void encode_record(const Record *r, uint8_t out[REC_BYTES]) { |
| 274 | const uint64_t meta[6] = { r->kind, r->slot, r->gen, r->seq, r->dtype, r->rank }; |
| 275 | tstore_encode_record(meta, r->shape, r->value_id, r->payload_sha, out); |
| 276 | sha256_hash(out, REC_SUM, out + REC_SUM); |
| 277 | } |
| 278 | |
| 279 | static int decode_record(const uint8_t in[REC_BYTES], Record *r) { |
| 280 | uint8_t sum[32]; |
| 281 | uint64_t meta[6] = {0}; |
| 282 | if (!tstore_decode_record(in, meta, r->shape, r->value_id, r->payload_sha)) return OMEGA_TSTORE_ERR_TORN; |
| 283 | sha256_hash(in, REC_SUM, sum); |
| 284 | if (memcmp(sum, in + REC_SUM, 32) != 0) return OMEGA_TSTORE_ERR_TORN; /* MUT:TSTORE_RECORD_SUM */ |
| 285 | r->kind = (uint32_t)meta[0]; |
| 286 | r->slot = (uint32_t)meta[1]; |
| 287 | r->gen = meta[2]; |
| 288 | r->seq = meta[3]; |
| 289 | r->dtype = (uint32_t)meta[4]; |
| 290 | r->rank = (uint32_t)meta[5]; |
| 291 | return OMEGA_TENSOR_OK; |
| 292 | } |
| 293 | |
| 294 | /* Applies one record to the slot table, refusing anything that does not |
| 295 | * follow from the state before it. Used by replay and by live commits. */ |
| 296 | static int apply_record(OmegaTensorStore *st, const Record *r, uint64_t expect_seq) { |
| 297 | if (r->seq != expect_seq || r->slot >= st->cap) return OMEGA_TSTORE_ERR_TORN; |
| 298 | StoreSlot *sl = &st->slots[r->slot]; |
| 299 | if (r->kind == REC_PUT) { |
| 300 | if (sl->live || sl->gen == UINT64_MAX || r->gen != sl->gen) return OMEGA_TSTORE_ERR_TORN; |
| 301 | uint64_t elems; |
| 302 | int rc = shape_elems(r->dtype, r->rank, r->shape, OMEGA_TSTORE_ERR_TORN, &elems); |
| 303 | if (rc) return rc; |
| 304 | for (uint32_t d = r->rank; d < OMEGA_TENSOR_MAX_RANK; d++) |
| 305 | if (r->shape[d] != 0) return OMEGA_TSTORE_ERR_TORN; |
| 306 | sl->live = true; |
| 307 | sl->dtype = (OmegaDType)r->dtype; |
| 308 | sl->rank = r->rank; |
| 309 | memcpy(sl->shape, r->shape, sizeof sl->shape); |
| 310 | memcpy(sl->value_id, r->value_id, 32); |
| 311 | memcpy(sl->payload_sha, r->payload_sha, 32); |
| 312 | return OMEGA_TENSOR_OK; |
| 313 | } |
| 314 | if (r->kind == REC_RELEASE) { |
| 315 | if (!sl->live || r->gen != sl->gen) return OMEGA_TSTORE_ERR_TORN; |
| 316 | sl->live = false; |
| 317 | sl->gen = r->gen + 1; /* MUT:TSTORE_GEN_RESET (released generation never returns) */ |
| 318 | return OMEGA_TENSOR_OK; |
| 319 | } |
| 320 | return OMEGA_TSTORE_ERR_TORN; |
| 321 | } |
| 322 | |
| 323 | static int jbuf_reserve(OmegaTensorStore *st, size_t need) { |
| 324 | if (need <= st->jcap) return 0; |
| 325 | size_t c = st->jcap ? st->jcap : 4096; |
| 326 | while (c < need) c *= 2; |
| 327 | uint8_t *p = realloc(st->jbuf, c); |
| 328 | if (!p) return -1; |
| 329 | st->jbuf = p; |
| 330 | st->jcap = c; |
| 331 | return 0; |
| 332 | } |
| 333 | |
| 334 | /* Appends r (seq = count) and commits the whole journal durably. On any |
| 335 | * failure the in-memory journal and slot table are unchanged. */ |
| 336 | static int commit_record(OmegaTensorStore *st, Record *r) { |
| 337 | if (r->slot >= st->cap) return OMEGA_TENSOR_ERR_BAD_ARGS; |
| 338 | r->seq = st->count; |
| 339 | StoreSlot saved = st->slots[r->slot]; |
| 340 | int rc = apply_record(st, r, st->count); |
| 341 | if (rc) return rc; |
| 342 | if (jbuf_reserve(st, st->jlen + REC_BYTES) != 0) { |
| 343 | st->slots[r->slot] = saved; |
| 344 | return OMEGA_TENSOR_ERR_CAPACITY; |
| 345 | } |
| 346 | size_t old_len = st->jlen; |
| 347 | encode_record(r, st->jbuf + st->jlen); |
| 348 | st->jlen += REC_BYTES; |
| 349 | encode_header(st->cap, st->count + 1, st->jbuf); |
| 350 | rc = commit_file(st, JOURNAL_TMP, JOURNAL, st->jbuf, st->jlen, OMEGA_TSTORE_CRASH_JOURNAL_PARTIAL); |
| 351 | if (rc) { |
| 352 | st->jlen = old_len; |
| 353 | encode_header(st->cap, st->count, st->jbuf); |
| 354 | st->slots[r->slot] = saved; |
| 355 | return rc; |
| 356 | } |
| 357 | st->count++; |
| 358 | return OMEGA_TENSOR_OK; |
| 359 | } |
| 360 | |
| 361 | /* ---- recovery ---------------------------------------------------------- */ |
| 362 | |
| 363 | static bool payload_referenced(const OmegaTensorStore *st, const char *name) { |
| 364 | char n[69]; |
| 365 | for (uint32_t i = 0; i < st->cap; i++) { |
| 366 | if (!st->slots[i].live) continue; |
| 367 | hex_name(st->slots[i].value_id, n); |
| 368 | if (strcmp(n, name) == 0) return true; |
| 369 | } |
| 370 | return false; |
| 371 | } |
| 372 | |
| 373 | /* Removes temp files and payloads no live slot refers to. When only_check is |
| 374 | * set, removes nothing and returns whether any payload file exists. */ |
| 375 | static int sweep_dir(OmegaTensorStore *st, bool only_check, bool *any_payload) { |
| 376 | DIR *d = opendir(st->dir); |
| 377 | if (!d) return OMEGA_TSTORE_ERR_IO; |
| 378 | struct dirent *e; |
| 379 | bool removed = false; |
| 380 | if (any_payload) *any_payload = false; |
| 381 | while ((e = readdir(d)) != NULL) { |
| 382 | bool is_tmp = strcmp(e->d_name, PAYLOAD_TMP) == 0 || strcmp(e->d_name, JOURNAL_TMP) == 0; |
| 383 | bool is_pay = is_payload_name(e->d_name); |
| 384 | if (is_pay && any_payload) *any_payload = true; |
| 385 | if (only_check) continue; |
| 386 | if (is_tmp || (is_pay && !payload_referenced(st, e->d_name))) { |
| 387 | char p[PATH_MAX_LEN]; |
| 388 | if (path_join(p, sizeof p, st->dir, e->d_name) == 0 && unlink(p) == 0) removed = true; |
| 389 | } |
| 390 | } |
| 391 | closedir(d); |
| 392 | if (removed && fsync_path(st->dir) != 0) return OMEGA_TSTORE_ERR_IO; |
| 393 | return OMEGA_TENSOR_OK; |
| 394 | } |
| 395 | |
| 396 | static int load_journal(OmegaTensorStore *st, const char *jpath, uint32_t capacity) { |
| 397 | struct stat sb; |
| 398 | int fd = open(jpath, O_RDONLY); |
| 399 | if (fd < 0) return OMEGA_TSTORE_ERR_IO; |
| 400 | if (fstat(fd, &sb) != 0 || sb.st_size < 0) { close(fd); return OMEGA_TSTORE_ERR_IO; } |
| 401 | size_t len = (size_t)sb.st_size; |
| 402 | if (jbuf_reserve(st, len + REC_BYTES) != 0) { close(fd); return OMEGA_TENSOR_ERR_CAPACITY; } |
| 403 | size_t got = 0; |
| 404 | while (got < len) { |
| 405 | ssize_t r = read(fd, st->jbuf + got, len - got); |
| 406 | if (r < 0 && errno == EINTR) continue; |
| 407 | if (r <= 0) break; |
| 408 | got += (size_t)r; |
| 409 | } |
| 410 | close(fd); |
| 411 | len = got; |
| 412 | if (len < HDR_BYTES) return OMEGA_TSTORE_ERR_TORN; |
| 413 | uint64_t count = 0; |
| 414 | uint64_t hdr_st = tstore_decode_header(st->jbuf, capacity, &count); |
| 415 | if (hdr_st == 0) return OMEGA_TSTORE_ERR_TORN; |
| 416 | uint8_t sum[32]; |
| 417 | sha256_hash(st->jbuf, HDR_SUM, sum); |
| 418 | if (memcmp(sum, st->jbuf + HDR_SUM, 32) != 0) return OMEGA_TSTORE_ERR_TORN; /* MUT:TSTORE_HEADER_SUM */ |
| 419 | if (hdr_st == 2) return OMEGA_TENSOR_ERR_BAD_ARGS; |
| 420 | if (count > (len - HDR_BYTES) / REC_BYTES + 1) count = (len - HDR_BYTES) / REC_BYTES + 1; |
| 421 | if (len != HDR_BYTES + count * REC_BYTES) return OMEGA_TSTORE_ERR_TORN; /* MUT:TSTORE_SHORT_RECORD */ |
| 422 | uint64_t i = 0; |
| 423 | for (; i < count && HDR_BYTES + (i + 1) * REC_BYTES <= len; i++) { |
| 424 | Record r; |
| 425 | int rc = decode_record(st->jbuf + HDR_BYTES + i * REC_BYTES, &r); |
| 426 | if (!rc) rc = apply_record(st, &r, i); |
| 427 | if (rc) return rc; |
| 428 | } |
| 429 | /* Keep exactly the records that were replayed (equal to the header count |
| 430 | * unless the length check above was bypassed). */ |
| 431 | st->count = i; |
| 432 | st->jlen = HDR_BYTES + i * REC_BYTES; |
| 433 | encode_header(st->cap, st->count, st->jbuf); |
| 434 | return OMEGA_TENSOR_OK; |
| 435 | } |
| 436 | |
| 437 | int omega_tensor_store_open(const char *dir, uint32_t capacity, OmegaTensorStore **out) { |
| 438 | if (!out) return OMEGA_TENSOR_ERR_BAD_ARGS; |
| 439 | *out = NULL; |
| 440 | if (!dir || capacity == 0 || capacity > OMEGA_TSTORE_MAX_CAPACITY || |
| 441 | strlen(dir) + 80 >= sizeof(((OmegaTensorStore *)0)->dir)) |
| 442 | return OMEGA_TENSOR_ERR_BAD_ARGS; |
| 443 | if (mkdir(dir, 0755) != 0 && errno != EEXIST) return OMEGA_TSTORE_ERR_IO; |
| 444 | OmegaTensorStore *st = calloc(1, sizeof(*st)); |
| 445 | if (!st) return OMEGA_TENSOR_ERR_CAPACITY; |
| 446 | snprintf(st->dir, sizeof st->dir, "%s", dir); |
| 447 | st->cap = capacity; |
| 448 | st->lock_fd = -1; |
| 449 | int rc = OMEGA_TENSOR_OK; |
| 450 | st->slots = calloc(capacity, sizeof(StoreSlot)); |
| 451 | if (!st->slots) { rc = OMEGA_TENSOR_ERR_CAPACITY; goto fail; } |
| 452 | for (uint32_t i = 0; i < capacity; i++) st->slots[i].gen = 1; |
| 453 | rc = omega_tensor_ctx_create(2, omega_tensor_cpu_realization(), &st->vctx); |
| 454 | if (rc) goto fail; |
| 455 | st->lock_fd = open(dir, O_RDONLY | O_DIRECTORY); |
| 456 | if (st->lock_fd < 0) { rc = OMEGA_TSTORE_ERR_IO; goto fail; } |
| 457 | if (flock(st->lock_fd, LOCK_EX | LOCK_NB) != 0) { |
| 458 | rc = errno == EWOULDBLOCK ? OMEGA_TSTORE_ERR_BUSY : OMEGA_TSTORE_ERR_IO; |
| 459 | goto fail; |
| 460 | } |
| 461 | char jpath[PATH_MAX_LEN]; |
| 462 | if (path_join(jpath, sizeof jpath, dir, JOURNAL)) { rc = OMEGA_TSTORE_ERR_IO; goto fail; } |
| 463 | if (access(jpath, F_OK) != 0) { |
| 464 | if (errno != ENOENT) { rc = OMEGA_TSTORE_ERR_IO; goto fail; } |
| 465 | /* No journal: a fresh store, unless payloads exist (a lost journal |
| 466 | * would silently reset every generation). */ |
| 467 | bool any = false; |
| 468 | rc = sweep_dir(st, true, &any); |
| 469 | if (rc) goto fail; |
| 470 | if (any) { rc = OMEGA_TSTORE_ERR_TORN; goto fail; } |
| 471 | if (jbuf_reserve(st, HDR_BYTES + REC_BYTES) != 0) { rc = OMEGA_TENSOR_ERR_CAPACITY; goto fail; } |
| 472 | encode_header(capacity, 0, st->jbuf); |
| 473 | st->jlen = HDR_BYTES; |
| 474 | rc = commit_file(st, JOURNAL_TMP, JOURNAL, st->jbuf, st->jlen, OMEGA_TSTORE_CRASH_COUNT); |
| 475 | if (rc) goto fail; |
| 476 | } else { |
| 477 | rc = load_journal(st, jpath, capacity); |
| 478 | if (rc) goto fail; |
| 479 | for (uint32_t i = 0; i < capacity; i++) { |
| 480 | if (!st->slots[i].live) continue; |
| 481 | rc = load_payload(st, &st->slots[i], NULL); |
| 482 | if (rc) goto fail; |
| 483 | } |
| 484 | } |
| 485 | rc = sweep_dir(st, false, NULL); |
| 486 | if (rc) goto fail; |
| 487 | *out = st; |
| 488 | return OMEGA_TENSOR_OK; |
| 489 | fail: |
| 490 | omega_tensor_store_close(st); |
| 491 | return rc; |
| 492 | } |
| 493 | |
| 494 | void omega_tensor_store_close(OmegaTensorStore *st) { |
| 495 | if (!st) return; |
| 496 | if (st->vctx) omega_tensor_ctx_destroy(st->vctx); |
| 497 | if (st->lock_fd >= 0) close(st->lock_fd); /* drops the flock */ |
| 498 | free(st->slots); |
| 499 | free(st->jbuf); |
| 500 | free(st); |
| 501 | } |
| 502 | |
| 503 | /* ---- live operations --------------------------------------------------- */ |
| 504 | |
| 505 | static StoreSlot *live_slot(const OmegaTensorStore *st, OmegaStorageHandle h) { |
| 506 | if (!st || h.slot >= st->cap) return NULL; |
| 507 | StoreSlot *sl = &st->slots[h.slot]; |
| 508 | if (!sl->live || sl->gen != h.generation) return NULL; |
| 509 | return sl; |
| 510 | } |
| 511 | |
| 512 | bool omega_tensor_store_valid(const OmegaTensorStore *st, OmegaStorageHandle h) { |
| 513 | return live_slot(st, h) != NULL; |
| 514 | } |
| 515 | |
| 516 | int omega_tensor_store_value_id(const OmegaTensorStore *st, OmegaStorageHandle h, uint8_t id[32]) { |
| 517 | if (!st || !id) return OMEGA_TENSOR_ERR_BAD_ARGS; |
| 518 | StoreSlot *sl = live_slot(st, h); |
| 519 | if (!sl) return OMEGA_TENSOR_ERR_STALE; |
| 520 | memcpy(id, sl->value_id, 32); |
| 521 | return OMEGA_TENSOR_OK; |
| 522 | } |
| 523 | |
| 524 | uint32_t omega_tensor_store_live_count(const OmegaTensorStore *st) { |
| 525 | uint32_t n = 0; |
| 526 | if (!st) return 0; |
| 527 | for (uint32_t i = 0; i < st->cap; i++) n += st->slots[i].live ? 1u : 0u; |
| 528 | return n; |
| 529 | } |
| 530 | |
| 531 | int omega_tensor_store_put(OmegaTensorStore *st, const OmegaTensorCtx *ctx, OmegaTensor t, |
| 532 | OmegaStorageHandle *out) { |
| 533 | if (!st || !ctx || !out) return OMEGA_TENSOR_ERR_BAD_ARGS; |
| 534 | OmegaTensorInfo info; |
| 535 | int rc = omega_tensor_info(ctx, t, &info); |
| 536 | if (rc) return rc; |
| 537 | uint32_t slot = st->cap; |
| 538 | for (uint32_t i = 0; i < st->cap; i++) |
| 539 | if (!st->slots[i].live && st->slots[i].gen != UINT64_MAX) { slot = i; break; } |
| 540 | if (slot == st->cap) return OMEGA_TENSOR_ERR_CAPACITY; |
| 541 | size_t es = omega_dtype_size(info.dtype); |
| 542 | size_t bytes = (size_t)info.elements * es; |
| 543 | uint8_t *buf = malloc(bytes); |
| 544 | if (!buf) return OMEGA_TENSOR_ERR_CAPACITY; |
| 545 | rc = omega_tensor_read(ctx, t, buf, bytes); |
| 546 | if (rc) { free(buf); return rc; } |
| 547 | canonicalize(info.dtype, buf, info.elements); |
| 548 | Record r; |
| 549 | memset(&r, 0, sizeof r); |
| 550 | r.kind = REC_PUT; |
| 551 | r.slot = slot; |
| 552 | r.gen = st->slots[slot].gen; |
| 553 | r.dtype = (uint32_t)info.dtype; |
| 554 | r.rank = info.rank; |
| 555 | for (uint32_t d = 0; d < info.rank; d++) r.shape[d] = info.shape[d]; |
| 556 | rc = omega_tensor_value_id(ctx, t, r.value_id); |
| 557 | if (rc) { free(buf); return rc; } |
| 558 | sha256_hash(buf, bytes, r.payload_sha); |
| 559 | char name[69]; |
| 560 | hex_name(r.value_id, name); |
| 561 | /* Payload first, journal second: the journal rename is the commit point. */ |
| 562 | rc = commit_file(st, PAYLOAD_TMP, name, buf, bytes, OMEGA_TSTORE_CRASH_PAYLOAD_PARTIAL); |
| 563 | free(buf); |
| 564 | if (rc) return rc; |
| 565 | rc = commit_record(st, &r); |
| 566 | if (rc) return rc; |
| 567 | out->slot = slot; |
| 568 | out->generation = r.gen; |
| 569 | return OMEGA_TENSOR_OK; |
| 570 | } |
| 571 | |
| 572 | int omega_tensor_store_get(OmegaTensorStore *st, OmegaStorageHandle h, OmegaTensorCtx *ctx, |
| 573 | OmegaTensor *out) { |
| 574 | if (!st || !ctx || !out) return OMEGA_TENSOR_ERR_BAD_ARGS; |
| 575 | StoreSlot *sl = live_slot(st, h); |
| 576 | if (!sl) return OMEGA_TENSOR_ERR_STALE; |
| 577 | uint8_t *buf = NULL; |
| 578 | int rc = load_payload(st, sl, &buf); |
| 579 | if (rc) return rc; |
| 580 | rc = omega_tensor_from_data(ctx, sl->dtype, sl->rank, sl->shape, buf, out); |
| 581 | free(buf); |
| 582 | return rc; |
| 583 | } |
| 584 | |
| 585 | int omega_tensor_store_release(OmegaTensorStore *st, OmegaStorageHandle h) { |
| 586 | if (!st) return OMEGA_TENSOR_ERR_BAD_ARGS; |
| 587 | StoreSlot *sl = live_slot(st, h); |
| 588 | if (!sl) return OMEGA_TENSOR_ERR_STALE; |
| 589 | uint8_t vid[32]; |
| 590 | memcpy(vid, sl->value_id, 32); |
| 591 | Record r; |
| 592 | memset(&r, 0, sizeof r); |
| 593 | r.kind = REC_RELEASE; |
| 594 | r.slot = h.slot; |
| 595 | r.gen = h.generation; |
| 596 | int rc = commit_record(st, &r); |
| 597 | if (rc) return rc; |
| 598 | /* Committed. Drop the payload if no other live slot shares it; a crash |
| 599 | * before this leaves an orphan that the next open removes. */ |
| 600 | char name[69], path[PATH_MAX_LEN]; |
| 601 | hex_name(vid, name); |
| 602 | if (!payload_referenced(st, name) && path_join(path, sizeof path, st->dir, name) == 0) |
| 603 | (void)unlink(path); |
| 604 | return OMEGA_TENSOR_OK; |
| 605 | } |