blob: 98479fd04fbebf9b2d24834e9e793f75c5447650 [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 Axboe3baddf22008-04-04 11:10:30 +020016/*
17 * Leaves f->fd open on success, caller must close
18 */
Jens Axboe7bb48f82007-03-27 15:30:28 +020019static int extend_file(struct thread_data *td, struct fio_file *f)
Jens Axboe25205e92006-10-19 20:50:14 +020020{
Jens Axboeea443652007-03-29 14:52:13 +020021 int r, new_layout = 0, unlink_file = 0, flags;
Jens Axboe25205e92006-10-19 20:50:14 +020022 unsigned long long left;
23 unsigned int bs;
24 char *b;
Jens Axboeb2a15192006-10-18 14:10:42 +020025
Jens Axboe4241ea82007-09-12 08:18:36 +020026 if (read_only) {
27 log_err("fio: refusing extend of file due to read-only\n");
28 return 0;
29 }
30
Jens Axboe507a7022007-03-27 15:52:46 +020031 /*
32 * check if we need to lay the file out complete again. fio
33 * does that for operations involving reads, or for writes
34 * where overwrite is set
35 */
36 if (td_read(td) || (td_write(td) && td->o.overwrite))
37 new_layout = 1;
Jens Axboeea443652007-03-29 14:52:13 +020038 if (td_write(td) && !td->o.overwrite)
39 unlink_file = 1;
Jens Axboe507a7022007-03-27 15:52:46 +020040
Jens Axboe6ae1f572008-02-21 10:20:00 +010041 if (unlink_file || new_layout) {
Zhang, Yanmin982016d2008-02-26 15:35:52 +010042 if ((unlink(f->file_name) < 0) && (errno != ENOENT)) {
Jens Axboe7bb48f82007-03-27 15:30:28 +020043 td_verror(td, errno, "unlink");
44 return 1;
45 }
46 }
47
Jens Axboe507a7022007-03-27 15:52:46 +020048 flags = O_WRONLY | O_CREAT;
49 if (new_layout)
50 flags |= O_TRUNC;
51
Jens Axboeee56ad52008-02-01 10:30:20 +010052 dprint(FD_FILE, "open file %s, flags %x\n", f->file_name, flags);
Jens Axboe507a7022007-03-27 15:52:46 +020053 f->fd = open(f->file_name, flags, 0644);
Jens Axboe53cdc682006-10-18 11:50:58 +020054 if (f->fd < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +010055 td_verror(td, errno, "open");
Jens Axboe53cdc682006-10-18 11:50:58 +020056 return 1;
57 }
58
Shawn Lewisfc74ac12008-01-11 09:45:11 +010059 if (!new_layout)
60 goto done;
61
Jens Axboeee56ad52008-02-01 10:30:20 +010062 dprint(FD_FILE, "truncate file %s, size %llu\n", f->file_name,
63 f->real_file_size);
Jens Axboe7bb48f82007-03-27 15:30:28 +020064 if (ftruncate(f->fd, f->real_file_size) == -1) {
Jens Axboee1161c32007-02-22 19:36:48 +010065 td_verror(td, errno, "ftruncate");
Jens Axboe53cdc682006-10-18 11:50:58 +020066 goto err;
67 }
68
Jens Axboeee56ad52008-02-01 10:30:20 +010069 dprint(FD_FILE, "fallocate file %s, size %llu\n", f->file_name,
70 f->real_file_size);
Jens Axboe7bb48f82007-03-27 15:30:28 +020071 if (posix_fallocate(f->fd, 0, f->real_file_size) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +010072 td_verror(td, errno, "posix_fallocate");
Jens Axboe40f82982006-10-25 11:21:05 +020073 goto err;
74 }
75
Jens Axboe2dc1bbe2007-03-15 15:01:33 +010076 b = malloc(td->o.max_bs[DDIR_WRITE]);
77 memset(b, 0, td->o.max_bs[DDIR_WRITE]);
Jens Axboe53cdc682006-10-18 11:50:58 +020078
Jens Axboe7bb48f82007-03-27 15:30:28 +020079 left = f->real_file_size;
Jens Axboe53cdc682006-10-18 11:50:58 +020080 while (left && !td->terminate) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +010081 bs = td->o.max_bs[DDIR_WRITE];
Jens Axboe53cdc682006-10-18 11:50:58 +020082 if (bs > left)
83 bs = left;
84
85 r = write(f->fd, b, bs);
86
87 if (r == (int) bs) {
88 left -= bs;
89 continue;
90 } else {
91 if (r < 0)
Jens Axboee1161c32007-02-22 19:36:48 +010092 td_verror(td, errno, "write");
Jens Axboe53cdc682006-10-18 11:50:58 +020093 else
Jens Axboee1161c32007-02-22 19:36:48 +010094 td_verror(td, EIO, "write");
Jens Axboe53cdc682006-10-18 11:50:58 +020095
96 break;
97 }
98 }
99
100 if (td->terminate)
101 unlink(f->file_name);
Jens Axboe98e1ac42008-03-06 11:56:48 +0100102 else if (td->o.create_fsync) {
103 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 return 0;
112err:
113 close(f->fd);
114 f->fd = -1;
115 return 1;
116}
117
Jens Axboe7bb48f82007-03-27 15:30:28 +0200118static unsigned long long get_rand_file_size(struct thread_data *td)
Jens Axboe9c60ce62007-03-15 09:14:47 +0100119{
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100120 unsigned long long ret, size_d;
Jens Axboe9c60ce62007-03-15 09:14:47 +0100121 long r;
122
Jens Axboe9c60ce62007-03-15 09:14:47 +0100123 r = os_random_long(&td->file_size_state);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100124 size_d = td->o.file_size_high - td->o.file_size_low;
125 ret = (unsigned long long) ((double) size_d * (r / (RAND_MAX + 1.0)));
126 ret += td->o.file_size_low;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100127 ret -= (ret % td->o.rw_min_bs);
Jens Axboe9c60ce62007-03-15 09:14:47 +0100128 return ret;
129}
130
Jens Axboe53cdc682006-10-18 11:50:58 +0200131static int file_size(struct thread_data *td, struct fio_file *f)
132{
133 struct stat st;
134
Jens Axboe7bb48f82007-03-27 15:30:28 +0200135 if (fstat(f->fd, &st) == -1) {
136 td_verror(td, errno, "fstat");
137 return 1;
138 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200139
Jens Axboe7bb48f82007-03-27 15:30:28 +0200140 f->real_file_size = st.st_size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200141 return 0;
142}
143
144static int bdev_size(struct thread_data *td, struct fio_file *f)
145{
146 unsigned long long bytes;
147 int r;
148
149 r = blockdev_size(f->fd, &bytes);
150 if (r) {
Jens Axboee1161c32007-02-22 19:36:48 +0100151 td_verror(td, r, "blockdev_size");
Jens Axboe53cdc682006-10-18 11:50:58 +0200152 return 1;
153 }
154
155 f->real_file_size = bytes;
Jens Axboe53cdc682006-10-18 11:50:58 +0200156 return 0;
157}
158
159static int get_file_size(struct thread_data *td, struct fio_file *f)
160{
161 int ret = 0;
162
Jens Axboe409b3412007-03-28 09:57:01 +0200163 if (f->flags & FIO_SIZE_KNOWN)
164 return 0;
165
Jens Axboe7bb48f82007-03-27 15:30:28 +0200166 if (f->filetype == FIO_TYPE_FILE)
167 ret = file_size(td, f);
168 else if (f->filetype == FIO_TYPE_BD)
Jens Axboe53cdc682006-10-18 11:50:58 +0200169 ret = bdev_size(td, f);
170 else
171 f->real_file_size = -1;
172
173 if (ret)
174 return ret;
175
176 if (f->file_offset > f->real_file_size) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100177 log_err("%s: offset extends end (%Lu > %Lu)\n", td->o.name,
178 f->file_offset, f->real_file_size);
Jens Axboe53cdc682006-10-18 11:50:58 +0200179 return 1;
180 }
181
Jens Axboe409b3412007-03-28 09:57:01 +0200182 f->flags |= FIO_SIZE_KNOWN;
Jens Axboe53cdc682006-10-18 11:50:58 +0200183 return 0;
184}
185
Jens Axboe3baddf22008-04-04 11:10:30 +0200186static int __file_invalidate_cache(struct thread_data *td, struct fio_file *f,
187 unsigned long long off,
188 unsigned long long len)
Jens Axboee5b401d2006-10-18 16:03:40 +0200189{
190 int ret = 0;
191
Jens Axboe3baddf22008-04-04 11:10:30 +0200192 if (len == -1ULL)
193 len = f->io_size;
194 if (off == -1ULL)
195 off = f->file_offset;
Jens Axboeee56ad52008-02-01 10:30:20 +0100196
Jens Axboe3baddf22008-04-04 11:10:30 +0200197 dprint(FD_IO, "invalidate cache %s: %llu/%llu\n", f->file_name, off,
198 len);
Jens Axboeb5af8292007-03-08 12:43:13 +0100199
Jens Axboee5b401d2006-10-18 16:03:40 +0200200 /*
201 * FIXME: add blockdev flushing too
202 */
Jens Axboeb5af8292007-03-08 12:43:13 +0100203 if (f->mmap)
Jens Axboe3baddf22008-04-04 11:10:30 +0200204 ret = madvise(f->mmap, len, MADV_DONTNEED);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100205 else if (f->filetype == FIO_TYPE_FILE) {
Jens Axboe3baddf22008-04-04 11:10:30 +0200206 ret = fadvise(f->fd, off, len, POSIX_FADV_DONTNEED);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100207 } else if (f->filetype == FIO_TYPE_BD) {
Jens Axboeb5af8292007-03-08 12:43:13 +0100208 ret = blockdev_invalidate_cache(f->fd);
Jens Axboe7e0e25c2007-03-27 12:49:56 +0200209 if (ret < 0 && errno == EACCES && geteuid()) {
Jens Axboe7172cfe2007-07-23 14:36:00 +0200210 if (!root_warn) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100211 log_err("fio: only root may flush block "
212 "devices. Cache flush bypassed!\n");
Jens Axboe7172cfe2007-07-23 14:36:00 +0200213 root_warn = 1;
214 }
Jens Axboe7e0e25c2007-03-27 12:49:56 +0200215 ret = 0;
216 }
Jens Axboeb5605e92007-04-16 20:42:33 +0200217 } else if (f->filetype == FIO_TYPE_CHAR || f->filetype == FIO_TYPE_PIPE)
Jens Axboee5b401d2006-10-18 16:03:40 +0200218 ret = 0;
219
220 if (ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100221 td_verror(td, errno, "invalidate_cache");
Jens Axboee5b401d2006-10-18 16:03:40 +0200222 return 1;
Jens Axboe3baddf22008-04-04 11:10:30 +0200223 } else if (ret > 0) {
224 td_verror(td, ret, "invalidate_cache");
225 return 1;
Jens Axboee5b401d2006-10-18 16:03:40 +0200226 }
227
Jens Axboead2da602007-02-26 12:33:27 +0100228 return ret;
Jens Axboe3baddf22008-04-04 11:10:30 +0200229
230}
231
232int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
233{
234 return __file_invalidate_cache(td, f, -1, -1);
Jens Axboee5b401d2006-10-18 16:03:40 +0200235}
236
Jens Axboe6977bcd2008-03-01 15:55:36 +0100237int generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
Jens Axboe53cdc682006-10-18 11:50:58 +0200238{
Jens Axboe6977bcd2008-03-01 15:55:36 +0100239 int ret = 0;
240
Jens Axboeee56ad52008-02-01 10:30:20 +0100241 dprint(FD_FILE, "fd close %s\n", f->file_name);
Jens Axboe4906e0b2008-03-01 18:59:51 +0100242
243 remove_file_hash(f);
244
Jens Axboe6977bcd2008-03-01 15:55:36 +0100245 if (close(f->fd) < 0)
246 ret = errno;
247
Jens Axboeb5af8292007-03-08 12:43:13 +0100248 f->fd = -1;
Jens Axboe6977bcd2008-03-01 15:55:36 +0100249 return ret;
Jens Axboe53cdc682006-10-18 11:50:58 +0200250}
251
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100252static int file_lookup_open(struct fio_file *f, int flags)
Jens Axboe53cdc682006-10-18 11:50:58 +0200253{
Jens Axboe29c13492008-03-01 19:25:20 +0100254 struct fio_file *__f;
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100255 int from_hash;
256
257 __f = lookup_file_hash(f->file_name);
258 if (__f) {
Jens Axboe9efef3c2008-03-06 10:26:25 +0100259 dprint(FD_FILE, "found file in hash %s\n", f->file_name);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100260 /*
261 * racy, need the __f->lock locked
262 */
263 f->lock = __f->lock;
264 f->lock_owner = __f->lock_owner;
265 f->lock_batch = __f->lock_batch;
266 f->lock_ddir = __f->lock_ddir;
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100267 from_hash = 1;
268 } else {
Jens Axboe9efef3c2008-03-06 10:26:25 +0100269 dprint(FD_FILE, "file not found in hash %s\n", f->file_name);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100270 from_hash = 0;
271 }
272
Jens Axboee8670ef2008-03-06 12:06:30 +0100273 f->fd = open(f->file_name, flags, 0600);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100274 return from_hash;
275}
276
277int generic_open_file(struct thread_data *td, struct fio_file *f)
278{
Jens Axboe66159822007-04-16 21:54:24 +0200279 int is_std = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200280 int flags = 0;
Jens Axboe29c13492008-03-01 19:25:20 +0100281 int from_hash = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200282
Jens Axboeee56ad52008-02-01 10:30:20 +0100283 dprint(FD_FILE, "fd open %s\n", f->file_name);
284
Jens Axboe66159822007-04-16 21:54:24 +0200285 if (!strcmp(f->file_name, "-")) {
286 if (td_rw(td)) {
287 log_err("fio: can't read/write to stdin/out\n");
288 return 1;
289 }
290 is_std = 1;
Jens Axboece98fa62007-04-17 13:27:32 +0200291
292 /*
293 * move output logging to stderr, if we are writing to stdout
294 */
295 if (td_write(td))
296 f_out = stderr;
Jens Axboe66159822007-04-16 21:54:24 +0200297 }
298
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100299 if (td->o.odirect)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100300 flags |= OS_O_DIRECT;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100301 if (td->o.sync_io)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100302 flags |= O_SYNC;
Jens Axboead923962007-10-11 21:41:41 +0200303 if (f->filetype != FIO_TYPE_FILE)
304 flags |= O_NOATIME;
Jens Axboe7abf8332006-11-23 15:01:19 +0100305
Aaron Carroll056f3452007-11-26 09:08:53 +0100306open_again:
Jens Axboe660a1cb2007-04-17 15:37:47 +0200307 if (td_write(td)) {
Jens Axboe17308152008-03-07 13:42:31 +0100308 if (!read_only)
309 flags |= O_RDWR;
Jens Axboe53cdc682006-10-18 11:50:58 +0200310
Jens Axboeaf52b342007-03-13 10:07:47 +0100311 if (f->filetype == FIO_TYPE_FILE)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100312 flags |= O_CREAT;
Jens Axboe2fd233b2007-03-02 08:44:25 +0100313
Jens Axboe66159822007-04-16 21:54:24 +0200314 if (is_std)
315 f->fd = dup(STDOUT_FILENO);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100316 else
317 from_hash = file_lookup_open(f, flags);
Jens Axboe2fd233b2007-03-02 08:44:25 +0100318 } else {
Jens Axboe4241ea82007-09-12 08:18:36 +0200319 if (f->filetype == FIO_TYPE_CHAR && !read_only)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100320 flags |= O_RDWR;
321 else
322 flags |= O_RDONLY;
323
Jens Axboe66159822007-04-16 21:54:24 +0200324 if (is_std)
325 f->fd = dup(STDIN_FILENO);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100326 else
327 from_hash = file_lookup_open(f, flags);
Jens Axboe53cdc682006-10-18 11:50:58 +0200328 }
329
330 if (f->fd == -1) {
Jens Axboee4e33252007-03-21 13:07:54 +0100331 char buf[FIO_VERROR_SIZE];
Jens Axboee1161c32007-02-22 19:36:48 +0100332 int __e = errno;
333
Aaron Carroll056f3452007-11-26 09:08:53 +0100334 if (errno == EPERM && (flags & O_NOATIME)) {
335 flags &= ~O_NOATIME;
336 goto open_again;
337 }
338
Jens Axboee8670ef2008-03-06 12:06:30 +0100339 snprintf(buf, sizeof(buf) - 1, "open(%s)", f->file_name);
Jens Axboee4e33252007-03-21 13:07:54 +0100340
341 td_verror(td, __e, buf);
Jens Axboe53cdc682006-10-18 11:50:58 +0200342 }
343
Jens Axboeb5af8292007-03-08 12:43:13 +0100344 if (get_file_size(td, f))
345 goto err;
346
Jens Axboe29c13492008-03-01 19:25:20 +0100347 if (!from_hash && f->fd != -1) {
348 if (add_file_hash(f)) {
349 int ret;
350
351 /*
352 * OK to ignore, we haven't done anything with it
353 */
354 ret = generic_close_file(td, f);
355 goto open_again;
356 }
357 }
Jens Axboe4906e0b2008-03-01 18:59:51 +0100358
Jens Axboe53cdc682006-10-18 11:50:58 +0200359 return 0;
Jens Axboeb5af8292007-03-08 12:43:13 +0100360err:
361 close(f->fd);
362 return 1;
363}
364
Jens Axboe21972cd2006-11-23 12:39:16 +0100365int open_files(struct thread_data *td)
366{
367 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100368 unsigned int i;
369 int err = 0;
Jens Axboe21972cd2006-11-23 12:39:16 +0100370
Jens Axboeee56ad52008-02-01 10:30:20 +0100371 dprint(FD_FILE, "open files\n");
372
Jens Axboe21972cd2006-11-23 12:39:16 +0100373 for_each_file(td, f, i) {
Jens Axboeb5af8292007-03-08 12:43:13 +0100374 err = td_io_open_file(td, f);
Jens Axboe9bf27b42007-03-27 19:49:31 +0200375 if (err) {
376 if (td->error == EMFILE) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100377 log_err("fio: limited open files to: %d\n",
378 td->nr_open_files);
Jens Axboe9bf27b42007-03-27 19:49:31 +0200379 td->o.open_files = td->nr_open_files;
380 err = 0;
381 clear_error(td);
382 }
Jens Axboe21972cd2006-11-23 12:39:16 +0100383 break;
Jens Axboe9bf27b42007-03-27 19:49:31 +0200384 }
Jens Axboeb5af8292007-03-08 12:43:13 +0100385
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100386 if (td->o.open_files == td->nr_open_files)
Jens Axboeb5af8292007-03-08 12:43:13 +0100387 break;
Jens Axboe21972cd2006-11-23 12:39:16 +0100388 }
389
Jens Axboe7abf8332006-11-23 15:01:19 +0100390 if (!err)
391 return 0;
392
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100393 for_each_file(td, f, i)
Jens Axboeb5af8292007-03-08 12:43:13 +0100394 td_io_close_file(td, f);
Jens Axboe7abf8332006-11-23 15:01:19 +0100395
Jens Axboe21972cd2006-11-23 12:39:16 +0100396 return err;
397}
398
Jens Axboe7bb48f82007-03-27 15:30:28 +0200399/*
400 * open/close all files, so that ->real_file_size gets set
401 */
Jens Axboebab3fd52007-04-02 10:34:18 +0200402static int get_file_sizes(struct thread_data *td)
Jens Axboe53cdc682006-10-18 11:50:58 +0200403{
404 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100405 unsigned int i;
Jens Axboebab3fd52007-04-02 10:34:18 +0200406 int err = 0;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200407
408 for_each_file(td, f, i) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100409 dprint(FD_FILE, "get file size for %p/%d/%p\n", f, i,
410 f->file_name);
Jens Axboe9efef3c2008-03-06 10:26:25 +0100411
Jens Axboebab3fd52007-04-02 10:34:18 +0200412 if (td->io_ops->open_file(td, f)) {
Jens Axboe40b44f42007-04-11 12:43:35 +0200413 if (td->error != ENOENT) {
414 log_err("%s\n", td->verror);
415 err = 1;
416 }
Jens Axboe541d66d2007-03-27 19:50:11 +0200417 clear_error(td);
Jens Axboe07eb79d2007-04-12 13:02:38 +0200418 } else {
419 if (td->io_ops->close_file)
420 td->io_ops->close_file(td, f);
421 }
Jens Axboe409b3412007-03-28 09:57:01 +0200422
423 if (f->real_file_size == -1ULL && td->o.size)
424 f->real_file_size = td->o.size / td->o.nr_files;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200425 }
Jens Axboebab3fd52007-04-02 10:34:18 +0200426
427 return err;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200428}
429
430/*
431 * Open the files and setup files sizes, creating files if necessary.
432 */
433int setup_files(struct thread_data *td)
434{
435 unsigned long long total_size, extend_size;
436 struct fio_file *f;
437 unsigned int i;
Jens Axboe000b0802007-03-27 15:56:10 +0200438 int err = 0, need_extend;
Jens Axboe53cdc682006-10-18 11:50:58 +0200439
Jens Axboeee56ad52008-02-01 10:30:20 +0100440 dprint(FD_FILE, "setup files\n");
441
Jens Axboe691c8fb2008-03-07 14:26:26 +0100442 if (td->o.read_iolog_file)
443 return 0;
444
Jens Axboe53cdc682006-10-18 11:50:58 +0200445 /*
446 * if ioengine defines a setup() method, it's responsible for
Jens Axboe7bb48f82007-03-27 15:30:28 +0200447 * opening the files and setting f->real_file_size to indicate
448 * the valid range for that file.
Jens Axboe53cdc682006-10-18 11:50:58 +0200449 */
450 if (td->io_ops->setup)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200451 err = td->io_ops->setup(td);
452 else
Jens Axboebab3fd52007-04-02 10:34:18 +0200453 err = get_file_sizes(td);
Jens Axboe53cdc682006-10-18 11:50:58 +0200454
Jens Axboef1027062006-10-20 10:14:01 +0200455 if (err)
456 return err;
457
Jens Axboe0a7eb122006-10-20 10:36:42 +0200458 /*
Jens Axboe7bb48f82007-03-27 15:30:28 +0200459 * check sizes. if the files/devices do not exist and the size
460 * isn't passed to fio, abort.
Jens Axboe0a7eb122006-10-20 10:36:42 +0200461 */
Jens Axboe7bb48f82007-03-27 15:30:28 +0200462 total_size = 0;
463 for_each_file(td, f, i) {
464 if (f->real_file_size == -1ULL)
465 total_size = -1ULL;
466 else
467 total_size += f->real_file_size;
468 }
Jens Axboe0a7eb122006-10-20 10:36:42 +0200469
Jens Axboe7bb48f82007-03-27 15:30:28 +0200470 /*
471 * device/file sizes are zero and no size given, punt
472 */
Jens Axboe1f809d12007-10-25 18:34:02 +0200473 if ((!total_size || total_size == -1ULL) && !td->o.size &&
Shawn Lewisaa31f1f2008-01-11 09:45:11 +0100474 !(td->io_ops->flags & FIO_NOIO) && !td->o.fill_device) {
Jens Axboe7bb48f82007-03-27 15:30:28 +0200475 log_err("%s: you need to specify size=\n", td->o.name);
Jens Axboee1161c32007-02-22 19:36:48 +0100476 td_verror(td, EINVAL, "total_file_size");
Jens Axboe53cdc682006-10-18 11:50:58 +0200477 return 1;
478 }
479
Jens Axboe7bb48f82007-03-27 15:30:28 +0200480 /*
481 * now file sizes are known, so we can set ->io_size. if size= is
482 * not given, ->io_size is just equal to ->real_file_size. if size
483 * is given, ->io_size is size / nr_files.
484 */
485 extend_size = total_size = 0;
486 need_extend = 0;
487 for_each_file(td, f, i) {
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200488 f->file_offset = td->o.start_offset;
489
Jens Axboe7bb48f82007-03-27 15:30:28 +0200490 if (!td->o.file_size_low) {
491 /*
492 * no file size range given, file size is equal to
493 * total size divided by number of files. if that is
494 * zero, set it to the real file size.
495 */
496 f->io_size = td->o.size / td->o.nr_files;
Jens Axboe65bdb102008-01-24 13:13:12 +0100497 if (!f->io_size)
Jens Axboe273f8c92008-01-25 14:02:15 +0100498 f->io_size = f->real_file_size - f->file_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200499 } else if (f->real_file_size < td->o.file_size_low ||
500 f->real_file_size > td->o.file_size_high) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100501 if (f->file_offset > td->o.file_size_low)
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200502 goto err_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200503 /*
504 * file size given. if it's fixed, use that. if it's a
505 * range, generate a random size in-between.
506 */
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100507 if (td->o.file_size_low == td->o.file_size_high) {
508 f->io_size = td->o.file_size_low
509 - f->file_offset;
510 } else {
511 f->io_size = get_rand_file_size(td)
512 - f->file_offset;
513 }
Jens Axboe65bdb102008-01-24 13:13:12 +0100514 } else
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200515 f->io_size = f->real_file_size - f->file_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200516
517 if (f->io_size == -1ULL)
518 total_size = -1ULL;
519 else
520 total_size += f->io_size;
521
522 if (f->filetype == FIO_TYPE_FILE &&
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200523 (f->io_size + f->file_offset) > f->real_file_size &&
Jens Axboe7bb48f82007-03-27 15:30:28 +0200524 !(td->io_ops->flags & FIO_DISKLESSIO)) {
525 need_extend++;
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200526 extend_size += (f->io_size + f->file_offset);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200527 f->flags |= FIO_FILE_EXTEND;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100528 }
Jens Axboe7bb48f82007-03-27 15:30:28 +0200529 }
530
ljzhang,Yaxin Hu,Jianchao Tang22982902007-07-27 13:21:28 +0200531 if (!td->o.size || td->o.size > total_size)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200532 td->o.size = total_size;
533
534 /*
535 * See if we need to extend some files
536 */
537 if (need_extend) {
538 temp_stall_ts = 1;
Jens Axboef627d8a2007-08-24 10:24:09 +0200539 log_info("%s: Laying out IO file(s) (%u file(s) / %LuMiB)\n",
Jens Axboe7bb48f82007-03-27 15:30:28 +0200540 td->o.name, need_extend, extend_size >> 20);
541
542 for_each_file(td, f, i) {
Jens Axboe3baddf22008-04-04 11:10:30 +0200543 unsigned long long old_len, extend_len;
544
Jens Axboe7bb48f82007-03-27 15:30:28 +0200545 if (!(f->flags & FIO_FILE_EXTEND))
546 continue;
547
Jens Axboe409b3412007-03-28 09:57:01 +0200548 assert(f->filetype == FIO_TYPE_FILE);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200549 f->flags &= ~FIO_FILE_EXTEND;
Jens Axboe3baddf22008-04-04 11:10:30 +0200550 old_len = f->real_file_size;
551 extend_len = f->io_size + f->file_offset - old_len;
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200552 f->real_file_size = (f->io_size + f->file_offset);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200553 err = extend_file(td, f);
554 if (err)
555 break;
Jens Axboe3baddf22008-04-04 11:10:30 +0200556
557 err = __file_invalidate_cache(td, f, old_len,
558 extend_len);
559 close(f->fd);
560 f->fd = -1;
561 if (err)
562 break;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200563 }
564 temp_stall_ts = 0;
565 }
566
567 if (err)
568 return err;
569
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100570 if (!td->o.zone_size)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200571 td->o.zone_size = td->o.size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200572
Jens Axboeea966f82007-09-03 10:09:37 +0200573 /*
574 * iolog already set the total io size, if we read back
575 * stored entries.
576 */
577 if (!td->o.read_iolog_file)
578 td->total_io_size = td->o.size * td->o.loops;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200579 return 0;
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200580err_offset:
581 log_err("%s: you need to specify valid offset=\n", td->o.name);
582 return 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200583}
584
Jens Axboe68727072007-03-15 20:49:25 +0100585int init_random_map(struct thread_data *td)
586{
Jens Axboe509eab12007-12-14 12:42:53 +0100587 unsigned long long blocks, num_maps;
Jens Axboe68727072007-03-15 20:49:25 +0100588 struct fio_file *f;
589 unsigned int i;
590
Jens Axboede8dd112008-03-01 15:34:44 +0100591 if (td->o.norandommap || !td_random(td))
Jens Axboe68727072007-03-15 20:49:25 +0100592 return 0;
593
594 for_each_file(td, f, i) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100595 blocks = (f->real_file_size + td->o.rw_min_bs - 1) /
596 (unsigned long long) td->o.rw_min_bs;
597 num_maps = (blocks + BLOCKS_PER_MAP - 1) /
598 (unsigned long long) BLOCKS_PER_MAP;
Jens Axboef17c4392008-03-01 18:40:46 +0100599 f->file_map = smalloc(num_maps * sizeof(long));
Jens Axboe68727072007-03-15 20:49:25 +0100600 if (!f->file_map) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100601 log_err("fio: failed allocating random map. If running"
602 " a large number of jobs, try the 'norandommap'"
603 " option\n");
Jens Axboe68727072007-03-15 20:49:25 +0100604 return 1;
605 }
606 f->num_maps = num_maps;
Jens Axboe68727072007-03-15 20:49:25 +0100607 }
608
609 return 0;
610}
611
Jens Axboe53cdc682006-10-18 11:50:58 +0200612void close_files(struct thread_data *td)
613{
Jens Axboe0ab8db82006-10-18 17:16:23 +0200614 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100615 unsigned int i;
Jens Axboe53cdc682006-10-18 11:50:58 +0200616
Jens Axboe24ffd2c2008-03-01 15:47:08 +0100617 for_each_file(td, f, i)
618 td_io_close_file(td, f);
619}
620
621void close_and_free_files(struct thread_data *td)
622{
623 struct fio_file *f;
624 unsigned int i;
625
Jens Axboeee56ad52008-02-01 10:30:20 +0100626 dprint(FD_FILE, "close files\n");
627
Jens Axboe0ab8db82006-10-18 17:16:23 +0200628 for_each_file(td, f, i) {
Jens Axboea9b7b302007-04-19 16:31:16 +0200629 if (td->o.unlink && f->filetype == FIO_TYPE_FILE)
Jens Axboe132ad462006-11-23 12:23:12 +0100630 unlink(f->file_name);
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100631
Jens Axboeb5af8292007-03-08 12:43:13 +0100632 td_io_close_file(td, f);
Jens Axboeb3dc7f02007-03-09 12:52:15 +0100633
Jens Axboef17c4392008-03-01 18:40:46 +0100634 sfree(f->file_name);
Jens Axboefa1da862007-03-23 15:20:54 +0100635 f->file_name = NULL;
636
Jens Axboec3439812007-03-20 13:54:53 +0100637 if (f->file_map) {
Jens Axboef17c4392008-03-01 18:40:46 +0100638 sfree(f->file_map);
Jens Axboec3439812007-03-20 13:54:53 +0100639 f->file_map = NULL;
640 }
Jens Axboe78d99e62008-03-01 18:41:50 +0100641 sfree(f);
Jens Axboe53cdc682006-10-18 11:50:58 +0200642 }
Jens Axboeb4a6a592006-10-20 13:54:47 +0200643
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100644 td->o.filename = NULL;
Jens Axboecade3ef2007-03-23 15:29:45 +0100645 free(td->files);
Jens Axboe9efef3c2008-03-06 10:26:25 +0100646 td->files_index = 0;
Jens Axboeb4a6a592006-10-20 13:54:47 +0200647 td->files = NULL;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100648 td->o.nr_files = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200649}
Jens Axboeaf52b342007-03-13 10:07:47 +0100650
Jens Axboee3bab462007-03-13 11:22:09 +0100651static void get_file_type(struct fio_file *f)
Jens Axboeaf52b342007-03-13 10:07:47 +0100652{
653 struct stat sb;
654
Jens Axboe66159822007-04-16 21:54:24 +0200655 if (!strcmp(f->file_name, "-"))
656 f->filetype = FIO_TYPE_PIPE;
657 else
658 f->filetype = FIO_TYPE_FILE;
Jens Axboeaf52b342007-03-13 10:07:47 +0100659
Jens Axboee3bab462007-03-13 11:22:09 +0100660 if (!lstat(f->file_name, &sb)) {
Jens Axboeaf52b342007-03-13 10:07:47 +0100661 if (S_ISBLK(sb.st_mode))
662 f->filetype = FIO_TYPE_BD;
663 else if (S_ISCHR(sb.st_mode))
664 f->filetype = FIO_TYPE_CHAR;
Jens Axboeb5605e92007-04-16 20:42:33 +0200665 else if (S_ISFIFO(sb.st_mode))
666 f->filetype = FIO_TYPE_PIPE;
Jens Axboeaf52b342007-03-13 10:07:47 +0100667 }
668}
669
Jens Axboef29b25a2007-07-23 08:56:43 +0200670int add_file(struct thread_data *td, const char *fname)
Jens Axboeaf52b342007-03-13 10:07:47 +0100671{
Jens Axboe7b4e4fe2007-03-13 12:51:40 +0100672 int cur_files = td->files_index;
Jens Axboebd0ee742007-03-23 14:26:23 +0100673 char file_name[PATH_MAX];
Jens Axboeaf52b342007-03-13 10:07:47 +0100674 struct fio_file *f;
Jens Axboebd0ee742007-03-23 14:26:23 +0100675 int len = 0;
Jens Axboeaf52b342007-03-13 10:07:47 +0100676
Jens Axboeee56ad52008-02-01 10:30:20 +0100677 dprint(FD_FILE, "add file %s\n", fname);
678
Jens Axboef17c4392008-03-01 18:40:46 +0100679 f = smalloc(sizeof(*f));
Jens Axboeaf52b342007-03-13 10:07:47 +0100680 f->fd = -1;
Jens Axboebd0ee742007-03-23 14:26:23 +0100681
Jens Axboe9efef3c2008-03-06 10:26:25 +0100682 dprint(FD_FILE, "resize file array to %d files\n", cur_files + 1);
Jens Axboe126d65c2008-03-01 18:04:31 +0100683
Jens Axboe9efef3c2008-03-06 10:26:25 +0100684 td->files = realloc(td->files, (cur_files + 1) * sizeof(f));
Jens Axboe126d65c2008-03-01 18:04:31 +0100685 td->files[cur_files] = f;
686
Jens Axboe07eb79d2007-04-12 13:02:38 +0200687 /*
688 * init function, io engine may not be loaded yet
689 */
690 if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO))
691 f->real_file_size = -1ULL;
692
Jens Axboebd0ee742007-03-23 14:26:23 +0100693 if (td->o.directory)
694 len = sprintf(file_name, "%s/", td->o.directory);
695
696 sprintf(file_name + len, "%s", fname);
Jens Axboef17c4392008-03-01 18:40:46 +0100697 f->file_name = smalloc_strdup(file_name);
Jens Axboeaf52b342007-03-13 10:07:47 +0100698
Jens Axboee3bab462007-03-13 11:22:09 +0100699 get_file_type(f);
Jens Axboeaf52b342007-03-13 10:07:47 +0100700
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100701 switch (td->o.file_lock_mode) {
702 case FILE_LOCK_NONE:
703 break;
704 case FILE_LOCK_READWRITE:
705 f->lock = fio_mutex_rw_init();
706 break;
707 case FILE_LOCK_EXCLUSIVE:
708 f->lock = fio_mutex_init(1);
709 break;
710 default:
711 log_err("fio: unknown lock mode: %d\n", td->o.file_lock_mode);
712 assert(0);
713 }
Jens Axboe29c13492008-03-01 19:25:20 +0100714
Jens Axboe7b4e4fe2007-03-13 12:51:40 +0100715 td->files_index++;
Jens Axboe15494412007-03-13 11:52:47 +0100716 if (f->filetype == FIO_TYPE_FILE)
717 td->nr_normal_files++;
Jens Axboef29b25a2007-07-23 08:56:43 +0200718
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100719 dprint(FD_FILE, "file %p \"%s\" added at %d\n", f, f->file_name,
720 cur_files);
Jens Axboe9efef3c2008-03-06 10:26:25 +0100721
Jens Axboef29b25a2007-07-23 08:56:43 +0200722 return cur_files;
Jens Axboeaf52b342007-03-13 10:07:47 +0100723}
Jens Axboe0ad920e2007-03-13 11:06:45 +0100724
725void get_file(struct fio_file *f)
726{
Jens Axboe8172fe92008-02-01 21:51:02 +0100727 dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references);
Jens Axboe97af62c2007-05-22 11:12:13 +0200728 assert(f->flags & FIO_FILE_OPEN);
Jens Axboe0ad920e2007-03-13 11:06:45 +0100729 f->references++;
730}
731
Jens Axboe6977bcd2008-03-01 15:55:36 +0100732int put_file(struct thread_data *td, struct fio_file *f)
Jens Axboe0ad920e2007-03-13 11:06:45 +0100733{
Jens Axboe98e1ac42008-03-06 11:56:48 +0100734 int f_ret = 0, ret = 0;
Jens Axboe6977bcd2008-03-01 15:55:36 +0100735
Jens Axboe8172fe92008-02-01 21:51:02 +0100736 dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references);
Jens Axboeee56ad52008-02-01 10:30:20 +0100737
Jens Axboe0ad920e2007-03-13 11:06:45 +0100738 if (!(f->flags & FIO_FILE_OPEN))
Jens Axboe6977bcd2008-03-01 15:55:36 +0100739 return 0;
Jens Axboe0ad920e2007-03-13 11:06:45 +0100740
741 assert(f->references);
742 if (--f->references)
Jens Axboe6977bcd2008-03-01 15:55:36 +0100743 return 0;
Jens Axboe0ad920e2007-03-13 11:06:45 +0100744
Jens Axboed424d4d2007-04-17 09:05:10 +0200745 if (should_fsync(td) && td->o.fsync_on_close)
Jens Axboe98e1ac42008-03-06 11:56:48 +0100746 f_ret = fsync(f->fd);
Jens Axboeebb14152007-03-13 14:42:15 +0100747
Jens Axboe0ad920e2007-03-13 11:06:45 +0100748 if (td->io_ops->close_file)
Jens Axboe6977bcd2008-03-01 15:55:36 +0100749 ret = td->io_ops->close_file(td, f);
Jens Axboe1020a132007-03-29 11:15:06 +0200750
Jens Axboe98e1ac42008-03-06 11:56:48 +0100751 if (!ret)
752 ret = !f_ret;
753
Jens Axboe0ad920e2007-03-13 11:06:45 +0100754 td->nr_open_files--;
755 f->flags &= ~FIO_FILE_OPEN;
Jens Axboe6977bcd2008-03-01 15:55:36 +0100756 return ret;
Jens Axboe0ad920e2007-03-13 11:06:45 +0100757}
Jens Axboebbf6b542007-03-13 15:28:55 +0100758
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100759void lock_file(struct thread_data *td, struct fio_file *f, enum fio_ddir ddir)
Jens Axboeb2bd2bd2008-03-01 18:19:52 +0100760{
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100761 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
762 return;
Jens Axboe29c13492008-03-01 19:25:20 +0100763
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100764 if (f->lock_owner == td && f->lock_batch--)
765 return;
766
767 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
768 if (ddir == DDIR_READ)
769 fio_mutex_down_read(f->lock);
770 else
771 fio_mutex_down_write(f->lock);
772 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
773 fio_mutex_down(f->lock);
774
775 f->lock_owner = td;
776 f->lock_batch = td->o.lockfile_batch;
777 f->lock_ddir = ddir;
778}
779
780void unlock_file(struct thread_data *td, struct fio_file *f)
781{
782 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
783 return;
784 if (f->lock_batch)
785 return;
786
787 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
788 const int is_read = f->lock_ddir == DDIR_READ;
789 int val = fio_mutex_getval(f->lock);
790
791 if ((is_read && val == 1) || (!is_read && val == -1))
792 f->lock_owner = NULL;
793
794 if (is_read)
795 fio_mutex_up_read(f->lock);
796 else
797 fio_mutex_up_write(f->lock);
798 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE) {
799 int val = fio_mutex_getval(f->lock);
800
801 if (val == 0)
802 f->lock_owner = NULL;
803
804 fio_mutex_up(f->lock);
Jens Axboe29c13492008-03-01 19:25:20 +0100805 }
Jens Axboeb2bd2bd2008-03-01 18:19:52 +0100806}
807
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100808void unlock_file_all(struct thread_data *td, struct fio_file *f)
Jens Axboeb2bd2bd2008-03-01 18:19:52 +0100809{
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100810 if (f->lock_owner != td)
811 return;
Jens Axboe29c13492008-03-01 19:25:20 +0100812
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100813 f->lock_batch = 0;
814 unlock_file(td, f);
Jens Axboeb2bd2bd2008-03-01 18:19:52 +0100815}
816
Jens Axboebbf6b542007-03-13 15:28:55 +0100817static int recurse_dir(struct thread_data *td, const char *dirname)
818{
819 struct dirent *dir;
820 int ret = 0;
821 DIR *D;
822
823 D = opendir(dirname);
824 if (!D) {
Jens Axboe0ddb2702007-03-27 19:43:53 +0200825 char buf[FIO_VERROR_SIZE];
826
827 snprintf(buf, FIO_VERROR_SIZE - 1, "opendir(%s)", dirname);
828 td_verror(td, errno, buf);
Jens Axboebbf6b542007-03-13 15:28:55 +0100829 return 1;
830 }
831
832 while ((dir = readdir(D)) != NULL) {
833 char full_path[PATH_MAX];
834 struct stat sb;
835
Jens Axboee85b2b82007-03-14 09:39:06 +0100836 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
837 continue;
Jens Axboe96d32d52007-03-14 09:16:09 +0100838
Jens Axboebbf6b542007-03-13 15:28:55 +0100839 sprintf(full_path, "%s/%s", dirname, dir->d_name);
840
841 if (lstat(full_path, &sb) == -1) {
842 if (errno != ENOENT) {
843 td_verror(td, errno, "stat");
844 return 1;
845 }
846 }
847
848 if (S_ISREG(sb.st_mode)) {
849 add_file(td, full_path);
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100850 td->o.nr_files++;
Jens Axboebbf6b542007-03-13 15:28:55 +0100851 continue;
852 }
Jens Axboe0ddb2702007-03-27 19:43:53 +0200853 if (!S_ISDIR(sb.st_mode))
854 continue;
Jens Axboebbf6b542007-03-13 15:28:55 +0100855
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100856 ret = recurse_dir(td, full_path);
857 if (ret)
Jens Axboebbf6b542007-03-13 15:28:55 +0100858 break;
859 }
860
861 closedir(D);
862 return ret;
863}
864
865int add_dir_files(struct thread_data *td, const char *path)
866{
Jens Axboe0ddb2702007-03-27 19:43:53 +0200867 int ret = recurse_dir(td, path);
868
869 if (!ret)
870 log_info("fio: opendir added %d files\n", td->o.nr_files);
871
872 return ret;
Jens Axboebbf6b542007-03-13 15:28:55 +0100873}
Jens Axboecade3ef2007-03-23 15:29:45 +0100874
875void dup_files(struct thread_data *td, struct thread_data *org)
876{
877 struct fio_file *f;
878 unsigned int i;
Jens Axboe9efef3c2008-03-06 10:26:25 +0100879
880 dprint(FD_FILE, "dup files: %d\n", org->files_index);
Jens Axboecade3ef2007-03-23 15:29:45 +0100881
882 if (!org->files)
883 return;
884
Jens Axboe9efef3c2008-03-06 10:26:25 +0100885 td->files = malloc(org->files_index * sizeof(f));
Jens Axboecade3ef2007-03-23 15:29:45 +0100886
Jens Axboe9efef3c2008-03-06 10:26:25 +0100887 for_each_file(org, f, i) {
Jens Axboeb0fe4212008-03-01 18:22:27 +0100888 struct fio_file *__f;
889
Jens Axboef17c4392008-03-01 18:40:46 +0100890 __f = smalloc(sizeof(*__f));
Jens Axboeb0fe4212008-03-01 18:22:27 +0100891
Jens Axboecade3ef2007-03-23 15:29:45 +0100892 if (f->file_name)
Jens Axboef17c4392008-03-01 18:40:46 +0100893 __f->file_name = smalloc_strdup(f->file_name);
Jens Axboeb0fe4212008-03-01 18:22:27 +0100894
895 td->files[i] = __f;
Jens Axboecade3ef2007-03-23 15:29:45 +0100896 }
897}
Jens Axboef29b25a2007-07-23 08:56:43 +0200898
899/*
900 * Returns the index that matches the filename, or -1 if not there
901 */
902int get_fileno(struct thread_data *td, const char *fname)
903{
904 struct fio_file *f;
905 unsigned int i;
906
907 for_each_file(td, f, i)
908 if (!strcmp(f->file_name, fname))
909 return i;
910
911 return -1;
912}
913
914/*
915 * For log usage, where we add/open/close files automatically
916 */
917void free_release_files(struct thread_data *td)
918{
919 close_files(td);
920 td->files_index = 0;
921 td->nr_normal_files = 0;
922}