blob: c1107a42eb6f098a78a5e48cc25bfaae889df3fd [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 Axboe15494412007-03-13 11:52:47 +0100104 for_each_file(td, f, i) {
105 if (f->filetype != FIO_TYPE_FILE)
106 continue;
107
108 f->file_size = td->total_file_size / td->nr_normal_files;
109 f->file_offset = td->start_offset;
110 }
Jens Axboef6971252006-11-15 10:00:31 +0100111
Jens Axboe53cdc682006-10-18 11:50:58 +0200112 /*
113 * unless specifically asked for overwrite, let normal io extend it
114 */
Jens Axboe02638822007-03-12 09:25:55 +0100115 can_extend = !td->overwrite && !(td->io_ops->flags & FIO_NOEXTEND);
116 if (can_extend)
Jens Axboe53cdc682006-10-18 11:50:58 +0200117 return 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200118
Jens Axboef1027062006-10-20 10:14:01 +0200119 need_create = 0;
Jens Axboeaf52b342007-03-13 10:07:47 +0100120 for_each_file(td, f, i) {
121 int file_there;
Jens Axboeeff68612007-01-14 05:56:44 +0100122
Jens Axboeaf52b342007-03-13 10:07:47 +0100123 if (f->filetype != FIO_TYPE_FILE)
124 continue;
Jens Axboeeff68612007-01-14 05:56:44 +0100125
Jens Axboeaf52b342007-03-13 10:07:47 +0100126 file_there = !file_ok(td, f);
127
128 if (file_there && td_write(td) && !td->overwrite) {
129 unlink(f->file_name);
130 file_there = 0;
Jens Axboeeff68612007-01-14 05:56:44 +0100131 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100132
133 need_create += !file_there;
Jens Axboeeff68612007-01-14 05:56:44 +0100134 }
Jens Axboef1027062006-10-20 10:14:01 +0200135
136 if (!need_create)
137 return 0;
138
Jens Axboe53cdc682006-10-18 11:50:58 +0200139 if (!td->total_file_size) {
140 log_err("Need size for create\n");
Jens Axboee1161c32007-02-22 19:36:48 +0100141 td_verror(td, EINVAL, "file_size");
Jens Axboe53cdc682006-10-18 11:50:58 +0200142 return 1;
143 }
144
Jens Axboe25205e92006-10-19 20:50:14 +0200145 temp_stall_ts = 1;
Jens Axboe1e97cce2006-12-05 11:44:16 +0100146 fprintf(f_out, "%s: Laying out IO file(s) (%u x %LuMiB == %LuMiB)\n",
Jens Axboe15494412007-03-13 11:52:47 +0100147 td->name, td->nr_normal_files,
148 (td->total_file_size >> 20) / td->nr_normal_files,
Jens Axboe25205e92006-10-19 20:50:14 +0200149 td->total_file_size >> 20);
150
151 err = 0;
152 for_each_file(td, f, i) {
Jens Axboe9b031dc2006-12-15 16:01:54 +0100153 /*
154 * Only unlink files that we created.
155 */
Jens Axboef11bd942007-03-13 10:16:34 +0100156 f->flags &= ~FIO_FILE_UNLINK;
Jens Axboe25205e92006-10-19 20:50:14 +0200157 if (file_ok(td, f)) {
Jens Axboef11bd942007-03-13 10:16:34 +0100158 if (td->unlink)
159 f->flags |= FIO_FILE_UNLINK;
160
Jens Axboe25205e92006-10-19 20:50:14 +0200161 err = create_file(td, f);
162 if (err)
163 break;
164 }
165 }
166
Jens Axboe53cdc682006-10-18 11:50:58 +0200167 temp_stall_ts = 0;
168 return err;
169}
170
171static int file_size(struct thread_data *td, struct fio_file *f)
172{
173 struct stat st;
174
175 if (td->overwrite) {
176 if (fstat(f->fd, &st) == -1) {
Jens Axboee1161c32007-02-22 19:36:48 +0100177 td_verror(td, errno, "fstat");
Jens Axboe53cdc682006-10-18 11:50:58 +0200178 return 1;
179 }
180
181 f->real_file_size = st.st_size;
182
183 if (!f->file_size || f->file_size > f->real_file_size)
184 f->file_size = f->real_file_size;
Jens Axboe745508d2007-01-08 10:25:51 +0100185 } else
186 f->real_file_size = f->file_size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200187
Jens Axboe53cdc682006-10-18 11:50:58 +0200188 return 0;
189}
190
191static int bdev_size(struct thread_data *td, struct fio_file *f)
192{
193 unsigned long long bytes;
194 int r;
195
196 r = blockdev_size(f->fd, &bytes);
197 if (r) {
Jens Axboee1161c32007-02-22 19:36:48 +0100198 td_verror(td, r, "blockdev_size");
Jens Axboe53cdc682006-10-18 11:50:58 +0200199 return 1;
200 }
201
202 f->real_file_size = bytes;
203
204 /*
205 * no extend possibilities, so limit size to device size if too large
206 */
207 if (!f->file_size || f->file_size > f->real_file_size)
208 f->file_size = f->real_file_size;
209
210 f->file_size -= f->file_offset;
211 return 0;
212}
213
214static int get_file_size(struct thread_data *td, struct fio_file *f)
215{
216 int ret = 0;
217
Jens Axboeaf52b342007-03-13 10:07:47 +0100218 if (f->filetype == FIO_TYPE_FILE)
Jens Axboe53cdc682006-10-18 11:50:58 +0200219 ret = file_size(td, f);
Jens Axboeaf52b342007-03-13 10:07:47 +0100220 else if (f->filetype == FIO_TYPE_BD)
Jens Axboe53cdc682006-10-18 11:50:58 +0200221 ret = bdev_size(td, f);
222 else
223 f->real_file_size = -1;
224
225 if (ret)
226 return ret;
227
228 if (f->file_offset > f->real_file_size) {
229 log_err("%s: offset extends end (%Lu > %Lu)\n", td->name, f->file_offset, f->real_file_size);
230 return 1;
231 }
232
Jens Axboe53cdc682006-10-18 11:50:58 +0200233 return 0;
234}
235
Jens Axboee5b401d2006-10-18 16:03:40 +0200236int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
237{
238 int ret = 0;
239
Jens Axboeda86ccb2007-03-08 12:49:37 +0100240 if (td->odirect)
Jens Axboeb5af8292007-03-08 12:43:13 +0100241 return 0;
242
Jens Axboee5b401d2006-10-18 16:03:40 +0200243 /*
244 * FIXME: add blockdev flushing too
245 */
Jens Axboeb5af8292007-03-08 12:43:13 +0100246 if (f->mmap)
Jens Axboee5b401d2006-10-18 16:03:40 +0200247 ret = madvise(f->mmap, f->file_size, MADV_DONTNEED);
Jens Axboeaf52b342007-03-13 10:07:47 +0100248 else if (f->filetype == FIO_TYPE_FILE) {
Jens Axboeb5af8292007-03-08 12:43:13 +0100249 ret = fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_DONTNEED);
Jens Axboeaf52b342007-03-13 10:07:47 +0100250 } else if (f->filetype == FIO_TYPE_BD) {
Jens Axboeb5af8292007-03-08 12:43:13 +0100251 ret = blockdev_invalidate_cache(f->fd);
Jens Axboeaf52b342007-03-13 10:07:47 +0100252 } else if (f->filetype == FIO_TYPE_CHAR)
Jens Axboee5b401d2006-10-18 16:03:40 +0200253 ret = 0;
254
255 if (ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100256 td_verror(td, errno, "invalidate_cache");
Jens Axboee5b401d2006-10-18 16:03:40 +0200257 return 1;
258 }
259
Jens Axboead2da602007-02-26 12:33:27 +0100260 return ret;
Jens Axboee5b401d2006-10-18 16:03:40 +0200261}
262
Jens Axboeb5af8292007-03-08 12:43:13 +0100263void generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
Jens Axboe53cdc682006-10-18 11:50:58 +0200264{
Jens Axboeb5af8292007-03-08 12:43:13 +0100265 close(f->fd);
266 f->fd = -1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200267}
268
Jens Axboeb5af8292007-03-08 12:43:13 +0100269int generic_open_file(struct thread_data *td, struct fio_file *f)
Jens Axboe53cdc682006-10-18 11:50:58 +0200270{
Jens Axboe53cdc682006-10-18 11:50:58 +0200271 int flags = 0;
272
Jens Axboe2fd233b2007-03-02 08:44:25 +0100273 if (td->odirect)
274 flags |= OS_O_DIRECT;
275 if (td->sync_io)
276 flags |= O_SYNC;
Jens Axboe7abf8332006-11-23 15:01:19 +0100277
Jens Axboe2fd233b2007-03-02 08:44:25 +0100278 if (td_write(td) || td_rw(td)) {
279 flags |= O_RDWR;
Jens Axboe53cdc682006-10-18 11:50:58 +0200280
Jens Axboeaf52b342007-03-13 10:07:47 +0100281 if (f->filetype == FIO_TYPE_FILE)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100282 flags |= O_CREAT;
Jens Axboe2fd233b2007-03-02 08:44:25 +0100283
Jens Axboeb5af8292007-03-08 12:43:13 +0100284 f->fd = open(f->file_name, flags, 0600);
Jens Axboe2fd233b2007-03-02 08:44:25 +0100285 } else {
Jens Axboeaf52b342007-03-13 10:07:47 +0100286 if (f->filetype == FIO_TYPE_CHAR)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100287 flags |= O_RDWR;
288 else
289 flags |= O_RDONLY;
290
Jens Axboeb5af8292007-03-08 12:43:13 +0100291 f->fd = open(f->file_name, flags);
Jens Axboe53cdc682006-10-18 11:50:58 +0200292 }
293
294 if (f->fd == -1) {
Jens Axboee1161c32007-02-22 19:36:48 +0100295 int __e = errno;
296
297 td_verror(td, __e, "open");
298 if (__e == EINVAL && td->odirect)
Jens Axboe9d80e112007-02-22 19:37:53 +0100299 log_err("fio: destination does not support O_DIRECT\n");
Jens Axboe53cdc682006-10-18 11:50:58 +0200300 return 1;
301 }
302
Jens Axboeb5af8292007-03-08 12:43:13 +0100303 if (get_file_size(td, f))
304 goto err;
305
Jens Axboe02638822007-03-12 09:25:55 +0100306 if (td->invalidate_cache && file_invalidate_cache(td, f))
Jens Axboeb5af8292007-03-08 12:43:13 +0100307 goto err;
308
309 if (!td_random(td)) {
310 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
311 td_verror(td, errno, "fadvise");
312 goto err;
313 }
314 } else {
315 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_RANDOM) < 0) {
316 td_verror(td, errno, "fadvise");
317 goto err;
318 }
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100319 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200320
321 return 0;
Jens Axboeb5af8292007-03-08 12:43:13 +0100322err:
323 close(f->fd);
324 return 1;
325}
326
Jens Axboe21972cd2006-11-23 12:39:16 +0100327int open_files(struct thread_data *td)
328{
329 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100330 unsigned int i;
331 int err = 0;
Jens Axboe21972cd2006-11-23 12:39:16 +0100332
333 for_each_file(td, f, i) {
Jens Axboeb5af8292007-03-08 12:43:13 +0100334 err = td_io_open_file(td, f);
Jens Axboe21972cd2006-11-23 12:39:16 +0100335 if (err)
336 break;
Jens Axboeb5af8292007-03-08 12:43:13 +0100337
338 if (td->open_files == td->nr_open_files)
339 break;
Jens Axboe21972cd2006-11-23 12:39:16 +0100340 }
341
Jens Axboe7abf8332006-11-23 15:01:19 +0100342 if (!err)
343 return 0;
344
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100345 for_each_file(td, f, i)
Jens Axboeb5af8292007-03-08 12:43:13 +0100346 td_io_close_file(td, f);
Jens Axboe7abf8332006-11-23 15:01:19 +0100347
Jens Axboe21972cd2006-11-23 12:39:16 +0100348 return err;
349}
350
Jens Axboe53cdc682006-10-18 11:50:58 +0200351int setup_files(struct thread_data *td)
352{
353 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100354 unsigned int i;
355 int err;
Jens Axboe53cdc682006-10-18 11:50:58 +0200356
357 /*
358 * if ioengine defines a setup() method, it's responsible for
359 * setting up everything in the td->files[] area.
360 */
361 if (td->io_ops->setup)
362 return td->io_ops->setup(td);
363
364 if (create_files(td))
365 return 1;
366
Jens Axboe21972cd2006-11-23 12:39:16 +0100367 err = open_files(td);
Jens Axboef1027062006-10-20 10:14:01 +0200368 if (err)
369 return err;
370
Jens Axboe0a7eb122006-10-20 10:36:42 +0200371 /*
372 * Recalculate the total file size now that files are set up.
373 */
374 td->total_file_size = 0;
375 for_each_file(td, f, i)
376 td->total_file_size += f->file_size;
377
Jens Axboe0f14fef2007-03-08 13:08:24 +0100378 td->total_file_size = (td->total_file_size * td->nr_files) / td->open_files;
379
Jens Axboef1027062006-10-20 10:14:01 +0200380 td->io_size = td->total_file_size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200381 if (td->io_size == 0) {
382 log_err("%s: no io blocks\n", td->name);
Jens Axboee1161c32007-02-22 19:36:48 +0100383 td_verror(td, EINVAL, "total_file_size");
Jens Axboe53cdc682006-10-18 11:50:58 +0200384 return 1;
385 }
386
387 if (!td->zone_size)
388 td->zone_size = td->io_size;
389
390 td->total_io_size = td->io_size * td->loops;
391
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100392 for_each_file(td, f, i)
Jens Axboeb5af8292007-03-08 12:43:13 +0100393 td_io_close_file(td, f);
Jens Axboe21972cd2006-11-23 12:39:16 +0100394
395 return err;
Jens Axboe53cdc682006-10-18 11:50:58 +0200396}
397
398void close_files(struct thread_data *td)
399{
Jens Axboe0ab8db82006-10-18 17:16:23 +0200400 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100401 unsigned int i;
Jens Axboe53cdc682006-10-18 11:50:58 +0200402
Jens Axboe0ab8db82006-10-18 17:16:23 +0200403 for_each_file(td, f, i) {
Jens Axboe1315a682007-03-13 11:25:07 +0100404 if (!f->file_name && (f->flags & FIO_FILE_UNLINK) &&
Jens Axboeaf52b342007-03-13 10:07:47 +0100405 f->filetype == FIO_TYPE_FILE) {
Jens Axboe132ad462006-11-23 12:23:12 +0100406 unlink(f->file_name);
Jens Axboe1315a682007-03-13 11:25:07 +0100407 free(f->file_name);
Jens Axboe132ad462006-11-23 12:23:12 +0100408 f->file_name = NULL;
409 }
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100410
Jens Axboeb5af8292007-03-08 12:43:13 +0100411 td_io_close_file(td, f);
Jens Axboeb3dc7f02007-03-09 12:52:15 +0100412
413 if (f->file_map)
414 free(f->file_map);
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}
Jens Axboeaf52b342007-03-13 10:07:47 +0100422
Jens Axboee3bab462007-03-13 11:22:09 +0100423static void get_file_type(struct fio_file *f)
Jens Axboeaf52b342007-03-13 10:07:47 +0100424{
425 struct stat sb;
426
427 f->filetype = FIO_TYPE_FILE;
428
Jens Axboee3bab462007-03-13 11:22:09 +0100429 if (!lstat(f->file_name, &sb)) {
Jens Axboeaf52b342007-03-13 10:07:47 +0100430 if (S_ISBLK(sb.st_mode))
431 f->filetype = FIO_TYPE_BD;
432 else if (S_ISCHR(sb.st_mode))
433 f->filetype = FIO_TYPE_CHAR;
434 }
435}
436
437void add_file(struct thread_data *td, const char *fname)
438{
Jens Axboe7b4e4fe2007-03-13 12:51:40 +0100439 int cur_files = td->files_index;
Jens Axboeaf52b342007-03-13 10:07:47 +0100440 struct fio_file *f;
441
442 td->files = realloc(td->files, (cur_files + 1) * sizeof(*f));
443
444 f = &td->files[cur_files];
445 memset(f, 0, sizeof(*f));
446 f->fd = -1;
Jens Axboecae61952007-03-13 11:12:14 +0100447 f->file_name = strdup(fname);
Jens Axboeaf52b342007-03-13 10:07:47 +0100448
Jens Axboee3bab462007-03-13 11:22:09 +0100449 get_file_type(f);
Jens Axboeaf52b342007-03-13 10:07:47 +0100450
Jens Axboe7b4e4fe2007-03-13 12:51:40 +0100451 td->files_index++;
Jens Axboe15494412007-03-13 11:52:47 +0100452 if (f->filetype == FIO_TYPE_FILE)
453 td->nr_normal_files++;
Jens Axboeaf52b342007-03-13 10:07:47 +0100454}
Jens Axboe0ad920e2007-03-13 11:06:45 +0100455
456void get_file(struct fio_file *f)
457{
458 f->references++;
459}
460
461void put_file(struct thread_data *td, struct fio_file *f)
462{
463 if (!(f->flags & FIO_FILE_OPEN))
464 return;
465
466 assert(f->references);
467 if (--f->references)
468 return;
469
470 if (td->io_ops->close_file)
471 td->io_ops->close_file(td, f);
472 td->nr_open_files--;
473 f->flags &= ~FIO_FILE_OPEN;
474}