]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - raid6check.c
2 * raid6check - extended consistency check for RAID-6
4 * Copyright (C) 2011 Piergiorgio Sartor
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.
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.
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
21 * Author: Piergiorgio Sartor
22 * Based on "restripe.c" from "mdadm" codebase
29 #define CHECK_PAGE_BITS (12)
30 #define CHECK_PAGE_SIZE (1 << CHECK_PAGE_BITS)
32 char const Name
[] = "raid6check";
40 int geo_map(int block
, unsigned long long stripe
, int raid_disks
,
41 int level
, int layout
);
42 int is_ddf(int layout
);
43 void qsyndrome(uint8_t *p
, uint8_t *q
, uint8_t **sources
, int disks
, int size
);
44 void make_tables(void);
45 void ensure_zero_has_size(int chunk_size
);
46 void raid6_datap_recov(int disks
, size_t bytes
, int faila
, uint8_t **ptrs
,
48 void raid6_2data_recov(int disks
, size_t bytes
, int faila
, int failb
,
49 uint8_t **ptrs
, int neg_offset
);
50 void xor_blocks(char *target
, char **sources
, int disks
, int size
);
52 /* Collect per stripe consistency information */
53 void raid6_collect(int chunk_size
, uint8_t *p
, uint8_t *q
,
54 char *chunkP
, char *chunkQ
, int *results
)
59 extern uint8_t raid6_gflog
[];
61 for(i
= 0; i
< chunk_size
; i
++) {
62 Px
= (uint8_t)chunkP
[i
] ^ (uint8_t)p
[i
];
63 Qx
= (uint8_t)chunkQ
[i
] ^ (uint8_t)q
[i
];
65 if((Px
!= 0) && (Qx
== 0))
68 if((Px
== 0) && (Qx
!= 0))
71 if((Px
!= 0) && (Qx
!= 0)) {
72 data_id
= (raid6_gflog
[Qx
] - raid6_gflog
[Px
]);
73 if(data_id
< 0) data_id
+= 255;
77 if((Px
== 0) && (Qx
== 0))
82 /* Try to find out if a specific disk has problems in a CHECK_PAGE_SIZE page size */
83 int raid6_stats_blk(int *results
, int raid_disks
)
86 int curr_broken_disk
= -255;
87 int prev_broken_disk
= -255;
88 int broken_status
= 0;
90 for(i
= 0; i
< CHECK_PAGE_SIZE
; i
++) {
92 if(results
[i
] != -255)
93 curr_broken_disk
= results
[i
];
95 if(curr_broken_disk
>= raid_disks
)
98 switch(broken_status
) {
100 if(curr_broken_disk
!= -255) {
101 prev_broken_disk
= curr_broken_disk
;
107 if(curr_broken_disk
!= prev_broken_disk
)
113 curr_broken_disk
= prev_broken_disk
= -65535;
118 return curr_broken_disk
;
121 /* Collect disks status for a strip in CHECK_PAGE_SIZE page size blocks */
122 void raid6_stats(int *disk
, int *results
, int raid_disks
, int chunk_size
)
126 for(i
= 0, j
= 0; i
< chunk_size
; i
+= CHECK_PAGE_SIZE
, j
++) {
127 disk
[j
] = raid6_stats_blk(&results
[i
], raid_disks
);
131 int lock_stripe(struct mdinfo
*info
, unsigned long long start
,
132 int chunk_size
, int data_disks
, sighandler_t
*sig
)
136 sig
[0] = signal_s(SIGTERM
, SIG_IGN
);
137 sig
[1] = signal_s(SIGINT
, SIG_IGN
);
138 sig
[2] = signal_s(SIGQUIT
, SIG_IGN
);
140 if (sig
[0] == SIG_ERR
|| sig
[1] == SIG_ERR
|| sig
[2] == SIG_ERR
)
143 if(mlockall(MCL_CURRENT
| MCL_FUTURE
) != 0) {
147 rv
= sysfs_set_num(info
, NULL
, "suspend_lo", start
* chunk_size
* data_disks
);
148 rv
|= sysfs_set_num(info
, NULL
, "suspend_hi", (start
+ 1) * chunk_size
* data_disks
);
152 int unlock_all_stripes(struct mdinfo
*info
, sighandler_t
*sig
)
155 rv
= sysfs_set_num(info
, NULL
, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL
);
156 rv
|= sysfs_set_num(info
, NULL
, "suspend_hi", 0);
157 rv
|= sysfs_set_num(info
, NULL
, "suspend_lo", 0);
159 signal_s(SIGQUIT
, sig
[2]);
160 signal_s(SIGINT
, sig
[1]);
161 signal_s(SIGTERM
, sig
[0]);
163 if(munlockall() != 0)
169 int autorepair(int *disk
, unsigned long long start
, int chunk_size
,
170 char *name
[], int raid_disks
, int syndrome_disks
, char **blocks_page
,
171 char **blocks
, uint8_t *p
, int *block_index_for_slot
,
172 int *source
, unsigned long long *offsets
)
175 int pages_to_write_count
= 0;
176 int page_to_write
[chunk_size
>> CHECK_PAGE_BITS
];
177 for(j
= 0; j
< (chunk_size
>> CHECK_PAGE_BITS
); j
++) {
178 if (disk
[j
] >= -2 && block_index_for_slot
[disk
[j
]] >= 0) {
179 int slot
= block_index_for_slot
[disk
[j
]];
180 printf("Auto-repairing slot %d (%s)\n", slot
, name
[slot
]);
181 pages_to_write_count
++;
182 page_to_write
[j
] = 1;
183 for(i
= -2; i
< syndrome_disks
; i
++) {
184 blocks_page
[i
] = blocks
[i
] + j
* CHECK_PAGE_SIZE
;
187 qsyndrome(p
, (uint8_t*)blocks_page
[-2],
188 (uint8_t**)blocks_page
,
189 syndrome_disks
, CHECK_PAGE_SIZE
);
192 char *all_but_failed_blocks
[syndrome_disks
];
193 for(i
= 0; i
< syndrome_disks
; i
++) {
195 all_but_failed_blocks
[i
] = blocks_page
[-1];
197 all_but_failed_blocks
[i
] = blocks_page
[i
];
199 xor_blocks(blocks_page
[disk
[j
]],
200 all_but_failed_blocks
, syndrome_disks
,
205 page_to_write
[j
] = 0;
209 if(pages_to_write_count
> 0) {
211 for(j
= 0; j
< (chunk_size
>> CHECK_PAGE_BITS
); j
++) {
212 if(page_to_write
[j
] == 1) {
213 int slot
= block_index_for_slot
[disk
[j
]];
214 lseek64(source
[slot
], offsets
[slot
] + start
* chunk_size
+ j
* CHECK_PAGE_SIZE
, SEEK_SET
);
215 write_res
+= write(source
[slot
],
216 blocks
[disk
[j
]] + j
* CHECK_PAGE_SIZE
,
221 if (write_res
!= (CHECK_PAGE_SIZE
* pages_to_write_count
)) {
222 fprintf(stderr
, "Failed to write a full chunk.\n");
231 int manual_repair(int chunk_size
, int syndrome_disks
,
232 int failed_slot1
, int failed_slot2
,
233 unsigned long long start
, int *block_index_for_slot
,
234 char *name
[], char **stripes
, char **blocks
, uint8_t *p
,
235 int *source
, unsigned long long *offsets
)
238 int fd1
= block_index_for_slot
[failed_slot1
];
239 int fd2
= block_index_for_slot
[failed_slot2
];
240 printf("Repairing stripe %llu\n", start
);
241 printf("Assuming slots %d (%s) and %d (%s) are incorrect\n",
245 if (failed_slot1
== -2 || failed_slot2
== -2) {
246 char *all_but_failed_blocks
[syndrome_disks
];
247 int failed_data_or_p
;
249 if (failed_slot1
== -2)
250 failed_data_or_p
= failed_slot2
;
252 failed_data_or_p
= failed_slot1
;
254 printf("Repairing D/P(%d) and Q\n", failed_data_or_p
);
256 for (i
= 0; i
< syndrome_disks
; i
++) {
257 if (i
== failed_data_or_p
)
258 all_but_failed_blocks
[i
] = blocks
[-1];
260 all_but_failed_blocks
[i
] = blocks
[i
];
262 xor_blocks(blocks
[failed_data_or_p
],
263 all_but_failed_blocks
, syndrome_disks
, chunk_size
);
264 qsyndrome(p
, (uint8_t*)blocks
[-2], (uint8_t**)blocks
,
265 syndrome_disks
, chunk_size
);
267 ensure_zero_has_size(chunk_size
);
268 if (failed_slot1
== -1 || failed_slot2
== -1) {
270 if (failed_slot1
== -1)
271 failed_data
= failed_slot2
;
273 failed_data
= failed_slot1
;
275 printf("Repairing D(%d) and P\n", failed_data
);
276 raid6_datap_recov(syndrome_disks
+2, chunk_size
,
277 failed_data
, (uint8_t**)blocks
, 1);
279 printf("Repairing D and D\n");
280 raid6_2data_recov(syndrome_disks
+2, chunk_size
,
281 failed_slot1
, failed_slot2
,
282 (uint8_t**)blocks
, 1);
286 int write_res1
, write_res2
;
289 seek_res
= lseek64(source
[fd1
],
290 offsets
[fd1
] + start
* chunk_size
, SEEK_SET
);
292 fprintf(stderr
, "lseek failed for failed_disk1\n");
295 write_res1
= write(source
[fd1
], blocks
[failed_slot1
], chunk_size
);
297 seek_res
= lseek64(source
[fd2
],
298 offsets
[fd2
] + start
* chunk_size
, SEEK_SET
);
300 fprintf(stderr
, "lseek failed for failed_disk2\n");
303 write_res2
= write(source
[fd2
], blocks
[failed_slot2
], chunk_size
);
305 if (write_res1
!= chunk_size
|| write_res2
!= chunk_size
) {
306 fprintf(stderr
, "Failed to write a complete chunk.\n");
313 int check_stripes(struct mdinfo
*info
, int *source
, unsigned long long *offsets
,
314 int raid_disks
, int chunk_size
, int level
, int layout
,
315 unsigned long long start
, unsigned long long length
, char *name
[],
316 enum repair repair
, int failed_disk1
, int failed_disk2
)
318 /* read the data and p and q blocks, and check we got them right */
319 int data_disks
= raid_disks
- 2;
320 int syndrome_disks
= data_disks
+ is_ddf(layout
) * 2;
323 /* stripes[] is indexed by raid_disk and holds chunks from each device */
324 char **stripes
= xmalloc(raid_disks
* sizeof(char*));
326 /* blocks[] is indexed by syndrome number and points to either one of the
327 * chunks from 'stripes[]', or to a chunk of zeros. -1 and -2 are
329 char **blocks
= xmalloc((syndrome_disks
+ 2) * sizeof(char*));
331 /* blocks_page[] is a temporary index to just one page of the chunks
332 * that blocks[] points to. */
333 char **blocks_page
= xmalloc((syndrome_disks
+ 2) * sizeof(char*));
335 /* block_index_for_slot[] provides the reverse mapping from blocks to stripes.
336 * The index is a syndrome position, the content is a raid_disk number.
337 * indicies -1 and -2 work, and are P and Q disks */
338 int *block_index_for_slot
= xmalloc((syndrome_disks
+2) * sizeof(int));
340 /* 'p' and 'q' contain calcualted P and Q, to be compared with
341 * blocks[-1] and blocks[-2];
343 uint8_t *p
= xmalloc(chunk_size
);
344 uint8_t *q
= xmalloc(chunk_size
);
345 char *zero
= xmalloc(chunk_size
);
346 int *results
= xmalloc(chunk_size
* sizeof(int));
347 sighandler_t
*sig
= xmalloc(3 * sizeof(sighandler_t
));
350 int diskP
, diskQ
, diskD
;
353 extern int tables_ready
;
358 if (posix_memalign((void**)&stripe_buf
, 4096, raid_disks
* chunk_size
) != 0)
360 block_index_for_slot
+= 2;
364 memset(zero
, 0, chunk_size
);
365 for ( i
= 0 ; i
< raid_disks
; i
++)
366 stripes
[i
] = stripe_buf
+ i
* chunk_size
;
369 /* The syndrome number of the broken disk is recorded
370 * in 'disk[]' which allows a different broken disk for
373 int disk
[chunk_size
>> CHECK_PAGE_BITS
];
375 err
= lock_stripe(info
, start
, chunk_size
, data_disks
, sig
);
378 unlock_all_stripes(info
, sig
);
381 for (i
= 0 ; i
< raid_disks
; i
++) {
382 off64_t seek_res
= lseek64(source
[i
], offsets
[i
] + start
* chunk_size
,
385 fprintf(stderr
, "lseek to source %d failed\n", i
);
386 unlock_all_stripes(info
, sig
);
390 int read_res
= read(source
[i
], stripes
[i
], chunk_size
);
391 if (read_res
< chunk_size
) {
392 fprintf(stderr
, "Failed to read complete chunk disk %d, aborting\n", i
);
393 unlock_all_stripes(info
, sig
);
399 diskP
= geo_map(-1, start
, raid_disks
, level
, layout
);
400 block_index_for_slot
[-1] = diskP
;
401 blocks
[-1] = stripes
[diskP
];
403 diskQ
= geo_map(-2, start
, raid_disks
, level
, layout
);
404 block_index_for_slot
[-2] = diskQ
;
405 blocks
[-2] = stripes
[diskQ
];
407 if (!is_ddf(layout
)) {
408 /* The syndrome-order of disks starts immediately after 'Q',
411 for (i
= 0 ; i
< data_disks
; i
++) {
413 if (diskD
>= raid_disks
)
417 if (diskD
>= raid_disks
)
419 blocks
[i
] = stripes
[diskD
];
420 block_index_for_slot
[i
] = diskD
;
423 /* The syndrome-order exactly follows raid-disk
424 * numbers, with ZERO in place of P and Q
426 for (i
= 0 ; i
< raid_disks
; i
++) {
427 if (i
== diskP
|| i
== diskQ
) {
429 block_index_for_slot
[i
] = -1;
431 blocks
[i
] = stripes
[i
];
432 block_index_for_slot
[i
] = i
;
437 qsyndrome(p
, q
, (uint8_t**)blocks
, syndrome_disks
, chunk_size
);
439 raid6_collect(chunk_size
, p
, q
, stripes
[diskP
], stripes
[diskQ
], results
);
440 raid6_stats(disk
, results
, raid_disks
, chunk_size
);
442 for(j
= 0; j
< (chunk_size
>> CHECK_PAGE_BITS
); j
++) {
445 int slot
= block_index_for_slot
[role
];
447 printf("Error detected at stripe %llu, page %d: possible failed disk slot %d: %d --> %s\n",
448 start
, j
, role
, slot
, name
[slot
]);
450 printf("Error detected at stripe %llu, page %d: failed slot %d should be zeros\n",
452 } else if(disk
[j
] == -65535) {
453 printf("Error detected at stripe %llu, page %d: disk slot unknown\n", start
, j
);
457 if(repair
== AUTO_REPAIR
) {
458 err
= autorepair(disk
, start
, chunk_size
,
459 name
, raid_disks
, syndrome_disks
, blocks_page
,
460 blocks
, p
, block_index_for_slot
,
463 unlock_all_stripes(info
, sig
);
468 if(repair
== MANUAL_REPAIR
) {
469 int failed_slot1
= -1, failed_slot2
= -1;
470 for (i
= -2; i
< syndrome_disks
; i
++) {
471 if (block_index_for_slot
[i
] == failed_disk1
)
473 if (block_index_for_slot
[i
] == failed_disk2
)
476 err
= manual_repair(chunk_size
, syndrome_disks
,
477 failed_slot1
, failed_slot2
,
478 start
, block_index_for_slot
,
479 name
, stripes
, blocks
, p
,
482 unlock_all_stripes(info
, sig
);
487 err
= unlock_all_stripes(info
, sig
);
502 free(block_index_for_slot
-2);
511 unsigned long long getnum(char *str
, char **err
)
514 unsigned long long rv
= strtoull(str
, &e
, 10);
522 int main(int argc
, char *argv
[])
524 /* md_device start length */
527 char **disk_name
= NULL
;
528 unsigned long long *offsets
= NULL
;
534 enum repair repair
= NO_REPAIR
;
535 int failed_disk1
= -1;
536 int failed_disk2
= -1;
537 unsigned long long start
, length
;
540 struct mdinfo
*info
= NULL
, *comp
= NULL
;
544 char *prg
= strrchr(argv
[0], '/');
552 fprintf(stderr
, "Usage: %s md_device start_stripe length_stripes [autorepair]\n", prg
);
553 fprintf(stderr
, " or: %s md_device repair stripe failed_slot_1 failed_slot_2\n", prg
);
558 mdfd
= open(argv
[1], O_RDONLY
);
561 fprintf(stderr
, "%s: cannot open %s\n", prg
, argv
[1]);
566 info
= sysfs_read(mdfd
, NULL
,
578 fprintf(stderr
, "%s: Error reading sysfs information of %s\n", prg
, argv
[1]);
583 if(info
->array
.level
!= level
) {
584 fprintf(stderr
, "%s: %s not a RAID-6\n", prg
, argv
[1]);
589 if(info
->array
.failed_disks
> 0) {
590 fprintf(stderr
, "%s: %s degraded array\n", prg
, argv
[1]);
595 printf("layout: %d\n", info
->array
.layout
);
596 printf("disks: %d\n", info
->array
.raid_disks
);
597 printf("component size: %llu\n", info
->component_size
* 512);
598 printf("total stripes: %llu\n", (info
->component_size
* 512) / info
->array
.chunk_size
);
599 printf("chunk size: %d\n", info
->array
.chunk_size
);
603 for(i
= 0, active_disks
= 0; active_disks
< info
->array
.raid_disks
; i
++) {
604 printf("disk: %d - offset: %llu - size: %llu - name: %s - slot: %d\n",
605 i
, comp
->data_offset
* 512, comp
->component_size
* 512,
606 map_dev(comp
->disk
.major
, comp
->disk
.minor
, 0),
607 comp
->disk
.raid_disk
);
608 if(comp
->disk
.raid_disk
>= 0)
616 raid_disks
= info
->array
.raid_disks
;
617 chunk_size
= info
->array
.chunk_size
;
618 layout
= info
->array
.layout
;
619 if (strcmp(argv
[2], "repair")==0) {
621 fprintf(stderr
, "For repair mode, call %s md_device repair stripe failed_slot_1 failed_slot_2\n", prg
);
625 repair
= MANUAL_REPAIR
;
626 start
= getnum(argv
[3], &err
);
628 failed_disk1
= getnum(argv
[4], &err
);
629 failed_disk2
= getnum(argv
[5], &err
);
631 if(failed_disk1
>= info
->array
.raid_disks
) {
632 fprintf(stderr
, "%s: failed_slot_1 index is higher than number of devices in raid\n", prg
);
636 if(failed_disk2
>= info
->array
.raid_disks
) {
637 fprintf(stderr
, "%s: failed_slot_2 index is higher than number of devices in raid\n", prg
);
641 if(failed_disk1
== failed_disk2
) {
642 fprintf(stderr
, "%s: failed_slot_1 and failed_slot_2 are the same\n", prg
);
648 start
= getnum(argv
[2], &err
);
649 length
= getnum(argv
[3], &err
);
650 if (argc
>= 5 && strcmp(argv
[4], "autorepair")==0)
651 repair
= AUTO_REPAIR
;
655 fprintf(stderr
, "%s: Bad number: %s\n", prg
, err
);
660 if(start
> ((info
->component_size
* 512) / chunk_size
)) {
661 start
= (info
->component_size
* 512) / chunk_size
;
662 fprintf(stderr
, "%s: start beyond disks size\n", prg
);
666 ((length
+ start
) > ((info
->component_size
* 512) / chunk_size
))) {
667 length
= (info
->component_size
* 512) / chunk_size
- start
;
670 disk_name
= xmalloc(raid_disks
* sizeof(*disk_name
));
671 fds
= xmalloc(raid_disks
* sizeof(*fds
));
672 offsets
= xcalloc(raid_disks
, sizeof(*offsets
));
673 buf
= xmalloc(raid_disks
* chunk_size
);
675 for(i
=0; i
<raid_disks
; i
++) {
681 for (i
=0, active_disks
=0; active_disks
<raid_disks
; i
++) {
682 int disk_slot
= comp
->disk
.raid_disk
;
684 disk_name
[disk_slot
] = map_dev(comp
->disk
.major
, comp
->disk
.minor
, 0);
685 offsets
[disk_slot
] = comp
->data_offset
* 512;
686 fds
[disk_slot
] = open(disk_name
[disk_slot
], O_RDWR
| O_DIRECT
);
687 if (fds
[disk_slot
] < 0) {
688 perror(disk_name
[disk_slot
]);
689 fprintf(stderr
,"%s: cannot open %s\n", prg
, disk_name
[disk_slot
]);
698 int rv
= check_stripes(info
, fds
, offsets
,
699 raid_disks
, chunk_size
, level
, layout
,
700 start
, length
, disk_name
, repair
, failed_disk1
, failed_disk2
);
702 fprintf(stderr
, "%s: check_stripes returned %d\n", prg
, rv
);
710 for(i
= 0; i
< raid_disks
; i
++)