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