blob: fa09219f62a2e2b93f0e001b2e7254da72e04e99 [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 Axboe1417dae2014-03-20 09:33:13 -060053 if (td_read(td) ||
54 (td_write(td) && td->o.overwrite && !td->o.file_append) ||
Jens Axboeffdfabd2008-03-26 09:18:14 +010055 (td_write(td) && td->io_ops->flags & FIO_NOEXTEND))
Jens Axboe507a7022007-03-27 15:52:46 +020056 new_layout = 1;
Jens Axboe1417dae2014-03-20 09:33:13 -060057 if (td_write(td) && !td->o.overwrite && !td->o.file_append)
Jens Axboeea443652007-03-29 14:52:13 +020058 unlink_file = 1;
Jens Axboe507a7022007-03-27 15:52:46 +020059
Jens Axboe6ae1f572008-02-21 10:20:00 +010060 if (unlink_file || new_layout) {
Jens Axboebd199f22008-03-26 09:57:18 +010061 dprint(FD_FILE, "layout unlink %s\n", f->file_name);
Zhang, Yanmin982016d2008-02-26 15:35:52 +010062 if ((unlink(f->file_name) < 0) && (errno != ENOENT)) {
Jens Axboe7bb48f82007-03-27 15:30:28 +020063 td_verror(td, errno, "unlink");
64 return 1;
65 }
66 }
67
Jens Axboe507a7022007-03-27 15:52:46 +020068 flags = O_WRONLY | O_CREAT;
69 if (new_layout)
70 flags |= O_TRUNC;
71
Jens Axboeee56ad52008-02-01 10:30:20 +010072 dprint(FD_FILE, "open file %s, flags %x\n", f->file_name, flags);
Jens Axboe507a7022007-03-27 15:52:46 +020073 f->fd = open(f->file_name, flags, 0644);
Jens Axboe53cdc682006-10-18 11:50:58 +020074 if (f->fd < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +010075 td_verror(td, errno, "open");
Jens Axboe53cdc682006-10-18 11:50:58 +020076 return 1;
77 }
78
Jens Axboe97ac9922013-01-24 15:00:25 -070079#ifdef CONFIG_POSIX_FALLOCATE
Eric Gourioua596f042011-06-17 09:11:45 +020080 if (!td->o.fill_device) {
81 switch (td->o.fallocate_mode) {
82 case FIO_FALLOCATE_NONE:
83 break;
84 case FIO_FALLOCATE_POSIX:
85 dprint(FD_FILE, "posix_fallocate file %s size %llu\n",
Jens Axboe4b91ee82013-02-25 10:18:33 +010086 f->file_name,
87 (unsigned long long) f->real_file_size);
Jens Axboe7bc8c2c2010-01-28 11:31:31 +010088
Eric Gourioua596f042011-06-17 09:11:45 +020089 r = posix_fallocate(f->fd, 0, f->real_file_size);
90 if (r > 0) {
91 log_err("fio: posix_fallocate fails: %s\n",
92 strerror(r));
93 }
94 break;
Jens Axboe97ac9922013-01-24 15:00:25 -070095#ifdef CONFIG_LINUX_FALLOCATE
Eric Gourioua596f042011-06-17 09:11:45 +020096 case FIO_FALLOCATE_KEEP_SIZE:
97 dprint(FD_FILE,
98 "fallocate(FALLOC_FL_KEEP_SIZE) "
Jens Axboe4b91ee82013-02-25 10:18:33 +010099 "file %s size %llu\n", f->file_name,
100 (unsigned long long) f->real_file_size);
Eric Gourioua596f042011-06-17 09:11:45 +0200101
102 r = fallocate(f->fd, FALLOC_FL_KEEP_SIZE, 0,
103 f->real_file_size);
Jens Axboe888677a2013-04-10 22:19:46 +0200104 if (r != 0)
Eric Gourioua596f042011-06-17 09:11:45 +0200105 td_verror(td, errno, "fallocate");
Jens Axboe888677a2013-04-10 22:19:46 +0200106
Eric Gourioua596f042011-06-17 09:11:45 +0200107 break;
Jens Axboe97ac9922013-01-24 15:00:25 -0700108#endif /* CONFIG_LINUX_FALLOCATE */
Eric Gourioua596f042011-06-17 09:11:45 +0200109 default:
110 log_err("fio: unknown fallocate mode: %d\n",
111 td->o.fallocate_mode);
112 assert(0);
Jens Axboe7bc8c2c2010-01-28 11:31:31 +0100113 }
114 }
Jens Axboe97ac9922013-01-24 15:00:25 -0700115#endif /* CONFIG_POSIX_FALLOCATE */
Bruce Cran9b836562011-01-08 19:49:54 +0100116
Shawn Lewisfc74ac12008-01-11 09:45:11 +0100117 if (!new_layout)
118 goto done;
119
Jens Axboe5e0074c2009-06-02 21:40:09 +0200120 /*
121 * The size will be -1ULL when fill_device is used, so don't truncate
122 * or fallocate this file, just write it
123 */
124 if (!td->o.fill_device) {
125 dprint(FD_FILE, "truncate file %s, size %llu\n", f->file_name,
Jens Axboe4b91ee82013-02-25 10:18:33 +0100126 (unsigned long long) f->real_file_size);
Jens Axboe5e0074c2009-06-02 21:40:09 +0200127 if (ftruncate(f->fd, f->real_file_size) == -1) {
John3cd4c662013-12-29 19:20:35 -0700128 if (errno != EFBIG) {
129 td_verror(td, errno, "ftruncate");
130 goto err;
131 }
Jens Axboe5e0074c2009-06-02 21:40:09 +0200132 }
Jens Axboe5e0074c2009-06-02 21:40:09 +0200133 }
Jens Axboe40f82982006-10-25 11:21:05 +0200134
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100135 b = malloc(td->o.max_bs[DDIR_WRITE]);
Jens Axboe53cdc682006-10-18 11:50:58 +0200136
Jens Axboe7bb48f82007-03-27 15:30:28 +0200137 left = f->real_file_size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200138 while (left && !td->terminate) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100139 bs = td->o.max_bs[DDIR_WRITE];
Jens Axboe53cdc682006-10-18 11:50:58 +0200140 if (bs > left)
141 bs = left;
142
Jens Axboecc86c392013-05-03 15:12:33 +0200143 fill_io_buffer(td, b, bs, bs);
144
Jens Axboe53cdc682006-10-18 11:50:58 +0200145 r = write(f->fd, b, bs);
146
Jens Axboe5e0074c2009-06-02 21:40:09 +0200147 if (r > 0) {
148 left -= r;
Jens Axboe53cdc682006-10-18 11:50:58 +0200149 continue;
150 } else {
Jens Axboe5e0074c2009-06-02 21:40:09 +0200151 if (r < 0) {
152 int __e = errno;
153
154 if (__e == ENOSPC) {
155 if (td->o.fill_device)
156 break;
157 log_info("fio: ENOSPC on laying out "
158 "file, stopping\n");
159 break;
160 }
Jens Axboee1161c32007-02-22 19:36:48 +0100161 td_verror(td, errno, "write");
Jens Axboe5e0074c2009-06-02 21:40:09 +0200162 } else
Jens Axboee1161c32007-02-22 19:36:48 +0100163 td_verror(td, EIO, "write");
Jens Axboe53cdc682006-10-18 11:50:58 +0200164
165 break;
166 }
167 }
168
Jens Axboeffdfabd2008-03-26 09:18:14 +0100169 if (td->terminate) {
170 dprint(FD_FILE, "terminate unlink %s\n", f->file_name);
Jens Axboe53cdc682006-10-18 11:50:58 +0200171 unlink(f->file_name);
Jens Axboeffdfabd2008-03-26 09:18:14 +0100172 } else if (td->o.create_fsync) {
Jens Axboe98e1ac42008-03-06 11:56:48 +0100173 if (fsync(f->fd) < 0) {
174 td_verror(td, errno, "fsync");
175 goto err;
176 }
177 }
Jens Axboe0d1cd202009-06-05 22:11:52 +0200178 if (td->o.fill_device && !td_write(td)) {
Jens Axboed6aed792009-06-03 08:41:15 +0200179 fio_file_clear_size_known(f);
Jens Axboe5e0074c2009-06-02 21:40:09 +0200180 if (td_io_get_file_size(td, f))
181 goto err;
182 if (f->io_size > f->real_file_size)
183 f->io_size = f->real_file_size;
184 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200185
186 free(b);
Jens Axboe507a7022007-03-27 15:52:46 +0200187done:
Jens Axboe53cdc682006-10-18 11:50:58 +0200188 return 0;
189err:
190 close(f->fd);
191 f->fd = -1;
192 return 1;
193}
194
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200195static int pre_read_file(struct thread_data *td, struct fio_file *f)
196{
Jens Axboeef799a62014-04-01 17:01:30 -0600197 int ret = 0, r, did_open = 0, old_runstate;
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200198 unsigned long long left;
199 unsigned int bs;
200 char *b;
201
Jens Axboe9c0d2242009-07-01 12:26:28 +0200202 if (td->io_ops->flags & FIO_PIPEIO)
203 return 0;
204
Jens Axboed6aed792009-06-03 08:41:15 +0200205 if (!fio_file_open(f)) {
Jens Axboeb0f65862009-05-20 11:52:15 +0200206 if (td->io_ops->open_file(td, f)) {
207 log_err("fio: cannot pre-read, failed to open file\n");
208 return 1;
209 }
210 did_open = 1;
211 }
212
Jens Axboe8edd9732014-03-01 08:24:03 -0700213 old_runstate = td_bump_runstate(td, TD_PRE_READING);
Jens Axboeb0f65862009-05-20 11:52:15 +0200214
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200215 bs = td->o.max_bs[DDIR_READ];
216 b = malloc(bs);
217 memset(b, 0, bs);
218
Jens Axboeef799a62014-04-01 17:01:30 -0600219 if (lseek(f->fd, f->file_offset, SEEK_SET) < 0) {
220 td_verror(td, errno, "lseek");
221 log_err("fio: failed to lseek pre-read file\n");
222 ret = 1;
223 goto error;
224 }
225
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200226 left = f->io_size;
227
228 while (left && !td->terminate) {
229 if (bs > left)
230 bs = left;
231
232 r = read(f->fd, b, bs);
233
234 if (r == (int) bs) {
235 left -= bs;
236 continue;
237 } else {
238 td_verror(td, EIO, "pre_read");
239 break;
240 }
241 }
242
Jens Axboeef799a62014-04-01 17:01:30 -0600243error:
Jens Axboe8edd9732014-03-01 08:24:03 -0700244 td_restore_runstate(td, old_runstate);
Jens Axboeb0f65862009-05-20 11:52:15 +0200245
246 if (did_open)
247 td->io_ops->close_file(td, f);
Jens Axboeef799a62014-04-01 17:01:30 -0600248
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200249 free(b);
Jens Axboeef799a62014-04-01 17:01:30 -0600250 return ret;
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200251}
252
Jens Axboe7bb48f82007-03-27 15:30:28 +0200253static unsigned long long get_rand_file_size(struct thread_data *td)
Jens Axboe9c60ce62007-03-15 09:14:47 +0100254{
Jens Axboedc873b62008-06-04 20:13:04 +0200255 unsigned long long ret, sized;
Jens Axboe1294c3e2011-05-11 08:15:18 +0200256 unsigned long r;
Jens Axboe9c60ce62007-03-15 09:14:47 +0100257
Jens Axboe4c07ad82011-03-28 09:51:09 +0200258 if (td->o.use_os_rand) {
259 r = os_random_long(&td->file_size_state);
260 sized = td->o.file_size_high - td->o.file_size_low;
261 ret = (unsigned long long) ((double) sized * (r / (OS_RAND_MAX + 1.0)));
262 } else {
263 r = __rand(&td->__file_size_state);
264 sized = td->o.file_size_high - td->o.file_size_low;
265 ret = (unsigned long long) ((double) sized * (r / (FRAND_MAX + 1.0)));
266 }
267
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100268 ret += td->o.file_size_low;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100269 ret -= (ret % td->o.rw_min_bs);
Jens Axboe9c60ce62007-03-15 09:14:47 +0100270 return ret;
271}
272
Jens Axboe53cdc682006-10-18 11:50:58 +0200273static int file_size(struct thread_data *td, struct fio_file *f)
274{
275 struct stat st;
276
Jens Axboedf9c26b2009-03-05 10:13:58 +0100277 if (stat(f->file_name, &st) == -1) {
Jens Axboe7bb48f82007-03-27 15:30:28 +0200278 td_verror(td, errno, "fstat");
279 return 1;
280 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200281
Jens Axboe7bb48f82007-03-27 15:30:28 +0200282 f->real_file_size = st.st_size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200283 return 0;
284}
285
286static int bdev_size(struct thread_data *td, struct fio_file *f)
287{
Bruce Cran9b836562011-01-08 19:49:54 +0100288 unsigned long long bytes = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200289 int r;
290
Jens Axboedf9c26b2009-03-05 10:13:58 +0100291 if (td->io_ops->open_file(td, f)) {
292 log_err("fio: failed opening blockdev %s for size check\n",
293 f->file_name);
294 return 1;
295 }
296
Bruce Cranecc314b2011-01-04 10:59:30 +0100297 r = blockdev_size(f, &bytes);
Jens Axboe53cdc682006-10-18 11:50:58 +0200298 if (r) {
Jens Axboee1161c32007-02-22 19:36:48 +0100299 td_verror(td, r, "blockdev_size");
Jens Axboedf9c26b2009-03-05 10:13:58 +0100300 goto err;
Jens Axboe53cdc682006-10-18 11:50:58 +0200301 }
302
Jens Axboe7ab077a2009-02-02 15:07:37 +0100303 if (!bytes) {
304 log_err("%s: zero sized block device?\n", f->file_name);
Jens Axboedf9c26b2009-03-05 10:13:58 +0100305 goto err;
Jens Axboe7ab077a2009-02-02 15:07:37 +0100306 }
307
Jens Axboe53cdc682006-10-18 11:50:58 +0200308 f->real_file_size = bytes;
Jens Axboe22a57ba2009-06-09 14:34:35 +0200309 td->io_ops->close_file(td, f);
Jens Axboe53cdc682006-10-18 11:50:58 +0200310 return 0;
Jens Axboedf9c26b2009-03-05 10:13:58 +0100311err:
312 td->io_ops->close_file(td, f);
313 return 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200314}
315
Jens Axboe4ccdccd2010-06-24 10:27:22 +0200316static int char_size(struct thread_data *td, struct fio_file *f)
317{
318#ifdef FIO_HAVE_CHARDEV_SIZE
Bruce Cran9b836562011-01-08 19:49:54 +0100319 unsigned long long bytes = 0;
Jens Axboe4ccdccd2010-06-24 10:27:22 +0200320 int r;
321
322 if (td->io_ops->open_file(td, f)) {
323 log_err("fio: failed opening blockdev %s for size check\n",
324 f->file_name);
325 return 1;
326 }
327
Bruce Cranecc314b2011-01-04 10:59:30 +0100328 r = chardev_size(f, &bytes);
Jens Axboe4ccdccd2010-06-24 10:27:22 +0200329 if (r) {
330 td_verror(td, r, "chardev_size");
331 goto err;
332 }
333
334 if (!bytes) {
335 log_err("%s: zero sized char device?\n", f->file_name);
336 goto err;
337 }
338
339 f->real_file_size = bytes;
340 td->io_ops->close_file(td, f);
341 return 0;
342err:
343 td->io_ops->close_file(td, f);
344 return 1;
345#else
346 f->real_file_size = -1ULL;
347 return 0;
348#endif
349}
350
Jens Axboe53cdc682006-10-18 11:50:58 +0200351static int get_file_size(struct thread_data *td, struct fio_file *f)
352{
353 int ret = 0;
354
Jens Axboed6aed792009-06-03 08:41:15 +0200355 if (fio_file_size_known(f))
Jens Axboe409b3412007-03-28 09:57:01 +0200356 return 0;
357
Jens Axboe7bb48f82007-03-27 15:30:28 +0200358 if (f->filetype == FIO_TYPE_FILE)
359 ret = file_size(td, f);
360 else if (f->filetype == FIO_TYPE_BD)
Jens Axboe53cdc682006-10-18 11:50:58 +0200361 ret = bdev_size(td, f);
Jens Axboe4ccdccd2010-06-24 10:27:22 +0200362 else if (f->filetype == FIO_TYPE_CHAR)
363 ret = char_size(td, f);
Jens Axboe53cdc682006-10-18 11:50:58 +0200364 else
365 f->real_file_size = -1;
366
367 if (ret)
368 return ret;
369
370 if (f->file_offset > f->real_file_size) {
Bruce Cran0f2152c2010-12-15 10:33:03 +0100371 log_err("%s: offset extends end (%llu > %llu)\n", td->o.name,
Jens Axboe4e0a8fa2013-04-15 11:40:57 +0200372 (unsigned long long) f->file_offset,
373 (unsigned long long) f->real_file_size);
Jens Axboe53cdc682006-10-18 11:50:58 +0200374 return 1;
375 }
376
Jens Axboed6aed792009-06-03 08:41:15 +0200377 fio_file_set_size_known(f);
Jens Axboe53cdc682006-10-18 11:50:58 +0200378 return 0;
379}
380
Jens Axboe3baddf22008-04-04 11:10:30 +0200381static int __file_invalidate_cache(struct thread_data *td, struct fio_file *f,
382 unsigned long long off,
383 unsigned long long len)
Jens Axboee5b401d2006-10-18 16:03:40 +0200384{
385 int ret = 0;
386
Jens Axboe5e0074c2009-06-02 21:40:09 +0200387 if (len == -1ULL)
Jens Axboe3baddf22008-04-04 11:10:30 +0200388 len = f->io_size;
389 if (off == -1ULL)
390 off = f->file_offset;
Jens Axboeee56ad52008-02-01 10:30:20 +0100391
Jens Axboe0d1cd202009-06-05 22:11:52 +0200392 if (len == -1ULL || off == -1ULL)
393 return 0;
394
Jens Axboe3baddf22008-04-04 11:10:30 +0200395 dprint(FD_IO, "invalidate cache %s: %llu/%llu\n", f->file_name, off,
396 len);
Jens Axboeb5af8292007-03-08 12:43:13 +0100397
Jens Axboee5b401d2006-10-18 16:03:40 +0200398 /*
399 * FIXME: add blockdev flushing too
400 */
Jens Axboea1c58072009-08-04 23:17:02 +0200401 if (f->mmap_ptr) {
Bruce Cran03e20d62011-01-02 20:14:54 +0100402 ret = posix_madvise(f->mmap_ptr, f->mmap_sz, POSIX_MADV_DONTNEED);
Jens Axboea1c58072009-08-04 23:17:02 +0200403#ifdef FIO_MADV_FREE
Jens Axboe3e10fb82013-08-09 14:31:06 -0600404 if (f->filetype == FIO_TYPE_BD)
405 (void) posix_madvise(f->mmap_ptr, f->mmap_sz, FIO_MADV_FREE);
Jens Axboea1c58072009-08-04 23:17:02 +0200406#endif
407 } else if (f->filetype == FIO_TYPE_FILE) {
Bruce Cranecc314b2011-01-04 10:59:30 +0100408 ret = posix_fadvise(f->fd, off, len, POSIX_FADV_DONTNEED);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100409 } else if (f->filetype == FIO_TYPE_BD) {
Bruce Cranecc314b2011-01-04 10:59:30 +0100410 ret = blockdev_invalidate_cache(f);
Jens Axboe7e0e25c2007-03-27 12:49:56 +0200411 if (ret < 0 && errno == EACCES && geteuid()) {
Jens Axboe7172cfe2007-07-23 14:36:00 +0200412 if (!root_warn) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100413 log_err("fio: only root may flush block "
414 "devices. Cache flush bypassed!\n");
Jens Axboe7172cfe2007-07-23 14:36:00 +0200415 root_warn = 1;
416 }
Jens Axboe7e0e25c2007-03-27 12:49:56 +0200417 ret = 0;
418 }
Jens Axboeb5605e92007-04-16 20:42:33 +0200419 } else if (f->filetype == FIO_TYPE_CHAR || f->filetype == FIO_TYPE_PIPE)
Jens Axboee5b401d2006-10-18 16:03:40 +0200420 ret = 0;
421
422 if (ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100423 td_verror(td, errno, "invalidate_cache");
Jens Axboee5b401d2006-10-18 16:03:40 +0200424 return 1;
Jens Axboe3baddf22008-04-04 11:10:30 +0200425 } else if (ret > 0) {
426 td_verror(td, ret, "invalidate_cache");
427 return 1;
Jens Axboee5b401d2006-10-18 16:03:40 +0200428 }
429
Jens Axboead2da602007-02-26 12:33:27 +0100430 return ret;
Jens Axboe3baddf22008-04-04 11:10:30 +0200431
432}
433
434int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
435{
Jens Axboed6aed792009-06-03 08:41:15 +0200436 if (!fio_file_open(f))
Jens Axboea5fb4612008-05-28 10:54:01 +0200437 return 0;
438
Jens Axboe5e0074c2009-06-02 21:40:09 +0200439 return __file_invalidate_cache(td, f, -1ULL, -1ULL);
Jens Axboee5b401d2006-10-18 16:03:40 +0200440}
441
Jens Axboe6977bcd2008-03-01 15:55:36 +0100442int generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
Jens Axboe53cdc682006-10-18 11:50:58 +0200443{
Jens Axboe6977bcd2008-03-01 15:55:36 +0100444 int ret = 0;
445
Jens Axboeee56ad52008-02-01 10:30:20 +0100446 dprint(FD_FILE, "fd close %s\n", f->file_name);
Jens Axboe4906e0b2008-03-01 18:59:51 +0100447
448 remove_file_hash(f);
449
Jens Axboe6977bcd2008-03-01 15:55:36 +0100450 if (close(f->fd) < 0)
451 ret = errno;
452
Jens Axboeb5af8292007-03-08 12:43:13 +0100453 f->fd = -1;
Jens Axboee6c4d732012-12-12 08:16:27 +0100454
455 if (f->shadow_fd != -1) {
456 close(f->shadow_fd);
457 f->shadow_fd = -1;
458 }
459
Juan Casse57e54e02013-09-20 09:20:52 -0600460 f->engine_data = 0;
Jens Axboe6977bcd2008-03-01 15:55:36 +0100461 return ret;
Jens Axboe53cdc682006-10-18 11:50:58 +0200462}
463
Dmitry Monakhov1ccc6dc2012-09-19 23:22:53 +0400464int file_lookup_open(struct fio_file *f, int flags)
Jens Axboe53cdc682006-10-18 11:50:58 +0200465{
Jens Axboe29c13492008-03-01 19:25:20 +0100466 struct fio_file *__f;
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100467 int from_hash;
468
469 __f = lookup_file_hash(f->file_name);
470 if (__f) {
Jens Axboe9efef3c2008-03-06 10:26:25 +0100471 dprint(FD_FILE, "found file in hash %s\n", f->file_name);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100472 /*
473 * racy, need the __f->lock locked
474 */
475 f->lock = __f->lock;
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100476 from_hash = 1;
477 } else {
Jens Axboe9efef3c2008-03-06 10:26:25 +0100478 dprint(FD_FILE, "file not found in hash %s\n", f->file_name);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100479 from_hash = 0;
480 }
481
Jens Axboee8670ef2008-03-06 12:06:30 +0100482 f->fd = open(f->file_name, flags, 0600);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100483 return from_hash;
484}
485
Jens Axboee6c4d732012-12-12 08:16:27 +0100486static int file_close_shadow_fds(struct thread_data *td)
487{
488 struct fio_file *f;
489 int num_closed = 0;
490 unsigned int i;
491
492 for_each_file(td, f, i) {
493 if (f->shadow_fd == -1)
494 continue;
495
496 close(f->shadow_fd);
497 f->shadow_fd = -1;
498 num_closed++;
499 }
500
501 return num_closed;
502}
503
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100504int generic_open_file(struct thread_data *td, struct fio_file *f)
505{
Jens Axboe66159822007-04-16 21:54:24 +0200506 int is_std = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200507 int flags = 0;
Jens Axboe29c13492008-03-01 19:25:20 +0100508 int from_hash = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200509
Jens Axboeee56ad52008-02-01 10:30:20 +0100510 dprint(FD_FILE, "fd open %s\n", f->file_name);
511
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200512 if (td_trim(td) && f->filetype != FIO_TYPE_BD) {
513 log_err("fio: trim only applies to block device\n");
514 return 1;
515 }
516
Jens Axboe66159822007-04-16 21:54:24 +0200517 if (!strcmp(f->file_name, "-")) {
518 if (td_rw(td)) {
519 log_err("fio: can't read/write to stdin/out\n");
520 return 1;
521 }
522 is_std = 1;
Jens Axboece98fa62007-04-17 13:27:32 +0200523
524 /*
525 * move output logging to stderr, if we are writing to stdout
526 */
527 if (td_write(td))
528 f_out = stderr;
Jens Axboe66159822007-04-16 21:54:24 +0200529 }
530
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200531 if (td_trim(td))
532 goto skip_flags;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100533 if (td->o.odirect)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100534 flags |= OS_O_DIRECT;
Chris Masond01612f2013-11-15 15:52:58 -0700535 if (td->o.oatomic) {
536 if (!FIO_O_ATOMIC) {
537 td_verror(td, EINVAL, "OS does not support atomic IO");
538 return 1;
539 }
540 flags |= OS_O_DIRECT | FIO_O_ATOMIC;
541 }
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100542 if (td->o.sync_io)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100543 flags |= O_SYNC;
Jens Axboe814452b2009-03-04 12:53:13 +0100544 if (td->o.create_on_open)
545 flags |= O_CREAT;
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200546skip_flags:
547 if (f->filetype != FIO_TYPE_FILE)
548 flags |= FIO_O_NOATIME;
Jens Axboe7abf8332006-11-23 15:01:19 +0100549
Aaron Carroll056f3452007-11-26 09:08:53 +0100550open_again:
Jens Axboe660a1cb2007-04-17 15:37:47 +0200551 if (td_write(td)) {
Jens Axboe17308152008-03-07 13:42:31 +0100552 if (!read_only)
553 flags |= O_RDWR;
Jens Axboe53cdc682006-10-18 11:50:58 +0200554
Jens Axboeaf52b342007-03-13 10:07:47 +0100555 if (f->filetype == FIO_TYPE_FILE)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100556 flags |= O_CREAT;
Jens Axboe2fd233b2007-03-02 08:44:25 +0100557
Jens Axboe66159822007-04-16 21:54:24 +0200558 if (is_std)
559 f->fd = dup(STDOUT_FILENO);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100560 else
561 from_hash = file_lookup_open(f, flags);
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200562 } else if (td_read(td)) {
Jens Axboe4241ea82007-09-12 08:18:36 +0200563 if (f->filetype == FIO_TYPE_CHAR && !read_only)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100564 flags |= O_RDWR;
565 else
566 flags |= O_RDONLY;
567
Jens Axboe66159822007-04-16 21:54:24 +0200568 if (is_std)
569 f->fd = dup(STDIN_FILENO);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100570 else
571 from_hash = file_lookup_open(f, flags);
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200572 } else { //td trim
573 flags |= O_RDWR;
574 from_hash = file_lookup_open(f, flags);
Jens Axboe53cdc682006-10-18 11:50:58 +0200575 }
576
577 if (f->fd == -1) {
Jens Axboee4e33252007-03-21 13:07:54 +0100578 char buf[FIO_VERROR_SIZE];
Jens Axboee1161c32007-02-22 19:36:48 +0100579 int __e = errno;
580
Jens Axboe835d9b92009-06-09 15:23:38 +0200581 if (__e == EPERM && (flags & FIO_O_NOATIME)) {
Jens Axboe5921e802008-05-30 15:02:38 +0200582 flags &= ~FIO_O_NOATIME;
Aaron Carroll056f3452007-11-26 09:08:53 +0100583 goto open_again;
584 }
Jens Axboee6c4d732012-12-12 08:16:27 +0100585 if (__e == EMFILE && file_close_shadow_fds(td))
586 goto open_again;
Aaron Carroll056f3452007-11-26 09:08:53 +0100587
Ken Raeburn98ffb8f2013-01-30 22:31:09 +0100588 snprintf(buf, sizeof(buf), "open(%s)", f->file_name);
Jens Axboee4e33252007-03-21 13:07:54 +0100589
Jens Axboea93c5f02012-06-11 08:53:53 +0200590 if (__e == EINVAL && (flags & OS_O_DIRECT)) {
591 log_err("fio: looks like your file system does not " \
592 "support direct=1/buffered=0\n");
593 }
594
Jens Axboee4e33252007-03-21 13:07:54 +0100595 td_verror(td, __e, buf);
Jens Axboe53cdc682006-10-18 11:50:58 +0200596 }
597
Jens Axboe29c13492008-03-01 19:25:20 +0100598 if (!from_hash && f->fd != -1) {
599 if (add_file_hash(f)) {
Jens Axboe3f0ca9b2011-05-24 21:36:24 +0200600 int fio_unused ret;
Jens Axboe29c13492008-03-01 19:25:20 +0100601
602 /*
Jens Axboee6c4d732012-12-12 08:16:27 +0100603 * Stash away descriptor for later close. This is to
604 * work-around a "feature" on Linux, where a close of
605 * an fd that has been opened for write will trigger
606 * udev to call blkid to check partitions, fs id, etc.
Anatol Pomozovde8f6de2013-09-26 16:31:34 -0700607 * That pollutes the device cache, which can slow down
Jens Axboee6c4d732012-12-12 08:16:27 +0100608 * unbuffered accesses.
Jens Axboe29c13492008-03-01 19:25:20 +0100609 */
Jens Axboee6c4d732012-12-12 08:16:27 +0100610 if (f->shadow_fd == -1)
611 f->shadow_fd = f->fd;
612 else {
613 /*
614 * OK to ignore, we haven't done anything
615 * with it
616 */
617 ret = generic_close_file(td, f);
618 }
Jens Axboe29c13492008-03-01 19:25:20 +0100619 goto open_again;
620 }
621 }
Jens Axboe4906e0b2008-03-01 18:59:51 +0100622
Jens Axboe53cdc682006-10-18 11:50:58 +0200623 return 0;
Jens Axboeb5af8292007-03-08 12:43:13 +0100624}
625
Jens Axboedf9c26b2009-03-05 10:13:58 +0100626int generic_get_file_size(struct thread_data *td, struct fio_file *f)
Jens Axboe21972cd2006-11-23 12:39:16 +0100627{
Jens Axboedf9c26b2009-03-05 10:13:58 +0100628 return get_file_size(td, f);
Jens Axboe21972cd2006-11-23 12:39:16 +0100629}
630
Jens Axboe7bb48f82007-03-27 15:30:28 +0200631/*
632 * open/close all files, so that ->real_file_size gets set
633 */
Jens Axboebab3fd52007-04-02 10:34:18 +0200634static int get_file_sizes(struct thread_data *td)
Jens Axboe53cdc682006-10-18 11:50:58 +0200635{
636 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100637 unsigned int i;
Jens Axboebab3fd52007-04-02 10:34:18 +0200638 int err = 0;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200639
640 for_each_file(td, f, i) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100641 dprint(FD_FILE, "get file size for %p/%d/%p\n", f, i,
642 f->file_name);
Jens Axboe9efef3c2008-03-06 10:26:25 +0100643
Jens Axboe99a47c62009-04-07 22:20:56 +0200644 if (td_io_get_file_size(td, f)) {
Jens Axboe40b44f42007-04-11 12:43:35 +0200645 if (td->error != ENOENT) {
646 log_err("%s\n", td->verror);
647 err = 1;
648 }
Jens Axboe541d66d2007-03-27 19:50:11 +0200649 clear_error(td);
Jens Axboe07eb79d2007-04-12 13:02:38 +0200650 }
Jens Axboe409b3412007-03-28 09:57:01 +0200651
652 if (f->real_file_size == -1ULL && td->o.size)
653 f->real_file_size = td->o.size / td->o.nr_files;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200654 }
Jens Axboebab3fd52007-04-02 10:34:18 +0200655
656 return err;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200657}
658
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200659struct fio_mount {
660 struct flist_head list;
661 const char *base;
662 char __base[256];
663 unsigned int key;
664};
665
666/*
667 * Get free number of bytes for each file on each unique mount.
668 */
669static unsigned long long get_fs_free_counts(struct thread_data *td)
670{
671 struct flist_head *n, *tmp;
Jens Axboe68b03162010-06-24 09:22:41 +0200672 unsigned long long ret = 0;
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200673 struct fio_mount *fm;
674 FLIST_HEAD(list);
675 struct fio_file *f;
676 unsigned int i;
677
678 for_each_file(td, f, i) {
679 struct stat sb;
680 char buf[256];
681
Jens Axboe4ccdccd2010-06-24 10:27:22 +0200682 if (f->filetype == FIO_TYPE_BD || f->filetype == FIO_TYPE_CHAR) {
683 if (f->real_file_size != -1ULL)
684 ret += f->real_file_size;
Jens Axboe68b03162010-06-24 09:22:41 +0200685 continue;
686 } else if (f->filetype != FIO_TYPE_FILE)
687 continue;
688
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200689 strcpy(buf, f->file_name);
690
691 if (stat(buf, &sb) < 0) {
692 if (errno != ENOENT)
693 break;
694 strcpy(buf, ".");
695 if (stat(buf, &sb) < 0)
696 break;
697 }
698
699 fm = NULL;
700 flist_for_each(n, &list) {
701 fm = flist_entry(n, struct fio_mount, list);
702 if (fm->key == sb.st_dev)
703 break;
704
705 fm = NULL;
706 }
707
708 if (fm)
709 continue;
710
711 fm = malloc(sizeof(*fm));
712 strcpy(fm->__base, buf);
713 fm->base = basename(fm->__base);
714 fm->key = sb.st_dev;
715 flist_add(&fm->list, &list);
716 }
717
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200718 flist_for_each_safe(n, tmp, &list) {
719 unsigned long long sz;
720
721 fm = flist_entry(n, struct fio_mount, list);
722 flist_del(&fm->list);
723
724 sz = get_fs_size(fm->base);
725 if (sz && sz != -1ULL)
726 ret += sz;
727
728 free(fm);
729 }
730
731 return ret;
732}
733
Jens Axboebedc9dc2014-03-17 12:51:09 -0600734uint64_t get_start_offset(struct thread_data *td, struct fio_file *f)
Dan Ehrenbergce95d652012-08-16 08:58:21 +0200735{
Jens Axboebedc9dc2014-03-17 12:51:09 -0600736 struct thread_options *o = &td->o;
737
738 if (o->file_append && f->filetype == FIO_TYPE_FILE)
739 return f->real_file_size;
740
Dan Ehrenbergce95d652012-08-16 08:58:21 +0200741 return td->o.start_offset +
742 (td->thread_number - 1) * td->o.offset_increment;
743}
744
Jens Axboe7bb48f82007-03-27 15:30:28 +0200745/*
746 * Open the files and setup files sizes, creating files if necessary.
747 */
748int setup_files(struct thread_data *td)
749{
750 unsigned long long total_size, extend_size;
Jens Axboede98bd32013-04-05 11:09:20 +0200751 struct thread_options *o = &td->o;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200752 struct fio_file *f;
Jens Axboe002fe732014-02-11 08:31:13 -0700753 unsigned int i, nr_fs_extra = 0;
Jens Axboe000b0802007-03-27 15:56:10 +0200754 int err = 0, need_extend;
Jens Axboee90391e2013-04-13 20:40:53 +0200755 int old_state;
Jens Axboe002fe732014-02-11 08:31:13 -0700756 const unsigned int bs = td_min_bs(td);
757 uint64_t fs = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200758
Jens Axboeee56ad52008-02-01 10:30:20 +0100759 dprint(FD_FILE, "setup files\n");
760
Jens Axboe8edd9732014-03-01 08:24:03 -0700761 old_state = td_bump_runstate(td, TD_SETTING_UP);
Jens Axboee90391e2013-04-13 20:40:53 +0200762
Jens Axboede98bd32013-04-05 11:09:20 +0200763 if (o->read_iolog_file)
Jens Axboe25460cf2012-05-02 13:58:02 +0200764 goto done;
Jens Axboe691c8fb2008-03-07 14:26:26 +0100765
Jens Axboe53cdc682006-10-18 11:50:58 +0200766 /*
767 * if ioengine defines a setup() method, it's responsible for
Jens Axboe7bb48f82007-03-27 15:30:28 +0200768 * opening the files and setting f->real_file_size to indicate
769 * the valid range for that file.
Jens Axboe53cdc682006-10-18 11:50:58 +0200770 */
771 if (td->io_ops->setup)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200772 err = td->io_ops->setup(td);
773 else
Jens Axboebab3fd52007-04-02 10:34:18 +0200774 err = get_file_sizes(td);
Jens Axboe53cdc682006-10-18 11:50:58 +0200775
Jens Axboef1027062006-10-20 10:14:01 +0200776 if (err)
Jens Axboee90391e2013-04-13 20:40:53 +0200777 goto err_out;
Jens Axboef1027062006-10-20 10:14:01 +0200778
Jens Axboe0a7eb122006-10-20 10:36:42 +0200779 /*
Jens Axboe7bb48f82007-03-27 15:30:28 +0200780 * check sizes. if the files/devices do not exist and the size
781 * isn't passed to fio, abort.
Jens Axboe0a7eb122006-10-20 10:36:42 +0200782 */
Jens Axboe7bb48f82007-03-27 15:30:28 +0200783 total_size = 0;
784 for_each_file(td, f, i) {
785 if (f->real_file_size == -1ULL)
786 total_size = -1ULL;
787 else
788 total_size += f->real_file_size;
789 }
Jens Axboe0a7eb122006-10-20 10:36:42 +0200790
Jens Axboede98bd32013-04-05 11:09:20 +0200791 if (o->fill_device)
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200792 td->fill_device_size = get_fs_free_counts(td);
793
Jens Axboe7bb48f82007-03-27 15:30:28 +0200794 /*
795 * device/file sizes are zero and no size given, punt
796 */
Jens Axboede98bd32013-04-05 11:09:20 +0200797 if ((!total_size || total_size == -1ULL) && !o->size &&
798 !(td->io_ops->flags & FIO_NOIO) && !o->fill_device &&
799 !(o->nr_files && (o->file_size_low || o->file_size_high))) {
800 log_err("%s: you need to specify size=\n", o->name);
Jens Axboee1161c32007-02-22 19:36:48 +0100801 td_verror(td, EINVAL, "total_file_size");
Jens Axboee90391e2013-04-13 20:40:53 +0200802 goto err_out;
Jens Axboe53cdc682006-10-18 11:50:58 +0200803 }
804
Jens Axboe7bb48f82007-03-27 15:30:28 +0200805 /*
Jens Axboe002fe732014-02-11 08:31:13 -0700806 * Calculate per-file size and potential extra size for the
807 * first files, if needed.
808 */
809 if (!o->file_size_low) {
810 uint64_t all_fs;
811
812 fs = o->size / o->nr_files;
813 all_fs = fs * o->nr_files;
814
815 if (all_fs < o->size)
816 nr_fs_extra = (o->size - all_fs) / bs;
817 }
818
819 /*
Jens Axboe7bb48f82007-03-27 15:30:28 +0200820 * now file sizes are known, so we can set ->io_size. if size= is
821 * not given, ->io_size is just equal to ->real_file_size. if size
822 * is given, ->io_size is size / nr_files.
823 */
824 extend_size = total_size = 0;
825 need_extend = 0;
826 for_each_file(td, f, i) {
Jens Axboebedc9dc2014-03-17 12:51:09 -0600827 f->file_offset = get_start_offset(td, f);
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200828
Jens Axboede98bd32013-04-05 11:09:20 +0200829 if (!o->file_size_low) {
Jens Axboe7bb48f82007-03-27 15:30:28 +0200830 /*
831 * no file size range given, file size is equal to
Jens Axboe002fe732014-02-11 08:31:13 -0700832 * total size divided by number of files. If that is
833 * zero, set it to the real file size. If the size
834 * doesn't divide nicely with the min blocksize,
835 * make the first files bigger.
Jens Axboe7bb48f82007-03-27 15:30:28 +0200836 */
Jens Axboe002fe732014-02-11 08:31:13 -0700837 f->io_size = fs;
838 if (nr_fs_extra) {
839 nr_fs_extra--;
840 f->io_size += bs;
841 }
842
Jens Axboe65bdb102008-01-24 13:13:12 +0100843 if (!f->io_size)
Jens Axboe273f8c92008-01-25 14:02:15 +0100844 f->io_size = f->real_file_size - f->file_offset;
Jens Axboede98bd32013-04-05 11:09:20 +0200845 } else if (f->real_file_size < o->file_size_low ||
846 f->real_file_size > o->file_size_high) {
847 if (f->file_offset > o->file_size_low)
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200848 goto err_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200849 /*
850 * file size given. if it's fixed, use that. if it's a
851 * range, generate a random size in-between.
852 */
Jens Axboede98bd32013-04-05 11:09:20 +0200853 if (o->file_size_low == o->file_size_high)
854 f->io_size = o->file_size_low - f->file_offset;
855 else {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100856 f->io_size = get_rand_file_size(td)
857 - f->file_offset;
858 }
Jens Axboe65bdb102008-01-24 13:13:12 +0100859 } else
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200860 f->io_size = f->real_file_size - f->file_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200861
862 if (f->io_size == -1ULL)
863 total_size = -1ULL;
Shaohua Li4d002562012-09-21 08:32:32 +0200864 else {
Jens Axboede98bd32013-04-05 11:09:20 +0200865 if (o->size_percent)
866 f->io_size = (f->io_size * o->size_percent) / 100;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200867 total_size += f->io_size;
Shaohua Li4d002562012-09-21 08:32:32 +0200868 }
Jens Axboe7bb48f82007-03-27 15:30:28 +0200869
870 if (f->filetype == FIO_TYPE_FILE &&
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200871 (f->io_size + f->file_offset) > f->real_file_size &&
Jens Axboe7bb48f82007-03-27 15:30:28 +0200872 !(td->io_ops->flags & FIO_DISKLESSIO)) {
Jens Axboede98bd32013-04-05 11:09:20 +0200873 if (!o->create_on_open) {
Jens Axboe814452b2009-03-04 12:53:13 +0100874 need_extend++;
875 extend_size += (f->io_size + f->file_offset);
876 } else
877 f->real_file_size = f->io_size + f->file_offset;
Jens Axboed6aed792009-06-03 08:41:15 +0200878 fio_file_set_extend(f);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100879 }
Jens Axboe7bb48f82007-03-27 15:30:28 +0200880 }
881
Jens Axboede98bd32013-04-05 11:09:20 +0200882 if (!o->size || o->size > total_size)
883 o->size = total_size;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200884
885 /*
886 * See if we need to extend some files
887 */
888 if (need_extend) {
889 temp_stall_ts = 1;
Jens Axboef3afa572012-09-17 13:34:16 +0200890 if (output_format == FIO_OUTPUT_NORMAL)
Shaozhi Shawn Yea7ba8c52008-09-10 09:04:18 +0200891 log_info("%s: Laying out IO file(s) (%u file(s) /"
Jens Axboede98bd32013-04-05 11:09:20 +0200892 " %lluMB)\n", o->name, need_extend,
Shaozhi Shawn Yea7ba8c52008-09-10 09:04:18 +0200893 extend_size >> 20);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200894
895 for_each_file(td, f, i) {
Jens Axboe5e0074c2009-06-02 21:40:09 +0200896 unsigned long long old_len = -1ULL, extend_len = -1ULL;
Jens Axboe3baddf22008-04-04 11:10:30 +0200897
Jens Axboed6aed792009-06-03 08:41:15 +0200898 if (!fio_file_extend(f))
Jens Axboe7bb48f82007-03-27 15:30:28 +0200899 continue;
900
Jens Axboe409b3412007-03-28 09:57:01 +0200901 assert(f->filetype == FIO_TYPE_FILE);
Jens Axboed6aed792009-06-03 08:41:15 +0200902 fio_file_clear_extend(f);
Jens Axboede98bd32013-04-05 11:09:20 +0200903 if (!o->fill_device) {
Jens Axboe5e0074c2009-06-02 21:40:09 +0200904 old_len = f->real_file_size;
Jens Axboe0b9d69e2009-09-11 22:29:54 +0200905 extend_len = f->io_size + f->file_offset -
906 old_len;
Jens Axboe5e0074c2009-06-02 21:40:09 +0200907 }
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200908 f->real_file_size = (f->io_size + f->file_offset);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200909 err = extend_file(td, f);
910 if (err)
911 break;
Jens Axboe5e0074c2009-06-02 21:40:09 +0200912
Jens Axboe3baddf22008-04-04 11:10:30 +0200913 err = __file_invalidate_cache(td, f, old_len,
914 extend_len);
915 close(f->fd);
916 f->fd = -1;
917 if (err)
918 break;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200919 }
920 temp_stall_ts = 0;
921 }
922
923 if (err)
Jens Axboee90391e2013-04-13 20:40:53 +0200924 goto err_out;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200925
Jens Axboede98bd32013-04-05 11:09:20 +0200926 if (!o->zone_size)
927 o->zone_size = o->size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200928
Jens Axboeea966f82007-09-03 10:09:37 +0200929 /*
930 * iolog already set the total io size, if we read back
931 * stored entries.
932 */
Jens Axboede98bd32013-04-05 11:09:20 +0200933 if (!o->read_iolog_file)
934 td->total_io_size = o->size * o->loops;
Jens Axboe25460cf2012-05-02 13:58:02 +0200935
936done:
Jens Axboede98bd32013-04-05 11:09:20 +0200937 if (o->create_only)
Jens Axboe25460cf2012-05-02 13:58:02 +0200938 td->done = 1;
939
Jens Axboe8edd9732014-03-01 08:24:03 -0700940 td_restore_runstate(td, old_state);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200941 return 0;
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200942err_offset:
Jens Axboede98bd32013-04-05 11:09:20 +0200943 log_err("%s: you need to specify valid offset=\n", o->name);
Jens Axboee90391e2013-04-13 20:40:53 +0200944err_out:
Jens Axboe8edd9732014-03-01 08:24:03 -0700945 td_restore_runstate(td, old_state);
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200946 return 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200947}
948
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200949int pre_read_files(struct thread_data *td)
950{
951 struct fio_file *f;
952 unsigned int i;
953
954 dprint(FD_FILE, "pre_read files\n");
955
956 for_each_file(td, f, i) {
957 pre_read_file(td, f);
958 }
959
960 return 1;
961}
962
Jens Axboe9c6f6312012-11-07 09:15:45 +0100963static int __init_rand_distribution(struct thread_data *td, struct fio_file *f)
964{
Jens Axboe23162962012-11-07 19:47:47 +0100965 unsigned int range_size, seed;
Jens Axboe9c6f6312012-11-07 09:15:45 +0100966 unsigned long nranges;
Jens Axboe21415db2013-01-04 13:09:29 +0100967 uint64_t file_size;
Jens Axboe9c6f6312012-11-07 09:15:45 +0100968
969 range_size = min(td->o.min_bs[DDIR_READ], td->o.min_bs[DDIR_WRITE]);
Jens Axboe21415db2013-01-04 13:09:29 +0100970 file_size = min(f->real_file_size, f->io_size);
Jens Axboe9c6f6312012-11-07 09:15:45 +0100971
Jens Axboe21415db2013-01-04 13:09:29 +0100972 nranges = (file_size + range_size - 1) / range_size;
Jens Axboe9c6f6312012-11-07 09:15:45 +0100973
Jens Axboe23162962012-11-07 19:47:47 +0100974 seed = jhash(f->file_name, strlen(f->file_name), 0) * td->thread_number;
Jens Axboe84256872012-11-17 10:06:36 -0700975 if (!td->o.rand_repeatable)
976 seed = td->rand_seeds[4];
977
Jens Axboe9c6f6312012-11-07 09:15:45 +0100978 if (td->o.random_distribution == FIO_RAND_DIST_ZIPF)
Jens Axboe888677a2013-04-10 22:19:46 +0200979 zipf_init(&f->zipf, nranges, td->o.zipf_theta.u.f, seed);
Jens Axboe9c6f6312012-11-07 09:15:45 +0100980 else
Jens Axboe888677a2013-04-10 22:19:46 +0200981 pareto_init(&f->zipf, nranges, td->o.pareto_h.u.f, seed);
Jens Axboe9c6f6312012-11-07 09:15:45 +0100982
983 return 1;
984}
985
986static int init_rand_distribution(struct thread_data *td)
987{
988 struct fio_file *f;
989 unsigned int i;
990 int state;
991
992 if (td->o.random_distribution == FIO_RAND_DIST_RANDOM)
993 return 0;
994
Jens Axboe8edd9732014-03-01 08:24:03 -0700995 state = td_bump_runstate(td, TD_SETTING_UP);
996
Jens Axboe9c6f6312012-11-07 09:15:45 +0100997 for_each_file(td, f, i)
998 __init_rand_distribution(td, f);
Jens Axboe8edd9732014-03-01 08:24:03 -0700999
1000 td_restore_runstate(td, state);
Jens Axboe9c6f6312012-11-07 09:15:45 +01001001
1002 return 1;
1003}
1004
Jens Axboe68727072007-03-15 20:49:25 +01001005int init_random_map(struct thread_data *td)
1006{
Jens Axboe51ede0b2012-11-22 13:50:29 +01001007 unsigned long long blocks;
Jens Axboe68727072007-03-15 20:49:25 +01001008 struct fio_file *f;
1009 unsigned int i;
1010
Jens Axboe9c6f6312012-11-07 09:15:45 +01001011 if (init_rand_distribution(td))
1012 return 0;
Jens Axboe3831a842012-11-26 19:59:14 +01001013 if (!td_random(td))
Jens Axboe68727072007-03-15 20:49:25 +01001014 return 0;
1015
1016 for_each_file(td, f, i) {
Jens Axboe38f30c82013-01-11 14:03:05 +01001017 uint64_t file_size = min(f->real_file_size, f->io_size);
1018
Jens Axboe53737ae2013-02-21 15:18:17 +01001019 blocks = file_size / (unsigned long long) td->o.rw_min_bs;
1020
Jens Axboe8055e412012-11-26 08:43:47 +01001021 if (td->o.random_generator == FIO_RAND_GEN_LFSR) {
Jens Axboe82af46b2012-12-04 13:12:25 +01001022 unsigned long seed;
1023
1024 seed = td->rand_seeds[FIO_RAND_BLOCK_OFF];
1025
Jens Axboe225ba9e2014-02-26 14:31:15 -08001026 if (!lfsr_init(&f->lfsr, blocks, seed, 0))
Jens Axboe8055e412012-11-26 08:43:47 +01001027 continue;
Jens Axboe3831a842012-11-26 19:59:14 +01001028 } else if (!td->o.norandommap) {
Jens Axboe7ebd7962012-11-28 21:24:46 +01001029 f->io_axmap = axmap_new(blocks);
1030 if (f->io_axmap)
Jens Axboe8055e412012-11-26 08:43:47 +01001031 continue;
Jens Axboe1cad7122012-11-29 21:37:11 +01001032 } else if (td->o.norandommap)
1033 continue;
Jens Axboeceadd592012-02-02 08:41:28 +01001034
Jens Axboe2b386d22008-03-26 10:32:57 +01001035 if (!td->o.softrandommap) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001036 log_err("fio: failed allocating random map. If running"
1037 " a large number of jobs, try the 'norandommap'"
Jens Axboe2b386d22008-03-26 10:32:57 +01001038 " option or set 'softrandommap'. Or give"
1039 " a larger --alloc-size to fio.\n");
Jens Axboe68727072007-03-15 20:49:25 +01001040 return 1;
1041 }
Jens Axboe303032a2008-03-26 10:11:10 +01001042
1043 log_info("fio: file %s failed allocating random map. Running "
1044 "job without.\n", f->file_name);
Jens Axboe68727072007-03-15 20:49:25 +01001045 }
1046
1047 return 0;
1048}
1049
Jens Axboe53cdc682006-10-18 11:50:58 +02001050void close_files(struct thread_data *td)
1051{
Jens Axboe0ab8db82006-10-18 17:16:23 +02001052 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +01001053 unsigned int i;
Jens Axboe53cdc682006-10-18 11:50:58 +02001054
Jens Axboe2be3eec2009-06-10 09:05:13 +02001055 for_each_file(td, f, i) {
1056 if (fio_file_open(f))
1057 td_io_close_file(td, f);
1058 }
Jens Axboe24ffd2c2008-03-01 15:47:08 +01001059}
1060
1061void close_and_free_files(struct thread_data *td)
1062{
1063 struct fio_file *f;
1064 unsigned int i;
1065
Jens Axboeee56ad52008-02-01 10:30:20 +01001066 dprint(FD_FILE, "close files\n");
1067
Jens Axboe0ab8db82006-10-18 17:16:23 +02001068 for_each_file(td, f, i) {
Jens Axboe22a57ba2009-06-09 14:34:35 +02001069 if (fio_file_open(f))
1070 td_io_close_file(td, f);
1071
Shaozhi Shawn Yeb9fbcf22008-09-10 09:05:17 +02001072 remove_file_hash(f);
Jens Axboeb3dc7f02007-03-09 12:52:15 +01001073
Jens Axboeb5f4d8b2014-03-20 08:28:11 -06001074 if (td->o.unlink && f->filetype == FIO_TYPE_FILE) {
1075 dprint(FD_FILE, "free unlink %s\n", f->file_name);
1076 unlink(f->file_name);
1077 }
1078
Jens Axboef17c4392008-03-01 18:40:46 +01001079 sfree(f->file_name);
Jens Axboefa1da862007-03-23 15:20:54 +01001080 f->file_name = NULL;
Jens Axboe7ebd7962012-11-28 21:24:46 +01001081 axmap_free(f->io_axmap);
1082 f->io_axmap = NULL;
Jens Axboe78d99e62008-03-01 18:41:50 +01001083 sfree(f);
Jens Axboe53cdc682006-10-18 11:50:58 +02001084 }
Jens Axboeb4a6a592006-10-20 13:54:47 +02001085
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001086 td->o.filename = NULL;
Jens Axboecade3ef2007-03-23 15:29:45 +01001087 free(td->files);
Jens Axboed7df1d12013-03-20 19:57:01 -06001088 free(td->file_locks);
Jens Axboe9efef3c2008-03-06 10:26:25 +01001089 td->files_index = 0;
Jens Axboeb4a6a592006-10-20 13:54:47 +02001090 td->files = NULL;
Jens Axboed7df1d12013-03-20 19:57:01 -06001091 td->file_locks = NULL;
Jens Axboe27ddbfa2014-01-22 16:27:32 -08001092 td->o.file_lock_mode = FILE_LOCK_NONE;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001093 td->o.nr_files = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +02001094}
Jens Axboeaf52b342007-03-13 10:07:47 +01001095
Jens Axboee3bab462007-03-13 11:22:09 +01001096static void get_file_type(struct fio_file *f)
Jens Axboeaf52b342007-03-13 10:07:47 +01001097{
1098 struct stat sb;
1099
Jens Axboe66159822007-04-16 21:54:24 +02001100 if (!strcmp(f->file_name, "-"))
1101 f->filetype = FIO_TYPE_PIPE;
1102 else
1103 f->filetype = FIO_TYPE_FILE;
Jens Axboeaf52b342007-03-13 10:07:47 +01001104
Bruce Cran38921822012-02-22 17:55:16 +00001105 /* \\.\ is the device namespace in Windows, where every file is
1106 * a block device */
1107 if (strncmp(f->file_name, "\\\\.\\", 4) == 0)
1108 f->filetype = FIO_TYPE_BD;
1109
Jens Axboeb30d3952010-02-06 23:36:26 +01001110 if (!stat(f->file_name, &sb)) {
Bruce Cran38921822012-02-22 17:55:16 +00001111 if (S_ISBLK(sb.st_mode))
Jens Axboeaf52b342007-03-13 10:07:47 +01001112 f->filetype = FIO_TYPE_BD;
1113 else if (S_ISCHR(sb.st_mode))
1114 f->filetype = FIO_TYPE_CHAR;
Jens Axboeb5605e92007-04-16 20:42:33 +02001115 else if (S_ISFIFO(sb.st_mode))
1116 f->filetype = FIO_TYPE_PIPE;
Jens Axboeaf52b342007-03-13 10:07:47 +01001117 }
1118}
1119
Jens Axboe90426232014-04-01 12:28:47 -06001120static int __is_already_allocated(const char *fname)
1121{
1122 struct flist_head *entry;
1123 char *filename;
1124
1125 if (flist_empty(&filename_list))
1126 return 0;
1127
1128 flist_for_each(entry, &filename_list) {
1129 filename = flist_entry(entry, struct file_name, list)->filename;
1130
1131 if (strcmp(filename, fname) == 0)
1132 return 1;
1133 }
1134
1135 return 0;
1136}
1137
1138static int is_already_allocated(const char *fname)
1139{
1140 int ret;
1141
1142 fio_file_hash_lock();
1143 ret = __is_already_allocated(fname);
1144 fio_file_hash_unlock();
1145 return ret;
1146}
1147
Castor Fu190b8f02014-03-20 10:59:29 -07001148static void set_already_allocated(const char *fname)
1149{
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08001150 struct file_name *fn;
1151
1152 fn = malloc(sizeof(struct file_name));
1153 fn->filename = strdup(fname);
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08001154
Jens Axboe90426232014-04-01 12:28:47 -06001155 fio_file_hash_lock();
1156 if (!__is_already_allocated(fname)) {
1157 flist_add_tail(&fn->list, &filename_list);
1158 fn = NULL;
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08001159 }
Jens Axboe90426232014-04-01 12:28:47 -06001160 fio_file_hash_unlock();
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08001161
Jens Axboe90426232014-04-01 12:28:47 -06001162 if (fn) {
1163 free(fn->filename);
1164 free(fn);
1165 }
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08001166}
1167
Jens Axboe90426232014-04-01 12:28:47 -06001168
Castor Fu190b8f02014-03-20 10:59:29 -07001169static void free_already_allocated(void)
1170{
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08001171 struct flist_head *entry, *tmp;
1172 struct file_name *fn;
1173
Jens Axboe90426232014-04-01 12:28:47 -06001174 if (flist_empty(&filename_list))
1175 return;
1176
1177 fio_file_hash_lock();
1178 flist_for_each_safe(entry, tmp, &filename_list) {
1179 fn = flist_entry(entry, struct file_name, list);
1180 free(fn->filename);
1181 flist_del(&fn->list);
1182 free(fn);
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08001183 }
Jens Axboe90426232014-04-01 12:28:47 -06001184
1185 fio_file_hash_unlock();
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08001186}
1187
Jens Axboe7b5cb702014-04-01 16:37:03 -06001188static struct fio_file *alloc_new_file(struct thread_data *td)
1189{
1190 struct fio_file *f;
1191
1192 f = smalloc(sizeof(*f));
1193 if (!f) {
1194 log_err("fio: smalloc OOM\n");
1195 assert(0);
1196 return NULL;
1197 }
1198
1199 f->fd = -1;
1200 f->shadow_fd = -1;
1201 fio_file_reset(td, f);
1202 return f;
1203}
1204
Jens Axboe5903e7b2014-02-26 13:42:13 -08001205int add_file(struct thread_data *td, const char *fname, int numjob, int inc)
Jens Axboeaf52b342007-03-13 10:07:47 +01001206{
Jens Axboe7b4e4fe2007-03-13 12:51:40 +01001207 int cur_files = td->files_index;
Jens Axboebd0ee742007-03-23 14:26:23 +01001208 char file_name[PATH_MAX];
Jens Axboeaf52b342007-03-13 10:07:47 +01001209 struct fio_file *f;
Jens Axboebd0ee742007-03-23 14:26:23 +01001210 int len = 0;
Jens Axboeaf52b342007-03-13 10:07:47 +01001211
Jens Axboeee56ad52008-02-01 10:30:20 +01001212 dprint(FD_FILE, "add file %s\n", fname);
1213
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08001214 if (td->o.directory)
1215 len = set_name_idx(file_name, td->o.directory, numjob);
1216
1217 sprintf(file_name + len, "%s", fname);
1218
1219 /* clean cloned siblings using existing files */
1220 if (numjob && is_already_allocated(file_name))
1221 return 0;
1222
Jens Axboe7b5cb702014-04-01 16:37:03 -06001223 f = alloc_new_file(td);
Jens Axboebd0ee742007-03-23 14:26:23 +01001224
Jens Axboefc99bc02009-03-04 15:27:42 +01001225 if (td->files_size <= td->files_index) {
Jianpeng Ma1983e322013-01-10 13:19:27 +01001226 unsigned int new_size = td->o.nr_files + 1;
Jens Axboe126d65c2008-03-01 18:04:31 +01001227
Jens Axboefc99bc02009-03-04 15:27:42 +01001228 dprint(FD_FILE, "resize file array to %d files\n", new_size);
1229
1230 td->files = realloc(td->files, new_size * sizeof(f));
Jianpeng Mad537c082013-01-11 08:52:55 +01001231 if (td->files == NULL) {
1232 log_err("fio: realloc OOM\n");
1233 assert(0);
1234 }
Jens Axboed7df1d12013-03-20 19:57:01 -06001235 if (td->o.file_lock_mode != FILE_LOCK_NONE) {
1236 td->file_locks = realloc(td->file_locks, new_size);
1237 if (!td->file_locks) {
1238 log_err("fio: realloc OOM\n");
1239 assert(0);
1240 }
1241 td->file_locks[cur_files] = FILE_LOCK_NONE;
1242 }
Jens Axboefc99bc02009-03-04 15:27:42 +01001243 td->files_size = new_size;
1244 }
Jens Axboe8bb76792009-03-04 15:37:52 +01001245 td->files[cur_files] = f;
Shaohua Li89ac1d42012-06-11 08:56:32 +02001246 f->fileno = cur_files;
Jens Axboe126d65c2008-03-01 18:04:31 +01001247
Jens Axboe07eb79d2007-04-12 13:02:38 +02001248 /*
1249 * init function, io engine may not be loaded yet
1250 */
1251 if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO))
1252 f->real_file_size = -1ULL;
1253
Jens Axboef17c4392008-03-01 18:40:46 +01001254 f->file_name = smalloc_strdup(file_name);
Jens Axboec48c0be2008-07-31 19:55:02 +02001255 if (!f->file_name) {
1256 log_err("fio: smalloc OOM\n");
1257 assert(0);
1258 }
Jens Axboe0b9d69e2009-09-11 22:29:54 +02001259
Jens Axboee3bab462007-03-13 11:22:09 +01001260 get_file_type(f);
Jens Axboeaf52b342007-03-13 10:07:47 +01001261
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001262 switch (td->o.file_lock_mode) {
1263 case FILE_LOCK_NONE:
1264 break;
1265 case FILE_LOCK_READWRITE:
Jens Axboed7df1d12013-03-20 19:57:01 -06001266 f->rwlock = fio_rwlock_init();
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001267 break;
1268 case FILE_LOCK_EXCLUSIVE:
Jens Axboe521da522012-08-02 11:21:36 +02001269 f->lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001270 break;
1271 default:
1272 log_err("fio: unknown lock mode: %d\n", td->o.file_lock_mode);
1273 assert(0);
1274 }
Jens Axboe29c13492008-03-01 19:25:20 +01001275
Jens Axboe7b4e4fe2007-03-13 12:51:40 +01001276 td->files_index++;
Jens Axboe15494412007-03-13 11:52:47 +01001277 if (f->filetype == FIO_TYPE_FILE)
1278 td->nr_normal_files++;
Jens Axboef29b25a2007-07-23 08:56:43 +02001279
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08001280 set_already_allocated(file_name);
1281
Jens Axboebcd27f72014-02-25 14:01:26 -08001282 /*
1283 * For adding files after the fact - if openfiles= isn't
1284 * given as an option, ensure we allow at least one file open
1285 */
1286 if (!td->o.open_files)
1287 td->o.open_files = 1;
1288
Jens Axboe5903e7b2014-02-26 13:42:13 -08001289 if (inc)
1290 td->o.nr_files++;
1291
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001292 dprint(FD_FILE, "file %p \"%s\" added at %d\n", f, f->file_name,
1293 cur_files);
Jens Axboe9efef3c2008-03-06 10:26:25 +01001294
Jens Axboef29b25a2007-07-23 08:56:43 +02001295 return cur_files;
Jens Axboeaf52b342007-03-13 10:07:47 +01001296}
Jens Axboe0ad920e2007-03-13 11:06:45 +01001297
Jens Axboe49ffb4a2010-08-24 15:01:37 +02001298int add_file_exclusive(struct thread_data *td, const char *fname)
1299{
1300 struct fio_file *f;
1301 unsigned int i;
1302
1303 for_each_file(td, f, i) {
1304 if (!strcmp(f->file_name, fname))
1305 return i;
1306 }
1307
Jens Axboe5903e7b2014-02-26 13:42:13 -08001308 return add_file(td, fname, 0, 1);
Jens Axboe49ffb4a2010-08-24 15:01:37 +02001309}
1310
Jens Axboe0ad920e2007-03-13 11:06:45 +01001311void get_file(struct fio_file *f)
1312{
Jens Axboe8172fe92008-02-01 21:51:02 +01001313 dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references);
Jens Axboed6aed792009-06-03 08:41:15 +02001314 assert(fio_file_open(f));
Jens Axboe0ad920e2007-03-13 11:06:45 +01001315 f->references++;
1316}
1317
Jens Axboe6977bcd2008-03-01 15:55:36 +01001318int put_file(struct thread_data *td, struct fio_file *f)
Jens Axboe0ad920e2007-03-13 11:06:45 +01001319{
Jens Axboe98e1ac42008-03-06 11:56:48 +01001320 int f_ret = 0, ret = 0;
Jens Axboe6977bcd2008-03-01 15:55:36 +01001321
Jens Axboe8172fe92008-02-01 21:51:02 +01001322 dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references);
Jens Axboeee56ad52008-02-01 10:30:20 +01001323
Jens Axboe22a57ba2009-06-09 14:34:35 +02001324 if (!fio_file_open(f)) {
1325 assert(f->fd == -1);
Jens Axboe6977bcd2008-03-01 15:55:36 +01001326 return 0;
Jens Axboe22a57ba2009-06-09 14:34:35 +02001327 }
Jens Axboe0ad920e2007-03-13 11:06:45 +01001328
1329 assert(f->references);
1330 if (--f->references)
Jens Axboe6977bcd2008-03-01 15:55:36 +01001331 return 0;
Jens Axboe0ad920e2007-03-13 11:06:45 +01001332
Jens Axboed424d4d2007-04-17 09:05:10 +02001333 if (should_fsync(td) && td->o.fsync_on_close)
Jens Axboe98e1ac42008-03-06 11:56:48 +01001334 f_ret = fsync(f->fd);
Jens Axboeebb14152007-03-13 14:42:15 +01001335
Jens Axboe0ad920e2007-03-13 11:06:45 +01001336 if (td->io_ops->close_file)
Jens Axboe6977bcd2008-03-01 15:55:36 +01001337 ret = td->io_ops->close_file(td, f);
Jens Axboe1020a132007-03-29 11:15:06 +02001338
Jens Axboe98e1ac42008-03-06 11:56:48 +01001339 if (!ret)
Jens Axboea5fb4612008-05-28 10:54:01 +02001340 ret = f_ret;
Jens Axboe98e1ac42008-03-06 11:56:48 +01001341
Jens Axboe0ad920e2007-03-13 11:06:45 +01001342 td->nr_open_files--;
Jens Axboed6aed792009-06-03 08:41:15 +02001343 fio_file_clear_open(f);
Jens Axboe22a57ba2009-06-09 14:34:35 +02001344 assert(f->fd == -1);
Jens Axboe6977bcd2008-03-01 15:55:36 +01001345 return ret;
Jens Axboe0ad920e2007-03-13 11:06:45 +01001346}
Jens Axboebbf6b542007-03-13 15:28:55 +01001347
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001348void lock_file(struct thread_data *td, struct fio_file *f, enum fio_ddir ddir)
Jens Axboeb2bd2bd2008-03-01 18:19:52 +01001349{
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001350 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1351 return;
Jens Axboe29c13492008-03-01 19:25:20 +01001352
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001353 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
1354 if (ddir == DDIR_READ)
Jens Axboed7df1d12013-03-20 19:57:01 -06001355 fio_rwlock_read(f->rwlock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001356 else
Jens Axboed7df1d12013-03-20 19:57:01 -06001357 fio_rwlock_write(f->rwlock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001358 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1359 fio_mutex_down(f->lock);
1360
Jens Axboed7df1d12013-03-20 19:57:01 -06001361 td->file_locks[f->fileno] = td->o.file_lock_mode;
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001362}
1363
1364void unlock_file(struct thread_data *td, struct fio_file *f)
1365{
1366 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1367 return;
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001368
Jens Axboed7df1d12013-03-20 19:57:01 -06001369 if (td->o.file_lock_mode == FILE_LOCK_READWRITE)
1370 fio_rwlock_unlock(f->rwlock);
1371 else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001372 fio_mutex_up(f->lock);
Jens Axboed7df1d12013-03-20 19:57:01 -06001373
1374 td->file_locks[f->fileno] = FILE_LOCK_NONE;
Jens Axboeb2bd2bd2008-03-01 18:19:52 +01001375}
1376
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001377void unlock_file_all(struct thread_data *td, struct fio_file *f)
Jens Axboeb2bd2bd2008-03-01 18:19:52 +01001378{
Jens Axboef2a28032014-02-11 09:12:06 -07001379 if (td->o.file_lock_mode == FILE_LOCK_NONE || !td->file_locks)
Jens Axboe27ddbfa2014-01-22 16:27:32 -08001380 return;
Jens Axboed7df1d12013-03-20 19:57:01 -06001381 if (td->file_locks[f->fileno] != FILE_LOCK_NONE)
1382 unlock_file(td, f);
Jens Axboeb2bd2bd2008-03-01 18:19:52 +01001383}
1384
Jens Axboebbf6b542007-03-13 15:28:55 +01001385static int recurse_dir(struct thread_data *td, const char *dirname)
1386{
1387 struct dirent *dir;
1388 int ret = 0;
1389 DIR *D;
1390
1391 D = opendir(dirname);
1392 if (!D) {
Jens Axboe0ddb2702007-03-27 19:43:53 +02001393 char buf[FIO_VERROR_SIZE];
1394
Ken Raeburn98ffb8f2013-01-30 22:31:09 +01001395 snprintf(buf, FIO_VERROR_SIZE, "opendir(%s)", dirname);
Jens Axboe0ddb2702007-03-27 19:43:53 +02001396 td_verror(td, errno, buf);
Jens Axboebbf6b542007-03-13 15:28:55 +01001397 return 1;
1398 }
1399
1400 while ((dir = readdir(D)) != NULL) {
1401 char full_path[PATH_MAX];
1402 struct stat sb;
1403
Jens Axboee85b2b82007-03-14 09:39:06 +01001404 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
1405 continue;
Jens Axboe96d32d52007-03-14 09:16:09 +01001406
Bruce Cranb9fd7882012-02-20 17:01:46 +00001407 sprintf(full_path, "%s%s%s", dirname, FIO_OS_PATH_SEPARATOR, dir->d_name);
Jens Axboebbf6b542007-03-13 15:28:55 +01001408
1409 if (lstat(full_path, &sb) == -1) {
1410 if (errno != ENOENT) {
1411 td_verror(td, errno, "stat");
1412 return 1;
1413 }
1414 }
1415
1416 if (S_ISREG(sb.st_mode)) {
Jens Axboe5903e7b2014-02-26 13:42:13 -08001417 add_file(td, full_path, 0, 1);
Jens Axboebbf6b542007-03-13 15:28:55 +01001418 continue;
1419 }
Jens Axboe0ddb2702007-03-27 19:43:53 +02001420 if (!S_ISDIR(sb.st_mode))
1421 continue;
Jens Axboebbf6b542007-03-13 15:28:55 +01001422
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001423 ret = recurse_dir(td, full_path);
1424 if (ret)
Jens Axboebbf6b542007-03-13 15:28:55 +01001425 break;
1426 }
1427
1428 closedir(D);
1429 return ret;
1430}
1431
1432int add_dir_files(struct thread_data *td, const char *path)
1433{
Jens Axboe0ddb2702007-03-27 19:43:53 +02001434 int ret = recurse_dir(td, path);
1435
1436 if (!ret)
1437 log_info("fio: opendir added %d files\n", td->o.nr_files);
1438
1439 return ret;
Jens Axboebbf6b542007-03-13 15:28:55 +01001440}
Jens Axboecade3ef2007-03-23 15:29:45 +01001441
1442void dup_files(struct thread_data *td, struct thread_data *org)
1443{
1444 struct fio_file *f;
1445 unsigned int i;
Jens Axboe9efef3c2008-03-06 10:26:25 +01001446
1447 dprint(FD_FILE, "dup files: %d\n", org->files_index);
Jens Axboecade3ef2007-03-23 15:29:45 +01001448
1449 if (!org->files)
1450 return;
1451
Jens Axboe9efef3c2008-03-06 10:26:25 +01001452 td->files = malloc(org->files_index * sizeof(f));
Jens Axboecade3ef2007-03-23 15:29:45 +01001453
Jens Axboed7df1d12013-03-20 19:57:01 -06001454 if (td->o.file_lock_mode != FILE_LOCK_NONE)
1455 td->file_locks = malloc(org->files_index);
1456
Jens Axboe9efef3c2008-03-06 10:26:25 +01001457 for_each_file(org, f, i) {
Jens Axboeb0fe4212008-03-01 18:22:27 +01001458 struct fio_file *__f;
1459
Jens Axboe7b5cb702014-04-01 16:37:03 -06001460 __f = alloc_new_file(td);
Jens Axboe0b9d69e2009-09-11 22:29:54 +02001461
Aaron Carrollbc3456f2008-06-25 09:20:37 +02001462 if (f->file_name) {
Jens Axboef17c4392008-03-01 18:40:46 +01001463 __f->file_name = smalloc_strdup(f->file_name);
Jens Axboec48c0be2008-07-31 19:55:02 +02001464 if (!__f->file_name) {
1465 log_err("fio: smalloc OOM\n");
1466 assert(0);
1467 }
Jens Axboe0b9d69e2009-09-11 22:29:54 +02001468
Aaron Carrollbc3456f2008-06-25 09:20:37 +02001469 __f->filetype = f->filetype;
1470 }
Jens Axboeb0fe4212008-03-01 18:22:27 +01001471
Jens Axboe67ad9242014-02-07 10:56:15 -07001472 if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1473 __f->lock = f->lock;
1474 else if (td->o.file_lock_mode == FILE_LOCK_READWRITE)
1475 __f->rwlock = f->rwlock;
1476
Jens Axboeb0fe4212008-03-01 18:22:27 +01001477 td->files[i] = __f;
Jens Axboecade3ef2007-03-23 15:29:45 +01001478 }
1479}
Jens Axboef29b25a2007-07-23 08:56:43 +02001480
1481/*
1482 * Returns the index that matches the filename, or -1 if not there
1483 */
1484int get_fileno(struct thread_data *td, const char *fname)
1485{
1486 struct fio_file *f;
1487 unsigned int i;
1488
1489 for_each_file(td, f, i)
1490 if (!strcmp(f->file_name, fname))
1491 return i;
1492
1493 return -1;
1494}
1495
1496/*
1497 * For log usage, where we add/open/close files automatically
1498 */
1499void free_release_files(struct thread_data *td)
1500{
1501 close_files(td);
1502 td->files_index = 0;
1503 td->nr_normal_files = 0;
1504}
Jens Axboe33c48812013-01-21 09:46:06 -07001505
1506void fio_file_reset(struct thread_data *td, struct fio_file *f)
1507{
1508 f->last_pos = f->file_offset;
1509 f->last_start = -1ULL;
1510 if (f->io_axmap)
1511 axmap_reset(f->io_axmap);
1512 if (td->o.random_generator == FIO_RAND_GEN_LFSR)
1513 lfsr_reset(&f->lfsr, td->rand_seeds[FIO_RAND_BLOCK_OFF]);
1514}
Jens Axboe002fe732014-02-11 08:31:13 -07001515
1516int fio_files_done(struct thread_data *td)
1517{
1518 struct fio_file *f;
1519 unsigned int i;
1520
1521 for_each_file(td, f, i)
1522 if (!fio_file_done(f))
1523 return 0;
1524
1525 return 1;
1526}
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08001527
1528/* free memory used in initialization phase only */
Castor Fu190b8f02014-03-20 10:59:29 -07001529void filesetup_mem_free(void)
1530{
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08001531 free_already_allocated();
1532}