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 1504 : struct snapraid_content* content_alloc(const char* path, uint64_t dev)
28 : {
29 : struct snapraid_content* content;
30 :
31 1504 : content = malloc_nofail(sizeof(struct snapraid_content));
32 1504 : pathimport(content->content, sizeof(content->content), path);
33 1504 : content->device = dev;
34 :
35 1504 : return content;
36 : }
37 :
38 1367 : void content_free(struct snapraid_content* content)
39 : {
40 1367 : free(content);
41 1367 : }
42 :
43 518 : 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 518 : filter = malloc_nofail(sizeof(struct snapraid_filter));
53 518 : pathimport(filter->pattern, sizeof(filter->pattern), pattern);
54 518 : filter->direction = direction;
55 :
56 : /* find first and last slash */
57 518 : first = 0;
58 518 : last = 0;
59 : /* reject invalid tokens, like "<empty>", ".", ".." and more dots */
60 518 : token_is_valid = 0;
61 518 : token_is_filled = 0;
62 6422 : for (i = filter->pattern; *i; ++i) {
63 5905 : 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 5900 : } else if (*i != '.') {
77 5386 : token_is_valid = 1;
78 5386 : token_is_filled = 1;
79 : } else {
80 514 : 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 517 : 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 516 : filter->is_disk = 0;
92 :
93 516 : if (first == 0) {
94 : /* no slash */
95 513 : filter->is_path = 0;
96 513 : 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 515 : 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 481 : void filter_free(struct snapraid_filter* filter)
147 : {
148 481 : free(filter);
149 481 : }
150 :
151 125183 : const char* filter_type(struct snapraid_filter* filter, char* out, size_t out_size)
152 : {
153 : const char* direction;
154 125183 : if (filter->direction < 0)
155 124927 : direction = "exclude";
156 : else
157 256 : direction = "include";
158 :
159 125183 : if (filter->is_disk)
160 0 : pathprint(out, out_size, "%s %s:", direction, filter->pattern);
161 125183 : else if (filter->is_dir)
162 0 : pathprint(out, out_size, "%s %s/", direction, filter->pattern);
163 : else
164 125183 : pathprint(out, out_size, "%s %s", direction, filter->pattern);
165 :
166 125183 : return out;
167 : }
168 :
169 12223998 : static int filter_apply(struct snapraid_filter* filter, struct snapraid_filter** reason, const char* path, const char* name, int is_dir)
170 : {
171 12223998 : int ret = 0;
172 :
173 : /* match dirs with dirs and files with files */
174 12223998 : if (filter->is_dir && !is_dir)
175 2949 : return 0;
176 12221049 : if (!filter->is_dir && is_dir)
177 6107090 : return 0;
178 :
179 6113959 : if (filter->is_path) {
180 : /* skip initial slash, as always missing from the path */
181 2949 : if (fnmatch(filter->pattern + 1, path, FNM_PATHNAME | FNM_CASEINSENSITIVE_FOR_WIN) == 0)
182 0 : ret = filter->direction;
183 : } else {
184 6111010 : if (fnmatch(filter->pattern, name, FNM_CASEINSENSITIVE_FOR_WIN) == 0)
185 127661 : ret = filter->direction;
186 : }
187 :
188 6113959 : if (reason != 0 && ret < 0)
189 124671 : *reason = filter;
190 :
191 6113959 : return ret;
192 : }
193 :
194 6119907 : 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 6119907 : pathcpy(path, sizeof(path), const_path);
201 :
202 : /* filter for all the directories */
203 6119907 : name = path;
204 88226852 : for (i = 0; path[i] != 0; ++i) {
205 82109935 : if (path[i] == '/') {
206 : /* set a terminator */
207 6107081 : path[i] = 0;
208 :
209 : /* filter the directory */
210 6107081 : if (filter_apply(filter, reason, path, name, 1) != 0)
211 2990 : return filter->direction;
212 :
213 : /* restore the slash */
214 6104091 : path[i] = '/';
215 :
216 : /* next name */
217 6104091 : name = path + i + 1;
218 : }
219 : }
220 :
221 : /* filter the final file */
222 6116917 : if (filter_apply(filter, reason, path, name, is_dir) != 0)
223 124671 : return filter->direction;
224 :
225 5992246 : return 0;
226 : }
227 :
228 3274485 : 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 3274485 : int direction = 1; /* by default include all */
233 :
234 : /* for each filter */
235 9368018 : for (i = tommy_list_head(filterlist); i != 0; i = i->next) {
236 : int ret;
237 6229872 : struct snapraid_filter* filter = i->data;
238 :
239 6229872 : if (filter->is_disk) {
240 109965 : if (fnmatch(filter->pattern, disk, FNM_CASEINSENSITIVE_FOR_WIN) == 0)
241 8678 : ret = filter->direction;
242 : else
243 101287 : ret = 0;
244 109965 : if (reason != 0 && ret < 0)
245 0 : *reason = filter;
246 : } else {
247 6119907 : ret = filter_recurse(filter, reason, sub, is_dir);
248 : }
249 :
250 6229872 : if (ret > 0) {
251 : /* include the file */
252 11668 : return 0;
253 6218204 : } else if (ret < 0) {
254 : /* exclude the file */
255 124671 : return -1;
256 : } else {
257 : /* default is opposite of the last filter */
258 6093533 : direction = -filter->direction;
259 6093533 : if (reason != 0 && direction < 0)
260 3052540 : *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 3138146 : if (is_dir)
268 3022 : return 0;
269 :
270 : /* files are excluded/included depending of the last rule processed */
271 3135124 : if (direction < 0)
272 57587 : return -1;
273 :
274 3077537 : return 0;
275 : }
276 :
277 3271462 : int filter_path(tommy_list* filterlist, struct snapraid_filter** reason, const char* disk, const char* sub)
278 : {
279 3271462 : return filter_element(filterlist, reason, disk, sub, 0);
280 : }
281 :
282 3023 : int filter_dir(tommy_list* filterlist, struct snapraid_filter** reason, const char* disk, const char* sub)
283 : {
284 3023 : return filter_element(filterlist, reason, disk, sub, 1);
285 : }
286 :
287 80702 : int filter_existence(int filter_missing, const char* dir, const char* sub)
288 : {
289 : char path[PATH_MAX];
290 : struct stat st;
291 :
292 80702 : if (!filter_missing)
293 34986 : return 0;
294 :
295 : /* we directly check if in the disk the file is present or not */
296 45716 : pathprint(path, sizeof(path), "%s%s", dir, sub);
297 :
298 45716 : if (lstat(path, &st) != 0) {
299 : /* if the file doesn't exist, we don't filter it out */
300 4871 : if (errno == ENOENT)
301 4871 : 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 40845 : return 1;
310 : }
311 :
312 38698 : int filter_correctness(int filter_error, tommy_arrayblkof* infoarr, struct snapraid_disk* disk, struct snapraid_file* file)
313 : {
314 : unsigned i;
315 :
316 38698 : if (!filter_error)
317 16150 : return 0;
318 :
319 : /* check each block of the file */
320 60678 : for (i = 0; i < file->blockmax; ++i) {
321 46466 : block_off_t parity_pos = fs_file2par_get(disk, file, i);
322 46466 : snapraid_info info = info_get(infoarr, parity_pos);
323 :
324 : /* if the file has a bad block, don't exclude it */
325 46466 : if (info_get_bad(info))
326 8336 : return 0;
327 : }
328 :
329 : /* the file is correct, so we filter it out */
330 14212 : return 1;
331 : }
332 :
333 3114995 : int filter_content(tommy_list* contentlist, const char* path)
334 : {
335 : tommy_node* i;
336 :
337 40446128 : for (i = tommy_list_head(contentlist); i != 0; i = i->next) {
338 17108069 : struct snapraid_content* content = i->data;
339 : char tmp[PATH_MAX];
340 :
341 17108069 : if (pathcmp(content->content, path) == 0)
342 0 : return -1;
343 :
344 : /* exclude also the ".tmp" copy used to save it */
345 17108069 : pathprint(tmp, sizeof(tmp), "%s.tmp", content->content);
346 17108069 : if (pathcmp(tmp, path) == 0)
347 0 : return -1;
348 :
349 : /* exclude also the ".lock" file */
350 17108069 : pathprint(tmp, sizeof(tmp), "%s.lock", content->content);
351 17108069 : if (pathcmp(tmp, path) == 0)
352 0 : return -1;
353 : }
354 :
355 3114995 : return 0;
356 : }
357 :
358 3612132 : 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 3612132 : file = malloc_nofail(sizeof(struct snapraid_file));
364 3612132 : file->sub = strdup_nofail(sub);
365 3612132 : file->size = size;
366 3612132 : file->blockmax = (size + block_size - 1) / block_size;
367 3612132 : file->mtime_sec = mtime_sec;
368 3612132 : file->mtime_nsec = mtime_nsec;
369 3612132 : file->inode = inode;
370 3612132 : file->physical = physical;
371 3612132 : file->flag = 0;
372 3612132 : file->blockvec = malloc_nofail(file->blockmax * sizeof(struct snapraid_block));
373 :
374 12536645 : for (i = 0; i < file->blockmax; ++i) {
375 8924513 : block_state_set(&file->blockvec[i], BLOCK_STATE_CHG);
376 8924513 : hash_invalid_set(file->blockvec[i].hash);
377 : }
378 :
379 3612132 : return file;
380 : }
381 :
382 24709 : struct snapraid_file* file_dup(struct snapraid_file* copy)
383 : {
384 : struct snapraid_file* file;
385 : block_off_t i;
386 :
387 24709 : file = malloc_nofail(sizeof(struct snapraid_file));
388 24709 : file->sub = strdup_nofail(copy->sub);
389 24709 : file->size = copy->size;
390 24709 : file->blockmax = copy->blockmax;
391 24709 : file->mtime_sec = copy->mtime_sec;
392 24709 : file->mtime_nsec = copy->mtime_nsec;
393 24709 : file->inode = copy->inode;
394 24709 : file->physical = copy->physical;
395 24709 : file->flag = copy->flag;
396 24709 : file->blockvec = malloc_nofail(file->blockmax * sizeof(struct snapraid_block));
397 :
398 81407 : for (i = 0; i < file->blockmax; ++i) {
399 56698 : file->blockvec[i].state = copy->blockvec[i].state;
400 56698 : memcpy(file->blockvec[i].hash, copy->blockvec[i].hash, HASH_SIZE);
401 : }
402 :
403 24709 : return file;
404 : }
405 :
406 3466869 : void file_free(struct snapraid_file* file)
407 : {
408 3466869 : free(file->sub);
409 3466869 : file->sub = 0;
410 3466869 : free(file->blockvec);
411 3466869 : file->blockvec = 0;
412 3466869 : free(file);
413 3466869 : }
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 23636 : void file_copy(struct snapraid_file* src_file, struct snapraid_file* dst_file)
422 : {
423 : block_off_t i;
424 :
425 23636 : if (src_file->size != dst_file->size) {
426 : /* LCOV_EXCL_START */
427 : log_fatal("Internal inconsistency in copy file with different size\n");
428 : os_abort();
429 : /* LCOV_EXCL_STOP */
430 : }
431 :
432 23636 : 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 : os_abort();
436 : /* LCOV_EXCL_STOP */
437 : }
438 :
439 23636 : 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 : os_abort();
443 : /* LCOV_EXCL_STOP */
444 : }
445 :
446 81879 : for (i = 0; i < dst_file->blockmax; ++i) {
447 : /* set a block with hash computed but without parity */
448 58243 : block_state_set(&dst_file->blockvec[i], BLOCK_STATE_REP);
449 :
450 : /* copy the hash */
451 58243 : memcpy(dst_file->blockvec[i].hash, src_file->blockvec[i].hash, HASH_SIZE);
452 : }
453 :
454 23636 : file_flag_set(dst_file, FILE_IS_COPY);
455 23636 : }
456 :
457 48014 : const char* file_name(const struct snapraid_file* file)
458 : {
459 48014 : const char* r = strrchr(file->sub, '/');
460 :
461 48014 : if (!r)
462 78 : r = file->sub;
463 : else
464 47936 : ++r;
465 48014 : return r;
466 : }
467 :
468 6680330 : 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 6680330 : if (file_pos + 1 == file->blockmax) {
472 : unsigned block_remainder;
473 2717718 : if (file->size == 0)
474 0 : return 0;
475 2717718 : block_remainder = file->size % block_size;
476 2717718 : if (block_remainder == 0)
477 2959 : block_remainder = block_size;
478 2717718 : return block_remainder;
479 : }
480 :
481 3962612 : return block_size;
482 : }
483 :
484 5329166 : int file_block_is_last(struct snapraid_file* file, block_off_t file_pos)
485 : {
486 5329166 : if (file_pos == 0 && file->blockmax == 0)
487 0 : return 1;
488 :
489 5329166 : if (file_pos >= file->blockmax) {
490 : /* LCOV_EXCL_START */
491 : log_fatal("Internal inconsistency in file block position\n");
492 : os_abort();
493 : /* LCOV_EXCL_STOP */
494 : }
495 :
496 5329166 : return file_pos == file->blockmax - 1;
497 : }
498 :
499 927285 : int file_inode_compare_to_arg(const void* void_arg, const void* void_data)
500 : {
501 927285 : const uint64_t* arg = void_arg;
502 927285 : const struct snapraid_file* file = void_data;
503 :
504 927285 : if (*arg < file->inode)
505 0 : return -1;
506 927285 : if (*arg > file->inode)
507 0 : return 1;
508 927285 : 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 129157 : int file_path_compare(const void* void_a, const void* void_b)
524 : {
525 129157 : const struct snapraid_file* file_a = void_a;
526 129157 : const struct snapraid_file* file_b = void_b;
527 :
528 129157 : return strcmp(file_a->sub, file_b->sub);
529 : }
530 :
531 59718 : int file_physical_compare(const void* void_a, const void* void_b)
532 : {
533 59718 : const struct snapraid_file* file_a = void_a;
534 59718 : const struct snapraid_file* file_b = void_b;
535 :
536 59718 : if (file_a->physical < file_b->physical)
537 37856 : return -1;
538 21862 : if (file_a->physical > file_b->physical)
539 21860 : return 1;
540 2 : return 0;
541 : }
542 :
543 103230 : int file_path_compare_to_arg(const void* void_arg, const void* void_data)
544 : {
545 103230 : const char* arg = void_arg;
546 103230 : const struct snapraid_file* file = void_data;
547 :
548 103230 : return strcmp(arg, file->sub);
549 : }
550 :
551 24007 : int file_name_compare(const void* void_a, const void* void_b)
552 : {
553 24007 : const struct snapraid_file* file_a = void_a;
554 24007 : const struct snapraid_file* file_b = void_b;
555 24007 : const char* name_a = file_name(file_a);
556 24007 : const char* name_b = file_name(file_b);
557 :
558 24007 : return strcmp(name_a, name_b);
559 : }
560 :
561 23670 : int file_stamp_compare(const void* void_a, const void* void_b)
562 : {
563 23670 : const struct snapraid_file* file_a = void_a;
564 23670 : const struct snapraid_file* file_b = void_b;
565 :
566 23670 : if (file_a->size < file_b->size)
567 0 : return -1;
568 23670 : if (file_a->size > file_b->size)
569 0 : return 1;
570 :
571 23670 : if (file_a->mtime_sec < file_b->mtime_sec)
572 0 : return -1;
573 23670 : if (file_a->mtime_sec > file_b->mtime_sec)
574 0 : return 1;
575 :
576 23670 : if (file_a->mtime_nsec < file_b->mtime_nsec)
577 0 : return -1;
578 23670 : if (file_a->mtime_nsec > file_b->mtime_nsec)
579 0 : return 1;
580 :
581 23670 : return 0;
582 : }
583 :
584 24007 : int file_namestamp_compare(const void* void_a, const void* void_b)
585 : {
586 : int ret;
587 :
588 24007 : ret = file_name_compare(void_a, void_b);
589 24007 : if (ret != 0)
590 337 : return ret;
591 :
592 23670 : 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 3658037 : 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 3658037 : if (count == 0) {
611 : /* LCOV_EXCL_START */
612 : log_fatal("Internal inconsistency when allocating empty chunk for file '%s' at position '%u/%u'\n", file->sub, file_pos, file->blockmax);
613 : os_abort();
614 : /* LCOV_EXCL_STOP */
615 : }
616 3658037 : if (file_pos + count > file->blockmax) {
617 : /* LCOV_EXCL_START */
618 : log_fatal("Internal inconsistency when allocating overflowing chunk for file '%s' at position '%u:%u/%u'\n", file->sub, file_pos, count, file->blockmax);
619 : os_abort();
620 : /* LCOV_EXCL_STOP */
621 : }
622 :
623 3658037 : chunk = malloc_nofail(sizeof(struct snapraid_chunk));
624 3658037 : chunk->parity_pos = parity_pos;
625 3658037 : chunk->file = file;
626 3658037 : chunk->file_pos = file_pos;
627 3658037 : chunk->count = count;
628 :
629 3658037 : return chunk;
630 : }
631 :
632 3487501 : void chunk_free(struct snapraid_chunk* chunk)
633 : {
634 3487501 : free(chunk);
635 3487501 : }
636 :
637 43323431 : int chunk_parity_compare(const void* void_a, const void* void_b)
638 : {
639 43323431 : const struct snapraid_chunk* arg_a = void_a;
640 43323431 : const struct snapraid_chunk* arg_b = void_b;
641 :
642 43323431 : if (arg_a->parity_pos < arg_b->parity_pos)
643 2215613 : return -1;
644 41107818 : if (arg_a->parity_pos > arg_b->parity_pos)
645 41066186 : return 1;
646 :
647 41632 : return 0;
648 : }
649 :
650 44162450 : int chunk_file_compare(const void* void_a, const void* void_b)
651 : {
652 44162450 : const struct snapraid_chunk* arg_a = void_a;
653 44162450 : const struct snapraid_chunk* arg_b = void_b;
654 :
655 44162450 : if (arg_a->file < arg_b->file)
656 877820 : return -1;
657 43284630 : if (arg_a->file > arg_b->file)
658 43212428 : return 1;
659 :
660 72202 : if (arg_a->file_pos < arg_b->file_pos)
661 208 : return -1;
662 71994 : if (arg_a->file_pos > arg_b->file_pos)
663 30362 : return 1;
664 :
665 41632 : return 0;
666 : }
667 :
668 116876 : struct snapraid_link* link_alloc(const char* sub, const char* linkto, unsigned link_flag)
669 : {
670 : struct snapraid_link* slink;
671 :
672 116876 : slink = malloc_nofail(sizeof(struct snapraid_link));
673 116876 : slink->sub = strdup_nofail(sub);
674 116876 : slink->linkto = strdup_nofail(linkto);
675 116876 : slink->flag = link_flag;
676 :
677 116876 : return slink;
678 : }
679 :
680 111078 : void link_free(struct snapraid_link* slink)
681 : {
682 111078 : free(slink->sub);
683 111078 : free(slink->linkto);
684 111078 : free(slink);
685 111078 : }
686 :
687 31837 : int link_name_compare_to_arg(const void* void_arg, const void* void_data)
688 : {
689 31837 : const char* arg = void_arg;
690 31837 : const struct snapraid_link* slink = void_data;
691 :
692 31837 : return strcmp(arg, slink->sub);
693 : }
694 :
695 1249 : int link_alpha_compare(const void* void_a, const void* void_b)
696 : {
697 1249 : const struct snapraid_link* slink_a = void_a;
698 1249 : const struct snapraid_link* slink_b = void_b;
699 :
700 1249 : return strcmp(slink_a->sub, slink_b->sub);
701 : }
702 :
703 772 : struct snapraid_dir* dir_alloc(const char* sub)
704 : {
705 : struct snapraid_dir* dir;
706 :
707 772 : dir = malloc_nofail(sizeof(struct snapraid_dir));
708 772 : dir->sub = strdup_nofail(sub);
709 772 : dir->flag = 0;
710 :
711 772 : return dir;
712 : }
713 :
714 724 : void dir_free(struct snapraid_dir* dir)
715 : {
716 724 : free(dir->sub);
717 724 : free(dir);
718 724 : }
719 :
720 229 : int dir_name_compare(const void* void_arg, const void* void_data)
721 : {
722 229 : const char* arg = void_arg;
723 229 : const struct snapraid_dir* dir = void_data;
724 :
725 229 : return strcmp(arg, dir->sub);
726 : }
727 :
728 1537 : struct snapraid_disk* disk_alloc(const char* name, const char* dir, uint64_t dev, const char* uuid, int skip)
729 : {
730 : struct snapraid_disk* disk;
731 :
732 1537 : disk = malloc_nofail(sizeof(struct snapraid_disk));
733 1537 : pathcpy(disk->name, sizeof(disk->name), name);
734 1537 : pathimport(disk->dir, sizeof(disk->dir), dir);
735 1537 : pathcpy(disk->uuid, sizeof(disk->uuid), uuid);
736 :
737 : /* ensure that the dir terminate with "/" if it isn't empty */
738 1537 : pathslash(disk->dir, sizeof(disk->dir));
739 :
740 1537 : disk->smartctl[0] = 0;
741 1537 : disk->device = dev;
742 1537 : disk->tick = 0;
743 1537 : disk->total_blocks = 0;
744 1537 : disk->free_blocks = 0;
745 1537 : disk->first_free_block = 0;
746 1537 : disk->has_volatile_inodes = 0;
747 1537 : disk->has_unreliable_physical = 0;
748 1537 : disk->has_different_uuid = 0;
749 1537 : disk->has_unsupported_uuid = *uuid == 0; /* empty UUID means unsupported */
750 1537 : disk->had_empty_uuid = 0;
751 1537 : disk->mapping_idx = -1;
752 1537 : disk->skip_access = skip;
753 1537 : tommy_list_init(&disk->filelist);
754 1537 : tommy_list_init(&disk->deletedlist);
755 1537 : tommy_hashdyn_init(&disk->inodeset);
756 1537 : tommy_hashdyn_init(&disk->pathset);
757 1537 : tommy_hashdyn_init(&disk->stampset);
758 1537 : tommy_list_init(&disk->linklist);
759 1537 : tommy_hashdyn_init(&disk->linkset);
760 1537 : tommy_list_init(&disk->dirlist);
761 1537 : tommy_hashdyn_init(&disk->dirset);
762 1537 : tommy_tree_init(&disk->fs_parity, chunk_parity_compare);
763 1537 : tommy_tree_init(&disk->fs_file, chunk_file_compare);
764 1537 : disk->fs_last = 0;
765 :
766 1537 : return disk;
767 : }
768 :
769 1411 : void disk_free(struct snapraid_disk* disk)
770 : {
771 1411 : tommy_list_foreach(&disk->filelist, (tommy_foreach_func*)file_free);
772 1411 : tommy_list_foreach(&disk->deletedlist, (tommy_foreach_func*)file_free);
773 1411 : tommy_tree_foreach(&disk->fs_file, (tommy_foreach_func*)chunk_free);
774 1411 : tommy_hashdyn_done(&disk->inodeset);
775 1411 : tommy_hashdyn_done(&disk->pathset);
776 1411 : tommy_hashdyn_done(&disk->stampset);
777 1411 : tommy_list_foreach(&disk->linklist, (tommy_foreach_func*)link_free);
778 1411 : tommy_hashdyn_done(&disk->linkset);
779 1411 : tommy_list_foreach(&disk->dirlist, (tommy_foreach_func*)dir_free);
780 1411 : tommy_hashdyn_done(&disk->dirset);
781 1411 : free(disk);
782 1411 : }
783 :
784 : struct chunk_disk_empty {
785 : block_off_t blockmax;
786 : };
787 :
788 : /**
789 : * Compare the chunk if inside the specified blockmax.
790 : */
791 12 : static int chunk_disk_empty_compare(const void* void_a, const void* void_b)
792 : {
793 12 : const struct chunk_disk_empty* arg_a = void_a;
794 12 : const struct snapraid_chunk* arg_b = void_b;
795 :
796 : /* if the block is inside the specified blockmax, it's found */
797 12 : if (arg_a->blockmax > arg_b->parity_pos)
798 7 : return 0;
799 :
800 : /* otherwise search for a smaller one */
801 5 : return -1;
802 : }
803 :
804 888 : int disk_is_empty(struct snapraid_disk* disk, block_off_t blockmax)
805 : {
806 888 : struct chunk_disk_empty arg = { blockmax };
807 :
808 : /* if there is an element, it's not empty */
809 : /* even if links and dirs have no block allocation */
810 888 : if (!tommy_list_empty(&disk->filelist))
811 789 : return 0;
812 99 : if (!tommy_list_empty(&disk->linklist))
813 0 : return 0;
814 99 : if (!tommy_list_empty(&disk->dirlist))
815 0 : return 0;
816 :
817 : /* search for any chunk inside blockmax */
818 99 : if (tommy_tree_search_compare(&disk->fs_parity, chunk_disk_empty_compare, &arg) != 0)
819 7 : return 0;
820 :
821 : /* finally, it's empty */
822 92 : return 1;
823 : }
824 :
825 : struct chunk_disk_size {
826 : block_off_t size;
827 : };
828 :
829 : /**
830 : * Compare the chunk by highest parity position.
831 : *
832 : * The maximum parity position is stored as size.
833 : */
834 48684 : static int chunk_disk_size_compare(const void* void_a, const void* void_b)
835 : {
836 48684 : struct chunk_disk_size* arg_a = (void*)void_a;
837 48684 : const struct snapraid_chunk* arg_b = void_b;
838 :
839 : /* get the maximum size */
840 48684 : if (arg_a->size < arg_b->parity_pos + arg_b->count)
841 48684 : arg_a->size = arg_b->parity_pos + arg_b->count;
842 :
843 : /* search always for a bigger one */
844 48684 : return 1;
845 : }
846 :
847 4219 : block_off_t disk_size(struct snapraid_disk* disk)
848 : {
849 4219 : struct chunk_disk_size arg = { 0 };
850 4219 : tommy_tree_search_compare(&disk->fs_parity, chunk_disk_size_compare, &arg);
851 4219 : return arg.size;
852 : }
853 :
854 : struct chunk_check {
855 : const struct snapraid_chunk* prev;
856 : int result;
857 : };
858 :
859 6425469 : void chunk_parity_check_foreach(void* void_arg, void* void_obj)
860 : {
861 6425469 : struct chunk_check* arg = void_arg;
862 6425469 : const struct snapraid_chunk* obj = void_obj;
863 6425469 : const struct snapraid_chunk* prev = arg->prev;
864 :
865 : /* set the next previous block */
866 6425469 : arg->prev = obj;
867 :
868 : /* stop reporting if too many errors */
869 6425469 : if (arg->result > 100) {
870 : /* LCOV_EXCL_START */
871 : return;
872 : /* LCOV_EXCL_STOP */
873 : }
874 :
875 6425469 : if (obj->count == 0) {
876 : /* LCOV_EXCL_START */
877 : log_fatal("Internal inconsistency in parity count zero for file '%s' at '%u'\n",
878 : obj->file->sub, obj->parity_pos);
879 : ++arg->result;
880 : return;
881 : /* LCOV_EXCL_STOP */
882 : }
883 :
884 : /* check only if there is a previous block */
885 6425469 : if (!prev)
886 2618 : return;
887 :
888 : /* check the order */
889 6422851 : if (prev->parity_pos >= obj->parity_pos) {
890 : /* LCOV_EXCL_START */
891 : log_fatal("Internal inconsistency in parity order for files '%s' at '%u:%u' and '%s' at '%u:%u'\n",
892 : prev->file->sub, prev->parity_pos, prev->count, obj->file->sub, obj->parity_pos, obj->count);
893 : ++arg->result;
894 : return;
895 : /* LCOV_EXCL_STOP */
896 : }
897 :
898 : /* check that the chunks don't overlap */
899 6422851 : if (prev->parity_pos + prev->count > obj->parity_pos) {
900 : /* LCOV_EXCL_START */
901 : log_fatal("Internal inconsistency for parity overlap for files '%s' at '%u:%u' and '%s' at '%u:%u'\n",
902 : prev->file->sub, prev->parity_pos, prev->count, obj->file->sub, obj->parity_pos, obj->count);
903 : ++arg->result;
904 : return;
905 : /* LCOV_EXCL_STOP */
906 : }
907 : }
908 :
909 6425469 : void chunk_file_check_foreach(void* void_arg, void* void_obj)
910 : {
911 6425469 : struct chunk_check* arg = void_arg;
912 6425469 : const struct snapraid_chunk* obj = void_obj;
913 6425469 : const struct snapraid_chunk* prev = arg->prev;
914 :
915 : /* set the next previous block */
916 6425469 : arg->prev = obj;
917 :
918 : /* stop reporting if too many errors */
919 6425469 : if (arg->result > 100) {
920 : /* LCOV_EXCL_START */
921 : return;
922 : /* LCOV_EXCL_STOP */
923 : }
924 :
925 6425469 : if (obj->count == 0) {
926 : /* LCOV_EXCL_START */
927 : log_fatal("Internal inconsistency in file count zero for file '%s' at '%u'\n",
928 : obj->file->sub, obj->file_pos);
929 : ++arg->result;
930 : return;
931 : /* LCOV_EXCL_STOP */
932 : }
933 :
934 : /* note that for deleted files, some chunks may be missing */
935 :
936 : /* if the files are different */
937 6425469 : if (!prev || prev->file != obj->file) {
938 6380270 : if (prev != 0) {
939 6377652 : if (file_flag_has(prev->file, FILE_IS_DELETED)) {
940 : /* check that the chunk doesn't overflow the file */
941 59765 : if (prev->file_pos + prev->count > prev->file->blockmax) {
942 : /* LCOV_EXCL_START */
943 : log_fatal("Internal inconsistency in delete end for file '%s' at '%u:%u' overflowing size '%u'\n",
944 : prev->file->sub, prev->file_pos, prev->count, prev->file->blockmax);
945 : ++arg->result;
946 : return;
947 : /* LCOV_EXCL_STOP */
948 : }
949 : } else {
950 : /* check that the chunk ends the file */
951 6317887 : if (prev->file_pos + prev->count != prev->file->blockmax) {
952 : /* LCOV_EXCL_START */
953 : log_fatal("Internal inconsistency in file end for file '%s' at '%u:%u' instead of size '%u'\n",
954 : prev->file->sub, prev->file_pos, prev->count, prev->file->blockmax);
955 : ++arg->result;
956 : return;
957 : /* LCOV_EXCL_STOP */
958 : }
959 : }
960 : }
961 :
962 12760540 : if (file_flag_has(obj->file, FILE_IS_DELETED)) {
963 : /* check that the chunk doesn't overflow the file */
964 59848 : if (obj->file_pos + obj->count > obj->file->blockmax) {
965 : /* LCOV_EXCL_START */
966 : log_fatal("Internal inconsistency in delete start for file '%s' at '%u:%u' overflowing size '%u'\n",
967 : obj->file->sub, obj->file_pos, obj->count, obj->file->blockmax);
968 : ++arg->result;
969 : return;
970 : /* LCOV_EXCL_STOP */
971 : }
972 : } else {
973 : /* check that the chunk starts the file */
974 6320422 : if (obj->file_pos != 0) {
975 : /* LCOV_EXCL_START */
976 : log_fatal("Internal inconsistency in file start for file '%s' at '%u:%u'\n",
977 : obj->file->sub, obj->file_pos, obj->count);
978 : ++arg->result;
979 : return;
980 : /* LCOV_EXCL_STOP */
981 : }
982 : }
983 : } else {
984 : /* check the order */
985 45199 : if (prev->file_pos >= obj->file_pos) {
986 : /* LCOV_EXCL_START */
987 : log_fatal("Internal inconsistency in file order for file '%s' at '%u:%u' and at '%u:%u'\n",
988 : prev->file->sub, prev->file_pos, prev->count, obj->file_pos, obj->count);
989 : ++arg->result;
990 : return;
991 : /* LCOV_EXCL_STOP */
992 : }
993 :
994 45199 : if (file_flag_has(obj->file, FILE_IS_DELETED)) {
995 : /* check that the chunks don't overlap */
996 27 : if (prev->file_pos + prev->count > obj->file_pos) {
997 : /* LCOV_EXCL_START */
998 : log_fatal("Internal inconsistency in delete sequence for file '%s' at '%u:%u' and at '%u:%u'\n",
999 : prev->file->sub, prev->file_pos, prev->count, obj->file_pos, obj->count);
1000 : ++arg->result;
1001 : return;
1002 : /* LCOV_EXCL_STOP */
1003 : }
1004 : } else {
1005 : /* check that the chunks are sequential */
1006 45172 : if (prev->file_pos + prev->count != obj->file_pos) {
1007 : /* LCOV_EXCL_START */
1008 : log_fatal("Internal inconsistency in file sequence for file '%s' at '%u:%u' and at '%u:%u'\n",
1009 : prev->file->sub, prev->file_pos, prev->count, obj->file_pos, obj->count);
1010 : ++arg->result;
1011 : return;
1012 : /* LCOV_EXCL_STOP */
1013 : }
1014 : }
1015 : }
1016 : }
1017 :
1018 2880 : int fs_check(struct snapraid_disk* disk)
1019 : {
1020 : struct chunk_check arg;
1021 :
1022 : /* error count starts from 0 */
1023 2880 : arg.result = 0;
1024 :
1025 : /* check parity sequence */
1026 2880 : arg.prev = 0;
1027 2880 : tommy_tree_foreach_arg(&disk->fs_parity, chunk_parity_check_foreach, &arg);
1028 :
1029 : /* check file sequence */
1030 2880 : arg.prev = 0;
1031 2880 : tommy_tree_foreach_arg(&disk->fs_file, chunk_file_check_foreach, &arg);
1032 :
1033 2880 : if (arg.result != 0)
1034 0 : return -1;
1035 :
1036 2880 : return 0;
1037 : }
1038 :
1039 : struct chunk_parity_inside {
1040 : block_off_t parity_pos;
1041 : };
1042 :
1043 : /**
1044 : * Compare the chunk if containing the specified parity position.
1045 : */
1046 102361796 : static int chunk_parity_inside_compare(const void* void_a, const void* void_b)
1047 : {
1048 102361796 : const struct chunk_parity_inside* arg_a = void_a;
1049 102361796 : const struct snapraid_chunk* arg_b = void_b;
1050 :
1051 102361796 : if (arg_a->parity_pos < arg_b->parity_pos)
1052 35326122 : return -1;
1053 67035674 : if (arg_a->parity_pos >= arg_b->parity_pos + arg_b->count)
1054 59415030 : return 1;
1055 :
1056 7620644 : return 0;
1057 : }
1058 :
1059 : /**
1060 : * Seach the chunk at the specified parity position.
1061 : * The search is optimized for sequential accesses.
1062 : * \return If not found return 0
1063 : */
1064 38412957 : static struct snapraid_chunk* fs_par2chunk_get_ts(struct snapraid_disk* disk, struct snapraid_chunk** fs_last, block_off_t parity_pos)
1065 : {
1066 : struct snapraid_chunk* chunk;
1067 :
1068 : /* check if the last accessed chunk matches */
1069 38412957 : if (*fs_last
1070 38393928 : && parity_pos >= (*fs_last)->parity_pos
1071 38235652 : && parity_pos < (*fs_last)->parity_pos + (*fs_last)->count
1072 : ) {
1073 29215970 : chunk = *fs_last;
1074 : } else {
1075 9196987 : struct chunk_parity_inside arg = { parity_pos };
1076 9196987 : chunk = tommy_tree_search_compare(&disk->fs_parity, chunk_parity_inside_compare, &arg);
1077 : }
1078 :
1079 38412957 : if (!chunk)
1080 1576343 : return 0;
1081 :
1082 : /* store the last accessed chunk */
1083 36836614 : *fs_last = chunk;
1084 :
1085 36836614 : return chunk;
1086 : }
1087 :
1088 : struct chunk_file_inside {
1089 : struct snapraid_file* file;
1090 : block_off_t file_pos;
1091 : };
1092 :
1093 : /**
1094 : * Compare the chunk if containing the specified file position.
1095 : */
1096 21328815 : static int chunk_file_inside_compare(const void* void_a, const void* void_b)
1097 : {
1098 21328815 : const struct chunk_file_inside* arg_a = void_a;
1099 21328815 : const struct snapraid_chunk* arg_b = void_b;
1100 :
1101 21328815 : if (arg_a->file < arg_b->file)
1102 8915963 : return -1;
1103 12412852 : if (arg_a->file > arg_b->file)
1104 10396177 : return 1;
1105 :
1106 2016675 : if (arg_a->file_pos < arg_b->file_pos)
1107 8204 : return -1;
1108 2008471 : if (arg_a->file_pos >= arg_b->file_pos + arg_b->count)
1109 7996 : return 1;
1110 :
1111 2000475 : return 0;
1112 : }
1113 :
1114 : /**
1115 : * Search the chunk at the specified file position.
1116 : * The search is optimized for sequential accesses.
1117 : * \return If not found return 0
1118 : */
1119 10190236 : 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)
1120 : {
1121 : struct snapraid_chunk* chunk;
1122 :
1123 : /* check if the last accessed chunk matches */
1124 10190236 : if (*fs_last
1125 10166262 : && file == (*fs_last)->file
1126 8203962 : && file_pos >= (*fs_last)->file_pos
1127 8203962 : && file_pos < (*fs_last)->file_pos + (*fs_last)->count
1128 : ) {
1129 8189753 : chunk = *fs_last;
1130 : } else {
1131 2000483 : struct chunk_file_inside arg = { file, file_pos };
1132 2000483 : chunk = tommy_tree_search_compare(&disk->fs_file, chunk_file_inside_compare, &arg);
1133 : }
1134 :
1135 10190236 : if (!chunk)
1136 8 : return 0;
1137 :
1138 : /* store the last accessed chunk */
1139 10190228 : *fs_last = chunk;
1140 :
1141 10190228 : return chunk;
1142 : }
1143 :
1144 38306069 : 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)
1145 : {
1146 : struct snapraid_chunk* chunk;
1147 :
1148 38306069 : chunk = fs_par2chunk_get_ts(disk, fs_last, parity_pos);
1149 38306069 : if (!chunk)
1150 1576343 : return 0;
1151 :
1152 36729726 : if (file_pos)
1153 36385398 : *file_pos = chunk->file_pos + (parity_pos - chunk->parity_pos);
1154 :
1155 36729726 : return chunk->file;
1156 : }
1157 :
1158 4840928 : block_off_t fs_file2par_maybe_ts(struct snapraid_disk* disk, struct snapraid_chunk** fs_last, struct snapraid_file* file, block_off_t file_pos)
1159 : {
1160 : struct snapraid_chunk* chunk;
1161 :
1162 4840928 : chunk = fs_file2chunk_get_ts(disk, fs_last, file, file_pos);
1163 4840928 : if (!chunk)
1164 8 : return POS_INVALID;
1165 :
1166 4840920 : return chunk->parity_pos + (file_pos - chunk->file_pos);
1167 : }
1168 :
1169 8981211 : void fs_allocate(struct snapraid_disk* disk, block_off_t parity_pos, struct snapraid_file* file, block_off_t file_pos)
1170 : {
1171 : struct snapraid_chunk* chunk;
1172 : struct snapraid_chunk* parity_chunk;
1173 : struct snapraid_chunk* file_chunk;
1174 :
1175 8981211 : if (file_pos > 0) {
1176 : /* search an existing chunk for the previous file_pos */
1177 5349308 : chunk = fs_file2chunk_get_ts(disk, &disk->fs_last, file, file_pos - 1);
1178 :
1179 5349308 : if (chunk != 0 && parity_pos == chunk->parity_pos + chunk->count) {
1180 : /* ensure that we are extending the chunk at the end */
1181 5323175 : if (file_pos != chunk->file_pos + chunk->count) {
1182 : /* LCOV_EXCL_START */
1183 : log_fatal("Internal inconsistency when allocating file '%s' at position '%u/%u' in the middle of chunk '%u:%u' in disk '%s'\n", file->sub, file_pos, file->blockmax, chunk->file_pos, chunk->count, disk->name);
1184 : os_abort();
1185 : /* LCOV_EXCL_STOP */
1186 : }
1187 :
1188 : /* extend the existing chunk */
1189 5323175 : ++chunk->count;
1190 :
1191 14304386 : return;
1192 : }
1193 : }
1194 :
1195 : /* a chunk doesn't exist, and we have to create a new one */
1196 3658036 : chunk = chunk_alloc(parity_pos, file, file_pos, 1);
1197 :
1198 : /* insert the chunk in the trees */
1199 3658036 : parity_chunk = tommy_tree_insert(&disk->fs_parity, &chunk->parity_node, chunk);
1200 3658036 : file_chunk = tommy_tree_insert(&disk->fs_file, &chunk->file_node, chunk);
1201 :
1202 3658036 : if (parity_chunk != chunk || file_chunk != chunk) {
1203 : /* LCOV_EXCL_START */
1204 : log_fatal("Internal inconsistency when allocating file '%s' at position '%u/%u' for existing chunk '%u:%u' in disk '%s'\n", file->sub, file_pos, file->blockmax, chunk->file_pos, chunk->count, disk->name);
1205 : os_abort();
1206 : /* LCOV_EXCL_STOP */
1207 : }
1208 :
1209 : /* store the last accessed chunk */
1210 3658036 : disk->fs_last = chunk;
1211 : }
1212 :
1213 106888 : void fs_deallocate(struct snapraid_disk* disk, block_off_t parity_pos)
1214 : {
1215 : struct snapraid_chunk* chunk;
1216 : struct snapraid_chunk* second_chunk;
1217 : struct snapraid_chunk* parity_chunk;
1218 : struct snapraid_chunk* file_chunk;
1219 : block_off_t first_count, second_count;
1220 :
1221 106888 : chunk = fs_par2chunk_get_ts(disk, &disk->fs_last, parity_pos);
1222 106888 : if (!chunk) {
1223 : /* LCOV_EXCL_START */
1224 : log_fatal("Internal inconsistency when deallocating parity position '%u' for not existing chunk in disk '%s'\n", parity_pos, disk->name);
1225 : os_abort();
1226 : /* LCOV_EXCL_STOP */
1227 : }
1228 :
1229 : /* if it's the only block of the chunk, delete it */
1230 106888 : if (chunk->count == 1) {
1231 : /* remove from the trees */
1232 41632 : tommy_tree_remove(&disk->fs_parity, chunk);
1233 41632 : tommy_tree_remove(&disk->fs_file, chunk);
1234 :
1235 : /* deallocate */
1236 41632 : chunk_free(chunk);
1237 :
1238 : /* clear the last accessed chunk */
1239 41632 : disk->fs_last = 0;
1240 41632 : return;
1241 : }
1242 :
1243 : /* if it's at the start of the chunk, shrink the chunk */
1244 65256 : if (parity_pos == chunk->parity_pos) {
1245 65254 : ++chunk->parity_pos;
1246 65254 : ++chunk->file_pos;
1247 65254 : --chunk->count;
1248 65254 : return;
1249 : }
1250 :
1251 : /* if it's at the end of the chunk, shrink the chunk */
1252 2 : if (parity_pos == chunk->parity_pos + chunk->count - 1) {
1253 1 : --chunk->count;
1254 1 : return;
1255 : }
1256 :
1257 : /* otherwise it's in the middle */
1258 1 : first_count = parity_pos - chunk->parity_pos;
1259 1 : second_count = chunk->count - first_count - 1;
1260 :
1261 : /* adjust the first chunk */
1262 1 : chunk->count = first_count;
1263 :
1264 : /* allocate the second chunk */
1265 1 : second_chunk = chunk_alloc(chunk->parity_pos + first_count + 1, chunk->file, chunk->file_pos + first_count + 1, second_count);
1266 :
1267 : /* insert the chunk in the trees */
1268 1 : parity_chunk = tommy_tree_insert(&disk->fs_parity, &second_chunk->parity_node, second_chunk);
1269 1 : file_chunk = tommy_tree_insert(&disk->fs_file, &second_chunk->file_node, second_chunk);
1270 :
1271 1 : if (parity_chunk != second_chunk || file_chunk != second_chunk) {
1272 : /* LCOV_EXCL_START */
1273 : 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);
1274 : os_abort();
1275 : /* LCOV_EXCL_STOP */
1276 : }
1277 :
1278 : /* store the last accessed chunk */
1279 1 : disk->fs_last = second_chunk;
1280 : }
1281 :
1282 44005284 : struct snapraid_block* fs_file2block_get(struct snapraid_file* file, block_off_t file_pos)
1283 : {
1284 44005284 : if (file_pos >= file->blockmax) {
1285 : /* LCOV_EXCL_START */
1286 : log_fatal("Internal inconsistency when dereferencing file '%s' at position '%u/%u'\n", file->sub, file_pos, file->blockmax);
1287 : os_abort();
1288 : /* LCOV_EXCL_STOP */
1289 : }
1290 :
1291 44005284 : return &file->blockvec[file_pos];
1292 : }
1293 :
1294 26721446 : struct snapraid_block* fs_par2block_get_ts(struct snapraid_disk* disk, struct snapraid_chunk** fs_last, block_off_t parity_pos)
1295 : {
1296 : struct snapraid_file* file;
1297 : block_off_t file_pos;
1298 :
1299 26721446 : file = fs_par2file_get_ts(disk, fs_last, parity_pos, &file_pos);
1300 :
1301 26721446 : if (!file)
1302 1576343 : return BLOCK_EMPTY;
1303 :
1304 25145103 : return fs_file2block_get(file, file_pos);
1305 : }
1306 :
1307 1507 : struct snapraid_map* map_alloc(const char* name, unsigned position, block_off_t total_blocks, block_off_t free_blocks, const char* uuid)
1308 : {
1309 : struct snapraid_map* map;
1310 :
1311 1507 : map = malloc_nofail(sizeof(struct snapraid_map));
1312 1507 : pathcpy(map->name, sizeof(map->name), name);
1313 1507 : map->position = position;
1314 1507 : map->total_blocks = total_blocks;
1315 1507 : map->free_blocks = free_blocks;
1316 1507 : pathcpy(map->uuid, sizeof(map->uuid), uuid);
1317 :
1318 1507 : return map;
1319 : }
1320 :
1321 1387 : void map_free(struct snapraid_map* map)
1322 : {
1323 1387 : free(map);
1324 1387 : }
1325 :
1326 717256 : int time_compare(const void* void_a, const void* void_b)
1327 : {
1328 717256 : const time_t* time_a = void_a;
1329 717256 : const time_t* time_b = void_b;
1330 :
1331 717256 : if (*time_a < *time_b)
1332 27900 : return -1;
1333 689356 : if (*time_a > *time_b)
1334 156362 : return 1;
1335 532994 : return 0;
1336 : }
|