LCOV - code coverage report
Current view: top level - cmdline - parity.c (source / functions) Hit Total Coverage
Test: lcov.info Lines: 111 140 79.3 %
Date: 2016-01-16 Functions: 10 11 90.9 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (C) 2011 Andrea Mazzoleni
       3             :  *
       4             :  * This program is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 3 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * This program is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #include "portable.h"
      19             : 
      20             : #include "support.h"
      21             : #include "elem.h"
      22             : #include "state.h"
      23             : #include "parity.h"
      24             : #include "handle.h"
      25             : 
      26             : /****************************************************************************/
      27             : /* parity */
      28             : 
      29         623 : block_off_t parity_allocated_size(struct snapraid_state* state)
      30             : {
      31             :         block_off_t parity_block;
      32             :         tommy_node* i;
      33             : 
      34             :         /* compute the size of the parity file */
      35         623 :         parity_block = 0;
      36        4351 :         for (i = state->disklist; i != 0; i = i->next) {
      37        3728 :                 struct snapraid_disk* disk = i->data;
      38             : 
      39             :                 /* start from the declared size */
      40        3728 :                 block_off_t block = disk_size(disk);
      41             : 
      42             :                 /* decrease the block until an allocated one, but part of a file */
      43             :                 /* we don't stop at deleted blocks, because we want to have them cleared */
      44             :                 /* if they are at the end of the parity */
      45      128810 :                 while (block > parity_block && !block_has_file(fs_par2block_get(disk, block - 1)))
      46      121354 :                         --block;
      47             : 
      48             :                 /* get the highest value */
      49        3728 :                 if (block > parity_block)
      50        2204 :                         parity_block = block;
      51             :         }
      52             : 
      53         623 :         return parity_block;
      54             : }
      55             : 
      56          82 : block_off_t parity_used_size(struct snapraid_state* state)
      57             : {
      58             :         block_off_t parity_block;
      59             :         tommy_node* i;
      60             : 
      61             :         /* compute the size of the parity file */
      62          82 :         parity_block = 0;
      63         573 :         for (i = state->disklist; i != 0; i = i->next) {
      64         491 :                 struct snapraid_disk* disk = i->data;
      65             : 
      66             :                 /* start from the declared size */
      67         491 :                 block_off_t block = disk_size(disk);
      68             : 
      69             :                 /* decrease the block until an used one */
      70      258661 :                 while (block > parity_block && !block_has_file_and_valid_parity(fs_par2block_get(disk, block - 1)))
      71      257679 :                         --block;
      72             : 
      73             :                 /* get the highest value */
      74         491 :                 if (block > parity_block)
      75         261 :                         parity_block = block;
      76             :         }
      77             : 
      78          82 :         return parity_block;
      79             : }
      80             : 
      81           1 : int parity_is_invalid(struct snapraid_state* state)
      82             : {
      83             :         block_off_t blockmax;
      84             :         block_off_t i;
      85             : 
      86           1 :         blockmax = parity_allocated_size(state);
      87             : 
      88           1 :         for (i = 0; i < blockmax; ++i) {
      89             :                 tommy_node* node_disk;
      90             :                 int one_invalid;
      91             :                 int one_valid;
      92             : 
      93             :                 /* for each disk */
      94           0 :                 one_invalid = 0;
      95           0 :                 one_valid = 0;
      96           0 :                 for (node_disk = state->disklist; node_disk != 0; node_disk = node_disk->next) {
      97           0 :                         struct snapraid_disk* disk = node_disk->data;
      98           0 :                         struct snapraid_block* block = fs_par2block_get(disk, i);
      99             : 
     100           0 :                         if (block_has_file(block))
     101           0 :                                 one_valid = 1;
     102           0 :                         if (block_has_invalid_parity(block))
     103           0 :                                 one_invalid = 1;
     104             :                 }
     105             : 
     106             :                 /* if both valid and invalid, we need to update */
     107           0 :                 if (one_invalid && one_valid)
     108           0 :                         return 1;
     109             :         }
     110             : 
     111           1 :         return 0;
     112             : }
     113             : 
     114           0 : void parity_overflow(struct snapraid_state* state, data_off_t size)
     115             : {
     116             :         tommy_node* i;
     117             :         block_off_t blockalloc;
     118           0 :         int first = 1;
     119             : 
     120             :         /* don't report if everything is outside or if the file is not accessible */
     121           0 :         if (size == 0) {
     122           0 :                 return;
     123             :         }
     124             : 
     125           0 :         blockalloc = size / state->block_size;
     126             : 
     127             :         /* for all disks */
     128           0 :         for (i = state->disklist; i != 0; i = i->next) {
     129           0 :                 struct snapraid_disk* disk = i->data;
     130             :                 tommy_node* j;
     131             : 
     132             :                 /* for all files */
     133           0 :                 for (j = disk->filelist; j != 0; j = j->next) {
     134           0 :                         struct snapraid_file* file = j->data;
     135             : 
     136           0 :                         if (file->blockmax > 0) {
     137           0 :                                 block_off_t parity_pos = fs_file2par_get(disk, file, file->blockmax - 1);
     138           0 :                                 if (parity_pos >= blockalloc) {
     139           0 :                                         log_tag("outofparity:%s:%s\n", disk->name, esc(file->sub));
     140           0 :                                         if (first) {
     141           0 :                                                 first = 0;
     142           0 :                                                 log_fatal("\nYour data requires more parity than the available space.\n");
     143           0 :                                                 log_fatal("Please move the files 'outofparity' to another data disk:\n");
     144             :                                         }
     145           0 :                                         log_fatal("outofparity %s%s\n", disk->dir, file->sub);
     146             :                                 }
     147             :                         }
     148             :                 }
     149             :         }
     150             : }
     151             : 
     152         611 : int parity_create(struct snapraid_parity_handle* parity, const char* path, data_off_t* out_size, int mode)
     153             : {
     154             :         int ret;
     155             :         int flags;
     156             : 
     157         611 :         pathcpy(parity->path, sizeof(parity->path), path);
     158             : 
     159             :         /* opening in sequential mode in Windows */
     160             :         /* O_SEQUENTIAL: opening in sequential mode in Windows */
     161         611 :         flags = O_RDWR | O_CREAT | O_BINARY;
     162         611 :         if ((mode & MODE_SEQUENTIAL) != 0)
     163         611 :                 flags |= O_SEQUENTIAL;
     164         611 :         parity->f = open(parity->path, flags, 0600);
     165         611 :         if (parity->f == -1) {
     166             :                 /* LCOV_EXCL_START */
     167             :                 log_fatal("Error opening parity file '%s'. %s.\n", parity->path, strerror(errno));
     168             :                 return -1;
     169             :                 /* LCOV_EXCL_STOP */
     170             :         }
     171             : 
     172             :         /* get the stat info */
     173         611 :         ret = fstat(parity->f, &parity->st);
     174         611 :         if (ret != 0) {
     175             :                 /* LCOV_EXCL_START */
     176             :                 log_fatal("Error accessing parity file '%s'. %s.\n", parity->path, strerror(errno));
     177             :                 goto bail;
     178             :                 /* LCOV_EXCL_STOP */
     179             :         }
     180             : 
     181             : #if HAVE_POSIX_FADVISE
     182         611 :         if ((mode & MODE_SEQUENTIAL) != 0) {
     183             :                 /* advise sequential access */
     184         611 :                 ret = posix_fadvise(parity->f, 0, 0, POSIX_FADV_SEQUENTIAL);
     185         611 :                 if (ret != 0) {
     186             :                         /* LCOV_EXCL_START */
     187             :                         log_fatal("Error advising parity file '%s'. %s.\n", parity->path, strerror(ret));
     188             :                         goto bail;
     189             :                         /* LCOV_EXCL_STOP */
     190             :                 }
     191             :         }
     192             : #endif
     193             : 
     194             :         /* get the size of the existing data */
     195         611 :         parity->valid_size = parity->st.st_size;
     196         611 :         *out_size = parity->st.st_size;
     197             : 
     198         611 :         return 0;
     199             : 
     200             : bail:
     201             :         /* LCOV_EXCL_START */
     202             :         close(parity->f);
     203             :         parity->f = -1;
     204             :         parity->valid_size = 0;
     205             :         return -1;
     206             :         /* LCOV_EXCL_STOP */
     207             : }
     208             : 
     209         611 : int parity_chsize(struct snapraid_parity_handle* parity, data_off_t size, data_off_t* out_size, int skip_fallocate)
     210             : {
     211             :         int ret;
     212             : 
     213         611 :         if (parity->st.st_size < size) {
     214             :                 int f_ret;
     215             :                 int f_errno;
     216             :                 const char* call;
     217             : 
     218             : #if HAVE_FALLOCATE
     219         101 :                 call = "fallocate";
     220         101 :                 if (!skip_fallocate) {
     221             :                         /* allocate real space using the specific Linux fallocate() operation. */
     222             :                         /* If the underline file-system doesn't support it, this operation fails, */
     223             :                         /* instead posix_fallocate() fallbacks to write the whole file. */
     224         100 :                         ret = fallocate(parity->f, 0, 0, size);
     225             : 
     226             :                         /* fallocate() returns the error number as positive integer, */
     227             :                         /* and in this case it doesn't set errno, just like posix_fallocate() */
     228             :                         /* Checking the glibc code (2.11.1 and 2.14.1) it seems that ENOSYS */
     229             :                         /* may be returned in errno, so we support both the return way */
     230         100 :                         if (ret > 0) { /* if a positive error is returned, convert it to errno */
     231             :                                 /* LCOV_EXCL_START */
     232             :                                 errno = ret;
     233             :                                 ret = -1;
     234             :                                 /* LCOV_EXCL_STOP */
     235             :                         }
     236             :                 } else {
     237           1 :                         errno = EOPNOTSUPP;
     238           1 :                         ret = -1;
     239             :                 }
     240             : 
     241             :                 /* we get EOPNOTSUPP if the operation is not supported, like in ext3/ext2 */
     242             :                 /* or ENOSYS with kernel before 2.6.23 */
     243         101 :                 if (errno == EOPNOTSUPP || errno == ENOSYS) {
     244             :                         /* fallback using ftruncate() */
     245           1 :                         call = "ftruncate";
     246           1 :                         ret = ftruncate(parity->f, size);
     247             :                 }
     248             : #else
     249             :                 (void)skip_fallocate; /* avoid the warning */
     250             : 
     251             :                 /* allocate using a sparse file */
     252             :                 call = "ftruncate";
     253             :                 ret = ftruncate(parity->f, size);
     254             : #endif
     255             :                 /* save the state of the grow operation */
     256         101 :                 f_ret = ret;
     257         101 :                 f_errno = errno;
     258             : 
     259             :                 /* get the stat info */
     260         101 :                 ret = fstat(parity->f, &parity->st);
     261         101 :                 if (ret != 0) {
     262             :                         /* LCOV_EXCL_START */
     263             :                         log_fatal("Error accessing parity file '%s'. %s.\n", parity->path, strerror(errno));
     264             :                         goto bail;
     265             :                         /* LCOV_EXCL_STOP */
     266             :                 }
     267             : 
     268             :                 /* return the new size */
     269         101 :                 *out_size = parity->st.st_size;
     270             : 
     271             :                 /* now check the error */
     272         101 :                 if (f_ret != 0) {
     273             :                         /* LCOV_EXCL_START */
     274             :                         if (f_errno == ENOSPC) {
     275             :                                 log_fatal("Failed to grow parity file '%s' to size %" PRIu64 " using %s due lack of space.\n", parity->path, size, call);
     276             :                         } else {
     277             :                                 log_fatal("Error growing parity file '%s' to size %" PRIu64 " using %s. Do you have enough space? %s.\n", parity->path, size, call, strerror(f_errno));
     278             :                         }
     279             :                         goto bail;
     280             :                         /* LCOV_EXCL_STOP */
     281             :                 }
     282         510 :         } else if (parity->st.st_size > size) {
     283             :                 int f_ret;
     284             :                 int f_errno;
     285             : 
     286             :                 /* truncate the parity file */
     287          72 :                 ret = ftruncate(parity->f, size);
     288             : 
     289             :                 /* save the state of the shrink operation */
     290          72 :                 f_ret = ret;
     291          72 :                 f_errno = errno;
     292             : 
     293             :                 /* get the stat info */
     294          72 :                 ret = fstat(parity->f, &parity->st);
     295          72 :                 if (ret != 0) {
     296             :                         /* LCOV_EXCL_START */
     297             :                         log_fatal("Error accessing parity file '%s'. %s.\n", parity->path, strerror(errno));
     298             :                         goto bail;
     299             :                         /* LCOV_EXCL_STOP */
     300             :                 }
     301             : 
     302             :                 /* return the new size */
     303          72 :                 *out_size = parity->st.st_size;
     304             : 
     305             :                 /* now check the error */
     306          72 :                 if (f_ret != 0) {
     307             :                         /* LCOV_EXCL_START */
     308             :                         log_fatal("Error truncating parity file '%s' to size %" PRIu64 ". %s.\n", parity->path, size, strerror(f_errno));
     309             :                         goto bail;
     310             :                         /* LCOV_EXCL_STOP */
     311             :                 }
     312             : 
     313             :                 /* adjust the valid to the new size */
     314          72 :                 parity->valid_size = size;
     315             :         }
     316             : 
     317         611 :         return 0;
     318             : 
     319             : bail:
     320             :         /* LCOV_EXCL_START */
     321             :         parity->valid_size = 0;
     322             :         return -1;
     323             :         /* LCOV_EXCL_STOP */
     324             : }
     325             : 
     326         402 : int parity_open(struct snapraid_parity_handle* parity, const char* path, int mode)
     327             : {
     328             :         int ret;
     329             :         int flags;
     330             : 
     331         402 :         pathcpy(parity->path, sizeof(parity->path), path);
     332             : 
     333             :         /* open for read */
     334             :         /* O_SEQUENTIAL: opening in sequential mode in Windows */
     335             :         /* O_NOATIME: do not change access time */
     336         402 :         flags = O_RDONLY | O_BINARY;
     337         402 :         if ((mode & MODE_SEQUENTIAL) != 0)
     338         401 :                 flags |= O_SEQUENTIAL;
     339         402 :         parity->f = open_noatime(parity->path, flags);
     340         402 :         if (parity->f == -1) {
     341           1 :                 log_fatal("Error opening parity file '%s'. %s.\n", parity->path, strerror(errno));
     342           1 :                 return -1;
     343             :         }
     344             : 
     345             :         /* get the stat info */
     346         401 :         ret = fstat(parity->f, &parity->st);
     347         401 :         if (ret != 0) {
     348             :                 /* LCOV_EXCL_START */
     349             :                 log_fatal("Error accessing parity file '%s'. %s.\n", parity->path, strerror(errno));
     350             :                 goto bail;
     351             :                 /* LCOV_EXCL_STOP */
     352             :         }
     353             : 
     354             :         /* get the size of the existing data */
     355         401 :         parity->valid_size = parity->st.st_size;
     356             : 
     357             : #if HAVE_POSIX_FADVISE
     358         401 :         if ((mode & MODE_SEQUENTIAL) != 0) {
     359             :                 /* advise sequential access */
     360         400 :                 ret = posix_fadvise(parity->f, 0, 0, POSIX_FADV_SEQUENTIAL);
     361         400 :                 if (ret != 0) {
     362             :                         /* LCOV_EXCL_START */
     363             :                         log_fatal("Error advising parity file '%s'. %s.\n", parity->path, strerror(ret));
     364             :                         goto bail;
     365             :                         /* LCOV_EXCL_STOP */
     366             :                 }
     367             :         }
     368             : #endif
     369             : 
     370         401 :         return 0;
     371             : 
     372             : bail:
     373             :         /* LCOV_EXCL_START */
     374             :         close(parity->f);
     375             :         parity->f = -1;
     376             :         parity->valid_size = 0;
     377             :         return -1;
     378             :         /* LCOV_EXCL_STOP */
     379             : }
     380             : 
     381         459 : int parity_sync(struct snapraid_parity_handle* parity)
     382             : {
     383             : #if HAVE_FSYNC
     384             :         int ret;
     385             : 
     386             :         /* Ensure that data changes are written to disk. */
     387             :         /* This is required to ensure that parity is more updated than content */
     388             :         /* in case of a system crash. */
     389         459 :         ret = fsync(parity->f);
     390         459 :         if (ret != 0) {
     391             :                 /* LCOV_EXCL_START */
     392             :                 log_fatal("Error synching parity file '%s'. %s.\n", parity->path, strerror(errno));
     393             :                 return -1;
     394             :                 /* LCOV_EXCL_STOP */
     395             :         }
     396             : #endif
     397             : 
     398         459 :         return 0;
     399             : }
     400             : 
     401        1012 : int parity_close(struct snapraid_parity_handle* parity)
     402             : {
     403             :         int ret;
     404             : 
     405        1012 :         ret = close(parity->f);
     406        1012 :         if (ret != 0) {
     407             :                 /* LCOV_EXCL_START */
     408             :                 /* invalidate for error */
     409             :                 parity->f = -1;
     410             :                 parity->valid_size = 0;
     411             : 
     412             :                 /* This is a serious error, as it may be the result of a failed write */
     413             :                 /* identified at later time. */
     414             :                 /* In a normal file-system (not NFS) it should never happen */
     415             :                 log_fatal("Error closing parity file '%s'. %s.\n", parity->path, strerror(errno));
     416             :                 return -1;
     417             :                 /* LCOV_EXCL_STOP */
     418             :         }
     419             : 
     420             :         /* reset the descriptor */
     421        1012 :         parity->f = -1;
     422        1012 :         parity->valid_size = 0;
     423             : 
     424        1012 :         return 0;
     425             : }
     426             : 
     427      560054 : int parity_write(struct snapraid_parity_handle* parity, block_off_t pos, unsigned char* block_buffer, unsigned block_size)
     428             : {
     429             :         ssize_t write_ret;
     430             :         data_off_t offset;
     431             : 
     432      560054 :         offset = pos * (data_off_t)block_size;
     433             : 
     434             : #if HAVE_PWRITE
     435      560054 :         write_ret = pwrite(parity->f, block_buffer, block_size, offset);
     436             : #else
     437             :         if (lseek(parity->f, offset, SEEK_SET) != offset) {
     438             :                 /* LCOV_EXCL_START */
     439             :                 if (errno == ENOSPC) {
     440             :                         log_fatal("Failed to grow parity file '%s' using lseek due lack of space.\n", parity->path);
     441             :                 } else {
     442             :                         log_fatal("Error seeking file '%s'. %s.\n", parity->path, strerror(errno));
     443             :                 }
     444             :                 return -1;
     445             :                 /* LCOV_EXCL_STOP */
     446             :         }
     447             : 
     448             :         write_ret = write(parity->f, block_buffer, block_size);
     449             : #endif
     450      560054 :         if (write_ret != (ssize_t)block_size) { /* conversion is safe because block_size is always small */
     451             :                 /* LCOV_EXCL_START */
     452             :                 if (errno == ENOSPC) {
     453             :                         log_fatal("Failed to grow parity file '%s' using write due lack of space.\n", parity->path);
     454             :                 } else {
     455             :                         log_fatal("Error writing file '%s'. %s.\n", parity->path, strerror(errno));
     456             :                 }
     457             :                 return -1;
     458             :                 /* LCOV_EXCL_STOP */
     459             :         }
     460             : 
     461             :         /* adjust the size of the valid data */
     462      560054 :         if (parity->valid_size < offset + block_size) {
     463      171013 :                 parity->valid_size = offset + block_size;
     464             :         }
     465             : 
     466             :         /* Here doesn't make sense to call posix_fadvise(..., POSIX_FADV_DONTNEED) because */
     467             :         /* at this time the data is still in not yet written and it cannot be discharged. */
     468             : 
     469      560054 :         return 0;
     470             : }
     471             : 
     472     3496527 : int parity_read(struct snapraid_parity_handle* parity, block_off_t pos, unsigned char* block_buffer, unsigned block_size, fptr* out)
     473             : {
     474             :         ssize_t read_ret;
     475             :         data_off_t offset;
     476             :         unsigned count;
     477             : 
     478     3496527 :         offset = pos * (data_off_t)block_size;
     479             : 
     480             :         /* check if we are going to read only not initialized data */
     481     3496527 :         if (offset >= parity->valid_size) {
     482       90335 :                 out("Reading missing data from file '%s' at offset %" PRIu64 ".\n", parity->path, offset);
     483       90335 :                 return -1;
     484             :         }
     485             : 
     486             : #if !HAVE_PREAD
     487             :         if (lseek(parity->f, offset, SEEK_SET) != offset) {
     488             :                 /* LCOV_EXCL_START */
     489             :                 out("Error seeking file '%s' at offset %" PRIu64 ". %s.\n", parity->path, offset, strerror(errno));
     490             :                 return -1;
     491             :                 /* LCOV_EXCL_STOP */
     492             :         }
     493             : #endif
     494             : 
     495     3406192 :         count = 0;
     496             :         do {
     497             : 
     498             : #if HAVE_PREAD
     499     3406192 :                 read_ret = pread(parity->f, block_buffer + count, block_size - count, offset + count);
     500             : #else
     501             :                 read_ret = read(parity->f, block_buffer + count, block_size - count);
     502             : #endif
     503     3406192 :                 if (read_ret < 0) {
     504             :                         /* LCOV_EXCL_START */
     505             :                         out("Error reading file '%s' at offset %" PRIu64 " for size %u. %s.\n", parity->path, offset + count, block_size - count, strerror(errno));
     506             :                         return -1;
     507             :                         /* LCOV_EXCL_STOP */
     508             :                 }
     509     3406192 :                 if (read_ret == 0) {
     510             :                         /* LCOV_EXCL_START */
     511             :                         out("Unexpected end of file '%s' at offset %" PRIu64 ". %s.\n", parity->path, offset, strerror(errno));
     512             :                         return -1;
     513             :                         /* LCOV_EXCL_STOP */
     514             :                 }
     515             : 
     516     3406192 :                 count += read_ret;
     517     3406192 :         } while (count < block_size);
     518             : 
     519             :         /* Here isn't needed to call posix_fadvise(..., POSIX_FADV_DONTNEED) */
     520             :         /* because we already advised sequential access with POSIX_FADV_SEQUENTIAL. */
     521             :         /* In Linux 2.6.33 it's enough to ensure that data is not kept in the cache. */
     522             :         /* Better to do nothing and save a syscall for each block. */
     523             : 
     524     3406192 :         return block_size;
     525             : }
     526             : 

Generated by: LCOV version 1.10