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

Generated by: LCOV version 1.10