LCOV - code coverage report
Current view: top level - cmdline - elem.c (source / functions) Hit Total Coverage
Test: lcov.info Lines: 648 683 94.9 %
Date: 2026-02-28 16:42:47 Functions: 70 72 97.2 %

          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 "state.h"
      23             : #include "util.h"
      24             : 
      25             : /****************************************************************************/
      26             : /* snapraid */
      27             : 
      28             : int BLOCK_HASH_SIZE = HASH_MAX;
      29             : 
      30        1667 : struct snapraid_content* content_alloc(const char* path, uint64_t dev)
      31             : {
      32             :         struct snapraid_content* content;
      33             : 
      34        1667 :         content = malloc_nofail(sizeof(struct snapraid_content));
      35        1667 :         pathimport(content->content, sizeof(content->content), path);
      36        1667 :         content->device = dev;
      37             : 
      38        1667 :         return content;
      39             : }
      40             : 
      41        1612 : void content_free(struct snapraid_content* content)
      42             : {
      43        1612 :         free(content);
      44        1612 : }
      45             : 
      46         671 : struct snapraid_filter* filter_alloc_file(int direction, const char* root, const char* pattern)
      47             : {
      48             :         struct snapraid_filter* filter;
      49             :         char* i;
      50             :         char* first;
      51             :         char* last;
      52             :         int token_is_valid;
      53             :         int token_is_filled;
      54             : 
      55         671 :         filter = malloc_nofail(sizeof(struct snapraid_filter));
      56         671 :         pathimport(filter->pattern, sizeof(filter->pattern), pattern);
      57         671 :         pathimport(filter->root, sizeof(filter->root), root);
      58         671 :         filter->direction = direction;
      59             : 
      60             :         /* find first and last slash */
      61         671 :         first = 0;
      62         671 :         last = 0;
      63             : 
      64             :         /* reject invalid tokens, like "<empty>", ".", ".." and more dots */
      65         671 :         token_is_valid = 0;
      66         671 :         token_is_filled = 0;
      67        7618 :         for (i = filter->pattern; *i; ++i) {
      68        6948 :                 if (*i == '/') {
      69             :                         /* reject invalid tokens, but accept an empty one as first */
      70          90 :                         if (!token_is_valid && (first != 0 || token_is_filled)) {
      71           1 :                                 free(filter);
      72           1 :                                 return 0;
      73             :                         }
      74          89 :                         token_is_valid = 0;
      75          89 :                         token_is_filled = 0;
      76             : 
      77             :                         /* update slash position */
      78          89 :                         if (!first)
      79          51 :                                 first = i;
      80          89 :                         last = i;
      81        6858 :                 } else if (*i != '.') {
      82        6274 :                         token_is_valid = 1;
      83        6274 :                         token_is_filled = 1;
      84             :                 } else {
      85         584 :                         token_is_filled = 1;
      86             :                 }
      87             :         }
      88             : 
      89             :         /* reject invalid tokens, but accept an empty one as last, but not if it's the only one */
      90         670 :         if (!token_is_valid && (first == 0 || token_is_filled)) {
      91           1 :                 free(filter);
      92           1 :                 return 0;
      93             :         }
      94             : 
      95             :         /* it's a file filter */
      96         669 :         filter->is_disk = 0;
      97             : 
      98         669 :         if (first == 0) {
      99             :                 /* no slash */
     100         618 :                 filter->is_abs = 0;
     101         618 :                 filter->is_dir = 0;
     102             :         } else {
     103          51 :                 if (first == filter->pattern) {
     104             :                         /* slash at the start */
     105          38 :                         filter->is_abs = 1;
     106             :                 } else {
     107             :                         /* no slash at the start */
     108          13 :                         filter->is_abs = 0;
     109             :                 }
     110          51 :                 if (last[1] == 0) {
     111             :                         /* slash at the end */
     112          50 :                         filter->is_dir = 1;
     113          50 :                         last[0] = 0; /* clear the slash */
     114             :                 } else {
     115             :                         /* no slash at the end */
     116           1 :                         filter->is_dir = 0;
     117             :                 }
     118             :         }
     119             : 
     120         669 :         return filter;
     121             : }
     122             : 
     123          11 : struct snapraid_filter* filter_alloc_disk(int direction, const char* pattern)
     124             : {
     125             :         struct snapraid_filter* filter;
     126             : 
     127          11 :         filter = malloc_nofail(sizeof(struct snapraid_filter));
     128          11 :         pathimport(filter->pattern, sizeof(filter->pattern), pattern);
     129          11 :         filter->direction = direction;
     130             : 
     131             :         /* it's a disk filter */
     132          11 :         filter->is_disk = 1;
     133          11 :         filter->is_abs = 0;
     134          11 :         filter->is_dir = 0;
     135             : 
     136             :         /* no slash allowed in disk names */
     137          11 :         if (strchr(filter->pattern, '/') != 0) {
     138             :                 /* LCOV_EXCL_START */
     139             :                 free(filter);
     140             :                 return 0;
     141             :                 /* LCOV_EXCL_STOP */
     142             :         }
     143             : 
     144          11 :         return filter;
     145             : }
     146             : 
     147         660 : void filter_free(void* filter)
     148             : {
     149         660 :         free(filter);
     150         660 : }
     151             : 
     152      124831 : const char* filter_type(struct snapraid_filter* filter, char* out, size_t out_size)
     153             : {
     154             :         const char* direction;
     155             : 
     156      124831 :         if (filter->direction < 0)
     157      124546 :                 direction = "exclude";
     158             :         else
     159         285 :                 direction = "include";
     160             : 
     161      124831 :         if (filter->is_disk)
     162           0 :                 pathprint(out, out_size, "%s %s:", direction, filter->pattern);
     163      124831 :         else if (filter->is_dir)
     164          18 :                 pathprint(out, out_size, "%s %s/", direction, filter->pattern);
     165             :         else
     166      124813 :                 pathprint(out, out_size, "%s %s", direction, filter->pattern);
     167             : 
     168      124831 :         return out;
     169             : }
     170             : 
     171             : /**
     172             :  * Apply a filter to the specified path
     173             :  *
     174             :  * @param filter The filter to apply.
     175             :  * @param reason Assign to it the filter that caused rejection.
     176             :  * @param path The path to filter.
     177             :  * @param is_dir if the path refers to a directory, otherwise to a file.
     178             :  * @return < 0 if the path is excluded, > 0 if the path is included, 0 if the filter doesn't apply.
     179             :  */
     180     6802349 : static int filter_apply(struct snapraid_filter* filter, struct snapraid_filter** reason, const char* path, int is_dir)
     181             : {
     182     6802349 :         if (filter->root[0] != 0) { /* if it's a local filter */
     183      127913 :                 if (!path_is_root_of(filter->root, path)) /* applies it only if it matches the root */
     184       68719 :                         return 0;
     185             :                 /* advance the path to compare to make it local */
     186       59194 :                 path += strlen(filter->root);
     187             :         }
     188             : 
     189             :         /* match dirs with dirs and files with files */
     190     6733630 :         if (filter->is_dir && !is_dir)
     191      119172 :                 return 0;
     192     6614458 :         if (!filter->is_dir && is_dir)
     193        6510 :                 return 0;
     194             : 
     195     6607948 :         int ret = 0;
     196             : 
     197     6607948 :         if (filter->is_abs) {
     198             :                 /* skip initial slash, as always missing in the path */
     199        6035 :                 if (wnmatch(filter->pattern + 1, path) == 0)
     200           0 :                         ret = filter->direction;
     201             :         } else {
     202             :                 /* the path is relative, first try to match from the root */
     203     6601913 :                 if (wnmatch(filter->pattern, path) == 0) {
     204         234 :                         ret = filter->direction;
     205             :                 } else {
     206             :                         /* then try to match after all the / presents */
     207     6601679 :                         char* slash = strchr(path, '/');
     208    13012187 :                         while (slash) {
     209     6534505 :                                 if (wnmatch(filter->pattern, slash + 1) == 0) {
     210      123997 :                                         ret = filter->direction;
     211      123997 :                                         break;
     212             :                                 }
     213             : 
     214     6410508 :                                 slash = strchr(slash + 1, '/');
     215             :                         }
     216             :                 }
     217             :         }
     218             : 
     219     6607948 :         if (reason != 0 && ret < 0)
     220      124231 :                 *reason = filter;
     221             : 
     222     6607948 :         return ret;
     223             : }
     224             : 
     225             : /**
     226             :  * Apply a list of filter to a specific disk/sub path
     227             :  *
     228             :  * @param filterlist The list of filter to apply
     229             :  * @param reason Assign to it the filter that caused rejection.
     230             :  * @param disk The disk that contain the path
     231             :  * @param sub The path in the disk
     232             :  * @param is_dir If the path refers to a directory, otherwise it's a file
     233             :  * @param is_def_include If no filter apply, if by default force the inclusion. Used to include directory by default.
     234             :  * @return < 0 if the path is excluded, == 0 if the path is included.
     235             :  */
     236             : 
     237     4767278 : static int filter_element(tommy_list* filterlist, struct snapraid_filter** reason, const char* disk, const char* sub, int is_dir, int is_def_include)
     238             : {
     239             :         tommy_node* i;
     240             : 
     241             :         /**
     242             :          * The rule is that the latest filter defines the behaviour of paths
     243             :          * that doesn't match any filter.
     244             :          *
     245             :          * If the latest filter is an 'exclusion' filter, the default is to include.
     246             :          * If the latest filter is an 'inclusion' filter, the default is to exclude.
     247             :          * If there are no filter, the default is to include.
     248             :          */
     249     4767278 :         int default_direction = 1;
     250             : 
     251             :         /* for each filter */
     252    11563865 :         for (i = tommy_list_head(filterlist); i != 0; i = i->next) {
     253             :                 int ret;
     254     6929498 :                 struct snapraid_filter* filter = i->data;
     255             : 
     256     6929498 :                 if (filter->is_disk) {
     257      127149 :                         if (wnmatch(filter->pattern, disk) == 0) {
     258        8680 :                                 ret = filter->direction;
     259             :                         } else {
     260      118469 :                                 ret = 0;
     261             :                         }
     262      127149 :                         if (reason != 0 && ret < 0)
     263           0 :                                 *reason = filter;
     264             :                 } else {
     265     6802349 :                         ret = filter_apply(filter, reason, sub, is_dir);
     266             :                 }
     267             : 
     268     6929498 :                 if (ret > 0) {
     269             :                         /* include the file */
     270        8680 :                         return 0;
     271     6920818 :                 } else if (ret < 0) {
     272             :                         /* exclude the file */
     273      124231 :                         return -1;
     274             :                 } else {
     275             :                         /* default is opposite of the last filter */
     276     6796587 :                         default_direction = -filter->direction;
     277     6796587 :                         if (reason != 0 && default_direction < 0)
     278     3271645 :                                 *reason = filter;
     279             :                         /* continue with the next one */
     280             :                 }
     281             :         }
     282             : 
     283             :         /* directories are always included by default, otherwise we cannot apply rules */
     284             :         /* to the contained files */
     285     4634367 :         if (is_def_include)
     286        4679 :                 return 0;
     287             : 
     288             :         /* files are excluded/included depending of the last rule processed */
     289     4629688 :         if (default_direction < 0)
     290       77759 :                 return -1;
     291             : 
     292     4551929 :         return 0;
     293             : }
     294             : 
     295     4762550 : int filter_path(tommy_list* filterlist, struct snapraid_filter** reason, const char* disk, const char* sub)
     296             : {
     297     4762550 :         return filter_element(filterlist, reason, disk, sub, 0, 0);
     298             : }
     299             : 
     300        4679 : int filter_subdir(tommy_list* filterlist, struct snapraid_filter** reason, const char* disk, const char* sub)
     301             : {
     302        4679 :         return filter_element(filterlist, reason, disk, sub, 1, 1);
     303             : }
     304             : 
     305          49 : int filter_emptydir(tommy_list* filterlist, struct snapraid_filter** reason, const char* disk, const char* sub)
     306             : {
     307          49 :         return filter_element(filterlist, reason, disk, sub, 1, 0);
     308             : }
     309             : 
     310       77590 : int filter_existence(int filter_missing, const char* dir, const char* sub)
     311             : {
     312             :         char path[PATH_MAX];
     313             :         struct stat st;
     314             : 
     315       77590 :         if (!filter_missing)
     316       31984 :                 return 0;
     317             : 
     318             :         /* we directly check if in the disk the file is present or not */
     319       45606 :         pathprint(path, sizeof(path), "%s%s", dir, sub);
     320             : 
     321       45606 :         if (lstat(path, &st) != 0) {
     322             :                 /* if the file doesn't exist, we don't filter it out */
     323        5018 :                 if (errno == ENOENT)
     324        5018 :                         return 0;
     325             :                 /* LCOV_EXCL_START */
     326             :                 log_fatal(errno, "Error in stat file '%s'. %s.\n", path, strerror(errno));
     327             :                 exit(EXIT_FAILURE);
     328             :                 /* LCOV_EXCL_STOP */
     329             :         }
     330             : 
     331             :         /* the file is present, so we filter it out */
     332       40588 :         return 1;
     333             : }
     334             : 
     335       35970 : int filter_correctness(int filter_error, tommy_arrayblkof* infoarr, struct snapraid_disk* disk, struct snapraid_file* file)
     336             : {
     337             :         unsigned i;
     338             : 
     339       35970 :         if (!filter_error)
     340       13422 :                 return 0;
     341             : 
     342             :         /* check each block of the file */
     343       60632 :         for (i = 0; i < file->blockmax; ++i) {
     344       46359 :                 block_off_t parity_pos = fs_file2par_get(disk, file, i);
     345       46359 :                 snapraid_info info = info_get(infoarr, parity_pos);
     346             : 
     347             :                 /* if the file has a bad block, don't exclude it */
     348       46359 :                 if (info_get_bad(info))
     349        8275 :                         return 0;
     350             :         }
     351             : 
     352             :         /* the file is correct, so we filter it out */
     353       14273 :         return 1;
     354             : }
     355             : 
     356     3335241 : int filter_content(tommy_list* contentlist, const char* path)
     357             : {
     358             :         tommy_node* i;
     359             : 
     360    21542032 :         for (i = tommy_list_head(contentlist); i != 0; i = i->next) {
     361    18206791 :                 struct snapraid_content* content = i->data;
     362             :                 char tmp[PATH_MAX];
     363             : 
     364    18206791 :                 if (pathcmp(content->content, path) == 0)
     365           0 :                         return -1;
     366             : 
     367             :                 /* exclude also the ".tmp" copy used to save it */
     368    18206791 :                 pathprint(tmp, sizeof(tmp), "%s.tmp", content->content);
     369    18206791 :                 if (pathcmp(tmp, path) == 0)
     370           0 :                         return -1;
     371             : 
     372             :                 /* exclude also the ".lock" file */
     373    18206791 :                 pathprint(tmp, sizeof(tmp), "%s.lock", content->content);
     374    18206791 :                 if (pathcmp(tmp, path) == 0)
     375           0 :                         return -1;
     376             :         }
     377             : 
     378     3335241 :         return 0;
     379             : }
     380             : 
     381     3923527 : 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)
     382             : {
     383             :         struct snapraid_file* file;
     384             :         block_off_t i;
     385             : 
     386     3923527 :         file = malloc_nofail(sizeof(struct snapraid_file));
     387     3923527 :         file->sub = strdup_nofail(sub);
     388     3923527 :         file->size = size;
     389     3923527 :         file->blockmax = (size + block_size - 1) / block_size;
     390     3923527 :         file->mtime_sec = mtime_sec;
     391     3923527 :         file->mtime_nsec = mtime_nsec;
     392     3923527 :         file->inode = inode;
     393     3923527 :         file->physical = physical;
     394     3923527 :         file->flag = 0;
     395     3923527 :         file->blockvec = malloc_nofail(file->blockmax * block_sizeof());
     396             : 
     397    13628313 :         for (i = 0; i < file->blockmax; ++i) {
     398     9704786 :                 struct snapraid_block* block = file_block(file, i);
     399     9704786 :                 block_state_set(block, BLOCK_STATE_CHG);
     400     9704786 :                 hash_invalid_set(block->hash);
     401             :         }
     402             : 
     403     3923527 :         return file;
     404             : }
     405             : 
     406       22637 : struct snapraid_file* file_dup(struct snapraid_file* copy)
     407             : {
     408             :         struct snapraid_file* file;
     409             :         block_off_t i;
     410             : 
     411       22637 :         file = malloc_nofail(sizeof(struct snapraid_file));
     412       22637 :         file->sub = strdup_nofail(copy->sub);
     413       22637 :         file->size = copy->size;
     414       22637 :         file->blockmax = copy->blockmax;
     415       22637 :         file->mtime_sec = copy->mtime_sec;
     416       22637 :         file->mtime_nsec = copy->mtime_nsec;
     417       22637 :         file->inode = copy->inode;
     418       22637 :         file->physical = copy->physical;
     419       22637 :         file->flag = copy->flag;
     420       22637 :         file->blockvec = malloc_nofail(file->blockmax * block_sizeof());
     421             : 
     422       79556 :         for (i = 0; i < file->blockmax; ++i) {
     423       56919 :                 struct snapraid_block* block = file_block(file, i);
     424       56919 :                 struct snapraid_block* copy_block = file_block(copy, i);
     425       56919 :                 block->state = copy_block->state;
     426       56919 :                 memcpy(block->hash, copy_block->hash, BLOCK_HASH_SIZE);
     427             :         }
     428             : 
     429       22637 :         return file;
     430             : }
     431             : 
     432     3900538 : void file_free(struct snapraid_file* file)
     433             : {
     434     3900538 :         free(file->sub);
     435     3900538 :         file->sub = 0;
     436     3900538 :         free(file->blockvec);
     437     3900538 :         file->blockvec = 0;
     438     3900538 :         free(file);
     439     3900538 : }
     440             : 
     441         155 : void file_rename(struct snapraid_file* file, const char* sub)
     442             : {
     443         155 :         free(file->sub);
     444         155 :         file->sub = strdup_nofail(sub);
     445         155 : }
     446             : 
     447       22989 : void file_copy(struct snapraid_file* src_file, struct snapraid_file* dst_file)
     448             : {
     449             :         block_off_t i;
     450             : 
     451       22989 :         if (src_file->size != dst_file->size) {
     452             :                 /* LCOV_EXCL_START */
     453             :                 log_fatal(EINTERNAL, "Internal inconsistency: Copy file with different size\n");
     454             :                 os_abort();
     455             :                 /* LCOV_EXCL_STOP */
     456             :         }
     457             : 
     458       22989 :         if (src_file->mtime_sec != dst_file->mtime_sec) {
     459             :                 /* LCOV_EXCL_START */
     460             :                 log_fatal(EINTERNAL, "Internal inconsistency: Copy file with different mtime_sec\n");
     461             :                 os_abort();
     462             :                 /* LCOV_EXCL_STOP */
     463             :         }
     464             : 
     465       22989 :         if (src_file->mtime_nsec != dst_file->mtime_nsec) {
     466             :                 /* LCOV_EXCL_START */
     467             :                 log_fatal(EINTERNAL, "Internal inconsistency: Copy file with different mtime_nsec\n");
     468             :                 os_abort();
     469             :                 /* LCOV_EXCL_STOP */
     470             :         }
     471             : 
     472       79542 :         for (i = 0; i < dst_file->blockmax; ++i) {
     473             :                 /* set a block with hash computed but without parity */
     474       56553 :                 block_state_set(file_block(dst_file, i), BLOCK_STATE_REP);
     475             : 
     476             :                 /* copy the hash */
     477       56553 :                 memcpy(file_block(dst_file, i)->hash, file_block(src_file, i)->hash, BLOCK_HASH_SIZE);
     478             :         }
     479             : 
     480       22989 :         file_flag_set(dst_file, FILE_IS_COPY);
     481       22989 : }
     482             : 
     483       46502 : const char* file_name(const struct snapraid_file* file)
     484             : {
     485       46502 :         const char* r = strrchr(file->sub, '/');
     486             : 
     487       46502 :         if (!r)
     488          78 :                 r = file->sub;
     489             :         else
     490       46424 :                 ++r;
     491       46502 :         return r;
     492             : }
     493             : 
     494     7090512 : unsigned file_block_size(struct snapraid_file* file, block_off_t file_pos, unsigned block_size)
     495             : {
     496             :         /* if it's the last block */
     497     7090512 :         if (file_pos + 1 == file->blockmax) {
     498             :                 unsigned block_remainder;
     499     2882009 :                 if (file->size == 0)
     500           0 :                         return 0;
     501     2882009 :                 block_remainder = file->size % block_size;
     502     2882009 :                 if (block_remainder == 0)
     503        3187 :                         block_remainder = block_size;
     504     2882009 :                 return block_remainder;
     505             :         }
     506             : 
     507     4208503 :         return block_size;
     508             : }
     509             : 
     510     5400968 : int file_block_is_last(struct snapraid_file* file, block_off_t file_pos)
     511             : {
     512     5400968 :         if (file_pos == 0 && file->blockmax == 0)
     513           0 :                 return 1;
     514             : 
     515     5400968 :         if (file_pos >= file->blockmax) {
     516             :                 /* LCOV_EXCL_START */
     517             :                 log_fatal(EINTERNAL, "Internal inconsistency: File block position over the max\n");
     518             :                 os_abort();
     519             :                 /* LCOV_EXCL_STOP */
     520             :         }
     521             : 
     522     5400968 :         return file_pos == file->blockmax - 1;
     523             : }
     524             : 
     525     1096883 : int file_inode_compare_to_arg(const void* void_arg, const void* void_data)
     526             : {
     527     1096883 :         const uint64_t* arg = void_arg;
     528     1096883 :         const struct snapraid_file* file = void_data;
     529             : 
     530     1096883 :         if (*arg < file->inode)
     531           0 :                 return -1;
     532     1096883 :         if (*arg > file->inode)
     533           0 :                 return 1;
     534     1096883 :         return 0;
     535             : }
     536             : 
     537           0 : int file_inode_compare(const void* void_a, const void* void_b)
     538             : {
     539           0 :         const struct snapraid_file* file_a = void_a;
     540           0 :         const struct snapraid_file* file_b = void_b;
     541             : 
     542           0 :         if (file_a->inode < file_b->inode)
     543           0 :                 return -1;
     544           0 :         if (file_a->inode > file_b->inode)
     545           0 :                 return 1;
     546           0 :         return 0;
     547             : }
     548             : 
     549      187022 : int file_path_compare(const void* void_a, const void* void_b)
     550             : {
     551      187022 :         const struct snapraid_file* file_a = void_a;
     552      187022 :         const struct snapraid_file* file_b = void_b;
     553             : 
     554      187022 :         return strcmp(file_a->sub, file_b->sub);
     555             : }
     556             : 
     557       93962 : int file_physical_compare(const void* void_a, const void* void_b)
     558             : {
     559       93962 :         const struct snapraid_file* file_a = void_a;
     560       93962 :         const struct snapraid_file* file_b = void_b;
     561             : 
     562       93962 :         if (file_a->physical < file_b->physical)
     563       52384 :                 return -1;
     564       41578 :         if (file_a->physical > file_b->physical)
     565       41576 :                 return 1;
     566           2 :         return 0;
     567             : }
     568             : 
     569      105747 : int file_path_compare_to_arg(const void* void_arg, const void* void_data)
     570             : {
     571      105747 :         const char* arg = void_arg;
     572      105747 :         const struct snapraid_file* file = void_data;
     573             : 
     574      105747 :         return strcmp(arg, file->sub);
     575             : }
     576             : 
     577       23251 : int file_name_compare(const void* void_a, const void* void_b)
     578             : {
     579       23251 :         const struct snapraid_file* file_a = void_a;
     580       23251 :         const struct snapraid_file* file_b = void_b;
     581       23251 :         const char* name_a = file_name(file_a);
     582       23251 :         const char* name_b = file_name(file_b);
     583             : 
     584       23251 :         return strcmp(name_a, name_b);
     585             : }
     586             : 
     587       23021 : int file_stamp_compare(const void* void_a, const void* void_b)
     588             : {
     589       23021 :         const struct snapraid_file* file_a = void_a;
     590       23021 :         const struct snapraid_file* file_b = void_b;
     591             : 
     592       23021 :         if (file_a->size < file_b->size)
     593           0 :                 return -1;
     594       23021 :         if (file_a->size > file_b->size)
     595           0 :                 return 1;
     596             : 
     597       23021 :         if (file_a->mtime_sec < file_b->mtime_sec)
     598           0 :                 return -1;
     599       23021 :         if (file_a->mtime_sec > file_b->mtime_sec)
     600           0 :                 return 1;
     601             : 
     602       23021 :         if (file_a->mtime_nsec < file_b->mtime_nsec)
     603           0 :                 return -1;
     604       23021 :         if (file_a->mtime_nsec > file_b->mtime_nsec)
     605           0 :                 return 1;
     606             : 
     607       23021 :         return 0;
     608             : }
     609             : 
     610       23251 : int file_namestamp_compare(const void* void_a, const void* void_b)
     611             : {
     612             :         int ret;
     613             : 
     614       23251 :         ret = file_name_compare(void_a, void_b);
     615       23251 :         if (ret != 0)
     616         230 :                 return ret;
     617             : 
     618       23021 :         return file_stamp_compare(void_a, void_b);
     619             : }
     620             : 
     621           0 : int file_pathstamp_compare(const void* void_a, const void* void_b)
     622             : {
     623             :         int ret;
     624             : 
     625           0 :         ret = file_path_compare(void_a, void_b);
     626           0 :         if (ret != 0)
     627           0 :                 return ret;
     628             : 
     629           0 :         return file_stamp_compare(void_a, void_b);
     630             : }
     631             : 
     632     3969256 : struct snapraid_extent* extent_alloc(block_off_t parity_pos, struct snapraid_file* file, block_off_t file_pos, block_off_t count)
     633             : {
     634             :         struct snapraid_extent* extent;
     635             : 
     636     3969256 :         if (count == 0) {
     637             :                 /* LCOV_EXCL_START */
     638             :                 log_fatal(EINTERNAL, "Internal inconsistency: Allocating empty extent for file '%s' at position '%u/%u'\n", file->sub, file_pos, file->blockmax);
     639             :                 os_abort();
     640             :                 /* LCOV_EXCL_STOP */
     641             :         }
     642     3969256 :         if (file_pos + count > file->blockmax) {
     643             :                 /* LCOV_EXCL_START */
     644             :                 log_fatal(EINTERNAL, "Internal inconsistency: Allocating overflowing extent for file '%s' at position '%u:%u/%u'\n", file->sub, file_pos, count, file->blockmax);
     645             :                 os_abort();
     646             :                 /* LCOV_EXCL_STOP */
     647             :         }
     648             : 
     649     3969256 :         extent = malloc_nofail(sizeof(struct snapraid_extent));
     650     3969256 :         extent->parity_pos = parity_pos;
     651     3969256 :         extent->file = file;
     652     3969256 :         extent->file_pos = file_pos;
     653     3969256 :         extent->count = count;
     654             : 
     655     3969256 :         return extent;
     656             : }
     657             : 
     658     3923687 : void extent_free(struct snapraid_extent* extent)
     659             : {
     660     3923687 :         free(extent);
     661     3923687 : }
     662             : 
     663    47200565 : int extent_parity_compare(const void* void_a, const void* void_b)
     664             : {
     665    47200565 :         const struct snapraid_extent* arg_a = void_a;
     666    47200565 :         const struct snapraid_extent* arg_b = void_b;
     667             : 
     668    47200565 :         if (arg_a->parity_pos < arg_b->parity_pos)
     669      965147 :                 return -1;
     670    46235418 :         if (arg_a->parity_pos > arg_b->parity_pos)
     671    46197186 :                 return 1;
     672             : 
     673       38232 :         return 0;
     674             : }
     675             : 
     676    47526066 : int extent_file_compare(const void* void_a, const void* void_b)
     677             : {
     678    47526066 :         const struct snapraid_extent* arg_a = void_a;
     679    47526066 :         const struct snapraid_extent* arg_b = void_b;
     680             : 
     681    47526066 :         if (arg_a->file < arg_b->file)
     682     1068937 :                 return -1;
     683    46457129 :         if (arg_a->file > arg_b->file)
     684    46385771 :                 return 1;
     685             : 
     686       71358 :         if (arg_a->file_pos < arg_b->file_pos)
     687         194 :                 return -1;
     688       71164 :         if (arg_a->file_pos > arg_b->file_pos)
     689       32932 :                 return 1;
     690             : 
     691       38232 :         return 0;
     692             : }
     693             : 
     694      126838 : struct snapraid_link* link_alloc(const char* sub, const char* linkto, unsigned link_flag)
     695             : {
     696             :         struct snapraid_link* slink;
     697             : 
     698      126838 :         slink = malloc_nofail(sizeof(struct snapraid_link));
     699      126838 :         slink->sub = strdup_nofail(sub);
     700      126838 :         slink->linkto = strdup_nofail(linkto);
     701      126838 :         slink->flag = link_flag;
     702             : 
     703      126838 :         return slink;
     704             : }
     705             : 
     706      125242 : void link_free(struct snapraid_link* slink)
     707             : {
     708      125242 :         free(slink->sub);
     709      125242 :         free(slink->linkto);
     710      125242 :         free(slink);
     711      125242 : }
     712             : 
     713       37307 : int link_name_compare_to_arg(const void* void_arg, const void* void_data)
     714             : {
     715       37307 :         const char* arg = void_arg;
     716       37307 :         const struct snapraid_link* slink = void_data;
     717             : 
     718       37307 :         return strcmp(arg, slink->sub);
     719             : }
     720             : 
     721        3337 : int link_alpha_compare(const void* void_a, const void* void_b)
     722             : {
     723        3337 :         const struct snapraid_link* slink_a = void_a;
     724        3337 :         const struct snapraid_link* slink_b = void_b;
     725             : 
     726        3337 :         return strcmp(slink_a->sub, slink_b->sub);
     727             : }
     728             : 
     729         872 : struct snapraid_dir* dir_alloc(const char* sub)
     730             : {
     731             :         struct snapraid_dir* dir;
     732             : 
     733         872 :         dir = malloc_nofail(sizeof(struct snapraid_dir));
     734         872 :         dir->sub = strdup_nofail(sub);
     735         872 :         dir->flag = 0;
     736             : 
     737         872 :         return dir;
     738             : }
     739             : 
     740         864 : void dir_free(struct snapraid_dir* dir)
     741             : {
     742         864 :         free(dir->sub);
     743         864 :         free(dir);
     744         864 : }
     745             : 
     746         285 : int dir_name_compare(const void* void_arg, const void* void_data)
     747             : {
     748         285 :         const char* arg = void_arg;
     749         285 :         const struct snapraid_dir* dir = void_data;
     750             : 
     751         285 :         return strcmp(arg, dir->sub);
     752             : }
     753             : 
     754        1771 : struct snapraid_disk* disk_alloc(const char* name, const char* dir, uint64_t dev, const char* uuid, int skip_access)
     755             : {
     756             :         struct snapraid_disk* disk;
     757             :         unsigned i;
     758             : 
     759        1771 :         disk = malloc_nofail(sizeof(struct snapraid_disk));
     760        1771 :         pathcpy(disk->name, sizeof(disk->name), name);
     761        1771 :         pathimport(disk->dir, sizeof(disk->dir), dir);
     762        1771 :         pathcpy(disk->uuid, sizeof(disk->uuid), uuid);
     763             : 
     764             :         /* ensure that the dir terminate with "/" if it isn't empty */
     765        1771 :         pathslash(disk->dir, sizeof(disk->dir));
     766             : 
     767             : #if HAVE_THREAD
     768        1771 :         thread_mutex_init(&disk->fs_mutex);
     769             : #endif
     770             : 
     771        1771 :         disk->smartctl[0] = 0;
     772        8855 :         for (i = 0; i < SMART_IGNORE_MAX; ++i)
     773        7084 :                 disk->smartignore[i] = 0;
     774        1771 :         disk->fstype[0] = 0;
     775        1771 :         disk->fslabel[0] = 0;
     776        1771 :         disk->device = dev;
     777        1771 :         disk->tick = 0;
     778        1771 :         disk->cached_blocks = 0;
     779        1771 :         disk->progress_file = 0;
     780        1771 :         disk->total_blocks = 0;
     781        1771 :         disk->free_blocks = 0;
     782        1771 :         disk->first_free_block = 0;
     783        1771 :         disk->has_volatile_inodes = 0;
     784        1771 :         disk->has_volatile_hardlinks = 0;
     785        1771 :         disk->has_unreliable_physical = 0;
     786        1771 :         disk->has_different_uuid = 0;
     787        1771 :         disk->has_unsupported_uuid = *uuid == 0; /* empty UUID means unsupported */
     788        1771 :         disk->had_empty_uuid = 0;
     789        1771 :         disk->mapping_idx = -1;
     790        1771 :         disk->skip_access = skip_access;
     791        1771 :         tommy_list_init(&disk->filelist);
     792        1771 :         tommy_list_init(&disk->deletedlist);
     793        1771 :         tommy_hashdyn_init(&disk->inodeset);
     794        1771 :         tommy_hashdyn_init(&disk->pathset);
     795        1771 :         tommy_hashdyn_init(&disk->stampset);
     796        1771 :         tommy_list_init(&disk->linklist);
     797        1771 :         tommy_hashdyn_init(&disk->linkset);
     798        1771 :         tommy_list_init(&disk->dirlist);
     799        1771 :         tommy_hashdyn_init(&disk->dirset);
     800        1771 :         tommy_tree_init(&disk->fs_parity, extent_parity_compare);
     801        1771 :         tommy_tree_init(&disk->fs_file, extent_file_compare);
     802        1771 :         disk->fs_last = 0;
     803             : 
     804        1771 :         return disk;
     805             : }
     806             : 
     807        1711 : void disk_free(struct snapraid_disk* disk)
     808             : {
     809        1711 :         tommy_list_foreach(&disk->filelist, (tommy_foreach_func*)file_free);
     810        1711 :         tommy_list_foreach(&disk->deletedlist, (tommy_foreach_func*)file_free);
     811        1711 :         tommy_tree_foreach(&disk->fs_file, (tommy_foreach_func*)extent_free);
     812        1711 :         tommy_hashdyn_done(&disk->inodeset);
     813        1711 :         tommy_hashdyn_done(&disk->pathset);
     814        1711 :         tommy_hashdyn_done(&disk->stampset);
     815        1711 :         tommy_list_foreach(&disk->linklist, (tommy_foreach_func*)link_free);
     816        1711 :         tommy_hashdyn_done(&disk->linkset);
     817        1711 :         tommy_list_foreach(&disk->dirlist, (tommy_foreach_func*)dir_free);
     818        1711 :         tommy_hashdyn_done(&disk->dirset);
     819             : 
     820             : #if HAVE_THREAD
     821        1711 :         thread_mutex_destroy(&disk->fs_mutex);
     822             : #endif
     823             : 
     824        1711 :         free(disk);
     825        1711 : }
     826             : 
     827    66940175 : static inline void fs_lock(struct snapraid_disk* disk)
     828             : {
     829             : #if HAVE_THREAD
     830    66940175 :         thread_mutex_lock(&disk->fs_mutex);
     831             : #else
     832             :         (void)disk;
     833             : #endif
     834    66940175 : }
     835             : 
     836    66940175 : static inline void fs_unlock(struct snapraid_disk* disk)
     837             : {
     838             : #if HAVE_THREAD
     839    66940175 :         thread_mutex_unlock(&disk->fs_mutex);
     840             : #else
     841             :         (void)disk;
     842             : #endif
     843    66940175 : }
     844             : 
     845             : struct extent_disk_empty {
     846             :         block_off_t blockmax;
     847             : };
     848             : 
     849             : /**
     850             :  * Compare the extent if inside the specified blockmax.
     851             :  */
     852          12 : static int extent_disk_empty_compare_unlock(const void* void_a, const void* void_b)
     853             : {
     854          12 :         const struct extent_disk_empty* arg_a = void_a;
     855          12 :         const struct snapraid_extent* arg_b = void_b;
     856             : 
     857             :         /* if the block is inside the specified blockmax, it's found */
     858          12 :         if (arg_a->blockmax > arg_b->parity_pos)
     859           7 :                 return 0;
     860             : 
     861             :         /* otherwise search for a smaller one */
     862           5 :         return -1;
     863             : }
     864             : 
     865         966 : int fs_is_empty(struct snapraid_disk* disk, block_off_t blockmax)
     866             : {
     867         966 :         struct extent_disk_empty arg = { blockmax };
     868             : 
     869             :         /* if there is an element, it's not empty */
     870             :         /* even if links and dirs have no block allocation */
     871         966 :         if (!tommy_list_empty(&disk->filelist))
     872         867 :                 return 0;
     873          99 :         if (!tommy_list_empty(&disk->linklist))
     874           0 :                 return 0;
     875          99 :         if (!tommy_list_empty(&disk->dirlist))
     876           0 :                 return 0;
     877             : 
     878          99 :         fs_lock(disk);
     879             : 
     880             :         /* search for any extent inside blockmax */
     881          99 :         if (tommy_tree_search_compare(&disk->fs_parity, extent_disk_empty_compare_unlock, &arg) != 0) {
     882           7 :                 fs_unlock(disk);
     883           7 :                 return 0;
     884             :         }
     885             : 
     886             :         /* finally, it's empty */
     887          92 :         fs_unlock(disk);
     888          92 :         return 1;
     889             : }
     890             : 
     891             : struct extent_disk_size {
     892             :         block_off_t size;
     893             : };
     894             : 
     895             : /**
     896             :  * Compare the extent by highest parity position.
     897             :  *
     898             :  * The maximum parity position is stored as size.
     899             :  */
     900       54911 : static int extent_disk_size_compare_unlock(const void* void_a, const void* void_b)
     901             : {
     902       54911 :         struct extent_disk_size* arg_a = (void*)void_a;
     903       54911 :         const struct snapraid_extent* arg_b = void_b;
     904             : 
     905             :         /* get the maximum size */
     906       54911 :         if (arg_a->size < arg_b->parity_pos + arg_b->count)
     907       54911 :                 arg_a->size = arg_b->parity_pos + arg_b->count;
     908             : 
     909             :         /* search always for a bigger one */
     910       54911 :         return 1;
     911             : }
     912             : 
     913        4693 : block_off_t fs_size(struct snapraid_disk* disk)
     914             : {
     915        4693 :         struct extent_disk_size arg = { 0 };
     916             : 
     917        4693 :         fs_lock(disk);
     918             : 
     919        4693 :         tommy_tree_search_compare(&disk->fs_parity, extent_disk_size_compare_unlock, &arg);
     920             : 
     921        4693 :         fs_unlock(disk);
     922             : 
     923        4693 :         return arg.size;
     924             : }
     925             : 
     926             : struct extent_check {
     927             :         const struct snapraid_extent* prev;
     928             :         int result;
     929             : };
     930             : 
     931     7063193 : static void extent_parity_check_foreach_unlock(void* void_arg, void* void_obj)
     932             : {
     933     7063193 :         struct extent_check* arg = void_arg;
     934     7063193 :         const struct snapraid_extent* obj = void_obj;
     935     7063193 :         const struct snapraid_extent* prev = arg->prev;
     936             : 
     937             :         /* set the next previous block */
     938     7063193 :         arg->prev = obj;
     939             : 
     940             :         /* stop reporting if too many errors */
     941     7063193 :         if (arg->result > 100) {
     942             :                 /* LCOV_EXCL_START */
     943             :                 return;
     944             :                 /* LCOV_EXCL_STOP */
     945             :         }
     946             : 
     947     7063193 :         if (obj->count == 0) {
     948             :                 /* LCOV_EXCL_START */
     949             :                 log_fatal(EINTERNAL, "Internal inconsistency: Parity count zero for file '%s' at '%u'\n",
     950             :                         obj->file->sub, obj->parity_pos);
     951             :                 ++arg->result;
     952             :                 return;
     953             :                 /* LCOV_EXCL_STOP */
     954             :         }
     955             : 
     956             :         /* check only if there is a previous block */
     957     7063193 :         if (!prev)
     958        2951 :                 return;
     959             : 
     960             :         /* check the order */
     961     7060242 :         if (prev->parity_pos >= obj->parity_pos) {
     962             :                 /* LCOV_EXCL_START */
     963             :                 log_fatal(EINTERNAL, "Internal inconsistency: Parity order for files '%s' at '%u:%u' and '%s' at '%u:%u'\n",
     964             :                         prev->file->sub, prev->parity_pos, prev->count, obj->file->sub, obj->parity_pos, obj->count);
     965             :                 ++arg->result;
     966             :                 return;
     967             :                 /* LCOV_EXCL_STOP */
     968             :         }
     969             : 
     970             :         /* check that the extents don't overlap */
     971     7060242 :         if (prev->parity_pos + prev->count > obj->parity_pos) {
     972             :                 /* LCOV_EXCL_START */
     973             :                 log_fatal(EINTERNAL, "Internal inconsistency: Parity overlap for files '%s' at '%u:%u' and '%s' at '%u:%u'\n",
     974             :                         prev->file->sub, prev->parity_pos, prev->count, obj->file->sub, obj->parity_pos, obj->count);
     975             :                 ++arg->result;
     976             :                 return;
     977             :                 /* LCOV_EXCL_STOP */
     978             :         }
     979             : }
     980             : 
     981     7063193 : static void extent_file_check_foreach_unlock(void* void_arg, void* void_obj)
     982             : {
     983     7063193 :         struct extent_check* arg = void_arg;
     984     7063193 :         const struct snapraid_extent* obj = void_obj;
     985     7063193 :         const struct snapraid_extent* prev = arg->prev;
     986             : 
     987             :         /* set the next previous block */
     988     7063193 :         arg->prev = obj;
     989             : 
     990             :         /* stop reporting if too many errors */
     991     7063193 :         if (arg->result > 100) {
     992             :                 /* LCOV_EXCL_START */
     993             :                 return;
     994             :                 /* LCOV_EXCL_STOP */
     995             :         }
     996             : 
     997     7063193 :         if (obj->count == 0) {
     998             :                 /* LCOV_EXCL_START */
     999             :                 log_fatal(EINTERNAL, "Internal inconsistency: File count zero for file '%s' at '%u'\n",
    1000             :                         obj->file->sub, obj->file_pos);
    1001             :                 ++arg->result;
    1002             :                 return;
    1003             :                 /* LCOV_EXCL_STOP */
    1004             :         }
    1005             : 
    1006             :         /* note that for deleted files, some extents may be missing */
    1007             : 
    1008             :         /* if the files are different */
    1009     7063193 :         if (!prev || prev->file != obj->file) {
    1010     7013948 :                 if (prev != 0) {
    1011     7010997 :                         if (file_flag_has(prev->file, FILE_IS_DELETED)) {
    1012             :                                 /* check that the extent doesn't overflow the file */
    1013       58267 :                                 if (prev->file_pos + prev->count > prev->file->blockmax) {
    1014             :                                         /* LCOV_EXCL_START */
    1015             :                                         log_fatal(EINTERNAL, "Internal inconsistency: Delete end for file '%s' at '%u:%u' overflowing size '%u'\n",
    1016             :                                                 prev->file->sub, prev->file_pos, prev->count, prev->file->blockmax);
    1017             :                                         ++arg->result;
    1018             :                                         return;
    1019             :                                         /* LCOV_EXCL_STOP */
    1020             :                                 }
    1021             :                         } else {
    1022             :                                 /* check that the extent ends the file */
    1023     6952730 :                                 if (prev->file_pos + prev->count != prev->file->blockmax) {
    1024             :                                         /* LCOV_EXCL_START */
    1025             :                                         log_fatal(EINTERNAL, "Internal inconsistency: File end for file '%s' at '%u:%u' instead of size '%u'\n",
    1026             :                                                 prev->file->sub, prev->file_pos, prev->count, prev->file->blockmax);
    1027             :                                         ++arg->result;
    1028             :                                         return;
    1029             :                                         /* LCOV_EXCL_STOP */
    1030             :                                 }
    1031             :                         }
    1032             :                 }
    1033             : 
    1034     7013948 :                 if (file_flag_has(obj->file, FILE_IS_DELETED)) {
    1035             :                         /* check that the extent doesn't overflow the file */
    1036       58344 :                         if (obj->file_pos + obj->count > obj->file->blockmax) {
    1037             :                                 /* LCOV_EXCL_START */
    1038             :                                 log_fatal(EINTERNAL, "Internal inconsistency: Delete start for file '%s' at '%u:%u' overflowing size '%u'\n",
    1039             :                                         obj->file->sub, obj->file_pos, obj->count, obj->file->blockmax);
    1040             :                                 ++arg->result;
    1041             :                                 return;
    1042             :                                 /* LCOV_EXCL_STOP */
    1043             :                         }
    1044             :                 } else {
    1045             :                         /* check that the extent starts the file */
    1046     6955604 :                         if (obj->file_pos != 0) {
    1047             :                                 /* LCOV_EXCL_START */
    1048             :                                 log_fatal(EINTERNAL, "Internal inconsistency: File start for file '%s' at '%u:%u'\n",
    1049             :                                         obj->file->sub, obj->file_pos, obj->count);
    1050             :                                 ++arg->result;
    1051             :                                 return;
    1052             :                                 /* LCOV_EXCL_STOP */
    1053             :                         }
    1054             :                 }
    1055             :         } else {
    1056             :                 /* check the order */
    1057       49245 :                 if (prev->file_pos >= obj->file_pos) {
    1058             :                         /* LCOV_EXCL_START */
    1059             :                         log_fatal(EINTERNAL, "Internal inconsistency: File order for file '%s' at '%u:%u' and at '%u:%u'\n",
    1060             :                                 prev->file->sub, prev->file_pos, prev->count, obj->file_pos, obj->count);
    1061             :                         ++arg->result;
    1062             :                         return;
    1063             :                         /* LCOV_EXCL_STOP */
    1064             :                 }
    1065             : 
    1066       49245 :                 if (file_flag_has(obj->file, FILE_IS_DELETED)) {
    1067             :                         /* check that the extents don't overlap */
    1068          27 :                         if (prev->file_pos + prev->count > obj->file_pos) {
    1069             :                                 /* LCOV_EXCL_START */
    1070             :                                 log_fatal(EINTERNAL, "Internal inconsistency: Delete sequence for file '%s' at '%u:%u' and at '%u:%u'\n",
    1071             :                                         prev->file->sub, prev->file_pos, prev->count, obj->file_pos, obj->count);
    1072             :                                 ++arg->result;
    1073             :                                 return;
    1074             :                                 /* LCOV_EXCL_STOP */
    1075             :                         }
    1076             :                 } else {
    1077             :                         /* check that the extents are sequential */
    1078       49218 :                         if (prev->file_pos + prev->count != obj->file_pos) {
    1079             :                                 /* LCOV_EXCL_START */
    1080             :                                 log_fatal(EINTERNAL, "Internal inconsistency: File sequence for file '%s' at '%u:%u' and at '%u:%u'\n",
    1081             :                                         prev->file->sub, prev->file_pos, prev->count, obj->file_pos, obj->count);
    1082             :                                 ++arg->result;
    1083             :                                 return;
    1084             :                                 /* LCOV_EXCL_STOP */
    1085             :                         }
    1086             :                 }
    1087             :         }
    1088             : }
    1089             : 
    1090        3216 : int fs_check(struct snapraid_disk* disk)
    1091             : {
    1092             :         struct extent_check arg;
    1093             : 
    1094             :         /* error count starts from 0 */
    1095        3216 :         arg.result = 0;
    1096             : 
    1097        3216 :         fs_lock(disk);
    1098             : 
    1099             :         /* check parity sequence */
    1100        3216 :         arg.prev = 0;
    1101        3216 :         tommy_tree_foreach_arg(&disk->fs_parity, extent_parity_check_foreach_unlock, &arg);
    1102             : 
    1103             :         /* check file sequence */
    1104        3216 :         arg.prev = 0;
    1105        3216 :         tommy_tree_foreach_arg(&disk->fs_file, extent_file_check_foreach_unlock, &arg);
    1106             : 
    1107        3216 :         fs_unlock(disk);
    1108             : 
    1109        3216 :         if (arg.result != 0)
    1110           0 :                 return -1;
    1111             : 
    1112        3216 :         return 0;
    1113             : }
    1114             : 
    1115             : struct extent_parity_inside {
    1116             :         block_off_t parity_pos;
    1117             : };
    1118             : 
    1119             : /**
    1120             :  * Compare the extent if containing the specified parity position.
    1121             :  */
    1122   165880879 : static int extent_parity_inside_compare_unlock(const void* void_a, const void* void_b)
    1123             : {
    1124   165880879 :         const struct extent_parity_inside* arg_a = void_a;
    1125   165880879 :         const struct snapraid_extent* arg_b = void_b;
    1126             : 
    1127   165880879 :         if (arg_a->parity_pos < arg_b->parity_pos)
    1128    59883776 :                 return -1;
    1129   105997103 :         if (arg_a->parity_pos >= arg_b->parity_pos + arg_b->count)
    1130    93017085 :                 return 1;
    1131             : 
    1132    12980018 :         return 0;
    1133             : }
    1134             : 
    1135             : /**
    1136             :  * Search the extent at the specified parity position.
    1137             :  * The search is optimized for sequential accesses.
    1138             :  * \return If not found return 0
    1139             :  */
    1140    51736981 : static struct snapraid_extent* fs_par2extent_get_unlock(struct snapraid_disk* disk, struct snapraid_extent** fs_last, block_off_t parity_pos)
    1141             : {
    1142             :         struct snapraid_extent* extent;
    1143             : 
    1144             :         /* check if the last accessed extent matches */
    1145    51736981 :         if (*fs_last
    1146    51702097 :                 && parity_pos >= (*fs_last)->parity_pos
    1147    51012448 :                 && parity_pos < (*fs_last)->parity_pos + (*fs_last)->count
    1148             :         ) {
    1149    36627814 :                 extent = *fs_last;
    1150             :         } else {
    1151    15109167 :                 struct extent_parity_inside arg = { parity_pos };
    1152    15109167 :                 extent = tommy_tree_search_compare(&disk->fs_parity, extent_parity_inside_compare_unlock, &arg);
    1153             :         }
    1154             : 
    1155    51736981 :         if (!extent)
    1156     2129149 :                 return 0;
    1157             : 
    1158             :         /* store the last accessed extent */
    1159    49607832 :         *fs_last = extent;
    1160             : 
    1161    49607832 :         return extent;
    1162             : }
    1163             : 
    1164             : struct extent_file_inside {
    1165             :         struct snapraid_file* file;
    1166             :         block_off_t file_pos;
    1167             : };
    1168             : 
    1169             : /**
    1170             :  * Compare the extent if containing the specified file position.
    1171             :  */
    1172    23525049 : static int extent_file_inside_compare_unlock(const void* void_a, const void* void_b)
    1173             : {
    1174    23525049 :         const struct extent_file_inside* arg_a = void_a;
    1175    23525049 :         const struct snapraid_extent* arg_b = void_b;
    1176             : 
    1177    23525049 :         if (arg_a->file < arg_b->file)
    1178     9891875 :                 return -1;
    1179    13633174 :         if (arg_a->file > arg_b->file)
    1180    11392855 :                 return 1;
    1181             : 
    1182     2240319 :         if (arg_a->file_pos < arg_b->file_pos)
    1183        8208 :                 return -1;
    1184     2232111 :         if (arg_a->file_pos >= arg_b->file_pos + arg_b->count)
    1185        9293 :                 return 1;
    1186             : 
    1187     2222818 :         return 0;
    1188             : }
    1189             : 
    1190             : /**
    1191             :  * Search the extent at the specified file position.
    1192             :  * The search is optimized for sequential accesses.
    1193             :  * \return If not found return 0
    1194             :  */
    1195    11254424 : static struct snapraid_extent* fs_file2extent_get_unlock(struct snapraid_disk* disk, struct snapraid_extent** fs_last, struct snapraid_file* file, block_off_t file_pos)
    1196             : {
    1197             :         struct snapraid_extent* extent;
    1198             : 
    1199             :         /* check if the last accessed extent matches */
    1200    11254424 :         if (*fs_last
    1201    11232125 :                 && file == (*fs_last)->file
    1202     9046832 :                 && file_pos >= (*fs_last)->file_pos
    1203     9046832 :                 && file_pos < (*fs_last)->file_pos + (*fs_last)->count
    1204             :         ) {
    1205     9031598 :                 extent = *fs_last;
    1206             :         } else {
    1207     2222826 :                 struct extent_file_inside arg = { file, file_pos };
    1208     2222826 :                 extent = tommy_tree_search_compare(&disk->fs_file, extent_file_inside_compare_unlock, &arg);
    1209             :         }
    1210             : 
    1211    11254424 :         if (!extent)
    1212           8 :                 return 0;
    1213             : 
    1214             :         /* store the last accessed extent */
    1215    11254416 :         *fs_last = extent;
    1216             : 
    1217    11254416 :         return extent;
    1218             : }
    1219             : 
    1220    51633082 : struct snapraid_file* fs_par2file_find(struct snapraid_disk* disk, block_off_t parity_pos, block_off_t* file_pos)
    1221             : {
    1222             :         struct snapraid_extent* extent;
    1223             :         struct snapraid_file* file;
    1224             : 
    1225    51633082 :         fs_lock(disk);
    1226             : 
    1227    51633082 :         extent = fs_par2extent_get_unlock(disk, &disk->fs_last, parity_pos);
    1228             : 
    1229    51633082 :         if (!extent) {
    1230     2129149 :                 fs_unlock(disk);
    1231     2129149 :                 return 0;
    1232             :         }
    1233             : 
    1234    49503933 :         if (file_pos)
    1235    49293807 :                 *file_pos = extent->file_pos + (parity_pos - extent->parity_pos);
    1236             : 
    1237    49503933 :         file = extent->file;
    1238             : 
    1239    49503933 :         fs_unlock(disk);
    1240    49503933 :         return file;
    1241             : }
    1242             : 
    1243     5433481 : block_off_t fs_file2par_find(struct snapraid_disk* disk, struct snapraid_file* file, block_off_t file_pos)
    1244             : {
    1245             :         struct snapraid_extent* extent;
    1246             :         block_off_t ret;
    1247             : 
    1248     5433481 :         fs_lock(disk);
    1249             : 
    1250     5433481 :         extent = fs_file2extent_get_unlock(disk, &disk->fs_last, file, file_pos);
    1251     5433481 :         if (!extent) {
    1252           8 :                 fs_unlock(disk);
    1253           8 :                 return POS_NULL;
    1254             :         }
    1255             : 
    1256     5433473 :         ret = extent->parity_pos + (file_pos - extent->file_pos);
    1257             : 
    1258     5433473 :         fs_unlock(disk);
    1259     5433473 :         return ret;
    1260             : }
    1261             : 
    1262     9761705 : void fs_allocate(struct snapraid_disk* disk, block_off_t parity_pos, struct snapraid_file* file, block_off_t file_pos)
    1263             : {
    1264             :         struct snapraid_extent* extent;
    1265             :         struct snapraid_extent* parity_extent;
    1266             :         struct snapraid_extent* file_extent;
    1267             : 
    1268     9761705 :         fs_lock(disk);
    1269             : 
    1270     9761705 :         if (file_pos > 0) {
    1271             :                 /* search an existing extent for the previous file_pos */
    1272     5820943 :                 extent = fs_file2extent_get_unlock(disk, &disk->fs_last, file, file_pos - 1);
    1273             : 
    1274     5820943 :                 if (extent != 0 && parity_pos == extent->parity_pos + extent->count) {
    1275             :                         /* ensure that we are extending the extent at the end */
    1276     5792451 :                         if (file_pos != extent->file_pos + extent->count) {
    1277             :                                 /* LCOV_EXCL_START */
    1278             :                                 log_fatal(EINTERNAL, "Internal inconsistency: Allocating file '%s' at position '%u/%u' in the middle of extent '%u:%u' in disk '%s'\n", file->sub, file_pos, file->blockmax, extent->file_pos, extent->count, disk->name);
    1279             :                                 os_abort();
    1280             :                                 /* LCOV_EXCL_STOP */
    1281             :                         }
    1282             : 
    1283             :                         /* extend the existing extent */
    1284     5792451 :                         ++extent->count;
    1285             : 
    1286     5792451 :                         fs_unlock(disk);
    1287     5792451 :                         return;
    1288             :                 }
    1289             :         }
    1290             : 
    1291             :         /* a extent doesn't exist, and we have to create a new one */
    1292     3969254 :         extent = extent_alloc(parity_pos, file, file_pos, 1);
    1293             : 
    1294             :         /* insert the extent in the trees */
    1295     3969254 :         parity_extent = tommy_tree_insert(&disk->fs_parity, &extent->parity_node, extent);
    1296     3969254 :         file_extent = tommy_tree_insert(&disk->fs_file, &extent->file_node, extent);
    1297             : 
    1298     3969254 :         if (parity_extent != extent || file_extent != extent) {
    1299             :                 /* LCOV_EXCL_START */
    1300             :                 log_fatal(EINTERNAL, "Internal inconsistency: Allocating file '%s' at position '%u/%u' for existing extent '%u:%u' in disk '%s'\n", file->sub, file_pos, file->blockmax, extent->file_pos, extent->count, disk->name);
    1301             :                 os_abort();
    1302             :                 /* LCOV_EXCL_STOP */
    1303             :         }
    1304             : 
    1305             :         /* store the last accessed extent */
    1306     3969254 :         disk->fs_last = extent;
    1307             : 
    1308     3969254 :         fs_unlock(disk);
    1309             : }
    1310             : 
    1311      103899 : void fs_deallocate(struct snapraid_disk* disk, block_off_t parity_pos)
    1312             : {
    1313             :         struct snapraid_extent* extent;
    1314             :         struct snapraid_extent* second_extent;
    1315             :         struct snapraid_extent* parity_extent;
    1316             :         struct snapraid_extent* file_extent;
    1317             :         block_off_t first_count, second_count;
    1318             : 
    1319      103899 :         fs_lock(disk);
    1320             : 
    1321      103899 :         extent = fs_par2extent_get_unlock(disk, &disk->fs_last, parity_pos);
    1322      103899 :         if (!extent) {
    1323             :                 /* LCOV_EXCL_START */
    1324             :                 log_fatal(EINTERNAL, "Internal inconsistency: Deallocating parity position '%u' for not existing extent in disk '%s'\n", parity_pos, disk->name);
    1325             :                 os_abort();
    1326             :                 /* LCOV_EXCL_STOP */
    1327             :         }
    1328             : 
    1329             :         /* if it's the only block of the extent, delete it */
    1330      103899 :         if (extent->count == 1) {
    1331             :                 /* remove from the trees */
    1332       38232 :                 tommy_tree_remove(&disk->fs_parity, extent);
    1333       38232 :                 tommy_tree_remove(&disk->fs_file, extent);
    1334             : 
    1335             :                 /* deallocate */
    1336       38232 :                 extent_free(extent);
    1337             : 
    1338             :                 /* clear the last accessed extent */
    1339       38232 :                 disk->fs_last = 0;
    1340             : 
    1341       38232 :                 fs_unlock(disk);
    1342       38232 :                 return;
    1343             :         }
    1344             : 
    1345             :         /* if it's at the start of the extent, shrink the extent */
    1346       65667 :         if (parity_pos == extent->parity_pos) {
    1347       65664 :                 ++extent->parity_pos;
    1348       65664 :                 ++extent->file_pos;
    1349       65664 :                 --extent->count;
    1350             : 
    1351       65664 :                 fs_unlock(disk);
    1352       65664 :                 return;
    1353             :         }
    1354             : 
    1355             :         /* if it's at the end of the extent, shrink the extent */
    1356           3 :         if (parity_pos == extent->parity_pos + extent->count - 1) {
    1357           1 :                 --extent->count;
    1358             : 
    1359           1 :                 fs_unlock(disk);
    1360           1 :                 return;
    1361             :         }
    1362             : 
    1363             :         /* otherwise it's in the middle */
    1364           2 :         first_count = parity_pos - extent->parity_pos;
    1365           2 :         second_count = extent->count - first_count - 1;
    1366             : 
    1367             :         /* adjust the first extent */
    1368           2 :         extent->count = first_count;
    1369             : 
    1370             :         /* allocate the second extent */
    1371           2 :         second_extent = extent_alloc(extent->parity_pos + first_count + 1, extent->file, extent->file_pos + first_count + 1, second_count);
    1372             : 
    1373             :         /* insert the extent in the trees */
    1374           2 :         parity_extent = tommy_tree_insert(&disk->fs_parity, &second_extent->parity_node, second_extent);
    1375           2 :         file_extent = tommy_tree_insert(&disk->fs_file, &second_extent->file_node, second_extent);
    1376             : 
    1377           2 :         if (parity_extent != second_extent || file_extent != second_extent) {
    1378             :                 /* LCOV_EXCL_START */
    1379             :                 log_fatal(EINTERNAL, "Internal inconsistency: Deallocating parity position '%u' for splitting extent '%u:%u' in disk '%s'\n", parity_pos, second_extent->file_pos, second_extent->count, disk->name);
    1380             :                 os_abort();
    1381             :                 /* LCOV_EXCL_STOP */
    1382             :         }
    1383             : 
    1384             :         /* store the last accessed extent */
    1385           2 :         disk->fs_last = second_extent;
    1386             : 
    1387           2 :         fs_unlock(disk);
    1388             : }
    1389             : 
    1390    58244824 : struct snapraid_block* fs_file2block_get(struct snapraid_file* file, block_off_t file_pos)
    1391             : {
    1392    58244824 :         if (file_pos >= file->blockmax) {
    1393             :                 /* LCOV_EXCL_START */
    1394             :                 log_fatal(EINTERNAL, "Internal inconsistency: Dereferencing file '%s' at position '%u/%u'\n", file->sub, file_pos, file->blockmax);
    1395             :                 os_abort();
    1396             :                 /* LCOV_EXCL_STOP */
    1397             :         }
    1398             : 
    1399    58244824 :         return file_block(file, file_pos);
    1400             : }
    1401             : 
    1402    39757320 : struct snapraid_block* fs_par2block_find(struct snapraid_disk* disk, block_off_t parity_pos)
    1403             : {
    1404             :         struct snapraid_file* file;
    1405             :         block_off_t file_pos;
    1406             : 
    1407    39757320 :         file = fs_par2file_find(disk, parity_pos, &file_pos);
    1408    39757320 :         if (file == 0)
    1409     2129149 :                 return BLOCK_NULL;
    1410             : 
    1411    37628171 :         return fs_file2block_get(file, file_pos);
    1412             : }
    1413             : 
    1414        1675 : struct snapraid_map* map_alloc(const char* name, unsigned position, block_off_t total_blocks, block_off_t free_blocks, const char* uuid)
    1415             : {
    1416             :         struct snapraid_map* map;
    1417             : 
    1418        1675 :         map = malloc_nofail(sizeof(struct snapraid_map));
    1419        1675 :         pathcpy(map->name, sizeof(map->name), name);
    1420        1675 :         map->position = position;
    1421        1675 :         map->total_blocks = total_blocks;
    1422        1675 :         map->free_blocks = free_blocks;
    1423        1675 :         pathcpy(map->uuid, sizeof(map->uuid), uuid);
    1424             : 
    1425        1675 :         return map;
    1426             : }
    1427             : 
    1428        1621 : void map_free(struct snapraid_map* map)
    1429             : {
    1430        1621 :         free(map);
    1431        1621 : }
    1432             : 
    1433             : /****************************************************************************/
    1434             : /* bucket */
    1435             : 
    1436         603 : static int bucket_cmp(const void* void_a, const void* void_b)
    1437             : {
    1438         603 :         const struct snapraid_bucket* a = void_a;
    1439         603 :         const struct snapraid_bucket* b = void_b;
    1440         603 :         if (a->time_at < b->time_at)
    1441         381 :                 return -1;
    1442         222 :         if (a->time_at > b->time_at)
    1443         222 :                 return 1;
    1444           0 :         return 0;
    1445             : }
    1446             : 
    1447         786 : void bucket_free(struct snapraid_bucket* bucket)
    1448             : {
    1449         786 :         free(bucket);
    1450         786 : }
    1451             : 
    1452       56928 : void bucket_insert(tommy_hashdyn* bucket_hash, time_t time_at, block_off_t count, int justsynced)
    1453             : {
    1454       56928 :         tommy_hash_t hash = tommy_inthash_u64(time_at);
    1455       56928 :         tommy_node* i = tommy_hashdyn_bucket(bucket_hash, hash);
    1456       58082 :         while (i) {
    1457       57286 :                 struct snapraid_bucket* entry = i->data;
    1458       57286 :                 if (entry->time_at == time_at) {
    1459       56132 :                         if (justsynced)
    1460       28005 :                                 entry->count_justsynced += count;
    1461             :                         else
    1462       28127 :                                 entry->count_scrubbed += count;
    1463       56132 :                         return;
    1464             :                 }
    1465        1154 :                 i = i->next;
    1466             :         }
    1467             : 
    1468         796 :         struct snapraid_bucket* entry = malloc_nofail(sizeof(struct snapraid_bucket));
    1469         796 :         entry->time_at = time_at;
    1470         796 :         if (justsynced) {
    1471         723 :                 entry->count_justsynced = count;
    1472         723 :                 entry->count_scrubbed = 0;
    1473             :         } else {
    1474          73 :                 entry->count_justsynced = 0;
    1475          73 :                 entry->count_scrubbed = count;
    1476             :         }
    1477             : 
    1478         796 :         tommy_hashdyn_insert(bucket_hash, &entry->node, entry, hash);
    1479             : }
    1480             : 
    1481         433 : void bucket_to_list(tommy_hashdyn* bucket_hash, tommy_list* bucket_list, block_off_t* bucketcount)
    1482             : {
    1483             :         /* clear previous list */
    1484         433 :         tommy_list_foreach(bucket_list, (tommy_foreach_func*)bucket_free);
    1485         433 :         tommy_list_init(bucket_list);
    1486             : 
    1487         433 :         log_tag("list:bucket_begin\n");
    1488             : 
    1489         433 :         log_tag("bucket:count:%" PRIu64 "\n", (uint64_t)tommy_hashdyn_count(bucket_hash));
    1490             : 
    1491         433 :         tommy_hashdyn_to_list(bucket_hash, bucket_list);
    1492             : 
    1493         433 :         tommy_list_sort(bucket_list, bucket_cmp);
    1494             : 
    1495         433 :         block_off_t count = 0;
    1496        1229 :         for (tommy_node* i = tommy_list_head(bucket_list); i != 0; i = i->next) {
    1497         796 :                 struct snapraid_bucket* entry = i->data;
    1498         796 :                 count += entry->count_scrubbed + entry->count_justsynced;
    1499         796 :                 log_tag("bucket:entry:%" PRIu64 ":%u:%u\n", (uint64_t)entry->time_at, entry->count_scrubbed, entry->count_justsynced);
    1500             :         }
    1501             : 
    1502         433 :         log_tag("bucket:block_count:%u\n", count);
    1503             : 
    1504         433 :         log_tag("list:bucket_end\n");
    1505             : 
    1506         433 :         *bucketcount = count;
    1507         433 : }
    1508             : 
    1509             : /****************************************************************************/
    1510             : /* format */
    1511             : 
    1512             : int FMT_MODE = FMT_FILE;
    1513             : 
    1514             : /**
    1515             :  * Format a file path for poll reference
    1516             :  */
    1517           6 : const char* fmt_poll(const struct snapraid_disk* disk, const char* str, char* buffer)
    1518             : {
    1519             :         (void)disk;
    1520           6 :         return esc_shell(str, buffer);
    1521             : }
    1522             : 
    1523             : /**
    1524             :  * Format a path name for terminal reference
    1525             :  */
    1526      381949 : const char* fmt_term(const struct snapraid_disk* disk, const char* str, char* buffer)
    1527             : {
    1528             :         const char* out[3];
    1529             : 
    1530      381949 :         switch (FMT_MODE) {
    1531      357884 :         case FMT_FILE :
    1532             :         default :
    1533      357884 :                 return esc_shell(str, buffer);
    1534       12030 :         case FMT_DISK :
    1535       12030 :                 out[0] = disk->name;
    1536       12030 :                 out[1] = ":";
    1537       12030 :                 out[2] = str;
    1538       12030 :                 return esc_shell_multi(out, 3, buffer);
    1539       12035 :         case FMT_PATH :
    1540       12035 :                 out[0] = disk->dir;
    1541       12035 :                 out[1] = str;
    1542       12035 :                 return esc_shell_multi(out, 2, buffer);
    1543             :         }
    1544             : }
    1545             : 
    1546          15 : const char* fmt_size(uint64_t size, char* buffer, size_t buffer_size)
    1547             : {
    1548          15 :         if (size >= 10UL * TERA)
    1549           0 :                 snprintf(buffer, buffer_size, "%" PRIu64 " T", size / TERA);
    1550          15 :         else if (size >= 10UL * GIGA)
    1551           0 :                 snprintf(buffer, buffer_size, "%" PRIu64 " G", size / GIGA);
    1552          15 :         else if (size >= 10UL * MEGA)
    1553           1 :                 snprintf(buffer, buffer_size, "%" PRIu64 " M", size / MEGA);
    1554          14 :         else if (size >= 10UL * KILO)
    1555          13 :                 snprintf(buffer, buffer_size, "%" PRIu64 " k", size / KILO);
    1556             :         else
    1557           1 :                 snprintf(buffer, buffer_size, "%" PRIu64 " ", size);
    1558             : 
    1559          15 :         return buffer;
    1560             : }
    1561             : 

Generated by: LCOV version 1.0