blob: bf56112b72b9784cf32583ebc0dbba72324b9f86 [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;
Jens Axboef3e1eb22014-04-11 11:28:54 -060041 char *b = NULL;
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;
Jens Axboef3e1eb22014-04-11 11:28:54 -0600192 if (b)
193 free(b);
Jens Axboe53cdc682006-10-18 11:50:58 +0200194 return 1;
195}
196
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200197static int pre_read_file(struct thread_data *td, struct fio_file *f)
198{
Jens Axboeef799a62014-04-01 17:01:30 -0600199 int ret = 0, r, did_open = 0, old_runstate;
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200200 unsigned long long left;
201 unsigned int bs;
202 char *b;
203
Jens Axboe9c0d2242009-07-01 12:26:28 +0200204 if (td->io_ops->flags & FIO_PIPEIO)
205 return 0;
206
Jens Axboed6aed792009-06-03 08:41:15 +0200207 if (!fio_file_open(f)) {
Jens Axboeb0f65862009-05-20 11:52:15 +0200208 if (td->io_ops->open_file(td, f)) {
209 log_err("fio: cannot pre-read, failed to open file\n");
210 return 1;
211 }
212 did_open = 1;
213 }
214
Jens Axboe8edd9732014-03-01 08:24:03 -0700215 old_runstate = td_bump_runstate(td, TD_PRE_READING);
Jens Axboeb0f65862009-05-20 11:52:15 +0200216
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200217 bs = td->o.max_bs[DDIR_READ];
218 b = malloc(bs);
219 memset(b, 0, bs);
220
Jens Axboeef799a62014-04-01 17:01:30 -0600221 if (lseek(f->fd, f->file_offset, SEEK_SET) < 0) {
222 td_verror(td, errno, "lseek");
223 log_err("fio: failed to lseek pre-read file\n");
224 ret = 1;
225 goto error;
226 }
227
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200228 left = f->io_size;
229
230 while (left && !td->terminate) {
231 if (bs > left)
232 bs = left;
233
234 r = read(f->fd, b, bs);
235
236 if (r == (int) bs) {
237 left -= bs;
238 continue;
239 } else {
240 td_verror(td, EIO, "pre_read");
241 break;
242 }
243 }
244
Jens Axboeef799a62014-04-01 17:01:30 -0600245error:
Jens Axboe8edd9732014-03-01 08:24:03 -0700246 td_restore_runstate(td, old_runstate);
Jens Axboeb0f65862009-05-20 11:52:15 +0200247
248 if (did_open)
249 td->io_ops->close_file(td, f);
Jens Axboeef799a62014-04-01 17:01:30 -0600250
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200251 free(b);
Jens Axboeef799a62014-04-01 17:01:30 -0600252 return ret;
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200253}
254
Jens Axboe7bb48f82007-03-27 15:30:28 +0200255static unsigned long long get_rand_file_size(struct thread_data *td)
Jens Axboe9c60ce62007-03-15 09:14:47 +0100256{
Jens Axboedc873b62008-06-04 20:13:04 +0200257 unsigned long long ret, sized;
Jens Axboe1294c3e2011-05-11 08:15:18 +0200258 unsigned long r;
Jens Axboe9c60ce62007-03-15 09:14:47 +0100259
Jens Axboe4c07ad82011-03-28 09:51:09 +0200260 if (td->o.use_os_rand) {
261 r = os_random_long(&td->file_size_state);
262 sized = td->o.file_size_high - td->o.file_size_low;
263 ret = (unsigned long long) ((double) sized * (r / (OS_RAND_MAX + 1.0)));
264 } else {
265 r = __rand(&td->__file_size_state);
266 sized = td->o.file_size_high - td->o.file_size_low;
267 ret = (unsigned long long) ((double) sized * (r / (FRAND_MAX + 1.0)));
268 }
269
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100270 ret += td->o.file_size_low;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100271 ret -= (ret % td->o.rw_min_bs);
Jens Axboe9c60ce62007-03-15 09:14:47 +0100272 return ret;
273}
274
Jens Axboe53cdc682006-10-18 11:50:58 +0200275static int file_size(struct thread_data *td, struct fio_file *f)
276{
277 struct stat st;
278
Jens Axboedf9c26b2009-03-05 10:13:58 +0100279 if (stat(f->file_name, &st) == -1) {
Jens Axboe7bb48f82007-03-27 15:30:28 +0200280 td_verror(td, errno, "fstat");
281 return 1;
282 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200283
Jens Axboe7bb48f82007-03-27 15:30:28 +0200284 f->real_file_size = st.st_size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200285 return 0;
286}
287
288static int bdev_size(struct thread_data *td, struct fio_file *f)
289{
Bruce Cran9b836562011-01-08 19:49:54 +0100290 unsigned long long bytes = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200291 int r;
292
Jens Axboedf9c26b2009-03-05 10:13:58 +0100293 if (td->io_ops->open_file(td, f)) {
294 log_err("fio: failed opening blockdev %s for size check\n",
295 f->file_name);
296 return 1;
297 }
298
Bruce Cranecc314b2011-01-04 10:59:30 +0100299 r = blockdev_size(f, &bytes);
Jens Axboe53cdc682006-10-18 11:50:58 +0200300 if (r) {
Jens Axboee1161c32007-02-22 19:36:48 +0100301 td_verror(td, r, "blockdev_size");
Jens Axboedf9c26b2009-03-05 10:13:58 +0100302 goto err;
Jens Axboe53cdc682006-10-18 11:50:58 +0200303 }
304
Jens Axboe7ab077a2009-02-02 15:07:37 +0100305 if (!bytes) {
306 log_err("%s: zero sized block device?\n", f->file_name);
Jens Axboedf9c26b2009-03-05 10:13:58 +0100307 goto err;
Jens Axboe7ab077a2009-02-02 15:07:37 +0100308 }
309
Jens Axboe53cdc682006-10-18 11:50:58 +0200310 f->real_file_size = bytes;
Jens Axboe22a57ba2009-06-09 14:34:35 +0200311 td->io_ops->close_file(td, f);
Jens Axboe53cdc682006-10-18 11:50:58 +0200312 return 0;
Jens Axboedf9c26b2009-03-05 10:13:58 +0100313err:
314 td->io_ops->close_file(td, f);
315 return 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200316}
317
Jens Axboe4ccdccd2010-06-24 10:27:22 +0200318static int char_size(struct thread_data *td, struct fio_file *f)
319{
320#ifdef FIO_HAVE_CHARDEV_SIZE
Bruce Cran9b836562011-01-08 19:49:54 +0100321 unsigned long long bytes = 0;
Jens Axboe4ccdccd2010-06-24 10:27:22 +0200322 int r;
323
324 if (td->io_ops->open_file(td, f)) {
325 log_err("fio: failed opening blockdev %s for size check\n",
326 f->file_name);
327 return 1;
328 }
329
Bruce Cranecc314b2011-01-04 10:59:30 +0100330 r = chardev_size(f, &bytes);
Jens Axboe4ccdccd2010-06-24 10:27:22 +0200331 if (r) {
332 td_verror(td, r, "chardev_size");
333 goto err;
334 }
335
336 if (!bytes) {
337 log_err("%s: zero sized char device?\n", f->file_name);
338 goto err;
339 }
340
341 f->real_file_size = bytes;
342 td->io_ops->close_file(td, f);
343 return 0;
344err:
345 td->io_ops->close_file(td, f);
346 return 1;
347#else
348 f->real_file_size = -1ULL;
349 return 0;
350#endif
351}
352
Jens Axboe53cdc682006-10-18 11:50:58 +0200353static int get_file_size(struct thread_data *td, struct fio_file *f)
354{
355 int ret = 0;
356
Jens Axboed6aed792009-06-03 08:41:15 +0200357 if (fio_file_size_known(f))
Jens Axboe409b3412007-03-28 09:57:01 +0200358 return 0;
359
Jens Axboe7bb48f82007-03-27 15:30:28 +0200360 if (f->filetype == FIO_TYPE_FILE)
361 ret = file_size(td, f);
362 else if (f->filetype == FIO_TYPE_BD)
Jens Axboe53cdc682006-10-18 11:50:58 +0200363 ret = bdev_size(td, f);
Jens Axboe4ccdccd2010-06-24 10:27:22 +0200364 else if (f->filetype == FIO_TYPE_CHAR)
365 ret = char_size(td, f);
Jens Axboe53cdc682006-10-18 11:50:58 +0200366 else
367 f->real_file_size = -1;
368
369 if (ret)
370 return ret;
371
372 if (f->file_offset > f->real_file_size) {
Bruce Cran0f2152c2010-12-15 10:33:03 +0100373 log_err("%s: offset extends end (%llu > %llu)\n", td->o.name,
Jens Axboe4e0a8fa2013-04-15 11:40:57 +0200374 (unsigned long long) f->file_offset,
375 (unsigned long long) f->real_file_size);
Jens Axboe53cdc682006-10-18 11:50:58 +0200376 return 1;
377 }
378
Jens Axboed6aed792009-06-03 08:41:15 +0200379 fio_file_set_size_known(f);
Jens Axboe53cdc682006-10-18 11:50:58 +0200380 return 0;
381}
382
Jens Axboe3baddf22008-04-04 11:10:30 +0200383static int __file_invalidate_cache(struct thread_data *td, struct fio_file *f,
384 unsigned long long off,
385 unsigned long long len)
Jens Axboee5b401d2006-10-18 16:03:40 +0200386{
387 int ret = 0;
388
Jens Axboe5e0074c2009-06-02 21:40:09 +0200389 if (len == -1ULL)
Jens Axboe3baddf22008-04-04 11:10:30 +0200390 len = f->io_size;
391 if (off == -1ULL)
392 off = f->file_offset;
Jens Axboeee56ad52008-02-01 10:30:20 +0100393
Jens Axboe0d1cd202009-06-05 22:11:52 +0200394 if (len == -1ULL || off == -1ULL)
395 return 0;
396
Jens Axboe3baddf22008-04-04 11:10:30 +0200397 dprint(FD_IO, "invalidate cache %s: %llu/%llu\n", f->file_name, off,
398 len);
Jens Axboeb5af8292007-03-08 12:43:13 +0100399
Jens Axboea1c58072009-08-04 23:17:02 +0200400 if (f->mmap_ptr) {
Bruce Cran03e20d62011-01-02 20:14:54 +0100401 ret = posix_madvise(f->mmap_ptr, f->mmap_sz, POSIX_MADV_DONTNEED);
Jens Axboea1c58072009-08-04 23:17:02 +0200402#ifdef FIO_MADV_FREE
Jens Axboe3e10fb82013-08-09 14:31:06 -0600403 if (f->filetype == FIO_TYPE_BD)
404 (void) posix_madvise(f->mmap_ptr, f->mmap_sz, FIO_MADV_FREE);
Jens Axboea1c58072009-08-04 23:17:02 +0200405#endif
406 } else if (f->filetype == FIO_TYPE_FILE) {
Bruce Cranecc314b2011-01-04 10:59:30 +0100407 ret = posix_fadvise(f->fd, off, len, POSIX_FADV_DONTNEED);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100408 } else if (f->filetype == FIO_TYPE_BD) {
Bruce Cranecc314b2011-01-04 10:59:30 +0100409 ret = blockdev_invalidate_cache(f);
Jens Axboe7e0e25c2007-03-27 12:49:56 +0200410 if (ret < 0 && errno == EACCES && geteuid()) {
Jens Axboe7172cfe2007-07-23 14:36:00 +0200411 if (!root_warn) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100412 log_err("fio: only root may flush block "
413 "devices. Cache flush bypassed!\n");
Jens Axboe7172cfe2007-07-23 14:36:00 +0200414 root_warn = 1;
415 }
Jens Axboe7e0e25c2007-03-27 12:49:56 +0200416 ret = 0;
417 }
Jens Axboeb5605e92007-04-16 20:42:33 +0200418 } else if (f->filetype == FIO_TYPE_CHAR || f->filetype == FIO_TYPE_PIPE)
Jens Axboee5b401d2006-10-18 16:03:40 +0200419 ret = 0;
420
Jens Axboedfe11fd2014-04-10 08:59:43 -0600421 /*
422 * Cache flushing isn't a fatal condition, and we know it will
423 * happen on some platforms where we don't have the proper
424 * function to flush eg block device caches. So just warn and
425 * continue on our way.
426 */
427 if (ret) {
428 log_info("fio: cache invalidation of %s failed: %s\n", f->file_name, strerror(errno));
429 ret = 0;
Jens Axboee5b401d2006-10-18 16:03:40 +0200430 }
431
Jens Axboedfe11fd2014-04-10 08:59:43 -0600432 return 0;
Jens Axboe3baddf22008-04-04 11:10:30 +0200433
434}
435
436int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
437{
Jens Axboed6aed792009-06-03 08:41:15 +0200438 if (!fio_file_open(f))
Jens Axboea5fb4612008-05-28 10:54:01 +0200439 return 0;
440
Jens Axboe5e0074c2009-06-02 21:40:09 +0200441 return __file_invalidate_cache(td, f, -1ULL, -1ULL);
Jens Axboee5b401d2006-10-18 16:03:40 +0200442}
443
Jens Axboe6977bcd2008-03-01 15:55:36 +0100444int generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
Jens Axboe53cdc682006-10-18 11:50:58 +0200445{
Jens Axboe6977bcd2008-03-01 15:55:36 +0100446 int ret = 0;
447
Jens Axboeee56ad52008-02-01 10:30:20 +0100448 dprint(FD_FILE, "fd close %s\n", f->file_name);
Jens Axboe4906e0b2008-03-01 18:59:51 +0100449
450 remove_file_hash(f);
451
Jens Axboe6977bcd2008-03-01 15:55:36 +0100452 if (close(f->fd) < 0)
453 ret = errno;
454
Jens Axboeb5af8292007-03-08 12:43:13 +0100455 f->fd = -1;
Jens Axboee6c4d732012-12-12 08:16:27 +0100456
457 if (f->shadow_fd != -1) {
458 close(f->shadow_fd);
459 f->shadow_fd = -1;
460 }
461
Juan Casse57e54e02013-09-20 09:20:52 -0600462 f->engine_data = 0;
Jens Axboe6977bcd2008-03-01 15:55:36 +0100463 return ret;
Jens Axboe53cdc682006-10-18 11:50:58 +0200464}
465
Dmitry Monakhov1ccc6dc2012-09-19 23:22:53 +0400466int file_lookup_open(struct fio_file *f, int flags)
Jens Axboe53cdc682006-10-18 11:50:58 +0200467{
Jens Axboe29c13492008-03-01 19:25:20 +0100468 struct fio_file *__f;
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100469 int from_hash;
470
471 __f = lookup_file_hash(f->file_name);
472 if (__f) {
Jens Axboe9efef3c2008-03-06 10:26:25 +0100473 dprint(FD_FILE, "found file in hash %s\n", f->file_name);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100474 /*
475 * racy, need the __f->lock locked
476 */
477 f->lock = __f->lock;
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100478 from_hash = 1;
479 } else {
Jens Axboe9efef3c2008-03-06 10:26:25 +0100480 dprint(FD_FILE, "file not found in hash %s\n", f->file_name);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100481 from_hash = 0;
482 }
483
Jens Axboee8670ef2008-03-06 12:06:30 +0100484 f->fd = open(f->file_name, flags, 0600);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100485 return from_hash;
486}
487
Jens Axboee6c4d732012-12-12 08:16:27 +0100488static int file_close_shadow_fds(struct thread_data *td)
489{
490 struct fio_file *f;
491 int num_closed = 0;
492 unsigned int i;
493
494 for_each_file(td, f, i) {
495 if (f->shadow_fd == -1)
496 continue;
497
498 close(f->shadow_fd);
499 f->shadow_fd = -1;
500 num_closed++;
501 }
502
503 return num_closed;
504}
505
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100506int generic_open_file(struct thread_data *td, struct fio_file *f)
507{
Jens Axboe66159822007-04-16 21:54:24 +0200508 int is_std = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200509 int flags = 0;
Jens Axboe29c13492008-03-01 19:25:20 +0100510 int from_hash = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200511
Jens Axboeee56ad52008-02-01 10:30:20 +0100512 dprint(FD_FILE, "fd open %s\n", f->file_name);
513
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200514 if (td_trim(td) && f->filetype != FIO_TYPE_BD) {
515 log_err("fio: trim only applies to block device\n");
516 return 1;
517 }
518
Jens Axboe66159822007-04-16 21:54:24 +0200519 if (!strcmp(f->file_name, "-")) {
520 if (td_rw(td)) {
521 log_err("fio: can't read/write to stdin/out\n");
522 return 1;
523 }
524 is_std = 1;
Jens Axboece98fa62007-04-17 13:27:32 +0200525
526 /*
527 * move output logging to stderr, if we are writing to stdout
528 */
529 if (td_write(td))
530 f_out = stderr;
Jens Axboe66159822007-04-16 21:54:24 +0200531 }
532
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200533 if (td_trim(td))
534 goto skip_flags;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100535 if (td->o.odirect)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100536 flags |= OS_O_DIRECT;
Chris Masond01612f2013-11-15 15:52:58 -0700537 if (td->o.oatomic) {
538 if (!FIO_O_ATOMIC) {
539 td_verror(td, EINVAL, "OS does not support atomic IO");
540 return 1;
541 }
542 flags |= OS_O_DIRECT | FIO_O_ATOMIC;
543 }
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100544 if (td->o.sync_io)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100545 flags |= O_SYNC;
Jens Axboe814452b2009-03-04 12:53:13 +0100546 if (td->o.create_on_open)
547 flags |= O_CREAT;
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200548skip_flags:
549 if (f->filetype != FIO_TYPE_FILE)
550 flags |= FIO_O_NOATIME;
Jens Axboe7abf8332006-11-23 15:01:19 +0100551
Aaron Carroll056f3452007-11-26 09:08:53 +0100552open_again:
Jens Axboe660a1cb2007-04-17 15:37:47 +0200553 if (td_write(td)) {
Jens Axboe17308152008-03-07 13:42:31 +0100554 if (!read_only)
555 flags |= O_RDWR;
Jens Axboe53cdc682006-10-18 11:50:58 +0200556
Jens Axboeaf52b342007-03-13 10:07:47 +0100557 if (f->filetype == FIO_TYPE_FILE)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100558 flags |= O_CREAT;
Jens Axboe2fd233b2007-03-02 08:44:25 +0100559
Jens Axboe66159822007-04-16 21:54:24 +0200560 if (is_std)
561 f->fd = dup(STDOUT_FILENO);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100562 else
563 from_hash = file_lookup_open(f, flags);
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200564 } else if (td_read(td)) {
Jens Axboe4241ea82007-09-12 08:18:36 +0200565 if (f->filetype == FIO_TYPE_CHAR && !read_only)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100566 flags |= O_RDWR;
567 else
568 flags |= O_RDONLY;
569
Jens Axboe66159822007-04-16 21:54:24 +0200570 if (is_std)
571 f->fd = dup(STDIN_FILENO);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100572 else
573 from_hash = file_lookup_open(f, flags);
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200574 } else { //td trim
575 flags |= O_RDWR;
576 from_hash = file_lookup_open(f, flags);
Jens Axboe53cdc682006-10-18 11:50:58 +0200577 }
578
579 if (f->fd == -1) {
Jens Axboee4e33252007-03-21 13:07:54 +0100580 char buf[FIO_VERROR_SIZE];
Jens Axboee1161c32007-02-22 19:36:48 +0100581 int __e = errno;
582
Jens Axboe835d9b92009-06-09 15:23:38 +0200583 if (__e == EPERM && (flags & FIO_O_NOATIME)) {
Jens Axboe5921e802008-05-30 15:02:38 +0200584 flags &= ~FIO_O_NOATIME;
Aaron Carroll056f3452007-11-26 09:08:53 +0100585 goto open_again;
586 }
Jens Axboee6c4d732012-12-12 08:16:27 +0100587 if (__e == EMFILE && file_close_shadow_fds(td))
588 goto open_again;
Aaron Carroll056f3452007-11-26 09:08:53 +0100589
Ken Raeburn98ffb8f2013-01-30 22:31:09 +0100590 snprintf(buf, sizeof(buf), "open(%s)", f->file_name);
Jens Axboee4e33252007-03-21 13:07:54 +0100591
Jens Axboea93c5f02012-06-11 08:53:53 +0200592 if (__e == EINVAL && (flags & OS_O_DIRECT)) {
593 log_err("fio: looks like your file system does not " \
594 "support direct=1/buffered=0\n");
595 }
596
Jens Axboee4e33252007-03-21 13:07:54 +0100597 td_verror(td, __e, buf);
Jens Axboe53cdc682006-10-18 11:50:58 +0200598 }
599
Jens Axboe29c13492008-03-01 19:25:20 +0100600 if (!from_hash && f->fd != -1) {
601 if (add_file_hash(f)) {
Jens Axboe3f0ca9b2011-05-24 21:36:24 +0200602 int fio_unused ret;
Jens Axboe29c13492008-03-01 19:25:20 +0100603
604 /*
Jens Axboee6c4d732012-12-12 08:16:27 +0100605 * Stash away descriptor for later close. This is to
606 * work-around a "feature" on Linux, where a close of
607 * an fd that has been opened for write will trigger
608 * udev to call blkid to check partitions, fs id, etc.
Anatol Pomozovde8f6de2013-09-26 16:31:34 -0700609 * That pollutes the device cache, which can slow down
Jens Axboee6c4d732012-12-12 08:16:27 +0100610 * unbuffered accesses.
Jens Axboe29c13492008-03-01 19:25:20 +0100611 */
Jens Axboee6c4d732012-12-12 08:16:27 +0100612 if (f->shadow_fd == -1)
613 f->shadow_fd = f->fd;
614 else {
615 /*
616 * OK to ignore, we haven't done anything
617 * with it
618 */
619 ret = generic_close_file(td, f);
620 }
Jens Axboe29c13492008-03-01 19:25:20 +0100621 goto open_again;
622 }
623 }
Jens Axboe4906e0b2008-03-01 18:59:51 +0100624
Jens Axboe53cdc682006-10-18 11:50:58 +0200625 return 0;
Jens Axboeb5af8292007-03-08 12:43:13 +0100626}
627
Jens Axboedf9c26b2009-03-05 10:13:58 +0100628int generic_get_file_size(struct thread_data *td, struct fio_file *f)
Jens Axboe21972cd2006-11-23 12:39:16 +0100629{
Jens Axboedf9c26b2009-03-05 10:13:58 +0100630 return get_file_size(td, f);
Jens Axboe21972cd2006-11-23 12:39:16 +0100631}
632
Jens Axboe7bb48f82007-03-27 15:30:28 +0200633/*
634 * open/close all files, so that ->real_file_size gets set
635 */
Jens Axboebab3fd52007-04-02 10:34:18 +0200636static int get_file_sizes(struct thread_data *td)
Jens Axboe53cdc682006-10-18 11:50:58 +0200637{
638 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100639 unsigned int i;
Jens Axboebab3fd52007-04-02 10:34:18 +0200640 int err = 0;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200641
642 for_each_file(td, f, i) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100643 dprint(FD_FILE, "get file size for %p/%d/%p\n", f, i,
644 f->file_name);
Jens Axboe9efef3c2008-03-06 10:26:25 +0100645
Jens Axboe99a47c62009-04-07 22:20:56 +0200646 if (td_io_get_file_size(td, f)) {
Jens Axboe40b44f42007-04-11 12:43:35 +0200647 if (td->error != ENOENT) {
648 log_err("%s\n", td->verror);
649 err = 1;
650 }
Jens Axboe541d66d2007-03-27 19:50:11 +0200651 clear_error(td);
Jens Axboe07eb79d2007-04-12 13:02:38 +0200652 }
Jens Axboe409b3412007-03-28 09:57:01 +0200653
654 if (f->real_file_size == -1ULL && td->o.size)
655 f->real_file_size = td->o.size / td->o.nr_files;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200656 }
Jens Axboebab3fd52007-04-02 10:34:18 +0200657
658 return err;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200659}
660
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200661struct fio_mount {
662 struct flist_head list;
663 const char *base;
664 char __base[256];
665 unsigned int key;
666};
667
668/*
669 * Get free number of bytes for each file on each unique mount.
670 */
671static unsigned long long get_fs_free_counts(struct thread_data *td)
672{
673 struct flist_head *n, *tmp;
Jens Axboe68b03162010-06-24 09:22:41 +0200674 unsigned long long ret = 0;
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200675 struct fio_mount *fm;
676 FLIST_HEAD(list);
677 struct fio_file *f;
678 unsigned int i;
679
680 for_each_file(td, f, i) {
681 struct stat sb;
682 char buf[256];
683
Jens Axboe4ccdccd2010-06-24 10:27:22 +0200684 if (f->filetype == FIO_TYPE_BD || f->filetype == FIO_TYPE_CHAR) {
685 if (f->real_file_size != -1ULL)
686 ret += f->real_file_size;
Jens Axboe68b03162010-06-24 09:22:41 +0200687 continue;
688 } else if (f->filetype != FIO_TYPE_FILE)
689 continue;
690
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200691 strcpy(buf, f->file_name);
692
693 if (stat(buf, &sb) < 0) {
694 if (errno != ENOENT)
695 break;
696 strcpy(buf, ".");
697 if (stat(buf, &sb) < 0)
698 break;
699 }
700
701 fm = NULL;
702 flist_for_each(n, &list) {
703 fm = flist_entry(n, struct fio_mount, list);
704 if (fm->key == sb.st_dev)
705 break;
706
707 fm = NULL;
708 }
709
710 if (fm)
711 continue;
712
713 fm = malloc(sizeof(*fm));
714 strcpy(fm->__base, buf);
715 fm->base = basename(fm->__base);
716 fm->key = sb.st_dev;
717 flist_add(&fm->list, &list);
718 }
719
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200720 flist_for_each_safe(n, tmp, &list) {
721 unsigned long long sz;
722
723 fm = flist_entry(n, struct fio_mount, list);
724 flist_del(&fm->list);
725
726 sz = get_fs_size(fm->base);
727 if (sz && sz != -1ULL)
728 ret += sz;
729
730 free(fm);
731 }
732
733 return ret;
734}
735
Jens Axboebedc9dc2014-03-17 12:51:09 -0600736uint64_t get_start_offset(struct thread_data *td, struct fio_file *f)
Dan Ehrenbergce95d652012-08-16 08:58:21 +0200737{
Jens Axboebedc9dc2014-03-17 12:51:09 -0600738 struct thread_options *o = &td->o;
739
740 if (o->file_append && f->filetype == FIO_TYPE_FILE)
741 return f->real_file_size;
742
Dan Ehrenbergce95d652012-08-16 08:58:21 +0200743 return td->o.start_offset +
744 (td->thread_number - 1) * td->o.offset_increment;
745}
746
Jens Axboe7bb48f82007-03-27 15:30:28 +0200747/*
748 * Open the files and setup files sizes, creating files if necessary.
749 */
750int setup_files(struct thread_data *td)
751{
752 unsigned long long total_size, extend_size;
Jens Axboede98bd32013-04-05 11:09:20 +0200753 struct thread_options *o = &td->o;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200754 struct fio_file *f;
Jens Axboe002fe732014-02-11 08:31:13 -0700755 unsigned int i, nr_fs_extra = 0;
Jens Axboe000b0802007-03-27 15:56:10 +0200756 int err = 0, need_extend;
Jens Axboee90391e2013-04-13 20:40:53 +0200757 int old_state;
Jens Axboe002fe732014-02-11 08:31:13 -0700758 const unsigned int bs = td_min_bs(td);
759 uint64_t fs = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200760
Jens Axboeee56ad52008-02-01 10:30:20 +0100761 dprint(FD_FILE, "setup files\n");
762
Jens Axboe8edd9732014-03-01 08:24:03 -0700763 old_state = td_bump_runstate(td, TD_SETTING_UP);
Jens Axboee90391e2013-04-13 20:40:53 +0200764
Jens Axboede98bd32013-04-05 11:09:20 +0200765 if (o->read_iolog_file)
Jens Axboe25460cf2012-05-02 13:58:02 +0200766 goto done;
Jens Axboe691c8fb2008-03-07 14:26:26 +0100767
Jens Axboe53cdc682006-10-18 11:50:58 +0200768 /*
769 * if ioengine defines a setup() method, it's responsible for
Jens Axboe7bb48f82007-03-27 15:30:28 +0200770 * opening the files and setting f->real_file_size to indicate
771 * the valid range for that file.
Jens Axboe53cdc682006-10-18 11:50:58 +0200772 */
773 if (td->io_ops->setup)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200774 err = td->io_ops->setup(td);
775 else
Jens Axboebab3fd52007-04-02 10:34:18 +0200776 err = get_file_sizes(td);
Jens Axboe53cdc682006-10-18 11:50:58 +0200777
Jens Axboef1027062006-10-20 10:14:01 +0200778 if (err)
Jens Axboee90391e2013-04-13 20:40:53 +0200779 goto err_out;
Jens Axboef1027062006-10-20 10:14:01 +0200780
Jens Axboe0a7eb122006-10-20 10:36:42 +0200781 /*
Jens Axboe7bb48f82007-03-27 15:30:28 +0200782 * check sizes. if the files/devices do not exist and the size
783 * isn't passed to fio, abort.
Jens Axboe0a7eb122006-10-20 10:36:42 +0200784 */
Jens Axboe7bb48f82007-03-27 15:30:28 +0200785 total_size = 0;
786 for_each_file(td, f, i) {
787 if (f->real_file_size == -1ULL)
788 total_size = -1ULL;
789 else
790 total_size += f->real_file_size;
791 }
Jens Axboe0a7eb122006-10-20 10:36:42 +0200792
Jens Axboede98bd32013-04-05 11:09:20 +0200793 if (o->fill_device)
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200794 td->fill_device_size = get_fs_free_counts(td);
795
Jens Axboe7bb48f82007-03-27 15:30:28 +0200796 /*
797 * device/file sizes are zero and no size given, punt
798 */
Jens Axboede98bd32013-04-05 11:09:20 +0200799 if ((!total_size || total_size == -1ULL) && !o->size &&
800 !(td->io_ops->flags & FIO_NOIO) && !o->fill_device &&
801 !(o->nr_files && (o->file_size_low || o->file_size_high))) {
802 log_err("%s: you need to specify size=\n", o->name);
Jens Axboee1161c32007-02-22 19:36:48 +0100803 td_verror(td, EINVAL, "total_file_size");
Jens Axboee90391e2013-04-13 20:40:53 +0200804 goto err_out;
Jens Axboe53cdc682006-10-18 11:50:58 +0200805 }
806
Jens Axboe7bb48f82007-03-27 15:30:28 +0200807 /*
Jens Axboe002fe732014-02-11 08:31:13 -0700808 * Calculate per-file size and potential extra size for the
809 * first files, if needed.
810 */
Jens Axboed1e78e62014-04-06 09:56:10 -0600811 if (!o->file_size_low && o->nr_files) {
Jens Axboe002fe732014-02-11 08:31:13 -0700812 uint64_t all_fs;
813
814 fs = o->size / o->nr_files;
815 all_fs = fs * o->nr_files;
816
817 if (all_fs < o->size)
818 nr_fs_extra = (o->size - all_fs) / bs;
819 }
820
821 /*
Jens Axboe7bb48f82007-03-27 15:30:28 +0200822 * now file sizes are known, so we can set ->io_size. if size= is
823 * not given, ->io_size is just equal to ->real_file_size. if size
824 * is given, ->io_size is size / nr_files.
825 */
826 extend_size = total_size = 0;
827 need_extend = 0;
828 for_each_file(td, f, i) {
Jens Axboebedc9dc2014-03-17 12:51:09 -0600829 f->file_offset = get_start_offset(td, f);
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200830
Jens Axboede98bd32013-04-05 11:09:20 +0200831 if (!o->file_size_low) {
Jens Axboe7bb48f82007-03-27 15:30:28 +0200832 /*
833 * no file size range given, file size is equal to
Jens Axboe002fe732014-02-11 08:31:13 -0700834 * total size divided by number of files. If that is
835 * zero, set it to the real file size. If the size
836 * doesn't divide nicely with the min blocksize,
837 * make the first files bigger.
Jens Axboe7bb48f82007-03-27 15:30:28 +0200838 */
Jens Axboe002fe732014-02-11 08:31:13 -0700839 f->io_size = fs;
840 if (nr_fs_extra) {
841 nr_fs_extra--;
842 f->io_size += bs;
843 }
844
Jens Axboe65bdb102008-01-24 13:13:12 +0100845 if (!f->io_size)
Jens Axboe273f8c92008-01-25 14:02:15 +0100846 f->io_size = f->real_file_size - f->file_offset;
Jens Axboede98bd32013-04-05 11:09:20 +0200847 } else if (f->real_file_size < o->file_size_low ||
848 f->real_file_size > o->file_size_high) {
849 if (f->file_offset > o->file_size_low)
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200850 goto err_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200851 /*
852 * file size given. if it's fixed, use that. if it's a
853 * range, generate a random size in-between.
854 */
Jens Axboede98bd32013-04-05 11:09:20 +0200855 if (o->file_size_low == o->file_size_high)
856 f->io_size = o->file_size_low - f->file_offset;
857 else {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100858 f->io_size = get_rand_file_size(td)
859 - f->file_offset;
860 }
Jens Axboe65bdb102008-01-24 13:13:12 +0100861 } else
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200862 f->io_size = f->real_file_size - f->file_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200863
864 if (f->io_size == -1ULL)
865 total_size = -1ULL;
Shaohua Li4d002562012-09-21 08:32:32 +0200866 else {
Jens Axboede98bd32013-04-05 11:09:20 +0200867 if (o->size_percent)
868 f->io_size = (f->io_size * o->size_percent) / 100;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200869 total_size += f->io_size;
Shaohua Li4d002562012-09-21 08:32:32 +0200870 }
Jens Axboe7bb48f82007-03-27 15:30:28 +0200871
872 if (f->filetype == FIO_TYPE_FILE &&
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200873 (f->io_size + f->file_offset) > f->real_file_size &&
Jens Axboe7bb48f82007-03-27 15:30:28 +0200874 !(td->io_ops->flags & FIO_DISKLESSIO)) {
Jens Axboede98bd32013-04-05 11:09:20 +0200875 if (!o->create_on_open) {
Jens Axboe814452b2009-03-04 12:53:13 +0100876 need_extend++;
877 extend_size += (f->io_size + f->file_offset);
878 } else
879 f->real_file_size = f->io_size + f->file_offset;
Jens Axboed6aed792009-06-03 08:41:15 +0200880 fio_file_set_extend(f);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100881 }
Jens Axboe7bb48f82007-03-27 15:30:28 +0200882 }
883
Jens Axboede98bd32013-04-05 11:09:20 +0200884 if (!o->size || o->size > total_size)
885 o->size = total_size;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200886
887 /*
888 * See if we need to extend some files
889 */
890 if (need_extend) {
891 temp_stall_ts = 1;
Jens Axboef3afa572012-09-17 13:34:16 +0200892 if (output_format == FIO_OUTPUT_NORMAL)
Shaozhi Shawn Yea7ba8c52008-09-10 09:04:18 +0200893 log_info("%s: Laying out IO file(s) (%u file(s) /"
Jens Axboede98bd32013-04-05 11:09:20 +0200894 " %lluMB)\n", o->name, need_extend,
Shaozhi Shawn Yea7ba8c52008-09-10 09:04:18 +0200895 extend_size >> 20);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200896
897 for_each_file(td, f, i) {
Jens Axboe5e0074c2009-06-02 21:40:09 +0200898 unsigned long long old_len = -1ULL, extend_len = -1ULL;
Jens Axboe3baddf22008-04-04 11:10:30 +0200899
Jens Axboed6aed792009-06-03 08:41:15 +0200900 if (!fio_file_extend(f))
Jens Axboe7bb48f82007-03-27 15:30:28 +0200901 continue;
902
Jens Axboe409b3412007-03-28 09:57:01 +0200903 assert(f->filetype == FIO_TYPE_FILE);
Jens Axboed6aed792009-06-03 08:41:15 +0200904 fio_file_clear_extend(f);
Jens Axboede98bd32013-04-05 11:09:20 +0200905 if (!o->fill_device) {
Jens Axboe5e0074c2009-06-02 21:40:09 +0200906 old_len = f->real_file_size;
Jens Axboe0b9d69e2009-09-11 22:29:54 +0200907 extend_len = f->io_size + f->file_offset -
908 old_len;
Jens Axboe5e0074c2009-06-02 21:40:09 +0200909 }
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200910 f->real_file_size = (f->io_size + f->file_offset);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200911 err = extend_file(td, f);
912 if (err)
913 break;
Jens Axboe5e0074c2009-06-02 21:40:09 +0200914
Jens Axboe3baddf22008-04-04 11:10:30 +0200915 err = __file_invalidate_cache(td, f, old_len,
916 extend_len);
917 close(f->fd);
918 f->fd = -1;
919 if (err)
920 break;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200921 }
922 temp_stall_ts = 0;
923 }
924
925 if (err)
Jens Axboee90391e2013-04-13 20:40:53 +0200926 goto err_out;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200927
Jens Axboede98bd32013-04-05 11:09:20 +0200928 if (!o->zone_size)
929 o->zone_size = o->size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200930
Jens Axboeea966f82007-09-03 10:09:37 +0200931 /*
932 * iolog already set the total io size, if we read back
933 * stored entries.
934 */
Jens Axboede98bd32013-04-05 11:09:20 +0200935 if (!o->read_iolog_file)
936 td->total_io_size = o->size * o->loops;
Jens Axboe25460cf2012-05-02 13:58:02 +0200937
938done:
Jens Axboede98bd32013-04-05 11:09:20 +0200939 if (o->create_only)
Jens Axboe25460cf2012-05-02 13:58:02 +0200940 td->done = 1;
941
Jens Axboe8edd9732014-03-01 08:24:03 -0700942 td_restore_runstate(td, old_state);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200943 return 0;
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200944err_offset:
Jens Axboede98bd32013-04-05 11:09:20 +0200945 log_err("%s: you need to specify valid offset=\n", o->name);
Jens Axboee90391e2013-04-13 20:40:53 +0200946err_out:
Jens Axboe8edd9732014-03-01 08:24:03 -0700947 td_restore_runstate(td, old_state);
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200948 return 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200949}
950
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200951int pre_read_files(struct thread_data *td)
952{
953 struct fio_file *f;
954 unsigned int i;
955
956 dprint(FD_FILE, "pre_read files\n");
957
958 for_each_file(td, f, i) {
959 pre_read_file(td, f);
960 }
961
962 return 1;
963}
964
Jens Axboe9c6f6312012-11-07 09:15:45 +0100965static int __init_rand_distribution(struct thread_data *td, struct fio_file *f)
966{
Jens Axboe23162962012-11-07 19:47:47 +0100967 unsigned int range_size, seed;
Jens Axboe9c6f6312012-11-07 09:15:45 +0100968 unsigned long nranges;
Jens Axboe21415db2013-01-04 13:09:29 +0100969 uint64_t file_size;
Jens Axboe9c6f6312012-11-07 09:15:45 +0100970
971 range_size = min(td->o.min_bs[DDIR_READ], td->o.min_bs[DDIR_WRITE]);
Jens Axboe21415db2013-01-04 13:09:29 +0100972 file_size = min(f->real_file_size, f->io_size);
Jens Axboe9c6f6312012-11-07 09:15:45 +0100973
Jens Axboe21415db2013-01-04 13:09:29 +0100974 nranges = (file_size + range_size - 1) / range_size;
Jens Axboe9c6f6312012-11-07 09:15:45 +0100975
Jens Axboe23162962012-11-07 19:47:47 +0100976 seed = jhash(f->file_name, strlen(f->file_name), 0) * td->thread_number;
Jens Axboe84256872012-11-17 10:06:36 -0700977 if (!td->o.rand_repeatable)
978 seed = td->rand_seeds[4];
979
Jens Axboe9c6f6312012-11-07 09:15:45 +0100980 if (td->o.random_distribution == FIO_RAND_DIST_ZIPF)
Jens Axboe888677a2013-04-10 22:19:46 +0200981 zipf_init(&f->zipf, nranges, td->o.zipf_theta.u.f, seed);
Jens Axboe9c6f6312012-11-07 09:15:45 +0100982 else
Jens Axboe888677a2013-04-10 22:19:46 +0200983 pareto_init(&f->zipf, nranges, td->o.pareto_h.u.f, seed);
Jens Axboe9c6f6312012-11-07 09:15:45 +0100984
985 return 1;
986}
987
988static int init_rand_distribution(struct thread_data *td)
989{
990 struct fio_file *f;
991 unsigned int i;
992 int state;
993
994 if (td->o.random_distribution == FIO_RAND_DIST_RANDOM)
995 return 0;
996
Jens Axboe8edd9732014-03-01 08:24:03 -0700997 state = td_bump_runstate(td, TD_SETTING_UP);
998
Jens Axboe9c6f6312012-11-07 09:15:45 +0100999 for_each_file(td, f, i)
1000 __init_rand_distribution(td, f);
Jens Axboe8edd9732014-03-01 08:24:03 -07001001
1002 td_restore_runstate(td, state);
Jens Axboe9c6f6312012-11-07 09:15:45 +01001003
1004 return 1;
1005}
1006
Jens Axboe68727072007-03-15 20:49:25 +01001007int init_random_map(struct thread_data *td)
1008{
Jens Axboe51ede0b2012-11-22 13:50:29 +01001009 unsigned long long blocks;
Jens Axboe68727072007-03-15 20:49:25 +01001010 struct fio_file *f;
1011 unsigned int i;
1012
Jens Axboe9c6f6312012-11-07 09:15:45 +01001013 if (init_rand_distribution(td))
1014 return 0;
Jens Axboe3831a842012-11-26 19:59:14 +01001015 if (!td_random(td))
Jens Axboe68727072007-03-15 20:49:25 +01001016 return 0;
1017
1018 for_each_file(td, f, i) {
Jens Axboe38f30c82013-01-11 14:03:05 +01001019 uint64_t file_size = min(f->real_file_size, f->io_size);
1020
Jens Axboe53737ae2013-02-21 15:18:17 +01001021 blocks = file_size / (unsigned long long) td->o.rw_min_bs;
1022
Jens Axboe8055e412012-11-26 08:43:47 +01001023 if (td->o.random_generator == FIO_RAND_GEN_LFSR) {
Jens Axboe82af46b2012-12-04 13:12:25 +01001024 unsigned long seed;
1025
1026 seed = td->rand_seeds[FIO_RAND_BLOCK_OFF];
1027
Jens Axboe225ba9e2014-02-26 14:31:15 -08001028 if (!lfsr_init(&f->lfsr, blocks, seed, 0))
Jens Axboe8055e412012-11-26 08:43:47 +01001029 continue;
Jens Axboe3831a842012-11-26 19:59:14 +01001030 } else if (!td->o.norandommap) {
Jens Axboe7ebd7962012-11-28 21:24:46 +01001031 f->io_axmap = axmap_new(blocks);
1032 if (f->io_axmap)
Jens Axboe8055e412012-11-26 08:43:47 +01001033 continue;
Jens Axboe1cad7122012-11-29 21:37:11 +01001034 } else if (td->o.norandommap)
1035 continue;
Jens Axboeceadd592012-02-02 08:41:28 +01001036
Jens Axboe2b386d22008-03-26 10:32:57 +01001037 if (!td->o.softrandommap) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001038 log_err("fio: failed allocating random map. If running"
1039 " a large number of jobs, try the 'norandommap'"
Jens Axboe2b386d22008-03-26 10:32:57 +01001040 " option or set 'softrandommap'. Or give"
1041 " a larger --alloc-size to fio.\n");
Jens Axboe68727072007-03-15 20:49:25 +01001042 return 1;
1043 }
Jens Axboe303032a2008-03-26 10:11:10 +01001044
1045 log_info("fio: file %s failed allocating random map. Running "
1046 "job without.\n", f->file_name);
Jens Axboe68727072007-03-15 20:49:25 +01001047 }
1048
1049 return 0;
1050}
1051
Jens Axboe53cdc682006-10-18 11:50:58 +02001052void close_files(struct thread_data *td)
1053{
Jens Axboe0ab8db82006-10-18 17:16:23 +02001054 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +01001055 unsigned int i;
Jens Axboe53cdc682006-10-18 11:50:58 +02001056
Jens Axboe2be3eec2009-06-10 09:05:13 +02001057 for_each_file(td, f, i) {
1058 if (fio_file_open(f))
1059 td_io_close_file(td, f);
1060 }
Jens Axboe24ffd2c2008-03-01 15:47:08 +01001061}
1062
1063void close_and_free_files(struct thread_data *td)
1064{
1065 struct fio_file *f;
1066 unsigned int i;
1067
Jens Axboeee56ad52008-02-01 10:30:20 +01001068 dprint(FD_FILE, "close files\n");
1069
Jens Axboe0ab8db82006-10-18 17:16:23 +02001070 for_each_file(td, f, i) {
Jens Axboe22a57ba2009-06-09 14:34:35 +02001071 if (fio_file_open(f))
1072 td_io_close_file(td, f);
1073
Shaozhi Shawn Yeb9fbcf22008-09-10 09:05:17 +02001074 remove_file_hash(f);
Jens Axboeb3dc7f02007-03-09 12:52:15 +01001075
Jens Axboeb5f4d8b2014-03-20 08:28:11 -06001076 if (td->o.unlink && f->filetype == FIO_TYPE_FILE) {
1077 dprint(FD_FILE, "free unlink %s\n", f->file_name);
1078 unlink(f->file_name);
1079 }
1080
Jens Axboef17c4392008-03-01 18:40:46 +01001081 sfree(f->file_name);
Jens Axboefa1da862007-03-23 15:20:54 +01001082 f->file_name = NULL;
Jens Axboe7ebd7962012-11-28 21:24:46 +01001083 axmap_free(f->io_axmap);
1084 f->io_axmap = NULL;
Jens Axboe78d99e62008-03-01 18:41:50 +01001085 sfree(f);
Jens Axboe53cdc682006-10-18 11:50:58 +02001086 }
Jens Axboeb4a6a592006-10-20 13:54:47 +02001087
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001088 td->o.filename = NULL;
Jens Axboecade3ef2007-03-23 15:29:45 +01001089 free(td->files);
Jens Axboed7df1d12013-03-20 19:57:01 -06001090 free(td->file_locks);
Jens Axboe9efef3c2008-03-06 10:26:25 +01001091 td->files_index = 0;
Jens Axboeb4a6a592006-10-20 13:54:47 +02001092 td->files = NULL;
Jens Axboed7df1d12013-03-20 19:57:01 -06001093 td->file_locks = NULL;
Jens Axboe27ddbfa2014-01-22 16:27:32 -08001094 td->o.file_lock_mode = FILE_LOCK_NONE;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001095 td->o.nr_files = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +02001096}
Jens Axboeaf52b342007-03-13 10:07:47 +01001097
Jens Axboee3bab462007-03-13 11:22:09 +01001098static void get_file_type(struct fio_file *f)
Jens Axboeaf52b342007-03-13 10:07:47 +01001099{
1100 struct stat sb;
1101
Jens Axboe66159822007-04-16 21:54:24 +02001102 if (!strcmp(f->file_name, "-"))
1103 f->filetype = FIO_TYPE_PIPE;
1104 else
1105 f->filetype = FIO_TYPE_FILE;
Jens Axboeaf52b342007-03-13 10:07:47 +01001106
Bruce Cran38921822012-02-22 17:55:16 +00001107 /* \\.\ is the device namespace in Windows, where every file is
1108 * a block device */
1109 if (strncmp(f->file_name, "\\\\.\\", 4) == 0)
1110 f->filetype = FIO_TYPE_BD;
1111
Jens Axboeb30d3952010-02-06 23:36:26 +01001112 if (!stat(f->file_name, &sb)) {
Bruce Cran38921822012-02-22 17:55:16 +00001113 if (S_ISBLK(sb.st_mode))
Jens Axboeaf52b342007-03-13 10:07:47 +01001114 f->filetype = FIO_TYPE_BD;
1115 else if (S_ISCHR(sb.st_mode))
1116 f->filetype = FIO_TYPE_CHAR;
Jens Axboeb5605e92007-04-16 20:42:33 +02001117 else if (S_ISFIFO(sb.st_mode))
1118 f->filetype = FIO_TYPE_PIPE;
Jens Axboeaf52b342007-03-13 10:07:47 +01001119 }
1120}
1121
Jens Axboe90426232014-04-01 12:28:47 -06001122static int __is_already_allocated(const char *fname)
1123{
1124 struct flist_head *entry;
1125 char *filename;
1126
1127 if (flist_empty(&filename_list))
1128 return 0;
1129
1130 flist_for_each(entry, &filename_list) {
1131 filename = flist_entry(entry, struct file_name, list)->filename;
1132
1133 if (strcmp(filename, fname) == 0)
1134 return 1;
1135 }
1136
1137 return 0;
1138}
1139
1140static int is_already_allocated(const char *fname)
1141{
1142 int ret;
1143
1144 fio_file_hash_lock();
1145 ret = __is_already_allocated(fname);
1146 fio_file_hash_unlock();
1147 return ret;
1148}
1149
Castor Fu190b8f02014-03-20 10:59:29 -07001150static void set_already_allocated(const char *fname)
1151{
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08001152 struct file_name *fn;
1153
1154 fn = malloc(sizeof(struct file_name));
1155 fn->filename = strdup(fname);
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08001156
Jens Axboe90426232014-04-01 12:28:47 -06001157 fio_file_hash_lock();
1158 if (!__is_already_allocated(fname)) {
1159 flist_add_tail(&fn->list, &filename_list);
1160 fn = NULL;
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08001161 }
Jens Axboe90426232014-04-01 12:28:47 -06001162 fio_file_hash_unlock();
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08001163
Jens Axboe90426232014-04-01 12:28:47 -06001164 if (fn) {
1165 free(fn->filename);
1166 free(fn);
1167 }
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08001168}
1169
Jens Axboe90426232014-04-01 12:28:47 -06001170
Castor Fu190b8f02014-03-20 10:59:29 -07001171static void free_already_allocated(void)
1172{
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08001173 struct flist_head *entry, *tmp;
1174 struct file_name *fn;
1175
Jens Axboe90426232014-04-01 12:28:47 -06001176 if (flist_empty(&filename_list))
1177 return;
1178
1179 fio_file_hash_lock();
1180 flist_for_each_safe(entry, tmp, &filename_list) {
1181 fn = flist_entry(entry, struct file_name, list);
1182 free(fn->filename);
1183 flist_del(&fn->list);
1184 free(fn);
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08001185 }
Jens Axboe90426232014-04-01 12:28:47 -06001186
1187 fio_file_hash_unlock();
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08001188}
1189
Jens Axboe7b5cb702014-04-01 16:37:03 -06001190static struct fio_file *alloc_new_file(struct thread_data *td)
1191{
1192 struct fio_file *f;
1193
1194 f = smalloc(sizeof(*f));
1195 if (!f) {
1196 log_err("fio: smalloc OOM\n");
1197 assert(0);
1198 return NULL;
1199 }
1200
1201 f->fd = -1;
1202 f->shadow_fd = -1;
1203 fio_file_reset(td, f);
1204 return f;
1205}
1206
Jens Axboe5903e7b2014-02-26 13:42:13 -08001207int add_file(struct thread_data *td, const char *fname, int numjob, int inc)
Jens Axboeaf52b342007-03-13 10:07:47 +01001208{
Jens Axboe7b4e4fe2007-03-13 12:51:40 +01001209 int cur_files = td->files_index;
Jens Axboebd0ee742007-03-23 14:26:23 +01001210 char file_name[PATH_MAX];
Jens Axboeaf52b342007-03-13 10:07:47 +01001211 struct fio_file *f;
Jens Axboebd0ee742007-03-23 14:26:23 +01001212 int len = 0;
Jens Axboeaf52b342007-03-13 10:07:47 +01001213
Jens Axboeee56ad52008-02-01 10:30:20 +01001214 dprint(FD_FILE, "add file %s\n", fname);
1215
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08001216 if (td->o.directory)
1217 len = set_name_idx(file_name, td->o.directory, numjob);
1218
1219 sprintf(file_name + len, "%s", fname);
1220
1221 /* clean cloned siblings using existing files */
1222 if (numjob && is_already_allocated(file_name))
1223 return 0;
1224
Jens Axboe7b5cb702014-04-01 16:37:03 -06001225 f = alloc_new_file(td);
Jens Axboebd0ee742007-03-23 14:26:23 +01001226
Jens Axboefc99bc02009-03-04 15:27:42 +01001227 if (td->files_size <= td->files_index) {
Jianpeng Ma1983e322013-01-10 13:19:27 +01001228 unsigned int new_size = td->o.nr_files + 1;
Jens Axboe126d65c2008-03-01 18:04:31 +01001229
Jens Axboefc99bc02009-03-04 15:27:42 +01001230 dprint(FD_FILE, "resize file array to %d files\n", new_size);
1231
1232 td->files = realloc(td->files, new_size * sizeof(f));
Jianpeng Mad537c082013-01-11 08:52:55 +01001233 if (td->files == NULL) {
1234 log_err("fio: realloc OOM\n");
1235 assert(0);
1236 }
Jens Axboed7df1d12013-03-20 19:57:01 -06001237 if (td->o.file_lock_mode != FILE_LOCK_NONE) {
1238 td->file_locks = realloc(td->file_locks, new_size);
1239 if (!td->file_locks) {
1240 log_err("fio: realloc OOM\n");
1241 assert(0);
1242 }
1243 td->file_locks[cur_files] = FILE_LOCK_NONE;
1244 }
Jens Axboefc99bc02009-03-04 15:27:42 +01001245 td->files_size = new_size;
1246 }
Jens Axboe8bb76792009-03-04 15:37:52 +01001247 td->files[cur_files] = f;
Shaohua Li89ac1d42012-06-11 08:56:32 +02001248 f->fileno = cur_files;
Jens Axboe126d65c2008-03-01 18:04:31 +01001249
Jens Axboe07eb79d2007-04-12 13:02:38 +02001250 /*
1251 * init function, io engine may not be loaded yet
1252 */
1253 if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO))
1254 f->real_file_size = -1ULL;
1255
Jens Axboef17c4392008-03-01 18:40:46 +01001256 f->file_name = smalloc_strdup(file_name);
Jens Axboec48c0be2008-07-31 19:55:02 +02001257 if (!f->file_name) {
1258 log_err("fio: smalloc OOM\n");
1259 assert(0);
1260 }
Jens Axboe0b9d69e2009-09-11 22:29:54 +02001261
Jens Axboee3bab462007-03-13 11:22:09 +01001262 get_file_type(f);
Jens Axboeaf52b342007-03-13 10:07:47 +01001263
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001264 switch (td->o.file_lock_mode) {
1265 case FILE_LOCK_NONE:
1266 break;
1267 case FILE_LOCK_READWRITE:
Jens Axboed7df1d12013-03-20 19:57:01 -06001268 f->rwlock = fio_rwlock_init();
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001269 break;
1270 case FILE_LOCK_EXCLUSIVE:
Jens Axboe521da522012-08-02 11:21:36 +02001271 f->lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001272 break;
1273 default:
1274 log_err("fio: unknown lock mode: %d\n", td->o.file_lock_mode);
1275 assert(0);
1276 }
Jens Axboe29c13492008-03-01 19:25:20 +01001277
Jens Axboe7b4e4fe2007-03-13 12:51:40 +01001278 td->files_index++;
Jens Axboe15494412007-03-13 11:52:47 +01001279 if (f->filetype == FIO_TYPE_FILE)
1280 td->nr_normal_files++;
Jens Axboef29b25a2007-07-23 08:56:43 +02001281
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08001282 set_already_allocated(file_name);
1283
Jens Axboe5903e7b2014-02-26 13:42:13 -08001284 if (inc)
1285 td->o.nr_files++;
1286
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001287 dprint(FD_FILE, "file %p \"%s\" added at %d\n", f, f->file_name,
1288 cur_files);
Jens Axboe9efef3c2008-03-06 10:26:25 +01001289
Jens Axboef29b25a2007-07-23 08:56:43 +02001290 return cur_files;
Jens Axboeaf52b342007-03-13 10:07:47 +01001291}
Jens Axboe0ad920e2007-03-13 11:06:45 +01001292
Jens Axboe49ffb4a2010-08-24 15:01:37 +02001293int add_file_exclusive(struct thread_data *td, const char *fname)
1294{
1295 struct fio_file *f;
1296 unsigned int i;
1297
1298 for_each_file(td, f, i) {
1299 if (!strcmp(f->file_name, fname))
1300 return i;
1301 }
1302
Jens Axboe5903e7b2014-02-26 13:42:13 -08001303 return add_file(td, fname, 0, 1);
Jens Axboe49ffb4a2010-08-24 15:01:37 +02001304}
1305
Jens Axboe0ad920e2007-03-13 11:06:45 +01001306void get_file(struct fio_file *f)
1307{
Jens Axboe8172fe92008-02-01 21:51:02 +01001308 dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references);
Jens Axboed6aed792009-06-03 08:41:15 +02001309 assert(fio_file_open(f));
Jens Axboe0ad920e2007-03-13 11:06:45 +01001310 f->references++;
1311}
1312
Jens Axboe6977bcd2008-03-01 15:55:36 +01001313int put_file(struct thread_data *td, struct fio_file *f)
Jens Axboe0ad920e2007-03-13 11:06:45 +01001314{
Jens Axboe98e1ac42008-03-06 11:56:48 +01001315 int f_ret = 0, ret = 0;
Jens Axboe6977bcd2008-03-01 15:55:36 +01001316
Jens Axboe8172fe92008-02-01 21:51:02 +01001317 dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references);
Jens Axboeee56ad52008-02-01 10:30:20 +01001318
Jens Axboe22a57ba2009-06-09 14:34:35 +02001319 if (!fio_file_open(f)) {
1320 assert(f->fd == -1);
Jens Axboe6977bcd2008-03-01 15:55:36 +01001321 return 0;
Jens Axboe22a57ba2009-06-09 14:34:35 +02001322 }
Jens Axboe0ad920e2007-03-13 11:06:45 +01001323
1324 assert(f->references);
1325 if (--f->references)
Jens Axboe6977bcd2008-03-01 15:55:36 +01001326 return 0;
Jens Axboe0ad920e2007-03-13 11:06:45 +01001327
Jens Axboed424d4d2007-04-17 09:05:10 +02001328 if (should_fsync(td) && td->o.fsync_on_close)
Jens Axboe98e1ac42008-03-06 11:56:48 +01001329 f_ret = fsync(f->fd);
Jens Axboeebb14152007-03-13 14:42:15 +01001330
Jens Axboe0ad920e2007-03-13 11:06:45 +01001331 if (td->io_ops->close_file)
Jens Axboe6977bcd2008-03-01 15:55:36 +01001332 ret = td->io_ops->close_file(td, f);
Jens Axboe1020a132007-03-29 11:15:06 +02001333
Jens Axboe98e1ac42008-03-06 11:56:48 +01001334 if (!ret)
Jens Axboea5fb4612008-05-28 10:54:01 +02001335 ret = f_ret;
Jens Axboe98e1ac42008-03-06 11:56:48 +01001336
Jens Axboe0ad920e2007-03-13 11:06:45 +01001337 td->nr_open_files--;
Jens Axboed6aed792009-06-03 08:41:15 +02001338 fio_file_clear_open(f);
Jens Axboe22a57ba2009-06-09 14:34:35 +02001339 assert(f->fd == -1);
Jens Axboe6977bcd2008-03-01 15:55:36 +01001340 return ret;
Jens Axboe0ad920e2007-03-13 11:06:45 +01001341}
Jens Axboebbf6b542007-03-13 15:28:55 +01001342
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001343void lock_file(struct thread_data *td, struct fio_file *f, enum fio_ddir ddir)
Jens Axboeb2bd2bd2008-03-01 18:19:52 +01001344{
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001345 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1346 return;
Jens Axboe29c13492008-03-01 19:25:20 +01001347
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001348 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
1349 if (ddir == DDIR_READ)
Jens Axboed7df1d12013-03-20 19:57:01 -06001350 fio_rwlock_read(f->rwlock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001351 else
Jens Axboed7df1d12013-03-20 19:57:01 -06001352 fio_rwlock_write(f->rwlock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001353 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1354 fio_mutex_down(f->lock);
1355
Jens Axboed7df1d12013-03-20 19:57:01 -06001356 td->file_locks[f->fileno] = td->o.file_lock_mode;
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001357}
1358
1359void unlock_file(struct thread_data *td, struct fio_file *f)
1360{
1361 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1362 return;
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001363
Jens Axboed7df1d12013-03-20 19:57:01 -06001364 if (td->o.file_lock_mode == FILE_LOCK_READWRITE)
1365 fio_rwlock_unlock(f->rwlock);
1366 else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001367 fio_mutex_up(f->lock);
Jens Axboed7df1d12013-03-20 19:57:01 -06001368
1369 td->file_locks[f->fileno] = FILE_LOCK_NONE;
Jens Axboeb2bd2bd2008-03-01 18:19:52 +01001370}
1371
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001372void unlock_file_all(struct thread_data *td, struct fio_file *f)
Jens Axboeb2bd2bd2008-03-01 18:19:52 +01001373{
Jens Axboef2a28032014-02-11 09:12:06 -07001374 if (td->o.file_lock_mode == FILE_LOCK_NONE || !td->file_locks)
Jens Axboe27ddbfa2014-01-22 16:27:32 -08001375 return;
Jens Axboed7df1d12013-03-20 19:57:01 -06001376 if (td->file_locks[f->fileno] != FILE_LOCK_NONE)
1377 unlock_file(td, f);
Jens Axboeb2bd2bd2008-03-01 18:19:52 +01001378}
1379
Jens Axboebbf6b542007-03-13 15:28:55 +01001380static int recurse_dir(struct thread_data *td, const char *dirname)
1381{
1382 struct dirent *dir;
1383 int ret = 0;
1384 DIR *D;
1385
1386 D = opendir(dirname);
1387 if (!D) {
Jens Axboe0ddb2702007-03-27 19:43:53 +02001388 char buf[FIO_VERROR_SIZE];
1389
Ken Raeburn98ffb8f2013-01-30 22:31:09 +01001390 snprintf(buf, FIO_VERROR_SIZE, "opendir(%s)", dirname);
Jens Axboe0ddb2702007-03-27 19:43:53 +02001391 td_verror(td, errno, buf);
Jens Axboebbf6b542007-03-13 15:28:55 +01001392 return 1;
1393 }
1394
1395 while ((dir = readdir(D)) != NULL) {
1396 char full_path[PATH_MAX];
1397 struct stat sb;
1398
Jens Axboee85b2b82007-03-14 09:39:06 +01001399 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
1400 continue;
Jens Axboe96d32d52007-03-14 09:16:09 +01001401
Bruce Cranb9fd7882012-02-20 17:01:46 +00001402 sprintf(full_path, "%s%s%s", dirname, FIO_OS_PATH_SEPARATOR, dir->d_name);
Jens Axboebbf6b542007-03-13 15:28:55 +01001403
1404 if (lstat(full_path, &sb) == -1) {
1405 if (errno != ENOENT) {
1406 td_verror(td, errno, "stat");
Jens Axboe68ace9e2014-04-11 11:24:00 -06001407 ret = 1;
1408 break;
Jens Axboebbf6b542007-03-13 15:28:55 +01001409 }
1410 }
1411
1412 if (S_ISREG(sb.st_mode)) {
Jens Axboe5903e7b2014-02-26 13:42:13 -08001413 add_file(td, full_path, 0, 1);
Jens Axboebbf6b542007-03-13 15:28:55 +01001414 continue;
1415 }
Jens Axboe0ddb2702007-03-27 19:43:53 +02001416 if (!S_ISDIR(sb.st_mode))
1417 continue;
Jens Axboebbf6b542007-03-13 15:28:55 +01001418
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001419 ret = recurse_dir(td, full_path);
1420 if (ret)
Jens Axboebbf6b542007-03-13 15:28:55 +01001421 break;
1422 }
1423
1424 closedir(D);
1425 return ret;
1426}
1427
1428int add_dir_files(struct thread_data *td, const char *path)
1429{
Jens Axboe0ddb2702007-03-27 19:43:53 +02001430 int ret = recurse_dir(td, path);
1431
1432 if (!ret)
1433 log_info("fio: opendir added %d files\n", td->o.nr_files);
1434
1435 return ret;
Jens Axboebbf6b542007-03-13 15:28:55 +01001436}
Jens Axboecade3ef2007-03-23 15:29:45 +01001437
1438void dup_files(struct thread_data *td, struct thread_data *org)
1439{
1440 struct fio_file *f;
1441 unsigned int i;
Jens Axboe9efef3c2008-03-06 10:26:25 +01001442
1443 dprint(FD_FILE, "dup files: %d\n", org->files_index);
Jens Axboecade3ef2007-03-23 15:29:45 +01001444
1445 if (!org->files)
1446 return;
1447
Jens Axboe9efef3c2008-03-06 10:26:25 +01001448 td->files = malloc(org->files_index * sizeof(f));
Jens Axboecade3ef2007-03-23 15:29:45 +01001449
Jens Axboed7df1d12013-03-20 19:57:01 -06001450 if (td->o.file_lock_mode != FILE_LOCK_NONE)
1451 td->file_locks = malloc(org->files_index);
1452
Jens Axboe9efef3c2008-03-06 10:26:25 +01001453 for_each_file(org, f, i) {
Jens Axboeb0fe4212008-03-01 18:22:27 +01001454 struct fio_file *__f;
1455
Jens Axboe7b5cb702014-04-01 16:37:03 -06001456 __f = alloc_new_file(td);
Jens Axboe0b9d69e2009-09-11 22:29:54 +02001457
Aaron Carrollbc3456f2008-06-25 09:20:37 +02001458 if (f->file_name) {
Jens Axboef17c4392008-03-01 18:40:46 +01001459 __f->file_name = smalloc_strdup(f->file_name);
Jens Axboec48c0be2008-07-31 19:55:02 +02001460 if (!__f->file_name) {
1461 log_err("fio: smalloc OOM\n");
1462 assert(0);
1463 }
Jens Axboe0b9d69e2009-09-11 22:29:54 +02001464
Aaron Carrollbc3456f2008-06-25 09:20:37 +02001465 __f->filetype = f->filetype;
1466 }
Jens Axboeb0fe4212008-03-01 18:22:27 +01001467
Jens Axboe67ad9242014-02-07 10:56:15 -07001468 if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1469 __f->lock = f->lock;
1470 else if (td->o.file_lock_mode == FILE_LOCK_READWRITE)
1471 __f->rwlock = f->rwlock;
1472
Jens Axboeb0fe4212008-03-01 18:22:27 +01001473 td->files[i] = __f;
Jens Axboecade3ef2007-03-23 15:29:45 +01001474 }
1475}
Jens Axboef29b25a2007-07-23 08:56:43 +02001476
1477/*
1478 * Returns the index that matches the filename, or -1 if not there
1479 */
1480int get_fileno(struct thread_data *td, const char *fname)
1481{
1482 struct fio_file *f;
1483 unsigned int i;
1484
1485 for_each_file(td, f, i)
1486 if (!strcmp(f->file_name, fname))
1487 return i;
1488
1489 return -1;
1490}
1491
1492/*
1493 * For log usage, where we add/open/close files automatically
1494 */
1495void free_release_files(struct thread_data *td)
1496{
1497 close_files(td);
1498 td->files_index = 0;
1499 td->nr_normal_files = 0;
1500}
Jens Axboe33c48812013-01-21 09:46:06 -07001501
1502void fio_file_reset(struct thread_data *td, struct fio_file *f)
1503{
1504 f->last_pos = f->file_offset;
1505 f->last_start = -1ULL;
1506 if (f->io_axmap)
1507 axmap_reset(f->io_axmap);
1508 if (td->o.random_generator == FIO_RAND_GEN_LFSR)
1509 lfsr_reset(&f->lfsr, td->rand_seeds[FIO_RAND_BLOCK_OFF]);
1510}
Jens Axboe002fe732014-02-11 08:31:13 -07001511
1512int fio_files_done(struct thread_data *td)
1513{
1514 struct fio_file *f;
1515 unsigned int i;
1516
1517 for_each_file(td, f, i)
1518 if (!fio_file_done(f))
1519 return 0;
1520
1521 return 1;
1522}
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08001523
1524/* free memory used in initialization phase only */
Castor Fu190b8f02014-03-20 10:59:29 -07001525void filesetup_mem_free(void)
1526{
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08001527 free_already_allocated();
1528}