blob: f6e997438c885e7d70aa07b36c212c8c39aa917e [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 */
Jens Axboef11bd942007-03-13 10:16:34 +0100151 f->flags &= ~FIO_FILE_UNLINK;
Jens Axboe25205e92006-10-19 20:50:14 +0200152 if (file_ok(td, f)) {
Jens Axboef11bd942007-03-13 10:16:34 +0100153 if (td->unlink)
154 f->flags |= FIO_FILE_UNLINK;
155
Jens Axboe25205e92006-10-19 20:50:14 +0200156 err = create_file(td, f);
157 if (err)
158 break;
159 }
160 }
161
Jens Axboe53cdc682006-10-18 11:50:58 +0200162 temp_stall_ts = 0;
163 return err;
164}
165
166static int file_size(struct thread_data *td, struct fio_file *f)
167{
168 struct stat st;
169
170 if (td->overwrite) {
171 if (fstat(f->fd, &st) == -1) {
Jens Axboee1161c32007-02-22 19:36:48 +0100172 td_verror(td, errno, "fstat");
Jens Axboe53cdc682006-10-18 11:50:58 +0200173 return 1;
174 }
175
176 f->real_file_size = st.st_size;
177
178 if (!f->file_size || f->file_size > f->real_file_size)
179 f->file_size = f->real_file_size;
Jens Axboe745508d2007-01-08 10:25:51 +0100180 } else
181 f->real_file_size = f->file_size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200182
Jens Axboe53cdc682006-10-18 11:50:58 +0200183 return 0;
184}
185
186static int bdev_size(struct thread_data *td, struct fio_file *f)
187{
188 unsigned long long bytes;
189 int r;
190
191 r = blockdev_size(f->fd, &bytes);
192 if (r) {
Jens Axboee1161c32007-02-22 19:36:48 +0100193 td_verror(td, r, "blockdev_size");
Jens Axboe53cdc682006-10-18 11:50:58 +0200194 return 1;
195 }
196
197 f->real_file_size = bytes;
198
199 /*
200 * no extend possibilities, so limit size to device size if too large
201 */
202 if (!f->file_size || f->file_size > f->real_file_size)
203 f->file_size = f->real_file_size;
204
205 f->file_size -= f->file_offset;
206 return 0;
207}
208
209static int get_file_size(struct thread_data *td, struct fio_file *f)
210{
211 int ret = 0;
212
Jens Axboeaf52b342007-03-13 10:07:47 +0100213 if (f->filetype == FIO_TYPE_FILE)
Jens Axboe53cdc682006-10-18 11:50:58 +0200214 ret = file_size(td, f);
Jens Axboeaf52b342007-03-13 10:07:47 +0100215 else if (f->filetype == FIO_TYPE_BD)
Jens Axboe53cdc682006-10-18 11:50:58 +0200216 ret = bdev_size(td, f);
217 else
218 f->real_file_size = -1;
219
220 if (ret)
221 return ret;
222
223 if (f->file_offset > f->real_file_size) {
224 log_err("%s: offset extends end (%Lu > %Lu)\n", td->name, f->file_offset, f->real_file_size);
225 return 1;
226 }
227
Jens Axboe53cdc682006-10-18 11:50:58 +0200228 return 0;
229}
230
Jens Axboee5b401d2006-10-18 16:03:40 +0200231int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
232{
233 int ret = 0;
234
Jens Axboeda86ccb2007-03-08 12:49:37 +0100235 if (td->odirect)
Jens Axboeb5af8292007-03-08 12:43:13 +0100236 return 0;
237
Jens Axboee5b401d2006-10-18 16:03:40 +0200238 /*
239 * FIXME: add blockdev flushing too
240 */
Jens Axboeb5af8292007-03-08 12:43:13 +0100241 if (f->mmap)
Jens Axboee5b401d2006-10-18 16:03:40 +0200242 ret = madvise(f->mmap, f->file_size, MADV_DONTNEED);
Jens Axboeaf52b342007-03-13 10:07:47 +0100243 else if (f->filetype == FIO_TYPE_FILE) {
Jens Axboeb5af8292007-03-08 12:43:13 +0100244 ret = fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_DONTNEED);
Jens Axboeaf52b342007-03-13 10:07:47 +0100245 } else if (f->filetype == FIO_TYPE_BD) {
Jens Axboeb5af8292007-03-08 12:43:13 +0100246 ret = blockdev_invalidate_cache(f->fd);
Jens Axboeaf52b342007-03-13 10:07:47 +0100247 } else if (f->filetype == FIO_TYPE_CHAR)
Jens Axboee5b401d2006-10-18 16:03:40 +0200248 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
Jens Axboead2da602007-02-26 12:33:27 +0100255 return ret;
Jens Axboee5b401d2006-10-18 16:03:40 +0200256}
257
Jens Axboeb5af8292007-03-08 12:43:13 +0100258void generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
Jens Axboe53cdc682006-10-18 11:50:58 +0200259{
Jens Axboeb5af8292007-03-08 12:43:13 +0100260 close(f->fd);
261 f->fd = -1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200262}
263
Jens Axboeb5af8292007-03-08 12:43:13 +0100264int generic_open_file(struct thread_data *td, struct fio_file *f)
Jens Axboe53cdc682006-10-18 11:50:58 +0200265{
Jens Axboe53cdc682006-10-18 11:50:58 +0200266 int flags = 0;
267
Jens Axboe2fd233b2007-03-02 08:44:25 +0100268 if (td->odirect)
269 flags |= OS_O_DIRECT;
270 if (td->sync_io)
271 flags |= O_SYNC;
Jens Axboe7abf8332006-11-23 15:01:19 +0100272
Jens Axboe2fd233b2007-03-02 08:44:25 +0100273 if (td_write(td) || td_rw(td)) {
274 flags |= O_RDWR;
Jens Axboe53cdc682006-10-18 11:50:58 +0200275
Jens Axboeaf52b342007-03-13 10:07:47 +0100276 if (f->filetype == FIO_TYPE_FILE)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100277 flags |= O_CREAT;
Jens Axboe2fd233b2007-03-02 08:44:25 +0100278
Jens Axboeb5af8292007-03-08 12:43:13 +0100279 f->fd = open(f->file_name, flags, 0600);
Jens Axboe2fd233b2007-03-02 08:44:25 +0100280 } else {
Jens Axboeaf52b342007-03-13 10:07:47 +0100281 if (f->filetype == FIO_TYPE_CHAR)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100282 flags |= O_RDWR;
283 else
284 flags |= O_RDONLY;
285
Jens Axboeb5af8292007-03-08 12:43:13 +0100286 f->fd = open(f->file_name, flags);
Jens Axboe53cdc682006-10-18 11:50:58 +0200287 }
288
289 if (f->fd == -1) {
Jens Axboee1161c32007-02-22 19:36:48 +0100290 int __e = errno;
291
292 td_verror(td, __e, "open");
293 if (__e == EINVAL && td->odirect)
Jens Axboe9d80e112007-02-22 19:37:53 +0100294 log_err("fio: destination does not support O_DIRECT\n");
Jens Axboe53cdc682006-10-18 11:50:58 +0200295 return 1;
296 }
297
Jens Axboeb5af8292007-03-08 12:43:13 +0100298 if (get_file_size(td, f))
299 goto err;
300
Jens Axboe02638822007-03-12 09:25:55 +0100301 if (td->invalidate_cache && file_invalidate_cache(td, f))
Jens Axboeb5af8292007-03-08 12:43:13 +0100302 goto err;
303
304 if (!td_random(td)) {
305 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
306 td_verror(td, errno, "fadvise");
307 goto err;
308 }
309 } else {
310 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_RANDOM) < 0) {
311 td_verror(td, errno, "fadvise");
312 goto err;
313 }
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100314 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200315
316 return 0;
Jens Axboeb5af8292007-03-08 12:43:13 +0100317err:
318 close(f->fd);
319 return 1;
320}
321
Jens Axboe21972cd2006-11-23 12:39:16 +0100322int open_files(struct thread_data *td)
323{
324 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100325 unsigned int i;
326 int err = 0;
Jens Axboe21972cd2006-11-23 12:39:16 +0100327
328 for_each_file(td, f, i) {
Jens Axboeb5af8292007-03-08 12:43:13 +0100329 err = td_io_open_file(td, f);
Jens Axboe21972cd2006-11-23 12:39:16 +0100330 if (err)
331 break;
Jens Axboeb5af8292007-03-08 12:43:13 +0100332
333 if (td->open_files == td->nr_open_files)
334 break;
Jens Axboe21972cd2006-11-23 12:39:16 +0100335 }
336
Jens Axboe7abf8332006-11-23 15:01:19 +0100337 if (!err)
338 return 0;
339
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100340 for_each_file(td, f, i)
Jens Axboeb5af8292007-03-08 12:43:13 +0100341 td_io_close_file(td, f);
Jens Axboe7abf8332006-11-23 15:01:19 +0100342
Jens Axboe21972cd2006-11-23 12:39:16 +0100343 return err;
344}
345
Jens Axboe53cdc682006-10-18 11:50:58 +0200346int setup_files(struct thread_data *td)
347{
348 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100349 unsigned int i;
350 int err;
Jens Axboe53cdc682006-10-18 11:50:58 +0200351
352 /*
353 * if ioengine defines a setup() method, it's responsible for
354 * setting up everything in the td->files[] area.
355 */
356 if (td->io_ops->setup)
357 return td->io_ops->setup(td);
358
359 if (create_files(td))
360 return 1;
361
Jens Axboe21972cd2006-11-23 12:39:16 +0100362 err = open_files(td);
Jens Axboef1027062006-10-20 10:14:01 +0200363 if (err)
364 return err;
365
Jens Axboe0a7eb122006-10-20 10:36:42 +0200366 /*
367 * Recalculate the total file size now that files are set up.
368 */
369 td->total_file_size = 0;
370 for_each_file(td, f, i)
371 td->total_file_size += f->file_size;
372
Jens Axboe0f14fef2007-03-08 13:08:24 +0100373 td->total_file_size = (td->total_file_size * td->nr_files) / td->open_files;
374
Jens Axboef1027062006-10-20 10:14:01 +0200375 td->io_size = td->total_file_size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200376 if (td->io_size == 0) {
377 log_err("%s: no io blocks\n", td->name);
Jens Axboee1161c32007-02-22 19:36:48 +0100378 td_verror(td, EINVAL, "total_file_size");
Jens Axboe53cdc682006-10-18 11:50:58 +0200379 return 1;
380 }
381
382 if (!td->zone_size)
383 td->zone_size = td->io_size;
384
385 td->total_io_size = td->io_size * td->loops;
386
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100387 for_each_file(td, f, i)
Jens Axboeb5af8292007-03-08 12:43:13 +0100388 td_io_close_file(td, f);
Jens Axboe21972cd2006-11-23 12:39:16 +0100389
390 return err;
Jens Axboe53cdc682006-10-18 11:50:58 +0200391}
392
393void close_files(struct thread_data *td)
394{
Jens Axboe0ab8db82006-10-18 17:16:23 +0200395 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100396 unsigned int i;
Jens Axboe53cdc682006-10-18 11:50:58 +0200397
Jens Axboe0ab8db82006-10-18 17:16:23 +0200398 for_each_file(td, f, i) {
Jens Axboef11bd942007-03-13 10:16:34 +0100399 if (!td->filename && (f->flags & FIO_FILE_UNLINK) &&
Jens Axboeaf52b342007-03-13 10:07:47 +0100400 f->filetype == FIO_TYPE_FILE) {
Jens Axboe132ad462006-11-23 12:23:12 +0100401 unlink(f->file_name);
Jens Axboe132ad462006-11-23 12:23:12 +0100402 f->file_name = NULL;
403 }
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100404
Jens Axboeb5af8292007-03-08 12:43:13 +0100405 td_io_close_file(td, f);
Jens Axboeb3dc7f02007-03-09 12:52:15 +0100406
407 if (f->file_map)
408 free(f->file_map);
Jens Axboe53cdc682006-10-18 11:50:58 +0200409 }
Jens Axboeb4a6a592006-10-20 13:54:47 +0200410
Jens Axboe132ad462006-11-23 12:23:12 +0100411 td->filename = NULL;
Jens Axboeb4a6a592006-10-20 13:54:47 +0200412 free(td->files);
413 td->files = NULL;
414 td->nr_files = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200415}
Jens Axboeaf52b342007-03-13 10:07:47 +0100416
417static void get_file_type(struct thread_data *td, struct fio_file *f)
418{
419 struct stat sb;
420
421 f->filetype = FIO_TYPE_FILE;
422
423 if (!lstat(td->filename, &sb)) {
424 if (S_ISBLK(sb.st_mode))
425 f->filetype = FIO_TYPE_BD;
426 else if (S_ISCHR(sb.st_mode))
427 f->filetype = FIO_TYPE_CHAR;
428 }
429}
430
431void add_file(struct thread_data *td, const char *fname)
432{
433 int cur_files = td->open_files;
434 struct fio_file *f;
435
436 td->files = realloc(td->files, (cur_files + 1) * sizeof(*f));
437
438 f = &td->files[cur_files];
439 memset(f, 0, sizeof(*f));
440 f->fd = -1;
441 f->file_name = fname;
442
443 get_file_type(td, f);
444
445 td->open_files++;
446 td->nr_uniq_files = td->open_files;
447}