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