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