blob: 47ddacfcddef8083b97e6c7f487af14d79475ae4 [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 Axboeea443652007-03-29 14:52:13 +020036 if ((unlink_file || new_layout) && (f->flags & FIO_FILE_EXISTS)) {
Jens Axboe7bb48f82007-03-27 15:30:28 +020037 if (unlink(f->file_name) < 0) {
38 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
47 f->fd = open(f->file_name, flags, 0644);
Jens Axboe53cdc682006-10-18 11:50:58 +020048 if (f->fd < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +010049 td_verror(td, errno, "open");
Jens Axboe53cdc682006-10-18 11:50:58 +020050 return 1;
51 }
52
Jens Axboe7bb48f82007-03-27 15:30:28 +020053 if (ftruncate(f->fd, f->real_file_size) == -1) {
Jens Axboee1161c32007-02-22 19:36:48 +010054 td_verror(td, errno, "ftruncate");
Jens Axboe53cdc682006-10-18 11:50:58 +020055 goto err;
56 }
57
Jens Axboee8be2ad2007-03-29 10:31:51 +020058 if (!new_layout)
59 goto done;
60
Jens Axboe7bb48f82007-03-27 15:30:28 +020061 if (posix_fallocate(f->fd, 0, f->real_file_size) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +010062 td_verror(td, errno, "posix_fallocate");
Jens Axboe40f82982006-10-25 11:21:05 +020063 goto err;
64 }
65
Jens Axboe2dc1bbe2007-03-15 15:01:33 +010066 b = malloc(td->o.max_bs[DDIR_WRITE]);
67 memset(b, 0, td->o.max_bs[DDIR_WRITE]);
Jens Axboe53cdc682006-10-18 11:50:58 +020068
Jens Axboe7bb48f82007-03-27 15:30:28 +020069 left = f->real_file_size;
Jens Axboe53cdc682006-10-18 11:50:58 +020070 while (left && !td->terminate) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +010071 bs = td->o.max_bs[DDIR_WRITE];
Jens Axboe53cdc682006-10-18 11:50:58 +020072 if (bs > left)
73 bs = left;
74
75 r = write(f->fd, b, bs);
76
77 if (r == (int) bs) {
78 left -= bs;
79 continue;
80 } else {
81 if (r < 0)
Jens Axboee1161c32007-02-22 19:36:48 +010082 td_verror(td, errno, "write");
Jens Axboe53cdc682006-10-18 11:50:58 +020083 else
Jens Axboee1161c32007-02-22 19:36:48 +010084 td_verror(td, EIO, "write");
Jens Axboe53cdc682006-10-18 11:50:58 +020085
86 break;
87 }
88 }
89
90 if (td->terminate)
91 unlink(f->file_name);
Jens Axboe2dc1bbe2007-03-15 15:01:33 +010092 else if (td->o.create_fsync)
Jens Axboe53cdc682006-10-18 11:50:58 +020093 fsync(f->fd);
94
95 free(b);
Jens Axboe507a7022007-03-27 15:52:46 +020096done:
Jens Axboe53cdc682006-10-18 11:50:58 +020097 close(f->fd);
98 f->fd = -1;
99 return 0;
100err:
101 close(f->fd);
102 f->fd = -1;
103 return 1;
104}
105
Jens Axboe7bb48f82007-03-27 15:30:28 +0200106static unsigned long long get_rand_file_size(struct thread_data *td)
Jens Axboe9c60ce62007-03-15 09:14:47 +0100107{
Jens Axboe9c60ce62007-03-15 09:14:47 +0100108 unsigned long long ret;
109 long r;
110
Jens Axboe9c60ce62007-03-15 09:14:47 +0100111 r = os_random_long(&td->file_size_state);
ljzhang,Yaxin Hu,Jianchao Tangd11a5312007-07-27 15:54:10 +0200112 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 +0100113 ret -= (ret % td->o.rw_min_bs);
Jens Axboe9c60ce62007-03-15 09:14:47 +0100114 return ret;
115}
116
Jens Axboe53cdc682006-10-18 11:50:58 +0200117static int file_size(struct thread_data *td, struct fio_file *f)
118{
119 struct stat st;
120
Jens Axboe7bb48f82007-03-27 15:30:28 +0200121 if (fstat(f->fd, &st) == -1) {
122 td_verror(td, errno, "fstat");
123 return 1;
124 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200125
Jens Axboe7bb48f82007-03-27 15:30:28 +0200126 f->real_file_size = st.st_size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200127 return 0;
128}
129
130static int bdev_size(struct thread_data *td, struct fio_file *f)
131{
132 unsigned long long bytes;
133 int r;
134
135 r = blockdev_size(f->fd, &bytes);
136 if (r) {
Jens Axboee1161c32007-02-22 19:36:48 +0100137 td_verror(td, r, "blockdev_size");
Jens Axboe53cdc682006-10-18 11:50:58 +0200138 return 1;
139 }
140
141 f->real_file_size = bytes;
Jens Axboe53cdc682006-10-18 11:50:58 +0200142 return 0;
143}
144
145static int get_file_size(struct thread_data *td, struct fio_file *f)
146{
147 int ret = 0;
148
Jens Axboe409b3412007-03-28 09:57:01 +0200149 if (f->flags & FIO_SIZE_KNOWN)
150 return 0;
151
Jens Axboe7bb48f82007-03-27 15:30:28 +0200152 if (f->filetype == FIO_TYPE_FILE)
153 ret = file_size(td, f);
154 else if (f->filetype == FIO_TYPE_BD)
Jens Axboe53cdc682006-10-18 11:50:58 +0200155 ret = bdev_size(td, f);
156 else
157 f->real_file_size = -1;
158
159 if (ret)
160 return ret;
161
162 if (f->file_offset > f->real_file_size) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100163 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 +0200164 return 1;
165 }
166
Jens Axboe409b3412007-03-28 09:57:01 +0200167 f->flags |= FIO_SIZE_KNOWN;
Jens Axboe53cdc682006-10-18 11:50:58 +0200168 return 0;
169}
170
Jens Axboee5b401d2006-10-18 16:03:40 +0200171int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
172{
173 int ret = 0;
174
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100175 if (td->o.odirect)
Jens Axboeb5af8292007-03-08 12:43:13 +0100176 return 0;
177
Jens Axboee5b401d2006-10-18 16:03:40 +0200178 /*
179 * FIXME: add blockdev flushing too
180 */
Jens Axboeb5af8292007-03-08 12:43:13 +0100181 if (f->mmap)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200182 ret = madvise(f->mmap, f->io_size, MADV_DONTNEED);
Jens Axboe467d1b62007-03-15 14:11:10 +0100183 else if (f->filetype == FIO_TYPE_FILE)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200184 ret = fadvise(f->fd, f->file_offset, f->io_size, POSIX_FADV_DONTNEED);
Jens Axboe7e0e25c2007-03-27 12:49:56 +0200185 else if (f->filetype == FIO_TYPE_BD) {
Jens Axboeb5af8292007-03-08 12:43:13 +0100186 ret = blockdev_invalidate_cache(f->fd);
Jens Axboe7e0e25c2007-03-27 12:49:56 +0200187 if (ret < 0 && errno == EACCES && geteuid()) {
Jens Axboe7172cfe2007-07-23 14:36:00 +0200188 if (!root_warn) {
189 log_err("fio: only root may flush block devices. Cache flush bypassed!\n");
190 root_warn = 1;
191 }
Jens Axboe7e0e25c2007-03-27 12:49:56 +0200192 ret = 0;
193 }
Jens Axboeb5605e92007-04-16 20:42:33 +0200194 } else if (f->filetype == FIO_TYPE_CHAR || f->filetype == FIO_TYPE_PIPE)
Jens Axboee5b401d2006-10-18 16:03:40 +0200195 ret = 0;
196
197 if (ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100198 td_verror(td, errno, "invalidate_cache");
Jens Axboee5b401d2006-10-18 16:03:40 +0200199 return 1;
200 }
201
Jens Axboead2da602007-02-26 12:33:27 +0100202 return ret;
Jens Axboee5b401d2006-10-18 16:03:40 +0200203}
204
Jens Axboeb5af8292007-03-08 12:43:13 +0100205void generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
Jens Axboe53cdc682006-10-18 11:50:58 +0200206{
Jens Axboeb5af8292007-03-08 12:43:13 +0100207 close(f->fd);
208 f->fd = -1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200209}
210
Jens Axboeb5af8292007-03-08 12:43:13 +0100211int generic_open_file(struct thread_data *td, struct fio_file *f)
Jens Axboe53cdc682006-10-18 11:50:58 +0200212{
Jens Axboe66159822007-04-16 21:54:24 +0200213 int is_std = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200214 int flags = 0;
215
Jens Axboe66159822007-04-16 21:54:24 +0200216 if (!strcmp(f->file_name, "-")) {
217 if (td_rw(td)) {
218 log_err("fio: can't read/write to stdin/out\n");
219 return 1;
220 }
221 is_std = 1;
Jens Axboece98fa62007-04-17 13:27:32 +0200222
223 /*
224 * move output logging to stderr, if we are writing to stdout
225 */
226 if (td_write(td))
227 f_out = stderr;
Jens Axboe66159822007-04-16 21:54:24 +0200228 }
229
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100230 if (td->o.odirect)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100231 flags |= OS_O_DIRECT;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100232 if (td->o.sync_io)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100233 flags |= O_SYNC;
Jens Axboe7abf8332006-11-23 15:01:19 +0100234
Jens Axboe660a1cb2007-04-17 15:37:47 +0200235 if (td_write(td)) {
Jens Axboe4241ea82007-09-12 08:18:36 +0200236 assert(!read_only);
237
Jens Axboe2fd233b2007-03-02 08:44:25 +0100238 flags |= O_RDWR;
Jens Axboe53cdc682006-10-18 11:50:58 +0200239
Jens Axboeaf52b342007-03-13 10:07:47 +0100240 if (f->filetype == FIO_TYPE_FILE)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100241 flags |= O_CREAT;
Jens Axboe2fd233b2007-03-02 08:44:25 +0100242
Jens Axboe66159822007-04-16 21:54:24 +0200243 if (is_std)
244 f->fd = dup(STDOUT_FILENO);
245 else
246 f->fd = open(f->file_name, flags, 0600);
Jens Axboe2fd233b2007-03-02 08:44:25 +0100247 } else {
Jens Axboe4241ea82007-09-12 08:18:36 +0200248 if (f->filetype == FIO_TYPE_CHAR && !read_only)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100249 flags |= O_RDWR;
250 else
251 flags |= O_RDONLY;
252
Jens Axboe66159822007-04-16 21:54:24 +0200253 if (is_std)
254 f->fd = dup(STDIN_FILENO);
255 else
256 f->fd = open(f->file_name, flags);
Jens Axboe53cdc682006-10-18 11:50:58 +0200257 }
258
259 if (f->fd == -1) {
Jens Axboee4e33252007-03-21 13:07:54 +0100260 char buf[FIO_VERROR_SIZE];
Jens Axboee1161c32007-02-22 19:36:48 +0100261 int __e = errno;
262
Jens Axboee4e33252007-03-21 13:07:54 +0100263 snprintf(buf, sizeof(buf) - 1, "open(%s)", f->file_name);
264
265 td_verror(td, __e, buf);
Jens Axboe53cdc682006-10-18 11:50:58 +0200266 }
267
Jens Axboeb5af8292007-03-08 12:43:13 +0100268 if (get_file_size(td, f))
269 goto err;
270
Jens Axboe53cdc682006-10-18 11:50:58 +0200271 return 0;
Jens Axboeb5af8292007-03-08 12:43:13 +0100272err:
273 close(f->fd);
274 return 1;
275}
276
Jens Axboe21972cd2006-11-23 12:39:16 +0100277int open_files(struct thread_data *td)
278{
279 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100280 unsigned int i;
281 int err = 0;
Jens Axboe21972cd2006-11-23 12:39:16 +0100282
283 for_each_file(td, f, i) {
Jens Axboeb5af8292007-03-08 12:43:13 +0100284 err = td_io_open_file(td, f);
Jens Axboe9bf27b42007-03-27 19:49:31 +0200285 if (err) {
286 if (td->error == EMFILE) {
287 log_err("fio: limited open files to: %d\n", td->nr_open_files);
288 td->o.open_files = td->nr_open_files;
289 err = 0;
290 clear_error(td);
291 }
Jens Axboe21972cd2006-11-23 12:39:16 +0100292 break;
Jens Axboe9bf27b42007-03-27 19:49:31 +0200293 }
Jens Axboeb5af8292007-03-08 12:43:13 +0100294
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100295 if (td->o.open_files == td->nr_open_files)
Jens Axboeb5af8292007-03-08 12:43:13 +0100296 break;
Jens Axboe21972cd2006-11-23 12:39:16 +0100297 }
298
Jens Axboe7abf8332006-11-23 15:01:19 +0100299 if (!err)
300 return 0;
301
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100302 for_each_file(td, f, i)
Jens Axboeb5af8292007-03-08 12:43:13 +0100303 td_io_close_file(td, f);
Jens Axboe7abf8332006-11-23 15:01:19 +0100304
Jens Axboe21972cd2006-11-23 12:39:16 +0100305 return err;
306}
307
Jens Axboe7bb48f82007-03-27 15:30:28 +0200308/*
309 * open/close all files, so that ->real_file_size gets set
310 */
Jens Axboebab3fd52007-04-02 10:34:18 +0200311static int get_file_sizes(struct thread_data *td)
Jens Axboe53cdc682006-10-18 11:50:58 +0200312{
313 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100314 unsigned int i;
Jens Axboebab3fd52007-04-02 10:34:18 +0200315 int err = 0;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200316
317 for_each_file(td, f, i) {
Jens Axboebab3fd52007-04-02 10:34:18 +0200318 if (td->io_ops->open_file(td, f)) {
Jens Axboe40b44f42007-04-11 12:43:35 +0200319 if (td->error != ENOENT) {
320 log_err("%s\n", td->verror);
321 err = 1;
322 }
Jens Axboe541d66d2007-03-27 19:50:11 +0200323 clear_error(td);
Jens Axboe07eb79d2007-04-12 13:02:38 +0200324 } else {
325 if (td->io_ops->close_file)
326 td->io_ops->close_file(td, f);
327 }
Jens Axboe409b3412007-03-28 09:57:01 +0200328
329 if (f->real_file_size == -1ULL && td->o.size)
330 f->real_file_size = td->o.size / td->o.nr_files;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200331 }
Jens Axboebab3fd52007-04-02 10:34:18 +0200332
333 return err;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200334}
335
336/*
337 * Open the files and setup files sizes, creating files if necessary.
338 */
339int setup_files(struct thread_data *td)
340{
341 unsigned long long total_size, extend_size;
342 struct fio_file *f;
343 unsigned int i;
Jens Axboe000b0802007-03-27 15:56:10 +0200344 int err = 0, need_extend;
Jens Axboe53cdc682006-10-18 11:50:58 +0200345
346 /*
347 * if ioengine defines a setup() method, it's responsible for
Jens Axboe7bb48f82007-03-27 15:30:28 +0200348 * opening the files and setting f->real_file_size to indicate
349 * the valid range for that file.
Jens Axboe53cdc682006-10-18 11:50:58 +0200350 */
351 if (td->io_ops->setup)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200352 err = td->io_ops->setup(td);
353 else
Jens Axboebab3fd52007-04-02 10:34:18 +0200354 err = get_file_sizes(td);
Jens Axboe53cdc682006-10-18 11:50:58 +0200355
Jens Axboef1027062006-10-20 10:14:01 +0200356 if (err)
357 return err;
358
Jens Axboe0a7eb122006-10-20 10:36:42 +0200359 /*
Jens Axboe7bb48f82007-03-27 15:30:28 +0200360 * check sizes. if the files/devices do not exist and the size
361 * isn't passed to fio, abort.
Jens Axboe0a7eb122006-10-20 10:36:42 +0200362 */
Jens Axboe7bb48f82007-03-27 15:30:28 +0200363 total_size = 0;
364 for_each_file(td, f, i) {
365 if (f->real_file_size == -1ULL)
366 total_size = -1ULL;
367 else
368 total_size += f->real_file_size;
369 }
Jens Axboe0a7eb122006-10-20 10:36:42 +0200370
Jens Axboe7bb48f82007-03-27 15:30:28 +0200371 /*
372 * device/file sizes are zero and no size given, punt
373 */
Jens Axboe409b3412007-03-28 09:57:01 +0200374 if ((!total_size || total_size == -1ULL) && !td->o.size) {
Jens Axboe7bb48f82007-03-27 15:30:28 +0200375 log_err("%s: you need to specify size=\n", td->o.name);
Jens Axboee1161c32007-02-22 19:36:48 +0100376 td_verror(td, EINVAL, "total_file_size");
Jens Axboe53cdc682006-10-18 11:50:58 +0200377 return 1;
378 }
379
Jens Axboe7bb48f82007-03-27 15:30:28 +0200380 /*
381 * now file sizes are known, so we can set ->io_size. if size= is
382 * not given, ->io_size is just equal to ->real_file_size. if size
383 * is given, ->io_size is size / nr_files.
384 */
385 extend_size = total_size = 0;
386 need_extend = 0;
387 for_each_file(td, f, i) {
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200388 f->file_offset = td->o.start_offset;
389
Jens Axboe7bb48f82007-03-27 15:30:28 +0200390 if (!td->o.file_size_low) {
391 /*
392 * no file size range given, file size is equal to
393 * total size divided by number of files. if that is
394 * zero, set it to the real file size.
395 */
396 f->io_size = td->o.size / td->o.nr_files;
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200397 if (!f->io_size) {
398 if (f->file_offset > f->real_file_size)
399 goto err_offset;
400 f->io_size = f->real_file_size - f->file_offset;
401 }
Jens Axboe7bb48f82007-03-27 15:30:28 +0200402 } else if (f->real_file_size < td->o.file_size_low ||
403 f->real_file_size > td->o.file_size_high) {
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200404 if (f->file_offset > td->o.file_size_low)
405 goto err_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200406 /*
407 * file size given. if it's fixed, use that. if it's a
408 * range, generate a random size in-between.
409 */
410 if (td->o.file_size_low == td->o.file_size_high)
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200411 f->io_size = td->o.file_size_low - f->file_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200412 else
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200413 f->io_size = get_rand_file_size(td) - f->file_offset;
414 } else if (f->file_offset > f->real_file_size)
415 goto err_offset;
416 else
417 f->io_size = f->real_file_size - f->file_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200418
419 if (f->io_size == -1ULL)
420 total_size = -1ULL;
421 else
422 total_size += f->io_size;
423
424 if (f->filetype == FIO_TYPE_FILE &&
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200425 (f->io_size + f->file_offset) > f->real_file_size &&
Jens Axboe7bb48f82007-03-27 15:30:28 +0200426 !(td->io_ops->flags & FIO_DISKLESSIO)) {
427 need_extend++;
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200428 extend_size += (f->io_size + f->file_offset);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200429 f->flags |= FIO_FILE_EXTEND;
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200430 }
Jens Axboe7bb48f82007-03-27 15:30:28 +0200431 }
432
ljzhang,Yaxin Hu,Jianchao Tang22982902007-07-27 13:21:28 +0200433 if (!td->o.size || td->o.size > total_size)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200434 td->o.size = total_size;
435
436 /*
437 * See if we need to extend some files
438 */
439 if (need_extend) {
440 temp_stall_ts = 1;
Jens Axboef627d8a2007-08-24 10:24:09 +0200441 log_info("%s: Laying out IO file(s) (%u file(s) / %LuMiB)\n",
Jens Axboe7bb48f82007-03-27 15:30:28 +0200442 td->o.name, need_extend, extend_size >> 20);
443
444 for_each_file(td, f, i) {
445 if (!(f->flags & FIO_FILE_EXTEND))
446 continue;
447
Jens Axboe409b3412007-03-28 09:57:01 +0200448 assert(f->filetype == FIO_TYPE_FILE);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200449 f->flags &= ~FIO_FILE_EXTEND;
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200450 f->real_file_size = (f->io_size + f->file_offset);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200451 err = extend_file(td, f);
452 if (err)
453 break;
454 }
455 temp_stall_ts = 0;
456 }
457
458 if (err)
459 return err;
460
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100461 if (!td->o.zone_size)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200462 td->o.zone_size = td->o.size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200463
Jens Axboeea966f82007-09-03 10:09:37 +0200464 /*
465 * iolog already set the total io size, if we read back
466 * stored entries.
467 */
468 if (!td->o.read_iolog_file)
469 td->total_io_size = td->o.size * td->o.loops;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200470 return 0;
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200471err_offset:
472 log_err("%s: you need to specify valid offset=\n", td->o.name);
473 return 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200474}
475
Jens Axboe68727072007-03-15 20:49:25 +0100476int init_random_map(struct thread_data *td)
477{
478 int num_maps, blocks;
479 struct fio_file *f;
480 unsigned int i;
481
482 if (td->o.norandommap)
483 return 0;
484
485 for_each_file(td, f, i) {
486 blocks = (f->real_file_size + td->o.rw_min_bs - 1) / td->o.rw_min_bs;
487 num_maps = (blocks + BLOCKS_PER_MAP-1)/ BLOCKS_PER_MAP;
488 f->file_map = malloc(num_maps * sizeof(long));
489 if (!f->file_map) {
490 log_err("fio: failed allocating random map. If running a large number of jobs, try the 'norandommap' option\n");
491 return 1;
492 }
493 f->num_maps = num_maps;
494 memset(f->file_map, 0, num_maps * sizeof(long));
495 }
496
497 return 0;
498}
499
Jens Axboe53cdc682006-10-18 11:50:58 +0200500void close_files(struct thread_data *td)
501{
Jens Axboe0ab8db82006-10-18 17:16:23 +0200502 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100503 unsigned int i;
Jens Axboe53cdc682006-10-18 11:50:58 +0200504
Jens Axboe0ab8db82006-10-18 17:16:23 +0200505 for_each_file(td, f, i) {
Jens Axboea9b7b302007-04-19 16:31:16 +0200506 if (td->o.unlink && f->filetype == FIO_TYPE_FILE)
Jens Axboe132ad462006-11-23 12:23:12 +0100507 unlink(f->file_name);
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100508
Jens Axboeb5af8292007-03-08 12:43:13 +0100509 td_io_close_file(td, f);
Jens Axboeb3dc7f02007-03-09 12:52:15 +0100510
Jens Axboefa1da862007-03-23 15:20:54 +0100511 free(f->file_name);
512 f->file_name = NULL;
513
Jens Axboec3439812007-03-20 13:54:53 +0100514 if (f->file_map) {
Jens Axboeb3dc7f02007-03-09 12:52:15 +0100515 free(f->file_map);
Jens Axboec3439812007-03-20 13:54:53 +0100516 f->file_map = NULL;
517 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200518 }
Jens Axboeb4a6a592006-10-20 13:54:47 +0200519
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100520 td->o.filename = NULL;
Jens Axboecade3ef2007-03-23 15:29:45 +0100521 free(td->files);
Jens Axboeb4a6a592006-10-20 13:54:47 +0200522 td->files = NULL;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100523 td->o.nr_files = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200524}
Jens Axboeaf52b342007-03-13 10:07:47 +0100525
Jens Axboee3bab462007-03-13 11:22:09 +0100526static void get_file_type(struct fio_file *f)
Jens Axboeaf52b342007-03-13 10:07:47 +0100527{
528 struct stat sb;
529
Jens Axboe66159822007-04-16 21:54:24 +0200530 if (!strcmp(f->file_name, "-"))
531 f->filetype = FIO_TYPE_PIPE;
532 else
533 f->filetype = FIO_TYPE_FILE;
Jens Axboeaf52b342007-03-13 10:07:47 +0100534
Jens Axboee3bab462007-03-13 11:22:09 +0100535 if (!lstat(f->file_name, &sb)) {
Jens Axboeaf52b342007-03-13 10:07:47 +0100536 if (S_ISBLK(sb.st_mode))
537 f->filetype = FIO_TYPE_BD;
538 else if (S_ISCHR(sb.st_mode))
539 f->filetype = FIO_TYPE_CHAR;
Jens Axboeb5605e92007-04-16 20:42:33 +0200540 else if (S_ISFIFO(sb.st_mode))
541 f->filetype = FIO_TYPE_PIPE;
Jens Axboeaf52b342007-03-13 10:07:47 +0100542 }
543}
544
Jens Axboef29b25a2007-07-23 08:56:43 +0200545int add_file(struct thread_data *td, const char *fname)
Jens Axboeaf52b342007-03-13 10:07:47 +0100546{
Jens Axboe7b4e4fe2007-03-13 12:51:40 +0100547 int cur_files = td->files_index;
Jens Axboebd0ee742007-03-23 14:26:23 +0100548 char file_name[PATH_MAX];
Jens Axboeaf52b342007-03-13 10:07:47 +0100549 struct fio_file *f;
Jens Axboebd0ee742007-03-23 14:26:23 +0100550 int len = 0;
Jens Axboeaf52b342007-03-13 10:07:47 +0100551
552 td->files = realloc(td->files, (cur_files + 1) * sizeof(*f));
553
554 f = &td->files[cur_files];
555 memset(f, 0, sizeof(*f));
556 f->fd = -1;
Jens Axboebd0ee742007-03-23 14:26:23 +0100557
Jens Axboe07eb79d2007-04-12 13:02:38 +0200558 /*
559 * init function, io engine may not be loaded yet
560 */
561 if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO))
562 f->real_file_size = -1ULL;
563
Jens Axboebd0ee742007-03-23 14:26:23 +0100564 if (td->o.directory)
565 len = sprintf(file_name, "%s/", td->o.directory);
566
567 sprintf(file_name + len, "%s", fname);
568 f->file_name = strdup(file_name);
Jens Axboeaf52b342007-03-13 10:07:47 +0100569
Jens Axboee3bab462007-03-13 11:22:09 +0100570 get_file_type(f);
Jens Axboeaf52b342007-03-13 10:07:47 +0100571
Jens Axboe7b4e4fe2007-03-13 12:51:40 +0100572 td->files_index++;
Jens Axboe15494412007-03-13 11:52:47 +0100573 if (f->filetype == FIO_TYPE_FILE)
574 td->nr_normal_files++;
Jens Axboef29b25a2007-07-23 08:56:43 +0200575
576 return cur_files;
Jens Axboeaf52b342007-03-13 10:07:47 +0100577}
Jens Axboe0ad920e2007-03-13 11:06:45 +0100578
579void get_file(struct fio_file *f)
580{
Jens Axboe97af62c2007-05-22 11:12:13 +0200581 assert(f->flags & FIO_FILE_OPEN);
Jens Axboe0ad920e2007-03-13 11:06:45 +0100582 f->references++;
583}
584
585void put_file(struct thread_data *td, struct fio_file *f)
586{
587 if (!(f->flags & FIO_FILE_OPEN))
588 return;
589
590 assert(f->references);
591 if (--f->references)
592 return;
593
Jens Axboed424d4d2007-04-17 09:05:10 +0200594 if (should_fsync(td) && td->o.fsync_on_close)
Jens Axboeebb14152007-03-13 14:42:15 +0100595 fsync(f->fd);
596
Jens Axboe0ad920e2007-03-13 11:06:45 +0100597 if (td->io_ops->close_file)
598 td->io_ops->close_file(td, f);
Jens Axboe1020a132007-03-29 11:15:06 +0200599
Jens Axboe0ad920e2007-03-13 11:06:45 +0100600 td->nr_open_files--;
601 f->flags &= ~FIO_FILE_OPEN;
602}
Jens Axboebbf6b542007-03-13 15:28:55 +0100603
604static int recurse_dir(struct thread_data *td, const char *dirname)
605{
606 struct dirent *dir;
607 int ret = 0;
608 DIR *D;
609
610 D = opendir(dirname);
611 if (!D) {
Jens Axboe0ddb2702007-03-27 19:43:53 +0200612 char buf[FIO_VERROR_SIZE];
613
614 snprintf(buf, FIO_VERROR_SIZE - 1, "opendir(%s)", dirname);
615 td_verror(td, errno, buf);
Jens Axboebbf6b542007-03-13 15:28:55 +0100616 return 1;
617 }
618
619 while ((dir = readdir(D)) != NULL) {
620 char full_path[PATH_MAX];
621 struct stat sb;
622
Jens Axboee85b2b82007-03-14 09:39:06 +0100623 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
624 continue;
Jens Axboe96d32d52007-03-14 09:16:09 +0100625
Jens Axboebbf6b542007-03-13 15:28:55 +0100626 sprintf(full_path, "%s/%s", dirname, dir->d_name);
627
628 if (lstat(full_path, &sb) == -1) {
629 if (errno != ENOENT) {
630 td_verror(td, errno, "stat");
631 return 1;
632 }
633 }
634
635 if (S_ISREG(sb.st_mode)) {
636 add_file(td, full_path);
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100637 td->o.nr_files++;
Jens Axboebbf6b542007-03-13 15:28:55 +0100638 continue;
639 }
Jens Axboe0ddb2702007-03-27 19:43:53 +0200640 if (!S_ISDIR(sb.st_mode))
641 continue;
Jens Axboebbf6b542007-03-13 15:28:55 +0100642
Jens Axboebbf6b542007-03-13 15:28:55 +0100643 if ((ret = recurse_dir(td, full_path)) != 0)
644 break;
645 }
646
647 closedir(D);
648 return ret;
649}
650
651int add_dir_files(struct thread_data *td, const char *path)
652{
Jens Axboe0ddb2702007-03-27 19:43:53 +0200653 int ret = recurse_dir(td, path);
654
655 if (!ret)
656 log_info("fio: opendir added %d files\n", td->o.nr_files);
657
658 return ret;
Jens Axboebbf6b542007-03-13 15:28:55 +0100659}
Jens Axboecade3ef2007-03-23 15:29:45 +0100660
661void dup_files(struct thread_data *td, struct thread_data *org)
662{
663 struct fio_file *f;
664 unsigned int i;
665 size_t bytes;
666
667 if (!org->files)
668 return;
669
670 bytes = org->files_index * sizeof(*f);
671 td->files = malloc(bytes);
672 memcpy(td->files, org->files, bytes);
673
674 for_each_file(td, f, i) {
675 if (f->file_name)
676 f->file_name = strdup(f->file_name);
677 }
678}
Jens Axboef29b25a2007-07-23 08:56:43 +0200679
680/*
681 * Returns the index that matches the filename, or -1 if not there
682 */
683int get_fileno(struct thread_data *td, const char *fname)
684{
685 struct fio_file *f;
686 unsigned int i;
687
688 for_each_file(td, f, i)
689 if (!strcmp(f->file_name, fname))
690 return i;
691
692 return -1;
693}
694
695/*
696 * For log usage, where we add/open/close files automatically
697 */
698void free_release_files(struct thread_data *td)
699{
700 close_files(td);
701 td->files_index = 0;
702 td->nr_normal_files = 0;
703}