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