Static error value checking
[fio.git] / filesetup.c
CommitLineData
53cdc686
JA
1#include <unistd.h>
2#include <fcntl.h>
3#include <string.h>
4#include <assert.h>
5#include <sys/stat.h>
6#include <sys/mman.h>
7
8#include "fio.h"
9#include "os.h"
10
25205e97
JA
11/*
12 * Check if the file exists and it's large enough.
13 */
14static int file_ok(struct thread_data *td, struct fio_file *f)
53cdc686 15{
b2a15192 16 struct stat st;
53cdc686 17
4d9345ae 18 if (td->filetype != FIO_TYPE_FILE || (td->io_ops->flags & FIO_NULLIO))
b2a15192
JA
19 return 0;
20
25205e97
JA
21 if (stat(f->file_name, &st) == -1)
22 return 1;
23 else if (st.st_size < (off_t) f->file_size)
24 return 1;
25
26 return 0;
27}
28
29static int create_file(struct thread_data *td, struct fio_file *f)
30{
31 unsigned long long left;
32 unsigned int bs;
33 char *b;
34 int r;
b2a15192 35
53cdc686
JA
36 f->fd = open(f->file_name, O_WRONLY | O_CREAT | O_TRUNC, 0644);
37 if (f->fd < 0) {
38 td_verror(td, errno);
39 return 1;
40 }
41
42 if (ftruncate(f->fd, f->file_size) == -1) {
43 td_verror(td, errno);
44 goto err;
45 }
46
40f8298c
JA
47 if (posix_fallocate(f->fd, 0, f->file_size) < 0) {
48 td_verror(td, errno);
49 goto err;
50 }
51
a00735e6
JA
52 b = malloc(td->max_bs[DDIR_WRITE]);
53 memset(b, 0, td->max_bs[DDIR_WRITE]);
53cdc686
JA
54
55 left = f->file_size;
56 while (left && !td->terminate) {
a00735e6 57 bs = td->max_bs[DDIR_WRITE];
53cdc686
JA
58 if (bs > left)
59 bs = left;
60
61 r = write(f->fd, b, bs);
62
63 if (r == (int) bs) {
64 left -= bs;
65 continue;
66 } else {
67 if (r < 0)
68 td_verror(td, errno);
69 else
70 td_verror(td, EIO);
71
72 break;
73 }
74 }
75
76 if (td->terminate)
77 unlink(f->file_name);
78 else if (td->create_fsync)
79 fsync(f->fd);
80
81 free(b);
82 close(f->fd);
83 f->fd = -1;
84 return 0;
85err:
86 close(f->fd);
87 f->fd = -1;
88 return 1;
89}
90
91static int create_files(struct thread_data *td)
92{
93 struct fio_file *f;
25205e97 94 int i, err, need_create;
53cdc686 95
f697125a
JA
96 for_each_file(td, f, i)
97 f->file_size = td->total_file_size / td->nr_files;
98
53cdc686
JA
99 /*
100 * unless specifically asked for overwrite, let normal io extend it
101 */
f697125a 102 if (!td->overwrite)
53cdc686 103 return 0;
53cdc686 104
25205e97 105 need_create = 0;
eff6861d
JA
106 if (td->filetype == FIO_TYPE_FILE) {
107 for_each_file(td, f, i) {
108 int file_there = !file_ok(td, f);
109
3404cefc
JA
110 if (file_there && td->ddir == DDIR_WRITE &&
111 !td->overwrite) {
eff6861d
JA
112 unlink(f->file_name);
113 file_there = 0;
114 }
115
116 need_create += !file_there;
117 }
118 }
25205e97 119
25205e97
JA
120 if (!need_create)
121 return 0;
122
f1027063
JA
123 if (!td->total_file_size) {
124 log_err("Need size for create\n");
125 td_verror(td, EINVAL);
126 return 1;
127 }
128
53cdc686 129 temp_stall_ts = 1;
1e97cce9 130 fprintf(f_out, "%s: Laying out IO file(s) (%u x %LuMiB == %LuMiB)\n",
13f8e2d2
JA
131 td->name, td->nr_uniq_files,
132 (td->total_file_size >> 20) / td->nr_uniq_files,
25205e97 133 td->total_file_size >> 20);
53cdc686
JA
134
135 err = 0;
136 for_each_file(td, f, i) {
9b031dc8
JA
137 /*
138 * Only unlink files that we created.
139 */
140 f->unlink = 0;
25205e97 141 if (file_ok(td, f)) {
9b031dc8 142 f->unlink = td->unlink;
25205e97
JA
143 err = create_file(td, f);
144 if (err)
145 break;
146 }
53cdc686
JA
147 }
148
149 temp_stall_ts = 0;
150 return err;
151}
152
153static int file_size(struct thread_data *td, struct fio_file *f)
154{
155 struct stat st;
156
f4ee22ca
JA
157 /*
158 * if we are not doing real io, just pretend the file is as large
159 * as the size= given. this works fine with nrfiles > 1 as well,
160 * we only really care about it being at least as big as size=
161 */
162 if (td->io_ops->flags & FIO_NULLIO) {
163 f->real_file_size = f->file_size = td->total_file_size;
164 return 0;
165 }
166
53cdc686
JA
167 if (td->overwrite) {
168 if (fstat(f->fd, &st) == -1) {
169 td_verror(td, errno);
170 return 1;
171 }
172
173 f->real_file_size = st.st_size;
174
175 if (!f->file_size || f->file_size > f->real_file_size)
176 f->file_size = f->real_file_size;
745508dd
JA
177 } else
178 f->real_file_size = f->file_size;
53cdc686 179
53cdc686
JA
180 return 0;
181}
182
183static int bdev_size(struct thread_data *td, struct fio_file *f)
184{
185 unsigned long long bytes;
186 int r;
187
188 r = blockdev_size(f->fd, &bytes);
189 if (r) {
190 td_verror(td, r);
191 return 1;
192 }
193
194 f->real_file_size = bytes;
195
196 /*
197 * no extend possibilities, so limit size to device size if too large
198 */
199 if (!f->file_size || f->file_size > f->real_file_size)
200 f->file_size = f->real_file_size;
201
202 f->file_size -= f->file_offset;
203 return 0;
204}
205
206static int get_file_size(struct thread_data *td, struct fio_file *f)
207{
208 int ret = 0;
209
210 if (td->filetype == FIO_TYPE_FILE)
211 ret = file_size(td, f);
212 else if (td->filetype == FIO_TYPE_BD)
213 ret = bdev_size(td, f);
214 else
215 f->real_file_size = -1;
216
217 if (ret)
218 return ret;
219
220 if (f->file_offset > f->real_file_size) {
221 log_err("%s: offset extends end (%Lu > %Lu)\n", td->name, f->file_offset, f->real_file_size);
222 return 1;
223 }
224
53cdc686
JA
225 return 0;
226}
227
e5b401d4
JA
228int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
229{
230 int ret = 0;
231
232 /*
233 * FIXME: add blockdev flushing too
234 */
235 if (td->io_ops->flags & FIO_MMAPIO)
236 ret = madvise(f->mmap, f->file_size, MADV_DONTNEED);
237 else if (td->filetype == FIO_TYPE_FILE)
238 ret = fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_DONTNEED);
239 else if (td->filetype == FIO_TYPE_BD)
240 ret = blockdev_invalidate_cache(f->fd);
241 else if (td->filetype == FIO_TYPE_CHAR)
242 ret = 0;
243
244 if (ret < 0) {
245 td_verror(td, errno);
246 return 1;
247 }
248
249 return 0;
250}
251
53cdc686
JA
252static int __setup_file_mmap(struct thread_data *td, struct fio_file *f)
253{
254 int flags;
255
256 if (td_rw(td))
257 flags = PROT_READ | PROT_WRITE;
258 else if (td_write(td)) {
259 flags = PROT_WRITE;
260
261 if (td->verify != VERIFY_NONE)
262 flags |= PROT_READ;
263 } else
264 flags = PROT_READ;
265
266 f->mmap = mmap(NULL, f->file_size, flags, MAP_SHARED, f->fd, f->file_offset);
267 if (f->mmap == MAP_FAILED) {
268 f->mmap = NULL;
269 td_verror(td, errno);
270 return 1;
271 }
272
e5b401d4
JA
273 if (td->invalidate_cache && file_invalidate_cache(td, f))
274 return 1;
53cdc686
JA
275
276 if (td->sequential) {
277 if (madvise(f->mmap, f->file_size, MADV_SEQUENTIAL) < 0) {
278 td_verror(td, errno);
279 return 1;
280 }
281 } else {
282 if (madvise(f->mmap, f->file_size, MADV_RANDOM) < 0) {
283 td_verror(td, errno);
284 return 1;
285 }
286 }
287
288 return 0;
289}
290
291static int setup_files_mmap(struct thread_data *td)
292{
293 struct fio_file *f;
294 int i, err = 0;
295
296 for_each_file(td, f, i) {
297 err = __setup_file_mmap(td, f);
298 if (err)
299 break;
300 }
301
302 return err;
303}
304
305static int __setup_file_plain(struct thread_data *td, struct fio_file *f)
306{
e5b401d4
JA
307 if (td->invalidate_cache && file_invalidate_cache(td, f))
308 return 1;
53cdc686
JA
309
310 if (td->sequential) {
311 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
312 td_verror(td, errno);
313 return 1;
314 }
315 } else {
316 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_RANDOM) < 0) {
317 td_verror(td, errno);
318 return 1;
319 }
320 }
321
322 return 0;
323}
324
325static int setup_files_plain(struct thread_data *td)
326{
327 struct fio_file *f;
328 int i, err = 0;
329
330 for_each_file(td, f, i) {
331 err = __setup_file_plain(td, f);
332 if (err)
333 break;
334 }
335
336 return err;
337}
338
339static int setup_file(struct thread_data *td, struct fio_file *f)
340{
53cdc686
JA
341 int flags = 0;
342
ed92ac0c
JA
343 if (td->io_ops->flags & FIO_NETIO)
344 return 0;
53cdc686 345
4d9345ae
JA
346 /*
347 * we need a valid file descriptor, but don't create a real file.
348 * lets just dup stdout, seems like a sensible approach.
349 */
350 if (td->io_ops->flags & FIO_NULLIO)
351 f->fd = dup(STDOUT_FILENO);
352 else {
353 if (td->odirect)
354 flags |= OS_O_DIRECT;
355 if (td->sync_io)
356 flags |= O_SYNC;
357
358 if (td_write(td) || td_rw(td)) {
359 flags |= O_RDWR;
7abf833d 360
4d9345ae
JA
361 if (td->filetype == FIO_TYPE_FILE) {
362 if (!td->overwrite)
363 flags |= O_TRUNC;
53cdc686 364
4d9345ae
JA
365 flags |= O_CREAT;
366 }
53cdc686 367
4d9345ae
JA
368 f->fd = open(f->file_name, flags, 0600);
369 } else {
370 if (td->filetype == FIO_TYPE_CHAR)
371 flags |= O_RDWR;
372 else
373 flags |= O_RDONLY;
53cdc686 374
4d9345ae
JA
375 f->fd = open(f->file_name, flags);
376 }
53cdc686
JA
377 }
378
379 if (f->fd == -1) {
380 td_verror(td, errno);
381 return 1;
382 }
383
384 if (get_file_size(td, f))
385 return 1;
386
387 return 0;
388}
389
21972cde
JA
390int open_files(struct thread_data *td)
391{
392 struct fio_file *f;
393 int i, err = 0;
394
395 for_each_file(td, f, i) {
396 err = setup_file(td, f);
397 if (err)
398 break;
399 }
400
7abf833d
JA
401 if (!err)
402 return 0;
403
404 for_each_file(td, f, i) {
405 if (f->fd != -1) {
406 close(f->fd);
407 f->fd = -1;
408 }
409 }
410
21972cde
JA
411 return err;
412}
413
53cdc686
JA
414int setup_files(struct thread_data *td)
415{
416 struct fio_file *f;
21972cde 417 int err, i;
53cdc686
JA
418
419 /*
420 * if ioengine defines a setup() method, it's responsible for
421 * setting up everything in the td->files[] area.
422 */
423 if (td->io_ops->setup)
424 return td->io_ops->setup(td);
425
426 if (create_files(td))
427 return 1;
428
21972cde 429 err = open_files(td);
f1027063
JA
430 if (err)
431 return err;
432
0a7eb121
JA
433 /*
434 * Recalculate the total file size now that files are set up.
435 */
436 td->total_file_size = 0;
437 for_each_file(td, f, i)
438 td->total_file_size += f->file_size;
439
f1027063 440 td->io_size = td->total_file_size;
53cdc686
JA
441 if (td->io_size == 0) {
442 log_err("%s: no io blocks\n", td->name);
443 td_verror(td, EINVAL);
444 return 1;
445 }
446
447 if (!td->zone_size)
448 td->zone_size = td->io_size;
449
450 td->total_io_size = td->io_size * td->loops;
451
452 if (td->io_ops->flags & FIO_MMAPIO)
21972cde 453 err = setup_files_mmap(td);
53cdc686 454 else
21972cde
JA
455 err = setup_files_plain(td);
456
457 for_each_file(td, f, i) {
458 if (f->fd != -1) {
459 close(f->fd);
460 f->fd = -1;
461 }
462 }
463
464 return err;
53cdc686
JA
465}
466
467void close_files(struct thread_data *td)
468{
0ab8db89 469 struct fio_file *f;
53cdc686
JA
470 int i;
471
0ab8db89 472 for_each_file(td, f, i) {
9b031dc8 473 if (!td->filename && f->unlink &&
02bcaa8c 474 td->filetype == FIO_TYPE_FILE) {
132ad46d
JA
475 unlink(f->file_name);
476 free(f->file_name);
477 f->file_name = NULL;
478 }
21972cde
JA
479 if (f->fd != -1) {
480 close(f->fd);
481 f->fd = -1;
482 }
53cdc686
JA
483 if (f->mmap) {
484 munmap(f->mmap, f->file_size);
485 f->mmap = NULL;
486 }
487 }
b4a6a59a 488
132ad46d 489 td->filename = NULL;
b4a6a59a
JA
490 free(td->files);
491 td->files = NULL;
492 td->nr_files = 0;
53cdc686 493}