blob: b37de5b726b3203d27eed4c99281948d67a67ed3 [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>
Jens Axboebbf6b542007-03-13 15:28:55 +01005#include <dirent.h>
Jens Axboe53cdc682006-10-18 11:50:58 +02006#include <sys/stat.h>
7#include <sys/mman.h>
Jens Axboebbf6b542007-03-13 15:28:55 +01008#include <sys/types.h>
Jens Axboe53cdc682006-10-18 11:50:58 +02009
10#include "fio.h"
Jens Axboe53cdc682006-10-18 11:50:58 +020011
Jens Axboe7172cfe2007-07-23 14:36:00 +020012static int root_warn;
13
Jens Axboe7bb48f82007-03-27 15:30:28 +020014static int extend_file(struct thread_data *td, struct fio_file *f)
Jens Axboe25205e92006-10-19 20:50:14 +020015{
Jens Axboeea443652007-03-29 14:52:13 +020016 int r, new_layout = 0, unlink_file = 0, flags;
Jens Axboe25205e92006-10-19 20:50:14 +020017 unsigned long long left;
18 unsigned int bs;
19 char *b;
Jens Axboeb2a15192006-10-18 14:10:42 +020020
Jens Axboe4241ea82007-09-12 08:18:36 +020021 if (read_only) {
22 log_err("fio: refusing extend of file due to read-only\n");
23 return 0;
24 }
25
Jens Axboe507a7022007-03-27 15:52:46 +020026 /*
27 * check if we need to lay the file out complete again. fio
28 * does that for operations involving reads, or for writes
29 * where overwrite is set
30 */
31 if (td_read(td) || (td_write(td) && td->o.overwrite))
32 new_layout = 1;
Jens Axboeea443652007-03-29 14:52:13 +020033 if (td_write(td) && !td->o.overwrite)
34 unlink_file = 1;
Jens Axboe507a7022007-03-27 15:52:46 +020035
Jens Axboe6ae1f572008-02-21 10:20:00 +010036 if (unlink_file || new_layout) {
Zhang, Yanmin982016d2008-02-26 15:35:52 +010037 if ((unlink(f->file_name) < 0) && (errno != ENOENT)) {
Jens Axboe7bb48f82007-03-27 15:30:28 +020038 td_verror(td, errno, "unlink");
39 return 1;
40 }
41 }
42
Jens Axboe507a7022007-03-27 15:52:46 +020043 flags = O_WRONLY | O_CREAT;
44 if (new_layout)
45 flags |= O_TRUNC;
46
Jens Axboeee56ad52008-02-01 10:30:20 +010047 dprint(FD_FILE, "open file %s, flags %x\n", f->file_name, flags);
Jens Axboe507a7022007-03-27 15:52:46 +020048 f->fd = open(f->file_name, flags, 0644);
Jens Axboe53cdc682006-10-18 11:50:58 +020049 if (f->fd < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +010050 td_verror(td, errno, "open");
Jens Axboe53cdc682006-10-18 11:50:58 +020051 return 1;
52 }
53
Shawn Lewisfc74ac12008-01-11 09:45:11 +010054 if (!new_layout)
55 goto done;
56
Jens Axboeee56ad52008-02-01 10:30:20 +010057 dprint(FD_FILE, "truncate file %s, size %llu\n", f->file_name,
58 f->real_file_size);
Jens Axboe7bb48f82007-03-27 15:30:28 +020059 if (ftruncate(f->fd, f->real_file_size) == -1) {
Jens Axboee1161c32007-02-22 19:36:48 +010060 td_verror(td, errno, "ftruncate");
Jens Axboe53cdc682006-10-18 11:50:58 +020061 goto err;
62 }
63
Jens Axboeee56ad52008-02-01 10:30:20 +010064 dprint(FD_FILE, "fallocate file %s, size %llu\n", f->file_name,
65 f->real_file_size);
Jens Axboe7bb48f82007-03-27 15:30:28 +020066 if (posix_fallocate(f->fd, 0, f->real_file_size) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +010067 td_verror(td, errno, "posix_fallocate");
Jens Axboe40f82982006-10-25 11:21:05 +020068 goto err;
69 }
70
Jens Axboe2dc1bbe2007-03-15 15:01:33 +010071 b = malloc(td->o.max_bs[DDIR_WRITE]);
72 memset(b, 0, td->o.max_bs[DDIR_WRITE]);
Jens Axboe53cdc682006-10-18 11:50:58 +020073
Jens Axboe7bb48f82007-03-27 15:30:28 +020074 left = f->real_file_size;
Jens Axboe53cdc682006-10-18 11:50:58 +020075 while (left && !td->terminate) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +010076 bs = td->o.max_bs[DDIR_WRITE];
Jens Axboe53cdc682006-10-18 11:50:58 +020077 if (bs > left)
78 bs = left;
79
80 r = write(f->fd, b, bs);
81
82 if (r == (int) bs) {
83 left -= bs;
84 continue;
85 } else {
86 if (r < 0)
Jens Axboee1161c32007-02-22 19:36:48 +010087 td_verror(td, errno, "write");
Jens Axboe53cdc682006-10-18 11:50:58 +020088 else
Jens Axboee1161c32007-02-22 19:36:48 +010089 td_verror(td, EIO, "write");
Jens Axboe53cdc682006-10-18 11:50:58 +020090
91 break;
92 }
93 }
94
95 if (td->terminate)
96 unlink(f->file_name);
Jens Axboe2dc1bbe2007-03-15 15:01:33 +010097 else if (td->o.create_fsync)
Jens Axboe53cdc682006-10-18 11:50:58 +020098 fsync(f->fd);
99
100 free(b);
Jens Axboe507a7022007-03-27 15:52:46 +0200101done:
Jens Axboe53cdc682006-10-18 11:50:58 +0200102 close(f->fd);
103 f->fd = -1;
104 return 0;
105err:
106 close(f->fd);
107 f->fd = -1;
108 return 1;
109}
110
Jens Axboe7bb48f82007-03-27 15:30:28 +0200111static unsigned long long get_rand_file_size(struct thread_data *td)
Jens Axboe9c60ce62007-03-15 09:14:47 +0100112{
Jens Axboe9c60ce62007-03-15 09:14:47 +0100113 unsigned long long ret;
114 long r;
115
Jens Axboe9c60ce62007-03-15 09:14:47 +0100116 r = os_random_long(&td->file_size_state);
ljzhang,Yaxin Hu,Jianchao Tangd11a5312007-07-27 15:54:10 +0200117 ret = td->o.file_size_low + (unsigned long long) ((double) (td->o.file_size_high - td->o.file_size_low) * (r / (RAND_MAX + 1.0)));
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100118 ret -= (ret % td->o.rw_min_bs);
Jens Axboe9c60ce62007-03-15 09:14:47 +0100119 return ret;
120}
121
Jens Axboe53cdc682006-10-18 11:50:58 +0200122static int file_size(struct thread_data *td, struct fio_file *f)
123{
124 struct stat st;
125
Jens Axboe7bb48f82007-03-27 15:30:28 +0200126 if (fstat(f->fd, &st) == -1) {
127 td_verror(td, errno, "fstat");
128 return 1;
129 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200130
Jens Axboe7bb48f82007-03-27 15:30:28 +0200131 f->real_file_size = st.st_size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200132 return 0;
133}
134
135static int bdev_size(struct thread_data *td, struct fio_file *f)
136{
137 unsigned long long bytes;
138 int r;
139
140 r = blockdev_size(f->fd, &bytes);
141 if (r) {
Jens Axboee1161c32007-02-22 19:36:48 +0100142 td_verror(td, r, "blockdev_size");
Jens Axboe53cdc682006-10-18 11:50:58 +0200143 return 1;
144 }
145
146 f->real_file_size = bytes;
Jens Axboe53cdc682006-10-18 11:50:58 +0200147 return 0;
148}
149
150static int get_file_size(struct thread_data *td, struct fio_file *f)
151{
152 int ret = 0;
153
Jens Axboe409b3412007-03-28 09:57:01 +0200154 if (f->flags & FIO_SIZE_KNOWN)
155 return 0;
156
Jens Axboe7bb48f82007-03-27 15:30:28 +0200157 if (f->filetype == FIO_TYPE_FILE)
158 ret = file_size(td, f);
159 else if (f->filetype == FIO_TYPE_BD)
Jens Axboe53cdc682006-10-18 11:50:58 +0200160 ret = bdev_size(td, f);
161 else
162 f->real_file_size = -1;
163
164 if (ret)
165 return ret;
166
167 if (f->file_offset > f->real_file_size) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100168 log_err("%s: offset extends end (%Lu > %Lu)\n", td->o.name, f->file_offset, f->real_file_size);
Jens Axboe53cdc682006-10-18 11:50:58 +0200169 return 1;
170 }
171
Jens Axboe409b3412007-03-28 09:57:01 +0200172 f->flags |= FIO_SIZE_KNOWN;
Jens Axboe53cdc682006-10-18 11:50:58 +0200173 return 0;
174}
175
Jens Axboee5b401d2006-10-18 16:03:40 +0200176int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
177{
178 int ret = 0;
179
Jens Axboeee56ad52008-02-01 10:30:20 +0100180 dprint(FD_IO, "invalidate cache (%d)\n", td->o.odirect);
181
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100182 if (td->o.odirect)
Jens Axboeb5af8292007-03-08 12:43:13 +0100183 return 0;
184
Jens Axboee5b401d2006-10-18 16:03:40 +0200185 /*
186 * FIXME: add blockdev flushing too
187 */
Jens Axboeb5af8292007-03-08 12:43:13 +0100188 if (f->mmap)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200189 ret = madvise(f->mmap, f->io_size, MADV_DONTNEED);
Jens Axboe467d1b62007-03-15 14:11:10 +0100190 else if (f->filetype == FIO_TYPE_FILE)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200191 ret = fadvise(f->fd, f->file_offset, f->io_size, POSIX_FADV_DONTNEED);
Jens Axboe7e0e25c2007-03-27 12:49:56 +0200192 else if (f->filetype == FIO_TYPE_BD) {
Jens Axboeb5af8292007-03-08 12:43:13 +0100193 ret = blockdev_invalidate_cache(f->fd);
Jens Axboe7e0e25c2007-03-27 12:49:56 +0200194 if (ret < 0 && errno == EACCES && geteuid()) {
Jens Axboe7172cfe2007-07-23 14:36:00 +0200195 if (!root_warn) {
196 log_err("fio: only root may flush block devices. Cache flush bypassed!\n");
197 root_warn = 1;
198 }
Jens Axboe7e0e25c2007-03-27 12:49:56 +0200199 ret = 0;
200 }
Jens Axboeb5605e92007-04-16 20:42:33 +0200201 } else if (f->filetype == FIO_TYPE_CHAR || f->filetype == FIO_TYPE_PIPE)
Jens Axboee5b401d2006-10-18 16:03:40 +0200202 ret = 0;
203
204 if (ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100205 td_verror(td, errno, "invalidate_cache");
Jens Axboee5b401d2006-10-18 16:03:40 +0200206 return 1;
207 }
208
Jens Axboead2da602007-02-26 12:33:27 +0100209 return ret;
Jens Axboee5b401d2006-10-18 16:03:40 +0200210}
211
Jens Axboe6977bcd2008-03-01 15:55:36 +0100212int generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
Jens Axboe53cdc682006-10-18 11:50:58 +0200213{
Jens Axboe6977bcd2008-03-01 15:55:36 +0100214 int ret = 0;
215
Jens Axboeee56ad52008-02-01 10:30:20 +0100216 dprint(FD_FILE, "fd close %s\n", f->file_name);
Jens Axboe6977bcd2008-03-01 15:55:36 +0100217 if (close(f->fd) < 0)
218 ret = errno;
219
Jens Axboeb5af8292007-03-08 12:43:13 +0100220 f->fd = -1;
Jens Axboe6977bcd2008-03-01 15:55:36 +0100221 return ret;
Jens Axboe53cdc682006-10-18 11:50:58 +0200222}
223
Jens Axboeb5af8292007-03-08 12:43:13 +0100224int generic_open_file(struct thread_data *td, struct fio_file *f)
Jens Axboe53cdc682006-10-18 11:50:58 +0200225{
Jens Axboe66159822007-04-16 21:54:24 +0200226 int is_std = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200227 int flags = 0;
228
Jens Axboeee56ad52008-02-01 10:30:20 +0100229 dprint(FD_FILE, "fd open %s\n", f->file_name);
230
Jens Axboe66159822007-04-16 21:54:24 +0200231 if (!strcmp(f->file_name, "-")) {
232 if (td_rw(td)) {
233 log_err("fio: can't read/write to stdin/out\n");
234 return 1;
235 }
236 is_std = 1;
Jens Axboece98fa62007-04-17 13:27:32 +0200237
238 /*
239 * move output logging to stderr, if we are writing to stdout
240 */
241 if (td_write(td))
242 f_out = stderr;
Jens Axboe66159822007-04-16 21:54:24 +0200243 }
244
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100245 if (td->o.odirect)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100246 flags |= OS_O_DIRECT;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100247 if (td->o.sync_io)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100248 flags |= O_SYNC;
Jens Axboead923962007-10-11 21:41:41 +0200249 if (f->filetype != FIO_TYPE_FILE)
250 flags |= O_NOATIME;
Jens Axboe7abf8332006-11-23 15:01:19 +0100251
Aaron Carroll056f3452007-11-26 09:08:53 +0100252open_again:
Jens Axboe660a1cb2007-04-17 15:37:47 +0200253 if (td_write(td)) {
Jens Axboe4241ea82007-09-12 08:18:36 +0200254 assert(!read_only);
255
Jens Axboe2fd233b2007-03-02 08:44:25 +0100256 flags |= O_RDWR;
Jens Axboe53cdc682006-10-18 11:50:58 +0200257
Jens Axboeaf52b342007-03-13 10:07:47 +0100258 if (f->filetype == FIO_TYPE_FILE)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100259 flags |= O_CREAT;
Jens Axboe2fd233b2007-03-02 08:44:25 +0100260
Jens Axboe66159822007-04-16 21:54:24 +0200261 if (is_std)
262 f->fd = dup(STDOUT_FILENO);
263 else
264 f->fd = open(f->file_name, flags, 0600);
Jens Axboe2fd233b2007-03-02 08:44:25 +0100265 } else {
Jens Axboe4241ea82007-09-12 08:18:36 +0200266 if (f->filetype == FIO_TYPE_CHAR && !read_only)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100267 flags |= O_RDWR;
268 else
269 flags |= O_RDONLY;
270
Jens Axboe66159822007-04-16 21:54:24 +0200271 if (is_std)
272 f->fd = dup(STDIN_FILENO);
273 else
274 f->fd = open(f->file_name, flags);
Jens Axboe53cdc682006-10-18 11:50:58 +0200275 }
276
277 if (f->fd == -1) {
Jens Axboee4e33252007-03-21 13:07:54 +0100278 char buf[FIO_VERROR_SIZE];
Jens Axboee1161c32007-02-22 19:36:48 +0100279 int __e = errno;
280
Aaron Carroll056f3452007-11-26 09:08:53 +0100281 if (errno == EPERM && (flags & O_NOATIME)) {
282 flags &= ~O_NOATIME;
283 goto open_again;
284 }
285
Jens Axboee4e33252007-03-21 13:07:54 +0100286 snprintf(buf, sizeof(buf) - 1, "open(%s)", f->file_name);
287
288 td_verror(td, __e, buf);
Jens Axboe53cdc682006-10-18 11:50:58 +0200289 }
290
Jens Axboeb5af8292007-03-08 12:43:13 +0100291 if (get_file_size(td, f))
292 goto err;
293
Jens Axboe53cdc682006-10-18 11:50:58 +0200294 return 0;
Jens Axboeb5af8292007-03-08 12:43:13 +0100295err:
296 close(f->fd);
297 return 1;
298}
299
Jens Axboe21972cd2006-11-23 12:39:16 +0100300int open_files(struct thread_data *td)
301{
302 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100303 unsigned int i;
304 int err = 0;
Jens Axboe21972cd2006-11-23 12:39:16 +0100305
Jens Axboeee56ad52008-02-01 10:30:20 +0100306 dprint(FD_FILE, "open files\n");
307
Jens Axboe21972cd2006-11-23 12:39:16 +0100308 for_each_file(td, f, i) {
Jens Axboeb5af8292007-03-08 12:43:13 +0100309 err = td_io_open_file(td, f);
Jens Axboe9bf27b42007-03-27 19:49:31 +0200310 if (err) {
311 if (td->error == EMFILE) {
312 log_err("fio: limited open files to: %d\n", td->nr_open_files);
313 td->o.open_files = td->nr_open_files;
314 err = 0;
315 clear_error(td);
316 }
Jens Axboe21972cd2006-11-23 12:39:16 +0100317 break;
Jens Axboe9bf27b42007-03-27 19:49:31 +0200318 }
Jens Axboeb5af8292007-03-08 12:43:13 +0100319
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100320 if (td->o.open_files == td->nr_open_files)
Jens Axboeb5af8292007-03-08 12:43:13 +0100321 break;
Jens Axboe21972cd2006-11-23 12:39:16 +0100322 }
323
Jens Axboe7abf8332006-11-23 15:01:19 +0100324 if (!err)
325 return 0;
326
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100327 for_each_file(td, f, i)
Jens Axboeb5af8292007-03-08 12:43:13 +0100328 td_io_close_file(td, f);
Jens Axboe7abf8332006-11-23 15:01:19 +0100329
Jens Axboe21972cd2006-11-23 12:39:16 +0100330 return err;
331}
332
Jens Axboe7bb48f82007-03-27 15:30:28 +0200333/*
334 * open/close all files, so that ->real_file_size gets set
335 */
Jens Axboebab3fd52007-04-02 10:34:18 +0200336static int get_file_sizes(struct thread_data *td)
Jens Axboe53cdc682006-10-18 11:50:58 +0200337{
338 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100339 unsigned int i;
Jens Axboebab3fd52007-04-02 10:34:18 +0200340 int err = 0;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200341
342 for_each_file(td, f, i) {
Jens Axboebab3fd52007-04-02 10:34:18 +0200343 if (td->io_ops->open_file(td, f)) {
Jens Axboe40b44f42007-04-11 12:43:35 +0200344 if (td->error != ENOENT) {
345 log_err("%s\n", td->verror);
346 err = 1;
347 }
Jens Axboe541d66d2007-03-27 19:50:11 +0200348 clear_error(td);
Jens Axboe07eb79d2007-04-12 13:02:38 +0200349 } else {
350 if (td->io_ops->close_file)
351 td->io_ops->close_file(td, f);
352 }
Jens Axboe409b3412007-03-28 09:57:01 +0200353
354 if (f->real_file_size == -1ULL && td->o.size)
355 f->real_file_size = td->o.size / td->o.nr_files;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200356 }
Jens Axboebab3fd52007-04-02 10:34:18 +0200357
358 return err;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200359}
360
361/*
362 * Open the files and setup files sizes, creating files if necessary.
363 */
364int setup_files(struct thread_data *td)
365{
366 unsigned long long total_size, extend_size;
367 struct fio_file *f;
368 unsigned int i;
Jens Axboe000b0802007-03-27 15:56:10 +0200369 int err = 0, need_extend;
Jens Axboe53cdc682006-10-18 11:50:58 +0200370
Jens Axboeee56ad52008-02-01 10:30:20 +0100371 dprint(FD_FILE, "setup files\n");
372
Jens Axboe53cdc682006-10-18 11:50:58 +0200373 /*
374 * if ioengine defines a setup() method, it's responsible for
Jens Axboe7bb48f82007-03-27 15:30:28 +0200375 * opening the files and setting f->real_file_size to indicate
376 * the valid range for that file.
Jens Axboe53cdc682006-10-18 11:50:58 +0200377 */
378 if (td->io_ops->setup)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200379 err = td->io_ops->setup(td);
380 else
Jens Axboebab3fd52007-04-02 10:34:18 +0200381 err = get_file_sizes(td);
Jens Axboe53cdc682006-10-18 11:50:58 +0200382
Jens Axboef1027062006-10-20 10:14:01 +0200383 if (err)
384 return err;
385
Jens Axboe0a7eb122006-10-20 10:36:42 +0200386 /*
Jens Axboe7bb48f82007-03-27 15:30:28 +0200387 * check sizes. if the files/devices do not exist and the size
388 * isn't passed to fio, abort.
Jens Axboe0a7eb122006-10-20 10:36:42 +0200389 */
Jens Axboe7bb48f82007-03-27 15:30:28 +0200390 total_size = 0;
391 for_each_file(td, f, i) {
392 if (f->real_file_size == -1ULL)
393 total_size = -1ULL;
394 else
395 total_size += f->real_file_size;
396 }
Jens Axboe0a7eb122006-10-20 10:36:42 +0200397
Jens Axboe7bb48f82007-03-27 15:30:28 +0200398 /*
399 * device/file sizes are zero and no size given, punt
400 */
Jens Axboe1f809d12007-10-25 18:34:02 +0200401 if ((!total_size || total_size == -1ULL) && !td->o.size &&
Shawn Lewisaa31f1f2008-01-11 09:45:11 +0100402 !(td->io_ops->flags & FIO_NOIO) && !td->o.fill_device) {
Jens Axboe7bb48f82007-03-27 15:30:28 +0200403 log_err("%s: you need to specify size=\n", td->o.name);
Jens Axboee1161c32007-02-22 19:36:48 +0100404 td_verror(td, EINVAL, "total_file_size");
Jens Axboe53cdc682006-10-18 11:50:58 +0200405 return 1;
406 }
407
Jens Axboe7bb48f82007-03-27 15:30:28 +0200408 /*
409 * now file sizes are known, so we can set ->io_size. if size= is
410 * not given, ->io_size is just equal to ->real_file_size. if size
411 * is given, ->io_size is size / nr_files.
412 */
413 extend_size = total_size = 0;
414 need_extend = 0;
415 for_each_file(td, f, i) {
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200416 f->file_offset = td->o.start_offset;
417
Jens Axboe7bb48f82007-03-27 15:30:28 +0200418 if (!td->o.file_size_low) {
419 /*
420 * no file size range given, file size is equal to
421 * total size divided by number of files. if that is
422 * zero, set it to the real file size.
423 */
424 f->io_size = td->o.size / td->o.nr_files;
Jens Axboe65bdb102008-01-24 13:13:12 +0100425 if (!f->io_size)
Jens Axboe273f8c92008-01-25 14:02:15 +0100426 f->io_size = f->real_file_size - f->file_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200427 } else if (f->real_file_size < td->o.file_size_low ||
428 f->real_file_size > td->o.file_size_high) {
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200429 if (f->file_offset > td->o.file_size_low)
430 goto err_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200431 /*
432 * file size given. if it's fixed, use that. if it's a
433 * range, generate a random size in-between.
434 */
435 if (td->o.file_size_low == td->o.file_size_high)
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200436 f->io_size = td->o.file_size_low - f->file_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200437 else
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200438 f->io_size = get_rand_file_size(td) - f->file_offset;
Jens Axboe65bdb102008-01-24 13:13:12 +0100439 } else
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200440 f->io_size = f->real_file_size - f->file_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200441
442 if (f->io_size == -1ULL)
443 total_size = -1ULL;
444 else
445 total_size += f->io_size;
446
447 if (f->filetype == FIO_TYPE_FILE &&
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200448 (f->io_size + f->file_offset) > f->real_file_size &&
Jens Axboe7bb48f82007-03-27 15:30:28 +0200449 !(td->io_ops->flags & FIO_DISKLESSIO)) {
450 need_extend++;
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200451 extend_size += (f->io_size + f->file_offset);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200452 f->flags |= FIO_FILE_EXTEND;
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200453 }
Jens Axboe7bb48f82007-03-27 15:30:28 +0200454 }
455
ljzhang,Yaxin Hu,Jianchao Tang22982902007-07-27 13:21:28 +0200456 if (!td->o.size || td->o.size > total_size)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200457 td->o.size = total_size;
458
459 /*
460 * See if we need to extend some files
461 */
462 if (need_extend) {
463 temp_stall_ts = 1;
Jens Axboef627d8a2007-08-24 10:24:09 +0200464 log_info("%s: Laying out IO file(s) (%u file(s) / %LuMiB)\n",
Jens Axboe7bb48f82007-03-27 15:30:28 +0200465 td->o.name, need_extend, extend_size >> 20);
466
467 for_each_file(td, f, i) {
468 if (!(f->flags & FIO_FILE_EXTEND))
469 continue;
470
Jens Axboe409b3412007-03-28 09:57:01 +0200471 assert(f->filetype == FIO_TYPE_FILE);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200472 f->flags &= ~FIO_FILE_EXTEND;
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200473 f->real_file_size = (f->io_size + f->file_offset);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200474 err = extend_file(td, f);
475 if (err)
476 break;
477 }
478 temp_stall_ts = 0;
479 }
480
481 if (err)
482 return err;
483
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100484 if (!td->o.zone_size)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200485 td->o.zone_size = td->o.size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200486
Jens Axboeea966f82007-09-03 10:09:37 +0200487 /*
488 * iolog already set the total io size, if we read back
489 * stored entries.
490 */
491 if (!td->o.read_iolog_file)
492 td->total_io_size = td->o.size * td->o.loops;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200493 return 0;
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200494err_offset:
495 log_err("%s: you need to specify valid offset=\n", td->o.name);
496 return 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200497}
498
Jens Axboe68727072007-03-15 20:49:25 +0100499int init_random_map(struct thread_data *td)
500{
Jens Axboe509eab12007-12-14 12:42:53 +0100501 unsigned long long blocks, num_maps;
Jens Axboe68727072007-03-15 20:49:25 +0100502 struct fio_file *f;
503 unsigned int i;
504
Jens Axboede8dd112008-03-01 15:34:44 +0100505 if (td->o.norandommap || !td_random(td))
Jens Axboe68727072007-03-15 20:49:25 +0100506 return 0;
507
508 for_each_file(td, f, i) {
Jens Axboe509eab12007-12-14 12:42:53 +0100509 blocks = (f->real_file_size + td->o.rw_min_bs - 1) / (unsigned long long) td->o.rw_min_bs;
510 num_maps = (blocks + BLOCKS_PER_MAP-1)/ (unsigned long long) BLOCKS_PER_MAP;
Jens Axboe68727072007-03-15 20:49:25 +0100511 f->file_map = malloc(num_maps * sizeof(long));
512 if (!f->file_map) {
513 log_err("fio: failed allocating random map. If running a large number of jobs, try the 'norandommap' option\n");
514 return 1;
515 }
516 f->num_maps = num_maps;
517 memset(f->file_map, 0, num_maps * sizeof(long));
518 }
519
520 return 0;
521}
522
Jens Axboe53cdc682006-10-18 11:50:58 +0200523void close_files(struct thread_data *td)
524{
Jens Axboe0ab8db82006-10-18 17:16:23 +0200525 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100526 unsigned int i;
Jens Axboe53cdc682006-10-18 11:50:58 +0200527
Jens Axboe24ffd2c2008-03-01 15:47:08 +0100528 for_each_file(td, f, i)
529 td_io_close_file(td, f);
530}
531
532void close_and_free_files(struct thread_data *td)
533{
534 struct fio_file *f;
535 unsigned int i;
536
Jens Axboeee56ad52008-02-01 10:30:20 +0100537 dprint(FD_FILE, "close files\n");
538
Jens Axboe0ab8db82006-10-18 17:16:23 +0200539 for_each_file(td, f, i) {
Jens Axboea9b7b302007-04-19 16:31:16 +0200540 if (td->o.unlink && f->filetype == FIO_TYPE_FILE)
Jens Axboe132ad462006-11-23 12:23:12 +0100541 unlink(f->file_name);
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100542
Jens Axboeb5af8292007-03-08 12:43:13 +0100543 td_io_close_file(td, f);
Jens Axboeb3dc7f02007-03-09 12:52:15 +0100544
Jens Axboefa1da862007-03-23 15:20:54 +0100545 free(f->file_name);
546 f->file_name = NULL;
547
Jens Axboec3439812007-03-20 13:54:53 +0100548 if (f->file_map) {
Jens Axboeb3dc7f02007-03-09 12:52:15 +0100549 free(f->file_map);
Jens Axboec3439812007-03-20 13:54:53 +0100550 f->file_map = NULL;
551 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200552 }
Jens Axboeb4a6a592006-10-20 13:54:47 +0200553
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100554 td->o.filename = NULL;
Jens Axboecade3ef2007-03-23 15:29:45 +0100555 free(td->files);
Jens Axboeb4a6a592006-10-20 13:54:47 +0200556 td->files = NULL;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100557 td->o.nr_files = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200558}
Jens Axboeaf52b342007-03-13 10:07:47 +0100559
Jens Axboee3bab462007-03-13 11:22:09 +0100560static void get_file_type(struct fio_file *f)
Jens Axboeaf52b342007-03-13 10:07:47 +0100561{
562 struct stat sb;
563
Jens Axboe66159822007-04-16 21:54:24 +0200564 if (!strcmp(f->file_name, "-"))
565 f->filetype = FIO_TYPE_PIPE;
566 else
567 f->filetype = FIO_TYPE_FILE;
Jens Axboeaf52b342007-03-13 10:07:47 +0100568
Jens Axboee3bab462007-03-13 11:22:09 +0100569 if (!lstat(f->file_name, &sb)) {
Jens Axboeaf52b342007-03-13 10:07:47 +0100570 if (S_ISBLK(sb.st_mode))
571 f->filetype = FIO_TYPE_BD;
572 else if (S_ISCHR(sb.st_mode))
573 f->filetype = FIO_TYPE_CHAR;
Jens Axboeb5605e92007-04-16 20:42:33 +0200574 else if (S_ISFIFO(sb.st_mode))
575 f->filetype = FIO_TYPE_PIPE;
Jens Axboeaf52b342007-03-13 10:07:47 +0100576 }
577}
578
Jens Axboef29b25a2007-07-23 08:56:43 +0200579int add_file(struct thread_data *td, const char *fname)
Jens Axboeaf52b342007-03-13 10:07:47 +0100580{
Jens Axboe7b4e4fe2007-03-13 12:51:40 +0100581 int cur_files = td->files_index;
Jens Axboebd0ee742007-03-23 14:26:23 +0100582 char file_name[PATH_MAX];
Jens Axboeaf52b342007-03-13 10:07:47 +0100583 struct fio_file *f;
Jens Axboebd0ee742007-03-23 14:26:23 +0100584 int len = 0;
Jens Axboeaf52b342007-03-13 10:07:47 +0100585
Jens Axboeee56ad52008-02-01 10:30:20 +0100586 dprint(FD_FILE, "add file %s\n", fname);
587
Jens Axboe126d65c2008-03-01 18:04:31 +0100588 f = malloc(sizeof(*f));
Jens Axboeaf52b342007-03-13 10:07:47 +0100589 memset(f, 0, sizeof(*f));
590 f->fd = -1;
Jens Axboebd0ee742007-03-23 14:26:23 +0100591
Jens Axboe126d65c2008-03-01 18:04:31 +0100592 td->files = realloc(td->files, (cur_files + 1) * sizeof(f));
593
594 td->files[cur_files] = f;
595
Jens Axboe07eb79d2007-04-12 13:02:38 +0200596 /*
597 * init function, io engine may not be loaded yet
598 */
599 if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO))
600 f->real_file_size = -1ULL;
601
Jens Axboebd0ee742007-03-23 14:26:23 +0100602 if (td->o.directory)
603 len = sprintf(file_name, "%s/", td->o.directory);
604
605 sprintf(file_name + len, "%s", fname);
606 f->file_name = strdup(file_name);
Jens Axboeaf52b342007-03-13 10:07:47 +0100607
Jens Axboee3bab462007-03-13 11:22:09 +0100608 get_file_type(f);
Jens Axboeaf52b342007-03-13 10:07:47 +0100609
Jens Axboe7b4e4fe2007-03-13 12:51:40 +0100610 td->files_index++;
Jens Axboe15494412007-03-13 11:52:47 +0100611 if (f->filetype == FIO_TYPE_FILE)
612 td->nr_normal_files++;
Jens Axboef29b25a2007-07-23 08:56:43 +0200613
614 return cur_files;
Jens Axboeaf52b342007-03-13 10:07:47 +0100615}
Jens Axboe0ad920e2007-03-13 11:06:45 +0100616
617void get_file(struct fio_file *f)
618{
Jens Axboe8172fe92008-02-01 21:51:02 +0100619 dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references);
Jens Axboe97af62c2007-05-22 11:12:13 +0200620 assert(f->flags & FIO_FILE_OPEN);
Jens Axboe0ad920e2007-03-13 11:06:45 +0100621 f->references++;
622}
623
Jens Axboe6977bcd2008-03-01 15:55:36 +0100624int put_file(struct thread_data *td, struct fio_file *f)
Jens Axboe0ad920e2007-03-13 11:06:45 +0100625{
Jens Axboe6977bcd2008-03-01 15:55:36 +0100626 int ret = 0;
627
Jens Axboe8172fe92008-02-01 21:51:02 +0100628 dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references);
Jens Axboeee56ad52008-02-01 10:30:20 +0100629
Jens Axboe0ad920e2007-03-13 11:06:45 +0100630 if (!(f->flags & FIO_FILE_OPEN))
Jens Axboe6977bcd2008-03-01 15:55:36 +0100631 return 0;
Jens Axboe0ad920e2007-03-13 11:06:45 +0100632
633 assert(f->references);
634 if (--f->references)
Jens Axboe6977bcd2008-03-01 15:55:36 +0100635 return 0;
Jens Axboe0ad920e2007-03-13 11:06:45 +0100636
Jens Axboed424d4d2007-04-17 09:05:10 +0200637 if (should_fsync(td) && td->o.fsync_on_close)
Jens Axboeebb14152007-03-13 14:42:15 +0100638 fsync(f->fd);
639
Jens Axboe0ad920e2007-03-13 11:06:45 +0100640 if (td->io_ops->close_file)
Jens Axboe6977bcd2008-03-01 15:55:36 +0100641 ret = td->io_ops->close_file(td, f);
Jens Axboe1020a132007-03-29 11:15:06 +0200642
Jens Axboe0ad920e2007-03-13 11:06:45 +0100643 td->nr_open_files--;
644 f->flags &= ~FIO_FILE_OPEN;
Jens Axboe6977bcd2008-03-01 15:55:36 +0100645 return ret;
Jens Axboe0ad920e2007-03-13 11:06:45 +0100646}
Jens Axboebbf6b542007-03-13 15:28:55 +0100647
Jens Axboeb2bd2bd2008-03-01 18:19:52 +0100648void lock_file(struct thread_data *td, struct fio_file *f)
649{
650}
651
652void unlock_file(struct fio_file *f)
653{
654}
655
Jens Axboebbf6b542007-03-13 15:28:55 +0100656static int recurse_dir(struct thread_data *td, const char *dirname)
657{
658 struct dirent *dir;
659 int ret = 0;
660 DIR *D;
661
662 D = opendir(dirname);
663 if (!D) {
Jens Axboe0ddb2702007-03-27 19:43:53 +0200664 char buf[FIO_VERROR_SIZE];
665
666 snprintf(buf, FIO_VERROR_SIZE - 1, "opendir(%s)", dirname);
667 td_verror(td, errno, buf);
Jens Axboebbf6b542007-03-13 15:28:55 +0100668 return 1;
669 }
670
671 while ((dir = readdir(D)) != NULL) {
672 char full_path[PATH_MAX];
673 struct stat sb;
674
Jens Axboee85b2b82007-03-14 09:39:06 +0100675 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
676 continue;
Jens Axboe96d32d52007-03-14 09:16:09 +0100677
Jens Axboebbf6b542007-03-13 15:28:55 +0100678 sprintf(full_path, "%s/%s", dirname, dir->d_name);
679
680 if (lstat(full_path, &sb) == -1) {
681 if (errno != ENOENT) {
682 td_verror(td, errno, "stat");
683 return 1;
684 }
685 }
686
687 if (S_ISREG(sb.st_mode)) {
688 add_file(td, full_path);
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100689 td->o.nr_files++;
Jens Axboebbf6b542007-03-13 15:28:55 +0100690 continue;
691 }
Jens Axboe0ddb2702007-03-27 19:43:53 +0200692 if (!S_ISDIR(sb.st_mode))
693 continue;
Jens Axboebbf6b542007-03-13 15:28:55 +0100694
Jens Axboebbf6b542007-03-13 15:28:55 +0100695 if ((ret = recurse_dir(td, full_path)) != 0)
696 break;
697 }
698
699 closedir(D);
700 return ret;
701}
702
703int add_dir_files(struct thread_data *td, const char *path)
704{
Jens Axboe0ddb2702007-03-27 19:43:53 +0200705 int ret = recurse_dir(td, path);
706
707 if (!ret)
708 log_info("fio: opendir added %d files\n", td->o.nr_files);
709
710 return ret;
Jens Axboebbf6b542007-03-13 15:28:55 +0100711}
Jens Axboecade3ef2007-03-23 15:29:45 +0100712
713void dup_files(struct thread_data *td, struct thread_data *org)
714{
715 struct fio_file *f;
716 unsigned int i;
717 size_t bytes;
718
719 if (!org->files)
720 return;
721
Jens Axboeb0fe4212008-03-01 18:22:27 +0100722 bytes = org->files_index * sizeof(f);
Jens Axboecade3ef2007-03-23 15:29:45 +0100723 td->files = malloc(bytes);
724 memcpy(td->files, org->files, bytes);
725
726 for_each_file(td, f, i) {
Jens Axboeb0fe4212008-03-01 18:22:27 +0100727 struct fio_file *__f;
728
729 __f = malloc(sizeof(*__f));
730 memset(f, 0, sizeof(*__f));
731
Jens Axboecade3ef2007-03-23 15:29:45 +0100732 if (f->file_name)
Jens Axboeb0fe4212008-03-01 18:22:27 +0100733 __f->file_name = strdup(f->file_name);
734
735 td->files[i] = __f;
Jens Axboecade3ef2007-03-23 15:29:45 +0100736 }
737}
Jens Axboef29b25a2007-07-23 08:56:43 +0200738
739/*
740 * Returns the index that matches the filename, or -1 if not there
741 */
742int get_fileno(struct thread_data *td, const char *fname)
743{
744 struct fio_file *f;
745 unsigned int i;
746
747 for_each_file(td, f, i)
748 if (!strcmp(f->file_name, fname))
749 return i;
750
751 return -1;
752}
753
754/*
755 * For log usage, where we add/open/close files automatically
756 */
757void free_release_files(struct thread_data *td)
758{
759 close_files(td);
760 td->files_index = 0;
761 td->nr_normal_files = 0;
762}