blob: 6c241516ff9424f526e587b9acd620b817c9cdfb [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 &&
383 !(td->io_ops->flags & FIO_NOIO)) {
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;
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200406 if (!f->io_size) {
407 if (f->file_offset > f->real_file_size)
408 goto err_offset;
409 f->io_size = f->real_file_size - f->file_offset;
410 }
Jens Axboe7bb48f82007-03-27 15:30:28 +0200411 } else if (f->real_file_size < td->o.file_size_low ||
412 f->real_file_size > td->o.file_size_high) {
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200413 if (f->file_offset > td->o.file_size_low)
414 goto err_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200415 /*
416 * file size given. if it's fixed, use that. if it's a
417 * range, generate a random size in-between.
418 */
419 if (td->o.file_size_low == td->o.file_size_high)
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200420 f->io_size = td->o.file_size_low - f->file_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200421 else
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200422 f->io_size = get_rand_file_size(td) - f->file_offset;
423 } else if (f->file_offset > f->real_file_size)
424 goto err_offset;
425 else
426 f->io_size = f->real_file_size - f->file_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200427
428 if (f->io_size == -1ULL)
429 total_size = -1ULL;
430 else
431 total_size += f->io_size;
432
433 if (f->filetype == FIO_TYPE_FILE &&
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200434 (f->io_size + f->file_offset) > f->real_file_size &&
Jens Axboe7bb48f82007-03-27 15:30:28 +0200435 !(td->io_ops->flags & FIO_DISKLESSIO)) {
436 need_extend++;
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200437 extend_size += (f->io_size + f->file_offset);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200438 f->flags |= FIO_FILE_EXTEND;
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200439 }
Jens Axboe7bb48f82007-03-27 15:30:28 +0200440 }
441
ljzhang,Yaxin Hu,Jianchao Tang22982902007-07-27 13:21:28 +0200442 if (!td->o.size || td->o.size > total_size)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200443 td->o.size = total_size;
444
445 /*
446 * See if we need to extend some files
447 */
448 if (need_extend) {
449 temp_stall_ts = 1;
Jens Axboef627d8a2007-08-24 10:24:09 +0200450 log_info("%s: Laying out IO file(s) (%u file(s) / %LuMiB)\n",
Jens Axboe7bb48f82007-03-27 15:30:28 +0200451 td->o.name, need_extend, extend_size >> 20);
452
453 for_each_file(td, f, i) {
454 if (!(f->flags & FIO_FILE_EXTEND))
455 continue;
456
Jens Axboe409b3412007-03-28 09:57:01 +0200457 assert(f->filetype == FIO_TYPE_FILE);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200458 f->flags &= ~FIO_FILE_EXTEND;
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200459 f->real_file_size = (f->io_size + f->file_offset);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200460 err = extend_file(td, f);
461 if (err)
462 break;
463 }
464 temp_stall_ts = 0;
465 }
466
467 if (err)
468 return err;
469
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100470 if (!td->o.zone_size)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200471 td->o.zone_size = td->o.size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200472
Jens Axboeea966f82007-09-03 10:09:37 +0200473 /*
474 * iolog already set the total io size, if we read back
475 * stored entries.
476 */
477 if (!td->o.read_iolog_file)
478 td->total_io_size = td->o.size * td->o.loops;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200479 return 0;
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200480err_offset:
481 log_err("%s: you need to specify valid offset=\n", td->o.name);
482 return 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200483}
484
Jens Axboe68727072007-03-15 20:49:25 +0100485int init_random_map(struct thread_data *td)
486{
487 int num_maps, blocks;
488 struct fio_file *f;
489 unsigned int i;
490
491 if (td->o.norandommap)
492 return 0;
493
494 for_each_file(td, f, i) {
495 blocks = (f->real_file_size + td->o.rw_min_bs - 1) / td->o.rw_min_bs;
496 num_maps = (blocks + BLOCKS_PER_MAP-1)/ BLOCKS_PER_MAP;
497 f->file_map = malloc(num_maps * sizeof(long));
498 if (!f->file_map) {
499 log_err("fio: failed allocating random map. If running a large number of jobs, try the 'norandommap' option\n");
500 return 1;
501 }
502 f->num_maps = num_maps;
503 memset(f->file_map, 0, num_maps * sizeof(long));
504 }
505
506 return 0;
507}
508
Jens Axboe53cdc682006-10-18 11:50:58 +0200509void close_files(struct thread_data *td)
510{
Jens Axboe0ab8db82006-10-18 17:16:23 +0200511 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100512 unsigned int i;
Jens Axboe53cdc682006-10-18 11:50:58 +0200513
Jens Axboe0ab8db82006-10-18 17:16:23 +0200514 for_each_file(td, f, i) {
Jens Axboea9b7b302007-04-19 16:31:16 +0200515 if (td->o.unlink && f->filetype == FIO_TYPE_FILE)
Jens Axboe132ad462006-11-23 12:23:12 +0100516 unlink(f->file_name);
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100517
Jens Axboeb5af8292007-03-08 12:43:13 +0100518 td_io_close_file(td, f);
Jens Axboeb3dc7f02007-03-09 12:52:15 +0100519
Jens Axboefa1da862007-03-23 15:20:54 +0100520 free(f->file_name);
521 f->file_name = NULL;
522
Jens Axboec3439812007-03-20 13:54:53 +0100523 if (f->file_map) {
Jens Axboeb3dc7f02007-03-09 12:52:15 +0100524 free(f->file_map);
Jens Axboec3439812007-03-20 13:54:53 +0100525 f->file_map = NULL;
526 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200527 }
Jens Axboeb4a6a592006-10-20 13:54:47 +0200528
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100529 td->o.filename = NULL;
Jens Axboecade3ef2007-03-23 15:29:45 +0100530 free(td->files);
Jens Axboeb4a6a592006-10-20 13:54:47 +0200531 td->files = NULL;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100532 td->o.nr_files = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200533}
Jens Axboeaf52b342007-03-13 10:07:47 +0100534
Jens Axboee3bab462007-03-13 11:22:09 +0100535static void get_file_type(struct fio_file *f)
Jens Axboeaf52b342007-03-13 10:07:47 +0100536{
537 struct stat sb;
538
Jens Axboe66159822007-04-16 21:54:24 +0200539 if (!strcmp(f->file_name, "-"))
540 f->filetype = FIO_TYPE_PIPE;
541 else
542 f->filetype = FIO_TYPE_FILE;
Jens Axboeaf52b342007-03-13 10:07:47 +0100543
Jens Axboee3bab462007-03-13 11:22:09 +0100544 if (!lstat(f->file_name, &sb)) {
Jens Axboeaf52b342007-03-13 10:07:47 +0100545 if (S_ISBLK(sb.st_mode))
546 f->filetype = FIO_TYPE_BD;
547 else if (S_ISCHR(sb.st_mode))
548 f->filetype = FIO_TYPE_CHAR;
Jens Axboeb5605e92007-04-16 20:42:33 +0200549 else if (S_ISFIFO(sb.st_mode))
550 f->filetype = FIO_TYPE_PIPE;
Jens Axboeaf52b342007-03-13 10:07:47 +0100551 }
552}
553
Jens Axboef29b25a2007-07-23 08:56:43 +0200554int add_file(struct thread_data *td, const char *fname)
Jens Axboeaf52b342007-03-13 10:07:47 +0100555{
Jens Axboe7b4e4fe2007-03-13 12:51:40 +0100556 int cur_files = td->files_index;
Jens Axboebd0ee742007-03-23 14:26:23 +0100557 char file_name[PATH_MAX];
Jens Axboeaf52b342007-03-13 10:07:47 +0100558 struct fio_file *f;
Jens Axboebd0ee742007-03-23 14:26:23 +0100559 int len = 0;
Jens Axboeaf52b342007-03-13 10:07:47 +0100560
561 td->files = realloc(td->files, (cur_files + 1) * sizeof(*f));
562
563 f = &td->files[cur_files];
564 memset(f, 0, sizeof(*f));
565 f->fd = -1;
Jens Axboebd0ee742007-03-23 14:26:23 +0100566
Jens Axboe07eb79d2007-04-12 13:02:38 +0200567 /*
568 * init function, io engine may not be loaded yet
569 */
570 if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO))
571 f->real_file_size = -1ULL;
572
Jens Axboebd0ee742007-03-23 14:26:23 +0100573 if (td->o.directory)
574 len = sprintf(file_name, "%s/", td->o.directory);
575
576 sprintf(file_name + len, "%s", fname);
577 f->file_name = strdup(file_name);
Jens Axboeaf52b342007-03-13 10:07:47 +0100578
Jens Axboee3bab462007-03-13 11:22:09 +0100579 get_file_type(f);
Jens Axboeaf52b342007-03-13 10:07:47 +0100580
Jens Axboe7b4e4fe2007-03-13 12:51:40 +0100581 td->files_index++;
Jens Axboe15494412007-03-13 11:52:47 +0100582 if (f->filetype == FIO_TYPE_FILE)
583 td->nr_normal_files++;
Jens Axboef29b25a2007-07-23 08:56:43 +0200584
585 return cur_files;
Jens Axboeaf52b342007-03-13 10:07:47 +0100586}
Jens Axboe0ad920e2007-03-13 11:06:45 +0100587
588void get_file(struct fio_file *f)
589{
Jens Axboe97af62c2007-05-22 11:12:13 +0200590 assert(f->flags & FIO_FILE_OPEN);
Jens Axboe0ad920e2007-03-13 11:06:45 +0100591 f->references++;
592}
593
594void put_file(struct thread_data *td, struct fio_file *f)
595{
596 if (!(f->flags & FIO_FILE_OPEN))
597 return;
598
599 assert(f->references);
600 if (--f->references)
601 return;
602
Jens Axboed424d4d2007-04-17 09:05:10 +0200603 if (should_fsync(td) && td->o.fsync_on_close)
Jens Axboeebb14152007-03-13 14:42:15 +0100604 fsync(f->fd);
605
Jens Axboe0ad920e2007-03-13 11:06:45 +0100606 if (td->io_ops->close_file)
607 td->io_ops->close_file(td, f);
Jens Axboe1020a132007-03-29 11:15:06 +0200608
Jens Axboe0ad920e2007-03-13 11:06:45 +0100609 td->nr_open_files--;
610 f->flags &= ~FIO_FILE_OPEN;
611}
Jens Axboebbf6b542007-03-13 15:28:55 +0100612
613static int recurse_dir(struct thread_data *td, const char *dirname)
614{
615 struct dirent *dir;
616 int ret = 0;
617 DIR *D;
618
619 D = opendir(dirname);
620 if (!D) {
Jens Axboe0ddb2702007-03-27 19:43:53 +0200621 char buf[FIO_VERROR_SIZE];
622
623 snprintf(buf, FIO_VERROR_SIZE - 1, "opendir(%s)", dirname);
624 td_verror(td, errno, buf);
Jens Axboebbf6b542007-03-13 15:28:55 +0100625 return 1;
626 }
627
628 while ((dir = readdir(D)) != NULL) {
629 char full_path[PATH_MAX];
630 struct stat sb;
631
Jens Axboee85b2b82007-03-14 09:39:06 +0100632 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
633 continue;
Jens Axboe96d32d52007-03-14 09:16:09 +0100634
Jens Axboebbf6b542007-03-13 15:28:55 +0100635 sprintf(full_path, "%s/%s", dirname, dir->d_name);
636
637 if (lstat(full_path, &sb) == -1) {
638 if (errno != ENOENT) {
639 td_verror(td, errno, "stat");
640 return 1;
641 }
642 }
643
644 if (S_ISREG(sb.st_mode)) {
645 add_file(td, full_path);
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100646 td->o.nr_files++;
Jens Axboebbf6b542007-03-13 15:28:55 +0100647 continue;
648 }
Jens Axboe0ddb2702007-03-27 19:43:53 +0200649 if (!S_ISDIR(sb.st_mode))
650 continue;
Jens Axboebbf6b542007-03-13 15:28:55 +0100651
Jens Axboebbf6b542007-03-13 15:28:55 +0100652 if ((ret = recurse_dir(td, full_path)) != 0)
653 break;
654 }
655
656 closedir(D);
657 return ret;
658}
659
660int add_dir_files(struct thread_data *td, const char *path)
661{
Jens Axboe0ddb2702007-03-27 19:43:53 +0200662 int ret = recurse_dir(td, path);
663
664 if (!ret)
665 log_info("fio: opendir added %d files\n", td->o.nr_files);
666
667 return ret;
Jens Axboebbf6b542007-03-13 15:28:55 +0100668}
Jens Axboecade3ef2007-03-23 15:29:45 +0100669
670void dup_files(struct thread_data *td, struct thread_data *org)
671{
672 struct fio_file *f;
673 unsigned int i;
674 size_t bytes;
675
676 if (!org->files)
677 return;
678
679 bytes = org->files_index * sizeof(*f);
680 td->files = malloc(bytes);
681 memcpy(td->files, org->files, bytes);
682
683 for_each_file(td, f, i) {
684 if (f->file_name)
685 f->file_name = strdup(f->file_name);
686 }
687}
Jens Axboef29b25a2007-07-23 08:56:43 +0200688
689/*
690 * Returns the index that matches the filename, or -1 if not there
691 */
692int get_fileno(struct thread_data *td, const char *fname)
693{
694 struct fio_file *f;
695 unsigned int i;
696
697 for_each_file(td, f, i)
698 if (!strcmp(f->file_name, fname))
699 return i;
700
701 return -1;
702}
703
704/*
705 * For log usage, where we add/open/close files automatically
706 */
707void free_release_files(struct thread_data *td)
708{
709 close_files(td);
710 td->files_index = 0;
711 td->nr_normal_files = 0;
712}