]>
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 int geo_map(int block
, unsigned long long stripe
, int raid_disks
,
31 int level
, int layout
);
32 void qsyndrome(uint8_t *p
, uint8_t *q
, uint8_t **sources
, int disks
, int size
);
33 void make_tables(void);
35 /* Collect per stripe consistency information */
36 void raid6_collect(int chunk_size
, uint8_t *p
, uint8_t *q
,
37 char *chunkP
, char *chunkQ
, int *results
)
42 extern uint8_t raid6_gflog
[];
44 for(i
= 0; i
< chunk_size
; i
++) {
45 Px
= (uint8_t)chunkP
[i
] ^ (uint8_t)p
[i
];
46 Qx
= (uint8_t)chunkQ
[i
] ^ (uint8_t)q
[i
];
48 if((Px
!= 0) && (Qx
== 0))
51 if((Px
== 0) && (Qx
!= 0))
54 if((Px
!= 0) && (Qx
!= 0)) {
55 data_id
= (raid6_gflog
[Qx
] - raid6_gflog
[Px
]);
56 if(data_id
< 0) data_id
+= 255;
60 if((Px
== 0) && (Qx
== 0))
65 /* Try to find out if a specific disk has problems */
66 int raid6_stats(int *results
, int raid_disks
, int chunk_size
)
69 int curr_broken_disk
= -255;
70 int prev_broken_disk
= -255;
71 int broken_status
= 0;
73 for(i
= 0; i
< chunk_size
; i
++) {
75 if(results
[i
] != -255)
76 curr_broken_disk
= results
[i
];
78 if(curr_broken_disk
>= raid_disks
)
81 switch(broken_status
) {
83 if(curr_broken_disk
!= -255) {
84 prev_broken_disk
= curr_broken_disk
;
90 if(curr_broken_disk
!= prev_broken_disk
)
96 curr_broken_disk
= prev_broken_disk
= -65535;
101 return curr_broken_disk
;
104 int check_stripes(struct mdinfo
*info
, int *source
, unsigned long long *offsets
,
105 int raid_disks
, int chunk_size
, int level
, int layout
,
106 unsigned long long start
, unsigned long long length
, char *name
[])
108 /* read the data and p and q blocks, and check we got them right */
109 char *stripe_buf
= malloc(raid_disks
* chunk_size
);
110 char **stripes
= malloc(raid_disks
* sizeof(char*));
111 char **blocks
= malloc(raid_disks
* sizeof(char*));
112 uint8_t *p
= malloc(chunk_size
);
113 uint8_t *q
= malloc(chunk_size
);
114 int *results
= malloc(chunk_size
* sizeof(int));
118 int data_disks
= raid_disks
- 2;
123 extern int tables_ready
;
125 if((stripe_buf
== NULL
) ||
138 for ( i
= 0 ; i
< raid_disks
; i
++)
139 stripes
[i
] = stripe_buf
+ i
* chunk_size
;
144 printf("pos --> %llu\n", start
);
146 if(mlockall(MCL_CURRENT
| MCL_FUTURE
) != 0) {
150 sig
[0] = signal(SIGTERM
, SIG_IGN
);
151 sig
[1] = signal(SIGINT
, SIG_IGN
);
152 sig
[2] = signal(SIGQUIT
, SIG_IGN
);
153 rv
= sysfs_set_num(info
, NULL
, "suspend_lo", start
* chunk_size
* data_disks
);
154 rv
|= sysfs_set_num(info
, NULL
, "suspend_hi", (start
+ 1) * chunk_size
* data_disks
);
155 for (i
= 0 ; i
< raid_disks
; i
++) {
156 lseek64(source
[i
], offsets
[i
] + start
* chunk_size
, 0);
157 read(source
[i
], stripes
[i
], chunk_size
);
159 rv
|= sysfs_set_num(info
, NULL
, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL
);
160 rv
|= sysfs_set_num(info
, NULL
, "suspend_hi", 0);
161 rv
|= sysfs_set_num(info
, NULL
, "suspend_lo", 0);
162 signal(SIGQUIT
, sig
[2]);
163 signal(SIGINT
, sig
[1]);
164 signal(SIGTERM
, sig
[0]);
165 if(munlockall() != 0) {
175 for (i
= 0 ; i
< data_disks
; i
++) {
176 int disk
= geo_map(i
, start
, raid_disks
, level
, layout
);
177 blocks
[i
] = stripes
[disk
];
178 printf("%d->%d\n", i
, disk
);
181 qsyndrome(p
, q
, (uint8_t**)blocks
, data_disks
, chunk_size
);
182 diskP
= geo_map(-1, start
, raid_disks
, level
, layout
);
183 if (memcmp(p
, stripes
[diskP
], chunk_size
) != 0) {
184 printf("P(%d) wrong at %llu\n", diskP
, start
);
186 diskQ
= geo_map(-2, start
, raid_disks
, level
, layout
);
187 if (memcmp(q
, stripes
[diskQ
], chunk_size
) != 0) {
188 printf("Q(%d) wrong at %llu\n", diskQ
, start
);
190 raid6_collect(chunk_size
, p
, q
, stripes
[diskP
], stripes
[diskQ
], results
);
191 disk
= raid6_stats(results
, raid_disks
, chunk_size
);
194 disk
= geo_map(disk
, start
, raid_disks
, level
, layout
);
197 printf("Error detected at %llu: possible failed disk slot: %d --> %s\n",
198 start
, disk
, name
[disk
]);
201 printf("Error detected at %llu: disk slot unknown\n", start
);
220 unsigned long long getnum(char *str
, char **err
)
223 unsigned long long rv
= strtoull(str
, &e
, 10);
231 int main(int argc
, char *argv
[])
233 /* md_device start length */
236 char **disk_name
= NULL
;
237 unsigned long long *offsets
= NULL
;
243 unsigned long long start
, length
;
246 struct mdinfo
*info
= NULL
, *comp
= NULL
;
250 char *prg
= strrchr(argv
[0], '/');
258 fprintf(stderr
, "Usage: %s md_device start_stripe length_stripes\n", prg
);
263 mdfd
= open(argv
[1], O_RDONLY
);
266 fprintf(stderr
,"%s: cannot open %s\n", prg
, argv
[1]);
271 info
= sysfs_read(mdfd
, -1,
283 fprintf(stderr
, "%s: Error reading sysfs information of %s\n", prg
, argv
[1]);
288 if(info
->array
.level
!= level
) {
289 fprintf(stderr
, "%s: %s not a RAID-6\n", prg
, argv
[1]);
294 if(info
->array
.failed_disks
> 0) {
295 fprintf(stderr
, "%s: %s degraded array\n", prg
, argv
[1]);
300 printf("layout: %d\n", info
->array
.layout
);
301 printf("disks: %d\n", info
->array
.raid_disks
);
302 printf("component size: %llu\n", info
->component_size
* 512);
303 printf("total stripes: %llu\n", (info
->component_size
* 512) / info
->array
.chunk_size
);
304 printf("chunk size: %d\n", info
->array
.chunk_size
);
308 for(i
= 0, active_disks
= 0; active_disks
< info
->array
.raid_disks
; i
++) {
309 printf("disk: %d - offset: %llu - size: %llu - name: %s - slot: %d\n",
310 i
, comp
->data_offset
* 512, comp
->component_size
* 512,
311 map_dev(comp
->disk
.major
, comp
->disk
.minor
, 0),
312 comp
->disk
.raid_disk
);
313 if(comp
->disk
.raid_disk
>= 0)
321 raid_disks
= info
->array
.raid_disks
;
322 chunk_size
= info
->array
.chunk_size
;
323 layout
= info
->array
.layout
;
324 start
= getnum(argv
[2], &err
);
325 length
= getnum(argv
[3], &err
);
328 fprintf(stderr
, "%s: Bad number: %s\n", prg
, err
);
333 if(start
> ((info
->component_size
* 512) / chunk_size
)) {
334 start
= (info
->component_size
* 512) / chunk_size
;
335 fprintf(stderr
, "%s: start beyond disks size\n", prg
);
339 ((length
+ start
) > ((info
->component_size
* 512) / chunk_size
))) {
340 length
= (info
->component_size
* 512) / chunk_size
- start
;
343 disk_name
= malloc(raid_disks
* sizeof(*disk_name
));
344 fds
= malloc(raid_disks
* sizeof(*fds
));
345 offsets
= malloc(raid_disks
* sizeof(*offsets
));
346 buf
= malloc(raid_disks
* chunk_size
);
348 if((disk_name
== NULL
) ||
352 fprintf(stderr
, "%s: allocation fail\n", prg
);
357 memset(offsets
, 0, raid_disks
* sizeof(*offsets
));
358 for(i
=0; i
<raid_disks
; i
++) {
364 for (i
=0, active_disks
=0; active_disks
<raid_disks
; i
++) {
365 int disk_slot
= comp
->disk
.raid_disk
;
367 disk_name
[disk_slot
] = map_dev(comp
->disk
.major
, comp
->disk
.minor
, 0);
368 offsets
[disk_slot
] = comp
->data_offset
* 512;
369 fds
[disk_slot
] = open(disk_name
[disk_slot
], O_RDWR
);
370 if (fds
[disk_slot
] < 0) {
371 perror(disk_name
[disk_slot
]);
372 fprintf(stderr
,"%s: cannot open %s\n", prg
, disk_name
[disk_slot
]);
381 int rv
= check_stripes(info
, fds
, offsets
,
382 raid_disks
, chunk_size
, level
, layout
,
383 start
, length
, disk_name
);
386 "%s: check_stripes returned %d\n", prg
, rv
);
394 for(i
= 0; i
< raid_disks
; i
++)