Line data Source code
1 : /*
2 : * Copyright (C) 2011 Andrea Mazzoleni
3 : *
4 : * This program is free software: you can redistribute it and/or modify
5 : * it under the terms of the GNU General Public License as published by
6 : * the Free Software Foundation, either version 3 of the License, or
7 : * (at your option) any later version.
8 : *
9 : * This program is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : * GNU General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU General Public License
15 : * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 : */
17 :
18 : #include "portable.h"
19 :
20 : #include "support.h"
21 : #include "util.h"
22 : #include "elem.h"
23 : #include "import.h"
24 : #include "search.h"
25 : #include "state.h"
26 : #include "parity.h"
27 : #include "handle.h"
28 : #include "raid/raid.h"
29 : #include "raid/combo.h"
30 :
31 : /****************************************************************************/
32 : /* check */
33 :
34 : /**
35 : * A block that failed the hash check, or that was deleted.
36 : */
37 : struct failed_struct {
38 : /**
39 : * If we know for sure that the block is garbage or missing
40 : * and it needs to be recovered and rewritten to the disk.
41 : */
42 : int is_bad;
43 :
44 : /**
45 : * If that we have recovered may be not updated data,
46 : * an old version, or just garbage.
47 : *
48 : * Essentially, it means that we are not sure what we have recovered
49 : * is really correct. It's just our best guess.
50 : *
51 : * These "recovered" block are also written to the disk if the block is marked as ::is_bad.
52 : * But these files are marked also as FILE_IS_DAMAGED, and then renamed to .unrecoverable.
53 : *
54 : * Note that this could happen only for CHG blocks.
55 : */
56 : int is_outofdate;
57 :
58 : unsigned index; /**< Index of the failed block. */
59 : struct snapraid_block* block; /**< The failed block */
60 : struct snapraid_disk* disk; /**< The failed disk. */
61 : struct snapraid_file* file; /**< The failed file. 0 for DELETED block. */
62 : block_off_t file_pos; /**< Offset inside the file */
63 : struct snapraid_handle* handle; /**< The handle containing the failed block, or 0 for a DELETED block */
64 : };
65 :
66 : /**
67 : * Check if a block hash matches the specified buffer.
68 : * Return ==0 if equal
69 : */
70 468990 : static int blockcmp(struct snapraid_state* state, int rehash, struct snapraid_block* block, unsigned pos_size, unsigned char* buffer, unsigned char* buffer_zero)
71 : {
72 : unsigned char hash[HASH_SIZE];
73 :
74 : /* now compute the hash of the valid part */
75 468990 : if (rehash) {
76 0 : memhash(state->prevhash, state->prevhashseed, hash, buffer, pos_size);
77 : } else {
78 468990 : memhash(state->hash, state->hashseed, hash, buffer, pos_size);
79 : }
80 :
81 : /* compare the hash */
82 468990 : if (memcmp(hash, block->hash, HASH_SIZE) != 0) {
83 13884 : return -1;
84 : }
85 :
86 : /* compare to the end of the block */
87 455106 : if (pos_size < state->block_size) {
88 187996 : if (memcmp(buffer + pos_size, buffer_zero + pos_size, state->block_size - pos_size) != 0) {
89 21 : return -1;
90 : }
91 : }
92 :
93 455085 : return 0;
94 : }
95 :
96 : /**
97 : * Check if the hash of all the failed block we are expecting to recover are now matching.
98 : */
99 211180 : static int is_hash_matching(struct snapraid_state* state, int rehash, unsigned diskmax, struct failed_struct* failed, unsigned* failed_map, unsigned failed_count, void** buffer, void* buffer_zero)
100 : {
101 : unsigned j;
102 : int hash_checked;
103 :
104 211180 : hash_checked = 0; /* keep track if we check at least one block */
105 :
106 : /* check if the recovered blocks are OK */
107 670787 : for (j = 0; j < failed_count; ++j) {
108 : /* if we are expected to recover this block */
109 473512 : if (!failed[failed_map[j]].is_outofdate
110 : /* if the block has a hash to check */
111 468990 : && block_has_updated_hash(failed[failed_map[j]].block)
112 : ) {
113 : /* if a hash doesn't match, fail the check */
114 468990 : unsigned pos_size = file_block_size(failed[failed_map[j]].file, failed[failed_map[j]].file_pos, state->block_size);
115 468990 : if (blockcmp(state, rehash, failed[failed_map[j]].block, pos_size, buffer[failed[failed_map[j]].index], buffer_zero) != 0) {
116 13905 : log_tag("hash_error: Hash mismatch on entry %u\n", failed_map[j]);
117 13905 : return 0;
118 : }
119 :
120 455085 : hash_checked = 1;
121 : }
122 : }
123 :
124 : /* if nothing checked, we reject it */
125 : /* note that we are excluding this case at upper level */
126 : /* but checking again doesn't hurt */
127 197275 : if (!hash_checked) {
128 : /* LCOV_EXCL_START */
129 : return 0;
130 : /* LCOV_EXCL_STOP */
131 : }
132 :
133 : /* if we checked something, and no block failed the check */
134 : /* recompute all the redundancy information */
135 197275 : raid_gen(diskmax, state->level, state->block_size, buffer);
136 197275 : return 1;
137 : }
138 :
139 : /**
140 : * Check if specified parity is now matching with a recomputed one.
141 : */
142 8 : static int is_parity_matching(struct snapraid_state* state, unsigned diskmax, unsigned i, void** buffer, void** buffer_recov)
143 : {
144 : /* recompute parity, note that we don't need parity over i */
145 8 : raid_gen(diskmax, i + 1, state->block_size, buffer);
146 :
147 : /* if the recovered parity block matches */
148 8 : if (memcmp(buffer[diskmax + i], buffer_recov[i], state->block_size) == 0) {
149 : /* recompute all the redundancy information */
150 8 : raid_gen(diskmax, state->level, state->block_size, buffer);
151 8 : return 1;
152 : }
153 :
154 0 : return 0;
155 : }
156 :
157 : /**
158 : * Repair errors.
159 : * Return <0 if failure for missing strategy, >0 if data is wrong and we cannot rebuild correctly, 0 on success.
160 : * If success, the parity are computed in the buffer variable.
161 : */
162 253884 : static int repair_step(struct snapraid_state* state, int rehash, unsigned pos, unsigned diskmax, struct failed_struct* failed, unsigned* failed_map, unsigned failed_count, void** buffer, void** buffer_recov, void* buffer_zero)
163 : {
164 : unsigned i, n;
165 : int error;
166 : int has_hash;
167 : int id[LEV_MAX];
168 : int ip[LEV_MAX];
169 :
170 : /* no fix required, already checked at higher level, but just to be sure */
171 253884 : if (failed_count == 0) {
172 : /* LCOV_EXCL_START */
173 : /* recompute only the parity */
174 : raid_gen(diskmax, state->level, state->block_size, buffer);
175 : return 0;
176 : /* LCOV_EXCL_STOP */
177 : }
178 :
179 253884 : n = state->level;
180 253884 : error = 0;
181 :
182 : /* setup vector of failed disk indexes */
183 893186 : for (i = 0; i < failed_count; ++i)
184 639302 : id[i] = failed[failed_map[i]].index;
185 :
186 : /* check if there is at least a failed block that can be checked for correctness using the hash */
187 : /* if there isn't, we have to sacrifice a parity block to check that the result is correct */
188 253884 : has_hash = 0;
189 893186 : for (i = 0; i < failed_count; ++i) {
190 : /* if we are expected to recover this block */
191 639302 : if (!failed[failed_map[i]].is_outofdate
192 : /* if the block has a hash to check */
193 630258 : && block_has_updated_hash(failed[failed_map[i]].block)
194 : )
195 630250 : has_hash = 1;
196 : }
197 :
198 : /* if we don't have a hash, but we have an extra parity */
199 : /* (strictly-less failures than number of parities) */
200 253884 : if (!has_hash && failed_count < n) {
201 : /* number of parity to use, one more to check the recovering */
202 8 : unsigned r = failed_count + 1;
203 :
204 : /* all combinations (r of n) parities */
205 8 : combination_first(r, n, ip);
206 : do {
207 : /* if a parity is missing, do nothing */
208 24 : for (i = 0; i < r; ++i) {
209 16 : if (buffer_recov[ip[i]] == 0)
210 0 : break;
211 : }
212 8 : if (i != r)
213 0 : continue;
214 :
215 : /* copy the parities to use, one less because the last is used for checking */
216 16 : for (i = 0; i < r - 1; ++i)
217 8 : memcpy(buffer[diskmax + ip[i]], buffer_recov[ip[i]], state->block_size);
218 :
219 : /* recover using one less parity, the ip[r-1] one */
220 8 : raid_data(r - 1, id, ip, diskmax, state->block_size, buffer);
221 :
222 : /* use the remaining ip[r-1] parity to check the result */
223 8 : if (is_parity_matching(state, diskmax, ip[r - 1], buffer, buffer_recov))
224 8 : return 0;
225 :
226 : /* log */
227 0 : log_tag("parity_error:%u:", pos);
228 0 : for (i = 0; i < r; ++i) {
229 0 : if (i != 0)
230 0 : log_tag("/");
231 0 : log_tag("%s", lev_config_name(ip[i]));
232 : }
233 0 : log_tag(":parity: Parity mismatch\n");
234 0 : ++error;
235 0 : } while (combination_next(r, n, ip));
236 : }
237 :
238 : /* if we have a hash, and enough parities */
239 : /* (less-or-equal failures than number of parities) */
240 253876 : if (has_hash && failed_count <= n) {
241 : /* number of parities to use equal at the number of failures */
242 206432 : unsigned r = failed_count;
243 :
244 : /* all combinations (r of n) parities */
245 206432 : combination_first(r, n, ip);
246 : do {
247 : /* if a parity is missing, do nothing */
248 689341 : for (i = 0; i < r; ++i) {
249 478161 : if (buffer_recov[ip[i]] == 0)
250 4649 : break;
251 : }
252 215829 : if (i != r)
253 4649 : continue;
254 :
255 : /* copy the parities to use */
256 684692 : for (i = 0; i < r; ++i)
257 473512 : memcpy(buffer[diskmax + ip[i]], buffer_recov[ip[i]], state->block_size);
258 :
259 : /* recover */
260 211180 : raid_data(r, id, ip, diskmax, state->block_size, buffer);
261 :
262 : /* use the hash to check the result */
263 211180 : if (is_hash_matching(state, rehash, diskmax, failed, failed_map, failed_count, buffer, buffer_zero))
264 197275 : return 0;
265 :
266 : /* log */
267 13905 : log_tag("parity_error:%u:", pos);
268 27810 : for (i = 0; i < r; ++i) {
269 13905 : if (i != 0)
270 0 : log_tag("/");
271 13905 : log_tag("%s", lev_config_name(ip[i]));
272 : }
273 13905 : log_tag(":hash: Hash mismatch\n");
274 13905 : ++error;
275 37108 : } while (combination_next(r, n, ip));
276 : }
277 :
278 : /* return the number of failed attempts, or -1 if no strategy */
279 56601 : if (error)
280 9157 : return error;
281 :
282 47444 : log_tag("strategy_error:%u: No strategy to recover from %u failures with %u parity %s hash\n",
283 : pos, failed_count, n, has_hash ? "with" : "without");
284 47444 : return -1;
285 : }
286 :
287 870992 : static int repair(struct snapraid_state* state, int rehash, unsigned pos, unsigned diskmax, struct failed_struct* failed, unsigned* failed_map, unsigned failed_count, void** buffer, void** buffer_recov, void* buffer_zero)
288 : {
289 : int ret;
290 : int error;
291 : unsigned j;
292 : int n;
293 : int something_to_recover;
294 : int something_unsynced;
295 :
296 870992 : error = 0;
297 :
298 : /* if nothing failed, just recompute the parity */
299 870992 : if (failed_count == 0) {
300 581962 : raid_gen(diskmax, state->level, state->block_size, buffer);
301 581962 : return 0;
302 : }
303 :
304 : /* logs the status */
305 1018351 : for (j = 0; j < failed_count; ++j) {
306 : const char* desc;
307 : const char* hash;
308 : const char* data;
309 729321 : struct snapraid_block* block = failed[j].block;
310 729321 : unsigned block_state = block_state_get(block);
311 :
312 729321 : switch (block_state) {
313 18454 : case BLOCK_STATE_DELETED : desc = "delete"; break;
314 13851 : case BLOCK_STATE_CHG : desc = "change"; break;
315 62282 : case BLOCK_STATE_REP : desc = "replace"; break;
316 634734 : case BLOCK_STATE_BLK : desc = "block"; break;
317 : /* LCOV_EXCL_START */
318 : default : desc = "unknown"; break;
319 : /* LCOV_EXCL_STOP */
320 : }
321 :
322 729321 : if (hash_is_invalid(block->hash)) {
323 13843 : hash = "lost";
324 715478 : } else if (hash_is_zero(block->hash)) {
325 8 : hash = "zero";
326 : } else {
327 715470 : hash = "known";
328 : }
329 :
330 729321 : if (failed[j].is_bad)
331 643596 : data = "bad";
332 : else
333 85725 : data = "good";
334 :
335 729321 : if (failed[j].file) {
336 710867 : struct snapraid_disk* disk = failed[j].disk;
337 710867 : struct snapraid_file* file = failed[j].file;
338 710867 : block_off_t file_pos = failed[j].file_pos;
339 :
340 710867 : log_tag("entry:%u:%s:%s:%s:%s:%s:%u:\n", j, desc, hash, data, disk->name, esc(file->sub), file_pos);
341 : } else {
342 18454 : log_tag("entry:%u:%s:%s:%s:\n", j, desc, hash, data);
343 : }
344 : }
345 :
346 : /* Here we have to try two different strategies to recover, because in case the 'sync' */
347 : /* process is aborted, we don't know if the parity data is really updated just like after 'sync', */
348 : /* or if it still represents the state before the 'sync'. */
349 :
350 : /* Note that if the 'sync' ends normally, we don't have any DELETED, REP and CHG blocks */
351 : /* and the two strategies are identical */
352 :
353 : /* As first, we assume that the parity IS updated for the current state */
354 : /* and that we are going to recover the state after the last 'sync'. */
355 : /* In this case, parity contains info from BLK, REP and CHG blocks, */
356 : /* but not for DELETED. */
357 : /* We need to put in the recovering process only the bad blocks, because all the */
358 : /* others already contains the correct data read from disk, and the parity is correctly computed for them. */
359 : /* We are interested to recover BLK, REP and CHG blocks if they are marked as bad, */
360 : /* but we are not interested in DELETED ones. */
361 :
362 289030 : n = 0;
363 289030 : something_to_recover = 0; /* keep track if there is at least one block to fix */
364 1018351 : for (j = 0; j < failed_count; ++j) {
365 729321 : if (failed[j].is_bad) {
366 643596 : unsigned block_state = block_state_get(failed[j].block);
367 :
368 643596 : assert(block_state != BLOCK_STATE_DELETED); /* we cannot have bad DELETED blocks */
369 :
370 : /* if we have the hash for it */
371 643596 : if ((block_state == BLOCK_STATE_BLK || block_state == BLOCK_STATE_REP)
372 : /* try to fetch the block using the known hash */
373 643588 : && (state_import_fetch(state, rehash, failed[j].block, buffer[failed[j].index]) == 0
374 639116 : || state_search_fetch(state, rehash, failed[j].file, failed[j].file_pos, failed[j].block, buffer[failed[j].index]) == 0)
375 : ) {
376 : /* we already have corrected it! */
377 22382 : log_tag("hash_import: Fixed entry %u\n", j);
378 : } else {
379 : /* otherwise try to recover it */
380 621214 : failed_map[n] = j;
381 621214 : ++n;
382 :
383 : /* we have something to try to recover */
384 621214 : something_to_recover = 1;
385 : }
386 : }
387 : }
388 :
389 : /* if nothing to fix */
390 289030 : if (!something_to_recover) {
391 44190 : log_tag("recover_sync:%u:%u: Skipped for already recovered\n", pos, n);
392 :
393 : /* recompute only the parity */
394 44190 : raid_gen(diskmax, state->level, state->block_size, buffer);
395 44190 : return 0;
396 : }
397 :
398 244840 : ret = repair_step(state, rehash, pos, diskmax, failed, failed_map, n, buffer, buffer_recov, buffer_zero);
399 244840 : if (ret == 0) {
400 : /* reprocess the CHG blocks, for which we don't have a hash to check */
401 : /* if they were BAD we have to use some heuristics to ensure that we have recovered */
402 : /* the state after the sync. If unsure, we assume the worst case */
403 :
404 669725 : for (j = 0; j < failed_count; ++j) {
405 : /* we take care only of BAD blocks we have to write back */
406 476964 : if (failed[j].is_bad) {
407 463492 : unsigned block_state = block_state_get(failed[j].block);
408 :
409 : /* BLK and REP blocks are always OK, because at this point */
410 : /* we have already checked their hash */
411 463492 : if (block_state != BLOCK_STATE_CHG) {
412 463484 : assert(block_state == BLOCK_STATE_BLK || block_state == BLOCK_STATE_REP);
413 463484 : continue;
414 : }
415 :
416 : /* for CHG blocks we have to 'guess' if they are correct or not */
417 :
418 : /* if the hash is invalid we cannot check the result */
419 : /* this could happen if we have lost this information */
420 : /* after an aborted sync */
421 8 : if (hash_is_invalid(failed[j].block->hash)) {
422 : /* it may contain garbage */
423 0 : failed[j].is_outofdate = 1;
424 :
425 0 : log_tag("hash_unknown: Unknown hash on entry %u\n", j);
426 8 : } else if (hash_is_zero(failed[j].block->hash)) {
427 : /* if the block is not filled with 0, we are sure to have */
428 : /* restored it to the state after the 'sync' */
429 : /* instead, if the block is filled with 0, it could be either that the */
430 : /* block after the sync is really filled by 0, or that */
431 : /* we restored the block before the 'sync'. */
432 8 : if (memcmp(buffer[failed[j].index], buffer_zero, state->block_size) == 0) {
433 : /* it may contain garbage */
434 8 : failed[j].is_outofdate = 1;
435 :
436 8 : log_tag("hash_unknown: Maybe old zero on entry %u\n", j);
437 : }
438 : } else {
439 : /* if the hash is different than the previous one, we are sure to have */
440 : /* restored it to the state after the 'sync' */
441 : /* instead, if the hash matches, it could be either that the */
442 : /* block after the sync has this hash, or that */
443 : /* we restored the block before the 'sync'. */
444 0 : unsigned pos_size = file_block_size(failed[j].file, failed[j].file_pos, state->block_size);
445 0 : if (blockcmp(state, rehash, failed[j].block, pos_size, buffer[failed[j].index], buffer_zero) == 0) {
446 : /* it may contain garbage */
447 0 : failed[j].is_outofdate = 1;
448 :
449 0 : log_tag("hash_unknown: Maybe old data on entry %u\n", j);
450 : }
451 : }
452 : }
453 : }
454 :
455 192761 : return 0;
456 : }
457 52079 : if (ret > 0)
458 9157 : error += ret;
459 :
460 52079 : if (ret < 0)
461 42922 : log_tag("recover_sync:%u:%u: Failed with no attempts\n", pos, n);
462 : else
463 9157 : log_tag("recover_sync:%u:%u: Failed with %d attempts\n", pos, n, ret);
464 :
465 : /* Now assume that the parity IS NOT updated at the current state, */
466 : /* but still represent the state before the last 'sync' process. */
467 : /* In this case, parity contains info from BLK, REP (old version), CHG (old version) and DELETED blocks, */
468 : /* but not for REP (new version) and CHG (new version). */
469 : /* We are interested to recover BLK ones marked as bad, */
470 : /* but we are not interested to recover CHG (new version) and REP (new version) blocks, */
471 : /* even if marked as bad, because we don't have parity for them and it's just impossible, */
472 : /* and we are not interested to recover DELETED ones. */
473 52079 : n = 0;
474 52079 : something_to_recover = 0; /* keep track if there is at least one block to fix */
475 52079 : something_unsynced = 0; /* keep track if we have some unsynced info to process */
476 231766 : for (j = 0; j < failed_count; ++j) {
477 179687 : unsigned block_state = block_state_get(failed[j].block);
478 :
479 179687 : if (block_state == BLOCK_STATE_DELETED
480 170643 : || block_state == BLOCK_STATE_CHG
481 170643 : || block_state == BLOCK_STATE_REP
482 : ) {
483 : /* If the block is CHG, REP or DELETED, we don't have the original content of block, */
484 : /* and we must try to recover it. */
485 : /* This apply to CHG and REP blocks even if they are not marked bad, */
486 : /* because the parity is computed with old content, and not with the new one. */
487 : /* Note that this recovering is done just to make possible to recover any other BLK one, */
488 : /* we are not really interested in DELETED, CHG (old version) and REP (old version). */
489 9044 : something_unsynced = 1;
490 :
491 9044 : if (block_state == BLOCK_STATE_CHG
492 0 : && hash_is_zero(failed[j].block->hash)
493 : ) {
494 : /* If the block was a ZERO block, restore it to the original 0 as before the 'sync' */
495 : /* We do this to just allow recovering of other BLK ones */
496 :
497 0 : memset(buffer[failed[j].index], 0, state->block_size);
498 : /* note that from now the buffer is definitively lost */
499 : /* we can do this only because it's the last retry of recovering */
500 :
501 : /* try to fetch the old block using the old hash for CHG and DELETED blocks */
502 9044 : } else if ((block_state == BLOCK_STATE_CHG || block_state == BLOCK_STATE_DELETED)
503 9044 : && hash_is_real(failed[j].block->hash)
504 9044 : && state_import_fetch(state, rehash, failed[j].block, buffer[failed[j].index]) == 0) {
505 :
506 : /* note that from now the buffer is definitively lost */
507 : /* we can do this only because it's the last retry of recovering */
508 : } else {
509 : /* otherwise try to recover it */
510 9044 : failed_map[n] = j;
511 9044 : ++n;
512 :
513 : /* note that we don't set something_to_recover, because we are */
514 : /* not really interested to recover *only* old blocks. */
515 : }
516 :
517 : /* avoid to use the hash of this block to verify the recovering */
518 : /* this applies to REP blocks because we are going to recover the old state */
519 : /* and the REP hash represent the new one */
520 : /* it also applies to CHG and DELETE blocks because we want to have */
521 : /* a successful recovering only if a BLK one is matching */
522 9044 : failed[j].is_outofdate = 1;
523 170643 : } else if (failed[j].is_bad) {
524 : /* If the block is bad we don't know its content, and we try to recover it */
525 : /* At this point, we can have only BLK ones */
526 :
527 170643 : assert(block_state == BLOCK_STATE_BLK);
528 :
529 : /* we have something we are interested to recover */
530 170643 : something_to_recover = 1;
531 :
532 : /* we try to recover it */
533 170643 : failed_map[n] = j;
534 170643 : ++n;
535 : }
536 : }
537 :
538 : /* if nothing to fix, we just don't try */
539 : /* if nothing unsynced we also don't retry, because it's the same try as before */
540 52079 : if (something_to_recover && something_unsynced) {
541 9044 : ret = repair_step(state, rehash, pos, diskmax, failed, failed_map, n, buffer, buffer_recov, buffer_zero);
542 9044 : if (ret == 0) {
543 : /* reprocess the REP and CHG blocks, for which we have recovered and old state */
544 : /* that we don't want to save into disk */
545 : /* we have already marked them, but we redo it for logging */
546 :
547 13566 : for (j = 0; j < failed_count; ++j) {
548 : /* we take care only of BAD blocks we have to write back */
549 9044 : if (failed[j].is_bad) {
550 4522 : unsigned block_state = block_state_get(failed[j].block);
551 :
552 4522 : if (block_state == BLOCK_STATE_CHG
553 4522 : || block_state == BLOCK_STATE_REP
554 : ) {
555 : /* mark that we have restored an old state */
556 : /* and we don't want to write it to the disk */
557 0 : failed[j].is_outofdate = 1;
558 :
559 0 : log_tag("hash_unknown: Surely old data on entry %u\n", j);
560 : }
561 : }
562 : }
563 :
564 4522 : return 0;
565 : }
566 4522 : if (ret > 0)
567 0 : error += ret;
568 :
569 9044 : if (ret < 0)
570 4522 : log_tag("recover_unsync:%u:%u: Failed with no attempts\n", pos, n);
571 : else
572 0 : log_tag("recover_unsync:%u:%u: Failed with %d attempts\n", pos, n, ret);
573 : } else {
574 43035 : log_tag("recover_unsync:%u:%u: Skipped for%s%s\n", pos, n,
575 : !something_to_recover ? " nothing to recover" : "",
576 : !something_unsynced ? " nothing unsynched" : ""
577 : );
578 : }
579 :
580 : /* return the number of failed attempts, or -1 if no strategy */
581 47557 : if (error)
582 4635 : return error;
583 : else
584 42922 : return -1;
585 : }
586 :
587 : /**
588 : * Post process all the files at the specified block index ::i.
589 : * For each file, if we are at the last block, closes it,
590 : * adjust the timestamp, and print the result.
591 : *
592 : * This works with the assumption to always process the whole files to
593 : * fix. This assumption is not always correct, and in such case we have to
594 : * skip the whole postprocessing. And example, is when fixing only bad blocks.
595 : */
596 935479 : static int file_post(struct snapraid_state* state, int fix, unsigned i, struct snapraid_handle* handle, unsigned diskmax)
597 : {
598 : unsigned j;
599 : int ret;
600 :
601 : /* if we are processing only bad blocks, we don't have to do any post-processing */
602 : /* as we don't have any guarantee to process the last block of the fixed files */
603 935479 : if (state->opt.badonly)
604 9374 : return 0;
605 :
606 : /* for all the files print the final status, and does the final time fix */
607 : /* we also ensure to close files after processing the last block */
608 6482735 : for (j = 0; j < diskmax; ++j) {
609 : struct snapraid_block* block;
610 : struct snapraid_disk* disk;
611 : struct snapraid_file* collide_file;
612 : struct snapraid_file* file;
613 : block_off_t file_pos;
614 : char path[PATH_MAX];
615 : uint64_t inode;
616 :
617 5556630 : disk = handle[j].disk;
618 5556630 : if (!disk) {
619 : /* if no disk, nothing to do */
620 3408433 : continue;
621 : }
622 :
623 5542569 : block = fs_par2block_get(disk, i);
624 5542569 : if (!block_has_file(block)) {
625 : /* if no file, nothing to do */
626 213403 : continue;
627 : }
628 :
629 5329166 : file = fs_par2file_get(disk, i, &file_pos);
630 5329166 : pathprint(path, sizeof(path), "%s%s", disk->dir, file->sub);
631 :
632 : /* if it isn't the last block in the file */
633 5329166 : if (!file_block_is_last(file, file_pos)) {
634 : /* nothing to do */
635 3166908 : continue;
636 : }
637 :
638 : /* if the file is excluded, we have nothing to adjust as the file is never written */
639 2162258 : if (file_flag_has(file, FILE_IS_EXCLUDED)
640 2065317 : || (state->opt.syncedonly && file_flag_has(file, FILE_IS_UNSYNCED))) {
641 : /* nothing to do, but close the file */
642 : goto close_and_continue;
643 : }
644 :
645 : /* finish the fix process if it's the last block of the files */
646 2065317 : if (fix) {
647 : /* mark that we finished with this file */
648 : /* to identify later any NOT finished ones */
649 757286 : file_flag_set(file, FILE_IS_FINISHED);
650 :
651 : /* if the file is damaged, meaning that a fix failed */
652 757286 : if (file_flag_has(file, FILE_IS_DAMAGED)) {
653 : /* rename it to .unrecoverable */
654 : char path_to[PATH_MAX];
655 :
656 63260 : pathprint(path_to, sizeof(path_to), "%s%s.unrecoverable", disk->dir, file->sub);
657 :
658 : /* ensure to close the file before renaming */
659 63260 : if (handle[j].file == file) {
660 63260 : ret = handle_close(&handle[j]);
661 63260 : if (ret != 0) {
662 : /* LCOV_EXCL_START */
663 : log_tag("error:%u:%s:%s: Close error. %s\n", i, disk->name, esc(file->sub), strerror(errno));
664 : log_fatal("DANGER! Unexpected close error in a data disk.\n");
665 : return -1;
666 : /* LCOV_EXCL_STOP */
667 : }
668 : }
669 :
670 63260 : ret = rename(path, path_to);
671 63260 : if (ret != 0) {
672 : /* LCOV_EXCL_START */
673 : log_fatal("Error renaming '%s' to '%s'. %s.\n", path, path_to, strerror(errno));
674 : log_fatal("WARNING! Without a working data disk, it isn't possible to fix errors on it.\n");
675 : return -1;
676 : /* LCOV_EXCL_STOP */
677 : }
678 :
679 63260 : log_tag("status:unrecoverable:%s:%s\n", disk->name, esc(file->sub));
680 63260 : msg_info("unrecoverable %s\n", path);
681 :
682 : /* and do not set the time if damaged */
683 63260 : goto close_and_continue;
684 : }
685 :
686 : /* if the file is not fixed, meaning that it is untouched */
687 694026 : if (!file_flag_has(file, FILE_IS_FIXED)) {
688 : /* nothing to do, but close the file */
689 574495 : goto close_and_continue;
690 : }
691 :
692 : /* if the file is closed or different than the one expected, reopen it */
693 : /* a different open file could happen when filtering for bad blocks */
694 119531 : if (handle[j].file != file) {
695 : /* close a potential different file */
696 0 : ret = handle_close(&handle[j]);
697 0 : if (ret != 0) {
698 : /* LCOV_EXCL_START */
699 : log_tag("error:%u:%s:%s: Close error. %s\n", i, disk->name, esc(handle[j].file->sub), strerror(errno));
700 : log_fatal("DANGER! Unexpected close error in a data disk.\n");
701 : return -1;
702 : /* LCOV_EXCL_STOP */
703 : }
704 :
705 : /* reopen it as readonly, as to set the mtime readonly access it's enough */
706 : /* we know that the file exists because it has the FILE_IS_FIXED tag */
707 0 : ret = handle_open(&handle[j], file, state->file_mode, log_error, 0);
708 0 : if (ret != 0) {
709 : /* LCOV_EXCL_START */
710 : log_tag("error:%u:%s:%s: Open error. %s\n", i, disk->name, esc(file->sub), strerror(errno));
711 : log_fatal("WARNING! Without a working data disk, it isn't possible to fix errors on it.\n");
712 : return -1;
713 : /* LCOV_EXCL_STOP */
714 : }
715 : }
716 :
717 119531 : log_tag("status:recovered:%s:%s\n", disk->name, esc(file->sub));
718 119531 : msg_info("recovered %s\n", path);
719 :
720 119531 : inode = handle[j].st.st_ino;
721 :
722 : /* search for the corresponding inode */
723 119531 : collide_file = tommy_hashdyn_search(&disk->inodeset, file_inode_compare_to_arg, &inode, file_inode_hash(inode));
724 :
725 : /* if the inode is already in the database and it refers at a different file name, */
726 : /* we can fix the file time ONLY if the time and size allow to differentiate */
727 : /* between the two files */
728 :
729 : /* for example, suppose we delete a bunch of files with all the same size and time, */
730 : /* when recreating them the inodes may be reused in a different order, */
731 : /* and at the next sync some files may have matching inode/size/time even if different name */
732 : /* not allowing sync to detect that the file is changed and not renamed */
733 119531 : if (!collide_file /* if not in the database, there is no collision */
734 36876 : || strcmp(collide_file->sub, file->sub) == 0 /* if the name is the same, it's the right collision */
735 33424 : || collide_file->size != file->size /* if the size is different, the collision is identified */
736 7 : || collide_file->mtime_sec != file->mtime_sec /* if the mtime is different, the collision is identified */
737 5 : || collide_file->mtime_nsec != file->mtime_nsec /* same for mtime_nsec */
738 : ) {
739 : /* set the original modification time */
740 119530 : ret = handle_utime(&handle[j]);
741 239060 : if (ret == -1) {
742 : /* LCOV_EXCL_START */
743 : /* mark the file as damaged */
744 : file_flag_set(file, FILE_IS_DAMAGED);
745 : log_fatal("WARNING! Without a working data disk, it isn't possible to fix errors on it.\n");
746 : return -1;
747 : /* LCOV_EXCL_STOP */
748 : }
749 : } else {
750 1 : log_tag("collision:%s:%s:%s: Not setting modification time to avoid inode collision\n", disk->name, esc(file->sub), esc(collide_file->sub));
751 : }
752 : } else {
753 : /* we are not fixing, but only checking */
754 : /* print just the final status */
755 1308031 : if (file_flag_has(file, FILE_IS_DAMAGED)) {
756 9029 : if (state->opt.auditonly) {
757 2748 : log_tag("status:damaged:%s:%s\n", disk->name, esc(file->sub));
758 2748 : msg_info("damaged %s\n", path);
759 : } else {
760 6281 : log_tag("status:unrecoverable:%s:%s\n", disk->name, esc(file->sub));
761 6281 : msg_info("unrecoverable %s\n", path);
762 : }
763 1299002 : } else if (file_flag_has(file, FILE_IS_FIXED)) {
764 76758 : log_tag("status:recoverable:%s:%s\n", disk->name, esc(file->sub));
765 76758 : msg_info("recoverable %s\n", path);
766 : } else {
767 1222244 : log_tag("status:correct:%s:%s\n", disk->name, esc(file->sub));
768 : /* we don't use msg_verbose() because it also goes into the log */
769 1222244 : if (msg_level >= MSG_VERBOSE)
770 23207 : msg_info("correct %s\n", path);
771 : }
772 : }
773 :
774 : close_and_continue:
775 : /* if the opened file is the correct one, close it */
776 : /* in case of excluded and fragmented files it's possible */
777 : /* that the opened file is not the current one */
778 2162258 : if (handle[j].file == file) {
779 : /* ensure to close the file just after finishing with it */
780 : /* to avoid to keep it open without any possible use */
781 1978786 : ret = handle_close(&handle[j]);
782 1978786 : if (ret != 0) {
783 : /* LCOV_EXCL_START */
784 : log_tag("error:%u:%s:%s: Close error. %s\n", i, disk->name, esc(file->sub), strerror(errno));
785 : log_fatal("DANGER! Unexpected close error in a data disk.\n");
786 : return -1;
787 : /* LCOV_EXCL_STOP */
788 : }
789 : }
790 : }
791 :
792 926105 : return 0;
793 : }
794 :
795 : /**
796 : * Check if we have to process the specified block index ::i.
797 : */
798 1870958 : static int block_is_enabled(struct snapraid_state* state, block_off_t i, struct snapraid_handle* handle, unsigned diskmax)
799 : {
800 : snapraid_info info;
801 : unsigned j;
802 : unsigned l;
803 :
804 : /* get block specific info */
805 1870958 : info = info_get(&state->infoarr, i);
806 :
807 : /* if we filter for only bad blocks */
808 1870958 : if (state->opt.badonly) {
809 : /* skip if this is not bad */
810 18748 : if (!info_get_bad(info))
811 15204 : return 0;
812 : }
813 :
814 : /* for each parity */
815 2125818 : for (l = 0; l < state->level; ++l) {
816 2042438 : if (!state->parity[l].is_excluded) {
817 1772374 : return 1;
818 : }
819 : }
820 :
821 : /* for each disk */
822 386210 : for (j = 0; j < diskmax; ++j) {
823 : struct snapraid_block* block;
824 :
825 : /* if no disk, nothing to check */
826 350990 : if (!handle[j].disk)
827 0 : continue;
828 :
829 350990 : block = fs_par2block_get(handle[j].disk, i);
830 :
831 : /* try to recover all files, even the ones without hash */
832 : /* because in some cases we can recover also them */
833 350990 : if (block_has_file(block)) {
834 344328 : struct snapraid_file* file = fs_par2file_get(handle[j].disk, i, 0);
835 344328 : if (!file_flag_has(file, FILE_IS_EXCLUDED)) { /* only if the file is not filtered out */
836 48160 : return 1;
837 : }
838 : }
839 : }
840 :
841 35220 : return 0;
842 : }
843 :
844 118 : static int state_check_process(struct snapraid_state* state, int fix, struct snapraid_parity_handle** parity, block_off_t blockstart, block_off_t blockmax)
845 : {
846 : struct snapraid_handle* handle;
847 : unsigned diskmax;
848 : block_off_t i;
849 : unsigned j;
850 : void* buffer_alloc;
851 : void** buffer;
852 : unsigned buffermax;
853 : int ret;
854 : data_off_t countsize;
855 : block_off_t countpos;
856 : block_off_t countmax;
857 : unsigned error;
858 : unsigned unrecoverable_error;
859 : unsigned recovered_error;
860 : struct failed_struct* failed;
861 : unsigned* failed_map;
862 : unsigned l;
863 :
864 118 : handle = handle_map(state, &diskmax);
865 :
866 : /* we need 1 * data + 2 * parity + 1 * zero */
867 118 : buffermax = diskmax + 2 * state->level + 1;
868 :
869 118 : buffer = malloc_nofail_vector_align(diskmax, buffermax, state->block_size, &buffer_alloc);
870 118 : if (!state->opt.skip_self)
871 0 : mtest_vector(buffermax, state->block_size, buffer);
872 :
873 : /* fill up the zero buffer */
874 118 : memset(buffer[buffermax - 1], 0, state->block_size);
875 118 : raid_zero(buffer[buffermax - 1]);
876 :
877 118 : failed = malloc_nofail(diskmax * sizeof(struct failed_struct));
878 118 : failed_map = malloc_nofail(diskmax * sizeof(unsigned));
879 :
880 118 : error = 0;
881 118 : unrecoverable_error = 0;
882 118 : recovered_error = 0;
883 :
884 : /* first count the number of blocks to process */
885 118 : countmax = 0;
886 935597 : for (i = blockstart; i < blockmax; ++i) {
887 935479 : if (!block_is_enabled(state, i, handle, diskmax))
888 25212 : continue;
889 910267 : ++countmax;
890 : }
891 :
892 : /* check all the blocks in files */
893 118 : countsize = 0;
894 118 : countpos = 0;
895 118 : state_progress_begin(state, blockstart, blockmax, countmax);
896 935597 : for (i = blockstart; i < blockmax; ++i) {
897 : unsigned failed_count;
898 : int valid_parity;
899 : snapraid_info info;
900 : int rehash;
901 :
902 935479 : if (!block_is_enabled(state, i, handle, diskmax)) {
903 : /* post process the files */
904 25212 : ret = file_post(state, fix, i, handle, diskmax);
905 25212 : if (ret == -1) {
906 0 : log_fatal("Stopping at block %u\n", i);
907 0 : ++unrecoverable_error;
908 0 : goto bail;
909 : }
910 :
911 : /* and now continue with the next block */
912 25212 : continue;
913 : }
914 :
915 : /* If we have valid parity, and it makes sense to check its content. */
916 : /* If we already know that the parity is invalid, we just read the file */
917 : /* but we don't report parity errors */
918 : /* Note that with auditonly, we anyway skip the full parity check, */
919 : /* because we also don't read it at all */
920 910267 : valid_parity = 1;
921 :
922 : /* keep track of the number of failed blocks */
923 910267 : failed_count = 0;
924 :
925 : /* get block specific info */
926 910267 : info = info_get(&state->infoarr, i);
927 :
928 : /* if we have to use the old hash */
929 910267 : rehash = info_get_rehash(info);
930 :
931 : /* for each disk, process the block */
932 6371869 : for (j = 0; j < diskmax; ++j) {
933 : int read_size;
934 : unsigned char hash[HASH_SIZE];
935 : struct snapraid_disk* disk;
936 : struct snapraid_block* block;
937 : struct snapraid_file* file;
938 : block_off_t file_pos;
939 : unsigned block_state;
940 :
941 : /* if the disk position is not used */
942 5461602 : disk = handle[j].disk;
943 5461602 : if (!disk) {
944 : /* use an empty block */
945 14061 : memset(buffer[j], 0, state->block_size);
946 955323 : continue;
947 : }
948 :
949 : /* if the disk block is not used */
950 5447541 : block = fs_par2block_get(disk, i);
951 5447541 : if (block == BLOCK_EMPTY) {
952 : /* use an empty block */
953 192064 : memset(buffer[j], 0, state->block_size);
954 192064 : continue;
955 : }
956 :
957 : /* get the state of the block */
958 5255477 : block_state = block_state_get(block);
959 :
960 : /* if the parity is not valid */
961 5255477 : if (block_has_invalid_parity(block)) {
962 : /* mark the parity as invalid, and don't try to check/fix it */
963 : /* because it will be recomputed at the next sync */
964 94587 : valid_parity = 0;
965 : /* follow */
966 : }
967 :
968 : /* if the block is DELETED */
969 5255477 : if (block_state == BLOCK_STATE_DELETED) {
970 : /* use an empty block */
971 18454 : memset(buffer[j], 0, state->block_size);
972 :
973 : /* store it in the failed set, because potentially */
974 : /* the parity may be still computed with the previous content */
975 18454 : failed[failed_count].is_bad = 0; /* note that is_bad==0 <=> file==0 */
976 18454 : failed[failed_count].is_outofdate = 0;
977 18454 : failed[failed_count].index = j;
978 18454 : failed[failed_count].block = block;
979 18454 : failed[failed_count].disk = disk;
980 18454 : failed[failed_count].file = 0;
981 18454 : failed[failed_count].file_pos = 0;
982 18454 : failed[failed_count].handle = 0;
983 18454 : ++failed_count;
984 18454 : continue;
985 : }
986 :
987 : /* get the file of this block */
988 5237023 : file = fs_par2file_get(disk, i, &file_pos);
989 :
990 : /* if we are only hashing, we can skip excluded files and don't even read them */
991 5237023 : if (state->opt.auditonly && file_flag_has(file, FILE_IS_EXCLUDED)) {
992 : /* use an empty block */
993 : /* in true, this is unnecessary, because we are not checking any parity */
994 : /* but we keep it for completeness */
995 0 : memset(buffer[j], 0, state->block_size);
996 0 : continue;
997 : }
998 :
999 : /* if the file is closed or different than the current one */
1000 5237023 : if (handle[j].file == 0 || handle[j].file != file) {
1001 : /* close the old one, if any */
1002 2259926 : ret = handle_close(&handle[j]);
1003 2259926 : if (ret == -1) {
1004 : /* LCOV_EXCL_START */
1005 : log_tag("error:%u:%s:%s: Close error. %s\n", i, disk->name, esc(handle[j].file->sub), strerror(errno));
1006 : log_fatal("DANGER! Unexpected close error in a data disk.\n");
1007 : log_fatal("Stopping at block %u\n", i);
1008 : ++unrecoverable_error;
1009 : goto bail;
1010 : /* LCOV_EXCL_STOP */
1011 : }
1012 :
1013 : /* if fixing, and the file is not excluded, we must open for writing */
1014 2259926 : if (fix && !file_flag_has(file, FILE_IS_EXCLUDED)) {
1015 : /* if fixing, create the file, open for writing and resize if required */
1016 771365 : ret = handle_create(&handle[j], file, state->file_mode);
1017 771365 : if (ret == -1) {
1018 : /* LCOV_EXCL_START */
1019 : if (errno == EACCES) {
1020 : log_fatal("WARNING! Please give write permission to the file.\n");
1021 : } else {
1022 : log_fatal("DANGER! Without a working data disk, it isn't possible to fix errors on it.\n");
1023 : }
1024 : log_fatal("Stopping at block %u\n", i);
1025 : ++unrecoverable_error;
1026 : goto bail;
1027 : /* LCOV_EXCL_STOP */
1028 : }
1029 :
1030 : /* check if the file was just created */
1031 1542730 : if (handle[j].created != 0) {
1032 : /* if fragmented, it may be reopened, so remember that the file */
1033 : /* was originally missing */
1034 113054 : file_flag_set(file, FILE_IS_CREATED);
1035 : }
1036 : } else {
1037 : /* open the file only for reading */
1038 1488561 : if (!file_flag_has(file, FILE_IS_MISSING))
1039 1372929 : ret = handle_open(&handle[j], file, state->file_mode,
1040 1372929 : log_error, state->opt.expected_missing ? log_expected : 0);
1041 : else
1042 115632 : ret = -1; /* if the file is missing, we cannot open it */
1043 1488561 : if (ret == -1) {
1044 : /* save the failed block for the check/fix */
1045 194158 : failed[failed_count].is_bad = 1;
1046 194158 : failed[failed_count].is_outofdate = 0;
1047 194158 : failed[failed_count].index = j;
1048 194158 : failed[failed_count].block = block;
1049 194158 : failed[failed_count].disk = disk;
1050 194158 : failed[failed_count].file = file;
1051 194158 : failed[failed_count].file_pos = file_pos;
1052 194158 : failed[failed_count].handle = &handle[j];
1053 194158 : ++failed_count;
1054 :
1055 194158 : log_tag("error:%u:%s:%s: Open error at position %u\n", i, disk->name, esc(file->sub), file_pos);
1056 194158 : ++error;
1057 :
1058 : /* mark the file as missing, to avoid to retry to open it again */
1059 : /* note that this can be done only if we are not fixing it */
1060 : /* otherwise, it could be recreated */
1061 194158 : file_flag_set(file, FILE_IS_MISSING);
1062 194158 : continue;
1063 : }
1064 : }
1065 :
1066 : /* if it's the first open, and not excluded */
1067 2065768 : if (!file_flag_has(file, FILE_IS_OPENED)
1068 2050390 : && !file_flag_has(file, FILE_IS_EXCLUDED)) {
1069 :
1070 : /* check if the file is changed */
1071 1998901 : if (handle[j].st.st_size != file->size
1072 1806252 : || handle[j].st.st_mtime != file->mtime_sec
1073 1781912 : || STAT_NSEC(&handle[j].st) != file->mtime_nsec
1074 : /* don't check the inode to support file-system without persistent inodes */
1075 : ) {
1076 : /* report that the file is not synced */
1077 216989 : file_flag_set(file, FILE_IS_UNSYNCED);
1078 : }
1079 : }
1080 :
1081 : /* if it's the first open, and not excluded and larger */
1082 2065768 : if (!file_flag_has(file, FILE_IS_OPENED)
1083 2050390 : && !file_flag_has(file, FILE_IS_EXCLUDED)
1084 1998901 : && !(state->opt.syncedonly && file_flag_has(file, FILE_IS_UNSYNCED))
1085 1998901 : && handle[j].st.st_size > file->size
1086 : ) {
1087 8464 : log_error("File '%s' is larger than expected.\n", handle[j].path);
1088 8464 : log_tag("error:%u:%s:%s: Size error\n", i, disk->name, esc(file->sub));
1089 8464 : ++error;
1090 :
1091 8464 : if (fix) {
1092 3129 : ret = handle_truncate(&handle[j], file);
1093 3129 : if (ret == -1) {
1094 : /* LCOV_EXCL_START */
1095 : log_fatal("DANGER! Unexpected truncate error in a data disk, it isn't possible to fix.\n");
1096 : log_fatal("Stopping at block %u\n", i);
1097 : ++unrecoverable_error;
1098 : goto bail;
1099 : /* LCOV_EXCL_STOP */
1100 : }
1101 :
1102 3129 : log_tag("fixed:%u:%s:%s: Fixed size\n", i, disk->name, esc(file->sub));
1103 3129 : ++recovered_error;
1104 : }
1105 : }
1106 :
1107 : /* mark the file as opened at least one time */
1108 : /* this is used to avoid to check the unsynced and size */
1109 : /* more than one time, in case the file is reopened later */
1110 2065768 : file_flag_set(file, FILE_IS_OPENED);
1111 : }
1112 :
1113 : /* read from the file */
1114 5042865 : read_size = handle_read(&handle[j], file_pos, buffer[j], state->block_size,
1115 5042865 : log_error, state->opt.expected_missing ? log_expected : 0);
1116 5042865 : if (read_size == -1) {
1117 : /* save the failed block for the check/fix */
1118 442280 : failed[failed_count].is_bad = 1; /* it's bad because we cannot read it */
1119 442280 : failed[failed_count].is_outofdate = 0;
1120 442280 : failed[failed_count].index = j;
1121 442280 : failed[failed_count].block = block;
1122 442280 : failed[failed_count].disk = disk;
1123 442280 : failed[failed_count].file = file;
1124 442280 : failed[failed_count].file_pos = file_pos;
1125 442280 : failed[failed_count].handle = &handle[j];
1126 442280 : ++failed_count;
1127 :
1128 442280 : log_tag("error:%u:%s:%s: Read error at position %u\n", i, disk->name, esc(file->sub), file_pos);
1129 442280 : ++error;
1130 442280 : continue;
1131 : }
1132 :
1133 4600585 : countsize += read_size;
1134 :
1135 : /* always insert CHG blocks, the repair functions needs all of them */
1136 : /* because the parity may be still referring at the old state */
1137 : /* and the repair must be aware of it */
1138 4600585 : if (block_state == BLOCK_STATE_CHG) {
1139 : /* we DO NOT mark them as bad to avoid to overwrite them with wrong data. */
1140 : /* if we don't have a hash, we always assume the first read of the block correct. */
1141 13843 : failed[failed_count].is_bad = 0; /* we assume the CHG block correct */
1142 13843 : failed[failed_count].is_outofdate = 0;
1143 13843 : failed[failed_count].index = j;
1144 13843 : failed[failed_count].block = block;
1145 13843 : failed[failed_count].disk = disk;
1146 13843 : failed[failed_count].file = file;
1147 13843 : failed[failed_count].file_pos = file_pos;
1148 13843 : failed[failed_count].handle = &handle[j];
1149 13843 : ++failed_count;
1150 13843 : continue;
1151 : }
1152 :
1153 4586742 : assert(block_state == BLOCK_STATE_BLK || block_state == BLOCK_STATE_REP);
1154 :
1155 : /* compute the hash of the block just read */
1156 4586742 : if (rehash) {
1157 27243 : memhash(state->prevhash, state->prevhashseed, hash, buffer[j], read_size);
1158 : } else {
1159 4559499 : memhash(state->hash, state->hashseed, hash, buffer[j], read_size);
1160 : }
1161 :
1162 : /* compare the hash */
1163 4586742 : if (memcmp(hash, block->hash, HASH_SIZE) != 0) {
1164 12974 : unsigned diff = memdiff(hash, block->hash, HASH_SIZE);
1165 :
1166 : /* save the failed block for the check/fix */
1167 12974 : failed[failed_count].is_bad = 1; /* it's bad because the hash doesn't match */
1168 12974 : failed[failed_count].is_outofdate = 0;
1169 12974 : failed[failed_count].index = j;
1170 12974 : failed[failed_count].block = block;
1171 12974 : failed[failed_count].disk = disk;
1172 12974 : failed[failed_count].file = file;
1173 12974 : failed[failed_count].file_pos = file_pos;
1174 12974 : failed[failed_count].handle = &handle[j];
1175 12974 : ++failed_count;
1176 :
1177 12974 : log_tag("error:%u:%s:%s: Data error at position %u, diff bits %u\n", i, disk->name, esc(file->sub), file_pos, diff);
1178 12974 : ++error;
1179 12974 : continue;
1180 : }
1181 :
1182 : /* always insert REP blocks, the repair functions needs all of them */
1183 : /* because the parity may be still referring at the old state */
1184 : /* and the repair must be aware of it */
1185 4573768 : if (block_state == BLOCK_STATE_REP) {
1186 53428 : failed[failed_count].is_bad = 0; /* it's not bad */
1187 53428 : failed[failed_count].is_outofdate = 0;
1188 53428 : failed[failed_count].index = j;
1189 53428 : failed[failed_count].block = block;
1190 53428 : failed[failed_count].disk = disk;
1191 53428 : failed[failed_count].file = file;
1192 53428 : failed[failed_count].file_pos = file_pos;
1193 53428 : failed[failed_count].handle = &handle[j];
1194 53428 : ++failed_count;
1195 53428 : continue;
1196 : }
1197 : }
1198 :
1199 : /* now read and check the parity if requested */
1200 910267 : if (!state->opt.auditonly) {
1201 : void* buffer_recov[LEV_MAX];
1202 : void* buffer_zero;
1203 :
1204 : /* buffers for parity read and not computed */
1205 4171025 : for (l = 0; l < state->level; ++l)
1206 3300033 : buffer_recov[l] = buffer[diskmax + state->level + l];
1207 2796911 : for (; l < LEV_MAX; ++l)
1208 1925919 : buffer_recov[l] = 0;
1209 :
1210 : /* the zero buffer is the last one */
1211 870992 : buffer_zero = buffer[buffermax - 1];
1212 :
1213 : /* read the parity */
1214 4171025 : for (l = 0; l < state->level; ++l) {
1215 3300033 : if (parity[l]) {
1216 3286131 : ret = parity_read(parity[l], i, buffer_recov[l], state->block_size, log_error);
1217 3286131 : if (ret == -1) {
1218 90335 : buffer_recov[l] = 0; /* no parity to use */
1219 :
1220 90335 : log_tag("parity_error:%u:%s: Read error\n", i, lev_config_name(l));
1221 90335 : ++error;
1222 : }
1223 : } else {
1224 13902 : buffer_recov[l] = 0;
1225 : }
1226 : }
1227 :
1228 : /* try all the recovering strategies */
1229 870992 : ret = repair(state, rehash, i, diskmax, failed, failed_map, failed_count, buffer, buffer_recov, buffer_zero);
1230 870992 : if (ret != 0) {
1231 : /* increment the number of errors */
1232 47557 : if (ret > 0)
1233 4635 : error += ret;
1234 47557 : ++unrecoverable_error;
1235 :
1236 : /* print a list of all the errors in files */
1237 218200 : for (j = 0; j < failed_count; ++j) {
1238 170643 : if (failed[j].is_bad)
1239 166121 : log_tag("unrecoverable:%u:%s:%s: Unrecoverable error at position %u\n", i, failed[j].disk->name, esc(failed[j].file->sub), failed[j].file_pos);
1240 : }
1241 :
1242 : /* keep track of damaged files */
1243 218200 : for (j = 0; j < failed_count; ++j) {
1244 170643 : if (failed[j].is_bad)
1245 166121 : file_flag_set(failed[j].file, FILE_IS_DAMAGED);
1246 : }
1247 : } else {
1248 : /* now counts partial recovers */
1249 : /* note that this could happen only when we have an incomplete 'sync' */
1250 : /* and that we have recovered is the state before the 'sync' */
1251 823435 : int partial_recover_error = 0;
1252 :
1253 : /* print a list of all the errors in files */
1254 1382113 : for (j = 0; j < failed_count; ++j) {
1255 558678 : if (failed[j].is_bad && failed[j].is_outofdate) {
1256 8 : ++partial_recover_error;
1257 8 : log_tag("unrecoverable:%u:%s:%s: Unrecoverable unsynced error at position %u\n", i, failed[j].disk->name, esc(failed[j].file->sub), failed[j].file_pos);
1258 : }
1259 : }
1260 823435 : if (partial_recover_error != 0) {
1261 8 : error += partial_recover_error;
1262 8 : ++unrecoverable_error;
1263 : }
1264 :
1265 : /* now check parities, but only if all the blocks have it computed */
1266 : /* if you check/fix after a partial sync, it's OK to have parity errors on the blocks with invalid parity */
1267 : /* and doesn't make sense to try to fix it */
1268 823435 : if (valid_parity) {
1269 : /* check the parity */
1270 3729908 : for (l = 0; l < state->level; ++l) {
1271 2968058 : if (buffer_recov[l] != 0 && memcmp(buffer_recov[l], buffer[diskmax + l], state->block_size) != 0) {
1272 10860 : unsigned diff = memdiff(buffer_recov[l], buffer[diskmax + l], state->block_size);
1273 :
1274 : /* mark that the read parity is wrong, setting ptr to 0 */
1275 10860 : buffer_recov[l] = 0;
1276 :
1277 10860 : log_tag("parity_error:%u:%s: Data error, diff bits %u\n", i, lev_config_name(l), diff);
1278 10860 : ++error;
1279 : }
1280 : }
1281 : }
1282 :
1283 : /* now write recovered files */
1284 823435 : if (fix) {
1285 : /* update the fixed files */
1286 652859 : for (j = 0; j < failed_count; ++j) {
1287 : /* nothing to do if it doesn't need recovering */
1288 338152 : if (!failed[j].is_bad)
1289 40318 : continue;
1290 :
1291 : /* do not fix if the file is excluded */
1292 297834 : if (file_flag_has(failed[j].file, FILE_IS_EXCLUDED)
1293 288554 : || (state->opt.syncedonly && file_flag_has(failed[j].file, FILE_IS_UNSYNCED)))
1294 9280 : continue;
1295 :
1296 288554 : ret = handle_write(failed[j].handle, failed[j].file_pos, buffer[failed[j].index], state->block_size);
1297 288554 : if (ret == -1) {
1298 : /* LCOV_EXCL_START */
1299 : /* mark the file as damaged */
1300 : file_flag_set(failed[j].file, FILE_IS_DAMAGED);
1301 :
1302 : if (errno == EACCES) {
1303 : log_fatal("WARNING! Please give write permission to the file.\n");
1304 : } else {
1305 : /* we do not use DANGER because it could be ENOSPC which is not always correctly reported */
1306 : log_fatal("WARNING! Without a working data disk, it isn't possible to fix errors on it.\n");
1307 : }
1308 : log_fatal("Stopping at block %u\n", i);
1309 : ++unrecoverable_error;
1310 : goto bail;
1311 : /* LCOV_EXCL_STOP */
1312 : }
1313 :
1314 : /* if we are not sure that the recovered content is uptodate */
1315 288554 : if (failed[j].is_outofdate) {
1316 : /* mark the file as damaged */
1317 8 : file_flag_set(failed[j].file, FILE_IS_DAMAGED);
1318 8 : continue;
1319 : }
1320 :
1321 : /* mark the file as containing some fixes */
1322 : /* note that it could be also marked as damaged in other iterations */
1323 288546 : file_flag_set(failed[j].file, FILE_IS_FIXED);
1324 :
1325 288546 : log_tag("fixed:%u:%s:%s: Fixed data error at position %u\n", i, failed[j].disk->name, esc(failed[j].file->sub), failed[j].file_pos);
1326 288546 : ++recovered_error;
1327 : }
1328 :
1329 : /* update parity only if all the blocks have it computed */
1330 : /* if you check/fix after a partial sync, you do not want to fix parity */
1331 : /* for blocks that are going to have it computed in the sync completion */
1332 314707 : if (valid_parity) {
1333 : /* update the parity */
1334 964277 : for (l = 0; l < state->level; ++l) {
1335 : /* if the parity on disk is wrong */
1336 698750 : if (buffer_recov[l] == 0
1337 : /* and we have access at the parity */
1338 67322 : && parity[l] != 0
1339 : /* and the parity is not excluded */
1340 62635 : && !state->parity[l].is_excluded
1341 : ) {
1342 62635 : ret = parity_write(parity[l], i, buffer[diskmax + l], state->block_size);
1343 62635 : if (ret == -1) {
1344 : /* LCOV_EXCL_START */
1345 : /* we do not use DANGER because it could be ENOSPC which is not always correctly reported */
1346 : log_fatal("WARNING! Without a working %s disk, it isn't possible to fix errors on it.\n", lev_name(l));
1347 : log_fatal("Stopping at block %u\n", i);
1348 : ++unrecoverable_error;
1349 : goto bail;
1350 : /* LCOV_EXCL_STOP */
1351 : }
1352 :
1353 62635 : log_tag("parity_fixed:%u:%s: Fixed data error\n", i, lev_config_name(l));
1354 62635 : ++recovered_error;
1355 : }
1356 : }
1357 : }
1358 : } else {
1359 : /* if we are not fixing, we just set the FIXED flag */
1360 : /* meaning that we could fix this file if we try */
1361 729254 : for (j = 0; j < failed_count; ++j) {
1362 220526 : if (failed[j].is_bad) {
1363 179641 : file_flag_set(failed[j].file, FILE_IS_FIXED);
1364 : }
1365 : }
1366 : }
1367 : }
1368 : } else {
1369 : /* if we are not checking, we just set the DAMAGED flag */
1370 : /* to report that the file is damaged, and we don't know if we can fix it */
1371 45091 : for (j = 0; j < failed_count; ++j) {
1372 5816 : if (failed[j].is_bad) {
1373 5816 : file_flag_set(failed[j].file, FILE_IS_DAMAGED);
1374 : }
1375 : }
1376 : }
1377 :
1378 : /* post process the files */
1379 910267 : ret = file_post(state, fix, i, handle, diskmax);
1380 910267 : if (ret == -1) {
1381 0 : log_fatal("Stopping at block %u\n", i);
1382 0 : ++unrecoverable_error;
1383 0 : goto bail;
1384 : }
1385 :
1386 : /* count the number of processed block */
1387 910267 : ++countpos;
1388 :
1389 : /* progress */
1390 910267 : if (state_progress(state, i, countpos, countmax, countsize)) {
1391 : /* LCOV_EXCL_START */
1392 : break;
1393 : /* LCOV_EXCL_STOP */
1394 : }
1395 : }
1396 :
1397 : /* for each disk, recover empty files, symlinks and empty dirs */
1398 826 : for (i = 0; i < diskmax; ++i) {
1399 : tommy_node* node;
1400 : struct snapraid_disk* disk;
1401 :
1402 708 : if (!handle[i].disk)
1403 3 : continue;
1404 :
1405 : /* for each empty file in the disk */
1406 705 : disk = handle[i].disk;
1407 705 : node = disk->filelist;
1408 2189134 : while (node) {
1409 : char path[PATH_MAX];
1410 : struct stat st;
1411 : struct snapraid_file* file;
1412 2187724 : int unsuccesful = 0;
1413 :
1414 2187724 : file = node->data;
1415 2187724 : node = node->next; /* next node */
1416 :
1417 : /* if not empty, it's already checked and continue to the next one */
1418 2187724 : if (file->size != 0) {
1419 4369713 : continue;
1420 : }
1421 :
1422 : /* if excluded continue to the next one */
1423 2952 : if (file_flag_has(file, FILE_IS_EXCLUDED)) {
1424 169 : continue;
1425 : }
1426 :
1427 : /* stat the file */
1428 2783 : pathprint(path, sizeof(path), "%s%s", disk->dir, file->sub);
1429 2783 : ret = stat(path, &st);
1430 2783 : if (ret == -1) {
1431 164 : unsuccesful = 1;
1432 :
1433 164 : log_error("Error stating empty file '%s'. %s.\n", path, strerror(errno));
1434 164 : log_tag("error:%s:%s: Empty file stat error\n", disk->name, esc(file->sub));
1435 164 : ++error;
1436 2619 : } else if (!S_ISREG(st.st_mode)) {
1437 0 : unsuccesful = 1;
1438 :
1439 0 : log_tag("error:%s:%s: Empty file error for not regular file\n", disk->name, esc(file->sub));
1440 0 : ++error;
1441 2619 : } else if (st.st_size != 0) {
1442 3 : unsuccesful = 1;
1443 :
1444 3 : log_tag("error:%s:%s: Empty file error for size '%" PRIu64 "'\n", disk->name, esc(file->sub), st.st_size);
1445 3 : ++error;
1446 : }
1447 :
1448 2783 : if (fix && unsuccesful) {
1449 : int f;
1450 :
1451 : /* create the ancestor directories */
1452 153 : ret = mkancestor(path);
1453 153 : if (ret != 0) {
1454 : /* LCOV_EXCL_START */
1455 : log_fatal("WARNING! Without a working data disk, it isn't possible to fix errors on it.\n");
1456 : log_fatal("Stopping\n");
1457 : ++unrecoverable_error;
1458 : goto bail;
1459 : /* LCOV_EXCL_STOP */
1460 : }
1461 :
1462 : /* create it */
1463 : /* O_NOFOLLOW: do not follow links to ensure to open the real file */
1464 153 : f = open(path, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_NOFOLLOW, 0600);
1465 153 : if (f == -1) {
1466 : /* LCOV_EXCL_START */
1467 : log_fatal("Error creating empty file '%s'. %s.\n", path, strerror(errno));
1468 : if (errno == EACCES) {
1469 : log_fatal("WARNING! Please give write permission to the file.\n");
1470 : } else {
1471 : /* we do not use DANGER because it could be ENOSPC which is not always correctly reported */
1472 : log_fatal("WARNING! Without a working data disk, it isn't possible to fix errors on it.\n");
1473 : }
1474 : log_fatal("Stopping\n");
1475 : ++unrecoverable_error;
1476 : goto bail;
1477 : /* LCOV_EXCL_STOP */
1478 : }
1479 :
1480 : /* set the original modification time */
1481 153 : ret = fmtime(f, file->mtime_sec, file->mtime_nsec);
1482 153 : if (ret != 0) {
1483 : /* LCOV_EXCL_START */
1484 : close(f);
1485 :
1486 : log_fatal("Error timing file '%s'. %s.\n", file->sub, strerror(errno));
1487 : log_fatal("WARNING! Without a working data disk, it isn't possible to fix errors on it.\n");
1488 : log_fatal("Stopping\n");
1489 : ++unrecoverable_error;
1490 : goto bail;
1491 : /* LCOV_EXCL_STOP */
1492 : }
1493 :
1494 : /* close it */
1495 153 : ret = close(f);
1496 153 : if (ret != 0) {
1497 : /* LCOV_EXCL_START */
1498 : log_fatal("WARNING! Without a working data disk, it isn't possible to fix errors on it.\n");
1499 : log_fatal("Stopping\n");
1500 : ++unrecoverable_error;
1501 : goto bail;
1502 : /* LCOV_EXCL_STOP */
1503 : }
1504 :
1505 153 : log_tag("fixed:%s:%s: Fixed empty file\n", disk->name, esc(file->sub));
1506 153 : ++recovered_error;
1507 :
1508 153 : log_tag("status:recovered:%s:%s\n", disk->name, esc(file->sub));
1509 153 : msg_info("recovered %s%s\n", disk->dir, file->sub);
1510 : }
1511 : }
1512 :
1513 : /* for each link in the disk */
1514 705 : disk = handle[i].disk;
1515 705 : node = disk->linklist;
1516 71537 : while (node) {
1517 : char path[PATH_MAX];
1518 : char pathto[PATH_MAX];
1519 : char linkto[PATH_MAX];
1520 : struct stat st;
1521 : struct stat stto;
1522 : struct snapraid_link* slink;
1523 70127 : int unsuccesful = 0;
1524 70127 : int unrecoverable = 0;
1525 :
1526 70127 : slink = node->data;
1527 70127 : node = node->next; /* next node */
1528 :
1529 : /* if excluded continue to the next one */
1530 70127 : if (link_flag_has(slink, FILE_IS_EXCLUDED)) {
1531 3277 : continue;
1532 : }
1533 :
1534 66850 : if (link_flag_has(slink, FILE_IS_HARDLINK)) {
1535 : /* stat the link */
1536 318 : pathprint(path, sizeof(path), "%s%s", disk->dir, slink->sub);
1537 318 : ret = stat(path, &st);
1538 318 : if (ret == -1) {
1539 36 : unsuccesful = 1;
1540 :
1541 36 : log_error("Error stating hardlink '%s'. %s.\n", path, strerror(errno));
1542 36 : log_tag("hardlink_error:%s:%s:%s: Hardlink stat error\n", disk->name, esc(slink->sub), esc(slink->linkto));
1543 36 : ++error;
1544 282 : } else if (!S_ISREG(st.st_mode)) {
1545 0 : unsuccesful = 1;
1546 :
1547 0 : log_tag("hardlink_error:%s:%s:%s: Hardlink error for not regular file\n", disk->name, esc(slink->sub), esc(slink->linkto));
1548 0 : ++error;
1549 : }
1550 :
1551 : /* stat the "to" file */
1552 318 : pathprint(pathto, sizeof(pathto), "%s%s", disk->dir, slink->linkto);
1553 318 : ret = stat(pathto, &stto);
1554 318 : if (ret == -1) {
1555 24 : unsuccesful = 1;
1556 :
1557 24 : if (errno == ENOENT) {
1558 24 : unrecoverable = 1;
1559 24 : if (fix) {
1560 : /* if the target doesn't exist, it's unrecoverable */
1561 : /* because we cannot create an hardlink of a file that */
1562 : /* doesn't exists */
1563 12 : ++unrecoverable_error;
1564 : } else {
1565 : /* but in check, we can assume that fixing will recover */
1566 : /* such missing file, so we assume a less drastic error */
1567 12 : ++error;
1568 : }
1569 : }
1570 :
1571 24 : log_error("Error stating hardlink-to '%s'. %s.\n", pathto, strerror(errno));
1572 24 : log_tag("hardlink_error:%s:%s:%s: Hardlink to stat error\n", disk->name, esc(slink->sub), esc(slink->linkto));
1573 24 : ++error;
1574 294 : } else if (!S_ISREG(stto.st_mode)) {
1575 0 : unsuccesful = 1;
1576 :
1577 0 : log_tag("hardlink_error:%s:%s:%s: Hardlink-to error for not regular file\n", disk->name, esc(slink->sub), esc(slink->linkto));
1578 0 : ++error;
1579 294 : } else if (!unsuccesful && st.st_ino != stto.st_ino) {
1580 0 : unsuccesful = 1;
1581 :
1582 0 : log_error("Mismatch hardlink '%s' and '%s'. Different inode.\n", path, pathto);
1583 0 : log_tag("hardlink_error:%s:%s:%s: Hardlink mismatch for different inode\n", disk->name, esc(slink->sub), esc(slink->linkto));
1584 0 : ++error;
1585 : }
1586 : } else {
1587 : /* read the symlink */
1588 66532 : pathprint(path, sizeof(path), "%s%s", disk->dir, slink->sub);
1589 66532 : ret = readlink(path, linkto, sizeof(linkto));
1590 66532 : if (ret < 0) {
1591 4297 : unsuccesful = 1;
1592 :
1593 4297 : log_error("Error reading symlink '%s'. %s.\n", path, strerror(errno));
1594 4297 : log_tag("symlink_error:%s:%s: Symlink read error\n", disk->name, esc(slink->sub));
1595 4297 : ++error;
1596 62235 : } else if (ret >= PATH_MAX) {
1597 0 : unsuccesful = 1;
1598 :
1599 0 : log_error("Error reading symlink '%s'. Symlink too long.\n", path);
1600 0 : log_tag("symlink_error:%s:%s: Symlink read error\n", disk->name, esc(slink->sub));
1601 0 : ++error;
1602 : } else {
1603 62235 : linkto[ret] = 0;
1604 :
1605 62235 : if (strcmp(linkto, slink->linkto) != 0) {
1606 539 : unsuccesful = 1;
1607 :
1608 539 : log_tag("symlink_error:%s:%s: Symlink data error '%s' instead of '%s'\n", disk->name, esc(slink->sub), linkto, slink->linkto);
1609 539 : ++error;
1610 : }
1611 : }
1612 : }
1613 :
1614 66850 : if (fix && unsuccesful && !unrecoverable) {
1615 : /* create the ancestor directories */
1616 3942 : ret = mkancestor(path);
1617 3942 : if (ret != 0) {
1618 : /* LCOV_EXCL_START */
1619 : log_fatal("WARNING! Without a working data disk, it isn't possible to fix errors on it.\n");
1620 : log_fatal("Stopping\n");
1621 : ++unrecoverable_error;
1622 : goto bail;
1623 : /* LCOV_EXCL_STOP */
1624 : }
1625 :
1626 : /* if it exists, it must be deleted before recreating */
1627 3942 : ret = remove(path);
1628 3942 : if (ret != 0 && errno != ENOENT) {
1629 : /* LCOV_EXCL_START */
1630 : log_fatal("Error removing '%s'. %s.\n", path, strerror(errno));
1631 : log_fatal("WARNING! Without a working data disk, it isn't possible to fix errors on it.\n");
1632 : log_fatal("Stopping\n");
1633 : ++unrecoverable_error;
1634 : goto bail;
1635 : /* LCOV_EXCL_STOP */
1636 : }
1637 :
1638 : /* create it */
1639 3942 : if (link_flag_has(slink, FILE_IS_HARDLINK)) {
1640 12 : ret = hardlink(pathto, path);
1641 12 : if (ret != 0) {
1642 : /* LCOV_EXCL_START */
1643 : log_fatal("Error writing hardlink '%s' to '%s'. %s.\n", path, pathto, strerror(errno));
1644 : if (errno == EACCES) {
1645 : log_fatal("WARNING! Please give write permission to the hardlink.\n");
1646 : } else {
1647 : /* we do not use DANGER because it could be ENOSPC which is not always correctly reported */
1648 : log_fatal("WARNING! Without a working data disk, it isn't possible to fix errors on it.\n");
1649 : }
1650 : log_fatal("Stopping\n");
1651 : ++unrecoverable_error;
1652 : goto bail;
1653 : /* LCOV_EXCL_STOP */
1654 : }
1655 :
1656 12 : log_tag("hardlink_fixed:%s:%s: Fixed hardlink error\n", disk->name, esc(slink->sub));
1657 12 : ++recovered_error;
1658 : } else {
1659 3930 : ret = symlink(slink->linkto, path);
1660 3930 : if (ret != 0) {
1661 : /* LCOV_EXCL_START */
1662 : log_fatal("Error writing symlink '%s' to '%s'. %s.\n", path, slink->linkto, strerror(errno));
1663 : if (errno == EACCES) {
1664 : log_fatal("WARNING! Please give write permission to the symlink.\n");
1665 : } else {
1666 : /* we do not use DANGER because it could be ENOSPC which is not always correctly reported */
1667 : log_fatal("WARNING! Without a working data disk, it isn't possible to fix errors on it.\n");
1668 : }
1669 : log_fatal("Stopping\n");
1670 : ++unrecoverable_error;
1671 : goto bail;
1672 : /* LCOV_EXCL_STOP */
1673 : }
1674 :
1675 3930 : log_tag("symlink_fixed:%s:%s: Fixed symlink error\n", disk->name, esc(slink->sub));
1676 3930 : ++recovered_error;
1677 : }
1678 :
1679 3942 : log_tag("status:recovered:%s:%s\n", disk->name, esc(slink->sub));
1680 3942 : msg_info("recovered %s%s\n", disk->dir, slink->sub);
1681 : }
1682 : }
1683 :
1684 : /* for each dir in the disk */
1685 705 : disk = handle[i].disk;
1686 705 : node = disk->dirlist;
1687 1850 : while (node) {
1688 : char path[PATH_MAX];
1689 : struct stat st;
1690 : struct snapraid_dir* dir;
1691 440 : int unsuccesful = 0;
1692 :
1693 440 : dir = node->data;
1694 440 : node = node->next; /* next node */
1695 :
1696 : /* if excluded continue to the next one */
1697 440 : if (dir_flag_has(dir, FILE_IS_EXCLUDED)) {
1698 8 : continue;
1699 : }
1700 :
1701 : /* stat the dir */
1702 432 : pathprint(path, sizeof(path), "%s%s", disk->dir, dir->sub);
1703 432 : ret = stat(path, &st);
1704 432 : if (ret == -1) {
1705 23 : unsuccesful = 1;
1706 :
1707 23 : log_error("Error stating dir '%s'. %s.\n", path, strerror(errno));
1708 23 : log_tag("dir_error:%s:%s: Dir stat error\n", disk->name, esc(dir->sub));
1709 23 : ++error;
1710 409 : } else if (!S_ISDIR(st.st_mode)) {
1711 0 : unsuccesful = 1;
1712 :
1713 0 : log_tag("dir_error:%s:%s: Dir error for not directory\n", disk->name, esc(dir->sub));
1714 0 : ++error;
1715 : }
1716 :
1717 432 : if (fix && unsuccesful) {
1718 : /* create the ancestor directories */
1719 21 : ret = mkancestor(path);
1720 21 : if (ret != 0) {
1721 : /* LCOV_EXCL_START */
1722 : log_fatal("WARNING! Without a working data disk, it isn't possible to fix errors on it.\n");
1723 : log_fatal("Stopping\n");
1724 : ++unrecoverable_error;
1725 : goto bail;
1726 : /* LCOV_EXCL_STOP */
1727 : }
1728 :
1729 : /* create it */
1730 21 : ret = mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
1731 21 : if (ret != 0) {
1732 : /* LCOV_EXCL_START */
1733 : log_fatal("Error creating dir '%s'. %s.\n", path, strerror(errno));
1734 : if (errno == EACCES) {
1735 : log_fatal("WARNING! Please give write permission to the dir.\n");
1736 : } else {
1737 : /* we do not use DANGER because it could be ENOSPC which is not always correctly reported */
1738 : log_fatal("WARNING! Without a working data disk, it isn't possible to fix errors on it.\n");
1739 : }
1740 : log_fatal("Stopping\n");
1741 : ++unrecoverable_error;
1742 : goto bail;
1743 : /* LCOV_EXCL_STOP */
1744 : }
1745 :
1746 21 : log_tag("dir_fixed:%s:%s: Fixed dir error\n", disk->name, esc(dir->sub));
1747 21 : ++recovered_error;
1748 :
1749 21 : log_tag("status:recovered:%s:%s\n", disk->name, esc(dir->sub));
1750 21 : msg_info("recovered %s%s\n", disk->dir, dir->sub);
1751 : }
1752 : }
1753 : }
1754 :
1755 118 : state_progress_end(state, countpos, countmax, countsize);
1756 :
1757 : bail:
1758 : /* close all the files left open */
1759 826 : for (j = 0; j < diskmax; ++j) {
1760 708 : struct snapraid_file* file = handle[j].file;
1761 708 : struct snapraid_disk* disk = handle[j].disk;
1762 708 : ret = handle_close(&handle[j]);
1763 708 : if (ret == -1) {
1764 : /* LCOV_EXCL_START */
1765 : log_tag("error:%u:%s:%s: Close error. %s\n", blockmax, disk->name, esc(file->sub), strerror(errno));
1766 : log_fatal("DANGER! Unexpected close error in a data disk.\n");
1767 : ++unrecoverable_error;
1768 : /* continue, as we are already exiting */
1769 : /* LCOV_EXCL_STOP */
1770 : }
1771 : }
1772 :
1773 : /* remove all the files created from scratch that have not finished the processing */
1774 : /* it happens only when aborting pressing Ctrl+C or other reason. */
1775 118 : if (fix) {
1776 : /* for each disk */
1777 315 : for (i = 0; i < diskmax; ++i) {
1778 : tommy_node* node;
1779 : struct snapraid_disk* disk;
1780 :
1781 270 : if (!handle[i].disk)
1782 1 : continue;
1783 :
1784 : /* for each file in the disk */
1785 269 : disk = handle[i].disk;
1786 269 : node = disk->filelist;
1787 870849 : while (node) {
1788 : char path[PATH_MAX];
1789 : struct snapraid_file* file;
1790 :
1791 870311 : file = node->data;
1792 870311 : node = node->next; /* next node */
1793 :
1794 : /* if the file was not created, meaning that it was already existing */
1795 870311 : if (!file_flag_has(file, FILE_IS_CREATED)) {
1796 : /* nothing to do */
1797 1627568 : continue;
1798 : }
1799 :
1800 : /* if processing was finished */
1801 113054 : if (file_flag_has(file, FILE_IS_FINISHED)) {
1802 : /* nothing to do */
1803 113054 : continue;
1804 : }
1805 :
1806 : /* if the file was originally missing, and processing not yet finished */
1807 : /* we have to throw it away to ensure that at the next run we will retry */
1808 : /* to fix it, in case we select to undelete missing files */
1809 0 : pathprint(path, sizeof(path), "%s%s", disk->dir, file->sub);
1810 :
1811 0 : ret = remove(path);
1812 0 : if (ret != 0) {
1813 : /* LCOV_EXCL_START */
1814 : log_fatal("Error removing '%s'. %s.\n", path, strerror(errno));
1815 : log_fatal("WARNING! Without a working data disk, it isn't possible to fix errors on it.\n");
1816 : ++unrecoverable_error;
1817 : /* continue, as we are already exiting */
1818 : /* LCOV_EXCL_STOP */
1819 : }
1820 : }
1821 : }
1822 : }
1823 :
1824 118 : if (error || recovered_error || unrecoverable_error) {
1825 68 : msg_status("\n");
1826 68 : msg_status("%8u errors\n", error);
1827 68 : if (fix) {
1828 44 : msg_status("%8u recovered errors\n", recovered_error);
1829 : }
1830 136 : if (unrecoverable_error) {
1831 13 : msg_status("%8u UNRECOVERABLE errors\n", unrecoverable_error);
1832 : } else {
1833 : /* without checking, we don't know if they are really recoverable or not */
1834 55 : if (!state->opt.auditonly)
1835 53 : msg_status("%8u unrecoverable errors\n", unrecoverable_error);
1836 55 : if (fix)
1837 35 : msg_status("Everything OK\n");
1838 : }
1839 : } else {
1840 50 : msg_status("Everything OK\n");
1841 : }
1842 :
1843 118 : if (error && !fix)
1844 24 : log_fatal("WARNING! There are errors!\n");
1845 118 : if (unrecoverable_error)
1846 13 : log_fatal("DANGER! There are unrecoverable errors!\n");
1847 :
1848 118 : log_tag("summary:error:%u\n", error);
1849 118 : if (fix)
1850 45 : log_tag("summary:error_recovered:%u\n", recovered_error);
1851 118 : if (!state->opt.auditonly)
1852 113 : log_tag("summary:error_unrecoverable:%u\n", unrecoverable_error);
1853 118 : if (fix) {
1854 45 : if (error + recovered_error + unrecoverable_error == 0)
1855 1 : log_tag("summary:exit:ok\n");
1856 44 : else if (unrecoverable_error == 0)
1857 35 : log_tag("summary:exit:recovered\n");
1858 : else
1859 9 : log_tag("summary:exit:unrecoverable\n");
1860 73 : } else if (!state->opt.auditonly) {
1861 68 : if (error + unrecoverable_error == 0)
1862 46 : log_tag("summary:exit:ok\n");
1863 22 : else if (unrecoverable_error == 0)
1864 18 : log_tag("summary:exit:recoverable\n");
1865 : else
1866 4 : log_tag("summary:exit:unrecoverable\n");
1867 : } else { /* audit only */
1868 5 : if (error == 0)
1869 3 : log_tag("summary:exit:ok\n");
1870 : else
1871 2 : log_tag("summary:exit:error\n");
1872 : }
1873 118 : log_flush();
1874 :
1875 118 : free(failed);
1876 118 : free(failed_map);
1877 118 : free(handle);
1878 118 : free(buffer_alloc);
1879 118 : free(buffer);
1880 :
1881 : /* fail if some error are present after the run */
1882 118 : if (fix) {
1883 45 : if (state->opt.expect_unrecoverable) {
1884 9 : if (unrecoverable_error == 0)
1885 0 : return -1;
1886 : } else {
1887 36 : if (unrecoverable_error != 0)
1888 0 : return -1;
1889 : }
1890 : } else {
1891 73 : if (state->opt.expect_unrecoverable) {
1892 4 : if (unrecoverable_error == 0)
1893 0 : return -1;
1894 69 : } else if (state->opt.expect_recoverable) {
1895 20 : if (unrecoverable_error != 0 || error == 0)
1896 0 : return -1;
1897 : } else {
1898 49 : if (error != 0 || unrecoverable_error != 0)
1899 0 : return -1;
1900 : }
1901 : }
1902 :
1903 118 : return 0;
1904 : }
1905 :
1906 120 : int state_check(struct snapraid_state* state, int fix, block_off_t blockstart, block_off_t blockcount)
1907 : {
1908 : block_off_t blockmax;
1909 : data_off_t size;
1910 : data_off_t out_size;
1911 : int ret;
1912 : struct snapraid_parity_handle parity[LEV_MAX];
1913 : struct snapraid_parity_handle* parity_ptr[LEV_MAX];
1914 : unsigned error;
1915 : unsigned l;
1916 :
1917 120 : msg_progress("Initializing...\n");
1918 :
1919 120 : blockmax = parity_allocated_size(state);
1920 120 : size = blockmax * (data_off_t)state->block_size;
1921 :
1922 120 : if (blockstart > blockmax) {
1923 : /* LCOV_EXCL_START */
1924 : log_fatal("Error in the specified starting block %u. It's bigger than the parity size %u.\n", blockstart, blockmax);
1925 : exit(EXIT_FAILURE);
1926 : /* LCOV_EXCL_STOP */
1927 : }
1928 :
1929 : /* adjust the number of block to process */
1930 120 : if (blockcount != 0 && blockstart + blockcount < blockmax) {
1931 0 : blockmax = blockstart + blockcount;
1932 : }
1933 :
1934 120 : if (fix) {
1935 : /* if fixing, create the file and open for writing */
1936 : /* if it fails, we cannot continue */
1937 180 : for (l = 0; l < state->level; ++l) {
1938 : /* skip parity disks that are not accessible */
1939 135 : if (state->parity[l].skip_access) {
1940 1 : parity_ptr[l] = 0;
1941 1 : continue;
1942 : }
1943 :
1944 134 : parity_ptr[l] = &parity[l];
1945 134 : ret = parity_create(parity_ptr[l], state->parity[l].path, &out_size, state->file_mode);
1946 134 : if (ret == -1) {
1947 : /* LCOV_EXCL_START */
1948 : log_fatal("WARNING! Without an accessible %s file, it isn't possible to fix any error.\n", lev_name(l));
1949 : exit(EXIT_FAILURE);
1950 : /* LCOV_EXCL_STOP */
1951 : }
1952 :
1953 134 : ret = parity_chsize(parity_ptr[l], size, &out_size, state->opt.skip_fallocate);
1954 134 : if (ret == -1) {
1955 : /* LCOV_EXCL_START */
1956 : log_fatal("WARNING! Without an accessible %s file, it isn't possible to sync.\n", lev_name(l));
1957 : exit(EXIT_FAILURE);
1958 : /* LCOV_EXCL_STOP */
1959 : }
1960 : }
1961 75 : } else if (!state->opt.auditonly) {
1962 : /* if checking, open the file for reading */
1963 : /* it may fail if the file doesn't exist, in this case we continue to check the files */
1964 393 : for (l = 0; l < state->level; ++l) {
1965 324 : parity_ptr[l] = &parity[l];
1966 324 : ret = parity_open(parity_ptr[l], state->parity[l].path, state->file_mode);
1967 324 : if (ret == -1) {
1968 1 : msg_status("No accessible %s file, only files will be checked.\n", lev_name(l));
1969 : /* continue anyway */
1970 1 : parity_ptr[l] = 0;
1971 : }
1972 : }
1973 : } else {
1974 : /* otherwise don't use any parity */
1975 28 : for (l = 0; l < state->level; ++l)
1976 22 : parity_ptr[l] = 0;
1977 : }
1978 :
1979 120 : if (fix)
1980 45 : msg_progress("Fixing...\n");
1981 75 : else if (!state->opt.auditonly)
1982 69 : msg_progress("Checking...\n");
1983 : else
1984 6 : msg_progress("Hashing...\n");
1985 :
1986 120 : error = 0;
1987 :
1988 : /* skip degenerated cases of empty parity, or skipping all */
1989 120 : if (blockstart < blockmax) {
1990 118 : ret = state_check_process(state, fix, parity_ptr, blockstart, blockmax);
1991 118 : if (ret == -1) {
1992 : /* LCOV_EXCL_START */
1993 : ++error;
1994 : /* continue, as we are already exiting */
1995 : /* LCOV_EXCL_STOP */
1996 : }
1997 : }
1998 :
1999 : /* try to close only if opened */
2000 601 : for (l = 0; l < state->level; ++l) {
2001 481 : if (parity_ptr[l]) {
2002 457 : ret = parity_close(parity_ptr[l]);
2003 : /* LCOV_EXCL_START */
2004 : if (ret == -1) {
2005 : log_fatal("DANGER! Unexpected close error in %s disk.\n", lev_name(l));
2006 : ++error;
2007 : /* continue, as we are already exiting */
2008 : }
2009 : /* LCOV_EXCL_STOP */
2010 : }
2011 : }
2012 :
2013 : /* abort if error are present */
2014 120 : if (error != 0)
2015 0 : return -1;
2016 120 : return 0;
2017 : }
2018 :
|