blob: 9f186fb46436a4d46998709a787050be24905316 [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",
Jens Axboe4b91ee82013-02-25 10:18:33 +010082 f->file_name,
83 (unsigned long long) f->real_file_size);
Jens Axboe7bc8c2c2010-01-28 11:31:31 +010084
Eric Gourioua596f042011-06-17 09:11:45 +020085 r = posix_fallocate(f->fd, 0, f->real_file_size);
86 if (r > 0) {
87 log_err("fio: posix_fallocate fails: %s\n",
88 strerror(r));
89 }
90 break;
Jens Axboe97ac9922013-01-24 15:00:25 -070091#ifdef CONFIG_LINUX_FALLOCATE
Eric Gourioua596f042011-06-17 09:11:45 +020092 case FIO_FALLOCATE_KEEP_SIZE:
93 dprint(FD_FILE,
94 "fallocate(FALLOC_FL_KEEP_SIZE) "
Jens Axboe4b91ee82013-02-25 10:18:33 +010095 "file %s size %llu\n", f->file_name,
96 (unsigned long long) f->real_file_size);
Eric Gourioua596f042011-06-17 09:11:45 +020097
98 r = fallocate(f->fd, FALLOC_FL_KEEP_SIZE, 0,
99 f->real_file_size);
Jens Axboe888677a2013-04-10 22:19:46 +0200100 if (r != 0)
Eric Gourioua596f042011-06-17 09:11:45 +0200101 td_verror(td, errno, "fallocate");
Jens Axboe888677a2013-04-10 22:19:46 +0200102
Eric Gourioua596f042011-06-17 09:11:45 +0200103 break;
Jens Axboe97ac9922013-01-24 15:00:25 -0700104#endif /* CONFIG_LINUX_FALLOCATE */
Eric Gourioua596f042011-06-17 09:11:45 +0200105 default:
106 log_err("fio: unknown fallocate mode: %d\n",
107 td->o.fallocate_mode);
108 assert(0);
Jens Axboe7bc8c2c2010-01-28 11:31:31 +0100109 }
110 }
Jens Axboe97ac9922013-01-24 15:00:25 -0700111#endif /* CONFIG_POSIX_FALLOCATE */
Bruce Cran9b836562011-01-08 19:49:54 +0100112
Shawn Lewisfc74ac12008-01-11 09:45:11 +0100113 if (!new_layout)
114 goto done;
115
Jens Axboe5e0074c2009-06-02 21:40:09 +0200116 /*
117 * The size will be -1ULL when fill_device is used, so don't truncate
118 * or fallocate this file, just write it
119 */
120 if (!td->o.fill_device) {
121 dprint(FD_FILE, "truncate file %s, size %llu\n", f->file_name,
Jens Axboe4b91ee82013-02-25 10:18:33 +0100122 (unsigned long long) f->real_file_size);
Jens Axboe5e0074c2009-06-02 21:40:09 +0200123 if (ftruncate(f->fd, f->real_file_size) == -1) {
124 td_verror(td, errno, "ftruncate");
125 goto err;
126 }
Jens Axboe5e0074c2009-06-02 21:40:09 +0200127 }
Jens Axboe40f82982006-10-25 11:21:05 +0200128
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100129 b = malloc(td->o.max_bs[DDIR_WRITE]);
130 memset(b, 0, td->o.max_bs[DDIR_WRITE]);
Jens Axboe53cdc682006-10-18 11:50:58 +0200131
Jens Axboe7bb48f82007-03-27 15:30:28 +0200132 left = f->real_file_size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200133 while (left && !td->terminate) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100134 bs = td->o.max_bs[DDIR_WRITE];
Jens Axboe53cdc682006-10-18 11:50:58 +0200135 if (bs > left)
136 bs = left;
137
138 r = write(f->fd, b, bs);
139
Jens Axboe5e0074c2009-06-02 21:40:09 +0200140 if (r > 0) {
141 left -= r;
Jens Axboe53cdc682006-10-18 11:50:58 +0200142 continue;
143 } else {
Jens Axboe5e0074c2009-06-02 21:40:09 +0200144 if (r < 0) {
145 int __e = errno;
146
147 if (__e == ENOSPC) {
148 if (td->o.fill_device)
149 break;
150 log_info("fio: ENOSPC on laying out "
151 "file, stopping\n");
152 break;
153 }
Jens Axboee1161c32007-02-22 19:36:48 +0100154 td_verror(td, errno, "write");
Jens Axboe5e0074c2009-06-02 21:40:09 +0200155 } else
Jens Axboee1161c32007-02-22 19:36:48 +0100156 td_verror(td, EIO, "write");
Jens Axboe53cdc682006-10-18 11:50:58 +0200157
158 break;
159 }
160 }
161
Jens Axboeffdfabd2008-03-26 09:18:14 +0100162 if (td->terminate) {
163 dprint(FD_FILE, "terminate unlink %s\n", f->file_name);
Jens Axboe53cdc682006-10-18 11:50:58 +0200164 unlink(f->file_name);
Jens Axboeffdfabd2008-03-26 09:18:14 +0100165 } else if (td->o.create_fsync) {
Jens Axboe98e1ac42008-03-06 11:56:48 +0100166 if (fsync(f->fd) < 0) {
167 td_verror(td, errno, "fsync");
168 goto err;
169 }
170 }
Jens Axboe0d1cd202009-06-05 22:11:52 +0200171 if (td->o.fill_device && !td_write(td)) {
Jens Axboed6aed792009-06-03 08:41:15 +0200172 fio_file_clear_size_known(f);
Jens Axboe5e0074c2009-06-02 21:40:09 +0200173 if (td_io_get_file_size(td, f))
174 goto err;
175 if (f->io_size > f->real_file_size)
176 f->io_size = f->real_file_size;
177 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200178
179 free(b);
Jens Axboe507a7022007-03-27 15:52:46 +0200180done:
Jens Axboe53cdc682006-10-18 11:50:58 +0200181 return 0;
182err:
183 close(f->fd);
184 f->fd = -1;
185 return 1;
186}
187
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200188static int pre_read_file(struct thread_data *td, struct fio_file *f)
189{
Jens Axboeb0f65862009-05-20 11:52:15 +0200190 int r, did_open = 0, old_runstate;
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200191 unsigned long long left;
192 unsigned int bs;
193 char *b;
194
Jens Axboe9c0d2242009-07-01 12:26:28 +0200195 if (td->io_ops->flags & FIO_PIPEIO)
196 return 0;
197
Jens Axboed6aed792009-06-03 08:41:15 +0200198 if (!fio_file_open(f)) {
Jens Axboeb0f65862009-05-20 11:52:15 +0200199 if (td->io_ops->open_file(td, f)) {
200 log_err("fio: cannot pre-read, failed to open file\n");
201 return 1;
202 }
203 did_open = 1;
204 }
205
206 old_runstate = td->runstate;
207 td_set_runstate(td, TD_PRE_READING);
208
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200209 bs = td->o.max_bs[DDIR_READ];
210 b = malloc(bs);
211 memset(b, 0, bs);
212
213 lseek(f->fd, f->file_offset, SEEK_SET);
214 left = f->io_size;
215
216 while (left && !td->terminate) {
217 if (bs > left)
218 bs = left;
219
220 r = read(f->fd, b, bs);
221
222 if (r == (int) bs) {
223 left -= bs;
224 continue;
225 } else {
226 td_verror(td, EIO, "pre_read");
227 break;
228 }
229 }
230
Jens Axboeb0f65862009-05-20 11:52:15 +0200231 td_set_runstate(td, old_runstate);
232
233 if (did_open)
234 td->io_ops->close_file(td, f);
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200235 free(b);
236 return 0;
237}
238
Jens Axboe7bb48f82007-03-27 15:30:28 +0200239static unsigned long long get_rand_file_size(struct thread_data *td)
Jens Axboe9c60ce62007-03-15 09:14:47 +0100240{
Jens Axboedc873b62008-06-04 20:13:04 +0200241 unsigned long long ret, sized;
Jens Axboe1294c3e2011-05-11 08:15:18 +0200242 unsigned long r;
Jens Axboe9c60ce62007-03-15 09:14:47 +0100243
Jens Axboe4c07ad82011-03-28 09:51:09 +0200244 if (td->o.use_os_rand) {
245 r = os_random_long(&td->file_size_state);
246 sized = td->o.file_size_high - td->o.file_size_low;
247 ret = (unsigned long long) ((double) sized * (r / (OS_RAND_MAX + 1.0)));
248 } else {
249 r = __rand(&td->__file_size_state);
250 sized = td->o.file_size_high - td->o.file_size_low;
251 ret = (unsigned long long) ((double) sized * (r / (FRAND_MAX + 1.0)));
252 }
253
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100254 ret += td->o.file_size_low;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100255 ret -= (ret % td->o.rw_min_bs);
Jens Axboe9c60ce62007-03-15 09:14:47 +0100256 return ret;
257}
258
Jens Axboe53cdc682006-10-18 11:50:58 +0200259static int file_size(struct thread_data *td, struct fio_file *f)
260{
261 struct stat st;
262
Jens Axboedf9c26b2009-03-05 10:13:58 +0100263 if (stat(f->file_name, &st) == -1) {
Jens Axboe7bb48f82007-03-27 15:30:28 +0200264 td_verror(td, errno, "fstat");
265 return 1;
266 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200267
Jens Axboe7bb48f82007-03-27 15:30:28 +0200268 f->real_file_size = st.st_size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200269 return 0;
270}
271
272static int bdev_size(struct thread_data *td, struct fio_file *f)
273{
Bruce Cran9b836562011-01-08 19:49:54 +0100274 unsigned long long bytes = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200275 int r;
276
Jens Axboedf9c26b2009-03-05 10:13:58 +0100277 if (td->io_ops->open_file(td, f)) {
278 log_err("fio: failed opening blockdev %s for size check\n",
279 f->file_name);
280 return 1;
281 }
282
Bruce Cranecc314b2011-01-04 10:59:30 +0100283 r = blockdev_size(f, &bytes);
Jens Axboe53cdc682006-10-18 11:50:58 +0200284 if (r) {
Jens Axboee1161c32007-02-22 19:36:48 +0100285 td_verror(td, r, "blockdev_size");
Jens Axboedf9c26b2009-03-05 10:13:58 +0100286 goto err;
Jens Axboe53cdc682006-10-18 11:50:58 +0200287 }
288
Jens Axboe7ab077a2009-02-02 15:07:37 +0100289 if (!bytes) {
290 log_err("%s: zero sized block device?\n", f->file_name);
Jens Axboedf9c26b2009-03-05 10:13:58 +0100291 goto err;
Jens Axboe7ab077a2009-02-02 15:07:37 +0100292 }
293
Jens Axboe53cdc682006-10-18 11:50:58 +0200294 f->real_file_size = bytes;
Jens Axboe22a57ba2009-06-09 14:34:35 +0200295 td->io_ops->close_file(td, f);
Jens Axboe53cdc682006-10-18 11:50:58 +0200296 return 0;
Jens Axboedf9c26b2009-03-05 10:13:58 +0100297err:
298 td->io_ops->close_file(td, f);
299 return 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200300}
301
Jens Axboe4ccdccd2010-06-24 10:27:22 +0200302static int char_size(struct thread_data *td, struct fio_file *f)
303{
304#ifdef FIO_HAVE_CHARDEV_SIZE
Bruce Cran9b836562011-01-08 19:49:54 +0100305 unsigned long long bytes = 0;
Jens Axboe4ccdccd2010-06-24 10:27:22 +0200306 int r;
307
308 if (td->io_ops->open_file(td, f)) {
309 log_err("fio: failed opening blockdev %s for size check\n",
310 f->file_name);
311 return 1;
312 }
313
Bruce Cranecc314b2011-01-04 10:59:30 +0100314 r = chardev_size(f, &bytes);
Jens Axboe4ccdccd2010-06-24 10:27:22 +0200315 if (r) {
316 td_verror(td, r, "chardev_size");
317 goto err;
318 }
319
320 if (!bytes) {
321 log_err("%s: zero sized char device?\n", f->file_name);
322 goto err;
323 }
324
325 f->real_file_size = bytes;
326 td->io_ops->close_file(td, f);
327 return 0;
328err:
329 td->io_ops->close_file(td, f);
330 return 1;
331#else
332 f->real_file_size = -1ULL;
333 return 0;
334#endif
335}
336
Jens Axboe53cdc682006-10-18 11:50:58 +0200337static int get_file_size(struct thread_data *td, struct fio_file *f)
338{
339 int ret = 0;
340
Jens Axboed6aed792009-06-03 08:41:15 +0200341 if (fio_file_size_known(f))
Jens Axboe409b3412007-03-28 09:57:01 +0200342 return 0;
343
Jens Axboe7bb48f82007-03-27 15:30:28 +0200344 if (f->filetype == FIO_TYPE_FILE)
345 ret = file_size(td, f);
346 else if (f->filetype == FIO_TYPE_BD)
Jens Axboe53cdc682006-10-18 11:50:58 +0200347 ret = bdev_size(td, f);
Jens Axboe4ccdccd2010-06-24 10:27:22 +0200348 else if (f->filetype == FIO_TYPE_CHAR)
349 ret = char_size(td, f);
Jens Axboe53cdc682006-10-18 11:50:58 +0200350 else
351 f->real_file_size = -1;
352
353 if (ret)
354 return ret;
355
356 if (f->file_offset > f->real_file_size) {
Bruce Cran0f2152c2010-12-15 10:33:03 +0100357 log_err("%s: offset extends end (%llu > %llu)\n", td->o.name,
Jens Axboe4e0a8fa2013-04-15 11:40:57 +0200358 (unsigned long long) f->file_offset,
359 (unsigned long long) f->real_file_size);
Jens Axboe53cdc682006-10-18 11:50:58 +0200360 return 1;
361 }
362
Jens Axboed6aed792009-06-03 08:41:15 +0200363 fio_file_set_size_known(f);
Jens Axboe53cdc682006-10-18 11:50:58 +0200364 return 0;
365}
366
Jens Axboe3baddf22008-04-04 11:10:30 +0200367static int __file_invalidate_cache(struct thread_data *td, struct fio_file *f,
368 unsigned long long off,
369 unsigned long long len)
Jens Axboee5b401d2006-10-18 16:03:40 +0200370{
371 int ret = 0;
372
Jens Axboe5e0074c2009-06-02 21:40:09 +0200373 if (len == -1ULL)
Jens Axboe3baddf22008-04-04 11:10:30 +0200374 len = f->io_size;
375 if (off == -1ULL)
376 off = f->file_offset;
Jens Axboeee56ad52008-02-01 10:30:20 +0100377
Jens Axboe0d1cd202009-06-05 22:11:52 +0200378 if (len == -1ULL || off == -1ULL)
379 return 0;
380
Jens Axboe3baddf22008-04-04 11:10:30 +0200381 dprint(FD_IO, "invalidate cache %s: %llu/%llu\n", f->file_name, off,
382 len);
Jens Axboeb5af8292007-03-08 12:43:13 +0100383
Jens Axboee5b401d2006-10-18 16:03:40 +0200384 /*
385 * FIXME: add blockdev flushing too
386 */
Jens Axboea1c58072009-08-04 23:17:02 +0200387 if (f->mmap_ptr) {
Bruce Cran03e20d62011-01-02 20:14:54 +0100388 ret = posix_madvise(f->mmap_ptr, f->mmap_sz, POSIX_MADV_DONTNEED);
Jens Axboea1c58072009-08-04 23:17:02 +0200389#ifdef FIO_MADV_FREE
Bruce Cran03e20d62011-01-02 20:14:54 +0100390 (void) posix_madvise(f->mmap_ptr, f->mmap_sz, FIO_MADV_FREE);
Jens Axboea1c58072009-08-04 23:17:02 +0200391#endif
392 } else if (f->filetype == FIO_TYPE_FILE) {
Bruce Cranecc314b2011-01-04 10:59:30 +0100393 ret = posix_fadvise(f->fd, off, len, POSIX_FADV_DONTNEED);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100394 } else if (f->filetype == FIO_TYPE_BD) {
Bruce Cranecc314b2011-01-04 10:59:30 +0100395 ret = blockdev_invalidate_cache(f);
Jens Axboe7e0e25c2007-03-27 12:49:56 +0200396 if (ret < 0 && errno == EACCES && geteuid()) {
Jens Axboe7172cfe2007-07-23 14:36:00 +0200397 if (!root_warn) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100398 log_err("fio: only root may flush block "
399 "devices. Cache flush bypassed!\n");
Jens Axboe7172cfe2007-07-23 14:36:00 +0200400 root_warn = 1;
401 }
Jens Axboe7e0e25c2007-03-27 12:49:56 +0200402 ret = 0;
403 }
Jens Axboeb5605e92007-04-16 20:42:33 +0200404 } else if (f->filetype == FIO_TYPE_CHAR || f->filetype == FIO_TYPE_PIPE)
Jens Axboee5b401d2006-10-18 16:03:40 +0200405 ret = 0;
406
407 if (ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100408 td_verror(td, errno, "invalidate_cache");
Jens Axboee5b401d2006-10-18 16:03:40 +0200409 return 1;
Jens Axboe3baddf22008-04-04 11:10:30 +0200410 } else if (ret > 0) {
411 td_verror(td, ret, "invalidate_cache");
412 return 1;
Jens Axboee5b401d2006-10-18 16:03:40 +0200413 }
414
Jens Axboead2da602007-02-26 12:33:27 +0100415 return ret;
Jens Axboe3baddf22008-04-04 11:10:30 +0200416
417}
418
419int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
420{
Jens Axboed6aed792009-06-03 08:41:15 +0200421 if (!fio_file_open(f))
Jens Axboea5fb4612008-05-28 10:54:01 +0200422 return 0;
423
Jens Axboe5e0074c2009-06-02 21:40:09 +0200424 return __file_invalidate_cache(td, f, -1ULL, -1ULL);
Jens Axboee5b401d2006-10-18 16:03:40 +0200425}
426
Jens Axboe6977bcd2008-03-01 15:55:36 +0100427int generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
Jens Axboe53cdc682006-10-18 11:50:58 +0200428{
Jens Axboe6977bcd2008-03-01 15:55:36 +0100429 int ret = 0;
430
Jens Axboeee56ad52008-02-01 10:30:20 +0100431 dprint(FD_FILE, "fd close %s\n", f->file_name);
Jens Axboe4906e0b2008-03-01 18:59:51 +0100432
433 remove_file_hash(f);
434
Jens Axboe6977bcd2008-03-01 15:55:36 +0100435 if (close(f->fd) < 0)
436 ret = errno;
437
Jens Axboeb5af8292007-03-08 12:43:13 +0100438 f->fd = -1;
Jens Axboee6c4d732012-12-12 08:16:27 +0100439
440 if (f->shadow_fd != -1) {
441 close(f->shadow_fd);
442 f->shadow_fd = -1;
443 }
444
Jens Axboe6977bcd2008-03-01 15:55:36 +0100445 return ret;
Jens Axboe53cdc682006-10-18 11:50:58 +0200446}
447
Dmitry Monakhov1ccc6dc2012-09-19 23:22:53 +0400448int file_lookup_open(struct fio_file *f, int flags)
Jens Axboe53cdc682006-10-18 11:50:58 +0200449{
Jens Axboe29c13492008-03-01 19:25:20 +0100450 struct fio_file *__f;
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100451 int from_hash;
452
453 __f = lookup_file_hash(f->file_name);
454 if (__f) {
Jens Axboe9efef3c2008-03-06 10:26:25 +0100455 dprint(FD_FILE, "found file in hash %s\n", f->file_name);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100456 /*
457 * racy, need the __f->lock locked
458 */
459 f->lock = __f->lock;
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100460 from_hash = 1;
461 } else {
Jens Axboe9efef3c2008-03-06 10:26:25 +0100462 dprint(FD_FILE, "file not found in hash %s\n", f->file_name);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100463 from_hash = 0;
464 }
465
Jens Axboee8670ef2008-03-06 12:06:30 +0100466 f->fd = open(f->file_name, flags, 0600);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100467 return from_hash;
468}
469
Jens Axboee6c4d732012-12-12 08:16:27 +0100470static int file_close_shadow_fds(struct thread_data *td)
471{
472 struct fio_file *f;
473 int num_closed = 0;
474 unsigned int i;
475
476 for_each_file(td, f, i) {
477 if (f->shadow_fd == -1)
478 continue;
479
480 close(f->shadow_fd);
481 f->shadow_fd = -1;
482 num_closed++;
483 }
484
485 return num_closed;
486}
487
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100488int generic_open_file(struct thread_data *td, struct fio_file *f)
489{
Jens Axboe66159822007-04-16 21:54:24 +0200490 int is_std = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200491 int flags = 0;
Jens Axboe29c13492008-03-01 19:25:20 +0100492 int from_hash = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200493
Jens Axboeee56ad52008-02-01 10:30:20 +0100494 dprint(FD_FILE, "fd open %s\n", f->file_name);
495
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200496 if (td_trim(td) && f->filetype != FIO_TYPE_BD) {
497 log_err("fio: trim only applies to block device\n");
498 return 1;
499 }
500
Jens Axboe66159822007-04-16 21:54:24 +0200501 if (!strcmp(f->file_name, "-")) {
502 if (td_rw(td)) {
503 log_err("fio: can't read/write to stdin/out\n");
504 return 1;
505 }
506 is_std = 1;
Jens Axboece98fa62007-04-17 13:27:32 +0200507
508 /*
509 * move output logging to stderr, if we are writing to stdout
510 */
511 if (td_write(td))
512 f_out = stderr;
Jens Axboe66159822007-04-16 21:54:24 +0200513 }
514
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200515 if (td_trim(td))
516 goto skip_flags;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100517 if (td->o.odirect)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100518 flags |= OS_O_DIRECT;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100519 if (td->o.sync_io)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100520 flags |= O_SYNC;
Jens Axboe814452b2009-03-04 12:53:13 +0100521 if (td->o.create_on_open)
522 flags |= O_CREAT;
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200523skip_flags:
524 if (f->filetype != FIO_TYPE_FILE)
525 flags |= FIO_O_NOATIME;
Jens Axboe7abf8332006-11-23 15:01:19 +0100526
Aaron Carroll056f3452007-11-26 09:08:53 +0100527open_again:
Jens Axboe660a1cb2007-04-17 15:37:47 +0200528 if (td_write(td)) {
Jens Axboe17308152008-03-07 13:42:31 +0100529 if (!read_only)
530 flags |= O_RDWR;
Jens Axboe53cdc682006-10-18 11:50:58 +0200531
Jens Axboeaf52b342007-03-13 10:07:47 +0100532 if (f->filetype == FIO_TYPE_FILE)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100533 flags |= O_CREAT;
Jens Axboe2fd233b2007-03-02 08:44:25 +0100534
Jens Axboe66159822007-04-16 21:54:24 +0200535 if (is_std)
536 f->fd = dup(STDOUT_FILENO);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100537 else
538 from_hash = file_lookup_open(f, flags);
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200539 } else if (td_read(td)) {
Jens Axboe4241ea82007-09-12 08:18:36 +0200540 if (f->filetype == FIO_TYPE_CHAR && !read_only)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100541 flags |= O_RDWR;
542 else
543 flags |= O_RDONLY;
544
Jens Axboe66159822007-04-16 21:54:24 +0200545 if (is_std)
546 f->fd = dup(STDIN_FILENO);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100547 else
548 from_hash = file_lookup_open(f, flags);
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200549 } else { //td trim
550 flags |= O_RDWR;
551 from_hash = file_lookup_open(f, flags);
Jens Axboe53cdc682006-10-18 11:50:58 +0200552 }
553
554 if (f->fd == -1) {
Jens Axboee4e33252007-03-21 13:07:54 +0100555 char buf[FIO_VERROR_SIZE];
Jens Axboee1161c32007-02-22 19:36:48 +0100556 int __e = errno;
557
Jens Axboe835d9b92009-06-09 15:23:38 +0200558 if (__e == EPERM && (flags & FIO_O_NOATIME)) {
Jens Axboe5921e802008-05-30 15:02:38 +0200559 flags &= ~FIO_O_NOATIME;
Aaron Carroll056f3452007-11-26 09:08:53 +0100560 goto open_again;
561 }
Jens Axboee6c4d732012-12-12 08:16:27 +0100562 if (__e == EMFILE && file_close_shadow_fds(td))
563 goto open_again;
Aaron Carroll056f3452007-11-26 09:08:53 +0100564
Ken Raeburn98ffb8f2013-01-30 22:31:09 +0100565 snprintf(buf, sizeof(buf), "open(%s)", f->file_name);
Jens Axboee4e33252007-03-21 13:07:54 +0100566
Jens Axboea93c5f02012-06-11 08:53:53 +0200567 if (__e == EINVAL && (flags & OS_O_DIRECT)) {
568 log_err("fio: looks like your file system does not " \
569 "support direct=1/buffered=0\n");
570 }
571
Jens Axboee4e33252007-03-21 13:07:54 +0100572 td_verror(td, __e, buf);
Jens Axboe53cdc682006-10-18 11:50:58 +0200573 }
574
Jens Axboe29c13492008-03-01 19:25:20 +0100575 if (!from_hash && f->fd != -1) {
576 if (add_file_hash(f)) {
Jens Axboe3f0ca9b2011-05-24 21:36:24 +0200577 int fio_unused ret;
Jens Axboe29c13492008-03-01 19:25:20 +0100578
579 /*
Jens Axboee6c4d732012-12-12 08:16:27 +0100580 * Stash away descriptor for later close. This is to
581 * work-around a "feature" on Linux, where a close of
582 * an fd that has been opened for write will trigger
583 * udev to call blkid to check partitions, fs id, etc.
584 * That polutes the device cache, which can slow down
585 * unbuffered accesses.
Jens Axboe29c13492008-03-01 19:25:20 +0100586 */
Jens Axboee6c4d732012-12-12 08:16:27 +0100587 if (f->shadow_fd == -1)
588 f->shadow_fd = f->fd;
589 else {
590 /*
591 * OK to ignore, we haven't done anything
592 * with it
593 */
594 ret = generic_close_file(td, f);
595 }
Jens Axboe29c13492008-03-01 19:25:20 +0100596 goto open_again;
597 }
598 }
Jens Axboe4906e0b2008-03-01 18:59:51 +0100599
Jens Axboe53cdc682006-10-18 11:50:58 +0200600 return 0;
Jens Axboeb5af8292007-03-08 12:43:13 +0100601}
602
Jens Axboedf9c26b2009-03-05 10:13:58 +0100603int generic_get_file_size(struct thread_data *td, struct fio_file *f)
Jens Axboe21972cd2006-11-23 12:39:16 +0100604{
Jens Axboedf9c26b2009-03-05 10:13:58 +0100605 return get_file_size(td, f);
Jens Axboe21972cd2006-11-23 12:39:16 +0100606}
607
Jens Axboe7bb48f82007-03-27 15:30:28 +0200608/*
609 * open/close all files, so that ->real_file_size gets set
610 */
Jens Axboebab3fd52007-04-02 10:34:18 +0200611static int get_file_sizes(struct thread_data *td)
Jens Axboe53cdc682006-10-18 11:50:58 +0200612{
613 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100614 unsigned int i;
Jens Axboebab3fd52007-04-02 10:34:18 +0200615 int err = 0;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200616
617 for_each_file(td, f, i) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100618 dprint(FD_FILE, "get file size for %p/%d/%p\n", f, i,
619 f->file_name);
Jens Axboe9efef3c2008-03-06 10:26:25 +0100620
Jens Axboe99a47c62009-04-07 22:20:56 +0200621 if (td_io_get_file_size(td, f)) {
Jens Axboe40b44f42007-04-11 12:43:35 +0200622 if (td->error != ENOENT) {
623 log_err("%s\n", td->verror);
624 err = 1;
625 }
Jens Axboe541d66d2007-03-27 19:50:11 +0200626 clear_error(td);
Jens Axboe07eb79d2007-04-12 13:02:38 +0200627 }
Jens Axboe409b3412007-03-28 09:57:01 +0200628
629 if (f->real_file_size == -1ULL && td->o.size)
630 f->real_file_size = td->o.size / td->o.nr_files;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200631 }
Jens Axboebab3fd52007-04-02 10:34:18 +0200632
633 return err;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200634}
635
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200636struct fio_mount {
637 struct flist_head list;
638 const char *base;
639 char __base[256];
640 unsigned int key;
641};
642
643/*
644 * Get free number of bytes for each file on each unique mount.
645 */
646static unsigned long long get_fs_free_counts(struct thread_data *td)
647{
648 struct flist_head *n, *tmp;
Jens Axboe68b03162010-06-24 09:22:41 +0200649 unsigned long long ret = 0;
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200650 struct fio_mount *fm;
651 FLIST_HEAD(list);
652 struct fio_file *f;
653 unsigned int i;
654
655 for_each_file(td, f, i) {
656 struct stat sb;
657 char buf[256];
658
Jens Axboe4ccdccd2010-06-24 10:27:22 +0200659 if (f->filetype == FIO_TYPE_BD || f->filetype == FIO_TYPE_CHAR) {
660 if (f->real_file_size != -1ULL)
661 ret += f->real_file_size;
Jens Axboe68b03162010-06-24 09:22:41 +0200662 continue;
663 } else if (f->filetype != FIO_TYPE_FILE)
664 continue;
665
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200666 strcpy(buf, f->file_name);
667
668 if (stat(buf, &sb) < 0) {
669 if (errno != ENOENT)
670 break;
671 strcpy(buf, ".");
672 if (stat(buf, &sb) < 0)
673 break;
674 }
675
676 fm = NULL;
677 flist_for_each(n, &list) {
678 fm = flist_entry(n, struct fio_mount, list);
679 if (fm->key == sb.st_dev)
680 break;
681
682 fm = NULL;
683 }
684
685 if (fm)
686 continue;
687
688 fm = malloc(sizeof(*fm));
689 strcpy(fm->__base, buf);
690 fm->base = basename(fm->__base);
691 fm->key = sb.st_dev;
692 flist_add(&fm->list, &list);
693 }
694
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200695 flist_for_each_safe(n, tmp, &list) {
696 unsigned long long sz;
697
698 fm = flist_entry(n, struct fio_mount, list);
699 flist_del(&fm->list);
700
701 sz = get_fs_size(fm->base);
702 if (sz && sz != -1ULL)
703 ret += sz;
704
705 free(fm);
706 }
707
708 return ret;
709}
710
Jens Axboe293b8c12013-01-04 13:16:54 +0100711uint64_t get_start_offset(struct thread_data *td)
Dan Ehrenbergce95d652012-08-16 08:58:21 +0200712{
713 return td->o.start_offset +
714 (td->thread_number - 1) * td->o.offset_increment;
715}
716
Jens Axboe7bb48f82007-03-27 15:30:28 +0200717/*
718 * Open the files and setup files sizes, creating files if necessary.
719 */
720int setup_files(struct thread_data *td)
721{
722 unsigned long long total_size, extend_size;
Jens Axboede98bd32013-04-05 11:09:20 +0200723 struct thread_options *o = &td->o;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200724 struct fio_file *f;
725 unsigned int i;
Jens Axboe000b0802007-03-27 15:56:10 +0200726 int err = 0, need_extend;
Jens Axboee90391e2013-04-13 20:40:53 +0200727 int old_state;
Jens Axboe53cdc682006-10-18 11:50:58 +0200728
Jens Axboeee56ad52008-02-01 10:30:20 +0100729 dprint(FD_FILE, "setup files\n");
730
Jens Axboee90391e2013-04-13 20:40:53 +0200731 old_state = td->runstate;
732 td_set_runstate(td, TD_SETTING_UP);
733
Jens Axboede98bd32013-04-05 11:09:20 +0200734 if (o->read_iolog_file)
Jens Axboe25460cf2012-05-02 13:58:02 +0200735 goto done;
Jens Axboe691c8fb2008-03-07 14:26:26 +0100736
Jens Axboe53cdc682006-10-18 11:50:58 +0200737 /*
738 * if ioengine defines a setup() method, it's responsible for
Jens Axboe7bb48f82007-03-27 15:30:28 +0200739 * opening the files and setting f->real_file_size to indicate
740 * the valid range for that file.
Jens Axboe53cdc682006-10-18 11:50:58 +0200741 */
742 if (td->io_ops->setup)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200743 err = td->io_ops->setup(td);
744 else
Jens Axboebab3fd52007-04-02 10:34:18 +0200745 err = get_file_sizes(td);
Jens Axboe53cdc682006-10-18 11:50:58 +0200746
Jens Axboef1027062006-10-20 10:14:01 +0200747 if (err)
Jens Axboee90391e2013-04-13 20:40:53 +0200748 goto err_out;
Jens Axboef1027062006-10-20 10:14:01 +0200749
Jens Axboe0a7eb122006-10-20 10:36:42 +0200750 /*
Jens Axboe7bb48f82007-03-27 15:30:28 +0200751 * check sizes. if the files/devices do not exist and the size
752 * isn't passed to fio, abort.
Jens Axboe0a7eb122006-10-20 10:36:42 +0200753 */
Jens Axboe7bb48f82007-03-27 15:30:28 +0200754 total_size = 0;
755 for_each_file(td, f, i) {
756 if (f->real_file_size == -1ULL)
757 total_size = -1ULL;
758 else
759 total_size += f->real_file_size;
760 }
Jens Axboe0a7eb122006-10-20 10:36:42 +0200761
Jens Axboede98bd32013-04-05 11:09:20 +0200762 if (o->fill_device)
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200763 td->fill_device_size = get_fs_free_counts(td);
764
Jens Axboe7bb48f82007-03-27 15:30:28 +0200765 /*
766 * device/file sizes are zero and no size given, punt
767 */
Jens Axboede98bd32013-04-05 11:09:20 +0200768 if ((!total_size || total_size == -1ULL) && !o->size &&
769 !(td->io_ops->flags & FIO_NOIO) && !o->fill_device &&
770 !(o->nr_files && (o->file_size_low || o->file_size_high))) {
771 log_err("%s: you need to specify size=\n", o->name);
Jens Axboee1161c32007-02-22 19:36:48 +0100772 td_verror(td, EINVAL, "total_file_size");
Jens Axboee90391e2013-04-13 20:40:53 +0200773 goto err_out;
Jens Axboe53cdc682006-10-18 11:50:58 +0200774 }
775
Jens Axboe7bb48f82007-03-27 15:30:28 +0200776 /*
777 * now file sizes are known, so we can set ->io_size. if size= is
778 * not given, ->io_size is just equal to ->real_file_size. if size
779 * is given, ->io_size is size / nr_files.
780 */
781 extend_size = total_size = 0;
782 need_extend = 0;
783 for_each_file(td, f, i) {
Dan Ehrenbergce95d652012-08-16 08:58:21 +0200784 f->file_offset = get_start_offset(td);
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200785
Jens Axboede98bd32013-04-05 11:09:20 +0200786 if (!o->file_size_low) {
Jens Axboe7bb48f82007-03-27 15:30:28 +0200787 /*
788 * no file size range given, file size is equal to
789 * total size divided by number of files. if that is
790 * zero, set it to the real file size.
791 */
Jens Axboede98bd32013-04-05 11:09:20 +0200792 f->io_size = o->size / o->nr_files;
Jens Axboe65bdb102008-01-24 13:13:12 +0100793 if (!f->io_size)
Jens Axboe273f8c92008-01-25 14:02:15 +0100794 f->io_size = f->real_file_size - f->file_offset;
Jens Axboede98bd32013-04-05 11:09:20 +0200795 } else if (f->real_file_size < o->file_size_low ||
796 f->real_file_size > o->file_size_high) {
797 if (f->file_offset > o->file_size_low)
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200798 goto err_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200799 /*
800 * file size given. if it's fixed, use that. if it's a
801 * range, generate a random size in-between.
802 */
Jens Axboede98bd32013-04-05 11:09:20 +0200803 if (o->file_size_low == o->file_size_high)
804 f->io_size = o->file_size_low - f->file_offset;
805 else {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100806 f->io_size = get_rand_file_size(td)
807 - f->file_offset;
808 }
Jens Axboe65bdb102008-01-24 13:13:12 +0100809 } else
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200810 f->io_size = f->real_file_size - f->file_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200811
812 if (f->io_size == -1ULL)
813 total_size = -1ULL;
Shaohua Li4d002562012-09-21 08:32:32 +0200814 else {
Jens Axboede98bd32013-04-05 11:09:20 +0200815 if (o->size_percent)
816 f->io_size = (f->io_size * o->size_percent) / 100;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200817 total_size += f->io_size;
Shaohua Li4d002562012-09-21 08:32:32 +0200818 }
Jens Axboe7bb48f82007-03-27 15:30:28 +0200819
820 if (f->filetype == FIO_TYPE_FILE &&
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200821 (f->io_size + f->file_offset) > f->real_file_size &&
Jens Axboe7bb48f82007-03-27 15:30:28 +0200822 !(td->io_ops->flags & FIO_DISKLESSIO)) {
Jens Axboede98bd32013-04-05 11:09:20 +0200823 if (!o->create_on_open) {
Jens Axboe814452b2009-03-04 12:53:13 +0100824 need_extend++;
825 extend_size += (f->io_size + f->file_offset);
826 } else
827 f->real_file_size = f->io_size + f->file_offset;
Jens Axboed6aed792009-06-03 08:41:15 +0200828 fio_file_set_extend(f);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100829 }
Jens Axboe7bb48f82007-03-27 15:30:28 +0200830 }
831
Jens Axboede98bd32013-04-05 11:09:20 +0200832 if (!o->size || o->size > total_size)
833 o->size = total_size;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200834
835 /*
836 * See if we need to extend some files
837 */
838 if (need_extend) {
839 temp_stall_ts = 1;
Jens Axboef3afa572012-09-17 13:34:16 +0200840 if (output_format == FIO_OUTPUT_NORMAL)
Shaozhi Shawn Yea7ba8c52008-09-10 09:04:18 +0200841 log_info("%s: Laying out IO file(s) (%u file(s) /"
Jens Axboede98bd32013-04-05 11:09:20 +0200842 " %lluMB)\n", o->name, need_extend,
Shaozhi Shawn Yea7ba8c52008-09-10 09:04:18 +0200843 extend_size >> 20);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200844
845 for_each_file(td, f, i) {
Jens Axboe5e0074c2009-06-02 21:40:09 +0200846 unsigned long long old_len = -1ULL, extend_len = -1ULL;
Jens Axboe3baddf22008-04-04 11:10:30 +0200847
Jens Axboed6aed792009-06-03 08:41:15 +0200848 if (!fio_file_extend(f))
Jens Axboe7bb48f82007-03-27 15:30:28 +0200849 continue;
850
Jens Axboe409b3412007-03-28 09:57:01 +0200851 assert(f->filetype == FIO_TYPE_FILE);
Jens Axboed6aed792009-06-03 08:41:15 +0200852 fio_file_clear_extend(f);
Jens Axboede98bd32013-04-05 11:09:20 +0200853 if (!o->fill_device) {
Jens Axboe5e0074c2009-06-02 21:40:09 +0200854 old_len = f->real_file_size;
Jens Axboe0b9d69e2009-09-11 22:29:54 +0200855 extend_len = f->io_size + f->file_offset -
856 old_len;
Jens Axboe5e0074c2009-06-02 21:40:09 +0200857 }
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200858 f->real_file_size = (f->io_size + f->file_offset);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200859 err = extend_file(td, f);
860 if (err)
861 break;
Jens Axboe5e0074c2009-06-02 21:40:09 +0200862
Jens Axboe3baddf22008-04-04 11:10:30 +0200863 err = __file_invalidate_cache(td, f, old_len,
864 extend_len);
865 close(f->fd);
866 f->fd = -1;
867 if (err)
868 break;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200869 }
870 temp_stall_ts = 0;
871 }
872
873 if (err)
Jens Axboee90391e2013-04-13 20:40:53 +0200874 goto err_out;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200875
Jens Axboede98bd32013-04-05 11:09:20 +0200876 if (!o->zone_size)
877 o->zone_size = o->size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200878
Jens Axboeea966f82007-09-03 10:09:37 +0200879 /*
880 * iolog already set the total io size, if we read back
881 * stored entries.
882 */
Jens Axboede98bd32013-04-05 11:09:20 +0200883 if (!o->read_iolog_file)
884 td->total_io_size = o->size * o->loops;
Jens Axboe25460cf2012-05-02 13:58:02 +0200885
886done:
Jens Axboede98bd32013-04-05 11:09:20 +0200887 if (o->create_only)
Jens Axboe25460cf2012-05-02 13:58:02 +0200888 td->done = 1;
889
Jens Axboee90391e2013-04-13 20:40:53 +0200890 td_set_runstate(td, old_state);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200891 return 0;
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200892err_offset:
Jens Axboede98bd32013-04-05 11:09:20 +0200893 log_err("%s: you need to specify valid offset=\n", o->name);
Jens Axboee90391e2013-04-13 20:40:53 +0200894err_out:
895 td_set_runstate(td, old_state);
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200896 return 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200897}
898
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200899int pre_read_files(struct thread_data *td)
900{
901 struct fio_file *f;
902 unsigned int i;
903
904 dprint(FD_FILE, "pre_read files\n");
905
906 for_each_file(td, f, i) {
907 pre_read_file(td, f);
908 }
909
910 return 1;
911}
912
Jens Axboe9c6f6312012-11-07 09:15:45 +0100913static int __init_rand_distribution(struct thread_data *td, struct fio_file *f)
914{
Jens Axboe23162962012-11-07 19:47:47 +0100915 unsigned int range_size, seed;
Jens Axboe9c6f6312012-11-07 09:15:45 +0100916 unsigned long nranges;
Jens Axboe21415db2013-01-04 13:09:29 +0100917 uint64_t file_size;
Jens Axboe9c6f6312012-11-07 09:15:45 +0100918
919 range_size = min(td->o.min_bs[DDIR_READ], td->o.min_bs[DDIR_WRITE]);
Jens Axboe21415db2013-01-04 13:09:29 +0100920 file_size = min(f->real_file_size, f->io_size);
Jens Axboe9c6f6312012-11-07 09:15:45 +0100921
Jens Axboe21415db2013-01-04 13:09:29 +0100922 nranges = (file_size + range_size - 1) / range_size;
Jens Axboe9c6f6312012-11-07 09:15:45 +0100923
Jens Axboe23162962012-11-07 19:47:47 +0100924 seed = jhash(f->file_name, strlen(f->file_name), 0) * td->thread_number;
Jens Axboe84256872012-11-17 10:06:36 -0700925 if (!td->o.rand_repeatable)
926 seed = td->rand_seeds[4];
927
Jens Axboe9c6f6312012-11-07 09:15:45 +0100928 if (td->o.random_distribution == FIO_RAND_DIST_ZIPF)
Jens Axboe888677a2013-04-10 22:19:46 +0200929 zipf_init(&f->zipf, nranges, td->o.zipf_theta.u.f, seed);
Jens Axboe9c6f6312012-11-07 09:15:45 +0100930 else
Jens Axboe888677a2013-04-10 22:19:46 +0200931 pareto_init(&f->zipf, nranges, td->o.pareto_h.u.f, seed);
Jens Axboe9c6f6312012-11-07 09:15:45 +0100932
933 return 1;
934}
935
936static int init_rand_distribution(struct thread_data *td)
937{
938 struct fio_file *f;
939 unsigned int i;
940 int state;
941
942 if (td->o.random_distribution == FIO_RAND_DIST_RANDOM)
943 return 0;
944
945 state = td->runstate;
946 td_set_runstate(td, TD_SETTING_UP);
947 for_each_file(td, f, i)
948 __init_rand_distribution(td, f);
949 td_set_runstate(td, state);
950
951 return 1;
952}
953
Jens Axboe68727072007-03-15 20:49:25 +0100954int init_random_map(struct thread_data *td)
955{
Jens Axboe51ede0b2012-11-22 13:50:29 +0100956 unsigned long long blocks;
Jens Axboe68727072007-03-15 20:49:25 +0100957 struct fio_file *f;
958 unsigned int i;
959
Jens Axboe9c6f6312012-11-07 09:15:45 +0100960 if (init_rand_distribution(td))
961 return 0;
Jens Axboe3831a842012-11-26 19:59:14 +0100962 if (!td_random(td))
Jens Axboe68727072007-03-15 20:49:25 +0100963 return 0;
964
965 for_each_file(td, f, i) {
Jens Axboe38f30c82013-01-11 14:03:05 +0100966 uint64_t file_size = min(f->real_file_size, f->io_size);
967
Jens Axboe53737ae2013-02-21 15:18:17 +0100968 blocks = file_size / (unsigned long long) td->o.rw_min_bs;
969
Jens Axboe8055e412012-11-26 08:43:47 +0100970 if (td->o.random_generator == FIO_RAND_GEN_LFSR) {
Jens Axboe82af46b2012-12-04 13:12:25 +0100971 unsigned long seed;
972
973 seed = td->rand_seeds[FIO_RAND_BLOCK_OFF];
974
Alex Pyrgiotisd474cbc2013-03-08 14:37:03 +0200975 if (!lfsr_init(&f->lfsr, blocks, seed, seed & 0xF))
Jens Axboe8055e412012-11-26 08:43:47 +0100976 continue;
Jens Axboe3831a842012-11-26 19:59:14 +0100977 } else if (!td->o.norandommap) {
Jens Axboe7ebd7962012-11-28 21:24:46 +0100978 f->io_axmap = axmap_new(blocks);
979 if (f->io_axmap)
Jens Axboe8055e412012-11-26 08:43:47 +0100980 continue;
Jens Axboe1cad7122012-11-29 21:37:11 +0100981 } else if (td->o.norandommap)
982 continue;
Jens Axboeceadd592012-02-02 08:41:28 +0100983
Jens Axboe2b386d22008-03-26 10:32:57 +0100984 if (!td->o.softrandommap) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100985 log_err("fio: failed allocating random map. If running"
986 " a large number of jobs, try the 'norandommap'"
Jens Axboe2b386d22008-03-26 10:32:57 +0100987 " option or set 'softrandommap'. Or give"
988 " a larger --alloc-size to fio.\n");
Jens Axboe68727072007-03-15 20:49:25 +0100989 return 1;
990 }
Jens Axboe303032a2008-03-26 10:11:10 +0100991
992 log_info("fio: file %s failed allocating random map. Running "
993 "job without.\n", f->file_name);
Jens Axboe68727072007-03-15 20:49:25 +0100994 }
995
996 return 0;
997}
998
Jens Axboe53cdc682006-10-18 11:50:58 +0200999void close_files(struct thread_data *td)
1000{
Jens Axboe0ab8db82006-10-18 17:16:23 +02001001 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +01001002 unsigned int i;
Jens Axboe53cdc682006-10-18 11:50:58 +02001003
Jens Axboe2be3eec2009-06-10 09:05:13 +02001004 for_each_file(td, f, i) {
1005 if (fio_file_open(f))
1006 td_io_close_file(td, f);
1007 }
Jens Axboe24ffd2c2008-03-01 15:47:08 +01001008}
1009
1010void close_and_free_files(struct thread_data *td)
1011{
1012 struct fio_file *f;
1013 unsigned int i;
1014
Jens Axboeee56ad52008-02-01 10:30:20 +01001015 dprint(FD_FILE, "close files\n");
1016
Jens Axboe0ab8db82006-10-18 17:16:23 +02001017 for_each_file(td, f, i) {
Jens Axboeffdfabd2008-03-26 09:18:14 +01001018 if (td->o.unlink && f->filetype == FIO_TYPE_FILE) {
1019 dprint(FD_FILE, "free unlink %s\n", f->file_name);
Jens Axboe132ad462006-11-23 12:23:12 +01001020 unlink(f->file_name);
Jens Axboeffdfabd2008-03-26 09:18:14 +01001021 }
Jens Axboebdb4e2e2007-03-01 09:54:57 +01001022
Jens Axboe22a57ba2009-06-09 14:34:35 +02001023 if (fio_file_open(f))
1024 td_io_close_file(td, f);
1025
Shaozhi Shawn Yeb9fbcf22008-09-10 09:05:17 +02001026 remove_file_hash(f);
Jens Axboeb3dc7f02007-03-09 12:52:15 +01001027
Jens Axboef17c4392008-03-01 18:40:46 +01001028 sfree(f->file_name);
Jens Axboefa1da862007-03-23 15:20:54 +01001029 f->file_name = NULL;
Jens Axboe7ebd7962012-11-28 21:24:46 +01001030 axmap_free(f->io_axmap);
1031 f->io_axmap = NULL;
Jens Axboe78d99e62008-03-01 18:41:50 +01001032 sfree(f);
Jens Axboe53cdc682006-10-18 11:50:58 +02001033 }
Jens Axboeb4a6a592006-10-20 13:54:47 +02001034
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001035 td->o.filename = NULL;
Jens Axboecade3ef2007-03-23 15:29:45 +01001036 free(td->files);
Jens Axboed7df1d12013-03-20 19:57:01 -06001037 free(td->file_locks);
Jens Axboe9efef3c2008-03-06 10:26:25 +01001038 td->files_index = 0;
Jens Axboeb4a6a592006-10-20 13:54:47 +02001039 td->files = NULL;
Jens Axboed7df1d12013-03-20 19:57:01 -06001040 td->file_locks = NULL;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001041 td->o.nr_files = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +02001042}
Jens Axboeaf52b342007-03-13 10:07:47 +01001043
Jens Axboee3bab462007-03-13 11:22:09 +01001044static void get_file_type(struct fio_file *f)
Jens Axboeaf52b342007-03-13 10:07:47 +01001045{
1046 struct stat sb;
1047
Jens Axboe66159822007-04-16 21:54:24 +02001048 if (!strcmp(f->file_name, "-"))
1049 f->filetype = FIO_TYPE_PIPE;
1050 else
1051 f->filetype = FIO_TYPE_FILE;
Jens Axboeaf52b342007-03-13 10:07:47 +01001052
Bruce Cran38921822012-02-22 17:55:16 +00001053 /* \\.\ is the device namespace in Windows, where every file is
1054 * a block device */
1055 if (strncmp(f->file_name, "\\\\.\\", 4) == 0)
1056 f->filetype = FIO_TYPE_BD;
1057
Jens Axboeb30d3952010-02-06 23:36:26 +01001058 if (!stat(f->file_name, &sb)) {
Bruce Cran38921822012-02-22 17:55:16 +00001059 if (S_ISBLK(sb.st_mode))
Jens Axboeaf52b342007-03-13 10:07:47 +01001060 f->filetype = FIO_TYPE_BD;
1061 else if (S_ISCHR(sb.st_mode))
1062 f->filetype = FIO_TYPE_CHAR;
Jens Axboeb5605e92007-04-16 20:42:33 +02001063 else if (S_ISFIFO(sb.st_mode))
1064 f->filetype = FIO_TYPE_PIPE;
Jens Axboeaf52b342007-03-13 10:07:47 +01001065 }
1066}
1067
Jens Axboef29b25a2007-07-23 08:56:43 +02001068int add_file(struct thread_data *td, const char *fname)
Jens Axboeaf52b342007-03-13 10:07:47 +01001069{
Jens Axboe7b4e4fe2007-03-13 12:51:40 +01001070 int cur_files = td->files_index;
Jens Axboebd0ee742007-03-23 14:26:23 +01001071 char file_name[PATH_MAX];
Jens Axboeaf52b342007-03-13 10:07:47 +01001072 struct fio_file *f;
Jens Axboebd0ee742007-03-23 14:26:23 +01001073 int len = 0;
Jens Axboeaf52b342007-03-13 10:07:47 +01001074
Jens Axboeee56ad52008-02-01 10:30:20 +01001075 dprint(FD_FILE, "add file %s\n", fname);
1076
Jens Axboef17c4392008-03-01 18:40:46 +01001077 f = smalloc(sizeof(*f));
Jens Axboec48c0be2008-07-31 19:55:02 +02001078 if (!f) {
1079 log_err("fio: smalloc OOM\n");
1080 assert(0);
1081 }
Jens Axboe0b9d69e2009-09-11 22:29:54 +02001082
Jens Axboeaf52b342007-03-13 10:07:47 +01001083 f->fd = -1;
Jens Axboee6c4d732012-12-12 08:16:27 +01001084 f->shadow_fd = -1;
Jens Axboe33c48812013-01-21 09:46:06 -07001085 fio_file_reset(td, f);
Jens Axboebd0ee742007-03-23 14:26:23 +01001086
Jens Axboefc99bc02009-03-04 15:27:42 +01001087 if (td->files_size <= td->files_index) {
Jianpeng Ma1983e322013-01-10 13:19:27 +01001088 unsigned int new_size = td->o.nr_files + 1;
Jens Axboe126d65c2008-03-01 18:04:31 +01001089
Jens Axboefc99bc02009-03-04 15:27:42 +01001090 dprint(FD_FILE, "resize file array to %d files\n", new_size);
1091
1092 td->files = realloc(td->files, new_size * sizeof(f));
Jianpeng Mad537c082013-01-11 08:52:55 +01001093 if (td->files == NULL) {
1094 log_err("fio: realloc OOM\n");
1095 assert(0);
1096 }
Jens Axboed7df1d12013-03-20 19:57:01 -06001097 if (td->o.file_lock_mode != FILE_LOCK_NONE) {
1098 td->file_locks = realloc(td->file_locks, new_size);
1099 if (!td->file_locks) {
1100 log_err("fio: realloc OOM\n");
1101 assert(0);
1102 }
1103 td->file_locks[cur_files] = FILE_LOCK_NONE;
1104 }
Jens Axboefc99bc02009-03-04 15:27:42 +01001105 td->files_size = new_size;
1106 }
Jens Axboe8bb76792009-03-04 15:37:52 +01001107 td->files[cur_files] = f;
Shaohua Li89ac1d42012-06-11 08:56:32 +02001108 f->fileno = cur_files;
Jens Axboe126d65c2008-03-01 18:04:31 +01001109
Jens Axboe07eb79d2007-04-12 13:02:38 +02001110 /*
1111 * init function, io engine may not be loaded yet
1112 */
1113 if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO))
1114 f->real_file_size = -1ULL;
1115
Jens Axboebd0ee742007-03-23 14:26:23 +01001116 if (td->o.directory)
1117 len = sprintf(file_name, "%s/", td->o.directory);
1118
1119 sprintf(file_name + len, "%s", fname);
Jens Axboef17c4392008-03-01 18:40:46 +01001120 f->file_name = smalloc_strdup(file_name);
Jens Axboec48c0be2008-07-31 19:55:02 +02001121 if (!f->file_name) {
1122 log_err("fio: smalloc OOM\n");
1123 assert(0);
1124 }
Jens Axboe0b9d69e2009-09-11 22:29:54 +02001125
Jens Axboee3bab462007-03-13 11:22:09 +01001126 get_file_type(f);
Jens Axboeaf52b342007-03-13 10:07:47 +01001127
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001128 switch (td->o.file_lock_mode) {
1129 case FILE_LOCK_NONE:
1130 break;
1131 case FILE_LOCK_READWRITE:
Jens Axboed7df1d12013-03-20 19:57:01 -06001132 f->rwlock = fio_rwlock_init();
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001133 break;
1134 case FILE_LOCK_EXCLUSIVE:
Jens Axboe521da522012-08-02 11:21:36 +02001135 f->lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001136 break;
1137 default:
1138 log_err("fio: unknown lock mode: %d\n", td->o.file_lock_mode);
1139 assert(0);
1140 }
Jens Axboe29c13492008-03-01 19:25:20 +01001141
Jens Axboe7b4e4fe2007-03-13 12:51:40 +01001142 td->files_index++;
Jens Axboe15494412007-03-13 11:52:47 +01001143 if (f->filetype == FIO_TYPE_FILE)
1144 td->nr_normal_files++;
Jens Axboef29b25a2007-07-23 08:56:43 +02001145
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001146 dprint(FD_FILE, "file %p \"%s\" added at %d\n", f, f->file_name,
1147 cur_files);
Jens Axboe9efef3c2008-03-06 10:26:25 +01001148
Jens Axboef29b25a2007-07-23 08:56:43 +02001149 return cur_files;
Jens Axboeaf52b342007-03-13 10:07:47 +01001150}
Jens Axboe0ad920e2007-03-13 11:06:45 +01001151
Jens Axboe49ffb4a2010-08-24 15:01:37 +02001152int add_file_exclusive(struct thread_data *td, const char *fname)
1153{
1154 struct fio_file *f;
1155 unsigned int i;
1156
1157 for_each_file(td, f, i) {
1158 if (!strcmp(f->file_name, fname))
1159 return i;
1160 }
1161
1162 return add_file(td, fname);
1163}
1164
Jens Axboe0ad920e2007-03-13 11:06:45 +01001165void get_file(struct fio_file *f)
1166{
Jens Axboe8172fe92008-02-01 21:51:02 +01001167 dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references);
Jens Axboed6aed792009-06-03 08:41:15 +02001168 assert(fio_file_open(f));
Jens Axboe0ad920e2007-03-13 11:06:45 +01001169 f->references++;
1170}
1171
Jens Axboe6977bcd2008-03-01 15:55:36 +01001172int put_file(struct thread_data *td, struct fio_file *f)
Jens Axboe0ad920e2007-03-13 11:06:45 +01001173{
Jens Axboe98e1ac42008-03-06 11:56:48 +01001174 int f_ret = 0, ret = 0;
Jens Axboe6977bcd2008-03-01 15:55:36 +01001175
Jens Axboe8172fe92008-02-01 21:51:02 +01001176 dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references);
Jens Axboeee56ad52008-02-01 10:30:20 +01001177
Jens Axboe22a57ba2009-06-09 14:34:35 +02001178 if (!fio_file_open(f)) {
1179 assert(f->fd == -1);
Jens Axboe6977bcd2008-03-01 15:55:36 +01001180 return 0;
Jens Axboe22a57ba2009-06-09 14:34:35 +02001181 }
Jens Axboe0ad920e2007-03-13 11:06:45 +01001182
1183 assert(f->references);
1184 if (--f->references)
Jens Axboe6977bcd2008-03-01 15:55:36 +01001185 return 0;
Jens Axboe0ad920e2007-03-13 11:06:45 +01001186
Jens Axboed424d4d2007-04-17 09:05:10 +02001187 if (should_fsync(td) && td->o.fsync_on_close)
Jens Axboe98e1ac42008-03-06 11:56:48 +01001188 f_ret = fsync(f->fd);
Jens Axboeebb14152007-03-13 14:42:15 +01001189
Jens Axboe0ad920e2007-03-13 11:06:45 +01001190 if (td->io_ops->close_file)
Jens Axboe6977bcd2008-03-01 15:55:36 +01001191 ret = td->io_ops->close_file(td, f);
Jens Axboe1020a132007-03-29 11:15:06 +02001192
Jens Axboe98e1ac42008-03-06 11:56:48 +01001193 if (!ret)
Jens Axboea5fb4612008-05-28 10:54:01 +02001194 ret = f_ret;
Jens Axboe98e1ac42008-03-06 11:56:48 +01001195
Jens Axboe0ad920e2007-03-13 11:06:45 +01001196 td->nr_open_files--;
Jens Axboed6aed792009-06-03 08:41:15 +02001197 fio_file_clear_open(f);
Jens Axboe22a57ba2009-06-09 14:34:35 +02001198 assert(f->fd == -1);
Jens Axboe6977bcd2008-03-01 15:55:36 +01001199 return ret;
Jens Axboe0ad920e2007-03-13 11:06:45 +01001200}
Jens Axboebbf6b542007-03-13 15:28:55 +01001201
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001202void lock_file(struct thread_data *td, struct fio_file *f, enum fio_ddir ddir)
Jens Axboeb2bd2bd2008-03-01 18:19:52 +01001203{
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001204 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1205 return;
Jens Axboe29c13492008-03-01 19:25:20 +01001206
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001207 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
1208 if (ddir == DDIR_READ)
Jens Axboed7df1d12013-03-20 19:57:01 -06001209 fio_rwlock_read(f->rwlock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001210 else
Jens Axboed7df1d12013-03-20 19:57:01 -06001211 fio_rwlock_write(f->rwlock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001212 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1213 fio_mutex_down(f->lock);
1214
Jens Axboed7df1d12013-03-20 19:57:01 -06001215 td->file_locks[f->fileno] = td->o.file_lock_mode;
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001216}
1217
1218void unlock_file(struct thread_data *td, struct fio_file *f)
1219{
1220 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1221 return;
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001222
Jens Axboed7df1d12013-03-20 19:57:01 -06001223 if (td->o.file_lock_mode == FILE_LOCK_READWRITE)
1224 fio_rwlock_unlock(f->rwlock);
1225 else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001226 fio_mutex_up(f->lock);
Jens Axboed7df1d12013-03-20 19:57:01 -06001227
1228 td->file_locks[f->fileno] = FILE_LOCK_NONE;
Jens Axboeb2bd2bd2008-03-01 18:19:52 +01001229}
1230
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001231void unlock_file_all(struct thread_data *td, struct fio_file *f)
Jens Axboeb2bd2bd2008-03-01 18:19:52 +01001232{
Jens Axboed7df1d12013-03-20 19:57:01 -06001233 if (td->file_locks[f->fileno] != FILE_LOCK_NONE)
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
Ken Raeburn98ffb8f2013-01-30 22:31:09 +01001247 snprintf(buf, FIO_VERROR_SIZE, "opendir(%s)", dirname);
Jens Axboe0ddb2702007-03-27 19:43:53 +02001248 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 Axboed7df1d12013-03-20 19:57:01 -06001307 if (td->o.file_lock_mode != FILE_LOCK_NONE)
1308 td->file_locks = malloc(org->files_index);
1309
Jens Axboe9efef3c2008-03-06 10:26:25 +01001310 for_each_file(org, f, i) {
Jens Axboeb0fe4212008-03-01 18:22:27 +01001311 struct fio_file *__f;
1312
Jens Axboef17c4392008-03-01 18:40:46 +01001313 __f = smalloc(sizeof(*__f));
Jens Axboec48c0be2008-07-31 19:55:02 +02001314 if (!__f) {
1315 log_err("fio: smalloc OOM\n");
1316 assert(0);
1317 }
Jens Axboe22a57ba2009-06-09 14:34:35 +02001318 __f->fd = -1;
Jens Axboe33c48812013-01-21 09:46:06 -07001319 fio_file_reset(td, __f);
Jens Axboe0b9d69e2009-09-11 22:29:54 +02001320
Aaron Carrollbc3456f2008-06-25 09:20:37 +02001321 if (f->file_name) {
Jens Axboef17c4392008-03-01 18:40:46 +01001322 __f->file_name = smalloc_strdup(f->file_name);
Jens Axboec48c0be2008-07-31 19:55:02 +02001323 if (!__f->file_name) {
1324 log_err("fio: smalloc OOM\n");
1325 assert(0);
1326 }
Jens Axboe0b9d69e2009-09-11 22:29:54 +02001327
Aaron Carrollbc3456f2008-06-25 09:20:37 +02001328 __f->filetype = f->filetype;
1329 }
Jens Axboeb0fe4212008-03-01 18:22:27 +01001330
1331 td->files[i] = __f;
Jens Axboecade3ef2007-03-23 15:29:45 +01001332 }
1333}
Jens Axboef29b25a2007-07-23 08:56:43 +02001334
1335/*
1336 * Returns the index that matches the filename, or -1 if not there
1337 */
1338int get_fileno(struct thread_data *td, const char *fname)
1339{
1340 struct fio_file *f;
1341 unsigned int i;
1342
1343 for_each_file(td, f, i)
1344 if (!strcmp(f->file_name, fname))
1345 return i;
1346
1347 return -1;
1348}
1349
1350/*
1351 * For log usage, where we add/open/close files automatically
1352 */
1353void free_release_files(struct thread_data *td)
1354{
1355 close_files(td);
1356 td->files_index = 0;
1357 td->nr_normal_files = 0;
1358}
Jens Axboe33c48812013-01-21 09:46:06 -07001359
1360void fio_file_reset(struct thread_data *td, struct fio_file *f)
1361{
1362 f->last_pos = f->file_offset;
1363 f->last_start = -1ULL;
1364 if (f->io_axmap)
1365 axmap_reset(f->io_axmap);
1366 if (td->o.random_generator == FIO_RAND_GEN_LFSR)
1367 lfsr_reset(&f->lfsr, td->rand_seeds[FIO_RAND_BLOCK_OFF]);
1368}