// gdluck // M1M2M3M4M5 M6M7M8M9MA P1P2P3P4P5 #include #include #include #include #include #define DISK_COUNT 3 #define MESSAGE_SIZE 10 #define BLOCK_SIZE 5 #define DISK_SIZE 0x40 #define POPCOUNT __builtin_popcount #define CTZ __builtin_ctz enum command { COMMAND_WRITE, COMMAND_READ, }; FILE *disks[DISK_COUNT]; #ifndef WIN32 #define LINE_SIZE 11 #else #define LINE_SIZE 12 static ssize_t getline (char **restrict lineptr, size_t *restrict n, FILE *restrict stream) { register char c; register char *cs = NULL; register int length = 0; while ((c = getc (stream)) != EOF) { cs = (char *) realloc (cs, ++length + 1); if ((*(cs + length - 1) = c) == '\n') { *(cs + length) = '\0'; break; } } // return the allocated memory if lineptr is null if ((*lineptr) == NULL) { *lineptr = cs; } else { // check if enough memory is allocated if ((length + 1) < *n) *lineptr = (char *) realloc (*lineptr, length + 1); memcpy (*lineptr, cs, length); free (cs); } return (ssize_t) (*n = strlen (*lineptr)); } #endif static void open_disks (uint8_t address) { uint16_t bad_disks = 0; for (int i = 0; i < DISK_COUNT; ++i) { char filename[] = "disk0"; filename[strlen (filename) - 1] += i; disks[i] = fopen (filename, "r+"); if (!disks[i]) { // disk file was deleted disks[i] = fopen (filename, "w+"); if (!disks[i]) { fprintf (stderr, "error: can't open the file: %s\n", filename); exit (EXIT_FAILURE); } fprintf (stderr, "warning: a bad disk has been found (missing): %s\n", filename); bad_disks |= 1 << i; } else { // disk file wasn't deleted fseek (disks[i], 0, SEEK_END); if (ftell (disks[i]) != DISK_SIZE*LINE_SIZE) { // disk file has bad format fprintf (stderr, "warning: a bad disk has been found (corrupted): %s\n", filename); bad_disks |= 1 << i; } rewind (disks[i]); } } // there were bad disks int bad_disk_count = POPCOUNT (bad_disks); if (bad_disk_count == 1) { // recompute lost disk fprintf (stderr, "information: there is only 1 bad disk, recomputing it...\n"); int bad_disk = CTZ (bad_disks); for (int j = 0; j < DISK_SIZE; ++j) { uint8_t block_bytes[BLOCK_SIZE*DISK_COUNT]; // read blocks from good disks for (int k = 0; k < DISK_COUNT; ++k) { if (k == bad_disk) continue; fscanf (disks[k], "%02hhX%02hhX%02hhX%02hhX%02hhX\n", &block_bytes[k*BLOCK_SIZE + 0], &block_bytes[k*BLOCK_SIZE + 1], &block_bytes[k*BLOCK_SIZE + 2], &block_bytes[k*BLOCK_SIZE + 3], &block_bytes[k*BLOCK_SIZE + 4]); } // compute lost block block_bytes[bad_disk*BLOCK_SIZE + 0] = 0x00; block_bytes[bad_disk*BLOCK_SIZE + 1] = 0x00; block_bytes[bad_disk*BLOCK_SIZE + 2] = 0x00; block_bytes[bad_disk*BLOCK_SIZE + 3] = 0x00; block_bytes[bad_disk*BLOCK_SIZE + 4] = 0x00; for (int k = 0; k < DISK_COUNT; ++k) { if (k == bad_disk) continue; block_bytes[bad_disk*BLOCK_SIZE + 0] ^= block_bytes[k*BLOCK_SIZE + 0]; block_bytes[bad_disk*BLOCK_SIZE + 1] ^= block_bytes[k*BLOCK_SIZE + 1]; block_bytes[bad_disk*BLOCK_SIZE + 2] ^= block_bytes[k*BLOCK_SIZE + 2]; block_bytes[bad_disk*BLOCK_SIZE + 3] ^= block_bytes[k*BLOCK_SIZE + 3]; block_bytes[bad_disk*BLOCK_SIZE + 4] ^= block_bytes[k*BLOCK_SIZE + 4]; } // write lost block fprintf (disks[bad_disk], "%02hhX%02hhX%02hhX%02hhX%02hhX\n", block_bytes[bad_disk*BLOCK_SIZE + 0], block_bytes[bad_disk*BLOCK_SIZE + 1], block_bytes[bad_disk*BLOCK_SIZE + 2], block_bytes[bad_disk*BLOCK_SIZE + 3], block_bytes[bad_disk*BLOCK_SIZE + 4]); } } else if (bad_disk_count > 1) { // replace all disks with empty ones, all data is lost fprintf (stderr, "error: too many damaged disks, reinitializing the array with new disks...\n"); for (int i = 0; i < DISK_COUNT; ++i) { for (int j = 0; j < DISK_SIZE; ++j) fputs ("0000000000\n", disks[i]); } } // position head for writing/reading message for (int i = 0; i < DISK_COUNT; ++i) fseek (disks[i], address*LINE_SIZE, SEEK_SET); } static void close_disks (void) { for (int i = 0; i < DISK_COUNT; ++i) fclose (disks[i]); } int main (void) { fprintf (stderr, "information: initializing RAID 4 on 3 disks with 10 byte messages...\n"); // check disk integrity on startup open_disks (0); close_disks ( ); for (;;) { char *line = NULL; size_t line_size = 0; const char *head; int assigned; int scanned; // read whole line printf ("> "); scanned = getline (&line, &line_size, stdin); if (scanned == -1) exit (EXIT_SUCCESS); else if (scanned == 1) continue; head = line; uint8_t address; { // scan address assigned = sscanf (head, "%hhx%n", &address, &scanned); if (assigned != 1 || address >= DISK_SIZE) { fprintf(stderr, "error: the address is not in set [0x00; 0x40)\n"); continue; } head += scanned; } enum command command; { // scan command char command_buffer[6] = {0}; assigned = sscanf (head, "%5s %n", command_buffer, &scanned); if (assigned != 1 || (!strncmp (command_buffer, "read", 4) && !strncmp (command_buffer, "write", 5))) { fprintf(stderr, "error: the command is not in set (read; write)\n"); continue; } head += scanned; command = command_buffer[0] == 'r' ? COMMAND_READ : COMMAND_WRITE; } switch (command) { case COMMAND_READ: { uint8_t message[MESSAGE_SIZE]; uint8_t parity [ BLOCK_SIZE]; // parse out message open_disks (address); fscanf (disks[0], "%02hhX%02hhX%02hhX%02hhX%02hhX\n", &message[0x0], &message[0x1], &message[0x2], &message[0x3], &message[0x4]); fscanf (disks[1], "%02hhX%02hhX%02hhX%02hhX%02hhX\n", &message[0x5], &message[0x6], &message[0x7], &message[0x8], &message[0x9]); fscanf (disks[2], "%02hhX%02hhX%02hhX%02hhX%02hhX\n", &parity [0x0], &parity [0x1], &parity [0x2], &parity [0x3], &parity [0x4]); close_disks (); printf ("%02hhX%02hhX%02hhX%02hhX%02hhX %02hhX%02hhX%02hhX%02hhX%02hhX\n", message[0x0], message[0x1], message[0x2], message[0x3], message[0x4], message[0x5], message[0x6], message[0x7], message[0x8], message[0x9]); } break; case COMMAND_WRITE: { uint8_t message[MESSAGE_SIZE] = {0}; { // scan message int message_size = 0; while (sscanf (head, "%2hhx%n", &message[message_size], &scanned) == 1 && message_size < MESSAGE_SIZE) { head += scanned; message_size += 1; } if (message_size != MESSAGE_SIZE) { fprintf (stderr, "error: the message is too short, expected %d bytes, received %d\n", MESSAGE_SIZE, message_size); continue; } while (isspace (*head)) ++head; if (*head) { fprintf (stderr, "error: junk after message: %c...\n", *head); continue; } } // compute parity uint8_t parity[BLOCK_SIZE] = { message[0x0] ^ message[0x5], message[0x1] ^ message[0x6], message[0x2] ^ message[0x7], message[0x3] ^ message[0x8], message[0x4] ^ message[0x9], }; // write blocks open_disks (address); // open disk files, ensuring their integrity fprintf (disks[0], "%02hhX%02hhX%02hhX%02hhX%02hhX\n", message[0x0], message[0x1], message[0x2], message[0x3], message[0x4]); fprintf (disks[1], "%02hhX%02hhX%02hhX%02hhX%02hhX\n", message[0x5], message[0x6], message[0x7], message[0x8], message[0x9]); fprintf (disks[2], "%02hhX%02hhX%02hhX%02hhX%02hhX\n", parity [0x0], parity [0x1], parity [0x2], parity [0x3], parity [0x4]); close_disks (); } break; } free (line); } return EXIT_SUCCESS; }