LCOV - code coverage report
Current view: top level - cmdline - elem.c (source / functions) Hit Total Coverage
Test: lcov.info Lines: 545 581 93.8 %
Date: 2015-11-20 Functions: 62 64 96.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 "elem.h"
      21             : #include "support.h"
      22             : #include "util.h"
      23             : 
      24             : /****************************************************************************/
      25             : /* snapraid */
      26             : 
      27        1434 : struct snapraid_content* content_alloc(const char* path, uint64_t dev)
      28             : {
      29             :         struct snapraid_content* content;
      30             : 
      31        1434 :         content = malloc_nofail(sizeof(struct snapraid_content));
      32        1434 :         pathimport(content->content, sizeof(content->content), path);
      33        1434 :         content->device = dev;
      34             : 
      35        1434 :         return content;
      36             : }
      37             : 
      38        1297 : void content_free(struct snapraid_content* content)
      39             : {
      40        1297 :         free(content);
      41        1297 : }
      42             : 
      43         498 : struct snapraid_filter* filter_alloc_file(int direction, const char* pattern)
      44             : {
      45             :         struct snapraid_filter* filter;
      46             :         char* i;
      47             :         char* first;
      48             :         char* last;
      49             :         int token_is_valid;
      50             :         int token_is_filled;
      51             : 
      52         498 :         filter = malloc_nofail(sizeof(struct snapraid_filter));
      53         498 :         pathimport(filter->pattern, sizeof(filter->pattern), pattern);
      54         498 :         filter->direction = direction;
      55             : 
      56             :         /* find first and last slash */
      57         498 :         first = 0;
      58         498 :         last = 0;
      59             :         /* reject invalid tokens, like "<empty>", ".", ".." and more dots */
      60         498 :         token_is_valid = 0;
      61         498 :         token_is_filled = 0;
      62        6172 :         for (i = filter->pattern; *i; ++i) {
      63        5675 :                 if (*i == '/') {
      64             :                         /* reject invalid tokens, but accept an empty one as first */
      65           5 :                         if (!token_is_valid && (first != 0 || token_is_filled)) {
      66           1 :                                 free(filter);
      67           1 :                                 return 0;
      68             :                         }
      69           4 :                         token_is_valid = 0;
      70           4 :                         token_is_filled = 0;
      71             : 
      72             :                         /* update slash position */
      73           4 :                         if (!first)
      74           3 :                                 first = i;
      75           4 :                         last = i;
      76        5670 :                 } else if (*i != '.') {
      77        5176 :                         token_is_valid = 1;
      78        5176 :                         token_is_filled = 1;
      79             :                 } else {
      80         494 :                         token_is_filled = 1;
      81             :                 }
      82             :         }
      83             : 
      84             :         /* reject invalid tokens, but accept an empty one as last, but not if it's the only one */
      85         497 :         if (!token_is_valid && (first == 0 || token_is_filled)) {
      86           1 :                 free(filter);
      87           1 :                 return 0;
      88             :         }
      89             : 
      90             :         /* it's a file filter */
      91         496 :         filter->is_disk = 0;
      92             : 
      93         496 :         if (first == 0) {
      94             :                 /* no slash */
      95         493 :                 filter->is_path = 0;
      96         493 :                 filter->is_dir = 0;
      97           3 :         } else if (first == last && last[1] == 0) {
      98             :                 /* one slash at the end */
      99           1 :                 filter->is_path = 0;
     100           1 :                 filter->is_dir = 1;
     101           1 :                 last[0] = 0;
     102             :         } else {
     103             :                 /* at least a slash not at the end */
     104           2 :                 filter->is_path = 1;
     105           2 :                 if (last[1] == 0) {
     106           0 :                         filter->is_dir = 1;
     107           0 :                         last[0] = 0;
     108             :                 } else {
     109           2 :                         filter->is_dir = 0;
     110             :                 }
     111             : 
     112             :                 /* a slash must be the first char, as we don't support PATH/FILE and PATH/DIR/ */
     113           2 :                 if (filter->pattern[0] != '/') {
     114           1 :                         free(filter);
     115           1 :                         return 0;
     116             :                 }
     117             :         }
     118             : 
     119         495 :         return filter;
     120             : }
     121             : 
     122           8 : struct snapraid_filter* filter_alloc_disk(int direction, const char* pattern)
     123             : {
     124             :         struct snapraid_filter* filter;
     125             : 
     126           8 :         filter = malloc_nofail(sizeof(struct snapraid_filter));
     127           8 :         pathimport(filter->pattern, sizeof(filter->pattern), pattern);
     128           8 :         filter->direction = direction;
     129             : 
     130             :         /* it's a disk filter */
     131           8 :         filter->is_disk = 1;
     132           8 :         filter->is_path = 0;
     133           8 :         filter->is_dir = 0;
     134             : 
     135             :         /* no slash allowed in disk names */
     136           8 :         if (strchr(filter->pattern, '/') != 0) {
     137             :                 /* LCOV_EXCL_START */
     138             :                 free(filter);
     139             :                 return 0;
     140             :                 /* LCOV_EXCL_STOP */
     141             :         }
     142             : 
     143           8 :         return filter;
     144             : }
     145             : 
     146         461 : void filter_free(struct snapraid_filter* filter)
     147             : {
     148         461 :         free(filter);
     149         461 : }
     150             : 
     151      125437 : const char* filter_type(struct snapraid_filter* filter, char* out, size_t out_size)
     152             : {
     153             :         const char* direction;
     154      125437 :         if (filter->direction < 0)
     155      125191 :                 direction = "exclude";
     156             :         else
     157         246 :                 direction = "include";
     158             : 
     159      125437 :         if (filter->is_disk)
     160           0 :                 pathprint(out, out_size, "%s %s:", direction, filter->pattern);
     161      125437 :         else if (filter->is_dir)
     162           0 :                 pathprint(out, out_size, "%s %s/", direction, filter->pattern);
     163             :         else
     164      125437 :                 pathprint(out, out_size, "%s %s", direction, filter->pattern);
     165             : 
     166      125437 :         return out;
     167             : }
     168             : 
     169    12035450 : static int filter_apply(struct snapraid_filter* filter, struct snapraid_filter** reason, const char* path, const char* name, int is_dir)
     170             : {
     171    12035450 :         int ret = 0;
     172             : 
     173             :         /* match dirs with dirs and files with files */
     174    12035450 :         if (filter->is_dir && !is_dir)
     175        2939 :                 return 0;
     176    12032511 :         if (!filter->is_dir && is_dir)
     177     6014680 :                 return 0;
     178             : 
     179     6017831 :         if (filter->is_path) {
     180             :                 /* skip initial slash, as always missing from the path */
     181        2939 :                 if (fnmatch(filter->pattern + 1, path, FNM_PATHNAME | FNM_CASEINSENSITIVE_FOR_WIN) == 0)
     182           0 :                         ret = filter->direction;
     183             :         } else {
     184     6014892 :                 if (fnmatch(filter->pattern, name, FNM_CASEINSENSITIVE_FOR_WIN) == 0)
     185      127935 :                         ret = filter->direction;
     186             :         }
     187             : 
     188     6017831 :         if (reason != 0 && ret < 0)
     189      124945 :                 *reason = filter;
     190             : 
     191     6017831 :         return ret;
     192             : }
     193             : 
     194     6023667 : static int filter_recurse(struct snapraid_filter* filter, struct snapraid_filter** reason, const char* const_path, int is_dir)
     195             : {
     196             :         char path[PATH_MAX];
     197             :         char* name;
     198             :         unsigned i;
     199             : 
     200     6023667 :         pathcpy(path, sizeof(path), const_path);
     201             : 
     202             :         /* filter for all the directories */
     203     6023667 :         name = path;
     204    86942366 :         for (i = 0; path[i] != 0; ++i) {
     205    80921689 :                 if (path[i] == '/') {
     206             :                         /* set a terminator */
     207     6014773 :                         path[i] = 0;
     208             : 
     209             :                         /* filter the directory */
     210     6014773 :                         if (filter_apply(filter, reason, path, name, 1) != 0)
     211        2990 :                                 return filter->direction;
     212             : 
     213             :                         /* restore the slash */
     214     6011783 :                         path[i] = '/';
     215             : 
     216             :                         /* next name */
     217     6011783 :                         name = path + i + 1;
     218             :                 }
     219             :         }
     220             : 
     221             :         /* filter the final file */
     222     6020677 :         if (filter_apply(filter, reason, path, name, is_dir) != 0)
     223      124945 :                 return filter->direction;
     224             : 
     225     5895732 :         return 0;
     226             : }
     227             : 
     228     3226232 : static int filter_element(tommy_list* filterlist, struct snapraid_filter** reason, const char* disk, const char* sub, int is_dir)
     229             : {
     230             :         tommy_node* i;
     231             : 
     232     3226232 :         int direction = 1; /* by default include all */
     233             : 
     234             :         /* for each filter */
     235     9223179 :         for (i = tommy_list_head(filterlist); i != 0; i = i->next) {
     236             :                 int ret;
     237     6133552 :                 struct snapraid_filter* filter = i->data;
     238             : 
     239     6133552 :                 if (filter->is_disk) {
     240      109885 :                         if (fnmatch(filter->pattern, disk, FNM_CASEINSENSITIVE_FOR_WIN) == 0)
     241        8670 :                                 ret = filter->direction;
     242             :                         else
     243      101215 :                                 ret = 0;
     244      109885 :                         if (reason != 0 && ret < 0)
     245           0 :                                 *reason = filter;
     246             :                 } else {
     247     6023667 :                         ret = filter_recurse(filter, reason, sub, is_dir);
     248             :                 }
     249             : 
     250     6133552 :                 if (ret > 0) {
     251             :                         /* include the file */
     252       11660 :                         return 0;
     253     6121892 :                 } else if (ret < 0) {
     254             :                         /* exclude the file */
     255      124945 :                         return -1;
     256             :                 } else {
     257             :                         /* default is opposite of the last filter */
     258     5996947 :                         direction = -filter->direction;
     259     5996947 :                         if (reason != 0 && direction < 0)
     260     3004435 :                                 *reason = filter;
     261             :                         /* continue with the next one */
     262             :                 }
     263             :         }
     264             : 
     265             :         /* directories are always included by default, otherwise we cannot apply rules */
     266             :         /* to the contained files */
     267     3089627 :         if (is_dir)
     268        2971 :                 return 0;
     269             : 
     270             :         /* files are excluded/included depending of the last rule processed */
     271     3086656 :         if (direction < 0)
     272       57545 :                 return -1;
     273             : 
     274     3029111 :         return 0;
     275             : }
     276             : 
     277     3223260 : int filter_path(tommy_list* filterlist, struct snapraid_filter** reason, const char* disk, const char* sub)
     278             : {
     279     3223260 :         return filter_element(filterlist, reason, disk, sub, 0);
     280             : }
     281             : 
     282        2972 : int filter_dir(tommy_list* filterlist, struct snapraid_filter** reason, const char* disk, const char* sub)
     283             : {
     284        2972 :         return filter_element(filterlist, reason, disk, sub, 1);
     285             : }
     286             : 
     287       80654 : int filter_existence(int filter_missing, const char* dir, const char* sub)
     288             : {
     289             :         char path[PATH_MAX];
     290             :         struct stat st;
     291             : 
     292       80654 :         if (!filter_missing)
     293       34958 :                 return 0;
     294             : 
     295             :         /* we directly check if in the disk the file is present or not */
     296       45696 :         pathprint(path, sizeof(path), "%s%s", dir, sub);
     297             : 
     298       45696 :         if (lstat(path, &st) != 0) {
     299             :                 /* if the file doesn't exist, we don't filter it out */
     300        5007 :                 if (errno == ENOENT)
     301        5007 :                         return 0;
     302             :                 /* LCOV_EXCL_START */
     303             :                 log_fatal("Error in stat file '%s'. %s.\n", path, strerror(errno));
     304             :                 exit(EXIT_FAILURE);
     305             :                 /* LCOV_EXCL_STOP */
     306             :         }
     307             : 
     308             :         /* the file is present, so we filter it out */
     309       40689 :         return 1;
     310             : }
     311             : 
     312       38806 : int filter_correctness(int filter_error, tommy_arrayblkof* infoarr, struct snapraid_disk* disk, struct snapraid_file* file)
     313             : {
     314             :         unsigned i;
     315             : 
     316       38806 :         if (!filter_error)
     317       16278 :                 return 0;
     318             : 
     319             :         /* check each block of the file */
     320       60231 :         for (i = 0; i < file->blockmax; ++i) {
     321       46123 :                 block_off_t parity_pos = fs_file2par_get(disk, file, i);
     322       46123 :                 snapraid_info info = info_get(infoarr, parity_pos);
     323             : 
     324             :                 /* if the file has a bad block, don't exclude it */
     325       46123 :                 if (info_get_bad(info))
     326        8420 :                         return 0;
     327             :         }
     328             : 
     329             :         /* the file is correct, so we filter it out */
     330       14108 :         return 1;
     331             : }
     332             : 
     333     3066852 : int filter_content(tommy_list* contentlist, const char* path)
     334             : {
     335             :         tommy_node* i;
     336             : 
     337    39681406 :         for (i = tommy_list_head(contentlist); i != 0; i = i->next) {
     338    16773851 :                 struct snapraid_content* content = i->data;
     339             :                 char tmp[PATH_MAX];
     340             : 
     341    16773851 :                 if (pathcmp(content->content, path) == 0)
     342           0 :                         return -1;
     343             : 
     344             :                 /* exclude also the ".tmp" copy used to save it */
     345    16773851 :                 pathprint(tmp, sizeof(tmp), "%s.tmp", content->content);
     346    16773851 :                 if (pathcmp(tmp, path) == 0)
     347           0 :                         return -1;
     348             : 
     349             :                 /* exclude also the ".lock" file */
     350    16773851 :                 pathprint(tmp, sizeof(tmp), "%s.lock", content->content);
     351    16773851 :                 if (pathcmp(tmp, path) == 0)
     352           0 :                         return -1;
     353             :         }
     354             : 
     355     3066852 :         return 0;
     356             : }
     357             : 
     358     3565141 : struct snapraid_file* file_alloc(unsigned block_size, const char* sub, data_off_t size, uint64_t mtime_sec, int mtime_nsec, uint64_t inode, uint64_t physical)
     359             : {
     360             :         struct snapraid_file* file;
     361             :         block_off_t i;
     362             : 
     363     3565141 :         file = malloc_nofail(sizeof(struct snapraid_file));
     364     3565141 :         file->sub = strdup_nofail(sub);
     365     3565141 :         file->size = size;
     366     3565141 :         file->blockmax = (size + block_size - 1) / block_size;
     367     3565141 :         file->mtime_sec = mtime_sec;
     368     3565141 :         file->mtime_nsec = mtime_nsec;
     369     3565141 :         file->inode = inode;
     370     3565141 :         file->physical = physical;
     371     3565141 :         file->flag = 0;
     372     3565141 :         file->blockvec = malloc_nofail(file->blockmax * sizeof(struct snapraid_block));
     373             : 
     374    12361356 :         for (i = 0; i < file->blockmax; ++i) {
     375     8796215 :                 block_state_set(&file->blockvec[i], BLOCK_STATE_CHG);
     376     8796215 :                 hash_invalid_set(file->blockvec[i].hash);
     377             :         }
     378             : 
     379     3565141 :         return file;
     380             : }
     381             : 
     382       21785 : struct snapraid_file* file_dup(struct snapraid_file* copy)
     383             : {
     384             :         struct snapraid_file* file;
     385             :         block_off_t i;
     386             : 
     387       21785 :         file = malloc_nofail(sizeof(struct snapraid_file));
     388       21785 :         file->sub = strdup_nofail(copy->sub);
     389       21785 :         file->size = copy->size;
     390       21785 :         file->blockmax = copy->blockmax;
     391       21785 :         file->mtime_sec = copy->mtime_sec;
     392       21785 :         file->mtime_nsec = copy->mtime_nsec;
     393       21785 :         file->inode = copy->inode;
     394       21785 :         file->physical = copy->physical;
     395       21785 :         file->flag = copy->flag;
     396       21785 :         file->blockvec = malloc_nofail(file->blockmax * sizeof(struct snapraid_block));
     397             : 
     398       75420 :         for (i = 0; i < file->blockmax; ++i) {
     399       53635 :                 file->blockvec[i].state = copy->blockvec[i].state;
     400       53635 :                 memcpy(file->blockvec[i].hash, copy->blockvec[i].hash, HASH_SIZE);
     401             :         }
     402             : 
     403       21785 :         return file;
     404             : }
     405             : 
     406     3417248 : void file_free(struct snapraid_file* file)
     407             : {
     408     3417248 :         free(file->sub);
     409     3417248 :         file->sub = 0;
     410     3417248 :         free(file->blockvec);
     411     3417248 :         file->blockvec = 0;
     412     3417248 :         free(file);
     413     3417248 : }
     414             : 
     415         155 : void file_rename(struct snapraid_file* file, const char* sub)
     416             : {
     417         155 :         free(file->sub);
     418         155 :         file->sub = strdup_nofail(sub);
     419         155 : }
     420             : 
     421        6209 : void file_copy(struct snapraid_file* src_file, struct snapraid_file* dst_file)
     422             : {
     423             :         block_off_t i;
     424             : 
     425        6209 :         if (src_file->size != dst_file->size) {
     426             :                 /* LCOV_EXCL_START */
     427             :                 log_fatal("Internal inconsistency in copy file with different size\n");
     428             :                 exit(EXIT_FAILURE);
     429             :                 /* LCOV_EXCL_STOP */
     430             :         }
     431             : 
     432        6209 :         if (src_file->mtime_sec != dst_file->mtime_sec) {
     433             :                 /* LCOV_EXCL_START */
     434             :                 log_fatal("Internal inconsistency in copy file with different mtime_sec\n");
     435             :                 exit(EXIT_FAILURE);
     436             :                 /* LCOV_EXCL_STOP */
     437             :         }
     438             : 
     439        6209 :         if (src_file->mtime_nsec != dst_file->mtime_nsec) {
     440             :                 /* LCOV_EXCL_START */
     441             :                 log_fatal("Internal inconsistency in copy file with different mtime_nsec\n");
     442             :                 exit(EXIT_FAILURE);
     443             :                 /* LCOV_EXCL_STOP */
     444             :         }
     445             : 
     446       17910 :         for (i = 0; i < dst_file->blockmax; ++i) {
     447             :                 /* set a block with hash computed but without parity */
     448       11701 :                 block_state_set(&dst_file->blockvec[i], BLOCK_STATE_REP);
     449             : 
     450             :                 /* copy the hash */
     451       11701 :                 memcpy(dst_file->blockvec[i].hash, src_file->blockvec[i].hash, HASH_SIZE);
     452             :         }
     453             : 
     454        6209 :         file_flag_set(dst_file, FILE_IS_COPY);
     455        6209 : }
     456             : 
     457       47908 : const char* file_name(const struct snapraid_file* file)
     458             : {
     459       47908 :         const char* r = strrchr(file->sub, '/');
     460             : 
     461       47908 :         if (!r)
     462           8 :                 r = file->sub;
     463             :         else
     464       47900 :                 ++r;
     465       47908 :         return r;
     466             : }
     467             : 
     468     6610122 : unsigned file_block_size(struct snapraid_file* file, block_off_t file_pos, unsigned block_size)
     469             : {
     470             :         /* if it's the last block */
     471     6610122 :         if (file_pos + 1 == file->blockmax) {
     472             :                 unsigned block_remainder;
     473     2692717 :                 if (file->size == 0)
     474           0 :                         return 0;
     475     2692717 :                 block_remainder = file->size % block_size;
     476     2692717 :                 if (block_remainder == 0)
     477        1544 :                         block_remainder = block_size;
     478     2692717 :                 return block_remainder;
     479             :         }
     480             : 
     481     3917405 :         return block_size;
     482             : }
     483             : 
     484     5319910 : int file_block_is_last(struct snapraid_file* file, block_off_t file_pos)
     485             : {
     486     5319910 :         if (file_pos == 0 && file->blockmax == 0)
     487           0 :                 return 1;
     488             : 
     489     5319910 :         if (file_pos >= file->blockmax) {
     490             :                 /* LCOV_EXCL_START */
     491             :                 log_fatal("Internal inconsistency in file block position\n");
     492             :                 exit(EXIT_FAILURE);
     493             :                 /* LCOV_EXCL_STOP */
     494             :         }
     495             : 
     496     5319910 :         return file_pos == file->blockmax - 1;
     497             : }
     498             : 
     499      866983 : int file_inode_compare_to_arg(const void* void_arg, const void* void_data)
     500             : {
     501      866983 :         const uint64_t* arg = void_arg;
     502      866983 :         const struct snapraid_file* file = void_data;
     503             : 
     504      866983 :         if (*arg < file->inode)
     505           0 :                 return -1;
     506      866983 :         if (*arg > file->inode)
     507           0 :                 return 1;
     508      866983 :         return 0;
     509             : }
     510             : 
     511           0 : int file_inode_compare(const void* void_a, const void* void_b)
     512             : {
     513           0 :         const struct snapraid_file* file_a = void_a;
     514           0 :         const struct snapraid_file* file_b = void_b;
     515             : 
     516           0 :         if (file_a->inode < file_b->inode)
     517           0 :                 return -1;
     518           0 :         if (file_a->inode > file_b->inode)
     519           0 :                 return 1;
     520           0 :         return 0;
     521             : }
     522             : 
     523      126356 : int file_path_compare(const void* void_a, const void* void_b)
     524             : {
     525      126356 :         const struct snapraid_file* file_a = void_a;
     526      126356 :         const struct snapraid_file* file_b = void_b;
     527             : 
     528      126356 :         return strcmp(file_a->sub, file_b->sub);
     529             : }
     530             : 
     531       34618 : int file_physical_compare(const void* void_a, const void* void_b)
     532             : {
     533       34618 :         const struct snapraid_file* file_a = void_a;
     534       34618 :         const struct snapraid_file* file_b = void_b;
     535             : 
     536       34618 :         if (file_a->physical < file_b->physical)
     537       26412 :                 return -1;
     538        8206 :         if (file_a->physical > file_b->physical)
     539        8204 :                 return 1;
     540           2 :         return 0;
     541             : }
     542             : 
     543      105104 : int file_path_compare_to_arg(const void* void_arg, const void* void_data)
     544             : {
     545      105104 :         const char* arg = void_arg;
     546      105104 :         const struct snapraid_file* file = void_data;
     547             : 
     548      105104 :         return strcmp(arg, file->sub);
     549             : }
     550             : 
     551       23954 : int file_name_compare(const void* void_a, const void* void_b)
     552             : {
     553       23954 :         const struct snapraid_file* file_a = void_a;
     554       23954 :         const struct snapraid_file* file_b = void_b;
     555       23954 :         const char* name_a = file_name(file_a);
     556       23954 :         const char* name_b = file_name(file_b);
     557             : 
     558       23954 :         return strcmp(name_a, name_b);
     559             : }
     560             : 
     561       23659 : int file_stamp_compare(const void* void_a, const void* void_b)
     562             : {
     563       23659 :         const struct snapraid_file* file_a = void_a;
     564       23659 :         const struct snapraid_file* file_b = void_b;
     565             : 
     566       23659 :         if (file_a->size < file_b->size)
     567           0 :                 return -1;
     568       23659 :         if (file_a->size > file_b->size)
     569           0 :                 return 1;
     570             : 
     571       23659 :         if (file_a->mtime_sec < file_b->mtime_sec)
     572           0 :                 return -1;
     573       23659 :         if (file_a->mtime_sec > file_b->mtime_sec)
     574           0 :                 return 1;
     575             : 
     576       23659 :         if (file_a->mtime_nsec < file_b->mtime_nsec)
     577           0 :                 return -1;
     578       23659 :         if (file_a->mtime_nsec > file_b->mtime_nsec)
     579           0 :                 return 1;
     580             : 
     581       23659 :         return 0;
     582             : }
     583             : 
     584       23954 : int file_namestamp_compare(const void* void_a, const void* void_b)
     585             : {
     586             :         int ret;
     587             : 
     588       23954 :         ret = file_name_compare(void_a, void_b);
     589       23954 :         if (ret != 0)
     590         295 :                 return ret;
     591             : 
     592       23659 :         return file_stamp_compare(void_a, void_b);
     593             : }
     594             : 
     595           0 : int file_pathstamp_compare(const void* void_a, const void* void_b)
     596             : {
     597             :         int ret;
     598             : 
     599           0 :         ret = file_path_compare(void_a, void_b);
     600           0 :         if (ret != 0)
     601           0 :                 return ret;
     602             : 
     603           0 :         return file_stamp_compare(void_a, void_b);
     604             : }
     605             : 
     606     3607761 : struct snapraid_chunk* chunk_alloc(block_off_t parity_pos, struct snapraid_file* file, block_off_t file_pos, block_off_t count)
     607             : {
     608             :         struct snapraid_chunk* chunk;
     609             : 
     610     3607761 :         if (count == 0) {
     611             :                 /* LCOV_EXCL_START */
     612             :                 log_fatal("Internal inconsistency when allocating empty chunk for file '%s' at position '%u/%u'\n", file->sub, file_pos, file->blockmax);
     613             :                 exit(EXIT_FAILURE);
     614             :                 /* LCOV_EXCL_STOP */
     615             :         }
     616     3607761 :         if (file_pos + count > file->blockmax) {
     617             :                 /* LCOV_EXCL_START */
     618             :                 log_fatal("Internal inconsistency when allocating overflowing chunk for file '%s' at position '%u:%u/%u'\n", file->sub, file_pos, count, file->blockmax);
     619             :                 exit(EXIT_FAILURE);
     620             :                 /* LCOV_EXCL_STOP */
     621             :         }
     622             : 
     623     3607761 :         chunk = malloc_nofail(sizeof(struct snapraid_chunk));
     624     3607761 :         chunk->parity_pos = parity_pos;
     625     3607761 :         chunk->file = file;
     626     3607761 :         chunk->file_pos = file_pos;
     627     3607761 :         chunk->count = count;
     628             : 
     629     3607761 :         return chunk;
     630             : }
     631             : 
     632     3437520 : void chunk_free(struct snapraid_chunk* chunk)
     633             : {
     634     3437520 :         free(chunk);
     635     3437520 : }
     636             : 
     637    43029600 : int chunk_parity_compare(const void* void_a, const void* void_b)
     638             : {
     639    43029600 :         const struct snapraid_chunk* arg_a = void_a;
     640    43029600 :         const struct snapraid_chunk* arg_b = void_b;
     641             : 
     642    43029600 :         if (arg_a->parity_pos < arg_b->parity_pos)
     643      932908 :                 return -1;
     644    42096692 :         if (arg_a->parity_pos > arg_b->parity_pos)
     645    42057855 :                 return 1;
     646             : 
     647       38837 :         return 0;
     648             : }
     649             : 
     650    43527418 : int chunk_file_compare(const void* void_a, const void* void_b)
     651             : {
     652    43527418 :         const struct snapraid_chunk* arg_a = void_a;
     653    43527418 :         const struct snapraid_chunk* arg_b = void_b;
     654             : 
     655    43527418 :         if (arg_a->file < arg_b->file)
     656      856585 :                 return -1;
     657    42670833 :         if (arg_a->file > arg_b->file)
     658    42601978 :                 return 1;
     659             : 
     660       68855 :         if (arg_a->file_pos < arg_b->file_pos)
     661         192 :                 return -1;
     662       68663 :         if (arg_a->file_pos > arg_b->file_pos)
     663       29826 :                 return 1;
     664             : 
     665       38837 :         return 0;
     666             : }
     667             : 
     668      115419 : struct snapraid_link* link_alloc(const char* sub, const char* linkto, unsigned link_flag)
     669             : {
     670             :         struct snapraid_link* slink;
     671             : 
     672      115419 :         slink = malloc_nofail(sizeof(struct snapraid_link));
     673      115419 :         slink->sub = strdup_nofail(sub);
     674      115419 :         slink->linkto = strdup_nofail(linkto);
     675      115419 :         slink->flag = link_flag;
     676             : 
     677      115419 :         return slink;
     678             : }
     679             : 
     680      109621 : void link_free(struct snapraid_link* slink)
     681             : {
     682      109621 :         free(slink->sub);
     683      109621 :         free(slink->linkto);
     684      109621 :         free(slink);
     685      109621 : }
     686             : 
     687       30380 : int link_name_compare_to_arg(const void* void_arg, const void* void_data)
     688             : {
     689       30380 :         const char* arg = void_arg;
     690       30380 :         const struct snapraid_link* slink = void_data;
     691             : 
     692       30380 :         return strcmp(arg, slink->sub);
     693             : }
     694             : 
     695        1249 : int link_alpha_compare(const void* void_a, const void* void_b)
     696             : {
     697        1249 :         const struct snapraid_link* slink_a = void_a;
     698        1249 :         const struct snapraid_link* slink_b = void_b;
     699             : 
     700        1249 :         return strcmp(slink_a->sub, slink_b->sub);
     701             : }
     702             : 
     703         760 : struct snapraid_dir* dir_alloc(const char* sub)
     704             : {
     705             :         struct snapraid_dir* dir;
     706             : 
     707         760 :         dir = malloc_nofail(sizeof(struct snapraid_dir));
     708         760 :         dir->sub = strdup_nofail(sub);
     709         760 :         dir->flag = 0;
     710             : 
     711         760 :         return dir;
     712             : }
     713             : 
     714         712 : void dir_free(struct snapraid_dir* dir)
     715             : {
     716         712 :         free(dir->sub);
     717         712 :         free(dir);
     718         712 : }
     719             : 
     720         217 : int dir_name_compare(const void* void_arg, const void* void_data)
     721             : {
     722         217 :         const char* arg = void_arg;
     723         217 :         const struct snapraid_dir* dir = void_data;
     724             : 
     725         217 :         return strcmp(arg, dir->sub);
     726             : }
     727             : 
     728        1477 : struct snapraid_disk* disk_alloc(const char* name, const char* dir, uint64_t dev, int skip)
     729             : {
     730             :         struct snapraid_disk* disk;
     731             : 
     732        1477 :         disk = malloc_nofail(sizeof(struct snapraid_disk));
     733        1477 :         pathcpy(disk->name, sizeof(disk->name), name);
     734        1477 :         pathimport(disk->dir, sizeof(disk->dir), dir);
     735             : 
     736             :         /* ensure that the dir terminate with "/" if it isn't empty */
     737        1477 :         pathslash(disk->dir, sizeof(disk->dir));
     738             : 
     739        1477 :         disk->smartctl[0] = 0;
     740        1477 :         disk->device = dev;
     741        1477 :         disk->tick = 0;
     742        1477 :         disk->total_blocks = 0;
     743        1477 :         disk->free_blocks = 0;
     744        1477 :         disk->first_free_block = 0;
     745        1477 :         disk->has_volatile_inodes = 0;
     746        1477 :         disk->has_unreliable_physical = 0;
     747        1477 :         disk->has_different_uuid = 0;
     748        1477 :         disk->has_unsupported_uuid = 0;
     749        1477 :         disk->had_empty_uuid = 0;
     750        1477 :         disk->mapping_idx = -1;
     751        1477 :         disk->skip_access = skip;
     752        1477 :         tommy_list_init(&disk->filelist);
     753        1477 :         tommy_list_init(&disk->deletedlist);
     754        1477 :         tommy_hashdyn_init(&disk->inodeset);
     755        1477 :         tommy_hashdyn_init(&disk->pathset);
     756        1477 :         tommy_hashdyn_init(&disk->stampset);
     757        1477 :         tommy_list_init(&disk->linklist);
     758        1477 :         tommy_hashdyn_init(&disk->linkset);
     759        1477 :         tommy_list_init(&disk->dirlist);
     760        1477 :         tommy_hashdyn_init(&disk->dirset);
     761        1477 :         tommy_tree_init(&disk->fs_parity, chunk_parity_compare);
     762        1477 :         tommy_tree_init(&disk->fs_file, chunk_file_compare);
     763        1477 :         disk->fs_last = 0;
     764             : 
     765        1477 :         return disk;
     766             : }
     767             : 
     768        1351 : void disk_free(struct snapraid_disk* disk)
     769             : {
     770        1351 :         tommy_list_foreach(&disk->filelist, (tommy_foreach_func*)file_free);
     771        1351 :         tommy_list_foreach(&disk->deletedlist, (tommy_foreach_func*)file_free);
     772        1351 :         tommy_tree_foreach(&disk->fs_file, (tommy_foreach_func*)chunk_free);
     773        1351 :         tommy_hashdyn_done(&disk->inodeset);
     774        1351 :         tommy_hashdyn_done(&disk->pathset);
     775        1351 :         tommy_hashdyn_done(&disk->stampset);
     776        1351 :         tommy_list_foreach(&disk->linklist, (tommy_foreach_func*)link_free);
     777        1351 :         tommy_hashdyn_done(&disk->linkset);
     778        1351 :         tommy_list_foreach(&disk->dirlist, (tommy_foreach_func*)dir_free);
     779        1351 :         tommy_hashdyn_done(&disk->dirset);
     780        1351 :         free(disk);
     781        1351 : }
     782             : 
     783             : struct chunk_disk_empty {
     784             :         block_off_t blockmax;
     785             : };
     786             : 
     787             : /**
     788             :  * Compare the chunk if inside the specified blockmax.
     789             :  */
     790          10 : static int chunk_disk_empty_compare(const void* void_a, const void* void_b)
     791             : {
     792          10 :         const struct chunk_disk_empty* arg_a = void_a;
     793          10 :         const struct snapraid_chunk* arg_b = void_b;
     794             : 
     795             :         /* if the block is inside the specified blockmax, it's found */
     796          10 :         if (arg_a->blockmax > arg_b->parity_pos)
     797           5 :                 return 0;
     798             : 
     799             :         /* otherwise search for a smaller one */
     800           5 :         return -1;
     801             : }
     802             : 
     803         804 : int disk_is_empty(struct snapraid_disk* disk, block_off_t blockmax)
     804             : {
     805         804 :         struct chunk_disk_empty arg = { blockmax };
     806             : 
     807             :         /* if there is an element, it's not empty */
     808             :         /* even if links and dirs have no block allocation */
     809         804 :         if (!tommy_list_empty(&disk->filelist))
     810         736 :                 return 0;
     811          68 :         if (!tommy_list_empty(&disk->linklist))
     812           0 :                 return 0;
     813          68 :         if (!tommy_list_empty(&disk->dirlist))
     814           0 :                 return 0;
     815             : 
     816             :         /* search for any chunk inside blockmax */
     817          68 :         if (tommy_tree_search_compare(&disk->fs_parity, chunk_disk_empty_compare, &arg) != 0)
     818           5 :                 return 0;
     819             : 
     820             :         /* finally, it's empty */
     821          63 :         return 1;
     822             : }
     823             : 
     824             : struct chunk_disk_size {
     825             :         block_off_t size;
     826             : };
     827             : 
     828             : /**
     829             :  * Compare the chunk by highest parity position.
     830             :  *
     831             :  * The maximum parity position is stored as size.
     832             :  */
     833       47779 : static int chunk_disk_size_compare(const void* void_a, const void* void_b)
     834             : {
     835       47779 :         struct chunk_disk_size* arg_a = (void*)void_a;
     836       47779 :         const struct snapraid_chunk* arg_b = void_b;
     837             : 
     838             :         /* get the maximum size */
     839       47779 :         if (arg_a->size < arg_b->parity_pos + arg_b->count)
     840       47779 :                 arg_a->size = arg_b->parity_pos + arg_b->count;
     841             : 
     842             :         /* search always for a bigger one */
     843       47779 :         return 1;
     844             : }
     845             : 
     846        3955 : block_off_t disk_size(struct snapraid_disk* disk)
     847             : {
     848        3955 :         struct chunk_disk_size arg = { 0 };
     849        3955 :         tommy_tree_search_compare(&disk->fs_parity, chunk_disk_size_compare, &arg);
     850        3955 :         return arg.size;
     851             : }
     852             : 
     853             : struct chunk_check {
     854             :         const struct snapraid_chunk* prev;
     855             :         int result;
     856             : };
     857             : 
     858     6291380 : void chunk_parity_check_foreach(void* void_arg, void* void_obj)
     859             : {
     860     6291380 :         struct chunk_check* arg = void_arg;
     861     6291380 :         const struct snapraid_chunk* obj = void_obj;
     862     6291380 :         const struct snapraid_chunk* prev = arg->prev;
     863             : 
     864             :         /* set the next previous block */
     865     6291380 :         arg->prev = obj;
     866             : 
     867             :         /* stop reporting if too many errors */
     868     6291380 :         if (arg->result > 100) {
     869             :                 /* LCOV_EXCL_START */
     870             :                 return;
     871             :                 /* LCOV_EXCL_STOP */
     872             :         }
     873             : 
     874     6291380 :         if (obj->count == 0) {
     875             :                 /* LCOV_EXCL_START */
     876             :                 log_fatal("Internal inconsistency in parity count zero for file '%s' at '%u'\n",
     877             :                         obj->file->sub, obj->parity_pos);
     878             :                 ++arg->result;
     879             :                 return;
     880             :                 /* LCOV_EXCL_STOP */
     881             :         }
     882             : 
     883             :         /* check only if there is a previous block */
     884     6291380 :         if (!prev)
     885        2473 :                 return;
     886             : 
     887             :         /* check the order */
     888     6288907 :         if (prev->parity_pos >= obj->parity_pos) {
     889             :                 /* LCOV_EXCL_START */
     890             :                 log_fatal("Internal inconsistency in parity order for files '%s' at '%u:%u' and '%s' at '%u:%u'\n",
     891             :                         prev->file->sub, prev->parity_pos, prev->count, obj->file->sub, obj->parity_pos, obj->count);
     892             :                 ++arg->result;
     893             :                 return;
     894             :                 /* LCOV_EXCL_STOP */
     895             :         }
     896             : 
     897             :         /* check that the chunks don't overlap */
     898     6288907 :         if (prev->parity_pos + prev->count > obj->parity_pos) {
     899             :                 /* LCOV_EXCL_START */
     900             :                 log_fatal("Internal inconsistency for parity overlap for files '%s' at '%u:%u' and '%s' at '%u:%u'\n",
     901             :                         prev->file->sub, prev->parity_pos, prev->count, obj->file->sub, obj->parity_pos, obj->count);
     902             :                 ++arg->result;
     903             :                 return;
     904             :                 /* LCOV_EXCL_STOP */
     905             :         }
     906             : }
     907             : 
     908     6291380 : void chunk_file_check_foreach(void* void_arg, void* void_obj)
     909             : {
     910     6291380 :         struct chunk_check* arg = void_arg;
     911     6291380 :         const struct snapraid_chunk* obj = void_obj;
     912     6291380 :         const struct snapraid_chunk* prev = arg->prev;
     913             : 
     914             :         /* set the next previous block */
     915     6291380 :         arg->prev = obj;
     916             : 
     917             :         /* stop reporting if too many errors */
     918     6291380 :         if (arg->result > 100) {
     919             :                 /* LCOV_EXCL_START */
     920             :                 return;
     921             :                 /* LCOV_EXCL_STOP */
     922             :         }
     923             : 
     924     6291380 :         if (obj->count == 0) {
     925             :                 /* LCOV_EXCL_START */
     926             :                 log_fatal("Internal inconsistency in file count zero for file '%s' at '%u'\n",
     927             :                         obj->file->sub, obj->file_pos);
     928             :                 ++arg->result;
     929             :                 return;
     930             :                 /* LCOV_EXCL_STOP */
     931             :         }
     932             : 
     933             :         /* note that for deleted files, some chunks may be missing */
     934             : 
     935             :         /* if the files are different */
     936     6291380 :         if (!prev || prev->file != obj->file) {
     937     6247390 :                 if (prev != 0) {
     938     6244917 :                         if (file_flag_has(prev->file, FILE_IS_DELETED)) {
     939             :                                 /* check that the chunk doesn't overflow the file */
     940       59701 :                                 if (prev->file_pos + prev->count > prev->file->blockmax) {
     941             :                                         /* LCOV_EXCL_START */
     942             :                                         log_fatal("Internal inconsistency in delete end for file '%s' at '%u:%u' overflowing size '%u'\n",
     943             :                                                 prev->file->sub, prev->file_pos, prev->count, prev->file->blockmax);
     944             :                                         ++arg->result;
     945             :                                         return;
     946             :                                         /* LCOV_EXCL_STOP */
     947             :                                 }
     948             :                         } else {
     949             :                                 /* check that the chunk ends the file */
     950     6185216 :                                 if (prev->file_pos + prev->count != prev->file->blockmax) {
     951             :                                         /* LCOV_EXCL_START */
     952             :                                         log_fatal("Internal inconsistency in file end for file '%s' at '%u:%u' instead of size '%u'\n",
     953             :                                                 prev->file->sub, prev->file_pos, prev->count, prev->file->blockmax);
     954             :                                         ++arg->result;
     955             :                                         return;
     956             :                                         /* LCOV_EXCL_STOP */
     957             :                                 }
     958             :                         }
     959             :                 }
     960             : 
     961    12494780 :                 if (file_flag_has(obj->file, FILE_IS_DELETED)) {
     962             :                         /* check that the chunk doesn't overflow the file */
     963       59776 :                         if (obj->file_pos + obj->count > obj->file->blockmax) {
     964             :                                 /* LCOV_EXCL_START */
     965             :                                 log_fatal("Internal inconsistency in delete start for file '%s' at '%u:%u' overflowing size '%u'\n",
     966             :                                         obj->file->sub, obj->file_pos, obj->count, obj->file->blockmax);
     967             :                                 ++arg->result;
     968             :                                 return;
     969             :                                 /* LCOV_EXCL_STOP */
     970             :                         }
     971             :                 } else {
     972             :                         /* check that the chunk starts the file */
     973     6187614 :                         if (obj->file_pos != 0) {
     974             :                                 /* LCOV_EXCL_START */
     975             :                                 log_fatal("Internal inconsistency in file start for file '%s' at '%u:%u'\n",
     976             :                                         obj->file->sub, obj->file_pos, obj->count);
     977             :                                 ++arg->result;
     978             :                                 return;
     979             :                                 /* LCOV_EXCL_STOP */
     980             :                         }
     981             :                 }
     982             :         } else {
     983             :                 /* check the order */
     984       43990 :                 if (prev->file_pos >= obj->file_pos) {
     985             :                         /* LCOV_EXCL_START */
     986             :                         log_fatal("Internal inconsistency in file order for file '%s' at '%u:%u' and at '%u:%u'\n",
     987             :                                 prev->file->sub, prev->file_pos, prev->count, obj->file_pos, obj->count);
     988             :                         ++arg->result;
     989             :                         return;
     990             :                         /* LCOV_EXCL_STOP */
     991             :                 }
     992             : 
     993       43990 :                 if (file_flag_has(obj->file, FILE_IS_DELETED)) {
     994             :                         /* check that the chunks don't overlap */
     995          27 :                         if (prev->file_pos + prev->count > obj->file_pos) {
     996             :                                 /* LCOV_EXCL_START */
     997             :                                 log_fatal("Internal inconsistency in delete sequence for file '%s' at '%u:%u' and at '%u:%u'\n",
     998             :                                         prev->file->sub, prev->file_pos, prev->count, obj->file_pos, obj->count);
     999             :                                 ++arg->result;
    1000             :                                 return;
    1001             :                                 /* LCOV_EXCL_STOP */
    1002             :                         }
    1003             :                 } else {
    1004             :                         /* check that the chunks are sequential */
    1005       43963 :                         if (prev->file_pos + prev->count != obj->file_pos) {
    1006             :                                 /* LCOV_EXCL_START */
    1007             :                                 log_fatal("Internal inconsistency in file sequence for file '%s' at '%u:%u' and at '%u:%u'\n",
    1008             :                                         prev->file->sub, prev->file_pos, prev->count, obj->file_pos, obj->count);
    1009             :                                 ++arg->result;
    1010             :                                 return;
    1011             :                                 /* LCOV_EXCL_STOP */
    1012             :                         }
    1013             :                 }
    1014             :         }
    1015             : }
    1016             : 
    1017        2676 : int fs_check(struct snapraid_disk* disk)
    1018             : {
    1019             :         struct chunk_check arg;
    1020             : 
    1021             :         /* error count starts from 0 */
    1022        2676 :         arg.result = 0;
    1023             : 
    1024             :         /* check parity sequence */
    1025        2676 :         arg.prev = 0;
    1026        2676 :         tommy_tree_foreach_arg(&disk->fs_parity, chunk_parity_check_foreach, &arg);
    1027             : 
    1028             :         /* check file sequence */
    1029        2676 :         arg.prev = 0;
    1030        2676 :         tommy_tree_foreach_arg(&disk->fs_file, chunk_file_check_foreach, &arg);
    1031             : 
    1032        2676 :         if (arg.result != 0)
    1033           0 :                 return -1;
    1034             : 
    1035        2676 :         return 0;
    1036             : }
    1037             : 
    1038             : struct chunk_parity_inside {
    1039             :         block_off_t parity_pos;
    1040             : };
    1041             : 
    1042             : /**
    1043             :  * Compare the chunk if containing the specified parity position.
    1044             :  */
    1045   211700412 : static int chunk_parity_inside_compare(const void* void_a, const void* void_b)
    1046             : {
    1047   211700412 :         const struct chunk_parity_inside* arg_a = void_a;
    1048   211700412 :         const struct snapraid_chunk* arg_b = void_b;
    1049             : 
    1050   211700412 :         if (arg_a->parity_pos < arg_b->parity_pos)
    1051    78963776 :                 return -1;
    1052   132736636 :         if (arg_a->parity_pos >= arg_b->parity_pos + arg_b->count)
    1053   115362811 :                 return 1;
    1054             : 
    1055    17373825 :         return 0;
    1056             : }
    1057             : 
    1058             : /**
    1059             :  * Seach the chunk at the specified parity position.
    1060             :  * The search is optimized for sequential accesses.
    1061             :  * \return If not found return 0
    1062             :  */
    1063    62562730 : static struct snapraid_chunk* fs_par2chunk_get_ts(struct snapraid_disk* disk, struct snapraid_chunk** fs_last, block_off_t parity_pos)
    1064             : {
    1065             :         struct snapraid_chunk* chunk;
    1066             : 
    1067             :         /* check if the last accessed chunk matches */
    1068    62562730 :         if (*fs_last
    1069    62828537 :                 && parity_pos >= (*fs_last)->parity_pos
    1070    62678201 :                 && parity_pos < (*fs_last)->parity_pos + (*fs_last)->count
    1071             :         ) {
    1072    43113171 :                 chunk = *fs_last;
    1073             :         } else {
    1074    19449559 :                 struct chunk_parity_inside arg = { parity_pos };
    1075    19449559 :                 chunk = tommy_tree_search_compare(&disk->fs_parity, chunk_parity_inside_compare, &arg);
    1076             :         }
    1077             : 
    1078    62800800 :         if (!chunk)
    1079     2351363 :                 return 0;
    1080             : 
    1081             :         /* store the last accessed chunk */
    1082    60449437 :         *fs_last = chunk;
    1083             : 
    1084    60449437 :         return chunk;
    1085             : }
    1086             : 
    1087             : struct chunk_file_inside {
    1088             :         struct snapraid_file* file;
    1089             :         block_off_t file_pos;
    1090             : };
    1091             : 
    1092             : /**
    1093             :  * Compare the chunk if containing the specified file position.
    1094             :  */
    1095   123424429 : static int chunk_file_inside_compare(const void* void_a, const void* void_b)
    1096             : {
    1097   123424429 :         const struct chunk_file_inside* arg_a = void_a;
    1098   123424429 :         const struct snapraid_chunk* arg_b = void_b;
    1099             : 
    1100   123424429 :         if (arg_a->file < arg_b->file)
    1101    52876931 :                 return -1;
    1102    70547498 :         if (arg_a->file > arg_b->file)
    1103    58630297 :                 return 1;
    1104             : 
    1105    11917201 :         if (arg_a->file_pos < arg_b->file_pos)
    1106       49120 :                 return -1;
    1107    11868081 :         if (arg_a->file_pos >= arg_b->file_pos + arg_b->count)
    1108       44006 :                 return 1;
    1109             : 
    1110    11824075 :         return 0;
    1111             : }
    1112             : 
    1113             : /**
    1114             :  * Seach the chunk at the specified file position.
    1115             :  * The search is optimized for sequential accesses.
    1116             :  * \return If not found return 0
    1117             :  */
    1118    34157182 : static struct snapraid_chunk* fs_file2chunk_get_ts(struct snapraid_disk* disk, struct snapraid_chunk** fs_last, struct snapraid_file* file, block_off_t file_pos)
    1119             : {
    1120             :         struct snapraid_chunk* chunk;
    1121             : 
    1122             :         /* check if the last accessed chunk matches */
    1123    34157182 :         if (*fs_last
    1124    34107260 :                 && file == (*fs_last)->file
    1125    22496891 :                 && file_pos >= (*fs_last)->file_pos
    1126    22481340 :                 && file_pos < (*fs_last)->file_pos + (*fs_last)->count
    1127             :         ) {
    1128    22376150 :                 chunk = *fs_last;
    1129             :         } else {
    1130    11781032 :                 struct chunk_file_inside arg = { file, file_pos };
    1131    11781032 :                 chunk = tommy_tree_search_compare(&disk->fs_file, chunk_file_inside_compare, &arg);
    1132             :         }
    1133             : 
    1134    34132437 :         if (!chunk)
    1135           0 :                 return 0;
    1136             : 
    1137             :         /* store the last accessed chunk */
    1138    34132437 :         *fs_last = chunk;
    1139             : 
    1140    34132437 :         return chunk;
    1141             : }
    1142             : 
    1143    62671366 : struct snapraid_file* fs_par2file_get_ts(struct snapraid_disk* disk, struct snapraid_chunk** fs_last, block_off_t parity_pos, block_off_t* file_pos)
    1144             : {
    1145             :         struct snapraid_chunk* chunk;
    1146             : 
    1147    62671366 :         chunk = fs_par2chunk_get_ts(disk, fs_last, parity_pos);
    1148    62750330 :         if (!chunk)
    1149     2351221 :                 return 0;
    1150             : 
    1151    60399109 :         if (file_pos)
    1152    59839728 :                 *file_pos = chunk->file_pos + (parity_pos - chunk->parity_pos);
    1153             : 
    1154    60399109 :         return chunk->file;
    1155             : }
    1156             : 
    1157    28740730 : block_off_t fs_file2par_get_ts(struct snapraid_disk* disk, struct snapraid_chunk** fs_last, struct snapraid_file* file, block_off_t file_pos)
    1158             : {
    1159             :         struct snapraid_chunk* chunk;
    1160             : 
    1161    28740730 :         chunk = fs_file2chunk_get_ts(disk, fs_last, file, file_pos);
    1162    28850478 :         if (!chunk) {
    1163             :                 /* LCOV_EXCL_START */
    1164             :                 log_fatal("Internal inconsistency when resolving file '%s' at position '%u/%u' in disk '%s'\n", file->sub, file_pos, file->blockmax, disk->name);
    1165             :                 exit(EXIT_FAILURE);
    1166             :                 /* LCOV_EXCL_STOP */
    1167             :         }
    1168             : 
    1169    28850478 :         return chunk->parity_pos + (file_pos - chunk->file_pos);
    1170             : }
    1171             : 
    1172     8849850 : void fs_allocate(struct snapraid_disk* disk, block_off_t parity_pos, struct snapraid_file* file, block_off_t file_pos)
    1173             : {
    1174             :         struct snapraid_chunk* chunk;
    1175             :         struct snapraid_chunk* parity_chunk;
    1176             :         struct snapraid_chunk* file_chunk;
    1177             : 
    1178     8849850 :         if (file_pos > 0) {
    1179             :                 /* search an existing chunk for the previous file_pos */
    1180     5267799 :                 chunk = fs_file2chunk_get_ts(disk, &disk->fs_last, file, file_pos - 1);
    1181             : 
    1182     5267799 :                 if (chunk != 0 && parity_pos == chunk->parity_pos + chunk->count) {
    1183             :                         /* ensure that we are extending the chunk at the end */
    1184     5242090 :                         if (file_pos != chunk->file_pos + chunk->count) {
    1185             :                                 /* LCOV_EXCL_START */
    1186             :                                 log_fatal("Internal inconsistency when allocating file '%s' at position '%u/%u' in the middle of chunk '%u:%u' in disk '%s'\n", file->sub, file_pos, file->blockmax, chunk->file_pos, chunk->count, disk->name);
    1187             :                                 exit(EXIT_FAILURE);
    1188             :                                 /* LCOV_EXCL_STOP */
    1189             :                         }
    1190             : 
    1191             :                         /* extend the existing chunk */
    1192     5242090 :                         ++chunk->count;
    1193             : 
    1194    14091940 :                         return;
    1195             :                 }
    1196             :         }
    1197             : 
    1198             :         /* a chunk doesn't exist, and we have to create a new one */
    1199     3607760 :         chunk = chunk_alloc(parity_pos, file, file_pos, 1);
    1200             : 
    1201             :         /* insert the chunk in the trees */
    1202     3607760 :         parity_chunk = tommy_tree_insert(&disk->fs_parity, &chunk->parity_node, chunk);
    1203     3607760 :         file_chunk = tommy_tree_insert(&disk->fs_file, &chunk->file_node, chunk);
    1204             : 
    1205     3607760 :         if (parity_chunk != chunk || file_chunk != chunk) {
    1206             :                 /* LCOV_EXCL_START */
    1207             :                 log_fatal("Internal inconsistency when allocating file '%s' at position '%u/%u' for existing chunk '%u:%u' in disk '%s'\n", file->sub, file_pos, file->blockmax, chunk->file_pos, chunk->count, disk->name);
    1208             :                 exit(EXIT_FAILURE);
    1209             :                 /* LCOV_EXCL_STOP */
    1210             :         }
    1211             : 
    1212             :         /* store the last accessed chunk */
    1213     3607760 :         disk->fs_last = chunk;
    1214             : }
    1215             : 
    1216      104155 : void fs_deallocate(struct snapraid_disk* disk, block_off_t parity_pos)
    1217             : {
    1218             :         struct snapraid_chunk* chunk;
    1219             :         struct snapraid_chunk* second_chunk;
    1220             :         struct snapraid_chunk* parity_chunk;
    1221             :         struct snapraid_chunk* file_chunk;
    1222             :         block_off_t first_count, second_count;
    1223             : 
    1224      104155 :         chunk = fs_par2chunk_get_ts(disk, &disk->fs_last, parity_pos);
    1225      104155 :         if (!chunk) {
    1226             :                 /* LCOV_EXCL_START */
    1227             :                 log_fatal("Internal inconsistency when deallocating parity position '%u' for not existing chunk in disk '%s'\n", parity_pos, disk->name);
    1228             :                 exit(EXIT_FAILURE);
    1229             :                 /* LCOV_EXCL_STOP */
    1230             :         }
    1231             : 
    1232             :         /* if it's the only block of the chunk, delete it */
    1233      104155 :         if (chunk->count == 1) {
    1234             :                 /* remove from the trees */
    1235       38837 :                 tommy_tree_remove(&disk->fs_parity, chunk);
    1236       38837 :                 tommy_tree_remove(&disk->fs_file, chunk);
    1237             : 
    1238             :                 /* deallocate */
    1239       38837 :                 chunk_free(chunk);
    1240             : 
    1241             :                 /* clear the last accessed chunk */
    1242       38837 :                 disk->fs_last = 0;
    1243       38837 :                 return;
    1244             :         }
    1245             : 
    1246             :         /* if it's at the start of the chunk, shrink the chunk */
    1247       65318 :         if (parity_pos == chunk->parity_pos) {
    1248       65316 :                 ++chunk->parity_pos;
    1249       65316 :                 ++chunk->file_pos;
    1250       65316 :                 --chunk->count;
    1251       65316 :                 return;
    1252             :         }
    1253             : 
    1254             :         /* if it's at the end of the chunk, shrink the chunk */
    1255           2 :         if (parity_pos == chunk->parity_pos + chunk->count - 1) {
    1256           1 :                 --chunk->count;
    1257           1 :                 return;
    1258             :         }
    1259             : 
    1260             :         /* otherwise it's in the middle */
    1261           1 :         first_count = parity_pos - chunk->parity_pos;
    1262           1 :         second_count = chunk->count - first_count - 1;
    1263             : 
    1264             :         /* adjust the first chunk */
    1265           1 :         chunk->count = first_count;
    1266             : 
    1267             :         /* allocate the second chunk */
    1268           1 :         second_chunk = chunk_alloc(chunk->parity_pos + first_count + 1, chunk->file, chunk->file_pos + first_count + 1, second_count);
    1269             : 
    1270             :         /* insert the chunk in the trees */
    1271           1 :         parity_chunk = tommy_tree_insert(&disk->fs_parity, &second_chunk->parity_node, second_chunk);
    1272           1 :         file_chunk = tommy_tree_insert(&disk->fs_file, &second_chunk->file_node, second_chunk);
    1273             : 
    1274           1 :         if (parity_chunk != second_chunk || file_chunk != second_chunk) {
    1275             :                 /* LCOV_EXCL_START */
    1276             :                 log_fatal("Internal inconsistency when deallocating parity position '%u' for splitting chunk '%u:%u' in disk '%s'\n", parity_pos, second_chunk->file_pos, second_chunk->count, disk->name);
    1277             :                 exit(EXIT_FAILURE);
    1278             :                 /* LCOV_EXCL_STOP */
    1279             :         }
    1280             : 
    1281             :         /* store the last accessed chunk */
    1282           1 :         disk->fs_last = second_chunk;
    1283             : }
    1284             : 
    1285   112904165 : struct snapraid_block* fs_file2block_get(struct snapraid_file* file, block_off_t file_pos)
    1286             : {
    1287   112904165 :         if (file_pos >= file->blockmax) {
    1288             :                 /* LCOV_EXCL_START */
    1289             :                 log_fatal("Internal inconsistency when dereferencing file '%s' at position '%u/%u'\n", file->sub, file_pos, file->blockmax);
    1290             :                 exit(EXIT_FAILURE);
    1291             :                 /* LCOV_EXCL_STOP */
    1292             :         }
    1293             : 
    1294   112904165 :         return &file->blockvec[file_pos];
    1295             : }
    1296             : 
    1297    51148943 : struct snapraid_block* fs_par2block_get_ts(struct snapraid_disk* disk, struct snapraid_chunk** fs_last, block_off_t parity_pos)
    1298             : {
    1299             :         struct snapraid_file* file;
    1300             :         block_off_t file_pos;
    1301             : 
    1302    51148943 :         file = fs_par2file_get_ts(disk, fs_last, parity_pos, &file_pos);
    1303             : 
    1304    51044271 :         if (!file)
    1305     2337947 :                 return BLOCK_EMPTY;
    1306             : 
    1307    48706324 :         return fs_file2block_get(file, file_pos);
    1308             : }
    1309             : 
    1310        1447 : struct snapraid_map* map_alloc(const char* name, unsigned position, block_off_t total_blocks, block_off_t free_blocks, const char* uuid)
    1311             : {
    1312             :         struct snapraid_map* map;
    1313             : 
    1314        1447 :         map = malloc_nofail(sizeof(struct snapraid_map));
    1315        1447 :         pathcpy(map->name, sizeof(map->name), name);
    1316        1447 :         map->position = position;
    1317        1447 :         map->total_blocks = total_blocks;
    1318        1447 :         map->free_blocks = free_blocks;
    1319        1447 :         pathcpy(map->uuid, sizeof(map->uuid), uuid);
    1320             : 
    1321        1447 :         return map;
    1322             : }
    1323             : 
    1324        1327 : void map_free(struct snapraid_map* map)
    1325             : {
    1326        1327 :         free(map);
    1327        1327 : }
    1328             : 
    1329      743492 : int time_compare(const void* void_a, const void* void_b)
    1330             : {
    1331      743492 :         const time_t* time_a = void_a;
    1332      743492 :         const time_t* time_b = void_b;
    1333             : 
    1334      743492 :         if (*time_a < *time_b)
    1335       28996 :                 return -1;
    1336      714496 :         if (*time_a > *time_b)
    1337      185655 :                 return 1;
    1338      528841 :         return 0;
    1339             : }

Generated by: LCOV version 1.10