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