LCOV - code coverage report
Current view: top level - cmdline - elem.h (source / functions) Hit Total Coverage
Test: lcov.info Lines: 121 122 99.2 %
Date: 2016-01-16 Functions: 41 41 100.0 %

          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             : #ifndef __ELEM_H
      19             : #define __ELEM_H
      20             : 
      21             : #include "util.h"
      22             : #include "support.h"
      23             : #include "tommyds/tommyhash.h"
      24             : #include "tommyds/tommylist.h"
      25             : #include "tommyds/tommytree.h"
      26             : #include "tommyds/tommyhashdyn.h"
      27             : #include "tommyds/tommyarray.h"
      28             : #include "tommyds/tommyarrayblk.h"
      29             : #include "tommyds/tommyarrayblkof.h"
      30             : 
      31             : /****************************************************************************/
      32             : /* snapraid */
      33             : 
      34             : /**
      35             :  * Max UUID length.
      36             :  */
      37             : #define UUID_MAX 128
      38             : 
      39             : /**
      40             :  * Invalid position.
      41             :  */
      42             : #define POS_INVALID ((block_off_t)-1)
      43             : 
      44             : /**
      45             :  * Basic block position type.
      46             :  * With 32 bits and 128k blocks you can address 256 TB.
      47             :  */
      48             : typedef uint32_t block_off_t;
      49             : 
      50             : /**
      51             :  * Basic data position type.
      52             :  * It's signed as file size and offset are usually signed.
      53             :  */
      54             : typedef int64_t data_off_t;
      55             : 
      56             : /**
      57             :  * Content file specification.
      58             :  */
      59             : struct snapraid_content {
      60             :         char content[PATH_MAX]; /**< Path of the content file. */
      61             :         uint64_t device; /**< Device identifier. */
      62             :         void* context; /**< Context used for multithread operations. */
      63             :         tommy_node node; /**< Next node in the list. */
      64             : };
      65             : 
      66             : /**
      67             :  * Filter for paths.
      68             :  */
      69             : struct snapraid_filter {
      70             :         char pattern[PATH_MAX]; /**< Filter pattern. */
      71             :         int is_disk; /**< If the pattern is a disk one. */
      72             :         int is_path; /**< If the pattern is only for the complete path. */
      73             :         int is_dir; /**< If the pattern is only for dir. */
      74             :         int direction; /**< If it's an inclusion (=1) or an exclusion (=-1). */
      75             :         tommy_node node; /**< Next node in the list. */
      76             : };
      77             : 
      78             : /**
      79             :  * Block pointer used to represent unused blocks.
      80             :  */
      81             : #define BLOCK_EMPTY 0
      82             : 
      83             : /**
      84             :  * This block is an empty one.
      85             :  * Note that an empty block is represent with ::BLOCK_EMPTY.
      86             :  */
      87             : #define BLOCK_STATE_EMPTY 0
      88             : 
      89             : /**
      90             :  * The block has both the hash and the parity computed.
      91             :  * This is the normal state of a saved block.
      92             :  *
      93             :  * The block hash field IS set.
      94             :  * The parity for this disk is updated.
      95             :  */
      96             : #define BLOCK_STATE_BLK 1
      97             : 
      98             : /**
      99             :  * The block is new and not yet hashed.
     100             :  * This happens when a new block overwrite a just removed block, or an empty space.
     101             :  *
     102             :  * The block hash field MAY be set and it represents the hash of the OLD data.
     103             :  * The hash may be also INVALID or ZERO.
     104             :  *
     105             :  * If the OLD block was empty, the hash is set to the special ZERO value.
     106             :  * If the OLD block was lost, the hash is set to the special INVALID value.
     107             :  *
     108             :  * The parity for this disk is not updated, but it contains the old data referenced by the hash.
     109             :  *
     110             :  * If the state is read from an incomplete sync, we don't really know if the hash is referring at the
     111             :  * data used to compute the parity, because the sync process was interrupted at an unknown point,
     112             :  * and the parity may or may not be updated.
     113             :  *
     114             :  * For this reason we clear all such hashes when reading the state from an incomplete sync before
     115             :  * starting a new sync, because sync is affected by such hashes, as sync updates the parity, only
     116             :  * if the new data read for CHG blocks has a mismatching hash.
     117             :  * Clearing is done setting the ::clear_past_hash flag before reading the state.
     118             :  * No clearing is done in other commands, as check and fix are instead able to work with unsynced
     119             :  * hashes, and scrub ignores CHG/DELETED blocks.
     120             :  */
     121             : #define BLOCK_STATE_CHG 2
     122             : 
     123             : /**
     124             :  * The block is new and hashed.
     125             :  * This happens when a new block overwrite a just removed block, or an empty space.
     126             :  *
     127             :  * Note that when the file copy heuristic is enabled, the REP blocks may be set
     128             :  * using this heuristic, meaning that the hash may be wrong.
     129             :  *
     130             :  * For this reason, when the ::force_nocopy flag is enabled in sync, we convert all the REP blocks
     131             :  * to CHG, invalidating the stored hash.
     132             :  * Clearing is done setting the ::clear_past_hash flag before reading the state.
     133             :  * No clearing is done in other commands, as they don't stop the process like in sync
     134             :  * when there is a false silent error.
     135             :  *
     136             :  * The block hash field IS set, and it represents the hash of the new data.
     137             :  * The parity for this disk is not updated.
     138             :  */
     139             : #define BLOCK_STATE_REP 3
     140             : 
     141             : /**
     142             :  * This block is a deleted one.
     143             :  * This happens when a file is deleted.
     144             :  *
     145             :  * The block hash field IS set, and it represents the hash of the previous data,
     146             :  * but only if it's different by all 0.
     147             :  * The parity for this disk is not updated, but it contains the old data referenced by the hash.
     148             :  *
     149             :  * If the state is read from an incomplete sync, we don't really know if the hash is referring at the
     150             :  * data used to compute the parity, because the sync process was interrupted at an unknown point,
     151             :  * and the parity may or may not be updated.
     152             :  *
     153             :  * A now the sync process is not affected by DELETED hash, so clearing won't be really needed,
     154             :  * but considering that we have to do it for CHG blocks, we do it also for DELETED ones,
     155             :  * clearing all the past hashes.
     156             :  * Clearing is done setting the ::clear_past_hash flag before reading the state.
     157             :  * No clearing is done in other commands, as check and fix are instead able to work with unsynced
     158             :  * hashes, and scrub ignores CHG/DELETED blocks.
     159             :  */
     160             : #define BLOCK_STATE_DELETED 4
     161             : 
     162             : /**
     163             :  * Block of a file.
     164             :  */
     165             : struct snapraid_block {
     166             :         unsigned char hash[HASH_SIZE]; /**< Hash of the block. */
     167             :         unsigned char state; /**< State of the block. */
     168             : };
     169             : 
     170             : /**
     171             :  * If a file is present in the disk.
     172             :  * It's used only in scan to detect present and missing files.
     173             :  */
     174             : #define FILE_IS_PRESENT 0x01
     175             : 
     176             : /**
     177             :  * If it's an excluded file from the processing.
     178             :  * It's used in both check and fix to mark files to exclude from the processing.
     179             :  */
     180             : #define FILE_IS_EXCLUDED 0x02
     181             : 
     182             : /**
     183             :  * If a fix was attempted but it failed.
     184             :  * It's used only in fix to mark that some data is unrecoverable.
     185             :  */
     186             : #define FILE_IS_DAMAGED 0x04
     187             : 
     188             : /**
     189             :  * If a fix was done.
     190             :  * It's used only in fix to mark that some data was recovered.
     191             :  */
     192             : #define FILE_IS_FIXED 0x08
     193             : 
     194             : /**
     195             :  * If the file was originally missing, and it was created in the fix process.
     196             :  * It's used only in fix to mark files recovered from scratch,
     197             :  * meaning that they don't have any previous content.
     198             :  * This is important because it means that deleting them, you are not going
     199             :  * to lose something that cannot be recovered.
     200             :  * Note that excluded files won't ever get this flag.
     201             :  */
     202             : #define FILE_IS_CREATED 0x10
     203             : 
     204             : /**
     205             :  * If the file has completed its processing, meaning that it won't be opened anymore.
     206             :  * It's used only in fix to mark when we finish processing one file.
     207             :  * Note that excluded files won't ever get this flag.
     208             :  */
     209             : #define FILE_IS_FINISHED 0x20
     210             : 
     211             : /**
     212             :  * If the file hash was obtained from a file copy
     213             :  * identified by the same name, size and stamp.
     214             :  */
     215             : #define FILE_IS_COPY 0x40
     216             : 
     217             : /**
     218             :  * If the file was opened.
     219             :  * It's used in fix to detect if it's the first time a file is opened.
     220             :  */
     221             : #define FILE_IS_OPENED 0x80
     222             : 
     223             : /**
     224             :  * If the file is modified from the latest sync.
     225             :  * It's used in fix to store if the state of the file before being modified.
     226             :  */
     227             : #define FILE_IS_UNSYNCED 0x100
     228             : 
     229             : /**
     230             :  * If the file is without inode.
     231             :  * It could happen in file-system where inodes are not persistent,
     232             :  * or when restoring a full disk with "fix".
     233             :  * In such cases we have to clear any stored duplicate inode.
     234             :  * After the scan process completes, no file should have this flag set.
     235             :  */
     236             : #define FILE_IS_WITHOUT_INODE 0x200
     237             : 
     238             : /**
     239             :  * The file is deleted.
     240             :  * This happens when a file is deleted from the array,
     241             :  * but it's keep inside the parity until the next sync.
     242             :  *
     243             :  * During the file-system check we needs this information,
     244             :  * because deleted files may be present only partially.
     245             :  */
     246             : #define FILE_IS_DELETED 0x400
     247             : 
     248             : /**
     249             :  * The file is missing.
     250             :  * This happens in fix/check when a file is cannot be opened,
     251             :  * and marking it as such prevents to retry to open it again.
     252             :  */
     253             : #define FILE_IS_MISSING 0x800
     254             : 
     255             : #define FILE_IS_HARDLINK 0x1000 /**< If it's an hardlink. */
     256             : #define FILE_IS_SYMLINK 0x2000 /**< If it's a file symlink. */
     257             : #define FILE_IS_SYMDIR 0x4000 /**< If it's a dir symlink for Windows. Not yet supported. */
     258             : #define FILE_IS_JUNCTION 0x8000 /**< If it's a junction for Windows. Not yet supported. */
     259             : #define FILE_IS_LINK_MASK 0xF000 /**< Mask for link type. */
     260             : 
     261             : /**
     262             :  * File.
     263             :  */
     264             : struct snapraid_file {
     265             :         int64_t mtime_sec; /**< Modification time. */
     266             :         uint64_t inode; /**< Inode. */
     267             :         uint64_t physical; /**< Physical offset of the file. */
     268             :         data_off_t size; /**< Size of the file. */
     269             :         struct snapraid_block* blockvec; /**< All the blocks of the file. */
     270             :         int mtime_nsec; /**< Modification time nanoseconds. In the range 0 <= x < 1,000,000,000, or STAT_NSEC_INVALID if not present. */
     271             :         block_off_t blockmax; /**< Number of blocks. */
     272             :         unsigned flag; /**< FILE_IS_* flags. */
     273             :         char* sub; /**< Sub path of the file. Without the disk dir. The disk is implicit. */
     274             : 
     275             :         /* nodes for data structures */
     276             :         tommy_node nodelist;
     277             :         tommy_hashdyn_node nodeset;
     278             :         tommy_hashdyn_node pathset;
     279             :         tommy_hashdyn_node stampset;
     280             : };
     281             : 
     282             : /**
     283             :  * Symbolic Link.
     284             :  */
     285             : struct snapraid_link {
     286             :         unsigned flag; /**< FILE_IS_* flags. */
     287             :         char* sub; /**< Sub path of the file. Without the disk dir. The disk is implicit. */
     288             :         char* linkto; /**< Link to. */
     289             : 
     290             :         /* nodes for data structures */
     291             :         tommy_node nodelist;
     292             :         tommy_hashdyn_node nodeset;
     293             : };
     294             : 
     295             : /**
     296             :  * Dir.
     297             :  */
     298             : struct snapraid_dir {
     299             :         unsigned flag; /**< FILE_IS_* flags. */
     300             :         char* sub; /**< Sub path of the file. Without the disk dir. The disk is implicit. */
     301             : 
     302             :         /* nodes for data structures */
     303             :         tommy_node nodelist;
     304             :         tommy_hashdyn_node nodeset;
     305             : };
     306             : 
     307             : /**
     308             :  * Chunk.
     309             :  *
     310             :  * A chunk represents a fragment of a file mapped into the parity.
     311             :  */
     312             : struct snapraid_chunk {
     313             :         struct snapraid_file* file; /**< File containing this chunk. */
     314             :         block_off_t parity_pos; /**< Parity position. */
     315             :         block_off_t file_pos; /**< Position in the file. */
     316             :         block_off_t count; /**< Number of sequential blocks in the file and parity. */
     317             :         tommy_tree_node parity_node; /**< Tree sorted by <parity_pos>. */
     318             :         tommy_tree_node file_node; /**< Tree sorter by <file,file_pos>. */
     319             : };
     320             : 
     321             : /**
     322             :  * Disk.
     323             :  */
     324             : struct snapraid_disk {
     325             :         char name[PATH_MAX]; /**< Name of the disk. */
     326             :         char dir[PATH_MAX]; /**< Mount point of the disk. It always terminates with /. */
     327             :         char smartctl[PATH_MAX]; /**< Custom command for smartctl. Empty means auto. */
     328             :         char uuid[UUID_MAX]; /**< UUID of the disk. */
     329             : 
     330             :         uint64_t device; /**< Device identifier. */
     331             :         uint64_t tick; /**< Usage time of the disk. */
     332             :         block_off_t total_blocks; /**< Number of total blocks. */
     333             :         block_off_t free_blocks; /**< Number of free blocks at the last sync. */
     334             : 
     335             :         /**
     336             :          * First free searching block.
     337             :          * Note that it doesn't necessarily point at the first free block,
     338             :          * but it just tell you that no free block is present before this position.
     339             :          */
     340             :         block_off_t first_free_block;
     341             : 
     342             :         int has_volatile_inodes; /**< If the underline file-system has not persistent inodes. */
     343             :         int has_unreliable_physical; /**< If the physical offset of files has duplicates. */
     344             :         int has_different_uuid; /**< If the disk has a different UUID, meaning that it is not the same file-system. */
     345             :         int has_unsupported_uuid; /**< If the disk doesn't report UUID, meaning it's not supported. */
     346             :         int had_empty_uuid; /**< If the disk had an empty UUID, meaning that it's a new disk. */
     347             :         int mapping_idx; /**< Index in the mapping vector. Used only as buffer when writing the content file. */
     348             :         int skip_access; /**< If the disk is inaccessible and it should be skipped. */
     349             : 
     350             :         /**
     351             :          * Mapping of chunks in the parity.
     352             :          * Sorted by <parity_pos> and by <file,file_pos>
     353             :          */
     354             :         tommy_tree fs_parity;
     355             :         tommy_tree fs_file;
     356             : 
     357             :         /**
     358             :          * Last chunk we accessed.
     359             :          * It's used to optimize access of sequential blocks.
     360             :          */
     361             :         struct snapraid_chunk* fs_last;
     362             : 
     363             :         /**
     364             :          * List of all the snapraid_file for the disk.
     365             :          */
     366             :         tommy_list filelist;
     367             : 
     368             :         /**
     369             :          * List of all the deleted file for the disk.
     370             :          *
     371             :          * These files are kept allocated, because the blocks are still referenced in
     372             :          * the ::blockarr.
     373             :          */
     374             :         tommy_list deletedlist;
     375             : 
     376             :         tommy_hashdyn inodeset; /**< Hashtable by inode of all the files. */
     377             :         tommy_hashdyn pathset; /**< Hashtable by path of all the files. */
     378             :         tommy_hashdyn stampset; /**< Hashtable by stamp (size and time) of all the files. */
     379             :         tommy_list linklist; /**< List of all the links. */
     380             :         tommy_hashdyn linkset; /**< Hashtable by name of all the links. */
     381             :         tommy_list dirlist; /**< List of all the dirs. */
     382             :         tommy_hashdyn dirset; /**< Hashtable by name of all the dirs. */
     383             : 
     384             :         /* nodes for data structures */
     385             :         tommy_node node;
     386             : };
     387             : 
     388             : /**
     389             :  * Disk mapping.
     390             :  */
     391             : struct snapraid_map {
     392             :         char name[PATH_MAX]; /**< Name of the disk. */
     393             :         char uuid[UUID_MAX]; /**< UUID of the disk. Empty if unknown. */
     394             :         block_off_t total_blocks; /**< Number of total blocks. */
     395             :         block_off_t free_blocks; /**< Number of free blocks at last 'sync'. */
     396             :         unsigned position; /**< Position of the disk in the parity. */
     397             : 
     398             :         /* nodes for data structures */
     399             :         tommy_node node;
     400             : };
     401             : 
     402             : /**
     403             :  * Parity.
     404             :  */
     405             : struct snapraid_parity {
     406             :         char path[PATH_MAX]; /**< Path of the parity file. */
     407             :         char uuid[UUID_MAX]; /**< UUID of the disk. Empty if unknown. */
     408             :         char smartctl[PATH_MAX]; /**< Custom command for smartctl. Empty means auto. */
     409             :         uint64_t device; /**< Device identifier of the parity. */
     410             :         block_off_t total_blocks; /**< Number of total blocks. */
     411             :         block_off_t free_blocks; /**< Number of free blocks at the last sync. */
     412             :         int skip_access; /**< If the disk is inaccessible and it should be skipped. */
     413             :         int is_excluded; /**< If the parity is excluded by disk filter. */
     414             : 
     415             :         /**
     416             :          * Cumulative time used for parity disks.
     417             :          */
     418             :         uint64_t tick;
     419             : };
     420             : 
     421             : /**
     422             :  * Info.
     423             :  */
     424             : typedef uint32_t snapraid_info;
     425             : 
     426             : /**
     427             :  * Allocate a content.
     428             :  */
     429             : struct snapraid_content* content_alloc(const char* path, uint64_t dev);
     430             : 
     431             : /**
     432             :  * Deallocate a content.
     433             :  */
     434             : void content_free(struct snapraid_content* content);
     435             : 
     436             : /**
     437             :  * Allocate a filter pattern for files and directories.
     438             :  */
     439             : struct snapraid_filter* filter_alloc_file(int is_include, const char* pattern);
     440             : 
     441             : /**
     442             :  * Allocate a filter pattern for disks.
     443             :  */
     444             : struct snapraid_filter* filter_alloc_disk(int is_include, const char* pattern);
     445             : 
     446             : /**
     447             :  * Deallocate an exclusion.
     448             :  */
     449             : void filter_free(struct snapraid_filter* filter);
     450             : 
     451             : /**
     452             :  * Filter type description.
     453             :  */
     454             : const char* filter_type(struct snapraid_filter* filter, char* out, size_t out_size);
     455             : 
     456             : /**
     457             :  * Filter hidden files.
     458             :  * Return !=0 if it matches and it should be excluded.
     459             :  */
     460     3114995 : static inline int filter_hidden(int enable, struct dirent* dd)
     461             : {
     462     3114995 :         if (enable && dirent_hidden(dd)) {
     463           0 :                 return 1; /* filter out */
     464             :         }
     465             : 
     466     3114995 :         return 0;
     467             : }
     468             : 
     469             : /**
     470             :  * Filter a path using a list of filters.
     471             :  * For each element of the path all the filters are applied, until the first one that matches.
     472             :  * Return !=0 if it should be excluded.
     473             :  */
     474             : int filter_path(tommy_list* filterlist, struct snapraid_filter** reason, const char* disk, const char* sub);
     475             : 
     476             : /**
     477             :  * Filter a file/link/dir if missing.
     478             :  * This call imply a disk check for the file presence.
     479             :  * Return !=0 if the file is present and it should be excluded.
     480             :  */
     481             : int filter_existence(int filter_missing, const char* dir, const char* sub);
     482             : 
     483             : /**
     484             :  * Filter a file if bad.
     485             :  * Return !=0 if the file is correct and it should be excluded.
     486             :  */
     487             : int filter_correctness(int filter_error, tommy_arrayblkof* infoarr, struct snapraid_disk* disk, struct snapraid_file* file);
     488             : 
     489             : /**
     490             :  * Filter a dir using a list of filters.
     491             :  * For each element of the path all the filters are applied, until the first one that matches.
     492             :  * Return !=0 if should be excluded.
     493             :  */
     494             : int filter_dir(tommy_list* filterlist, struct snapraid_filter** reason, const char* disk, const char* sub);
     495             : 
     496             : /**
     497             :  * Filter a path if it's a content file.
     498             :  * Return !=0 if should be excluded.
     499             :  */
     500             : int filter_content(tommy_list* contentlist, const char* path);
     501             : 
     502             : /**
     503             :  * Check if the specified hash is invalid.
     504             :  *
     505             :  * An invalid hash is represented with all bytes at 0x00.
     506             :  */
     507      759088 : static inline int hash_is_invalid(const unsigned char* hash)
     508             : {
     509             :         unsigned i;
     510             : 
     511     1178874 :         for (i = 0; i < HASH_SIZE; ++i)
     512     1152810 :                 if (hash[i] != 0x00)
     513      733024 :                         return 0;
     514             : 
     515       26064 :         return 1;
     516             : }
     517             : 
     518             : /**
     519             :  * Check if the specified hash represent the zero block.
     520             :  *
     521             :  * A zero hash is represented with all bytes at 0xFF.
     522             :  */
     523      760550 : static inline int hash_is_zero(const unsigned char* hash)
     524             : {
     525             :         unsigned i;
     526             : 
     527      994989 :         for (i = 0; i < HASH_SIZE; ++i)
     528      980520 :                 if (hash[i] != 0xFF)
     529      746081 :                         return 0;
     530             : 
     531       14469 :         return 1;
     532             : }
     533             : 
     534             : /**
     535             :  * Check if the specified hash is a real hash.
     536             :  */
     537       44212 : static inline int hash_is_real(const unsigned char* hash)
     538             : {
     539       44212 :         return !hash_is_zero(hash) && !hash_is_invalid(hash);
     540             : }
     541             : 
     542             : /**
     543             :  * Set the hash to the special INVALID value.
     544             :  */
     545     9042162 : static inline void hash_invalid_set(unsigned char* hash)
     546             : {
     547     9042162 :         memset(hash, 0x00, HASH_SIZE);
     548     9042162 : }
     549             : 
     550             : /**
     551             :  * Set the hash to the special ZERO value.
     552             :  */
     553      115543 : static inline void hash_zero_set(unsigned char* hash)
     554             : {
     555      115543 :         memset(hash, 0xFF, HASH_SIZE);
     556      115543 : }
     557             : 
     558             : /**
     559             :  * Get the state of the block.
     560             :  *
     561             :  * For this function, it's allowed to pass a NULL block
     562             :  * pointer than results in the BLOCK_STATE_EMPTY state.
     563             :  */
     564    49924589 : static inline unsigned block_state_get(const struct snapraid_block* block)
     565             : {
     566    49924589 :         if (block == BLOCK_EMPTY)
     567     1624550 :                 return BLOCK_STATE_EMPTY;
     568             : 
     569    48300039 :         return block->state;
     570             : }
     571             : 
     572             : /**
     573             :  * Set the state of the block.
     574             :  */
     575    18644176 : static inline void block_state_set(struct snapraid_block* block, unsigned state)
     576             : {
     577    18644176 :         block->state = state;
     578    18644176 : }
     579             : 
     580             : /**
     581             :  * Check if the specified block has an updated hash.
     582             :  *
     583             :  * Note that EMPTY / CHG / DELETED return 0.
     584             :  */
     585     2074724 : static inline int block_has_updated_hash(const struct snapraid_block* block)
     586             : {
     587     2074724 :         unsigned state = block_state_get(block);
     588             : 
     589     2074724 :         return state == BLOCK_STATE_BLK || state == BLOCK_STATE_REP;
     590             : }
     591             : 
     592             : /**
     593             :  * Check if the specified block has a past hash,
     594             :  * i.e. the hash of the data that it's now overwritten or lost.
     595             :  *
     596             :  * Note that EMPTY / BLK / REP return 0.
     597             :  */
     598     2403226 : static inline int block_has_past_hash(const struct snapraid_block* block)
     599             : {
     600     2403226 :         unsigned state = block_state_get(block);
     601             : 
     602     2403226 :         return state == BLOCK_STATE_CHG || state == BLOCK_STATE_DELETED;
     603             : }
     604             : 
     605             : /**
     606             :  * Check if the specified block is part of a file.
     607             :  *
     608             :  * Note that EMPTY / DELETED return 0.
     609             :  */
     610    13254717 : static inline int block_has_file(const struct snapraid_block* block)
     611             : {
     612    13254717 :         unsigned state = block_state_get(block);
     613             : 
     614    13254717 :         return state == BLOCK_STATE_BLK
     615    13254717 :                || state == BLOCK_STATE_CHG || state == BLOCK_STATE_REP;
     616             : }
     617             : 
     618             : /**
     619             :  * Check if the block has an invalid parity than needs to be updated.
     620             :  *
     621             :  * Note that EMPTY / BLK return 0.
     622             :  */
     623    11847406 : static inline int block_has_invalid_parity(const struct snapraid_block* block)
     624             : {
     625    11847406 :         unsigned state = block_state_get(block);
     626             : 
     627    11847406 :         return state == BLOCK_STATE_DELETED
     628    11847406 :                || state == BLOCK_STATE_CHG || state == BLOCK_STATE_REP;
     629             : }
     630             : 
     631             : /**
     632             :  * Check if the block is part of a file with valid parity.
     633             :  *
     634             :  * Note that anything different than BLK return 0.
     635             :  */
     636      257940 : static inline int block_has_file_and_valid_parity(const struct snapraid_block* block)
     637             : {
     638      257940 :         unsigned state = block_state_get(block);
     639             : 
     640      257940 :         return state == BLOCK_STATE_BLK;
     641             : }
     642             : 
     643    33676848 : static inline int file_flag_has(const struct snapraid_file* file, unsigned mask)
     644             : {
     645    33676848 :         return (file->flag & mask) == mask;
     646             : }
     647             : 
     648     5249321 : static inline void file_flag_set(struct snapraid_file* file, unsigned mask)
     649             : {
     650     5249321 :         file->flag |= mask;
     651     5249321 : }
     652             : 
     653       19577 : static inline void file_flag_clear(struct snapraid_file* file, unsigned mask)
     654             : {
     655       19577 :         file->flag &= ~mask;
     656       19577 : }
     657             : 
     658             : /**
     659             :  * Allocate a file.
     660             :  */
     661             : 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);
     662             : 
     663             : /**
     664             :  * Duplicate a file.
     665             :  */
     666             : struct snapraid_file* file_dup(struct snapraid_file* copy);
     667             : 
     668             : /**
     669             :  * Deallocate a file.
     670             :  */
     671             : void file_free(struct snapraid_file* file);
     672             : 
     673             : /**
     674             :  * Rename a file.
     675             :  */
     676             : void file_rename(struct snapraid_file* file, const char* sub);
     677             : 
     678             : /**
     679             :  * Copy a file.
     680             :  */
     681             : void file_copy(struct snapraid_file* src_file, struct snapraid_file* dest_file);
     682             : 
     683             : /**
     684             :  * Return the name of the file, without the dir.
     685             :  */
     686             : const char* file_name(const struct snapraid_file* file);
     687             : 
     688             : /**
     689             :  * Check if the block is the last in the file.
     690             :  */
     691             : int file_block_is_last(struct snapraid_file* file, block_off_t file_pos);
     692             : 
     693             : /**
     694             :  * Get the size in bytes of the block.
     695             :  * If it's the last block of a file it could be less than block_size.
     696             :  */
     697             : unsigned file_block_size(struct snapraid_file* file, block_off_t file_pos, unsigned block_size);
     698             : 
     699             : /**
     700             :  * Compare a file with an inode.
     701             :  */
     702             : int file_inode_compare_to_arg(const void* void_arg, const void* void_data);
     703             : 
     704             : /**
     705             :  * Compare files by inode.
     706             :  */
     707             : int file_inode_compare(const void* void_a, const void* void_b);
     708             : 
     709             : /**
     710             :  * Compare files by path.
     711             :  */
     712             : int file_path_compare(const void* void_a, const void* void_b);
     713             : 
     714             : /**
     715             :  * Compare files by physical address.
     716             :  */
     717             : int file_physical_compare(const void* void_a, const void* void_b);
     718             : 
     719             : /**
     720             :  * Compute the hash of a file inode.
     721             :  */
     722     4923043 : static inline tommy_uint32_t file_inode_hash(uint64_t inode)
     723             : {
     724     4923043 :         return (tommy_uint32_t)tommy_inthash_u64(inode);
     725             : }
     726             : 
     727             : /**
     728             :  * Compare a file with a path.
     729             :  */
     730             : int file_path_compare_to_arg(const void* void_arg, const void* void_data);
     731             : 
     732             : /**
     733             :  * Compare a file with another file for name, stamp, and both.
     734             :  */
     735             : int file_name_compare(const void* void_a, const void* void_b);
     736             : int file_stamp_compare(const void* void_a, const void* void_b);
     737             : int file_namestamp_compare(const void* void_a, const void* void_b);
     738             : int file_pathstamp_compare(const void* void_a, const void* void_b);
     739             : 
     740             : /**
     741             :  * Compute the hash of a file path.
     742             :  */
     743     3814698 : static inline tommy_uint32_t file_path_hash(const char* sub)
     744             : {
     745     3814698 :         return tommy_hash_u32(0, sub, strlen(sub));
     746             : }
     747             : 
     748             : /**
     749             :  * Compute the hash of a file stamp.
     750             :  */
     751     6199048 : static inline tommy_uint32_t file_stamp_hash(data_off_t size, int64_t mtime_sec, int mtime_nsec)
     752             : {
     753     6199048 :         return tommy_inthash_u32((tommy_uint32_t)size ^ tommy_inthash_u32(mtime_sec ^ tommy_inthash_u32(mtime_nsec)));
     754             : }
     755             : 
     756             : /**
     757             :  * Allocate a chunk.
     758             :  */
     759             : struct snapraid_chunk* chunk_alloc(block_off_t parity_pos, struct snapraid_file* file, block_off_t file_pos, block_off_t count);
     760             : 
     761             : /**
     762             :  * Deallocate a chunk.
     763             :  */
     764             : void chunk_free(struct snapraid_chunk* chunk);
     765             : 
     766             : /**
     767             :  * Compare chunk by parity position.
     768             :  */
     769             : int chunk_parity_compare(const void* void_a, const void* void_b);
     770             : 
     771             : /**
     772             :  * Compare chunk by file and file position.
     773             :  */
     774             : int chunk_file_compare(const void* void_a, const void* void_b);
     775             : 
     776      205549 : static inline int link_flag_has(const struct snapraid_link* slink, unsigned mask)
     777             : {
     778      205549 :         return (slink->flag & mask) == mask;
     779             : }
     780             : 
     781       37571 : static inline void link_flag_set(struct snapraid_link* slink, unsigned mask)
     782             : {
     783       37571 :         slink->flag |= mask;
     784       37571 : }
     785             : 
     786             : static inline void link_flag_clear(struct snapraid_link* slink, unsigned mask)
     787             : {
     788             :         slink->flag &= ~mask;
     789             : }
     790             : 
     791           6 : static inline void link_flag_let(struct snapraid_link* slink, unsigned flag, unsigned mask)
     792             : {
     793           6 :         slink->flag &= ~mask;
     794           6 :         slink->flag |= flag & mask;
     795           6 : }
     796             : 
     797       89093 : static inline unsigned link_flag_get(struct snapraid_link* slink, unsigned mask)
     798             : {
     799       89093 :         return slink->flag & mask;
     800             : }
     801             : 
     802             : /**
     803             :  * Allocate a link.
     804             :  */
     805             : struct snapraid_link* link_alloc(const char* name, const char* slink, unsigned link_flag);
     806             : 
     807             : /**
     808             :  * Deallocate a link.
     809             :  */
     810             : void link_free(struct snapraid_link* slink);
     811             : 
     812             : /**
     813             :  * Compare a link with a name.
     814             :  */
     815             : int link_name_compare_to_arg(const void* void_arg, const void* void_data);
     816             : 
     817             : /**
     818             :  * Compare links by path.
     819             :  */
     820             : int link_alpha_compare(const void* void_a, const void* void_b);
     821             : 
     822             : /**
     823             :  * Compute the hash of a link name.
     824             :  */
     825      151170 : static inline tommy_uint32_t link_name_hash(const char* name)
     826             : {
     827      151170 :         return tommy_hash_u32(0, name, strlen(name));
     828             : }
     829             : 
     830         900 : static inline int dir_flag_has(const struct snapraid_dir* dir, unsigned mask)
     831             : {
     832         900 :         return (dir->flag & mask) == mask;
     833             : }
     834             : 
     835         247 : static inline void dir_flag_set(struct snapraid_dir* dir, unsigned mask)
     836             : {
     837         247 :         dir->flag |= mask;
     838         247 : }
     839             : 
     840             : static inline void dir_flag_clear(struct snapraid_dir* dir, unsigned mask)
     841             : {
     842             :         dir->flag &= ~mask;
     843             : }
     844             : 
     845             : /**
     846             :  * Allocate a dir.
     847             :  */
     848             : struct snapraid_dir* dir_alloc(const char* name);
     849             : 
     850             : /**
     851             :  * Deallocate a dir.
     852             :  */
     853             : void dir_free(struct snapraid_dir* dir);
     854             : 
     855             : /**
     856             :  * Compare a dir with a name.
     857             :  */
     858             : int dir_name_compare(const void* void_arg, const void* void_data);
     859             : 
     860             : /**
     861             :  * Compute the hash of a dir name.
     862             :  */
     863        1011 : static inline tommy_uint32_t dir_name_hash(const char* name)
     864             : {
     865        1011 :         return tommy_hash_u32(0, name, strlen(name));
     866             : }
     867             : 
     868             : /**
     869             :  * Allocate a disk.
     870             :  */
     871             : struct snapraid_disk* disk_alloc(const char* name, const char* dir, uint64_t dev, const char* uuid, int skip);
     872             : 
     873             : /**
     874             :  * Deallocate a disk.
     875             :  */
     876             : void disk_free(struct snapraid_disk* disk);
     877             : 
     878             : /**
     879             :  * Get the size of the disk in blocks.
     880             :  */
     881             : block_off_t disk_size(struct snapraid_disk* disk);
     882             : 
     883             : /**
     884             :  * Check if a disk is totally empty and can be discarded from the content file.
     885             :  * A disk is empty if it doesn't contain any file, symlink, hardlink or dir
     886             :  * and without any DELETED block.
     887             :  * The blockmax is used to limit the search of DELETED block up to blockmax.
     888             :  */
     889             : int disk_is_empty(struct snapraid_disk* disk, block_off_t blockmax);
     890             : 
     891             : /**
     892             :  * Check the file-system for errors.
     893             :  * Return 0 if it's OK.
     894             :  */
     895             : int fs_check(struct snapraid_disk* disk);
     896             : 
     897             : /**
     898             :  * Allocate a parity position for the specified file position.
     899             :  *
     900             :  * After this call you can use the par2file/par2block operations
     901             :  * to query the relation.
     902             :  *
     903             :  * \note This function is NOT thread-safe as it uses the the disk cache. +
     904             :  */
     905             : void fs_allocate(struct snapraid_disk* disk, block_off_t parity_pos, struct snapraid_file* file, block_off_t file_pos);
     906             : 
     907             : /**
     908             :  * Deallocate the parity position.
     909             :  *
     910             :  * After this call the par2file/par2block operations
     911             :  * won't find anymore the parity association.
     912             :  *
     913             :  * \note This function is NOT thread-safe as it uses the the disk cache.
     914             :  */
     915             : void fs_deallocate(struct snapraid_disk* disk, block_off_t pos);
     916             : 
     917             : /**
     918             :  * Get the block from the file position.
     919             :  */
     920             : struct snapraid_block* fs_file2block_get(struct snapraid_file* file, block_off_t file_pos);
     921             : 
     922             : /**
     923             :  * Like fs_file2par_get() but thread-safe.
     924             :  */
     925             : 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);
     926             : 
     927             : /**
     928             :  * Get the file position from the parity position.
     929             :  * Return 0 if no file is using it.
     930             :  * \note This function is NOT thread-safe as it uses the the disk cache.
     931             :  */
     932    11584623 : static inline struct snapraid_file* fs_par2file_get(struct snapraid_disk* disk, block_off_t parity_pos, block_off_t* file_pos)
     933             : {
     934    11584623 :         return fs_par2file_get_ts(disk, &disk->fs_last, parity_pos, file_pos);
     935             : }
     936             : 
     937             : /**
     938             :  * Like fs_file2par_get() but thread-safe.
     939             :  */
     940             : block_off_t fs_file2par_maybe_ts(struct snapraid_disk* disk, struct snapraid_chunk** fs_last, struct snapraid_file* file, block_off_t file_pos);
     941             : 
     942             : /**
     943             :  * Get the parity position from the file position.
     944             :  * Return POS_INVALID if no parity is allocated.
     945             :  * \note This function is NOT thread-safe as it uses the the disk cache.
     946             :  */
     947      142395 : static inline block_off_t fs_file2par_maybe(struct snapraid_disk* disk, struct snapraid_file* file, block_off_t file_pos)
     948             : {
     949      142395 :         return fs_file2par_maybe_ts(disk, &disk->fs_last, file, file_pos);
     950             : }
     951             : 
     952             : /**
     953             :  * Like fs_file2par_get() but thread-safe.
     954             :  */
     955     4698533 : static inline 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)
     956             : {
     957             :         block_off_t ret;
     958             : 
     959     4698533 :         ret = fs_file2par_maybe_ts(disk, fs_last, file, file_pos);
     960     4698533 :         if (ret == POS_INVALID) {
     961             :                 /* LCOV_EXCL_START */
     962             :                 log_fatal("Internal inconsistency when resolving file '%s' at position '%u/%u' in disk '%s'\n", file->sub, file_pos, file->blockmax, disk->name);
     963             :                 os_abort();
     964             :                 /* LCOV_EXCL_STOP */
     965             :         }
     966             : 
     967     4698533 :         return ret;
     968             : }
     969             : 
     970             : /**
     971             :  * Get the parity position from the file position.
     972             :  * \note This function is NOT thread-safe as it uses the the disk cache.
     973             :  */
     974      377489 : static inline block_off_t fs_file2par_get(struct snapraid_disk* disk, struct snapraid_file* file, block_off_t file_pos)
     975             : {
     976      377489 :         return fs_file2par_get_ts(disk, &disk->fs_last, file, file_pos);
     977             : }
     978             : 
     979             : /**
     980             :  * Like fs_par2block_get() but thread-safe.
     981             :  */
     982             : struct snapraid_block* fs_par2block_get_ts(struct snapraid_disk* disk, struct snapraid_chunk** fs_last, block_off_t parity_pos);
     983             : 
     984             : /**
     985             :  * Get the block from the parity position.
     986             :  * Return BLOCK_EMPTY==0 if the block is over the end of the disk or not used.
     987             :  * \note This function is NOT thread-safe as it uses the the disk cache. 
     988             :  */
     989    22233152 : static inline struct snapraid_block* fs_par2block_get(struct snapraid_disk* disk, block_off_t parity_pos)
     990             : {
     991    22233152 :         return fs_par2block_get_ts(disk, &disk->fs_last, parity_pos);
     992             : }
     993             : 
     994             : /**
     995             :  * Allocate a disk mapping.
     996             :  * Uses uuid="" if not available.
     997             :  */
     998             : struct snapraid_map* map_alloc(const char* name, unsigned position, block_off_t total_blocks, block_off_t free_blocks, const char* uuid);
     999             : 
    1000             : /**
    1001             :  * Deallocate a disk mapping.
    1002             :  */
    1003             : void map_free(struct snapraid_map* map);
    1004             : 
    1005             : /**
    1006             :  * Mask used to store additional information in the info bits.
    1007             :  *
    1008             :  * These bits reduce the granularity of the time in the memory representation.
    1009             :  */
    1010             : #define INFO_MASK 0x7
    1011             : 
    1012             : /**
    1013             :  * Make an info.
    1014             :  */
    1015      174544 : static inline snapraid_info info_make(time_t last_access, int error, int rehash, int justsynced)
    1016             : {
    1017             :         /* clear the lowest bits as reserved for other information */
    1018      174544 :         snapraid_info info = last_access & ~INFO_MASK;
    1019             : 
    1020      174544 :         if (error != 0)
    1021        5311 :                 info |= 0x1;
    1022      174544 :         if (rehash != 0)
    1023       14426 :                 info |= 0x2;
    1024      174544 :         if (justsynced != 0)
    1025      122837 :                 info |= 0x4;
    1026      174544 :         return info;
    1027             : }
    1028             : 
    1029             : /**
    1030             :  * Extract the time information.
    1031             :  * This is the last time when the block was know to be correct.
    1032             :  * The "scrubbed" info tells if the time is referreing at the latest sync or scrub.
    1033             :  */
    1034      884150 : static inline time_t info_get_time(snapraid_info info)
    1035             : {
    1036      884150 :         return info & ~INFO_MASK;
    1037             : }
    1038             : 
    1039             : /**
    1040             :  * Extract the error information.
    1041             :  * Report if the block address had some problem.
    1042             :  */
    1043      339090 : static inline int info_get_bad(snapraid_info info)
    1044             : {
    1045      339090 :         return (info & 0x1) != 0;
    1046             : }
    1047             : 
    1048             : /**
    1049             :  * Extract the rehash information.
    1050             :  * Report if the block address is using the old hash and needs to be rehashed.
    1051             :  */
    1052     2053300 : static inline int info_get_rehash(snapraid_info info)
    1053             : {
    1054     2053300 :         return (info & 0x2) != 0;
    1055             : }
    1056             : 
    1057             : /**
    1058             :  * Extract the scrubbed information.
    1059             :  * Report if the block address was never scrubbed.
    1060             :  */
    1061       79114 : static inline int info_get_justsynced(snapraid_info info)
    1062             : {
    1063       79114 :         return (info & 0x4) != 0;
    1064             : }
    1065             : 
    1066             : /**
    1067             :  * Mark the block address as with error.
    1068             :  */
    1069        1772 : static inline snapraid_info info_set_bad(snapraid_info info)
    1070             : {
    1071        1772 :         return info | 0x1;
    1072             : }
    1073             : 
    1074             : /**
    1075             :  * Mark the block address as with rehash.
    1076             :  */
    1077        9215 : static inline snapraid_info info_set_rehash(snapraid_info info)
    1078             : {
    1079        9215 :         return info | 0x2;
    1080             : }
    1081             : 
    1082             : /**
    1083             :  * Set the info at the specified position.
    1084             :  * The position is allocated if not yet done.
    1085             :  */
    1086     1641726 : static inline void info_set(tommy_arrayblkof* array, unsigned pos, snapraid_info info)
    1087             : {
    1088     1641726 :         tommy_arrayblkof_grow(array, pos + 1);
    1089             : 
    1090     1641726 :         memcpy(tommy_arrayblkof_ref(array, pos), &info, sizeof(snapraid_info));
    1091     1641726 : }
    1092             : 
    1093             : /**
    1094             :  * Get the info at the specified position.
    1095             :  * For not allocated position, 0 is returned.
    1096             :  */
    1097     5095494 : static inline snapraid_info info_get(tommy_arrayblkof* array, unsigned pos)
    1098             : {
    1099             :         snapraid_info info;
    1100             : 
    1101     5095494 :         if (pos >= tommy_arrayblkof_size(array))
    1102      262643 :                 return 0;
    1103             : 
    1104     4832851 :         memcpy(&info, tommy_arrayblkof_ref(array, pos), sizeof(snapraid_info));
    1105             : 
    1106     4832851 :         return info;
    1107             : }
    1108             : 
    1109             : /**
    1110             :  * Compare times
    1111             :  */
    1112             : int time_compare(const void* void_a, const void* void_b);
    1113             : 
    1114             : #endif
    1115             : 

Generated by: LCOV version 1.10