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

Generated by: LCOV version 1.10