blob: f4004588fcd4f9ac803322528d8a5f5876027510 [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 Axboeb5af8292007-03-08 12:43:13 +010018 if (td->filetype != FIO_TYPE_FILE ||
19 (td->io_ops->flags & FIO_DISKLESSIO))
Jens Axboeb2a15192006-10-18 14:10:42 +020020 return 0;
21
Jens Axboefa01d132007-02-22 14:07:39 +010022 if (lstat(f->file_name, &st) == -1)
Jens Axboe25205e92006-10-19 20:50:14 +020023 return 1;
Jens Axboefa01d132007-02-22 14:07:39 +010024
25 /*
26 * if it's a special file, size is always ok for now
27 */
28 if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))
29 return 0;
30 if (st.st_size < (off_t) f->file_size)
Jens Axboe25205e92006-10-19 20:50:14 +020031 return 1;
32
33 return 0;
34}
35
36static int create_file(struct thread_data *td, struct fio_file *f)
37{
38 unsigned long long left;
39 unsigned int bs;
40 char *b;
41 int r;
Jens Axboeb2a15192006-10-18 14:10:42 +020042
Jens Axboe53cdc682006-10-18 11:50:58 +020043 f->fd = open(f->file_name, O_WRONLY | O_CREAT | O_TRUNC, 0644);
44 if (f->fd < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +010045 td_verror(td, errno, "open");
Jens Axboe53cdc682006-10-18 11:50:58 +020046 return 1;
47 }
48
49 if (ftruncate(f->fd, f->file_size) == -1) {
Jens Axboee1161c32007-02-22 19:36:48 +010050 td_verror(td, errno, "ftruncate");
Jens Axboe53cdc682006-10-18 11:50:58 +020051 goto err;
52 }
53
Jens Axboe40f82982006-10-25 11:21:05 +020054 if (posix_fallocate(f->fd, 0, f->file_size) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +010055 td_verror(td, errno, "posix_fallocate");
Jens Axboe40f82982006-10-25 11:21:05 +020056 goto err;
57 }
58
Jens Axboea00735e2006-11-03 08:58:08 +010059 b = malloc(td->max_bs[DDIR_WRITE]);
60 memset(b, 0, td->max_bs[DDIR_WRITE]);
Jens Axboe53cdc682006-10-18 11:50:58 +020061
62 left = f->file_size;
63 while (left && !td->terminate) {
Jens Axboea00735e2006-11-03 08:58:08 +010064 bs = td->max_bs[DDIR_WRITE];
Jens Axboe53cdc682006-10-18 11:50:58 +020065 if (bs > left)
66 bs = left;
67
68 r = write(f->fd, b, bs);
69
70 if (r == (int) bs) {
71 left -= bs;
72 continue;
73 } else {
74 if (r < 0)
Jens Axboee1161c32007-02-22 19:36:48 +010075 td_verror(td, errno, "write");
Jens Axboe53cdc682006-10-18 11:50:58 +020076 else
Jens Axboee1161c32007-02-22 19:36:48 +010077 td_verror(td, EIO, "write");
Jens Axboe53cdc682006-10-18 11:50:58 +020078
79 break;
80 }
81 }
82
83 if (td->terminate)
84 unlink(f->file_name);
85 else if (td->create_fsync)
86 fsync(f->fd);
87
88 free(b);
89 close(f->fd);
90 f->fd = -1;
91 return 0;
92err:
93 close(f->fd);
94 f->fd = -1;
95 return 1;
96}
97
98static int create_files(struct thread_data *td)
99{
100 struct fio_file *f;
Jens Axboe25205e92006-10-19 20:50:14 +0200101 int i, err, need_create;
Jens Axboe53cdc682006-10-18 11:50:58 +0200102
Jens Axboef6971252006-11-15 10:00:31 +0100103 for_each_file(td, f, i)
104 f->file_size = td->total_file_size / td->nr_files;
105
Jens Axboe53cdc682006-10-18 11:50:58 +0200106 /*
107 * unless specifically asked for overwrite, let normal io extend it
108 */
Jens Axboef6971252006-11-15 10:00:31 +0100109 if (!td->overwrite)
Jens Axboe53cdc682006-10-18 11:50:58 +0200110 return 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200111
Jens Axboef1027062006-10-20 10:14:01 +0200112 need_create = 0;
Jens Axboeeff68612007-01-14 05:56:44 +0100113 if (td->filetype == FIO_TYPE_FILE) {
114 for_each_file(td, f, i) {
115 int file_there = !file_ok(td, f);
116
Jens Axboe413dd452007-02-23 09:26:09 +0100117 if (file_there && td_write(td) && !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
163 if (td->overwrite) {
164 if (fstat(f->fd, &st) == -1) {
Jens Axboee1161c32007-02-22 19:36:48 +0100165 td_verror(td, errno, "fstat");
Jens Axboe53cdc682006-10-18 11:50:58 +0200166 return 1;
167 }
168
169 f->real_file_size = st.st_size;
170
171 if (!f->file_size || f->file_size > f->real_file_size)
172 f->file_size = f->real_file_size;
Jens Axboe745508d2007-01-08 10:25:51 +0100173 } else
174 f->real_file_size = f->file_size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200175
Jens Axboe53cdc682006-10-18 11:50:58 +0200176 return 0;
177}
178
179static int bdev_size(struct thread_data *td, struct fio_file *f)
180{
181 unsigned long long bytes;
182 int r;
183
184 r = blockdev_size(f->fd, &bytes);
185 if (r) {
Jens Axboee1161c32007-02-22 19:36:48 +0100186 td_verror(td, r, "blockdev_size");
Jens Axboe53cdc682006-10-18 11:50:58 +0200187 return 1;
188 }
189
190 f->real_file_size = bytes;
191
192 /*
193 * no extend possibilities, so limit size to device size if too large
194 */
195 if (!f->file_size || f->file_size > f->real_file_size)
196 f->file_size = f->real_file_size;
197
198 f->file_size -= f->file_offset;
199 return 0;
200}
201
202static int get_file_size(struct thread_data *td, struct fio_file *f)
203{
204 int ret = 0;
205
206 if (td->filetype == FIO_TYPE_FILE)
207 ret = file_size(td, f);
208 else if (td->filetype == FIO_TYPE_BD)
209 ret = bdev_size(td, f);
210 else
211 f->real_file_size = -1;
212
213 if (ret)
214 return ret;
215
216 if (f->file_offset > f->real_file_size) {
217 log_err("%s: offset extends end (%Lu > %Lu)\n", td->name, f->file_offset, f->real_file_size);
218 return 1;
219 }
220
Jens Axboe53cdc682006-10-18 11:50:58 +0200221 return 0;
222}
223
Jens Axboee5b401d2006-10-18 16:03:40 +0200224int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
225{
226 int ret = 0;
227
Jens Axboeb5af8292007-03-08 12:43:13 +0100228 if (!td->invalidate_cache)
229 return 0;
230 if (!td->odirect)
231 return 0;
232
Jens Axboee5b401d2006-10-18 16:03:40 +0200233 /*
234 * FIXME: add blockdev flushing too
235 */
Jens Axboeb5af8292007-03-08 12:43:13 +0100236 if (f->mmap)
Jens Axboee5b401d2006-10-18 16:03:40 +0200237 ret = madvise(f->mmap, f->file_size, MADV_DONTNEED);
Jens Axboead2da602007-02-26 12:33:27 +0100238 else if (td->filetype == FIO_TYPE_FILE) {
Jens Axboeb5af8292007-03-08 12:43:13 +0100239 ret = fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_DONTNEED);
Jens Axboead2da602007-02-26 12:33:27 +0100240 } else if (td->filetype == FIO_TYPE_BD) {
Jens Axboeb5af8292007-03-08 12:43:13 +0100241 ret = blockdev_invalidate_cache(f->fd);
Jens Axboead2da602007-02-26 12:33:27 +0100242 } else if (td->filetype == FIO_TYPE_CHAR)
Jens Axboee5b401d2006-10-18 16:03:40 +0200243 ret = 0;
244
245 if (ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100246 td_verror(td, errno, "invalidate_cache");
Jens Axboee5b401d2006-10-18 16:03:40 +0200247 return 1;
248 }
249
Jens Axboead2da602007-02-26 12:33:27 +0100250 return ret;
Jens Axboee5b401d2006-10-18 16:03:40 +0200251}
252
Jens Axboeb5af8292007-03-08 12:43:13 +0100253void generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
Jens Axboe53cdc682006-10-18 11:50:58 +0200254{
Jens Axboeb5af8292007-03-08 12:43:13 +0100255 close(f->fd);
256 f->fd = -1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200257}
258
Jens Axboeb5af8292007-03-08 12:43:13 +0100259int generic_open_file(struct thread_data *td, struct fio_file *f)
Jens Axboe53cdc682006-10-18 11:50:58 +0200260{
Jens Axboe53cdc682006-10-18 11:50:58 +0200261 int flags = 0;
262
Jens Axboe2fd233b2007-03-02 08:44:25 +0100263 if (td->odirect)
264 flags |= OS_O_DIRECT;
265 if (td->sync_io)
266 flags |= O_SYNC;
Jens Axboe7abf8332006-11-23 15:01:19 +0100267
Jens Axboe2fd233b2007-03-02 08:44:25 +0100268 if (td_write(td) || td_rw(td)) {
269 flags |= O_RDWR;
Jens Axboe53cdc682006-10-18 11:50:58 +0200270
Jens Axboe2fd233b2007-03-02 08:44:25 +0100271 if (td->filetype == FIO_TYPE_FILE) {
272 if (!td->overwrite)
273 flags |= O_TRUNC;
Jens Axboe4d9345a2007-02-07 13:14:57 +0100274
Jens Axboe2fd233b2007-03-02 08:44:25 +0100275 flags |= O_CREAT;
Jens Axboe4d9345a2007-02-07 13:14:57 +0100276 }
Jens Axboe2fd233b2007-03-02 08:44:25 +0100277
Jens Axboeb5af8292007-03-08 12:43:13 +0100278 f->fd = open(f->file_name, flags, 0600);
Jens Axboe2fd233b2007-03-02 08:44:25 +0100279 } else {
280 if (td->filetype == FIO_TYPE_CHAR)
281 flags |= O_RDWR;
282 else
283 flags |= O_RDONLY;
284
Jens Axboeb5af8292007-03-08 12:43:13 +0100285 f->fd = open(f->file_name, flags);
Jens Axboe53cdc682006-10-18 11:50:58 +0200286 }
287
288 if (f->fd == -1) {
Jens Axboee1161c32007-02-22 19:36:48 +0100289 int __e = errno;
290
291 td_verror(td, __e, "open");
292 if (__e == EINVAL && td->odirect)
Jens Axboe9d80e112007-02-22 19:37:53 +0100293 log_err("fio: destination does not support O_DIRECT\n");
Jens Axboe53cdc682006-10-18 11:50:58 +0200294 return 1;
295 }
296
Jens Axboeb5af8292007-03-08 12:43:13 +0100297 if (get_file_size(td, f))
298 goto err;
299
300 if (file_invalidate_cache(td, f))
301 goto err;
302
303 if (!td_random(td)) {
304 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
305 td_verror(td, errno, "fadvise");
306 goto err;
307 }
308 } else {
309 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_RANDOM) < 0) {
310 td_verror(td, errno, "fadvise");
311 goto err;
312 }
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100313 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200314
315 return 0;
Jens Axboeb5af8292007-03-08 12:43:13 +0100316err:
317 close(f->fd);
318 return 1;
319}
320
321int reopen_file(struct thread_data *td, struct fio_file *f)
322{
323 f->last_free_lookup = 0;
324 f->last_completed_pos = 0;
325 f->last_pos = 0;
326
327 if (f->file_map)
328 memset(f->file_map, 0, f->num_maps * sizeof(long));
329
330 printf("setting up %s again\n", f->file_name);
331 return td_io_open_file(td, f);
Jens Axboe53cdc682006-10-18 11:50:58 +0200332}
333
Jens Axboe21972cd2006-11-23 12:39:16 +0100334int open_files(struct thread_data *td)
335{
336 struct fio_file *f;
337 int i, err = 0;
338
339 for_each_file(td, f, i) {
Jens Axboeb5af8292007-03-08 12:43:13 +0100340 err = td_io_open_file(td, f);
Jens Axboe21972cd2006-11-23 12:39:16 +0100341 if (err)
342 break;
Jens Axboeb5af8292007-03-08 12:43:13 +0100343
344 if (td->open_files == td->nr_open_files)
345 break;
Jens Axboe21972cd2006-11-23 12:39:16 +0100346 }
347
Jens Axboe7abf8332006-11-23 15:01:19 +0100348 if (!err)
349 return 0;
350
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100351 for_each_file(td, f, i)
Jens Axboeb5af8292007-03-08 12:43:13 +0100352 td_io_close_file(td, f);
Jens Axboe7abf8332006-11-23 15:01:19 +0100353
Jens Axboe21972cd2006-11-23 12:39:16 +0100354 return err;
355}
356
Jens Axboe53cdc682006-10-18 11:50:58 +0200357int setup_files(struct thread_data *td)
358{
359 struct fio_file *f;
Jens Axboe21972cd2006-11-23 12:39:16 +0100360 int err, i;
Jens Axboe53cdc682006-10-18 11:50:58 +0200361
362 /*
363 * if ioengine defines a setup() method, it's responsible for
364 * setting up everything in the td->files[] area.
365 */
366 if (td->io_ops->setup)
367 return td->io_ops->setup(td);
368
369 if (create_files(td))
370 return 1;
371
Jens Axboe21972cd2006-11-23 12:39:16 +0100372 err = open_files(td);
Jens Axboef1027062006-10-20 10:14:01 +0200373 if (err)
374 return err;
375
Jens Axboe0a7eb122006-10-20 10:36:42 +0200376 /*
377 * Recalculate the total file size now that files are set up.
378 */
379 td->total_file_size = 0;
380 for_each_file(td, f, i)
381 td->total_file_size += f->file_size;
382
Jens Axboef1027062006-10-20 10:14:01 +0200383 td->io_size = td->total_file_size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200384 if (td->io_size == 0) {
385 log_err("%s: no io blocks\n", td->name);
Jens Axboee1161c32007-02-22 19:36:48 +0100386 td_verror(td, EINVAL, "total_file_size");
Jens Axboe53cdc682006-10-18 11:50:58 +0200387 return 1;
388 }
389
390 if (!td->zone_size)
391 td->zone_size = td->io_size;
392
393 td->total_io_size = td->io_size * td->loops;
394
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100395 for_each_file(td, f, i)
Jens Axboeb5af8292007-03-08 12:43:13 +0100396 td_io_close_file(td, f);
Jens Axboe21972cd2006-11-23 12:39:16 +0100397
398 return err;
Jens Axboe53cdc682006-10-18 11:50:58 +0200399}
400
401void close_files(struct thread_data *td)
402{
Jens Axboe0ab8db82006-10-18 17:16:23 +0200403 struct fio_file *f;
Jens Axboe53cdc682006-10-18 11:50:58 +0200404 int i;
405
Jens Axboe0ab8db82006-10-18 17:16:23 +0200406 for_each_file(td, f, i) {
Jens Axboe9b031dc2006-12-15 16:01:54 +0100407 if (!td->filename && f->unlink &&
Jens Axboe02bcaa82006-11-24 10:42:00 +0100408 td->filetype == FIO_TYPE_FILE) {
Jens Axboe132ad462006-11-23 12:23:12 +0100409 unlink(f->file_name);
410 free(f->file_name);
411 f->file_name = NULL;
412 }
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100413
Jens Axboeb5af8292007-03-08 12:43:13 +0100414 td_io_close_file(td, f);
Jens Axboe53cdc682006-10-18 11:50:58 +0200415 }
Jens Axboeb4a6a592006-10-20 13:54:47 +0200416
Jens Axboe132ad462006-11-23 12:23:12 +0100417 td->filename = NULL;
Jens Axboeb4a6a592006-10-20 13:54:47 +0200418 free(td->files);
419 td->files = NULL;
420 td->nr_files = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200421}