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 / train / tg_store.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note tg_store.om -- Pure seeded OM-2 M22 transactional parameter-state substrate. |
| 3 | note Uses native OM-2 syscalls (fopen, fclose, read, write, pread, pwrite, fsync, |
| 4 | note ftrunc, truncate, unlink, makedir, fstat, stat, getdnts, renames, flock, faccess). |
| 5 | note Composed with generation.om and tg_sha256.om. |
| 6 | |
| 7 | const TG_DIGEST ulong 32 |
| 8 | const TG_MAX_REFS ulong 3 |
| 9 | const TG_GEN_HDR ulong 152 |
| 10 | const TG_GEN_HASHED_HDR ulong 120 |
| 11 | const TG_CUR_LEN ulong 95 |
| 12 | const TG_REC_SIZE ulong 192 |
| 13 | const TG_REC_HASHED ulong 160 |
| 14 | const TG_PATHMAX ulong 1024 |
| 15 | const TG_MAX_BUF ulong 4194304 |
| 16 | const TG_MAX_RECS ulong 256 |
| 17 | |
| 18 | const TG_OK long 0 |
| 19 | const TG_E_ARG long -1 |
| 20 | const TG_E_IO long -2 |
| 21 | const TG_E_NOSTORE long -3 |
| 22 | const TG_E_FORMAT long -4 |
| 23 | const TG_E_TRUNC long -5 |
| 24 | const TG_E_DIGEST long -6 |
| 25 | const TG_E_CHAIN long -7 |
| 26 | const TG_E_REJECT long -8 |
| 27 | const TG_E_STATE long -9 |
| 28 | const TG_E_STALE long -10 |
| 29 | const TG_E_SHADOW long -11 |
| 30 | const TG_E_LOCKED long -12 |
| 31 | const TG_E_EXISTS long -13 |
| 32 | const TG_E_NOMEM long -14 |
| 33 | |
| 34 | const TG_FP_NONE ulong 0 |
| 35 | const TG_FP_BEFORE_SHADOW_WRITE ulong 1 |
| 36 | const TG_FP_MID_SHADOW_WRITE ulong 2 |
| 37 | const TG_FP_AFTER_SHADOW_WRITE ulong 3 |
| 38 | const TG_FP_AFTER_GEN_RENAME ulong 4 |
| 39 | const TG_FP_MID_SWITCH ulong 5 |
| 40 | const TG_FP_BEFORE_SWITCH_RENAME ulong 6 |
| 41 | const TG_FP_AFTER_SWITCH_BEFORE_FSYNC ulong 7 |
| 42 | const TG_FP_AFTER_SWITCH ulong 8 |
| 43 | const TG_FP_COUNT ulong 9 |
| 44 | |
| 45 | const TG_FPMODE_EXIT ulong 1 |
| 46 | const TG_FPMODE_CORRUPT ulong 2 |
| 47 | const TG_FP_EXIT_CODE int 77 |
| 48 | |
| 49 | const TG_BUF_PARAMS ulong 1 |
| 50 | const TG_BUF_OPT ulong 2 |
| 51 | const TG_BUF_EXTERNAL ulong 3 |
| 52 | |
| 53 | const TG_SH_OPEN ulong 1 |
| 54 | const TG_SH_COMMITTED ulong 2 |
| 55 | const TG_SH_DISCARDED ulong 3 |
| 56 | |
| 57 | const TG_O_RDONLY int 0 |
| 58 | const TG_O_WRONLY int 1 |
| 59 | const TG_O_RDWR int 2 |
| 60 | const TG_O_CREAT int 64 |
| 61 | const TG_O_TRUNC int 512 |
| 62 | const TG_O_APPEND int 1024 |
| 63 | const TG_O_DIRECTORY int 16384 |
| 64 | const TG_LOCK_EX_NB int 6 |
| 65 | |
| 66 | global tg_fp_state ulong 2 |
| 67 | |
| 68 | global tg_st_dir uchar 1024 |
| 69 | global tg_st_meta ulong 12 |
| 70 | note tg_st_meta: 0=open, 1=dir_len, 2=lock_fd+1, 3=log_fd+1, 4=poisoned, |
| 71 | note 5=cur_gen, 6=cur_step, 7=cur_pb, 8=cur_ob, 9=cur_dlen |
| 72 | global tg_st_digest uchar 32 |
| 73 | global tg_st_prev uchar 32 |
| 74 | global tg_st_dhead uchar 32 |
| 75 | global tg_st_params uchar 4194304 |
| 76 | global tg_st_opt uchar 4194304 |
| 77 | global tg_st_stats ulong 4 |
| 78 | note tg_st_stats: 0=dispatch_records, 1=dispatch_hashed_bytes, 2=commit_hashed_bytes, 3=refusals |
| 79 | |
| 80 | global tg_sh_meta ulong 16 |
| 81 | note per slot (stride 8): 0=state, 1=pb, 2=ob, 3=base_gen, 4=base_step, 5=n_dispatch |
| 82 | global tg_sh_base_dig uchar 64 |
| 83 | global tg_sh0_params uchar 4194304 |
| 84 | global tg_sh0_opt uchar 4194304 |
| 85 | global tg_sh0_recs uchar 49152 |
| 86 | global tg_sh1_params uchar 4194304 |
| 87 | global tg_sh1_opt uchar 4194304 |
| 88 | global tg_sh1_recs uchar 49152 |
| 89 | |
| 90 | global tg_snap_meta ulong 6 |
| 91 | note tg_snap_meta: 0=gen, 1=step, 2=pb, 3=ob, 4=dlen |
| 92 | global tg_snap_digest uchar 32 |
| 93 | global tg_snap_prev uchar 32 |
| 94 | global tg_snap_dhead uchar 32 |
| 95 | global tg_snap_params uchar 4194304 |
| 96 | global tg_snap_opt uchar 4194304 |
| 97 | |
| 98 | global tg_io_buf uchar 4096 |
| 99 | global tg_dents_buf uchar 16384 |
| 100 | global tg_rec_meta ulong 8 |
| 101 | note tg_rec_meta: 0=removed_tmp, 1=removed_orphan_gen, 2=dispatch_truncated |
| 102 | |
| 103 | note Shared scratch for low-register-pressure internal helpers |
| 104 | global tg_dir_arg uchar 1024 |
| 105 | global tg_dir_len ulong 1 |
| 106 | global tg_pa uchar 1024 |
| 107 | global tg_pb uchar 1024 |
| 108 | global tg_plen ulong 2 |
| 109 | global tg_hdr_tmp uchar 152 |
| 110 | global tg_dig_tmp uchar 32 |
| 111 | global tg_dig_exp uchar 32 |
| 112 | global tg_head_tmp uchar 32 |
| 113 | global tg_rec_tmp uchar 192 |
| 114 | global tg_sha_ctx uchar 104 |
| 115 | global tg_fields_tmp ulong 8 |
| 116 | |
| 117 | text tg_txt_cur "CURRENT" |
| 118 | text tg_txt_cur_tmp "CURRENT.tmp" |
| 119 | text tg_txt_dlog "dispatch.log" |
| 120 | text tg_txt_lock "LOCK" |
| 121 | text tg_txt_reflog "refusals.log" |
| 122 | text tg_txt_gen_pfx "gen-" |
| 123 | text tg_txt_sh_pfx "shadow-" |
| 124 | text tg_txt_bin_sfx ".bin" |
| 125 | text tg_txt_tmp_sfx ".tmp" |
| 126 | text tg_txt_refuse_pfx "REFUSE " |
| 127 | |
| 128 | fn tg_failpoint_arm fp ulong mode ulong to void |
| 129 | put tg_fp_state 0 fp |
| 130 | put tg_fp_state 1 mode |
| 131 | end |
| 132 | |
| 133 | fn tg_failpoint_disarm to void |
| 134 | put tg_fp_state 0 TG_FP_NONE |
| 135 | end |
| 136 | |
| 137 | fn tg_fp_hit fp ulong to void |
| 138 | if eq get tg_fp_state 0 fp |
| 139 | if eq get tg_fp_state 1 TG_FPMODE_EXIT |
| 140 | exit TG_FP_EXIT_CODE |
| 141 | end |
| 142 | end |
| 143 | end |
| 144 | |
| 145 | fn tg_set_dir_from_st to void |
| 146 | dl = get tg_st_meta 1 |
| 147 | for i ulong dl |
| 148 | put tg_dir_arg i get tg_st_dir i |
| 149 | end |
| 150 | put tg_dir_len 0 dl |
| 151 | end |
| 152 | |
| 153 | fn tg_set_dir dir view uchar * dlen ulong to void |
| 154 | for i ulong dlen |
| 155 | put tg_dir_arg i get dir i |
| 156 | end |
| 157 | put tg_dir_len 0 dlen |
| 158 | end |
| 159 | |
| 160 | fn tg_mk_path slot ulong name view uchar * to ulong |
| 161 | dl = get tg_dir_len 0 |
| 162 | nl = len name |
| 163 | if ge add add dl 1 nl TG_PATHMAX |
| 164 | put tg_plen slot 0 |
| 165 | ret 0 |
| 166 | end |
| 167 | if eq slot 0 |
| 168 | for i ulong dl |
| 169 | put tg_pa i get tg_dir_arg i |
| 170 | end |
| 171 | put tg_pa dl 47 |
| 172 | for i ulong nl |
| 173 | put tg_pa add add dl 1 i get name i |
| 174 | end |
| 175 | else |
| 176 | for i ulong dl |
| 177 | put tg_pb i get tg_dir_arg i |
| 178 | end |
| 179 | put tg_pb dl 47 |
| 180 | for i ulong nl |
| 181 | put tg_pb add add dl 1 i get name i |
| 182 | end |
| 183 | end |
| 184 | tot = add add dl 1 nl |
| 185 | put tg_plen slot tot |
| 186 | ret tot |
| 187 | end |
| 188 | |
| 189 | fn tg_mk_gen_path slot ulong is_sh ulong g ulong to ulong |
| 190 | dl = get tg_dir_len 0 |
| 191 | if ge add dl 35 TG_PATHMAX |
| 192 | put tg_plen slot 0 |
| 193 | ret 0 |
| 194 | end |
| 195 | buf = new uchar 36 0 |
| 196 | p = ulong 0 |
| 197 | if ne is_sh 0 |
| 198 | for i ulong 7 |
| 199 | put buf i get tg_txt_sh_pfx i |
| 200 | end |
| 201 | p = 7 |
| 202 | else |
| 203 | for i ulong 4 |
| 204 | put buf i get tg_txt_gen_pfx i |
| 205 | end |
| 206 | p = 4 |
| 207 | end |
| 208 | rg = g |
| 209 | for i ulong 20 |
| 210 | pos = add p sub 19 i |
| 211 | put buf pos uchar add 48 rem rg 10 |
| 212 | rg = div rg 10 |
| 213 | end |
| 214 | p = add p 20 |
| 215 | for i ulong 4 |
| 216 | if ne is_sh 0 |
| 217 | put buf add p i get tg_txt_tmp_sfx i |
| 218 | else |
| 219 | put buf add p i get tg_txt_bin_sfx i |
| 220 | end |
| 221 | end |
| 222 | p = add p 4 |
| 223 | ret tg_mk_path slot part buf 0 p |
| 224 | end |
| 225 | |
| 226 | fn tg_rename_pa_to_pb to long |
| 227 | ol = get tg_plen 0 |
| 228 | nl = get tg_plen 1 |
| 229 | pkt = new uchar 2060 0 |
| 230 | put pkt 0 2 |
| 231 | put pkt 1 0 |
| 232 | put pkt 2 uchar and ol 255 |
| 233 | put pkt 3 uchar and shr ol 8 255 |
| 234 | for i ulong ol |
| 235 | put pkt add 4 i get tg_pa i |
| 236 | end |
| 237 | o2 = add 4 ol |
| 238 | put pkt o2 uchar and nl 255 |
| 239 | put pkt add o2 1 uchar and shr nl 8 255 |
| 240 | for i ulong nl |
| 241 | put pkt add add o2 2 i get tg_pb i |
| 242 | end |
| 243 | ret renames pkt 0 add add o2 2 nl 0 |
| 244 | end |
| 245 | |
| 246 | fn tg_write_all_local fd int buf view uchar * n ulong to long |
| 247 | off = ulong 0 |
| 248 | while lt off n bound 1000000 |
| 249 | w = write fd buf off sub n off |
| 250 | if lt w 0 |
| 251 | if eq w -4 |
| 252 | continue |
| 253 | end |
| 254 | ret -1 |
| 255 | end |
| 256 | off = add off ulong w |
| 257 | end |
| 258 | ret 0 |
| 259 | end |
| 260 | |
| 261 | fn tg_read_full_local fd int buf edit uchar * n ulong got edit ulong 1 to long |
| 262 | t = ulong 0 |
| 263 | while lt t n bound 1000000 |
| 264 | r = read fd buf t sub n t |
| 265 | if lt r 0 |
| 266 | if eq r -4 |
| 267 | continue |
| 268 | end |
| 269 | ret -1 |
| 270 | end |
| 271 | if eq r 0 |
| 272 | break |
| 273 | end |
| 274 | t = add t ulong r |
| 275 | end |
| 276 | put got 0 t |
| 277 | ret 0 |
| 278 | end |
| 279 | |
| 280 | fn tg_fsync_dir_arg to long |
| 281 | dl = get tg_dir_len 0 |
| 282 | fd = fopen tg_dir_arg 0 dl or TG_O_RDONLY TG_O_DIRECTORY 0 |
| 283 | if lt fd 0 |
| 284 | ret -1 |
| 285 | end |
| 286 | r = fsync int fd |
| 287 | c = fclose int fd |
| 288 | if ne r 0 |
| 289 | ret -1 |
| 290 | end |
| 291 | ret c |
| 292 | end |
| 293 | |
| 294 | fn tg_stat_size fd int to long |
| 295 | sb = new uchar 128 0 |
| 296 | r = fstat fd sb 0 128 |
| 297 | if lt r 0 |
| 298 | ret -1 |
| 299 | end |
| 300 | sz = ulong 0 |
| 301 | for i ulong 8 |
| 302 | sz = or sz shl ulong get sb add 48 i mul i 8 |
| 303 | end |
| 304 | ret long sz |
| 305 | end |
| 306 | |
| 307 | fn tg_record_refusal_arg err long gen ulong to void |
| 308 | pl = tg_mk_path 0 tg_txt_reflog |
| 309 | if eq pl 0 |
| 310 | ret |
| 311 | end |
| 312 | fd = fopen tg_pa 0 pl or or TG_O_WRONLY TG_O_CREAT TG_O_APPEND 420 |
| 313 | if lt fd 0 |
| 314 | ret |
| 315 | end |
| 316 | line = new uchar 64 0 |
| 317 | for i ulong 7 |
| 318 | put line i get tg_txt_refuse_pfx i |
| 319 | end |
| 320 | put line 7 uchar add 65 rem ulong sub 0 err 26 |
| 321 | put line 8 32 |
| 322 | put line 9 uchar add 48 rem gen 10 |
| 323 | put line 10 10 |
| 324 | w = write int fd line 0 11 |
| 325 | c = fclose int fd |
| 326 | end |
| 327 | |
| 328 | fn tg_copy_to_io src_kind ulong is_opt ulong base ulong take ulong to void |
| 329 | for i ulong take |
| 330 | idx = add base i |
| 331 | v = uchar 0 |
| 332 | if eq src_kind 0 |
| 333 | if eq is_opt 0 |
| 334 | v = get tg_snap_params idx |
| 335 | else |
| 336 | v = get tg_snap_opt idx |
| 337 | end |
| 338 | else |
| 339 | if eq src_kind 1 |
| 340 | if eq is_opt 0 |
| 341 | v = get tg_sh0_params idx |
| 342 | else |
| 343 | v = get tg_sh0_opt idx |
| 344 | end |
| 345 | else |
| 346 | if eq src_kind 2 |
| 347 | if eq is_opt 0 |
| 348 | v = get tg_sh1_params idx |
| 349 | else |
| 350 | v = get tg_sh1_opt idx |
| 351 | end |
| 352 | else |
| 353 | if eq is_opt 0 |
| 354 | v = get tg_st_params idx |
| 355 | else |
| 356 | v = get tg_st_opt idx |
| 357 | end |
| 358 | end |
| 359 | end |
| 360 | end |
| 361 | put tg_io_buf i v |
| 362 | end |
| 363 | end |
| 364 | |
| 365 | fn tg_sha_update_io ctx edit uchar 104 take ulong to void |
| 366 | loc = new uchar 4096 0 |
| 367 | for i ulong take |
| 368 | put loc i get tg_io_buf i |
| 369 | end |
| 370 | tg_sha_update ctx loc take |
| 371 | end |
| 372 | |
| 373 | fn tg_sha_update_global ctx edit uchar 104 src_kind ulong is_opt ulong n ulong to void |
| 374 | pos = ulong 0 |
| 375 | while lt pos n bound 2048 |
| 376 | take = sub n pos |
| 377 | if gt take 4096 |
| 378 | take = 4096 |
| 379 | end |
| 380 | tg_copy_to_io src_kind is_opt pos take |
| 381 | tg_sha_update_io ctx take |
| 382 | pos = add pos take |
| 383 | end |
| 384 | end |
| 385 | |
| 386 | fn tg_gen_digest_tmp src_kind ulong pb ulong ob ulong to void |
| 387 | ctx = new uchar 104 0 |
| 388 | tg_sha_init ctx |
| 389 | hb = new uchar 120 0 |
| 390 | for i ulong 120 |
| 391 | put hb i get tg_hdr_tmp i |
| 392 | end |
| 393 | tg_sha_update ctx hb 120 |
| 394 | if gt pb 0 |
| 395 | tg_sha_update_global ctx src_kind 0 pb |
| 396 | end |
| 397 | if gt ob 0 |
| 398 | tg_sha_update_global ctx src_kind 1 ob |
| 399 | end |
| 400 | dig = new uchar 32 0 |
| 401 | tg_sha_final ctx dig |
| 402 | for i ulong 32 |
| 403 | put tg_dig_tmp i get dig i |
| 404 | end |
| 405 | end |
| 406 | |
| 407 | fn tg_write_global_fd fd int src_kind ulong is_opt ulong off ulong n ulong to long |
| 408 | pos = ulong 0 |
| 409 | while lt pos n bound 2048 |
| 410 | take = sub n pos |
| 411 | if gt take 4096 |
| 412 | take = 4096 |
| 413 | end |
| 414 | tg_copy_to_io src_kind is_opt add off pos take |
| 415 | w = write fd tg_io_buf 0 take |
| 416 | if lt w 0 |
| 417 | if eq w -4 |
| 418 | continue |
| 419 | end |
| 420 | ret -1 |
| 421 | end |
| 422 | pos = add pos ulong w |
| 423 | end |
| 424 | ret 0 |
| 425 | end |
| 426 | |
| 427 | fn tg_copy_io_to_snap is_opt ulong pos ulong n ulong to void |
| 428 | for i ulong n |
| 429 | v = get tg_io_buf i |
| 430 | if eq is_opt 0 |
| 431 | put tg_snap_params add pos i v |
| 432 | else |
| 433 | put tg_snap_opt add pos i v |
| 434 | end |
| 435 | end |
| 436 | end |
| 437 | |
| 438 | fn tg_read_to_snap fd int is_opt ulong n ulong to long |
| 439 | pos = ulong 0 |
| 440 | while lt pos n bound 2048 |
| 441 | take = sub n pos |
| 442 | if gt take 4096 |
| 443 | take = 4096 |
| 444 | end |
| 445 | r = read fd tg_io_buf 0 take |
| 446 | if lt r 0 |
| 447 | if eq r -4 |
| 448 | continue |
| 449 | end |
| 450 | ret -1 |
| 451 | end |
| 452 | if eq r 0 |
| 453 | break |
| 454 | end |
| 455 | tg_copy_io_to_snap is_opt pos ulong r |
| 456 | pos = add pos ulong r |
| 457 | end |
| 458 | ret long pos |
| 459 | end |
| 460 | |
| 461 | fn tg_load_gen_pa expect_gen ulong check_dig ulong to long |
| 462 | fd = fopen tg_pa 0 get tg_plen 0 TG_O_RDONLY 0 |
| 463 | if lt fd 0 |
| 464 | if eq fd -2 |
| 465 | ret TG_E_NOSTORE |
| 466 | end |
| 467 | ret TG_E_IO |
| 468 | end |
| 469 | sz = tg_stat_size int fd |
| 470 | if lt sz 0 |
| 471 | c = fclose int fd |
| 472 | ret TG_E_IO |
| 473 | end |
| 474 | h = new uchar 152 0 |
| 475 | got = new ulong 1 0 |
| 476 | if lt tg_read_full_local int fd h TG_GEN_HDR got 0 |
| 477 | c = fclose int fd |
| 478 | ret TG_E_IO |
| 479 | end |
| 480 | if lt get got 0 TG_GEN_HDR |
| 481 | c = fclose int fd |
| 482 | ret TG_E_TRUNC |
| 483 | end |
| 484 | fields = new ulong 5 0 |
| 485 | st = tg_decode_generation h ulong sz expect_gen fields |
| 486 | if ne st 1 |
| 487 | c = fclose int fd |
| 488 | if eq st 2 |
| 489 | ret TG_E_TRUNC |
| 490 | end |
| 491 | ret TG_E_FORMAT |
| 492 | end |
| 493 | for i ulong 152 |
| 494 | put tg_hdr_tmp i get h i |
| 495 | end |
| 496 | for i ulong 5 |
| 497 | put tg_fields_tmp i get fields i |
| 498 | end |
| 499 | ret tg_load_gen_body int fd expect_gen check_dig |
| 500 | end |
| 501 | |
| 502 | fn tg_load_gen_body fd int expect_gen ulong check_dig ulong to long |
| 503 | pb = get tg_fields_tmp 2 |
| 504 | ob = get tg_fields_tmp 3 |
| 505 | if or gt pb TG_MAX_BUF gt ob TG_MAX_BUF |
| 506 | c = fclose fd |
| 507 | ret TG_E_NOMEM |
| 508 | end |
| 509 | g1 = tg_read_to_snap fd 0 pb |
| 510 | if lt g1 0 |
| 511 | c = fclose fd |
| 512 | ret TG_E_IO |
| 513 | end |
| 514 | g2 = tg_read_to_snap fd 1 ob |
| 515 | c = fclose fd |
| 516 | if lt g2 0 |
| 517 | ret TG_E_IO |
| 518 | end |
| 519 | if or ne ulong g1 pb ne ulong g2 ob |
| 520 | ret TG_E_TRUNC |
| 521 | end |
| 522 | tg_gen_digest_tmp 0 pb ob |
| 523 | for i ulong 32 |
| 524 | if ne get tg_dig_tmp i get tg_hdr_tmp add 120 i |
| 525 | ret TG_E_DIGEST |
| 526 | end |
| 527 | if ne check_dig 0 |
| 528 | if ne get tg_dig_tmp i get tg_dig_exp i |
| 529 | ret TG_E_DIGEST |
| 530 | end |
| 531 | end |
| 532 | end |
| 533 | put tg_snap_meta 0 expect_gen |
| 534 | put tg_snap_meta 1 get tg_fields_tmp 1 |
| 535 | put tg_snap_meta 2 pb |
| 536 | put tg_snap_meta 3 ob |
| 537 | put tg_snap_meta 4 get tg_fields_tmp 4 |
| 538 | for i ulong 32 |
| 539 | put tg_snap_dhead i get tg_hdr_tmp add 56 i |
| 540 | put tg_snap_prev i get tg_hdr_tmp add 88 i |
| 541 | put tg_snap_digest i get tg_dig_tmp i |
| 542 | end |
| 543 | ret TG_OK |
| 544 | end |
| 545 | |
| 546 | fn tg_write_gen_pa src_kind ulong pb ulong ob ulong with_fp ulong to long |
| 547 | fd = fopen tg_pa 0 get tg_plen 0 or or TG_O_WRONLY TG_O_CREAT TG_O_TRUNC 420 |
| 548 | if lt fd 0 |
| 549 | ret TG_E_IO |
| 550 | end |
| 551 | w0 = write int fd tg_hdr_tmp 0 TG_GEN_HDR |
| 552 | if ne w0 long TG_GEN_HDR |
| 553 | c = fclose int fd |
| 554 | ret TG_E_IO |
| 555 | end |
| 556 | if and ne with_fp 0 eq get tg_fp_state 0 TG_FP_MID_SHADOW_WRITE |
| 557 | half = div pb 2 |
| 558 | if lt tg_write_global_fd int fd src_kind 0 0 half 0 |
| 559 | c = fclose int fd |
| 560 | ret TG_E_IO |
| 561 | end |
| 562 | tg_fp_hit TG_FP_MID_SHADOW_WRITE |
| 563 | if lt tg_write_global_fd int fd src_kind 0 half sub pb half 0 |
| 564 | c = fclose int fd |
| 565 | ret TG_E_IO |
| 566 | end |
| 567 | else |
| 568 | if gt pb 0 |
| 569 | if lt tg_write_global_fd int fd src_kind 0 0 pb 0 |
| 570 | c = fclose int fd |
| 571 | ret TG_E_IO |
| 572 | end |
| 573 | end |
| 574 | end |
| 575 | if gt ob 0 |
| 576 | if lt tg_write_global_fd int fd src_kind 1 0 ob 0 |
| 577 | c = fclose int fd |
| 578 | ret TG_E_IO |
| 579 | end |
| 580 | end |
| 581 | if ne fsync int fd 0 |
| 582 | c = fclose int fd |
| 583 | ret TG_E_IO |
| 584 | end |
| 585 | if ne fclose int fd 0 |
| 586 | ret TG_E_IO |
| 587 | end |
| 588 | ret TG_OK |
| 589 | end |
| 590 | |
| 591 | fn tg_write_pointer_arg gen ulong with_fp ulong to long |
| 592 | if or eq tg_mk_path 0 tg_txt_cur_tmp 0 eq tg_mk_path 1 tg_txt_cur 0 |
| 593 | ret TG_E_ARG |
| 594 | end |
| 595 | dig = new uchar 32 0 |
| 596 | for i ulong 32 |
| 597 | put dig i get tg_dig_tmp i |
| 598 | end |
| 599 | buf = new uchar 95 0 |
| 600 | tg_encode_pointer gen dig buf |
| 601 | fd = fopen tg_pa 0 get tg_plen 0 or or TG_O_WRONLY TG_O_CREAT TG_O_TRUNC 420 |
| 602 | if lt fd 0 |
| 603 | ret TG_E_IO |
| 604 | end |
| 605 | if and ne with_fp 0 eq get tg_fp_state 0 TG_FP_MID_SWITCH |
| 606 | half = div TG_CUR_LEN 2 |
| 607 | if lt tg_write_all_local int fd part buf 0 half half 0 |
| 608 | c = fclose int fd |
| 609 | ret TG_E_IO |
| 610 | end |
| 611 | tg_fp_hit TG_FP_MID_SWITCH |
| 612 | if lt tg_write_all_local int fd part buf half sub TG_CUR_LEN half sub TG_CUR_LEN half 0 |
| 613 | c = fclose int fd |
| 614 | ret TG_E_IO |
| 615 | end |
| 616 | else |
| 617 | if lt tg_write_all_local int fd buf TG_CUR_LEN 0 |
| 618 | c = fclose int fd |
| 619 | ret TG_E_IO |
| 620 | end |
| 621 | end |
| 622 | if ne fsync int fd 0 |
| 623 | c = fclose int fd |
| 624 | ret TG_E_IO |
| 625 | end |
| 626 | if ne fclose int fd 0 |
| 627 | ret TG_E_IO |
| 628 | end |
| 629 | if ne with_fp 0 |
| 630 | tg_fp_hit TG_FP_BEFORE_SWITCH_RENAME |
| 631 | end |
| 632 | if ne tg_rename_pa_to_pb 0 |
| 633 | ret TG_E_IO |
| 634 | end |
| 635 | if ne with_fp 0 |
| 636 | tg_fp_hit TG_FP_AFTER_SWITCH_BEFORE_FSYNC |
| 637 | end |
| 638 | if ne tg_fsync_dir_arg 0 |
| 639 | ret 1 |
| 640 | end |
| 641 | ret TG_OK |
| 642 | end |
| 643 | |
| 644 | fn tg_read_pointer_arg gen_out edit ulong 1 to long |
| 645 | if eq tg_mk_path 0 tg_txt_cur 0 |
| 646 | ret TG_E_ARG |
| 647 | end |
| 648 | fd = fopen tg_pa 0 get tg_plen 0 TG_O_RDONLY 0 |
| 649 | if lt fd 0 |
| 650 | if eq fd -2 |
| 651 | ret TG_E_NOSTORE |
| 652 | end |
| 653 | ret TG_E_IO |
| 654 | end |
| 655 | buf = new uchar 103 0 |
| 656 | got = new ulong 1 0 |
| 657 | r = tg_read_full_local int fd buf 103 got |
| 658 | c = fclose int fd |
| 659 | if lt r 0 |
| 660 | ret TG_E_IO |
| 661 | end |
| 662 | if ne get got 0 TG_CUR_LEN |
| 663 | ret TG_E_FORMAT |
| 664 | end |
| 665 | b95 = new uchar 95 0 |
| 666 | for i ulong 95 |
| 667 | put b95 i get buf i |
| 668 | end |
| 669 | dig = new uchar 32 0 |
| 670 | if eq tg_decode_pointer b95 gen_out dig 0 |
| 671 | ret TG_E_FORMAT |
| 672 | end |
| 673 | for i ulong 32 |
| 674 | put tg_dig_exp i get dig i |
| 675 | end |
| 676 | ret TG_OK |
| 677 | end |
| 678 | |
| 679 | fn tg_chain_step prev view uchar 32 rec view uchar 192 dst edit uchar 32 to void |
| 680 | ctx = new uchar 104 0 |
| 681 | tg_sha_init ctx |
| 682 | tg_sha_update ctx prev TG_DIGEST |
| 683 | tg_sha_update ctx part rec 0 TG_REC_HASHED TG_REC_HASHED |
| 684 | tg_sha_final ctx dst |
| 685 | end |
| 686 | |
| 687 | fn tg_dispatch_chain_arg clen ulong to long |
| 688 | if ne rem clen TG_REC_SIZE 0 |
| 689 | ret TG_E_CHAIN |
| 690 | end |
| 691 | if eq tg_mk_path 0 tg_txt_dlog 0 |
| 692 | ret TG_E_ARG |
| 693 | end |
| 694 | for i ulong 32 |
| 695 | put tg_head_tmp i 0 |
| 696 | end |
| 697 | fd = fopen tg_pa 0 get tg_plen 0 TG_O_RDONLY 0 |
| 698 | if lt fd 0 |
| 699 | if eq clen 0 |
| 700 | ret TG_OK |
| 701 | end |
| 702 | ret TG_E_CHAIN |
| 703 | end |
| 704 | cnt = div clen TG_REC_SIZE |
| 705 | rec = new uchar 192 0 |
| 706 | head = new uchar 32 0 |
| 707 | nh = new uchar 32 0 |
| 708 | got = new ulong 1 0 |
| 709 | for i ulong cnt bound 100000 |
| 710 | if lt tg_read_full_local int fd rec TG_REC_SIZE got 0 |
| 711 | c = fclose int fd |
| 712 | ret TG_E_CHAIN |
| 713 | end |
| 714 | if ne get got 0 TG_REC_SIZE |
| 715 | c = fclose int fd |
| 716 | ret TG_E_CHAIN |
| 717 | end |
| 718 | if eq tg_check_dispatch_header rec i 0 |
| 719 | c = fclose int fd |
| 720 | ret TG_E_CHAIN |
| 721 | end |
| 722 | tg_chain_step head rec nh |
| 723 | for j ulong 32 |
| 724 | if ne get nh j get rec add TG_REC_HASHED j |
| 725 | c = fclose int fd |
| 726 | ret TG_E_CHAIN |
| 727 | end |
| 728 | put head j get nh j |
| 729 | end |
| 730 | end |
| 731 | c = fclose int fd |
| 732 | for i ulong 32 |
| 733 | put tg_head_tmp i get head i |
| 734 | end |
| 735 | ret TG_OK |
| 736 | end |
| 737 | |
| 738 | fn tg_take_lock_arg fd_out edit long 1 to long |
| 739 | if eq tg_mk_path 0 tg_txt_lock 0 |
| 740 | ret TG_E_ARG |
| 741 | end |
| 742 | fd = fopen tg_pa 0 get tg_plen 0 or TG_O_RDWR TG_O_CREAT 420 |
| 743 | if lt fd 0 |
| 744 | ret TG_E_IO |
| 745 | end |
| 746 | if ne flock int fd TG_LOCK_EX_NB 0 |
| 747 | c = fclose int fd |
| 748 | ret TG_E_LOCKED |
| 749 | end |
| 750 | put fd_out 0 fd |
| 751 | ret TG_OK |
| 752 | end |
| 753 | |
| 754 | fn tg_build_gen0_hdr pb ulong ob ulong to void |
| 755 | zero1 = new uchar 32 0 |
| 756 | zero2 = new uchar 32 0 |
| 757 | fields = new ulong 5 0 |
| 758 | put fields 0 0 |
| 759 | put fields 1 0 |
| 760 | put fields 2 pb |
| 761 | put fields 3 ob |
| 762 | put fields 4 0 |
| 763 | h = new uchar 152 0 |
| 764 | tg_encode_generation fields zero1 zero2 h |
| 765 | for i ulong 152 |
| 766 | put tg_hdr_tmp i get h i |
| 767 | end |
| 768 | tg_gen_digest_tmp 0 pb ob |
| 769 | for i ulong 32 |
| 770 | put tg_hdr_tmp add 120 i get tg_dig_tmp i |
| 771 | end |
| 772 | end |
| 773 | |
| 774 | fn tg_create dir view uchar * dlen ulong pb ulong ob ulong to long |
| 775 | if or eq dlen 0 or gt pb TG_MAX_BUF gt ob TG_MAX_BUF |
| 776 | ret TG_E_ARG |
| 777 | end |
| 778 | tg_set_dir dir dlen |
| 779 | mr = makedir tg_dir_arg 0 dlen 493 |
| 780 | if and lt mr 0 ne mr -17 |
| 781 | ret TG_E_IO |
| 782 | end |
| 783 | g0 = new ulong 1 0 |
| 784 | if ne tg_read_pointer_arg g0 TG_E_NOSTORE |
| 785 | ret TG_E_EXISTS |
| 786 | end |
| 787 | lk = new long 1 -1 |
| 788 | r = tg_take_lock_arg lk |
| 789 | if ne r TG_OK |
| 790 | ret r |
| 791 | end |
| 792 | r = tg_create_locked pb ob |
| 793 | c = fclose int get lk 0 |
| 794 | ret r |
| 795 | end |
| 796 | |
| 797 | fn tg_create_locked pb ulong ob ulong to long |
| 798 | if eq tg_mk_path 0 tg_txt_dlog 0 |
| 799 | ret TG_E_ARG |
| 800 | end |
| 801 | lf = fopen tg_pa 0 get tg_plen 0 or or TG_O_WRONLY TG_O_CREAT TG_O_TRUNC 420 |
| 802 | if lt lf 0 |
| 803 | ret TG_E_IO |
| 804 | end |
| 805 | if ne fsync int lf 0 |
| 806 | c = fclose int lf |
| 807 | ret TG_E_IO |
| 808 | end |
| 809 | c = fclose int lf |
| 810 | tg_build_gen0_hdr pb ob |
| 811 | if or eq tg_mk_gen_path 0 1 0 0 eq tg_mk_gen_path 1 0 0 0 |
| 812 | ret TG_E_ARG |
| 813 | end |
| 814 | r = tg_write_gen_pa 0 pb ob 0 |
| 815 | if ne r TG_OK |
| 816 | ret r |
| 817 | end |
| 818 | if ne tg_rename_pa_to_pb 0 |
| 819 | ret TG_E_IO |
| 820 | end |
| 821 | if ne tg_fsync_dir_arg 0 |
| 822 | ret TG_E_IO |
| 823 | end |
| 824 | ret tg_write_pointer_arg 0 0 |
| 825 | end |
| 826 | |
| 827 | fn tg_parse_gen_name name view uchar 256 nlen ulong gen_out edit ulong 1 to ulong |
| 828 | if ne nlen 28 |
| 829 | ret 0 |
| 830 | end |
| 831 | for i ulong 4 |
| 832 | if ne get name i get tg_txt_gen_pfx i |
| 833 | ret 0 |
| 834 | end |
| 835 | if ne get name add 24 i get tg_txt_bin_sfx i |
| 836 | ret 0 |
| 837 | end |
| 838 | end |
| 839 | g = ulong 0 |
| 840 | for i ulong 20 |
| 841 | c = ulong get name add 4 i |
| 842 | if or lt c 48 gt c 57 |
| 843 | ret 0 |
| 844 | end |
| 845 | g = add mul g 10 sub c 48 |
| 846 | end |
| 847 | put gen_out 0 g |
| 848 | ret 1 |
| 849 | end |
| 850 | |
| 851 | fn tg_ends_tmp name view uchar 256 nlen ulong to ulong |
| 852 | if le nlen 4 |
| 853 | ret 0 |
| 854 | end |
| 855 | for i ulong 4 |
| 856 | if ne get name add sub nlen 4 i get tg_txt_tmp_sfx i |
| 857 | ret 0 |
| 858 | end |
| 859 | end |
| 860 | ret 1 |
| 861 | end |
| 862 | |
| 863 | fn tg_sweep_entry pos ulong nru ulong cur_gen ulong to ulong |
| 864 | nm = new uchar 256 0 |
| 865 | nlen = ulong 0 |
| 866 | noff = add pos 19 |
| 867 | while lt nlen 255 bound 256 |
| 868 | if ge add noff nlen nru |
| 869 | break |
| 870 | end |
| 871 | ch = get tg_dents_buf add noff nlen |
| 872 | if eq ch 0 |
| 873 | break |
| 874 | end |
| 875 | put nm nlen ch |
| 876 | nlen = add nlen 1 |
| 877 | end |
| 878 | if eq tg_ends_tmp nm nlen 1 |
| 879 | pl = tg_mk_path 0 part nm 0 nlen |
| 880 | if gt pl 0 |
| 881 | if eq unlink tg_pa 0 pl 0 |
| 882 | put tg_rec_meta 0 add get tg_rec_meta 0 1 |
| 883 | ret 1 |
| 884 | end |
| 885 | end |
| 886 | ret 0 |
| 887 | end |
| 888 | gout = new ulong 1 0 |
| 889 | if eq tg_parse_gen_name nm nlen gout 1 |
| 890 | if gt get gout 0 cur_gen |
| 891 | pl = tg_mk_path 0 part nm 0 nlen |
| 892 | if gt pl 0 |
| 893 | if eq unlink tg_pa 0 pl 0 |
| 894 | put tg_rec_meta 1 add get tg_rec_meta 1 1 |
| 895 | ret 1 |
| 896 | end |
| 897 | end |
| 898 | end |
| 899 | end |
| 900 | ret 0 |
| 901 | end |
| 902 | |
| 903 | fn tg_sweep_chunk nru ulong cur_gen ulong to ulong |
| 904 | changed = ulong 0 |
| 905 | pos = ulong 0 |
| 906 | while lt pos nru bound 4096 |
| 907 | lo = ulong get tg_dents_buf add pos 16 |
| 908 | hi = shl ulong get tg_dents_buf add pos 17 8 |
| 909 | reclen = or lo hi |
| 910 | if eq reclen 0 |
| 911 | break |
| 912 | end |
| 913 | if ne tg_sweep_entry pos nru cur_gen 0 |
| 914 | changed = 1 |
| 915 | end |
| 916 | pos = add pos reclen |
| 917 | end |
| 918 | ret changed |
| 919 | end |
| 920 | |
| 921 | fn tg_sweep_arg cur_gen ulong to long |
| 922 | dfd = fopen tg_dir_arg 0 get tg_dir_len 0 or TG_O_RDONLY TG_O_DIRECTORY 0 |
| 923 | if lt dfd 0 |
| 924 | ret TG_E_IO |
| 925 | end |
| 926 | changed = ulong 0 |
| 927 | while true bound 1024 |
| 928 | nr = getdnts int dfd tg_dents_buf 0 16384 |
| 929 | if lt nr 0 |
| 930 | c = fclose int dfd |
| 931 | ret TG_E_IO |
| 932 | end |
| 933 | if eq nr 0 |
| 934 | break |
| 935 | end |
| 936 | if ne tg_sweep_chunk ulong nr cur_gen 0 |
| 937 | changed = 1 |
| 938 | end |
| 939 | end |
| 940 | c = fclose int dfd |
| 941 | if ne changed 0 |
| 942 | if ne tg_fsync_dir_arg 0 |
| 943 | ret TG_E_IO |
| 944 | end |
| 945 | end |
| 946 | ret TG_OK |
| 947 | end |
| 948 | |
| 949 | fn tg_close to void |
| 950 | if eq get tg_st_meta 0 0 |
| 951 | ret |
| 952 | end |
| 953 | if gt get tg_st_meta 3 0 |
| 954 | c = fclose int sub get tg_st_meta 3 1 |
| 955 | put tg_st_meta 3 0 |
| 956 | end |
| 957 | if gt get tg_st_meta 2 0 |
| 958 | c = fclose int sub get tg_st_meta 2 1 |
| 959 | put tg_st_meta 2 0 |
| 960 | end |
| 961 | put tg_st_meta 0 0 |
| 962 | put tg_st_meta 4 0 |
| 963 | end |
| 964 | |
| 965 | fn tg_adopt_snap_to_cur to void |
| 966 | pb = get tg_snap_meta 2 |
| 967 | ob = get tg_snap_meta 3 |
| 968 | put tg_st_meta 5 get tg_snap_meta 0 |
| 969 | put tg_st_meta 6 get tg_snap_meta 1 |
| 970 | put tg_st_meta 7 pb |
| 971 | put tg_st_meta 8 ob |
| 972 | put tg_st_meta 9 get tg_snap_meta 4 |
| 973 | for i ulong 32 |
| 974 | put tg_st_digest i get tg_snap_digest i |
| 975 | put tg_st_prev i get tg_snap_prev i |
| 976 | put tg_st_dhead i get tg_snap_dhead i |
| 977 | end |
| 978 | for i ulong pb bound 4194305 |
| 979 | put tg_st_params i get tg_snap_params i |
| 980 | end |
| 981 | for i ulong ob bound 4194305 |
| 982 | put tg_st_opt i get tg_snap_opt i |
| 983 | end |
| 984 | end |
| 985 | |
| 986 | fn tg_open_verify_log gen ulong to long |
| 987 | if eq tg_mk_path 0 tg_txt_dlog 0 |
| 988 | ret TG_E_ARG |
| 989 | end |
| 990 | lfd = fopen tg_pa 0 get tg_plen 0 TG_O_RDWR 0 |
| 991 | if lt lfd 0 |
| 992 | ret TG_E_IO |
| 993 | end |
| 994 | put tg_st_meta 3 add ulong lfd 1 |
| 995 | lsz = tg_stat_size int lfd |
| 996 | if lt lsz 0 |
| 997 | ret TG_E_IO |
| 998 | end |
| 999 | dc = get tg_st_meta 9 |
| 1000 | if lt ulong lsz dc |
| 1001 | tg_record_refusal_arg TG_E_CHAIN gen |
| 1002 | ret TG_E_CHAIN |
| 1003 | end |
| 1004 | r = tg_dispatch_chain_arg dc |
| 1005 | if eq r TG_OK |
| 1006 | for i ulong 32 |
| 1007 | if ne get tg_head_tmp i get tg_st_dhead i |
| 1008 | r = TG_E_CHAIN |
| 1009 | end |
| 1010 | end |
| 1011 | end |
| 1012 | if ne r TG_OK |
| 1013 | tg_record_refusal_arg r gen |
| 1014 | ret r |
| 1015 | end |
| 1016 | if gt ulong lsz dc |
| 1017 | put tg_rec_meta 2 sub ulong lsz dc |
| 1018 | if ne ftrunc int lfd long dc 0 |
| 1019 | ret TG_E_IO |
| 1020 | end |
| 1021 | if ne fsync int lfd 0 |
| 1022 | ret TG_E_IO |
| 1023 | end |
| 1024 | end |
| 1025 | ret tg_sweep_arg gen |
| 1026 | end |
| 1027 | |
| 1028 | fn tg_open dir view uchar * dlen ulong to long |
| 1029 | if or eq dlen 0 ge dlen TG_PATHMAX |
| 1030 | ret TG_E_ARG |
| 1031 | end |
| 1032 | tg_set_dir dir dlen |
| 1033 | for i ulong 3 |
| 1034 | put tg_rec_meta i 0 |
| 1035 | end |
| 1036 | for i ulong 4 |
| 1037 | put tg_st_stats i 0 |
| 1038 | end |
| 1039 | for i ulong 12 |
| 1040 | put tg_st_meta i 0 |
| 1041 | end |
| 1042 | for i ulong dlen |
| 1043 | put tg_st_dir i get dir i |
| 1044 | end |
| 1045 | put tg_st_meta 1 dlen |
| 1046 | lk = new long 1 -1 |
| 1047 | r = tg_take_lock_arg lk |
| 1048 | if ne r TG_OK |
| 1049 | ret r |
| 1050 | end |
| 1051 | put tg_st_meta 0 1 |
| 1052 | put tg_st_meta 2 add ulong get lk 0 1 |
| 1053 | ga = new ulong 1 0 |
| 1054 | r = tg_read_pointer_arg ga |
| 1055 | if eq r TG_OK |
| 1056 | if eq tg_mk_gen_path 0 0 get ga 0 0 |
| 1057 | r = TG_E_ARG |
| 1058 | else |
| 1059 | r = tg_load_gen_pa get ga 0 1 |
| 1060 | end |
| 1061 | end |
| 1062 | if ne r TG_OK |
| 1063 | tg_record_refusal_arg r get ga 0 |
| 1064 | tg_close |
| 1065 | ret r |
| 1066 | end |
| 1067 | tg_adopt_snap_to_cur |
| 1068 | r = tg_open_verify_log get ga 0 |
| 1069 | if ne r TG_OK |
| 1070 | tg_close |
| 1071 | ret r |
| 1072 | end |
| 1073 | ret TG_OK |
| 1074 | end |
| 1075 | |
| 1076 | fn tg_shadow_begin slot ulong to long |
| 1077 | if or eq get tg_st_meta 0 0 gt slot 1 |
| 1078 | ret TG_E_ARG |
| 1079 | end |
| 1080 | base = mul slot 8 |
| 1081 | pb = get tg_st_meta 7 |
| 1082 | ob = get tg_st_meta 8 |
| 1083 | put tg_sh_meta base TG_SH_OPEN |
| 1084 | put tg_sh_meta add base 1 pb |
| 1085 | put tg_sh_meta add base 2 ob |
| 1086 | put tg_sh_meta add base 3 get tg_st_meta 5 |
| 1087 | put tg_sh_meta add base 4 get tg_st_meta 6 |
| 1088 | put tg_sh_meta add base 5 0 |
| 1089 | dbase = mul slot 32 |
| 1090 | for i ulong 32 |
| 1091 | put tg_sh_base_dig add dbase i get tg_st_digest i |
| 1092 | end |
| 1093 | if eq slot 0 |
| 1094 | for i ulong pb bound 4194305 |
| 1095 | put tg_sh0_params i get tg_st_params i |
| 1096 | end |
| 1097 | for i ulong ob bound 4194305 |
| 1098 | put tg_sh0_opt i get tg_st_opt i |
| 1099 | end |
| 1100 | else |
| 1101 | for i ulong pb bound 4194305 |
| 1102 | put tg_sh1_params i get tg_st_params i |
| 1103 | end |
| 1104 | for i ulong ob bound 4194305 |
| 1105 | put tg_sh1_opt i get tg_st_opt i |
| 1106 | end |
| 1107 | end |
| 1108 | ret TG_OK |
| 1109 | end |
| 1110 | |
| 1111 | fn tg_shadow_discard slot ulong to void |
| 1112 | if gt slot 1 |
| 1113 | ret |
| 1114 | end |
| 1115 | base = mul slot 8 |
| 1116 | put tg_sh_meta add base 5 0 |
| 1117 | if eq get tg_sh_meta base TG_SH_OPEN |
| 1118 | put tg_sh_meta base TG_SH_DISCARDED |
| 1119 | end |
| 1120 | end |
| 1121 | |
| 1122 | fn tg_dispatch_store slot ulong nd ulong fields view ulong 6 refs view ulong 9 to void |
| 1123 | bdig = new uchar 32 0 |
| 1124 | for i ulong 32 |
| 1125 | put bdig i get tg_sh_base_dig add mul slot 32 i |
| 1126 | end |
| 1127 | rec = new uchar 192 0 |
| 1128 | tg_encode_dispatch fields refs bdig rec |
| 1129 | roff = mul nd TG_REC_SIZE |
| 1130 | for i ulong TG_REC_SIZE |
| 1131 | v = get rec i |
| 1132 | if eq slot 0 |
| 1133 | put tg_sh0_recs add roff i v |
| 1134 | else |
| 1135 | put tg_sh1_recs add roff i v |
| 1136 | end |
| 1137 | end |
| 1138 | end |
| 1139 | |
| 1140 | fn tg_dispatch slot ulong op_id ulong arg0 ulong arg1 ulong refs view ulong 9 n_refs ulong to long |
| 1141 | if or eq get tg_st_meta 0 0 or gt slot 1 gt n_refs TG_MAX_REFS |
| 1142 | ret TG_E_ARG |
| 1143 | end |
| 1144 | base = mul slot 8 |
| 1145 | if ne get tg_sh_meta base TG_SH_OPEN |
| 1146 | ret TG_E_STATE |
| 1147 | end |
| 1148 | nd = get tg_sh_meta add base 5 |
| 1149 | if ge nd TG_MAX_RECS |
| 1150 | ret TG_E_NOMEM |
| 1151 | end |
| 1152 | fields = new ulong 6 0 |
| 1153 | put fields 0 op_id |
| 1154 | put fields 1 add get tg_sh_meta add base 4 1 |
| 1155 | put fields 2 get tg_sh_meta add base 3 |
| 1156 | put fields 3 arg0 |
| 1157 | put fields 4 arg1 |
| 1158 | put fields 5 n_refs |
| 1159 | tg_dispatch_store slot nd fields refs |
| 1160 | put tg_sh_meta add base 5 add nd 1 |
| 1161 | ret TG_OK |
| 1162 | end |
| 1163 | |
| 1164 | fn tg_refuse slot ulong err long to long |
| 1165 | put tg_st_stats 3 add get tg_st_stats 3 1 |
| 1166 | tg_set_dir_from_st |
| 1167 | tg_record_refusal_arg err get tg_st_meta 5 |
| 1168 | if le slot 1 |
| 1169 | if eq get tg_sh_meta mul slot 8 TG_SH_OPEN |
| 1170 | tg_shadow_discard slot |
| 1171 | end |
| 1172 | end |
| 1173 | ret err |
| 1174 | end |
| 1175 | |
| 1176 | fn tg_validate_finite slot ulong to long |
| 1177 | pb = get tg_sh_meta add mul slot 8 1 |
| 1178 | nf = div pb 4 |
| 1179 | for i ulong nf bound 1048577 |
| 1180 | p = mul i 4 |
| 1181 | u = uint 0 |
| 1182 | if eq slot 0 |
| 1183 | u = uint get tg_sh0_params p |
| 1184 | u = or u shl uint get tg_sh0_params add p 1 8 |
| 1185 | u = or u shl uint get tg_sh0_params add p 2 16 |
| 1186 | u = or u shl uint get tg_sh0_params add p 3 24 |
| 1187 | else |
| 1188 | u = uint get tg_sh1_params p |
| 1189 | u = or u shl uint get tg_sh1_params add p 1 8 |
| 1190 | u = or u shl uint get tg_sh1_params add p 2 16 |
| 1191 | u = or u shl uint get tg_sh1_params add p 3 24 |
| 1192 | end |
| 1193 | if ge and u uint 0x7fffffff uint 0x7f800000 |
| 1194 | ret 1 |
| 1195 | end |
| 1196 | end |
| 1197 | ret 0 |
| 1198 | end |
| 1199 | |
| 1200 | fn tg_commit slot ulong validate_mode ulong to long |
| 1201 | if or eq get tg_st_meta 0 0 gt slot 1 |
| 1202 | ret TG_E_ARG |
| 1203 | end |
| 1204 | if ne get tg_st_meta 4 0 |
| 1205 | ret tg_refuse slot TG_E_STATE |
| 1206 | end |
| 1207 | base = mul slot 8 |
| 1208 | if ne get tg_sh_meta base TG_SH_OPEN |
| 1209 | put tg_st_stats 3 add get tg_st_stats 3 1 |
| 1210 | tg_set_dir_from_st |
| 1211 | tg_record_refusal_arg TG_E_STATE get tg_st_meta 5 |
| 1212 | ret TG_E_STATE |
| 1213 | end |
| 1214 | stale = ulong 0 |
| 1215 | if ne get tg_sh_meta add base 3 get tg_st_meta 5 |
| 1216 | stale = 1 |
| 1217 | end |
| 1218 | for i ulong 32 |
| 1219 | if ne get tg_sh_base_dig add mul slot 32 i get tg_st_digest i |
| 1220 | stale = 1 |
| 1221 | end |
| 1222 | end |
| 1223 | if ne stale 0 |
| 1224 | ret tg_refuse slot TG_E_STALE |
| 1225 | end |
| 1226 | if ne get tg_sh_meta add base 1 get tg_st_meta 7 |
| 1227 | ret tg_refuse slot TG_E_ARG |
| 1228 | end |
| 1229 | if ne get tg_sh_meta add base 2 get tg_st_meta 8 |
| 1230 | ret tg_refuse slot TG_E_ARG |
| 1231 | end |
| 1232 | if eq validate_mode 2 |
| 1233 | ret tg_refuse slot TG_E_REJECT |
| 1234 | end |
| 1235 | if eq validate_mode 1 |
| 1236 | if ne tg_validate_finite slot 0 |
| 1237 | ret tg_refuse slot TG_E_REJECT |
| 1238 | end |
| 1239 | end |
| 1240 | ret tg_commit_write slot |
| 1241 | end |
| 1242 | |
| 1243 | fn tg_commit_write slot ulong to long |
| 1244 | nd = get tg_sh_meta add mul slot 8 5 |
| 1245 | base_len = get tg_st_meta 9 |
| 1246 | head = new uchar 32 0 |
| 1247 | for i ulong 32 |
| 1248 | put head i get tg_st_dhead i |
| 1249 | end |
| 1250 | rec = new uchar 192 0 |
| 1251 | nh = new uchar 32 0 |
| 1252 | lfd = int sub get tg_st_meta 3 1 |
| 1253 | for i ulong nd bound 256 |
| 1254 | roff = mul i TG_REC_SIZE |
| 1255 | for j ulong TG_REC_SIZE |
| 1256 | if eq slot 0 |
| 1257 | put rec j get tg_sh0_recs add roff j |
| 1258 | else |
| 1259 | put rec j get tg_sh1_recs add roff j |
| 1260 | end |
| 1261 | end |
| 1262 | tg_set_dispatch_seq rec add div base_len TG_REC_SIZE i |
| 1263 | tg_chain_step head rec nh |
| 1264 | for j ulong 32 |
| 1265 | put rec add TG_REC_HASHED j get nh j |
| 1266 | put head j get nh j |
| 1267 | end |
| 1268 | if ne pwrite lfd rec 0 TG_REC_SIZE long add base_len roff long TG_REC_SIZE |
| 1269 | ft = ftrunc lfd long base_len |
| 1270 | ret tg_refuse slot TG_E_IO |
| 1271 | end |
| 1272 | put tg_st_stats 0 add get tg_st_stats 0 1 |
| 1273 | put tg_st_stats 1 add get tg_st_stats 1 add TG_DIGEST TG_REC_HASHED |
| 1274 | end |
| 1275 | for i ulong 32 |
| 1276 | put tg_head_tmp i get head i |
| 1277 | end |
| 1278 | if gt nd 0 |
| 1279 | if ne fsync lfd 0 |
| 1280 | ft = ftrunc lfd long base_len |
| 1281 | ret tg_refuse slot TG_E_IO |
| 1282 | end |
| 1283 | end |
| 1284 | ret tg_commit_publish slot add base_len mul nd TG_REC_SIZE |
| 1285 | end |
| 1286 | |
| 1287 | fn tg_commit_hdr slot ulong new_len ulong to void |
| 1288 | base = mul slot 8 |
| 1289 | pb = get tg_sh_meta add base 1 |
| 1290 | ob = get tg_sh_meta add base 2 |
| 1291 | fields = new ulong 5 0 |
| 1292 | put fields 0 add get tg_st_meta 5 1 |
| 1293 | put fields 1 add get tg_st_meta 6 1 |
| 1294 | put fields 2 pb |
| 1295 | put fields 3 ob |
| 1296 | put fields 4 new_len |
| 1297 | head = new uchar 32 0 |
| 1298 | prev = new uchar 32 0 |
| 1299 | for i ulong 32 |
| 1300 | put head i get tg_head_tmp i |
| 1301 | put prev i get tg_st_digest i |
| 1302 | end |
| 1303 | h = new uchar 152 0 |
| 1304 | tg_encode_generation fields head prev h |
| 1305 | for i ulong 152 |
| 1306 | put tg_hdr_tmp i get h i |
| 1307 | end |
| 1308 | tg_gen_digest_tmp add slot 1 pb ob |
| 1309 | for i ulong 32 |
| 1310 | put tg_hdr_tmp add 120 i get tg_dig_tmp i |
| 1311 | put tg_dig_exp i get tg_dig_tmp i |
| 1312 | end |
| 1313 | put tg_st_stats 2 add get tg_st_stats 2 add TG_GEN_HASHED_HDR add pb ob |
| 1314 | end |
| 1315 | |
| 1316 | fn tg_corrupt_shadow_pa pb ulong to void |
| 1317 | cfd = fopen tg_pa 0 get tg_plen 0 TG_O_RDWR 0 |
| 1318 | if lt cfd 0 |
| 1319 | ret |
| 1320 | end |
| 1321 | cb = new uchar 1 0 |
| 1322 | cpos = add TG_GEN_HDR div pb 3 |
| 1323 | if eq pread int cfd cb 0 1 long cpos 1 |
| 1324 | put cb 0 xor get cb 0 90 |
| 1325 | pw = pwrite int cfd cb 0 1 long cpos |
| 1326 | end |
| 1327 | cc = fclose int cfd |
| 1328 | end |
| 1329 | |
| 1330 | fn tg_commit_undo slot ulong unlink_pb ulong to long |
| 1331 | if eq unlink_pb 0 |
| 1332 | u = unlink tg_pa 0 get tg_plen 0 |
| 1333 | else |
| 1334 | u = unlink tg_pb 0 get tg_plen 1 |
| 1335 | end |
| 1336 | lfd = int sub get tg_st_meta 3 1 |
| 1337 | ft = ftrunc lfd long get tg_st_meta 9 |
| 1338 | ret tg_refuse slot TG_E_IO |
| 1339 | end |
| 1340 | |
| 1341 | fn tg_commit_publish slot ulong new_len ulong to long |
| 1342 | base = mul slot 8 |
| 1343 | pb = get tg_sh_meta add base 1 |
| 1344 | ob = get tg_sh_meta add base 2 |
| 1345 | ng = add get tg_st_meta 5 1 |
| 1346 | tg_set_dir_from_st |
| 1347 | tg_commit_hdr slot new_len |
| 1348 | if or eq tg_mk_gen_path 0 1 ng 0 eq tg_mk_gen_path 1 0 ng 0 |
| 1349 | ret tg_refuse slot TG_E_ARG |
| 1350 | end |
| 1351 | tg_fp_hit TG_FP_BEFORE_SHADOW_WRITE |
| 1352 | if ne tg_write_gen_pa add slot 1 pb ob 1 TG_OK |
| 1353 | ret tg_commit_undo slot 0 |
| 1354 | end |
| 1355 | if and eq get tg_fp_state 0 TG_FP_AFTER_SHADOW_WRITE eq get tg_fp_state 1 TG_FPMODE_CORRUPT |
| 1356 | tg_corrupt_shadow_pa pb |
| 1357 | end |
| 1358 | if ne tg_load_gen_pa ng 1 TG_OK |
| 1359 | u = unlink tg_pa 0 get tg_plen 0 |
| 1360 | lfd = int sub get tg_st_meta 3 1 |
| 1361 | ft = ftrunc lfd long get tg_st_meta 9 |
| 1362 | fs = fsync lfd |
| 1363 | ret tg_refuse slot TG_E_SHADOW |
| 1364 | end |
| 1365 | tg_fp_hit TG_FP_AFTER_SHADOW_WRITE |
| 1366 | if ne tg_rename_pa_to_pb 0 |
| 1367 | ret tg_commit_undo slot 0 |
| 1368 | end |
| 1369 | if ne tg_fsync_dir_arg 0 |
| 1370 | ret tg_commit_undo slot 1 |
| 1371 | end |
| 1372 | tg_fp_hit TG_FP_AFTER_GEN_RENAME |
| 1373 | wp = tg_write_pointer_arg ng 1 |
| 1374 | if lt wp 0 |
| 1375 | ret tg_commit_undo slot 1 |
| 1376 | end |
| 1377 | if eq wp 0 |
| 1378 | tg_fp_hit TG_FP_AFTER_SWITCH |
| 1379 | end |
| 1380 | tg_commit_adopt slot ng add get tg_st_meta 6 1 new_len |
| 1381 | if gt wp 0 |
| 1382 | put tg_st_meta 4 1 |
| 1383 | put tg_st_stats 3 add get tg_st_stats 3 1 |
| 1384 | tg_record_refusal_arg TG_E_IO ng |
| 1385 | ret TG_E_IO |
| 1386 | end |
| 1387 | ret TG_OK |
| 1388 | end |
| 1389 | |
| 1390 | fn tg_commit_adopt slot ulong ng ulong nstep ulong new_len ulong to void |
| 1391 | base = mul slot 8 |
| 1392 | pb = get tg_sh_meta add base 1 |
| 1393 | ob = get tg_sh_meta add base 2 |
| 1394 | put tg_st_meta 5 ng |
| 1395 | put tg_st_meta 6 nstep |
| 1396 | put tg_st_meta 7 pb |
| 1397 | put tg_st_meta 8 ob |
| 1398 | put tg_st_meta 9 new_len |
| 1399 | for i ulong 32 |
| 1400 | put tg_st_prev i get tg_sh_base_dig add mul slot 32 i |
| 1401 | put tg_st_digest i get tg_dig_tmp i |
| 1402 | put tg_st_dhead i get tg_head_tmp i |
| 1403 | end |
| 1404 | if eq slot 0 |
| 1405 | for i ulong pb bound 4194305 |
| 1406 | put tg_st_params i get tg_sh0_params i |
| 1407 | end |
| 1408 | for i ulong ob bound 4194305 |
| 1409 | put tg_st_opt i get tg_sh0_opt i |
| 1410 | end |
| 1411 | else |
| 1412 | for i ulong pb bound 4194305 |
| 1413 | put tg_st_params i get tg_sh1_params i |
| 1414 | end |
| 1415 | for i ulong ob bound 4194305 |
| 1416 | put tg_st_opt i get tg_sh1_opt i |
| 1417 | end |
| 1418 | end |
| 1419 | put tg_sh_meta base TG_SH_COMMITTED |
| 1420 | end |
| 1421 | |
| 1422 | fn tg_read_current dir view uchar * dlen ulong to long |
| 1423 | if eq dlen 0 |
| 1424 | ret TG_E_ARG |
| 1425 | end |
| 1426 | tg_set_dir dir dlen |
| 1427 | ga = new ulong 1 0 |
| 1428 | r = tg_read_pointer_arg ga |
| 1429 | if ne r TG_OK |
| 1430 | ret r |
| 1431 | end |
| 1432 | if eq tg_mk_gen_path 0 0 get ga 0 0 |
| 1433 | ret TG_E_ARG |
| 1434 | end |
| 1435 | ret tg_load_gen_pa get ga 0 1 |
| 1436 | end |
| 1437 | |
| 1438 | fn tg_read_gen dir view uchar * dlen ulong gen ulong check_dig ulong expect_dig view uchar 32 to long |
| 1439 | if eq dlen 0 |
| 1440 | ret TG_E_ARG |
| 1441 | end |
| 1442 | tg_set_dir dir dlen |
| 1443 | for i ulong 32 |
| 1444 | put tg_dig_exp i get expect_dig i |
| 1445 | end |
| 1446 | if eq tg_mk_gen_path 0 0 gen 0 |
| 1447 | ret TG_E_ARG |
| 1448 | end |
| 1449 | ret tg_load_gen_pa gen check_dig |
| 1450 | end |
| 1451 | |
| 1452 | fn tg_verify_history dir view uchar * dlen ulong n_out edit ulong 1 to long |
| 1453 | put n_out 0 0 |
| 1454 | tg_set_dir dir dlen |
| 1455 | ga = new ulong 1 0 |
| 1456 | r = tg_read_pointer_arg ga |
| 1457 | if ne r TG_OK |
| 1458 | ret r |
| 1459 | end |
| 1460 | gen = get ga 0 |
| 1461 | later_len = ulong 0xffffffffffffffff |
| 1462 | nv = ulong 0 |
| 1463 | while true bound 100000 |
| 1464 | if eq tg_mk_gen_path 0 0 gen 0 |
| 1465 | ret TG_E_ARG |
| 1466 | end |
| 1467 | r = tg_load_gen_pa gen 1 |
| 1468 | if ne r TG_OK |
| 1469 | ret r |
| 1470 | end |
| 1471 | if gt get tg_snap_meta 4 later_len |
| 1472 | ret TG_E_CHAIN |
| 1473 | end |
| 1474 | if eq gen 0 |
| 1475 | if ne get tg_snap_meta 1 0 |
| 1476 | ret TG_E_CHAIN |
| 1477 | end |
| 1478 | for i ulong 32 |
| 1479 | if ne get tg_snap_prev i 0 |
| 1480 | ret TG_E_CHAIN |
| 1481 | end |
| 1482 | end |
| 1483 | end |
| 1484 | later_len = get tg_snap_meta 4 |
| 1485 | for i ulong 32 |
| 1486 | put tg_dig_exp i get tg_snap_prev i |
| 1487 | end |
| 1488 | nv = add nv 1 |
| 1489 | if eq gen 0 |
| 1490 | put n_out 0 nv |
| 1491 | ret TG_OK |
| 1492 | end |
| 1493 | gen = sub gen 1 |
| 1494 | end |
| 1495 | ret TG_E_CHAIN |
| 1496 | end |
| 1497 | |
| 1498 | fn tg_dispatch_read dir view uchar * dlen ulong seq ulong fields edit ulong 7 bdig edit uchar 32 to long |
| 1499 | tg_set_dir dir dlen |
| 1500 | r = tg_dispatch_chain_arg mul add seq 1 TG_REC_SIZE |
| 1501 | if ne r TG_OK |
| 1502 | ret r |
| 1503 | end |
| 1504 | if eq tg_mk_path 0 tg_txt_dlog 0 |
| 1505 | ret TG_E_ARG |
| 1506 | end |
| 1507 | fd = fopen tg_pa 0 get tg_plen 0 TG_O_RDONLY 0 |
| 1508 | if lt fd 0 |
| 1509 | ret TG_E_IO |
| 1510 | end |
| 1511 | rec = new uchar 192 0 |
| 1512 | g = pread int fd rec 0 TG_REC_SIZE long mul seq TG_REC_SIZE |
| 1513 | c = fclose int fd |
| 1514 | if ne g long TG_REC_SIZE |
| 1515 | ret TG_E_CHAIN |
| 1516 | end |
| 1517 | refs = new ulong 9 0 |
| 1518 | if eq tg_decode_dispatch rec fields refs 0 |
| 1519 | ret TG_E_FORMAT |
| 1520 | end |
| 1521 | for i ulong 32 |
| 1522 | put bdig i get rec add 128 i |
| 1523 | end |
| 1524 | ret TG_OK |
| 1525 | end |