blob: c132c326e33afe5766cb2fbe15e1d031b8a30fa3 [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 Axboeda27a4b2014-04-14 08:45:13 -0600691 buf[255] = '\0';
692 strncpy(buf, f->file_name, 255);
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200693
694 if (stat(buf, &sb) < 0) {
695 if (errno != ENOENT)
696 break;
697 strcpy(buf, ".");
698 if (stat(buf, &sb) < 0)
699 break;
700 }
701
702 fm = NULL;
703 flist_for_each(n, &list) {
704 fm = flist_entry(n, struct fio_mount, list);
705 if (fm->key == sb.st_dev)
706 break;
707
708 fm = NULL;
709 }
710
711 if (fm)
712 continue;
713
714 fm = malloc(sizeof(*fm));
715 strcpy(fm->__base, buf);
716 fm->base = basename(fm->__base);
717 fm->key = sb.st_dev;
718 flist_add(&fm->list, &list);
719 }
720
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200721 flist_for_each_safe(n, tmp, &list) {
722 unsigned long long sz;
723
724 fm = flist_entry(n, struct fio_mount, list);
725 flist_del(&fm->list);
726
727 sz = get_fs_size(fm->base);
728 if (sz && sz != -1ULL)
729 ret += sz;
730
731 free(fm);
732 }
733
734 return ret;
735}
736
Jens Axboebedc9dc2014-03-17 12:51:09 -0600737uint64_t get_start_offset(struct thread_data *td, struct fio_file *f)
Dan Ehrenbergce95d652012-08-16 08:58:21 +0200738{
Jens Axboebedc9dc2014-03-17 12:51:09 -0600739 struct thread_options *o = &td->o;
740
741 if (o->file_append && f->filetype == FIO_TYPE_FILE)
742 return f->real_file_size;
743
Dan Ehrenbergce95d652012-08-16 08:58:21 +0200744 return td->o.start_offset +
745 (td->thread_number - 1) * td->o.offset_increment;
746}
747
Jens Axboe7bb48f82007-03-27 15:30:28 +0200748/*
749 * Open the files and setup files sizes, creating files if necessary.
750 */
751int setup_files(struct thread_data *td)
752{
753 unsigned long long total_size, extend_size;
Jens Axboede98bd32013-04-05 11:09:20 +0200754 struct thread_options *o = &td->o;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200755 struct fio_file *f;
Jens Axboe002fe732014-02-11 08:31:13 -0700756 unsigned int i, nr_fs_extra = 0;
Jens Axboe000b0802007-03-27 15:56:10 +0200757 int err = 0, need_extend;
Jens Axboee90391e2013-04-13 20:40:53 +0200758 int old_state;
Jens Axboe002fe732014-02-11 08:31:13 -0700759 const unsigned int bs = td_min_bs(td);
760 uint64_t fs = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200761
Jens Axboeee56ad52008-02-01 10:30:20 +0100762 dprint(FD_FILE, "setup files\n");
763
Jens Axboe8edd9732014-03-01 08:24:03 -0700764 old_state = td_bump_runstate(td, TD_SETTING_UP);
Jens Axboee90391e2013-04-13 20:40:53 +0200765
Jens Axboede98bd32013-04-05 11:09:20 +0200766 if (o->read_iolog_file)
Jens Axboe25460cf2012-05-02 13:58:02 +0200767 goto done;
Jens Axboe691c8fb2008-03-07 14:26:26 +0100768
Jens Axboe53cdc682006-10-18 11:50:58 +0200769 /*
770 * if ioengine defines a setup() method, it's responsible for
Jens Axboe7bb48f82007-03-27 15:30:28 +0200771 * opening the files and setting f->real_file_size to indicate
772 * the valid range for that file.
Jens Axboe53cdc682006-10-18 11:50:58 +0200773 */
774 if (td->io_ops->setup)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200775 err = td->io_ops->setup(td);
776 else
Jens Axboebab3fd52007-04-02 10:34:18 +0200777 err = get_file_sizes(td);
Jens Axboe53cdc682006-10-18 11:50:58 +0200778
Jens Axboef1027062006-10-20 10:14:01 +0200779 if (err)
Jens Axboee90391e2013-04-13 20:40:53 +0200780 goto err_out;
Jens Axboef1027062006-10-20 10:14:01 +0200781
Jens Axboe0a7eb122006-10-20 10:36:42 +0200782 /*
Jens Axboe7bb48f82007-03-27 15:30:28 +0200783 * check sizes. if the files/devices do not exist and the size
784 * isn't passed to fio, abort.
Jens Axboe0a7eb122006-10-20 10:36:42 +0200785 */
Jens Axboe7bb48f82007-03-27 15:30:28 +0200786 total_size = 0;
787 for_each_file(td, f, i) {
788 if (f->real_file_size == -1ULL)
789 total_size = -1ULL;
790 else
791 total_size += f->real_file_size;
792 }
Jens Axboe0a7eb122006-10-20 10:36:42 +0200793
Jens Axboede98bd32013-04-05 11:09:20 +0200794 if (o->fill_device)
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200795 td->fill_device_size = get_fs_free_counts(td);
796
Jens Axboe7bb48f82007-03-27 15:30:28 +0200797 /*
798 * device/file sizes are zero and no size given, punt
799 */
Jens Axboede98bd32013-04-05 11:09:20 +0200800 if ((!total_size || total_size == -1ULL) && !o->size &&
801 !(td->io_ops->flags & FIO_NOIO) && !o->fill_device &&
802 !(o->nr_files && (o->file_size_low || o->file_size_high))) {
803 log_err("%s: you need to specify size=\n", o->name);
Jens Axboee1161c32007-02-22 19:36:48 +0100804 td_verror(td, EINVAL, "total_file_size");
Jens Axboee90391e2013-04-13 20:40:53 +0200805 goto err_out;
Jens Axboe53cdc682006-10-18 11:50:58 +0200806 }
807
Jens Axboe7bb48f82007-03-27 15:30:28 +0200808 /*
Jens Axboe002fe732014-02-11 08:31:13 -0700809 * Calculate per-file size and potential extra size for the
810 * first files, if needed.
811 */
Jens Axboed1e78e62014-04-06 09:56:10 -0600812 if (!o->file_size_low && o->nr_files) {
Jens Axboe002fe732014-02-11 08:31:13 -0700813 uint64_t all_fs;
814
815 fs = o->size / o->nr_files;
816 all_fs = fs * o->nr_files;
817
818 if (all_fs < o->size)
819 nr_fs_extra = (o->size - all_fs) / bs;
820 }
821
822 /*
Jens Axboe7bb48f82007-03-27 15:30:28 +0200823 * now file sizes are known, so we can set ->io_size. if size= is
824 * not given, ->io_size is just equal to ->real_file_size. if size
825 * is given, ->io_size is size / nr_files.
826 */
827 extend_size = total_size = 0;
828 need_extend = 0;
829 for_each_file(td, f, i) {
Jens Axboebedc9dc2014-03-17 12:51:09 -0600830 f->file_offset = get_start_offset(td, f);
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200831
Jens Axboede98bd32013-04-05 11:09:20 +0200832 if (!o->file_size_low) {
Jens Axboe7bb48f82007-03-27 15:30:28 +0200833 /*
834 * no file size range given, file size is equal to
Jens Axboe002fe732014-02-11 08:31:13 -0700835 * total size divided by number of files. If that is
836 * zero, set it to the real file size. If the size
837 * doesn't divide nicely with the min blocksize,
838 * make the first files bigger.
Jens Axboe7bb48f82007-03-27 15:30:28 +0200839 */
Jens Axboe002fe732014-02-11 08:31:13 -0700840 f->io_size = fs;
841 if (nr_fs_extra) {
842 nr_fs_extra--;
843 f->io_size += bs;
844 }
845
Jens Axboe65bdb102008-01-24 13:13:12 +0100846 if (!f->io_size)
Jens Axboe273f8c92008-01-25 14:02:15 +0100847 f->io_size = f->real_file_size - f->file_offset;
Jens Axboede98bd32013-04-05 11:09:20 +0200848 } else if (f->real_file_size < o->file_size_low ||
849 f->real_file_size > o->file_size_high) {
850 if (f->file_offset > o->file_size_low)
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200851 goto err_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200852 /*
853 * file size given. if it's fixed, use that. if it's a
854 * range, generate a random size in-between.
855 */
Jens Axboede98bd32013-04-05 11:09:20 +0200856 if (o->file_size_low == o->file_size_high)
857 f->io_size = o->file_size_low - f->file_offset;
858 else {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100859 f->io_size = get_rand_file_size(td)
860 - f->file_offset;
861 }
Jens Axboe65bdb102008-01-24 13:13:12 +0100862 } else
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200863 f->io_size = f->real_file_size - f->file_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200864
865 if (f->io_size == -1ULL)
866 total_size = -1ULL;
Shaohua Li4d002562012-09-21 08:32:32 +0200867 else {
Jens Axboede98bd32013-04-05 11:09:20 +0200868 if (o->size_percent)
869 f->io_size = (f->io_size * o->size_percent) / 100;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200870 total_size += f->io_size;
Shaohua Li4d002562012-09-21 08:32:32 +0200871 }
Jens Axboe7bb48f82007-03-27 15:30:28 +0200872
873 if (f->filetype == FIO_TYPE_FILE &&
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200874 (f->io_size + f->file_offset) > f->real_file_size &&
Jens Axboe7bb48f82007-03-27 15:30:28 +0200875 !(td->io_ops->flags & FIO_DISKLESSIO)) {
Jens Axboede98bd32013-04-05 11:09:20 +0200876 if (!o->create_on_open) {
Jens Axboe814452b2009-03-04 12:53:13 +0100877 need_extend++;
878 extend_size += (f->io_size + f->file_offset);
879 } else
880 f->real_file_size = f->io_size + f->file_offset;
Jens Axboed6aed792009-06-03 08:41:15 +0200881 fio_file_set_extend(f);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100882 }
Jens Axboe7bb48f82007-03-27 15:30:28 +0200883 }
884
Jens Axboede98bd32013-04-05 11:09:20 +0200885 if (!o->size || o->size > total_size)
886 o->size = total_size;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200887
888 /*
889 * See if we need to extend some files
890 */
891 if (need_extend) {
892 temp_stall_ts = 1;
Jens Axboef3afa572012-09-17 13:34:16 +0200893 if (output_format == FIO_OUTPUT_NORMAL)
Shaozhi Shawn Yea7ba8c52008-09-10 09:04:18 +0200894 log_info("%s: Laying out IO file(s) (%u file(s) /"
Jens Axboede98bd32013-04-05 11:09:20 +0200895 " %lluMB)\n", o->name, need_extend,
Shaozhi Shawn Yea7ba8c52008-09-10 09:04:18 +0200896 extend_size >> 20);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200897
898 for_each_file(td, f, i) {
Jens Axboe5e0074c2009-06-02 21:40:09 +0200899 unsigned long long old_len = -1ULL, extend_len = -1ULL;
Jens Axboe3baddf22008-04-04 11:10:30 +0200900
Jens Axboed6aed792009-06-03 08:41:15 +0200901 if (!fio_file_extend(f))
Jens Axboe7bb48f82007-03-27 15:30:28 +0200902 continue;
903
Jens Axboe409b3412007-03-28 09:57:01 +0200904 assert(f->filetype == FIO_TYPE_FILE);
Jens Axboed6aed792009-06-03 08:41:15 +0200905 fio_file_clear_extend(f);
Jens Axboede98bd32013-04-05 11:09:20 +0200906 if (!o->fill_device) {
Jens Axboe5e0074c2009-06-02 21:40:09 +0200907 old_len = f->real_file_size;
Jens Axboe0b9d69e2009-09-11 22:29:54 +0200908 extend_len = f->io_size + f->file_offset -
909 old_len;
Jens Axboe5e0074c2009-06-02 21:40:09 +0200910 }
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200911 f->real_file_size = (f->io_size + f->file_offset);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200912 err = extend_file(td, f);
913 if (err)
914 break;
Jens Axboe5e0074c2009-06-02 21:40:09 +0200915
Jens Axboe3baddf22008-04-04 11:10:30 +0200916 err = __file_invalidate_cache(td, f, old_len,
917 extend_len);
918 close(f->fd);
919 f->fd = -1;
920 if (err)
921 break;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200922 }
923 temp_stall_ts = 0;
924 }
925
926 if (err)
Jens Axboee90391e2013-04-13 20:40:53 +0200927 goto err_out;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200928
Jens Axboede98bd32013-04-05 11:09:20 +0200929 if (!o->zone_size)
930 o->zone_size = o->size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200931
Jens Axboeea966f82007-09-03 10:09:37 +0200932 /*
933 * iolog already set the total io size, if we read back
934 * stored entries.
935 */
Jens Axboede98bd32013-04-05 11:09:20 +0200936 if (!o->read_iolog_file)
937 td->total_io_size = o->size * o->loops;
Jens Axboe25460cf2012-05-02 13:58:02 +0200938
939done:
Jens Axboede98bd32013-04-05 11:09:20 +0200940 if (o->create_only)
Jens Axboe25460cf2012-05-02 13:58:02 +0200941 td->done = 1;
942
Jens Axboe8edd9732014-03-01 08:24:03 -0700943 td_restore_runstate(td, old_state);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200944 return 0;
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200945err_offset:
Jens Axboede98bd32013-04-05 11:09:20 +0200946 log_err("%s: you need to specify valid offset=\n", o->name);
Jens Axboee90391e2013-04-13 20:40:53 +0200947err_out:
Jens Axboe8edd9732014-03-01 08:24:03 -0700948 td_restore_runstate(td, old_state);
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200949 return 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200950}
951
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200952int pre_read_files(struct thread_data *td)
953{
954 struct fio_file *f;
955 unsigned int i;
956
957 dprint(FD_FILE, "pre_read files\n");
958
959 for_each_file(td, f, i) {
960 pre_read_file(td, f);
961 }
962
963 return 1;
964}
965
Jens Axboe9c6f6312012-11-07 09:15:45 +0100966static int __init_rand_distribution(struct thread_data *td, struct fio_file *f)
967{
Jens Axboe23162962012-11-07 19:47:47 +0100968 unsigned int range_size, seed;
Jens Axboe9c6f6312012-11-07 09:15:45 +0100969 unsigned long nranges;
Jens Axboe21415db2013-01-04 13:09:29 +0100970 uint64_t file_size;
Jens Axboe9c6f6312012-11-07 09:15:45 +0100971
972 range_size = min(td->o.min_bs[DDIR_READ], td->o.min_bs[DDIR_WRITE]);
Jens Axboe21415db2013-01-04 13:09:29 +0100973 file_size = min(f->real_file_size, f->io_size);
Jens Axboe9c6f6312012-11-07 09:15:45 +0100974
Jens Axboe21415db2013-01-04 13:09:29 +0100975 nranges = (file_size + range_size - 1) / range_size;
Jens Axboe9c6f6312012-11-07 09:15:45 +0100976
Jens Axboe23162962012-11-07 19:47:47 +0100977 seed = jhash(f->file_name, strlen(f->file_name), 0) * td->thread_number;
Jens Axboe84256872012-11-17 10:06:36 -0700978 if (!td->o.rand_repeatable)
979 seed = td->rand_seeds[4];
980
Jens Axboe9c6f6312012-11-07 09:15:45 +0100981 if (td->o.random_distribution == FIO_RAND_DIST_ZIPF)
Jens Axboe888677a2013-04-10 22:19:46 +0200982 zipf_init(&f->zipf, nranges, td->o.zipf_theta.u.f, seed);
Jens Axboe9c6f6312012-11-07 09:15:45 +0100983 else
Jens Axboe888677a2013-04-10 22:19:46 +0200984 pareto_init(&f->zipf, nranges, td->o.pareto_h.u.f, seed);
Jens Axboe9c6f6312012-11-07 09:15:45 +0100985
986 return 1;
987}
988
989static int init_rand_distribution(struct thread_data *td)
990{
991 struct fio_file *f;
992 unsigned int i;
993 int state;
994
995 if (td->o.random_distribution == FIO_RAND_DIST_RANDOM)
996 return 0;
997
Jens Axboe8edd9732014-03-01 08:24:03 -0700998 state = td_bump_runstate(td, TD_SETTING_UP);
999
Jens Axboe9c6f6312012-11-07 09:15:45 +01001000 for_each_file(td, f, i)
1001 __init_rand_distribution(td, f);
Jens Axboe8edd9732014-03-01 08:24:03 -07001002
1003 td_restore_runstate(td, state);
Jens Axboe9c6f6312012-11-07 09:15:45 +01001004
1005 return 1;
1006}
1007
Jens Axboe68727072007-03-15 20:49:25 +01001008int init_random_map(struct thread_data *td)
1009{
Jens Axboe51ede0b2012-11-22 13:50:29 +01001010 unsigned long long blocks;
Jens Axboe68727072007-03-15 20:49:25 +01001011 struct fio_file *f;
1012 unsigned int i;
1013
Jens Axboe9c6f6312012-11-07 09:15:45 +01001014 if (init_rand_distribution(td))
1015 return 0;
Jens Axboe3831a842012-11-26 19:59:14 +01001016 if (!td_random(td))
Jens Axboe68727072007-03-15 20:49:25 +01001017 return 0;
1018
1019 for_each_file(td, f, i) {
Jens Axboe38f30c82013-01-11 14:03:05 +01001020 uint64_t file_size = min(f->real_file_size, f->io_size);
1021
Jens Axboe53737ae2013-02-21 15:18:17 +01001022 blocks = file_size / (unsigned long long) td->o.rw_min_bs;
1023
Jens Axboe8055e412012-11-26 08:43:47 +01001024 if (td->o.random_generator == FIO_RAND_GEN_LFSR) {
Jens Axboe82af46b2012-12-04 13:12:25 +01001025 unsigned long seed;
1026
1027 seed = td->rand_seeds[FIO_RAND_BLOCK_OFF];
1028
Jens Axboe225ba9e2014-02-26 14:31:15 -08001029 if (!lfsr_init(&f->lfsr, blocks, seed, 0))
Jens Axboe8055e412012-11-26 08:43:47 +01001030 continue;
Jens Axboe3831a842012-11-26 19:59:14 +01001031 } else if (!td->o.norandommap) {
Jens Axboe7ebd7962012-11-28 21:24:46 +01001032 f->io_axmap = axmap_new(blocks);
1033 if (f->io_axmap)
Jens Axboe8055e412012-11-26 08:43:47 +01001034 continue;
Jens Axboe1cad7122012-11-29 21:37:11 +01001035 } else if (td->o.norandommap)
1036 continue;
Jens Axboeceadd592012-02-02 08:41:28 +01001037
Jens Axboe2b386d22008-03-26 10:32:57 +01001038 if (!td->o.softrandommap) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001039 log_err("fio: failed allocating random map. If running"
1040 " a large number of jobs, try the 'norandommap'"
Jens Axboe2b386d22008-03-26 10:32:57 +01001041 " option or set 'softrandommap'. Or give"
1042 " a larger --alloc-size to fio.\n");
Jens Axboe68727072007-03-15 20:49:25 +01001043 return 1;
1044 }
Jens Axboe303032a2008-03-26 10:11:10 +01001045
1046 log_info("fio: file %s failed allocating random map. Running "
1047 "job without.\n", f->file_name);
Jens Axboe68727072007-03-15 20:49:25 +01001048 }
1049
1050 return 0;
1051}
1052
Jens Axboe53cdc682006-10-18 11:50:58 +02001053void close_files(struct thread_data *td)
1054{
Jens Axboe0ab8db82006-10-18 17:16:23 +02001055 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +01001056 unsigned int i;
Jens Axboe53cdc682006-10-18 11:50:58 +02001057
Jens Axboe2be3eec2009-06-10 09:05:13 +02001058 for_each_file(td, f, i) {
1059 if (fio_file_open(f))
1060 td_io_close_file(td, f);
1061 }
Jens Axboe24ffd2c2008-03-01 15:47:08 +01001062}
1063
1064void close_and_free_files(struct thread_data *td)
1065{
1066 struct fio_file *f;
1067 unsigned int i;
1068
Jens Axboeee56ad52008-02-01 10:30:20 +01001069 dprint(FD_FILE, "close files\n");
1070
Jens Axboe0ab8db82006-10-18 17:16:23 +02001071 for_each_file(td, f, i) {
Jens Axboe22a57ba2009-06-09 14:34:35 +02001072 if (fio_file_open(f))
1073 td_io_close_file(td, f);
1074
Shaozhi Shawn Yeb9fbcf22008-09-10 09:05:17 +02001075 remove_file_hash(f);
Jens Axboeb3dc7f02007-03-09 12:52:15 +01001076
Jens Axboeb5f4d8b2014-03-20 08:28:11 -06001077 if (td->o.unlink && f->filetype == FIO_TYPE_FILE) {
1078 dprint(FD_FILE, "free unlink %s\n", f->file_name);
1079 unlink(f->file_name);
1080 }
1081
Jens Axboef17c4392008-03-01 18:40:46 +01001082 sfree(f->file_name);
Jens Axboefa1da862007-03-23 15:20:54 +01001083 f->file_name = NULL;
Jens Axboe7ebd7962012-11-28 21:24:46 +01001084 axmap_free(f->io_axmap);
1085 f->io_axmap = NULL;
Jens Axboe78d99e62008-03-01 18:41:50 +01001086 sfree(f);
Jens Axboe53cdc682006-10-18 11:50:58 +02001087 }
Jens Axboeb4a6a592006-10-20 13:54:47 +02001088
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001089 td->o.filename = NULL;
Jens Axboecade3ef2007-03-23 15:29:45 +01001090 free(td->files);
Jens Axboed7df1d12013-03-20 19:57:01 -06001091 free(td->file_locks);
Jens Axboe9efef3c2008-03-06 10:26:25 +01001092 td->files_index = 0;
Jens Axboeb4a6a592006-10-20 13:54:47 +02001093 td->files = NULL;
Jens Axboed7df1d12013-03-20 19:57:01 -06001094 td->file_locks = NULL;
Jens Axboe27ddbfa2014-01-22 16:27:32 -08001095 td->o.file_lock_mode = FILE_LOCK_NONE;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001096 td->o.nr_files = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +02001097}
Jens Axboeaf52b342007-03-13 10:07:47 +01001098
Jens Axboee3bab462007-03-13 11:22:09 +01001099static void get_file_type(struct fio_file *f)
Jens Axboeaf52b342007-03-13 10:07:47 +01001100{
1101 struct stat sb;
1102
Jens Axboe66159822007-04-16 21:54:24 +02001103 if (!strcmp(f->file_name, "-"))
1104 f->filetype = FIO_TYPE_PIPE;
1105 else
1106 f->filetype = FIO_TYPE_FILE;
Jens Axboeaf52b342007-03-13 10:07:47 +01001107
Bruce Cran38921822012-02-22 17:55:16 +00001108 /* \\.\ is the device namespace in Windows, where every file is
1109 * a block device */
1110 if (strncmp(f->file_name, "\\\\.\\", 4) == 0)
1111 f->filetype = FIO_TYPE_BD;
1112
Jens Axboeb30d3952010-02-06 23:36:26 +01001113 if (!stat(f->file_name, &sb)) {
Bruce Cran38921822012-02-22 17:55:16 +00001114 if (S_ISBLK(sb.st_mode))
Jens Axboeaf52b342007-03-13 10:07:47 +01001115 f->filetype = FIO_TYPE_BD;
1116 else if (S_ISCHR(sb.st_mode))
1117 f->filetype = FIO_TYPE_CHAR;
Jens Axboeb5605e92007-04-16 20:42:33 +02001118 else if (S_ISFIFO(sb.st_mode))
1119 f->filetype = FIO_TYPE_PIPE;
Jens Axboeaf52b342007-03-13 10:07:47 +01001120 }
1121}
1122
Jens Axboe90426232014-04-01 12:28:47 -06001123static int __is_already_allocated(const char *fname)
1124{
1125 struct flist_head *entry;
1126 char *filename;
1127
1128 if (flist_empty(&filename_list))
1129 return 0;
1130
1131 flist_for_each(entry, &filename_list) {
1132 filename = flist_entry(entry, struct file_name, list)->filename;
1133
1134 if (strcmp(filename, fname) == 0)
1135 return 1;
1136 }
1137
1138 return 0;
1139}
1140
1141static int is_already_allocated(const char *fname)
1142{
1143 int ret;
1144
1145 fio_file_hash_lock();
1146 ret = __is_already_allocated(fname);
1147 fio_file_hash_unlock();
1148 return ret;
1149}
1150
Castor Fu190b8f02014-03-20 10:59:29 -07001151static void set_already_allocated(const char *fname)
1152{
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08001153 struct file_name *fn;
1154
1155 fn = malloc(sizeof(struct file_name));
1156 fn->filename = strdup(fname);
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08001157
Jens Axboe90426232014-04-01 12:28:47 -06001158 fio_file_hash_lock();
1159 if (!__is_already_allocated(fname)) {
1160 flist_add_tail(&fn->list, &filename_list);
1161 fn = NULL;
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08001162 }
Jens Axboe90426232014-04-01 12:28:47 -06001163 fio_file_hash_unlock();
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08001164
Jens Axboe90426232014-04-01 12:28:47 -06001165 if (fn) {
1166 free(fn->filename);
1167 free(fn);
1168 }
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08001169}
1170
Jens Axboe90426232014-04-01 12:28:47 -06001171
Castor Fu190b8f02014-03-20 10:59:29 -07001172static void free_already_allocated(void)
1173{
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08001174 struct flist_head *entry, *tmp;
1175 struct file_name *fn;
1176
Jens Axboe90426232014-04-01 12:28:47 -06001177 if (flist_empty(&filename_list))
1178 return;
1179
1180 fio_file_hash_lock();
1181 flist_for_each_safe(entry, tmp, &filename_list) {
1182 fn = flist_entry(entry, struct file_name, list);
1183 free(fn->filename);
1184 flist_del(&fn->list);
1185 free(fn);
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08001186 }
Jens Axboe90426232014-04-01 12:28:47 -06001187
1188 fio_file_hash_unlock();
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08001189}
1190
Jens Axboe7b5cb702014-04-01 16:37:03 -06001191static struct fio_file *alloc_new_file(struct thread_data *td)
1192{
1193 struct fio_file *f;
1194
1195 f = smalloc(sizeof(*f));
1196 if (!f) {
1197 log_err("fio: smalloc OOM\n");
1198 assert(0);
1199 return NULL;
1200 }
1201
1202 f->fd = -1;
1203 f->shadow_fd = -1;
1204 fio_file_reset(td, f);
1205 return f;
1206}
1207
Jens Axboe5903e7b2014-02-26 13:42:13 -08001208int add_file(struct thread_data *td, const char *fname, int numjob, int inc)
Jens Axboeaf52b342007-03-13 10:07:47 +01001209{
Jens Axboe7b4e4fe2007-03-13 12:51:40 +01001210 int cur_files = td->files_index;
Jens Axboebd0ee742007-03-23 14:26:23 +01001211 char file_name[PATH_MAX];
Jens Axboeaf52b342007-03-13 10:07:47 +01001212 struct fio_file *f;
Jens Axboebd0ee742007-03-23 14:26:23 +01001213 int len = 0;
Jens Axboeaf52b342007-03-13 10:07:47 +01001214
Jens Axboeee56ad52008-02-01 10:30:20 +01001215 dprint(FD_FILE, "add file %s\n", fname);
1216
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08001217 if (td->o.directory)
1218 len = set_name_idx(file_name, td->o.directory, numjob);
1219
1220 sprintf(file_name + len, "%s", fname);
1221
1222 /* clean cloned siblings using existing files */
1223 if (numjob && is_already_allocated(file_name))
1224 return 0;
1225
Jens Axboe7b5cb702014-04-01 16:37:03 -06001226 f = alloc_new_file(td);
Jens Axboebd0ee742007-03-23 14:26:23 +01001227
Jens Axboefc99bc02009-03-04 15:27:42 +01001228 if (td->files_size <= td->files_index) {
Jianpeng Ma1983e322013-01-10 13:19:27 +01001229 unsigned int new_size = td->o.nr_files + 1;
Jens Axboe126d65c2008-03-01 18:04:31 +01001230
Jens Axboefc99bc02009-03-04 15:27:42 +01001231 dprint(FD_FILE, "resize file array to %d files\n", new_size);
1232
1233 td->files = realloc(td->files, new_size * sizeof(f));
Jianpeng Mad537c082013-01-11 08:52:55 +01001234 if (td->files == NULL) {
1235 log_err("fio: realloc OOM\n");
1236 assert(0);
1237 }
Jens Axboed7df1d12013-03-20 19:57:01 -06001238 if (td->o.file_lock_mode != FILE_LOCK_NONE) {
1239 td->file_locks = realloc(td->file_locks, new_size);
1240 if (!td->file_locks) {
1241 log_err("fio: realloc OOM\n");
1242 assert(0);
1243 }
1244 td->file_locks[cur_files] = FILE_LOCK_NONE;
1245 }
Jens Axboefc99bc02009-03-04 15:27:42 +01001246 td->files_size = new_size;
1247 }
Jens Axboe8bb76792009-03-04 15:37:52 +01001248 td->files[cur_files] = f;
Shaohua Li89ac1d42012-06-11 08:56:32 +02001249 f->fileno = cur_files;
Jens Axboe126d65c2008-03-01 18:04:31 +01001250
Jens Axboe07eb79d2007-04-12 13:02:38 +02001251 /*
1252 * init function, io engine may not be loaded yet
1253 */
1254 if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO))
1255 f->real_file_size = -1ULL;
1256
Jens Axboef17c4392008-03-01 18:40:46 +01001257 f->file_name = smalloc_strdup(file_name);
Jens Axboec48c0be2008-07-31 19:55:02 +02001258 if (!f->file_name) {
1259 log_err("fio: smalloc OOM\n");
1260 assert(0);
1261 }
Jens Axboe0b9d69e2009-09-11 22:29:54 +02001262
Jens Axboee3bab462007-03-13 11:22:09 +01001263 get_file_type(f);
Jens Axboeaf52b342007-03-13 10:07:47 +01001264
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001265 switch (td->o.file_lock_mode) {
1266 case FILE_LOCK_NONE:
1267 break;
1268 case FILE_LOCK_READWRITE:
Jens Axboed7df1d12013-03-20 19:57:01 -06001269 f->rwlock = fio_rwlock_init();
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001270 break;
1271 case FILE_LOCK_EXCLUSIVE:
Jens Axboe521da522012-08-02 11:21:36 +02001272 f->lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001273 break;
1274 default:
1275 log_err("fio: unknown lock mode: %d\n", td->o.file_lock_mode);
1276 assert(0);
1277 }
Jens Axboe29c13492008-03-01 19:25:20 +01001278
Jens Axboe7b4e4fe2007-03-13 12:51:40 +01001279 td->files_index++;
Jens Axboe15494412007-03-13 11:52:47 +01001280 if (f->filetype == FIO_TYPE_FILE)
1281 td->nr_normal_files++;
Jens Axboef29b25a2007-07-23 08:56:43 +02001282
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08001283 set_already_allocated(file_name);
1284
Jens Axboe5903e7b2014-02-26 13:42:13 -08001285 if (inc)
1286 td->o.nr_files++;
1287
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001288 dprint(FD_FILE, "file %p \"%s\" added at %d\n", f, f->file_name,
1289 cur_files);
Jens Axboe9efef3c2008-03-06 10:26:25 +01001290
Jens Axboef29b25a2007-07-23 08:56:43 +02001291 return cur_files;
Jens Axboeaf52b342007-03-13 10:07:47 +01001292}
Jens Axboe0ad920e2007-03-13 11:06:45 +01001293
Jens Axboe49ffb4a2010-08-24 15:01:37 +02001294int add_file_exclusive(struct thread_data *td, const char *fname)
1295{
1296 struct fio_file *f;
1297 unsigned int i;
1298
1299 for_each_file(td, f, i) {
1300 if (!strcmp(f->file_name, fname))
1301 return i;
1302 }
1303
Jens Axboe5903e7b2014-02-26 13:42:13 -08001304 return add_file(td, fname, 0, 1);
Jens Axboe49ffb4a2010-08-24 15:01:37 +02001305}
1306
Jens Axboe0ad920e2007-03-13 11:06:45 +01001307void get_file(struct fio_file *f)
1308{
Jens Axboe8172fe92008-02-01 21:51:02 +01001309 dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references);
Jens Axboed6aed792009-06-03 08:41:15 +02001310 assert(fio_file_open(f));
Jens Axboe0ad920e2007-03-13 11:06:45 +01001311 f->references++;
1312}
1313
Jens Axboe6977bcd2008-03-01 15:55:36 +01001314int put_file(struct thread_data *td, struct fio_file *f)
Jens Axboe0ad920e2007-03-13 11:06:45 +01001315{
Jens Axboe98e1ac42008-03-06 11:56:48 +01001316 int f_ret = 0, ret = 0;
Jens Axboe6977bcd2008-03-01 15:55:36 +01001317
Jens Axboe8172fe92008-02-01 21:51:02 +01001318 dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references);
Jens Axboeee56ad52008-02-01 10:30:20 +01001319
Jens Axboe22a57ba2009-06-09 14:34:35 +02001320 if (!fio_file_open(f)) {
1321 assert(f->fd == -1);
Jens Axboe6977bcd2008-03-01 15:55:36 +01001322 return 0;
Jens Axboe22a57ba2009-06-09 14:34:35 +02001323 }
Jens Axboe0ad920e2007-03-13 11:06:45 +01001324
1325 assert(f->references);
1326 if (--f->references)
Jens Axboe6977bcd2008-03-01 15:55:36 +01001327 return 0;
Jens Axboe0ad920e2007-03-13 11:06:45 +01001328
Jens Axboed424d4d2007-04-17 09:05:10 +02001329 if (should_fsync(td) && td->o.fsync_on_close)
Jens Axboe98e1ac42008-03-06 11:56:48 +01001330 f_ret = fsync(f->fd);
Jens Axboeebb14152007-03-13 14:42:15 +01001331
Jens Axboe0ad920e2007-03-13 11:06:45 +01001332 if (td->io_ops->close_file)
Jens Axboe6977bcd2008-03-01 15:55:36 +01001333 ret = td->io_ops->close_file(td, f);
Jens Axboe1020a132007-03-29 11:15:06 +02001334
Jens Axboe98e1ac42008-03-06 11:56:48 +01001335 if (!ret)
Jens Axboea5fb4612008-05-28 10:54:01 +02001336 ret = f_ret;
Jens Axboe98e1ac42008-03-06 11:56:48 +01001337
Jens Axboe0ad920e2007-03-13 11:06:45 +01001338 td->nr_open_files--;
Jens Axboed6aed792009-06-03 08:41:15 +02001339 fio_file_clear_open(f);
Jens Axboe22a57ba2009-06-09 14:34:35 +02001340 assert(f->fd == -1);
Jens Axboe6977bcd2008-03-01 15:55:36 +01001341 return ret;
Jens Axboe0ad920e2007-03-13 11:06:45 +01001342}
Jens Axboebbf6b542007-03-13 15:28:55 +01001343
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001344void lock_file(struct thread_data *td, struct fio_file *f, enum fio_ddir ddir)
Jens Axboeb2bd2bd2008-03-01 18:19:52 +01001345{
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001346 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1347 return;
Jens Axboe29c13492008-03-01 19:25:20 +01001348
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001349 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
1350 if (ddir == DDIR_READ)
Jens Axboed7df1d12013-03-20 19:57:01 -06001351 fio_rwlock_read(f->rwlock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001352 else
Jens Axboed7df1d12013-03-20 19:57:01 -06001353 fio_rwlock_write(f->rwlock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001354 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1355 fio_mutex_down(f->lock);
1356
Jens Axboed7df1d12013-03-20 19:57:01 -06001357 td->file_locks[f->fileno] = td->o.file_lock_mode;
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001358}
1359
1360void unlock_file(struct thread_data *td, struct fio_file *f)
1361{
1362 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1363 return;
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001364
Jens Axboed7df1d12013-03-20 19:57:01 -06001365 if (td->o.file_lock_mode == FILE_LOCK_READWRITE)
1366 fio_rwlock_unlock(f->rwlock);
1367 else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001368 fio_mutex_up(f->lock);
Jens Axboed7df1d12013-03-20 19:57:01 -06001369
1370 td->file_locks[f->fileno] = FILE_LOCK_NONE;
Jens Axboeb2bd2bd2008-03-01 18:19:52 +01001371}
1372
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001373void unlock_file_all(struct thread_data *td, struct fio_file *f)
Jens Axboeb2bd2bd2008-03-01 18:19:52 +01001374{
Jens Axboef2a28032014-02-11 09:12:06 -07001375 if (td->o.file_lock_mode == FILE_LOCK_NONE || !td->file_locks)
Jens Axboe27ddbfa2014-01-22 16:27:32 -08001376 return;
Jens Axboed7df1d12013-03-20 19:57:01 -06001377 if (td->file_locks[f->fileno] != FILE_LOCK_NONE)
1378 unlock_file(td, f);
Jens Axboeb2bd2bd2008-03-01 18:19:52 +01001379}
1380
Jens Axboebbf6b542007-03-13 15:28:55 +01001381static int recurse_dir(struct thread_data *td, const char *dirname)
1382{
1383 struct dirent *dir;
1384 int ret = 0;
1385 DIR *D;
1386
1387 D = opendir(dirname);
1388 if (!D) {
Jens Axboe0ddb2702007-03-27 19:43:53 +02001389 char buf[FIO_VERROR_SIZE];
1390
Ken Raeburn98ffb8f2013-01-30 22:31:09 +01001391 snprintf(buf, FIO_VERROR_SIZE, "opendir(%s)", dirname);
Jens Axboe0ddb2702007-03-27 19:43:53 +02001392 td_verror(td, errno, buf);
Jens Axboebbf6b542007-03-13 15:28:55 +01001393 return 1;
1394 }
1395
1396 while ((dir = readdir(D)) != NULL) {
1397 char full_path[PATH_MAX];
1398 struct stat sb;
1399
Jens Axboee85b2b82007-03-14 09:39:06 +01001400 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
1401 continue;
Jens Axboe96d32d52007-03-14 09:16:09 +01001402
Bruce Cranb9fd7882012-02-20 17:01:46 +00001403 sprintf(full_path, "%s%s%s", dirname, FIO_OS_PATH_SEPARATOR, dir->d_name);
Jens Axboebbf6b542007-03-13 15:28:55 +01001404
1405 if (lstat(full_path, &sb) == -1) {
1406 if (errno != ENOENT) {
1407 td_verror(td, errno, "stat");
Jens Axboe68ace9e2014-04-11 11:24:00 -06001408 ret = 1;
1409 break;
Jens Axboebbf6b542007-03-13 15:28:55 +01001410 }
1411 }
1412
1413 if (S_ISREG(sb.st_mode)) {
Jens Axboe5903e7b2014-02-26 13:42:13 -08001414 add_file(td, full_path, 0, 1);
Jens Axboebbf6b542007-03-13 15:28:55 +01001415 continue;
1416 }
Jens Axboe0ddb2702007-03-27 19:43:53 +02001417 if (!S_ISDIR(sb.st_mode))
1418 continue;
Jens Axboebbf6b542007-03-13 15:28:55 +01001419
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001420 ret = recurse_dir(td, full_path);
1421 if (ret)
Jens Axboebbf6b542007-03-13 15:28:55 +01001422 break;
1423 }
1424
1425 closedir(D);
1426 return ret;
1427}
1428
1429int add_dir_files(struct thread_data *td, const char *path)
1430{
Jens Axboe0ddb2702007-03-27 19:43:53 +02001431 int ret = recurse_dir(td, path);
1432
1433 if (!ret)
1434 log_info("fio: opendir added %d files\n", td->o.nr_files);
1435
1436 return ret;
Jens Axboebbf6b542007-03-13 15:28:55 +01001437}
Jens Axboecade3ef2007-03-23 15:29:45 +01001438
1439void dup_files(struct thread_data *td, struct thread_data *org)
1440{
1441 struct fio_file *f;
1442 unsigned int i;
Jens Axboe9efef3c2008-03-06 10:26:25 +01001443
1444 dprint(FD_FILE, "dup files: %d\n", org->files_index);
Jens Axboecade3ef2007-03-23 15:29:45 +01001445
1446 if (!org->files)
1447 return;
1448
Jens Axboe9efef3c2008-03-06 10:26:25 +01001449 td->files = malloc(org->files_index * sizeof(f));
Jens Axboecade3ef2007-03-23 15:29:45 +01001450
Jens Axboed7df1d12013-03-20 19:57:01 -06001451 if (td->o.file_lock_mode != FILE_LOCK_NONE)
1452 td->file_locks = malloc(org->files_index);
1453
Jens Axboe9efef3c2008-03-06 10:26:25 +01001454 for_each_file(org, f, i) {
Jens Axboeb0fe4212008-03-01 18:22:27 +01001455 struct fio_file *__f;
1456
Jens Axboe7b5cb702014-04-01 16:37:03 -06001457 __f = alloc_new_file(td);
Jens Axboe0b9d69e2009-09-11 22:29:54 +02001458
Aaron Carrollbc3456f2008-06-25 09:20:37 +02001459 if (f->file_name) {
Jens Axboef17c4392008-03-01 18:40:46 +01001460 __f->file_name = smalloc_strdup(f->file_name);
Jens Axboec48c0be2008-07-31 19:55:02 +02001461 if (!__f->file_name) {
1462 log_err("fio: smalloc OOM\n");
1463 assert(0);
1464 }
Jens Axboe0b9d69e2009-09-11 22:29:54 +02001465
Aaron Carrollbc3456f2008-06-25 09:20:37 +02001466 __f->filetype = f->filetype;
1467 }
Jens Axboeb0fe4212008-03-01 18:22:27 +01001468
Jens Axboe67ad9242014-02-07 10:56:15 -07001469 if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1470 __f->lock = f->lock;
1471 else if (td->o.file_lock_mode == FILE_LOCK_READWRITE)
1472 __f->rwlock = f->rwlock;
1473
Jens Axboeb0fe4212008-03-01 18:22:27 +01001474 td->files[i] = __f;
Jens Axboecade3ef2007-03-23 15:29:45 +01001475 }
1476}
Jens Axboef29b25a2007-07-23 08:56:43 +02001477
1478/*
1479 * Returns the index that matches the filename, or -1 if not there
1480 */
1481int get_fileno(struct thread_data *td, const char *fname)
1482{
1483 struct fio_file *f;
1484 unsigned int i;
1485
1486 for_each_file(td, f, i)
1487 if (!strcmp(f->file_name, fname))
1488 return i;
1489
1490 return -1;
1491}
1492
1493/*
1494 * For log usage, where we add/open/close files automatically
1495 */
1496void free_release_files(struct thread_data *td)
1497{
1498 close_files(td);
1499 td->files_index = 0;
1500 td->nr_normal_files = 0;
1501}
Jens Axboe33c48812013-01-21 09:46:06 -07001502
1503void fio_file_reset(struct thread_data *td, struct fio_file *f)
1504{
1505 f->last_pos = f->file_offset;
1506 f->last_start = -1ULL;
1507 if (f->io_axmap)
1508 axmap_reset(f->io_axmap);
1509 if (td->o.random_generator == FIO_RAND_GEN_LFSR)
1510 lfsr_reset(&f->lfsr, td->rand_seeds[FIO_RAND_BLOCK_OFF]);
1511}
Jens Axboe002fe732014-02-11 08:31:13 -07001512
1513int fio_files_done(struct thread_data *td)
1514{
1515 struct fio_file *f;
1516 unsigned int i;
1517
1518 for_each_file(td, f, i)
1519 if (!fio_file_done(f))
1520 return 0;
1521
1522 return 1;
1523}
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08001524
1525/* free memory used in initialization phase only */
Castor Fu190b8f02014-03-20 10:59:29 -07001526void filesetup_mem_free(void)
1527{
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08001528 free_already_allocated();
1529}