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 "support.h"
22 : #include "handle.h"
23 :
24 : /****************************************************************************/
25 : /* handle */
26 :
27 773174 : int handle_create(struct snapraid_handle* handle, struct snapraid_file* file, int mode)
28 : {
29 : int ret;
30 : int flags;
31 :
32 : /* if it's the same file, and already opened, nothing to do */
33 773174 : if (handle->file == file && handle->f != -1) {
34 0 : return 0;
35 : }
36 :
37 773174 : pathprint(handle->path, sizeof(handle->path), "%s%s", handle->disk->dir, file->sub);
38 :
39 773174 : ret = mkancestor(handle->path);
40 773174 : if (ret != 0) {
41 : /* LCOV_EXCL_START */
42 : return -1;
43 : /* LCOV_EXCL_STOP */
44 : }
45 :
46 : /* initial values, changed later if required */
47 773174 : handle->created = 0;
48 :
49 : /* flags for opening */
50 : /* O_BINARY: open as binary file (Windows only) */
51 : /* O_NOFOLLOW: do not follow links to ensure to open the real file */
52 : /* O_SEQUENTIAL: improve performance for sequential access (Windows only) */
53 773174 : flags = O_BINARY | O_NOFOLLOW;
54 773174 : if ((mode & MODE_SEQUENTIAL) != 0)
55 773174 : flags |= O_SEQUENTIAL;
56 :
57 : /* open for read write */
58 773174 : handle->f = open(handle->path, flags | O_RDWR);
59 :
60 : /* if failed for missing write permission */
61 773174 : if (handle->f == -1 && (errno == EACCES || errno == EROFS)) {
62 : /* open for real-only */
63 0 : handle->f = open(handle->path, flags | O_RDONLY);
64 : }
65 :
66 : /* if failed for missing file */
67 773174 : if (handle->f == -1 && errno == ENOENT) {
68 : char path_from[PATH_MAX];
69 :
70 : /* check if exists a .unrecoverable copy, and rename to the real one */
71 176361 : pathprint(path_from, sizeof(path_from), "%s.unrecoverable", handle->path);
72 :
73 176361 : if (rename(path_from, handle->path) == 0) {
74 : /* open for read write */
75 63387 : handle->f = open(handle->path, flags | O_RDWR);
76 : } else {
77 : /* create it */
78 112974 : handle->f = open(handle->path, flags | O_RDWR | O_CREAT, 0600);
79 112974 : if (handle->f != -1) {
80 : /* mark it as created if really done */
81 112974 : handle->created = 1;
82 : }
83 : }
84 : }
85 :
86 773174 : if (handle->f == -1) {
87 : /* LCOV_EXCL_START */
88 : /* invalidate for error */
89 : handle->file = 0;
90 : handle->f = -1;
91 : handle->valid_size = 0;
92 :
93 : log_fatal("Error opening file '%s'. %s.\n", handle->path, strerror(errno));
94 : return -1;
95 : /* LCOV_EXCL_STOP */
96 : }
97 :
98 : /* just opened */
99 773174 : handle->file = file;
100 :
101 : /* get the stat info */
102 773174 : ret = fstat(handle->f, &handle->st);
103 773174 : if (ret != 0) {
104 : /* LCOV_EXCL_START */
105 : log_fatal("Error accessing file '%s'. %s.\n", handle->path, strerror(errno));
106 : return -1;
107 : /* LCOV_EXCL_STOP */
108 : }
109 :
110 : /* get the size of the existing data */
111 773174 : handle->valid_size = handle->st.st_size;
112 :
113 : #if HAVE_POSIX_FADVISE
114 773174 : if ((mode & MODE_SEQUENTIAL) != 0) {
115 : /* advise sequential access */
116 773174 : ret = posix_fadvise(handle->f, 0, 0, POSIX_FADV_SEQUENTIAL);
117 773174 : if (ret != 0) {
118 : /* LCOV_EXCL_START */
119 : log_fatal("Error advising file '%s'. %s.\n", handle->path, strerror(ret));
120 : return -1;
121 : /* LCOV_EXCL_STOP */
122 : }
123 : }
124 : #endif
125 :
126 773174 : return 0;
127 : }
128 :
129 3195 : int handle_truncate(struct snapraid_handle* handle, struct snapraid_file* file)
130 : {
131 : int ret;
132 :
133 3195 : ret = ftruncate(handle->f, file->size);
134 3195 : if (ret != 0) {
135 : /* LCOV_EXCL_START */
136 : if (errno == EACCES) {
137 : log_fatal("Failed to truncate file '%s' for missing write permission.\n", handle->path);
138 : } else {
139 : log_fatal("Error truncating file '%s'. %s.\n", handle->path, strerror(errno));
140 : }
141 : return -1;
142 : /* LCOV_EXCL_STOP */
143 : }
144 :
145 : /* adjust the size to the truncated size */
146 3195 : handle->valid_size = file->size;
147 :
148 3195 : return 0;
149 : }
150 :
151 2005010 : int handle_open(struct snapraid_handle* handle, struct snapraid_file* file, int mode, fptr* out, fptr* out_missing)
152 : {
153 : int ret;
154 : int flags;
155 :
156 2005010 : if (!out_missing)
157 2004503 : out_missing = out;
158 :
159 : /* if already opened, nothing to do */
160 2005010 : if (handle->file == file && handle->f != -1) {
161 284379 : return 0;
162 : }
163 :
164 1720631 : pathprint(handle->path, sizeof(handle->path), "%s%s", handle->disk->dir, file->sub);
165 :
166 : /* for sure not created */
167 1720631 : handle->created = 0;
168 :
169 : /* flags for opening */
170 : /* O_BINARY: open as binary file (Windows only) */
171 : /* O_NOFOLLOW: do not follow links to ensure to open the real file */
172 : /* O_SEQUENTIAL: improve performance for sequential access (Windows only) */
173 1720631 : flags = O_BINARY | O_NOFOLLOW;
174 1720631 : if ((mode & MODE_SEQUENTIAL) != 0)
175 1709301 : flags |= O_SEQUENTIAL;
176 :
177 : /* open for read */
178 1720631 : handle->f = open_noatime(handle->path, flags | O_RDONLY);
179 1720631 : if (handle->f == -1) {
180 : /* invalidate for error */
181 189310 : handle->file = 0;
182 189310 : handle->f = -1;
183 189310 : handle->valid_size = 0;
184 :
185 189310 : if (errno == ENOENT)
186 189298 : out_missing("Missing file '%s'.\n", handle->path);
187 : else
188 12 : out("Error opening file '%s'. %s.\n", handle->path, strerror(errno));
189 189310 : return -1;
190 : }
191 :
192 : /* just opened */
193 1531321 : handle->file = file;
194 :
195 : /* get the stat info */
196 1531321 : ret = fstat(handle->f, &handle->st);
197 1531321 : if (ret != 0) {
198 : /* LCOV_EXCL_START */
199 : out("Error accessing file '%s'. %s.\n", handle->path, strerror(errno));
200 : return -1;
201 : /* LCOV_EXCL_STOP */
202 : }
203 :
204 : /* get the size of the existing data */
205 1531321 : handle->valid_size = handle->st.st_size;
206 :
207 : #if HAVE_POSIX_FADVISE
208 1531321 : if ((mode & MODE_SEQUENTIAL) != 0) {
209 : /* advise sequential access */
210 1519991 : ret = posix_fadvise(handle->f, 0, 0, POSIX_FADV_SEQUENTIAL);
211 1519991 : if (ret != 0) {
212 : /* LCOV_EXCL_START */
213 : out("Error advising file '%s'. %s.\n", handle->path, strerror(ret));
214 : return -1;
215 : /* LCOV_EXCL_STOP */
216 : }
217 : }
218 : #endif
219 :
220 1531321 : return 0;
221 : }
222 :
223 4545866 : int handle_close(struct snapraid_handle* handle)
224 : {
225 : int ret;
226 :
227 : /* close if open */
228 4545866 : if (handle->f != -1) {
229 2304495 : ret = close(handle->f);
230 2304495 : if (ret != 0) {
231 : /* LCOV_EXCL_START */
232 : log_fatal("Error closing file '%s'. %s.\n", handle->file->sub, strerror(errno));
233 :
234 : /* invalidate for error */
235 : handle->file = 0;
236 : handle->f = -1;
237 : handle->valid_size = 0;
238 : return -1;
239 : /* LCOV_EXCL_STOP */
240 : }
241 : }
242 :
243 : /* reset the descriptor */
244 4545866 : handle->file = 0;
245 4545866 : handle->f = -1;
246 4545866 : handle->valid_size = 0;
247 :
248 4545866 : return 0;
249 : }
250 :
251 5583462 : int handle_read(struct snapraid_handle* handle, struct snapraid_block* block, unsigned char* block_buffer, unsigned block_size, fptr* out, fptr* out_missing)
252 : {
253 : ssize_t read_ret;
254 : data_off_t offset;
255 : unsigned read_size;
256 : unsigned count;
257 :
258 5583462 : offset = block_file_pos(block) * (data_off_t)block_size;
259 :
260 5583462 : if (!out_missing)
261 5570037 : out_missing = out;
262 :
263 : /* check if we are going to read only not initialized data */
264 5583462 : if (offset >= handle->valid_size) {
265 : /* if the file is missing, it's at 0 size, or it's rebuilt while reading */
266 432889 : if (offset == handle->valid_size || handle->valid_size == 0)
267 431572 : out_missing("Reading data from missing file '%s' at offset %" PRIu64 ".\n", handle->path, offset);
268 : else
269 1317 : out("Reading missing data from file '%s' at offset %" PRIu64 ".\n", handle->path, offset);
270 432889 : return -1;
271 : }
272 :
273 5150573 : read_size = block_file_size(block, block_size);
274 :
275 : #if !HAVE_PREAD
276 : if (lseek(handle->f, offset, SEEK_SET) != offset) {
277 : /* LCOV_EXCL_START */
278 : out("Error seeking file '%s' at offset %" PRIu64 ". %s.\n", handle->path, offset, strerror(errno));
279 : return -1;
280 : /* LCOV_EXCL_STOP */
281 : }
282 : #endif
283 :
284 5150573 : count = 0;
285 : do {
286 :
287 : #if HAVE_PREAD
288 5159817 : read_ret = pread(handle->f, block_buffer + count, read_size - count, offset + count);
289 : #else
290 : read_ret = read(handle->f, block_buffer + count, read_size - count);
291 : #endif
292 :
293 5159817 : if (read_ret < 0) {
294 : /* LCOV_EXCL_START */
295 : out("Error reading file '%s' at offset %" PRIu64 " for size %u. %s.\n", handle->path, offset + count, read_size - count, strerror(errno));
296 : return -1;
297 : /* LCOV_EXCL_STOP */
298 : }
299 5159817 : if (read_ret == 0) {
300 9244 : out("Unexpected end of file '%s' at offset %" PRIu64 ". %s.\n", handle->path, offset, strerror(errno));
301 9244 : return -1;
302 : }
303 :
304 5150573 : count += read_ret;
305 5150573 : } while (count < read_size);
306 :
307 : /* pad with 0 */
308 5141329 : if (read_size < block_size) {
309 2081857 : memset(block_buffer + read_size, 0, block_size - read_size);
310 : }
311 :
312 : /* Here isn't needed to call posix_fadvise(..., POSIX_FADV_DONTNEED) */
313 : /* because we already advised sequential access with POSIX_FADV_SEQUENTIAL. */
314 : /* In Linux 2.6.33 it's enough to ensure that data is not kept in the cache. */
315 : /* Better to do nothing and save a syscall for each block. */
316 :
317 : /* Here we cannot call posix_fadvise(..., POSIX_FADV_WILLNEED) for the next block */
318 : /* because it may be blocking */
319 : /* See Ted Ts'o comment in "posix_fadvise(POSIX_FADV_WILLNEED) waits before returning?" */
320 : /* at: https://lkml.org/lkml/2010/12/6/122 */
321 : /* We relay only on the automatic readahead triggered by POSIX_FADV_SEQUENTIAL. */
322 :
323 5141329 : return read_size;
324 : }
325 :
326 288068 : int handle_write(struct snapraid_handle* handle, struct snapraid_block* block, unsigned char* block_buffer, unsigned block_size)
327 : {
328 : ssize_t write_ret;
329 : data_off_t offset;
330 : unsigned write_size;
331 :
332 288068 : offset = block_file_pos(block) * (data_off_t)block_size;
333 :
334 288068 : write_size = block_file_size(block, block_size);
335 :
336 : #if HAVE_PWRITE
337 288068 : write_ret = pwrite(handle->f, block_buffer, write_size, offset);
338 : #else
339 : if (lseek(handle->f, offset, SEEK_SET) != offset) {
340 : /* LCOV_EXCL_START */
341 : log_fatal("Error seeking file '%s'. %s.\n", handle->path, strerror(errno));
342 : return -1;
343 : /* LCOV_EXCL_STOP */
344 : }
345 :
346 : write_ret = write(handle->f, block_buffer, write_size);
347 : #endif
348 288068 : if (write_ret != (ssize_t)write_size) { /* conversion is safe because block_size is always small */
349 : /* LCOV_EXCL_START */
350 : log_fatal("Error writing file '%s'. %s.\n", handle->path, strerror(errno));
351 : return -1;
352 : /* LCOV_EXCL_STOP */
353 : }
354 :
355 : /* adjust the size of the valid data */
356 288068 : if (handle->valid_size < offset + write_size) {
357 282038 : handle->valid_size = offset + write_size;
358 : }
359 :
360 : /* Here doesn't make sense to call posix_fadvise(..., POSIX_FADV_DONTNEED) because */
361 : /* at this time the data is still not yet written and it cannot be discharged. */
362 :
363 288068 : return 0;
364 : }
365 :
366 119610 : int handle_utime(struct snapraid_handle* handle)
367 : {
368 : int ret;
369 :
370 : /* do nothing if not opened */
371 119610 : if (handle->f == -1)
372 7 : return 0;
373 :
374 119603 : ret = fmtime(handle->f, handle->file->mtime_sec, handle->file->mtime_nsec);
375 :
376 119603 : if (ret != 0) {
377 : /* LCOV_EXCL_START */
378 : log_fatal("Error timing file '%s'. %s.\n", handle->file->sub, strerror(errno));
379 : return -1;
380 : /* LCOV_EXCL_STOP */
381 : }
382 :
383 119603 : return 0;
384 : }
385 :
386 197 : struct snapraid_handle* handle_map(struct snapraid_state* state, unsigned* handlemax)
387 : {
388 : tommy_node* i;
389 : unsigned j;
390 197 : unsigned size = 0;
391 : struct snapraid_handle* handle;
392 :
393 : /* get the size of the mapping */
394 197 : size = 0;
395 1374 : for (i = state->maplist; i != 0; i = i->next) {
396 1177 : struct snapraid_map* map = i->data;
397 1177 : if (map->position > size)
398 850 : size = map->position;
399 : }
400 197 : ++size; /* size is one more than the max */
401 :
402 197 : handle = malloc_nofail(size * sizeof(struct snapraid_handle));
403 :
404 1379 : for (j = 0; j < size; ++j) {
405 : /* default for empty position */
406 1182 : handle[j].disk = 0;
407 1182 : handle[j].file = 0;
408 1182 : handle[j].f = -1;
409 1182 : handle[j].valid_size = 0;
410 : }
411 :
412 : /* set the vector */
413 1374 : for (i = state->disklist; i != 0; i = i->next) {
414 : struct snapraid_map* map;
415 1177 : struct snapraid_disk* disk = i->data;
416 : tommy_node* k;
417 :
418 : /* search the mapping for this disk */
419 4107 : for (k = state->maplist; k != 0; k = k->next) {
420 4107 : map = k->data;
421 4107 : if (strcmp(disk->name, map->name) == 0)
422 1177 : break;
423 : }
424 1177 : if (k == 0) {
425 : /* LCOV_EXCL_START */
426 : log_fatal("Internal error for inconsistent disk mapping.\n");
427 : exit(EXIT_FAILURE);
428 : /* LCOV_EXCL_STOP */
429 : }
430 :
431 1177 : handle[map->position].disk = disk;
432 : }
433 :
434 197 : *handlemax = size;
435 197 : return handle;
436 : }
437 :
|