]>
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> | |
8d8ab389 PS |
27 | #include <signal.h> |
28 | #include <sys/mman.h> | |
979afcb8 | 29 | |
3b9c9603 PS |
30 | #define CHECK_PAGE_BITS (12) |
31 | #define CHECK_PAGE_SIZE (1 << CHECK_PAGE_BITS) | |
32 | ||
42129b3f N |
33 | char const Name[] = "raid6check"; |
34 | ||
635b5861 BS |
35 | enum repair { |
36 | NO_REPAIR = 0, | |
37 | MANUAL_REPAIR, | |
38 | AUTO_REPAIR | |
39 | }; | |
40 | ||
979afcb8 PS |
41 | int geo_map(int block, unsigned long long stripe, int raid_disks, |
42 | int level, int layout); | |
43 | void qsyndrome(uint8_t *p, uint8_t *q, uint8_t **sources, int disks, int size); | |
44 | void make_tables(void); | |
f2e29ad6 RB |
45 | void ensure_zero_has_size(int chunk_size); |
46 | void raid6_datap_recov(int disks, size_t bytes, int faila, uint8_t **ptrs); | |
47 | void raid6_2data_recov(int disks, size_t bytes, int faila, int failb, | |
48 | uint8_t **ptrs); | |
49 | void xor_blocks(char *target, char **sources, int disks, int size); | |
50 | ||
979afcb8 PS |
51 | /* Collect per stripe consistency information */ |
52 | void raid6_collect(int chunk_size, uint8_t *p, uint8_t *q, | |
53 | char *chunkP, char *chunkQ, int *results) | |
54 | { | |
55 | int i; | |
56 | int data_id; | |
57 | uint8_t Px, Qx; | |
58 | extern uint8_t raid6_gflog[]; | |
59 | ||
60 | for(i = 0; i < chunk_size; i++) { | |
61 | Px = (uint8_t)chunkP[i] ^ (uint8_t)p[i]; | |
62 | Qx = (uint8_t)chunkQ[i] ^ (uint8_t)q[i]; | |
63 | ||
64 | if((Px != 0) && (Qx == 0)) | |
65 | results[i] = -1; | |
66 | ||
67 | if((Px == 0) && (Qx != 0)) | |
68 | results[i] = -2; | |
69 | ||
70 | if((Px != 0) && (Qx != 0)) { | |
71 | data_id = (raid6_gflog[Qx] - raid6_gflog[Px]); | |
72 | if(data_id < 0) data_id += 255; | |
73 | results[i] = data_id; | |
74 | } | |
75 | ||
76 | if((Px == 0) && (Qx == 0)) | |
77 | results[i] = -255; | |
78 | } | |
79 | } | |
80 | ||
3b9c9603 PS |
81 | /* Try to find out if a specific disk has problems in a CHECK_PAGE_SIZE page size */ |
82 | int raid6_stats_blk(int *results, int raid_disks) | |
979afcb8 PS |
83 | { |
84 | int i; | |
85 | int curr_broken_disk = -255; | |
86 | int prev_broken_disk = -255; | |
87 | int broken_status = 0; | |
88 | ||
3b9c9603 | 89 | for(i = 0; i < CHECK_PAGE_SIZE; i++) { |
979afcb8 PS |
90 | |
91 | if(results[i] != -255) | |
92 | curr_broken_disk = results[i]; | |
93 | ||
94 | if(curr_broken_disk >= raid_disks) | |
95 | broken_status = 2; | |
96 | ||
97 | switch(broken_status) { | |
98 | case 0: | |
99 | if(curr_broken_disk != -255) { | |
100 | prev_broken_disk = curr_broken_disk; | |
101 | broken_status = 1; | |
102 | } | |
103 | break; | |
104 | ||
105 | case 1: | |
106 | if(curr_broken_disk != prev_broken_disk) | |
107 | broken_status = 2; | |
108 | break; | |
109 | ||
110 | case 2: | |
111 | default: | |
112 | curr_broken_disk = prev_broken_disk = -65535; | |
113 | break; | |
114 | } | |
115 | } | |
116 | ||
117 | return curr_broken_disk; | |
118 | } | |
119 | ||
3b9c9603 PS |
120 | /* Collect disks status for a strip in CHECK_PAGE_SIZE page size blocks */ |
121 | void raid6_stats(int *disk, int *results, int raid_disks, int chunk_size) | |
122 | { | |
123 | int i, j; | |
124 | ||
125 | for(i = 0, j = 0; i < chunk_size; i += CHECK_PAGE_SIZE, j++) { | |
126 | disk[j] = raid6_stats_blk(&results[i], raid_disks); | |
127 | } | |
128 | } | |
129 | ||
351d7680 RB |
130 | int lock_stripe(struct mdinfo *info, unsigned long long start, |
131 | int chunk_size, int data_disks, sighandler_t *sig) { | |
132 | int rv; | |
133 | if(mlockall(MCL_CURRENT | MCL_FUTURE) != 0) { | |
134 | return 2; | |
135 | } | |
136 | ||
137 | sig[0] = signal(SIGTERM, SIG_IGN); | |
138 | sig[1] = signal(SIGINT, SIG_IGN); | |
139 | sig[2] = signal(SIGQUIT, SIG_IGN); | |
140 | ||
141 | rv = sysfs_set_num(info, NULL, "suspend_lo", start * chunk_size * data_disks); | |
142 | rv |= sysfs_set_num(info, NULL, "suspend_hi", (start + 1) * chunk_size * data_disks); | |
143 | return rv * 256; | |
144 | } | |
145 | ||
146 | int unlock_all_stripes(struct mdinfo *info, sighandler_t *sig) { | |
147 | int rv; | |
148 | rv = sysfs_set_num(info, NULL, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL); | |
149 | rv |= sysfs_set_num(info, NULL, "suspend_hi", 0); | |
150 | rv |= sysfs_set_num(info, NULL, "suspend_lo", 0); | |
151 | ||
152 | signal(SIGQUIT, sig[2]); | |
153 | signal(SIGINT, sig[1]); | |
154 | signal(SIGTERM, sig[0]); | |
155 | ||
156 | if(munlockall() != 0) | |
157 | return 3; | |
158 | return rv * 256; | |
159 | } | |
160 | ||
c22ac3b1 PS |
161 | /* Autorepair */ |
162 | int autorepair(int *disk, int diskP, int diskQ, unsigned long long start, int chunk_size, | |
163 | char *name[], int raid_disks, int data_disks, char **blocks_page, | |
164 | char **blocks, uint8_t *p, char **stripes, int *block_index_for_slot, | |
165 | int *source, unsigned long long *offsets) | |
166 | { | |
167 | int i, j; | |
168 | int pages_to_write_count = 0; | |
169 | int page_to_write[chunk_size >> CHECK_PAGE_BITS]; | |
170 | for(j = 0; j < (chunk_size >> CHECK_PAGE_BITS); j++) { | |
171 | if (disk[j] >= 0) { | |
172 | printf("Auto-repairing slot %d (%s)\n", disk[j], name[disk[j]]); | |
173 | pages_to_write_count++; | |
174 | page_to_write[j] = 1; | |
175 | for(i = 0; i < raid_disks; i++) { | |
176 | blocks_page[i] = blocks[i] + j * CHECK_PAGE_SIZE; | |
177 | } | |
178 | if (disk[j] == diskQ) { | |
179 | qsyndrome(p, (uint8_t*)stripes[diskQ] + j * CHECK_PAGE_SIZE, (uint8_t**)blocks_page, data_disks, CHECK_PAGE_SIZE); | |
180 | } | |
181 | else { | |
182 | char *all_but_failed_blocks[data_disks]; | |
183 | int failed_block_index = block_index_for_slot[disk[j]]; | |
184 | for(i = 0; i < data_disks; i++) { | |
185 | if (failed_block_index == i) { | |
186 | all_but_failed_blocks[i] = stripes[diskP] + j * CHECK_PAGE_SIZE; | |
187 | } | |
188 | else { | |
189 | all_but_failed_blocks[i] = blocks_page[i]; | |
190 | } | |
191 | } | |
192 | xor_blocks(stripes[disk[j]] + j * CHECK_PAGE_SIZE, | |
193 | all_but_failed_blocks, data_disks, CHECK_PAGE_SIZE); | |
194 | } | |
195 | } | |
196 | else { | |
197 | page_to_write[j] = 0; | |
198 | } | |
199 | } | |
200 | ||
201 | if(pages_to_write_count > 0) { | |
202 | int write_res = 0; | |
203 | for(j = 0; j < (chunk_size >> CHECK_PAGE_BITS); j++) { | |
204 | if(page_to_write[j] == 1) { | |
205 | lseek64(source[disk[j]], offsets[disk[j]] + start * chunk_size + j * CHECK_PAGE_SIZE, SEEK_SET); | |
206 | write_res += write(source[disk[j]], stripes[disk[j]] + j * CHECK_PAGE_SIZE, CHECK_PAGE_SIZE); | |
207 | } | |
208 | } | |
209 | ||
210 | if (write_res != (CHECK_PAGE_SIZE * pages_to_write_count)) { | |
211 | fprintf(stderr, "Failed to write a full chunk.\n"); | |
212 | return -1; | |
213 | } | |
214 | } | |
215 | ||
216 | return 0; | |
217 | } | |
218 | ||
497b6d6b PS |
219 | /* Manual repair */ |
220 | int manual_repair(int diskP, int diskQ, int chunk_size, int raid_disks, int data_disks, | |
221 | int failed_disk1, int failed_disk2, unsigned long long start, int *block_index_for_slot, | |
222 | char *name[], char **stripes, char **blocks, uint8_t *p, struct mdinfo *info, sighandler_t *sig, | |
223 | int *source, unsigned long long *offsets) | |
224 | { | |
225 | int err = 0; | |
226 | int i; | |
227 | printf("Repairing stripe %llu\n", start); | |
228 | printf("Assuming slots %d (%s) and %d (%s) are incorrect\n", | |
229 | failed_disk1, name[failed_disk1], | |
230 | failed_disk2, name[failed_disk2]); | |
231 | ||
232 | if (failed_disk1 == diskQ || failed_disk2 == diskQ) { | |
233 | char *all_but_failed_blocks[data_disks]; | |
234 | int failed_data_or_p; | |
235 | int failed_block_index; | |
236 | ||
237 | if (failed_disk1 == diskQ) { | |
238 | failed_data_or_p = failed_disk2; | |
239 | } | |
240 | else { | |
241 | failed_data_or_p = failed_disk1; | |
242 | } | |
243 | printf("Repairing D/P(%d) and Q\n", failed_data_or_p); | |
244 | failed_block_index = block_index_for_slot[failed_data_or_p]; | |
245 | for (i = 0; i < data_disks; i++) { | |
246 | if (failed_block_index == i) { | |
247 | all_but_failed_blocks[i] = stripes[diskP]; | |
248 | } | |
249 | else { | |
250 | all_but_failed_blocks[i] = blocks[i]; | |
251 | } | |
252 | } | |
253 | xor_blocks(stripes[failed_data_or_p], | |
254 | all_but_failed_blocks, data_disks, chunk_size); | |
255 | qsyndrome(p, (uint8_t*)stripes[diskQ], (uint8_t**)blocks, data_disks, chunk_size); | |
256 | } | |
257 | else { | |
258 | ensure_zero_has_size(chunk_size); | |
259 | if (failed_disk1 == diskP || failed_disk2 == diskP) { | |
260 | int failed_data, failed_block_index; | |
261 | if (failed_disk1 == diskP) { | |
262 | failed_data = failed_disk2; | |
263 | } | |
264 | else { | |
265 | failed_data = failed_disk1; | |
266 | } | |
267 | failed_block_index = block_index_for_slot[failed_data]; | |
268 | printf("Repairing D(%d) and P\n", failed_data); | |
269 | raid6_datap_recov(raid_disks, chunk_size, failed_block_index, (uint8_t**)blocks); | |
270 | } | |
271 | else { | |
272 | printf("Repairing D and D\n"); | |
273 | int failed_block_index1 = block_index_for_slot[failed_disk1]; | |
274 | int failed_block_index2 = block_index_for_slot[failed_disk2]; | |
275 | if (failed_block_index1 > failed_block_index2) { | |
276 | int t = failed_block_index1; | |
277 | failed_block_index1 = failed_block_index2; | |
278 | failed_block_index2 = t; | |
279 | } | |
280 | raid6_2data_recov(raid_disks, chunk_size, failed_block_index1, failed_block_index2, (uint8_t**)blocks); | |
281 | } | |
282 | } | |
283 | ||
284 | err = lock_stripe(info, start, chunk_size, data_disks, sig); | |
285 | if(err != 0) { | |
286 | if (err != 2) { | |
287 | return -1; | |
288 | } | |
289 | return -2;; | |
290 | } | |
291 | ||
292 | int write_res1, write_res2; | |
293 | off64_t seek_res; | |
294 | ||
295 | seek_res = lseek64(source[failed_disk1], | |
296 | offsets[failed_disk1] + start * chunk_size, SEEK_SET); | |
297 | if (seek_res < 0) { | |
298 | fprintf(stderr, "lseek failed for failed_disk1\n"); | |
299 | return -1; | |
300 | } | |
301 | write_res1 = write(source[failed_disk1], stripes[failed_disk1], chunk_size); | |
302 | ||
303 | seek_res = lseek64(source[failed_disk2], | |
304 | offsets[failed_disk2] + start * chunk_size, SEEK_SET); | |
305 | if (seek_res < 0) { | |
306 | fprintf(stderr, "lseek failed for failed_disk1\n"); | |
307 | return -1; | |
308 | } | |
309 | write_res2 = write(source[failed_disk2], stripes[failed_disk2], chunk_size); | |
310 | ||
311 | err = unlock_all_stripes(info, sig); | |
312 | if(err != 0) { | |
313 | return -2; | |
314 | } | |
315 | ||
316 | if (write_res1 != chunk_size || write_res2 != chunk_size) { | |
317 | fprintf(stderr, "Failed to write a complete chunk.\n"); | |
318 | return -2; | |
319 | } | |
320 | ||
321 | return 0; | |
322 | } | |
323 | ||
8d8ab389 | 324 | int check_stripes(struct mdinfo *info, int *source, unsigned long long *offsets, |
979afcb8 | 325 | int raid_disks, int chunk_size, int level, int layout, |
f2e29ad6 | 326 | unsigned long long start, unsigned long long length, char *name[], |
635b5861 | 327 | enum repair repair, int failed_disk1, int failed_disk2) |
979afcb8 PS |
328 | { |
329 | /* read the data and p and q blocks, and check we got them right */ | |
503975b9 N |
330 | char *stripe_buf = xmalloc(raid_disks * chunk_size); |
331 | char **stripes = xmalloc(raid_disks * sizeof(char*)); | |
332 | char **blocks = xmalloc(raid_disks * sizeof(char*)); | |
3b9c9603 | 333 | char **blocks_page = xmalloc(raid_disks * sizeof(char*)); |
696e95a1 | 334 | int *block_index_for_slot = xmalloc(raid_disks * sizeof(int)); |
503975b9 N |
335 | uint8_t *p = xmalloc(chunk_size); |
336 | uint8_t *q = xmalloc(chunk_size); | |
337 | int *results = xmalloc(chunk_size * sizeof(int)); | |
351d7680 | 338 | sighandler_t *sig = xmalloc(3 * sizeof(sighandler_t)); |
979afcb8 | 339 | |
3b9c9603 | 340 | int i, j; |
979afcb8 PS |
341 | int diskP, diskQ; |
342 | int data_disks = raid_disks - 2; | |
af3c3750 | 343 | int err = 0; |
979afcb8 PS |
344 | |
345 | extern int tables_ready; | |
346 | ||
347 | if (!tables_ready) | |
348 | make_tables(); | |
349 | ||
350 | for ( i = 0 ; i < raid_disks ; i++) | |
351 | stripes[i] = stripe_buf + i * chunk_size; | |
352 | ||
353 | while (length > 0) { | |
3b9c9603 | 354 | int disk[chunk_size >> CHECK_PAGE_BITS]; |
979afcb8 | 355 | |
351d7680 RB |
356 | err = lock_stripe(info, start, chunk_size, data_disks, sig); |
357 | if(err != 0) { | |
358 | if (err != 2) | |
359 | unlock_all_stripes(info, sig); | |
8d8ab389 PS |
360 | goto exitCheck; |
361 | } | |
979afcb8 | 362 | for (i = 0 ; i < raid_disks ; i++) { |
2161adce BS |
363 | off64_t seek_res = lseek64(source[i], offsets[i] + start * chunk_size, |
364 | SEEK_SET); | |
365 | if (seek_res < 0) { | |
366 | fprintf(stderr, "lseek to source %d failed\n", i); | |
367 | unlock_all_stripes(info, sig); | |
368 | err = -1; | |
369 | goto exitCheck; | |
370 | } | |
2c7b668d BS |
371 | int read_res = read(source[i], stripes[i], chunk_size); |
372 | if (read_res < chunk_size) { | |
373 | fprintf(stderr, "Failed to read complete chunk disk %d, aborting\n", i); | |
374 | unlock_all_stripes(info, sig); | |
2161adce | 375 | err = -1; |
2c7b668d BS |
376 | goto exitCheck; |
377 | } | |
979afcb8 | 378 | } |
8d8ab389 | 379 | |
979afcb8 | 380 | for (i = 0 ; i < data_disks ; i++) { |
af3c3750 | 381 | int disk = geo_map(i, start, raid_disks, level, layout); |
979afcb8 | 382 | blocks[i] = stripes[disk]; |
696e95a1 | 383 | block_index_for_slot[disk] = i; |
979afcb8 PS |
384 | } |
385 | ||
386 | qsyndrome(p, q, (uint8_t**)blocks, data_disks, chunk_size); | |
af3c3750 | 387 | diskP = geo_map(-1, start, raid_disks, level, layout); |
f2e29ad6 RB |
388 | diskQ = geo_map(-2, start, raid_disks, level, layout); |
389 | blocks[data_disks] = stripes[diskP]; | |
696e95a1 | 390 | block_index_for_slot[diskP] = data_disks; |
f2e29ad6 | 391 | blocks[data_disks+1] = stripes[diskQ]; |
696e95a1 | 392 | block_index_for_slot[diskQ] = data_disks+1; |
237e40ce | 393 | |
af3c3750 | 394 | raid6_collect(chunk_size, p, q, stripes[diskP], stripes[diskQ], results); |
3b9c9603 | 395 | raid6_stats(disk, results, raid_disks, chunk_size); |
979afcb8 | 396 | |
3b9c9603 PS |
397 | for(j = 0; j < (chunk_size >> CHECK_PAGE_BITS); j++) { |
398 | if(disk[j] >= -2) { | |
399 | disk[j] = geo_map(disk[j], start, raid_disks, level, layout); | |
400 | } | |
401 | if(disk[j] >= 0) { | |
afc755e9 | 402 | printf("Error detected at stripe %llu, page %d: possible failed disk slot: %d --> %s\n", |
3b9c9603 PS |
403 | start, j, disk[j], name[disk[j]]); |
404 | } | |
405 | if(disk[j] == -65535) { | |
afc755e9 | 406 | printf("Error detected at stripe %llu, page %d: disk slot unknown\n", start, j); |
3b9c9603 | 407 | } |
979afcb8 | 408 | } |
3b9c9603 | 409 | |
3cfd0297 | 410 | if(repair == AUTO_REPAIR) { |
c22ac3b1 PS |
411 | err = autorepair(disk, diskP, diskQ, start, chunk_size, |
412 | name, raid_disks, data_disks, blocks_page, | |
413 | blocks, p, stripes, block_index_for_slot, | |
414 | source, offsets); | |
415 | if(err != 0) { | |
416 | unlock_all_stripes(info, sig); | |
417 | goto exitCheck; | |
3cfd0297 PS |
418 | } |
419 | } | |
420 | ||
421 | err = unlock_all_stripes(info, sig); | |
422 | if(err != 0) { | |
423 | goto exitCheck; | |
424 | } | |
425 | ||
635b5861 | 426 | if(repair == MANUAL_REPAIR) { |
497b6d6b PS |
427 | err = manual_repair(diskP, diskQ, chunk_size, raid_disks, data_disks, |
428 | failed_disk1, failed_disk2, start, block_index_for_slot, | |
429 | name, stripes, blocks, p, info, sig, | |
430 | source, offsets); | |
431 | if(err == -1) { | |
2161adce | 432 | unlock_all_stripes(info, sig); |
2161adce BS |
433 | goto exitCheck; |
434 | } | |
3b9c9603 PS |
435 | } |
436 | ||
af3c3750 PS |
437 | length--; |
438 | start++; | |
979afcb8 PS |
439 | } |
440 | ||
af3c3750 PS |
441 | exitCheck: |
442 | ||
979afcb8 PS |
443 | free(stripe_buf); |
444 | free(stripes); | |
445 | free(blocks); | |
3b9c9603 | 446 | free(blocks_page); |
3a89d754 | 447 | free(block_index_for_slot); |
979afcb8 PS |
448 | free(p); |
449 | free(q); | |
450 | free(results); | |
3a89d754 | 451 | free(sig); |
979afcb8 | 452 | |
af3c3750 | 453 | return err; |
979afcb8 PS |
454 | } |
455 | ||
456 | unsigned long long getnum(char *str, char **err) | |
457 | { | |
458 | char *e; | |
459 | unsigned long long rv = strtoull(str, &e, 10); | |
460 | if (e==str || *e) { | |
461 | *err = str; | |
462 | return 0; | |
463 | } | |
464 | return rv; | |
465 | } | |
466 | ||
467 | int main(int argc, char *argv[]) | |
468 | { | |
a9c2c6c6 | 469 | /* md_device start length */ |
af3c3750 PS |
470 | int *fds = NULL; |
471 | char *buf = NULL; | |
472 | char **disk_name = NULL; | |
473 | unsigned long long *offsets = NULL; | |
474 | int raid_disks = 0; | |
2cf31121 | 475 | int active_disks; |
af3c3750 PS |
476 | int chunk_size = 0; |
477 | int layout = -1; | |
979afcb8 | 478 | int level = 6; |
635b5861 | 479 | enum repair repair = NO_REPAIR; |
2c7b668d BS |
480 | int failed_disk1 = -1; |
481 | int failed_disk2 = -1; | |
979afcb8 PS |
482 | unsigned long long start, length; |
483 | int i; | |
a9c2c6c6 | 484 | int mdfd; |
8d8ab389 | 485 | struct mdinfo *info = NULL, *comp = NULL; |
979afcb8 | 486 | char *err = NULL; |
af3c3750 PS |
487 | int exit_err = 0; |
488 | int close_flag = 0; | |
489 | char *prg = strrchr(argv[0], '/'); | |
490 | ||
491 | if (prg == NULL) | |
492 | prg = argv[0]; | |
493 | else | |
494 | prg++; | |
495 | ||
496 | if (argc < 4) { | |
8a63c731 | 497 | fprintf(stderr, "Usage: %s md_device start_stripe length_stripes [autorepair]\n", prg); |
f2e29ad6 | 498 | fprintf(stderr, " or: %s md_device repair stripe failed_slot_1 failed_slot_2\n", prg); |
af3c3750 PS |
499 | exit_err = 1; |
500 | goto exitHere; | |
979afcb8 PS |
501 | } |
502 | ||
a9c2c6c6 PS |
503 | mdfd = open(argv[1], O_RDONLY); |
504 | if(mdfd < 0) { | |
505 | perror(argv[1]); | |
e7b84f9d | 506 | fprintf(stderr, "%s: cannot open %s\n", prg, argv[1]); |
af3c3750 PS |
507 | exit_err = 2; |
508 | goto exitHere; | |
a9c2c6c6 PS |
509 | } |
510 | ||
f8fcf7a1 | 511 | info = sysfs_read(mdfd, NULL, |
a9c2c6c6 PS |
512 | GET_LEVEL| |
513 | GET_LAYOUT| | |
514 | GET_DISKS| | |
2cf31121 | 515 | GET_DEGRADED | |
a9c2c6c6 PS |
516 | GET_COMPONENT| |
517 | GET_CHUNK| | |
518 | GET_DEVS| | |
519 | GET_OFFSET| | |
520 | GET_SIZE); | |
521 | ||
8d8ab389 PS |
522 | if(info == NULL) { |
523 | fprintf(stderr, "%s: Error reading sysfs information of %s\n", prg, argv[1]); | |
524 | exit_err = 9; | |
525 | goto exitHere; | |
526 | } | |
527 | ||
a9c2c6c6 PS |
528 | if(info->array.level != level) { |
529 | fprintf(stderr, "%s: %s not a RAID-6\n", prg, argv[1]); | |
af3c3750 PS |
530 | exit_err = 3; |
531 | goto exitHere; | |
a9c2c6c6 PS |
532 | } |
533 | ||
2cf31121 PS |
534 | if(info->array.failed_disks > 0) { |
535 | fprintf(stderr, "%s: %s degraded array\n", prg, argv[1]); | |
536 | exit_err = 8; | |
537 | goto exitHere; | |
538 | } | |
539 | ||
a9c2c6c6 PS |
540 | printf("layout: %d\n", info->array.layout); |
541 | printf("disks: %d\n", info->array.raid_disks); | |
af3c3750 PS |
542 | printf("component size: %llu\n", info->component_size * 512); |
543 | printf("total stripes: %llu\n", (info->component_size * 512) / info->array.chunk_size); | |
a9c2c6c6 PS |
544 | printf("chunk size: %d\n", info->array.chunk_size); |
545 | printf("\n"); | |
546 | ||
547 | comp = info->devs; | |
2cf31121 | 548 | for(i = 0, active_disks = 0; active_disks < info->array.raid_disks; i++) { |
a9c2c6c6 | 549 | printf("disk: %d - offset: %llu - size: %llu - name: %s - slot: %d\n", |
af3c3750 | 550 | i, comp->data_offset * 512, comp->component_size * 512, |
a9c2c6c6 PS |
551 | map_dev(comp->disk.major, comp->disk.minor, 0), |
552 | comp->disk.raid_disk); | |
2cf31121 PS |
553 | if(comp->disk.raid_disk >= 0) |
554 | active_disks++; | |
a9c2c6c6 PS |
555 | comp = comp->next; |
556 | } | |
557 | printf("\n"); | |
558 | ||
559 | close(mdfd); | |
560 | ||
561 | raid_disks = info->array.raid_disks; | |
562 | chunk_size = info->array.chunk_size; | |
563 | layout = info->array.layout; | |
f2e29ad6 RB |
564 | if (strcmp(argv[2], "repair")==0) { |
565 | if (argc < 6) { | |
566 | fprintf(stderr, "For repair mode, call %s md_device repair stripe failed_slot_1 failed_slot_2\n", prg); | |
567 | exit_err = 1; | |
568 | goto exitHere; | |
569 | } | |
635b5861 | 570 | repair = MANUAL_REPAIR; |
f2e29ad6 RB |
571 | start = getnum(argv[3], &err); |
572 | length = 1; | |
573 | failed_disk1 = getnum(argv[4], &err); | |
574 | failed_disk2 = getnum(argv[5], &err); | |
575 | ||
b67e45b8 | 576 | if(failed_disk1 >= info->array.raid_disks) { |
f2e29ad6 RB |
577 | fprintf(stderr, "%s: failed_slot_1 index is higher than number of devices in raid\n", prg); |
578 | exit_err = 4; | |
579 | goto exitHere; | |
580 | } | |
b67e45b8 | 581 | if(failed_disk2 >= info->array.raid_disks) { |
f2e29ad6 RB |
582 | fprintf(stderr, "%s: failed_slot_2 index is higher than number of devices in raid\n", prg); |
583 | exit_err = 4; | |
584 | goto exitHere; | |
585 | } | |
586 | if(failed_disk1 == failed_disk2) { | |
587 | fprintf(stderr, "%s: failed_slot_1 and failed_slot_2 are the same\n", prg); | |
588 | exit_err = 4; | |
589 | goto exitHere; | |
590 | } | |
591 | } | |
592 | else { | |
593 | start = getnum(argv[2], &err); | |
594 | length = getnum(argv[3], &err); | |
8a63c731 | 595 | if (argc >= 5 && strcmp(argv[4], "autorepair")==0) |
635b5861 | 596 | repair = AUTO_REPAIR; |
f2e29ad6 | 597 | } |
a9c2c6c6 | 598 | |
979afcb8 | 599 | if (err) { |
a9c2c6c6 | 600 | fprintf(stderr, "%s: Bad number: %s\n", prg, err); |
af3c3750 PS |
601 | exit_err = 4; |
602 | goto exitHere; | |
979afcb8 | 603 | } |
a9c2c6c6 | 604 | |
af3c3750 PS |
605 | if(start > ((info->component_size * 512) / chunk_size)) { |
606 | start = (info->component_size * 512) / chunk_size; | |
607 | fprintf(stderr, "%s: start beyond disks size\n", prg); | |
608 | } | |
a9c2c6c6 | 609 | |
af3c3750 PS |
610 | if((length == 0) || |
611 | ((length + start) > ((info->component_size * 512) / chunk_size))) { | |
612 | length = (info->component_size * 512) / chunk_size - start; | |
979afcb8 | 613 | } |
a9c2c6c6 | 614 | |
503975b9 N |
615 | disk_name = xmalloc(raid_disks * sizeof(*disk_name)); |
616 | fds = xmalloc(raid_disks * sizeof(*fds)); | |
617 | offsets = xcalloc(raid_disks, sizeof(*offsets)); | |
618 | buf = xmalloc(raid_disks * chunk_size); | |
af3c3750 | 619 | |
af3c3750 PS |
620 | for(i=0; i<raid_disks; i++) { |
621 | fds[i] = -1; | |
622 | } | |
623 | close_flag = 1; | |
979afcb8 | 624 | |
a9c2c6c6 | 625 | comp = info->devs; |
2cf31121 | 626 | for (i=0, active_disks=0; active_disks<raid_disks; i++) { |
a9c2c6c6 | 627 | int disk_slot = comp->disk.raid_disk; |
2cf31121 PS |
628 | if(disk_slot >= 0) { |
629 | disk_name[disk_slot] = map_dev(comp->disk.major, comp->disk.minor, 0); | |
630 | offsets[disk_slot] = comp->data_offset * 512; | |
e645b341 | 631 | fds[disk_slot] = open(disk_name[disk_slot], O_RDWR | O_SYNC); |
2cf31121 PS |
632 | if (fds[disk_slot] < 0) { |
633 | perror(disk_name[disk_slot]); | |
634 | fprintf(stderr,"%s: cannot open %s\n", prg, disk_name[disk_slot]); | |
635 | exit_err = 6; | |
636 | goto exitHere; | |
637 | } | |
638 | active_disks++; | |
979afcb8 | 639 | } |
a9c2c6c6 | 640 | comp = comp->next; |
979afcb8 PS |
641 | } |
642 | ||
8d8ab389 | 643 | int rv = check_stripes(info, fds, offsets, |
979afcb8 | 644 | raid_disks, chunk_size, level, layout, |
f2e29ad6 | 645 | start, length, disk_name, repair, failed_disk1, failed_disk2); |
979afcb8 | 646 | if (rv != 0) { |
7a862a02 | 647 | fprintf(stderr, "%s: check_stripes returned %d\n", prg, rv); |
af3c3750 PS |
648 | exit_err = 7; |
649 | goto exitHere; | |
979afcb8 PS |
650 | } |
651 | ||
af3c3750 PS |
652 | exitHere: |
653 | ||
654 | if (close_flag) | |
655 | for(i = 0; i < raid_disks; i++) | |
656 | close(fds[i]); | |
657 | ||
a9c2c6c6 | 658 | free(disk_name); |
979afcb8 PS |
659 | free(fds); |
660 | free(offsets); | |
661 | free(buf); | |
662 | ||
af3c3750 | 663 | exit(exit_err); |
979afcb8 | 664 | } |