blob: f509220276c4097cb7c60e740b3f0204ba08a1fb [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
Shawn Lewisfc74ac12008-01-11 09:45:11 +010053 if (!new_layout)
54 goto done;
55
Jens Axboe7bb48f82007-03-27 15:30:28 +020056 if (ftruncate(f->fd, f->real_file_size) == -1) {
Jens Axboee1161c32007-02-22 19:36:48 +010057 td_verror(td, errno, "ftruncate");
Jens Axboe53cdc682006-10-18 11:50:58 +020058 goto err;
59 }
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;
Jens Axboe65bdb102008-01-24 13:13:12 +0100406 if (!f->io_size)
Jens Axboe273f8c92008-01-25 14:02:15 +0100407 f->io_size = f->real_file_size - f->file_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200408 } else if (f->real_file_size < td->o.file_size_low ||
409 f->real_file_size > td->o.file_size_high) {
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200410 if (f->file_offset > td->o.file_size_low)
411 goto err_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200412 /*
413 * file size given. if it's fixed, use that. if it's a
414 * range, generate a random size in-between.
415 */
416 if (td->o.file_size_low == td->o.file_size_high)
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200417 f->io_size = td->o.file_size_low - f->file_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200418 else
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200419 f->io_size = get_rand_file_size(td) - f->file_offset;
Jens Axboe65bdb102008-01-24 13:13:12 +0100420 } else
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200421 f->io_size = f->real_file_size - f->file_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200422
423 if (f->io_size == -1ULL)
424 total_size = -1ULL;
425 else
426 total_size += f->io_size;
427
428 if (f->filetype == FIO_TYPE_FILE &&
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200429 (f->io_size + f->file_offset) > f->real_file_size &&
Jens Axboe7bb48f82007-03-27 15:30:28 +0200430 !(td->io_ops->flags & FIO_DISKLESSIO)) {
431 need_extend++;
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200432 extend_size += (f->io_size + f->file_offset);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200433 f->flags |= FIO_FILE_EXTEND;
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200434 }
Jens Axboe7bb48f82007-03-27 15:30:28 +0200435 }
436
ljzhang,Yaxin Hu,Jianchao Tang22982902007-07-27 13:21:28 +0200437 if (!td->o.size || td->o.size > total_size)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200438 td->o.size = total_size;
439
440 /*
441 * See if we need to extend some files
442 */
443 if (need_extend) {
444 temp_stall_ts = 1;
Jens Axboef627d8a2007-08-24 10:24:09 +0200445 log_info("%s: Laying out IO file(s) (%u file(s) / %LuMiB)\n",
Jens Axboe7bb48f82007-03-27 15:30:28 +0200446 td->o.name, need_extend, extend_size >> 20);
447
448 for_each_file(td, f, i) {
449 if (!(f->flags & FIO_FILE_EXTEND))
450 continue;
451
Jens Axboe409b3412007-03-28 09:57:01 +0200452 assert(f->filetype == FIO_TYPE_FILE);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200453 f->flags &= ~FIO_FILE_EXTEND;
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200454 f->real_file_size = (f->io_size + f->file_offset);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200455 err = extend_file(td, f);
456 if (err)
457 break;
458 }
459 temp_stall_ts = 0;
460 }
461
462 if (err)
463 return err;
464
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100465 if (!td->o.zone_size)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200466 td->o.zone_size = td->o.size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200467
Jens Axboeea966f82007-09-03 10:09:37 +0200468 /*
469 * iolog already set the total io size, if we read back
470 * stored entries.
471 */
472 if (!td->o.read_iolog_file)
473 td->total_io_size = td->o.size * td->o.loops;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200474 return 0;
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200475err_offset:
476 log_err("%s: you need to specify valid offset=\n", td->o.name);
477 return 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200478}
479
Jens Axboe68727072007-03-15 20:49:25 +0100480int init_random_map(struct thread_data *td)
481{
Jens Axboe509eab12007-12-14 12:42:53 +0100482 unsigned long long blocks, num_maps;
Jens Axboe68727072007-03-15 20:49:25 +0100483 struct fio_file *f;
484 unsigned int i;
485
486 if (td->o.norandommap)
487 return 0;
488
489 for_each_file(td, f, i) {
Jens Axboe509eab12007-12-14 12:42:53 +0100490 blocks = (f->real_file_size + td->o.rw_min_bs - 1) / (unsigned long long) td->o.rw_min_bs;
491 num_maps = (blocks + BLOCKS_PER_MAP-1)/ (unsigned long long) BLOCKS_PER_MAP;
Jens Axboe68727072007-03-15 20:49:25 +0100492 f->file_map = malloc(num_maps * sizeof(long));
493 if (!f->file_map) {
494 log_err("fio: failed allocating random map. If running a large number of jobs, try the 'norandommap' option\n");
495 return 1;
496 }
497 f->num_maps = num_maps;
498 memset(f->file_map, 0, num_maps * sizeof(long));
499 }
500
501 return 0;
502}
503
Jens Axboe53cdc682006-10-18 11:50:58 +0200504void close_files(struct thread_data *td)
505{
Jens Axboe0ab8db82006-10-18 17:16:23 +0200506 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100507 unsigned int i;
Jens Axboe53cdc682006-10-18 11:50:58 +0200508
Jens Axboe0ab8db82006-10-18 17:16:23 +0200509 for_each_file(td, f, i) {
Jens Axboea9b7b302007-04-19 16:31:16 +0200510 if (td->o.unlink && f->filetype == FIO_TYPE_FILE)
Jens Axboe132ad462006-11-23 12:23:12 +0100511 unlink(f->file_name);
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100512
Jens Axboeb5af8292007-03-08 12:43:13 +0100513 td_io_close_file(td, f);
Jens Axboeb3dc7f02007-03-09 12:52:15 +0100514
Jens Axboefa1da862007-03-23 15:20:54 +0100515 free(f->file_name);
516 f->file_name = NULL;
517
Jens Axboec3439812007-03-20 13:54:53 +0100518 if (f->file_map) {
Jens Axboeb3dc7f02007-03-09 12:52:15 +0100519 free(f->file_map);
Jens Axboec3439812007-03-20 13:54:53 +0100520 f->file_map = NULL;
521 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200522 }
Jens Axboeb4a6a592006-10-20 13:54:47 +0200523
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100524 td->o.filename = NULL;
Jens Axboecade3ef2007-03-23 15:29:45 +0100525 free(td->files);
Jens Axboeb4a6a592006-10-20 13:54:47 +0200526 td->files = NULL;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100527 td->o.nr_files = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200528}
Jens Axboeaf52b342007-03-13 10:07:47 +0100529
Jens Axboee3bab462007-03-13 11:22:09 +0100530static void get_file_type(struct fio_file *f)
Jens Axboeaf52b342007-03-13 10:07:47 +0100531{
532 struct stat sb;
533
Jens Axboe66159822007-04-16 21:54:24 +0200534 if (!strcmp(f->file_name, "-"))
535 f->filetype = FIO_TYPE_PIPE;
536 else
537 f->filetype = FIO_TYPE_FILE;
Jens Axboeaf52b342007-03-13 10:07:47 +0100538
Jens Axboee3bab462007-03-13 11:22:09 +0100539 if (!lstat(f->file_name, &sb)) {
Jens Axboeaf52b342007-03-13 10:07:47 +0100540 if (S_ISBLK(sb.st_mode))
541 f->filetype = FIO_TYPE_BD;
542 else if (S_ISCHR(sb.st_mode))
543 f->filetype = FIO_TYPE_CHAR;
Jens Axboeb5605e92007-04-16 20:42:33 +0200544 else if (S_ISFIFO(sb.st_mode))
545 f->filetype = FIO_TYPE_PIPE;
Jens Axboeaf52b342007-03-13 10:07:47 +0100546 }
547}
548
Jens Axboef29b25a2007-07-23 08:56:43 +0200549int add_file(struct thread_data *td, const char *fname)
Jens Axboeaf52b342007-03-13 10:07:47 +0100550{
Jens Axboe7b4e4fe2007-03-13 12:51:40 +0100551 int cur_files = td->files_index;
Jens Axboebd0ee742007-03-23 14:26:23 +0100552 char file_name[PATH_MAX];
Jens Axboeaf52b342007-03-13 10:07:47 +0100553 struct fio_file *f;
Jens Axboebd0ee742007-03-23 14:26:23 +0100554 int len = 0;
Jens Axboeaf52b342007-03-13 10:07:47 +0100555
556 td->files = realloc(td->files, (cur_files + 1) * sizeof(*f));
557
558 f = &td->files[cur_files];
559 memset(f, 0, sizeof(*f));
560 f->fd = -1;
Jens Axboebd0ee742007-03-23 14:26:23 +0100561
Jens Axboe07eb79d2007-04-12 13:02:38 +0200562 /*
563 * init function, io engine may not be loaded yet
564 */
565 if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO))
566 f->real_file_size = -1ULL;
567
Jens Axboebd0ee742007-03-23 14:26:23 +0100568 if (td->o.directory)
569 len = sprintf(file_name, "%s/", td->o.directory);
570
571 sprintf(file_name + len, "%s", fname);
572 f->file_name = strdup(file_name);
Jens Axboeaf52b342007-03-13 10:07:47 +0100573
Jens Axboee3bab462007-03-13 11:22:09 +0100574 get_file_type(f);
Jens Axboeaf52b342007-03-13 10:07:47 +0100575
Jens Axboe7b4e4fe2007-03-13 12:51:40 +0100576 td->files_index++;
Jens Axboe15494412007-03-13 11:52:47 +0100577 if (f->filetype == FIO_TYPE_FILE)
578 td->nr_normal_files++;
Jens Axboef29b25a2007-07-23 08:56:43 +0200579
580 return cur_files;
Jens Axboeaf52b342007-03-13 10:07:47 +0100581}
Jens Axboe0ad920e2007-03-13 11:06:45 +0100582
583void get_file(struct fio_file *f)
584{
Jens Axboe97af62c2007-05-22 11:12:13 +0200585 assert(f->flags & FIO_FILE_OPEN);
Jens Axboe0ad920e2007-03-13 11:06:45 +0100586 f->references++;
587}
588
589void put_file(struct thread_data *td, struct fio_file *f)
590{
591 if (!(f->flags & FIO_FILE_OPEN))
592 return;
593
594 assert(f->references);
595 if (--f->references)
596 return;
597
Jens Axboed424d4d2007-04-17 09:05:10 +0200598 if (should_fsync(td) && td->o.fsync_on_close)
Jens Axboeebb14152007-03-13 14:42:15 +0100599 fsync(f->fd);
600
Jens Axboe0ad920e2007-03-13 11:06:45 +0100601 if (td->io_ops->close_file)
602 td->io_ops->close_file(td, f);
Jens Axboe1020a132007-03-29 11:15:06 +0200603
Jens Axboe0ad920e2007-03-13 11:06:45 +0100604 td->nr_open_files--;
605 f->flags &= ~FIO_FILE_OPEN;
606}
Jens Axboebbf6b542007-03-13 15:28:55 +0100607
608static int recurse_dir(struct thread_data *td, const char *dirname)
609{
610 struct dirent *dir;
611 int ret = 0;
612 DIR *D;
613
614 D = opendir(dirname);
615 if (!D) {
Jens Axboe0ddb2702007-03-27 19:43:53 +0200616 char buf[FIO_VERROR_SIZE];
617
618 snprintf(buf, FIO_VERROR_SIZE - 1, "opendir(%s)", dirname);
619 td_verror(td, errno, buf);
Jens Axboebbf6b542007-03-13 15:28:55 +0100620 return 1;
621 }
622
623 while ((dir = readdir(D)) != NULL) {
624 char full_path[PATH_MAX];
625 struct stat sb;
626
Jens Axboee85b2b82007-03-14 09:39:06 +0100627 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
628 continue;
Jens Axboe96d32d52007-03-14 09:16:09 +0100629
Jens Axboebbf6b542007-03-13 15:28:55 +0100630 sprintf(full_path, "%s/%s", dirname, dir->d_name);
631
632 if (lstat(full_path, &sb) == -1) {
633 if (errno != ENOENT) {
634 td_verror(td, errno, "stat");
635 return 1;
636 }
637 }
638
639 if (S_ISREG(sb.st_mode)) {
640 add_file(td, full_path);
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100641 td->o.nr_files++;
Jens Axboebbf6b542007-03-13 15:28:55 +0100642 continue;
643 }
Jens Axboe0ddb2702007-03-27 19:43:53 +0200644 if (!S_ISDIR(sb.st_mode))
645 continue;
Jens Axboebbf6b542007-03-13 15:28:55 +0100646
Jens Axboebbf6b542007-03-13 15:28:55 +0100647 if ((ret = recurse_dir(td, full_path)) != 0)
648 break;
649 }
650
651 closedir(D);
652 return ret;
653}
654
655int add_dir_files(struct thread_data *td, const char *path)
656{
Jens Axboe0ddb2702007-03-27 19:43:53 +0200657 int ret = recurse_dir(td, path);
658
659 if (!ret)
660 log_info("fio: opendir added %d files\n", td->o.nr_files);
661
662 return ret;
Jens Axboebbf6b542007-03-13 15:28:55 +0100663}
Jens Axboecade3ef2007-03-23 15:29:45 +0100664
665void dup_files(struct thread_data *td, struct thread_data *org)
666{
667 struct fio_file *f;
668 unsigned int i;
669 size_t bytes;
670
671 if (!org->files)
672 return;
673
674 bytes = org->files_index * sizeof(*f);
675 td->files = malloc(bytes);
676 memcpy(td->files, org->files, bytes);
677
678 for_each_file(td, f, i) {
679 if (f->file_name)
680 f->file_name = strdup(f->file_name);
681 }
682}
Jens Axboef29b25a2007-07-23 08:56:43 +0200683
684/*
685 * Returns the index that matches the filename, or -1 if not there
686 */
687int get_fileno(struct thread_data *td, const char *fname)
688{
689 struct fio_file *f;
690 unsigned int i;
691
692 for_each_file(td, f, i)
693 if (!strcmp(f->file_name, fname))
694 return i;
695
696 return -1;
697}
698
699/*
700 * For log usage, where we add/open/close files automatically
701 */
702void free_release_files(struct thread_data *td)
703{
704 close_files(td);
705 td->files_index = 0;
706 td->nr_normal_files = 0;
707}