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