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