LCOV - code coverage report
Current view: top level - cmdline - dry.c (source / functions) Hit Total Coverage
Test: lcov.info Lines: 70 82 85.4 %
Date: 2016-01-16 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        7104 :         for (i = blockstart; i < blockmax; ++i) {
      58             :                 /* for each disk, process the block */
      59       49714 :                 for (j = 0; j < diskmax; ++j) {
      60             :                         int read_size;
      61             :                         struct snapraid_block* block;
      62       42612 :                         struct snapraid_disk* disk = handle[j].disk;
      63             :                         struct snapraid_file* file;
      64             :                         block_off_t file_pos;
      65             : 
      66       42612 :                         if (!disk) {
      67             :                                 /* if no disk, nothing to do */
      68        9919 :                                 continue;
      69             :                         }
      70             : 
      71       37925 :                         block = fs_par2block_get(disk, i);
      72             : 
      73       37925 :                         if (!block_has_file(block)) {
      74             :                                 /* if no file, nothing to do */
      75         545 :                                 continue;
      76             :                         }
      77             : 
      78             :                         /* get the file of this block */
      79       37380 :                         file = fs_par2file_get(disk, i, &file_pos);
      80             : 
      81             :                         /* until now is CPU */
      82       37380 :                         state_usage_cpu(state);
      83             : 
      84             :                         /* if the file is closed or different than the current one */
      85       37380 :                         if (handle[j].file == 0 || handle[j].file != file) {
      86             :                                 /* close the old one, if any */
      87       15101 :                                 ret = handle_close(&handle[j]);
      88       15101 :                                 if (ret == -1) {
      89             :                                         /* LCOV_EXCL_START */
      90             :                                         log_tag("error:%u:%s:%s: Close error. %s\n", i, disk->name, esc(handle[j].file->sub), strerror(errno));
      91             :                                         log_fatal("DANGER! Unexpected close error in a data disk, it isn't possible to dry.\n");
      92             :                                         log_fatal("Stopping at block %u\n", i);
      93             :                                         ++error;
      94             :                                         goto bail;
      95             :                                         /* LCOV_EXCL_STOP */
      96             :                                 }
      97             : 
      98             :                                 /* open the file only for reading */
      99       15101 :                                 ret = handle_open(&handle[j], file, state->file_mode, log_fatal, 0);
     100       15101 :                                 if (ret == -1) {
     101             :                                         /* LCOV_EXCL_START */
     102             :                                         log_tag("error:%u:%s:%s: Open error. %s\n", i, disk->name, esc(file->sub), strerror(errno));
     103             :                                         log_fatal("DANGER! Unexpected open error in a data disk, it isn't possible to dry.\n");
     104             :                                         log_fatal("Stopping at block %u\n", i);
     105             :                                         ++error;
     106             :                                         goto bail;
     107             :                                         /* LCOV_EXCL_STOP */
     108             :                                 }
     109             :                         }
     110             : 
     111             :                         /* read from the file */
     112       37380 :                         read_size = handle_read(&handle[j], file_pos, buffer_aligned, state->block_size, log_error, 0);
     113       37380 :                         if (read_size == -1) {
     114           0 :                                 log_tag("error:%u:%s:%s: Read error at position %u\n", i, disk->name, esc(file->sub), file_pos);
     115           0 :                                 ++error;
     116           0 :                                 continue;
     117             :                         }
     118             : 
     119             :                         /* until now is disk */
     120       37380 :                         state_usage_disk(state, disk);
     121             : 
     122       37380 :                         countsize += read_size;
     123             :                 }
     124             : 
     125             :                 /* read the parity */
     126       49714 :                 for (l = 0; l < state->level; ++l) {
     127       42612 :                         if (parity[l]) {
     128             :                                 /* until now is CPU */
     129       42612 :                                 state_usage_cpu(state);
     130             : 
     131       42612 :                                 ret = parity_read(parity[l], i, buffer_aligned, state->block_size, log_error);
     132       42612 :                                 if (ret == -1) {
     133           0 :                                         log_tag("parity_error:%u:%s: Read error\n", i, lev_config_name(l));
     134           0 :                                         ++error;
     135             :                                 }
     136             : 
     137             :                                 /* until now is parity */
     138       42612 :                                 state_usage_parity(state, l);
     139             :                         }
     140             :                 }
     141             : 
     142             :                 /* count the number of processed block */
     143        7102 :                 ++countpos;
     144             : 
     145             :                 /* progress */
     146        7102 :                 if (state_progress(state, i, countpos, countmax, countsize)) {
     147             :                         /* LCOV_EXCL_START */
     148             :                         break;
     149             :                         /* LCOV_EXCL_STOP */
     150             :                 }
     151             :         }
     152             : 
     153           2 :         state_progress_end(state, countpos, countmax, countsize);
     154             : 
     155           2 :         state_usage_print(state);
     156             : 
     157             : bail:
     158             :         /* close all the files left open */
     159          14 :         for (j = 0; j < diskmax; ++j) {
     160          12 :                 struct snapraid_file* file = handle[j].file;
     161          12 :                 struct snapraid_disk* disk = handle[j].disk;
     162          12 :                 ret = handle_close(&handle[j]);
     163          12 :                 if (ret == -1) {
     164             :                         /* LCOV_EXCL_START */
     165             :                         log_tag("error:%u:%s:%s: Close error. %s\n", blockmax, disk->name, esc(file->sub), strerror(errno));
     166             :                         log_fatal("DANGER! Unexpected close error in a data disk.\n");
     167             :                         ++error;
     168             :                         /* continue, as we are already exiting */
     169             :                         /* LCOV_EXCL_STOP */
     170             :                 }
     171             :         }
     172             : 
     173           2 :         if (error) {
     174           0 :                 msg_status("\n");
     175           0 :                 msg_status("%8u errors\n", error);
     176             :         } else {
     177           2 :                 msg_status("Everything OK\n");
     178             :         }
     179             : 
     180           2 :         if (error)
     181           0 :                 log_fatal("DANGER! Unexpected errors!\n");
     182             : 
     183           2 :         free(handle);
     184           2 :         free(buffer_alloc);
     185             : 
     186           2 :         if (error != 0)
     187           0 :                 return -1;
     188           2 :         return 0;
     189             : }
     190             : 
     191           2 : void state_dry(struct snapraid_state* state, block_off_t blockstart, block_off_t blockcount)
     192             : {
     193             :         block_off_t blockmax;
     194             :         int ret;
     195             :         struct snapraid_parity_handle parity[LEV_MAX];
     196             :         /* the following initialization is to avoid clang warnings about */
     197             :         /* potential state->level change, that never happens */
     198           2 :         struct snapraid_parity_handle* parity_ptr[LEV_MAX] = { 0 };
     199             :         unsigned error;
     200             :         unsigned l;
     201             : 
     202           2 :         msg_progress("Drying...\n");
     203             : 
     204           2 :         blockmax = parity_allocated_size(state);
     205             : 
     206           2 :         if (blockstart > blockmax) {
     207             :                 /* LCOV_EXCL_START */
     208             :                 log_fatal("Error in the specified starting block %u. It's bigger than the parity size %u.\n", blockstart, blockmax);
     209             :                 exit(EXIT_FAILURE);
     210             :                 /* LCOV_EXCL_STOP */
     211             :         }
     212             : 
     213             :         /* adjust the number of block to process */
     214           2 :         if (blockcount != 0 && blockstart + blockcount < blockmax) {
     215           0 :                 blockmax = blockstart + blockcount;
     216             :         }
     217             : 
     218             :         /* open the file for reading */
     219             :         /* it may fail if the file doesn't exist, in this case we continue to dry the files */
     220          14 :         for (l = 0; l < state->level; ++l) {
     221          12 :                 parity_ptr[l] = &parity[l];
     222          12 :                 ret = parity_open(parity_ptr[l], state->parity[l].path, state->file_mode);
     223          12 :                 if (ret == -1) {
     224           0 :                         msg_status("No accessible %s file.\n", lev_name(l));
     225             :                         /* continue anyway */
     226           0 :                         parity_ptr[l] = 0;
     227             :                 }
     228             :         }
     229             : 
     230           2 :         error = 0;
     231             : 
     232             :         /* skip degenerated cases of empty parity, or skipping all */
     233           2 :         if (blockstart < blockmax) {
     234           2 :                 ret = state_dry_process(state, parity_ptr, blockstart, blockmax);
     235           2 :                 if (ret == -1) {
     236             :                         /* LCOV_EXCL_START */
     237             :                         ++error;
     238             :                         /* continue, as we are already exiting */
     239             :                         /* LCOV_EXCL_STOP */
     240             :                 }
     241             :         }
     242             : 
     243             :         /* try to close only if opened */
     244          14 :         for (l = 0; l < state->level; ++l) {
     245          12 :                 if (parity_ptr[l]) {
     246          12 :                         ret = parity_close(parity_ptr[l]);
     247          12 :                         if (ret == -1) {
     248             :                                 /* LCOV_EXCL_START */
     249             :                                 ++error;
     250             :                                 /* continue, as we are already exiting */
     251             :                                 /* LCOV_EXCL_STOP */
     252             :                         }
     253             :                 }
     254             :         }
     255             : 
     256             :         /* abort if required */
     257           2 :         if (error != 0) {
     258             :                 /* LCOV_EXCL_START */
     259             :                 exit(EXIT_FAILURE);
     260             :                 /* LCOV_EXCL_STOP */
     261             :         }
     262           2 : }
     263             : 

Generated by: LCOV version 1.10