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