blob: c99d5b36dad8195129d40c048eb966d36caebae0 [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 }
184 } else if (f->filetype == FIO_TYPE_CHAR)
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 Axboe53cdc682006-10-18 11:50:58 +0200203 int flags = 0;
204
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100205 if (td->o.odirect)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100206 flags |= OS_O_DIRECT;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100207 if (td->o.sync_io)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100208 flags |= O_SYNC;
Jens Axboe7abf8332006-11-23 15:01:19 +0100209
Jens Axboe2fd233b2007-03-02 08:44:25 +0100210 if (td_write(td) || td_rw(td)) {
211 flags |= O_RDWR;
Jens Axboe53cdc682006-10-18 11:50:58 +0200212
Jens Axboeaf52b342007-03-13 10:07:47 +0100213 if (f->filetype == FIO_TYPE_FILE)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100214 flags |= O_CREAT;
Jens Axboe2fd233b2007-03-02 08:44:25 +0100215
Jens Axboeb5af8292007-03-08 12:43:13 +0100216 f->fd = open(f->file_name, flags, 0600);
Jens Axboe2fd233b2007-03-02 08:44:25 +0100217 } else {
Jens Axboeaf52b342007-03-13 10:07:47 +0100218 if (f->filetype == FIO_TYPE_CHAR)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100219 flags |= O_RDWR;
220 else
221 flags |= O_RDONLY;
222
Jens Axboeb5af8292007-03-08 12:43:13 +0100223 f->fd = open(f->file_name, flags);
Jens Axboe53cdc682006-10-18 11:50:58 +0200224 }
225
226 if (f->fd == -1) {
Jens Axboee4e33252007-03-21 13:07:54 +0100227 char buf[FIO_VERROR_SIZE];
Jens Axboee1161c32007-02-22 19:36:48 +0100228 int __e = errno;
229
Jens Axboee4e33252007-03-21 13:07:54 +0100230 snprintf(buf, sizeof(buf) - 1, "open(%s)", f->file_name);
231
232 td_verror(td, __e, buf);
Jens Axboe53cdc682006-10-18 11:50:58 +0200233 }
234
Jens Axboeb5af8292007-03-08 12:43:13 +0100235 if (get_file_size(td, f))
236 goto err;
237
Jens Axboe53cdc682006-10-18 11:50:58 +0200238 return 0;
Jens Axboeb5af8292007-03-08 12:43:13 +0100239err:
240 close(f->fd);
241 return 1;
242}
243
Jens Axboe21972cd2006-11-23 12:39:16 +0100244int open_files(struct thread_data *td)
245{
246 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100247 unsigned int i;
248 int err = 0;
Jens Axboe21972cd2006-11-23 12:39:16 +0100249
250 for_each_file(td, f, i) {
Jens Axboeb5af8292007-03-08 12:43:13 +0100251 err = td_io_open_file(td, f);
Jens Axboe9bf27b42007-03-27 19:49:31 +0200252 if (err) {
253 if (td->error == EMFILE) {
254 log_err("fio: limited open files to: %d\n", td->nr_open_files);
255 td->o.open_files = td->nr_open_files;
256 err = 0;
257 clear_error(td);
258 }
Jens Axboe21972cd2006-11-23 12:39:16 +0100259 break;
Jens Axboe9bf27b42007-03-27 19:49:31 +0200260 }
Jens Axboeb5af8292007-03-08 12:43:13 +0100261
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100262 if (td->o.open_files == td->nr_open_files)
Jens Axboeb5af8292007-03-08 12:43:13 +0100263 break;
Jens Axboe21972cd2006-11-23 12:39:16 +0100264 }
265
Jens Axboe7abf8332006-11-23 15:01:19 +0100266 if (!err)
267 return 0;
268
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100269 for_each_file(td, f, i)
Jens Axboeb5af8292007-03-08 12:43:13 +0100270 td_io_close_file(td, f);
Jens Axboe7abf8332006-11-23 15:01:19 +0100271
Jens Axboe21972cd2006-11-23 12:39:16 +0100272 return err;
273}
274
Jens Axboe7bb48f82007-03-27 15:30:28 +0200275/*
276 * open/close all files, so that ->real_file_size gets set
277 */
Jens Axboebab3fd52007-04-02 10:34:18 +0200278static int get_file_sizes(struct thread_data *td)
Jens Axboe53cdc682006-10-18 11:50:58 +0200279{
280 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100281 unsigned int i;
Jens Axboebab3fd52007-04-02 10:34:18 +0200282 int err = 0;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200283
284 for_each_file(td, f, i) {
Jens Axboebab3fd52007-04-02 10:34:18 +0200285 if (td->io_ops->open_file(td, f)) {
286 log_err("%s\n", td->verror);
287 err = 1;
Jens Axboe541d66d2007-03-27 19:50:11 +0200288 clear_error(td);
Jens Axboebab3fd52007-04-02 10:34:18 +0200289 } else
Jens Axboe000b0802007-03-27 15:56:10 +0200290 td->io_ops->close_file(td, f);
Jens Axboe409b3412007-03-28 09:57:01 +0200291
292 if (f->real_file_size == -1ULL && td->o.size)
293 f->real_file_size = td->o.size / td->o.nr_files;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200294 }
Jens Axboebab3fd52007-04-02 10:34:18 +0200295
296 return err;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200297}
298
299/*
300 * Open the files and setup files sizes, creating files if necessary.
301 */
302int setup_files(struct thread_data *td)
303{
304 unsigned long long total_size, extend_size;
305 struct fio_file *f;
306 unsigned int i;
Jens Axboe000b0802007-03-27 15:56:10 +0200307 int err = 0, need_extend;
Jens Axboe53cdc682006-10-18 11:50:58 +0200308
309 /*
310 * if ioengine defines a setup() method, it's responsible for
Jens Axboe7bb48f82007-03-27 15:30:28 +0200311 * opening the files and setting f->real_file_size to indicate
312 * the valid range for that file.
Jens Axboe53cdc682006-10-18 11:50:58 +0200313 */
314 if (td->io_ops->setup)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200315 err = td->io_ops->setup(td);
316 else
Jens Axboebab3fd52007-04-02 10:34:18 +0200317 err = get_file_sizes(td);
Jens Axboe53cdc682006-10-18 11:50:58 +0200318
Jens Axboef1027062006-10-20 10:14:01 +0200319 if (err)
320 return err;
321
Jens Axboe0a7eb122006-10-20 10:36:42 +0200322 /*
Jens Axboe7bb48f82007-03-27 15:30:28 +0200323 * check sizes. if the files/devices do not exist and the size
324 * isn't passed to fio, abort.
Jens Axboe0a7eb122006-10-20 10:36:42 +0200325 */
Jens Axboe7bb48f82007-03-27 15:30:28 +0200326 total_size = 0;
327 for_each_file(td, f, i) {
328 if (f->real_file_size == -1ULL)
329 total_size = -1ULL;
330 else
331 total_size += f->real_file_size;
332 }
Jens Axboe0a7eb122006-10-20 10:36:42 +0200333
Jens Axboe7bb48f82007-03-27 15:30:28 +0200334 /*
335 * device/file sizes are zero and no size given, punt
336 */
Jens Axboe409b3412007-03-28 09:57:01 +0200337 if ((!total_size || total_size == -1ULL) && !td->o.size) {
Jens Axboe7bb48f82007-03-27 15:30:28 +0200338 log_err("%s: you need to specify size=\n", td->o.name);
Jens Axboee1161c32007-02-22 19:36:48 +0100339 td_verror(td, EINVAL, "total_file_size");
Jens Axboe53cdc682006-10-18 11:50:58 +0200340 return 1;
341 }
342
Jens Axboe7bb48f82007-03-27 15:30:28 +0200343 /*
344 * now file sizes are known, so we can set ->io_size. if size= is
345 * not given, ->io_size is just equal to ->real_file_size. if size
346 * is given, ->io_size is size / nr_files.
347 */
348 extend_size = total_size = 0;
349 need_extend = 0;
350 for_each_file(td, f, i) {
351 if (!td->o.file_size_low) {
352 /*
353 * no file size range given, file size is equal to
354 * total size divided by number of files. if that is
355 * zero, set it to the real file size.
356 */
357 f->io_size = td->o.size / td->o.nr_files;
358 if (!f->io_size)
359 f->io_size = f->real_file_size;
360 } else if (f->real_file_size < td->o.file_size_low ||
361 f->real_file_size > td->o.file_size_high) {
362 /*
363 * file size given. if it's fixed, use that. if it's a
364 * range, generate a random size in-between.
365 */
366 if (td->o.file_size_low == td->o.file_size_high)
367 f->io_size = td->o.file_size_low;
368 else
369 f->io_size = get_rand_file_size(td);
370 } else
371 f->io_size = f->real_file_size;
372
373 if (f->io_size == -1ULL)
374 total_size = -1ULL;
375 else
376 total_size += f->io_size;
377
378 if (f->filetype == FIO_TYPE_FILE &&
379 f->io_size > f->real_file_size &&
380 !(td->io_ops->flags & FIO_DISKLESSIO)) {
381 need_extend++;
382 extend_size += f->io_size;
383 f->flags |= FIO_FILE_EXTEND;
384 }
385 }
386
387 if (!td->o.size)
388 td->o.size = total_size;
389
390 /*
391 * See if we need to extend some files
392 */
393 if (need_extend) {
394 temp_stall_ts = 1;
395 log_info("%s: Laying out IO file(s) (%u files / %LuMiB)\n",
396 td->o.name, need_extend, extend_size >> 20);
397
398 for_each_file(td, f, i) {
399 if (!(f->flags & FIO_FILE_EXTEND))
400 continue;
401
Jens Axboe409b3412007-03-28 09:57:01 +0200402 assert(f->filetype == FIO_TYPE_FILE);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200403 f->flags &= ~FIO_FILE_EXTEND;
404 f->real_file_size = f->io_size;
405 err = extend_file(td, f);
406 if (err)
407 break;
408 }
409 temp_stall_ts = 0;
410 }
411
412 if (err)
413 return err;
414
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100415 if (!td->o.zone_size)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200416 td->o.zone_size = td->o.size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200417
Jens Axboe7bb48f82007-03-27 15:30:28 +0200418 td->total_io_size = td->o.size * td->o.loops;
419 return 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200420}
421
Jens Axboe68727072007-03-15 20:49:25 +0100422int init_random_map(struct thread_data *td)
423{
424 int num_maps, blocks;
425 struct fio_file *f;
426 unsigned int i;
427
428 if (td->o.norandommap)
429 return 0;
430
431 for_each_file(td, f, i) {
432 blocks = (f->real_file_size + td->o.rw_min_bs - 1) / td->o.rw_min_bs;
433 num_maps = (blocks + BLOCKS_PER_MAP-1)/ BLOCKS_PER_MAP;
434 f->file_map = malloc(num_maps * sizeof(long));
435 if (!f->file_map) {
436 log_err("fio: failed allocating random map. If running a large number of jobs, try the 'norandommap' option\n");
437 return 1;
438 }
439 f->num_maps = num_maps;
440 memset(f->file_map, 0, num_maps * sizeof(long));
441 }
442
443 return 0;
444}
445
Jens Axboe53cdc682006-10-18 11:50:58 +0200446void close_files(struct thread_data *td)
447{
Jens Axboe0ab8db82006-10-18 17:16:23 +0200448 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100449 unsigned int i;
Jens Axboe53cdc682006-10-18 11:50:58 +0200450
Jens Axboe0ab8db82006-10-18 17:16:23 +0200451 for_each_file(td, f, i) {
Jens Axboefa1da862007-03-23 15:20:54 +0100452 if ((f->flags & FIO_FILE_UNLINK) &&
453 f->filetype == FIO_TYPE_FILE)
Jens Axboe132ad462006-11-23 12:23:12 +0100454 unlink(f->file_name);
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100455
Jens Axboeb5af8292007-03-08 12:43:13 +0100456 td_io_close_file(td, f);
Jens Axboeb3dc7f02007-03-09 12:52:15 +0100457
Jens Axboefa1da862007-03-23 15:20:54 +0100458 free(f->file_name);
459 f->file_name = NULL;
460
Jens Axboec3439812007-03-20 13:54:53 +0100461 if (f->file_map) {
Jens Axboeb3dc7f02007-03-09 12:52:15 +0100462 free(f->file_map);
Jens Axboec3439812007-03-20 13:54:53 +0100463 f->file_map = NULL;
464 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200465 }
Jens Axboeb4a6a592006-10-20 13:54:47 +0200466
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100467 td->o.filename = NULL;
Jens Axboecade3ef2007-03-23 15:29:45 +0100468 free(td->files);
Jens Axboeb4a6a592006-10-20 13:54:47 +0200469 td->files = NULL;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100470 td->o.nr_files = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200471}
Jens Axboeaf52b342007-03-13 10:07:47 +0100472
Jens Axboee3bab462007-03-13 11:22:09 +0100473static void get_file_type(struct fio_file *f)
Jens Axboeaf52b342007-03-13 10:07:47 +0100474{
475 struct stat sb;
476
477 f->filetype = FIO_TYPE_FILE;
478
Jens Axboee3bab462007-03-13 11:22:09 +0100479 if (!lstat(f->file_name, &sb)) {
Jens Axboeaf52b342007-03-13 10:07:47 +0100480 if (S_ISBLK(sb.st_mode))
481 f->filetype = FIO_TYPE_BD;
482 else if (S_ISCHR(sb.st_mode))
483 f->filetype = FIO_TYPE_CHAR;
484 }
485}
486
487void add_file(struct thread_data *td, const char *fname)
488{
Jens Axboe7b4e4fe2007-03-13 12:51:40 +0100489 int cur_files = td->files_index;
Jens Axboebd0ee742007-03-23 14:26:23 +0100490 char file_name[PATH_MAX];
Jens Axboeaf52b342007-03-13 10:07:47 +0100491 struct fio_file *f;
Jens Axboebd0ee742007-03-23 14:26:23 +0100492 int len = 0;
Jens Axboeaf52b342007-03-13 10:07:47 +0100493
494 td->files = realloc(td->files, (cur_files + 1) * sizeof(*f));
495
496 f = &td->files[cur_files];
497 memset(f, 0, sizeof(*f));
498 f->fd = -1;
Jens Axboebd0ee742007-03-23 14:26:23 +0100499
500 if (td->o.directory)
501 len = sprintf(file_name, "%s/", td->o.directory);
502
503 sprintf(file_name + len, "%s", fname);
504 f->file_name = strdup(file_name);
Jens Axboeaf52b342007-03-13 10:07:47 +0100505
Jens Axboee3bab462007-03-13 11:22:09 +0100506 get_file_type(f);
Jens Axboeaf52b342007-03-13 10:07:47 +0100507
Jens Axboe7b4e4fe2007-03-13 12:51:40 +0100508 td->files_index++;
Jens Axboe15494412007-03-13 11:52:47 +0100509 if (f->filetype == FIO_TYPE_FILE)
510 td->nr_normal_files++;
Jens Axboeaf52b342007-03-13 10:07:47 +0100511}
Jens Axboe0ad920e2007-03-13 11:06:45 +0100512
513void get_file(struct fio_file *f)
514{
515 f->references++;
516}
517
518void put_file(struct thread_data *td, struct fio_file *f)
519{
520 if (!(f->flags & FIO_FILE_OPEN))
521 return;
522
523 assert(f->references);
524 if (--f->references)
525 return;
526
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100527 if (should_fsync(td) && td->o.fsync_on_close)
Jens Axboeebb14152007-03-13 14:42:15 +0100528 fsync(f->fd);
529
Jens Axboe0ad920e2007-03-13 11:06:45 +0100530 if (td->io_ops->close_file)
531 td->io_ops->close_file(td, f);
Jens Axboe1020a132007-03-29 11:15:06 +0200532
Jens Axboe0ad920e2007-03-13 11:06:45 +0100533 td->nr_open_files--;
534 f->flags &= ~FIO_FILE_OPEN;
535}
Jens Axboebbf6b542007-03-13 15:28:55 +0100536
537static int recurse_dir(struct thread_data *td, const char *dirname)
538{
539 struct dirent *dir;
540 int ret = 0;
541 DIR *D;
542
543 D = opendir(dirname);
544 if (!D) {
Jens Axboe0ddb2702007-03-27 19:43:53 +0200545 char buf[FIO_VERROR_SIZE];
546
547 snprintf(buf, FIO_VERROR_SIZE - 1, "opendir(%s)", dirname);
548 td_verror(td, errno, buf);
Jens Axboebbf6b542007-03-13 15:28:55 +0100549 return 1;
550 }
551
552 while ((dir = readdir(D)) != NULL) {
553 char full_path[PATH_MAX];
554 struct stat sb;
555
Jens Axboee85b2b82007-03-14 09:39:06 +0100556 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
557 continue;
Jens Axboe96d32d52007-03-14 09:16:09 +0100558
Jens Axboebbf6b542007-03-13 15:28:55 +0100559 sprintf(full_path, "%s/%s", dirname, dir->d_name);
560
561 if (lstat(full_path, &sb) == -1) {
562 if (errno != ENOENT) {
563 td_verror(td, errno, "stat");
564 return 1;
565 }
566 }
567
568 if (S_ISREG(sb.st_mode)) {
569 add_file(td, full_path);
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100570 td->o.nr_files++;
Jens Axboebbf6b542007-03-13 15:28:55 +0100571 continue;
572 }
Jens Axboe0ddb2702007-03-27 19:43:53 +0200573 if (!S_ISDIR(sb.st_mode))
574 continue;
Jens Axboebbf6b542007-03-13 15:28:55 +0100575
Jens Axboebbf6b542007-03-13 15:28:55 +0100576 if ((ret = recurse_dir(td, full_path)) != 0)
577 break;
578 }
579
580 closedir(D);
581 return ret;
582}
583
584int add_dir_files(struct thread_data *td, const char *path)
585{
Jens Axboe0ddb2702007-03-27 19:43:53 +0200586 int ret = recurse_dir(td, path);
587
588 if (!ret)
589 log_info("fio: opendir added %d files\n", td->o.nr_files);
590
591 return ret;
Jens Axboebbf6b542007-03-13 15:28:55 +0100592}
Jens Axboecade3ef2007-03-23 15:29:45 +0100593
594void dup_files(struct thread_data *td, struct thread_data *org)
595{
596 struct fio_file *f;
597 unsigned int i;
598 size_t bytes;
599
600 if (!org->files)
601 return;
602
603 bytes = org->files_index * sizeof(*f);
604 td->files = malloc(bytes);
605 memcpy(td->files, org->files, bytes);
606
607 for_each_file(td, f, i) {
608 if (f->file_name)
609 f->file_name = strdup(f->file_name);
610 }
611}