blob: b356b447ddaeadffa01f4d551ea868621cff3220 [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 Axboef17c4392008-03-01 18:40:46 +010011#include "smalloc.h"
Jens Axboe4906e0b2008-03-01 18:59:51 +010012#include "filehash.h"
Jens Axboe53cdc682006-10-18 11:50:58 +020013
Jens Axboe7172cfe2007-07-23 14:36:00 +020014static int root_warn;
15
Jens Axboe7bb48f82007-03-27 15:30:28 +020016static int extend_file(struct thread_data *td, struct fio_file *f)
Jens Axboe25205e92006-10-19 20:50:14 +020017{
Jens Axboeea443652007-03-29 14:52:13 +020018 int r, new_layout = 0, unlink_file = 0, flags;
Jens Axboe25205e92006-10-19 20:50:14 +020019 unsigned long long left;
20 unsigned int bs;
21 char *b;
Jens Axboeb2a15192006-10-18 14:10:42 +020022
Jens Axboe4241ea82007-09-12 08:18:36 +020023 if (read_only) {
24 log_err("fio: refusing extend of file due to read-only\n");
25 return 0;
26 }
27
Jens Axboe507a7022007-03-27 15:52:46 +020028 /*
29 * check if we need to lay the file out complete again. fio
30 * does that for operations involving reads, or for writes
31 * where overwrite is set
32 */
Jens Axboeffdfabd2008-03-26 09:18:14 +010033 if (td_read(td) || (td_write(td) && td->o.overwrite) ||
34 (td_write(td) && td->io_ops->flags & FIO_NOEXTEND))
Jens Axboe507a7022007-03-27 15:52:46 +020035 new_layout = 1;
Jens Axboeea443652007-03-29 14:52:13 +020036 if (td_write(td) && !td->o.overwrite)
37 unlink_file = 1;
Jens Axboe507a7022007-03-27 15:52:46 +020038
Jens Axboe6ae1f572008-02-21 10:20:00 +010039 if (unlink_file || new_layout) {
Jens Axboebd199f22008-03-26 09:57:18 +010040 dprint(FD_FILE, "layout unlink %s\n", f->file_name);
Zhang, Yanmin982016d2008-02-26 15:35:52 +010041 if ((unlink(f->file_name) < 0) && (errno != ENOENT)) {
Jens Axboe7bb48f82007-03-27 15:30:28 +020042 td_verror(td, errno, "unlink");
43 return 1;
44 }
45 }
46
Jens Axboe507a7022007-03-27 15:52:46 +020047 flags = O_WRONLY | O_CREAT;
48 if (new_layout)
49 flags |= O_TRUNC;
50
Jens Axboeee56ad52008-02-01 10:30:20 +010051 dprint(FD_FILE, "open file %s, flags %x\n", f->file_name, flags);
Jens Axboe507a7022007-03-27 15:52:46 +020052 f->fd = open(f->file_name, flags, 0644);
Jens Axboe53cdc682006-10-18 11:50:58 +020053 if (f->fd < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +010054 td_verror(td, errno, "open");
Jens Axboe53cdc682006-10-18 11:50:58 +020055 return 1;
56 }
57
Shawn Lewisfc74ac12008-01-11 09:45:11 +010058 if (!new_layout)
59 goto done;
60
Jens Axboeee56ad52008-02-01 10:30:20 +010061 dprint(FD_FILE, "truncate file %s, size %llu\n", f->file_name,
62 f->real_file_size);
Jens Axboe7bb48f82007-03-27 15:30:28 +020063 if (ftruncate(f->fd, f->real_file_size) == -1) {
Jens Axboee1161c32007-02-22 19:36:48 +010064 td_verror(td, errno, "ftruncate");
Jens Axboe53cdc682006-10-18 11:50:58 +020065 goto err;
66 }
67
Jens Axboeee56ad52008-02-01 10:30:20 +010068 dprint(FD_FILE, "fallocate file %s, size %llu\n", f->file_name,
69 f->real_file_size);
Jens Axboe7bb48f82007-03-27 15:30:28 +020070 if (posix_fallocate(f->fd, 0, f->real_file_size) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +010071 td_verror(td, errno, "posix_fallocate");
Jens Axboe40f82982006-10-25 11:21:05 +020072 goto err;
73 }
74
Jens Axboe2dc1bbe2007-03-15 15:01:33 +010075 b = malloc(td->o.max_bs[DDIR_WRITE]);
76 memset(b, 0, td->o.max_bs[DDIR_WRITE]);
Jens Axboe53cdc682006-10-18 11:50:58 +020077
Jens Axboe7bb48f82007-03-27 15:30:28 +020078 left = f->real_file_size;
Jens Axboe53cdc682006-10-18 11:50:58 +020079 while (left && !td->terminate) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +010080 bs = td->o.max_bs[DDIR_WRITE];
Jens Axboe53cdc682006-10-18 11:50:58 +020081 if (bs > left)
82 bs = left;
83
84 r = write(f->fd, b, bs);
85
86 if (r == (int) bs) {
87 left -= bs;
88 continue;
89 } else {
90 if (r < 0)
Jens Axboee1161c32007-02-22 19:36:48 +010091 td_verror(td, errno, "write");
Jens Axboe53cdc682006-10-18 11:50:58 +020092 else
Jens Axboee1161c32007-02-22 19:36:48 +010093 td_verror(td, EIO, "write");
Jens Axboe53cdc682006-10-18 11:50:58 +020094
95 break;
96 }
97 }
98
Jens Axboeffdfabd2008-03-26 09:18:14 +010099 if (td->terminate) {
100 dprint(FD_FILE, "terminate unlink %s\n", f->file_name);
Jens Axboe53cdc682006-10-18 11:50:58 +0200101 unlink(f->file_name);
Jens Axboeffdfabd2008-03-26 09:18:14 +0100102 } else if (td->o.create_fsync) {
Jens Axboe98e1ac42008-03-06 11:56:48 +0100103 if (fsync(f->fd) < 0) {
104 td_verror(td, errno, "fsync");
105 goto err;
106 }
107 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200108
109 free(b);
Jens Axboe507a7022007-03-27 15:52:46 +0200110done:
Jens Axboe53cdc682006-10-18 11:50:58 +0200111 close(f->fd);
112 f->fd = -1;
113 return 0;
114err:
115 close(f->fd);
116 f->fd = -1;
117 return 1;
118}
119
Jens Axboe7bb48f82007-03-27 15:30:28 +0200120static unsigned long long get_rand_file_size(struct thread_data *td)
Jens Axboe9c60ce62007-03-15 09:14:47 +0100121{
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100122 unsigned long long ret, size_d;
Jens Axboe9c60ce62007-03-15 09:14:47 +0100123 long r;
124
Jens Axboe9c60ce62007-03-15 09:14:47 +0100125 r = os_random_long(&td->file_size_state);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100126 size_d = td->o.file_size_high - td->o.file_size_low;
127 ret = (unsigned long long) ((double) size_d * (r / (RAND_MAX + 1.0)));
128 ret += td->o.file_size_low;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100129 ret -= (ret % td->o.rw_min_bs);
Jens Axboe9c60ce62007-03-15 09:14:47 +0100130 return ret;
131}
132
Jens Axboe53cdc682006-10-18 11:50:58 +0200133static int file_size(struct thread_data *td, struct fio_file *f)
134{
135 struct stat st;
136
Jens Axboe7bb48f82007-03-27 15:30:28 +0200137 if (fstat(f->fd, &st) == -1) {
138 td_verror(td, errno, "fstat");
139 return 1;
140 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200141
Jens Axboe7bb48f82007-03-27 15:30:28 +0200142 f->real_file_size = st.st_size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200143 return 0;
144}
145
146static int bdev_size(struct thread_data *td, struct fio_file *f)
147{
148 unsigned long long bytes;
149 int r;
150
151 r = blockdev_size(f->fd, &bytes);
152 if (r) {
Jens Axboee1161c32007-02-22 19:36:48 +0100153 td_verror(td, r, "blockdev_size");
Jens Axboe53cdc682006-10-18 11:50:58 +0200154 return 1;
155 }
156
157 f->real_file_size = bytes;
Jens Axboe53cdc682006-10-18 11:50:58 +0200158 return 0;
159}
160
161static int get_file_size(struct thread_data *td, struct fio_file *f)
162{
163 int ret = 0;
164
Jens Axboe409b3412007-03-28 09:57:01 +0200165 if (f->flags & FIO_SIZE_KNOWN)
166 return 0;
167
Jens Axboe7bb48f82007-03-27 15:30:28 +0200168 if (f->filetype == FIO_TYPE_FILE)
169 ret = file_size(td, f);
170 else if (f->filetype == FIO_TYPE_BD)
Jens Axboe53cdc682006-10-18 11:50:58 +0200171 ret = bdev_size(td, f);
172 else
173 f->real_file_size = -1;
174
175 if (ret)
176 return ret;
177
178 if (f->file_offset > f->real_file_size) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100179 log_err("%s: offset extends end (%Lu > %Lu)\n", td->o.name,
180 f->file_offset, f->real_file_size);
Jens Axboe53cdc682006-10-18 11:50:58 +0200181 return 1;
182 }
183
Jens Axboe409b3412007-03-28 09:57:01 +0200184 f->flags |= FIO_SIZE_KNOWN;
Jens Axboe53cdc682006-10-18 11:50:58 +0200185 return 0;
186}
187
Jens Axboee5b401d2006-10-18 16:03:40 +0200188int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
189{
190 int ret = 0;
191
Jens Axboeee56ad52008-02-01 10:30:20 +0100192 dprint(FD_IO, "invalidate cache (%d)\n", td->o.odirect);
193
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100194 if (td->o.odirect)
Jens Axboeb5af8292007-03-08 12:43:13 +0100195 return 0;
196
Jens Axboee5b401d2006-10-18 16:03:40 +0200197 /*
198 * FIXME: add blockdev flushing too
199 */
Jens Axboeb5af8292007-03-08 12:43:13 +0100200 if (f->mmap)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200201 ret = madvise(f->mmap, f->io_size, MADV_DONTNEED);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100202 else if (f->filetype == FIO_TYPE_FILE) {
203 ret = fadvise(f->fd, f->file_offset, f->io_size,
204 POSIX_FADV_DONTNEED);
205 } else if (f->filetype == FIO_TYPE_BD) {
Jens Axboeb5af8292007-03-08 12:43:13 +0100206 ret = blockdev_invalidate_cache(f->fd);
Jens Axboe7e0e25c2007-03-27 12:49:56 +0200207 if (ret < 0 && errno == EACCES && geteuid()) {
Jens Axboe7172cfe2007-07-23 14:36:00 +0200208 if (!root_warn) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100209 log_err("fio: only root may flush block "
210 "devices. Cache flush bypassed!\n");
Jens Axboe7172cfe2007-07-23 14:36:00 +0200211 root_warn = 1;
212 }
Jens Axboe7e0e25c2007-03-27 12:49:56 +0200213 ret = 0;
214 }
Jens Axboeb5605e92007-04-16 20:42:33 +0200215 } else if (f->filetype == FIO_TYPE_CHAR || f->filetype == FIO_TYPE_PIPE)
Jens Axboee5b401d2006-10-18 16:03:40 +0200216 ret = 0;
217
218 if (ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100219 td_verror(td, errno, "invalidate_cache");
Jens Axboee5b401d2006-10-18 16:03:40 +0200220 return 1;
221 }
222
Jens Axboead2da602007-02-26 12:33:27 +0100223 return ret;
Jens Axboee5b401d2006-10-18 16:03:40 +0200224}
225
Jens Axboe6977bcd2008-03-01 15:55:36 +0100226int generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
Jens Axboe53cdc682006-10-18 11:50:58 +0200227{
Jens Axboe6977bcd2008-03-01 15:55:36 +0100228 int ret = 0;
229
Jens Axboeee56ad52008-02-01 10:30:20 +0100230 dprint(FD_FILE, "fd close %s\n", f->file_name);
Jens Axboe4906e0b2008-03-01 18:59:51 +0100231
232 remove_file_hash(f);
233
Jens Axboe6977bcd2008-03-01 15:55:36 +0100234 if (close(f->fd) < 0)
235 ret = errno;
236
Jens Axboeb5af8292007-03-08 12:43:13 +0100237 f->fd = -1;
Jens Axboe6977bcd2008-03-01 15:55:36 +0100238 return ret;
Jens Axboe53cdc682006-10-18 11:50:58 +0200239}
240
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100241static int file_lookup_open(struct fio_file *f, int flags)
Jens Axboe53cdc682006-10-18 11:50:58 +0200242{
Jens Axboe29c13492008-03-01 19:25:20 +0100243 struct fio_file *__f;
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100244 int from_hash;
245
246 __f = lookup_file_hash(f->file_name);
247 if (__f) {
Jens Axboe9efef3c2008-03-06 10:26:25 +0100248 dprint(FD_FILE, "found file in hash %s\n", f->file_name);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100249 /*
250 * racy, need the __f->lock locked
251 */
252 f->lock = __f->lock;
253 f->lock_owner = __f->lock_owner;
254 f->lock_batch = __f->lock_batch;
255 f->lock_ddir = __f->lock_ddir;
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100256 from_hash = 1;
257 } else {
Jens Axboe9efef3c2008-03-06 10:26:25 +0100258 dprint(FD_FILE, "file not found in hash %s\n", f->file_name);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100259 from_hash = 0;
260 }
261
Jens Axboee8670ef2008-03-06 12:06:30 +0100262 f->fd = open(f->file_name, flags, 0600);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100263 return from_hash;
264}
265
266int generic_open_file(struct thread_data *td, struct fio_file *f)
267{
Jens Axboe66159822007-04-16 21:54:24 +0200268 int is_std = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200269 int flags = 0;
Jens Axboe29c13492008-03-01 19:25:20 +0100270 int from_hash = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200271
Jens Axboeee56ad52008-02-01 10:30:20 +0100272 dprint(FD_FILE, "fd open %s\n", f->file_name);
273
Jens Axboe66159822007-04-16 21:54:24 +0200274 if (!strcmp(f->file_name, "-")) {
275 if (td_rw(td)) {
276 log_err("fio: can't read/write to stdin/out\n");
277 return 1;
278 }
279 is_std = 1;
Jens Axboece98fa62007-04-17 13:27:32 +0200280
281 /*
282 * move output logging to stderr, if we are writing to stdout
283 */
284 if (td_write(td))
285 f_out = stderr;
Jens Axboe66159822007-04-16 21:54:24 +0200286 }
287
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100288 if (td->o.odirect)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100289 flags |= OS_O_DIRECT;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100290 if (td->o.sync_io)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100291 flags |= O_SYNC;
Jens Axboead923962007-10-11 21:41:41 +0200292 if (f->filetype != FIO_TYPE_FILE)
293 flags |= O_NOATIME;
Jens Axboe7abf8332006-11-23 15:01:19 +0100294
Aaron Carroll056f3452007-11-26 09:08:53 +0100295open_again:
Jens Axboe660a1cb2007-04-17 15:37:47 +0200296 if (td_write(td)) {
Jens Axboe17308152008-03-07 13:42:31 +0100297 if (!read_only)
298 flags |= O_RDWR;
Jens Axboe53cdc682006-10-18 11:50:58 +0200299
Jens Axboeaf52b342007-03-13 10:07:47 +0100300 if (f->filetype == FIO_TYPE_FILE)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100301 flags |= O_CREAT;
Jens Axboe2fd233b2007-03-02 08:44:25 +0100302
Jens Axboe66159822007-04-16 21:54:24 +0200303 if (is_std)
304 f->fd = dup(STDOUT_FILENO);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100305 else
306 from_hash = file_lookup_open(f, flags);
Jens Axboe2fd233b2007-03-02 08:44:25 +0100307 } else {
Jens Axboe4241ea82007-09-12 08:18:36 +0200308 if (f->filetype == FIO_TYPE_CHAR && !read_only)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100309 flags |= O_RDWR;
310 else
311 flags |= O_RDONLY;
312
Jens Axboe66159822007-04-16 21:54:24 +0200313 if (is_std)
314 f->fd = dup(STDIN_FILENO);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100315 else
316 from_hash = file_lookup_open(f, flags);
Jens Axboe53cdc682006-10-18 11:50:58 +0200317 }
318
319 if (f->fd == -1) {
Jens Axboee4e33252007-03-21 13:07:54 +0100320 char buf[FIO_VERROR_SIZE];
Jens Axboee1161c32007-02-22 19:36:48 +0100321 int __e = errno;
322
Aaron Carroll056f3452007-11-26 09:08:53 +0100323 if (errno == EPERM && (flags & O_NOATIME)) {
324 flags &= ~O_NOATIME;
325 goto open_again;
326 }
327
Jens Axboee8670ef2008-03-06 12:06:30 +0100328 snprintf(buf, sizeof(buf) - 1, "open(%s)", f->file_name);
Jens Axboee4e33252007-03-21 13:07:54 +0100329
330 td_verror(td, __e, buf);
Jens Axboe53cdc682006-10-18 11:50:58 +0200331 }
332
Jens Axboeb5af8292007-03-08 12:43:13 +0100333 if (get_file_size(td, f))
334 goto err;
335
Jens Axboe29c13492008-03-01 19:25:20 +0100336 if (!from_hash && f->fd != -1) {
337 if (add_file_hash(f)) {
338 int ret;
339
340 /*
341 * OK to ignore, we haven't done anything with it
342 */
343 ret = generic_close_file(td, f);
344 goto open_again;
345 }
346 }
Jens Axboe4906e0b2008-03-01 18:59:51 +0100347
Jens Axboe53cdc682006-10-18 11:50:58 +0200348 return 0;
Jens Axboeb5af8292007-03-08 12:43:13 +0100349err:
350 close(f->fd);
351 return 1;
352}
353
Jens Axboe21972cd2006-11-23 12:39:16 +0100354int open_files(struct thread_data *td)
355{
356 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100357 unsigned int i;
358 int err = 0;
Jens Axboe21972cd2006-11-23 12:39:16 +0100359
Jens Axboeee56ad52008-02-01 10:30:20 +0100360 dprint(FD_FILE, "open files\n");
361
Jens Axboe21972cd2006-11-23 12:39:16 +0100362 for_each_file(td, f, i) {
Jens Axboeb5af8292007-03-08 12:43:13 +0100363 err = td_io_open_file(td, f);
Jens Axboe9bf27b42007-03-27 19:49:31 +0200364 if (err) {
365 if (td->error == EMFILE) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100366 log_err("fio: limited open files to: %d\n",
367 td->nr_open_files);
Jens Axboe9bf27b42007-03-27 19:49:31 +0200368 td->o.open_files = td->nr_open_files;
369 err = 0;
370 clear_error(td);
371 }
Jens Axboe21972cd2006-11-23 12:39:16 +0100372 break;
Jens Axboe9bf27b42007-03-27 19:49:31 +0200373 }
Jens Axboeb5af8292007-03-08 12:43:13 +0100374
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100375 if (td->o.open_files == td->nr_open_files)
Jens Axboeb5af8292007-03-08 12:43:13 +0100376 break;
Jens Axboe21972cd2006-11-23 12:39:16 +0100377 }
378
Jens Axboe7abf8332006-11-23 15:01:19 +0100379 if (!err)
380 return 0;
381
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100382 for_each_file(td, f, i)
Jens Axboeb5af8292007-03-08 12:43:13 +0100383 td_io_close_file(td, f);
Jens Axboe7abf8332006-11-23 15:01:19 +0100384
Jens Axboe21972cd2006-11-23 12:39:16 +0100385 return err;
386}
387
Jens Axboe7bb48f82007-03-27 15:30:28 +0200388/*
389 * open/close all files, so that ->real_file_size gets set
390 */
Jens Axboebab3fd52007-04-02 10:34:18 +0200391static int get_file_sizes(struct thread_data *td)
Jens Axboe53cdc682006-10-18 11:50:58 +0200392{
393 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100394 unsigned int i;
Jens Axboebab3fd52007-04-02 10:34:18 +0200395 int err = 0;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200396
397 for_each_file(td, f, i) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100398 dprint(FD_FILE, "get file size for %p/%d/%p\n", f, i,
399 f->file_name);
Jens Axboe9efef3c2008-03-06 10:26:25 +0100400
Jens Axboebab3fd52007-04-02 10:34:18 +0200401 if (td->io_ops->open_file(td, f)) {
Jens Axboe40b44f42007-04-11 12:43:35 +0200402 if (td->error != ENOENT) {
403 log_err("%s\n", td->verror);
404 err = 1;
405 }
Jens Axboe541d66d2007-03-27 19:50:11 +0200406 clear_error(td);
Jens Axboe07eb79d2007-04-12 13:02:38 +0200407 } else {
408 if (td->io_ops->close_file)
409 td->io_ops->close_file(td, f);
410 }
Jens Axboe409b3412007-03-28 09:57:01 +0200411
412 if (f->real_file_size == -1ULL && td->o.size)
413 f->real_file_size = td->o.size / td->o.nr_files;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200414 }
Jens Axboebab3fd52007-04-02 10:34:18 +0200415
416 return err;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200417}
418
419/*
420 * Open the files and setup files sizes, creating files if necessary.
421 */
422int setup_files(struct thread_data *td)
423{
424 unsigned long long total_size, extend_size;
425 struct fio_file *f;
426 unsigned int i;
Jens Axboe000b0802007-03-27 15:56:10 +0200427 int err = 0, need_extend;
Jens Axboe53cdc682006-10-18 11:50:58 +0200428
Jens Axboeee56ad52008-02-01 10:30:20 +0100429 dprint(FD_FILE, "setup files\n");
430
Jens Axboe691c8fb2008-03-07 14:26:26 +0100431 if (td->o.read_iolog_file)
432 return 0;
433
Jens Axboe53cdc682006-10-18 11:50:58 +0200434 /*
435 * if ioengine defines a setup() method, it's responsible for
Jens Axboe7bb48f82007-03-27 15:30:28 +0200436 * opening the files and setting f->real_file_size to indicate
437 * the valid range for that file.
Jens Axboe53cdc682006-10-18 11:50:58 +0200438 */
439 if (td->io_ops->setup)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200440 err = td->io_ops->setup(td);
441 else
Jens Axboebab3fd52007-04-02 10:34:18 +0200442 err = get_file_sizes(td);
Jens Axboe53cdc682006-10-18 11:50:58 +0200443
Jens Axboef1027062006-10-20 10:14:01 +0200444 if (err)
445 return err;
446
Jens Axboe0a7eb122006-10-20 10:36:42 +0200447 /*
Jens Axboe7bb48f82007-03-27 15:30:28 +0200448 * check sizes. if the files/devices do not exist and the size
449 * isn't passed to fio, abort.
Jens Axboe0a7eb122006-10-20 10:36:42 +0200450 */
Jens Axboe7bb48f82007-03-27 15:30:28 +0200451 total_size = 0;
452 for_each_file(td, f, i) {
453 if (f->real_file_size == -1ULL)
454 total_size = -1ULL;
455 else
456 total_size += f->real_file_size;
457 }
Jens Axboe0a7eb122006-10-20 10:36:42 +0200458
Jens Axboe7bb48f82007-03-27 15:30:28 +0200459 /*
460 * device/file sizes are zero and no size given, punt
461 */
Jens Axboe1f809d12007-10-25 18:34:02 +0200462 if ((!total_size || total_size == -1ULL) && !td->o.size &&
Shawn Lewisaa31f1f2008-01-11 09:45:11 +0100463 !(td->io_ops->flags & FIO_NOIO) && !td->o.fill_device) {
Jens Axboe7bb48f82007-03-27 15:30:28 +0200464 log_err("%s: you need to specify size=\n", td->o.name);
Jens Axboee1161c32007-02-22 19:36:48 +0100465 td_verror(td, EINVAL, "total_file_size");
Jens Axboe53cdc682006-10-18 11:50:58 +0200466 return 1;
467 }
468
Jens Axboe7bb48f82007-03-27 15:30:28 +0200469 /*
470 * now file sizes are known, so we can set ->io_size. if size= is
471 * not given, ->io_size is just equal to ->real_file_size. if size
472 * is given, ->io_size is size / nr_files.
473 */
474 extend_size = total_size = 0;
475 need_extend = 0;
476 for_each_file(td, f, i) {
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200477 f->file_offset = td->o.start_offset;
478
Jens Axboe7bb48f82007-03-27 15:30:28 +0200479 if (!td->o.file_size_low) {
480 /*
481 * no file size range given, file size is equal to
482 * total size divided by number of files. if that is
483 * zero, set it to the real file size.
484 */
485 f->io_size = td->o.size / td->o.nr_files;
Jens Axboe65bdb102008-01-24 13:13:12 +0100486 if (!f->io_size)
Jens Axboe273f8c92008-01-25 14:02:15 +0100487 f->io_size = f->real_file_size - f->file_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200488 } else if (f->real_file_size < td->o.file_size_low ||
489 f->real_file_size > td->o.file_size_high) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100490 if (f->file_offset > td->o.file_size_low)
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200491 goto err_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200492 /*
493 * file size given. if it's fixed, use that. if it's a
494 * range, generate a random size in-between.
495 */
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100496 if (td->o.file_size_low == td->o.file_size_high) {
497 f->io_size = td->o.file_size_low
498 - f->file_offset;
499 } else {
500 f->io_size = get_rand_file_size(td)
501 - f->file_offset;
502 }
Jens Axboe65bdb102008-01-24 13:13:12 +0100503 } else
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200504 f->io_size = f->real_file_size - f->file_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200505
506 if (f->io_size == -1ULL)
507 total_size = -1ULL;
508 else
509 total_size += f->io_size;
510
511 if (f->filetype == FIO_TYPE_FILE &&
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200512 (f->io_size + f->file_offset) > f->real_file_size &&
Jens Axboe7bb48f82007-03-27 15:30:28 +0200513 !(td->io_ops->flags & FIO_DISKLESSIO)) {
514 need_extend++;
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200515 extend_size += (f->io_size + f->file_offset);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200516 f->flags |= FIO_FILE_EXTEND;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100517 }
Jens Axboe7bb48f82007-03-27 15:30:28 +0200518 }
519
ljzhang,Yaxin Hu,Jianchao Tang22982902007-07-27 13:21:28 +0200520 if (!td->o.size || td->o.size > total_size)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200521 td->o.size = total_size;
522
523 /*
524 * See if we need to extend some files
525 */
526 if (need_extend) {
527 temp_stall_ts = 1;
Jens Axboef627d8a2007-08-24 10:24:09 +0200528 log_info("%s: Laying out IO file(s) (%u file(s) / %LuMiB)\n",
Jens Axboe7bb48f82007-03-27 15:30:28 +0200529 td->o.name, need_extend, extend_size >> 20);
530
531 for_each_file(td, f, i) {
532 if (!(f->flags & FIO_FILE_EXTEND))
533 continue;
534
Jens Axboe409b3412007-03-28 09:57:01 +0200535 assert(f->filetype == FIO_TYPE_FILE);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200536 f->flags &= ~FIO_FILE_EXTEND;
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200537 f->real_file_size = (f->io_size + f->file_offset);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200538 err = extend_file(td, f);
539 if (err)
540 break;
541 }
542 temp_stall_ts = 0;
543 }
544
545 if (err)
546 return err;
547
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100548 if (!td->o.zone_size)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200549 td->o.zone_size = td->o.size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200550
Jens Axboeea966f82007-09-03 10:09:37 +0200551 /*
552 * iolog already set the total io size, if we read back
553 * stored entries.
554 */
555 if (!td->o.read_iolog_file)
556 td->total_io_size = td->o.size * td->o.loops;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200557 return 0;
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200558err_offset:
559 log_err("%s: you need to specify valid offset=\n", td->o.name);
560 return 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200561}
562
Jens Axboe68727072007-03-15 20:49:25 +0100563int init_random_map(struct thread_data *td)
564{
Jens Axboe509eab12007-12-14 12:42:53 +0100565 unsigned long long blocks, num_maps;
Jens Axboe68727072007-03-15 20:49:25 +0100566 struct fio_file *f;
567 unsigned int i;
568
Jens Axboede8dd112008-03-01 15:34:44 +0100569 if (td->o.norandommap || !td_random(td))
Jens Axboe68727072007-03-15 20:49:25 +0100570 return 0;
571
572 for_each_file(td, f, i) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100573 blocks = (f->real_file_size + td->o.rw_min_bs - 1) /
574 (unsigned long long) td->o.rw_min_bs;
575 num_maps = (blocks + BLOCKS_PER_MAP - 1) /
576 (unsigned long long) BLOCKS_PER_MAP;
Jens Axboef17c4392008-03-01 18:40:46 +0100577 f->file_map = smalloc(num_maps * sizeof(long));
Jens Axboe303032a2008-03-26 10:11:10 +0100578 if (f->file_map) {
579 f->num_maps = num_maps;
580 continue;
Jens Axboe68727072007-03-15 20:49:25 +0100581 }
Jens Axboe2b386d22008-03-26 10:32:57 +0100582 if (!td->o.softrandommap) {
583 log_err("fio: failed allocating random map. If running"
584 " a large number of jobs, try the 'norandommap'"
585 " option or set 'softrandommap'. Or give"
586 " a larger --alloc-size to fio.\n");
587 return 1;
588 }
Jens Axboe303032a2008-03-26 10:11:10 +0100589
590 log_info("fio: file %s failed allocating random map. Running "
591 "job without.\n", f->file_name);
592 f->num_maps = 0;
Jens Axboe68727072007-03-15 20:49:25 +0100593 }
594
595 return 0;
596}
597
Jens Axboe53cdc682006-10-18 11:50:58 +0200598void close_files(struct thread_data *td)
599{
Jens Axboe0ab8db82006-10-18 17:16:23 +0200600 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100601 unsigned int i;
Jens Axboe53cdc682006-10-18 11:50:58 +0200602
Jens Axboe24ffd2c2008-03-01 15:47:08 +0100603 for_each_file(td, f, i)
604 td_io_close_file(td, f);
605}
606
607void close_and_free_files(struct thread_data *td)
608{
609 struct fio_file *f;
610 unsigned int i;
611
Jens Axboeee56ad52008-02-01 10:30:20 +0100612 dprint(FD_FILE, "close files\n");
613
Jens Axboe0ab8db82006-10-18 17:16:23 +0200614 for_each_file(td, f, i) {
Jens Axboeffdfabd2008-03-26 09:18:14 +0100615 if (td->o.unlink && f->filetype == FIO_TYPE_FILE) {
616 dprint(FD_FILE, "free unlink %s\n", f->file_name);
Jens Axboe132ad462006-11-23 12:23:12 +0100617 unlink(f->file_name);
Jens Axboeffdfabd2008-03-26 09:18:14 +0100618 }
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100619
Jens Axboeb5af8292007-03-08 12:43:13 +0100620 td_io_close_file(td, f);
Jens Axboeb3dc7f02007-03-09 12:52:15 +0100621
Jens Axboef17c4392008-03-01 18:40:46 +0100622 sfree(f->file_name);
Jens Axboefa1da862007-03-23 15:20:54 +0100623 f->file_name = NULL;
624
Jens Axboec3439812007-03-20 13:54:53 +0100625 if (f->file_map) {
Jens Axboef17c4392008-03-01 18:40:46 +0100626 sfree(f->file_map);
Jens Axboec3439812007-03-20 13:54:53 +0100627 f->file_map = NULL;
628 }
Jens Axboe78d99e62008-03-01 18:41:50 +0100629 sfree(f);
Jens Axboe53cdc682006-10-18 11:50:58 +0200630 }
Jens Axboeb4a6a592006-10-20 13:54:47 +0200631
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100632 td->o.filename = NULL;
Jens Axboecade3ef2007-03-23 15:29:45 +0100633 free(td->files);
Jens Axboe9efef3c2008-03-06 10:26:25 +0100634 td->files_index = 0;
Jens Axboeb4a6a592006-10-20 13:54:47 +0200635 td->files = NULL;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100636 td->o.nr_files = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200637}
Jens Axboeaf52b342007-03-13 10:07:47 +0100638
Jens Axboee3bab462007-03-13 11:22:09 +0100639static void get_file_type(struct fio_file *f)
Jens Axboeaf52b342007-03-13 10:07:47 +0100640{
641 struct stat sb;
642
Jens Axboe66159822007-04-16 21:54:24 +0200643 if (!strcmp(f->file_name, "-"))
644 f->filetype = FIO_TYPE_PIPE;
645 else
646 f->filetype = FIO_TYPE_FILE;
Jens Axboeaf52b342007-03-13 10:07:47 +0100647
Jens Axboee3bab462007-03-13 11:22:09 +0100648 if (!lstat(f->file_name, &sb)) {
Jens Axboeaf52b342007-03-13 10:07:47 +0100649 if (S_ISBLK(sb.st_mode))
650 f->filetype = FIO_TYPE_BD;
651 else if (S_ISCHR(sb.st_mode))
652 f->filetype = FIO_TYPE_CHAR;
Jens Axboeb5605e92007-04-16 20:42:33 +0200653 else if (S_ISFIFO(sb.st_mode))
654 f->filetype = FIO_TYPE_PIPE;
Jens Axboeaf52b342007-03-13 10:07:47 +0100655 }
656}
657
Jens Axboef29b25a2007-07-23 08:56:43 +0200658int add_file(struct thread_data *td, const char *fname)
Jens Axboeaf52b342007-03-13 10:07:47 +0100659{
Jens Axboe7b4e4fe2007-03-13 12:51:40 +0100660 int cur_files = td->files_index;
Jens Axboebd0ee742007-03-23 14:26:23 +0100661 char file_name[PATH_MAX];
Jens Axboeaf52b342007-03-13 10:07:47 +0100662 struct fio_file *f;
Jens Axboebd0ee742007-03-23 14:26:23 +0100663 int len = 0;
Jens Axboeaf52b342007-03-13 10:07:47 +0100664
Jens Axboeee56ad52008-02-01 10:30:20 +0100665 dprint(FD_FILE, "add file %s\n", fname);
666
Jens Axboef17c4392008-03-01 18:40:46 +0100667 f = smalloc(sizeof(*f));
Jens Axboeaf52b342007-03-13 10:07:47 +0100668 f->fd = -1;
Jens Axboebd0ee742007-03-23 14:26:23 +0100669
Jens Axboe9efef3c2008-03-06 10:26:25 +0100670 dprint(FD_FILE, "resize file array to %d files\n", cur_files + 1);
Jens Axboe126d65c2008-03-01 18:04:31 +0100671
Jens Axboe9efef3c2008-03-06 10:26:25 +0100672 td->files = realloc(td->files, (cur_files + 1) * sizeof(f));
Jens Axboe126d65c2008-03-01 18:04:31 +0100673 td->files[cur_files] = f;
674
Jens Axboe07eb79d2007-04-12 13:02:38 +0200675 /*
676 * init function, io engine may not be loaded yet
677 */
678 if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO))
679 f->real_file_size = -1ULL;
680
Jens Axboebd0ee742007-03-23 14:26:23 +0100681 if (td->o.directory)
682 len = sprintf(file_name, "%s/", td->o.directory);
683
684 sprintf(file_name + len, "%s", fname);
Jens Axboef17c4392008-03-01 18:40:46 +0100685 f->file_name = smalloc_strdup(file_name);
Jens Axboeaf52b342007-03-13 10:07:47 +0100686
Jens Axboee3bab462007-03-13 11:22:09 +0100687 get_file_type(f);
Jens Axboeaf52b342007-03-13 10:07:47 +0100688
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100689 switch (td->o.file_lock_mode) {
690 case FILE_LOCK_NONE:
691 break;
692 case FILE_LOCK_READWRITE:
693 f->lock = fio_mutex_rw_init();
694 break;
695 case FILE_LOCK_EXCLUSIVE:
696 f->lock = fio_mutex_init(1);
697 break;
698 default:
699 log_err("fio: unknown lock mode: %d\n", td->o.file_lock_mode);
700 assert(0);
701 }
Jens Axboe29c13492008-03-01 19:25:20 +0100702
Jens Axboe7b4e4fe2007-03-13 12:51:40 +0100703 td->files_index++;
Jens Axboe15494412007-03-13 11:52:47 +0100704 if (f->filetype == FIO_TYPE_FILE)
705 td->nr_normal_files++;
Jens Axboef29b25a2007-07-23 08:56:43 +0200706
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100707 dprint(FD_FILE, "file %p \"%s\" added at %d\n", f, f->file_name,
708 cur_files);
Jens Axboe9efef3c2008-03-06 10:26:25 +0100709
Jens Axboef29b25a2007-07-23 08:56:43 +0200710 return cur_files;
Jens Axboeaf52b342007-03-13 10:07:47 +0100711}
Jens Axboe0ad920e2007-03-13 11:06:45 +0100712
713void get_file(struct fio_file *f)
714{
Jens Axboe8172fe92008-02-01 21:51:02 +0100715 dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references);
Jens Axboe97af62c2007-05-22 11:12:13 +0200716 assert(f->flags & FIO_FILE_OPEN);
Jens Axboe0ad920e2007-03-13 11:06:45 +0100717 f->references++;
718}
719
Jens Axboe6977bcd2008-03-01 15:55:36 +0100720int put_file(struct thread_data *td, struct fio_file *f)
Jens Axboe0ad920e2007-03-13 11:06:45 +0100721{
Jens Axboe98e1ac42008-03-06 11:56:48 +0100722 int f_ret = 0, ret = 0;
Jens Axboe6977bcd2008-03-01 15:55:36 +0100723
Jens Axboe8172fe92008-02-01 21:51:02 +0100724 dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references);
Jens Axboeee56ad52008-02-01 10:30:20 +0100725
Jens Axboe0ad920e2007-03-13 11:06:45 +0100726 if (!(f->flags & FIO_FILE_OPEN))
Jens Axboe6977bcd2008-03-01 15:55:36 +0100727 return 0;
Jens Axboe0ad920e2007-03-13 11:06:45 +0100728
729 assert(f->references);
730 if (--f->references)
Jens Axboe6977bcd2008-03-01 15:55:36 +0100731 return 0;
Jens Axboe0ad920e2007-03-13 11:06:45 +0100732
Jens Axboed424d4d2007-04-17 09:05:10 +0200733 if (should_fsync(td) && td->o.fsync_on_close)
Jens Axboe98e1ac42008-03-06 11:56:48 +0100734 f_ret = fsync(f->fd);
Jens Axboeebb14152007-03-13 14:42:15 +0100735
Jens Axboe0ad920e2007-03-13 11:06:45 +0100736 if (td->io_ops->close_file)
Jens Axboe6977bcd2008-03-01 15:55:36 +0100737 ret = td->io_ops->close_file(td, f);
Jens Axboe1020a132007-03-29 11:15:06 +0200738
Jens Axboe98e1ac42008-03-06 11:56:48 +0100739 if (!ret)
740 ret = !f_ret;
741
Jens Axboe0ad920e2007-03-13 11:06:45 +0100742 td->nr_open_files--;
743 f->flags &= ~FIO_FILE_OPEN;
Jens Axboe6977bcd2008-03-01 15:55:36 +0100744 return ret;
Jens Axboe0ad920e2007-03-13 11:06:45 +0100745}
Jens Axboebbf6b542007-03-13 15:28:55 +0100746
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100747void lock_file(struct thread_data *td, struct fio_file *f, enum fio_ddir ddir)
Jens Axboeb2bd2bd2008-03-01 18:19:52 +0100748{
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100749 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
750 return;
Jens Axboe29c13492008-03-01 19:25:20 +0100751
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100752 if (f->lock_owner == td && f->lock_batch--)
753 return;
754
755 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
756 if (ddir == DDIR_READ)
757 fio_mutex_down_read(f->lock);
758 else
759 fio_mutex_down_write(f->lock);
760 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
761 fio_mutex_down(f->lock);
762
763 f->lock_owner = td;
764 f->lock_batch = td->o.lockfile_batch;
765 f->lock_ddir = ddir;
766}
767
768void unlock_file(struct thread_data *td, struct fio_file *f)
769{
770 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
771 return;
772 if (f->lock_batch)
773 return;
774
775 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
776 const int is_read = f->lock_ddir == DDIR_READ;
777 int val = fio_mutex_getval(f->lock);
778
779 if ((is_read && val == 1) || (!is_read && val == -1))
780 f->lock_owner = NULL;
781
782 if (is_read)
783 fio_mutex_up_read(f->lock);
784 else
785 fio_mutex_up_write(f->lock);
786 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE) {
787 int val = fio_mutex_getval(f->lock);
788
789 if (val == 0)
790 f->lock_owner = NULL;
791
792 fio_mutex_up(f->lock);
Jens Axboe29c13492008-03-01 19:25:20 +0100793 }
Jens Axboeb2bd2bd2008-03-01 18:19:52 +0100794}
795
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100796void unlock_file_all(struct thread_data *td, struct fio_file *f)
Jens Axboeb2bd2bd2008-03-01 18:19:52 +0100797{
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100798 if (f->lock_owner != td)
799 return;
Jens Axboe29c13492008-03-01 19:25:20 +0100800
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100801 f->lock_batch = 0;
802 unlock_file(td, f);
Jens Axboeb2bd2bd2008-03-01 18:19:52 +0100803}
804
Jens Axboebbf6b542007-03-13 15:28:55 +0100805static int recurse_dir(struct thread_data *td, const char *dirname)
806{
807 struct dirent *dir;
808 int ret = 0;
809 DIR *D;
810
811 D = opendir(dirname);
812 if (!D) {
Jens Axboe0ddb2702007-03-27 19:43:53 +0200813 char buf[FIO_VERROR_SIZE];
814
815 snprintf(buf, FIO_VERROR_SIZE - 1, "opendir(%s)", dirname);
816 td_verror(td, errno, buf);
Jens Axboebbf6b542007-03-13 15:28:55 +0100817 return 1;
818 }
819
820 while ((dir = readdir(D)) != NULL) {
821 char full_path[PATH_MAX];
822 struct stat sb;
823
Jens Axboee85b2b82007-03-14 09:39:06 +0100824 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
825 continue;
Jens Axboe96d32d52007-03-14 09:16:09 +0100826
Jens Axboebbf6b542007-03-13 15:28:55 +0100827 sprintf(full_path, "%s/%s", dirname, dir->d_name);
828
829 if (lstat(full_path, &sb) == -1) {
830 if (errno != ENOENT) {
831 td_verror(td, errno, "stat");
832 return 1;
833 }
834 }
835
836 if (S_ISREG(sb.st_mode)) {
837 add_file(td, full_path);
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100838 td->o.nr_files++;
Jens Axboebbf6b542007-03-13 15:28:55 +0100839 continue;
840 }
Jens Axboe0ddb2702007-03-27 19:43:53 +0200841 if (!S_ISDIR(sb.st_mode))
842 continue;
Jens Axboebbf6b542007-03-13 15:28:55 +0100843
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100844 ret = recurse_dir(td, full_path);
845 if (ret)
Jens Axboebbf6b542007-03-13 15:28:55 +0100846 break;
847 }
848
849 closedir(D);
850 return ret;
851}
852
853int add_dir_files(struct thread_data *td, const char *path)
854{
Jens Axboe0ddb2702007-03-27 19:43:53 +0200855 int ret = recurse_dir(td, path);
856
857 if (!ret)
858 log_info("fio: opendir added %d files\n", td->o.nr_files);
859
860 return ret;
Jens Axboebbf6b542007-03-13 15:28:55 +0100861}
Jens Axboecade3ef2007-03-23 15:29:45 +0100862
863void dup_files(struct thread_data *td, struct thread_data *org)
864{
865 struct fio_file *f;
866 unsigned int i;
Jens Axboe9efef3c2008-03-06 10:26:25 +0100867
868 dprint(FD_FILE, "dup files: %d\n", org->files_index);
Jens Axboecade3ef2007-03-23 15:29:45 +0100869
870 if (!org->files)
871 return;
872
Jens Axboe9efef3c2008-03-06 10:26:25 +0100873 td->files = malloc(org->files_index * sizeof(f));
Jens Axboecade3ef2007-03-23 15:29:45 +0100874
Jens Axboe9efef3c2008-03-06 10:26:25 +0100875 for_each_file(org, f, i) {
Jens Axboeb0fe4212008-03-01 18:22:27 +0100876 struct fio_file *__f;
877
Jens Axboef17c4392008-03-01 18:40:46 +0100878 __f = smalloc(sizeof(*__f));
Jens Axboeb0fe4212008-03-01 18:22:27 +0100879
Jens Axboecade3ef2007-03-23 15:29:45 +0100880 if (f->file_name)
Jens Axboef17c4392008-03-01 18:40:46 +0100881 __f->file_name = smalloc_strdup(f->file_name);
Jens Axboeb0fe4212008-03-01 18:22:27 +0100882
883 td->files[i] = __f;
Jens Axboecade3ef2007-03-23 15:29:45 +0100884 }
885}
Jens Axboef29b25a2007-07-23 08:56:43 +0200886
887/*
888 * Returns the index that matches the filename, or -1 if not there
889 */
890int get_fileno(struct thread_data *td, const char *fname)
891{
892 struct fio_file *f;
893 unsigned int i;
894
895 for_each_file(td, f, i)
896 if (!strcmp(f->file_name, fname))
897 return i;
898
899 return -1;
900}
901
902/*
903 * For log usage, where we add/open/close files automatically
904 */
905void free_release_files(struct thread_data *td)
906{
907 close_files(td);
908 td->files_index = 0;
909 td->nr_normal_files = 0;
910}