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 "import.h"
22 : #include "search.h"
23 : #include "state.h"
24 : #include "support.h"
25 : #include "parity.h"
26 : #include "stream.h"
27 : #include "raid/raid.h"
28 : #include "raid/cpu.h"
29 :
30 7 : const char* lev_name(unsigned l)
31 : {
32 7 : switch (l) {
33 2 : case 0 : return "Parity";
34 1 : case 1 : return "2-Parity";
35 1 : case 2 : return "3-Parity";
36 1 : case 3 : return "4-Parity";
37 1 : case 4 : return "5-Parity";
38 1 : case 5 : return "6-Parity";
39 : }
40 :
41 0 : return 0;
42 : }
43 :
44 178770 : const char* lev_config_name(unsigned l)
45 : {
46 178770 : switch (l) {
47 70480 : case 0 : return "parity";
48 57423 : case 1 : return "2-parity";
49 12723 : case 2 : return "3-parity";
50 12719 : case 3 : return "4-parity";
51 12715 : case 4 : return "5-parity";
52 12710 : case 5 : return "6-parity";
53 : }
54 :
55 0 : return 0;
56 : }
57 :
58 5905 : static int lev_config_scan(const char* s, unsigned* level, unsigned* mode)
59 : {
60 5905 : if (strcmp(s, "parity") == 0 || strcmp(s, "1-parity") == 0) {
61 419 : *level = 0;
62 419 : return 0;
63 : }
64 :
65 5486 : if (strcmp(s, "q-parity") == 0 || strcmp(s, "2-parity") == 0) {
66 213 : *level = 1;
67 213 : return 0;
68 : }
69 :
70 5273 : if (strcmp(s, "r-parity") == 0 || strcmp(s, "3-parity") == 0) {
71 188 : *level = 2;
72 188 : return 0;
73 : }
74 :
75 5085 : if (strcmp(s, "4-parity") == 0) {
76 185 : *level = 3;
77 185 : return 0;
78 : }
79 :
80 4900 : if (strcmp(s, "5-parity") == 0) {
81 182 : *level = 4;
82 182 : return 0;
83 : }
84 :
85 4718 : if (strcmp(s, "6-parity") == 0) {
86 178 : *level = 5;
87 178 : return 0;
88 : }
89 :
90 4540 : if (strcmp(s, "z-parity") == 0) {
91 0 : *level = 2;
92 0 : if (mode)
93 0 : *mode = RAID_MODE_VANDERMONDE;
94 0 : return 0;
95 : }
96 :
97 4540 : return -1;
98 : }
99 :
100 246 : const char* lev_raid_name(unsigned mode, unsigned n)
101 : {
102 246 : switch (n) {
103 34 : case 1 : return "par1";
104 25 : case 2 : return "par2";
105 3 : case 3 : if (mode == RAID_MODE_CAUCHY)
106 3 : return "par3";
107 : else
108 0 : return "parz";
109 3 : case 4 : return "par4";
110 4 : case 5 : return "par5";
111 177 : case 6 : return "par6";
112 : }
113 :
114 0 : return 0;
115 : }
116 :
117 248 : void state_init(struct snapraid_state* state)
118 : {
119 : unsigned l;
120 :
121 248 : memset(&state->opt, 0, sizeof(state->opt));
122 248 : state->filter_hidden = 0;
123 248 : state->autosave = 0;
124 248 : state->need_write = 0;
125 248 : state->checked_read = 0;
126 248 : state->block_size = 256 * 1024; /* default 256 KiB */
127 248 : state->raid_mode = RAID_MODE_CAUCHY;
128 248 : state->file_mode = MODE_SEQUENTIAL;
129 1736 : for (l = 0; l < LEV_MAX; ++l) {
130 1488 : state->parity[l].path[0] = 0;
131 1488 : state->parity[l].smartctl[0] = 0;
132 1488 : state->parity[l].uuid[0] = 0;
133 1488 : state->parity[l].device = 0;
134 1488 : state->parity[l].total_blocks = 0;
135 1488 : state->parity[l].free_blocks = 0;
136 1488 : state->parity[l].tick = 0;
137 1488 : state->parity[l].skip_access = 0;
138 1488 : state->parity[l].is_excluded = 0;
139 : }
140 248 : state->tick_io = 0;
141 248 : state->tick_cpu = 0;
142 248 : state->tick_last = tick();
143 248 : state->share[0] = 0;
144 248 : state->pool[0] = 0;
145 248 : state->pool_device = 0;
146 248 : state->lockfile[0] = 0;
147 248 : state->level = 1; /* default is the lowest protection */
148 248 : state->clear_past_hash = 0;
149 248 : state->no_conf = 0;
150 :
151 248 : tommy_list_init(&state->disklist);
152 248 : tommy_list_init(&state->maplist);
153 248 : tommy_list_init(&state->contentlist);
154 248 : tommy_list_init(&state->filterlist);
155 248 : tommy_list_init(&state->importlist);
156 248 : tommy_hashdyn_init(&state->importset);
157 248 : tommy_hashdyn_init(&state->previmportset);
158 248 : tommy_hashdyn_init(&state->searchset);
159 248 : tommy_arrayblkof_init(&state->infoarr, sizeof(snapraid_info));
160 248 : }
161 :
162 226 : void state_done(struct snapraid_state* state)
163 : {
164 226 : tommy_list_foreach(&state->disklist, (tommy_foreach_func*)disk_free);
165 226 : tommy_list_foreach(&state->maplist, (tommy_foreach_func*)map_free);
166 226 : tommy_list_foreach(&state->contentlist, (tommy_foreach_func*)content_free);
167 226 : tommy_list_foreach(&state->filterlist, (tommy_foreach_func*)filter_free);
168 226 : tommy_list_foreach(&state->importlist, (tommy_foreach_func*)import_file_free);
169 226 : tommy_hashdyn_foreach(&state->searchset, (tommy_foreach_func*)search_file_free);
170 226 : tommy_hashdyn_done(&state->importset);
171 226 : tommy_hashdyn_done(&state->previmportset);
172 226 : tommy_hashdyn_done(&state->searchset);
173 226 : tommy_arrayblkof_done(&state->infoarr);
174 226 : }
175 :
176 : /**
177 : * Check the configuration.
178 : */
179 246 : static void state_config_check(struct snapraid_state* state, const char* path, tommy_list* filterlist_disk)
180 : {
181 : tommy_node* i;
182 : unsigned l;
183 :
184 : /* check for parity level */
185 246 : if (state->raid_mode == RAID_MODE_VANDERMONDE) {
186 0 : if (state->level > 3) {
187 : /* LCOV_EXCL_START */
188 : log_fatal("If you use the z-parity you cannot have more than 3 parities.\n");
189 : exit(EXIT_FAILURE);
190 : /* LCOV_EXCL_STIO */
191 : }
192 : }
193 :
194 : for (l = 0; l < state->level; ++l) {
195 : if (state->parity[l].path[0] == 0) {
196 : /* LCOV_EXCL_START */
197 : log_fatal("No '%s' specification in '%s'\n", lev_config_name(l), path);
198 : exit(EXIT_FAILURE);
199 : /* LCOV_EXCL_STOP */
200 : }
201 : }
202 :
203 246 : if (tommy_list_empty(&state->contentlist)) {
204 : /* LCOV_EXCL_START */
205 : log_fatal("No 'content' specification in '%s'\n", path);
206 : exit(EXIT_FAILURE);
207 : /* LCOV_EXCL_STOP */
208 : }
209 :
210 : /* check for equal paths */
211 1679 : for (i = state->contentlist; i != 0; i = i->next) {
212 1433 : struct snapraid_content* content = i->data;
213 :
214 9301 : for (l = 0; l < state->level; ++l) {
215 7868 : if (pathcmp(state->parity[l].path, content->content) == 0) {
216 : /* LCOV_EXCL_START */
217 : log_fatal("Same path used for '%s' and 'content' as '%s'\n", lev_config_name(l), content->content);
218 : exit(EXIT_FAILURE);
219 : /* LCOV_EXCL_STOP */
220 : }
221 : }
222 : }
223 :
224 : /* check device of data disks */
225 246 : if (!state->opt.skip_device && !state->opt.skip_disk_access) {
226 0 : for (i = state->disklist; i != 0; i = i->next) {
227 : tommy_node* j;
228 0 : struct snapraid_disk* disk = i->data;
229 :
230 : /* skip data disks that are not accessible */
231 0 : if (disk->skip_access)
232 0 : continue;
233 :
234 : #ifdef _WIN32
235 : if (disk->device == 0) {
236 : /* LCOV_EXCL_START */
237 : log_fatal("Disk '%s' has a zero serial number.\n", disk->dir);
238 : log_fatal("This is not necessarely wrong, but for using SnapRAID\n");
239 : log_fatal("it's better to change the serial number of the disk.\n");
240 : log_fatal("Try using the 'VolumeID' tool by 'Mark Russinovich'.\n");
241 : exit(EXIT_FAILURE);
242 : /* LCOV_EXCL_STOP */
243 : }
244 : #endif
245 :
246 0 : for (j = i->next; j != 0; j = j->next) {
247 0 : struct snapraid_disk* other = j->data;
248 0 : if (disk->device == other->device) {
249 0 : if (state->opt.force_device) {
250 : /* mark disks to be skipped */
251 0 : disk->skip_access = 1;
252 0 : other->skip_access = 1;
253 0 : log_fatal("DANGER! Ignoring that disks '%s' and '%s' are on the same device\n", disk->name, other->name);
254 : } else {
255 : /* LCOV_EXCL_START */
256 : log_fatal("Disks '%s' and '%s' are on the same device.\n", disk->dir, other->dir);
257 : #ifdef _WIN32
258 : log_fatal("Both have the serial number '%" PRIx64 "'.\n", disk->device);
259 : log_fatal("Try using the 'VolumeID' tool by 'Mark Russinovich'\n");
260 : log_fatal("to change one of the disk serial.\n");
261 : #endif
262 : /* in "fix" we allow to continue anyway */
263 : if (strcmp(state->command, "fix") == 0) {
264 : log_fatal("You can '%s' anyway, using 'snapraid --force-device %s'.\n", state->command, state->command);
265 : }
266 : exit(EXIT_FAILURE);
267 : /* LCOV_EXCL_STOP */
268 : }
269 : }
270 : }
271 :
272 : /* skip data disks that are not accessible */
273 0 : if (disk->skip_access)
274 0 : continue;
275 :
276 0 : if (!state->opt.skip_parity_access) {
277 0 : for (l = 0; l < state->level; ++l) {
278 0 : if (disk->device == state->parity[l].device) {
279 : /* LCOV_EXCL_START */
280 : log_fatal("Disk '%s' and %s '%s' are on the same device.\n", disk->dir, lev_name(l), state->parity[l].path);
281 : #ifdef _WIN32
282 : log_fatal("Both have the serial number '%" PRIx64 "'.\n", disk->device);
283 : log_fatal("Try using the 'VolumeID' tool by 'Mark Russinovich'\n");
284 : log_fatal("to change one of the disk serial.\n");
285 : #endif
286 : exit(EXIT_FAILURE);
287 : /* LCOV_EXCL_STOP */
288 : }
289 : }
290 : }
291 :
292 0 : if (state->pool[0] != 0 && disk->device == state->pool_device) {
293 : /* LCOV_EXCL_START */
294 : log_fatal("Disk '%s' and pool '%s' are on the same device.\n", disk->dir, state->pool);
295 : #ifdef _WIN32
296 : log_fatal("Both have the serial number '%" PRIx64 "'.\n", disk->device);
297 : log_fatal("Try using the 'VolumeID' tool by 'Mark Russinovich'\n");
298 : log_fatal("to change one of the disk serial.\n");
299 : #endif
300 : exit(EXIT_FAILURE);
301 : /* LCOV_EXCL_STOP */
302 : }
303 : }
304 : }
305 :
306 : /* check device of parity disks */
307 246 : if (!state->opt.skip_device && !state->opt.skip_parity_access) {
308 0 : for (l = 0; l < state->level; ++l) {
309 : unsigned j;
310 :
311 : /* skip parity disks that are not accessible */
312 0 : if (state->parity[l].skip_access)
313 0 : continue;
314 :
315 : #ifdef _WIN32
316 : if (state->parity[l].device == 0) {
317 : /* LCOV_EXCL_START */
318 : log_fatal("Disk '%s' has a zero serial number.\n", state->parity[l].path);
319 : log_fatal("This is not necessarely wrong, but for using SnapRAID\n");
320 : log_fatal("it's better to change the serial number of the disk.\n");
321 : log_fatal("Try using the 'VolumeID' tool by 'Mark Russinovich'.\n");
322 : exit(EXIT_FAILURE);
323 : /* LCOV_EXCL_STOP */
324 : }
325 : #endif
326 :
327 0 : for (j = l + 1; j < state->level; ++j) {
328 0 : if (state->parity[l].device == state->parity[j].device) {
329 0 : if (state->opt.force_device) {
330 : /* mark disks to be skipped */
331 0 : state->parity[l].skip_access = 1;
332 0 : state->parity[j].skip_access = 1;
333 0 : log_fatal("DANGER! Skipping parities '%s' and '%s' on the same device\n", lev_config_name(l), lev_config_name(j));
334 : } else {
335 : /* LCOV_EXCL_START */
336 : log_fatal("Parity '%s' and '%s' are on the same device.\n", state->parity[l].path, state->parity[j].path);
337 : #ifdef _WIN32
338 : log_fatal("Both have the serial number '%" PRIx64 "'.\n", state->parity[l].device);
339 : log_fatal("Try using the 'VolumeID' tool by 'Mark Russinovich'\n");
340 : log_fatal("to change one of the disk serial.\n");
341 : #endif
342 : /* in "fix" we allow to continue anyway */
343 : if (strcmp(state->command, "fix") == 0) {
344 : log_fatal("You can '%s' anyway, using 'snapraid --force-device %s'.\n", state->command, state->command);
345 : }
346 : exit(EXIT_FAILURE);
347 : /* LCOV_EXCL_STOP */
348 : }
349 : }
350 : }
351 :
352 : /* skip parity disks that are not accessible */
353 0 : if (state->parity[l].skip_access)
354 0 : continue;
355 :
356 0 : if (state->pool[0] != 0 && state->pool_device == state->parity[l].device) {
357 : /* LCOV_EXCL_START */
358 : log_fatal("Pool '%s' and parity '%s' are on the same device.\n", state->pool, state->parity[l].path);
359 : #ifdef _WIN32
360 : log_fatal("Both have the serial number '%" PRIx64 "'.\n", state->pool_device);
361 : log_fatal("Try using the 'VolumeID' tool by 'Mark Russinovich'\n");
362 : log_fatal("to change one of the disk serial.\n");
363 : #endif
364 : exit(EXIT_FAILURE);
365 : /* LCOV_EXCL_STOP */
366 : }
367 : }
368 : }
369 :
370 : /* check device of pool disk */
371 : #ifdef _WIN32
372 : if (!state->opt.skip_device) {
373 : if (state->pool[0] != 0 && state->pool_device == 0) {
374 : /* LCOV_EXCL_START */
375 : log_fatal("Disk '%s' has a zero serial number.\n", state->pool);
376 : log_fatal("This is not necessarely wrong, but for using SnapRAID\n");
377 : log_fatal("it's better to change the serial number of the disk.\n");
378 : log_fatal("Try using the 'VolumeID' tool by 'Mark Russinovich'.\n");
379 : exit(EXIT_FAILURE);
380 : /* LCOV_EXCL_STOP */
381 : }
382 : }
383 : #endif
384 :
385 : /* count the content files */
386 246 : if (!state->opt.skip_device && !state->opt.skip_content_access) {
387 : unsigned content_count;
388 :
389 0 : content_count = 0;
390 0 : for (i = state->contentlist; i != 0; i = i->next) {
391 : tommy_node* j;
392 0 : struct snapraid_content* content = i->data;
393 :
394 : /* check if there are others in the same disk */
395 0 : for (j = i->next; j != 0; j = j->next) {
396 0 : struct snapraid_content* other = j->data;
397 0 : if (content->device == other->device) {
398 0 : break;
399 : }
400 : }
401 0 : if (j != 0) {
402 : /* skip it */
403 0 : continue;
404 : }
405 :
406 0 : ++content_count;
407 : }
408 :
409 0 : if (content_count < state->level + 1) {
410 : /* LCOV_EXCL_START */
411 : log_fatal("You must have at least %d 'content' files in different disks.\n", state->level + 1);
412 : exit(EXIT_FAILURE);
413 : /* LCOV_EXCL_STOP */
414 : }
415 : }
416 :
417 : /* check for speed */
418 : #ifdef CONFIG_X86
419 246 : if (!raid_cpu_has_ssse3())
420 : #endif
421 0 : if (state->raid_mode == RAID_MODE_CAUCHY) {
422 0 : if (state->level == 3) {
423 0 : log_fatal("WARNING! Your CPU doesn't have a fast implementation for triple parity.\n");
424 0 : log_fatal("WARNING! It's recommended to switch to 'z-parity' instead than '3-parity'.\n");
425 0 : } else if (state->level > 3) {
426 0 : log_fatal("WARNING! Your CPU doesn't have a fast implementation beyond triple parity.\n");
427 0 : log_fatal("WARNING! It's recommended to reduce the parity levels to triple parity.\n");
428 : }
429 : }
430 :
431 : /* ensure that specified filter disks are valid ones */
432 254 : for (i = tommy_list_head(filterlist_disk); i != 0; i = i->next) {
433 : tommy_node* j;
434 8 : struct snapraid_filter* filter = i->data;
435 43 : for (j = state->disklist; j != 0; j = j->next) {
436 38 : struct snapraid_disk* disk = j->data;
437 38 : if (fnmatch(filter->pattern, disk->name, FNM_CASEINSENSITIVE_FOR_WIN) == 0)
438 3 : break;
439 : }
440 8 : if (j == 0) {
441 : /* check matching with parity disks */
442 20 : for (l = 0; l < state->level; ++l)
443 20 : if (fnmatch(filter->pattern, lev_config_name(l), FNM_CASEINSENSITIVE_FOR_WIN) == 0)
444 5 : break;
445 5 : if (l == state->level) {
446 : /* LCOV_EXCL_START */
447 : log_fatal("Option -d, --filter-disk %s doesn't match any data or parity disk.\n", filter->pattern);
448 : exit(EXIT_FAILURE);
449 : /* LCOV_EXCL_STOP */
450 : }
451 : }
452 : }
453 246 : }
454 :
455 : /**
456 : * Validate the smartctl command.
457 : *
458 : * It must contains only one %s string, and not other % chars.
459 : */
460 344 : static int validate_smartctl(const char* custom)
461 : {
462 344 : const char* s = custom;
463 344 : int arg = 0;
464 :
465 2408 : while (*s) {
466 1720 : if (s[0] == '%' && s[1] == 's') {
467 172 : if (arg) {
468 : /* LCOV_EXCL_START */
469 : return -1;
470 : /* LCOV_EXCL_STOP */
471 : }
472 172 : arg = 1;
473 1548 : } else if (s[0] == '%') {
474 : /* LCOV_EXCL_START */
475 : return -1;
476 : /* LCOV_EXCL_STOP */
477 : }
478 :
479 1720 : ++s;
480 : }
481 :
482 344 : return 0;
483 : }
484 :
485 247 : void state_config(struct snapraid_state* state, const char* path, const char* command, struct snapraid_option* opt, tommy_list* filterlist_disk)
486 : {
487 : STREAM* f;
488 : unsigned line;
489 : tommy_node* i;
490 : unsigned l;
491 :
492 : /* copy the options */
493 247 : state->opt = *opt;
494 :
495 : /* if unset, sort by physical order */
496 247 : if (!state->opt.force_order)
497 1 : state->opt.force_order = SORT_PHYSICAL;
498 :
499 : /* adjust file mode */
500 247 : if (state->opt.skip_sequential)
501 1 : state->file_mode &= ~MODE_SEQUENTIAL;
502 :
503 : /* store current command */
504 247 : state->command = command;
505 :
506 247 : log_tag("conf:%s\n", path);
507 :
508 247 : f = sopen_read(path);
509 247 : if (!f) {
510 : /* LCOV_EXCL_START */
511 : if (errno == ENOENT) {
512 : log_fatal("No configuration file found at '%s'\n", path);
513 : } else if (errno == EACCES) {
514 : log_fatal("You do not have rights to access the configuration file '%s'\n", path);
515 : } else {
516 : log_fatal("Error opening the configuration file '%s'. %s.\n", path, strerror(errno));
517 : }
518 : exit(EXIT_FAILURE);
519 : /* LCOV_EXCL_STOP */
520 : }
521 :
522 247 : line = 1;
523 : while (1) {
524 : char tag[PATH_MAX];
525 : char buffer[PATH_MAX];
526 : int ret;
527 : int c;
528 : unsigned level;
529 :
530 : /* skip initial spaces */
531 5808 : sgetspace(f);
532 :
533 : /* read the command */
534 5808 : ret = sgettok(f, tag, sizeof(tag));
535 5808 : if (ret < 0) {
536 : /* LCOV_EXCL_START */
537 : log_fatal("Error reading the configuration file '%s' at line %u\n", path, line);
538 : exit(EXIT_FAILURE);
539 : /* LCOV_EXCL_STOP */
540 : }
541 :
542 : /* skip spaces after the command */
543 5808 : sgetspace(f);
544 :
545 5808 : if (strcmp(tag, "blocksize") == 0
546 : /* block_size is the old format of the option */
547 5561 : || strcmp(tag, "block_size") == 0) {
548 :
549 247 : ret = sgetu32(f, &state->block_size);
550 247 : if (ret < 0) {
551 : /* LCOV_EXCL_START */
552 : log_fatal("Invalid 'blocksize' specification in '%s' at line %u\n", path, line);
553 : exit(EXIT_FAILURE);
554 : /* LCOV_EXCL_STOP */
555 : }
556 247 : if (state->block_size < 1) {
557 : /* LCOV_EXCL_START */
558 : log_fatal("Too small 'blocksize' specification in '%s' at line %u\n", path, line);
559 : exit(EXIT_FAILURE);
560 : /* LCOV_EXCL_STOP */
561 : }
562 247 : if (state->block_size > 16 * 1024) {
563 : /* LCOV_EXCL_START */
564 : log_fatal("Too big 'blocksize' specification in '%s' at line %u\n", path, line);
565 : exit(EXIT_FAILURE);
566 : /* LCOV_EXCL_STOP */
567 : }
568 : /* check if it's a power of 2 */
569 247 : if ((state->block_size & (state->block_size - 1)) != 0) {
570 : /* LCOV_EXCL_START */
571 : log_fatal("Not power of 2 'blocksize' specification in '%s' at line %u\n", path, line);
572 : exit(EXIT_FAILURE);
573 : /* LCOV_EXCL_STOP */
574 : }
575 247 : state->block_size *= 1024;
576 5561 : } else if (lev_config_scan(tag, &level, &state->raid_mode) == 0) {
577 : char device[PATH_MAX];
578 : char* slash;
579 : uint64_t dev;
580 : int skip_access;
581 :
582 1193 : if (*state->parity[level].path) {
583 : /* LCOV_EXCL_START */
584 : log_fatal("Multiple '%s' specification in '%s' at line %u\n", tag, path, line);
585 : exit(EXIT_FAILURE);
586 : /* LCOV_EXCL_STOP */
587 : }
588 :
589 1193 : ret = sgetlasttok(f, buffer, sizeof(buffer));
590 1193 : if (ret < 0) {
591 : /* LCOV_EXCL_START */
592 : log_fatal("Invalid '%s' specification in '%s' at line %u\n", tag, path, line);
593 : exit(EXIT_FAILURE);
594 : /* LCOV_EXCL_STOP */
595 : }
596 :
597 1193 : if (!*buffer) {
598 : /* LCOV_EXCL_START */
599 : log_fatal("Empty '%s' specification in '%s' at line %u\n", tag, path, line);
600 : exit(EXIT_FAILURE);
601 : /* LCOV_EXCL_STOP */
602 : }
603 :
604 1193 : pathimport(state->parity[level].path, sizeof(state->parity[level].path), buffer);
605 :
606 1193 : skip_access = 0;
607 1193 : if (!state->opt.skip_parity_access) {
608 : struct stat st;
609 :
610 : /* get the device of the directory containing the parity file */
611 1018 : pathimport(device, sizeof(device), buffer);
612 1018 : slash = strrchr(device, '/');
613 1018 : if (slash)
614 1018 : *slash = 0;
615 : else
616 0 : pathcpy(device, sizeof(device), ".");
617 :
618 1018 : if (stat(device, &st) == 0) {
619 1016 : dev = st.st_dev;
620 : } else {
621 : /* if the disk can be skipped */
622 2 : if (state->opt.force_device) {
623 : /* use a fake device, and mark the disk to be skipped */
624 1 : dev = 0;
625 1 : skip_access = 1;
626 1 : log_fatal("DANGER! Skipping unaccessible parity disk '%s'...\n", tag);
627 : } else {
628 : /* LCOV_EXCL_START */
629 : log_fatal("Error accessing 'parity' dir '%s' specification in '%s' at line %u\n", device, path, line);
630 :
631 : /* in "fix" we allow to continue anyway */
632 : if (strcmp(state->command, "fix") == 0) {
633 : log_fatal("You can '%s' anyway, using 'snapraid --force-device %s'.\n", state->command, state->command);
634 : }
635 : exit(EXIT_FAILURE);
636 : /* LCOV_EXCL_STOP */
637 : }
638 : }
639 : } else {
640 : /* use a fake device */
641 175 : dev = 0;
642 : }
643 :
644 1192 : state->parity[level].device = dev;
645 1192 : state->parity[level].skip_access = skip_access;
646 :
647 : /* adjust the level */
648 1192 : if (state->level < level + 1)
649 945 : state->level = level + 1;
650 4368 : } else if (strcmp(tag, "share") == 0) {
651 34 : if (*state->share) {
652 : /* LCOV_EXCL_START */
653 : log_fatal("Multiple 'share' specification in '%s' at line %u\n", path, line);
654 : exit(EXIT_FAILURE);
655 : /* LCOV_EXCL_STOP */
656 : }
657 :
658 34 : ret = sgetlasttok(f, buffer, sizeof(buffer));
659 34 : if (ret < 0) {
660 : /* LCOV_EXCL_START */
661 : log_fatal("Invalid 'share' specification in '%s' at line %u\n", path, line);
662 : exit(EXIT_FAILURE);
663 : /* LCOV_EXCL_STOP */
664 : }
665 :
666 34 : if (!*buffer) {
667 : /* LCOV_EXCL_START */
668 : log_fatal("Empty 'share' specification in '%s' at line %u\n", path, line);
669 : exit(EXIT_FAILURE);
670 : /* LCOV_EXCL_STOP */
671 : }
672 :
673 34 : pathimport(state->share, sizeof(state->share), buffer);
674 4334 : } else if (strcmp(tag, "pool") == 0) {
675 : struct stat st;
676 :
677 34 : if (*state->pool) {
678 : /* LCOV_EXCL_START */
679 : log_fatal("Multiple 'pool' specification in '%s' at line %u\n", path, line);
680 : exit(EXIT_FAILURE);
681 : /* LCOV_EXCL_STOP */
682 : }
683 :
684 34 : ret = sgetlasttok(f, buffer, sizeof(buffer));
685 34 : if (ret < 0) {
686 : /* LCOV_EXCL_START */
687 : log_fatal("Invalid 'pool' specification in '%s' at line %u\n", path, line);
688 : exit(EXIT_FAILURE);
689 : /* LCOV_EXCL_STOP */
690 : }
691 :
692 34 : if (!*buffer) {
693 : /* LCOV_EXCL_START */
694 : log_fatal("Empty 'pool' specification in '%s' at line %u\n", path, line);
695 : exit(EXIT_FAILURE);
696 : /* LCOV_EXCL_STOP */
697 : }
698 :
699 34 : pathimport(state->pool, sizeof(state->pool), buffer);
700 :
701 : /* get the device of the directory containing the pool tree */
702 34 : if (stat(buffer, &st) != 0) {
703 : /* LCOV_EXCL_START */
704 : log_fatal("Error accessing 'pool' dir '%s' specification in '%s' at line %u\n", buffer, path, line);
705 : exit(EXIT_FAILURE);
706 : /* LCOV_EXCL_STOP */
707 : }
708 :
709 34 : state->pool_device = st.st_dev;
710 4300 : } else if (strcmp(tag, "content") == 0) {
711 : struct snapraid_content* content;
712 : char device[PATH_MAX];
713 : char* slash;
714 : struct stat st;
715 : uint64_t dev;
716 :
717 1433 : ret = sgetlasttok(f, buffer, sizeof(buffer));
718 1433 : if (ret < 0) {
719 : /* LCOV_EXCL_START */
720 : log_fatal("Invalid 'content' specification in '%s' at line %u\n", path, line);
721 : exit(EXIT_FAILURE);
722 : /* LCOV_EXCL_STOP */
723 : }
724 :
725 1433 : if (pathcmp(buffer, "/dev/null") == 0 || pathcmp(buffer, "NUL") == 0) {
726 : /* LCOV_EXCL_START */
727 : log_fatal("You cannot use the null device as 'content' specification in '%s' at line %u\n", path, line);
728 : exit(EXIT_FAILURE);
729 : /* LCOV_EXCL_STOP */
730 : }
731 :
732 1433 : if (!*buffer) {
733 : /* LCOV_EXCL_START */
734 : log_fatal("Empty 'content' specification in '%s' at line %u\n", path, line);
735 : exit(EXIT_FAILURE);
736 : /* LCOV_EXCL_STOP */
737 : }
738 :
739 : /* check if the content file is already specified */
740 5367 : for (i = state->contentlist; i != 0; i = i->next) {
741 3934 : content = i->data;
742 3934 : if (pathcmp(content->content, buffer) == 0)
743 0 : break;
744 : }
745 1433 : if (i) {
746 : /* LCOV_EXCL_START */
747 : log_fatal("Duplicate 'content' specification in '%s' at line %u\n", path, line);
748 : exit(EXIT_FAILURE);
749 : /* LCOV_EXCL_STOP */
750 : }
751 :
752 : /* get the device of the directory containing the content file */
753 1433 : pathimport(device, sizeof(device), buffer);
754 1433 : slash = strrchr(device, '/');
755 1433 : if (slash)
756 1433 : *slash = 0;
757 : else
758 0 : pathcpy(device, sizeof(device), ".");
759 1433 : if (stat(device, &st) == 0) {
760 1432 : dev = st.st_dev;
761 : } else {
762 1 : if (state->opt.skip_content_access) {
763 : /* use a fake device */
764 1 : dev = 0;
765 1 : log_fatal("WARNING! Skipping unaccessible content file '%s'...\n", buffer);
766 : } else {
767 : /* LCOV_EXCL_START */
768 : log_fatal("Error accessing 'content' dir '%s' specification in '%s' at line %u\n", device, path, line);
769 : exit(EXIT_FAILURE);
770 : /* LCOV_EXCL_STOP */
771 : }
772 : }
773 :
774 : /* set the lock file at the first accessible content file */
775 1433 : if (state->lockfile[0] == 0 && dev != 0) {
776 246 : pathcpy(state->lockfile, sizeof(state->lockfile), buffer);
777 246 : pathcat(state->lockfile, sizeof(state->lockfile), ".lock");
778 : }
779 :
780 1433 : content = content_alloc(buffer, dev);
781 :
782 1433 : tommy_list_insert_tail(&state->contentlist, &content->node, content);
783 2867 : } else if (strcmp(tag, "disk") == 0) {
784 : char dir[PATH_MAX];
785 : char device[PATH_MAX];
786 : struct snapraid_disk* disk;
787 : uint64_t dev;
788 : int skip_access;
789 :
790 1471 : ret = sgettok(f, buffer, sizeof(buffer));
791 1471 : if (ret < 0) {
792 : /* LCOV_EXCL_START */
793 : log_fatal("Invalid 'disk' name specification in '%s' at line %u\n", path, line);
794 : exit(EXIT_FAILURE);
795 : /* LCOV_EXCL_STOP */
796 : }
797 :
798 1471 : if (!*buffer) {
799 : /* LCOV_EXCL_START */
800 : log_fatal("Empty 'disk' name specification in '%s' at line %u\n", path, line);
801 : exit(EXIT_FAILURE);
802 : /* LCOV_EXCL_STOP */
803 : }
804 :
805 1471 : sgetspace(f);
806 :
807 1471 : ret = sgetlasttok(f, dir, sizeof(dir));
808 1471 : if (ret < 0) {
809 : /* LCOV_EXCL_START */
810 : log_fatal("Invalid 'disk' dir specification in '%s' at line %u\n", path, line);
811 : exit(EXIT_FAILURE);
812 : /* LCOV_EXCL_STOP */
813 : }
814 :
815 1471 : if (!*dir) {
816 : /* LCOV_EXCL_START */
817 : log_fatal("Empty 'disk' dir specification in '%s' at line %u\n", path, line);
818 : exit(EXIT_FAILURE);
819 : /* LCOV_EXCL_STOP */
820 : }
821 :
822 : /* get the device of the dir */
823 1471 : pathimport(device, sizeof(device), dir);
824 :
825 : /* check if the disk name already exists */
826 5136 : for (i = state->disklist; i != 0; i = i->next) {
827 3665 : disk = i->data;
828 3665 : if (strcmp(disk->name, buffer) == 0)
829 0 : break;
830 : }
831 1471 : if (i) {
832 : /* LCOV_EXCL_START */
833 : log_fatal("Duplicate disk name '%s' at line %u\n", buffer, line);
834 : exit(EXIT_FAILURE);
835 : /* LCOV_EXCL_STOP */
836 : }
837 :
838 : /* if the disk has to be present */
839 1471 : skip_access = 0;
840 1471 : if (!state->opt.skip_disk_access) {
841 : struct stat st;
842 :
843 1327 : if (stat(device, &st) == 0) {
844 1326 : dev = st.st_dev;
845 : } else {
846 : /* if the disk can be skipped */
847 1 : if (state->opt.force_device) {
848 : /* use a fake device, and mark the disk to be skipped */
849 1 : dev = 0;
850 1 : skip_access = 1;
851 1 : log_fatal("DANGER! Skipping unaccessible data disk '%s'...\n", buffer);
852 : } else {
853 : /* LCOV_EXCL_START */
854 : log_fatal("Error accessing 'disk' '%s' specification in '%s' at line %u\n", dir, device, line);
855 :
856 : /* in "fix" we allow to continue anyway */
857 : if (strcmp(state->command, "fix") == 0) {
858 : log_fatal("You can '%s' anyway, using 'snapraid --force-device %s'.\n", state->command, state->command);
859 : }
860 : exit(EXIT_FAILURE);
861 : /* LCOV_EXCL_STOP */
862 : }
863 : }
864 : } else {
865 : /* use a fake device */
866 144 : dev = 0;
867 : }
868 :
869 1471 : disk = disk_alloc(buffer, dir, dev, skip_access);
870 :
871 1471 : tommy_list_insert_tail(&state->disklist, &disk->node, disk);
872 1396 : } else if (strcmp(tag, "smartctl") == 0) {
873 : char custom[PATH_MAX];
874 :
875 344 : ret = sgettok(f, buffer, sizeof(buffer));
876 344 : if (ret < 0) {
877 : /* LCOV_EXCL_START */
878 : log_fatal("Invalid 'smartctl' name specification in '%s' at line %u\n", path, line);
879 : exit(EXIT_FAILURE);
880 : /* LCOV_EXCL_STOP */
881 : }
882 :
883 344 : if (!*buffer) {
884 : /* LCOV_EXCL_START */
885 : log_fatal("Empty 'smartctl' name specification in '%s' at line %u\n", path, line);
886 : exit(EXIT_FAILURE);
887 : /* LCOV_EXCL_STOP */
888 : }
889 :
890 344 : sgetspace(f);
891 :
892 344 : ret = sgetlasttok(f, custom, sizeof(custom));
893 344 : if (ret < 0) {
894 : /* LCOV_EXCL_START */
895 : log_fatal("Invalid 'smartctl' option specification in '%s' at line %u\n", path, line);
896 : exit(EXIT_FAILURE);
897 : /* LCOV_EXCL_STOP */
898 : }
899 :
900 344 : if (!*custom) {
901 : /* LCOV_EXCL_START */
902 : log_fatal("Empty 'smartctl' option specification in '%s' at line %u\n", path, line);
903 : exit(EXIT_FAILURE);
904 : /* LCOV_EXCL_STOP */
905 : }
906 :
907 344 : if (validate_smartctl(custom) != 0) {
908 : /* LCOV_EXCL_START */
909 : log_fatal("Invalid 'smartctl' option specification in '%s' at line %u\n", path, line);
910 : exit(EXIT_FAILURE);
911 : /* LCOV_EXCL_STOP */
912 : }
913 :
914 : /* search for parity */
915 344 : if (lev_config_scan(buffer, &level, 0) == 0) {
916 172 : if (state->parity[level].smartctl[0] != 0) {
917 : /* LCOV_EXCL_START */
918 : log_fatal("Duplicate parity smartctl '%s' at line %u\n", buffer, line);
919 : exit(EXIT_FAILURE);
920 : /* LCOV_EXCL_STOP */
921 : }
922 :
923 172 : pathcpy(state->parity[level].smartctl, sizeof(state->parity[level].smartctl), custom);
924 : } else {
925 : /* search the disk */
926 : struct snapraid_disk* disk;
927 172 : for (i = state->disklist; i != 0; i = i->next) {
928 172 : disk = i->data;
929 172 : if (strcmp(disk->name, buffer) == 0)
930 172 : break;
931 : }
932 172 : if (!i) {
933 : /* LCOV_EXCL_START */
934 : log_fatal("Missing disk smartctl '%s' at line %u\n", buffer, line);
935 : exit(EXIT_FAILURE);
936 : /* LCOV_EXCL_STOP */
937 : }
938 172 : if (disk->smartctl[0] != 0) {
939 : /* LCOV_EXCL_START */
940 : log_fatal("Duplicate disk name '%s' at line %u\n", buffer, line);
941 : exit(EXIT_FAILURE);
942 : /* LCOV_EXCL_STOP */
943 : }
944 :
945 172 : pathcpy(disk->smartctl, sizeof(disk->smartctl), custom);
946 : }
947 1052 : } else if (strcmp(tag, "nohidden") == 0) {
948 0 : state->filter_hidden = 1;
949 1052 : } else if (strcmp(tag, "exclude") == 0) {
950 : struct snapraid_filter* filter;
951 :
952 246 : ret = sgetlasttok(f, buffer, sizeof(buffer));
953 246 : if (ret < 0) {
954 : /* LCOV_EXCL_START */
955 : log_fatal("Invalid 'exclude' specification in '%s' at line %u\n", path, line);
956 : exit(EXIT_FAILURE);
957 : /* LCOV_EXCL_STOP */
958 : }
959 :
960 246 : if (!*buffer) {
961 : /* LCOV_EXCL_START */
962 : log_fatal("Empty 'exclude' specification in '%s' at line %u\n", path, line);
963 : exit(EXIT_FAILURE);
964 : /* LCOV_EXCL_STOP */
965 : }
966 :
967 246 : filter = filter_alloc_file(-1, buffer);
968 246 : if (!filter) {
969 : /* LCOV_EXCL_START */
970 : log_fatal("Invalid 'exclude' specification '%s' in '%s' at line %u\n", buffer, path, line);
971 : log_fatal("Filters using relative paths are not supported. Ensure to add an initial slash\n");
972 : exit(EXIT_FAILURE);
973 : /* LCOV_EXCL_STOP */
974 : }
975 246 : tommy_list_insert_tail(&state->filterlist, &filter->node, filter);
976 806 : } else if (strcmp(tag, "include") == 0) {
977 : struct snapraid_filter* filter;
978 :
979 246 : ret = sgetlasttok(f, buffer, sizeof(buffer));
980 246 : if (ret < 0) {
981 : /* LCOV_EXCL_START */
982 : log_fatal("Invalid 'include' specification in '%s' at line %u\n", path, line);
983 : exit(EXIT_FAILURE);
984 : /* LCOV_EXCL_STOP */
985 : }
986 :
987 246 : if (!*buffer) {
988 : /* LCOV_EXCL_START */
989 : log_fatal("Empty 'include' specification in '%s' at line %u\n", path, line);
990 : exit(EXIT_FAILURE);
991 : /* LCOV_EXCL_STOP */
992 : }
993 :
994 246 : filter = filter_alloc_file(1, buffer);
995 246 : if (!filter) {
996 : /* LCOV_EXCL_START */
997 : log_fatal("Invalid 'include' specification '%s' in '%s' at line %u\n", buffer, path, line);
998 : log_fatal("Filters using relative paths are not supported. Ensure to add an initial slash\n");
999 : exit(EXIT_FAILURE);
1000 : /* LCOV_EXCL_STOP */
1001 : }
1002 246 : tommy_list_insert_tail(&state->filterlist, &filter->node, filter);
1003 560 : } else if (strcmp(tag, "autosave") == 0) {
1004 : char* e;
1005 :
1006 34 : ret = sgetlasttok(f, buffer, sizeof(buffer));
1007 34 : if (ret < 0) {
1008 : /* LCOV_EXCL_START */
1009 : log_fatal("Invalid 'autosave' specification in '%s' at line %u\n", path, line);
1010 : exit(EXIT_FAILURE);
1011 : /* LCOV_EXCL_STOP */
1012 : }
1013 :
1014 34 : if (!*buffer) {
1015 : /* LCOV_EXCL_START */
1016 : log_fatal("Empty 'autosave' specification in '%s' at line %u\n", path, line);
1017 : exit(EXIT_FAILURE);
1018 : /* LCOV_EXCL_STOP */
1019 : }
1020 :
1021 34 : state->autosave = strtoul(buffer, &e, 0);
1022 :
1023 34 : if (!e || *e) {
1024 : /* LCOV_EXCL_START */
1025 : log_fatal("Invalid 'autosave' specification in '%s' at line %u\n", path, line);
1026 : exit(EXIT_FAILURE);
1027 : /* LCOV_EXCL_STOP */
1028 : }
1029 :
1030 : /* convert to GB */
1031 34 : state->autosave *= GIGA;
1032 526 : } else if (tag[0] == 0) {
1033 : /* allow empty lines */
1034 34 : } else if (tag[0] == '#') {
1035 34 : ret = sgetline(f, buffer, sizeof(buffer));
1036 34 : if (ret < 0) {
1037 : /* LCOV_EXCL_START */
1038 : log_fatal("Invalid comment in '%s' at line %u\n", path, line);
1039 : exit(EXIT_FAILURE);
1040 : /* LCOV_EXCL_STOP */
1041 : }
1042 : } else {
1043 : /* LCOV_EXCL_START */
1044 : log_fatal("Invalid command '%s' in '%s' at line %u\n", tag, path, line);
1045 : exit(EXIT_FAILURE);
1046 : /* LCOV_EXCL_STOP */
1047 : }
1048 :
1049 : /* skip final spaces */
1050 5807 : sgetspace(f);
1051 :
1052 : /* next line */
1053 5807 : c = sgeteol(f);
1054 5807 : if (c == EOF) {
1055 246 : break;
1056 : }
1057 5561 : if (c != '\n') {
1058 : /* LCOV_EXCL_START */
1059 : log_fatal("Extra data in '%s' at line %u\n", path, line);
1060 : exit(EXIT_FAILURE);
1061 : /* LCOV_EXCL_STOP */
1062 : }
1063 5561 : ++line;
1064 5561 : }
1065 :
1066 246 : if (serror(f)) {
1067 : /* LCOV_EXCL_START */
1068 : log_fatal("Error reading the configuration file '%s' at line %u\n", path, line);
1069 : exit(EXIT_FAILURE);
1070 : /* LCOV_EXCL_STOP */
1071 : }
1072 :
1073 246 : sclose(f);
1074 :
1075 246 : state_config_check(state, path, filterlist_disk);
1076 :
1077 : /* select the default hash */
1078 246 : if (state->opt.force_murmur3) {
1079 1 : state->besthash = HASH_MURMUR3;
1080 245 : } else if (state->opt.force_spooky2) {
1081 1 : state->besthash = HASH_SPOOKY2;
1082 : } else {
1083 : #ifdef CONFIG_X86
1084 244 : if (sizeof(void*) == 4 && !raid_cpu_has_slowmult())
1085 244 : state->besthash = HASH_MURMUR3;
1086 : else
1087 0 : state->besthash = HASH_SPOOKY2;
1088 : #else
1089 : if (sizeof(void*) == 4)
1090 : state->besthash = HASH_MURMUR3;
1091 : else
1092 : state->besthash = HASH_SPOOKY2;
1093 : #endif
1094 : }
1095 :
1096 : /* by default use the best hash */
1097 246 : state->hash = state->besthash;
1098 :
1099 : /* by default use a random hash seed */
1100 246 : if (randomize(state->hashseed, HASH_SIZE) != 0) {
1101 : /* LCOV_EXCL_START */
1102 : log_fatal("Failed to get random values.\n");
1103 : exit(EXIT_FAILURE);
1104 : /* LCOV_EXCL_STOP */
1105 : }
1106 :
1107 : /* no previous hash by default */
1108 246 : state->prevhash = HASH_UNDEFINED;
1109 :
1110 : /* intentionally not set the prevhashseed, if used valgrind will warn about it */
1111 :
1112 246 : log_tag("blocksize:%u\n", state->block_size);
1113 1717 : for (i = state->disklist; i != 0; i = i->next) {
1114 1471 : struct snapraid_disk* disk = i->data;
1115 1471 : log_tag("disk:%s:%s\n", disk->name, disk->dir);
1116 : }
1117 :
1118 246 : log_tag("mode:%s\n", lev_raid_name(state->raid_mode, state->level));
1119 1433 : for (l = 0; l < state->level; ++l)
1120 1187 : log_tag("%s:%s\n", lev_config_name(l), state->parity[l].path);
1121 246 : if (state->pool[0] != 0)
1122 34 : log_tag("pool:%s\n", state->pool);
1123 246 : if (state->share[0] != 0)
1124 34 : log_tag("share:%s\n", state->share);
1125 246 : if (state->autosave != 0)
1126 34 : log_tag("autosave:%" PRIu64 "\n", state->autosave);
1127 738 : for (i = tommy_list_head(&state->filterlist); i != 0; i = i->next) {
1128 : char out[PATH_MAX];
1129 492 : struct snapraid_filter* filter = i->data;
1130 492 : log_tag("filter:%s\n", filter_type(filter, out, sizeof(out)));
1131 : }
1132 246 : if (state->filter_hidden)
1133 0 : log_tag("filter:nohidden:\n");
1134 246 : log_flush();
1135 246 : }
1136 :
1137 : /**
1138 : * Find a disk by name.
1139 : */
1140 11051 : static struct snapraid_disk* find_disk(struct snapraid_state* state, const char* name)
1141 : {
1142 : tommy_node* i;
1143 :
1144 38600 : for (i = state->disklist; i != 0; i = i->next) {
1145 38594 : struct snapraid_disk* disk = i->data;
1146 38594 : if (strcmp(disk->name, name) == 0)
1147 11045 : return disk;
1148 : }
1149 :
1150 6 : if (state->no_conf) {
1151 : /* without a configuration file, add disks automatically */
1152 : struct snapraid_disk* disk;
1153 :
1154 6 : disk = disk_alloc(name, "DUMMY/", -1, 0);
1155 :
1156 6 : tommy_list_insert_tail(&state->disklist, &disk->node, disk);
1157 :
1158 6 : return disk;
1159 : }
1160 :
1161 0 : return 0;
1162 : }
1163 :
1164 : /**
1165 : * Update the disk mapping if required.
1166 : */
1167 241 : static void state_map(struct snapraid_state* state)
1168 : {
1169 : unsigned hole;
1170 : tommy_node* i;
1171 : unsigned uuid_mismatch;
1172 : unsigned diskcount;
1173 : unsigned l;
1174 :
1175 : /* remove all the mapping without a disk */
1176 : /* this happens when a disk is removed from the configuration file */
1177 : /* From SnapRAID 4.0 mappings are automatically removed if a disk is not used */
1178 : /* when saving the content file, but we keep this code to import older content files. */
1179 1781 : for (i = state->maplist; i != 0; ) {
1180 1299 : struct snapraid_map* map = i->data;
1181 : struct snapraid_disk* disk;
1182 :
1183 1299 : disk = find_disk(state, map->name);
1184 :
1185 : /* go to the next mapping before removing */
1186 1299 : i = i->next;
1187 :
1188 1299 : if (disk == 0) {
1189 : /* disk not found, remove the mapping */
1190 0 : tommy_list_remove_existing(&state->maplist, &map->node);
1191 0 : map_free(map);
1192 : }
1193 : }
1194 :
1195 : /* maps each unmapped disk present in the configuration file in the first available hole */
1196 : /* this happens when you add disks for the first time in the configuration file */
1197 241 : hole = 0; /* first position to try */
1198 1682 : for (i = state->disklist; i != 0; i = i->next) {
1199 1441 : struct snapraid_disk* disk = i->data;
1200 : struct snapraid_map* map;
1201 : tommy_node* j;
1202 :
1203 : /* check if the disk is already mapped */
1204 5031 : for (j = state->maplist; j != 0; j = j->next) {
1205 4889 : map = j->data;
1206 4889 : if (strcmp(disk->name, map->name) == 0) {
1207 : /* mapping found */
1208 1299 : break;
1209 : }
1210 : }
1211 1441 : if (j != 0) {
1212 : /* mapping is present, then copy the free blocks into to disk */
1213 1299 : disk->total_blocks = map->total_blocks;
1214 1299 : disk->free_blocks = map->free_blocks;
1215 1299 : continue;
1216 : }
1217 :
1218 : /* mapping not found, search for an hole */
1219 : while (1) {
1220 900 : for (j = state->maplist; j != 0; j = j->next) {
1221 758 : map = j->data;
1222 758 : if (map->position == hole) {
1223 : /* position already used */
1224 127 : break;
1225 : }
1226 : }
1227 269 : if (j == 0) {
1228 : /* hole found */
1229 142 : break;
1230 : }
1231 :
1232 : /* try with the next one */
1233 127 : ++hole;
1234 127 : }
1235 :
1236 : /* insert the new mapping */
1237 142 : map = map_alloc(disk->name, hole, 0, 0, "");
1238 :
1239 142 : tommy_list_insert_tail(&state->maplist, &map->node, map);
1240 : }
1241 :
1242 : /* without configuration don't check for number of data disks or uuid changes */
1243 241 : if (state->no_conf)
1244 242 : return;
1245 :
1246 : /* counter for the number of UUID mismatches */
1247 240 : uuid_mismatch = 0;
1248 :
1249 : /* check if mapping match the disk uuid */
1250 240 : if (!state->opt.skip_disk_access) {
1251 1514 : for (i = state->maplist; i != 0; i = i->next) {
1252 1297 : struct snapraid_map* map = i->data;
1253 : struct snapraid_disk* disk;
1254 : char uuid[UUID_MAX];
1255 : int ret;
1256 :
1257 1297 : disk = find_disk(state, map->name);
1258 1297 : if (disk == 0) {
1259 : /* LCOV_EXCL_START */
1260 : log_fatal("Internal inconsistency for mapping '%s'\n", map->name);
1261 : exit(EXIT_FAILURE);
1262 : /* LCOV_EXCL_STOP */
1263 : }
1264 :
1265 1297 : ret = devuuid(disk->device, uuid, sizeof(uuid));
1266 1297 : if (ret != 0) {
1267 : /* uuid not available, just ignore but marks the disk with unsupported UUID */
1268 1 : disk->has_unsupported_uuid = 1;
1269 1 : continue;
1270 : }
1271 :
1272 : /* if the uuid is changed */
1273 1296 : if (strcmp(uuid, map->uuid) != 0) {
1274 : /* mark the disk as with an UUID change */
1275 94 : disk->has_different_uuid = 1;
1276 :
1277 : /* if the previous uuid is available */
1278 94 : if (map->uuid[0] != 0) {
1279 : /* count the number of uuid change */
1280 0 : ++uuid_mismatch;
1281 0 : log_fatal("UUID change for disk '%s' from '%s' to '%s'\n", disk->name, map->uuid, uuid);
1282 : } else {
1283 : /* no message here, because having a disk without */
1284 : /* UUID is the normal state of an empty disk */
1285 94 : disk->had_empty_uuid = 1;
1286 : }
1287 :
1288 : /* update the uuid in the mapping, */
1289 94 : pathcpy(map->uuid, sizeof(map->uuid), uuid);
1290 :
1291 : /* write the new state with the new uuid */
1292 94 : state->need_write = 1;
1293 : }
1294 : }
1295 : }
1296 :
1297 : /* check the parity uuid */
1298 240 : if (!state->opt.skip_parity_access) {
1299 1188 : for (l = 0; l < state->level; ++l) {
1300 : char uuid[UUID_MAX];
1301 : int ret;
1302 :
1303 982 : ret = devuuid(state->parity[l].device, uuid, sizeof(uuid));
1304 982 : if (ret != 0) {
1305 : /* uuid not available, just ignore */
1306 1 : continue;
1307 : }
1308 :
1309 : /* if the uuid is changed */
1310 981 : if (strcmp(uuid, state->parity[l].uuid) != 0) {
1311 : /* if the previous uuid is available */
1312 46 : if (state->parity[l].uuid[0] != 0) {
1313 : /* count the number of uuid change */
1314 0 : ++uuid_mismatch;
1315 0 : log_fatal("UUID change for parity '%s' from '%s' to '%s'\n", lev_config_name(l), state->parity[l].uuid, uuid);
1316 : }
1317 :
1318 : /* update the uuid */
1319 46 : pathcpy(state->parity[l].uuid, sizeof(state->parity[l].uuid), uuid);
1320 :
1321 : /* write the new state with the new uuid */
1322 46 : state->need_write = 1;
1323 : }
1324 : }
1325 : }
1326 :
1327 240 : if (!state->opt.force_uuid && uuid_mismatch > state->level) {
1328 : /* LCOV_EXCL_START */
1329 : log_fatal("Too many disks have UUID changed from the latest 'sync'.\n");
1330 : log_fatal("If this happens because you really replaced them,\n");
1331 : log_fatal("you can '%s' anyway, using 'snapraid --force-uuid %s'.\n", state->command, state->command);
1332 : log_fatal("Instead, it's possible that you messed up the disk mount points,\n");
1333 : log_fatal("and you have to restore the mount points at the state of the latest sync.\n");
1334 : exit(EXIT_FAILURE);
1335 : /* LCOV_EXCL_STOP */
1336 : }
1337 :
1338 : /* count the number of data disks, including holes left after removing some */
1339 240 : diskcount = 0;
1340 1675 : for (i = state->maplist; i != 0; i = i->next) {
1341 1435 : struct snapraid_map* map = i->data;
1342 :
1343 1435 : if (map->position + 1 > diskcount)
1344 1304 : diskcount = map->position + 1;
1345 : }
1346 :
1347 : /* ensure to don't go over the limit of the RAID engine */
1348 240 : if (diskcount > RAID_DATA_MAX) {
1349 : /* LCOV_EXCL_START */
1350 : log_fatal("Too many data disks. No more than %u.\n", RAID_DATA_MAX);
1351 : exit(EXIT_FAILURE);
1352 : /* LCOV_EXCL_STOP */
1353 : }
1354 :
1355 : /* now count the real number of data disks, excluding holes left after removing some */
1356 240 : diskcount = tommy_list_count(&state->maplist);
1357 :
1358 : /* recommend number of parities */
1359 240 : if (diskcount >= 36 && state->level < 6) {
1360 0 : log_fatal("WARNING! With %u disks it's recommended to use six parity levels.\n", diskcount);
1361 240 : } else if (diskcount >= 29 && state->level < 5) {
1362 0 : log_fatal("WARNING! With %u disks it's recommended to use five parity levels.\n", diskcount);
1363 240 : } else if (diskcount >= 22 && state->level < 4) {
1364 0 : log_fatal("WARNING! With %u disks it's recommended to use four parity levels.\n", diskcount);
1365 240 : } else if (diskcount >= 15 && state->level < 3) {
1366 0 : log_fatal("WARNING! With %u disks it's recommended to use three parity levels.\n", diskcount);
1367 240 : } else if (diskcount >= 5 && state->level < 2) {
1368 34 : log_fatal("WARNING! With %u disks it's recommended to use two parity levels.\n", diskcount);
1369 : }
1370 : }
1371 :
1372 144 : void state_refresh(struct snapraid_state* state)
1373 : {
1374 : tommy_node* i;
1375 : unsigned l;
1376 :
1377 : /* for all disks */
1378 1006 : for (i = state->maplist; i != 0; i = i->next) {
1379 862 : struct snapraid_map* map = i->data;
1380 : struct snapraid_disk* disk;
1381 : uint64_t total_space;
1382 : uint64_t free_space;
1383 : int ret;
1384 :
1385 862 : disk = find_disk(state, map->name);
1386 862 : if (disk == 0) {
1387 : /* LCOV_EXCL_START */
1388 : log_fatal("Internal inconsistency for mapping '%s'\n", map->name);
1389 : exit(EXIT_FAILURE);
1390 : /* LCOV_EXCL_STOP */
1391 : }
1392 :
1393 862 : ret = fsinfo(disk->dir, 0, &total_space, &free_space);
1394 862 : if (ret != 0) {
1395 : /* LCOV_EXCL_START */
1396 : log_fatal("Error accessing disk '%s' to get filesystem info. %s.\n", disk->dir, strerror(errno));
1397 : exit(EXIT_FAILURE);
1398 : /* LCOV_EXCL_STOP */
1399 : }
1400 :
1401 : /* set the new free blocks */
1402 862 : map->total_blocks = total_space / state->block_size;
1403 862 : map->free_blocks = free_space / state->block_size;
1404 :
1405 : /* also update the disk info */
1406 862 : disk->total_blocks = map->total_blocks;
1407 862 : disk->free_blocks = map->free_blocks;
1408 : }
1409 :
1410 : /* for all parities */
1411 978 : for (l = 0; l < state->level; ++l) {
1412 : uint64_t total_space;
1413 : uint64_t free_space;
1414 : int ret;
1415 :
1416 834 : ret = fsinfo(state->parity[l].path, 0, &total_space, &free_space);
1417 834 : if (ret != 0) {
1418 : /* LCOV_EXCL_START */
1419 : log_fatal("Error accessing file '%s' to get filesystem info. %s.\n", state->parity[l].path, strerror(errno));
1420 : exit(EXIT_FAILURE);
1421 : /* LCOV_EXCL_STOP */
1422 : }
1423 :
1424 : /* set the new free blocks */
1425 834 : state->parity[l].total_blocks = total_space / state->block_size;
1426 834 : state->parity[l].free_blocks = free_space / state->block_size;
1427 : }
1428 :
1429 : /* note what we don't set need_write = 1, because we don't want */
1430 : /* to update the content file only for the free space info. */
1431 144 : }
1432 :
1433 : /**
1434 : * Check the content.
1435 : */
1436 234 : static void state_content_check(struct snapraid_state* state, const char* path)
1437 : {
1438 : tommy_node* i;
1439 :
1440 : /* check that any map has different name and position */
1441 1633 : for (i = state->maplist; i != 0; i = i->next) {
1442 1399 : struct snapraid_map* map = i->data;
1443 : tommy_node* j;
1444 4884 : for (j = i->next; j != 0; j = j->next) {
1445 3485 : struct snapraid_map* other = j->data;
1446 3485 : if (strcmp(map->name, other->name) == 0) {
1447 : /* LCOV_EXCL_START */
1448 : log_fatal("Colliding 'map' disk specification in '%s'\n", path);
1449 : exit(EXIT_FAILURE);
1450 : /* LCOV_EXCL_STOP */
1451 : }
1452 3485 : if (map->position == other->position) {
1453 : /* LCOV_EXCL_START */
1454 : log_fatal("Colliding 'map' index specification in '%s'\n", path);
1455 : exit(EXIT_FAILURE);
1456 : /* LCOV_EXCL_STOP */
1457 : }
1458 : }
1459 : }
1460 234 : }
1461 :
1462 : /**
1463 : * Check if the position is REQUIRED, or we can completely clear it from the state.
1464 : *
1465 : * Note that position with only DELETED blocks are discarged.
1466 : */
1467 729418 : static int fs_position_is_required(struct snapraid_state* state, block_off_t pos)
1468 : {
1469 : tommy_node* i;
1470 :
1471 : /* check for each disk */
1472 773475 : for (i = state->disklist; i != 0; i = i->next) {
1473 773475 : struct snapraid_disk* disk = i->data;
1474 773475 : struct snapraid_block* block = fs_par2block_get(disk, pos);
1475 :
1476 : /* if we have at least one file, the position is needed */
1477 773475 : if (block_has_file(block))
1478 729418 : return 1;
1479 : }
1480 :
1481 0 : return 0;
1482 : }
1483 :
1484 : /**
1485 : * Check if the info block is REQUIREQ.
1486 : *
1487 : * This is used to ensure that we keep the last check used for scrubbing.
1488 : * and that we add it when importing old context files.
1489 : *
1490 : * Note that you can have position without info blocks, for example
1491 : * if all the blocks are not synced.
1492 : *
1493 : * Note also that not requiring an info block, doesn't mean that if present it
1494 : * can be discarded.
1495 : */
1496 1491820 : static int fs_info_is_required(struct snapraid_state* state, block_off_t pos)
1497 : {
1498 : tommy_node* i;
1499 :
1500 : /* check for each disk */
1501 1985452 : for (i = state->disklist; i != 0; i = i->next) {
1502 1927007 : struct snapraid_disk* disk = i->data;
1503 1927007 : struct snapraid_block* block = fs_par2block_get(disk, pos);
1504 :
1505 : /* if we have at least one synced file, the info is required */
1506 1927007 : if (block_state_get(block) == BLOCK_STATE_BLK)
1507 1433375 : return 1;
1508 : }
1509 :
1510 58445 : return 0;
1511 : }
1512 :
1513 0 : static void fs_position_clear_deleted(struct snapraid_state* state, block_off_t pos)
1514 : {
1515 : tommy_node* i;
1516 :
1517 : /* check for each disk if block is really used */
1518 0 : for (i = state->disklist; i != 0; i = i->next) {
1519 0 : struct snapraid_disk* disk = i->data;
1520 0 : struct snapraid_block* block = fs_par2block_get(disk, pos);
1521 :
1522 : /* if the block is deleted */
1523 0 : if (block_state_get(block) == BLOCK_STATE_DELETED) {
1524 : /* set it to empty */
1525 0 : fs_deallocate(disk, pos);
1526 : }
1527 : }
1528 0 : }
1529 :
1530 : /**
1531 : * Check if a block position in a disk is deleted.
1532 : */
1533 29383119 : static int fs_is_block_deleted_ts(struct snapraid_disk* disk, struct snapraid_chunk** fs_last, block_off_t pos)
1534 : {
1535 29383119 : struct snapraid_block* block = fs_par2block_get_ts(disk, fs_last, pos);
1536 :
1537 29381514 : return block_state_get(block) == BLOCK_STATE_DELETED;
1538 : }
1539 :
1540 : /**
1541 : * Flush the file checking the final CRC.
1542 : * We exploit the fact that the CRC is always stored in the last 4 bytes.
1543 : */
1544 1 : static void decoding_error(const char* path, STREAM* f)
1545 : {
1546 : unsigned char buf[4];
1547 : uint32_t crc_stored;
1548 : uint32_t crc_computed;
1549 :
1550 1 : if (seof(f)) {
1551 : /* LCOV_EXCL_START */
1552 : log_fatal("Unexpected end of content file '%s' at offset %" PRIi64 "\n", path, stell(f));
1553 : log_fatal("This content file is truncated. Use an alternate copy.\n");
1554 : exit(EXIT_FAILURE);
1555 : /* LCOV_EXCL_STOP */
1556 : }
1557 :
1558 1 : if (serror(f)) {
1559 : /* LCOV_EXCL_START */
1560 : log_fatal("Error reading the content file '%s' at offset %" PRIi64 "\n", path, stell(f));
1561 : exit(EXIT_FAILURE);
1562 : /* LCOV_EXCL_STOP */
1563 : }
1564 :
1565 1 : log_fatal("Decoding error in '%s' at offset %" PRIi64 "\n", path, stell(f));
1566 :
1567 1 : if (sdeplete(f, buf) != 0) {
1568 : /* LCOV_EXCL_START */
1569 : log_fatal("Error flushing the content file '%s' at offset %" PRIi64 "\n", path, stell(f));
1570 : exit(EXIT_FAILURE);
1571 : /* LCOV_EXCL_STOP */
1572 : }
1573 :
1574 : /* get the stored crc from the last four bytes */
1575 1 : crc_stored = buf[0] | (uint32_t)buf[1] << 8 | (uint32_t)buf[2] << 16 | (uint32_t)buf[3] << 24;
1576 :
1577 : /* get the computed crc */
1578 1 : crc_computed = scrc(f);
1579 :
1580 : /* adjust the stored crc to include itself */
1581 1 : crc_stored = crc32c(crc_stored, buf, 4);
1582 :
1583 1 : if (crc_computed != crc_stored) {
1584 1 : log_fatal("Mismatching CRC in '%s'\n", path);
1585 1 : log_fatal("This content file is damaged! Use an alternate copy.\n");
1586 1 : exit(EXIT_FAILURE);
1587 : } else {
1588 0 : log_fatal("The file CRC is correct!\n");
1589 : }
1590 0 : }
1591 :
1592 235 : static void state_read_content(struct snapraid_state* state, const char* path, STREAM* f)
1593 : {
1594 : block_off_t blockmax;
1595 : unsigned count_file;
1596 : unsigned count_hardlink;
1597 : unsigned count_symlink;
1598 : unsigned count_dir;
1599 : int crc_checked;
1600 : char buffer[PATH_MAX];
1601 : int ret;
1602 : tommy_array disk_mapping;
1603 : uint32_t mapping_max;
1604 :
1605 235 : blockmax = 0;
1606 235 : count_file = 0;
1607 235 : count_hardlink = 0;
1608 235 : count_symlink = 0;
1609 235 : count_dir = 0;
1610 235 : crc_checked = 0;
1611 235 : mapping_max = 0;
1612 235 : tommy_array_init(&disk_mapping);
1613 :
1614 235 : ret = sread(f, buffer, 12);
1615 235 : if (ret < 0) {
1616 : /* LCOV_EXCL_START */
1617 : decoding_error(path, f);
1618 : log_fatal("Invalid header!\n");
1619 : exit(EXIT_FAILURE);
1620 : /* LCOV_EXCL_STOP */
1621 : }
1622 :
1623 : /*
1624 : * File format versions:
1625 : * - SNAPCNT1/SnapRAID 4.0 First version.
1626 : * - SNAPCNT2/SnapRAID 7.0 Adds entries 'M' and 'P', to add free_blocks support.
1627 : * The previous 'm' entry is now deprecated, but supported for importing.
1628 : * Similarly for text file, we add 'mapping' and 'parity' deprecating 'map'.
1629 : */
1630 235 : if (memcmp(buffer, "SNAPCNT1\n\3\0\0", 12) != 0
1631 235 : && memcmp(buffer, "SNAPCNT2\n\3\0\0", 12) != 0) {
1632 : /* LCOV_EXCL_START */
1633 : if (memcmp(buffer, "SNAPCNT", 7) != 0) {
1634 : decoding_error(path, f);
1635 : log_fatal("Invalid header!\n");
1636 : } else {
1637 : log_fatal("The content file '%s' was generated with a newer version of SnapRAID!\n", path);
1638 : }
1639 : exit(EXIT_FAILURE);
1640 : /* LCOV_EXCL_STOP */
1641 : }
1642 :
1643 : while (1) {
1644 : int c;
1645 :
1646 : /* read the command */
1647 3604981 : c = sgetc(f);
1648 3604981 : if (c == EOF) {
1649 234 : break;
1650 : }
1651 :
1652 3604747 : if (c == 'f') {
1653 : /* file */
1654 : char sub[PATH_MAX];
1655 : uint64_t v_size;
1656 : uint64_t v_mtime_sec;
1657 : uint32_t v_mtime_nsec;
1658 : uint64_t v_inode;
1659 : uint32_t v_idx;
1660 : struct snapraid_file* file;
1661 : struct snapraid_disk* disk;
1662 : uint32_t mapping;
1663 :
1664 3485836 : ret = sgetb32(f, &mapping);
1665 3485836 : if (ret < 0 || mapping >= mapping_max) {
1666 : /* LCOV_EXCL_START */
1667 : decoding_error(path, f);
1668 : log_fatal("Internal inconsistency in mapping index!\n");
1669 : exit(EXIT_FAILURE);
1670 : /* LCOV_EXCL_STOP */
1671 : }
1672 3485836 : disk = tommy_array_get(&disk_mapping, mapping);
1673 :
1674 3485836 : ret = sgetb64(f, &v_size);
1675 3485836 : if (ret < 0) {
1676 : /* LCOV_EXCL_START */
1677 : decoding_error(path, f);
1678 : exit(EXIT_FAILURE);
1679 : /* LCOV_EXCL_STOP */
1680 : }
1681 :
1682 : /* check for impossible file size to avoid to crash for a too big allocation */
1683 3485836 : if (v_size / state->block_size > blockmax) {
1684 : /* LCOV_EXCL_START */
1685 : decoding_error(path, f);
1686 : log_fatal("Internal inconsistency in file size too big!\n");
1687 : exit(EXIT_FAILURE);
1688 : /* LCOV_EXCL_STOP */
1689 : }
1690 :
1691 3485836 : ret = sgetb64(f, &v_mtime_sec);
1692 3485836 : if (ret < 0) {
1693 : /* LCOV_EXCL_START */
1694 : decoding_error(path, f);
1695 : exit(EXIT_FAILURE);
1696 : /* LCOV_EXCL_STOP */
1697 : }
1698 :
1699 3485836 : ret = sgetb32(f, &v_mtime_nsec);
1700 3485836 : if (ret < 0) {
1701 : /* LCOV_EXCL_START */
1702 : decoding_error(path, f);
1703 : exit(EXIT_FAILURE);
1704 : /* LCOV_EXCL_STOP */
1705 : }
1706 :
1707 : /* STAT_NSEC_INVALID is encoded as 0 */
1708 3485836 : if (v_mtime_nsec == 0)
1709 0 : v_mtime_nsec = STAT_NSEC_INVALID;
1710 : else
1711 3485836 : --v_mtime_nsec;
1712 :
1713 3485836 : ret = sgetb64(f, &v_inode);
1714 3485836 : if (ret < 0) {
1715 : /* LCOV_EXCL_START */
1716 : decoding_error(path, f);
1717 : exit(EXIT_FAILURE);
1718 : /* LCOV_EXCL_STOP */
1719 : }
1720 :
1721 3485836 : ret = sgetbs(f, sub, sizeof(sub));
1722 3485836 : if (ret < 0) {
1723 : /* LCOV_EXCL_START */
1724 : decoding_error(path, f);
1725 : exit(EXIT_FAILURE);
1726 : /* LCOV_EXCL_STOP */
1727 : }
1728 3485836 : if (!*sub) {
1729 : /* LCOV_EXCL_START */
1730 : decoding_error(path, f);
1731 : exit(EXIT_FAILURE);
1732 : /* LCOV_EXCL_STOP */
1733 : }
1734 :
1735 : /* allocate the file */
1736 3485836 : file = file_alloc(state->block_size, sub, v_size, v_mtime_sec, v_mtime_nsec, v_inode, 0);
1737 :
1738 : /* insert the file in the file containers */
1739 3485836 : tommy_hashdyn_insert(&disk->inodeset, &file->nodeset, file, file_inode_hash(file->inode));
1740 3485836 : tommy_hashdyn_insert(&disk->pathset, &file->pathset, file, file_path_hash(file->sub));
1741 3485836 : tommy_hashdyn_insert(&disk->stampset, &file->stampset, file, file_stamp_hash(file->size, file->mtime_sec, file->mtime_nsec));
1742 3485836 : tommy_list_insert_tail(&disk->filelist, &file->nodelist, file);
1743 :
1744 : /* read all the blocks */
1745 3485836 : v_idx = 0;
1746 10477605 : while (v_idx < file->blockmax) {
1747 : block_off_t v_pos;
1748 : uint32_t v_count;
1749 :
1750 : /* get the "subcommand */
1751 3505934 : c = sgetc(f);
1752 :
1753 3505934 : ret = sgetb32(f, &v_pos);
1754 3505934 : if (ret < 0) {
1755 : /* LCOV_EXCL_START */
1756 : decoding_error(path, f);
1757 : exit(EXIT_FAILURE);
1758 : /* LCOV_EXCL_STOP */
1759 : }
1760 :
1761 3505934 : ret = sgetb32(f, &v_count);
1762 3505934 : if (ret < 0) {
1763 : /* LCOV_EXCL_START */
1764 : decoding_error(path, f);
1765 : exit(EXIT_FAILURE);
1766 : /* LCOV_EXCL_STOP */
1767 : }
1768 :
1769 3505934 : if (v_idx + v_count > file->blockmax) {
1770 : /* LCOV_EXCL_START */
1771 : decoding_error(path, f);
1772 : log_fatal("Internal inconsistency in block number!\n");
1773 : exit(EXIT_FAILURE);
1774 : /* LCOV_EXCL_STOP */
1775 : }
1776 :
1777 3505933 : if (v_pos + v_count > blockmax) {
1778 : /* LCOV_EXCL_START */
1779 : log_fatal("Internal inconsistency in block size %u/%u!\n", blockmax, v_pos + v_count);
1780 : decoding_error(path, f);
1781 : exit(EXIT_FAILURE);
1782 : /* LCOV_EXCL_START */
1783 : }
1784 :
1785 : /* fill the blocks in the run */
1786 : while (v_count) {
1787 : struct snapraid_block* block = fs_file2block_get(file, v_idx);
1788 :
1789 : switch (c) {
1790 : case 'b' :
1791 : block_state_set(block, BLOCK_STATE_BLK);
1792 : break;
1793 : case 'n' :
1794 : /* deprecated NEW blocks are converted to CHG ones */
1795 : block_state_set(block, BLOCK_STATE_CHG);
1796 : break;
1797 : case 'g' :
1798 : block_state_set(block, BLOCK_STATE_CHG);
1799 : break;
1800 : case 'p' :
1801 : block_state_set(block, BLOCK_STATE_REP);
1802 : break;
1803 : default :
1804 : /* LCOV_EXCL_START */
1805 : decoding_error(path, f);
1806 : log_fatal("Invalid block type!\n");
1807 : exit(EXIT_FAILURE);
1808 : /* LCOV_EXCL_STOP */
1809 : }
1810 :
1811 : /* read the hash only for 'blk/chg/rep', and not for 'new' */
1812 8573599 : if (c != 'n') {
1813 8573599 : ret = sread(f, block->hash, HASH_SIZE);
1814 8573599 : if (ret < 0) {
1815 : /* LCOV_EXCL_START */
1816 : decoding_error(path, f);
1817 : exit(EXIT_FAILURE);
1818 : /* LCOV_EXCL_STOP */
1819 : }
1820 : } else {
1821 : /* set the ZERO hash for deprecated NEW blocks */
1822 0 : hash_zero_set(block->hash);
1823 : }
1824 :
1825 : /* if the block contains a hash of past data */
1826 : /* and we are clearing such undeterminated hashes */
1827 8573599 : if (state->clear_past_hash
1828 2287716 : && block_has_past_hash(block)
1829 : ) {
1830 : /* set the hash value to INVALID */
1831 57602 : hash_invalid_set(block->hash);
1832 : }
1833 :
1834 : /* if we are disabling the copy optimization */
1835 : /* we want also to clear any already previously stored information */
1836 : /* in other sync commands */
1837 : /* note that this is required only in sync, and we detect */
1838 : /* this using the clear_past_hash flag */
1839 8573599 : if (state->clear_past_hash
1840 2287716 : && state->opt.force_nocopy
1841 14115 : && block_state_get(block) == BLOCK_STATE_REP
1842 : ) {
1843 : /* set the hash value to INVALID */
1844 1 : hash_invalid_set(block->hash);
1845 : /* convert from REP to CHG block */
1846 1 : block_state_set(block, BLOCK_STATE_CHG);
1847 : }
1848 :
1849 : /* if we want a full sync, marks block as invalid parity */
1850 : /* note that we do this after the force_nocopy option */
1851 : /* to avoid to mixup the two things */
1852 8573599 : if (state->opt.force_full
1853 27746 : && block_state_get(block) == BLOCK_STATE_BLK) {
1854 : /* convert from BLK to REP */
1855 27746 : block_state_set(block, BLOCK_STATE_REP);
1856 : }
1857 :
1858 : /* set the parity association */
1859 8573599 : fs_allocate(disk, v_pos, file, v_idx);
1860 :
1861 : /* go to the next block */
1862 8573599 : ++v_idx;
1863 8573599 : ++v_pos;
1864 8573599 : --v_count;
1865 : }
1866 : }
1867 :
1868 : /* stat */
1869 3485835 : ++count_file;
1870 118911 : } else if (c == 'i') {
1871 : /* "inf" command */
1872 : snapraid_info info;
1873 : uint32_t v_pos;
1874 : uint32_t v_oldest;
1875 :
1876 234 : ret = sgetb32(f, &v_oldest);
1877 234 : if (ret < 0) {
1878 : /* LCOV_EXCL_START */
1879 : decoding_error(path, f);
1880 : exit(EXIT_FAILURE);
1881 : /* LCOV_EXCL_STOP */
1882 : }
1883 :
1884 234 : v_pos = 0;
1885 284859 : while (v_pos < blockmax) {
1886 : int bad;
1887 : int rehash;
1888 : int justsynced;
1889 : uint32_t t;
1890 : uint32_t flag;
1891 : uint32_t v_count;
1892 :
1893 284391 : ret = sgetb32(f, &v_count);
1894 284391 : if (ret < 0) {
1895 : /* LCOV_EXCL_START */
1896 : decoding_error(path, f);
1897 : exit(EXIT_FAILURE);
1898 : /* LCOV_EXCL_STOP */
1899 : }
1900 :
1901 284391 : if (v_pos + v_count > blockmax) {
1902 : /* LCOV_EXCL_START */
1903 : log_fatal("Internal inconsistency in info size %u/%u!\n", blockmax, v_pos + v_count);
1904 : decoding_error(path, f);
1905 : exit(EXIT_FAILURE);
1906 : /* LCOV_EXCL_STOP */
1907 : }
1908 :
1909 284391 : ret = sgetb32(f, &flag);
1910 284391 : if (ret < 0) {
1911 : /* LCOV_EXCL_START */
1912 : decoding_error(path, f);
1913 : exit(EXIT_FAILURE);
1914 : /* LCOV_EXCL_STOP */
1915 : }
1916 :
1917 : /* if there is an info */
1918 284391 : if ((flag & 1) != 0) {
1919 : /* read the time */
1920 284376 : ret = sgetb32(f, &t);
1921 284376 : if (ret < 0) {
1922 : /* LCOV_EXCL_START */
1923 : decoding_error(path, f);
1924 : exit(EXIT_FAILURE);
1925 : /* LCOV_EXCL_STOP */
1926 : }
1927 :
1928 : /* analyze the flags */
1929 284376 : bad = (flag & 2) != 0;
1930 284376 : rehash = (flag & 4) != 0;
1931 284376 : justsynced = (flag & 8) != 0;
1932 :
1933 284376 : if (rehash && state->prevhash == HASH_UNDEFINED) {
1934 : /* LCOV_EXCL_START */
1935 : decoding_error(path, f);
1936 : log_fatal("Internal inconsistency for missing previous checksum!\n");
1937 : exit(EXIT_FAILURE);
1938 : /* LCOV_EXCL_STOP */
1939 : }
1940 :
1941 284376 : info = info_make(t + v_oldest, bad, rehash, justsynced);
1942 : } else {
1943 15 : info = 0;
1944 : }
1945 :
1946 2060602 : while (v_count) {
1947 : /* insert the info in the array */
1948 1491820 : info_set(&state->infoarr, v_pos, info);
1949 :
1950 : /* ensure that an info is present only for used positions */
1951 1491820 : if (fs_info_is_required(state, v_pos)) {
1952 1433375 : if (!info) {
1953 : /* LCOV_EXCL_START */
1954 : decoding_error(path, f);
1955 : log_fatal("Internal inconsistency for missing info!\n");
1956 : exit(EXIT_FAILURE);
1957 : /* LCOV_EXCL_STOP */
1958 : }
1959 : } else {
1960 : /* extra info are accepted for backward compatibility */
1961 : /* they are discarged at the first write */
1962 : }
1963 :
1964 : /* go to next block */
1965 1491820 : ++v_pos;
1966 1491820 : --v_count;
1967 : }
1968 : }
1969 118677 : } else if (c == 'h') {
1970 : /* hole */
1971 : uint32_t v_pos;
1972 : struct snapraid_disk* disk;
1973 : uint32_t mapping;
1974 :
1975 1299 : ret = sgetb32(f, &mapping);
1976 1299 : if (ret < 0 || mapping >= mapping_max) {
1977 : /* LCOV_EXCL_START */
1978 : decoding_error(path, f);
1979 : log_fatal("Internal inconsistency in mapping index!\n");
1980 : exit(EXIT_FAILURE);
1981 : /* LCOV_EXCL_STOP */
1982 : }
1983 1299 : disk = tommy_array_get(&disk_mapping, mapping);
1984 :
1985 1299 : v_pos = 0;
1986 3965 : while (v_pos < blockmax) {
1987 : uint32_t v_idx;
1988 : uint32_t v_count;
1989 : struct snapraid_file* deleted;
1990 :
1991 1367 : ret = sgetb32(f, &v_count);
1992 1367 : if (ret < 0) {
1993 : /* LCOV_EXCL_START */
1994 : decoding_error(path, f);
1995 : exit(EXIT_FAILURE);
1996 : /* LCOV_EXCL_STOP */
1997 : }
1998 :
1999 1367 : if (v_pos + v_count > blockmax) {
2000 : /* LCOV_EXCL_START */
2001 : log_fatal("Internal inconsistency in hole size %u/%u!\n", blockmax, v_pos + v_count);
2002 : decoding_error(path, f);
2003 : exit(EXIT_FAILURE);
2004 : /* LCOV_EXCL_STOP */
2005 : }
2006 :
2007 : /* get the subcommand */
2008 1367 : c = sgetc(f);
2009 :
2010 1367 : switch (c) {
2011 : case 'o' :
2012 : /* if it's a run of deleted blocks */
2013 :
2014 : /* allocate a fake deleted file */
2015 36 : deleted = file_alloc(state->block_size, "<deleted>", v_count * state->block_size, 0, 0, 0, 0);
2016 :
2017 : /* mark the file as deleted */
2018 36 : file_flag_set(deleted, FILE_IS_DELETED);
2019 :
2020 : /* insert it in the list of deleted files */
2021 36 : tommy_list_insert_tail(&disk->deletedlist, &deleted->nodelist, deleted);
2022 :
2023 : /* process all blocks */
2024 36 : v_idx = 0;
2025 27959 : while (v_count) {
2026 27887 : struct snapraid_block* block = fs_file2block_get(deleted, v_idx);
2027 :
2028 : /* set the block as deleted */
2029 27887 : block_state_set(block, BLOCK_STATE_DELETED);
2030 :
2031 : /* read the hash */
2032 27887 : ret = sread(f, block->hash, HASH_SIZE);
2033 27887 : if (ret < 0) {
2034 : /* LCOV_EXCL_START */
2035 : decoding_error(path, f);
2036 : exit(EXIT_FAILURE);
2037 : /* LCOV_EXCL_STOP */
2038 : }
2039 :
2040 : /* if we are clearing undeterminated hashes */
2041 27887 : if (state->clear_past_hash) {
2042 : /* set the hash value to INVALID */
2043 9433 : hash_invalid_set(block->hash);
2044 : }
2045 :
2046 : /* insert the block in the block array */
2047 27887 : fs_allocate(disk, v_pos, deleted, v_idx);
2048 :
2049 : /* go to next block */
2050 27887 : ++v_pos;
2051 27887 : ++v_idx;
2052 27887 : --v_count;
2053 : }
2054 36 : break;
2055 : case 'O' :
2056 : /* go to the next run */
2057 1331 : v_pos += v_count;
2058 1331 : break;
2059 : default :
2060 : /* LCOV_EXCL_START */
2061 : decoding_error(path, f);
2062 : log_fatal("Invalid hole type!\n");
2063 : exit(EXIT_FAILURE);
2064 : /* LCOV_EXCL_STOP */
2065 : }
2066 : }
2067 117378 : } else if (c == 's') {
2068 : /* symlink */
2069 : char sub[PATH_MAX];
2070 : char linkto[PATH_MAX];
2071 : struct snapraid_link* slink;
2072 : struct snapraid_disk* disk;
2073 : uint32_t mapping;
2074 :
2075 112395 : ret = sgetb32(f, &mapping);
2076 112395 : if (ret < 0 || mapping >= mapping_max) {
2077 : /* LCOV_EXCL_START */
2078 : decoding_error(path, f);
2079 : log_fatal("Internal inconsistency in mapping index!\n");
2080 : exit(EXIT_FAILURE);
2081 : /* LCOV_EXCL_STOP */
2082 : }
2083 112395 : disk = tommy_array_get(&disk_mapping, mapping);
2084 :
2085 112395 : ret = sgetbs(f, sub, sizeof(sub));
2086 112395 : if (ret < 0) {
2087 : /* LCOV_EXCL_START */
2088 : decoding_error(path, f);
2089 : exit(EXIT_FAILURE);
2090 : /* LCOV_EXCL_STOP */
2091 : }
2092 :
2093 112395 : ret = sgetbs(f, linkto, sizeof(linkto));
2094 112395 : if (ret < 0) {
2095 : /* LCOV_EXCL_START */
2096 : decoding_error(path, f);
2097 : exit(EXIT_FAILURE);
2098 : /* LCOV_EXCL_STOP */
2099 : }
2100 :
2101 112395 : if (!*sub || !*linkto) {
2102 : /* LCOV_EXCL_START */
2103 : decoding_error(path, f);
2104 : exit(EXIT_FAILURE);
2105 : /* LCOV_EXCL_STOP */
2106 : }
2107 :
2108 : /* allocate the link as symbolic link */
2109 112395 : slink = link_alloc(sub, linkto, FILE_IS_SYMLINK);
2110 :
2111 : /* insert the link in the link containers */
2112 112395 : tommy_hashdyn_insert(&disk->linkset, &slink->nodeset, slink, link_name_hash(slink->sub));
2113 112395 : tommy_list_insert_tail(&disk->linklist, &slink->nodelist, slink);
2114 :
2115 : /* stat */
2116 112395 : ++count_symlink;
2117 4983 : } else if (c == 'a') {
2118 : /* hardlink */
2119 : char sub[PATH_MAX];
2120 : char linkto[PATH_MAX];
2121 : struct snapraid_link* slink;
2122 : struct snapraid_disk* disk;
2123 : uint32_t mapping;
2124 :
2125 567 : ret = sgetb32(f, &mapping);
2126 567 : if (ret < 0 || mapping >= mapping_max) {
2127 : /* LCOV_EXCL_START */
2128 : decoding_error(path, f);
2129 : log_fatal("Internal inconsistency in mapping index!\n");
2130 : exit(EXIT_FAILURE);
2131 : /* LCOV_EXCL_STOP */
2132 : }
2133 567 : disk = tommy_array_get(&disk_mapping, mapping);
2134 :
2135 567 : ret = sgetbs(f, sub, sizeof(sub));
2136 567 : if (ret < 0) {
2137 : /* LCOV_EXCL_START */
2138 : decoding_error(path, f);
2139 : exit(EXIT_FAILURE);
2140 : /* LCOV_EXCL_STOP */
2141 : }
2142 :
2143 567 : ret = sgetbs(f, linkto, sizeof(linkto));
2144 567 : if (ret < 0) {
2145 : /* LCOV_EXCL_START */
2146 : decoding_error(path, f);
2147 : exit(EXIT_FAILURE);
2148 : /* LCOV_EXCL_STOP */
2149 : }
2150 :
2151 567 : if (!*sub || !*linkto) {
2152 : /* LCOV_EXCL_START */
2153 : decoding_error(path, f);
2154 : exit(EXIT_FAILURE);
2155 : /* LCOV_EXCL_STOP */
2156 : }
2157 :
2158 : /* allocate the link as hard link */
2159 567 : slink = link_alloc(sub, linkto, FILE_IS_HARDLINK);
2160 :
2161 : /* insert the link in the link containers */
2162 567 : tommy_hashdyn_insert(&disk->linkset, &slink->nodeset, slink, link_name_hash(slink->sub));
2163 567 : tommy_list_insert_tail(&disk->linklist, &slink->nodelist, slink);
2164 :
2165 : /* stat */
2166 567 : ++count_hardlink;
2167 4416 : } else if (c == 'r') {
2168 : /* dir */
2169 : char sub[PATH_MAX];
2170 : struct snapraid_dir* dir;
2171 : struct snapraid_disk* disk;
2172 : uint32_t mapping;
2173 :
2174 750 : ret = sgetb32(f, &mapping);
2175 750 : if (ret < 0 || mapping >= mapping_max) {
2176 : /* LCOV_EXCL_START */
2177 : decoding_error(path, f);
2178 : log_fatal("Internal inconsistency in mapping index!\n");
2179 : exit(EXIT_FAILURE);
2180 : /* LCOV_EXCL_STOP */
2181 : }
2182 750 : disk = tommy_array_get(&disk_mapping, mapping);
2183 :
2184 750 : ret = sgetbs(f, sub, sizeof(sub));
2185 750 : if (ret < 0) {
2186 : /* LCOV_EXCL_START */
2187 : decoding_error(path, f);
2188 : exit(EXIT_FAILURE);
2189 : /* LCOV_EXCL_STOP */
2190 : }
2191 :
2192 750 : if (!*sub) {
2193 : /* LCOV_EXCL_START */
2194 : decoding_error(path, f);
2195 : exit(EXIT_FAILURE);
2196 : /* LCOV_EXCL_STOP */
2197 : }
2198 :
2199 : /* allocate the dir */
2200 750 : dir = dir_alloc(sub);
2201 :
2202 : /* insert the dir in the dir containers */
2203 750 : tommy_hashdyn_insert(&disk->dirset, &dir->nodeset, dir, dir_name_hash(dir->sub));
2204 750 : tommy_list_insert_tail(&disk->dirlist, &dir->nodelist, dir);
2205 :
2206 : /* stat */
2207 750 : ++count_dir;
2208 3666 : } else if (c == 'c') {
2209 : /* get the subcommand */
2210 235 : c = sgetc(f);
2211 :
2212 235 : switch (c) {
2213 : case 'u' :
2214 148 : state->hash = HASH_MURMUR3;
2215 148 : break;
2216 : case 'k' :
2217 87 : state->hash = HASH_SPOOKY2;
2218 87 : break;
2219 : default :
2220 : /* LCOV_EXCL_START */
2221 : decoding_error(path, f);
2222 : log_fatal("Invalid checksum!\n");
2223 : exit(EXIT_FAILURE);
2224 : /* LCOV_EXCL_STOP */
2225 : }
2226 :
2227 : /* read the seed */
2228 235 : ret = sread(f, state->hashseed, HASH_SIZE);
2229 235 : if (ret < 0) {
2230 : /* LCOV_EXCL_START */
2231 : decoding_error(path, f);
2232 : exit(EXIT_FAILURE);
2233 : /* LCOV_EXCL_STOP */
2234 : }
2235 3431 : } else if (c == 'C') {
2236 : /* get the subcommand */
2237 87 : c = sgetc(f);
2238 :
2239 87 : switch (c) {
2240 : case 'u' :
2241 87 : state->prevhash = HASH_MURMUR3;
2242 87 : break;
2243 : case 'k' :
2244 0 : state->prevhash = HASH_SPOOKY2;
2245 0 : break;
2246 : default :
2247 : /* LCOV_EXCL_START */
2248 : decoding_error(path, f);
2249 : log_fatal("Invalid checksum!\n");
2250 : exit(EXIT_FAILURE);
2251 : /* LCOV_EXCL_STOP */
2252 : }
2253 :
2254 : /* read the seed */
2255 87 : ret = sread(f, state->prevhashseed, HASH_SIZE);
2256 87 : if (ret < 0) {
2257 : /* LCOV_EXCL_START */
2258 : decoding_error(path, f);
2259 : exit(EXIT_FAILURE);
2260 : /* LCOV_EXCL_STOP */
2261 : }
2262 3344 : } else if (c == 'z') {
2263 : block_off_t blksize;
2264 :
2265 235 : ret = sgetb32(f, &blksize);
2266 235 : if (ret < 0) {
2267 : /* LCOV_EXCL_START */
2268 : decoding_error(path, f);
2269 : exit(EXIT_FAILURE);
2270 : /* LCOV_EXCL_STOP */
2271 : }
2272 :
2273 : /* without configuration, auto assign the block size */
2274 235 : if (state->no_conf) {
2275 1 : state->block_size = blksize;
2276 : }
2277 :
2278 235 : if (blksize != state->block_size) {
2279 : /* LCOV_EXCL_START */
2280 : decoding_error(path, f);
2281 : log_fatal("Mismatching 'blksize' and 'blocksize' specification!\n");
2282 : log_fatal("Please restore the 'blocksize' value in the configuration file to '%u'\n", blksize / 1024);
2283 : exit(EXIT_FAILURE);
2284 : /* LCOV_EXCL_STOP */
2285 : }
2286 3109 : } else if (c == 'x') {
2287 235 : ret = sgetb32(f, &blockmax);
2288 235 : if (ret < 0) {
2289 : /* LCOV_EXCL_START */
2290 : decoding_error(path, f);
2291 : exit(EXIT_FAILURE);
2292 : /* LCOV_EXCL_STOP */
2293 : }
2294 4179 : } else if (c == 'm' || c == 'M') {
2295 : struct snapraid_map* map;
2296 : char uuid[UUID_MAX];
2297 : uint32_t v_pos;
2298 : uint32_t v_total_blocks;
2299 : uint32_t v_free_blocks;
2300 : struct snapraid_disk* disk;
2301 :
2302 1305 : ret = sgetbs(f, buffer, sizeof(buffer));
2303 1305 : if (ret < 0) {
2304 : /* LCOV_EXCL_START */
2305 : decoding_error(path, f);
2306 : exit(EXIT_FAILURE);
2307 : /* LCOV_EXCL_STOP */
2308 : }
2309 :
2310 1305 : ret = sgetb32(f, &v_pos);
2311 1305 : if (ret < 0) {
2312 : /* LCOV_EXCL_START */
2313 : decoding_error(path, f);
2314 : exit(EXIT_FAILURE);
2315 : /* LCOV_EXCL_STOP */
2316 : }
2317 :
2318 : /* from SnapRAID 7.0 the 'M' command includes the free space */
2319 1305 : if (c == 'M') {
2320 1305 : ret = sgetb32(f, &v_total_blocks);
2321 1305 : if (ret < 0) {
2322 : /* LCOV_EXCL_START */
2323 : decoding_error(path, f);
2324 : exit(EXIT_FAILURE);
2325 : /* LCOV_EXCL_STOP */
2326 : }
2327 :
2328 1305 : ret = sgetb32(f, &v_free_blocks);
2329 1305 : if (ret < 0) {
2330 : /* LCOV_EXCL_START */
2331 : decoding_error(path, f);
2332 : exit(EXIT_FAILURE);
2333 : /* LCOV_EXCL_STOP */
2334 : }
2335 : } else {
2336 0 : v_total_blocks = 0;
2337 0 : v_free_blocks = 0;
2338 : }
2339 :
2340 : /* read the uuid */
2341 1305 : ret = sgetbs(f, uuid, sizeof(uuid));
2342 1305 : if (ret < 0) {
2343 : /* LCOV_EXCL_START */
2344 : decoding_error(path, f);
2345 : exit(EXIT_FAILURE);
2346 : /* LCOV_EXCL_STOP */
2347 : }
2348 :
2349 1305 : map = map_alloc(buffer, v_pos, v_total_blocks, v_free_blocks, uuid);
2350 :
2351 1305 : tommy_list_insert_tail(&state->maplist, &map->node, map);
2352 :
2353 : /* find the disk */
2354 1305 : disk = find_disk(state, buffer);
2355 1305 : if (!disk) {
2356 : /* LCOV_EXCL_START */
2357 : decoding_error(path, f);
2358 : log_fatal("Disk named '%s' not present in the configuration file!\n", buffer);
2359 : log_fatal("If you have removed it from the configuration file, please restore it\n");
2360 : exit(EXIT_FAILURE);
2361 : /* LCOV_EXCL_STOP */
2362 : }
2363 :
2364 : /* insert in the mapping vector */
2365 1305 : tommy_array_grow(&disk_mapping, mapping_max + 1);
2366 1305 : tommy_array_set(&disk_mapping, mapping_max, disk);
2367 1305 : ++mapping_max;
2368 1569 : } else if (c == 'P') {
2369 : /* from SnapRAID 7.0 the 'P' command includes the free space */
2370 : char uuid[UUID_MAX];
2371 : uint32_t v_level;
2372 : uint32_t v_total_blocks;
2373 : uint32_t v_free_blocks;
2374 :
2375 1335 : ret = sgetb32(f, &v_level);
2376 1335 : if (ret < 0) {
2377 : /* LCOV_EXCL_START */
2378 : decoding_error(path, f);
2379 : exit(EXIT_FAILURE);
2380 : /* LCOV_EXCL_STOP */
2381 : }
2382 :
2383 1335 : ret = sgetb32(f, &v_total_blocks);
2384 1335 : if (ret < 0) {
2385 : /* LCOV_EXCL_START */
2386 : decoding_error(path, f);
2387 : exit(EXIT_FAILURE);
2388 : /* LCOV_EXCL_STOP */
2389 : }
2390 :
2391 1335 : ret = sgetb32(f, &v_free_blocks);
2392 1335 : if (ret < 0) {
2393 : /* LCOV_EXCL_START */
2394 : decoding_error(path, f);
2395 : exit(EXIT_FAILURE);
2396 : /* LCOV_EXCL_STOP */
2397 : }
2398 :
2399 1335 : ret = sgetbs(f, uuid, sizeof(uuid));
2400 1335 : if (ret < 0) {
2401 : /* LCOV_EXCL_START */
2402 : decoding_error(path, f);
2403 : exit(EXIT_FAILURE);
2404 : /* LCOV_EXCL_STOP */
2405 : }
2406 :
2407 : /* auto configure if configuration is missing */
2408 1335 : if (state->no_conf && v_level < LEV_MAX && v_level >= state->level)
2409 5 : state->level = v_level + 1;
2410 :
2411 : /* if we use this parity entry */
2412 1335 : if (v_level < state->level) {
2413 : /* set the parity info */
2414 1081 : pathcpy(state->parity[v_level].uuid, sizeof(state->parity[v_level].uuid), uuid);
2415 1081 : state->parity[v_level].total_blocks = v_total_blocks;
2416 1081 : state->parity[v_level].free_blocks = v_free_blocks;
2417 : }
2418 234 : } else if (c == 'N') {
2419 : uint32_t crc_stored;
2420 : uint32_t crc_computed;
2421 :
2422 : /* get the crc before reading it from the file */
2423 234 : crc_computed = scrc(f);
2424 :
2425 234 : ret = sgetble32(f, &crc_stored);
2426 234 : if (ret < 0) {
2427 : /* LCOV_EXCL_START */
2428 : /* here don't call decoding_error() because it's too late to get the crc */
2429 : log_fatal("Error reading the CRC in '%s' at offset %" PRIi64 "\n", path, stell(f));
2430 : log_fatal("This content file is damaged! Use an alternate copy.\n");
2431 : exit(EXIT_FAILURE);
2432 : /* LCOV_EXCL_STOP */
2433 : }
2434 :
2435 234 : if (crc_stored != crc_computed) {
2436 : /* LCOV_EXCL_START */
2437 : /* here don't call decoding_error() because it's too late to get the crc */
2438 : log_fatal("Mismatching CRC in '%s'\n", path);
2439 : log_fatal("This content file is damaged! Use an alternate copy.\n");
2440 : exit(EXIT_FAILURE);
2441 : /* LCOV_EXCL_STOP */
2442 : }
2443 :
2444 234 : crc_checked = 1;
2445 : } else {
2446 : /* LCOV_EXCL_START */
2447 : decoding_error(path, f);
2448 : log_fatal("Invalid command '%c'!\n", (char)c);
2449 : exit(EXIT_FAILURE);
2450 : /* LCOV_EXCL_STOP */
2451 : }
2452 3604746 : }
2453 :
2454 234 : tommy_array_done(&disk_mapping);
2455 :
2456 234 : if (serror(f)) {
2457 : /* LCOV_EXCL_START */
2458 : log_fatal("Error reading the content file '%s' at offset %" PRIi64 "\n", path, stell(f));
2459 : exit(EXIT_FAILURE);
2460 : /* LCOV_EXCL_STOP */
2461 : }
2462 :
2463 234 : if (!crc_checked) {
2464 : /* LCOV_EXCL_START */
2465 : log_fatal("Finished reading '%s' without finding the CRC\n", path);
2466 : log_fatal("This content file is truncated or damaged! Use an alternate copy.\n");
2467 : exit(EXIT_FAILURE);
2468 : /* LCOV_EXCL_STOP */
2469 : }
2470 :
2471 : /* check the filesystem on all disks */
2472 234 : state_fscheck(state, "after read");
2473 :
2474 : /* check that the stored parity size matches the loaded state */
2475 234 : if (blockmax != parity_allocated_size(state)) {
2476 : /* LCOV_EXCL_START */
2477 : log_fatal("Internal inconsistency in parity size %u/%u in '%s' at offset %" PRIi64 "\n", blockmax, parity_allocated_size(state), path, stell(f));
2478 : if (state->opt.skip_content_check) {
2479 : log_fatal("Overriding.\n");
2480 : blockmax = parity_allocated_size(state);
2481 : } else {
2482 : exit(EXIT_FAILURE);
2483 : }
2484 : /* LCOV_EXCL_STOP */
2485 : }
2486 :
2487 234 : msg_verbose("%8u files\n", count_file);
2488 234 : msg_verbose("%8u hardlinks\n", count_hardlink);
2489 234 : msg_verbose("%8u symlinks\n", count_symlink);
2490 234 : msg_verbose("%8u empty dirs\n", count_dir);
2491 234 : }
2492 :
2493 : struct state_write_thread_context {
2494 : struct snapraid_state* state;
2495 : struct snapraid_content* content;
2496 : #if HAVE_PTHREAD_CREATE
2497 : pthread_t thread;
2498 : #else
2499 : void* retval;
2500 : #endif
2501 : /* input */
2502 : block_off_t blockmax;
2503 : time_t info_oldest;
2504 : int info_has_rehash;
2505 : /* output */
2506 : uint32_t crc;
2507 : unsigned count_file;
2508 : unsigned count_hardlink;
2509 : unsigned count_symlink;
2510 : unsigned count_dir;
2511 : };
2512 :
2513 913 : static void* state_write_thread(void* arg)
2514 : {
2515 913 : struct state_write_thread_context* context = arg;
2516 913 : struct snapraid_content* content = context->content;
2517 913 : struct snapraid_state* state = context->state;
2518 913 : block_off_t blockmax = context->blockmax;
2519 913 : time_t info_oldest = context->info_oldest;
2520 913 : int info_has_rehash = context->info_has_rehash;
2521 : uint32_t crc;
2522 : unsigned count_file;
2523 : unsigned count_hardlink;
2524 : unsigned count_symlink;
2525 : unsigned count_dir;
2526 : char tmp[PATH_MAX];
2527 : STREAM* f;
2528 : tommy_node* i;
2529 : block_off_t idx;
2530 : block_off_t begin;
2531 : unsigned l;
2532 :
2533 913 : count_file = 0;
2534 913 : count_hardlink = 0;
2535 913 : count_symlink = 0;
2536 913 : count_dir = 0;
2537 :
2538 913 : pathprint(tmp, sizeof(tmp), "%s.tmp", content->content);
2539 904 : f = sopen_write(tmp);
2540 913 : if (f == 0) {
2541 : /* LCOV_EXCL_START */
2542 : log_fatal("Error opening the content file '%s'. %s.\n", tmp, strerror(errno));
2543 : goto bail;
2544 : /* LCOV_EXCL_STOP */
2545 : }
2546 :
2547 : /* write header */
2548 913 : swrite("SNAPCNT2\n\3\0\0", 12, f);
2549 :
2550 : /* write block size and block max */
2551 913 : sputc('z', f);
2552 913 : sputb32(state->block_size, f);
2553 913 : sputc('x', f);
2554 913 : sputb32(blockmax, f);
2555 913 : if (serror(f)) {
2556 : /* LCOV_EXCL_START */
2557 : log_fatal("Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
2558 : goto bail;
2559 : /* LCOV_EXCL_STOP */
2560 : }
2561 :
2562 913 : sputc('c', f);
2563 913 : if (state->hash == HASH_MURMUR3) {
2564 715 : sputc('u', f);
2565 198 : } else if (state->hash == HASH_SPOOKY2) {
2566 198 : sputc('k', f);
2567 : } else {
2568 : /* LCOV_EXCL_START */
2569 : log_fatal("Unexpected hash when writing the content file '%s'.\n", serrorfile(f));
2570 : goto bail;
2571 : /* LCOV_EXCL_STOP */
2572 : }
2573 913 : swrite(state->hashseed, HASH_SIZE, f);
2574 913 : if (serror(f)) {
2575 : /* LCOV_EXCL_START */
2576 : log_fatal("Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
2577 : goto bail;
2578 : /* LCOV_EXCL_STOP */
2579 : }
2580 :
2581 : /* previous hash only present */
2582 913 : if (state->prevhash != HASH_UNDEFINED) {
2583 : /* if at least one rehash tag found, we have to save the previous hash */
2584 198 : if (info_has_rehash) {
2585 198 : sputc('C', f);
2586 198 : if (state->prevhash == HASH_MURMUR3) {
2587 198 : sputc('u', f);
2588 0 : } else if (state->prevhash == HASH_SPOOKY2) {
2589 0 : sputc('k', f);
2590 : } else {
2591 : /* LCOV_EXCL_START */
2592 : log_fatal("Unexpected prevhash when writing the content file '%s'.\n", serrorfile(f));
2593 : goto bail;
2594 : /* LCOV_EXCL_STOP */
2595 : }
2596 198 : swrite(state->prevhashseed, HASH_SIZE, f);
2597 198 : if (serror(f)) {
2598 : /* LCOV_EXCL_START */
2599 : log_fatal("Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
2600 : goto bail;
2601 : /* LCOV_EXCL_STOP */
2602 : }
2603 : }
2604 : }
2605 :
2606 : /* for each map */
2607 6391 : for (i = state->maplist; i != 0; i = i->next) {
2608 5478 : struct snapraid_map* map = i->data;
2609 : struct snapraid_disk* disk;
2610 :
2611 : /* find the disk for this mapping */
2612 5478 : disk = find_disk(state, map->name);
2613 5478 : if (!disk) {
2614 : /* LCOV_EXCL_START */
2615 : log_fatal("Internal inconsistency for unmapped disk '%s'\n", map->name);
2616 : goto bail;
2617 : /* LCOV_EXCL_STOP */
2618 : }
2619 :
2620 : /* save the mapping only if disk is mapped */
2621 5478 : if (disk->mapping_idx != -1) {
2622 5037 : sputc('M', f);
2623 5037 : sputbs(map->name, f);
2624 5037 : sputb32(map->position, f);
2625 5037 : sputb32(map->total_blocks, f);
2626 5037 : sputb32(map->free_blocks, f);
2627 5037 : sputbs(map->uuid, f);
2628 5037 : if (serror(f)) {
2629 : /* LCOV_EXCL_START */
2630 : log_fatal("Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
2631 : goto bail;
2632 : /* LCOV_EXCL_STOP */
2633 : }
2634 : }
2635 : }
2636 :
2637 : /* for each parity */
2638 6341 : for (l = 0; l < state->level; ++l) {
2639 5428 : sputc('P', f);
2640 5428 : sputb32(l, f);
2641 5428 : sputb32(state->parity[l].total_blocks, f);
2642 5428 : sputb32(state->parity[l].free_blocks, f);
2643 5428 : sputbs(state->parity[l].uuid, f);
2644 5428 : if (serror(f)) {
2645 : /* LCOV_EXCL_START */
2646 : log_fatal("Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
2647 : goto bail;
2648 : /* LCOV_EXCL_STOP */
2649 : }
2650 : }
2651 :
2652 : /* for each disk */
2653 6391 : for (i = state->disklist; i != 0; i = i->next) {
2654 : tommy_node* j;
2655 5478 : struct snapraid_disk* disk = i->data;
2656 : struct snapraid_chunk* fs_last;
2657 :
2658 : /**
2659 : * This is the last accessed chunk in the tree operations.
2660 : * Here we are inside a thread, and we cannot use the
2661 : * ::disk member to store this information.
2662 : */
2663 5478 : fs_last = 0;
2664 :
2665 : /* if the disk is not mapped, skip it */
2666 5478 : if (disk->mapping_idx < 0)
2667 441 : continue;
2668 :
2669 : /* for each file */
2670 11587893 : for (j = disk->filelist; j != 0; j = j->next) {
2671 11582856 : struct snapraid_file* file = j->data;
2672 : uint64_t size;
2673 : uint64_t mtime_sec;
2674 : int32_t mtime_nsec;
2675 : uint64_t inode;
2676 :
2677 11582856 : size = file->size;
2678 11582856 : mtime_sec = file->mtime_sec;
2679 11582856 : mtime_nsec = file->mtime_nsec;
2680 11582856 : inode = file->inode;
2681 :
2682 11582856 : sputc('f', f);
2683 11521097 : sputb32(disk->mapping_idx, f);
2684 11524134 : sputb64(size, f);
2685 11502453 : sputb64(mtime_sec, f);
2686 : /* encode STAT_NSEC_INVALID as 0 */
2687 11514392 : if (mtime_nsec == STAT_NSEC_INVALID)
2688 0 : sputb32(0, f);
2689 : else
2690 11514392 : sputb32(mtime_nsec + 1, f);
2691 11535535 : sputb64(inode, f);
2692 11530269 : sputbs(file->sub, f);
2693 11528881 : if (serror(f)) {
2694 : /* LCOV_EXCL_START */
2695 : log_fatal("Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
2696 : goto bail;
2697 : /* LCOV_EXCL_STOP */
2698 : }
2699 :
2700 : /* for all the blocks of the file */
2701 11541902 : begin = 0;
2702 34727907 : while (begin < file->blockmax) {
2703 11603149 : unsigned v_state = block_state_get(fs_file2block_get(file, begin));
2704 11623161 : block_off_t v_pos = fs_file2par_get_ts(disk, &fs_last, file, begin);
2705 : uint32_t v_count;
2706 :
2707 : block_off_t end;
2708 :
2709 : /* find the end of run of blocks */
2710 11615466 : end = begin + 1;
2711 39962808 : while (end < file->blockmax) {
2712 16803655 : if (v_state != block_state_get(fs_file2block_get(file, end)))
2713 63 : break;
2714 16856743 : if (v_pos + (end - begin) != fs_file2par_get_ts(disk, &fs_last, file, end))
2715 82936 : break;
2716 16731876 : ++end;
2717 : }
2718 :
2719 11626686 : switch (v_state) {
2720 : case BLOCK_STATE_BLK :
2721 10916510 : sputc('b', f);
2722 10892646 : break;
2723 : case BLOCK_STATE_CHG :
2724 558744 : sputc('g', f);
2725 558725 : break;
2726 : case BLOCK_STATE_REP :
2727 151432 : sputc('p', f);
2728 151433 : break;
2729 : default :
2730 : /* LCOV_EXCL_START */
2731 : log_fatal("Internal inconsistency in state for block %u state %u\n", v_pos, v_state);
2732 : goto bail;
2733 : /* LCOV_EXCL_STOP */
2734 : }
2735 :
2736 11602804 : sputb32(v_pos, f);
2737 :
2738 11631290 : v_count = end - begin;
2739 11631290 : sputb32(v_count, f);
2740 :
2741 : /* write hashes */
2742 39802834 : for (idx = begin; idx < end; ++idx) {
2743 28167488 : struct snapraid_block* block = fs_file2block_get(file, idx);
2744 :
2745 28196345 : swrite(block->hash, HASH_SIZE, f);
2746 : }
2747 :
2748 11635346 : if (serror(f)) {
2749 : /* LCOV_EXCL_START */
2750 : log_fatal("Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
2751 : goto bail;
2752 : /* LCOV_EXCL_STOP */
2753 : }
2754 :
2755 : /* next begin position */
2756 11644103 : begin = end;
2757 : }
2758 :
2759 11582856 : ++count_file;
2760 : }
2761 :
2762 : /* for each link */
2763 385486 : for (j = disk->linklist; j != 0; j = j->next) {
2764 380449 : struct snapraid_link* slink = j->data;
2765 :
2766 380449 : switch (link_flag_get(slink, FILE_IS_LINK_MASK)) {
2767 : case FILE_IS_HARDLINK :
2768 2046 : sputc('a', f);
2769 2046 : ++count_hardlink;
2770 2046 : break;
2771 : case FILE_IS_SYMLINK :
2772 378355 : sputc('s', f);
2773 378402 : ++count_symlink;
2774 378402 : break;
2775 : }
2776 :
2777 380132 : sputb32(disk->mapping_idx, f);
2778 380405 : sputbs(slink->sub, f);
2779 380450 : sputbs(slink->linkto, f);
2780 380448 : if (serror(f)) {
2781 : /* LCOV_EXCL_START */
2782 : log_fatal("Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
2783 : goto bail;
2784 : /* LCOV_EXCL_STOP */
2785 : }
2786 : }
2787 :
2788 : /* for each dir */
2789 7758 : for (j = disk->dirlist; j != 0; j = j->next) {
2790 2721 : struct snapraid_dir* dir = j->data;
2791 :
2792 2721 : sputc('r', f);
2793 2721 : sputb32(disk->mapping_idx, f);
2794 2721 : sputbs(dir->sub, f);
2795 2721 : if (serror(f)) {
2796 : /* LCOV_EXCL_START */
2797 : log_fatal("Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
2798 : goto bail;
2799 : /* LCOV_EXCL_STOP */
2800 : }
2801 :
2802 2721 : ++count_dir;
2803 : }
2804 :
2805 : /* deleted blocks of the disk */
2806 5037 : sputc('h', f);
2807 5037 : sputb32(disk->mapping_idx, f);
2808 5037 : if (serror(f)) {
2809 : /* LCOV_EXCL_START */
2810 : log_fatal("Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
2811 : goto bail;
2812 : /* LCOV_EXCL_STOP */
2813 : }
2814 5037 : begin = 0;
2815 15643 : while (begin < blockmax) {
2816 : int is_deleted;
2817 : block_off_t end;
2818 :
2819 5569 : is_deleted = fs_is_block_deleted_ts(disk, &fs_last, begin);
2820 :
2821 : /* find the end of run of blocks */
2822 5568 : end = begin + 1;
2823 29329135 : while (end < blockmax
2824 29372732 : && is_deleted == fs_is_block_deleted_ts(disk, &fs_last, end)
2825 : ) {
2826 29317999 : ++end;
2827 : }
2828 :
2829 0 : sputb32(end - begin, f);
2830 :
2831 5569 : if (is_deleted) {
2832 : /* write the run of deleted blocks with hash */
2833 329 : sputc('o', f);
2834 :
2835 : /* write all the hash */
2836 112525 : while (begin < end) {
2837 111867 : struct snapraid_block* block = fs_par2block_get_ts(disk, &fs_last, begin);
2838 :
2839 111867 : swrite(block->hash, HASH_SIZE, f);
2840 :
2841 111867 : ++begin;
2842 : }
2843 : } else {
2844 : /* write the run of blocks without hash */
2845 : /* they can be either used or empty blocks */
2846 5240 : sputc('O', f);
2847 :
2848 : /* next begin position */
2849 5240 : begin = end;
2850 : }
2851 :
2852 5569 : if (serror(f)) {
2853 : /* LCOV_EXCL_START */
2854 : log_fatal("Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
2855 : goto bail;
2856 : /* LCOV_EXCL_STOP */
2857 : }
2858 : }
2859 : }
2860 :
2861 : /* write the info for each block */
2862 913 : sputc('i', f);
2863 913 : sputb32(info_oldest, f);
2864 8223 : begin = 0;
2865 1418395 : while (begin < blockmax) {
2866 : snapraid_info info;
2867 : block_off_t end;
2868 : time_t t;
2869 : unsigned flag;
2870 :
2871 1409259 : info = info_get(&state->infoarr, begin);
2872 :
2873 : /* find the end of run of blocks */
2874 1408072 : end = begin + 1;
2875 6328564 : while (end < blockmax
2876 4918189 : && info == info_get(&state->infoarr, end)
2877 : ) {
2878 3512420 : ++end;
2879 : }
2880 :
2881 1407793 : sputb32(end - begin, f);
2882 :
2883 : /* if there is info */
2884 1408365 : if (info) {
2885 : /* other flags */
2886 1408235 : flag = 1; /* info is present */
2887 1408235 : if (info_get_bad(info))
2888 9500 : flag |= 2;
2889 1407263 : if (info_get_rehash(info))
2890 673630 : flag |= 4;
2891 1406735 : if (info_get_justsynced(info))
2892 723766 : flag |= 8;
2893 1398202 : sputb32(flag, f);
2894 :
2895 1408672 : t = info_get_time(info) - info_oldest;
2896 1408134 : sputb32(t, f);
2897 : } else {
2898 : /* write a special 0 flag to mark missing info */
2899 130 : sputb32(0, f);
2900 : }
2901 :
2902 1409231 : if (serror(f)) {
2903 : /* LCOV_EXCL_START */
2904 : log_fatal("Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
2905 : goto bail;
2906 : /* LCOV_EXCL_STOP */
2907 : }
2908 :
2909 : /* next begin position */
2910 1401949 : begin = end;
2911 : }
2912 :
2913 913 : sputc('N', f);
2914 :
2915 : /* flush data written to the disk */
2916 913 : if (sflush(f)) {
2917 : /* LCOV_EXCL_START */
2918 : log_fatal("Error writing the content file '%s' (in flush before crc). %s.\n", serrorfile(f), strerror(errno));
2919 : goto bail;
2920 : /* LCOV_EXCL_STOP */
2921 : }
2922 :
2923 : /* get the file crc */
2924 913 : crc = scrc(f);
2925 :
2926 : /* compare the crc of the data written to file */
2927 : /* with the one of the data written to the stream */
2928 913 : if (crc != scrc_stream(f)) {
2929 : /* LCOV_EXCL_START */
2930 : log_fatal("CRC mismatch writing the content stream.\n");
2931 : log_fatal("DANGER! Your RAM memory is broken! DO NOT PROCEED UNTIL FIXED!\n");
2932 : log_fatal("Try running a memory test like http://www.memtest86.com/\n");
2933 : goto bail;
2934 : /* LCOV_EXCL_STOP */
2935 : }
2936 :
2937 913 : sputble32(crc, f);
2938 913 : if (serror(f)) {
2939 : /* LCOV_EXCL_START */
2940 : log_fatal("Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
2941 : goto bail;
2942 : /* LCOV_EXCL_STOP */
2943 : }
2944 :
2945 : /* Use the sequence fflush() -> fsync() -> fclose() -> rename() to ensure */
2946 : /* than even in a system crash event we have one valid copy of the file. */
2947 913 : if (sflush(f) != 0) {
2948 : /* LCOV_EXCL_START */
2949 : log_fatal("Error writing the content file '%s', in flush(). %s.\n", serrorfile(f), strerror(errno));
2950 : goto bail;
2951 : /* LCOV_EXCL_STOP */
2952 : }
2953 :
2954 : #if HAVE_FSYNC
2955 913 : if (ssync(f) != 0) {
2956 : /* LCOV_EXCL_START */
2957 : log_fatal("Error writing the content file '%s' in sync(). %s.\n", serrorfile(f), strerror(errno));
2958 : goto bail;
2959 : /* LCOV_EXCL_STOP */
2960 : }
2961 : #endif
2962 :
2963 912 : if (sclose(f) != 0) {
2964 : /* LCOV_EXCL_START */
2965 : f = 0;
2966 : log_fatal("Error closing the content files. %s.\n", strerror(errno));
2967 : goto bail;
2968 : /* LCOV_EXCL_STOP */
2969 : }
2970 :
2971 : /* set output variables */
2972 913 : context->crc = crc;
2973 913 : context->count_file = count_file;
2974 913 : context->count_hardlink = count_hardlink;
2975 913 : context->count_symlink = count_symlink;
2976 913 : context->count_dir = count_dir;
2977 :
2978 913 : return 0;
2979 :
2980 : /* LCOV_EXCL_START */
2981 : bail:
2982 : /* cleanup resources */
2983 : if (f != 0)
2984 : sclose(f);
2985 : return context;
2986 : /* LCOV_EXCL_STOP */
2987 : }
2988 :
2989 134 : static void state_write_content(struct snapraid_state* state, uint32_t* out_crc)
2990 : {
2991 : tommy_node* i;
2992 : int fail;
2993 : block_off_t blockmax;
2994 : time_t info_oldest;
2995 : int info_has_rehash;
2996 : int mapping_idx;
2997 : block_off_t idx;
2998 : uint32_t crc;
2999 : unsigned count_file;
3000 : unsigned count_hardlink;
3001 : unsigned count_symlink;
3002 : unsigned count_dir;
3003 : int first;
3004 :
3005 : /* blocks of all array */
3006 134 : blockmax = parity_allocated_size(state);
3007 :
3008 : /* check the filesystem on all disks */
3009 134 : state_fscheck(state, "before write");
3010 :
3011 : /* clear the info for unused blocks */
3012 : /* and get some other info */
3013 134 : info_oldest = 0; /* oldest time in info */
3014 134 : info_has_rehash = 0; /* if there is a rehash info */
3015 729552 : for (idx = 0; idx < blockmax; ++idx) {
3016 : /* if the position is used */
3017 729418 : if (fs_position_is_required(state, idx)) {
3018 729418 : snapraid_info info = info_get(&state->infoarr, idx);
3019 :
3020 : /* only if there is some info to store */
3021 729418 : if (info) {
3022 680773 : time_t info_time = info_get_time(info);
3023 :
3024 680773 : if (!info_oldest || info_time < info_oldest)
3025 169 : info_oldest = info_time;
3026 :
3027 680773 : if (info_get_rehash(info))
3028 108421 : info_has_rehash = 1;
3029 : }
3030 : } else {
3031 : /* clear any previous info */
3032 0 : info_set(&state->infoarr, idx, 0);
3033 :
3034 : /* and clear any deleted blocks */
3035 0 : fs_position_clear_deleted(state, idx);
3036 : }
3037 : }
3038 :
3039 : /* map disks */
3040 134 : mapping_idx = 0;
3041 938 : for (i = state->maplist; i != 0; i = i->next) {
3042 804 : struct snapraid_map* map = i->data;
3043 : struct snapraid_disk* disk;
3044 :
3045 : /* find the disk for this mapping */
3046 804 : disk = find_disk(state, map->name);
3047 804 : if (!disk) {
3048 : /* LCOV_EXCL_START */
3049 : log_fatal("Internal inconsistency for unmapped disk '%s'\n", map->name);
3050 : exit(EXIT_FAILURE);
3051 : /* LCOV_EXCL_STOP */
3052 : }
3053 :
3054 : /* save the mapping only for not empty disks */
3055 804 : if (!disk_is_empty(disk, blockmax)) {
3056 : /* assign the mapping index used to identify disks */
3057 741 : disk->mapping_idx = mapping_idx;
3058 741 : ++mapping_idx;
3059 : } else {
3060 : /* mark the disk as without mapping */
3061 63 : disk->mapping_idx = -1;
3062 : }
3063 : }
3064 :
3065 : /* start all writing threads */
3066 134 : i = tommy_list_head(&state->contentlist);
3067 1181 : while (i) {
3068 913 : struct snapraid_content* content = i->data;
3069 : struct state_write_thread_context* context;
3070 :
3071 : /* allocate the thread context */
3072 913 : context = malloc_nofail(sizeof(struct state_write_thread_context));
3073 913 : content->context = context;
3074 :
3075 : /* initialize */
3076 913 : context->state = state;
3077 913 : context->content = content;
3078 913 : context->blockmax = blockmax;
3079 913 : context->info_oldest = info_oldest;
3080 913 : context->info_has_rehash = info_has_rehash;
3081 :
3082 913 : msg_progress("Saving state to %s...\n", content->content);
3083 :
3084 : #if HAVE_PTHREAD_CREATE
3085 913 : if (pthread_create(&context->thread, 0, state_write_thread, context) != 0) {
3086 : /* LCOV_EXCL_START */
3087 : log_fatal("Failed to create thread.\n");
3088 : exit(EXIT_FAILURE);
3089 : /* LCOV_EXCL_STOP */
3090 : }
3091 : #else
3092 : content->retval = state_write_thread(context);
3093 : #endif
3094 :
3095 913 : i = i->next;
3096 : }
3097 :
3098 : /* join all thread */
3099 134 : fail = 0;
3100 134 : first = 1;
3101 134 : crc = 0;
3102 134 : count_file = 0;
3103 134 : count_hardlink = 0;
3104 134 : count_symlink = 0;
3105 134 : count_dir = 0;
3106 134 : i = tommy_list_head(&state->contentlist);
3107 1181 : while (i) {
3108 913 : struct snapraid_content* content = i->data;
3109 913 : struct state_write_thread_context* context = content->context;
3110 : void* retval;
3111 :
3112 : #if HAVE_PTHREAD_CREATE
3113 913 : if (pthread_join(context->thread, &retval) != 0) {
3114 : /* LCOV_EXCL_START */
3115 : log_fatal("Failed to join thread.\n");
3116 : exit(EXIT_FAILURE);
3117 : /* LCOV_EXCL_STOP */
3118 : }
3119 : #else
3120 : retval = context->retval;
3121 : #endif
3122 913 : if (retval) {
3123 : /* LCOV_EXCL_START */
3124 : fail = 1;
3125 : /* LCOV_EXCL_STOP */
3126 : } else {
3127 913 : if (first) {
3128 134 : first = 0;
3129 134 : crc = context->crc;
3130 134 : count_file = context->count_file;
3131 134 : count_hardlink = context->count_hardlink;
3132 134 : count_symlink = context->count_symlink;
3133 134 : count_dir = context->count_dir;
3134 : } else {
3135 779 : if (crc != context->crc) {
3136 : /* LCOV_EXCL_START */
3137 : log_fatal("Different CRCs writing content streams.\n");
3138 : log_fatal("DANGER! Your RAM memory is broken! DO NOT PROCEED UNTIL FIXED!\n");
3139 : log_fatal("Try running a memory test like http://www.memtest86.com/\n");
3140 : exit(EXIT_FAILURE);
3141 : /* LCOV_EXCL_STOP */
3142 : }
3143 : }
3144 : }
3145 :
3146 913 : free(context);
3147 :
3148 913 : i = i->next;
3149 : }
3150 :
3151 : /* abort on failure */
3152 134 : if (fail) {
3153 : /* LCOV_EXCL_START */
3154 : exit(EXIT_FAILURE);
3155 : /* LCOV_EXCL_STOP */
3156 : }
3157 :
3158 134 : msg_verbose("%8u files\n", count_file);
3159 134 : msg_verbose("%8u hardlinks\n", count_hardlink);
3160 134 : msg_verbose("%8u symlinks\n", count_symlink);
3161 134 : msg_verbose("%8u empty dirs\n", count_dir);
3162 :
3163 134 : *out_crc = crc;
3164 134 : }
3165 :
3166 242 : void state_read(struct snapraid_state* state)
3167 : {
3168 : STREAM* f;
3169 : char path[PATH_MAX];
3170 : struct stat st;
3171 : tommy_node* node;
3172 : int ret;
3173 : int c;
3174 :
3175 : /* iterate over all the available content files and load the first one present */
3176 242 : f = 0;
3177 242 : node = tommy_list_head(&state->contentlist);
3178 534 : while (node) {
3179 285 : struct snapraid_content* content = node->data;
3180 285 : pathcpy(path, sizeof(path), content->content);
3181 :
3182 285 : if (!state->no_conf) {
3183 284 : log_tag("content:%s\n", path);
3184 284 : log_flush();
3185 : }
3186 285 : msg_progress("Loading state from %s...\n", path);
3187 :
3188 285 : f = sopen_read(path);
3189 285 : if (f != 0) {
3190 : /* if openend stop the search */
3191 235 : break;
3192 : } else {
3193 : /* if it's real error of an existing file, abort */
3194 50 : if (errno != ENOENT) {
3195 : /* LCOV_EXCL_START */
3196 : log_fatal("Error opening the content file '%s'. %s.\n", path, strerror(errno));
3197 : exit(EXIT_FAILURE);
3198 : /* LCOV_EXCL_STOP */
3199 : }
3200 :
3201 : /* otherwise continue */
3202 50 : if (node->next) {
3203 43 : log_fatal("WARNING! Content file '%s' not found, trying with another copy...\n", path);
3204 :
3205 : /* ensure to rewrite all the content files */
3206 43 : state->need_write = 1;
3207 : }
3208 : }
3209 :
3210 : /* next content file */
3211 50 : node = node->next;
3212 : }
3213 :
3214 : /* if not found, assume empty */
3215 242 : if (!f) {
3216 7 : log_fatal("No content file found. Assuming empty.\n");
3217 :
3218 : /* create the initial mapping */
3219 7 : state_map(state);
3220 248 : return;
3221 : }
3222 :
3223 : /* get the stat of the content file */
3224 235 : ret = fstat(shandle(f), &st);
3225 235 : if (ret != 0) {
3226 : /* LCOV_EXCL_START */
3227 : log_fatal("Error stating the content file '%s'. %s.\n", path, strerror(errno));
3228 : exit(EXIT_FAILURE);
3229 : /* LCOV_EXCL_STOP */
3230 : }
3231 :
3232 : /* go further to check other content files */
3233 1819 : while (node) {
3234 : char other_path[PATH_MAX];
3235 : struct stat other_st;
3236 1349 : struct snapraid_content* content = node->data;
3237 1349 : pathcpy(other_path, sizeof(other_path), content->content);
3238 :
3239 1349 : ret = stat(other_path, &other_st);
3240 1349 : if (ret != 0) {
3241 : /* allow missing content files, but not any other kind of error */
3242 1 : if (errno != ENOENT) {
3243 : /* LCOV_EXCL_START */
3244 : log_fatal("Error stating the content file '%s'. %s.\n", other_path, strerror(errno));
3245 : exit(EXIT_FAILURE);
3246 : /* LCOV_EXCL_STOP */
3247 : }
3248 :
3249 : /* ensure to rewrite all the content files */
3250 1 : state->need_write = 1;
3251 : } else {
3252 : /* if the size is different */
3253 1348 : if (other_st.st_size != st.st_size) {
3254 39 : log_fatal("WARNING! Content files '%s' and '%s' have a different size!\n", path, other_path);
3255 39 : log_fatal("Likely one of the two is broken!\n");
3256 :
3257 : /* ensure to rewrite all the content files */
3258 39 : state->need_write = 1;
3259 : }
3260 : }
3261 :
3262 : /* next content file */
3263 1349 : node = node->next;
3264 : }
3265 :
3266 : /* start with a undefined default. */
3267 : /* it's for compatibility with version 1.0 where MD5 was implicit. */
3268 235 : state->hash = HASH_UNDEFINED;
3269 :
3270 : /* start with a zero seed, it was the default in old versions */
3271 235 : memset(state->hashseed, 0, HASH_SIZE);
3272 :
3273 : /* previous hash, start with an undefined value */
3274 235 : state->prevhash = HASH_UNDEFINED;
3275 :
3276 : /* intentionally not set the prevhashseed, if used valgrind will warn about it */
3277 :
3278 : /* get the first char to detect the file type */
3279 235 : c = sgetc(f);
3280 235 : sungetc(c, f);
3281 :
3282 : /* guess the file type from the first char */
3283 235 : if (c == 'S') {
3284 235 : state_read_content(state, path, f);
3285 : } else {
3286 : /* LCOV_EXCL_START */
3287 : log_fatal("The text file format is not supported anymore.\n");
3288 : exit(EXIT_FAILURE);
3289 : /* LCOV_EXCL_STOP */
3290 : }
3291 :
3292 234 : sclose(f);
3293 :
3294 234 : if (state->hash == HASH_UNDEFINED) {
3295 : /* LCOV_EXCL_START */
3296 : log_fatal("The checksum to use is not specified.\n");
3297 : log_fatal("This happens because you are likely upgrading from SnapRAID 1.0.\n");
3298 : log_fatal("To use a new SnapRAID you must restart from scratch,\n");
3299 : log_fatal("deleting all the content and parity files.\n");
3300 : exit(EXIT_FAILURE);
3301 : /* LCOV_EXCL_STOP */
3302 : }
3303 :
3304 : /* update the mapping */
3305 234 : state_map(state);
3306 :
3307 234 : state_content_check(state, path);
3308 :
3309 : /* mark that we read the content file, and it passed all the checks */
3310 234 : state->checked_read = 1;
3311 : }
3312 :
3313 : struct state_verify_thread_context {
3314 : struct snapraid_state* state;
3315 : struct snapraid_content* content;
3316 : #if HAVE_PTHREAD_CREATE
3317 : pthread_t thread;
3318 : #else
3319 : void* retval;
3320 : #endif
3321 : /* input */
3322 : uint32_t crc;
3323 : };
3324 :
3325 908 : static void* state_verify_thread(void* arg)
3326 : {
3327 908 : struct state_verify_thread_context* context = arg;
3328 908 : struct snapraid_content* content = context->content;
3329 : STREAM* f;
3330 : char tmp[PATH_MAX];
3331 : unsigned char buf[4];
3332 : uint32_t crc_stored;
3333 : uint32_t crc_computed;
3334 :
3335 908 : pathprint(tmp, sizeof(tmp), "%s.tmp", content->content);
3336 :
3337 913 : f = sopen_read(tmp);
3338 913 : if (f == 0) {
3339 : /* LCOV_EXCL_START */
3340 : log_fatal("Error reopening the content file '%s'. %s.\n", tmp, strerror(errno));
3341 : goto bail;
3342 : /* LCOV_EXCL_STOP */
3343 : }
3344 :
3345 913 : if (sdeplete(f, buf) != 0) {
3346 : /* LCOV_EXCL_START */
3347 : log_fatal("Error flushing the content file '%s'. %s.\n", tmp, strerror(errno));
3348 : goto bail;
3349 : /* LCOV_EXCL_STOP */
3350 : }
3351 :
3352 : /* get the stored crc from the last four bytes */
3353 913 : crc_stored = buf[0] | (uint32_t)buf[1] << 8 | (uint32_t)buf[2] << 16 | (uint32_t)buf[3] << 24;
3354 :
3355 913 : if (crc_stored != context->crc) {
3356 : /* LCOV_EXCL_START */
3357 : log_fatal("DANGER! Wrong stored CRC in '%s'\n", tmp);
3358 : goto bail;
3359 : /* LCOV_EXCL_STOP */
3360 : }
3361 :
3362 : /* get the computed crc */
3363 913 : crc_computed = scrc(f);
3364 :
3365 : /* adjust the stored crc to include itself */
3366 913 : crc_stored = crc32c(crc_stored, buf, 4);
3367 :
3368 913 : if (crc_computed != crc_stored) {
3369 : /* LCOV_EXCL_START */
3370 : log_fatal("DANGER! Wrong file CRC in '%s'\n", tmp);
3371 : goto bail;
3372 : /* LCOV_EXCL_STOP */
3373 : }
3374 :
3375 913 : if (sclose(f) != 0) {
3376 : /* LCOV_EXCL_START */
3377 : f = 0;
3378 : log_fatal("Error closing the content file '%s'. %s.\n", tmp, strerror(errno));
3379 : goto bail;
3380 : /* LCOV_EXCL_STOP */
3381 : }
3382 :
3383 913 : return 0;
3384 :
3385 : bail:
3386 : /* LCOV_EXCL_START */
3387 : /* cleanup resources */
3388 : if (f != 0)
3389 : sclose(f);
3390 : return context;
3391 : /* LCOV_EXCL_STOP */
3392 : }
3393 :
3394 134 : static void state_verify_content(struct snapraid_state* state, uint32_t crc)
3395 : {
3396 : tommy_node* i;
3397 : int fail;
3398 :
3399 : /* start all reading threads */
3400 134 : i = tommy_list_head(&state->contentlist);
3401 1181 : while (i) {
3402 913 : struct snapraid_content* content = i->data;
3403 : struct state_verify_thread_context* context;
3404 :
3405 : /* allocate the thread context */
3406 913 : context = malloc_nofail(sizeof(struct state_verify_thread_context));
3407 913 : content->context = context;
3408 :
3409 : /* initialize */
3410 913 : context->state = state;
3411 913 : context->content = content;
3412 913 : context->crc = crc;
3413 :
3414 913 : msg_progress("Verifying %s...\n", content->content);
3415 :
3416 : #if HAVE_PTHREAD_CREATE
3417 913 : if (pthread_create(&context->thread, 0, state_verify_thread, context) != 0) {
3418 : /* LCOV_EXCL_START */
3419 : log_fatal("Failed to create thread.\n");
3420 : exit(EXIT_FAILURE);
3421 : /* LCOV_EXCL_STOP */
3422 : }
3423 : #else
3424 : content->retval = state_verify_thread(context);
3425 : #endif
3426 :
3427 913 : i = i->next;
3428 : }
3429 :
3430 : /* join all thread */
3431 134 : fail = 0;
3432 134 : i = tommy_list_head(&state->contentlist);
3433 1181 : while (i) {
3434 913 : struct snapraid_content* content = i->data;
3435 913 : struct state_verify_thread_context* context = content->context;
3436 : void* retval;
3437 :
3438 : #if HAVE_PTHREAD_CREATE
3439 913 : if (pthread_join(context->thread, &retval) != 0) {
3440 : /* LCOV_EXCL_START */
3441 : log_fatal("Failed to join thread.\n");
3442 : exit(EXIT_FAILURE);
3443 : /* LCOV_EXCL_STOP */
3444 : }
3445 : #else
3446 : retval = context->retval;
3447 : #endif
3448 913 : if (retval) {
3449 : /* LCOV_EXCL_START */
3450 : fail = 1;
3451 : /* LCOV_EXCL_STOP */
3452 : }
3453 :
3454 913 : free(context);
3455 :
3456 913 : i = i->next;
3457 : }
3458 :
3459 : /* abort on failure */
3460 134 : if (fail) {
3461 : /* LCOV_EXCL_START */
3462 : exit(EXIT_FAILURE);
3463 : /* LCOV_EXCL_STOP */
3464 : }
3465 134 : }
3466 :
3467 134 : static void state_rename_content(struct snapraid_state* state)
3468 : {
3469 : tommy_node* i;
3470 :
3471 134 : i = tommy_list_head(&state->contentlist);
3472 1181 : while (i) {
3473 913 : struct snapraid_content* content = i->data;
3474 : char tmp[PATH_MAX];
3475 :
3476 : /* now renames the just written copy with the correct name */
3477 913 : pathprint(tmp, sizeof(tmp), "%s.tmp", content->content);
3478 913 : if (rename(tmp, content->content) != 0) {
3479 : /* LCOV_EXCL_START */
3480 : log_fatal("Error renaming the content file '%s' to '%s' in rename(). %s.\n", tmp, content->content, strerror(errno));
3481 : exit(EXIT_FAILURE);
3482 : /* LCOV_EXCL_STOP */
3483 : }
3484 :
3485 913 : i = i->next;
3486 : }
3487 134 : }
3488 :
3489 134 : void state_write(struct snapraid_state* state)
3490 : {
3491 : uint32_t crc;
3492 :
3493 : /* write all the content files */
3494 134 : state_write_content(state, &crc);
3495 :
3496 : /* verify the just written files */
3497 134 : state_verify_content(state, crc);
3498 :
3499 : /* rename the new files, over the old ones */
3500 134 : state_rename_content(state);
3501 :
3502 134 : state->need_write = 0; /* no write needed anymore */
3503 134 : state->checked_read = 0; /* what we wrote is not checked in read */
3504 134 : }
3505 :
3506 122 : void state_skip(struct snapraid_state* state)
3507 : {
3508 : tommy_node* i;
3509 :
3510 : /* for each disk */
3511 850 : for (i = state->disklist; i != 0; i = i->next) {
3512 : tommy_node* j;
3513 728 : struct snapraid_disk* disk = i->data;
3514 :
3515 728 : if (!disk->skip_access)
3516 727 : continue;
3517 :
3518 : /* for each file */
3519 1883 : for (j = tommy_list_head(&disk->filelist); j != 0; j = j->next) {
3520 1882 : struct snapraid_file* file = j->data;
3521 1882 : file_flag_set(file, FILE_IS_EXCLUDED);
3522 : }
3523 :
3524 : /* for each link */
3525 57 : for (j = tommy_list_head(&disk->linklist); j != 0; j = j->next) {
3526 56 : struct snapraid_link* slink = j->data;
3527 56 : link_flag_set(slink, FILE_IS_EXCLUDED);
3528 : }
3529 :
3530 : /* for each dir */
3531 1 : for (j = tommy_list_head(&disk->dirlist); j != 0; j = j->next) {
3532 0 : struct snapraid_dir* dir = j->data;
3533 0 : dir_flag_set(dir, FILE_IS_EXCLUDED);
3534 : }
3535 : }
3536 122 : }
3537 :
3538 122 : void state_filter(struct snapraid_state* state, tommy_list* filterlist_file, tommy_list* filterlist_disk, int filter_missing, int filter_error)
3539 : {
3540 : tommy_node* i;
3541 : unsigned l;
3542 :
3543 : /* if no filter, include all */
3544 122 : if (!filter_missing && !filter_error && tommy_list_empty(filterlist_file) && tommy_list_empty(filterlist_disk))
3545 235 : return;
3546 :
3547 9 : msg_progress("Filtering...\n");
3548 :
3549 17 : for (i = tommy_list_head(filterlist_disk); i != 0; i = i->next) {
3550 8 : struct snapraid_filter* filter = i->data;
3551 8 : msg_verbose("\t%s%s\n", filter->pattern, filter->is_disk ? "//" : "");
3552 : }
3553 12 : for (i = tommy_list_head(filterlist_file); i != 0; i = i->next) {
3554 3 : struct snapraid_filter* filter = i->data;
3555 3 : msg_verbose("\t%s%s\n", filter->pattern, filter->is_dir ? "/" : "");
3556 : }
3557 9 : if (filter_missing)
3558 2 : msg_verbose("\t<missing>\n");
3559 9 : if (filter_error)
3560 2 : msg_verbose("\t<error>\n");
3561 :
3562 : /* for each disk */
3563 63 : for (i = state->disklist; i != 0; i = i->next) {
3564 : tommy_node* j;
3565 54 : struct snapraid_disk* disk = i->data;
3566 :
3567 : /* if we filter for presence, we have to access the disk, so better to print something */
3568 54 : if (filter_missing)
3569 12 : msg_progress("Scanning disk %s...\n", disk->name);
3570 :
3571 : /* for each file */
3572 133845 : for (j = tommy_list_head(&disk->filelist); j != 0; j = j->next) {
3573 133791 : struct snapraid_file* file = j->data;
3574 :
3575 133791 : if (filter_path(filterlist_disk, 0, disk->name, file->sub) != 0
3576 80918 : || filter_path(filterlist_file, 0, disk->name, file->sub) != 0
3577 78085 : || filter_existence(filter_missing, disk->dir, file->sub) != 0
3578 38803 : || filter_correctness(filter_error, &state->infoarr, disk, file) != 0
3579 : ) {
3580 109060 : file_flag_set(file, FILE_IS_EXCLUDED);
3581 : }
3582 : }
3583 :
3584 : /* for each link */
3585 4414 : for (j = tommy_list_head(&disk->linklist); j != 0; j = j->next) {
3586 4360 : struct snapraid_link* slink = j->data;
3587 :
3588 4360 : if (filter_path(filterlist_disk, 0, disk->name, slink->sub) != 0
3589 2641 : || filter_path(filterlist_file, 0, disk->name, slink->sub) != 0
3590 2536 : || filter_existence(filter_missing, disk->dir, slink->sub) != 0
3591 : ) {
3592 3221 : link_flag_set(slink, FILE_IS_EXCLUDED);
3593 : }
3594 : }
3595 :
3596 : /* for each dir */
3597 82 : for (j = tommy_list_head(&disk->dirlist); j != 0; j = j->next) {
3598 28 : struct snapraid_dir* dir = j->data;
3599 :
3600 28 : if (filter_dir(filterlist_disk, 0, disk->name, dir->sub) != 0
3601 28 : || filter_dir(filterlist_file, 0, disk->name, dir->sub) != 0
3602 28 : || filter_existence(filter_missing, disk->dir, dir->sub) != 0
3603 : ) {
3604 8 : dir_flag_set(dir, FILE_IS_EXCLUDED);
3605 : }
3606 : }
3607 : }
3608 :
3609 : /* if we are filtering by disk */
3610 9 : if (!tommy_list_empty(filterlist_disk)) {
3611 : /* for each parity disk */
3612 20 : for (l = 0; l < state->level; ++l) {
3613 : /* check if the parity is excluded by name */
3614 16 : if (filter_path(filterlist_disk, 0, lev_config_name(l), 0) != 0) {
3615 : /* excluded the parity from further operation */
3616 11 : state->parity[l].is_excluded = 1;
3617 : }
3618 : }
3619 : } else {
3620 : /* if we are filtering by file, exclude all parity */
3621 5 : if (filter_missing || !tommy_list_empty(filterlist_file)) {
3622 : /* for each parity disk */
3623 16 : for (l = 0; l < state->level; ++l) {
3624 13 : state->parity[l].is_excluded = 1;
3625 : }
3626 : }
3627 : }
3628 : }
3629 :
3630 206 : int state_progress_begin(struct snapraid_state* state, block_off_t blockstart, block_off_t blockmax, block_off_t countmax)
3631 : {
3632 : time_t now;
3633 :
3634 206 : if (state->opt.gui) {
3635 5 : log_tag("run:begin:%u:%u:%u\n", blockstart, blockmax, countmax);
3636 5 : log_flush();
3637 : }
3638 :
3639 206 : now = time(0);
3640 :
3641 206 : state->progress_whole_start = now;
3642 :
3643 206 : state->progress_tick = 0;
3644 206 : state->progress_ptr = 0;
3645 206 : state->progress_wasted = 0;
3646 :
3647 : /* stop if requested */
3648 206 : if (global_interrupt) {
3649 : /* LCOV_EXCL_START */
3650 : if (!state->opt.gui) {
3651 : msg_status("Not starting for interruption\n");
3652 : }
3653 : log_tag("sigint:0: SIGINT received\n");
3654 : log_flush();
3655 : return 0;
3656 : /* LCOV_EXCL_STOP */
3657 : }
3658 :
3659 206 : return 1;
3660 : }
3661 :
3662 206 : void state_progress_end(struct snapraid_state* state, block_off_t countpos, block_off_t countmax, data_off_t countsize)
3663 : {
3664 206 : if (state->opt.gui) {
3665 5 : log_tag("run:end\n");
3666 5 : log_flush();
3667 201 : } else if (countmax == 0) {
3668 15 : msg_status("Nothing to do\n");
3669 : } else {
3670 : time_t now;
3671 : time_t elapsed;
3672 :
3673 186 : unsigned countsize_MB = (countsize + MEGA - 1) / MEGA;
3674 :
3675 186 : now = time(0);
3676 :
3677 186 : elapsed = now - state->progress_whole_start - state->progress_wasted;
3678 :
3679 186 : msg_bar("%u%% completed, %u MB processed", countpos * 100 / countmax, countsize_MB);
3680 186 : if (elapsed >= 60)
3681 0 : msg_bar(" in %u:%02u", (unsigned)(elapsed / 3600), (unsigned)((elapsed % 3600) / 60));
3682 186 : msg_bar("\n");
3683 186 : msg_flush();
3684 : }
3685 206 : }
3686 :
3687 1 : void state_progress_stop(struct snapraid_state* state)
3688 : {
3689 : time_t now;
3690 :
3691 1 : now = time(0);
3692 :
3693 1 : if (!state->opt.gui) {
3694 1 : msg_bar("\n");
3695 1 : msg_flush();
3696 : }
3697 :
3698 1 : state->progress_interruption = now;
3699 1 : }
3700 :
3701 1 : void state_progress_restart(struct snapraid_state* state)
3702 : {
3703 : time_t now;
3704 :
3705 1 : now = time(0);
3706 :
3707 : /* reset the progress counter */
3708 1 : state->progress_tick = 0;
3709 1 : state->progress_ptr = 0;
3710 :
3711 1 : if (now >= state->progress_interruption) /* avoid degenerated cases when the clock is manually adjusted */
3712 1 : state->progress_wasted += now - state->progress_interruption;
3713 1 : }
3714 :
3715 : #define PROGRESS_CLEAR " "
3716 :
3717 1038828 : int state_progress(struct snapraid_state* state, block_off_t blockpos, block_off_t countpos, block_off_t countmax, data_off_t countsize)
3718 : {
3719 : time_t now;
3720 : int pred;
3721 :
3722 1038828 : now = time(0);
3723 :
3724 : /* previous position */
3725 1038828 : pred = state->progress_ptr + PROGRESS_MAX - 1;
3726 1038828 : if (pred >= PROGRESS_MAX)
3727 1038640 : pred -= PROGRESS_MAX;
3728 :
3729 : /* if the previous measure is different */
3730 1038828 : if (state->progress_tick == 0
3731 1038640 : || state->progress_time[pred] != now
3732 : ) {
3733 : uint64_t tick_total;
3734 : uint64_t tick_cpu;
3735 : time_t elapsed;
3736 260 : unsigned out_perc = 0;
3737 260 : unsigned out_speed = 0;
3738 260 : unsigned out_cpu = 0;
3739 260 : unsigned out_eta = 0;
3740 :
3741 260 : tick_total = state->tick_cpu + state->tick_io;
3742 260 : tick_cpu = state->tick_cpu;
3743 260 : elapsed = now - state->progress_whole_start - state->progress_wasted;
3744 :
3745 : /* completion percentage */
3746 260 : if (countmax)
3747 260 : out_perc = countpos * 100 / countmax;
3748 :
3749 : /* if we have at least 5 measures */
3750 260 : if (state->progress_tick >= 5
3751 : /* or if we are running in test mode, with at least one measure */
3752 260 : || (state->opt.force_progress && state->progress_tick >= 1)
3753 : ) {
3754 : int oldest;
3755 : int past;
3756 : time_t delta_time;
3757 : block_off_t delta_pos;
3758 : data_off_t delta_size;
3759 : uint64_t delta_tick_total;
3760 : uint64_t delta_tick_cpu;
3761 :
3762 : /* number of past measures */
3763 72 : past = state->progress_tick;
3764 :
3765 : /* drop the oldest ones, to promptly */
3766 : /* skip the startup phase */
3767 72 : past -= past / 5;
3768 :
3769 : /* check how much we can go in the past */
3770 72 : if (past >= PROGRESS_MAX) {
3771 : /* the vector is filled, so we are already in position */
3772 : /* to get the possible oldest one */
3773 0 : oldest = state->progress_ptr;
3774 : } else {
3775 : /* go backward the number of positions selected */
3776 72 : oldest = state->progress_ptr + PROGRESS_MAX - past;
3777 72 : if (oldest >= PROGRESS_MAX)
3778 72 : oldest -= PROGRESS_MAX;
3779 : }
3780 :
3781 72 : delta_time = now - state->progress_time[oldest];
3782 72 : delta_pos = countpos - state->progress_pos[oldest];
3783 72 : delta_size = countsize - state->progress_size[oldest];
3784 72 : delta_tick_total = tick_total - state->progress_tick_total[oldest];
3785 72 : delta_tick_cpu = tick_cpu - state->progress_tick_cpu[oldest];
3786 :
3787 : /* estimate the speed in MiB/s */
3788 72 : if (delta_time != 0)
3789 72 : out_speed = (unsigned)(delta_size / MEGA / delta_time);
3790 :
3791 : /* estimate the cpu usage percentage */
3792 72 : if (delta_tick_total != 0)
3793 12 : out_cpu = (unsigned)(delta_tick_cpu * 100U / delta_tick_total);
3794 :
3795 : /* estimate the remaining time in minutes */
3796 72 : if (delta_pos != 0)
3797 72 : out_eta = (countmax - countpos) * delta_time / (60 * delta_pos);
3798 : }
3799 :
3800 260 : if (state->opt.gui) {
3801 7 : log_tag("run:pos:%u:%u:%" PRIu64 ":%u:%u:%u:%u:%" PRIu64 "\n", blockpos, countpos, countsize, out_perc, out_eta, out_speed, out_cpu, (uint64_t)elapsed);
3802 7 : log_flush();
3803 : } else {
3804 253 : msg_bar("%u%%, %u MiB", out_perc, (unsigned)(countsize / MEGA));
3805 253 : if (out_speed)
3806 61 : msg_bar(", %u MiB/s", out_speed);
3807 253 : if (out_cpu)
3808 12 : msg_bar(", CPU %u%%", out_cpu);
3809 253 : if (out_eta)
3810 2 : msg_bar(", %u:%02u ETA", out_eta / 60, out_eta % 60);
3811 253 : msg_bar("%s\r", PROGRESS_CLEAR);
3812 253 : msg_flush();
3813 : }
3814 :
3815 : /* store the new measure */
3816 260 : state->progress_time[state->progress_ptr] = now;
3817 260 : state->progress_pos[state->progress_ptr] = countpos;
3818 260 : state->progress_size[state->progress_ptr] = countsize;
3819 260 : state->progress_tick_cpu[state->progress_ptr] = tick_cpu;
3820 260 : state->progress_tick_total[state->progress_ptr] = tick_total;
3821 :
3822 : /* next position */
3823 260 : ++state->progress_ptr;
3824 260 : if (state->progress_ptr >= PROGRESS_MAX)
3825 0 : state->progress_ptr -= PROGRESS_MAX;
3826 :
3827 : /* one more measure */
3828 260 : ++state->progress_tick;
3829 : }
3830 :
3831 : /* stop if requested */
3832 1038828 : if (global_interrupt) {
3833 : /* LCOV_EXCL_START */
3834 : if (!state->opt.gui) {
3835 : log_fatal("\n");
3836 : log_fatal("Stopping for interruption at block %u\n", blockpos);
3837 : }
3838 : log_tag("sigint:%u: SIGINT received\n", blockpos);
3839 : log_flush();
3840 : return 1;
3841 : /* LCOV_EXCL_STOP */
3842 : }
3843 :
3844 1038828 : return 0;
3845 : }
3846 :
3847 81 : void state_usage_print(struct snapraid_state* state)
3848 : {
3849 : uint64_t tick_total;
3850 : uint64_t tick_max;
3851 : tommy_node* i;
3852 : unsigned l;
3853 :
3854 81 : tick_max = 0;
3855 81 : tick_total = 0;
3856 565 : for (i = state->disklist; i != 0; i = i->next) {
3857 484 : struct snapraid_disk* disk = i->data;
3858 484 : tick_total += disk->tick;
3859 484 : if (disk->tick > tick_max)
3860 100 : tick_max = disk->tick;
3861 : }
3862 552 : for (l = 0; l < state->level; ++l) {
3863 471 : tick_total += state->parity[l].tick;
3864 471 : if (state->parity[l].tick > tick_max)
3865 17 : tick_max = state->parity[l].tick;
3866 : }
3867 :
3868 81 : if (!tick_total)
3869 96 : return;
3870 :
3871 66 : if (msg_level >= MSG_PROGRESS) {
3872 : /* here we don't use msg_progress() because it doesn't allow partial output */
3873 1 : printf("Time for disk:");
3874 7 : for (i = state->disklist; i != 0; i = i->next) {
3875 6 : struct snapraid_disk* disk = i->data;
3876 6 : if (disk->tick == tick_max)
3877 1 : printf(" %s:", disk->name);
3878 : else
3879 5 : printf(" ");
3880 6 : printf("%" PRIu64 "%%", disk->tick * 100U / tick_total);
3881 : }
3882 1 : printf("\n");
3883 :
3884 1 : printf("Time for parity:");
3885 7 : for (l = 0; l < state->level; ++l) {
3886 6 : if (state->parity[l].tick == tick_max)
3887 0 : printf(" %s:", lev_config_name(l));
3888 : else
3889 6 : printf(" ");
3890 6 : printf("%" PRIu64 "%%", state->parity[l].tick * 100U / tick_total);
3891 : }
3892 1 : printf("\n");
3893 : }
3894 : }
3895 :
3896 447 : void state_fscheck(struct snapraid_state* state, const char* ope)
3897 : {
3898 : tommy_node* i;
3899 :
3900 : /* check the filesystem on all disks */
3901 3123 : for (i = state->disklist; i != 0; i = i->next) {
3902 2676 : struct snapraid_disk* disk = i->data;
3903 :
3904 2676 : if (fs_check(disk) != 0) {
3905 : /* LCOV_EXCL_START */
3906 : log_fatal("Internal inconsistency in filesystem for disk '%s' %s\n", disk->name, ope);
3907 : exit(EXIT_FAILURE);
3908 : /* LCOV_EXCL_STOP */
3909 : }
3910 : }
3911 447 : }
3912 :
3913 1 : void generate_configuration(const char* path)
3914 : {
3915 : struct snapraid_state state;
3916 : struct snapraid_content* content;
3917 : unsigned i;
3918 : tommy_node* j;
3919 :
3920 1 : state_init(&state);
3921 :
3922 : /* mark that we are without a configuration file */
3923 1 : state.no_conf = 1;
3924 :
3925 : /* create the dummy content entry */
3926 1 : content = content_alloc(path, -1);
3927 :
3928 : /* adds the content entry */
3929 1 : tommy_list_insert_tail(&state.contentlist, &content->node, content);
3930 :
3931 : /* read the content file */
3932 1 : state_read(&state);
3933 :
3934 : /* output a dummy configuration file */
3935 1 : printf("# Configuration file generated from %s\n", path);
3936 1 : printf("\n");
3937 1 : printf("# Use this blocksize\n");
3938 1 : printf("blocksize %u\n", state.block_size / 1024);
3939 1 : printf("\n");
3940 7 : for (i = 0; i < state.level; ++i) {
3941 6 : printf("# Set the correct path for the %s file\n", lev_name(i));
3942 6 : if (state.parity[i].uuid[0])
3943 6 : printf("# The file was in the disk with id '%s'\n", state.parity[i].uuid);
3944 6 : printf("%s ENTER_HERE_THE_PARITY_FILE\n", lev_config_name(i));
3945 6 : printf("\n");
3946 : }
3947 1 : printf("# Add any other content file\n");
3948 1 : printf("content %s\n", path);
3949 1 : printf("\n");
3950 7 : for (j = state.maplist; j; j = j->next) {
3951 6 : struct snapraid_map* map = j->data;
3952 : struct snapraid_disk* disk;
3953 6 : printf("# Set the correct dir for disk '%s'\n", map->name);
3954 6 : if (map->uuid[0])
3955 6 : printf("# Disk '%s' is the one with id '%s'\n", map->name, map->uuid);
3956 6 : disk = find_disk(&state, map->name);
3957 6 : if (disk && disk->filelist) {
3958 6 : struct snapraid_file* file = disk->filelist->data;
3959 6 : if (file) {
3960 6 : printf("# and containing: %s\n", file->sub);
3961 : }
3962 : }
3963 6 : printf("disk %s ENTER_HERE_THE_DIR\n", map->name);
3964 6 : printf("\n");
3965 : }
3966 :
3967 1 : state_done(&state);
3968 1 : }
3969 :
|