interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / aienux / disk / disk_file.om

204 lines · 3881 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note aienux/disk/disk_file.om: Pure-Omega Hosted File-Backed Disk Backend with Power-Cut Injection.
3note Converted from upstream aienux native/disk/disk_file.{h,c}.
4note Compose with: si/directives/om2.om aienux/disk/disk.om aienux/disk/disk_file.om aienux/disk/disk_sdhci.om <test.om>
5
6note Linux AArch64 open(2) flags: O_RDWR=2, O_CREAT=64, O_TRUNC=512
7const DFILE_O_RDWR int 2
8const DFILE_O_CREATE_TRUNC int 578
9const DFILE_MODE_0600 int 384
10const DFILE_EINTR long -4
11
12note File backend state
13global df_fd int 1
14global df_bsz uint 1
15global df_bcnt ulong 1
16global df_cut_armed uchar 1
17global df_cut_rem ulong 1
18global df_written ulong 1
19global df_flushes ulong 1
20
21fn df_full_pread fd int buf edit uchar * off ulong nbytes ulong pos ulong to int
22done = ulong 0
23while lt done nbytes bound 65536
24k = pread fd buf add off done sub nbytes done long add pos done
25if eq k DFILE_EINTR
26continue
27end
28if le k 0
29ret -1
30end
31done = add done ulong k
32end
33ret 0
34end
35
36fn df_full_pwrite fd int buf view uchar * off ulong nbytes ulong pos ulong to int
37done = ulong 0
38while lt done nbytes bound 65536
39k = pwrite fd buf add off done sub nbytes done long add pos done
40if eq k DFILE_EINTR
41continue
42end
43if le k 0
44ret -1
45end
46done = add done ulong k
47end
48ret 0
49end
50
51fn df_stat_size fd int to ulong
52stbuf = new uchar 128 0
53sr = fstat fd stbuf 0 128
54if lt sr 0
55ret 0
56end
57sz = ulong 0
58for i ulong 8
59b = ulong get stbuf add 48 i
60sz = or sz shl b mul i 8
61end
62ret sz
63end
64
65fn disk_file_open dev edit DiskDev path view uchar * bsz uint bcnt ulong create uint to int
66if and ne bsz 512 ne bsz 4096
67ret DISK_EGEOMETRY
68end
69put df_fd 0 -1
70put df_bsz 0 0
71put df_bcnt 0 0
72put df_cut_armed 0 0
73put df_cut_rem 0 0
74put df_written 0 0
75put df_flushes 0 0
76flags = DFILE_O_RDWR
77if ne create 0
78flags = DFILE_O_CREATE_TRUNC
79end
80fd_r = fopen path 0 len path flags DFILE_MODE_0600
81if lt fd_r 0
82ret DISK_EIO
83end
84fd = int fd_r
85if ne create 0
86if eq bcnt 0
87cl = fclose fd
88ret DISK_EIO
89end
90total = mul bcnt ulong bsz
91tr = ftrunc fd long total
92if lt tr 0
93cl = fclose fd
94ret DISK_EIO
95end
96else
97sz = df_stat_size fd
98if or eq sz 0 ne rem sz ulong bsz 0
99cl = fclose fd
100ret DISK_EGEOMETRY
101end
102fblks = div sz ulong bsz
103if eq bcnt 0
104bcnt = fblks
105else
106if ne bcnt fblks
107cl = fclose fd
108ret DISK_EGEOMETRY
109end
110end
111end
112put df_fd 0 fd
113put df_bsz 0 bsz
114put df_bcnt 0 bcnt
115put dev backend DISK_BACKEND_FILE
116put dev block_size bsz
117put dev block_count bcnt
118put dev max_blocks 256
119ret DISK_OK
120end
121
122fn disk_file_close to void
123fd = get df_fd 0
124if ge fd 0
125cl = fclose fd
126put df_fd 0 -1
127end
128end
129
130fn disk_file_arm_cut blocks_allowed ulong to void
131put df_cut_armed 0 1
132put df_cut_rem 0 blocks_allowed
133end
134
135fn disk_file_disarm to void
136put df_cut_armed 0 0
137put df_cut_rem 0 0
138end
139
140fn disk_file_blocks_written to ulong
141ret get df_written 0
142end
143
144fn disk_file_flushes to ulong
145ret get df_flushes 0
146end
147
148fn disk_file_read lba ulong cnt uint buf edit uchar * off ulong to int
149fd = get df_fd 0
150if lt fd 0
151ret DISK_ESTATE
152end
153bsz = ulong get df_bsz 0
154nbytes = mul ulong cnt bsz
155pos = mul lba bsz
156rc = df_full_pread fd buf off nbytes pos
157if ne rc 0
158ret DISK_EIO
159end
160ret DISK_OK
161end
162
163fn disk_file_write lba ulong cnt uint buf view uchar * off ulong to int
164fd = get df_fd 0
165if lt fd 0
166ret DISK_ESTATE
167end
168bsz = ulong get df_bsz 0
169i = uint 0
170while lt i cnt bound 65536
171if ne get df_cut_armed 0 0
172rem_b = get df_cut_rem 0
173if eq rem_b 0
174ret DISK_EPOWER
175end
176put df_cut_rem 0 sub rem_b 1
177end
178boff = add off mul ulong i bsz
179pos = mul add lba ulong i bsz
180rc = df_full_pwrite fd buf boff bsz pos
181if ne rc 0
182ret DISK_EIO
183end
184put df_written 0 add get df_written 0 1
185i = add i 1
186end
187ret DISK_OK
188end
189
190fn disk_file_flush to int
191fd = get df_fd 0
192if lt fd 0
193ret DISK_ESTATE
194end
195if and ne get df_cut_armed 0 0 eq get df_cut_rem 0 0
196ret DISK_EPOWER
197end
198put df_flushes 0 add get df_flushes 0 1
199rc = fdsync fd
200if lt rc 0
201ret DISK_EIO
202end
203ret DISK_OK
204end