// g, slock // M1M2M3M4M5 M6M7M8M9MA MBMCMDME00 P1P2P3P4P5 // M1M2M3M4M5 M6M7M8M9MA P1P2P3P4P5 MBMCMDME00 // M1M2M3M4M5 P1P2P3P4P5 M6M7M8M9MA MBMCMDME00 // P1P2P3P4P5 M1M2M3M4M5 M6M7M8M9MA MBMCMDME00 #include #include #include #include #include #include #define DISK_COUNT 4 #define MESSAGE_SIZE 14 #define PADDING_SIZE 1 #define BLOCK_SIZE 5 uint8_t coefficients[DISK_COUNT - 1] = { 1, 1, 1, }; int shift_table[][DISK_COUNT] = { { 0, 1, 2, 3, }, { 0, 1, 3, 2, }, { 0, 2, 3, 1, }, { 1, 2, 3, 0, }, }; #define DISK_SIZE 0x40 #define POPCOUNT __builtin_popcount #define CTZ __builtin_ctz #ifndef WIN32 #define LINE_SIZE (2*BLOCK_SIZE + 1) #else #define LINE_SIZE (2*BLOCK_SIZE + 2) 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; } } if ((*lineptr) == NULL) { *lineptr = cs; } else { if ((length + 1) < *n) *lineptr = (char *) realloc (*lineptr, length + 1); memcpy (*lineptr, cs, length); free (cs); } return (ssize_t) (*n = strlen (*lineptr)); } #endif enum command { COMMAND_WRITE, COMMAND_READ, }; FILE *disks[DISK_COUNT]; static void recompute_bad_disk (int bad_disk) { for (int j = 0; j < DISK_SIZE; ++j) { uint8_t block_bytes[MESSAGE_SIZE + PADDING_SIZE + BLOCK_SIZE]; int shift = j % DISK_COUNT; int logical_bad_disk; bool empty = false; for (int k = 0; k < DISK_COUNT; ++k) { if (shift_table[shift][k] == bad_disk) { logical_bad_disk = k; continue; } for (int l = 0; l < BLOCK_SIZE; ++l) { int c = fgetc (disks[shift_table[shift][k]]); if (c == ' ') { fgetc (disks[shift_table[shift][k]]); empty = true; } else { ungetc (c, disks[shift_table[shift][k]]); fscanf (disks[shift_table[shift][k]], "%02hhX", &block_bytes[(k*BLOCK_SIZE + l)]); } } fgetc (disks[shift_table[shift][k]]); } if (empty) { // address empty for (int l = 0; l < BLOCK_SIZE; ++l) { fprintf (disks[bad_disk], " "); } fprintf (disks[bad_disk], "\n"); continue; } else if (logical_bad_disk % DISK_COUNT == DISK_COUNT - 1) { // lost parity for (int l = 0; l < BLOCK_SIZE; ++l) block_bytes[(logical_bad_disk*BLOCK_SIZE + l)] = 0x00; for (int k = 0, c = 0; k < DISK_COUNT; ++k) { if (k == logical_bad_disk) continue; for (int l = 0; l < BLOCK_SIZE; ++l) block_bytes[(logical_bad_disk*BLOCK_SIZE + l)] += coefficients[c]*block_bytes[(k*BLOCK_SIZE + l)]; c += 1; } } else { // lost data for (int l = 0; l < BLOCK_SIZE; ++l) { block_bytes[(logical_bad_disk*BLOCK_SIZE + l)] = block_bytes[((DISK_COUNT - 1)*BLOCK_SIZE + l)]; } for (int k = 0, c = 0; k < DISK_COUNT - 1; ++k) { if (k == logical_bad_disk) continue; for (int l = 0; l < BLOCK_SIZE; ++l) block_bytes[(logical_bad_disk*BLOCK_SIZE + l)] -= coefficients[c]*block_bytes[(k*BLOCK_SIZE + l)]; c += 1; } } for (int l = 0; l < BLOCK_SIZE; ++l) { fprintf (disks[bad_disk], "%02hhX", block_bytes[(logical_bad_disk*BLOCK_SIZE + l)]); } fprintf (disks[bad_disk], "\n"); } } 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]) { disks[i] = fopen (filename, "w+"); if (!disks[i]) { fprintf (stderr, "Ошибка! Невозможно открыть файл: %s\n", filename); exit (EXIT_FAILURE); } fprintf (stderr, "Предупреждение! Найден плохой диск: %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, "Предупреждение!Найден плохой диск: %s\n", filename); fclose (disks[i]); disks[i] = fopen (filename, "w+"); bad_disks |= 1 << i; } else rewind (disks[i]); } } int bad_disk_count = POPCOUNT (bad_disks); if (bad_disk_count == 1) { fprintf (stderr, "Повторный расчет, один диск плохой...\n"); int bad_disk = CTZ (bad_disks); recompute_bad_disk (bad_disk); } else if (bad_disk_count) { fprintf (stderr, "Ошибка! Повторная инициализация массива с новыми дисками.\n"); for (int i = 0; i < DISK_COUNT; ++i) { for (int j = 0; j < DISK_SIZE; ++j) { for (int k = 0; k < BLOCK_SIZE; ++k) { fputs (" ", disks[i]); } fputs ("\n", disks[i]); } } } 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, "Инициализация RAID 5 с POLY на 4 дисках с 14 байтовым сообщением.\n"); open_disks (0); close_disks ( ); for (;;) { char *line = NULL; size_t line_size = 0; const char *head; int assigned; int scanned; printf ("> "); scanned = getline (&line, &line_size, stdin); if (scanned == -1) exit (EXIT_SUCCESS); else if (scanned == 1) continue; head = line; uint8_t address; { assigned = sscanf (head, "%hhu%n", &address, &scanned); if (assigned != 1 || address >= DISK_SIZE) { fprintf(stderr, "Ошибка! Адрес не входит в границы! [0; 64)\n"); continue; } head += scanned; } enum command 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, "Ошибка! Не корректная команда! (read; write)\n"); continue; } head += scanned; command = command_buffer[0] == 'r' ? COMMAND_READ : COMMAND_WRITE; } switch (command) { case COMMAND_READ: { uint8_t block_bytes[MESSAGE_SIZE + PADDING_SIZE + BLOCK_SIZE]; int shift = address % DISK_COUNT; open_disks (address); int c = fgetc (disks[0]); if (c == ' ') { close_disks (); printf ("Пусто\n"); break; } else ungetc (c, disks[0]); for (int i = 0; i < DISK_COUNT; ++i) { for (int j = 0; j < BLOCK_SIZE; ++j) { fscanf (disks[shift_table[shift][i]], "%02hhX", &block_bytes[BLOCK_SIZE*i + j]); } } close_disks (); for (int i = 0; i < DISK_COUNT - 1; ++i) { for (int j = 0; j < BLOCK_SIZE; ++j) { if (i == DISK_COUNT - 2 && j + PADDING_SIZE >= BLOCK_SIZE) continue; printf ("%02hhX", block_bytes[BLOCK_SIZE*i + j]); } printf (" "); } printf ("\n"); } break; case COMMAND_WRITE: { uint8_t block_bytes[MESSAGE_SIZE + PADDING_SIZE + BLOCK_SIZE] = {0}; { // scan message uint8_t *write_head = block_bytes; int message_size = 0; while (sscanf (head, "%2hhx%n", write_head, &scanned) == 1 && message_size < MESSAGE_SIZE) { head += scanned; message_size += 1; write_head += 1; if (message_size % ((DISK_COUNT - 1)*BLOCK_SIZE) == 0) write_head += BLOCK_SIZE; } if (message_size != MESSAGE_SIZE) { fprintf (stderr, "Ошибка! Сообщение слишком большое, ожидалось %d байт, восстановленно %d\n", MESSAGE_SIZE, message_size); continue; } while (isspace(*head)) ++head; if (*head) { fprintf (stderr, "Ошибка! Выводится мусор после сообщения: %c...\n", *head); continue; } } for (int i = 0; i < DISK_COUNT - 1; ++i) { for (int j = 0; j < BLOCK_SIZE; ++j) block_bytes[(DISK_COUNT - 1)*BLOCK_SIZE + j] += coefficients[i]*block_bytes[i*BLOCK_SIZE + j]; } int shift = address % DISK_COUNT; open_disks (address); for (int i = 0; i < DISK_COUNT; ++i) { for (int j = 0; j < BLOCK_SIZE; ++j) fprintf (disks[shift_table[shift][i]], "%02hhX", block_bytes[i*BLOCK_SIZE + j]); fprintf (disks[shift_table[shift][i]], "\n"); } close_disks (); } break; } free (line); } return EXIT_SUCCESS; }