]> git.ipfire.org Git - thirdparty/mdadm.git/blame - raid6check.c
raid6check: Fix compiler warnings.
[thirdparty/mdadm.git] / raid6check.c
CommitLineData
979afcb8
PS
1/*
2 * raid6check - extended consistency check for RAID-6
3 *
4 * Copyright (C) 2011 Piergiorgio Sartor
5 *
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 * Author: Piergiorgio Sartor
22 * Based on "restripe.c" from "mdadm" codebase
23 */
24
25#include "mdadm.h"
26#include <stdint.h>
8d8ab389
PS
27#include <signal.h>
28#include <sys/mman.h>
979afcb8 29
635b5861
BS
30enum repair {
31 NO_REPAIR = 0,
32 MANUAL_REPAIR,
33 AUTO_REPAIR
34};
35
979afcb8
PS
36int geo_map(int block, unsigned long long stripe, int raid_disks,
37 int level, int layout);
38void qsyndrome(uint8_t *p, uint8_t *q, uint8_t **sources, int disks, int size);
39void make_tables(void);
f2e29ad6
RB
40void ensure_zero_has_size(int chunk_size);
41void raid6_datap_recov(int disks, size_t bytes, int faila, uint8_t **ptrs);
42void raid6_2data_recov(int disks, size_t bytes, int faila, int failb,
43 uint8_t **ptrs);
44void xor_blocks(char *target, char **sources, int disks, int size);
45
979afcb8
PS
46
47/* Collect per stripe consistency information */
48void raid6_collect(int chunk_size, uint8_t *p, uint8_t *q,
49 char *chunkP, char *chunkQ, int *results)
50{
51 int i;
52 int data_id;
53 uint8_t Px, Qx;
54 extern uint8_t raid6_gflog[];
55
56 for(i = 0; i < chunk_size; i++) {
57 Px = (uint8_t)chunkP[i] ^ (uint8_t)p[i];
58 Qx = (uint8_t)chunkQ[i] ^ (uint8_t)q[i];
59
60 if((Px != 0) && (Qx == 0))
61 results[i] = -1;
62
63 if((Px == 0) && (Qx != 0))
64 results[i] = -2;
65
66 if((Px != 0) && (Qx != 0)) {
67 data_id = (raid6_gflog[Qx] - raid6_gflog[Px]);
68 if(data_id < 0) data_id += 255;
69 results[i] = data_id;
70 }
71
72 if((Px == 0) && (Qx == 0))
73 results[i] = -255;
74 }
75}
76
77/* Try to find out if a specific disk has problems */
78int raid6_stats(int *results, int raid_disks, int chunk_size)
79{
80 int i;
81 int curr_broken_disk = -255;
82 int prev_broken_disk = -255;
83 int broken_status = 0;
84
85 for(i = 0; i < chunk_size; i++) {
86
87 if(results[i] != -255)
88 curr_broken_disk = results[i];
89
90 if(curr_broken_disk >= raid_disks)
91 broken_status = 2;
92
93 switch(broken_status) {
94 case 0:
95 if(curr_broken_disk != -255) {
96 prev_broken_disk = curr_broken_disk;
97 broken_status = 1;
98 }
99 break;
100
101 case 1:
102 if(curr_broken_disk != prev_broken_disk)
103 broken_status = 2;
104 break;
105
106 case 2:
107 default:
108 curr_broken_disk = prev_broken_disk = -65535;
109 break;
110 }
111 }
112
113 return curr_broken_disk;
114}
115
351d7680
RB
116int lock_stripe(struct mdinfo *info, unsigned long long start,
117 int chunk_size, int data_disks, sighandler_t *sig) {
118 int rv;
119 if(mlockall(MCL_CURRENT | MCL_FUTURE) != 0) {
120 return 2;
121 }
122
123 sig[0] = signal(SIGTERM, SIG_IGN);
124 sig[1] = signal(SIGINT, SIG_IGN);
125 sig[2] = signal(SIGQUIT, SIG_IGN);
126
127 rv = sysfs_set_num(info, NULL, "suspend_lo", start * chunk_size * data_disks);
128 rv |= sysfs_set_num(info, NULL, "suspend_hi", (start + 1) * chunk_size * data_disks);
129 return rv * 256;
130}
131
132int unlock_all_stripes(struct mdinfo *info, sighandler_t *sig) {
133 int rv;
134 rv = sysfs_set_num(info, NULL, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL);
135 rv |= sysfs_set_num(info, NULL, "suspend_hi", 0);
136 rv |= sysfs_set_num(info, NULL, "suspend_lo", 0);
137
138 signal(SIGQUIT, sig[2]);
139 signal(SIGINT, sig[1]);
140 signal(SIGTERM, sig[0]);
141
142 if(munlockall() != 0)
143 return 3;
144 return rv * 256;
145}
146
147
8d8ab389 148int check_stripes(struct mdinfo *info, int *source, unsigned long long *offsets,
979afcb8 149 int raid_disks, int chunk_size, int level, int layout,
f2e29ad6 150 unsigned long long start, unsigned long long length, char *name[],
635b5861 151 enum repair repair, int failed_disk1, int failed_disk2)
979afcb8
PS
152{
153 /* read the data and p and q blocks, and check we got them right */
503975b9
N
154 char *stripe_buf = xmalloc(raid_disks * chunk_size);
155 char **stripes = xmalloc(raid_disks * sizeof(char*));
156 char **blocks = xmalloc(raid_disks * sizeof(char*));
696e95a1 157 int *block_index_for_slot = xmalloc(raid_disks * sizeof(int));
503975b9
N
158 uint8_t *p = xmalloc(chunk_size);
159 uint8_t *q = xmalloc(chunk_size);
160 int *results = xmalloc(chunk_size * sizeof(int));
351d7680 161 sighandler_t *sig = xmalloc(3 * sizeof(sighandler_t));
979afcb8
PS
162
163 int i;
164 int diskP, diskQ;
165 int data_disks = raid_disks - 2;
af3c3750 166 int err = 0;
979afcb8
PS
167
168 extern int tables_ready;
169
170 if (!tables_ready)
171 make_tables();
172
173 for ( i = 0 ; i < raid_disks ; i++)
174 stripes[i] = stripe_buf + i * chunk_size;
175
176 while (length > 0) {
177 int disk;
178
af3c3750
PS
179 printf("pos --> %llu\n", start);
180
351d7680
RB
181 err = lock_stripe(info, start, chunk_size, data_disks, sig);
182 if(err != 0) {
183 if (err != 2)
184 unlock_all_stripes(info, sig);
8d8ab389
PS
185 goto exitCheck;
186 }
979afcb8 187 for (i = 0 ; i < raid_disks ; i++) {
af3c3750 188 lseek64(source[i], offsets[i] + start * chunk_size, 0);
2c7b668d
BS
189 int read_res = read(source[i], stripes[i], chunk_size);
190 if (read_res < chunk_size) {
191 fprintf(stderr, "Failed to read complete chunk disk %d, aborting\n", i);
192 unlock_all_stripes(info, sig);
193 goto exitCheck;
194 }
979afcb8 195 }
351d7680
RB
196 err = unlock_all_stripes(info, sig);
197 if(err != 0)
8d8ab389 198 goto exitCheck;
8d8ab389 199
979afcb8 200 for (i = 0 ; i < data_disks ; i++) {
af3c3750 201 int disk = geo_map(i, start, raid_disks, level, layout);
979afcb8 202 blocks[i] = stripes[disk];
696e95a1 203 block_index_for_slot[disk] = i;
979afcb8
PS
204 printf("%d->%d\n", i, disk);
205 }
206
207 qsyndrome(p, q, (uint8_t**)blocks, data_disks, chunk_size);
af3c3750 208 diskP = geo_map(-1, start, raid_disks, level, layout);
f2e29ad6
RB
209 diskQ = geo_map(-2, start, raid_disks, level, layout);
210 blocks[data_disks] = stripes[diskP];
696e95a1 211 block_index_for_slot[diskP] = data_disks;
f2e29ad6 212 blocks[data_disks+1] = stripes[diskQ];
696e95a1 213 block_index_for_slot[diskQ] = data_disks+1;
f2e29ad6 214
979afcb8 215 if (memcmp(p, stripes[diskP], chunk_size) != 0) {
af3c3750 216 printf("P(%d) wrong at %llu\n", diskP, start);
979afcb8 217 }
979afcb8 218 if (memcmp(q, stripes[diskQ], chunk_size) != 0) {
af3c3750 219 printf("Q(%d) wrong at %llu\n", diskQ, start);
979afcb8 220 }
af3c3750 221 raid6_collect(chunk_size, p, q, stripes[diskP], stripes[diskQ], results);
979afcb8
PS
222 disk = raid6_stats(results, raid_disks, chunk_size);
223
224 if(disk >= -2) {
af3c3750 225 disk = geo_map(disk, start, raid_disks, level, layout);
979afcb8
PS
226 }
227 if(disk >= 0) {
af3c3750
PS
228 printf("Error detected at %llu: possible failed disk slot: %d --> %s\n",
229 start, disk, name[disk]);
979afcb8
PS
230 }
231 if(disk == -65535) {
af3c3750 232 printf("Error detected at %llu: disk slot unknown\n", start);
979afcb8 233 }
635b5861 234 if(repair == MANUAL_REPAIR) {
f2e29ad6
RB
235 printf("Repairing stripe %llu\n", start);
236 printf("Assuming slots %d (%s) and %d (%s) are incorrect\n",
237 failed_disk1, name[failed_disk1],
238 failed_disk2, name[failed_disk2]);
239
240 if (failed_disk1 == diskQ || failed_disk2 == diskQ) {
241 char *all_but_failed_blocks[data_disks];
696e95a1 242 int failed_data_or_p;
f2e29ad6
RB
243 int failed_block_index;
244
245 if (failed_disk1 == diskQ)
696e95a1 246 failed_data_or_p = failed_disk2;
f2e29ad6 247 else
696e95a1
RB
248 failed_data_or_p = failed_disk1;
249 printf("Repairing D/P(%d) and Q\n", failed_data_or_p);
250 failed_block_index = block_index_for_slot[failed_data_or_p];
f2e29ad6
RB
251 for (i=0; i < data_disks; i++)
252 if (failed_block_index == i)
253 all_but_failed_blocks[i] = stripes[diskP];
254 else
255 all_but_failed_blocks[i] = blocks[i];
696e95a1 256 xor_blocks(stripes[failed_data_or_p],
f2e29ad6
RB
257 all_but_failed_blocks, data_disks, chunk_size);
258 qsyndrome(p, (uint8_t*)stripes[diskQ], (uint8_t**)blocks, data_disks, chunk_size);
259 } else {
260 ensure_zero_has_size(chunk_size);
261 if (failed_disk1 == diskP || failed_disk2 == diskP) {
262 int failed_data, failed_block_index;
263 if (failed_disk1 == diskP)
264 failed_data = failed_disk2;
265 else
266 failed_data = failed_disk1;
696e95a1 267 failed_block_index = block_index_for_slot[failed_data];
f2e29ad6
RB
268 printf("Repairing D(%d) and P\n", failed_data);
269 raid6_datap_recov(raid_disks, chunk_size, failed_block_index, (uint8_t**)blocks);
270 } else {
271 printf("Repairing D and D\n");
696e95a1
RB
272 int failed_block_index1 = block_index_for_slot[failed_disk1];
273 int failed_block_index2 = block_index_for_slot[failed_disk2];
f2e29ad6
RB
274 if (failed_block_index1 > failed_block_index2) {
275 int t = failed_block_index1;
276 failed_block_index1 = failed_block_index2;
277 failed_block_index2 = t;
278 }
279 raid6_2data_recov(raid_disks, chunk_size, failed_block_index1, failed_block_index2, (uint8_t**)blocks);
280 }
281 }
351d7680
RB
282
283 err = lock_stripe(info, start, chunk_size, data_disks, sig);
284 if(err != 0) {
285 if (err != 2)
286 unlock_all_stripes(info, sig);
f2e29ad6
RB
287 goto exitCheck;
288 }
351d7680 289
2c7b668d
BS
290 int write_res1, write_res2;
291
f2e29ad6 292 lseek64(source[failed_disk1], offsets[failed_disk1] + start * chunk_size, 0);
2c7b668d 293 write_res1 = write(source[failed_disk1], stripes[failed_disk1], chunk_size);
f2e29ad6 294 lseek64(source[failed_disk2], offsets[failed_disk2] + start * chunk_size, 0);
2c7b668d 295 write_res2 = write(source[failed_disk2], stripes[failed_disk2], chunk_size);
f2e29ad6 296
8a63c731
RB
297 err = unlock_all_stripes(info, sig);
298 if(err != 0)
299 goto exitCheck;
2c7b668d
BS
300
301
302 if (write_res1 != chunk_size || write_res2 != chunk_size) {
303 fprintf(stderr, "Failed to write a complete chunk.\n");
304 goto exitCheck;
305 }
306
635b5861 307 } else if (disk >= 0 && repair == AUTO_REPAIR) {
8a63c731
RB
308 printf("Auto-repairing slot %d (%s)\n", disk, name[disk]);
309 if (disk == diskQ) {
310 qsyndrome(p, (uint8_t*)stripes[diskQ], (uint8_t**)blocks, data_disks, chunk_size);
311 } else {
312 char *all_but_failed_blocks[data_disks];
313 int failed_block_index = block_index_for_slot[disk];
314 for (i=0; i < data_disks; i++)
315 if (failed_block_index == i)
316 all_but_failed_blocks[i] = stripes[diskP];
317 else
318 all_but_failed_blocks[i] = blocks[i];
319 xor_blocks(stripes[disk],
320 all_but_failed_blocks, data_disks, chunk_size);
321 }
322
323 err = lock_stripe(info, start, chunk_size, data_disks, sig);
324 if(err != 0) {
325 if (err != 2)
326 unlock_all_stripes(info, sig);
327 goto exitCheck;
328 }
329
330 lseek64(source[disk], offsets[disk] + start * chunk_size, 0);
2c7b668d 331 int write_res = write(source[disk], stripes[disk], chunk_size);
8a63c731 332
351d7680 333 err = unlock_all_stripes(info, sig);
2c7b668d 334 if(err != 0 || write_res != chunk_size)
f2e29ad6 335 goto exitCheck;
2c7b668d
BS
336
337 if (write_res != chunk_size) {
338 fprintf(stderr, "Failed to write a full chunk.\n");
339 goto exitCheck;
340 }
f2e29ad6
RB
341 }
342
979afcb8 343
af3c3750
PS
344 length--;
345 start++;
979afcb8
PS
346 }
347
af3c3750
PS
348exitCheck:
349
979afcb8
PS
350 free(stripe_buf);
351 free(stripes);
352 free(blocks);
3a89d754 353 free(block_index_for_slot);
979afcb8
PS
354 free(p);
355 free(q);
356 free(results);
3a89d754 357 free(sig);
979afcb8 358
af3c3750 359 return err;
979afcb8
PS
360}
361
362unsigned long long getnum(char *str, char **err)
363{
364 char *e;
365 unsigned long long rv = strtoull(str, &e, 10);
366 if (e==str || *e) {
367 *err = str;
368 return 0;
369 }
370 return rv;
371}
372
373int main(int argc, char *argv[])
374{
a9c2c6c6 375 /* md_device start length */
af3c3750
PS
376 int *fds = NULL;
377 char *buf = NULL;
378 char **disk_name = NULL;
379 unsigned long long *offsets = NULL;
380 int raid_disks = 0;
2cf31121 381 int active_disks;
af3c3750
PS
382 int chunk_size = 0;
383 int layout = -1;
979afcb8 384 int level = 6;
635b5861 385 enum repair repair = NO_REPAIR;
2c7b668d
BS
386 int failed_disk1 = -1;
387 int failed_disk2 = -1;
979afcb8
PS
388 unsigned long long start, length;
389 int i;
a9c2c6c6 390 int mdfd;
8d8ab389 391 struct mdinfo *info = NULL, *comp = NULL;
979afcb8 392 char *err = NULL;
af3c3750
PS
393 int exit_err = 0;
394 int close_flag = 0;
395 char *prg = strrchr(argv[0], '/');
396
397 if (prg == NULL)
398 prg = argv[0];
399 else
400 prg++;
401
402 if (argc < 4) {
8a63c731 403 fprintf(stderr, "Usage: %s md_device start_stripe length_stripes [autorepair]\n", prg);
f2e29ad6 404 fprintf(stderr, " or: %s md_device repair stripe failed_slot_1 failed_slot_2\n", prg);
af3c3750
PS
405 exit_err = 1;
406 goto exitHere;
979afcb8
PS
407 }
408
a9c2c6c6
PS
409 mdfd = open(argv[1], O_RDONLY);
410 if(mdfd < 0) {
411 perror(argv[1]);
e7b84f9d 412 fprintf(stderr, "%s: cannot open %s\n", prg, argv[1]);
af3c3750
PS
413 exit_err = 2;
414 goto exitHere;
a9c2c6c6
PS
415 }
416
f8fcf7a1 417 info = sysfs_read(mdfd, NULL,
a9c2c6c6
PS
418 GET_LEVEL|
419 GET_LAYOUT|
420 GET_DISKS|
2cf31121 421 GET_DEGRADED |
a9c2c6c6
PS
422 GET_COMPONENT|
423 GET_CHUNK|
424 GET_DEVS|
425 GET_OFFSET|
426 GET_SIZE);
427
8d8ab389
PS
428 if(info == NULL) {
429 fprintf(stderr, "%s: Error reading sysfs information of %s\n", prg, argv[1]);
430 exit_err = 9;
431 goto exitHere;
432 }
433
a9c2c6c6
PS
434 if(info->array.level != level) {
435 fprintf(stderr, "%s: %s not a RAID-6\n", prg, argv[1]);
af3c3750
PS
436 exit_err = 3;
437 goto exitHere;
a9c2c6c6
PS
438 }
439
2cf31121
PS
440 if(info->array.failed_disks > 0) {
441 fprintf(stderr, "%s: %s degraded array\n", prg, argv[1]);
442 exit_err = 8;
443 goto exitHere;
444 }
445
a9c2c6c6
PS
446 printf("layout: %d\n", info->array.layout);
447 printf("disks: %d\n", info->array.raid_disks);
af3c3750
PS
448 printf("component size: %llu\n", info->component_size * 512);
449 printf("total stripes: %llu\n", (info->component_size * 512) / info->array.chunk_size);
a9c2c6c6
PS
450 printf("chunk size: %d\n", info->array.chunk_size);
451 printf("\n");
452
453 comp = info->devs;
2cf31121 454 for(i = 0, active_disks = 0; active_disks < info->array.raid_disks; i++) {
a9c2c6c6 455 printf("disk: %d - offset: %llu - size: %llu - name: %s - slot: %d\n",
af3c3750 456 i, comp->data_offset * 512, comp->component_size * 512,
a9c2c6c6
PS
457 map_dev(comp->disk.major, comp->disk.minor, 0),
458 comp->disk.raid_disk);
2cf31121
PS
459 if(comp->disk.raid_disk >= 0)
460 active_disks++;
a9c2c6c6
PS
461 comp = comp->next;
462 }
463 printf("\n");
464
465 close(mdfd);
466
467 raid_disks = info->array.raid_disks;
468 chunk_size = info->array.chunk_size;
469 layout = info->array.layout;
f2e29ad6
RB
470 if (strcmp(argv[2], "repair")==0) {
471 if (argc < 6) {
472 fprintf(stderr, "For repair mode, call %s md_device repair stripe failed_slot_1 failed_slot_2\n", prg);
473 exit_err = 1;
474 goto exitHere;
475 }
635b5861 476 repair = MANUAL_REPAIR;
f2e29ad6
RB
477 start = getnum(argv[3], &err);
478 length = 1;
479 failed_disk1 = getnum(argv[4], &err);
480 failed_disk2 = getnum(argv[5], &err);
481
b67e45b8 482 if(failed_disk1 >= info->array.raid_disks) {
f2e29ad6
RB
483 fprintf(stderr, "%s: failed_slot_1 index is higher than number of devices in raid\n", prg);
484 exit_err = 4;
485 goto exitHere;
486 }
b67e45b8 487 if(failed_disk2 >= info->array.raid_disks) {
f2e29ad6
RB
488 fprintf(stderr, "%s: failed_slot_2 index is higher than number of devices in raid\n", prg);
489 exit_err = 4;
490 goto exitHere;
491 }
492 if(failed_disk1 == failed_disk2) {
493 fprintf(stderr, "%s: failed_slot_1 and failed_slot_2 are the same\n", prg);
494 exit_err = 4;
495 goto exitHere;
496 }
497 }
498 else {
499 start = getnum(argv[2], &err);
500 length = getnum(argv[3], &err);
8a63c731 501 if (argc >= 5 && strcmp(argv[4], "autorepair")==0)
635b5861 502 repair = AUTO_REPAIR;
f2e29ad6 503 }
a9c2c6c6 504
979afcb8 505 if (err) {
a9c2c6c6 506 fprintf(stderr, "%s: Bad number: %s\n", prg, err);
af3c3750
PS
507 exit_err = 4;
508 goto exitHere;
979afcb8 509 }
a9c2c6c6 510
af3c3750
PS
511 if(start > ((info->component_size * 512) / chunk_size)) {
512 start = (info->component_size * 512) / chunk_size;
513 fprintf(stderr, "%s: start beyond disks size\n", prg);
514 }
a9c2c6c6 515
af3c3750
PS
516 if((length == 0) ||
517 ((length + start) > ((info->component_size * 512) / chunk_size))) {
518 length = (info->component_size * 512) / chunk_size - start;
979afcb8 519 }
a9c2c6c6 520
503975b9
N
521 disk_name = xmalloc(raid_disks * sizeof(*disk_name));
522 fds = xmalloc(raid_disks * sizeof(*fds));
523 offsets = xcalloc(raid_disks, sizeof(*offsets));
524 buf = xmalloc(raid_disks * chunk_size);
af3c3750 525
af3c3750
PS
526 for(i=0; i<raid_disks; i++) {
527 fds[i] = -1;
528 }
529 close_flag = 1;
979afcb8 530
a9c2c6c6 531 comp = info->devs;
2cf31121 532 for (i=0, active_disks=0; active_disks<raid_disks; i++) {
a9c2c6c6 533 int disk_slot = comp->disk.raid_disk;
2cf31121
PS
534 if(disk_slot >= 0) {
535 disk_name[disk_slot] = map_dev(comp->disk.major, comp->disk.minor, 0);
536 offsets[disk_slot] = comp->data_offset * 512;
537 fds[disk_slot] = open(disk_name[disk_slot], O_RDWR);
538 if (fds[disk_slot] < 0) {
539 perror(disk_name[disk_slot]);
540 fprintf(stderr,"%s: cannot open %s\n", prg, disk_name[disk_slot]);
541 exit_err = 6;
542 goto exitHere;
543 }
544 active_disks++;
979afcb8 545 }
a9c2c6c6 546 comp = comp->next;
979afcb8
PS
547 }
548
8d8ab389 549 int rv = check_stripes(info, fds, offsets,
979afcb8 550 raid_disks, chunk_size, level, layout,
f2e29ad6 551 start, length, disk_name, repair, failed_disk1, failed_disk2);
979afcb8
PS
552 if (rv != 0) {
553 fprintf(stderr,
a9c2c6c6 554 "%s: check_stripes returned %d\n", prg, rv);
af3c3750
PS
555 exit_err = 7;
556 goto exitHere;
979afcb8
PS
557 }
558
af3c3750
PS
559exitHere:
560
561 if (close_flag)
562 for(i = 0; i < raid_disks; i++)
563 close(fds[i]);
564
a9c2c6c6 565 free(disk_name);
979afcb8
PS
566 free(fds);
567 free(offsets);
568 free(buf);
569
af3c3750 570 exit(exit_err);
979afcb8 571}