blob: 9c03a626986722491c5ef18694e6f66ba7d841e2 [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"
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -080014#include "options.h"
Bruce Cranecc314b2011-01-04 10:59:30 +010015#include "os/os.h"
Jens Axboe23162962012-11-07 19:47:47 +010016#include "hash.h"
Jens Axboe7ebd7962012-11-28 21:24:46 +010017#include "lib/axmap.h"
Jens Axboe53cdc682006-10-18 11:50:58 +020018
Jens Axboe97ac9922013-01-24 15:00:25 -070019#ifdef CONFIG_LINUX_FALLOCATE
Eric Gourioua596f042011-06-17 09:11:45 +020020#include <linux/falloc.h>
21#endif
22
Jens Axboe7172cfe2007-07-23 14:36:00 +020023static int root_warn;
24
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -080025static FLIST_HEAD(filename_list);
26
Jens Axboec592b9f2009-06-03 09:29:28 +020027static inline void clear_error(struct thread_data *td)
28{
29 td->error = 0;
30 td->verror[0] = '\0';
31}
32
Jens Axboe3baddf22008-04-04 11:10:30 +020033/*
34 * Leaves f->fd open on success, caller must close
35 */
Jens Axboe7bb48f82007-03-27 15:30:28 +020036static int extend_file(struct thread_data *td, struct fio_file *f)
Jens Axboe25205e92006-10-19 20:50:14 +020037{
Jens Axboeea443652007-03-29 14:52:13 +020038 int r, new_layout = 0, unlink_file = 0, flags;
Jens Axboe25205e92006-10-19 20:50:14 +020039 unsigned long long left;
40 unsigned int bs;
41 char *b;
Jens Axboeb2a15192006-10-18 14:10:42 +020042
Jens Axboe4241ea82007-09-12 08:18:36 +020043 if (read_only) {
44 log_err("fio: refusing extend of file due to read-only\n");
45 return 0;
46 }
47
Jens Axboe507a7022007-03-27 15:52:46 +020048 /*
49 * check if we need to lay the file out complete again. fio
50 * does that for operations involving reads, or for writes
51 * where overwrite is set
52 */
Jens Axboeffdfabd2008-03-26 09:18:14 +010053 if (td_read(td) || (td_write(td) && td->o.overwrite) ||
54 (td_write(td) && td->io_ops->flags & FIO_NOEXTEND))
Jens Axboe507a7022007-03-27 15:52:46 +020055 new_layout = 1;
Jens Axboeea443652007-03-29 14:52:13 +020056 if (td_write(td) && !td->o.overwrite)
57 unlink_file = 1;
Jens Axboe507a7022007-03-27 15:52:46 +020058
Jens Axboe6ae1f572008-02-21 10:20:00 +010059 if (unlink_file || new_layout) {
Jens Axboebd199f22008-03-26 09:57:18 +010060 dprint(FD_FILE, "layout unlink %s\n", f->file_name);
Zhang, Yanmin982016d2008-02-26 15:35:52 +010061 if ((unlink(f->file_name) < 0) && (errno != ENOENT)) {
Jens Axboe7bb48f82007-03-27 15:30:28 +020062 td_verror(td, errno, "unlink");
63 return 1;
64 }
65 }
66
Jens Axboe507a7022007-03-27 15:52:46 +020067 flags = O_WRONLY | O_CREAT;
68 if (new_layout)
69 flags |= O_TRUNC;
70
Jens Axboeee56ad52008-02-01 10:30:20 +010071 dprint(FD_FILE, "open file %s, flags %x\n", f->file_name, flags);
Jens Axboe507a7022007-03-27 15:52:46 +020072 f->fd = open(f->file_name, flags, 0644);
Jens Axboe53cdc682006-10-18 11:50:58 +020073 if (f->fd < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +010074 td_verror(td, errno, "open");
Jens Axboe53cdc682006-10-18 11:50:58 +020075 return 1;
76 }
77
Jens Axboe97ac9922013-01-24 15:00:25 -070078#ifdef CONFIG_POSIX_FALLOCATE
Eric Gourioua596f042011-06-17 09:11:45 +020079 if (!td->o.fill_device) {
80 switch (td->o.fallocate_mode) {
81 case FIO_FALLOCATE_NONE:
82 break;
83 case FIO_FALLOCATE_POSIX:
84 dprint(FD_FILE, "posix_fallocate file %s size %llu\n",
Jens Axboe4b91ee82013-02-25 10:18:33 +010085 f->file_name,
86 (unsigned long long) f->real_file_size);
Jens Axboe7bc8c2c2010-01-28 11:31:31 +010087
Eric Gourioua596f042011-06-17 09:11:45 +020088 r = posix_fallocate(f->fd, 0, f->real_file_size);
89 if (r > 0) {
90 log_err("fio: posix_fallocate fails: %s\n",
91 strerror(r));
92 }
93 break;
Jens Axboe97ac9922013-01-24 15:00:25 -070094#ifdef CONFIG_LINUX_FALLOCATE
Eric Gourioua596f042011-06-17 09:11:45 +020095 case FIO_FALLOCATE_KEEP_SIZE:
96 dprint(FD_FILE,
97 "fallocate(FALLOC_FL_KEEP_SIZE) "
Jens Axboe4b91ee82013-02-25 10:18:33 +010098 "file %s size %llu\n", f->file_name,
99 (unsigned long long) f->real_file_size);
Eric Gourioua596f042011-06-17 09:11:45 +0200100
101 r = fallocate(f->fd, FALLOC_FL_KEEP_SIZE, 0,
102 f->real_file_size);
Jens Axboe888677a2013-04-10 22:19:46 +0200103 if (r != 0)
Eric Gourioua596f042011-06-17 09:11:45 +0200104 td_verror(td, errno, "fallocate");
Jens Axboe888677a2013-04-10 22:19:46 +0200105
Eric Gourioua596f042011-06-17 09:11:45 +0200106 break;
Jens Axboe97ac9922013-01-24 15:00:25 -0700107#endif /* CONFIG_LINUX_FALLOCATE */
Eric Gourioua596f042011-06-17 09:11:45 +0200108 default:
109 log_err("fio: unknown fallocate mode: %d\n",
110 td->o.fallocate_mode);
111 assert(0);
Jens Axboe7bc8c2c2010-01-28 11:31:31 +0100112 }
113 }
Jens Axboe97ac9922013-01-24 15:00:25 -0700114#endif /* CONFIG_POSIX_FALLOCATE */
Bruce Cran9b836562011-01-08 19:49:54 +0100115
Shawn Lewisfc74ac12008-01-11 09:45:11 +0100116 if (!new_layout)
117 goto done;
118
Jens Axboe5e0074c2009-06-02 21:40:09 +0200119 /*
120 * The size will be -1ULL when fill_device is used, so don't truncate
121 * or fallocate this file, just write it
122 */
123 if (!td->o.fill_device) {
124 dprint(FD_FILE, "truncate file %s, size %llu\n", f->file_name,
Jens Axboe4b91ee82013-02-25 10:18:33 +0100125 (unsigned long long) f->real_file_size);
Jens Axboe5e0074c2009-06-02 21:40:09 +0200126 if (ftruncate(f->fd, f->real_file_size) == -1) {
John3cd4c662013-12-29 19:20:35 -0700127 if (errno != EFBIG) {
128 td_verror(td, errno, "ftruncate");
129 goto err;
130 }
Jens Axboe5e0074c2009-06-02 21:40:09 +0200131 }
Jens Axboe5e0074c2009-06-02 21:40:09 +0200132 }
Jens Axboe40f82982006-10-25 11:21:05 +0200133
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100134 b = malloc(td->o.max_bs[DDIR_WRITE]);
Jens Axboe53cdc682006-10-18 11:50:58 +0200135
Jens Axboe7bb48f82007-03-27 15:30:28 +0200136 left = f->real_file_size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200137 while (left && !td->terminate) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100138 bs = td->o.max_bs[DDIR_WRITE];
Jens Axboe53cdc682006-10-18 11:50:58 +0200139 if (bs > left)
140 bs = left;
141
Jens Axboecc86c392013-05-03 15:12:33 +0200142 fill_io_buffer(td, b, bs, bs);
143
Jens Axboe53cdc682006-10-18 11:50:58 +0200144 r = write(f->fd, b, bs);
145
Jens Axboe5e0074c2009-06-02 21:40:09 +0200146 if (r > 0) {
147 left -= r;
Jens Axboe53cdc682006-10-18 11:50:58 +0200148 continue;
149 } else {
Jens Axboe5e0074c2009-06-02 21:40:09 +0200150 if (r < 0) {
151 int __e = errno;
152
153 if (__e == ENOSPC) {
154 if (td->o.fill_device)
155 break;
156 log_info("fio: ENOSPC on laying out "
157 "file, stopping\n");
158 break;
159 }
Jens Axboee1161c32007-02-22 19:36:48 +0100160 td_verror(td, errno, "write");
Jens Axboe5e0074c2009-06-02 21:40:09 +0200161 } else
Jens Axboee1161c32007-02-22 19:36:48 +0100162 td_verror(td, EIO, "write");
Jens Axboe53cdc682006-10-18 11:50:58 +0200163
164 break;
165 }
166 }
167
Jens Axboeffdfabd2008-03-26 09:18:14 +0100168 if (td->terminate) {
169 dprint(FD_FILE, "terminate unlink %s\n", f->file_name);
Jens Axboe53cdc682006-10-18 11:50:58 +0200170 unlink(f->file_name);
Jens Axboeffdfabd2008-03-26 09:18:14 +0100171 } else if (td->o.create_fsync) {
Jens Axboe98e1ac42008-03-06 11:56:48 +0100172 if (fsync(f->fd) < 0) {
173 td_verror(td, errno, "fsync");
174 goto err;
175 }
176 }
Jens Axboe0d1cd202009-06-05 22:11:52 +0200177 if (td->o.fill_device && !td_write(td)) {
Jens Axboed6aed792009-06-03 08:41:15 +0200178 fio_file_clear_size_known(f);
Jens Axboe5e0074c2009-06-02 21:40:09 +0200179 if (td_io_get_file_size(td, f))
180 goto err;
181 if (f->io_size > f->real_file_size)
182 f->io_size = f->real_file_size;
183 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200184
185 free(b);
Jens Axboe507a7022007-03-27 15:52:46 +0200186done:
Jens Axboe53cdc682006-10-18 11:50:58 +0200187 return 0;
188err:
189 close(f->fd);
190 f->fd = -1;
191 return 1;
192}
193
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200194static int pre_read_file(struct thread_data *td, struct fio_file *f)
195{
Jens Axboeb0f65862009-05-20 11:52:15 +0200196 int r, did_open = 0, old_runstate;
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200197 unsigned long long left;
198 unsigned int bs;
199 char *b;
200
Jens Axboe9c0d2242009-07-01 12:26:28 +0200201 if (td->io_ops->flags & FIO_PIPEIO)
202 return 0;
203
Jens Axboed6aed792009-06-03 08:41:15 +0200204 if (!fio_file_open(f)) {
Jens Axboeb0f65862009-05-20 11:52:15 +0200205 if (td->io_ops->open_file(td, f)) {
206 log_err("fio: cannot pre-read, failed to open file\n");
207 return 1;
208 }
209 did_open = 1;
210 }
211
Jens Axboe8edd9732014-03-01 08:24:03 -0700212 old_runstate = td_bump_runstate(td, TD_PRE_READING);
Jens Axboeb0f65862009-05-20 11:52:15 +0200213
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200214 bs = td->o.max_bs[DDIR_READ];
215 b = malloc(bs);
216 memset(b, 0, bs);
217
218 lseek(f->fd, f->file_offset, SEEK_SET);
219 left = f->io_size;
220
221 while (left && !td->terminate) {
222 if (bs > left)
223 bs = left;
224
225 r = read(f->fd, b, bs);
226
227 if (r == (int) bs) {
228 left -= bs;
229 continue;
230 } else {
231 td_verror(td, EIO, "pre_read");
232 break;
233 }
234 }
235
Jens Axboe8edd9732014-03-01 08:24:03 -0700236 td_restore_runstate(td, old_runstate);
Jens Axboeb0f65862009-05-20 11:52:15 +0200237
238 if (did_open)
239 td->io_ops->close_file(td, f);
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200240 free(b);
241 return 0;
242}
243
Jens Axboe7bb48f82007-03-27 15:30:28 +0200244static unsigned long long get_rand_file_size(struct thread_data *td)
Jens Axboe9c60ce62007-03-15 09:14:47 +0100245{
Jens Axboedc873b62008-06-04 20:13:04 +0200246 unsigned long long ret, sized;
Jens Axboe1294c3e2011-05-11 08:15:18 +0200247 unsigned long r;
Jens Axboe9c60ce62007-03-15 09:14:47 +0100248
Jens Axboe4c07ad82011-03-28 09:51:09 +0200249 if (td->o.use_os_rand) {
250 r = os_random_long(&td->file_size_state);
251 sized = td->o.file_size_high - td->o.file_size_low;
252 ret = (unsigned long long) ((double) sized * (r / (OS_RAND_MAX + 1.0)));
253 } else {
254 r = __rand(&td->__file_size_state);
255 sized = td->o.file_size_high - td->o.file_size_low;
256 ret = (unsigned long long) ((double) sized * (r / (FRAND_MAX + 1.0)));
257 }
258
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100259 ret += td->o.file_size_low;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100260 ret -= (ret % td->o.rw_min_bs);
Jens Axboe9c60ce62007-03-15 09:14:47 +0100261 return ret;
262}
263
Jens Axboe53cdc682006-10-18 11:50:58 +0200264static int file_size(struct thread_data *td, struct fio_file *f)
265{
266 struct stat st;
267
Jens Axboedf9c26b2009-03-05 10:13:58 +0100268 if (stat(f->file_name, &st) == -1) {
Jens Axboe7bb48f82007-03-27 15:30:28 +0200269 td_verror(td, errno, "fstat");
270 return 1;
271 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200272
Jens Axboe7bb48f82007-03-27 15:30:28 +0200273 f->real_file_size = st.st_size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200274 return 0;
275}
276
277static int bdev_size(struct thread_data *td, struct fio_file *f)
278{
Bruce Cran9b836562011-01-08 19:49:54 +0100279 unsigned long long bytes = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200280 int r;
281
Jens Axboedf9c26b2009-03-05 10:13:58 +0100282 if (td->io_ops->open_file(td, f)) {
283 log_err("fio: failed opening blockdev %s for size check\n",
284 f->file_name);
285 return 1;
286 }
287
Bruce Cranecc314b2011-01-04 10:59:30 +0100288 r = blockdev_size(f, &bytes);
Jens Axboe53cdc682006-10-18 11:50:58 +0200289 if (r) {
Jens Axboee1161c32007-02-22 19:36:48 +0100290 td_verror(td, r, "blockdev_size");
Jens Axboedf9c26b2009-03-05 10:13:58 +0100291 goto err;
Jens Axboe53cdc682006-10-18 11:50:58 +0200292 }
293
Jens Axboe7ab077a2009-02-02 15:07:37 +0100294 if (!bytes) {
295 log_err("%s: zero sized block device?\n", f->file_name);
Jens Axboedf9c26b2009-03-05 10:13:58 +0100296 goto err;
Jens Axboe7ab077a2009-02-02 15:07:37 +0100297 }
298
Jens Axboe53cdc682006-10-18 11:50:58 +0200299 f->real_file_size = bytes;
Jens Axboe22a57ba2009-06-09 14:34:35 +0200300 td->io_ops->close_file(td, f);
Jens Axboe53cdc682006-10-18 11:50:58 +0200301 return 0;
Jens Axboedf9c26b2009-03-05 10:13:58 +0100302err:
303 td->io_ops->close_file(td, f);
304 return 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200305}
306
Jens Axboe4ccdccd2010-06-24 10:27:22 +0200307static int char_size(struct thread_data *td, struct fio_file *f)
308{
309#ifdef FIO_HAVE_CHARDEV_SIZE
Bruce Cran9b836562011-01-08 19:49:54 +0100310 unsigned long long bytes = 0;
Jens Axboe4ccdccd2010-06-24 10:27:22 +0200311 int r;
312
313 if (td->io_ops->open_file(td, f)) {
314 log_err("fio: failed opening blockdev %s for size check\n",
315 f->file_name);
316 return 1;
317 }
318
Bruce Cranecc314b2011-01-04 10:59:30 +0100319 r = chardev_size(f, &bytes);
Jens Axboe4ccdccd2010-06-24 10:27:22 +0200320 if (r) {
321 td_verror(td, r, "chardev_size");
322 goto err;
323 }
324
325 if (!bytes) {
326 log_err("%s: zero sized char device?\n", f->file_name);
327 goto err;
328 }
329
330 f->real_file_size = bytes;
331 td->io_ops->close_file(td, f);
332 return 0;
333err:
334 td->io_ops->close_file(td, f);
335 return 1;
336#else
337 f->real_file_size = -1ULL;
338 return 0;
339#endif
340}
341
Jens Axboe53cdc682006-10-18 11:50:58 +0200342static int get_file_size(struct thread_data *td, struct fio_file *f)
343{
344 int ret = 0;
345
Jens Axboed6aed792009-06-03 08:41:15 +0200346 if (fio_file_size_known(f))
Jens Axboe409b3412007-03-28 09:57:01 +0200347 return 0;
348
Jens Axboe7bb48f82007-03-27 15:30:28 +0200349 if (f->filetype == FIO_TYPE_FILE)
350 ret = file_size(td, f);
351 else if (f->filetype == FIO_TYPE_BD)
Jens Axboe53cdc682006-10-18 11:50:58 +0200352 ret = bdev_size(td, f);
Jens Axboe4ccdccd2010-06-24 10:27:22 +0200353 else if (f->filetype == FIO_TYPE_CHAR)
354 ret = char_size(td, f);
Jens Axboe53cdc682006-10-18 11:50:58 +0200355 else
356 f->real_file_size = -1;
357
358 if (ret)
359 return ret;
360
361 if (f->file_offset > f->real_file_size) {
Bruce Cran0f2152c2010-12-15 10:33:03 +0100362 log_err("%s: offset extends end (%llu > %llu)\n", td->o.name,
Jens Axboe4e0a8fa2013-04-15 11:40:57 +0200363 (unsigned long long) f->file_offset,
364 (unsigned long long) f->real_file_size);
Jens Axboe53cdc682006-10-18 11:50:58 +0200365 return 1;
366 }
367
Jens Axboed6aed792009-06-03 08:41:15 +0200368 fio_file_set_size_known(f);
Jens Axboe53cdc682006-10-18 11:50:58 +0200369 return 0;
370}
371
Jens Axboe3baddf22008-04-04 11:10:30 +0200372static int __file_invalidate_cache(struct thread_data *td, struct fio_file *f,
373 unsigned long long off,
374 unsigned long long len)
Jens Axboee5b401d2006-10-18 16:03:40 +0200375{
376 int ret = 0;
377
Jens Axboe5e0074c2009-06-02 21:40:09 +0200378 if (len == -1ULL)
Jens Axboe3baddf22008-04-04 11:10:30 +0200379 len = f->io_size;
380 if (off == -1ULL)
381 off = f->file_offset;
Jens Axboeee56ad52008-02-01 10:30:20 +0100382
Jens Axboe0d1cd202009-06-05 22:11:52 +0200383 if (len == -1ULL || off == -1ULL)
384 return 0;
385
Jens Axboe3baddf22008-04-04 11:10:30 +0200386 dprint(FD_IO, "invalidate cache %s: %llu/%llu\n", f->file_name, off,
387 len);
Jens Axboeb5af8292007-03-08 12:43:13 +0100388
Jens Axboee5b401d2006-10-18 16:03:40 +0200389 /*
390 * FIXME: add blockdev flushing too
391 */
Jens Axboea1c58072009-08-04 23:17:02 +0200392 if (f->mmap_ptr) {
Bruce Cran03e20d62011-01-02 20:14:54 +0100393 ret = posix_madvise(f->mmap_ptr, f->mmap_sz, POSIX_MADV_DONTNEED);
Jens Axboea1c58072009-08-04 23:17:02 +0200394#ifdef FIO_MADV_FREE
Jens Axboe3e10fb82013-08-09 14:31:06 -0600395 if (f->filetype == FIO_TYPE_BD)
396 (void) posix_madvise(f->mmap_ptr, f->mmap_sz, FIO_MADV_FREE);
Jens Axboea1c58072009-08-04 23:17:02 +0200397#endif
398 } else if (f->filetype == FIO_TYPE_FILE) {
Bruce Cranecc314b2011-01-04 10:59:30 +0100399 ret = posix_fadvise(f->fd, off, len, POSIX_FADV_DONTNEED);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100400 } else if (f->filetype == FIO_TYPE_BD) {
Bruce Cranecc314b2011-01-04 10:59:30 +0100401 ret = blockdev_invalidate_cache(f);
Jens Axboe7e0e25c2007-03-27 12:49:56 +0200402 if (ret < 0 && errno == EACCES && geteuid()) {
Jens Axboe7172cfe2007-07-23 14:36:00 +0200403 if (!root_warn) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100404 log_err("fio: only root may flush block "
405 "devices. Cache flush bypassed!\n");
Jens Axboe7172cfe2007-07-23 14:36:00 +0200406 root_warn = 1;
407 }
Jens Axboe7e0e25c2007-03-27 12:49:56 +0200408 ret = 0;
409 }
Jens Axboeb5605e92007-04-16 20:42:33 +0200410 } else if (f->filetype == FIO_TYPE_CHAR || f->filetype == FIO_TYPE_PIPE)
Jens Axboee5b401d2006-10-18 16:03:40 +0200411 ret = 0;
412
413 if (ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100414 td_verror(td, errno, "invalidate_cache");
Jens Axboee5b401d2006-10-18 16:03:40 +0200415 return 1;
Jens Axboe3baddf22008-04-04 11:10:30 +0200416 } else if (ret > 0) {
417 td_verror(td, ret, "invalidate_cache");
418 return 1;
Jens Axboee5b401d2006-10-18 16:03:40 +0200419 }
420
Jens Axboead2da602007-02-26 12:33:27 +0100421 return ret;
Jens Axboe3baddf22008-04-04 11:10:30 +0200422
423}
424
425int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
426{
Jens Axboed6aed792009-06-03 08:41:15 +0200427 if (!fio_file_open(f))
Jens Axboea5fb4612008-05-28 10:54:01 +0200428 return 0;
429
Jens Axboe5e0074c2009-06-02 21:40:09 +0200430 return __file_invalidate_cache(td, f, -1ULL, -1ULL);
Jens Axboee5b401d2006-10-18 16:03:40 +0200431}
432
Jens Axboe6977bcd2008-03-01 15:55:36 +0100433int generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
Jens Axboe53cdc682006-10-18 11:50:58 +0200434{
Jens Axboe6977bcd2008-03-01 15:55:36 +0100435 int ret = 0;
436
Jens Axboeee56ad52008-02-01 10:30:20 +0100437 dprint(FD_FILE, "fd close %s\n", f->file_name);
Jens Axboe4906e0b2008-03-01 18:59:51 +0100438
439 remove_file_hash(f);
440
Jens Axboe6977bcd2008-03-01 15:55:36 +0100441 if (close(f->fd) < 0)
442 ret = errno;
443
Jens Axboeb5af8292007-03-08 12:43:13 +0100444 f->fd = -1;
Jens Axboee6c4d732012-12-12 08:16:27 +0100445
446 if (f->shadow_fd != -1) {
447 close(f->shadow_fd);
448 f->shadow_fd = -1;
449 }
450
Juan Casse57e54e02013-09-20 09:20:52 -0600451 f->engine_data = 0;
Jens Axboe6977bcd2008-03-01 15:55:36 +0100452 return ret;
Jens Axboe53cdc682006-10-18 11:50:58 +0200453}
454
Dmitry Monakhov1ccc6dc2012-09-19 23:22:53 +0400455int file_lookup_open(struct fio_file *f, int flags)
Jens Axboe53cdc682006-10-18 11:50:58 +0200456{
Jens Axboe29c13492008-03-01 19:25:20 +0100457 struct fio_file *__f;
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100458 int from_hash;
459
460 __f = lookup_file_hash(f->file_name);
461 if (__f) {
Jens Axboe9efef3c2008-03-06 10:26:25 +0100462 dprint(FD_FILE, "found file in hash %s\n", f->file_name);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100463 /*
464 * racy, need the __f->lock locked
465 */
466 f->lock = __f->lock;
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100467 from_hash = 1;
468 } else {
Jens Axboe9efef3c2008-03-06 10:26:25 +0100469 dprint(FD_FILE, "file not found in hash %s\n", f->file_name);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100470 from_hash = 0;
471 }
472
Jens Axboee8670ef2008-03-06 12:06:30 +0100473 f->fd = open(f->file_name, flags, 0600);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100474 return from_hash;
475}
476
Jens Axboee6c4d732012-12-12 08:16:27 +0100477static int file_close_shadow_fds(struct thread_data *td)
478{
479 struct fio_file *f;
480 int num_closed = 0;
481 unsigned int i;
482
483 for_each_file(td, f, i) {
484 if (f->shadow_fd == -1)
485 continue;
486
487 close(f->shadow_fd);
488 f->shadow_fd = -1;
489 num_closed++;
490 }
491
492 return num_closed;
493}
494
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100495int generic_open_file(struct thread_data *td, struct fio_file *f)
496{
Jens Axboe66159822007-04-16 21:54:24 +0200497 int is_std = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200498 int flags = 0;
Jens Axboe29c13492008-03-01 19:25:20 +0100499 int from_hash = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200500
Jens Axboeee56ad52008-02-01 10:30:20 +0100501 dprint(FD_FILE, "fd open %s\n", f->file_name);
502
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200503 if (td_trim(td) && f->filetype != FIO_TYPE_BD) {
504 log_err("fio: trim only applies to block device\n");
505 return 1;
506 }
507
Jens Axboe66159822007-04-16 21:54:24 +0200508 if (!strcmp(f->file_name, "-")) {
509 if (td_rw(td)) {
510 log_err("fio: can't read/write to stdin/out\n");
511 return 1;
512 }
513 is_std = 1;
Jens Axboece98fa62007-04-17 13:27:32 +0200514
515 /*
516 * move output logging to stderr, if we are writing to stdout
517 */
518 if (td_write(td))
519 f_out = stderr;
Jens Axboe66159822007-04-16 21:54:24 +0200520 }
521
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200522 if (td_trim(td))
523 goto skip_flags;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100524 if (td->o.odirect)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100525 flags |= OS_O_DIRECT;
Chris Masond01612f2013-11-15 15:52:58 -0700526 if (td->o.oatomic) {
527 if (!FIO_O_ATOMIC) {
528 td_verror(td, EINVAL, "OS does not support atomic IO");
529 return 1;
530 }
531 flags |= OS_O_DIRECT | FIO_O_ATOMIC;
532 }
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100533 if (td->o.sync_io)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100534 flags |= O_SYNC;
Jens Axboe814452b2009-03-04 12:53:13 +0100535 if (td->o.create_on_open)
536 flags |= O_CREAT;
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200537skip_flags:
538 if (f->filetype != FIO_TYPE_FILE)
539 flags |= FIO_O_NOATIME;
Jens Axboe7abf8332006-11-23 15:01:19 +0100540
Aaron Carroll056f3452007-11-26 09:08:53 +0100541open_again:
Jens Axboe660a1cb2007-04-17 15:37:47 +0200542 if (td_write(td)) {
Jens Axboe17308152008-03-07 13:42:31 +0100543 if (!read_only)
544 flags |= O_RDWR;
Jens Axboe53cdc682006-10-18 11:50:58 +0200545
Jens Axboeaf52b342007-03-13 10:07:47 +0100546 if (f->filetype == FIO_TYPE_FILE)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100547 flags |= O_CREAT;
Jens Axboe2fd233b2007-03-02 08:44:25 +0100548
Jens Axboe66159822007-04-16 21:54:24 +0200549 if (is_std)
550 f->fd = dup(STDOUT_FILENO);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100551 else
552 from_hash = file_lookup_open(f, flags);
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200553 } else if (td_read(td)) {
Jens Axboe4241ea82007-09-12 08:18:36 +0200554 if (f->filetype == FIO_TYPE_CHAR && !read_only)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100555 flags |= O_RDWR;
556 else
557 flags |= O_RDONLY;
558
Jens Axboe66159822007-04-16 21:54:24 +0200559 if (is_std)
560 f->fd = dup(STDIN_FILENO);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100561 else
562 from_hash = file_lookup_open(f, flags);
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200563 } else { //td trim
564 flags |= O_RDWR;
565 from_hash = file_lookup_open(f, flags);
Jens Axboe53cdc682006-10-18 11:50:58 +0200566 }
567
568 if (f->fd == -1) {
Jens Axboee4e33252007-03-21 13:07:54 +0100569 char buf[FIO_VERROR_SIZE];
Jens Axboee1161c32007-02-22 19:36:48 +0100570 int __e = errno;
571
Jens Axboe835d9b92009-06-09 15:23:38 +0200572 if (__e == EPERM && (flags & FIO_O_NOATIME)) {
Jens Axboe5921e802008-05-30 15:02:38 +0200573 flags &= ~FIO_O_NOATIME;
Aaron Carroll056f3452007-11-26 09:08:53 +0100574 goto open_again;
575 }
Jens Axboee6c4d732012-12-12 08:16:27 +0100576 if (__e == EMFILE && file_close_shadow_fds(td))
577 goto open_again;
Aaron Carroll056f3452007-11-26 09:08:53 +0100578
Ken Raeburn98ffb8f2013-01-30 22:31:09 +0100579 snprintf(buf, sizeof(buf), "open(%s)", f->file_name);
Jens Axboee4e33252007-03-21 13:07:54 +0100580
Jens Axboea93c5f02012-06-11 08:53:53 +0200581 if (__e == EINVAL && (flags & OS_O_DIRECT)) {
582 log_err("fio: looks like your file system does not " \
583 "support direct=1/buffered=0\n");
584 }
585
Jens Axboee4e33252007-03-21 13:07:54 +0100586 td_verror(td, __e, buf);
Jens Axboe53cdc682006-10-18 11:50:58 +0200587 }
588
Jens Axboe29c13492008-03-01 19:25:20 +0100589 if (!from_hash && f->fd != -1) {
590 if (add_file_hash(f)) {
Jens Axboe3f0ca9b2011-05-24 21:36:24 +0200591 int fio_unused ret;
Jens Axboe29c13492008-03-01 19:25:20 +0100592
593 /*
Jens Axboee6c4d732012-12-12 08:16:27 +0100594 * Stash away descriptor for later close. This is to
595 * work-around a "feature" on Linux, where a close of
596 * an fd that has been opened for write will trigger
597 * udev to call blkid to check partitions, fs id, etc.
Anatol Pomozovde8f6de2013-09-26 16:31:34 -0700598 * That pollutes the device cache, which can slow down
Jens Axboee6c4d732012-12-12 08:16:27 +0100599 * unbuffered accesses.
Jens Axboe29c13492008-03-01 19:25:20 +0100600 */
Jens Axboee6c4d732012-12-12 08:16:27 +0100601 if (f->shadow_fd == -1)
602 f->shadow_fd = f->fd;
603 else {
604 /*
605 * OK to ignore, we haven't done anything
606 * with it
607 */
608 ret = generic_close_file(td, f);
609 }
Jens Axboe29c13492008-03-01 19:25:20 +0100610 goto open_again;
611 }
612 }
Jens Axboe4906e0b2008-03-01 18:59:51 +0100613
Jens Axboe53cdc682006-10-18 11:50:58 +0200614 return 0;
Jens Axboeb5af8292007-03-08 12:43:13 +0100615}
616
Jens Axboedf9c26b2009-03-05 10:13:58 +0100617int generic_get_file_size(struct thread_data *td, struct fio_file *f)
Jens Axboe21972cd2006-11-23 12:39:16 +0100618{
Jens Axboedf9c26b2009-03-05 10:13:58 +0100619 return get_file_size(td, f);
Jens Axboe21972cd2006-11-23 12:39:16 +0100620}
621
Jens Axboe7bb48f82007-03-27 15:30:28 +0200622/*
623 * open/close all files, so that ->real_file_size gets set
624 */
Jens Axboebab3fd52007-04-02 10:34:18 +0200625static int get_file_sizes(struct thread_data *td)
Jens Axboe53cdc682006-10-18 11:50:58 +0200626{
627 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100628 unsigned int i;
Jens Axboebab3fd52007-04-02 10:34:18 +0200629 int err = 0;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200630
631 for_each_file(td, f, i) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100632 dprint(FD_FILE, "get file size for %p/%d/%p\n", f, i,
633 f->file_name);
Jens Axboe9efef3c2008-03-06 10:26:25 +0100634
Jens Axboe99a47c62009-04-07 22:20:56 +0200635 if (td_io_get_file_size(td, f)) {
Jens Axboe40b44f42007-04-11 12:43:35 +0200636 if (td->error != ENOENT) {
637 log_err("%s\n", td->verror);
638 err = 1;
639 }
Jens Axboe541d66d2007-03-27 19:50:11 +0200640 clear_error(td);
Jens Axboe07eb79d2007-04-12 13:02:38 +0200641 }
Jens Axboe409b3412007-03-28 09:57:01 +0200642
643 if (f->real_file_size == -1ULL && td->o.size)
644 f->real_file_size = td->o.size / td->o.nr_files;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200645 }
Jens Axboebab3fd52007-04-02 10:34:18 +0200646
647 return err;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200648}
649
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200650struct fio_mount {
651 struct flist_head list;
652 const char *base;
653 char __base[256];
654 unsigned int key;
655};
656
657/*
658 * Get free number of bytes for each file on each unique mount.
659 */
660static unsigned long long get_fs_free_counts(struct thread_data *td)
661{
662 struct flist_head *n, *tmp;
Jens Axboe68b03162010-06-24 09:22:41 +0200663 unsigned long long ret = 0;
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200664 struct fio_mount *fm;
665 FLIST_HEAD(list);
666 struct fio_file *f;
667 unsigned int i;
668
669 for_each_file(td, f, i) {
670 struct stat sb;
671 char buf[256];
672
Jens Axboe4ccdccd2010-06-24 10:27:22 +0200673 if (f->filetype == FIO_TYPE_BD || f->filetype == FIO_TYPE_CHAR) {
674 if (f->real_file_size != -1ULL)
675 ret += f->real_file_size;
Jens Axboe68b03162010-06-24 09:22:41 +0200676 continue;
677 } else if (f->filetype != FIO_TYPE_FILE)
678 continue;
679
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200680 strcpy(buf, f->file_name);
681
682 if (stat(buf, &sb) < 0) {
683 if (errno != ENOENT)
684 break;
685 strcpy(buf, ".");
686 if (stat(buf, &sb) < 0)
687 break;
688 }
689
690 fm = NULL;
691 flist_for_each(n, &list) {
692 fm = flist_entry(n, struct fio_mount, list);
693 if (fm->key == sb.st_dev)
694 break;
695
696 fm = NULL;
697 }
698
699 if (fm)
700 continue;
701
702 fm = malloc(sizeof(*fm));
703 strcpy(fm->__base, buf);
704 fm->base = basename(fm->__base);
705 fm->key = sb.st_dev;
706 flist_add(&fm->list, &list);
707 }
708
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200709 flist_for_each_safe(n, tmp, &list) {
710 unsigned long long sz;
711
712 fm = flist_entry(n, struct fio_mount, list);
713 flist_del(&fm->list);
714
715 sz = get_fs_size(fm->base);
716 if (sz && sz != -1ULL)
717 ret += sz;
718
719 free(fm);
720 }
721
722 return ret;
723}
724
Jens Axboebedc9dc2014-03-17 12:51:09 -0600725uint64_t get_start_offset(struct thread_data *td, struct fio_file *f)
Dan Ehrenbergce95d652012-08-16 08:58:21 +0200726{
Jens Axboebedc9dc2014-03-17 12:51:09 -0600727 struct thread_options *o = &td->o;
728
729 if (o->file_append && f->filetype == FIO_TYPE_FILE)
730 return f->real_file_size;
731
Dan Ehrenbergce95d652012-08-16 08:58:21 +0200732 return td->o.start_offset +
733 (td->thread_number - 1) * td->o.offset_increment;
734}
735
Jens Axboe7bb48f82007-03-27 15:30:28 +0200736/*
737 * Open the files and setup files sizes, creating files if necessary.
738 */
739int setup_files(struct thread_data *td)
740{
741 unsigned long long total_size, extend_size;
Jens Axboede98bd32013-04-05 11:09:20 +0200742 struct thread_options *o = &td->o;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200743 struct fio_file *f;
Jens Axboe002fe732014-02-11 08:31:13 -0700744 unsigned int i, nr_fs_extra = 0;
Jens Axboe000b0802007-03-27 15:56:10 +0200745 int err = 0, need_extend;
Jens Axboee90391e2013-04-13 20:40:53 +0200746 int old_state;
Jens Axboe002fe732014-02-11 08:31:13 -0700747 const unsigned int bs = td_min_bs(td);
748 uint64_t fs = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200749
Jens Axboeee56ad52008-02-01 10:30:20 +0100750 dprint(FD_FILE, "setup files\n");
751
Jens Axboe8edd9732014-03-01 08:24:03 -0700752 old_state = td_bump_runstate(td, TD_SETTING_UP);
Jens Axboee90391e2013-04-13 20:40:53 +0200753
Jens Axboede98bd32013-04-05 11:09:20 +0200754 if (o->read_iolog_file)
Jens Axboe25460cf2012-05-02 13:58:02 +0200755 goto done;
Jens Axboe691c8fb2008-03-07 14:26:26 +0100756
Jens Axboe53cdc682006-10-18 11:50:58 +0200757 /*
758 * if ioengine defines a setup() method, it's responsible for
Jens Axboe7bb48f82007-03-27 15:30:28 +0200759 * opening the files and setting f->real_file_size to indicate
760 * the valid range for that file.
Jens Axboe53cdc682006-10-18 11:50:58 +0200761 */
762 if (td->io_ops->setup)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200763 err = td->io_ops->setup(td);
764 else
Jens Axboebab3fd52007-04-02 10:34:18 +0200765 err = get_file_sizes(td);
Jens Axboe53cdc682006-10-18 11:50:58 +0200766
Jens Axboef1027062006-10-20 10:14:01 +0200767 if (err)
Jens Axboee90391e2013-04-13 20:40:53 +0200768 goto err_out;
Jens Axboef1027062006-10-20 10:14:01 +0200769
Jens Axboe0a7eb122006-10-20 10:36:42 +0200770 /*
Jens Axboe7bb48f82007-03-27 15:30:28 +0200771 * check sizes. if the files/devices do not exist and the size
772 * isn't passed to fio, abort.
Jens Axboe0a7eb122006-10-20 10:36:42 +0200773 */
Jens Axboe7bb48f82007-03-27 15:30:28 +0200774 total_size = 0;
775 for_each_file(td, f, i) {
776 if (f->real_file_size == -1ULL)
777 total_size = -1ULL;
778 else
779 total_size += f->real_file_size;
780 }
Jens Axboe0a7eb122006-10-20 10:36:42 +0200781
Jens Axboede98bd32013-04-05 11:09:20 +0200782 if (o->fill_device)
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200783 td->fill_device_size = get_fs_free_counts(td);
784
Jens Axboe7bb48f82007-03-27 15:30:28 +0200785 /*
786 * device/file sizes are zero and no size given, punt
787 */
Jens Axboede98bd32013-04-05 11:09:20 +0200788 if ((!total_size || total_size == -1ULL) && !o->size &&
789 !(td->io_ops->flags & FIO_NOIO) && !o->fill_device &&
790 !(o->nr_files && (o->file_size_low || o->file_size_high))) {
791 log_err("%s: you need to specify size=\n", o->name);
Jens Axboee1161c32007-02-22 19:36:48 +0100792 td_verror(td, EINVAL, "total_file_size");
Jens Axboee90391e2013-04-13 20:40:53 +0200793 goto err_out;
Jens Axboe53cdc682006-10-18 11:50:58 +0200794 }
795
Jens Axboe7bb48f82007-03-27 15:30:28 +0200796 /*
Jens Axboe002fe732014-02-11 08:31:13 -0700797 * Calculate per-file size and potential extra size for the
798 * first files, if needed.
799 */
800 if (!o->file_size_low) {
801 uint64_t all_fs;
802
803 fs = o->size / o->nr_files;
804 all_fs = fs * o->nr_files;
805
806 if (all_fs < o->size)
807 nr_fs_extra = (o->size - all_fs) / bs;
808 }
809
810 /*
Jens Axboe7bb48f82007-03-27 15:30:28 +0200811 * now file sizes are known, so we can set ->io_size. if size= is
812 * not given, ->io_size is just equal to ->real_file_size. if size
813 * is given, ->io_size is size / nr_files.
814 */
815 extend_size = total_size = 0;
816 need_extend = 0;
817 for_each_file(td, f, i) {
Jens Axboebedc9dc2014-03-17 12:51:09 -0600818 f->file_offset = get_start_offset(td, f);
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200819
Jens Axboede98bd32013-04-05 11:09:20 +0200820 if (!o->file_size_low) {
Jens Axboe7bb48f82007-03-27 15:30:28 +0200821 /*
822 * no file size range given, file size is equal to
Jens Axboe002fe732014-02-11 08:31:13 -0700823 * total size divided by number of files. If that is
824 * zero, set it to the real file size. If the size
825 * doesn't divide nicely with the min blocksize,
826 * make the first files bigger.
Jens Axboe7bb48f82007-03-27 15:30:28 +0200827 */
Jens Axboe002fe732014-02-11 08:31:13 -0700828 f->io_size = fs;
829 if (nr_fs_extra) {
830 nr_fs_extra--;
831 f->io_size += bs;
832 }
833
Jens Axboe65bdb102008-01-24 13:13:12 +0100834 if (!f->io_size)
Jens Axboe273f8c92008-01-25 14:02:15 +0100835 f->io_size = f->real_file_size - f->file_offset;
Jens Axboede98bd32013-04-05 11:09:20 +0200836 } else if (f->real_file_size < o->file_size_low ||
837 f->real_file_size > o->file_size_high) {
838 if (f->file_offset > o->file_size_low)
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200839 goto err_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200840 /*
841 * file size given. if it's fixed, use that. if it's a
842 * range, generate a random size in-between.
843 */
Jens Axboede98bd32013-04-05 11:09:20 +0200844 if (o->file_size_low == o->file_size_high)
845 f->io_size = o->file_size_low - f->file_offset;
846 else {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100847 f->io_size = get_rand_file_size(td)
848 - f->file_offset;
849 }
Jens Axboe65bdb102008-01-24 13:13:12 +0100850 } else
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200851 f->io_size = f->real_file_size - f->file_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200852
853 if (f->io_size == -1ULL)
854 total_size = -1ULL;
Shaohua Li4d002562012-09-21 08:32:32 +0200855 else {
Jens Axboede98bd32013-04-05 11:09:20 +0200856 if (o->size_percent)
857 f->io_size = (f->io_size * o->size_percent) / 100;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200858 total_size += f->io_size;
Shaohua Li4d002562012-09-21 08:32:32 +0200859 }
Jens Axboe7bb48f82007-03-27 15:30:28 +0200860
861 if (f->filetype == FIO_TYPE_FILE &&
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200862 (f->io_size + f->file_offset) > f->real_file_size &&
Jens Axboe7bb48f82007-03-27 15:30:28 +0200863 !(td->io_ops->flags & FIO_DISKLESSIO)) {
Jens Axboede98bd32013-04-05 11:09:20 +0200864 if (!o->create_on_open) {
Jens Axboe814452b2009-03-04 12:53:13 +0100865 need_extend++;
866 extend_size += (f->io_size + f->file_offset);
867 } else
868 f->real_file_size = f->io_size + f->file_offset;
Jens Axboed6aed792009-06-03 08:41:15 +0200869 fio_file_set_extend(f);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100870 }
Jens Axboe7bb48f82007-03-27 15:30:28 +0200871 }
872
Jens Axboede98bd32013-04-05 11:09:20 +0200873 if (!o->size || o->size > total_size)
874 o->size = total_size;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200875
876 /*
877 * See if we need to extend some files
878 */
879 if (need_extend) {
880 temp_stall_ts = 1;
Jens Axboef3afa572012-09-17 13:34:16 +0200881 if (output_format == FIO_OUTPUT_NORMAL)
Shaozhi Shawn Yea7ba8c52008-09-10 09:04:18 +0200882 log_info("%s: Laying out IO file(s) (%u file(s) /"
Jens Axboede98bd32013-04-05 11:09:20 +0200883 " %lluMB)\n", o->name, need_extend,
Shaozhi Shawn Yea7ba8c52008-09-10 09:04:18 +0200884 extend_size >> 20);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200885
886 for_each_file(td, f, i) {
Jens Axboe5e0074c2009-06-02 21:40:09 +0200887 unsigned long long old_len = -1ULL, extend_len = -1ULL;
Jens Axboe3baddf22008-04-04 11:10:30 +0200888
Jens Axboed6aed792009-06-03 08:41:15 +0200889 if (!fio_file_extend(f))
Jens Axboe7bb48f82007-03-27 15:30:28 +0200890 continue;
891
Jens Axboe409b3412007-03-28 09:57:01 +0200892 assert(f->filetype == FIO_TYPE_FILE);
Jens Axboed6aed792009-06-03 08:41:15 +0200893 fio_file_clear_extend(f);
Jens Axboede98bd32013-04-05 11:09:20 +0200894 if (!o->fill_device) {
Jens Axboe5e0074c2009-06-02 21:40:09 +0200895 old_len = f->real_file_size;
Jens Axboe0b9d69e2009-09-11 22:29:54 +0200896 extend_len = f->io_size + f->file_offset -
897 old_len;
Jens Axboe5e0074c2009-06-02 21:40:09 +0200898 }
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200899 f->real_file_size = (f->io_size + f->file_offset);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200900 err = extend_file(td, f);
901 if (err)
902 break;
Jens Axboe5e0074c2009-06-02 21:40:09 +0200903
Jens Axboe3baddf22008-04-04 11:10:30 +0200904 err = __file_invalidate_cache(td, f, old_len,
905 extend_len);
906 close(f->fd);
907 f->fd = -1;
908 if (err)
909 break;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200910 }
911 temp_stall_ts = 0;
912 }
913
914 if (err)
Jens Axboee90391e2013-04-13 20:40:53 +0200915 goto err_out;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200916
Jens Axboede98bd32013-04-05 11:09:20 +0200917 if (!o->zone_size)
918 o->zone_size = o->size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200919
Jens Axboeea966f82007-09-03 10:09:37 +0200920 /*
921 * iolog already set the total io size, if we read back
922 * stored entries.
923 */
Jens Axboede98bd32013-04-05 11:09:20 +0200924 if (!o->read_iolog_file)
925 td->total_io_size = o->size * o->loops;
Jens Axboe25460cf2012-05-02 13:58:02 +0200926
927done:
Jens Axboede98bd32013-04-05 11:09:20 +0200928 if (o->create_only)
Jens Axboe25460cf2012-05-02 13:58:02 +0200929 td->done = 1;
930
Jens Axboe8edd9732014-03-01 08:24:03 -0700931 td_restore_runstate(td, old_state);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200932 return 0;
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200933err_offset:
Jens Axboede98bd32013-04-05 11:09:20 +0200934 log_err("%s: you need to specify valid offset=\n", o->name);
Jens Axboee90391e2013-04-13 20:40:53 +0200935err_out:
Jens Axboe8edd9732014-03-01 08:24:03 -0700936 td_restore_runstate(td, old_state);
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200937 return 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200938}
939
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200940int pre_read_files(struct thread_data *td)
941{
942 struct fio_file *f;
943 unsigned int i;
944
945 dprint(FD_FILE, "pre_read files\n");
946
947 for_each_file(td, f, i) {
948 pre_read_file(td, f);
949 }
950
951 return 1;
952}
953
Jens Axboe9c6f6312012-11-07 09:15:45 +0100954static int __init_rand_distribution(struct thread_data *td, struct fio_file *f)
955{
Jens Axboe23162962012-11-07 19:47:47 +0100956 unsigned int range_size, seed;
Jens Axboe9c6f6312012-11-07 09:15:45 +0100957 unsigned long nranges;
Jens Axboe21415db2013-01-04 13:09:29 +0100958 uint64_t file_size;
Jens Axboe9c6f6312012-11-07 09:15:45 +0100959
960 range_size = min(td->o.min_bs[DDIR_READ], td->o.min_bs[DDIR_WRITE]);
Jens Axboe21415db2013-01-04 13:09:29 +0100961 file_size = min(f->real_file_size, f->io_size);
Jens Axboe9c6f6312012-11-07 09:15:45 +0100962
Jens Axboe21415db2013-01-04 13:09:29 +0100963 nranges = (file_size + range_size - 1) / range_size;
Jens Axboe9c6f6312012-11-07 09:15:45 +0100964
Jens Axboe23162962012-11-07 19:47:47 +0100965 seed = jhash(f->file_name, strlen(f->file_name), 0) * td->thread_number;
Jens Axboe84256872012-11-17 10:06:36 -0700966 if (!td->o.rand_repeatable)
967 seed = td->rand_seeds[4];
968
Jens Axboe9c6f6312012-11-07 09:15:45 +0100969 if (td->o.random_distribution == FIO_RAND_DIST_ZIPF)
Jens Axboe888677a2013-04-10 22:19:46 +0200970 zipf_init(&f->zipf, nranges, td->o.zipf_theta.u.f, seed);
Jens Axboe9c6f6312012-11-07 09:15:45 +0100971 else
Jens Axboe888677a2013-04-10 22:19:46 +0200972 pareto_init(&f->zipf, nranges, td->o.pareto_h.u.f, seed);
Jens Axboe9c6f6312012-11-07 09:15:45 +0100973
974 return 1;
975}
976
977static int init_rand_distribution(struct thread_data *td)
978{
979 struct fio_file *f;
980 unsigned int i;
981 int state;
982
983 if (td->o.random_distribution == FIO_RAND_DIST_RANDOM)
984 return 0;
985
Jens Axboe8edd9732014-03-01 08:24:03 -0700986 state = td_bump_runstate(td, TD_SETTING_UP);
987
Jens Axboe9c6f6312012-11-07 09:15:45 +0100988 for_each_file(td, f, i)
989 __init_rand_distribution(td, f);
Jens Axboe8edd9732014-03-01 08:24:03 -0700990
991 td_restore_runstate(td, state);
Jens Axboe9c6f6312012-11-07 09:15:45 +0100992
993 return 1;
994}
995
Jens Axboe68727072007-03-15 20:49:25 +0100996int init_random_map(struct thread_data *td)
997{
Jens Axboe51ede0b2012-11-22 13:50:29 +0100998 unsigned long long blocks;
Jens Axboe68727072007-03-15 20:49:25 +0100999 struct fio_file *f;
1000 unsigned int i;
1001
Jens Axboe9c6f6312012-11-07 09:15:45 +01001002 if (init_rand_distribution(td))
1003 return 0;
Jens Axboe3831a842012-11-26 19:59:14 +01001004 if (!td_random(td))
Jens Axboe68727072007-03-15 20:49:25 +01001005 return 0;
1006
1007 for_each_file(td, f, i) {
Jens Axboe38f30c82013-01-11 14:03:05 +01001008 uint64_t file_size = min(f->real_file_size, f->io_size);
1009
Jens Axboe53737ae2013-02-21 15:18:17 +01001010 blocks = file_size / (unsigned long long) td->o.rw_min_bs;
1011
Jens Axboe8055e412012-11-26 08:43:47 +01001012 if (td->o.random_generator == FIO_RAND_GEN_LFSR) {
Jens Axboe82af46b2012-12-04 13:12:25 +01001013 unsigned long seed;
1014
1015 seed = td->rand_seeds[FIO_RAND_BLOCK_OFF];
1016
Jens Axboe225ba9e2014-02-26 14:31:15 -08001017 if (!lfsr_init(&f->lfsr, blocks, seed, 0))
Jens Axboe8055e412012-11-26 08:43:47 +01001018 continue;
Jens Axboe3831a842012-11-26 19:59:14 +01001019 } else if (!td->o.norandommap) {
Jens Axboe7ebd7962012-11-28 21:24:46 +01001020 f->io_axmap = axmap_new(blocks);
1021 if (f->io_axmap)
Jens Axboe8055e412012-11-26 08:43:47 +01001022 continue;
Jens Axboe1cad7122012-11-29 21:37:11 +01001023 } else if (td->o.norandommap)
1024 continue;
Jens Axboeceadd592012-02-02 08:41:28 +01001025
Jens Axboe2b386d22008-03-26 10:32:57 +01001026 if (!td->o.softrandommap) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001027 log_err("fio: failed allocating random map. If running"
1028 " a large number of jobs, try the 'norandommap'"
Jens Axboe2b386d22008-03-26 10:32:57 +01001029 " option or set 'softrandommap'. Or give"
1030 " a larger --alloc-size to fio.\n");
Jens Axboe68727072007-03-15 20:49:25 +01001031 return 1;
1032 }
Jens Axboe303032a2008-03-26 10:11:10 +01001033
1034 log_info("fio: file %s failed allocating random map. Running "
1035 "job without.\n", f->file_name);
Jens Axboe68727072007-03-15 20:49:25 +01001036 }
1037
1038 return 0;
1039}
1040
Jens Axboe53cdc682006-10-18 11:50:58 +02001041void close_files(struct thread_data *td)
1042{
Jens Axboe0ab8db82006-10-18 17:16:23 +02001043 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +01001044 unsigned int i;
Jens Axboe53cdc682006-10-18 11:50:58 +02001045
Jens Axboe2be3eec2009-06-10 09:05:13 +02001046 for_each_file(td, f, i) {
1047 if (fio_file_open(f))
1048 td_io_close_file(td, f);
1049 }
Jens Axboe24ffd2c2008-03-01 15:47:08 +01001050}
1051
1052void close_and_free_files(struct thread_data *td)
1053{
1054 struct fio_file *f;
1055 unsigned int i;
1056
Jens Axboeee56ad52008-02-01 10:30:20 +01001057 dprint(FD_FILE, "close files\n");
1058
Jens Axboe0ab8db82006-10-18 17:16:23 +02001059 for_each_file(td, f, i) {
Jens Axboe22a57ba2009-06-09 14:34:35 +02001060 if (fio_file_open(f))
1061 td_io_close_file(td, f);
1062
Shaozhi Shawn Yeb9fbcf22008-09-10 09:05:17 +02001063 remove_file_hash(f);
Jens Axboeb3dc7f02007-03-09 12:52:15 +01001064
Jens Axboeb5f4d8b2014-03-20 08:28:11 -06001065 if (td->o.unlink && f->filetype == FIO_TYPE_FILE) {
1066 dprint(FD_FILE, "free unlink %s\n", f->file_name);
1067 unlink(f->file_name);
1068 }
1069
Jens Axboef17c4392008-03-01 18:40:46 +01001070 sfree(f->file_name);
Jens Axboefa1da862007-03-23 15:20:54 +01001071 f->file_name = NULL;
Jens Axboe7ebd7962012-11-28 21:24:46 +01001072 axmap_free(f->io_axmap);
1073 f->io_axmap = NULL;
Jens Axboe78d99e62008-03-01 18:41:50 +01001074 sfree(f);
Jens Axboe53cdc682006-10-18 11:50:58 +02001075 }
Jens Axboeb4a6a592006-10-20 13:54:47 +02001076
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001077 td->o.filename = NULL;
Jens Axboecade3ef2007-03-23 15:29:45 +01001078 free(td->files);
Jens Axboed7df1d12013-03-20 19:57:01 -06001079 free(td->file_locks);
Jens Axboe9efef3c2008-03-06 10:26:25 +01001080 td->files_index = 0;
Jens Axboeb4a6a592006-10-20 13:54:47 +02001081 td->files = NULL;
Jens Axboed7df1d12013-03-20 19:57:01 -06001082 td->file_locks = NULL;
Jens Axboe27ddbfa2014-01-22 16:27:32 -08001083 td->o.file_lock_mode = FILE_LOCK_NONE;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001084 td->o.nr_files = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +02001085}
Jens Axboeaf52b342007-03-13 10:07:47 +01001086
Jens Axboee3bab462007-03-13 11:22:09 +01001087static void get_file_type(struct fio_file *f)
Jens Axboeaf52b342007-03-13 10:07:47 +01001088{
1089 struct stat sb;
1090
Jens Axboe66159822007-04-16 21:54:24 +02001091 if (!strcmp(f->file_name, "-"))
1092 f->filetype = FIO_TYPE_PIPE;
1093 else
1094 f->filetype = FIO_TYPE_FILE;
Jens Axboeaf52b342007-03-13 10:07:47 +01001095
Bruce Cran38921822012-02-22 17:55:16 +00001096 /* \\.\ is the device namespace in Windows, where every file is
1097 * a block device */
1098 if (strncmp(f->file_name, "\\\\.\\", 4) == 0)
1099 f->filetype = FIO_TYPE_BD;
1100
Jens Axboeb30d3952010-02-06 23:36:26 +01001101 if (!stat(f->file_name, &sb)) {
Bruce Cran38921822012-02-22 17:55:16 +00001102 if (S_ISBLK(sb.st_mode))
Jens Axboeaf52b342007-03-13 10:07:47 +01001103 f->filetype = FIO_TYPE_BD;
1104 else if (S_ISCHR(sb.st_mode))
1105 f->filetype = FIO_TYPE_CHAR;
Jens Axboeb5605e92007-04-16 20:42:33 +02001106 else if (S_ISFIFO(sb.st_mode))
1107 f->filetype = FIO_TYPE_PIPE;
Jens Axboeaf52b342007-03-13 10:07:47 +01001108 }
1109}
1110
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08001111static void set_already_allocated(const char *fname) {
1112 struct file_name *fn;
1113
1114 fn = malloc(sizeof(struct file_name));
1115 fn->filename = strdup(fname);
1116 flist_add_tail(&fn->list, &filename_list);
1117}
1118
1119static int is_already_allocated(const char *fname)
1120{
1121 struct flist_head *entry;
1122 char *filename;
1123
1124 if (!flist_empty(&filename_list))
1125 {
1126 flist_for_each(entry, &filename_list) {
1127 filename = flist_entry(entry, struct file_name, list)->filename;
1128
1129 if (strcmp(filename, fname) == 0)
1130 return 1;
1131 }
1132 }
1133
1134 return 0;
1135}
1136
1137static void free_already_allocated() {
1138 struct flist_head *entry, *tmp;
1139 struct file_name *fn;
1140
1141 if (!flist_empty(&filename_list))
1142 {
1143 flist_for_each_safe(entry, tmp, &filename_list) {
1144 fn = flist_entry(entry, struct file_name, list);
1145 free(fn->filename);
1146 flist_del(&fn->list);
1147 free(fn);
1148 }
1149 }
1150}
1151
Jens Axboe5903e7b2014-02-26 13:42:13 -08001152int add_file(struct thread_data *td, const char *fname, int numjob, int inc)
Jens Axboeaf52b342007-03-13 10:07:47 +01001153{
Jens Axboe7b4e4fe2007-03-13 12:51:40 +01001154 int cur_files = td->files_index;
Jens Axboebd0ee742007-03-23 14:26:23 +01001155 char file_name[PATH_MAX];
Jens Axboeaf52b342007-03-13 10:07:47 +01001156 struct fio_file *f;
Jens Axboebd0ee742007-03-23 14:26:23 +01001157 int len = 0;
Jens Axboeaf52b342007-03-13 10:07:47 +01001158
Jens Axboeee56ad52008-02-01 10:30:20 +01001159 dprint(FD_FILE, "add file %s\n", fname);
1160
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08001161 if (td->o.directory)
1162 len = set_name_idx(file_name, td->o.directory, numjob);
1163
1164 sprintf(file_name + len, "%s", fname);
1165
1166 /* clean cloned siblings using existing files */
1167 if (numjob && is_already_allocated(file_name))
1168 return 0;
1169
Jens Axboef17c4392008-03-01 18:40:46 +01001170 f = smalloc(sizeof(*f));
Jens Axboec48c0be2008-07-31 19:55:02 +02001171 if (!f) {
1172 log_err("fio: smalloc OOM\n");
1173 assert(0);
1174 }
Jens Axboe0b9d69e2009-09-11 22:29:54 +02001175
Jens Axboeaf52b342007-03-13 10:07:47 +01001176 f->fd = -1;
Jens Axboee6c4d732012-12-12 08:16:27 +01001177 f->shadow_fd = -1;
Jens Axboe33c48812013-01-21 09:46:06 -07001178 fio_file_reset(td, f);
Jens Axboebd0ee742007-03-23 14:26:23 +01001179
Jens Axboefc99bc02009-03-04 15:27:42 +01001180 if (td->files_size <= td->files_index) {
Jianpeng Ma1983e322013-01-10 13:19:27 +01001181 unsigned int new_size = td->o.nr_files + 1;
Jens Axboe126d65c2008-03-01 18:04:31 +01001182
Jens Axboefc99bc02009-03-04 15:27:42 +01001183 dprint(FD_FILE, "resize file array to %d files\n", new_size);
1184
1185 td->files = realloc(td->files, new_size * sizeof(f));
Jianpeng Mad537c082013-01-11 08:52:55 +01001186 if (td->files == NULL) {
1187 log_err("fio: realloc OOM\n");
1188 assert(0);
1189 }
Jens Axboed7df1d12013-03-20 19:57:01 -06001190 if (td->o.file_lock_mode != FILE_LOCK_NONE) {
1191 td->file_locks = realloc(td->file_locks, new_size);
1192 if (!td->file_locks) {
1193 log_err("fio: realloc OOM\n");
1194 assert(0);
1195 }
1196 td->file_locks[cur_files] = FILE_LOCK_NONE;
1197 }
Jens Axboefc99bc02009-03-04 15:27:42 +01001198 td->files_size = new_size;
1199 }
Jens Axboe8bb76792009-03-04 15:37:52 +01001200 td->files[cur_files] = f;
Shaohua Li89ac1d42012-06-11 08:56:32 +02001201 f->fileno = cur_files;
Jens Axboe126d65c2008-03-01 18:04:31 +01001202
Jens Axboe07eb79d2007-04-12 13:02:38 +02001203 /*
1204 * init function, io engine may not be loaded yet
1205 */
1206 if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO))
1207 f->real_file_size = -1ULL;
1208
Jens Axboef17c4392008-03-01 18:40:46 +01001209 f->file_name = smalloc_strdup(file_name);
Jens Axboec48c0be2008-07-31 19:55:02 +02001210 if (!f->file_name) {
1211 log_err("fio: smalloc OOM\n");
1212 assert(0);
1213 }
Jens Axboe0b9d69e2009-09-11 22:29:54 +02001214
Jens Axboee3bab462007-03-13 11:22:09 +01001215 get_file_type(f);
Jens Axboeaf52b342007-03-13 10:07:47 +01001216
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001217 switch (td->o.file_lock_mode) {
1218 case FILE_LOCK_NONE:
1219 break;
1220 case FILE_LOCK_READWRITE:
Jens Axboed7df1d12013-03-20 19:57:01 -06001221 f->rwlock = fio_rwlock_init();
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001222 break;
1223 case FILE_LOCK_EXCLUSIVE:
Jens Axboe521da522012-08-02 11:21:36 +02001224 f->lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001225 break;
1226 default:
1227 log_err("fio: unknown lock mode: %d\n", td->o.file_lock_mode);
1228 assert(0);
1229 }
Jens Axboe29c13492008-03-01 19:25:20 +01001230
Jens Axboe7b4e4fe2007-03-13 12:51:40 +01001231 td->files_index++;
Jens Axboe15494412007-03-13 11:52:47 +01001232 if (f->filetype == FIO_TYPE_FILE)
1233 td->nr_normal_files++;
Jens Axboef29b25a2007-07-23 08:56:43 +02001234
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08001235 set_already_allocated(file_name);
1236
Jens Axboebcd27f72014-02-25 14:01:26 -08001237 /*
1238 * For adding files after the fact - if openfiles= isn't
1239 * given as an option, ensure we allow at least one file open
1240 */
1241 if (!td->o.open_files)
1242 td->o.open_files = 1;
1243
Jens Axboe5903e7b2014-02-26 13:42:13 -08001244 if (inc)
1245 td->o.nr_files++;
1246
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001247 dprint(FD_FILE, "file %p \"%s\" added at %d\n", f, f->file_name,
1248 cur_files);
Jens Axboe9efef3c2008-03-06 10:26:25 +01001249
Jens Axboef29b25a2007-07-23 08:56:43 +02001250 return cur_files;
Jens Axboeaf52b342007-03-13 10:07:47 +01001251}
Jens Axboe0ad920e2007-03-13 11:06:45 +01001252
Jens Axboe49ffb4a2010-08-24 15:01:37 +02001253int add_file_exclusive(struct thread_data *td, const char *fname)
1254{
1255 struct fio_file *f;
1256 unsigned int i;
1257
1258 for_each_file(td, f, i) {
1259 if (!strcmp(f->file_name, fname))
1260 return i;
1261 }
1262
Jens Axboe5903e7b2014-02-26 13:42:13 -08001263 return add_file(td, fname, 0, 1);
Jens Axboe49ffb4a2010-08-24 15:01:37 +02001264}
1265
Jens Axboe0ad920e2007-03-13 11:06:45 +01001266void get_file(struct fio_file *f)
1267{
Jens Axboe8172fe92008-02-01 21:51:02 +01001268 dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references);
Jens Axboed6aed792009-06-03 08:41:15 +02001269 assert(fio_file_open(f));
Jens Axboe0ad920e2007-03-13 11:06:45 +01001270 f->references++;
1271}
1272
Jens Axboe6977bcd2008-03-01 15:55:36 +01001273int put_file(struct thread_data *td, struct fio_file *f)
Jens Axboe0ad920e2007-03-13 11:06:45 +01001274{
Jens Axboe98e1ac42008-03-06 11:56:48 +01001275 int f_ret = 0, ret = 0;
Jens Axboe6977bcd2008-03-01 15:55:36 +01001276
Jens Axboe8172fe92008-02-01 21:51:02 +01001277 dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references);
Jens Axboeee56ad52008-02-01 10:30:20 +01001278
Jens Axboe22a57ba2009-06-09 14:34:35 +02001279 if (!fio_file_open(f)) {
1280 assert(f->fd == -1);
Jens Axboe6977bcd2008-03-01 15:55:36 +01001281 return 0;
Jens Axboe22a57ba2009-06-09 14:34:35 +02001282 }
Jens Axboe0ad920e2007-03-13 11:06:45 +01001283
1284 assert(f->references);
1285 if (--f->references)
Jens Axboe6977bcd2008-03-01 15:55:36 +01001286 return 0;
Jens Axboe0ad920e2007-03-13 11:06:45 +01001287
Jens Axboed424d4d2007-04-17 09:05:10 +02001288 if (should_fsync(td) && td->o.fsync_on_close)
Jens Axboe98e1ac42008-03-06 11:56:48 +01001289 f_ret = fsync(f->fd);
Jens Axboeebb14152007-03-13 14:42:15 +01001290
Jens Axboe0ad920e2007-03-13 11:06:45 +01001291 if (td->io_ops->close_file)
Jens Axboe6977bcd2008-03-01 15:55:36 +01001292 ret = td->io_ops->close_file(td, f);
Jens Axboe1020a132007-03-29 11:15:06 +02001293
Jens Axboe98e1ac42008-03-06 11:56:48 +01001294 if (!ret)
Jens Axboea5fb4612008-05-28 10:54:01 +02001295 ret = f_ret;
Jens Axboe98e1ac42008-03-06 11:56:48 +01001296
Jens Axboe0ad920e2007-03-13 11:06:45 +01001297 td->nr_open_files--;
Jens Axboed6aed792009-06-03 08:41:15 +02001298 fio_file_clear_open(f);
Jens Axboe22a57ba2009-06-09 14:34:35 +02001299 assert(f->fd == -1);
Jens Axboe6977bcd2008-03-01 15:55:36 +01001300 return ret;
Jens Axboe0ad920e2007-03-13 11:06:45 +01001301}
Jens Axboebbf6b542007-03-13 15:28:55 +01001302
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001303void lock_file(struct thread_data *td, struct fio_file *f, enum fio_ddir ddir)
Jens Axboeb2bd2bd2008-03-01 18:19:52 +01001304{
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001305 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1306 return;
Jens Axboe29c13492008-03-01 19:25:20 +01001307
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001308 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
1309 if (ddir == DDIR_READ)
Jens Axboed7df1d12013-03-20 19:57:01 -06001310 fio_rwlock_read(f->rwlock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001311 else
Jens Axboed7df1d12013-03-20 19:57:01 -06001312 fio_rwlock_write(f->rwlock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001313 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1314 fio_mutex_down(f->lock);
1315
Jens Axboed7df1d12013-03-20 19:57:01 -06001316 td->file_locks[f->fileno] = td->o.file_lock_mode;
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001317}
1318
1319void unlock_file(struct thread_data *td, struct fio_file *f)
1320{
1321 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1322 return;
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001323
Jens Axboed7df1d12013-03-20 19:57:01 -06001324 if (td->o.file_lock_mode == FILE_LOCK_READWRITE)
1325 fio_rwlock_unlock(f->rwlock);
1326 else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001327 fio_mutex_up(f->lock);
Jens Axboed7df1d12013-03-20 19:57:01 -06001328
1329 td->file_locks[f->fileno] = FILE_LOCK_NONE;
Jens Axboeb2bd2bd2008-03-01 18:19:52 +01001330}
1331
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001332void unlock_file_all(struct thread_data *td, struct fio_file *f)
Jens Axboeb2bd2bd2008-03-01 18:19:52 +01001333{
Jens Axboef2a28032014-02-11 09:12:06 -07001334 if (td->o.file_lock_mode == FILE_LOCK_NONE || !td->file_locks)
Jens Axboe27ddbfa2014-01-22 16:27:32 -08001335 return;
Jens Axboed7df1d12013-03-20 19:57:01 -06001336 if (td->file_locks[f->fileno] != FILE_LOCK_NONE)
1337 unlock_file(td, f);
Jens Axboeb2bd2bd2008-03-01 18:19:52 +01001338}
1339
Jens Axboebbf6b542007-03-13 15:28:55 +01001340static int recurse_dir(struct thread_data *td, const char *dirname)
1341{
1342 struct dirent *dir;
1343 int ret = 0;
1344 DIR *D;
1345
1346 D = opendir(dirname);
1347 if (!D) {
Jens Axboe0ddb2702007-03-27 19:43:53 +02001348 char buf[FIO_VERROR_SIZE];
1349
Ken Raeburn98ffb8f2013-01-30 22:31:09 +01001350 snprintf(buf, FIO_VERROR_SIZE, "opendir(%s)", dirname);
Jens Axboe0ddb2702007-03-27 19:43:53 +02001351 td_verror(td, errno, buf);
Jens Axboebbf6b542007-03-13 15:28:55 +01001352 return 1;
1353 }
1354
1355 while ((dir = readdir(D)) != NULL) {
1356 char full_path[PATH_MAX];
1357 struct stat sb;
1358
Jens Axboee85b2b82007-03-14 09:39:06 +01001359 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
1360 continue;
Jens Axboe96d32d52007-03-14 09:16:09 +01001361
Bruce Cranb9fd7882012-02-20 17:01:46 +00001362 sprintf(full_path, "%s%s%s", dirname, FIO_OS_PATH_SEPARATOR, dir->d_name);
Jens Axboebbf6b542007-03-13 15:28:55 +01001363
1364 if (lstat(full_path, &sb) == -1) {
1365 if (errno != ENOENT) {
1366 td_verror(td, errno, "stat");
1367 return 1;
1368 }
1369 }
1370
1371 if (S_ISREG(sb.st_mode)) {
Jens Axboe5903e7b2014-02-26 13:42:13 -08001372 add_file(td, full_path, 0, 1);
Jens Axboebbf6b542007-03-13 15:28:55 +01001373 continue;
1374 }
Jens Axboe0ddb2702007-03-27 19:43:53 +02001375 if (!S_ISDIR(sb.st_mode))
1376 continue;
Jens Axboebbf6b542007-03-13 15:28:55 +01001377
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001378 ret = recurse_dir(td, full_path);
1379 if (ret)
Jens Axboebbf6b542007-03-13 15:28:55 +01001380 break;
1381 }
1382
1383 closedir(D);
1384 return ret;
1385}
1386
1387int add_dir_files(struct thread_data *td, const char *path)
1388{
Jens Axboe0ddb2702007-03-27 19:43:53 +02001389 int ret = recurse_dir(td, path);
1390
1391 if (!ret)
1392 log_info("fio: opendir added %d files\n", td->o.nr_files);
1393
1394 return ret;
Jens Axboebbf6b542007-03-13 15:28:55 +01001395}
Jens Axboecade3ef2007-03-23 15:29:45 +01001396
1397void dup_files(struct thread_data *td, struct thread_data *org)
1398{
1399 struct fio_file *f;
1400 unsigned int i;
Jens Axboe9efef3c2008-03-06 10:26:25 +01001401
1402 dprint(FD_FILE, "dup files: %d\n", org->files_index);
Jens Axboecade3ef2007-03-23 15:29:45 +01001403
1404 if (!org->files)
1405 return;
1406
Jens Axboe9efef3c2008-03-06 10:26:25 +01001407 td->files = malloc(org->files_index * sizeof(f));
Jens Axboecade3ef2007-03-23 15:29:45 +01001408
Jens Axboed7df1d12013-03-20 19:57:01 -06001409 if (td->o.file_lock_mode != FILE_LOCK_NONE)
1410 td->file_locks = malloc(org->files_index);
1411
Jens Axboe9efef3c2008-03-06 10:26:25 +01001412 for_each_file(org, f, i) {
Jens Axboeb0fe4212008-03-01 18:22:27 +01001413 struct fio_file *__f;
1414
Jens Axboef17c4392008-03-01 18:40:46 +01001415 __f = smalloc(sizeof(*__f));
Jens Axboec48c0be2008-07-31 19:55:02 +02001416 if (!__f) {
1417 log_err("fio: smalloc OOM\n");
1418 assert(0);
1419 }
Jens Axboe22a57ba2009-06-09 14:34:35 +02001420 __f->fd = -1;
Jens Axboe33c48812013-01-21 09:46:06 -07001421 fio_file_reset(td, __f);
Jens Axboe0b9d69e2009-09-11 22:29:54 +02001422
Aaron Carrollbc3456f2008-06-25 09:20:37 +02001423 if (f->file_name) {
Jens Axboef17c4392008-03-01 18:40:46 +01001424 __f->file_name = smalloc_strdup(f->file_name);
Jens Axboec48c0be2008-07-31 19:55:02 +02001425 if (!__f->file_name) {
1426 log_err("fio: smalloc OOM\n");
1427 assert(0);
1428 }
Jens Axboe0b9d69e2009-09-11 22:29:54 +02001429
Aaron Carrollbc3456f2008-06-25 09:20:37 +02001430 __f->filetype = f->filetype;
1431 }
Jens Axboeb0fe4212008-03-01 18:22:27 +01001432
Jens Axboe67ad9242014-02-07 10:56:15 -07001433 if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1434 __f->lock = f->lock;
1435 else if (td->o.file_lock_mode == FILE_LOCK_READWRITE)
1436 __f->rwlock = f->rwlock;
1437
Jens Axboeb0fe4212008-03-01 18:22:27 +01001438 td->files[i] = __f;
Jens Axboecade3ef2007-03-23 15:29:45 +01001439 }
1440}
Jens Axboef29b25a2007-07-23 08:56:43 +02001441
1442/*
1443 * Returns the index that matches the filename, or -1 if not there
1444 */
1445int get_fileno(struct thread_data *td, const char *fname)
1446{
1447 struct fio_file *f;
1448 unsigned int i;
1449
1450 for_each_file(td, f, i)
1451 if (!strcmp(f->file_name, fname))
1452 return i;
1453
1454 return -1;
1455}
1456
1457/*
1458 * For log usage, where we add/open/close files automatically
1459 */
1460void free_release_files(struct thread_data *td)
1461{
1462 close_files(td);
1463 td->files_index = 0;
1464 td->nr_normal_files = 0;
1465}
Jens Axboe33c48812013-01-21 09:46:06 -07001466
1467void fio_file_reset(struct thread_data *td, struct fio_file *f)
1468{
1469 f->last_pos = f->file_offset;
1470 f->last_start = -1ULL;
1471 if (f->io_axmap)
1472 axmap_reset(f->io_axmap);
1473 if (td->o.random_generator == FIO_RAND_GEN_LFSR)
1474 lfsr_reset(&f->lfsr, td->rand_seeds[FIO_RAND_BLOCK_OFF]);
1475}
Jens Axboe002fe732014-02-11 08:31:13 -07001476
1477int fio_files_done(struct thread_data *td)
1478{
1479 struct fio_file *f;
1480 unsigned int i;
1481
1482 for_each_file(td, f, i)
1483 if (!fio_file_done(f))
1484 return 0;
1485
1486 return 1;
1487}
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08001488
1489/* free memory used in initialization phase only */
1490void filesetup_mem_free() {
1491 free_already_allocated();
1492}