blob: 93cddb1f467b3aea77cfc3b858c1164e89da8964 [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 Axboe7bb48f82007-03-27 15:30:28 +020012static int extend_file(struct thread_data *td, struct fio_file *f)
Jens Axboe25205e92006-10-19 20:50:14 +020013{
Jens Axboeea443652007-03-29 14:52:13 +020014 int r, new_layout = 0, unlink_file = 0, flags;
Jens Axboe25205e92006-10-19 20:50:14 +020015 unsigned long long left;
16 unsigned int bs;
17 char *b;
Jens Axboeb2a15192006-10-18 14:10:42 +020018
Jens Axboe507a7022007-03-27 15:52:46 +020019 /*
20 * check if we need to lay the file out complete again. fio
21 * does that for operations involving reads, or for writes
22 * where overwrite is set
23 */
24 if (td_read(td) || (td_write(td) && td->o.overwrite))
25 new_layout = 1;
Jens Axboeea443652007-03-29 14:52:13 +020026 if (td_write(td) && !td->o.overwrite)
27 unlink_file = 1;
Jens Axboe507a7022007-03-27 15:52:46 +020028
Jens Axboeea443652007-03-29 14:52:13 +020029 if ((unlink_file || new_layout) && (f->flags & FIO_FILE_EXISTS)) {
Jens Axboe7bb48f82007-03-27 15:30:28 +020030 if (unlink(f->file_name) < 0) {
31 td_verror(td, errno, "unlink");
32 return 1;
33 }
34 }
35
Jens Axboe507a7022007-03-27 15:52:46 +020036 flags = O_WRONLY | O_CREAT;
37 if (new_layout)
38 flags |= O_TRUNC;
39
40 f->fd = open(f->file_name, flags, 0644);
Jens Axboe53cdc682006-10-18 11:50:58 +020041 if (f->fd < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +010042 td_verror(td, errno, "open");
Jens Axboe53cdc682006-10-18 11:50:58 +020043 return 1;
44 }
45
Jens Axboe7bb48f82007-03-27 15:30:28 +020046 if (ftruncate(f->fd, f->real_file_size) == -1) {
Jens Axboee1161c32007-02-22 19:36:48 +010047 td_verror(td, errno, "ftruncate");
Jens Axboe53cdc682006-10-18 11:50:58 +020048 goto err;
49 }
50
Jens Axboee8be2ad2007-03-29 10:31:51 +020051 if (!new_layout)
52 goto done;
53
Jens Axboe7bb48f82007-03-27 15:30:28 +020054 if (posix_fallocate(f->fd, 0, f->real_file_size) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +010055 td_verror(td, errno, "posix_fallocate");
Jens Axboe40f82982006-10-25 11:21:05 +020056 goto err;
57 }
58
Jens Axboe2dc1bbe2007-03-15 15:01:33 +010059 b = malloc(td->o.max_bs[DDIR_WRITE]);
60 memset(b, 0, td->o.max_bs[DDIR_WRITE]);
Jens Axboe53cdc682006-10-18 11:50:58 +020061
Jens Axboe7bb48f82007-03-27 15:30:28 +020062 left = f->real_file_size;
Jens Axboe53cdc682006-10-18 11:50:58 +020063 while (left && !td->terminate) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +010064 bs = td->o.max_bs[DDIR_WRITE];
Jens Axboe53cdc682006-10-18 11:50:58 +020065 if (bs > left)
66 bs = left;
67
68 r = write(f->fd, b, bs);
69
70 if (r == (int) bs) {
71 left -= bs;
72 continue;
73 } else {
74 if (r < 0)
Jens Axboee1161c32007-02-22 19:36:48 +010075 td_verror(td, errno, "write");
Jens Axboe53cdc682006-10-18 11:50:58 +020076 else
Jens Axboee1161c32007-02-22 19:36:48 +010077 td_verror(td, EIO, "write");
Jens Axboe53cdc682006-10-18 11:50:58 +020078
79 break;
80 }
81 }
82
83 if (td->terminate)
84 unlink(f->file_name);
Jens Axboe2dc1bbe2007-03-15 15:01:33 +010085 else if (td->o.create_fsync)
Jens Axboe53cdc682006-10-18 11:50:58 +020086 fsync(f->fd);
87
88 free(b);
Jens Axboe507a7022007-03-27 15:52:46 +020089done:
Jens Axboe53cdc682006-10-18 11:50:58 +020090 close(f->fd);
91 f->fd = -1;
92 return 0;
93err:
94 close(f->fd);
95 f->fd = -1;
96 return 1;
97}
98
Jens Axboe7bb48f82007-03-27 15:30:28 +020099static unsigned long long get_rand_file_size(struct thread_data *td)
Jens Axboe9c60ce62007-03-15 09:14:47 +0100100{
Jens Axboe9c60ce62007-03-15 09:14:47 +0100101 unsigned long long ret;
102 long r;
103
Jens Axboe9c60ce62007-03-15 09:14:47 +0100104 r = os_random_long(&td->file_size_state);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200105 ret = td->o.file_size_low + (unsigned long long) ((double) td->o.file_size_high * (r / (RAND_MAX + 1.0)));
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100106 ret -= (ret % td->o.rw_min_bs);
Jens Axboe9c60ce62007-03-15 09:14:47 +0100107 return ret;
108}
109
Jens Axboe53cdc682006-10-18 11:50:58 +0200110static int file_size(struct thread_data *td, struct fio_file *f)
111{
112 struct stat st;
113
Jens Axboe7bb48f82007-03-27 15:30:28 +0200114 if (fstat(f->fd, &st) == -1) {
115 td_verror(td, errno, "fstat");
116 return 1;
117 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200118
Jens Axboe7bb48f82007-03-27 15:30:28 +0200119 f->real_file_size = st.st_size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200120 return 0;
121}
122
123static int bdev_size(struct thread_data *td, struct fio_file *f)
124{
125 unsigned long long bytes;
126 int r;
127
128 r = blockdev_size(f->fd, &bytes);
129 if (r) {
Jens Axboee1161c32007-02-22 19:36:48 +0100130 td_verror(td, r, "blockdev_size");
Jens Axboe53cdc682006-10-18 11:50:58 +0200131 return 1;
132 }
133
134 f->real_file_size = bytes;
Jens Axboe53cdc682006-10-18 11:50:58 +0200135 return 0;
136}
137
138static int get_file_size(struct thread_data *td, struct fio_file *f)
139{
140 int ret = 0;
141
Jens Axboe409b3412007-03-28 09:57:01 +0200142 if (f->flags & FIO_SIZE_KNOWN)
143 return 0;
144
Jens Axboe7bb48f82007-03-27 15:30:28 +0200145 if (f->filetype == FIO_TYPE_FILE)
146 ret = file_size(td, f);
147 else if (f->filetype == FIO_TYPE_BD)
Jens Axboe53cdc682006-10-18 11:50:58 +0200148 ret = bdev_size(td, f);
149 else
150 f->real_file_size = -1;
151
152 if (ret)
153 return ret;
154
155 if (f->file_offset > f->real_file_size) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100156 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 +0200157 return 1;
158 }
159
Jens Axboe409b3412007-03-28 09:57:01 +0200160 f->flags |= FIO_SIZE_KNOWN;
Jens Axboe53cdc682006-10-18 11:50:58 +0200161 return 0;
162}
163
Jens Axboee5b401d2006-10-18 16:03:40 +0200164int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
165{
166 int ret = 0;
167
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100168 if (td->o.odirect)
Jens Axboeb5af8292007-03-08 12:43:13 +0100169 return 0;
170
Jens Axboee5b401d2006-10-18 16:03:40 +0200171 /*
172 * FIXME: add blockdev flushing too
173 */
Jens Axboeb5af8292007-03-08 12:43:13 +0100174 if (f->mmap)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200175 ret = madvise(f->mmap, f->io_size, MADV_DONTNEED);
Jens Axboe467d1b62007-03-15 14:11:10 +0100176 else if (f->filetype == FIO_TYPE_FILE)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200177 ret = fadvise(f->fd, f->file_offset, f->io_size, POSIX_FADV_DONTNEED);
Jens Axboe7e0e25c2007-03-27 12:49:56 +0200178 else if (f->filetype == FIO_TYPE_BD) {
Jens Axboeb5af8292007-03-08 12:43:13 +0100179 ret = blockdev_invalidate_cache(f->fd);
Jens Axboe7e0e25c2007-03-27 12:49:56 +0200180 if (ret < 0 && errno == EACCES && geteuid()) {
181 log_err("fio: only root may flush block devices. Cache flush bypassed!\n");
182 ret = 0;
183 }
Jens Axboeb5605e92007-04-16 20:42:33 +0200184 } else if (f->filetype == FIO_TYPE_CHAR || f->filetype == FIO_TYPE_PIPE)
Jens Axboee5b401d2006-10-18 16:03:40 +0200185 ret = 0;
186
187 if (ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100188 td_verror(td, errno, "invalidate_cache");
Jens Axboee5b401d2006-10-18 16:03:40 +0200189 return 1;
190 }
191
Jens Axboead2da602007-02-26 12:33:27 +0100192 return ret;
Jens Axboee5b401d2006-10-18 16:03:40 +0200193}
194
Jens Axboeb5af8292007-03-08 12:43:13 +0100195void generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
Jens Axboe53cdc682006-10-18 11:50:58 +0200196{
Jens Axboeb5af8292007-03-08 12:43:13 +0100197 close(f->fd);
198 f->fd = -1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200199}
200
Jens Axboeb5af8292007-03-08 12:43:13 +0100201int generic_open_file(struct thread_data *td, struct fio_file *f)
Jens Axboe53cdc682006-10-18 11:50:58 +0200202{
Jens Axboe66159822007-04-16 21:54:24 +0200203 int is_std = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200204 int flags = 0;
205
Jens Axboe66159822007-04-16 21:54:24 +0200206 if (!strcmp(f->file_name, "-")) {
207 if (td_rw(td)) {
208 log_err("fio: can't read/write to stdin/out\n");
209 return 1;
210 }
211 is_std = 1;
Jens Axboece98fa62007-04-17 13:27:32 +0200212
213 /*
214 * move output logging to stderr, if we are writing to stdout
215 */
216 if (td_write(td))
217 f_out = stderr;
Jens Axboe66159822007-04-16 21:54:24 +0200218 }
219
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100220 if (td->o.odirect)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100221 flags |= OS_O_DIRECT;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100222 if (td->o.sync_io)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100223 flags |= O_SYNC;
Jens Axboe7abf8332006-11-23 15:01:19 +0100224
Jens Axboe660a1cb2007-04-17 15:37:47 +0200225 if (td_write(td)) {
Jens Axboe2fd233b2007-03-02 08:44:25 +0100226 flags |= O_RDWR;
Jens Axboe53cdc682006-10-18 11:50:58 +0200227
Jens Axboeaf52b342007-03-13 10:07:47 +0100228 if (f->filetype == FIO_TYPE_FILE)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100229 flags |= O_CREAT;
Jens Axboe2fd233b2007-03-02 08:44:25 +0100230
Jens Axboe66159822007-04-16 21:54:24 +0200231 if (is_std)
232 f->fd = dup(STDOUT_FILENO);
233 else
234 f->fd = open(f->file_name, flags, 0600);
Jens Axboe2fd233b2007-03-02 08:44:25 +0100235 } else {
Jens Axboeaf52b342007-03-13 10:07:47 +0100236 if (f->filetype == FIO_TYPE_CHAR)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100237 flags |= O_RDWR;
238 else
239 flags |= O_RDONLY;
240
Jens Axboe66159822007-04-16 21:54:24 +0200241 if (is_std)
242 f->fd = dup(STDIN_FILENO);
243 else
244 f->fd = open(f->file_name, flags);
Jens Axboe53cdc682006-10-18 11:50:58 +0200245 }
246
247 if (f->fd == -1) {
Jens Axboee4e33252007-03-21 13:07:54 +0100248 char buf[FIO_VERROR_SIZE];
Jens Axboee1161c32007-02-22 19:36:48 +0100249 int __e = errno;
250
Jens Axboee4e33252007-03-21 13:07:54 +0100251 snprintf(buf, sizeof(buf) - 1, "open(%s)", f->file_name);
252
253 td_verror(td, __e, buf);
Jens Axboe53cdc682006-10-18 11:50:58 +0200254 }
255
Jens Axboeb5af8292007-03-08 12:43:13 +0100256 if (get_file_size(td, f))
257 goto err;
258
Jens Axboe53cdc682006-10-18 11:50:58 +0200259 return 0;
Jens Axboeb5af8292007-03-08 12:43:13 +0100260err:
261 close(f->fd);
262 return 1;
263}
264
Jens Axboe21972cd2006-11-23 12:39:16 +0100265int open_files(struct thread_data *td)
266{
267 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100268 unsigned int i;
269 int err = 0;
Jens Axboe21972cd2006-11-23 12:39:16 +0100270
271 for_each_file(td, f, i) {
Jens Axboeb5af8292007-03-08 12:43:13 +0100272 err = td_io_open_file(td, f);
Jens Axboe9bf27b42007-03-27 19:49:31 +0200273 if (err) {
274 if (td->error == EMFILE) {
275 log_err("fio: limited open files to: %d\n", td->nr_open_files);
276 td->o.open_files = td->nr_open_files;
277 err = 0;
278 clear_error(td);
279 }
Jens Axboe21972cd2006-11-23 12:39:16 +0100280 break;
Jens Axboe9bf27b42007-03-27 19:49:31 +0200281 }
Jens Axboeb5af8292007-03-08 12:43:13 +0100282
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100283 if (td->o.open_files == td->nr_open_files)
Jens Axboeb5af8292007-03-08 12:43:13 +0100284 break;
Jens Axboe21972cd2006-11-23 12:39:16 +0100285 }
286
Jens Axboe7abf8332006-11-23 15:01:19 +0100287 if (!err)
288 return 0;
289
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100290 for_each_file(td, f, i)
Jens Axboeb5af8292007-03-08 12:43:13 +0100291 td_io_close_file(td, f);
Jens Axboe7abf8332006-11-23 15:01:19 +0100292
Jens Axboe21972cd2006-11-23 12:39:16 +0100293 return err;
294}
295
Jens Axboe7bb48f82007-03-27 15:30:28 +0200296/*
297 * open/close all files, so that ->real_file_size gets set
298 */
Jens Axboebab3fd52007-04-02 10:34:18 +0200299static int get_file_sizes(struct thread_data *td)
Jens Axboe53cdc682006-10-18 11:50:58 +0200300{
301 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100302 unsigned int i;
Jens Axboebab3fd52007-04-02 10:34:18 +0200303 int err = 0;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200304
305 for_each_file(td, f, i) {
Jens Axboebab3fd52007-04-02 10:34:18 +0200306 if (td->io_ops->open_file(td, f)) {
Jens Axboe40b44f42007-04-11 12:43:35 +0200307 if (td->error != ENOENT) {
308 log_err("%s\n", td->verror);
309 err = 1;
310 }
Jens Axboe541d66d2007-03-27 19:50:11 +0200311 clear_error(td);
Jens Axboe07eb79d2007-04-12 13:02:38 +0200312 } else {
313 if (td->io_ops->close_file)
314 td->io_ops->close_file(td, f);
315 }
Jens Axboe409b3412007-03-28 09:57:01 +0200316
317 if (f->real_file_size == -1ULL && td->o.size)
318 f->real_file_size = td->o.size / td->o.nr_files;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200319 }
Jens Axboebab3fd52007-04-02 10:34:18 +0200320
321 return err;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200322}
323
324/*
325 * Open the files and setup files sizes, creating files if necessary.
326 */
327int setup_files(struct thread_data *td)
328{
329 unsigned long long total_size, extend_size;
330 struct fio_file *f;
331 unsigned int i;
Jens Axboe000b0802007-03-27 15:56:10 +0200332 int err = 0, need_extend;
Jens Axboe53cdc682006-10-18 11:50:58 +0200333
334 /*
335 * if ioengine defines a setup() method, it's responsible for
Jens Axboe7bb48f82007-03-27 15:30:28 +0200336 * opening the files and setting f->real_file_size to indicate
337 * the valid range for that file.
Jens Axboe53cdc682006-10-18 11:50:58 +0200338 */
339 if (td->io_ops->setup)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200340 err = td->io_ops->setup(td);
341 else
Jens Axboebab3fd52007-04-02 10:34:18 +0200342 err = get_file_sizes(td);
Jens Axboe53cdc682006-10-18 11:50:58 +0200343
Jens Axboef1027062006-10-20 10:14:01 +0200344 if (err)
345 return err;
346
Jens Axboe0a7eb122006-10-20 10:36:42 +0200347 /*
Jens Axboe7bb48f82007-03-27 15:30:28 +0200348 * check sizes. if the files/devices do not exist and the size
349 * isn't passed to fio, abort.
Jens Axboe0a7eb122006-10-20 10:36:42 +0200350 */
Jens Axboe7bb48f82007-03-27 15:30:28 +0200351 total_size = 0;
352 for_each_file(td, f, i) {
353 if (f->real_file_size == -1ULL)
354 total_size = -1ULL;
355 else
356 total_size += f->real_file_size;
357 }
Jens Axboe0a7eb122006-10-20 10:36:42 +0200358
Jens Axboe7bb48f82007-03-27 15:30:28 +0200359 /*
360 * device/file sizes are zero and no size given, punt
361 */
Jens Axboe409b3412007-03-28 09:57:01 +0200362 if ((!total_size || total_size == -1ULL) && !td->o.size) {
Jens Axboe7bb48f82007-03-27 15:30:28 +0200363 log_err("%s: you need to specify size=\n", td->o.name);
Jens Axboee1161c32007-02-22 19:36:48 +0100364 td_verror(td, EINVAL, "total_file_size");
Jens Axboe53cdc682006-10-18 11:50:58 +0200365 return 1;
366 }
367
Jens Axboe7bb48f82007-03-27 15:30:28 +0200368 /*
369 * now file sizes are known, so we can set ->io_size. if size= is
370 * not given, ->io_size is just equal to ->real_file_size. if size
371 * is given, ->io_size is size / nr_files.
372 */
373 extend_size = total_size = 0;
374 need_extend = 0;
375 for_each_file(td, f, i) {
376 if (!td->o.file_size_low) {
377 /*
378 * no file size range given, file size is equal to
379 * total size divided by number of files. if that is
380 * zero, set it to the real file size.
381 */
382 f->io_size = td->o.size / td->o.nr_files;
383 if (!f->io_size)
384 f->io_size = f->real_file_size;
385 } else if (f->real_file_size < td->o.file_size_low ||
386 f->real_file_size > td->o.file_size_high) {
387 /*
388 * file size given. if it's fixed, use that. if it's a
389 * range, generate a random size in-between.
390 */
391 if (td->o.file_size_low == td->o.file_size_high)
392 f->io_size = td->o.file_size_low;
393 else
394 f->io_size = get_rand_file_size(td);
395 } else
396 f->io_size = f->real_file_size;
397
398 if (f->io_size == -1ULL)
399 total_size = -1ULL;
400 else
401 total_size += f->io_size;
402
403 if (f->filetype == FIO_TYPE_FILE &&
404 f->io_size > f->real_file_size &&
405 !(td->io_ops->flags & FIO_DISKLESSIO)) {
406 need_extend++;
407 extend_size += f->io_size;
408 f->flags |= FIO_FILE_EXTEND;
409 }
410 }
411
412 if (!td->o.size)
413 td->o.size = total_size;
414
415 /*
416 * See if we need to extend some files
417 */
418 if (need_extend) {
419 temp_stall_ts = 1;
420 log_info("%s: Laying out IO file(s) (%u files / %LuMiB)\n",
421 td->o.name, need_extend, extend_size >> 20);
422
423 for_each_file(td, f, i) {
424 if (!(f->flags & FIO_FILE_EXTEND))
425 continue;
426
Jens Axboe409b3412007-03-28 09:57:01 +0200427 assert(f->filetype == FIO_TYPE_FILE);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200428 f->flags &= ~FIO_FILE_EXTEND;
429 f->real_file_size = f->io_size;
430 err = extend_file(td, f);
431 if (err)
432 break;
433 }
434 temp_stall_ts = 0;
435 }
436
437 if (err)
438 return err;
439
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100440 if (!td->o.zone_size)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200441 td->o.zone_size = td->o.size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200442
Jens Axboe7bb48f82007-03-27 15:30:28 +0200443 td->total_io_size = td->o.size * td->o.loops;
444 return 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200445}
446
Jens Axboe68727072007-03-15 20:49:25 +0100447int init_random_map(struct thread_data *td)
448{
449 int num_maps, blocks;
450 struct fio_file *f;
451 unsigned int i;
452
453 if (td->o.norandommap)
454 return 0;
455
456 for_each_file(td, f, i) {
457 blocks = (f->real_file_size + td->o.rw_min_bs - 1) / td->o.rw_min_bs;
458 num_maps = (blocks + BLOCKS_PER_MAP-1)/ BLOCKS_PER_MAP;
459 f->file_map = malloc(num_maps * sizeof(long));
460 if (!f->file_map) {
461 log_err("fio: failed allocating random map. If running a large number of jobs, try the 'norandommap' option\n");
462 return 1;
463 }
464 f->num_maps = num_maps;
465 memset(f->file_map, 0, num_maps * sizeof(long));
466 }
467
468 return 0;
469}
470
Jens Axboe53cdc682006-10-18 11:50:58 +0200471void close_files(struct thread_data *td)
472{
Jens Axboe0ab8db82006-10-18 17:16:23 +0200473 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100474 unsigned int i;
Jens Axboe53cdc682006-10-18 11:50:58 +0200475
Jens Axboe0ab8db82006-10-18 17:16:23 +0200476 for_each_file(td, f, i) {
Jens Axboea9b7b302007-04-19 16:31:16 +0200477 if (td->o.unlink && f->filetype == FIO_TYPE_FILE)
Jens Axboe132ad462006-11-23 12:23:12 +0100478 unlink(f->file_name);
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100479
Jens Axboeb5af8292007-03-08 12:43:13 +0100480 td_io_close_file(td, f);
Jens Axboeb3dc7f02007-03-09 12:52:15 +0100481
Jens Axboefa1da862007-03-23 15:20:54 +0100482 free(f->file_name);
483 f->file_name = NULL;
484
Jens Axboec3439812007-03-20 13:54:53 +0100485 if (f->file_map) {
Jens Axboeb3dc7f02007-03-09 12:52:15 +0100486 free(f->file_map);
Jens Axboec3439812007-03-20 13:54:53 +0100487 f->file_map = NULL;
488 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200489 }
Jens Axboeb4a6a592006-10-20 13:54:47 +0200490
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100491 td->o.filename = NULL;
Jens Axboecade3ef2007-03-23 15:29:45 +0100492 free(td->files);
Jens Axboeb4a6a592006-10-20 13:54:47 +0200493 td->files = NULL;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100494 td->o.nr_files = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200495}
Jens Axboeaf52b342007-03-13 10:07:47 +0100496
Jens Axboee3bab462007-03-13 11:22:09 +0100497static void get_file_type(struct fio_file *f)
Jens Axboeaf52b342007-03-13 10:07:47 +0100498{
499 struct stat sb;
500
Jens Axboe66159822007-04-16 21:54:24 +0200501 if (!strcmp(f->file_name, "-"))
502 f->filetype = FIO_TYPE_PIPE;
503 else
504 f->filetype = FIO_TYPE_FILE;
Jens Axboeaf52b342007-03-13 10:07:47 +0100505
Jens Axboee3bab462007-03-13 11:22:09 +0100506 if (!lstat(f->file_name, &sb)) {
Jens Axboeaf52b342007-03-13 10:07:47 +0100507 if (S_ISBLK(sb.st_mode))
508 f->filetype = FIO_TYPE_BD;
509 else if (S_ISCHR(sb.st_mode))
510 f->filetype = FIO_TYPE_CHAR;
Jens Axboeb5605e92007-04-16 20:42:33 +0200511 else if (S_ISFIFO(sb.st_mode))
512 f->filetype = FIO_TYPE_PIPE;
Jens Axboeaf52b342007-03-13 10:07:47 +0100513 }
514}
515
516void add_file(struct thread_data *td, const char *fname)
517{
Jens Axboe7b4e4fe2007-03-13 12:51:40 +0100518 int cur_files = td->files_index;
Jens Axboebd0ee742007-03-23 14:26:23 +0100519 char file_name[PATH_MAX];
Jens Axboeaf52b342007-03-13 10:07:47 +0100520 struct fio_file *f;
Jens Axboebd0ee742007-03-23 14:26:23 +0100521 int len = 0;
Jens Axboeaf52b342007-03-13 10:07:47 +0100522
523 td->files = realloc(td->files, (cur_files + 1) * sizeof(*f));
524
525 f = &td->files[cur_files];
526 memset(f, 0, sizeof(*f));
527 f->fd = -1;
Jens Axboebd0ee742007-03-23 14:26:23 +0100528
Jens Axboe07eb79d2007-04-12 13:02:38 +0200529 /*
530 * init function, io engine may not be loaded yet
531 */
532 if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO))
533 f->real_file_size = -1ULL;
534
Jens Axboebd0ee742007-03-23 14:26:23 +0100535 if (td->o.directory)
536 len = sprintf(file_name, "%s/", td->o.directory);
537
538 sprintf(file_name + len, "%s", fname);
539 f->file_name = strdup(file_name);
Jens Axboeaf52b342007-03-13 10:07:47 +0100540
Jens Axboee3bab462007-03-13 11:22:09 +0100541 get_file_type(f);
Jens Axboeaf52b342007-03-13 10:07:47 +0100542
Jens Axboe7b4e4fe2007-03-13 12:51:40 +0100543 td->files_index++;
Jens Axboe15494412007-03-13 11:52:47 +0100544 if (f->filetype == FIO_TYPE_FILE)
545 td->nr_normal_files++;
Jens Axboeaf52b342007-03-13 10:07:47 +0100546}
Jens Axboe0ad920e2007-03-13 11:06:45 +0100547
548void get_file(struct fio_file *f)
549{
Jens Axboe97af62c2007-05-22 11:12:13 +0200550 assert(f->flags & FIO_FILE_OPEN);
Jens Axboe0ad920e2007-03-13 11:06:45 +0100551 f->references++;
552}
553
554void put_file(struct thread_data *td, struct fio_file *f)
555{
556 if (!(f->flags & FIO_FILE_OPEN))
557 return;
558
559 assert(f->references);
560 if (--f->references)
561 return;
562
Jens Axboed424d4d2007-04-17 09:05:10 +0200563 if (should_fsync(td) && td->o.fsync_on_close)
Jens Axboeebb14152007-03-13 14:42:15 +0100564 fsync(f->fd);
565
Jens Axboe0ad920e2007-03-13 11:06:45 +0100566 if (td->io_ops->close_file)
567 td->io_ops->close_file(td, f);
Jens Axboe1020a132007-03-29 11:15:06 +0200568
Jens Axboe0ad920e2007-03-13 11:06:45 +0100569 td->nr_open_files--;
570 f->flags &= ~FIO_FILE_OPEN;
571}
Jens Axboebbf6b542007-03-13 15:28:55 +0100572
573static int recurse_dir(struct thread_data *td, const char *dirname)
574{
575 struct dirent *dir;
576 int ret = 0;
577 DIR *D;
578
579 D = opendir(dirname);
580 if (!D) {
Jens Axboe0ddb2702007-03-27 19:43:53 +0200581 char buf[FIO_VERROR_SIZE];
582
583 snprintf(buf, FIO_VERROR_SIZE - 1, "opendir(%s)", dirname);
584 td_verror(td, errno, buf);
Jens Axboebbf6b542007-03-13 15:28:55 +0100585 return 1;
586 }
587
588 while ((dir = readdir(D)) != NULL) {
589 char full_path[PATH_MAX];
590 struct stat sb;
591
Jens Axboee85b2b82007-03-14 09:39:06 +0100592 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
593 continue;
Jens Axboe96d32d52007-03-14 09:16:09 +0100594
Jens Axboebbf6b542007-03-13 15:28:55 +0100595 sprintf(full_path, "%s/%s", dirname, dir->d_name);
596
597 if (lstat(full_path, &sb) == -1) {
598 if (errno != ENOENT) {
599 td_verror(td, errno, "stat");
600 return 1;
601 }
602 }
603
604 if (S_ISREG(sb.st_mode)) {
605 add_file(td, full_path);
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100606 td->o.nr_files++;
Jens Axboebbf6b542007-03-13 15:28:55 +0100607 continue;
608 }
Jens Axboe0ddb2702007-03-27 19:43:53 +0200609 if (!S_ISDIR(sb.st_mode))
610 continue;
Jens Axboebbf6b542007-03-13 15:28:55 +0100611
Jens Axboebbf6b542007-03-13 15:28:55 +0100612 if ((ret = recurse_dir(td, full_path)) != 0)
613 break;
614 }
615
616 closedir(D);
617 return ret;
618}
619
620int add_dir_files(struct thread_data *td, const char *path)
621{
Jens Axboe0ddb2702007-03-27 19:43:53 +0200622 int ret = recurse_dir(td, path);
623
624 if (!ret)
625 log_info("fio: opendir added %d files\n", td->o.nr_files);
626
627 return ret;
Jens Axboebbf6b542007-03-13 15:28:55 +0100628}
Jens Axboecade3ef2007-03-23 15:29:45 +0100629
630void dup_files(struct thread_data *td, struct thread_data *org)
631{
632 struct fio_file *f;
633 unsigned int i;
634 size_t bytes;
635
636 if (!org->files)
637 return;
638
639 bytes = org->files_index * sizeof(*f);
640 td->files = malloc(bytes);
641 memcpy(td->files, org->files, bytes);
642
643 for_each_file(td, f, i) {
644 if (f->file_name)
645 f->file_name = strdup(f->file_name);
646 }
647}