blob: 850dc2f078cd0967703624fc51f6dc2f634dae3f [file] [log] [blame]
Jens Axboe53cdc682006-10-18 11:50:58 +02001#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
Jens Axboebdb4e2e2007-03-01 09:54:57 +010011int open_file(struct thread_data *td, struct fio_file *f, int flags, int perm)
12{
13 if (flags & O_CREAT)
14 f->fd = open(f->file_name, flags, perm);
15 else
16 f->fd = open(f->file_name, flags);
17
18 if (f->fd != -1) {
19 td->nr_open_files++;
20 return 0;
21 }
22
23 return 1;
24}
25
26void close_file(struct thread_data *td, struct fio_file *f)
27{
28 if (f->fd != -1) {
29 close(f->fd);
30 f->fd = -1;
31 td->nr_open_files--;
32 }
33}
34
Jens Axboe25205e92006-10-19 20:50:14 +020035/*
36 * Check if the file exists and it's large enough.
37 */
38static int file_ok(struct thread_data *td, struct fio_file *f)
Jens Axboe53cdc682006-10-18 11:50:58 +020039{
Jens Axboeb2a15192006-10-18 14:10:42 +020040 struct stat st;
Jens Axboe53cdc682006-10-18 11:50:58 +020041
Jens Axboe4d9345a2007-02-07 13:14:57 +010042 if (td->filetype != FIO_TYPE_FILE || (td->io_ops->flags & FIO_NULLIO))
Jens Axboeb2a15192006-10-18 14:10:42 +020043 return 0;
44
Jens Axboefa01d132007-02-22 14:07:39 +010045 if (lstat(f->file_name, &st) == -1)
Jens Axboe25205e92006-10-19 20:50:14 +020046 return 1;
Jens Axboefa01d132007-02-22 14:07:39 +010047
48 /*
49 * if it's a special file, size is always ok for now
50 */
51 if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))
52 return 0;
53 if (st.st_size < (off_t) f->file_size)
Jens Axboe25205e92006-10-19 20:50:14 +020054 return 1;
55
56 return 0;
57}
58
59static int create_file(struct thread_data *td, struct fio_file *f)
60{
61 unsigned long long left;
62 unsigned int bs;
63 char *b;
64 int r;
Jens Axboeb2a15192006-10-18 14:10:42 +020065
Jens Axboe53cdc682006-10-18 11:50:58 +020066 f->fd = open(f->file_name, O_WRONLY | O_CREAT | O_TRUNC, 0644);
67 if (f->fd < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +010068 td_verror(td, errno, "open");
Jens Axboe53cdc682006-10-18 11:50:58 +020069 return 1;
70 }
71
72 if (ftruncate(f->fd, f->file_size) == -1) {
Jens Axboee1161c32007-02-22 19:36:48 +010073 td_verror(td, errno, "ftruncate");
Jens Axboe53cdc682006-10-18 11:50:58 +020074 goto err;
75 }
76
Jens Axboe40f82982006-10-25 11:21:05 +020077 if (posix_fallocate(f->fd, 0, f->file_size) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +010078 td_verror(td, errno, "posix_fallocate");
Jens Axboe40f82982006-10-25 11:21:05 +020079 goto err;
80 }
81
Jens Axboea00735e2006-11-03 08:58:08 +010082 b = malloc(td->max_bs[DDIR_WRITE]);
83 memset(b, 0, td->max_bs[DDIR_WRITE]);
Jens Axboe53cdc682006-10-18 11:50:58 +020084
85 left = f->file_size;
86 while (left && !td->terminate) {
Jens Axboea00735e2006-11-03 08:58:08 +010087 bs = td->max_bs[DDIR_WRITE];
Jens Axboe53cdc682006-10-18 11:50:58 +020088 if (bs > left)
89 bs = left;
90
91 r = write(f->fd, b, bs);
92
93 if (r == (int) bs) {
94 left -= bs;
95 continue;
96 } else {
97 if (r < 0)
Jens Axboee1161c32007-02-22 19:36:48 +010098 td_verror(td, errno, "write");
Jens Axboe53cdc682006-10-18 11:50:58 +020099 else
Jens Axboee1161c32007-02-22 19:36:48 +0100100 td_verror(td, EIO, "write");
Jens Axboe53cdc682006-10-18 11:50:58 +0200101
102 break;
103 }
104 }
105
106 if (td->terminate)
107 unlink(f->file_name);
108 else if (td->create_fsync)
109 fsync(f->fd);
110
111 free(b);
112 close(f->fd);
113 f->fd = -1;
114 return 0;
115err:
116 close(f->fd);
117 f->fd = -1;
118 return 1;
119}
120
121static int create_files(struct thread_data *td)
122{
123 struct fio_file *f;
Jens Axboe25205e92006-10-19 20:50:14 +0200124 int i, err, need_create;
Jens Axboe53cdc682006-10-18 11:50:58 +0200125
Jens Axboef6971252006-11-15 10:00:31 +0100126 for_each_file(td, f, i)
127 f->file_size = td->total_file_size / td->nr_files;
128
Jens Axboe53cdc682006-10-18 11:50:58 +0200129 /*
130 * unless specifically asked for overwrite, let normal io extend it
131 */
Jens Axboef6971252006-11-15 10:00:31 +0100132 if (!td->overwrite)
Jens Axboe53cdc682006-10-18 11:50:58 +0200133 return 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200134
Jens Axboef1027062006-10-20 10:14:01 +0200135 need_create = 0;
Jens Axboeeff68612007-01-14 05:56:44 +0100136 if (td->filetype == FIO_TYPE_FILE) {
137 for_each_file(td, f, i) {
138 int file_there = !file_ok(td, f);
139
Jens Axboe413dd452007-02-23 09:26:09 +0100140 if (file_there && td_write(td) && !td->overwrite) {
Jens Axboeeff68612007-01-14 05:56:44 +0100141 unlink(f->file_name);
142 file_there = 0;
143 }
144
145 need_create += !file_there;
146 }
147 }
Jens Axboef1027062006-10-20 10:14:01 +0200148
149 if (!need_create)
150 return 0;
151
Jens Axboe53cdc682006-10-18 11:50:58 +0200152 if (!td->total_file_size) {
153 log_err("Need size for create\n");
Jens Axboee1161c32007-02-22 19:36:48 +0100154 td_verror(td, EINVAL, "file_size");
Jens Axboe53cdc682006-10-18 11:50:58 +0200155 return 1;
156 }
157
Jens Axboe25205e92006-10-19 20:50:14 +0200158 temp_stall_ts = 1;
Jens Axboe1e97cce2006-12-05 11:44:16 +0100159 fprintf(f_out, "%s: Laying out IO file(s) (%u x %LuMiB == %LuMiB)\n",
Jens Axboe13f8e2d2006-10-24 09:29:45 +0200160 td->name, td->nr_uniq_files,
161 (td->total_file_size >> 20) / td->nr_uniq_files,
Jens Axboe25205e92006-10-19 20:50:14 +0200162 td->total_file_size >> 20);
163
164 err = 0;
165 for_each_file(td, f, i) {
Jens Axboe9b031dc2006-12-15 16:01:54 +0100166 /*
167 * Only unlink files that we created.
168 */
169 f->unlink = 0;
Jens Axboe25205e92006-10-19 20:50:14 +0200170 if (file_ok(td, f)) {
Jens Axboe9b031dc2006-12-15 16:01:54 +0100171 f->unlink = td->unlink;
Jens Axboe25205e92006-10-19 20:50:14 +0200172 err = create_file(td, f);
173 if (err)
174 break;
175 }
176 }
177
Jens Axboe53cdc682006-10-18 11:50:58 +0200178 temp_stall_ts = 0;
179 return err;
180}
181
182static int file_size(struct thread_data *td, struct fio_file *f)
183{
184 struct stat st;
185
Jens Axboef4ee22c2007-02-10 18:57:18 +0100186 /*
187 * if we are not doing real io, just pretend the file is as large
188 * as the size= given. this works fine with nrfiles > 1 as well,
189 * we only really care about it being at least as big as size=
190 */
191 if (td->io_ops->flags & FIO_NULLIO) {
192 f->real_file_size = f->file_size = td->total_file_size;
193 return 0;
194 }
195
Jens Axboe53cdc682006-10-18 11:50:58 +0200196 if (td->overwrite) {
197 if (fstat(f->fd, &st) == -1) {
Jens Axboee1161c32007-02-22 19:36:48 +0100198 td_verror(td, errno, "fstat");
Jens Axboe53cdc682006-10-18 11:50:58 +0200199 return 1;
200 }
201
202 f->real_file_size = st.st_size;
203
204 if (!f->file_size || f->file_size > f->real_file_size)
205 f->file_size = f->real_file_size;
Jens Axboe745508d2007-01-08 10:25:51 +0100206 } else
207 f->real_file_size = f->file_size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200208
Jens Axboe53cdc682006-10-18 11:50:58 +0200209 return 0;
210}
211
212static int bdev_size(struct thread_data *td, struct fio_file *f)
213{
214 unsigned long long bytes;
215 int r;
216
217 r = blockdev_size(f->fd, &bytes);
218 if (r) {
Jens Axboee1161c32007-02-22 19:36:48 +0100219 td_verror(td, r, "blockdev_size");
Jens Axboe53cdc682006-10-18 11:50:58 +0200220 return 1;
221 }
222
223 f->real_file_size = bytes;
224
225 /*
226 * no extend possibilities, so limit size to device size if too large
227 */
228 if (!f->file_size || f->file_size > f->real_file_size)
229 f->file_size = f->real_file_size;
230
231 f->file_size -= f->file_offset;
232 return 0;
233}
234
235static int get_file_size(struct thread_data *td, struct fio_file *f)
236{
237 int ret = 0;
238
239 if (td->filetype == FIO_TYPE_FILE)
240 ret = file_size(td, f);
241 else if (td->filetype == FIO_TYPE_BD)
242 ret = bdev_size(td, f);
243 else
244 f->real_file_size = -1;
245
246 if (ret)
247 return ret;
248
249 if (f->file_offset > f->real_file_size) {
250 log_err("%s: offset extends end (%Lu > %Lu)\n", td->name, f->file_offset, f->real_file_size);
251 return 1;
252 }
253
Jens Axboe53cdc682006-10-18 11:50:58 +0200254 return 0;
255}
256
Jens Axboee5b401d2006-10-18 16:03:40 +0200257int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
258{
259 int ret = 0;
260
261 /*
262 * FIXME: add blockdev flushing too
263 */
264 if (td->io_ops->flags & FIO_MMAPIO)
265 ret = madvise(f->mmap, f->file_size, MADV_DONTNEED);
Jens Axboead2da602007-02-26 12:33:27 +0100266 else if (td->filetype == FIO_TYPE_FILE) {
267 if (!td->odirect)
268 ret = fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_DONTNEED);
269 } else if (td->filetype == FIO_TYPE_BD) {
270 if (!td->odirect)
271 ret = blockdev_invalidate_cache(f->fd);
272 } else if (td->filetype == FIO_TYPE_CHAR)
Jens Axboee5b401d2006-10-18 16:03:40 +0200273 ret = 0;
274
275 if (ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100276 td_verror(td, errno, "invalidate_cache");
Jens Axboee5b401d2006-10-18 16:03:40 +0200277 return 1;
278 }
279
Jens Axboead2da602007-02-26 12:33:27 +0100280 return ret;
Jens Axboee5b401d2006-10-18 16:03:40 +0200281}
282
Jens Axboe53cdc682006-10-18 11:50:58 +0200283static int __setup_file_mmap(struct thread_data *td, struct fio_file *f)
284{
285 int flags;
286
287 if (td_rw(td))
288 flags = PROT_READ | PROT_WRITE;
289 else if (td_write(td)) {
290 flags = PROT_WRITE;
291
292 if (td->verify != VERIFY_NONE)
293 flags |= PROT_READ;
294 } else
295 flags = PROT_READ;
296
297 f->mmap = mmap(NULL, f->file_size, flags, MAP_SHARED, f->fd, f->file_offset);
298 if (f->mmap == MAP_FAILED) {
299 f->mmap = NULL;
Jens Axboee1161c32007-02-22 19:36:48 +0100300 td_verror(td, errno, "mmap");
Jens Axboe53cdc682006-10-18 11:50:58 +0200301 return 1;
302 }
303
Jens Axboee5b401d2006-10-18 16:03:40 +0200304 if (td->invalidate_cache && file_invalidate_cache(td, f))
305 return 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200306
Jens Axboe413dd452007-02-23 09:26:09 +0100307 if (!td_random(td)) {
Jens Axboe53cdc682006-10-18 11:50:58 +0200308 if (madvise(f->mmap, f->file_size, MADV_SEQUENTIAL) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100309 td_verror(td, errno, "madvise");
Jens Axboe53cdc682006-10-18 11:50:58 +0200310 return 1;
311 }
312 } else {
313 if (madvise(f->mmap, f->file_size, MADV_RANDOM) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100314 td_verror(td, errno, "madvise");
Jens Axboe53cdc682006-10-18 11:50:58 +0200315 return 1;
316 }
317 }
318
319 return 0;
320}
321
322static int setup_files_mmap(struct thread_data *td)
323{
324 struct fio_file *f;
325 int i, err = 0;
326
327 for_each_file(td, f, i) {
328 err = __setup_file_mmap(td, f);
329 if (err)
330 break;
331 }
332
333 return err;
334}
335
336static int __setup_file_plain(struct thread_data *td, struct fio_file *f)
337{
Jens Axboee5b401d2006-10-18 16:03:40 +0200338 if (td->invalidate_cache && file_invalidate_cache(td, f))
339 return 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200340
Jens Axboe413dd452007-02-23 09:26:09 +0100341 if (!td_random(td)) {
Jens Axboe53cdc682006-10-18 11:50:58 +0200342 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100343 td_verror(td, errno, "fadvise");
Jens Axboe53cdc682006-10-18 11:50:58 +0200344 return 1;
345 }
346 } else {
347 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_RANDOM) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100348 td_verror(td, errno, "fadvise");
Jens Axboe53cdc682006-10-18 11:50:58 +0200349 return 1;
350 }
351 }
352
353 return 0;
354}
355
356static int setup_files_plain(struct thread_data *td)
357{
358 struct fio_file *f;
359 int i, err = 0;
360
361 for_each_file(td, f, i) {
362 err = __setup_file_plain(td, f);
363 if (err)
364 break;
365 }
366
367 return err;
368}
369
370static int setup_file(struct thread_data *td, struct fio_file *f)
371{
Jens Axboe53cdc682006-10-18 11:50:58 +0200372 int flags = 0;
373
Joel Beckera9defc92007-03-01 08:26:38 +0100374 if (td->io_ops->flags & FIO_SELFOPEN)
Jens Axboeed92ac02007-02-06 14:43:52 +0100375 return 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200376
Jens Axboe4d9345a2007-02-07 13:14:57 +0100377 /*
378 * we need a valid file descriptor, but don't create a real file.
379 * lets just dup stdout, seems like a sensible approach.
380 */
381 if (td->io_ops->flags & FIO_NULLIO)
382 f->fd = dup(STDOUT_FILENO);
383 else {
384 if (td->odirect)
385 flags |= OS_O_DIRECT;
386 if (td->sync_io)
387 flags |= O_SYNC;
Jens Axboe7abf8332006-11-23 15:01:19 +0100388
Jens Axboe4d9345a2007-02-07 13:14:57 +0100389 if (td_write(td) || td_rw(td)) {
Jens Axboe53cdc682006-10-18 11:50:58 +0200390 flags |= O_RDWR;
Jens Axboe53cdc682006-10-18 11:50:58 +0200391
Jens Axboe4d9345a2007-02-07 13:14:57 +0100392 if (td->filetype == FIO_TYPE_FILE) {
393 if (!td->overwrite)
394 flags |= O_TRUNC;
395
396 flags |= O_CREAT;
397 }
398
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100399 open_file(td, f, flags, 0600);
Jens Axboe4d9345a2007-02-07 13:14:57 +0100400 } else {
401 if (td->filetype == FIO_TYPE_CHAR)
402 flags |= O_RDWR;
403 else
404 flags |= O_RDONLY;
405
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100406 open_file(td, f, flags, 0);
Jens Axboe4d9345a2007-02-07 13:14:57 +0100407 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200408 }
409
410 if (f->fd == -1) {
Jens Axboee1161c32007-02-22 19:36:48 +0100411 int __e = errno;
412
413 td_verror(td, __e, "open");
414 if (__e == EINVAL && td->odirect)
Jens Axboe9d80e112007-02-22 19:37:53 +0100415 log_err("fio: destination does not support O_DIRECT\n");
Jens Axboe53cdc682006-10-18 11:50:58 +0200416 return 1;
417 }
418
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100419 if (get_file_size(td, f)) {
420 close_file(td, f);
Jens Axboe53cdc682006-10-18 11:50:58 +0200421 return 1;
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100422 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200423
424 return 0;
425}
426
Jens Axboe21972cd2006-11-23 12:39:16 +0100427int open_files(struct thread_data *td)
428{
429 struct fio_file *f;
430 int i, err = 0;
431
432 for_each_file(td, f, i) {
433 err = setup_file(td, f);
434 if (err)
435 break;
436 }
437
Jens Axboe7abf8332006-11-23 15:01:19 +0100438 if (!err)
439 return 0;
440
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100441 for_each_file(td, f, i)
442 close_file(td, f);
Jens Axboe7abf8332006-11-23 15:01:19 +0100443
Jens Axboe21972cd2006-11-23 12:39:16 +0100444 return err;
445}
446
Jens Axboe53cdc682006-10-18 11:50:58 +0200447int setup_files(struct thread_data *td)
448{
449 struct fio_file *f;
Jens Axboe21972cd2006-11-23 12:39:16 +0100450 int err, i;
Jens Axboe53cdc682006-10-18 11:50:58 +0200451
452 /*
453 * if ioengine defines a setup() method, it's responsible for
454 * setting up everything in the td->files[] area.
455 */
456 if (td->io_ops->setup)
457 return td->io_ops->setup(td);
458
459 if (create_files(td))
460 return 1;
461
Jens Axboe21972cd2006-11-23 12:39:16 +0100462 err = open_files(td);
Jens Axboef1027062006-10-20 10:14:01 +0200463 if (err)
464 return err;
465
Jens Axboe0a7eb122006-10-20 10:36:42 +0200466 /*
467 * Recalculate the total file size now that files are set up.
468 */
469 td->total_file_size = 0;
470 for_each_file(td, f, i)
471 td->total_file_size += f->file_size;
472
Jens Axboef1027062006-10-20 10:14:01 +0200473 td->io_size = td->total_file_size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200474 if (td->io_size == 0) {
475 log_err("%s: no io blocks\n", td->name);
Jens Axboee1161c32007-02-22 19:36:48 +0100476 td_verror(td, EINVAL, "total_file_size");
Jens Axboe53cdc682006-10-18 11:50:58 +0200477 return 1;
478 }
479
480 if (!td->zone_size)
481 td->zone_size = td->io_size;
482
483 td->total_io_size = td->io_size * td->loops;
484
485 if (td->io_ops->flags & FIO_MMAPIO)
Jens Axboe21972cd2006-11-23 12:39:16 +0100486 err = setup_files_mmap(td);
Jens Axboe53cdc682006-10-18 11:50:58 +0200487 else
Jens Axboe21972cd2006-11-23 12:39:16 +0100488 err = setup_files_plain(td);
489
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100490 for_each_file(td, f, i)
491 close_file(td, f);
Jens Axboe21972cd2006-11-23 12:39:16 +0100492
493 return err;
Jens Axboe53cdc682006-10-18 11:50:58 +0200494}
495
496void close_files(struct thread_data *td)
497{
Jens Axboe0ab8db82006-10-18 17:16:23 +0200498 struct fio_file *f;
Jens Axboe53cdc682006-10-18 11:50:58 +0200499 int i;
500
Jens Axboe0ab8db82006-10-18 17:16:23 +0200501 for_each_file(td, f, i) {
Jens Axboe9b031dc2006-12-15 16:01:54 +0100502 if (!td->filename && f->unlink &&
Jens Axboe02bcaa82006-11-24 10:42:00 +0100503 td->filetype == FIO_TYPE_FILE) {
Jens Axboe132ad462006-11-23 12:23:12 +0100504 unlink(f->file_name);
505 free(f->file_name);
506 f->file_name = NULL;
507 }
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100508
509 close_file(td, f);
510
Jens Axboe53cdc682006-10-18 11:50:58 +0200511 if (f->mmap) {
512 munmap(f->mmap, f->file_size);
513 f->mmap = NULL;
514 }
515 }
Jens Axboeb4a6a592006-10-20 13:54:47 +0200516
Jens Axboe132ad462006-11-23 12:23:12 +0100517 td->filename = NULL;
Jens Axboeb4a6a592006-10-20 13:54:47 +0200518 free(td->files);
519 td->files = NULL;
520 td->nr_files = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200521}