blob: 4265e3830a73c57f0f3a1c1d24e87db6b3344d9a [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]);
Jens Axboe53cdc682006-10-18 11:50:58 +0200130
Jens Axboe7bb48f82007-03-27 15:30:28 +0200131 left = f->real_file_size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200132 while (left && !td->terminate) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100133 bs = td->o.max_bs[DDIR_WRITE];
Jens Axboe53cdc682006-10-18 11:50:58 +0200134 if (bs > left)
135 bs = left;
136
Jens Axboecc86c392013-05-03 15:12:33 +0200137 fill_io_buffer(td, b, bs, bs);
138
Jens Axboe53cdc682006-10-18 11:50:58 +0200139 r = write(f->fd, b, bs);
140
Jens Axboe5e0074c2009-06-02 21:40:09 +0200141 if (r > 0) {
142 left -= r;
Jens Axboe53cdc682006-10-18 11:50:58 +0200143 continue;
144 } else {
Jens Axboe5e0074c2009-06-02 21:40:09 +0200145 if (r < 0) {
146 int __e = errno;
147
148 if (__e == ENOSPC) {
149 if (td->o.fill_device)
150 break;
151 log_info("fio: ENOSPC on laying out "
152 "file, stopping\n");
153 break;
154 }
Jens Axboee1161c32007-02-22 19:36:48 +0100155 td_verror(td, errno, "write");
Jens Axboe5e0074c2009-06-02 21:40:09 +0200156 } else
Jens Axboee1161c32007-02-22 19:36:48 +0100157 td_verror(td, EIO, "write");
Jens Axboe53cdc682006-10-18 11:50:58 +0200158
159 break;
160 }
161 }
162
Jens Axboeffdfabd2008-03-26 09:18:14 +0100163 if (td->terminate) {
164 dprint(FD_FILE, "terminate unlink %s\n", f->file_name);
Jens Axboe53cdc682006-10-18 11:50:58 +0200165 unlink(f->file_name);
Jens Axboeffdfabd2008-03-26 09:18:14 +0100166 } else if (td->o.create_fsync) {
Jens Axboe98e1ac42008-03-06 11:56:48 +0100167 if (fsync(f->fd) < 0) {
168 td_verror(td, errno, "fsync");
169 goto err;
170 }
171 }
Jens Axboe0d1cd202009-06-05 22:11:52 +0200172 if (td->o.fill_device && !td_write(td)) {
Jens Axboed6aed792009-06-03 08:41:15 +0200173 fio_file_clear_size_known(f);
Jens Axboe5e0074c2009-06-02 21:40:09 +0200174 if (td_io_get_file_size(td, f))
175 goto err;
176 if (f->io_size > f->real_file_size)
177 f->io_size = f->real_file_size;
178 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200179
180 free(b);
Jens Axboe507a7022007-03-27 15:52:46 +0200181done:
Jens Axboe53cdc682006-10-18 11:50:58 +0200182 return 0;
183err:
184 close(f->fd);
185 f->fd = -1;
186 return 1;
187}
188
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200189static int pre_read_file(struct thread_data *td, struct fio_file *f)
190{
Jens Axboeb0f65862009-05-20 11:52:15 +0200191 int r, did_open = 0, old_runstate;
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200192 unsigned long long left;
193 unsigned int bs;
194 char *b;
195
Jens Axboe9c0d2242009-07-01 12:26:28 +0200196 if (td->io_ops->flags & FIO_PIPEIO)
197 return 0;
198
Jens Axboed6aed792009-06-03 08:41:15 +0200199 if (!fio_file_open(f)) {
Jens Axboeb0f65862009-05-20 11:52:15 +0200200 if (td->io_ops->open_file(td, f)) {
201 log_err("fio: cannot pre-read, failed to open file\n");
202 return 1;
203 }
204 did_open = 1;
205 }
206
207 old_runstate = td->runstate;
208 td_set_runstate(td, TD_PRE_READING);
209
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200210 bs = td->o.max_bs[DDIR_READ];
211 b = malloc(bs);
212 memset(b, 0, bs);
213
214 lseek(f->fd, f->file_offset, SEEK_SET);
215 left = f->io_size;
216
217 while (left && !td->terminate) {
218 if (bs > left)
219 bs = left;
220
221 r = read(f->fd, b, bs);
222
223 if (r == (int) bs) {
224 left -= bs;
225 continue;
226 } else {
227 td_verror(td, EIO, "pre_read");
228 break;
229 }
230 }
231
Jens Axboeb0f65862009-05-20 11:52:15 +0200232 td_set_runstate(td, old_runstate);
233
234 if (did_open)
235 td->io_ops->close_file(td, f);
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200236 free(b);
237 return 0;
238}
239
Jens Axboe7bb48f82007-03-27 15:30:28 +0200240static unsigned long long get_rand_file_size(struct thread_data *td)
Jens Axboe9c60ce62007-03-15 09:14:47 +0100241{
Jens Axboedc873b62008-06-04 20:13:04 +0200242 unsigned long long ret, sized;
Jens Axboe1294c3e2011-05-11 08:15:18 +0200243 unsigned long r;
Jens Axboe9c60ce62007-03-15 09:14:47 +0100244
Jens Axboe4c07ad82011-03-28 09:51:09 +0200245 if (td->o.use_os_rand) {
246 r = os_random_long(&td->file_size_state);
247 sized = td->o.file_size_high - td->o.file_size_low;
248 ret = (unsigned long long) ((double) sized * (r / (OS_RAND_MAX + 1.0)));
249 } else {
250 r = __rand(&td->__file_size_state);
251 sized = td->o.file_size_high - td->o.file_size_low;
252 ret = (unsigned long long) ((double) sized * (r / (FRAND_MAX + 1.0)));
253 }
254
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100255 ret += td->o.file_size_low;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100256 ret -= (ret % td->o.rw_min_bs);
Jens Axboe9c60ce62007-03-15 09:14:47 +0100257 return ret;
258}
259
Jens Axboe53cdc682006-10-18 11:50:58 +0200260static int file_size(struct thread_data *td, struct fio_file *f)
261{
262 struct stat st;
263
Jens Axboedf9c26b2009-03-05 10:13:58 +0100264 if (stat(f->file_name, &st) == -1) {
Jens Axboe7bb48f82007-03-27 15:30:28 +0200265 td_verror(td, errno, "fstat");
266 return 1;
267 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200268
Jens Axboe7bb48f82007-03-27 15:30:28 +0200269 f->real_file_size = st.st_size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200270 return 0;
271}
272
273static int bdev_size(struct thread_data *td, struct fio_file *f)
274{
Bruce Cran9b836562011-01-08 19:49:54 +0100275 unsigned long long bytes = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200276 int r;
277
Jens Axboedf9c26b2009-03-05 10:13:58 +0100278 if (td->io_ops->open_file(td, f)) {
279 log_err("fio: failed opening blockdev %s for size check\n",
280 f->file_name);
281 return 1;
282 }
283
Bruce Cranecc314b2011-01-04 10:59:30 +0100284 r = blockdev_size(f, &bytes);
Jens Axboe53cdc682006-10-18 11:50:58 +0200285 if (r) {
Jens Axboee1161c32007-02-22 19:36:48 +0100286 td_verror(td, r, "blockdev_size");
Jens Axboedf9c26b2009-03-05 10:13:58 +0100287 goto err;
Jens Axboe53cdc682006-10-18 11:50:58 +0200288 }
289
Jens Axboe7ab077a2009-02-02 15:07:37 +0100290 if (!bytes) {
291 log_err("%s: zero sized block device?\n", f->file_name);
Jens Axboedf9c26b2009-03-05 10:13:58 +0100292 goto err;
Jens Axboe7ab077a2009-02-02 15:07:37 +0100293 }
294
Jens Axboe53cdc682006-10-18 11:50:58 +0200295 f->real_file_size = bytes;
Jens Axboe22a57ba2009-06-09 14:34:35 +0200296 td->io_ops->close_file(td, f);
Jens Axboe53cdc682006-10-18 11:50:58 +0200297 return 0;
Jens Axboedf9c26b2009-03-05 10:13:58 +0100298err:
299 td->io_ops->close_file(td, f);
300 return 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200301}
302
Jens Axboe4ccdccd2010-06-24 10:27:22 +0200303static int char_size(struct thread_data *td, struct fio_file *f)
304{
305#ifdef FIO_HAVE_CHARDEV_SIZE
Bruce Cran9b836562011-01-08 19:49:54 +0100306 unsigned long long bytes = 0;
Jens Axboe4ccdccd2010-06-24 10:27:22 +0200307 int r;
308
309 if (td->io_ops->open_file(td, f)) {
310 log_err("fio: failed opening blockdev %s for size check\n",
311 f->file_name);
312 return 1;
313 }
314
Bruce Cranecc314b2011-01-04 10:59:30 +0100315 r = chardev_size(f, &bytes);
Jens Axboe4ccdccd2010-06-24 10:27:22 +0200316 if (r) {
317 td_verror(td, r, "chardev_size");
318 goto err;
319 }
320
321 if (!bytes) {
322 log_err("%s: zero sized char device?\n", f->file_name);
323 goto err;
324 }
325
326 f->real_file_size = bytes;
327 td->io_ops->close_file(td, f);
328 return 0;
329err:
330 td->io_ops->close_file(td, f);
331 return 1;
332#else
333 f->real_file_size = -1ULL;
334 return 0;
335#endif
336}
337
Jens Axboe53cdc682006-10-18 11:50:58 +0200338static int get_file_size(struct thread_data *td, struct fio_file *f)
339{
340 int ret = 0;
341
Jens Axboed6aed792009-06-03 08:41:15 +0200342 if (fio_file_size_known(f))
Jens Axboe409b3412007-03-28 09:57:01 +0200343 return 0;
344
Jens Axboe7bb48f82007-03-27 15:30:28 +0200345 if (f->filetype == FIO_TYPE_FILE)
346 ret = file_size(td, f);
347 else if (f->filetype == FIO_TYPE_BD)
Jens Axboe53cdc682006-10-18 11:50:58 +0200348 ret = bdev_size(td, f);
Jens Axboe4ccdccd2010-06-24 10:27:22 +0200349 else if (f->filetype == FIO_TYPE_CHAR)
350 ret = char_size(td, f);
Jens Axboe53cdc682006-10-18 11:50:58 +0200351 else
352 f->real_file_size = -1;
353
354 if (ret)
355 return ret;
356
357 if (f->file_offset > f->real_file_size) {
Bruce Cran0f2152c2010-12-15 10:33:03 +0100358 log_err("%s: offset extends end (%llu > %llu)\n", td->o.name,
Jens Axboe4e0a8fa2013-04-15 11:40:57 +0200359 (unsigned long long) f->file_offset,
360 (unsigned long long) f->real_file_size);
Jens Axboe53cdc682006-10-18 11:50:58 +0200361 return 1;
362 }
363
Jens Axboed6aed792009-06-03 08:41:15 +0200364 fio_file_set_size_known(f);
Jens Axboe53cdc682006-10-18 11:50:58 +0200365 return 0;
366}
367
Jens Axboe3baddf22008-04-04 11:10:30 +0200368static int __file_invalidate_cache(struct thread_data *td, struct fio_file *f,
369 unsigned long long off,
370 unsigned long long len)
Jens Axboee5b401d2006-10-18 16:03:40 +0200371{
372 int ret = 0;
373
Jens Axboe5e0074c2009-06-02 21:40:09 +0200374 if (len == -1ULL)
Jens Axboe3baddf22008-04-04 11:10:30 +0200375 len = f->io_size;
376 if (off == -1ULL)
377 off = f->file_offset;
Jens Axboeee56ad52008-02-01 10:30:20 +0100378
Jens Axboe0d1cd202009-06-05 22:11:52 +0200379 if (len == -1ULL || off == -1ULL)
380 return 0;
381
Jens Axboe3baddf22008-04-04 11:10:30 +0200382 dprint(FD_IO, "invalidate cache %s: %llu/%llu\n", f->file_name, off,
383 len);
Jens Axboeb5af8292007-03-08 12:43:13 +0100384
Jens Axboee5b401d2006-10-18 16:03:40 +0200385 /*
386 * FIXME: add blockdev flushing too
387 */
Jens Axboea1c58072009-08-04 23:17:02 +0200388 if (f->mmap_ptr) {
Bruce Cran03e20d62011-01-02 20:14:54 +0100389 ret = posix_madvise(f->mmap_ptr, f->mmap_sz, POSIX_MADV_DONTNEED);
Jens Axboea1c58072009-08-04 23:17:02 +0200390#ifdef FIO_MADV_FREE
Jens Axboe3e10fb82013-08-09 14:31:06 -0600391 if (f->filetype == FIO_TYPE_BD)
392 (void) posix_madvise(f->mmap_ptr, f->mmap_sz, FIO_MADV_FREE);
Jens Axboea1c58072009-08-04 23:17:02 +0200393#endif
394 } else if (f->filetype == FIO_TYPE_FILE) {
Bruce Cranecc314b2011-01-04 10:59:30 +0100395 ret = posix_fadvise(f->fd, off, len, POSIX_FADV_DONTNEED);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100396 } else if (f->filetype == FIO_TYPE_BD) {
Bruce Cranecc314b2011-01-04 10:59:30 +0100397 ret = blockdev_invalidate_cache(f);
Jens Axboe7e0e25c2007-03-27 12:49:56 +0200398 if (ret < 0 && errno == EACCES && geteuid()) {
Jens Axboe7172cfe2007-07-23 14:36:00 +0200399 if (!root_warn) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100400 log_err("fio: only root may flush block "
401 "devices. Cache flush bypassed!\n");
Jens Axboe7172cfe2007-07-23 14:36:00 +0200402 root_warn = 1;
403 }
Jens Axboe7e0e25c2007-03-27 12:49:56 +0200404 ret = 0;
405 }
Jens Axboeb5605e92007-04-16 20:42:33 +0200406 } else if (f->filetype == FIO_TYPE_CHAR || f->filetype == FIO_TYPE_PIPE)
Jens Axboee5b401d2006-10-18 16:03:40 +0200407 ret = 0;
408
409 if (ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100410 td_verror(td, errno, "invalidate_cache");
Jens Axboee5b401d2006-10-18 16:03:40 +0200411 return 1;
Jens Axboe3baddf22008-04-04 11:10:30 +0200412 } else if (ret > 0) {
413 td_verror(td, ret, "invalidate_cache");
414 return 1;
Jens Axboee5b401d2006-10-18 16:03:40 +0200415 }
416
Jens Axboead2da602007-02-26 12:33:27 +0100417 return ret;
Jens Axboe3baddf22008-04-04 11:10:30 +0200418
419}
420
421int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
422{
Jens Axboed6aed792009-06-03 08:41:15 +0200423 if (!fio_file_open(f))
Jens Axboea5fb4612008-05-28 10:54:01 +0200424 return 0;
425
Jens Axboe5e0074c2009-06-02 21:40:09 +0200426 return __file_invalidate_cache(td, f, -1ULL, -1ULL);
Jens Axboee5b401d2006-10-18 16:03:40 +0200427}
428
Jens Axboe6977bcd2008-03-01 15:55:36 +0100429int generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
Jens Axboe53cdc682006-10-18 11:50:58 +0200430{
Jens Axboe6977bcd2008-03-01 15:55:36 +0100431 int ret = 0;
432
Jens Axboeee56ad52008-02-01 10:30:20 +0100433 dprint(FD_FILE, "fd close %s\n", f->file_name);
Jens Axboe4906e0b2008-03-01 18:59:51 +0100434
435 remove_file_hash(f);
436
Jens Axboe6977bcd2008-03-01 15:55:36 +0100437 if (close(f->fd) < 0)
438 ret = errno;
439
Jens Axboeb5af8292007-03-08 12:43:13 +0100440 f->fd = -1;
Jens Axboee6c4d732012-12-12 08:16:27 +0100441
442 if (f->shadow_fd != -1) {
443 close(f->shadow_fd);
444 f->shadow_fd = -1;
445 }
446
Juan Casse57e54e02013-09-20 09:20:52 -0600447 f->engine_data = 0;
Jens Axboe6977bcd2008-03-01 15:55:36 +0100448 return ret;
Jens Axboe53cdc682006-10-18 11:50:58 +0200449}
450
Dmitry Monakhov1ccc6dc2012-09-19 23:22:53 +0400451int file_lookup_open(struct fio_file *f, int flags)
Jens Axboe53cdc682006-10-18 11:50:58 +0200452{
Jens Axboe29c13492008-03-01 19:25:20 +0100453 struct fio_file *__f;
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100454 int from_hash;
455
456 __f = lookup_file_hash(f->file_name);
457 if (__f) {
Jens Axboe9efef3c2008-03-06 10:26:25 +0100458 dprint(FD_FILE, "found file in hash %s\n", f->file_name);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100459 /*
460 * racy, need the __f->lock locked
461 */
462 f->lock = __f->lock;
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100463 from_hash = 1;
464 } else {
Jens Axboe9efef3c2008-03-06 10:26:25 +0100465 dprint(FD_FILE, "file not found in hash %s\n", f->file_name);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100466 from_hash = 0;
467 }
468
Jens Axboee8670ef2008-03-06 12:06:30 +0100469 f->fd = open(f->file_name, flags, 0600);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100470 return from_hash;
471}
472
Jens Axboee6c4d732012-12-12 08:16:27 +0100473static int file_close_shadow_fds(struct thread_data *td)
474{
475 struct fio_file *f;
476 int num_closed = 0;
477 unsigned int i;
478
479 for_each_file(td, f, i) {
480 if (f->shadow_fd == -1)
481 continue;
482
483 close(f->shadow_fd);
484 f->shadow_fd = -1;
485 num_closed++;
486 }
487
488 return num_closed;
489}
490
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100491int generic_open_file(struct thread_data *td, struct fio_file *f)
492{
Jens Axboe66159822007-04-16 21:54:24 +0200493 int is_std = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200494 int flags = 0;
Jens Axboe29c13492008-03-01 19:25:20 +0100495 int from_hash = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200496
Jens Axboeee56ad52008-02-01 10:30:20 +0100497 dprint(FD_FILE, "fd open %s\n", f->file_name);
498
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200499 if (td_trim(td) && f->filetype != FIO_TYPE_BD) {
500 log_err("fio: trim only applies to block device\n");
501 return 1;
502 }
503
Jens Axboe66159822007-04-16 21:54:24 +0200504 if (!strcmp(f->file_name, "-")) {
505 if (td_rw(td)) {
506 log_err("fio: can't read/write to stdin/out\n");
507 return 1;
508 }
509 is_std = 1;
Jens Axboece98fa62007-04-17 13:27:32 +0200510
511 /*
512 * move output logging to stderr, if we are writing to stdout
513 */
514 if (td_write(td))
515 f_out = stderr;
Jens Axboe66159822007-04-16 21:54:24 +0200516 }
517
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200518 if (td_trim(td))
519 goto skip_flags;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100520 if (td->o.odirect)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100521 flags |= OS_O_DIRECT;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100522 if (td->o.sync_io)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100523 flags |= O_SYNC;
Jens Axboe814452b2009-03-04 12:53:13 +0100524 if (td->o.create_on_open)
525 flags |= O_CREAT;
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200526skip_flags:
527 if (f->filetype != FIO_TYPE_FILE)
528 flags |= FIO_O_NOATIME;
Jens Axboe7abf8332006-11-23 15:01:19 +0100529
Aaron Carroll056f3452007-11-26 09:08:53 +0100530open_again:
Jens Axboe660a1cb2007-04-17 15:37:47 +0200531 if (td_write(td)) {
Jens Axboe17308152008-03-07 13:42:31 +0100532 if (!read_only)
533 flags |= O_RDWR;
Jens Axboe53cdc682006-10-18 11:50:58 +0200534
Jens Axboeaf52b342007-03-13 10:07:47 +0100535 if (f->filetype == FIO_TYPE_FILE)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100536 flags |= O_CREAT;
Jens Axboe2fd233b2007-03-02 08:44:25 +0100537
Jens Axboe66159822007-04-16 21:54:24 +0200538 if (is_std)
539 f->fd = dup(STDOUT_FILENO);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100540 else
541 from_hash = file_lookup_open(f, flags);
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200542 } else if (td_read(td)) {
Jens Axboe4241ea82007-09-12 08:18:36 +0200543 if (f->filetype == FIO_TYPE_CHAR && !read_only)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100544 flags |= O_RDWR;
545 else
546 flags |= O_RDONLY;
547
Jens Axboe66159822007-04-16 21:54:24 +0200548 if (is_std)
549 f->fd = dup(STDIN_FILENO);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100550 else
551 from_hash = file_lookup_open(f, flags);
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200552 } else { //td trim
553 flags |= O_RDWR;
554 from_hash = file_lookup_open(f, flags);
Jens Axboe53cdc682006-10-18 11:50:58 +0200555 }
556
557 if (f->fd == -1) {
Jens Axboee4e33252007-03-21 13:07:54 +0100558 char buf[FIO_VERROR_SIZE];
Jens Axboee1161c32007-02-22 19:36:48 +0100559 int __e = errno;
560
Jens Axboe835d9b92009-06-09 15:23:38 +0200561 if (__e == EPERM && (flags & FIO_O_NOATIME)) {
Jens Axboe5921e802008-05-30 15:02:38 +0200562 flags &= ~FIO_O_NOATIME;
Aaron Carroll056f3452007-11-26 09:08:53 +0100563 goto open_again;
564 }
Jens Axboee6c4d732012-12-12 08:16:27 +0100565 if (__e == EMFILE && file_close_shadow_fds(td))
566 goto open_again;
Aaron Carroll056f3452007-11-26 09:08:53 +0100567
Ken Raeburn98ffb8f2013-01-30 22:31:09 +0100568 snprintf(buf, sizeof(buf), "open(%s)", f->file_name);
Jens Axboee4e33252007-03-21 13:07:54 +0100569
Jens Axboea93c5f02012-06-11 08:53:53 +0200570 if (__e == EINVAL && (flags & OS_O_DIRECT)) {
571 log_err("fio: looks like your file system does not " \
572 "support direct=1/buffered=0\n");
573 }
574
Jens Axboee4e33252007-03-21 13:07:54 +0100575 td_verror(td, __e, buf);
Jens Axboe53cdc682006-10-18 11:50:58 +0200576 }
577
Jens Axboe29c13492008-03-01 19:25:20 +0100578 if (!from_hash && f->fd != -1) {
579 if (add_file_hash(f)) {
Jens Axboe3f0ca9b2011-05-24 21:36:24 +0200580 int fio_unused ret;
Jens Axboe29c13492008-03-01 19:25:20 +0100581
582 /*
Jens Axboee6c4d732012-12-12 08:16:27 +0100583 * Stash away descriptor for later close. This is to
584 * work-around a "feature" on Linux, where a close of
585 * an fd that has been opened for write will trigger
586 * udev to call blkid to check partitions, fs id, etc.
Anatol Pomozovde8f6de2013-09-26 16:31:34 -0700587 * That pollutes the device cache, which can slow down
Jens Axboee6c4d732012-12-12 08:16:27 +0100588 * unbuffered accesses.
Jens Axboe29c13492008-03-01 19:25:20 +0100589 */
Jens Axboee6c4d732012-12-12 08:16:27 +0100590 if (f->shadow_fd == -1)
591 f->shadow_fd = f->fd;
592 else {
593 /*
594 * OK to ignore, we haven't done anything
595 * with it
596 */
597 ret = generic_close_file(td, f);
598 }
Jens Axboe29c13492008-03-01 19:25:20 +0100599 goto open_again;
600 }
601 }
Jens Axboe4906e0b2008-03-01 18:59:51 +0100602
Jens Axboe53cdc682006-10-18 11:50:58 +0200603 return 0;
Jens Axboeb5af8292007-03-08 12:43:13 +0100604}
605
Jens Axboedf9c26b2009-03-05 10:13:58 +0100606int generic_get_file_size(struct thread_data *td, struct fio_file *f)
Jens Axboe21972cd2006-11-23 12:39:16 +0100607{
Jens Axboedf9c26b2009-03-05 10:13:58 +0100608 return get_file_size(td, f);
Jens Axboe21972cd2006-11-23 12:39:16 +0100609}
610
Jens Axboe7bb48f82007-03-27 15:30:28 +0200611/*
612 * open/close all files, so that ->real_file_size gets set
613 */
Jens Axboebab3fd52007-04-02 10:34:18 +0200614static int get_file_sizes(struct thread_data *td)
Jens Axboe53cdc682006-10-18 11:50:58 +0200615{
616 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100617 unsigned int i;
Jens Axboebab3fd52007-04-02 10:34:18 +0200618 int err = 0;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200619
620 for_each_file(td, f, i) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100621 dprint(FD_FILE, "get file size for %p/%d/%p\n", f, i,
622 f->file_name);
Jens Axboe9efef3c2008-03-06 10:26:25 +0100623
Jens Axboe99a47c62009-04-07 22:20:56 +0200624 if (td_io_get_file_size(td, f)) {
Jens Axboe40b44f42007-04-11 12:43:35 +0200625 if (td->error != ENOENT) {
626 log_err("%s\n", td->verror);
627 err = 1;
628 }
Jens Axboe541d66d2007-03-27 19:50:11 +0200629 clear_error(td);
Jens Axboe07eb79d2007-04-12 13:02:38 +0200630 }
Jens Axboe409b3412007-03-28 09:57:01 +0200631
632 if (f->real_file_size == -1ULL && td->o.size)
633 f->real_file_size = td->o.size / td->o.nr_files;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200634 }
Jens Axboebab3fd52007-04-02 10:34:18 +0200635
636 return err;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200637}
638
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200639struct fio_mount {
640 struct flist_head list;
641 const char *base;
642 char __base[256];
643 unsigned int key;
644};
645
646/*
647 * Get free number of bytes for each file on each unique mount.
648 */
649static unsigned long long get_fs_free_counts(struct thread_data *td)
650{
651 struct flist_head *n, *tmp;
Jens Axboe68b03162010-06-24 09:22:41 +0200652 unsigned long long ret = 0;
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200653 struct fio_mount *fm;
654 FLIST_HEAD(list);
655 struct fio_file *f;
656 unsigned int i;
657
658 for_each_file(td, f, i) {
659 struct stat sb;
660 char buf[256];
661
Jens Axboe4ccdccd2010-06-24 10:27:22 +0200662 if (f->filetype == FIO_TYPE_BD || f->filetype == FIO_TYPE_CHAR) {
663 if (f->real_file_size != -1ULL)
664 ret += f->real_file_size;
Jens Axboe68b03162010-06-24 09:22:41 +0200665 continue;
666 } else if (f->filetype != FIO_TYPE_FILE)
667 continue;
668
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200669 strcpy(buf, f->file_name);
670
671 if (stat(buf, &sb) < 0) {
672 if (errno != ENOENT)
673 break;
674 strcpy(buf, ".");
675 if (stat(buf, &sb) < 0)
676 break;
677 }
678
679 fm = NULL;
680 flist_for_each(n, &list) {
681 fm = flist_entry(n, struct fio_mount, list);
682 if (fm->key == sb.st_dev)
683 break;
684
685 fm = NULL;
686 }
687
688 if (fm)
689 continue;
690
691 fm = malloc(sizeof(*fm));
692 strcpy(fm->__base, buf);
693 fm->base = basename(fm->__base);
694 fm->key = sb.st_dev;
695 flist_add(&fm->list, &list);
696 }
697
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200698 flist_for_each_safe(n, tmp, &list) {
699 unsigned long long sz;
700
701 fm = flist_entry(n, struct fio_mount, list);
702 flist_del(&fm->list);
703
704 sz = get_fs_size(fm->base);
705 if (sz && sz != -1ULL)
706 ret += sz;
707
708 free(fm);
709 }
710
711 return ret;
712}
713
Jens Axboe293b8c12013-01-04 13:16:54 +0100714uint64_t get_start_offset(struct thread_data *td)
Dan Ehrenbergce95d652012-08-16 08:58:21 +0200715{
716 return td->o.start_offset +
717 (td->thread_number - 1) * td->o.offset_increment;
718}
719
Jens Axboe7bb48f82007-03-27 15:30:28 +0200720/*
721 * Open the files and setup files sizes, creating files if necessary.
722 */
723int setup_files(struct thread_data *td)
724{
725 unsigned long long total_size, extend_size;
Jens Axboede98bd32013-04-05 11:09:20 +0200726 struct thread_options *o = &td->o;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200727 struct fio_file *f;
728 unsigned int i;
Jens Axboe000b0802007-03-27 15:56:10 +0200729 int err = 0, need_extend;
Jens Axboee90391e2013-04-13 20:40:53 +0200730 int old_state;
Jens Axboe53cdc682006-10-18 11:50:58 +0200731
Jens Axboeee56ad52008-02-01 10:30:20 +0100732 dprint(FD_FILE, "setup files\n");
733
Jens Axboee90391e2013-04-13 20:40:53 +0200734 old_state = td->runstate;
735 td_set_runstate(td, TD_SETTING_UP);
736
Jens Axboede98bd32013-04-05 11:09:20 +0200737 if (o->read_iolog_file)
Jens Axboe25460cf2012-05-02 13:58:02 +0200738 goto done;
Jens Axboe691c8fb2008-03-07 14:26:26 +0100739
Jens Axboe53cdc682006-10-18 11:50:58 +0200740 /*
741 * if ioengine defines a setup() method, it's responsible for
Jens Axboe7bb48f82007-03-27 15:30:28 +0200742 * opening the files and setting f->real_file_size to indicate
743 * the valid range for that file.
Jens Axboe53cdc682006-10-18 11:50:58 +0200744 */
745 if (td->io_ops->setup)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200746 err = td->io_ops->setup(td);
747 else
Jens Axboebab3fd52007-04-02 10:34:18 +0200748 err = get_file_sizes(td);
Jens Axboe53cdc682006-10-18 11:50:58 +0200749
Jens Axboef1027062006-10-20 10:14:01 +0200750 if (err)
Jens Axboee90391e2013-04-13 20:40:53 +0200751 goto err_out;
Jens Axboef1027062006-10-20 10:14:01 +0200752
Jens Axboe0a7eb122006-10-20 10:36:42 +0200753 /*
Jens Axboe7bb48f82007-03-27 15:30:28 +0200754 * check sizes. if the files/devices do not exist and the size
755 * isn't passed to fio, abort.
Jens Axboe0a7eb122006-10-20 10:36:42 +0200756 */
Jens Axboe7bb48f82007-03-27 15:30:28 +0200757 total_size = 0;
758 for_each_file(td, f, i) {
759 if (f->real_file_size == -1ULL)
760 total_size = -1ULL;
761 else
762 total_size += f->real_file_size;
763 }
Jens Axboe0a7eb122006-10-20 10:36:42 +0200764
Jens Axboede98bd32013-04-05 11:09:20 +0200765 if (o->fill_device)
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200766 td->fill_device_size = get_fs_free_counts(td);
767
Jens Axboe7bb48f82007-03-27 15:30:28 +0200768 /*
769 * device/file sizes are zero and no size given, punt
770 */
Jens Axboede98bd32013-04-05 11:09:20 +0200771 if ((!total_size || total_size == -1ULL) && !o->size &&
772 !(td->io_ops->flags & FIO_NOIO) && !o->fill_device &&
773 !(o->nr_files && (o->file_size_low || o->file_size_high))) {
774 log_err("%s: you need to specify size=\n", o->name);
Jens Axboee1161c32007-02-22 19:36:48 +0100775 td_verror(td, EINVAL, "total_file_size");
Jens Axboee90391e2013-04-13 20:40:53 +0200776 goto err_out;
Jens Axboe53cdc682006-10-18 11:50:58 +0200777 }
778
Jens Axboe7bb48f82007-03-27 15:30:28 +0200779 /*
780 * now file sizes are known, so we can set ->io_size. if size= is
781 * not given, ->io_size is just equal to ->real_file_size. if size
782 * is given, ->io_size is size / nr_files.
783 */
784 extend_size = total_size = 0;
785 need_extend = 0;
786 for_each_file(td, f, i) {
Dan Ehrenbergce95d652012-08-16 08:58:21 +0200787 f->file_offset = get_start_offset(td);
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200788
Jens Axboede98bd32013-04-05 11:09:20 +0200789 if (!o->file_size_low) {
Jens Axboe7bb48f82007-03-27 15:30:28 +0200790 /*
791 * no file size range given, file size is equal to
792 * total size divided by number of files. if that is
793 * zero, set it to the real file size.
794 */
Jens Axboede98bd32013-04-05 11:09:20 +0200795 f->io_size = o->size / o->nr_files;
Jens Axboe65bdb102008-01-24 13:13:12 +0100796 if (!f->io_size)
Jens Axboe273f8c92008-01-25 14:02:15 +0100797 f->io_size = f->real_file_size - f->file_offset;
Jens Axboede98bd32013-04-05 11:09:20 +0200798 } else if (f->real_file_size < o->file_size_low ||
799 f->real_file_size > o->file_size_high) {
800 if (f->file_offset > o->file_size_low)
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200801 goto err_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200802 /*
803 * file size given. if it's fixed, use that. if it's a
804 * range, generate a random size in-between.
805 */
Jens Axboede98bd32013-04-05 11:09:20 +0200806 if (o->file_size_low == o->file_size_high)
807 f->io_size = o->file_size_low - f->file_offset;
808 else {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100809 f->io_size = get_rand_file_size(td)
810 - f->file_offset;
811 }
Jens Axboe65bdb102008-01-24 13:13:12 +0100812 } else
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200813 f->io_size = f->real_file_size - f->file_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200814
815 if (f->io_size == -1ULL)
816 total_size = -1ULL;
Shaohua Li4d002562012-09-21 08:32:32 +0200817 else {
Jens Axboede98bd32013-04-05 11:09:20 +0200818 if (o->size_percent)
819 f->io_size = (f->io_size * o->size_percent) / 100;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200820 total_size += f->io_size;
Shaohua Li4d002562012-09-21 08:32:32 +0200821 }
Jens Axboe7bb48f82007-03-27 15:30:28 +0200822
823 if (f->filetype == FIO_TYPE_FILE &&
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200824 (f->io_size + f->file_offset) > f->real_file_size &&
Jens Axboe7bb48f82007-03-27 15:30:28 +0200825 !(td->io_ops->flags & FIO_DISKLESSIO)) {
Jens Axboede98bd32013-04-05 11:09:20 +0200826 if (!o->create_on_open) {
Jens Axboe814452b2009-03-04 12:53:13 +0100827 need_extend++;
828 extend_size += (f->io_size + f->file_offset);
829 } else
830 f->real_file_size = f->io_size + f->file_offset;
Jens Axboed6aed792009-06-03 08:41:15 +0200831 fio_file_set_extend(f);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100832 }
Jens Axboe7bb48f82007-03-27 15:30:28 +0200833 }
834
Jens Axboede98bd32013-04-05 11:09:20 +0200835 if (!o->size || o->size > total_size)
836 o->size = total_size;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200837
838 /*
839 * See if we need to extend some files
840 */
841 if (need_extend) {
842 temp_stall_ts = 1;
Jens Axboef3afa572012-09-17 13:34:16 +0200843 if (output_format == FIO_OUTPUT_NORMAL)
Shaozhi Shawn Yea7ba8c52008-09-10 09:04:18 +0200844 log_info("%s: Laying out IO file(s) (%u file(s) /"
Jens Axboede98bd32013-04-05 11:09:20 +0200845 " %lluMB)\n", o->name, need_extend,
Shaozhi Shawn Yea7ba8c52008-09-10 09:04:18 +0200846 extend_size >> 20);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200847
848 for_each_file(td, f, i) {
Jens Axboe5e0074c2009-06-02 21:40:09 +0200849 unsigned long long old_len = -1ULL, extend_len = -1ULL;
Jens Axboe3baddf22008-04-04 11:10:30 +0200850
Jens Axboed6aed792009-06-03 08:41:15 +0200851 if (!fio_file_extend(f))
Jens Axboe7bb48f82007-03-27 15:30:28 +0200852 continue;
853
Jens Axboe409b3412007-03-28 09:57:01 +0200854 assert(f->filetype == FIO_TYPE_FILE);
Jens Axboed6aed792009-06-03 08:41:15 +0200855 fio_file_clear_extend(f);
Jens Axboede98bd32013-04-05 11:09:20 +0200856 if (!o->fill_device) {
Jens Axboe5e0074c2009-06-02 21:40:09 +0200857 old_len = f->real_file_size;
Jens Axboe0b9d69e2009-09-11 22:29:54 +0200858 extend_len = f->io_size + f->file_offset -
859 old_len;
Jens Axboe5e0074c2009-06-02 21:40:09 +0200860 }
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200861 f->real_file_size = (f->io_size + f->file_offset);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200862 err = extend_file(td, f);
863 if (err)
864 break;
Jens Axboe5e0074c2009-06-02 21:40:09 +0200865
Jens Axboe3baddf22008-04-04 11:10:30 +0200866 err = __file_invalidate_cache(td, f, old_len,
867 extend_len);
868 close(f->fd);
869 f->fd = -1;
870 if (err)
871 break;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200872 }
873 temp_stall_ts = 0;
874 }
875
876 if (err)
Jens Axboee90391e2013-04-13 20:40:53 +0200877 goto err_out;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200878
Jens Axboede98bd32013-04-05 11:09:20 +0200879 if (!o->zone_size)
880 o->zone_size = o->size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200881
Jens Axboeea966f82007-09-03 10:09:37 +0200882 /*
883 * iolog already set the total io size, if we read back
884 * stored entries.
885 */
Jens Axboede98bd32013-04-05 11:09:20 +0200886 if (!o->read_iolog_file)
887 td->total_io_size = o->size * o->loops;
Jens Axboe25460cf2012-05-02 13:58:02 +0200888
889done:
Jens Axboede98bd32013-04-05 11:09:20 +0200890 if (o->create_only)
Jens Axboe25460cf2012-05-02 13:58:02 +0200891 td->done = 1;
892
Jens Axboee90391e2013-04-13 20:40:53 +0200893 td_set_runstate(td, old_state);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200894 return 0;
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200895err_offset:
Jens Axboede98bd32013-04-05 11:09:20 +0200896 log_err("%s: you need to specify valid offset=\n", o->name);
Jens Axboee90391e2013-04-13 20:40:53 +0200897err_out:
898 td_set_runstate(td, old_state);
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200899 return 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200900}
901
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200902int pre_read_files(struct thread_data *td)
903{
904 struct fio_file *f;
905 unsigned int i;
906
907 dprint(FD_FILE, "pre_read files\n");
908
909 for_each_file(td, f, i) {
910 pre_read_file(td, f);
911 }
912
913 return 1;
914}
915
Jens Axboe9c6f6312012-11-07 09:15:45 +0100916static int __init_rand_distribution(struct thread_data *td, struct fio_file *f)
917{
Jens Axboe23162962012-11-07 19:47:47 +0100918 unsigned int range_size, seed;
Jens Axboe9c6f6312012-11-07 09:15:45 +0100919 unsigned long nranges;
Jens Axboe21415db2013-01-04 13:09:29 +0100920 uint64_t file_size;
Jens Axboe9c6f6312012-11-07 09:15:45 +0100921
922 range_size = min(td->o.min_bs[DDIR_READ], td->o.min_bs[DDIR_WRITE]);
Jens Axboe21415db2013-01-04 13:09:29 +0100923 file_size = min(f->real_file_size, f->io_size);
Jens Axboe9c6f6312012-11-07 09:15:45 +0100924
Jens Axboe21415db2013-01-04 13:09:29 +0100925 nranges = (file_size + range_size - 1) / range_size;
Jens Axboe9c6f6312012-11-07 09:15:45 +0100926
Jens Axboe23162962012-11-07 19:47:47 +0100927 seed = jhash(f->file_name, strlen(f->file_name), 0) * td->thread_number;
Jens Axboe84256872012-11-17 10:06:36 -0700928 if (!td->o.rand_repeatable)
929 seed = td->rand_seeds[4];
930
Jens Axboe9c6f6312012-11-07 09:15:45 +0100931 if (td->o.random_distribution == FIO_RAND_DIST_ZIPF)
Jens Axboe888677a2013-04-10 22:19:46 +0200932 zipf_init(&f->zipf, nranges, td->o.zipf_theta.u.f, seed);
Jens Axboe9c6f6312012-11-07 09:15:45 +0100933 else
Jens Axboe888677a2013-04-10 22:19:46 +0200934 pareto_init(&f->zipf, nranges, td->o.pareto_h.u.f, seed);
Jens Axboe9c6f6312012-11-07 09:15:45 +0100935
936 return 1;
937}
938
939static int init_rand_distribution(struct thread_data *td)
940{
941 struct fio_file *f;
942 unsigned int i;
943 int state;
944
945 if (td->o.random_distribution == FIO_RAND_DIST_RANDOM)
946 return 0;
947
948 state = td->runstate;
949 td_set_runstate(td, TD_SETTING_UP);
950 for_each_file(td, f, i)
951 __init_rand_distribution(td, f);
952 td_set_runstate(td, state);
953
954 return 1;
955}
956
Jens Axboe68727072007-03-15 20:49:25 +0100957int init_random_map(struct thread_data *td)
958{
Jens Axboe51ede0b2012-11-22 13:50:29 +0100959 unsigned long long blocks;
Jens Axboe68727072007-03-15 20:49:25 +0100960 struct fio_file *f;
961 unsigned int i;
962
Jens Axboe9c6f6312012-11-07 09:15:45 +0100963 if (init_rand_distribution(td))
964 return 0;
Jens Axboe3831a842012-11-26 19:59:14 +0100965 if (!td_random(td))
Jens Axboe68727072007-03-15 20:49:25 +0100966 return 0;
967
968 for_each_file(td, f, i) {
Jens Axboe38f30c82013-01-11 14:03:05 +0100969 uint64_t file_size = min(f->real_file_size, f->io_size);
970
Jens Axboe53737ae2013-02-21 15:18:17 +0100971 blocks = file_size / (unsigned long long) td->o.rw_min_bs;
972
Jens Axboe8055e412012-11-26 08:43:47 +0100973 if (td->o.random_generator == FIO_RAND_GEN_LFSR) {
Jens Axboe82af46b2012-12-04 13:12:25 +0100974 unsigned long seed;
975
976 seed = td->rand_seeds[FIO_RAND_BLOCK_OFF];
977
Alex Pyrgiotisd474cbc2013-03-08 14:37:03 +0200978 if (!lfsr_init(&f->lfsr, blocks, seed, seed & 0xF))
Jens Axboe8055e412012-11-26 08:43:47 +0100979 continue;
Jens Axboe3831a842012-11-26 19:59:14 +0100980 } else if (!td->o.norandommap) {
Jens Axboe7ebd7962012-11-28 21:24:46 +0100981 f->io_axmap = axmap_new(blocks);
982 if (f->io_axmap)
Jens Axboe8055e412012-11-26 08:43:47 +0100983 continue;
Jens Axboe1cad7122012-11-29 21:37:11 +0100984 } else if (td->o.norandommap)
985 continue;
Jens Axboeceadd592012-02-02 08:41:28 +0100986
Jens Axboe2b386d22008-03-26 10:32:57 +0100987 if (!td->o.softrandommap) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100988 log_err("fio: failed allocating random map. If running"
989 " a large number of jobs, try the 'norandommap'"
Jens Axboe2b386d22008-03-26 10:32:57 +0100990 " option or set 'softrandommap'. Or give"
991 " a larger --alloc-size to fio.\n");
Jens Axboe68727072007-03-15 20:49:25 +0100992 return 1;
993 }
Jens Axboe303032a2008-03-26 10:11:10 +0100994
995 log_info("fio: file %s failed allocating random map. Running "
996 "job without.\n", f->file_name);
Jens Axboe68727072007-03-15 20:49:25 +0100997 }
998
999 return 0;
1000}
1001
Jens Axboe53cdc682006-10-18 11:50:58 +02001002void close_files(struct thread_data *td)
1003{
Jens Axboe0ab8db82006-10-18 17:16:23 +02001004 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +01001005 unsigned int i;
Jens Axboe53cdc682006-10-18 11:50:58 +02001006
Jens Axboe2be3eec2009-06-10 09:05:13 +02001007 for_each_file(td, f, i) {
1008 if (fio_file_open(f))
1009 td_io_close_file(td, f);
1010 }
Jens Axboe24ffd2c2008-03-01 15:47:08 +01001011}
1012
1013void close_and_free_files(struct thread_data *td)
1014{
1015 struct fio_file *f;
1016 unsigned int i;
1017
Jens Axboeee56ad52008-02-01 10:30:20 +01001018 dprint(FD_FILE, "close files\n");
1019
Jens Axboe0ab8db82006-10-18 17:16:23 +02001020 for_each_file(td, f, i) {
Jens Axboeffdfabd2008-03-26 09:18:14 +01001021 if (td->o.unlink && f->filetype == FIO_TYPE_FILE) {
1022 dprint(FD_FILE, "free unlink %s\n", f->file_name);
Jens Axboe132ad462006-11-23 12:23:12 +01001023 unlink(f->file_name);
Jens Axboeffdfabd2008-03-26 09:18:14 +01001024 }
Jens Axboebdb4e2e2007-03-01 09:54:57 +01001025
Jens Axboe22a57ba2009-06-09 14:34:35 +02001026 if (fio_file_open(f))
1027 td_io_close_file(td, f);
1028
Shaozhi Shawn Yeb9fbcf22008-09-10 09:05:17 +02001029 remove_file_hash(f);
Jens Axboeb3dc7f02007-03-09 12:52:15 +01001030
Jens Axboef17c4392008-03-01 18:40:46 +01001031 sfree(f->file_name);
Jens Axboefa1da862007-03-23 15:20:54 +01001032 f->file_name = NULL;
Jens Axboe7ebd7962012-11-28 21:24:46 +01001033 axmap_free(f->io_axmap);
1034 f->io_axmap = NULL;
Jens Axboe78d99e62008-03-01 18:41:50 +01001035 sfree(f);
Jens Axboe53cdc682006-10-18 11:50:58 +02001036 }
Jens Axboeb4a6a592006-10-20 13:54:47 +02001037
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001038 td->o.filename = NULL;
Jens Axboecade3ef2007-03-23 15:29:45 +01001039 free(td->files);
Jens Axboed7df1d12013-03-20 19:57:01 -06001040 free(td->file_locks);
Jens Axboe9efef3c2008-03-06 10:26:25 +01001041 td->files_index = 0;
Jens Axboeb4a6a592006-10-20 13:54:47 +02001042 td->files = NULL;
Jens Axboed7df1d12013-03-20 19:57:01 -06001043 td->file_locks = NULL;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001044 td->o.nr_files = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +02001045}
Jens Axboeaf52b342007-03-13 10:07:47 +01001046
Jens Axboee3bab462007-03-13 11:22:09 +01001047static void get_file_type(struct fio_file *f)
Jens Axboeaf52b342007-03-13 10:07:47 +01001048{
1049 struct stat sb;
1050
Jens Axboe66159822007-04-16 21:54:24 +02001051 if (!strcmp(f->file_name, "-"))
1052 f->filetype = FIO_TYPE_PIPE;
1053 else
1054 f->filetype = FIO_TYPE_FILE;
Jens Axboeaf52b342007-03-13 10:07:47 +01001055
Bruce Cran38921822012-02-22 17:55:16 +00001056 /* \\.\ is the device namespace in Windows, where every file is
1057 * a block device */
1058 if (strncmp(f->file_name, "\\\\.\\", 4) == 0)
1059 f->filetype = FIO_TYPE_BD;
1060
Jens Axboeb30d3952010-02-06 23:36:26 +01001061 if (!stat(f->file_name, &sb)) {
Bruce Cran38921822012-02-22 17:55:16 +00001062 if (S_ISBLK(sb.st_mode))
Jens Axboeaf52b342007-03-13 10:07:47 +01001063 f->filetype = FIO_TYPE_BD;
1064 else if (S_ISCHR(sb.st_mode))
1065 f->filetype = FIO_TYPE_CHAR;
Jens Axboeb5605e92007-04-16 20:42:33 +02001066 else if (S_ISFIFO(sb.st_mode))
1067 f->filetype = FIO_TYPE_PIPE;
Jens Axboeaf52b342007-03-13 10:07:47 +01001068 }
1069}
1070
Jens Axboef29b25a2007-07-23 08:56:43 +02001071int add_file(struct thread_data *td, const char *fname)
Jens Axboeaf52b342007-03-13 10:07:47 +01001072{
Jens Axboe7b4e4fe2007-03-13 12:51:40 +01001073 int cur_files = td->files_index;
Jens Axboebd0ee742007-03-23 14:26:23 +01001074 char file_name[PATH_MAX];
Jens Axboeaf52b342007-03-13 10:07:47 +01001075 struct fio_file *f;
Jens Axboebd0ee742007-03-23 14:26:23 +01001076 int len = 0;
Jens Axboeaf52b342007-03-13 10:07:47 +01001077
Jens Axboeee56ad52008-02-01 10:30:20 +01001078 dprint(FD_FILE, "add file %s\n", fname);
1079
Jens Axboef17c4392008-03-01 18:40:46 +01001080 f = smalloc(sizeof(*f));
Jens Axboec48c0be2008-07-31 19:55:02 +02001081 if (!f) {
1082 log_err("fio: smalloc OOM\n");
1083 assert(0);
1084 }
Jens Axboe0b9d69e2009-09-11 22:29:54 +02001085
Jens Axboeaf52b342007-03-13 10:07:47 +01001086 f->fd = -1;
Jens Axboee6c4d732012-12-12 08:16:27 +01001087 f->shadow_fd = -1;
Jens Axboe33c48812013-01-21 09:46:06 -07001088 fio_file_reset(td, f);
Jens Axboebd0ee742007-03-23 14:26:23 +01001089
Jens Axboefc99bc02009-03-04 15:27:42 +01001090 if (td->files_size <= td->files_index) {
Jianpeng Ma1983e322013-01-10 13:19:27 +01001091 unsigned int new_size = td->o.nr_files + 1;
Jens Axboe126d65c2008-03-01 18:04:31 +01001092
Jens Axboefc99bc02009-03-04 15:27:42 +01001093 dprint(FD_FILE, "resize file array to %d files\n", new_size);
1094
1095 td->files = realloc(td->files, new_size * sizeof(f));
Jianpeng Mad537c082013-01-11 08:52:55 +01001096 if (td->files == NULL) {
1097 log_err("fio: realloc OOM\n");
1098 assert(0);
1099 }
Jens Axboed7df1d12013-03-20 19:57:01 -06001100 if (td->o.file_lock_mode != FILE_LOCK_NONE) {
1101 td->file_locks = realloc(td->file_locks, new_size);
1102 if (!td->file_locks) {
1103 log_err("fio: realloc OOM\n");
1104 assert(0);
1105 }
1106 td->file_locks[cur_files] = FILE_LOCK_NONE;
1107 }
Jens Axboefc99bc02009-03-04 15:27:42 +01001108 td->files_size = new_size;
1109 }
Jens Axboe8bb76792009-03-04 15:37:52 +01001110 td->files[cur_files] = f;
Shaohua Li89ac1d42012-06-11 08:56:32 +02001111 f->fileno = cur_files;
Jens Axboe126d65c2008-03-01 18:04:31 +01001112
Jens Axboe07eb79d2007-04-12 13:02:38 +02001113 /*
1114 * init function, io engine may not be loaded yet
1115 */
1116 if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO))
1117 f->real_file_size = -1ULL;
1118
Jens Axboebd0ee742007-03-23 14:26:23 +01001119 if (td->o.directory)
1120 len = sprintf(file_name, "%s/", td->o.directory);
1121
1122 sprintf(file_name + len, "%s", fname);
Jens Axboef17c4392008-03-01 18:40:46 +01001123 f->file_name = smalloc_strdup(file_name);
Jens Axboec48c0be2008-07-31 19:55:02 +02001124 if (!f->file_name) {
1125 log_err("fio: smalloc OOM\n");
1126 assert(0);
1127 }
Jens Axboe0b9d69e2009-09-11 22:29:54 +02001128
Jens Axboee3bab462007-03-13 11:22:09 +01001129 get_file_type(f);
Jens Axboeaf52b342007-03-13 10:07:47 +01001130
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001131 switch (td->o.file_lock_mode) {
1132 case FILE_LOCK_NONE:
1133 break;
1134 case FILE_LOCK_READWRITE:
Jens Axboed7df1d12013-03-20 19:57:01 -06001135 f->rwlock = fio_rwlock_init();
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001136 break;
1137 case FILE_LOCK_EXCLUSIVE:
Jens Axboe521da522012-08-02 11:21:36 +02001138 f->lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001139 break;
1140 default:
1141 log_err("fio: unknown lock mode: %d\n", td->o.file_lock_mode);
1142 assert(0);
1143 }
Jens Axboe29c13492008-03-01 19:25:20 +01001144
Jens Axboe7b4e4fe2007-03-13 12:51:40 +01001145 td->files_index++;
Jens Axboe15494412007-03-13 11:52:47 +01001146 if (f->filetype == FIO_TYPE_FILE)
1147 td->nr_normal_files++;
Jens Axboef29b25a2007-07-23 08:56:43 +02001148
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001149 dprint(FD_FILE, "file %p \"%s\" added at %d\n", f, f->file_name,
1150 cur_files);
Jens Axboe9efef3c2008-03-06 10:26:25 +01001151
Jens Axboef29b25a2007-07-23 08:56:43 +02001152 return cur_files;
Jens Axboeaf52b342007-03-13 10:07:47 +01001153}
Jens Axboe0ad920e2007-03-13 11:06:45 +01001154
Jens Axboe49ffb4a2010-08-24 15:01:37 +02001155int add_file_exclusive(struct thread_data *td, const char *fname)
1156{
1157 struct fio_file *f;
1158 unsigned int i;
1159
1160 for_each_file(td, f, i) {
1161 if (!strcmp(f->file_name, fname))
1162 return i;
1163 }
1164
1165 return add_file(td, fname);
1166}
1167
Jens Axboe0ad920e2007-03-13 11:06:45 +01001168void get_file(struct fio_file *f)
1169{
Jens Axboe8172fe92008-02-01 21:51:02 +01001170 dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references);
Jens Axboed6aed792009-06-03 08:41:15 +02001171 assert(fio_file_open(f));
Jens Axboe0ad920e2007-03-13 11:06:45 +01001172 f->references++;
1173}
1174
Jens Axboe6977bcd2008-03-01 15:55:36 +01001175int put_file(struct thread_data *td, struct fio_file *f)
Jens Axboe0ad920e2007-03-13 11:06:45 +01001176{
Jens Axboe98e1ac42008-03-06 11:56:48 +01001177 int f_ret = 0, ret = 0;
Jens Axboe6977bcd2008-03-01 15:55:36 +01001178
Jens Axboe8172fe92008-02-01 21:51:02 +01001179 dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references);
Jens Axboeee56ad52008-02-01 10:30:20 +01001180
Jens Axboe22a57ba2009-06-09 14:34:35 +02001181 if (!fio_file_open(f)) {
1182 assert(f->fd == -1);
Jens Axboe6977bcd2008-03-01 15:55:36 +01001183 return 0;
Jens Axboe22a57ba2009-06-09 14:34:35 +02001184 }
Jens Axboe0ad920e2007-03-13 11:06:45 +01001185
1186 assert(f->references);
1187 if (--f->references)
Jens Axboe6977bcd2008-03-01 15:55:36 +01001188 return 0;
Jens Axboe0ad920e2007-03-13 11:06:45 +01001189
Jens Axboed424d4d2007-04-17 09:05:10 +02001190 if (should_fsync(td) && td->o.fsync_on_close)
Jens Axboe98e1ac42008-03-06 11:56:48 +01001191 f_ret = fsync(f->fd);
Jens Axboeebb14152007-03-13 14:42:15 +01001192
Jens Axboe0ad920e2007-03-13 11:06:45 +01001193 if (td->io_ops->close_file)
Jens Axboe6977bcd2008-03-01 15:55:36 +01001194 ret = td->io_ops->close_file(td, f);
Jens Axboe1020a132007-03-29 11:15:06 +02001195
Jens Axboe98e1ac42008-03-06 11:56:48 +01001196 if (!ret)
Jens Axboea5fb4612008-05-28 10:54:01 +02001197 ret = f_ret;
Jens Axboe98e1ac42008-03-06 11:56:48 +01001198
Jens Axboe0ad920e2007-03-13 11:06:45 +01001199 td->nr_open_files--;
Jens Axboed6aed792009-06-03 08:41:15 +02001200 fio_file_clear_open(f);
Jens Axboe22a57ba2009-06-09 14:34:35 +02001201 assert(f->fd == -1);
Jens Axboe6977bcd2008-03-01 15:55:36 +01001202 return ret;
Jens Axboe0ad920e2007-03-13 11:06:45 +01001203}
Jens Axboebbf6b542007-03-13 15:28:55 +01001204
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001205void lock_file(struct thread_data *td, struct fio_file *f, enum fio_ddir ddir)
Jens Axboeb2bd2bd2008-03-01 18:19:52 +01001206{
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001207 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1208 return;
Jens Axboe29c13492008-03-01 19:25:20 +01001209
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001210 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
1211 if (ddir == DDIR_READ)
Jens Axboed7df1d12013-03-20 19:57:01 -06001212 fio_rwlock_read(f->rwlock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001213 else
Jens Axboed7df1d12013-03-20 19:57:01 -06001214 fio_rwlock_write(f->rwlock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001215 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1216 fio_mutex_down(f->lock);
1217
Jens Axboed7df1d12013-03-20 19:57:01 -06001218 td->file_locks[f->fileno] = td->o.file_lock_mode;
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001219}
1220
1221void unlock_file(struct thread_data *td, struct fio_file *f)
1222{
1223 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1224 return;
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001225
Jens Axboed7df1d12013-03-20 19:57:01 -06001226 if (td->o.file_lock_mode == FILE_LOCK_READWRITE)
1227 fio_rwlock_unlock(f->rwlock);
1228 else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001229 fio_mutex_up(f->lock);
Jens Axboed7df1d12013-03-20 19:57:01 -06001230
1231 td->file_locks[f->fileno] = FILE_LOCK_NONE;
Jens Axboeb2bd2bd2008-03-01 18:19:52 +01001232}
1233
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001234void unlock_file_all(struct thread_data *td, struct fio_file *f)
Jens Axboeb2bd2bd2008-03-01 18:19:52 +01001235{
Jens Axboed7df1d12013-03-20 19:57:01 -06001236 if (td->file_locks[f->fileno] != FILE_LOCK_NONE)
1237 unlock_file(td, f);
Jens Axboeb2bd2bd2008-03-01 18:19:52 +01001238}
1239
Jens Axboebbf6b542007-03-13 15:28:55 +01001240static int recurse_dir(struct thread_data *td, const char *dirname)
1241{
1242 struct dirent *dir;
1243 int ret = 0;
1244 DIR *D;
1245
1246 D = opendir(dirname);
1247 if (!D) {
Jens Axboe0ddb2702007-03-27 19:43:53 +02001248 char buf[FIO_VERROR_SIZE];
1249
Ken Raeburn98ffb8f2013-01-30 22:31:09 +01001250 snprintf(buf, FIO_VERROR_SIZE, "opendir(%s)", dirname);
Jens Axboe0ddb2702007-03-27 19:43:53 +02001251 td_verror(td, errno, buf);
Jens Axboebbf6b542007-03-13 15:28:55 +01001252 return 1;
1253 }
1254
1255 while ((dir = readdir(D)) != NULL) {
1256 char full_path[PATH_MAX];
1257 struct stat sb;
1258
Jens Axboee85b2b82007-03-14 09:39:06 +01001259 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
1260 continue;
Jens Axboe96d32d52007-03-14 09:16:09 +01001261
Bruce Cranb9fd7882012-02-20 17:01:46 +00001262 sprintf(full_path, "%s%s%s", dirname, FIO_OS_PATH_SEPARATOR, dir->d_name);
Jens Axboebbf6b542007-03-13 15:28:55 +01001263
1264 if (lstat(full_path, &sb) == -1) {
1265 if (errno != ENOENT) {
1266 td_verror(td, errno, "stat");
1267 return 1;
1268 }
1269 }
1270
1271 if (S_ISREG(sb.st_mode)) {
1272 add_file(td, full_path);
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001273 td->o.nr_files++;
Jens Axboebbf6b542007-03-13 15:28:55 +01001274 continue;
1275 }
Jens Axboe0ddb2702007-03-27 19:43:53 +02001276 if (!S_ISDIR(sb.st_mode))
1277 continue;
Jens Axboebbf6b542007-03-13 15:28:55 +01001278
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001279 ret = recurse_dir(td, full_path);
1280 if (ret)
Jens Axboebbf6b542007-03-13 15:28:55 +01001281 break;
1282 }
1283
1284 closedir(D);
1285 return ret;
1286}
1287
1288int add_dir_files(struct thread_data *td, const char *path)
1289{
Jens Axboe0ddb2702007-03-27 19:43:53 +02001290 int ret = recurse_dir(td, path);
1291
1292 if (!ret)
1293 log_info("fio: opendir added %d files\n", td->o.nr_files);
1294
1295 return ret;
Jens Axboebbf6b542007-03-13 15:28:55 +01001296}
Jens Axboecade3ef2007-03-23 15:29:45 +01001297
1298void dup_files(struct thread_data *td, struct thread_data *org)
1299{
1300 struct fio_file *f;
1301 unsigned int i;
Jens Axboe9efef3c2008-03-06 10:26:25 +01001302
1303 dprint(FD_FILE, "dup files: %d\n", org->files_index);
Jens Axboecade3ef2007-03-23 15:29:45 +01001304
1305 if (!org->files)
1306 return;
1307
Jens Axboe9efef3c2008-03-06 10:26:25 +01001308 td->files = malloc(org->files_index * sizeof(f));
Jens Axboecade3ef2007-03-23 15:29:45 +01001309
Jens Axboed7df1d12013-03-20 19:57:01 -06001310 if (td->o.file_lock_mode != FILE_LOCK_NONE)
1311 td->file_locks = malloc(org->files_index);
1312
Jens Axboe9efef3c2008-03-06 10:26:25 +01001313 for_each_file(org, f, i) {
Jens Axboeb0fe4212008-03-01 18:22:27 +01001314 struct fio_file *__f;
1315
Jens Axboef17c4392008-03-01 18:40:46 +01001316 __f = smalloc(sizeof(*__f));
Jens Axboec48c0be2008-07-31 19:55:02 +02001317 if (!__f) {
1318 log_err("fio: smalloc OOM\n");
1319 assert(0);
1320 }
Jens Axboe22a57ba2009-06-09 14:34:35 +02001321 __f->fd = -1;
Jens Axboe33c48812013-01-21 09:46:06 -07001322 fio_file_reset(td, __f);
Jens Axboe0b9d69e2009-09-11 22:29:54 +02001323
Aaron Carrollbc3456f2008-06-25 09:20:37 +02001324 if (f->file_name) {
Jens Axboef17c4392008-03-01 18:40:46 +01001325 __f->file_name = smalloc_strdup(f->file_name);
Jens Axboec48c0be2008-07-31 19:55:02 +02001326 if (!__f->file_name) {
1327 log_err("fio: smalloc OOM\n");
1328 assert(0);
1329 }
Jens Axboe0b9d69e2009-09-11 22:29:54 +02001330
Aaron Carrollbc3456f2008-06-25 09:20:37 +02001331 __f->filetype = f->filetype;
1332 }
Jens Axboeb0fe4212008-03-01 18:22:27 +01001333
1334 td->files[i] = __f;
Jens Axboecade3ef2007-03-23 15:29:45 +01001335 }
1336}
Jens Axboef29b25a2007-07-23 08:56:43 +02001337
1338/*
1339 * Returns the index that matches the filename, or -1 if not there
1340 */
1341int get_fileno(struct thread_data *td, const char *fname)
1342{
1343 struct fio_file *f;
1344 unsigned int i;
1345
1346 for_each_file(td, f, i)
1347 if (!strcmp(f->file_name, fname))
1348 return i;
1349
1350 return -1;
1351}
1352
1353/*
1354 * For log usage, where we add/open/close files automatically
1355 */
1356void free_release_files(struct thread_data *td)
1357{
1358 close_files(td);
1359 td->files_index = 0;
1360 td->nr_normal_files = 0;
1361}
Jens Axboe33c48812013-01-21 09:46:06 -07001362
1363void fio_file_reset(struct thread_data *td, struct fio_file *f)
1364{
1365 f->last_pos = f->file_offset;
1366 f->last_start = -1ULL;
1367 if (f->io_axmap)
1368 axmap_reset(f->io_axmap);
1369 if (td->o.random_generator == FIO_RAND_GEN_LFSR)
1370 lfsr_reset(&f->lfsr, td->rand_seeds[FIO_RAND_BLOCK_OFF]);
1371}