interplane.Get Omega
Read in your language

omnet

aien-dev/omnet

Brought by drakestapleton

branch main Branches Tags History

omnet / om / store / storepool.om

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