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