LCOV - code coverage report
Current view: top level - cmdline - elem.c (source / functions) Hit Total Coverage
Test: lcov.info Lines: 546 582 93.8 %
Date: 2016-01-09 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        1490 : struct snapraid_content* content_alloc(const char* path, uint64_t dev)
      28             : {
      29             :         struct snapraid_content* content;
      30             : 
      31        1490 :         content = malloc_nofail(sizeof(struct snapraid_content));
      32        1490 :         pathimport(content->content, sizeof(content->content), path);
      33        1490 :         content->device = dev;
      34             : 
      35        1490 :         return content;
      36             : }
      37             : 
      38        1353 : void content_free(struct snapraid_content* content)
      39             : {
      40        1353 :         free(content);
      41        1353 : }
      42             : 
      43         514 : 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         514 :         filter = malloc_nofail(sizeof(struct snapraid_filter));
      53         514 :         pathimport(filter->pattern, sizeof(filter->pattern), pattern);
      54         514 :         filter->direction = direction;
      55             : 
      56             :         /* find first and last slash */
      57         514 :         first = 0;
      58         514 :         last = 0;
      59             :         /* reject invalid tokens, like "<empty>", ".", ".." and more dots */
      60         514 :         token_is_valid = 0;
      61         514 :         token_is_filled = 0;
      62        6372 :         for (i = filter->pattern; *i; ++i) {
      63        5859 :                 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        5854 :                 } else if (*i != '.') {
      77        5344 :                         token_is_valid = 1;
      78        5344 :                         token_is_filled = 1;
      79             :                 } else {
      80         510 :                         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         513 :         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         512 :         filter->is_disk = 0;
      92             : 
      93         512 :         if (first == 0) {
      94             :                 /* no slash */
      95         509 :                 filter->is_path = 0;
      96         509 :                 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         511 :         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         477 : void filter_free(struct snapraid_filter* filter)
     147             : {
     148         477 :         free(filter);
     149         477 : }
     150             : 
     151      125387 : const char* filter_type(struct snapraid_filter* filter, char* out, size_t out_size)
     152             : {
     153             :         const char* direction;
     154      125387 :         if (filter->direction < 0)
     155      125133 :                 direction = "exclude";
     156             :         else
     157         254 :                 direction = "include";
     158             : 
     159      125387 :         if (filter->is_disk)
     160           0 :                 pathprint(out, out_size, "%s %s:", direction, filter->pattern);
     161      125387 :         else if (filter->is_dir)
     162           0 :                 pathprint(out, out_size, "%s %s/", direction, filter->pattern);
     163             :         else
     164      125387 :                 pathprint(out, out_size, "%s %s", direction, filter->pattern);
     165             : 
     166      125387 :         return out;
     167             : }
     168             : 
     169    12222233 : static int filter_apply(struct snapraid_filter* filter, struct snapraid_filter** reason, const char* path, const char* name, int is_dir)
     170             : {
     171    12222233 :         int ret = 0;
     172             : 
     173             :         /* match dirs with dirs and files with files */
     174    12222233 :         if (filter->is_dir && !is_dir)
     175        2946 :                 return 0;
     176    12219287 :         if (!filter->is_dir && is_dir)
     177     6106768 :                 return 0;
     178             : 
     179     6112519 :         if (filter->is_path) {
     180             :                 /* skip initial slash, as always missing from the path */
     181        2946 :                 if (fnmatch(filter->pattern + 1, path, FNM_PATHNAME | FNM_CASEINSENSITIVE_FOR_WIN) == 0)
     182           0 :                         ret = filter->direction;
     183             :         } else {
     184     6109573 :                 if (fnmatch(filter->pattern, name, FNM_CASEINSENSITIVE_FOR_WIN) == 0)
     185      127869 :                         ret = filter->direction;
     186             :         }
     187             : 
     188     6112519 :         if (reason != 0 && ret < 0)
     189      124879 :                 *reason = filter;
     190             : 
     191     6112519 :         return ret;
     192             : }
     193             : 
     194     6118464 : 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     6118464 :         pathcpy(path, sizeof(path), const_path);
     201             : 
     202             :         /* filter for all the directories */
     203     6118464 :         name = path;
     204    88222216 :         for (i = 0; path[i] != 0; ++i) {
     205    82106742 :                 if (path[i] == '/') {
     206             :                         /* set a terminator */
     207     6106759 :                         path[i] = 0;
     208             : 
     209             :                         /* filter the directory */
     210     6106759 :                         if (filter_apply(filter, reason, path, name, 1) != 0)
     211        2990 :                                 return filter->direction;
     212             : 
     213             :                         /* restore the slash */
     214     6103769 :                         path[i] = '/';
     215             : 
     216             :                         /* next name */
     217     6103769 :                         name = path + i + 1;
     218             :                 }
     219             :         }
     220             : 
     221             :         /* filter the final file */
     222     6115474 :         if (filter_apply(filter, reason, path, name, is_dir) != 0)
     223      124879 :                 return filter->direction;
     224             : 
     225     5990595 :         return 0;
     226             : }
     227             : 
     228     3273724 : 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     3273724 :         int direction = 1; /* by default include all */
     233             : 
     234             :         /* for each filter */
     235     9365584 :         for (i = tommy_list_head(filterlist); i != 0; i = i->next) {
     236             :                 int ret;
     237     6228405 :                 struct snapraid_filter* filter = i->data;
     238             : 
     239     6228405 :                 if (filter->is_disk) {
     240      109941 :                         if (fnmatch(filter->pattern, disk, FNM_CASEINSENSITIVE_FOR_WIN) == 0)
     241        8676 :                                 ret = filter->direction;
     242             :                         else
     243      101265 :                                 ret = 0;
     244      109941 :                         if (reason != 0 && ret < 0)
     245           0 :                                 *reason = filter;
     246             :                 } else {
     247     6118464 :                         ret = filter_recurse(filter, reason, sub, is_dir);
     248             :                 }
     249             : 
     250     6228405 :                 if (ret > 0) {
     251             :                         /* include the file */
     252       11666 :                         return 0;
     253     6216739 :                 } else if (ret < 0) {
     254             :                         /* exclude the file */
     255      124879 :                         return -1;
     256             :                 } else {
     257             :                         /* default is opposite of the last filter */
     258     6091860 :                         direction = -filter->direction;
     259     6091860 :                         if (reason != 0 && direction < 0)
     260     3051823 :                                 *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     3137179 :         if (is_dir)
     268        3022 :                 return 0;
     269             : 
     270             :         /* files are excluded/included depending of the last rule processed */
     271     3134157 :         if (direction < 0)
     272       57574 :                 return -1;
     273             : 
     274     3076583 :         return 0;
     275             : }
     276             : 
     277     3270701 : int filter_path(tommy_list* filterlist, struct snapraid_filter** reason, const char* disk, const char* sub)
     278             : {
     279     3270701 :         return filter_element(filterlist, reason, disk, sub, 0);
     280             : }
     281             : 
     282        3023 : int filter_dir(tommy_list* filterlist, struct snapraid_filter** reason, const char* disk, const char* sub)
     283             : {
     284        3023 :         return filter_element(filterlist, reason, disk, sub, 1);
     285             : }
     286             : 
     287       80688 : int filter_existence(int filter_missing, const char* dir, const char* sub)
     288             : {
     289             :         char path[PATH_MAX];
     290             :         struct stat st;
     291             : 
     292       80688 :         if (!filter_missing)
     293       34978 :                 return 0;
     294             : 
     295             :         /* we directly check if in the disk the file is present or not */
     296       45710 :         pathprint(path, sizeof(path), "%s%s", dir, sub);
     297             : 
     298       45710 :         if (lstat(path, &st) != 0) {
     299             :                 /* if the file doesn't exist, we don't filter it out */
     300        4913 :                 if (errno == ENOENT)
     301        4913 :                         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       40797 :         return 1;
     310             : }
     311             : 
     312       38732 : int filter_correctness(int filter_error, tommy_arrayblkof* infoarr, struct snapraid_disk* disk, struct snapraid_file* file)
     313             : {
     314             :         unsigned i;
     315             : 
     316       38732 :         if (!filter_error)
     317       16190 :                 return 0;
     318             : 
     319             :         /* check each block of the file */
     320       60567 :         for (i = 0; i < file->blockmax; ++i) {
     321       46357 :                 block_off_t parity_pos = fs_file2par_get(disk, file, i);
     322       46357 :                 snapraid_info info = info_get(infoarr, parity_pos);
     323             : 
     324             :                 /* if the file has a bad block, don't exclude it */
     325       46357 :                 if (info_get_bad(info))
     326        8332 :                         return 0;
     327             :         }
     328             : 
     329             :         /* the file is correct, so we filter it out */
     330       14210 :         return 1;
     331             : }
     332             : 
     333     3114300 : int filter_content(tommy_list* contentlist, const char* path)
     334             : {
     335             :         tommy_node* i;
     336             : 
     337    40437716 :         for (i = tommy_list_head(contentlist); i != 0; i = i->next) {
     338    17104558 :                 struct snapraid_content* content = i->data;
     339             :                 char tmp[PATH_MAX];
     340             : 
     341    17104558 :                 if (pathcmp(content->content, path) == 0)
     342           0 :                         return -1;
     343             : 
     344             :                 /* exclude also the ".tmp" copy used to save it */
     345    17104558 :                 pathprint(tmp, sizeof(tmp), "%s.tmp", content->content);
     346    17104558 :                 if (pathcmp(tmp, path) == 0)
     347           0 :                         return -1;
     348             : 
     349             :                 /* exclude also the ".lock" file */
     350    17104558 :                 pathprint(tmp, sizeof(tmp), "%s.lock", content->content);
     351    17104558 :                 if (pathcmp(tmp, path) == 0)
     352           0 :                         return -1;
     353             :         }
     354             : 
     355     3114300 :         return 0;
     356             : }
     357             : 
     358     3611495 : 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     3611495 :         file = malloc_nofail(sizeof(struct snapraid_file));
     364     3611495 :         file->sub = strdup_nofail(sub);
     365     3611495 :         file->size = size;
     366     3611495 :         file->blockmax = (size + block_size - 1) / block_size;
     367     3611495 :         file->mtime_sec = mtime_sec;
     368     3611495 :         file->mtime_nsec = mtime_nsec;
     369     3611495 :         file->inode = inode;
     370     3611495 :         file->physical = physical;
     371     3611495 :         file->flag = 0;
     372     3611495 :         file->blockvec = malloc_nofail(file->blockmax * sizeof(struct snapraid_block));
     373             : 
     374    12530604 :         for (i = 0; i < file->blockmax; ++i) {
     375     8919109 :                 block_state_set(&file->blockvec[i], BLOCK_STATE_CHG);
     376     8919109 :                 hash_invalid_set(file->blockvec[i].hash);
     377             :         }
     378             : 
     379     3611495 :         return file;
     380             : }
     381             : 
     382       24707 : struct snapraid_file* file_dup(struct snapraid_file* copy)
     383             : {
     384             :         struct snapraid_file* file;
     385             :         block_off_t i;
     386             : 
     387       24707 :         file = malloc_nofail(sizeof(struct snapraid_file));
     388       24707 :         file->sub = strdup_nofail(copy->sub);
     389       24707 :         file->size = copy->size;
     390       24707 :         file->blockmax = copy->blockmax;
     391       24707 :         file->mtime_sec = copy->mtime_sec;
     392       24707 :         file->mtime_nsec = copy->mtime_nsec;
     393       24707 :         file->inode = copy->inode;
     394       24707 :         file->physical = copy->physical;
     395       24707 :         file->flag = copy->flag;
     396       24707 :         file->blockvec = malloc_nofail(file->blockmax * sizeof(struct snapraid_block));
     397             : 
     398       81385 :         for (i = 0; i < file->blockmax; ++i) {
     399       56678 :                 file->blockvec[i].state = copy->blockvec[i].state;
     400       56678 :                 memcpy(file->blockvec[i].hash, copy->blockvec[i].hash, HASH_SIZE);
     401             :         }
     402             : 
     403       24707 :         return file;
     404             : }
     405             : 
     406     3466242 : void file_free(struct snapraid_file* file)
     407             : {
     408     3466242 :         free(file->sub);
     409     3466242 :         file->sub = 0;
     410     3466242 :         free(file->blockvec);
     411     3466242 :         file->blockvec = 0;
     412     3466242 :         free(file);
     413     3466242 : }
     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       23633 : void file_copy(struct snapraid_file* src_file, struct snapraid_file* dst_file)
     422             : {
     423             :         block_off_t i;
     424             : 
     425       23633 :         if (src_file->size != dst_file->size) {
     426             :                 /* LCOV_EXCL_START */
     427             :                 log_fatal("Internal inconsistency in copy file with different size\n");
     428             :                 os_abort();
     429             :                 /* LCOV_EXCL_STOP */
     430             :         }
     431             : 
     432       23633 :         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             :                 os_abort();
     436             :                 /* LCOV_EXCL_STOP */
     437             :         }
     438             : 
     439       23633 :         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             :                 os_abort();
     443             :                 /* LCOV_EXCL_STOP */
     444             :         }
     445             : 
     446       81852 :         for (i = 0; i < dst_file->blockmax; ++i) {
     447             :                 /* set a block with hash computed but without parity */
     448       58219 :                 block_state_set(&dst_file->blockvec[i], BLOCK_STATE_REP);
     449             : 
     450             :                 /* copy the hash */
     451       58219 :                 memcpy(dst_file->blockvec[i].hash, src_file->blockvec[i].hash, HASH_SIZE);
     452             :         }
     453             : 
     454       23633 :         file_flag_set(dst_file, FILE_IS_COPY);
     455       23633 : }
     456             : 
     457       47994 : const char* file_name(const struct snapraid_file* file)
     458             : {
     459       47994 :         const char* r = strrchr(file->sub, '/');
     460             : 
     461       47994 :         if (!r)
     462          60 :                 r = file->sub;
     463             :         else
     464       47934 :                 ++r;
     465       47994 :         return r;
     466             : }
     467             : 
     468     6677441 : 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     6677441 :         if (file_pos + 1 == file->blockmax) {
     472             :                 unsigned block_remainder;
     473     2717238 :                 if (file->size == 0)
     474           0 :                         return 0;
     475     2717238 :                 block_remainder = file->size % block_size;
     476     2717238 :                 if (block_remainder == 0)
     477        2543 :                         block_remainder = block_size;
     478     2717238 :                 return block_remainder;
     479             :         }
     480             : 
     481     3960203 :         return block_size;
     482             : }
     483             : 
     484     5326414 : int file_block_is_last(struct snapraid_file* file, block_off_t file_pos)
     485             : {
     486     5326414 :         if (file_pos == 0 && file->blockmax == 0)
     487           0 :                 return 1;
     488             : 
     489     5326414 :         if (file_pos >= file->blockmax) {
     490             :                 /* LCOV_EXCL_START */
     491             :                 log_fatal("Internal inconsistency in file block position\n");
     492             :                 os_abort();
     493             :                 /* LCOV_EXCL_STOP */
     494             :         }
     495             : 
     496     5326414 :         return file_pos == file->blockmax - 1;
     497             : }
     498             : 
     499      927047 : int file_inode_compare_to_arg(const void* void_arg, const void* void_data)
     500             : {
     501      927047 :         const uint64_t* arg = void_arg;
     502      927047 :         const struct snapraid_file* file = void_data;
     503             : 
     504      927047 :         if (*arg < file->inode)
     505           0 :                 return -1;
     506      927047 :         if (*arg > file->inode)
     507           0 :                 return 1;
     508      927047 :         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      129121 : int file_path_compare(const void* void_a, const void* void_b)
     524             : {
     525      129121 :         const struct snapraid_file* file_a = void_a;
     526      129121 :         const struct snapraid_file* file_b = void_b;
     527             : 
     528      129121 :         return strcmp(file_a->sub, file_b->sub);
     529             : }
     530             : 
     531       49676 : int file_physical_compare(const void* void_a, const void* void_b)
     532             : {
     533       49676 :         const struct snapraid_file* file_a = void_a;
     534       49676 :         const struct snapraid_file* file_b = void_b;
     535             : 
     536       49676 :         if (file_a->physical < file_b->physical)
     537       34678 :                 return -1;
     538       14998 :         if (file_a->physical > file_b->physical)
     539       14996 :                 return 1;
     540           2 :         return 0;
     541             : }
     542             : 
     543      104908 : int file_path_compare_to_arg(const void* void_arg, const void* void_data)
     544             : {
     545      104908 :         const char* arg = void_arg;
     546      104908 :         const struct snapraid_file* file = void_data;
     547             : 
     548      104908 :         return strcmp(arg, file->sub);
     549             : }
     550             : 
     551       23997 : int file_name_compare(const void* void_a, const void* void_b)
     552             : {
     553       23997 :         const struct snapraid_file* file_a = void_a;
     554       23997 :         const struct snapraid_file* file_b = void_b;
     555       23997 :         const char* name_a = file_name(file_a);
     556       23997 :         const char* name_b = file_name(file_b);
     557             : 
     558       23997 :         return strcmp(name_a, name_b);
     559             : }
     560             : 
     561       23661 : int file_stamp_compare(const void* void_a, const void* void_b)
     562             : {
     563       23661 :         const struct snapraid_file* file_a = void_a;
     564       23661 :         const struct snapraid_file* file_b = void_b;
     565             : 
     566       23661 :         if (file_a->size < file_b->size)
     567           0 :                 return -1;
     568       23661 :         if (file_a->size > file_b->size)
     569           0 :                 return 1;
     570             : 
     571       23661 :         if (file_a->mtime_sec < file_b->mtime_sec)
     572           0 :                 return -1;
     573       23661 :         if (file_a->mtime_sec > file_b->mtime_sec)
     574           0 :                 return 1;
     575             : 
     576       23661 :         if (file_a->mtime_nsec < file_b->mtime_nsec)
     577           0 :                 return -1;
     578       23661 :         if (file_a->mtime_nsec > file_b->mtime_nsec)
     579           0 :                 return 1;
     580             : 
     581       23661 :         return 0;
     582             : }
     583             : 
     584       23997 : int file_namestamp_compare(const void* void_a, const void* void_b)
     585             : {
     586             :         int ret;
     587             : 
     588       23997 :         ret = file_name_compare(void_a, void_b);
     589       23997 :         if (ret != 0)
     590         336 :                 return ret;
     591             : 
     592       23661 :         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     3657427 : 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     3657427 :         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             :                 os_abort();
     614             :                 /* LCOV_EXCL_STOP */
     615             :         }
     616     3657427 :         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             :                 os_abort();
     620             :                 /* LCOV_EXCL_STOP */
     621             :         }
     622             : 
     623     3657427 :         chunk = malloc_nofail(sizeof(struct snapraid_chunk));
     624     3657427 :         chunk->parity_pos = parity_pos;
     625     3657427 :         chunk->file = file;
     626     3657427 :         chunk->file_pos = file_pos;
     627     3657427 :         chunk->count = count;
     628             : 
     629     3657427 :         return chunk;
     630             : }
     631             : 
     632     3486903 : void chunk_free(struct snapraid_chunk* chunk)
     633             : {
     634     3486903 :         free(chunk);
     635     3486903 : }
     636             : 
     637    43351652 : int chunk_parity_compare(const void* void_a, const void* void_b)
     638             : {
     639    43351652 :         const struct snapraid_chunk* arg_a = void_a;
     640    43351652 :         const struct snapraid_chunk* arg_b = void_b;
     641             : 
     642    43351652 :         if (arg_a->parity_pos < arg_b->parity_pos)
     643     2220745 :                 return -1;
     644    41130907 :         if (arg_a->parity_pos > arg_b->parity_pos)
     645    41089282 :                 return 1;
     646             : 
     647       41625 :         return 0;
     648             : }
     649             : 
     650    44153325 : int chunk_file_compare(const void* void_a, const void* void_b)
     651             : {
     652    44153325 :         const struct snapraid_chunk* arg_a = void_a;
     653    44153325 :         const struct snapraid_chunk* arg_b = void_b;
     654             : 
     655    44153325 :         if (arg_a->file < arg_b->file)
     656      875219 :                 return -1;
     657    43278106 :         if (arg_a->file > arg_b->file)
     658    43205895 :                 return 1;
     659             : 
     660       72211 :         if (arg_a->file_pos < arg_b->file_pos)
     661         194 :                 return -1;
     662       72017 :         if (arg_a->file_pos > arg_b->file_pos)
     663       30392 :                 return 1;
     664             : 
     665       41625 :         return 0;
     666             : }
     667             : 
     668      116876 : struct snapraid_link* link_alloc(const char* sub, const char* linkto, unsigned link_flag)
     669             : {
     670             :         struct snapraid_link* slink;
     671             : 
     672      116876 :         slink = malloc_nofail(sizeof(struct snapraid_link));
     673      116876 :         slink->sub = strdup_nofail(sub);
     674      116876 :         slink->linkto = strdup_nofail(linkto);
     675      116876 :         slink->flag = link_flag;
     676             : 
     677      116876 :         return slink;
     678             : }
     679             : 
     680      111078 : void link_free(struct snapraid_link* slink)
     681             : {
     682      111078 :         free(slink->sub);
     683      111078 :         free(slink->linkto);
     684      111078 :         free(slink);
     685      111078 : }
     686             : 
     687       31837 : int link_name_compare_to_arg(const void* void_arg, const void* void_data)
     688             : {
     689       31837 :         const char* arg = void_arg;
     690       31837 :         const struct snapraid_link* slink = void_data;
     691             : 
     692       31837 :         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         772 : struct snapraid_dir* dir_alloc(const char* sub)
     704             : {
     705             :         struct snapraid_dir* dir;
     706             : 
     707         772 :         dir = malloc_nofail(sizeof(struct snapraid_dir));
     708         772 :         dir->sub = strdup_nofail(sub);
     709         772 :         dir->flag = 0;
     710             : 
     711         772 :         return dir;
     712             : }
     713             : 
     714         724 : void dir_free(struct snapraid_dir* dir)
     715             : {
     716         724 :         free(dir->sub);
     717         724 :         free(dir);
     718         724 : }
     719             : 
     720         229 : int dir_name_compare(const void* void_arg, const void* void_data)
     721             : {
     722         229 :         const char* arg = void_arg;
     723         229 :         const struct snapraid_dir* dir = void_data;
     724             : 
     725         229 :         return strcmp(arg, dir->sub);
     726             : }
     727             : 
     728        1525 : struct snapraid_disk* disk_alloc(const char* name, const char* dir, uint64_t dev, const char* uuid, int skip)
     729             : {
     730             :         struct snapraid_disk* disk;
     731             : 
     732        1525 :         disk = malloc_nofail(sizeof(struct snapraid_disk));
     733        1525 :         pathcpy(disk->name, sizeof(disk->name), name);
     734        1525 :         pathimport(disk->dir, sizeof(disk->dir), dir);
     735        1525 :         pathcpy(disk->uuid, sizeof(disk->uuid), uuid);
     736             : 
     737             :         /* ensure that the dir terminate with "/" if it isn't empty */
     738        1525 :         pathslash(disk->dir, sizeof(disk->dir));
     739             : 
     740        1525 :         disk->smartctl[0] = 0;
     741        1525 :         disk->device = dev;
     742        1525 :         disk->tick = 0;
     743        1525 :         disk->total_blocks = 0;
     744        1525 :         disk->free_blocks = 0;
     745        1525 :         disk->first_free_block = 0;
     746        1525 :         disk->has_volatile_inodes = 0;
     747        1525 :         disk->has_unreliable_physical = 0;
     748        1525 :         disk->has_different_uuid = 0;
     749        1525 :         disk->has_unsupported_uuid = *uuid == 0; /* empty UUID means unsupported */
     750        1525 :         disk->had_empty_uuid = 0;
     751        1525 :         disk->mapping_idx = -1;
     752        1525 :         disk->skip_access = skip;
     753        1525 :         tommy_list_init(&disk->filelist);
     754        1525 :         tommy_list_init(&disk->deletedlist);
     755        1525 :         tommy_hashdyn_init(&disk->inodeset);
     756        1525 :         tommy_hashdyn_init(&disk->pathset);
     757        1525 :         tommy_hashdyn_init(&disk->stampset);
     758        1525 :         tommy_list_init(&disk->linklist);
     759        1525 :         tommy_hashdyn_init(&disk->linkset);
     760        1525 :         tommy_list_init(&disk->dirlist);
     761        1525 :         tommy_hashdyn_init(&disk->dirset);
     762        1525 :         tommy_tree_init(&disk->fs_parity, chunk_parity_compare);
     763        1525 :         tommy_tree_init(&disk->fs_file, chunk_file_compare);
     764        1525 :         disk->fs_last = 0;
     765             : 
     766        1525 :         return disk;
     767             : }
     768             : 
     769        1399 : void disk_free(struct snapraid_disk* disk)
     770             : {
     771        1399 :         tommy_list_foreach(&disk->filelist, (tommy_foreach_func*)file_free);
     772        1399 :         tommy_list_foreach(&disk->deletedlist, (tommy_foreach_func*)file_free);
     773        1399 :         tommy_tree_foreach(&disk->fs_file, (tommy_foreach_func*)chunk_free);
     774        1399 :         tommy_hashdyn_done(&disk->inodeset);
     775        1399 :         tommy_hashdyn_done(&disk->pathset);
     776        1399 :         tommy_hashdyn_done(&disk->stampset);
     777        1399 :         tommy_list_foreach(&disk->linklist, (tommy_foreach_func*)link_free);
     778        1399 :         tommy_hashdyn_done(&disk->linkset);
     779        1399 :         tommy_list_foreach(&disk->dirlist, (tommy_foreach_func*)dir_free);
     780        1399 :         tommy_hashdyn_done(&disk->dirset);
     781        1399 :         free(disk);
     782        1399 : }
     783             : 
     784             : struct chunk_disk_empty {
     785             :         block_off_t blockmax;
     786             : };
     787             : 
     788             : /**
     789             :  * Compare the chunk if inside the specified blockmax.
     790             :  */
     791          13 : static int chunk_disk_empty_compare(const void* void_a, const void* void_b)
     792             : {
     793          13 :         const struct chunk_disk_empty* arg_a = void_a;
     794          13 :         const struct snapraid_chunk* arg_b = void_b;
     795             : 
     796             :         /* if the block is inside the specified blockmax, it's found */
     797          13 :         if (arg_a->blockmax > arg_b->parity_pos)
     798           8 :                 return 0;
     799             : 
     800             :         /* otherwise search for a smaller one */
     801           5 :         return -1;
     802             : }
     803             : 
     804         870 : int disk_is_empty(struct snapraid_disk* disk, block_off_t blockmax)
     805             : {
     806         870 :         struct chunk_disk_empty arg = { blockmax };
     807             : 
     808             :         /* if there is an element, it's not empty */
     809             :         /* even if links and dirs have no block allocation */
     810         870 :         if (!tommy_list_empty(&disk->filelist))
     811         778 :                 return 0;
     812          92 :         if (!tommy_list_empty(&disk->linklist))
     813           0 :                 return 0;
     814          92 :         if (!tommy_list_empty(&disk->dirlist))
     815           0 :                 return 0;
     816             : 
     817             :         /* search for any chunk inside blockmax */
     818          92 :         if (tommy_tree_search_compare(&disk->fs_parity, chunk_disk_empty_compare, &arg) != 0)
     819           8 :                 return 0;
     820             : 
     821             :         /* finally, it's empty */
     822          84 :         return 1;
     823             : }
     824             : 
     825             : struct chunk_disk_size {
     826             :         block_off_t size;
     827             : };
     828             : 
     829             : /**
     830             :  * Compare the chunk by highest parity position.
     831             :  *
     832             :  * The maximum parity position is stored as size.
     833             :  */
     834       48702 : static int chunk_disk_size_compare(const void* void_a, const void* void_b)
     835             : {
     836       48702 :         struct chunk_disk_size* arg_a = (void*)void_a;
     837       48702 :         const struct snapraid_chunk* arg_b = void_b;
     838             : 
     839             :         /* get the maximum size */
     840       48702 :         if (arg_a->size < arg_b->parity_pos + arg_b->count)
     841       48702 :                 arg_a->size = arg_b->parity_pos + arg_b->count;
     842             : 
     843             :         /* search always for a bigger one */
     844       48702 :         return 1;
     845             : }
     846             : 
     847        4165 : block_off_t disk_size(struct snapraid_disk* disk)
     848             : {
     849        4165 :         struct chunk_disk_size arg = { 0 };
     850        4165 :         tommy_tree_search_compare(&disk->fs_parity, chunk_disk_size_compare, &arg);
     851        4165 :         return arg.size;
     852             : }
     853             : 
     854             : struct chunk_check {
     855             :         const struct snapraid_chunk* prev;
     856             :         int result;
     857             : };
     858             : 
     859     6441048 : void chunk_parity_check_foreach(void* void_arg, void* void_obj)
     860             : {
     861     6441048 :         struct chunk_check* arg = void_arg;
     862     6441048 :         const struct snapraid_chunk* obj = void_obj;
     863     6441048 :         const struct snapraid_chunk* prev = arg->prev;
     864             : 
     865             :         /* set the next previous block */
     866     6441048 :         arg->prev = obj;
     867             : 
     868             :         /* stop reporting if too many errors */
     869     6441048 :         if (arg->result > 100) {
     870             :                 /* LCOV_EXCL_START */
     871             :                 return;
     872             :                 /* LCOV_EXCL_STOP */
     873             :         }
     874             : 
     875     6441048 :         if (obj->count == 0) {
     876             :                 /* LCOV_EXCL_START */
     877             :                 log_fatal("Internal inconsistency in parity count zero for file '%s' at '%u'\n",
     878             :                         obj->file->sub, obj->parity_pos);
     879             :                 ++arg->result;
     880             :                 return;
     881             :                 /* LCOV_EXCL_STOP */
     882             :         }
     883             : 
     884             :         /* check only if there is a previous block */
     885     6441048 :         if (!prev)
     886        2588 :                 return;
     887             : 
     888             :         /* check the order */
     889     6438460 :         if (prev->parity_pos >= obj->parity_pos) {
     890             :                 /* LCOV_EXCL_START */
     891             :                 log_fatal("Internal inconsistency in parity order for files '%s' at '%u:%u' and '%s' at '%u:%u'\n",
     892             :                         prev->file->sub, prev->parity_pos, prev->count, obj->file->sub, obj->parity_pos, obj->count);
     893             :                 ++arg->result;
     894             :                 return;
     895             :                 /* LCOV_EXCL_STOP */
     896             :         }
     897             : 
     898             :         /* check that the chunks don't overlap */
     899     6438460 :         if (prev->parity_pos + prev->count > obj->parity_pos) {
     900             :                 /* LCOV_EXCL_START */
     901             :                 log_fatal("Internal inconsistency for parity overlap for files '%s' at '%u:%u' and '%s' at '%u:%u'\n",
     902             :                         prev->file->sub, prev->parity_pos, prev->count, obj->file->sub, obj->parity_pos, obj->count);
     903             :                 ++arg->result;
     904             :                 return;
     905             :                 /* LCOV_EXCL_STOP */
     906             :         }
     907             : }
     908             : 
     909     6441048 : void chunk_file_check_foreach(void* void_arg, void* void_obj)
     910             : {
     911     6441048 :         struct chunk_check* arg = void_arg;
     912     6441048 :         const struct snapraid_chunk* obj = void_obj;
     913     6441048 :         const struct snapraid_chunk* prev = arg->prev;
     914             : 
     915             :         /* set the next previous block */
     916     6441048 :         arg->prev = obj;
     917             : 
     918             :         /* stop reporting if too many errors */
     919     6441048 :         if (arg->result > 100) {
     920             :                 /* LCOV_EXCL_START */
     921             :                 return;
     922             :                 /* LCOV_EXCL_STOP */
     923             :         }
     924             : 
     925     6441048 :         if (obj->count == 0) {
     926             :                 /* LCOV_EXCL_START */
     927             :                 log_fatal("Internal inconsistency in file count zero for file '%s' at '%u'\n",
     928             :                         obj->file->sub, obj->file_pos);
     929             :                 ++arg->result;
     930             :                 return;
     931             :                 /* LCOV_EXCL_STOP */
     932             :         }
     933             : 
     934             :         /* note that for deleted files, some chunks may be missing */
     935             : 
     936             :         /* if the files are different */
     937     6441048 :         if (!prev || prev->file != obj->file) {
     938     6395699 :                 if (prev != 0) {
     939     6393111 :                         if (file_flag_has(prev->file, FILE_IS_DELETED)) {
     940             :                                 /* check that the chunk doesn't overflow the file */
     941       59771 :                                 if (prev->file_pos + prev->count > prev->file->blockmax) {
     942             :                                         /* LCOV_EXCL_START */
     943             :                                         log_fatal("Internal inconsistency in delete end for file '%s' at '%u:%u' overflowing size '%u'\n",
     944             :                                                 prev->file->sub, prev->file_pos, prev->count, prev->file->blockmax);
     945             :                                         ++arg->result;
     946             :                                         return;
     947             :                                         /* LCOV_EXCL_STOP */
     948             :                                 }
     949             :                         } else {
     950             :                                 /* check that the chunk ends the file */
     951     6333340 :                                 if (prev->file_pos + prev->count != prev->file->blockmax) {
     952             :                                         /* LCOV_EXCL_START */
     953             :                                         log_fatal("Internal inconsistency in file end for file '%s' at '%u:%u' instead of size '%u'\n",
     954             :                                                 prev->file->sub, prev->file_pos, prev->count, prev->file->blockmax);
     955             :                                         ++arg->result;
     956             :                                         return;
     957             :                                         /* LCOV_EXCL_STOP */
     958             :                                 }
     959             :                         }
     960             :                 }
     961             : 
     962    12791398 :                 if (file_flag_has(obj->file, FILE_IS_DELETED)) {
     963             :                         /* check that the chunk doesn't overflow the file */
     964       59854 :                         if (obj->file_pos + obj->count > obj->file->blockmax) {
     965             :                                 /* LCOV_EXCL_START */
     966             :                                 log_fatal("Internal inconsistency in delete start for file '%s' at '%u:%u' overflowing size '%u'\n",
     967             :                                         obj->file->sub, obj->file_pos, obj->count, obj->file->blockmax);
     968             :                                 ++arg->result;
     969             :                                 return;
     970             :                                 /* LCOV_EXCL_STOP */
     971             :                         }
     972             :                 } else {
     973             :                         /* check that the chunk starts the file */
     974     6335845 :                         if (obj->file_pos != 0) {
     975             :                                 /* LCOV_EXCL_START */
     976             :                                 log_fatal("Internal inconsistency in file start for file '%s' at '%u:%u'\n",
     977             :                                         obj->file->sub, obj->file_pos, obj->count);
     978             :                                 ++arg->result;
     979             :                                 return;
     980             :                                 /* LCOV_EXCL_STOP */
     981             :                         }
     982             :                 }
     983             :         } else {
     984             :                 /* check the order */
     985       45349 :                 if (prev->file_pos >= obj->file_pos) {
     986             :                         /* LCOV_EXCL_START */
     987             :                         log_fatal("Internal inconsistency in file order for file '%s' at '%u:%u' and at '%u:%u'\n",
     988             :                                 prev->file->sub, prev->file_pos, prev->count, obj->file_pos, obj->count);
     989             :                         ++arg->result;
     990             :                         return;
     991             :                         /* LCOV_EXCL_STOP */
     992             :                 }
     993             : 
     994       45349 :                 if (file_flag_has(obj->file, FILE_IS_DELETED)) {
     995             :                         /* check that the chunks don't overlap */
     996          27 :                         if (prev->file_pos + prev->count > obj->file_pos) {
     997             :                                 /* LCOV_EXCL_START */
     998             :                                 log_fatal("Internal inconsistency in delete sequence for file '%s' at '%u:%u' and at '%u:%u'\n",
     999             :                                         prev->file->sub, prev->file_pos, prev->count, obj->file_pos, obj->count);
    1000             :                                 ++arg->result;
    1001             :                                 return;
    1002             :                                 /* LCOV_EXCL_STOP */
    1003             :                         }
    1004             :                 } else {
    1005             :                         /* check that the chunks are sequential */
    1006       45322 :                         if (prev->file_pos + prev->count != obj->file_pos) {
    1007             :                                 /* LCOV_EXCL_START */
    1008             :                                 log_fatal("Internal inconsistency in file sequence for file '%s' at '%u:%u' and at '%u:%u'\n",
    1009             :                                         prev->file->sub, prev->file_pos, prev->count, obj->file_pos, obj->count);
    1010             :                                 ++arg->result;
    1011             :                                 return;
    1012             :                                 /* LCOV_EXCL_STOP */
    1013             :                         }
    1014             :                 }
    1015             :         }
    1016             : }
    1017             : 
    1018        2838 : int fs_check(struct snapraid_disk* disk)
    1019             : {
    1020             :         struct chunk_check arg;
    1021             : 
    1022             :         /* error count starts from 0 */
    1023        2838 :         arg.result = 0;
    1024             : 
    1025             :         /* check parity sequence */
    1026        2838 :         arg.prev = 0;
    1027        2838 :         tommy_tree_foreach_arg(&disk->fs_parity, chunk_parity_check_foreach, &arg);
    1028             : 
    1029             :         /* check file sequence */
    1030        2838 :         arg.prev = 0;
    1031        2838 :         tommy_tree_foreach_arg(&disk->fs_file, chunk_file_check_foreach, &arg);
    1032             : 
    1033        2838 :         if (arg.result != 0)
    1034           0 :                 return -1;
    1035             : 
    1036        2838 :         return 0;
    1037             : }
    1038             : 
    1039             : struct chunk_parity_inside {
    1040             :         block_off_t parity_pos;
    1041             : };
    1042             : 
    1043             : /**
    1044             :  * Compare the chunk if containing the specified parity position.
    1045             :  */
    1046   102351431 : static int chunk_parity_inside_compare(const void* void_a, const void* void_b)
    1047             : {
    1048   102351431 :         const struct chunk_parity_inside* arg_a = void_a;
    1049   102351431 :         const struct snapraid_chunk* arg_b = void_b;
    1050             : 
    1051   102351431 :         if (arg_a->parity_pos < arg_b->parity_pos)
    1052    35386523 :                 return -1;
    1053    66964908 :         if (arg_a->parity_pos >= arg_b->parity_pos + arg_b->count)
    1054    59326333 :                 return 1;
    1055             : 
    1056     7638575 :         return 0;
    1057             : }
    1058             : 
    1059             : /**
    1060             :  * Seach the chunk at the specified parity position.
    1061             :  * The search is optimized for sequential accesses.
    1062             :  * \return If not found return 0
    1063             :  */
    1064    38418652 : static struct snapraid_chunk* fs_par2chunk_get_ts(struct snapraid_disk* disk, struct snapraid_chunk** fs_last, block_off_t parity_pos)
    1065             : {
    1066             :         struct snapraid_chunk* chunk;
    1067             : 
    1068             :         /* check if the last accessed chunk matches */
    1069    38418652 :         if (*fs_last
    1070    38399475 :                 && parity_pos >= (*fs_last)->parity_pos
    1071    38241144 :                 && parity_pos < (*fs_last)->parity_pos + (*fs_last)->count
    1072             :         ) {
    1073    29222600 :                 chunk = *fs_last;
    1074             :         } else {
    1075     9196052 :                 struct chunk_parity_inside arg = { parity_pos };
    1076     9196052 :                 chunk = tommy_tree_search_compare(&disk->fs_parity, chunk_parity_inside_compare, &arg);
    1077             :         }
    1078             : 
    1079    38418652 :         if (!chunk)
    1080     1557477 :                 return 0;
    1081             : 
    1082             :         /* store the last accessed chunk */
    1083    36861175 :         *fs_last = chunk;
    1084             : 
    1085    36861175 :         return chunk;
    1086             : }
    1087             : 
    1088             : struct chunk_file_inside {
    1089             :         struct snapraid_file* file;
    1090             :         block_off_t file_pos;
    1091             : };
    1092             : 
    1093             : /**
    1094             :  * Compare the chunk if containing the specified file position.
    1095             :  */
    1096    21495373 : static int chunk_file_inside_compare(const void* void_a, const void* void_b)
    1097             : {
    1098    21495373 :         const struct chunk_file_inside* arg_a = void_a;
    1099    21495373 :         const struct snapraid_chunk* arg_b = void_b;
    1100             : 
    1101    21495373 :         if (arg_a->file < arg_b->file)
    1102     8992026 :                 return -1;
    1103    12503347 :         if (arg_a->file > arg_b->file)
    1104    10470075 :                 return 1;
    1105             : 
    1106     2033272 :         if (arg_a->file_pos < arg_b->file_pos)
    1107        7395 :                 return -1;
    1108     2025877 :         if (arg_a->file_pos >= arg_b->file_pos + arg_b->count)
    1109        9044 :                 return 1;
    1110             : 
    1111     2016833 :         return 0;
    1112             : }
    1113             : 
    1114             : /**
    1115             :  * Search the chunk at the specified file position.
    1116             :  * The search is optimized for sequential accesses.
    1117             :  * \return If not found return 0
    1118             :  */
    1119    10223247 : 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)
    1120             : {
    1121             :         struct snapraid_chunk* chunk;
    1122             : 
    1123             :         /* check if the last accessed chunk matches */
    1124    10223247 :         if (*fs_last
    1125    10199301 :                 && file == (*fs_last)->file
    1126     8220750 :                 && file_pos >= (*fs_last)->file_pos
    1127     8220750 :                 && file_pos < (*fs_last)->file_pos + (*fs_last)->count
    1128             :         ) {
    1129     8206414 :                 chunk = *fs_last;
    1130             :         } else {
    1131     2016833 :                 struct chunk_file_inside arg = { file, file_pos };
    1132     2016833 :                 chunk = tommy_tree_search_compare(&disk->fs_file, chunk_file_inside_compare, &arg);
    1133             :         }
    1134             : 
    1135    10223247 :         if (!chunk)
    1136           0 :                 return 0;
    1137             : 
    1138             :         /* store the last accessed chunk */
    1139    10223247 :         *fs_last = chunk;
    1140             : 
    1141    10223247 :         return chunk;
    1142             : }
    1143             : 
    1144    38311850 : 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)
    1145             : {
    1146             :         struct snapraid_chunk* chunk;
    1147             : 
    1148    38311850 :         chunk = fs_par2chunk_get_ts(disk, fs_last, parity_pos);
    1149    38311850 :         if (!chunk)
    1150     1557477 :                 return 0;
    1151             : 
    1152    36754373 :         if (file_pos)
    1153    36410379 :                 *file_pos = chunk->file_pos + (parity_pos - chunk->parity_pos);
    1154             : 
    1155    36754373 :         return chunk->file;
    1156             : }
    1157             : 
    1158     4878725 : 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)
    1159             : {
    1160             :         struct snapraid_chunk* chunk;
    1161             : 
    1162     4878725 :         chunk = fs_file2chunk_get_ts(disk, fs_last, file, file_pos);
    1163     4878725 :         if (!chunk) {
    1164             :                 /* LCOV_EXCL_START */
    1165             :                 log_fatal("Internal inconsistency when resolving file '%s' at position '%u/%u' in disk '%s'\n", file->sub, file_pos, file->blockmax, disk->name);
    1166             :                 os_abort();
    1167             :                 /* LCOV_EXCL_STOP */
    1168             :         }
    1169             : 
    1170     4878725 :         return chunk->parity_pos + (file_pos - chunk->file_pos);
    1171             : }
    1172             : 
    1173     8975787 : void fs_allocate(struct snapraid_disk* disk, block_off_t parity_pos, struct snapraid_file* file, block_off_t file_pos)
    1174             : {
    1175             :         struct snapraid_chunk* chunk;
    1176             :         struct snapraid_chunk* parity_chunk;
    1177             :         struct snapraid_chunk* file_chunk;
    1178             : 
    1179     8975787 :         if (file_pos > 0) {
    1180             :                 /* search an existing chunk for the previous file_pos */
    1181     5344522 :                 chunk = fs_file2chunk_get_ts(disk, &disk->fs_last, file, file_pos - 1);
    1182             : 
    1183     5344522 :                 if (chunk != 0 && parity_pos == chunk->parity_pos + chunk->count) {
    1184             :                         /* ensure that we are extending the chunk at the end */
    1185     5318361 :                         if (file_pos != chunk->file_pos + chunk->count) {
    1186             :                                 /* LCOV_EXCL_START */
    1187             :                                 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);
    1188             :                                 os_abort();
    1189             :                                 /* LCOV_EXCL_STOP */
    1190             :                         }
    1191             : 
    1192             :                         /* extend the existing chunk */
    1193     5318361 :                         ++chunk->count;
    1194             : 
    1195    14294148 :                         return;
    1196             :                 }
    1197             :         }
    1198             : 
    1199             :         /* a chunk doesn't exist, and we have to create a new one */
    1200     3657426 :         chunk = chunk_alloc(parity_pos, file, file_pos, 1);
    1201             : 
    1202             :         /* insert the chunk in the trees */
    1203     3657426 :         parity_chunk = tommy_tree_insert(&disk->fs_parity, &chunk->parity_node, chunk);
    1204     3657426 :         file_chunk = tommy_tree_insert(&disk->fs_file, &chunk->file_node, chunk);
    1205             : 
    1206     3657426 :         if (parity_chunk != chunk || file_chunk != chunk) {
    1207             :                 /* LCOV_EXCL_START */
    1208             :                 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);
    1209             :                 os_abort();
    1210             :                 /* LCOV_EXCL_STOP */
    1211             :         }
    1212             : 
    1213             :         /* store the last accessed chunk */
    1214     3657426 :         disk->fs_last = chunk;
    1215             : }
    1216             : 
    1217      106802 : void fs_deallocate(struct snapraid_disk* disk, block_off_t parity_pos)
    1218             : {
    1219             :         struct snapraid_chunk* chunk;
    1220             :         struct snapraid_chunk* second_chunk;
    1221             :         struct snapraid_chunk* parity_chunk;
    1222             :         struct snapraid_chunk* file_chunk;
    1223             :         block_off_t first_count, second_count;
    1224             : 
    1225      106802 :         chunk = fs_par2chunk_get_ts(disk, &disk->fs_last, parity_pos);
    1226      106802 :         if (!chunk) {
    1227             :                 /* LCOV_EXCL_START */
    1228             :                 log_fatal("Internal inconsistency when deallocating parity position '%u' for not existing chunk in disk '%s'\n", parity_pos, disk->name);
    1229             :                 os_abort();
    1230             :                 /* LCOV_EXCL_STOP */
    1231             :         }
    1232             : 
    1233             :         /* if it's the only block of the chunk, delete it */
    1234      106802 :         if (chunk->count == 1) {
    1235             :                 /* remove from the trees */
    1236       41625 :                 tommy_tree_remove(&disk->fs_parity, chunk);
    1237       41625 :                 tommy_tree_remove(&disk->fs_file, chunk);
    1238             : 
    1239             :                 /* deallocate */
    1240       41625 :                 chunk_free(chunk);
    1241             : 
    1242             :                 /* clear the last accessed chunk */
    1243       41625 :                 disk->fs_last = 0;
    1244       41625 :                 return;
    1245             :         }
    1246             : 
    1247             :         /* if it's at the start of the chunk, shrink the chunk */
    1248       65177 :         if (parity_pos == chunk->parity_pos) {
    1249       65175 :                 ++chunk->parity_pos;
    1250       65175 :                 ++chunk->file_pos;
    1251       65175 :                 --chunk->count;
    1252       65175 :                 return;
    1253             :         }
    1254             : 
    1255             :         /* if it's at the end of the chunk, shrink the chunk */
    1256           2 :         if (parity_pos == chunk->parity_pos + chunk->count - 1) {
    1257           1 :                 --chunk->count;
    1258           1 :                 return;
    1259             :         }
    1260             : 
    1261             :         /* otherwise it's in the middle */
    1262           1 :         first_count = parity_pos - chunk->parity_pos;
    1263           1 :         second_count = chunk->count - first_count - 1;
    1264             : 
    1265             :         /* adjust the first chunk */
    1266           1 :         chunk->count = first_count;
    1267             : 
    1268             :         /* allocate the second chunk */
    1269           1 :         second_chunk = chunk_alloc(chunk->parity_pos + first_count + 1, chunk->file, chunk->file_pos + first_count + 1, second_count);
    1270             : 
    1271             :         /* insert the chunk in the trees */
    1272           1 :         parity_chunk = tommy_tree_insert(&disk->fs_parity, &second_chunk->parity_node, second_chunk);
    1273           1 :         file_chunk = tommy_tree_insert(&disk->fs_file, &second_chunk->file_node, second_chunk);
    1274             : 
    1275           1 :         if (parity_chunk != second_chunk || file_chunk != second_chunk) {
    1276             :                 /* LCOV_EXCL_START */
    1277             :                 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);
    1278             :                 os_abort();
    1279             :                 /* LCOV_EXCL_STOP */
    1280             :         }
    1281             : 
    1282             :         /* store the last accessed chunk */
    1283           1 :         disk->fs_last = second_chunk;
    1284             : }
    1285             : 
    1286    44106216 : struct snapraid_block* fs_file2block_get(struct snapraid_file* file, block_off_t file_pos)
    1287             : {
    1288    44106216 :         if (file_pos >= file->blockmax) {
    1289             :                 /* LCOV_EXCL_START */
    1290             :                 log_fatal("Internal inconsistency when dereferencing file '%s' at position '%u/%u'\n", file->sub, file_pos, file->blockmax);
    1291             :                 os_abort();
    1292             :                 /* LCOV_EXCL_STOP */
    1293             :         }
    1294             : 
    1295    44106216 :         return &file->blockvec[file_pos];
    1296             : }
    1297             : 
    1298    26733115 : struct snapraid_block* fs_par2block_get_ts(struct snapraid_disk* disk, struct snapraid_chunk** fs_last, block_off_t parity_pos)
    1299             : {
    1300             :         struct snapraid_file* file;
    1301             :         block_off_t file_pos;
    1302             : 
    1303    26733115 :         file = fs_par2file_get_ts(disk, fs_last, parity_pos, &file_pos);
    1304             : 
    1305    26733115 :         if (!file)
    1306     1557477 :                 return BLOCK_EMPTY;
    1307             : 
    1308    25175638 :         return fs_file2block_get(file, file_pos);
    1309             : }
    1310             : 
    1311        1495 : struct snapraid_map* map_alloc(const char* name, unsigned position, block_off_t total_blocks, block_off_t free_blocks, const char* uuid)
    1312             : {
    1313             :         struct snapraid_map* map;
    1314             : 
    1315        1495 :         map = malloc_nofail(sizeof(struct snapraid_map));
    1316        1495 :         pathcpy(map->name, sizeof(map->name), name);
    1317        1495 :         map->position = position;
    1318        1495 :         map->total_blocks = total_blocks;
    1319        1495 :         map->free_blocks = free_blocks;
    1320        1495 :         pathcpy(map->uuid, sizeof(map->uuid), uuid);
    1321             : 
    1322        1495 :         return map;
    1323             : }
    1324             : 
    1325        1375 : void map_free(struct snapraid_map* map)
    1326             : {
    1327        1375 :         free(map);
    1328        1375 : }
    1329             : 
    1330      707506 : int time_compare(const void* void_a, const void* void_b)
    1331             : {
    1332      707506 :         const time_t* time_a = void_a;
    1333      707506 :         const time_t* time_b = void_b;
    1334             : 
    1335      707506 :         if (*time_a < *time_b)
    1336       26871 :                 return -1;
    1337      680635 :         if (*time_a > *time_b)
    1338      146878 :                 return 1;
    1339      533757 :         return 0;
    1340             : }

Generated by: LCOV version 1.10