LCOV - code coverage report
Current view: top level - cmdline - dry.c (source / functions) Hit Total Coverage
Test: lcov.info Lines: 72 84 85.7 %
Date: 2015-05-12 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (C) 2011 Andrea Mazzoleni
       3             :  *
       4             :  * This program is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 3 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * This program is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #include "portable.h"
      19             : 
      20             : #include "util.h"
      21             : #include "elem.h"
      22             : #include "state.h"
      23             : #include "parity.h"
      24             : #include "handle.h"
      25             : 
      26             : /****************************************************************************/
      27             : /* dry */
      28             : 
      29           2 : static int state_dry_process(struct snapraid_state* state, struct snapraid_parity_handle** parity, block_off_t blockstart, block_off_t blockmax)
      30             : {
      31             :         struct snapraid_handle* handle;
      32             :         unsigned diskmax;
      33             :         block_off_t i;
      34             :         unsigned j;
      35             :         void* buffer_alloc;
      36             :         unsigned char* buffer_aligned;
      37             :         int ret;
      38             :         data_off_t countsize;
      39             :         block_off_t countpos;
      40             :         block_off_t countmax;
      41             :         unsigned error;
      42             :         unsigned l;
      43             : 
      44           2 :         handle = handle_map(state, &diskmax);
      45             : 
      46           2 :         buffer_aligned = malloc_nofail_align(state->block_size, &buffer_alloc);
      47             : 
      48           2 :         error = 0;
      49             : 
      50             :         /* drop until now */
      51           2 :         state_usage_waste(state);
      52             : 
      53           2 :         countmax = blockmax - blockstart;
      54           2 :         countsize = 0;
      55           2 :         countpos = 0;
      56           2 :         state_progress_begin(state, blockstart, blockmax, countmax);
      57        7054 :         for (i = blockstart; i < blockmax; ++i) {
      58             :                 /* for each disk, process the block */
      59       49364 :                 for (j = 0; j < diskmax; ++j) {
      60             :                         int read_size;
      61       42312 :                         struct snapraid_block* block = BLOCK_EMPTY;
      62       42312 :                         struct snapraid_disk* disk = handle[j].disk;
      63             : 
      64       42312 :                         if (!disk) {
      65             :                                 /* if no disk, nothing to do */
      66        4671 :                                 continue;
      67             :                         }
      68             : 
      69       37641 :                         block = disk_block_get(disk, i);
      70             : 
      71       37641 :                         if (!block_has_file(block)) {
      72             :                                 /* if no file, nothing to do */
      73         413 :                                 continue;
      74             :                         }
      75             : 
      76             :                         /* until now is CPU */
      77       37228 :                         state_usage_cpu(state);
      78             : 
      79             :                         /* if the file is closed or different than the current one */
      80       37228 :                         if (handle[j].file == 0 || handle[j].file != block_file_get(block)) {
      81       15082 :                                 struct snapraid_file* file = handle[j].file;
      82       15082 :                                 if (file != 0) {
      83       15071 :                                         ret = handle_close(&handle[j]);
      84       15071 :                                         if (ret == -1) {
      85             :                                                 /* LCOV_EXCL_START */
      86             :                                                 log_tag("error:%u:%s:%s: Close error. %s\n", i, disk->name, esc(file->sub), strerror(errno));
      87             :                                                 log_fatal("DANGER! Unexpected close error in a data disk, it isn't possible to dry.\n");
      88             :                                                 log_fatal("Stopping at block %u\n", i);
      89             :                                                 ++error;
      90             :                                                 goto bail;
      91             :                                                 /* LCOV_EXCL_STOP */
      92             :                                         }
      93             :                                 }
      94             : 
      95             :                                 /* open the file only for reading */
      96       15082 :                                 ret = handle_open(&handle[j], block_file_get(block), state->file_mode, log_fatal, 0);
      97       15082 :                                 if (ret == -1) {
      98             :                                         /* LCOV_EXCL_START */
      99             :                                         log_fatal("DANGER! Unexpected open error in a data disk, it isn't possible to dry.\n");
     100             :                                         log_fatal("Stopping at block %u\n", i);
     101             :                                         ++error;
     102             :                                         goto bail;
     103             :                                         /* LCOV_EXCL_STOP */
     104             :                                 }
     105             :                         }
     106             : 
     107             :                         /* read from the file */
     108       37228 :                         read_size = handle_read(&handle[j], block, buffer_aligned, state->block_size, log_error, 0);
     109       37228 :                         if (read_size == -1) {
     110           0 :                                 log_tag("error:%u:%s:%s: Read error at position %u\n", i, disk->name, esc(block_file_get(block)->sub), block_file_pos(block));
     111           0 :                                 ++error;
     112           0 :                                 continue;
     113             :                         }
     114             : 
     115             :                         /* until now is disk */
     116       37228 :                         state_usage_disk(state, disk);
     117             : 
     118       37228 :                         countsize += read_size;
     119             :                 }
     120             : 
     121             :                 /* read the parity */
     122       49364 :                 for (l = 0; l < state->level; ++l) {
     123       42312 :                         if (parity[l]) {
     124             :                                 /* until now is CPU */
     125       42312 :                                 state_usage_cpu(state);
     126             : 
     127       42312 :                                 ret = parity_read(parity[l], i, buffer_aligned, state->block_size, log_error);
     128       42312 :                                 if (ret == -1) {
     129           0 :                                         log_tag("parity_error:%u:%s: Read error\n", i, lev_config_name(l));
     130           0 :                                         ++error;
     131             :                                 }
     132             : 
     133             :                                 /* until now is parity */
     134       42312 :                                 state_usage_parity(state, l);
     135             :                         }
     136             :                 }
     137             : 
     138             :                 /* count the number of processed block */
     139        7052 :                 ++countpos;
     140             : 
     141             :                 /* progress */
     142        7052 :                 if (state_progress(state, i, countpos, countmax, countsize)) {
     143             :                         /* LCOV_EXCL_START */
     144             :                         break;
     145             :                         /* LCOV_EXCL_STOP */
     146             :                 }
     147             :         }
     148             : 
     149           2 :         state_progress_end(state, countpos, countmax, countsize);
     150             : 
     151           2 :         state_usage_print(state);
     152             : 
     153             : bail:
     154             :         /* close all the files left open */
     155          14 :         for (j = 0; j < diskmax; ++j) {
     156          12 :                 struct snapraid_file* file = handle[j].file;
     157          12 :                 struct snapraid_disk* disk = handle[j].disk;
     158          12 :                 ret = handle_close(&handle[j]);
     159          12 :                 if (ret == -1) {
     160             :                         /* LCOV_EXCL_START */
     161             :                         log_tag("error:%u:%s:%s: Close error. %s\n", i, disk->name, esc(file->sub), strerror(errno));
     162             :                         log_fatal("DANGER! Unexpected close error in a data disk.\n");
     163             :                         ++error;
     164             :                         /* continue, as we are already exiting */
     165             :                         /* LCOV_EXCL_STOP */
     166             :                 }
     167             :         }
     168             : 
     169           2 :         if (error) {
     170           0 :                 msg_status("\n");
     171           0 :                 msg_status("%8u errors\n", error);
     172             :         } else {
     173           2 :                 msg_status("Everything OK\n");
     174             :         }
     175             : 
     176           2 :         if (error)
     177           0 :                 log_fatal("DANGER! Unexpected errors!\n");
     178             : 
     179           2 :         free(handle);
     180           2 :         free(buffer_alloc);
     181             : 
     182           2 :         if (error != 0)
     183           0 :                 return -1;
     184           2 :         return 0;
     185             : }
     186             : 
     187           2 : void state_dry(struct snapraid_state* state, block_off_t blockstart, block_off_t blockcount)
     188             : {
     189             :         block_off_t blockmax;
     190             :         int ret;
     191             :         struct snapraid_parity_handle parity[LEV_MAX];
     192             :         /* the following initialization is to avoid clang warnings about */
     193             :         /* potential state->level change, that never happens */
     194           2 :         struct snapraid_parity_handle* parity_ptr[LEV_MAX] = { 0 };
     195             :         unsigned error;
     196             :         unsigned l;
     197             : 
     198           2 :         msg_progress("Drying...\n");
     199             : 
     200           2 :         blockmax = parity_allocated_size(state);
     201             : 
     202           2 :         if (blockstart > blockmax) {
     203             :                 /* LCOV_EXCL_START */
     204             :                 log_fatal("Error in the specified starting block %u. It's bigger than the parity size %u.\n", blockstart, blockmax);
     205             :                 exit(EXIT_FAILURE);
     206             :                 /* LCOV_EXCL_STOP */
     207             :         }
     208             : 
     209             :         /* adjust the number of block to process */
     210           2 :         if (blockcount != 0 && blockstart + blockcount < blockmax) {
     211           0 :                 blockmax = blockstart + blockcount;
     212             :         }
     213             : 
     214             :         /* open the file for reading */
     215             :         /* it may fail if the file doesn't exist, in this case we continue to dry the files */
     216          14 :         for (l = 0; l < state->level; ++l) {
     217          12 :                 parity_ptr[l] = &parity[l];
     218          12 :                 ret = parity_open(parity_ptr[l], state->parity[l].path, state->file_mode);
     219          12 :                 if (ret == -1) {
     220           0 :                         msg_status("No accessible %s file.\n", lev_name(l));
     221             :                         /* continue anyway */
     222           0 :                         parity_ptr[l] = 0;
     223             :                 }
     224             :         }
     225             : 
     226           2 :         error = 0;
     227             : 
     228             :         /* skip degenerated cases of empty parity, or skipping all */
     229           2 :         if (blockstart < blockmax) {
     230           2 :                 ret = state_dry_process(state, parity_ptr, blockstart, blockmax);
     231           2 :                 if (ret == -1) {
     232             :                         /* LCOV_EXCL_START */
     233             :                         ++error;
     234             :                         /* continue, as we are already exiting */
     235             :                         /* LCOV_EXCL_STOP */
     236             :                 }
     237             :         }
     238             : 
     239             :         /* try to close only if opened */
     240          14 :         for (l = 0; l < state->level; ++l) {
     241          12 :                 if (parity_ptr[l]) {
     242          12 :                         ret = parity_close(parity_ptr[l]);
     243          12 :                         if (ret == -1) {
     244             :                                 /* LCOV_EXCL_START */
     245             :                                 ++error;
     246             :                                 /* continue, as we are already exiting */
     247             :                                 /* LCOV_EXCL_STOP */
     248             :                         }
     249             :                 }
     250             :         }
     251             : 
     252             :         /* abort if required */
     253           2 :         if (error != 0) {
     254             :                 /* LCOV_EXCL_START */
     255             :                 exit(EXIT_FAILURE);
     256             :                 /* LCOV_EXCL_STOP */
     257             :         }
     258           2 : }
     259             : 

Generated by: LCOV version 1.10