]>
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
30 #define CHECK_PAGE_BITS (12)
31 #define CHECK_PAGE_SIZE (1 << CHECK_PAGE_BITS)
33 char const Name
[] = "raid6check";
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
,
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
);
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
)
60 extern uint8_t raid6_gflog
[];
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
];
66 if((Px
!= 0) && (Qx
== 0))
69 if((Px
== 0) && (Qx
!= 0))
72 if((Px
!= 0) && (Qx
!= 0)) {
73 data_id
= (raid6_gflog
[Qx
] - raid6_gflog
[Px
]);
74 if(data_id
< 0) data_id
+= 255;
78 if((Px
== 0) && (Qx
== 0))
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
)
87 int curr_broken_disk
= -255;
88 int prev_broken_disk
= -255;
89 int broken_status
= 0;
91 for(i
= 0; i
< CHECK_PAGE_SIZE
; i
++) {
93 if(results
[i
] != -255)
94 curr_broken_disk
= results
[i
];
96 if(curr_broken_disk
>= raid_disks
)
99 switch(broken_status
) {
101 if(curr_broken_disk
!= -255) {
102 prev_broken_disk
= curr_broken_disk
;
108 if(curr_broken_disk
!= prev_broken_disk
)
114 curr_broken_disk
= prev_broken_disk
= -65535;
119 return curr_broken_disk
;
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
)
127 for(i
= 0, j
= 0; i
< chunk_size
; i
+= CHECK_PAGE_SIZE
, j
++) {
128 disk
[j
] = raid6_stats_blk(&results
[i
], raid_disks
);
132 int lock_stripe(struct mdinfo
*info
, unsigned long long start
,
133 int chunk_size
, int data_disks
, sighandler_t
*sig
) {
135 if(mlockall(MCL_CURRENT
| MCL_FUTURE
) != 0) {
139 sig
[0] = signal(SIGTERM
, SIG_IGN
);
140 sig
[1] = signal(SIGINT
, SIG_IGN
);
141 sig
[2] = signal(SIGQUIT
, SIG_IGN
);
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
);
148 int unlock_all_stripes(struct mdinfo
*info
, sighandler_t
*sig
) {
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);
154 signal(SIGQUIT
, sig
[2]);
155 signal(SIGINT
, sig
[1]);
156 signal(SIGTERM
, sig
[0]);
158 if(munlockall() != 0)
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
)
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
;
182 qsyndrome(p
, (uint8_t*)blocks_page
[-2],
183 (uint8_t**)blocks_page
,
184 syndrome_disks
, CHECK_PAGE_SIZE
);
187 char *all_but_failed_blocks
[syndrome_disks
];
188 for(i
= 0; i
< syndrome_disks
; i
++) {
190 all_but_failed_blocks
[i
] = blocks_page
[-1];
192 all_but_failed_blocks
[i
] = blocks_page
[i
];
194 xor_blocks(blocks_page
[disk
[j
]],
195 all_but_failed_blocks
, syndrome_disks
,
200 page_to_write
[j
] = 0;
204 if(pages_to_write_count
> 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
,
216 if (write_res
!= (CHECK_PAGE_SIZE
* pages_to_write_count
)) {
217 fprintf(stderr
, "Failed to write a full chunk.\n");
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
)
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",
240 if (failed_slot1
== -2 || failed_slot2
== -2) {
241 char *all_but_failed_blocks
[syndrome_disks
];
242 int failed_data_or_p
;
244 if (failed_slot1
== -2)
245 failed_data_or_p
= failed_slot2
;
247 failed_data_or_p
= failed_slot1
;
249 printf("Repairing D/P(%d) and Q\n", failed_data_or_p
);
251 for (i
= 0; i
< syndrome_disks
; i
++) {
252 if (i
== failed_data_or_p
)
253 all_but_failed_blocks
[i
] = blocks
[-1];
255 all_but_failed_blocks
[i
] = blocks
[i
];
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
);
262 ensure_zero_has_size(chunk_size
);
263 if (failed_slot1
== -1 || failed_slot2
== -1) {
265 if (failed_slot1
== -1)
266 failed_data
= failed_slot2
;
268 failed_data
= failed_slot1
;
270 printf("Repairing D(%d) and P\n", failed_data
);
271 raid6_datap_recov(syndrome_disks
+2, chunk_size
,
272 failed_data
, (uint8_t**)blocks
, 1);
274 printf("Repairing D and D\n");
275 raid6_2data_recov(syndrome_disks
+2, chunk_size
,
276 failed_slot1
, failed_slot2
,
277 (uint8_t**)blocks
, 1);
281 int write_res1
, write_res2
;
284 seek_res
= lseek64(source
[fd1
],
285 offsets
[fd1
] + start
* chunk_size
, SEEK_SET
);
287 fprintf(stderr
, "lseek failed for failed_disk1\n");
290 write_res1
= write(source
[fd1
], blocks
[failed_slot1
], chunk_size
);
292 seek_res
= lseek64(source
[fd2
],
293 offsets
[fd2
] + start
* chunk_size
, SEEK_SET
);
295 fprintf(stderr
, "lseek failed for failed_disk2\n");
298 write_res2
= write(source
[fd2
], blocks
[failed_slot2
], chunk_size
);
300 if (write_res1
!= chunk_size
|| write_res2
!= chunk_size
) {
301 fprintf(stderr
, "Failed to write a complete chunk.\n");
308 int check_stripes(struct mdinfo
*info
, int *source
, unsigned long long *offsets
,
309 int raid_disks
, int chunk_size
, int level
, int layout
,
310 unsigned long long start
, unsigned long long length
, char *name
[],
311 enum repair repair
, int failed_disk1
, int failed_disk2
)
313 /* read the data and p and q blocks, and check we got them right */
314 int data_disks
= raid_disks
- 2;
315 int syndrome_disks
= data_disks
+ is_ddf(layout
) * 2;
318 /* stripes[] is indexed by raid_disk and holds chunks from each device */
319 char **stripes
= xmalloc(raid_disks
* sizeof(char*));
321 /* blocks[] is indexed by syndrome number and points to either one of the
322 * chunks from 'stripes[]', or to a chunk of zeros. -1 and -2 are
324 char **blocks
= xmalloc((syndrome_disks
+ 2) * sizeof(char*));
326 /* blocks_page[] is a temporary index to just one page of the chunks
327 * that blocks[] points to. */
328 char **blocks_page
= xmalloc((syndrome_disks
+ 2) * sizeof(char*));
330 /* block_index_for_slot[] provides the reverse mapping from blocks to stripes.
331 * The index is a syndrome position, the content is a raid_disk number.
332 * indicies -1 and -2 work, and are P and Q disks */
333 int *block_index_for_slot
= xmalloc((syndrome_disks
+2) * sizeof(int));
335 /* 'p' and 'q' contain calcualted P and Q, to be compared with
336 * blocks[-1] and blocks[-2];
338 uint8_t *p
= xmalloc(chunk_size
);
339 uint8_t *q
= xmalloc(chunk_size
);
340 char *zero
= xmalloc(chunk_size
);
341 int *results
= xmalloc(chunk_size
* sizeof(int));
342 sighandler_t
*sig
= xmalloc(3 * sizeof(sighandler_t
));
345 int diskP
, diskQ
, diskD
;
348 extern int tables_ready
;
353 if (posix_memalign((void**)&stripe_buf
, 4096, raid_disks
* chunk_size
) != 0)
355 block_index_for_slot
+= 2;
359 memset(zero
, 0, chunk_size
);
360 for ( i
= 0 ; i
< raid_disks
; i
++)
361 stripes
[i
] = stripe_buf
+ i
* chunk_size
;
364 /* The syndrome number of the broken disk is recorded
365 * in 'disk[]' which allows a different broken disk for
368 int disk
[chunk_size
>> CHECK_PAGE_BITS
];
370 err
= lock_stripe(info
, start
, chunk_size
, data_disks
, sig
);
373 unlock_all_stripes(info
, sig
);
376 for (i
= 0 ; i
< raid_disks
; i
++) {
377 off64_t seek_res
= lseek64(source
[i
], offsets
[i
] + start
* chunk_size
,
380 fprintf(stderr
, "lseek to source %d failed\n", i
);
381 unlock_all_stripes(info
, sig
);
385 int read_res
= read(source
[i
], stripes
[i
], chunk_size
);
386 if (read_res
< chunk_size
) {
387 fprintf(stderr
, "Failed to read complete chunk disk %d, aborting\n", i
);
388 unlock_all_stripes(info
, sig
);
394 diskP
= geo_map(-1, start
, raid_disks
, level
, layout
);
395 block_index_for_slot
[-1] = diskP
;
396 blocks
[-1] = stripes
[diskP
];
398 diskQ
= geo_map(-2, start
, raid_disks
, level
, layout
);
399 block_index_for_slot
[-2] = diskQ
;
400 blocks
[-2] = stripes
[diskQ
];
402 if (!is_ddf(layout
)) {
403 /* The syndrome-order of disks starts immediately after 'Q',
406 for (i
= 0 ; i
< data_disks
; i
++) {
408 if (diskD
>= raid_disks
)
412 if (diskD
>= raid_disks
)
414 blocks
[i
] = stripes
[diskD
];
415 block_index_for_slot
[i
] = diskD
;
418 /* The syndrome-order exactly follows raid-disk
419 * numbers, with ZERO in place of P and Q
421 for (i
= 0 ; i
< raid_disks
; i
++) {
422 if (i
== diskP
|| i
== diskQ
) {
424 block_index_for_slot
[i
] = -1;
426 blocks
[i
] = stripes
[i
];
427 block_index_for_slot
[i
] = i
;
432 qsyndrome(p
, q
, (uint8_t**)blocks
, syndrome_disks
, chunk_size
);
434 raid6_collect(chunk_size
, p
, q
, stripes
[diskP
], stripes
[diskQ
], results
);
435 raid6_stats(disk
, results
, raid_disks
, chunk_size
);
437 for(j
= 0; j
< (chunk_size
>> CHECK_PAGE_BITS
); j
++) {
440 int slot
= block_index_for_slot
[role
];
442 printf("Error detected at stripe %llu, page %d: possible failed disk slot %d: %d --> %s\n",
443 start
, j
, role
, slot
, name
[slot
]);
445 printf("Error detected at stripe %llu, page %d: failed slot %d should be zeros\n",
447 } else if(disk
[j
] == -65535) {
448 printf("Error detected at stripe %llu, page %d: disk slot unknown\n", start
, j
);
452 if(repair
== AUTO_REPAIR
) {
453 err
= autorepair(disk
, start
, chunk_size
,
454 name
, raid_disks
, syndrome_disks
, blocks_page
,
455 blocks
, p
, block_index_for_slot
,
458 unlock_all_stripes(info
, sig
);
463 if(repair
== MANUAL_REPAIR
) {
464 int failed_slot1
= -1, failed_slot2
= -1;
465 for (i
= -2; i
< syndrome_disks
; i
++) {
466 if (block_index_for_slot
[i
] == failed_disk1
)
468 if (block_index_for_slot
[i
] == failed_disk2
)
471 err
= manual_repair(chunk_size
, syndrome_disks
,
472 failed_slot1
, failed_slot2
,
473 start
, block_index_for_slot
,
474 name
, stripes
, blocks
, p
,
477 unlock_all_stripes(info
, sig
);
482 err
= unlock_all_stripes(info
, sig
);
497 free(block_index_for_slot
-2);
506 unsigned long long getnum(char *str
, char **err
)
509 unsigned long long rv
= strtoull(str
, &e
, 10);
517 int main(int argc
, char *argv
[])
519 /* md_device start length */
522 char **disk_name
= NULL
;
523 unsigned long long *offsets
= NULL
;
529 enum repair repair
= NO_REPAIR
;
530 int failed_disk1
= -1;
531 int failed_disk2
= -1;
532 unsigned long long start
, length
;
535 struct mdinfo
*info
= NULL
, *comp
= NULL
;
539 char *prg
= strrchr(argv
[0], '/');
547 fprintf(stderr
, "Usage: %s md_device start_stripe length_stripes [autorepair]\n", prg
);
548 fprintf(stderr
, " or: %s md_device repair stripe failed_slot_1 failed_slot_2\n", prg
);
553 mdfd
= open(argv
[1], O_RDONLY
);
556 fprintf(stderr
, "%s: cannot open %s\n", prg
, argv
[1]);
561 info
= sysfs_read(mdfd
, NULL
,
573 fprintf(stderr
, "%s: Error reading sysfs information of %s\n", prg
, argv
[1]);
578 if(info
->array
.level
!= level
) {
579 fprintf(stderr
, "%s: %s not a RAID-6\n", prg
, argv
[1]);
584 if(info
->array
.failed_disks
> 0) {
585 fprintf(stderr
, "%s: %s degraded array\n", prg
, argv
[1]);
590 printf("layout: %d\n", info
->array
.layout
);
591 printf("disks: %d\n", info
->array
.raid_disks
);
592 printf("component size: %llu\n", info
->component_size
* 512);
593 printf("total stripes: %llu\n", (info
->component_size
* 512) / info
->array
.chunk_size
);
594 printf("chunk size: %d\n", info
->array
.chunk_size
);
598 for(i
= 0, active_disks
= 0; active_disks
< info
->array
.raid_disks
; i
++) {
599 printf("disk: %d - offset: %llu - size: %llu - name: %s - slot: %d\n",
600 i
, comp
->data_offset
* 512, comp
->component_size
* 512,
601 map_dev(comp
->disk
.major
, comp
->disk
.minor
, 0),
602 comp
->disk
.raid_disk
);
603 if(comp
->disk
.raid_disk
>= 0)
611 raid_disks
= info
->array
.raid_disks
;
612 chunk_size
= info
->array
.chunk_size
;
613 layout
= info
->array
.layout
;
614 if (strcmp(argv
[2], "repair")==0) {
616 fprintf(stderr
, "For repair mode, call %s md_device repair stripe failed_slot_1 failed_slot_2\n", prg
);
620 repair
= MANUAL_REPAIR
;
621 start
= getnum(argv
[3], &err
);
623 failed_disk1
= getnum(argv
[4], &err
);
624 failed_disk2
= getnum(argv
[5], &err
);
626 if(failed_disk1
>= info
->array
.raid_disks
) {
627 fprintf(stderr
, "%s: failed_slot_1 index is higher than number of devices in raid\n", prg
);
631 if(failed_disk2
>= info
->array
.raid_disks
) {
632 fprintf(stderr
, "%s: failed_slot_2 index is higher than number of devices in raid\n", prg
);
636 if(failed_disk1
== failed_disk2
) {
637 fprintf(stderr
, "%s: failed_slot_1 and failed_slot_2 are the same\n", prg
);
643 start
= getnum(argv
[2], &err
);
644 length
= getnum(argv
[3], &err
);
645 if (argc
>= 5 && strcmp(argv
[4], "autorepair")==0)
646 repair
= AUTO_REPAIR
;
650 fprintf(stderr
, "%s: Bad number: %s\n", prg
, err
);
655 if(start
> ((info
->component_size
* 512) / chunk_size
)) {
656 start
= (info
->component_size
* 512) / chunk_size
;
657 fprintf(stderr
, "%s: start beyond disks size\n", prg
);
661 ((length
+ start
) > ((info
->component_size
* 512) / chunk_size
))) {
662 length
= (info
->component_size
* 512) / chunk_size
- start
;
665 disk_name
= xmalloc(raid_disks
* sizeof(*disk_name
));
666 fds
= xmalloc(raid_disks
* sizeof(*fds
));
667 offsets
= xcalloc(raid_disks
, sizeof(*offsets
));
668 buf
= xmalloc(raid_disks
* chunk_size
);
670 for(i
=0; i
<raid_disks
; i
++) {
676 for (i
=0, active_disks
=0; active_disks
<raid_disks
; i
++) {
677 int disk_slot
= comp
->disk
.raid_disk
;
679 disk_name
[disk_slot
] = map_dev(comp
->disk
.major
, comp
->disk
.minor
, 0);
680 offsets
[disk_slot
] = comp
->data_offset
* 512;
681 fds
[disk_slot
] = open(disk_name
[disk_slot
], O_RDWR
| O_DIRECT
);
682 if (fds
[disk_slot
] < 0) {
683 perror(disk_name
[disk_slot
]);
684 fprintf(stderr
,"%s: cannot open %s\n", prg
, disk_name
[disk_slot
]);
693 int rv
= check_stripes(info
, fds
, offsets
,
694 raid_disks
, chunk_size
, level
, layout
,
695 start
, length
, disk_name
, repair
, failed_disk1
, failed_disk2
);
697 fprintf(stderr
, "%s: check_stripes returned %d\n", prg
, rv
);
705 for(i
= 0; i
< raid_disks
; i
++)