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 "state.h"
23 : #include "util.h"
24 :
25 : /****************************************************************************/
26 : /* snapraid */
27 :
28 : int BLOCK_HASH_SIZE = HASH_MAX;
29 :
30 1606 : struct snapraid_content* content_alloc(const char* path, uint64_t dev)
31 : {
32 : struct snapraid_content* content;
33 :
34 1606 : content = malloc_nofail(sizeof(struct snapraid_content));
35 1606 : pathimport(content->content, sizeof(content->content), path);
36 1606 : content->device = dev;
37 :
38 1606 : return content;
39 : }
40 :
41 1551 : void content_free(struct snapraid_content* content)
42 : {
43 1551 : free(content);
44 1551 : }
45 :
46 559 : struct snapraid_filter* filter_alloc_file(int direction, const char* root, const char* pattern)
47 : {
48 : struct snapraid_filter* filter;
49 : char* i;
50 : char* first;
51 : char* last;
52 : int token_is_valid;
53 : int token_is_filled;
54 :
55 559 : filter = malloc_nofail(sizeof(struct snapraid_filter));
56 559 : pathimport(filter->pattern, sizeof(filter->pattern), pattern);
57 559 : pathimport(filter->root, sizeof(filter->root), root);
58 559 : filter->direction = direction;
59 :
60 : /* find first and last slash */
61 559 : first = 0;
62 559 : last = 0;
63 :
64 : /* reject invalid tokens, like "<empty>", ".", ".." and more dots */
65 559 : token_is_valid = 0;
66 559 : token_is_filled = 0;
67 6943 : for (i = filter->pattern; *i; ++i) {
68 6385 : if (*i == '/') {
69 : /* reject invalid tokens, but accept an empty one as first */
70 4 : if (!token_is_valid && (first != 0 || token_is_filled)) {
71 1 : free(filter);
72 1 : return 0;
73 : }
74 3 : token_is_valid = 0;
75 3 : token_is_filled = 0;
76 :
77 : /* update slash position */
78 3 : if (!first)
79 2 : first = i;
80 3 : last = i;
81 6381 : } else if (*i != '.') {
82 5825 : token_is_valid = 1;
83 5825 : token_is_filled = 1;
84 : } else {
85 556 : token_is_filled = 1;
86 : }
87 : }
88 :
89 : /* reject invalid tokens, but accept an empty one as last, but not if it's the only one */
90 558 : if (!token_is_valid && (first == 0 || token_is_filled)) {
91 1 : free(filter);
92 1 : return 0;
93 : }
94 :
95 : /* it's a file filter */
96 557 : filter->is_disk = 0;
97 :
98 557 : if (first == 0) {
99 : /* no slash */
100 555 : filter->is_abs = 0;
101 555 : filter->is_dir = 0;
102 : } else {
103 2 : if (first == filter->pattern) {
104 : /* slash at the start */
105 1 : filter->is_abs = 1;
106 : } else {
107 : /* no slash at the start */
108 1 : filter->is_abs = 0;
109 : }
110 2 : if (last[1] == 0) {
111 : /* slash at the end */
112 1 : filter->is_dir = 1;
113 1 : last[0] = 0; /* clear the slash */
114 : } else {
115 : /* no slash at the end */
116 1 : filter->is_dir = 0;
117 : }
118 : }
119 :
120 557 : return filter;
121 : }
122 :
123 10 : struct snapraid_filter* filter_alloc_disk(int direction, const char* pattern)
124 : {
125 : struct snapraid_filter* filter;
126 :
127 10 : filter = malloc_nofail(sizeof(struct snapraid_filter));
128 10 : pathimport(filter->pattern, sizeof(filter->pattern), pattern);
129 10 : filter->direction = direction;
130 :
131 : /* it's a disk filter */
132 10 : filter->is_disk = 1;
133 10 : filter->is_abs = 0;
134 10 : filter->is_dir = 0;
135 :
136 : /* no slash allowed in disk names */
137 10 : if (strchr(filter->pattern, '/') != 0) {
138 : /* LCOV_EXCL_START */
139 : free(filter);
140 : return 0;
141 : /* LCOV_EXCL_STOP */
142 : }
143 :
144 10 : return filter;
145 : }
146 :
147 547 : void filter_free(void* filter)
148 : {
149 547 : free(filter);
150 547 : }
151 :
152 125231 : const char* filter_type(struct snapraid_filter* filter, char* out, size_t out_size)
153 : {
154 : const char* direction;
155 :
156 125231 : if (filter->direction < 0)
157 124954 : direction = "exclude";
158 : else
159 277 : direction = "include";
160 :
161 125231 : if (filter->is_disk)
162 0 : pathprint(out, out_size, "%s %s:", direction, filter->pattern);
163 125231 : else if (filter->is_dir)
164 0 : pathprint(out, out_size, "%s %s/", direction, filter->pattern);
165 : else
166 125231 : pathprint(out, out_size, "%s %s", direction, filter->pattern);
167 :
168 125231 : return out;
169 : }
170 :
171 : /**
172 : * Apply a filter to the specified path
173 : *
174 : * @param filter The filter to apply.
175 : * @param reason Assign to it the filter that caused rejection.
176 : * @param path The path to filter.
177 : * @param is_dir if the path refers to a directory, otherwise to a file.
178 : * @return < 0 if the path is excluded, > 0 if the path is included, 0 if the filter doesn't apply.
179 : */
180 6355925 : static int filter_apply(struct snapraid_filter* filter, struct snapraid_filter** reason, const char* path, int is_dir)
181 : {
182 6355925 : if (filter->root[0] != 0) { /* if it's a local filter */
183 0 : if (!path_is_root_of(filter->root, path)) /* applies it only if it matches the root */
184 0 : return 0;
185 : /* advance the path to compare to make it local */
186 0 : path += strlen(filter->root);
187 : }
188 :
189 : /* match dirs with dirs and files with files */
190 6355925 : if (filter->is_dir && !is_dir)
191 5939 : return 0;
192 6349986 : if (!filter->is_dir && is_dir)
193 6274 : return 0;
194 :
195 6343712 : int ret = 0;
196 :
197 6343712 : if (filter->is_abs) {
198 : /* skip initial slash, as always missing in the path */
199 5939 : if (wnmatch(filter->pattern + 1, path) == 0)
200 0 : ret = filter->direction;
201 : } else {
202 : /* the path is relative, first try to match from the root */
203 6337773 : if (wnmatch(filter->pattern, path) == 0) {
204 92 : ret = filter->direction;
205 : } else {
206 : /* then try to match after all the / presents */
207 6337681 : char* slash = strchr(path, '/');
208 12543215 : while (slash) {
209 6330119 : if (wnmatch(filter->pattern, slash + 1) == 0) {
210 124585 : ret = filter->direction;
211 124585 : break;
212 : }
213 :
214 6205534 : slash = strchr(slash + 1, '/');
215 : }
216 : }
217 : }
218 :
219 6343712 : if (reason != 0 && ret < 0)
220 124677 : *reason = filter;
221 :
222 6343712 : return ret;
223 : }
224 :
225 : /**
226 : * Apply a list of filter to a specific disk/sub path
227 : *
228 : * @param filterlist The list of filter to apply
229 : * @param reason Assign to it the filter that caused rejection.
230 : * @param disk The disk that contain the path
231 : * @param sub The path in the disk
232 : * @param is_dir If the path refers to a directory, otherwise it's a file
233 : * @param is_def_include If no filter apply, if by default force the inclusion. Used to include directory by default.
234 : * @return < 0 if the path is excluded, == 0 if the path is included.
235 : */
236 :
237 4591561 : static int filter_element(tommy_list* filterlist, struct snapraid_filter** reason, const char* disk, const char* sub, int is_dir, int is_def_include)
238 : {
239 : tommy_node* i;
240 :
241 : /**
242 : * The rule is that the latest filter defines the behaviour of paths
243 : * that doesn't match any filter.
244 : *
245 : * If the latest filter is an 'exclusion' filter, the default is to include.
246 : * If the latest filter is an 'inclusion' filter, the default is to exclude.
247 : * If there are no filter, the default is to include.
248 : */
249 4591561 : int default_direction = 1;
250 :
251 : /* for each filter */
252 10924117 : for (i = tommy_list_head(filterlist); i != 0; i = i->next) {
253 : int ret;
254 6465913 : struct snapraid_filter* filter = i->data;
255 :
256 6465913 : if (filter->is_disk) {
257 109988 : if (wnmatch(filter->pattern, disk) == 0) {
258 8680 : ret = filter->direction;
259 : } else {
260 101308 : ret = 0;
261 : }
262 109988 : if (reason != 0 && ret < 0)
263 0 : *reason = filter;
264 : } else {
265 6355925 : ret = filter_apply(filter, reason, sub, is_dir);
266 : }
267 :
268 6465913 : if (ret > 0) {
269 : /* include the file */
270 8680 : return 0;
271 6457233 : } else if (ret < 0) {
272 : /* exclude the file */
273 124677 : return -1;
274 : } else {
275 : /* default is opposite of the last filter */
276 6332556 : default_direction = -filter->direction;
277 6332556 : if (reason != 0 && default_direction < 0)
278 3169054 : *reason = filter;
279 : /* continue with the next one */
280 : }
281 : }
282 :
283 : /* directories are always included by default, otherwise we cannot apply rules */
284 : /* to the contained files */
285 4458204 : if (is_def_include)
286 4493 : return 0;
287 :
288 : /* files are excluded/included depending of the last rule processed */
289 4453711 : if (default_direction < 0)
290 60598 : return -1;
291 :
292 4393113 : return 0;
293 : }
294 :
295 4587023 : int filter_path(tommy_list* filterlist, struct snapraid_filter** reason, const char* disk, const char* sub)
296 : {
297 4587023 : return filter_element(filterlist, reason, disk, sub, 0, 0);
298 : }
299 :
300 4493 : int filter_subdir(tommy_list* filterlist, struct snapraid_filter** reason, const char* disk, const char* sub)
301 : {
302 4493 : return filter_element(filterlist, reason, disk, sub, 1, 1);
303 : }
304 :
305 45 : int filter_emptydir(tommy_list* filterlist, struct snapraid_filter** reason, const char* disk, const char* sub)
306 : {
307 45 : return filter_element(filterlist, reason, disk, sub, 1, 0);
308 : }
309 :
310 77701 : int filter_existence(int filter_missing, const char* dir, const char* sub)
311 : {
312 : char path[PATH_MAX];
313 : struct stat st;
314 :
315 77701 : if (!filter_missing)
316 31985 : return 0;
317 :
318 : /* we directly check if in the disk the file is present or not */
319 45716 : pathprint(path, sizeof(path), "%s%s", dir, sub);
320 :
321 45716 : if (lstat(path, &st) != 0) {
322 : /* if the file doesn't exist, we don't filter it out */
323 4819 : if (errno == ENOENT)
324 4819 : return 0;
325 : /* LCOV_EXCL_START */
326 : log_fatal(errno, "Error in stat file '%s'. %s.\n", path, strerror(errno));
327 : exit(EXIT_FAILURE);
328 : /* LCOV_EXCL_STOP */
329 : }
330 :
331 : /* the file is present, so we filter it out */
332 40897 : return 1;
333 : }
334 :
335 35762 : int filter_correctness(int filter_error, tommy_arrayblkof* infoarr, struct snapraid_disk* disk, struct snapraid_file* file)
336 : {
337 : unsigned i;
338 :
339 35762 : if (!filter_error)
340 13214 : return 0;
341 :
342 : /* check each block of the file */
343 60662 : for (i = 0; i < file->blockmax; ++i) {
344 46388 : block_off_t parity_pos = fs_file2par_get(disk, file, i);
345 46388 : snapraid_info info = info_get(infoarr, parity_pos);
346 :
347 : /* if the file has a bad block, don't exclude it */
348 46388 : if (info_get_bad(info))
349 8274 : return 0;
350 : }
351 :
352 : /* the file is correct, so we filter it out */
353 14274 : return 1;
354 : }
355 :
356 3231524 : int filter_content(tommy_list* contentlist, const char* path)
357 : {
358 : tommy_node* i;
359 :
360 20922616 : for (i = tommy_list_head(contentlist); i != 0; i = i->next) {
361 17691092 : struct snapraid_content* content = i->data;
362 : char tmp[PATH_MAX];
363 :
364 17691092 : if (pathcmp(content->content, path) == 0)
365 0 : return -1;
366 :
367 : /* exclude also the ".tmp" copy used to save it */
368 17691092 : pathprint(tmp, sizeof(tmp), "%s.tmp", content->content);
369 17691092 : if (pathcmp(tmp, path) == 0)
370 0 : return -1;
371 :
372 : /* exclude also the ".lock" file */
373 17691092 : pathprint(tmp, sizeof(tmp), "%s.lock", content->content);
374 17691092 : if (pathcmp(tmp, path) == 0)
375 0 : return -1;
376 : }
377 :
378 3231524 : return 0;
379 : }
380 :
381 3807696 : 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)
382 : {
383 : struct snapraid_file* file;
384 : block_off_t i;
385 :
386 3807696 : file = malloc_nofail(sizeof(struct snapraid_file));
387 3807696 : file->sub = strdup_nofail(sub);
388 3807696 : file->size = size;
389 3807696 : file->blockmax = (size + block_size - 1) / block_size;
390 3807696 : file->mtime_sec = mtime_sec;
391 3807696 : file->mtime_nsec = mtime_nsec;
392 3807696 : file->inode = inode;
393 3807696 : file->physical = physical;
394 3807696 : file->flag = 0;
395 3807696 : file->blockvec = malloc_nofail(file->blockmax * block_sizeof());
396 :
397 13224281 : for (i = 0; i < file->blockmax; ++i) {
398 9416585 : struct snapraid_block* block = file_block(file, i);
399 9416585 : block_state_set(block, BLOCK_STATE_CHG);
400 9416585 : hash_invalid_set(block->hash);
401 : }
402 :
403 3807696 : return file;
404 : }
405 :
406 22687 : struct snapraid_file* file_dup(struct snapraid_file* copy)
407 : {
408 : struct snapraid_file* file;
409 : block_off_t i;
410 :
411 22687 : file = malloc_nofail(sizeof(struct snapraid_file));
412 22687 : file->sub = strdup_nofail(copy->sub);
413 22687 : file->size = copy->size;
414 22687 : file->blockmax = copy->blockmax;
415 22687 : file->mtime_sec = copy->mtime_sec;
416 22687 : file->mtime_nsec = copy->mtime_nsec;
417 22687 : file->inode = copy->inode;
418 22687 : file->physical = copy->physical;
419 22687 : file->flag = copy->flag;
420 22687 : file->blockvec = malloc_nofail(file->blockmax * block_sizeof());
421 :
422 79678 : for (i = 0; i < file->blockmax; ++i) {
423 56991 : struct snapraid_block* block = file_block(file, i);
424 56991 : struct snapraid_block* copy_block = file_block(copy, i);
425 56991 : block->state = copy_block->state;
426 56991 : memcpy(block->hash, copy_block->hash, BLOCK_HASH_SIZE);
427 : }
428 :
429 22687 : return file;
430 : }
431 :
432 3784741 : void file_free(struct snapraid_file* file)
433 : {
434 3784741 : free(file->sub);
435 3784741 : file->sub = 0;
436 3784741 : free(file->blockvec);
437 3784741 : file->blockvec = 0;
438 3784741 : free(file);
439 3784741 : }
440 :
441 155 : void file_rename(struct snapraid_file* file, const char* sub)
442 : {
443 155 : free(file->sub);
444 155 : file->sub = strdup_nofail(sub);
445 155 : }
446 :
447 23637 : void file_copy(struct snapraid_file* src_file, struct snapraid_file* dst_file)
448 : {
449 : block_off_t i;
450 :
451 23637 : if (src_file->size != dst_file->size) {
452 : /* LCOV_EXCL_START */
453 : log_fatal(EINTERNAL, "Internal inconsistency: Copy file with different size\n");
454 : os_abort();
455 : /* LCOV_EXCL_STOP */
456 : }
457 :
458 23637 : if (src_file->mtime_sec != dst_file->mtime_sec) {
459 : /* LCOV_EXCL_START */
460 : log_fatal(EINTERNAL, "Internal inconsistency: Copy file with different mtime_sec\n");
461 : os_abort();
462 : /* LCOV_EXCL_STOP */
463 : }
464 :
465 23637 : if (src_file->mtime_nsec != dst_file->mtime_nsec) {
466 : /* LCOV_EXCL_START */
467 : log_fatal(EINTERNAL, "Internal inconsistency: Copy file with different mtime_nsec\n");
468 : os_abort();
469 : /* LCOV_EXCL_STOP */
470 : }
471 :
472 81881 : for (i = 0; i < dst_file->blockmax; ++i) {
473 : /* set a block with hash computed but without parity */
474 58244 : block_state_set(file_block(dst_file, i), BLOCK_STATE_REP);
475 :
476 : /* copy the hash */
477 58244 : memcpy(file_block(dst_file, i)->hash, file_block(src_file, i)->hash, BLOCK_HASH_SIZE);
478 : }
479 :
480 23637 : file_flag_set(dst_file, FILE_IS_COPY);
481 23637 : }
482 :
483 47712 : const char* file_name(const struct snapraid_file* file)
484 : {
485 47712 : const char* r = strrchr(file->sub, '/');
486 :
487 47712 : if (!r)
488 80 : r = file->sub;
489 : else
490 47632 : ++r;
491 47712 : return r;
492 : }
493 :
494 6817779 : unsigned file_block_size(struct snapraid_file* file, block_off_t file_pos, unsigned block_size)
495 : {
496 : /* if it's the last block */
497 6817779 : if (file_pos + 1 == file->blockmax) {
498 : unsigned block_remainder;
499 2771709 : if (file->size == 0)
500 0 : return 0;
501 2771709 : block_remainder = file->size % block_size;
502 2771709 : if (block_remainder == 0)
503 3046 : block_remainder = block_size;
504 2771709 : return block_remainder;
505 : }
506 :
507 4046070 : return block_size;
508 : }
509 :
510 5245100 : int file_block_is_last(struct snapraid_file* file, block_off_t file_pos)
511 : {
512 5245100 : if (file_pos == 0 && file->blockmax == 0)
513 0 : return 1;
514 :
515 5245100 : if (file_pos >= file->blockmax) {
516 : /* LCOV_EXCL_START */
517 : log_fatal(EINTERNAL, "Internal inconsistency: File block position over the max\n");
518 : os_abort();
519 : /* LCOV_EXCL_STOP */
520 : }
521 :
522 5245100 : return file_pos == file->blockmax - 1;
523 : }
524 :
525 1043743 : int file_inode_compare_to_arg(const void* void_arg, const void* void_data)
526 : {
527 1043743 : const uint64_t* arg = void_arg;
528 1043743 : const struct snapraid_file* file = void_data;
529 :
530 1043743 : if (*arg < file->inode)
531 0 : return -1;
532 1043743 : if (*arg > file->inode)
533 0 : return 1;
534 1043743 : return 0;
535 : }
536 :
537 0 : int file_inode_compare(const void* void_a, const void* void_b)
538 : {
539 0 : const struct snapraid_file* file_a = void_a;
540 0 : const struct snapraid_file* file_b = void_b;
541 :
542 0 : if (file_a->inode < file_b->inode)
543 0 : return -1;
544 0 : if (file_a->inode > file_b->inode)
545 0 : return 1;
546 0 : return 0;
547 : }
548 :
549 187689 : int file_path_compare(const void* void_a, const void* void_b)
550 : {
551 187689 : const struct snapraid_file* file_a = void_a;
552 187689 : const struct snapraid_file* file_b = void_b;
553 :
554 187689 : return strcmp(file_a->sub, file_b->sub);
555 : }
556 :
557 99756 : int file_physical_compare(const void* void_a, const void* void_b)
558 : {
559 99756 : const struct snapraid_file* file_a = void_a;
560 99756 : const struct snapraid_file* file_b = void_b;
561 :
562 99756 : if (file_a->physical < file_b->physical)
563 52632 : return -1;
564 47124 : if (file_a->physical > file_b->physical)
565 47122 : return 1;
566 2 : return 0;
567 : }
568 :
569 97862 : int file_path_compare_to_arg(const void* void_arg, const void* void_data)
570 : {
571 97862 : const char* arg = void_arg;
572 97862 : const struct snapraid_file* file = void_data;
573 :
574 97862 : return strcmp(arg, file->sub);
575 : }
576 :
577 23856 : int file_name_compare(const void* void_a, const void* void_b)
578 : {
579 23856 : const struct snapraid_file* file_a = void_a;
580 23856 : const struct snapraid_file* file_b = void_b;
581 23856 : const char* name_a = file_name(file_a);
582 23856 : const char* name_b = file_name(file_b);
583 :
584 23856 : return strcmp(name_a, name_b);
585 : }
586 :
587 23671 : int file_stamp_compare(const void* void_a, const void* void_b)
588 : {
589 23671 : const struct snapraid_file* file_a = void_a;
590 23671 : const struct snapraid_file* file_b = void_b;
591 :
592 23671 : if (file_a->size < file_b->size)
593 0 : return -1;
594 23671 : if (file_a->size > file_b->size)
595 0 : return 1;
596 :
597 23671 : if (file_a->mtime_sec < file_b->mtime_sec)
598 0 : return -1;
599 23671 : if (file_a->mtime_sec > file_b->mtime_sec)
600 0 : return 1;
601 :
602 23671 : if (file_a->mtime_nsec < file_b->mtime_nsec)
603 0 : return -1;
604 23671 : if (file_a->mtime_nsec > file_b->mtime_nsec)
605 0 : return 1;
606 :
607 23671 : return 0;
608 : }
609 :
610 23856 : int file_namestamp_compare(const void* void_a, const void* void_b)
611 : {
612 : int ret;
613 :
614 23856 : ret = file_name_compare(void_a, void_b);
615 23856 : if (ret != 0)
616 185 : return ret;
617 :
618 23671 : return file_stamp_compare(void_a, void_b);
619 : }
620 :
621 0 : int file_pathstamp_compare(const void* void_a, const void* void_b)
622 : {
623 : int ret;
624 :
625 0 : ret = file_path_compare(void_a, void_b);
626 0 : if (ret != 0)
627 0 : return ret;
628 :
629 0 : return file_stamp_compare(void_a, void_b);
630 : }
631 :
632 3852016 : struct snapraid_extent* extent_alloc(block_off_t parity_pos, struct snapraid_file* file, block_off_t file_pos, block_off_t count)
633 : {
634 : struct snapraid_extent* extent;
635 :
636 3852016 : if (count == 0) {
637 : /* LCOV_EXCL_START */
638 : log_fatal(EINTERNAL, "Internal inconsistency: Allocating empty extent for file '%s' at position '%u/%u'\n", file->sub, file_pos, file->blockmax);
639 : os_abort();
640 : /* LCOV_EXCL_STOP */
641 : }
642 3852016 : if (file_pos + count > file->blockmax) {
643 : /* LCOV_EXCL_START */
644 : log_fatal(EINTERNAL, "Internal inconsistency: Allocating overflowing extent for file '%s' at position '%u:%u/%u'\n", file->sub, file_pos, count, file->blockmax);
645 : os_abort();
646 : /* LCOV_EXCL_STOP */
647 : }
648 :
649 3852016 : extent = malloc_nofail(sizeof(struct snapraid_extent));
650 3852016 : extent->parity_pos = parity_pos;
651 3852016 : extent->file = file;
652 3852016 : extent->file_pos = file_pos;
653 3852016 : extent->count = count;
654 :
655 3852016 : return extent;
656 : }
657 :
658 3806435 : void extent_free(struct snapraid_extent* extent)
659 : {
660 3806435 : free(extent);
661 3806435 : }
662 :
663 45768745 : int extent_parity_compare(const void* void_a, const void* void_b)
664 : {
665 45768745 : const struct snapraid_extent* arg_a = void_a;
666 45768745 : const struct snapraid_extent* arg_b = void_b;
667 :
668 45768745 : if (arg_a->parity_pos < arg_b->parity_pos)
669 939508 : return -1;
670 44829237 : if (arg_a->parity_pos > arg_b->parity_pos)
671 44791372 : return 1;
672 :
673 37865 : return 0;
674 : }
675 :
676 46158505 : int extent_file_compare(const void* void_a, const void* void_b)
677 : {
678 46158505 : const struct snapraid_extent* arg_a = void_a;
679 46158505 : const struct snapraid_extent* arg_b = void_b;
680 :
681 46158505 : if (arg_a->file < arg_b->file)
682 1049004 : return -1;
683 45109501 : if (arg_a->file > arg_b->file)
684 45040264 : return 1;
685 :
686 69237 : if (arg_a->file_pos < arg_b->file_pos)
687 183 : return -1;
688 69054 : if (arg_a->file_pos > arg_b->file_pos)
689 31189 : return 1;
690 :
691 37865 : return 0;
692 : }
693 :
694 123513 : struct snapraid_link* link_alloc(const char* sub, const char* linkto, unsigned link_flag)
695 : {
696 : struct snapraid_link* slink;
697 :
698 123513 : slink = malloc_nofail(sizeof(struct snapraid_link));
699 123513 : slink->sub = strdup_nofail(sub);
700 123513 : slink->linkto = strdup_nofail(linkto);
701 123513 : slink->flag = link_flag;
702 :
703 123513 : return slink;
704 : }
705 :
706 121917 : void link_free(struct snapraid_link* slink)
707 : {
708 121917 : free(slink->sub);
709 121917 : free(slink->linkto);
710 121917 : free(slink);
711 121917 : }
712 :
713 35617 : int link_name_compare_to_arg(const void* void_arg, const void* void_data)
714 : {
715 35617 : const char* arg = void_arg;
716 35617 : const struct snapraid_link* slink = void_data;
717 :
718 35617 : return strcmp(arg, slink->sub);
719 : }
720 :
721 3337 : int link_alpha_compare(const void* void_a, const void* void_b)
722 : {
723 3337 : const struct snapraid_link* slink_a = void_a;
724 3337 : const struct snapraid_link* slink_b = void_b;
725 :
726 3337 : return strcmp(slink_a->sub, slink_b->sub);
727 : }
728 :
729 840 : struct snapraid_dir* dir_alloc(const char* sub)
730 : {
731 : struct snapraid_dir* dir;
732 :
733 840 : dir = malloc_nofail(sizeof(struct snapraid_dir));
734 840 : dir->sub = strdup_nofail(sub);
735 840 : dir->flag = 0;
736 :
737 840 : return dir;
738 : }
739 :
740 832 : void dir_free(struct snapraid_dir* dir)
741 : {
742 832 : free(dir->sub);
743 832 : free(dir);
744 832 : }
745 :
746 269 : int dir_name_compare(const void* void_arg, const void* void_data)
747 : {
748 269 : const char* arg = void_arg;
749 269 : const struct snapraid_dir* dir = void_data;
750 :
751 269 : return strcmp(arg, dir->sub);
752 : }
753 :
754 1663 : struct snapraid_disk* disk_alloc(const char* name, const char* dir, uint64_t dev, const char* uuid, int skip_access)
755 : {
756 : struct snapraid_disk* disk;
757 : unsigned i;
758 :
759 1663 : disk = malloc_nofail(sizeof(struct snapraid_disk));
760 1663 : pathcpy(disk->name, sizeof(disk->name), name);
761 1663 : pathimport(disk->dir, sizeof(disk->dir), dir);
762 1663 : pathcpy(disk->uuid, sizeof(disk->uuid), uuid);
763 :
764 : /* ensure that the dir terminate with "/" if it isn't empty */
765 1663 : pathslash(disk->dir, sizeof(disk->dir));
766 :
767 : #if HAVE_THREAD
768 1663 : thread_mutex_init(&disk->fs_mutex);
769 : #endif
770 :
771 1663 : disk->smartctl[0] = 0;
772 8315 : for (i = 0; i < SMART_IGNORE_MAX; ++i)
773 6652 : disk->smartignore[i] = 0;
774 1663 : disk->fstype[0] = 0;
775 1663 : disk->fslabel[0] = 0;
776 1663 : disk->device = dev;
777 1663 : disk->tick = 0;
778 1663 : disk->cached_blocks = 0;
779 1663 : disk->progress_file = 0;
780 1663 : disk->total_blocks = 0;
781 1663 : disk->free_blocks = 0;
782 1663 : disk->first_free_block = 0;
783 1663 : disk->has_volatile_inodes = 0;
784 1663 : disk->has_volatile_hardlinks = 0;
785 1663 : disk->has_unreliable_physical = 0;
786 1663 : disk->has_different_uuid = 0;
787 1663 : disk->has_unsupported_uuid = *uuid == 0; /* empty UUID means unsupported */
788 1663 : disk->had_empty_uuid = 0;
789 1663 : disk->mapping_idx = -1;
790 1663 : disk->skip_access = skip_access;
791 1663 : tommy_list_init(&disk->filelist);
792 1663 : tommy_list_init(&disk->deletedlist);
793 1663 : tommy_hashdyn_init(&disk->inodeset);
794 1663 : tommy_hashdyn_init(&disk->pathset);
795 1663 : tommy_hashdyn_init(&disk->stampset);
796 1663 : tommy_list_init(&disk->linklist);
797 1663 : tommy_hashdyn_init(&disk->linkset);
798 1663 : tommy_list_init(&disk->dirlist);
799 1663 : tommy_hashdyn_init(&disk->dirset);
800 1663 : tommy_tree_init(&disk->fs_parity, extent_parity_compare);
801 1663 : tommy_tree_init(&disk->fs_file, extent_file_compare);
802 1663 : disk->fs_last = 0;
803 :
804 1663 : return disk;
805 : }
806 :
807 1603 : void disk_free(struct snapraid_disk* disk)
808 : {
809 1603 : tommy_list_foreach(&disk->filelist, (tommy_foreach_func*)file_free);
810 1603 : tommy_list_foreach(&disk->deletedlist, (tommy_foreach_func*)file_free);
811 1603 : tommy_tree_foreach(&disk->fs_file, (tommy_foreach_func*)extent_free);
812 1603 : tommy_hashdyn_done(&disk->inodeset);
813 1603 : tommy_hashdyn_done(&disk->pathset);
814 1603 : tommy_hashdyn_done(&disk->stampset);
815 1603 : tommy_list_foreach(&disk->linklist, (tommy_foreach_func*)link_free);
816 1603 : tommy_hashdyn_done(&disk->linkset);
817 1603 : tommy_list_foreach(&disk->dirlist, (tommy_foreach_func*)dir_free);
818 1603 : tommy_hashdyn_done(&disk->dirset);
819 :
820 : #if HAVE_THREAD
821 1603 : thread_mutex_destroy(&disk->fs_mutex);
822 : #endif
823 :
824 1603 : free(disk);
825 1603 : }
826 :
827 64751658 : static inline void fs_lock(struct snapraid_disk* disk)
828 : {
829 : #if HAVE_THREAD
830 64751658 : thread_mutex_lock(&disk->fs_mutex);
831 : #else
832 : (void)disk;
833 : #endif
834 64751658 : }
835 :
836 64751658 : static inline void fs_unlock(struct snapraid_disk* disk)
837 : {
838 : #if HAVE_THREAD
839 64751658 : thread_mutex_unlock(&disk->fs_mutex);
840 : #else
841 : (void)disk;
842 : #endif
843 64751658 : }
844 :
845 : struct extent_disk_empty {
846 : block_off_t blockmax;
847 : };
848 :
849 : /**
850 : * Compare the extent if inside the specified blockmax.
851 : */
852 12 : static int extent_disk_empty_compare_unlock(const void* void_a, const void* void_b)
853 : {
854 12 : const struct extent_disk_empty* arg_a = void_a;
855 12 : const struct snapraid_extent* arg_b = void_b;
856 :
857 : /* if the block is inside the specified blockmax, it's found */
858 12 : if (arg_a->blockmax > arg_b->parity_pos)
859 7 : return 0;
860 :
861 : /* otherwise search for a smaller one */
862 5 : return -1;
863 : }
864 :
865 942 : int fs_is_empty(struct snapraid_disk* disk, block_off_t blockmax)
866 : {
867 942 : struct extent_disk_empty arg = { blockmax };
868 :
869 : /* if there is an element, it's not empty */
870 : /* even if links and dirs have no block allocation */
871 942 : if (!tommy_list_empty(&disk->filelist))
872 843 : return 0;
873 99 : if (!tommy_list_empty(&disk->linklist))
874 0 : return 0;
875 99 : if (!tommy_list_empty(&disk->dirlist))
876 0 : return 0;
877 :
878 99 : fs_lock(disk);
879 :
880 : /* search for any extent inside blockmax */
881 99 : if (tommy_tree_search_compare(&disk->fs_parity, extent_disk_empty_compare_unlock, &arg) != 0) {
882 7 : fs_unlock(disk);
883 7 : return 0;
884 : }
885 :
886 : /* finally, it's empty */
887 92 : fs_unlock(disk);
888 92 : return 1;
889 : }
890 :
891 : struct extent_disk_size {
892 : block_off_t size;
893 : };
894 :
895 : /**
896 : * Compare the extent by highest parity position.
897 : *
898 : * The maximum parity position is stored as size.
899 : */
900 52890 : static int extent_disk_size_compare_unlock(const void* void_a, const void* void_b)
901 : {
902 52890 : struct extent_disk_size* arg_a = (void*)void_a;
903 52890 : const struct snapraid_extent* arg_b = void_b;
904 :
905 : /* get the maximum size */
906 52890 : if (arg_a->size < arg_b->parity_pos + arg_b->count)
907 52890 : arg_a->size = arg_b->parity_pos + arg_b->count;
908 :
909 : /* search always for a bigger one */
910 52890 : return 1;
911 : }
912 :
913 4549 : block_off_t fs_size(struct snapraid_disk* disk)
914 : {
915 4549 : struct extent_disk_size arg = { 0 };
916 :
917 4549 : fs_lock(disk);
918 :
919 4549 : tommy_tree_search_compare(&disk->fs_parity, extent_disk_size_compare_unlock, &arg);
920 :
921 4549 : fs_unlock(disk);
922 :
923 4549 : return arg.size;
924 : }
925 :
926 : struct extent_check {
927 : const struct snapraid_extent* prev;
928 : int result;
929 : };
930 :
931 6837675 : static void extent_parity_check_foreach_unlock(void* void_arg, void* void_obj)
932 : {
933 6837675 : struct extent_check* arg = void_arg;
934 6837675 : const struct snapraid_extent* obj = void_obj;
935 6837675 : const struct snapraid_extent* prev = arg->prev;
936 :
937 : /* set the next previous block */
938 6837675 : arg->prev = obj;
939 :
940 : /* stop reporting if too many errors */
941 6837675 : if (arg->result > 100) {
942 : /* LCOV_EXCL_START */
943 : return;
944 : /* LCOV_EXCL_STOP */
945 : }
946 :
947 6837675 : if (obj->count == 0) {
948 : /* LCOV_EXCL_START */
949 : log_fatal(EINTERNAL, "Internal inconsistency: Parity count zero for file '%s' at '%u'\n",
950 : obj->file->sub, obj->parity_pos);
951 : ++arg->result;
952 : return;
953 : /* LCOV_EXCL_STOP */
954 : }
955 :
956 : /* check only if there is a previous block */
957 6837675 : if (!prev)
958 2855 : return;
959 :
960 : /* check the order */
961 6834820 : if (prev->parity_pos >= obj->parity_pos) {
962 : /* LCOV_EXCL_START */
963 : log_fatal(EINTERNAL, "Internal inconsistency: Parity order for files '%s' at '%u:%u' and '%s' at '%u:%u'\n",
964 : prev->file->sub, prev->parity_pos, prev->count, obj->file->sub, obj->parity_pos, obj->count);
965 : ++arg->result;
966 : return;
967 : /* LCOV_EXCL_STOP */
968 : }
969 :
970 : /* check that the extents don't overlap */
971 6834820 : if (prev->parity_pos + prev->count > obj->parity_pos) {
972 : /* LCOV_EXCL_START */
973 : log_fatal(EINTERNAL, "Internal inconsistency: Parity overlap for files '%s' at '%u:%u' and '%s' at '%u:%u'\n",
974 : prev->file->sub, prev->parity_pos, prev->count, obj->file->sub, obj->parity_pos, obj->count);
975 : ++arg->result;
976 : return;
977 : /* LCOV_EXCL_STOP */
978 : }
979 : }
980 :
981 6837675 : static void extent_file_check_foreach_unlock(void* void_arg, void* void_obj)
982 : {
983 6837675 : struct extent_check* arg = void_arg;
984 6837675 : const struct snapraid_extent* obj = void_obj;
985 6837675 : const struct snapraid_extent* prev = arg->prev;
986 :
987 : /* set the next previous block */
988 6837675 : arg->prev = obj;
989 :
990 : /* stop reporting if too many errors */
991 6837675 : if (arg->result > 100) {
992 : /* LCOV_EXCL_START */
993 : return;
994 : /* LCOV_EXCL_STOP */
995 : }
996 :
997 6837675 : if (obj->count == 0) {
998 : /* LCOV_EXCL_START */
999 : log_fatal(EINTERNAL, "Internal inconsistency: File count zero for file '%s' at '%u'\n",
1000 : obj->file->sub, obj->file_pos);
1001 : ++arg->result;
1002 : return;
1003 : /* LCOV_EXCL_STOP */
1004 : }
1005 :
1006 : /* note that for deleted files, some extents may be missing */
1007 :
1008 : /* if the files are different */
1009 6837675 : if (!prev || prev->file != obj->file) {
1010 6791430 : if (prev != 0) {
1011 6788575 : if (file_flag_has(prev->file, FILE_IS_DELETED)) {
1012 : /* check that the extent doesn't overflow the file */
1013 59775 : if (prev->file_pos + prev->count > prev->file->blockmax) {
1014 : /* LCOV_EXCL_START */
1015 : log_fatal(EINTERNAL, "Internal inconsistency: Delete end for file '%s' at '%u:%u' overflowing size '%u'\n",
1016 : prev->file->sub, prev->file_pos, prev->count, prev->file->blockmax);
1017 : ++arg->result;
1018 : return;
1019 : /* LCOV_EXCL_STOP */
1020 : }
1021 : } else {
1022 : /* check that the extent ends the file */
1023 6728800 : if (prev->file_pos + prev->count != prev->file->blockmax) {
1024 : /* LCOV_EXCL_START */
1025 : log_fatal(EINTERNAL, "Internal inconsistency: File end for file '%s' at '%u:%u' instead of size '%u'\n",
1026 : prev->file->sub, prev->file_pos, prev->count, prev->file->blockmax);
1027 : ++arg->result;
1028 : return;
1029 : /* LCOV_EXCL_STOP */
1030 : }
1031 : }
1032 : }
1033 :
1034 6791430 : if (file_flag_has(obj->file, FILE_IS_DELETED)) {
1035 : /* check that the extent doesn't overflow the file */
1036 59852 : if (obj->file_pos + obj->count > obj->file->blockmax) {
1037 : /* LCOV_EXCL_START */
1038 : log_fatal(EINTERNAL, "Internal inconsistency: Delete start for file '%s' at '%u:%u' overflowing size '%u'\n",
1039 : obj->file->sub, obj->file_pos, obj->count, obj->file->blockmax);
1040 : ++arg->result;
1041 : return;
1042 : /* LCOV_EXCL_STOP */
1043 : }
1044 : } else {
1045 : /* check that the extent starts the file */
1046 6731578 : if (obj->file_pos != 0) {
1047 : /* LCOV_EXCL_START */
1048 : log_fatal(EINTERNAL, "Internal inconsistency: File start for file '%s' at '%u:%u'\n",
1049 : obj->file->sub, obj->file_pos, obj->count);
1050 : ++arg->result;
1051 : return;
1052 : /* LCOV_EXCL_STOP */
1053 : }
1054 : }
1055 : } else {
1056 : /* check the order */
1057 46245 : if (prev->file_pos >= obj->file_pos) {
1058 : /* LCOV_EXCL_START */
1059 : log_fatal(EINTERNAL, "Internal inconsistency: File order for file '%s' at '%u:%u' and at '%u:%u'\n",
1060 : prev->file->sub, prev->file_pos, prev->count, obj->file_pos, obj->count);
1061 : ++arg->result;
1062 : return;
1063 : /* LCOV_EXCL_STOP */
1064 : }
1065 :
1066 46245 : if (file_flag_has(obj->file, FILE_IS_DELETED)) {
1067 : /* check that the extents don't overlap */
1068 27 : if (prev->file_pos + prev->count > obj->file_pos) {
1069 : /* LCOV_EXCL_START */
1070 : log_fatal(EINTERNAL, "Internal inconsistency: Delete sequence for file '%s' at '%u:%u' and at '%u:%u'\n",
1071 : prev->file->sub, prev->file_pos, prev->count, obj->file_pos, obj->count);
1072 : ++arg->result;
1073 : return;
1074 : /* LCOV_EXCL_STOP */
1075 : }
1076 : } else {
1077 : /* check that the extents are sequential */
1078 46218 : if (prev->file_pos + prev->count != obj->file_pos) {
1079 : /* LCOV_EXCL_START */
1080 : log_fatal(EINTERNAL, "Internal inconsistency: File sequence for file '%s' at '%u:%u' and at '%u:%u'\n",
1081 : prev->file->sub, prev->file_pos, prev->count, obj->file_pos, obj->count);
1082 : ++arg->result;
1083 : return;
1084 : /* LCOV_EXCL_STOP */
1085 : }
1086 : }
1087 : }
1088 : }
1089 :
1090 3120 : int fs_check(struct snapraid_disk* disk)
1091 : {
1092 : struct extent_check arg;
1093 :
1094 : /* error count starts from 0 */
1095 3120 : arg.result = 0;
1096 :
1097 3120 : fs_lock(disk);
1098 :
1099 : /* check parity sequence */
1100 3120 : arg.prev = 0;
1101 3120 : tommy_tree_foreach_arg(&disk->fs_parity, extent_parity_check_foreach_unlock, &arg);
1102 :
1103 : /* check file sequence */
1104 3120 : arg.prev = 0;
1105 3120 : tommy_tree_foreach_arg(&disk->fs_file, extent_file_check_foreach_unlock, &arg);
1106 :
1107 3120 : fs_unlock(disk);
1108 :
1109 3120 : if (arg.result != 0)
1110 0 : return -1;
1111 :
1112 3120 : return 0;
1113 : }
1114 :
1115 : struct extent_parity_inside {
1116 : block_off_t parity_pos;
1117 : };
1118 :
1119 : /**
1120 : * Compare the extent if containing the specified parity position.
1121 : */
1122 158551658 : static int extent_parity_inside_compare_unlock(const void* void_a, const void* void_b)
1123 : {
1124 158551658 : const struct extent_parity_inside* arg_a = void_a;
1125 158551658 : const struct snapraid_extent* arg_b = void_b;
1126 :
1127 158551658 : if (arg_a->parity_pos < arg_b->parity_pos)
1128 58193079 : return -1;
1129 100358579 : if (arg_a->parity_pos >= arg_b->parity_pos + arg_b->count)
1130 87784588 : return 1;
1131 :
1132 12573991 : return 0;
1133 : }
1134 :
1135 : /**
1136 : * Search the extent at the specified parity position.
1137 : * The search is optimized for sequential accesses.
1138 : * \return If not found return 0
1139 : */
1140 49968851 : static struct snapraid_extent* fs_par2extent_get_unlock(struct snapraid_disk* disk, struct snapraid_extent** fs_last, block_off_t parity_pos)
1141 : {
1142 : struct snapraid_extent* extent;
1143 :
1144 : /* check if the last accessed extent matches */
1145 49968851 : if (*fs_last
1146 49934657 : && parity_pos >= (*fs_last)->parity_pos
1147 49256770 : && parity_pos < (*fs_last)->parity_pos + (*fs_last)->count
1148 : ) {
1149 35471955 : extent = *fs_last;
1150 : } else {
1151 14496896 : struct extent_parity_inside arg = { parity_pos };
1152 14496896 : extent = tommy_tree_search_compare(&disk->fs_parity, extent_parity_inside_compare_unlock, &arg);
1153 : }
1154 :
1155 49968851 : if (!extent)
1156 1922905 : return 0;
1157 :
1158 : /* store the last accessed extent */
1159 48045946 : *fs_last = extent;
1160 :
1161 48045946 : return extent;
1162 : }
1163 :
1164 : struct extent_file_inside {
1165 : struct snapraid_file* file;
1166 : block_off_t file_pos;
1167 : };
1168 :
1169 : /**
1170 : * Compare the extent if containing the specified file position.
1171 : */
1172 22976989 : static int extent_file_inside_compare_unlock(const void* void_a, const void* void_b)
1173 : {
1174 22976989 : const struct extent_file_inside* arg_a = void_a;
1175 22976989 : const struct snapraid_extent* arg_b = void_b;
1176 :
1177 22976989 : if (arg_a->file < arg_b->file)
1178 9660370 : return -1;
1179 13316619 : if (arg_a->file > arg_b->file)
1180 11130615 : return 1;
1181 :
1182 2186004 : if (arg_a->file_pos < arg_b->file_pos)
1183 7579 : return -1;
1184 2178425 : if (arg_a->file_pos >= arg_b->file_pos + arg_b->count)
1185 8807 : return 1;
1186 :
1187 2169618 : return 0;
1188 : }
1189 :
1190 : /**
1191 : * Search the extent at the specified file position.
1192 : * The search is optimized for sequential accesses.
1193 : * \return If not found return 0
1194 : */
1195 10949888 : static struct snapraid_extent* fs_file2extent_get_unlock(struct snapraid_disk* disk, struct snapraid_extent** fs_last, struct snapraid_file* file, block_off_t file_pos)
1196 : {
1197 : struct snapraid_extent* extent;
1198 :
1199 : /* check if the last accessed extent matches */
1200 10949888 : if (*fs_last
1201 10927396 : && file == (*fs_last)->file
1202 8794650 : && file_pos >= (*fs_last)->file_pos
1203 8794650 : && file_pos < (*fs_last)->file_pos + (*fs_last)->count
1204 : ) {
1205 8780262 : extent = *fs_last;
1206 : } else {
1207 2169626 : struct extent_file_inside arg = { file, file_pos };
1208 2169626 : extent = tommy_tree_search_compare(&disk->fs_file, extent_file_inside_compare_unlock, &arg);
1209 : }
1210 :
1211 10949888 : if (!extent)
1212 8 : return 0;
1213 :
1214 : /* store the last accessed extent */
1215 10949880 : *fs_last = extent;
1216 :
1217 10949880 : return extent;
1218 : }
1219 :
1220 49865865 : struct snapraid_file* fs_par2file_find(struct snapraid_disk* disk, block_off_t parity_pos, block_off_t* file_pos)
1221 : {
1222 : struct snapraid_extent* extent;
1223 : struct snapraid_file* file;
1224 :
1225 49865865 : fs_lock(disk);
1226 :
1227 49865865 : extent = fs_par2extent_get_unlock(disk, &disk->fs_last, parity_pos);
1228 :
1229 49865865 : if (!extent) {
1230 1922905 : fs_unlock(disk);
1231 1922905 : return 0;
1232 : }
1233 :
1234 47942960 : if (file_pos)
1235 47732115 : *file_pos = extent->file_pos + (parity_pos - extent->parity_pos);
1236 :
1237 47942960 : file = extent->file;
1238 :
1239 47942960 : fs_unlock(disk);
1240 47942960 : return file;
1241 : }
1242 :
1243 5301463 : block_off_t fs_file2par_find(struct snapraid_disk* disk, struct snapraid_file* file, block_off_t file_pos)
1244 : {
1245 : struct snapraid_extent* extent;
1246 : block_off_t ret;
1247 :
1248 5301463 : fs_lock(disk);
1249 :
1250 5301463 : extent = fs_file2extent_get_unlock(disk, &disk->fs_last, file, file_pos);
1251 5301463 : if (!extent) {
1252 8 : fs_unlock(disk);
1253 8 : return POS_NULL;
1254 : }
1255 :
1256 5301455 : ret = extent->parity_pos + (file_pos - extent->file_pos);
1257 :
1258 5301455 : fs_unlock(disk);
1259 5301455 : return ret;
1260 : }
1261 :
1262 9473576 : void fs_allocate(struct snapraid_disk* disk, block_off_t parity_pos, struct snapraid_file* file, block_off_t file_pos)
1263 : {
1264 : struct snapraid_extent* extent;
1265 : struct snapraid_extent* parity_extent;
1266 : struct snapraid_extent* file_extent;
1267 :
1268 9473576 : fs_lock(disk);
1269 :
1270 9473576 : if (file_pos > 0) {
1271 : /* search an existing extent for the previous file_pos */
1272 5648425 : extent = fs_file2extent_get_unlock(disk, &disk->fs_last, file, file_pos - 1);
1273 :
1274 5648425 : if (extent != 0 && parity_pos == extent->parity_pos + extent->count) {
1275 : /* ensure that we are extending the extent at the end */
1276 5621562 : if (file_pos != extent->file_pos + extent->count) {
1277 : /* LCOV_EXCL_START */
1278 : log_fatal(EINTERNAL, "Internal inconsistency: Allocating file '%s' at position '%u/%u' in the middle of extent '%u:%u' in disk '%s'\n", file->sub, file_pos, file->blockmax, extent->file_pos, extent->count, disk->name);
1279 : os_abort();
1280 : /* LCOV_EXCL_STOP */
1281 : }
1282 :
1283 : /* extend the existing extent */
1284 5621562 : ++extent->count;
1285 :
1286 5621562 : fs_unlock(disk);
1287 5621562 : return;
1288 : }
1289 : }
1290 :
1291 : /* a extent doesn't exist, and we have to create a new one */
1292 3852014 : extent = extent_alloc(parity_pos, file, file_pos, 1);
1293 :
1294 : /* insert the extent in the trees */
1295 3852014 : parity_extent = tommy_tree_insert(&disk->fs_parity, &extent->parity_node, extent);
1296 3852014 : file_extent = tommy_tree_insert(&disk->fs_file, &extent->file_node, extent);
1297 :
1298 3852014 : if (parity_extent != extent || file_extent != extent) {
1299 : /* LCOV_EXCL_START */
1300 : log_fatal(EINTERNAL, "Internal inconsistency: Allocating file '%s' at position '%u/%u' for existing extent '%u:%u' in disk '%s'\n", file->sub, file_pos, file->blockmax, extent->file_pos, extent->count, disk->name);
1301 : os_abort();
1302 : /* LCOV_EXCL_STOP */
1303 : }
1304 :
1305 : /* store the last accessed extent */
1306 3852014 : disk->fs_last = extent;
1307 :
1308 3852014 : fs_unlock(disk);
1309 : }
1310 :
1311 102986 : void fs_deallocate(struct snapraid_disk* disk, block_off_t parity_pos)
1312 : {
1313 : struct snapraid_extent* extent;
1314 : struct snapraid_extent* second_extent;
1315 : struct snapraid_extent* parity_extent;
1316 : struct snapraid_extent* file_extent;
1317 : block_off_t first_count, second_count;
1318 :
1319 102986 : fs_lock(disk);
1320 :
1321 102986 : extent = fs_par2extent_get_unlock(disk, &disk->fs_last, parity_pos);
1322 102986 : if (!extent) {
1323 : /* LCOV_EXCL_START */
1324 : log_fatal(EINTERNAL, "Internal inconsistency: Deallocating parity position '%u' for not existing extent in disk '%s'\n", parity_pos, disk->name);
1325 : os_abort();
1326 : /* LCOV_EXCL_STOP */
1327 : }
1328 :
1329 : /* if it's the only block of the extent, delete it */
1330 102986 : if (extent->count == 1) {
1331 : /* remove from the trees */
1332 37865 : tommy_tree_remove(&disk->fs_parity, extent);
1333 37865 : tommy_tree_remove(&disk->fs_file, extent);
1334 :
1335 : /* deallocate */
1336 37865 : extent_free(extent);
1337 :
1338 : /* clear the last accessed extent */
1339 37865 : disk->fs_last = 0;
1340 :
1341 37865 : fs_unlock(disk);
1342 37865 : return;
1343 : }
1344 :
1345 : /* if it's at the start of the extent, shrink the extent */
1346 65121 : if (parity_pos == extent->parity_pos) {
1347 65118 : ++extent->parity_pos;
1348 65118 : ++extent->file_pos;
1349 65118 : --extent->count;
1350 :
1351 65118 : fs_unlock(disk);
1352 65118 : return;
1353 : }
1354 :
1355 : /* if it's at the end of the extent, shrink the extent */
1356 3 : if (parity_pos == extent->parity_pos + extent->count - 1) {
1357 1 : --extent->count;
1358 :
1359 1 : fs_unlock(disk);
1360 1 : return;
1361 : }
1362 :
1363 : /* otherwise it's in the middle */
1364 2 : first_count = parity_pos - extent->parity_pos;
1365 2 : second_count = extent->count - first_count - 1;
1366 :
1367 : /* adjust the first extent */
1368 2 : extent->count = first_count;
1369 :
1370 : /* allocate the second extent */
1371 2 : second_extent = extent_alloc(extent->parity_pos + first_count + 1, extent->file, extent->file_pos + first_count + 1, second_count);
1372 :
1373 : /* insert the extent in the trees */
1374 2 : parity_extent = tommy_tree_insert(&disk->fs_parity, &second_extent->parity_node, second_extent);
1375 2 : file_extent = tommy_tree_insert(&disk->fs_file, &second_extent->file_node, second_extent);
1376 :
1377 2 : if (parity_extent != second_extent || file_extent != second_extent) {
1378 : /* LCOV_EXCL_START */
1379 : log_fatal(EINTERNAL, "Internal inconsistency: Deallocating parity position '%u' for splitting extent '%u:%u' in disk '%s'\n", parity_pos, second_extent->file_pos, second_extent->count, disk->name);
1380 : os_abort();
1381 : /* LCOV_EXCL_STOP */
1382 : }
1383 :
1384 : /* store the last accessed extent */
1385 2 : disk->fs_last = second_extent;
1386 :
1387 2 : fs_unlock(disk);
1388 : }
1389 :
1390 56447214 : struct snapraid_block* fs_file2block_get(struct snapraid_file* file, block_off_t file_pos)
1391 : {
1392 56447214 : if (file_pos >= file->blockmax) {
1393 : /* LCOV_EXCL_START */
1394 : log_fatal(EINTERNAL, "Internal inconsistency: Dereferencing file '%s' at position '%u/%u'\n", file->sub, file_pos, file->blockmax);
1395 : os_abort();
1396 : /* LCOV_EXCL_STOP */
1397 : }
1398 :
1399 56447214 : return file_block(file, file_pos);
1400 : }
1401 :
1402 38360216 : struct snapraid_block* fs_par2block_find(struct snapraid_disk* disk, block_off_t parity_pos)
1403 : {
1404 : struct snapraid_file* file;
1405 : block_off_t file_pos;
1406 :
1407 38360216 : file = fs_par2file_find(disk, parity_pos, &file_pos);
1408 38360216 : if (file == 0)
1409 1922905 : return BLOCK_NULL;
1410 :
1411 36437311 : return fs_file2block_get(file, file_pos);
1412 : }
1413 :
1414 1627 : struct snapraid_map* map_alloc(const char* name, unsigned position, block_off_t total_blocks, block_off_t free_blocks, const char* uuid)
1415 : {
1416 : struct snapraid_map* map;
1417 :
1418 1627 : map = malloc_nofail(sizeof(struct snapraid_map));
1419 1627 : pathcpy(map->name, sizeof(map->name), name);
1420 1627 : map->position = position;
1421 1627 : map->total_blocks = total_blocks;
1422 1627 : map->free_blocks = free_blocks;
1423 1627 : pathcpy(map->uuid, sizeof(map->uuid), uuid);
1424 :
1425 1627 : return map;
1426 : }
1427 :
1428 1573 : void map_free(struct snapraid_map* map)
1429 : {
1430 1573 : free(map);
1431 1573 : }
1432 :
1433 : /****************************************************************************/
1434 : /* bucket */
1435 :
1436 711 : static int bucket_cmp(const void* void_a, const void* void_b)
1437 : {
1438 711 : const struct snapraid_bucket* a = void_a;
1439 711 : const struct snapraid_bucket* b = void_b;
1440 711 : if (a->time_at < b->time_at)
1441 355 : return -1;
1442 356 : if (a->time_at > b->time_at)
1443 356 : return 1;
1444 0 : return 0;
1445 : }
1446 :
1447 746 : void bucket_free(struct snapraid_bucket* bucket)
1448 : {
1449 746 : free(bucket);
1450 746 : }
1451 :
1452 72052 : void bucket_insert(tommy_hashdyn* bucket_hash, time_t time_at, block_off_t count, int justsynced)
1453 : {
1454 72052 : tommy_hash_t hash = tommy_inthash_u64(time_at);
1455 72052 : tommy_node* i = tommy_hashdyn_bucket(bucket_hash, hash);
1456 72078 : while (i) {
1457 71322 : struct snapraid_bucket* entry = i->data;
1458 71322 : if (entry->time_at == time_at) {
1459 71296 : if (justsynced)
1460 43455 : entry->count_justsynced += count;
1461 : else
1462 27841 : entry->count_scrubbed += count;
1463 71296 : return;
1464 : }
1465 26 : i = i->next;
1466 : }
1467 :
1468 756 : struct snapraid_bucket* entry = malloc_nofail(sizeof(struct snapraid_bucket));
1469 756 : entry->time_at = time_at;
1470 756 : if (justsynced) {
1471 683 : entry->count_justsynced = count;
1472 683 : entry->count_scrubbed = 0;
1473 : } else {
1474 73 : entry->count_justsynced = 0;
1475 73 : entry->count_scrubbed = count;
1476 : }
1477 :
1478 756 : tommy_hashdyn_insert(bucket_hash, &entry->node, entry, hash);
1479 : }
1480 :
1481 421 : void bucket_to_list(tommy_hashdyn* bucket_hash, tommy_list* bucket_list, block_off_t* bucketcount)
1482 : {
1483 : /* clear previous list */
1484 421 : tommy_list_foreach(bucket_list, (tommy_foreach_func*)bucket_free);
1485 421 : tommy_list_init(bucket_list);
1486 :
1487 421 : log_tag("list:bucket_begin\n");
1488 :
1489 421 : log_tag("bucket:count:%" PRIu64 "\n", (uint64_t)tommy_hashdyn_count(bucket_hash));
1490 :
1491 421 : tommy_hashdyn_to_list(bucket_hash, bucket_list);
1492 :
1493 421 : tommy_list_sort(bucket_list, bucket_cmp);
1494 :
1495 421 : block_off_t count = 0;
1496 1177 : for (tommy_node* i = tommy_list_head(bucket_list); i != 0; i = i->next) {
1497 756 : struct snapraid_bucket* entry = i->data;
1498 756 : count += entry->count_scrubbed + entry->count_justsynced;
1499 756 : log_tag("bucket:entry:%" PRIu64 ":%u:%u\n", (uint64_t)entry->time_at, entry->count_scrubbed, entry->count_justsynced);
1500 : }
1501 :
1502 421 : log_tag("bucket:block_count:%u\n", count);
1503 :
1504 421 : log_tag("list:bucket_end\n");
1505 :
1506 421 : *bucketcount = count;
1507 421 : }
1508 :
1509 : /****************************************************************************/
1510 : /* format */
1511 :
1512 : int FMT_MODE = FMT_FILE;
1513 :
1514 : /**
1515 : * Format a file path for poll reference
1516 : */
1517 6 : const char* fmt_poll(const struct snapraid_disk* disk, const char* str, char* buffer)
1518 : {
1519 : (void)disk;
1520 6 : return esc_shell(str, buffer);
1521 : }
1522 :
1523 : /**
1524 : * Format a path name for terminal reference
1525 : */
1526 366067 : const char* fmt_term(const struct snapraid_disk* disk, const char* str, char* buffer)
1527 : {
1528 : const char* out[3];
1529 :
1530 366067 : switch (FMT_MODE) {
1531 342002 : case FMT_FILE :
1532 : default :
1533 342002 : return esc_shell(str, buffer);
1534 12030 : case FMT_DISK :
1535 12030 : out[0] = disk->name;
1536 12030 : out[1] = ":";
1537 12030 : out[2] = str;
1538 12030 : return esc_shell_multi(out, 3, buffer);
1539 12035 : case FMT_PATH :
1540 12035 : out[0] = disk->dir;
1541 12035 : out[1] = str;
1542 12035 : return esc_shell_multi(out, 2, buffer);
1543 : }
1544 : }
1545 :
1546 15 : const char* fmt_size(uint64_t size, char* buffer, size_t buffer_size)
1547 : {
1548 15 : if (size >= 10UL * TERA)
1549 0 : snprintf(buffer, buffer_size, "%" PRIu64 " T", size / TERA);
1550 15 : else if (size >= 10UL * GIGA)
1551 0 : snprintf(buffer, buffer_size, "%" PRIu64 " G", size / GIGA);
1552 15 : else if (size >= 10UL * MEGA)
1553 1 : snprintf(buffer, buffer_size, "%" PRIu64 " M", size / MEGA);
1554 14 : else if (size >= 10UL * KILO)
1555 13 : snprintf(buffer, buffer_size, "%" PRIu64 " k", size / KILO);
1556 : else
1557 1 : snprintf(buffer, buffer_size, "%" PRIu64 " ", size);
1558 :
1559 15 : return buffer;
1560 : }
1561 :
|