]> git.neil.brown.name Git - mdadm.git/blob - raid6check.c
mdadm: fix a buffer overflow
[mdadm.git] / raid6check.c
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>
27 #include <signal.h>
28 #include <sys/mman.h>
29
30 #define CHECK_PAGE_BITS (12)
31 #define CHECK_PAGE_SIZE (1 << CHECK_PAGE_BITS)
32
33 char const Name[] = "raid6check";
34
35 enum repair {
36         NO_REPAIR = 0,
37         MANUAL_REPAIR,
38         AUTO_REPAIR
39 };
40
41 int geo_map(int block, unsigned long long stripe, int raid_disks,
42             int level, int layout);
43 int is_ddf(int layout);
44 void qsyndrome(uint8_t *p, uint8_t *q, uint8_t **sources, int disks, int size);
45 void make_tables(void);
46 void ensure_zero_has_size(int chunk_size);
47 void raid6_datap_recov(int disks, size_t bytes, int faila, uint8_t **ptrs,
48                        int neg_offset);
49 void raid6_2data_recov(int disks, size_t bytes, int faila, int failb,
50                        uint8_t **ptrs, int neg_offset);
51 void xor_blocks(char *target, char **sources, int disks, int size);
52
53 /* Collect per stripe consistency information */
54 void 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
83 /* Try to find out if a specific disk has problems in a CHECK_PAGE_SIZE page size */
84 int raid6_stats_blk(int *results, int raid_disks)
85 {
86         int i;
87         int curr_broken_disk = -255;
88         int prev_broken_disk = -255;
89         int broken_status = 0;
90
91         for(i = 0; i < CHECK_PAGE_SIZE; i++) {
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
122 /* Collect disks status for a strip in CHECK_PAGE_SIZE page size blocks */
123 void 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
132 int 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
148 int 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
163 /* Autorepair */
164 int 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,
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++) {
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]);
176                         pages_to_write_count++;
177                         page_to_write[j] = 1;
178                         for(i = -2; i < syndrome_disks; i++) {
179                                 blocks_page[i] = blocks[i] + j * CHECK_PAGE_SIZE;
180                         }
181                         if (disk[j] == -2) {
182                                 qsyndrome(p, (uint8_t*)blocks_page[-2],
183                                           (uint8_t**)blocks_page,
184                                           syndrome_disks, CHECK_PAGE_SIZE);
185                         }
186                         else {
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
192                                                 all_but_failed_blocks[i] = blocks_page[i];
193                                 }
194                                 xor_blocks(blocks_page[disk[j]],
195                                            all_but_failed_blocks, syndrome_disks,
196                                            CHECK_PAGE_SIZE);
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) {
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);
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
225 /* Manual repair */
226 int 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,
230                   int *source, unsigned long long *offsets)
231 {
232         int i;
233         int fd1 = block_index_for_slot[failed_slot1];
234         int fd2 = block_index_for_slot[failed_slot2];
235         printf("Repairing stripe %llu\n", start);
236         printf("Assuming slots %d (%s) and %d (%s) are incorrect\n",
237                fd1, name[fd1],
238                fd2, name[fd2]);
239
240         if (failed_slot1 == -2 || failed_slot2 == -2) {
241                 char *all_but_failed_blocks[syndrome_disks];
242                 int failed_data_or_p;
243
244                 if (failed_slot1 == -2)
245                         failed_data_or_p = failed_slot2;
246                 else
247                         failed_data_or_p = failed_slot1;
248
249                 printf("Repairing D/P(%d) and Q\n", failed_data_or_p);
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
255                                 all_but_failed_blocks[i] = blocks[i];
256                 }
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 {
262                 ensure_zero_has_size(chunk_size);
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 {
273                         printf("Repairing D and D\n");
274                         raid6_2data_recov(syndrome_disks+2, chunk_size,
275                                           failed_slot1, failed_slot2,
276                                           (uint8_t**)blocks, 1);
277                 }
278         }
279
280         int write_res1, write_res2;
281         off64_t seek_res;
282
283         seek_res = lseek64(source[fd1],
284                            offsets[fd1] + start * chunk_size, SEEK_SET);
285         if (seek_res < 0) {
286                 fprintf(stderr, "lseek failed for failed_disk1\n");
287                 return -1;
288         }
289         write_res1 = write(source[fd1], blocks[failed_slot1], chunk_size);
290
291         seek_res = lseek64(source[fd2],
292                            offsets[fd2] + start * chunk_size, SEEK_SET);
293         if (seek_res < 0) {
294                 fprintf(stderr, "lseek failed for failed_disk2\n");
295                 return -1;
296         }
297         write_res2 = write(source[fd2], blocks[failed_slot2], chunk_size);
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
307 int check_stripes(struct mdinfo *info, int *source, unsigned long long *offsets,
308                   int raid_disks, int chunk_size, int level, int layout,
309                   unsigned long long start, unsigned long long length, char *name[],
310                   enum repair repair, int failed_disk1, int failed_disk2)
311 {
312         /* read the data and p and q blocks, and check we got them right */
313         int data_disks = raid_disks - 2;
314         int syndrome_disks = data_disks + is_ddf(layout) * 2;
315         char *stripe_buf;
316
317         /* stripes[] is indexed by raid_disk and holds chunks from each device */
318         char **stripes = xmalloc(raid_disks * sizeof(char*));
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 */
323         char **blocks = xmalloc((syndrome_disks + 2) * sizeof(char*));
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 */
332         int *block_index_for_slot = xmalloc((syndrome_disks+2) * sizeof(int));
333
334         /* 'p' and 'q' contain calcualted P and Q, to be compared with
335          * blocks[-1] and blocks[-2];
336          */
337         uint8_t *p = xmalloc(chunk_size);
338         uint8_t *q = xmalloc(chunk_size);
339         char *zero = xmalloc(chunk_size);
340         int *results = xmalloc(chunk_size * sizeof(int));
341         sighandler_t *sig = xmalloc(3 * sizeof(sighandler_t));
342
343         int i, j;
344         int diskP, diskQ, diskD;
345         int err = 0;
346
347         extern int tables_ready;
348
349         if (!tables_ready)
350                 make_tables();
351
352         if (posix_memalign((void**)&stripe_buf, 4096, raid_disks * chunk_size) != 0)
353                 exit(4);
354         block_index_for_slot += 2;
355         blocks += 2;
356         blocks_page += 2;
357
358         memset(zero, 0, chunk_size);
359         for ( i = 0 ; i < raid_disks ; i++)
360                 stripes[i] = stripe_buf + i * chunk_size;
361
362         while (length > 0) {
363                 /* The syndrome number of the broken disk is recorded
364                  * in 'disk[]' which allows a different broken disk for
365                  * each page.
366                  */
367                 int disk[chunk_size >> CHECK_PAGE_BITS];
368
369                 err = lock_stripe(info, start, chunk_size, data_disks, sig);
370                 if(err != 0) {
371                         if (err != 2)
372                                 unlock_all_stripes(info, sig);
373                         goto exitCheck;
374                 }
375                 for (i = 0 ; i < raid_disks ; i++) {
376                         off64_t seek_res = lseek64(source[i], offsets[i] + start * chunk_size,
377                                                    SEEK_SET);
378                         if (seek_res < 0) {
379                                 fprintf(stderr, "lseek to source %d failed\n", i);
380                                 unlock_all_stripes(info, sig);
381                                 err = -1;
382                                 goto exitCheck;
383                         }
384                         int read_res = read(source[i], stripes[i], chunk_size);
385                         if (read_res < chunk_size) {
386                                 fprintf(stderr, "Failed to read complete chunk disk %d, aborting\n", i);
387                                 unlock_all_stripes(info, sig);
388                                 err = -1;
389                                 goto exitCheck;
390                         }
391                 }
392
393                 diskP = geo_map(-1, start, raid_disks, level, layout);
394                 block_index_for_slot[-1] = diskP;
395                 blocks[-1] = stripes[diskP];
396
397                 diskQ = geo_map(-2, start, raid_disks, level, layout);
398                 block_index_for_slot[-2] = diskQ;
399                 blocks[-2] = stripes[diskQ];
400
401                 if (!is_ddf(layout)) {
402                         /* The syndrome-order of disks starts immediately after 'Q',
403                          * but skips P */
404                         diskD = diskQ;
405                         for (i = 0 ; i < data_disks ; i++) {
406                                 diskD = diskD + 1;
407                                 if (diskD >= raid_disks)
408                                         diskD = 0;
409                                 if (diskD == diskP)
410                                         diskD += 1;
411                                 if (diskD >= raid_disks)
412                                         diskD = 0;
413                                 blocks[i] = stripes[diskD];
414                                 block_index_for_slot[i] = diskD;
415                         }
416                 } else {
417                         /* The syndrome-order exactly follows raid-disk
418                          * numbers, with ZERO in place of P and Q
419                          */
420                         for (i = 0 ; i < raid_disks; i++) {
421                                 if (i == diskP || i == diskQ) {
422                                         blocks[i] = zero;
423                                         block_index_for_slot[i] = -1;
424                                 } else {
425                                         blocks[i] = stripes[i];
426                                         block_index_for_slot[i] = i;
427                                 }
428                         }
429                 }
430
431                 qsyndrome(p, q, (uint8_t**)blocks, syndrome_disks, chunk_size);
432
433                 raid6_collect(chunk_size, p, q, stripes[diskP], stripes[diskQ], results);
434                 raid6_stats(disk, results, raid_disks, chunk_size);
435
436                 for(j = 0; j < (chunk_size >> CHECK_PAGE_BITS); j++) {
437                         int role = disk[j];
438                         if (role >= -2) {
439                                 int slot = block_index_for_slot[role];
440                                 if (slot >= 0)
441                                         printf("Error detected at stripe %llu, page %d: possible failed disk slot %d: %d --> %s\n",
442                                                start, j, role, slot, name[slot]);
443                                 else
444                                         printf("Error detected at stripe %llu, page %d: failed slot %d should be zeros\n",
445                                                start, j, role);
446                         } else if(disk[j] == -65535) {
447                                 printf("Error detected at stripe %llu, page %d: disk slot unknown\n", start, j);
448                         }
449                 }
450
451                 if(repair == AUTO_REPAIR) {
452                         err = autorepair(disk, start, chunk_size,
453                                         name, raid_disks, syndrome_disks, blocks_page,
454                                         blocks, p, block_index_for_slot,
455                                         source, offsets);
456                         if(err != 0) {
457                                 unlock_all_stripes(info, sig);
458                                 goto exitCheck;
459                         }
460                 }
461
462                 if(repair == MANUAL_REPAIR) {
463                         int failed_slot1 = -1, failed_slot2 = -1;
464                         for (i = -2; i < syndrome_disks; i++) {
465                                 if (block_index_for_slot[i] == failed_disk1)
466                                         failed_slot1 = i;
467                                 if (block_index_for_slot[i] == failed_disk2)
468                                         failed_slot2 = i;
469                         }
470                         err = manual_repair(chunk_size, syndrome_disks,
471                                             failed_slot1, failed_slot2,
472                                             start, block_index_for_slot,
473                                             name, stripes, blocks, p,
474                                             source, offsets);
475                         if(err == -1) {
476                                 unlock_all_stripes(info, sig);
477                                 goto exitCheck;
478                         }
479                 }
480
481                 err = unlock_all_stripes(info, sig);
482                 if(err != 0) {
483                         goto exitCheck;
484                 }
485
486                 length--;
487                 start++;
488         }
489
490 exitCheck:
491
492         free(stripe_buf);
493         free(stripes);
494         free(blocks-2);
495         free(blocks_page-2);
496         free(block_index_for_slot-2);
497         free(p);
498         free(q);
499         free(results);
500         free(sig);
501
502         return err;
503 }
504
505 unsigned long long getnum(char *str, char **err)
506 {
507         char *e;
508         unsigned long long rv = strtoull(str, &e, 10);
509         if (e==str || *e) {
510                 *err = str;
511                 return 0;
512         }
513         return rv;
514 }
515
516 int main(int argc, char *argv[])
517 {
518         /* md_device start length */
519         int *fds = NULL;
520         char *buf = NULL;
521         char **disk_name = NULL;
522         unsigned long long *offsets = NULL;
523         int raid_disks = 0;
524         int active_disks;
525         int chunk_size = 0;
526         int layout = -1;
527         int level = 6;
528         enum repair repair = NO_REPAIR;
529         int failed_disk1 = -1;
530         int failed_disk2 = -1;
531         unsigned long long start, length;
532         int i;
533         int mdfd;
534         struct mdinfo *info = NULL, *comp = NULL;
535         char *err = NULL;
536         int exit_err = 0;
537         int close_flag = 0;
538         char *prg = strrchr(argv[0], '/');
539
540         if (prg == NULL)
541                 prg = argv[0];
542         else
543                 prg++;
544
545         if (argc < 4) {
546                 fprintf(stderr, "Usage: %s md_device start_stripe length_stripes [autorepair]\n", prg);
547                 fprintf(stderr, "   or: %s md_device repair stripe failed_slot_1 failed_slot_2\n", prg);
548                 exit_err = 1;
549                 goto exitHere;
550         }
551
552         mdfd = open(argv[1], O_RDONLY);
553         if(mdfd < 0) {
554                 perror(argv[1]);
555                 fprintf(stderr, "%s: cannot open %s\n", prg, argv[1]);
556                 exit_err = 2;
557                 goto exitHere;
558         }
559
560         info = sysfs_read(mdfd, NULL,
561                           GET_LEVEL|
562                           GET_LAYOUT|
563                           GET_DISKS|
564                           GET_DEGRADED |
565                           GET_COMPONENT|
566                           GET_CHUNK|
567                           GET_DEVS|
568                           GET_OFFSET|
569                           GET_SIZE);
570
571         if(info == NULL) {
572                 fprintf(stderr, "%s: Error reading sysfs information of %s\n", prg, argv[1]);
573                 exit_err = 9;
574                 goto exitHere;
575         }
576
577         if(info->array.level != level) {
578                 fprintf(stderr, "%s: %s not a RAID-6\n", prg, argv[1]);
579                 exit_err = 3;
580                 goto exitHere;
581         }
582
583         if(info->array.failed_disks > 0) {
584                 fprintf(stderr, "%s: %s degraded array\n", prg, argv[1]);
585                 exit_err = 8;
586                 goto exitHere;
587         }
588
589         printf("layout: %d\n", info->array.layout);
590         printf("disks: %d\n", info->array.raid_disks);
591         printf("component size: %llu\n", info->component_size * 512);
592         printf("total stripes: %llu\n", (info->component_size * 512) / info->array.chunk_size);
593         printf("chunk size: %d\n", info->array.chunk_size);
594         printf("\n");
595
596         comp = info->devs;
597         for(i = 0, active_disks = 0; active_disks < info->array.raid_disks; i++) {
598                 printf("disk: %d - offset: %llu - size: %llu - name: %s - slot: %d\n",
599                         i, comp->data_offset * 512, comp->component_size * 512,
600                         map_dev(comp->disk.major, comp->disk.minor, 0),
601                         comp->disk.raid_disk);
602                 if(comp->disk.raid_disk >= 0)
603                         active_disks++;
604                 comp = comp->next;
605         }
606         printf("\n");
607
608         close(mdfd);
609
610         raid_disks = info->array.raid_disks;
611         chunk_size = info->array.chunk_size;
612         layout = info->array.layout;
613         if (strcmp(argv[2], "repair")==0) {
614                 if (argc < 6) {
615                         fprintf(stderr, "For repair mode, call %s md_device repair stripe failed_slot_1 failed_slot_2\n", prg);
616                         exit_err = 1;
617                         goto exitHere;
618                 }
619                 repair = MANUAL_REPAIR;
620                 start = getnum(argv[3], &err);
621                 length = 1;
622                 failed_disk1 = getnum(argv[4], &err);
623                 failed_disk2 = getnum(argv[5], &err);
624
625                 if(failed_disk1 >= info->array.raid_disks) {
626                         fprintf(stderr, "%s: failed_slot_1 index is higher than number of devices in raid\n", prg);
627                         exit_err = 4;
628                         goto exitHere;
629                 }
630                 if(failed_disk2 >= info->array.raid_disks) {
631                         fprintf(stderr, "%s: failed_slot_2 index is higher than number of devices in raid\n", prg);
632                         exit_err = 4;
633                         goto exitHere;
634                 }
635                 if(failed_disk1 == failed_disk2) {
636                         fprintf(stderr, "%s: failed_slot_1 and failed_slot_2 are the same\n", prg);
637                         exit_err = 4;
638                         goto exitHere;
639                 }
640         }
641         else {
642                 start = getnum(argv[2], &err);
643                 length = getnum(argv[3], &err);
644                 if (argc >= 5 && strcmp(argv[4], "autorepair")==0)
645                         repair = AUTO_REPAIR;
646         }
647
648         if (err) {
649                 fprintf(stderr, "%s: Bad number: %s\n", prg, err);
650                 exit_err = 4;
651                 goto exitHere;
652         }
653
654         if(start > ((info->component_size * 512) / chunk_size)) {
655                 start = (info->component_size * 512) / chunk_size;
656                 fprintf(stderr, "%s: start beyond disks size\n", prg);
657         }
658
659         if((length == 0) ||
660            ((length + start) > ((info->component_size * 512) / chunk_size))) {
661                 length = (info->component_size * 512) / chunk_size - start;
662         }
663
664         disk_name = xmalloc(raid_disks * sizeof(*disk_name));
665         fds = xmalloc(raid_disks * sizeof(*fds));
666         offsets = xcalloc(raid_disks, sizeof(*offsets));
667         buf = xmalloc(raid_disks * chunk_size);
668
669         for(i=0; i<raid_disks; i++) {
670                 fds[i] = -1;
671         }
672         close_flag = 1;
673
674         comp = info->devs;
675         for (i=0, active_disks=0; active_disks<raid_disks; i++) {
676                 int disk_slot = comp->disk.raid_disk;
677                 if(disk_slot >= 0) {
678                         disk_name[disk_slot] = map_dev(comp->disk.major, comp->disk.minor, 0);
679                         offsets[disk_slot] = comp->data_offset * 512;
680                         fds[disk_slot] = open(disk_name[disk_slot], O_RDWR | O_DIRECT);
681                         if (fds[disk_slot] < 0) {
682                                 perror(disk_name[disk_slot]);
683                                 fprintf(stderr,"%s: cannot open %s\n", prg, disk_name[disk_slot]);
684                                 exit_err = 6;
685                                 goto exitHere;
686                         }
687                         active_disks++;
688                 }
689                 comp = comp->next;
690         }
691
692         int rv = check_stripes(info, fds, offsets,
693                                raid_disks, chunk_size, level, layout,
694                                start, length, disk_name, repair, failed_disk1, failed_disk2);
695         if (rv != 0) {
696                 fprintf(stderr, "%s: check_stripes returned %d\n", prg, rv);
697                 exit_err = 7;
698                 goto exitHere;
699         }
700
701 exitHere:
702
703         if (close_flag)
704                 for(i = 0; i < raid_disks; i++)
705                         close(fds[i]);
706
707         free(disk_name);
708         free(fds);
709         free(offsets);
710         free(buf);
711
712         exit(exit_err);
713 }