blob: 5134fdbb8af5a52e08e5ad8b60ea820e0caeab9f [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 Axboead923962007-10-11 21:41:41 +0200234 if (f->filetype != FIO_TYPE_FILE)
235 flags |= O_NOATIME;
Jens Axboe7abf8332006-11-23 15:01:19 +0100236
Jens Axboe660a1cb2007-04-17 15:37:47 +0200237 if (td_write(td)) {
Jens Axboe4241ea82007-09-12 08:18:36 +0200238 assert(!read_only);
239
Jens Axboe2fd233b2007-03-02 08:44:25 +0100240 flags |= O_RDWR;
Jens Axboe53cdc682006-10-18 11:50:58 +0200241
Jens Axboeaf52b342007-03-13 10:07:47 +0100242 if (f->filetype == FIO_TYPE_FILE)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100243 flags |= O_CREAT;
Jens Axboe2fd233b2007-03-02 08:44:25 +0100244
Jens Axboe66159822007-04-16 21:54:24 +0200245 if (is_std)
246 f->fd = dup(STDOUT_FILENO);
247 else
248 f->fd = open(f->file_name, flags, 0600);
Jens Axboe2fd233b2007-03-02 08:44:25 +0100249 } else {
Jens Axboe4241ea82007-09-12 08:18:36 +0200250 if (f->filetype == FIO_TYPE_CHAR && !read_only)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100251 flags |= O_RDWR;
252 else
253 flags |= O_RDONLY;
254
Jens Axboe66159822007-04-16 21:54:24 +0200255 if (is_std)
256 f->fd = dup(STDIN_FILENO);
257 else
258 f->fd = open(f->file_name, flags);
Jens Axboe53cdc682006-10-18 11:50:58 +0200259 }
260
261 if (f->fd == -1) {
Jens Axboee4e33252007-03-21 13:07:54 +0100262 char buf[FIO_VERROR_SIZE];
Jens Axboee1161c32007-02-22 19:36:48 +0100263 int __e = errno;
264
Jens Axboee4e33252007-03-21 13:07:54 +0100265 snprintf(buf, sizeof(buf) - 1, "open(%s)", f->file_name);
266
267 td_verror(td, __e, buf);
Jens Axboe53cdc682006-10-18 11:50:58 +0200268 }
269
Jens Axboeb5af8292007-03-08 12:43:13 +0100270 if (get_file_size(td, f))
271 goto err;
272
Jens Axboe53cdc682006-10-18 11:50:58 +0200273 return 0;
Jens Axboeb5af8292007-03-08 12:43:13 +0100274err:
275 close(f->fd);
276 return 1;
277}
278
Jens Axboe21972cd2006-11-23 12:39:16 +0100279int open_files(struct thread_data *td)
280{
281 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100282 unsigned int i;
283 int err = 0;
Jens Axboe21972cd2006-11-23 12:39:16 +0100284
285 for_each_file(td, f, i) {
Jens Axboeb5af8292007-03-08 12:43:13 +0100286 err = td_io_open_file(td, f);
Jens Axboe9bf27b42007-03-27 19:49:31 +0200287 if (err) {
288 if (td->error == EMFILE) {
289 log_err("fio: limited open files to: %d\n", td->nr_open_files);
290 td->o.open_files = td->nr_open_files;
291 err = 0;
292 clear_error(td);
293 }
Jens Axboe21972cd2006-11-23 12:39:16 +0100294 break;
Jens Axboe9bf27b42007-03-27 19:49:31 +0200295 }
Jens Axboeb5af8292007-03-08 12:43:13 +0100296
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100297 if (td->o.open_files == td->nr_open_files)
Jens Axboeb5af8292007-03-08 12:43:13 +0100298 break;
Jens Axboe21972cd2006-11-23 12:39:16 +0100299 }
300
Jens Axboe7abf8332006-11-23 15:01:19 +0100301 if (!err)
302 return 0;
303
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100304 for_each_file(td, f, i)
Jens Axboeb5af8292007-03-08 12:43:13 +0100305 td_io_close_file(td, f);
Jens Axboe7abf8332006-11-23 15:01:19 +0100306
Jens Axboe21972cd2006-11-23 12:39:16 +0100307 return err;
308}
309
Jens Axboe7bb48f82007-03-27 15:30:28 +0200310/*
311 * open/close all files, so that ->real_file_size gets set
312 */
Jens Axboebab3fd52007-04-02 10:34:18 +0200313static int get_file_sizes(struct thread_data *td)
Jens Axboe53cdc682006-10-18 11:50:58 +0200314{
315 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100316 unsigned int i;
Jens Axboebab3fd52007-04-02 10:34:18 +0200317 int err = 0;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200318
319 for_each_file(td, f, i) {
Jens Axboebab3fd52007-04-02 10:34:18 +0200320 if (td->io_ops->open_file(td, f)) {
Jens Axboe40b44f42007-04-11 12:43:35 +0200321 if (td->error != ENOENT) {
322 log_err("%s\n", td->verror);
323 err = 1;
324 }
Jens Axboe541d66d2007-03-27 19:50:11 +0200325 clear_error(td);
Jens Axboe07eb79d2007-04-12 13:02:38 +0200326 } else {
327 if (td->io_ops->close_file)
328 td->io_ops->close_file(td, f);
329 }
Jens Axboe409b3412007-03-28 09:57:01 +0200330
331 if (f->real_file_size == -1ULL && td->o.size)
332 f->real_file_size = td->o.size / td->o.nr_files;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200333 }
Jens Axboebab3fd52007-04-02 10:34:18 +0200334
335 return err;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200336}
337
338/*
339 * Open the files and setup files sizes, creating files if necessary.
340 */
341int setup_files(struct thread_data *td)
342{
343 unsigned long long total_size, extend_size;
344 struct fio_file *f;
345 unsigned int i;
Jens Axboe000b0802007-03-27 15:56:10 +0200346 int err = 0, need_extend;
Jens Axboe53cdc682006-10-18 11:50:58 +0200347
348 /*
349 * if ioengine defines a setup() method, it's responsible for
Jens Axboe7bb48f82007-03-27 15:30:28 +0200350 * opening the files and setting f->real_file_size to indicate
351 * the valid range for that file.
Jens Axboe53cdc682006-10-18 11:50:58 +0200352 */
353 if (td->io_ops->setup)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200354 err = td->io_ops->setup(td);
355 else
Jens Axboebab3fd52007-04-02 10:34:18 +0200356 err = get_file_sizes(td);
Jens Axboe53cdc682006-10-18 11:50:58 +0200357
Jens Axboef1027062006-10-20 10:14:01 +0200358 if (err)
359 return err;
360
Jens Axboe0a7eb122006-10-20 10:36:42 +0200361 /*
Jens Axboe7bb48f82007-03-27 15:30:28 +0200362 * check sizes. if the files/devices do not exist and the size
363 * isn't passed to fio, abort.
Jens Axboe0a7eb122006-10-20 10:36:42 +0200364 */
Jens Axboe7bb48f82007-03-27 15:30:28 +0200365 total_size = 0;
366 for_each_file(td, f, i) {
367 if (f->real_file_size == -1ULL)
368 total_size = -1ULL;
369 else
370 total_size += f->real_file_size;
371 }
Jens Axboe0a7eb122006-10-20 10:36:42 +0200372
Jens Axboe7bb48f82007-03-27 15:30:28 +0200373 /*
374 * device/file sizes are zero and no size given, punt
375 */
Jens Axboe1f809d12007-10-25 18:34:02 +0200376 if ((!total_size || total_size == -1ULL) && !td->o.size &&
377 !(td->io_ops->flags & FIO_NOIO)) {
Jens Axboe7bb48f82007-03-27 15:30:28 +0200378 log_err("%s: you need to specify size=\n", td->o.name);
Jens Axboee1161c32007-02-22 19:36:48 +0100379 td_verror(td, EINVAL, "total_file_size");
Jens Axboe53cdc682006-10-18 11:50:58 +0200380 return 1;
381 }
382
Jens Axboe7bb48f82007-03-27 15:30:28 +0200383 /*
384 * now file sizes are known, so we can set ->io_size. if size= is
385 * not given, ->io_size is just equal to ->real_file_size. if size
386 * is given, ->io_size is size / nr_files.
387 */
388 extend_size = total_size = 0;
389 need_extend = 0;
390 for_each_file(td, f, i) {
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200391 f->file_offset = td->o.start_offset;
392
Jens Axboe7bb48f82007-03-27 15:30:28 +0200393 if (!td->o.file_size_low) {
394 /*
395 * no file size range given, file size is equal to
396 * total size divided by number of files. if that is
397 * zero, set it to the real file size.
398 */
399 f->io_size = td->o.size / td->o.nr_files;
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200400 if (!f->io_size) {
401 if (f->file_offset > f->real_file_size)
402 goto err_offset;
403 f->io_size = f->real_file_size - f->file_offset;
404 }
Jens Axboe7bb48f82007-03-27 15:30:28 +0200405 } else if (f->real_file_size < td->o.file_size_low ||
406 f->real_file_size > td->o.file_size_high) {
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200407 if (f->file_offset > td->o.file_size_low)
408 goto err_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200409 /*
410 * file size given. if it's fixed, use that. if it's a
411 * range, generate a random size in-between.
412 */
413 if (td->o.file_size_low == td->o.file_size_high)
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200414 f->io_size = td->o.file_size_low - f->file_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200415 else
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200416 f->io_size = get_rand_file_size(td) - f->file_offset;
417 } else if (f->file_offset > f->real_file_size)
418 goto err_offset;
419 else
420 f->io_size = f->real_file_size - f->file_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200421
422 if (f->io_size == -1ULL)
423 total_size = -1ULL;
424 else
425 total_size += f->io_size;
426
427 if (f->filetype == FIO_TYPE_FILE &&
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200428 (f->io_size + f->file_offset) > f->real_file_size &&
Jens Axboe7bb48f82007-03-27 15:30:28 +0200429 !(td->io_ops->flags & FIO_DISKLESSIO)) {
430 need_extend++;
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200431 extend_size += (f->io_size + f->file_offset);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200432 f->flags |= FIO_FILE_EXTEND;
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200433 }
Jens Axboe7bb48f82007-03-27 15:30:28 +0200434 }
435
ljzhang,Yaxin Hu,Jianchao Tang22982902007-07-27 13:21:28 +0200436 if (!td->o.size || td->o.size > total_size)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200437 td->o.size = total_size;
438
439 /*
440 * See if we need to extend some files
441 */
442 if (need_extend) {
443 temp_stall_ts = 1;
Jens Axboef627d8a2007-08-24 10:24:09 +0200444 log_info("%s: Laying out IO file(s) (%u file(s) / %LuMiB)\n",
Jens Axboe7bb48f82007-03-27 15:30:28 +0200445 td->o.name, need_extend, extend_size >> 20);
446
447 for_each_file(td, f, i) {
448 if (!(f->flags & FIO_FILE_EXTEND))
449 continue;
450
Jens Axboe409b3412007-03-28 09:57:01 +0200451 assert(f->filetype == FIO_TYPE_FILE);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200452 f->flags &= ~FIO_FILE_EXTEND;
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200453 f->real_file_size = (f->io_size + f->file_offset);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200454 err = extend_file(td, f);
455 if (err)
456 break;
457 }
458 temp_stall_ts = 0;
459 }
460
461 if (err)
462 return err;
463
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100464 if (!td->o.zone_size)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200465 td->o.zone_size = td->o.size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200466
Jens Axboeea966f82007-09-03 10:09:37 +0200467 /*
468 * iolog already set the total io size, if we read back
469 * stored entries.
470 */
471 if (!td->o.read_iolog_file)
472 td->total_io_size = td->o.size * td->o.loops;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200473 return 0;
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200474err_offset:
475 log_err("%s: you need to specify valid offset=\n", td->o.name);
476 return 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200477}
478
Jens Axboe68727072007-03-15 20:49:25 +0100479int init_random_map(struct thread_data *td)
480{
481 int num_maps, blocks;
482 struct fio_file *f;
483 unsigned int i;
484
485 if (td->o.norandommap)
486 return 0;
487
488 for_each_file(td, f, i) {
489 blocks = (f->real_file_size + td->o.rw_min_bs - 1) / td->o.rw_min_bs;
490 num_maps = (blocks + BLOCKS_PER_MAP-1)/ BLOCKS_PER_MAP;
491 f->file_map = malloc(num_maps * sizeof(long));
492 if (!f->file_map) {
493 log_err("fio: failed allocating random map. If running a large number of jobs, try the 'norandommap' option\n");
494 return 1;
495 }
496 f->num_maps = num_maps;
497 memset(f->file_map, 0, num_maps * sizeof(long));
498 }
499
500 return 0;
501}
502
Jens Axboe53cdc682006-10-18 11:50:58 +0200503void close_files(struct thread_data *td)
504{
Jens Axboe0ab8db82006-10-18 17:16:23 +0200505 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100506 unsigned int i;
Jens Axboe53cdc682006-10-18 11:50:58 +0200507
Jens Axboe0ab8db82006-10-18 17:16:23 +0200508 for_each_file(td, f, i) {
Jens Axboea9b7b302007-04-19 16:31:16 +0200509 if (td->o.unlink && f->filetype == FIO_TYPE_FILE)
Jens Axboe132ad462006-11-23 12:23:12 +0100510 unlink(f->file_name);
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100511
Jens Axboeb5af8292007-03-08 12:43:13 +0100512 td_io_close_file(td, f);
Jens Axboeb3dc7f02007-03-09 12:52:15 +0100513
Jens Axboefa1da862007-03-23 15:20:54 +0100514 free(f->file_name);
515 f->file_name = NULL;
516
Jens Axboec3439812007-03-20 13:54:53 +0100517 if (f->file_map) {
Jens Axboeb3dc7f02007-03-09 12:52:15 +0100518 free(f->file_map);
Jens Axboec3439812007-03-20 13:54:53 +0100519 f->file_map = NULL;
520 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200521 }
Jens Axboeb4a6a592006-10-20 13:54:47 +0200522
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100523 td->o.filename = NULL;
Jens Axboecade3ef2007-03-23 15:29:45 +0100524 free(td->files);
Jens Axboeb4a6a592006-10-20 13:54:47 +0200525 td->files = NULL;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100526 td->o.nr_files = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200527}
Jens Axboeaf52b342007-03-13 10:07:47 +0100528
Jens Axboee3bab462007-03-13 11:22:09 +0100529static void get_file_type(struct fio_file *f)
Jens Axboeaf52b342007-03-13 10:07:47 +0100530{
531 struct stat sb;
532
Jens Axboe66159822007-04-16 21:54:24 +0200533 if (!strcmp(f->file_name, "-"))
534 f->filetype = FIO_TYPE_PIPE;
535 else
536 f->filetype = FIO_TYPE_FILE;
Jens Axboeaf52b342007-03-13 10:07:47 +0100537
Jens Axboee3bab462007-03-13 11:22:09 +0100538 if (!lstat(f->file_name, &sb)) {
Jens Axboeaf52b342007-03-13 10:07:47 +0100539 if (S_ISBLK(sb.st_mode))
540 f->filetype = FIO_TYPE_BD;
541 else if (S_ISCHR(sb.st_mode))
542 f->filetype = FIO_TYPE_CHAR;
Jens Axboeb5605e92007-04-16 20:42:33 +0200543 else if (S_ISFIFO(sb.st_mode))
544 f->filetype = FIO_TYPE_PIPE;
Jens Axboeaf52b342007-03-13 10:07:47 +0100545 }
546}
547
Jens Axboef29b25a2007-07-23 08:56:43 +0200548int add_file(struct thread_data *td, const char *fname)
Jens Axboeaf52b342007-03-13 10:07:47 +0100549{
Jens Axboe7b4e4fe2007-03-13 12:51:40 +0100550 int cur_files = td->files_index;
Jens Axboebd0ee742007-03-23 14:26:23 +0100551 char file_name[PATH_MAX];
Jens Axboeaf52b342007-03-13 10:07:47 +0100552 struct fio_file *f;
Jens Axboebd0ee742007-03-23 14:26:23 +0100553 int len = 0;
Jens Axboeaf52b342007-03-13 10:07:47 +0100554
555 td->files = realloc(td->files, (cur_files + 1) * sizeof(*f));
556
557 f = &td->files[cur_files];
558 memset(f, 0, sizeof(*f));
559 f->fd = -1;
Jens Axboebd0ee742007-03-23 14:26:23 +0100560
Jens Axboe07eb79d2007-04-12 13:02:38 +0200561 /*
562 * init function, io engine may not be loaded yet
563 */
564 if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO))
565 f->real_file_size = -1ULL;
566
Jens Axboebd0ee742007-03-23 14:26:23 +0100567 if (td->o.directory)
568 len = sprintf(file_name, "%s/", td->o.directory);
569
570 sprintf(file_name + len, "%s", fname);
571 f->file_name = strdup(file_name);
Jens Axboeaf52b342007-03-13 10:07:47 +0100572
Jens Axboee3bab462007-03-13 11:22:09 +0100573 get_file_type(f);
Jens Axboeaf52b342007-03-13 10:07:47 +0100574
Jens Axboe7b4e4fe2007-03-13 12:51:40 +0100575 td->files_index++;
Jens Axboe15494412007-03-13 11:52:47 +0100576 if (f->filetype == FIO_TYPE_FILE)
577 td->nr_normal_files++;
Jens Axboef29b25a2007-07-23 08:56:43 +0200578
579 return cur_files;
Jens Axboeaf52b342007-03-13 10:07:47 +0100580}
Jens Axboe0ad920e2007-03-13 11:06:45 +0100581
582void get_file(struct fio_file *f)
583{
Jens Axboe97af62c2007-05-22 11:12:13 +0200584 assert(f->flags & FIO_FILE_OPEN);
Jens Axboe0ad920e2007-03-13 11:06:45 +0100585 f->references++;
586}
587
588void put_file(struct thread_data *td, struct fio_file *f)
589{
590 if (!(f->flags & FIO_FILE_OPEN))
591 return;
592
593 assert(f->references);
594 if (--f->references)
595 return;
596
Jens Axboed424d4d2007-04-17 09:05:10 +0200597 if (should_fsync(td) && td->o.fsync_on_close)
Jens Axboeebb14152007-03-13 14:42:15 +0100598 fsync(f->fd);
599
Jens Axboe0ad920e2007-03-13 11:06:45 +0100600 if (td->io_ops->close_file)
601 td->io_ops->close_file(td, f);
Jens Axboe1020a132007-03-29 11:15:06 +0200602
Jens Axboe0ad920e2007-03-13 11:06:45 +0100603 td->nr_open_files--;
604 f->flags &= ~FIO_FILE_OPEN;
605}
Jens Axboebbf6b542007-03-13 15:28:55 +0100606
607static int recurse_dir(struct thread_data *td, const char *dirname)
608{
609 struct dirent *dir;
610 int ret = 0;
611 DIR *D;
612
613 D = opendir(dirname);
614 if (!D) {
Jens Axboe0ddb2702007-03-27 19:43:53 +0200615 char buf[FIO_VERROR_SIZE];
616
617 snprintf(buf, FIO_VERROR_SIZE - 1, "opendir(%s)", dirname);
618 td_verror(td, errno, buf);
Jens Axboebbf6b542007-03-13 15:28:55 +0100619 return 1;
620 }
621
622 while ((dir = readdir(D)) != NULL) {
623 char full_path[PATH_MAX];
624 struct stat sb;
625
Jens Axboee85b2b82007-03-14 09:39:06 +0100626 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
627 continue;
Jens Axboe96d32d52007-03-14 09:16:09 +0100628
Jens Axboebbf6b542007-03-13 15:28:55 +0100629 sprintf(full_path, "%s/%s", dirname, dir->d_name);
630
631 if (lstat(full_path, &sb) == -1) {
632 if (errno != ENOENT) {
633 td_verror(td, errno, "stat");
634 return 1;
635 }
636 }
637
638 if (S_ISREG(sb.st_mode)) {
639 add_file(td, full_path);
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100640 td->o.nr_files++;
Jens Axboebbf6b542007-03-13 15:28:55 +0100641 continue;
642 }
Jens Axboe0ddb2702007-03-27 19:43:53 +0200643 if (!S_ISDIR(sb.st_mode))
644 continue;
Jens Axboebbf6b542007-03-13 15:28:55 +0100645
Jens Axboebbf6b542007-03-13 15:28:55 +0100646 if ((ret = recurse_dir(td, full_path)) != 0)
647 break;
648 }
649
650 closedir(D);
651 return ret;
652}
653
654int add_dir_files(struct thread_data *td, const char *path)
655{
Jens Axboe0ddb2702007-03-27 19:43:53 +0200656 int ret = recurse_dir(td, path);
657
658 if (!ret)
659 log_info("fio: opendir added %d files\n", td->o.nr_files);
660
661 return ret;
Jens Axboebbf6b542007-03-13 15:28:55 +0100662}
Jens Axboecade3ef2007-03-23 15:29:45 +0100663
664void dup_files(struct thread_data *td, struct thread_data *org)
665{
666 struct fio_file *f;
667 unsigned int i;
668 size_t bytes;
669
670 if (!org->files)
671 return;
672
673 bytes = org->files_index * sizeof(*f);
674 td->files = malloc(bytes);
675 memcpy(td->files, org->files, bytes);
676
677 for_each_file(td, f, i) {
678 if (f->file_name)
679 f->file_name = strdup(f->file_name);
680 }
681}
Jens Axboef29b25a2007-07-23 08:56:43 +0200682
683/*
684 * Returns the index that matches the filename, or -1 if not there
685 */
686int get_fileno(struct thread_data *td, const char *fname)
687{
688 struct fio_file *f;
689 unsigned int i;
690
691 for_each_file(td, f, i)
692 if (!strcmp(f->file_name, fname))
693 return i;
694
695 return -1;
696}
697
698/*
699 * For log usage, where we add/open/close files automatically
700 */
701void free_release_files(struct thread_data *td)
702{
703 close_files(td);
704 td->files_index = 0;
705 td->nr_normal_files = 0;
706}