]>
Commit | Line | Data |
---|---|---|
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> | |
27 | ||
28 | int geo_map(int block, unsigned long long stripe, int raid_disks, | |
29 | int level, int layout); | |
30 | void qsyndrome(uint8_t *p, uint8_t *q, uint8_t **sources, int disks, int size); | |
31 | void make_tables(void); | |
32 | ||
33 | /* Collect per stripe consistency information */ | |
34 | void raid6_collect(int chunk_size, uint8_t *p, uint8_t *q, | |
35 | char *chunkP, char *chunkQ, int *results) | |
36 | { | |
37 | int i; | |
38 | int data_id; | |
39 | uint8_t Px, Qx; | |
40 | extern uint8_t raid6_gflog[]; | |
41 | ||
42 | for(i = 0; i < chunk_size; i++) { | |
43 | Px = (uint8_t)chunkP[i] ^ (uint8_t)p[i]; | |
44 | Qx = (uint8_t)chunkQ[i] ^ (uint8_t)q[i]; | |
45 | ||
46 | if((Px != 0) && (Qx == 0)) | |
47 | results[i] = -1; | |
48 | ||
49 | if((Px == 0) && (Qx != 0)) | |
50 | results[i] = -2; | |
51 | ||
52 | if((Px != 0) && (Qx != 0)) { | |
53 | data_id = (raid6_gflog[Qx] - raid6_gflog[Px]); | |
54 | if(data_id < 0) data_id += 255; | |
55 | results[i] = data_id; | |
56 | } | |
57 | ||
58 | if((Px == 0) && (Qx == 0)) | |
59 | results[i] = -255; | |
60 | } | |
61 | } | |
62 | ||
63 | /* Try to find out if a specific disk has problems */ | |
64 | int raid6_stats(int *results, int raid_disks, int chunk_size) | |
65 | { | |
66 | int i; | |
67 | int curr_broken_disk = -255; | |
68 | int prev_broken_disk = -255; | |
69 | int broken_status = 0; | |
70 | ||
71 | for(i = 0; i < chunk_size; i++) { | |
72 | ||
73 | if(results[i] != -255) | |
74 | curr_broken_disk = results[i]; | |
75 | ||
76 | if(curr_broken_disk >= raid_disks) | |
77 | broken_status = 2; | |
78 | ||
79 | switch(broken_status) { | |
80 | case 0: | |
81 | if(curr_broken_disk != -255) { | |
82 | prev_broken_disk = curr_broken_disk; | |
83 | broken_status = 1; | |
84 | } | |
85 | break; | |
86 | ||
87 | case 1: | |
88 | if(curr_broken_disk != prev_broken_disk) | |
89 | broken_status = 2; | |
90 | break; | |
91 | ||
92 | case 2: | |
93 | default: | |
94 | curr_broken_disk = prev_broken_disk = -65535; | |
95 | break; | |
96 | } | |
97 | } | |
98 | ||
99 | return curr_broken_disk; | |
100 | } | |
101 | ||
102 | int check_stripes(int *source, unsigned long long *offsets, | |
103 | int raid_disks, int chunk_size, int level, int layout, | |
104 | unsigned long long start, unsigned long long length, char *name[]) | |
105 | { | |
106 | /* read the data and p and q blocks, and check we got them right */ | |
107 | char *stripe_buf = malloc(raid_disks * chunk_size); | |
108 | char **stripes = malloc(raid_disks * sizeof(char*)); | |
109 | char **blocks = malloc(raid_disks * sizeof(char*)); | |
110 | uint8_t *p = malloc(chunk_size); | |
111 | uint8_t *q = malloc(chunk_size); | |
112 | int *results = malloc(chunk_size * sizeof(int)); | |
113 | ||
114 | int i; | |
115 | int diskP, diskQ; | |
116 | int data_disks = raid_disks - 2; | |
af3c3750 | 117 | int err = 0; |
979afcb8 PS |
118 | |
119 | extern int tables_ready; | |
120 | ||
af3c3750 PS |
121 | if((stripe_buf == NULL) || |
122 | (stripes == NULL) || | |
123 | (blocks == NULL) || | |
124 | (p == NULL) || | |
125 | (q == NULL) || | |
126 | (results == NULL)) { | |
127 | err = 1; | |
128 | goto exitCheck; | |
129 | } | |
130 | ||
979afcb8 PS |
131 | if (!tables_ready) |
132 | make_tables(); | |
133 | ||
134 | for ( i = 0 ; i < raid_disks ; i++) | |
135 | stripes[i] = stripe_buf + i * chunk_size; | |
136 | ||
137 | while (length > 0) { | |
138 | int disk; | |
139 | ||
af3c3750 PS |
140 | printf("pos --> %llu\n", start); |
141 | ||
979afcb8 | 142 | for (i = 0 ; i < raid_disks ; i++) { |
af3c3750 | 143 | lseek64(source[i], offsets[i] + start * chunk_size, 0); |
979afcb8 PS |
144 | read(source[i], stripes[i], chunk_size); |
145 | } | |
146 | for (i = 0 ; i < data_disks ; i++) { | |
af3c3750 | 147 | int disk = geo_map(i, start, raid_disks, level, layout); |
979afcb8 PS |
148 | blocks[i] = stripes[disk]; |
149 | printf("%d->%d\n", i, disk); | |
150 | } | |
151 | ||
152 | qsyndrome(p, q, (uint8_t**)blocks, data_disks, chunk_size); | |
af3c3750 | 153 | diskP = geo_map(-1, start, raid_disks, level, layout); |
979afcb8 | 154 | if (memcmp(p, stripes[diskP], chunk_size) != 0) { |
af3c3750 | 155 | printf("P(%d) wrong at %llu\n", diskP, start); |
979afcb8 | 156 | } |
af3c3750 | 157 | diskQ = geo_map(-2, start, raid_disks, level, layout); |
979afcb8 | 158 | if (memcmp(q, stripes[diskQ], chunk_size) != 0) { |
af3c3750 | 159 | printf("Q(%d) wrong at %llu\n", diskQ, start); |
979afcb8 | 160 | } |
af3c3750 | 161 | raid6_collect(chunk_size, p, q, stripes[diskP], stripes[diskQ], results); |
979afcb8 PS |
162 | disk = raid6_stats(results, raid_disks, chunk_size); |
163 | ||
164 | if(disk >= -2) { | |
af3c3750 | 165 | disk = geo_map(disk, start, raid_disks, level, layout); |
979afcb8 PS |
166 | } |
167 | if(disk >= 0) { | |
af3c3750 PS |
168 | printf("Error detected at %llu: possible failed disk slot: %d --> %s\n", |
169 | start, disk, name[disk]); | |
979afcb8 PS |
170 | } |
171 | if(disk == -65535) { | |
af3c3750 | 172 | printf("Error detected at %llu: disk slot unknown\n", start); |
979afcb8 PS |
173 | } |
174 | ||
af3c3750 PS |
175 | length--; |
176 | start++; | |
979afcb8 PS |
177 | } |
178 | ||
af3c3750 PS |
179 | exitCheck: |
180 | ||
979afcb8 PS |
181 | free(stripe_buf); |
182 | free(stripes); | |
183 | free(blocks); | |
184 | free(p); | |
185 | free(q); | |
186 | free(results); | |
187 | ||
af3c3750 | 188 | return err; |
979afcb8 PS |
189 | } |
190 | ||
191 | unsigned long long getnum(char *str, char **err) | |
192 | { | |
193 | char *e; | |
194 | unsigned long long rv = strtoull(str, &e, 10); | |
195 | if (e==str || *e) { | |
196 | *err = str; | |
197 | return 0; | |
198 | } | |
199 | return rv; | |
200 | } | |
201 | ||
202 | int main(int argc, char *argv[]) | |
203 | { | |
a9c2c6c6 | 204 | /* md_device start length */ |
af3c3750 PS |
205 | int *fds = NULL; |
206 | char *buf = NULL; | |
207 | char **disk_name = NULL; | |
208 | unsigned long long *offsets = NULL; | |
209 | int raid_disks = 0; | |
2cf31121 | 210 | int active_disks; |
af3c3750 PS |
211 | int chunk_size = 0; |
212 | int layout = -1; | |
979afcb8 PS |
213 | int level = 6; |
214 | unsigned long long start, length; | |
215 | int i; | |
a9c2c6c6 PS |
216 | int mdfd; |
217 | struct mdinfo *info, *comp; | |
979afcb8 | 218 | char *err = NULL; |
af3c3750 PS |
219 | int exit_err = 0; |
220 | int close_flag = 0; | |
221 | char *prg = strrchr(argv[0], '/'); | |
222 | ||
223 | if (prg == NULL) | |
224 | prg = argv[0]; | |
225 | else | |
226 | prg++; | |
227 | ||
228 | if (argc < 4) { | |
229 | fprintf(stderr, "Usage: %s md_device start_stripe length_stripes\n", prg); | |
230 | exit_err = 1; | |
231 | goto exitHere; | |
979afcb8 PS |
232 | } |
233 | ||
a9c2c6c6 PS |
234 | mdfd = open(argv[1], O_RDONLY); |
235 | if(mdfd < 0) { | |
236 | perror(argv[1]); | |
237 | fprintf(stderr,"%s: cannot open %s\n", prg, argv[1]); | |
af3c3750 PS |
238 | exit_err = 2; |
239 | goto exitHere; | |
a9c2c6c6 PS |
240 | } |
241 | ||
242 | info = sysfs_read(mdfd, -1, | |
243 | GET_LEVEL| | |
244 | GET_LAYOUT| | |
245 | GET_DISKS| | |
2cf31121 | 246 | GET_DEGRADED | |
a9c2c6c6 PS |
247 | GET_COMPONENT| |
248 | GET_CHUNK| | |
249 | GET_DEVS| | |
250 | GET_OFFSET| | |
251 | GET_SIZE); | |
252 | ||
253 | if(info->array.level != level) { | |
254 | fprintf(stderr, "%s: %s not a RAID-6\n", prg, argv[1]); | |
af3c3750 PS |
255 | exit_err = 3; |
256 | goto exitHere; | |
a9c2c6c6 PS |
257 | } |
258 | ||
2cf31121 PS |
259 | if(info->array.failed_disks > 0) { |
260 | fprintf(stderr, "%s: %s degraded array\n", prg, argv[1]); | |
261 | exit_err = 8; | |
262 | goto exitHere; | |
263 | } | |
264 | ||
a9c2c6c6 PS |
265 | printf("layout: %d\n", info->array.layout); |
266 | printf("disks: %d\n", info->array.raid_disks); | |
af3c3750 PS |
267 | printf("component size: %llu\n", info->component_size * 512); |
268 | printf("total stripes: %llu\n", (info->component_size * 512) / info->array.chunk_size); | |
a9c2c6c6 PS |
269 | printf("chunk size: %d\n", info->array.chunk_size); |
270 | printf("\n"); | |
271 | ||
272 | comp = info->devs; | |
2cf31121 | 273 | for(i = 0, active_disks = 0; active_disks < info->array.raid_disks; i++) { |
a9c2c6c6 | 274 | printf("disk: %d - offset: %llu - size: %llu - name: %s - slot: %d\n", |
af3c3750 | 275 | i, comp->data_offset * 512, comp->component_size * 512, |
a9c2c6c6 PS |
276 | map_dev(comp->disk.major, comp->disk.minor, 0), |
277 | comp->disk.raid_disk); | |
2cf31121 PS |
278 | if(comp->disk.raid_disk >= 0) |
279 | active_disks++; | |
a9c2c6c6 PS |
280 | comp = comp->next; |
281 | } | |
282 | printf("\n"); | |
283 | ||
284 | close(mdfd); | |
285 | ||
286 | raid_disks = info->array.raid_disks; | |
287 | chunk_size = info->array.chunk_size; | |
288 | layout = info->array.layout; | |
289 | start = getnum(argv[2], &err); | |
290 | length = getnum(argv[3], &err); | |
291 | ||
979afcb8 | 292 | if (err) { |
a9c2c6c6 | 293 | fprintf(stderr, "%s: Bad number: %s\n", prg, err); |
af3c3750 PS |
294 | exit_err = 4; |
295 | goto exitHere; | |
979afcb8 | 296 | } |
a9c2c6c6 | 297 | |
af3c3750 PS |
298 | if(start > ((info->component_size * 512) / chunk_size)) { |
299 | start = (info->component_size * 512) / chunk_size; | |
300 | fprintf(stderr, "%s: start beyond disks size\n", prg); | |
301 | } | |
a9c2c6c6 | 302 | |
af3c3750 PS |
303 | if((length == 0) || |
304 | ((length + start) > ((info->component_size * 512) / chunk_size))) { | |
305 | length = (info->component_size * 512) / chunk_size - start; | |
979afcb8 | 306 | } |
a9c2c6c6 PS |
307 | |
308 | disk_name = malloc(raid_disks * sizeof(*disk_name)); | |
979afcb8 PS |
309 | fds = malloc(raid_disks * sizeof(*fds)); |
310 | offsets = malloc(raid_disks * sizeof(*offsets)); | |
af3c3750 PS |
311 | buf = malloc(raid_disks * chunk_size); |
312 | ||
313 | if((disk_name == NULL) || | |
314 | (fds == NULL) || | |
315 | (offsets == NULL) || | |
316 | (buf == NULL)) { | |
317 | fprintf(stderr, "%s: allocation fail\n", prg); | |
318 | exit_err = 5; | |
319 | goto exitHere; | |
320 | } | |
321 | ||
979afcb8 | 322 | memset(offsets, 0, raid_disks * sizeof(*offsets)); |
af3c3750 PS |
323 | for(i=0; i<raid_disks; i++) { |
324 | fds[i] = -1; | |
325 | } | |
326 | close_flag = 1; | |
979afcb8 | 327 | |
a9c2c6c6 | 328 | comp = info->devs; |
2cf31121 | 329 | for (i=0, active_disks=0; active_disks<raid_disks; i++) { |
a9c2c6c6 | 330 | int disk_slot = comp->disk.raid_disk; |
2cf31121 PS |
331 | if(disk_slot >= 0) { |
332 | disk_name[disk_slot] = map_dev(comp->disk.major, comp->disk.minor, 0); | |
333 | offsets[disk_slot] = comp->data_offset * 512; | |
334 | fds[disk_slot] = open(disk_name[disk_slot], O_RDWR); | |
335 | if (fds[disk_slot] < 0) { | |
336 | perror(disk_name[disk_slot]); | |
337 | fprintf(stderr,"%s: cannot open %s\n", prg, disk_name[disk_slot]); | |
338 | exit_err = 6; | |
339 | goto exitHere; | |
340 | } | |
341 | active_disks++; | |
979afcb8 | 342 | } |
a9c2c6c6 | 343 | comp = comp->next; |
979afcb8 PS |
344 | } |
345 | ||
979afcb8 PS |
346 | int rv = check_stripes(fds, offsets, |
347 | raid_disks, chunk_size, level, layout, | |
a9c2c6c6 | 348 | start, length, disk_name); |
979afcb8 PS |
349 | if (rv != 0) { |
350 | fprintf(stderr, | |
a9c2c6c6 | 351 | "%s: check_stripes returned %d\n", prg, rv); |
af3c3750 PS |
352 | exit_err = 7; |
353 | goto exitHere; | |
979afcb8 PS |
354 | } |
355 | ||
af3c3750 PS |
356 | exitHere: |
357 | ||
358 | if (close_flag) | |
359 | for(i = 0; i < raid_disks; i++) | |
360 | close(fds[i]); | |
361 | ||
a9c2c6c6 | 362 | free(disk_name); |
979afcb8 PS |
363 | free(fds); |
364 | free(offsets); | |
365 | free(buf); | |
366 | ||
af3c3750 | 367 | exit(exit_err); |
979afcb8 | 368 | } |