LCOV - code coverage report
Current view: top level - cmdline - parity.c (source / functions) Hit Total Coverage
Test: lcov.info Lines: 232 254 91.3 %
Date: 2026-03-15 15:58:19 Functions: 18 19 94.7 %

          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             :  * Pseudo random limits for parity
      28             :  */
      29             : #define PARITY_LIMIT(size, split, level) \
      30             :         size ? size + (123562341 + split * 634542351 + level * 983491341) % size : 0
      31             : 
      32             : /****************************************************************************/
      33             : /* parity */
      34             : 
      35         688 : block_off_t parity_allocated_size(struct snapraid_state* state)
      36             : {
      37             :         block_off_t parity_block;
      38             :         tommy_node* i;
      39             : 
      40             :         /* compute the size of the parity file */
      41         688 :         parity_block = 0;
      42        4806 :         for (i = state->disklist; i != 0; i = i->next) {
      43        4118 :                 struct snapraid_disk* disk = i->data;
      44             : 
      45             :                 /* start from the declared size */
      46        4118 :                 block_off_t block = fs_size(disk);
      47             : 
      48             :                 /* decrease the block until an allocated one, but part of a file */
      49             :                 /* we don't stop at deleted blocks, because we want to have them cleared */
      50             :                 /* if they are at the end of the parity */
      51      187957 :                 while (block > parity_block && !block_has_file(fs_par2block_find(disk, block - 1)))
      52      183839 :                         --block;
      53             : 
      54             :                 /* get the highest value */
      55        4118 :                 if (block > parity_block)
      56        2407 :                         parity_block = block;
      57             :         }
      58             : 
      59         688 :         return parity_block;
      60             : }
      61             : 
      62          96 : block_off_t parity_used_size(struct snapraid_state* state)
      63             : {
      64             :         block_off_t parity_block;
      65             :         tommy_node* i;
      66             : 
      67             :         /* compute the size of the parity file */
      68          96 :         parity_block = 0;
      69         671 :         for (i = state->disklist; i != 0; i = i->next) {
      70         575 :                 struct snapraid_disk* disk = i->data;
      71             : 
      72             :                 /* start from the declared size */
      73         575 :                 block_off_t block = fs_size(disk);
      74             : 
      75             :                 /* decrease the block until an used one */
      76      223640 :                 while (block > parity_block && !block_has_file_and_valid_parity(fs_par2block_find(disk, block - 1)))
      77      223065 :                         --block;
      78             : 
      79             :                 /* get the highest value */
      80         575 :                 if (block > parity_block)
      81         318 :                         parity_block = block;
      82             :         }
      83             : 
      84          96 :         return parity_block;
      85             : }
      86             : 
      87           0 : void parity_overflow(struct snapraid_state* state, data_off_t size)
      88             : {
      89             :         tommy_node* i;
      90             :         block_off_t blockalloc;
      91           0 :         int found = 0;
      92             :         char esc_buffer[ESC_MAX];
      93             : 
      94             :         /* don't report if everything is outside or if the file is not accessible */
      95           0 :         if (size == 0) {
      96           0 :                 return;
      97             :         }
      98             : 
      99           0 :         blockalloc = size / state->block_size;
     100             : 
     101             :         /* for all disks */
     102           0 :         for (i = state->disklist; i != 0; i = i->next) {
     103           0 :                 struct snapraid_disk* disk = i->data;
     104             :                 tommy_node* j;
     105             : 
     106             :                 /* for all files */
     107           0 :                 for (j = disk->filelist; j != 0; j = j->next) {
     108           0 :                         struct snapraid_file* file = j->data;
     109             : 
     110           0 :                         if (file->blockmax > 0) {
     111           0 :                                 block_off_t parity_pos = fs_file2par_get(disk, file, file->blockmax - 1);
     112           0 :                                 if (parity_pos >= blockalloc) {
     113           0 :                                         found = 1;
     114           0 :                                         log_tag("outofparity:%s:%s\n", disk->name, esc_tag(file->sub, esc_buffer));
     115           0 :                                         log_fatal(ESOFT, "outofparity %s%s\n", disk->dir, file->sub);
     116             :                                 }
     117             :                         }
     118             :                 }
     119             :         }
     120             : 
     121           0 :         if (found) {
     122           0 :                 log_fatal(ESOFT, "\nInsufficient parity space. Data requires more parity than available.\n");
     123           0 :                 log_fatal(ESOFT, "Move the 'outofparity' files to a larger disk.\n");
     124             :         }
     125             : }
     126             : 
     127         531 : void parity_size(struct snapraid_parity_handle* handle, data_off_t* out_size)
     128             : {
     129             :         unsigned s;
     130             :         data_off_t size;
     131             : 
     132             :         /* now compute the size summing all the parity splits */
     133         531 :         size = 0;
     134             : 
     135        2655 :         for (s = 0; s < handle->split_mac; ++s) {
     136        2124 :                 struct snapraid_split_handle* split = &handle->split_map[s];
     137             : 
     138        2124 :                 size += split->size;
     139             :         }
     140             : 
     141         531 :         *out_size = size;
     142         531 : }
     143             : 
     144         657 : int parity_create(struct snapraid_parity_handle* handle, const struct snapraid_parity* parity, unsigned level, int mode, uint32_t block_size, data_off_t limit_size)
     145             : {
     146             :         unsigned s;
     147             :         data_off_t block_mask;
     148             : 
     149             :         /* mask of bits used by the block size */
     150         657 :         block_mask = ((data_off_t)block_size) - 1;
     151             : 
     152         657 :         handle->level = level;
     153         657 :         handle->split_mac = 0;
     154             : 
     155        3285 :         for (s = 0; s < parity->split_mac; ++s) {
     156        2628 :                 struct snapraid_split_handle* split = &handle->split_map[s];
     157             :                 int ret;
     158             :                 int flags;
     159             : 
     160        2628 :                 advise_init(&split->advise, mode);
     161        2628 :                 pathcpy(split->path, sizeof(split->path), parity->split_map[s].path);
     162        2628 :                 split->size = parity->split_map[s].size;
     163        2628 :                 split->limit_size = PARITY_LIMIT(limit_size, s, level);
     164             : 
     165             :                 /* opening in sequential mode in Windows */
     166        2628 :                 flags = O_RDWR | O_CREAT | O_BINARY | advise_flags(&split->advise);
     167        2628 :                 split->f = open(split->path, flags, 0600);
     168        2628 :                 if (split->f == -1) {
     169             :                         /* LCOV_EXCL_START */
     170             :                         log_fatal(errno, "Error opening parity file '%s'. %s.\n", split->path, strerror(errno));
     171             :                         goto bail;
     172             :                         /* LCOV_EXCL_STOP */
     173             :                 }
     174             : 
     175             :                 /* we have a valid file handle */
     176        2628 :                 ++handle->split_mac;
     177             : 
     178             :                 /* get the stat info */
     179        2628 :                 ret = fstat(split->f, &split->st);
     180        2628 :                 if (ret != 0) {
     181             :                         /* LCOV_EXCL_START */
     182             :                         log_fatal(errno, "Error accessing parity file '%s'. %s.\n", split->path, strerror(errno));
     183             :                         goto bail;
     184             :                         /* LCOV_EXCL_STOP */
     185             :                 }
     186             : 
     187             :                 /* the initial valid size is the size on disk */
     188        2628 :                 split->valid_size = split->st.st_size;
     189             : 
     190             :                 /**
     191             :                  * If the parity size is not yet set, set it now.
     192             :                  * This happens when expanding the number of parities,
     193             :                  * or when upgrading from a content file that has not split->size data.
     194             :                  */
     195        2628 :                 if (split->size == PARITY_SIZE_INVALID) {
     196         144 :                         split->size = split->st.st_size;
     197             : 
     198             :                         /* ensure that the resulting size if block aligned */
     199         144 :                         if ((split->size & block_mask) != 0) {
     200             :                                 /* LCOV_EXCL_START */
     201             :                                 log_fatal(ESOFT, "Error in preallocated size of parity file '%s' with size %" PRIu64 " and block %u .\n", split->path, split->size, block_size);
     202             :                                 goto bail;
     203             :                                 /* LCOV_EXCL_STOP */
     204             :                         }
     205             :                 }
     206             : 
     207        2628 :                 ret = advise_open(&split->advise, split->f);
     208        2628 :                 if (ret != 0) {
     209             :                         /* LCOV_EXCL_START */
     210             :                         log_fatal(errno, "Error advising parity file '%s'. %s.\n", split->path, strerror(errno));
     211             :                         goto bail;
     212             :                         /* LCOV_EXCL_STOP */
     213             :                 }
     214             :         }
     215             : 
     216         657 :         return 0;
     217             : 
     218           0 : bail:
     219             :         /* LCOV_EXCL_START */
     220             :         for (s = 0; s < handle->split_mac; ++s) {
     221             :                 struct snapraid_split_handle* split = &handle->split_map[s];
     222             :                 close(split->f);
     223             :                 split->f = -1;
     224             :         }
     225             :         return -1;
     226             :         /* LCOV_EXCL_STOP */
     227             : }
     228             : 
     229         839 : static int parity_handle_grow(struct snapraid_split_handle* split, data_off_t previous_size, data_off_t size, int skip_fallocate)
     230             : {
     231             :         int ret;
     232             : 
     233             :         (void)previous_size;
     234             : 
     235             :         /* simulate a failure for testing limits */
     236         839 :         if (split->limit_size != 0 && size > (data_off_t)split->limit_size) {
     237         140 :                 errno = ENXIO;
     238         140 :                 return -1;
     239             :         }
     240             : 
     241             : #if HAVE_FALLOCATE
     242         699 :         if (!skip_fallocate) {
     243             :                 /*
     244             :                  * Allocate real space using the specific Linux fallocate() operation.
     245             :                  * If the underline file-system doesn't support it, this operation fails.
     246             :                  *
     247             :                  * Instead posix_fallocate() fallbacks to write the whole file,
     248             :                  * and we cannot use it as we may need to initialize a multi terabyte
     249             :                  * file.
     250             :                  *
     251             :                  * See: fallocate vs posix_fallocate
     252             :                  * http://stackoverflow.com/questions/14063046/fallocate-vs-posix-fallocate
     253             :                  *
     254             :                  * To work better with Btrfs, use as offset the previous allocated size.
     255             :                  * Otherwise Btrfs will count as space needed even the already allocated one.
     256             :                  *
     257             :                  * See: Massive loss of disk space
     258             :                  * https://www.mail-archive.com/linux-btrfs@vger.kernel.org/msg66454.html
     259             :                  */
     260         688 :                 ret = fallocate(split->f, 0, previous_size, size - previous_size);
     261             : 
     262             :                 /*
     263             :                  * In some legacy system fallocate() may return the error number
     264             :                  * as  positive integer, and in this case it doesn't set errno.
     265             :                  *
     266             :                  * Detect and handle this case.
     267             :                  *
     268             :                  * See: Fix fallocate error return on i386
     269             :                  * https://sourceware.org/ml/libc-hacker/2010-04/msg00000.html
     270             :                  *
     271             :                  * See: [PATCH XFS] Fix error return for fallocate() on XFS
     272             :                  * http://oss.sgi.com/archives/xfs/2009-11/msg00201.html
     273             :                  */
     274         688 :                 if (ret > 0) {
     275             :                         /* LCOV_EXCL_START */
     276             :                         errno = ret;
     277             :                         ret = -1;
     278             :                         /* LCOV_EXCL_STOP */
     279             :                 }
     280             :         } else {
     281          11 :                 errno = EOPNOTSUPP;
     282          11 :                 ret = -1;
     283             :         }
     284             : 
     285             :         /*
     286             :          * Fallback to ftruncate() if the operation is not supported.
     287             :          *
     288             :          * We get EOPNOTSUPP if the operation is not supported, like in ext3/ext2
     289             :          * or ENOSYS with kernel before 2.6.23, because fallocate is not supported
     290             :          * at all.
     291             :          *
     292             :          * See: man fallocate
     293             :          * ENOSYS - This kernel does not implement fallocate().
     294             :          * EOPNOTSUPP - The file system containing the file referred to by fd does not support this operation
     295             :          */
     296         699 :         if (ret != 0 && (errno == EOPNOTSUPP || errno == ENOSYS)) {
     297             :                 /* fallback using ftruncate() */
     298          11 :                 ret = ftruncate(split->f, size);
     299             :         }
     300             : #else
     301             :         (void)skip_fallocate; /* avoid the warning */
     302             : 
     303             :         /* allocate using a sparse file */
     304             :         ret = ftruncate(split->f, size);
     305             : #endif
     306             : 
     307         699 :         if (ret != 0)
     308           0 :                 log_tag("split:grow:%s:%" PRIu64 ": failed with error %s\n", split->path, size, strerror(errno));
     309             :         else
     310         699 :                 log_tag("split:grow:%s:%" PRIu64 ": ok\n", split->path, size);
     311             : 
     312         699 :         return ret;
     313             : }
     314             : 
     315         235 : static int parity_handle_shrink(struct snapraid_split_handle* split, data_off_t size)
     316             : {
     317             :         int ret;
     318             : 
     319         235 :         ret = ftruncate(split->f, size);
     320             : 
     321         235 :         if (ret != 0)
     322           0 :                 log_tag("split:shrink:%s:%" PRIu64 ": failed with error %s\n", split->path, size, strerror(errno));
     323             :         else
     324         235 :                 log_tag("split:shrink:%s:%" PRIu64 ": ok\n", split->path, size);
     325             : 
     326         235 :         return ret;
     327             : }
     328             : 
     329             : /**
     330             :  * Get the highest bit set.
     331             :  */
     332         839 : uint64_t hbit_u64(uint64_t v)
     333             : {
     334             :         unsigned ilog;
     335             : 
     336         839 :         ilog = 0;
     337       13639 :         while ((v /= 2) != 0)
     338       12800 :                 ++ilog;
     339             : 
     340         839 :         return 1ULL << ilog;
     341             : }
     342             : 
     343         147 : static int parity_handle_fill(struct snapraid_split_handle* split, data_off_t size, uint32_t block_size, int skip_fallocate, int skip_space_holder)
     344             : {
     345             :         data_off_t base;
     346             :         data_off_t delta;
     347             :         data_off_t block_mask;
     348             : 
     349             : #ifdef _WIN32
     350             :         /*
     351             :          * In Windows we want to avoid the annoying warning
     352             :          * message of disk full.
     353             :          *
     354             :          * To ensure to leave some space available, we first create
     355             :          * a spaceholder file >200 MB, to ensure to not fill completely
     356             :          * the disk.
     357             :          */
     358             :         char spaceholder_path[PATH_MAX];
     359             : 
     360             :         pathprint(spaceholder_path, sizeof(spaceholder_path), "%s%s", split->path, ".spaceholder");
     361             : 
     362             :         if (!skip_space_holder) {
     363             :                 int spaceholder_f;
     364             : 
     365             :                 spaceholder_f = open(spaceholder_path, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0600);
     366             :                 if (spaceholder_f == -1) {
     367             :                         log_fatal(errno, "Failed to create space holder file '%s'.\n", spaceholder_path);
     368             :                         return -1;
     369             :                 }
     370             : 
     371             :                 /* note that in Windows ftruncate is really allocating space */
     372             :                 if (ftruncate(spaceholder_f, WINDOWS_SPACEHOLDER_SIZE) != 0) {
     373             :                         log_fatal(errno, "WARNING Failed to resize the space holder file '%s' to %u bytes.\n", spaceholder_path, WINDOWS_SPACEHOLDER_SIZE);
     374             :                         log_fatal(errno, "Assuming that no more space is available.\n");
     375             :                         close(spaceholder_f);
     376             :                         remove(spaceholder_path);
     377             :                         return 0;
     378             :                 }
     379             : 
     380             :                 if (fsync(spaceholder_f) != 0) {
     381             :                         log_fatal(errno, "Failed to sync the space holder file '%s'.\n", spaceholder_path);
     382             :                         close(spaceholder_f);
     383             :                         remove(spaceholder_path);
     384             :                         return -1;
     385             :                 }
     386             : 
     387             :                 if (close(spaceholder_f) != 0) {
     388             :                         log_fatal(errno, "Failed to close the space holder file '%s'.\n", spaceholder_path);
     389             :                         remove(spaceholder_path);
     390             :                         return -1;
     391             :                 }
     392             :         }
     393             : #else
     394             :         (void)skip_space_holder;
     395             : #endif
     396             : 
     397             :         /* mask of bits used by the block size */
     398         147 :         block_mask = ((data_off_t)block_size) - 1;
     399             : 
     400             :         /* present size */
     401         147 :         base = split->st.st_size;
     402             : 
     403             :         /* truncate it to block size multiplier */
     404             :         /* in case of damage the size may get wrong */
     405         147 :         base &= ~block_mask;
     406             : 
     407             :         /* size we have to increase */
     408         147 :         delta = size - base;
     409             : 
     410         147 :         log_tag("split:fill:%s:%" PRIu64 ":%" PRIu64 ":\n", split->path, base, size);
     411             : 
     412             :         /* grow the size one bit at time, like a kind of binary search */
     413         986 :         while (delta != 0) {
     414             :                 int ret;
     415         839 :                 data_off_t run = hbit_u64(delta);
     416             : 
     417             :                 /* mask out the bit we process */
     418         839 :                 delta &= ~run;
     419             : 
     420         839 :                 log_tag("split:delta:%s:%" PRIu64 ":%" PRIu64 ":\n", split->path, base, run);
     421             : 
     422         839 :                 ret = parity_handle_grow(split, base, base + run, skip_fallocate);
     423         839 :                 if (ret != 0) {
     424             :                         /* we cannot grow, fallback enabling all the smaller bits */
     425         140 :                         delta = run - 1;
     426             : 
     427             :                         /* mask out the block size */
     428         140 :                         delta &= ~block_mask;
     429             :                 } else {
     430             :                         /* increase the effective size */
     431         699 :                         base += run;
     432             :                 }
     433             :         }
     434             : 
     435             :         /* ensure that the resulting size if block aligned */
     436         147 :         if ((base & block_mask) != 0) {
     437             :                 /* LCOV_EXCL_START */
     438             :                 log_fatal(EINTERNAL, "Internal inconsistency in requested parity size %" PRIu64 " with block %u\n", base, block_size);
     439             :                 os_abort();
     440             :                 /* LCOV_EXCL_STOP */
     441             :         }
     442             : 
     443             : #ifdef _WIN32
     444             :         /* now delete the spaceholder file */
     445             :         if (remove(spaceholder_path) != 0) {
     446             :                 log_error(errno, "WARNING Failed to remove the space holder file '%s'.\n", spaceholder_path);
     447             :                 log_error(errno, "Continuing anyway.\n");
     448             :         }
     449             : #endif
     450             : 
     451             :         /* shrink to the expected size to ensure to throw away any extra */
     452             :         /* data allocated when the grow operation fails */
     453         147 :         return parity_handle_shrink(split, base);
     454             : }
     455             : 
     456        2604 : static int parity_handle_chsize(struct snapraid_split_handle* split, data_off_t size, uint32_t block_size, int skip_fallocate, int skip_space_holder)
     457             : {
     458             :         int ret;
     459             :         int f_ret;
     460             :         int f_errno;
     461             :         int f_dir;
     462             : 
     463        2604 :         if (split->st.st_size < size) {
     464         147 :                 f_ret = parity_handle_fill(split, size, block_size, skip_fallocate, skip_space_holder);
     465         147 :                 f_errno = errno;
     466         147 :                 f_dir = 1;
     467        2457 :         } else if (split->st.st_size > size) {
     468          88 :                 f_ret = parity_handle_shrink(split, size);
     469          88 :                 f_errno = errno;
     470          88 :                 f_dir = -1;
     471             :         } else {
     472        2369 :                 f_ret = 0;
     473        2369 :                 f_errno = 0;
     474        2369 :                 f_dir = 0;
     475             :         }
     476             : 
     477             :         /* get the stat info */
     478        2604 :         ret = fstat(split->f, &split->st);
     479        2604 :         if (ret != 0) {
     480             :                 /* LCOV_EXCL_START */
     481             :                 log_fatal(errno, "Error accessing parity file '%s'. %s.\n", split->path, strerror(errno));
     482             :                 return -1;
     483             :                 /* LCOV_EXCL_STOP */
     484             :         }
     485             : 
     486             :         /* now check the error */
     487        2604 :         if (f_ret != 0) {
     488             :                 /* LCOV_EXCL_START */
     489             :                 if (f_dir > 0) {
     490             :                         if (f_errno == ENOSPC) {
     491             :                                 log_fatal(errno, "Failed to grow parity file '%s' to size %" PRIu64 " due lack of space.\n", split->path, size);
     492             :                         } else {
     493             :                                 log_fatal(errno, "Error growing parity file '%s' to size %" PRIu64 ". Do you have enough space? %s.\n", split->path, size, strerror(f_errno));
     494             :                         }
     495             :                 } else {
     496             :                         log_fatal(errno, "Error truncating parity file '%s' to size %" PRIu64 ". %s.\n", split->path, size, strerror(f_errno));
     497             :                 }
     498             :                 return -1;
     499             :                 /* LCOV_EXCL_STOP */
     500             :         }
     501             : 
     502             :         /* if we shrink, update the valid size, but don't update when growing */
     503        2604 :         if (split->valid_size > split->st.st_size)
     504          88 :                 split->valid_size = split->st.st_size;
     505             : 
     506        2604 :         return 0;
     507             : }
     508             : 
     509        2604 : static int parity_split_is_fixed(struct snapraid_parity_handle* handle, unsigned s)
     510             : {
     511             :         /* next one */
     512        2604 :         ++s;
     513             : 
     514             :         /* the latest one is always growing */
     515        2604 :         if (s >= handle->split_mac)
     516         651 :                 return 0;
     517             : 
     518             :         /* if the next it's 0, this one is growing */
     519        1953 :         if (handle->split_map[s].size == 0)
     520        1321 :                 return 0;
     521             : 
     522         632 :         return 1;
     523             : }
     524             : 
     525         651 : int parity_chsize(struct snapraid_parity_handle* handle, struct snapraid_parity* parity, int* is_modified, data_off_t size, uint32_t block_size, int skip_fallocate, int skip_space_holder)
     526             : {
     527             :         int ret;
     528             :         unsigned s;
     529             :         data_off_t block_mask;
     530             : 
     531             :         /* mask of bits used by the block size */
     532         651 :         block_mask = ((data_off_t)block_size) - 1;
     533             : 
     534         651 :         if (size < 0) {
     535             :                 /* LCOV_EXCL_START */
     536             :                 errno = ENXIO;
     537             :                 return -1;
     538             :                 /* LCOV_EXCL_STOP */
     539             :         }
     540             : 
     541        3255 :         for (s = 0; s < handle->split_mac; ++s) {
     542        2604 :                 struct snapraid_split_handle* split = &handle->split_map[s];
     543        2604 :                 int is_fixed = parity_split_is_fixed(handle, s);
     544             :                 data_off_t run;
     545             : 
     546        2604 :                 if (is_fixed) {
     547             :                         /* if the required size is smaller, we have to reduce also the file */
     548             :                         /* ignoring the previous size */
     549         632 :                         if (size <= split->size) {
     550             :                                 /* mark it as not fixed anymore for the later check */
     551           9 :                                 is_fixed = 0;
     552             : 
     553           9 :                                 run = size; /* allocate only the needed size */
     554             :                         } else {
     555             :                                 /* if the size cannot be changed, use the fixed one */
     556         623 :                                 run = split->size;
     557             : 
     558         623 :                                 if ((run & block_mask) != 0) {
     559             :                                         /* LCOV_EXCL_START */
     560             :                                         errno = ENXIO;
     561             :                                         log_fatal(EINTERNAL, "Internal inconsistency in split '%s' size with extra '%" PRIu64 "' bytes.\n", split->path, run & block_mask);
     562             :                                         return -1;
     563             :                                         /* LCOV_EXCL_STOP */
     564             :                                 }
     565             :                         }
     566             :                 } else {
     567             :                         /* otherwise tries to allocate all the needed remaining size */
     568        1972 :                         run = size;
     569             :                 }
     570             : 
     571        2604 :                 ret = parity_handle_chsize(split, run, block_size, skip_fallocate, skip_space_holder);
     572        2604 :                 if (ret != 0) {
     573             :                         /* LCOV_EXCL_START */
     574             :                         return -1;
     575             :                         /* LCOV_EXCL_STOP */
     576             :                 }
     577             : 
     578        2604 :                 if (split->st.st_size > run) {
     579             :                         /* LCOV_EXCL_START */
     580             :                         errno = ENXIO;
     581             :                         log_fatal(errno, "Unexpected over resizing parity file '%s' to size %" PRIu64 " resulting in size %" PRIu64 ".\n", split->path, run, (uint64_t)split->st.st_size);
     582             :                         return -1;
     583             :                         /* LCOV_EXCL_STOP */
     584        2604 :                 } else if (is_fixed && split->st.st_size < run) {
     585             :                         /* LCOV_EXCL_START */
     586             :                         errno = ENXIO;
     587             :                         log_fatal(errno, "Failed restoring parity file '%s' to size %" PRIu64 " resulting in size %" PRIu64 ".\n", split->path, run, (uint64_t)split->st.st_size);
     588             :                         return -1;
     589             :                         /* LCOV_EXCL_STOP */
     590             :                 } else {
     591             :                         /* here it's possible to get less than the requested size */
     592        2604 :                         run = split->st.st_size;
     593             : 
     594        2604 :                         if ((run & block_mask) != 0) {
     595             :                                 /* LCOV_EXCL_START */
     596             :                                 log_fatal(EINTERNAL, "Internal inconsistency in final parity size %" PRIu64 " with block size %u\n", run, block_size);
     597             :                                 os_abort();
     598             :                                 /* LCOV_EXCL_STOP */
     599             :                         }
     600             : 
     601             :                         /* store what we have allocated */
     602        2604 :                         split->size = run;
     603             : 
     604             :                         /* decrease the remaining size */
     605        2604 :                         size -= run;
     606             :                 }
     607             :         }
     608             : 
     609             :         /* if we cannot allocate all the space */
     610         651 :         if (size != 0) {
     611             :                 /* LCOV_EXCL_START */
     612             :                 errno = ENXIO;
     613             :                 log_fatal(errno, "Failed to allocate all the required parity space. You miss %" PRIu64 " bytes.\n", size);
     614             :                 return -1;
     615             :                 /* LCOV_EXCL_STOP */
     616             :         }
     617             : 
     618             :         /* now copy the new size in the parity data */
     619         651 :         if (is_modified)
     620         525 :                 *is_modified = 0;
     621             : 
     622        3255 :         for (s = 0; s < handle->split_mac; ++s) {
     623        2604 :                 struct snapraid_split_handle* split = &handle->split_map[s];
     624             : 
     625        2604 :                 if (parity->split_map[s].size != split->size) {
     626         346 :                         parity->split_map[s].size = split->size;
     627         346 :                         if (is_modified)
     628         290 :                                 *is_modified = 1;
     629             :                 }
     630             :         }
     631             : 
     632         651 :         return 0;
     633             : }
     634             : 
     635         431 : int parity_open(struct snapraid_parity_handle* handle, const struct snapraid_parity* parity, unsigned level, int mode, uint32_t block_size, data_off_t limit_size)
     636             : {
     637             :         unsigned s;
     638             :         data_off_t block_mask;
     639             : 
     640         431 :         handle->level = level;
     641         431 :         handle->split_mac = 0;
     642             : 
     643             :         /* mask of bits used by the block size */
     644         431 :         block_mask = ((data_off_t)block_size) - 1;
     645             : 
     646        2151 :         for (s = 0; s < parity->split_mac; ++s) {
     647        1721 :                 struct snapraid_split_handle* split = &handle->split_map[s];
     648             :                 int ret;
     649             :                 int flags;
     650             : 
     651        1721 :                 advise_init(&split->advise, mode);
     652        1721 :                 pathcpy(split->path, sizeof(split->path), parity->split_map[s].path);
     653        1721 :                 split->size = parity->split_map[s].size;
     654        1721 :                 split->limit_size = PARITY_LIMIT(limit_size, s, level);
     655             : 
     656             :                 /* open for read */
     657             :                 /* O_NOATIME: do not change access time */
     658        1721 :                 flags = O_RDONLY | O_BINARY | advise_flags(&split->advise);
     659             : 
     660        1721 :                 split->f = open_noatime(split->path, flags);
     661        1721 :                 if (split->f == -1) {
     662             :                         /* LCOV_EXCL_START */
     663             :                         log_fatal(errno, "Error opening parity file '%s'. %s.\n", split->path, strerror(errno));
     664             :                         goto bail;
     665             :                         /* LCOV_EXCL_STOP */
     666             :                 }
     667             : 
     668             :                 /* we have a valid file handle */
     669        1720 :                 ++handle->split_mac;
     670             : 
     671             :                 /* get the stat info */
     672        1720 :                 ret = fstat(split->f, &split->st);
     673        1720 :                 if (ret != 0) {
     674             :                         /* LCOV_EXCL_START */
     675             :                         log_fatal(errno, "Error accessing parity file '%s'. %s.\n", split->path, strerror(errno));
     676             :                         goto bail;
     677             :                         /* LCOV_EXCL_STOP */
     678             :                 }
     679             : 
     680             :                 /* the initial valid size is the size on disk */
     681        1720 :                 split->valid_size = split->st.st_size;
     682             : 
     683             :                 /**
     684             :                  * If the parity size is not yet set, set it now.
     685             :                  * This happens when expanding the number of parities,
     686             :                  * or when upgrading from a content file that has not split->size data.
     687             :                  */
     688        1720 :                 if (split->size == PARITY_SIZE_INVALID) {
     689          40 :                         split->size = split->st.st_size;
     690             : 
     691             :                         /* ensure that the resulting size if block aligned */
     692          40 :                         if ((split->size & block_mask) != 0) {
     693             :                                 /* LCOV_EXCL_START */
     694             :                                 log_fatal(ESOFT, "Error in preallocated size of parity file '%s' with size %" PRIu64 " and block %u .\n", split->path, split->size, block_size);
     695             :                                 goto bail;
     696             :                                 /* LCOV_EXCL_STOP */
     697             :                         }
     698             :                 }
     699             : 
     700        1720 :                 ret = advise_open(&split->advise, split->f);
     701        1720 :                 if (ret != 0) {
     702             :                         /* LCOV_EXCL_START */
     703             :                         log_fatal(errno, "Error advising parity file '%s'. %s.\n", split->path, strerror(errno));
     704             :                         goto bail;
     705             :                         /* LCOV_EXCL_STOP */
     706             :                 }
     707             :         }
     708             : 
     709         430 :         return 0;
     710             : 
     711           1 : bail:
     712             :         /* LCOV_EXCL_START */
     713             :         for (s = 0; s < handle->split_mac; ++s) {
     714             :                 struct snapraid_split_handle* split = &handle->split_map[s];
     715             :                 close(split->f);
     716             :                 split->f = -1;
     717             :         }
     718             :         return -1;
     719             :         /* LCOV_EXCL_STOP */
     720             : }
     721             : 
     722         513 : int parity_sync(struct snapraid_parity_handle* handle)
     723             : {
     724             : #if HAVE_FSYNC
     725             :         unsigned s;
     726             : 
     727        2565 :         for (s = 0; s < handle->split_mac; ++s) {
     728        2052 :                 struct snapraid_split_handle* split = &handle->split_map[s];
     729             :                 int ret;
     730             : 
     731             :                 /* Ensure that data changes are written to disk. */
     732             :                 /* This is required to ensure that parity is more updated than content */
     733             :                 /* in case of a system crash. */
     734        2052 :                 ret = fsync(split->f);
     735        2052 :                 if (ret != 0) {
     736             :                         /* LCOV_EXCL_START */
     737             :                         log_fatal(errno, "Error syncing parity file '%s'. %s.\n", split->path, strerror(errno));
     738             :                         return -1;
     739             :                         /* LCOV_EXCL_STOP */
     740             :                 }
     741             :         }
     742             : #endif
     743             : 
     744         513 :         return 0;
     745             : }
     746             : 
     747         126 : int parity_truncate(struct snapraid_parity_handle* handle)
     748             : {
     749             :         unsigned s;
     750         126 :         int f_ret = 0;
     751             : 
     752         630 :         for (s = 0; s < handle->split_mac; ++s) {
     753         504 :                 struct snapraid_split_handle* split = &handle->split_map[s];
     754             :                 int ret;
     755             : 
     756             :                 /* truncate any data that we know it's not valid */
     757         504 :                 ret = ftruncate(split->f, split->valid_size);
     758         504 :                 if (ret != 0) {
     759             :                         /* LCOV_EXCL_START */
     760             :                         log_fatal(errno, "Error truncating the parity file '%s' to size %" PRIu64 ". %s.\n", split->path, split->valid_size, strerror(errno));
     761             :                         f_ret = -1;
     762             :                         /* LCOV_EXCL_STOP */
     763             : 
     764             :                         /* continue to truncate the others */
     765             :                 }
     766             :         }
     767             : 
     768         126 :         return f_ret;
     769             : }
     770             : 
     771        1087 : int parity_close(struct snapraid_parity_handle* handle)
     772             : {
     773             :         unsigned s;
     774        1087 :         int f_ret = 0;
     775             : 
     776        5435 :         for (s = 0; s < handle->split_mac; ++s) {
     777        4348 :                 struct snapraid_split_handle* split = &handle->split_map[s];
     778             :                 int ret;
     779             : 
     780        4348 :                 ret = close(split->f);
     781        4348 :                 if (ret != 0) {
     782             :                         /* LCOV_EXCL_START */
     783             :                         /* This is a serious error, as it may be the result of a failed write */
     784             :                         /* identified at later time. */
     785             :                         /* In a normal file-system (not NFS) it should never happen */
     786             :                         log_fatal(errno, "Error closing parity file '%s'. %s.\n", split->path, strerror(errno));
     787             :                         f_ret = -1;
     788             :                         /* LCOV_EXCL_STOP */
     789             : 
     790             :                         /* continue to close the others */
     791             :                 }
     792             : 
     793             :                 /* reset the descriptor */
     794        4348 :                 split->f = -1;
     795             :         }
     796             : 
     797        1087 :         return f_ret;
     798             : }
     799             : 
     800     4274283 : struct snapraid_split_handle* parity_split_find(struct snapraid_parity_handle* handle, data_off_t* offset)
     801             : {
     802             :         unsigned s;
     803             : 
     804     4274283 :         if (*offset < 0)
     805           0 :                 return 0;
     806             : 
     807     6757945 :         for (s = 0; s < handle->split_mac; ++s) {
     808     6746585 :                 struct snapraid_split_handle* split = &handle->split_map[s];
     809             : 
     810     6746585 :                 if (*offset < split->size)
     811     4262923 :                         return split;
     812             : 
     813     2483662 :                 *offset -= split->size;
     814             :         }
     815             : 
     816       11360 :         return 0;
     817             : }
     818             : 
     819      648353 : int parity_write(struct snapraid_parity_handle* handle, block_off_t pos, unsigned char* block_buffer, unsigned block_size)
     820             : {
     821             :         ssize_t write_ret;
     822             :         data_off_t offset;
     823             :         struct snapraid_split_handle* split;
     824             :         unsigned count;
     825             :         int ret;
     826             : 
     827      648353 :         offset = pos * (data_off_t)block_size;
     828             : 
     829      648353 :         split = parity_split_find(handle, &offset);
     830      648353 :         if (!split) {
     831             :                 /* LCOV_EXCL_START */
     832             :                 errno = ENXIO;
     833             :                 log_fatal(errno, "Writing parity data outside range at extra offset %" PRIu64 ".\n", offset);
     834             :                 return -1;
     835             :                 /* LCOV_EXCL_STOP */
     836             :         }
     837             : 
     838             :         /* update the valid range */
     839      648353 :         if (split->valid_size < offset + block_size)
     840      186950 :                 split->valid_size = offset + block_size;
     841             : 
     842      648353 :         count = 0;
     843             :         do {
     844      648353 :                 bw_limit(handle->bw, block_size - count);
     845             : 
     846      648353 :                 write_ret = pwrite(split->f, block_buffer + count, block_size - count, offset + count);
     847      648353 :                 if (write_ret == -1) {
     848             :                         /* LCOV_EXCL_START */
     849             :                         if (errno == ENOSPC) {
     850             :                                 log_fatal(errno, "Failed to grow parity file '%s' using write due lack of space.\n", split->path);
     851             :                         } else {
     852             :                                 log_fatal(errno, "Error writing parity file '%s'. %s.\n", split->path, strerror(errno));
     853             :                         }
     854             :                         return -1;
     855             :                         /* LCOV_EXCL_STOP */
     856             :                 }
     857      648353 :                 if (write_ret == 0) {
     858             :                         /* LCOV_EXCL_START */
     859             :                         errno = ENXIO;
     860             :                         log_fatal(errno, "Unexpected 0 write to file '%s'. %s.\n", split->path, strerror(errno));
     861             :                         return -1;
     862             :                         /* LCOV_EXCL_STOP */
     863             :                 }
     864             : 
     865      648353 :                 count += write_ret;
     866      648353 :         } while (count < block_size);
     867             : 
     868      648353 :         ret = advise_write(&split->advise, split->f, offset, block_size);
     869      648353 :         if (ret != 0) {
     870             :                 /* LCOV_EXCL_START */
     871             :                 log_fatal(errno, "Error advising parity file '%s'. %s.\n", split->path, strerror(errno));
     872             :                 return -1;
     873             :                 /* LCOV_EXCL_STOP */
     874             :         }
     875             : 
     876      648353 :         return 0;
     877             : }
     878             : 
     879     3625930 : int parity_read(struct snapraid_parity_handle* handle, block_off_t pos, unsigned char* block_buffer, unsigned block_size, log_ptr* out)
     880             : {
     881             :         ssize_t read_ret;
     882             :         data_off_t offset;
     883             :         unsigned count;
     884             :         struct snapraid_split_handle* split;
     885             :         int ret;
     886             : 
     887     3625930 :         offset = pos * (data_off_t)block_size;
     888             : 
     889     3625930 :         split = parity_split_find(handle, &offset);
     890     3625930 :         if (!split) {
     891             :                 /* LCOV_EXCL_START */
     892             :                 errno = ENXIO;
     893             :                 out(errno, "Reading parity data outside range at extra offset %" PRIu64 ".\n", offset);
     894             :                 return -1;
     895             :                 /* LCOV_EXCL_STOP */
     896             :         }
     897             : 
     898             :         /* if read is completely out of the valid range */
     899     3614570 :         if (offset >= split->valid_size) {
     900             :                 /* LCOV_EXCL_START */
     901             :                 errno = ENXIO;
     902             :                 out(errno, "Reading over the end from parity file '%s' at offset %" PRIu64 " for size %u.\n", split->path, offset, block_size);
     903             :                 return -1;
     904             :                 /* LCOV_EXCL_STOP */
     905             :         }
     906             : 
     907     3529108 :         count = 0;
     908     3529108 :         errno = 0;
     909             :         do {
     910     3529108 :                 bw_limit(handle->bw, block_size - count);
     911             : 
     912     3529108 :                 read_ret = pread(split->f, block_buffer + count, block_size - count, offset + count);
     913     3529108 :                 if (read_ret == -1) {
     914             :                         /* LCOV_EXCL_START */
     915             :                         out(errno, "Error reading parity file '%s' at offset %" PRIu64 " for size %u. %s.\n", split->path, offset + count, block_size - count, strerror(errno));
     916             :                         return -1;
     917             :                         /* LCOV_EXCL_STOP */
     918             :                 }
     919     3529108 :                 if (read_ret == 0) {
     920             :                         /* LCOV_EXCL_START */
     921             :                         if (errno == 0)
     922             :                                 errno = ENXIO;
     923             :                         out(errno, "Unexpected end of parity file '%s' at offset %" PRIu64 ". %s.\n", split->path, offset, strerror(errno));
     924             :                         return -1;
     925             :                         /* LCOV_EXCL_STOP */
     926             :                 }
     927             : 
     928     3529108 :                 count += read_ret;
     929     3529108 :         } while (count < block_size);
     930             : 
     931     3529108 :         ret = advise_read(&split->advise, split->f, offset, block_size);
     932     3529108 :         if (ret != 0) {
     933             :                 /* LCOV_EXCL_START */
     934             :                 out(errno, "Error advising parity file '%s'. %s.\n", split->path, strerror(errno));
     935             :                 return -1;
     936             :                 /* LCOV_EXCL_STOP */
     937             :         }
     938             : 
     939     3529108 :         return block_size;
     940             : }
     941             : 

Generated by: LCOV version 1.0