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 "handle.h"
28 : #include "io.h"
29 : #include "thermal.h"
30 : #include "raid/raid.h"
31 : #include "raid/cpu.h"
32 :
33 : /**
34 : * Configure the multithread support.
35 : *
36 : * Multi thread for write could be either faster or slower, depending
37 : * on the specific conditions. With multithreads it's likely faster
38 : * writing to disk, but you'll need to access multiple times the same data,
39 : * being potentially slower.
40 : *
41 : * Multi thread for verify is instead always generally faster,
42 : * so we enable it if possible.
43 : */
44 : #if HAVE_THREAD
45 : /* #define HAVE_MT_WRITE 1 */
46 : #define HAVE_MT_VERIFY 1
47 : #endif
48 :
49 7 : const char* lev_name(unsigned l)
50 : {
51 7 : switch (l) {
52 2 : case 0 : return "Parity";
53 1 : case 1 : return "2-Parity";
54 1 : case 2 : return "3-Parity";
55 1 : case 3 : return "4-Parity";
56 1 : case 4 : return "5-Parity";
57 1 : case 5 : return "6-Parity";
58 : }
59 :
60 0 : return "invalid";
61 : }
62 :
63 288131 : const char* lev_config_name(unsigned l)
64 : {
65 288131 : switch (l) {
66 94703 : case 0 : return "parity";
67 76778 : case 1 : return "2-parity";
68 39717 : case 2 : return "3-parity";
69 25672 : case 3 : return "4-parity";
70 25647 : case 4 : return "5-parity";
71 25614 : case 5 : return "6-parity";
72 : }
73 :
74 0 : return "invalid";
75 : }
76 :
77 7327 : static int lev_config_scan(const char* s, unsigned* level, unsigned* mode)
78 : {
79 7327 : if (strcmp(s, "parity") == 0 || strcmp(s, "1-parity") == 0) {
80 679 : *level = 0;
81 679 : return 0;
82 : }
83 :
84 6648 : if (strcmp(s, "q-parity") == 0 || strcmp(s, "2-parity") == 0) {
85 245 : *level = 1;
86 245 : return 0;
87 : }
88 :
89 6403 : if (strcmp(s, "r-parity") == 0 || strcmp(s, "3-parity") == 0) {
90 213 : *level = 2;
91 213 : return 0;
92 : }
93 :
94 6190 : if (strcmp(s, "4-parity") == 0) {
95 210 : *level = 3;
96 210 : return 0;
97 : }
98 :
99 5980 : if (strcmp(s, "5-parity") == 0) {
100 207 : *level = 4;
101 207 : return 0;
102 : }
103 :
104 5773 : if (strcmp(s, "6-parity") == 0) {
105 203 : *level = 5;
106 203 : return 0;
107 : }
108 :
109 5570 : if (strcmp(s, "z-parity") == 0) {
110 3 : *level = 2;
111 3 : if (mode)
112 3 : *mode = RAID_MODE_VANDERMONDE;
113 3 : return 0;
114 : }
115 :
116 5567 : return -1;
117 : }
118 :
119 289 : const char* lev_raid_name(unsigned mode, unsigned n)
120 : {
121 289 : switch (n) {
122 45 : case 1 : return "par1";
123 29 : case 2 : return "par2";
124 6 : case 3 : if (mode == RAID_MODE_CAUCHY)
125 3 : return "par3";
126 : else
127 3 : return "parz";
128 3 : case 4 : return "par4";
129 4 : case 5 : return "par5";
130 202 : case 6 : return "par6";
131 : }
132 :
133 0 : return 0;
134 : }
135 :
136 291 : void state_init(struct snapraid_state* state)
137 : {
138 : unsigned l, s, i;
139 :
140 291 : memset(&state->opt, 0, sizeof(state->opt));
141 291 : state->filter_hidden = 0;
142 291 : state->autosave = 0;
143 291 : state->need_write = 0;
144 291 : state->written = 0;
145 291 : state->checked_read = 0;
146 291 : state->block_size = 256 * KIBI; /* default 256 KiB */
147 291 : state->raid_mode = RAID_MODE_CAUCHY;
148 291 : state->file_mode = ADVISE_DEFAULT;
149 2037 : for (l = 0; l < LEV_MAX; ++l) {
150 1746 : state->parity[l].split_mac = 0;
151 15714 : for (s = 0; s < SPLIT_MAX; ++s) {
152 13968 : state->parity[l].split_map[s].path[0] = 0;
153 13968 : state->parity[l].split_map[s].uuid[0] = 0;
154 13968 : state->parity[l].split_map[s].fstype[0] = 0;
155 13968 : state->parity[l].split_map[s].fslabel[0] = 0;
156 13968 : state->parity[l].split_map[s].size = PARITY_SIZE_INVALID;
157 13968 : state->parity[l].split_map[s].device = 0;
158 : }
159 1746 : state->parity[l].smartctl[0] = 0;
160 8730 : for (i = 0; i < SMART_IGNORE_MAX; ++i)
161 6984 : state->parity[l].smartignore[i] = 0;
162 1746 : state->parity[l].total_blocks = 0;
163 1746 : state->parity[l].free_blocks = 0;
164 1746 : state->parity[l].skip_access = 0;
165 1746 : state->parity[l].tick = 0;
166 1746 : state->parity[l].cached_blocks = 0;
167 1746 : state->parity[l].is_excluded_by_filter = 0;
168 : }
169 291 : state->tick_io = 0;
170 291 : state->tick_misc = 0;
171 291 : state->tick_sched = 0;
172 291 : state->tick_raid = 0;
173 291 : state->tick_hash = 0;
174 291 : state->tick_last = tick();
175 291 : state->share[0] = 0;
176 291 : state->pool[0] = 0;
177 291 : state->pool_device = 0;
178 291 : state->lockfile[0] = 0;
179 291 : state->level = 1; /* default is the lowest protection */
180 291 : state->clear_past_hash = 0;
181 291 : state->no_conf = 0;
182 1455 : for (i = 0; i < SMART_IGNORE_MAX; ++i)
183 1164 : state->smartignore[i] = 0;
184 291 : state->rehash_blocks = 0;
185 291 : state->bad_blocks = 0;
186 291 : state->unsynced_blocks = 0;
187 291 : state->unscrubbed_blocks = 0;
188 291 : state->thermal_stop_gathering = 0;
189 291 : state->thermal_ambient_temperature = 0;
190 291 : state->thermal_highest_temperature = 0;
191 291 : state->thermal_first = 0;
192 291 : state->thermal_latest = 0;
193 291 : state->thermal_temperature_limit = 0;
194 291 : state->thermal_cooldown_time = 0;
195 :
196 291 : tommy_list_init(&state->disklist);
197 291 : tommy_list_init(&state->maplist);
198 291 : tommy_list_init(&state->contentlist);
199 291 : tommy_list_init(&state->filterlist);
200 291 : tommy_list_init(&state->importlist);
201 291 : tommy_list_init(&state->thermallist);
202 291 : tommy_hashdyn_init(&state->importset);
203 291 : tommy_hashdyn_init(&state->previmportset);
204 291 : tommy_hashdyn_init(&state->searchset);
205 291 : tommy_arrayblkof_init(&state->infoarr, sizeof(snapraid_info));
206 291 : tommy_list_init(&state->bucketlist);
207 291 : }
208 :
209 280 : void state_done(struct snapraid_state* state)
210 : {
211 280 : tommy_list_foreach(&state->disklist, (tommy_foreach_func*)disk_free);
212 280 : tommy_list_foreach(&state->maplist, (tommy_foreach_func*)map_free);
213 280 : tommy_list_foreach(&state->contentlist, (tommy_foreach_func*)content_free);
214 280 : tommy_list_foreach(&state->filterlist, (tommy_foreach_func*)filter_free);
215 280 : tommy_list_foreach(&state->importlist, (tommy_foreach_func*)import_file_free);
216 280 : tommy_list_foreach(&state->thermallist, (tommy_foreach_func*)thermal_free);
217 280 : tommy_hashdyn_foreach(&state->searchset, (tommy_foreach_func*)search_file_free);
218 280 : tommy_hashdyn_done(&state->importset);
219 280 : tommy_hashdyn_done(&state->previmportset);
220 280 : tommy_hashdyn_done(&state->searchset);
221 280 : tommy_arrayblkof_done(&state->infoarr);
222 280 : tommy_list_foreach(&state->bucketlist, (tommy_foreach_func*)bucket_free);
223 280 : }
224 :
225 : /**
226 : * Check the configuration.
227 : */
228 289 : static void state_config_check(struct snapraid_state* state, const char* path, tommy_list* filterlist_disk)
229 : {
230 : tommy_node* i;
231 : unsigned l, s;
232 :
233 : /* check for parity level */
234 289 : if (state->raid_mode == RAID_MODE_VANDERMONDE) {
235 3 : if (state->level > 3) {
236 : /* LCOV_EXCL_START */
237 : log_fatal(EUSER, "Using z-parity limits you to a maximum of 3 parities.\n");
238 : exit(EXIT_FAILURE);
239 : /* LCOV_EXCL_STIO */
240 : }
241 : }
242 :
243 : for (l = 0; l < state->level; ++l) {
244 : if (state->parity[l].split_mac == 0) {
245 : /* LCOV_EXCL_START */
246 : log_fatal(EUSER, "Missing '%s' specification in '%s'\n", lev_config_name(l), path);
247 : exit(EXIT_FAILURE);
248 : /* LCOV_EXCL_STOP */
249 : }
250 : }
251 :
252 289 : if (tommy_list_empty(&state->contentlist)) {
253 : /* LCOV_EXCL_START */
254 : log_fatal(EUSER, "Missing 'content' specification in '%s'\n", path);
255 : exit(EXIT_FAILURE);
256 : /* LCOV_EXCL_STOP */
257 : }
258 :
259 : /* check for equal paths */
260 1947 : for (i = state->contentlist; i != 0; i = i->next) {
261 1658 : struct snapraid_content* content = i->data;
262 :
263 10666 : for (l = 0; l < state->level; ++l) {
264 44944 : for (s = 0; s < state->parity[l].split_mac; ++s) {
265 35936 : if (pathcmp(state->parity[l].split_map[s].path, content->content) == 0) {
266 : /* LCOV_EXCL_START */
267 : log_fatal(EUSER, "Same path used for '%s' and 'content' as '%s'\n", lev_config_name(l), content->content);
268 : exit(EXIT_FAILURE);
269 : /* LCOV_EXCL_STOP */
270 : }
271 : }
272 : }
273 : }
274 :
275 : /* check device of data disks */
276 289 : if (!state->opt.skip_device && !state->opt.skip_disk_access) {
277 44 : for (i = state->disklist; i != 0; i = i->next) {
278 : tommy_node* j;
279 40 : struct snapraid_disk* disk = i->data;
280 :
281 : /* skip data disks that are not accessible */
282 40 : if (disk->skip_access)
283 0 : continue;
284 :
285 : #ifdef _WIN32
286 : if (disk->device == 0) {
287 : /* LCOV_EXCL_START */
288 : log_fatal(ESOFT, "Disk '%s' has a zero serial number.\n", disk->dir);
289 : log_fatal(ESOFT, "This is not necessarily wrong, but for using SnapRAID\n");
290 : log_fatal(ESOFT, "it's better to change the serial number of the disk.\n");
291 : log_fatal(ESOFT, "Try using the 'VolumeID' tool by 'Mark Russinovich'.\n");
292 : exit(EXIT_FAILURE);
293 : /* LCOV_EXCL_STOP */
294 : }
295 : #endif
296 :
297 220 : for (j = i->next; j != 0; j = j->next) {
298 180 : struct snapraid_disk* other = j->data;
299 180 : if (disk->device == other->device) {
300 0 : if (state->opt.force_device) {
301 : /* note that we just ignore the issue */
302 : /* and we DON'T mark the disk to be skipped */
303 : /* because we want to use these disks */
304 0 : if (!state->opt.no_warnings)
305 0 : log_fatal(EUSER, "DANGER! Ignoring that disks '%s' and '%s' are on the same device\n", disk->name, other->name);
306 : } else {
307 : /* LCOV_EXCL_START */
308 : log_fatal(ESOFT, "Disks '%s' and '%s' are on the same device.\n", disk->dir, other->dir);
309 : #ifdef _WIN32
310 : log_fatal(ESOFT, "Both have the serial number '%" PRIx64 "'.\n", disk->device);
311 : log_fatal(ESOFT, "Try using the 'VolumeID' tool by 'Mark Russinovich'\n");
312 : log_fatal(ESOFT, "to change one of the disk serial.\n");
313 : #endif
314 : /* in "fix" we allow to continue anyway */
315 : if (strcmp(state->command, "fix") == 0) {
316 : log_fatal(ESOFT, "You can '%s' anyway, using 'snapraid --force-device %s'.\n", state->command, state->command);
317 : }
318 : exit(EXIT_FAILURE);
319 : /* LCOV_EXCL_STOP */
320 : }
321 : }
322 : }
323 :
324 : /* skip data disks that are not accessible */
325 40 : if (disk->skip_access)
326 0 : continue;
327 :
328 40 : if (!state->opt.skip_parity_access) {
329 120 : for (l = 0; l < state->level; ++l) {
330 160 : for (s = 0; s < state->parity[l].split_mac; ++s) {
331 80 : if (disk->device == state->parity[l].split_map[s].device) {
332 0 : if (state->opt.force_device) {
333 : /* note that we just ignore the issue */
334 : /* and we DON'T mark the disk to be skipped */
335 : /* because we want to use these disks */
336 0 : if (!state->opt.no_warnings)
337 0 : log_fatal(EUSER, "DANGER! Ignoring that disks '%s' and %s '%s' are on the same device\n", disk->dir, lev_name(l), state->parity[l].split_map[s].path);
338 : } else {
339 : /* LCOV_EXCL_START */
340 : log_fatal(ESOFT, "Disk '%s' and %s '%s' are on the same device.\n", disk->dir, lev_name(l), state->parity[l].split_map[s].path);
341 : #ifdef _WIN32
342 : log_fatal(ESOFT, "Both have the serial number '%" PRIx64 "'.\n", disk->device);
343 : log_fatal(ESOFT, "Try using the 'VolumeID' tool by 'Mark Russinovich'\n");
344 : log_fatal(ESOFT, "to change one of the disk serial.\n");
345 : #endif
346 : exit(EXIT_FAILURE);
347 : /* LCOV_EXCL_STOP */
348 : }
349 : }
350 : }
351 : }
352 : }
353 :
354 40 : if (state->pool[0] != 0 && disk->device == state->pool_device) {
355 : /* LCOV_EXCL_START */
356 : log_fatal(ESOFT, "Disk '%s' and pool '%s' are on the same device.\n", disk->dir, state->pool);
357 : #ifdef _WIN32
358 : log_fatal(ESOFT, "Both have the serial number '%" PRIx64 "'.\n", disk->device);
359 : log_fatal(ESOFT, "Try using the 'VolumeID' tool by 'Mark Russinovich'\n");
360 : log_fatal(ESOFT, "to change one of the disk serial.\n");
361 : #endif
362 : exit(EXIT_FAILURE);
363 : /* LCOV_EXCL_STOP */
364 : }
365 : }
366 : }
367 :
368 : /* check device of parity disks */
369 289 : if (!state->opt.skip_device && !state->opt.skip_parity_access) {
370 12 : for (l = 0; l < state->level; ++l) {
371 16 : for (s = 0; s < state->parity[l].split_mac; ++s) {
372 : unsigned j, t;
373 :
374 : /* skip parity disks that are not accessible */
375 8 : if (state->parity[l].skip_access)
376 0 : continue;
377 :
378 : #ifdef _WIN32
379 : if (state->parity[l].split_map[s].device == 0) {
380 : /* LCOV_EXCL_START */
381 : log_fatal(ESOFT, "Disk '%s' has a zero serial number.\n", state->parity[l].split_map[s].path);
382 : log_fatal(ESOFT, "This is not necessarily wrong, but for using SnapRAID\n");
383 : log_fatal(ESOFT, "it's better to change the serial number of the disk.\n");
384 : log_fatal(ESOFT, "Try using the 'VolumeID' tool by 'Mark Russinovich'.\n");
385 : exit(EXIT_FAILURE);
386 : /* LCOV_EXCL_STOP */
387 : }
388 : #endif
389 :
390 12 : for (j = l + 1; j < state->level; ++j) {
391 8 : for (t = 0; t < state->parity[j].split_mac; ++t) {
392 4 : if (state->parity[l].split_map[s].device == state->parity[j].split_map[t].device) {
393 0 : if (state->opt.force_device) {
394 : /* note that we just ignore the issue */
395 : /* and we DON'T mark the disk to be skipped */
396 : /* because we want to use these disks */
397 0 : if (!state->opt.no_warnings)
398 0 : log_fatal(EUSER, "DANGER! Skipping parities '%s' and '%s' on the same device\n", lev_config_name(l), lev_config_name(j));
399 : } else {
400 : /* LCOV_EXCL_START */
401 : log_fatal(ESOFT, "Parity '%s' and '%s' are on the same device.\n", state->parity[l].split_map[s].path, state->parity[j].split_map[t].path);
402 : #ifdef _WIN32
403 : log_fatal(ESOFT, "Both have the serial number '%" PRIx64 "'.\n", state->parity[l].split_map[s].device);
404 : log_fatal(ESOFT, "Try using the 'VolumeID' tool by 'Mark Russinovich'\n");
405 : log_fatal(ESOFT, "to change one of the disk serial.\n");
406 : #endif
407 : /* in "fix" we allow to continue anyway */
408 : if (strcmp(state->command, "fix") == 0) {
409 : log_fatal(ESOFT, "You can '%s' anyway, using 'snapraid --force-device %s'.\n", state->command, state->command);
410 : }
411 : exit(EXIT_FAILURE);
412 : /* LCOV_EXCL_STOP */
413 : }
414 : }
415 : }
416 : }
417 :
418 8 : if (state->pool[0] != 0 && state->pool_device == state->parity[l].split_map[s].device) {
419 : /* LCOV_EXCL_START */
420 : log_fatal(ESOFT, "Pool '%s' and parity '%s' are on the same device.\n", state->pool, state->parity[l].split_map[s].path);
421 : #ifdef _WIN32
422 : log_fatal(ESOFT, "Both have the serial number '%" PRIx64 "'.\n", state->pool_device);
423 : log_fatal(ESOFT, "Try using the 'VolumeID' tool by 'Mark Russinovich'\n");
424 : log_fatal(ESOFT, "to change one of the disk serial.\n");
425 : #endif
426 : exit(EXIT_FAILURE);
427 : /* LCOV_EXCL_STOP */
428 : }
429 : }
430 : }
431 : }
432 :
433 : /* check device of pool disk */
434 : #ifdef _WIN32
435 : if (!state->opt.skip_device) {
436 : if (state->pool[0] != 0 && state->pool_device == 0) {
437 : /* LCOV_EXCL_START */
438 : log_fatal(ESOFT, "Disk '%s' has a zero serial number.\n", state->pool);
439 : log_fatal(ESOFT, "This is not necessarily wrong, but for using SnapRAID\n");
440 : log_fatal(ESOFT, "it's better to change the serial number of the disk.\n");
441 : log_fatal(ESOFT, "Try using the 'VolumeID' tool by 'Mark Russinovich'.\n");
442 : exit(EXIT_FAILURE);
443 : /* LCOV_EXCL_STOP */
444 : }
445 : }
446 : #endif
447 :
448 : /* count the content files */
449 289 : if (!state->opt.skip_device && !state->opt.skip_content_access) {
450 : unsigned content_count;
451 :
452 4 : content_count = 0;
453 20 : for (i = state->contentlist; i != 0; i = i->next) {
454 : tommy_node* j;
455 16 : struct snapraid_content* content = i->data;
456 :
457 : /* check if there are others in the same disk */
458 40 : for (j = i->next; j != 0; j = j->next) {
459 24 : struct snapraid_content* other = j->data;
460 24 : if (content->device == other->device) {
461 0 : log_fatal(ESOFT, "WARNING! Content files on the same disk: '%s' and '%s'.\n", content->content, other->content);
462 0 : break;
463 : }
464 : }
465 16 : if (j != 0) {
466 : /* skip it */
467 0 : continue;
468 : }
469 :
470 16 : ++content_count;
471 : }
472 :
473 4 : if (content_count < state->level + 1) {
474 : /* LCOV_EXCL_START */
475 : log_fatal(EUSER, "You must have at least %d 'content' files in different disks.\n", state->level + 1);
476 : exit(EXIT_FAILURE);
477 : /* LCOV_EXCL_STOP */
478 : }
479 : }
480 :
481 : /* check for speed */
482 : #ifdef CONFIG_X86
483 289 : if (!raid_cpu_has_ssse3())
484 : #endif
485 0 : if (state->raid_mode == RAID_MODE_CAUCHY && !state->opt.no_warnings) {
486 0 : if (state->level == 3) {
487 0 : log_fatal(ESOFT, "WARNING! Your CPU doesn't have a fast implementation for triple parity.\n");
488 0 : log_fatal(ESOFT, "WARNING! It's recommended to switch to 'z-parity' instead than '3-parity'.\n");
489 0 : } else if (state->level > 3) {
490 0 : log_fatal(ESOFT, "WARNING! Your CPU doesn't have a fast implementation beyond triple parity.\n");
491 0 : log_fatal(ESOFT, "WARNING! It's recommended to reduce the parity levels to triple parity.\n");
492 : }
493 : }
494 :
495 : /* ensure that specified filter disks are valid ones */
496 300 : for (i = tommy_list_head(filterlist_disk); i != 0; i = i->next) {
497 : tommy_node* j;
498 11 : struct snapraid_filter* filter = i->data;
499 58 : for (j = state->disklist; j != 0; j = j->next) {
500 51 : struct snapraid_disk* disk = j->data;
501 51 : if (wnmatch(filter->pattern, disk->name) == 0)
502 4 : break;
503 : }
504 11 : if (j == 0) {
505 : /* check matching with parity disks */
506 24 : for (l = 0; l < state->level; ++l)
507 24 : if (wnmatch(filter->pattern, lev_config_name(l)) == 0)
508 7 : break;
509 7 : if (l == state->level) {
510 : /* LCOV_EXCL_START */
511 : log_fatal(EUSER, "Option -d, --filter-disk %s doesn't match any data or parity disk.\n", filter->pattern);
512 : exit(EXIT_FAILURE);
513 : /* LCOV_EXCL_STOP */
514 : }
515 : }
516 : }
517 289 : }
518 :
519 : /**
520 : * Validate the smartctl command.
521 : *
522 : * It must contains only one %s string, and not other % chars.
523 : */
524 390 : static int validate_smartctl(const char* custom)
525 : {
526 390 : const char* s = custom;
527 390 : int arg = 0;
528 :
529 2340 : while (*s) {
530 1950 : if (s[0] == '%' && s[1] == 's') {
531 195 : if (arg) {
532 : /* LCOV_EXCL_START */
533 : return -1;
534 : /* LCOV_EXCL_STOP */
535 : }
536 195 : arg = 1;
537 1755 : } else if (s[0] == '%') {
538 : /* LCOV_EXCL_START */
539 : return -1;
540 : /* LCOV_EXCL_STOP */
541 : }
542 :
543 1950 : ++s;
544 : }
545 :
546 390 : return 0;
547 : }
548 :
549 290 : void state_config(struct snapraid_state* state, const char* path, const char* command, struct snapraid_option* opt, tommy_list* filterlist_disk)
550 : {
551 : STREAM* f;
552 : unsigned line;
553 : tommy_node* i;
554 : unsigned l, s;
555 :
556 : /* copy the options */
557 290 : state->opt = *opt;
558 :
559 : /* if unset, sort by physical order */
560 290 : if (!state->opt.force_order)
561 5 : state->opt.force_order = SORT_PHYSICAL;
562 :
563 : /* adjust file mode */
564 290 : if (state->opt.file_mode != ADVISE_DEFAULT) {
565 285 : state->file_mode = state->opt.file_mode;
566 : } else {
567 : /* default mode, if nothing is specified */
568 5 : state->file_mode = ADVISE_DISCARD;
569 : }
570 :
571 : /* store current command */
572 290 : state->command = command;
573 :
574 290 : log_tag("conf:file:%s\n", path);
575 :
576 290 : f = sopen_read(path, 0);
577 290 : if (!f) {
578 : /* LCOV_EXCL_START */
579 : if (errno == ENOENT) {
580 : if (state->opt.auto_conf) {
581 : log_tag("conf:missing:\n");
582 : /* mark that we are without a configuration file */
583 : state->no_conf = 1;
584 : state->level = 0;
585 : return;
586 : }
587 :
588 : log_fatal(errno, "No configuration file found at '%s'\n", path);
589 : } else if (errno == EACCES) {
590 : log_fatal(errno, "You do not have rights to access the configuration file '%s'\n", path);
591 : } else {
592 : log_fatal(errno, "Error opening the configuration file '%s'. %s.\n", path, strerror(errno));
593 : }
594 : exit(EXIT_FAILURE);
595 : /* LCOV_EXCL_STOP */
596 : }
597 :
598 290 : line = 1;
599 6788 : while (1) {
600 : char tag[PATH_MAX];
601 : char buffer[PATH_MAX];
602 : int ret;
603 : int c;
604 : unsigned level;
605 :
606 : /* skip initial spaces */
607 7078 : sgetspace(f);
608 :
609 : /* read the command */
610 7078 : ret = sgettok(f, tag, sizeof(tag));
611 7078 : if (ret < 0) {
612 : /* LCOV_EXCL_START */
613 : log_fatal(EUSER, "Error reading the configuration file '%s' at line %u\n", path, line);
614 : exit(EXIT_FAILURE);
615 : /* LCOV_EXCL_STOP */
616 : }
617 :
618 : /* skip spaces after the command */
619 7078 : sgetspace(f);
620 :
621 7078 : if (strcmp(tag, "blocksize") == 0
622 : /* block_size is the old format of the option */
623 6792 : || strcmp(tag, "block_size") == 0) {
624 :
625 290 : ret = sgetu32(f, &state->block_size);
626 290 : if (ret < 0) {
627 : /* LCOV_EXCL_START */
628 : log_fatal(EUSER, "Invalid 'blocksize' specification in '%s' at line %u\n", path, line);
629 : exit(EXIT_FAILURE);
630 : /* LCOV_EXCL_STOP */
631 : }
632 290 : if (state->block_size < 1) {
633 : /* LCOV_EXCL_START */
634 : log_fatal(EUSER, "Too small 'blocksize' specification in '%s' at line %u\n", path, line);
635 : exit(EXIT_FAILURE);
636 : /* LCOV_EXCL_STOP */
637 : }
638 290 : if (state->block_size > 16 * KIBI) {
639 : /* LCOV_EXCL_START */
640 : log_fatal(EUSER, "Too big 'blocksize' specification in '%s' at line %u\n", path, line);
641 : exit(EXIT_FAILURE);
642 : /* LCOV_EXCL_STOP */
643 : }
644 : /* check if it's a power of 2 */
645 290 : if ((state->block_size & (state->block_size - 1)) != 0) {
646 : /* LCOV_EXCL_START */
647 : log_fatal(EUSER, "Not power of 2 'blocksize' specification in '%s' at line %u\n", path, line);
648 : exit(EXIT_FAILURE);
649 : /* LCOV_EXCL_STOP */
650 : }
651 290 : state->block_size *= KIBI;
652 6788 : } else if (strcmp(tag, "hashsize") == 0
653 6743 : || strcmp(tag, "hash_size") == 0 /* v11.0 used incorrectly this one, kept now for backward compatibility */
654 45 : ) {
655 : uint32_t hash_size;
656 :
657 45 : ret = sgetu32(f, &hash_size);
658 45 : if (ret < 0) {
659 : /* LCOV_EXCL_START */
660 : log_fatal(EUSER, "Invalid 'hashsize' specification in '%s' at line %u\n", path, line);
661 : exit(EXIT_FAILURE);
662 : /* LCOV_EXCL_STOP */
663 : }
664 45 : if (hash_size < 2) {
665 : /* LCOV_EXCL_START */
666 : log_fatal(EUSER, "Too small 'hashsize' specification in '%s' at line %u\n", path, line);
667 : exit(EXIT_FAILURE);
668 : /* LCOV_EXCL_STOP */
669 : }
670 45 : if (hash_size > HASH_MAX) {
671 : /* LCOV_EXCL_START */
672 : log_fatal(EUSER, "Too big 'hashsize' specification in '%s' at line %u\n", path, line);
673 : exit(EXIT_FAILURE);
674 : /* LCOV_EXCL_STOP */
675 : }
676 : /* check if it's a power of 2 */
677 45 : if ((hash_size & (hash_size - 1)) != 0) {
678 : /* LCOV_EXCL_START */
679 : log_fatal(EUSER, "Not power of 2 'hashsize' specification in '%s' at line %u\n", path, line);
680 : exit(EXIT_FAILURE);
681 : /* LCOV_EXCL_STOP */
682 : }
683 :
684 45 : BLOCK_HASH_SIZE = hash_size;
685 6743 : } else if (lev_config_scan(tag, &level, &state->raid_mode) == 0) {
686 : char device[PATH_MAX];
687 : char* split_map[SPLIT_MAX + 1];
688 : unsigned split_mac;
689 : char* slash;
690 : uint64_t dev;
691 : int skip_access;
692 :
693 1371 : if (state->parity[level].split_mac != 0) {
694 : /* LCOV_EXCL_START */
695 : log_fatal(EUSER, "Multiple '%s' specification in '%s' at line %u\n", tag, path, line);
696 : exit(EXIT_FAILURE);
697 : /* LCOV_EXCL_STOP */
698 : }
699 :
700 1371 : ret = sgetlasttok(f, buffer, sizeof(buffer));
701 1371 : if (ret < 0) {
702 : /* LCOV_EXCL_START */
703 : log_fatal(EUSER, "Invalid '%s' specification in '%s' at line %u\n", tag, path, line);
704 : exit(EXIT_FAILURE);
705 : /* LCOV_EXCL_STOP */
706 : }
707 :
708 1371 : if (!*buffer) {
709 : /* LCOV_EXCL_START */
710 : log_fatal(EUSER, "Empty '%s' specification in '%s' at line %u\n", tag, path, line);
711 : exit(EXIT_FAILURE);
712 : /* LCOV_EXCL_STOP */
713 : }
714 :
715 1371 : split_mac = strsplit(split_map, SPLIT_MAX + 1, buffer, ",");
716 :
717 1371 : if (split_mac > SPLIT_MAX) {
718 : /* LCOV_EXCL_START */
719 : log_fatal(EUSER, "Too many files in '%s' specification in '%s' at line %u\n", tag, path, line);
720 : exit(EXIT_FAILURE);
721 : /* LCOV_EXCL_STOP */
722 : }
723 :
724 1371 : skip_access = 0;
725 1371 : state->parity[level].split_mac = split_mac;
726 6827 : for (s = 0; s < split_mac; ++s) {
727 : char uuid[UUID_MAX];
728 5457 : pathimport(state->parity[level].split_map[s].path, sizeof(state->parity[level].split_map[s].path), split_map[s]);
729 :
730 5457 : if (!state->opt.skip_parity_access) {
731 : struct stat st;
732 :
733 : /* get the device of the directory containing the parity file */
734 4745 : pathimport(device, sizeof(device), split_map[s]);
735 4745 : slash = strrchr(device, '/');
736 4745 : if (slash)
737 4745 : *slash = 0;
738 : else
739 0 : pathcpy(device, sizeof(device), ".");
740 :
741 4745 : if (stat(device, &st) == 0) {
742 4740 : dev = st.st_dev;
743 :
744 : /* read the uuid, if unsupported use an empty one */
745 4740 : if (devuuid(dev, split_map[s], uuid, sizeof(uuid)) != 0) {
746 0 : *uuid = 0;
747 : }
748 : } else {
749 : /* if the disk can be skipped */
750 5 : if (state->opt.force_device) {
751 : /* use a fake device, and mark the disk to be skipped */
752 4 : dev = 0;
753 4 : *uuid = 0;
754 4 : skip_access = 1;
755 4 : log_fatal(errno, "DANGER! Skipping inaccessible parity disk '%s'...\n", tag);
756 : } else {
757 : /* LCOV_EXCL_START */
758 : log_fatal(errno, "Error accessing 'parity' dir '%s' specification in '%s' at line %u\n", device, path, line);
759 :
760 : /* in "fix" we allow to continue anyway */
761 : if (strcmp(state->command, "fix") == 0) {
762 : log_fatal(errno, "You can '%s' anyway, using 'snapraid --force-device %s'.\n", state->command, state->command);
763 : }
764 : exit(EXIT_FAILURE);
765 : /* LCOV_EXCL_STOP */
766 : }
767 : }
768 : } else {
769 : /* use a fake device */
770 712 : dev = 0;
771 712 : *uuid = 0;
772 : }
773 :
774 5456 : state->parity[level].split_map[s].device = dev;
775 5456 : pathcpy(state->parity[level].split_map[s].uuid, sizeof(state->parity[level].split_map[s].uuid), uuid);
776 : }
777 :
778 : /* store the global parity skip_access */
779 1370 : state->parity[level].skip_access = skip_access;
780 :
781 : /* adjust the level */
782 1370 : if (state->level < level + 1)
783 1080 : state->level = level + 1;
784 5372 : } else if (strcmp(tag, "share") == 0) {
785 45 : if (*state->share) {
786 : /* LCOV_EXCL_START */
787 : log_fatal(EUSER, "Multiple 'share' specification in '%s' at line %u\n", path, line);
788 : exit(EXIT_FAILURE);
789 : /* LCOV_EXCL_STOP */
790 : }
791 :
792 45 : ret = sgetlasttok(f, buffer, sizeof(buffer));
793 45 : if (ret < 0) {
794 : /* LCOV_EXCL_START */
795 : log_fatal(EUSER, "Invalid 'share' specification in '%s' at line %u\n", path, line);
796 : exit(EXIT_FAILURE);
797 : /* LCOV_EXCL_STOP */
798 : }
799 :
800 45 : if (!*buffer) {
801 : /* LCOV_EXCL_START */
802 : log_fatal(EUSER, "Empty 'share' specification in '%s' at line %u\n", path, line);
803 : exit(EXIT_FAILURE);
804 : /* LCOV_EXCL_STOP */
805 : }
806 :
807 45 : pathimport(state->share, sizeof(state->share), buffer);
808 5327 : } else if (strcmp(tag, "pool") == 0) {
809 : struct stat st;
810 :
811 49 : if (*state->pool) {
812 : /* LCOV_EXCL_START */
813 : log_fatal(EUSER, "Multiple 'pool' specification in '%s' at line %u\n", path, line);
814 : exit(EXIT_FAILURE);
815 : /* LCOV_EXCL_STOP */
816 : }
817 :
818 49 : ret = sgetlasttok(f, buffer, sizeof(buffer));
819 49 : if (ret < 0) {
820 : /* LCOV_EXCL_START */
821 : log_fatal(EUSER, "Invalid 'pool' specification in '%s' at line %u\n", path, line);
822 : exit(EXIT_FAILURE);
823 : /* LCOV_EXCL_STOP */
824 : }
825 :
826 49 : if (!*buffer) {
827 : /* LCOV_EXCL_START */
828 : log_fatal(EUSER, "Empty 'pool' specification in '%s' at line %u\n", path, line);
829 : exit(EXIT_FAILURE);
830 : /* LCOV_EXCL_STOP */
831 : }
832 :
833 49 : pathimport(state->pool, sizeof(state->pool), buffer);
834 :
835 : /* get the device of the directory containing the pool tree */
836 49 : if (stat(buffer, &st) != 0) {
837 : /* LCOV_EXCL_START */
838 : log_fatal(errno, "Error accessing 'pool' dir '%s' specification in '%s' at line %u\n", buffer, path, line);
839 : exit(EXIT_FAILURE);
840 : /* LCOV_EXCL_STOP */
841 : }
842 :
843 49 : state->pool_device = st.st_dev;
844 5278 : } else if (strcmp(tag, "content") == 0) {
845 : struct snapraid_content* content;
846 : char device[PATH_MAX];
847 : char* slash;
848 : struct stat st;
849 : uint64_t dev;
850 :
851 1658 : ret = sgetlasttok(f, buffer, sizeof(buffer));
852 1658 : if (ret < 0) {
853 : /* LCOV_EXCL_START */
854 : log_fatal(EUSER, "Invalid 'content' specification in '%s' at line %u\n", path, line);
855 : exit(EXIT_FAILURE);
856 : /* LCOV_EXCL_STOP */
857 : }
858 :
859 1658 : if (pathcmp(buffer, "/dev/null") == 0 || pathcmp(buffer, "NUL") == 0) {
860 : /* LCOV_EXCL_START */
861 : log_fatal(EUSER, "You cannot use the null device as 'content' specification in '%s' at line %u\n", path, line);
862 : exit(EXIT_FAILURE);
863 : /* LCOV_EXCL_STOP */
864 : }
865 :
866 1658 : if (!*buffer) {
867 : /* LCOV_EXCL_START */
868 : log_fatal(EUSER, "Empty 'content' specification in '%s' at line %u\n", path, line);
869 : exit(EXIT_FAILURE);
870 : /* LCOV_EXCL_STOP */
871 : }
872 :
873 : /* check if the content file is already specified */
874 6170 : for (i = state->contentlist; i != 0; i = i->next) {
875 4512 : content = i->data;
876 4512 : if (pathcmp(content->content, buffer) == 0)
877 0 : break;
878 : }
879 1658 : if (i) {
880 : /* LCOV_EXCL_START */
881 : log_fatal(EUSER, "Duplicate 'content' specification in '%s' at line %u\n", path, line);
882 : exit(EXIT_FAILURE);
883 : /* LCOV_EXCL_STOP */
884 : }
885 :
886 : /* get the device of the directory containing the content file */
887 1658 : pathimport(device, sizeof(device), buffer);
888 1658 : slash = strrchr(device, '/');
889 1658 : if (slash)
890 1658 : *(slash + 1) = 0;
891 : else
892 0 : pathcpy(device, sizeof(device), ".");
893 1658 : if (stat(device, &st) == 0) {
894 1657 : dev = st.st_dev;
895 : } else {
896 1 : if (state->opt.skip_content_access) {
897 : /* use a fake device */
898 1 : dev = 0;
899 1 : log_fatal(errno, "WARNING! Skipping inaccessible content file '%s'...\n", buffer);
900 : } else {
901 : /* LCOV_EXCL_START */
902 : log_fatal(errno, "Error accessing 'content' dir '%s' specification in '%s' at line %u\n", device, path, line);
903 : exit(EXIT_FAILURE);
904 : /* LCOV_EXCL_STOP */
905 : }
906 : }
907 :
908 : /* set the lock file at the first accessible content file */
909 1658 : if (state->lockfile[0] == 0 && dev != 0) {
910 289 : pathcpy(state->lockfile, sizeof(state->lockfile), buffer);
911 289 : pathcat(state->lockfile, sizeof(state->lockfile), ".lock");
912 : }
913 :
914 1658 : content = content_alloc(buffer, dev);
915 :
916 1658 : tommy_list_insert_tail(&state->contentlist, &content->node, content);
917 5365 : } else if (strcmp(tag, "data") == 0 || strcmp(tag, "disk") == 0) {
918 : /* "disk" is the deprecated name up to SnapRAID 9.x */
919 : char dir[PATH_MAX];
920 : char device[PATH_MAX];
921 : char uuid[UUID_MAX];
922 : struct snapraid_disk* disk;
923 : uint64_t dev;
924 : int skip_access;
925 :
926 1745 : ret = sgettok(f, buffer, sizeof(buffer));
927 1745 : if (ret < 0) {
928 : /* LCOV_EXCL_START */
929 : log_fatal(EUSER, "Invalid 'data' name specification in '%s' at line %u\n", path, line);
930 : exit(EXIT_FAILURE);
931 : /* LCOV_EXCL_STOP */
932 : }
933 :
934 1745 : if (!*buffer) {
935 : /* LCOV_EXCL_START */
936 : log_fatal(EUSER, "Empty 'data' name specification in '%s' at line %u\n", path, line);
937 : exit(EXIT_FAILURE);
938 : /* LCOV_EXCL_STOP */
939 : }
940 :
941 1745 : sgetspace(f);
942 :
943 1745 : ret = sgetlasttok(f, dir, sizeof(dir));
944 1745 : if (ret < 0) {
945 : /* LCOV_EXCL_START */
946 : log_fatal(EUSER, "Invalid 'data' dir specification in '%s' at line %u\n", path, line);
947 : exit(EXIT_FAILURE);
948 : /* LCOV_EXCL_STOP */
949 : }
950 :
951 1745 : if (!*dir) {
952 : /* LCOV_EXCL_START */
953 : log_fatal(EUSER, "Empty 'data' dir specification in '%s' at line %u\n", path, line);
954 : exit(EXIT_FAILURE);
955 : /* LCOV_EXCL_STOP */
956 : }
957 :
958 : /* get the device of the dir */
959 1745 : pathimport(device, sizeof(device), dir);
960 :
961 : /* check if the disk name already exists */
962 6175 : for (i = state->disklist; i != 0; i = i->next) {
963 4430 : disk = i->data;
964 4430 : if (strcmp(disk->name, buffer) == 0)
965 0 : break;
966 : }
967 1745 : if (i) {
968 : /* LCOV_EXCL_START */
969 : log_fatal(EUSER, "Duplicate 'data' name '%s' at line %u\n", buffer, line);
970 : exit(EXIT_FAILURE);
971 : /* LCOV_EXCL_STOP */
972 : }
973 :
974 : /* if the disk has to be present */
975 1745 : skip_access = 0;
976 1745 : if (!state->opt.skip_disk_access) {
977 : struct stat st;
978 :
979 1589 : if (stat(device, &st) == 0) {
980 1588 : dev = st.st_dev;
981 :
982 : /* read the uuid, if unsupported use an empty one */
983 1588 : if (devuuid(dev, dir, uuid, sizeof(uuid)) != 0) {
984 0 : *uuid = 0;
985 : }
986 :
987 : /* fake a different UUID when testing */
988 1588 : if (state->opt.fake_uuid) {
989 2 : snprintf(uuid, sizeof(uuid), "fake-uuid-%d", state->opt.fake_uuid);
990 2 : --state->opt.fake_uuid;
991 : }
992 : } else {
993 : /* if the disk can be skipped */
994 1 : if (state->opt.force_device) {
995 : /* use a fake device, and mark the disk to be skipped */
996 1 : dev = 0;
997 1 : *uuid = 0;
998 1 : skip_access = 1;
999 1 : log_fatal(errno, "DANGER! Skipping inaccessible data disk '%s'...\n", buffer);
1000 : } else {
1001 : /* LCOV_EXCL_START */
1002 : log_fatal(errno, "Error accessing 'disk' '%s' specification in '%s' at line %u\n", dir, device, line);
1003 :
1004 : /* in "fix" we allow to continue anyway */
1005 : if (strcmp(state->command, "fix") == 0) {
1006 : log_fatal(errno, "You can '%s' anyway, using 'snapraid --force-device %s'.\n", state->command, state->command);
1007 : }
1008 : exit(EXIT_FAILURE);
1009 : /* LCOV_EXCL_STOP */
1010 : }
1011 : }
1012 : } else {
1013 : /* use a fake device */
1014 156 : dev = 0;
1015 156 : *uuid = 0;
1016 : }
1017 :
1018 1745 : disk = disk_alloc(buffer, dir, dev, uuid, skip_access);
1019 :
1020 1745 : tommy_list_insert_tail(&state->disklist, &disk->node, disk);
1021 1875 : } else if (strcmp(tag, "smartctl") == 0) {
1022 : char custom[PATH_MAX];
1023 :
1024 390 : ret = sgettok(f, buffer, sizeof(buffer));
1025 390 : if (ret < 0) {
1026 : /* LCOV_EXCL_START */
1027 : log_fatal(EUSER, "Invalid 'smartctl' name in '%s' at line %u\n", path, line);
1028 : exit(EXIT_FAILURE);
1029 : /* LCOV_EXCL_STOP */
1030 : }
1031 :
1032 390 : if (!*buffer) {
1033 : /* LCOV_EXCL_START */
1034 : log_fatal(EUSER, "Empty 'smartctl' name in '%s' at line %u\n", path, line);
1035 : exit(EXIT_FAILURE);
1036 : /* LCOV_EXCL_STOP */
1037 : }
1038 :
1039 390 : sgetspace(f);
1040 :
1041 390 : ret = sgetlasttok(f, custom, sizeof(custom));
1042 390 : if (ret < 0) {
1043 : /* LCOV_EXCL_START */
1044 : log_fatal(EUSER, "Invalid 'smartctl' option in '%s' at line %u\n", path, line);
1045 : exit(EXIT_FAILURE);
1046 : /* LCOV_EXCL_STOP */
1047 : }
1048 :
1049 390 : if (!*custom) {
1050 : /* LCOV_EXCL_START */
1051 : log_fatal(EUSER, "Empty 'smartctl' option in '%s' at line %u\n", path, line);
1052 : exit(EXIT_FAILURE);
1053 : /* LCOV_EXCL_STOP */
1054 : }
1055 :
1056 390 : if (validate_smartctl(custom) != 0) {
1057 : /* LCOV_EXCL_START */
1058 : log_fatal(EUSER, "Invalid 'smartctl' option in '%s' at line %u\n", path, line);
1059 : exit(EXIT_FAILURE);
1060 : /* LCOV_EXCL_STOP */
1061 : }
1062 :
1063 : /* search for parity */
1064 390 : if (lev_config_scan(buffer, &level, 0) == 0) {
1065 195 : if (state->parity[level].smartctl[0] != 0) {
1066 : /* LCOV_EXCL_START */
1067 : log_fatal(EUSER, "Duplicate parity 'smartctl' '%s' at line %u\n", buffer, line);
1068 : exit(EXIT_FAILURE);
1069 : /* LCOV_EXCL_STOP */
1070 : }
1071 :
1072 195 : pathcpy(state->parity[level].smartctl, sizeof(state->parity[level].smartctl), custom);
1073 : } else {
1074 : struct snapraid_disk* disk;
1075 :
1076 : /* search the disk */
1077 195 : disk = 0;
1078 195 : for (i = state->disklist; i != 0; i = i->next) {
1079 195 : disk = i->data;
1080 195 : if (strcmp(disk->name, buffer) == 0)
1081 195 : break;
1082 : }
1083 195 : if (!disk) {
1084 : /* LCOV_EXCL_START */
1085 : log_fatal(EUSER, "Missing disk 'smartctl' '%s' at line %u\n", buffer, line);
1086 : exit(EXIT_FAILURE);
1087 : /* LCOV_EXCL_STOP */
1088 : }
1089 195 : if (disk->smartctl[0] != 0) {
1090 : /* LCOV_EXCL_START */
1091 : log_fatal(EUSER, "Duplicate disk name '%s' at line %u\n", buffer, line);
1092 : exit(EXIT_FAILURE);
1093 : /* LCOV_EXCL_STOP */
1094 : }
1095 :
1096 195 : pathcpy(disk->smartctl, sizeof(disk->smartctl), custom);
1097 : }
1098 1485 : } else if (strcmp(tag, "smartignore") == 0) {
1099 : int* smart;
1100 :
1101 388 : ret = sgettok(f, buffer, sizeof(buffer));
1102 388 : if (ret < 0) {
1103 : /* LCOV_EXCL_START */
1104 : log_fatal(EUSER, "Invalid 'smartignore' name in '%s' at line %u\n", path, line);
1105 : exit(EXIT_FAILURE);
1106 : /* LCOV_EXCL_STOP */
1107 : }
1108 :
1109 388 : if (!*buffer) {
1110 : /* LCOV_EXCL_START */
1111 : log_fatal(EUSER, "Empty 'smartignore' name in '%s' at line %u\n", path, line);
1112 : exit(EXIT_FAILURE);
1113 : /* LCOV_EXCL_STOP */
1114 : }
1115 :
1116 388 : sgetspace(f);
1117 :
1118 388 : if (strcmp(buffer, "*") == 0) {
1119 194 : smart = state->smartignore;
1120 194 : } else if (lev_config_scan(buffer, &level, 0) == 0) { /* search for parity */
1121 194 : smart = state->parity[level].smartignore;
1122 : } else {
1123 : struct snapraid_disk* disk;
1124 :
1125 : /* search the disk */
1126 0 : disk = 0;
1127 0 : for (i = state->disklist; i != 0; i = i->next) {
1128 0 : disk = i->data;
1129 0 : if (strcmp(disk->name, buffer) == 0)
1130 0 : break;
1131 : }
1132 0 : if (!disk) {
1133 : /* LCOV_EXCL_START */
1134 : log_fatal(EUSER, "Missing disk 'smartctl' '%s' at line %u\n", buffer, line);
1135 : exit(EXIT_FAILURE);
1136 : /* LCOV_EXCL_STOP */
1137 : }
1138 :
1139 0 : smart = disk->smartignore;
1140 : }
1141 :
1142 388 : int si = 0;
1143 :
1144 388 : while (1) {
1145 : char* e;
1146 :
1147 776 : ret = sgettok(f, buffer, sizeof(buffer));
1148 776 : if (ret < 0) {
1149 : /* LCOV_EXCL_START */
1150 : log_fatal(EUSER, "Invalid 'smartignore' specification in '%s' at line %u\n", path, line);
1151 : exit(EXIT_FAILURE);
1152 : /* LCOV_EXCL_STOP */
1153 : }
1154 :
1155 776 : if (!*buffer)
1156 388 : break;
1157 :
1158 388 : if (si == SMART_IGNORE_MAX) {
1159 : /* LCOV_EXCL_START */
1160 : log_fatal(EUSER, "Too many 'smartignore' specification in '%s' at line %u\n", path, line);
1161 : exit(EXIT_FAILURE);
1162 : /* LCOV_EXCL_STOP */
1163 : }
1164 :
1165 388 : smart[si] = strtoul(buffer, &e, 0);
1166 :
1167 388 : if (!e || *e) {
1168 : /* LCOV_EXCL_START */
1169 : log_fatal(EUSER, "Invalid 'smartignore' specification in '%s' at line %u\n", path, line);
1170 : exit(EXIT_FAILURE);
1171 : /* LCOV_EXCL_STOP */
1172 : }
1173 :
1174 388 : ++si;
1175 :
1176 388 : sgetspace(f);
1177 : }
1178 :
1179 388 : if (si == 0) {
1180 : /* LCOV_EXCL_START */
1181 : log_fatal(EUSER, "Invalid 'smartignore' specification in '%s' at line %u\n", path, line);
1182 : exit(EXIT_FAILURE);
1183 : /* LCOV_EXCL_STOP */
1184 : }
1185 1097 : } else if (strcmp(tag, "temp_limit") == 0) {
1186 : unsigned temp;
1187 :
1188 5 : ret = sgetu32(f, &temp);
1189 5 : if (ret < 0) {
1190 : /* LCOV_EXCL_START */
1191 : log_fatal(EUSER, "Invalid 'temp_limit' specification in '%s' at line %u\n", path, line);
1192 : exit(EXIT_FAILURE);
1193 : /* LCOV_EXCL_STOP */
1194 : }
1195 5 : if (temp < 40 || temp > 70) {
1196 : /* LCOV_EXCL_START */
1197 : log_fatal(EUSER, "Invalid 'temp_limit' temperature specification in '%s' at line %u. It must be between 40 and 70\n", path, line);
1198 : exit(EXIT_FAILURE);
1199 : /* LCOV_EXCL_STOP */
1200 : }
1201 :
1202 5 : state->thermal_temperature_limit = temp;
1203 1092 : } else if (strcmp(tag, "temp_sleep") == 0) {
1204 : unsigned time;
1205 :
1206 5 : ret = sgetu32(f, &time);
1207 5 : if (ret < 0) {
1208 : /* LCOV_EXCL_START */
1209 : log_fatal(EUSER, "Invalid 'temp_sleep' specification in '%s' at line %u\n", path, line);
1210 : exit(EXIT_FAILURE);
1211 : /* LCOV_EXCL_STOP */
1212 : }
1213 5 : if (time < 3 || time > 120) {
1214 : /* LCOV_EXCL_START */
1215 : log_fatal(EUSER, "Invalid 'temp_sleep' temperature specification in '%s' at line %u. It must be between 3 and 120\n", path, line);
1216 : exit(EXIT_FAILURE);
1217 : /* LCOV_EXCL_STOP */
1218 : }
1219 :
1220 5 : state->thermal_cooldown_time = time * 60;
1221 1087 : } else if (strcmp(tag, "nohidden") == 0) {
1222 0 : state->filter_hidden = 1;
1223 1087 : } else if (strcmp(tag, "exclude") == 0) {
1224 : struct snapraid_filter* filter;
1225 :
1226 305 : ret = sgetlasttok(f, buffer, sizeof(buffer));
1227 305 : if (ret < 0) {
1228 : /* LCOV_EXCL_START */
1229 : log_fatal(EUSER, "Invalid 'exclude' specification in '%s' at line %u\n", path, line);
1230 : exit(EXIT_FAILURE);
1231 : /* LCOV_EXCL_STOP */
1232 : }
1233 :
1234 305 : if (!*buffer) {
1235 : /* LCOV_EXCL_START */
1236 : log_fatal(EUSER, "Empty 'exclude' specification in '%s' at line %u\n", path, line);
1237 : exit(EXIT_FAILURE);
1238 : /* LCOV_EXCL_STOP */
1239 : }
1240 :
1241 305 : filter = filter_alloc_file(-1, "", buffer);
1242 305 : if (!filter) {
1243 : /* LCOV_EXCL_START */
1244 : log_fatal(EUSER, "Invalid 'exclude' specification '%s' in '%s' at line %u\n", buffer, path, line);
1245 : exit(EXIT_FAILURE);
1246 : /* LCOV_EXCL_STOP */
1247 : }
1248 305 : tommy_list_insert_tail(&state->filterlist, &filter->node, filter);
1249 782 : } else if (strcmp(tag, "include") == 0) {
1250 : struct snapraid_filter* filter;
1251 :
1252 285 : ret = sgetlasttok(f, buffer, sizeof(buffer));
1253 285 : if (ret < 0) {
1254 : /* LCOV_EXCL_START */
1255 : log_fatal(EUSER, "Invalid 'include' specification in '%s' at line %u\n", path, line);
1256 : exit(EXIT_FAILURE);
1257 : /* LCOV_EXCL_STOP */
1258 : }
1259 :
1260 285 : if (!*buffer) {
1261 : /* LCOV_EXCL_START */
1262 : log_fatal(EUSER, "Empty 'include' specification in '%s' at line %u\n", path, line);
1263 : exit(EXIT_FAILURE);
1264 : /* LCOV_EXCL_STOP */
1265 : }
1266 :
1267 285 : filter = filter_alloc_file(1, "", buffer);
1268 285 : if (!filter) {
1269 : /* LCOV_EXCL_START */
1270 : log_fatal(EUSER, "Invalid 'include' specification '%s' in '%s' at line %u\n", buffer, path, line);
1271 : exit(EXIT_FAILURE);
1272 : /* LCOV_EXCL_STOP */
1273 : }
1274 285 : tommy_list_insert_tail(&state->filterlist, &filter->node, filter);
1275 497 : } else if (strcmp(tag, "autosave") == 0) {
1276 : char* e;
1277 :
1278 49 : ret = sgetlasttok(f, buffer, sizeof(buffer));
1279 49 : if (ret < 0) {
1280 : /* LCOV_EXCL_START */
1281 : log_fatal(EUSER, "Invalid 'autosave' specification in '%s' at line %u\n", path, line);
1282 : exit(EXIT_FAILURE);
1283 : /* LCOV_EXCL_STOP */
1284 : }
1285 :
1286 49 : if (!*buffer) {
1287 : /* LCOV_EXCL_START */
1288 : log_fatal(EUSER, "Empty 'autosave' specification in '%s' at line %u\n", path, line);
1289 : exit(EXIT_FAILURE);
1290 : /* LCOV_EXCL_STOP */
1291 : }
1292 :
1293 49 : state->autosave = strtoul(buffer, &e, 0);
1294 :
1295 49 : if (!e || *e) {
1296 : /* LCOV_EXCL_START */
1297 : log_fatal(EUSER, "Invalid 'autosave' specification in '%s' at line %u\n", path, line);
1298 : exit(EXIT_FAILURE);
1299 : /* LCOV_EXCL_STOP */
1300 : }
1301 :
1302 : /* convert to GB */
1303 49 : state->autosave *= GIGA;
1304 448 : } else if (tag[0] == 0) {
1305 : /* allow empty lines */
1306 81 : } else if (tag[0] == '#') {
1307 81 : ret = sgetline(f, buffer, sizeof(buffer));
1308 81 : if (ret < 0) {
1309 : /* LCOV_EXCL_START */
1310 : log_fatal(EUSER, "Invalid comment in '%s' at line %u\n", path, line);
1311 : exit(EXIT_FAILURE);
1312 : /* LCOV_EXCL_STOP */
1313 : }
1314 : } else {
1315 : /* LCOV_EXCL_START */
1316 : log_fatal(EUSER, "Invalid command '%s' in '%s' at line %u\n", tag, path, line);
1317 : exit(EXIT_FAILURE);
1318 : /* LCOV_EXCL_STOP */
1319 : }
1320 :
1321 : /* skip final spaces */
1322 7077 : sgetspace(f);
1323 :
1324 : /* next line */
1325 7077 : c = sgeteol(f);
1326 7077 : if (c == EOF) {
1327 289 : break;
1328 : }
1329 6788 : if (c != '\n') {
1330 : /* LCOV_EXCL_START */
1331 : log_fatal(EUSER, "Extra data in '%s' at line %u\n", path, line);
1332 : exit(EXIT_FAILURE);
1333 : /* LCOV_EXCL_STOP */
1334 : }
1335 6788 : ++line;
1336 : }
1337 :
1338 289 : if (serror(f)) {
1339 : /* LCOV_EXCL_START */
1340 : log_fatal(errno, "Error reading the configuration file '%s' at line %u\n", path, line);
1341 : exit(EXIT_FAILURE);
1342 : /* LCOV_EXCL_STOP */
1343 : }
1344 :
1345 289 : sclose(f);
1346 :
1347 289 : state_config_check(state, path, filterlist_disk);
1348 :
1349 : /* select the default hash */
1350 289 : if (state->opt.force_murmur3) {
1351 1 : state->besthash = HASH_MURMUR3;
1352 288 : } else if (state->opt.force_spooky2) {
1353 1 : state->besthash = HASH_SPOOKY2;
1354 : } else {
1355 287 : state->besthash = membesthash();
1356 : }
1357 :
1358 : /* by default use the best hash */
1359 289 : state->hash = state->besthash;
1360 :
1361 : /* by default use a random hash seed */
1362 289 : if (randomize(state->hashseed, HASH_MAX) != 0) {
1363 : /* LCOV_EXCL_START */
1364 : log_fatal(errno, "Failed to retrieve random values.\n");
1365 : exit(EXIT_FAILURE);
1366 : /* LCOV_EXCL_STOP */
1367 : }
1368 :
1369 : /* no previous hash by default */
1370 289 : state->prevhash = HASH_UNDEFINED;
1371 :
1372 : /* intentionally not set the prevhashseed, if used valgrind will warn about it */
1373 :
1374 289 : log_tag("blocksize:%u\n", state->block_size);
1375 2034 : for (i = state->disklist; i != 0; i = i->next) {
1376 1745 : struct snapraid_disk* disk = i->data;
1377 1745 : log_tag("data:%s:%s:%s\n", disk->name, disk->dir, disk->uuid);
1378 : }
1379 :
1380 289 : log_tag("mode:%s\n", lev_raid_name(state->raid_mode, state->level));
1381 1654 : for (l = 0; l < state->level; ++l) {
1382 1365 : log_tag("%s:%s:%s\n", lev_config_name(l), state->parity[l].split_map[0].path, state->parity[l].split_map[0].uuid);
1383 5436 : for (s = 1; s < state->parity[l].split_mac; ++s) {
1384 4071 : log_tag("%s/%u:%s:%s\n", lev_config_name(l), s, state->parity[l].split_map[s].path, state->parity[l].split_map[s].uuid);
1385 : }
1386 : }
1387 289 : if (state->pool[0] != 0)
1388 49 : log_tag("pool:%s\n", state->pool);
1389 289 : if (state->share[0] != 0)
1390 45 : log_tag("share:%s\n", state->share);
1391 289 : if (state->autosave != 0)
1392 49 : log_tag("autosave:%" PRIu64 "\n", state->autosave);
1393 879 : for (i = tommy_list_head(&state->filterlist); i != 0; i = i->next) {
1394 : char out[PATH_MAX];
1395 590 : struct snapraid_filter* filter = i->data;
1396 590 : log_tag("filter:%s\n", filter_type(filter, out, sizeof(out)));
1397 : }
1398 289 : if (state->filter_hidden)
1399 0 : log_tag("filter:nohidden:\n");
1400 289 : log_flush();
1401 : }
1402 :
1403 : /**
1404 : * Find a disk by name.
1405 : */
1406 7760 : static struct snapraid_disk* find_disk_by_name(struct snapraid_state* state, const char* name)
1407 : {
1408 : tommy_node* i;
1409 :
1410 27081 : for (i = state->disklist; i != 0; i = i->next) {
1411 27073 : struct snapraid_disk* disk = i->data;
1412 27073 : if (strcmp(disk->name, name) == 0)
1413 7752 : return disk;
1414 : }
1415 :
1416 8 : if (state->no_conf) {
1417 : /* without a configuration file, add disks automatically */
1418 : struct snapraid_disk* disk;
1419 :
1420 6 : disk = disk_alloc(name, "DUMMY/", -1, "", 0);
1421 :
1422 6 : tommy_list_insert_tail(&state->disklist, &disk->node, disk);
1423 :
1424 6 : return disk;
1425 : }
1426 :
1427 2 : return 0;
1428 : }
1429 :
1430 : /**
1431 : * Find a disk by UUID.
1432 : */
1433 2 : static struct snapraid_disk* find_disk_by_uuid(struct snapraid_state* state, const char* uuid)
1434 : {
1435 : tommy_node* i;
1436 2 : struct snapraid_disk* found = 0;
1437 :
1438 : /* special test case to find the first matching UUID */
1439 : /* when testing UUID are all equal or not supported */
1440 : /* and we should handle this case specifically */
1441 2 : if (state->opt.match_first_uuid)
1442 2 : return state->disklist->data;
1443 :
1444 : /* LCOV_EXCL_START */
1445 : /* never find an empty uuid */
1446 : if (!*uuid)
1447 : return 0;
1448 :
1449 : for (i = state->disklist; i != 0; i = i->next) {
1450 : struct snapraid_disk* disk = i->data;
1451 : if (strcmp(disk->uuid, uuid) == 0) {
1452 : /* never match duplicate UUID */
1453 : if (found)
1454 : return 0;
1455 : found = disk;
1456 : }
1457 : }
1458 :
1459 : return found;
1460 : /* LCOV_EXCL_STOP */
1461 : }
1462 :
1463 : /**
1464 : * Update the disk mapping if required.
1465 : */
1466 279 : static void state_map(struct snapraid_state* state)
1467 : {
1468 : unsigned hole;
1469 : tommy_node* i;
1470 : unsigned uuid_mismatch;
1471 : unsigned diskcount;
1472 : unsigned l, s;
1473 :
1474 : /* remove all the mapping without a disk */
1475 : /* this happens when a disk is removed from the configuration file */
1476 : /* From SnapRAID 4.0 mappings are automatically removed if a disk is not used */
1477 : /* when saving the content file, but we keep this code to import older content files. */
1478 1790 : for (i = state->maplist; i != 0; ) {
1479 1511 : struct snapraid_map* map = i->data;
1480 : struct snapraid_disk* disk;
1481 :
1482 1511 : disk = find_disk_by_name(state, map->name);
1483 :
1484 : /* go to the next mapping before removing */
1485 1511 : i = i->next;
1486 :
1487 1511 : if (disk == 0) {
1488 : /* disk not found, remove the mapping */
1489 0 : tommy_list_remove_existing(&state->maplist, &map->node);
1490 0 : map_free(map);
1491 : }
1492 : }
1493 :
1494 : /* maps each unmapped disk present in the configuration file in the first available hole */
1495 : /* this happens when you add disks for the first time in the configuration file */
1496 279 : hole = 0; /* first position to try */
1497 1948 : for (i = state->disklist; i != 0; i = i->next) {
1498 1669 : struct snapraid_disk* disk = i->data;
1499 : struct snapraid_map* map;
1500 : tommy_node* j;
1501 :
1502 : /* check if the disk is already mapped */
1503 5829 : for (j = state->maplist; j != 0; j = j->next) {
1504 5671 : map = j->data;
1505 5671 : if (strcmp(disk->name, map->name) == 0) {
1506 : /* mapping found */
1507 1511 : break;
1508 : }
1509 : }
1510 1669 : if (j != 0) {
1511 : /* mapping is present, then copy the free blocks into to disk */
1512 1511 : disk->total_blocks = map->total_blocks;
1513 1511 : disk->free_blocks = map->free_blocks;
1514 1511 : continue;
1515 : }
1516 :
1517 : /* mapping not found, search for an hole */
1518 : while (1) {
1519 1100 : for (j = state->maplist; j != 0; j = j->next) {
1520 942 : map = j->data;
1521 942 : if (map->position == hole) {
1522 : /* position already used */
1523 158 : break;
1524 : }
1525 : }
1526 316 : if (j == 0) {
1527 : /* hole found */
1528 158 : break;
1529 : }
1530 :
1531 : /* try with the next one */
1532 158 : ++hole;
1533 : }
1534 :
1535 : /* insert the new mapping */
1536 158 : map = map_alloc(disk->name, hole, 0, 0, "");
1537 :
1538 158 : tommy_list_insert_tail(&state->maplist, &map->node, map);
1539 : }
1540 :
1541 : /* without configuration don't check for number of data disks or uuid changes */
1542 279 : if (state->no_conf)
1543 1 : return;
1544 :
1545 : /* counter for the number of UUID mismatches */
1546 278 : uuid_mismatch = 0;
1547 :
1548 : /* check if mapping match the disk uuid */
1549 278 : if (!state->opt.skip_disk_access) {
1550 1759 : for (i = state->maplist; i != 0; i = i->next) {
1551 1507 : struct snapraid_map* map = i->data;
1552 : struct snapraid_disk* disk;
1553 :
1554 1507 : disk = find_disk_by_name(state, map->name);
1555 1507 : if (disk == 0) {
1556 : /* LCOV_EXCL_START */
1557 : log_fatal(EINTERNAL, "Internal inconsistency: Mapping not found for '%s'\n", map->name);
1558 : os_abort();
1559 : /* LCOV_EXCL_STOP */
1560 : }
1561 :
1562 1507 : if (disk->has_unsupported_uuid) {
1563 : /* if uuid is not available, skip this one */
1564 1 : continue;
1565 : }
1566 :
1567 : /* if the uuid is changed */
1568 1506 : if (strcmp(disk->uuid, map->uuid) != 0) {
1569 : /* mark the disk as with an UUID change */
1570 113 : disk->has_different_uuid = 1;
1571 :
1572 : /* if the previous uuid is available */
1573 113 : if (map->uuid[0] != 0) {
1574 : /* count the number of uuid change */
1575 4 : ++uuid_mismatch;
1576 4 : log_fatal(ESOFT, "UUID change for disk '%s' from '%s' to '%s'\n", disk->name, map->uuid, disk->uuid);
1577 : } else {
1578 : /* no message here, because having a disk without */
1579 : /* UUID is the normal state of an empty disk */
1580 109 : disk->had_empty_uuid = 1;
1581 : }
1582 :
1583 : /* update the uuid in the mapping, */
1584 113 : pathcpy(map->uuid, sizeof(map->uuid), disk->uuid);
1585 :
1586 : /* write the new state with the new uuid */
1587 113 : state->need_write = 1;
1588 : }
1589 : }
1590 : }
1591 :
1592 : /* check the parity uuid */
1593 278 : if (!state->opt.skip_parity_access) {
1594 1377 : for (l = 0; l < state->level; ++l) {
1595 5685 : for (s = 0; s < state->parity[l].split_mac; ++s) {
1596 : char uuid[UUID_MAX];
1597 : int ret;
1598 :
1599 4548 : ret = devuuid(state->parity[l].split_map[s].device, state->parity[l].split_map[s].path, uuid, sizeof(uuid));
1600 :
1601 4548 : if (ret != 0) {
1602 : /* uuid not available, just ignore */
1603 4 : continue;
1604 : }
1605 :
1606 : /* if the uuid is changed */
1607 4544 : if (strcmp(uuid, state->parity[l].split_map[s].uuid) != 0) {
1608 : /* if the previous uuid is available */
1609 0 : if (state->parity[l].split_map[s].uuid[0] != 0) {
1610 : /* count the number of uuid change */
1611 0 : ++uuid_mismatch;
1612 0 : log_fatal(ESOFT, "UUID change for parity '%s/%u' from '%s' to '%s'\n", lev_config_name(l), s, state->parity[l].split_map[s].uuid, uuid);
1613 : }
1614 :
1615 : /* update the uuid */
1616 0 : pathcpy(state->parity[l].split_map[s].uuid, sizeof(state->parity[l].split_map[s].uuid), uuid);
1617 :
1618 : /* write the new state with the new uuid */
1619 0 : state->need_write = 1;
1620 : }
1621 : }
1622 : }
1623 : }
1624 :
1625 278 : if (!state->opt.force_uuid && uuid_mismatch > state->level) {
1626 : /* LCOV_EXCL_START */
1627 : log_fatal(ESOFT, "Too many disks have changed UUIDs since the last 'sync'.\n");
1628 : log_fatal(ESOFT, "If this happened because you actually replaced them,\n");
1629 : log_fatal(ESOFT, "you can still '%s', using 'snapraid --force-uuid %s'.\n", state->command, state->command);
1630 : log_fatal(ESOFT, "Alternatively, you may have misconfigured the disk mount points,\n");
1631 : log_fatal(ESOFT, "and you need to restore the mount points to the state of the latest sync.\n");
1632 : exit(EXIT_FAILURE);
1633 : /* LCOV_EXCL_STOP */
1634 : }
1635 :
1636 : /* count the number of data disks, including holes left after removing some */
1637 278 : diskcount = 0;
1638 1941 : for (i = state->maplist; i != 0; i = i->next) {
1639 1663 : struct snapraid_map* map = i->data;
1640 :
1641 1663 : if (map->position + 1 > diskcount)
1642 1475 : diskcount = map->position + 1;
1643 : }
1644 :
1645 : /* ensure to don't go over the limit of the RAID engine */
1646 278 : if (diskcount > RAID_DATA_MAX) {
1647 : /* LCOV_EXCL_START */
1648 : log_fatal(ESOFT, "Too many data disks. Maximum allowed is %u.\n", RAID_DATA_MAX);
1649 : exit(EXIT_FAILURE);
1650 : /* LCOV_EXCL_STOP */
1651 : }
1652 :
1653 : /* now count the real number of data disks, excluding holes left after removing some */
1654 278 : diskcount = tommy_list_count(&state->maplist);
1655 :
1656 : /* recommend number of parities */
1657 278 : if (!state->opt.no_warnings) {
1658 : /* LCOV_EXCL_START */
1659 : if (diskcount >= 36 && state->level < 6) {
1660 : log_error(EUSER, "WARNING! For %u disks, it's recommended to use six parity levels.\n", diskcount);
1661 : } else if (diskcount >= 29 && state->level < 5) {
1662 : log_error(EUSER, "WARNING! For %u disks, it's recommended to use five parity levels.\n", diskcount);
1663 : } else if (diskcount >= 22 && state->level < 4) {
1664 : log_error(EUSER, "WARNING! For %u disks, it's recommended to use four parity levels.\n", diskcount);
1665 : } else if (diskcount >= 15 && state->level < 3) {
1666 : log_error(EUSER, "WARNING! For %u disks, it's recommended to use three parity levels.\n", diskcount);
1667 : } else if (diskcount >= 5 && state->level < 2) {
1668 : log_error(EUSER, "WARNING! For %u disks, it's recommended to use two parity levels.\n", diskcount);
1669 : }
1670 : /* LCOV_EXCL_STOP */
1671 : }
1672 : }
1673 :
1674 213 : void state_refresh(struct snapraid_state* state)
1675 : {
1676 : tommy_node* i;
1677 : unsigned l, s;
1678 213 : uint64_t bs = (uint64_t)state->block_size;
1679 :
1680 : /* for all disks */
1681 1488 : for (i = state->maplist; i != 0; i = i->next) {
1682 1275 : struct snapraid_map* map = i->data;
1683 : struct snapraid_disk* disk;
1684 : uint64_t total_space;
1685 : uint64_t free_space;
1686 : int ret;
1687 :
1688 1275 : disk = find_disk_by_name(state, map->name);
1689 1275 : if (disk == 0) {
1690 : /* LCOV_EXCL_START */
1691 : log_fatal(EINTERNAL, "Internal inconsistency: Mapping not found for '%s'\n", map->name);
1692 : os_abort();
1693 : /* LCOV_EXCL_STOP */
1694 : }
1695 :
1696 1275 : ret = fsinfo(disk->dir, 0, 0, &total_space, &free_space, disk->fstype, sizeof(disk->fstype), disk->fslabel, sizeof(disk->fslabel));
1697 1275 : if (ret != 0) {
1698 : /* LCOV_EXCL_START */
1699 : log_fatal(errno, "Error accessing disk '%s' to retrieve file-system info. %s.\n", disk->dir, strerror(errno));
1700 : exit(EXIT_FAILURE);
1701 : /* LCOV_EXCL_STOP */
1702 : }
1703 :
1704 : /* set the new free blocks */
1705 1275 : map->total_blocks = total_space / bs;
1706 1275 : map->free_blocks = free_space / bs;
1707 :
1708 : /* also update the disk info */
1709 1275 : disk->total_blocks = map->total_blocks;
1710 1275 : disk->free_blocks = map->free_blocks;
1711 :
1712 1275 : log_tag("fsinfo_data_split:%s:%" PRIu64 ":%" PRIu64 ":%s:%s\n", disk->name, disk->total_blocks * bs, disk->free_blocks * bs, disk->fstype, disk->fslabel);
1713 1275 : log_tag("fsinfo_data:%s:%" PRIu64 ":%" PRIu64 "\n", disk->name, disk->total_blocks * bs, disk->free_blocks * bs);
1714 : }
1715 :
1716 : /* for all parities */
1717 1386 : for (l = 0; l < state->level; ++l) {
1718 : /* set the new free blocks */
1719 1173 : state->parity[l].total_blocks = 0;
1720 1173 : state->parity[l].free_blocks = 0;
1721 :
1722 5865 : for (s = 0; s < state->parity[l].split_mac; ++s) {
1723 4692 : struct snapraid_split* split = &state->parity[l].split_map[s];
1724 : uint64_t total_space;
1725 : uint64_t free_space;
1726 : int ret;
1727 :
1728 4692 : ret = fsinfo(split->path, 0, 0, &total_space, &free_space, split->fstype, sizeof(split->fstype), split->fslabel, sizeof(split->fslabel));
1729 4692 : if (ret != 0) {
1730 : /* LCOV_EXCL_START */
1731 : log_fatal(errno, "Error accessing file '%s' to retrieve file-system info. %s.\n", split->path, strerror(errno));
1732 : exit(EXIT_FAILURE);
1733 : /* LCOV_EXCL_STOP */
1734 : }
1735 :
1736 : #ifdef WIN32
1737 : /*
1738 : * Take into account the space we have to leave free
1739 : * to avoid the waring about low space
1740 : */
1741 : if (free_space >= WINDOWS_SPACEHOLDER_SIZE)
1742 : free_space -= WINDOWS_SPACEHOLDER_SIZE;
1743 : else
1744 : free_space = 0;
1745 : #endif
1746 :
1747 : /* add the new free blocks */
1748 4692 : uint64_t split_total_blocks = total_space / bs;
1749 4692 : uint64_t split_free_blocks = free_space / bs;
1750 :
1751 4692 : state->parity[l].total_blocks += split_total_blocks;
1752 4692 : state->parity[l].free_blocks += split_free_blocks;
1753 :
1754 4692 : if (s == 0)
1755 1173 : log_tag("fsinfo_parity_split:%s:%" PRIu64 ":%" PRIu64 ":%s:%s\n", lev_config_name(l), split_total_blocks * bs, split_free_blocks * bs, split->fstype, split->fslabel);
1756 : else
1757 3519 : log_tag("fsinfo_parity_split:%s/%u:%" PRIu64 ":%" PRIu64 ":%s:%s\n", lev_config_name(l), s, split_total_blocks * bs, split_free_blocks * bs, split->fstype, split->fslabel);
1758 : }
1759 :
1760 1173 : log_tag("fsinfo_parity:%s:%" PRIu64 ":%" PRIu64 "\n", lev_config_name(l), state->parity[l].total_blocks * bs, state->parity[l].free_blocks * bs);
1761 : }
1762 :
1763 : /* note what we don't set need_write = 1, because we don't want */
1764 : /* to update the content file only for the free space info. */
1765 213 : }
1766 :
1767 : /**
1768 : * Check the content.
1769 : */
1770 272 : static void state_content_check(struct snapraid_state* state, const char* path)
1771 : {
1772 : tommy_node* i;
1773 :
1774 : /* check that any map has different name and position */
1775 1899 : for (i = state->maplist; i != 0; i = i->next) {
1776 1627 : struct snapraid_map* map = i->data;
1777 : tommy_node* j;
1778 5682 : for (j = i->next; j != 0; j = j->next) {
1779 4055 : struct snapraid_map* other = j->data;
1780 4055 : if (strcmp(map->name, other->name) == 0) {
1781 : /* LCOV_EXCL_START */
1782 : log_fatal(ECONTENT, "Conflicting 'map' disk specification in '%s'\n", path);
1783 : exit(EXIT_FAILURE);
1784 : /* LCOV_EXCL_STOP */
1785 : }
1786 4055 : if (map->position == other->position) {
1787 : /* LCOV_EXCL_START */
1788 : log_fatal(ECONTENT, "Conflicting 'map' index specification in '%s'\n", path);
1789 : exit(EXIT_FAILURE);
1790 : /* LCOV_EXCL_STOP */
1791 : }
1792 : }
1793 : }
1794 272 : }
1795 :
1796 : /**
1797 : * Check if the position is REQUIRED, or we can completely clear it from the state.
1798 : *
1799 : * Note that position with only DELETED blocks are discharged.
1800 : */
1801 831830 : static int fs_position_is_required(struct snapraid_state* state, block_off_t pos)
1802 : {
1803 : tommy_node* i;
1804 :
1805 : /* check for each disk */
1806 910666 : for (i = state->disklist; i != 0; i = i->next) {
1807 908788 : struct snapraid_disk* disk = i->data;
1808 908788 : struct snapraid_block* block = fs_par2block_find(disk, pos);
1809 :
1810 : /* if we have at least one file, the position is needed */
1811 908788 : if (block_has_file(block))
1812 829952 : return 1;
1813 : }
1814 :
1815 1878 : return 0;
1816 : }
1817 :
1818 : /**
1819 : * Check if the info block is REQUIREQ.
1820 : *
1821 : * This is used to ensure that we keep the last check used for scrubbing.
1822 : * and that we add it when importing old context files.
1823 : *
1824 : * Note that you can have position without info blocks, for example
1825 : * if all the blocks are not synced.
1826 : *
1827 : * Note also that not requiring an info block, doesn't mean that if present it
1828 : * can be discarded.
1829 : */
1830 1660597 : static int fs_info_is_required(struct snapraid_state* state, block_off_t pos)
1831 : {
1832 : tommy_node* i;
1833 :
1834 : /* check for each disk */
1835 2187239 : for (i = state->disklist; i != 0; i = i->next) {
1836 2133357 : struct snapraid_disk* disk = i->data;
1837 2133357 : struct snapraid_block* block = fs_par2block_find(disk, pos);
1838 :
1839 : /* if we have at least one synced file, the info is required */
1840 2133357 : if (block_state_get(block) == BLOCK_STATE_BLK)
1841 1606715 : return 1;
1842 : }
1843 :
1844 53882 : return 0;
1845 : }
1846 :
1847 1878 : static void fs_position_clear_deleted(struct snapraid_state* state, block_off_t pos)
1848 : {
1849 : tommy_node* i;
1850 :
1851 : /* check for each disk if block is really used */
1852 13146 : for (i = state->disklist; i != 0; i = i->next) {
1853 11268 : struct snapraid_disk* disk = i->data;
1854 11268 : struct snapraid_block* block = fs_par2block_find(disk, pos);
1855 :
1856 : /* if the block is deleted */
1857 11268 : if (block_state_get(block) == BLOCK_STATE_DELETED) {
1858 : /* set it to empty */
1859 939 : fs_deallocate(disk, pos);
1860 : }
1861 : }
1862 1878 : }
1863 :
1864 : /**
1865 : * Check if a block position in a disk is deleted.
1866 : */
1867 4984014 : static int fs_is_block_deleted(struct snapraid_disk* disk, block_off_t pos)
1868 : {
1869 4984014 : struct snapraid_block* block = fs_par2block_find(disk, pos);
1870 :
1871 4984014 : return block_state_get(block) == BLOCK_STATE_DELETED;
1872 : }
1873 :
1874 : /**
1875 : * Check if a block parity needs sync
1876 : *
1877 : * This is the same logic used in "status" to detect an incomplete "sync",
1878 : * that ignores invalid block, if they are not used by a file in any disk.
1879 : *
1880 : * This means that DELETED blocks won't necessarily imply an invalid parity.
1881 : */
1882 2492427 : static int fs_is_block_unsynced(struct snapraid_state* state, block_off_t pos)
1883 : {
1884 2492427 : int one_invalid = 0;
1885 2492427 : int one_valid = 0;
1886 16158627 : for (tommy_node* i = state->disklist; i != 0; i = i->next) {
1887 13891397 : struct snapraid_disk* disk = i->data;
1888 13891397 : struct snapraid_block* block = fs_par2block_find(disk, pos);
1889 :
1890 13891397 : if (block_has_file(block))
1891 13496976 : one_valid = 1;
1892 13891397 : if (block_has_invalid_parity(block))
1893 226385 : one_invalid = 1;
1894 :
1895 13891397 : if (one_invalid && one_valid)
1896 225197 : return 1;
1897 : }
1898 :
1899 2267230 : return 0;
1900 : }
1901 :
1902 : /**
1903 : * Flush the file checking the final CRC.
1904 : * We exploit the fact that the CRC is always stored in the last 4 bytes.
1905 : */
1906 1 : static void decoding_error(const char* path, STREAM* f)
1907 : {
1908 : unsigned char buf[4];
1909 : uint32_t crc_stored;
1910 : uint32_t crc_computed;
1911 :
1912 1 : if (seof(f)) {
1913 : /* LCOV_EXCL_START */
1914 : log_fatal(ECONTENT, "Unexpected end of content file '%s' at offset %" PRIi64 "\n", path, stell(f));
1915 : log_fatal(ECONTENT, "This content file is truncated. Please use an alternate copy.\n");
1916 : exit(EXIT_FAILURE);
1917 : /* LCOV_EXCL_STOP */
1918 : }
1919 :
1920 1 : if (serror(f)) {
1921 : /* LCOV_EXCL_START */
1922 : log_fatal(errno, "Error reading the content file '%s' at offset %" PRIi64 "\n", path, stell(f));
1923 : exit(EXIT_FAILURE);
1924 : /* LCOV_EXCL_STOP */
1925 : }
1926 :
1927 1 : log_fatal(ECONTENT, "Error decoding '%s' at offset %" PRIi64 "\n", path, stell(f));
1928 :
1929 1 : if (sdeplete(f, buf) != 0) {
1930 : /* LCOV_EXCL_START */
1931 : log_fatal(errno, "Failed to flush content file '%s' at offset %" PRIi64 "\n", path, stell(f));
1932 : exit(EXIT_FAILURE);
1933 : /* LCOV_EXCL_STOP */
1934 : }
1935 :
1936 : /* get the stored crc from the last four bytes */
1937 1 : crc_stored = buf[0] | (uint32_t)buf[1] << 8 | (uint32_t)buf[2] << 16 | (uint32_t)buf[3] << 24;
1938 :
1939 : /* get the computed crc */
1940 1 : crc_computed = scrc(f);
1941 :
1942 : /* adjust the stored crc to include itself */
1943 1 : crc_stored = crc32c(crc_stored, buf, 4);
1944 :
1945 1 : if (crc_computed != crc_stored) {
1946 1 : log_fatal(ECONTENT, "CRC mismatch in '%s'\n", path);
1947 1 : log_fatal(ECONTENT, "This content file is damaged! Please use an alternate copy.\n");
1948 1 : exit(EXIT_FAILURE);
1949 : } else {
1950 0 : log_fatal(ECONTENT, "The CRC of the file is correct!\n");
1951 : }
1952 0 : }
1953 :
1954 273 : static void state_read_content(struct snapraid_state* state, const char* path, STREAM* f)
1955 : {
1956 : block_off_t blockmax;
1957 : unsigned count_file;
1958 : unsigned count_hardlink;
1959 : unsigned count_symlink;
1960 : unsigned count_dir;
1961 : unsigned count_bad;
1962 : unsigned count_rehash;
1963 : unsigned count_unsynced;
1964 : unsigned count_unscrubbed;
1965 : int crc_checked;
1966 : char buffer[PATH_MAX];
1967 : int ret;
1968 : tommy_array disk_mapping;
1969 : uint32_t mapping_max;
1970 : tommy_hashdyn bucket_hash;
1971 :
1972 273 : blockmax = 0;
1973 273 : count_file = 0;
1974 273 : count_hardlink = 0;
1975 273 : count_symlink = 0;
1976 273 : count_dir = 0;
1977 273 : count_bad = 0;
1978 273 : count_rehash = 0;
1979 273 : count_unsynced = 0;
1980 273 : count_unscrubbed = 0;
1981 273 : crc_checked = 0;
1982 273 : mapping_max = 0;
1983 273 : tommy_array_init(&disk_mapping);
1984 273 : tommy_hashdyn_init(&bucket_hash);
1985 :
1986 273 : ret = sread(f, buffer, 12);
1987 273 : if (ret < 0) {
1988 : /* LCOV_EXCL_START */
1989 : decoding_error(path, f);
1990 : log_fatal(ECONTENT, "Invalid header!\n");
1991 : os_abort();
1992 : /* LCOV_EXCL_STOP */
1993 : }
1994 :
1995 : /*
1996 : * File format versions:
1997 : * - SNAPCNT1/SnapRAID 4.0 First version.
1998 : * - SNAPCNT2/SnapRAID 7.0 Adds entries 'M' and 'P', to add free_blocks support.
1999 : * The previous 'm' entry is now deprecated, but supported for importing.
2000 : * Similarly for text file, we add 'mapping' and 'parity' deprecating 'map'.
2001 : * - SNAPCNT3/SnapRAID 11.0 Adds entry 'y' for hash size.
2002 : * - SNAPCNT3/SnapRAID 11.0 Adds entry 'Q' for multi parity file.
2003 : * The previous 'P' entry is now deprecated, but supported for importing.
2004 : */
2005 273 : if (memcmp(buffer, "SNAPCNT1\n\3\0\0", 12) != 0
2006 273 : && memcmp(buffer, "SNAPCNT2\n\3\0\0", 12) != 0
2007 273 : && memcmp(buffer, "SNAPCNT3\n\3\0\0", 12) != 0
2008 : ) {
2009 : /* LCOV_EXCL_START */
2010 : if (memcmp(buffer, "SNAPCNT", 7) != 0) {
2011 : decoding_error(path, f);
2012 : log_fatal(ECONTENT, "Invalid header!\n");
2013 : os_abort();
2014 : } else {
2015 : log_fatal(ECONTENT, "The content file '%s' was generated with a newer version of SnapRAID!\n", path);
2016 : exit(EXIT_FAILURE);
2017 : }
2018 : /* LCOV_EXCL_STOP */
2019 : }
2020 :
2021 3978155 : while (1) {
2022 : int c;
2023 :
2024 : /* read the command */
2025 3978428 : c = sgetc(f);
2026 3978428 : if (c == EOF) {
2027 272 : break;
2028 : }
2029 :
2030 3978156 : if (c == 'f') {
2031 : /* file */
2032 : char sub[PATH_MAX];
2033 : uint64_t v_size;
2034 : uint64_t v_mtime_sec;
2035 : uint32_t v_mtime_nsec;
2036 : uint64_t v_inode;
2037 : uint32_t v_idx;
2038 : struct snapraid_file* file;
2039 : struct snapraid_disk* disk;
2040 : uint32_t mapping;
2041 :
2042 3846723 : ret = sgetb32(f, &mapping);
2043 3846723 : if (ret < 0 || mapping >= mapping_max) {
2044 : /* LCOV_EXCL_START */
2045 : decoding_error(path, f);
2046 : log_fatal(EINTERNAL, "Internal inconsistency: File mapping index out of range\n");
2047 : os_abort();
2048 : /* LCOV_EXCL_STOP */
2049 : }
2050 3846723 : disk = tommy_array_get(&disk_mapping, mapping);
2051 :
2052 3846723 : ret = sgetb64(f, &v_size);
2053 3846723 : if (ret < 0) {
2054 : /* LCOV_EXCL_START */
2055 : decoding_error(path, f);
2056 : os_abort();
2057 : /* LCOV_EXCL_STOP */
2058 : }
2059 :
2060 3846723 : if (state->block_size == 0) {
2061 : /* LCOV_EXCL_START */
2062 : decoding_error(path, f);
2063 : log_fatal(EINTERNAL, "Internal inconsistency: Zero blocksize\n");
2064 : exit(EXIT_FAILURE);
2065 : /* LCOV_EXCL_STOP */
2066 : }
2067 :
2068 : /* check for impossible file size to avoid to crash for a too big allocation */
2069 3846723 : if (v_size / state->block_size > blockmax) {
2070 : /* LCOV_EXCL_START */
2071 : decoding_error(path, f);
2072 : log_fatal(EINTERNAL, "Internal inconsistency: File size too big!\n");
2073 : os_abort();
2074 : /* LCOV_EXCL_STOP */
2075 : }
2076 :
2077 3846723 : ret = sgetb64(f, &v_mtime_sec);
2078 3846723 : if (ret < 0) {
2079 : /* LCOV_EXCL_START */
2080 : decoding_error(path, f);
2081 : os_abort();
2082 : /* LCOV_EXCL_STOP */
2083 : }
2084 :
2085 3846723 : ret = sgetb32(f, &v_mtime_nsec);
2086 3846723 : if (ret < 0) {
2087 : /* LCOV_EXCL_START */
2088 : decoding_error(path, f);
2089 : os_abort();
2090 : /* LCOV_EXCL_STOP */
2091 : }
2092 :
2093 : /* STAT_NSEC_INVALID is encoded as 0 */
2094 3846723 : if (v_mtime_nsec == 0)
2095 0 : v_mtime_nsec = STAT_NSEC_INVALID;
2096 : else
2097 3846723 : --v_mtime_nsec;
2098 :
2099 3846723 : ret = sgetb64(f, &v_inode);
2100 3846723 : if (ret < 0) {
2101 : /* LCOV_EXCL_START */
2102 : decoding_error(path, f);
2103 : os_abort();
2104 : /* LCOV_EXCL_STOP */
2105 : }
2106 :
2107 3846723 : ret = sgetbs(f, sub, sizeof(sub));
2108 3846723 : if (ret < 0) {
2109 : /* LCOV_EXCL_START */
2110 : decoding_error(path, f);
2111 : os_abort();
2112 : /* LCOV_EXCL_STOP */
2113 : }
2114 3846723 : if (!*sub) {
2115 : /* LCOV_EXCL_START */
2116 : decoding_error(path, f);
2117 : log_fatal(EINTERNAL, "Internal inconsistency: Null file!\n");
2118 : os_abort();
2119 : /* LCOV_EXCL_STOP */
2120 : }
2121 :
2122 : /* allocate the file */
2123 3846723 : file = file_alloc(state->block_size, sub, v_size, v_mtime_sec, v_mtime_nsec, v_inode, 0);
2124 :
2125 : /* insert the file in the file containers */
2126 3846723 : tommy_hashdyn_insert(&disk->inodeset, &file->nodeset, file, file_inode_hash(file->inode));
2127 3846723 : tommy_hashdyn_insert(&disk->pathset, &file->pathset, file, file_path_hash(file->sub));
2128 3846723 : tommy_hashdyn_insert(&disk->stampset, &file->stampset, file, file_stamp_hash(file->size, file->mtime_sec, file->mtime_nsec));
2129 3846723 : tommy_list_insert_tail(&disk->filelist, &file->nodelist, file);
2130 :
2131 : /* read all the blocks */
2132 3846723 : v_idx = 0;
2133 7715619 : while (v_idx < file->blockmax) {
2134 : block_off_t v_pos;
2135 : uint32_t v_count;
2136 :
2137 : /* get the "subcommand */
2138 3868896 : c = sgetc(f);
2139 :
2140 3868896 : ret = sgetb32(f, &v_pos);
2141 3868896 : if (ret < 0) {
2142 : /* LCOV_EXCL_START */
2143 : decoding_error(path, f);
2144 : os_abort();
2145 : /* LCOV_EXCL_STOP */
2146 : }
2147 :
2148 3868896 : ret = sgetb32(f, &v_count);
2149 3868896 : if (ret < 0) {
2150 : /* LCOV_EXCL_START */
2151 : decoding_error(path, f);
2152 : os_abort();
2153 : /* LCOV_EXCL_STOP */
2154 : }
2155 :
2156 3868896 : if (v_idx + v_count > file->blockmax) {
2157 : /* LCOV_EXCL_START */
2158 : decoding_error(path, f);
2159 : log_fatal(EINTERNAL, "Internal inconsistency: Block number out of range\n");
2160 : os_abort();
2161 : /* LCOV_EXCL_STOP */
2162 : }
2163 :
2164 3868896 : if (v_pos + v_count > blockmax) {
2165 : /* LCOV_EXCL_START */
2166 : decoding_error(path, f);
2167 : log_fatal(EINTERNAL, "Internal inconsistency: Block size %u/%u!\n", blockmax, v_pos + v_count);
2168 : os_abort();
2169 : /* LCOV_EXCL_START */
2170 : }
2171 :
2172 : /* fill the blocks in the run */
2173 : while (v_count) {
2174 : struct snapraid_block* block = fs_file2block_get(file, v_idx);
2175 :
2176 : switch (c) {
2177 : case 'b' :
2178 : block_state_set(block, BLOCK_STATE_BLK);
2179 : break;
2180 : case 'n' :
2181 : /* deprecated NEW blocks are converted to CHG ones */
2182 : block_state_set(block, BLOCK_STATE_CHG);
2183 : break;
2184 : case 'g' :
2185 : block_state_set(block, BLOCK_STATE_CHG);
2186 : break;
2187 : case 'p' :
2188 : block_state_set(block, BLOCK_STATE_REP);
2189 : break;
2190 : default :
2191 : /* LCOV_EXCL_START */
2192 : decoding_error(path, f);
2193 : log_fatal(ECONTENT, "Invalid block type!\n");
2194 : os_abort();
2195 : /* LCOV_EXCL_STOP */
2196 : }
2197 :
2198 : /* read the hash only for 'blk/chg/rep', and not for 'new' */
2199 9485442 : if (c != 'n') {
2200 9485442 : ret = sread(f, block->hash, BLOCK_HASH_SIZE);
2201 9485442 : if (ret < 0) {
2202 : /* LCOV_EXCL_START */
2203 : decoding_error(path, f);
2204 : os_abort();
2205 : /* LCOV_EXCL_STOP */
2206 : }
2207 : } else {
2208 : /* set the ZERO hash for deprecated NEW blocks */
2209 0 : hash_zero_set(block->hash);
2210 : }
2211 :
2212 : /* if the block contains a hash of past data */
2213 : /* and we are clearing such indeterminate hashes */
2214 9485442 : if (state->clear_past_hash
2215 2820828 : && block_has_past_hash(block)
2216 : ) {
2217 : /* set the hash value to INVALID */
2218 27935 : hash_invalid_set(block->hash);
2219 : }
2220 :
2221 : /* if we are disabling the copy optimization */
2222 : /* we want also to clear any already previously stored information */
2223 : /* in other sync commands */
2224 : /* note that this is required only in sync, and we detect */
2225 : /* this using the clear_past_hash flag */
2226 9485442 : if (state->clear_past_hash
2227 2820828 : && state->opt.force_nocopy
2228 14195 : && block_state_get(block) == BLOCK_STATE_REP
2229 : ) {
2230 : /* set the hash value to INVALID */
2231 1 : hash_invalid_set(block->hash);
2232 : /* convert from REP to CHG block */
2233 1 : block_state_set(block, BLOCK_STATE_CHG);
2234 : }
2235 :
2236 : /* set the parity association */
2237 9485442 : fs_allocate(disk, v_pos, file, v_idx);
2238 :
2239 : /* go to the next block */
2240 9485442 : ++v_idx;
2241 9485442 : ++v_pos;
2242 9485442 : --v_count;
2243 : }
2244 : }
2245 :
2246 : /* stat */
2247 3846723 : ++count_file;
2248 131433 : } else if (c == 'i') {
2249 : /* "inf" command */
2250 : snapraid_info info;
2251 : uint32_t v_pos;
2252 : uint64_t v_oldest;
2253 :
2254 272 : ret = sgetb64(f, &v_oldest);
2255 272 : if (ret < 0) {
2256 : /* LCOV_EXCL_START */
2257 : decoding_error(path, f);
2258 : os_abort();
2259 : /* LCOV_EXCL_STOP */
2260 : }
2261 :
2262 272 : v_pos = 0;
2263 42701 : while (v_pos < blockmax) {
2264 : int bad;
2265 : int rehash;
2266 : int justsynced;
2267 : uint64_t t64;
2268 : uint32_t flag;
2269 : uint32_t v_count;
2270 :
2271 42429 : ret = sgetb32(f, &v_count);
2272 42429 : if (ret < 0) {
2273 : /* LCOV_EXCL_START */
2274 : decoding_error(path, f);
2275 : os_abort();
2276 : /* LCOV_EXCL_STOP */
2277 : }
2278 :
2279 42429 : if (v_pos + v_count > blockmax) {
2280 : /* LCOV_EXCL_START */
2281 : decoding_error(path, f);
2282 : log_fatal(EINTERNAL, "Internal inconsistency: Info size %u/%u!\n", blockmax, v_pos + v_count);
2283 : os_abort();
2284 : /* LCOV_EXCL_STOP */
2285 : }
2286 :
2287 42429 : ret = sgetb32(f, &flag);
2288 42429 : if (ret < 0) {
2289 : /* LCOV_EXCL_START */
2290 : decoding_error(path, f);
2291 : os_abort();
2292 : /* LCOV_EXCL_STOP */
2293 : }
2294 :
2295 : /* if there is an info */
2296 42429 : if ((flag & 1) != 0) {
2297 : /* read the time */
2298 42412 : ret = sgetb64(f, &t64);
2299 42412 : if (ret < 0) {
2300 : /* LCOV_EXCL_START */
2301 : decoding_error(path, f);
2302 : os_abort();
2303 : /* LCOV_EXCL_STOP */
2304 : }
2305 :
2306 : /* analyze the flags */
2307 42412 : bad = (flag & 2) != 0;
2308 42412 : rehash = (flag & 4) != 0;
2309 42412 : justsynced = (flag & 8) != 0;
2310 :
2311 42412 : if (bad)
2312 5301 : count_bad += v_count;
2313 42412 : if (rehash)
2314 14266 : count_rehash += v_count;
2315 42412 : if (justsynced)
2316 21063 : count_unscrubbed += v_count;
2317 :
2318 42412 : if (rehash && state->prevhash == HASH_UNDEFINED) {
2319 : /* LCOV_EXCL_START */
2320 : decoding_error(path, f);
2321 : log_fatal(EINTERNAL, "Internal inconsistency: Missing previous checksum!\n");
2322 : os_abort();
2323 : /* LCOV_EXCL_STOP */
2324 : }
2325 :
2326 42412 : info = info_make(t64 + v_oldest, bad, rehash, justsynced);
2327 :
2328 42412 : bucket_insert(&bucket_hash, t64 + v_oldest, v_count, justsynced);
2329 : } else {
2330 17 : info = 0;
2331 : }
2332 :
2333 1703026 : while (v_count) {
2334 : /* insert the info in the array */
2335 1660597 : info_set(&state->infoarr, v_pos, info);
2336 :
2337 : /* ensure that an info is present only for used positions */
2338 1660597 : if (fs_info_is_required(state, v_pos)) {
2339 1606715 : if (!info) {
2340 : /* LCOV_EXCL_START */
2341 : decoding_error(path, f);
2342 : log_fatal(EINTERNAL, "Internal inconsistency: Missing info!\n");
2343 : os_abort();
2344 : /* LCOV_EXCL_STOP */
2345 : }
2346 : } else {
2347 : /* extra info are accepted for backward compatibility */
2348 : /* they are discarded at the first write */
2349 : }
2350 :
2351 1660597 : if (fs_is_block_unsynced(state, v_pos))
2352 112705 : ++count_unsynced;
2353 :
2354 : /* go to next block */
2355 1660597 : ++v_pos;
2356 1660597 : --v_count;
2357 : }
2358 : }
2359 131161 : } else if (c == 'h') {
2360 : /* hole */
2361 : uint32_t v_pos;
2362 : struct snapraid_disk* disk;
2363 : uint32_t mapping;
2364 :
2365 1511 : ret = sgetb32(f, &mapping);
2366 1511 : if (ret < 0 || mapping >= mapping_max) {
2367 : /* LCOV_EXCL_START */
2368 : decoding_error(path, f);
2369 : log_fatal(EINTERNAL, "Internal inconsistency: Hole mapping index out of range\n");
2370 : os_abort();
2371 : /* LCOV_EXCL_STOP */
2372 : }
2373 1511 : disk = tommy_array_get(&disk_mapping, mapping);
2374 :
2375 1511 : v_pos = 0;
2376 3088 : while (v_pos < blockmax) {
2377 : uint32_t v_idx;
2378 : uint32_t v_count;
2379 : struct snapraid_file* deleted;
2380 :
2381 1577 : ret = sgetb32(f, &v_count);
2382 1577 : if (ret < 0) {
2383 : /* LCOV_EXCL_START */
2384 : decoding_error(path, f);
2385 : os_abort();
2386 : /* LCOV_EXCL_STOP */
2387 : }
2388 :
2389 1577 : if (v_pos + v_count > blockmax) {
2390 : /* LCOV_EXCL_START */
2391 : decoding_error(path, f);
2392 : log_fatal(EINTERNAL, "Internal inconsistency: Hole size %u/%u!\n", blockmax, v_pos + v_count);
2393 : os_abort();
2394 : /* LCOV_EXCL_STOP */
2395 : }
2396 :
2397 : /* get the sub-command */
2398 1577 : c = sgetc(f);
2399 :
2400 1577 : switch (c) {
2401 35 : case 'o' :
2402 : /* if it's a run of deleted blocks */
2403 :
2404 : /* allocate a fake deleted file */
2405 35 : deleted = file_alloc(state->block_size, "<deleted>", v_count * (data_off_t)state->block_size, 0, 0, 0, 0);
2406 :
2407 : /* mark the file as deleted */
2408 35 : file_flag_set(deleted, FILE_IS_DELETED);
2409 :
2410 : /* insert it in the list of deleted files */
2411 35 : tommy_list_insert_tail(&disk->deletedlist, &deleted->nodelist, deleted);
2412 :
2413 : /* process all blocks */
2414 35 : v_idx = 0;
2415 28167 : while (v_count) {
2416 28132 : struct snapraid_block* block = fs_file2block_get(deleted, v_idx);
2417 :
2418 : /* set the block as deleted */
2419 28132 : block_state_set(block, BLOCK_STATE_DELETED);
2420 :
2421 : /* read the hash */
2422 28132 : ret = sread(f, block->hash, BLOCK_HASH_SIZE);
2423 28132 : if (ret < 0) {
2424 : /* LCOV_EXCL_START */
2425 : decoding_error(path, f);
2426 : os_abort();
2427 : /* LCOV_EXCL_STOP */
2428 : }
2429 :
2430 : /* if we are clearing indeterminate hashes */
2431 28132 : if (state->clear_past_hash) {
2432 : /* set the hash value to INVALID */
2433 9514 : hash_invalid_set(block->hash);
2434 : }
2435 :
2436 : /* insert the block in the block array */
2437 28132 : fs_allocate(disk, v_pos, deleted, v_idx);
2438 :
2439 : /* go to next block */
2440 28132 : ++v_pos;
2441 28132 : ++v_idx;
2442 28132 : --v_count;
2443 : }
2444 35 : break;
2445 1542 : case 'O' :
2446 : /* go to the next run */
2447 1542 : v_pos += v_count;
2448 1542 : break;
2449 0 : default :
2450 : /* LCOV_EXCL_START */
2451 : decoding_error(path, f);
2452 : log_fatal(ECONTENT, "Invalid hole type!\n");
2453 : os_abort();
2454 : /* LCOV_EXCL_STOP */
2455 : }
2456 : }
2457 129650 : } else if (c == 's') {
2458 : /* symlink */
2459 : char sub[PATH_MAX];
2460 : char linkto[PATH_MAX];
2461 : struct snapraid_link* slink;
2462 : struct snapraid_disk* disk;
2463 : uint32_t mapping;
2464 :
2465 123738 : ret = sgetb32(f, &mapping);
2466 123738 : if (ret < 0 || mapping >= mapping_max) {
2467 : /* LCOV_EXCL_START */
2468 : decoding_error(path, f);
2469 : log_fatal(EINTERNAL, "Internal inconsistency: Symlink mapping index out of range\n");
2470 : os_abort();
2471 : /* LCOV_EXCL_STOP */
2472 : }
2473 123738 : disk = tommy_array_get(&disk_mapping, mapping);
2474 :
2475 123738 : ret = sgetbs(f, sub, sizeof(sub));
2476 123738 : if (ret < 0) {
2477 : /* LCOV_EXCL_START */
2478 : decoding_error(path, f);
2479 : os_abort();
2480 : /* LCOV_EXCL_STOP */
2481 : }
2482 :
2483 123738 : if (!*sub) {
2484 : /* LCOV_EXCL_START */
2485 : decoding_error(path, f);
2486 : log_fatal(EINTERNAL, "Internal inconsistency: Null symlink!\n");
2487 : os_abort();
2488 : /* LCOV_EXCL_STOP */
2489 : }
2490 :
2491 123738 : ret = sgetbs(f, linkto, sizeof(linkto));
2492 123738 : if (ret < 0) {
2493 : /* LCOV_EXCL_START */
2494 : decoding_error(path, f);
2495 : os_abort();
2496 : /* LCOV_EXCL_STOP */
2497 : }
2498 :
2499 : /* allocate the link as symbolic link */
2500 123738 : slink = link_alloc(sub, linkto, FILE_IS_SYMLINK);
2501 :
2502 : /* insert the link in the link containers */
2503 123738 : tommy_hashdyn_insert(&disk->linkset, &slink->nodeset, slink, link_name_hash(slink->sub));
2504 123738 : tommy_list_insert_tail(&disk->linklist, &slink->nodelist, slink);
2505 :
2506 : /* stat */
2507 123738 : ++count_symlink;
2508 5912 : } else if (c == 'a') {
2509 : /* hardlink */
2510 : char sub[PATH_MAX];
2511 : char linkto[PATH_MAX];
2512 : struct snapraid_link* slink;
2513 : struct snapraid_disk* disk;
2514 : uint32_t mapping;
2515 :
2516 651 : ret = sgetb32(f, &mapping);
2517 651 : if (ret < 0 || mapping >= mapping_max) {
2518 : /* LCOV_EXCL_START */
2519 : decoding_error(path, f);
2520 : log_fatal(EINTERNAL, "Internal inconsistency: Hardlink mapping index out of range!\n");
2521 : os_abort();
2522 : /* LCOV_EXCL_STOP */
2523 : }
2524 651 : disk = tommy_array_get(&disk_mapping, mapping);
2525 :
2526 651 : ret = sgetbs(f, sub, sizeof(sub));
2527 651 : if (ret < 0) {
2528 : /* LCOV_EXCL_START */
2529 : decoding_error(path, f);
2530 : os_abort();
2531 : /* LCOV_EXCL_STOP */
2532 : }
2533 :
2534 651 : if (!*sub) {
2535 : /* LCOV_EXCL_START */
2536 : decoding_error(path, f);
2537 : log_fatal(EINTERNAL, "Internal inconsistency: Null hardlink!\n");
2538 : os_abort();
2539 : /* LCOV_EXCL_STOP */
2540 : }
2541 :
2542 651 : ret = sgetbs(f, linkto, sizeof(linkto));
2543 651 : if (ret < 0) {
2544 : /* LCOV_EXCL_START */
2545 : decoding_error(path, f);
2546 : os_abort();
2547 : /* LCOV_EXCL_STOP */
2548 : }
2549 :
2550 651 : if (!*linkto) {
2551 : /* LCOV_EXCL_START */
2552 : decoding_error(path, f);
2553 : log_fatal(EINTERNAL, "Internal inconsistency: Empty hardlink '%s'!\n", sub);
2554 : os_abort();
2555 : /* LCOV_EXCL_STOP */
2556 : }
2557 :
2558 : /* allocate the link as hard link */
2559 651 : slink = link_alloc(sub, linkto, FILE_IS_HARDLINK);
2560 :
2561 : /* insert the link in the link containers */
2562 651 : tommy_hashdyn_insert(&disk->linkset, &slink->nodeset, slink, link_name_hash(slink->sub));
2563 651 : tommy_list_insert_tail(&disk->linklist, &slink->nodelist, slink);
2564 :
2565 : /* stat */
2566 651 : ++count_hardlink;
2567 5261 : } else if (c == 'r') {
2568 : /* dir */
2569 : char sub[PATH_MAX];
2570 : struct snapraid_dir* dir;
2571 : struct snapraid_disk* disk;
2572 : uint32_t mapping;
2573 :
2574 862 : ret = sgetb32(f, &mapping);
2575 862 : if (ret < 0 || mapping >= mapping_max) {
2576 : /* LCOV_EXCL_START */
2577 : decoding_error(path, f);
2578 : log_fatal(EINTERNAL, "Internal inconsistency: Dir mapping index ouf of range!\n");
2579 : os_abort();
2580 : /* LCOV_EXCL_STOP */
2581 : }
2582 862 : disk = tommy_array_get(&disk_mapping, mapping);
2583 :
2584 862 : ret = sgetbs(f, sub, sizeof(sub));
2585 862 : if (ret < 0) {
2586 : /* LCOV_EXCL_START */
2587 : decoding_error(path, f);
2588 : os_abort();
2589 : /* LCOV_EXCL_STOP */
2590 : }
2591 :
2592 862 : if (!*sub) {
2593 : /* LCOV_EXCL_START */
2594 : decoding_error(path, f);
2595 : log_fatal(EINTERNAL, "Internal inconsistency: Null dir!\n");
2596 : os_abort();
2597 : /* LCOV_EXCL_STOP */
2598 : }
2599 :
2600 : /* allocate the dir */
2601 862 : dir = dir_alloc(sub);
2602 :
2603 : /* insert the dir in the dir containers */
2604 862 : tommy_hashdyn_insert(&disk->dirset, &dir->nodeset, dir, dir_name_hash(dir->sub));
2605 862 : tommy_list_insert_tail(&disk->dirlist, &dir->nodelist, dir);
2606 :
2607 : /* stat */
2608 862 : ++count_dir;
2609 4399 : } else if (c == 'c') {
2610 : /* get the subcommand */
2611 273 : c = sgetc(f);
2612 :
2613 273 : switch (c) {
2614 158 : case 'u' :
2615 158 : state->hash = HASH_MURMUR3;
2616 158 : break;
2617 115 : case 'k' :
2618 115 : state->hash = HASH_SPOOKY2;
2619 115 : break;
2620 0 : case 'm' :
2621 0 : state->hash = HASH_METRO;
2622 0 : break;
2623 0 : default :
2624 : /* LCOV_EXCL_START */
2625 : decoding_error(path, f);
2626 : log_fatal(ECONTENT, "Invalid checksum!\n");
2627 : os_abort();
2628 : /* LCOV_EXCL_STOP */
2629 : }
2630 :
2631 : /* read the seed */
2632 273 : ret = sread(f, state->hashseed, HASH_MAX);
2633 273 : if (ret < 0) {
2634 : /* LCOV_EXCL_START */
2635 : decoding_error(path, f);
2636 : os_abort();
2637 : /* LCOV_EXCL_STOP */
2638 : }
2639 4126 : } else if (c == 'C') {
2640 : /* get the sub-command */
2641 4 : c = sgetc(f);
2642 :
2643 4 : switch (c) {
2644 4 : case 'u' :
2645 4 : state->prevhash = HASH_MURMUR3;
2646 4 : break;
2647 0 : case 'k' :
2648 0 : state->prevhash = HASH_SPOOKY2;
2649 0 : break;
2650 0 : case 'm' :
2651 0 : state->prevhash = HASH_METRO;
2652 0 : break;
2653 0 : default :
2654 : /* LCOV_EXCL_START */
2655 : decoding_error(path, f);
2656 : log_fatal(ECONTENT, "Invalid checksum!\n");
2657 : os_abort();
2658 : /* LCOV_EXCL_STOP */
2659 : }
2660 :
2661 : /* read the seed */
2662 4 : ret = sread(f, state->prevhashseed, HASH_MAX);
2663 4 : if (ret < 0) {
2664 : /* LCOV_EXCL_START */
2665 : decoding_error(path, f);
2666 : os_abort();
2667 : /* LCOV_EXCL_STOP */
2668 : }
2669 4122 : } else if (c == 'z') {
2670 : uint32_t block_size;
2671 :
2672 273 : ret = sgetb32(f, &block_size);
2673 273 : if (ret < 0) {
2674 : /* LCOV_EXCL_START */
2675 : decoding_error(path, f);
2676 : os_abort();
2677 : /* LCOV_EXCL_STOP */
2678 : }
2679 :
2680 273 : if (block_size == 0) {
2681 : /* LCOV_EXCL_START */
2682 : decoding_error(path, f);
2683 : log_fatal(ECONTENT, "Zero 'blocksize' specification in the content file!\n");
2684 : exit(EXIT_FAILURE);
2685 : /* LCOV_EXCL_STOP */
2686 : }
2687 :
2688 : /* without configuration, auto assign the block size */
2689 273 : if (state->no_conf) {
2690 1 : state->block_size = block_size;
2691 : }
2692 :
2693 273 : if (block_size != state->block_size) {
2694 : /* LCOV_EXCL_START */
2695 : decoding_error(path, f);
2696 : log_fatal(EUSER, "Mismatching 'blocksize' specification in the content file!\n");
2697 : log_fatal(EUSER, "Please restore the 'blocksize' value in the configuration file to '%u'\n", block_size / KIBI);
2698 : exit(EXIT_FAILURE);
2699 : /* LCOV_EXCL_STOP */
2700 : }
2701 3849 : } else if (c == 'y') {
2702 : uint32_t hash_size;
2703 :
2704 273 : ret = sgetb32(f, &hash_size);
2705 273 : if (ret < 0) {
2706 : /* LCOV_EXCL_START */
2707 : decoding_error(path, f);
2708 : os_abort();
2709 : /* LCOV_EXCL_STOP */
2710 : }
2711 :
2712 273 : if (hash_size < 2 || hash_size > HASH_MAX) {
2713 : /* LCOV_EXCL_START */
2714 : decoding_error(path, f);
2715 : log_fatal(ECONTENT, "Invalid 'hashsize' specification in the content file!\n");
2716 : exit(EXIT_FAILURE);
2717 : /* LCOV_EXCL_STOP */
2718 : }
2719 :
2720 : /* without configuration, auto assign the block size */
2721 273 : if (state->no_conf) {
2722 1 : BLOCK_HASH_SIZE = hash_size;
2723 : }
2724 :
2725 273 : if ((int)hash_size != BLOCK_HASH_SIZE) {
2726 : /* LCOV_EXCL_START */
2727 : decoding_error(path, f);
2728 : log_fatal(EUSER, "Mismatching 'hashsize' specification in the content file!\n");
2729 : log_fatal(EUSER, "Please restore the 'hashsize' value in the configuration file to '%u'\n", hash_size);
2730 : exit(EXIT_FAILURE);
2731 : /* LCOV_EXCL_STOP */
2732 : }
2733 3576 : } else if (c == 'x') {
2734 273 : ret = sgetb32(f, &blockmax);
2735 273 : if (ret < 0) {
2736 : /* LCOV_EXCL_START */
2737 : decoding_error(path, f);
2738 : os_abort();
2739 : /* LCOV_EXCL_STOP */
2740 : }
2741 3303 : } else if (c == 'm' || c == 'M') {
2742 : struct snapraid_map* map;
2743 : char uuid[UUID_MAX];
2744 : uint32_t v_pos;
2745 : uint32_t v_total_blocks;
2746 : uint32_t v_free_blocks;
2747 : struct snapraid_disk* disk;
2748 :
2749 1517 : ret = sgetbs(f, buffer, sizeof(buffer));
2750 1517 : if (ret < 0) {
2751 : /* LCOV_EXCL_START */
2752 : decoding_error(path, f);
2753 : os_abort();
2754 : /* LCOV_EXCL_STOP */
2755 : }
2756 :
2757 1517 : ret = sgetb32(f, &v_pos);
2758 1517 : if (ret < 0) {
2759 : /* LCOV_EXCL_START */
2760 : decoding_error(path, f);
2761 : os_abort();
2762 : /* LCOV_EXCL_STOP */
2763 : }
2764 :
2765 : /* from SnapRAID 7.0 the 'M' command includes the free space */
2766 1517 : if (c == 'M') {
2767 1517 : ret = sgetb32(f, &v_total_blocks);
2768 1517 : if (ret < 0) {
2769 : /* LCOV_EXCL_START */
2770 : decoding_error(path, f);
2771 : os_abort();
2772 : /* LCOV_EXCL_STOP */
2773 : }
2774 :
2775 1517 : ret = sgetb32(f, &v_free_blocks);
2776 1517 : if (ret < 0) {
2777 : /* LCOV_EXCL_START */
2778 : decoding_error(path, f);
2779 : os_abort();
2780 : /* LCOV_EXCL_STOP */
2781 : }
2782 :
2783 1517 : log_tag("content_data:%s:%" PRIi64 ":%" PRIi64 "\n",
2784 : buffer,
2785 1517 : v_total_blocks * (uint64_t)state->block_size,
2786 1517 : v_free_blocks * (uint64_t)state->block_size);
2787 : } else {
2788 0 : v_total_blocks = 0;
2789 0 : v_free_blocks = 0;
2790 : }
2791 :
2792 : /* read the uuid */
2793 1517 : ret = sgetbs(f, uuid, sizeof(uuid));
2794 1517 : if (ret < 0) {
2795 : /* LCOV_EXCL_START */
2796 : decoding_error(path, f);
2797 : os_abort();
2798 : /* LCOV_EXCL_STOP */
2799 : }
2800 :
2801 1517 : log_tag("content_data_split:%s:%s\n",
2802 : buffer,
2803 : uuid);
2804 :
2805 : /* find the disk */
2806 1517 : disk = find_disk_by_name(state, buffer);
2807 1517 : if (!disk) {
2808 : /* search by UUID if renamed */
2809 2 : disk = find_disk_by_uuid(state, uuid);
2810 2 : if (disk) {
2811 2 : log_fatal(EUSER, "WARNING! Renaming disk '%s' to '%s'\n", buffer, disk->name);
2812 :
2813 : /* write the new state with the new name */
2814 2 : state->need_write = 1;
2815 : }
2816 : }
2817 1517 : if (!disk) {
2818 : /* LCOV_EXCL_START */
2819 : decoding_error(path, f);
2820 : log_fatal(EUSER, "Disk '%s' with uuid '%s' not present in the configuration file!\n", buffer, uuid);
2821 : log_fatal(EUSER, "If you have removed it from the configuration file, please restore it\n");
2822 : /* if it's a command without UUID, it cannot autorename using UUID */
2823 : if (state->opt.skip_disk_access)
2824 : log_fatal(EUSER, "If you have renamed it, run 'sync' to update the new name\n");
2825 : exit(EXIT_FAILURE);
2826 : /* LCOV_EXCL_STOP */
2827 : }
2828 :
2829 1517 : map = map_alloc(disk->name, v_pos, v_total_blocks, v_free_blocks, uuid);
2830 :
2831 1517 : tommy_list_insert_tail(&state->maplist, &map->node, map);
2832 :
2833 : /* insert in the mapping vector */
2834 1517 : tommy_array_grow(&disk_mapping, mapping_max + 1);
2835 1517 : tommy_array_set(&disk_mapping, mapping_max, disk);
2836 1517 : ++mapping_max;
2837 1786 : } else if (c == 'P') {
2838 : /* from SnapRAID 7.0 the 'P' command includes the free space */
2839 : /* from SnapRAID 11.0 the 'P' command is deprecated by 'Q' */
2840 : char v_uuid[UUID_MAX];
2841 : uint32_t v_level;
2842 : uint32_t v_total_blocks;
2843 : uint32_t v_free_blocks;
2844 :
2845 0 : ret = sgetb32(f, &v_level);
2846 0 : if (ret < 0) {
2847 : /* LCOV_EXCL_START */
2848 : decoding_error(path, f);
2849 : os_abort();
2850 : /* LCOV_EXCL_STOP */
2851 : }
2852 :
2853 0 : ret = sgetb32(f, &v_total_blocks);
2854 0 : if (ret < 0) {
2855 : /* LCOV_EXCL_START */
2856 : decoding_error(path, f);
2857 : os_abort();
2858 : /* LCOV_EXCL_STOP */
2859 : }
2860 :
2861 0 : ret = sgetb32(f, &v_free_blocks);
2862 0 : if (ret < 0) {
2863 : /* LCOV_EXCL_START */
2864 : decoding_error(path, f);
2865 : os_abort();
2866 : /* LCOV_EXCL_STOP */
2867 : }
2868 :
2869 0 : ret = sgetbs(f, v_uuid, sizeof(v_uuid));
2870 0 : if (ret < 0) {
2871 : /* LCOV_EXCL_START */
2872 : decoding_error(path, f);
2873 : os_abort();
2874 : /* LCOV_EXCL_STOP */
2875 : }
2876 :
2877 0 : if (v_level >= LEV_MAX) {
2878 : /* LCOV_EXCL_START */
2879 : decoding_error(path, f);
2880 : log_fatal(ECONTENT, "Invalid parity level '%u' in the configuration file!\n", v_level);
2881 : exit(EXIT_FAILURE);
2882 : /* LCOV_EXCL_STOP */
2883 : }
2884 :
2885 0 : log_tag("content_parity:%s:%" PRIi64 ":%" PRIi64 "\n",
2886 : lev_config_name(v_level),
2887 0 : v_total_blocks * (uint64_t)state->block_size,
2888 0 : v_free_blocks * (uint64_t)state->block_size);
2889 :
2890 : /* auto configure if configuration is missing */
2891 0 : if (state->no_conf) {
2892 0 : if (v_level >= state->level)
2893 0 : state->level = v_level + 1;
2894 : }
2895 :
2896 : /* if we use this parity entry */
2897 0 : if (v_level < state->level) {
2898 : /* if the configuration has more splits, keep them */
2899 0 : if (state->parity[v_level].split_mac < 1)
2900 0 : state->parity[v_level].split_mac = 1;
2901 : /* set the parity info */
2902 0 : pathcpy(state->parity[v_level].split_map[0].uuid, sizeof(state->parity[v_level].split_map[0].uuid), v_uuid);
2903 0 : state->parity[v_level].total_blocks = v_total_blocks;
2904 0 : state->parity[v_level].free_blocks = v_free_blocks;
2905 :
2906 0 : log_tag("content_parity_split:%s:%s:%s:%" PRIi64 "\n",
2907 : lev_config_name(v_level), /* P command always has a single split */
2908 0 : state->parity[v_level].split_map[0].uuid,
2909 0 : state->parity[v_level].split_map[0].path,
2910 : state->parity[v_level].split_map[0].size);
2911 : }
2912 1786 : } else if (c == 'Q') {
2913 : /* from SnapRAID 11.0 the 'Q' command include size info and multi file support */
2914 : uint32_t v_level;
2915 : uint32_t v_total_blocks;
2916 : uint32_t v_free_blocks;
2917 : uint32_t v_split_mac;
2918 : unsigned s;
2919 :
2920 1513 : ret = sgetb32(f, &v_level);
2921 1513 : if (ret < 0) {
2922 : /* LCOV_EXCL_START */
2923 : decoding_error(path, f);
2924 : os_abort();
2925 : /* LCOV_EXCL_STOP */
2926 : }
2927 :
2928 1513 : ret = sgetb32(f, &v_total_blocks);
2929 1513 : if (ret < 0) {
2930 : /* LCOV_EXCL_START */
2931 : decoding_error(path, f);
2932 : os_abort();
2933 : /* LCOV_EXCL_STOP */
2934 : }
2935 :
2936 1513 : ret = sgetb32(f, &v_free_blocks);
2937 1513 : if (ret < 0) {
2938 : /* LCOV_EXCL_START */
2939 : decoding_error(path, f);
2940 : os_abort();
2941 : /* LCOV_EXCL_STOP */
2942 : }
2943 :
2944 1513 : ret = sgetb32(f, &v_split_mac);
2945 1513 : if (ret < 0) {
2946 : /* LCOV_EXCL_START */
2947 : decoding_error(path, f);
2948 : os_abort();
2949 : /* LCOV_EXCL_STOP */
2950 : }
2951 :
2952 1513 : if (v_level >= LEV_MAX) {
2953 : /* LCOV_EXCL_START */
2954 : decoding_error(path, f);
2955 : log_fatal(ECONTENT, "Invalid parity level '%u' in the configuration file!\n", v_level);
2956 : exit(EXIT_FAILURE);
2957 : /* LCOV_EXCL_STOP */
2958 : }
2959 :
2960 : /* auto configure if configuration is missing */
2961 1513 : if (state->no_conf) {
2962 6 : if (v_level >= state->level)
2963 5 : state->level = v_level + 1;
2964 6 : if (state->parity[v_level].split_mac < v_split_mac)
2965 6 : state->parity[v_level].split_mac = v_split_mac;
2966 : }
2967 :
2968 : /* if we use this parity entry */
2969 1513 : if (v_level < state->level) {
2970 : /* set the parity info */
2971 1245 : state->parity[v_level].total_blocks = v_total_blocks;
2972 1245 : state->parity[v_level].free_blocks = v_free_blocks;
2973 : }
2974 :
2975 1513 : log_tag("content_parity:%s:%" PRIi64 ":%" PRIi64 "\n",
2976 : lev_config_name(v_level),
2977 1513 : v_total_blocks * (uint64_t)state->block_size,
2978 1513 : v_free_blocks * (uint64_t)state->block_size);
2979 :
2980 7565 : for (s = 0; s < v_split_mac; ++s) {
2981 : char v_path[PATH_MAX];
2982 : char v_uuid[UUID_MAX];
2983 : uint64_t v_size;
2984 :
2985 6052 : ret = sgetbs(f, v_path, sizeof(v_path));
2986 6052 : if (ret < 0) {
2987 : /* LCOV_EXCL_START */
2988 : decoding_error(path, f);
2989 : os_abort();
2990 : /* LCOV_EXCL_STOP */
2991 : }
2992 :
2993 6052 : ret = sgetbs(f, v_uuid, sizeof(v_uuid));
2994 6052 : if (ret < 0) {
2995 : /* LCOV_EXCL_START */
2996 : decoding_error(path, f);
2997 : os_abort();
2998 : /* LCOV_EXCL_STOP */
2999 : }
3000 :
3001 6052 : ret = sgetb64(f, &v_size);
3002 6052 : if (ret < 0) {
3003 : /* LCOV_EXCL_START */
3004 : decoding_error(path, f);
3005 : os_abort();
3006 : /* LCOV_EXCL_STOP */
3007 : }
3008 :
3009 : /* if we use this parity entry */
3010 6052 : if (v_level < state->level) {
3011 : /* if this split was removed from the configuration */
3012 4980 : if (s >= state->parity[v_level].split_mac) {
3013 : /* if the file is used, we really need it */
3014 0 : if (v_size != 0) {
3015 : /* LCOV_EXCL_START */
3016 : decoding_error(path, f);
3017 : log_fatal(EUSER, "Parity '%s' misses used file '%u'!\n", lev_config_name(v_level), s);
3018 : log_fatal(EUSER, "If you have removed it from the configuration file, please restore it\n");
3019 : exit(EXIT_FAILURE);
3020 : /* LCOV_EXCL_STOP */
3021 : }
3022 :
3023 : /* otherwise we can drop it */
3024 0 : log_fatal(EUSER, "WARNING! Dropping from '%s' unused split '%u'\n", lev_config_name(v_level), s);
3025 : } else {
3026 : char parity_name[64];
3027 :
3028 : /* we copy the path only if without configuration file */
3029 4980 : if (state->no_conf)
3030 24 : pathcpy(state->parity[v_level].split_map[s].path, sizeof(state->parity[v_level].split_map[s].path), v_path);
3031 :
3032 : /* set the split info */
3033 4980 : pathcpy(state->parity[v_level].split_map[s].uuid, sizeof(state->parity[v_level].split_map[s].uuid), v_uuid);
3034 4980 : state->parity[v_level].split_map[s].size = v_size;
3035 :
3036 4980 : if (s == 0)
3037 1245 : pathcpy(parity_name, sizeof(parity_name), lev_config_name(v_level));
3038 : else
3039 3735 : pathprint(parity_name, sizeof(parity_name), "%s/%u", lev_config_name(v_level), s);
3040 4980 : log_tag("content_parity_split:%s:%s:%s:%" PRIi64 "\n",
3041 : parity_name,
3042 4980 : state->parity[v_level].split_map[s].uuid,
3043 4980 : state->parity[v_level].split_map[s].path,
3044 : state->parity[v_level].split_map[s].size);
3045 : }
3046 : }
3047 : }
3048 273 : } else if (c == 'N') {
3049 : uint32_t crc_stored;
3050 : uint32_t crc_computed;
3051 :
3052 : /* get the crc before reading it from the file */
3053 272 : crc_computed = scrc(f);
3054 :
3055 272 : ret = sgetble32(f, &crc_stored);
3056 272 : if (ret < 0) {
3057 : /* LCOV_EXCL_START */
3058 : /* here don't call decoding_error() because it's too late to get the crc */
3059 : log_fatal(errno, "Error reading the CRC in '%s' at offset %" PRIi64 "\n", path, stell(f));
3060 : log_fatal(errno, "This content file is damaged! Use an alternate copy.\n");
3061 : exit(EXIT_FAILURE);
3062 : /* LCOV_EXCL_STOP */
3063 : }
3064 :
3065 272 : if (crc_stored != crc_computed) {
3066 : /* LCOV_EXCL_START */
3067 : /* here don't call decoding_error() because it's too late to get the crc */
3068 : log_fatal(ECONTENT, "CRC mismatch in '%s'\n", path);
3069 : log_fatal(ECONTENT, "The content file is damaged! Please use an alternate copy.\n");
3070 : exit(EXIT_FAILURE);
3071 : /* LCOV_EXCL_STOP */
3072 : }
3073 :
3074 272 : crc_checked = 1;
3075 : } else {
3076 : /* LCOV_EXCL_START */
3077 : decoding_error(path, f);
3078 : log_fatal(ECONTENT, "Invalid command '%c'!\n", (char)c);
3079 : os_abort();
3080 : /* LCOV_EXCL_STOP */
3081 : }
3082 : }
3083 :
3084 272 : tommy_array_done(&disk_mapping);
3085 :
3086 272 : if (serror(f)) {
3087 : /* LCOV_EXCL_START */
3088 : log_fatal(errno, "Error reading the content file '%s' at offset %" PRIi64 "\n", path, stell(f));
3089 : exit(EXIT_FAILURE);
3090 : /* LCOV_EXCL_STOP */
3091 : }
3092 :
3093 272 : if (!crc_checked) {
3094 : /* LCOV_EXCL_START */
3095 : log_fatal(ECONTENT, "Reached the end of '%s' without finding the expected CRC\n", path);
3096 : log_fatal(ECONTENT, "This content file is truncated or damaged! Use an alternate copy.\n");
3097 : exit(EXIT_FAILURE);
3098 : /* LCOV_EXCL_STOP */
3099 : }
3100 :
3101 : /* check the file-system on all disks */
3102 272 : state_fscheck(state, "after read");
3103 :
3104 : /* check that the stored parity size matches the loaded state */
3105 272 : if (blockmax != parity_allocated_size(state)) {
3106 : /* LCOV_EXCL_START */
3107 : log_fatal(EINTERNAL, "Internal inconsistency: Parity size %u/%u in '%s' at offset %" PRIi64 "\n", blockmax, parity_allocated_size(state), path, stell(f));
3108 : if (state->opt.skip_content_check) {
3109 : log_fatal(ECONTENT, "Overriding.\n");
3110 : blockmax = parity_allocated_size(state);
3111 : } else {
3112 : exit(EXIT_FAILURE);
3113 : }
3114 : /* LCOV_EXCL_STOP */
3115 : }
3116 :
3117 272 : msg_verbose("%8u files\n", count_file);
3118 272 : msg_verbose("%8u hardlinks\n", count_hardlink);
3119 272 : msg_verbose("%8u symlinks\n", count_symlink);
3120 272 : msg_verbose("%8u empty dirs\n", count_dir);
3121 :
3122 272 : log_tag("content_info:file:%u\n", count_file);
3123 272 : log_tag("content_info:hardlink:%u\n", count_hardlink);
3124 272 : log_tag("content_info:symlink:%u\n", count_symlink);
3125 272 : log_tag("content_info:dir_empty:%u\n", count_dir);
3126 :
3127 272 : log_tag("content_info:block:%u\n", blockmax);
3128 272 : log_tag("content_info:block_bad:%u\n", count_bad);
3129 272 : log_tag("content_info:block_rehash:%u\n", count_rehash);
3130 272 : log_tag("content_info:block_unsynced:%u\n", count_unsynced);
3131 272 : log_tag("content_info:block_unscrubbed:%u\n", count_unscrubbed);
3132 :
3133 : /* store the blocks counters */
3134 272 : state->rehash_blocks = count_rehash;
3135 272 : state->bad_blocks = count_bad;
3136 272 : state->unsynced_blocks = count_unsynced;
3137 272 : state->unscrubbed_blocks = count_unscrubbed;
3138 :
3139 : /* store the bucket info list */
3140 272 : bucket_to_list(&bucket_hash, &state->bucketlist, &state->bucketcount);
3141 272 : tommy_hashdyn_done(&bucket_hash);
3142 272 : }
3143 :
3144 : struct state_write_thread_context {
3145 : struct snapraid_state* state;
3146 : #if HAVE_MT_WRITE
3147 : thread_id_t thread;
3148 : #endif
3149 : /* input */
3150 : block_off_t blockmax;
3151 : time_t info_oldest;
3152 : time_t info_now;
3153 : int info_has_rehash;
3154 : STREAM* f;
3155 : int first;
3156 :
3157 : /* output (required to postpone the output to the terminal after the latest write) */
3158 : uint32_t crc;
3159 : unsigned count_file;
3160 : unsigned count_hardlink;
3161 : unsigned count_symlink;
3162 : unsigned count_dir;
3163 : unsigned count_bad;
3164 : unsigned count_rehash;
3165 : unsigned count_unsynced;
3166 : unsigned count_unscrubbed;
3167 : };
3168 :
3169 162 : static void* state_write_thread(void* arg)
3170 : {
3171 162 : struct state_write_thread_context* context = arg;
3172 162 : struct snapraid_state* state = context->state;
3173 162 : block_off_t blockmax = context->blockmax;
3174 162 : time_t info_oldest = context->info_oldest;
3175 162 : time_t info_now = context->info_now;
3176 162 : int info_has_rehash = context->info_has_rehash;
3177 162 : STREAM* f = context->f;
3178 : uint32_t crc;
3179 : uint64_t t64;
3180 : unsigned count_file;
3181 : unsigned count_hardlink;
3182 : unsigned count_symlink;
3183 : unsigned count_dir;
3184 : unsigned count_bad;
3185 : unsigned count_rehash;
3186 : unsigned count_unsynced;
3187 : unsigned count_unscrubbed;
3188 : tommy_node* i;
3189 : block_off_t idx;
3190 : block_off_t begin;
3191 : unsigned l, s;
3192 : tommy_hashdyn bucket_hash;
3193 :
3194 162 : count_file = 0;
3195 162 : count_hardlink = 0;
3196 162 : count_symlink = 0;
3197 162 : count_dir = 0;
3198 162 : count_bad = 0;
3199 162 : count_rehash = 0;
3200 162 : count_unsynced = 0;
3201 162 : count_unscrubbed = 0;
3202 162 : tommy_hashdyn_init(&bucket_hash);
3203 :
3204 : /* force version 3 a we want to always store the parity size */
3205 : /* write header */
3206 162 : swrite("SNAPCNT3\n\3\0\0", 12, f);
3207 :
3208 : /* write block size and block max */
3209 162 : sputc('z', f);
3210 162 : sputb32(state->block_size, f);
3211 162 : sputc('x', f);
3212 162 : sputb32(blockmax, f);
3213 :
3214 : /* hash size */
3215 162 : sputc('y', f);
3216 162 : sputb32(BLOCK_HASH_SIZE, f);
3217 :
3218 162 : if (serror(f)) {
3219 : /* LCOV_EXCL_START */
3220 : log_fatal(errno, "Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
3221 : goto bail;
3222 : /* LCOV_EXCL_STOP */
3223 : }
3224 :
3225 162 : sputc('c', f);
3226 162 : if (state->hash == HASH_MURMUR3) {
3227 107 : sputc('u', f);
3228 55 : } else if (state->hash == HASH_SPOOKY2) {
3229 55 : sputc('k', f);
3230 0 : } else if (state->hash == HASH_METRO) {
3231 0 : sputc('m', f);
3232 : } else {
3233 : /* LCOV_EXCL_START */
3234 : log_fatal(EINTERNAL, "Unexpected hash when writing the content file '%s'.\n", serrorfile(f));
3235 : goto bail;
3236 : /* LCOV_EXCL_STOP */
3237 : }
3238 162 : swrite(state->hashseed, HASH_MAX, f);
3239 162 : if (serror(f)) {
3240 : /* LCOV_EXCL_START */
3241 : log_fatal(errno, "Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
3242 : goto bail;
3243 : /* LCOV_EXCL_STOP */
3244 : }
3245 :
3246 : /* previous hash only present */
3247 162 : if (state->prevhash != HASH_UNDEFINED) {
3248 : /* if at least one rehash tag found, we have to save the previous hash */
3249 3 : if (info_has_rehash) {
3250 2 : sputc('C', f);
3251 2 : if (state->prevhash == HASH_MURMUR3) {
3252 2 : sputc('u', f);
3253 0 : } else if (state->prevhash == HASH_SPOOKY2) {
3254 0 : sputc('k', f);
3255 0 : } else if (state->prevhash == HASH_METRO) {
3256 0 : sputc('m', f);
3257 : } else {
3258 : /* LCOV_EXCL_START */
3259 : log_fatal(EINTERNAL, "Unexpected prevhash when writing the content file '%s'.\n", serrorfile(f));
3260 : goto bail;
3261 : /* LCOV_EXCL_STOP */
3262 : }
3263 2 : swrite(state->prevhashseed, HASH_MAX, f);
3264 2 : if (serror(f)) {
3265 : /* LCOV_EXCL_START */
3266 : log_fatal(errno, "Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
3267 : goto bail;
3268 : /* LCOV_EXCL_STOP */
3269 : }
3270 : }
3271 : }
3272 :
3273 : /* for each map */
3274 1134 : for (i = state->maplist; i != 0; i = i->next) {
3275 972 : struct snapraid_map* map = i->data;
3276 : struct snapraid_disk* disk;
3277 :
3278 : /* find the disk for this mapping */
3279 972 : disk = find_disk_by_name(state, map->name);
3280 972 : if (!disk) {
3281 : /* LCOV_EXCL_START */
3282 : log_fatal(EINTERNAL, "Internal inconsistency: Unmapped disk '%s'\n", map->name);
3283 : goto bail;
3284 : /* LCOV_EXCL_STOP */
3285 : }
3286 :
3287 : /* save the mapping only if disk is mapped */
3288 972 : if (disk->mapping_idx != -1) {
3289 880 : sputc('M', f);
3290 880 : sputbs(map->name, f);
3291 880 : sputb32(map->position, f);
3292 880 : sputb32(map->total_blocks, f);
3293 880 : sputb32(map->free_blocks, f);
3294 880 : sputbs(map->uuid, f);
3295 880 : if (context->first) {
3296 880 : log_tag("content_data:%s:%" PRIi64 ":%" PRIi64 "\n",
3297 880 : map->name,
3298 880 : map->total_blocks * (uint64_t)state->block_size,
3299 880 : map->free_blocks * (uint64_t)state->block_size);
3300 880 : log_tag("content_data_split:%s:%s\n",
3301 880 : map->name,
3302 880 : map->uuid);
3303 : }
3304 880 : if (serror(f)) {
3305 : /* LCOV_EXCL_START */
3306 : log_fatal(errno, "Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
3307 : goto bail;
3308 : /* LCOV_EXCL_STOP */
3309 : }
3310 : }
3311 : }
3312 :
3313 : /* for each parity */
3314 1079 : for (l = 0; l < state->level; ++l) {
3315 917 : sputc('Q', f);
3316 917 : sputb32(l, f);
3317 917 : sputb32(state->parity[l].total_blocks, f);
3318 917 : sputb32(state->parity[l].free_blocks, f);
3319 917 : if (context->first) {
3320 917 : log_tag("content_parity:%s:%" PRIi64 ":%" PRIi64 "\n",
3321 : lev_config_name(l),
3322 917 : state->parity[l].total_blocks * (uint64_t)state->block_size,
3323 917 : state->parity[l].free_blocks * (uint64_t)state->block_size);
3324 : }
3325 917 : sputb32(state->parity[l].split_mac, f);
3326 4585 : for (s = 0; s < state->parity[l].split_mac; ++s) {
3327 : char parity_name[64];
3328 3668 : sputbs(state->parity[l].split_map[s].path, f);
3329 3668 : sputbs(state->parity[l].split_map[s].uuid, f);
3330 3668 : sputb64(state->parity[l].split_map[s].size, f);
3331 3668 : if (s == 0)
3332 917 : pathcpy(parity_name, sizeof(parity_name), lev_config_name(l));
3333 : else
3334 2751 : pathprint(parity_name, sizeof(parity_name), "%s/%u", lev_config_name(l), s);
3335 3668 : if (context->first) {
3336 3668 : log_tag("content_parity_split:%s:%s:%s:%" PRIi64 "\n",
3337 : parity_name,
3338 3668 : state->parity[l].split_map[s].uuid,
3339 3668 : state->parity[l].split_map[s].path,
3340 : state->parity[l].split_map[s].size);
3341 : }
3342 : }
3343 917 : if (serror(f)) {
3344 : /* LCOV_EXCL_START */
3345 : log_fatal(errno, "Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
3346 : goto bail;
3347 : /* LCOV_EXCL_STOP */
3348 : }
3349 : }
3350 :
3351 : /* for each disk */
3352 1134 : for (i = state->disklist; i != 0; i = i->next) {
3353 : tommy_node* j;
3354 972 : struct snapraid_disk* disk = i->data;
3355 :
3356 : /* if the disk is not mapped, skip it */
3357 972 : if (disk->mapping_idx < 0)
3358 92 : continue;
3359 :
3360 : /* for each file */
3361 1920787 : for (j = disk->filelist; j != 0; j = j->next) {
3362 1919907 : struct snapraid_file* file = j->data;
3363 : uint64_t size;
3364 : uint64_t mtime_sec;
3365 : int32_t mtime_nsec;
3366 : uint64_t inode;
3367 :
3368 1919907 : size = file->size;
3369 1919907 : mtime_sec = file->mtime_sec;
3370 1919907 : mtime_nsec = file->mtime_nsec;
3371 1919907 : inode = file->inode;
3372 :
3373 1919907 : sputc('f', f);
3374 1919907 : sputb32(disk->mapping_idx, f);
3375 1919907 : sputb64(size, f);
3376 1919907 : sputb64(mtime_sec, f);
3377 : /* encode STAT_NSEC_INVALID as 0 */
3378 1919907 : if (mtime_nsec == STAT_NSEC_INVALID)
3379 0 : sputb32(0, f);
3380 : else
3381 1919907 : sputb32(mtime_nsec + 1, f);
3382 1919907 : sputb64(inode, f);
3383 1919907 : sputbs(file->sub, f);
3384 1919907 : if (serror(f)) {
3385 : /* LCOV_EXCL_START */
3386 : log_fatal(errno, "Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
3387 : goto bail;
3388 : /* LCOV_EXCL_STOP */
3389 : }
3390 :
3391 : /* for all the blocks of the file */
3392 1919907 : begin = 0;
3393 3850675 : while (begin < file->blockmax) {
3394 1930768 : unsigned v_state = block_state_get(fs_file2block_get(file, begin));
3395 1930768 : block_off_t v_pos = fs_file2par_get(disk, file, begin);
3396 : uint32_t v_count;
3397 :
3398 : block_off_t end;
3399 :
3400 : /* find the end of run of blocks */
3401 1930768 : end = begin + 1;
3402 4743761 : while (end < file->blockmax) {
3403 2826549 : if (v_state != block_state_get(fs_file2block_get(file, end)))
3404 14 : break;
3405 2826535 : if (v_pos + (end - begin) != fs_file2par_get(disk, file, end))
3406 13542 : break;
3407 2812993 : ++end;
3408 : }
3409 :
3410 1930768 : switch (v_state) {
3411 1830481 : case BLOCK_STATE_BLK :
3412 1830481 : sputc('b', f);
3413 1830481 : break;
3414 66013 : case BLOCK_STATE_CHG :
3415 66013 : sputc('g', f);
3416 66013 : break;
3417 34274 : case BLOCK_STATE_REP :
3418 34274 : sputc('p', f);
3419 34274 : break;
3420 0 : default :
3421 : /* LCOV_EXCL_START */
3422 : log_fatal(EINTERNAL, "Internal inconsistency: State for block %u state %u\n", v_pos, v_state);
3423 : goto bail;
3424 : /* LCOV_EXCL_STOP */
3425 : }
3426 :
3427 1930768 : sputb32(v_pos, f);
3428 :
3429 1930768 : v_count = end - begin;
3430 1930768 : sputb32(v_count, f);
3431 :
3432 : /* write hashes */
3433 6674529 : for (idx = begin; idx < end; ++idx) {
3434 4743761 : struct snapraid_block* block = fs_file2block_get(file, idx);
3435 :
3436 4743761 : swrite(block->hash, BLOCK_HASH_SIZE, f);
3437 : }
3438 :
3439 1930768 : if (serror(f)) {
3440 : /* LCOV_EXCL_START */
3441 : log_fatal(errno, "Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
3442 : goto bail;
3443 : /* LCOV_EXCL_STOP */
3444 : }
3445 :
3446 : /* next begin position */
3447 1930768 : begin = end;
3448 : }
3449 :
3450 1919907 : ++count_file;
3451 : }
3452 :
3453 : /* for each link */
3454 63687 : for (j = disk->linklist; j != 0; j = j->next) {
3455 62807 : struct snapraid_link* slink = j->data;
3456 :
3457 62807 : switch (link_flag_get(slink, FILE_IS_LINK_MASK)) {
3458 357 : case FILE_IS_HARDLINK :
3459 357 : sputc('a', f);
3460 357 : ++count_hardlink;
3461 357 : break;
3462 62450 : case FILE_IS_SYMLINK :
3463 62450 : sputc('s', f);
3464 62450 : ++count_symlink;
3465 62450 : break;
3466 : }
3467 :
3468 62807 : sputb32(disk->mapping_idx, f);
3469 62807 : sputbs(slink->sub, f);
3470 62807 : sputbs(slink->linkto, f);
3471 62807 : if (serror(f)) {
3472 : /* LCOV_EXCL_START */
3473 : log_fatal(errno, "Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
3474 : goto bail;
3475 : /* LCOV_EXCL_STOP */
3476 : }
3477 : }
3478 :
3479 : /* for each dir */
3480 1355 : for (j = disk->dirlist; j != 0; j = j->next) {
3481 475 : struct snapraid_dir* dir = j->data;
3482 :
3483 475 : sputc('r', f);
3484 475 : sputb32(disk->mapping_idx, f);
3485 475 : sputbs(dir->sub, f);
3486 475 : if (serror(f)) {
3487 : /* LCOV_EXCL_START */
3488 : log_fatal(errno, "Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
3489 : goto bail;
3490 : /* LCOV_EXCL_STOP */
3491 : }
3492 :
3493 475 : ++count_dir;
3494 : }
3495 :
3496 : /* deleted blocks of the disk */
3497 880 : sputc('h', f);
3498 880 : sputb32(disk->mapping_idx, f);
3499 880 : if (serror(f)) {
3500 : /* LCOV_EXCL_START */
3501 : log_fatal(errno, "Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
3502 : goto bail;
3503 : /* LCOV_EXCL_STOP */
3504 : }
3505 880 : begin = 0;
3506 1841 : while (begin < blockmax) {
3507 : int is_deleted;
3508 : block_off_t end;
3509 :
3510 961 : is_deleted = fs_is_block_deleted(disk, begin);
3511 :
3512 : /* find the end of run of blocks */
3513 961 : end = begin + 1;
3514 961 : while (end < blockmax
3515 4983933 : && is_deleted == fs_is_block_deleted(disk, end)
3516 : ) {
3517 4982972 : ++end;
3518 : }
3519 :
3520 961 : sputb32(end - begin, f);
3521 :
3522 961 : if (is_deleted) {
3523 : /* write the run of deleted blocks with hash */
3524 51 : sputc('o', f);
3525 :
3526 : /* write all the hash */
3527 17793 : while (begin < end) {
3528 17742 : struct snapraid_block* block = fs_par2block_get(disk, begin);
3529 :
3530 17742 : swrite(block->hash, BLOCK_HASH_SIZE, f);
3531 :
3532 17742 : ++begin;
3533 : }
3534 : } else {
3535 : /* write the run of blocks without hash */
3536 : /* they can be either used or empty blocks */
3537 910 : sputc('O', f);
3538 :
3539 : /* next begin position */
3540 910 : begin = end;
3541 : }
3542 :
3543 961 : if (serror(f)) {
3544 : /* LCOV_EXCL_START */
3545 : log_fatal(errno, "Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
3546 : goto bail;
3547 : /* LCOV_EXCL_STOP */
3548 : }
3549 : }
3550 : }
3551 :
3552 : /* write the info for each block */
3553 162 : sputc('i', f);
3554 : /* ensure to write a 64 bit time */
3555 162 : t64 = info_oldest;
3556 162 : sputb64(t64, f);
3557 162 : begin = 0;
3558 14726 : while (begin < blockmax) {
3559 : snapraid_info info;
3560 : block_off_t end;
3561 : block_off_t count;
3562 : time_t t;
3563 : unsigned flag;
3564 :
3565 14564 : info = info_get(&state->infoarr, begin);
3566 :
3567 : /* avoid this slow operation if not needed */
3568 14564 : if (context->first && fs_is_block_unsynced(state, begin))
3569 125 : ++count_unsynced;
3570 :
3571 : /* find the end of run of blocks */
3572 14564 : end = begin + 1;
3573 14564 : while (end < blockmax
3574 831830 : && info == info_get(&state->infoarr, end)
3575 : ) {
3576 817266 : if (context->first && fs_is_block_unsynced(state, end))
3577 112367 : ++count_unsynced;
3578 817266 : ++end;
3579 : }
3580 :
3581 14564 : count = end - begin;
3582 14564 : sputb32(count, f);
3583 :
3584 : /* if there is info */
3585 14564 : if (info) {
3586 : /* other flags */
3587 14536 : flag = 1; /* info is present */
3588 14536 : if (info_get_bad(info)) {
3589 1331 : flag |= 2;
3590 1331 : if (context->first)
3591 1331 : count_bad += count;
3592 : }
3593 14536 : if (info_get_rehash(info)) {
3594 4992 : flag |= 4;
3595 4992 : if (context->first)
3596 4992 : count_rehash += count;
3597 : }
3598 14536 : if (info_get_justsynced(info)) {
3599 7829 : flag |= 8;
3600 7829 : if (context->first)
3601 7829 : count_unscrubbed += count;
3602 : }
3603 14536 : sputb32(flag, f);
3604 :
3605 14536 : t = info_get_time(info);
3606 :
3607 14536 : if (context->first)
3608 14536 : bucket_insert(&bucket_hash, t, count, flag & 8);
3609 :
3610 : /* truncate any time that is in the future */
3611 14536 : if (t > info_now)
3612 0 : t = info_now;
3613 :
3614 : /* the oldest info is computed only on required blocks, so it may not be the absolute oldest */
3615 14536 : if (t < info_oldest)
3616 0 : t = 0;
3617 : else
3618 14536 : t -= info_oldest;
3619 :
3620 : /* ensure to write a 64 bit time */
3621 14536 : t64 = t;
3622 14536 : sputb64(t64, f);
3623 : } else {
3624 : /* write a special 0 flag to mark missing info */
3625 28 : sputb32(0, f);
3626 : }
3627 :
3628 14564 : if (serror(f)) {
3629 : /* LCOV_EXCL_START */
3630 : log_fatal(errno, "Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
3631 : goto bail;
3632 : /* LCOV_EXCL_STOP */
3633 : }
3634 :
3635 : /* next begin position */
3636 14564 : begin = end;
3637 : }
3638 :
3639 162 : sputc('N', f);
3640 :
3641 : /* flush data written to the disk */
3642 162 : if (sflush(f)) {
3643 : /* LCOV_EXCL_START */
3644 : log_fatal(errno, "Error writing the content file '%s' (in flush before crc). %s.\n", serrorfile(f), strerror(errno));
3645 : goto bail;
3646 : /* LCOV_EXCL_STOP */
3647 : }
3648 :
3649 : /* get the file crc */
3650 162 : crc = scrc(f);
3651 :
3652 : /* compare the crc of the data written to file */
3653 : /* with the one of the data written to the stream */
3654 162 : if (crc != scrc_stream(f)) {
3655 : /* LCOV_EXCL_START */
3656 : log_fatal(ECONTENT, "CRC mismatch while writing the content stream.\n");
3657 : log_fatal(ECONTENT, "DANGER! Your RAM memory is faulty! DO NOT PROCEED UNTIL FIXED!\n");
3658 : log_fatal(ECONTENT, "Try running a memory test like http://www.memtest86.com/\n");
3659 : goto bail;
3660 : /* LCOV_EXCL_STOP */
3661 : }
3662 :
3663 162 : sputble32(crc, f);
3664 162 : if (serror(f)) {
3665 : /* LCOV_EXCL_START */
3666 : log_fatal(errno, "Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
3667 : goto bail;
3668 : /* LCOV_EXCL_STOP */
3669 : }
3670 :
3671 : /* set output variables */
3672 162 : context->crc = crc;
3673 162 : context->count_file = count_file;
3674 162 : context->count_hardlink = count_hardlink;
3675 162 : context->count_symlink = count_symlink;
3676 162 : context->count_dir = count_dir;
3677 162 : context->count_bad = count_bad;
3678 162 : context->count_rehash = count_rehash;
3679 162 : context->count_unsynced = count_unsynced;
3680 162 : context->count_unscrubbed = count_unscrubbed;
3681 :
3682 : /* store the bucket info list */
3683 162 : if (context->first)
3684 162 : bucket_to_list(&bucket_hash, &state->bucketlist, &state->bucketcount);
3685 :
3686 162 : tommy_hashdyn_done(&bucket_hash);
3687 162 : return 0;
3688 :
3689 0 : bail:
3690 0 : tommy_hashdyn_done(&bucket_hash);
3691 0 : return context;
3692 : }
3693 :
3694 162 : static void state_write_content(struct snapraid_state* state, uint32_t* out_crc)
3695 : {
3696 : #if HAVE_MT_WRITE
3697 : int fail;
3698 : int first;
3699 : #else
3700 : STREAM* f;
3701 : unsigned count_content;
3702 : unsigned k;
3703 : struct state_write_thread_context* context;
3704 : void* retval;
3705 : #endif
3706 : tommy_node* i;
3707 : block_off_t blockmax;
3708 : time_t info_oldest;
3709 : time_t info_now;
3710 : int info_has_rehash;
3711 : int mapping_idx;
3712 : block_off_t idx;
3713 : uint32_t crc;
3714 : unsigned count_file;
3715 : unsigned count_hardlink;
3716 : unsigned count_symlink;
3717 : unsigned count_dir;
3718 : unsigned count_bad;
3719 : unsigned count_rehash;
3720 : unsigned count_unsynced;
3721 : unsigned count_unscrubbed;
3722 :
3723 : /* blocks of all array */
3724 162 : blockmax = parity_allocated_size(state);
3725 :
3726 : /* check the file-system on all disks */
3727 162 : state_fscheck(state, "before write");
3728 :
3729 : /* clear the info for unused blocks */
3730 : /* and get some other info */
3731 162 : info_oldest = 0; /* oldest time in info */
3732 162 : info_now = time(0); /* get the present time */
3733 162 : info_has_rehash = 0; /* if there is a rehash info */
3734 831992 : for (idx = 0; idx < blockmax; ++idx) {
3735 : /* if the position is used */
3736 831830 : if (fs_position_is_required(state, idx)) {
3737 829952 : snapraid_info info = info_get(&state->infoarr, idx);
3738 :
3739 : /* only if there is some info to store */
3740 829952 : if (info) {
3741 783311 : time_t info_time = info_get_time(info);
3742 :
3743 783311 : if (!info_oldest || info_time < info_oldest)
3744 161 : info_oldest = info_time;
3745 :
3746 783311 : if (info_get_rehash(info))
3747 13912 : info_has_rehash = 1;
3748 : }
3749 : } else {
3750 : /* clear any previous info */
3751 1878 : info_set(&state->infoarr, idx, 0);
3752 :
3753 : /* and clear any deleted blocks */
3754 1878 : fs_position_clear_deleted(state, idx);
3755 : }
3756 : }
3757 :
3758 : /* map disks */
3759 162 : mapping_idx = 0;
3760 1134 : for (i = state->maplist; i != 0; i = i->next) {
3761 972 : struct snapraid_map* map = i->data;
3762 : struct snapraid_disk* disk;
3763 :
3764 : /* find the disk for this mapping */
3765 972 : disk = find_disk_by_name(state, map->name);
3766 972 : if (!disk) {
3767 : /* LCOV_EXCL_START */
3768 : log_fatal(EINTERNAL, "Internal inconsistency: Unmapped disk '%s'\n", map->name);
3769 : os_abort();
3770 : /* LCOV_EXCL_STOP */
3771 : }
3772 :
3773 : /* save the mapping only for not empty disks */
3774 972 : if (!fs_is_empty(disk, blockmax)) {
3775 : /* assign the mapping index used to identify disks */
3776 880 : disk->mapping_idx = mapping_idx;
3777 880 : ++mapping_idx;
3778 : } else {
3779 : /* mark the disk as without mapping */
3780 92 : disk->mapping_idx = -1;
3781 : }
3782 : }
3783 :
3784 : #if HAVE_MT_WRITE
3785 : /* start all writing threads */
3786 : first = 1;
3787 : i = tommy_list_head(&state->contentlist);
3788 : while (i) {
3789 : struct snapraid_content* content = i->data;
3790 : struct state_write_thread_context* context;
3791 : char tmp[PATH_MAX];
3792 : char esc_buffer[ESC_MAX];
3793 : STREAM* f;
3794 :
3795 : msg_progress("Saving state to %s...\n", content->content);
3796 : log_tag("content_write:%s\n", esc_tag(content->content, esc_buffer));
3797 :
3798 : pathprint(tmp, sizeof(tmp), "%s.tmp", content->content);
3799 :
3800 : /* ensure to delete a previous stale file */
3801 : if (remove(tmp) != 0) {
3802 : if (errno != ENOENT) {
3803 : /* LCOV_EXCL_START */
3804 : log_fatal(errno, "Error removing the stale content file '%s'. %s.\n", tmp, strerror(errno));
3805 : exit(EXIT_FAILURE);
3806 : /* LCOV_EXCL_STOP */
3807 : }
3808 : }
3809 :
3810 : f = sopen_write(tmp);
3811 : if (f == 0) {
3812 : /* LCOV_EXCL_START */
3813 : log_fatal(errno, "Error opening the temporary content file '%s'. %s.\n", tmp, strerror(errno));
3814 : exit(EXIT_FAILURE);
3815 : /* LCOV_EXCL_STOP */
3816 : }
3817 :
3818 : /* allocate the thread context */
3819 : context = malloc_nofail(sizeof(struct state_write_thread_context));
3820 : content->context = context;
3821 :
3822 : /* initialize */
3823 : context->state = state;
3824 : context->blockmax = blockmax;
3825 : context->info_oldest = info_oldest;
3826 : context->info_now = info_now;
3827 : context->info_has_rehash = info_has_rehash;
3828 : context->f = f;
3829 : context->first = first;
3830 : first = 0;
3831 :
3832 : thread_create(&context->thread, state_write_thread, context);
3833 :
3834 : i = i->next;
3835 : }
3836 :
3837 : /* join all thread */
3838 : fail = 0;
3839 : first = 1;
3840 : crc = 0;
3841 : count_file = 0;
3842 : count_hardlink = 0;
3843 : count_symlink = 0;
3844 : count_dir = 0;
3845 : count_bad = 0;
3846 : count_rehash = 0;
3847 : count_unsynced = 0;
3848 : count_unscrubbed = 0;
3849 : i = tommy_list_head(&state->contentlist);
3850 : while (i) {
3851 : struct snapraid_content* content = i->data;
3852 : struct state_write_thread_context* context = content->context;
3853 : void* retval;
3854 :
3855 : thread_join(context->thread, &retval);
3856 :
3857 : if (retval) {
3858 : /* LCOV_EXCL_START */
3859 : fail = 1;
3860 : /* LCOV_EXCL_STOP */
3861 : } else {
3862 : STREAM* f = context->f;
3863 :
3864 : /* Use the sequence fflush() -> fsync() -> fclose() -> rename() to ensure */
3865 : /* than even in a system crash event we have one valid copy of the file. */
3866 : if (sflush(f) != 0) {
3867 : /* LCOV_EXCL_START */
3868 : log_fatal(errno, "Error writing the content file '%s', in flush(). %s.\n", serrorfile(f), strerror(errno));
3869 : exit(EXIT_FAILURE);
3870 : /* LCOV_EXCL_STOP */
3871 : }
3872 :
3873 : #if HAVE_FSYNC
3874 : if (ssync(f) != 0) {
3875 : /* LCOV_EXCL_START */
3876 : log_fatal(errno, "Error writing the content file '%s' in sync(). %s.\n", serrorfile(f), strerror(errno));
3877 : exit(EXIT_FAILURE);
3878 : /* LCOV_EXCL_STOP */
3879 : }
3880 : #endif
3881 :
3882 : if (sclose(f) != 0) {
3883 : /* LCOV_EXCL_START */
3884 : log_fatal(errno, "Error closing the content file. %s.\n", strerror(errno));
3885 : exit(EXIT_FAILURE);
3886 : /* LCOV_EXCL_STOP */
3887 : }
3888 :
3889 : if (first) {
3890 : first = 0;
3891 : crc = context->crc;
3892 : count_file = context->count_file;
3893 : count_hardlink = context->count_hardlink;
3894 : count_symlink = context->count_symlink;
3895 : count_dir = context->count_dir;
3896 : cound_bad = context->count_bad;
3897 : count_rehash = context->count_rehash;
3898 : count_unsynced = context->count_unsynced;
3899 : count_unscrubbed = context->count_unscrubbed;
3900 : } else {
3901 : if (crc != context->crc) {
3902 : /* LCOV_EXCL_START */
3903 : log_fatal(ECONTENT, "Different CRCs writing content streams.\n");
3904 : log_fatal(ECONTENT, "DANGER! Your RAM memory is broken! DO NOT PROCEED UNTIL FIXED!\n");
3905 : log_fatal(ECONTENT, "Try running a memory test like http://www.memtest86.com/\n");
3906 : exit(EXIT_FAILURE);
3907 : /* LCOV_EXCL_STOP */
3908 : }
3909 : }
3910 : }
3911 :
3912 : free(context);
3913 :
3914 : i = i->next;
3915 : }
3916 :
3917 : /* abort on failure */
3918 : if (fail) {
3919 : /* LCOV_EXCL_START */
3920 : exit(EXIT_FAILURE);
3921 : /* LCOV_EXCL_STOP */
3922 : }
3923 : #else
3924 : /* count the content files */
3925 162 : count_content = 0;
3926 162 : i = tommy_list_head(&state->contentlist);
3927 1241 : while (i) {
3928 1079 : struct snapraid_content* content = i->data;
3929 1079 : msg_progress("Saving state to %s...\n", content->content);
3930 1079 : ++count_content;
3931 1079 : i = i->next;
3932 : }
3933 :
3934 : /* open all the content files */
3935 162 : f = sopen_multi_write(count_content, STREAM_FLAGS_SEQUENTIAL | STREAM_FLAGS_CRC);
3936 162 : if (!f) {
3937 : /* LCOV_EXCL_START */
3938 : log_fatal(errno, "Error opening the content files.\n");
3939 : exit(EXIT_FAILURE);
3940 : /* LCOV_EXCL_STOP */
3941 : }
3942 :
3943 162 : k = 0;
3944 162 : i = tommy_list_head(&state->contentlist);
3945 1241 : while (i) {
3946 1079 : struct snapraid_content* content = i->data;
3947 : char tmp[PATH_MAX];
3948 1079 : pathprint(tmp, sizeof(tmp), "%s.tmp", content->content);
3949 :
3950 : /* ensure to delete a previous stale file */
3951 1079 : if (remove(tmp) != 0) {
3952 1079 : if (errno != ENOENT) {
3953 : /* LCOV_EXCL_START */
3954 : log_fatal(errno, "Error removing the stale content file '%s'. %s.\n", tmp, strerror(errno));
3955 : exit(EXIT_FAILURE);
3956 : /* LCOV_EXCL_STOP */
3957 : }
3958 : }
3959 :
3960 1079 : if (sopen_multi_file(f, k, tmp) != 0) {
3961 : /* LCOV_EXCL_START */
3962 : log_fatal(errno, "Error opening the temporary content file '%s'. %s.\n", tmp, strerror(errno));
3963 : exit(EXIT_FAILURE);
3964 : /* LCOV_EXCL_STOP */
3965 : }
3966 :
3967 1079 : ++k;
3968 1079 : i = i->next;
3969 : }
3970 :
3971 : /* allocate the thread context */
3972 162 : context = malloc_nofail(sizeof(struct state_write_thread_context));
3973 :
3974 : /* initialize */
3975 162 : context->state = state;
3976 162 : context->blockmax = blockmax;
3977 162 : context->info_oldest = info_oldest;
3978 162 : context->info_now = info_now;
3979 162 : context->info_has_rehash = info_has_rehash;
3980 162 : context->f = f;
3981 162 : context->first = 1;
3982 :
3983 162 : retval = state_write_thread(context);
3984 :
3985 : /* abort on failure */
3986 162 : if (retval) {
3987 : /* LCOV_EXCL_START */
3988 : exit(EXIT_FAILURE);
3989 : /* LCOV_EXCL_STOP */
3990 : }
3991 :
3992 : /* Use the sequence fflush() -> fsync() -> fclose() -> rename() to ensure */
3993 : /* than even in a system crash event we have one valid copy of the file. */
3994 162 : if (sflush(f) != 0) {
3995 : /* LCOV_EXCL_START */
3996 : log_fatal(errno, "Error writing the content file '%s', in flush(). %s.\n", serrorfile(f), strerror(errno));
3997 : exit(EXIT_FAILURE);
3998 : /* LCOV_EXCL_STOP */
3999 : }
4000 :
4001 : #if HAVE_FSYNC
4002 162 : if (ssync(f) != 0) {
4003 : /* LCOV_EXCL_START */
4004 : log_fatal(errno, "Error writing the content file '%s' in sync(). %s.\n", serrorfile(f), strerror(errno));
4005 : exit(EXIT_FAILURE);
4006 : /* LCOV_EXCL_STOP */
4007 : }
4008 : #endif
4009 :
4010 162 : if (sclose(f) != 0) {
4011 : /* LCOV_EXCL_START */
4012 : log_fatal(errno, "Error closing the content file. %s.\n", strerror(errno));
4013 : exit(EXIT_FAILURE);
4014 : /* LCOV_EXCL_STOP */
4015 : }
4016 :
4017 162 : crc = context->crc;
4018 162 : count_file = context->count_file;
4019 162 : count_hardlink = context->count_hardlink;
4020 162 : count_symlink = context->count_symlink;
4021 162 : count_dir = context->count_dir;
4022 162 : count_bad = context->count_bad;
4023 162 : count_rehash = context->count_rehash;
4024 162 : count_unsynced = context->count_unsynced;
4025 162 : count_unscrubbed = context->count_unscrubbed;
4026 :
4027 162 : free(context);
4028 : #endif
4029 :
4030 162 : msg_verbose("%8u files\n", count_file);
4031 162 : msg_verbose("%8u hardlinks\n", count_hardlink);
4032 162 : msg_verbose("%8u symlinks\n", count_symlink);
4033 162 : msg_verbose("%8u empty dirs\n", count_dir);
4034 :
4035 162 : log_tag("content_info:file:%u\n", count_file);
4036 162 : log_tag("content_info:hardlink:%u\n", count_hardlink);
4037 162 : log_tag("content_info:symlink:%u\n", count_symlink);
4038 162 : log_tag("content_info:dir_empty:%u\n", count_dir);
4039 :
4040 162 : log_tag("content_info:block:%u\n", blockmax);
4041 162 : log_tag("content_info:block_bad:%u\n", count_bad);
4042 162 : log_tag("content_info:block_rehash:%u\n", count_rehash);
4043 162 : log_tag("content_info:block_unsynced:%u\n", count_unsynced);
4044 162 : log_tag("content_info:block_unscrubbed:%u\n", count_unscrubbed);
4045 :
4046 : /* store the blocks counters */
4047 162 : state->rehash_blocks = count_rehash;
4048 162 : state->bad_blocks = count_bad;
4049 162 : state->unsynced_blocks = count_unsynced;
4050 162 : state->unscrubbed_blocks = count_unscrubbed;
4051 :
4052 162 : *out_crc = crc;
4053 162 : }
4054 :
4055 280 : void state_read(struct snapraid_state* state)
4056 : {
4057 : STREAM* f;
4058 : char path[PATH_MAX];
4059 : struct stat st;
4060 : tommy_node* node;
4061 : int ret;
4062 : int c;
4063 :
4064 : /* iterate over all the available content files and load the first one present */
4065 280 : f = 0;
4066 280 : node = tommy_list_head(&state->contentlist);
4067 330 : while (node) {
4068 323 : struct snapraid_content* content = node->data;
4069 323 : pathcpy(path, sizeof(path), content->content);
4070 :
4071 323 : if (!state->no_conf) {
4072 : char esc_buffer[ESC_MAX];
4073 322 : log_tag("content:%s\n", esc_tag(path, esc_buffer));
4074 322 : log_flush();
4075 : }
4076 323 : msg_progress("Loading state from %s...\n", path);
4077 :
4078 323 : f = sopen_read(path, STREAM_FLAGS_SEQUENTIAL | STREAM_FLAGS_CRC);
4079 323 : if (f != 0) {
4080 : /* if opened stop the search */
4081 273 : break;
4082 : } else {
4083 : /* if it's real error of an existing file, abort */
4084 50 : if (errno != ENOENT) {
4085 : /* LCOV_EXCL_START */
4086 : log_fatal(errno, "Error opening the content file '%s'. %s.\n", path, strerror(errno));
4087 : exit(EXIT_FAILURE);
4088 : /* LCOV_EXCL_STOP */
4089 : }
4090 :
4091 : /* otherwise continue */
4092 50 : if (node->next) {
4093 43 : log_fatal(errno, "WARNING! Content file '%s' not found, attempting with another copy...\n", path);
4094 :
4095 : /* ensure to rewrite all the content files */
4096 43 : state->need_write = 1;
4097 : }
4098 : }
4099 :
4100 : /* next content file */
4101 50 : node = node->next;
4102 : }
4103 :
4104 : /* if not found, assume empty */
4105 280 : if (!f) {
4106 7 : log_fatal(EUSER, "No content file found. Assuming empty.\n");
4107 :
4108 : /* create the initial mapping */
4109 7 : state_map(state);
4110 7 : return;
4111 : }
4112 :
4113 : /* get the stat of the content file */
4114 273 : ret = fstat(shandle(f), &st);
4115 273 : if (ret != 0) {
4116 : /* LCOV_EXCL_START */
4117 : log_fatal(errno, "Error stating the content file '%s'. %s.\n", path, strerror(errno));
4118 : exit(EXIT_FAILURE);
4119 : /* LCOV_EXCL_STOP */
4120 : }
4121 :
4122 : /* go further to check other content files */
4123 1824 : while (node) {
4124 : char other_path[PATH_MAX];
4125 : struct stat other_st;
4126 1551 : struct snapraid_content* content = node->data;
4127 1551 : pathcpy(other_path, sizeof(other_path), content->content);
4128 :
4129 1551 : ret = stat(other_path, &other_st);
4130 1551 : if (ret != 0) {
4131 : /* allow missing content files, but not any other kind of error */
4132 1 : if (errno != ENOENT) {
4133 : /* LCOV_EXCL_START */
4134 : log_fatal(errno, "Error stating the content file '%s'. %s.\n", other_path, strerror(errno));
4135 : exit(EXIT_FAILURE);
4136 : /* LCOV_EXCL_STOP */
4137 : }
4138 :
4139 : /* ensure to rewrite all the content files */
4140 1 : state->need_write = 1;
4141 : } else {
4142 : /* if the size is different */
4143 1550 : if (other_st.st_size != st.st_size) {
4144 39 : log_fatal(ECONTENT, "WARNING! Content files '%s' and '%s' have a different size!\n", path, other_path);
4145 39 : log_fatal(ECONTENT, "Likely one of the two is broken!\n");
4146 :
4147 : /* ensure to rewrite all the content files */
4148 39 : state->need_write = 1;
4149 : }
4150 : }
4151 :
4152 : /* next content file */
4153 1551 : node = node->next;
4154 : }
4155 :
4156 : /* start with a undefined default. */
4157 : /* it's for compatibility with version 1.0 where MD5 was implicit. */
4158 273 : state->hash = HASH_UNDEFINED;
4159 :
4160 : /* start with a zero seed, it was the default in old versions */
4161 273 : memset(state->hashseed, 0, HASH_MAX);
4162 :
4163 : /* previous hash, start with an undefined value */
4164 273 : state->prevhash = HASH_UNDEFINED;
4165 :
4166 : /* intentionally not set the prevhashseed, if used valgrind will warn about it */
4167 :
4168 : /* get the first char to detect the file type */
4169 273 : c = sgetc(f);
4170 273 : sungetc(c, f);
4171 :
4172 : /* guess the file type from the first char */
4173 273 : if (c == 'S') {
4174 273 : state_read_content(state, path, f);
4175 : } else {
4176 : /* LCOV_EXCL_START */
4177 : log_fatal(EUSER, "From SnapRAID v9.0 the text content file is not supported anymore.\n");
4178 : log_fatal(EUSER, "You have first to upgrade to SnapRAID v8.1 to convert it to binary format.\n");
4179 : exit(EXIT_FAILURE);
4180 : /* LCOV_EXCL_STOP */
4181 : }
4182 :
4183 272 : sclose(f);
4184 :
4185 272 : if (state->hash == HASH_UNDEFINED) {
4186 : /* LCOV_EXCL_START */
4187 : log_fatal(EUSER, "The checksum to use is not specified.\n");
4188 : log_fatal(EUSER, "This happens because you are likely upgrading from SnapRAID 1.0.\n");
4189 : log_fatal(EUSER, "To use a new SnapRAID you must restart from scratch,\n");
4190 : log_fatal(EUSER, "deleting all the content and parity files.\n");
4191 : exit(EXIT_FAILURE);
4192 : /* LCOV_EXCL_STOP */
4193 : }
4194 :
4195 : /* update the mapping */
4196 272 : state_map(state);
4197 :
4198 272 : state_content_check(state, path);
4199 :
4200 : /* mark that we read the content file, and it passed all the checks */
4201 272 : state->checked_read = 1;
4202 : }
4203 :
4204 : struct state_verify_thread_context {
4205 : struct snapraid_state* state;
4206 : struct snapraid_content* content;
4207 : #if HAVE_MT_VERIFY
4208 : thread_id_t thread;
4209 : #else
4210 : void* retval;
4211 : #endif
4212 : /* input */
4213 : uint32_t crc;
4214 : STREAM* f;
4215 : };
4216 :
4217 1079 : static void* state_verify_thread(void* arg)
4218 : {
4219 1079 : struct state_verify_thread_context* context = arg;
4220 1079 : struct snapraid_content* content = context->content;
4221 1079 : STREAM* f = context->f;
4222 : unsigned char buf[4];
4223 : uint32_t crc_stored;
4224 : uint32_t crc_computed;
4225 : uint64_t start;
4226 :
4227 1079 : start = tick_ms();
4228 :
4229 1079 : if (sdeplete(f, buf) != 0) {
4230 : /* LCOV_EXCL_START */
4231 : log_fatal(errno, "Failed to flush content file '%s'. %s.\n", serrorfile(f), strerror(errno));
4232 : return context;
4233 : /* LCOV_EXCL_STOP */
4234 : }
4235 :
4236 : /* get the stored crc from the last four bytes */
4237 1079 : crc_stored = buf[0] | (uint32_t)buf[1] << 8 | (uint32_t)buf[2] << 16 | (uint32_t)buf[3] << 24;
4238 :
4239 1079 : if (crc_stored != context->crc) {
4240 : /* LCOV_EXCL_START */
4241 : log_fatal(ECONTENT, "DANGER! Wrong stored CRC in '%s'\n", serrorfile(f));
4242 : return context;
4243 : /* LCOV_EXCL_STOP */
4244 : }
4245 :
4246 : /* get the computed crc */
4247 1079 : crc_computed = scrc(f);
4248 :
4249 : /* adjust the stored crc to include itself */
4250 1079 : crc_stored = crc32c(crc_stored, buf, 4);
4251 :
4252 1079 : if (crc_computed != crc_stored) {
4253 : /* LCOV_EXCL_START */
4254 : log_fatal(ECONTENT, "DANGER! Wrong file CRC in '%s'\n", serrorfile(f));
4255 : return context;
4256 : /* LCOV_EXCL_STOP */
4257 : }
4258 :
4259 1079 : msg_progress("Verified %s in %" PRIu64 " seconds\n", content->content, (tick_ms() - start) / 1000);
4260 :
4261 1079 : return 0;
4262 : }
4263 :
4264 162 : static void state_verify_content(struct snapraid_state* state, uint32_t crc)
4265 : {
4266 : tommy_node* i;
4267 : int fail;
4268 :
4269 162 : msg_progress("Verifying...\n");
4270 :
4271 : /* start all reading threads */
4272 162 : i = tommy_list_head(&state->contentlist);
4273 1241 : while (i) {
4274 1079 : struct snapraid_content* content = i->data;
4275 : struct state_verify_thread_context* context;
4276 : char tmp[PATH_MAX];
4277 : STREAM* f;
4278 :
4279 1079 : pathprint(tmp, sizeof(tmp), "%s.tmp", content->content);
4280 1079 : f = sopen_read(tmp, STREAM_FLAGS_SEQUENTIAL | STREAM_FLAGS_CRC);
4281 1079 : if (f == 0) {
4282 : /* LCOV_EXCL_START */
4283 : log_fatal(errno, "Error reopening the temporary content file '%s'. %s.\n", tmp, strerror(errno));
4284 : exit(EXIT_FAILURE);
4285 : /* LCOV_EXCL_STOP */
4286 : }
4287 :
4288 : /* allocate the thread context */
4289 1079 : context = malloc_nofail(sizeof(struct state_verify_thread_context));
4290 1079 : content->context = context;
4291 :
4292 : /* initialize */
4293 1079 : context->state = state;
4294 1079 : context->content = content;
4295 1079 : context->crc = crc;
4296 1079 : context->f = f;
4297 :
4298 : #if HAVE_MT_VERIFY
4299 1079 : thread_create(&context->thread, state_verify_thread, context);
4300 : #else
4301 : context->retval = state_verify_thread(context);
4302 : #endif
4303 :
4304 1079 : i = i->next;
4305 : }
4306 :
4307 : /* join all thread */
4308 162 : fail = 0;
4309 162 : i = tommy_list_head(&state->contentlist);
4310 1241 : while (i) {
4311 1079 : struct snapraid_content* content = i->data;
4312 1079 : struct state_verify_thread_context* context = content->context;
4313 : void* retval;
4314 :
4315 : #if HAVE_MT_VERIFY
4316 1079 : thread_join(context->thread, &retval);
4317 : #else
4318 : retval = context->retval;
4319 : #endif
4320 1079 : if (retval) {
4321 : /* LCOV_EXCL_START */
4322 : fail = 1;
4323 : /* LCOV_EXCL_STOP */
4324 : } else {
4325 1079 : STREAM* f = context->f;
4326 :
4327 1079 : if (sclose(f) != 0) {
4328 : /* LCOV_EXCL_START */
4329 : log_fatal(errno, "Error closing the content file. %s.\n", strerror(errno));
4330 : exit(EXIT_FAILURE);
4331 : /* LCOV_EXCL_STOP */
4332 : }
4333 : }
4334 :
4335 1079 : free(context);
4336 :
4337 1079 : i = i->next;
4338 : }
4339 :
4340 : /* abort on failure */
4341 162 : if (fail) {
4342 : /* LCOV_EXCL_START */
4343 : exit(EXIT_FAILURE);
4344 : /* LCOV_EXCL_STOP */
4345 : }
4346 162 : }
4347 :
4348 162 : static void state_rename_content(struct snapraid_state* state)
4349 : {
4350 : tommy_node* i;
4351 :
4352 : #if defined(_linux) /* this sequence is linux specific */
4353 : i = tommy_list_head(&state->contentlist);
4354 : while (i) {
4355 : struct snapraid_content* content = i->data;
4356 : char tmp[PATH_MAX];
4357 : char dir[PATH_MAX];
4358 : char* slash;
4359 : int handle;
4360 :
4361 : pathcpy(dir, sizeof(dir), content->content);
4362 :
4363 : slash = strrchr(tmp, '/');
4364 : if (slash)
4365 : *slash = 0;
4366 : else
4367 : pathcpy(dir, sizeof(dir), ".");
4368 :
4369 : /* open the directory to get the handle */
4370 : handle = open(dir, O_RDONLY | O_DIRECTORY);
4371 : if (handle < 0) {
4372 : /* LCOV_EXCL_START */
4373 : log_fatal(errno, "Error opening the directory '%s'. %s.\n", dir, strerror(errno));
4374 : exit(EXIT_FAILURE);
4375 : /* LCOV_EXCL_STOP */
4376 : }
4377 :
4378 : /* now rename the just written copy with the correct name */
4379 : pathprint(tmp, sizeof(tmp), "%s.tmp", content->content);
4380 : if (rename(tmp, content->content) != 0) {
4381 : /* LCOV_EXCL_START */
4382 : log_fatal(errno, "Error renaming the content file '%s' to '%s'. %s.\n", tmp, content->content, strerror(errno));
4383 : exit(EXIT_FAILURE);
4384 : /* LCOV_EXCL_STOP */
4385 : }
4386 :
4387 : /* sync the directory */
4388 : if (fsync(handle) != 0) {
4389 : /* LCOV_EXCL_START */
4390 : log_fatal(errno, "Error syncing the directory '%s'. %s.\n", dir, strerror(errno));
4391 : exit(EXIT_FAILURE);
4392 : /* LCOV_EXCL_STOP */
4393 : }
4394 :
4395 : if (close(handle) != 0) {
4396 : /* LCOV_EXCL_START */
4397 : log_fatal(errno, "Error closing the directory '%s'. %s.\n", dir, strerror(errno));
4398 : exit(EXIT_FAILURE);
4399 : /* LCOV_EXCL_STOP */
4400 : }
4401 :
4402 : i = i->next;
4403 : }
4404 : #else
4405 162 : i = tommy_list_head(&state->contentlist);
4406 1241 : while (i) {
4407 1079 : struct snapraid_content* content = i->data;
4408 : char tmp[PATH_MAX];
4409 :
4410 : /* now renames the just written copy with the correct name */
4411 1079 : pathprint(tmp, sizeof(tmp), "%s.tmp", content->content);
4412 1079 : if (rename(tmp, content->content) != 0) {
4413 : /* LCOV_EXCL_START */
4414 : log_fatal(errno, "Error renaming the content file '%s' to '%s' in rename(). %s.\n", tmp, content->content, strerror(errno));
4415 : exit(EXIT_FAILURE);
4416 : /* LCOV_EXCL_STOP */
4417 : }
4418 :
4419 1079 : i = i->next;
4420 : }
4421 : #endif
4422 162 : }
4423 :
4424 162 : void state_write(struct snapraid_state* state)
4425 : {
4426 : uint32_t crc;
4427 :
4428 : /* write all the content files */
4429 162 : state_write_content(state, &crc);
4430 :
4431 : /* verify the just written files */
4432 162 : state_verify_content(state, crc);
4433 :
4434 : /* rename the new files, over the old ones */
4435 162 : state_rename_content(state);
4436 :
4437 162 : state->need_write = 0; /* no write needed anymore */
4438 162 : state->checked_read = 0; /* what we wrote is not checked in read */
4439 162 : state->written = 1;
4440 162 : }
4441 :
4442 127 : void state_skip(struct snapraid_state* state)
4443 : {
4444 : tommy_node* i;
4445 :
4446 : /* for each disk */
4447 885 : for (i = state->disklist; i != 0; i = i->next) {
4448 : tommy_node* j;
4449 758 : struct snapraid_disk* disk = i->data;
4450 :
4451 758 : if (!disk->skip_access)
4452 757 : continue;
4453 :
4454 : /* for each file */
4455 1885 : for (j = tommy_list_head(&disk->filelist); j != 0; j = j->next) {
4456 1884 : struct snapraid_file* file = j->data;
4457 1884 : file_flag_set(file, FILE_IS_EXCLUDED);
4458 : }
4459 :
4460 : /* for each link */
4461 57 : for (j = tommy_list_head(&disk->linklist); j != 0; j = j->next) {
4462 56 : struct snapraid_link* slink = j->data;
4463 56 : link_flag_set(slink, FILE_IS_EXCLUDED);
4464 : }
4465 :
4466 : /* for each dir */
4467 1 : for (j = tommy_list_head(&disk->dirlist); j != 0; j = j->next) {
4468 0 : struct snapraid_dir* dir = j->data;
4469 0 : dir_flag_set(dir, FILE_IS_EXCLUDED);
4470 : }
4471 : }
4472 127 : }
4473 :
4474 127 : void state_filter(struct snapraid_state* state, tommy_list* filterlist_file, tommy_list* filterlist_disk, int filter_missing, int filter_error)
4475 : {
4476 : tommy_node* i;
4477 : unsigned l;
4478 :
4479 : /* if no filter, include all */
4480 127 : if (!filter_missing && !filter_error && tommy_list_empty(filterlist_file) && tommy_list_empty(filterlist_disk))
4481 117 : return;
4482 :
4483 10 : msg_progress("Selecting...\n");
4484 :
4485 19 : for (i = tommy_list_head(filterlist_disk); i != 0; i = i->next) {
4486 9 : struct snapraid_filter* filter = i->data;
4487 9 : msg_verbose("\t%s%s\n", filter->pattern, filter->is_disk ? "//" : "");
4488 : }
4489 13 : for (i = tommy_list_head(filterlist_file); i != 0; i = i->next) {
4490 3 : struct snapraid_filter* filter = i->data;
4491 3 : msg_verbose("\t%s%s\n", filter->pattern, filter->is_dir ? "/" : "");
4492 : }
4493 10 : if (filter_missing)
4494 2 : msg_verbose("\t<missing>\n");
4495 10 : if (filter_error)
4496 2 : msg_verbose("\t<error>\n");
4497 :
4498 : /* for each disk */
4499 70 : for (i = state->disklist; i != 0; i = i->next) {
4500 : tommy_node* j;
4501 60 : struct snapraid_disk* disk = i->data;
4502 :
4503 : /* if we filter for presence, we have to access the disk, so better to print something */
4504 60 : if (filter_missing)
4505 12 : msg_progress("Scanning disk %s...\n", disk->name);
4506 :
4507 : /* for each file */
4508 150479 : for (j = tommy_list_head(&disk->filelist); j != 0; j = j->next) {
4509 150419 : struct snapraid_file* file = j->data;
4510 :
4511 150419 : if (filter_path(filterlist_disk, 0, disk->name, file->sub) != 0
4512 80894 : || filter_path(filterlist_file, 0, disk->name, file->sub) != 0
4513 75166 : || filter_existence(filter_missing, disk->dir, file->sub) != 0
4514 35970 : || filter_correctness(filter_error, &state->infoarr, disk, file) != 0
4515 : ) {
4516 128683 : file_flag_set(file, FILE_IS_EXCLUDED);
4517 : }
4518 : }
4519 :
4520 : /* for each link */
4521 4935 : for (j = tommy_list_head(&disk->linklist); j != 0; j = j->next) {
4522 4875 : struct snapraid_link* slink = j->data;
4523 :
4524 4875 : if (filter_path(filterlist_disk, 0, disk->name, slink->sub) != 0
4525 2618 : || filter_path(filterlist_file, 0, disk->name, slink->sub) != 0
4526 2407 : || filter_existence(filter_missing, disk->dir, slink->sub) != 0
4527 : ) {
4528 3852 : link_flag_set(slink, FILE_IS_EXCLUDED);
4529 : }
4530 : }
4531 :
4532 : /* for each empty dir */
4533 92 : for (j = tommy_list_head(&disk->dirlist); j != 0; j = j->next) {
4534 32 : struct snapraid_dir* dir = j->data;
4535 :
4536 32 : if (filter_emptydir(filterlist_disk, 0, disk->name, dir->sub) != 0
4537 17 : || filter_emptydir(filterlist_file, 0, disk->name, dir->sub) != 0
4538 17 : || filter_existence(filter_missing, disk->dir, dir->sub) != 0
4539 : ) {
4540 23 : dir_flag_set(dir, FILE_IS_EXCLUDED);
4541 : }
4542 : }
4543 : }
4544 :
4545 : /* if we are filtering by disk, exclude any parity not explicitly included */
4546 10 : if (!tommy_list_empty(filterlist_disk)) {
4547 : /* for each parity disk */
4548 24 : for (l = 0; l < state->level; ++l) {
4549 : /* check if the parity is excluded by name */
4550 19 : if (filter_path(filterlist_disk, 0, lev_config_name(l), 0) != 0) {
4551 : /* excluded the parity from further operation */
4552 13 : state->parity[l].is_excluded_by_filter = 1;
4553 : }
4554 : }
4555 : } else {
4556 : /* if we are filtering by file, exclude all parity */
4557 5 : if (filter_missing || !tommy_list_empty(filterlist_file)) {
4558 : /* for each parity disk */
4559 16 : for (l = 0; l < state->level; ++l) {
4560 13 : state->parity[l].is_excluded_by_filter = 1;
4561 : }
4562 : }
4563 : }
4564 : }
4565 :
4566 235 : int state_progress_begin(struct snapraid_state* state, block_off_t blockstart, block_off_t blockmax, block_off_t countmax)
4567 : {
4568 : time_t now;
4569 :
4570 235 : if (state->opt.gui) {
4571 5 : log_tag("run:begin:%u:%u:%u\n", blockstart, blockmax, countmax);
4572 5 : log_flush();
4573 : }
4574 :
4575 235 : now = time(0);
4576 :
4577 235 : state->progress_whole_start = now;
4578 235 : state->progress_tick = 0;
4579 235 : state->progress_ptr = 0;
4580 235 : state->progress_wasted = 0;
4581 :
4582 : /* start of thermal control */
4583 235 : if (!state_thermal_begin(state, now))
4584 0 : return -1;
4585 :
4586 : /* stop if requested */
4587 235 : if (global_interrupt) {
4588 : /* LCOV_EXCL_START */
4589 : if (!state->opt.gui) {
4590 : msg_status("Not starting due to interruption\n");
4591 : }
4592 : log_tag("sigint:0: SIGINT received\n");
4593 : log_flush();
4594 : return 1;
4595 : /* LCOV_EXCL_STOP */
4596 : }
4597 :
4598 235 : return 0;
4599 : }
4600 :
4601 235 : void state_progress_end(struct snapraid_state* state, block_off_t countpos, block_off_t countmax, data_off_t countsize, const char* msg)
4602 : {
4603 235 : if (state->opt.gui) {
4604 5 : log_tag("run:end\n");
4605 5 : log_flush();
4606 230 : } else if (countmax == 0) {
4607 21 : if (state->need_write || state->written) {
4608 16 : msg_status("100%% completed\n");
4609 : } else {
4610 5 : msg_status("%s", msg);
4611 : }
4612 : } else {
4613 : time_t now;
4614 : time_t elapsed;
4615 :
4616 209 : unsigned countsize_MB = (countsize + MEGA - 1) / MEGA;
4617 :
4618 209 : now = time(0);
4619 :
4620 209 : elapsed = now - state->progress_whole_start - state->progress_wasted;
4621 :
4622 209 : msg_bar("%u%% completed, %u MB accessed", muldiv(countpos, 100, countmax), countsize_MB);
4623 :
4624 209 : msg_bar(" in %u:%02u", (unsigned)(elapsed / 3600), (unsigned)((elapsed % 3600) / 60));
4625 :
4626 209 : msg_bar("\n");
4627 209 : msg_flush();
4628 : }
4629 235 : }
4630 :
4631 1 : void state_progress_stop(struct snapraid_state* state)
4632 : {
4633 : time_t now;
4634 :
4635 1 : now = time(0);
4636 :
4637 1 : state->progress_interruption = now;
4638 1 : }
4639 :
4640 1 : void state_progress_restart(struct snapraid_state* state)
4641 : {
4642 : time_t now;
4643 :
4644 1 : now = time(0);
4645 :
4646 : /* reset the progress counter */
4647 1 : state->progress_tick = 0;
4648 1 : state->progress_ptr = 0;
4649 :
4650 1 : if (now >= state->progress_interruption) /* avoid degenerated cases when the clock is manually adjusted */
4651 1 : state->progress_wasted += now - state->progress_interruption;
4652 1 : }
4653 :
4654 : /**
4655 : * Set the latest tick data in the progress vector.
4656 : */
4657 517 : static void state_progress_latest(struct snapraid_state* state)
4658 : {
4659 : tommy_node* i;
4660 : unsigned l;
4661 :
4662 517 : state->progress_tick_misc[state->progress_ptr] = state->tick_misc;
4663 517 : state->progress_tick_sched[state->progress_ptr] = state->tick_sched;
4664 517 : state->progress_tick_raid[state->progress_ptr] = state->tick_raid;
4665 517 : state->progress_tick_hash[state->progress_ptr] = state->tick_hash;
4666 517 : state->progress_tick_io[state->progress_ptr] = state->tick_io;
4667 3611 : for (i = state->disklist; i != 0; i = i->next) {
4668 3094 : struct snapraid_disk* disk = i->data;
4669 3094 : disk->progress_tick[state->progress_ptr] = disk->tick;
4670 : }
4671 2959 : for (l = 0; l < state->level; ++l)
4672 2442 : state->parity[l].progress_tick[state->progress_ptr] = state->parity[l].tick;
4673 517 : }
4674 :
4675 : /**
4676 : * Number of columns
4677 : */
4678 : #define GRAPH_COLUMN 78
4679 :
4680 : /**
4681 : * Get the reference value, 0 if index is PROGRESS_MAX
4682 : */
4683 : #define ref(map, index) (index < PROGRESS_MAX ? map[index] : 0)
4684 :
4685 0 : static struct snapraid_thermal* state_thermal_find(struct snapraid_state* state, const char* name)
4686 : {
4687 : tommy_node* i;
4688 0 : struct snapraid_thermal* found = 0;
4689 :
4690 0 : for (i = tommy_list_head(&state->thermallist); i != 0; i = i->next) {
4691 0 : struct snapraid_thermal* thermal = i->data;
4692 0 : if (strcmp(thermal->name, name) == 0) {
4693 0 : if (found == 0
4694 : /* if multiple matches, return the one with highest temperature */
4695 0 : || found->latest_temperature < thermal->latest_temperature
4696 : /* or, if the temperature is equal, the one with the higher r_squared */
4697 0 : || (found->latest_temperature == thermal->latest_temperature && found->params.r_squared < thermal->params.r_squared)
4698 : ) {
4699 0 : found = thermal;
4700 : }
4701 : }
4702 : }
4703 :
4704 0 : return found;
4705 : }
4706 :
4707 : #define THERMAL_PAD " "
4708 :
4709 1 : static void state_progress_graph(struct snapraid_state* state, struct snapraid_io* io, unsigned current, unsigned oldest)
4710 : {
4711 : uint64_t v;
4712 : uint64_t tick_total;
4713 : unsigned bar;
4714 : tommy_node* i;
4715 : unsigned l;
4716 : size_t pad;
4717 : size_t pre;
4718 :
4719 1 : tick_total = 0;
4720 :
4721 1 : tick_total += state->progress_tick_misc[current] - ref(state->progress_tick_misc, oldest);
4722 1 : tick_total += state->progress_tick_sched[current] - ref(state->progress_tick_sched, oldest);
4723 1 : tick_total += state->progress_tick_raid[current] - ref(state->progress_tick_raid, oldest);
4724 1 : tick_total += state->progress_tick_hash[current] - ref(state->progress_tick_hash, oldest);
4725 1 : tick_total += state->progress_tick_io[current] - ref(state->progress_tick_io, oldest);
4726 1 : if (!tick_total)
4727 0 : return;
4728 :
4729 1 : pad = 4;
4730 7 : for (i = state->disklist; i != 0; i = i->next) {
4731 6 : struct snapraid_disk* disk = i->data;
4732 6 : size_t len = strlen(disk->name);
4733 6 : if (pad < len)
4734 1 : pad = len;
4735 : }
4736 7 : for (l = 0; l < state->level; ++l) {
4737 6 : size_t len = strlen(lev_config_name(l));
4738 6 : if (pad < len)
4739 2 : pad = len;
4740 : }
4741 :
4742 : /* extra space */
4743 1 : pad += 1;
4744 :
4745 1 : pre = 4;
4746 1 : if (pad + pre + 30 < GRAPH_COLUMN)
4747 1 : bar = GRAPH_COLUMN - pad - pre;
4748 : else
4749 0 : bar = 30; /* at least a bar of 30 */
4750 :
4751 1 : if (io) {
4752 0 : const char* legend = "cached blocks (instant, more is better)";
4753 :
4754 : /* refresh the cached blocks info */
4755 0 : io_refresh(io);
4756 :
4757 0 : printf("\n");
4758 :
4759 0 : for (i = state->disklist; i != 0; i = i->next) {
4760 0 : struct snapraid_disk* disk = i->data;
4761 0 : v = disk->cached_blocks;
4762 0 : printr(disk->name, pad);
4763 0 : printf("%4" PRIu64 " | ", v);
4764 :
4765 0 : if (disk->progress_file && disk->progress_file->sub)
4766 0 : printf("%s", disk->progress_file->sub);
4767 : else
4768 0 : printf("-");
4769 :
4770 0 : printf("\n");
4771 : }
4772 :
4773 0 : for (l = 0; l < state->level; ++l) {
4774 0 : v = state->parity[l].cached_blocks;
4775 0 : printr(lev_config_name(l), pad);
4776 0 : printf("%4" PRIu64 " | ", v);
4777 0 : printc('o', v * bar / io->io_max);
4778 0 : printf("\n");
4779 : }
4780 :
4781 0 : printc(' ', pad);
4782 0 : printf(" |_");
4783 0 : printc('_', bar);
4784 0 : printf("\n");
4785 :
4786 0 : printc(' ', 5 + pad + 1 + bar / 2 - strlen(legend) / 2);
4787 0 : printf("%s", legend);
4788 0 : printf("\n");
4789 : }
4790 :
4791 1 : printf("\n");
4792 :
4793 1 : pre = 4;
4794 1 : if (state->thermal_temperature_limit != 0)
4795 0 : pre += strlen(THERMAL_PAD);
4796 1 : if (pad + pre + 30 < GRAPH_COLUMN)
4797 1 : bar = GRAPH_COLUMN - pad - pre;
4798 : else
4799 0 : bar = 30; /* at least a bar of 30 */
4800 :
4801 7 : for (i = state->disklist; i != 0; i = i->next) {
4802 6 : struct snapraid_disk* disk = i->data;
4803 :
4804 6 : v = disk->progress_tick[current] - ref(disk->progress_tick, oldest);
4805 6 : printr(disk->name, pad);
4806 :
4807 6 : if (state->thermal_temperature_limit != 0) {
4808 0 : struct snapraid_thermal* thermal = state_thermal_find(state, disk->name);
4809 0 : if (thermal) {
4810 0 : if (thermal->params.r_squared >= THERMAL_R_SQUARED_LIMIT)
4811 0 : printf(" %2u (%2u)", thermal->latest_temperature, (int)thermal->params.t_steady);
4812 : else
4813 0 : printf(" %2u ", thermal->latest_temperature);
4814 : } else {
4815 0 : printf(THERMAL_PAD);
4816 : }
4817 : }
4818 :
4819 6 : printf("%3u%% | ", muldiv(v, 100, tick_total));
4820 6 : printc('*', v * bar / tick_total);
4821 6 : printf("\n");
4822 :
4823 : /* clear the file in progress */
4824 6 : disk->progress_file = 0;
4825 : }
4826 :
4827 7 : for (l = 0; l < state->level; ++l) {
4828 6 : v = state->parity[l].progress_tick[current] - ref(state->parity[l].progress_tick, oldest);
4829 6 : printr(lev_config_name(l), pad);
4830 :
4831 6 : if (state->thermal_temperature_limit != 0) {
4832 0 : struct snapraid_thermal* thermal = state_thermal_find(state, lev_config_name(l));
4833 0 : if (thermal) {
4834 0 : if (thermal->params.r_squared >= THERMAL_R_SQUARED_LIMIT)
4835 0 : printf(" %2u (%2u)", thermal->latest_temperature, (int)thermal->params.t_steady);
4836 : else
4837 0 : printf(" %2u ", thermal->latest_temperature);
4838 : } else {
4839 0 : printf(THERMAL_PAD);
4840 : }
4841 : }
4842 :
4843 6 : printf("%3u%% | ", muldiv(v, 100, tick_total));
4844 6 : printc('*', v * bar / tick_total);
4845 6 : printf("\n");
4846 : }
4847 :
4848 1 : v = state->progress_tick_raid[current] - ref(state->progress_tick_raid, oldest);
4849 1 : printr("raid", pad);
4850 1 : if (state->thermal_temperature_limit != 0)
4851 0 : printf(THERMAL_PAD);
4852 1 : printf("%3u%% | ", muldiv(v, 100, tick_total));
4853 1 : printc('*', v * bar / tick_total);
4854 1 : printf("\n");
4855 :
4856 1 : v = state->progress_tick_hash[current] - ref(state->progress_tick_hash, oldest);
4857 1 : printr("hash", pad);
4858 1 : if (state->thermal_temperature_limit != 0)
4859 0 : printf(THERMAL_PAD);
4860 1 : printf("%3u%% | ", muldiv(v, 100, tick_total));
4861 1 : printc('*', v * bar / tick_total);
4862 1 : printf("\n");
4863 :
4864 1 : v = state->progress_tick_sched[current] - ref(state->progress_tick_sched, oldest);
4865 1 : printr("sched", pad);
4866 1 : if (state->thermal_temperature_limit != 0)
4867 0 : printf(THERMAL_PAD);
4868 1 : printf("%3u%% | ", muldiv(v, 100, tick_total));
4869 1 : printc('*', v * bar / tick_total);
4870 1 : printf("\n");
4871 :
4872 1 : v = state->progress_tick_misc[current] - ref(state->progress_tick_misc, oldest);
4873 1 : printr("misc", pad);
4874 1 : if (state->thermal_temperature_limit != 0)
4875 0 : printf(THERMAL_PAD);
4876 1 : printf("%3u%% | ", muldiv(v, 100, tick_total));
4877 1 : printc('*', v * bar / tick_total);
4878 1 : printf("\n");
4879 :
4880 1 : printc(' ', pad);
4881 1 : if (state->thermal_temperature_limit != 0)
4882 0 : printf(THERMAL_PAD);
4883 1 : printf(" |_");
4884 1 : printc('_', bar);
4885 1 : printf("\n");
4886 :
4887 1 : if (oldest == PROGRESS_MAX) {
4888 1 : const char* legend = "wait time (total, less is better)";
4889 1 : if (state->thermal_temperature_limit != 0)
4890 0 : printf(THERMAL_PAD);
4891 1 : printc(' ', 5 + pad + 1 + bar / 2 - strlen(legend) / 2);
4892 1 : printf("%s", legend);
4893 1 : printf("\n");
4894 : } else {
4895 0 : const char* legend_d = "wait time (last %d secs, less is better)";
4896 0 : if (state->thermal_temperature_limit != 0)
4897 0 : printf(THERMAL_PAD);
4898 0 : printc(' ', 5 + pad + 1 + bar / 2 - strlen(legend_d) / 2);
4899 0 : printf(legend_d, PROGRESS_MAX);
4900 0 : printf("\n");
4901 : }
4902 :
4903 1 : printf("\n");
4904 : }
4905 :
4906 1116995 : int state_progress(struct snapraid_state* state, struct snapraid_io* io, block_off_t blockpos, block_off_t countpos, block_off_t countmax, data_off_t countsize)
4907 : {
4908 : time_t now;
4909 : int pred;
4910 :
4911 1116995 : now = time(0);
4912 :
4913 : /* thermal measure */
4914 1116995 : if (now > state->thermal_latest + THERMAL_PERIOD_SECONDS || state->opt.fake_device) {
4915 2620 : state->thermal_latest = now;
4916 2620 : state_thermal(state, now);
4917 : }
4918 :
4919 : /* previous position */
4920 1116995 : pred = state->progress_ptr + PROGRESS_MAX - 1;
4921 1116995 : if (pred >= PROGRESS_MAX)
4922 1116785 : pred -= PROGRESS_MAX;
4923 :
4924 : /* if the previous measure is different */
4925 1116995 : if (state->progress_tick == 0
4926 1116785 : || state->progress_time[pred] != now
4927 : ) {
4928 : time_t elapsed;
4929 411 : unsigned out_perc = 0;
4930 411 : unsigned out_size_speed = 0;
4931 411 : unsigned out_block_speed = 0;
4932 411 : unsigned out_cpu = 0;
4933 411 : unsigned out_eta = 0;
4934 411 : int out_temperature = 0;
4935 411 : int out_steady = 0;
4936 411 : int out_computed = 0;
4937 :
4938 : /* store the new measure */
4939 411 : state->progress_time[state->progress_ptr] = now;
4940 411 : state->progress_pos[state->progress_ptr] = countpos;
4941 411 : state->progress_size[state->progress_ptr] = countsize;
4942 411 : state_progress_latest(state);
4943 :
4944 411 : elapsed = now - state->progress_whole_start - state->progress_wasted;
4945 :
4946 : /* completion percentage */
4947 411 : if (countmax)
4948 411 : out_perc = muldiv(countpos, 100, countmax);
4949 :
4950 : /* if we have at least 5 measures */
4951 411 : if (state->progress_tick >= 5
4952 : /* or if we are running in test mode, with at least one measure */
4953 398 : || (state->opt.force_progress && state->progress_tick >= 1)
4954 : ) {
4955 : int oldest;
4956 : int past;
4957 : time_t delta_time;
4958 : block_off_t delta_pos;
4959 : data_off_t delta_size;
4960 : uint64_t tick_cpu;
4961 : uint64_t tick_total;
4962 : uint64_t delta_tick_cpu;
4963 : uint64_t delta_tick_total;
4964 : uint64_t oldest_tick_cpu;
4965 : uint64_t oldest_tick_total;
4966 : tommy_node* i;
4967 :
4968 : /* number of past measures */
4969 201 : past = state->progress_tick;
4970 :
4971 : /* drop the oldest ones, to promptly */
4972 : /* skip the startup phase */
4973 201 : past -= past / 5;
4974 :
4975 : /* check how much we can go in the past */
4976 201 : if (past >= PROGRESS_MAX - 1) {
4977 : /* the vector is filled, so we are already in position */
4978 : /* to get the possible oldest one */
4979 0 : oldest = state->progress_ptr + 1;
4980 : } else {
4981 : /* go backward the number of positions selected */
4982 201 : oldest = state->progress_ptr + PROGRESS_MAX - past;
4983 : }
4984 201 : if (oldest >= PROGRESS_MAX)
4985 201 : oldest -= PROGRESS_MAX;
4986 :
4987 201 : tick_cpu = state->progress_tick_misc[state->progress_ptr]
4988 201 : + state->progress_tick_sched[state->progress_ptr]
4989 201 : + state->progress_tick_raid[state->progress_ptr]
4990 201 : + state->progress_tick_hash[state->progress_ptr];
4991 201 : tick_total = tick_cpu + state->progress_tick_io[state->progress_ptr];
4992 :
4993 201 : oldest_tick_cpu = state->progress_tick_misc[oldest]
4994 201 : + state->progress_tick_sched[oldest]
4995 201 : + state->progress_tick_raid[oldest]
4996 201 : + state->progress_tick_hash[oldest];
4997 201 : oldest_tick_total = oldest_tick_cpu + state->progress_tick_io[oldest];
4998 :
4999 201 : delta_time = now - state->progress_time[oldest];
5000 201 : delta_pos = countpos - state->progress_pos[oldest];
5001 201 : delta_size = countsize - state->progress_size[oldest];
5002 201 : delta_tick_cpu = tick_cpu - oldest_tick_cpu;
5003 201 : delta_tick_total = tick_total - oldest_tick_total;
5004 :
5005 : /* estimate the speed in MB/s */
5006 201 : if (delta_time != 0)
5007 201 : out_size_speed = (unsigned)(delta_size / MEGA / delta_time);
5008 :
5009 : /* estimate the speed in block/s */
5010 201 : if (delta_time != 0)
5011 201 : out_block_speed = (unsigned)(delta_pos / delta_time);
5012 :
5013 : /* estimate the cpu usage percentage */
5014 201 : if (delta_tick_total != 0)
5015 57 : out_cpu = muldiv(delta_tick_cpu, 100, delta_tick_total);
5016 :
5017 : /* estimate the remaining time in minutes */
5018 201 : if (delta_pos != 0)
5019 201 : out_eta = muldiv(countmax - countpos, delta_time, delta_pos);
5020 :
5021 201 : if (state->opt.force_stats) {
5022 0 : os_clear();
5023 0 : state_progress_graph(state, io, state->progress_ptr, oldest);
5024 : }
5025 :
5026 201 : if (state->thermal_temperature_limit != 0) {
5027 : /* get the max temperature */
5028 6 : out_temperature = 0;
5029 186 : for (i = tommy_list_head(&state->thermallist); i != 0; i = i->next) {
5030 180 : struct snapraid_thermal* thermal = i->data;
5031 180 : if (out_temperature < thermal->latest_temperature) {
5032 6 : out_temperature = thermal->latest_temperature;
5033 6 : if (thermal->params.r_squared >= THERMAL_R_SQUARED_LIMIT)
5034 6 : out_steady = thermal->params.t_steady;
5035 : else
5036 0 : out_steady = 0;
5037 : }
5038 : }
5039 : } else {
5040 195 : out_temperature = 0;
5041 195 : out_steady = 0;
5042 : }
5043 :
5044 : /* we have the output value */
5045 201 : out_computed = 1;
5046 : }
5047 :
5048 411 : if (state->opt.gui) {
5049 : char str_eta[32];
5050 : char str_size_speed[32];
5051 : char str_cpu[32];
5052 : char str_temp[32];
5053 : char str_steady[32];
5054 4 : if (out_computed) {
5055 0 : snprintf(str_eta, sizeof(str_eta), "%u", out_eta);
5056 0 : snprintf(str_size_speed, sizeof(str_size_speed), "%u", out_size_speed);
5057 0 : snprintf(str_cpu, sizeof(str_cpu), "%u", out_cpu);
5058 : } else {
5059 4 : str_eta[0] = 0;
5060 4 : str_size_speed[0] = 0;
5061 4 : str_cpu[0] = 0;
5062 : }
5063 4 : if (out_temperature) {
5064 0 : snprintf(str_temp, sizeof(str_eta), "%u", out_temperature);
5065 : } else {
5066 4 : str_temp[0] = 0;
5067 : }
5068 4 : if (out_steady) {
5069 0 : snprintf(str_steady, sizeof(str_steady), "%u", out_steady);
5070 : } else {
5071 4 : str_steady[0] = 0;
5072 : }
5073 4 : log_tag("run:pos:%u:%u:%" PRIu64 ":%u:%s:%s:%s:%" PRIu64 ":%s:%s\n", blockpos, countpos, countsize, out_perc, str_eta, str_size_speed, str_cpu, (uint64_t)elapsed, str_temp, str_steady);
5074 4 : log_flush();
5075 : } else {
5076 407 : msg_bar("%u%%, %u MB", out_perc, (unsigned)(countsize / MEGA));
5077 407 : if (out_computed) {
5078 201 : msg_bar(", %u MB/s", out_size_speed);
5079 201 : msg_bar(", %u stripe/s", out_block_speed);
5080 201 : msg_bar(", CPU %u%%", out_cpu);
5081 201 : if (out_temperature != 0) {
5082 6 : if (out_steady != 0)
5083 6 : msg_bar(", Tmax %u (%u)", out_temperature, out_steady);
5084 : else
5085 0 : msg_bar(", Tmax %u", out_temperature);
5086 : }
5087 201 : unsigned out_eta_minutes = out_eta / 60; /* minutes */
5088 201 : msg_bar(", %u:%02u ETA", out_eta_minutes / 60, out_eta_minutes % 60);
5089 : }
5090 407 : msg_bar("\r");
5091 407 : msg_flush();
5092 : }
5093 :
5094 : /* next position to fill */
5095 411 : ++state->progress_ptr;
5096 411 : if (state->progress_ptr >= PROGRESS_MAX)
5097 0 : state->progress_ptr -= PROGRESS_MAX;
5098 :
5099 : /* one more measure */
5100 411 : ++state->progress_tick;
5101 : }
5102 :
5103 : /* stop if requested */
5104 1116995 : if (global_interrupt) {
5105 : /* LCOV_EXCL_START */
5106 : if (!state->opt.gui) {
5107 : log_fatal(EUSER, "\n");
5108 : log_fatal(EUSER, "Stopping for interruption at block %u\n", blockpos);
5109 : }
5110 : log_tag("sigint:%u: SIGINT received\n", blockpos);
5111 : log_flush();
5112 : return 1;
5113 : /* LCOV_EXCL_STOP */
5114 : }
5115 :
5116 1116995 : return 0;
5117 : }
5118 :
5119 114 : void state_usage_waste(struct snapraid_state* state)
5120 : {
5121 114 : uint64_t now = tick();
5122 :
5123 114 : state->tick_last = now;
5124 114 : }
5125 :
5126 1144686 : void state_usage_misc(struct snapraid_state* state)
5127 : {
5128 1144686 : uint64_t now = tick();
5129 1144686 : uint64_t delta = now - state->tick_last;
5130 :
5131 : /* increment the time spent in computations */
5132 1144686 : state->tick_misc += delta;
5133 :
5134 1144686 : state->tick_last = now;
5135 1144686 : }
5136 :
5137 161542 : void state_usage_sched(struct snapraid_state* state)
5138 : {
5139 161542 : uint64_t now = tick();
5140 161542 : uint64_t delta = now - state->tick_last;
5141 :
5142 : /* increment the time spent in computations */
5143 161542 : state->tick_sched += delta;
5144 :
5145 161542 : state->tick_last = now;
5146 161542 : }
5147 :
5148 151036 : void state_usage_raid(struct snapraid_state* state)
5149 : {
5150 151036 : uint64_t now = tick();
5151 151036 : uint64_t delta = now - state->tick_last;
5152 :
5153 : /* increment the time spent in computations */
5154 151036 : state->tick_raid += delta;
5155 :
5156 151036 : state->tick_last = now;
5157 151036 : }
5158 :
5159 813586 : void state_usage_hash(struct snapraid_state* state)
5160 : {
5161 813586 : uint64_t now = tick();
5162 813586 : uint64_t delta = now - state->tick_last;
5163 :
5164 : /* increment the time spent in computations */
5165 813586 : state->tick_hash += delta;
5166 :
5167 813586 : state->tick_last = now;
5168 813586 : }
5169 :
5170 977595 : void state_usage_file(struct snapraid_state* state, struct snapraid_disk* disk, struct snapraid_file* file)
5171 : {
5172 : (void)state;
5173 :
5174 977595 : disk->progress_file = file;
5175 977595 : }
5176 :
5177 982282 : void state_usage_disk(struct snapraid_state* state, struct snapraid_handle* handle_map, unsigned* waiting_map, unsigned waiting_mac)
5178 : {
5179 982282 : uint64_t now = tick();
5180 982282 : uint64_t delta = now - state->tick_last;
5181 : unsigned i;
5182 :
5183 : /* increment the time spent in the data disks */
5184 1052362 : for (i = 0; i < waiting_mac; ++i) {
5185 70080 : struct snapraid_disk* disk = handle_map[waiting_map[i]].disk;
5186 :
5187 70080 : if (!disk)
5188 0 : continue;
5189 :
5190 70080 : disk->tick += delta;
5191 : }
5192 982282 : state->tick_io += delta;
5193 :
5194 982282 : state->tick_last = now;
5195 982282 : }
5196 :
5197 797184 : void state_usage_parity(struct snapraid_state* state, unsigned* waiting_map, unsigned waiting_mac)
5198 : {
5199 797184 : uint64_t now = tick();
5200 797184 : uint64_t delta = now - state->tick_last;
5201 : unsigned i;
5202 :
5203 : /* increment the time spent in the parity disk */
5204 816133 : for (i = 0; i < waiting_mac; ++i)
5205 18949 : state->parity[waiting_map[i]].tick += delta;
5206 797184 : state->tick_io += delta;
5207 :
5208 797184 : state->tick_last = now;
5209 797184 : }
5210 :
5211 106 : void state_usage_print(struct snapraid_state* state)
5212 : {
5213 : /* set the latest data */
5214 106 : state_progress_latest(state);
5215 :
5216 106 : if (msg_level < MSG_PROGRESS)
5217 105 : return;
5218 :
5219 : /* print a graph for it */
5220 1 : state_progress_graph(state, 0, state->progress_ptr, PROGRESS_MAX);
5221 : }
5222 :
5223 538 : void state_fscheck(struct snapraid_state* state, const char* ope)
5224 : {
5225 : tommy_node* i;
5226 :
5227 : /* check the file-system on all disks */
5228 3760 : for (i = state->disklist; i != 0; i = i->next) {
5229 3222 : struct snapraid_disk* disk = i->data;
5230 :
5231 3222 : if (fs_check(disk) != 0) {
5232 : /* LCOV_EXCL_START */
5233 : log_fatal(EINTERNAL, "Internal inconsistency: File-system check for disk '%s' %s\n", disk->name, ope);
5234 : os_abort();
5235 : /* LCOV_EXCL_STOP */
5236 : }
5237 : }
5238 538 : }
5239 :
5240 1 : void generate_configuration(const char* path)
5241 : {
5242 : struct snapraid_state state;
5243 : struct snapraid_content* content;
5244 : char esc_buffer[ESC_MAX];
5245 : unsigned l, s;
5246 : tommy_node* j;
5247 :
5248 1 : state_init(&state);
5249 :
5250 : /* mark that we are without a configuration file */
5251 1 : state.no_conf = 1;
5252 :
5253 : /* create the dummy content entry */
5254 1 : content = content_alloc(path, -1);
5255 :
5256 : /* adds the content entry */
5257 1 : tommy_list_insert_tail(&state.contentlist, &content->node, content);
5258 :
5259 : /* read the content file */
5260 1 : state_read(&state);
5261 :
5262 : /* output a dummy configuration file */
5263 1 : printf("# Configuration file generated from %s\n", path);
5264 1 : printf("\n");
5265 1 : printf("# Use this blocksize\n");
5266 1 : printf("blocksize %u\n", state.block_size / KIBI);
5267 1 : printf("\n");
5268 1 : printf("# Use this hashsize\n");
5269 1 : printf("hashsize %u\n", BLOCK_HASH_SIZE);
5270 1 : printf("\n");
5271 7 : for (l = 0; l < state.level; ++l) {
5272 6 : printf("# Set the correct path for the %s files\n", lev_name(l));
5273 6 : printf("# You had %u of them:\n", state.parity[l].split_mac);
5274 30 : for (s = 0; s < state.parity[l].split_mac; ++s) {
5275 24 : printf("# %u:\n", s);
5276 24 : printf("# PATH:");
5277 24 : if (state.parity[l].split_map[s].path[0])
5278 24 : printf("%s", state.parity[l].split_map[s].path);
5279 : else
5280 0 : printf("?");
5281 24 : printf("\n");
5282 24 : printf("# SIZE:");
5283 24 : if (state.parity[l].split_map[s].size != PARITY_SIZE_INVALID)
5284 24 : printf("%" PRIu64, state.parity[l].split_map[s].size);
5285 : else
5286 0 : printf("?");
5287 24 : printf("\n");
5288 24 : printf("# UUID:");
5289 24 : if (state.parity[l].split_map[s].uuid[0])
5290 24 : printf("%s", state.parity[l].split_map[s].uuid);
5291 : else
5292 0 : printf("?");
5293 24 : printf("\n");
5294 24 : printf("#\n");
5295 : }
5296 6 : printf("%s ENTER_HERE_THE_PARITY_FILES_COMMA_SEPARATED\n", lev_config_name(l));
5297 6 : printf("\n");
5298 : }
5299 1 : printf("# Add any other content file\n");
5300 1 : printf("content %s\n", path);
5301 1 : printf("\n");
5302 7 : for (j = state.maplist; j; j = j->next) {
5303 6 : struct snapraid_map* map = j->data;
5304 : struct snapraid_disk* disk;
5305 6 : printf("# Set the correct dir for disk '%s'\n", map->name);
5306 6 : if (map->uuid[0])
5307 6 : printf("# Disk '%s' is the one with id '%s'\n", map->name, map->uuid);
5308 6 : disk = find_disk_by_name(&state, map->name);
5309 6 : if (disk && disk->filelist) {
5310 6 : struct snapraid_file* file = disk->filelist->data;
5311 6 : if (file) {
5312 6 : printf("# and containing: %s\n", fmt_poll(disk, file->sub, esc_buffer));
5313 : }
5314 : }
5315 6 : printf("data %s ENTER_HERE_THE_DIR\n", map->name);
5316 6 : printf("\n");
5317 : }
5318 :
5319 1 : state_done(&state);
5320 1 : }
5321 :
5322 :
5323 : /**
5324 : * Flush and sync the parity files.
5325 : */
5326 93 : int state_flush(struct snapraid_state* state, struct snapraid_io* io, struct snapraid_parity_handle* parity_handle, block_off_t blockcur)
5327 : {
5328 : unsigned l;
5329 :
5330 93 : if (io)
5331 93 : io_flush(io);
5332 :
5333 : /* flush all parity handles to ensure data is written to disk */
5334 606 : for (l = 0; l < state->level; ++l) {
5335 513 : int ret = parity_sync(&parity_handle[l]);
5336 513 : if (ret == -1) {
5337 : /* LCOV_EXCL_START */
5338 : log_tag("parity_error_io:%u:%s: Sync error. %s.\n", blockcur, lev_config_name(l), strerror(errno));
5339 : log_fatal(errno, "DANGER! Unexpected input/output error in disk %s. It isn't possible to continue.\n", lev_config_name(l));
5340 : return -1;
5341 : /* LCOV_EXCL_STOP */
5342 : }
5343 : }
5344 :
5345 93 : return 0;
5346 : }
5347 :
5348 66 : void state_load_ignore_file(tommy_list* filter_list, const char* path, const char* sub)
5349 : {
5350 : STREAM* f;
5351 : int line;
5352 :
5353 66 : f = sopen_read(path, 0);
5354 66 : if (!f) {
5355 : /* LCOV_EXCL_START */
5356 : log_error(errno, "Error opening the ignore file '%s'. %s.\n", path, strerror(errno));
5357 : return;
5358 : /* LCOV_EXCL_STOP */
5359 : }
5360 :
5361 66 : line = 1;
5362 66 : while (1) {
5363 : char buffer[PATH_MAX];
5364 : int ret;
5365 : int c;
5366 :
5367 : /* skip initial spaces */
5368 132 : sgetspace(f);
5369 :
5370 : /* get the whole line */
5371 132 : ret = sgetline(f, buffer, sizeof(buffer));
5372 132 : if (ret < 0) {
5373 : /* LCOV_EXCL_START */
5374 : log_error(EUSER, "Too long line in '%s' at line %u\n", path, line);
5375 : break;
5376 : /* LCOV_EXCL_STOP */
5377 : }
5378 :
5379 132 : if (buffer[0] == 0) {
5380 : /* allow empty lines */
5381 66 : } else if (buffer[0] == '#') {
5382 : /* ignore comment lines */
5383 : } else {
5384 : struct snapraid_filter* filter;
5385 :
5386 66 : filter = filter_alloc_file(-1, sub, buffer);
5387 66 : if (!filter) {
5388 : /* LCOV_EXCL_START */
5389 : log_error(EUSER, "Invalid ignore specification '%s' in '%s' at line %u\n", buffer, path, line);
5390 : break;
5391 : /* LCOV_EXCL_STOP */
5392 : }
5393 :
5394 66 : tommy_list_insert_tail(filter_list, &filter->node, filter);
5395 : }
5396 :
5397 : /* next line */
5398 132 : c = sgeteol(f);
5399 132 : if (c == EOF) {
5400 66 : break;
5401 : }
5402 66 : if (c != '\n') {
5403 : /* LCOV_EXCL_START */
5404 : log_error(EUSER, "Extra data in '%s' at line %u\n", path, line);
5405 : break;
5406 : /* LCOV_EXCL_STOP */
5407 : }
5408 66 : ++line;
5409 : }
5410 :
5411 66 : if (serror(f)) {
5412 : /* LCOV_EXCL_START */
5413 : log_error(errno, "Error reading the ignore file '%s' at line %u\n", path, line);
5414 : /* LCOV_EXCL_STOP */
5415 : }
5416 :
5417 66 : sclose(f);
5418 : }
5419 :
|