LCOV - code coverage report
Current view: top level - cmdline - scrub.c (source / functions) Hit Total Coverage
Test: lcov.info Lines: 215 276 77.9 %
Date: 2015-05-12 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (C) 2013 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             : /* scrub */
      29             : 
      30             : /**
      31             :  * Buffer for storing the new hashes.
      32             :  */
      33             : struct snapraid_rehash {
      34             :         unsigned char hash[HASH_SIZE];
      35             :         struct snapraid_block* block;
      36             : };
      37             : 
      38             : /**
      39             :  * Check if we have to process the specified block index ::i.
      40             :  */
      41       93134 : static int block_is_enabled(struct snapraid_state* state, block_off_t i, time_t timelimit, block_off_t lastlimit, block_off_t* countlast)
      42             : {
      43             :         time_t blocktime;
      44             :         snapraid_info info;
      45             : 
      46             :         /* if it's unused */
      47       93134 :         info = info_get(&state->infoarr, i);
      48       93134 :         if (info == 0) {
      49             :                 /* skip it */
      50           0 :                 return 0;
      51             :         }
      52             : 
      53             :         /* blocks marked as bad are always checked */
      54       93134 :         if (!info_get_bad(info)) {
      55             : 
      56             :                 /* if it's too new */
      57       89528 :                 blocktime = info_get_time(info);
      58       89528 :                 if (blocktime > timelimit) {
      59             :                         /* skip it */
      60       37584 :                         return 0;
      61             :                 }
      62             : 
      63             :                 /* skip odd blocks, used only for testing */
      64       51944 :                 if (state->opt.force_scrub_even && (i % 2) != 0) {
      65             :                         /* skip it */
      66        9198 :                         return 0;
      67             :                 }
      68             : 
      69             :                 /* if the time is less than the limit, always include */
      70             :                 /* otherwise, check if we reached the last limit count */
      71       42746 :                 if (blocktime == timelimit) {
      72             :                         /* if we reached the count limit */
      73       20014 :                         if (*countlast >= lastlimit) {
      74             :                                 /* skip it */
      75       11882 :                                 return 0;
      76             :                         }
      77             : 
      78        8132 :                         ++*countlast;
      79             :                 }
      80             :         }
      81             : 
      82       34470 :         return 1;
      83             : }
      84             : 
      85           9 : static int state_scrub_process(struct snapraid_state* state, struct snapraid_parity_handle** parity, block_off_t blockstart, block_off_t blockmax, time_t timelimit, block_off_t lastlimit, time_t now)
      86             : {
      87             :         struct snapraid_handle* handle;
      88             :         void* rehandle_alloc;
      89             :         struct snapraid_rehash* rehandle;
      90             :         unsigned diskmax;
      91             :         block_off_t i;
      92             :         unsigned j;
      93             :         void* buffer_alloc;
      94             :         void** buffer;
      95             :         unsigned buffermax;
      96             :         data_off_t countsize;
      97             :         block_off_t countpos;
      98             :         block_off_t countmax;
      99             :         block_off_t countlast;
     100             :         block_off_t autosavedone;
     101             :         block_off_t autosavelimit;
     102             :         block_off_t autosavemissing;
     103             :         int ret;
     104             :         unsigned error;
     105             :         unsigned silent_error;
     106             :         unsigned io_error;
     107             :         unsigned l;
     108             : 
     109             :         /* maps the disks to handles */
     110           9 :         handle = handle_map(state, &diskmax);
     111             : 
     112             :         /* rehash buffers */
     113           9 :         rehandle = malloc_nofail_align(diskmax * sizeof(struct snapraid_rehash), &rehandle_alloc);
     114             : 
     115             :         /* we need disk + 2 for each parity level buffers */
     116           9 :         buffermax = diskmax + state->level * 2;
     117             : 
     118           9 :         buffer = malloc_nofail_vector_align(diskmax, buffermax, state->block_size, &buffer_alloc);
     119           9 :         if (!state->opt.skip_self)
     120           0 :                 mtest_vector(buffermax, state->block_size, buffer);
     121             : 
     122           9 :         error = 0;
     123           9 :         silent_error = 0;
     124           9 :         io_error = 0;
     125             : 
     126             :         /* first count the number of blocks to process */
     127           9 :         countmax = 0;
     128           9 :         countlast = 0;
     129       46576 :         for (i = blockstart; i < blockmax; ++i) {
     130       46567 :                 if (!block_is_enabled(state, i, timelimit, lastlimit, &countlast))
     131       29332 :                         continue;
     132             : 
     133       17235 :                 ++countmax;
     134             :         }
     135             : 
     136             :         /* compute the autosave size for all disk, even if not read */
     137             :         /* this makes sense because the speed should be almost the same */
     138             :         /* if the disks are read in parallel */
     139           9 :         autosavelimit = state->autosave / (diskmax * state->block_size);
     140           9 :         autosavemissing = countmax; /* blocks to do */
     141           9 :         autosavedone = 0; /* blocks done */
     142             : 
     143             :         /* drop until now */
     144           9 :         state_usage_waste(state);
     145             : 
     146           9 :         countsize = 0;
     147           9 :         countpos = 0;
     148           9 :         countlast = 0;
     149           9 :         state_progress_begin(state, blockstart, blockmax, countmax);
     150       46576 :         for (i = blockstart; i < blockmax; ++i) {
     151             :                 snapraid_info info;
     152             :                 int error_on_this_block;
     153             :                 int silent_error_on_this_block;
     154             :                 int io_error_on_this_block;
     155             :                 int block_is_unsynced;
     156             :                 int rehash;
     157             : 
     158       46567 :                 if (!block_is_enabled(state, i, timelimit, lastlimit, &countlast))
     159       29332 :                         continue;
     160             : 
     161             :                 /* one more block processed for autosave */
     162       17235 :                 ++autosavedone;
     163       17235 :                 --autosavemissing;
     164             : 
     165             :                 /* by default process the block, and skip it if something goes wrong */
     166       17235 :                 error_on_this_block = 0;
     167       17235 :                 silent_error_on_this_block = 0;
     168       17235 :                 io_error_on_this_block = 0;
     169             : 
     170             :                 /* if all the blocks at this address are synced */
     171             :                 /* if not, parity is not even checked */
     172       17235 :                 block_is_unsynced = 0;
     173             : 
     174             :                 /* get block specific info */
     175       17235 :                 info = info_get(&state->infoarr, i);
     176             : 
     177             :                 /* if we have to use the old hash */
     178       17235 :                 rehash = info_get_rehash(info);
     179             : 
     180             :                 /* for each disk, process the block */
     181      120645 :                 for (j = 0; j < diskmax; ++j) {
     182             :                         int read_size;
     183             :                         unsigned char hash[HASH_SIZE];
     184             :                         struct snapraid_block* block;
     185             :                         int file_is_unsynced;
     186      103410 :                         struct snapraid_disk* disk = handle[j].disk;
     187             : 
     188             :                         /* if the file on this disk is synced */
     189             :                         /* if not, silent errors are assumed as expected error */
     190      103410 :                         file_is_unsynced = 0;
     191             : 
     192             :                         /* by default no rehash in case of "continue" */
     193      103410 :                         rehandle[j].block = 0;
     194             : 
     195             :                         /* if the disk position is not used */
     196      103410 :                         if (!disk) {
     197             :                                 /* use an empty block */
     198           0 :                                 memset(buffer[j], 0, state->block_size);
     199        1871 :                                 continue;
     200             :                         }
     201             : 
     202             :                         /* if the block is not used */
     203      103410 :                         block = disk_block_get(disk, i);
     204      103410 :                         if (!block_has_file(block)) {
     205             :                                 /* use an empty block */
     206         951 :                                 memset(buffer[j], 0, state->block_size);
     207         951 :                                 continue;
     208             :                         }
     209             : 
     210             :                         /* if the block is unsynced, errors are expected */
     211      102459 :                         if (block_has_invalid_parity(block)) {
     212             :                                 /* report that the block and the file are not synced */
     213           0 :                                 block_is_unsynced = 1;
     214           0 :                                 file_is_unsynced = 1;
     215             :                                 /* follow */
     216             :                         }
     217             : 
     218             :                         /* until now is CPU */
     219      102459 :                         state_usage_cpu(state);
     220             : 
     221             :                         /* if the file is different than the current one, close it */
     222      102459 :                         if (handle[j].file != 0 && handle[j].file != block_file_get(block)) {
     223             :                                 /* keep a pointer at the file we are going to close for error reporting */
     224       54520 :                                 struct snapraid_file* file = handle[j].file;
     225       54520 :                                 ret = handle_close(&handle[j]);
     226       54520 :                                 if (ret == -1) {
     227             :                                         /* LCOV_EXCL_START */
     228             :                                         /* This one is really an unexpected error, because we are only reading */
     229             :                                         /* and closing a descriptor should never fail */
     230             :                                         if (errno == EIO) {
     231             :                                                 log_tag("error:%u:%s:%s: Close EIO error. %s\n", i, disk->name, esc(file->sub), strerror(errno));
     232             :                                                 log_fatal("DANGER! Unexpected input/output close error in a data disk, it isn't possible to scrub.\n");
     233             :                                                 log_fatal("Ensure that disk '%s' is sane and that file '%s' can be accessed.\n", disk->dir, handle[j].path);
     234             :                                                 log_fatal("Stopping at block %u\n", i);
     235             :                                                 ++io_error;
     236             :                                                 goto bail;
     237             :                                         }
     238             : 
     239             :                                         log_tag("error:%u:%s:%s: Close error. %s\n", i, disk->name, esc(file->sub), strerror(errno));
     240             :                                         log_fatal("WARNING! Unexpected close error in a data disk, it isn't possible to scrub.\n");
     241             :                                         log_fatal("Ensure that file '%s' can be accessed.\n", handle[j].path);
     242             :                                         log_fatal("Stopping at block %u\n", i);
     243             :                                         ++error;
     244             :                                         goto bail;
     245             :                                         /* LCOV_EXCL_STOP */
     246             :                                 }
     247             :                         }
     248             : 
     249      102459 :                         ret = handle_open(&handle[j], block_file_get(block), state->file_mode, log_error, 0);
     250      102459 :                         if (ret == -1) {
     251             :                                 /* file we have tried to open for error reporting */
     252           0 :                                 struct snapraid_file* file = block_file_get(block);
     253           0 :                                 if (errno == EIO) {
     254             :                                         /* LCOV_EXCL_START */
     255             :                                         log_tag("error:%u:%s:%s: Open EIO error. %s\n", i, disk->name, esc(file->sub), strerror(errno));
     256             :                                         log_fatal("DANGER! Unexpected input/output open error in a data disk, it isn't possible to scrub.\n");
     257             :                                         log_fatal("Ensure that disk '%s' is sane and that file '%s' can be accessed.\n", disk->dir, handle[j].path);
     258             :                                         log_fatal("Stopping at block %u\n", i);
     259             :                                         ++io_error;
     260             :                                         goto bail;
     261             :                                         /* LCOV_EXCL_STOP */
     262             :                                 }
     263             : 
     264           0 :                                 log_tag("error:%u:%s:%s: Open error. %s\n", i, disk->name, esc(file->sub), strerror(errno));
     265           0 :                                 ++error;
     266           0 :                                 error_on_this_block = 1;
     267           0 :                                 continue;
     268             :                         }
     269             : 
     270             :                         /* check if the file is changed */
     271      102459 :                         if (handle[j].st.st_size != block_file_get(block)->size
     272      102459 :                                 || handle[j].st.st_mtime != block_file_get(block)->mtime_sec
     273      102450 :                                 || STAT_NSEC(&handle[j].st) != block_file_get(block)->mtime_nsec
     274             :                                 /* don't check the inode to support filesystem without persistent inodes */
     275             :                         ) {
     276             :                                 /* report that the block and the file are not synced */
     277           9 :                                 block_is_unsynced = 1;
     278           9 :                                 file_is_unsynced = 1;
     279             :                                 /* follow */
     280             :                         }
     281             : 
     282             :                         /* note that we intentionally don't abort if the file has different attributes */
     283             :                         /* from the last sync, as we are expected to return errors if running */
     284             :                         /* in an unsynced array. This is just like the check command. */
     285             : 
     286      102459 :                         read_size = handle_read(&handle[j], block, buffer[j], state->block_size, log_error, 0);
     287      102459 :                         if (read_size == -1) {
     288             :                                 /* file we are processing for error reporting */
     289           0 :                                 struct snapraid_file* file = block_file_get(block);
     290           0 :                                 if (errno == EIO) {
     291           0 :                                         log_tag("error:%u:%s:%s: Read EIO error at position %u. %s\n", i, disk->name, esc(file->sub), block_file_pos(block), strerror(errno));
     292           0 :                                         if (io_error >= state->opt.io_error_limit) {
     293             :                                                 /* LCOV_EXCL_START */
     294             :                                                 log_fatal("DANGER! Too many input/output read error in a data disk, it isn't possible to scrub.\n");
     295             :                                                 log_fatal("Ensure that disk '%s' is sane and that file '%s' can be accessed.\n", disk->dir, handle[j].path);
     296             :                                                 log_fatal("Stopping at block %u\n", i);
     297             :                                                 ++io_error;
     298             :                                                 goto bail;
     299             :                                                 /* LCOV_EXCL_STOP */
     300             :                                         }
     301             : 
     302           0 :                                         log_error("Input/Output error in file '%s' at position '%u'\n", handle[j].path, block_file_pos(block));
     303           0 :                                         ++io_error;
     304           0 :                                         io_error_on_this_block = 1;
     305           0 :                                         continue;
     306             :                                 }
     307             : 
     308           0 :                                 log_tag("error:%u:%s:%s: Read error at position %u. %s\n", i, disk->name, esc(file->sub), block_file_pos(block), strerror(errno));
     309           0 :                                 ++error;
     310           0 :                                 error_on_this_block = 1;
     311           0 :                                 continue;
     312             :                         }
     313             : 
     314             :                         /* until now is disk */
     315      102459 :                         state_usage_disk(state, disk);
     316             : 
     317      102459 :                         countsize += read_size;
     318             : 
     319             :                         /* now compute the hash */
     320      102459 :                         if (rehash) {
     321       27207 :                                 memhash(state->prevhash, state->prevhashseed, hash, buffer[j], read_size);
     322             : 
     323             :                                 /* compute the new hash, and store it */
     324       27207 :                                 rehandle[j].block = block;
     325       27207 :                                 memhash(state->hash, state->hashseed, rehandle[j].hash, buffer[j], read_size);
     326             :                         } else {
     327       75252 :                                 memhash(state->hash, state->hashseed, hash, buffer[j], read_size);
     328             :                         }
     329             : 
     330      102459 :                         if (block_has_updated_hash(block)) {
     331             :                                 /* compare the hash */
     332      102459 :                                 if (memcmp(hash, block->hash, HASH_SIZE) != 0) {
     333         920 :                                         log_tag("error:%u:%s:%s: Data error at position %u\n", i, disk->name, esc(handle[j].file->sub), block_file_pos(block));
     334             : 
     335             :                                         /* it's a silent error only if we are dealing with synced files */
     336         920 :                                         if (file_is_unsynced) {
     337           0 :                                                 ++error;
     338           0 :                                                 error_on_this_block = 1;
     339             :                                         } else {
     340         920 :                                                 log_error("Data error in file '%s' at position '%u'\n", handle[j].path, block_file_pos(block));
     341         920 :                                                 ++silent_error;
     342         920 :                                                 silent_error_on_this_block = 1;
     343             :                                         }
     344         920 :                                         continue;
     345             :                                 }
     346             :                         }
     347             :                 }
     348             : 
     349             :                 /* if we have read all the data required and it's correct, proceed with the parity check */
     350       17235 :                 if (!error_on_this_block && !silent_error_on_this_block && !io_error_on_this_block) {
     351             :                         unsigned char* buffer_recov[LEV_MAX];
     352             : 
     353             :                         /* until now is CPU */
     354       16315 :                         state_usage_cpu(state);
     355             : 
     356             :                         /* buffers for parity read and not computed */
     357      114205 :                         for (l = 0; l < state->level; ++l)
     358       97890 :                                 buffer_recov[l] = buffer[diskmax + state->level + l];
     359       16315 :                         for (; l < LEV_MAX; ++l)
     360           0 :                                 buffer_recov[l] = 0;
     361             : 
     362             :                         /* read the parity */
     363      114205 :                         for (l = 0; l < state->level; ++l) {
     364       97890 :                                 ret = parity_read(parity[l], i, buffer_recov[l], state->block_size, log_error);
     365       97890 :                                 if (ret == -1) {
     366           0 :                                         buffer_recov[l] = 0;
     367             : 
     368           0 :                                         if (errno == EIO) {
     369           0 :                                                 log_tag("parity_error:%u:%s: Read EIO error. %s\n", i, lev_config_name(l), strerror(errno));
     370           0 :                                                 if (io_error >= state->opt.io_error_limit) {
     371             :                                                         /* LCOV_EXCL_START */
     372             :                                                         log_fatal("DANGER! Too many input/output read error in the %s disk, it isn't possible to scrub.\n", lev_name(l));
     373             :                                                         log_fatal("Ensure that disk '%s' is sane and can be read.\n", lev_config_name(l));
     374             :                                                         log_fatal("Stopping at block %u\n", i);
     375             :                                                         ++io_error;
     376             :                                                         goto bail;
     377             :                                                         /* LCOV_EXCL_STOP */
     378             :                                                 }
     379             : 
     380           0 :                                                 log_error("Input/Output error in parity '%s' at position '%u'\n", lev_config_name(l), i);
     381           0 :                                                 ++io_error;
     382           0 :                                                 io_error_on_this_block = 1;
     383           0 :                                                 continue;
     384             :                                         }
     385             : 
     386           0 :                                         log_tag("parity_error:%u:%s: Read error. %s\n", i, lev_config_name(l), strerror(errno));
     387           0 :                                         ++error;
     388           0 :                                         error_on_this_block = 1;
     389           0 :                                         continue;
     390             :                                 }
     391             : 
     392             :                                 /* until now is parity */
     393       97890 :                                 state_usage_parity(state, l);
     394             :                         }
     395             : 
     396             :                         /* compute the parity */
     397       16315 :                         raid_gen(diskmax, state->level, state->block_size, buffer);
     398             : 
     399             :                         /* compare the parity */
     400      114205 :                         for (l = 0; l < state->level; ++l) {
     401       97890 :                                 if (buffer_recov[l] && memcmp(buffer[diskmax + l], buffer_recov[l], state->block_size) != 0) {
     402           0 :                                         log_tag("parity_error:%u:%s: Data error\n", i, lev_config_name(l));
     403             : 
     404             :                                         /* it's a silent error only if we are dealing with synced blocks */
     405           0 :                                         if (block_is_unsynced) {
     406           0 :                                                 ++error;
     407           0 :                                                 error_on_this_block = 1;
     408             :                                         } else {
     409           0 :                                                 log_fatal("Data error in parity '%s' at position '%u'\n", lev_config_name(l), i);
     410           0 :                                                 ++silent_error;
     411           0 :                                                 silent_error_on_this_block = 1;
     412             :                                         }
     413             :                                 }
     414             :                         }
     415             :                 }
     416             : 
     417       17235 :                 if (silent_error_on_this_block || io_error_on_this_block) {
     418             :                         /* set the error status keeping the existing time and hash */
     419         920 :                         info_set(&state->infoarr, i, info_set_bad(info));
     420       16315 :                 } else if (error_on_this_block) {
     421             :                         /* do nothing, as this is a generic error */
     422             :                         /* likely caused by a not synced array */
     423             :                 } else {
     424             :                         /* if rehash is needed */
     425       16315 :                         if (rehash) {
     426             :                                 /* store all the new hash already computed */
     427       32200 :                                 for (j = 0; j < diskmax; ++j) {
     428       27600 :                                         if (rehandle[j].block)
     429       27207 :                                                 memcpy(rehandle[j].block->hash, rehandle[j].hash, HASH_SIZE);
     430             :                                 }
     431             :                         }
     432             : 
     433             :                         /* update the time info of the block */
     434             :                         /* and clear any other flag */
     435       16315 :                         info_set(&state->infoarr, i, info_make(now, 0, 0));
     436             :                 }
     437             : 
     438             :                 /* mark the state as needing write */
     439       17235 :                 state->need_write = 1;
     440             : 
     441             :                 /* count the number of processed block */
     442       17235 :                 ++countpos;
     443             : 
     444             :                 /* progress */
     445       17235 :                 if (state_progress(state, i, countpos, countmax, countsize)) {
     446             :                         /* LCOV_EXCL_START */
     447             :                         break;
     448             :                         /* LCOV_EXCL_STOP */
     449             :                 }
     450             : 
     451             :                 /* autosave */
     452       17235 :                 if (state->autosave != 0
     453           0 :                         && autosavedone >= autosavelimit /* if we have reached the limit */
     454           0 :                         && autosavemissing >= autosavelimit /* if we have at least a full step to do */
     455             :                 ) {
     456           0 :                         autosavedone = 0; /* restart the counter */
     457             : 
     458             :                         /* until now is CPU */
     459           0 :                         state_usage_cpu(state);
     460             : 
     461           0 :                         state_progress_stop(state);
     462             : 
     463           0 :                         msg_progress("Autosaving...\n");
     464           0 :                         state_write(state);
     465             : 
     466           0 :                         state_progress_restart(state);
     467             : 
     468             :                         /* drop until now */
     469           0 :                         state_usage_waste(state);
     470             :                 }
     471             :         }
     472             : 
     473           9 :         state_progress_end(state, countpos, countmax, countsize);
     474             : 
     475           9 :         state_usage_print(state);
     476             : 
     477           9 :         if (error || silent_error || io_error) {
     478           1 :                 msg_status("\n");
     479           1 :                 msg_status("%8u file errors\n", error);
     480           1 :                 msg_status("%8u io errors\n", io_error);
     481           1 :                 msg_status("%8u data errors\n", silent_error);
     482             :         } else {
     483             :                 /* print the result only if processed something */
     484           8 :                 if (countpos != 0)
     485           7 :                         msg_status("Everything OK\n");
     486             :         }
     487             : 
     488           9 :         if (error)
     489           0 :                 log_fatal("WARNING! Unexpected file errors!\n");
     490           9 :         if (io_error)
     491           0 :                 log_fatal("DANGER! Unexpected input/output errors! The failing blocks are now marked as bad!\n");
     492           9 :         if (silent_error)
     493           1 :                 log_fatal("DANGER! Unexpected data errors! The failing blocks are now marked as bad!\n");
     494           9 :         if (io_error || silent_error) {
     495           1 :                 log_fatal("Use 'snapraid status' to list the bad blocks.\n");
     496           1 :                 log_fatal("Use 'snapraid -e fix' to recover.\n");
     497             :         }
     498             : 
     499           9 :         log_tag("summary:error_file:%u\n", error);
     500           9 :         log_tag("summary:error_io:%u\n", io_error);
     501           9 :         log_tag("summary:error_data:%u\n", silent_error);
     502           9 :         if (error + silent_error + io_error == 0)
     503           8 :                 log_tag("summary:exit:ok\n");
     504             :         else
     505           1 :                 log_tag("summary:exit:error\n");
     506           9 :         log_flush();
     507             : 
     508             : bail:
     509          63 :         for (j = 0; j < diskmax; ++j) {
     510          54 :                 struct snapraid_file* file = handle[j].file;
     511          54 :                 struct snapraid_disk* disk = handle[j].disk;
     512          54 :                 ret = handle_close(&handle[j]);
     513          54 :                 if (ret == -1) {
     514             :                         /* LCOV_EXCL_START */
     515             :                         log_tag("error:%u:%s:%s: Close error. %s\n", i, disk->name, esc(file->sub), strerror(errno));
     516             :                         log_fatal("DANGER! Unexpected close error in a data disk.\n");
     517             :                         ++error;
     518             :                         /* continue, as we are already exiting */
     519             :                         /* LCOV_EXCL_STOP */
     520             :                 }
     521             :         }
     522             : 
     523           9 :         free(handle);
     524           9 :         free(buffer_alloc);
     525           9 :         free(buffer);
     526           9 :         free(rehandle_alloc);
     527             : 
     528           9 :         if (state->opt.expect_recoverable) {
     529           1 :                 if (error + silent_error + io_error == 0)
     530           0 :                         return -1;
     531             :         } else {
     532           8 :                 if (error + silent_error + io_error != 0)
     533           0 :                         return -1;
     534             :         }
     535           9 :         return 0;
     536             : }
     537             : 
     538             : /**
     539             :  * Returns a * b / c approximated to the upper value.
     540             :  */
     541           6 : static uint32_t md(uint32_t a, uint32_t b, uint32_t c)
     542             : {
     543           6 :         uint64_t v = a;
     544             : 
     545           6 :         v *= b;
     546           6 :         v += c - 1;
     547           6 :         v /= c;
     548             : 
     549           6 :         return v;
     550             : }
     551             : 
     552           9 : int state_scrub(struct snapraid_state* state, int percentage, int olderthan)
     553             : {
     554             :         block_off_t blockmax;
     555             :         block_off_t countlimit;
     556             :         block_off_t lastlimit;
     557             :         block_off_t i;
     558             :         time_t timelimit;
     559             :         time_t recentlimit;
     560             :         unsigned count;
     561             :         int ret;
     562             :         struct snapraid_parity_handle parity[LEV_MAX];
     563             :         struct snapraid_parity_handle* parity_ptr[LEV_MAX];
     564             :         snapraid_info* infomap;
     565             :         unsigned error;
     566             :         time_t now;
     567             :         unsigned l;
     568             : 
     569             :         /* get the present time */
     570           9 :         now = time(0);
     571             : 
     572           9 :         msg_progress("Initializing...\n");
     573             : 
     574           9 :         blockmax = parity_allocated_size(state);
     575             : 
     576           9 :         if (state->opt.force_scrub_even) {
     577             :                 /* no limit */
     578           1 :                 countlimit = blockmax;
     579           1 :                 recentlimit = now;
     580           8 :         } else if (state->opt.force_scrub_at) {
     581             :                 /* scrub the specified amount of blocks */
     582           4 :                 countlimit = state->opt.force_scrub_at;
     583           4 :                 recentlimit = now;
     584             :         } else {
     585             :                 /* by default scrub 1/12 of the array */
     586           4 :                 countlimit = md(blockmax, 1, 12);
     587             : 
     588           4 :                 if (percentage != -1)
     589           2 :                         countlimit = md(blockmax, percentage, 100);
     590             : 
     591             :                 /* by default use a 10 day time limit */
     592           4 :                 recentlimit = now - 10 * 24 * 3600;
     593             : 
     594           4 :                 if (olderthan != -1)
     595           1 :                         recentlimit = now - olderthan * 24 * 3600;
     596             :         }
     597             : 
     598             :         /* identify the time limit */
     599             :         /* we sort all the block times, and we identify the time limit for which we reach the quota */
     600             :         /* this allow to process first the oldest blocks */
     601           9 :         infomap = malloc_nofail(blockmax * sizeof(snapraid_info));
     602             : 
     603             :         /* copy the info in the temp vector */
     604           9 :         count = 0;
     605           9 :         log_tag("block_count:%u\n", blockmax);
     606       46576 :         for (i = 0; i < blockmax; ++i) {
     607       46567 :                 snapraid_info info = info_get(&state->infoarr, i);
     608             : 
     609             :                 /* skip unused blocks */
     610       46567 :                 if (info == 0)
     611           0 :                         continue;
     612             : 
     613       46567 :                 infomap[count++] = info;
     614             :         }
     615             : 
     616           9 :         if (!count) {
     617             :                 /* LCOV_EXCL_START */
     618             :                 log_fatal("The array appears to be empty.\n");
     619             :                 exit(EXIT_FAILURE);
     620             :                 /* LCOV_EXCL_STOP */
     621             :         }
     622             : 
     623             :         /* sort it */
     624           9 :         qsort(infomap, count, sizeof(snapraid_info), info_time_compare);
     625             : 
     626             :         /* output the info map */
     627           9 :         i = 0;
     628           9 :         log_tag("info_count:%u\n", count);
     629          42 :         while (i < count) {
     630          24 :                 unsigned j = i + 1;
     631       46591 :                 while (j < count && info_get_time(infomap[i]) == info_get_time(infomap[j]))
     632       46543 :                         ++j;
     633          24 :                 log_tag("info_time:%" PRIu64 ":%u\n", (uint64_t)info_get_time(infomap[i]), j - i);
     634          24 :                 i = j;
     635             :         }
     636             : 
     637             :         /* no more than the full count */
     638           9 :         if (countlimit > count)
     639           2 :                 countlimit = count;
     640             : 
     641             :         /* decrease until we reach the specific recentlimit */
     642        5079 :         while (countlimit > 0 && info_get_time(infomap[countlimit - 1]) > recentlimit)
     643        5061 :                 --countlimit;
     644             : 
     645             :         /* if there is something to scrub */
     646           9 :         if (countlimit > 0) {
     647             :                 /* get the most recent time we want to scrub */
     648           6 :                 timelimit = info_get_time(infomap[countlimit - 1]);
     649             : 
     650             :                 /* count how many entries for this exact time we have to scrub */
     651             :                 /* if the blocks have all the same time, we end with countlimit == lastlimit */
     652           6 :                 lastlimit = 1;
     653        5562 :                 while (countlimit > lastlimit && info_get_time(infomap[countlimit - lastlimit - 1]) == timelimit)
     654        5550 :                         ++lastlimit;
     655             :         } else {
     656             :                 /* if nothing to scrub, disable also other limits */
     657           3 :                 timelimit = 0;
     658           3 :                 lastlimit = 0;
     659             :         }
     660             : 
     661           9 :         log_tag("count_limit:%u\n", countlimit);
     662           9 :         log_tag("time_limit:%" PRIu64 "\n", (uint64_t)timelimit);
     663           9 :         log_tag("last_limit:%u\n", lastlimit);
     664             : 
     665             :         /* free the temp vector */
     666           9 :         free(infomap);
     667             : 
     668             :         /* open the file for reading */
     669          63 :         for (l = 0; l < state->level; ++l) {
     670          54 :                 parity_ptr[l] = &parity[l];
     671          54 :                 ret = parity_open(parity_ptr[l], state->parity[l].path, state->file_mode);
     672          54 :                 if (ret == -1) {
     673             :                         /* LCOV_EXCL_START */
     674             :                         log_fatal("WARNING! Without an accessible %s file, it isn't possible to scrub.\n", lev_name(l));
     675             :                         exit(EXIT_FAILURE);
     676             :                         /* LCOV_EXCL_STOP */
     677             :                 }
     678             :         }
     679             : 
     680           9 :         msg_progress("Scrubbing...\n");
     681             : 
     682           9 :         error = 0;
     683             : 
     684           9 :         ret = state_scrub_process(state, parity_ptr, 0, blockmax, timelimit, lastlimit, now);
     685           9 :         if (ret == -1) {
     686           0 :                 ++error;
     687             :                 /* continue, as we are already exiting */
     688             :         }
     689             : 
     690          63 :         for (l = 0; l < state->level; ++l) {
     691          54 :                 ret = parity_close(parity_ptr[l]);
     692          54 :                 if (ret == -1) {
     693             :                         /* LCOV_EXCL_START */
     694             :                         log_fatal("DANGER! Unexpected close error in %s disk.\n", lev_name(l));
     695             :                         ++error;
     696             :                         /* continue, as we are already exiting */
     697             :                         /* LCOV_EXCL_STOP */
     698             :                 }
     699             :         }
     700             : 
     701             :         /* abort if required */
     702           9 :         if (error != 0)
     703           0 :                 return -1;
     704           9 :         return 0;
     705             : }
     706             : 

Generated by: LCOV version 1.10