// yana // M1M2 M3M4 M5M6 M7M8 0000 0000 P1P2 #include #include #include #include #include #include #include #include #define DISK_COUNT 7 #define MESSAGE_SIZE 8 #define BLOCK_SIZE 2 #define PADDING_SIZE 4 // uint8_t coefficients[DISK_COUNT - 1] = { 1, 3, 5, 7, 11, 13 }; uint8_t coefficients[DISK_COUNT - 1] = { 1, 1, 1, 1, 1, 1 }; #define DISK_SIZE 0x40 #define POPCOUNT __builtin_popcount #define CTZ __builtin_ctz enum command { COMMAND_NONE, COMMAND_WRITE, COMMAND_READ, }; FILE *disks[DISK_COUNT]; #ifndef WIN32 #define LINE_SIZE 5 #else #define LINE_SIZE 6 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[6]; snprintf (filename, 6, "disk%d", i); disks[i] = fopen (filename, "r+"); if (!disks[i]) { // disk file was deleted disks[i] = fopen (filename, "w+"); if (!disks[i]) { fprintf (stderr, "ERROR - unable to open file: %s\n", filename); exit (EXIT_FAILURE); } fprintf (stderr, "WARNING - found bad disk that is 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 - found corrupted disk: %s\n", filename); bad_disks |= 1 << i; fclose (disks[i]); disks[i] = fopen (filename, "w+"); } else rewind (disks[i]); } } // there were bad disks int bad_disk_count = POPCOUNT (bad_disks); if (bad_disk_count == 1) { // recompute lost disk fprintf (stderr, "NOTICE - only 1 bad disk, recomputing it again \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 bool empty = false; for (int k = 0; k < DISK_COUNT; ++k) { if (k == bad_disk) continue; int c = fgetc (disks[k]); if (c == ' ') { empty = true; fgetc (disks[k]); fgetc (disks[k]); fgetc (disks[k]); } else { ungetc (c, disks[k]); for (int l = 0; l < BLOCK_SIZE; ++l) { fscanf (disks[k], "%02hhX", &block_bytes[k*BLOCK_SIZE + l]); } } fgetc (disks[k]); } if (empty) { fprintf (disks[bad_disk], " \n"); continue; } // compute lost block if (bad_disk == DISK_COUNT - 1) { // lost parity for (int l = 0; l < BLOCK_SIZE; ++l) block_bytes[bad_disk*BLOCK_SIZE + l] = 0x00; for (int k = 0; k < DISK_COUNT - 1; ++k) { for (int l = 0; l < BLOCK_SIZE; ++l) block_bytes[bad_disk*BLOCK_SIZE + l] += coefficients[k]*block_bytes[k*BLOCK_SIZE + l]; } } else { // lost data for (int l = 0; l < BLOCK_SIZE; ++l) block_bytes[bad_disk*BLOCK_SIZE + l] = block_bytes[(DISK_COUNT - 1)*BLOCK_SIZE + l]/coefficients[bad_disk]; for (int k = 0; k < DISK_COUNT - 1; ++k) { if (k == bad_disk) continue; for (int l = 0; l < BLOCK_SIZE; ++l) block_bytes[bad_disk*BLOCK_SIZE + l] -= coefficients[k]*block_bytes[k*BLOCK_SIZE + l]/coefficients[bad_disk]; // printf ("%02hhX\n", coefficients[k]*block_bytes[k*BLOCK_SIZE + 0]/coefficients[bad_disk]); } } // write lost block for (int l = 0; l < BLOCK_SIZE; ++l) fprintf (disks[bad_disk], "%02hhX", block_bytes[bad_disk*BLOCK_SIZE + l]); fprintf (disks[bad_disk], "\n"); } } else if (bad_disk_count > 1) { // replace all disks with empty ones, all data is lost fprintf (stderr, "ERROR - more than one bad 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 (" \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, "Initialization RAID 4 on 7 disks. The message size is 8 bytes. \n"); // check disk integrity on startup open_disks (0); close_disks ( ); for (;;) { char *head; int assigned; int scanned; // read whole line printf ("> "); char *line = NULL; size_t line_size = 0; 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, "%hhu%n", &address, &scanned); if (assigned != 1 || address >= DISK_SIZE) { fprintf(stderr, "ERROR - address not in set [0; 64) \n"); continue; } head += scanned; } enum command command; { // scan command while (*head == ' ') head += 1; if (!strncmp (head, "read", 4)) { head += 4; command = COMMAND_READ; } else if (!strncmp (head, "write", 5)) { head += 5; command = COMMAND_WRITE; } if (!isspace(*head)) command = COMMAND_NONE; } switch (command) { case COMMAND_NONE: { fprintf(stderr, "ERROR - there is no such command, try entering read or write \n"); continue; } break; case COMMAND_READ: { uint8_t message[MESSAGE_SIZE]; uint8_t parity [ BLOCK_SIZE]; uint8_t padding[PADDING_SIZE]; while (isspace (*head)) { head += 1; if (*head == '\n' || *head == '\r') { *head = '\0'; } } if (*head) { fprintf (stderr, "ERROR - extraneous data after the message: %s", head); continue; } open_disks (address); int c = fgetc (disks[0]); if (c == ' ') { close_disks (); printf ("The line is empty \n"); break; } ungetc (c, disks[0]); // parse out message fscanf (disks[0], "%02hhX%02hhX\n", &message[0x0], &message[0x1]); fscanf (disks[1], "%02hhX%02hhX\n", &message[0x2], &message[0x3]); fscanf (disks[2], "%02hhX%02hhX\n", &message[0x4], &message[0x5]); fscanf (disks[3], "%02hhX%02hhX\n", &message[0x6], &message[0x7]); fscanf (disks[4], "%02hhX%02hhX\n", &padding[0x0], &padding[0x1]); fscanf (disks[5], "%02hhX%02hhX\n", &padding[0x2], &padding[0x3]); fscanf (disks[6], "%02hhX%02hhX\n", &parity [0x0], &parity [0x1]); close_disks (); printf ("%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]); } break; case COMMAND_WRITE: { uint8_t message[MESSAGE_SIZE] = {0}; { // scan message int message_size = 0; while (*head == ' ' || *head == '\t') ++head; const char *invalid = NULL; size_t len = 0; for (const char *c = head; *c && *c != '\r' && *c != '\n'; ++c) { if (!strchr ("0123456789abcdefABCDEF", *c)) { invalid = c; break; } len += 1; } if (invalid) { fprintf (stderr, "ERROR - message contains invalid character `%c'\n", *invalid); break; } if (len != 2*MESSAGE_SIZE) { fprintf (stderr, "ERROR - expected %d hex digits for message, received %zu\n", 2*MESSAGE_SIZE, len); break; } while (sscanf (head, "%2hhx%n", &message[message_size], &scanned) == 1 && message_size < MESSAGE_SIZE) { head += scanned; message_size += 1; } } uint8_t padding[PADDING_SIZE] = {0}; // compute parity uint8_t parity[BLOCK_SIZE] = { coefficients[0]*message[0x0] + coefficients[1]*message[0x2] + coefficients[2]*message[0x4] + coefficients[3]*message[0x6] + coefficients[4]*padding[0x0] + coefficients[5]*padding[0x2], coefficients[0]*message[0x1] + coefficients[1]*message[0x3] + coefficients[2]*message[0x5] + coefficients[3]*message[0x7] + coefficients[4]*padding[0x1] + coefficients[5]*padding[0x3], }; // write blocks open_disks (address); // open disk files, ensuring their integrity fprintf (disks[0], "%02hhX%02hhX\n", message[0x0], message[0x1]); fprintf (disks[1], "%02hhX%02hhX\n", message[0x2], message[0x3]); fprintf (disks[2], "%02hhX%02hhX\n", message[0x4], message[0x5]); fprintf (disks[3], "%02hhX%02hhX\n", message[0x6], message[0x7]); fprintf (disks[4], "%02hhX%02hhX\n", padding[0x0], padding[0x1]); fprintf (disks[5], "%02hhX%02hhX\n", padding[0x2], padding[0x3]); fprintf (disks[6], "%02hhX%02hhX\n", parity [0x0], parity [0x1]); close_disks (); } break; } free (line); line = NULL; } return EXIT_SUCCESS; }