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