]> git.ipfire.org Git - thirdparty/mdadm.git/blame - raid6check.c
restripe: fix data block order in raid6_2_data_recov
[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
3b9c9603
PS
30#define CHECK_PAGE_BITS (12)
31#define CHECK_PAGE_SIZE (1 << CHECK_PAGE_BITS)
32
42129b3f
N
33char const Name[] = "raid6check";
34
635b5861
BS
35enum repair {
36 NO_REPAIR = 0,
37 MANUAL_REPAIR,
38 AUTO_REPAIR
39};
40
979afcb8
PS
41int geo_map(int block, unsigned long long stripe, int raid_disks,
42 int level, int layout);
ad1a3c2f 43int is_ddf(int layout);
979afcb8
PS
44void qsyndrome(uint8_t *p, uint8_t *q, uint8_t **sources, int disks, int size);
45void make_tables(void);
f2e29ad6 46void ensure_zero_has_size(int chunk_size);
50786d47
N
47void raid6_datap_recov(int disks, size_t bytes, int faila, uint8_t **ptrs,
48 int neg_offset);
f2e29ad6 49void raid6_2data_recov(int disks, size_t bytes, int faila, int failb,
50786d47 50 uint8_t **ptrs, int neg_offset);
f2e29ad6
RB
51void xor_blocks(char *target, char **sources, int disks, int size);
52
979afcb8
PS
53/* Collect per stripe consistency information */
54void raid6_collect(int chunk_size, uint8_t *p, uint8_t *q,
55 char *chunkP, char *chunkQ, int *results)
56{
57 int i;
58 int data_id;
59 uint8_t Px, Qx;
60 extern uint8_t raid6_gflog[];
61
62 for(i = 0; i < chunk_size; i++) {
63 Px = (uint8_t)chunkP[i] ^ (uint8_t)p[i];
64 Qx = (uint8_t)chunkQ[i] ^ (uint8_t)q[i];
65
66 if((Px != 0) && (Qx == 0))
67 results[i] = -1;
68
69 if((Px == 0) && (Qx != 0))
70 results[i] = -2;
71
72 if((Px != 0) && (Qx != 0)) {
73 data_id = (raid6_gflog[Qx] - raid6_gflog[Px]);
74 if(data_id < 0) data_id += 255;
75 results[i] = data_id;
76 }
77
78 if((Px == 0) && (Qx == 0))
79 results[i] = -255;
80 }
81}
82
3b9c9603
PS
83/* Try to find out if a specific disk has problems in a CHECK_PAGE_SIZE page size */
84int raid6_stats_blk(int *results, int raid_disks)
979afcb8
PS
85{
86 int i;
87 int curr_broken_disk = -255;
88 int prev_broken_disk = -255;
89 int broken_status = 0;
90
3b9c9603 91 for(i = 0; i < CHECK_PAGE_SIZE; i++) {
979afcb8
PS
92
93 if(results[i] != -255)
94 curr_broken_disk = results[i];
95
96 if(curr_broken_disk >= raid_disks)
97 broken_status = 2;
98
99 switch(broken_status) {
100 case 0:
101 if(curr_broken_disk != -255) {
102 prev_broken_disk = curr_broken_disk;
103 broken_status = 1;
104 }
105 break;
106
107 case 1:
108 if(curr_broken_disk != prev_broken_disk)
109 broken_status = 2;
110 break;
111
112 case 2:
113 default:
114 curr_broken_disk = prev_broken_disk = -65535;
115 break;
116 }
117 }
118
119 return curr_broken_disk;
120}
121
3b9c9603
PS
122/* Collect disks status for a strip in CHECK_PAGE_SIZE page size blocks */
123void raid6_stats(int *disk, int *results, int raid_disks, int chunk_size)
124{
125 int i, j;
126
127 for(i = 0, j = 0; i < chunk_size; i += CHECK_PAGE_SIZE, j++) {
128 disk[j] = raid6_stats_blk(&results[i], raid_disks);
129 }
130}
131
351d7680
RB
132int lock_stripe(struct mdinfo *info, unsigned long long start,
133 int chunk_size, int data_disks, sighandler_t *sig) {
134 int rv;
135 if(mlockall(MCL_CURRENT | MCL_FUTURE) != 0) {
136 return 2;
137 }
138
139 sig[0] = signal(SIGTERM, SIG_IGN);
140 sig[1] = signal(SIGINT, SIG_IGN);
141 sig[2] = signal(SIGQUIT, SIG_IGN);
142
143 rv = sysfs_set_num(info, NULL, "suspend_lo", start * chunk_size * data_disks);
144 rv |= sysfs_set_num(info, NULL, "suspend_hi", (start + 1) * chunk_size * data_disks);
145 return rv * 256;
146}
147
148int unlock_all_stripes(struct mdinfo *info, sighandler_t *sig) {
149 int rv;
150 rv = sysfs_set_num(info, NULL, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL);
151 rv |= sysfs_set_num(info, NULL, "suspend_hi", 0);
152 rv |= sysfs_set_num(info, NULL, "suspend_lo", 0);
153
154 signal(SIGQUIT, sig[2]);
155 signal(SIGINT, sig[1]);
156 signal(SIGTERM, sig[0]);
157
158 if(munlockall() != 0)
159 return 3;
160 return rv * 256;
161}
162
c22ac3b1 163/* Autorepair */
50786d47
N
164int autorepair(int *disk, unsigned long long start, int chunk_size,
165 char *name[], int raid_disks, int syndrome_disks, char **blocks_page,
166 char **blocks, uint8_t *p, int *block_index_for_slot,
c22ac3b1
PS
167 int *source, unsigned long long *offsets)
168{
169 int i, j;
170 int pages_to_write_count = 0;
171 int page_to_write[chunk_size >> CHECK_PAGE_BITS];
172 for(j = 0; j < (chunk_size >> CHECK_PAGE_BITS); j++) {
50786d47
N
173 if (disk[j] >= -2 && block_index_for_slot[disk[j]] >= 0) {
174 int slot = block_index_for_slot[disk[j]];
175 printf("Auto-repairing slot %d (%s)\n", slot, name[slot]);
c22ac3b1
PS
176 pages_to_write_count++;
177 page_to_write[j] = 1;
50786d47 178 for(i = -2; i < syndrome_disks; i++) {
c22ac3b1
PS
179 blocks_page[i] = blocks[i] + j * CHECK_PAGE_SIZE;
180 }
50786d47
N
181 if (disk[j] == -2) {
182 qsyndrome(p, (uint8_t*)blocks_page[-2],
183 (uint8_t**)blocks_page,
184 syndrome_disks, CHECK_PAGE_SIZE);
c22ac3b1
PS
185 }
186 else {
50786d47
N
187 char *all_but_failed_blocks[syndrome_disks];
188 for(i = 0; i < syndrome_disks; i++) {
189 if (i == disk[j])
190 all_but_failed_blocks[i] = blocks_page[-1];
191 else
c22ac3b1 192 all_but_failed_blocks[i] = blocks_page[i];
c22ac3b1 193 }
50786d47
N
194 xor_blocks(blocks_page[disk[j]],
195 all_but_failed_blocks, syndrome_disks,
196 CHECK_PAGE_SIZE);
c22ac3b1
PS
197 }
198 }
199 else {
200 page_to_write[j] = 0;
201 }
202 }
203
204 if(pages_to_write_count > 0) {
205 int write_res = 0;
206 for(j = 0; j < (chunk_size >> CHECK_PAGE_BITS); j++) {
207 if(page_to_write[j] == 1) {
50786d47
N
208 int slot = block_index_for_slot[disk[j]];
209 lseek64(source[slot], offsets[slot] + start * chunk_size + j * CHECK_PAGE_SIZE, SEEK_SET);
210 write_res += write(source[slot],
211 blocks[disk[j]] + j * CHECK_PAGE_SIZE,
212 CHECK_PAGE_SIZE);
c22ac3b1
PS
213 }
214 }
215
216 if (write_res != (CHECK_PAGE_SIZE * pages_to_write_count)) {
217 fprintf(stderr, "Failed to write a full chunk.\n");
218 return -1;
219 }
220 }
221
222 return 0;
223}
224
497b6d6b 225/* Manual repair */
50786d47
N
226int manual_repair(int chunk_size, int syndrome_disks,
227 int failed_slot1, int failed_slot2,
228 unsigned long long start, int *block_index_for_slot,
229 char *name[], char **stripes, char **blocks, uint8_t *p,
497b6d6b
PS
230 int *source, unsigned long long *offsets)
231{
497b6d6b 232 int i;
50786d47
N
233 int fd1 = block_index_for_slot[failed_slot1];
234 int fd2 = block_index_for_slot[failed_slot2];
497b6d6b
PS
235 printf("Repairing stripe %llu\n", start);
236 printf("Assuming slots %d (%s) and %d (%s) are incorrect\n",
50786d47
N
237 fd1, name[fd1],
238 fd2, name[fd2]);
497b6d6b 239
50786d47
N
240 if (failed_slot1 == -2 || failed_slot2 == -2) {
241 char *all_but_failed_blocks[syndrome_disks];
497b6d6b 242 int failed_data_or_p;
497b6d6b 243
50786d47
N
244 if (failed_slot1 == -2)
245 failed_data_or_p = failed_slot2;
246 else
247 failed_data_or_p = failed_slot1;
248
497b6d6b 249 printf("Repairing D/P(%d) and Q\n", failed_data_or_p);
50786d47
N
250
251 for (i = 0; i < syndrome_disks; i++) {
252 if (i == failed_data_or_p)
253 all_but_failed_blocks[i] = blocks[-1];
254 else
497b6d6b 255 all_but_failed_blocks[i] = blocks[i];
497b6d6b 256 }
50786d47
N
257 xor_blocks(blocks[failed_data_or_p],
258 all_but_failed_blocks, syndrome_disks, chunk_size);
259 qsyndrome(p, (uint8_t*)blocks[-2], (uint8_t**)blocks,
260 syndrome_disks, chunk_size);
261 } else {
497b6d6b 262 ensure_zero_has_size(chunk_size);
50786d47
N
263 if (failed_slot1 == -1 || failed_slot2 == -1) {
264 int failed_data;
265 if (failed_slot1 == -1)
266 failed_data = failed_slot2;
267 else
268 failed_data = failed_slot1;
269 printf("Repairing D(%d) and P\n", failed_data);
270 raid6_datap_recov(syndrome_disks+2, chunk_size,
271 failed_data, (uint8_t**)blocks, 1);
272 } else {
497b6d6b 273 printf("Repairing D and D\n");
50786d47
N
274 raid6_2data_recov(syndrome_disks+2, chunk_size,
275 failed_slot1, failed_slot2,
276 (uint8_t**)blocks, 1);
497b6d6b 277 }
497b6d6b
PS
278 }
279
280 int write_res1, write_res2;
281 off64_t seek_res;
282
50786d47
N
283 seek_res = lseek64(source[fd1],
284 offsets[fd1] + start * chunk_size, SEEK_SET);
497b6d6b
PS
285 if (seek_res < 0) {
286 fprintf(stderr, "lseek failed for failed_disk1\n");
287 return -1;
288 }
50786d47 289 write_res1 = write(source[fd1], blocks[failed_slot1], chunk_size);
497b6d6b 290
50786d47
N
291 seek_res = lseek64(source[fd2],
292 offsets[fd2] + start * chunk_size, SEEK_SET);
497b6d6b 293 if (seek_res < 0) {
50786d47 294 fprintf(stderr, "lseek failed for failed_disk2\n");
497b6d6b
PS
295 return -1;
296 }
50786d47 297 write_res2 = write(source[fd2], blocks[failed_slot2], chunk_size);
497b6d6b
PS
298
299 if (write_res1 != chunk_size || write_res2 != chunk_size) {
300 fprintf(stderr, "Failed to write a complete chunk.\n");
301 return -2;
302 }
303
304 return 0;
305}
306
8d8ab389 307int check_stripes(struct mdinfo *info, int *source, unsigned long long *offsets,
979afcb8 308 int raid_disks, int chunk_size, int level, int layout,
f2e29ad6 309 unsigned long long start, unsigned long long length, char *name[],
635b5861 310 enum repair repair, int failed_disk1, int failed_disk2)
979afcb8
PS
311{
312 /* read the data and p and q blocks, and check we got them right */
ad1a3c2f
N
313 int data_disks = raid_disks - 2;
314 int syndrome_disks = data_disks + is_ddf(layout) * 2;
503975b9 315 char *stripe_buf = xmalloc(raid_disks * chunk_size);
50786d47
N
316
317 /* stripes[] is indexed by raid_disk and holds chunks from each device */
503975b9 318 char **stripes = xmalloc(raid_disks * sizeof(char*));
50786d47
N
319
320 /* blocks[] is indexed by syndrome number and points to either one of the
321 * chunks from 'stripes[]', or to a chunk of zeros. -1 and -2 are
322 * P and Q */
ad1a3c2f 323 char **blocks = xmalloc((syndrome_disks + 2) * sizeof(char*));
50786d47
N
324
325 /* blocks_page[] is a temporary index to just one page of the chunks
326 * that blocks[] points to. */
327 char **blocks_page = xmalloc((syndrome_disks + 2) * sizeof(char*));
328
329 /* block_index_for_slot[] provides the reverse mapping from blocks to stripes.
330 * The index is a syndrome position, the content is a raid_disk number.
331 * indicies -1 and -2 work, and are P and Q disks */
ad1a3c2f 332 int *block_index_for_slot = xmalloc((syndrome_disks+2) * sizeof(int));
50786d47
N
333
334 /* 'p' and 'q' contain calcualted P and Q, to be compared with
335 * blocks[-1] and blocks[-2];
336 */
503975b9
N
337 uint8_t *p = xmalloc(chunk_size);
338 uint8_t *q = xmalloc(chunk_size);
ad1a3c2f 339 char *zero = xmalloc(chunk_size);
503975b9 340 int *results = xmalloc(chunk_size * sizeof(int));
351d7680 341 sighandler_t *sig = xmalloc(3 * sizeof(sighandler_t));
979afcb8 342
3b9c9603 343 int i, j;
76cd79d3 344 int diskP, diskQ, diskD;
af3c3750 345 int err = 0;
979afcb8
PS
346
347 extern int tables_ready;
348
349 if (!tables_ready)
350 make_tables();
351
50786d47
N
352 block_index_for_slot += 2;
353 blocks += 2;
354 blocks_page += 2;
355
ad1a3c2f 356 memset(zero, 0, chunk_size);
979afcb8
PS
357 for ( i = 0 ; i < raid_disks ; i++)
358 stripes[i] = stripe_buf + i * chunk_size;
359
360 while (length > 0) {
50786d47
N
361 /* The syndrome number of the broken disk is recorded
362 * in 'disk[]' which allows a different broken disk for
363 * each page.
364 */
3b9c9603 365 int disk[chunk_size >> CHECK_PAGE_BITS];
979afcb8 366
351d7680
RB
367 err = lock_stripe(info, start, chunk_size, data_disks, sig);
368 if(err != 0) {
369 if (err != 2)
370 unlock_all_stripes(info, sig);
8d8ab389
PS
371 goto exitCheck;
372 }
979afcb8 373 for (i = 0 ; i < raid_disks ; i++) {
2161adce
BS
374 off64_t seek_res = lseek64(source[i], offsets[i] + start * chunk_size,
375 SEEK_SET);
376 if (seek_res < 0) {
377 fprintf(stderr, "lseek to source %d failed\n", i);
378 unlock_all_stripes(info, sig);
379 err = -1;
380 goto exitCheck;
381 }
2c7b668d
BS
382 int read_res = read(source[i], stripes[i], chunk_size);
383 if (read_res < chunk_size) {
384 fprintf(stderr, "Failed to read complete chunk disk %d, aborting\n", i);
385 unlock_all_stripes(info, sig);
2161adce 386 err = -1;
2c7b668d
BS
387 goto exitCheck;
388 }
979afcb8 389 }
8d8ab389 390
76cd79d3 391 diskP = geo_map(-1, start, raid_disks, level, layout);
50786d47
N
392 block_index_for_slot[-1] = diskP;
393 blocks[-1] = stripes[diskP];
394
76cd79d3 395 diskQ = geo_map(-2, start, raid_disks, level, layout);
50786d47
N
396 block_index_for_slot[-2] = diskQ;
397 blocks[-2] = stripes[diskQ];
398
ad1a3c2f
N
399 if (!is_ddf(layout)) {
400 /* The syndrome-order of disks starts immediately after 'Q',
401 * but skips P */
402 diskD = diskQ;
403 for (i = 0 ; i < data_disks ; i++) {
404 diskD = diskD + 1;
405 if (diskD >= raid_disks)
406 diskD = 0;
407 if (diskD == diskP)
408 diskD += 1;
409 if (diskD >= raid_disks)
410 diskD = 0;
411 blocks[i] = stripes[diskD];
50786d47 412 block_index_for_slot[i] = diskD;
ad1a3c2f
N
413 }
414 } else {
415 /* The syndrome-order exactly follows raid-disk
416 * numbers, with ZERO in place of P and Q
417 */
50786d47
N
418 for (i = 0 ; i < raid_disks; i++) {
419 if (i == diskP || i == diskQ) {
ad1a3c2f 420 blocks[i] = zero;
50786d47
N
421 block_index_for_slot[i] = -1;
422 } else {
ad1a3c2f 423 blocks[i] = stripes[i];
50786d47
N
424 block_index_for_slot[i] = i;
425 }
426 }
979afcb8
PS
427 }
428
ad1a3c2f 429 qsyndrome(p, q, (uint8_t**)blocks, syndrome_disks, chunk_size);
237e40ce 430
af3c3750 431 raid6_collect(chunk_size, p, q, stripes[diskP], stripes[diskQ], results);
3b9c9603 432 raid6_stats(disk, results, raid_disks, chunk_size);
979afcb8 433
3b9c9603 434 for(j = 0; j < (chunk_size >> CHECK_PAGE_BITS); j++) {
108bd874 435 int role = disk[j];
50786d47
N
436 if (role >= -2) {
437 int slot = block_index_for_slot[role];
438 if (slot >= 0)
439 printf("Error detected at stripe %llu, page %d: possible failed disk slot %d: %d --> %s\n",
440 start, j, role, slot, name[slot]);
441 else
442 printf("Error detected at stripe %llu, page %d: failed slot %d should be zeros\n",
443 start, j, role);
444 } else if(disk[j] == -65535) {
afc755e9 445 printf("Error detected at stripe %llu, page %d: disk slot unknown\n", start, j);
3b9c9603 446 }
979afcb8 447 }
3b9c9603 448
3cfd0297 449 if(repair == AUTO_REPAIR) {
50786d47
N
450 err = autorepair(disk, start, chunk_size,
451 name, raid_disks, syndrome_disks, blocks_page,
452 blocks, p, block_index_for_slot,
c22ac3b1
PS
453 source, offsets);
454 if(err != 0) {
455 unlock_all_stripes(info, sig);
456 goto exitCheck;
3cfd0297
PS
457 }
458 }
459
635b5861 460 if(repair == MANUAL_REPAIR) {
50786d47
N
461 int failed_slot1 = -1, failed_slot2 = -1;
462 for (i = -2; i < syndrome_disks; i++) {
463 if (block_index_for_slot[i] == failed_disk1)
464 failed_slot1 = i;
465 if (block_index_for_slot[i] == failed_disk2)
466 failed_slot2 = i;
467 }
468 err = manual_repair(chunk_size, syndrome_disks,
469 failed_slot1, failed_slot2,
470 start, block_index_for_slot,
471 name, stripes, blocks, p,
472 source, offsets);
497b6d6b 473 if(err == -1) {
2161adce 474 unlock_all_stripes(info, sig);
2161adce
BS
475 goto exitCheck;
476 }
3b9c9603
PS
477 }
478
50786d47
N
479 err = unlock_all_stripes(info, sig);
480 if(err != 0) {
481 goto exitCheck;
482 }
483
af3c3750
PS
484 length--;
485 start++;
979afcb8
PS
486 }
487
af3c3750
PS
488exitCheck:
489
979afcb8
PS
490 free(stripe_buf);
491 free(stripes);
50786d47
N
492 free(blocks-2);
493 free(blocks_page-2);
494 free(block_index_for_slot-2);
979afcb8
PS
495 free(p);
496 free(q);
497 free(results);
3a89d754 498 free(sig);
979afcb8 499
af3c3750 500 return err;
979afcb8
PS
501}
502
503unsigned long long getnum(char *str, char **err)
504{
505 char *e;
506 unsigned long long rv = strtoull(str, &e, 10);
507 if (e==str || *e) {
508 *err = str;
509 return 0;
510 }
511 return rv;
512}
513
514int main(int argc, char *argv[])
515{
a9c2c6c6 516 /* md_device start length */
af3c3750
PS
517 int *fds = NULL;
518 char *buf = NULL;
519 char **disk_name = NULL;
520 unsigned long long *offsets = NULL;
521 int raid_disks = 0;
2cf31121 522 int active_disks;
af3c3750
PS
523 int chunk_size = 0;
524 int layout = -1;
979afcb8 525 int level = 6;
635b5861 526 enum repair repair = NO_REPAIR;
2c7b668d
BS
527 int failed_disk1 = -1;
528 int failed_disk2 = -1;
979afcb8
PS
529 unsigned long long start, length;
530 int i;
a9c2c6c6 531 int mdfd;
8d8ab389 532 struct mdinfo *info = NULL, *comp = NULL;
979afcb8 533 char *err = NULL;
af3c3750
PS
534 int exit_err = 0;
535 int close_flag = 0;
536 char *prg = strrchr(argv[0], '/');
537
538 if (prg == NULL)
539 prg = argv[0];
540 else
541 prg++;
542
543 if (argc < 4) {
8a63c731 544 fprintf(stderr, "Usage: %s md_device start_stripe length_stripes [autorepair]\n", prg);
f2e29ad6 545 fprintf(stderr, " or: %s md_device repair stripe failed_slot_1 failed_slot_2\n", prg);
af3c3750
PS
546 exit_err = 1;
547 goto exitHere;
979afcb8
PS
548 }
549
a9c2c6c6
PS
550 mdfd = open(argv[1], O_RDONLY);
551 if(mdfd < 0) {
552 perror(argv[1]);
e7b84f9d 553 fprintf(stderr, "%s: cannot open %s\n", prg, argv[1]);
af3c3750
PS
554 exit_err = 2;
555 goto exitHere;
a9c2c6c6
PS
556 }
557
f8fcf7a1 558 info = sysfs_read(mdfd, NULL,
a9c2c6c6
PS
559 GET_LEVEL|
560 GET_LAYOUT|
561 GET_DISKS|
2cf31121 562 GET_DEGRADED |
a9c2c6c6
PS
563 GET_COMPONENT|
564 GET_CHUNK|
565 GET_DEVS|
566 GET_OFFSET|
567 GET_SIZE);
568
8d8ab389
PS
569 if(info == NULL) {
570 fprintf(stderr, "%s: Error reading sysfs information of %s\n", prg, argv[1]);
571 exit_err = 9;
572 goto exitHere;
573 }
574
a9c2c6c6
PS
575 if(info->array.level != level) {
576 fprintf(stderr, "%s: %s not a RAID-6\n", prg, argv[1]);
af3c3750
PS
577 exit_err = 3;
578 goto exitHere;
a9c2c6c6
PS
579 }
580
2cf31121
PS
581 if(info->array.failed_disks > 0) {
582 fprintf(stderr, "%s: %s degraded array\n", prg, argv[1]);
583 exit_err = 8;
584 goto exitHere;
585 }
586
a9c2c6c6
PS
587 printf("layout: %d\n", info->array.layout);
588 printf("disks: %d\n", info->array.raid_disks);
af3c3750
PS
589 printf("component size: %llu\n", info->component_size * 512);
590 printf("total stripes: %llu\n", (info->component_size * 512) / info->array.chunk_size);
a9c2c6c6
PS
591 printf("chunk size: %d\n", info->array.chunk_size);
592 printf("\n");
593
594 comp = info->devs;
2cf31121 595 for(i = 0, active_disks = 0; active_disks < info->array.raid_disks; i++) {
a9c2c6c6 596 printf("disk: %d - offset: %llu - size: %llu - name: %s - slot: %d\n",
af3c3750 597 i, comp->data_offset * 512, comp->component_size * 512,
a9c2c6c6
PS
598 map_dev(comp->disk.major, comp->disk.minor, 0),
599 comp->disk.raid_disk);
2cf31121
PS
600 if(comp->disk.raid_disk >= 0)
601 active_disks++;
a9c2c6c6
PS
602 comp = comp->next;
603 }
604 printf("\n");
605
606 close(mdfd);
607
608 raid_disks = info->array.raid_disks;
609 chunk_size = info->array.chunk_size;
610 layout = info->array.layout;
f2e29ad6
RB
611 if (strcmp(argv[2], "repair")==0) {
612 if (argc < 6) {
613 fprintf(stderr, "For repair mode, call %s md_device repair stripe failed_slot_1 failed_slot_2\n", prg);
614 exit_err = 1;
615 goto exitHere;
616 }
635b5861 617 repair = MANUAL_REPAIR;
f2e29ad6
RB
618 start = getnum(argv[3], &err);
619 length = 1;
620 failed_disk1 = getnum(argv[4], &err);
621 failed_disk2 = getnum(argv[5], &err);
622
b67e45b8 623 if(failed_disk1 >= info->array.raid_disks) {
f2e29ad6
RB
624 fprintf(stderr, "%s: failed_slot_1 index is higher than number of devices in raid\n", prg);
625 exit_err = 4;
626 goto exitHere;
627 }
b67e45b8 628 if(failed_disk2 >= info->array.raid_disks) {
f2e29ad6
RB
629 fprintf(stderr, "%s: failed_slot_2 index is higher than number of devices in raid\n", prg);
630 exit_err = 4;
631 goto exitHere;
632 }
633 if(failed_disk1 == failed_disk2) {
634 fprintf(stderr, "%s: failed_slot_1 and failed_slot_2 are the same\n", prg);
635 exit_err = 4;
636 goto exitHere;
637 }
638 }
639 else {
640 start = getnum(argv[2], &err);
641 length = getnum(argv[3], &err);
8a63c731 642 if (argc >= 5 && strcmp(argv[4], "autorepair")==0)
635b5861 643 repair = AUTO_REPAIR;
f2e29ad6 644 }
a9c2c6c6 645
979afcb8 646 if (err) {
a9c2c6c6 647 fprintf(stderr, "%s: Bad number: %s\n", prg, err);
af3c3750
PS
648 exit_err = 4;
649 goto exitHere;
979afcb8 650 }
a9c2c6c6 651
af3c3750
PS
652 if(start > ((info->component_size * 512) / chunk_size)) {
653 start = (info->component_size * 512) / chunk_size;
654 fprintf(stderr, "%s: start beyond disks size\n", prg);
655 }
a9c2c6c6 656
af3c3750
PS
657 if((length == 0) ||
658 ((length + start) > ((info->component_size * 512) / chunk_size))) {
659 length = (info->component_size * 512) / chunk_size - start;
979afcb8 660 }
a9c2c6c6 661
503975b9
N
662 disk_name = xmalloc(raid_disks * sizeof(*disk_name));
663 fds = xmalloc(raid_disks * sizeof(*fds));
664 offsets = xcalloc(raid_disks, sizeof(*offsets));
665 buf = xmalloc(raid_disks * chunk_size);
af3c3750 666
af3c3750
PS
667 for(i=0; i<raid_disks; i++) {
668 fds[i] = -1;
669 }
670 close_flag = 1;
979afcb8 671
a9c2c6c6 672 comp = info->devs;
2cf31121 673 for (i=0, active_disks=0; active_disks<raid_disks; i++) {
a9c2c6c6 674 int disk_slot = comp->disk.raid_disk;
2cf31121
PS
675 if(disk_slot >= 0) {
676 disk_name[disk_slot] = map_dev(comp->disk.major, comp->disk.minor, 0);
677 offsets[disk_slot] = comp->data_offset * 512;
e645b341 678 fds[disk_slot] = open(disk_name[disk_slot], O_RDWR | O_SYNC);
2cf31121
PS
679 if (fds[disk_slot] < 0) {
680 perror(disk_name[disk_slot]);
681 fprintf(stderr,"%s: cannot open %s\n", prg, disk_name[disk_slot]);
682 exit_err = 6;
683 goto exitHere;
684 }
685 active_disks++;
979afcb8 686 }
a9c2c6c6 687 comp = comp->next;
979afcb8
PS
688 }
689
8d8ab389 690 int rv = check_stripes(info, fds, offsets,
979afcb8 691 raid_disks, chunk_size, level, layout,
f2e29ad6 692 start, length, disk_name, repair, failed_disk1, failed_disk2);
979afcb8 693 if (rv != 0) {
7a862a02 694 fprintf(stderr, "%s: check_stripes returned %d\n", prg, rv);
af3c3750
PS
695 exit_err = 7;
696 goto exitHere;
979afcb8
PS
697 }
698
af3c3750
PS
699exitHere:
700
701 if (close_flag)
702 for(i = 0; i < raid_disks; i++)
703 close(fds[i]);
704
a9c2c6c6 705 free(disk_name);
979afcb8
PS
706 free(fds);
707 free(offsets);
708 free(buf);
709
af3c3750 710 exit(exit_err);
979afcb8 711}