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