]>
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; |
76cd79d3 | 341 | int diskP, diskQ, diskD; |
979afcb8 | 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 | |
76cd79d3 N |
380 | diskP = geo_map(-1, start, raid_disks, level, layout); |
381 | diskQ = geo_map(-2, start, raid_disks, level, layout); | |
382 | /* The syndrome-order if disks starts immediately after 'Q', | |
383 | * but skips P */ | |
384 | diskD = diskQ; | |
979afcb8 | 385 | for (i = 0 ; i < data_disks ; i++) { |
76cd79d3 N |
386 | diskD = diskD + 1; |
387 | if (diskD > raid_disks) | |
388 | diskD = 0; | |
389 | if (diskD == diskP) | |
390 | diskD += 1; | |
391 | if (diskD > raid_disks) | |
392 | diskD = 0; | |
393 | blocks[i] = stripes[diskD]; | |
394 | block_index_for_slot[diskD] = i; | |
979afcb8 PS |
395 | } |
396 | ||
397 | qsyndrome(p, q, (uint8_t**)blocks, data_disks, chunk_size); | |
f2e29ad6 | 398 | blocks[data_disks] = stripes[diskP]; |
696e95a1 | 399 | block_index_for_slot[diskP] = data_disks; |
f2e29ad6 | 400 | blocks[data_disks+1] = stripes[diskQ]; |
696e95a1 | 401 | block_index_for_slot[diskQ] = data_disks+1; |
237e40ce | 402 | |
af3c3750 | 403 | raid6_collect(chunk_size, p, q, stripes[diskP], stripes[diskQ], results); |
3b9c9603 | 404 | raid6_stats(disk, results, raid_disks, chunk_size); |
979afcb8 | 405 | |
3b9c9603 | 406 | for(j = 0; j < (chunk_size >> CHECK_PAGE_BITS); j++) { |
108bd874 | 407 | int role = disk[j]; |
3b9c9603 PS |
408 | if(disk[j] >= -2) { |
409 | disk[j] = geo_map(disk[j], start, raid_disks, level, layout); | |
410 | } | |
411 | if(disk[j] >= 0) { | |
108bd874 N |
412 | printf("Error detected at stripe %llu, page %d: possible failed disk slot %d: %d --> %s\n", |
413 | start, j, role, disk[j], name[disk[j]]); | |
3b9c9603 PS |
414 | } |
415 | if(disk[j] == -65535) { | |
afc755e9 | 416 | printf("Error detected at stripe %llu, page %d: disk slot unknown\n", start, j); |
3b9c9603 | 417 | } |
979afcb8 | 418 | } |
3b9c9603 | 419 | |
3cfd0297 | 420 | if(repair == AUTO_REPAIR) { |
c22ac3b1 PS |
421 | err = autorepair(disk, diskP, diskQ, start, chunk_size, |
422 | name, raid_disks, data_disks, blocks_page, | |
423 | blocks, p, stripes, block_index_for_slot, | |
424 | source, offsets); | |
425 | if(err != 0) { | |
426 | unlock_all_stripes(info, sig); | |
427 | goto exitCheck; | |
3cfd0297 PS |
428 | } |
429 | } | |
430 | ||
431 | err = unlock_all_stripes(info, sig); | |
432 | if(err != 0) { | |
433 | goto exitCheck; | |
434 | } | |
435 | ||
635b5861 | 436 | if(repair == MANUAL_REPAIR) { |
497b6d6b PS |
437 | err = manual_repair(diskP, diskQ, chunk_size, raid_disks, data_disks, |
438 | failed_disk1, failed_disk2, start, block_index_for_slot, | |
439 | name, stripes, blocks, p, info, sig, | |
440 | source, offsets); | |
441 | if(err == -1) { | |
2161adce | 442 | unlock_all_stripes(info, sig); |
2161adce BS |
443 | goto exitCheck; |
444 | } | |
3b9c9603 PS |
445 | } |
446 | ||
af3c3750 PS |
447 | length--; |
448 | start++; | |
979afcb8 PS |
449 | } |
450 | ||
af3c3750 PS |
451 | exitCheck: |
452 | ||
979afcb8 PS |
453 | free(stripe_buf); |
454 | free(stripes); | |
455 | free(blocks); | |
3b9c9603 | 456 | free(blocks_page); |
3a89d754 | 457 | free(block_index_for_slot); |
979afcb8 PS |
458 | free(p); |
459 | free(q); | |
460 | free(results); | |
3a89d754 | 461 | free(sig); |
979afcb8 | 462 | |
af3c3750 | 463 | return err; |
979afcb8 PS |
464 | } |
465 | ||
466 | unsigned long long getnum(char *str, char **err) | |
467 | { | |
468 | char *e; | |
469 | unsigned long long rv = strtoull(str, &e, 10); | |
470 | if (e==str || *e) { | |
471 | *err = str; | |
472 | return 0; | |
473 | } | |
474 | return rv; | |
475 | } | |
476 | ||
477 | int main(int argc, char *argv[]) | |
478 | { | |
a9c2c6c6 | 479 | /* md_device start length */ |
af3c3750 PS |
480 | int *fds = NULL; |
481 | char *buf = NULL; | |
482 | char **disk_name = NULL; | |
483 | unsigned long long *offsets = NULL; | |
484 | int raid_disks = 0; | |
2cf31121 | 485 | int active_disks; |
af3c3750 PS |
486 | int chunk_size = 0; |
487 | int layout = -1; | |
979afcb8 | 488 | int level = 6; |
635b5861 | 489 | enum repair repair = NO_REPAIR; |
2c7b668d BS |
490 | int failed_disk1 = -1; |
491 | int failed_disk2 = -1; | |
979afcb8 PS |
492 | unsigned long long start, length; |
493 | int i; | |
a9c2c6c6 | 494 | int mdfd; |
8d8ab389 | 495 | struct mdinfo *info = NULL, *comp = NULL; |
979afcb8 | 496 | char *err = NULL; |
af3c3750 PS |
497 | int exit_err = 0; |
498 | int close_flag = 0; | |
499 | char *prg = strrchr(argv[0], '/'); | |
500 | ||
501 | if (prg == NULL) | |
502 | prg = argv[0]; | |
503 | else | |
504 | prg++; | |
505 | ||
506 | if (argc < 4) { | |
8a63c731 | 507 | fprintf(stderr, "Usage: %s md_device start_stripe length_stripes [autorepair]\n", prg); |
f2e29ad6 | 508 | fprintf(stderr, " or: %s md_device repair stripe failed_slot_1 failed_slot_2\n", prg); |
af3c3750 PS |
509 | exit_err = 1; |
510 | goto exitHere; | |
979afcb8 PS |
511 | } |
512 | ||
a9c2c6c6 PS |
513 | mdfd = open(argv[1], O_RDONLY); |
514 | if(mdfd < 0) { | |
515 | perror(argv[1]); | |
e7b84f9d | 516 | fprintf(stderr, "%s: cannot open %s\n", prg, argv[1]); |
af3c3750 PS |
517 | exit_err = 2; |
518 | goto exitHere; | |
a9c2c6c6 PS |
519 | } |
520 | ||
f8fcf7a1 | 521 | info = sysfs_read(mdfd, NULL, |
a9c2c6c6 PS |
522 | GET_LEVEL| |
523 | GET_LAYOUT| | |
524 | GET_DISKS| | |
2cf31121 | 525 | GET_DEGRADED | |
a9c2c6c6 PS |
526 | GET_COMPONENT| |
527 | GET_CHUNK| | |
528 | GET_DEVS| | |
529 | GET_OFFSET| | |
530 | GET_SIZE); | |
531 | ||
8d8ab389 PS |
532 | if(info == NULL) { |
533 | fprintf(stderr, "%s: Error reading sysfs information of %s\n", prg, argv[1]); | |
534 | exit_err = 9; | |
535 | goto exitHere; | |
536 | } | |
537 | ||
a9c2c6c6 PS |
538 | if(info->array.level != level) { |
539 | fprintf(stderr, "%s: %s not a RAID-6\n", prg, argv[1]); | |
af3c3750 PS |
540 | exit_err = 3; |
541 | goto exitHere; | |
a9c2c6c6 PS |
542 | } |
543 | ||
2cf31121 PS |
544 | if(info->array.failed_disks > 0) { |
545 | fprintf(stderr, "%s: %s degraded array\n", prg, argv[1]); | |
546 | exit_err = 8; | |
547 | goto exitHere; | |
548 | } | |
549 | ||
a9c2c6c6 PS |
550 | printf("layout: %d\n", info->array.layout); |
551 | printf("disks: %d\n", info->array.raid_disks); | |
af3c3750 PS |
552 | printf("component size: %llu\n", info->component_size * 512); |
553 | printf("total stripes: %llu\n", (info->component_size * 512) / info->array.chunk_size); | |
a9c2c6c6 PS |
554 | printf("chunk size: %d\n", info->array.chunk_size); |
555 | printf("\n"); | |
556 | ||
557 | comp = info->devs; | |
2cf31121 | 558 | for(i = 0, active_disks = 0; active_disks < info->array.raid_disks; i++) { |
a9c2c6c6 | 559 | printf("disk: %d - offset: %llu - size: %llu - name: %s - slot: %d\n", |
af3c3750 | 560 | i, comp->data_offset * 512, comp->component_size * 512, |
a9c2c6c6 PS |
561 | map_dev(comp->disk.major, comp->disk.minor, 0), |
562 | comp->disk.raid_disk); | |
2cf31121 PS |
563 | if(comp->disk.raid_disk >= 0) |
564 | active_disks++; | |
a9c2c6c6 PS |
565 | comp = comp->next; |
566 | } | |
567 | printf("\n"); | |
568 | ||
569 | close(mdfd); | |
570 | ||
571 | raid_disks = info->array.raid_disks; | |
572 | chunk_size = info->array.chunk_size; | |
573 | layout = info->array.layout; | |
f2e29ad6 RB |
574 | if (strcmp(argv[2], "repair")==0) { |
575 | if (argc < 6) { | |
576 | fprintf(stderr, "For repair mode, call %s md_device repair stripe failed_slot_1 failed_slot_2\n", prg); | |
577 | exit_err = 1; | |
578 | goto exitHere; | |
579 | } | |
635b5861 | 580 | repair = MANUAL_REPAIR; |
f2e29ad6 RB |
581 | start = getnum(argv[3], &err); |
582 | length = 1; | |
583 | failed_disk1 = getnum(argv[4], &err); | |
584 | failed_disk2 = getnum(argv[5], &err); | |
585 | ||
b67e45b8 | 586 | if(failed_disk1 >= info->array.raid_disks) { |
f2e29ad6 RB |
587 | fprintf(stderr, "%s: failed_slot_1 index is higher than number of devices in raid\n", prg); |
588 | exit_err = 4; | |
589 | goto exitHere; | |
590 | } | |
b67e45b8 | 591 | if(failed_disk2 >= info->array.raid_disks) { |
f2e29ad6 RB |
592 | fprintf(stderr, "%s: failed_slot_2 index is higher than number of devices in raid\n", prg); |
593 | exit_err = 4; | |
594 | goto exitHere; | |
595 | } | |
596 | if(failed_disk1 == failed_disk2) { | |
597 | fprintf(stderr, "%s: failed_slot_1 and failed_slot_2 are the same\n", prg); | |
598 | exit_err = 4; | |
599 | goto exitHere; | |
600 | } | |
601 | } | |
602 | else { | |
603 | start = getnum(argv[2], &err); | |
604 | length = getnum(argv[3], &err); | |
8a63c731 | 605 | if (argc >= 5 && strcmp(argv[4], "autorepair")==0) |
635b5861 | 606 | repair = AUTO_REPAIR; |
f2e29ad6 | 607 | } |
a9c2c6c6 | 608 | |
979afcb8 | 609 | if (err) { |
a9c2c6c6 | 610 | fprintf(stderr, "%s: Bad number: %s\n", prg, err); |
af3c3750 PS |
611 | exit_err = 4; |
612 | goto exitHere; | |
979afcb8 | 613 | } |
a9c2c6c6 | 614 | |
af3c3750 PS |
615 | if(start > ((info->component_size * 512) / chunk_size)) { |
616 | start = (info->component_size * 512) / chunk_size; | |
617 | fprintf(stderr, "%s: start beyond disks size\n", prg); | |
618 | } | |
a9c2c6c6 | 619 | |
af3c3750 PS |
620 | if((length == 0) || |
621 | ((length + start) > ((info->component_size * 512) / chunk_size))) { | |
622 | length = (info->component_size * 512) / chunk_size - start; | |
979afcb8 | 623 | } |
a9c2c6c6 | 624 | |
503975b9 N |
625 | disk_name = xmalloc(raid_disks * sizeof(*disk_name)); |
626 | fds = xmalloc(raid_disks * sizeof(*fds)); | |
627 | offsets = xcalloc(raid_disks, sizeof(*offsets)); | |
628 | buf = xmalloc(raid_disks * chunk_size); | |
af3c3750 | 629 | |
af3c3750 PS |
630 | for(i=0; i<raid_disks; i++) { |
631 | fds[i] = -1; | |
632 | } | |
633 | close_flag = 1; | |
979afcb8 | 634 | |
a9c2c6c6 | 635 | comp = info->devs; |
2cf31121 | 636 | for (i=0, active_disks=0; active_disks<raid_disks; i++) { |
a9c2c6c6 | 637 | int disk_slot = comp->disk.raid_disk; |
2cf31121 PS |
638 | if(disk_slot >= 0) { |
639 | disk_name[disk_slot] = map_dev(comp->disk.major, comp->disk.minor, 0); | |
640 | offsets[disk_slot] = comp->data_offset * 512; | |
e645b341 | 641 | fds[disk_slot] = open(disk_name[disk_slot], O_RDWR | O_SYNC); |
2cf31121 PS |
642 | if (fds[disk_slot] < 0) { |
643 | perror(disk_name[disk_slot]); | |
644 | fprintf(stderr,"%s: cannot open %s\n", prg, disk_name[disk_slot]); | |
645 | exit_err = 6; | |
646 | goto exitHere; | |
647 | } | |
648 | active_disks++; | |
979afcb8 | 649 | } |
a9c2c6c6 | 650 | comp = comp->next; |
979afcb8 PS |
651 | } |
652 | ||
8d8ab389 | 653 | int rv = check_stripes(info, fds, offsets, |
979afcb8 | 654 | raid_disks, chunk_size, level, layout, |
f2e29ad6 | 655 | start, length, disk_name, repair, failed_disk1, failed_disk2); |
979afcb8 | 656 | if (rv != 0) { |
7a862a02 | 657 | fprintf(stderr, "%s: check_stripes returned %d\n", prg, rv); |
af3c3750 PS |
658 | exit_err = 7; |
659 | goto exitHere; | |
979afcb8 PS |
660 | } |
661 | ||
af3c3750 PS |
662 | exitHere: |
663 | ||
664 | if (close_flag) | |
665 | for(i = 0; i < raid_disks; i++) | |
666 | close(fds[i]); | |
667 | ||
a9c2c6c6 | 668 | free(disk_name); |
979afcb8 PS |
669 | free(fds); |
670 | free(offsets); | |
671 | free(buf); | |
672 | ||
af3c3750 | 673 | exit(exit_err); |
979afcb8 | 674 | } |