blob: 5aadf126311b9322668c3e835b673126949fa4d5 [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
Jens Axboe97ac9922013-01-24 15:00:25 -070018#ifdef CONFIG_LINUX_FALLOCATE
Eric Gourioua596f042011-06-17 09:11:45 +020019#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 Axboe97ac9922013-01-24 15:00:25 -070075#ifdef CONFIG_POSIX_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;
Jens Axboe97ac9922013-01-24 15:00:25 -070090#ifdef CONFIG_LINUX_FALLOCATE
Eric Gourioua596f042011-06-17 09:11:45 +020091 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;
Jens Axboe97ac9922013-01-24 15:00:25 -0700103#endif /* CONFIG_LINUX_FALLOCATE */
Eric Gourioua596f042011-06-17 09:11:45 +0200104 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 }
Jens Axboe97ac9922013-01-24 15:00:25 -0700110#endif /* CONFIG_POSIX_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
Ken Raeburn98ffb8f2013-01-30 22:31:09 +0100566 snprintf(buf, sizeof(buf), "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 Axboe38f30c82013-01-11 14:03:05 +0100959 uint64_t file_size = min(f->real_file_size, f->io_size);
960
961 blocks = (file_size + td->o.rw_min_bs - 1) /
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100962 (unsigned long long) td->o.rw_min_bs;
Jens Axboe8055e412012-11-26 08:43:47 +0100963 if (td->o.random_generator == FIO_RAND_GEN_LFSR) {
Jens Axboe82af46b2012-12-04 13:12:25 +0100964 unsigned long seed;
965
966 seed = td->rand_seeds[FIO_RAND_BLOCK_OFF];
967
968 if (!lfsr_init(&f->lfsr, blocks, seed))
Jens Axboe8055e412012-11-26 08:43:47 +0100969 continue;
Jens Axboe3831a842012-11-26 19:59:14 +0100970 } else if (!td->o.norandommap) {
Jens Axboe7ebd7962012-11-28 21:24:46 +0100971 f->io_axmap = axmap_new(blocks);
972 if (f->io_axmap)
Jens Axboe8055e412012-11-26 08:43:47 +0100973 continue;
Jens Axboe1cad7122012-11-29 21:37:11 +0100974 } else if (td->o.norandommap)
975 continue;
Jens Axboeceadd592012-02-02 08:41:28 +0100976
Jens Axboe2b386d22008-03-26 10:32:57 +0100977 if (!td->o.softrandommap) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100978 log_err("fio: failed allocating random map. If running"
979 " a large number of jobs, try the 'norandommap'"
Jens Axboe2b386d22008-03-26 10:32:57 +0100980 " option or set 'softrandommap'. Or give"
981 " a larger --alloc-size to fio.\n");
Jens Axboe68727072007-03-15 20:49:25 +0100982 return 1;
983 }
Jens Axboe303032a2008-03-26 10:11:10 +0100984
985 log_info("fio: file %s failed allocating random map. Running "
986 "job without.\n", f->file_name);
Jens Axboe68727072007-03-15 20:49:25 +0100987 }
988
989 return 0;
990}
991
Jens Axboe53cdc682006-10-18 11:50:58 +0200992void close_files(struct thread_data *td)
993{
Jens Axboe0ab8db82006-10-18 17:16:23 +0200994 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100995 unsigned int i;
Jens Axboe53cdc682006-10-18 11:50:58 +0200996
Jens Axboe2be3eec2009-06-10 09:05:13 +0200997 for_each_file(td, f, i) {
998 if (fio_file_open(f))
999 td_io_close_file(td, f);
1000 }
Jens Axboe24ffd2c2008-03-01 15:47:08 +01001001}
1002
1003void close_and_free_files(struct thread_data *td)
1004{
1005 struct fio_file *f;
1006 unsigned int i;
1007
Jens Axboeee56ad52008-02-01 10:30:20 +01001008 dprint(FD_FILE, "close files\n");
1009
Jens Axboe0ab8db82006-10-18 17:16:23 +02001010 for_each_file(td, f, i) {
Jens Axboeffdfabd2008-03-26 09:18:14 +01001011 if (td->o.unlink && f->filetype == FIO_TYPE_FILE) {
1012 dprint(FD_FILE, "free unlink %s\n", f->file_name);
Jens Axboe132ad462006-11-23 12:23:12 +01001013 unlink(f->file_name);
Jens Axboeffdfabd2008-03-26 09:18:14 +01001014 }
Jens Axboebdb4e2e2007-03-01 09:54:57 +01001015
Jens Axboe22a57ba2009-06-09 14:34:35 +02001016 if (fio_file_open(f))
1017 td_io_close_file(td, f);
1018
Shaozhi Shawn Yeb9fbcf22008-09-10 09:05:17 +02001019 remove_file_hash(f);
Jens Axboeb3dc7f02007-03-09 12:52:15 +01001020
Jens Axboef17c4392008-03-01 18:40:46 +01001021 sfree(f->file_name);
Jens Axboefa1da862007-03-23 15:20:54 +01001022 f->file_name = NULL;
Jens Axboe7ebd7962012-11-28 21:24:46 +01001023 axmap_free(f->io_axmap);
1024 f->io_axmap = NULL;
Jens Axboe78d99e62008-03-01 18:41:50 +01001025 sfree(f);
Jens Axboe53cdc682006-10-18 11:50:58 +02001026 }
Jens Axboeb4a6a592006-10-20 13:54:47 +02001027
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001028 td->o.filename = NULL;
Jens Axboecade3ef2007-03-23 15:29:45 +01001029 free(td->files);
Jens Axboe9efef3c2008-03-06 10:26:25 +01001030 td->files_index = 0;
Jens Axboeb4a6a592006-10-20 13:54:47 +02001031 td->files = NULL;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001032 td->o.nr_files = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +02001033}
Jens Axboeaf52b342007-03-13 10:07:47 +01001034
Jens Axboee3bab462007-03-13 11:22:09 +01001035static void get_file_type(struct fio_file *f)
Jens Axboeaf52b342007-03-13 10:07:47 +01001036{
1037 struct stat sb;
1038
Jens Axboe66159822007-04-16 21:54:24 +02001039 if (!strcmp(f->file_name, "-"))
1040 f->filetype = FIO_TYPE_PIPE;
1041 else
1042 f->filetype = FIO_TYPE_FILE;
Jens Axboeaf52b342007-03-13 10:07:47 +01001043
Bruce Cran38921822012-02-22 17:55:16 +00001044 /* \\.\ is the device namespace in Windows, where every file is
1045 * a block device */
1046 if (strncmp(f->file_name, "\\\\.\\", 4) == 0)
1047 f->filetype = FIO_TYPE_BD;
1048
Jens Axboeb30d3952010-02-06 23:36:26 +01001049 if (!stat(f->file_name, &sb)) {
Bruce Cran38921822012-02-22 17:55:16 +00001050 if (S_ISBLK(sb.st_mode))
Jens Axboeaf52b342007-03-13 10:07:47 +01001051 f->filetype = FIO_TYPE_BD;
1052 else if (S_ISCHR(sb.st_mode))
1053 f->filetype = FIO_TYPE_CHAR;
Jens Axboeb5605e92007-04-16 20:42:33 +02001054 else if (S_ISFIFO(sb.st_mode))
1055 f->filetype = FIO_TYPE_PIPE;
Jens Axboeaf52b342007-03-13 10:07:47 +01001056 }
1057}
1058
Jens Axboef29b25a2007-07-23 08:56:43 +02001059int add_file(struct thread_data *td, const char *fname)
Jens Axboeaf52b342007-03-13 10:07:47 +01001060{
Jens Axboe7b4e4fe2007-03-13 12:51:40 +01001061 int cur_files = td->files_index;
Jens Axboebd0ee742007-03-23 14:26:23 +01001062 char file_name[PATH_MAX];
Jens Axboeaf52b342007-03-13 10:07:47 +01001063 struct fio_file *f;
Jens Axboebd0ee742007-03-23 14:26:23 +01001064 int len = 0;
Jens Axboeaf52b342007-03-13 10:07:47 +01001065
Jens Axboeee56ad52008-02-01 10:30:20 +01001066 dprint(FD_FILE, "add file %s\n", fname);
1067
Jens Axboef17c4392008-03-01 18:40:46 +01001068 f = smalloc(sizeof(*f));
Jens Axboec48c0be2008-07-31 19:55:02 +02001069 if (!f) {
1070 log_err("fio: smalloc OOM\n");
1071 assert(0);
1072 }
Jens Axboe0b9d69e2009-09-11 22:29:54 +02001073
Jens Axboeaf52b342007-03-13 10:07:47 +01001074 f->fd = -1;
Jens Axboee6c4d732012-12-12 08:16:27 +01001075 f->shadow_fd = -1;
Jens Axboe33c48812013-01-21 09:46:06 -07001076 fio_file_reset(td, f);
Jens Axboebd0ee742007-03-23 14:26:23 +01001077
Jens Axboefc99bc02009-03-04 15:27:42 +01001078 if (td->files_size <= td->files_index) {
Jianpeng Ma1983e322013-01-10 13:19:27 +01001079 unsigned int new_size = td->o.nr_files + 1;
Jens Axboe126d65c2008-03-01 18:04:31 +01001080
Jens Axboefc99bc02009-03-04 15:27:42 +01001081 dprint(FD_FILE, "resize file array to %d files\n", new_size);
1082
1083 td->files = realloc(td->files, new_size * sizeof(f));
Jianpeng Mad537c082013-01-11 08:52:55 +01001084 if (td->files == NULL) {
1085 log_err("fio: realloc OOM\n");
1086 assert(0);
1087 }
Jens Axboefc99bc02009-03-04 15:27:42 +01001088 td->files_size = new_size;
1089 }
Jens Axboe8bb76792009-03-04 15:37:52 +01001090 td->files[cur_files] = f;
Shaohua Li89ac1d42012-06-11 08:56:32 +02001091 f->fileno = cur_files;
Jens Axboe126d65c2008-03-01 18:04:31 +01001092
Jens Axboe07eb79d2007-04-12 13:02:38 +02001093 /*
1094 * init function, io engine may not be loaded yet
1095 */
1096 if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO))
1097 f->real_file_size = -1ULL;
1098
Jens Axboebd0ee742007-03-23 14:26:23 +01001099 if (td->o.directory)
1100 len = sprintf(file_name, "%s/", td->o.directory);
1101
1102 sprintf(file_name + len, "%s", fname);
Jens Axboef17c4392008-03-01 18:40:46 +01001103 f->file_name = smalloc_strdup(file_name);
Jens Axboec48c0be2008-07-31 19:55:02 +02001104 if (!f->file_name) {
1105 log_err("fio: smalloc OOM\n");
1106 assert(0);
1107 }
Jens Axboe0b9d69e2009-09-11 22:29:54 +02001108
Jens Axboee3bab462007-03-13 11:22:09 +01001109 get_file_type(f);
Jens Axboeaf52b342007-03-13 10:07:47 +01001110
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001111 switch (td->o.file_lock_mode) {
1112 case FILE_LOCK_NONE:
1113 break;
1114 case FILE_LOCK_READWRITE:
1115 f->lock = fio_mutex_rw_init();
1116 break;
1117 case FILE_LOCK_EXCLUSIVE:
Jens Axboe521da522012-08-02 11:21:36 +02001118 f->lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001119 break;
1120 default:
1121 log_err("fio: unknown lock mode: %d\n", td->o.file_lock_mode);
1122 assert(0);
1123 }
Jens Axboe29c13492008-03-01 19:25:20 +01001124
Jens Axboe7b4e4fe2007-03-13 12:51:40 +01001125 td->files_index++;
Jens Axboe15494412007-03-13 11:52:47 +01001126 if (f->filetype == FIO_TYPE_FILE)
1127 td->nr_normal_files++;
Jens Axboef29b25a2007-07-23 08:56:43 +02001128
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001129 dprint(FD_FILE, "file %p \"%s\" added at %d\n", f, f->file_name,
1130 cur_files);
Jens Axboe9efef3c2008-03-06 10:26:25 +01001131
Jens Axboef29b25a2007-07-23 08:56:43 +02001132 return cur_files;
Jens Axboeaf52b342007-03-13 10:07:47 +01001133}
Jens Axboe0ad920e2007-03-13 11:06:45 +01001134
Jens Axboe49ffb4a2010-08-24 15:01:37 +02001135int add_file_exclusive(struct thread_data *td, const char *fname)
1136{
1137 struct fio_file *f;
1138 unsigned int i;
1139
1140 for_each_file(td, f, i) {
1141 if (!strcmp(f->file_name, fname))
1142 return i;
1143 }
1144
1145 return add_file(td, fname);
1146}
1147
Jens Axboe0ad920e2007-03-13 11:06:45 +01001148void get_file(struct fio_file *f)
1149{
Jens Axboe8172fe92008-02-01 21:51:02 +01001150 dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references);
Jens Axboed6aed792009-06-03 08:41:15 +02001151 assert(fio_file_open(f));
Jens Axboe0ad920e2007-03-13 11:06:45 +01001152 f->references++;
1153}
1154
Jens Axboe6977bcd2008-03-01 15:55:36 +01001155int put_file(struct thread_data *td, struct fio_file *f)
Jens Axboe0ad920e2007-03-13 11:06:45 +01001156{
Jens Axboe98e1ac42008-03-06 11:56:48 +01001157 int f_ret = 0, ret = 0;
Jens Axboe6977bcd2008-03-01 15:55:36 +01001158
Jens Axboe8172fe92008-02-01 21:51:02 +01001159 dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references);
Jens Axboeee56ad52008-02-01 10:30:20 +01001160
Jens Axboe22a57ba2009-06-09 14:34:35 +02001161 if (!fio_file_open(f)) {
1162 assert(f->fd == -1);
Jens Axboe6977bcd2008-03-01 15:55:36 +01001163 return 0;
Jens Axboe22a57ba2009-06-09 14:34:35 +02001164 }
Jens Axboe0ad920e2007-03-13 11:06:45 +01001165
1166 assert(f->references);
1167 if (--f->references)
Jens Axboe6977bcd2008-03-01 15:55:36 +01001168 return 0;
Jens Axboe0ad920e2007-03-13 11:06:45 +01001169
Jens Axboed424d4d2007-04-17 09:05:10 +02001170 if (should_fsync(td) && td->o.fsync_on_close)
Jens Axboe98e1ac42008-03-06 11:56:48 +01001171 f_ret = fsync(f->fd);
Jens Axboeebb14152007-03-13 14:42:15 +01001172
Jens Axboe0ad920e2007-03-13 11:06:45 +01001173 if (td->io_ops->close_file)
Jens Axboe6977bcd2008-03-01 15:55:36 +01001174 ret = td->io_ops->close_file(td, f);
Jens Axboe1020a132007-03-29 11:15:06 +02001175
Jens Axboe98e1ac42008-03-06 11:56:48 +01001176 if (!ret)
Jens Axboea5fb4612008-05-28 10:54:01 +02001177 ret = f_ret;
Jens Axboe98e1ac42008-03-06 11:56:48 +01001178
Jens Axboe0ad920e2007-03-13 11:06:45 +01001179 td->nr_open_files--;
Jens Axboed6aed792009-06-03 08:41:15 +02001180 fio_file_clear_open(f);
Jens Axboe22a57ba2009-06-09 14:34:35 +02001181 assert(f->fd == -1);
Jens Axboe6977bcd2008-03-01 15:55:36 +01001182 return ret;
Jens Axboe0ad920e2007-03-13 11:06:45 +01001183}
Jens Axboebbf6b542007-03-13 15:28:55 +01001184
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001185void lock_file(struct thread_data *td, struct fio_file *f, enum fio_ddir ddir)
Jens Axboeb2bd2bd2008-03-01 18:19:52 +01001186{
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001187 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1188 return;
Jens Axboe29c13492008-03-01 19:25:20 +01001189
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001190 if (f->lock_owner == td && f->lock_batch--)
1191 return;
1192
1193 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
1194 if (ddir == DDIR_READ)
1195 fio_mutex_down_read(f->lock);
1196 else
1197 fio_mutex_down_write(f->lock);
1198 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1199 fio_mutex_down(f->lock);
1200
1201 f->lock_owner = td;
1202 f->lock_batch = td->o.lockfile_batch;
1203 f->lock_ddir = ddir;
1204}
1205
1206void unlock_file(struct thread_data *td, struct fio_file *f)
1207{
1208 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1209 return;
1210 if (f->lock_batch)
1211 return;
1212
1213 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
1214 const int is_read = f->lock_ddir == DDIR_READ;
1215 int val = fio_mutex_getval(f->lock);
1216
1217 if ((is_read && val == 1) || (!is_read && val == -1))
1218 f->lock_owner = NULL;
1219
1220 if (is_read)
1221 fio_mutex_up_read(f->lock);
1222 else
1223 fio_mutex_up_write(f->lock);
1224 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE) {
1225 int val = fio_mutex_getval(f->lock);
1226
1227 if (val == 0)
1228 f->lock_owner = NULL;
1229
1230 fio_mutex_up(f->lock);
Jens Axboe29c13492008-03-01 19:25:20 +01001231 }
Jens Axboeb2bd2bd2008-03-01 18:19:52 +01001232}
1233
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001234void unlock_file_all(struct thread_data *td, struct fio_file *f)
Jens Axboeb2bd2bd2008-03-01 18:19:52 +01001235{
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001236 if (f->lock_owner != td)
1237 return;
Jens Axboe29c13492008-03-01 19:25:20 +01001238
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001239 f->lock_batch = 0;
1240 unlock_file(td, f);
Jens Axboeb2bd2bd2008-03-01 18:19:52 +01001241}
1242
Jens Axboebbf6b542007-03-13 15:28:55 +01001243static int recurse_dir(struct thread_data *td, const char *dirname)
1244{
1245 struct dirent *dir;
1246 int ret = 0;
1247 DIR *D;
1248
1249 D = opendir(dirname);
1250 if (!D) {
Jens Axboe0ddb2702007-03-27 19:43:53 +02001251 char buf[FIO_VERROR_SIZE];
1252
Ken Raeburn98ffb8f2013-01-30 22:31:09 +01001253 snprintf(buf, FIO_VERROR_SIZE, "opendir(%s)", dirname);
Jens Axboe0ddb2702007-03-27 19:43:53 +02001254 td_verror(td, errno, buf);
Jens Axboebbf6b542007-03-13 15:28:55 +01001255 return 1;
1256 }
1257
1258 while ((dir = readdir(D)) != NULL) {
1259 char full_path[PATH_MAX];
1260 struct stat sb;
1261
Jens Axboee85b2b82007-03-14 09:39:06 +01001262 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
1263 continue;
Jens Axboe96d32d52007-03-14 09:16:09 +01001264
Bruce Cranb9fd7882012-02-20 17:01:46 +00001265 sprintf(full_path, "%s%s%s", dirname, FIO_OS_PATH_SEPARATOR, dir->d_name);
Jens Axboebbf6b542007-03-13 15:28:55 +01001266
1267 if (lstat(full_path, &sb) == -1) {
1268 if (errno != ENOENT) {
1269 td_verror(td, errno, "stat");
1270 return 1;
1271 }
1272 }
1273
1274 if (S_ISREG(sb.st_mode)) {
1275 add_file(td, full_path);
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001276 td->o.nr_files++;
Jens Axboebbf6b542007-03-13 15:28:55 +01001277 continue;
1278 }
Jens Axboe0ddb2702007-03-27 19:43:53 +02001279 if (!S_ISDIR(sb.st_mode))
1280 continue;
Jens Axboebbf6b542007-03-13 15:28:55 +01001281
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001282 ret = recurse_dir(td, full_path);
1283 if (ret)
Jens Axboebbf6b542007-03-13 15:28:55 +01001284 break;
1285 }
1286
1287 closedir(D);
1288 return ret;
1289}
1290
1291int add_dir_files(struct thread_data *td, const char *path)
1292{
Jens Axboe0ddb2702007-03-27 19:43:53 +02001293 int ret = recurse_dir(td, path);
1294
1295 if (!ret)
1296 log_info("fio: opendir added %d files\n", td->o.nr_files);
1297
1298 return ret;
Jens Axboebbf6b542007-03-13 15:28:55 +01001299}
Jens Axboecade3ef2007-03-23 15:29:45 +01001300
1301void dup_files(struct thread_data *td, struct thread_data *org)
1302{
1303 struct fio_file *f;
1304 unsigned int i;
Jens Axboe9efef3c2008-03-06 10:26:25 +01001305
1306 dprint(FD_FILE, "dup files: %d\n", org->files_index);
Jens Axboecade3ef2007-03-23 15:29:45 +01001307
1308 if (!org->files)
1309 return;
1310
Jens Axboe9efef3c2008-03-06 10:26:25 +01001311 td->files = malloc(org->files_index * sizeof(f));
Jens Axboecade3ef2007-03-23 15:29:45 +01001312
Jens Axboe9efef3c2008-03-06 10:26:25 +01001313 for_each_file(org, f, i) {
Jens Axboeb0fe4212008-03-01 18:22:27 +01001314 struct fio_file *__f;
1315
Jens Axboef17c4392008-03-01 18:40:46 +01001316 __f = smalloc(sizeof(*__f));
Jens Axboec48c0be2008-07-31 19:55:02 +02001317 if (!__f) {
1318 log_err("fio: smalloc OOM\n");
1319 assert(0);
1320 }
Jens Axboe22a57ba2009-06-09 14:34:35 +02001321 __f->fd = -1;
Jens Axboe33c48812013-01-21 09:46:06 -07001322 fio_file_reset(td, __f);
Jens Axboe0b9d69e2009-09-11 22:29:54 +02001323
Aaron Carrollbc3456f2008-06-25 09:20:37 +02001324 if (f->file_name) {
Jens Axboef17c4392008-03-01 18:40:46 +01001325 __f->file_name = smalloc_strdup(f->file_name);
Jens Axboec48c0be2008-07-31 19:55:02 +02001326 if (!__f->file_name) {
1327 log_err("fio: smalloc OOM\n");
1328 assert(0);
1329 }
Jens Axboe0b9d69e2009-09-11 22:29:54 +02001330
Aaron Carrollbc3456f2008-06-25 09:20:37 +02001331 __f->filetype = f->filetype;
1332 }
Jens Axboeb0fe4212008-03-01 18:22:27 +01001333
1334 td->files[i] = __f;
Jens Axboecade3ef2007-03-23 15:29:45 +01001335 }
1336}
Jens Axboef29b25a2007-07-23 08:56:43 +02001337
1338/*
1339 * Returns the index that matches the filename, or -1 if not there
1340 */
1341int get_fileno(struct thread_data *td, const char *fname)
1342{
1343 struct fio_file *f;
1344 unsigned int i;
1345
1346 for_each_file(td, f, i)
1347 if (!strcmp(f->file_name, fname))
1348 return i;
1349
1350 return -1;
1351}
1352
1353/*
1354 * For log usage, where we add/open/close files automatically
1355 */
1356void free_release_files(struct thread_data *td)
1357{
1358 close_files(td);
1359 td->files_index = 0;
1360 td->nr_normal_files = 0;
1361}
Jens Axboe33c48812013-01-21 09:46:06 -07001362
1363void fio_file_reset(struct thread_data *td, struct fio_file *f)
1364{
1365 f->last_pos = f->file_offset;
1366 f->last_start = -1ULL;
1367 if (f->io_axmap)
1368 axmap_reset(f->io_axmap);
1369 if (td->o.random_generator == FIO_RAND_GEN_LFSR)
1370 lfsr_reset(&f->lfsr, td->rand_seeds[FIO_RAND_BLOCK_OFF]);
1371}