blob: fd55cc4b2bc4c98723194a3a655d420f82bb6c79 [file] [log] [blame]
Jens Axboe53cdc682006-10-18 11:50:58 +02001#include <unistd.h>
2#include <fcntl.h>
3#include <string.h>
4#include <assert.h>
Jens Axboebbf6b542007-03-13 15:28:55 +01005#include <dirent.h>
YAMAMOTO Takashid74ac842010-06-09 09:44:53 +02006#include <libgen.h>
Jens Axboe53cdc682006-10-18 11:50:58 +02007#include <sys/stat.h>
8#include <sys/mman.h>
Jens Axboebbf6b542007-03-13 15:28:55 +01009#include <sys/types.h>
Jens Axboe53cdc682006-10-18 11:50:58 +020010
11#include "fio.h"
Jens Axboef17c4392008-03-01 18:40:46 +010012#include "smalloc.h"
Jens Axboe4906e0b2008-03-01 18:59:51 +010013#include "filehash.h"
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -080014#include "options.h"
Bruce Cranecc314b2011-01-04 10:59:30 +010015#include "os/os.h"
Jens Axboe23162962012-11-07 19:47:47 +010016#include "hash.h"
Jens Axboe7ebd7962012-11-28 21:24:46 +010017#include "lib/axmap.h"
Jens Axboe53cdc682006-10-18 11:50:58 +020018
Jens Axboe97ac9922013-01-24 15:00:25 -070019#ifdef CONFIG_LINUX_FALLOCATE
Eric Gourioua596f042011-06-17 09:11:45 +020020#include <linux/falloc.h>
21#endif
22
Jens Axboe7172cfe2007-07-23 14:36:00 +020023static int root_warn;
24
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -080025static FLIST_HEAD(filename_list);
26
Jens Axboec592b9f2009-06-03 09:29:28 +020027static inline void clear_error(struct thread_data *td)
28{
29 td->error = 0;
30 td->verror[0] = '\0';
31}
32
Jens Axboe3baddf22008-04-04 11:10:30 +020033/*
34 * Leaves f->fd open on success, caller must close
35 */
Jens Axboe7bb48f82007-03-27 15:30:28 +020036static int extend_file(struct thread_data *td, struct fio_file *f)
Jens Axboe25205e92006-10-19 20:50:14 +020037{
Jens Axboeea443652007-03-29 14:52:13 +020038 int r, new_layout = 0, unlink_file = 0, flags;
Jens Axboe25205e92006-10-19 20:50:14 +020039 unsigned long long left;
40 unsigned int bs;
41 char *b;
Jens Axboeb2a15192006-10-18 14:10:42 +020042
Jens Axboe4241ea82007-09-12 08:18:36 +020043 if (read_only) {
44 log_err("fio: refusing extend of file due to read-only\n");
45 return 0;
46 }
47
Jens Axboe507a7022007-03-27 15:52:46 +020048 /*
49 * check if we need to lay the file out complete again. fio
50 * does that for operations involving reads, or for writes
51 * where overwrite is set
52 */
Jens Axboe1417dae2014-03-20 09:33:13 -060053 if (td_read(td) ||
54 (td_write(td) && td->o.overwrite && !td->o.file_append) ||
Jens Axboeffdfabd2008-03-26 09:18:14 +010055 (td_write(td) && td->io_ops->flags & FIO_NOEXTEND))
Jens Axboe507a7022007-03-27 15:52:46 +020056 new_layout = 1;
Jens Axboe1417dae2014-03-20 09:33:13 -060057 if (td_write(td) && !td->o.overwrite && !td->o.file_append)
Jens Axboeea443652007-03-29 14:52:13 +020058 unlink_file = 1;
Jens Axboe507a7022007-03-27 15:52:46 +020059
Jens Axboe6ae1f572008-02-21 10:20:00 +010060 if (unlink_file || new_layout) {
Jens Axboebd199f22008-03-26 09:57:18 +010061 dprint(FD_FILE, "layout unlink %s\n", f->file_name);
Zhang, Yanmin982016d2008-02-26 15:35:52 +010062 if ((unlink(f->file_name) < 0) && (errno != ENOENT)) {
Jens Axboe7bb48f82007-03-27 15:30:28 +020063 td_verror(td, errno, "unlink");
64 return 1;
65 }
66 }
67
Jens Axboe507a7022007-03-27 15:52:46 +020068 flags = O_WRONLY | O_CREAT;
69 if (new_layout)
70 flags |= O_TRUNC;
71
Jens Axboeee56ad52008-02-01 10:30:20 +010072 dprint(FD_FILE, "open file %s, flags %x\n", f->file_name, flags);
Jens Axboe507a7022007-03-27 15:52:46 +020073 f->fd = open(f->file_name, flags, 0644);
Jens Axboe53cdc682006-10-18 11:50:58 +020074 if (f->fd < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +010075 td_verror(td, errno, "open");
Jens Axboe53cdc682006-10-18 11:50:58 +020076 return 1;
77 }
78
Jens Axboe97ac9922013-01-24 15:00:25 -070079#ifdef CONFIG_POSIX_FALLOCATE
Eric Gourioua596f042011-06-17 09:11:45 +020080 if (!td->o.fill_device) {
81 switch (td->o.fallocate_mode) {
82 case FIO_FALLOCATE_NONE:
83 break;
84 case FIO_FALLOCATE_POSIX:
85 dprint(FD_FILE, "posix_fallocate file %s size %llu\n",
Jens Axboe4b91ee82013-02-25 10:18:33 +010086 f->file_name,
87 (unsigned long long) f->real_file_size);
Jens Axboe7bc8c2c2010-01-28 11:31:31 +010088
Eric Gourioua596f042011-06-17 09:11:45 +020089 r = posix_fallocate(f->fd, 0, f->real_file_size);
90 if (r > 0) {
91 log_err("fio: posix_fallocate fails: %s\n",
92 strerror(r));
93 }
94 break;
Jens Axboe97ac9922013-01-24 15:00:25 -070095#ifdef CONFIG_LINUX_FALLOCATE
Eric Gourioua596f042011-06-17 09:11:45 +020096 case FIO_FALLOCATE_KEEP_SIZE:
97 dprint(FD_FILE,
98 "fallocate(FALLOC_FL_KEEP_SIZE) "
Jens Axboe4b91ee82013-02-25 10:18:33 +010099 "file %s size %llu\n", f->file_name,
100 (unsigned long long) f->real_file_size);
Eric Gourioua596f042011-06-17 09:11:45 +0200101
102 r = fallocate(f->fd, FALLOC_FL_KEEP_SIZE, 0,
103 f->real_file_size);
Jens Axboe888677a2013-04-10 22:19:46 +0200104 if (r != 0)
Eric Gourioua596f042011-06-17 09:11:45 +0200105 td_verror(td, errno, "fallocate");
Jens Axboe888677a2013-04-10 22:19:46 +0200106
Eric Gourioua596f042011-06-17 09:11:45 +0200107 break;
Jens Axboe97ac9922013-01-24 15:00:25 -0700108#endif /* CONFIG_LINUX_FALLOCATE */
Eric Gourioua596f042011-06-17 09:11:45 +0200109 default:
110 log_err("fio: unknown fallocate mode: %d\n",
111 td->o.fallocate_mode);
112 assert(0);
Jens Axboe7bc8c2c2010-01-28 11:31:31 +0100113 }
114 }
Jens Axboe97ac9922013-01-24 15:00:25 -0700115#endif /* CONFIG_POSIX_FALLOCATE */
Bruce Cran9b836562011-01-08 19:49:54 +0100116
Shawn Lewisfc74ac12008-01-11 09:45:11 +0100117 if (!new_layout)
118 goto done;
119
Jens Axboe5e0074c2009-06-02 21:40:09 +0200120 /*
121 * The size will be -1ULL when fill_device is used, so don't truncate
122 * or fallocate this file, just write it
123 */
124 if (!td->o.fill_device) {
125 dprint(FD_FILE, "truncate file %s, size %llu\n", f->file_name,
Jens Axboe4b91ee82013-02-25 10:18:33 +0100126 (unsigned long long) f->real_file_size);
Jens Axboe5e0074c2009-06-02 21:40:09 +0200127 if (ftruncate(f->fd, f->real_file_size) == -1) {
John3cd4c662013-12-29 19:20:35 -0700128 if (errno != EFBIG) {
129 td_verror(td, errno, "ftruncate");
130 goto err;
131 }
Jens Axboe5e0074c2009-06-02 21:40:09 +0200132 }
Jens Axboe5e0074c2009-06-02 21:40:09 +0200133 }
Jens Axboe40f82982006-10-25 11:21:05 +0200134
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100135 b = malloc(td->o.max_bs[DDIR_WRITE]);
Jens Axboe53cdc682006-10-18 11:50:58 +0200136
Jens Axboe7bb48f82007-03-27 15:30:28 +0200137 left = f->real_file_size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200138 while (left && !td->terminate) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100139 bs = td->o.max_bs[DDIR_WRITE];
Jens Axboe53cdc682006-10-18 11:50:58 +0200140 if (bs > left)
141 bs = left;
142
Jens Axboecc86c392013-05-03 15:12:33 +0200143 fill_io_buffer(td, b, bs, bs);
144
Jens Axboe53cdc682006-10-18 11:50:58 +0200145 r = write(f->fd, b, bs);
146
Jens Axboe5e0074c2009-06-02 21:40:09 +0200147 if (r > 0) {
148 left -= r;
Jens Axboe53cdc682006-10-18 11:50:58 +0200149 continue;
150 } else {
Jens Axboe5e0074c2009-06-02 21:40:09 +0200151 if (r < 0) {
152 int __e = errno;
153
154 if (__e == ENOSPC) {
155 if (td->o.fill_device)
156 break;
157 log_info("fio: ENOSPC on laying out "
158 "file, stopping\n");
159 break;
160 }
Jens Axboee1161c32007-02-22 19:36:48 +0100161 td_verror(td, errno, "write");
Jens Axboe5e0074c2009-06-02 21:40:09 +0200162 } else
Jens Axboee1161c32007-02-22 19:36:48 +0100163 td_verror(td, EIO, "write");
Jens Axboe53cdc682006-10-18 11:50:58 +0200164
165 break;
166 }
167 }
168
Jens Axboeffdfabd2008-03-26 09:18:14 +0100169 if (td->terminate) {
170 dprint(FD_FILE, "terminate unlink %s\n", f->file_name);
Jens Axboe53cdc682006-10-18 11:50:58 +0200171 unlink(f->file_name);
Jens Axboeffdfabd2008-03-26 09:18:14 +0100172 } else if (td->o.create_fsync) {
Jens Axboe98e1ac42008-03-06 11:56:48 +0100173 if (fsync(f->fd) < 0) {
174 td_verror(td, errno, "fsync");
175 goto err;
176 }
177 }
Jens Axboe0d1cd202009-06-05 22:11:52 +0200178 if (td->o.fill_device && !td_write(td)) {
Jens Axboed6aed792009-06-03 08:41:15 +0200179 fio_file_clear_size_known(f);
Jens Axboe5e0074c2009-06-02 21:40:09 +0200180 if (td_io_get_file_size(td, f))
181 goto err;
182 if (f->io_size > f->real_file_size)
183 f->io_size = f->real_file_size;
184 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200185
186 free(b);
Jens Axboe507a7022007-03-27 15:52:46 +0200187done:
Jens Axboe53cdc682006-10-18 11:50:58 +0200188 return 0;
189err:
190 close(f->fd);
191 f->fd = -1;
192 return 1;
193}
194
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200195static int pre_read_file(struct thread_data *td, struct fio_file *f)
196{
Jens Axboeb0f65862009-05-20 11:52:15 +0200197 int r, did_open = 0, old_runstate;
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200198 unsigned long long left;
199 unsigned int bs;
200 char *b;
201
Jens Axboe9c0d2242009-07-01 12:26:28 +0200202 if (td->io_ops->flags & FIO_PIPEIO)
203 return 0;
204
Jens Axboed6aed792009-06-03 08:41:15 +0200205 if (!fio_file_open(f)) {
Jens Axboeb0f65862009-05-20 11:52:15 +0200206 if (td->io_ops->open_file(td, f)) {
207 log_err("fio: cannot pre-read, failed to open file\n");
208 return 1;
209 }
210 did_open = 1;
211 }
212
Jens Axboe8edd9732014-03-01 08:24:03 -0700213 old_runstate = td_bump_runstate(td, TD_PRE_READING);
Jens Axboeb0f65862009-05-20 11:52:15 +0200214
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200215 bs = td->o.max_bs[DDIR_READ];
216 b = malloc(bs);
217 memset(b, 0, bs);
218
219 lseek(f->fd, f->file_offset, SEEK_SET);
220 left = f->io_size;
221
222 while (left && !td->terminate) {
223 if (bs > left)
224 bs = left;
225
226 r = read(f->fd, b, bs);
227
228 if (r == (int) bs) {
229 left -= bs;
230 continue;
231 } else {
232 td_verror(td, EIO, "pre_read");
233 break;
234 }
235 }
236
Jens Axboe8edd9732014-03-01 08:24:03 -0700237 td_restore_runstate(td, old_runstate);
Jens Axboeb0f65862009-05-20 11:52:15 +0200238
239 if (did_open)
240 td->io_ops->close_file(td, f);
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200241 free(b);
242 return 0;
243}
244
Jens Axboe7bb48f82007-03-27 15:30:28 +0200245static unsigned long long get_rand_file_size(struct thread_data *td)
Jens Axboe9c60ce62007-03-15 09:14:47 +0100246{
Jens Axboedc873b62008-06-04 20:13:04 +0200247 unsigned long long ret, sized;
Jens Axboe1294c3e2011-05-11 08:15:18 +0200248 unsigned long r;
Jens Axboe9c60ce62007-03-15 09:14:47 +0100249
Jens Axboe4c07ad82011-03-28 09:51:09 +0200250 if (td->o.use_os_rand) {
251 r = os_random_long(&td->file_size_state);
252 sized = td->o.file_size_high - td->o.file_size_low;
253 ret = (unsigned long long) ((double) sized * (r / (OS_RAND_MAX + 1.0)));
254 } else {
255 r = __rand(&td->__file_size_state);
256 sized = td->o.file_size_high - td->o.file_size_low;
257 ret = (unsigned long long) ((double) sized * (r / (FRAND_MAX + 1.0)));
258 }
259
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100260 ret += td->o.file_size_low;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100261 ret -= (ret % td->o.rw_min_bs);
Jens Axboe9c60ce62007-03-15 09:14:47 +0100262 return ret;
263}
264
Jens Axboe53cdc682006-10-18 11:50:58 +0200265static int file_size(struct thread_data *td, struct fio_file *f)
266{
267 struct stat st;
268
Jens Axboedf9c26b2009-03-05 10:13:58 +0100269 if (stat(f->file_name, &st) == -1) {
Jens Axboe7bb48f82007-03-27 15:30:28 +0200270 td_verror(td, errno, "fstat");
271 return 1;
272 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200273
Jens Axboe7bb48f82007-03-27 15:30:28 +0200274 f->real_file_size = st.st_size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200275 return 0;
276}
277
278static int bdev_size(struct thread_data *td, struct fio_file *f)
279{
Bruce Cran9b836562011-01-08 19:49:54 +0100280 unsigned long long bytes = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200281 int r;
282
Jens Axboedf9c26b2009-03-05 10:13:58 +0100283 if (td->io_ops->open_file(td, f)) {
284 log_err("fio: failed opening blockdev %s for size check\n",
285 f->file_name);
286 return 1;
287 }
288
Bruce Cranecc314b2011-01-04 10:59:30 +0100289 r = blockdev_size(f, &bytes);
Jens Axboe53cdc682006-10-18 11:50:58 +0200290 if (r) {
Jens Axboee1161c32007-02-22 19:36:48 +0100291 td_verror(td, r, "blockdev_size");
Jens Axboedf9c26b2009-03-05 10:13:58 +0100292 goto err;
Jens Axboe53cdc682006-10-18 11:50:58 +0200293 }
294
Jens Axboe7ab077a2009-02-02 15:07:37 +0100295 if (!bytes) {
296 log_err("%s: zero sized block device?\n", f->file_name);
Jens Axboedf9c26b2009-03-05 10:13:58 +0100297 goto err;
Jens Axboe7ab077a2009-02-02 15:07:37 +0100298 }
299
Jens Axboe53cdc682006-10-18 11:50:58 +0200300 f->real_file_size = bytes;
Jens Axboe22a57ba2009-06-09 14:34:35 +0200301 td->io_ops->close_file(td, f);
Jens Axboe53cdc682006-10-18 11:50:58 +0200302 return 0;
Jens Axboedf9c26b2009-03-05 10:13:58 +0100303err:
304 td->io_ops->close_file(td, f);
305 return 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200306}
307
Jens Axboe4ccdccd2010-06-24 10:27:22 +0200308static int char_size(struct thread_data *td, struct fio_file *f)
309{
310#ifdef FIO_HAVE_CHARDEV_SIZE
Bruce Cran9b836562011-01-08 19:49:54 +0100311 unsigned long long bytes = 0;
Jens Axboe4ccdccd2010-06-24 10:27:22 +0200312 int r;
313
314 if (td->io_ops->open_file(td, f)) {
315 log_err("fio: failed opening blockdev %s for size check\n",
316 f->file_name);
317 return 1;
318 }
319
Bruce Cranecc314b2011-01-04 10:59:30 +0100320 r = chardev_size(f, &bytes);
Jens Axboe4ccdccd2010-06-24 10:27:22 +0200321 if (r) {
322 td_verror(td, r, "chardev_size");
323 goto err;
324 }
325
326 if (!bytes) {
327 log_err("%s: zero sized char device?\n", f->file_name);
328 goto err;
329 }
330
331 f->real_file_size = bytes;
332 td->io_ops->close_file(td, f);
333 return 0;
334err:
335 td->io_ops->close_file(td, f);
336 return 1;
337#else
338 f->real_file_size = -1ULL;
339 return 0;
340#endif
341}
342
Jens Axboe53cdc682006-10-18 11:50:58 +0200343static int get_file_size(struct thread_data *td, struct fio_file *f)
344{
345 int ret = 0;
346
Jens Axboed6aed792009-06-03 08:41:15 +0200347 if (fio_file_size_known(f))
Jens Axboe409b3412007-03-28 09:57:01 +0200348 return 0;
349
Jens Axboe7bb48f82007-03-27 15:30:28 +0200350 if (f->filetype == FIO_TYPE_FILE)
351 ret = file_size(td, f);
352 else if (f->filetype == FIO_TYPE_BD)
Jens Axboe53cdc682006-10-18 11:50:58 +0200353 ret = bdev_size(td, f);
Jens Axboe4ccdccd2010-06-24 10:27:22 +0200354 else if (f->filetype == FIO_TYPE_CHAR)
355 ret = char_size(td, f);
Jens Axboe53cdc682006-10-18 11:50:58 +0200356 else
357 f->real_file_size = -1;
358
359 if (ret)
360 return ret;
361
362 if (f->file_offset > f->real_file_size) {
Bruce Cran0f2152c2010-12-15 10:33:03 +0100363 log_err("%s: offset extends end (%llu > %llu)\n", td->o.name,
Jens Axboe4e0a8fa2013-04-15 11:40:57 +0200364 (unsigned long long) f->file_offset,
365 (unsigned long long) f->real_file_size);
Jens Axboe53cdc682006-10-18 11:50:58 +0200366 return 1;
367 }
368
Jens Axboed6aed792009-06-03 08:41:15 +0200369 fio_file_set_size_known(f);
Jens Axboe53cdc682006-10-18 11:50:58 +0200370 return 0;
371}
372
Jens Axboe3baddf22008-04-04 11:10:30 +0200373static int __file_invalidate_cache(struct thread_data *td, struct fio_file *f,
374 unsigned long long off,
375 unsigned long long len)
Jens Axboee5b401d2006-10-18 16:03:40 +0200376{
377 int ret = 0;
378
Jens Axboe5e0074c2009-06-02 21:40:09 +0200379 if (len == -1ULL)
Jens Axboe3baddf22008-04-04 11:10:30 +0200380 len = f->io_size;
381 if (off == -1ULL)
382 off = f->file_offset;
Jens Axboeee56ad52008-02-01 10:30:20 +0100383
Jens Axboe0d1cd202009-06-05 22:11:52 +0200384 if (len == -1ULL || off == -1ULL)
385 return 0;
386
Jens Axboe3baddf22008-04-04 11:10:30 +0200387 dprint(FD_IO, "invalidate cache %s: %llu/%llu\n", f->file_name, off,
388 len);
Jens Axboeb5af8292007-03-08 12:43:13 +0100389
Jens Axboee5b401d2006-10-18 16:03:40 +0200390 /*
391 * FIXME: add blockdev flushing too
392 */
Jens Axboea1c58072009-08-04 23:17:02 +0200393 if (f->mmap_ptr) {
Bruce Cran03e20d62011-01-02 20:14:54 +0100394 ret = posix_madvise(f->mmap_ptr, f->mmap_sz, POSIX_MADV_DONTNEED);
Jens Axboea1c58072009-08-04 23:17:02 +0200395#ifdef FIO_MADV_FREE
Jens Axboe3e10fb82013-08-09 14:31:06 -0600396 if (f->filetype == FIO_TYPE_BD)
397 (void) posix_madvise(f->mmap_ptr, f->mmap_sz, FIO_MADV_FREE);
Jens Axboea1c58072009-08-04 23:17:02 +0200398#endif
399 } else if (f->filetype == FIO_TYPE_FILE) {
Bruce Cranecc314b2011-01-04 10:59:30 +0100400 ret = posix_fadvise(f->fd, off, len, POSIX_FADV_DONTNEED);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100401 } else if (f->filetype == FIO_TYPE_BD) {
Bruce Cranecc314b2011-01-04 10:59:30 +0100402 ret = blockdev_invalidate_cache(f);
Jens Axboe7e0e25c2007-03-27 12:49:56 +0200403 if (ret < 0 && errno == EACCES && geteuid()) {
Jens Axboe7172cfe2007-07-23 14:36:00 +0200404 if (!root_warn) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100405 log_err("fio: only root may flush block "
406 "devices. Cache flush bypassed!\n");
Jens Axboe7172cfe2007-07-23 14:36:00 +0200407 root_warn = 1;
408 }
Jens Axboe7e0e25c2007-03-27 12:49:56 +0200409 ret = 0;
410 }
Jens Axboeb5605e92007-04-16 20:42:33 +0200411 } else if (f->filetype == FIO_TYPE_CHAR || f->filetype == FIO_TYPE_PIPE)
Jens Axboee5b401d2006-10-18 16:03:40 +0200412 ret = 0;
413
414 if (ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100415 td_verror(td, errno, "invalidate_cache");
Jens Axboee5b401d2006-10-18 16:03:40 +0200416 return 1;
Jens Axboe3baddf22008-04-04 11:10:30 +0200417 } else if (ret > 0) {
418 td_verror(td, ret, "invalidate_cache");
419 return 1;
Jens Axboee5b401d2006-10-18 16:03:40 +0200420 }
421
Jens Axboead2da602007-02-26 12:33:27 +0100422 return ret;
Jens Axboe3baddf22008-04-04 11:10:30 +0200423
424}
425
426int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
427{
Jens Axboed6aed792009-06-03 08:41:15 +0200428 if (!fio_file_open(f))
Jens Axboea5fb4612008-05-28 10:54:01 +0200429 return 0;
430
Jens Axboe5e0074c2009-06-02 21:40:09 +0200431 return __file_invalidate_cache(td, f, -1ULL, -1ULL);
Jens Axboee5b401d2006-10-18 16:03:40 +0200432}
433
Jens Axboe6977bcd2008-03-01 15:55:36 +0100434int generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
Jens Axboe53cdc682006-10-18 11:50:58 +0200435{
Jens Axboe6977bcd2008-03-01 15:55:36 +0100436 int ret = 0;
437
Jens Axboeee56ad52008-02-01 10:30:20 +0100438 dprint(FD_FILE, "fd close %s\n", f->file_name);
Jens Axboe4906e0b2008-03-01 18:59:51 +0100439
440 remove_file_hash(f);
441
Jens Axboe6977bcd2008-03-01 15:55:36 +0100442 if (close(f->fd) < 0)
443 ret = errno;
444
Jens Axboeb5af8292007-03-08 12:43:13 +0100445 f->fd = -1;
Jens Axboee6c4d732012-12-12 08:16:27 +0100446
447 if (f->shadow_fd != -1) {
448 close(f->shadow_fd);
449 f->shadow_fd = -1;
450 }
451
Juan Casse57e54e02013-09-20 09:20:52 -0600452 f->engine_data = 0;
Jens Axboe6977bcd2008-03-01 15:55:36 +0100453 return ret;
Jens Axboe53cdc682006-10-18 11:50:58 +0200454}
455
Dmitry Monakhov1ccc6dc2012-09-19 23:22:53 +0400456int file_lookup_open(struct fio_file *f, int flags)
Jens Axboe53cdc682006-10-18 11:50:58 +0200457{
Jens Axboe29c13492008-03-01 19:25:20 +0100458 struct fio_file *__f;
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100459 int from_hash;
460
461 __f = lookup_file_hash(f->file_name);
462 if (__f) {
Jens Axboe9efef3c2008-03-06 10:26:25 +0100463 dprint(FD_FILE, "found file in hash %s\n", f->file_name);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100464 /*
465 * racy, need the __f->lock locked
466 */
467 f->lock = __f->lock;
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100468 from_hash = 1;
469 } else {
Jens Axboe9efef3c2008-03-06 10:26:25 +0100470 dprint(FD_FILE, "file not found in hash %s\n", f->file_name);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100471 from_hash = 0;
472 }
473
Jens Axboee8670ef2008-03-06 12:06:30 +0100474 f->fd = open(f->file_name, flags, 0600);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100475 return from_hash;
476}
477
Jens Axboee6c4d732012-12-12 08:16:27 +0100478static int file_close_shadow_fds(struct thread_data *td)
479{
480 struct fio_file *f;
481 int num_closed = 0;
482 unsigned int i;
483
484 for_each_file(td, f, i) {
485 if (f->shadow_fd == -1)
486 continue;
487
488 close(f->shadow_fd);
489 f->shadow_fd = -1;
490 num_closed++;
491 }
492
493 return num_closed;
494}
495
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100496int generic_open_file(struct thread_data *td, struct fio_file *f)
497{
Jens Axboe66159822007-04-16 21:54:24 +0200498 int is_std = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200499 int flags = 0;
Jens Axboe29c13492008-03-01 19:25:20 +0100500 int from_hash = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200501
Jens Axboeee56ad52008-02-01 10:30:20 +0100502 dprint(FD_FILE, "fd open %s\n", f->file_name);
503
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200504 if (td_trim(td) && f->filetype != FIO_TYPE_BD) {
505 log_err("fio: trim only applies to block device\n");
506 return 1;
507 }
508
Jens Axboe66159822007-04-16 21:54:24 +0200509 if (!strcmp(f->file_name, "-")) {
510 if (td_rw(td)) {
511 log_err("fio: can't read/write to stdin/out\n");
512 return 1;
513 }
514 is_std = 1;
Jens Axboece98fa62007-04-17 13:27:32 +0200515
516 /*
517 * move output logging to stderr, if we are writing to stdout
518 */
519 if (td_write(td))
520 f_out = stderr;
Jens Axboe66159822007-04-16 21:54:24 +0200521 }
522
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200523 if (td_trim(td))
524 goto skip_flags;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100525 if (td->o.odirect)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100526 flags |= OS_O_DIRECT;
Chris Masond01612f2013-11-15 15:52:58 -0700527 if (td->o.oatomic) {
528 if (!FIO_O_ATOMIC) {
529 td_verror(td, EINVAL, "OS does not support atomic IO");
530 return 1;
531 }
532 flags |= OS_O_DIRECT | FIO_O_ATOMIC;
533 }
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100534 if (td->o.sync_io)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100535 flags |= O_SYNC;
Jens Axboe814452b2009-03-04 12:53:13 +0100536 if (td->o.create_on_open)
537 flags |= O_CREAT;
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200538skip_flags:
539 if (f->filetype != FIO_TYPE_FILE)
540 flags |= FIO_O_NOATIME;
Jens Axboe7abf8332006-11-23 15:01:19 +0100541
Aaron Carroll056f3452007-11-26 09:08:53 +0100542open_again:
Jens Axboe660a1cb2007-04-17 15:37:47 +0200543 if (td_write(td)) {
Jens Axboe17308152008-03-07 13:42:31 +0100544 if (!read_only)
545 flags |= O_RDWR;
Jens Axboe53cdc682006-10-18 11:50:58 +0200546
Jens Axboeaf52b342007-03-13 10:07:47 +0100547 if (f->filetype == FIO_TYPE_FILE)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100548 flags |= O_CREAT;
Jens Axboe2fd233b2007-03-02 08:44:25 +0100549
Jens Axboe66159822007-04-16 21:54:24 +0200550 if (is_std)
551 f->fd = dup(STDOUT_FILENO);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100552 else
553 from_hash = file_lookup_open(f, flags);
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200554 } else if (td_read(td)) {
Jens Axboe4241ea82007-09-12 08:18:36 +0200555 if (f->filetype == FIO_TYPE_CHAR && !read_only)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100556 flags |= O_RDWR;
557 else
558 flags |= O_RDONLY;
559
Jens Axboe66159822007-04-16 21:54:24 +0200560 if (is_std)
561 f->fd = dup(STDIN_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 { //td trim
565 flags |= O_RDWR;
566 from_hash = file_lookup_open(f, flags);
Jens Axboe53cdc682006-10-18 11:50:58 +0200567 }
568
569 if (f->fd == -1) {
Jens Axboee4e33252007-03-21 13:07:54 +0100570 char buf[FIO_VERROR_SIZE];
Jens Axboee1161c32007-02-22 19:36:48 +0100571 int __e = errno;
572
Jens Axboe835d9b92009-06-09 15:23:38 +0200573 if (__e == EPERM && (flags & FIO_O_NOATIME)) {
Jens Axboe5921e802008-05-30 15:02:38 +0200574 flags &= ~FIO_O_NOATIME;
Aaron Carroll056f3452007-11-26 09:08:53 +0100575 goto open_again;
576 }
Jens Axboee6c4d732012-12-12 08:16:27 +0100577 if (__e == EMFILE && file_close_shadow_fds(td))
578 goto open_again;
Aaron Carroll056f3452007-11-26 09:08:53 +0100579
Ken Raeburn98ffb8f2013-01-30 22:31:09 +0100580 snprintf(buf, sizeof(buf), "open(%s)", f->file_name);
Jens Axboee4e33252007-03-21 13:07:54 +0100581
Jens Axboea93c5f02012-06-11 08:53:53 +0200582 if (__e == EINVAL && (flags & OS_O_DIRECT)) {
583 log_err("fio: looks like your file system does not " \
584 "support direct=1/buffered=0\n");
585 }
586
Jens Axboee4e33252007-03-21 13:07:54 +0100587 td_verror(td, __e, buf);
Jens Axboe53cdc682006-10-18 11:50:58 +0200588 }
589
Jens Axboe29c13492008-03-01 19:25:20 +0100590 if (!from_hash && f->fd != -1) {
591 if (add_file_hash(f)) {
Jens Axboe3f0ca9b2011-05-24 21:36:24 +0200592 int fio_unused ret;
Jens Axboe29c13492008-03-01 19:25:20 +0100593
594 /*
Jens Axboee6c4d732012-12-12 08:16:27 +0100595 * Stash away descriptor for later close. This is to
596 * work-around a "feature" on Linux, where a close of
597 * an fd that has been opened for write will trigger
598 * udev to call blkid to check partitions, fs id, etc.
Anatol Pomozovde8f6de2013-09-26 16:31:34 -0700599 * That pollutes the device cache, which can slow down
Jens Axboee6c4d732012-12-12 08:16:27 +0100600 * unbuffered accesses.
Jens Axboe29c13492008-03-01 19:25:20 +0100601 */
Jens Axboee6c4d732012-12-12 08:16:27 +0100602 if (f->shadow_fd == -1)
603 f->shadow_fd = f->fd;
604 else {
605 /*
606 * OK to ignore, we haven't done anything
607 * with it
608 */
609 ret = generic_close_file(td, f);
610 }
Jens Axboe29c13492008-03-01 19:25:20 +0100611 goto open_again;
612 }
613 }
Jens Axboe4906e0b2008-03-01 18:59:51 +0100614
Jens Axboe53cdc682006-10-18 11:50:58 +0200615 return 0;
Jens Axboeb5af8292007-03-08 12:43:13 +0100616}
617
Jens Axboedf9c26b2009-03-05 10:13:58 +0100618int generic_get_file_size(struct thread_data *td, struct fio_file *f)
Jens Axboe21972cd2006-11-23 12:39:16 +0100619{
Jens Axboedf9c26b2009-03-05 10:13:58 +0100620 return get_file_size(td, f);
Jens Axboe21972cd2006-11-23 12:39:16 +0100621}
622
Jens Axboe7bb48f82007-03-27 15:30:28 +0200623/*
624 * open/close all files, so that ->real_file_size gets set
625 */
Jens Axboebab3fd52007-04-02 10:34:18 +0200626static int get_file_sizes(struct thread_data *td)
Jens Axboe53cdc682006-10-18 11:50:58 +0200627{
628 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100629 unsigned int i;
Jens Axboebab3fd52007-04-02 10:34:18 +0200630 int err = 0;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200631
632 for_each_file(td, f, i) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100633 dprint(FD_FILE, "get file size for %p/%d/%p\n", f, i,
634 f->file_name);
Jens Axboe9efef3c2008-03-06 10:26:25 +0100635
Jens Axboe99a47c62009-04-07 22:20:56 +0200636 if (td_io_get_file_size(td, f)) {
Jens Axboe40b44f42007-04-11 12:43:35 +0200637 if (td->error != ENOENT) {
638 log_err("%s\n", td->verror);
639 err = 1;
640 }
Jens Axboe541d66d2007-03-27 19:50:11 +0200641 clear_error(td);
Jens Axboe07eb79d2007-04-12 13:02:38 +0200642 }
Jens Axboe409b3412007-03-28 09:57:01 +0200643
644 if (f->real_file_size == -1ULL && td->o.size)
645 f->real_file_size = td->o.size / td->o.nr_files;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200646 }
Jens Axboebab3fd52007-04-02 10:34:18 +0200647
648 return err;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200649}
650
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200651struct fio_mount {
652 struct flist_head list;
653 const char *base;
654 char __base[256];
655 unsigned int key;
656};
657
658/*
659 * Get free number of bytes for each file on each unique mount.
660 */
661static unsigned long long get_fs_free_counts(struct thread_data *td)
662{
663 struct flist_head *n, *tmp;
Jens Axboe68b03162010-06-24 09:22:41 +0200664 unsigned long long ret = 0;
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200665 struct fio_mount *fm;
666 FLIST_HEAD(list);
667 struct fio_file *f;
668 unsigned int i;
669
670 for_each_file(td, f, i) {
671 struct stat sb;
672 char buf[256];
673
Jens Axboe4ccdccd2010-06-24 10:27:22 +0200674 if (f->filetype == FIO_TYPE_BD || f->filetype == FIO_TYPE_CHAR) {
675 if (f->real_file_size != -1ULL)
676 ret += f->real_file_size;
Jens Axboe68b03162010-06-24 09:22:41 +0200677 continue;
678 } else if (f->filetype != FIO_TYPE_FILE)
679 continue;
680
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200681 strcpy(buf, f->file_name);
682
683 if (stat(buf, &sb) < 0) {
684 if (errno != ENOENT)
685 break;
686 strcpy(buf, ".");
687 if (stat(buf, &sb) < 0)
688 break;
689 }
690
691 fm = NULL;
692 flist_for_each(n, &list) {
693 fm = flist_entry(n, struct fio_mount, list);
694 if (fm->key == sb.st_dev)
695 break;
696
697 fm = NULL;
698 }
699
700 if (fm)
701 continue;
702
703 fm = malloc(sizeof(*fm));
704 strcpy(fm->__base, buf);
705 fm->base = basename(fm->__base);
706 fm->key = sb.st_dev;
707 flist_add(&fm->list, &list);
708 }
709
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200710 flist_for_each_safe(n, tmp, &list) {
711 unsigned long long sz;
712
713 fm = flist_entry(n, struct fio_mount, list);
714 flist_del(&fm->list);
715
716 sz = get_fs_size(fm->base);
717 if (sz && sz != -1ULL)
718 ret += sz;
719
720 free(fm);
721 }
722
723 return ret;
724}
725
Jens Axboebedc9dc2014-03-17 12:51:09 -0600726uint64_t get_start_offset(struct thread_data *td, struct fio_file *f)
Dan Ehrenbergce95d652012-08-16 08:58:21 +0200727{
Jens Axboebedc9dc2014-03-17 12:51:09 -0600728 struct thread_options *o = &td->o;
729
730 if (o->file_append && f->filetype == FIO_TYPE_FILE)
731 return f->real_file_size;
732
Dan Ehrenbergce95d652012-08-16 08:58:21 +0200733 return td->o.start_offset +
734 (td->thread_number - 1) * td->o.offset_increment;
735}
736
Jens Axboe7bb48f82007-03-27 15:30:28 +0200737/*
738 * Open the files and setup files sizes, creating files if necessary.
739 */
740int setup_files(struct thread_data *td)
741{
742 unsigned long long total_size, extend_size;
Jens Axboede98bd32013-04-05 11:09:20 +0200743 struct thread_options *o = &td->o;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200744 struct fio_file *f;
Jens Axboe002fe732014-02-11 08:31:13 -0700745 unsigned int i, nr_fs_extra = 0;
Jens Axboe000b0802007-03-27 15:56:10 +0200746 int err = 0, need_extend;
Jens Axboee90391e2013-04-13 20:40:53 +0200747 int old_state;
Jens Axboe002fe732014-02-11 08:31:13 -0700748 const unsigned int bs = td_min_bs(td);
749 uint64_t fs = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200750
Jens Axboeee56ad52008-02-01 10:30:20 +0100751 dprint(FD_FILE, "setup files\n");
752
Jens Axboe8edd9732014-03-01 08:24:03 -0700753 old_state = td_bump_runstate(td, TD_SETTING_UP);
Jens Axboee90391e2013-04-13 20:40:53 +0200754
Jens Axboede98bd32013-04-05 11:09:20 +0200755 if (o->read_iolog_file)
Jens Axboe25460cf2012-05-02 13:58:02 +0200756 goto done;
Jens Axboe691c8fb2008-03-07 14:26:26 +0100757
Jens Axboe53cdc682006-10-18 11:50:58 +0200758 /*
759 * if ioengine defines a setup() method, it's responsible for
Jens Axboe7bb48f82007-03-27 15:30:28 +0200760 * opening the files and setting f->real_file_size to indicate
761 * the valid range for that file.
Jens Axboe53cdc682006-10-18 11:50:58 +0200762 */
763 if (td->io_ops->setup)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200764 err = td->io_ops->setup(td);
765 else
Jens Axboebab3fd52007-04-02 10:34:18 +0200766 err = get_file_sizes(td);
Jens Axboe53cdc682006-10-18 11:50:58 +0200767
Jens Axboef1027062006-10-20 10:14:01 +0200768 if (err)
Jens Axboee90391e2013-04-13 20:40:53 +0200769 goto err_out;
Jens Axboef1027062006-10-20 10:14:01 +0200770
Jens Axboe0a7eb122006-10-20 10:36:42 +0200771 /*
Jens Axboe7bb48f82007-03-27 15:30:28 +0200772 * check sizes. if the files/devices do not exist and the size
773 * isn't passed to fio, abort.
Jens Axboe0a7eb122006-10-20 10:36:42 +0200774 */
Jens Axboe7bb48f82007-03-27 15:30:28 +0200775 total_size = 0;
776 for_each_file(td, f, i) {
777 if (f->real_file_size == -1ULL)
778 total_size = -1ULL;
779 else
780 total_size += f->real_file_size;
781 }
Jens Axboe0a7eb122006-10-20 10:36:42 +0200782
Jens Axboede98bd32013-04-05 11:09:20 +0200783 if (o->fill_device)
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200784 td->fill_device_size = get_fs_free_counts(td);
785
Jens Axboe7bb48f82007-03-27 15:30:28 +0200786 /*
787 * device/file sizes are zero and no size given, punt
788 */
Jens Axboede98bd32013-04-05 11:09:20 +0200789 if ((!total_size || total_size == -1ULL) && !o->size &&
790 !(td->io_ops->flags & FIO_NOIO) && !o->fill_device &&
791 !(o->nr_files && (o->file_size_low || o->file_size_high))) {
792 log_err("%s: you need to specify size=\n", o->name);
Jens Axboee1161c32007-02-22 19:36:48 +0100793 td_verror(td, EINVAL, "total_file_size");
Jens Axboee90391e2013-04-13 20:40:53 +0200794 goto err_out;
Jens Axboe53cdc682006-10-18 11:50:58 +0200795 }
796
Jens Axboe7bb48f82007-03-27 15:30:28 +0200797 /*
Jens Axboe002fe732014-02-11 08:31:13 -0700798 * Calculate per-file size and potential extra size for the
799 * first files, if needed.
800 */
801 if (!o->file_size_low) {
802 uint64_t all_fs;
803
804 fs = o->size / o->nr_files;
805 all_fs = fs * o->nr_files;
806
807 if (all_fs < o->size)
808 nr_fs_extra = (o->size - all_fs) / bs;
809 }
810
811 /*
Jens Axboe7bb48f82007-03-27 15:30:28 +0200812 * now file sizes are known, so we can set ->io_size. if size= is
813 * not given, ->io_size is just equal to ->real_file_size. if size
814 * is given, ->io_size is size / nr_files.
815 */
816 extend_size = total_size = 0;
817 need_extend = 0;
818 for_each_file(td, f, i) {
Jens Axboebedc9dc2014-03-17 12:51:09 -0600819 f->file_offset = get_start_offset(td, f);
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200820
Jens Axboede98bd32013-04-05 11:09:20 +0200821 if (!o->file_size_low) {
Jens Axboe7bb48f82007-03-27 15:30:28 +0200822 /*
823 * no file size range given, file size is equal to
Jens Axboe002fe732014-02-11 08:31:13 -0700824 * total size divided by number of files. If that is
825 * zero, set it to the real file size. If the size
826 * doesn't divide nicely with the min blocksize,
827 * make the first files bigger.
Jens Axboe7bb48f82007-03-27 15:30:28 +0200828 */
Jens Axboe002fe732014-02-11 08:31:13 -0700829 f->io_size = fs;
830 if (nr_fs_extra) {
831 nr_fs_extra--;
832 f->io_size += bs;
833 }
834
Jens Axboe65bdb102008-01-24 13:13:12 +0100835 if (!f->io_size)
Jens Axboe273f8c92008-01-25 14:02:15 +0100836 f->io_size = f->real_file_size - f->file_offset;
Jens Axboede98bd32013-04-05 11:09:20 +0200837 } else if (f->real_file_size < o->file_size_low ||
838 f->real_file_size > o->file_size_high) {
839 if (f->file_offset > o->file_size_low)
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200840 goto err_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200841 /*
842 * file size given. if it's fixed, use that. if it's a
843 * range, generate a random size in-between.
844 */
Jens Axboede98bd32013-04-05 11:09:20 +0200845 if (o->file_size_low == o->file_size_high)
846 f->io_size = o->file_size_low - f->file_offset;
847 else {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100848 f->io_size = get_rand_file_size(td)
849 - f->file_offset;
850 }
Jens Axboe65bdb102008-01-24 13:13:12 +0100851 } else
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200852 f->io_size = f->real_file_size - f->file_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200853
854 if (f->io_size == -1ULL)
855 total_size = -1ULL;
Shaohua Li4d002562012-09-21 08:32:32 +0200856 else {
Jens Axboede98bd32013-04-05 11:09:20 +0200857 if (o->size_percent)
858 f->io_size = (f->io_size * o->size_percent) / 100;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200859 total_size += f->io_size;
Shaohua Li4d002562012-09-21 08:32:32 +0200860 }
Jens Axboe7bb48f82007-03-27 15:30:28 +0200861
862 if (f->filetype == FIO_TYPE_FILE &&
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200863 (f->io_size + f->file_offset) > f->real_file_size &&
Jens Axboe7bb48f82007-03-27 15:30:28 +0200864 !(td->io_ops->flags & FIO_DISKLESSIO)) {
Jens Axboede98bd32013-04-05 11:09:20 +0200865 if (!o->create_on_open) {
Jens Axboe814452b2009-03-04 12:53:13 +0100866 need_extend++;
867 extend_size += (f->io_size + f->file_offset);
868 } else
869 f->real_file_size = f->io_size + f->file_offset;
Jens Axboed6aed792009-06-03 08:41:15 +0200870 fio_file_set_extend(f);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100871 }
Jens Axboe7bb48f82007-03-27 15:30:28 +0200872 }
873
Jens Axboede98bd32013-04-05 11:09:20 +0200874 if (!o->size || o->size > total_size)
875 o->size = total_size;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200876
877 /*
878 * See if we need to extend some files
879 */
880 if (need_extend) {
881 temp_stall_ts = 1;
Jens Axboef3afa572012-09-17 13:34:16 +0200882 if (output_format == FIO_OUTPUT_NORMAL)
Shaozhi Shawn Yea7ba8c52008-09-10 09:04:18 +0200883 log_info("%s: Laying out IO file(s) (%u file(s) /"
Jens Axboede98bd32013-04-05 11:09:20 +0200884 " %lluMB)\n", o->name, need_extend,
Shaozhi Shawn Yea7ba8c52008-09-10 09:04:18 +0200885 extend_size >> 20);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200886
887 for_each_file(td, f, i) {
Jens Axboe5e0074c2009-06-02 21:40:09 +0200888 unsigned long long old_len = -1ULL, extend_len = -1ULL;
Jens Axboe3baddf22008-04-04 11:10:30 +0200889
Jens Axboed6aed792009-06-03 08:41:15 +0200890 if (!fio_file_extend(f))
Jens Axboe7bb48f82007-03-27 15:30:28 +0200891 continue;
892
Jens Axboe409b3412007-03-28 09:57:01 +0200893 assert(f->filetype == FIO_TYPE_FILE);
Jens Axboed6aed792009-06-03 08:41:15 +0200894 fio_file_clear_extend(f);
Jens Axboede98bd32013-04-05 11:09:20 +0200895 if (!o->fill_device) {
Jens Axboe5e0074c2009-06-02 21:40:09 +0200896 old_len = f->real_file_size;
Jens Axboe0b9d69e2009-09-11 22:29:54 +0200897 extend_len = f->io_size + f->file_offset -
898 old_len;
Jens Axboe5e0074c2009-06-02 21:40:09 +0200899 }
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200900 f->real_file_size = (f->io_size + f->file_offset);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200901 err = extend_file(td, f);
902 if (err)
903 break;
Jens Axboe5e0074c2009-06-02 21:40:09 +0200904
Jens Axboe3baddf22008-04-04 11:10:30 +0200905 err = __file_invalidate_cache(td, f, old_len,
906 extend_len);
907 close(f->fd);
908 f->fd = -1;
909 if (err)
910 break;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200911 }
912 temp_stall_ts = 0;
913 }
914
915 if (err)
Jens Axboee90391e2013-04-13 20:40:53 +0200916 goto err_out;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200917
Jens Axboede98bd32013-04-05 11:09:20 +0200918 if (!o->zone_size)
919 o->zone_size = o->size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200920
Jens Axboeea966f82007-09-03 10:09:37 +0200921 /*
922 * iolog already set the total io size, if we read back
923 * stored entries.
924 */
Jens Axboede98bd32013-04-05 11:09:20 +0200925 if (!o->read_iolog_file)
926 td->total_io_size = o->size * o->loops;
Jens Axboe25460cf2012-05-02 13:58:02 +0200927
928done:
Jens Axboede98bd32013-04-05 11:09:20 +0200929 if (o->create_only)
Jens Axboe25460cf2012-05-02 13:58:02 +0200930 td->done = 1;
931
Jens Axboe8edd9732014-03-01 08:24:03 -0700932 td_restore_runstate(td, old_state);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200933 return 0;
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200934err_offset:
Jens Axboede98bd32013-04-05 11:09:20 +0200935 log_err("%s: you need to specify valid offset=\n", o->name);
Jens Axboee90391e2013-04-13 20:40:53 +0200936err_out:
Jens Axboe8edd9732014-03-01 08:24:03 -0700937 td_restore_runstate(td, old_state);
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200938 return 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200939}
940
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200941int pre_read_files(struct thread_data *td)
942{
943 struct fio_file *f;
944 unsigned int i;
945
946 dprint(FD_FILE, "pre_read files\n");
947
948 for_each_file(td, f, i) {
949 pre_read_file(td, f);
950 }
951
952 return 1;
953}
954
Jens Axboe9c6f6312012-11-07 09:15:45 +0100955static int __init_rand_distribution(struct thread_data *td, struct fio_file *f)
956{
Jens Axboe23162962012-11-07 19:47:47 +0100957 unsigned int range_size, seed;
Jens Axboe9c6f6312012-11-07 09:15:45 +0100958 unsigned long nranges;
Jens Axboe21415db2013-01-04 13:09:29 +0100959 uint64_t file_size;
Jens Axboe9c6f6312012-11-07 09:15:45 +0100960
961 range_size = min(td->o.min_bs[DDIR_READ], td->o.min_bs[DDIR_WRITE]);
Jens Axboe21415db2013-01-04 13:09:29 +0100962 file_size = min(f->real_file_size, f->io_size);
Jens Axboe9c6f6312012-11-07 09:15:45 +0100963
Jens Axboe21415db2013-01-04 13:09:29 +0100964 nranges = (file_size + range_size - 1) / range_size;
Jens Axboe9c6f6312012-11-07 09:15:45 +0100965
Jens Axboe23162962012-11-07 19:47:47 +0100966 seed = jhash(f->file_name, strlen(f->file_name), 0) * td->thread_number;
Jens Axboe84256872012-11-17 10:06:36 -0700967 if (!td->o.rand_repeatable)
968 seed = td->rand_seeds[4];
969
Jens Axboe9c6f6312012-11-07 09:15:45 +0100970 if (td->o.random_distribution == FIO_RAND_DIST_ZIPF)
Jens Axboe888677a2013-04-10 22:19:46 +0200971 zipf_init(&f->zipf, nranges, td->o.zipf_theta.u.f, seed);
Jens Axboe9c6f6312012-11-07 09:15:45 +0100972 else
Jens Axboe888677a2013-04-10 22:19:46 +0200973 pareto_init(&f->zipf, nranges, td->o.pareto_h.u.f, seed);
Jens Axboe9c6f6312012-11-07 09:15:45 +0100974
975 return 1;
976}
977
978static int init_rand_distribution(struct thread_data *td)
979{
980 struct fio_file *f;
981 unsigned int i;
982 int state;
983
984 if (td->o.random_distribution == FIO_RAND_DIST_RANDOM)
985 return 0;
986
Jens Axboe8edd9732014-03-01 08:24:03 -0700987 state = td_bump_runstate(td, TD_SETTING_UP);
988
Jens Axboe9c6f6312012-11-07 09:15:45 +0100989 for_each_file(td, f, i)
990 __init_rand_distribution(td, f);
Jens Axboe8edd9732014-03-01 08:24:03 -0700991
992 td_restore_runstate(td, state);
Jens Axboe9c6f6312012-11-07 09:15:45 +0100993
994 return 1;
995}
996
Jens Axboe68727072007-03-15 20:49:25 +0100997int init_random_map(struct thread_data *td)
998{
Jens Axboe51ede0b2012-11-22 13:50:29 +0100999 unsigned long long blocks;
Jens Axboe68727072007-03-15 20:49:25 +01001000 struct fio_file *f;
1001 unsigned int i;
1002
Jens Axboe9c6f6312012-11-07 09:15:45 +01001003 if (init_rand_distribution(td))
1004 return 0;
Jens Axboe3831a842012-11-26 19:59:14 +01001005 if (!td_random(td))
Jens Axboe68727072007-03-15 20:49:25 +01001006 return 0;
1007
1008 for_each_file(td, f, i) {
Jens Axboe38f30c82013-01-11 14:03:05 +01001009 uint64_t file_size = min(f->real_file_size, f->io_size);
1010
Jens Axboe53737ae2013-02-21 15:18:17 +01001011 blocks = file_size / (unsigned long long) td->o.rw_min_bs;
1012
Jens Axboe8055e412012-11-26 08:43:47 +01001013 if (td->o.random_generator == FIO_RAND_GEN_LFSR) {
Jens Axboe82af46b2012-12-04 13:12:25 +01001014 unsigned long seed;
1015
1016 seed = td->rand_seeds[FIO_RAND_BLOCK_OFF];
1017
Jens Axboe225ba9e2014-02-26 14:31:15 -08001018 if (!lfsr_init(&f->lfsr, blocks, seed, 0))
Jens Axboe8055e412012-11-26 08:43:47 +01001019 continue;
Jens Axboe3831a842012-11-26 19:59:14 +01001020 } else if (!td->o.norandommap) {
Jens Axboe7ebd7962012-11-28 21:24:46 +01001021 f->io_axmap = axmap_new(blocks);
1022 if (f->io_axmap)
Jens Axboe8055e412012-11-26 08:43:47 +01001023 continue;
Jens Axboe1cad7122012-11-29 21:37:11 +01001024 } else if (td->o.norandommap)
1025 continue;
Jens Axboeceadd592012-02-02 08:41:28 +01001026
Jens Axboe2b386d22008-03-26 10:32:57 +01001027 if (!td->o.softrandommap) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001028 log_err("fio: failed allocating random map. If running"
1029 " a large number of jobs, try the 'norandommap'"
Jens Axboe2b386d22008-03-26 10:32:57 +01001030 " option or set 'softrandommap'. Or give"
1031 " a larger --alloc-size to fio.\n");
Jens Axboe68727072007-03-15 20:49:25 +01001032 return 1;
1033 }
Jens Axboe303032a2008-03-26 10:11:10 +01001034
1035 log_info("fio: file %s failed allocating random map. Running "
1036 "job without.\n", f->file_name);
Jens Axboe68727072007-03-15 20:49:25 +01001037 }
1038
1039 return 0;
1040}
1041
Jens Axboe53cdc682006-10-18 11:50:58 +02001042void close_files(struct thread_data *td)
1043{
Jens Axboe0ab8db82006-10-18 17:16:23 +02001044 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +01001045 unsigned int i;
Jens Axboe53cdc682006-10-18 11:50:58 +02001046
Jens Axboe2be3eec2009-06-10 09:05:13 +02001047 for_each_file(td, f, i) {
1048 if (fio_file_open(f))
1049 td_io_close_file(td, f);
1050 }
Jens Axboe24ffd2c2008-03-01 15:47:08 +01001051}
1052
1053void close_and_free_files(struct thread_data *td)
1054{
1055 struct fio_file *f;
1056 unsigned int i;
1057
Jens Axboeee56ad52008-02-01 10:30:20 +01001058 dprint(FD_FILE, "close files\n");
1059
Jens Axboe0ab8db82006-10-18 17:16:23 +02001060 for_each_file(td, f, i) {
Jens Axboe22a57ba2009-06-09 14:34:35 +02001061 if (fio_file_open(f))
1062 td_io_close_file(td, f);
1063
Shaozhi Shawn Yeb9fbcf22008-09-10 09:05:17 +02001064 remove_file_hash(f);
Jens Axboeb3dc7f02007-03-09 12:52:15 +01001065
Jens Axboeb5f4d8b2014-03-20 08:28:11 -06001066 if (td->o.unlink && f->filetype == FIO_TYPE_FILE) {
1067 dprint(FD_FILE, "free unlink %s\n", f->file_name);
1068 unlink(f->file_name);
1069 }
1070
Jens Axboef17c4392008-03-01 18:40:46 +01001071 sfree(f->file_name);
Jens Axboefa1da862007-03-23 15:20:54 +01001072 f->file_name = NULL;
Jens Axboe7ebd7962012-11-28 21:24:46 +01001073 axmap_free(f->io_axmap);
1074 f->io_axmap = NULL;
Jens Axboe78d99e62008-03-01 18:41:50 +01001075 sfree(f);
Jens Axboe53cdc682006-10-18 11:50:58 +02001076 }
Jens Axboeb4a6a592006-10-20 13:54:47 +02001077
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001078 td->o.filename = NULL;
Jens Axboecade3ef2007-03-23 15:29:45 +01001079 free(td->files);
Jens Axboed7df1d12013-03-20 19:57:01 -06001080 free(td->file_locks);
Jens Axboe9efef3c2008-03-06 10:26:25 +01001081 td->files_index = 0;
Jens Axboeb4a6a592006-10-20 13:54:47 +02001082 td->files = NULL;
Jens Axboed7df1d12013-03-20 19:57:01 -06001083 td->file_locks = NULL;
Jens Axboe27ddbfa2014-01-22 16:27:32 -08001084 td->o.file_lock_mode = FILE_LOCK_NONE;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001085 td->o.nr_files = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +02001086}
Jens Axboeaf52b342007-03-13 10:07:47 +01001087
Jens Axboee3bab462007-03-13 11:22:09 +01001088static void get_file_type(struct fio_file *f)
Jens Axboeaf52b342007-03-13 10:07:47 +01001089{
1090 struct stat sb;
1091
Jens Axboe66159822007-04-16 21:54:24 +02001092 if (!strcmp(f->file_name, "-"))
1093 f->filetype = FIO_TYPE_PIPE;
1094 else
1095 f->filetype = FIO_TYPE_FILE;
Jens Axboeaf52b342007-03-13 10:07:47 +01001096
Bruce Cran38921822012-02-22 17:55:16 +00001097 /* \\.\ is the device namespace in Windows, where every file is
1098 * a block device */
1099 if (strncmp(f->file_name, "\\\\.\\", 4) == 0)
1100 f->filetype = FIO_TYPE_BD;
1101
Jens Axboeb30d3952010-02-06 23:36:26 +01001102 if (!stat(f->file_name, &sb)) {
Bruce Cran38921822012-02-22 17:55:16 +00001103 if (S_ISBLK(sb.st_mode))
Jens Axboeaf52b342007-03-13 10:07:47 +01001104 f->filetype = FIO_TYPE_BD;
1105 else if (S_ISCHR(sb.st_mode))
1106 f->filetype = FIO_TYPE_CHAR;
Jens Axboeb5605e92007-04-16 20:42:33 +02001107 else if (S_ISFIFO(sb.st_mode))
1108 f->filetype = FIO_TYPE_PIPE;
Jens Axboeaf52b342007-03-13 10:07:47 +01001109 }
1110}
1111
Castor Fu190b8f02014-03-20 10:59:29 -07001112static void set_already_allocated(const char *fname)
1113{
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08001114 struct file_name *fn;
1115
1116 fn = malloc(sizeof(struct file_name));
1117 fn->filename = strdup(fname);
1118 flist_add_tail(&fn->list, &filename_list);
1119}
1120
1121static int is_already_allocated(const char *fname)
1122{
1123 struct flist_head *entry;
1124 char *filename;
1125
1126 if (!flist_empty(&filename_list))
1127 {
1128 flist_for_each(entry, &filename_list) {
1129 filename = flist_entry(entry, struct file_name, list)->filename;
1130
1131 if (strcmp(filename, fname) == 0)
1132 return 1;
1133 }
1134 }
1135
1136 return 0;
1137}
1138
Castor Fu190b8f02014-03-20 10:59:29 -07001139static void free_already_allocated(void)
1140{
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08001141 struct flist_head *entry, *tmp;
1142 struct file_name *fn;
1143
1144 if (!flist_empty(&filename_list))
1145 {
1146 flist_for_each_safe(entry, tmp, &filename_list) {
1147 fn = flist_entry(entry, struct file_name, list);
1148 free(fn->filename);
1149 flist_del(&fn->list);
1150 free(fn);
1151 }
1152 }
1153}
1154
Jens Axboe5903e7b2014-02-26 13:42:13 -08001155int add_file(struct thread_data *td, const char *fname, int numjob, int inc)
Jens Axboeaf52b342007-03-13 10:07:47 +01001156{
Jens Axboe7b4e4fe2007-03-13 12:51:40 +01001157 int cur_files = td->files_index;
Jens Axboebd0ee742007-03-23 14:26:23 +01001158 char file_name[PATH_MAX];
Jens Axboeaf52b342007-03-13 10:07:47 +01001159 struct fio_file *f;
Jens Axboebd0ee742007-03-23 14:26:23 +01001160 int len = 0;
Jens Axboeaf52b342007-03-13 10:07:47 +01001161
Jens Axboeee56ad52008-02-01 10:30:20 +01001162 dprint(FD_FILE, "add file %s\n", fname);
1163
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08001164 if (td->o.directory)
1165 len = set_name_idx(file_name, td->o.directory, numjob);
1166
1167 sprintf(file_name + len, "%s", fname);
1168
1169 /* clean cloned siblings using existing files */
1170 if (numjob && is_already_allocated(file_name))
1171 return 0;
1172
Jens Axboef17c4392008-03-01 18:40:46 +01001173 f = smalloc(sizeof(*f));
Jens Axboec48c0be2008-07-31 19:55:02 +02001174 if (!f) {
1175 log_err("fio: smalloc OOM\n");
1176 assert(0);
1177 }
Jens Axboe0b9d69e2009-09-11 22:29:54 +02001178
Jens Axboeaf52b342007-03-13 10:07:47 +01001179 f->fd = -1;
Jens Axboee6c4d732012-12-12 08:16:27 +01001180 f->shadow_fd = -1;
Jens Axboe33c48812013-01-21 09:46:06 -07001181 fio_file_reset(td, f);
Jens Axboebd0ee742007-03-23 14:26:23 +01001182
Jens Axboefc99bc02009-03-04 15:27:42 +01001183 if (td->files_size <= td->files_index) {
Jianpeng Ma1983e322013-01-10 13:19:27 +01001184 unsigned int new_size = td->o.nr_files + 1;
Jens Axboe126d65c2008-03-01 18:04:31 +01001185
Jens Axboefc99bc02009-03-04 15:27:42 +01001186 dprint(FD_FILE, "resize file array to %d files\n", new_size);
1187
1188 td->files = realloc(td->files, new_size * sizeof(f));
Jianpeng Mad537c082013-01-11 08:52:55 +01001189 if (td->files == NULL) {
1190 log_err("fio: realloc OOM\n");
1191 assert(0);
1192 }
Jens Axboed7df1d12013-03-20 19:57:01 -06001193 if (td->o.file_lock_mode != FILE_LOCK_NONE) {
1194 td->file_locks = realloc(td->file_locks, new_size);
1195 if (!td->file_locks) {
1196 log_err("fio: realloc OOM\n");
1197 assert(0);
1198 }
1199 td->file_locks[cur_files] = FILE_LOCK_NONE;
1200 }
Jens Axboefc99bc02009-03-04 15:27:42 +01001201 td->files_size = new_size;
1202 }
Jens Axboe8bb76792009-03-04 15:37:52 +01001203 td->files[cur_files] = f;
Shaohua Li89ac1d42012-06-11 08:56:32 +02001204 f->fileno = cur_files;
Jens Axboe126d65c2008-03-01 18:04:31 +01001205
Jens Axboe07eb79d2007-04-12 13:02:38 +02001206 /*
1207 * init function, io engine may not be loaded yet
1208 */
1209 if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO))
1210 f->real_file_size = -1ULL;
1211
Jens Axboef17c4392008-03-01 18:40:46 +01001212 f->file_name = smalloc_strdup(file_name);
Jens Axboec48c0be2008-07-31 19:55:02 +02001213 if (!f->file_name) {
1214 log_err("fio: smalloc OOM\n");
1215 assert(0);
1216 }
Jens Axboe0b9d69e2009-09-11 22:29:54 +02001217
Jens Axboee3bab462007-03-13 11:22:09 +01001218 get_file_type(f);
Jens Axboeaf52b342007-03-13 10:07:47 +01001219
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001220 switch (td->o.file_lock_mode) {
1221 case FILE_LOCK_NONE:
1222 break;
1223 case FILE_LOCK_READWRITE:
Jens Axboed7df1d12013-03-20 19:57:01 -06001224 f->rwlock = fio_rwlock_init();
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001225 break;
1226 case FILE_LOCK_EXCLUSIVE:
Jens Axboe521da522012-08-02 11:21:36 +02001227 f->lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001228 break;
1229 default:
1230 log_err("fio: unknown lock mode: %d\n", td->o.file_lock_mode);
1231 assert(0);
1232 }
Jens Axboe29c13492008-03-01 19:25:20 +01001233
Jens Axboe7b4e4fe2007-03-13 12:51:40 +01001234 td->files_index++;
Jens Axboe15494412007-03-13 11:52:47 +01001235 if (f->filetype == FIO_TYPE_FILE)
1236 td->nr_normal_files++;
Jens Axboef29b25a2007-07-23 08:56:43 +02001237
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08001238 set_already_allocated(file_name);
1239
Jens Axboebcd27f72014-02-25 14:01:26 -08001240 /*
1241 * For adding files after the fact - if openfiles= isn't
1242 * given as an option, ensure we allow at least one file open
1243 */
1244 if (!td->o.open_files)
1245 td->o.open_files = 1;
1246
Jens Axboe5903e7b2014-02-26 13:42:13 -08001247 if (inc)
1248 td->o.nr_files++;
1249
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001250 dprint(FD_FILE, "file %p \"%s\" added at %d\n", f, f->file_name,
1251 cur_files);
Jens Axboe9efef3c2008-03-06 10:26:25 +01001252
Jens Axboef29b25a2007-07-23 08:56:43 +02001253 return cur_files;
Jens Axboeaf52b342007-03-13 10:07:47 +01001254}
Jens Axboe0ad920e2007-03-13 11:06:45 +01001255
Jens Axboe49ffb4a2010-08-24 15:01:37 +02001256int add_file_exclusive(struct thread_data *td, const char *fname)
1257{
1258 struct fio_file *f;
1259 unsigned int i;
1260
1261 for_each_file(td, f, i) {
1262 if (!strcmp(f->file_name, fname))
1263 return i;
1264 }
1265
Jens Axboe5903e7b2014-02-26 13:42:13 -08001266 return add_file(td, fname, 0, 1);
Jens Axboe49ffb4a2010-08-24 15:01:37 +02001267}
1268
Jens Axboe0ad920e2007-03-13 11:06:45 +01001269void get_file(struct fio_file *f)
1270{
Jens Axboe8172fe92008-02-01 21:51:02 +01001271 dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references);
Jens Axboed6aed792009-06-03 08:41:15 +02001272 assert(fio_file_open(f));
Jens Axboe0ad920e2007-03-13 11:06:45 +01001273 f->references++;
1274}
1275
Jens Axboe6977bcd2008-03-01 15:55:36 +01001276int put_file(struct thread_data *td, struct fio_file *f)
Jens Axboe0ad920e2007-03-13 11:06:45 +01001277{
Jens Axboe98e1ac42008-03-06 11:56:48 +01001278 int f_ret = 0, ret = 0;
Jens Axboe6977bcd2008-03-01 15:55:36 +01001279
Jens Axboe8172fe92008-02-01 21:51:02 +01001280 dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references);
Jens Axboeee56ad52008-02-01 10:30:20 +01001281
Jens Axboe22a57ba2009-06-09 14:34:35 +02001282 if (!fio_file_open(f)) {
1283 assert(f->fd == -1);
Jens Axboe6977bcd2008-03-01 15:55:36 +01001284 return 0;
Jens Axboe22a57ba2009-06-09 14:34:35 +02001285 }
Jens Axboe0ad920e2007-03-13 11:06:45 +01001286
1287 assert(f->references);
1288 if (--f->references)
Jens Axboe6977bcd2008-03-01 15:55:36 +01001289 return 0;
Jens Axboe0ad920e2007-03-13 11:06:45 +01001290
Jens Axboed424d4d2007-04-17 09:05:10 +02001291 if (should_fsync(td) && td->o.fsync_on_close)
Jens Axboe98e1ac42008-03-06 11:56:48 +01001292 f_ret = fsync(f->fd);
Jens Axboeebb14152007-03-13 14:42:15 +01001293
Jens Axboe0ad920e2007-03-13 11:06:45 +01001294 if (td->io_ops->close_file)
Jens Axboe6977bcd2008-03-01 15:55:36 +01001295 ret = td->io_ops->close_file(td, f);
Jens Axboe1020a132007-03-29 11:15:06 +02001296
Jens Axboe98e1ac42008-03-06 11:56:48 +01001297 if (!ret)
Jens Axboea5fb4612008-05-28 10:54:01 +02001298 ret = f_ret;
Jens Axboe98e1ac42008-03-06 11:56:48 +01001299
Jens Axboe0ad920e2007-03-13 11:06:45 +01001300 td->nr_open_files--;
Jens Axboed6aed792009-06-03 08:41:15 +02001301 fio_file_clear_open(f);
Jens Axboe22a57ba2009-06-09 14:34:35 +02001302 assert(f->fd == -1);
Jens Axboe6977bcd2008-03-01 15:55:36 +01001303 return ret;
Jens Axboe0ad920e2007-03-13 11:06:45 +01001304}
Jens Axboebbf6b542007-03-13 15:28:55 +01001305
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001306void lock_file(struct thread_data *td, struct fio_file *f, enum fio_ddir ddir)
Jens Axboeb2bd2bd2008-03-01 18:19:52 +01001307{
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001308 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1309 return;
Jens Axboe29c13492008-03-01 19:25:20 +01001310
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001311 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
1312 if (ddir == DDIR_READ)
Jens Axboed7df1d12013-03-20 19:57:01 -06001313 fio_rwlock_read(f->rwlock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001314 else
Jens Axboed7df1d12013-03-20 19:57:01 -06001315 fio_rwlock_write(f->rwlock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001316 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1317 fio_mutex_down(f->lock);
1318
Jens Axboed7df1d12013-03-20 19:57:01 -06001319 td->file_locks[f->fileno] = td->o.file_lock_mode;
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001320}
1321
1322void unlock_file(struct thread_data *td, struct fio_file *f)
1323{
1324 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1325 return;
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001326
Jens Axboed7df1d12013-03-20 19:57:01 -06001327 if (td->o.file_lock_mode == FILE_LOCK_READWRITE)
1328 fio_rwlock_unlock(f->rwlock);
1329 else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001330 fio_mutex_up(f->lock);
Jens Axboed7df1d12013-03-20 19:57:01 -06001331
1332 td->file_locks[f->fileno] = FILE_LOCK_NONE;
Jens Axboeb2bd2bd2008-03-01 18:19:52 +01001333}
1334
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001335void unlock_file_all(struct thread_data *td, struct fio_file *f)
Jens Axboeb2bd2bd2008-03-01 18:19:52 +01001336{
Jens Axboef2a28032014-02-11 09:12:06 -07001337 if (td->o.file_lock_mode == FILE_LOCK_NONE || !td->file_locks)
Jens Axboe27ddbfa2014-01-22 16:27:32 -08001338 return;
Jens Axboed7df1d12013-03-20 19:57:01 -06001339 if (td->file_locks[f->fileno] != FILE_LOCK_NONE)
1340 unlock_file(td, f);
Jens Axboeb2bd2bd2008-03-01 18:19:52 +01001341}
1342
Jens Axboebbf6b542007-03-13 15:28:55 +01001343static int recurse_dir(struct thread_data *td, const char *dirname)
1344{
1345 struct dirent *dir;
1346 int ret = 0;
1347 DIR *D;
1348
1349 D = opendir(dirname);
1350 if (!D) {
Jens Axboe0ddb2702007-03-27 19:43:53 +02001351 char buf[FIO_VERROR_SIZE];
1352
Ken Raeburn98ffb8f2013-01-30 22:31:09 +01001353 snprintf(buf, FIO_VERROR_SIZE, "opendir(%s)", dirname);
Jens Axboe0ddb2702007-03-27 19:43:53 +02001354 td_verror(td, errno, buf);
Jens Axboebbf6b542007-03-13 15:28:55 +01001355 return 1;
1356 }
1357
1358 while ((dir = readdir(D)) != NULL) {
1359 char full_path[PATH_MAX];
1360 struct stat sb;
1361
Jens Axboee85b2b82007-03-14 09:39:06 +01001362 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
1363 continue;
Jens Axboe96d32d52007-03-14 09:16:09 +01001364
Bruce Cranb9fd7882012-02-20 17:01:46 +00001365 sprintf(full_path, "%s%s%s", dirname, FIO_OS_PATH_SEPARATOR, dir->d_name);
Jens Axboebbf6b542007-03-13 15:28:55 +01001366
1367 if (lstat(full_path, &sb) == -1) {
1368 if (errno != ENOENT) {
1369 td_verror(td, errno, "stat");
1370 return 1;
1371 }
1372 }
1373
1374 if (S_ISREG(sb.st_mode)) {
Jens Axboe5903e7b2014-02-26 13:42:13 -08001375 add_file(td, full_path, 0, 1);
Jens Axboebbf6b542007-03-13 15:28:55 +01001376 continue;
1377 }
Jens Axboe0ddb2702007-03-27 19:43:53 +02001378 if (!S_ISDIR(sb.st_mode))
1379 continue;
Jens Axboebbf6b542007-03-13 15:28:55 +01001380
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001381 ret = recurse_dir(td, full_path);
1382 if (ret)
Jens Axboebbf6b542007-03-13 15:28:55 +01001383 break;
1384 }
1385
1386 closedir(D);
1387 return ret;
1388}
1389
1390int add_dir_files(struct thread_data *td, const char *path)
1391{
Jens Axboe0ddb2702007-03-27 19:43:53 +02001392 int ret = recurse_dir(td, path);
1393
1394 if (!ret)
1395 log_info("fio: opendir added %d files\n", td->o.nr_files);
1396
1397 return ret;
Jens Axboebbf6b542007-03-13 15:28:55 +01001398}
Jens Axboecade3ef2007-03-23 15:29:45 +01001399
1400void dup_files(struct thread_data *td, struct thread_data *org)
1401{
1402 struct fio_file *f;
1403 unsigned int i;
Jens Axboe9efef3c2008-03-06 10:26:25 +01001404
1405 dprint(FD_FILE, "dup files: %d\n", org->files_index);
Jens Axboecade3ef2007-03-23 15:29:45 +01001406
1407 if (!org->files)
1408 return;
1409
Jens Axboe9efef3c2008-03-06 10:26:25 +01001410 td->files = malloc(org->files_index * sizeof(f));
Jens Axboecade3ef2007-03-23 15:29:45 +01001411
Jens Axboed7df1d12013-03-20 19:57:01 -06001412 if (td->o.file_lock_mode != FILE_LOCK_NONE)
1413 td->file_locks = malloc(org->files_index);
1414
Jens Axboe9efef3c2008-03-06 10:26:25 +01001415 for_each_file(org, f, i) {
Jens Axboeb0fe4212008-03-01 18:22:27 +01001416 struct fio_file *__f;
1417
Jens Axboef17c4392008-03-01 18:40:46 +01001418 __f = smalloc(sizeof(*__f));
Jens Axboec48c0be2008-07-31 19:55:02 +02001419 if (!__f) {
1420 log_err("fio: smalloc OOM\n");
1421 assert(0);
1422 }
Jens Axboe22a57ba2009-06-09 14:34:35 +02001423 __f->fd = -1;
Jens Axboe4db205d2014-04-01 12:28:15 -06001424 __f->shadow_fd = -1;
Jens Axboe33c48812013-01-21 09:46:06 -07001425 fio_file_reset(td, __f);
Jens Axboe0b9d69e2009-09-11 22:29:54 +02001426
Aaron Carrollbc3456f2008-06-25 09:20:37 +02001427 if (f->file_name) {
Jens Axboef17c4392008-03-01 18:40:46 +01001428 __f->file_name = smalloc_strdup(f->file_name);
Jens Axboec48c0be2008-07-31 19:55:02 +02001429 if (!__f->file_name) {
1430 log_err("fio: smalloc OOM\n");
1431 assert(0);
1432 }
Jens Axboe0b9d69e2009-09-11 22:29:54 +02001433
Aaron Carrollbc3456f2008-06-25 09:20:37 +02001434 __f->filetype = f->filetype;
1435 }
Jens Axboeb0fe4212008-03-01 18:22:27 +01001436
Jens Axboe67ad9242014-02-07 10:56:15 -07001437 if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1438 __f->lock = f->lock;
1439 else if (td->o.file_lock_mode == FILE_LOCK_READWRITE)
1440 __f->rwlock = f->rwlock;
1441
Jens Axboeb0fe4212008-03-01 18:22:27 +01001442 td->files[i] = __f;
Jens Axboecade3ef2007-03-23 15:29:45 +01001443 }
1444}
Jens Axboef29b25a2007-07-23 08:56:43 +02001445
1446/*
1447 * Returns the index that matches the filename, or -1 if not there
1448 */
1449int get_fileno(struct thread_data *td, const char *fname)
1450{
1451 struct fio_file *f;
1452 unsigned int i;
1453
1454 for_each_file(td, f, i)
1455 if (!strcmp(f->file_name, fname))
1456 return i;
1457
1458 return -1;
1459}
1460
1461/*
1462 * For log usage, where we add/open/close files automatically
1463 */
1464void free_release_files(struct thread_data *td)
1465{
1466 close_files(td);
1467 td->files_index = 0;
1468 td->nr_normal_files = 0;
1469}
Jens Axboe33c48812013-01-21 09:46:06 -07001470
1471void fio_file_reset(struct thread_data *td, struct fio_file *f)
1472{
1473 f->last_pos = f->file_offset;
1474 f->last_start = -1ULL;
1475 if (f->io_axmap)
1476 axmap_reset(f->io_axmap);
1477 if (td->o.random_generator == FIO_RAND_GEN_LFSR)
1478 lfsr_reset(&f->lfsr, td->rand_seeds[FIO_RAND_BLOCK_OFF]);
1479}
Jens Axboe002fe732014-02-11 08:31:13 -07001480
1481int fio_files_done(struct thread_data *td)
1482{
1483 struct fio_file *f;
1484 unsigned int i;
1485
1486 for_each_file(td, f, i)
1487 if (!fio_file_done(f))
1488 return 0;
1489
1490 return 1;
1491}
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08001492
1493/* free memory used in initialization phase only */
Castor Fu190b8f02014-03-20 10:59:29 -07001494void filesetup_mem_free(void)
1495{
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08001496 free_already_allocated();
1497}