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 / seed / ng / memo / memo_store.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note seed/ng/memo/memo_store.om: Universal Content-Addressed Memoization (OMEGA-MEMO) |
| 3 | note Stage 1 (Pillar 2) of Sovereign Intelligence 2.0. |
| 4 | note |
| 5 | note Pure seeded OM-2-compatible library unit (no om2 header, no fn main; prefix mm_ / MM_). |
| 6 | note Link after seed/ng/sha256.om and seed/ng/memo/memo_policy.om. |
| 7 | note |
| 8 | note Disk-backed CAS layout (NO arbitrary fixed entry cap): |
| 9 | note <root>/<2hex>/<64hex>.ome |
| 10 | note where base_key = SHA-256("omega.memo.key.v1\0" || func_sem_id[32] || inputs_sha256[32]). |
| 11 | note |
| 12 | note Binary .ome format (OMEM v1, fixed 224-byte header + output_bytes[0..output_len)): |
| 13 | note [0..3] magic "OMEM" (79, 77, 69, 77) |
| 14 | note [4..7] version u32 LE (1) |
| 15 | note [8] purity_flag u8 (MUST be 1) |
| 16 | note [9] precision_tier u8 (1 = @exact, 2 = @tol) |
| 17 | note [10] prov_flags u8 (bit 0 = ck_pure verified, bit 1 = OMPK v2 verified; bits 2..7 = 0) |
| 18 | note [11] reserved0 u8 (MUST be 0) |
| 19 | note [12..15] reserved1 u32 LE (MUST be 0) |
| 20 | note [16..23] err_bound u64 LE (MUST be 0 when precision_tier == 1) |
| 21 | note [24..55] func_sem_id [32B] |
| 22 | note [56..87] inputs_sha256 [32B] |
| 23 | note [88..95] output_len u64 LE |
| 24 | note [96..127] output_sha256 [32B] (FIPS 180-4 SHA-256 of output_bytes) |
| 25 | note [128..135] prov_created_ns u64 LE (realns) |
| 26 | note [136..143] prov_compute_ns u64 LE |
| 27 | note [144..151] prov_creator_pid u64 LE |
| 28 | note [152..159] prov_target_id u64 LE |
| 29 | note [160..191] prov_ir_sha256 [32B] |
| 30 | note [192..223] entry_sha256 [32B] (FIPS 180-4 SHA-256 of bytes [0..192)) |
| 31 | note [224..end) output_bytes [output_len B] |
| 32 | |
| 33 | const MM_MAGIC_0 ulong 79 |
| 34 | const MM_MAGIC_1 ulong 77 |
| 35 | const MM_MAGIC_2 ulong 69 |
| 36 | const MM_MAGIC_3 ulong 77 |
| 37 | const MM_VERSION ulong 1 |
| 38 | const MM_HDR_LEN ulong 224 |
| 39 | const MM_MAX_VAL_LEN ulong 4194304 |
| 40 | const MM_MAX_ROOT_LEN ulong 1900 |
| 41 | |
| 42 | note Precision tiers & provenance bits |
| 43 | const MM_TIER_EXACT ulong 1 |
| 44 | const MM_TIER_TOL ulong 2 |
| 45 | const MM_PROV_CK_PURE ulong 1 |
| 46 | const MM_PROV_OMPK_V2 ulong 2 |
| 47 | |
| 48 | note Return & fail-closed refusal codes |
| 49 | const MM_OK ulong 0 |
| 50 | const MM_MISS ulong 1 |
| 51 | const MM_OK_DOMINATED ulong 2 |
| 52 | const MM_SKIP_UNPROFITABLE ulong 3 |
| 53 | const MM_OK_ALARM ulong 4 |
| 54 | |
| 55 | const MM_ERR_MAGIC ulong 10 |
| 56 | const MM_ERR_VERSION ulong 11 |
| 57 | const MM_ERR_TRUNCATED ulong 12 |
| 58 | const MM_ERR_TRAILING ulong 13 |
| 59 | const MM_ERR_TAMPER ulong 14 |
| 60 | const MM_ERR_KEY_MISMATCH ulong 15 |
| 61 | const MM_ERR_FORMAT ulong 16 |
| 62 | const MM_ERR_PRECISION_MISS ulong 17 |
| 63 | const MM_ERR_IMPURE ulong 20 |
| 64 | const MM_ERR_CONTRACT ulong 21 |
| 65 | const MM_ERR_NONDET ulong 22 |
| 66 | const MM_ERR_NO_ROOT ulong 23 |
| 67 | const MM_ERR_IO ulong 24 |
| 68 | const MM_ERR_OMPK ulong 25 |
| 69 | |
| 70 | note Linux ARM64 fopen flags (with O_NOFOLLOW = 32768) |
| 71 | const MM_O_RDONLY int 32768 |
| 72 | const MM_O_WRNEW int 33345 |
| 73 | |
| 74 | note Active root directory |
| 75 | global mm_root uchar 4096 |
| 76 | global mm_root_len ulong 1 |
| 77 | |
| 78 | note Active request staging: func_sem_id, inputs_sha256, contract, provenance |
| 79 | global mm_req_fid uchar 32 |
| 80 | global mm_req_in_sha uchar 32 |
| 81 | note [0]=purity_flag [1]=ck_pure_verified [2]=precision_tier [3]=err_bound |
| 82 | note [4]=prov_compute_ns [5]=prov_target_id [6]=prov_flags_extra |
| 83 | global mm_req_contract ulong 8 |
| 84 | global mm_req_ir_sha uchar 32 |
| 85 | |
| 86 | note Active value payload buffer (caller writes before mm_store, reads after mm_lookup hit) |
| 87 | global mm_val_buf uchar 4194304 |
| 88 | global mm_val_len ulong 1 |
| 89 | global mm_val_sha uchar 32 |
| 90 | |
| 91 | note Verified hit metadata from last read/lookup: |
| 92 | note [0]=purity [1]=tier [2]=err_bound [3]=prov_flags |
| 93 | note [4]=created_ns [5]=compute_ns [6]=creator_pid [7]=target_id |
| 94 | global mm_hit_meta ulong 8 |
| 95 | global mm_hit_ir_sha uchar 32 |
| 96 | global mm_hit_entry_sha uchar 32 |
| 97 | |
| 98 | note Internal key, path, header, atomic-rename, and environment scratch buffers |
| 99 | global mm_key_dig uchar 32 |
| 100 | global mm_key_hex uchar 64 |
| 101 | global mm_shard_path uchar 4096 |
| 102 | global mm_shard_plen ulong 1 |
| 103 | global mm_file_path uchar 4096 |
| 104 | global mm_file_plen ulong 1 |
| 105 | global mm_tmp_path uchar 4096 |
| 106 | global mm_tmp_plen ulong 1 |
| 107 | global mm_ren_pkt uchar 4096 |
| 108 | global mm_hdr uchar 224 |
| 109 | global mm_chk_hdr uchar 224 |
| 110 | global mm_chk_sha uchar 32 |
| 111 | global mm_env_buf uchar 4096 |
| 112 | global mm_home_buf uchar 2048 |
| 113 | global mm_home_len ulong 1 |
| 114 | global mm_probe_b uchar 16 |
| 115 | global mm_tmp_seq ulong 1 |
| 116 | global mm_ndig uchar 24 |
| 117 | |
| 118 | note Telemetry counters: |
| 119 | note [0]=lookups [1]=hits [2]=dominance_hits [3]=misses [4]=precision_refusals |
| 120 | note [5]=purity_refusals [6]=tamper_refusals [7]=stores [8]=upgrades |
| 121 | note [9]=downgrade_refusals [10]=policy_skips [11]=disk_alarms |
| 122 | global mm_stats ulong 16 |
| 123 | |
| 124 | text mm_key_tag "omega.memo.key.v1" |
| 125 | text mm_ext_ome ".ome" |
| 126 | text mm_ext_tmp ".tmp" |
| 127 | text mm_env_dir "OMEGA_MEMO_DIR=" |
| 128 | text mm_env_home "HOME=" |
| 129 | text mm_def_suf "/.local/share/omega-memo" |
| 130 | |
| 131 | note ---- Reset & Little-Endian Header Helpers ---- |
| 132 | |
| 133 | fn mm_reset_stats to void |
| 134 | for i ulong 16 |
| 135 | put mm_stats i 0 |
| 136 | end |
| 137 | end |
| 138 | |
| 139 | fn mm_reset_req to void |
| 140 | for i ulong 32 |
| 141 | put mm_req_fid i 0 |
| 142 | put mm_req_in_sha i 0 |
| 143 | put mm_req_ir_sha i 0 |
| 144 | end |
| 145 | for i ulong 8 |
| 146 | put mm_req_contract i 0 |
| 147 | end |
| 148 | put mm_val_len 0 0 |
| 149 | end |
| 150 | |
| 151 | fn mm_hdr_put_u32 off ulong v ulong to void |
| 152 | put mm_hdr off uchar and v 255 |
| 153 | put mm_hdr add off 1 uchar and shr v 8 255 |
| 154 | put mm_hdr add off 2 uchar and shr v 16 255 |
| 155 | put mm_hdr add off 3 uchar and shr v 24 255 |
| 156 | end |
| 157 | |
| 158 | fn mm_hdr_put_u64 off ulong v ulong to void |
| 159 | mm_hdr_put_u32 off and v 4294967295 |
| 160 | mm_hdr_put_u32 add off 4 shr v 32 |
| 161 | end |
| 162 | |
| 163 | fn mm_hdr_get_u32 use_chk bool off ulong to ulong |
| 164 | b0 = ulong 0 |
| 165 | b1 = ulong 0 |
| 166 | b2 = ulong 0 |
| 167 | b3 = ulong 0 |
| 168 | if use_chk |
| 169 | b0 = ulong get mm_chk_hdr off |
| 170 | b1 = shl ulong get mm_chk_hdr add off 1 8 |
| 171 | b2 = shl ulong get mm_chk_hdr add off 2 16 |
| 172 | b3 = shl ulong get mm_chk_hdr add off 3 24 |
| 173 | else |
| 174 | b0 = ulong get mm_hdr off |
| 175 | b1 = shl ulong get mm_hdr add off 1 8 |
| 176 | b2 = shl ulong get mm_hdr add off 2 16 |
| 177 | b3 = shl ulong get mm_hdr add off 3 24 |
| 178 | end |
| 179 | ret or or b0 b1 or b2 b3 |
| 180 | end |
| 181 | |
| 182 | fn mm_hdr_get_u64 use_chk bool off ulong to ulong |
| 183 | lo = mm_hdr_get_u32 use_chk off |
| 184 | hi = mm_hdr_get_u32 use_chk add off 4 |
| 185 | ret or lo shl hi 32 |
| 186 | end |
| 187 | |
| 188 | note ---- Root Directory Management & Default Env Resolution ---- |
| 189 | |
| 190 | fn mm_sync_policy_root to void |
| 191 | n = get mm_root_len 0 |
| 192 | for i ulong n bound 4096 |
| 193 | put mp_root i get mm_root i |
| 194 | end |
| 195 | put mp_root_len 0 n |
| 196 | end |
| 197 | |
| 198 | fn mm_mkdir_p to ulong |
| 199 | n = get mm_root_len 0 |
| 200 | if eq n 0 |
| 201 | ret MM_ERR_NO_ROOT |
| 202 | end |
| 203 | i = ulong 1 |
| 204 | while lt i n bound 4096 |
| 205 | if eq get mm_root i 47 |
| 206 | r = makedir mm_root 0 i 493 |
| 207 | if and ne r 0 ne r neg long 17 |
| 208 | ret MM_ERR_IO |
| 209 | end |
| 210 | end |
| 211 | i = add i 1 |
| 212 | end |
| 213 | r2 = makedir mm_root 0 n 493 |
| 214 | if and ne r2 0 ne r2 neg long 17 |
| 215 | ret MM_ERR_IO |
| 216 | end |
| 217 | ret MM_OK |
| 218 | end |
| 219 | |
| 220 | note Set root from mm_env_buf[0..n) (strips trailing slashes except single "/") |
| 221 | fn mm_set_root_from_envbuf n ulong to ulong |
| 222 | if eq n 0 |
| 223 | ret MM_ERR_NO_ROOT |
| 224 | end |
| 225 | m = n |
| 226 | while gt m 1 bound 4096 |
| 227 | if ne get mm_env_buf sub m 1 47 |
| 228 | break |
| 229 | end |
| 230 | m = sub m 1 |
| 231 | end |
| 232 | if gt m MM_MAX_ROOT_LEN |
| 233 | ret MM_ERR_NO_ROOT |
| 234 | end |
| 235 | for i ulong m bound 4096 |
| 236 | ch = get mm_env_buf i |
| 237 | if eq ch 0 |
| 238 | ret MM_ERR_NO_ROOT |
| 239 | end |
| 240 | put mm_root i ch |
| 241 | end |
| 242 | put mm_root_len 0 m |
| 243 | mm_sync_policy_root |
| 244 | ret mm_mkdir_p |
| 245 | end |
| 246 | |
| 247 | fn mm_env_starts_dir n ulong to bool |
| 248 | pl = len mm_env_dir |
| 249 | if le n pl |
| 250 | ret false |
| 251 | end |
| 252 | for i ulong pl bound 32 |
| 253 | if ne get mm_env_buf i get mm_env_dir i |
| 254 | ret false |
| 255 | end |
| 256 | end |
| 257 | ret true |
| 258 | end |
| 259 | |
| 260 | fn mm_env_starts_home n ulong to bool |
| 261 | pl = len mm_env_home |
| 262 | if le n pl |
| 263 | ret false |
| 264 | end |
| 265 | for i ulong pl bound 16 |
| 266 | if ne get mm_env_buf i get mm_env_home i |
| 267 | ret false |
| 268 | end |
| 269 | end |
| 270 | ret true |
| 271 | end |
| 272 | |
| 273 | fn mm_init_from_home to ulong |
| 274 | hl = get mm_home_len 0 |
| 275 | if eq hl 0 |
| 276 | ret MM_ERR_NO_ROOT |
| 277 | end |
| 278 | while gt hl 1 bound 2048 |
| 279 | if ne get mm_home_buf sub hl 1 47 |
| 280 | break |
| 281 | end |
| 282 | hl = sub hl 1 |
| 283 | end |
| 284 | sl = len mm_def_suf |
| 285 | tot = add hl sl |
| 286 | if gt tot MM_MAX_ROOT_LEN |
| 287 | ret MM_ERR_NO_ROOT |
| 288 | end |
| 289 | for j ulong hl bound 2048 |
| 290 | put mm_env_buf j get mm_home_buf j |
| 291 | end |
| 292 | for j ulong sl bound 64 |
| 293 | put mm_env_buf add hl j get mm_def_suf j |
| 294 | end |
| 295 | ret mm_set_root_from_envbuf tot |
| 296 | end |
| 297 | |
| 298 | fn mm_env_take_dir n ulong to ulong |
| 299 | pl = len mm_env_dir |
| 300 | vlen = sub n pl |
| 301 | for j ulong vlen bound 4096 |
| 302 | put mm_env_buf j get mm_env_buf add pl j |
| 303 | end |
| 304 | ret mm_set_root_from_envbuf vlen |
| 305 | end |
| 306 | |
| 307 | fn mm_env_take_home n ulong to void |
| 308 | pl = len mm_env_home |
| 309 | vlen = sub n pl |
| 310 | if and gt vlen 0 le vlen 1800 |
| 311 | for j ulong vlen bound 2048 |
| 312 | put mm_home_buf j get mm_env_buf add pl j |
| 313 | end |
| 314 | put mm_home_len 0 vlen |
| 315 | end |
| 316 | end |
| 317 | |
| 318 | note Resolve default root: OMEGA_MEMO_DIR if set, else $HOME/.local/share/omega-memo |
| 319 | fn mm_init_default_root to ulong |
| 320 | c = envcnt |
| 321 | if lt c 0 |
| 322 | ret MM_ERR_NO_ROOT |
| 323 | end |
| 324 | cnt = ulong c |
| 325 | put mm_home_len 0 0 |
| 326 | i = ulong 0 |
| 327 | while lt i cnt bound 4096 |
| 328 | rn = envread i mm_env_buf 0 4095 |
| 329 | if gt rn 0 |
| 330 | n = ulong rn |
| 331 | if mm_env_starts_dir n |
| 332 | ret mm_env_take_dir n |
| 333 | end |
| 334 | if mm_env_starts_home n |
| 335 | mm_env_take_home n |
| 336 | end |
| 337 | end |
| 338 | i = add i 1 |
| 339 | end |
| 340 | ret mm_init_from_home |
| 341 | end |
| 342 | |
| 343 | note ---- Contract, Purity Admission, and One-Way Dominance ---- |
| 344 | |
| 345 | fn mm_set_contract pur ulong ck_pure_ok ulong tier ulong err_b ulong to void |
| 346 | put mm_req_contract 0 pur |
| 347 | put mm_req_contract 1 ck_pure_ok |
| 348 | put mm_req_contract 2 tier |
| 349 | put mm_req_contract 3 err_b |
| 350 | end |
| 351 | |
| 352 | fn mm_set_provenance target_id ulong extra_flags ulong to void |
| 353 | put mm_req_contract 5 target_id |
| 354 | put mm_req_contract 6 extra_flags |
| 355 | end |
| 356 | |
| 357 | fn mm_set_fid_from_sh to void |
| 358 | for i ulong 32 |
| 359 | put mm_req_fid i get sh_dig i |
| 360 | end |
| 361 | end |
| 362 | |
| 363 | fn mm_set_in_sha_from_sh to void |
| 364 | for i ulong 32 |
| 365 | put mm_req_in_sha i get sh_dig i |
| 366 | end |
| 367 | end |
| 368 | |
| 369 | fn mm_set_ir_sha_from_sh to void |
| 370 | for i ulong 32 |
| 371 | put mm_req_ir_sha i get sh_dig i |
| 372 | end |
| 373 | end |
| 374 | |
| 375 | note Hash an 8-byte u64 input cleanly into mm_req_in_sha |
| 376 | fn mm_hash_input_u64 v ulong to void |
| 377 | for j ulong 8 |
| 378 | put sh_dat j uchar and shr v mul j 8 255 |
| 379 | end |
| 380 | sh_all 8 |
| 381 | mm_set_in_sha_from_sh |
| 382 | end |
| 383 | |
| 384 | note Hash mm_val_buf[0..n) into mm_req_in_sha (for arbitrary input buffers) |
| 385 | fn mm_hash_valbuf_as_input n ulong to ulong |
| 386 | if gt n MM_MAX_VAL_LEN |
| 387 | ret MM_ERR_FORMAT |
| 388 | end |
| 389 | for i ulong n bound 4194304 |
| 390 | put sh_dat i get mm_val_buf i |
| 391 | end |
| 392 | sh_all n |
| 393 | mm_set_in_sha_from_sh |
| 394 | ret MM_OK |
| 395 | end |
| 396 | |
| 397 | fn mm_val_u32 off ulong to ulong |
| 398 | b0 = ulong get mm_val_buf off |
| 399 | b1 = shl ulong get mm_val_buf add off 1 8 |
| 400 | b2 = shl ulong get mm_val_buf add off 2 16 |
| 401 | b3 = shl ulong get mm_val_buf add off 3 24 |
| 402 | ret or or b0 b1 or b2 b3 |
| 403 | end |
| 404 | |
| 405 | fn mm_val_u64 off ulong to ulong |
| 406 | lo = mm_val_u32 off |
| 407 | hi = mm_val_u32 add off 4 |
| 408 | ret or lo shl hi 32 |
| 409 | end |
| 410 | |
| 411 | note Admit directly from raw OMPK v2 wire bytes staged in mm_val_buf[0..plen) |
| 412 | note Extracts func_sem_id, prov_ir_sha256, purity_flag, precision_tier, err_bound; |
| 413 | note fails closed on OMPK v1, malformed header, or purity_flag != 1! |
| 414 | fn mm_admit_ompk_v2 plen ulong ck_pure_ok ulong to ulong |
| 415 | if lt plen 128 |
| 416 | ret MM_ERR_OMPK |
| 417 | end |
| 418 | if or ne ulong get mm_val_buf 0 79 ne ulong get mm_val_buf 1 77 |
| 419 | ret MM_ERR_OMPK |
| 420 | end |
| 421 | if or ne ulong get mm_val_buf 2 80 ne ulong get mm_val_buf 3 75 |
| 422 | ret MM_ERR_OMPK |
| 423 | end |
| 424 | ver = mm_val_u32 4 |
| 425 | if eq ver 1 |
| 426 | put mm_stats 5 add get mm_stats 5 1 |
| 427 | ret MM_ERR_IMPURE |
| 428 | end |
| 429 | if or ne ver 2 lt plen 160 |
| 430 | ret MM_ERR_OMPK |
| 431 | end |
| 432 | if ne mm_val_u32 132 844516176 |
| 433 | ret MM_ERR_OMPK |
| 434 | end |
| 435 | if ne ulong get mm_val_buf 131 0 |
| 436 | ret MM_ERR_OMPK |
| 437 | end |
| 438 | pur = ulong get mm_val_buf 128 |
| 439 | if or ne pur 1 ne ck_pure_ok 1 |
| 440 | put mm_stats 5 add get mm_stats 5 1 |
| 441 | ret MM_ERR_IMPURE |
| 442 | end |
| 443 | tier = ulong get mm_val_buf 129 |
| 444 | err_b = mm_val_u64 136 |
| 445 | if or lt tier 1 gt tier 2 |
| 446 | ret MM_ERR_CONTRACT |
| 447 | end |
| 448 | if and eq tier 1 ne err_b 0 |
| 449 | ret MM_ERR_CONTRACT |
| 450 | end |
| 451 | for i ulong 32 |
| 452 | put mm_req_ir_sha i get mm_val_buf add 8 i |
| 453 | put mm_req_fid i get mm_val_buf add 40 i |
| 454 | end |
| 455 | mm_set_contract pur ck_pure_ok tier err_b |
| 456 | put mm_req_contract 6 MM_PROV_OMPK_V2 |
| 457 | ret MM_OK |
| 458 | end |
| 459 | |
| 460 | note Core contract & purity admission gate (fail-closed) |
| 461 | fn mm_check_admission to ulong |
| 462 | pur = get mm_req_contract 0 |
| 463 | ckp = get mm_req_contract 1 |
| 464 | if or ne pur 1 ne ckp 1 |
| 465 | put mm_stats 5 add get mm_stats 5 1 |
| 466 | ret MM_ERR_IMPURE |
| 467 | end |
| 468 | tier = get mm_req_contract 2 |
| 469 | err_b = get mm_req_contract 3 |
| 470 | if or lt tier 1 gt tier 2 |
| 471 | ret MM_ERR_CONTRACT |
| 472 | end |
| 473 | if and eq tier 1 ne err_b 0 |
| 474 | ret MM_ERR_CONTRACT |
| 475 | end |
| 476 | ret MM_OK |
| 477 | end |
| 478 | |
| 479 | note One-way precision dominance relation: |
| 480 | note (1, 0) satisfies (1, 0) and (2, any) |
| 481 | note (2, e1) satisfies (2, e2) iff e1 <= e2 |
| 482 | note (2, any) NEVER satisfies (1, 0) |
| 483 | fn mm_dominates c_tier ulong c_err ulong r_tier ulong r_err ulong to bool |
| 484 | if or lt c_tier 1 gt c_tier 2 |
| 485 | ret false |
| 486 | end |
| 487 | if or lt r_tier 1 gt r_tier 2 |
| 488 | ret false |
| 489 | end |
| 490 | if and eq c_tier 1 ne c_err 0 |
| 491 | ret false |
| 492 | end |
| 493 | if and eq r_tier 1 ne r_err 0 |
| 494 | ret false |
| 495 | end |
| 496 | if eq r_tier 1 |
| 497 | ret eq c_tier 1 |
| 498 | end |
| 499 | if eq c_tier 1 |
| 500 | ret true |
| 501 | end |
| 502 | ret le c_err r_err |
| 503 | end |
| 504 | |
| 505 | fn mm_strictly_tighter a_tier ulong a_err ulong b_tier ulong b_err ulong to bool |
| 506 | if not mm_dominates a_tier a_err b_tier b_err |
| 507 | ret false |
| 508 | end |
| 509 | ret not mm_dominates b_tier b_err a_tier a_err |
| 510 | end |
| 511 | |
| 512 | note ---- CAS Key & Sharded Path Derivation (<root>/<2hex>/<64hex>.ome) ---- |
| 513 | |
| 514 | fn mm_derive_paths to ulong |
| 515 | rl = get mm_root_len 0 |
| 516 | if or eq rl 0 gt rl MM_MAX_ROOT_LEN |
| 517 | ret MM_ERR_NO_ROOT |
| 518 | end |
| 519 | tl = len mm_key_tag |
| 520 | for i ulong tl bound 32 |
| 521 | put sh_dat i get mm_key_tag i |
| 522 | end |
| 523 | put sh_dat tl 0 |
| 524 | o1 = add tl 1 |
| 525 | for i ulong 32 |
| 526 | put sh_dat add o1 i get mm_req_fid i |
| 527 | end |
| 528 | o2 = add o1 32 |
| 529 | for i ulong 32 |
| 530 | put sh_dat add o2 i get mm_req_in_sha i |
| 531 | end |
| 532 | sh_all add o2 32 |
| 533 | for i ulong 32 |
| 534 | b = get sh_dig i |
| 535 | put mm_key_dig i b |
| 536 | u = uint b |
| 537 | put mm_key_hex mul i 2 sh_hexd shr u 4 |
| 538 | put mm_key_hex add mul i 2 1 sh_hexd and u 15 |
| 539 | end |
| 540 | for i ulong rl bound 2048 |
| 541 | put mm_shard_path i get mm_root i |
| 542 | put mm_file_path i get mm_root i |
| 543 | end |
| 544 | put mm_shard_path rl 47 |
| 545 | put mm_shard_path add rl 1 get mm_key_hex 0 |
| 546 | put mm_shard_path add rl 2 get mm_key_hex 1 |
| 547 | put mm_shard_plen 0 add rl 3 |
| 548 | |
| 549 | put mm_file_path rl 47 |
| 550 | put mm_file_path add rl 1 get mm_key_hex 0 |
| 551 | put mm_file_path add rl 2 get mm_key_hex 1 |
| 552 | put mm_file_path add rl 3 47 |
| 553 | p0 = add rl 4 |
| 554 | for i ulong 64 |
| 555 | put mm_file_path add p0 i get mm_key_hex i |
| 556 | end |
| 557 | p1 = add p0 64 |
| 558 | for j ulong 4 |
| 559 | put mm_file_path add p1 j get mm_ext_ome j |
| 560 | end |
| 561 | put mm_file_plen 0 add p1 4 |
| 562 | ret MM_OK |
| 563 | end |
| 564 | |
| 565 | fn mm_tmp_push_num v ulong p ulong to ulong |
| 566 | x = v |
| 567 | q = ulong 24 |
| 568 | for i ulong 21 |
| 569 | q = sub q 1 |
| 570 | put mm_ndig q uchar add 48 rem x 10 |
| 571 | x = div x 10 |
| 572 | if eq x 0 |
| 573 | break |
| 574 | end |
| 575 | end |
| 576 | dc = sub 24 q |
| 577 | for j ulong dc bound 24 |
| 578 | put mm_tmp_path add p j get mm_ndig add q j |
| 579 | end |
| 580 | ret add p dc |
| 581 | end |
| 582 | |
| 583 | fn mm_build_tmp_path to void |
| 584 | sl = get mm_shard_plen 0 |
| 585 | for i ulong sl bound 2048 |
| 586 | put mm_tmp_path i get mm_shard_path i |
| 587 | end |
| 588 | put mm_tmp_path sl 47 |
| 589 | p = add sl 1 |
| 590 | for i ulong 16 |
| 591 | put mm_tmp_path add p i get mm_key_hex i |
| 592 | end |
| 593 | p = add p 16 |
| 594 | put mm_tmp_path p 46 |
| 595 | p = mm_tmp_push_num ulong getpid add p 1 |
| 596 | put mm_tmp_path p 46 |
| 597 | seq = add get mm_tmp_seq 0 1 |
| 598 | put mm_tmp_seq 0 seq |
| 599 | p = mm_tmp_push_num seq add p 1 |
| 600 | for j ulong 4 |
| 601 | put mm_tmp_path add p j get mm_ext_tmp j |
| 602 | end |
| 603 | put mm_tmp_plen 0 add p 4 |
| 604 | end |
| 605 | |
| 606 | note ---- Exact Read/Write Loops ---- |
| 607 | |
| 608 | fn mm_read_hdr fd int use_chk bool to ulong |
| 609 | off = ulong 0 |
| 610 | while lt off MM_HDR_LEN bound 512 |
| 611 | k = long 0 |
| 612 | if use_chk |
| 613 | k = read fd mm_chk_hdr off sub MM_HDR_LEN off |
| 614 | else |
| 615 | k = read fd mm_hdr off sub MM_HDR_LEN off |
| 616 | end |
| 617 | if lt k 0 |
| 618 | ret MM_ERR_IO |
| 619 | end |
| 620 | if eq k 0 |
| 621 | ret MM_ERR_TRUNCATED |
| 622 | end |
| 623 | off = add off ulong k |
| 624 | end |
| 625 | ret MM_OK |
| 626 | end |
| 627 | |
| 628 | fn mm_read_body_val fd int olen ulong to ulong |
| 629 | off = ulong 0 |
| 630 | while lt off olen bound 1048576 |
| 631 | want = sub olen off |
| 632 | if gt want 65536 |
| 633 | want = ulong 65536 |
| 634 | end |
| 635 | k = read fd mm_val_buf off want |
| 636 | if lt k 0 |
| 637 | ret MM_ERR_IO |
| 638 | end |
| 639 | if eq k 0 |
| 640 | ret MM_ERR_TRUNCATED |
| 641 | end |
| 642 | off = add off ulong k |
| 643 | end |
| 644 | ret MM_OK |
| 645 | end |
| 646 | |
| 647 | fn mm_read_body_sh fd int olen ulong to ulong |
| 648 | off = ulong 0 |
| 649 | while lt off olen bound 1048576 |
| 650 | want = sub olen off |
| 651 | if gt want 65536 |
| 652 | want = ulong 65536 |
| 653 | end |
| 654 | k = read fd sh_dat off want |
| 655 | if lt k 0 |
| 656 | ret MM_ERR_IO |
| 657 | end |
| 658 | if eq k 0 |
| 659 | ret MM_ERR_TRUNCATED |
| 660 | end |
| 661 | off = add off ulong k |
| 662 | end |
| 663 | ret MM_OK |
| 664 | end |
| 665 | |
| 666 | fn mm_check_eof fd int to ulong |
| 667 | k = read fd mm_probe_b 0 1 |
| 668 | if lt k 0 |
| 669 | ret MM_ERR_IO |
| 670 | end |
| 671 | if gt k 0 |
| 672 | ret MM_ERR_TRAILING |
| 673 | end |
| 674 | ret MM_OK |
| 675 | end |
| 676 | |
| 677 | fn mm_write_hdr fd int to ulong |
| 678 | off = ulong 0 |
| 679 | while lt off MM_HDR_LEN bound 512 |
| 680 | k = write fd mm_hdr off sub MM_HDR_LEN off |
| 681 | if le k 0 |
| 682 | ret MM_ERR_IO |
| 683 | end |
| 684 | off = add off ulong k |
| 685 | end |
| 686 | ret MM_OK |
| 687 | end |
| 688 | |
| 689 | fn mm_write_val fd int olen ulong to ulong |
| 690 | off = ulong 0 |
| 691 | while lt off olen bound 1048576 |
| 692 | want = sub olen off |
| 693 | if gt want 65536 |
| 694 | want = ulong 65536 |
| 695 | end |
| 696 | k = write fd mm_val_buf off want |
| 697 | if le k 0 |
| 698 | ret MM_ERR_IO |
| 699 | end |
| 700 | off = add off ulong k |
| 701 | end |
| 702 | ret MM_OK |
| 703 | end |
| 704 | |
| 705 | note ---- Fail-Closed Header & Digest Verification ---- |
| 706 | |
| 707 | fn mm_hdr_byte use_chk bool off ulong to uchar |
| 708 | if use_chk |
| 709 | ret get mm_chk_hdr off |
| 710 | end |
| 711 | ret get mm_hdr off |
| 712 | end |
| 713 | |
| 714 | fn mm_verify_hdr_fields use_chk bool to ulong |
| 715 | if or ne ulong mm_hdr_byte use_chk 0 MM_MAGIC_0 ne ulong mm_hdr_byte use_chk 1 MM_MAGIC_1 |
| 716 | ret MM_ERR_MAGIC |
| 717 | end |
| 718 | if or ne ulong mm_hdr_byte use_chk 2 MM_MAGIC_2 ne ulong mm_hdr_byte use_chk 3 MM_MAGIC_3 |
| 719 | ret MM_ERR_MAGIC |
| 720 | end |
| 721 | if ne mm_hdr_get_u32 use_chk 4 MM_VERSION |
| 722 | ret MM_ERR_VERSION |
| 723 | end |
| 724 | pur = ulong mm_hdr_byte use_chk 8 |
| 725 | if ne pur 1 |
| 726 | ret MM_ERR_IMPURE |
| 727 | end |
| 728 | tier = ulong mm_hdr_byte use_chk 9 |
| 729 | pflags = ulong mm_hdr_byte use_chk 10 |
| 730 | rsv0 = ulong mm_hdr_byte use_chk 11 |
| 731 | rsv1 = mm_hdr_get_u32 use_chk 12 |
| 732 | if or ne rsv0 0 ne rsv1 0 |
| 733 | ret MM_ERR_FORMAT |
| 734 | end |
| 735 | if or eq and pflags MM_PROV_CK_PURE 0 gt pflags 3 |
| 736 | ret MM_ERR_FORMAT |
| 737 | end |
| 738 | err_b = mm_hdr_get_u64 use_chk 16 |
| 739 | if or lt tier 1 gt tier 2 |
| 740 | ret MM_ERR_CONTRACT |
| 741 | end |
| 742 | if and eq tier 1 ne err_b 0 |
| 743 | ret MM_ERR_CONTRACT |
| 744 | end |
| 745 | for i ulong 32 |
| 746 | if ne mm_hdr_byte use_chk add 24 i get mm_req_fid i |
| 747 | ret MM_ERR_KEY_MISMATCH |
| 748 | end |
| 749 | if ne mm_hdr_byte use_chk add 56 i get mm_req_in_sha i |
| 750 | ret MM_ERR_KEY_MISMATCH |
| 751 | end |
| 752 | end |
| 753 | olen = mm_hdr_get_u64 use_chk 88 |
| 754 | if gt olen MM_MAX_VAL_LEN |
| 755 | ret MM_ERR_FORMAT |
| 756 | end |
| 757 | ret MM_OK |
| 758 | end |
| 759 | |
| 760 | fn mm_sh_eq_hdr use_chk bool off ulong to bool |
| 761 | diff = ulong 0 |
| 762 | for i ulong 32 |
| 763 | if ne get sh_dig i mm_hdr_byte use_chk add off i |
| 764 | diff = add diff 1 |
| 765 | end |
| 766 | end |
| 767 | ret eq diff 0 |
| 768 | end |
| 769 | |
| 770 | fn mm_verify_entry_digest use_chk bool to bool |
| 771 | for i ulong 192 |
| 772 | put sh_dat i mm_hdr_byte use_chk i |
| 773 | end |
| 774 | sh_all 192 |
| 775 | ret mm_sh_eq_hdr use_chk 192 |
| 776 | end |
| 777 | |
| 778 | note Read & cryptographically verify mm_file_path into mm_hdr + mm_val_buf |
| 779 | fn mm_read_and_verify to ulong |
| 780 | fd = fopen mm_file_path 0 get mm_file_plen 0 MM_O_RDONLY 0 |
| 781 | if lt fd 0 |
| 782 | if eq fd neg long 2 |
| 783 | ret MM_MISS |
| 784 | end |
| 785 | ret MM_ERR_IO |
| 786 | end |
| 787 | rc = mm_read_hdr int fd false |
| 788 | if ne rc MM_OK |
| 789 | cf = fclose int fd |
| 790 | ret rc |
| 791 | end |
| 792 | rc = mm_verify_hdr_fields false |
| 793 | if ne rc MM_OK |
| 794 | cf = fclose int fd |
| 795 | ret rc |
| 796 | end |
| 797 | olen = mm_hdr_get_u64 false 88 |
| 798 | rc = mm_read_body_val int fd olen |
| 799 | if ne rc MM_OK |
| 800 | cf = fclose int fd |
| 801 | ret rc |
| 802 | end |
| 803 | rc = mm_check_eof int fd |
| 804 | cf = fclose int fd |
| 805 | if ne rc MM_OK |
| 806 | ret rc |
| 807 | end |
| 808 | note Verify output_sha256 over output_bytes[0..olen) |
| 809 | for i ulong olen bound 4194304 |
| 810 | put sh_dat i get mm_val_buf i |
| 811 | end |
| 812 | sh_all olen |
| 813 | if not mm_sh_eq_hdr false 96 |
| 814 | put mm_stats 6 add get mm_stats 6 1 |
| 815 | put mm_val_len 0 0 |
| 816 | ret MM_ERR_TAMPER |
| 817 | end |
| 818 | note Verify entry_sha256 over header[0..192) (protects tier, err_bound, keys, provenance, output_sha256) |
| 819 | if not mm_verify_entry_digest false |
| 820 | put mm_stats 6 add get mm_stats 6 1 |
| 821 | put mm_val_len 0 0 |
| 822 | ret MM_ERR_TAMPER |
| 823 | end |
| 824 | put mm_val_len 0 olen |
| 825 | for i ulong 32 |
| 826 | put mm_val_sha i get mm_hdr add 96 i |
| 827 | put mm_hit_ir_sha i get mm_hdr add 160 i |
| 828 | put mm_hit_entry_sha i get mm_hdr add 192 i |
| 829 | end |
| 830 | put mm_hit_meta 0 ulong get mm_hdr 8 |
| 831 | put mm_hit_meta 1 ulong get mm_hdr 9 |
| 832 | put mm_hit_meta 2 mm_hdr_get_u64 false 16 |
| 833 | put mm_hit_meta 3 ulong get mm_hdr 10 |
| 834 | put mm_hit_meta 4 mm_hdr_get_u64 false 128 |
| 835 | put mm_hit_meta 5 mm_hdr_get_u64 false 136 |
| 836 | put mm_hit_meta 6 mm_hdr_get_u64 false 144 |
| 837 | put mm_hit_meta 7 mm_hdr_get_u64 false 152 |
| 838 | ret MM_OK |
| 839 | end |
| 840 | |
| 841 | note Peek & verify existing file on disk WITHOUT touching mm_val_buf (used by mm_store) |
| 842 | fn mm_peek_existing to ulong |
| 843 | fd = fopen mm_file_path 0 get mm_file_plen 0 MM_O_RDONLY 0 |
| 844 | if lt fd 0 |
| 845 | if eq fd neg long 2 |
| 846 | ret MM_MISS |
| 847 | end |
| 848 | ret MM_ERR_IO |
| 849 | end |
| 850 | rc = mm_read_hdr int fd true |
| 851 | if ne rc MM_OK |
| 852 | cf = fclose int fd |
| 853 | ret rc |
| 854 | end |
| 855 | rc = mm_verify_hdr_fields true |
| 856 | if ne rc MM_OK |
| 857 | cf = fclose int fd |
| 858 | ret rc |
| 859 | end |
| 860 | olen = mm_hdr_get_u64 true 88 |
| 861 | rc = mm_read_body_sh int fd olen |
| 862 | if ne rc MM_OK |
| 863 | cf = fclose int fd |
| 864 | ret rc |
| 865 | end |
| 866 | rc = mm_check_eof int fd |
| 867 | cf = fclose int fd |
| 868 | if ne rc MM_OK |
| 869 | ret rc |
| 870 | end |
| 871 | sh_all olen |
| 872 | if not mm_sh_eq_hdr true 96 |
| 873 | put mm_stats 6 add get mm_stats 6 1 |
| 874 | ret MM_ERR_TAMPER |
| 875 | end |
| 876 | if not mm_verify_entry_digest true |
| 877 | put mm_stats 6 add get mm_stats 6 1 |
| 878 | ret MM_ERR_TAMPER |
| 879 | end |
| 880 | ret MM_OK |
| 881 | end |
| 882 | |
| 883 | note ---- Public Lookup, Store, and Prune API ---- |
| 884 | |
| 885 | fn mm_sync_policy_sid to void |
| 886 | for i ulong 32 |
| 887 | put mp_q_sid i get mm_req_fid i |
| 888 | end |
| 889 | end |
| 890 | |
| 891 | fn mm_lookup to ulong |
| 892 | put mm_stats 0 add get mm_stats 0 1 |
| 893 | put mm_val_len 0 0 |
| 894 | arc = mm_check_admission |
| 895 | if ne arc MM_OK |
| 896 | ret arc |
| 897 | end |
| 898 | mm_sync_policy_sid |
| 899 | pdec = mp_precheck |
| 900 | if or eq pdec MP_DEC_SKIP_TRIVIAL eq pdec MP_DEC_DISABLED |
| 901 | put mm_stats 10 add get mm_stats 10 1 |
| 902 | ret MM_SKIP_UNPROFITABLE |
| 903 | end |
| 904 | drc = mm_derive_paths |
| 905 | if ne drc MM_OK |
| 906 | ret drc |
| 907 | end |
| 908 | rrc = mm_read_and_verify |
| 909 | if eq rrc MM_MISS |
| 910 | put mm_stats 3 add get mm_stats 3 1 |
| 911 | ret MM_MISS |
| 912 | end |
| 913 | if ne rrc MM_OK |
| 914 | ret rrc |
| 915 | end |
| 916 | c_tier = get mm_hit_meta 1 |
| 917 | c_err = get mm_hit_meta 2 |
| 918 | r_tier = get mm_req_contract 2 |
| 919 | r_err = get mm_req_contract 3 |
| 920 | if not mm_dominates c_tier c_err r_tier r_err |
| 921 | put mm_val_len 0 0 |
| 922 | put mm_stats 4 add get mm_stats 4 1 |
| 923 | ret MM_ERR_PRECISION_MISS |
| 924 | end |
| 925 | put mm_stats 1 add get mm_stats 1 1 |
| 926 | if mm_strictly_tighter c_tier c_err r_tier r_err |
| 927 | put mm_stats 2 add get mm_stats 2 1 |
| 928 | end |
| 929 | mp_record_hit |
| 930 | ret MM_OK |
| 931 | end |
| 932 | |
| 933 | fn mm_atomic_rename_tmp to ulong |
| 934 | tl = get mm_tmp_plen 0 |
| 935 | fl = get mm_file_plen 0 |
| 936 | tot = add add add 6 tl fl 0 |
| 937 | if gt tot 4096 |
| 938 | ret MM_ERR_IO |
| 939 | end |
| 940 | put mm_ren_pkt 0 2 |
| 941 | put mm_ren_pkt 1 0 |
| 942 | put mm_ren_pkt 2 uchar and tl 255 |
| 943 | put mm_ren_pkt 3 uchar and shr tl 8 255 |
| 944 | for i ulong tl bound 2048 |
| 945 | put mm_ren_pkt add 4 i get mm_tmp_path i |
| 946 | end |
| 947 | p = add 4 tl |
| 948 | put mm_ren_pkt p uchar and fl 255 |
| 949 | put mm_ren_pkt add p 1 uchar and shr fl 8 255 |
| 950 | p = add p 2 |
| 951 | for i ulong fl bound 2048 |
| 952 | put mm_ren_pkt add p i get mm_file_path i |
| 953 | end |
| 954 | p = add p fl |
| 955 | rr = renames mm_ren_pkt 0 p 0 |
| 956 | if ne rr 0 |
| 957 | u = unlink mm_tmp_path 0 tl |
| 958 | ret MM_ERR_IO |
| 959 | end |
| 960 | ret MM_OK |
| 961 | end |
| 962 | |
| 963 | fn mm_chk_sha_eq_existing to bool |
| 964 | for i ulong 32 |
| 965 | if ne get mm_chk_sha i get mm_chk_hdr add 96 i |
| 966 | ret false |
| 967 | end |
| 968 | end |
| 969 | ret true |
| 970 | end |
| 971 | |
| 972 | note Build canonical 224-byte OMEM v1 header into mm_hdr |
| 973 | fn mm_store_build_hdr n_tier ulong n_err ulong olen ulong compute_ns ulong to void |
| 974 | put mm_hdr 0 uchar MM_MAGIC_0 |
| 975 | put mm_hdr 1 uchar MM_MAGIC_1 |
| 976 | put mm_hdr 2 uchar MM_MAGIC_2 |
| 977 | put mm_hdr 3 uchar MM_MAGIC_3 |
| 978 | mm_hdr_put_u32 4 MM_VERSION |
| 979 | put mm_hdr 8 1 |
| 980 | put mm_hdr 9 uchar n_tier |
| 981 | pf = or MM_PROV_CK_PURE and get mm_req_contract 6 2 |
| 982 | put mm_hdr 10 uchar pf |
| 983 | put mm_hdr 11 0 |
| 984 | mm_hdr_put_u32 12 0 |
| 985 | mm_hdr_put_u64 16 n_err |
| 986 | for i ulong 32 |
| 987 | put mm_hdr add 24 i get mm_req_fid i |
| 988 | put mm_hdr add 56 i get mm_req_in_sha i |
| 989 | put mm_hdr add 96 i get mm_chk_sha i |
| 990 | put mm_hdr add 160 i get mm_req_ir_sha i |
| 991 | end |
| 992 | mm_hdr_put_u64 88 olen |
| 993 | rns = realns |
| 994 | now_ns = ulong 0 |
| 995 | if gt rns 0 |
| 996 | now_ns = ulong rns |
| 997 | end |
| 998 | mm_hdr_put_u64 128 now_ns |
| 999 | mm_hdr_put_u64 136 compute_ns |
| 1000 | mm_hdr_put_u64 144 ulong getpid |
| 1001 | mm_hdr_put_u64 152 get mm_req_contract 5 |
| 1002 | for i ulong 192 |
| 1003 | put sh_dat i get mm_hdr i |
| 1004 | end |
| 1005 | sh_all 192 |
| 1006 | for i ulong 32 |
| 1007 | put mm_hdr add 192 i get sh_dig i |
| 1008 | put mm_val_sha i get mm_chk_sha i |
| 1009 | end |
| 1010 | end |
| 1011 | |
| 1012 | fn mm_store_write_atomic olen ulong to ulong |
| 1013 | mm_build_tmp_path |
| 1014 | tfd = fopen mm_tmp_path 0 get mm_tmp_plen 0 MM_O_WRNEW 420 |
| 1015 | if lt tfd 0 |
| 1016 | ret MM_ERR_IO |
| 1017 | end |
| 1018 | rc = mm_write_hdr int tfd |
| 1019 | if ne rc MM_OK |
| 1020 | cf = fclose int tfd |
| 1021 | u = unlink mm_tmp_path 0 get mm_tmp_plen 0 |
| 1022 | ret rc |
| 1023 | end |
| 1024 | rc = mm_write_val int tfd olen |
| 1025 | cf = fclose int tfd |
| 1026 | if ne rc MM_OK |
| 1027 | u = unlink mm_tmp_path 0 get mm_tmp_plen 0 |
| 1028 | ret rc |
| 1029 | end |
| 1030 | ret mm_atomic_rename_tmp |
| 1031 | end |
| 1032 | |
| 1033 | note Returns: 100=write-new, 101=write-upgrade, MM_OK=idempotent, MM_OK_DOMINATED, or error |
| 1034 | fn mm_store_check_existing n_tier ulong n_err ulong to ulong |
| 1035 | prc = mm_peek_existing |
| 1036 | if eq prc MM_MISS |
| 1037 | ret 100 |
| 1038 | end |
| 1039 | if ne prc MM_OK |
| 1040 | ret prc |
| 1041 | end |
| 1042 | c_tier = ulong get mm_chk_hdr 9 |
| 1043 | c_err = mm_hdr_get_u64 true 16 |
| 1044 | if mm_strictly_tighter c_tier c_err n_tier n_err |
| 1045 | put mm_stats 9 add get mm_stats 9 1 |
| 1046 | ret MM_OK_DOMINATED |
| 1047 | end |
| 1048 | if and eq c_tier n_tier eq c_err n_err |
| 1049 | if and eq n_tier MM_TIER_EXACT not mm_chk_sha_eq_existing |
| 1050 | ret MM_ERR_NONDET |
| 1051 | end |
| 1052 | ret MM_OK |
| 1053 | end |
| 1054 | ret 101 |
| 1055 | end |
| 1056 | |
| 1057 | fn mm_store compute_ns ulong overhead_ns ulong to ulong |
| 1058 | rc = mm_check_admission |
| 1059 | if ne rc MM_OK |
| 1060 | ret rc |
| 1061 | end |
| 1062 | olen = get mm_val_len 0 |
| 1063 | if gt olen MM_MAX_VAL_LEN |
| 1064 | ret MM_ERR_FORMAT |
| 1065 | end |
| 1066 | mm_sync_policy_sid |
| 1067 | rc = mp_should_memoize compute_ns overhead_ns |
| 1068 | if or eq rc MP_DEC_SKIP_TRIVIAL eq rc MP_DEC_DISABLED |
| 1069 | put mm_stats 10 add get mm_stats 10 1 |
| 1070 | ret MM_SKIP_UNPROFITABLE |
| 1071 | end |
| 1072 | rc = mm_derive_paths |
| 1073 | if ne rc MM_OK |
| 1074 | ret rc |
| 1075 | end |
| 1076 | rc = mm_mkdir_p |
| 1077 | if ne rc MM_OK |
| 1078 | ret rc |
| 1079 | end |
| 1080 | sr = makedir mm_shard_path 0 get mm_shard_plen 0 493 |
| 1081 | if and ne sr 0 ne sr neg long 17 |
| 1082 | ret MM_ERR_IO |
| 1083 | end |
| 1084 | for i ulong olen bound 4194304 |
| 1085 | put sh_dat i get mm_val_buf i |
| 1086 | end |
| 1087 | sh_all olen |
| 1088 | for i ulong 32 |
| 1089 | put mm_chk_sha i get sh_dig i |
| 1090 | end |
| 1091 | n_tier = get mm_req_contract 2 |
| 1092 | n_err = get mm_req_contract 3 |
| 1093 | rc = mm_store_check_existing n_tier n_err |
| 1094 | if and ne rc 100 ne rc 101 |
| 1095 | ret rc |
| 1096 | end |
| 1097 | is_upg = eq rc 101 |
| 1098 | mm_store_build_hdr n_tier n_err olen compute_ns |
| 1099 | rc = mm_store_write_atomic olen |
| 1100 | if ne rc MM_OK |
| 1101 | ret rc |
| 1102 | end |
| 1103 | put mm_stats 7 add get mm_stats 7 1 |
| 1104 | if is_upg |
| 1105 | put mm_stats 8 add get mm_stats 8 1 |
| 1106 | end |
| 1107 | mm_sync_policy_root |
| 1108 | if eq mp_check_disk_floor MP_ALARM_DISK_FLOOR |
| 1109 | put mm_stats 11 add get mm_stats 11 1 |
| 1110 | ret MM_OK_ALARM |
| 1111 | end |
| 1112 | ret MM_OK |
| 1113 | end |
| 1114 | |
| 1115 | fn mm_prune target_bytes ulong to ulong |
| 1116 | mm_sync_policy_root |
| 1117 | prc = mp_memo_prune target_bytes |
| 1118 | if ne prc MP_DISK_OK |
| 1119 | ret MM_ERR_IO |
| 1120 | end |
| 1121 | ret MM_OK |
| 1122 | end |