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