blob: b1ca9217c1f30a1a7bb8eec6452f1c0ecb8c1316 [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>
YAMAMOTO Takashid74ac842010-06-09 09:44:53 +02006#include <libgen.h>
Jens Axboe53cdc682006-10-18 11:50:58 +02007#include <sys/stat.h>
8#include <sys/mman.h>
Jens Axboebbf6b542007-03-13 15:28:55 +01009#include <sys/types.h>
Jens Axboe53cdc682006-10-18 11:50:58 +020010
11#include "fio.h"
Jens Axboef17c4392008-03-01 18:40:46 +010012#include "smalloc.h"
Jens Axboe4906e0b2008-03-01 18:59:51 +010013#include "filehash.h"
Bruce Cranecc314b2011-01-04 10:59:30 +010014#include "os/os.h"
Jens Axboe23162962012-11-07 19:47:47 +010015#include "hash.h"
Jens Axboe7ebd7962012-11-28 21:24:46 +010016#include "lib/axmap.h"
Jens Axboe53cdc682006-10-18 11:50:58 +020017
Eric Gourioua596f042011-06-17 09:11:45 +020018#ifdef FIO_HAVE_LINUX_FALLOCATE
19#include <linux/falloc.h>
20#endif
21
Jens Axboe7172cfe2007-07-23 14:36:00 +020022static int root_warn;
23
Jens Axboec592b9f2009-06-03 09:29:28 +020024static inline void clear_error(struct thread_data *td)
25{
26 td->error = 0;
27 td->verror[0] = '\0';
28}
29
Jens Axboe3baddf22008-04-04 11:10:30 +020030/*
31 * Leaves f->fd open on success, caller must close
32 */
Jens Axboe7bb48f82007-03-27 15:30:28 +020033static int extend_file(struct thread_data *td, struct fio_file *f)
Jens Axboe25205e92006-10-19 20:50:14 +020034{
Jens Axboeea443652007-03-29 14:52:13 +020035 int r, new_layout = 0, unlink_file = 0, flags;
Jens Axboe25205e92006-10-19 20:50:14 +020036 unsigned long long left;
37 unsigned int bs;
38 char *b;
Jens Axboeb2a15192006-10-18 14:10:42 +020039
Jens Axboe4241ea82007-09-12 08:18:36 +020040 if (read_only) {
41 log_err("fio: refusing extend of file due to read-only\n");
42 return 0;
43 }
44
Jens Axboe507a7022007-03-27 15:52:46 +020045 /*
46 * check if we need to lay the file out complete again. fio
47 * does that for operations involving reads, or for writes
48 * where overwrite is set
49 */
Jens Axboeffdfabd2008-03-26 09:18:14 +010050 if (td_read(td) || (td_write(td) && td->o.overwrite) ||
51 (td_write(td) && td->io_ops->flags & FIO_NOEXTEND))
Jens Axboe507a7022007-03-27 15:52:46 +020052 new_layout = 1;
Jens Axboeea443652007-03-29 14:52:13 +020053 if (td_write(td) && !td->o.overwrite)
54 unlink_file = 1;
Jens Axboe507a7022007-03-27 15:52:46 +020055
Jens Axboe6ae1f572008-02-21 10:20:00 +010056 if (unlink_file || new_layout) {
Jens Axboebd199f22008-03-26 09:57:18 +010057 dprint(FD_FILE, "layout unlink %s\n", f->file_name);
Zhang, Yanmin982016d2008-02-26 15:35:52 +010058 if ((unlink(f->file_name) < 0) && (errno != ENOENT)) {
Jens Axboe7bb48f82007-03-27 15:30:28 +020059 td_verror(td, errno, "unlink");
60 return 1;
61 }
62 }
63
Jens Axboe507a7022007-03-27 15:52:46 +020064 flags = O_WRONLY | O_CREAT;
65 if (new_layout)
66 flags |= O_TRUNC;
67
Jens Axboeee56ad52008-02-01 10:30:20 +010068 dprint(FD_FILE, "open file %s, flags %x\n", f->file_name, flags);
Jens Axboe507a7022007-03-27 15:52:46 +020069 f->fd = open(f->file_name, flags, 0644);
Jens Axboe53cdc682006-10-18 11:50:58 +020070 if (f->fd < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +010071 td_verror(td, errno, "open");
Jens Axboe53cdc682006-10-18 11:50:58 +020072 return 1;
73 }
74
Jens Axboe7bc8c2c2010-01-28 11:31:31 +010075#ifdef FIO_HAVE_FALLOCATE
Eric Gourioua596f042011-06-17 09:11:45 +020076 if (!td->o.fill_device) {
77 switch (td->o.fallocate_mode) {
78 case FIO_FALLOCATE_NONE:
79 break;
80 case FIO_FALLOCATE_POSIX:
81 dprint(FD_FILE, "posix_fallocate file %s size %llu\n",
82 f->file_name, f->real_file_size);
Jens Axboe7bc8c2c2010-01-28 11:31:31 +010083
Eric Gourioua596f042011-06-17 09:11:45 +020084 r = posix_fallocate(f->fd, 0, f->real_file_size);
85 if (r > 0) {
86 log_err("fio: posix_fallocate fails: %s\n",
87 strerror(r));
88 }
89 break;
90#ifdef FIO_HAVE_LINUX_FALLOCATE
91 case FIO_FALLOCATE_KEEP_SIZE:
92 dprint(FD_FILE,
93 "fallocate(FALLOC_FL_KEEP_SIZE) "
94 "file %s size %llu\n",
95 f->file_name, f->real_file_size);
96
97 r = fallocate(f->fd, FALLOC_FL_KEEP_SIZE, 0,
98 f->real_file_size);
99 if (r != 0) {
100 td_verror(td, errno, "fallocate");
101 }
102 break;
103#endif /* FIO_HAVE_LINUX_FALLOCATE */
104 default:
105 log_err("fio: unknown fallocate mode: %d\n",
106 td->o.fallocate_mode);
107 assert(0);
Jens Axboe7bc8c2c2010-01-28 11:31:31 +0100108 }
109 }
Eric Gourioua596f042011-06-17 09:11:45 +0200110#endif /* FIO_HAVE_FALLOCATE */
Bruce Cran9b836562011-01-08 19:49:54 +0100111
Shawn Lewisfc74ac12008-01-11 09:45:11 +0100112 if (!new_layout)
113 goto done;
114
Jens Axboe5e0074c2009-06-02 21:40:09 +0200115 /*
116 * The size will be -1ULL when fill_device is used, so don't truncate
117 * or fallocate this file, just write it
118 */
119 if (!td->o.fill_device) {
120 dprint(FD_FILE, "truncate file %s, size %llu\n", f->file_name,
Jens Axboeee56ad52008-02-01 10:30:20 +0100121 f->real_file_size);
Jens Axboe5e0074c2009-06-02 21:40:09 +0200122 if (ftruncate(f->fd, f->real_file_size) == -1) {
123 td_verror(td, errno, "ftruncate");
124 goto err;
125 }
Jens Axboe5e0074c2009-06-02 21:40:09 +0200126 }
Jens Axboe40f82982006-10-25 11:21:05 +0200127
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100128 b = malloc(td->o.max_bs[DDIR_WRITE]);
129 memset(b, 0, td->o.max_bs[DDIR_WRITE]);
Jens Axboe53cdc682006-10-18 11:50:58 +0200130
Jens Axboe7bb48f82007-03-27 15:30:28 +0200131 left = f->real_file_size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200132 while (left && !td->terminate) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100133 bs = td->o.max_bs[DDIR_WRITE];
Jens Axboe53cdc682006-10-18 11:50:58 +0200134 if (bs > left)
135 bs = left;
136
137 r = write(f->fd, b, bs);
138
Jens Axboe5e0074c2009-06-02 21:40:09 +0200139 if (r > 0) {
140 left -= r;
Jens Axboe53cdc682006-10-18 11:50:58 +0200141 continue;
142 } else {
Jens Axboe5e0074c2009-06-02 21:40:09 +0200143 if (r < 0) {
144 int __e = errno;
145
146 if (__e == ENOSPC) {
147 if (td->o.fill_device)
148 break;
149 log_info("fio: ENOSPC on laying out "
150 "file, stopping\n");
151 break;
152 }
Jens Axboee1161c32007-02-22 19:36:48 +0100153 td_verror(td, errno, "write");
Jens Axboe5e0074c2009-06-02 21:40:09 +0200154 } else
Jens Axboee1161c32007-02-22 19:36:48 +0100155 td_verror(td, EIO, "write");
Jens Axboe53cdc682006-10-18 11:50:58 +0200156
157 break;
158 }
159 }
160
Jens Axboeffdfabd2008-03-26 09:18:14 +0100161 if (td->terminate) {
162 dprint(FD_FILE, "terminate unlink %s\n", f->file_name);
Jens Axboe53cdc682006-10-18 11:50:58 +0200163 unlink(f->file_name);
Jens Axboeffdfabd2008-03-26 09:18:14 +0100164 } else if (td->o.create_fsync) {
Jens Axboe98e1ac42008-03-06 11:56:48 +0100165 if (fsync(f->fd) < 0) {
166 td_verror(td, errno, "fsync");
167 goto err;
168 }
169 }
Jens Axboe0d1cd202009-06-05 22:11:52 +0200170 if (td->o.fill_device && !td_write(td)) {
Jens Axboed6aed792009-06-03 08:41:15 +0200171 fio_file_clear_size_known(f);
Jens Axboe5e0074c2009-06-02 21:40:09 +0200172 if (td_io_get_file_size(td, f))
173 goto err;
174 if (f->io_size > f->real_file_size)
175 f->io_size = f->real_file_size;
176 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200177
178 free(b);
Jens Axboe507a7022007-03-27 15:52:46 +0200179done:
Jens Axboe53cdc682006-10-18 11:50:58 +0200180 return 0;
181err:
182 close(f->fd);
183 f->fd = -1;
184 return 1;
185}
186
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200187static int pre_read_file(struct thread_data *td, struct fio_file *f)
188{
Jens Axboeb0f65862009-05-20 11:52:15 +0200189 int r, did_open = 0, old_runstate;
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200190 unsigned long long left;
191 unsigned int bs;
192 char *b;
193
Jens Axboe9c0d2242009-07-01 12:26:28 +0200194 if (td->io_ops->flags & FIO_PIPEIO)
195 return 0;
196
Jens Axboed6aed792009-06-03 08:41:15 +0200197 if (!fio_file_open(f)) {
Jens Axboeb0f65862009-05-20 11:52:15 +0200198 if (td->io_ops->open_file(td, f)) {
199 log_err("fio: cannot pre-read, failed to open file\n");
200 return 1;
201 }
202 did_open = 1;
203 }
204
205 old_runstate = td->runstate;
206 td_set_runstate(td, TD_PRE_READING);
207
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200208 bs = td->o.max_bs[DDIR_READ];
209 b = malloc(bs);
210 memset(b, 0, bs);
211
212 lseek(f->fd, f->file_offset, SEEK_SET);
213 left = f->io_size;
214
215 while (left && !td->terminate) {
216 if (bs > left)
217 bs = left;
218
219 r = read(f->fd, b, bs);
220
221 if (r == (int) bs) {
222 left -= bs;
223 continue;
224 } else {
225 td_verror(td, EIO, "pre_read");
226 break;
227 }
228 }
229
Jens Axboeb0f65862009-05-20 11:52:15 +0200230 td_set_runstate(td, old_runstate);
231
232 if (did_open)
233 td->io_ops->close_file(td, f);
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200234 free(b);
235 return 0;
236}
237
Jens Axboe7bb48f82007-03-27 15:30:28 +0200238static unsigned long long get_rand_file_size(struct thread_data *td)
Jens Axboe9c60ce62007-03-15 09:14:47 +0100239{
Jens Axboedc873b62008-06-04 20:13:04 +0200240 unsigned long long ret, sized;
Jens Axboe1294c3e2011-05-11 08:15:18 +0200241 unsigned long r;
Jens Axboe9c60ce62007-03-15 09:14:47 +0100242
Jens Axboe4c07ad82011-03-28 09:51:09 +0200243 if (td->o.use_os_rand) {
244 r = os_random_long(&td->file_size_state);
245 sized = td->o.file_size_high - td->o.file_size_low;
246 ret = (unsigned long long) ((double) sized * (r / (OS_RAND_MAX + 1.0)));
247 } else {
248 r = __rand(&td->__file_size_state);
249 sized = td->o.file_size_high - td->o.file_size_low;
250 ret = (unsigned long long) ((double) sized * (r / (FRAND_MAX + 1.0)));
251 }
252
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100253 ret += td->o.file_size_low;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100254 ret -= (ret % td->o.rw_min_bs);
Jens Axboe9c60ce62007-03-15 09:14:47 +0100255 return ret;
256}
257
Jens Axboe53cdc682006-10-18 11:50:58 +0200258static int file_size(struct thread_data *td, struct fio_file *f)
259{
260 struct stat st;
261
Jens Axboedf9c26b2009-03-05 10:13:58 +0100262 if (stat(f->file_name, &st) == -1) {
Jens Axboe7bb48f82007-03-27 15:30:28 +0200263 td_verror(td, errno, "fstat");
264 return 1;
265 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200266
Jens Axboe7bb48f82007-03-27 15:30:28 +0200267 f->real_file_size = st.st_size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200268 return 0;
269}
270
271static int bdev_size(struct thread_data *td, struct fio_file *f)
272{
Bruce Cran9b836562011-01-08 19:49:54 +0100273 unsigned long long bytes = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200274 int r;
275
Jens Axboedf9c26b2009-03-05 10:13:58 +0100276 if (td->io_ops->open_file(td, f)) {
277 log_err("fio: failed opening blockdev %s for size check\n",
278 f->file_name);
279 return 1;
280 }
281
Bruce Cranecc314b2011-01-04 10:59:30 +0100282 r = blockdev_size(f, &bytes);
Jens Axboe53cdc682006-10-18 11:50:58 +0200283 if (r) {
Jens Axboee1161c32007-02-22 19:36:48 +0100284 td_verror(td, r, "blockdev_size");
Jens Axboedf9c26b2009-03-05 10:13:58 +0100285 goto err;
Jens Axboe53cdc682006-10-18 11:50:58 +0200286 }
287
Jens Axboe7ab077a2009-02-02 15:07:37 +0100288 if (!bytes) {
289 log_err("%s: zero sized block device?\n", f->file_name);
Jens Axboedf9c26b2009-03-05 10:13:58 +0100290 goto err;
Jens Axboe7ab077a2009-02-02 15:07:37 +0100291 }
292
Jens Axboe53cdc682006-10-18 11:50:58 +0200293 f->real_file_size = bytes;
Jens Axboe22a57ba2009-06-09 14:34:35 +0200294 td->io_ops->close_file(td, f);
Jens Axboe53cdc682006-10-18 11:50:58 +0200295 return 0;
Jens Axboedf9c26b2009-03-05 10:13:58 +0100296err:
297 td->io_ops->close_file(td, f);
298 return 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200299}
300
Jens Axboe4ccdccd2010-06-24 10:27:22 +0200301static int char_size(struct thread_data *td, struct fio_file *f)
302{
303#ifdef FIO_HAVE_CHARDEV_SIZE
Bruce Cran9b836562011-01-08 19:49:54 +0100304 unsigned long long bytes = 0;
Jens Axboe4ccdccd2010-06-24 10:27:22 +0200305 int r;
306
307 if (td->io_ops->open_file(td, f)) {
308 log_err("fio: failed opening blockdev %s for size check\n",
309 f->file_name);
310 return 1;
311 }
312
Bruce Cranecc314b2011-01-04 10:59:30 +0100313 r = chardev_size(f, &bytes);
Jens Axboe4ccdccd2010-06-24 10:27:22 +0200314 if (r) {
315 td_verror(td, r, "chardev_size");
316 goto err;
317 }
318
319 if (!bytes) {
320 log_err("%s: zero sized char device?\n", f->file_name);
321 goto err;
322 }
323
324 f->real_file_size = bytes;
325 td->io_ops->close_file(td, f);
326 return 0;
327err:
328 td->io_ops->close_file(td, f);
329 return 1;
330#else
331 f->real_file_size = -1ULL;
332 return 0;
333#endif
334}
335
Jens Axboe53cdc682006-10-18 11:50:58 +0200336static int get_file_size(struct thread_data *td, struct fio_file *f)
337{
338 int ret = 0;
339
Jens Axboed6aed792009-06-03 08:41:15 +0200340 if (fio_file_size_known(f))
Jens Axboe409b3412007-03-28 09:57:01 +0200341 return 0;
342
Jens Axboe7bb48f82007-03-27 15:30:28 +0200343 if (f->filetype == FIO_TYPE_FILE)
344 ret = file_size(td, f);
345 else if (f->filetype == FIO_TYPE_BD)
Jens Axboe53cdc682006-10-18 11:50:58 +0200346 ret = bdev_size(td, f);
Jens Axboe4ccdccd2010-06-24 10:27:22 +0200347 else if (f->filetype == FIO_TYPE_CHAR)
348 ret = char_size(td, f);
Jens Axboe53cdc682006-10-18 11:50:58 +0200349 else
350 f->real_file_size = -1;
351
352 if (ret)
353 return ret;
354
355 if (f->file_offset > f->real_file_size) {
Bruce Cran0f2152c2010-12-15 10:33:03 +0100356 log_err("%s: offset extends end (%llu > %llu)\n", td->o.name,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100357 f->file_offset, f->real_file_size);
Jens Axboe53cdc682006-10-18 11:50:58 +0200358 return 1;
359 }
360
Jens Axboed6aed792009-06-03 08:41:15 +0200361 fio_file_set_size_known(f);
Jens Axboe53cdc682006-10-18 11:50:58 +0200362 return 0;
363}
364
Jens Axboe3baddf22008-04-04 11:10:30 +0200365static int __file_invalidate_cache(struct thread_data *td, struct fio_file *f,
366 unsigned long long off,
367 unsigned long long len)
Jens Axboee5b401d2006-10-18 16:03:40 +0200368{
369 int ret = 0;
370
Jens Axboe5e0074c2009-06-02 21:40:09 +0200371 if (len == -1ULL)
Jens Axboe3baddf22008-04-04 11:10:30 +0200372 len = f->io_size;
373 if (off == -1ULL)
374 off = f->file_offset;
Jens Axboeee56ad52008-02-01 10:30:20 +0100375
Jens Axboe0d1cd202009-06-05 22:11:52 +0200376 if (len == -1ULL || off == -1ULL)
377 return 0;
378
Jens Axboe3baddf22008-04-04 11:10:30 +0200379 dprint(FD_IO, "invalidate cache %s: %llu/%llu\n", f->file_name, off,
380 len);
Jens Axboeb5af8292007-03-08 12:43:13 +0100381
Jens Axboee5b401d2006-10-18 16:03:40 +0200382 /*
383 * FIXME: add blockdev flushing too
384 */
Jens Axboea1c58072009-08-04 23:17:02 +0200385 if (f->mmap_ptr) {
Bruce Cran03e20d62011-01-02 20:14:54 +0100386 ret = posix_madvise(f->mmap_ptr, f->mmap_sz, POSIX_MADV_DONTNEED);
Jens Axboea1c58072009-08-04 23:17:02 +0200387#ifdef FIO_MADV_FREE
Bruce Cran03e20d62011-01-02 20:14:54 +0100388 (void) posix_madvise(f->mmap_ptr, f->mmap_sz, FIO_MADV_FREE);
Jens Axboea1c58072009-08-04 23:17:02 +0200389#endif
390 } else if (f->filetype == FIO_TYPE_FILE) {
Bruce Cranecc314b2011-01-04 10:59:30 +0100391 ret = posix_fadvise(f->fd, off, len, POSIX_FADV_DONTNEED);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100392 } else if (f->filetype == FIO_TYPE_BD) {
Bruce Cranecc314b2011-01-04 10:59:30 +0100393 ret = blockdev_invalidate_cache(f);
Jens Axboe7e0e25c2007-03-27 12:49:56 +0200394 if (ret < 0 && errno == EACCES && geteuid()) {
Jens Axboe7172cfe2007-07-23 14:36:00 +0200395 if (!root_warn) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100396 log_err("fio: only root may flush block "
397 "devices. Cache flush bypassed!\n");
Jens Axboe7172cfe2007-07-23 14:36:00 +0200398 root_warn = 1;
399 }
Jens Axboe7e0e25c2007-03-27 12:49:56 +0200400 ret = 0;
401 }
Jens Axboeb5605e92007-04-16 20:42:33 +0200402 } else if (f->filetype == FIO_TYPE_CHAR || f->filetype == FIO_TYPE_PIPE)
Jens Axboee5b401d2006-10-18 16:03:40 +0200403 ret = 0;
404
405 if (ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100406 td_verror(td, errno, "invalidate_cache");
Jens Axboee5b401d2006-10-18 16:03:40 +0200407 return 1;
Jens Axboe3baddf22008-04-04 11:10:30 +0200408 } else if (ret > 0) {
409 td_verror(td, ret, "invalidate_cache");
410 return 1;
Jens Axboee5b401d2006-10-18 16:03:40 +0200411 }
412
Jens Axboead2da602007-02-26 12:33:27 +0100413 return ret;
Jens Axboe3baddf22008-04-04 11:10:30 +0200414
415}
416
417int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
418{
Jens Axboed6aed792009-06-03 08:41:15 +0200419 if (!fio_file_open(f))
Jens Axboea5fb4612008-05-28 10:54:01 +0200420 return 0;
421
Jens Axboe5e0074c2009-06-02 21:40:09 +0200422 return __file_invalidate_cache(td, f, -1ULL, -1ULL);
Jens Axboee5b401d2006-10-18 16:03:40 +0200423}
424
Jens Axboe6977bcd2008-03-01 15:55:36 +0100425int generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
Jens Axboe53cdc682006-10-18 11:50:58 +0200426{
Jens Axboe6977bcd2008-03-01 15:55:36 +0100427 int ret = 0;
428
Jens Axboeee56ad52008-02-01 10:30:20 +0100429 dprint(FD_FILE, "fd close %s\n", f->file_name);
Jens Axboe4906e0b2008-03-01 18:59:51 +0100430
431 remove_file_hash(f);
432
Jens Axboe6977bcd2008-03-01 15:55:36 +0100433 if (close(f->fd) < 0)
434 ret = errno;
435
Jens Axboeb5af8292007-03-08 12:43:13 +0100436 f->fd = -1;
Jens Axboee6c4d732012-12-12 08:16:27 +0100437
438 if (f->shadow_fd != -1) {
439 close(f->shadow_fd);
440 f->shadow_fd = -1;
441 }
442
Jens Axboe6977bcd2008-03-01 15:55:36 +0100443 return ret;
Jens Axboe53cdc682006-10-18 11:50:58 +0200444}
445
Dmitry Monakhov1ccc6dc2012-09-19 23:22:53 +0400446int file_lookup_open(struct fio_file *f, int flags)
Jens Axboe53cdc682006-10-18 11:50:58 +0200447{
Jens Axboe29c13492008-03-01 19:25:20 +0100448 struct fio_file *__f;
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100449 int from_hash;
450
451 __f = lookup_file_hash(f->file_name);
452 if (__f) {
Jens Axboe9efef3c2008-03-06 10:26:25 +0100453 dprint(FD_FILE, "found file in hash %s\n", f->file_name);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100454 /*
455 * racy, need the __f->lock locked
456 */
457 f->lock = __f->lock;
458 f->lock_owner = __f->lock_owner;
459 f->lock_batch = __f->lock_batch;
460 f->lock_ddir = __f->lock_ddir;
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100461 from_hash = 1;
462 } else {
Jens Axboe9efef3c2008-03-06 10:26:25 +0100463 dprint(FD_FILE, "file not found in hash %s\n", f->file_name);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100464 from_hash = 0;
465 }
466
Jens Axboee8670ef2008-03-06 12:06:30 +0100467 f->fd = open(f->file_name, flags, 0600);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100468 return from_hash;
469}
470
Jens Axboee6c4d732012-12-12 08:16:27 +0100471static int file_close_shadow_fds(struct thread_data *td)
472{
473 struct fio_file *f;
474 int num_closed = 0;
475 unsigned int i;
476
477 for_each_file(td, f, i) {
478 if (f->shadow_fd == -1)
479 continue;
480
481 close(f->shadow_fd);
482 f->shadow_fd = -1;
483 num_closed++;
484 }
485
486 return num_closed;
487}
488
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100489int generic_open_file(struct thread_data *td, struct fio_file *f)
490{
Jens Axboe66159822007-04-16 21:54:24 +0200491 int is_std = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200492 int flags = 0;
Jens Axboe29c13492008-03-01 19:25:20 +0100493 int from_hash = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200494
Jens Axboeee56ad52008-02-01 10:30:20 +0100495 dprint(FD_FILE, "fd open %s\n", f->file_name);
496
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200497 if (td_trim(td) && f->filetype != FIO_TYPE_BD) {
498 log_err("fio: trim only applies to block device\n");
499 return 1;
500 }
501
Jens Axboe66159822007-04-16 21:54:24 +0200502 if (!strcmp(f->file_name, "-")) {
503 if (td_rw(td)) {
504 log_err("fio: can't read/write to stdin/out\n");
505 return 1;
506 }
507 is_std = 1;
Jens Axboece98fa62007-04-17 13:27:32 +0200508
509 /*
510 * move output logging to stderr, if we are writing to stdout
511 */
512 if (td_write(td))
513 f_out = stderr;
Jens Axboe66159822007-04-16 21:54:24 +0200514 }
515
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200516 if (td_trim(td))
517 goto skip_flags;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100518 if (td->o.odirect)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100519 flags |= OS_O_DIRECT;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100520 if (td->o.sync_io)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100521 flags |= O_SYNC;
Jens Axboe814452b2009-03-04 12:53:13 +0100522 if (td->o.create_on_open)
523 flags |= O_CREAT;
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200524skip_flags:
525 if (f->filetype != FIO_TYPE_FILE)
526 flags |= FIO_O_NOATIME;
Jens Axboe7abf8332006-11-23 15:01:19 +0100527
Aaron Carroll056f3452007-11-26 09:08:53 +0100528open_again:
Jens Axboe660a1cb2007-04-17 15:37:47 +0200529 if (td_write(td)) {
Jens Axboe17308152008-03-07 13:42:31 +0100530 if (!read_only)
531 flags |= O_RDWR;
Jens Axboe53cdc682006-10-18 11:50:58 +0200532
Jens Axboeaf52b342007-03-13 10:07:47 +0100533 if (f->filetype == FIO_TYPE_FILE)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100534 flags |= O_CREAT;
Jens Axboe2fd233b2007-03-02 08:44:25 +0100535
Jens Axboe66159822007-04-16 21:54:24 +0200536 if (is_std)
537 f->fd = dup(STDOUT_FILENO);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100538 else
539 from_hash = file_lookup_open(f, flags);
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200540 } else if (td_read(td)) {
Jens Axboe4241ea82007-09-12 08:18:36 +0200541 if (f->filetype == FIO_TYPE_CHAR && !read_only)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100542 flags |= O_RDWR;
543 else
544 flags |= O_RDONLY;
545
Jens Axboe66159822007-04-16 21:54:24 +0200546 if (is_std)
547 f->fd = dup(STDIN_FILENO);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100548 else
549 from_hash = file_lookup_open(f, flags);
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200550 } else { //td trim
551 flags |= O_RDWR;
552 from_hash = file_lookup_open(f, flags);
Jens Axboe53cdc682006-10-18 11:50:58 +0200553 }
554
555 if (f->fd == -1) {
Jens Axboee4e33252007-03-21 13:07:54 +0100556 char buf[FIO_VERROR_SIZE];
Jens Axboee1161c32007-02-22 19:36:48 +0100557 int __e = errno;
558
Jens Axboe835d9b92009-06-09 15:23:38 +0200559 if (__e == EPERM && (flags & FIO_O_NOATIME)) {
Jens Axboe5921e802008-05-30 15:02:38 +0200560 flags &= ~FIO_O_NOATIME;
Aaron Carroll056f3452007-11-26 09:08:53 +0100561 goto open_again;
562 }
Jens Axboee6c4d732012-12-12 08:16:27 +0100563 if (__e == EMFILE && file_close_shadow_fds(td))
564 goto open_again;
Aaron Carroll056f3452007-11-26 09:08:53 +0100565
Jens Axboee8670ef2008-03-06 12:06:30 +0100566 snprintf(buf, sizeof(buf) - 1, "open(%s)", f->file_name);
Jens Axboee4e33252007-03-21 13:07:54 +0100567
Jens Axboea93c5f02012-06-11 08:53:53 +0200568 if (__e == EINVAL && (flags & OS_O_DIRECT)) {
569 log_err("fio: looks like your file system does not " \
570 "support direct=1/buffered=0\n");
571 }
572
Jens Axboee4e33252007-03-21 13:07:54 +0100573 td_verror(td, __e, buf);
Jens Axboe53cdc682006-10-18 11:50:58 +0200574 }
575
Jens Axboe29c13492008-03-01 19:25:20 +0100576 if (!from_hash && f->fd != -1) {
577 if (add_file_hash(f)) {
Jens Axboe3f0ca9b2011-05-24 21:36:24 +0200578 int fio_unused ret;
Jens Axboe29c13492008-03-01 19:25:20 +0100579
580 /*
Jens Axboee6c4d732012-12-12 08:16:27 +0100581 * Stash away descriptor for later close. This is to
582 * work-around a "feature" on Linux, where a close of
583 * an fd that has been opened for write will trigger
584 * udev to call blkid to check partitions, fs id, etc.
585 * That polutes the device cache, which can slow down
586 * unbuffered accesses.
Jens Axboe29c13492008-03-01 19:25:20 +0100587 */
Jens Axboee6c4d732012-12-12 08:16:27 +0100588 if (f->shadow_fd == -1)
589 f->shadow_fd = f->fd;
590 else {
591 /*
592 * OK to ignore, we haven't done anything
593 * with it
594 */
595 ret = generic_close_file(td, f);
596 }
Jens Axboe29c13492008-03-01 19:25:20 +0100597 goto open_again;
598 }
599 }
Jens Axboe4906e0b2008-03-01 18:59:51 +0100600
Jens Axboe53cdc682006-10-18 11:50:58 +0200601 return 0;
Jens Axboeb5af8292007-03-08 12:43:13 +0100602}
603
Jens Axboedf9c26b2009-03-05 10:13:58 +0100604int generic_get_file_size(struct thread_data *td, struct fio_file *f)
Jens Axboe21972cd2006-11-23 12:39:16 +0100605{
Jens Axboedf9c26b2009-03-05 10:13:58 +0100606 return get_file_size(td, f);
Jens Axboe21972cd2006-11-23 12:39:16 +0100607}
608
Jens Axboe7bb48f82007-03-27 15:30:28 +0200609/*
610 * open/close all files, so that ->real_file_size gets set
611 */
Jens Axboebab3fd52007-04-02 10:34:18 +0200612static int get_file_sizes(struct thread_data *td)
Jens Axboe53cdc682006-10-18 11:50:58 +0200613{
614 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100615 unsigned int i;
Jens Axboebab3fd52007-04-02 10:34:18 +0200616 int err = 0;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200617
618 for_each_file(td, f, i) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100619 dprint(FD_FILE, "get file size for %p/%d/%p\n", f, i,
620 f->file_name);
Jens Axboe9efef3c2008-03-06 10:26:25 +0100621
Jens Axboe99a47c62009-04-07 22:20:56 +0200622 if (td_io_get_file_size(td, f)) {
Jens Axboe40b44f42007-04-11 12:43:35 +0200623 if (td->error != ENOENT) {
624 log_err("%s\n", td->verror);
625 err = 1;
626 }
Jens Axboe541d66d2007-03-27 19:50:11 +0200627 clear_error(td);
Jens Axboe07eb79d2007-04-12 13:02:38 +0200628 }
Jens Axboe409b3412007-03-28 09:57:01 +0200629
630 if (f->real_file_size == -1ULL && td->o.size)
631 f->real_file_size = td->o.size / td->o.nr_files;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200632 }
Jens Axboebab3fd52007-04-02 10:34:18 +0200633
634 return err;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200635}
636
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200637struct fio_mount {
638 struct flist_head list;
639 const char *base;
640 char __base[256];
641 unsigned int key;
642};
643
644/*
645 * Get free number of bytes for each file on each unique mount.
646 */
647static unsigned long long get_fs_free_counts(struct thread_data *td)
648{
649 struct flist_head *n, *tmp;
Jens Axboe68b03162010-06-24 09:22:41 +0200650 unsigned long long ret = 0;
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200651 struct fio_mount *fm;
652 FLIST_HEAD(list);
653 struct fio_file *f;
654 unsigned int i;
655
656 for_each_file(td, f, i) {
657 struct stat sb;
658 char buf[256];
659
Jens Axboe4ccdccd2010-06-24 10:27:22 +0200660 if (f->filetype == FIO_TYPE_BD || f->filetype == FIO_TYPE_CHAR) {
661 if (f->real_file_size != -1ULL)
662 ret += f->real_file_size;
Jens Axboe68b03162010-06-24 09:22:41 +0200663 continue;
664 } else if (f->filetype != FIO_TYPE_FILE)
665 continue;
666
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200667 strcpy(buf, f->file_name);
668
669 if (stat(buf, &sb) < 0) {
670 if (errno != ENOENT)
671 break;
672 strcpy(buf, ".");
673 if (stat(buf, &sb) < 0)
674 break;
675 }
676
677 fm = NULL;
678 flist_for_each(n, &list) {
679 fm = flist_entry(n, struct fio_mount, list);
680 if (fm->key == sb.st_dev)
681 break;
682
683 fm = NULL;
684 }
685
686 if (fm)
687 continue;
688
689 fm = malloc(sizeof(*fm));
690 strcpy(fm->__base, buf);
691 fm->base = basename(fm->__base);
692 fm->key = sb.st_dev;
693 flist_add(&fm->list, &list);
694 }
695
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200696 flist_for_each_safe(n, tmp, &list) {
697 unsigned long long sz;
698
699 fm = flist_entry(n, struct fio_mount, list);
700 flist_del(&fm->list);
701
702 sz = get_fs_size(fm->base);
703 if (sz && sz != -1ULL)
704 ret += sz;
705
706 free(fm);
707 }
708
709 return ret;
710}
711
Jens Axboe293b8c12013-01-04 13:16:54 +0100712uint64_t get_start_offset(struct thread_data *td)
Dan Ehrenbergce95d652012-08-16 08:58:21 +0200713{
714 return td->o.start_offset +
715 (td->thread_number - 1) * td->o.offset_increment;
716}
717
Jens Axboe7bb48f82007-03-27 15:30:28 +0200718/*
719 * Open the files and setup files sizes, creating files if necessary.
720 */
721int setup_files(struct thread_data *td)
722{
723 unsigned long long total_size, extend_size;
724 struct fio_file *f;
725 unsigned int i;
Jens Axboe000b0802007-03-27 15:56:10 +0200726 int err = 0, need_extend;
Jens Axboe53cdc682006-10-18 11:50:58 +0200727
Jens Axboeee56ad52008-02-01 10:30:20 +0100728 dprint(FD_FILE, "setup files\n");
729
Jens Axboe691c8fb2008-03-07 14:26:26 +0100730 if (td->o.read_iolog_file)
Jens Axboe25460cf2012-05-02 13:58:02 +0200731 goto done;
Jens Axboe691c8fb2008-03-07 14:26:26 +0100732
Jens Axboe53cdc682006-10-18 11:50:58 +0200733 /*
734 * if ioengine defines a setup() method, it's responsible for
Jens Axboe7bb48f82007-03-27 15:30:28 +0200735 * opening the files and setting f->real_file_size to indicate
736 * the valid range for that file.
Jens Axboe53cdc682006-10-18 11:50:58 +0200737 */
738 if (td->io_ops->setup)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200739 err = td->io_ops->setup(td);
740 else
Jens Axboebab3fd52007-04-02 10:34:18 +0200741 err = get_file_sizes(td);
Jens Axboe53cdc682006-10-18 11:50:58 +0200742
Jens Axboef1027062006-10-20 10:14:01 +0200743 if (err)
744 return err;
745
Jens Axboe0a7eb122006-10-20 10:36:42 +0200746 /*
Jens Axboe7bb48f82007-03-27 15:30:28 +0200747 * check sizes. if the files/devices do not exist and the size
748 * isn't passed to fio, abort.
Jens Axboe0a7eb122006-10-20 10:36:42 +0200749 */
Jens Axboe7bb48f82007-03-27 15:30:28 +0200750 total_size = 0;
751 for_each_file(td, f, i) {
752 if (f->real_file_size == -1ULL)
753 total_size = -1ULL;
754 else
755 total_size += f->real_file_size;
756 }
Jens Axboe0a7eb122006-10-20 10:36:42 +0200757
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200758 if (td->o.fill_device)
759 td->fill_device_size = get_fs_free_counts(td);
760
Jens Axboe7bb48f82007-03-27 15:30:28 +0200761 /*
762 * device/file sizes are zero and no size given, punt
763 */
Jens Axboe1f809d12007-10-25 18:34:02 +0200764 if ((!total_size || total_size == -1ULL) && !td->o.size &&
Shawn Lewisaa31f1f2008-01-11 09:45:11 +0100765 !(td->io_ops->flags & FIO_NOIO) && !td->o.fill_device) {
Jens Axboe7bb48f82007-03-27 15:30:28 +0200766 log_err("%s: you need to specify size=\n", td->o.name);
Jens Axboee1161c32007-02-22 19:36:48 +0100767 td_verror(td, EINVAL, "total_file_size");
Jens Axboe53cdc682006-10-18 11:50:58 +0200768 return 1;
769 }
770
Jens Axboe7bb48f82007-03-27 15:30:28 +0200771 /*
772 * now file sizes are known, so we can set ->io_size. if size= is
773 * not given, ->io_size is just equal to ->real_file_size. if size
774 * is given, ->io_size is size / nr_files.
775 */
776 extend_size = total_size = 0;
777 need_extend = 0;
778 for_each_file(td, f, i) {
Dan Ehrenbergce95d652012-08-16 08:58:21 +0200779 f->file_offset = get_start_offset(td);
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200780
Jens Axboe7bb48f82007-03-27 15:30:28 +0200781 if (!td->o.file_size_low) {
782 /*
783 * no file size range given, file size is equal to
784 * total size divided by number of files. if that is
785 * zero, set it to the real file size.
786 */
787 f->io_size = td->o.size / td->o.nr_files;
Jens Axboe65bdb102008-01-24 13:13:12 +0100788 if (!f->io_size)
Jens Axboe273f8c92008-01-25 14:02:15 +0100789 f->io_size = f->real_file_size - f->file_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200790 } else if (f->real_file_size < td->o.file_size_low ||
791 f->real_file_size > td->o.file_size_high) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100792 if (f->file_offset > td->o.file_size_low)
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200793 goto err_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200794 /*
795 * file size given. if it's fixed, use that. if it's a
796 * range, generate a random size in-between.
797 */
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100798 if (td->o.file_size_low == td->o.file_size_high) {
799 f->io_size = td->o.file_size_low
800 - f->file_offset;
801 } else {
802 f->io_size = get_rand_file_size(td)
803 - f->file_offset;
804 }
Jens Axboe65bdb102008-01-24 13:13:12 +0100805 } else
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200806 f->io_size = f->real_file_size - f->file_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200807
808 if (f->io_size == -1ULL)
809 total_size = -1ULL;
Shaohua Li4d002562012-09-21 08:32:32 +0200810 else {
811 if (td->o.size_percent)
812 f->io_size = (f->io_size * td->o.size_percent) / 100;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200813 total_size += f->io_size;
Shaohua Li4d002562012-09-21 08:32:32 +0200814 }
Jens Axboe7bb48f82007-03-27 15:30:28 +0200815
816 if (f->filetype == FIO_TYPE_FILE &&
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200817 (f->io_size + f->file_offset) > f->real_file_size &&
Jens Axboe7bb48f82007-03-27 15:30:28 +0200818 !(td->io_ops->flags & FIO_DISKLESSIO)) {
Jens Axboe814452b2009-03-04 12:53:13 +0100819 if (!td->o.create_on_open) {
820 need_extend++;
821 extend_size += (f->io_size + f->file_offset);
822 } else
823 f->real_file_size = f->io_size + f->file_offset;
Jens Axboed6aed792009-06-03 08:41:15 +0200824 fio_file_set_extend(f);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100825 }
Jens Axboe7bb48f82007-03-27 15:30:28 +0200826 }
827
ljzhang,Yaxin Hu,Jianchao Tang22982902007-07-27 13:21:28 +0200828 if (!td->o.size || td->o.size > total_size)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200829 td->o.size = total_size;
830
831 /*
832 * See if we need to extend some files
833 */
834 if (need_extend) {
835 temp_stall_ts = 1;
Jens Axboef3afa572012-09-17 13:34:16 +0200836 if (output_format == FIO_OUTPUT_NORMAL)
Shaozhi Shawn Yea7ba8c52008-09-10 09:04:18 +0200837 log_info("%s: Laying out IO file(s) (%u file(s) /"
Bruce Cran0f2152c2010-12-15 10:33:03 +0100838 " %lluMB)\n", td->o.name, need_extend,
Shaozhi Shawn Yea7ba8c52008-09-10 09:04:18 +0200839 extend_size >> 20);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200840
841 for_each_file(td, f, i) {
Jens Axboe5e0074c2009-06-02 21:40:09 +0200842 unsigned long long old_len = -1ULL, extend_len = -1ULL;
Jens Axboe3baddf22008-04-04 11:10:30 +0200843
Jens Axboed6aed792009-06-03 08:41:15 +0200844 if (!fio_file_extend(f))
Jens Axboe7bb48f82007-03-27 15:30:28 +0200845 continue;
846
Jens Axboe409b3412007-03-28 09:57:01 +0200847 assert(f->filetype == FIO_TYPE_FILE);
Jens Axboed6aed792009-06-03 08:41:15 +0200848 fio_file_clear_extend(f);
Jens Axboe5e0074c2009-06-02 21:40:09 +0200849 if (!td->o.fill_device) {
850 old_len = f->real_file_size;
Jens Axboe0b9d69e2009-09-11 22:29:54 +0200851 extend_len = f->io_size + f->file_offset -
852 old_len;
Jens Axboe5e0074c2009-06-02 21:40:09 +0200853 }
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200854 f->real_file_size = (f->io_size + f->file_offset);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200855 err = extend_file(td, f);
856 if (err)
857 break;
Jens Axboe5e0074c2009-06-02 21:40:09 +0200858
Jens Axboe3baddf22008-04-04 11:10:30 +0200859 err = __file_invalidate_cache(td, f, old_len,
860 extend_len);
861 close(f->fd);
862 f->fd = -1;
863 if (err)
864 break;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200865 }
866 temp_stall_ts = 0;
867 }
868
869 if (err)
870 return err;
871
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100872 if (!td->o.zone_size)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200873 td->o.zone_size = td->o.size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200874
Jens Axboeea966f82007-09-03 10:09:37 +0200875 /*
876 * iolog already set the total io size, if we read back
877 * stored entries.
878 */
879 if (!td->o.read_iolog_file)
880 td->total_io_size = td->o.size * td->o.loops;
Jens Axboe25460cf2012-05-02 13:58:02 +0200881
882done:
883 if (td->o.create_only)
884 td->done = 1;
885
Jens Axboe7bb48f82007-03-27 15:30:28 +0200886 return 0;
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200887err_offset:
888 log_err("%s: you need to specify valid offset=\n", td->o.name);
889 return 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200890}
891
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200892int pre_read_files(struct thread_data *td)
893{
894 struct fio_file *f;
895 unsigned int i;
896
897 dprint(FD_FILE, "pre_read files\n");
898
899 for_each_file(td, f, i) {
900 pre_read_file(td, f);
901 }
902
903 return 1;
904}
905
Jens Axboe9c6f6312012-11-07 09:15:45 +0100906static int __init_rand_distribution(struct thread_data *td, struct fio_file *f)
907{
Jens Axboe23162962012-11-07 19:47:47 +0100908 unsigned int range_size, seed;
Jens Axboe9c6f6312012-11-07 09:15:45 +0100909 unsigned long nranges;
Jens Axboe21415db2013-01-04 13:09:29 +0100910 uint64_t file_size;
Jens Axboe9c6f6312012-11-07 09:15:45 +0100911
912 range_size = min(td->o.min_bs[DDIR_READ], td->o.min_bs[DDIR_WRITE]);
Jens Axboe21415db2013-01-04 13:09:29 +0100913 file_size = min(f->real_file_size, f->io_size);
Jens Axboe9c6f6312012-11-07 09:15:45 +0100914
Jens Axboe21415db2013-01-04 13:09:29 +0100915 nranges = (file_size + range_size - 1) / range_size;
Jens Axboe9c6f6312012-11-07 09:15:45 +0100916
Jens Axboe23162962012-11-07 19:47:47 +0100917 seed = jhash(f->file_name, strlen(f->file_name), 0) * td->thread_number;
Jens Axboe84256872012-11-17 10:06:36 -0700918 if (!td->o.rand_repeatable)
919 seed = td->rand_seeds[4];
920
Jens Axboe9c6f6312012-11-07 09:15:45 +0100921 if (td->o.random_distribution == FIO_RAND_DIST_ZIPF)
Jens Axboe23162962012-11-07 19:47:47 +0100922 zipf_init(&f->zipf, nranges, td->o.zipf_theta, seed);
Jens Axboe9c6f6312012-11-07 09:15:45 +0100923 else
Jens Axboe23162962012-11-07 19:47:47 +0100924 pareto_init(&f->zipf, nranges, td->o.pareto_h, seed);
Jens Axboe9c6f6312012-11-07 09:15:45 +0100925
926 return 1;
927}
928
929static int init_rand_distribution(struct thread_data *td)
930{
931 struct fio_file *f;
932 unsigned int i;
933 int state;
934
935 if (td->o.random_distribution == FIO_RAND_DIST_RANDOM)
936 return 0;
937
938 state = td->runstate;
939 td_set_runstate(td, TD_SETTING_UP);
940 for_each_file(td, f, i)
941 __init_rand_distribution(td, f);
942 td_set_runstate(td, state);
943
944 return 1;
945}
946
Jens Axboe68727072007-03-15 20:49:25 +0100947int init_random_map(struct thread_data *td)
948{
Jens Axboe51ede0b2012-11-22 13:50:29 +0100949 unsigned long long blocks;
Jens Axboe68727072007-03-15 20:49:25 +0100950 struct fio_file *f;
951 unsigned int i;
952
Jens Axboe9c6f6312012-11-07 09:15:45 +0100953 if (init_rand_distribution(td))
954 return 0;
Jens Axboe3831a842012-11-26 19:59:14 +0100955 if (!td_random(td))
Jens Axboe68727072007-03-15 20:49:25 +0100956 return 0;
957
958 for_each_file(td, f, i) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100959 blocks = (f->real_file_size + td->o.rw_min_bs - 1) /
960 (unsigned long long) td->o.rw_min_bs;
Jens Axboe8055e412012-11-26 08:43:47 +0100961 if (td->o.random_generator == FIO_RAND_GEN_LFSR) {
Jens Axboe82af46b2012-12-04 13:12:25 +0100962 unsigned long seed;
963
964 seed = td->rand_seeds[FIO_RAND_BLOCK_OFF];
965
966 if (!lfsr_init(&f->lfsr, blocks, seed))
Jens Axboe8055e412012-11-26 08:43:47 +0100967 continue;
Jens Axboe3831a842012-11-26 19:59:14 +0100968 } else if (!td->o.norandommap) {
Jens Axboe7ebd7962012-11-28 21:24:46 +0100969 f->io_axmap = axmap_new(blocks);
970 if (f->io_axmap)
Jens Axboe8055e412012-11-26 08:43:47 +0100971 continue;
Jens Axboe1cad7122012-11-29 21:37:11 +0100972 } else if (td->o.norandommap)
973 continue;
Jens Axboeceadd592012-02-02 08:41:28 +0100974
Jens Axboe2b386d22008-03-26 10:32:57 +0100975 if (!td->o.softrandommap) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100976 log_err("fio: failed allocating random map. If running"
977 " a large number of jobs, try the 'norandommap'"
Jens Axboe2b386d22008-03-26 10:32:57 +0100978 " option or set 'softrandommap'. Or give"
979 " a larger --alloc-size to fio.\n");
Jens Axboe68727072007-03-15 20:49:25 +0100980 return 1;
981 }
Jens Axboe303032a2008-03-26 10:11:10 +0100982
983 log_info("fio: file %s failed allocating random map. Running "
984 "job without.\n", f->file_name);
Jens Axboe68727072007-03-15 20:49:25 +0100985 }
986
987 return 0;
988}
989
Jens Axboe53cdc682006-10-18 11:50:58 +0200990void close_files(struct thread_data *td)
991{
Jens Axboe0ab8db82006-10-18 17:16:23 +0200992 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100993 unsigned int i;
Jens Axboe53cdc682006-10-18 11:50:58 +0200994
Jens Axboe2be3eec2009-06-10 09:05:13 +0200995 for_each_file(td, f, i) {
996 if (fio_file_open(f))
997 td_io_close_file(td, f);
998 }
Jens Axboe24ffd2c2008-03-01 15:47:08 +0100999}
1000
1001void close_and_free_files(struct thread_data *td)
1002{
1003 struct fio_file *f;
1004 unsigned int i;
1005
Jens Axboeee56ad52008-02-01 10:30:20 +01001006 dprint(FD_FILE, "close files\n");
1007
Jens Axboe0ab8db82006-10-18 17:16:23 +02001008 for_each_file(td, f, i) {
Jens Axboeffdfabd2008-03-26 09:18:14 +01001009 if (td->o.unlink && f->filetype == FIO_TYPE_FILE) {
1010 dprint(FD_FILE, "free unlink %s\n", f->file_name);
Jens Axboe132ad462006-11-23 12:23:12 +01001011 unlink(f->file_name);
Jens Axboeffdfabd2008-03-26 09:18:14 +01001012 }
Jens Axboebdb4e2e2007-03-01 09:54:57 +01001013
Jens Axboe22a57ba2009-06-09 14:34:35 +02001014 if (fio_file_open(f))
1015 td_io_close_file(td, f);
1016
Shaozhi Shawn Yeb9fbcf22008-09-10 09:05:17 +02001017 remove_file_hash(f);
Jens Axboeb3dc7f02007-03-09 12:52:15 +01001018
Jens Axboef17c4392008-03-01 18:40:46 +01001019 sfree(f->file_name);
Jens Axboefa1da862007-03-23 15:20:54 +01001020 f->file_name = NULL;
Jens Axboe7ebd7962012-11-28 21:24:46 +01001021 axmap_free(f->io_axmap);
1022 f->io_axmap = NULL;
Jens Axboe78d99e62008-03-01 18:41:50 +01001023 sfree(f);
Jens Axboe53cdc682006-10-18 11:50:58 +02001024 }
Jens Axboeb4a6a592006-10-20 13:54:47 +02001025
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001026 td->o.filename = NULL;
Jens Axboecade3ef2007-03-23 15:29:45 +01001027 free(td->files);
Jens Axboe9efef3c2008-03-06 10:26:25 +01001028 td->files_index = 0;
Jens Axboeb4a6a592006-10-20 13:54:47 +02001029 td->files = NULL;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001030 td->o.nr_files = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +02001031}
Jens Axboeaf52b342007-03-13 10:07:47 +01001032
Jens Axboee3bab462007-03-13 11:22:09 +01001033static void get_file_type(struct fio_file *f)
Jens Axboeaf52b342007-03-13 10:07:47 +01001034{
1035 struct stat sb;
1036
Jens Axboe66159822007-04-16 21:54:24 +02001037 if (!strcmp(f->file_name, "-"))
1038 f->filetype = FIO_TYPE_PIPE;
1039 else
1040 f->filetype = FIO_TYPE_FILE;
Jens Axboeaf52b342007-03-13 10:07:47 +01001041
Bruce Cran38921822012-02-22 17:55:16 +00001042 /* \\.\ is the device namespace in Windows, where every file is
1043 * a block device */
1044 if (strncmp(f->file_name, "\\\\.\\", 4) == 0)
1045 f->filetype = FIO_TYPE_BD;
1046
Jens Axboeb30d3952010-02-06 23:36:26 +01001047 if (!stat(f->file_name, &sb)) {
Bruce Cran38921822012-02-22 17:55:16 +00001048 if (S_ISBLK(sb.st_mode))
Jens Axboeaf52b342007-03-13 10:07:47 +01001049 f->filetype = FIO_TYPE_BD;
1050 else if (S_ISCHR(sb.st_mode))
1051 f->filetype = FIO_TYPE_CHAR;
Jens Axboeb5605e92007-04-16 20:42:33 +02001052 else if (S_ISFIFO(sb.st_mode))
1053 f->filetype = FIO_TYPE_PIPE;
Jens Axboeaf52b342007-03-13 10:07:47 +01001054 }
1055}
1056
Jens Axboef29b25a2007-07-23 08:56:43 +02001057int add_file(struct thread_data *td, const char *fname)
Jens Axboeaf52b342007-03-13 10:07:47 +01001058{
Jens Axboe7b4e4fe2007-03-13 12:51:40 +01001059 int cur_files = td->files_index;
Jens Axboebd0ee742007-03-23 14:26:23 +01001060 char file_name[PATH_MAX];
Jens Axboeaf52b342007-03-13 10:07:47 +01001061 struct fio_file *f;
Jens Axboebd0ee742007-03-23 14:26:23 +01001062 int len = 0;
Jens Axboeaf52b342007-03-13 10:07:47 +01001063
Jens Axboeee56ad52008-02-01 10:30:20 +01001064 dprint(FD_FILE, "add file %s\n", fname);
1065
Jens Axboef17c4392008-03-01 18:40:46 +01001066 f = smalloc(sizeof(*f));
Jens Axboec48c0be2008-07-31 19:55:02 +02001067 if (!f) {
1068 log_err("fio: smalloc OOM\n");
1069 assert(0);
1070 }
Jens Axboe0b9d69e2009-09-11 22:29:54 +02001071
Jens Axboeaf52b342007-03-13 10:07:47 +01001072 f->fd = -1;
Jens Axboee6c4d732012-12-12 08:16:27 +01001073 f->shadow_fd = -1;
Jens Axboe38dad622010-07-20 14:46:00 -06001074 fio_file_reset(f);
Jens Axboebd0ee742007-03-23 14:26:23 +01001075
Jens Axboefc99bc02009-03-04 15:27:42 +01001076 if (td->files_size <= td->files_index) {
Carl Henrik Lunde4b341fc2009-04-20 08:41:37 +02001077 int new_size = td->o.nr_files + 1;
Jens Axboe126d65c2008-03-01 18:04:31 +01001078
Jens Axboefc99bc02009-03-04 15:27:42 +01001079 dprint(FD_FILE, "resize file array to %d files\n", new_size);
1080
1081 td->files = realloc(td->files, new_size * sizeof(f));
Jens Axboefc99bc02009-03-04 15:27:42 +01001082 td->files_size = new_size;
1083 }
Jens Axboe8bb76792009-03-04 15:37:52 +01001084 td->files[cur_files] = f;
Shaohua Li89ac1d42012-06-11 08:56:32 +02001085 f->fileno = cur_files;
Jens Axboe126d65c2008-03-01 18:04:31 +01001086
Jens Axboe07eb79d2007-04-12 13:02:38 +02001087 /*
1088 * init function, io engine may not be loaded yet
1089 */
1090 if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO))
1091 f->real_file_size = -1ULL;
1092
Jens Axboebd0ee742007-03-23 14:26:23 +01001093 if (td->o.directory)
1094 len = sprintf(file_name, "%s/", td->o.directory);
1095
1096 sprintf(file_name + len, "%s", fname);
Jens Axboef17c4392008-03-01 18:40:46 +01001097 f->file_name = smalloc_strdup(file_name);
Jens Axboec48c0be2008-07-31 19:55:02 +02001098 if (!f->file_name) {
1099 log_err("fio: smalloc OOM\n");
1100 assert(0);
1101 }
Jens Axboe0b9d69e2009-09-11 22:29:54 +02001102
Jens Axboee3bab462007-03-13 11:22:09 +01001103 get_file_type(f);
Jens Axboeaf52b342007-03-13 10:07:47 +01001104
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001105 switch (td->o.file_lock_mode) {
1106 case FILE_LOCK_NONE:
1107 break;
1108 case FILE_LOCK_READWRITE:
1109 f->lock = fio_mutex_rw_init();
1110 break;
1111 case FILE_LOCK_EXCLUSIVE:
Jens Axboe521da522012-08-02 11:21:36 +02001112 f->lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001113 break;
1114 default:
1115 log_err("fio: unknown lock mode: %d\n", td->o.file_lock_mode);
1116 assert(0);
1117 }
Jens Axboe29c13492008-03-01 19:25:20 +01001118
Jens Axboe7b4e4fe2007-03-13 12:51:40 +01001119 td->files_index++;
Jens Axboe15494412007-03-13 11:52:47 +01001120 if (f->filetype == FIO_TYPE_FILE)
1121 td->nr_normal_files++;
Jens Axboef29b25a2007-07-23 08:56:43 +02001122
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001123 dprint(FD_FILE, "file %p \"%s\" added at %d\n", f, f->file_name,
1124 cur_files);
Jens Axboe9efef3c2008-03-06 10:26:25 +01001125
Jens Axboef29b25a2007-07-23 08:56:43 +02001126 return cur_files;
Jens Axboeaf52b342007-03-13 10:07:47 +01001127}
Jens Axboe0ad920e2007-03-13 11:06:45 +01001128
Jens Axboe49ffb4a2010-08-24 15:01:37 +02001129int add_file_exclusive(struct thread_data *td, const char *fname)
1130{
1131 struct fio_file *f;
1132 unsigned int i;
1133
1134 for_each_file(td, f, i) {
1135 if (!strcmp(f->file_name, fname))
1136 return i;
1137 }
1138
1139 return add_file(td, fname);
1140}
1141
Jens Axboe0ad920e2007-03-13 11:06:45 +01001142void get_file(struct fio_file *f)
1143{
Jens Axboe8172fe92008-02-01 21:51:02 +01001144 dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references);
Jens Axboed6aed792009-06-03 08:41:15 +02001145 assert(fio_file_open(f));
Jens Axboe0ad920e2007-03-13 11:06:45 +01001146 f->references++;
1147}
1148
Jens Axboe6977bcd2008-03-01 15:55:36 +01001149int put_file(struct thread_data *td, struct fio_file *f)
Jens Axboe0ad920e2007-03-13 11:06:45 +01001150{
Jens Axboe98e1ac42008-03-06 11:56:48 +01001151 int f_ret = 0, ret = 0;
Jens Axboe6977bcd2008-03-01 15:55:36 +01001152
Jens Axboe8172fe92008-02-01 21:51:02 +01001153 dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references);
Jens Axboeee56ad52008-02-01 10:30:20 +01001154
Jens Axboe22a57ba2009-06-09 14:34:35 +02001155 if (!fio_file_open(f)) {
1156 assert(f->fd == -1);
Jens Axboe6977bcd2008-03-01 15:55:36 +01001157 return 0;
Jens Axboe22a57ba2009-06-09 14:34:35 +02001158 }
Jens Axboe0ad920e2007-03-13 11:06:45 +01001159
1160 assert(f->references);
1161 if (--f->references)
Jens Axboe6977bcd2008-03-01 15:55:36 +01001162 return 0;
Jens Axboe0ad920e2007-03-13 11:06:45 +01001163
Jens Axboed424d4d2007-04-17 09:05:10 +02001164 if (should_fsync(td) && td->o.fsync_on_close)
Jens Axboe98e1ac42008-03-06 11:56:48 +01001165 f_ret = fsync(f->fd);
Jens Axboeebb14152007-03-13 14:42:15 +01001166
Jens Axboe0ad920e2007-03-13 11:06:45 +01001167 if (td->io_ops->close_file)
Jens Axboe6977bcd2008-03-01 15:55:36 +01001168 ret = td->io_ops->close_file(td, f);
Jens Axboe1020a132007-03-29 11:15:06 +02001169
Jens Axboe98e1ac42008-03-06 11:56:48 +01001170 if (!ret)
Jens Axboea5fb4612008-05-28 10:54:01 +02001171 ret = f_ret;
Jens Axboe98e1ac42008-03-06 11:56:48 +01001172
Jens Axboe0ad920e2007-03-13 11:06:45 +01001173 td->nr_open_files--;
Jens Axboed6aed792009-06-03 08:41:15 +02001174 fio_file_clear_open(f);
Jens Axboe22a57ba2009-06-09 14:34:35 +02001175 assert(f->fd == -1);
Jens Axboe6977bcd2008-03-01 15:55:36 +01001176 return ret;
Jens Axboe0ad920e2007-03-13 11:06:45 +01001177}
Jens Axboebbf6b542007-03-13 15:28:55 +01001178
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001179void lock_file(struct thread_data *td, struct fio_file *f, enum fio_ddir ddir)
Jens Axboeb2bd2bd2008-03-01 18:19:52 +01001180{
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001181 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1182 return;
Jens Axboe29c13492008-03-01 19:25:20 +01001183
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001184 if (f->lock_owner == td && f->lock_batch--)
1185 return;
1186
1187 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
1188 if (ddir == DDIR_READ)
1189 fio_mutex_down_read(f->lock);
1190 else
1191 fio_mutex_down_write(f->lock);
1192 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1193 fio_mutex_down(f->lock);
1194
1195 f->lock_owner = td;
1196 f->lock_batch = td->o.lockfile_batch;
1197 f->lock_ddir = ddir;
1198}
1199
1200void unlock_file(struct thread_data *td, struct fio_file *f)
1201{
1202 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1203 return;
1204 if (f->lock_batch)
1205 return;
1206
1207 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
1208 const int is_read = f->lock_ddir == DDIR_READ;
1209 int val = fio_mutex_getval(f->lock);
1210
1211 if ((is_read && val == 1) || (!is_read && val == -1))
1212 f->lock_owner = NULL;
1213
1214 if (is_read)
1215 fio_mutex_up_read(f->lock);
1216 else
1217 fio_mutex_up_write(f->lock);
1218 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE) {
1219 int val = fio_mutex_getval(f->lock);
1220
1221 if (val == 0)
1222 f->lock_owner = NULL;
1223
1224 fio_mutex_up(f->lock);
Jens Axboe29c13492008-03-01 19:25:20 +01001225 }
Jens Axboeb2bd2bd2008-03-01 18:19:52 +01001226}
1227
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001228void unlock_file_all(struct thread_data *td, struct fio_file *f)
Jens Axboeb2bd2bd2008-03-01 18:19:52 +01001229{
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001230 if (f->lock_owner != td)
1231 return;
Jens Axboe29c13492008-03-01 19:25:20 +01001232
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001233 f->lock_batch = 0;
1234 unlock_file(td, f);
Jens Axboeb2bd2bd2008-03-01 18:19:52 +01001235}
1236
Jens Axboebbf6b542007-03-13 15:28:55 +01001237static int recurse_dir(struct thread_data *td, const char *dirname)
1238{
1239 struct dirent *dir;
1240 int ret = 0;
1241 DIR *D;
1242
1243 D = opendir(dirname);
1244 if (!D) {
Jens Axboe0ddb2702007-03-27 19:43:53 +02001245 char buf[FIO_VERROR_SIZE];
1246
1247 snprintf(buf, FIO_VERROR_SIZE - 1, "opendir(%s)", dirname);
1248 td_verror(td, errno, buf);
Jens Axboebbf6b542007-03-13 15:28:55 +01001249 return 1;
1250 }
1251
1252 while ((dir = readdir(D)) != NULL) {
1253 char full_path[PATH_MAX];
1254 struct stat sb;
1255
Jens Axboee85b2b82007-03-14 09:39:06 +01001256 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
1257 continue;
Jens Axboe96d32d52007-03-14 09:16:09 +01001258
Bruce Cranb9fd7882012-02-20 17:01:46 +00001259 sprintf(full_path, "%s%s%s", dirname, FIO_OS_PATH_SEPARATOR, dir->d_name);
Jens Axboebbf6b542007-03-13 15:28:55 +01001260
1261 if (lstat(full_path, &sb) == -1) {
1262 if (errno != ENOENT) {
1263 td_verror(td, errno, "stat");
1264 return 1;
1265 }
1266 }
1267
1268 if (S_ISREG(sb.st_mode)) {
1269 add_file(td, full_path);
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001270 td->o.nr_files++;
Jens Axboebbf6b542007-03-13 15:28:55 +01001271 continue;
1272 }
Jens Axboe0ddb2702007-03-27 19:43:53 +02001273 if (!S_ISDIR(sb.st_mode))
1274 continue;
Jens Axboebbf6b542007-03-13 15:28:55 +01001275
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001276 ret = recurse_dir(td, full_path);
1277 if (ret)
Jens Axboebbf6b542007-03-13 15:28:55 +01001278 break;
1279 }
1280
1281 closedir(D);
1282 return ret;
1283}
1284
1285int add_dir_files(struct thread_data *td, const char *path)
1286{
Jens Axboe0ddb2702007-03-27 19:43:53 +02001287 int ret = recurse_dir(td, path);
1288
1289 if (!ret)
1290 log_info("fio: opendir added %d files\n", td->o.nr_files);
1291
1292 return ret;
Jens Axboebbf6b542007-03-13 15:28:55 +01001293}
Jens Axboecade3ef2007-03-23 15:29:45 +01001294
1295void dup_files(struct thread_data *td, struct thread_data *org)
1296{
1297 struct fio_file *f;
1298 unsigned int i;
Jens Axboe9efef3c2008-03-06 10:26:25 +01001299
1300 dprint(FD_FILE, "dup files: %d\n", org->files_index);
Jens Axboecade3ef2007-03-23 15:29:45 +01001301
1302 if (!org->files)
1303 return;
1304
Jens Axboe9efef3c2008-03-06 10:26:25 +01001305 td->files = malloc(org->files_index * sizeof(f));
Jens Axboecade3ef2007-03-23 15:29:45 +01001306
Jens Axboe9efef3c2008-03-06 10:26:25 +01001307 for_each_file(org, f, i) {
Jens Axboeb0fe4212008-03-01 18:22:27 +01001308 struct fio_file *__f;
1309
Jens Axboef17c4392008-03-01 18:40:46 +01001310 __f = smalloc(sizeof(*__f));
Jens Axboec48c0be2008-07-31 19:55:02 +02001311 if (!__f) {
1312 log_err("fio: smalloc OOM\n");
1313 assert(0);
1314 }
Jens Axboe22a57ba2009-06-09 14:34:35 +02001315 __f->fd = -1;
Jens Axboe38dad622010-07-20 14:46:00 -06001316 fio_file_reset(__f);
Jens Axboe0b9d69e2009-09-11 22:29:54 +02001317
Aaron Carrollbc3456f2008-06-25 09:20:37 +02001318 if (f->file_name) {
Jens Axboef17c4392008-03-01 18:40:46 +01001319 __f->file_name = smalloc_strdup(f->file_name);
Jens Axboec48c0be2008-07-31 19:55:02 +02001320 if (!__f->file_name) {
1321 log_err("fio: smalloc OOM\n");
1322 assert(0);
1323 }
Jens Axboe0b9d69e2009-09-11 22:29:54 +02001324
Aaron Carrollbc3456f2008-06-25 09:20:37 +02001325 __f->filetype = f->filetype;
1326 }
Jens Axboeb0fe4212008-03-01 18:22:27 +01001327
1328 td->files[i] = __f;
Jens Axboecade3ef2007-03-23 15:29:45 +01001329 }
1330}
Jens Axboef29b25a2007-07-23 08:56:43 +02001331
1332/*
1333 * Returns the index that matches the filename, or -1 if not there
1334 */
1335int get_fileno(struct thread_data *td, const char *fname)
1336{
1337 struct fio_file *f;
1338 unsigned int i;
1339
1340 for_each_file(td, f, i)
1341 if (!strcmp(f->file_name, fname))
1342 return i;
1343
1344 return -1;
1345}
1346
1347/*
1348 * For log usage, where we add/open/close files automatically
1349 */
1350void free_release_files(struct thread_data *td)
1351{
1352 close_files(td);
1353 td->files_index = 0;
1354 td->nr_normal_files = 0;
1355}