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/tommyarrayblkof.h"
29 :
30 : /****************************************************************************/
31 : /* snapraid */
32 :
33 : /**
34 : * Number of measures of the operation progress.
35 : */
36 : #define PROGRESS_MAX 100
37 :
38 : /**
39 : * Max UUID length.
40 : */
41 : #define UUID_MAX 128
42 :
43 : /**
44 : * Max filesystem type/label length
45 : */
46 :
47 : #define FSINFO_MAX 64
48 :
49 : /**
50 : * Invalid position.
51 : */
52 : #define POS_NULL ((block_off_t)-1)
53 :
54 : /**
55 : * Content file specification.
56 : */
57 : struct snapraid_content {
58 : char content[PATH_MAX]; /**< Path of the content file. */
59 : uint64_t device; /**< Device identifier. */
60 : void* context; /**< Context used for multithread operations. */
61 : tommy_node node; /**< Next node in the list. */
62 : };
63 :
64 : /**
65 : * Filter for paths.
66 : */
67 : struct snapraid_filter {
68 : char pattern[PATH_MAX]; /**< Filter pattern. */
69 : char root[PATH_MAX]; /**< If empty, it's a global pattern. If not empty, it's a local pattern that applies only to that dir. */
70 : int is_disk; /**< If the pattern is a disk one, otherwise it's a file pattern */
71 : int is_abs; /**< If the pattern is an absolute path (start with /), otherwise it's a relative one */
72 : int is_dir; /**< If the pattern is only for dir, otherwise it's only for file. */
73 : int direction; /**< If it's an inclusion (=1) or an exclusion (=-1). */
74 : tommy_node node; /**< Next node in the list. */
75 : };
76 :
77 : /**
78 : * Block pointer used to represent unused blocks.
79 : */
80 : #define BLOCK_NULL 0
81 :
82 : /**
83 : * This block is an empty one.
84 : * Note that an empty block is represent with ::BLOCK_NULL.
85 : */
86 : #define BLOCK_STATE_EMPTY 0
87 :
88 : /**
89 : * The block has both the hash and the parity computed.
90 : * This is the normal state of a saved block.
91 : *
92 : * The block hash field IS set.
93 : * The parity for this disk is updated.
94 : */
95 : #define BLOCK_STATE_BLK 1
96 :
97 : /**
98 : * The block is new and not yet hashed.
99 : * This happens when a new block overwrites a just removed block, or an empty
100 : * 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 to 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 overwrites 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 to 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 : * As 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 hash size.
164 : *
165 : * At max HASH_MAX.
166 : */
167 : extern int BLOCK_HASH_SIZE;
168 :
169 : /**
170 : * Block of a file.
171 : */
172 : struct snapraid_block {
173 : unsigned char state; /**< State of the block. */
174 :
175 : /**
176 : * Hash of the block.
177 : *
178 : * The effective stored size is BLOCK_HASH_SIZE.
179 : */
180 : unsigned char hash[HASH_MAX];
181 : };
182 :
183 : /**
184 : * If a file is present in the disk.
185 : * It's used only in scan to detect present and missing files.
186 : */
187 : #define FILE_IS_PRESENT 0x01
188 :
189 : /**
190 : * If it's an excluded file from the processing.
191 : * It's used in both check and fix to mark files to exclude from the processing.
192 : */
193 : #define FILE_IS_EXCLUDED 0x02
194 :
195 : /**
196 : * If a fix was attempted but it failed.
197 : * It's used only in fix to mark that some data is unrecoverable.
198 : */
199 : #define FILE_IS_DAMAGED 0x04
200 :
201 : /**
202 : * If a fix was done.
203 : * It's used only in fix to mark that some data was recovered.
204 : */
205 : #define FILE_IS_FIXED 0x08
206 :
207 : /**
208 : * If the file was originally missing, and it was created in the fix process.
209 : * It's used only in fix to mark files recovered from scratch,
210 : * meaning that they don't have any previous content.
211 : * This is important because it means that deleting them, you are not going
212 : * to lose something that cannot be recovered.
213 : * Note that excluded files won't ever get this flag.
214 : */
215 : #define FILE_IS_CREATED 0x10
216 :
217 : /**
218 : * If the file has completed its processing, meaning that it won't be opened anymore.
219 : * It's used only in fix to mark when we finish processing one file.
220 : * Note that excluded files won't ever get this flag.
221 : */
222 : #define FILE_IS_FINISHED 0x20
223 :
224 : /**
225 : * If the file hash was obtained from a file copy
226 : * identified by the same name, size and stamp.
227 : */
228 : #define FILE_IS_COPY 0x40
229 :
230 : /**
231 : * If the file was opened.
232 : * It's used in fix to detect if it's the first time a file is opened.
233 : */
234 : #define FILE_IS_OPENED 0x80
235 :
236 : /**
237 : * If the file is modified from the latest sync.
238 : * It's used in fix to store the state of the file before being modified.
239 : */
240 : #define FILE_IS_UNSYNCED 0x100
241 :
242 : /**
243 : * If the file is without inode.
244 : * It could happen in file-systems where inodes are not persistent,
245 : * or when restoring a full disk with "fix".
246 : * In such cases we have to clear any stored duplicate inode.
247 : * After the scan process completes, no file should have this flag set.
248 : */
249 : #define FILE_IS_WITHOUT_INODE 0x200
250 :
251 : /**
252 : * The file is deleted.
253 : * This happens when a file is deleted from the array,
254 : * but it's keep inside the parity until the next sync.
255 : *
256 : * During the file-system check we needs this information,
257 : * because deleted files may be present only partially.
258 : */
259 : #define FILE_IS_DELETED 0x400
260 :
261 : /**
262 : * The file is missing.
263 : * This happens in fix/check when a file is cannot be opened,
264 : * and marking it as such prevents to retry to open it again.
265 : */
266 : #define FILE_IS_MISSING 0x800
267 :
268 : #define FILE_IS_HARDLINK 0x1000 /**< If it's an hardlink. */
269 : #define FILE_IS_SYMLINK 0x2000 /**< If it's a file symlink. */
270 : #define FILE_IS_SYMDIR 0x4000 /**< If it's a dir symlink for Windows. Not yet supported. */
271 : #define FILE_IS_JUNCTION 0x8000 /**< If it's a junction for Windows. Not yet supported. */
272 : #define FILE_IS_LINK_MASK 0xF000 /**< Mask for link type. */
273 :
274 : /**
275 : * File.
276 : */
277 : struct snapraid_file {
278 : int64_t mtime_sec; /**< Modification time. */
279 : uint64_t inode; /**< Inode. */
280 : uint64_t physical; /**< Physical offset of the file. */
281 : data_off_t size; /**< Size of the file. */
282 : struct snapraid_block* blockvec; /**< All the blocks of the file. */
283 : int mtime_nsec; /**< Modification time nanoseconds. In the range 0 <= x < 1,000,000,000, or STAT_NSEC_INVALID if not present. */
284 : block_off_t blockmax; /**< Number of blocks. */
285 : unsigned flag; /**< FILE_IS_* flags. */
286 : char* sub; /**< Sub path of the file. Without the disk dir. The disk is implicit. */
287 :
288 : /* nodes for data structures */
289 : tommy_node nodelist;
290 : tommy_hashdyn_node nodeset;
291 : tommy_hashdyn_node pathset;
292 : tommy_hashdyn_node stampset;
293 : };
294 :
295 : /**
296 : * Symbolic Link.
297 : */
298 : struct snapraid_link {
299 : unsigned flag; /**< FILE_IS_* flags. */
300 : char* sub; /**< Sub path of the file. Without the disk dir. The disk is implicit. */
301 : char* linkto; /**< Link to. */
302 :
303 : /* nodes for data structures */
304 : tommy_node nodelist;
305 : tommy_hashdyn_node nodeset;
306 : };
307 :
308 : /**
309 : * Dir.
310 : */
311 : struct snapraid_dir {
312 : unsigned flag; /**< FILE_IS_* flags. */
313 : char* sub; /**< Sub path of the file. Without the disk dir. The disk is implicit. */
314 :
315 : /* nodes for data structures */
316 : tommy_node nodelist;
317 : tommy_hashdyn_node nodeset;
318 : };
319 :
320 : /**
321 : * Chunk.
322 : *
323 : * A extent represents a fragment of a file mapped into the parity.
324 : */
325 : struct snapraid_extent {
326 : struct snapraid_file* file; /**< File containing this extent. */
327 : block_off_t parity_pos; /**< Parity position. */
328 : block_off_t file_pos; /**< Position in the file. */
329 : block_off_t count; /**< Number of sequential blocks in the file and parity. */
330 : tommy_tree_node parity_node; /**< Tree sorted by <parity_pos>. */
331 : tommy_tree_node file_node; /**< Tree sorter by <file,file_pos>. */
332 : };
333 :
334 : /**
335 : * Disk.
336 : */
337 : struct snapraid_disk {
338 : char name[PATH_MAX]; /**< Name of the disk. */
339 : char dir[PATH_MAX]; /**< Mount point of the disk. It always terminates with /. */
340 : char smartctl[PATH_MAX]; /**< Custom command for smartctl. Empty means auto. */
341 : int smartignore[SMART_IGNORE_MAX]; /**< Smart attributes to ignore for this device. */
342 : char uuid[UUID_MAX]; /**< UUID of the disk. They are probed during the config reading. */
343 : char fstype[FSINFO_MAX]; /**< Filesystem type */
344 : char fslabel[FSINFO_MAX]; /**< Filesystem label */
345 :
346 : uint64_t device; /**< Device identifier. */
347 : block_off_t total_blocks; /**< Number of total blocks. */
348 : block_off_t free_blocks; /**< Number of free blocks at the last sync. */
349 :
350 : uint64_t tick; /**< Usage time. */
351 : uint64_t progress_tick[PROGRESS_MAX]; /**< Last ticks of progress. */
352 : unsigned cached_blocks; /**< Number of IO blocks cached. */
353 : struct snapraid_file* progress_file; /**< File in progress. */
354 :
355 : /**
356 : * First free searching block.
357 : * Note that it doesn't necessarily point at the first free block,
358 : * but it just tell you that no free block is present before this position.
359 : */
360 : block_off_t first_free_block;
361 :
362 : int has_volatile_inodes; /**< If the underline file-system has not persistent inodes. */
363 : int has_volatile_hardlinks; /**< If the underline file-system has not synchronized metadata for hardlink (NTFS). */
364 : int has_unreliable_physical; /**< If the physical offset of files has duplicates. */
365 : int has_different_uuid; /**< If the disk has a different UUID, meaning that it is not the same file-system. */
366 : int has_unsupported_uuid; /**< If the disk doesn't report UUID, meaning it's not supported. */
367 : int had_empty_uuid; /**< If the disk had an empty UUID, meaning that it's a new disk. */
368 : int mapping_idx; /**< Index in the mapping vector. Used only as buffer when writing the content file. */
369 : int skip_access; /**< If the disk is inaccessible and it should be skipped. */
370 :
371 : #if HAVE_THREAD
372 : /**
373 : * Mutex for protecting the filesystem structure.
374 : *
375 : * Specifically, this protects ::fs_parity, ::fs_file, and ::fs_last,
376 : * meaning that it protects only extents.
377 : *
378 : * Files, links and dirs are not protected as they are not expected to
379 : * change during multithread processing.
380 : */
381 : thread_mutex_t fs_mutex;
382 :
383 : /**
384 : * Mutex for protecting the scan process.
385 : *
386 : * It's used during the scan process to protect the stampset to identify copy of files
387 : */
388 : thread_mutex_t stamp_mutex;
389 : #endif
390 :
391 : /**
392 : * Mapping of extents in the parity.
393 : * Sorted by <parity_pos> and by <file,file_pos>
394 : */
395 : tommy_tree fs_parity;
396 : tommy_tree fs_file;
397 :
398 : /**
399 : * Last extent we accessed.
400 : * It's used to optimize access of sequential blocks.
401 : */
402 : struct snapraid_extent* fs_last;
403 :
404 : /**
405 : * List of all the snapraid_file for the disk.
406 : */
407 : tommy_list filelist;
408 :
409 : /**
410 : * List of all the deleted file for the disk.
411 : *
412 : * These files are kept allocated, because the blocks are still referenced in
413 : * the ::blockarr.
414 : */
415 : tommy_list deletedlist;
416 :
417 : tommy_hashdyn inodeset; /**< Hashtable by inode of all the files. */
418 : tommy_hashdyn pathset; /**< Hashtable by path of all the files. */
419 : tommy_hashdyn stampset; /**< Hashtable by stamp (size and time) of all the files. */
420 : tommy_list linklist; /**< List of all the links. */
421 : tommy_hashdyn linkset; /**< Hashtable by name of all the links. */
422 : tommy_list dirlist; /**< List of all the empty dirs. */
423 : tommy_hashdyn dirset; /**< Hashtable by name of all the empty dirs. */
424 :
425 : /* nodes for data structures */
426 : tommy_node node;
427 : };
428 :
429 : /**
430 : * Disk mapping.
431 : */
432 : struct snapraid_map {
433 : char name[PATH_MAX]; /**< Name of the disk. */
434 : char uuid[UUID_MAX]; /**< UUID of the disk. Empty if unknown. They are read from the content file. */
435 : block_off_t total_blocks; /**< Number of total blocks. */
436 : block_off_t free_blocks; /**< Number of free blocks at last 'sync'. */
437 : unsigned position; /**< Position of the disk in the parity. */
438 :
439 : /* nodes for data structures */
440 : tommy_node node;
441 : };
442 :
443 : /**
444 : * Max number of parity split.
445 : */
446 : #define SPLIT_MAX 8
447 :
448 : /**
449 : * Invalid parity size.
450 : *
451 : * This value is used to identify new parities,
452 : * like when you alter the configuration adding
453 : * a new parity level, creating it with 'fix'.
454 : * Given that 'fix' doesn't write the content file,
455 : * the new size will be written only at the next
456 : * 'sync'.
457 : */
458 : #define PARITY_SIZE_INVALID -1LL
459 :
460 : /**
461 : * Parity split.
462 : */
463 : struct snapraid_split {
464 : char path[PATH_MAX]; /**< Path of the parity file. */
465 : char uuid[UUID_MAX]; /**< UUID of the disk. Empty if unknown. They are probed during the config reading, and later read from the content file. */
466 : char fstype[FSINFO_MAX]; /**< Filesystem type */
467 : char fslabel[FSINFO_MAX]; /**< Filesystem label */
468 :
469 : /**
470 : * Size of the parity split.
471 : * Only the latest not zero size is allowed to grow.
472 : * If the value is unset, it's PARITY_SIZE_INVALID.
473 : */
474 : data_off_t size;
475 :
476 : uint64_t device; /**< Device identifier of the parity. */
477 : };
478 :
479 : /**
480 : * Parity.
481 : */
482 : struct snapraid_parity {
483 : struct snapraid_split split_map[SPLIT_MAX]; /**< Parity splits. */
484 : unsigned split_mac; /**< Number of parity splits. */
485 : char smartctl[PATH_MAX]; /**< Custom command for smartctl. Empty means auto. */
486 : int smartignore[SMART_IGNORE_MAX]; /**< Smart attributes to ignore for this device. */
487 : block_off_t total_blocks; /**< Number of total blocks. */
488 : block_off_t free_blocks; /**< Number of free blocks at the last sync. */
489 : int is_excluded_by_filter; /**< If the parity is excluded by filters. */
490 : int skip_access; /**< If at least one of the parity disk is inaccessible and it should be skipped. */
491 : uint64_t tick; /**< Usage time. */
492 : uint64_t progress_tick[PROGRESS_MAX]; /**< Last cpu ticks of progress. */
493 : unsigned cached_blocks; /**< Number of IO blocks cached. */
494 : };
495 :
496 : /**
497 : * Info.
498 : */
499 : typedef uint64_t snapraid_info;
500 :
501 : /**
502 : * Allocate a content.
503 : */
504 : struct snapraid_content* content_alloc(const char* path, uint64_t dev);
505 :
506 : /**
507 : * Deallocate a content.
508 : */
509 : void content_free(struct snapraid_content* content);
510 :
511 : /**
512 : * Allocate a filter pattern for files and directories.
513 : */
514 : struct snapraid_filter* filter_alloc_file(int is_include, const char* root, const char* pattern);
515 :
516 : /**
517 : * Allocate a filter pattern for disks.
518 : */
519 : struct snapraid_filter* filter_alloc_disk(int is_include, const char* pattern);
520 :
521 : /**
522 : * Deallocate an exclusion.
523 : */
524 : void filter_free(void* filter);
525 :
526 : /**
527 : * Filter type description.
528 : */
529 : const char* filter_type(struct snapraid_filter* filter, char* out, size_t out_size);
530 :
531 : /**
532 : * Filter hidden files.
533 : * Return !=0 if it matches and it should be excluded.
534 : */
535 3335241 : static inline int filter_hidden(int enable, struct dirent* dd)
536 : {
537 3335241 : if (enable && dirent_hidden(dd)) {
538 0 : return 1; /* filter out */
539 : }
540 :
541 3335241 : return 0;
542 : }
543 :
544 : /**
545 : * Filter a path using a list of filters.
546 : * For each element of the path all the filters are applied, until the first one that matches.
547 : * Return !=0 if it should be excluded.
548 : */
549 : int filter_path(tommy_list* filterlist, struct snapraid_filter** reason, const char* disk, const char* sub);
550 :
551 : /**
552 : * Filter a file/link/dir if missing.
553 : * This call imply a disk check for the file presence.
554 : * Return !=0 if the file is present and it should be excluded.
555 : */
556 : int filter_existence(int filter_missing, const char* dir, const char* sub);
557 :
558 : /**
559 : * Filter a file if bad.
560 : * Return !=0 if the file is correct and it should be excluded.
561 : */
562 : int filter_correctness(int filter_error, tommy_arrayblkof* infoarr, struct snapraid_disk* disk, struct snapraid_file* file);
563 :
564 : /**
565 : * Filter a dir using a list of filters.
566 : * For each element of the path all the filters are applied, until the first one that matches.
567 : * Thesesdir are always by included by default, to allow to apply rules at the contained files.
568 : * Return !=0 if should be excluded.
569 : */
570 : int filter_subdir(tommy_list* filterlist, struct snapraid_filter** reason, const char* disk, const char* sub);
571 :
572 : /**
573 : * Filter a dir using a list of filters.
574 : * For each element of the path all the filters are applied, until the first one that matches.
575 : * Return !=0 if should be excluded.
576 : */
577 : int filter_emptydir(tommy_list* filterlist, struct snapraid_filter** reason, const char* disk, const char* sub);
578 :
579 : /**
580 : * Filter a path if it's a content file.
581 : * Return !=0 if should be excluded.
582 : */
583 : int filter_content(tommy_list* contentlist, const char* path);
584 :
585 : /**
586 : * Check if the specified hash is invalid.
587 : *
588 : * An invalid hash is represented with all bytes at 0x00.
589 : *
590 : * If working with reduced hash lengths, this function always return 0.
591 : */
592 793636 : static inline int hash_is_invalid(const unsigned char* hash)
593 : {
594 : int i;
595 :
596 : /* if the hash is reduced, we cannot grant that it's a specific kind of hash */
597 793636 : if (BLOCK_HASH_SIZE != HASH_MAX)
598 0 : return 0;
599 :
600 1216204 : for (i = 0; i < BLOCK_HASH_SIZE; ++i)
601 1189986 : if (hash[i] != 0x00)
602 767418 : return 0;
603 :
604 26218 : return 1;
605 : }
606 :
607 : /**
608 : * Check if the specified hash represent the zero block.
609 : *
610 : * A zero hash is represented with all bytes at 0xFF.
611 : *
612 : * If working with reduced hash lengths, this function always return 0.
613 : */
614 797210 : static inline int hash_is_zero(const unsigned char* hash)
615 : {
616 : int i;
617 :
618 : /* if the hash is reduced, we cannot grant that it's a specific kind of hash */
619 797210 : if (BLOCK_HASH_SIZE != HASH_MAX)
620 0 : return 0;
621 :
622 1064162 : for (i = 0; i < BLOCK_HASH_SIZE; ++i)
623 1047645 : if (hash[i] != 0xFF)
624 780693 : return 0;
625 :
626 16517 : return 1;
627 : }
628 :
629 : /**
630 : * Check if the specified hash is unequivocally representing the data.
631 : *
632 : * If working with reduced hash lengths, this function always return 0.
633 : */
634 44343 : static inline int hash_is_unique(const unsigned char* hash)
635 : {
636 : /* if the hash is reduced, we cannot grant that it's a specific kind of hash */
637 44343 : if (BLOCK_HASH_SIZE != HASH_MAX)
638 0 : return 0;
639 :
640 44343 : return !hash_is_zero(hash) && !hash_is_invalid(hash);
641 : }
642 :
643 : /**
644 : * Set the hash to the special INVALID value.
645 : */
646 9819356 : static inline void hash_invalid_set(unsigned char* hash)
647 : {
648 9819356 : memset(hash, 0x00, BLOCK_HASH_SIZE);
649 9819356 : }
650 :
651 : /**
652 : * Set the hash to the special ZERO value.
653 : */
654 117596 : static inline void hash_zero_set(unsigned char* hash)
655 : {
656 117596 : memset(hash, 0xFF, BLOCK_HASH_SIZE);
657 117596 : }
658 :
659 : /**
660 : * Allocated space for block.
661 : */
662 72179271 : static inline size_t block_sizeof(void)
663 : {
664 72179271 : return 1 + BLOCK_HASH_SIZE;
665 : }
666 :
667 : /**
668 : * Get the state of the block.
669 : *
670 : * For this function, it's allowed to pass a NULL block
671 : * pointer than results in the BLOCK_STATE_EMPTY state.
672 : */
673 77478618 : static inline unsigned block_state_get(const struct snapraid_block* block)
674 : {
675 77478618 : if (block == BLOCK_NULL)
676 2553277 : return BLOCK_STATE_EMPTY;
677 :
678 74925341 : return block->state;
679 : }
680 :
681 : /**
682 : * Set the state of the block.
683 : */
684 20335584 : static inline void block_state_set(struct snapraid_block* block, unsigned state)
685 : {
686 20335584 : block->state = state;
687 20335584 : }
688 :
689 : /**
690 : * Check if the specified block has an updated hash.
691 : *
692 : * Note that EMPTY / CHG / DELETED return 0.
693 : */
694 2321532 : static inline int block_has_updated_hash(const struct snapraid_block* block)
695 : {
696 2321532 : unsigned state = block_state_get(block);
697 :
698 2321532 : return state == BLOCK_STATE_BLK || state == BLOCK_STATE_REP;
699 : }
700 :
701 : /**
702 : * Check if the specified block has a past hash,
703 : * i.e. the hash of the data that it's now overwritten or lost.
704 : *
705 : * Note that EMPTY / BLK / REP return 0.
706 : */
707 2820746 : static inline int block_has_past_hash(const struct snapraid_block* block)
708 : {
709 2820746 : unsigned state = block_state_get(block);
710 :
711 2820746 : return state == BLOCK_STATE_CHG || state == BLOCK_STATE_DELETED;
712 : }
713 :
714 : /**
715 : * Check if the specified block is part of a file.
716 : *
717 : * Note that EMPTY / DELETED return 0.
718 : */
719 26274837 : static inline int block_has_file(const struct snapraid_block* block)
720 : {
721 26274837 : unsigned state = block_state_get(block);
722 :
723 : return state == BLOCK_STATE_BLK
724 26274837 : || state == BLOCK_STATE_CHG || state == BLOCK_STATE_REP;
725 : }
726 :
727 : /**
728 : * Check if the block has an invalid parity than needs to be updated.
729 : *
730 : * Note that EMPTY / BLK return 0.
731 : */
732 24126864 : static inline int block_has_invalid_parity(const struct snapraid_block* block)
733 : {
734 24126864 : unsigned state = block_state_get(block);
735 :
736 : return state == BLOCK_STATE_DELETED
737 24126864 : || state == BLOCK_STATE_CHG || state == BLOCK_STATE_REP;
738 : }
739 :
740 : /**
741 : * Check if the block is part of a file with valid parity.
742 : *
743 : * Note that anything different than BLK return 0.
744 : */
745 223377 : static inline int block_has_file_and_valid_parity(const struct snapraid_block* block)
746 : {
747 223377 : unsigned state = block_state_get(block);
748 :
749 223377 : return state == BLOCK_STATE_BLK;
750 : }
751 :
752 40833654 : static inline int file_flag_has(const struct snapraid_file* file, unsigned mask)
753 : {
754 40833654 : return (file->flag & mask) == mask;
755 : }
756 :
757 6026300 : static inline void file_flag_set(struct snapraid_file* file, unsigned mask)
758 : {
759 6026300 : file->flag |= mask;
760 6026300 : }
761 :
762 15189 : static inline void file_flag_clear(struct snapraid_file* file, unsigned mask)
763 : {
764 15189 : file->flag &= ~mask;
765 15189 : }
766 :
767 : /**
768 : * Allocate a file.
769 : */
770 : 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);
771 :
772 : /**
773 : * Duplicate a file.
774 : */
775 : struct snapraid_file* file_dup(struct snapraid_file* copy);
776 :
777 : /**
778 : * Deallocate a file.
779 : */
780 : void file_free(struct snapraid_file* file);
781 :
782 : /**
783 : * Rename a file.
784 : */
785 : void file_rename(struct snapraid_file* file, const char* sub);
786 :
787 : /**
788 : * Copy a file.
789 : */
790 : void file_copy(struct snapraid_file* src_file, struct snapraid_file* dest_file);
791 :
792 : /**
793 : * Return the block at the specified position.
794 : *
795 : * Note that the block size if a runtime value.
796 : */
797 68233107 : static inline struct snapraid_block* file_block(struct snapraid_file* file, size_t pos)
798 : {
799 68233107 : unsigned char* ptr = (unsigned char*)file->blockvec;
800 :
801 68233107 : return (struct snapraid_block*)(ptr + pos * block_sizeof());
802 : }
803 :
804 : /**
805 : * Return the name of the file, without the dir.
806 : */
807 : const char* file_name(const struct snapraid_file* file);
808 :
809 : /**
810 : * Check if the block is the last in the file.
811 : */
812 : int file_block_is_last(struct snapraid_file* file, block_off_t file_pos);
813 :
814 : /**
815 : * Get the size in bytes of the block.
816 : * If it's the last block of a file it could be less than block_size.
817 : */
818 : unsigned file_block_size(struct snapraid_file* file, block_off_t file_pos, unsigned block_size);
819 :
820 : /**
821 : * Compare a file with an inode.
822 : */
823 : int file_inode_compare_to_arg(const void* void_arg, const void* void_data);
824 :
825 : /**
826 : * Compare files by inode.
827 : */
828 : int file_inode_compare(const void* void_a, const void* void_b);
829 :
830 : /**
831 : * Compare files by path.
832 : */
833 : int file_path_compare(const void* void_a, const void* void_b);
834 :
835 : /**
836 : * Compare files by physical address.
837 : */
838 : int file_physical_compare(const void* void_a, const void* void_b);
839 :
840 : /**
841 : * Compute the hash of a file inode.
842 : */
843 5408829 : static inline tommy_uint32_t file_inode_hash(uint64_t inode)
844 : {
845 5408829 : return (tommy_uint32_t)tommy_inthash_u64(inode);
846 : }
847 :
848 : /**
849 : * Compare a file with a path.
850 : */
851 : int file_path_compare_to_arg(const void* void_arg, const void* void_data);
852 :
853 : /**
854 : * Compare a file with another file for name, stamp, and both.
855 : */
856 : int file_name_compare(const void* void_a, const void* void_b);
857 : int file_stamp_compare(const void* void_a, const void* void_b);
858 : int file_namestamp_compare(const void* void_a, const void* void_b);
859 : int file_pathstamp_compare(const void* void_a, const void* void_b);
860 :
861 : /**
862 : * Compute the hash of a file path.
863 : */
864 4125938 : static inline tommy_uint32_t file_path_hash(const char* sub)
865 : {
866 4125938 : return tommy_hash_u32(0, sub, strlen(sub));
867 : }
868 :
869 : /**
870 : * Compute the hash of a file stamp.
871 : */
872 6590399 : static inline tommy_uint32_t file_stamp_hash(data_off_t size, int64_t mtime_sec, int mtime_nsec)
873 : {
874 6590399 : return tommy_inthash_u32((tommy_uint32_t)size ^ tommy_inthash_u32(mtime_sec ^ tommy_inthash_u32(mtime_nsec)));
875 : }
876 :
877 : /**
878 : * Allocate a extent.
879 : */
880 : struct snapraid_extent* extent_alloc(block_off_t parity_pos, struct snapraid_file* file, block_off_t file_pos, block_off_t count);
881 :
882 : /**
883 : * Deallocate a extent.
884 : */
885 : void extent_free(struct snapraid_extent* extent);
886 :
887 : /**
888 : * Compare extent by parity position.
889 : */
890 : int extent_parity_compare(const void* void_a, const void* void_b);
891 :
892 : /**
893 : * Compare extent by file and file position.
894 : */
895 : int extent_file_compare(const void* void_a, const void* void_b);
896 :
897 223705 : static inline int link_flag_has(const struct snapraid_link* slink, unsigned mask)
898 : {
899 223705 : return (slink->flag & mask) == mask;
900 : }
901 :
902 43664 : static inline void link_flag_set(struct snapraid_link* slink, unsigned mask)
903 : {
904 43664 : slink->flag |= mask;
905 43664 : }
906 :
907 : static inline void link_flag_clear(struct snapraid_link* slink, unsigned mask)
908 : {
909 : slink->flag &= ~mask;
910 : }
911 :
912 7 : static inline void link_flag_let(struct snapraid_link* slink, unsigned flag, unsigned mask)
913 : {
914 7 : slink->flag &= ~mask;
915 7 : slink->flag |= flag & mask;
916 7 : }
917 :
918 99564 : static inline unsigned link_flag_get(struct snapraid_link* slink, unsigned mask)
919 : {
920 99564 : return slink->flag & mask;
921 : }
922 :
923 : /**
924 : * Allocate a link.
925 : */
926 : struct snapraid_link* link_alloc(const char* name, const char* slink, unsigned link_flag);
927 :
928 : /**
929 : * Deallocate a link.
930 : */
931 : void link_free(struct snapraid_link* slink);
932 :
933 : /**
934 : * Compare a link with a name.
935 : */
936 : int link_name_compare_to_arg(const void* void_arg, const void* void_data);
937 :
938 : /**
939 : * Compare links by path.
940 : */
941 : int link_alpha_compare(const void* void_a, const void* void_b);
942 :
943 : /**
944 : * Compute the hash of a link name.
945 : */
946 166594 : static inline tommy_uint32_t link_name_hash(const char* name)
947 : {
948 166594 : return tommy_hash_u32(0, name, strlen(name));
949 : }
950 :
951 1028 : static inline int dir_flag_has(const struct snapraid_dir* dir, unsigned mask)
952 : {
953 1028 : return (dir->flag & mask) == mask;
954 : }
955 :
956 318 : static inline void dir_flag_set(struct snapraid_dir* dir, unsigned mask)
957 : {
958 318 : dir->flag |= mask;
959 318 : }
960 :
961 : static inline void dir_flag_clear(struct snapraid_dir* dir, unsigned mask)
962 : {
963 : dir->flag &= ~mask;
964 : }
965 :
966 : /**
967 : * Allocate a dir.
968 : */
969 : struct snapraid_dir* dir_alloc(const char* name);
970 :
971 : /**
972 : * Deallocate a dir.
973 : */
974 : void dir_free(struct snapraid_dir* dir);
975 :
976 : /**
977 : * Compare a dir with a name.
978 : */
979 : int dir_name_compare(const void* void_arg, const void* void_data);
980 :
981 : /**
982 : * Compute the hash of a dir name.
983 : */
984 1167 : static inline tommy_uint32_t dir_name_hash(const char* name)
985 : {
986 1167 : return tommy_hash_u32(0, name, strlen(name));
987 : }
988 :
989 : /**
990 : * Allocate a disk.
991 : */
992 : struct snapraid_disk* disk_alloc(const char* name, const char* dir, uint64_t dev, const char* uuid, int skip_access);
993 :
994 : /**
995 : * Deallocate a disk.
996 : */
997 : void disk_free(struct snapraid_disk* disk);
998 :
999 : /**
1000 : * Get the size of the disk in blocks.
1001 : */
1002 : block_off_t fs_size(struct snapraid_disk* disk);
1003 :
1004 : /**
1005 : * Check if a disk is totally empty and can be discarded from the content file.
1006 : * A disk is empty if it doesn't contain any file, symlink, hardlink or dir
1007 : * and without any DELETED block.
1008 : * The blockmax is used to limit the search of DELETED block up to blockmax.
1009 : */
1010 : int fs_is_empty(struct snapraid_disk* disk, block_off_t blockmax);
1011 :
1012 : /**
1013 : * Check the file-system for errors.
1014 : * Return 0 if it's OK.
1015 : */
1016 : int fs_check(struct snapraid_disk* disk);
1017 :
1018 : /**
1019 : * Allocate a parity position for the specified file position.
1020 : *
1021 : * After this call you can use the par2file/par2block operations
1022 : * to query the relation.
1023 : *
1024 : * \note This function is NOT thread-safe as it uses the disk cache. +
1025 : */
1026 : void fs_allocate(struct snapraid_disk* disk, block_off_t parity_pos, struct snapraid_file* file, block_off_t file_pos);
1027 :
1028 : /**
1029 : * Deallocate the parity position.
1030 : *
1031 : * After this call the par2file/par2block operations
1032 : * won't find anymore the parity association.
1033 : *
1034 : * \note This function is NOT thread-safe as it uses the disk cache.
1035 : */
1036 : void fs_deallocate(struct snapraid_disk* disk, block_off_t pos);
1037 :
1038 : /**
1039 : * Get the block from the file position.
1040 : */
1041 : struct snapraid_block* fs_file2block_get(struct snapraid_file* file, block_off_t file_pos);
1042 :
1043 : /**
1044 : * Get the file position from the parity position.
1045 : * Return 0 if no file is using it.
1046 : */
1047 : struct snapraid_file* fs_par2file_find(struct snapraid_disk* disk, block_off_t parity_pos, block_off_t* file_pos);
1048 :
1049 : /**
1050 : * Get the file position from the parity position.
1051 : */
1052 11875762 : static inline struct snapraid_file* fs_par2file_get(struct snapraid_disk* disk, block_off_t parity_pos, block_off_t* file_pos)
1053 : {
1054 : struct snapraid_file* ret;
1055 :
1056 11875762 : ret = fs_par2file_find(disk, parity_pos, file_pos);
1057 11875762 : if (ret == 0) {
1058 : /* LCOV_EXCL_START */
1059 : log_fatal(EINTERNAL, "Internal inconsistency: Deresolving parity to file at position '%u' in disk '%s'\n", parity_pos, disk->name);
1060 : os_abort();
1061 : /* LCOV_EXCL_STOP */
1062 : }
1063 :
1064 11875762 : return ret;
1065 : }
1066 :
1067 : /**
1068 : * Get the parity position from the file position.
1069 : * Return POS_NULL if no parity is allocated.
1070 : */
1071 : block_off_t fs_file2par_find(struct snapraid_disk* disk, struct snapraid_file* file, block_off_t file_pos);
1072 :
1073 : /**
1074 : * Get the parity position from the file position.
1075 : */
1076 5093305 : static inline block_off_t fs_file2par_get(struct snapraid_disk* disk, struct snapraid_file* file, block_off_t file_pos)
1077 : {
1078 : block_off_t ret;
1079 :
1080 5093305 : ret = fs_file2par_find(disk, file, file_pos);
1081 5093305 : if (ret == POS_NULL) {
1082 : /* LCOV_EXCL_START */
1083 : log_fatal(EINTERNAL, "Internal inconsistency: Resolving file '%s' at position '%u/%u' in disk '%s'\n", file->sub, file_pos, file->blockmax, disk->name);
1084 : os_abort();
1085 : /* LCOV_EXCL_STOP */
1086 : }
1087 :
1088 5093305 : return ret;
1089 : }
1090 :
1091 : /**
1092 : * Get the block from the parity position.
1093 : * Return BLOCK_NULL==0 if the block is over the end of the disk or not used.
1094 : */
1095 : struct snapraid_block* fs_par2block_find(struct snapraid_disk* disk, block_off_t parity_pos);
1096 :
1097 : /**
1098 : * Get the block from the parity position.
1099 : */
1100 17742 : static inline struct snapraid_block* fs_par2block_get(struct snapraid_disk* disk, block_off_t parity_pos)
1101 : {
1102 : struct snapraid_block* ret;
1103 :
1104 17742 : ret = fs_par2block_find(disk, parity_pos);
1105 17742 : if (ret == BLOCK_NULL) {
1106 : /* LCOV_EXCL_START */
1107 : log_fatal(EINTERNAL, "Internal inconsistency: Deresolving parity to block at position '%u' in disk '%s'\n", parity_pos, disk->name);
1108 : os_abort();
1109 : /* LCOV_EXCL_STOP */
1110 : }
1111 :
1112 17742 : return ret;
1113 : }
1114 :
1115 : /**
1116 : * Allocate a disk mapping.
1117 : * Uses uuid="" if not available.
1118 : */
1119 : struct snapraid_map* map_alloc(const char* name, unsigned position, block_off_t total_blocks, block_off_t free_blocks, const char* uuid);
1120 :
1121 : /**
1122 : * Deallocate a disk mapping.
1123 : */
1124 : void map_free(struct snapraid_map* map);
1125 :
1126 : /**
1127 : * Mask used to store additional information in the info bits.
1128 : *
1129 : * These bits reduce the granularity of the time in the memory representation.
1130 : */
1131 : #define INFO_MASK ((snapraid_info)0x7)
1132 :
1133 : /**
1134 : * Make an info.
1135 : */
1136 191554 : static inline snapraid_info info_make(time_t last_access, int error, int rehash, int justsynced)
1137 : {
1138 : /* clear the lowest bits as reserved for other information */
1139 191554 : snapraid_info info = last_access & ~INFO_MASK;
1140 :
1141 191554 : if (error != 0)
1142 5333 : info |= 0x1;
1143 191554 : if (rehash != 0)
1144 14266 : info |= 0x2;
1145 191554 : if (justsynced != 0)
1146 145224 : info |= 0x4;
1147 191554 : return info;
1148 : }
1149 :
1150 : /**
1151 : * Extract the time information.
1152 : * This is the last time when the block was know to be correct.
1153 : * The "scrubbed" info tells if the time is referring at the latest sync or scrub.
1154 : */
1155 818937 : static inline time_t info_get_time(snapraid_info info)
1156 : {
1157 818937 : return info & ~INFO_MASK;
1158 : }
1159 :
1160 : /**
1161 : * Extract the error information.
1162 : * Report if the block address had some problem.
1163 : */
1164 253366 : static inline int info_get_bad(snapraid_info info)
1165 : {
1166 253366 : return (info & 0x1) != 0;
1167 : }
1168 :
1169 : /**
1170 : * Extract the rehash information.
1171 : * Report if the block address is using the old hash and needs to be rehashed.
1172 : */
1173 2106478 : static inline int info_get_rehash(snapraid_info info)
1174 : {
1175 2106478 : return (info & 0x2) != 0;
1176 : }
1177 :
1178 : /**
1179 : * Extract the scrubbed information.
1180 : * Report if the block address was never scrubbed.
1181 : */
1182 19188 : static inline int info_get_justsynced(snapraid_info info)
1183 : {
1184 19188 : return (info & 0x4) != 0;
1185 : }
1186 :
1187 : /**
1188 : * Mark the block address as with error.
1189 : */
1190 1772 : static inline snapraid_info info_set_bad(snapraid_info info)
1191 : {
1192 1772 : return info | 0x1;
1193 : }
1194 :
1195 : /**
1196 : * Mark the block address as with rehash.
1197 : */
1198 9275 : static inline snapraid_info info_set_rehash(snapraid_info info)
1199 : {
1200 9275 : return info | 0x2;
1201 : }
1202 :
1203 : /**
1204 : * Set the info at the specified position.
1205 : * The position is allocated if not yet done.
1206 : */
1207 1822649 : static inline void info_set(tommy_arrayblkof* array, block_off_t pos, snapraid_info info)
1208 : {
1209 1822649 : tommy_arrayblkof_grow(array, pos + 1);
1210 :
1211 1822649 : memcpy(tommy_arrayblkof_ref(array, pos), &info, sizeof(snapraid_info));
1212 1822649 : }
1213 :
1214 : /**
1215 : * Get the info at the specified position.
1216 : * For not allocated position, 0 is returned.
1217 : */
1218 3253235 : static inline snapraid_info info_get(tommy_arrayblkof* array, block_off_t pos)
1219 : {
1220 : snapraid_info info;
1221 :
1222 3253235 : if (pos >= tommy_arrayblkof_size(array))
1223 264646 : return 0;
1224 :
1225 2988589 : memcpy(&info, tommy_arrayblkof_ref(array, pos), sizeof(snapraid_info));
1226 :
1227 2988589 : return info;
1228 : }
1229 :
1230 : /****************************************************************************/
1231 : /* bucket */
1232 :
1233 : struct snapraid_bucket {
1234 : time_t time_at; /**< Time of the scrub */
1235 : block_off_t count_scrubbed; /**< Number of blocks scrubbed */
1236 : block_off_t count_justsynced; /**< Number of blocks justsynced */
1237 : tommy_node node;
1238 : };
1239 :
1240 : void bucket_free(struct snapraid_bucket* bucket);
1241 :
1242 : /**
1243 : * Insert an bucket record into the hashtable
1244 : */
1245 : void bucket_insert(tommy_hashdyn* bucket_hash, time_t time_at, block_off_t count, int justsynced);
1246 :
1247 : /**
1248 : * Convert the bucket hashtable to a sorted list
1249 : */
1250 : void bucket_to_list(tommy_hashdyn* bucket_hash, tommy_list* bucket_list, block_off_t* bucketcount);
1251 :
1252 : /****************************************************************************/
1253 : /* format */
1254 :
1255 : #define FMT_FILE 0 /**< Print only the file. */
1256 : #define FMT_DISK 1 /**< Print the disk name and the file. */
1257 : #define FMT_PATH 2 /**< Print the full path. */
1258 :
1259 : extern int FMT_MODE;
1260 :
1261 : /**
1262 : * Format a file path for poll reference
1263 : */
1264 : const char* fmt_poll(const struct snapraid_disk* disk, const char* str, char* buffer);
1265 :
1266 : /**
1267 : * Format a path name for terminal reference
1268 : */
1269 : const char* fmt_term(const struct snapraid_disk* disk, const char* str, char* buffer);
1270 :
1271 : /**
1272 : * Format a size adding suffix K/M/G/T
1273 : */
1274 : const char* fmt_size(uint64_t size, char* buffer, size_t buffer_size);
1275 :
1276 : #endif
1277 :
|