Line data Source code
1 : /*
2 : * Copyright (C) 2013 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 "support.h"
21 : #include "elem.h"
22 : #include "state.h"
23 : #include "parity.h"
24 : #include "handle.h"
25 : #include "io.h"
26 : #include "raid/raid.h"
27 :
28 : /****************************************************************************/
29 : /* scrub */
30 :
31 : /**
32 : * Buffer for storing the new hashes.
33 : */
34 : struct snapraid_rehash {
35 : unsigned char hash[HASH_MAX];
36 : struct snapraid_block* block;
37 : };
38 :
39 : /**
40 : * Scrub plan to use.
41 : */
42 : struct snapraid_plan {
43 : struct snapraid_state* state;
44 : int plan; /**< One of the SCRUB_*. */
45 : time_t timelimit; /**< Time limit. Valid only with SCRUB_AUTO. */
46 : block_off_t lastlimit; /**< Number of blocks allowed with time exactly at ::timelimit. */
47 : block_off_t countlast; /**< Counter of blocks with time exactly at ::timelimit. */
48 : };
49 :
50 : /**
51 : * Check if we have to process the specified block index ::i.
52 : */
53 112170 : static int block_is_enabled(void* void_plan, block_off_t i)
54 : {
55 112170 : struct snapraid_plan* plan = void_plan;
56 : time_t blocktime;
57 : snapraid_info info;
58 :
59 : /* don't scrub unused blocks in all plans */
60 112170 : info = info_get(&plan->state->infoarr, i);
61 112170 : if (info == 0)
62 0 : return 0;
63 :
64 : /* bad blocks are always scrubbed in all plans */
65 112170 : if (info_get_bad(info))
66 3544 : return 1;
67 :
68 108626 : switch (plan->plan) {
69 : case SCRUB_FULL :
70 : /* in 'full' plan everything is scrubbed */
71 17044 : return 1;
72 : case SCRUB_EVEN :
73 : /* in 'even' plan, scrub only even blocks */
74 18430 : return i % 2 == 0;
75 : case SCRUB_NEW :
76 : /* in 'sync' plan, only blocks never scrubbed */
77 9374 : return info_get_justsynced(info);
78 : case SCRUB_BAD :
79 : /* in 'bad' plan, only bad blocks (already reported) */
80 7534 : return 0;
81 : }
82 :
83 : /* if it's too new */
84 56244 : blocktime = info_get_time(info);
85 56244 : if (blocktime > plan->timelimit) {
86 : /* skip it */
87 9574 : return 0;
88 : }
89 :
90 : /* if the time is less than the limit, always include */
91 : /* otherwise, check if we reached the last limit count */
92 46670 : if (blocktime == plan->timelimit) {
93 : /* if we reached the count limit */
94 30132 : if (plan->countlast >= plan->lastlimit) {
95 : /* skip it */
96 24940 : return 0;
97 : }
98 :
99 5192 : ++plan->countlast;
100 : }
101 :
102 21730 : return 1;
103 : }
104 :
105 168178 : static void scrub_data_reader(struct snapraid_worker* worker, struct snapraid_task* task)
106 : {
107 168178 : struct snapraid_io* io = worker->io;
108 168178 : struct snapraid_state* state = io->state;
109 168178 : struct snapraid_handle* handle = worker->handle;
110 168178 : struct snapraid_disk* disk = handle->disk;
111 168178 : block_off_t blockcur = task->position;
112 168178 : unsigned char* buffer = task->buffer;
113 : int ret;
114 : char esc_buffer[ESC_MAX];
115 :
116 : /* if the disk position is not used */
117 168178 : if (!disk) {
118 : /* use an empty block */
119 0 : memset(buffer, 0, state->block_size);
120 0 : task->state = TASK_STATE_DONE;
121 1585 : return;
122 : }
123 :
124 : /* get the block */
125 168178 : task->block = fs_par2block_find(disk, blockcur);
126 :
127 : /* if the block is not used */
128 168143 : if (!block_has_file(task->block)) {
129 : /* use an empty block */
130 1585 : memset(buffer, 0, state->block_size);
131 1585 : task->state = TASK_STATE_DONE;
132 1585 : return;
133 : }
134 :
135 : /* get the file of this block */
136 166543 : task->file = fs_par2file_get(disk, blockcur, &task->file_pos);
137 :
138 : /* if the file is different than the current one, close it */
139 166579 : if (handle->file != 0 && handle->file != task->file) {
140 : /* keep a pointer at the file we are going to close for error reporting */
141 78065 : struct snapraid_file* report = handle->file;
142 78065 : ret = handle_close(handle);
143 78039 : if (ret == -1) {
144 : /* LCOV_EXCL_START */
145 : /* This one is really an unexpected error, because we are only reading */
146 : /* and closing a descriptor should never fail */
147 : if (errno == EIO) {
148 : log_tag("error:%u:%s:%s: Close EIO error. %s\n", blockcur, disk->name, esc_tag(report->sub, esc_buffer), strerror(errno));
149 : log_fatal("DANGER! Unexpected input/output close error in a data disk, it isn't possible to scrub.\n");
150 : log_fatal("Ensure that disk '%s' is sane and that file '%s' can be accessed.\n", disk->dir, handle->path);
151 : log_fatal("Stopping at block %u\n", blockcur);
152 : task->state = TASK_STATE_IOERROR;
153 : return;
154 : }
155 :
156 : log_tag("error:%u:%s:%s: Close error. %s\n", blockcur, disk->name, esc_tag(report->sub, esc_buffer), strerror(errno));
157 : log_fatal("WARNING! Unexpected close error in a data disk, it isn't possible to scrub.\n");
158 : log_fatal("Ensure that file '%s' can be accessed.\n", handle->path);
159 : log_fatal("Stopping at block %u\n", blockcur);
160 : task->state = TASK_STATE_ERROR;
161 : return;
162 : /* LCOV_EXCL_STOP */
163 : }
164 : }
165 :
166 166553 : ret = handle_open(handle, task->file, state->file_mode, log_error, 0);
167 166585 : if (ret == -1) {
168 0 : if (errno == EIO) {
169 : /* LCOV_EXCL_START */
170 : log_tag("error:%u:%s:%s: Open EIO error. %s\n", blockcur, disk->name, esc_tag(task->file->sub, esc_buffer), strerror(errno));
171 : log_fatal("DANGER! Unexpected input/output open error in a data disk, it isn't possible to scrub.\n");
172 : log_fatal("Ensure that disk '%s' is sane and that file '%s' can be accessed.\n", disk->dir, handle->path);
173 : log_fatal("Stopping at block %u\n", blockcur);
174 : task->state = TASK_STATE_IOERROR;
175 : return;
176 : /* LCOV_EXCL_STOP */
177 : }
178 :
179 0 : log_tag("error:%u:%s:%s: Open error. %s\n", blockcur, disk->name, esc_tag(task->file->sub, esc_buffer), strerror(errno));
180 0 : task->state = TASK_STATE_ERROR_CONTINUE;
181 0 : return;
182 : }
183 :
184 : /* check if the file is changed */
185 166585 : if (handle->st.st_size != task->file->size
186 166583 : || handle->st.st_mtime != task->file->mtime_sec
187 160991 : || STAT_NSEC(&handle->st) != task->file->mtime_nsec
188 : /* don't check the inode to support filesystem without persistent inodes */
189 : ) {
190 : /* report that the block and the file are not synced */
191 5595 : task->is_timestamp_different = 1;
192 : /* follow */
193 : }
194 :
195 : /* note that we intentionally don't abort if the file has different attributes */
196 : /* from the last sync, as we are expected to return errors if running */
197 : /* in an unsynced array. This is just like the check command. */
198 :
199 166585 : task->read_size = handle_read(handle, task->file_pos, buffer, state->block_size, log_error, 0);
200 166516 : if (task->read_size == -1) {
201 0 : if (errno == EIO) {
202 0 : log_tag("error:%u:%s:%s: Read EIO error at position %u. %s\n", blockcur, disk->name, esc_tag(task->file->sub, esc_buffer), task->file_pos, strerror(errno));
203 0 : log_error("Input/Output error in file '%s' at position '%u'\n", handle->path, task->file_pos);
204 0 : task->state = TASK_STATE_IOERROR_CONTINUE;
205 0 : return;
206 : }
207 :
208 0 : log_tag("error:%u:%s:%s: Read error at position %u. %s\n", blockcur, disk->name, esc_tag(task->file->sub, esc_buffer), task->file_pos, strerror(errno));
209 0 : task->state = TASK_STATE_ERROR_CONTINUE;
210 0 : return;
211 : }
212 :
213 : /* store the path of the opened file */
214 166516 : pathcpy(task->path, sizeof(task->path), handle->path);
215 :
216 166568 : task->state = TASK_STATE_DONE;
217 : }
218 :
219 168184 : static void scrub_parity_reader(struct snapraid_worker* worker, struct snapraid_task* task)
220 : {
221 168184 : struct snapraid_io* io = worker->io;
222 168184 : struct snapraid_state* state = io->state;
223 168184 : struct snapraid_parity_handle* parity_handle = worker->parity_handle;
224 168184 : unsigned level = parity_handle->level;
225 168184 : block_off_t blockcur = task->position;
226 168184 : unsigned char* buffer = task->buffer;
227 : int ret;
228 :
229 : /* read the parity */
230 168184 : ret = parity_read(parity_handle, blockcur, buffer, state->block_size, log_error);
231 168168 : if (ret == -1) {
232 0 : if (errno == EIO) {
233 0 : log_tag("parity_error:%u:%s: Read EIO error. %s\n", blockcur, lev_config_name(level), strerror(errno));
234 0 : log_error("Input/Output error in parity '%s' at position '%u'\n", lev_config_name(level), blockcur);
235 0 : task->state = TASK_STATE_IOERROR_CONTINUE;
236 0 : return;
237 : }
238 :
239 0 : log_tag("parity_error:%u:%s: Read error. %s\n", blockcur, lev_config_name(level), strerror(errno));
240 0 : task->state = TASK_STATE_ERROR_CONTINUE;
241 0 : return;
242 : }
243 :
244 168168 : task->state = TASK_STATE_DONE;
245 : }
246 :
247 11 : static int state_scrub_process(struct snapraid_state* state, struct snapraid_parity_handle* parity_handle, block_off_t blockstart, block_off_t blockmax, struct snapraid_plan* plan, time_t now)
248 : {
249 : struct snapraid_io io;
250 : struct snapraid_handle* handle;
251 : void* rehandle_alloc;
252 : struct snapraid_rehash* rehandle;
253 : unsigned diskmax;
254 : block_off_t blockcur;
255 : unsigned j;
256 : unsigned buffermax;
257 : data_off_t countsize;
258 : block_off_t countpos;
259 : block_off_t countmax;
260 : block_off_t autosavedone;
261 : block_off_t autosavelimit;
262 : block_off_t autosavemissing;
263 : int ret;
264 : unsigned error;
265 : unsigned silent_error;
266 : unsigned io_error;
267 : unsigned l;
268 : unsigned* waiting_map;
269 : unsigned waiting_mac;
270 : char esc_buffer[ESC_MAX];
271 :
272 : /* maps the disks to handles */
273 11 : handle = handle_mapping(state, &diskmax);
274 :
275 : /* rehash buffers */
276 11 : rehandle = malloc_nofail_align(diskmax * sizeof(struct snapraid_rehash), &rehandle_alloc);
277 :
278 : /* we need 1 * data + 2 * parity */
279 11 : buffermax = diskmax + 2 * state->level;
280 :
281 : /* initialize the io threads */
282 11 : io_init(&io, state, state->opt.io_cache, buffermax, scrub_data_reader, handle, diskmax, scrub_parity_reader, 0, parity_handle, state->level);
283 :
284 : /* possibly waiting disks */
285 11 : waiting_mac = diskmax > RAID_PARITY_MAX ? diskmax : RAID_PARITY_MAX;
286 11 : waiting_map = malloc_nofail(waiting_mac * sizeof(unsigned));
287 :
288 11 : error = 0;
289 11 : silent_error = 0;
290 11 : io_error = 0;
291 :
292 : /* first count the number of blocks to process */
293 11 : countmax = 0;
294 11 : plan->countlast = 0;
295 56096 : for (blockcur = blockstart; blockcur < blockmax; ++blockcur) {
296 56085 : if (!block_is_enabled(plan, blockcur))
297 28053 : continue;
298 28032 : ++countmax;
299 : }
300 :
301 : /* compute the autosave size for all disk, even if not read */
302 : /* this makes sense because the speed should be almost the same */
303 : /* if the disks are read in parallel */
304 11 : autosavelimit = state->autosave / (diskmax * state->block_size);
305 11 : autosavemissing = countmax; /* blocks to do */
306 11 : autosavedone = 0; /* blocks done */
307 :
308 : /* drop until now */
309 11 : state_usage_waste(state);
310 :
311 11 : countsize = 0;
312 11 : countpos = 0;
313 11 : plan->countlast = 0;
314 :
315 : /* start all the worker threads */
316 11 : io_start(&io, blockstart, blockmax, &block_is_enabled, plan);
317 :
318 11 : state_progress_begin(state, blockstart, blockmax, countmax);
319 : while (1) {
320 : unsigned char* buffer_recov[LEV_MAX];
321 : snapraid_info info;
322 : int error_on_this_block;
323 : int silent_error_on_this_block;
324 : int io_error_on_this_block;
325 : int block_is_unsynced;
326 : int rehash;
327 : void** buffer;
328 :
329 : /* go to the next block */
330 28043 : blockcur = io_read_next(&io, &buffer);
331 28043 : if (blockcur >= blockmax)
332 22 : break;
333 :
334 : /* until now is scheduling */
335 28032 : state_usage_sched(state);
336 :
337 : /* one more block processed for autosave */
338 28032 : ++autosavedone;
339 28032 : --autosavemissing;
340 :
341 : /* by default process the block, and skip it if something goes wrong */
342 28032 : error_on_this_block = 0;
343 28032 : silent_error_on_this_block = 0;
344 28032 : io_error_on_this_block = 0;
345 :
346 : /* if all the blocks at this address are synced */
347 : /* if not, parity is not even checked */
348 28032 : block_is_unsynced = 0;
349 :
350 : /* get block specific info */
351 28032 : info = info_get(&state->infoarr, blockcur);
352 :
353 : /* if we have to use the old hash */
354 28032 : rehash = info_get_rehash(info);
355 :
356 : /* for each disk, process the block */
357 196224 : for (j = 0; j < diskmax; ++j) {
358 : struct snapraid_task* task;
359 : int read_size;
360 : unsigned char hash[HASH_MAX];
361 : struct snapraid_block* block;
362 : int file_is_unsynced;
363 : struct snapraid_disk* disk;
364 : struct snapraid_file* file;
365 : block_off_t file_pos;
366 : unsigned diskcur;
367 :
368 : /* if the file on this disk is synced */
369 : /* if not, silent errors are assumed as expected error */
370 168192 : file_is_unsynced = 0;
371 :
372 : /* until now is misc */
373 168192 : state_usage_misc(state);
374 :
375 : /* get the next task */
376 168192 : task = io_data_read(&io, &diskcur, waiting_map, &waiting_mac);
377 :
378 : /* until now is disk */
379 168192 : state_usage_disk(state, handle, waiting_map, waiting_mac);
380 :
381 : /* get the task results */
382 168192 : disk = task->disk;
383 168192 : block = task->block;
384 168192 : file = task->file;
385 168192 : file_pos = task->file_pos;
386 168192 : read_size = task->read_size;
387 :
388 : /* by default no rehash in case of "continue" */
389 168192 : rehandle[diskcur].block = 0;
390 :
391 : /* if the disk position is not used */
392 168192 : if (!disk)
393 2505 : continue;
394 :
395 : /* if the block is unsynced, errors are expected */
396 168192 : if (block_has_invalid_parity(block)) {
397 : /* report that the block and the file are not synced */
398 0 : block_is_unsynced = 1;
399 0 : file_is_unsynced = 1;
400 : /* follow */
401 : }
402 :
403 : /* if the block is not used */
404 168192 : if (!block_has_file(block))
405 1585 : continue;
406 :
407 : /* if the block is unsynced, errors are expected */
408 166607 : if (task->is_timestamp_different) {
409 : /* report that the block and the file are not synced */
410 5595 : block_is_unsynced = 1;
411 5595 : file_is_unsynced = 1;
412 : /* follow */
413 : }
414 :
415 : /* handle error conditions */
416 166607 : if (task->state == TASK_STATE_IOERROR) {
417 0 : ++io_error;
418 0 : goto bail;
419 : }
420 166607 : if (task->state == TASK_STATE_ERROR) {
421 0 : ++error;
422 0 : goto bail;
423 : }
424 166607 : if (task->state == TASK_STATE_ERROR_CONTINUE) {
425 0 : ++error;
426 0 : error_on_this_block = 1;
427 0 : continue;
428 : }
429 166607 : if (task->state == TASK_STATE_IOERROR_CONTINUE) {
430 0 : ++io_error;
431 0 : if (io_error >= state->opt.io_error_limit) {
432 : /* LCOV_EXCL_START */
433 : log_fatal("DANGER! Too many input/output read error in a data disk, it isn't possible to scrub.\n");
434 : log_fatal("Ensure that disk '%s' is sane and that file '%s' can be accessed.\n", disk->dir, task->path);
435 : log_fatal("Stopping at block %u\n", blockcur);
436 : goto bail;
437 : /* LCOV_EXCL_STOP */
438 : }
439 :
440 : /* otherwise continue */
441 0 : io_error_on_this_block = 1;
442 0 : continue;
443 : }
444 166607 : if (task->state != TASK_STATE_DONE) {
445 : /* LCOV_EXCL_START */
446 : log_fatal("Internal inconsistency in task state\n");
447 : os_abort();
448 : /* LCOV_EXCL_STOP */
449 : }
450 :
451 166607 : countsize += read_size;
452 :
453 : /* now compute the hash */
454 166607 : if (rehash) {
455 27247 : memhash(state->prevhash, state->prevhashseed, hash, buffer[diskcur], read_size);
456 :
457 : /* compute the new hash, and store it */
458 27247 : rehandle[diskcur].block = block;
459 27247 : memhash(state->hash, state->hashseed, rehandle[diskcur].hash, buffer[diskcur], read_size);
460 : } else {
461 139360 : memhash(state->hash, state->hashseed, hash, buffer[diskcur], read_size);
462 : }
463 :
464 : /* until now is hash */
465 166607 : state_usage_hash(state);
466 :
467 166607 : if (block_has_updated_hash(block)) {
468 : /* compare the hash */
469 166607 : if (memcmp(hash, block->hash, BLOCK_HASH_SIZE) != 0) {
470 920 : unsigned diff = memdiff(hash, block->hash, BLOCK_HASH_SIZE);
471 :
472 920 : log_tag("error:%u:%s:%s: Data error at position %u, diff bits %u\n", blockcur, disk->name, esc_tag(file->sub, esc_buffer), file_pos, diff);
473 :
474 : /* it's a silent error only if we are dealing with synced files */
475 920 : if (file_is_unsynced) {
476 0 : ++error;
477 0 : error_on_this_block = 1;
478 : } else {
479 920 : log_error("Data error in file '%s' at position '%u', diff bits %u\n", task->path, file_pos, diff);
480 920 : ++silent_error;
481 920 : silent_error_on_this_block = 1;
482 : }
483 920 : continue;
484 : }
485 : }
486 : }
487 :
488 : /* buffers for parity read and not computed */
489 196224 : for (l = 0; l < state->level; ++l)
490 168192 : buffer_recov[l] = buffer[diskmax + state->level + l];
491 28032 : for (; l < LEV_MAX; ++l)
492 0 : buffer_recov[l] = 0;
493 :
494 : /* until now is misc */
495 28032 : state_usage_misc(state);
496 :
497 : /* read the parity */
498 196224 : for (l = 0; l < state->level; ++l) {
499 : struct snapraid_task* task;
500 : unsigned levcur;
501 :
502 168192 : task = io_parity_read(&io, &levcur, waiting_map, &waiting_mac);
503 :
504 : /* until now is parity */
505 168192 : state_usage_parity(state, waiting_map, waiting_mac);
506 :
507 : /* handle error conditions */
508 168192 : if (task->state == TASK_STATE_IOERROR) {
509 0 : ++io_error;
510 0 : goto bail;
511 : }
512 168192 : if (task->state == TASK_STATE_ERROR) {
513 0 : ++error;
514 0 : goto bail;
515 : }
516 168192 : if (task->state == TASK_STATE_ERROR_CONTINUE) {
517 0 : ++error;
518 0 : error_on_this_block = 1;
519 :
520 : /* if continuing on error, clear the missing buffer */
521 0 : buffer_recov[levcur] = 0;
522 0 : continue;
523 : }
524 168192 : if (task->state == TASK_STATE_IOERROR_CONTINUE) {
525 0 : ++io_error;
526 0 : if (io_error >= state->opt.io_error_limit) {
527 : /* LCOV_EXCL_START */
528 : log_fatal("DANGER! Too many input/output read error in the %s disk, it isn't possible to scrub.\n", lev_name(levcur));
529 : log_fatal("Ensure that disk '%s' is sane and can be read.\n", lev_config_name(levcur));
530 : log_fatal("Stopping at block %u\n", blockcur);
531 : goto bail;
532 : /* LCOV_EXCL_STOP */
533 : }
534 :
535 : /* otherwise continue */
536 0 : io_error_on_this_block = 1;
537 :
538 : /* if continuing on error, clear the missing buffer */
539 0 : buffer_recov[levcur] = 0;
540 0 : continue;
541 : }
542 168192 : if (task->state != TASK_STATE_DONE) {
543 : /* LCOV_EXCL_START */
544 : log_fatal("Internal inconsistency in task state\n");
545 : os_abort();
546 : /* LCOV_EXCL_STOP */
547 : }
548 : }
549 :
550 : /* if we have read all the data required and it's correct, proceed with the parity check */
551 28032 : if (!error_on_this_block && !silent_error_on_this_block && !io_error_on_this_block) {
552 :
553 : /* compute the parity */
554 27112 : raid_gen(diskmax, state->level, state->block_size, buffer);
555 :
556 : /* compare the parity */
557 189784 : for (l = 0; l < state->level; ++l) {
558 162672 : if (buffer_recov[l] && memcmp(buffer[diskmax + l], buffer_recov[l], state->block_size) != 0) {
559 0 : unsigned diff = memdiff(buffer[diskmax + l], buffer_recov[l], state->block_size);
560 :
561 0 : log_tag("parity_error:%u:%s: Data error, diff bits %u\n", blockcur, lev_config_name(l), diff);
562 :
563 : /* it's a silent error only if we are dealing with synced blocks */
564 0 : if (block_is_unsynced) {
565 0 : ++error;
566 0 : error_on_this_block = 1;
567 : } else {
568 0 : log_fatal("Data error in parity '%s' at position '%u', diff bits %u\n", lev_config_name(l), blockcur, diff);
569 0 : ++silent_error;
570 0 : silent_error_on_this_block = 1;
571 : }
572 : }
573 : }
574 :
575 : /* until now is raid */
576 27112 : state_usage_raid(state);
577 : }
578 :
579 28032 : if (silent_error_on_this_block || io_error_on_this_block) {
580 : /* set the error status keeping other info */
581 920 : info_set(&state->infoarr, blockcur, info_set_bad(info));
582 27112 : } else if (error_on_this_block) {
583 : /* do nothing, as this is a generic error */
584 : /* likely caused by a not synced array */
585 : } else {
586 : /* if rehash is needed */
587 27112 : if (rehash) {
588 : /* store all the new hash already computed */
589 32256 : for (j = 0; j < diskmax; ++j) {
590 27648 : if (rehandle[j].block)
591 27247 : memcpy(rehandle[j].block->hash, rehandle[j].hash, BLOCK_HASH_SIZE);
592 : }
593 : }
594 :
595 : /* update the time info of the block */
596 : /* and clear any other flag */
597 27112 : info_set(&state->infoarr, blockcur, info_make(now, 0, 0, 0));
598 : }
599 :
600 : /* mark the state as needing write */
601 28032 : state->need_write = 1;
602 :
603 : /* count the number of processed block */
604 28032 : ++countpos;
605 :
606 : /* progress */
607 28032 : if (state_progress(state, &io, blockcur, countpos, countmax, countsize)) {
608 : /* LCOV_EXCL_START */
609 : break;
610 : /* LCOV_EXCL_STOP */
611 : }
612 :
613 : /* autosave */
614 28032 : if (state->autosave != 0
615 0 : && autosavedone >= autosavelimit /* if we have reached the limit */
616 0 : && autosavemissing >= autosavelimit /* if we have at least a full step to do */
617 : ) {
618 0 : autosavedone = 0; /* restart the counter */
619 :
620 : /* until now is misc */
621 0 : state_usage_misc(state);
622 :
623 0 : state_progress_stop(state);
624 :
625 0 : msg_progress("Autosaving...\n");
626 0 : state_write(state);
627 :
628 0 : state_progress_restart(state);
629 :
630 : /* drop until now */
631 0 : state_usage_waste(state);
632 : }
633 28032 : }
634 :
635 11 : state_progress_end(state, countpos, countmax, countsize);
636 :
637 11 : state_usage_print(state);
638 :
639 11 : if (error || silent_error || io_error) {
640 1 : msg_status("\n");
641 1 : msg_status("%8u file errors\n", error);
642 1 : msg_status("%8u io errors\n", io_error);
643 1 : msg_status("%8u data errors\n", silent_error);
644 : } else {
645 : /* print the result only if processed something */
646 10 : if (countpos != 0)
647 9 : msg_status("Everything OK\n");
648 : }
649 :
650 11 : if (error)
651 0 : log_fatal("WARNING! Unexpected file errors!\n");
652 11 : if (io_error)
653 0 : log_fatal("DANGER! Unexpected input/output errors! The failing blocks are now marked as bad!\n");
654 11 : if (silent_error)
655 1 : log_fatal("DANGER! Unexpected data errors! The failing blocks are now marked as bad!\n");
656 11 : if (io_error || silent_error) {
657 1 : log_fatal("Use 'snapraid status' to list the bad blocks.\n");
658 1 : log_fatal("Use 'snapraid -e fix' to recover.\n");
659 : }
660 :
661 11 : log_tag("summary:error_file:%u\n", error);
662 11 : log_tag("summary:error_io:%u\n", io_error);
663 11 : log_tag("summary:error_data:%u\n", silent_error);
664 11 : if (error + silent_error + io_error == 0)
665 10 : log_tag("summary:exit:ok\n");
666 : else
667 1 : log_tag("summary:exit:error\n");
668 11 : log_flush();
669 :
670 : bail:
671 : /* stop all the worker threads */
672 11 : io_stop(&io);
673 :
674 77 : for (j = 0; j < diskmax; ++j) {
675 66 : struct snapraid_file* file = handle[j].file;
676 66 : struct snapraid_disk* disk = handle[j].disk;
677 66 : ret = handle_close(&handle[j]);
678 66 : if (ret == -1) {
679 : /* LCOV_EXCL_START */
680 : log_tag("error:%u:%s:%s: Close error. %s\n", blockcur, disk->name, esc_tag(file->sub, esc_buffer), strerror(errno));
681 : log_fatal("DANGER! Unexpected close error in a data disk.\n");
682 : ++error;
683 : /* continue, as we are already exiting */
684 : /* LCOV_EXCL_STOP */
685 : }
686 : }
687 :
688 11 : free(handle);
689 11 : free(rehandle_alloc);
690 11 : free(waiting_map);
691 11 : io_done(&io);
692 :
693 11 : if (state->opt.expect_recoverable) {
694 1 : if (error + silent_error + io_error == 0)
695 0 : return -1;
696 : } else {
697 10 : if (error + silent_error + io_error != 0)
698 0 : return -1;
699 : }
700 11 : return 0;
701 : }
702 :
703 : /**
704 : * Return a * b / c approximated to the upper value.
705 : */
706 2 : static uint32_t md(uint32_t a, uint32_t b, uint32_t c)
707 : {
708 2 : uint64_t v = a;
709 :
710 2 : v *= b;
711 2 : v += c - 1;
712 2 : v /= c;
713 :
714 2 : return v;
715 : }
716 :
717 11 : int state_scrub(struct snapraid_state* state, int plan, int olderthan)
718 : {
719 : block_off_t blockmax;
720 : block_off_t countlimit;
721 : block_off_t i;
722 : block_off_t count;
723 : time_t recentlimit;
724 : int ret;
725 : struct snapraid_parity_handle parity_handle[LEV_MAX];
726 : struct snapraid_plan ps;
727 : time_t* timemap;
728 : unsigned error;
729 : time_t now;
730 : unsigned l;
731 :
732 : /* get the present time */
733 11 : now = time(0);
734 :
735 11 : msg_progress("Initializing...\n");
736 :
737 11 : if ((plan == SCRUB_BAD || plan == SCRUB_NEW || plan == SCRUB_FULL)
738 4 : && olderthan >= 0) {
739 : /* LCOV_EXCL_START */
740 : log_fatal("You can specify -o, --older-than only with a numeric percentage.\n");
741 : exit(EXIT_FAILURE);
742 : /* LCOV_EXCL_STOP */
743 : }
744 :
745 11 : blockmax = parity_allocated_size(state);
746 :
747 : /* preinitialize to avoid warnings */
748 11 : countlimit = 0;
749 11 : recentlimit = 0;
750 :
751 11 : ps.state = state;
752 11 : if (state->opt.force_scrub_even) {
753 1 : ps.plan = SCRUB_EVEN;
754 10 : } else if (plan == SCRUB_FULL) {
755 2 : ps.plan = SCRUB_FULL;
756 8 : } else if (plan == SCRUB_NEW) {
757 1 : ps.plan = SCRUB_NEW;
758 7 : } else if (plan == SCRUB_BAD) {
759 1 : ps.plan = SCRUB_BAD;
760 6 : } else if (state->opt.force_scrub_at) {
761 : /* scrub the specified amount of blocks */
762 4 : ps.plan = SCRUB_AUTO;
763 4 : countlimit = state->opt.force_scrub_at;
764 4 : recentlimit = now;
765 : } else {
766 2 : ps.plan = SCRUB_AUTO;
767 2 : if (plan >= 0) {
768 1 : countlimit = md(blockmax, plan, 100);
769 : } else {
770 : /* by default scrub 8.33% of the array (100/12=8.(3)) */
771 1 : countlimit = md(blockmax, 1, 12);
772 : }
773 :
774 2 : if (olderthan >= 0) {
775 1 : recentlimit = now - olderthan * 24 * 3600;
776 : } else {
777 : /* by default use a 10 day time limit */
778 1 : recentlimit = now - 10 * 24 * 3600;
779 : }
780 : }
781 :
782 : /* identify the time limit */
783 : /* we sort all the block times, and we identify the time limit for which we reach the quota */
784 : /* this allow to process first the oldest blocks */
785 11 : timemap = malloc_nofail(blockmax * sizeof(time_t));
786 :
787 : /* copy the info in the temp vector */
788 11 : count = 0;
789 11 : log_tag("block_count:%u\n", blockmax);
790 56096 : for (i = 0; i < blockmax; ++i) {
791 56085 : snapraid_info info = info_get(&state->infoarr, i);
792 :
793 : /* skip unused blocks */
794 56085 : if (info == 0)
795 0 : continue;
796 :
797 56085 : timemap[count++] = info_get_time(info);
798 : }
799 :
800 11 : if (!count) {
801 : /* LCOV_EXCL_START */
802 : log_fatal("The array appears to be empty.\n");
803 : exit(EXIT_FAILURE);
804 : /* LCOV_EXCL_STOP */
805 : }
806 :
807 : /* sort it */
808 11 : qsort(timemap, count, sizeof(time_t), time_compare);
809 :
810 : /* output the info map */
811 11 : i = 0;
812 11 : log_tag("info_count:%u\n", count);
813 40 : while (i < count) {
814 18 : unsigned j = i + 1;
815 56103 : while (j < count && timemap[i] == timemap[j])
816 56067 : ++j;
817 18 : log_tag("info_time:%" PRIu64 ":%u\n", (uint64_t)timemap[i], j - i);
818 18 : i = j;
819 : }
820 :
821 : /* compute the limits from count/recentlimit */
822 11 : if (ps.plan == SCRUB_AUTO) {
823 : /* no more than the full count */
824 6 : if (countlimit > count)
825 2 : countlimit = count;
826 :
827 : /* decrease until we reach the specific recentlimit */
828 59 : while (countlimit > 0 && timemap[countlimit - 1] > recentlimit)
829 47 : --countlimit;
830 :
831 : /* if there is something to scrub */
832 6 : if (countlimit > 0) {
833 : /* get the most recent time we want to scrub */
834 5 : ps.timelimit = timemap[countlimit - 1];
835 :
836 : /* count how many entries for this exact time we have to scrub */
837 : /* if the blocks have all the same time, we end with countlimit == lastlimit */
838 5 : ps.lastlimit = 1;
839 2601 : while (countlimit > ps.lastlimit && timemap[countlimit - ps.lastlimit - 1] == ps.timelimit)
840 2591 : ++ps.lastlimit;
841 : } else {
842 : /* if nothing to scrub, disable also other limits */
843 1 : ps.timelimit = 0;
844 1 : ps.lastlimit = 0;
845 : }
846 :
847 6 : log_tag("count_limit:%u\n", countlimit);
848 6 : log_tag("time_limit:%" PRIu64 "\n", (uint64_t)ps.timelimit);
849 6 : log_tag("last_limit:%u\n", ps.lastlimit);
850 : }
851 :
852 : /* free the temp vector */
853 11 : free(timemap);
854 :
855 : /* open the file for reading */
856 77 : for (l = 0; l < state->level; ++l) {
857 66 : ret = parity_open(&parity_handle[l], &state->parity[l], l, state->file_mode, state->block_size, state->opt.parity_limit_size);
858 66 : if (ret == -1) {
859 : /* LCOV_EXCL_START */
860 : log_fatal("WARNING! Without an accessible %s file, it isn't possible to scrub.\n", lev_name(l));
861 : exit(EXIT_FAILURE);
862 : /* LCOV_EXCL_STOP */
863 : }
864 : }
865 :
866 11 : msg_progress("Scrubbing...\n");
867 :
868 11 : error = 0;
869 :
870 11 : ret = state_scrub_process(state, parity_handle, 0, blockmax, &ps, now);
871 11 : if (ret == -1) {
872 0 : ++error;
873 : /* continue, as we are already exiting */
874 : }
875 :
876 77 : for (l = 0; l < state->level; ++l) {
877 66 : ret = parity_close(&parity_handle[l]);
878 66 : if (ret == -1) {
879 : /* LCOV_EXCL_START */
880 : log_fatal("DANGER! Unexpected close error in %s disk.\n", lev_name(l));
881 : ++error;
882 : /* continue, as we are already exiting */
883 : /* LCOV_EXCL_STOP */
884 : }
885 : }
886 :
887 : /* abort if required */
888 11 : if (error != 0)
889 0 : return -1;
890 11 : return 0;
891 : }
892 :
|