blob: 210cd2ff7ed437a1f19414a82e20600486cdfbc5 [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
Aaron Carroll056f3452007-11-26 09:08:53 +0100237open_again:
Jens Axboe660a1cb2007-04-17 15:37:47 +0200238 if (td_write(td)) {
Jens Axboe4241ea82007-09-12 08:18:36 +0200239 assert(!read_only);
240
Jens Axboe2fd233b2007-03-02 08:44:25 +0100241 flags |= O_RDWR;
Jens Axboe53cdc682006-10-18 11:50:58 +0200242
Jens Axboeaf52b342007-03-13 10:07:47 +0100243 if (f->filetype == FIO_TYPE_FILE)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100244 flags |= O_CREAT;
Jens Axboe2fd233b2007-03-02 08:44:25 +0100245
Jens Axboe66159822007-04-16 21:54:24 +0200246 if (is_std)
247 f->fd = dup(STDOUT_FILENO);
248 else
249 f->fd = open(f->file_name, flags, 0600);
Jens Axboe2fd233b2007-03-02 08:44:25 +0100250 } else {
Jens Axboe4241ea82007-09-12 08:18:36 +0200251 if (f->filetype == FIO_TYPE_CHAR && !read_only)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100252 flags |= O_RDWR;
253 else
254 flags |= O_RDONLY;
255
Jens Axboe66159822007-04-16 21:54:24 +0200256 if (is_std)
257 f->fd = dup(STDIN_FILENO);
258 else
259 f->fd = open(f->file_name, flags);
Jens Axboe53cdc682006-10-18 11:50:58 +0200260 }
261
262 if (f->fd == -1) {
Jens Axboee4e33252007-03-21 13:07:54 +0100263 char buf[FIO_VERROR_SIZE];
Jens Axboee1161c32007-02-22 19:36:48 +0100264 int __e = errno;
265
Aaron Carroll056f3452007-11-26 09:08:53 +0100266 if (errno == EPERM && (flags & O_NOATIME)) {
267 flags &= ~O_NOATIME;
268 goto open_again;
269 }
270
Jens Axboee4e33252007-03-21 13:07:54 +0100271 snprintf(buf, sizeof(buf) - 1, "open(%s)", f->file_name);
272
273 td_verror(td, __e, buf);
Jens Axboe53cdc682006-10-18 11:50:58 +0200274 }
275
Jens Axboeb5af8292007-03-08 12:43:13 +0100276 if (get_file_size(td, f))
277 goto err;
278
Jens Axboe53cdc682006-10-18 11:50:58 +0200279 return 0;
Jens Axboeb5af8292007-03-08 12:43:13 +0100280err:
281 close(f->fd);
282 return 1;
283}
284
Jens Axboe21972cd2006-11-23 12:39:16 +0100285int open_files(struct thread_data *td)
286{
287 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100288 unsigned int i;
289 int err = 0;
Jens Axboe21972cd2006-11-23 12:39:16 +0100290
291 for_each_file(td, f, i) {
Jens Axboeb5af8292007-03-08 12:43:13 +0100292 err = td_io_open_file(td, f);
Jens Axboe9bf27b42007-03-27 19:49:31 +0200293 if (err) {
294 if (td->error == EMFILE) {
295 log_err("fio: limited open files to: %d\n", td->nr_open_files);
296 td->o.open_files = td->nr_open_files;
297 err = 0;
298 clear_error(td);
299 }
Jens Axboe21972cd2006-11-23 12:39:16 +0100300 break;
Jens Axboe9bf27b42007-03-27 19:49:31 +0200301 }
Jens Axboeb5af8292007-03-08 12:43:13 +0100302
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100303 if (td->o.open_files == td->nr_open_files)
Jens Axboeb5af8292007-03-08 12:43:13 +0100304 break;
Jens Axboe21972cd2006-11-23 12:39:16 +0100305 }
306
Jens Axboe7abf8332006-11-23 15:01:19 +0100307 if (!err)
308 return 0;
309
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100310 for_each_file(td, f, i)
Jens Axboeb5af8292007-03-08 12:43:13 +0100311 td_io_close_file(td, f);
Jens Axboe7abf8332006-11-23 15:01:19 +0100312
Jens Axboe21972cd2006-11-23 12:39:16 +0100313 return err;
314}
315
Jens Axboe7bb48f82007-03-27 15:30:28 +0200316/*
317 * open/close all files, so that ->real_file_size gets set
318 */
Jens Axboebab3fd52007-04-02 10:34:18 +0200319static int get_file_sizes(struct thread_data *td)
Jens Axboe53cdc682006-10-18 11:50:58 +0200320{
321 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100322 unsigned int i;
Jens Axboebab3fd52007-04-02 10:34:18 +0200323 int err = 0;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200324
325 for_each_file(td, f, i) {
Jens Axboebab3fd52007-04-02 10:34:18 +0200326 if (td->io_ops->open_file(td, f)) {
Jens Axboe40b44f42007-04-11 12:43:35 +0200327 if (td->error != ENOENT) {
328 log_err("%s\n", td->verror);
329 err = 1;
330 }
Jens Axboe541d66d2007-03-27 19:50:11 +0200331 clear_error(td);
Jens Axboe07eb79d2007-04-12 13:02:38 +0200332 } else {
333 if (td->io_ops->close_file)
334 td->io_ops->close_file(td, f);
335 }
Jens Axboe409b3412007-03-28 09:57:01 +0200336
337 if (f->real_file_size == -1ULL && td->o.size)
338 f->real_file_size = td->o.size / td->o.nr_files;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200339 }
Jens Axboebab3fd52007-04-02 10:34:18 +0200340
341 return err;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200342}
343
344/*
345 * Open the files and setup files sizes, creating files if necessary.
346 */
347int setup_files(struct thread_data *td)
348{
349 unsigned long long total_size, extend_size;
350 struct fio_file *f;
351 unsigned int i;
Jens Axboe000b0802007-03-27 15:56:10 +0200352 int err = 0, need_extend;
Jens Axboe53cdc682006-10-18 11:50:58 +0200353
354 /*
355 * if ioengine defines a setup() method, it's responsible for
Jens Axboe7bb48f82007-03-27 15:30:28 +0200356 * opening the files and setting f->real_file_size to indicate
357 * the valid range for that file.
Jens Axboe53cdc682006-10-18 11:50:58 +0200358 */
359 if (td->io_ops->setup)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200360 err = td->io_ops->setup(td);
361 else
Jens Axboebab3fd52007-04-02 10:34:18 +0200362 err = get_file_sizes(td);
Jens Axboe53cdc682006-10-18 11:50:58 +0200363
Jens Axboef1027062006-10-20 10:14:01 +0200364 if (err)
365 return err;
366
Jens Axboe0a7eb122006-10-20 10:36:42 +0200367 /*
Jens Axboe7bb48f82007-03-27 15:30:28 +0200368 * check sizes. if the files/devices do not exist and the size
369 * isn't passed to fio, abort.
Jens Axboe0a7eb122006-10-20 10:36:42 +0200370 */
Jens Axboe7bb48f82007-03-27 15:30:28 +0200371 total_size = 0;
372 for_each_file(td, f, i) {
373 if (f->real_file_size == -1ULL)
374 total_size = -1ULL;
375 else
376 total_size += f->real_file_size;
377 }
Jens Axboe0a7eb122006-10-20 10:36:42 +0200378
Jens Axboe7bb48f82007-03-27 15:30:28 +0200379 /*
380 * device/file sizes are zero and no size given, punt
381 */
Jens Axboe1f809d12007-10-25 18:34:02 +0200382 if ((!total_size || total_size == -1ULL) && !td->o.size &&
Shawn Lewisaa31f1f2008-01-11 09:45:11 +0100383 !(td->io_ops->flags & FIO_NOIO) && !td->o.fill_device) {
Jens Axboe7bb48f82007-03-27 15:30:28 +0200384 log_err("%s: you need to specify size=\n", td->o.name);
Jens Axboee1161c32007-02-22 19:36:48 +0100385 td_verror(td, EINVAL, "total_file_size");
Jens Axboe53cdc682006-10-18 11:50:58 +0200386 return 1;
387 }
388
Jens Axboe7bb48f82007-03-27 15:30:28 +0200389 /*
390 * now file sizes are known, so we can set ->io_size. if size= is
391 * not given, ->io_size is just equal to ->real_file_size. if size
392 * is given, ->io_size is size / nr_files.
393 */
394 extend_size = total_size = 0;
395 need_extend = 0;
396 for_each_file(td, f, i) {
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200397 f->file_offset = td->o.start_offset;
398
Jens Axboe7bb48f82007-03-27 15:30:28 +0200399 if (!td->o.file_size_low) {
400 /*
401 * no file size range given, file size is equal to
402 * total size divided by number of files. if that is
403 * zero, set it to the real file size.
404 */
405 f->io_size = td->o.size / td->o.nr_files;
Gurudas Pai8f9cc4a2007-12-21 10:50:33 +0100406 if ((!f->io_size || f->io_size > f->real_file_size) &&
407 f->real_file_size) {
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200408 if (f->file_offset > f->real_file_size)
409 goto err_offset;
410 f->io_size = f->real_file_size - f->file_offset;
411 }
Jens Axboe7bb48f82007-03-27 15:30:28 +0200412 } else if (f->real_file_size < td->o.file_size_low ||
413 f->real_file_size > td->o.file_size_high) {
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200414 if (f->file_offset > td->o.file_size_low)
415 goto err_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200416 /*
417 * file size given. if it's fixed, use that. if it's a
418 * range, generate a random size in-between.
419 */
420 if (td->o.file_size_low == td->o.file_size_high)
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200421 f->io_size = td->o.file_size_low - f->file_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200422 else
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200423 f->io_size = get_rand_file_size(td) - f->file_offset;
424 } else if (f->file_offset > f->real_file_size)
425 goto err_offset;
426 else
427 f->io_size = f->real_file_size - f->file_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200428
429 if (f->io_size == -1ULL)
430 total_size = -1ULL;
431 else
432 total_size += f->io_size;
433
434 if (f->filetype == FIO_TYPE_FILE &&
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200435 (f->io_size + f->file_offset) > f->real_file_size &&
Jens Axboe7bb48f82007-03-27 15:30:28 +0200436 !(td->io_ops->flags & FIO_DISKLESSIO)) {
437 need_extend++;
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200438 extend_size += (f->io_size + f->file_offset);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200439 f->flags |= FIO_FILE_EXTEND;
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200440 }
Jens Axboe7bb48f82007-03-27 15:30:28 +0200441 }
442
ljzhang,Yaxin Hu,Jianchao Tang22982902007-07-27 13:21:28 +0200443 if (!td->o.size || td->o.size > total_size)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200444 td->o.size = total_size;
445
446 /*
447 * See if we need to extend some files
448 */
449 if (need_extend) {
450 temp_stall_ts = 1;
Jens Axboef627d8a2007-08-24 10:24:09 +0200451 log_info("%s: Laying out IO file(s) (%u file(s) / %LuMiB)\n",
Jens Axboe7bb48f82007-03-27 15:30:28 +0200452 td->o.name, need_extend, extend_size >> 20);
453
454 for_each_file(td, f, i) {
455 if (!(f->flags & FIO_FILE_EXTEND))
456 continue;
457
Jens Axboe409b3412007-03-28 09:57:01 +0200458 assert(f->filetype == FIO_TYPE_FILE);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200459 f->flags &= ~FIO_FILE_EXTEND;
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200460 f->real_file_size = (f->io_size + f->file_offset);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200461 err = extend_file(td, f);
462 if (err)
463 break;
464 }
465 temp_stall_ts = 0;
466 }
467
468 if (err)
469 return err;
470
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100471 if (!td->o.zone_size)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200472 td->o.zone_size = td->o.size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200473
Jens Axboeea966f82007-09-03 10:09:37 +0200474 /*
475 * iolog already set the total io size, if we read back
476 * stored entries.
477 */
478 if (!td->o.read_iolog_file)
479 td->total_io_size = td->o.size * td->o.loops;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200480 return 0;
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200481err_offset:
482 log_err("%s: you need to specify valid offset=\n", td->o.name);
483 return 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200484}
485
Jens Axboe68727072007-03-15 20:49:25 +0100486int init_random_map(struct thread_data *td)
487{
Jens Axboe509eab12007-12-14 12:42:53 +0100488 unsigned long long blocks, num_maps;
Jens Axboe68727072007-03-15 20:49:25 +0100489 struct fio_file *f;
490 unsigned int i;
491
492 if (td->o.norandommap)
493 return 0;
494
495 for_each_file(td, f, i) {
Jens Axboe509eab12007-12-14 12:42:53 +0100496 blocks = (f->real_file_size + td->o.rw_min_bs - 1) / (unsigned long long) td->o.rw_min_bs;
497 num_maps = (blocks + BLOCKS_PER_MAP-1)/ (unsigned long long) BLOCKS_PER_MAP;
Jens Axboe68727072007-03-15 20:49:25 +0100498 f->file_map = malloc(num_maps * sizeof(long));
499 if (!f->file_map) {
500 log_err("fio: failed allocating random map. If running a large number of jobs, try the 'norandommap' option\n");
501 return 1;
502 }
503 f->num_maps = num_maps;
504 memset(f->file_map, 0, num_maps * sizeof(long));
505 }
506
507 return 0;
508}
509
Jens Axboe53cdc682006-10-18 11:50:58 +0200510void close_files(struct thread_data *td)
511{
Jens Axboe0ab8db82006-10-18 17:16:23 +0200512 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100513 unsigned int i;
Jens Axboe53cdc682006-10-18 11:50:58 +0200514
Jens Axboe0ab8db82006-10-18 17:16:23 +0200515 for_each_file(td, f, i) {
Jens Axboea9b7b302007-04-19 16:31:16 +0200516 if (td->o.unlink && f->filetype == FIO_TYPE_FILE)
Jens Axboe132ad462006-11-23 12:23:12 +0100517 unlink(f->file_name);
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100518
Jens Axboeb5af8292007-03-08 12:43:13 +0100519 td_io_close_file(td, f);
Jens Axboeb3dc7f02007-03-09 12:52:15 +0100520
Jens Axboefa1da862007-03-23 15:20:54 +0100521 free(f->file_name);
522 f->file_name = NULL;
523
Jens Axboec3439812007-03-20 13:54:53 +0100524 if (f->file_map) {
Jens Axboeb3dc7f02007-03-09 12:52:15 +0100525 free(f->file_map);
Jens Axboec3439812007-03-20 13:54:53 +0100526 f->file_map = NULL;
527 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200528 }
Jens Axboeb4a6a592006-10-20 13:54:47 +0200529
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100530 td->o.filename = NULL;
Jens Axboecade3ef2007-03-23 15:29:45 +0100531 free(td->files);
Jens Axboeb4a6a592006-10-20 13:54:47 +0200532 td->files = NULL;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100533 td->o.nr_files = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200534}
Jens Axboeaf52b342007-03-13 10:07:47 +0100535
Jens Axboee3bab462007-03-13 11:22:09 +0100536static void get_file_type(struct fio_file *f)
Jens Axboeaf52b342007-03-13 10:07:47 +0100537{
538 struct stat sb;
539
Jens Axboe66159822007-04-16 21:54:24 +0200540 if (!strcmp(f->file_name, "-"))
541 f->filetype = FIO_TYPE_PIPE;
542 else
543 f->filetype = FIO_TYPE_FILE;
Jens Axboeaf52b342007-03-13 10:07:47 +0100544
Jens Axboee3bab462007-03-13 11:22:09 +0100545 if (!lstat(f->file_name, &sb)) {
Jens Axboeaf52b342007-03-13 10:07:47 +0100546 if (S_ISBLK(sb.st_mode))
547 f->filetype = FIO_TYPE_BD;
548 else if (S_ISCHR(sb.st_mode))
549 f->filetype = FIO_TYPE_CHAR;
Jens Axboeb5605e92007-04-16 20:42:33 +0200550 else if (S_ISFIFO(sb.st_mode))
551 f->filetype = FIO_TYPE_PIPE;
Jens Axboeaf52b342007-03-13 10:07:47 +0100552 }
553}
554
Jens Axboef29b25a2007-07-23 08:56:43 +0200555int add_file(struct thread_data *td, const char *fname)
Jens Axboeaf52b342007-03-13 10:07:47 +0100556{
Jens Axboe7b4e4fe2007-03-13 12:51:40 +0100557 int cur_files = td->files_index;
Jens Axboebd0ee742007-03-23 14:26:23 +0100558 char file_name[PATH_MAX];
Jens Axboeaf52b342007-03-13 10:07:47 +0100559 struct fio_file *f;
Jens Axboebd0ee742007-03-23 14:26:23 +0100560 int len = 0;
Jens Axboeaf52b342007-03-13 10:07:47 +0100561
562 td->files = realloc(td->files, (cur_files + 1) * sizeof(*f));
563
564 f = &td->files[cur_files];
565 memset(f, 0, sizeof(*f));
566 f->fd = -1;
Jens Axboebd0ee742007-03-23 14:26:23 +0100567
Jens Axboe07eb79d2007-04-12 13:02:38 +0200568 /*
569 * init function, io engine may not be loaded yet
570 */
571 if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO))
572 f->real_file_size = -1ULL;
573
Jens Axboebd0ee742007-03-23 14:26:23 +0100574 if (td->o.directory)
575 len = sprintf(file_name, "%s/", td->o.directory);
576
577 sprintf(file_name + len, "%s", fname);
578 f->file_name = strdup(file_name);
Jens Axboeaf52b342007-03-13 10:07:47 +0100579
Jens Axboee3bab462007-03-13 11:22:09 +0100580 get_file_type(f);
Jens Axboeaf52b342007-03-13 10:07:47 +0100581
Jens Axboe7b4e4fe2007-03-13 12:51:40 +0100582 td->files_index++;
Jens Axboe15494412007-03-13 11:52:47 +0100583 if (f->filetype == FIO_TYPE_FILE)
584 td->nr_normal_files++;
Jens Axboef29b25a2007-07-23 08:56:43 +0200585
586 return cur_files;
Jens Axboeaf52b342007-03-13 10:07:47 +0100587}
Jens Axboe0ad920e2007-03-13 11:06:45 +0100588
589void get_file(struct fio_file *f)
590{
Jens Axboe97af62c2007-05-22 11:12:13 +0200591 assert(f->flags & FIO_FILE_OPEN);
Jens Axboe0ad920e2007-03-13 11:06:45 +0100592 f->references++;
593}
594
595void put_file(struct thread_data *td, struct fio_file *f)
596{
597 if (!(f->flags & FIO_FILE_OPEN))
598 return;
599
600 assert(f->references);
601 if (--f->references)
602 return;
603
Jens Axboed424d4d2007-04-17 09:05:10 +0200604 if (should_fsync(td) && td->o.fsync_on_close)
Jens Axboeebb14152007-03-13 14:42:15 +0100605 fsync(f->fd);
606
Jens Axboe0ad920e2007-03-13 11:06:45 +0100607 if (td->io_ops->close_file)
608 td->io_ops->close_file(td, f);
Jens Axboe1020a132007-03-29 11:15:06 +0200609
Jens Axboe0ad920e2007-03-13 11:06:45 +0100610 td->nr_open_files--;
611 f->flags &= ~FIO_FILE_OPEN;
612}
Jens Axboebbf6b542007-03-13 15:28:55 +0100613
614static int recurse_dir(struct thread_data *td, const char *dirname)
615{
616 struct dirent *dir;
617 int ret = 0;
618 DIR *D;
619
620 D = opendir(dirname);
621 if (!D) {
Jens Axboe0ddb2702007-03-27 19:43:53 +0200622 char buf[FIO_VERROR_SIZE];
623
624 snprintf(buf, FIO_VERROR_SIZE - 1, "opendir(%s)", dirname);
625 td_verror(td, errno, buf);
Jens Axboebbf6b542007-03-13 15:28:55 +0100626 return 1;
627 }
628
629 while ((dir = readdir(D)) != NULL) {
630 char full_path[PATH_MAX];
631 struct stat sb;
632
Jens Axboee85b2b82007-03-14 09:39:06 +0100633 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
634 continue;
Jens Axboe96d32d52007-03-14 09:16:09 +0100635
Jens Axboebbf6b542007-03-13 15:28:55 +0100636 sprintf(full_path, "%s/%s", dirname, dir->d_name);
637
638 if (lstat(full_path, &sb) == -1) {
639 if (errno != ENOENT) {
640 td_verror(td, errno, "stat");
641 return 1;
642 }
643 }
644
645 if (S_ISREG(sb.st_mode)) {
646 add_file(td, full_path);
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100647 td->o.nr_files++;
Jens Axboebbf6b542007-03-13 15:28:55 +0100648 continue;
649 }
Jens Axboe0ddb2702007-03-27 19:43:53 +0200650 if (!S_ISDIR(sb.st_mode))
651 continue;
Jens Axboebbf6b542007-03-13 15:28:55 +0100652
Jens Axboebbf6b542007-03-13 15:28:55 +0100653 if ((ret = recurse_dir(td, full_path)) != 0)
654 break;
655 }
656
657 closedir(D);
658 return ret;
659}
660
661int add_dir_files(struct thread_data *td, const char *path)
662{
Jens Axboe0ddb2702007-03-27 19:43:53 +0200663 int ret = recurse_dir(td, path);
664
665 if (!ret)
666 log_info("fio: opendir added %d files\n", td->o.nr_files);
667
668 return ret;
Jens Axboebbf6b542007-03-13 15:28:55 +0100669}
Jens Axboecade3ef2007-03-23 15:29:45 +0100670
671void dup_files(struct thread_data *td, struct thread_data *org)
672{
673 struct fio_file *f;
674 unsigned int i;
675 size_t bytes;
676
677 if (!org->files)
678 return;
679
680 bytes = org->files_index * sizeof(*f);
681 td->files = malloc(bytes);
682 memcpy(td->files, org->files, bytes);
683
684 for_each_file(td, f, i) {
685 if (f->file_name)
686 f->file_name = strdup(f->file_name);
687 }
688}
Jens Axboef29b25a2007-07-23 08:56:43 +0200689
690/*
691 * Returns the index that matches the filename, or -1 if not there
692 */
693int get_fileno(struct thread_data *td, const char *fname)
694{
695 struct fio_file *f;
696 unsigned int i;
697
698 for_each_file(td, f, i)
699 if (!strcmp(f->file_name, fname))
700 return i;
701
702 return -1;
703}
704
705/*
706 * For log usage, where we add/open/close files automatically
707 */
708void free_release_files(struct thread_data *td)
709{
710 close_files(td);
711 td->files_index = 0;
712 td->nr_normal_files = 0;
713}