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 / aienux / store / store_engine.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note aienux/store/store_engine.om: Pure-Omega Store v1 mount, transaction, and disk geometry engine. |
| 3 | note Faithful OM-2 library (no om2 line, no fn main) converted from upstream aienux |
| 4 | note native/store/store_engine.{h,c} and native/store/store_disk.c. |
| 5 | note Compose after: si/directives/om2.om aienux/store/store_v1.om aienux/store/store_engine.om ... om/tls/sha2.om |
| 6 | |
| 7 | note MountState |
| 8 | const ST_VALID int 0 |
| 9 | const ST_DEGRADED_RECOVERY int 1 |
| 10 | |
| 11 | note PeerCondition |
| 12 | const ST_PEER_ZERO int 0 |
| 13 | const ST_PEER_VALID int 1 |
| 14 | const ST_PEER_MALFORMED int 2 |
| 15 | const ST_PEER_GRAPH_BAD_NEWER int 3 |
| 16 | const ST_PEER_GRAPH_BAD_OLDER int 4 |
| 17 | |
| 18 | note MountError |
| 19 | const ST_M_IO int -101 |
| 20 | const ST_M_UNFORMATTED int -102 |
| 21 | const ST_M_FOREIGN int -103 |
| 22 | const ST_M_UNSUPPORTED_VERSION int -104 |
| 23 | const ST_M_CORRUPT_RECOVERY_REQUIRED int -105 |
| 24 | const ST_M_CONFLICTING_ROOTS int -106 |
| 25 | const ST_M_INCONSISTENT_HISTORY int -107 |
| 26 | |
| 27 | note StoreError |
| 28 | const ST_E_IO int -201 |
| 29 | const ST_E_CORRUPT int -202 |
| 30 | const ST_E_READ_ONLY_DEGRADED int -203 |
| 31 | const ST_E_DUPLICATE_OBJECT_CONFLICT int -204 |
| 32 | const ST_E_NO_SPACE int -205 |
| 33 | const ST_E_CATALOG_FULL int -206 |
| 34 | const ST_E_GENERATION_EXHAUSTED int -207 |
| 35 | const ST_E_TRANSACTION_LIMIT int -208 |
| 36 | const ST_E_INVALID_OBJECT int -209 |
| 37 | const ST_E_NEEDS_REOPEN int -210 |
| 38 | const ST_E_ARG int -211 |
| 39 | const ST_E_GEOMETRY int -212 |
| 40 | |
| 41 | note Internal root-validation results |
| 42 | const ST_RE_OK int 0 |
| 43 | const ST_RE_IO int 1 |
| 44 | const ST_RE_GRAPH int 2 |
| 45 | |
| 46 | note Media and workspace capacities |
| 47 | const ST_MEDIA_UNITS ulong 256 |
| 48 | const ST_MEDIA_BYTES ulong 1048576 |
| 49 | const ST_CAT_CAP ulong 512 |
| 50 | const ST_CAT_BYTES ulong 32768 |
| 51 | const ST_CATBUF_BYTES ulong 36864 |
| 52 | const ST_TX_MAX_OBJS ulong 16 |
| 53 | const ST_TX_BUF_BYTES ulong 262144 |
| 54 | |
| 55 | note Engine metadata indices (global st_meta ulong 16) |
| 56 | const ST_M_BLKSIZE ulong 0 |
| 57 | const ST_M_BLKCNT ulong 1 |
| 58 | const ST_M_BASE_LBA ulong 2 |
| 59 | const ST_M_BPU ulong 3 |
| 60 | const ST_M_REG_UNITS ulong 4 |
| 61 | const ST_M_STATE ulong 5 |
| 62 | const ST_M_PEER ulong 6 |
| 63 | const ST_M_POISONED ulong 7 |
| 64 | const ST_M_CAT_IDX ulong 8 |
| 65 | const ST_M_CAT_N ulong 9 |
| 66 | const ST_M_ACTIVE_SLOT ulong 10 |
| 67 | const ST_M_GEN ulong 11 |
| 68 | const ST_M_HW ulong 12 |
| 69 | const ST_M_FAULT_WRITE ulong 13 |
| 70 | const ST_M_FAULT_FLUSH ulong 14 |
| 71 | const ST_M_WRITE_CNT ulong 15 |
| 72 | |
| 73 | note Global media and engine state |
| 74 | global st_meta ulong 16 |
| 75 | global st_media uchar 1048576 |
| 76 | global st_root_sb uchar 4096 |
| 77 | global st_root_cmt uchar 200 |
| 78 | global st_root_cid uchar 32 |
| 79 | global st_root_uuid uchar 16 |
| 80 | |
| 81 | note Two catalog tables (active + spare, up to 512 entries * 64 bytes each) |
| 82 | global st_cat0 uchar 32768 |
| 83 | global st_cat1 uchar 32768 |
| 84 | global st_catbuf uchar 36864 |
| 85 | |
| 86 | note Scratch units and root-open temporaries |
| 87 | global st_raw0 uchar 4096 |
| 88 | global st_raw1 uchar 4096 |
| 89 | global st_unit uchar 4096 |
| 90 | global st_plan_cmt uchar 4096 |
| 91 | global st_plan_sb uchar 4096 |
| 92 | global st_open_cmt0 uchar 200 |
| 93 | global st_open_cmt1 uchar 200 |
| 94 | global st_open_cid0 uchar 32 |
| 95 | global st_open_cid1 uchar 32 |
| 96 | global st_open_n ulong 2 |
| 97 | |
| 98 | note Extent scratch for overlap & append-transition checks |
| 99 | global st_ext_f ulong 512 |
| 100 | global st_ext_e ulong 512 |
| 101 | |
| 102 | note Transaction staging buffers (up to 16 objects per transaction) |
| 103 | global st_tx_n ulong 1 |
| 104 | global st_tx_pos ulong 1 |
| 105 | global st_tx_buf uchar 262144 |
| 106 | global st_tx_off ulong 16 |
| 107 | global st_tx_len ulong 16 |
| 108 | global st_tx_kind ushort 16 |
| 109 | global st_tx_ver ushort 16 |
| 110 | global st_tx_id uchar 512 |
| 111 | global st_tx_units uint 16 |
| 112 | global st_tx_first ulong 16 |
| 113 | global st_tx_append uchar 16 |
| 114 | |
| 115 | note ---- Disk geometry adapter (store_disk.c) ---- |
| 116 | |
| 117 | fn st_media_zero to void |
| 118 | for i ulong ST_MEDIA_BYTES |
| 119 | put st_media i 0 |
| 120 | end |
| 121 | for i ulong 16 |
| 122 | put st_meta i 0 |
| 123 | end |
| 124 | put st_meta ST_M_POISONED 1 |
| 125 | end |
| 126 | |
| 127 | fn st_set_faults fail_write ulong fail_flush ulong to void |
| 128 | put st_meta ST_M_FAULT_WRITE fail_write |
| 129 | put st_meta ST_M_FAULT_FLUSH fail_flush |
| 130 | put st_meta ST_M_WRITE_CNT 0 |
| 131 | end |
| 132 | |
| 133 | fn st_disk_bind blk_size ulong blk_cnt ulong base_lba ulong req_units ulong to int |
| 134 | bpu = ulong 0 |
| 135 | if eq blk_size 512 |
| 136 | bpu = 8 |
| 137 | else |
| 138 | if eq blk_size 4096 |
| 139 | bpu = 1 |
| 140 | else |
| 141 | ret ST_E_GEOMETRY |
| 142 | end |
| 143 | end |
| 144 | if ne rem base_lba bpu 0 |
| 145 | ret ST_E_GEOMETRY |
| 146 | end |
| 147 | if ge base_lba blk_cnt |
| 148 | ret ST_E_GEOMETRY |
| 149 | end |
| 150 | max_u = div sub blk_cnt base_lba bpu |
| 151 | ru = req_units |
| 152 | if eq ru 0 |
| 153 | ru = max_u |
| 154 | end |
| 155 | if or gt ru max_u gt mul add div base_lba bpu ru SV1_UNIT ST_MEDIA_BYTES |
| 156 | ret ST_E_GEOMETRY |
| 157 | end |
| 158 | put st_meta ST_M_BLKSIZE blk_size |
| 159 | put st_meta ST_M_BLKCNT blk_cnt |
| 160 | put st_meta ST_M_BASE_LBA base_lba |
| 161 | put st_meta ST_M_BPU bpu |
| 162 | put st_meta ST_M_REG_UNITS ru |
| 163 | ret SV1_OK |
| 164 | end |
| 165 | |
| 166 | fn st_dev_read unit ulong dst edit uchar 4096 to int |
| 167 | if ge unit get st_meta ST_M_REG_UNITS |
| 168 | ret ST_E_IO |
| 169 | end |
| 170 | bpu = get st_meta ST_M_BPU |
| 171 | bsize = get st_meta ST_M_BLKSIZE |
| 172 | lba = add get st_meta ST_M_BASE_LBA mul unit bpu |
| 173 | boff = mul lba bsize |
| 174 | if gt add boff SV1_UNIT ST_MEDIA_BYTES |
| 175 | ret ST_E_IO |
| 176 | end |
| 177 | for i ulong 4096 |
| 178 | put dst i get st_media add boff i |
| 179 | end |
| 180 | ret SV1_OK |
| 181 | end |
| 182 | |
| 183 | fn st_dev_write unit ulong src view uchar 4096 to int |
| 184 | if ge unit get st_meta ST_M_REG_UNITS |
| 185 | ret ST_E_IO |
| 186 | end |
| 187 | fw = get st_meta ST_M_FAULT_WRITE |
| 188 | wcnt = add get st_meta ST_M_WRITE_CNT 1 |
| 189 | put st_meta ST_M_WRITE_CNT wcnt |
| 190 | if and ne fw 0 eq wcnt fw |
| 191 | ret ST_E_IO |
| 192 | end |
| 193 | bpu = get st_meta ST_M_BPU |
| 194 | bsize = get st_meta ST_M_BLKSIZE |
| 195 | lba = add get st_meta ST_M_BASE_LBA mul unit bpu |
| 196 | boff = mul lba bsize |
| 197 | if gt add boff SV1_UNIT ST_MEDIA_BYTES |
| 198 | ret ST_E_IO |
| 199 | end |
| 200 | for i ulong 4096 |
| 201 | put st_media add boff i get src i |
| 202 | end |
| 203 | ret SV1_OK |
| 204 | end |
| 205 | |
| 206 | fn st_dev_flush to int |
| 207 | if ne get st_meta ST_M_FAULT_FLUSH 0 |
| 208 | ret ST_E_IO |
| 209 | end |
| 210 | ret SV1_OK |
| 211 | end |
| 212 | |
| 213 | fn st_media_poke unit ulong off ulong val uchar to void |
| 214 | bpu = get st_meta ST_M_BPU |
| 215 | bsize = get st_meta ST_M_BLKSIZE |
| 216 | lba = add get st_meta ST_M_BASE_LBA mul unit bpu |
| 217 | boff = add mul lba bsize off |
| 218 | if lt boff ST_MEDIA_BYTES |
| 219 | put st_media boff val |
| 220 | end |
| 221 | end |
| 222 | |
| 223 | note ---- Catalog table accessors (cat_idx 0 -> st_cat0, 1 -> st_cat1) ---- |
| 224 | |
| 225 | fn st_cat_get_entry cidx ulong idx ulong dst edit uchar 64 to void |
| 226 | base = mul idx SV1_CATALOG_ENTRY |
| 227 | if eq cidx 0 |
| 228 | for i ulong 64 |
| 229 | put dst i get st_cat0 add base i |
| 230 | end |
| 231 | else |
| 232 | for i ulong 64 |
| 233 | put dst i get st_cat1 add base i |
| 234 | end |
| 235 | end |
| 236 | end |
| 237 | |
| 238 | fn st_cat_put_entry cidx ulong idx ulong src view uchar 64 to void |
| 239 | base = mul idx SV1_CATALOG_ENTRY |
| 240 | if eq cidx 0 |
| 241 | for i ulong 64 |
| 242 | put st_cat0 add base i get src i |
| 243 | end |
| 244 | else |
| 245 | for i ulong 64 |
| 246 | put st_cat1 add base i get src i |
| 247 | end |
| 248 | end |
| 249 | end |
| 250 | |
| 251 | fn st_cat_cmp_idx cidx ulong a ulong b ulong to int |
| 252 | ea = new uchar 64 0 |
| 253 | eb = new uchar 64 0 |
| 254 | st_cat_get_entry cidx a ea |
| 255 | st_cat_get_entry cidx b eb |
| 256 | ret sv1_cmp_id ea 0 eb 0 |
| 257 | end |
| 258 | |
| 259 | fn st_cat_swap cidx ulong a ulong b ulong to void |
| 260 | ea = new uchar 64 0 |
| 261 | eb = new uchar 64 0 |
| 262 | st_cat_get_entry cidx a ea |
| 263 | st_cat_get_entry cidx b eb |
| 264 | st_cat_put_entry cidx a eb |
| 265 | st_cat_put_entry cidx b ea |
| 266 | end |
| 267 | |
| 268 | fn st_cat_check_order cidx ulong n ulong to int |
| 269 | if lt n 2 |
| 270 | ret SV1_OK |
| 271 | end |
| 272 | ea = new uchar 64 0 |
| 273 | eb = new uchar 64 0 |
| 274 | i = ulong 0 |
| 275 | lim = sub n 1 |
| 276 | while lt i lim bound 512 |
| 277 | st_cat_get_entry cidx i ea |
| 278 | st_cat_get_entry cidx add i 1 eb |
| 279 | c = sv1_cmp_id ea 0 eb 0 |
| 280 | if eq c 0 |
| 281 | ret SV1_E_DUPLICATE_OBJECT |
| 282 | end |
| 283 | if gt c 0 |
| 284 | ret SV1_E_CATALOG_ORDER |
| 285 | end |
| 286 | i = add i 1 |
| 287 | end |
| 288 | ret SV1_OK |
| 289 | end |
| 290 | |
| 291 | fn st_cat_find cidx ulong n ulong id view uchar * to int |
| 292 | lo = ulong 0 |
| 293 | hi = n |
| 294 | ent = new uchar 64 0 |
| 295 | while lt lo hi bound 32 |
| 296 | mid = add lo div sub hi lo 2 |
| 297 | st_cat_get_entry cidx mid ent |
| 298 | c = sv1_cmp_id ent 0 id 0 |
| 299 | if eq c 0 |
| 300 | ret int mid |
| 301 | end |
| 302 | if lt c 0 |
| 303 | lo = add mid 1 |
| 304 | else |
| 305 | hi = mid |
| 306 | end |
| 307 | end |
| 308 | ret -1 |
| 309 | end |
| 310 | |
| 311 | fn st_cat_sift cidx ulong start ulong n ulong to void |
| 312 | root = start |
| 313 | while true bound 32 |
| 314 | ch = add mul 2 root 1 |
| 315 | if ge ch n |
| 316 | ret |
| 317 | end |
| 318 | if lt add ch 1 n |
| 319 | if lt st_cat_cmp_idx cidx ch add ch 1 0 |
| 320 | ch = add ch 1 |
| 321 | end |
| 322 | end |
| 323 | if ge st_cat_cmp_idx cidx root ch 0 |
| 324 | ret |
| 325 | end |
| 326 | st_cat_swap cidx root ch |
| 327 | root = ch |
| 328 | end |
| 329 | end |
| 330 | |
| 331 | fn st_cat_sort cidx ulong n ulong to void |
| 332 | if lt n 2 |
| 333 | ret |
| 334 | end |
| 335 | i = div n 2 |
| 336 | while gt i 0 bound 512 |
| 337 | i = sub i 1 |
| 338 | st_cat_sift cidx i n |
| 339 | end |
| 340 | en = sub n 1 |
| 341 | while gt en 0 bound 512 |
| 342 | st_cat_swap cidx 0 en |
| 343 | st_cat_sift cidx 0 en |
| 344 | en = sub en 1 |
| 345 | end |
| 346 | end |
| 347 | |
| 348 | note ---- Extent heapsort (st_ext_f / st_ext_e) ---- |
| 349 | |
| 350 | fn st_ext_less a ulong b ulong to bool |
| 351 | fa = get st_ext_f a |
| 352 | fb = get st_ext_f b |
| 353 | if lt fa fb |
| 354 | ret true |
| 355 | end |
| 356 | if eq fa fb |
| 357 | ret lt get st_ext_e a get st_ext_e b |
| 358 | end |
| 359 | ret false |
| 360 | end |
| 361 | |
| 362 | fn st_ext_swap a ulong b ulong to void |
| 363 | tf = get st_ext_f a |
| 364 | te = get st_ext_e a |
| 365 | put st_ext_f a get st_ext_f b |
| 366 | put st_ext_e a get st_ext_e b |
| 367 | put st_ext_f b tf |
| 368 | put st_ext_e b te |
| 369 | end |
| 370 | |
| 371 | fn st_ext_sift start ulong n ulong to void |
| 372 | root = start |
| 373 | while true bound 32 |
| 374 | ch = add mul 2 root 1 |
| 375 | if ge ch n |
| 376 | ret |
| 377 | end |
| 378 | if lt add ch 1 n |
| 379 | if st_ext_less ch add ch 1 |
| 380 | ch = add ch 1 |
| 381 | end |
| 382 | end |
| 383 | if not st_ext_less root ch |
| 384 | ret |
| 385 | end |
| 386 | st_ext_swap root ch |
| 387 | root = ch |
| 388 | end |
| 389 | end |
| 390 | |
| 391 | fn st_ext_sort n ulong to void |
| 392 | if lt n 2 |
| 393 | ret |
| 394 | end |
| 395 | i = div n 2 |
| 396 | while gt i 0 bound 512 |
| 397 | i = sub i 1 |
| 398 | st_ext_sift i n |
| 399 | end |
| 400 | en = sub n 1 |
| 401 | while gt en 0 bound 512 |
| 402 | st_ext_swap 0 en |
| 403 | st_ext_sift 0 en |
| 404 | en = sub en 1 |
| 405 | end |
| 406 | end |
| 407 | |
| 408 | note ---- Catalog encode / decode / validate ---- |
| 409 | |
| 410 | fn st_cat_encode_to_buf cidx ulong n ulong out_len edit ulong 1 to int |
| 411 | if gt n ST_CAT_CAP |
| 412 | ret SV1_E_CATALOG_TOO_LARGE |
| 413 | end |
| 414 | r = st_cat_check_order cidx n |
| 415 | if ne r SV1_OK |
| 416 | ret r |
| 417 | end |
| 418 | clen = sv1_catalog_len n |
| 419 | if gt clen ST_CATBUF_BYTES |
| 420 | ret SV1_E_ARG |
| 421 | end |
| 422 | hdr = new uchar 16 0 |
| 423 | for i ulong 8 |
| 424 | put hdr i get sv1_cat_magic i |
| 425 | end |
| 426 | sv1_put16 hdr 8 1 |
| 427 | sv1_put16 hdr 10 ushort SV1_CATALOG_ENTRY |
| 428 | sv1_put32 hdr 12 uint n |
| 429 | for i ulong 16 |
| 430 | put st_catbuf i get hdr i |
| 431 | end |
| 432 | ent = new uchar 64 0 |
| 433 | i = ulong 0 |
| 434 | while lt i n bound 512 |
| 435 | st_cat_get_entry cidx i ent |
| 436 | r = sv1_entry_validate_shape ent 0 |
| 437 | if ne r SV1_OK |
| 438 | ret r |
| 439 | end |
| 440 | off = add SV1_CATALOG_HEADER mul i SV1_CATALOG_ENTRY |
| 441 | for j ulong 64 |
| 442 | put st_catbuf add off j get ent j |
| 443 | end |
| 444 | i = add i 1 |
| 445 | end |
| 446 | put out_len 0 clen |
| 447 | ret SV1_OK |
| 448 | end |
| 449 | |
| 450 | fn st_cat_decode_from_buf blen ulong cidx ulong n_out edit ulong 1 to int |
| 451 | if lt blen SV1_CATALOG_HEADER |
| 452 | ret SV1_E_INVALID_LENGTH |
| 453 | end |
| 454 | hdr = new uchar 16 0 |
| 455 | for i ulong 16 |
| 456 | put hdr i get st_catbuf i |
| 457 | end |
| 458 | if not sv1_equal part hdr 0 8 sv1_cat_magic |
| 459 | ret SV1_E_BAD_CATALOG_MAGIC |
| 460 | end |
| 461 | if or ne sv1_get16 hdr 8 1 ne sv1_get16 hdr 10 ushort SV1_CATALOG_ENTRY |
| 462 | ret SV1_E_UNSUPPORTED_VERSION |
| 463 | end |
| 464 | cnt = ulong sv1_get32 hdr 12 |
| 465 | if gt cnt SV1_MAX_CATALOG_ENTRIES |
| 466 | ret SV1_E_CATALOG_TOO_LARGE |
| 467 | end |
| 468 | if ne blen sv1_catalog_len cnt |
| 469 | ret SV1_E_INVALID_LENGTH |
| 470 | end |
| 471 | if gt cnt ST_CAT_CAP |
| 472 | ret SV1_E_ARG |
| 473 | end |
| 474 | ent = new uchar 64 0 |
| 475 | i = ulong 0 |
| 476 | while lt i cnt bound 512 |
| 477 | off = add SV1_CATALOG_HEADER mul i SV1_CATALOG_ENTRY |
| 478 | for j ulong 64 |
| 479 | put ent j get st_catbuf add off j |
| 480 | end |
| 481 | r = sv1_entry_validate_shape ent 0 |
| 482 | if ne r SV1_OK |
| 483 | ret r |
| 484 | end |
| 485 | st_cat_put_entry cidx i ent |
| 486 | i = add i 1 |
| 487 | end |
| 488 | r = st_cat_check_order cidx cnt |
| 489 | if ne r SV1_OK |
| 490 | ret r |
| 491 | end |
| 492 | put n_out 0 cnt |
| 493 | ret SV1_OK |
| 494 | end |
| 495 | |
| 496 | fn st_cat_validate_extents cidx ulong n ulong reg_u ulong cat_fu ulong hw ulong to int |
| 497 | if or lt reg_u 4 gt reg_u SV1_MAX_REGION_UNITS |
| 498 | ret SV1_E_OUT_OF_BOUNDS |
| 499 | end |
| 500 | if or lt hw 4 gt hw reg_u |
| 501 | ret SV1_E_OUT_OF_BOUNDS |
| 502 | end |
| 503 | if or lt cat_fu 2 ge cat_fu reg_u |
| 504 | ret SV1_E_OUT_OF_BOUNDS |
| 505 | end |
| 506 | ent = new uchar 64 0 |
| 507 | i = ulong 0 |
| 508 | while lt i n bound 512 |
| 509 | st_cat_get_entry cidx i ent |
| 510 | r = sv1_entry_validate_bounds ent 0 reg_u cat_fu hw |
| 511 | if ne r SV1_OK |
| 512 | ret r |
| 513 | end |
| 514 | fu = sv1_get64 ent 36 |
| 515 | uc = ulong sv1_get32 ent 52 |
| 516 | put st_ext_f i fu |
| 517 | put st_ext_e i add fu uc |
| 518 | i = add i 1 |
| 519 | end |
| 520 | st_ext_sort n |
| 521 | if ge n 2 |
| 522 | lim = sub n 1 |
| 523 | i = ulong 0 |
| 524 | while lt i lim bound 512 |
| 525 | if gt get st_ext_e i get st_ext_f add i 1 |
| 526 | ret SV1_E_OVERLAP |
| 527 | end |
| 528 | i = add i 1 |
| 529 | end |
| 530 | end |
| 531 | ret SV1_OK |
| 532 | end |
| 533 | |
| 534 | note ---- Streamed object verification & reading ---- |
| 535 | |
| 536 | fn st_hash_unit_step ctx edit ulong 40 ubuf view uchar 4096 start ulong blen ulong to bool |
| 537 | take = ulong 0 |
| 538 | if gt blen start |
| 539 | take = sub blen start |
| 540 | end |
| 541 | if gt take SV1_UNIT |
| 542 | take = SV1_UNIT |
| 543 | end |
| 544 | sha256_update ctx part ubuf 0 take |
| 545 | ret sv1_all_zero ubuf take sub SV1_UNIT take |
| 546 | end |
| 547 | |
| 548 | fn st_verify_digest ctx edit ulong 40 ent view uchar 64 to int |
| 549 | got = new uchar 32 0 |
| 550 | sha256_final ctx got |
| 551 | if not sv1_equal got part ent 0 32 |
| 552 | ret ST_RE_GRAPH |
| 553 | end |
| 554 | ret ST_RE_OK |
| 555 | end |
| 556 | |
| 557 | fn st_verify_app_object ent view uchar 64 to int |
| 558 | kind = sv1_get16 ent 32 |
| 559 | ver = sv1_get16 ent 34 |
| 560 | fu = sv1_get64 ent 36 |
| 561 | blen = sv1_get64 ent 44 |
| 562 | ucnt = sv1_get32 ent 52 |
| 563 | ctx = new ulong 40 0 |
| 564 | graph = false |
| 565 | u = new uint 1 0 |
| 566 | if or ne sv1_unit_count blen u SV1_OK ne get u 0 ucnt |
| 567 | graph = true |
| 568 | end |
| 569 | if not graph |
| 570 | if ne sv1_object_id_begin ctx kind ver blen SV1_OK |
| 571 | graph = true |
| 572 | end |
| 573 | end |
| 574 | ubuf = new uchar 4096 0 |
| 575 | i = ulong 0 |
| 576 | ulim = ulong ucnt |
| 577 | while lt i ulim bound 256 |
| 578 | if ne st_dev_read add fu i ubuf SV1_OK |
| 579 | ret ST_RE_IO |
| 580 | end |
| 581 | if not graph |
| 582 | if not st_hash_unit_step ctx ubuf mul i SV1_UNIT blen |
| 583 | graph = true |
| 584 | end |
| 585 | end |
| 586 | i = add i 1 |
| 587 | end |
| 588 | if graph |
| 589 | ret ST_RE_GRAPH |
| 590 | end |
| 591 | ret st_verify_digest ctx ent |
| 592 | end |
| 593 | |
| 594 | fn st_load_cat_units cat_fu ulong cat_uc ulong cat_bl ulong to int |
| 595 | if gt mul cat_uc SV1_UNIT ST_CATBUF_BYTES |
| 596 | ret ST_RE_GRAPH |
| 597 | end |
| 598 | ubuf = new uchar 4096 0 |
| 599 | i = ulong 0 |
| 600 | while lt i cat_uc bound 65 |
| 601 | if ne st_dev_read add cat_fu i ubuf SV1_OK |
| 602 | ret ST_RE_IO |
| 603 | end |
| 604 | off = mul i SV1_UNIT |
| 605 | for j ulong 4096 |
| 606 | put st_catbuf add off j get ubuf j |
| 607 | end |
| 608 | i = add i 1 |
| 609 | end |
| 610 | p_start = cat_bl |
| 611 | p_end = mul cat_uc SV1_UNIT |
| 612 | while lt p_start p_end bound 36864 |
| 613 | if ne get st_catbuf p_start 0 |
| 614 | ret ST_RE_GRAPH |
| 615 | end |
| 616 | p_start = add p_start 1 |
| 617 | end |
| 618 | ret ST_RE_OK |
| 619 | end |
| 620 | |
| 621 | fn st_hash_catbuf cat_bl ulong digest_out edit uchar 32 to int |
| 622 | ctx = new ulong 40 0 |
| 623 | if ne sv1_object_id_begin ctx SV1_KIND_CATALOG SV1_VERSION cat_bl SV1_OK |
| 624 | ret ST_RE_GRAPH |
| 625 | end |
| 626 | ubuf = new uchar 4096 0 |
| 627 | pos = ulong 0 |
| 628 | while lt pos cat_bl bound 65 |
| 629 | take = sub cat_bl pos |
| 630 | if gt take SV1_UNIT |
| 631 | take = SV1_UNIT |
| 632 | end |
| 633 | for j ulong 4096 |
| 634 | if lt j take |
| 635 | put ubuf j get st_catbuf add pos j |
| 636 | end |
| 637 | end |
| 638 | sha256_update ctx part ubuf 0 take |
| 639 | pos = add pos take |
| 640 | end |
| 641 | sha256_final ctx digest_out |
| 642 | ret ST_RE_OK |
| 643 | end |
| 644 | |
| 645 | fn st_validate_root_cat cmt_out view uchar 200 cidx ulong n_out edit ulong 1 to int |
| 646 | cat_fu = sv1_get64 cmt_out 168 |
| 647 | cat_bl = sv1_get64 cmt_out 176 |
| 648 | cat_uc = ulong sv1_get32 cmt_out 184 |
| 649 | lr = st_load_cat_units cat_fu cat_uc cat_bl |
| 650 | if ne lr ST_RE_OK |
| 651 | ret lr |
| 652 | end |
| 653 | got_cat_id = new uchar 32 0 |
| 654 | hr = st_hash_catbuf cat_bl got_cat_id |
| 655 | if ne hr ST_RE_OK |
| 656 | ret hr |
| 657 | end |
| 658 | if not sv1_equal got_cat_id part cmt_out 136 32 |
| 659 | ret ST_RE_GRAPH |
| 660 | end |
| 661 | if ne st_cat_decode_from_buf cat_bl cidx n_out SV1_OK |
| 662 | ret ST_RE_GRAPH |
| 663 | end |
| 664 | cnt = get n_out 0 |
| 665 | if ne cnt ulong sv1_get32 cmt_out 188 |
| 666 | ret ST_RE_GRAPH |
| 667 | end |
| 668 | if ne st_cat_validate_extents cidx cnt sv1_get64 cmt_out 48 cat_fu sv1_get64 cmt_out 192 SV1_OK |
| 669 | ret ST_RE_GRAPH |
| 670 | end |
| 671 | ent = new uchar 64 0 |
| 672 | i = ulong 0 |
| 673 | while lt i cnt bound 512 |
| 674 | st_cat_get_entry cidx i ent |
| 675 | r = st_verify_app_object ent |
| 676 | if ne r ST_RE_OK |
| 677 | ret r |
| 678 | end |
| 679 | i = add i 1 |
| 680 | end |
| 681 | ret ST_RE_OK |
| 682 | end |
| 683 | |
| 684 | fn st_validate_root sb view uchar 4096 cidx ulong cmt_out edit uchar 200 cid_out edit uchar 32 n_out edit ulong 1 to int |
| 685 | if ne sv1_get64 sb 48 get st_meta ST_M_REG_UNITS |
| 686 | ret ST_RE_GRAPH |
| 687 | end |
| 688 | ubuf = new uchar 4096 0 |
| 689 | if ne st_dev_read sv1_get64 sb 96 ubuf SV1_OK |
| 690 | ret ST_RE_IO |
| 691 | end |
| 692 | if not sv1_all_zero ubuf SV1_COMMIT_BYTES sub SV1_UNIT SV1_COMMIT_BYTES |
| 693 | ret ST_RE_GRAPH |
| 694 | end |
| 695 | if ne sv1_object_id SV1_KIND_COMMIT SV1_VERSION part ubuf 0 SV1_COMMIT_BYTES cid_out SV1_OK |
| 696 | ret ST_RE_GRAPH |
| 697 | end |
| 698 | if not sv1_equal cid_out part sb 64 32 |
| 699 | ret ST_RE_GRAPH |
| 700 | end |
| 701 | for i ulong 200 |
| 702 | put cmt_out i get ubuf i |
| 703 | end |
| 704 | if ne sv1_commit_validate cmt_out 0 SV1_OK |
| 705 | ret ST_RE_GRAPH |
| 706 | end |
| 707 | if ne sv1_superblock_validate_commit sb cid_out cmt_out SV1_OK |
| 708 | ret ST_RE_GRAPH |
| 709 | end |
| 710 | ret st_validate_root_cat cmt_out cidx n_out |
| 711 | end |
| 712 | |
| 713 | fn st_verify_append_trans old_idx ulong old_n ulong new_idx ulong new_n ulong old_hw ulong new_cat_fu ulong to int |
| 714 | oe = new uchar 64 0 |
| 715 | ne_ent = new uchar 64 0 |
| 716 | i = ulong 0 |
| 717 | while lt i old_n bound 512 |
| 718 | st_cat_get_entry old_idx i oe |
| 719 | j = st_cat_find new_idx new_n part oe 0 32 |
| 720 | if lt j 0 |
| 721 | ret ST_M_INCONSISTENT_HISTORY |
| 722 | end |
| 723 | st_cat_get_entry new_idx ulong j ne_ent |
| 724 | if not sv1_equal ne_ent oe |
| 725 | ret ST_M_INCONSISTENT_HISTORY |
| 726 | end |
| 727 | i = add i 1 |
| 728 | end |
| 729 | k = ulong 0 |
| 730 | i = ulong 0 |
| 731 | while lt i new_n bound 512 |
| 732 | st_cat_get_entry new_idx i ne_ent |
| 733 | j = st_cat_find old_idx old_n part ne_ent 0 32 |
| 734 | if ge j 0 |
| 735 | st_cat_get_entry old_idx ulong j oe |
| 736 | if not sv1_equal oe ne_ent |
| 737 | ret ST_M_INCONSISTENT_HISTORY |
| 738 | end |
| 739 | else |
| 740 | put st_ext_f k sv1_get64 ne_ent 36 |
| 741 | put st_ext_e k ulong sv1_get32 ne_ent 52 |
| 742 | k = add k 1 |
| 743 | end |
| 744 | i = add i 1 |
| 745 | end |
| 746 | st_ext_sort k |
| 747 | nxt = old_hw |
| 748 | i = ulong 0 |
| 749 | while lt i k bound 512 |
| 750 | if ne get st_ext_f i nxt |
| 751 | ret ST_M_INCONSISTENT_HISTORY |
| 752 | end |
| 753 | uc = get st_ext_e i |
| 754 | if gt nxt sub SV1_U64_MAX uc |
| 755 | ret ST_M_INCONSISTENT_HISTORY |
| 756 | end |
| 757 | nxt = add nxt uc |
| 758 | i = add i 1 |
| 759 | end |
| 760 | if ne nxt new_cat_fu |
| 761 | ret ST_M_INCONSISTENT_HISTORY |
| 762 | end |
| 763 | ret SV1_OK |
| 764 | end |
| 765 | |
| 766 | fn st_install_root slot ulong sb view uchar 4096 cmt view uchar 200 cid view uchar 32 n ulong to void |
| 767 | for i ulong 4096 |
| 768 | put st_root_sb i get sb i |
| 769 | end |
| 770 | for i ulong 200 |
| 771 | put st_root_cmt i get cmt i |
| 772 | end |
| 773 | for i ulong 32 |
| 774 | put st_root_cid i get cid i |
| 775 | end |
| 776 | for i ulong 16 |
| 777 | put st_root_uuid i get sb add 28 i |
| 778 | end |
| 779 | put st_meta ST_M_CAT_IDX slot |
| 780 | put st_meta ST_M_CAT_N n |
| 781 | put st_meta ST_M_ACTIVE_SLOT ulong sv1_get32 sb 44 |
| 782 | put st_meta ST_M_GEN sv1_get64 cmt 56 |
| 783 | put st_meta ST_M_HW sv1_get64 cmt 192 |
| 784 | end |
| 785 | |
| 786 | fn st_is_unsup_sb sb view uchar 4096 magic bool crc_ok bool to bool |
| 787 | if or not magic not crc_ok |
| 788 | ret false |
| 789 | end |
| 790 | if or ne sv1_get16 sb 8 1 ne sv1_get16 sb 10 0 |
| 791 | ret true |
| 792 | end |
| 793 | ret not sv1_all_zero sb 12 16 |
| 794 | end |
| 795 | |
| 796 | note ---- Public Store Engine API ---- |
| 797 | |
| 798 | fn st_format uuid view uchar 16 to int |
| 799 | ru = get st_meta ST_M_REG_UNITS |
| 800 | if or lt ru 4 gt ru SV1_MAX_REGION_UNITS |
| 801 | ret SV1_E_MALFORMED_COMMIT |
| 802 | end |
| 803 | g = new uchar 16384 0 |
| 804 | r = sv1_genesis_units uuid ru g |
| 805 | if ne r SV1_OK |
| 806 | ret r |
| 807 | end |
| 808 | zu = new uchar 4096 0 |
| 809 | if or ne st_dev_write 0 zu SV1_OK or ne st_dev_write 1 zu SV1_OK ne st_dev_flush SV1_OK |
| 810 | ret ST_E_IO |
| 811 | end |
| 812 | u2 = new uchar 4096 0 |
| 813 | u3 = new uchar 4096 0 |
| 814 | u0 = new uchar 4096 0 |
| 815 | for i ulong 4096 |
| 816 | put u0 i get g i |
| 817 | put u2 i get g add 8192 i |
| 818 | put u3 i get g add 12288 i |
| 819 | end |
| 820 | if or ne st_dev_write 2 u2 SV1_OK or ne st_dev_write 3 u3 SV1_OK ne st_dev_flush SV1_OK |
| 821 | ret ST_E_IO |
| 822 | end |
| 823 | if or ne st_dev_write 0 u0 SV1_OK ne st_dev_flush SV1_OK |
| 824 | ret ST_E_IO |
| 825 | end |
| 826 | ret SV1_OK |
| 827 | end |
| 828 | |
| 829 | note Slot flags: 1=zero 2=magic 4=crc 8=unsup 16=dec 32=mal 64=cs 128=have 256=gbad |
| 830 | fn st_classify_slot r view uchar 4096 slot uint ru ulong to uint |
| 831 | fl = uint 0 |
| 832 | z = sv1_all_zero r 0 SV1_UNIT |
| 833 | m = sv1_equal part r 0 8 sv1_sb_magic |
| 834 | c = sv1_superblock_crc_ok r 0 |
| 835 | u = st_is_unsup_sb r m c |
| 836 | if z |
| 837 | fl = or fl 1 |
| 838 | end |
| 839 | if m |
| 840 | fl = or fl 2 |
| 841 | end |
| 842 | if c |
| 843 | fl = or fl 4 |
| 844 | end |
| 845 | if u |
| 846 | fl = or fl 8 |
| 847 | end |
| 848 | if not z |
| 849 | if or not m or not c u |
| 850 | fl = or fl 32 |
| 851 | else |
| 852 | fl = or fl 64 |
| 853 | if or ne sv1_superblock_decode r 0 slot SV1_OK ne sv1_get64 r 48 ru |
| 854 | fl = or fl 32 |
| 855 | else |
| 856 | fl = or fl 16 |
| 857 | end |
| 858 | end |
| 859 | end |
| 860 | ret fl |
| 861 | end |
| 862 | |
| 863 | fn st_open_both_valid r0 view uchar 4096 cmt0 view uchar 200 cid0 view uchar 32 r1 view uchar 4096 cmt1 view uchar 200 cid1 view uchar 32 to int |
| 864 | n0 = ulong sv1_get32 cmt0 188 |
| 865 | n1 = ulong sv1_get32 cmt1 188 |
| 866 | g0 = sv1_get64 cmt0 56 |
| 867 | g1 = sv1_get64 cmt1 56 |
| 868 | if eq g0 g1 |
| 869 | if not sv1_superblock_equivalent r0 r1 |
| 870 | ret ST_M_CONFLICTING_ROOTS |
| 871 | end |
| 872 | st_install_root 0 r0 cmt0 cid0 n0 |
| 873 | put st_meta ST_M_STATE ulong long ST_VALID |
| 874 | put st_meta ST_M_PEER ulong long ST_PEER_VALID |
| 875 | ret SV1_OK |
| 876 | end |
| 877 | if lt g0 g1 |
| 878 | if or eq g0 SV1_U64_MAX ne add g0 1 g1 |
| 879 | ret ST_M_INCONSISTENT_HISTORY |
| 880 | end |
| 881 | if not sv1_commit_identifies_pred cmt1 cmt0 cid0 |
| 882 | ret ST_M_INCONSISTENT_HISTORY |
| 883 | end |
| 884 | ar = st_verify_append_trans 0 n0 1 n1 sv1_get64 cmt0 192 sv1_get64 cmt1 168 |
| 885 | if ne ar SV1_OK |
| 886 | ret ar |
| 887 | end |
| 888 | st_install_root 1 r1 cmt1 cid1 n1 |
| 889 | else |
| 890 | if or eq g1 SV1_U64_MAX ne add g1 1 g0 |
| 891 | ret ST_M_INCONSISTENT_HISTORY |
| 892 | end |
| 893 | if not sv1_commit_identifies_pred cmt0 cmt1 cid1 |
| 894 | ret ST_M_INCONSISTENT_HISTORY |
| 895 | end |
| 896 | ar = st_verify_append_trans 1 n1 0 n0 sv1_get64 cmt1 192 sv1_get64 cmt0 168 |
| 897 | if ne ar SV1_OK |
| 898 | ret ar |
| 899 | end |
| 900 | st_install_root 0 r0 cmt0 cid0 n0 |
| 901 | end |
| 902 | put st_meta ST_M_STATE ulong long ST_VALID |
| 903 | put st_meta ST_M_PEER ulong long ST_PEER_VALID |
| 904 | ret SV1_OK |
| 905 | end |
| 906 | |
| 907 | fn st_open_one_peer r_good view uchar 4096 r_other view uchar 4096 fl_other uint to int |
| 908 | if ne and fl_other 1 0 |
| 909 | put st_meta ST_M_STATE ulong long ST_VALID |
| 910 | put st_meta ST_M_PEER ulong long ST_PEER_ZERO |
| 911 | ret SV1_OK |
| 912 | end |
| 913 | if ne and fl_other 32 0 |
| 914 | put st_meta ST_M_STATE ulong long ST_DEGRADED_RECOVERY |
| 915 | put st_meta ST_M_PEER ulong long ST_PEER_MALFORMED |
| 916 | ret SV1_OK |
| 917 | end |
| 918 | if and ne and fl_other 256 0 ne and fl_other 64 0 |
| 919 | og = sv1_get64 r_other 56 |
| 920 | gg = sv1_get64 r_good 56 |
| 921 | if gt og gg |
| 922 | put st_meta ST_M_STATE ulong long ST_DEGRADED_RECOVERY |
| 923 | put st_meta ST_M_PEER ulong long ST_PEER_GRAPH_BAD_NEWER |
| 924 | ret SV1_OK |
| 925 | end |
| 926 | if lt og gg |
| 927 | put st_meta ST_M_STATE ulong long ST_VALID |
| 928 | put st_meta ST_M_PEER ulong long ST_PEER_GRAPH_BAD_OLDER |
| 929 | ret SV1_OK |
| 930 | end |
| 931 | end |
| 932 | ret ST_M_CORRUPT_RECOVERY_REQUIRED |
| 933 | end |
| 934 | |
| 935 | fn st_open_impl to int |
| 936 | ru = get st_meta ST_M_REG_UNITS |
| 937 | if or lt ru 4 gt ru SV1_MAX_REGION_UNITS |
| 938 | ret ST_M_CORRUPT_RECOVERY_REQUIRED |
| 939 | end |
| 940 | r0 = new uchar 4096 0 |
| 941 | r1 = new uchar 4096 0 |
| 942 | if ne st_dev_read 0 r0 SV1_OK |
| 943 | ret ST_M_IO |
| 944 | end |
| 945 | if ne st_dev_read 1 r1 SV1_OK |
| 946 | ret ST_M_IO |
| 947 | end |
| 948 | f0 = st_classify_slot r0 0 ru |
| 949 | f1 = st_classify_slot r1 1 ru |
| 950 | if and ne and f0 1 0 ne and f1 1 0 |
| 951 | ret ST_M_UNFORMATTED |
| 952 | end |
| 953 | if and eq and f0 2 0 eq and f1 2 0 |
| 954 | ret ST_M_FOREIGN |
| 955 | end |
| 956 | if or eq and f0 12 12 eq and f1 12 12 |
| 957 | ret ST_M_UNSUPPORTED_VERSION |
| 958 | end |
| 959 | cmt0 = new uchar 200 0 |
| 960 | cid0 = new uchar 32 0 |
| 961 | ntmp = new ulong 1 0 |
| 962 | if ne and f0 16 0 |
| 963 | vr0 = st_validate_root r0 0 cmt0 cid0 ntmp |
| 964 | if eq vr0 ST_RE_IO |
| 965 | ret ST_M_IO |
| 966 | end |
| 967 | if eq vr0 ST_RE_GRAPH |
| 968 | f0 = or f0 256 |
| 969 | else |
| 970 | f0 = or f0 128 |
| 971 | end |
| 972 | end |
| 973 | cmt1 = new uchar 200 0 |
| 974 | cid1 = new uchar 32 0 |
| 975 | if ne and f1 16 0 |
| 976 | vr1 = st_validate_root r1 1 cmt1 cid1 ntmp |
| 977 | if eq vr1 ST_RE_IO |
| 978 | ret ST_M_IO |
| 979 | end |
| 980 | if eq vr1 ST_RE_GRAPH |
| 981 | f1 = or f1 256 |
| 982 | else |
| 983 | f1 = or f1 128 |
| 984 | end |
| 985 | end |
| 986 | put st_meta ST_M_POISONED 0 |
| 987 | if ne and and f0 f1 128 0 |
| 988 | ret st_open_both_valid r0 cmt0 cid0 r1 cmt1 cid1 |
| 989 | end |
| 990 | if ne and f0 128 0 |
| 991 | st_install_root 0 r0 cmt0 cid0 ulong sv1_get32 cmt0 188 |
| 992 | ret st_open_one_peer r0 r1 f1 |
| 993 | end |
| 994 | if ne and f1 128 0 |
| 995 | st_install_root 1 r1 cmt1 cid1 ulong sv1_get32 cmt1 188 |
| 996 | ret st_open_one_peer r1 r0 f0 |
| 997 | end |
| 998 | ret ST_M_CORRUPT_RECOVERY_REQUIRED |
| 999 | end |
| 1000 | |
| 1001 | fn st_open to int |
| 1002 | put st_meta ST_M_POISONED 1 |
| 1003 | ret st_open_impl |
| 1004 | end |
| 1005 | |
| 1006 | fn st_generation to ulong |
| 1007 | ret get st_meta ST_M_GEN |
| 1008 | end |
| 1009 | |
| 1010 | fn st_high_water to ulong |
| 1011 | ret get st_meta ST_M_HW |
| 1012 | end |
| 1013 | |
| 1014 | fn st_active_slot to ulong |
| 1015 | ret get st_meta ST_M_ACTIVE_SLOT |
| 1016 | end |
| 1017 | |
| 1018 | fn st_state to int |
| 1019 | ret int long get st_meta ST_M_STATE |
| 1020 | end |
| 1021 | |
| 1022 | fn st_peer to int |
| 1023 | ret int long get st_meta ST_M_PEER |
| 1024 | end |
| 1025 | |
| 1026 | fn st_catalog_count to ulong |
| 1027 | ret get st_meta ST_M_CAT_N |
| 1028 | end |
| 1029 | |
| 1030 | fn st_catalog_entry idx ulong dst edit uchar 64 to bool |
| 1031 | if ge idx get st_meta ST_M_CAT_N |
| 1032 | ret false |
| 1033 | end |
| 1034 | st_cat_get_entry get st_meta ST_M_CAT_IDX idx dst |
| 1035 | ret true |
| 1036 | end |
| 1037 | |
| 1038 | fn st_get_uuid dst edit uchar 16 to void |
| 1039 | for i ulong 16 |
| 1040 | put dst i get st_root_uuid i |
| 1041 | end |
| 1042 | end |
| 1043 | |
| 1044 | note ---- Transaction builder & commit ---- |
| 1045 | |
| 1046 | fn st_tx_begin to void |
| 1047 | put st_tx_n 0 0 |
| 1048 | put st_tx_pos 0 0 |
| 1049 | end |
| 1050 | |
| 1051 | fn st_tx_add kind ushort ver ushort data view uchar * to int |
| 1052 | n = get st_tx_n 0 |
| 1053 | if ge n ST_TX_MAX_OBJS |
| 1054 | ret ST_E_TRANSACTION_LIMIT |
| 1055 | end |
| 1056 | dlen = len data |
| 1057 | pos = get st_tx_pos 0 |
| 1058 | if gt add pos dlen ST_TX_BUF_BYTES |
| 1059 | ret ST_E_ARG |
| 1060 | end |
| 1061 | put st_tx_kind n kind |
| 1062 | put st_tx_ver n ver |
| 1063 | put st_tx_off n pos |
| 1064 | put st_tx_len n dlen |
| 1065 | i = ulong 0 |
| 1066 | while lt i dlen bound 65536 |
| 1067 | put st_tx_buf add pos i get data i |
| 1068 | i = add i 1 |
| 1069 | end |
| 1070 | put st_tx_pos 0 add pos dlen |
| 1071 | put st_tx_n 0 add n 1 |
| 1072 | ret SV1_OK |
| 1073 | end |
| 1074 | |
| 1075 | fn st_write_staged_buf first ulong units ulong boff ulong blen ulong to int |
| 1076 | ubuf = new uchar 4096 0 |
| 1077 | i = ulong 0 |
| 1078 | while lt i units bound 256 |
| 1079 | start = mul i SV1_UNIT |
| 1080 | take = ulong 0 |
| 1081 | if gt blen start |
| 1082 | take = sub blen start |
| 1083 | end |
| 1084 | if gt take SV1_UNIT |
| 1085 | take = SV1_UNIT |
| 1086 | end |
| 1087 | for j ulong 4096 |
| 1088 | if lt j take |
| 1089 | put ubuf j get st_tx_buf add boff add start j |
| 1090 | else |
| 1091 | put ubuf j 0 |
| 1092 | end |
| 1093 | end |
| 1094 | if ne st_dev_write add first i ubuf SV1_OK |
| 1095 | ret ST_E_IO |
| 1096 | end |
| 1097 | i = add i 1 |
| 1098 | end |
| 1099 | ret SV1_OK |
| 1100 | end |
| 1101 | |
| 1102 | fn st_write_catbuf first ulong units ulong blen ulong to int |
| 1103 | ubuf = new uchar 4096 0 |
| 1104 | i = ulong 0 |
| 1105 | while lt i units bound 65 |
| 1106 | start = mul i SV1_UNIT |
| 1107 | take = ulong 0 |
| 1108 | if gt blen start |
| 1109 | take = sub blen start |
| 1110 | end |
| 1111 | if gt take SV1_UNIT |
| 1112 | take = SV1_UNIT |
| 1113 | end |
| 1114 | for j ulong 4096 |
| 1115 | if lt j take |
| 1116 | put ubuf j get st_catbuf add start j |
| 1117 | else |
| 1118 | put ubuf j 0 |
| 1119 | end |
| 1120 | end |
| 1121 | if ne st_dev_write add first i ubuf SV1_OK |
| 1122 | ret ST_E_IO |
| 1123 | end |
| 1124 | i = add i 1 |
| 1125 | end |
| 1126 | ret SV1_OK |
| 1127 | end |
| 1128 | |
| 1129 | fn st_copy_tx_chunk ubuf edit uchar 4096 src ulong take ulong to void |
| 1130 | for j ulong 4096 |
| 1131 | if lt j take |
| 1132 | put ubuf j get st_tx_buf add src j |
| 1133 | end |
| 1134 | end |
| 1135 | end |
| 1136 | |
| 1137 | fn st_tx_hash_staged idx ulong id_out edit uchar 32 to int |
| 1138 | boff = get st_tx_off idx |
| 1139 | blen = get st_tx_len idx |
| 1140 | ctx = new ulong 40 0 |
| 1141 | r = sv1_object_id_begin ctx get st_tx_kind idx get st_tx_ver idx blen |
| 1142 | if ne r SV1_OK |
| 1143 | ret r |
| 1144 | end |
| 1145 | ubuf = new uchar 4096 0 |
| 1146 | pos = ulong 0 |
| 1147 | while lt pos blen bound 65 |
| 1148 | take = sub blen pos |
| 1149 | if gt take SV1_UNIT |
| 1150 | take = SV1_UNIT |
| 1151 | end |
| 1152 | st_copy_tx_chunk ubuf add boff pos take |
| 1153 | sha256_update ctx part ubuf 0 take |
| 1154 | pos = add pos take |
| 1155 | end |
| 1156 | sha256_final ctx id_out |
| 1157 | ret SV1_OK |
| 1158 | end |
| 1159 | |
| 1160 | fn st_tx_id_eq j ulong id view uchar 32 to bool |
| 1161 | base = mul j 32 |
| 1162 | for b ulong 32 |
| 1163 | if ne get st_tx_id add base b get id b |
| 1164 | ret false |
| 1165 | end |
| 1166 | end |
| 1167 | ret true |
| 1168 | end |
| 1169 | |
| 1170 | fn st_tx_find_dup k ulong i ulong id view uchar 32 to int |
| 1171 | j = ulong 0 |
| 1172 | while lt j k bound 16 |
| 1173 | if st_tx_id_eq j id |
| 1174 | if ne get st_tx_kind j get st_tx_kind i |
| 1175 | ret -1 |
| 1176 | end |
| 1177 | if ne get st_tx_ver j get st_tx_ver i |
| 1178 | ret -1 |
| 1179 | end |
| 1180 | if ne get st_tx_len j get st_tx_len i |
| 1181 | ret -1 |
| 1182 | end |
| 1183 | ret 1 |
| 1184 | end |
| 1185 | j = add j 1 |
| 1186 | end |
| 1187 | ret 0 |
| 1188 | end |
| 1189 | |
| 1190 | fn st_tx_step1_dedup n ulong k_out edit ulong 1 to int |
| 1191 | k = ulong 0 |
| 1192 | id = new uchar 32 0 |
| 1193 | u = new uint 1 0 |
| 1194 | i = ulong 0 |
| 1195 | while lt i n bound 16 |
| 1196 | r = st_tx_hash_staged i id |
| 1197 | if ne r SV1_OK |
| 1198 | ret r |
| 1199 | end |
| 1200 | r = sv1_unit_count get st_tx_len i u |
| 1201 | if ne r SV1_OK |
| 1202 | ret r |
| 1203 | end |
| 1204 | d = st_tx_find_dup k i id |
| 1205 | if lt d 0 |
| 1206 | ret ST_E_DUPLICATE_OBJECT_CONFLICT |
| 1207 | end |
| 1208 | if eq d 0 |
| 1209 | base = mul k 32 |
| 1210 | for b ulong 32 |
| 1211 | put st_tx_id add base b get id b |
| 1212 | end |
| 1213 | put st_tx_kind k get st_tx_kind i |
| 1214 | put st_tx_ver k get st_tx_ver i |
| 1215 | put st_tx_off k get st_tx_off i |
| 1216 | put st_tx_len k get st_tx_len i |
| 1217 | put st_tx_units k get u 0 |
| 1218 | put st_tx_first k 0 |
| 1219 | put st_tx_append k 0 |
| 1220 | k = add k 1 |
| 1221 | end |
| 1222 | i = add i 1 |
| 1223 | end |
| 1224 | put k_out 0 k |
| 1225 | ret SV1_OK |
| 1226 | end |
| 1227 | |
| 1228 | fn st_tx_check_existing old_idx ulong found ulong i ulong ent edit uchar 64 to int |
| 1229 | st_cat_get_entry old_idx found ent |
| 1230 | if or ne sv1_get16 ent 32 get st_tx_kind i ne sv1_get16 ent 34 get st_tx_ver i |
| 1231 | ret ST_E_DUPLICATE_OBJECT_CONFLICT |
| 1232 | end |
| 1233 | if or ne sv1_get64 ent 44 get st_tx_len i ne sv1_get32 ent 52 get st_tx_units i |
| 1234 | ret ST_E_DUPLICATE_OBJECT_CONFLICT |
| 1235 | end |
| 1236 | vr = st_verify_app_object ent |
| 1237 | if eq vr ST_RE_IO |
| 1238 | ret ST_E_IO |
| 1239 | end |
| 1240 | if eq vr ST_RE_GRAPH |
| 1241 | ret ST_E_CORRUPT |
| 1242 | end |
| 1243 | ret SV1_OK |
| 1244 | end |
| 1245 | |
| 1246 | fn st_tx_fill_new_ent ent edit uchar 64 id view uchar 32 i ulong nxt ulong to void |
| 1247 | for b ulong 64 |
| 1248 | put ent b 0 |
| 1249 | end |
| 1250 | for b ulong 32 |
| 1251 | put ent b get id b |
| 1252 | end |
| 1253 | sv1_put16 ent 32 get st_tx_kind i |
| 1254 | sv1_put16 ent 34 get st_tx_ver i |
| 1255 | sv1_put64 ent 36 nxt |
| 1256 | sv1_put64 ent 44 get st_tx_len i |
| 1257 | sv1_put32 ent 52 get st_tx_units i |
| 1258 | sv1_put16 ent 56 0 |
| 1259 | end |
| 1260 | |
| 1261 | fn st_tx_load_id i ulong id edit uchar 32 to void |
| 1262 | base = mul i 32 |
| 1263 | for b ulong 32 |
| 1264 | put id b get st_tx_id add base b |
| 1265 | end |
| 1266 | end |
| 1267 | |
| 1268 | fn st_tx_step2_build_cat k ulong spare ulong out_mn edit ulong 2 to int |
| 1269 | old_idx = get st_meta ST_M_CAT_IDX |
| 1270 | old_n = get st_meta ST_M_CAT_N |
| 1271 | ent = new uchar 64 0 |
| 1272 | id = new uchar 32 0 |
| 1273 | i = ulong 0 |
| 1274 | while lt i old_n bound 512 |
| 1275 | st_cat_get_entry old_idx i ent |
| 1276 | st_cat_put_entry spare i ent |
| 1277 | i = add i 1 |
| 1278 | end |
| 1279 | m = old_n |
| 1280 | app_u = ulong 0 |
| 1281 | nxt = get st_meta ST_M_HW |
| 1282 | i = ulong 0 |
| 1283 | while lt i k bound 16 |
| 1284 | st_tx_load_id i id |
| 1285 | found = st_cat_find old_idx old_n id |
| 1286 | if ge found 0 |
| 1287 | r = st_tx_check_existing old_idx ulong found i ent |
| 1288 | if ne r SV1_OK |
| 1289 | ret r |
| 1290 | end |
| 1291 | else |
| 1292 | if ge m ST_CAT_CAP |
| 1293 | ret ST_E_CATALOG_FULL |
| 1294 | end |
| 1295 | uc = ulong get st_tx_units i |
| 1296 | if gt app_u sub SV1_U64_MAX uc |
| 1297 | ret ST_E_NO_SPACE |
| 1298 | end |
| 1299 | app_u = add app_u uc |
| 1300 | st_tx_fill_new_ent ent id i nxt |
| 1301 | st_cat_put_entry spare m ent |
| 1302 | m = add m 1 |
| 1303 | put st_tx_first i nxt |
| 1304 | put st_tx_append i 1 |
| 1305 | if gt nxt sub SV1_U64_MAX uc |
| 1306 | ret ST_E_NO_SPACE |
| 1307 | end |
| 1308 | nxt = add nxt uc |
| 1309 | end |
| 1310 | i = add i 1 |
| 1311 | end |
| 1312 | st_cat_sort spare m |
| 1313 | if gt app_u SV1_MAX_TX_UNITS |
| 1314 | ret ST_E_TRANSACTION_LIMIT |
| 1315 | end |
| 1316 | put out_mn 0 m |
| 1317 | put out_mn 1 nxt |
| 1318 | ret SV1_OK |
| 1319 | end |
| 1320 | |
| 1321 | note txp: 0=ru 1=ngen 2=cur_gen 3=cat_fu 4=cat_len 5=cat_u 6=m 7=hw 8=cmt_u 9=nslot |
| 1322 | fn st_tx_build_commit txp view ulong 10 cat_id view uchar 32 pc edit uchar 4096 cmt_id edit uchar 32 to int |
| 1323 | for j ulong 8 |
| 1324 | put pc j get sv1_cmt_magic j |
| 1325 | end |
| 1326 | sv1_put16 pc 8 1 |
| 1327 | sv1_put16 pc 10 ushort SV1_COMMIT_BYTES |
| 1328 | sv1_put16 pc 12 1 |
| 1329 | sv1_put16 pc 14 0 |
| 1330 | for j ulong 16 |
| 1331 | put pc add 32 j get st_root_uuid j |
| 1332 | end |
| 1333 | sv1_put64 pc 48 get txp 0 |
| 1334 | sv1_put64 pc 56 get txp 1 |
| 1335 | sv1_put64 pc 64 get txp 2 |
| 1336 | for j ulong 32 |
| 1337 | put pc add 72 j get st_root_cid j |
| 1338 | put pc add 104 j get st_root_cmt add 136 j |
| 1339 | put pc add 136 j get cat_id j |
| 1340 | end |
| 1341 | sv1_put64 pc 168 get txp 3 |
| 1342 | sv1_put64 pc 176 get txp 4 |
| 1343 | sv1_put32 pc 184 uint get txp 5 |
| 1344 | sv1_put32 pc 188 uint get txp 6 |
| 1345 | sv1_put64 pc 192 get txp 7 |
| 1346 | r = sv1_commit_validate pc 0 |
| 1347 | if ne r SV1_OK |
| 1348 | ret r |
| 1349 | end |
| 1350 | ret sv1_object_id SV1_KIND_COMMIT SV1_VERSION part pc 0 SV1_COMMIT_BYTES cmt_id |
| 1351 | end |
| 1352 | |
| 1353 | fn st_tx_build_sb txp view ulong 10 cat_id view uchar 32 cmt_id view uchar 32 psb edit uchar 4096 to void |
| 1354 | for j ulong 8 |
| 1355 | put psb j get sv1_sb_magic j |
| 1356 | end |
| 1357 | sv1_put16 psb 8 1 |
| 1358 | sv1_put16 psb 10 0 |
| 1359 | for j ulong 16 |
| 1360 | put psb add 28 j get st_root_uuid j |
| 1361 | end |
| 1362 | sv1_put32 psb 44 uint get txp 9 |
| 1363 | sv1_put64 psb 48 get txp 0 |
| 1364 | sv1_put64 psb 56 get txp 1 |
| 1365 | for j ulong 32 |
| 1366 | put psb add 64 j get cmt_id j |
| 1367 | put psb add 104 j get cat_id j |
| 1368 | end |
| 1369 | sv1_put64 psb 96 get txp 8 |
| 1370 | sv1_put64 psb 136 get txp 3 |
| 1371 | sv1_put64 psb 144 get txp 4 |
| 1372 | sv1_put32 psb 152 uint get txp 5 |
| 1373 | sv1_put32 psb 156 uint get txp 6 |
| 1374 | sv1_put64 psb 160 get txp 7 |
| 1375 | sv1_put32 psb SV1_SB_CRC_OFFSET sv1_sb_crc psb 0 |
| 1376 | end |
| 1377 | |
| 1378 | fn st_tx_write_all k ulong txp view ulong 10 pc view uchar 4096 psb view uchar 4096 to int |
| 1379 | i = ulong 0 |
| 1380 | while lt i k bound 16 |
| 1381 | if ne get st_tx_append i 0 |
| 1382 | if ne st_write_staged_buf get st_tx_first i ulong get st_tx_units i get st_tx_off i get st_tx_len i SV1_OK |
| 1383 | put st_meta ST_M_POISONED 1 |
| 1384 | ret ST_E_IO |
| 1385 | end |
| 1386 | end |
| 1387 | i = add i 1 |
| 1388 | end |
| 1389 | if ne st_write_catbuf get txp 3 get txp 5 get txp 4 SV1_OK |
| 1390 | put st_meta ST_M_POISONED 1 |
| 1391 | ret ST_E_IO |
| 1392 | end |
| 1393 | if ne st_dev_write get txp 8 pc SV1_OK |
| 1394 | put st_meta ST_M_POISONED 1 |
| 1395 | ret ST_E_IO |
| 1396 | end |
| 1397 | if ne st_dev_flush SV1_OK |
| 1398 | put st_meta ST_M_POISONED 1 |
| 1399 | ret ST_E_IO |
| 1400 | end |
| 1401 | if ne st_dev_write get txp 9 psb SV1_OK |
| 1402 | put st_meta ST_M_POISONED 1 |
| 1403 | ret ST_E_IO |
| 1404 | end |
| 1405 | if ne st_dev_flush SV1_OK |
| 1406 | put st_meta ST_M_POISONED 1 |
| 1407 | ret ST_E_IO |
| 1408 | end |
| 1409 | ret SV1_OK |
| 1410 | end |
| 1411 | |
| 1412 | fn st_tx_finish spare ulong m ulong pc view uchar 4096 psb view uchar 4096 cmt_id view uchar 32 to void |
| 1413 | cmt200 = new uchar 200 0 |
| 1414 | for j ulong 200 |
| 1415 | put cmt200 j get pc j |
| 1416 | end |
| 1417 | st_install_root spare psb cmt200 cmt_id m |
| 1418 | put st_meta ST_M_PEER ulong long ST_PEER_VALID |
| 1419 | end |
| 1420 | |
| 1421 | fn st_tx_commit to int |
| 1422 | if ne get st_meta ST_M_POISONED 0 |
| 1423 | ret ST_E_NEEDS_REOPEN |
| 1424 | end |
| 1425 | if eq st_state ST_DEGRADED_RECOVERY |
| 1426 | ret ST_E_READ_ONLY_DEGRADED |
| 1427 | end |
| 1428 | n = get st_tx_n 0 |
| 1429 | if gt n ST_TX_MAX_OBJS |
| 1430 | ret ST_E_TRANSACTION_LIMIT |
| 1431 | end |
| 1432 | k_arr = new ulong 1 0 |
| 1433 | r = st_tx_step1_dedup n k_arr |
| 1434 | if ne r SV1_OK |
| 1435 | ret r |
| 1436 | end |
| 1437 | k = get k_arr 0 |
| 1438 | spare = sub 1 get st_meta ST_M_CAT_IDX |
| 1439 | mn = new ulong 2 0 |
| 1440 | r = st_tx_step2_build_cat k spare mn |
| 1441 | if ne r SV1_OK |
| 1442 | ret r |
| 1443 | end |
| 1444 | cat_len_arr = new ulong 1 0 |
| 1445 | r = st_cat_encode_to_buf spare get mn 0 cat_len_arr |
| 1446 | if ne r SV1_OK |
| 1447 | ret r |
| 1448 | end |
| 1449 | u = new uint 1 0 |
| 1450 | r = sv1_unit_count get cat_len_arr 0 u |
| 1451 | if ne r SV1_OK |
| 1452 | ret r |
| 1453 | end |
| 1454 | txp = new ulong 10 0 |
| 1455 | put txp 0 get st_meta ST_M_REG_UNITS |
| 1456 | if eq get st_meta ST_M_GEN SV1_U64_MAX |
| 1457 | ret ST_E_GENERATION_EXHAUSTED |
| 1458 | end |
| 1459 | put txp 2 get st_meta ST_M_GEN |
| 1460 | put txp 1 add get txp 2 1 |
| 1461 | put txp 3 get mn 1 |
| 1462 | put txp 4 get cat_len_arr 0 |
| 1463 | put txp 5 ulong get u 0 |
| 1464 | put txp 6 get mn 0 |
| 1465 | if gt get txp 3 sub SV1_U64_MAX get txp 5 |
| 1466 | ret ST_E_NO_SPACE |
| 1467 | end |
| 1468 | put txp 8 add get txp 3 get txp 5 |
| 1469 | if eq get txp 8 SV1_U64_MAX |
| 1470 | ret ST_E_NO_SPACE |
| 1471 | end |
| 1472 | put txp 7 add get txp 8 1 |
| 1473 | if or gt get txp 7 get txp 0 gt get txp 7 SV1_MAX_REGION_UNITS |
| 1474 | ret ST_E_NO_SPACE |
| 1475 | end |
| 1476 | put txp 9 sub 1 get st_meta ST_M_ACTIVE_SLOT |
| 1477 | cat_id = new uchar 32 0 |
| 1478 | if ne st_hash_catbuf get txp 4 cat_id ST_RE_OK |
| 1479 | ret ST_E_CORRUPT |
| 1480 | end |
| 1481 | pc = new uchar 4096 0 |
| 1482 | cmt_id = new uchar 32 0 |
| 1483 | r = st_tx_build_commit txp cat_id pc cmt_id |
| 1484 | if ne r SV1_OK |
| 1485 | ret r |
| 1486 | end |
| 1487 | psb = new uchar 4096 0 |
| 1488 | st_tx_build_sb txp cat_id cmt_id psb |
| 1489 | r = st_tx_write_all k txp pc psb |
| 1490 | if ne r SV1_OK |
| 1491 | ret r |
| 1492 | end |
| 1493 | st_tx_finish spare get txp 6 pc psb cmt_id |
| 1494 | ret SV1_OK |
| 1495 | end |
| 1496 | |
| 1497 | fn st_zero_dst dst edit uchar * to void |
| 1498 | for z ulong len dst bound 65536 |
| 1499 | put dst z 0 |
| 1500 | end |
| 1501 | end |
| 1502 | |
| 1503 | fn st_read_unit_step ctx edit ulong 40 ubuf view uchar 4096 dst edit uchar * start ulong blen ulong to bool |
| 1504 | take = ulong 0 |
| 1505 | if gt blen start |
| 1506 | take = sub blen start |
| 1507 | end |
| 1508 | if gt take SV1_UNIT |
| 1509 | take = SV1_UNIT |
| 1510 | end |
| 1511 | sha256_update ctx part ubuf 0 take |
| 1512 | ok = sv1_all_zero ubuf take sub SV1_UNIT take |
| 1513 | k = ulong 0 |
| 1514 | while lt k take bound 4096 |
| 1515 | put dst add start k get ubuf k |
| 1516 | k = add k 1 |
| 1517 | end |
| 1518 | ret ok |
| 1519 | end |
| 1520 | |
| 1521 | fn st_read_loop ctx edit ulong 40 ent view uchar 64 dst edit uchar * to int |
| 1522 | blen = sv1_get64 ent 44 |
| 1523 | fu = sv1_get64 ent 36 |
| 1524 | ucnt = ulong sv1_get32 ent 52 |
| 1525 | ubuf = new uchar 4096 0 |
| 1526 | bad = false |
| 1527 | i = ulong 0 |
| 1528 | while lt i ucnt bound 256 |
| 1529 | if ne st_dev_read add fu i ubuf SV1_OK |
| 1530 | st_zero_dst dst |
| 1531 | ret ST_E_IO |
| 1532 | end |
| 1533 | if not st_read_unit_step ctx ubuf dst mul i SV1_UNIT blen |
| 1534 | bad = true |
| 1535 | end |
| 1536 | i = add i 1 |
| 1537 | end |
| 1538 | if bad |
| 1539 | ret ST_E_CORRUPT |
| 1540 | end |
| 1541 | ret SV1_OK |
| 1542 | end |
| 1543 | |
| 1544 | fn st_read_object id view uchar 32 dst edit uchar * out_len edit ulong 1 to int |
| 1545 | put out_len 0 0 |
| 1546 | cidx = get st_meta ST_M_CAT_IDX |
| 1547 | j = st_cat_find cidx get st_meta ST_M_CAT_N id |
| 1548 | if lt j 0 |
| 1549 | ret ST_E_INVALID_OBJECT |
| 1550 | end |
| 1551 | ent = new uchar 64 0 |
| 1552 | st_cat_get_entry cidx ulong j ent |
| 1553 | blen = sv1_get64 ent 44 |
| 1554 | if gt blen len dst |
| 1555 | ret ST_E_ARG |
| 1556 | end |
| 1557 | ctx = new ulong 40 0 |
| 1558 | r = sv1_object_id_begin ctx sv1_get16 ent 32 sv1_get16 ent 34 blen |
| 1559 | if ne r SV1_OK |
| 1560 | st_zero_dst dst |
| 1561 | ret ST_E_CORRUPT |
| 1562 | end |
| 1563 | lr = st_read_loop ctx ent dst |
| 1564 | if eq lr ST_E_IO |
| 1565 | ret ST_E_IO |
| 1566 | end |
| 1567 | got = new uchar 32 0 |
| 1568 | sha256_final ctx got |
| 1569 | if or ne lr SV1_OK not sv1_equal got id |
| 1570 | st_zero_dst dst |
| 1571 | ret ST_E_CORRUPT |
| 1572 | end |
| 1573 | put out_len 0 blen |
| 1574 | ret SV1_OK |
| 1575 | end |