[PATCH] Rename io engines
[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
b2a15192
JA
18 if (td->filetype != FIO_TYPE_FILE)
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
157 if (td->overwrite) {
158 if (fstat(f->fd, &st) == -1) {
159 td_verror(td, errno);
160 return 1;
161 }
162
163 f->real_file_size = st.st_size;
164
165 if (!f->file_size || f->file_size > f->real_file_size)
166 f->file_size = f->real_file_size;
745508dd
JA
167 } else
168 f->real_file_size = f->file_size;
53cdc686 169
53cdc686
JA
170 return 0;
171}
172
173static int bdev_size(struct thread_data *td, struct fio_file *f)
174{
175 unsigned long long bytes;
176 int r;
177
178 r = blockdev_size(f->fd, &bytes);
179 if (r) {
180 td_verror(td, r);
181 return 1;
182 }
183
184 f->real_file_size = bytes;
185
186 /*
187 * no extend possibilities, so limit size to device size if too large
188 */
189 if (!f->file_size || f->file_size > f->real_file_size)
190 f->file_size = f->real_file_size;
191
192 f->file_size -= f->file_offset;
193 return 0;
194}
195
196static int get_file_size(struct thread_data *td, struct fio_file *f)
197{
198 int ret = 0;
199
200 if (td->filetype == FIO_TYPE_FILE)
201 ret = file_size(td, f);
202 else if (td->filetype == FIO_TYPE_BD)
203 ret = bdev_size(td, f);
204 else
205 f->real_file_size = -1;
206
207 if (ret)
208 return ret;
209
210 if (f->file_offset > f->real_file_size) {
211 log_err("%s: offset extends end (%Lu > %Lu)\n", td->name, f->file_offset, f->real_file_size);
212 return 1;
213 }
214
53cdc686
JA
215 return 0;
216}
217
e5b401d4
JA
218int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
219{
220 int ret = 0;
221
222 /*
223 * FIXME: add blockdev flushing too
224 */
225 if (td->io_ops->flags & FIO_MMAPIO)
226 ret = madvise(f->mmap, f->file_size, MADV_DONTNEED);
227 else if (td->filetype == FIO_TYPE_FILE)
228 ret = fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_DONTNEED);
229 else if (td->filetype == FIO_TYPE_BD)
230 ret = blockdev_invalidate_cache(f->fd);
231 else if (td->filetype == FIO_TYPE_CHAR)
232 ret = 0;
233
234 if (ret < 0) {
235 td_verror(td, errno);
236 return 1;
237 }
238
239 return 0;
240}
241
53cdc686
JA
242static int __setup_file_mmap(struct thread_data *td, struct fio_file *f)
243{
244 int flags;
245
246 if (td_rw(td))
247 flags = PROT_READ | PROT_WRITE;
248 else if (td_write(td)) {
249 flags = PROT_WRITE;
250
251 if (td->verify != VERIFY_NONE)
252 flags |= PROT_READ;
253 } else
254 flags = PROT_READ;
255
256 f->mmap = mmap(NULL, f->file_size, flags, MAP_SHARED, f->fd, f->file_offset);
257 if (f->mmap == MAP_FAILED) {
258 f->mmap = NULL;
259 td_verror(td, errno);
260 return 1;
261 }
262
e5b401d4
JA
263 if (td->invalidate_cache && file_invalidate_cache(td, f))
264 return 1;
53cdc686
JA
265
266 if (td->sequential) {
267 if (madvise(f->mmap, f->file_size, MADV_SEQUENTIAL) < 0) {
268 td_verror(td, errno);
269 return 1;
270 }
271 } else {
272 if (madvise(f->mmap, f->file_size, MADV_RANDOM) < 0) {
273 td_verror(td, errno);
274 return 1;
275 }
276 }
277
278 return 0;
279}
280
281static int setup_files_mmap(struct thread_data *td)
282{
283 struct fio_file *f;
284 int i, err = 0;
285
286 for_each_file(td, f, i) {
287 err = __setup_file_mmap(td, f);
288 if (err)
289 break;
290 }
291
292 return err;
293}
294
295static int __setup_file_plain(struct thread_data *td, struct fio_file *f)
296{
e5b401d4
JA
297 if (td->invalidate_cache && file_invalidate_cache(td, f))
298 return 1;
53cdc686
JA
299
300 if (td->sequential) {
301 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
302 td_verror(td, errno);
303 return 1;
304 }
305 } else {
306 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_RANDOM) < 0) {
307 td_verror(td, errno);
308 return 1;
309 }
310 }
311
312 return 0;
313}
314
315static int setup_files_plain(struct thread_data *td)
316{
317 struct fio_file *f;
318 int i, err = 0;
319
320 for_each_file(td, f, i) {
321 err = __setup_file_plain(td, f);
322 if (err)
323 break;
324 }
325
326 return err;
327}
328
329static int setup_file(struct thread_data *td, struct fio_file *f)
330{
53cdc686
JA
331 int flags = 0;
332
53cdc686
JA
333 if (td->odirect)
334 flags |= OS_O_DIRECT;
7abf833d
JA
335 if (td->sync_io)
336 flags |= O_SYNC;
53cdc686
JA
337
338 if (td_write(td) || td_rw(td)) {
7abf833d
JA
339 flags |= O_RDWR;
340
53cdc686
JA
341 if (td->filetype == FIO_TYPE_FILE) {
342 if (!td->overwrite)
343 flags |= O_TRUNC;
344
345 flags |= O_CREAT;
346 }
53cdc686
JA
347
348 f->fd = open(f->file_name, flags, 0600);
349 } else {
350 if (td->filetype == FIO_TYPE_CHAR)
351 flags |= O_RDWR;
352 else
353 flags |= O_RDONLY;
354
355 f->fd = open(f->file_name, flags);
356 }
357
358 if (f->fd == -1) {
359 td_verror(td, errno);
360 return 1;
361 }
362
363 if (get_file_size(td, f))
364 return 1;
365
366 return 0;
367}
368
21972cde
JA
369int open_files(struct thread_data *td)
370{
371 struct fio_file *f;
372 int i, err = 0;
373
374 for_each_file(td, f, i) {
375 err = setup_file(td, f);
376 if (err)
377 break;
378 }
379
7abf833d
JA
380 if (!err)
381 return 0;
382
383 for_each_file(td, f, i) {
384 if (f->fd != -1) {
385 close(f->fd);
386 f->fd = -1;
387 }
388 }
389
21972cde
JA
390 return err;
391}
392
53cdc686
JA
393int setup_files(struct thread_data *td)
394{
395 struct fio_file *f;
21972cde 396 int err, i;
53cdc686
JA
397
398 /*
399 * if ioengine defines a setup() method, it's responsible for
400 * setting up everything in the td->files[] area.
401 */
402 if (td->io_ops->setup)
403 return td->io_ops->setup(td);
404
405 if (create_files(td))
406 return 1;
407
21972cde 408 err = open_files(td);
f1027063
JA
409 if (err)
410 return err;
411
0a7eb121
JA
412 /*
413 * Recalculate the total file size now that files are set up.
414 */
415 td->total_file_size = 0;
416 for_each_file(td, f, i)
417 td->total_file_size += f->file_size;
418
f1027063 419 td->io_size = td->total_file_size;
53cdc686
JA
420 if (td->io_size == 0) {
421 log_err("%s: no io blocks\n", td->name);
422 td_verror(td, EINVAL);
423 return 1;
424 }
425
426 if (!td->zone_size)
427 td->zone_size = td->io_size;
428
429 td->total_io_size = td->io_size * td->loops;
430
431 if (td->io_ops->flags & FIO_MMAPIO)
21972cde 432 err = setup_files_mmap(td);
53cdc686 433 else
21972cde
JA
434 err = setup_files_plain(td);
435
436 for_each_file(td, f, i) {
437 if (f->fd != -1) {
438 close(f->fd);
439 f->fd = -1;
440 }
441 }
442
443 return err;
53cdc686
JA
444}
445
446void close_files(struct thread_data *td)
447{
0ab8db89 448 struct fio_file *f;
53cdc686
JA
449 int i;
450
0ab8db89 451 for_each_file(td, f, i) {
9b031dc8 452 if (!td->filename && f->unlink &&
02bcaa8c 453 td->filetype == FIO_TYPE_FILE) {
132ad46d
JA
454 unlink(f->file_name);
455 free(f->file_name);
456 f->file_name = NULL;
457 }
21972cde
JA
458 if (f->fd != -1) {
459 close(f->fd);
460 f->fd = -1;
461 }
53cdc686
JA
462 if (f->mmap) {
463 munmap(f->mmap, f->file_size);
464 f->mmap = NULL;
465 }
466 }
b4a6a59a 467
132ad46d 468 td->filename = NULL;
b4a6a59a
JA
469 free(td->files);
470 td->files = NULL;
471 td->nr_files = 0;
53cdc686 472}