blob: 11138b7a5cddc662f0b872819ecc812d40361196 [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 Axboe25205e92006-10-19 20:50:14 +020011/*
12 * Check if the file exists and it's large enough.
13 */
14static int file_ok(struct thread_data *td, struct fio_file *f)
Jens Axboe53cdc682006-10-18 11:50:58 +020015{
Jens Axboeb2a15192006-10-18 14:10:42 +020016 struct stat st;
Jens Axboe53cdc682006-10-18 11:50:58 +020017
Jens Axboe4d9345a2007-02-07 13:14:57 +010018 if (td->filetype != FIO_TYPE_FILE || (td->io_ops->flags & FIO_NULLIO))
Jens Axboeb2a15192006-10-18 14:10:42 +020019 return 0;
20
Jens Axboefa01d132007-02-22 14:07:39 +010021 if (lstat(f->file_name, &st) == -1)
Jens Axboe25205e92006-10-19 20:50:14 +020022 return 1;
Jens Axboefa01d132007-02-22 14:07:39 +010023
24 /*
25 * if it's a special file, size is always ok for now
26 */
27 if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))
28 return 0;
29 if (st.st_size < (off_t) f->file_size)
Jens Axboe25205e92006-10-19 20:50:14 +020030 return 1;
31
32 return 0;
33}
34
35static int create_file(struct thread_data *td, struct fio_file *f)
36{
37 unsigned long long left;
38 unsigned int bs;
39 char *b;
40 int r;
Jens Axboeb2a15192006-10-18 14:10:42 +020041
Jens Axboe53cdc682006-10-18 11:50:58 +020042 f->fd = open(f->file_name, O_WRONLY | O_CREAT | O_TRUNC, 0644);
43 if (f->fd < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +010044 td_verror(td, errno, "open");
Jens Axboe53cdc682006-10-18 11:50:58 +020045 return 1;
46 }
47
48 if (ftruncate(f->fd, f->file_size) == -1) {
Jens Axboee1161c32007-02-22 19:36:48 +010049 td_verror(td, errno, "ftruncate");
Jens Axboe53cdc682006-10-18 11:50:58 +020050 goto err;
51 }
52
Jens Axboe40f82982006-10-25 11:21:05 +020053 if (posix_fallocate(f->fd, 0, f->file_size) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +010054 td_verror(td, errno, "posix_fallocate");
Jens Axboe40f82982006-10-25 11:21:05 +020055 goto err;
56 }
57
Jens Axboea00735e2006-11-03 08:58:08 +010058 b = malloc(td->max_bs[DDIR_WRITE]);
59 memset(b, 0, td->max_bs[DDIR_WRITE]);
Jens Axboe53cdc682006-10-18 11:50:58 +020060
61 left = f->file_size;
62 while (left && !td->terminate) {
Jens Axboea00735e2006-11-03 08:58:08 +010063 bs = td->max_bs[DDIR_WRITE];
Jens Axboe53cdc682006-10-18 11:50:58 +020064 if (bs > left)
65 bs = left;
66
67 r = write(f->fd, b, bs);
68
69 if (r == (int) bs) {
70 left -= bs;
71 continue;
72 } else {
73 if (r < 0)
Jens Axboee1161c32007-02-22 19:36:48 +010074 td_verror(td, errno, "write");
Jens Axboe53cdc682006-10-18 11:50:58 +020075 else
Jens Axboee1161c32007-02-22 19:36:48 +010076 td_verror(td, EIO, "write");
Jens Axboe53cdc682006-10-18 11:50:58 +020077
78 break;
79 }
80 }
81
82 if (td->terminate)
83 unlink(f->file_name);
84 else if (td->create_fsync)
85 fsync(f->fd);
86
87 free(b);
88 close(f->fd);
89 f->fd = -1;
90 return 0;
91err:
92 close(f->fd);
93 f->fd = -1;
94 return 1;
95}
96
97static int create_files(struct thread_data *td)
98{
99 struct fio_file *f;
Jens Axboe25205e92006-10-19 20:50:14 +0200100 int i, err, need_create;
Jens Axboe53cdc682006-10-18 11:50:58 +0200101
Jens Axboef6971252006-11-15 10:00:31 +0100102 for_each_file(td, f, i)
103 f->file_size = td->total_file_size / td->nr_files;
104
Jens Axboe53cdc682006-10-18 11:50:58 +0200105 /*
106 * unless specifically asked for overwrite, let normal io extend it
107 */
Jens Axboef6971252006-11-15 10:00:31 +0100108 if (!td->overwrite)
Jens Axboe53cdc682006-10-18 11:50:58 +0200109 return 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200110
Jens Axboef1027062006-10-20 10:14:01 +0200111 need_create = 0;
Jens Axboeeff68612007-01-14 05:56:44 +0100112 if (td->filetype == FIO_TYPE_FILE) {
113 for_each_file(td, f, i) {
114 int file_there = !file_ok(td, f);
115
Jens Axboe3404cef2007-01-24 09:21:31 +0100116 if (file_there && td->ddir == DDIR_WRITE &&
117 !td->overwrite) {
Jens Axboeeff68612007-01-14 05:56:44 +0100118 unlink(f->file_name);
119 file_there = 0;
120 }
121
122 need_create += !file_there;
123 }
124 }
Jens Axboef1027062006-10-20 10:14:01 +0200125
126 if (!need_create)
127 return 0;
128
Jens Axboe53cdc682006-10-18 11:50:58 +0200129 if (!td->total_file_size) {
130 log_err("Need size for create\n");
Jens Axboee1161c32007-02-22 19:36:48 +0100131 td_verror(td, EINVAL, "file_size");
Jens Axboe53cdc682006-10-18 11:50:58 +0200132 return 1;
133 }
134
Jens Axboe25205e92006-10-19 20:50:14 +0200135 temp_stall_ts = 1;
Jens Axboe1e97cce2006-12-05 11:44:16 +0100136 fprintf(f_out, "%s: Laying out IO file(s) (%u x %LuMiB == %LuMiB)\n",
Jens Axboe13f8e2d2006-10-24 09:29:45 +0200137 td->name, td->nr_uniq_files,
138 (td->total_file_size >> 20) / td->nr_uniq_files,
Jens Axboe25205e92006-10-19 20:50:14 +0200139 td->total_file_size >> 20);
140
141 err = 0;
142 for_each_file(td, f, i) {
Jens Axboe9b031dc2006-12-15 16:01:54 +0100143 /*
144 * Only unlink files that we created.
145 */
146 f->unlink = 0;
Jens Axboe25205e92006-10-19 20:50:14 +0200147 if (file_ok(td, f)) {
Jens Axboe9b031dc2006-12-15 16:01:54 +0100148 f->unlink = td->unlink;
Jens Axboe25205e92006-10-19 20:50:14 +0200149 err = create_file(td, f);
150 if (err)
151 break;
152 }
153 }
154
Jens Axboe53cdc682006-10-18 11:50:58 +0200155 temp_stall_ts = 0;
156 return err;
157}
158
159static int file_size(struct thread_data *td, struct fio_file *f)
160{
161 struct stat st;
162
Jens Axboef4ee22c2007-02-10 18:57:18 +0100163 /*
164 * if we are not doing real io, just pretend the file is as large
165 * as the size= given. this works fine with nrfiles > 1 as well,
166 * we only really care about it being at least as big as size=
167 */
168 if (td->io_ops->flags & FIO_NULLIO) {
169 f->real_file_size = f->file_size = td->total_file_size;
170 return 0;
171 }
172
Jens Axboe53cdc682006-10-18 11:50:58 +0200173 if (td->overwrite) {
174 if (fstat(f->fd, &st) == -1) {
Jens Axboee1161c32007-02-22 19:36:48 +0100175 td_verror(td, errno, "fstat");
Jens Axboe53cdc682006-10-18 11:50:58 +0200176 return 1;
177 }
178
179 f->real_file_size = st.st_size;
180
181 if (!f->file_size || f->file_size > f->real_file_size)
182 f->file_size = f->real_file_size;
Jens Axboe745508d2007-01-08 10:25:51 +0100183 } else
184 f->real_file_size = f->file_size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200185
Jens Axboe53cdc682006-10-18 11:50:58 +0200186 return 0;
187}
188
189static int bdev_size(struct thread_data *td, struct fio_file *f)
190{
191 unsigned long long bytes;
192 int r;
193
194 r = blockdev_size(f->fd, &bytes);
195 if (r) {
Jens Axboee1161c32007-02-22 19:36:48 +0100196 td_verror(td, r, "blockdev_size");
Jens Axboe53cdc682006-10-18 11:50:58 +0200197 return 1;
198 }
199
200 f->real_file_size = bytes;
201
202 /*
203 * no extend possibilities, so limit size to device size if too large
204 */
205 if (!f->file_size || f->file_size > f->real_file_size)
206 f->file_size = f->real_file_size;
207
208 f->file_size -= f->file_offset;
209 return 0;
210}
211
212static int get_file_size(struct thread_data *td, struct fio_file *f)
213{
214 int ret = 0;
215
216 if (td->filetype == FIO_TYPE_FILE)
217 ret = file_size(td, f);
218 else if (td->filetype == FIO_TYPE_BD)
219 ret = bdev_size(td, f);
220 else
221 f->real_file_size = -1;
222
223 if (ret)
224 return ret;
225
226 if (f->file_offset > f->real_file_size) {
227 log_err("%s: offset extends end (%Lu > %Lu)\n", td->name, f->file_offset, f->real_file_size);
228 return 1;
229 }
230
Jens Axboe53cdc682006-10-18 11:50:58 +0200231 return 0;
232}
233
Jens Axboee5b401d2006-10-18 16:03:40 +0200234int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
235{
236 int ret = 0;
237
238 /*
239 * FIXME: add blockdev flushing too
240 */
241 if (td->io_ops->flags & FIO_MMAPIO)
242 ret = madvise(f->mmap, f->file_size, MADV_DONTNEED);
243 else if (td->filetype == FIO_TYPE_FILE)
244 ret = fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_DONTNEED);
245 else if (td->filetype == FIO_TYPE_BD)
246 ret = blockdev_invalidate_cache(f->fd);
247 else if (td->filetype == FIO_TYPE_CHAR)
248 ret = 0;
249
250 if (ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100251 td_verror(td, errno, "invalidate_cache");
Jens Axboee5b401d2006-10-18 16:03:40 +0200252 return 1;
253 }
254
255 return 0;
256}
257
Jens Axboe53cdc682006-10-18 11:50:58 +0200258static int __setup_file_mmap(struct thread_data *td, struct fio_file *f)
259{
260 int flags;
261
262 if (td_rw(td))
263 flags = PROT_READ | PROT_WRITE;
264 else if (td_write(td)) {
265 flags = PROT_WRITE;
266
267 if (td->verify != VERIFY_NONE)
268 flags |= PROT_READ;
269 } else
270 flags = PROT_READ;
271
272 f->mmap = mmap(NULL, f->file_size, flags, MAP_SHARED, f->fd, f->file_offset);
273 if (f->mmap == MAP_FAILED) {
274 f->mmap = NULL;
Jens Axboee1161c32007-02-22 19:36:48 +0100275 td_verror(td, errno, "mmap");
Jens Axboe53cdc682006-10-18 11:50:58 +0200276 return 1;
277 }
278
Jens Axboee5b401d2006-10-18 16:03:40 +0200279 if (td->invalidate_cache && file_invalidate_cache(td, f))
280 return 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200281
282 if (td->sequential) {
283 if (madvise(f->mmap, f->file_size, MADV_SEQUENTIAL) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100284 td_verror(td, errno, "madvise");
Jens Axboe53cdc682006-10-18 11:50:58 +0200285 return 1;
286 }
287 } else {
288 if (madvise(f->mmap, f->file_size, MADV_RANDOM) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100289 td_verror(td, errno, "madvise");
Jens Axboe53cdc682006-10-18 11:50:58 +0200290 return 1;
291 }
292 }
293
294 return 0;
295}
296
297static int setup_files_mmap(struct thread_data *td)
298{
299 struct fio_file *f;
300 int i, err = 0;
301
302 for_each_file(td, f, i) {
303 err = __setup_file_mmap(td, f);
304 if (err)
305 break;
306 }
307
308 return err;
309}
310
311static int __setup_file_plain(struct thread_data *td, struct fio_file *f)
312{
Jens Axboee5b401d2006-10-18 16:03:40 +0200313 if (td->invalidate_cache && file_invalidate_cache(td, f))
314 return 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200315
316 if (td->sequential) {
317 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100318 td_verror(td, errno, "fadvise");
Jens Axboe53cdc682006-10-18 11:50:58 +0200319 return 1;
320 }
321 } else {
322 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_RANDOM) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100323 td_verror(td, errno, "fadvise");
Jens Axboe53cdc682006-10-18 11:50:58 +0200324 return 1;
325 }
326 }
327
328 return 0;
329}
330
331static int setup_files_plain(struct thread_data *td)
332{
333 struct fio_file *f;
334 int i, err = 0;
335
336 for_each_file(td, f, i) {
337 err = __setup_file_plain(td, f);
338 if (err)
339 break;
340 }
341
342 return err;
343}
344
345static int setup_file(struct thread_data *td, struct fio_file *f)
346{
Jens Axboe53cdc682006-10-18 11:50:58 +0200347 int flags = 0;
348
Jens Axboeed92ac02007-02-06 14:43:52 +0100349 if (td->io_ops->flags & FIO_NETIO)
350 return 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200351
Jens Axboe4d9345a2007-02-07 13:14:57 +0100352 /*
353 * we need a valid file descriptor, but don't create a real file.
354 * lets just dup stdout, seems like a sensible approach.
355 */
356 if (td->io_ops->flags & FIO_NULLIO)
357 f->fd = dup(STDOUT_FILENO);
358 else {
359 if (td->odirect)
360 flags |= OS_O_DIRECT;
361 if (td->sync_io)
362 flags |= O_SYNC;
Jens Axboe7abf8332006-11-23 15:01:19 +0100363
Jens Axboe4d9345a2007-02-07 13:14:57 +0100364 if (td_write(td) || td_rw(td)) {
Jens Axboe53cdc682006-10-18 11:50:58 +0200365 flags |= O_RDWR;
Jens Axboe53cdc682006-10-18 11:50:58 +0200366
Jens Axboe4d9345a2007-02-07 13:14:57 +0100367 if (td->filetype == FIO_TYPE_FILE) {
368 if (!td->overwrite)
369 flags |= O_TRUNC;
370
371 flags |= O_CREAT;
372 }
373
374 f->fd = open(f->file_name, flags, 0600);
375 } else {
376 if (td->filetype == FIO_TYPE_CHAR)
377 flags |= O_RDWR;
378 else
379 flags |= O_RDONLY;
380
381 f->fd = open(f->file_name, flags);
382 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200383 }
384
385 if (f->fd == -1) {
Jens Axboee1161c32007-02-22 19:36:48 +0100386 int __e = errno;
387
388 td_verror(td, __e, "open");
389 if (__e == EINVAL && td->odirect)
Jens Axboe9d80e112007-02-22 19:37:53 +0100390 log_err("fio: destination does not support O_DIRECT\n");
Jens Axboe53cdc682006-10-18 11:50:58 +0200391 return 1;
392 }
393
394 if (get_file_size(td, f))
395 return 1;
396
397 return 0;
398}
399
Jens Axboe21972cd2006-11-23 12:39:16 +0100400int open_files(struct thread_data *td)
401{
402 struct fio_file *f;
403 int i, err = 0;
404
405 for_each_file(td, f, i) {
406 err = setup_file(td, f);
407 if (err)
408 break;
409 }
410
Jens Axboe7abf8332006-11-23 15:01:19 +0100411 if (!err)
412 return 0;
413
414 for_each_file(td, f, i) {
415 if (f->fd != -1) {
416 close(f->fd);
417 f->fd = -1;
418 }
419 }
420
Jens Axboe21972cd2006-11-23 12:39:16 +0100421 return err;
422}
423
Jens Axboe53cdc682006-10-18 11:50:58 +0200424int setup_files(struct thread_data *td)
425{
426 struct fio_file *f;
Jens Axboe21972cd2006-11-23 12:39:16 +0100427 int err, i;
Jens Axboe53cdc682006-10-18 11:50:58 +0200428
429 /*
430 * if ioengine defines a setup() method, it's responsible for
431 * setting up everything in the td->files[] area.
432 */
433 if (td->io_ops->setup)
434 return td->io_ops->setup(td);
435
436 if (create_files(td))
437 return 1;
438
Jens Axboe21972cd2006-11-23 12:39:16 +0100439 err = open_files(td);
Jens Axboef1027062006-10-20 10:14:01 +0200440 if (err)
441 return err;
442
Jens Axboe0a7eb122006-10-20 10:36:42 +0200443 /*
444 * Recalculate the total file size now that files are set up.
445 */
446 td->total_file_size = 0;
447 for_each_file(td, f, i)
448 td->total_file_size += f->file_size;
449
Jens Axboef1027062006-10-20 10:14:01 +0200450 td->io_size = td->total_file_size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200451 if (td->io_size == 0) {
452 log_err("%s: no io blocks\n", td->name);
Jens Axboee1161c32007-02-22 19:36:48 +0100453 td_verror(td, EINVAL, "total_file_size");
Jens Axboe53cdc682006-10-18 11:50:58 +0200454 return 1;
455 }
456
457 if (!td->zone_size)
458 td->zone_size = td->io_size;
459
460 td->total_io_size = td->io_size * td->loops;
461
462 if (td->io_ops->flags & FIO_MMAPIO)
Jens Axboe21972cd2006-11-23 12:39:16 +0100463 err = setup_files_mmap(td);
Jens Axboe53cdc682006-10-18 11:50:58 +0200464 else
Jens Axboe21972cd2006-11-23 12:39:16 +0100465 err = setup_files_plain(td);
466
467 for_each_file(td, f, i) {
468 if (f->fd != -1) {
469 close(f->fd);
470 f->fd = -1;
471 }
472 }
473
474 return err;
Jens Axboe53cdc682006-10-18 11:50:58 +0200475}
476
477void close_files(struct thread_data *td)
478{
Jens Axboe0ab8db82006-10-18 17:16:23 +0200479 struct fio_file *f;
Jens Axboe53cdc682006-10-18 11:50:58 +0200480 int i;
481
Jens Axboe0ab8db82006-10-18 17:16:23 +0200482 for_each_file(td, f, i) {
Jens Axboe9b031dc2006-12-15 16:01:54 +0100483 if (!td->filename && f->unlink &&
Jens Axboe02bcaa82006-11-24 10:42:00 +0100484 td->filetype == FIO_TYPE_FILE) {
Jens Axboe132ad462006-11-23 12:23:12 +0100485 unlink(f->file_name);
486 free(f->file_name);
487 f->file_name = NULL;
488 }
Jens Axboe21972cd2006-11-23 12:39:16 +0100489 if (f->fd != -1) {
490 close(f->fd);
491 f->fd = -1;
492 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200493 if (f->mmap) {
494 munmap(f->mmap, f->file_size);
495 f->mmap = NULL;
496 }
497 }
Jens Axboeb4a6a592006-10-20 13:54:47 +0200498
Jens Axboe132ad462006-11-23 12:23:12 +0100499 td->filename = NULL;
Jens Axboeb4a6a592006-10-20 13:54:47 +0200500 free(td->files);
501 td->files = NULL;
502 td->nr_files = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200503}