LCOV - code coverage report
Current view: top level - cmdline - check.c (source / functions) Hit Total Coverage
Test: lcov.info Lines: 709 781 90.8 %
Date: 2016-11-06 Functions: 9 9 100.0 %

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

Generated by: LCOV version 1.10