Line data Source code
1 : /*
2 : * Copyright (C) 2011 Andrea Mazzoleni
3 : *
4 : * This program is free software: you can redistribute it and/or modify
5 : * it under the terms of the GNU General Public License as published by
6 : * the Free Software Foundation, either version 3 of the License, or
7 : * (at your option) any later version.
8 : *
9 : * This program is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : * GNU General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU General Public License
15 : * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 : */
17 :
18 : #include "portable.h"
19 :
20 : #include "elem.h"
21 : #include "support.h"
22 : #include "util.h"
23 :
24 : /****************************************************************************/
25 : /* snapraid */
26 :
27 1434 : struct snapraid_content* content_alloc(const char* path, uint64_t dev)
28 : {
29 : struct snapraid_content* content;
30 :
31 1434 : content = malloc_nofail(sizeof(struct snapraid_content));
32 1434 : pathimport(content->content, sizeof(content->content), path);
33 1434 : content->device = dev;
34 :
35 1434 : return content;
36 : }
37 :
38 1297 : void content_free(struct snapraid_content* content)
39 : {
40 1297 : free(content);
41 1297 : }
42 :
43 498 : struct snapraid_filter* filter_alloc_file(int direction, const char* pattern)
44 : {
45 : struct snapraid_filter* filter;
46 : char* i;
47 : char* first;
48 : char* last;
49 : int token_is_valid;
50 : int token_is_filled;
51 :
52 498 : filter = malloc_nofail(sizeof(struct snapraid_filter));
53 498 : pathimport(filter->pattern, sizeof(filter->pattern), pattern);
54 498 : filter->direction = direction;
55 :
56 : /* find first and last slash */
57 498 : first = 0;
58 498 : last = 0;
59 : /* reject invalid tokens, like "<empty>", ".", ".." and more dots */
60 498 : token_is_valid = 0;
61 498 : token_is_filled = 0;
62 6172 : for (i = filter->pattern; *i; ++i) {
63 5675 : if (*i == '/') {
64 : /* reject invalid tokens, but accept an empty one as first */
65 5 : if (!token_is_valid && (first != 0 || token_is_filled)) {
66 1 : free(filter);
67 1 : return 0;
68 : }
69 4 : token_is_valid = 0;
70 4 : token_is_filled = 0;
71 :
72 : /* update slash position */
73 4 : if (!first)
74 3 : first = i;
75 4 : last = i;
76 5670 : } else if (*i != '.') {
77 5176 : token_is_valid = 1;
78 5176 : token_is_filled = 1;
79 : } else {
80 494 : token_is_filled = 1;
81 : }
82 : }
83 :
84 : /* reject invalid tokens, but accept an empty one as last, but not if it's the only one */
85 497 : if (!token_is_valid && (first == 0 || token_is_filled)) {
86 1 : free(filter);
87 1 : return 0;
88 : }
89 :
90 : /* it's a file filter */
91 496 : filter->is_disk = 0;
92 :
93 496 : if (first == 0) {
94 : /* no slash */
95 493 : filter->is_path = 0;
96 493 : filter->is_dir = 0;
97 3 : } else if (first == last && last[1] == 0) {
98 : /* one slash at the end */
99 1 : filter->is_path = 0;
100 1 : filter->is_dir = 1;
101 1 : last[0] = 0;
102 : } else {
103 : /* at least a slash not at the end */
104 2 : filter->is_path = 1;
105 2 : if (last[1] == 0) {
106 0 : filter->is_dir = 1;
107 0 : last[0] = 0;
108 : } else {
109 2 : filter->is_dir = 0;
110 : }
111 :
112 : /* a slash must be the first char, as we don't support PATH/FILE and PATH/DIR/ */
113 2 : if (filter->pattern[0] != '/') {
114 1 : free(filter);
115 1 : return 0;
116 : }
117 : }
118 :
119 495 : return filter;
120 : }
121 :
122 8 : struct snapraid_filter* filter_alloc_disk(int direction, const char* pattern)
123 : {
124 : struct snapraid_filter* filter;
125 :
126 8 : filter = malloc_nofail(sizeof(struct snapraid_filter));
127 8 : pathimport(filter->pattern, sizeof(filter->pattern), pattern);
128 8 : filter->direction = direction;
129 :
130 : /* it's a disk filter */
131 8 : filter->is_disk = 1;
132 8 : filter->is_path = 0;
133 8 : filter->is_dir = 0;
134 :
135 : /* no slash allowed in disk names */
136 8 : if (strchr(filter->pattern, '/') != 0) {
137 : /* LCOV_EXCL_START */
138 : free(filter);
139 : return 0;
140 : /* LCOV_EXCL_STOP */
141 : }
142 :
143 8 : return filter;
144 : }
145 :
146 461 : void filter_free(struct snapraid_filter* filter)
147 : {
148 461 : free(filter);
149 461 : }
150 :
151 125579 : const char* filter_type(struct snapraid_filter* filter, char* out, size_t out_size)
152 : {
153 : const char* direction;
154 125579 : if (filter->direction < 0)
155 125333 : direction = "exclude";
156 : else
157 246 : direction = "include";
158 :
159 125579 : if (filter->is_disk)
160 0 : pathprint(out, out_size, "%s %s:", direction, filter->pattern);
161 125579 : else if (filter->is_dir)
162 0 : pathprint(out, out_size, "%s %s/", direction, filter->pattern);
163 : else
164 125579 : pathprint(out, out_size, "%s %s", direction, filter->pattern);
165 :
166 125579 : return out;
167 : }
168 :
169 12035121 : static int filter_apply(struct snapraid_filter* filter, struct snapraid_filter** reason, const char* path, const char* name, int is_dir)
170 : {
171 12035121 : int ret = 0;
172 :
173 : /* match dirs with dirs and files with files */
174 12035121 : if (filter->is_dir && !is_dir)
175 2938 : return 0;
176 12032183 : if (!filter->is_dir && is_dir)
177 6014696 : return 0;
178 :
179 6017487 : if (filter->is_path) {
180 : /* skip initial slash, as always missing from the path */
181 2938 : if (fnmatch(filter->pattern + 1, path, FNM_PATHNAME | FNM_CASEINSENSITIVE_FOR_WIN) == 0)
182 0 : ret = filter->direction;
183 : } else {
184 6014549 : if (fnmatch(filter->pattern, name, FNM_CASEINSENSITIVE_FOR_WIN) == 0)
185 128077 : ret = filter->direction;
186 : }
187 :
188 6017487 : if (reason != 0 && ret < 0)
189 125087 : *reason = filter;
190 :
191 6017487 : return ret;
192 : }
193 :
194 6023322 : static int filter_recurse(struct snapraid_filter* filter, struct snapraid_filter** reason, const char* const_path, int is_dir)
195 : {
196 : char path[PATH_MAX];
197 : char* name;
198 : unsigned i;
199 :
200 6023322 : pathcpy(path, sizeof(path), const_path);
201 :
202 : /* filter for all the directories */
203 6023322 : name = path;
204 86943549 : for (i = 0; path[i] != 0; ++i) {
205 80923217 : if (path[i] == '/') {
206 : /* set a terminator */
207 6014789 : path[i] = 0;
208 :
209 : /* filter the directory */
210 6014789 : if (filter_apply(filter, reason, path, name, 1) != 0)
211 2990 : return filter->direction;
212 :
213 : /* restore the slash */
214 6011799 : path[i] = '/';
215 :
216 : /* next name */
217 6011799 : name = path + i + 1;
218 : }
219 : }
220 :
221 : /* filter the final file */
222 6020332 : if (filter_apply(filter, reason, path, name, is_dir) != 0)
223 125087 : return filter->direction;
224 :
225 5895245 : return 0;
226 : }
227 :
228 3226046 : static int filter_element(tommy_list* filterlist, struct snapraid_filter** reason, const char* disk, const char* sub, int is_dir)
229 : {
230 : tommy_node* i;
231 :
232 3226046 : int direction = 1; /* by default include all */
233 :
234 : /* for each filter */
235 9222499 : for (i = tommy_list_head(filterlist); i != 0; i = i->next) {
236 : int ret;
237 6133199 : struct snapraid_filter* filter = i->data;
238 :
239 6133199 : if (filter->is_disk) {
240 109877 : if (fnmatch(filter->pattern, disk, FNM_CASEINSENSITIVE_FOR_WIN) == 0)
241 8669 : ret = filter->direction;
242 : else
243 101208 : ret = 0;
244 109877 : if (reason != 0 && ret < 0)
245 0 : *reason = filter;
246 : } else {
247 6023322 : ret = filter_recurse(filter, reason, sub, is_dir);
248 : }
249 :
250 6133199 : if (ret > 0) {
251 : /* include the file */
252 11659 : return 0;
253 6121540 : } else if (ret < 0) {
254 : /* exclude the file */
255 125087 : return -1;
256 : } else {
257 : /* default is opposite of the last filter */
258 5996453 : direction = -filter->direction;
259 5996453 : if (reason != 0 && direction < 0)
260 3004264 : *reason = filter;
261 : /* continue with the next one */
262 : }
263 : }
264 :
265 : /* directories are always included by default, otherwise we cannot apply rules */
266 : /* to the contained files */
267 3089300 : if (is_dir)
268 2971 : return 0;
269 :
270 : /* files are excluded/included depending of the last rule processed */
271 3086329 : if (direction < 0)
272 57541 : return -1;
273 :
274 3028788 : return 0;
275 : }
276 :
277 3223074 : int filter_path(tommy_list* filterlist, struct snapraid_filter** reason, const char* disk, const char* sub)
278 : {
279 3223074 : return filter_element(filterlist, reason, disk, sub, 0);
280 : }
281 :
282 2972 : int filter_dir(tommy_list* filterlist, struct snapraid_filter** reason, const char* disk, const char* sub)
283 : {
284 2972 : return filter_element(filterlist, reason, disk, sub, 1);
285 : }
286 :
287 80649 : int filter_existence(int filter_missing, const char* dir, const char* sub)
288 : {
289 : char path[PATH_MAX];
290 : struct stat st;
291 :
292 80649 : if (!filter_missing)
293 34955 : return 0;
294 :
295 : /* we directly check if in the disk the file is present or not */
296 45694 : pathprint(path, sizeof(path), "%s%s", dir, sub);
297 :
298 45694 : if (lstat(path, &st) != 0) {
299 : /* if the file doesn't exist, we don't filter it out */
300 5007 : if (errno == ENOENT)
301 5007 : return 0;
302 : /* LCOV_EXCL_START */
303 : log_fatal("Error in stat file '%s'. %s.\n", path, strerror(errno));
304 : exit(EXIT_FAILURE);
305 : /* LCOV_EXCL_STOP */
306 : }
307 :
308 : /* the file is present, so we filter it out */
309 40687 : return 1;
310 : }
311 :
312 38803 : int filter_correctness(int filter_error, tommy_arrayblkof* infoarr, struct snapraid_disk* disk, struct snapraid_file* file)
313 : {
314 : unsigned i;
315 :
316 38803 : if (!filter_error)
317 16277 : return 0;
318 :
319 : /* check each block of the file */
320 60103 : for (i = 0; i < file->blockmax; ++i) {
321 46031 : block_off_t parity_pos = fs_file2par_get(disk, file, i);
322 46031 : snapraid_info info = info_get(infoarr, parity_pos);
323 :
324 : /* if the file has a bad block, don't exclude it */
325 46031 : if (info_get_bad(info))
326 8454 : return 0;
327 : }
328 :
329 : /* the file is correct, so we filter it out */
330 14072 : return 1;
331 : }
332 :
333 3066757 : int filter_content(tommy_list* contentlist, const char* path)
334 : {
335 : tommy_node* i;
336 :
337 39679746 : for (i = tommy_list_head(contentlist); i != 0; i = i->next) {
338 16773116 : struct snapraid_content* content = i->data;
339 : char tmp[PATH_MAX];
340 :
341 16773116 : if (pathcmp(content->content, path) == 0)
342 0 : return -1;
343 :
344 : /* exclude also the ".tmp" copy used to save it */
345 16773116 : pathprint(tmp, sizeof(tmp), "%s.tmp", content->content);
346 16773116 : if (pathcmp(tmp, path) == 0)
347 0 : return -1;
348 :
349 : /* exclude also the ".lock" file */
350 16773116 : pathprint(tmp, sizeof(tmp), "%s.lock", content->content);
351 16773116 : if (pathcmp(tmp, path) == 0)
352 0 : return -1;
353 : }
354 :
355 3066757 : return 0;
356 : }
357 :
358 3565018 : 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)
359 : {
360 : struct snapraid_file* file;
361 : block_off_t i;
362 :
363 3565018 : file = malloc_nofail(sizeof(struct snapraid_file));
364 3565018 : file->sub = strdup_nofail(sub);
365 3565018 : file->size = size;
366 3565018 : file->blockmax = (size + block_size - 1) / block_size;
367 3565018 : file->mtime_sec = mtime_sec;
368 3565018 : file->mtime_nsec = mtime_nsec;
369 3565018 : file->inode = inode;
370 3565018 : file->physical = physical;
371 3565018 : file->flag = 0;
372 3565018 : file->blockvec = malloc_nofail(file->blockmax * sizeof(struct snapraid_block));
373 :
374 12361466 : for (i = 0; i < file->blockmax; ++i) {
375 8796448 : block_state_set(&file->blockvec[i], BLOCK_STATE_CHG);
376 8796448 : hash_invalid_set(file->blockvec[i].hash);
377 : }
378 :
379 3565018 : return file;
380 : }
381 :
382 21784 : struct snapraid_file* file_dup(struct snapraid_file* copy)
383 : {
384 : struct snapraid_file* file;
385 : block_off_t i;
386 :
387 21784 : file = malloc_nofail(sizeof(struct snapraid_file));
388 21784 : file->sub = strdup_nofail(copy->sub);
389 21784 : file->size = copy->size;
390 21784 : file->blockmax = copy->blockmax;
391 21784 : file->mtime_sec = copy->mtime_sec;
392 21784 : file->mtime_nsec = copy->mtime_nsec;
393 21784 : file->inode = copy->inode;
394 21784 : file->physical = copy->physical;
395 21784 : file->flag = copy->flag;
396 21784 : file->blockvec = malloc_nofail(file->blockmax * sizeof(struct snapraid_block));
397 :
398 75416 : for (i = 0; i < file->blockmax; ++i) {
399 53632 : file->blockvec[i].state = copy->blockvec[i].state;
400 53632 : memcpy(file->blockvec[i].hash, copy->blockvec[i].hash, HASH_SIZE);
401 : }
402 :
403 21784 : return file;
404 : }
405 :
406 3417005 : void file_free(struct snapraid_file* file)
407 : {
408 3417005 : free(file->sub);
409 3417005 : file->sub = 0;
410 3417005 : free(file->blockvec);
411 3417005 : file->blockvec = 0;
412 3417005 : free(file);
413 3417005 : }
414 :
415 155 : void file_rename(struct snapraid_file* file, const char* sub)
416 : {
417 155 : free(file->sub);
418 155 : file->sub = strdup_nofail(sub);
419 155 : }
420 :
421 6209 : void file_copy(struct snapraid_file* src_file, struct snapraid_file* dst_file)
422 : {
423 : block_off_t i;
424 :
425 6209 : if (src_file->size != dst_file->size) {
426 : /* LCOV_EXCL_START */
427 : log_fatal("Internal inconsistency in copy file with different size\n");
428 : exit(EXIT_FAILURE);
429 : /* LCOV_EXCL_STOP */
430 : }
431 :
432 6209 : if (src_file->mtime_sec != dst_file->mtime_sec) {
433 : /* LCOV_EXCL_START */
434 : log_fatal("Internal inconsistency in copy file with different mtime_sec\n");
435 : exit(EXIT_FAILURE);
436 : /* LCOV_EXCL_STOP */
437 : }
438 :
439 6209 : if (src_file->mtime_nsec != dst_file->mtime_nsec) {
440 : /* LCOV_EXCL_START */
441 : log_fatal("Internal inconsistency in copy file with different mtime_nsec\n");
442 : exit(EXIT_FAILURE);
443 : /* LCOV_EXCL_STOP */
444 : }
445 :
446 17910 : for (i = 0; i < dst_file->blockmax; ++i) {
447 : /* set a block with hash computed but without parity */
448 11701 : block_state_set(&dst_file->blockvec[i], BLOCK_STATE_REP);
449 :
450 : /* copy the hash */
451 11701 : memcpy(dst_file->blockvec[i].hash, src_file->blockvec[i].hash, HASH_SIZE);
452 : }
453 :
454 6209 : file_flag_set(dst_file, FILE_IS_COPY);
455 6209 : }
456 :
457 47858 : const char* file_name(const struct snapraid_file* file)
458 : {
459 47858 : const char* r = strrchr(file->sub, '/');
460 :
461 47858 : if (!r)
462 8 : r = file->sub;
463 : else
464 47850 : ++r;
465 47858 : return r;
466 : }
467 :
468 6609423 : unsigned file_block_size(struct snapraid_file* file, block_off_t file_pos, unsigned block_size)
469 : {
470 : /* if it's the last block */
471 6609423 : if (file_pos + 1 == file->blockmax) {
472 : unsigned block_remainder;
473 2692095 : if (file->size == 0)
474 0 : return 0;
475 2692095 : block_remainder = file->size % block_size;
476 2692095 : if (block_remainder == 0)
477 1567 : block_remainder = block_size;
478 2692095 : return block_remainder;
479 : }
480 :
481 3917328 : return block_size;
482 : }
483 :
484 5319910 : int file_block_is_last(struct snapraid_file* file, block_off_t file_pos)
485 : {
486 5319910 : if (file_pos == 0 && file->blockmax == 0)
487 0 : return 1;
488 :
489 5319910 : if (file_pos >= file->blockmax) {
490 : /* LCOV_EXCL_START */
491 : log_fatal("Internal inconsistency in file block position\n");
492 : exit(EXIT_FAILURE);
493 : /* LCOV_EXCL_STOP */
494 : }
495 :
496 5319910 : return file_pos == file->blockmax - 1;
497 : }
498 :
499 869924 : int file_inode_compare_to_arg(const void* void_arg, const void* void_data)
500 : {
501 869924 : const uint64_t* arg = void_arg;
502 869924 : const struct snapraid_file* file = void_data;
503 :
504 869924 : if (*arg < file->inode)
505 0 : return -1;
506 869924 : if (*arg > file->inode)
507 0 : return 1;
508 869924 : return 0;
509 : }
510 :
511 0 : int file_inode_compare(const void* void_a, const void* void_b)
512 : {
513 0 : const struct snapraid_file* file_a = void_a;
514 0 : const struct snapraid_file* file_b = void_b;
515 :
516 0 : if (file_a->inode < file_b->inode)
517 0 : return -1;
518 0 : if (file_a->inode > file_b->inode)
519 0 : return 1;
520 0 : return 0;
521 : }
522 :
523 126320 : int file_path_compare(const void* void_a, const void* void_b)
524 : {
525 126320 : const struct snapraid_file* file_a = void_a;
526 126320 : const struct snapraid_file* file_b = void_b;
527 :
528 126320 : return strcmp(file_a->sub, file_b->sub);
529 : }
530 :
531 54526 : int file_physical_compare(const void* void_a, const void* void_b)
532 : {
533 54526 : const struct snapraid_file* file_a = void_a;
534 54526 : const struct snapraid_file* file_b = void_b;
535 :
536 54526 : if (file_a->physical < file_b->physical)
537 35814 : return -1;
538 18712 : if (file_a->physical > file_b->physical)
539 18712 : return 1;
540 0 : return 0;
541 : }
542 :
543 105086 : int file_path_compare_to_arg(const void* void_arg, const void* void_data)
544 : {
545 105086 : const char* arg = void_arg;
546 105086 : const struct snapraid_file* file = void_data;
547 :
548 105086 : return strcmp(arg, file->sub);
549 : }
550 :
551 23929 : int file_name_compare(const void* void_a, const void* void_b)
552 : {
553 23929 : const struct snapraid_file* file_a = void_a;
554 23929 : const struct snapraid_file* file_b = void_b;
555 23929 : const char* name_a = file_name(file_a);
556 23929 : const char* name_b = file_name(file_b);
557 :
558 23929 : return strcmp(name_a, name_b);
559 : }
560 :
561 23659 : int file_stamp_compare(const void* void_a, const void* void_b)
562 : {
563 23659 : const struct snapraid_file* file_a = void_a;
564 23659 : const struct snapraid_file* file_b = void_b;
565 :
566 23659 : if (file_a->size < file_b->size)
567 0 : return -1;
568 23659 : if (file_a->size > file_b->size)
569 0 : return 1;
570 :
571 23659 : if (file_a->mtime_sec < file_b->mtime_sec)
572 0 : return -1;
573 23659 : if (file_a->mtime_sec > file_b->mtime_sec)
574 0 : return 1;
575 :
576 23659 : if (file_a->mtime_nsec < file_b->mtime_nsec)
577 0 : return -1;
578 23659 : if (file_a->mtime_nsec > file_b->mtime_nsec)
579 0 : return 1;
580 :
581 23659 : return 0;
582 : }
583 :
584 23929 : int file_namestamp_compare(const void* void_a, const void* void_b)
585 : {
586 : int ret;
587 :
588 23929 : ret = file_name_compare(void_a, void_b);
589 23929 : if (ret != 0)
590 270 : return ret;
591 :
592 23659 : return file_stamp_compare(void_a, void_b);
593 : }
594 :
595 0 : int file_pathstamp_compare(const void* void_a, const void* void_b)
596 : {
597 : int ret;
598 :
599 0 : ret = file_path_compare(void_a, void_b);
600 0 : if (ret != 0)
601 0 : return ret;
602 :
603 0 : return file_stamp_compare(void_a, void_b);
604 : }
605 :
606 3607803 : struct snapraid_chunk* chunk_alloc(block_off_t parity_pos, struct snapraid_file* file, block_off_t file_pos, block_off_t count)
607 : {
608 : struct snapraid_chunk* chunk;
609 :
610 3607803 : chunk = malloc_nofail(sizeof(struct snapraid_chunk));
611 3607803 : chunk->parity_pos = parity_pos;
612 3607803 : chunk->file = file;
613 3607803 : chunk->file_pos = file_pos;
614 3607803 : chunk->count = count;
615 :
616 3607803 : return chunk;
617 : }
618 :
619 3437425 : void chunk_free(struct snapraid_chunk* chunk)
620 : {
621 3437425 : free(chunk);
622 3437425 : }
623 :
624 43029334 : int chunk_parity_compare(const void* void_a, const void* void_b)
625 : {
626 43029334 : const struct snapraid_chunk* arg_a = void_a;
627 43029334 : const struct snapraid_chunk* arg_b = void_b;
628 :
629 43029334 : if (arg_a->parity_pos < arg_b->parity_pos)
630 931264 : return -1;
631 42098070 : if (arg_a->parity_pos > arg_b->parity_pos)
632 42059274 : return 1;
633 :
634 38796 : return 0;
635 : }
636 :
637 43527266 : int chunk_file_compare(const void* void_a, const void* void_b)
638 : {
639 43527266 : const struct snapraid_chunk* arg_a = void_a;
640 43527266 : const struct snapraid_chunk* arg_b = void_b;
641 :
642 43527266 : if (arg_a->file < arg_b->file)
643 855502 : return -1;
644 42671764 : if (arg_a->file > arg_b->file)
645 42603010 : return 1;
646 :
647 68754 : if (arg_a->file_pos < arg_b->file_pos)
648 192 : return -1;
649 68562 : if (arg_a->file_pos > arg_b->file_pos)
650 29766 : return 1;
651 :
652 38796 : return 0;
653 : }
654 :
655 115419 : struct snapraid_link* link_alloc(const char* sub, const char* linkto, unsigned link_flag)
656 : {
657 : struct snapraid_link* slink;
658 :
659 115419 : slink = malloc_nofail(sizeof(struct snapraid_link));
660 115419 : slink->sub = strdup_nofail(sub);
661 115419 : slink->linkto = strdup_nofail(linkto);
662 115419 : slink->flag = link_flag;
663 :
664 115419 : return slink;
665 : }
666 :
667 109621 : void link_free(struct snapraid_link* slink)
668 : {
669 109621 : free(slink->sub);
670 109621 : free(slink->linkto);
671 109621 : free(slink);
672 109621 : }
673 :
674 30380 : int link_name_compare_to_arg(const void* void_arg, const void* void_data)
675 : {
676 30380 : const char* arg = void_arg;
677 30380 : const struct snapraid_link* slink = void_data;
678 :
679 30380 : return strcmp(arg, slink->sub);
680 : }
681 :
682 1249 : int link_alpha_compare(const void* void_a, const void* void_b)
683 : {
684 1249 : const struct snapraid_link* slink_a = void_a;
685 1249 : const struct snapraid_link* slink_b = void_b;
686 :
687 1249 : return strcmp(slink_a->sub, slink_b->sub);
688 : }
689 :
690 760 : struct snapraid_dir* dir_alloc(const char* sub)
691 : {
692 : struct snapraid_dir* dir;
693 :
694 760 : dir = malloc_nofail(sizeof(struct snapraid_dir));
695 760 : dir->sub = strdup_nofail(sub);
696 760 : dir->flag = 0;
697 :
698 760 : return dir;
699 : }
700 :
701 712 : void dir_free(struct snapraid_dir* dir)
702 : {
703 712 : free(dir->sub);
704 712 : free(dir);
705 712 : }
706 :
707 217 : int dir_name_compare(const void* void_arg, const void* void_data)
708 : {
709 217 : const char* arg = void_arg;
710 217 : const struct snapraid_dir* dir = void_data;
711 :
712 217 : return strcmp(arg, dir->sub);
713 : }
714 :
715 1477 : struct snapraid_disk* disk_alloc(const char* name, const char* dir, uint64_t dev, int skip)
716 : {
717 : struct snapraid_disk* disk;
718 :
719 1477 : disk = malloc_nofail(sizeof(struct snapraid_disk));
720 1477 : pathcpy(disk->name, sizeof(disk->name), name);
721 1477 : pathimport(disk->dir, sizeof(disk->dir), dir);
722 :
723 : /* ensure that the dir terminate with "/" if it isn't empty */
724 1477 : pathslash(disk->dir, sizeof(disk->dir));
725 :
726 1477 : disk->smartctl[0] = 0;
727 1477 : disk->device = dev;
728 1477 : disk->tick = 0;
729 1477 : disk->total_blocks = 0;
730 1477 : disk->free_blocks = 0;
731 1477 : disk->first_free_block = 0;
732 1477 : disk->has_volatile_inodes = 0;
733 1477 : disk->has_unreliable_physical = 0;
734 1477 : disk->has_different_uuid = 0;
735 1477 : disk->has_unsupported_uuid = 0;
736 1477 : disk->had_empty_uuid = 0;
737 1477 : disk->mapping_idx = -1;
738 1477 : disk->skip_access = skip;
739 1477 : tommy_list_init(&disk->filelist);
740 1477 : tommy_list_init(&disk->deletedlist);
741 1477 : tommy_hashdyn_init(&disk->inodeset);
742 1477 : tommy_hashdyn_init(&disk->pathset);
743 1477 : tommy_hashdyn_init(&disk->stampset);
744 1477 : tommy_list_init(&disk->linklist);
745 1477 : tommy_hashdyn_init(&disk->linkset);
746 1477 : tommy_list_init(&disk->dirlist);
747 1477 : tommy_hashdyn_init(&disk->dirset);
748 1477 : tommy_tree_init(&disk->fs_parity, chunk_parity_compare);
749 1477 : tommy_tree_init(&disk->fs_file, chunk_file_compare);
750 1477 : disk->fs_last = 0;
751 :
752 1477 : return disk;
753 : }
754 :
755 1351 : void disk_free(struct snapraid_disk* disk)
756 : {
757 1351 : tommy_list_foreach(&disk->filelist, (tommy_foreach_func*)file_free);
758 1351 : tommy_list_foreach(&disk->deletedlist, (tommy_foreach_func*)file_free);
759 1351 : tommy_tree_foreach(&disk->fs_file, (tommy_foreach_func*)chunk_free);
760 1351 : tommy_hashdyn_done(&disk->inodeset);
761 1351 : tommy_hashdyn_done(&disk->pathset);
762 1351 : tommy_hashdyn_done(&disk->stampset);
763 1351 : tommy_list_foreach(&disk->linklist, (tommy_foreach_func*)link_free);
764 1351 : tommy_hashdyn_done(&disk->linkset);
765 1351 : tommy_list_foreach(&disk->dirlist, (tommy_foreach_func*)dir_free);
766 1351 : tommy_hashdyn_done(&disk->dirset);
767 1351 : free(disk);
768 1351 : }
769 :
770 : struct chunk_disk_empty {
771 : block_off_t blockmax;
772 : };
773 :
774 : /**
775 : * Compare the chunk if inside the specified blockmax.
776 : */
777 10 : static int chunk_disk_empty_compare(const void* void_a, const void* void_b)
778 : {
779 10 : const struct chunk_disk_empty* arg_a = void_a;
780 10 : const struct snapraid_chunk* arg_b = void_b;
781 :
782 : /* if the block is inside the specified blockmax, it's found */
783 10 : if (arg_a->blockmax > arg_b->parity_pos)
784 5 : return 0;
785 :
786 : /* otherwise search for a smaller one */
787 5 : return -1;
788 : }
789 :
790 804 : int disk_is_empty(struct snapraid_disk* disk, block_off_t blockmax)
791 : {
792 804 : struct chunk_disk_empty arg = { blockmax };
793 :
794 : /* if there is an element, it's not empty */
795 : /* even if links and dirs have no block allocation */
796 804 : if (!tommy_list_empty(&disk->filelist))
797 736 : return 0;
798 68 : if (!tommy_list_empty(&disk->linklist))
799 0 : return 0;
800 68 : if (!tommy_list_empty(&disk->dirlist))
801 0 : return 0;
802 :
803 : /* search for any chunk inside blockmax */
804 68 : if (tommy_tree_search_compare(&disk->fs_parity, chunk_disk_empty_compare, &arg) != 0)
805 5 : return 0;
806 :
807 : /* finally, it's empty */
808 63 : return 1;
809 : }
810 :
811 : struct chunk_disk_size {
812 : block_off_t size;
813 : };
814 :
815 : /**
816 : * Compare the chunk by highest parity position.
817 : *
818 : * The maximum parity position is stored as size.
819 : */
820 47777 : static int chunk_disk_size_compare(const void* void_a, const void* void_b)
821 : {
822 47777 : struct chunk_disk_size* arg_a = (void*)void_a;
823 47777 : const struct snapraid_chunk* arg_b = void_b;
824 :
825 : /* get the maximum size */
826 47777 : if (arg_a->size < arg_b->parity_pos + arg_b->count)
827 47777 : arg_a->size = arg_b->parity_pos + arg_b->count;
828 :
829 : /* search always for a bigger one */
830 47777 : return 1;
831 : }
832 :
833 3955 : block_off_t disk_size(struct snapraid_disk* disk)
834 : {
835 3955 : struct chunk_disk_size arg = { 0 };
836 3955 : tommy_tree_search_compare(&disk->fs_parity, chunk_disk_size_compare, &arg);
837 3955 : return arg.size;
838 : }
839 :
840 : struct chunk_check {
841 : const struct snapraid_chunk* prev;
842 : int result;
843 : };
844 :
845 6291272 : void chunk_parity_check_foreach(void* void_arg, void* void_obj)
846 : {
847 6291272 : struct chunk_check* arg = void_arg;
848 6291272 : const struct snapraid_chunk* obj = void_obj;
849 6291272 : const struct snapraid_chunk* prev = arg->prev;
850 :
851 : /* set the next previous block */
852 6291272 : arg->prev = obj;
853 :
854 : /* stop reporting if too many errors */
855 6291272 : if (arg->result > 100) {
856 : /* LCOV_EXCL_START */
857 : return;
858 : /* LCOV_EXCL_STOP */
859 : }
860 :
861 : /* check only if there is a previous block */
862 6291272 : if (!prev)
863 2473 : return;
864 :
865 : /* check the order */
866 6288799 : if (prev->parity_pos >= obj->parity_pos) {
867 : /* LCOV_EXCL_START */
868 : log_fatal("Internal inconsistency in parity order for files '%s' at '%u:%u' and '%s' at '%u:%u'\n",
869 : prev->file->sub, prev->parity_pos, prev->count, obj->file->sub, obj->parity_pos, obj->count);
870 : ++arg->result;
871 : return;
872 : /* LCOV_EXCL_STOP */
873 : }
874 :
875 : /* check that the chunks don't overlap */
876 6288799 : if (prev->parity_pos + prev->count > obj->parity_pos) {
877 : /* LCOV_EXCL_START */
878 : log_fatal("Internal inconsistency for parity overlap for files '%s' at '%u:%u' and '%s' at '%u:%u'\n",
879 : prev->file->sub, prev->parity_pos, prev->count, obj->file->sub, obj->parity_pos, obj->count);
880 : ++arg->result;
881 : return;
882 : /* LCOV_EXCL_STOP */
883 : }
884 : }
885 :
886 6291272 : void chunk_file_check_foreach(void* void_arg, void* void_obj)
887 : {
888 6291272 : struct chunk_check* arg = void_arg;
889 6291272 : const struct snapraid_chunk* obj = void_obj;
890 6291272 : const struct snapraid_chunk* prev = arg->prev;
891 :
892 : /* set the next previous block */
893 6291272 : arg->prev = obj;
894 :
895 : /* stop reporting if too many errors */
896 6291272 : if (arg->result > 100) {
897 : /* LCOV_EXCL_START */
898 : return;
899 : /* LCOV_EXCL_STOP */
900 : }
901 :
902 : /* note that for deleted files, some chunks may be missing */
903 :
904 : /* if the files are different */
905 6291272 : if (!prev || prev->file != obj->file) {
906 6247390 : if (prev != 0) {
907 6244917 : if (file_flag_has(prev->file, FILE_IS_DELETED)) {
908 : /* check that the chunk doesn't overflow the file */
909 59701 : if (prev->file_pos + prev->count > prev->file->blockmax) {
910 : /* LCOV_EXCL_START */
911 : log_fatal("Internal inconsistency in delete end for file '%s' at '%u:%u' overflowing size '%u'\n",
912 : prev->file->sub, prev->file_pos, prev->count, prev->file->blockmax);
913 : ++arg->result;
914 : return;
915 : /* LCOV_EXCL_STOP */
916 : }
917 : } else {
918 : /* check that the chunk ends the file */
919 6185216 : if (prev->file_pos + prev->count != prev->file->blockmax) {
920 : /* LCOV_EXCL_START */
921 : log_fatal("Internal inconsistency in file end for file '%s' at '%u:%u' instead of size '%u'\n",
922 : prev->file->sub, prev->file_pos, prev->count, prev->file->blockmax);
923 : ++arg->result;
924 : return;
925 : /* LCOV_EXCL_STOP */
926 : }
927 : }
928 : }
929 :
930 12494780 : if (file_flag_has(obj->file, FILE_IS_DELETED)) {
931 : /* check that the chunk doesn't overflow the file */
932 59776 : if (obj->file_pos + obj->count > obj->file->blockmax) {
933 : /* LCOV_EXCL_START */
934 : log_fatal("Internal inconsistency in delete start for file '%s' at '%u:%u' overflowing size '%u'\n",
935 : obj->file->sub, obj->file_pos, obj->count, obj->file->blockmax);
936 : ++arg->result;
937 : return;
938 : /* LCOV_EXCL_STOP */
939 : }
940 : } else {
941 : /* check that the chunk starts the file */
942 6187614 : if (obj->file_pos != 0) {
943 : /* LCOV_EXCL_START */
944 : log_fatal("Internal inconsistency in file start for file '%s' at '%u:%u'\n",
945 : obj->file->sub, obj->file_pos, obj->count);
946 : ++arg->result;
947 : return;
948 : /* LCOV_EXCL_STOP */
949 : }
950 : }
951 : } else {
952 : /* check the order */
953 43882 : if (prev->file_pos >= obj->file_pos) {
954 : /* LCOV_EXCL_START */
955 : log_fatal("Internal inconsistency in file order for file '%s' at '%u:%u' and at '%u:%u'\n",
956 : prev->file->sub, prev->file_pos, prev->count, obj->file_pos, obj->count);
957 : ++arg->result;
958 : return;
959 : /* LCOV_EXCL_STOP */
960 : }
961 :
962 43882 : if (file_flag_has(obj->file, FILE_IS_DELETED)) {
963 : /* check that the chunks don't overlap */
964 27 : if (prev->file_pos + prev->count > obj->file_pos) {
965 : /* LCOV_EXCL_START */
966 : log_fatal("Internal inconsistency in delete sequence for file '%s' at '%u:%u' and at '%u:%u'\n",
967 : prev->file->sub, prev->file_pos, prev->count, obj->file_pos, obj->count);
968 : ++arg->result;
969 : return;
970 : /* LCOV_EXCL_STOP */
971 : }
972 : } else {
973 : /* check that the chunks are sequential */
974 43855 : if (prev->file_pos + prev->count != obj->file_pos) {
975 : /* LCOV_EXCL_START */
976 : log_fatal("Internal inconsistency in file sequence for file '%s' at '%u:%u' and at '%u:%u'\n",
977 : prev->file->sub, prev->file_pos, prev->count, obj->file_pos, obj->count);
978 : ++arg->result;
979 : return;
980 : /* LCOV_EXCL_STOP */
981 : }
982 : }
983 : }
984 : }
985 :
986 2676 : int fs_check(struct snapraid_disk* disk)
987 : {
988 : struct chunk_check arg;
989 :
990 : /* error count starts from 0 */
991 2676 : arg.result = 0;
992 :
993 : /* check parity sequence */
994 2676 : arg.prev = 0;
995 2676 : tommy_tree_foreach_arg(&disk->fs_parity, chunk_parity_check_foreach, &arg);
996 :
997 : /* check file sequence */
998 2676 : arg.prev = 0;
999 2676 : tommy_tree_foreach_arg(&disk->fs_file, chunk_file_check_foreach, &arg);
1000 :
1001 2676 : if (arg.result != 0)
1002 0 : return -1;
1003 :
1004 2676 : return 0;
1005 : }
1006 :
1007 : struct chunk_parity_inside {
1008 : block_off_t parity_pos;
1009 : };
1010 :
1011 : /**
1012 : * Compare the chunk if containing the specified parity position.
1013 : */
1014 213182654 : static int chunk_parity_inside_compare(const void* void_a, const void* void_b)
1015 : {
1016 213182654 : const struct chunk_parity_inside* arg_a = void_a;
1017 213182654 : const struct snapraid_chunk* arg_b = void_b;
1018 :
1019 213182654 : if (arg_a->parity_pos < arg_b->parity_pos)
1020 79586474 : return -1;
1021 133596180 : if (arg_a->parity_pos >= arg_b->parity_pos + arg_b->count)
1022 116201011 : return 1;
1023 :
1024 17395169 : return 0;
1025 : }
1026 :
1027 : /**
1028 : * Seach the chunk at the specified parity position.
1029 : * The search is optimized for sequential accesses.
1030 : * \return If not found return 0
1031 : */
1032 62657428 : static struct snapraid_chunk* fs_par2chunk_get_ts(struct snapraid_disk* disk, struct snapraid_chunk** fs_last, block_off_t parity_pos)
1033 : {
1034 : struct snapraid_chunk* chunk;
1035 :
1036 : /* check if the last accessed chunk matches */
1037 62657428 : if (*fs_last
1038 62843273 : && parity_pos >= (*fs_last)->parity_pos
1039 62738070 : && parity_pos < (*fs_last)->parity_pos + (*fs_last)->count
1040 : ) {
1041 43160159 : chunk = *fs_last;
1042 : } else {
1043 19497269 : struct chunk_parity_inside arg = { parity_pos };
1044 19497269 : chunk = tommy_tree_search_compare(&disk->fs_parity, chunk_parity_inside_compare, &arg);
1045 : }
1046 :
1047 62844816 : if (!chunk)
1048 2353186 : return 0;
1049 :
1050 : /* store the last accessed chunk */
1051 60491630 : *fs_last = chunk;
1052 :
1053 60491630 : return chunk;
1054 : }
1055 :
1056 : struct chunk_file_inside {
1057 : struct snapraid_file* file;
1058 : block_off_t file_pos;
1059 : };
1060 :
1061 : /**
1062 : * Compare the chunk if containing the specified file position.
1063 : */
1064 123631586 : static int chunk_file_inside_compare(const void* void_a, const void* void_b)
1065 : {
1066 123631586 : const struct chunk_file_inside* arg_a = void_a;
1067 123631586 : const struct snapraid_chunk* arg_b = void_b;
1068 :
1069 123631586 : if (arg_a->file < arg_b->file)
1070 52889679 : return -1;
1071 70741907 : if (arg_a->file > arg_b->file)
1072 58811856 : return 1;
1073 :
1074 11930051 : if (arg_a->file_pos < arg_b->file_pos)
1075 48627 : return -1;
1076 11881424 : if (arg_a->file_pos >= arg_b->file_pos + arg_b->count)
1077 44495 : return 1;
1078 :
1079 11836929 : return 0;
1080 : }
1081 :
1082 : /**
1083 : * Seach the chunk at the specified file position.
1084 : * The search is optimized for sequential accesses.
1085 : * \return If not found return 0
1086 : */
1087 34130326 : static struct snapraid_chunk* fs_file2chunk_get_ts(struct snapraid_disk* disk, struct snapraid_chunk** fs_last, struct snapraid_file* file, block_off_t file_pos)
1088 : {
1089 : struct snapraid_chunk* chunk;
1090 :
1091 : /* check if the last accessed chunk matches */
1092 34130326 : if (*fs_last
1093 34328656 : && file == (*fs_last)->file
1094 22569402 : && file_pos >= (*fs_last)->file_pos
1095 22577024 : && file_pos < (*fs_last)->file_pos + (*fs_last)->count
1096 : ) {
1097 22445049 : chunk = *fs_last;
1098 : } else {
1099 11685277 : struct chunk_file_inside arg = { file, file_pos };
1100 11685277 : chunk = tommy_tree_search_compare(&disk->fs_file, chunk_file_inside_compare, &arg);
1101 : }
1102 :
1103 34258963 : if (!chunk)
1104 0 : return 0;
1105 :
1106 : /* store the last accessed chunk */
1107 34258963 : *fs_last = chunk;
1108 :
1109 34258963 : return chunk;
1110 : }
1111 :
1112 62727960 : 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)
1113 : {
1114 : struct snapraid_chunk* chunk;
1115 :
1116 62727960 : chunk = fs_par2chunk_get_ts(disk, fs_last, parity_pos);
1117 62761969 : if (!chunk)
1118 2353128 : return 0;
1119 :
1120 60408841 : if (file_pos)
1121 59975482 : *file_pos = chunk->file_pos + (parity_pos - chunk->parity_pos);
1122 :
1123 60408841 : return chunk->file;
1124 : }
1125 :
1126 28966119 : 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)
1127 : {
1128 : struct snapraid_chunk* chunk;
1129 :
1130 28966119 : chunk = fs_file2chunk_get_ts(disk, fs_last, file, file_pos);
1131 28884994 : if (!chunk) {
1132 : /* LCOV_EXCL_START */
1133 : log_fatal("Internal inconsistency when resolving file '%s' at position '%u' in disk '%s'\n", file->sub, file_pos, disk->name);
1134 : exit(EXIT_FAILURE);
1135 : /* LCOV_EXCL_STOP */
1136 : }
1137 :
1138 28884994 : return chunk->parity_pos + (file_pos - chunk->file_pos);
1139 : }
1140 :
1141 8850076 : void fs_allocate(struct snapraid_disk* disk, block_off_t parity_pos, struct snapraid_file* file, block_off_t file_pos)
1142 : {
1143 : struct snapraid_chunk* chunk;
1144 : struct snapraid_chunk* parity_chunk;
1145 : struct snapraid_chunk* file_chunk;
1146 :
1147 8850076 : if (file_pos > 0) {
1148 : /* search an existing chunk for the previous file_pos */
1149 5267929 : chunk = fs_file2chunk_get_ts(disk, &disk->fs_last, file, file_pos - 1);
1150 :
1151 5267929 : if (chunk != 0 && parity_pos == chunk->parity_pos + chunk->count) {
1152 : /* ensure that we are extending the chunk at the end */
1153 5242274 : if (file_pos != chunk->file_pos + chunk->count) {
1154 : /* LCOV_EXCL_START */
1155 : log_fatal("Internal inconsistency when allocating file '%s' at position '%u' in the middle of chunk '%u:%u' in disk '%s'\n", file->sub, file_pos, chunk->file_pos, chunk->count, disk->name);
1156 : exit(EXIT_FAILURE);
1157 : /* LCOV_EXCL_STOP */
1158 : }
1159 :
1160 : /* extend the existing chunk */
1161 5242274 : ++chunk->count;
1162 :
1163 14092350 : return;
1164 : }
1165 : }
1166 :
1167 : /* a chunk doesn't exist, and we have to create a new one */
1168 3607802 : chunk = chunk_alloc(parity_pos, file, file_pos, 1);
1169 :
1170 : /* insert the chunk in the trees */
1171 3607802 : parity_chunk = tommy_tree_insert(&disk->fs_parity, &chunk->parity_node, chunk);
1172 3607802 : file_chunk = tommy_tree_insert(&disk->fs_file, &chunk->file_node, chunk);
1173 :
1174 3607802 : if (parity_chunk != chunk || file_chunk != chunk) {
1175 : /* LCOV_EXCL_START */
1176 : log_fatal("Internal inconsistency when allocating file '%s' at position '%u' for existing chunk '%u:%u' in disk '%s'\n", file->sub, file_pos, chunk->file_pos, chunk->count, disk->name);
1177 : exit(EXIT_FAILURE);
1178 : /* LCOV_EXCL_STOP */
1179 : }
1180 :
1181 : /* store the last accessed chunk */
1182 3607802 : disk->fs_last = chunk;
1183 : }
1184 :
1185 104031 : void fs_deallocate(struct snapraid_disk* disk, block_off_t parity_pos)
1186 : {
1187 : struct snapraid_chunk* chunk;
1188 : struct snapraid_chunk* second_chunk;
1189 : struct snapraid_chunk* parity_chunk;
1190 : struct snapraid_chunk* file_chunk;
1191 : block_off_t first_count, second_count;
1192 :
1193 104031 : chunk = fs_par2chunk_get_ts(disk, &disk->fs_last, parity_pos);
1194 104031 : if (!chunk) {
1195 : /* LCOV_EXCL_START */
1196 : log_fatal("Internal inconsistency when deallocating parity position '%u' for not existing chunk in disk '%s'\n", parity_pos, disk->name);
1197 : exit(EXIT_FAILURE);
1198 : /* LCOV_EXCL_STOP */
1199 : }
1200 :
1201 : /* if it's the only block of the chunk, delete it */
1202 104031 : if (chunk->count == 1) {
1203 : /* remove from the trees */
1204 38796 : tommy_tree_remove(&disk->fs_parity, chunk);
1205 38796 : tommy_tree_remove(&disk->fs_file, chunk);
1206 :
1207 : /* deallocate */
1208 38796 : chunk_free(chunk);
1209 :
1210 : /* clear the last accessed chunk */
1211 38796 : disk->fs_last = 0;
1212 38796 : return;
1213 : }
1214 :
1215 : /* if it's at the start of the chunk, shrink the chunk */
1216 65235 : if (parity_pos == chunk->parity_pos) {
1217 65233 : ++chunk->parity_pos;
1218 65233 : ++chunk->file_pos;
1219 65233 : --chunk->count;
1220 65233 : return;
1221 : }
1222 :
1223 : /* if it's at the end of the chunk, shrink the chunk */
1224 2 : if (parity_pos == chunk->parity_pos + chunk->count - 1) {
1225 1 : --chunk->count;
1226 1 : return;
1227 : }
1228 :
1229 : /* otherwise it's in the middle */
1230 1 : first_count = parity_pos - chunk->parity_pos;
1231 1 : second_count = chunk->count - first_count - 1;
1232 :
1233 : /* adjust the first chunk */
1234 1 : chunk->count = first_count;
1235 :
1236 : /* allocate the second chunk */
1237 1 : second_chunk = chunk_alloc(chunk->parity_pos + first_count + 1, chunk->file, chunk->file_pos + first_count + 1, second_count);
1238 :
1239 : /* insert the chunk in the trees */
1240 1 : parity_chunk = tommy_tree_insert(&disk->fs_parity, &second_chunk->parity_node, second_chunk);
1241 1 : file_chunk = tommy_tree_insert(&disk->fs_file, &second_chunk->file_node, second_chunk);
1242 :
1243 1 : if (parity_chunk != second_chunk || file_chunk != second_chunk) {
1244 : /* LCOV_EXCL_START */
1245 : log_fatal("Internal inconsistency when deallocating parity position '%u' for splitting chunk '%u:%u' in disk '%s'\n", parity_pos, second_chunk->file_pos, second_chunk->count, disk->name);
1246 : exit(EXIT_FAILURE);
1247 : /* LCOV_EXCL_STOP */
1248 : }
1249 :
1250 : /* store the last accessed chunk */
1251 1 : disk->fs_last = second_chunk;
1252 : }
1253 :
1254 51081912 : struct snapraid_block* fs_par2block_get_ts(struct snapraid_disk* disk, struct snapraid_chunk** fs_last, block_off_t parity_pos)
1255 : {
1256 : struct snapraid_file* file;
1257 : block_off_t file_pos;
1258 :
1259 51081912 : file = fs_par2file_get_ts(disk, fs_last, parity_pos, &file_pos);
1260 :
1261 51089852 : if (!file)
1262 2353186 : return BLOCK_EMPTY;
1263 :
1264 48736666 : return fs_file2block_get(file, file_pos);
1265 : }
1266 :
1267 1447 : struct snapraid_map* map_alloc(const char* name, unsigned position, block_off_t total_blocks, block_off_t free_blocks, const char* uuid)
1268 : {
1269 : struct snapraid_map* map;
1270 :
1271 1447 : map = malloc_nofail(sizeof(struct snapraid_map));
1272 1447 : pathcpy(map->name, sizeof(map->name), name);
1273 1447 : map->position = position;
1274 1447 : map->total_blocks = total_blocks;
1275 1447 : map->free_blocks = free_blocks;
1276 1447 : pathcpy(map->uuid, sizeof(map->uuid), uuid);
1277 :
1278 1447 : return map;
1279 : }
1280 :
1281 1327 : void map_free(struct snapraid_map* map)
1282 : {
1283 1327 : free(map);
1284 1327 : }
1285 :
1286 713560 : int time_compare(const void* void_a, const void* void_b)
1287 : {
1288 713560 : const time_t* time_a = void_a;
1289 713560 : const time_t* time_b = void_b;
1290 :
1291 713560 : if (*time_a < *time_b)
1292 26114 : return -1;
1293 687446 : if (*time_a > *time_b)
1294 154206 : return 1;
1295 533240 : return 0;
1296 : }
|