LCOV - code coverage report
Current view: top level - cmdline - sync.c (source / functions) Hit Total Coverage
Test: lcov.info Lines: 431 463 93.1 %
Date: 2016-01-16 Functions: 4 4 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 "elem.h"
      22             : #include "state.h"
      23             : #include "parity.h"
      24             : #include "handle.h"
      25             : #include "raid/raid.h"
      26             : 
      27             : /****************************************************************************/
      28             : /* hash */
      29             : 
      30           7 : static int state_hash_process(struct snapraid_state* state, block_off_t blockstart, block_off_t blockmax, int* skip_sync)
      31             : {
      32             :         struct snapraid_handle* handle;
      33             :         unsigned diskmax;
      34             :         block_off_t i;
      35             :         unsigned j;
      36             :         void* buffer;
      37             :         data_off_t countsize;
      38             :         block_off_t countpos;
      39             :         block_off_t countmax;
      40             :         int ret;
      41             :         unsigned error;
      42             :         unsigned io_error;
      43             : 
      44             :         /* maps the disks to handles */
      45           7 :         handle = handle_map(state, &diskmax);
      46             : 
      47             :         /* buffer for reading */
      48           7 :         buffer = malloc_nofail(state->block_size);
      49           7 :         if (!state->opt.skip_self)
      50           0 :                 mtest_vector(1, state->block_size, &buffer);
      51             : 
      52           7 :         error = 0;
      53           7 :         io_error = 0;
      54             : 
      55             :         /* first count the number of blocks to process */
      56           7 :         countmax = 0;
      57          49 :         for (j = 0; j < diskmax; ++j) {
      58          42 :                 struct snapraid_disk* disk = handle[j].disk;
      59             : 
      60             :                 /* if no disk, nothing to check */
      61          42 :                 if (!disk)
      62           0 :                         continue;
      63             : 
      64      236670 :                 for (i = blockstart; i < blockmax; ++i) {
      65             :                         struct snapraid_block* block;
      66             :                         unsigned block_state;
      67             : 
      68      236628 :                         block = fs_par2block_get(disk, i);
      69             : 
      70             :                         /* get the state of the block */
      71      236628 :                         block_state = block_state_get(block);
      72             : 
      73             :                         /* process REP and CHG blocks */
      74      236628 :                         if (block_state != BLOCK_STATE_REP && block_state != BLOCK_STATE_CHG)
      75      223278 :                                 continue;
      76             : 
      77       13350 :                         ++countmax;
      78             :                 }
      79             :         }
      80             : 
      81             :         /* drop until now */
      82           7 :         state_usage_waste(state);
      83             : 
      84           7 :         countsize = 0;
      85           7 :         countpos = 0;
      86           7 :         if (!state_progress_begin(state, blockstart, blockmax, countmax))
      87           0 :                 goto end;
      88             : 
      89          49 :         for (j = 0; j < diskmax; ++j) {
      90          42 :                 struct snapraid_disk* disk = handle[j].disk;
      91             : 
      92             :                 /* if no disk, nothing to check */
      93          42 :                 if (!disk)
      94           0 :                         continue;
      95             : 
      96      236670 :                 for (i = blockstart; i < blockmax; ++i) {
      97             :                         snapraid_info info;
      98             :                         int rehash;
      99             :                         struct snapraid_block* block;
     100             :                         int read_size;
     101             :                         unsigned char hash[HASH_SIZE];
     102             :                         unsigned block_state;
     103             :                         struct snapraid_file* file;
     104             :                         block_off_t file_pos;
     105             : 
     106      236628 :                         block = fs_par2block_get(disk, i);
     107             : 
     108             :                         /* get the state of the block */
     109      236628 :                         block_state = block_state_get(block);
     110             : 
     111             :                         /* process REP and CHG blocks */
     112      236628 :                         if (block_state != BLOCK_STATE_REP && block_state != BLOCK_STATE_CHG)
     113      446566 :                                 continue;
     114             : 
     115             :                         /* get the file of this block */
     116       13350 :                         file = fs_par2file_get(disk, i, &file_pos);
     117             : 
     118             :                         /* get block specific info */
     119       13350 :                         info = info_get(&state->infoarr, i);
     120             : 
     121             :                         /* if we have to use the old hash */
     122       13350 :                         rehash = info_get_rehash(info);
     123             : 
     124             :                         /* until now is CPU */
     125       13350 :                         state_usage_cpu(state);
     126             : 
     127             :                         /* if the file is different than the current one, close it */
     128       13350 :                         if (handle[j].file != 0 && handle[j].file != file) {
     129             :                                 /* keep a pointer at the file we are going to close for error reporting */
     130        5366 :                                 struct snapraid_file* report = handle[j].file;
     131        5366 :                                 ret = handle_close(&handle[j]);
     132        5366 :                                 if (ret == -1) {
     133             :                                         /* LCOV_EXCL_START */
     134             :                                         /* This one is really an unexpected error, because we are only reading */
     135             :                                         /* and closing a descriptor should never fail */
     136             :                                         if (errno == EIO) {
     137             :                                                 log_tag("error:%u:%s:%s: Close EIO error. %s\n", i, disk->name, esc(report->sub), strerror(errno));
     138             :                                                 log_fatal("DANGER! Unexpected input/output close error in a data disk, it isn't possible to sync.\n");
     139             :                                                 log_fatal("Ensure that disk '%s' is sane and that file '%s' can be accessed.\n", disk->dir, handle[j].path);
     140             :                                                 log_fatal("Stopping at block %u\n", i);
     141             :                                                 ++io_error;
     142             :                                                 goto bail;
     143             :                                         }
     144             : 
     145             :                                         log_tag("error:%u:%s:%s: Close error. %s\n", i, disk->name, esc(report->sub), strerror(errno));
     146             :                                         log_fatal("WARNING! Unexpected close error in a data disk, it isn't possible to sync.\n");
     147             :                                         log_fatal("Ensure that file '%s' can be accessed.\n", handle[j].path);
     148             :                                         log_fatal("Stopping at block %u\n", i);
     149             :                                         ++error;
     150             :                                         goto bail;
     151             :                                         /* LCOV_EXCL_STOP */
     152             :                                 }
     153             :                         }
     154             : 
     155       13350 :                         ret = handle_open(&handle[j], file, state->file_mode, log_error, 0);
     156       13350 :                         if (ret == -1) {
     157           6 :                                 if (errno == EIO) {
     158             :                                         /* LCOV_EXCL_START */
     159             :                                         log_tag("error:%u:%s:%s: Open EIO error. %s\n", i, disk->name, esc(file->sub), strerror(errno));
     160             :                                         log_fatal("DANGER! Unexpected input/output open error in a data disk, it isn't possible to sync.\n");
     161             :                                         log_fatal("Ensure that disk '%s' is sane and that file '%s' can be accessed.\n", disk->dir, handle[j].path);
     162             :                                         log_fatal("Stopping at block %u\n", i);
     163             :                                         ++io_error;
     164             :                                         goto bail;
     165             :                                         /* LCOV_EXCL_STOP */
     166             :                                 }
     167             : 
     168           6 :                                 if (errno == ENOENT) {
     169           2 :                                         log_tag("error:%u:%s:%s: Open ENOENT error. %s\n", i, disk->name, esc(file->sub), strerror(errno));
     170           2 :                                         log_error("Missing file '%s'.\n", handle[j].path);
     171           2 :                                         log_error("WARNING! You cannot modify data disk during a sync.\n");
     172           2 :                                         log_error("Rerun the sync command when finished.\n");
     173           2 :                                         ++error;
     174             :                                         /* if the file is missing, it means that it was removed during sync */
     175             :                                         /* this isn't a serious error, so we skip this block, and continue with others */
     176           2 :                                         continue;
     177             :                                 }
     178             : 
     179           4 :                                 if (errno == EACCES) {
     180           4 :                                         log_tag("error:%u:%s:%s: Open EACCES error. %s\n", i, disk->name, esc(file->sub), strerror(errno));
     181           4 :                                         log_error("No access at file '%s'.\n", handle[j].path);
     182           4 :                                         log_error("WARNING! Please fix the access permission in the data disk.\n");
     183           4 :                                         log_error("Rerun the sync command when finished.\n");
     184           4 :                                         ++error;
     185             :                                         /* this isn't a serious error, so we skip this block, and continue with others */
     186           4 :                                         continue;
     187             :                                 }
     188             : 
     189             :                                 /* LCOV_EXCL_START */
     190             :                                 log_tag("error:%u:%s:%s: Open error. %s\n", i, disk->name, esc(file->sub), strerror(errno));
     191             :                                 log_fatal("WARNING! Unexpected open error in a data disk, it isn't possible to sync.\n");
     192             :                                 log_fatal("Ensure that file '%s' can be accessed.\n", handle[j].path);
     193             :                                 log_fatal("Stopping to allow recovery. Try with 'snapraid check -f %s'\n", file->sub);
     194             :                                 ++error;
     195             :                                 goto bail;
     196             :                                 /* LCOV_EXCL_STOP */
     197             :                         }
     198             : 
     199             :                         /* check if the file is changed */
     200       13344 :                         if (handle[j].st.st_size != file->size
     201       13343 :                                 || handle[j].st.st_mtime != file->mtime_sec
     202       13342 :                                 || STAT_NSEC(&handle[j].st) != file->mtime_nsec
     203       13342 :                                 || handle[j].st.st_ino != file->inode
     204             :                         ) {
     205           3 :                                 log_tag("error:%u:%s:%s: Unexpected attribute change\n", i, disk->name, esc(file->sub));
     206           3 :                                 if (handle[j].st.st_size != file->size) {
     207           1 :                                         log_error("Unexpected size change at file '%s' from %" PRIu64 " to %" PRIu64 ".\n", handle[j].path, file->size, handle[j].st.st_size);
     208           2 :                                 } else if (handle[j].st.st_mtime != file->mtime_sec
     209           1 :                                         || STAT_NSEC(&handle[j].st) != file->mtime_nsec) {
     210           1 :                                         log_error("Unexpected time change at file '%s' from %" PRIu64 ".%d to %" PRIu64 ".%d.\n", handle[j].path, file->mtime_sec, file->mtime_nsec, (uint64_t)handle[j].st.st_mtime, (uint32_t)STAT_NSEC(&handle[j].st));
     211             :                                 } else {
     212           1 :                                         log_error("Unexpected inode change from %" PRIu64 " to %" PRIu64 " at file '%s'.\n", file->inode, (uint64_t)handle[j].st.st_ino, handle[j].path);
     213             :                                 }
     214           3 :                                 log_error("WARNING! You cannot modify files during a sync.\n");
     215           3 :                                 log_error("Rerun the sync command when finished.\n");
     216           3 :                                 ++error;
     217             :                                 /* if the file is changed, it means that it was modified during sync */
     218             :                                 /* this isn't a serious error, so we skip this block, and continue with others */
     219           3 :                                 continue;
     220             :                         }
     221             : 
     222       13341 :                         read_size = handle_read(&handle[j], file_pos, buffer, state->block_size, log_fatal, 0);
     223       13341 :                         if (read_size == -1) {
     224             :                                 /* LCOV_EXCL_START */
     225             :                                 if (errno == EIO) {
     226             :                                         log_tag("error:%u:%s:%s: Read EIO error at position %u. %s\n", i, disk->name, esc(file->sub), file_pos, strerror(errno));
     227             :                                         log_fatal("DANGER! Unexpected input/output read error in a data disk, it isn't possible to sync.\n");
     228             :                                         log_fatal("Ensure that disk '%s' is sane and that file '%s' can be read.\n", disk->dir, handle[j].path);
     229             :                                         log_fatal("Stopping at block %u\n", i);
     230             :                                         ++io_error;
     231             :                                         goto bail;
     232             :                                 }
     233             : 
     234             :                                 log_tag("error:%u:%s:%s: Read error at position %u. %s\n", i, disk->name, esc(file->sub), file_pos, strerror(errno));
     235             :                                 log_fatal("WARNING! Unexpected read error in a data disk, it isn't possible to sync.\n");
     236             :                                 log_fatal("Ensure that file '%s' can be read.\n", handle[j].path);
     237             :                                 log_fatal("Stopping to allow recovery. Try with 'snapraid check -f %s'\n", file->sub);
     238             :                                 ++error;
     239             :                                 goto bail;
     240             :                                 /* LCOV_EXCL_STOP */
     241             :                         }
     242             : 
     243             :                         /* until now is disk */
     244       13341 :                         state_usage_disk(state, disk);
     245             : 
     246       13341 :                         countsize += read_size;
     247             : 
     248             :                         /* now compute the hash */
     249       13341 :                         if (rehash) {
     250           0 :                                 memhash(state->prevhash, state->prevhashseed, hash, buffer, read_size);
     251             :                         } else {
     252       13341 :                                 memhash(state->hash, state->hashseed, hash, buffer, read_size);
     253             :                         }
     254             : 
     255       13341 :                         if (block_state == BLOCK_STATE_REP) {
     256             :                                 /* compare the hash */
     257       13339 :                                 if (memcmp(hash, block->hash, HASH_SIZE) != 0) {
     258           1 :                                         log_tag("error:%u:%s:%s: Unexpected data change\n", i, disk->name, esc(file->sub));
     259           1 :                                         log_error("Data change at file '%s' at position '%u'\n", handle[j].path, file_pos);
     260           1 :                                         log_error("WARNING! Unexpected data modification of a file without parity!\n");
     261             : 
     262           1 :                                         if (file_flag_has(file, FILE_IS_COPY)) {
     263           1 :                                                 log_error("This file was detected as a copy of another file with the same name, size,\n");
     264           1 :                                                 log_error("and timestamp, but the file data isn't matching the assumed copy.\n");
     265           1 :                                                 log_error("If this is a false positive, and the files are expected to be different,\n");
     266           1 :                                                 log_error("you can 'sync' anyway using 'snapraid --force-nocopy sync'\n");
     267             :                                         } else {
     268           0 :                                                 log_error("Try removing the file from the array and rerun the 'sync' command!\n");
     269             :                                         }
     270             : 
     271             :                                         /* block sync to allow a recovery before overwriting */
     272             :                                         /* the parity needed to make such recovery */
     273           1 :                                         *skip_sync = 1; /* avoid to run the next sync */
     274             : 
     275           1 :                                         ++error;
     276           1 :                                         continue;
     277             :                                 }
     278             :                         } else {
     279             :                                 /* the only other case is BLOCK_STATE_CHG */
     280           2 :                                 assert(block_state == BLOCK_STATE_CHG);
     281             : 
     282             :                                 /* copy the hash in the block */
     283           2 :                                 memcpy(block->hash, hash, HASH_SIZE);
     284             : 
     285             :                                 /* and mark the block as hashed */
     286           2 :                                 block_state_set(block, BLOCK_STATE_REP);
     287             : 
     288             :                                 /* mark the state as needing write */
     289           2 :                                 state->need_write = 1;
     290             :                         }
     291             : 
     292             :                         /* count the number of processed block */
     293       13340 :                         ++countpos;
     294             : 
     295             :                         /* progress */
     296       13340 :                         if (state_progress(state, i, countpos, countmax, countsize)) {
     297             :                                 /* LCOV_EXCL_START */
     298             :                                 *skip_sync = 1; /* avoid to run the next sync */
     299             :                                 break;
     300             :                                 /* LCOV_EXCL_STOP */
     301             :                         }
     302             :                 }
     303             : 
     304             :                 /* close the last file in the disk */
     305          42 :                 if (handle[j].file != 0) {
     306             :                         /* keep a pointer at the file we are going to close for error reporting */
     307           7 :                         struct snapraid_file* report = handle[j].file;
     308           7 :                         ret = handle_close(&handle[j]);
     309           7 :                         if (ret == -1) {
     310             :                                 /* LCOV_EXCL_START */
     311             :                                 /* This one is really an unexpected error, because we are only reading */
     312             :                                 /* and closing a descriptor should never fail */
     313             :                                 if (errno == EIO) {
     314             :                                         log_tag("error:%u:%s:%s: Close EIO error. %s\n", blockmax, disk->name, esc(report->sub), strerror(errno));
     315             :                                         log_fatal("DANGER! Unexpected input/output close error in a data disk, it isn't possible to sync.\n");
     316             :                                         log_fatal("Ensure that disk '%s' is sane and that file '%s' can be accessed.\n", disk->dir, handle[j].path);
     317             :                                         log_fatal("Stopping at block %u\n", blockmax);
     318             :                                         ++io_error;
     319             :                                         goto bail;
     320             :                                 }
     321             : 
     322             :                                 log_tag("error:%u:%s:%s: Close error. %s\n", blockmax, disk->name, esc(report->sub), strerror(errno));
     323             :                                 log_fatal("WARNING! Unexpected close error in a data disk, it isn't possible to sync.\n");
     324             :                                 log_fatal("Ensure that file '%s' can be accessed.\n", handle[j].path);
     325             :                                 log_fatal("Stopping at block %u\n", blockmax);
     326             :                                 ++error;
     327             :                                 goto bail;
     328             :                                 /* LCOV_EXCL_STOP */
     329             :                         }
     330             :                 }
     331             :         }
     332             : 
     333             : end:
     334           7 :         state_progress_end(state, countpos, countmax, countsize);
     335             : 
     336             :         /* note that at this point no io_error is possible */
     337             :         /* becasue at the first one we bail out */
     338           7 :         assert(io_error == 0);
     339             : 
     340           7 :         if (error) {
     341           6 :                 msg_status("\n");
     342           6 :                 msg_status("%8u file errors\n", error);
     343             :         } else {
     344             :                 /* print the result only if processed something */
     345           1 :                 if (countpos != 0)
     346           1 :                         msg_status("Everything OK\n");
     347             :         }
     348             : 
     349           7 :         if (error)
     350           6 :                 log_fatal("WARNING! Unexpected file errors!\n");
     351             : 
     352           7 :         log_tag("hash_summary:error_file:%u\n", error);
     353             : 
     354             :         /* proceed without bailing out */
     355           7 :         goto finish;
     356             : 
     357             : bail:
     358             :         /* on bail, don't run the next sync */
     359           0 :         *skip_sync = 1;
     360             : 
     361             :         /* close files left open */
     362           0 :         for (j = 0; j < diskmax; ++j) {
     363           0 :                 struct snapraid_file* file = handle[j].file;
     364           0 :                 struct snapraid_disk* disk = handle[j].disk;
     365           0 :                 ret = handle_close(&handle[j]);
     366           0 :                 if (ret == -1) {
     367           0 :                         log_tag("error:%u:%s:%s: Close error. %s\n", i, disk->name, esc(file->sub), strerror(errno));
     368           0 :                         log_fatal("DANGER! Unexpected close error in a data disk.\n");
     369           0 :                         ++error;
     370             :                         /* continue, as we are already exiting */
     371             :                 }
     372             :         }
     373             : 
     374             : finish:
     375           7 :         free(handle);
     376           7 :         free(buffer);
     377             : 
     378           7 :         if (error + io_error != 0)
     379           6 :                 return -1;
     380           1 :         return 0;
     381             : }
     382             : 
     383             : /****************************************************************************/
     384             : /* sync */
     385             : 
     386             : /**
     387             :  * A block that failed the hash check, or that was deleted.
     388             :  */
     389             : struct failed_struct {
     390             :         unsigned index; /**< Index of the failed block. */
     391             :         unsigned size; /**< Size of the block. */
     392             : 
     393             :         struct snapraid_block* block; /**< The failed block, or BLOCK_DELETED for a deleted block */
     394             : };
     395             : 
     396             : /**
     397             :  * Buffer for storing the new hashes.
     398             :  */
     399             : struct snapraid_rehash {
     400             :         unsigned char hash[HASH_SIZE];
     401             :         struct snapraid_block* block;
     402             : };
     403             : 
     404             : /**
     405             :  * Check if we have to process the specified block index ::i.
     406             :  */
     407      773300 : static int block_is_enabled(block_off_t i, struct snapraid_handle* handle, unsigned diskmax)
     408             : {
     409             :         unsigned j;
     410             :         int one_invalid;
     411             :         int one_valid;
     412             : 
     413             :         /* for each disk */
     414      773300 :         one_invalid = 0;
     415      773300 :         one_valid = 0;
     416     5413100 :         for (j = 0; j < diskmax; ++j) {
     417             :                 struct snapraid_block* block;
     418             : 
     419             :                 /* if no disk, nothing to check */
     420     4639800 :                 if (!handle[j].disk)
     421        9374 :                         continue;
     422             : 
     423     4630426 :                 block = fs_par2block_get(handle[j].disk, i);
     424             : 
     425     4630426 :                 if (block_has_file(block))
     426     4410490 :                         one_valid = 1;
     427             : 
     428     4630426 :                 if (block_has_invalid_parity(block))
     429      470272 :                         one_invalid = 1;
     430             :         }
     431             : 
     432             :         /* if none valid or none invalid, we don't need to update */
     433      773300 :         if (!one_invalid || !one_valid)
     434      590814 :                 return 0;
     435             : 
     436      182486 :         return 1;
     437             : }
     438             : 
     439          78 : static int state_sync_process(struct snapraid_state* state, struct snapraid_parity_handle** parity, block_off_t blockstart, block_off_t blockmax)
     440             : {
     441             :         struct snapraid_handle* handle;
     442             :         void* rehandle_alloc;
     443             :         struct snapraid_rehash* rehandle;
     444             :         unsigned diskmax;
     445             :         block_off_t i;
     446             :         unsigned j;
     447             :         void* buffer_alloc;
     448             :         void** buffer;
     449             :         unsigned buffermax;
     450             :         data_off_t countsize;
     451             :         block_off_t countpos;
     452             :         block_off_t countmax;
     453             :         block_off_t autosavedone;
     454             :         block_off_t autosavelimit;
     455             :         block_off_t autosavemissing;
     456             :         int ret;
     457             :         unsigned error;
     458             :         unsigned silent_error;
     459             :         unsigned io_error;
     460             :         time_t now;
     461             :         struct failed_struct* failed;
     462             :         int* failed_map;
     463             :         unsigned l;
     464             : 
     465             :         /* the sync process assumes that all the hashes are correct */
     466             :         /* including the ones from CHG and DELETED blocks */
     467          78 :         assert(state->clear_past_hash != 0);
     468             : 
     469             :         /* get the present time */
     470          78 :         now = time(0);
     471             : 
     472             :         /* maps the disks to handles */
     473          78 :         handle = handle_map(state, &diskmax);
     474             : 
     475             :         /* rehash buffers */
     476          78 :         rehandle = malloc_nofail_align(diskmax * sizeof(struct snapraid_rehash), &rehandle_alloc);
     477             : 
     478             :         /* we need 2 * data + 1 * parity + 1 * zero */
     479          78 :         buffermax = 2 * diskmax + state->level + 1;
     480             : 
     481          78 :         buffer = malloc_nofail_vector_align(diskmax, buffermax, state->block_size, &buffer_alloc);
     482          78 :         if (!state->opt.skip_self)
     483           0 :                 mtest_vector(buffermax, state->block_size, buffer);
     484             : 
     485             :         /* fill up the zero buffer */
     486          78 :         memset(buffer[buffermax - 1], 0, state->block_size);
     487          78 :         raid_zero(buffer[buffermax - 1]);
     488             : 
     489          78 :         failed = malloc_nofail(diskmax * sizeof(struct failed_struct));
     490          78 :         failed_map = malloc_nofail(diskmax * sizeof(unsigned));
     491             : 
     492          78 :         error = 0;
     493          78 :         silent_error = 0;
     494          78 :         io_error = 0;
     495             : 
     496             :         /* first count the number of blocks to process */
     497          78 :         countmax = 0;
     498      386728 :         for (i = blockstart; i < blockmax; ++i) {
     499      386650 :                 if (!block_is_enabled(i, handle, diskmax))
     500      295407 :                         continue;
     501       91243 :                 ++countmax;
     502             :         }
     503             : 
     504             :         /* compute the autosave size for all disk, even if not read */
     505             :         /* this makes sense because the speed should be almost the same */
     506             :         /* if the disks are read in parallel */
     507          78 :         autosavelimit = state->autosave / (diskmax * state->block_size);
     508          78 :         autosavemissing = countmax; /* blocks to do */
     509          78 :         autosavedone = 0; /* blocks done */
     510             : 
     511             :         /* drop until now */
     512          78 :         state_usage_waste(state);
     513             : 
     514          78 :         countsize = 0;
     515          78 :         countpos = 0;
     516          78 :         if (!state_progress_begin(state, blockstart, blockmax, countmax))
     517           0 :                 goto end;
     518             : 
     519      386728 :         for (i = blockstart; i < blockmax; ++i) {
     520             :                 unsigned failed_count;
     521             :                 int error_on_this_block;
     522             :                 int silent_error_on_this_block;
     523             :                 int io_error_on_this_block;
     524             :                 int fixed_error_on_this_block;
     525             :                 int parity_needs_to_be_updated;
     526             :                 snapraid_info info;
     527             :                 int rehash;
     528             : 
     529      386650 :                 if (!block_is_enabled(i, handle, diskmax))
     530      295407 :                         continue;
     531             : 
     532             :                 /* one more block processed for autosave */
     533       91243 :                 ++autosavedone;
     534       91243 :                 --autosavemissing;
     535             : 
     536             :                 /* by default process the block, and skip it if something go wrong */
     537       91243 :                 error_on_this_block = 0;
     538       91243 :                 silent_error_on_this_block = 0;
     539       91243 :                 io_error_on_this_block = 0;
     540       91243 :                 fixed_error_on_this_block = 0;
     541             : 
     542             :                 /* keep track of the number of failed blocks */
     543       91243 :                 failed_count = 0;
     544             : 
     545             :                 /* get block specific info */
     546       91243 :                 info = info_get(&state->infoarr, i);
     547             : 
     548             :                 /* if we have to use the old hash */
     549       91243 :                 rehash = info_get_rehash(info);
     550             : 
     551             :                 /* it could happens that all the blocks are EMPTY/BLK and CHG but with the hash */
     552             :                 /* still matching because the specific CHG block was not modified. */
     553             :                 /* Note that CHG/DELETED blocks already present in the content file loaded */
     554             :                 /* have the hash cleared (::clear_past_hash flag), and then they won't never match the hash. */
     555             :                 /* We are treating only CHG blocks created at runtime. */
     556             :                 /* In such case, we can avoid to update parity, because it would be the same as before */
     557       91243 :                 parity_needs_to_be_updated = state->opt.force_parity_update;
     558             : 
     559             :                 /* if the block is marked as bad, we force the parity update */
     560             :                 /* because the bad block may be the result of a wrong parity */
     561       91243 :                 if (info_get_bad(info))
     562           0 :                         parity_needs_to_be_updated = 1;
     563             : 
     564             :                 /* for each disk, process the block */
     565      638701 :                 for (j = 0; j < diskmax; ++j) {
     566             :                         int read_size;
     567             :                         unsigned char hash[HASH_SIZE];
     568             :                         struct snapraid_block* block;
     569             :                         unsigned block_state;
     570      547458 :                         struct snapraid_disk* disk = handle[j].disk;
     571             :                         struct snapraid_file* file;
     572             :                         block_off_t file_pos;
     573             : 
     574             :                         /* by default no rehash in case of "continue" */
     575      547458 :                         rehandle[j].block = 0;
     576             : 
     577             :                         /* if the disk position is not used */
     578      547458 :                         if (!disk) {
     579             :                                 /* use an empty block */
     580           0 :                                 memset(buffer[j], 0, state->block_size);
     581       91560 :                                 continue;
     582             :                         }
     583             : 
     584             :                         /* get the block */
     585      547458 :                         block = fs_par2block_get(disk, i);
     586             : 
     587             :                         /* get the state of the block */
     588      547458 :                         block_state = block_state_get(block);
     589             : 
     590             :                         /* if the block has invalid parity, */
     591             :                         /* we have to take care of it in case of recover */
     592      547458 :                         if (block_has_invalid_parity(block)) {
     593             :                                 /* store it in the failed set, because */
     594             :                                 /* the parity may be still computed with the previous content */
     595      235136 :                                 failed[failed_count].index = j;
     596      235136 :                                 failed[failed_count].size = state->block_size;
     597      235136 :                                 failed[failed_count].block = block;
     598      235136 :                                 ++failed_count;
     599             : 
     600             :                                 /* if the block has invalid parity, we have to update the parity */
     601             :                                 /* to include this block change */
     602             :                                 /* This also apply to CHG blocks, but we are going to handle */
     603             :                                 /* later this case to do the updates only if really needed */
     604      235136 :                                 if (block_state != BLOCK_STATE_CHG)
     605      140613 :                                         parity_needs_to_be_updated = 1;
     606             : 
     607             :                                 /* note that DELETE blocks are skipped in the next check */
     608             :                                 /* and we have to store them in the failed blocks */
     609             :                                 /* before skipping */
     610             : 
     611             :                                 /* follow */
     612             :                         }
     613             : 
     614             :                         /* if the block has no file, meanining that it's EMPTY or DELETED, */
     615             :                         /* it doesn't partecipate in the new parity computation */
     616      547458 :                         if (!block_has_file(block)) {
     617             :                                 /* use an empty block */
     618       90689 :                                 memset(buffer[j], 0, state->block_size);
     619       90689 :                                 continue;
     620             :                         }
     621             : 
     622             :                         /* get the file of this block */
     623      456769 :                         file = fs_par2file_get(disk, i, &file_pos);
     624             : 
     625             :                         /* until now is CPU */
     626      456769 :                         state_usage_cpu(state);
     627             : 
     628             :                         /* if the file is different than the current one, close it */
     629      456769 :                         if (handle[j].file != 0 && handle[j].file != file) {
     630             :                                 /* keep a pointer at the file we are going to close for error reporting */
     631      189414 :                                 struct snapraid_file* report = handle[j].file;
     632      189414 :                                 ret = handle_close(&handle[j]);
     633      189414 :                                 if (ret == -1) {
     634             :                                         /* LCOV_EXCL_START */
     635             :                                         /* This one is really an unexpected error, because we are only reading */
     636             :                                         /* and closing a descriptor should never fail */
     637             :                                         if (errno == EIO) {
     638             :                                                 log_tag("error:%u:%s:%s: Close EIO error. %s\n", i, disk->name, esc(report->sub), strerror(errno));
     639             :                                                 log_fatal("DANGER! Unexpected input/output close error in a data disk, it isn't possible to sync.\n");
     640             :                                                 log_fatal("Ensure that disk '%s' is sane and that file '%s' can be accessed.\n", disk->dir, handle[j].path);
     641             :                                                 log_fatal("Stopping at block %u\n", i);
     642             :                                                 ++io_error;
     643             :                                                 goto bail;
     644             :                                         }
     645             : 
     646             :                                         log_tag("error:%u:%s:%s: Close error. %s\n", i, disk->name, esc(report->sub), strerror(errno));
     647             :                                         log_fatal("WARNING! Unexpected close error in a data disk, it isn't possible to sync.\n");
     648             :                                         log_fatal("Ensure that file '%s' can be accessed.\n", handle[j].path);
     649             :                                         log_fatal("Stopping at block %u\n", i);
     650             :                                         ++error;
     651             :                                         goto bail;
     652             :                                         /* LCOV_EXCL_STOP */
     653             :                                 }
     654             :                         }
     655             : 
     656      456769 :                         ret = handle_open(&handle[j], file, state->file_mode, log_error, 0);
     657      456769 :                         if (ret == -1) {
     658          12 :                                 if (errno == EIO) {
     659             :                                         /* LCOV_EXCL_START */
     660             :                                         log_tag("error:%u:%s:%s: Open EIO error. %s\n", i, disk->name, esc(file->sub), strerror(errno));
     661             :                                         log_fatal("DANGER! Unexpected input/output open error in a data disk, it isn't possible to sync.\n");
     662             :                                         log_fatal("Ensure that disk '%s' is sane and that file '%s' can be accessed.\n", disk->dir, handle[j].path);
     663             :                                         log_fatal("Stopping at block %u\n", i);
     664             :                                         ++io_error;
     665             :                                         goto bail;
     666             :                                         /* LCOV_EXCL_STOP */
     667             :                                 }
     668             : 
     669          12 :                                 if (errno == ENOENT) {
     670           4 :                                         log_tag("error:%u:%s:%s: Open ENOENT error. %s\n", i, disk->name, esc(file->sub), strerror(errno));
     671           4 :                                         log_error("Missing file '%s'.\n", handle[j].path);
     672           4 :                                         log_error("WARNING! You cannot modify data disk during a sync.\n");
     673           4 :                                         log_error("Rerun the sync command when finished.\n");
     674           4 :                                         ++error;
     675             :                                         /* if the file is missing, it means that it was removed during sync */
     676             :                                         /* this isn't a serious error, so we skip this block, and continue with others */
     677           4 :                                         error_on_this_block = 1;
     678           4 :                                         continue;
     679             :                                 }
     680             : 
     681           8 :                                 if (errno == EACCES) {
     682           8 :                                         log_tag("error:%u:%s:%s: Open EACCES error. %s\n", i, disk->name, esc(file->sub), strerror(errno));
     683           8 :                                         log_error("No access at file '%s'.\n", handle[j].path);
     684           8 :                                         log_error("WARNING! Please fix the access permission in the data disk.\n");
     685           8 :                                         log_error("Rerun the sync command when finished.\n");
     686           8 :                                         ++error;
     687             :                                         /* this isn't a serious error, so we skip this block, and continue with others */
     688           8 :                                         error_on_this_block = 1;
     689           8 :                                         continue;
     690             :                                 }
     691             : 
     692             :                                 /* LCOV_EXCL_START */
     693             :                                 log_tag("error:%u:%s:%s: Open error. %s\n", i, disk->name, esc(file->sub), strerror(errno));
     694             :                                 log_fatal("WARNING! Unexpected open error in a data disk, it isn't possible to sync.\n");
     695             :                                 log_fatal("Ensure that file '%s' can be accessed.\n", handle[j].path);
     696             :                                 log_fatal("Stopping to allow recovery. Try with 'snapraid check -f %s'\n", file->sub);
     697             :                                 ++error;
     698             :                                 goto bail;
     699             :                                 /* LCOV_EXCL_STOP */
     700             :                         }
     701             : 
     702             :                         /* check if the file is changed */
     703      456757 :                         if (handle[j].st.st_size != file->size
     704      456755 :                                 || handle[j].st.st_mtime != file->mtime_sec
     705      456753 :                                 || STAT_NSEC(&handle[j].st) != file->mtime_nsec
     706      456753 :                                 || handle[j].st.st_ino != file->inode
     707             :                         ) {
     708           6 :                                 log_tag("error:%u:%s:%s: Unexpected attribute change\n", i, disk->name, esc(file->sub));
     709           6 :                                 if (handle[j].st.st_size != file->size) {
     710           2 :                                         log_error("Unexpected size change at file '%s' from %" PRIu64 " to %" PRIu64 ".\n", handle[j].path, file->size, handle[j].st.st_size);
     711           4 :                                 } else if (handle[j].st.st_mtime != file->mtime_sec
     712           2 :                                         || STAT_NSEC(&handle[j].st) != file->mtime_nsec) {
     713           2 :                                         log_error("Unexpected time change at file '%s' from %" PRIu64 ".%d to %" PRIu64 ".%d.\n", handle[j].path, file->mtime_sec, file->mtime_nsec, (uint64_t)handle[j].st.st_mtime, (uint32_t)STAT_NSEC(&handle[j].st));
     714             :                                 } else {
     715           2 :                                         log_error("Unexpected inode change from %" PRIu64 " to %" PRIu64 " at file '%s'.\n", file->inode, (uint64_t)handle[j].st.st_ino, handle[j].path);
     716             :                                 }
     717           6 :                                 log_error("WARNING! You cannot modify files during a sync.\n");
     718           6 :                                 log_error("Rerun the sync command when finished.\n");
     719           6 :                                 ++error;
     720             :                                 /* if the file is changed, it means that it was modified during sync */
     721             :                                 /* this isn't a serious error, so we skip this block, and continue with others */
     722           6 :                                 error_on_this_block = 1;
     723           6 :                                 continue;
     724             :                         }
     725             : 
     726      456751 :                         read_size = handle_read(&handle[j], file_pos, buffer[j], state->block_size, log_error, 0);
     727      456751 :                         if (read_size == -1) {
     728             :                                 /* LCOV_EXCL_START */
     729             :                                 if (errno == EIO) {
     730             :                                         log_tag("error:%u:%s:%s: Read EIO error at position %u. %s\n", i, disk->name, esc(file->sub), file_pos, strerror(errno));
     731             :                                         if (io_error >= state->opt.io_error_limit) {
     732             :                                                 log_fatal("DANGER! Unexpected input/output read error in a data disk, it isn't possible to sync.\n");
     733             :                                                 log_fatal("Ensure that disk '%s' is sane and that file '%s' can be read.\n", disk->dir, handle[j].path);
     734             :                                                 log_fatal("Stopping at block %u\n", i);
     735             :                                                 ++io_error;
     736             :                                                 goto bail;
     737             :                                         }
     738             : 
     739             :                                         log_error("Input/Output error in file '%s' at position '%u'\n", handle[j].path, file_pos);
     740             :                                         ++io_error;
     741             :                                         io_error_on_this_block = 1;
     742             :                                         continue;
     743             :                                 }
     744             : 
     745             :                                 log_tag("error:%u:%s:%s: Read error at position %u. %s\n", i, disk->name, esc(file->sub), file_pos, strerror(errno));
     746             :                                 log_fatal("WARNING! Unexpected read error in a data disk, it isn't possible to sync.\n");
     747             :                                 log_fatal("Ensure that file '%s' can be read.\n", handle[j].path);
     748             :                                 log_fatal("Stopping to allow recovery. Try with 'snapraid check -f %s'\n", file->sub);
     749             :                                 ++error;
     750             :                                 goto bail;
     751             :                                 /* LCOV_EXCL_STOP */
     752             :                         }
     753             : 
     754             :                         /* until now is disk */
     755      456751 :                         state_usage_disk(state, disk);
     756             : 
     757      456751 :                         countsize += read_size;
     758             : 
     759             :                         /* now compute the hash */
     760      456751 :                         if (rehash) {
     761       27243 :                                 memhash(state->prevhash, state->prevhashseed, hash, buffer[j], read_size);
     762             : 
     763             :                                 /* compute the new hash, and store it */
     764       27243 :                                 rehandle[j].block = block;
     765       27243 :                                 memhash(state->hash, state->hashseed, rehandle[j].hash, buffer[j], read_size);
     766             :                         } else {
     767      429508 :                                 memhash(state->hash, state->hashseed, hash, buffer[j], read_size);
     768             :                         }
     769             : 
     770      456751 :                         if (block_has_updated_hash(block)) {
     771             :                                 /* compare the hash */
     772      362246 :                                 if (memcmp(hash, block->hash, HASH_SIZE) != 0) {
     773             :                                         /* if the file has invalid parity, it's a REP changed during the sync */
     774         853 :                                         if (block_has_invalid_parity(block)) {
     775           1 :                                                 log_tag("error:%u:%s:%s: Unexpected data change\n", i, disk->name, esc(file->sub));
     776           1 :                                                 log_error("Data change at file '%s' at position '%u'\n", handle[j].path, file_pos);
     777           1 :                                                 log_error("WARNING! Unexpected data modification of a file without parity!\n");
     778             : 
     779           1 :                                                 if (file_flag_has(file, FILE_IS_COPY)) {
     780           0 :                                                         log_error("This file was detected as a copy of another file with the same name, size,\n");
     781           0 :                                                         log_error("and timestamp, but the file data isn't matching the assumed copy.\n");
     782           0 :                                                         log_error("If this is a false positive, and the files are expected to be different,\n");
     783           0 :                                                         log_error("you can 'sync' anyway using 'snapraid --force-nocopy sync'\n");
     784             :                                                 } else {
     785           1 :                                                         log_error("Try removing the file from the array and rerun the 'sync' command!\n");
     786             :                                                 }
     787             : 
     788           1 :                                                 ++error;
     789             : 
     790             :                                                 /* if the file is changed, it means that it was modified during sync */
     791             :                                                 /* this isn't a serious error, so we skip this block, and continue with others */
     792           1 :                                                 error_on_this_block = 1;
     793           1 :                                                 continue;
     794             :                                         } else { /* otherwise it's a BLK with silent error */
     795         852 :                                                 unsigned diff = memdiff(hash, block->hash, HASH_SIZE);
     796         852 :                                                 log_tag("error:%u:%s:%s: Data error at position %u, diff bits %u\n", i, disk->name, esc(file->sub), file_pos, diff);
     797         852 :                                                 log_error("Data error in file '%s' at position '%u', diff bits %u\n", handle[j].path, file_pos, diff);
     798             : 
     799             :                                                 /* save the failed block for the fix */
     800         852 :                                                 failed[failed_count].index = j;
     801         852 :                                                 failed[failed_count].size = read_size;
     802         852 :                                                 failed[failed_count].block = block;
     803         852 :                                                 ++failed_count;
     804             : 
     805             :                                                 /* silent errors are very rare, and are not a signal that a disk */
     806             :                                                 /* is going to fail. So, we just continue marking the block as bad */
     807             :                                                 /* just like in scrub */
     808         852 :                                                 ++silent_error;
     809         852 :                                                 silent_error_on_this_block = 1;
     810         852 :                                                 continue;
     811             :                                         }
     812             :                                 }
     813             :                         } else {
     814             :                                 /* if until now the parity doesn't need to be updated */
     815       94505 :                                 if (!parity_needs_to_be_updated) {
     816             :                                         /* for sure it's a CHG block, because EMPTY are processed before with "continue" */
     817             :                                         /* and BLK and REP have "block_has_updated_hash()" as 1, and all the others */
     818             :                                         /* have "parity_needs_to_be_updated" already at 1 */
     819       35168 :                                         assert(block_state_get(block) == BLOCK_STATE_CHG);
     820             : 
     821             :                                         /* if there is an hash */
     822       35168 :                                         if (hash_is_real(block->hash)) {
     823             :                                                 /* check if the hash is changed */
     824        8494 :                                                 if (memcmp(hash, block->hash, HASH_SIZE) != 0) {
     825             :                                                         /* the block is different, and we must update parity */
     826        6000 :                                                         parity_needs_to_be_updated = 1;
     827             :                                                 }
     828             :                                         } else {
     829             :                                                 /* if the hash is already invalid, we update parity */
     830       26674 :                                                 parity_needs_to_be_updated = 1;
     831             :                                         }
     832             :                                 }
     833             : 
     834             :                                 /* copy the hash in the block, but doesn't mark the block as hashed */
     835             :                                 /* this allow in case of skipped block to do not save the failed computation */
     836       94505 :                                 memcpy(block->hash, hash, HASH_SIZE);
     837             : 
     838             :                                 /* note that in case of rehash, this is the wrong hash, */
     839             :                                 /* but it will be overwritten later */
     840             :                         }
     841             :                 }
     842             : 
     843             :                 /* if we have only silent errors we can try to fix them on-the-fly */
     844             :                 /* note the the fix is not written to disk, but used only to */
     845             :                 /* compute the new parity */
     846       91243 :                 if (!error_on_this_block && !io_error_on_this_block && silent_error_on_this_block) {
     847             :                         unsigned failed_mac;
     848         852 :                         int something_to_recover = 0;
     849             : 
     850             :                         /* setup the blocks to recover */
     851         852 :                         failed_mac = 0;
     852        2556 :                         for (j = 0; j < failed_count; ++j) {
     853        1704 :                                 unsigned char* block_buffer = buffer[failed[j].index];
     854        1704 :                                 unsigned char* block_copy = buffer[diskmax + state->level + failed[j].index];
     855        1704 :                                 unsigned block_state = block_state_get(failed[j].block);
     856             : 
     857             :                                 /* we try to recover only if at least one BLK is present */
     858        1704 :                                 if (block_state == BLOCK_STATE_BLK)
     859         852 :                                         something_to_recover = 1;
     860             : 
     861             :                                 /* save a copy of the content just read */
     862             :                                 /* that it's going to be overwritten by the recovering function */
     863        1704 :                                 memcpy(block_copy, block_buffer, state->block_size);
     864             : 
     865        1704 :                                 if (block_state == BLOCK_STATE_CHG
     866         852 :                                         && hash_is_zero(failed[j].block->hash)
     867             :                                 ) {
     868             :                                         /* if the block was filled with 0, restore this state */
     869             :                                         /* and avoid to recover it */
     870           0 :                                         memset(block_buffer, 0, state->block_size);
     871             :                                 } else {
     872             :                                         /* if we have too many failures, we cannot recover */
     873        1704 :                                         if (failed_mac >= state->level)
     874           0 :                                                 break;
     875             : 
     876             :                                         /* otherwise it has to be recovered */
     877        1704 :                                         failed_map[failed_mac++] = failed[j].index;
     878             :                                 }
     879             :                         }
     880             : 
     881             :                         /* if we have something to recover and enough parity */
     882         852 :                         if (something_to_recover && j == failed_count) {
     883             :                                 /* until now is CPU */
     884         852 :                                 state_usage_cpu(state);
     885             : 
     886             :                                 /* read the parity */
     887             :                                 /* we are sure that parity exists because */
     888             :                                 /* we have at least one BLK block */
     889        5964 :                                 for (l = 0; l < state->level; ++l) {
     890        5112 :                                         ret = parity_read(parity[l], i, buffer[diskmax + l], state->block_size, log_error);
     891        5112 :                                         if (ret == -1) {
     892             :                                                 /* LCOV_EXCL_START */
     893             :                                                 if (errno == EIO) {
     894             :                                                         log_tag("parity_error:%u:%s: Read EIO error. %s\n", i, lev_config_name(l), strerror(errno));
     895             :                                                         if (io_error >= state->opt.io_error_limit) {
     896             :                                                                 log_fatal("DANGER! Unexpected input/output read error in the %s disk, it isn't possible to sync.\n", lev_name(l));
     897             :                                                                 log_fatal("Ensure that disk '%s' is sane and can be read.\n", lev_config_name(l));
     898             :                                                                 log_fatal("Stopping at block %u\n", i);
     899             :                                                                 ++io_error;
     900             :                                                                 goto bail;
     901             :                                                         }
     902             : 
     903             :                                                         log_error("Input/Output error in parity '%s' at position '%u'\n", lev_config_name(l), i);
     904             :                                                         ++io_error;
     905             :                                                         io_error_on_this_block = 1;
     906             :                                                         continue;
     907             :                                                 }
     908             : 
     909             :                                                 log_tag("parity_error:%u:%s: Read error. %s\n", i, lev_config_name(l), strerror(errno));
     910             :                                                 log_fatal("WARNING! Unexpected read error in the %s disk, it isn't possible to sync.\n", lev_name(l));
     911             :                                                 log_fatal("Ensure that disk '%s' can be read.\n", lev_config_name(l));
     912             :                                                 log_fatal("Stopping at block %u\n", i);
     913             :                                                 ++error;
     914             :                                                 goto bail;
     915             :                                                 /* LCOV_EXCL_STOP */
     916             :                                         }
     917             : 
     918             :                                         /* until now is parity */
     919        5112 :                                         state_usage_parity(state, l);
     920             :                                 }
     921             : 
     922             :                                 /* if no error in parity read */
     923         852 :                                 if (!io_error_on_this_block) {
     924             :                                         /* try to fix the data */
     925             :                                         /* note that this is a simple fix algorithm, that doesn't take into */
     926             :                                         /* account the case of a wrong parity */
     927             :                                         /* only 'fix' supports the most advanced fixing */
     928         852 :                                         raid_rec(failed_mac, failed_map, diskmax, state->level, state->block_size, buffer);
     929             : 
     930             :                                         /* check the result and prepare the data */
     931        5112 :                                         for (j = 0; j < failed_count; ++j) {
     932             :                                                 unsigned char hash[HASH_SIZE];
     933        1704 :                                                 unsigned char* block_buffer = buffer[failed[j].index];
     934        1704 :                                                 unsigned char* block_copy = buffer[diskmax + state->level + failed[j].index];
     935        1704 :                                                 unsigned block_state = block_state_get(failed[j].block);
     936             : 
     937        1704 :                                                 if (block_state == BLOCK_STATE_BLK) {
     938         852 :                                                         unsigned size = failed[j].size;
     939             : 
     940             :                                                         /* compute the hash of the recovered block */
     941         852 :                                                         if (rehash) {
     942           0 :                                                                 memhash(state->prevhash, state->prevhashseed, hash, block_buffer, size);
     943             :                                                         } else {
     944         852 :                                                                 memhash(state->hash, state->hashseed, hash, block_buffer, size);
     945             :                                                         }
     946             : 
     947             :                                                         /* if the hash doesn't match */
     948         852 :                                                         if (memcmp(hash, failed[j].block->hash, HASH_SIZE) != 0) {
     949             :                                                                 /* we have not recovered */
     950           0 :                                                                 break;
     951             :                                                         }
     952             : 
     953             :                                                         /* pad with 0 if needed */
     954         852 :                                                         if (size < state->block_size)
     955         345 :                                                                 memset(block_buffer + size, 0, state->block_size - size);
     956             :                                                 } else {
     957             :                                                         /* otherwise restore the content */
     958             :                                                         /* because we are not interested in the old state */
     959             :                                                         /* that it's recovered for CHG, REP and DELETED blocks */
     960         852 :                                                         memcpy(block_buffer, block_copy, state->block_size);
     961             :                                                 }
     962             :                                         }
     963             : 
     964             :                                         /* if all is processed, we have fixed it */
     965         852 :                                         if (j == failed_count)
     966         852 :                                                 fixed_error_on_this_block = 1;
     967             :                                 }
     968             :                         }
     969             :                 }
     970             : 
     971             :                 /* if we have read all the data required and it's correct, proceed with the parity */
     972       91243 :                 if (!error_on_this_block && !io_error_on_this_block
     973       91224 :                         && (!silent_error_on_this_block || fixed_error_on_this_block)
     974             :                 ) {
     975             :                         /* update the parity only if really needed */
     976       91224 :                         if (parity_needs_to_be_updated) {
     977             :                                 /* compute the parity */
     978       88964 :                                 raid_gen(diskmax, state->level, state->block_size, buffer);
     979             : 
     980             :                                 /* until now is CPU */
     981       88964 :                                 state_usage_cpu(state);
     982             : 
     983             :                                 /* write the parity */
     984      586383 :                                 for (l = 0; l < state->level; ++l) {
     985      497419 :                                         ret = parity_write(parity[l], i, buffer[diskmax + l], state->block_size);
     986      497419 :                                         if (ret == -1) {
     987             :                                                 /* LCOV_EXCL_START */
     988             :                                                 if (errno == EIO) {
     989             :                                                         log_tag("parity_error:%u:%s: Write EIO error. %s\n", i, lev_config_name(l), strerror(errno));
     990             :                                                         if (io_error >= state->opt.io_error_limit) {
     991             :                                                                 log_fatal("DANGER! Unexpected input/output write error in the %s disk, it isn't possible to sync.\n", lev_name(l));
     992             :                                                                 log_fatal("Ensure that disk '%s' is sane and can be written.\n", lev_config_name(l));
     993             :                                                                 log_fatal("Stopping at block %u\n", i);
     994             :                                                                 ++io_error;
     995             :                                                                 goto bail;
     996             :                                                         }
     997             : 
     998             :                                                         log_error("Input/Output error in parity '%s' at position '%u'\n", lev_config_name(l), i);
     999             :                                                         ++io_error;
    1000             :                                                         io_error_on_this_block = 1;
    1001             :                                                         continue;
    1002             :                                                 }
    1003             : 
    1004             :                                                 log_tag("parity_error:%u:%s: Write error. %s\n", i, lev_config_name(l), strerror(errno));
    1005             :                                                 log_fatal("WARNING! Unexpected write error in the %s disk, it isn't possible to sync.\n", lev_name(l));
    1006             :                                                 log_fatal("Ensure that disk '%s' has some free space available.\n", lev_config_name(l));
    1007             :                                                 log_fatal("Stopping at block %u\n", i);
    1008             :                                                 ++error;
    1009             :                                                 goto bail;
    1010             :                                                 /* LCOV_EXCL_STOP */
    1011             :                                         }
    1012             : 
    1013             :                                         /* until now is parity */
    1014      497419 :                                         state_usage_parity(state, l);
    1015             :                                 }
    1016             :                         }
    1017             : 
    1018             :                         /* if no error in parity write */
    1019       91224 :                         if (!io_error_on_this_block) {
    1020             :                                 /* for each disk, mark the blocks as processed */
    1021      638568 :                                 for (j = 0; j < diskmax; ++j) {
    1022             :                                         struct snapraid_block* block;
    1023             : 
    1024      547344 :                                         if (!handle[j].disk)
    1025           0 :                                                 continue;
    1026             : 
    1027      547344 :                                         block = fs_par2block_get(handle[j].disk, i);
    1028             : 
    1029      547344 :                                         if (block == BLOCK_EMPTY) {
    1030             :                                                 /* nothing to do */
    1031       79734 :                                                 continue;
    1032             :                                         }
    1033             : 
    1034             :                                         /* if it's a deleted block */
    1035      467610 :                                         if (block_state_get(block) == BLOCK_STATE_DELETED) {
    1036             :                                                 /* the parity is now updated without this block, so it's now empty */
    1037       10953 :                                                 fs_deallocate(handle[j].disk, i);
    1038       10953 :                                                 continue;
    1039             :                                         }
    1040             : 
    1041             :                                         /* now all the blocks have the hash and the parity computed */
    1042      456657 :                                         block_state_set(block, BLOCK_STATE_BLK);
    1043             :                                 }
    1044             :                         }
    1045             : 
    1046             :                         /* we update the info block only if we really have updated the parity */
    1047             :                         /* because otherwise the time/justsynced info would be misleading as we didn't */
    1048             :                         /* wrote the parity at this time */
    1049             :                         /* we also update the info block only if no silent error was found */
    1050             :                         /* because has no sense to refresh the time for data that we know bad */
    1051       91224 :                         if (parity_needs_to_be_updated
    1052       88964 :                                 && !silent_error_on_this_block
    1053       88625 :                                 && !io_error_on_this_block
    1054             :                         ) {
    1055             :                                 /* if rehash is neeed */
    1056       88625 :                                 if (rehash) {
    1057             :                                         /* store all the new hash already computed */
    1058       32249 :                                         for (j = 0; j < diskmax; ++j) {
    1059       27642 :                                                 if (rehandle[j].block)
    1060       27243 :                                                         memcpy(rehandle[j].block->hash, rehandle[j].hash, HASH_SIZE);
    1061             :                                         }
    1062             :                                 }
    1063             : 
    1064             :                                 /* update the time info of the block */
    1065             :                                 /* we are also clearing any previous bad and rehash flag */
    1066       88625 :                                 info_set(&state->infoarr, i, info_make(now, 0, 0, 1));
    1067             :                         }
    1068             :                 }
    1069             : 
    1070             :                 /* if a silent (even if corrected) or input/output error was found */
    1071             :                 /* mark the block as bad to have check/fix to handle it */
    1072             :                 /* because our correction is in memory only and not yet written */
    1073       91243 :                 if (silent_error_on_this_block || io_error_on_this_block) {
    1074             :                         /* set the error status keeping the other info */
    1075         852 :                         info_set(&state->infoarr, i, info_set_bad(info));
    1076             :                 }
    1077             : 
    1078             :                 /* mark the state as needing write */
    1079       91243 :                 state->need_write = 1;
    1080             : 
    1081             :                 /* count the number of processed block */
    1082       91243 :                 ++countpos;
    1083             : 
    1084             :                 /* progress */
    1085       91243 :                 if (state_progress(state, i, countpos, countmax, countsize)) {
    1086             :                         /* LCOV_EXCL_START */
    1087             :                         break;
    1088             :                         /* LCOV_EXCL_STOP */
    1089             :                 }
    1090             : 
    1091             :                 /* autosave */
    1092       91243 :                 if ((state->autosave != 0
    1093        7273 :                         && autosavedone >= autosavelimit /* if we have reached the limit */
    1094           0 :                         && autosavemissing >= autosavelimit) /* if we have at least a full step to do */
    1095             :                         /* or if we have a forced autosave at the specified block */
    1096       91243 :                         || (state->opt.force_autosave_at != 0 && state->opt.force_autosave_at == i)
    1097             :                 ) {
    1098           1 :                         autosavedone = 0; /* restart the counter */
    1099             : 
    1100             :                         /* until now is CPU */
    1101           1 :                         state_usage_cpu(state);
    1102             : 
    1103           1 :                         state_progress_stop(state);
    1104             : 
    1105           1 :                         msg_progress("Autosaving...\n");
    1106             : 
    1107             :                         /* before writing the new content file we ensure that */
    1108             :                         /* the parity is really written flushing the disk cache */
    1109           7 :                         for (l = 0; l < state->level; ++l) {
    1110           6 :                                 ret = parity_sync(parity[l]);
    1111           6 :                                 if (ret == -1) {
    1112             :                                         /* LCOV_EXCL_START */
    1113             :                                         log_tag("parity_error:%u:%s: Sync error\n", i, lev_config_name(l));
    1114             :                                         log_fatal("DANGER! Unexpected sync error in %s disk.\n", lev_name(l));
    1115             :                                         log_fatal("Ensure that disk '%s' is sane.\n", lev_config_name(l));
    1116             :                                         log_fatal("Stopping at block %u\n", i);
    1117             :                                         ++error;
    1118             :                                         goto bail;
    1119             :                                         /* LCOV_EXCL_STOP */
    1120             :                                 }
    1121             :                         }
    1122             : 
    1123             :                         /* now we can safely write the content file */
    1124           1 :                         state_write(state);
    1125             : 
    1126           1 :                         state_progress_restart(state);
    1127             : 
    1128             :                         /* drop until now */
    1129           1 :                         state_usage_waste(state);
    1130             :                 }
    1131             :         }
    1132             : 
    1133             : end:
    1134          78 :         state_progress_end(state, countpos, countmax, countsize);
    1135             : 
    1136          78 :         state_usage_print(state);
    1137             : 
    1138             :         /* before returning we ensure that */
    1139             :         /* the parity is really written flushing the disk cache */
    1140         531 :         for (l = 0; l < state->level; ++l) {
    1141         453 :                 ret = parity_sync(parity[l]);
    1142         453 :                 if (ret == -1) {
    1143             :                         /* LCOV_EXCL_START */
    1144             :                         log_tag("parity_error:%u:%s: Sync error\n", i, lev_config_name(l));
    1145             :                         log_fatal("DANGER! Unexpected sync error in %s disk.\n", lev_name(l));
    1146             :                         log_fatal("Ensure that disk '%s' is sane.\n", lev_config_name(l));
    1147             :                         log_fatal("Stopping at block %u\n", i);
    1148             :                         ++error;
    1149             :                         goto bail;
    1150             :                         /* LCOV_EXCL_STOP */
    1151             :                 }
    1152             :         }
    1153             : 
    1154          78 :         if (error || silent_error || io_error) {
    1155          12 :                 msg_status("\n");
    1156          12 :                 msg_status("%8u file errors\n", error);
    1157          12 :                 msg_status("%8u io errors\n", io_error);
    1158          12 :                 msg_status("%8u data errors\n", silent_error);
    1159             :         } else {
    1160             :                 /* print the result only if processed something */
    1161          66 :                 if (countpos != 0)
    1162          48 :                         msg_status("Everything OK\n");
    1163             :         }
    1164             : 
    1165          78 :         if (error)
    1166          11 :                 log_fatal("WARNING! Unexpected file errors!\n");
    1167          78 :         if (io_error)
    1168           0 :                 log_fatal("DANGER! Unexpected input/output errors! The failing blocks are now marked as bad!\n");
    1169          78 :         if (silent_error)
    1170           1 :                 log_fatal("DANGER! Unexpected data errors! The failing blocks are now marked as bad!\n");
    1171          78 :         if (io_error || silent_error) {
    1172           1 :                 log_fatal("Use 'snapraid status' to list the bad blocks.\n");
    1173           1 :                 log_fatal("Use 'snapraid -e fix' to recover.\n");
    1174             :         }
    1175             : 
    1176          78 :         log_tag("summary:error_file:%u\n", error);
    1177          78 :         log_tag("summary:error_io:%u\n", io_error);
    1178          78 :         log_tag("summary:error_data:%u\n", silent_error);
    1179          78 :         if (error + silent_error + io_error == 0)
    1180          66 :                 log_tag("summary:exit:ok\n");
    1181             :         else
    1182          12 :                 log_tag("summary:exit:error\n");
    1183          78 :         log_flush();
    1184             : 
    1185             : bail:
    1186         546 :         for (j = 0; j < diskmax; ++j) {
    1187         468 :                 struct snapraid_file* file = handle[j].file;
    1188         468 :                 struct snapraid_disk* disk = handle[j].disk;
    1189         468 :                 ret = handle_close(&handle[j]);
    1190         468 :                 if (ret == -1) {
    1191             :                         /* LCOV_EXCL_START */
    1192             :                         log_tag("error:%u:%s:%s: Close error. %s\n", i, disk->name, esc(file->sub), strerror(errno));
    1193             :                         log_fatal("DANGER! Unexpected close error in a data disk.\n");
    1194             :                         ++error;
    1195             :                         /* continue, as we are already exiting */
    1196             :                         /* LCOV_EXCL_STOP */
    1197             :                 }
    1198             :         }
    1199             : 
    1200          78 :         free(handle);
    1201          78 :         free(buffer_alloc);
    1202          78 :         free(buffer);
    1203          78 :         free(rehandle_alloc);
    1204          78 :         free(failed);
    1205          78 :         free(failed_map);
    1206             : 
    1207          78 :         if (state->opt.expect_recoverable) {
    1208           1 :                 if (error + silent_error + io_error == 0)
    1209           0 :                         return -1;
    1210             :         } else {
    1211          77 :                 if (error + silent_error + io_error != 0)
    1212          11 :                         return -1;
    1213             :         }
    1214          67 :         return 0;
    1215             : }
    1216             : 
    1217          82 : int state_sync(struct snapraid_state* state, block_off_t blockstart, block_off_t blockcount)
    1218             : {
    1219             :         block_off_t blockmax;
    1220             :         block_off_t used_paritymax;
    1221             :         block_off_t file_paritymax;
    1222             :         data_off_t size;
    1223             :         data_off_t out_size;
    1224             :         int ret;
    1225             :         struct snapraid_parity_handle parity[LEV_MAX];
    1226             :         /* the following initialization is to avoid clang warnings about */
    1227             :         /* potential state->level change, that never happens */
    1228          82 :         struct snapraid_parity_handle* parity_ptr[LEV_MAX] = { 0 };
    1229             :         unsigned unrecoverable_error;
    1230             :         unsigned l;
    1231          82 :         int skip_sync = 0;
    1232             : 
    1233          82 :         msg_progress("Initializing...\n");
    1234             : 
    1235          82 :         blockmax = parity_allocated_size(state);
    1236          82 :         size = blockmax * (data_off_t)state->block_size;
    1237             : 
    1238             :         /* minum size of the parity files we expect */
    1239          82 :         used_paritymax = parity_used_size(state);
    1240             : 
    1241             :         /* effective size of the parity files */
    1242          82 :         file_paritymax = 0;
    1243             : 
    1244          82 :         if (blockstart > blockmax) {
    1245             :                 /* LCOV_EXCL_START */
    1246             :                 log_fatal("Error in the starting block %u. It's bigger than the parity size %u.\n", blockstart, blockmax);
    1247             :                 exit(EXIT_FAILURE);
    1248             :                 /* LCOV_EXCL_STOP */
    1249             :         }
    1250             : 
    1251             :         /* adjust the number of block to process */
    1252          82 :         if (blockcount != 0 && blockstart + blockcount < blockmax) {
    1253           6 :                 blockmax = blockstart + blockcount;
    1254             :         }
    1255             : 
    1256         559 :         for (l = 0; l < state->level; ++l) {
    1257             :                 block_off_t parityblocks;
    1258             : 
    1259             :                 /* create the file and open for writing */
    1260         477 :                 parity_ptr[l] = &parity[l];
    1261         477 :                 ret = parity_create(parity_ptr[l], state->parity[l].path, &out_size, state->file_mode);
    1262         477 :                 if (ret == -1) {
    1263             :                         /* LCOV_EXCL_START */
    1264             :                         log_fatal("WARNING! Without an accessible %s file, it isn't possible to sync.\n", lev_name(l));
    1265             :                         exit(EXIT_FAILURE);
    1266             :                         /* LCOV_EXCL_STOP */
    1267             :                 }
    1268             : 
    1269             :                 /* number of block in the parity file */
    1270         477 :                 parityblocks = out_size / state->block_size;
    1271             : 
    1272             :                 /* if the file is too small */
    1273         477 :                 if (parityblocks < used_paritymax) {
    1274           0 :                         log_fatal("WARNING! The %s file %s has data only %u blocks instead of %u.\n", lev_name(l), state->parity[l].path, parityblocks, used_paritymax);
    1275             :                 }
    1276             : 
    1277             :                 /* keep the smallest parity number of blocks */
    1278         477 :                 if (l == 0 || file_paritymax > parityblocks)
    1279          82 :                         file_paritymax = parityblocks;
    1280             :         }
    1281             : 
    1282             :         /* if we do a full sync, having a wrong parity size is expected */
    1283          82 :         if (!state->opt.force_full) {
    1284             :                 /* if the parities are too small */
    1285          80 :                 if (file_paritymax < used_paritymax) {
    1286             :                         /* LCOV_EXCL_START */
    1287             :                         log_fatal("DANGER! One or more the parity files are smaller than expected!\n");
    1288             :                         if (file_paritymax != 0) {
    1289             :                                 log_fatal("If this happens because you are using an old content file,\n");
    1290             :                                 log_fatal("you can 'sync' anyway using 'snapraid --force-full sync'\n");
    1291             :                                 log_fatal("to force a full rebuild of the parity.\n");
    1292             :                         } else {
    1293             :                                 log_fatal("It's possible that the parity disks are not mounted.\n");
    1294             :                                 log_fatal("If instead you are adding a new parity level, you can 'sync' using\n");
    1295             :                                 log_fatal("'snapraid --force-full sync' to force a full rebuild of the parity.\n");
    1296             :                         }
    1297             :                         exit(EXIT_FAILURE);
    1298             :                         /* LCOV_EXCL_STOP */
    1299             :                 }
    1300             :         }
    1301             : 
    1302             :         /* now change the size of all parities */
    1303         559 :         for (l = 0; l < state->level; ++l) {
    1304             :                 /* change the size of the parity file, truncating or extending it */
    1305             :                 /* from this point all the DELETED blocks after the end of the parity are invalid */
    1306             :                 /* and they are automatically removed when we save the new content file */
    1307         477 :                 ret = parity_chsize(parity_ptr[l], size, &out_size, state->opt.skip_fallocate);
    1308         477 :                 if (ret == -1) {
    1309             :                         /* LCOV_EXCL_START */
    1310             :                         parity_overflow(state, out_size);
    1311             :                         log_fatal("WARNING! Without an accessible %s file, it isn't possible to sync.\n", lev_name(l));
    1312             :                         exit(EXIT_FAILURE);
    1313             :                         /* LCOV_EXCL_STOP */
    1314             :                 }
    1315             :         }
    1316             : 
    1317             :         /* after resizing parity files, refresh again the free info */
    1318          82 :         state_refresh(state);
    1319             : 
    1320          82 :         unrecoverable_error = 0;
    1321             : 
    1322          82 :         if (state->opt.prehash) {
    1323           7 :                 msg_progress("Hashing...\n");
    1324             : 
    1325           7 :                 ret = state_hash_process(state, blockstart, blockmax, &skip_sync);
    1326           7 :                 if (ret == -1) {
    1327             :                         /* LCOV_EXCL_START */
    1328             :                         ++unrecoverable_error;
    1329             :                         /* continue, in case also doing the sync */
    1330             :                         /* LCOV_EXCL_STOP */
    1331             :                 }
    1332             : 
    1333             :                 /* save with the new hash information */
    1334           7 :                 if (state->need_write)
    1335           2 :                         state_write(state);
    1336             :         }
    1337             : 
    1338          82 :         if (!skip_sync) {
    1339          81 :                 msg_progress("Syncing...\n");
    1340             : 
    1341             :                 /* skip degenerated cases of empty parity, or skipping all */
    1342          81 :                 if (blockstart < blockmax) {
    1343          78 :                         ret = state_sync_process(state, parity_ptr, blockstart, blockmax);
    1344          78 :                         if (ret == -1) {
    1345             :                                 /* LCOV_EXCL_START */
    1346             :                                 ++unrecoverable_error;
    1347             :                                 /* continue, as we are already exiting */
    1348             :                                 /* LCOV_EXCL_STOP */
    1349             :                         }
    1350             :                 } else {
    1351           3 :                         msg_status("Nothing to do\n");
    1352             :                 }
    1353             :         }
    1354             : 
    1355         559 :         for (l = 0; l < state->level; ++l) {
    1356         477 :                 ret = parity_close(parity_ptr[l]);
    1357         477 :                 if (ret == -1) {
    1358             :                         /* LCOV_EXCL_START */
    1359             :                         log_fatal("DANGER! Unexpected close error in %s disk.\n", lev_name(l));
    1360             :                         ++unrecoverable_error;
    1361             :                         /* continue, as we are already exiting */
    1362             :                         /* LCOV_EXCL_STOP */
    1363             :                 }
    1364             :         }
    1365             : 
    1366             :         /* abort if required */
    1367          82 :         if (unrecoverable_error != 0)
    1368          12 :                 return -1;
    1369          70 :         return 0;
    1370             : }
    1371             : 

Generated by: LCOV version 1.10