omnet
aien-dev/omnet
Brought by drakestapleton
omnet / om / store / storepool.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note Single-threaded program companion. Descriptor cells store fd+1, zero means absent. |
| 3 | note ctx: dir,lock,writer,reader,poison,init,total,count,readtotal,readcount,writebad,eof. |
| 4 | note ctx12/13 track first-epoch inheritance copies until exec or shutdown. |
| 5 | global stctx long 14 |
| 6 | global sthash uchar 64 |
| 7 | |
| 8 | fn stgethash slot ulong h edit uchar 32 to void |
| 9 | for i ulong 32 |
| 10 | put h i get sthash add mul slot 32 i |
| 11 | end |
| 12 | end |
| 13 | fn stsethash slot ulong h view uchar 32 to void |
| 14 | for i ulong 32 |
| 15 | put sthash add mul slot 32 i get h i |
| 16 | end |
| 17 | end |
| 18 | fn stresetread to void |
| 19 | put stctx 8 0 |
| 20 | put stctx 9 0 |
| 21 | put stctx 11 0 |
| 22 | for i ulong 32 |
| 23 | put sthash add i 32 0 |
| 24 | end |
| 25 | end |
| 26 | fn stdrop slot ulong to int |
| 27 | v = get stctx slot |
| 28 | put stctx slot 0 |
| 29 | if gt v long 0 |
| 30 | if ne fclose int sub v long 1 long 0 |
| 31 | ret 1 |
| 32 | end |
| 33 | end |
| 34 | ret 0 |
| 35 | end |
| 36 | |
| 37 | note Store capacity is charged as checked quantities. A record length is admitted |
| 38 | note only if it fits the fixed 65536 frame buffer, and the running byte total and |
| 39 | note record count are accumulated with checked addition up to their declared caps. |
| 40 | note A refused charge returns its cap plus one, which no caller can accept. |
| 41 | fn stbound n ulong to ulong |
| 42 | q = new qty ulong 0 ulong 0 |
| 43 | if ne qword q n ulong 65536 int 0 |
| 44 | ret 65537 |
| 45 | end |
| 46 | ret get q val |
| 47 | end |
| 48 | fn stcharge cur long n ulong to ulong |
| 49 | note A charge below zero is not a reachable store state; refuse it rather than |
| 50 | note trap on the unsigned conversion below. |
| 51 | if lt cur long 0 |
| 52 | ret 33554433 |
| 53 | end |
| 54 | if gt stbound n 65536 |
| 55 | ret 33554433 |
| 56 | end |
| 57 | a = new qty ulong 0 ulong 0 |
| 58 | b = new qty ulong 0 ulong 0 |
| 59 | o = new qty ulong 0 ulong 0 |
| 60 | if ne qword a ulong cur ulong 33554432 int 0 |
| 61 | ret 33554433 |
| 62 | end |
| 63 | if ne qword b n ulong 33554432 int 0 |
| 64 | ret 33554433 |
| 65 | end |
| 66 | note SC1_CHARGE mutation bypasses this checked addition with a modular sum. |
| 67 | if ne qadd o a b int 0 |
| 68 | ret 33554433 |
| 69 | end |
| 70 | ret get o val |
| 71 | end |
| 72 | fn stcount cur long to ulong |
| 73 | note A count below zero is not a reachable store state; refuse it rather than |
| 74 | note trap on the unsigned conversion below. |
| 75 | if lt cur long 0 |
| 76 | ret 65537 |
| 77 | end |
| 78 | c = new qty ulong 0 ulong 0 |
| 79 | o = new qty ulong 0 ulong 0 |
| 80 | if ne qword c ulong cur ulong 65536 int 0 |
| 81 | ret 65537 |
| 82 | end |
| 83 | note SC1_COUNT mutation bypasses this checked step with a modular sum. |
| 84 | if ne qnext o c int 0 |
| 85 | ret 65537 |
| 86 | end |
| 87 | ret get o val |
| 88 | end |
| 89 | |
| 90 | note Reader rejects all damage including trailing data and missing terminator. |
| 91 | fn stnext fd int out edit uchar 65536 to long |
| 92 | if ne get stctx 11 long 0 |
| 93 | ret 0 |
| 94 | end |
| 95 | h = new uchar 65536 0 |
| 96 | d = new uchar 32 0 |
| 97 | p = new uchar 32 0 |
| 98 | q = new uchar 32 0 |
| 99 | if ne streadall fd h 40 0 |
| 100 | ret -1 |
| 101 | end |
| 102 | n = stget64 h 0 |
| 103 | if eq n 0 |
| 104 | stgethash 1 p |
| 105 | if ne steqhash h 8 p 0 |
| 106 | ret -1 |
| 107 | end |
| 108 | if ne streadall fd h 16 0 |
| 109 | ret -1 |
| 110 | end |
| 111 | if ne stget64 h 0 ulong get stctx 8 |
| 112 | ret -1 |
| 113 | end |
| 114 | if ne stget64 h 8 ulong get stctx 9 |
| 115 | ret -1 |
| 116 | end |
| 117 | if ne strd fd h 0 1 long 0 |
| 118 | ret -1 |
| 119 | end |
| 120 | put stctx 11 1 |
| 121 | ret 0 |
| 122 | end |
| 123 | if gt n 65536 |
| 124 | ret -1 |
| 125 | end |
| 126 | nb = stcharge get stctx 8 n |
| 127 | if gt nb 33554432 |
| 128 | ret -1 |
| 129 | end |
| 130 | nc = stcount get stctx 9 |
| 131 | if gt nc 65536 |
| 132 | ret -1 |
| 133 | end |
| 134 | if ne streadall fd out n 0 |
| 135 | ret -1 |
| 136 | end |
| 137 | sthashdata out n d |
| 138 | if ne steqhash h 8 d 0 |
| 139 | ret -1 |
| 140 | end |
| 141 | put stctx 8 long nb |
| 142 | put stctx 9 long nc |
| 143 | stgethash 1 p |
| 144 | stchain p d n nb nc q |
| 145 | stsethash 1 q |
| 146 | ret long n |
| 147 | end |
| 148 | |
| 149 | fn stvalidate fd int to int |
| 150 | if ne stmeta fd 0 0 |
| 151 | ret 1 |
| 152 | end |
| 153 | if ne sthead fd 0 |
| 154 | ret 1 |
| 155 | end |
| 156 | stresetread |
| 157 | b = new uchar 65536 0 |
| 158 | for i ulong 65537 |
| 159 | r = stnext fd b |
| 160 | if lt r long 0 |
| 161 | ret 1 |
| 162 | end |
| 163 | if eq r long 0 |
| 164 | ret 0 |
| 165 | end |
| 166 | end |
| 167 | ret 1 |
| 168 | end |
| 169 | |
| 170 | fn stread out edit uchar 65536 to long |
| 171 | if ne get stctx 4 long 0 |
| 172 | ret -1 |
| 173 | end |
| 174 | if eq get stctx 3 long 0 |
| 175 | ret -1 |
| 176 | end |
| 177 | r = stnext int sub get stctx 3 long 1 out |
| 178 | if lt r long 0 |
| 179 | put stctx 4 1 |
| 180 | end |
| 181 | ret r |
| 182 | end |
| 183 | |
| 184 | fn stload to int |
| 185 | if eq get stctx 0 long 0 |
| 186 | ret 1 |
| 187 | end |
| 188 | if ne get stctx 4 long 0 |
| 189 | ret 1 |
| 190 | end |
| 191 | if ne get stctx 2 long 0 |
| 192 | ret 1 |
| 193 | end |
| 194 | if ne stdrop 3 0 |
| 195 | ret 1 |
| 196 | end |
| 197 | name = new uchar 65536 0 |
| 198 | stpath name |
| 199 | f = fopen name ulong 0 ulong 8 int 559104 int 0 |
| 200 | if lt f long 0 |
| 201 | ret 1 |
| 202 | end |
| 203 | put stctx 3 add f long 1 |
| 204 | if ne stvalidate int f 0 |
| 205 | r = stdrop 3 |
| 206 | ret 1 |
| 207 | end |
| 208 | note Re-establish durability before resuming after a possible uncertain prior rename. |
| 209 | if ne stsync int f long 0 |
| 210 | r = stdrop 3 |
| 211 | ret 1 |
| 212 | end |
| 213 | if ne stsync int sub get stctx 0 long 1 long 0 |
| 214 | r = stdrop 3 |
| 215 | ret 1 |
| 216 | end |
| 217 | if ne sthead int f 0 |
| 218 | r = stdrop 3 |
| 219 | ret 1 |
| 220 | end |
| 221 | stresetread |
| 222 | ret 0 |
| 223 | end |
| 224 | |
| 225 | note O_NOFOLLOW|O_NONBLOCK prevents symlink following and FIFO open blocking. |
| 226 | fn ststale to int |
| 227 | name = new uchar 65536 0 |
| 228 | stpath name |
| 229 | f = fopen name ulong 0 ulong 12 int 559104 int 0 |
| 230 | if eq f long -2 |
| 231 | ret 0 |
| 232 | end |
| 233 | if lt f long 0 |
| 234 | ret 1 |
| 235 | end |
| 236 | r = stmeta int f 0 |
| 237 | c = fclose int f |
| 238 | if ne r 0 |
| 239 | ret 1 |
| 240 | end |
| 241 | if ne c long 0 |
| 242 | ret 1 |
| 243 | end |
| 244 | if ne unlink name ulong 0 ulong 12 long 0 |
| 245 | ret 1 |
| 246 | end |
| 247 | ret 0 |
| 248 | end |
| 249 | |
| 250 | fn stlock fd int to int |
| 251 | a = new uchar 65536 0 |
| 252 | if ne staddr fd a 0 |
| 253 | ret 1 |
| 254 | end |
| 255 | s = socknew int 1 int 524289 int 0 |
| 256 | if lt s long 0 |
| 257 | ret 1 |
| 258 | end |
| 259 | if ne bind int s a ulong 0 ulong 34 long 0 |
| 260 | r = fclose int s |
| 261 | ret 1 |
| 262 | end |
| 263 | put stctx 1 add s long 1 |
| 264 | ret 0 |
| 265 | end |
| 266 | |
| 267 | fn stclose to int |
| 268 | note All directory references close before either writer-lock reference. |
| 269 | r = stdrop 3 |
| 270 | r = or r stdrop 2 |
| 271 | r = or r stdrop 12 |
| 272 | r = or r stdrop 0 |
| 273 | r = or r stdrop 13 |
| 274 | r = or r stdrop 1 |
| 275 | put stctx 4 0 |
| 276 | ret r |
| 277 | end |
| 278 | |
| 279 | fn stopen path view uchar 65536 n ulong init int to int |
| 280 | if ne get stctx 0 long 0 |
| 281 | ret 1 |
| 282 | end |
| 283 | if eq n 0 |
| 284 | ret 1 |
| 285 | end |
| 286 | if gt n 4095 |
| 287 | ret 1 |
| 288 | end |
| 289 | for i ulong n |
| 290 | if eq get path i uchar 0 |
| 291 | ret 1 |
| 292 | end |
| 293 | end |
| 294 | if ne init 0 |
| 295 | if ne init 1 |
| 296 | ret 1 |
| 297 | end |
| 298 | end |
| 299 | note ARM64 O_DIRECTORY=16384, O_NOFOLLOW=32768, O_CLOEXEC=524288. |
| 300 | f = fopen path ulong 0 n int 573440 int 0 |
| 301 | if lt f long 0 |
| 302 | ret 1 |
| 303 | end |
| 304 | put stctx 0 add f long 1 |
| 305 | put stctx 4 0 |
| 306 | if ne stmeta int f 1 0 |
| 307 | r = stclose |
| 308 | ret 1 |
| 309 | end |
| 310 | if ne stlock int f 0 |
| 311 | r = stclose |
| 312 | ret 1 |
| 313 | end |
| 314 | if ne fchdir int f long 0 |
| 315 | r = stclose |
| 316 | ret 1 |
| 317 | end |
| 318 | put stctx 5 long init |
| 319 | name = new uchar 65536 0 |
| 320 | stpath name |
| 321 | if eq init 1 |
| 322 | v = fopen name ulong 0 ulong 8 int 559104 int 0 |
| 323 | if ne v long -2 |
| 324 | if ge v long 0 |
| 325 | r = fclose int v |
| 326 | end |
| 327 | r = stclose |
| 328 | ret 1 |
| 329 | end |
| 330 | else |
| 331 | if ne stload 0 |
| 332 | r = stclose |
| 333 | ret 1 |
| 334 | end |
| 335 | end |
| 336 | if ne ststale 0 |
| 337 | r = stclose |
| 338 | ret 1 |
| 339 | end |
| 340 | ret 0 |
| 341 | end |
| 342 | |
| 343 | fn stbegin to int |
| 344 | if eq get stctx 0 long 0 |
| 345 | ret 1 |
| 346 | end |
| 347 | if ne get stctx 4 long 0 |
| 348 | ret 1 |
| 349 | end |
| 350 | if ne get stctx 2 long 0 |
| 351 | ret 1 |
| 352 | end |
| 353 | if ne stdrop 3 0 |
| 354 | ret 1 |
| 355 | end |
| 356 | if ne ststale 0 |
| 357 | ret 1 |
| 358 | end |
| 359 | name = new uchar 65536 0 |
| 360 | stpath name |
| 361 | f = fopen name ulong 0 ulong 12 int 559298 int 384 |
| 362 | if lt f long 0 |
| 363 | ret 1 |
| 364 | end |
| 365 | put stctx 2 add f long 1 |
| 366 | put stctx 10 0 |
| 367 | put stctx 6 0 |
| 368 | put stctx 7 0 |
| 369 | for i ulong 32 |
| 370 | put sthash i 0 |
| 371 | end |
| 372 | if ne stmeta int f 0 0 |
| 373 | put stctx 10 1 |
| 374 | ret 1 |
| 375 | end |
| 376 | b = new uchar 65536 0 |
| 377 | stmagic b |
| 378 | if ne stwriteall int f b 16 0 |
| 379 | put stctx 10 1 |
| 380 | ret 1 |
| 381 | end |
| 382 | ret 0 |
| 383 | end |
| 384 | |
| 385 | fn stwrite b view uchar 65536 n ulong to int |
| 386 | if eq get stctx 2 long 0 |
| 387 | ret 1 |
| 388 | end |
| 389 | if ne get stctx 4 long 0 |
| 390 | ret 1 |
| 391 | end |
| 392 | if ne get stctx 10 long 0 |
| 393 | ret 1 |
| 394 | end |
| 395 | if eq n 0 |
| 396 | ret 0 |
| 397 | end |
| 398 | nb = stcharge get stctx 6 n |
| 399 | if gt nb 33554432 |
| 400 | put stctx 10 1 |
| 401 | ret 1 |
| 402 | end |
| 403 | nc = stcount get stctx 7 |
| 404 | if gt nc 65536 |
| 405 | put stctx 10 1 |
| 406 | ret 1 |
| 407 | end |
| 408 | h = new uchar 65536 0 |
| 409 | d = new uchar 32 0 |
| 410 | p = new uchar 32 0 |
| 411 | q = new uchar 32 0 |
| 412 | sthashdata b n d |
| 413 | stput64 h 0 n |
| 414 | for i ulong 32 |
| 415 | put h add i 8 get d i |
| 416 | end |
| 417 | f = int sub get stctx 2 long 1 |
| 418 | if ne stwriteall f h 40 0 |
| 419 | put stctx 10 1 |
| 420 | ret 1 |
| 421 | end |
| 422 | if ne stwriteall f b n 0 |
| 423 | put stctx 10 1 |
| 424 | ret 1 |
| 425 | end |
| 426 | put stctx 6 long nb |
| 427 | put stctx 7 long nc |
| 428 | stgethash 0 p |
| 429 | stchain p d n nb nc q |
| 430 | stsethash 0 q |
| 431 | ret 0 |
| 432 | end |
| 433 | |
| 434 | fn stabort to int |
| 435 | if ne get stctx 4 long 0 |
| 436 | ret 1 |
| 437 | end |
| 438 | if eq get stctx 2 long 0 |
| 439 | ret 0 |
| 440 | end |
| 441 | r = stdrop 2 |
| 442 | if ne ststale 0 |
| 443 | ret 1 |
| 444 | end |
| 445 | ret r |
| 446 | end |
| 447 | |
| 448 | fn stcommit to int |
| 449 | if eq get stctx 2 long 0 |
| 450 | ret 1 |
| 451 | end |
| 452 | if ne get stctx 4 long 0 |
| 453 | ret 1 |
| 454 | end |
| 455 | if ne get stctx 10 long 0 |
| 456 | ret 1 |
| 457 | end |
| 458 | f = int sub get stctx 2 long 1 |
| 459 | b = new uchar 65536 0 |
| 460 | for i ulong 32 |
| 461 | put b add i 8 get sthash i |
| 462 | end |
| 463 | stput64 b 40 ulong get stctx 6 |
| 464 | stput64 b 48 ulong get stctx 7 |
| 465 | if ne stwriteall f b 56 0 |
| 466 | put stctx 10 1 |
| 467 | ret 1 |
| 468 | end |
| 469 | note A failed commit cannot be attempted again on the same staged file. |
| 470 | put stctx 10 1 |
| 471 | if ne stsync f long 0 |
| 472 | ret 1 |
| 473 | end |
| 474 | if ne stvalidate f 0 |
| 475 | ret 1 |
| 476 | end |
| 477 | if ne strename long 0 |
| 478 | ret 1 |
| 479 | end |
| 480 | note No rollback, retry or serving is permitted after an uncertain commit. |
| 481 | put stctx 4 1 |
| 482 | if ne stsync int sub get stctx 0 long 1 long 0 |
| 483 | ret 2 |
| 484 | end |
| 485 | if ne stdrop 2 0 |
| 486 | ret 2 |
| 487 | end |
| 488 | put stctx 4 0 |
| 489 | put stctx 5 0 |
| 490 | ret 0 |
| 491 | end |
| 492 | |
| 493 | note First cold-start epoch only. Caller must exec immediately or exit on failure. |
| 494 | fn stpass dirtgt int locktgt int to int |
| 495 | if eq get stctx 0 long 0 |
| 496 | ret 1 |
| 497 | end |
| 498 | if eq get stctx 1 long 0 |
| 499 | ret 1 |
| 500 | end |
| 501 | if ne get stctx 4 long 0 |
| 502 | ret 1 |
| 503 | end |
| 504 | if ne get stctx 5 long 0 |
| 505 | ret 1 |
| 506 | end |
| 507 | if ne get stctx 2 long 0 |
| 508 | ret 1 |
| 509 | end |
| 510 | if ne get stctx 12 long 0 |
| 511 | ret 1 |
| 512 | end |
| 513 | if ne get stctx 13 long 0 |
| 514 | ret 1 |
| 515 | end |
| 516 | if ne get stctx 3 long 0 |
| 517 | if eq get stctx 11 long 0 |
| 518 | ret 1 |
| 519 | end |
| 520 | end |
| 521 | if eq dirtgt locktgt |
| 522 | ret 1 |
| 523 | end |
| 524 | if ne stunused dirtgt 0 |
| 525 | ret 1 |
| 526 | end |
| 527 | if ne stunused locktgt 0 |
| 528 | ret 1 |
| 529 | end |
| 530 | if ne stdrop 3 0 |
| 531 | ret 1 |
| 532 | end |
| 533 | note Poison while constructing handoff; any partial failure is fail-stop. |
| 534 | put stctx 4 1 |
| 535 | r = dupfd int sub get stctx 0 long 1 dirtgt int 0 |
| 536 | if ne r long dirtgt |
| 537 | ret 1 |
| 538 | end |
| 539 | put stctx 12 add long dirtgt long 1 |
| 540 | r = dupfd int sub get stctx 1 long 1 locktgt int 0 |
| 541 | if ne r long locktgt |
| 542 | ret 1 |
| 543 | end |
| 544 | put stctx 13 add long locktgt long 1 |
| 545 | note Keep poisoned after handoff: no new storage work may race the exec boundary. |
| 546 | ret 0 |
| 547 | end |
| 548 | |
| 549 | note Structural adoption only; same-UID/root launcher and trusted procfs are prerequisites. |
| 550 | fn stresume dirfd int lockfd int to int |
| 551 | if ne get stctx 0 long 0 |
| 552 | ret 1 |
| 553 | end |
| 554 | if lt dirfd 3 |
| 555 | ret 1 |
| 556 | end |
| 557 | if lt lockfd 3 |
| 558 | ret 1 |
| 559 | end |
| 560 | if eq dirfd lockfd |
| 561 | ret 1 |
| 562 | end |
| 563 | if ne stmeta dirfd 1 0 |
| 564 | ret 1 |
| 565 | end |
| 566 | if ne stlockmeta dirfd lockfd 0 |
| 567 | ret 1 |
| 568 | end |
| 569 | put stctx 0 add long dirfd long 1 |
| 570 | put stctx 1 add long lockfd long 1 |
| 571 | put stctx 4 0 |
| 572 | put stctx 5 0 |
| 573 | if ne fchdir dirfd long 0 |
| 574 | r = stclose |
| 575 | ret 1 |
| 576 | end |
| 577 | if ne stload 0 |
| 578 | r = stclose |
| 579 | ret 1 |
| 580 | end |
| 581 | ret 0 |
| 582 | end |