blob: 43146ba7671f61216fa938380c4bab908cd44b20 [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);
Castor Fu9187a262014-08-19 09:28:53 -070062 if ((td_io_unlink_file(td, f) < 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
Bruce Cran9c0f3f32014-04-28 15:54:43 -060072#ifdef WIN32
73 flags |= _O_BINARY;
74#endif
75
Jens Axboeee56ad52008-02-01 10:30:20 +010076 dprint(FD_FILE, "open file %s, flags %x\n", f->file_name, flags);
Jens Axboe507a7022007-03-27 15:52:46 +020077 f->fd = open(f->file_name, flags, 0644);
Jens Axboe53cdc682006-10-18 11:50:58 +020078 if (f->fd < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +010079 td_verror(td, errno, "open");
Jens Axboe53cdc682006-10-18 11:50:58 +020080 return 1;
81 }
82
Jens Axboe97ac9922013-01-24 15:00:25 -070083#ifdef CONFIG_POSIX_FALLOCATE
Eric Gourioua596f042011-06-17 09:11:45 +020084 if (!td->o.fill_device) {
85 switch (td->o.fallocate_mode) {
86 case FIO_FALLOCATE_NONE:
87 break;
88 case FIO_FALLOCATE_POSIX:
89 dprint(FD_FILE, "posix_fallocate file %s size %llu\n",
Jens Axboe4b91ee82013-02-25 10:18:33 +010090 f->file_name,
91 (unsigned long long) f->real_file_size);
Jens Axboe7bc8c2c2010-01-28 11:31:31 +010092
Eric Gourioua596f042011-06-17 09:11:45 +020093 r = posix_fallocate(f->fd, 0, f->real_file_size);
94 if (r > 0) {
95 log_err("fio: posix_fallocate fails: %s\n",
96 strerror(r));
97 }
98 break;
Jens Axboe97ac9922013-01-24 15:00:25 -070099#ifdef CONFIG_LINUX_FALLOCATE
Eric Gourioua596f042011-06-17 09:11:45 +0200100 case FIO_FALLOCATE_KEEP_SIZE:
101 dprint(FD_FILE,
102 "fallocate(FALLOC_FL_KEEP_SIZE) "
Jens Axboe4b91ee82013-02-25 10:18:33 +0100103 "file %s size %llu\n", f->file_name,
104 (unsigned long long) f->real_file_size);
Eric Gourioua596f042011-06-17 09:11:45 +0200105
106 r = fallocate(f->fd, FALLOC_FL_KEEP_SIZE, 0,
107 f->real_file_size);
Jens Axboe888677a2013-04-10 22:19:46 +0200108 if (r != 0)
Eric Gourioua596f042011-06-17 09:11:45 +0200109 td_verror(td, errno, "fallocate");
Jens Axboe888677a2013-04-10 22:19:46 +0200110
Eric Gourioua596f042011-06-17 09:11:45 +0200111 break;
Jens Axboe97ac9922013-01-24 15:00:25 -0700112#endif /* CONFIG_LINUX_FALLOCATE */
Eric Gourioua596f042011-06-17 09:11:45 +0200113 default:
114 log_err("fio: unknown fallocate mode: %d\n",
115 td->o.fallocate_mode);
116 assert(0);
Jens Axboe7bc8c2c2010-01-28 11:31:31 +0100117 }
118 }
Jens Axboe97ac9922013-01-24 15:00:25 -0700119#endif /* CONFIG_POSIX_FALLOCATE */
Bruce Cran9b836562011-01-08 19:49:54 +0100120
Shawn Lewisfc74ac12008-01-11 09:45:11 +0100121 if (!new_layout)
122 goto done;
123
Jens Axboe5e0074c2009-06-02 21:40:09 +0200124 /*
125 * The size will be -1ULL when fill_device is used, so don't truncate
126 * or fallocate this file, just write it
127 */
128 if (!td->o.fill_device) {
129 dprint(FD_FILE, "truncate file %s, size %llu\n", f->file_name,
Jens Axboe4b91ee82013-02-25 10:18:33 +0100130 (unsigned long long) f->real_file_size);
Jens Axboe5e0074c2009-06-02 21:40:09 +0200131 if (ftruncate(f->fd, f->real_file_size) == -1) {
John3cd4c662013-12-29 19:20:35 -0700132 if (errno != EFBIG) {
133 td_verror(td, errno, "ftruncate");
134 goto err;
135 }
Jens Axboe5e0074c2009-06-02 21:40:09 +0200136 }
Jens Axboe5e0074c2009-06-02 21:40:09 +0200137 }
Jens Axboe40f82982006-10-25 11:21:05 +0200138
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100139 b = malloc(td->o.max_bs[DDIR_WRITE]);
Jens Axboe53cdc682006-10-18 11:50:58 +0200140
Jens Axboe7bb48f82007-03-27 15:30:28 +0200141 left = f->real_file_size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200142 while (left && !td->terminate) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100143 bs = td->o.max_bs[DDIR_WRITE];
Jens Axboe53cdc682006-10-18 11:50:58 +0200144 if (bs > left)
145 bs = left;
146
Jens Axboecc86c392013-05-03 15:12:33 +0200147 fill_io_buffer(td, b, bs, bs);
148
Jens Axboe53cdc682006-10-18 11:50:58 +0200149 r = write(f->fd, b, bs);
150
Jens Axboe5e0074c2009-06-02 21:40:09 +0200151 if (r > 0) {
152 left -= r;
Jens Axboe53cdc682006-10-18 11:50:58 +0200153 continue;
154 } else {
Jens Axboe5e0074c2009-06-02 21:40:09 +0200155 if (r < 0) {
156 int __e = errno;
157
158 if (__e == ENOSPC) {
159 if (td->o.fill_device)
160 break;
161 log_info("fio: ENOSPC on laying out "
162 "file, stopping\n");
163 break;
164 }
Jens Axboee1161c32007-02-22 19:36:48 +0100165 td_verror(td, errno, "write");
Jens Axboe5e0074c2009-06-02 21:40:09 +0200166 } else
Jens Axboee1161c32007-02-22 19:36:48 +0100167 td_verror(td, EIO, "write");
Jens Axboe53cdc682006-10-18 11:50:58 +0200168
169 break;
170 }
171 }
172
Jens Axboeffdfabd2008-03-26 09:18:14 +0100173 if (td->terminate) {
174 dprint(FD_FILE, "terminate unlink %s\n", f->file_name);
Castor Fu9187a262014-08-19 09:28:53 -0700175 td_io_unlink_file(td, f);
Jens Axboeffdfabd2008-03-26 09:18:14 +0100176 } else if (td->o.create_fsync) {
Jens Axboe98e1ac42008-03-06 11:56:48 +0100177 if (fsync(f->fd) < 0) {
178 td_verror(td, errno, "fsync");
179 goto err;
180 }
181 }
Jens Axboe0d1cd202009-06-05 22:11:52 +0200182 if (td->o.fill_device && !td_write(td)) {
Jens Axboed6aed792009-06-03 08:41:15 +0200183 fio_file_clear_size_known(f);
Jens Axboe5e0074c2009-06-02 21:40:09 +0200184 if (td_io_get_file_size(td, f))
185 goto err;
186 if (f->io_size > f->real_file_size)
187 f->io_size = f->real_file_size;
188 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200189
190 free(b);
Jens Axboe507a7022007-03-27 15:52:46 +0200191done:
Jens Axboe53cdc682006-10-18 11:50:58 +0200192 return 0;
193err:
194 close(f->fd);
195 f->fd = -1;
Jens Axboef3e1eb22014-04-11 11:28:54 -0600196 if (b)
197 free(b);
Jens Axboe53cdc682006-10-18 11:50:58 +0200198 return 1;
199}
200
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200201static int pre_read_file(struct thread_data *td, struct fio_file *f)
202{
Jens Axboeef799a62014-04-01 17:01:30 -0600203 int ret = 0, r, did_open = 0, old_runstate;
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200204 unsigned long long left;
205 unsigned int bs;
206 char *b;
207
Jens Axboe9c0d2242009-07-01 12:26:28 +0200208 if (td->io_ops->flags & FIO_PIPEIO)
209 return 0;
210
Jens Axboed6aed792009-06-03 08:41:15 +0200211 if (!fio_file_open(f)) {
Jens Axboeb0f65862009-05-20 11:52:15 +0200212 if (td->io_ops->open_file(td, f)) {
213 log_err("fio: cannot pre-read, failed to open file\n");
214 return 1;
215 }
216 did_open = 1;
217 }
218
Jens Axboe8edd9732014-03-01 08:24:03 -0700219 old_runstate = td_bump_runstate(td, TD_PRE_READING);
Jens Axboeb0f65862009-05-20 11:52:15 +0200220
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200221 bs = td->o.max_bs[DDIR_READ];
222 b = malloc(bs);
223 memset(b, 0, bs);
224
Jens Axboeef799a62014-04-01 17:01:30 -0600225 if (lseek(f->fd, f->file_offset, SEEK_SET) < 0) {
226 td_verror(td, errno, "lseek");
227 log_err("fio: failed to lseek pre-read file\n");
228 ret = 1;
229 goto error;
230 }
231
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200232 left = f->io_size;
233
234 while (left && !td->terminate) {
235 if (bs > left)
236 bs = left;
237
238 r = read(f->fd, b, bs);
239
240 if (r == (int) bs) {
241 left -= bs;
242 continue;
243 } else {
244 td_verror(td, EIO, "pre_read");
245 break;
246 }
247 }
248
Jens Axboeef799a62014-04-01 17:01:30 -0600249error:
Jens Axboe8edd9732014-03-01 08:24:03 -0700250 td_restore_runstate(td, old_runstate);
Jens Axboeb0f65862009-05-20 11:52:15 +0200251
252 if (did_open)
253 td->io_ops->close_file(td, f);
Jens Axboeef799a62014-04-01 17:01:30 -0600254
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200255 free(b);
Jens Axboeef799a62014-04-01 17:01:30 -0600256 return ret;
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200257}
258
Jens Axboe7bb48f82007-03-27 15:30:28 +0200259static unsigned long long get_rand_file_size(struct thread_data *td)
Jens Axboe9c60ce62007-03-15 09:14:47 +0100260{
Jens Axboedc873b62008-06-04 20:13:04 +0200261 unsigned long long ret, sized;
Jens Axboe1294c3e2011-05-11 08:15:18 +0200262 unsigned long r;
Jens Axboe9c60ce62007-03-15 09:14:47 +0100263
Jens Axboe4c07ad82011-03-28 09:51:09 +0200264 if (td->o.use_os_rand) {
265 r = os_random_long(&td->file_size_state);
266 sized = td->o.file_size_high - td->o.file_size_low;
267 ret = (unsigned long long) ((double) sized * (r / (OS_RAND_MAX + 1.0)));
268 } else {
269 r = __rand(&td->__file_size_state);
270 sized = td->o.file_size_high - td->o.file_size_low;
271 ret = (unsigned long long) ((double) sized * (r / (FRAND_MAX + 1.0)));
272 }
273
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100274 ret += td->o.file_size_low;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100275 ret -= (ret % td->o.rw_min_bs);
Jens Axboe9c60ce62007-03-15 09:14:47 +0100276 return ret;
277}
278
Jens Axboe53cdc682006-10-18 11:50:58 +0200279static int file_size(struct thread_data *td, struct fio_file *f)
280{
281 struct stat st;
282
Jens Axboedf9c26b2009-03-05 10:13:58 +0100283 if (stat(f->file_name, &st) == -1) {
Jens Axboe7bb48f82007-03-27 15:30:28 +0200284 td_verror(td, errno, "fstat");
285 return 1;
286 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200287
Jens Axboe7bb48f82007-03-27 15:30:28 +0200288 f->real_file_size = st.st_size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200289 return 0;
290}
291
292static int bdev_size(struct thread_data *td, struct fio_file *f)
293{
Bruce Cran9b836562011-01-08 19:49:54 +0100294 unsigned long long bytes = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200295 int r;
296
Jens Axboedf9c26b2009-03-05 10:13:58 +0100297 if (td->io_ops->open_file(td, f)) {
298 log_err("fio: failed opening blockdev %s for size check\n",
299 f->file_name);
300 return 1;
301 }
302
Bruce Cranecc314b2011-01-04 10:59:30 +0100303 r = blockdev_size(f, &bytes);
Jens Axboe53cdc682006-10-18 11:50:58 +0200304 if (r) {
Jens Axboee1161c32007-02-22 19:36:48 +0100305 td_verror(td, r, "blockdev_size");
Jens Axboedf9c26b2009-03-05 10:13:58 +0100306 goto err;
Jens Axboe53cdc682006-10-18 11:50:58 +0200307 }
308
Jens Axboe7ab077a2009-02-02 15:07:37 +0100309 if (!bytes) {
310 log_err("%s: zero sized block device?\n", f->file_name);
Jens Axboedf9c26b2009-03-05 10:13:58 +0100311 goto err;
Jens Axboe7ab077a2009-02-02 15:07:37 +0100312 }
313
Jens Axboe53cdc682006-10-18 11:50:58 +0200314 f->real_file_size = bytes;
Jens Axboe22a57ba2009-06-09 14:34:35 +0200315 td->io_ops->close_file(td, f);
Jens Axboe53cdc682006-10-18 11:50:58 +0200316 return 0;
Jens Axboedf9c26b2009-03-05 10:13:58 +0100317err:
318 td->io_ops->close_file(td, f);
319 return 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200320}
321
Jens Axboe4ccdccd2010-06-24 10:27:22 +0200322static int char_size(struct thread_data *td, struct fio_file *f)
323{
324#ifdef FIO_HAVE_CHARDEV_SIZE
Bruce Cran9b836562011-01-08 19:49:54 +0100325 unsigned long long bytes = 0;
Jens Axboe4ccdccd2010-06-24 10:27:22 +0200326 int r;
327
328 if (td->io_ops->open_file(td, f)) {
329 log_err("fio: failed opening blockdev %s for size check\n",
330 f->file_name);
331 return 1;
332 }
333
Bruce Cranecc314b2011-01-04 10:59:30 +0100334 r = chardev_size(f, &bytes);
Jens Axboe4ccdccd2010-06-24 10:27:22 +0200335 if (r) {
336 td_verror(td, r, "chardev_size");
337 goto err;
338 }
339
340 if (!bytes) {
341 log_err("%s: zero sized char device?\n", f->file_name);
342 goto err;
343 }
344
345 f->real_file_size = bytes;
346 td->io_ops->close_file(td, f);
347 return 0;
348err:
349 td->io_ops->close_file(td, f);
350 return 1;
351#else
352 f->real_file_size = -1ULL;
353 return 0;
354#endif
355}
356
Jens Axboe53cdc682006-10-18 11:50:58 +0200357static int get_file_size(struct thread_data *td, struct fio_file *f)
358{
359 int ret = 0;
360
Jens Axboed6aed792009-06-03 08:41:15 +0200361 if (fio_file_size_known(f))
Jens Axboe409b3412007-03-28 09:57:01 +0200362 return 0;
363
Jens Axboe7bb48f82007-03-27 15:30:28 +0200364 if (f->filetype == FIO_TYPE_FILE)
365 ret = file_size(td, f);
366 else if (f->filetype == FIO_TYPE_BD)
Jens Axboe53cdc682006-10-18 11:50:58 +0200367 ret = bdev_size(td, f);
Jens Axboe4ccdccd2010-06-24 10:27:22 +0200368 else if (f->filetype == FIO_TYPE_CHAR)
369 ret = char_size(td, f);
Jens Axboe53cdc682006-10-18 11:50:58 +0200370 else
371 f->real_file_size = -1;
372
373 if (ret)
374 return ret;
375
376 if (f->file_offset > f->real_file_size) {
Bruce Cran0f2152c2010-12-15 10:33:03 +0100377 log_err("%s: offset extends end (%llu > %llu)\n", td->o.name,
Jens Axboe4e0a8fa2013-04-15 11:40:57 +0200378 (unsigned long long) f->file_offset,
379 (unsigned long long) f->real_file_size);
Jens Axboe53cdc682006-10-18 11:50:58 +0200380 return 1;
381 }
382
Jens Axboed6aed792009-06-03 08:41:15 +0200383 fio_file_set_size_known(f);
Jens Axboe53cdc682006-10-18 11:50:58 +0200384 return 0;
385}
386
Jens Axboe3baddf22008-04-04 11:10:30 +0200387static int __file_invalidate_cache(struct thread_data *td, struct fio_file *f,
388 unsigned long long off,
389 unsigned long long len)
Jens Axboee5b401d2006-10-18 16:03:40 +0200390{
391 int ret = 0;
392
Jens Axboeef7035a2014-06-18 15:30:09 -0700393#ifdef CONFIG_ESX
394 return 0;
395#endif
396
Jens Axboe5e0074c2009-06-02 21:40:09 +0200397 if (len == -1ULL)
Jens Axboe3baddf22008-04-04 11:10:30 +0200398 len = f->io_size;
399 if (off == -1ULL)
400 off = f->file_offset;
Jens Axboeee56ad52008-02-01 10:30:20 +0100401
Jens Axboe0d1cd202009-06-05 22:11:52 +0200402 if (len == -1ULL || off == -1ULL)
403 return 0;
404
Jens Axboe3baddf22008-04-04 11:10:30 +0200405 dprint(FD_IO, "invalidate cache %s: %llu/%llu\n", f->file_name, off,
406 len);
Jens Axboeb5af8292007-03-08 12:43:13 +0100407
Jens Axboe1be9f212014-05-19 19:57:05 -0600408 if (td->io_ops->invalidate)
409 ret = td->io_ops->invalidate(td, f);
410 else if (f->mmap_ptr) {
Bruce Cran03e20d62011-01-02 20:14:54 +0100411 ret = posix_madvise(f->mmap_ptr, f->mmap_sz, POSIX_MADV_DONTNEED);
Jens Axboea1c58072009-08-04 23:17:02 +0200412#ifdef FIO_MADV_FREE
Jens Axboe3e10fb82013-08-09 14:31:06 -0600413 if (f->filetype == FIO_TYPE_BD)
414 (void) posix_madvise(f->mmap_ptr, f->mmap_sz, FIO_MADV_FREE);
Jens Axboea1c58072009-08-04 23:17:02 +0200415#endif
416 } else if (f->filetype == FIO_TYPE_FILE) {
Bruce Cranecc314b2011-01-04 10:59:30 +0100417 ret = posix_fadvise(f->fd, off, len, POSIX_FADV_DONTNEED);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100418 } else if (f->filetype == FIO_TYPE_BD) {
Bruce Cranecc314b2011-01-04 10:59:30 +0100419 ret = blockdev_invalidate_cache(f);
Jens Axboe7e0e25c2007-03-27 12:49:56 +0200420 if (ret < 0 && errno == EACCES && geteuid()) {
Jens Axboe7172cfe2007-07-23 14:36:00 +0200421 if (!root_warn) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100422 log_err("fio: only root may flush block "
423 "devices. Cache flush bypassed!\n");
Jens Axboe7172cfe2007-07-23 14:36:00 +0200424 root_warn = 1;
425 }
Jens Axboe7e0e25c2007-03-27 12:49:56 +0200426 ret = 0;
427 }
Jens Axboeb5605e92007-04-16 20:42:33 +0200428 } else if (f->filetype == FIO_TYPE_CHAR || f->filetype == FIO_TYPE_PIPE)
Jens Axboee5b401d2006-10-18 16:03:40 +0200429 ret = 0;
430
Jens Axboedfe11fd2014-04-10 08:59:43 -0600431 /*
432 * Cache flushing isn't a fatal condition, and we know it will
433 * happen on some platforms where we don't have the proper
434 * function to flush eg block device caches. So just warn and
435 * continue on our way.
436 */
437 if (ret) {
438 log_info("fio: cache invalidation of %s failed: %s\n", f->file_name, strerror(errno));
439 ret = 0;
Jens Axboee5b401d2006-10-18 16:03:40 +0200440 }
441
Jens Axboedfe11fd2014-04-10 08:59:43 -0600442 return 0;
Jens Axboe3baddf22008-04-04 11:10:30 +0200443
444}
445
446int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
447{
Jens Axboed6aed792009-06-03 08:41:15 +0200448 if (!fio_file_open(f))
Jens Axboea5fb4612008-05-28 10:54:01 +0200449 return 0;
450
Jens Axboe5e0074c2009-06-02 21:40:09 +0200451 return __file_invalidate_cache(td, f, -1ULL, -1ULL);
Jens Axboee5b401d2006-10-18 16:03:40 +0200452}
453
Jens Axboe6977bcd2008-03-01 15:55:36 +0100454int generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
Jens Axboe53cdc682006-10-18 11:50:58 +0200455{
Jens Axboe6977bcd2008-03-01 15:55:36 +0100456 int ret = 0;
457
Jens Axboeee56ad52008-02-01 10:30:20 +0100458 dprint(FD_FILE, "fd close %s\n", f->file_name);
Jens Axboe4906e0b2008-03-01 18:59:51 +0100459
460 remove_file_hash(f);
461
Jens Axboe6977bcd2008-03-01 15:55:36 +0100462 if (close(f->fd) < 0)
463 ret = errno;
464
Jens Axboeb5af8292007-03-08 12:43:13 +0100465 f->fd = -1;
Jens Axboee6c4d732012-12-12 08:16:27 +0100466
467 if (f->shadow_fd != -1) {
468 close(f->shadow_fd);
469 f->shadow_fd = -1;
470 }
471
Juan Casse57e54e02013-09-20 09:20:52 -0600472 f->engine_data = 0;
Jens Axboe6977bcd2008-03-01 15:55:36 +0100473 return ret;
Jens Axboe53cdc682006-10-18 11:50:58 +0200474}
475
Dmitry Monakhov1ccc6dc2012-09-19 23:22:53 +0400476int file_lookup_open(struct fio_file *f, int flags)
Jens Axboe53cdc682006-10-18 11:50:58 +0200477{
Jens Axboe29c13492008-03-01 19:25:20 +0100478 struct fio_file *__f;
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100479 int from_hash;
480
481 __f = lookup_file_hash(f->file_name);
482 if (__f) {
Jens Axboe9efef3c2008-03-06 10:26:25 +0100483 dprint(FD_FILE, "found file in hash %s\n", f->file_name);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100484 /*
485 * racy, need the __f->lock locked
486 */
487 f->lock = __f->lock;
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100488 from_hash = 1;
489 } else {
Jens Axboe9efef3c2008-03-06 10:26:25 +0100490 dprint(FD_FILE, "file not found in hash %s\n", f->file_name);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100491 from_hash = 0;
492 }
493
Bruce Cran9c0f3f32014-04-28 15:54:43 -0600494#ifdef WIN32
495 flags |= _O_BINARY;
496#endif
497
Jens Axboee8670ef2008-03-06 12:06:30 +0100498 f->fd = open(f->file_name, flags, 0600);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100499 return from_hash;
500}
501
Jens Axboee6c4d732012-12-12 08:16:27 +0100502static int file_close_shadow_fds(struct thread_data *td)
503{
504 struct fio_file *f;
505 int num_closed = 0;
506 unsigned int i;
507
508 for_each_file(td, f, i) {
509 if (f->shadow_fd == -1)
510 continue;
511
512 close(f->shadow_fd);
513 f->shadow_fd = -1;
514 num_closed++;
515 }
516
517 return num_closed;
518}
519
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100520int generic_open_file(struct thread_data *td, struct fio_file *f)
521{
Jens Axboe66159822007-04-16 21:54:24 +0200522 int is_std = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200523 int flags = 0;
Jens Axboe29c13492008-03-01 19:25:20 +0100524 int from_hash = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200525
Jens Axboeee56ad52008-02-01 10:30:20 +0100526 dprint(FD_FILE, "fd open %s\n", f->file_name);
527
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200528 if (td_trim(td) && f->filetype != FIO_TYPE_BD) {
529 log_err("fio: trim only applies to block device\n");
530 return 1;
531 }
532
Jens Axboe66159822007-04-16 21:54:24 +0200533 if (!strcmp(f->file_name, "-")) {
534 if (td_rw(td)) {
535 log_err("fio: can't read/write to stdin/out\n");
536 return 1;
537 }
538 is_std = 1;
Jens Axboece98fa62007-04-17 13:27:32 +0200539
540 /*
541 * move output logging to stderr, if we are writing to stdout
542 */
543 if (td_write(td))
544 f_out = stderr;
Jens Axboe66159822007-04-16 21:54:24 +0200545 }
546
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200547 if (td_trim(td))
548 goto skip_flags;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100549 if (td->o.odirect)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100550 flags |= OS_O_DIRECT;
Chris Masond01612f2013-11-15 15:52:58 -0700551 if (td->o.oatomic) {
552 if (!FIO_O_ATOMIC) {
553 td_verror(td, EINVAL, "OS does not support atomic IO");
554 return 1;
555 }
556 flags |= OS_O_DIRECT | FIO_O_ATOMIC;
557 }
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100558 if (td->o.sync_io)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100559 flags |= O_SYNC;
Jens Axboe814452b2009-03-04 12:53:13 +0100560 if (td->o.create_on_open)
561 flags |= O_CREAT;
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200562skip_flags:
563 if (f->filetype != FIO_TYPE_FILE)
564 flags |= FIO_O_NOATIME;
Jens Axboe7abf8332006-11-23 15:01:19 +0100565
Aaron Carroll056f3452007-11-26 09:08:53 +0100566open_again:
Jens Axboe660a1cb2007-04-17 15:37:47 +0200567 if (td_write(td)) {
Jens Axboe17308152008-03-07 13:42:31 +0100568 if (!read_only)
569 flags |= O_RDWR;
Jens Axboe53cdc682006-10-18 11:50:58 +0200570
Jens Axboeaf52b342007-03-13 10:07:47 +0100571 if (f->filetype == FIO_TYPE_FILE)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100572 flags |= O_CREAT;
Jens Axboe2fd233b2007-03-02 08:44:25 +0100573
Jens Axboe66159822007-04-16 21:54:24 +0200574 if (is_std)
575 f->fd = dup(STDOUT_FILENO);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100576 else
577 from_hash = file_lookup_open(f, flags);
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200578 } else if (td_read(td)) {
Jens Axboe4241ea82007-09-12 08:18:36 +0200579 if (f->filetype == FIO_TYPE_CHAR && !read_only)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100580 flags |= O_RDWR;
581 else
582 flags |= O_RDONLY;
583
Jens Axboe66159822007-04-16 21:54:24 +0200584 if (is_std)
585 f->fd = dup(STDIN_FILENO);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100586 else
587 from_hash = file_lookup_open(f, flags);
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200588 } else { //td trim
589 flags |= O_RDWR;
590 from_hash = file_lookup_open(f, flags);
Jens Axboe53cdc682006-10-18 11:50:58 +0200591 }
592
593 if (f->fd == -1) {
Jens Axboee4e33252007-03-21 13:07:54 +0100594 char buf[FIO_VERROR_SIZE];
Jens Axboee1161c32007-02-22 19:36:48 +0100595 int __e = errno;
596
Jens Axboe835d9b92009-06-09 15:23:38 +0200597 if (__e == EPERM && (flags & FIO_O_NOATIME)) {
Jens Axboe5921e802008-05-30 15:02:38 +0200598 flags &= ~FIO_O_NOATIME;
Aaron Carroll056f3452007-11-26 09:08:53 +0100599 goto open_again;
600 }
Jens Axboee6c4d732012-12-12 08:16:27 +0100601 if (__e == EMFILE && file_close_shadow_fds(td))
602 goto open_again;
Aaron Carroll056f3452007-11-26 09:08:53 +0100603
Ken Raeburn98ffb8f2013-01-30 22:31:09 +0100604 snprintf(buf, sizeof(buf), "open(%s)", f->file_name);
Jens Axboee4e33252007-03-21 13:07:54 +0100605
Jens Axboea93c5f02012-06-11 08:53:53 +0200606 if (__e == EINVAL && (flags & OS_O_DIRECT)) {
607 log_err("fio: looks like your file system does not " \
608 "support direct=1/buffered=0\n");
609 }
610
Jens Axboee4e33252007-03-21 13:07:54 +0100611 td_verror(td, __e, buf);
Andreas Gruenbacher34329ca2014-07-09 19:37:33 +0200612 return 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200613 }
614
Jens Axboe29c13492008-03-01 19:25:20 +0100615 if (!from_hash && f->fd != -1) {
616 if (add_file_hash(f)) {
Jens Axboe3f0ca9b2011-05-24 21:36:24 +0200617 int fio_unused ret;
Jens Axboe29c13492008-03-01 19:25:20 +0100618
619 /*
Jens Axboee6c4d732012-12-12 08:16:27 +0100620 * Stash away descriptor for later close. This is to
621 * work-around a "feature" on Linux, where a close of
622 * an fd that has been opened for write will trigger
623 * udev to call blkid to check partitions, fs id, etc.
Anatol Pomozovde8f6de2013-09-26 16:31:34 -0700624 * That pollutes the device cache, which can slow down
Jens Axboee6c4d732012-12-12 08:16:27 +0100625 * unbuffered accesses.
Jens Axboe29c13492008-03-01 19:25:20 +0100626 */
Jens Axboee6c4d732012-12-12 08:16:27 +0100627 if (f->shadow_fd == -1)
628 f->shadow_fd = f->fd;
629 else {
630 /*
631 * OK to ignore, we haven't done anything
632 * with it
633 */
634 ret = generic_close_file(td, f);
635 }
Jens Axboe29c13492008-03-01 19:25:20 +0100636 goto open_again;
637 }
638 }
Jens Axboe4906e0b2008-03-01 18:59:51 +0100639
Jens Axboe53cdc682006-10-18 11:50:58 +0200640 return 0;
Jens Axboeb5af8292007-03-08 12:43:13 +0100641}
642
Jens Axboedf9c26b2009-03-05 10:13:58 +0100643int generic_get_file_size(struct thread_data *td, struct fio_file *f)
Jens Axboe21972cd2006-11-23 12:39:16 +0100644{
Jens Axboedf9c26b2009-03-05 10:13:58 +0100645 return get_file_size(td, f);
Jens Axboe21972cd2006-11-23 12:39:16 +0100646}
647
Jens Axboe7bb48f82007-03-27 15:30:28 +0200648/*
649 * open/close all files, so that ->real_file_size gets set
650 */
Jens Axboebab3fd52007-04-02 10:34:18 +0200651static int get_file_sizes(struct thread_data *td)
Jens Axboe53cdc682006-10-18 11:50:58 +0200652{
653 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100654 unsigned int i;
Jens Axboebab3fd52007-04-02 10:34:18 +0200655 int err = 0;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200656
657 for_each_file(td, f, i) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100658 dprint(FD_FILE, "get file size for %p/%d/%p\n", f, i,
659 f->file_name);
Jens Axboe9efef3c2008-03-06 10:26:25 +0100660
Jens Axboe99a47c62009-04-07 22:20:56 +0200661 if (td_io_get_file_size(td, f)) {
Jens Axboe40b44f42007-04-11 12:43:35 +0200662 if (td->error != ENOENT) {
663 log_err("%s\n", td->verror);
664 err = 1;
Andreas Gruenbacher34329ca2014-07-09 19:37:33 +0200665 break;
Jens Axboe40b44f42007-04-11 12:43:35 +0200666 }
Jens Axboe541d66d2007-03-27 19:50:11 +0200667 clear_error(td);
Jens Axboe07eb79d2007-04-12 13:02:38 +0200668 }
Jens Axboe409b3412007-03-28 09:57:01 +0200669
670 if (f->real_file_size == -1ULL && td->o.size)
671 f->real_file_size = td->o.size / td->o.nr_files;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200672 }
Jens Axboebab3fd52007-04-02 10:34:18 +0200673
674 return err;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200675}
676
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200677struct fio_mount {
678 struct flist_head list;
679 const char *base;
680 char __base[256];
681 unsigned int key;
682};
683
684/*
685 * Get free number of bytes for each file on each unique mount.
686 */
687static unsigned long long get_fs_free_counts(struct thread_data *td)
688{
689 struct flist_head *n, *tmp;
Jens Axboe68b03162010-06-24 09:22:41 +0200690 unsigned long long ret = 0;
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200691 struct fio_mount *fm;
692 FLIST_HEAD(list);
693 struct fio_file *f;
694 unsigned int i;
695
696 for_each_file(td, f, i) {
697 struct stat sb;
698 char buf[256];
699
Jens Axboe4ccdccd2010-06-24 10:27:22 +0200700 if (f->filetype == FIO_TYPE_BD || f->filetype == FIO_TYPE_CHAR) {
701 if (f->real_file_size != -1ULL)
702 ret += f->real_file_size;
Jens Axboe68b03162010-06-24 09:22:41 +0200703 continue;
704 } else if (f->filetype != FIO_TYPE_FILE)
705 continue;
706
Jens Axboeda27a4b2014-04-14 08:45:13 -0600707 buf[255] = '\0';
708 strncpy(buf, f->file_name, 255);
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200709
710 if (stat(buf, &sb) < 0) {
711 if (errno != ENOENT)
712 break;
713 strcpy(buf, ".");
714 if (stat(buf, &sb) < 0)
715 break;
716 }
717
718 fm = NULL;
719 flist_for_each(n, &list) {
720 fm = flist_entry(n, struct fio_mount, list);
721 if (fm->key == sb.st_dev)
722 break;
723
724 fm = NULL;
725 }
726
727 if (fm)
728 continue;
729
Jens Axboe3660cea2014-04-15 08:18:08 -0600730 fm = calloc(1, sizeof(*fm));
731 strncpy(fm->__base, buf, sizeof(fm->__base) - 1);
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200732 fm->base = basename(fm->__base);
733 fm->key = sb.st_dev;
734 flist_add(&fm->list, &list);
735 }
736
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200737 flist_for_each_safe(n, tmp, &list) {
738 unsigned long long sz;
739
740 fm = flist_entry(n, struct fio_mount, list);
741 flist_del(&fm->list);
742
743 sz = get_fs_size(fm->base);
744 if (sz && sz != -1ULL)
745 ret += sz;
746
747 free(fm);
748 }
749
750 return ret;
751}
752
Jens Axboebedc9dc2014-03-17 12:51:09 -0600753uint64_t get_start_offset(struct thread_data *td, struct fio_file *f)
Dan Ehrenbergce95d652012-08-16 08:58:21 +0200754{
Jens Axboebedc9dc2014-03-17 12:51:09 -0600755 struct thread_options *o = &td->o;
756
757 if (o->file_append && f->filetype == FIO_TYPE_FILE)
758 return f->real_file_size;
759
Dan Ehrenbergce95d652012-08-16 08:58:21 +0200760 return td->o.start_offset +
Jiri Horky5a65b4e2014-07-25 09:55:03 +0200761 td->subjob_number * td->o.offset_increment;
Dan Ehrenbergce95d652012-08-16 08:58:21 +0200762}
763
Jens Axboe7bb48f82007-03-27 15:30:28 +0200764/*
765 * Open the files and setup files sizes, creating files if necessary.
766 */
767int setup_files(struct thread_data *td)
768{
769 unsigned long long total_size, extend_size;
Jens Axboede98bd32013-04-05 11:09:20 +0200770 struct thread_options *o = &td->o;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200771 struct fio_file *f;
Jens Axboe002fe732014-02-11 08:31:13 -0700772 unsigned int i, nr_fs_extra = 0;
Jens Axboe000b0802007-03-27 15:56:10 +0200773 int err = 0, need_extend;
Jens Axboee90391e2013-04-13 20:40:53 +0200774 int old_state;
Jens Axboe002fe732014-02-11 08:31:13 -0700775 const unsigned int bs = td_min_bs(td);
776 uint64_t fs = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200777
Jens Axboeee56ad52008-02-01 10:30:20 +0100778 dprint(FD_FILE, "setup files\n");
779
Jens Axboe8edd9732014-03-01 08:24:03 -0700780 old_state = td_bump_runstate(td, TD_SETTING_UP);
Jens Axboee90391e2013-04-13 20:40:53 +0200781
Jens Axboede98bd32013-04-05 11:09:20 +0200782 if (o->read_iolog_file)
Jens Axboe25460cf2012-05-02 13:58:02 +0200783 goto done;
Jens Axboe691c8fb2008-03-07 14:26:26 +0100784
Jens Axboe53cdc682006-10-18 11:50:58 +0200785 /*
786 * if ioengine defines a setup() method, it's responsible for
Jens Axboe7bb48f82007-03-27 15:30:28 +0200787 * opening the files and setting f->real_file_size to indicate
788 * the valid range for that file.
Jens Axboe53cdc682006-10-18 11:50:58 +0200789 */
790 if (td->io_ops->setup)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200791 err = td->io_ops->setup(td);
792 else
Jens Axboebab3fd52007-04-02 10:34:18 +0200793 err = get_file_sizes(td);
Jens Axboe53cdc682006-10-18 11:50:58 +0200794
Jens Axboef1027062006-10-20 10:14:01 +0200795 if (err)
Jens Axboee90391e2013-04-13 20:40:53 +0200796 goto err_out;
Jens Axboef1027062006-10-20 10:14:01 +0200797
Jens Axboe0a7eb122006-10-20 10:36:42 +0200798 /*
Jens Axboe7bb48f82007-03-27 15:30:28 +0200799 * check sizes. if the files/devices do not exist and the size
800 * isn't passed to fio, abort.
Jens Axboe0a7eb122006-10-20 10:36:42 +0200801 */
Jens Axboe7bb48f82007-03-27 15:30:28 +0200802 total_size = 0;
803 for_each_file(td, f, i) {
804 if (f->real_file_size == -1ULL)
805 total_size = -1ULL;
806 else
807 total_size += f->real_file_size;
808 }
Jens Axboe0a7eb122006-10-20 10:36:42 +0200809
Jens Axboede98bd32013-04-05 11:09:20 +0200810 if (o->fill_device)
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200811 td->fill_device_size = get_fs_free_counts(td);
812
Jens Axboe7bb48f82007-03-27 15:30:28 +0200813 /*
814 * device/file sizes are zero and no size given, punt
815 */
Jens Axboede98bd32013-04-05 11:09:20 +0200816 if ((!total_size || total_size == -1ULL) && !o->size &&
817 !(td->io_ops->flags & FIO_NOIO) && !o->fill_device &&
818 !(o->nr_files && (o->file_size_low || o->file_size_high))) {
819 log_err("%s: you need to specify size=\n", o->name);
Jens Axboee1161c32007-02-22 19:36:48 +0100820 td_verror(td, EINVAL, "total_file_size");
Jens Axboee90391e2013-04-13 20:40:53 +0200821 goto err_out;
Jens Axboe53cdc682006-10-18 11:50:58 +0200822 }
823
Jens Axboe7bb48f82007-03-27 15:30:28 +0200824 /*
Jens Axboe002fe732014-02-11 08:31:13 -0700825 * Calculate per-file size and potential extra size for the
826 * first files, if needed.
827 */
Jens Axboed1e78e62014-04-06 09:56:10 -0600828 if (!o->file_size_low && o->nr_files) {
Jens Axboe002fe732014-02-11 08:31:13 -0700829 uint64_t all_fs;
830
831 fs = o->size / o->nr_files;
832 all_fs = fs * o->nr_files;
833
834 if (all_fs < o->size)
835 nr_fs_extra = (o->size - all_fs) / bs;
836 }
837
838 /*
Jens Axboe7bb48f82007-03-27 15:30:28 +0200839 * now file sizes are known, so we can set ->io_size. if size= is
840 * not given, ->io_size is just equal to ->real_file_size. if size
841 * is given, ->io_size is size / nr_files.
842 */
843 extend_size = total_size = 0;
844 need_extend = 0;
845 for_each_file(td, f, i) {
Jens Axboebedc9dc2014-03-17 12:51:09 -0600846 f->file_offset = get_start_offset(td, f);
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200847
Jens Axboede98bd32013-04-05 11:09:20 +0200848 if (!o->file_size_low) {
Jens Axboe7bb48f82007-03-27 15:30:28 +0200849 /*
850 * no file size range given, file size is equal to
Jens Axboe002fe732014-02-11 08:31:13 -0700851 * total size divided by number of files. If that is
852 * zero, set it to the real file size. If the size
853 * doesn't divide nicely with the min blocksize,
854 * make the first files bigger.
Jens Axboe7bb48f82007-03-27 15:30:28 +0200855 */
Jens Axboe002fe732014-02-11 08:31:13 -0700856 f->io_size = fs;
857 if (nr_fs_extra) {
858 nr_fs_extra--;
859 f->io_size += bs;
860 }
861
Jens Axboe65bdb102008-01-24 13:13:12 +0100862 if (!f->io_size)
Jens Axboe273f8c92008-01-25 14:02:15 +0100863 f->io_size = f->real_file_size - f->file_offset;
Jens Axboede98bd32013-04-05 11:09:20 +0200864 } else if (f->real_file_size < o->file_size_low ||
865 f->real_file_size > o->file_size_high) {
866 if (f->file_offset > o->file_size_low)
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200867 goto err_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200868 /*
869 * file size given. if it's fixed, use that. if it's a
870 * range, generate a random size in-between.
871 */
Jens Axboede98bd32013-04-05 11:09:20 +0200872 if (o->file_size_low == o->file_size_high)
873 f->io_size = o->file_size_low - f->file_offset;
874 else {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100875 f->io_size = get_rand_file_size(td)
876 - f->file_offset;
877 }
Jens Axboe65bdb102008-01-24 13:13:12 +0100878 } else
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200879 f->io_size = f->real_file_size - f->file_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200880
881 if (f->io_size == -1ULL)
882 total_size = -1ULL;
Shaohua Li4d002562012-09-21 08:32:32 +0200883 else {
Jens Axboede98bd32013-04-05 11:09:20 +0200884 if (o->size_percent)
885 f->io_size = (f->io_size * o->size_percent) / 100;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200886 total_size += f->io_size;
Shaohua Li4d002562012-09-21 08:32:32 +0200887 }
Jens Axboe7bb48f82007-03-27 15:30:28 +0200888
889 if (f->filetype == FIO_TYPE_FILE &&
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200890 (f->io_size + f->file_offset) > f->real_file_size &&
Jens Axboe7bb48f82007-03-27 15:30:28 +0200891 !(td->io_ops->flags & FIO_DISKLESSIO)) {
Jens Axboede98bd32013-04-05 11:09:20 +0200892 if (!o->create_on_open) {
Jens Axboe814452b2009-03-04 12:53:13 +0100893 need_extend++;
894 extend_size += (f->io_size + f->file_offset);
895 } else
896 f->real_file_size = f->io_size + f->file_offset;
Jens Axboed6aed792009-06-03 08:41:15 +0200897 fio_file_set_extend(f);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100898 }
Jens Axboe7bb48f82007-03-27 15:30:28 +0200899 }
900
Jens Axboea85066d2014-09-29 21:23:51 -0600901 if (!o->size || (total_size && o->size > total_size))
Jens Axboede98bd32013-04-05 11:09:20 +0200902 o->size = total_size;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200903
Jens Axboed1faa062014-04-16 23:51:27 +0200904 if (o->size < td_min_bs(td)) {
905 log_err("fio: blocksize too large for data set\n");
906 goto err_out;
907 }
908
Jens Axboe7bb48f82007-03-27 15:30:28 +0200909 /*
910 * See if we need to extend some files
911 */
912 if (need_extend) {
913 temp_stall_ts = 1;
Jens Axboef3afa572012-09-17 13:34:16 +0200914 if (output_format == FIO_OUTPUT_NORMAL)
Shaozhi Shawn Yea7ba8c52008-09-10 09:04:18 +0200915 log_info("%s: Laying out IO file(s) (%u file(s) /"
Jens Axboede98bd32013-04-05 11:09:20 +0200916 " %lluMB)\n", o->name, need_extend,
Shaozhi Shawn Yea7ba8c52008-09-10 09:04:18 +0200917 extend_size >> 20);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200918
919 for_each_file(td, f, i) {
Jens Axboe5e0074c2009-06-02 21:40:09 +0200920 unsigned long long old_len = -1ULL, extend_len = -1ULL;
Jens Axboe3baddf22008-04-04 11:10:30 +0200921
Jens Axboed6aed792009-06-03 08:41:15 +0200922 if (!fio_file_extend(f))
Jens Axboe7bb48f82007-03-27 15:30:28 +0200923 continue;
924
Jens Axboe409b3412007-03-28 09:57:01 +0200925 assert(f->filetype == FIO_TYPE_FILE);
Jens Axboed6aed792009-06-03 08:41:15 +0200926 fio_file_clear_extend(f);
Jens Axboede98bd32013-04-05 11:09:20 +0200927 if (!o->fill_device) {
Jens Axboe5e0074c2009-06-02 21:40:09 +0200928 old_len = f->real_file_size;
Jens Axboe0b9d69e2009-09-11 22:29:54 +0200929 extend_len = f->io_size + f->file_offset -
930 old_len;
Jens Axboe5e0074c2009-06-02 21:40:09 +0200931 }
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200932 f->real_file_size = (f->io_size + f->file_offset);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200933 err = extend_file(td, f);
934 if (err)
935 break;
Jens Axboe5e0074c2009-06-02 21:40:09 +0200936
Jens Axboe3baddf22008-04-04 11:10:30 +0200937 err = __file_invalidate_cache(td, f, old_len,
938 extend_len);
Jens Axboe9824f732014-04-14 12:05:22 -0600939
940 /*
941 * Shut up static checker
942 */
943 if (f->fd != -1)
944 close(f->fd);
945
Jens Axboe3baddf22008-04-04 11:10:30 +0200946 f->fd = -1;
947 if (err)
948 break;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200949 }
950 temp_stall_ts = 0;
951 }
952
953 if (err)
Jens Axboee90391e2013-04-13 20:40:53 +0200954 goto err_out;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200955
Jens Axboede98bd32013-04-05 11:09:20 +0200956 if (!o->zone_size)
957 o->zone_size = o->size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200958
Jens Axboeea966f82007-09-03 10:09:37 +0200959 /*
960 * iolog already set the total io size, if we read back
961 * stored entries.
962 */
Jens Axboe77731b22014-04-28 12:08:47 -0600963 if (!o->read_iolog_file) {
964 if (o->io_limit)
965 td->total_io_size = o->io_limit * o->loops;
966 else
967 td->total_io_size = o->size * o->loops;
968 }
Jens Axboe25460cf2012-05-02 13:58:02 +0200969
970done:
Jens Axboede98bd32013-04-05 11:09:20 +0200971 if (o->create_only)
Jens Axboe25460cf2012-05-02 13:58:02 +0200972 td->done = 1;
973
Jens Axboe8edd9732014-03-01 08:24:03 -0700974 td_restore_runstate(td, old_state);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200975 return 0;
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200976err_offset:
Jens Axboede98bd32013-04-05 11:09:20 +0200977 log_err("%s: you need to specify valid offset=\n", o->name);
Jens Axboee90391e2013-04-13 20:40:53 +0200978err_out:
Jens Axboe8edd9732014-03-01 08:24:03 -0700979 td_restore_runstate(td, old_state);
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200980 return 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200981}
982
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200983int pre_read_files(struct thread_data *td)
984{
985 struct fio_file *f;
986 unsigned int i;
987
988 dprint(FD_FILE, "pre_read files\n");
989
990 for_each_file(td, f, i) {
991 pre_read_file(td, f);
992 }
993
994 return 1;
995}
996
Jens Axboe9c6f6312012-11-07 09:15:45 +0100997static int __init_rand_distribution(struct thread_data *td, struct fio_file *f)
998{
Jens Axboe23162962012-11-07 19:47:47 +0100999 unsigned int range_size, seed;
Jens Axboe9c6f6312012-11-07 09:15:45 +01001000 unsigned long nranges;
Jens Axboe21415db2013-01-04 13:09:29 +01001001 uint64_t file_size;
Jens Axboe9c6f6312012-11-07 09:15:45 +01001002
1003 range_size = min(td->o.min_bs[DDIR_READ], td->o.min_bs[DDIR_WRITE]);
Jens Axboe21415db2013-01-04 13:09:29 +01001004 file_size = min(f->real_file_size, f->io_size);
Jens Axboe9c6f6312012-11-07 09:15:45 +01001005
Jens Axboe21415db2013-01-04 13:09:29 +01001006 nranges = (file_size + range_size - 1) / range_size;
Jens Axboe9c6f6312012-11-07 09:15:45 +01001007
Jens Axboe23162962012-11-07 19:47:47 +01001008 seed = jhash(f->file_name, strlen(f->file_name), 0) * td->thread_number;
Jens Axboe84256872012-11-17 10:06:36 -07001009 if (!td->o.rand_repeatable)
1010 seed = td->rand_seeds[4];
1011
Jens Axboe9c6f6312012-11-07 09:15:45 +01001012 if (td->o.random_distribution == FIO_RAND_DIST_ZIPF)
Jens Axboe888677a2013-04-10 22:19:46 +02001013 zipf_init(&f->zipf, nranges, td->o.zipf_theta.u.f, seed);
Jens Axboe9c6f6312012-11-07 09:15:45 +01001014 else
Jens Axboe888677a2013-04-10 22:19:46 +02001015 pareto_init(&f->zipf, nranges, td->o.pareto_h.u.f, seed);
Jens Axboe9c6f6312012-11-07 09:15:45 +01001016
1017 return 1;
1018}
1019
1020static int init_rand_distribution(struct thread_data *td)
1021{
1022 struct fio_file *f;
1023 unsigned int i;
1024 int state;
1025
1026 if (td->o.random_distribution == FIO_RAND_DIST_RANDOM)
1027 return 0;
1028
Jens Axboe8edd9732014-03-01 08:24:03 -07001029 state = td_bump_runstate(td, TD_SETTING_UP);
1030
Jens Axboe9c6f6312012-11-07 09:15:45 +01001031 for_each_file(td, f, i)
1032 __init_rand_distribution(td, f);
Jens Axboe8edd9732014-03-01 08:24:03 -07001033
1034 td_restore_runstate(td, state);
Jens Axboe9c6f6312012-11-07 09:15:45 +01001035
1036 return 1;
1037}
1038
Jens Axboe68727072007-03-15 20:49:25 +01001039int init_random_map(struct thread_data *td)
1040{
Jens Axboe51ede0b2012-11-22 13:50:29 +01001041 unsigned long long blocks;
Jens Axboe68727072007-03-15 20:49:25 +01001042 struct fio_file *f;
1043 unsigned int i;
1044
Jens Axboe9c6f6312012-11-07 09:15:45 +01001045 if (init_rand_distribution(td))
1046 return 0;
Jens Axboe3831a842012-11-26 19:59:14 +01001047 if (!td_random(td))
Jens Axboe68727072007-03-15 20:49:25 +01001048 return 0;
1049
1050 for_each_file(td, f, i) {
Jens Axboe38f30c82013-01-11 14:03:05 +01001051 uint64_t file_size = min(f->real_file_size, f->io_size);
1052
Jens Axboe53737ae2013-02-21 15:18:17 +01001053 blocks = file_size / (unsigned long long) td->o.rw_min_bs;
1054
Jens Axboe8055e412012-11-26 08:43:47 +01001055 if (td->o.random_generator == FIO_RAND_GEN_LFSR) {
Jens Axboe82af46b2012-12-04 13:12:25 +01001056 unsigned long seed;
1057
1058 seed = td->rand_seeds[FIO_RAND_BLOCK_OFF];
Bruce Cran9c0f3f32014-04-28 15:54:43 -06001059
Jens Axboe225ba9e2014-02-26 14:31:15 -08001060 if (!lfsr_init(&f->lfsr, blocks, seed, 0))
Jens Axboe8055e412012-11-26 08:43:47 +01001061 continue;
Jens Axboe3831a842012-11-26 19:59:14 +01001062 } else if (!td->o.norandommap) {
Jens Axboe7ebd7962012-11-28 21:24:46 +01001063 f->io_axmap = axmap_new(blocks);
1064 if (f->io_axmap)
Jens Axboe8055e412012-11-26 08:43:47 +01001065 continue;
Jens Axboe1cad7122012-11-29 21:37:11 +01001066 } else if (td->o.norandommap)
1067 continue;
Jens Axboeceadd592012-02-02 08:41:28 +01001068
Jens Axboe2b386d22008-03-26 10:32:57 +01001069 if (!td->o.softrandommap) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001070 log_err("fio: failed allocating random map. If running"
1071 " a large number of jobs, try the 'norandommap'"
Jens Axboe2b386d22008-03-26 10:32:57 +01001072 " option or set 'softrandommap'. Or give"
1073 " a larger --alloc-size to fio.\n");
Jens Axboe68727072007-03-15 20:49:25 +01001074 return 1;
1075 }
Jens Axboe303032a2008-03-26 10:11:10 +01001076
1077 log_info("fio: file %s failed allocating random map. Running "
1078 "job without.\n", f->file_name);
Jens Axboe68727072007-03-15 20:49:25 +01001079 }
1080
1081 return 0;
1082}
1083
Jens Axboe53cdc682006-10-18 11:50:58 +02001084void close_files(struct thread_data *td)
1085{
Jens Axboe0ab8db82006-10-18 17:16:23 +02001086 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +01001087 unsigned int i;
Jens Axboe53cdc682006-10-18 11:50:58 +02001088
Jens Axboe2be3eec2009-06-10 09:05:13 +02001089 for_each_file(td, f, i) {
1090 if (fio_file_open(f))
1091 td_io_close_file(td, f);
1092 }
Jens Axboe24ffd2c2008-03-01 15:47:08 +01001093}
1094
1095void close_and_free_files(struct thread_data *td)
1096{
1097 struct fio_file *f;
1098 unsigned int i;
1099
Jens Axboeee56ad52008-02-01 10:30:20 +01001100 dprint(FD_FILE, "close files\n");
1101
Jens Axboe0ab8db82006-10-18 17:16:23 +02001102 for_each_file(td, f, i) {
Castor Fu9187a262014-08-19 09:28:53 -07001103 if (td->o.unlink && f->filetype == FIO_TYPE_FILE) {
1104 dprint(FD_FILE, "free unlink %s\n", f->file_name);
1105 td_io_unlink_file(td, f);
1106 }
1107
Jens Axboe22a57ba2009-06-09 14:34:35 +02001108 if (fio_file_open(f))
1109 td_io_close_file(td, f);
1110
Shaozhi Shawn Yeb9fbcf22008-09-10 09:05:17 +02001111 remove_file_hash(f);
Jens Axboeb3dc7f02007-03-09 12:52:15 +01001112
Jens Axboeb5f4d8b2014-03-20 08:28:11 -06001113 if (td->o.unlink && f->filetype == FIO_TYPE_FILE) {
1114 dprint(FD_FILE, "free unlink %s\n", f->file_name);
Castor Fu9187a262014-08-19 09:28:53 -07001115 td_io_unlink_file(td, f);
Jens Axboeb5f4d8b2014-03-20 08:28:11 -06001116 }
1117
Jens Axboef17c4392008-03-01 18:40:46 +01001118 sfree(f->file_name);
Jens Axboefa1da862007-03-23 15:20:54 +01001119 f->file_name = NULL;
Jens Axboe7ebd7962012-11-28 21:24:46 +01001120 axmap_free(f->io_axmap);
1121 f->io_axmap = NULL;
Jens Axboe78d99e62008-03-01 18:41:50 +01001122 sfree(f);
Jens Axboe53cdc682006-10-18 11:50:58 +02001123 }
Jens Axboeb4a6a592006-10-20 13:54:47 +02001124
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001125 td->o.filename = NULL;
Jens Axboecade3ef2007-03-23 15:29:45 +01001126 free(td->files);
Jens Axboed7df1d12013-03-20 19:57:01 -06001127 free(td->file_locks);
Jens Axboe9efef3c2008-03-06 10:26:25 +01001128 td->files_index = 0;
Jens Axboeb4a6a592006-10-20 13:54:47 +02001129 td->files = NULL;
Jens Axboed7df1d12013-03-20 19:57:01 -06001130 td->file_locks = NULL;
Jens Axboe27ddbfa2014-01-22 16:27:32 -08001131 td->o.file_lock_mode = FILE_LOCK_NONE;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001132 td->o.nr_files = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +02001133}
Jens Axboeaf52b342007-03-13 10:07:47 +01001134
Jens Axboee3bab462007-03-13 11:22:09 +01001135static void get_file_type(struct fio_file *f)
Jens Axboeaf52b342007-03-13 10:07:47 +01001136{
1137 struct stat sb;
1138
Jens Axboe66159822007-04-16 21:54:24 +02001139 if (!strcmp(f->file_name, "-"))
1140 f->filetype = FIO_TYPE_PIPE;
1141 else
1142 f->filetype = FIO_TYPE_FILE;
Jens Axboeaf52b342007-03-13 10:07:47 +01001143
Bruce Cran38921822012-02-22 17:55:16 +00001144 /* \\.\ is the device namespace in Windows, where every file is
1145 * a block device */
1146 if (strncmp(f->file_name, "\\\\.\\", 4) == 0)
1147 f->filetype = FIO_TYPE_BD;
1148
Jens Axboeb30d3952010-02-06 23:36:26 +01001149 if (!stat(f->file_name, &sb)) {
Bruce Cran38921822012-02-22 17:55:16 +00001150 if (S_ISBLK(sb.st_mode))
Jens Axboeaf52b342007-03-13 10:07:47 +01001151 f->filetype = FIO_TYPE_BD;
1152 else if (S_ISCHR(sb.st_mode))
1153 f->filetype = FIO_TYPE_CHAR;
Jens Axboeb5605e92007-04-16 20:42:33 +02001154 else if (S_ISFIFO(sb.st_mode))
1155 f->filetype = FIO_TYPE_PIPE;
Jens Axboeaf52b342007-03-13 10:07:47 +01001156 }
1157}
1158
Jens Axboe90426232014-04-01 12:28:47 -06001159static int __is_already_allocated(const char *fname)
1160{
1161 struct flist_head *entry;
1162 char *filename;
1163
1164 if (flist_empty(&filename_list))
1165 return 0;
1166
1167 flist_for_each(entry, &filename_list) {
1168 filename = flist_entry(entry, struct file_name, list)->filename;
1169
1170 if (strcmp(filename, fname) == 0)
1171 return 1;
1172 }
1173
1174 return 0;
1175}
1176
1177static int is_already_allocated(const char *fname)
1178{
1179 int ret;
1180
1181 fio_file_hash_lock();
1182 ret = __is_already_allocated(fname);
1183 fio_file_hash_unlock();
1184 return ret;
1185}
1186
Castor Fu190b8f02014-03-20 10:59:29 -07001187static void set_already_allocated(const char *fname)
1188{
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08001189 struct file_name *fn;
1190
1191 fn = malloc(sizeof(struct file_name));
1192 fn->filename = strdup(fname);
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08001193
Jens Axboe90426232014-04-01 12:28:47 -06001194 fio_file_hash_lock();
1195 if (!__is_already_allocated(fname)) {
1196 flist_add_tail(&fn->list, &filename_list);
1197 fn = NULL;
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08001198 }
Jens Axboe90426232014-04-01 12:28:47 -06001199 fio_file_hash_unlock();
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08001200
Jens Axboe90426232014-04-01 12:28:47 -06001201 if (fn) {
1202 free(fn->filename);
1203 free(fn);
1204 }
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08001205}
1206
Jens Axboe90426232014-04-01 12:28:47 -06001207
Castor Fu190b8f02014-03-20 10:59:29 -07001208static void free_already_allocated(void)
1209{
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08001210 struct flist_head *entry, *tmp;
1211 struct file_name *fn;
1212
Jens Axboe90426232014-04-01 12:28:47 -06001213 if (flist_empty(&filename_list))
1214 return;
1215
1216 fio_file_hash_lock();
1217 flist_for_each_safe(entry, tmp, &filename_list) {
1218 fn = flist_entry(entry, struct file_name, list);
1219 free(fn->filename);
1220 flist_del(&fn->list);
1221 free(fn);
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08001222 }
Jens Axboe90426232014-04-01 12:28:47 -06001223
1224 fio_file_hash_unlock();
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08001225}
1226
Jens Axboe7b5cb702014-04-01 16:37:03 -06001227static struct fio_file *alloc_new_file(struct thread_data *td)
1228{
1229 struct fio_file *f;
1230
1231 f = smalloc(sizeof(*f));
1232 if (!f) {
1233 log_err("fio: smalloc OOM\n");
1234 assert(0);
1235 return NULL;
1236 }
1237
1238 f->fd = -1;
1239 f->shadow_fd = -1;
1240 fio_file_reset(td, f);
1241 return f;
1242}
1243
Jens Axboe5903e7b2014-02-26 13:42:13 -08001244int add_file(struct thread_data *td, const char *fname, int numjob, int inc)
Jens Axboeaf52b342007-03-13 10:07:47 +01001245{
Jens Axboe7b4e4fe2007-03-13 12:51:40 +01001246 int cur_files = td->files_index;
Jens Axboebd0ee742007-03-23 14:26:23 +01001247 char file_name[PATH_MAX];
Jens Axboeaf52b342007-03-13 10:07:47 +01001248 struct fio_file *f;
Jens Axboebd0ee742007-03-23 14:26:23 +01001249 int len = 0;
Jens Axboeaf52b342007-03-13 10:07:47 +01001250
Jens Axboeee56ad52008-02-01 10:30:20 +01001251 dprint(FD_FILE, "add file %s\n", fname);
1252
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08001253 if (td->o.directory)
1254 len = set_name_idx(file_name, td->o.directory, numjob);
1255
1256 sprintf(file_name + len, "%s", fname);
1257
1258 /* clean cloned siblings using existing files */
1259 if (numjob && is_already_allocated(file_name))
1260 return 0;
1261
Jens Axboe7b5cb702014-04-01 16:37:03 -06001262 f = alloc_new_file(td);
Jens Axboebd0ee742007-03-23 14:26:23 +01001263
Jens Axboefc99bc02009-03-04 15:27:42 +01001264 if (td->files_size <= td->files_index) {
Jianpeng Ma1983e322013-01-10 13:19:27 +01001265 unsigned int new_size = td->o.nr_files + 1;
Jens Axboe126d65c2008-03-01 18:04:31 +01001266
Jens Axboefc99bc02009-03-04 15:27:42 +01001267 dprint(FD_FILE, "resize file array to %d files\n", new_size);
1268
1269 td->files = realloc(td->files, new_size * sizeof(f));
Jianpeng Mad537c082013-01-11 08:52:55 +01001270 if (td->files == NULL) {
1271 log_err("fio: realloc OOM\n");
1272 assert(0);
1273 }
Jens Axboed7df1d12013-03-20 19:57:01 -06001274 if (td->o.file_lock_mode != FILE_LOCK_NONE) {
1275 td->file_locks = realloc(td->file_locks, new_size);
1276 if (!td->file_locks) {
1277 log_err("fio: realloc OOM\n");
1278 assert(0);
1279 }
1280 td->file_locks[cur_files] = FILE_LOCK_NONE;
1281 }
Jens Axboefc99bc02009-03-04 15:27:42 +01001282 td->files_size = new_size;
1283 }
Jens Axboe8bb76792009-03-04 15:37:52 +01001284 td->files[cur_files] = f;
Shaohua Li89ac1d42012-06-11 08:56:32 +02001285 f->fileno = cur_files;
Jens Axboe126d65c2008-03-01 18:04:31 +01001286
Jens Axboe07eb79d2007-04-12 13:02:38 +02001287 /*
1288 * init function, io engine may not be loaded yet
1289 */
1290 if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO))
1291 f->real_file_size = -1ULL;
1292
Jens Axboef17c4392008-03-01 18:40:46 +01001293 f->file_name = smalloc_strdup(file_name);
Jens Axboec48c0be2008-07-31 19:55:02 +02001294 if (!f->file_name) {
1295 log_err("fio: smalloc OOM\n");
1296 assert(0);
1297 }
Jens Axboe0b9d69e2009-09-11 22:29:54 +02001298
Jens Axboee3bab462007-03-13 11:22:09 +01001299 get_file_type(f);
Jens Axboeaf52b342007-03-13 10:07:47 +01001300
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001301 switch (td->o.file_lock_mode) {
1302 case FILE_LOCK_NONE:
1303 break;
1304 case FILE_LOCK_READWRITE:
Jens Axboed7df1d12013-03-20 19:57:01 -06001305 f->rwlock = fio_rwlock_init();
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001306 break;
1307 case FILE_LOCK_EXCLUSIVE:
Jens Axboe521da522012-08-02 11:21:36 +02001308 f->lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001309 break;
1310 default:
1311 log_err("fio: unknown lock mode: %d\n", td->o.file_lock_mode);
1312 assert(0);
1313 }
Jens Axboe29c13492008-03-01 19:25:20 +01001314
Jens Axboe7b4e4fe2007-03-13 12:51:40 +01001315 td->files_index++;
Jens Axboe15494412007-03-13 11:52:47 +01001316 if (f->filetype == FIO_TYPE_FILE)
1317 td->nr_normal_files++;
Jens Axboef29b25a2007-07-23 08:56:43 +02001318
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08001319 set_already_allocated(file_name);
1320
Jens Axboe5903e7b2014-02-26 13:42:13 -08001321 if (inc)
1322 td->o.nr_files++;
1323
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001324 dprint(FD_FILE, "file %p \"%s\" added at %d\n", f, f->file_name,
1325 cur_files);
Jens Axboe9efef3c2008-03-06 10:26:25 +01001326
Jens Axboef29b25a2007-07-23 08:56:43 +02001327 return cur_files;
Jens Axboeaf52b342007-03-13 10:07:47 +01001328}
Jens Axboe0ad920e2007-03-13 11:06:45 +01001329
Jens Axboe49ffb4a2010-08-24 15:01:37 +02001330int add_file_exclusive(struct thread_data *td, const char *fname)
1331{
1332 struct fio_file *f;
1333 unsigned int i;
1334
1335 for_each_file(td, f, i) {
1336 if (!strcmp(f->file_name, fname))
1337 return i;
1338 }
1339
Jens Axboe5903e7b2014-02-26 13:42:13 -08001340 return add_file(td, fname, 0, 1);
Jens Axboe49ffb4a2010-08-24 15:01:37 +02001341}
1342
Jens Axboe0ad920e2007-03-13 11:06:45 +01001343void get_file(struct fio_file *f)
1344{
Jens Axboe8172fe92008-02-01 21:51:02 +01001345 dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references);
Jens Axboed6aed792009-06-03 08:41:15 +02001346 assert(fio_file_open(f));
Jens Axboe0ad920e2007-03-13 11:06:45 +01001347 f->references++;
1348}
1349
Jens Axboe6977bcd2008-03-01 15:55:36 +01001350int put_file(struct thread_data *td, struct fio_file *f)
Jens Axboe0ad920e2007-03-13 11:06:45 +01001351{
Jens Axboe98e1ac42008-03-06 11:56:48 +01001352 int f_ret = 0, ret = 0;
Jens Axboe6977bcd2008-03-01 15:55:36 +01001353
Jens Axboe8172fe92008-02-01 21:51:02 +01001354 dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references);
Jens Axboeee56ad52008-02-01 10:30:20 +01001355
Jens Axboe22a57ba2009-06-09 14:34:35 +02001356 if (!fio_file_open(f)) {
1357 assert(f->fd == -1);
Jens Axboe6977bcd2008-03-01 15:55:36 +01001358 return 0;
Jens Axboe22a57ba2009-06-09 14:34:35 +02001359 }
Jens Axboe0ad920e2007-03-13 11:06:45 +01001360
1361 assert(f->references);
1362 if (--f->references)
Jens Axboe6977bcd2008-03-01 15:55:36 +01001363 return 0;
Jens Axboe0ad920e2007-03-13 11:06:45 +01001364
Jens Axboe71b84ca2014-04-14 12:01:45 -06001365 if (should_fsync(td) && td->o.fsync_on_close) {
Jens Axboe98e1ac42008-03-06 11:56:48 +01001366 f_ret = fsync(f->fd);
Jens Axboe71b84ca2014-04-14 12:01:45 -06001367 if (f_ret < 0)
1368 f_ret = errno;
1369 }
Jens Axboeebb14152007-03-13 14:42:15 +01001370
Jens Axboe0ad920e2007-03-13 11:06:45 +01001371 if (td->io_ops->close_file)
Jens Axboe6977bcd2008-03-01 15:55:36 +01001372 ret = td->io_ops->close_file(td, f);
Jens Axboe1020a132007-03-29 11:15:06 +02001373
Jens Axboe98e1ac42008-03-06 11:56:48 +01001374 if (!ret)
Jens Axboea5fb4612008-05-28 10:54:01 +02001375 ret = f_ret;
Jens Axboe98e1ac42008-03-06 11:56:48 +01001376
Jens Axboe0ad920e2007-03-13 11:06:45 +01001377 td->nr_open_files--;
Jens Axboed6aed792009-06-03 08:41:15 +02001378 fio_file_clear_open(f);
Jens Axboe22a57ba2009-06-09 14:34:35 +02001379 assert(f->fd == -1);
Jens Axboe6977bcd2008-03-01 15:55:36 +01001380 return ret;
Jens Axboe0ad920e2007-03-13 11:06:45 +01001381}
Jens Axboebbf6b542007-03-13 15:28:55 +01001382
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001383void lock_file(struct thread_data *td, struct fio_file *f, enum fio_ddir ddir)
Jens Axboeb2bd2bd2008-03-01 18:19:52 +01001384{
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001385 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1386 return;
Jens Axboe29c13492008-03-01 19:25:20 +01001387
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001388 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
1389 if (ddir == DDIR_READ)
Jens Axboed7df1d12013-03-20 19:57:01 -06001390 fio_rwlock_read(f->rwlock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001391 else
Jens Axboed7df1d12013-03-20 19:57:01 -06001392 fio_rwlock_write(f->rwlock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001393 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1394 fio_mutex_down(f->lock);
1395
Jens Axboed7df1d12013-03-20 19:57:01 -06001396 td->file_locks[f->fileno] = td->o.file_lock_mode;
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001397}
1398
1399void unlock_file(struct thread_data *td, struct fio_file *f)
1400{
1401 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1402 return;
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001403
Jens Axboed7df1d12013-03-20 19:57:01 -06001404 if (td->o.file_lock_mode == FILE_LOCK_READWRITE)
1405 fio_rwlock_unlock(f->rwlock);
1406 else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001407 fio_mutex_up(f->lock);
Jens Axboed7df1d12013-03-20 19:57:01 -06001408
1409 td->file_locks[f->fileno] = FILE_LOCK_NONE;
Jens Axboeb2bd2bd2008-03-01 18:19:52 +01001410}
1411
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001412void unlock_file_all(struct thread_data *td, struct fio_file *f)
Jens Axboeb2bd2bd2008-03-01 18:19:52 +01001413{
Jens Axboef2a28032014-02-11 09:12:06 -07001414 if (td->o.file_lock_mode == FILE_LOCK_NONE || !td->file_locks)
Jens Axboe27ddbfa2014-01-22 16:27:32 -08001415 return;
Jens Axboed7df1d12013-03-20 19:57:01 -06001416 if (td->file_locks[f->fileno] != FILE_LOCK_NONE)
1417 unlock_file(td, f);
Jens Axboeb2bd2bd2008-03-01 18:19:52 +01001418}
1419
Jens Axboebbf6b542007-03-13 15:28:55 +01001420static int recurse_dir(struct thread_data *td, const char *dirname)
1421{
1422 struct dirent *dir;
1423 int ret = 0;
1424 DIR *D;
1425
1426 D = opendir(dirname);
1427 if (!D) {
Jens Axboe0ddb2702007-03-27 19:43:53 +02001428 char buf[FIO_VERROR_SIZE];
1429
Ken Raeburn98ffb8f2013-01-30 22:31:09 +01001430 snprintf(buf, FIO_VERROR_SIZE, "opendir(%s)", dirname);
Jens Axboe0ddb2702007-03-27 19:43:53 +02001431 td_verror(td, errno, buf);
Jens Axboebbf6b542007-03-13 15:28:55 +01001432 return 1;
1433 }
1434
1435 while ((dir = readdir(D)) != NULL) {
1436 char full_path[PATH_MAX];
1437 struct stat sb;
1438
Jens Axboee85b2b82007-03-14 09:39:06 +01001439 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
1440 continue;
Jens Axboe96d32d52007-03-14 09:16:09 +01001441
Bruce Cranb9fd7882012-02-20 17:01:46 +00001442 sprintf(full_path, "%s%s%s", dirname, FIO_OS_PATH_SEPARATOR, dir->d_name);
Jens Axboebbf6b542007-03-13 15:28:55 +01001443
1444 if (lstat(full_path, &sb) == -1) {
1445 if (errno != ENOENT) {
1446 td_verror(td, errno, "stat");
Jens Axboe68ace9e2014-04-11 11:24:00 -06001447 ret = 1;
1448 break;
Jens Axboebbf6b542007-03-13 15:28:55 +01001449 }
1450 }
1451
1452 if (S_ISREG(sb.st_mode)) {
Jens Axboe5903e7b2014-02-26 13:42:13 -08001453 add_file(td, full_path, 0, 1);
Jens Axboebbf6b542007-03-13 15:28:55 +01001454 continue;
1455 }
Jens Axboe0ddb2702007-03-27 19:43:53 +02001456 if (!S_ISDIR(sb.st_mode))
1457 continue;
Jens Axboebbf6b542007-03-13 15:28:55 +01001458
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001459 ret = recurse_dir(td, full_path);
1460 if (ret)
Jens Axboebbf6b542007-03-13 15:28:55 +01001461 break;
1462 }
1463
1464 closedir(D);
1465 return ret;
1466}
1467
1468int add_dir_files(struct thread_data *td, const char *path)
1469{
Jens Axboe0ddb2702007-03-27 19:43:53 +02001470 int ret = recurse_dir(td, path);
1471
1472 if (!ret)
1473 log_info("fio: opendir added %d files\n", td->o.nr_files);
1474
1475 return ret;
Jens Axboebbf6b542007-03-13 15:28:55 +01001476}
Jens Axboecade3ef2007-03-23 15:29:45 +01001477
1478void dup_files(struct thread_data *td, struct thread_data *org)
1479{
1480 struct fio_file *f;
1481 unsigned int i;
Jens Axboe9efef3c2008-03-06 10:26:25 +01001482
1483 dprint(FD_FILE, "dup files: %d\n", org->files_index);
Jens Axboecade3ef2007-03-23 15:29:45 +01001484
1485 if (!org->files)
1486 return;
1487
Jens Axboe9efef3c2008-03-06 10:26:25 +01001488 td->files = malloc(org->files_index * sizeof(f));
Jens Axboecade3ef2007-03-23 15:29:45 +01001489
Jens Axboed7df1d12013-03-20 19:57:01 -06001490 if (td->o.file_lock_mode != FILE_LOCK_NONE)
1491 td->file_locks = malloc(org->files_index);
1492
Jens Axboe9efef3c2008-03-06 10:26:25 +01001493 for_each_file(org, f, i) {
Jens Axboeb0fe4212008-03-01 18:22:27 +01001494 struct fio_file *__f;
1495
Jens Axboe7b5cb702014-04-01 16:37:03 -06001496 __f = alloc_new_file(td);
Jens Axboe0b9d69e2009-09-11 22:29:54 +02001497
Aaron Carrollbc3456f2008-06-25 09:20:37 +02001498 if (f->file_name) {
Jens Axboef17c4392008-03-01 18:40:46 +01001499 __f->file_name = smalloc_strdup(f->file_name);
Jens Axboec48c0be2008-07-31 19:55:02 +02001500 if (!__f->file_name) {
1501 log_err("fio: smalloc OOM\n");
1502 assert(0);
1503 }
Jens Axboe0b9d69e2009-09-11 22:29:54 +02001504
Aaron Carrollbc3456f2008-06-25 09:20:37 +02001505 __f->filetype = f->filetype;
1506 }
Jens Axboeb0fe4212008-03-01 18:22:27 +01001507
Jens Axboe67ad9242014-02-07 10:56:15 -07001508 if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1509 __f->lock = f->lock;
1510 else if (td->o.file_lock_mode == FILE_LOCK_READWRITE)
1511 __f->rwlock = f->rwlock;
1512
Jens Axboeb0fe4212008-03-01 18:22:27 +01001513 td->files[i] = __f;
Jens Axboecade3ef2007-03-23 15:29:45 +01001514 }
1515}
Jens Axboef29b25a2007-07-23 08:56:43 +02001516
1517/*
1518 * Returns the index that matches the filename, or -1 if not there
1519 */
1520int get_fileno(struct thread_data *td, const char *fname)
1521{
1522 struct fio_file *f;
1523 unsigned int i;
1524
1525 for_each_file(td, f, i)
1526 if (!strcmp(f->file_name, fname))
1527 return i;
1528
1529 return -1;
1530}
1531
1532/*
1533 * For log usage, where we add/open/close files automatically
1534 */
1535void free_release_files(struct thread_data *td)
1536{
1537 close_files(td);
Jens Axboe4c8e9f32014-06-09 19:52:05 -06001538 td->o.nr_files = 0;
1539 td->o.open_files = 0;
Jens Axboef29b25a2007-07-23 08:56:43 +02001540 td->files_index = 0;
1541 td->nr_normal_files = 0;
1542}
Jens Axboe33c48812013-01-21 09:46:06 -07001543
1544void fio_file_reset(struct thread_data *td, struct fio_file *f)
1545{
1546 f->last_pos = f->file_offset;
1547 f->last_start = -1ULL;
1548 if (f->io_axmap)
1549 axmap_reset(f->io_axmap);
1550 if (td->o.random_generator == FIO_RAND_GEN_LFSR)
1551 lfsr_reset(&f->lfsr, td->rand_seeds[FIO_RAND_BLOCK_OFF]);
1552}
Jens Axboe002fe732014-02-11 08:31:13 -07001553
1554int fio_files_done(struct thread_data *td)
1555{
1556 struct fio_file *f;
1557 unsigned int i;
1558
1559 for_each_file(td, f, i)
1560 if (!fio_file_done(f))
1561 return 0;
1562
1563 return 1;
1564}
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08001565
1566/* free memory used in initialization phase only */
Castor Fu190b8f02014-03-20 10:59:29 -07001567void filesetup_mem_free(void)
1568{
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08001569 free_already_allocated();
1570}