blob: ac2bed74d4c3e19705caefbbf76c689888adb291 [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 Axboef17c4392008-03-01 18:40:46 +010011#include "smalloc.h"
Jens Axboe53cdc682006-10-18 11:50:58 +020012
Jens Axboe7172cfe2007-07-23 14:36:00 +020013static int root_warn;
14
Jens Axboe7bb48f82007-03-27 15:30:28 +020015static int extend_file(struct thread_data *td, struct fio_file *f)
Jens Axboe25205e92006-10-19 20:50:14 +020016{
Jens Axboeea443652007-03-29 14:52:13 +020017 int r, new_layout = 0, unlink_file = 0, flags;
Jens Axboe25205e92006-10-19 20:50:14 +020018 unsigned long long left;
19 unsigned int bs;
20 char *b;
Jens Axboeb2a15192006-10-18 14:10:42 +020021
Jens Axboe4241ea82007-09-12 08:18:36 +020022 if (read_only) {
23 log_err("fio: refusing extend of file due to read-only\n");
24 return 0;
25 }
26
Jens Axboe507a7022007-03-27 15:52:46 +020027 /*
28 * check if we need to lay the file out complete again. fio
29 * does that for operations involving reads, or for writes
30 * where overwrite is set
31 */
32 if (td_read(td) || (td_write(td) && td->o.overwrite))
33 new_layout = 1;
Jens Axboeea443652007-03-29 14:52:13 +020034 if (td_write(td) && !td->o.overwrite)
35 unlink_file = 1;
Jens Axboe507a7022007-03-27 15:52:46 +020036
Jens Axboe6ae1f572008-02-21 10:20:00 +010037 if (unlink_file || new_layout) {
Zhang, Yanmin982016d2008-02-26 15:35:52 +010038 if ((unlink(f->file_name) < 0) && (errno != ENOENT)) {
Jens Axboe7bb48f82007-03-27 15:30:28 +020039 td_verror(td, errno, "unlink");
40 return 1;
41 }
42 }
43
Jens Axboe507a7022007-03-27 15:52:46 +020044 flags = O_WRONLY | O_CREAT;
45 if (new_layout)
46 flags |= O_TRUNC;
47
Jens Axboeee56ad52008-02-01 10:30:20 +010048 dprint(FD_FILE, "open file %s, flags %x\n", f->file_name, flags);
Jens Axboe507a7022007-03-27 15:52:46 +020049 f->fd = open(f->file_name, flags, 0644);
Jens Axboe53cdc682006-10-18 11:50:58 +020050 if (f->fd < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +010051 td_verror(td, errno, "open");
Jens Axboe53cdc682006-10-18 11:50:58 +020052 return 1;
53 }
54
Shawn Lewisfc74ac12008-01-11 09:45:11 +010055 if (!new_layout)
56 goto done;
57
Jens Axboeee56ad52008-02-01 10:30:20 +010058 dprint(FD_FILE, "truncate file %s, size %llu\n", f->file_name,
59 f->real_file_size);
Jens Axboe7bb48f82007-03-27 15:30:28 +020060 if (ftruncate(f->fd, f->real_file_size) == -1) {
Jens Axboee1161c32007-02-22 19:36:48 +010061 td_verror(td, errno, "ftruncate");
Jens Axboe53cdc682006-10-18 11:50:58 +020062 goto err;
63 }
64
Jens Axboeee56ad52008-02-01 10:30:20 +010065 dprint(FD_FILE, "fallocate file %s, size %llu\n", f->file_name,
66 f->real_file_size);
Jens Axboe7bb48f82007-03-27 15:30:28 +020067 if (posix_fallocate(f->fd, 0, f->real_file_size) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +010068 td_verror(td, errno, "posix_fallocate");
Jens Axboe40f82982006-10-25 11:21:05 +020069 goto err;
70 }
71
Jens Axboe2dc1bbe2007-03-15 15:01:33 +010072 b = malloc(td->o.max_bs[DDIR_WRITE]);
73 memset(b, 0, td->o.max_bs[DDIR_WRITE]);
Jens Axboe53cdc682006-10-18 11:50:58 +020074
Jens Axboe7bb48f82007-03-27 15:30:28 +020075 left = f->real_file_size;
Jens Axboe53cdc682006-10-18 11:50:58 +020076 while (left && !td->terminate) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +010077 bs = td->o.max_bs[DDIR_WRITE];
Jens Axboe53cdc682006-10-18 11:50:58 +020078 if (bs > left)
79 bs = left;
80
81 r = write(f->fd, b, bs);
82
83 if (r == (int) bs) {
84 left -= bs;
85 continue;
86 } else {
87 if (r < 0)
Jens Axboee1161c32007-02-22 19:36:48 +010088 td_verror(td, errno, "write");
Jens Axboe53cdc682006-10-18 11:50:58 +020089 else
Jens Axboee1161c32007-02-22 19:36:48 +010090 td_verror(td, EIO, "write");
Jens Axboe53cdc682006-10-18 11:50:58 +020091
92 break;
93 }
94 }
95
96 if (td->terminate)
97 unlink(f->file_name);
Jens Axboe2dc1bbe2007-03-15 15:01:33 +010098 else if (td->o.create_fsync)
Jens Axboe53cdc682006-10-18 11:50:58 +020099 fsync(f->fd);
100
101 free(b);
Jens Axboe507a7022007-03-27 15:52:46 +0200102done:
Jens Axboe53cdc682006-10-18 11:50:58 +0200103 close(f->fd);
104 f->fd = -1;
105 return 0;
106err:
107 close(f->fd);
108 f->fd = -1;
109 return 1;
110}
111
Jens Axboe7bb48f82007-03-27 15:30:28 +0200112static unsigned long long get_rand_file_size(struct thread_data *td)
Jens Axboe9c60ce62007-03-15 09:14:47 +0100113{
Jens Axboe9c60ce62007-03-15 09:14:47 +0100114 unsigned long long ret;
115 long r;
116
Jens Axboe9c60ce62007-03-15 09:14:47 +0100117 r = os_random_long(&td->file_size_state);
ljzhang,Yaxin Hu,Jianchao Tangd11a5312007-07-27 15:54:10 +0200118 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 +0100119 ret -= (ret % td->o.rw_min_bs);
Jens Axboe9c60ce62007-03-15 09:14:47 +0100120 return ret;
121}
122
Jens Axboe53cdc682006-10-18 11:50:58 +0200123static int file_size(struct thread_data *td, struct fio_file *f)
124{
125 struct stat st;
126
Jens Axboe7bb48f82007-03-27 15:30:28 +0200127 if (fstat(f->fd, &st) == -1) {
128 td_verror(td, errno, "fstat");
129 return 1;
130 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200131
Jens Axboe7bb48f82007-03-27 15:30:28 +0200132 f->real_file_size = st.st_size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200133 return 0;
134}
135
136static int bdev_size(struct thread_data *td, struct fio_file *f)
137{
138 unsigned long long bytes;
139 int r;
140
141 r = blockdev_size(f->fd, &bytes);
142 if (r) {
Jens Axboee1161c32007-02-22 19:36:48 +0100143 td_verror(td, r, "blockdev_size");
Jens Axboe53cdc682006-10-18 11:50:58 +0200144 return 1;
145 }
146
147 f->real_file_size = bytes;
Jens Axboe53cdc682006-10-18 11:50:58 +0200148 return 0;
149}
150
151static int get_file_size(struct thread_data *td, struct fio_file *f)
152{
153 int ret = 0;
154
Jens Axboe409b3412007-03-28 09:57:01 +0200155 if (f->flags & FIO_SIZE_KNOWN)
156 return 0;
157
Jens Axboe7bb48f82007-03-27 15:30:28 +0200158 if (f->filetype == FIO_TYPE_FILE)
159 ret = file_size(td, f);
160 else if (f->filetype == FIO_TYPE_BD)
Jens Axboe53cdc682006-10-18 11:50:58 +0200161 ret = bdev_size(td, f);
162 else
163 f->real_file_size = -1;
164
165 if (ret)
166 return ret;
167
168 if (f->file_offset > f->real_file_size) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100169 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 +0200170 return 1;
171 }
172
Jens Axboe409b3412007-03-28 09:57:01 +0200173 f->flags |= FIO_SIZE_KNOWN;
Jens Axboe53cdc682006-10-18 11:50:58 +0200174 return 0;
175}
176
Jens Axboee5b401d2006-10-18 16:03:40 +0200177int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
178{
179 int ret = 0;
180
Jens Axboeee56ad52008-02-01 10:30:20 +0100181 dprint(FD_IO, "invalidate cache (%d)\n", td->o.odirect);
182
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100183 if (td->o.odirect)
Jens Axboeb5af8292007-03-08 12:43:13 +0100184 return 0;
185
Jens Axboee5b401d2006-10-18 16:03:40 +0200186 /*
187 * FIXME: add blockdev flushing too
188 */
Jens Axboeb5af8292007-03-08 12:43:13 +0100189 if (f->mmap)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200190 ret = madvise(f->mmap, f->io_size, MADV_DONTNEED);
Jens Axboe467d1b62007-03-15 14:11:10 +0100191 else if (f->filetype == FIO_TYPE_FILE)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200192 ret = fadvise(f->fd, f->file_offset, f->io_size, POSIX_FADV_DONTNEED);
Jens Axboe7e0e25c2007-03-27 12:49:56 +0200193 else if (f->filetype == FIO_TYPE_BD) {
Jens Axboeb5af8292007-03-08 12:43:13 +0100194 ret = blockdev_invalidate_cache(f->fd);
Jens Axboe7e0e25c2007-03-27 12:49:56 +0200195 if (ret < 0 && errno == EACCES && geteuid()) {
Jens Axboe7172cfe2007-07-23 14:36:00 +0200196 if (!root_warn) {
197 log_err("fio: only root may flush block devices. Cache flush bypassed!\n");
198 root_warn = 1;
199 }
Jens Axboe7e0e25c2007-03-27 12:49:56 +0200200 ret = 0;
201 }
Jens Axboeb5605e92007-04-16 20:42:33 +0200202 } else if (f->filetype == FIO_TYPE_CHAR || f->filetype == FIO_TYPE_PIPE)
Jens Axboee5b401d2006-10-18 16:03:40 +0200203 ret = 0;
204
205 if (ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100206 td_verror(td, errno, "invalidate_cache");
Jens Axboee5b401d2006-10-18 16:03:40 +0200207 return 1;
208 }
209
Jens Axboead2da602007-02-26 12:33:27 +0100210 return ret;
Jens Axboee5b401d2006-10-18 16:03:40 +0200211}
212
Jens Axboe6977bcd2008-03-01 15:55:36 +0100213int generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
Jens Axboe53cdc682006-10-18 11:50:58 +0200214{
Jens Axboe6977bcd2008-03-01 15:55:36 +0100215 int ret = 0;
216
Jens Axboeee56ad52008-02-01 10:30:20 +0100217 dprint(FD_FILE, "fd close %s\n", f->file_name);
Jens Axboe6977bcd2008-03-01 15:55:36 +0100218 if (close(f->fd) < 0)
219 ret = errno;
220
Jens Axboeb5af8292007-03-08 12:43:13 +0100221 f->fd = -1;
Jens Axboe6977bcd2008-03-01 15:55:36 +0100222 return ret;
Jens Axboe53cdc682006-10-18 11:50:58 +0200223}
224
Jens Axboeb5af8292007-03-08 12:43:13 +0100225int generic_open_file(struct thread_data *td, struct fio_file *f)
Jens Axboe53cdc682006-10-18 11:50:58 +0200226{
Jens Axboe66159822007-04-16 21:54:24 +0200227 int is_std = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200228 int flags = 0;
229
Jens Axboeee56ad52008-02-01 10:30:20 +0100230 dprint(FD_FILE, "fd open %s\n", f->file_name);
231
Jens Axboe66159822007-04-16 21:54:24 +0200232 if (!strcmp(f->file_name, "-")) {
233 if (td_rw(td)) {
234 log_err("fio: can't read/write to stdin/out\n");
235 return 1;
236 }
237 is_std = 1;
Jens Axboece98fa62007-04-17 13:27:32 +0200238
239 /*
240 * move output logging to stderr, if we are writing to stdout
241 */
242 if (td_write(td))
243 f_out = stderr;
Jens Axboe66159822007-04-16 21:54:24 +0200244 }
245
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100246 if (td->o.odirect)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100247 flags |= OS_O_DIRECT;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100248 if (td->o.sync_io)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100249 flags |= O_SYNC;
Jens Axboead923962007-10-11 21:41:41 +0200250 if (f->filetype != FIO_TYPE_FILE)
251 flags |= O_NOATIME;
Jens Axboe7abf8332006-11-23 15:01:19 +0100252
Aaron Carroll056f3452007-11-26 09:08:53 +0100253open_again:
Jens Axboe660a1cb2007-04-17 15:37:47 +0200254 if (td_write(td)) {
Jens Axboe4241ea82007-09-12 08:18:36 +0200255 assert(!read_only);
256
Jens Axboe2fd233b2007-03-02 08:44:25 +0100257 flags |= O_RDWR;
Jens Axboe53cdc682006-10-18 11:50:58 +0200258
Jens Axboeaf52b342007-03-13 10:07:47 +0100259 if (f->filetype == FIO_TYPE_FILE)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100260 flags |= O_CREAT;
Jens Axboe2fd233b2007-03-02 08:44:25 +0100261
Jens Axboe66159822007-04-16 21:54:24 +0200262 if (is_std)
263 f->fd = dup(STDOUT_FILENO);
264 else
265 f->fd = open(f->file_name, flags, 0600);
Jens Axboe2fd233b2007-03-02 08:44:25 +0100266 } else {
Jens Axboe4241ea82007-09-12 08:18:36 +0200267 if (f->filetype == FIO_TYPE_CHAR && !read_only)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100268 flags |= O_RDWR;
269 else
270 flags |= O_RDONLY;
271
Jens Axboe66159822007-04-16 21:54:24 +0200272 if (is_std)
273 f->fd = dup(STDIN_FILENO);
274 else
275 f->fd = open(f->file_name, flags);
Jens Axboe53cdc682006-10-18 11:50:58 +0200276 }
277
278 if (f->fd == -1) {
Jens Axboee4e33252007-03-21 13:07:54 +0100279 char buf[FIO_VERROR_SIZE];
Jens Axboee1161c32007-02-22 19:36:48 +0100280 int __e = errno;
281
Aaron Carroll056f3452007-11-26 09:08:53 +0100282 if (errno == EPERM && (flags & O_NOATIME)) {
283 flags &= ~O_NOATIME;
284 goto open_again;
285 }
286
Jens Axboee4e33252007-03-21 13:07:54 +0100287 snprintf(buf, sizeof(buf) - 1, "open(%s)", f->file_name);
288
289 td_verror(td, __e, buf);
Jens Axboe53cdc682006-10-18 11:50:58 +0200290 }
291
Jens Axboeb5af8292007-03-08 12:43:13 +0100292 if (get_file_size(td, f))
293 goto err;
294
Jens Axboe53cdc682006-10-18 11:50:58 +0200295 return 0;
Jens Axboeb5af8292007-03-08 12:43:13 +0100296err:
297 close(f->fd);
298 return 1;
299}
300
Jens Axboe21972cd2006-11-23 12:39:16 +0100301int open_files(struct thread_data *td)
302{
303 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100304 unsigned int i;
305 int err = 0;
Jens Axboe21972cd2006-11-23 12:39:16 +0100306
Jens Axboeee56ad52008-02-01 10:30:20 +0100307 dprint(FD_FILE, "open files\n");
308
Jens Axboe21972cd2006-11-23 12:39:16 +0100309 for_each_file(td, f, i) {
Jens Axboeb5af8292007-03-08 12:43:13 +0100310 err = td_io_open_file(td, f);
Jens Axboe9bf27b42007-03-27 19:49:31 +0200311 if (err) {
312 if (td->error == EMFILE) {
313 log_err("fio: limited open files to: %d\n", td->nr_open_files);
314 td->o.open_files = td->nr_open_files;
315 err = 0;
316 clear_error(td);
317 }
Jens Axboe21972cd2006-11-23 12:39:16 +0100318 break;
Jens Axboe9bf27b42007-03-27 19:49:31 +0200319 }
Jens Axboeb5af8292007-03-08 12:43:13 +0100320
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100321 if (td->o.open_files == td->nr_open_files)
Jens Axboeb5af8292007-03-08 12:43:13 +0100322 break;
Jens Axboe21972cd2006-11-23 12:39:16 +0100323 }
324
Jens Axboe7abf8332006-11-23 15:01:19 +0100325 if (!err)
326 return 0;
327
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100328 for_each_file(td, f, i)
Jens Axboeb5af8292007-03-08 12:43:13 +0100329 td_io_close_file(td, f);
Jens Axboe7abf8332006-11-23 15:01:19 +0100330
Jens Axboe21972cd2006-11-23 12:39:16 +0100331 return err;
332}
333
Jens Axboe7bb48f82007-03-27 15:30:28 +0200334/*
335 * open/close all files, so that ->real_file_size gets set
336 */
Jens Axboebab3fd52007-04-02 10:34:18 +0200337static int get_file_sizes(struct thread_data *td)
Jens Axboe53cdc682006-10-18 11:50:58 +0200338{
339 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100340 unsigned int i;
Jens Axboebab3fd52007-04-02 10:34:18 +0200341 int err = 0;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200342
343 for_each_file(td, f, i) {
Jens Axboebab3fd52007-04-02 10:34:18 +0200344 if (td->io_ops->open_file(td, f)) {
Jens Axboe40b44f42007-04-11 12:43:35 +0200345 if (td->error != ENOENT) {
346 log_err("%s\n", td->verror);
347 err = 1;
348 }
Jens Axboe541d66d2007-03-27 19:50:11 +0200349 clear_error(td);
Jens Axboe07eb79d2007-04-12 13:02:38 +0200350 } else {
351 if (td->io_ops->close_file)
352 td->io_ops->close_file(td, f);
353 }
Jens Axboe409b3412007-03-28 09:57:01 +0200354
355 if (f->real_file_size == -1ULL && td->o.size)
356 f->real_file_size = td->o.size / td->o.nr_files;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200357 }
Jens Axboebab3fd52007-04-02 10:34:18 +0200358
359 return err;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200360}
361
362/*
363 * Open the files and setup files sizes, creating files if necessary.
364 */
365int setup_files(struct thread_data *td)
366{
367 unsigned long long total_size, extend_size;
368 struct fio_file *f;
369 unsigned int i;
Jens Axboe000b0802007-03-27 15:56:10 +0200370 int err = 0, need_extend;
Jens Axboe53cdc682006-10-18 11:50:58 +0200371
Jens Axboeee56ad52008-02-01 10:30:20 +0100372 dprint(FD_FILE, "setup files\n");
373
Jens Axboe53cdc682006-10-18 11:50:58 +0200374 /*
375 * if ioengine defines a setup() method, it's responsible for
Jens Axboe7bb48f82007-03-27 15:30:28 +0200376 * opening the files and setting f->real_file_size to indicate
377 * the valid range for that file.
Jens Axboe53cdc682006-10-18 11:50:58 +0200378 */
379 if (td->io_ops->setup)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200380 err = td->io_ops->setup(td);
381 else
Jens Axboebab3fd52007-04-02 10:34:18 +0200382 err = get_file_sizes(td);
Jens Axboe53cdc682006-10-18 11:50:58 +0200383
Jens Axboef1027062006-10-20 10:14:01 +0200384 if (err)
385 return err;
386
Jens Axboe0a7eb122006-10-20 10:36:42 +0200387 /*
Jens Axboe7bb48f82007-03-27 15:30:28 +0200388 * check sizes. if the files/devices do not exist and the size
389 * isn't passed to fio, abort.
Jens Axboe0a7eb122006-10-20 10:36:42 +0200390 */
Jens Axboe7bb48f82007-03-27 15:30:28 +0200391 total_size = 0;
392 for_each_file(td, f, i) {
393 if (f->real_file_size == -1ULL)
394 total_size = -1ULL;
395 else
396 total_size += f->real_file_size;
397 }
Jens Axboe0a7eb122006-10-20 10:36:42 +0200398
Jens Axboe7bb48f82007-03-27 15:30:28 +0200399 /*
400 * device/file sizes are zero and no size given, punt
401 */
Jens Axboe1f809d12007-10-25 18:34:02 +0200402 if ((!total_size || total_size == -1ULL) && !td->o.size &&
Shawn Lewisaa31f1f2008-01-11 09:45:11 +0100403 !(td->io_ops->flags & FIO_NOIO) && !td->o.fill_device) {
Jens Axboe7bb48f82007-03-27 15:30:28 +0200404 log_err("%s: you need to specify size=\n", td->o.name);
Jens Axboee1161c32007-02-22 19:36:48 +0100405 td_verror(td, EINVAL, "total_file_size");
Jens Axboe53cdc682006-10-18 11:50:58 +0200406 return 1;
407 }
408
Jens Axboe7bb48f82007-03-27 15:30:28 +0200409 /*
410 * now file sizes are known, so we can set ->io_size. if size= is
411 * not given, ->io_size is just equal to ->real_file_size. if size
412 * is given, ->io_size is size / nr_files.
413 */
414 extend_size = total_size = 0;
415 need_extend = 0;
416 for_each_file(td, f, i) {
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200417 f->file_offset = td->o.start_offset;
418
Jens Axboe7bb48f82007-03-27 15:30:28 +0200419 if (!td->o.file_size_low) {
420 /*
421 * no file size range given, file size is equal to
422 * total size divided by number of files. if that is
423 * zero, set it to the real file size.
424 */
425 f->io_size = td->o.size / td->o.nr_files;
Jens Axboe65bdb102008-01-24 13:13:12 +0100426 if (!f->io_size)
Jens Axboe273f8c92008-01-25 14:02:15 +0100427 f->io_size = f->real_file_size - f->file_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200428 } else if (f->real_file_size < td->o.file_size_low ||
429 f->real_file_size > td->o.file_size_high) {
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200430 if (f->file_offset > td->o.file_size_low)
431 goto err_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200432 /*
433 * file size given. if it's fixed, use that. if it's a
434 * range, generate a random size in-between.
435 */
436 if (td->o.file_size_low == td->o.file_size_high)
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200437 f->io_size = td->o.file_size_low - f->file_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200438 else
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200439 f->io_size = get_rand_file_size(td) - f->file_offset;
Jens Axboe65bdb102008-01-24 13:13:12 +0100440 } else
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200441 f->io_size = f->real_file_size - f->file_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200442
443 if (f->io_size == -1ULL)
444 total_size = -1ULL;
445 else
446 total_size += f->io_size;
447
448 if (f->filetype == FIO_TYPE_FILE &&
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200449 (f->io_size + f->file_offset) > f->real_file_size &&
Jens Axboe7bb48f82007-03-27 15:30:28 +0200450 !(td->io_ops->flags & FIO_DISKLESSIO)) {
451 need_extend++;
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200452 extend_size += (f->io_size + f->file_offset);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200453 f->flags |= FIO_FILE_EXTEND;
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200454 }
Jens Axboe7bb48f82007-03-27 15:30:28 +0200455 }
456
ljzhang,Yaxin Hu,Jianchao Tang22982902007-07-27 13:21:28 +0200457 if (!td->o.size || td->o.size > total_size)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200458 td->o.size = total_size;
459
460 /*
461 * See if we need to extend some files
462 */
463 if (need_extend) {
464 temp_stall_ts = 1;
Jens Axboef627d8a2007-08-24 10:24:09 +0200465 log_info("%s: Laying out IO file(s) (%u file(s) / %LuMiB)\n",
Jens Axboe7bb48f82007-03-27 15:30:28 +0200466 td->o.name, need_extend, extend_size >> 20);
467
468 for_each_file(td, f, i) {
469 if (!(f->flags & FIO_FILE_EXTEND))
470 continue;
471
Jens Axboe409b3412007-03-28 09:57:01 +0200472 assert(f->filetype == FIO_TYPE_FILE);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200473 f->flags &= ~FIO_FILE_EXTEND;
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200474 f->real_file_size = (f->io_size + f->file_offset);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200475 err = extend_file(td, f);
476 if (err)
477 break;
478 }
479 temp_stall_ts = 0;
480 }
481
482 if (err)
483 return err;
484
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100485 if (!td->o.zone_size)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200486 td->o.zone_size = td->o.size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200487
Jens Axboeea966f82007-09-03 10:09:37 +0200488 /*
489 * iolog already set the total io size, if we read back
490 * stored entries.
491 */
492 if (!td->o.read_iolog_file)
493 td->total_io_size = td->o.size * td->o.loops;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200494 return 0;
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200495err_offset:
496 log_err("%s: you need to specify valid offset=\n", td->o.name);
497 return 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200498}
499
Jens Axboe68727072007-03-15 20:49:25 +0100500int init_random_map(struct thread_data *td)
501{
Jens Axboe509eab12007-12-14 12:42:53 +0100502 unsigned long long blocks, num_maps;
Jens Axboe68727072007-03-15 20:49:25 +0100503 struct fio_file *f;
504 unsigned int i;
505
Jens Axboede8dd112008-03-01 15:34:44 +0100506 if (td->o.norandommap || !td_random(td))
Jens Axboe68727072007-03-15 20:49:25 +0100507 return 0;
508
509 for_each_file(td, f, i) {
Jens Axboe509eab12007-12-14 12:42:53 +0100510 blocks = (f->real_file_size + td->o.rw_min_bs - 1) / (unsigned long long) td->o.rw_min_bs;
511 num_maps = (blocks + BLOCKS_PER_MAP-1)/ (unsigned long long) BLOCKS_PER_MAP;
Jens Axboef17c4392008-03-01 18:40:46 +0100512 f->file_map = smalloc(num_maps * sizeof(long));
Jens Axboe68727072007-03-15 20:49:25 +0100513 if (!f->file_map) {
514 log_err("fio: failed allocating random map. If running a large number of jobs, try the 'norandommap' option\n");
515 return 1;
516 }
517 f->num_maps = num_maps;
Jens Axboe68727072007-03-15 20:49:25 +0100518 }
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 Axboef17c4392008-03-01 18:40:46 +0100545 sfree(f->file_name);
Jens Axboefa1da862007-03-23 15:20:54 +0100546 f->file_name = NULL;
547
Jens Axboec3439812007-03-20 13:54:53 +0100548 if (f->file_map) {
Jens Axboef17c4392008-03-01 18:40:46 +0100549 sfree(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 Axboef17c4392008-03-01 18:40:46 +0100588 f = smalloc(sizeof(*f));
Jens Axboeaf52b342007-03-13 10:07:47 +0100589 f->fd = -1;
Jens Axboebd0ee742007-03-23 14:26:23 +0100590
Jens Axboe126d65c2008-03-01 18:04:31 +0100591 td->files = realloc(td->files, (cur_files + 1) * sizeof(f));
592
593 td->files[cur_files] = f;
594
Jens Axboe07eb79d2007-04-12 13:02:38 +0200595 /*
596 * init function, io engine may not be loaded yet
597 */
598 if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO))
599 f->real_file_size = -1ULL;
600
Jens Axboebd0ee742007-03-23 14:26:23 +0100601 if (td->o.directory)
602 len = sprintf(file_name, "%s/", td->o.directory);
603
604 sprintf(file_name + len, "%s", fname);
Jens Axboef17c4392008-03-01 18:40:46 +0100605 f->file_name = smalloc_strdup(file_name);
Jens Axboeaf52b342007-03-13 10:07:47 +0100606
Jens Axboee3bab462007-03-13 11:22:09 +0100607 get_file_type(f);
Jens Axboeaf52b342007-03-13 10:07:47 +0100608
Jens Axboe7b4e4fe2007-03-13 12:51:40 +0100609 td->files_index++;
Jens Axboe15494412007-03-13 11:52:47 +0100610 if (f->filetype == FIO_TYPE_FILE)
611 td->nr_normal_files++;
Jens Axboef29b25a2007-07-23 08:56:43 +0200612
613 return cur_files;
Jens Axboeaf52b342007-03-13 10:07:47 +0100614}
Jens Axboe0ad920e2007-03-13 11:06:45 +0100615
616void get_file(struct fio_file *f)
617{
Jens Axboe8172fe92008-02-01 21:51:02 +0100618 dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references);
Jens Axboe97af62c2007-05-22 11:12:13 +0200619 assert(f->flags & FIO_FILE_OPEN);
Jens Axboe0ad920e2007-03-13 11:06:45 +0100620 f->references++;
621}
622
Jens Axboe6977bcd2008-03-01 15:55:36 +0100623int put_file(struct thread_data *td, struct fio_file *f)
Jens Axboe0ad920e2007-03-13 11:06:45 +0100624{
Jens Axboe6977bcd2008-03-01 15:55:36 +0100625 int ret = 0;
626
Jens Axboe8172fe92008-02-01 21:51:02 +0100627 dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references);
Jens Axboeee56ad52008-02-01 10:30:20 +0100628
Jens Axboe0ad920e2007-03-13 11:06:45 +0100629 if (!(f->flags & FIO_FILE_OPEN))
Jens Axboe6977bcd2008-03-01 15:55:36 +0100630 return 0;
Jens Axboe0ad920e2007-03-13 11:06:45 +0100631
632 assert(f->references);
633 if (--f->references)
Jens Axboe6977bcd2008-03-01 15:55:36 +0100634 return 0;
Jens Axboe0ad920e2007-03-13 11:06:45 +0100635
Jens Axboed424d4d2007-04-17 09:05:10 +0200636 if (should_fsync(td) && td->o.fsync_on_close)
Jens Axboeebb14152007-03-13 14:42:15 +0100637 fsync(f->fd);
638
Jens Axboe0ad920e2007-03-13 11:06:45 +0100639 if (td->io_ops->close_file)
Jens Axboe6977bcd2008-03-01 15:55:36 +0100640 ret = td->io_ops->close_file(td, f);
Jens Axboe1020a132007-03-29 11:15:06 +0200641
Jens Axboe0ad920e2007-03-13 11:06:45 +0100642 td->nr_open_files--;
643 f->flags &= ~FIO_FILE_OPEN;
Jens Axboe6977bcd2008-03-01 15:55:36 +0100644 return ret;
Jens Axboe0ad920e2007-03-13 11:06:45 +0100645}
Jens Axboebbf6b542007-03-13 15:28:55 +0100646
Jens Axboeb2bd2bd2008-03-01 18:19:52 +0100647void lock_file(struct thread_data *td, struct fio_file *f)
648{
649}
650
651void unlock_file(struct fio_file *f)
652{
653}
654
Jens Axboebbf6b542007-03-13 15:28:55 +0100655static int recurse_dir(struct thread_data *td, const char *dirname)
656{
657 struct dirent *dir;
658 int ret = 0;
659 DIR *D;
660
661 D = opendir(dirname);
662 if (!D) {
Jens Axboe0ddb2702007-03-27 19:43:53 +0200663 char buf[FIO_VERROR_SIZE];
664
665 snprintf(buf, FIO_VERROR_SIZE - 1, "opendir(%s)", dirname);
666 td_verror(td, errno, buf);
Jens Axboebbf6b542007-03-13 15:28:55 +0100667 return 1;
668 }
669
670 while ((dir = readdir(D)) != NULL) {
671 char full_path[PATH_MAX];
672 struct stat sb;
673
Jens Axboee85b2b82007-03-14 09:39:06 +0100674 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
675 continue;
Jens Axboe96d32d52007-03-14 09:16:09 +0100676
Jens Axboebbf6b542007-03-13 15:28:55 +0100677 sprintf(full_path, "%s/%s", dirname, dir->d_name);
678
679 if (lstat(full_path, &sb) == -1) {
680 if (errno != ENOENT) {
681 td_verror(td, errno, "stat");
682 return 1;
683 }
684 }
685
686 if (S_ISREG(sb.st_mode)) {
687 add_file(td, full_path);
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100688 td->o.nr_files++;
Jens Axboebbf6b542007-03-13 15:28:55 +0100689 continue;
690 }
Jens Axboe0ddb2702007-03-27 19:43:53 +0200691 if (!S_ISDIR(sb.st_mode))
692 continue;
Jens Axboebbf6b542007-03-13 15:28:55 +0100693
Jens Axboebbf6b542007-03-13 15:28:55 +0100694 if ((ret = recurse_dir(td, full_path)) != 0)
695 break;
696 }
697
698 closedir(D);
699 return ret;
700}
701
702int add_dir_files(struct thread_data *td, const char *path)
703{
Jens Axboe0ddb2702007-03-27 19:43:53 +0200704 int ret = recurse_dir(td, path);
705
706 if (!ret)
707 log_info("fio: opendir added %d files\n", td->o.nr_files);
708
709 return ret;
Jens Axboebbf6b542007-03-13 15:28:55 +0100710}
Jens Axboecade3ef2007-03-23 15:29:45 +0100711
712void dup_files(struct thread_data *td, struct thread_data *org)
713{
714 struct fio_file *f;
715 unsigned int i;
716 size_t bytes;
717
718 if (!org->files)
719 return;
720
Jens Axboeb0fe4212008-03-01 18:22:27 +0100721 bytes = org->files_index * sizeof(f);
Jens Axboecade3ef2007-03-23 15:29:45 +0100722 td->files = malloc(bytes);
723 memcpy(td->files, org->files, bytes);
724
725 for_each_file(td, f, i) {
Jens Axboeb0fe4212008-03-01 18:22:27 +0100726 struct fio_file *__f;
727
Jens Axboef17c4392008-03-01 18:40:46 +0100728 __f = smalloc(sizeof(*__f));
Jens Axboeb0fe4212008-03-01 18:22:27 +0100729
Jens Axboecade3ef2007-03-23 15:29:45 +0100730 if (f->file_name)
Jens Axboef17c4392008-03-01 18:40:46 +0100731 __f->file_name = smalloc_strdup(f->file_name);
Jens Axboeb0fe4212008-03-01 18:22:27 +0100732
733 td->files[i] = __f;
Jens Axboecade3ef2007-03-23 15:29:45 +0100734 }
735}
Jens Axboef29b25a2007-07-23 08:56:43 +0200736
737/*
738 * Returns the index that matches the filename, or -1 if not there
739 */
740int get_fileno(struct thread_data *td, const char *fname)
741{
742 struct fio_file *f;
743 unsigned int i;
744
745 for_each_file(td, f, i)
746 if (!strcmp(f->file_name, fname))
747 return i;
748
749 return -1;
750}
751
752/*
753 * For log usage, where we add/open/close files automatically
754 */
755void free_release_files(struct thread_data *td)
756{
757 close_files(td);
758 td->files_index = 0;
759 td->nr_normal_files = 0;
760}