blob: d67f1124618744db85595c2f037ffe07f9788794 [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) {
John3cd4c662013-12-29 19:20:35 -0700124 if (errno != EFBIG) {
125 td_verror(td, errno, "ftruncate");
126 goto err;
127 }
Jens Axboe5e0074c2009-06-02 21:40:09 +0200128 }
Jens Axboe5e0074c2009-06-02 21:40:09 +0200129 }
Jens Axboe40f82982006-10-25 11:21:05 +0200130
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100131 b = malloc(td->o.max_bs[DDIR_WRITE]);
Jens Axboe53cdc682006-10-18 11:50:58 +0200132
Jens Axboe7bb48f82007-03-27 15:30:28 +0200133 left = f->real_file_size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200134 while (left && !td->terminate) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100135 bs = td->o.max_bs[DDIR_WRITE];
Jens Axboe53cdc682006-10-18 11:50:58 +0200136 if (bs > left)
137 bs = left;
138
Jens Axboecc86c392013-05-03 15:12:33 +0200139 fill_io_buffer(td, b, bs, bs);
140
Jens Axboe53cdc682006-10-18 11:50:58 +0200141 r = write(f->fd, b, bs);
142
Jens Axboe5e0074c2009-06-02 21:40:09 +0200143 if (r > 0) {
144 left -= r;
Jens Axboe53cdc682006-10-18 11:50:58 +0200145 continue;
146 } else {
Jens Axboe5e0074c2009-06-02 21:40:09 +0200147 if (r < 0) {
148 int __e = errno;
149
150 if (__e == ENOSPC) {
151 if (td->o.fill_device)
152 break;
153 log_info("fio: ENOSPC on laying out "
154 "file, stopping\n");
155 break;
156 }
Jens Axboee1161c32007-02-22 19:36:48 +0100157 td_verror(td, errno, "write");
Jens Axboe5e0074c2009-06-02 21:40:09 +0200158 } else
Jens Axboee1161c32007-02-22 19:36:48 +0100159 td_verror(td, EIO, "write");
Jens Axboe53cdc682006-10-18 11:50:58 +0200160
161 break;
162 }
163 }
164
Jens Axboeffdfabd2008-03-26 09:18:14 +0100165 if (td->terminate) {
166 dprint(FD_FILE, "terminate unlink %s\n", f->file_name);
Jens Axboe53cdc682006-10-18 11:50:58 +0200167 unlink(f->file_name);
Jens Axboeffdfabd2008-03-26 09:18:14 +0100168 } else if (td->o.create_fsync) {
Jens Axboe98e1ac42008-03-06 11:56:48 +0100169 if (fsync(f->fd) < 0) {
170 td_verror(td, errno, "fsync");
171 goto err;
172 }
173 }
Jens Axboe0d1cd202009-06-05 22:11:52 +0200174 if (td->o.fill_device && !td_write(td)) {
Jens Axboed6aed792009-06-03 08:41:15 +0200175 fio_file_clear_size_known(f);
Jens Axboe5e0074c2009-06-02 21:40:09 +0200176 if (td_io_get_file_size(td, f))
177 goto err;
178 if (f->io_size > f->real_file_size)
179 f->io_size = f->real_file_size;
180 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200181
182 free(b);
Jens Axboe507a7022007-03-27 15:52:46 +0200183done:
Jens Axboe53cdc682006-10-18 11:50:58 +0200184 return 0;
185err:
186 close(f->fd);
187 f->fd = -1;
188 return 1;
189}
190
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200191static int pre_read_file(struct thread_data *td, struct fio_file *f)
192{
Jens Axboeb0f65862009-05-20 11:52:15 +0200193 int r, did_open = 0, old_runstate;
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200194 unsigned long long left;
195 unsigned int bs;
196 char *b;
197
Jens Axboe9c0d2242009-07-01 12:26:28 +0200198 if (td->io_ops->flags & FIO_PIPEIO)
199 return 0;
200
Jens Axboed6aed792009-06-03 08:41:15 +0200201 if (!fio_file_open(f)) {
Jens Axboeb0f65862009-05-20 11:52:15 +0200202 if (td->io_ops->open_file(td, f)) {
203 log_err("fio: cannot pre-read, failed to open file\n");
204 return 1;
205 }
206 did_open = 1;
207 }
208
209 old_runstate = td->runstate;
210 td_set_runstate(td, TD_PRE_READING);
211
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200212 bs = td->o.max_bs[DDIR_READ];
213 b = malloc(bs);
214 memset(b, 0, bs);
215
216 lseek(f->fd, f->file_offset, SEEK_SET);
217 left = f->io_size;
218
219 while (left && !td->terminate) {
220 if (bs > left)
221 bs = left;
222
223 r = read(f->fd, b, bs);
224
225 if (r == (int) bs) {
226 left -= bs;
227 continue;
228 } else {
229 td_verror(td, EIO, "pre_read");
230 break;
231 }
232 }
233
Jens Axboeb0f65862009-05-20 11:52:15 +0200234 td_set_runstate(td, old_runstate);
235
236 if (did_open)
237 td->io_ops->close_file(td, f);
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200238 free(b);
239 return 0;
240}
241
Jens Axboe7bb48f82007-03-27 15:30:28 +0200242static unsigned long long get_rand_file_size(struct thread_data *td)
Jens Axboe9c60ce62007-03-15 09:14:47 +0100243{
Jens Axboedc873b62008-06-04 20:13:04 +0200244 unsigned long long ret, sized;
Jens Axboe1294c3e2011-05-11 08:15:18 +0200245 unsigned long r;
Jens Axboe9c60ce62007-03-15 09:14:47 +0100246
Jens Axboe4c07ad82011-03-28 09:51:09 +0200247 if (td->o.use_os_rand) {
248 r = os_random_long(&td->file_size_state);
249 sized = td->o.file_size_high - td->o.file_size_low;
250 ret = (unsigned long long) ((double) sized * (r / (OS_RAND_MAX + 1.0)));
251 } else {
252 r = __rand(&td->__file_size_state);
253 sized = td->o.file_size_high - td->o.file_size_low;
254 ret = (unsigned long long) ((double) sized * (r / (FRAND_MAX + 1.0)));
255 }
256
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100257 ret += td->o.file_size_low;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100258 ret -= (ret % td->o.rw_min_bs);
Jens Axboe9c60ce62007-03-15 09:14:47 +0100259 return ret;
260}
261
Jens Axboe53cdc682006-10-18 11:50:58 +0200262static int file_size(struct thread_data *td, struct fio_file *f)
263{
264 struct stat st;
265
Jens Axboedf9c26b2009-03-05 10:13:58 +0100266 if (stat(f->file_name, &st) == -1) {
Jens Axboe7bb48f82007-03-27 15:30:28 +0200267 td_verror(td, errno, "fstat");
268 return 1;
269 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200270
Jens Axboe7bb48f82007-03-27 15:30:28 +0200271 f->real_file_size = st.st_size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200272 return 0;
273}
274
275static int bdev_size(struct thread_data *td, struct fio_file *f)
276{
Bruce Cran9b836562011-01-08 19:49:54 +0100277 unsigned long long bytes = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200278 int r;
279
Jens Axboedf9c26b2009-03-05 10:13:58 +0100280 if (td->io_ops->open_file(td, f)) {
281 log_err("fio: failed opening blockdev %s for size check\n",
282 f->file_name);
283 return 1;
284 }
285
Bruce Cranecc314b2011-01-04 10:59:30 +0100286 r = blockdev_size(f, &bytes);
Jens Axboe53cdc682006-10-18 11:50:58 +0200287 if (r) {
Jens Axboee1161c32007-02-22 19:36:48 +0100288 td_verror(td, r, "blockdev_size");
Jens Axboedf9c26b2009-03-05 10:13:58 +0100289 goto err;
Jens Axboe53cdc682006-10-18 11:50:58 +0200290 }
291
Jens Axboe7ab077a2009-02-02 15:07:37 +0100292 if (!bytes) {
293 log_err("%s: zero sized block device?\n", f->file_name);
Jens Axboedf9c26b2009-03-05 10:13:58 +0100294 goto err;
Jens Axboe7ab077a2009-02-02 15:07:37 +0100295 }
296
Jens Axboe53cdc682006-10-18 11:50:58 +0200297 f->real_file_size = bytes;
Jens Axboe22a57ba2009-06-09 14:34:35 +0200298 td->io_ops->close_file(td, f);
Jens Axboe53cdc682006-10-18 11:50:58 +0200299 return 0;
Jens Axboedf9c26b2009-03-05 10:13:58 +0100300err:
301 td->io_ops->close_file(td, f);
302 return 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200303}
304
Jens Axboe4ccdccd2010-06-24 10:27:22 +0200305static int char_size(struct thread_data *td, struct fio_file *f)
306{
307#ifdef FIO_HAVE_CHARDEV_SIZE
Bruce Cran9b836562011-01-08 19:49:54 +0100308 unsigned long long bytes = 0;
Jens Axboe4ccdccd2010-06-24 10:27:22 +0200309 int r;
310
311 if (td->io_ops->open_file(td, f)) {
312 log_err("fio: failed opening blockdev %s for size check\n",
313 f->file_name);
314 return 1;
315 }
316
Bruce Cranecc314b2011-01-04 10:59:30 +0100317 r = chardev_size(f, &bytes);
Jens Axboe4ccdccd2010-06-24 10:27:22 +0200318 if (r) {
319 td_verror(td, r, "chardev_size");
320 goto err;
321 }
322
323 if (!bytes) {
324 log_err("%s: zero sized char device?\n", f->file_name);
325 goto err;
326 }
327
328 f->real_file_size = bytes;
329 td->io_ops->close_file(td, f);
330 return 0;
331err:
332 td->io_ops->close_file(td, f);
333 return 1;
334#else
335 f->real_file_size = -1ULL;
336 return 0;
337#endif
338}
339
Jens Axboe53cdc682006-10-18 11:50:58 +0200340static int get_file_size(struct thread_data *td, struct fio_file *f)
341{
342 int ret = 0;
343
Jens Axboed6aed792009-06-03 08:41:15 +0200344 if (fio_file_size_known(f))
Jens Axboe409b3412007-03-28 09:57:01 +0200345 return 0;
346
Jens Axboe7bb48f82007-03-27 15:30:28 +0200347 if (f->filetype == FIO_TYPE_FILE)
348 ret = file_size(td, f);
349 else if (f->filetype == FIO_TYPE_BD)
Jens Axboe53cdc682006-10-18 11:50:58 +0200350 ret = bdev_size(td, f);
Jens Axboe4ccdccd2010-06-24 10:27:22 +0200351 else if (f->filetype == FIO_TYPE_CHAR)
352 ret = char_size(td, f);
Jens Axboe53cdc682006-10-18 11:50:58 +0200353 else
354 f->real_file_size = -1;
355
356 if (ret)
357 return ret;
358
359 if (f->file_offset > f->real_file_size) {
Bruce Cran0f2152c2010-12-15 10:33:03 +0100360 log_err("%s: offset extends end (%llu > %llu)\n", td->o.name,
Jens Axboe4e0a8fa2013-04-15 11:40:57 +0200361 (unsigned long long) f->file_offset,
362 (unsigned long long) f->real_file_size);
Jens Axboe53cdc682006-10-18 11:50:58 +0200363 return 1;
364 }
365
Jens Axboed6aed792009-06-03 08:41:15 +0200366 fio_file_set_size_known(f);
Jens Axboe53cdc682006-10-18 11:50:58 +0200367 return 0;
368}
369
Jens Axboe3baddf22008-04-04 11:10:30 +0200370static int __file_invalidate_cache(struct thread_data *td, struct fio_file *f,
371 unsigned long long off,
372 unsigned long long len)
Jens Axboee5b401d2006-10-18 16:03:40 +0200373{
374 int ret = 0;
375
Jens Axboe5e0074c2009-06-02 21:40:09 +0200376 if (len == -1ULL)
Jens Axboe3baddf22008-04-04 11:10:30 +0200377 len = f->io_size;
378 if (off == -1ULL)
379 off = f->file_offset;
Jens Axboeee56ad52008-02-01 10:30:20 +0100380
Jens Axboe0d1cd202009-06-05 22:11:52 +0200381 if (len == -1ULL || off == -1ULL)
382 return 0;
383
Jens Axboe3baddf22008-04-04 11:10:30 +0200384 dprint(FD_IO, "invalidate cache %s: %llu/%llu\n", f->file_name, off,
385 len);
Jens Axboeb5af8292007-03-08 12:43:13 +0100386
Jens Axboee5b401d2006-10-18 16:03:40 +0200387 /*
388 * FIXME: add blockdev flushing too
389 */
Jens Axboea1c58072009-08-04 23:17:02 +0200390 if (f->mmap_ptr) {
Bruce Cran03e20d62011-01-02 20:14:54 +0100391 ret = posix_madvise(f->mmap_ptr, f->mmap_sz, POSIX_MADV_DONTNEED);
Jens Axboea1c58072009-08-04 23:17:02 +0200392#ifdef FIO_MADV_FREE
Jens Axboe3e10fb82013-08-09 14:31:06 -0600393 if (f->filetype == FIO_TYPE_BD)
394 (void) posix_madvise(f->mmap_ptr, f->mmap_sz, FIO_MADV_FREE);
Jens Axboea1c58072009-08-04 23:17:02 +0200395#endif
396 } else if (f->filetype == FIO_TYPE_FILE) {
Bruce Cranecc314b2011-01-04 10:59:30 +0100397 ret = posix_fadvise(f->fd, off, len, POSIX_FADV_DONTNEED);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100398 } else if (f->filetype == FIO_TYPE_BD) {
Bruce Cranecc314b2011-01-04 10:59:30 +0100399 ret = blockdev_invalidate_cache(f);
Jens Axboe7e0e25c2007-03-27 12:49:56 +0200400 if (ret < 0 && errno == EACCES && geteuid()) {
Jens Axboe7172cfe2007-07-23 14:36:00 +0200401 if (!root_warn) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100402 log_err("fio: only root may flush block "
403 "devices. Cache flush bypassed!\n");
Jens Axboe7172cfe2007-07-23 14:36:00 +0200404 root_warn = 1;
405 }
Jens Axboe7e0e25c2007-03-27 12:49:56 +0200406 ret = 0;
407 }
Jens Axboeb5605e92007-04-16 20:42:33 +0200408 } else if (f->filetype == FIO_TYPE_CHAR || f->filetype == FIO_TYPE_PIPE)
Jens Axboee5b401d2006-10-18 16:03:40 +0200409 ret = 0;
410
411 if (ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100412 td_verror(td, errno, "invalidate_cache");
Jens Axboee5b401d2006-10-18 16:03:40 +0200413 return 1;
Jens Axboe3baddf22008-04-04 11:10:30 +0200414 } else if (ret > 0) {
415 td_verror(td, ret, "invalidate_cache");
416 return 1;
Jens Axboee5b401d2006-10-18 16:03:40 +0200417 }
418
Jens Axboead2da602007-02-26 12:33:27 +0100419 return ret;
Jens Axboe3baddf22008-04-04 11:10:30 +0200420
421}
422
423int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
424{
Jens Axboed6aed792009-06-03 08:41:15 +0200425 if (!fio_file_open(f))
Jens Axboea5fb4612008-05-28 10:54:01 +0200426 return 0;
427
Jens Axboe5e0074c2009-06-02 21:40:09 +0200428 return __file_invalidate_cache(td, f, -1ULL, -1ULL);
Jens Axboee5b401d2006-10-18 16:03:40 +0200429}
430
Jens Axboe6977bcd2008-03-01 15:55:36 +0100431int generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
Jens Axboe53cdc682006-10-18 11:50:58 +0200432{
Jens Axboe6977bcd2008-03-01 15:55:36 +0100433 int ret = 0;
434
Jens Axboeee56ad52008-02-01 10:30:20 +0100435 dprint(FD_FILE, "fd close %s\n", f->file_name);
Jens Axboe4906e0b2008-03-01 18:59:51 +0100436
437 remove_file_hash(f);
438
Jens Axboe6977bcd2008-03-01 15:55:36 +0100439 if (close(f->fd) < 0)
440 ret = errno;
441
Jens Axboeb5af8292007-03-08 12:43:13 +0100442 f->fd = -1;
Jens Axboee6c4d732012-12-12 08:16:27 +0100443
444 if (f->shadow_fd != -1) {
445 close(f->shadow_fd);
446 f->shadow_fd = -1;
447 }
448
Juan Casse57e54e02013-09-20 09:20:52 -0600449 f->engine_data = 0;
Jens Axboe6977bcd2008-03-01 15:55:36 +0100450 return ret;
Jens Axboe53cdc682006-10-18 11:50:58 +0200451}
452
Dmitry Monakhov1ccc6dc2012-09-19 23:22:53 +0400453int file_lookup_open(struct fio_file *f, int flags)
Jens Axboe53cdc682006-10-18 11:50:58 +0200454{
Jens Axboe29c13492008-03-01 19:25:20 +0100455 struct fio_file *__f;
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100456 int from_hash;
457
458 __f = lookup_file_hash(f->file_name);
459 if (__f) {
Jens Axboe9efef3c2008-03-06 10:26:25 +0100460 dprint(FD_FILE, "found file in hash %s\n", f->file_name);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100461 /*
462 * racy, need the __f->lock locked
463 */
464 f->lock = __f->lock;
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100465 from_hash = 1;
466 } else {
Jens Axboe9efef3c2008-03-06 10:26:25 +0100467 dprint(FD_FILE, "file not found in hash %s\n", f->file_name);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100468 from_hash = 0;
469 }
470
Jens Axboee8670ef2008-03-06 12:06:30 +0100471 f->fd = open(f->file_name, flags, 0600);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100472 return from_hash;
473}
474
Jens Axboee6c4d732012-12-12 08:16:27 +0100475static int file_close_shadow_fds(struct thread_data *td)
476{
477 struct fio_file *f;
478 int num_closed = 0;
479 unsigned int i;
480
481 for_each_file(td, f, i) {
482 if (f->shadow_fd == -1)
483 continue;
484
485 close(f->shadow_fd);
486 f->shadow_fd = -1;
487 num_closed++;
488 }
489
490 return num_closed;
491}
492
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100493int generic_open_file(struct thread_data *td, struct fio_file *f)
494{
Jens Axboe66159822007-04-16 21:54:24 +0200495 int is_std = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200496 int flags = 0;
Jens Axboe29c13492008-03-01 19:25:20 +0100497 int from_hash = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200498
Jens Axboeee56ad52008-02-01 10:30:20 +0100499 dprint(FD_FILE, "fd open %s\n", f->file_name);
500
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200501 if (td_trim(td) && f->filetype != FIO_TYPE_BD) {
502 log_err("fio: trim only applies to block device\n");
503 return 1;
504 }
505
Jens Axboe66159822007-04-16 21:54:24 +0200506 if (!strcmp(f->file_name, "-")) {
507 if (td_rw(td)) {
508 log_err("fio: can't read/write to stdin/out\n");
509 return 1;
510 }
511 is_std = 1;
Jens Axboece98fa62007-04-17 13:27:32 +0200512
513 /*
514 * move output logging to stderr, if we are writing to stdout
515 */
516 if (td_write(td))
517 f_out = stderr;
Jens Axboe66159822007-04-16 21:54:24 +0200518 }
519
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200520 if (td_trim(td))
521 goto skip_flags;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100522 if (td->o.odirect)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100523 flags |= OS_O_DIRECT;
Chris Masond01612f2013-11-15 15:52:58 -0700524 if (td->o.oatomic) {
525 if (!FIO_O_ATOMIC) {
526 td_verror(td, EINVAL, "OS does not support atomic IO");
527 return 1;
528 }
529 flags |= OS_O_DIRECT | FIO_O_ATOMIC;
530 }
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100531 if (td->o.sync_io)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100532 flags |= O_SYNC;
Jens Axboe814452b2009-03-04 12:53:13 +0100533 if (td->o.create_on_open)
534 flags |= O_CREAT;
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200535skip_flags:
536 if (f->filetype != FIO_TYPE_FILE)
537 flags |= FIO_O_NOATIME;
Jens Axboe7abf8332006-11-23 15:01:19 +0100538
Aaron Carroll056f3452007-11-26 09:08:53 +0100539open_again:
Jens Axboe660a1cb2007-04-17 15:37:47 +0200540 if (td_write(td)) {
Jens Axboe17308152008-03-07 13:42:31 +0100541 if (!read_only)
542 flags |= O_RDWR;
Jens Axboe53cdc682006-10-18 11:50:58 +0200543
Jens Axboeaf52b342007-03-13 10:07:47 +0100544 if (f->filetype == FIO_TYPE_FILE)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100545 flags |= O_CREAT;
Jens Axboe2fd233b2007-03-02 08:44:25 +0100546
Jens Axboe66159822007-04-16 21:54:24 +0200547 if (is_std)
548 f->fd = dup(STDOUT_FILENO);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100549 else
550 from_hash = file_lookup_open(f, flags);
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200551 } else if (td_read(td)) {
Jens Axboe4241ea82007-09-12 08:18:36 +0200552 if (f->filetype == FIO_TYPE_CHAR && !read_only)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100553 flags |= O_RDWR;
554 else
555 flags |= O_RDONLY;
556
Jens Axboe66159822007-04-16 21:54:24 +0200557 if (is_std)
558 f->fd = dup(STDIN_FILENO);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100559 else
560 from_hash = file_lookup_open(f, flags);
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200561 } else { //td trim
562 flags |= O_RDWR;
563 from_hash = file_lookup_open(f, flags);
Jens Axboe53cdc682006-10-18 11:50:58 +0200564 }
565
566 if (f->fd == -1) {
Jens Axboee4e33252007-03-21 13:07:54 +0100567 char buf[FIO_VERROR_SIZE];
Jens Axboee1161c32007-02-22 19:36:48 +0100568 int __e = errno;
569
Jens Axboe835d9b92009-06-09 15:23:38 +0200570 if (__e == EPERM && (flags & FIO_O_NOATIME)) {
Jens Axboe5921e802008-05-30 15:02:38 +0200571 flags &= ~FIO_O_NOATIME;
Aaron Carroll056f3452007-11-26 09:08:53 +0100572 goto open_again;
573 }
Jens Axboee6c4d732012-12-12 08:16:27 +0100574 if (__e == EMFILE && file_close_shadow_fds(td))
575 goto open_again;
Aaron Carroll056f3452007-11-26 09:08:53 +0100576
Ken Raeburn98ffb8f2013-01-30 22:31:09 +0100577 snprintf(buf, sizeof(buf), "open(%s)", f->file_name);
Jens Axboee4e33252007-03-21 13:07:54 +0100578
Jens Axboea93c5f02012-06-11 08:53:53 +0200579 if (__e == EINVAL && (flags & OS_O_DIRECT)) {
580 log_err("fio: looks like your file system does not " \
581 "support direct=1/buffered=0\n");
582 }
583
Jens Axboee4e33252007-03-21 13:07:54 +0100584 td_verror(td, __e, buf);
Jens Axboe53cdc682006-10-18 11:50:58 +0200585 }
586
Jens Axboe29c13492008-03-01 19:25:20 +0100587 if (!from_hash && f->fd != -1) {
588 if (add_file_hash(f)) {
Jens Axboe3f0ca9b2011-05-24 21:36:24 +0200589 int fio_unused ret;
Jens Axboe29c13492008-03-01 19:25:20 +0100590
591 /*
Jens Axboee6c4d732012-12-12 08:16:27 +0100592 * Stash away descriptor for later close. This is to
593 * work-around a "feature" on Linux, where a close of
594 * an fd that has been opened for write will trigger
595 * udev to call blkid to check partitions, fs id, etc.
Anatol Pomozovde8f6de2013-09-26 16:31:34 -0700596 * That pollutes the device cache, which can slow down
Jens Axboee6c4d732012-12-12 08:16:27 +0100597 * unbuffered accesses.
Jens Axboe29c13492008-03-01 19:25:20 +0100598 */
Jens Axboee6c4d732012-12-12 08:16:27 +0100599 if (f->shadow_fd == -1)
600 f->shadow_fd = f->fd;
601 else {
602 /*
603 * OK to ignore, we haven't done anything
604 * with it
605 */
606 ret = generic_close_file(td, f);
607 }
Jens Axboe29c13492008-03-01 19:25:20 +0100608 goto open_again;
609 }
610 }
Jens Axboe4906e0b2008-03-01 18:59:51 +0100611
Jens Axboe53cdc682006-10-18 11:50:58 +0200612 return 0;
Jens Axboeb5af8292007-03-08 12:43:13 +0100613}
614
Jens Axboedf9c26b2009-03-05 10:13:58 +0100615int generic_get_file_size(struct thread_data *td, struct fio_file *f)
Jens Axboe21972cd2006-11-23 12:39:16 +0100616{
Jens Axboedf9c26b2009-03-05 10:13:58 +0100617 return get_file_size(td, f);
Jens Axboe21972cd2006-11-23 12:39:16 +0100618}
619
Jens Axboe7bb48f82007-03-27 15:30:28 +0200620/*
621 * open/close all files, so that ->real_file_size gets set
622 */
Jens Axboebab3fd52007-04-02 10:34:18 +0200623static int get_file_sizes(struct thread_data *td)
Jens Axboe53cdc682006-10-18 11:50:58 +0200624{
625 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100626 unsigned int i;
Jens Axboebab3fd52007-04-02 10:34:18 +0200627 int err = 0;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200628
629 for_each_file(td, f, i) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100630 dprint(FD_FILE, "get file size for %p/%d/%p\n", f, i,
631 f->file_name);
Jens Axboe9efef3c2008-03-06 10:26:25 +0100632
Jens Axboe99a47c62009-04-07 22:20:56 +0200633 if (td_io_get_file_size(td, f)) {
Jens Axboe40b44f42007-04-11 12:43:35 +0200634 if (td->error != ENOENT) {
635 log_err("%s\n", td->verror);
636 err = 1;
637 }
Jens Axboe541d66d2007-03-27 19:50:11 +0200638 clear_error(td);
Jens Axboe07eb79d2007-04-12 13:02:38 +0200639 }
Jens Axboe409b3412007-03-28 09:57:01 +0200640
641 if (f->real_file_size == -1ULL && td->o.size)
642 f->real_file_size = td->o.size / td->o.nr_files;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200643 }
Jens Axboebab3fd52007-04-02 10:34:18 +0200644
645 return err;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200646}
647
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200648struct fio_mount {
649 struct flist_head list;
650 const char *base;
651 char __base[256];
652 unsigned int key;
653};
654
655/*
656 * Get free number of bytes for each file on each unique mount.
657 */
658static unsigned long long get_fs_free_counts(struct thread_data *td)
659{
660 struct flist_head *n, *tmp;
Jens Axboe68b03162010-06-24 09:22:41 +0200661 unsigned long long ret = 0;
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200662 struct fio_mount *fm;
663 FLIST_HEAD(list);
664 struct fio_file *f;
665 unsigned int i;
666
667 for_each_file(td, f, i) {
668 struct stat sb;
669 char buf[256];
670
Jens Axboe4ccdccd2010-06-24 10:27:22 +0200671 if (f->filetype == FIO_TYPE_BD || f->filetype == FIO_TYPE_CHAR) {
672 if (f->real_file_size != -1ULL)
673 ret += f->real_file_size;
Jens Axboe68b03162010-06-24 09:22:41 +0200674 continue;
675 } else if (f->filetype != FIO_TYPE_FILE)
676 continue;
677
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200678 strcpy(buf, f->file_name);
679
680 if (stat(buf, &sb) < 0) {
681 if (errno != ENOENT)
682 break;
683 strcpy(buf, ".");
684 if (stat(buf, &sb) < 0)
685 break;
686 }
687
688 fm = NULL;
689 flist_for_each(n, &list) {
690 fm = flist_entry(n, struct fio_mount, list);
691 if (fm->key == sb.st_dev)
692 break;
693
694 fm = NULL;
695 }
696
697 if (fm)
698 continue;
699
700 fm = malloc(sizeof(*fm));
701 strcpy(fm->__base, buf);
702 fm->base = basename(fm->__base);
703 fm->key = sb.st_dev;
704 flist_add(&fm->list, &list);
705 }
706
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200707 flist_for_each_safe(n, tmp, &list) {
708 unsigned long long sz;
709
710 fm = flist_entry(n, struct fio_mount, list);
711 flist_del(&fm->list);
712
713 sz = get_fs_size(fm->base);
714 if (sz && sz != -1ULL)
715 ret += sz;
716
717 free(fm);
718 }
719
720 return ret;
721}
722
Jens Axboe293b8c12013-01-04 13:16:54 +0100723uint64_t get_start_offset(struct thread_data *td)
Dan Ehrenbergce95d652012-08-16 08:58:21 +0200724{
725 return td->o.start_offset +
726 (td->thread_number - 1) * td->o.offset_increment;
727}
728
Jens Axboe7bb48f82007-03-27 15:30:28 +0200729/*
730 * Open the files and setup files sizes, creating files if necessary.
731 */
732int setup_files(struct thread_data *td)
733{
734 unsigned long long total_size, extend_size;
Jens Axboede98bd32013-04-05 11:09:20 +0200735 struct thread_options *o = &td->o;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200736 struct fio_file *f;
737 unsigned int i;
Jens Axboe000b0802007-03-27 15:56:10 +0200738 int err = 0, need_extend;
Jens Axboee90391e2013-04-13 20:40:53 +0200739 int old_state;
Jens Axboe53cdc682006-10-18 11:50:58 +0200740
Jens Axboeee56ad52008-02-01 10:30:20 +0100741 dprint(FD_FILE, "setup files\n");
742
Jens Axboee90391e2013-04-13 20:40:53 +0200743 old_state = td->runstate;
744 td_set_runstate(td, TD_SETTING_UP);
745
Jens Axboede98bd32013-04-05 11:09:20 +0200746 if (o->read_iolog_file)
Jens Axboe25460cf2012-05-02 13:58:02 +0200747 goto done;
Jens Axboe691c8fb2008-03-07 14:26:26 +0100748
Jens Axboe53cdc682006-10-18 11:50:58 +0200749 /*
750 * if ioengine defines a setup() method, it's responsible for
Jens Axboe7bb48f82007-03-27 15:30:28 +0200751 * opening the files and setting f->real_file_size to indicate
752 * the valid range for that file.
Jens Axboe53cdc682006-10-18 11:50:58 +0200753 */
754 if (td->io_ops->setup)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200755 err = td->io_ops->setup(td);
756 else
Jens Axboebab3fd52007-04-02 10:34:18 +0200757 err = get_file_sizes(td);
Jens Axboe53cdc682006-10-18 11:50:58 +0200758
Jens Axboef1027062006-10-20 10:14:01 +0200759 if (err)
Jens Axboee90391e2013-04-13 20:40:53 +0200760 goto err_out;
Jens Axboef1027062006-10-20 10:14:01 +0200761
Jens Axboe0a7eb122006-10-20 10:36:42 +0200762 /*
Jens Axboe7bb48f82007-03-27 15:30:28 +0200763 * check sizes. if the files/devices do not exist and the size
764 * isn't passed to fio, abort.
Jens Axboe0a7eb122006-10-20 10:36:42 +0200765 */
Jens Axboe7bb48f82007-03-27 15:30:28 +0200766 total_size = 0;
767 for_each_file(td, f, i) {
768 if (f->real_file_size == -1ULL)
769 total_size = -1ULL;
770 else
771 total_size += f->real_file_size;
772 }
Jens Axboe0a7eb122006-10-20 10:36:42 +0200773
Jens Axboede98bd32013-04-05 11:09:20 +0200774 if (o->fill_device)
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200775 td->fill_device_size = get_fs_free_counts(td);
776
Jens Axboe7bb48f82007-03-27 15:30:28 +0200777 /*
778 * device/file sizes are zero and no size given, punt
779 */
Jens Axboede98bd32013-04-05 11:09:20 +0200780 if ((!total_size || total_size == -1ULL) && !o->size &&
781 !(td->io_ops->flags & FIO_NOIO) && !o->fill_device &&
782 !(o->nr_files && (o->file_size_low || o->file_size_high))) {
783 log_err("%s: you need to specify size=\n", o->name);
Jens Axboee1161c32007-02-22 19:36:48 +0100784 td_verror(td, EINVAL, "total_file_size");
Jens Axboee90391e2013-04-13 20:40:53 +0200785 goto err_out;
Jens Axboe53cdc682006-10-18 11:50:58 +0200786 }
787
Jens Axboe7bb48f82007-03-27 15:30:28 +0200788 /*
789 * now file sizes are known, so we can set ->io_size. if size= is
790 * not given, ->io_size is just equal to ->real_file_size. if size
791 * is given, ->io_size is size / nr_files.
792 */
793 extend_size = total_size = 0;
794 need_extend = 0;
795 for_each_file(td, f, i) {
Dan Ehrenbergce95d652012-08-16 08:58:21 +0200796 f->file_offset = get_start_offset(td);
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200797
Jens Axboede98bd32013-04-05 11:09:20 +0200798 if (!o->file_size_low) {
Jens Axboe7bb48f82007-03-27 15:30:28 +0200799 /*
800 * no file size range given, file size is equal to
801 * total size divided by number of files. if that is
802 * zero, set it to the real file size.
803 */
Jens Axboede98bd32013-04-05 11:09:20 +0200804 f->io_size = o->size / o->nr_files;
Jens Axboe65bdb102008-01-24 13:13:12 +0100805 if (!f->io_size)
Jens Axboe273f8c92008-01-25 14:02:15 +0100806 f->io_size = f->real_file_size - f->file_offset;
Jens Axboede98bd32013-04-05 11:09:20 +0200807 } else if (f->real_file_size < o->file_size_low ||
808 f->real_file_size > o->file_size_high) {
809 if (f->file_offset > o->file_size_low)
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200810 goto err_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200811 /*
812 * file size given. if it's fixed, use that. if it's a
813 * range, generate a random size in-between.
814 */
Jens Axboede98bd32013-04-05 11:09:20 +0200815 if (o->file_size_low == o->file_size_high)
816 f->io_size = o->file_size_low - f->file_offset;
817 else {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100818 f->io_size = get_rand_file_size(td)
819 - f->file_offset;
820 }
Jens Axboe65bdb102008-01-24 13:13:12 +0100821 } else
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200822 f->io_size = f->real_file_size - f->file_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200823
824 if (f->io_size == -1ULL)
825 total_size = -1ULL;
Shaohua Li4d002562012-09-21 08:32:32 +0200826 else {
Jens Axboede98bd32013-04-05 11:09:20 +0200827 if (o->size_percent)
828 f->io_size = (f->io_size * o->size_percent) / 100;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200829 total_size += f->io_size;
Shaohua Li4d002562012-09-21 08:32:32 +0200830 }
Jens Axboe7bb48f82007-03-27 15:30:28 +0200831
832 if (f->filetype == FIO_TYPE_FILE &&
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200833 (f->io_size + f->file_offset) > f->real_file_size &&
Jens Axboe7bb48f82007-03-27 15:30:28 +0200834 !(td->io_ops->flags & FIO_DISKLESSIO)) {
Jens Axboede98bd32013-04-05 11:09:20 +0200835 if (!o->create_on_open) {
Jens Axboe814452b2009-03-04 12:53:13 +0100836 need_extend++;
837 extend_size += (f->io_size + f->file_offset);
838 } else
839 f->real_file_size = f->io_size + f->file_offset;
Jens Axboed6aed792009-06-03 08:41:15 +0200840 fio_file_set_extend(f);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100841 }
Jens Axboe7bb48f82007-03-27 15:30:28 +0200842 }
843
Jens Axboede98bd32013-04-05 11:09:20 +0200844 if (!o->size || o->size > total_size)
845 o->size = total_size;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200846
847 /*
848 * See if we need to extend some files
849 */
850 if (need_extend) {
851 temp_stall_ts = 1;
Jens Axboef3afa572012-09-17 13:34:16 +0200852 if (output_format == FIO_OUTPUT_NORMAL)
Shaozhi Shawn Yea7ba8c52008-09-10 09:04:18 +0200853 log_info("%s: Laying out IO file(s) (%u file(s) /"
Jens Axboede98bd32013-04-05 11:09:20 +0200854 " %lluMB)\n", o->name, need_extend,
Shaozhi Shawn Yea7ba8c52008-09-10 09:04:18 +0200855 extend_size >> 20);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200856
857 for_each_file(td, f, i) {
Jens Axboe5e0074c2009-06-02 21:40:09 +0200858 unsigned long long old_len = -1ULL, extend_len = -1ULL;
Jens Axboe3baddf22008-04-04 11:10:30 +0200859
Jens Axboed6aed792009-06-03 08:41:15 +0200860 if (!fio_file_extend(f))
Jens Axboe7bb48f82007-03-27 15:30:28 +0200861 continue;
862
Jens Axboe409b3412007-03-28 09:57:01 +0200863 assert(f->filetype == FIO_TYPE_FILE);
Jens Axboed6aed792009-06-03 08:41:15 +0200864 fio_file_clear_extend(f);
Jens Axboede98bd32013-04-05 11:09:20 +0200865 if (!o->fill_device) {
Jens Axboe5e0074c2009-06-02 21:40:09 +0200866 old_len = f->real_file_size;
Jens Axboe0b9d69e2009-09-11 22:29:54 +0200867 extend_len = f->io_size + f->file_offset -
868 old_len;
Jens Axboe5e0074c2009-06-02 21:40:09 +0200869 }
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200870 f->real_file_size = (f->io_size + f->file_offset);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200871 err = extend_file(td, f);
872 if (err)
873 break;
Jens Axboe5e0074c2009-06-02 21:40:09 +0200874
Jens Axboe3baddf22008-04-04 11:10:30 +0200875 err = __file_invalidate_cache(td, f, old_len,
876 extend_len);
877 close(f->fd);
878 f->fd = -1;
879 if (err)
880 break;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200881 }
882 temp_stall_ts = 0;
883 }
884
885 if (err)
Jens Axboee90391e2013-04-13 20:40:53 +0200886 goto err_out;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200887
Jens Axboede98bd32013-04-05 11:09:20 +0200888 if (!o->zone_size)
889 o->zone_size = o->size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200890
Jens Axboeea966f82007-09-03 10:09:37 +0200891 /*
892 * iolog already set the total io size, if we read back
893 * stored entries.
894 */
Jens Axboede98bd32013-04-05 11:09:20 +0200895 if (!o->read_iolog_file)
896 td->total_io_size = o->size * o->loops;
Jens Axboe25460cf2012-05-02 13:58:02 +0200897
898done:
Jens Axboede98bd32013-04-05 11:09:20 +0200899 if (o->create_only)
Jens Axboe25460cf2012-05-02 13:58:02 +0200900 td->done = 1;
901
Jens Axboee90391e2013-04-13 20:40:53 +0200902 td_set_runstate(td, old_state);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200903 return 0;
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200904err_offset:
Jens Axboede98bd32013-04-05 11:09:20 +0200905 log_err("%s: you need to specify valid offset=\n", o->name);
Jens Axboee90391e2013-04-13 20:40:53 +0200906err_out:
907 td_set_runstate(td, old_state);
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200908 return 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200909}
910
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200911int pre_read_files(struct thread_data *td)
912{
913 struct fio_file *f;
914 unsigned int i;
915
916 dprint(FD_FILE, "pre_read files\n");
917
918 for_each_file(td, f, i) {
919 pre_read_file(td, f);
920 }
921
922 return 1;
923}
924
Jens Axboe9c6f6312012-11-07 09:15:45 +0100925static int __init_rand_distribution(struct thread_data *td, struct fio_file *f)
926{
Jens Axboe23162962012-11-07 19:47:47 +0100927 unsigned int range_size, seed;
Jens Axboe9c6f6312012-11-07 09:15:45 +0100928 unsigned long nranges;
Jens Axboe21415db2013-01-04 13:09:29 +0100929 uint64_t file_size;
Jens Axboe9c6f6312012-11-07 09:15:45 +0100930
931 range_size = min(td->o.min_bs[DDIR_READ], td->o.min_bs[DDIR_WRITE]);
Jens Axboe21415db2013-01-04 13:09:29 +0100932 file_size = min(f->real_file_size, f->io_size);
Jens Axboe9c6f6312012-11-07 09:15:45 +0100933
Jens Axboe21415db2013-01-04 13:09:29 +0100934 nranges = (file_size + range_size - 1) / range_size;
Jens Axboe9c6f6312012-11-07 09:15:45 +0100935
Jens Axboe23162962012-11-07 19:47:47 +0100936 seed = jhash(f->file_name, strlen(f->file_name), 0) * td->thread_number;
Jens Axboe84256872012-11-17 10:06:36 -0700937 if (!td->o.rand_repeatable)
938 seed = td->rand_seeds[4];
939
Jens Axboe9c6f6312012-11-07 09:15:45 +0100940 if (td->o.random_distribution == FIO_RAND_DIST_ZIPF)
Jens Axboe888677a2013-04-10 22:19:46 +0200941 zipf_init(&f->zipf, nranges, td->o.zipf_theta.u.f, seed);
Jens Axboe9c6f6312012-11-07 09:15:45 +0100942 else
Jens Axboe888677a2013-04-10 22:19:46 +0200943 pareto_init(&f->zipf, nranges, td->o.pareto_h.u.f, seed);
Jens Axboe9c6f6312012-11-07 09:15:45 +0100944
945 return 1;
946}
947
948static int init_rand_distribution(struct thread_data *td)
949{
950 struct fio_file *f;
951 unsigned int i;
952 int state;
953
954 if (td->o.random_distribution == FIO_RAND_DIST_RANDOM)
955 return 0;
956
957 state = td->runstate;
958 td_set_runstate(td, TD_SETTING_UP);
959 for_each_file(td, f, i)
960 __init_rand_distribution(td, f);
961 td_set_runstate(td, state);
962
963 return 1;
964}
965
Jens Axboe68727072007-03-15 20:49:25 +0100966int init_random_map(struct thread_data *td)
967{
Jens Axboe51ede0b2012-11-22 13:50:29 +0100968 unsigned long long blocks;
Jens Axboe68727072007-03-15 20:49:25 +0100969 struct fio_file *f;
970 unsigned int i;
971
Jens Axboe9c6f6312012-11-07 09:15:45 +0100972 if (init_rand_distribution(td))
973 return 0;
Jens Axboe3831a842012-11-26 19:59:14 +0100974 if (!td_random(td))
Jens Axboe68727072007-03-15 20:49:25 +0100975 return 0;
976
977 for_each_file(td, f, i) {
Jens Axboe38f30c82013-01-11 14:03:05 +0100978 uint64_t file_size = min(f->real_file_size, f->io_size);
979
Jens Axboe53737ae2013-02-21 15:18:17 +0100980 blocks = file_size / (unsigned long long) td->o.rw_min_bs;
981
Jens Axboe8055e412012-11-26 08:43:47 +0100982 if (td->o.random_generator == FIO_RAND_GEN_LFSR) {
Jens Axboe82af46b2012-12-04 13:12:25 +0100983 unsigned long seed;
984
985 seed = td->rand_seeds[FIO_RAND_BLOCK_OFF];
986
Alex Pyrgiotisd474cbc2013-03-08 14:37:03 +0200987 if (!lfsr_init(&f->lfsr, blocks, seed, seed & 0xF))
Jens Axboe8055e412012-11-26 08:43:47 +0100988 continue;
Jens Axboe3831a842012-11-26 19:59:14 +0100989 } else if (!td->o.norandommap) {
Jens Axboe7ebd7962012-11-28 21:24:46 +0100990 f->io_axmap = axmap_new(blocks);
991 if (f->io_axmap)
Jens Axboe8055e412012-11-26 08:43:47 +0100992 continue;
Jens Axboe1cad7122012-11-29 21:37:11 +0100993 } else if (td->o.norandommap)
994 continue;
Jens Axboeceadd592012-02-02 08:41:28 +0100995
Jens Axboe2b386d22008-03-26 10:32:57 +0100996 if (!td->o.softrandommap) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100997 log_err("fio: failed allocating random map. If running"
998 " a large number of jobs, try the 'norandommap'"
Jens Axboe2b386d22008-03-26 10:32:57 +0100999 " option or set 'softrandommap'. Or give"
1000 " a larger --alloc-size to fio.\n");
Jens Axboe68727072007-03-15 20:49:25 +01001001 return 1;
1002 }
Jens Axboe303032a2008-03-26 10:11:10 +01001003
1004 log_info("fio: file %s failed allocating random map. Running "
1005 "job without.\n", f->file_name);
Jens Axboe68727072007-03-15 20:49:25 +01001006 }
1007
1008 return 0;
1009}
1010
Jens Axboe53cdc682006-10-18 11:50:58 +02001011void close_files(struct thread_data *td)
1012{
Jens Axboe0ab8db82006-10-18 17:16:23 +02001013 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +01001014 unsigned int i;
Jens Axboe53cdc682006-10-18 11:50:58 +02001015
Jens Axboe2be3eec2009-06-10 09:05:13 +02001016 for_each_file(td, f, i) {
1017 if (fio_file_open(f))
1018 td_io_close_file(td, f);
1019 }
Jens Axboe24ffd2c2008-03-01 15:47:08 +01001020}
1021
1022void close_and_free_files(struct thread_data *td)
1023{
1024 struct fio_file *f;
1025 unsigned int i;
1026
Jens Axboeee56ad52008-02-01 10:30:20 +01001027 dprint(FD_FILE, "close files\n");
1028
Jens Axboe0ab8db82006-10-18 17:16:23 +02001029 for_each_file(td, f, i) {
Jens Axboeffdfabd2008-03-26 09:18:14 +01001030 if (td->o.unlink && f->filetype == FIO_TYPE_FILE) {
1031 dprint(FD_FILE, "free unlink %s\n", f->file_name);
Jens Axboe132ad462006-11-23 12:23:12 +01001032 unlink(f->file_name);
Jens Axboeffdfabd2008-03-26 09:18:14 +01001033 }
Jens Axboebdb4e2e2007-03-01 09:54:57 +01001034
Jens Axboe22a57ba2009-06-09 14:34:35 +02001035 if (fio_file_open(f))
1036 td_io_close_file(td, f);
1037
Shaozhi Shawn Yeb9fbcf22008-09-10 09:05:17 +02001038 remove_file_hash(f);
Jens Axboeb3dc7f02007-03-09 12:52:15 +01001039
Jens Axboef17c4392008-03-01 18:40:46 +01001040 sfree(f->file_name);
Jens Axboefa1da862007-03-23 15:20:54 +01001041 f->file_name = NULL;
Jens Axboe7ebd7962012-11-28 21:24:46 +01001042 axmap_free(f->io_axmap);
1043 f->io_axmap = NULL;
Jens Axboe78d99e62008-03-01 18:41:50 +01001044 sfree(f);
Jens Axboe53cdc682006-10-18 11:50:58 +02001045 }
Jens Axboeb4a6a592006-10-20 13:54:47 +02001046
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001047 td->o.filename = NULL;
Jens Axboecade3ef2007-03-23 15:29:45 +01001048 free(td->files);
Jens Axboed7df1d12013-03-20 19:57:01 -06001049 free(td->file_locks);
Jens Axboe9efef3c2008-03-06 10:26:25 +01001050 td->files_index = 0;
Jens Axboeb4a6a592006-10-20 13:54:47 +02001051 td->files = NULL;
Jens Axboed7df1d12013-03-20 19:57:01 -06001052 td->file_locks = NULL;
Jens Axboe27ddbfa2014-01-22 16:27:32 -08001053 td->o.file_lock_mode = FILE_LOCK_NONE;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001054 td->o.nr_files = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +02001055}
Jens Axboeaf52b342007-03-13 10:07:47 +01001056
Jens Axboee3bab462007-03-13 11:22:09 +01001057static void get_file_type(struct fio_file *f)
Jens Axboeaf52b342007-03-13 10:07:47 +01001058{
1059 struct stat sb;
1060
Jens Axboe66159822007-04-16 21:54:24 +02001061 if (!strcmp(f->file_name, "-"))
1062 f->filetype = FIO_TYPE_PIPE;
1063 else
1064 f->filetype = FIO_TYPE_FILE;
Jens Axboeaf52b342007-03-13 10:07:47 +01001065
Bruce Cran38921822012-02-22 17:55:16 +00001066 /* \\.\ is the device namespace in Windows, where every file is
1067 * a block device */
1068 if (strncmp(f->file_name, "\\\\.\\", 4) == 0)
1069 f->filetype = FIO_TYPE_BD;
1070
Jens Axboeb30d3952010-02-06 23:36:26 +01001071 if (!stat(f->file_name, &sb)) {
Bruce Cran38921822012-02-22 17:55:16 +00001072 if (S_ISBLK(sb.st_mode))
Jens Axboeaf52b342007-03-13 10:07:47 +01001073 f->filetype = FIO_TYPE_BD;
1074 else if (S_ISCHR(sb.st_mode))
1075 f->filetype = FIO_TYPE_CHAR;
Jens Axboeb5605e92007-04-16 20:42:33 +02001076 else if (S_ISFIFO(sb.st_mode))
1077 f->filetype = FIO_TYPE_PIPE;
Jens Axboeaf52b342007-03-13 10:07:47 +01001078 }
1079}
1080
Jens Axboef29b25a2007-07-23 08:56:43 +02001081int add_file(struct thread_data *td, const char *fname)
Jens Axboeaf52b342007-03-13 10:07:47 +01001082{
Jens Axboe7b4e4fe2007-03-13 12:51:40 +01001083 int cur_files = td->files_index;
Jens Axboebd0ee742007-03-23 14:26:23 +01001084 char file_name[PATH_MAX];
Jens Axboeaf52b342007-03-13 10:07:47 +01001085 struct fio_file *f;
Jens Axboebd0ee742007-03-23 14:26:23 +01001086 int len = 0;
Jens Axboeaf52b342007-03-13 10:07:47 +01001087
Jens Axboeee56ad52008-02-01 10:30:20 +01001088 dprint(FD_FILE, "add file %s\n", fname);
1089
Jens Axboef17c4392008-03-01 18:40:46 +01001090 f = smalloc(sizeof(*f));
Jens Axboec48c0be2008-07-31 19:55:02 +02001091 if (!f) {
1092 log_err("fio: smalloc OOM\n");
1093 assert(0);
1094 }
Jens Axboe0b9d69e2009-09-11 22:29:54 +02001095
Jens Axboeaf52b342007-03-13 10:07:47 +01001096 f->fd = -1;
Jens Axboee6c4d732012-12-12 08:16:27 +01001097 f->shadow_fd = -1;
Jens Axboe33c48812013-01-21 09:46:06 -07001098 fio_file_reset(td, f);
Jens Axboebd0ee742007-03-23 14:26:23 +01001099
Jens Axboefc99bc02009-03-04 15:27:42 +01001100 if (td->files_size <= td->files_index) {
Jianpeng Ma1983e322013-01-10 13:19:27 +01001101 unsigned int new_size = td->o.nr_files + 1;
Jens Axboe126d65c2008-03-01 18:04:31 +01001102
Jens Axboefc99bc02009-03-04 15:27:42 +01001103 dprint(FD_FILE, "resize file array to %d files\n", new_size);
1104
1105 td->files = realloc(td->files, new_size * sizeof(f));
Jianpeng Mad537c082013-01-11 08:52:55 +01001106 if (td->files == NULL) {
1107 log_err("fio: realloc OOM\n");
1108 assert(0);
1109 }
Jens Axboed7df1d12013-03-20 19:57:01 -06001110 if (td->o.file_lock_mode != FILE_LOCK_NONE) {
1111 td->file_locks = realloc(td->file_locks, new_size);
1112 if (!td->file_locks) {
1113 log_err("fio: realloc OOM\n");
1114 assert(0);
1115 }
1116 td->file_locks[cur_files] = FILE_LOCK_NONE;
1117 }
Jens Axboefc99bc02009-03-04 15:27:42 +01001118 td->files_size = new_size;
1119 }
Jens Axboe8bb76792009-03-04 15:37:52 +01001120 td->files[cur_files] = f;
Shaohua Li89ac1d42012-06-11 08:56:32 +02001121 f->fileno = cur_files;
Jens Axboe126d65c2008-03-01 18:04:31 +01001122
Jens Axboe07eb79d2007-04-12 13:02:38 +02001123 /*
1124 * init function, io engine may not be loaded yet
1125 */
1126 if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO))
1127 f->real_file_size = -1ULL;
1128
Jens Axboebd0ee742007-03-23 14:26:23 +01001129 if (td->o.directory)
1130 len = sprintf(file_name, "%s/", td->o.directory);
1131
1132 sprintf(file_name + len, "%s", fname);
Jens Axboef17c4392008-03-01 18:40:46 +01001133 f->file_name = smalloc_strdup(file_name);
Jens Axboec48c0be2008-07-31 19:55:02 +02001134 if (!f->file_name) {
1135 log_err("fio: smalloc OOM\n");
1136 assert(0);
1137 }
Jens Axboe0b9d69e2009-09-11 22:29:54 +02001138
Jens Axboee3bab462007-03-13 11:22:09 +01001139 get_file_type(f);
Jens Axboeaf52b342007-03-13 10:07:47 +01001140
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001141 switch (td->o.file_lock_mode) {
1142 case FILE_LOCK_NONE:
1143 break;
1144 case FILE_LOCK_READWRITE:
Jens Axboed7df1d12013-03-20 19:57:01 -06001145 f->rwlock = fio_rwlock_init();
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001146 break;
1147 case FILE_LOCK_EXCLUSIVE:
Jens Axboe521da522012-08-02 11:21:36 +02001148 f->lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001149 break;
1150 default:
1151 log_err("fio: unknown lock mode: %d\n", td->o.file_lock_mode);
1152 assert(0);
1153 }
Jens Axboe29c13492008-03-01 19:25:20 +01001154
Jens Axboe7b4e4fe2007-03-13 12:51:40 +01001155 td->files_index++;
Jens Axboe15494412007-03-13 11:52:47 +01001156 if (f->filetype == FIO_TYPE_FILE)
1157 td->nr_normal_files++;
Jens Axboef29b25a2007-07-23 08:56:43 +02001158
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001159 dprint(FD_FILE, "file %p \"%s\" added at %d\n", f, f->file_name,
1160 cur_files);
Jens Axboe9efef3c2008-03-06 10:26:25 +01001161
Jens Axboef29b25a2007-07-23 08:56:43 +02001162 return cur_files;
Jens Axboeaf52b342007-03-13 10:07:47 +01001163}
Jens Axboe0ad920e2007-03-13 11:06:45 +01001164
Jens Axboe49ffb4a2010-08-24 15:01:37 +02001165int add_file_exclusive(struct thread_data *td, const char *fname)
1166{
1167 struct fio_file *f;
1168 unsigned int i;
1169
1170 for_each_file(td, f, i) {
1171 if (!strcmp(f->file_name, fname))
1172 return i;
1173 }
1174
1175 return add_file(td, fname);
1176}
1177
Jens Axboe0ad920e2007-03-13 11:06:45 +01001178void get_file(struct fio_file *f)
1179{
Jens Axboe8172fe92008-02-01 21:51:02 +01001180 dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references);
Jens Axboed6aed792009-06-03 08:41:15 +02001181 assert(fio_file_open(f));
Jens Axboe0ad920e2007-03-13 11:06:45 +01001182 f->references++;
1183}
1184
Jens Axboe6977bcd2008-03-01 15:55:36 +01001185int put_file(struct thread_data *td, struct fio_file *f)
Jens Axboe0ad920e2007-03-13 11:06:45 +01001186{
Jens Axboe98e1ac42008-03-06 11:56:48 +01001187 int f_ret = 0, ret = 0;
Jens Axboe6977bcd2008-03-01 15:55:36 +01001188
Jens Axboe8172fe92008-02-01 21:51:02 +01001189 dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references);
Jens Axboeee56ad52008-02-01 10:30:20 +01001190
Jens Axboe22a57ba2009-06-09 14:34:35 +02001191 if (!fio_file_open(f)) {
1192 assert(f->fd == -1);
Jens Axboe6977bcd2008-03-01 15:55:36 +01001193 return 0;
Jens Axboe22a57ba2009-06-09 14:34:35 +02001194 }
Jens Axboe0ad920e2007-03-13 11:06:45 +01001195
1196 assert(f->references);
1197 if (--f->references)
Jens Axboe6977bcd2008-03-01 15:55:36 +01001198 return 0;
Jens Axboe0ad920e2007-03-13 11:06:45 +01001199
Jens Axboed424d4d2007-04-17 09:05:10 +02001200 if (should_fsync(td) && td->o.fsync_on_close)
Jens Axboe98e1ac42008-03-06 11:56:48 +01001201 f_ret = fsync(f->fd);
Jens Axboeebb14152007-03-13 14:42:15 +01001202
Jens Axboe0ad920e2007-03-13 11:06:45 +01001203 if (td->io_ops->close_file)
Jens Axboe6977bcd2008-03-01 15:55:36 +01001204 ret = td->io_ops->close_file(td, f);
Jens Axboe1020a132007-03-29 11:15:06 +02001205
Jens Axboe98e1ac42008-03-06 11:56:48 +01001206 if (!ret)
Jens Axboea5fb4612008-05-28 10:54:01 +02001207 ret = f_ret;
Jens Axboe98e1ac42008-03-06 11:56:48 +01001208
Jens Axboe0ad920e2007-03-13 11:06:45 +01001209 td->nr_open_files--;
Jens Axboed6aed792009-06-03 08:41:15 +02001210 fio_file_clear_open(f);
Jens Axboe22a57ba2009-06-09 14:34:35 +02001211 assert(f->fd == -1);
Jens Axboe6977bcd2008-03-01 15:55:36 +01001212 return ret;
Jens Axboe0ad920e2007-03-13 11:06:45 +01001213}
Jens Axboebbf6b542007-03-13 15:28:55 +01001214
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001215void lock_file(struct thread_data *td, struct fio_file *f, enum fio_ddir ddir)
Jens Axboeb2bd2bd2008-03-01 18:19:52 +01001216{
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001217 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1218 return;
Jens Axboe29c13492008-03-01 19:25:20 +01001219
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001220 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
1221 if (ddir == DDIR_READ)
Jens Axboed7df1d12013-03-20 19:57:01 -06001222 fio_rwlock_read(f->rwlock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001223 else
Jens Axboed7df1d12013-03-20 19:57:01 -06001224 fio_rwlock_write(f->rwlock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001225 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1226 fio_mutex_down(f->lock);
1227
Jens Axboed7df1d12013-03-20 19:57:01 -06001228 td->file_locks[f->fileno] = td->o.file_lock_mode;
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001229}
1230
1231void unlock_file(struct thread_data *td, struct fio_file *f)
1232{
1233 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1234 return;
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001235
Jens Axboed7df1d12013-03-20 19:57:01 -06001236 if (td->o.file_lock_mode == FILE_LOCK_READWRITE)
1237 fio_rwlock_unlock(f->rwlock);
1238 else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001239 fio_mutex_up(f->lock);
Jens Axboed7df1d12013-03-20 19:57:01 -06001240
1241 td->file_locks[f->fileno] = FILE_LOCK_NONE;
Jens Axboeb2bd2bd2008-03-01 18:19:52 +01001242}
1243
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001244void unlock_file_all(struct thread_data *td, struct fio_file *f)
Jens Axboeb2bd2bd2008-03-01 18:19:52 +01001245{
Jens Axboe27ddbfa2014-01-22 16:27:32 -08001246 if (td->o.file_lock_mode == FILE_LOCK_NONE)
1247 return;
Jens Axboed7df1d12013-03-20 19:57:01 -06001248 if (td->file_locks[f->fileno] != FILE_LOCK_NONE)
1249 unlock_file(td, f);
Jens Axboeb2bd2bd2008-03-01 18:19:52 +01001250}
1251
Jens Axboebbf6b542007-03-13 15:28:55 +01001252static int recurse_dir(struct thread_data *td, const char *dirname)
1253{
1254 struct dirent *dir;
1255 int ret = 0;
1256 DIR *D;
1257
1258 D = opendir(dirname);
1259 if (!D) {
Jens Axboe0ddb2702007-03-27 19:43:53 +02001260 char buf[FIO_VERROR_SIZE];
1261
Ken Raeburn98ffb8f2013-01-30 22:31:09 +01001262 snprintf(buf, FIO_VERROR_SIZE, "opendir(%s)", dirname);
Jens Axboe0ddb2702007-03-27 19:43:53 +02001263 td_verror(td, errno, buf);
Jens Axboebbf6b542007-03-13 15:28:55 +01001264 return 1;
1265 }
1266
1267 while ((dir = readdir(D)) != NULL) {
1268 char full_path[PATH_MAX];
1269 struct stat sb;
1270
Jens Axboee85b2b82007-03-14 09:39:06 +01001271 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
1272 continue;
Jens Axboe96d32d52007-03-14 09:16:09 +01001273
Bruce Cranb9fd7882012-02-20 17:01:46 +00001274 sprintf(full_path, "%s%s%s", dirname, FIO_OS_PATH_SEPARATOR, dir->d_name);
Jens Axboebbf6b542007-03-13 15:28:55 +01001275
1276 if (lstat(full_path, &sb) == -1) {
1277 if (errno != ENOENT) {
1278 td_verror(td, errno, "stat");
1279 return 1;
1280 }
1281 }
1282
1283 if (S_ISREG(sb.st_mode)) {
1284 add_file(td, full_path);
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001285 td->o.nr_files++;
Jens Axboebbf6b542007-03-13 15:28:55 +01001286 continue;
1287 }
Jens Axboe0ddb2702007-03-27 19:43:53 +02001288 if (!S_ISDIR(sb.st_mode))
1289 continue;
Jens Axboebbf6b542007-03-13 15:28:55 +01001290
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001291 ret = recurse_dir(td, full_path);
1292 if (ret)
Jens Axboebbf6b542007-03-13 15:28:55 +01001293 break;
1294 }
1295
1296 closedir(D);
1297 return ret;
1298}
1299
1300int add_dir_files(struct thread_data *td, const char *path)
1301{
Jens Axboe0ddb2702007-03-27 19:43:53 +02001302 int ret = recurse_dir(td, path);
1303
1304 if (!ret)
1305 log_info("fio: opendir added %d files\n", td->o.nr_files);
1306
1307 return ret;
Jens Axboebbf6b542007-03-13 15:28:55 +01001308}
Jens Axboecade3ef2007-03-23 15:29:45 +01001309
1310void dup_files(struct thread_data *td, struct thread_data *org)
1311{
1312 struct fio_file *f;
1313 unsigned int i;
Jens Axboe9efef3c2008-03-06 10:26:25 +01001314
1315 dprint(FD_FILE, "dup files: %d\n", org->files_index);
Jens Axboecade3ef2007-03-23 15:29:45 +01001316
1317 if (!org->files)
1318 return;
1319
Jens Axboe9efef3c2008-03-06 10:26:25 +01001320 td->files = malloc(org->files_index * sizeof(f));
Jens Axboecade3ef2007-03-23 15:29:45 +01001321
Jens Axboed7df1d12013-03-20 19:57:01 -06001322 if (td->o.file_lock_mode != FILE_LOCK_NONE)
1323 td->file_locks = malloc(org->files_index);
1324
Jens Axboe9efef3c2008-03-06 10:26:25 +01001325 for_each_file(org, f, i) {
Jens Axboeb0fe4212008-03-01 18:22:27 +01001326 struct fio_file *__f;
1327
Jens Axboef17c4392008-03-01 18:40:46 +01001328 __f = smalloc(sizeof(*__f));
Jens Axboec48c0be2008-07-31 19:55:02 +02001329 if (!__f) {
1330 log_err("fio: smalloc OOM\n");
1331 assert(0);
1332 }
Jens Axboe22a57ba2009-06-09 14:34:35 +02001333 __f->fd = -1;
Jens Axboe33c48812013-01-21 09:46:06 -07001334 fio_file_reset(td, __f);
Jens Axboe0b9d69e2009-09-11 22:29:54 +02001335
Aaron Carrollbc3456f2008-06-25 09:20:37 +02001336 if (f->file_name) {
Jens Axboef17c4392008-03-01 18:40:46 +01001337 __f->file_name = smalloc_strdup(f->file_name);
Jens Axboec48c0be2008-07-31 19:55:02 +02001338 if (!__f->file_name) {
1339 log_err("fio: smalloc OOM\n");
1340 assert(0);
1341 }
Jens Axboe0b9d69e2009-09-11 22:29:54 +02001342
Aaron Carrollbc3456f2008-06-25 09:20:37 +02001343 __f->filetype = f->filetype;
1344 }
Jens Axboeb0fe4212008-03-01 18:22:27 +01001345
1346 td->files[i] = __f;
Jens Axboecade3ef2007-03-23 15:29:45 +01001347 }
1348}
Jens Axboef29b25a2007-07-23 08:56:43 +02001349
1350/*
1351 * Returns the index that matches the filename, or -1 if not there
1352 */
1353int get_fileno(struct thread_data *td, const char *fname)
1354{
1355 struct fio_file *f;
1356 unsigned int i;
1357
1358 for_each_file(td, f, i)
1359 if (!strcmp(f->file_name, fname))
1360 return i;
1361
1362 return -1;
1363}
1364
1365/*
1366 * For log usage, where we add/open/close files automatically
1367 */
1368void free_release_files(struct thread_data *td)
1369{
1370 close_files(td);
1371 td->files_index = 0;
1372 td->nr_normal_files = 0;
1373}
Jens Axboe33c48812013-01-21 09:46:06 -07001374
1375void fio_file_reset(struct thread_data *td, struct fio_file *f)
1376{
1377 f->last_pos = f->file_offset;
1378 f->last_start = -1ULL;
1379 if (f->io_axmap)
1380 axmap_reset(f->io_axmap);
1381 if (td->o.random_generator == FIO_RAND_GEN_LFSR)
1382 lfsr_reset(&f->lfsr, td->rand_seeds[FIO_RAND_BLOCK_OFF]);
1383}