blob: c488eb471caa8a2d442c8cf58a1c203ff18309e7 [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 Axboe53cdc682006-10-18 11:50:58 +020016
Eric Gourioua596f042011-06-17 09:11:45 +020017#ifdef FIO_HAVE_LINUX_FALLOCATE
18#include <linux/falloc.h>
19#endif
20
Jens Axboe7172cfe2007-07-23 14:36:00 +020021static int root_warn;
22
Jens Axboec592b9f2009-06-03 09:29:28 +020023static inline void clear_error(struct thread_data *td)
24{
25 td->error = 0;
26 td->verror[0] = '\0';
27}
28
Jens Axboe3baddf22008-04-04 11:10:30 +020029/*
30 * Leaves f->fd open on success, caller must close
31 */
Jens Axboe7bb48f82007-03-27 15:30:28 +020032static int extend_file(struct thread_data *td, struct fio_file *f)
Jens Axboe25205e92006-10-19 20:50:14 +020033{
Jens Axboeea443652007-03-29 14:52:13 +020034 int r, new_layout = 0, unlink_file = 0, flags;
Jens Axboe25205e92006-10-19 20:50:14 +020035 unsigned long long left;
36 unsigned int bs;
37 char *b;
Jens Axboeb2a15192006-10-18 14:10:42 +020038
Jens Axboe4241ea82007-09-12 08:18:36 +020039 if (read_only) {
40 log_err("fio: refusing extend of file due to read-only\n");
41 return 0;
42 }
43
Jens Axboe507a7022007-03-27 15:52:46 +020044 /*
45 * check if we need to lay the file out complete again. fio
46 * does that for operations involving reads, or for writes
47 * where overwrite is set
48 */
Jens Axboeffdfabd2008-03-26 09:18:14 +010049 if (td_read(td) || (td_write(td) && td->o.overwrite) ||
50 (td_write(td) && td->io_ops->flags & FIO_NOEXTEND))
Jens Axboe507a7022007-03-27 15:52:46 +020051 new_layout = 1;
Jens Axboeea443652007-03-29 14:52:13 +020052 if (td_write(td) && !td->o.overwrite)
53 unlink_file = 1;
Jens Axboe507a7022007-03-27 15:52:46 +020054
Jens Axboe6ae1f572008-02-21 10:20:00 +010055 if (unlink_file || new_layout) {
Jens Axboebd199f22008-03-26 09:57:18 +010056 dprint(FD_FILE, "layout unlink %s\n", f->file_name);
Zhang, Yanmin982016d2008-02-26 15:35:52 +010057 if ((unlink(f->file_name) < 0) && (errno != ENOENT)) {
Jens Axboe7bb48f82007-03-27 15:30:28 +020058 td_verror(td, errno, "unlink");
59 return 1;
60 }
61 }
62
Jens Axboe507a7022007-03-27 15:52:46 +020063 flags = O_WRONLY | O_CREAT;
64 if (new_layout)
65 flags |= O_TRUNC;
66
Jens Axboeee56ad52008-02-01 10:30:20 +010067 dprint(FD_FILE, "open file %s, flags %x\n", f->file_name, flags);
Jens Axboe507a7022007-03-27 15:52:46 +020068 f->fd = open(f->file_name, flags, 0644);
Jens Axboe53cdc682006-10-18 11:50:58 +020069 if (f->fd < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +010070 td_verror(td, errno, "open");
Jens Axboe53cdc682006-10-18 11:50:58 +020071 return 1;
72 }
73
Jens Axboe7bc8c2c2010-01-28 11:31:31 +010074#ifdef FIO_HAVE_FALLOCATE
Eric Gourioua596f042011-06-17 09:11:45 +020075 if (!td->o.fill_device) {
76 switch (td->o.fallocate_mode) {
77 case FIO_FALLOCATE_NONE:
78 break;
79 case FIO_FALLOCATE_POSIX:
80 dprint(FD_FILE, "posix_fallocate file %s size %llu\n",
81 f->file_name, f->real_file_size);
Jens Axboe7bc8c2c2010-01-28 11:31:31 +010082
Eric Gourioua596f042011-06-17 09:11:45 +020083 r = posix_fallocate(f->fd, 0, f->real_file_size);
84 if (r > 0) {
85 log_err("fio: posix_fallocate fails: %s\n",
86 strerror(r));
87 }
88 break;
89#ifdef FIO_HAVE_LINUX_FALLOCATE
90 case FIO_FALLOCATE_KEEP_SIZE:
91 dprint(FD_FILE,
92 "fallocate(FALLOC_FL_KEEP_SIZE) "
93 "file %s size %llu\n",
94 f->file_name, f->real_file_size);
95
96 r = fallocate(f->fd, FALLOC_FL_KEEP_SIZE, 0,
97 f->real_file_size);
98 if (r != 0) {
99 td_verror(td, errno, "fallocate");
100 }
101 break;
102#endif /* FIO_HAVE_LINUX_FALLOCATE */
103 default:
104 log_err("fio: unknown fallocate mode: %d\n",
105 td->o.fallocate_mode);
106 assert(0);
Jens Axboe7bc8c2c2010-01-28 11:31:31 +0100107 }
108 }
Eric Gourioua596f042011-06-17 09:11:45 +0200109#endif /* FIO_HAVE_FALLOCATE */
Bruce Cran9b836562011-01-08 19:49:54 +0100110
Shawn Lewisfc74ac12008-01-11 09:45:11 +0100111 if (!new_layout)
112 goto done;
113
Jens Axboe5e0074c2009-06-02 21:40:09 +0200114 /*
115 * The size will be -1ULL when fill_device is used, so don't truncate
116 * or fallocate this file, just write it
117 */
118 if (!td->o.fill_device) {
119 dprint(FD_FILE, "truncate file %s, size %llu\n", f->file_name,
Jens Axboeee56ad52008-02-01 10:30:20 +0100120 f->real_file_size);
Jens Axboe5e0074c2009-06-02 21:40:09 +0200121 if (ftruncate(f->fd, f->real_file_size) == -1) {
122 td_verror(td, errno, "ftruncate");
123 goto err;
124 }
Jens Axboe5e0074c2009-06-02 21:40:09 +0200125 }
Jens Axboe40f82982006-10-25 11:21:05 +0200126
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100127 b = malloc(td->o.max_bs[DDIR_WRITE]);
128 memset(b, 0, td->o.max_bs[DDIR_WRITE]);
Jens Axboe53cdc682006-10-18 11:50:58 +0200129
Jens Axboe7bb48f82007-03-27 15:30:28 +0200130 left = f->real_file_size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200131 while (left && !td->terminate) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100132 bs = td->o.max_bs[DDIR_WRITE];
Jens Axboe53cdc682006-10-18 11:50:58 +0200133 if (bs > left)
134 bs = left;
135
136 r = write(f->fd, b, bs);
137
Jens Axboe5e0074c2009-06-02 21:40:09 +0200138 if (r > 0) {
139 left -= r;
Jens Axboe53cdc682006-10-18 11:50:58 +0200140 continue;
141 } else {
Jens Axboe5e0074c2009-06-02 21:40:09 +0200142 if (r < 0) {
143 int __e = errno;
144
145 if (__e == ENOSPC) {
146 if (td->o.fill_device)
147 break;
148 log_info("fio: ENOSPC on laying out "
149 "file, stopping\n");
150 break;
151 }
Jens Axboee1161c32007-02-22 19:36:48 +0100152 td_verror(td, errno, "write");
Jens Axboe5e0074c2009-06-02 21:40:09 +0200153 } else
Jens Axboee1161c32007-02-22 19:36:48 +0100154 td_verror(td, EIO, "write");
Jens Axboe53cdc682006-10-18 11:50:58 +0200155
156 break;
157 }
158 }
159
Jens Axboeffdfabd2008-03-26 09:18:14 +0100160 if (td->terminate) {
161 dprint(FD_FILE, "terminate unlink %s\n", f->file_name);
Jens Axboe53cdc682006-10-18 11:50:58 +0200162 unlink(f->file_name);
Jens Axboeffdfabd2008-03-26 09:18:14 +0100163 } else if (td->o.create_fsync) {
Jens Axboe98e1ac42008-03-06 11:56:48 +0100164 if (fsync(f->fd) < 0) {
165 td_verror(td, errno, "fsync");
166 goto err;
167 }
168 }
Jens Axboe0d1cd202009-06-05 22:11:52 +0200169 if (td->o.fill_device && !td_write(td)) {
Jens Axboed6aed792009-06-03 08:41:15 +0200170 fio_file_clear_size_known(f);
Jens Axboe5e0074c2009-06-02 21:40:09 +0200171 if (td_io_get_file_size(td, f))
172 goto err;
173 if (f->io_size > f->real_file_size)
174 f->io_size = f->real_file_size;
175 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200176
177 free(b);
Jens Axboe507a7022007-03-27 15:52:46 +0200178done:
Jens Axboe53cdc682006-10-18 11:50:58 +0200179 return 0;
180err:
181 close(f->fd);
182 f->fd = -1;
183 return 1;
184}
185
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200186static int pre_read_file(struct thread_data *td, struct fio_file *f)
187{
Jens Axboeb0f65862009-05-20 11:52:15 +0200188 int r, did_open = 0, old_runstate;
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200189 unsigned long long left;
190 unsigned int bs;
191 char *b;
192
Jens Axboe9c0d2242009-07-01 12:26:28 +0200193 if (td->io_ops->flags & FIO_PIPEIO)
194 return 0;
195
Jens Axboed6aed792009-06-03 08:41:15 +0200196 if (!fio_file_open(f)) {
Jens Axboeb0f65862009-05-20 11:52:15 +0200197 if (td->io_ops->open_file(td, f)) {
198 log_err("fio: cannot pre-read, failed to open file\n");
199 return 1;
200 }
201 did_open = 1;
202 }
203
204 old_runstate = td->runstate;
205 td_set_runstate(td, TD_PRE_READING);
206
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200207 bs = td->o.max_bs[DDIR_READ];
208 b = malloc(bs);
209 memset(b, 0, bs);
210
211 lseek(f->fd, f->file_offset, SEEK_SET);
212 left = f->io_size;
213
214 while (left && !td->terminate) {
215 if (bs > left)
216 bs = left;
217
218 r = read(f->fd, b, bs);
219
220 if (r == (int) bs) {
221 left -= bs;
222 continue;
223 } else {
224 td_verror(td, EIO, "pre_read");
225 break;
226 }
227 }
228
Jens Axboeb0f65862009-05-20 11:52:15 +0200229 td_set_runstate(td, old_runstate);
230
231 if (did_open)
232 td->io_ops->close_file(td, f);
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200233 free(b);
234 return 0;
235}
236
Jens Axboe7bb48f82007-03-27 15:30:28 +0200237static unsigned long long get_rand_file_size(struct thread_data *td)
Jens Axboe9c60ce62007-03-15 09:14:47 +0100238{
Jens Axboedc873b62008-06-04 20:13:04 +0200239 unsigned long long ret, sized;
Jens Axboe1294c3e2011-05-11 08:15:18 +0200240 unsigned long r;
Jens Axboe9c60ce62007-03-15 09:14:47 +0100241
Jens Axboe4c07ad82011-03-28 09:51:09 +0200242 if (td->o.use_os_rand) {
243 r = os_random_long(&td->file_size_state);
244 sized = td->o.file_size_high - td->o.file_size_low;
245 ret = (unsigned long long) ((double) sized * (r / (OS_RAND_MAX + 1.0)));
246 } else {
247 r = __rand(&td->__file_size_state);
248 sized = td->o.file_size_high - td->o.file_size_low;
249 ret = (unsigned long long) ((double) sized * (r / (FRAND_MAX + 1.0)));
250 }
251
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100252 ret += td->o.file_size_low;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100253 ret -= (ret % td->o.rw_min_bs);
Jens Axboe9c60ce62007-03-15 09:14:47 +0100254 return ret;
255}
256
Jens Axboe53cdc682006-10-18 11:50:58 +0200257static int file_size(struct thread_data *td, struct fio_file *f)
258{
259 struct stat st;
260
Jens Axboedf9c26b2009-03-05 10:13:58 +0100261 if (stat(f->file_name, &st) == -1) {
Jens Axboe7bb48f82007-03-27 15:30:28 +0200262 td_verror(td, errno, "fstat");
263 return 1;
264 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200265
Jens Axboe7bb48f82007-03-27 15:30:28 +0200266 f->real_file_size = st.st_size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200267 return 0;
268}
269
270static int bdev_size(struct thread_data *td, struct fio_file *f)
271{
Bruce Cran9b836562011-01-08 19:49:54 +0100272 unsigned long long bytes = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200273 int r;
274
Jens Axboedf9c26b2009-03-05 10:13:58 +0100275 if (td->io_ops->open_file(td, f)) {
276 log_err("fio: failed opening blockdev %s for size check\n",
277 f->file_name);
278 return 1;
279 }
280
Bruce Cranecc314b2011-01-04 10:59:30 +0100281 r = blockdev_size(f, &bytes);
Jens Axboe53cdc682006-10-18 11:50:58 +0200282 if (r) {
Jens Axboee1161c32007-02-22 19:36:48 +0100283 td_verror(td, r, "blockdev_size");
Jens Axboedf9c26b2009-03-05 10:13:58 +0100284 goto err;
Jens Axboe53cdc682006-10-18 11:50:58 +0200285 }
286
Jens Axboe7ab077a2009-02-02 15:07:37 +0100287 if (!bytes) {
288 log_err("%s: zero sized block device?\n", f->file_name);
Jens Axboedf9c26b2009-03-05 10:13:58 +0100289 goto err;
Jens Axboe7ab077a2009-02-02 15:07:37 +0100290 }
291
Jens Axboe53cdc682006-10-18 11:50:58 +0200292 f->real_file_size = bytes;
Jens Axboe22a57ba2009-06-09 14:34:35 +0200293 td->io_ops->close_file(td, f);
Jens Axboe53cdc682006-10-18 11:50:58 +0200294 return 0;
Jens Axboedf9c26b2009-03-05 10:13:58 +0100295err:
296 td->io_ops->close_file(td, f);
297 return 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200298}
299
Jens Axboe4ccdccd2010-06-24 10:27:22 +0200300static int char_size(struct thread_data *td, struct fio_file *f)
301{
302#ifdef FIO_HAVE_CHARDEV_SIZE
Bruce Cran9b836562011-01-08 19:49:54 +0100303 unsigned long long bytes = 0;
Jens Axboe4ccdccd2010-06-24 10:27:22 +0200304 int r;
305
306 if (td->io_ops->open_file(td, f)) {
307 log_err("fio: failed opening blockdev %s for size check\n",
308 f->file_name);
309 return 1;
310 }
311
Bruce Cranecc314b2011-01-04 10:59:30 +0100312 r = chardev_size(f, &bytes);
Jens Axboe4ccdccd2010-06-24 10:27:22 +0200313 if (r) {
314 td_verror(td, r, "chardev_size");
315 goto err;
316 }
317
318 if (!bytes) {
319 log_err("%s: zero sized char device?\n", f->file_name);
320 goto err;
321 }
322
323 f->real_file_size = bytes;
324 td->io_ops->close_file(td, f);
325 return 0;
326err:
327 td->io_ops->close_file(td, f);
328 return 1;
329#else
330 f->real_file_size = -1ULL;
331 return 0;
332#endif
333}
334
Jens Axboe53cdc682006-10-18 11:50:58 +0200335static int get_file_size(struct thread_data *td, struct fio_file *f)
336{
337 int ret = 0;
338
Jens Axboed6aed792009-06-03 08:41:15 +0200339 if (fio_file_size_known(f))
Jens Axboe409b3412007-03-28 09:57:01 +0200340 return 0;
341
Jens Axboe7bb48f82007-03-27 15:30:28 +0200342 if (f->filetype == FIO_TYPE_FILE)
343 ret = file_size(td, f);
344 else if (f->filetype == FIO_TYPE_BD)
Jens Axboe53cdc682006-10-18 11:50:58 +0200345 ret = bdev_size(td, f);
Jens Axboe4ccdccd2010-06-24 10:27:22 +0200346 else if (f->filetype == FIO_TYPE_CHAR)
347 ret = char_size(td, f);
Jens Axboe53cdc682006-10-18 11:50:58 +0200348 else
349 f->real_file_size = -1;
350
351 if (ret)
352 return ret;
353
354 if (f->file_offset > f->real_file_size) {
Bruce Cran0f2152c2010-12-15 10:33:03 +0100355 log_err("%s: offset extends end (%llu > %llu)\n", td->o.name,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100356 f->file_offset, f->real_file_size);
Jens Axboe53cdc682006-10-18 11:50:58 +0200357 return 1;
358 }
359
Jens Axboed6aed792009-06-03 08:41:15 +0200360 fio_file_set_size_known(f);
Jens Axboe53cdc682006-10-18 11:50:58 +0200361 return 0;
362}
363
Jens Axboe3baddf22008-04-04 11:10:30 +0200364static int __file_invalidate_cache(struct thread_data *td, struct fio_file *f,
365 unsigned long long off,
366 unsigned long long len)
Jens Axboee5b401d2006-10-18 16:03:40 +0200367{
368 int ret = 0;
369
Jens Axboe5e0074c2009-06-02 21:40:09 +0200370 if (len == -1ULL)
Jens Axboe3baddf22008-04-04 11:10:30 +0200371 len = f->io_size;
372 if (off == -1ULL)
373 off = f->file_offset;
Jens Axboeee56ad52008-02-01 10:30:20 +0100374
Jens Axboe0d1cd202009-06-05 22:11:52 +0200375 if (len == -1ULL || off == -1ULL)
376 return 0;
377
Jens Axboe3baddf22008-04-04 11:10:30 +0200378 dprint(FD_IO, "invalidate cache %s: %llu/%llu\n", f->file_name, off,
379 len);
Jens Axboeb5af8292007-03-08 12:43:13 +0100380
Jens Axboee5b401d2006-10-18 16:03:40 +0200381 /*
382 * FIXME: add blockdev flushing too
383 */
Jens Axboea1c58072009-08-04 23:17:02 +0200384 if (f->mmap_ptr) {
Bruce Cran03e20d62011-01-02 20:14:54 +0100385 ret = posix_madvise(f->mmap_ptr, f->mmap_sz, POSIX_MADV_DONTNEED);
Jens Axboea1c58072009-08-04 23:17:02 +0200386#ifdef FIO_MADV_FREE
Bruce Cran03e20d62011-01-02 20:14:54 +0100387 (void) posix_madvise(f->mmap_ptr, f->mmap_sz, FIO_MADV_FREE);
Jens Axboea1c58072009-08-04 23:17:02 +0200388#endif
389 } else if (f->filetype == FIO_TYPE_FILE) {
Bruce Cranecc314b2011-01-04 10:59:30 +0100390 ret = posix_fadvise(f->fd, off, len, POSIX_FADV_DONTNEED);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100391 } else if (f->filetype == FIO_TYPE_BD) {
Bruce Cranecc314b2011-01-04 10:59:30 +0100392 ret = blockdev_invalidate_cache(f);
Jens Axboe7e0e25c2007-03-27 12:49:56 +0200393 if (ret < 0 && errno == EACCES && geteuid()) {
Jens Axboe7172cfe2007-07-23 14:36:00 +0200394 if (!root_warn) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100395 log_err("fio: only root may flush block "
396 "devices. Cache flush bypassed!\n");
Jens Axboe7172cfe2007-07-23 14:36:00 +0200397 root_warn = 1;
398 }
Jens Axboe7e0e25c2007-03-27 12:49:56 +0200399 ret = 0;
400 }
Jens Axboeb5605e92007-04-16 20:42:33 +0200401 } else if (f->filetype == FIO_TYPE_CHAR || f->filetype == FIO_TYPE_PIPE)
Jens Axboee5b401d2006-10-18 16:03:40 +0200402 ret = 0;
403
404 if (ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100405 td_verror(td, errno, "invalidate_cache");
Jens Axboee5b401d2006-10-18 16:03:40 +0200406 return 1;
Jens Axboe3baddf22008-04-04 11:10:30 +0200407 } else if (ret > 0) {
408 td_verror(td, ret, "invalidate_cache");
409 return 1;
Jens Axboee5b401d2006-10-18 16:03:40 +0200410 }
411
Jens Axboead2da602007-02-26 12:33:27 +0100412 return ret;
Jens Axboe3baddf22008-04-04 11:10:30 +0200413
414}
415
416int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
417{
Jens Axboed6aed792009-06-03 08:41:15 +0200418 if (!fio_file_open(f))
Jens Axboea5fb4612008-05-28 10:54:01 +0200419 return 0;
420
Jens Axboe5e0074c2009-06-02 21:40:09 +0200421 return __file_invalidate_cache(td, f, -1ULL, -1ULL);
Jens Axboee5b401d2006-10-18 16:03:40 +0200422}
423
Jens Axboe6977bcd2008-03-01 15:55:36 +0100424int generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
Jens Axboe53cdc682006-10-18 11:50:58 +0200425{
Jens Axboe6977bcd2008-03-01 15:55:36 +0100426 int ret = 0;
427
Jens Axboeee56ad52008-02-01 10:30:20 +0100428 dprint(FD_FILE, "fd close %s\n", f->file_name);
Jens Axboe4906e0b2008-03-01 18:59:51 +0100429
430 remove_file_hash(f);
431
Jens Axboe6977bcd2008-03-01 15:55:36 +0100432 if (close(f->fd) < 0)
433 ret = errno;
434
Jens Axboeb5af8292007-03-08 12:43:13 +0100435 f->fd = -1;
Jens Axboe6977bcd2008-03-01 15:55:36 +0100436 return ret;
Jens Axboe53cdc682006-10-18 11:50:58 +0200437}
438
Dmitry Monakhov1ccc6dc2012-09-19 23:22:53 +0400439int file_lookup_open(struct fio_file *f, int flags)
Jens Axboe53cdc682006-10-18 11:50:58 +0200440{
Jens Axboe29c13492008-03-01 19:25:20 +0100441 struct fio_file *__f;
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100442 int from_hash;
443
444 __f = lookup_file_hash(f->file_name);
445 if (__f) {
Jens Axboe9efef3c2008-03-06 10:26:25 +0100446 dprint(FD_FILE, "found file in hash %s\n", f->file_name);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100447 /*
448 * racy, need the __f->lock locked
449 */
450 f->lock = __f->lock;
451 f->lock_owner = __f->lock_owner;
452 f->lock_batch = __f->lock_batch;
453 f->lock_ddir = __f->lock_ddir;
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100454 from_hash = 1;
455 } else {
Jens Axboe9efef3c2008-03-06 10:26:25 +0100456 dprint(FD_FILE, "file not found in hash %s\n", f->file_name);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100457 from_hash = 0;
458 }
459
Jens Axboee8670ef2008-03-06 12:06:30 +0100460 f->fd = open(f->file_name, flags, 0600);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100461 return from_hash;
462}
463
464int generic_open_file(struct thread_data *td, struct fio_file *f)
465{
Jens Axboe66159822007-04-16 21:54:24 +0200466 int is_std = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200467 int flags = 0;
Jens Axboe29c13492008-03-01 19:25:20 +0100468 int from_hash = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200469
Jens Axboeee56ad52008-02-01 10:30:20 +0100470 dprint(FD_FILE, "fd open %s\n", f->file_name);
471
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200472 if (td_trim(td) && f->filetype != FIO_TYPE_BD) {
473 log_err("fio: trim only applies to block device\n");
474 return 1;
475 }
476
Jens Axboe66159822007-04-16 21:54:24 +0200477 if (!strcmp(f->file_name, "-")) {
478 if (td_rw(td)) {
479 log_err("fio: can't read/write to stdin/out\n");
480 return 1;
481 }
482 is_std = 1;
Jens Axboece98fa62007-04-17 13:27:32 +0200483
484 /*
485 * move output logging to stderr, if we are writing to stdout
486 */
487 if (td_write(td))
488 f_out = stderr;
Jens Axboe66159822007-04-16 21:54:24 +0200489 }
490
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200491 if (td_trim(td))
492 goto skip_flags;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100493 if (td->o.odirect)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100494 flags |= OS_O_DIRECT;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100495 if (td->o.sync_io)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100496 flags |= O_SYNC;
Jens Axboe814452b2009-03-04 12:53:13 +0100497 if (td->o.create_on_open)
498 flags |= O_CREAT;
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200499skip_flags:
500 if (f->filetype != FIO_TYPE_FILE)
501 flags |= FIO_O_NOATIME;
Jens Axboe7abf8332006-11-23 15:01:19 +0100502
Aaron Carroll056f3452007-11-26 09:08:53 +0100503open_again:
Jens Axboe660a1cb2007-04-17 15:37:47 +0200504 if (td_write(td)) {
Jens Axboe17308152008-03-07 13:42:31 +0100505 if (!read_only)
506 flags |= O_RDWR;
Jens Axboe53cdc682006-10-18 11:50:58 +0200507
Jens Axboeaf52b342007-03-13 10:07:47 +0100508 if (f->filetype == FIO_TYPE_FILE)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100509 flags |= O_CREAT;
Jens Axboe2fd233b2007-03-02 08:44:25 +0100510
Jens Axboe66159822007-04-16 21:54:24 +0200511 if (is_std)
512 f->fd = dup(STDOUT_FILENO);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100513 else
514 from_hash = file_lookup_open(f, flags);
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200515 } else if (td_read(td)) {
Jens Axboe4241ea82007-09-12 08:18:36 +0200516 if (f->filetype == FIO_TYPE_CHAR && !read_only)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100517 flags |= O_RDWR;
518 else
519 flags |= O_RDONLY;
520
Jens Axboe66159822007-04-16 21:54:24 +0200521 if (is_std)
522 f->fd = dup(STDIN_FILENO);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100523 else
524 from_hash = file_lookup_open(f, flags);
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200525 } else { //td trim
526 flags |= O_RDWR;
527 from_hash = file_lookup_open(f, flags);
Jens Axboe53cdc682006-10-18 11:50:58 +0200528 }
529
530 if (f->fd == -1) {
Jens Axboee4e33252007-03-21 13:07:54 +0100531 char buf[FIO_VERROR_SIZE];
Jens Axboee1161c32007-02-22 19:36:48 +0100532 int __e = errno;
533
Jens Axboe835d9b92009-06-09 15:23:38 +0200534 if (__e == EPERM && (flags & FIO_O_NOATIME)) {
Jens Axboe5921e802008-05-30 15:02:38 +0200535 flags &= ~FIO_O_NOATIME;
Aaron Carroll056f3452007-11-26 09:08:53 +0100536 goto open_again;
537 }
538
Jens Axboee8670ef2008-03-06 12:06:30 +0100539 snprintf(buf, sizeof(buf) - 1, "open(%s)", f->file_name);
Jens Axboee4e33252007-03-21 13:07:54 +0100540
Jens Axboea93c5f02012-06-11 08:53:53 +0200541 if (__e == EINVAL && (flags & OS_O_DIRECT)) {
542 log_err("fio: looks like your file system does not " \
543 "support direct=1/buffered=0\n");
544 }
545
Jens Axboee4e33252007-03-21 13:07:54 +0100546 td_verror(td, __e, buf);
Jens Axboe53cdc682006-10-18 11:50:58 +0200547 }
548
Jens Axboe29c13492008-03-01 19:25:20 +0100549 if (!from_hash && f->fd != -1) {
550 if (add_file_hash(f)) {
Jens Axboe3f0ca9b2011-05-24 21:36:24 +0200551 int fio_unused ret;
Jens Axboe29c13492008-03-01 19:25:20 +0100552
553 /*
554 * OK to ignore, we haven't done anything with it
555 */
556 ret = generic_close_file(td, f);
557 goto open_again;
558 }
559 }
Jens Axboe4906e0b2008-03-01 18:59:51 +0100560
Jens Axboe53cdc682006-10-18 11:50:58 +0200561 return 0;
Jens Axboeb5af8292007-03-08 12:43:13 +0100562}
563
Jens Axboedf9c26b2009-03-05 10:13:58 +0100564int generic_get_file_size(struct thread_data *td, struct fio_file *f)
Jens Axboe21972cd2006-11-23 12:39:16 +0100565{
Jens Axboedf9c26b2009-03-05 10:13:58 +0100566 return get_file_size(td, f);
Jens Axboe21972cd2006-11-23 12:39:16 +0100567}
568
Jens Axboe7bb48f82007-03-27 15:30:28 +0200569/*
570 * open/close all files, so that ->real_file_size gets set
571 */
Jens Axboebab3fd52007-04-02 10:34:18 +0200572static int get_file_sizes(struct thread_data *td)
Jens Axboe53cdc682006-10-18 11:50:58 +0200573{
574 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100575 unsigned int i;
Jens Axboebab3fd52007-04-02 10:34:18 +0200576 int err = 0;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200577
578 for_each_file(td, f, i) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100579 dprint(FD_FILE, "get file size for %p/%d/%p\n", f, i,
580 f->file_name);
Jens Axboe9efef3c2008-03-06 10:26:25 +0100581
Jens Axboe99a47c62009-04-07 22:20:56 +0200582 if (td_io_get_file_size(td, f)) {
Jens Axboe40b44f42007-04-11 12:43:35 +0200583 if (td->error != ENOENT) {
584 log_err("%s\n", td->verror);
585 err = 1;
586 }
Jens Axboe541d66d2007-03-27 19:50:11 +0200587 clear_error(td);
Jens Axboe07eb79d2007-04-12 13:02:38 +0200588 }
Jens Axboe409b3412007-03-28 09:57:01 +0200589
590 if (f->real_file_size == -1ULL && td->o.size)
591 f->real_file_size = td->o.size / td->o.nr_files;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200592 }
Jens Axboebab3fd52007-04-02 10:34:18 +0200593
594 return err;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200595}
596
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200597struct fio_mount {
598 struct flist_head list;
599 const char *base;
600 char __base[256];
601 unsigned int key;
602};
603
604/*
605 * Get free number of bytes for each file on each unique mount.
606 */
607static unsigned long long get_fs_free_counts(struct thread_data *td)
608{
609 struct flist_head *n, *tmp;
Jens Axboe68b03162010-06-24 09:22:41 +0200610 unsigned long long ret = 0;
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200611 struct fio_mount *fm;
612 FLIST_HEAD(list);
613 struct fio_file *f;
614 unsigned int i;
615
616 for_each_file(td, f, i) {
617 struct stat sb;
618 char buf[256];
619
Jens Axboe4ccdccd2010-06-24 10:27:22 +0200620 if (f->filetype == FIO_TYPE_BD || f->filetype == FIO_TYPE_CHAR) {
621 if (f->real_file_size != -1ULL)
622 ret += f->real_file_size;
Jens Axboe68b03162010-06-24 09:22:41 +0200623 continue;
624 } else if (f->filetype != FIO_TYPE_FILE)
625 continue;
626
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200627 strcpy(buf, f->file_name);
628
629 if (stat(buf, &sb) < 0) {
630 if (errno != ENOENT)
631 break;
632 strcpy(buf, ".");
633 if (stat(buf, &sb) < 0)
634 break;
635 }
636
637 fm = NULL;
638 flist_for_each(n, &list) {
639 fm = flist_entry(n, struct fio_mount, list);
640 if (fm->key == sb.st_dev)
641 break;
642
643 fm = NULL;
644 }
645
646 if (fm)
647 continue;
648
649 fm = malloc(sizeof(*fm));
650 strcpy(fm->__base, buf);
651 fm->base = basename(fm->__base);
652 fm->key = sb.st_dev;
653 flist_add(&fm->list, &list);
654 }
655
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200656 flist_for_each_safe(n, tmp, &list) {
657 unsigned long long sz;
658
659 fm = flist_entry(n, struct fio_mount, list);
660 flist_del(&fm->list);
661
662 sz = get_fs_size(fm->base);
663 if (sz && sz != -1ULL)
664 ret += sz;
665
666 free(fm);
667 }
668
669 return ret;
670}
671
Dan Ehrenbergce95d652012-08-16 08:58:21 +0200672unsigned long long get_start_offset(struct thread_data *td)
673{
674 return td->o.start_offset +
675 (td->thread_number - 1) * td->o.offset_increment;
676}
677
Jens Axboe7bb48f82007-03-27 15:30:28 +0200678/*
679 * Open the files and setup files sizes, creating files if necessary.
680 */
681int setup_files(struct thread_data *td)
682{
683 unsigned long long total_size, extend_size;
684 struct fio_file *f;
685 unsigned int i;
Jens Axboe000b0802007-03-27 15:56:10 +0200686 int err = 0, need_extend;
Jens Axboe53cdc682006-10-18 11:50:58 +0200687
Jens Axboeee56ad52008-02-01 10:30:20 +0100688 dprint(FD_FILE, "setup files\n");
689
Jens Axboe691c8fb2008-03-07 14:26:26 +0100690 if (td->o.read_iolog_file)
Jens Axboe25460cf2012-05-02 13:58:02 +0200691 goto done;
Jens Axboe691c8fb2008-03-07 14:26:26 +0100692
Jens Axboe53cdc682006-10-18 11:50:58 +0200693 /*
694 * if ioengine defines a setup() method, it's responsible for
Jens Axboe7bb48f82007-03-27 15:30:28 +0200695 * opening the files and setting f->real_file_size to indicate
696 * the valid range for that file.
Jens Axboe53cdc682006-10-18 11:50:58 +0200697 */
698 if (td->io_ops->setup)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200699 err = td->io_ops->setup(td);
700 else
Jens Axboebab3fd52007-04-02 10:34:18 +0200701 err = get_file_sizes(td);
Jens Axboe53cdc682006-10-18 11:50:58 +0200702
Jens Axboef1027062006-10-20 10:14:01 +0200703 if (err)
704 return err;
705
Jens Axboe0a7eb122006-10-20 10:36:42 +0200706 /*
Jens Axboe7bb48f82007-03-27 15:30:28 +0200707 * check sizes. if the files/devices do not exist and the size
708 * isn't passed to fio, abort.
Jens Axboe0a7eb122006-10-20 10:36:42 +0200709 */
Jens Axboe7bb48f82007-03-27 15:30:28 +0200710 total_size = 0;
711 for_each_file(td, f, i) {
712 if (f->real_file_size == -1ULL)
713 total_size = -1ULL;
714 else
715 total_size += f->real_file_size;
716 }
Jens Axboe0a7eb122006-10-20 10:36:42 +0200717
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200718 if (td->o.fill_device)
719 td->fill_device_size = get_fs_free_counts(td);
720
Jens Axboe7bb48f82007-03-27 15:30:28 +0200721 /*
722 * device/file sizes are zero and no size given, punt
723 */
Jens Axboe1f809d12007-10-25 18:34:02 +0200724 if ((!total_size || total_size == -1ULL) && !td->o.size &&
Shawn Lewisaa31f1f2008-01-11 09:45:11 +0100725 !(td->io_ops->flags & FIO_NOIO) && !td->o.fill_device) {
Jens Axboe7bb48f82007-03-27 15:30:28 +0200726 log_err("%s: you need to specify size=\n", td->o.name);
Jens Axboee1161c32007-02-22 19:36:48 +0100727 td_verror(td, EINVAL, "total_file_size");
Jens Axboe53cdc682006-10-18 11:50:58 +0200728 return 1;
729 }
730
Jens Axboe7bb48f82007-03-27 15:30:28 +0200731 /*
732 * now file sizes are known, so we can set ->io_size. if size= is
733 * not given, ->io_size is just equal to ->real_file_size. if size
734 * is given, ->io_size is size / nr_files.
735 */
736 extend_size = total_size = 0;
737 need_extend = 0;
738 for_each_file(td, f, i) {
Dan Ehrenbergce95d652012-08-16 08:58:21 +0200739 f->file_offset = get_start_offset(td);
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200740
Jens Axboe7bb48f82007-03-27 15:30:28 +0200741 if (!td->o.file_size_low) {
742 /*
743 * no file size range given, file size is equal to
744 * total size divided by number of files. if that is
745 * zero, set it to the real file size.
746 */
747 f->io_size = td->o.size / td->o.nr_files;
Jens Axboe65bdb102008-01-24 13:13:12 +0100748 if (!f->io_size)
Jens Axboe273f8c92008-01-25 14:02:15 +0100749 f->io_size = f->real_file_size - f->file_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200750 } else if (f->real_file_size < td->o.file_size_low ||
751 f->real_file_size > td->o.file_size_high) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100752 if (f->file_offset > td->o.file_size_low)
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200753 goto err_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200754 /*
755 * file size given. if it's fixed, use that. if it's a
756 * range, generate a random size in-between.
757 */
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100758 if (td->o.file_size_low == td->o.file_size_high) {
759 f->io_size = td->o.file_size_low
760 - f->file_offset;
761 } else {
762 f->io_size = get_rand_file_size(td)
763 - f->file_offset;
764 }
Jens Axboe65bdb102008-01-24 13:13:12 +0100765 } else
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200766 f->io_size = f->real_file_size - f->file_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200767
768 if (f->io_size == -1ULL)
769 total_size = -1ULL;
Shaohua Li4d002562012-09-21 08:32:32 +0200770 else {
771 if (td->o.size_percent)
772 f->io_size = (f->io_size * td->o.size_percent) / 100;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200773 total_size += f->io_size;
Shaohua Li4d002562012-09-21 08:32:32 +0200774 }
Jens Axboe7bb48f82007-03-27 15:30:28 +0200775
776 if (f->filetype == FIO_TYPE_FILE &&
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200777 (f->io_size + f->file_offset) > f->real_file_size &&
Jens Axboe7bb48f82007-03-27 15:30:28 +0200778 !(td->io_ops->flags & FIO_DISKLESSIO)) {
Jens Axboe814452b2009-03-04 12:53:13 +0100779 if (!td->o.create_on_open) {
780 need_extend++;
781 extend_size += (f->io_size + f->file_offset);
782 } else
783 f->real_file_size = f->io_size + f->file_offset;
Jens Axboed6aed792009-06-03 08:41:15 +0200784 fio_file_set_extend(f);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100785 }
Jens Axboe7bb48f82007-03-27 15:30:28 +0200786 }
787
ljzhang,Yaxin Hu,Jianchao Tang22982902007-07-27 13:21:28 +0200788 if (!td->o.size || td->o.size > total_size)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200789 td->o.size = total_size;
790
791 /*
792 * See if we need to extend some files
793 */
794 if (need_extend) {
795 temp_stall_ts = 1;
Jens Axboef3afa572012-09-17 13:34:16 +0200796 if (output_format == FIO_OUTPUT_NORMAL)
Shaozhi Shawn Yea7ba8c52008-09-10 09:04:18 +0200797 log_info("%s: Laying out IO file(s) (%u file(s) /"
Bruce Cran0f2152c2010-12-15 10:33:03 +0100798 " %lluMB)\n", td->o.name, need_extend,
Shaozhi Shawn Yea7ba8c52008-09-10 09:04:18 +0200799 extend_size >> 20);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200800
801 for_each_file(td, f, i) {
Jens Axboe5e0074c2009-06-02 21:40:09 +0200802 unsigned long long old_len = -1ULL, extend_len = -1ULL;
Jens Axboe3baddf22008-04-04 11:10:30 +0200803
Jens Axboed6aed792009-06-03 08:41:15 +0200804 if (!fio_file_extend(f))
Jens Axboe7bb48f82007-03-27 15:30:28 +0200805 continue;
806
Jens Axboe409b3412007-03-28 09:57:01 +0200807 assert(f->filetype == FIO_TYPE_FILE);
Jens Axboed6aed792009-06-03 08:41:15 +0200808 fio_file_clear_extend(f);
Jens Axboe5e0074c2009-06-02 21:40:09 +0200809 if (!td->o.fill_device) {
810 old_len = f->real_file_size;
Jens Axboe0b9d69e2009-09-11 22:29:54 +0200811 extend_len = f->io_size + f->file_offset -
812 old_len;
Jens Axboe5e0074c2009-06-02 21:40:09 +0200813 }
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200814 f->real_file_size = (f->io_size + f->file_offset);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200815 err = extend_file(td, f);
816 if (err)
817 break;
Jens Axboe5e0074c2009-06-02 21:40:09 +0200818
Jens Axboe3baddf22008-04-04 11:10:30 +0200819 err = __file_invalidate_cache(td, f, old_len,
820 extend_len);
821 close(f->fd);
822 f->fd = -1;
823 if (err)
824 break;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200825 }
826 temp_stall_ts = 0;
827 }
828
829 if (err)
830 return err;
831
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100832 if (!td->o.zone_size)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200833 td->o.zone_size = td->o.size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200834
Jens Axboeea966f82007-09-03 10:09:37 +0200835 /*
836 * iolog already set the total io size, if we read back
837 * stored entries.
838 */
839 if (!td->o.read_iolog_file)
840 td->total_io_size = td->o.size * td->o.loops;
Jens Axboe25460cf2012-05-02 13:58:02 +0200841
842done:
843 if (td->o.create_only)
844 td->done = 1;
845
Jens Axboe7bb48f82007-03-27 15:30:28 +0200846 return 0;
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200847err_offset:
848 log_err("%s: you need to specify valid offset=\n", td->o.name);
849 return 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200850}
851
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200852int pre_read_files(struct thread_data *td)
853{
854 struct fio_file *f;
855 unsigned int i;
856
857 dprint(FD_FILE, "pre_read files\n");
858
859 for_each_file(td, f, i) {
860 pre_read_file(td, f);
861 }
862
863 return 1;
864}
865
Jens Axboe9c6f6312012-11-07 09:15:45 +0100866static int __init_rand_distribution(struct thread_data *td, struct fio_file *f)
867{
Jens Axboe23162962012-11-07 19:47:47 +0100868 unsigned int range_size, seed;
Jens Axboe9c6f6312012-11-07 09:15:45 +0100869 unsigned long nranges;
870
871 range_size = min(td->o.min_bs[DDIR_READ], td->o.min_bs[DDIR_WRITE]);
872
873 nranges = (f->real_file_size + range_size - 1) / range_size;
874
Jens Axboe23162962012-11-07 19:47:47 +0100875 seed = jhash(f->file_name, strlen(f->file_name), 0) * td->thread_number;
Jens Axboe84256872012-11-17 10:06:36 -0700876 if (!td->o.rand_repeatable)
877 seed = td->rand_seeds[4];
878
Jens Axboe9c6f6312012-11-07 09:15:45 +0100879 if (td->o.random_distribution == FIO_RAND_DIST_ZIPF)
Jens Axboe23162962012-11-07 19:47:47 +0100880 zipf_init(&f->zipf, nranges, td->o.zipf_theta, seed);
Jens Axboe9c6f6312012-11-07 09:15:45 +0100881 else
Jens Axboe23162962012-11-07 19:47:47 +0100882 pareto_init(&f->zipf, nranges, td->o.pareto_h, seed);
Jens Axboe9c6f6312012-11-07 09:15:45 +0100883
884 return 1;
885}
886
887static int init_rand_distribution(struct thread_data *td)
888{
889 struct fio_file *f;
890 unsigned int i;
891 int state;
892
893 if (td->o.random_distribution == FIO_RAND_DIST_RANDOM)
894 return 0;
895
896 state = td->runstate;
897 td_set_runstate(td, TD_SETTING_UP);
898 for_each_file(td, f, i)
899 __init_rand_distribution(td, f);
900 td_set_runstate(td, state);
901
902 return 1;
903}
904
Jens Axboe68727072007-03-15 20:49:25 +0100905int init_random_map(struct thread_data *td)
906{
Jens Axboe509eab12007-12-14 12:42:53 +0100907 unsigned long long blocks, num_maps;
Jens Axboe68727072007-03-15 20:49:25 +0100908 struct fio_file *f;
909 unsigned int i;
910
Jens Axboe9c6f6312012-11-07 09:15:45 +0100911 if (init_rand_distribution(td))
912 return 0;
Jens Axboede8dd112008-03-01 15:34:44 +0100913 if (td->o.norandommap || !td_random(td))
Jens Axboe68727072007-03-15 20:49:25 +0100914 return 0;
915
916 for_each_file(td, f, i) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100917 blocks = (f->real_file_size + td->o.rw_min_bs - 1) /
918 (unsigned long long) td->o.rw_min_bs;
919 num_maps = (blocks + BLOCKS_PER_MAP - 1) /
920 (unsigned long long) BLOCKS_PER_MAP;
Jens Axboeceadd592012-02-02 08:41:28 +0100921 if (num_maps == (unsigned long) num_maps) {
922 f->file_map = smalloc(num_maps * sizeof(unsigned long));
923 if (f->file_map) {
924 f->num_maps = num_maps;
925 continue;
926 }
927 } else
928 f->file_map = NULL;
929
Jens Axboe2b386d22008-03-26 10:32:57 +0100930 if (!td->o.softrandommap) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100931 log_err("fio: failed allocating random map. If running"
932 " a large number of jobs, try the 'norandommap'"
Jens Axboe2b386d22008-03-26 10:32:57 +0100933 " option or set 'softrandommap'. Or give"
934 " a larger --alloc-size to fio.\n");
Jens Axboe68727072007-03-15 20:49:25 +0100935 return 1;
936 }
Jens Axboe303032a2008-03-26 10:11:10 +0100937
938 log_info("fio: file %s failed allocating random map. Running "
939 "job without.\n", f->file_name);
940 f->num_maps = 0;
Jens Axboe68727072007-03-15 20:49:25 +0100941 }
942
943 return 0;
944}
945
Jens Axboe53cdc682006-10-18 11:50:58 +0200946void close_files(struct thread_data *td)
947{
Jens Axboe0ab8db82006-10-18 17:16:23 +0200948 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100949 unsigned int i;
Jens Axboe53cdc682006-10-18 11:50:58 +0200950
Jens Axboe2be3eec2009-06-10 09:05:13 +0200951 for_each_file(td, f, i) {
952 if (fio_file_open(f))
953 td_io_close_file(td, f);
954 }
Jens Axboe24ffd2c2008-03-01 15:47:08 +0100955}
956
957void close_and_free_files(struct thread_data *td)
958{
959 struct fio_file *f;
960 unsigned int i;
961
Jens Axboeee56ad52008-02-01 10:30:20 +0100962 dprint(FD_FILE, "close files\n");
963
Jens Axboe0ab8db82006-10-18 17:16:23 +0200964 for_each_file(td, f, i) {
Jens Axboeffdfabd2008-03-26 09:18:14 +0100965 if (td->o.unlink && f->filetype == FIO_TYPE_FILE) {
966 dprint(FD_FILE, "free unlink %s\n", f->file_name);
Jens Axboe132ad462006-11-23 12:23:12 +0100967 unlink(f->file_name);
Jens Axboeffdfabd2008-03-26 09:18:14 +0100968 }
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100969
Jens Axboe22a57ba2009-06-09 14:34:35 +0200970 if (fio_file_open(f))
971 td_io_close_file(td, f);
972
Shaozhi Shawn Yeb9fbcf22008-09-10 09:05:17 +0200973 remove_file_hash(f);
Jens Axboeb3dc7f02007-03-09 12:52:15 +0100974
Jens Axboef17c4392008-03-01 18:40:46 +0100975 sfree(f->file_name);
Jens Axboefa1da862007-03-23 15:20:54 +0100976 f->file_name = NULL;
Jens Axboe61eb3132010-03-31 20:05:59 +0200977 sfree(f->file_map);
978 f->file_map = NULL;
Jens Axboe78d99e62008-03-01 18:41:50 +0100979 sfree(f);
Jens Axboe53cdc682006-10-18 11:50:58 +0200980 }
Jens Axboeb4a6a592006-10-20 13:54:47 +0200981
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100982 td->o.filename = NULL;
Jens Axboecade3ef2007-03-23 15:29:45 +0100983 free(td->files);
Jens Axboe9efef3c2008-03-06 10:26:25 +0100984 td->files_index = 0;
Jens Axboeb4a6a592006-10-20 13:54:47 +0200985 td->files = NULL;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100986 td->o.nr_files = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200987}
Jens Axboeaf52b342007-03-13 10:07:47 +0100988
Jens Axboee3bab462007-03-13 11:22:09 +0100989static void get_file_type(struct fio_file *f)
Jens Axboeaf52b342007-03-13 10:07:47 +0100990{
991 struct stat sb;
992
Jens Axboe66159822007-04-16 21:54:24 +0200993 if (!strcmp(f->file_name, "-"))
994 f->filetype = FIO_TYPE_PIPE;
995 else
996 f->filetype = FIO_TYPE_FILE;
Jens Axboeaf52b342007-03-13 10:07:47 +0100997
Bruce Cran38921822012-02-22 17:55:16 +0000998 /* \\.\ is the device namespace in Windows, where every file is
999 * a block device */
1000 if (strncmp(f->file_name, "\\\\.\\", 4) == 0)
1001 f->filetype = FIO_TYPE_BD;
1002
Jens Axboeb30d3952010-02-06 23:36:26 +01001003 if (!stat(f->file_name, &sb)) {
Bruce Cran38921822012-02-22 17:55:16 +00001004 if (S_ISBLK(sb.st_mode))
Jens Axboeaf52b342007-03-13 10:07:47 +01001005 f->filetype = FIO_TYPE_BD;
1006 else if (S_ISCHR(sb.st_mode))
1007 f->filetype = FIO_TYPE_CHAR;
Jens Axboeb5605e92007-04-16 20:42:33 +02001008 else if (S_ISFIFO(sb.st_mode))
1009 f->filetype = FIO_TYPE_PIPE;
Jens Axboeaf52b342007-03-13 10:07:47 +01001010 }
1011}
1012
Jens Axboef29b25a2007-07-23 08:56:43 +02001013int add_file(struct thread_data *td, const char *fname)
Jens Axboeaf52b342007-03-13 10:07:47 +01001014{
Jens Axboe7b4e4fe2007-03-13 12:51:40 +01001015 int cur_files = td->files_index;
Jens Axboebd0ee742007-03-23 14:26:23 +01001016 char file_name[PATH_MAX];
Jens Axboeaf52b342007-03-13 10:07:47 +01001017 struct fio_file *f;
Jens Axboebd0ee742007-03-23 14:26:23 +01001018 int len = 0;
Jens Axboeaf52b342007-03-13 10:07:47 +01001019
Jens Axboeee56ad52008-02-01 10:30:20 +01001020 dprint(FD_FILE, "add file %s\n", fname);
1021
Jens Axboef17c4392008-03-01 18:40:46 +01001022 f = smalloc(sizeof(*f));
Jens Axboec48c0be2008-07-31 19:55:02 +02001023 if (!f) {
1024 log_err("fio: smalloc OOM\n");
1025 assert(0);
1026 }
Jens Axboe0b9d69e2009-09-11 22:29:54 +02001027
Jens Axboeaf52b342007-03-13 10:07:47 +01001028 f->fd = -1;
Jens Axboe38dad622010-07-20 14:46:00 -06001029 fio_file_reset(f);
Jens Axboebd0ee742007-03-23 14:26:23 +01001030
Jens Axboefc99bc02009-03-04 15:27:42 +01001031 if (td->files_size <= td->files_index) {
Carl Henrik Lunde4b341fc2009-04-20 08:41:37 +02001032 int new_size = td->o.nr_files + 1;
Jens Axboe126d65c2008-03-01 18:04:31 +01001033
Jens Axboefc99bc02009-03-04 15:27:42 +01001034 dprint(FD_FILE, "resize file array to %d files\n", new_size);
1035
1036 td->files = realloc(td->files, new_size * sizeof(f));
Jens Axboefc99bc02009-03-04 15:27:42 +01001037 td->files_size = new_size;
1038 }
Jens Axboe8bb76792009-03-04 15:37:52 +01001039 td->files[cur_files] = f;
Shaohua Li89ac1d42012-06-11 08:56:32 +02001040 f->fileno = cur_files;
Jens Axboe126d65c2008-03-01 18:04:31 +01001041
Jens Axboe07eb79d2007-04-12 13:02:38 +02001042 /*
1043 * init function, io engine may not be loaded yet
1044 */
1045 if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO))
1046 f->real_file_size = -1ULL;
1047
Jens Axboebd0ee742007-03-23 14:26:23 +01001048 if (td->o.directory)
1049 len = sprintf(file_name, "%s/", td->o.directory);
1050
1051 sprintf(file_name + len, "%s", fname);
Jens Axboef17c4392008-03-01 18:40:46 +01001052 f->file_name = smalloc_strdup(file_name);
Jens Axboec48c0be2008-07-31 19:55:02 +02001053 if (!f->file_name) {
1054 log_err("fio: smalloc OOM\n");
1055 assert(0);
1056 }
Jens Axboe0b9d69e2009-09-11 22:29:54 +02001057
Jens Axboee3bab462007-03-13 11:22:09 +01001058 get_file_type(f);
Jens Axboeaf52b342007-03-13 10:07:47 +01001059
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001060 switch (td->o.file_lock_mode) {
1061 case FILE_LOCK_NONE:
1062 break;
1063 case FILE_LOCK_READWRITE:
1064 f->lock = fio_mutex_rw_init();
1065 break;
1066 case FILE_LOCK_EXCLUSIVE:
Jens Axboe521da522012-08-02 11:21:36 +02001067 f->lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001068 break;
1069 default:
1070 log_err("fio: unknown lock mode: %d\n", td->o.file_lock_mode);
1071 assert(0);
1072 }
Jens Axboe29c13492008-03-01 19:25:20 +01001073
Jens Axboe7b4e4fe2007-03-13 12:51:40 +01001074 td->files_index++;
Jens Axboe15494412007-03-13 11:52:47 +01001075 if (f->filetype == FIO_TYPE_FILE)
1076 td->nr_normal_files++;
Jens Axboef29b25a2007-07-23 08:56:43 +02001077
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001078 dprint(FD_FILE, "file %p \"%s\" added at %d\n", f, f->file_name,
1079 cur_files);
Jens Axboe9efef3c2008-03-06 10:26:25 +01001080
Jens Axboef29b25a2007-07-23 08:56:43 +02001081 return cur_files;
Jens Axboeaf52b342007-03-13 10:07:47 +01001082}
Jens Axboe0ad920e2007-03-13 11:06:45 +01001083
Jens Axboe49ffb4a2010-08-24 15:01:37 +02001084int add_file_exclusive(struct thread_data *td, const char *fname)
1085{
1086 struct fio_file *f;
1087 unsigned int i;
1088
1089 for_each_file(td, f, i) {
1090 if (!strcmp(f->file_name, fname))
1091 return i;
1092 }
1093
1094 return add_file(td, fname);
1095}
1096
Jens Axboe0ad920e2007-03-13 11:06:45 +01001097void get_file(struct fio_file *f)
1098{
Jens Axboe8172fe92008-02-01 21:51:02 +01001099 dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references);
Jens Axboed6aed792009-06-03 08:41:15 +02001100 assert(fio_file_open(f));
Jens Axboe0ad920e2007-03-13 11:06:45 +01001101 f->references++;
1102}
1103
Jens Axboe6977bcd2008-03-01 15:55:36 +01001104int put_file(struct thread_data *td, struct fio_file *f)
Jens Axboe0ad920e2007-03-13 11:06:45 +01001105{
Jens Axboe98e1ac42008-03-06 11:56:48 +01001106 int f_ret = 0, ret = 0;
Jens Axboe6977bcd2008-03-01 15:55:36 +01001107
Jens Axboe8172fe92008-02-01 21:51:02 +01001108 dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references);
Jens Axboeee56ad52008-02-01 10:30:20 +01001109
Jens Axboe22a57ba2009-06-09 14:34:35 +02001110 if (!fio_file_open(f)) {
1111 assert(f->fd == -1);
Jens Axboe6977bcd2008-03-01 15:55:36 +01001112 return 0;
Jens Axboe22a57ba2009-06-09 14:34:35 +02001113 }
Jens Axboe0ad920e2007-03-13 11:06:45 +01001114
1115 assert(f->references);
1116 if (--f->references)
Jens Axboe6977bcd2008-03-01 15:55:36 +01001117 return 0;
Jens Axboe0ad920e2007-03-13 11:06:45 +01001118
Jens Axboed424d4d2007-04-17 09:05:10 +02001119 if (should_fsync(td) && td->o.fsync_on_close)
Jens Axboe98e1ac42008-03-06 11:56:48 +01001120 f_ret = fsync(f->fd);
Jens Axboeebb14152007-03-13 14:42:15 +01001121
Jens Axboe0ad920e2007-03-13 11:06:45 +01001122 if (td->io_ops->close_file)
Jens Axboe6977bcd2008-03-01 15:55:36 +01001123 ret = td->io_ops->close_file(td, f);
Jens Axboe1020a132007-03-29 11:15:06 +02001124
Jens Axboe98e1ac42008-03-06 11:56:48 +01001125 if (!ret)
Jens Axboea5fb4612008-05-28 10:54:01 +02001126 ret = f_ret;
Jens Axboe98e1ac42008-03-06 11:56:48 +01001127
Jens Axboe0ad920e2007-03-13 11:06:45 +01001128 td->nr_open_files--;
Jens Axboed6aed792009-06-03 08:41:15 +02001129 fio_file_clear_open(f);
Jens Axboe22a57ba2009-06-09 14:34:35 +02001130 assert(f->fd == -1);
Jens Axboe6977bcd2008-03-01 15:55:36 +01001131 return ret;
Jens Axboe0ad920e2007-03-13 11:06:45 +01001132}
Jens Axboebbf6b542007-03-13 15:28:55 +01001133
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001134void lock_file(struct thread_data *td, struct fio_file *f, enum fio_ddir ddir)
Jens Axboeb2bd2bd2008-03-01 18:19:52 +01001135{
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001136 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1137 return;
Jens Axboe29c13492008-03-01 19:25:20 +01001138
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001139 if (f->lock_owner == td && f->lock_batch--)
1140 return;
1141
1142 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
1143 if (ddir == DDIR_READ)
1144 fio_mutex_down_read(f->lock);
1145 else
1146 fio_mutex_down_write(f->lock);
1147 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1148 fio_mutex_down(f->lock);
1149
1150 f->lock_owner = td;
1151 f->lock_batch = td->o.lockfile_batch;
1152 f->lock_ddir = ddir;
1153}
1154
1155void unlock_file(struct thread_data *td, struct fio_file *f)
1156{
1157 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1158 return;
1159 if (f->lock_batch)
1160 return;
1161
1162 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
1163 const int is_read = f->lock_ddir == DDIR_READ;
1164 int val = fio_mutex_getval(f->lock);
1165
1166 if ((is_read && val == 1) || (!is_read && val == -1))
1167 f->lock_owner = NULL;
1168
1169 if (is_read)
1170 fio_mutex_up_read(f->lock);
1171 else
1172 fio_mutex_up_write(f->lock);
1173 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE) {
1174 int val = fio_mutex_getval(f->lock);
1175
1176 if (val == 0)
1177 f->lock_owner = NULL;
1178
1179 fio_mutex_up(f->lock);
Jens Axboe29c13492008-03-01 19:25:20 +01001180 }
Jens Axboeb2bd2bd2008-03-01 18:19:52 +01001181}
1182
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001183void unlock_file_all(struct thread_data *td, struct fio_file *f)
Jens Axboeb2bd2bd2008-03-01 18:19:52 +01001184{
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001185 if (f->lock_owner != td)
1186 return;
Jens Axboe29c13492008-03-01 19:25:20 +01001187
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001188 f->lock_batch = 0;
1189 unlock_file(td, f);
Jens Axboeb2bd2bd2008-03-01 18:19:52 +01001190}
1191
Jens Axboebbf6b542007-03-13 15:28:55 +01001192static int recurse_dir(struct thread_data *td, const char *dirname)
1193{
1194 struct dirent *dir;
1195 int ret = 0;
1196 DIR *D;
1197
1198 D = opendir(dirname);
1199 if (!D) {
Jens Axboe0ddb2702007-03-27 19:43:53 +02001200 char buf[FIO_VERROR_SIZE];
1201
1202 snprintf(buf, FIO_VERROR_SIZE - 1, "opendir(%s)", dirname);
1203 td_verror(td, errno, buf);
Jens Axboebbf6b542007-03-13 15:28:55 +01001204 return 1;
1205 }
1206
1207 while ((dir = readdir(D)) != NULL) {
1208 char full_path[PATH_MAX];
1209 struct stat sb;
1210
Jens Axboee85b2b82007-03-14 09:39:06 +01001211 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
1212 continue;
Jens Axboe96d32d52007-03-14 09:16:09 +01001213
Bruce Cranb9fd7882012-02-20 17:01:46 +00001214 sprintf(full_path, "%s%s%s", dirname, FIO_OS_PATH_SEPARATOR, dir->d_name);
Jens Axboebbf6b542007-03-13 15:28:55 +01001215
1216 if (lstat(full_path, &sb) == -1) {
1217 if (errno != ENOENT) {
1218 td_verror(td, errno, "stat");
1219 return 1;
1220 }
1221 }
1222
1223 if (S_ISREG(sb.st_mode)) {
1224 add_file(td, full_path);
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001225 td->o.nr_files++;
Jens Axboebbf6b542007-03-13 15:28:55 +01001226 continue;
1227 }
Jens Axboe0ddb2702007-03-27 19:43:53 +02001228 if (!S_ISDIR(sb.st_mode))
1229 continue;
Jens Axboebbf6b542007-03-13 15:28:55 +01001230
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001231 ret = recurse_dir(td, full_path);
1232 if (ret)
Jens Axboebbf6b542007-03-13 15:28:55 +01001233 break;
1234 }
1235
1236 closedir(D);
1237 return ret;
1238}
1239
1240int add_dir_files(struct thread_data *td, const char *path)
1241{
Jens Axboe0ddb2702007-03-27 19:43:53 +02001242 int ret = recurse_dir(td, path);
1243
1244 if (!ret)
1245 log_info("fio: opendir added %d files\n", td->o.nr_files);
1246
1247 return ret;
Jens Axboebbf6b542007-03-13 15:28:55 +01001248}
Jens Axboecade3ef2007-03-23 15:29:45 +01001249
1250void dup_files(struct thread_data *td, struct thread_data *org)
1251{
1252 struct fio_file *f;
1253 unsigned int i;
Jens Axboe9efef3c2008-03-06 10:26:25 +01001254
1255 dprint(FD_FILE, "dup files: %d\n", org->files_index);
Jens Axboecade3ef2007-03-23 15:29:45 +01001256
1257 if (!org->files)
1258 return;
1259
Jens Axboe9efef3c2008-03-06 10:26:25 +01001260 td->files = malloc(org->files_index * sizeof(f));
Jens Axboecade3ef2007-03-23 15:29:45 +01001261
Jens Axboe9efef3c2008-03-06 10:26:25 +01001262 for_each_file(org, f, i) {
Jens Axboeb0fe4212008-03-01 18:22:27 +01001263 struct fio_file *__f;
1264
Jens Axboef17c4392008-03-01 18:40:46 +01001265 __f = smalloc(sizeof(*__f));
Jens Axboec48c0be2008-07-31 19:55:02 +02001266 if (!__f) {
1267 log_err("fio: smalloc OOM\n");
1268 assert(0);
1269 }
Jens Axboe22a57ba2009-06-09 14:34:35 +02001270 __f->fd = -1;
Jens Axboe38dad622010-07-20 14:46:00 -06001271 fio_file_reset(__f);
Jens Axboe0b9d69e2009-09-11 22:29:54 +02001272
Aaron Carrollbc3456f2008-06-25 09:20:37 +02001273 if (f->file_name) {
Jens Axboef17c4392008-03-01 18:40:46 +01001274 __f->file_name = smalloc_strdup(f->file_name);
Jens Axboec48c0be2008-07-31 19:55:02 +02001275 if (!__f->file_name) {
1276 log_err("fio: smalloc OOM\n");
1277 assert(0);
1278 }
Jens Axboe0b9d69e2009-09-11 22:29:54 +02001279
Aaron Carrollbc3456f2008-06-25 09:20:37 +02001280 __f->filetype = f->filetype;
1281 }
Jens Axboeb0fe4212008-03-01 18:22:27 +01001282
1283 td->files[i] = __f;
Jens Axboecade3ef2007-03-23 15:29:45 +01001284 }
1285}
Jens Axboef29b25a2007-07-23 08:56:43 +02001286
1287/*
1288 * Returns the index that matches the filename, or -1 if not there
1289 */
1290int get_fileno(struct thread_data *td, const char *fname)
1291{
1292 struct fio_file *f;
1293 unsigned int i;
1294
1295 for_each_file(td, f, i)
1296 if (!strcmp(f->file_name, fname))
1297 return i;
1298
1299 return -1;
1300}
1301
1302/*
1303 * For log usage, where we add/open/close files automatically
1304 */
1305void free_release_files(struct thread_data *td)
1306{
1307 close_files(td);
1308 td->files_index = 0;
1309 td->nr_normal_files = 0;
1310}