aboutsummaryrefslogtreecommitdiff
path: root/src/raid-5-xor-12b-6d.c
diff options
context:
space:
mode:
authorLibravatar Martin Michalec <martin@michalec.dev>2026-11-07 07:26:34 +0000
committerLibravatar Martin Michalec <martin@michalec.dev>2026-11-07 08:38:33 +0000
commit7fe23e4a8fa6fce19fb3d5e3a8a1f5472496c928 (patch)
tree64c163a368fa600d6054796e05a4d8847ed1cd53 /src/raid-5-xor-12b-6d.c
downloadlab-7fe23e4a8fa6fce19fb3d5e3a8a1f5472496c928.tar.gz
publishHEADmaster
Diffstat (limited to 'src/raid-5-xor-12b-6d.c')
-rw-r--r--src/raid-5-xor-12b-6d.c380
1 files changed, 380 insertions, 0 deletions
diff --git a/src/raid-5-xor-12b-6d.c b/src/raid-5-xor-12b-6d.c
new file mode 100644
index 0000000..d10721d
--- /dev/null
+++ b/src/raid-5-xor-12b-6d.c
@@ -0,0 +1,380 @@
1// M1M2M3 M4M5M6 M7M8M9 MAMBMC 000000 P1P2P3
2// M1M2M3 M4M5M6 M7M8M9 MAMBMC P1P2P3 000000
3// M1M2M3 M4M5M6 M7M8M9 P1P2P3 MAMBMC 000000
4// M1M2M3 M4M5M6 P1P2P3 M7M8M9 MAMBMC 000000
5// M1M2M3 P1P2P3 M4M5M6 M7M8M9 MAMBMC 000000
6// P1P2P3 M1M2M3 M4M5M6 M7M8M9 MAMBMC 000000
7// M1M2M3 M4M5M6 M7M8M9 MAMBMC 000000 P1P2P3
8#include <stdlib.h>
9#include <stdio.h>
10#include <stdint.h>
11#include <stdbool.h>
12#include <string.h>
13#include <ctype.h>
14
15#define DISK_COUNT 6
16#define MESSAGE_SIZE 12
17#define BLOCK_SIZE 3
18#define PADDING_SIZE 3
19#define PARITY_SIZE 3
20
21#define DISK_SIZE 0x40
22
23#define POPCOUNT __builtin_popcount
24#define CTZ __builtin_ctz
25
26enum command {
27 COMMAND_WRITE,
28 COMMAND_READ,
29};
30
31FILE *disks[DISK_COUNT];
32
33#ifndef WIN32
34#define LINE_SIZE (2*BLOCK_SIZE + 1)
35#else
36#define LINE_SIZE (2*BLOCK_SIZE + 2)
37static ssize_t
38getline (char **restrict lineptr, size_t *restrict n, FILE *restrict stream) {
39 register char c;
40 register char *cs = NULL;
41 register int length = 0;
42 while ((c = getc (stream)) != EOF) {
43 cs = (char *) realloc (cs, ++length + 1);
44 if ((*(cs + length - 1) = c) == '\n') {
45 *(cs + length) = '\0';
46 break;
47 }
48 }
49
50 // return the allocated memory if lineptr is null
51 if ((*lineptr) == NULL) {
52 *lineptr = cs;
53 } else {
54 // check if enough memory is allocated
55 if ((length + 1) < *n)
56 *lineptr = (char *) realloc (*lineptr, length + 1);
57 memcpy (*lineptr, cs, length);
58 free (cs);
59 }
60
61 return (ssize_t) (*n = strlen (*lineptr));
62}
63#endif
64
65static void
66open_disks (uint8_t address)
67{
68 uint16_t bad_disks = 0;
69 for (int i = 0; i < DISK_COUNT; ++i) {
70 char filename[] = "disk0";
71 filename[strlen (filename) - 1] += i;
72 disks[i] = fopen (filename, "r+");
73 if (!disks[i]) { // disk file was deleted
74 disks[i] = fopen (filename, "w+");
75 if (!disks[i]) {
76 fprintf (stderr, "ERROR: unable to open file: %s\n", filename);
77 exit (EXIT_FAILURE);
78 }
79
80 fprintf (stderr, "WARNING: found bad disk (missing): %s\n", filename);
81 bad_disks |= 1 << i;
82 } else { // disk file wasn't deleted
83 fseek (disks[i], 0, SEEK_END);
84 if (ftell (disks[i]) != DISK_SIZE*LINE_SIZE) { // disk file has bad format
85 fprintf (stderr, "WARNING: found bad disk (corrupted): %s\n", filename);
86 bad_disks |= 1 << i;
87 } rewind (disks[i]);
88 }
89 }
90
91 // there were bad disks
92 int bad_disk_count = POPCOUNT (bad_disks);
93 if (bad_disk_count == 1) { // recompute lost disk
94 fprintf (stderr, "INFO: there is just 1 bad disk, recomputing it...\n");
95 int bad_disk = CTZ (bad_disks);
96
97 for (int j = 0; j < DISK_SIZE; ++j) {
98 uint8_t block_bytes[MESSAGE_SIZE + PADDING_SIZE + PARITY_SIZE] = {0};
99
100 // read blocks from good disks
101 bool empty = false;
102 for (int k = 0; k < DISK_COUNT; ++k) {
103 if (k == bad_disk) continue;
104 int c = fgetc (disks[k]);
105 if (c == ' ') {
106 fgetc (disks[k]);
107
108 fgetc (disks[k]);
109 fgetc (disks[k]);
110
111 fgetc (disks[k]);
112 fgetc (disks[k]);
113 empty = true;
114 } else {
115 ungetc(c, disks[k]);
116 fscanf(disks[k], "%02hhX%02hhX%02hhX",
117 &block_bytes[k * BLOCK_SIZE + 0],
118 &block_bytes[k * BLOCK_SIZE + 1],
119 &block_bytes[k * BLOCK_SIZE + 2]);
120 } fgetc (disks[k]);
121 }
122
123 if (empty) { // address empty
124 for (int l = 0; l < BLOCK_SIZE; ++l) {
125 fprintf (disks[bad_disk], " ");
126 } fprintf (disks[bad_disk], "\n");
127 continue;
128 }
129
130 // compute lost block
131 block_bytes[bad_disk*BLOCK_SIZE + 0] = 0x00;
132 block_bytes[bad_disk*BLOCK_SIZE + 1] = 0x00;
133 block_bytes[bad_disk*BLOCK_SIZE + 2] = 0x00;
134 for (int k = 0; k < DISK_COUNT; ++k) {
135 if (k == bad_disk) continue;
136 block_bytes[bad_disk*BLOCK_SIZE + 0] ^= block_bytes[k*BLOCK_SIZE + 0];
137 block_bytes[bad_disk*BLOCK_SIZE + 1] ^= block_bytes[k*BLOCK_SIZE + 1];
138 block_bytes[bad_disk*BLOCK_SIZE + 2] ^= block_bytes[k*BLOCK_SIZE + 2];
139 }
140
141 // write lost block
142 fprintf (disks[bad_disk], "%02hhX%02hhX%02hhX\n",
143 block_bytes[bad_disk*BLOCK_SIZE + 0],
144 block_bytes[bad_disk*BLOCK_SIZE + 1],
145 block_bytes[bad_disk*BLOCK_SIZE + 2]);
146 }
147 } else if (bad_disk_count > 1) { // replace all disks with empty ones, all data is lost
148 fprintf (stderr, "ERROR: too many bad disks, reinitializing the array with new disks...\n");
149
150 for (int i = 0; i < DISK_COUNT; ++i) {
151 for (int j = 0; j < DISK_SIZE; ++j)
152 fputs (" \n", disks[i]);
153 }
154 }
155
156 // position head for writing/reading message
157 for (int i = 0; i < DISK_COUNT; ++i)
158 fseek (disks[i], address*LINE_SIZE, SEEK_SET);
159}
160
161static void
162close_disks (void)
163{
164 for (int i = 0; i < DISK_COUNT; ++i)
165 fclose (disks[i]);
166}
167
168int
169main (void)
170{
171 fprintf (stderr, "INFO: initializing RAID 5 XOR on 5 disks with 12 byte messages...\n");
172 // check disk integrity on startup
173 open_disks (0);
174 close_disks ( );
175
176 for (;;) {
177 char *line = NULL;
178 size_t line_size = 0;
179 const char *head;
180 int assigned;
181 int scanned;
182
183 // read whole line
184 printf ("> ");
185 scanned = getline (&line, &line_size, stdin);
186 if (scanned == -1)
187 exit (EXIT_SUCCESS);
188 else if (scanned == 1)
189 continue;
190 head = line;
191
192 uint8_t address; { // scan address
193 assigned = sscanf (head, "%hhx%n", &address, &scanned);
194 if (assigned != 1 || address >= DISK_SIZE) {
195 fprintf(stderr, "ERROR: address not in set [0x00; 0x40)\n");
196 continue;
197 } head += scanned;
198 }
199
200 enum command command; { // scan command
201 char command_buffer[6] = {0};
202 assigned = sscanf (head, "%5s %n", command_buffer, &scanned);
203 if (assigned != 1 ||
204 (!strncmp (command_buffer, "read", 4) &&
205 !strncmp (command_buffer, "write", 5))) {
206 fprintf(stderr, "ERROR: command not in set (read; write)\n");
207 continue;
208 } head += scanned;
209 command = command_buffer[0] == 'r' ? COMMAND_READ : COMMAND_WRITE;
210 }
211
212 switch (command) {
213 case COMMAND_READ: {
214 uint8_t message[MESSAGE_SIZE];
215 uint8_t padding[PADDING_SIZE];
216 uint8_t parity [ PARITY_SIZE];
217
218 // parse out message
219 open_disks (address);
220 int c = fgetc (disks[0]);
221 if (c == ' ') {
222 close_disks ();
223 printf ("EMPTY!\n");
224 break;
225 } ungetc (c, disks[0]);
226
227 switch (address % DISK_COUNT) {
228 case 0:
229 fscanf (disks[0], "%02hhX%02hhX%02hhX\n", &message[0x0], &message[0x1], &message[0x2]);
230 fscanf (disks[1], "%02hhX%02hhX%02hhX\n", &message[0x3], &message[0x4], &message[0x5]);
231 fscanf (disks[2], "%02hhX%02hhX%02hhX\n", &message[0x6], &message[0x7], &message[0x8]);
232 fscanf (disks[3], "%02hhX%02hhX%02hhX\n", &message[0x9], &message[0xA], &message[0xB]);
233 fscanf (disks[4], "%02hhX%02hhX%02hhX\n", &padding[0x0], &padding[0x1], &padding[0x2]);
234 fscanf (disks[5], "%02hhX%02hhX%02hhX\n", &parity [0x0], &parity [0x1], &parity [0x2]);
235 break;
236 case 1:
237 fscanf (disks[0], "%02hhX%02hhX%02hhX\n", &message[0x0], &message[0x1], &message[0x2]);
238 fscanf (disks[1], "%02hhX%02hhX%02hhX\n", &message[0x3], &message[0x4], &message[0x5]);
239 fscanf (disks[2], "%02hhX%02hhX%02hhX\n", &message[0x6], &message[0x7], &message[0x8]);
240 fscanf (disks[3], "%02hhX%02hhX%02hhX\n", &message[0x9], &message[0xA], &message[0xB]);
241 fscanf (disks[4], "%02hhX%02hhX%02hhX\n", &parity [0x0], &parity [0x1], &parity [0x2]);
242 fscanf (disks[5], "%02hhX%02hhX%02hhX\n", &padding[0x0], &padding[0x1], &padding[0x2]);
243 break;
244 case 2:
245 fscanf (disks[0], "%02hhX%02hhX%02hhX\n", &message[0x0], &message[0x1], &message[0x2]);
246 fscanf (disks[1], "%02hhX%02hhX%02hhX\n", &message[0x3], &message[0x4], &message[0x5]);
247 fscanf (disks[2], "%02hhX%02hhX%02hhX\n", &message[0x6], &message[0x7], &message[0x8]);
248 fscanf (disks[3], "%02hhX%02hhX%02hhX\n", &padding[0x0], &padding[0x1], &padding[0x2]);
249 fscanf (disks[4], "%02hhX%02hhX%02hhX\n", &message[0x9], &message[0xA], &message[0xB]);
250 fscanf (disks[5], "%02hhX%02hhX%02hhX\n", &parity [0x0], &parity [0x1], &parity [0x2]);
251 break;
252 case 3:
253 fscanf (disks[0], "%02hhX%02hhX%02hhX\n", &message[0x0], &message[0x1], &message[0x2]);
254 fscanf (disks[1], "%02hhX%02hhX%02hhX\n", &message[0x3], &message[0x4], &message[0x5]);
255 fscanf (disks[2], "%02hhX%02hhX%02hhX\n", &parity [0x0], &parity [0x1], &parity [0x2]);
256 fscanf (disks[3], "%02hhX%02hhX%02hhX\n", &message[0x6], &message[0x7], &message[0x8]);
257 fscanf (disks[4], "%02hhX%02hhX%02hhX\n", &message[0x9], &message[0xA], &message[0xB]);
258 fscanf (disks[5], "%02hhX%02hhX%02hhX\n", &padding[0x0], &padding[0x1], &padding[0x2]);
259 break;
260 case 4:
261 fscanf (disks[0], "%02hhX%02hhX%02hhX\n", &message[0x0], &message[0x1], &message[0x2]);
262 fscanf (disks[1], "%02hhX%02hhX%02hhX\n", &parity [0x0], &parity [0x1], &parity [0x2]);
263 fscanf (disks[2], "%02hhX%02hhX%02hhX\n", &message[0x3], &message[0x4], &message[0x5]);
264 fscanf (disks[3], "%02hhX%02hhX%02hhX\n", &message[0x6], &message[0x7], &message[0x8]);
265 fscanf (disks[4], "%02hhX%02hhX%02hhX\n", &message[0x9], &message[0xA], &message[0xB]);
266 fscanf (disks[5], "%02hhX%02hhX%02hhX\n", &padding[0x0], &padding[0x1], &padding[0x2]);
267 break;
268 case 5:
269 fscanf (disks[0], "%02hhX%02hhX%02hhX\n", &parity [0x0], &parity [0x1], &parity [0x2]);
270 fscanf (disks[1], "%02hhX%02hhX%02hhX\n", &message[0x0], &message[0x1], &message[0x2]);
271 fscanf (disks[2], "%02hhX%02hhX%02hhX\n", &message[0x3], &message[0x4], &message[0x5]);
272 fscanf (disks[3], "%02hhX%02hhX%02hhX\n", &message[0x6], &message[0x7], &message[0x8]);
273 fscanf (disks[4], "%02hhX%02hhX%02hhX\n", &message[0x9], &message[0xA], &message[0xB]);
274 fscanf (disks[5], "%02hhX%02hhX%02hhX\n", &padding[0x0], &padding[0x1], &padding[0x2]);
275 break;
276 } close_disks ();
277
278 printf ("%02hhX%02hhX%02hhX %02hhX%02hhX%02hhX "
279 "%02hhX%02hhX%02hhX %02hhX%02hhX%02hhX\n",
280 message[0x0], message[0x1], message[0x2], message[0x3], message[0x4], message[0x5],
281 message[0x6], message[0x7], message[0x8], message[0x9], message[0xA], message[0xB]);
282 } break;
283 case COMMAND_WRITE: {
284 uint8_t message[MESSAGE_SIZE] = {0}; { // scan message
285 int message_size = 0;
286
287 while (*head == ' ' ||
288 *head == '\t') ++head;
289 const char *invalid = NULL;
290 size_t len = 0;
291 for (const char *c = head; *c && *c != '\r' && *c != '\n'; ++c) {
292 if (!strchr ("0123456789abcdefABCDEF", *c)) {
293 invalid = c;
294 break;
295 } len += 1;
296 } if (invalid) {
297 fprintf (stderr, "ERROR: message contains invalid character `%c'\n", *invalid);
298 break;
299 } if (len != 2*MESSAGE_SIZE) {
300 fprintf (stderr, "ERROR: expected %d hex digits for message, received %zu\n", 2*MESSAGE_SIZE, len);
301 break;
302 }
303
304 while (sscanf (head, "%2hhx%n", &message[message_size], &scanned) == 1 &&
305 message_size < MESSAGE_SIZE) {
306 head += scanned;
307 message_size += 1;
308 }
309 }
310
311 // compute parity
312 uint8_t padding[PADDING_SIZE] = {
313 0x00,
314 0x00,
315 0x00,
316 };
317 uint8_t parity[PARITY_SIZE] = {
318 message[0x0] ^ message[0x3] ^ message[0x6] ^ message[0x9],
319 message[0x1] ^ message[0x4] ^ message[0x7] ^ message[0xA],
320 message[0x2] ^ message[0x5] ^ message[0x8] ^ message[0xB],
321 };
322
323 // write blocks
324 open_disks (address); // open disk files, ensuring their integrity
325 switch (address % DISK_COUNT) {
326 case 0:
327 fprintf (disks[0], "%02hhX%02hhX%02hhX\n", message[0x0], message[0x1], message[0x2]);
328 fprintf (disks[1], "%02hhX%02hhX%02hhX\n", message[0x3], message[0x4], message[0x5]);
329 fprintf (disks[2], "%02hhX%02hhX%02hhX\n", message[0x6], message[0x7], message[0x8]);
330 fprintf (disks[3], "%02hhX%02hhX%02hhX\n", message[0x9], message[0xA], message[0xB]);
331 fprintf (disks[4], "%02hhX%02hhX%02hhX\n", padding[0x0], padding[0x1], padding[0x2]);
332 fprintf (disks[5], "%02hhX%02hhX%02hhX\n", parity [0x0], parity [0x1], parity [0x2]);
333 break;
334 case 1:
335 fprintf (disks[0], "%02hhX%02hhX%02hhX\n", message[0x0], message[0x1], message[0x2]);
336 fprintf (disks[1], "%02hhX%02hhX%02hhX\n", message[0x3], message[0x4], message[0x5]);
337 fprintf (disks[2], "%02hhX%02hhX%02hhX\n", message[0x6], message[0x7], message[0x8]);
338 fprintf (disks[3], "%02hhX%02hhX%02hhX\n", message[0x9], message[0xA], message[0xB]);
339 fprintf (disks[4], "%02hhX%02hhX%02hhX\n", parity [0x0], parity [0x1], parity [0x2]);
340 fprintf (disks[5], "%02hhX%02hhX%02hhX\n", padding[0x0], padding[0x1], padding[0x2]);
341 break;
342 case 2:
343 fprintf (disks[0], "%02hhX%02hhX%02hhX\n", message[0x0], message[0x1], message[0x2]);
344 fprintf (disks[1], "%02hhX%02hhX%02hhX\n", message[0x3], message[0x4], message[0x5]);
345 fprintf (disks[2], "%02hhX%02hhX%02hhX\n", message[0x6], message[0x7], message[0x8]);
346 fprintf (disks[3], "%02hhX%02hhX%02hhX\n", parity [0x0], parity [0x1], parity [0x2]);
347 fprintf (disks[4], "%02hhX%02hhX%02hhX\n", message[0x9], message[0xA], message[0xB]);
348 fprintf (disks[5], "%02hhX%02hhX%02hhX\n", padding[0x0], padding[0x1], padding[0x2]);
349 break;
350 case 3:
351 fprintf (disks[0], "%02hhX%02hhX%02hhX\n", message[0x0], message[0x1], message[0x2]);
352 fprintf (disks[1], "%02hhX%02hhX%02hhX\n", message[0x3], message[0x4], message[0x5]);
353 fprintf (disks[2], "%02hhX%02hhX%02hhX\n", parity [0x0], parity [0x1], parity [0x2]);
354 fprintf (disks[3], "%02hhX%02hhX%02hhX\n", message[0x6], message[0x7], message[0x8]);
355 fprintf (disks[4], "%02hhX%02hhX%02hhX\n", message[0x9], message[0xA], message[0xB]);
356 fprintf (disks[5], "%02hhX%02hhX%02hhX\n", padding[0x0], padding[0x1], padding[0x2]);
357 break;
358 case 4:
359 fprintf (disks[0], "%02hhX%02hhX%02hhX\n", message[0x0], message[0x1], message[0x2]);
360 fprintf (disks[1], "%02hhX%02hhX%02hhX\n", parity [0x0], parity [0x1], parity [0x2]);
361 fprintf (disks[2], "%02hhX%02hhX%02hhX\n", message[0x3], message[0x4], message[0x5]);
362 fprintf (disks[3], "%02hhX%02hhX%02hhX\n", message[0x6], message[0x7], message[0x8]);
363 fprintf (disks[4], "%02hhX%02hhX%02hhX\n", message[0x9], message[0xA], message[0xB]);
364 fprintf (disks[5], "%02hhX%02hhX%02hhX\n", padding[0x0], padding[0x1], padding[0x2]);
365 break;
366 case 5:
367 fprintf (disks[0], "%02hhX%02hhX%02hhX\n", parity [0x0], parity [0x1], parity [0x2]);
368 fprintf (disks[1], "%02hhX%02hhX%02hhX\n", message[0x0], message[0x1], message[0x2]);
369 fprintf (disks[2], "%02hhX%02hhX%02hhX\n", message[0x3], message[0x4], message[0x5]);
370 fprintf (disks[3], "%02hhX%02hhX%02hhX\n", message[0x6], message[0x7], message[0x8]);
371 fprintf (disks[4], "%02hhX%02hhX%02hhX\n", message[0x9], message[0xA], message[0xB]);
372 fprintf (disks[5], "%02hhX%02hhX%02hhX\n", padding[0x0], padding[0x1], padding[0x2]);
373 break;
374 } close_disks ();
375 } break;
376 } free (line);
377 }
378
379 return EXIT_SUCCESS;
380}