blob: 948cecda7fb56e70de64c0c00ef9723e7564be74 [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 Axboeaf52b342007-03-13 10:07:47 +010018 if (f->filetype != FIO_TYPE_FILE ||
Jens Axboeb5af8292007-03-08 12:43:13 +010019 (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 Axboeaf52b342007-03-13 10:07:47 +0100101 int err, need_create, can_extend;
102 unsigned int i;
Jens Axboe53cdc682006-10-18 11:50:58 +0200103
Jens Axboef6971252006-11-15 10:00:31 +0100104 for_each_file(td, f, i)
105 f->file_size = td->total_file_size / td->nr_files;
106
Jens Axboe53cdc682006-10-18 11:50:58 +0200107 /*
108 * unless specifically asked for overwrite, let normal io extend it
109 */
Jens Axboe02638822007-03-12 09:25:55 +0100110 can_extend = !td->overwrite && !(td->io_ops->flags & FIO_NOEXTEND);
111 if (can_extend)
Jens Axboe53cdc682006-10-18 11:50:58 +0200112 return 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200113
Jens Axboef1027062006-10-20 10:14:01 +0200114 need_create = 0;
Jens Axboeaf52b342007-03-13 10:07:47 +0100115 for_each_file(td, f, i) {
116 int file_there;
Jens Axboeeff68612007-01-14 05:56:44 +0100117
Jens Axboeaf52b342007-03-13 10:07:47 +0100118 if (f->filetype != FIO_TYPE_FILE)
119 continue;
Jens Axboeeff68612007-01-14 05:56:44 +0100120
Jens Axboeaf52b342007-03-13 10:07:47 +0100121 file_there = !file_ok(td, f);
122
123 if (file_there && td_write(td) && !td->overwrite) {
124 unlink(f->file_name);
125 file_there = 0;
Jens Axboeeff68612007-01-14 05:56:44 +0100126 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100127
128 need_create += !file_there;
Jens Axboeeff68612007-01-14 05:56:44 +0100129 }
Jens Axboef1027062006-10-20 10:14:01 +0200130
131 if (!need_create)
132 return 0;
133
Jens Axboe53cdc682006-10-18 11:50:58 +0200134 if (!td->total_file_size) {
135 log_err("Need size for create\n");
Jens Axboee1161c32007-02-22 19:36:48 +0100136 td_verror(td, EINVAL, "file_size");
Jens Axboe53cdc682006-10-18 11:50:58 +0200137 return 1;
138 }
139
Jens Axboe25205e92006-10-19 20:50:14 +0200140 temp_stall_ts = 1;
Jens Axboe1e97cce2006-12-05 11:44:16 +0100141 fprintf(f_out, "%s: Laying out IO file(s) (%u x %LuMiB == %LuMiB)\n",
Jens Axboe13f8e2d2006-10-24 09:29:45 +0200142 td->name, td->nr_uniq_files,
143 (td->total_file_size >> 20) / td->nr_uniq_files,
Jens Axboe25205e92006-10-19 20:50:14 +0200144 td->total_file_size >> 20);
145
146 err = 0;
147 for_each_file(td, f, i) {
Jens Axboe9b031dc2006-12-15 16:01:54 +0100148 /*
149 * Only unlink files that we created.
150 */
151 f->unlink = 0;
Jens Axboe25205e92006-10-19 20:50:14 +0200152 if (file_ok(td, f)) {
Jens Axboe9b031dc2006-12-15 16:01:54 +0100153 f->unlink = td->unlink;
Jens Axboe25205e92006-10-19 20:50:14 +0200154 err = create_file(td, f);
155 if (err)
156 break;
157 }
158 }
159
Jens Axboe53cdc682006-10-18 11:50:58 +0200160 temp_stall_ts = 0;
161 return err;
162}
163
164static int file_size(struct thread_data *td, struct fio_file *f)
165{
166 struct stat st;
167
168 if (td->overwrite) {
169 if (fstat(f->fd, &st) == -1) {
Jens Axboee1161c32007-02-22 19:36:48 +0100170 td_verror(td, errno, "fstat");
Jens Axboe53cdc682006-10-18 11:50:58 +0200171 return 1;
172 }
173
174 f->real_file_size = st.st_size;
175
176 if (!f->file_size || f->file_size > f->real_file_size)
177 f->file_size = f->real_file_size;
Jens Axboe745508d2007-01-08 10:25:51 +0100178 } else
179 f->real_file_size = f->file_size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200180
Jens Axboe53cdc682006-10-18 11:50:58 +0200181 return 0;
182}
183
184static int bdev_size(struct thread_data *td, struct fio_file *f)
185{
186 unsigned long long bytes;
187 int r;
188
189 r = blockdev_size(f->fd, &bytes);
190 if (r) {
Jens Axboee1161c32007-02-22 19:36:48 +0100191 td_verror(td, r, "blockdev_size");
Jens Axboe53cdc682006-10-18 11:50:58 +0200192 return 1;
193 }
194
195 f->real_file_size = bytes;
196
197 /*
198 * no extend possibilities, so limit size to device size if too large
199 */
200 if (!f->file_size || f->file_size > f->real_file_size)
201 f->file_size = f->real_file_size;
202
203 f->file_size -= f->file_offset;
204 return 0;
205}
206
207static int get_file_size(struct thread_data *td, struct fio_file *f)
208{
209 int ret = 0;
210
Jens Axboeaf52b342007-03-13 10:07:47 +0100211 if (f->filetype == FIO_TYPE_FILE)
Jens Axboe53cdc682006-10-18 11:50:58 +0200212 ret = file_size(td, f);
Jens Axboeaf52b342007-03-13 10:07:47 +0100213 else if (f->filetype == FIO_TYPE_BD)
Jens Axboe53cdc682006-10-18 11:50:58 +0200214 ret = bdev_size(td, f);
215 else
216 f->real_file_size = -1;
217
218 if (ret)
219 return ret;
220
221 if (f->file_offset > f->real_file_size) {
222 log_err("%s: offset extends end (%Lu > %Lu)\n", td->name, f->file_offset, f->real_file_size);
223 return 1;
224 }
225
Jens Axboe53cdc682006-10-18 11:50:58 +0200226 return 0;
227}
228
Jens Axboee5b401d2006-10-18 16:03:40 +0200229int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
230{
231 int ret = 0;
232
Jens Axboeda86ccb2007-03-08 12:49:37 +0100233 if (td->odirect)
Jens Axboeb5af8292007-03-08 12:43:13 +0100234 return 0;
235
Jens Axboee5b401d2006-10-18 16:03:40 +0200236 /*
237 * FIXME: add blockdev flushing too
238 */
Jens Axboeb5af8292007-03-08 12:43:13 +0100239 if (f->mmap)
Jens Axboee5b401d2006-10-18 16:03:40 +0200240 ret = madvise(f->mmap, f->file_size, MADV_DONTNEED);
Jens Axboeaf52b342007-03-13 10:07:47 +0100241 else if (f->filetype == FIO_TYPE_FILE) {
Jens Axboeb5af8292007-03-08 12:43:13 +0100242 ret = fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_DONTNEED);
Jens Axboeaf52b342007-03-13 10:07:47 +0100243 } else if (f->filetype == FIO_TYPE_BD) {
Jens Axboeb5af8292007-03-08 12:43:13 +0100244 ret = blockdev_invalidate_cache(f->fd);
Jens Axboeaf52b342007-03-13 10:07:47 +0100245 } else if (f->filetype == FIO_TYPE_CHAR)
Jens Axboee5b401d2006-10-18 16:03:40 +0200246 ret = 0;
247
248 if (ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100249 td_verror(td, errno, "invalidate_cache");
Jens Axboee5b401d2006-10-18 16:03:40 +0200250 return 1;
251 }
252
Jens Axboead2da602007-02-26 12:33:27 +0100253 return ret;
Jens Axboee5b401d2006-10-18 16:03:40 +0200254}
255
Jens Axboeb5af8292007-03-08 12:43:13 +0100256void generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
Jens Axboe53cdc682006-10-18 11:50:58 +0200257{
Jens Axboeb5af8292007-03-08 12:43:13 +0100258 close(f->fd);
259 f->fd = -1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200260}
261
Jens Axboeb5af8292007-03-08 12:43:13 +0100262int generic_open_file(struct thread_data *td, struct fio_file *f)
Jens Axboe53cdc682006-10-18 11:50:58 +0200263{
Jens Axboe53cdc682006-10-18 11:50:58 +0200264 int flags = 0;
265
Jens Axboe2fd233b2007-03-02 08:44:25 +0100266 if (td->odirect)
267 flags |= OS_O_DIRECT;
268 if (td->sync_io)
269 flags |= O_SYNC;
Jens Axboe7abf8332006-11-23 15:01:19 +0100270
Jens Axboe2fd233b2007-03-02 08:44:25 +0100271 if (td_write(td) || td_rw(td)) {
272 flags |= O_RDWR;
Jens Axboe53cdc682006-10-18 11:50:58 +0200273
Jens Axboeaf52b342007-03-13 10:07:47 +0100274 if (f->filetype == FIO_TYPE_FILE)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100275 flags |= O_CREAT;
Jens Axboe2fd233b2007-03-02 08:44:25 +0100276
Jens Axboeb5af8292007-03-08 12:43:13 +0100277 f->fd = open(f->file_name, flags, 0600);
Jens Axboe2fd233b2007-03-02 08:44:25 +0100278 } else {
Jens Axboeaf52b342007-03-13 10:07:47 +0100279 if (f->filetype == FIO_TYPE_CHAR)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100280 flags |= O_RDWR;
281 else
282 flags |= O_RDONLY;
283
Jens Axboeb5af8292007-03-08 12:43:13 +0100284 f->fd = open(f->file_name, flags);
Jens Axboe53cdc682006-10-18 11:50:58 +0200285 }
286
287 if (f->fd == -1) {
Jens Axboee1161c32007-02-22 19:36:48 +0100288 int __e = errno;
289
290 td_verror(td, __e, "open");
291 if (__e == EINVAL && td->odirect)
Jens Axboe9d80e112007-02-22 19:37:53 +0100292 log_err("fio: destination does not support O_DIRECT\n");
Jens Axboe53cdc682006-10-18 11:50:58 +0200293 return 1;
294 }
295
Jens Axboeb5af8292007-03-08 12:43:13 +0100296 if (get_file_size(td, f))
297 goto err;
298
Jens Axboe02638822007-03-12 09:25:55 +0100299 if (td->invalidate_cache && file_invalidate_cache(td, f))
Jens Axboeb5af8292007-03-08 12:43:13 +0100300 goto err;
301
302 if (!td_random(td)) {
303 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
304 td_verror(td, errno, "fadvise");
305 goto err;
306 }
307 } else {
308 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_RANDOM) < 0) {
309 td_verror(td, errno, "fadvise");
310 goto err;
311 }
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100312 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200313
314 return 0;
Jens Axboeb5af8292007-03-08 12:43:13 +0100315err:
316 close(f->fd);
317 return 1;
318}
319
Jens Axboe21972cd2006-11-23 12:39:16 +0100320int open_files(struct thread_data *td)
321{
322 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100323 unsigned int i;
324 int err = 0;
Jens Axboe21972cd2006-11-23 12:39:16 +0100325
326 for_each_file(td, f, i) {
Jens Axboeb5af8292007-03-08 12:43:13 +0100327 err = td_io_open_file(td, f);
Jens Axboe21972cd2006-11-23 12:39:16 +0100328 if (err)
329 break;
Jens Axboeb5af8292007-03-08 12:43:13 +0100330
331 if (td->open_files == td->nr_open_files)
332 break;
Jens Axboe21972cd2006-11-23 12:39:16 +0100333 }
334
Jens Axboe7abf8332006-11-23 15:01:19 +0100335 if (!err)
336 return 0;
337
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100338 for_each_file(td, f, i)
Jens Axboeb5af8292007-03-08 12:43:13 +0100339 td_io_close_file(td, f);
Jens Axboe7abf8332006-11-23 15:01:19 +0100340
Jens Axboe21972cd2006-11-23 12:39:16 +0100341 return err;
342}
343
Jens Axboe53cdc682006-10-18 11:50:58 +0200344int setup_files(struct thread_data *td)
345{
346 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100347 unsigned int i;
348 int err;
Jens Axboe53cdc682006-10-18 11:50:58 +0200349
350 /*
351 * if ioengine defines a setup() method, it's responsible for
352 * setting up everything in the td->files[] area.
353 */
354 if (td->io_ops->setup)
355 return td->io_ops->setup(td);
356
357 if (create_files(td))
358 return 1;
359
Jens Axboe21972cd2006-11-23 12:39:16 +0100360 err = open_files(td);
Jens Axboef1027062006-10-20 10:14:01 +0200361 if (err)
362 return err;
363
Jens Axboe0a7eb122006-10-20 10:36:42 +0200364 /*
365 * Recalculate the total file size now that files are set up.
366 */
367 td->total_file_size = 0;
368 for_each_file(td, f, i)
369 td->total_file_size += f->file_size;
370
Jens Axboe0f14fef2007-03-08 13:08:24 +0100371 td->total_file_size = (td->total_file_size * td->nr_files) / td->open_files;
372
Jens Axboef1027062006-10-20 10:14:01 +0200373 td->io_size = td->total_file_size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200374 if (td->io_size == 0) {
375 log_err("%s: no io blocks\n", td->name);
Jens Axboee1161c32007-02-22 19:36:48 +0100376 td_verror(td, EINVAL, "total_file_size");
Jens Axboe53cdc682006-10-18 11:50:58 +0200377 return 1;
378 }
379
380 if (!td->zone_size)
381 td->zone_size = td->io_size;
382
383 td->total_io_size = td->io_size * td->loops;
384
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100385 for_each_file(td, f, i)
Jens Axboeb5af8292007-03-08 12:43:13 +0100386 td_io_close_file(td, f);
Jens Axboe21972cd2006-11-23 12:39:16 +0100387
388 return err;
Jens Axboe53cdc682006-10-18 11:50:58 +0200389}
390
391void close_files(struct thread_data *td)
392{
Jens Axboe0ab8db82006-10-18 17:16:23 +0200393 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100394 unsigned int i;
Jens Axboe53cdc682006-10-18 11:50:58 +0200395
Jens Axboe0ab8db82006-10-18 17:16:23 +0200396 for_each_file(td, f, i) {
Jens Axboe9b031dc2006-12-15 16:01:54 +0100397 if (!td->filename && f->unlink &&
Jens Axboeaf52b342007-03-13 10:07:47 +0100398 f->filetype == FIO_TYPE_FILE) {
Jens Axboe132ad462006-11-23 12:23:12 +0100399 unlink(f->file_name);
Jens Axboe132ad462006-11-23 12:23:12 +0100400 f->file_name = NULL;
401 }
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100402
Jens Axboeb5af8292007-03-08 12:43:13 +0100403 td_io_close_file(td, f);
Jens Axboeb3dc7f02007-03-09 12:52:15 +0100404
405 if (f->file_map)
406 free(f->file_map);
Jens Axboe53cdc682006-10-18 11:50:58 +0200407 }
Jens Axboeb4a6a592006-10-20 13:54:47 +0200408
Jens Axboe132ad462006-11-23 12:23:12 +0100409 td->filename = NULL;
Jens Axboeb4a6a592006-10-20 13:54:47 +0200410 free(td->files);
411 td->files = NULL;
412 td->nr_files = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200413}
Jens Axboeaf52b342007-03-13 10:07:47 +0100414
415static void get_file_type(struct thread_data *td, struct fio_file *f)
416{
417 struct stat sb;
418
419 f->filetype = FIO_TYPE_FILE;
420
421 if (!lstat(td->filename, &sb)) {
422 if (S_ISBLK(sb.st_mode))
423 f->filetype = FIO_TYPE_BD;
424 else if (S_ISCHR(sb.st_mode))
425 f->filetype = FIO_TYPE_CHAR;
426 }
427}
428
429void add_file(struct thread_data *td, const char *fname)
430{
431 int cur_files = td->open_files;
432 struct fio_file *f;
433
434 td->files = realloc(td->files, (cur_files + 1) * sizeof(*f));
435
436 f = &td->files[cur_files];
437 memset(f, 0, sizeof(*f));
438 f->fd = -1;
439 f->file_name = fname;
440
441 get_file_type(td, f);
442
443 td->open_files++;
444 td->nr_uniq_files = td->open_files;
445}