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 "support.h"
21 : #include "elem.h"
22 : #include "state.h"
23 : #include "parity.h"
24 : #include "handle.h"
25 :
26 : /****************************************************************************/
27 : /* parity */
28 :
29 553 : block_off_t parity_allocated_size(struct snapraid_state* state)
30 : {
31 : block_off_t parity_block;
32 : tommy_node* i;
33 :
34 : /* compute the size of the parity file */
35 553 : parity_block = 0;
36 3861 : for (i = state->disklist; i != 0; i = i->next) {
37 3308 : struct snapraid_disk* disk = i->data;
38 :
39 : /* start from the declared size */
40 3308 : block_off_t block = tommy_arrayblk_size(&disk->blockarr);
41 :
42 : /* decrease the block until an allocated one, but part of a file */
43 : /* we don't stop at deleted blocks, because we want to have them cleared */
44 : /* if they are at the end of the parity */
45 189015 : while (block > parity_block && !block_has_file(tommy_arrayblk_get(&disk->blockarr, block - 1)))
46 182399 : --block;
47 :
48 : /* get the highest value */
49 3308 : if (block > parity_block)
50 2041 : parity_block = block;
51 : }
52 :
53 553 : return parity_block;
54 : }
55 :
56 63 : block_off_t parity_used_size(struct snapraid_state* state)
57 : {
58 : block_off_t parity_block;
59 : tommy_node* i;
60 :
61 : /* compute the size of the parity file */
62 63 : parity_block = 0;
63 440 : for (i = state->disklist; i != 0; i = i->next) {
64 377 : struct snapraid_disk* disk = i->data;
65 :
66 : /* start from the declared size */
67 377 : block_off_t block = tommy_arrayblk_size(&disk->blockarr);
68 :
69 : /* decrease the block until an used one */
70 221368 : while (block > parity_block && !block_has_file_and_valid_parity(tommy_arrayblk_get(&disk->blockarr, block - 1)))
71 220614 : --block;
72 :
73 : /* get the highest value */
74 377 : if (block > parity_block)
75 235 : parity_block = block;
76 : }
77 :
78 63 : return parity_block;
79 : }
80 :
81 1 : int parity_is_invalid(struct snapraid_state* state)
82 : {
83 : block_off_t blockmax;
84 : block_off_t i;
85 :
86 1 : blockmax = parity_allocated_size(state);
87 :
88 1 : for (i = 0; i < blockmax; ++i) {
89 : tommy_node* node_disk;
90 : int one_invalid;
91 : int one_valid;
92 :
93 : /* for each disk */
94 0 : one_invalid = 0;
95 0 : one_valid = 0;
96 0 : for (node_disk = state->disklist; node_disk != 0; node_disk = node_disk->next) {
97 0 : struct snapraid_disk* disk = node_disk->data;
98 0 : struct snapraid_block* block = disk_block_get(disk, i);
99 :
100 0 : if (block_has_file(block))
101 0 : one_valid = 1;
102 0 : if (block_has_invalid_parity(block))
103 0 : one_invalid = 1;
104 : }
105 :
106 : /* if both valid and invalid, we need to update */
107 0 : if (one_invalid && one_valid)
108 0 : return 1;
109 : }
110 :
111 1 : return 0;
112 : }
113 :
114 0 : void parity_overflow(struct snapraid_state* state, data_off_t size)
115 : {
116 : tommy_node* i;
117 : block_off_t blockalloc;
118 0 : int first = 1;
119 :
120 : /* don't report if everything is outside or if the file is not accessible */
121 0 : if (size == 0) {
122 0 : return;
123 : }
124 :
125 0 : blockalloc = size / state->block_size;
126 :
127 : /* for all disks */
128 0 : for (i = state->disklist; i != 0; i = i->next) {
129 0 : struct snapraid_disk* disk = i->data;
130 : tommy_node* j;
131 :
132 : /* for all files */
133 0 : for (j = disk->filelist; j != 0; j = j->next) {
134 0 : struct snapraid_file* file = j->data;
135 :
136 0 : if (file->blockmax > 0) {
137 0 : if (file->blockvec[file->blockmax - 1].parity_pos >= blockalloc) {
138 0 : log_tag("outofparity:%s:%s\n", disk->name, esc(file->sub));
139 0 : if (first) {
140 0 : first = 0;
141 0 : log_fatal("\nYour data requires more parity than the available space.\n");
142 0 : log_fatal("Please move the files 'outofparity' to another data disk:\n");
143 : }
144 0 : log_fatal("outofparity %s%s\n", disk->dir, file->sub);
145 : }
146 : }
147 : }
148 : }
149 : }
150 :
151 497 : int parity_create(struct snapraid_parity_handle* parity, const char* path, data_off_t* out_size, int mode)
152 : {
153 : int ret;
154 : int flags;
155 :
156 497 : pathcpy(parity->path, sizeof(parity->path), path);
157 :
158 : /* opening in sequential mode in Windows */
159 : /* O_SEQUENTIAL: opening in sequential mode in Windows */
160 497 : flags = O_RDWR | O_CREAT | O_BINARY;
161 497 : if ((mode & MODE_SEQUENTIAL) != 0)
162 497 : flags |= O_SEQUENTIAL;
163 497 : parity->f = open(parity->path, flags, 0600);
164 497 : if (parity->f == -1) {
165 : /* LCOV_EXCL_START */
166 : log_fatal("Error opening parity file '%s'. %s.\n", parity->path, strerror(errno));
167 : return -1;
168 : /* LCOV_EXCL_STOP */
169 : }
170 :
171 : /* get the stat info */
172 497 : ret = fstat(parity->f, &parity->st);
173 497 : if (ret != 0) {
174 : /* LCOV_EXCL_START */
175 : log_fatal("Error accessing parity file '%s'. %s.\n", parity->path, strerror(errno));
176 : goto bail;
177 : /* LCOV_EXCL_STOP */
178 : }
179 :
180 : #if HAVE_POSIX_FADVISE
181 497 : if ((mode & MODE_SEQUENTIAL) != 0) {
182 : /* advise sequential access */
183 497 : ret = posix_fadvise(parity->f, 0, 0, POSIX_FADV_SEQUENTIAL);
184 497 : if (ret != 0) {
185 : /* LCOV_EXCL_START */
186 : log_fatal("Error advising parity file '%s'. %s.\n", parity->path, strerror(ret));
187 : goto bail;
188 : /* LCOV_EXCL_STOP */
189 : }
190 : }
191 : #endif
192 :
193 : /* get the size of the existing data */
194 497 : parity->valid_size = parity->st.st_size;
195 497 : *out_size = parity->st.st_size;
196 :
197 497 : return 0;
198 :
199 : bail:
200 : /* LCOV_EXCL_START */
201 : close(parity->f);
202 : parity->f = -1;
203 : parity->valid_size = 0;
204 : return -1;
205 : /* LCOV_EXCL_STOP */
206 : }
207 :
208 497 : int parity_chsize(struct snapraid_parity_handle* parity, data_off_t size, data_off_t* out_size, int skip_fallocate)
209 : {
210 : int ret;
211 :
212 497 : if (parity->st.st_size < size) {
213 : int f_ret;
214 : int f_errno;
215 : const char* call;
216 :
217 : #if HAVE_FALLOCATE
218 65 : call = "fallocate";
219 65 : if (!skip_fallocate) {
220 : /* allocate real space using the specific Linux fallocate() operation. */
221 : /* If the underline filesystem doesn't support it, this operation fails, */
222 : /* instead posix_fallocate() fallbacks to write the whole file. */
223 64 : ret = fallocate(parity->f, 0, 0, size);
224 :
225 : /* fallocate() returns the error number as positive integer, */
226 : /* and in this case it doesn't set errno, just like posix_fallocate() */
227 : /* Checking the glibc code (2.11.1 and 2.14.1) it seems that ENOSYS */
228 : /* may be returned in errno, so we support both the return way */
229 64 : if (ret > 0) { /* if a positive error is returned, convert it to errno */
230 : /* LCOV_EXCL_START */
231 : errno = ret;
232 : ret = -1;
233 : /* LCOV_EXCL_STOP */
234 : }
235 : } else {
236 1 : errno = EOPNOTSUPP;
237 1 : ret = -1;
238 : }
239 :
240 : /* we get EOPNOTSUPP if the operation is not supported, like in ext3/ext2 */
241 : /* or ENOSYS with kernel before 2.6.23 */
242 65 : if (errno == EOPNOTSUPP || errno == ENOSYS) {
243 : /* fallback using ftruncate() */
244 1 : call = "ftruncate";
245 1 : ret = ftruncate(parity->f, size);
246 : }
247 : #else
248 : (void)skip_fallocate; /* avoid the warning */
249 :
250 : /* allocate using a sparse file */
251 : call = "ftruncate";
252 : ret = ftruncate(parity->f, size);
253 : #endif
254 : /* save the state of the grow operation */
255 65 : f_ret = ret;
256 65 : f_errno = errno;
257 :
258 : /* get the stat info */
259 65 : ret = fstat(parity->f, &parity->st);
260 65 : if (ret != 0) {
261 : /* LCOV_EXCL_START */
262 : log_fatal("Error accessing parity file '%s'. %s.\n", parity->path, strerror(errno));
263 : goto bail;
264 : /* LCOV_EXCL_STOP */
265 : }
266 :
267 : /* return the new size */
268 65 : *out_size = parity->st.st_size;
269 :
270 : /* now check the error */
271 65 : if (f_ret != 0) {
272 : /* LCOV_EXCL_START */
273 : if (f_errno == ENOSPC) {
274 : log_fatal("Failed to grow parity file '%s' to size %" PRIu64 " using %s due lack of space.\n", parity->path, size, call);
275 : } else {
276 : log_fatal("Error growing parity file '%s' to size %" PRIu64 " using %s. Do you have enough space? %s.\n", parity->path, size, call, strerror(f_errno));
277 : }
278 : goto bail;
279 : /* LCOV_EXCL_STOP */
280 : }
281 432 : } else if (parity->st.st_size > size) {
282 : int f_ret;
283 : int f_errno;
284 :
285 : /* truncate the parity file */
286 36 : ret = ftruncate(parity->f, size);
287 :
288 : /* save the state of the shrink operation */
289 36 : f_ret = ret;
290 36 : f_errno = errno;
291 :
292 : /* get the stat info */
293 36 : ret = fstat(parity->f, &parity->st);
294 36 : if (ret != 0) {
295 : /* LCOV_EXCL_START */
296 : log_fatal("Error accessing parity file '%s'. %s.\n", parity->path, strerror(errno));
297 : goto bail;
298 : /* LCOV_EXCL_STOP */
299 : }
300 :
301 : /* return the new size */
302 36 : *out_size = parity->st.st_size;
303 :
304 : /* now check the error */
305 36 : if (f_ret != 0) {
306 : /* LCOV_EXCL_START */
307 : log_fatal("Error truncating parity file '%s' to size %" PRIu64 ". %s.\n", parity->path, size, strerror(f_errno));
308 : goto bail;
309 : /* LCOV_EXCL_STOP */
310 : }
311 :
312 : /* adjust the valid to the new size */
313 36 : parity->valid_size = size;
314 : }
315 :
316 497 : return 0;
317 :
318 : bail:
319 : /* LCOV_EXCL_START */
320 : parity->valid_size = 0;
321 : return -1;
322 : /* LCOV_EXCL_STOP */
323 : }
324 :
325 391 : int parity_open(struct snapraid_parity_handle* parity, const char* path, int mode)
326 : {
327 : int ret;
328 : int flags;
329 :
330 391 : pathcpy(parity->path, sizeof(parity->path), path);
331 :
332 : /* open for read */
333 : /* O_SEQUENTIAL: opening in sequential mode in Windows */
334 : /* O_NOATIME: do not change access time */
335 391 : flags = O_RDONLY | O_BINARY;
336 391 : if ((mode & MODE_SEQUENTIAL) != 0)
337 390 : flags |= O_SEQUENTIAL;
338 391 : parity->f = open_noatime(parity->path, flags);
339 391 : if (parity->f == -1) {
340 1 : log_fatal("Error opening parity file '%s'. %s.\n", parity->path, strerror(errno));
341 1 : return -1;
342 : }
343 :
344 : /* get the stat info */
345 390 : ret = fstat(parity->f, &parity->st);
346 390 : if (ret != 0) {
347 : /* LCOV_EXCL_START */
348 : log_fatal("Error accessing parity file '%s'. %s.\n", parity->path, strerror(errno));
349 : goto bail;
350 : /* LCOV_EXCL_STOP */
351 : }
352 :
353 : /* get the size of the exising data */
354 390 : parity->valid_size = parity->st.st_size;
355 :
356 : #if HAVE_POSIX_FADVISE
357 390 : if ((mode & MODE_SEQUENTIAL) != 0) {
358 : /* advise sequential access */
359 389 : ret = posix_fadvise(parity->f, 0, 0, POSIX_FADV_SEQUENTIAL);
360 389 : if (ret != 0) {
361 : /* LCOV_EXCL_START */
362 : log_fatal("Error advising parity file '%s'. %s.\n", parity->path, strerror(ret));
363 : goto bail;
364 : /* LCOV_EXCL_STOP */
365 : }
366 : }
367 : #endif
368 :
369 390 : return 0;
370 :
371 : bail:
372 : /* LCOV_EXCL_START */
373 : close(parity->f);
374 : parity->f = -1;
375 : parity->valid_size = 0;
376 : return -1;
377 : /* LCOV_EXCL_STOP */
378 : }
379 :
380 368 : int parity_sync(struct snapraid_parity_handle* parity)
381 : {
382 : #if HAVE_FSYNC
383 : int ret;
384 :
385 : /* Ensure that data changes are written to disk. */
386 : /* This is required to ensure that parity is more updated than content */
387 : /* in case of a system crash. */
388 368 : ret = fsync(parity->f);
389 368 : if (ret != 0) {
390 : /* LCOV_EXCL_START */
391 : log_fatal("Error synching parity file '%s'. %s.\n", parity->path, strerror(errno));
392 : return -1;
393 : /* LCOV_EXCL_STOP */
394 : }
395 : #endif
396 :
397 368 : return 0;
398 : }
399 :
400 887 : int parity_close(struct snapraid_parity_handle* parity)
401 : {
402 : int ret;
403 :
404 887 : ret = close(parity->f);
405 887 : if (ret != 0) {
406 : /* LCOV_EXCL_START */
407 : /* invalidate for error */
408 : parity->f = -1;
409 : parity->valid_size = 0;
410 :
411 : /* This is a serious error, as it may be the result of a failed write */
412 : /* identified at later time. */
413 : /* In a normal filesystem (not NFS) it should never happen */
414 : log_fatal("Error closing parity file '%s'. %s.\n", parity->path, strerror(errno));
415 : return -1;
416 : /* LCOV_EXCL_STOP */
417 : }
418 :
419 : /* reset the descriptor */
420 887 : parity->f = -1;
421 887 : parity->valid_size = 0;
422 :
423 887 : return 0;
424 : }
425 :
426 500002 : int parity_write(struct snapraid_parity_handle* parity, block_off_t pos, unsigned char* block_buffer, unsigned block_size)
427 : {
428 : ssize_t write_ret;
429 : data_off_t offset;
430 :
431 500002 : offset = pos * (data_off_t)block_size;
432 :
433 : #if HAVE_PWRITE
434 500002 : write_ret = pwrite(parity->f, block_buffer, block_size, offset);
435 : #else
436 : if (lseek(parity->f, offset, SEEK_SET) != offset) {
437 : /* LCOV_EXCL_START */
438 : if (errno == ENOSPC) {
439 : log_fatal("Failed to grow parity file '%s' using lseek due lack of space.\n", parity->path);
440 : } else {
441 : log_fatal("Error seeking file '%s'. %s.\n", parity->path, strerror(errno));
442 : }
443 : return -1;
444 : /* LCOV_EXCL_STOP */
445 : }
446 :
447 : write_ret = write(parity->f, block_buffer, block_size);
448 : #endif
449 500002 : if (write_ret != (ssize_t)block_size) { /* conversion is safe because block_size is always small */
450 : /* LCOV_EXCL_START */
451 : if (errno == ENOSPC) {
452 : log_fatal("Failed to grow parity file '%s' using write due lack of space.\n", parity->path);
453 : } else {
454 : log_fatal("Error writing file '%s'. %s.\n", parity->path, strerror(errno));
455 : }
456 : return -1;
457 : /* LCOV_EXCL_STOP */
458 : }
459 :
460 : /* adjust the size of the valid data */
461 500002 : if (parity->valid_size < offset + block_size) {
462 169982 : parity->valid_size = offset + block_size;
463 : }
464 :
465 : /* Here doesn't make sense to call posix_fadvise(..., POSIX_FADV_DONTNEED) because */
466 : /* at this time the data is still in not yet written and it cannot be discharged. */
467 :
468 500002 : return 0;
469 : }
470 :
471 3405588 : int parity_read(struct snapraid_parity_handle* parity, block_off_t pos, unsigned char* block_buffer, unsigned block_size, fptr* out)
472 : {
473 : ssize_t read_ret;
474 : data_off_t offset;
475 : unsigned count;
476 :
477 3405588 : offset = pos * (data_off_t)block_size;
478 :
479 : /* check if we are going to read only not initialized data */
480 3405588 : if (offset >= parity->valid_size) {
481 90270 : out("Reading missing data from file '%s' at offset %" PRIu64 ".\n", parity->path, offset);
482 90270 : return -1;
483 : }
484 :
485 : #if !HAVE_PREAD
486 : if (lseek(parity->f, offset, SEEK_SET) != offset) {
487 : /* LCOV_EXCL_START */
488 : out("Error seeking file '%s' at offset %" PRIu64 ". %s.\n", parity->path, offset, strerror(errno));
489 : return -1;
490 : /* LCOV_EXCL_STOP */
491 : }
492 : #endif
493 :
494 3315318 : count = 0;
495 : do {
496 :
497 : #if HAVE_PREAD
498 3315318 : read_ret = pread(parity->f, block_buffer + count, block_size - count, offset + count);
499 : #else
500 : read_ret = read(parity->f, block_buffer + count, block_size - count);
501 : #endif
502 3315318 : if (read_ret < 0) {
503 : /* LCOV_EXCL_START */
504 : out("Error reading file '%s' at offset %" PRIu64 " for size %u. %s.\n", parity->path, offset + count, block_size - count, strerror(errno));
505 : return -1;
506 : /* LCOV_EXCL_STOP */
507 : }
508 3315318 : if (read_ret == 0) {
509 : /* LCOV_EXCL_START */
510 : out("Unexpected end of file '%s' at offset %" PRIu64 ". %s.\n", parity->path, offset, strerror(errno));
511 : return -1;
512 : /* LCOV_EXCL_STOP */
513 : }
514 :
515 3315318 : count += read_ret;
516 3315318 : } while (count < block_size);
517 :
518 : /* Here isn't needed to call posix_fadvise(..., POSIX_FADV_DONTNEED) */
519 : /* because we already advised sequential access with POSIX_FADV_SEQUENTIAL. */
520 : /* In Linux 2.6.33 it's enough to ensure that data is not kept in the cache. */
521 : /* Better to do nothing and save a syscall for each block. */
522 :
523 3315318 : return block_size;
524 : }
525 :
|