]>
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; | |
210 | int chunk_size = 0; | |
211 | int layout = -1; | |
979afcb8 PS |
212 | int level = 6; |
213 | unsigned long long start, length; | |
214 | int i; | |
a9c2c6c6 PS |
215 | int mdfd; |
216 | struct mdinfo *info, *comp; | |
979afcb8 | 217 | char *err = NULL; |
af3c3750 PS |
218 | int exit_err = 0; |
219 | int close_flag = 0; | |
220 | char *prg = strrchr(argv[0], '/'); | |
221 | ||
222 | if (prg == NULL) | |
223 | prg = argv[0]; | |
224 | else | |
225 | prg++; | |
226 | ||
227 | if (argc < 4) { | |
228 | fprintf(stderr, "Usage: %s md_device start_stripe length_stripes\n", prg); | |
229 | exit_err = 1; | |
230 | goto exitHere; | |
979afcb8 PS |
231 | } |
232 | ||
a9c2c6c6 PS |
233 | mdfd = open(argv[1], O_RDONLY); |
234 | if(mdfd < 0) { | |
235 | perror(argv[1]); | |
236 | fprintf(stderr,"%s: cannot open %s\n", prg, argv[1]); | |
af3c3750 PS |
237 | exit_err = 2; |
238 | goto exitHere; | |
a9c2c6c6 PS |
239 | } |
240 | ||
241 | info = sysfs_read(mdfd, -1, | |
242 | GET_LEVEL| | |
243 | GET_LAYOUT| | |
244 | GET_DISKS| | |
245 | GET_COMPONENT| | |
246 | GET_CHUNK| | |
247 | GET_DEVS| | |
248 | GET_OFFSET| | |
249 | GET_SIZE); | |
250 | ||
251 | if(info->array.level != level) { | |
252 | fprintf(stderr, "%s: %s not a RAID-6\n", prg, argv[1]); | |
af3c3750 PS |
253 | exit_err = 3; |
254 | goto exitHere; | |
a9c2c6c6 PS |
255 | } |
256 | ||
257 | printf("layout: %d\n", info->array.layout); | |
258 | printf("disks: %d\n", info->array.raid_disks); | |
af3c3750 PS |
259 | printf("component size: %llu\n", info->component_size * 512); |
260 | printf("total stripes: %llu\n", (info->component_size * 512) / info->array.chunk_size); | |
a9c2c6c6 PS |
261 | printf("chunk size: %d\n", info->array.chunk_size); |
262 | printf("\n"); | |
263 | ||
264 | comp = info->devs; | |
265 | for(i = 0; i < info->array.raid_disks; i++) { | |
266 | printf("disk: %d - offset: %llu - size: %llu - name: %s - slot: %d\n", | |
af3c3750 | 267 | i, comp->data_offset * 512, comp->component_size * 512, |
a9c2c6c6 PS |
268 | map_dev(comp->disk.major, comp->disk.minor, 0), |
269 | comp->disk.raid_disk); | |
270 | ||
271 | comp = comp->next; | |
272 | } | |
273 | printf("\n"); | |
274 | ||
275 | close(mdfd); | |
276 | ||
277 | raid_disks = info->array.raid_disks; | |
278 | chunk_size = info->array.chunk_size; | |
279 | layout = info->array.layout; | |
280 | start = getnum(argv[2], &err); | |
281 | length = getnum(argv[3], &err); | |
282 | ||
979afcb8 | 283 | if (err) { |
a9c2c6c6 | 284 | fprintf(stderr, "%s: Bad number: %s\n", prg, err); |
af3c3750 PS |
285 | exit_err = 4; |
286 | goto exitHere; | |
979afcb8 | 287 | } |
a9c2c6c6 | 288 | |
af3c3750 PS |
289 | if(start > ((info->component_size * 512) / chunk_size)) { |
290 | start = (info->component_size * 512) / chunk_size; | |
291 | fprintf(stderr, "%s: start beyond disks size\n", prg); | |
292 | } | |
a9c2c6c6 | 293 | |
af3c3750 PS |
294 | if((length == 0) || |
295 | ((length + start) > ((info->component_size * 512) / chunk_size))) { | |
296 | length = (info->component_size * 512) / chunk_size - start; | |
979afcb8 | 297 | } |
a9c2c6c6 PS |
298 | |
299 | disk_name = malloc(raid_disks * sizeof(*disk_name)); | |
979afcb8 PS |
300 | fds = malloc(raid_disks * sizeof(*fds)); |
301 | offsets = malloc(raid_disks * sizeof(*offsets)); | |
af3c3750 PS |
302 | buf = malloc(raid_disks * chunk_size); |
303 | ||
304 | if((disk_name == NULL) || | |
305 | (fds == NULL) || | |
306 | (offsets == NULL) || | |
307 | (buf == NULL)) { | |
308 | fprintf(stderr, "%s: allocation fail\n", prg); | |
309 | exit_err = 5; | |
310 | goto exitHere; | |
311 | } | |
312 | ||
979afcb8 | 313 | memset(offsets, 0, raid_disks * sizeof(*offsets)); |
af3c3750 PS |
314 | for(i=0; i<raid_disks; i++) { |
315 | fds[i] = -1; | |
316 | } | |
317 | close_flag = 1; | |
979afcb8 | 318 | |
a9c2c6c6 | 319 | comp = info->devs; |
979afcb8 | 320 | for (i=0; i<raid_disks; i++) { |
a9c2c6c6 PS |
321 | int disk_slot = comp->disk.raid_disk; |
322 | disk_name[disk_slot] = map_dev(comp->disk.major, comp->disk.minor, 0); | |
323 | offsets[disk_slot] = comp->data_offset * 512; | |
324 | fds[disk_slot] = open(disk_name[disk_slot], O_RDWR); | |
325 | if (fds[disk_slot] < 0) { | |
326 | perror(disk_name[disk_slot]); | |
327 | fprintf(stderr,"%s: cannot open %s\n", prg, disk_name[disk_slot]); | |
af3c3750 PS |
328 | exit_err = 6; |
329 | goto exitHere; | |
979afcb8 | 330 | } |
a9c2c6c6 PS |
331 | |
332 | comp = comp->next; | |
979afcb8 PS |
333 | } |
334 | ||
979afcb8 PS |
335 | int rv = check_stripes(fds, offsets, |
336 | raid_disks, chunk_size, level, layout, | |
a9c2c6c6 | 337 | start, length, disk_name); |
979afcb8 PS |
338 | if (rv != 0) { |
339 | fprintf(stderr, | |
a9c2c6c6 | 340 | "%s: check_stripes returned %d\n", prg, rv); |
af3c3750 PS |
341 | exit_err = 7; |
342 | goto exitHere; | |
979afcb8 PS |
343 | } |
344 | ||
af3c3750 PS |
345 | exitHere: |
346 | ||
347 | if (close_flag) | |
348 | for(i = 0; i < raid_disks; i++) | |
349 | close(fds[i]); | |
350 | ||
a9c2c6c6 | 351 | free(disk_name); |
979afcb8 PS |
352 | free(fds); |
353 | free(offsets); | |
354 | free(buf); | |
355 | ||
af3c3750 | 356 | exit(exit_err); |
979afcb8 | 357 | } |