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 "util.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 : /* dry */
30 :
31 0 : static const char* es(int err)
32 : {
33 0 : if (is_hw(err))
34 0 : return "error_io";
35 : else
36 0 : return "error";
37 : }
38 :
39 57102 : static void dry_data_reader(struct snapraid_worker* worker, struct snapraid_task* task)
40 : {
41 57102 : struct snapraid_io* io = worker->io;
42 57102 : struct snapraid_state* state = io->state;
43 57102 : struct snapraid_handle* handle = worker->handle;
44 57102 : struct snapraid_disk* disk = handle->disk;
45 57102 : block_off_t blockcur = task->position;
46 57102 : unsigned char* buffer = task->buffer;
47 : int ret;
48 : char esc_buffer[ESC_MAX];
49 :
50 : /* if the disk position is not used */
51 57102 : if (!disk) {
52 : /* use an empty block */
53 4687 : memset(buffer, 0, state->block_size);
54 4687 : task->state = TASK_STATE_DONE;
55 5528 : return;
56 : }
57 :
58 : /* get the block */
59 52415 : task->block = fs_par2block_find(disk, blockcur);
60 :
61 : /* if the block is not used */
62 52415 : if (!block_has_file(task->block)) {
63 : /* use an empty block */
64 841 : memset(buffer, 0, state->block_size);
65 841 : task->state = TASK_STATE_DONE;
66 841 : return;
67 : }
68 :
69 : /* get the file of this block */
70 51574 : task->file = fs_par2file_get(disk, blockcur, &task->file_pos);
71 :
72 : /* if the file is different than the current one, close it */
73 51574 : if (handle->file != 0 && handle->file != task->file) {
74 : /* keep a pointer at the file we are going to close for error reporting */
75 20803 : struct snapraid_file* report = handle->file;
76 20803 : ret = handle_close(handle);
77 20803 : if (ret == -1) {
78 : /* LCOV_EXCL_START */
79 : /* This one is really an unexpected error, because we are only reading */
80 : /* and closing a descriptor should never fail */
81 : log_tag("%s:%u:%s:%s: Close error. %s.\n", es(errno), blockcur, disk->name, esc_tag(report->sub, esc_buffer), strerror(errno));
82 : log_fatal_errno(errno, disk->name);
83 : log_fatal(errno, "Stopping at block %u\n", blockcur);
84 :
85 : if (is_hw(errno)) {
86 : task->state = TASK_STATE_IOERROR;
87 : } else {
88 : task->state = TASK_STATE_ERROR;
89 : }
90 : return;
91 : /* LCOV_EXCL_STOP */
92 : }
93 : }
94 :
95 51574 : ret = handle_open(handle, task->file, state->file_mode, log_error, 0); /* for missing file don't output a message */
96 51574 : if (ret == -1) {
97 0 : log_tag("%s:%u:%s:%s: Open error. %s.\n", es(errno), blockcur, disk->name, esc_tag(task->file->sub, esc_buffer), strerror(errno));
98 0 : if (is_hw(errno)) {
99 : /* LCOV_EXCL_START */
100 : log_fatal_errno(errno, disk->name);
101 : log_fatal(errno, "Stopping at block %u\n", blockcur);
102 : task->state = TASK_STATE_IOERROR;
103 : return;
104 : /* LCOV_EXCL_STOP */
105 : }
106 :
107 0 : task->state = TASK_STATE_ERROR_CONTINUE;
108 0 : return;
109 : }
110 :
111 51574 : task->read_size = handle_read(handle, task->file_pos, buffer, state->block_size, log_error, 0);
112 51574 : if (task->read_size == -1) {
113 0 : log_tag("%s:%u:%s:%s: Read error at position %u. %s.\n", es(errno), blockcur, disk->name, esc_tag(task->file->sub, esc_buffer), task->file_pos, strerror(errno));
114 0 : if (is_hw(errno)) {
115 : /* LCOV_EXCL_START */
116 : log_error_errno(errno, disk->name);
117 : task->state = TASK_STATE_IOERROR_CONTINUE;
118 : return;
119 : /* LCOV_EXCL_STOP */
120 : }
121 :
122 0 : task->state = TASK_STATE_ERROR_CONTINUE;
123 0 : return;
124 : }
125 :
126 : /* store the path of the opened file */
127 51574 : pathcpy(task->path, sizeof(task->path), handle->path);
128 :
129 51574 : task->state = TASK_STATE_DONE;
130 : }
131 :
132 57102 : static void dry_parity_reader(struct snapraid_worker* worker, struct snapraid_task* task)
133 : {
134 57102 : struct snapraid_io* io = worker->io;
135 57102 : struct snapraid_state* state = io->state;
136 57102 : struct snapraid_parity_handle* parity_handle = worker->parity_handle;
137 57102 : unsigned level = parity_handle->level;
138 57102 : block_off_t blockcur = task->position;
139 57102 : unsigned char* buffer = task->buffer;
140 : int ret;
141 :
142 : /* read the parity */
143 57102 : ret = parity_read(parity_handle, blockcur, buffer, state->block_size, log_error);
144 57102 : if (ret == -1) {
145 0 : log_tag("parity_%s:%u:%s: Read error. %s.\n", es(errno), blockcur, lev_config_name(level), strerror(errno));
146 0 : if (is_hw(errno)) {
147 : /* LCOV_EXCL_START */
148 : log_error_errno(errno, lev_config_name(level));
149 : task->state = TASK_STATE_IOERROR_CONTINUE;
150 : return;
151 : /* LCOV_EXCL_STOP */
152 : }
153 :
154 0 : task->state = TASK_STATE_ERROR_CONTINUE;
155 0 : return;
156 : }
157 :
158 57102 : task->state = TASK_STATE_DONE;
159 : }
160 :
161 3 : static int state_dry_process(struct snapraid_state* state, struct snapraid_parity_handle* parity_handle, block_off_t blockstart, block_off_t blockmax)
162 : {
163 : struct snapraid_io io;
164 : struct snapraid_handle* handle;
165 : unsigned diskmax;
166 : block_off_t blockcur;
167 : unsigned j;
168 : unsigned buffermax;
169 : int ret;
170 : data_off_t countsize;
171 : block_off_t countpos;
172 : block_off_t countmax;
173 : unsigned soft_error;
174 : unsigned io_error;
175 : unsigned l;
176 : unsigned* waiting_map;
177 : unsigned waiting_mac;
178 : char esc_buffer[ESC_MAX];
179 :
180 3 : handle = handle_mapping(state, &diskmax);
181 :
182 : /* we need 1 * data + 2 * parity */
183 3 : buffermax = diskmax + 2 * state->level;
184 :
185 : /* initialize the io threads */
186 3 : io_init(&io, state, state->opt.io_cache, buffermax, dry_data_reader, handle, diskmax, dry_parity_reader, 0, parity_handle, state->level);
187 :
188 : /* possibly waiting disks */
189 3 : waiting_mac = diskmax > RAID_PARITY_MAX ? diskmax : RAID_PARITY_MAX;
190 3 : waiting_map = malloc_nofail(waiting_mac * sizeof(unsigned));
191 :
192 3 : soft_error = 0;
193 3 : io_error = 0;
194 :
195 : /* drop until now */
196 3 : state_usage_waste(state);
197 :
198 3 : countmax = blockmax - blockstart;
199 3 : countsize = 0;
200 3 : countpos = 0;
201 :
202 : /* start all the worker threads */
203 3 : io_start(&io, blockstart, blockmax, 0);
204 :
205 3 : blockcur = blockstart;
206 :
207 3 : int alert = state_progress_begin(state, blockstart, blockmax, countmax);
208 3 : if (alert > 0)
209 0 : goto end;
210 3 : if (alert < 0)
211 0 : goto bail;
212 :
213 9517 : while (1) {
214 : void** buffer;
215 :
216 : /* go to the next block */
217 9520 : blockcur = io_read_next(&io, &buffer);
218 9520 : if (blockcur >= blockmax)
219 3 : break;
220 :
221 : /* until now is scheduling */
222 9517 : state_usage_sched(state);
223 :
224 : /* for each disk, process the block */
225 66619 : for (j = 0; j < diskmax; ++j) {
226 : struct snapraid_task* task;
227 : int read_size;
228 : struct snapraid_block* block;
229 : struct snapraid_disk* disk;
230 : unsigned diskcur;
231 :
232 : /* until now is misc */
233 57102 : state_usage_misc(state);
234 :
235 : /* get the next task */
236 57102 : task = io_data_read(&io, &diskcur, waiting_map, &waiting_mac);
237 :
238 : /* until now is disk */
239 57102 : state_usage_disk(state, handle, waiting_map, waiting_mac);
240 :
241 : /* get the task results */
242 57102 : disk = task->disk;
243 57102 : block = task->block;
244 57102 : read_size = task->read_size;
245 :
246 : /* if the disk position is not used */
247 57102 : if (!disk)
248 5528 : continue;
249 :
250 52415 : state_usage_file(state, disk, task->file);
251 :
252 : /* if the block is not used */
253 52415 : if (!block_has_file(block))
254 841 : continue;
255 :
256 : /* handle error conditions */
257 51574 : if (task->state == TASK_STATE_IOERROR) {
258 : /* LCOV_EXCL_START */
259 : ++io_error;
260 : goto bail;
261 : /* LCOV_EXCL_STOP */
262 : }
263 51574 : if (task->state == TASK_STATE_ERROR) {
264 : /* LCOV_EXCL_START */
265 : ++soft_error;
266 : goto bail;
267 : /* LCOV_EXCL_STOP */
268 : }
269 51574 : if (task->state == TASK_STATE_ERROR_CONTINUE) {
270 0 : ++soft_error;
271 0 : continue;
272 : }
273 51574 : if (task->state == TASK_STATE_IOERROR_CONTINUE) {
274 0 : ++io_error;
275 0 : if (io_error >= state->opt.io_error_limit) {
276 : /* LCOV_EXCL_START */
277 : log_fatal(EIO, "DANGER! Too many input/output errors in the %s disk. It isn't possible to continue.\n", disk->dir);
278 : log_fatal(EIO, "Stopping at block %u\n", blockcur);
279 : goto bail;
280 : /* LCOV_EXCL_STOP */
281 : }
282 :
283 : /* otherwise continue */
284 0 : continue;
285 : }
286 51574 : if (task->state != TASK_STATE_DONE) {
287 : /* LCOV_EXCL_START */
288 : log_fatal(EINTERNAL, "Internal inconsistency in task state\n");
289 : os_abort();
290 : /* LCOV_EXCL_STOP */
291 : }
292 :
293 51574 : countsize += read_size;
294 : }
295 :
296 : /* until now is misc */
297 9517 : state_usage_misc(state);
298 :
299 : /* read the parity */
300 66619 : for (l = 0; l < state->level; ++l) {
301 : struct snapraid_task* task;
302 : unsigned levcur;
303 :
304 57102 : task = io_parity_read(&io, &levcur, waiting_map, &waiting_mac);
305 :
306 : /* until now is parity */
307 57102 : state_usage_parity(state, waiting_map, waiting_mac);
308 :
309 : /* handle error conditions */
310 57102 : if (task->state == TASK_STATE_IOERROR) {
311 : /* LCOV_EXCL_START */
312 : ++io_error;
313 : goto bail;
314 : /* LCOV_EXCL_STOP */
315 : }
316 57102 : if (task->state == TASK_STATE_ERROR) {
317 : /* LCOV_EXCL_START */
318 : ++soft_error;
319 : goto bail;
320 : /* LCOV_EXCL_STOP */
321 : }
322 57102 : if (task->state == TASK_STATE_ERROR_CONTINUE) {
323 0 : ++soft_error;
324 0 : continue;
325 : }
326 57102 : if (task->state == TASK_STATE_IOERROR_CONTINUE) {
327 0 : ++io_error;
328 0 : if (io_error >= state->opt.io_error_limit) {
329 : /* LCOV_EXCL_START */
330 : log_fatal(EIO, "DANGER! Too many input/output errors in the %s disk. It isn't possible to continue.\n", lev_name(levcur));
331 : log_fatal(EIO, "Stopping at block %u\n", blockcur);
332 : goto bail;
333 : /* LCOV_EXCL_STOP */
334 : }
335 0 : continue;
336 : }
337 57102 : if (task->state != TASK_STATE_DONE) {
338 : /* LCOV_EXCL_START */
339 : log_fatal(EINTERNAL, "Internal inconsistency in task state\n");
340 : os_abort();
341 : /* LCOV_EXCL_STOP */
342 : }
343 : }
344 :
345 : /* count the number of processed block */
346 9517 : ++countpos;
347 :
348 : /* progress */
349 9517 : if (state_progress(state, &io, blockcur, countpos, countmax, countsize)) {
350 : /* LCOV_EXCL_START */
351 : break;
352 : /* LCOV_EXCL_STOP */
353 : }
354 :
355 : /* thermal control */
356 9517 : if (state_thermal_alarm(state)) {
357 : /* until now is misc */
358 0 : state_usage_misc(state);
359 :
360 0 : state_progress_stop(state);
361 :
362 0 : state_thermal_cooldown(state);
363 :
364 0 : state_progress_restart(state);
365 :
366 : /* drop until now */
367 0 : state_usage_waste(state);
368 : }
369 : }
370 :
371 3 : end:
372 3 : state_progress_end(state, countpos, countmax, countsize, "Nothing to dry.\n");
373 :
374 3 : state_usage_print(state);
375 :
376 3 : bail:
377 : /* stop all the worker threads */
378 3 : io_stop(&io);
379 :
380 21 : for (j = 0; j < diskmax; ++j) {
381 18 : struct snapraid_file* file = handle[j].file;
382 18 : struct snapraid_disk* disk = handle[j].disk;
383 18 : ret = handle_close(&handle[j]);
384 18 : if (ret == -1) {
385 : /* LCOV_EXCL_START */
386 : log_tag("%s:%u:%s:%s: Close error. %s.\n", es(errno), blockcur, disk->name, esc_tag(file->sub, esc_buffer), strerror(errno));
387 : log_fatal_errno(errno, disk->name);
388 :
389 : if (is_hw(errno)) {
390 : ++io_error;
391 : } else {
392 : ++soft_error;
393 : }
394 : /* continue, as we are already exiting */
395 : /* LCOV_EXCL_STOP */
396 : }
397 : }
398 :
399 3 : if (soft_error || io_error) {
400 0 : msg_status("\n");
401 0 : msg_status("%8u soft errors\n", soft_error);
402 0 : msg_status("%8u io errors\n", io_error);
403 : } else {
404 3 : msg_status("Everything OK\n");
405 : }
406 :
407 3 : if (soft_error)
408 0 : log_fatal(ESOFT, "DANGER! Unexpected errors!\n");
409 3 : if (io_error)
410 0 : log_fatal(EIO, "DANGER! Unexpected input/output errors!\n");
411 :
412 3 : log_tag("summary:error_soft:%u\n", soft_error);
413 3 : log_tag("summary:error_io:%u\n", io_error);
414 3 : if (soft_error + io_error == 0)
415 3 : log_tag("summary:exit:ok\n");
416 : else
417 0 : log_tag("summary:exit:error\n");
418 3 : log_flush();
419 :
420 3 : free(handle);
421 3 : free(waiting_map);
422 3 : io_done(&io);
423 :
424 3 : if (soft_error + io_error != 0)
425 0 : return -1;
426 :
427 3 : if (alert < 0)
428 0 : return -1;
429 :
430 3 : return 0;
431 : }
432 :
433 3 : int state_dry(struct snapraid_state* state, block_off_t blockstart, block_off_t blockcount)
434 : {
435 : block_off_t blockmax;
436 : int ret;
437 : struct snapraid_parity_handle parity_handle[LEV_MAX];
438 : unsigned process_error;
439 : unsigned l;
440 :
441 3 : msg_progress("Drying...\n");
442 :
443 3 : blockmax = parity_allocated_size(state);
444 :
445 3 : if (blockstart > blockmax) {
446 : /* LCOV_EXCL_START */
447 : log_fatal(EUSER, "Error in the specified starting block %u. It's larger than the parity size %u.\n", blockstart, blockmax);
448 : exit(EXIT_FAILURE);
449 : /* LCOV_EXCL_STOP */
450 : }
451 :
452 : /* adjust the number of block to process */
453 3 : if (blockcount != 0 && blockstart + blockcount < blockmax) {
454 0 : blockmax = blockstart + blockcount;
455 : }
456 :
457 : /* open the file for reading */
458 : /* it may fail if the file doesn't exist, in this case we continue to dry the files */
459 21 : for (l = 0; l < state->level; ++l) {
460 18 : ret = parity_open(&parity_handle[l], &state->parity[l], l, state->file_mode, state->block_size, state->opt.parity_limit_size);
461 18 : if (ret == -1) {
462 : /* LCOV_EXCL_START */
463 : log_tag("parity_%s:%u:%s: Open error. %s.\n", es(errno), blockmax, lev_config_name(l), strerror(errno));
464 : log_fatal_errno(errno, lev_config_name(l));
465 : exit(EXIT_FAILURE);
466 : /* LCOV_EXCL_STOP */
467 : }
468 : }
469 :
470 3 : process_error = 0;
471 :
472 : /* skip degenerated cases of empty parity, or skipping all */
473 3 : if (blockstart < blockmax) {
474 3 : ret = state_dry_process(state, parity_handle, blockstart, blockmax);
475 3 : if (ret == -1) {
476 : /* LCOV_EXCL_START */
477 : ++process_error;
478 : /* continue, as we are already exiting */
479 : /* LCOV_EXCL_STOP */
480 : }
481 : }
482 :
483 : /* try to close only if opened */
484 21 : for (l = 0; l < state->level; ++l) {
485 18 : ret = parity_close(&parity_handle[l]);
486 18 : if (ret == -1) {
487 : /* LCOV_EXCL_START */
488 : log_tag("parity_%s:%u:%s: Close error. %s.\n", es(errno), blockmax, lev_config_name(l), strerror(errno));
489 : log_fatal_errno(errno, lev_config_name(l));
490 :
491 : ++process_error;
492 : /* continue, as we are already exiting */
493 : /* LCOV_EXCL_STOP */
494 : }
495 : }
496 :
497 3 : if (process_error != 0)
498 0 : return -1;
499 3 : return 0;
500 : }
501 :
|