blob: 9fb325bd18b392f1c1419a01a6518b34da6d90fb [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"
Bruce Cranecc314b2011-01-04 10:59:30 +010014#include "os/os.h"
Jens Axboe23162962012-11-07 19:47:47 +010015#include "hash.h"
Jens Axboe7ebd7962012-11-28 21:24:46 +010016#include "lib/axmap.h"
Jens Axboe53cdc682006-10-18 11:50:58 +020017
Eric Gourioua596f042011-06-17 09:11:45 +020018#ifdef FIO_HAVE_LINUX_FALLOCATE
19#include <linux/falloc.h>
20#endif
21
Jens Axboe7172cfe2007-07-23 14:36:00 +020022static int root_warn;
23
Jens Axboec592b9f2009-06-03 09:29:28 +020024static inline void clear_error(struct thread_data *td)
25{
26 td->error = 0;
27 td->verror[0] = '\0';
28}
29
Jens Axboe3baddf22008-04-04 11:10:30 +020030/*
31 * Leaves f->fd open on success, caller must close
32 */
Jens Axboe7bb48f82007-03-27 15:30:28 +020033static int extend_file(struct thread_data *td, struct fio_file *f)
Jens Axboe25205e92006-10-19 20:50:14 +020034{
Jens Axboeea443652007-03-29 14:52:13 +020035 int r, new_layout = 0, unlink_file = 0, flags;
Jens Axboe25205e92006-10-19 20:50:14 +020036 unsigned long long left;
37 unsigned int bs;
38 char *b;
Jens Axboeb2a15192006-10-18 14:10:42 +020039
Jens Axboe4241ea82007-09-12 08:18:36 +020040 if (read_only) {
41 log_err("fio: refusing extend of file due to read-only\n");
42 return 0;
43 }
44
Jens Axboe507a7022007-03-27 15:52:46 +020045 /*
46 * check if we need to lay the file out complete again. fio
47 * does that for operations involving reads, or for writes
48 * where overwrite is set
49 */
Jens Axboeffdfabd2008-03-26 09:18:14 +010050 if (td_read(td) || (td_write(td) && td->o.overwrite) ||
51 (td_write(td) && td->io_ops->flags & FIO_NOEXTEND))
Jens Axboe507a7022007-03-27 15:52:46 +020052 new_layout = 1;
Jens Axboeea443652007-03-29 14:52:13 +020053 if (td_write(td) && !td->o.overwrite)
54 unlink_file = 1;
Jens Axboe507a7022007-03-27 15:52:46 +020055
Jens Axboe6ae1f572008-02-21 10:20:00 +010056 if (unlink_file || new_layout) {
Jens Axboebd199f22008-03-26 09:57:18 +010057 dprint(FD_FILE, "layout unlink %s\n", f->file_name);
Zhang, Yanmin982016d2008-02-26 15:35:52 +010058 if ((unlink(f->file_name) < 0) && (errno != ENOENT)) {
Jens Axboe7bb48f82007-03-27 15:30:28 +020059 td_verror(td, errno, "unlink");
60 return 1;
61 }
62 }
63
Jens Axboe507a7022007-03-27 15:52:46 +020064 flags = O_WRONLY | O_CREAT;
65 if (new_layout)
66 flags |= O_TRUNC;
67
Jens Axboeee56ad52008-02-01 10:30:20 +010068 dprint(FD_FILE, "open file %s, flags %x\n", f->file_name, flags);
Jens Axboe507a7022007-03-27 15:52:46 +020069 f->fd = open(f->file_name, flags, 0644);
Jens Axboe53cdc682006-10-18 11:50:58 +020070 if (f->fd < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +010071 td_verror(td, errno, "open");
Jens Axboe53cdc682006-10-18 11:50:58 +020072 return 1;
73 }
74
Jens Axboe7bc8c2c2010-01-28 11:31:31 +010075#ifdef FIO_HAVE_FALLOCATE
Eric Gourioua596f042011-06-17 09:11:45 +020076 if (!td->o.fill_device) {
77 switch (td->o.fallocate_mode) {
78 case FIO_FALLOCATE_NONE:
79 break;
80 case FIO_FALLOCATE_POSIX:
81 dprint(FD_FILE, "posix_fallocate file %s size %llu\n",
82 f->file_name, f->real_file_size);
Jens Axboe7bc8c2c2010-01-28 11:31:31 +010083
Eric Gourioua596f042011-06-17 09:11:45 +020084 r = posix_fallocate(f->fd, 0, f->real_file_size);
85 if (r > 0) {
86 log_err("fio: posix_fallocate fails: %s\n",
87 strerror(r));
88 }
89 break;
90#ifdef FIO_HAVE_LINUX_FALLOCATE
91 case FIO_FALLOCATE_KEEP_SIZE:
92 dprint(FD_FILE,
93 "fallocate(FALLOC_FL_KEEP_SIZE) "
94 "file %s size %llu\n",
95 f->file_name, f->real_file_size);
96
97 r = fallocate(f->fd, FALLOC_FL_KEEP_SIZE, 0,
98 f->real_file_size);
99 if (r != 0) {
100 td_verror(td, errno, "fallocate");
101 }
102 break;
103#endif /* FIO_HAVE_LINUX_FALLOCATE */
104 default:
105 log_err("fio: unknown fallocate mode: %d\n",
106 td->o.fallocate_mode);
107 assert(0);
Jens Axboe7bc8c2c2010-01-28 11:31:31 +0100108 }
109 }
Eric Gourioua596f042011-06-17 09:11:45 +0200110#endif /* FIO_HAVE_FALLOCATE */
Bruce Cran9b836562011-01-08 19:49:54 +0100111
Shawn Lewisfc74ac12008-01-11 09:45:11 +0100112 if (!new_layout)
113 goto done;
114
Jens Axboe5e0074c2009-06-02 21:40:09 +0200115 /*
116 * The size will be -1ULL when fill_device is used, so don't truncate
117 * or fallocate this file, just write it
118 */
119 if (!td->o.fill_device) {
120 dprint(FD_FILE, "truncate file %s, size %llu\n", f->file_name,
Jens Axboeee56ad52008-02-01 10:30:20 +0100121 f->real_file_size);
Jens Axboe5e0074c2009-06-02 21:40:09 +0200122 if (ftruncate(f->fd, f->real_file_size) == -1) {
123 td_verror(td, errno, "ftruncate");
124 goto err;
125 }
Jens Axboe5e0074c2009-06-02 21:40:09 +0200126 }
Jens Axboe40f82982006-10-25 11:21:05 +0200127
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100128 b = malloc(td->o.max_bs[DDIR_WRITE]);
129 memset(b, 0, td->o.max_bs[DDIR_WRITE]);
Jens Axboe53cdc682006-10-18 11:50:58 +0200130
Jens Axboe7bb48f82007-03-27 15:30:28 +0200131 left = f->real_file_size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200132 while (left && !td->terminate) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100133 bs = td->o.max_bs[DDIR_WRITE];
Jens Axboe53cdc682006-10-18 11:50:58 +0200134 if (bs > left)
135 bs = left;
136
137 r = write(f->fd, b, bs);
138
Jens Axboe5e0074c2009-06-02 21:40:09 +0200139 if (r > 0) {
140 left -= r;
Jens Axboe53cdc682006-10-18 11:50:58 +0200141 continue;
142 } else {
Jens Axboe5e0074c2009-06-02 21:40:09 +0200143 if (r < 0) {
144 int __e = errno;
145
146 if (__e == ENOSPC) {
147 if (td->o.fill_device)
148 break;
149 log_info("fio: ENOSPC on laying out "
150 "file, stopping\n");
151 break;
152 }
Jens Axboee1161c32007-02-22 19:36:48 +0100153 td_verror(td, errno, "write");
Jens Axboe5e0074c2009-06-02 21:40:09 +0200154 } else
Jens Axboee1161c32007-02-22 19:36:48 +0100155 td_verror(td, EIO, "write");
Jens Axboe53cdc682006-10-18 11:50:58 +0200156
157 break;
158 }
159 }
160
Jens Axboeffdfabd2008-03-26 09:18:14 +0100161 if (td->terminate) {
162 dprint(FD_FILE, "terminate unlink %s\n", f->file_name);
Jens Axboe53cdc682006-10-18 11:50:58 +0200163 unlink(f->file_name);
Jens Axboeffdfabd2008-03-26 09:18:14 +0100164 } else if (td->o.create_fsync) {
Jens Axboe98e1ac42008-03-06 11:56:48 +0100165 if (fsync(f->fd) < 0) {
166 td_verror(td, errno, "fsync");
167 goto err;
168 }
169 }
Jens Axboe0d1cd202009-06-05 22:11:52 +0200170 if (td->o.fill_device && !td_write(td)) {
Jens Axboed6aed792009-06-03 08:41:15 +0200171 fio_file_clear_size_known(f);
Jens Axboe5e0074c2009-06-02 21:40:09 +0200172 if (td_io_get_file_size(td, f))
173 goto err;
174 if (f->io_size > f->real_file_size)
175 f->io_size = f->real_file_size;
176 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200177
178 free(b);
Jens Axboe507a7022007-03-27 15:52:46 +0200179done:
Jens Axboe53cdc682006-10-18 11:50:58 +0200180 return 0;
181err:
182 close(f->fd);
183 f->fd = -1;
184 return 1;
185}
186
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200187static int pre_read_file(struct thread_data *td, struct fio_file *f)
188{
Jens Axboeb0f65862009-05-20 11:52:15 +0200189 int r, did_open = 0, old_runstate;
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200190 unsigned long long left;
191 unsigned int bs;
192 char *b;
193
Jens Axboe9c0d2242009-07-01 12:26:28 +0200194 if (td->io_ops->flags & FIO_PIPEIO)
195 return 0;
196
Jens Axboed6aed792009-06-03 08:41:15 +0200197 if (!fio_file_open(f)) {
Jens Axboeb0f65862009-05-20 11:52:15 +0200198 if (td->io_ops->open_file(td, f)) {
199 log_err("fio: cannot pre-read, failed to open file\n");
200 return 1;
201 }
202 did_open = 1;
203 }
204
205 old_runstate = td->runstate;
206 td_set_runstate(td, TD_PRE_READING);
207
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200208 bs = td->o.max_bs[DDIR_READ];
209 b = malloc(bs);
210 memset(b, 0, bs);
211
212 lseek(f->fd, f->file_offset, SEEK_SET);
213 left = f->io_size;
214
215 while (left && !td->terminate) {
216 if (bs > left)
217 bs = left;
218
219 r = read(f->fd, b, bs);
220
221 if (r == (int) bs) {
222 left -= bs;
223 continue;
224 } else {
225 td_verror(td, EIO, "pre_read");
226 break;
227 }
228 }
229
Jens Axboeb0f65862009-05-20 11:52:15 +0200230 td_set_runstate(td, old_runstate);
231
232 if (did_open)
233 td->io_ops->close_file(td, f);
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200234 free(b);
235 return 0;
236}
237
Jens Axboe7bb48f82007-03-27 15:30:28 +0200238static unsigned long long get_rand_file_size(struct thread_data *td)
Jens Axboe9c60ce62007-03-15 09:14:47 +0100239{
Jens Axboedc873b62008-06-04 20:13:04 +0200240 unsigned long long ret, sized;
Jens Axboe1294c3e2011-05-11 08:15:18 +0200241 unsigned long r;
Jens Axboe9c60ce62007-03-15 09:14:47 +0100242
Jens Axboe4c07ad82011-03-28 09:51:09 +0200243 if (td->o.use_os_rand) {
244 r = os_random_long(&td->file_size_state);
245 sized = td->o.file_size_high - td->o.file_size_low;
246 ret = (unsigned long long) ((double) sized * (r / (OS_RAND_MAX + 1.0)));
247 } else {
248 r = __rand(&td->__file_size_state);
249 sized = td->o.file_size_high - td->o.file_size_low;
250 ret = (unsigned long long) ((double) sized * (r / (FRAND_MAX + 1.0)));
251 }
252
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100253 ret += td->o.file_size_low;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100254 ret -= (ret % td->o.rw_min_bs);
Jens Axboe9c60ce62007-03-15 09:14:47 +0100255 return ret;
256}
257
Jens Axboe53cdc682006-10-18 11:50:58 +0200258static int file_size(struct thread_data *td, struct fio_file *f)
259{
260 struct stat st;
261
Jens Axboedf9c26b2009-03-05 10:13:58 +0100262 if (stat(f->file_name, &st) == -1) {
Jens Axboe7bb48f82007-03-27 15:30:28 +0200263 td_verror(td, errno, "fstat");
264 return 1;
265 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200266
Jens Axboe7bb48f82007-03-27 15:30:28 +0200267 f->real_file_size = st.st_size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200268 return 0;
269}
270
271static int bdev_size(struct thread_data *td, struct fio_file *f)
272{
Bruce Cran9b836562011-01-08 19:49:54 +0100273 unsigned long long bytes = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200274 int r;
275
Jens Axboedf9c26b2009-03-05 10:13:58 +0100276 if (td->io_ops->open_file(td, f)) {
277 log_err("fio: failed opening blockdev %s for size check\n",
278 f->file_name);
279 return 1;
280 }
281
Bruce Cranecc314b2011-01-04 10:59:30 +0100282 r = blockdev_size(f, &bytes);
Jens Axboe53cdc682006-10-18 11:50:58 +0200283 if (r) {
Jens Axboee1161c32007-02-22 19:36:48 +0100284 td_verror(td, r, "blockdev_size");
Jens Axboedf9c26b2009-03-05 10:13:58 +0100285 goto err;
Jens Axboe53cdc682006-10-18 11:50:58 +0200286 }
287
Jens Axboe7ab077a2009-02-02 15:07:37 +0100288 if (!bytes) {
289 log_err("%s: zero sized block device?\n", f->file_name);
Jens Axboedf9c26b2009-03-05 10:13:58 +0100290 goto err;
Jens Axboe7ab077a2009-02-02 15:07:37 +0100291 }
292
Jens Axboe53cdc682006-10-18 11:50:58 +0200293 f->real_file_size = bytes;
Jens Axboe22a57ba2009-06-09 14:34:35 +0200294 td->io_ops->close_file(td, f);
Jens Axboe53cdc682006-10-18 11:50:58 +0200295 return 0;
Jens Axboedf9c26b2009-03-05 10:13:58 +0100296err:
297 td->io_ops->close_file(td, f);
298 return 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200299}
300
Jens Axboe4ccdccd2010-06-24 10:27:22 +0200301static int char_size(struct thread_data *td, struct fio_file *f)
302{
303#ifdef FIO_HAVE_CHARDEV_SIZE
Bruce Cran9b836562011-01-08 19:49:54 +0100304 unsigned long long bytes = 0;
Jens Axboe4ccdccd2010-06-24 10:27:22 +0200305 int r;
306
307 if (td->io_ops->open_file(td, f)) {
308 log_err("fio: failed opening blockdev %s for size check\n",
309 f->file_name);
310 return 1;
311 }
312
Bruce Cranecc314b2011-01-04 10:59:30 +0100313 r = chardev_size(f, &bytes);
Jens Axboe4ccdccd2010-06-24 10:27:22 +0200314 if (r) {
315 td_verror(td, r, "chardev_size");
316 goto err;
317 }
318
319 if (!bytes) {
320 log_err("%s: zero sized char device?\n", f->file_name);
321 goto err;
322 }
323
324 f->real_file_size = bytes;
325 td->io_ops->close_file(td, f);
326 return 0;
327err:
328 td->io_ops->close_file(td, f);
329 return 1;
330#else
331 f->real_file_size = -1ULL;
332 return 0;
333#endif
334}
335
Jens Axboe53cdc682006-10-18 11:50:58 +0200336static int get_file_size(struct thread_data *td, struct fio_file *f)
337{
338 int ret = 0;
339
Jens Axboed6aed792009-06-03 08:41:15 +0200340 if (fio_file_size_known(f))
Jens Axboe409b3412007-03-28 09:57:01 +0200341 return 0;
342
Jens Axboe7bb48f82007-03-27 15:30:28 +0200343 if (f->filetype == FIO_TYPE_FILE)
344 ret = file_size(td, f);
345 else if (f->filetype == FIO_TYPE_BD)
Jens Axboe53cdc682006-10-18 11:50:58 +0200346 ret = bdev_size(td, f);
Jens Axboe4ccdccd2010-06-24 10:27:22 +0200347 else if (f->filetype == FIO_TYPE_CHAR)
348 ret = char_size(td, f);
Jens Axboe53cdc682006-10-18 11:50:58 +0200349 else
350 f->real_file_size = -1;
351
352 if (ret)
353 return ret;
354
355 if (f->file_offset > f->real_file_size) {
Bruce Cran0f2152c2010-12-15 10:33:03 +0100356 log_err("%s: offset extends end (%llu > %llu)\n", td->o.name,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100357 f->file_offset, f->real_file_size);
Jens Axboe53cdc682006-10-18 11:50:58 +0200358 return 1;
359 }
360
Jens Axboed6aed792009-06-03 08:41:15 +0200361 fio_file_set_size_known(f);
Jens Axboe53cdc682006-10-18 11:50:58 +0200362 return 0;
363}
364
Jens Axboe3baddf22008-04-04 11:10:30 +0200365static int __file_invalidate_cache(struct thread_data *td, struct fio_file *f,
366 unsigned long long off,
367 unsigned long long len)
Jens Axboee5b401d2006-10-18 16:03:40 +0200368{
369 int ret = 0;
370
Jens Axboe5e0074c2009-06-02 21:40:09 +0200371 if (len == -1ULL)
Jens Axboe3baddf22008-04-04 11:10:30 +0200372 len = f->io_size;
373 if (off == -1ULL)
374 off = f->file_offset;
Jens Axboeee56ad52008-02-01 10:30:20 +0100375
Jens Axboe0d1cd202009-06-05 22:11:52 +0200376 if (len == -1ULL || off == -1ULL)
377 return 0;
378
Jens Axboe3baddf22008-04-04 11:10:30 +0200379 dprint(FD_IO, "invalidate cache %s: %llu/%llu\n", f->file_name, off,
380 len);
Jens Axboeb5af8292007-03-08 12:43:13 +0100381
Jens Axboee5b401d2006-10-18 16:03:40 +0200382 /*
383 * FIXME: add blockdev flushing too
384 */
Jens Axboea1c58072009-08-04 23:17:02 +0200385 if (f->mmap_ptr) {
Bruce Cran03e20d62011-01-02 20:14:54 +0100386 ret = posix_madvise(f->mmap_ptr, f->mmap_sz, POSIX_MADV_DONTNEED);
Jens Axboea1c58072009-08-04 23:17:02 +0200387#ifdef FIO_MADV_FREE
Bruce Cran03e20d62011-01-02 20:14:54 +0100388 (void) posix_madvise(f->mmap_ptr, f->mmap_sz, FIO_MADV_FREE);
Jens Axboea1c58072009-08-04 23:17:02 +0200389#endif
390 } else if (f->filetype == FIO_TYPE_FILE) {
Bruce Cranecc314b2011-01-04 10:59:30 +0100391 ret = posix_fadvise(f->fd, off, len, POSIX_FADV_DONTNEED);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100392 } else if (f->filetype == FIO_TYPE_BD) {
Bruce Cranecc314b2011-01-04 10:59:30 +0100393 ret = blockdev_invalidate_cache(f);
Jens Axboe7e0e25c2007-03-27 12:49:56 +0200394 if (ret < 0 && errno == EACCES && geteuid()) {
Jens Axboe7172cfe2007-07-23 14:36:00 +0200395 if (!root_warn) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100396 log_err("fio: only root may flush block "
397 "devices. Cache flush bypassed!\n");
Jens Axboe7172cfe2007-07-23 14:36:00 +0200398 root_warn = 1;
399 }
Jens Axboe7e0e25c2007-03-27 12:49:56 +0200400 ret = 0;
401 }
Jens Axboeb5605e92007-04-16 20:42:33 +0200402 } else if (f->filetype == FIO_TYPE_CHAR || f->filetype == FIO_TYPE_PIPE)
Jens Axboee5b401d2006-10-18 16:03:40 +0200403 ret = 0;
404
405 if (ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100406 td_verror(td, errno, "invalidate_cache");
Jens Axboee5b401d2006-10-18 16:03:40 +0200407 return 1;
Jens Axboe3baddf22008-04-04 11:10:30 +0200408 } else if (ret > 0) {
409 td_verror(td, ret, "invalidate_cache");
410 return 1;
Jens Axboee5b401d2006-10-18 16:03:40 +0200411 }
412
Jens Axboead2da602007-02-26 12:33:27 +0100413 return ret;
Jens Axboe3baddf22008-04-04 11:10:30 +0200414
415}
416
417int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
418{
Jens Axboed6aed792009-06-03 08:41:15 +0200419 if (!fio_file_open(f))
Jens Axboea5fb4612008-05-28 10:54:01 +0200420 return 0;
421
Jens Axboe5e0074c2009-06-02 21:40:09 +0200422 return __file_invalidate_cache(td, f, -1ULL, -1ULL);
Jens Axboee5b401d2006-10-18 16:03:40 +0200423}
424
Jens Axboe6977bcd2008-03-01 15:55:36 +0100425int generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
Jens Axboe53cdc682006-10-18 11:50:58 +0200426{
Jens Axboe6977bcd2008-03-01 15:55:36 +0100427 int ret = 0;
428
Jens Axboeee56ad52008-02-01 10:30:20 +0100429 dprint(FD_FILE, "fd close %s\n", f->file_name);
Jens Axboe4906e0b2008-03-01 18:59:51 +0100430
431 remove_file_hash(f);
432
Jens Axboe6977bcd2008-03-01 15:55:36 +0100433 if (close(f->fd) < 0)
434 ret = errno;
435
Jens Axboeb5af8292007-03-08 12:43:13 +0100436 f->fd = -1;
Jens Axboe6977bcd2008-03-01 15:55:36 +0100437 return ret;
Jens Axboe53cdc682006-10-18 11:50:58 +0200438}
439
Dmitry Monakhov1ccc6dc2012-09-19 23:22:53 +0400440int file_lookup_open(struct fio_file *f, int flags)
Jens Axboe53cdc682006-10-18 11:50:58 +0200441{
Jens Axboe29c13492008-03-01 19:25:20 +0100442 struct fio_file *__f;
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100443 int from_hash;
444
445 __f = lookup_file_hash(f->file_name);
446 if (__f) {
Jens Axboe9efef3c2008-03-06 10:26:25 +0100447 dprint(FD_FILE, "found file in hash %s\n", f->file_name);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100448 /*
449 * racy, need the __f->lock locked
450 */
451 f->lock = __f->lock;
452 f->lock_owner = __f->lock_owner;
453 f->lock_batch = __f->lock_batch;
454 f->lock_ddir = __f->lock_ddir;
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100455 from_hash = 1;
456 } else {
Jens Axboe9efef3c2008-03-06 10:26:25 +0100457 dprint(FD_FILE, "file not found in hash %s\n", f->file_name);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100458 from_hash = 0;
459 }
460
Jens Axboee8670ef2008-03-06 12:06:30 +0100461 f->fd = open(f->file_name, flags, 0600);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100462 return from_hash;
463}
464
465int generic_open_file(struct thread_data *td, struct fio_file *f)
466{
Jens Axboe66159822007-04-16 21:54:24 +0200467 int is_std = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200468 int flags = 0;
Jens Axboe29c13492008-03-01 19:25:20 +0100469 int from_hash = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200470
Jens Axboeee56ad52008-02-01 10:30:20 +0100471 dprint(FD_FILE, "fd open %s\n", f->file_name);
472
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200473 if (td_trim(td) && f->filetype != FIO_TYPE_BD) {
474 log_err("fio: trim only applies to block device\n");
475 return 1;
476 }
477
Jens Axboe66159822007-04-16 21:54:24 +0200478 if (!strcmp(f->file_name, "-")) {
479 if (td_rw(td)) {
480 log_err("fio: can't read/write to stdin/out\n");
481 return 1;
482 }
483 is_std = 1;
Jens Axboece98fa62007-04-17 13:27:32 +0200484
485 /*
486 * move output logging to stderr, if we are writing to stdout
487 */
488 if (td_write(td))
489 f_out = stderr;
Jens Axboe66159822007-04-16 21:54:24 +0200490 }
491
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200492 if (td_trim(td))
493 goto skip_flags;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100494 if (td->o.odirect)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100495 flags |= OS_O_DIRECT;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100496 if (td->o.sync_io)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100497 flags |= O_SYNC;
Jens Axboe814452b2009-03-04 12:53:13 +0100498 if (td->o.create_on_open)
499 flags |= O_CREAT;
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200500skip_flags:
501 if (f->filetype != FIO_TYPE_FILE)
502 flags |= FIO_O_NOATIME;
Jens Axboe7abf8332006-11-23 15:01:19 +0100503
Aaron Carroll056f3452007-11-26 09:08:53 +0100504open_again:
Jens Axboe660a1cb2007-04-17 15:37:47 +0200505 if (td_write(td)) {
Jens Axboe17308152008-03-07 13:42:31 +0100506 if (!read_only)
507 flags |= O_RDWR;
Jens Axboe53cdc682006-10-18 11:50:58 +0200508
Jens Axboeaf52b342007-03-13 10:07:47 +0100509 if (f->filetype == FIO_TYPE_FILE)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100510 flags |= O_CREAT;
Jens Axboe2fd233b2007-03-02 08:44:25 +0100511
Jens Axboe66159822007-04-16 21:54:24 +0200512 if (is_std)
513 f->fd = dup(STDOUT_FILENO);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100514 else
515 from_hash = file_lookup_open(f, flags);
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200516 } else if (td_read(td)) {
Jens Axboe4241ea82007-09-12 08:18:36 +0200517 if (f->filetype == FIO_TYPE_CHAR && !read_only)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100518 flags |= O_RDWR;
519 else
520 flags |= O_RDONLY;
521
Jens Axboe66159822007-04-16 21:54:24 +0200522 if (is_std)
523 f->fd = dup(STDIN_FILENO);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100524 else
525 from_hash = file_lookup_open(f, flags);
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200526 } else { //td trim
527 flags |= O_RDWR;
528 from_hash = file_lookup_open(f, flags);
Jens Axboe53cdc682006-10-18 11:50:58 +0200529 }
530
531 if (f->fd == -1) {
Jens Axboee4e33252007-03-21 13:07:54 +0100532 char buf[FIO_VERROR_SIZE];
Jens Axboee1161c32007-02-22 19:36:48 +0100533 int __e = errno;
534
Jens Axboe835d9b92009-06-09 15:23:38 +0200535 if (__e == EPERM && (flags & FIO_O_NOATIME)) {
Jens Axboe5921e802008-05-30 15:02:38 +0200536 flags &= ~FIO_O_NOATIME;
Aaron Carroll056f3452007-11-26 09:08:53 +0100537 goto open_again;
538 }
539
Jens Axboee8670ef2008-03-06 12:06:30 +0100540 snprintf(buf, sizeof(buf) - 1, "open(%s)", f->file_name);
Jens Axboee4e33252007-03-21 13:07:54 +0100541
Jens Axboea93c5f02012-06-11 08:53:53 +0200542 if (__e == EINVAL && (flags & OS_O_DIRECT)) {
543 log_err("fio: looks like your file system does not " \
544 "support direct=1/buffered=0\n");
545 }
546
Jens Axboee4e33252007-03-21 13:07:54 +0100547 td_verror(td, __e, buf);
Jens Axboe53cdc682006-10-18 11:50:58 +0200548 }
549
Jens Axboe29c13492008-03-01 19:25:20 +0100550 if (!from_hash && f->fd != -1) {
551 if (add_file_hash(f)) {
Jens Axboe3f0ca9b2011-05-24 21:36:24 +0200552 int fio_unused ret;
Jens Axboe29c13492008-03-01 19:25:20 +0100553
554 /*
555 * OK to ignore, we haven't done anything with it
556 */
557 ret = generic_close_file(td, f);
558 goto open_again;
559 }
560 }
Jens Axboe4906e0b2008-03-01 18:59:51 +0100561
Jens Axboe53cdc682006-10-18 11:50:58 +0200562 return 0;
Jens Axboeb5af8292007-03-08 12:43:13 +0100563}
564
Jens Axboedf9c26b2009-03-05 10:13:58 +0100565int generic_get_file_size(struct thread_data *td, struct fio_file *f)
Jens Axboe21972cd2006-11-23 12:39:16 +0100566{
Jens Axboedf9c26b2009-03-05 10:13:58 +0100567 return get_file_size(td, f);
Jens Axboe21972cd2006-11-23 12:39:16 +0100568}
569
Jens Axboe7bb48f82007-03-27 15:30:28 +0200570/*
571 * open/close all files, so that ->real_file_size gets set
572 */
Jens Axboebab3fd52007-04-02 10:34:18 +0200573static int get_file_sizes(struct thread_data *td)
Jens Axboe53cdc682006-10-18 11:50:58 +0200574{
575 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100576 unsigned int i;
Jens Axboebab3fd52007-04-02 10:34:18 +0200577 int err = 0;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200578
579 for_each_file(td, f, i) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100580 dprint(FD_FILE, "get file size for %p/%d/%p\n", f, i,
581 f->file_name);
Jens Axboe9efef3c2008-03-06 10:26:25 +0100582
Jens Axboe99a47c62009-04-07 22:20:56 +0200583 if (td_io_get_file_size(td, f)) {
Jens Axboe40b44f42007-04-11 12:43:35 +0200584 if (td->error != ENOENT) {
585 log_err("%s\n", td->verror);
586 err = 1;
587 }
Jens Axboe541d66d2007-03-27 19:50:11 +0200588 clear_error(td);
Jens Axboe07eb79d2007-04-12 13:02:38 +0200589 }
Jens Axboe409b3412007-03-28 09:57:01 +0200590
591 if (f->real_file_size == -1ULL && td->o.size)
592 f->real_file_size = td->o.size / td->o.nr_files;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200593 }
Jens Axboebab3fd52007-04-02 10:34:18 +0200594
595 return err;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200596}
597
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200598struct fio_mount {
599 struct flist_head list;
600 const char *base;
601 char __base[256];
602 unsigned int key;
603};
604
605/*
606 * Get free number of bytes for each file on each unique mount.
607 */
608static unsigned long long get_fs_free_counts(struct thread_data *td)
609{
610 struct flist_head *n, *tmp;
Jens Axboe68b03162010-06-24 09:22:41 +0200611 unsigned long long ret = 0;
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200612 struct fio_mount *fm;
613 FLIST_HEAD(list);
614 struct fio_file *f;
615 unsigned int i;
616
617 for_each_file(td, f, i) {
618 struct stat sb;
619 char buf[256];
620
Jens Axboe4ccdccd2010-06-24 10:27:22 +0200621 if (f->filetype == FIO_TYPE_BD || f->filetype == FIO_TYPE_CHAR) {
622 if (f->real_file_size != -1ULL)
623 ret += f->real_file_size;
Jens Axboe68b03162010-06-24 09:22:41 +0200624 continue;
625 } else if (f->filetype != FIO_TYPE_FILE)
626 continue;
627
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200628 strcpy(buf, f->file_name);
629
630 if (stat(buf, &sb) < 0) {
631 if (errno != ENOENT)
632 break;
633 strcpy(buf, ".");
634 if (stat(buf, &sb) < 0)
635 break;
636 }
637
638 fm = NULL;
639 flist_for_each(n, &list) {
640 fm = flist_entry(n, struct fio_mount, list);
641 if (fm->key == sb.st_dev)
642 break;
643
644 fm = NULL;
645 }
646
647 if (fm)
648 continue;
649
650 fm = malloc(sizeof(*fm));
651 strcpy(fm->__base, buf);
652 fm->base = basename(fm->__base);
653 fm->key = sb.st_dev;
654 flist_add(&fm->list, &list);
655 }
656
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200657 flist_for_each_safe(n, tmp, &list) {
658 unsigned long long sz;
659
660 fm = flist_entry(n, struct fio_mount, list);
661 flist_del(&fm->list);
662
663 sz = get_fs_size(fm->base);
664 if (sz && sz != -1ULL)
665 ret += sz;
666
667 free(fm);
668 }
669
670 return ret;
671}
672
Dan Ehrenbergce95d652012-08-16 08:58:21 +0200673unsigned long long get_start_offset(struct thread_data *td)
674{
675 return td->o.start_offset +
676 (td->thread_number - 1) * td->o.offset_increment;
677}
678
Jens Axboe7bb48f82007-03-27 15:30:28 +0200679/*
680 * Open the files and setup files sizes, creating files if necessary.
681 */
682int setup_files(struct thread_data *td)
683{
684 unsigned long long total_size, extend_size;
685 struct fio_file *f;
686 unsigned int i;
Jens Axboe000b0802007-03-27 15:56:10 +0200687 int err = 0, need_extend;
Jens Axboe53cdc682006-10-18 11:50:58 +0200688
Jens Axboeee56ad52008-02-01 10:30:20 +0100689 dprint(FD_FILE, "setup files\n");
690
Jens Axboe691c8fb2008-03-07 14:26:26 +0100691 if (td->o.read_iolog_file)
Jens Axboe25460cf2012-05-02 13:58:02 +0200692 goto done;
Jens Axboe691c8fb2008-03-07 14:26:26 +0100693
Jens Axboe53cdc682006-10-18 11:50:58 +0200694 /*
695 * if ioengine defines a setup() method, it's responsible for
Jens Axboe7bb48f82007-03-27 15:30:28 +0200696 * opening the files and setting f->real_file_size to indicate
697 * the valid range for that file.
Jens Axboe53cdc682006-10-18 11:50:58 +0200698 */
699 if (td->io_ops->setup)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200700 err = td->io_ops->setup(td);
701 else
Jens Axboebab3fd52007-04-02 10:34:18 +0200702 err = get_file_sizes(td);
Jens Axboe53cdc682006-10-18 11:50:58 +0200703
Jens Axboef1027062006-10-20 10:14:01 +0200704 if (err)
705 return err;
706
Jens Axboe0a7eb122006-10-20 10:36:42 +0200707 /*
Jens Axboe7bb48f82007-03-27 15:30:28 +0200708 * check sizes. if the files/devices do not exist and the size
709 * isn't passed to fio, abort.
Jens Axboe0a7eb122006-10-20 10:36:42 +0200710 */
Jens Axboe7bb48f82007-03-27 15:30:28 +0200711 total_size = 0;
712 for_each_file(td, f, i) {
713 if (f->real_file_size == -1ULL)
714 total_size = -1ULL;
715 else
716 total_size += f->real_file_size;
717 }
Jens Axboe0a7eb122006-10-20 10:36:42 +0200718
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200719 if (td->o.fill_device)
720 td->fill_device_size = get_fs_free_counts(td);
721
Jens Axboe7bb48f82007-03-27 15:30:28 +0200722 /*
723 * device/file sizes are zero and no size given, punt
724 */
Jens Axboe1f809d12007-10-25 18:34:02 +0200725 if ((!total_size || total_size == -1ULL) && !td->o.size &&
Shawn Lewisaa31f1f2008-01-11 09:45:11 +0100726 !(td->io_ops->flags & FIO_NOIO) && !td->o.fill_device) {
Jens Axboe7bb48f82007-03-27 15:30:28 +0200727 log_err("%s: you need to specify size=\n", td->o.name);
Jens Axboee1161c32007-02-22 19:36:48 +0100728 td_verror(td, EINVAL, "total_file_size");
Jens Axboe53cdc682006-10-18 11:50:58 +0200729 return 1;
730 }
731
Jens Axboe7bb48f82007-03-27 15:30:28 +0200732 /*
733 * now file sizes are known, so we can set ->io_size. if size= is
734 * not given, ->io_size is just equal to ->real_file_size. if size
735 * is given, ->io_size is size / nr_files.
736 */
737 extend_size = total_size = 0;
738 need_extend = 0;
739 for_each_file(td, f, i) {
Dan Ehrenbergce95d652012-08-16 08:58:21 +0200740 f->file_offset = get_start_offset(td);
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200741
Jens Axboe7bb48f82007-03-27 15:30:28 +0200742 if (!td->o.file_size_low) {
743 /*
744 * no file size range given, file size is equal to
745 * total size divided by number of files. if that is
746 * zero, set it to the real file size.
747 */
748 f->io_size = td->o.size / td->o.nr_files;
Jens Axboe65bdb102008-01-24 13:13:12 +0100749 if (!f->io_size)
Jens Axboe273f8c92008-01-25 14:02:15 +0100750 f->io_size = f->real_file_size - f->file_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200751 } else if (f->real_file_size < td->o.file_size_low ||
752 f->real_file_size > td->o.file_size_high) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100753 if (f->file_offset > td->o.file_size_low)
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200754 goto err_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200755 /*
756 * file size given. if it's fixed, use that. if it's a
757 * range, generate a random size in-between.
758 */
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100759 if (td->o.file_size_low == td->o.file_size_high) {
760 f->io_size = td->o.file_size_low
761 - f->file_offset;
762 } else {
763 f->io_size = get_rand_file_size(td)
764 - f->file_offset;
765 }
Jens Axboe65bdb102008-01-24 13:13:12 +0100766 } else
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200767 f->io_size = f->real_file_size - f->file_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200768
769 if (f->io_size == -1ULL)
770 total_size = -1ULL;
Shaohua Li4d002562012-09-21 08:32:32 +0200771 else {
772 if (td->o.size_percent)
773 f->io_size = (f->io_size * td->o.size_percent) / 100;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200774 total_size += f->io_size;
Shaohua Li4d002562012-09-21 08:32:32 +0200775 }
Jens Axboe7bb48f82007-03-27 15:30:28 +0200776
777 if (f->filetype == FIO_TYPE_FILE &&
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200778 (f->io_size + f->file_offset) > f->real_file_size &&
Jens Axboe7bb48f82007-03-27 15:30:28 +0200779 !(td->io_ops->flags & FIO_DISKLESSIO)) {
Jens Axboe814452b2009-03-04 12:53:13 +0100780 if (!td->o.create_on_open) {
781 need_extend++;
782 extend_size += (f->io_size + f->file_offset);
783 } else
784 f->real_file_size = f->io_size + f->file_offset;
Jens Axboed6aed792009-06-03 08:41:15 +0200785 fio_file_set_extend(f);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100786 }
Jens Axboe7bb48f82007-03-27 15:30:28 +0200787 }
788
ljzhang,Yaxin Hu,Jianchao Tang22982902007-07-27 13:21:28 +0200789 if (!td->o.size || td->o.size > total_size)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200790 td->o.size = total_size;
791
792 /*
793 * See if we need to extend some files
794 */
795 if (need_extend) {
796 temp_stall_ts = 1;
Jens Axboef3afa572012-09-17 13:34:16 +0200797 if (output_format == FIO_OUTPUT_NORMAL)
Shaozhi Shawn Yea7ba8c52008-09-10 09:04:18 +0200798 log_info("%s: Laying out IO file(s) (%u file(s) /"
Bruce Cran0f2152c2010-12-15 10:33:03 +0100799 " %lluMB)\n", td->o.name, need_extend,
Shaozhi Shawn Yea7ba8c52008-09-10 09:04:18 +0200800 extend_size >> 20);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200801
802 for_each_file(td, f, i) {
Jens Axboe5e0074c2009-06-02 21:40:09 +0200803 unsigned long long old_len = -1ULL, extend_len = -1ULL;
Jens Axboe3baddf22008-04-04 11:10:30 +0200804
Jens Axboed6aed792009-06-03 08:41:15 +0200805 if (!fio_file_extend(f))
Jens Axboe7bb48f82007-03-27 15:30:28 +0200806 continue;
807
Jens Axboe409b3412007-03-28 09:57:01 +0200808 assert(f->filetype == FIO_TYPE_FILE);
Jens Axboed6aed792009-06-03 08:41:15 +0200809 fio_file_clear_extend(f);
Jens Axboe5e0074c2009-06-02 21:40:09 +0200810 if (!td->o.fill_device) {
811 old_len = f->real_file_size;
Jens Axboe0b9d69e2009-09-11 22:29:54 +0200812 extend_len = f->io_size + f->file_offset -
813 old_len;
Jens Axboe5e0074c2009-06-02 21:40:09 +0200814 }
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200815 f->real_file_size = (f->io_size + f->file_offset);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200816 err = extend_file(td, f);
817 if (err)
818 break;
Jens Axboe5e0074c2009-06-02 21:40:09 +0200819
Jens Axboe3baddf22008-04-04 11:10:30 +0200820 err = __file_invalidate_cache(td, f, old_len,
821 extend_len);
822 close(f->fd);
823 f->fd = -1;
824 if (err)
825 break;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200826 }
827 temp_stall_ts = 0;
828 }
829
830 if (err)
831 return err;
832
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100833 if (!td->o.zone_size)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200834 td->o.zone_size = td->o.size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200835
Jens Axboeea966f82007-09-03 10:09:37 +0200836 /*
837 * iolog already set the total io size, if we read back
838 * stored entries.
839 */
840 if (!td->o.read_iolog_file)
841 td->total_io_size = td->o.size * td->o.loops;
Jens Axboe25460cf2012-05-02 13:58:02 +0200842
843done:
844 if (td->o.create_only)
845 td->done = 1;
846
Jens Axboe7bb48f82007-03-27 15:30:28 +0200847 return 0;
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200848err_offset:
849 log_err("%s: you need to specify valid offset=\n", td->o.name);
850 return 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200851}
852
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200853int pre_read_files(struct thread_data *td)
854{
855 struct fio_file *f;
856 unsigned int i;
857
858 dprint(FD_FILE, "pre_read files\n");
859
860 for_each_file(td, f, i) {
861 pre_read_file(td, f);
862 }
863
864 return 1;
865}
866
Jens Axboe9c6f6312012-11-07 09:15:45 +0100867static int __init_rand_distribution(struct thread_data *td, struct fio_file *f)
868{
Jens Axboe23162962012-11-07 19:47:47 +0100869 unsigned int range_size, seed;
Jens Axboe9c6f6312012-11-07 09:15:45 +0100870 unsigned long nranges;
871
872 range_size = min(td->o.min_bs[DDIR_READ], td->o.min_bs[DDIR_WRITE]);
873
874 nranges = (f->real_file_size + range_size - 1) / range_size;
875
Jens Axboe23162962012-11-07 19:47:47 +0100876 seed = jhash(f->file_name, strlen(f->file_name), 0) * td->thread_number;
Jens Axboe84256872012-11-17 10:06:36 -0700877 if (!td->o.rand_repeatable)
878 seed = td->rand_seeds[4];
879
Jens Axboe9c6f6312012-11-07 09:15:45 +0100880 if (td->o.random_distribution == FIO_RAND_DIST_ZIPF)
Jens Axboe23162962012-11-07 19:47:47 +0100881 zipf_init(&f->zipf, nranges, td->o.zipf_theta, seed);
Jens Axboe9c6f6312012-11-07 09:15:45 +0100882 else
Jens Axboe23162962012-11-07 19:47:47 +0100883 pareto_init(&f->zipf, nranges, td->o.pareto_h, seed);
Jens Axboe9c6f6312012-11-07 09:15:45 +0100884
885 return 1;
886}
887
888static int init_rand_distribution(struct thread_data *td)
889{
890 struct fio_file *f;
891 unsigned int i;
892 int state;
893
894 if (td->o.random_distribution == FIO_RAND_DIST_RANDOM)
895 return 0;
896
897 state = td->runstate;
898 td_set_runstate(td, TD_SETTING_UP);
899 for_each_file(td, f, i)
900 __init_rand_distribution(td, f);
901 td_set_runstate(td, state);
902
903 return 1;
904}
905
Jens Axboe68727072007-03-15 20:49:25 +0100906int init_random_map(struct thread_data *td)
907{
Jens Axboe51ede0b2012-11-22 13:50:29 +0100908 unsigned long long blocks;
Jens Axboe68727072007-03-15 20:49:25 +0100909 struct fio_file *f;
910 unsigned int i;
911
Jens Axboe9c6f6312012-11-07 09:15:45 +0100912 if (init_rand_distribution(td))
913 return 0;
Jens Axboe3831a842012-11-26 19:59:14 +0100914 if (!td_random(td))
Jens Axboe68727072007-03-15 20:49:25 +0100915 return 0;
916
917 for_each_file(td, f, i) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100918 blocks = (f->real_file_size + td->o.rw_min_bs - 1) /
919 (unsigned long long) td->o.rw_min_bs;
Jens Axboe8055e412012-11-26 08:43:47 +0100920 if (td->o.random_generator == FIO_RAND_GEN_LFSR) {
921 if (!lfsr_init(&f->lfsr, blocks))
922 continue;
Jens Axboe3831a842012-11-26 19:59:14 +0100923 } else if (!td->o.norandommap) {
Jens Axboe7ebd7962012-11-28 21:24:46 +0100924 f->io_axmap = axmap_new(blocks);
925 if (f->io_axmap)
Jens Axboe8055e412012-11-26 08:43:47 +0100926 continue;
Jens Axboe1cad7122012-11-29 21:37:11 +0100927 } else if (td->o.norandommap)
928 continue;
Jens Axboeceadd592012-02-02 08:41:28 +0100929
Jens Axboe2b386d22008-03-26 10:32:57 +0100930 if (!td->o.softrandommap) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100931 log_err("fio: failed allocating random map. If running"
932 " a large number of jobs, try the 'norandommap'"
Jens Axboe2b386d22008-03-26 10:32:57 +0100933 " option or set 'softrandommap'. Or give"
934 " a larger --alloc-size to fio.\n");
Jens Axboe68727072007-03-15 20:49:25 +0100935 return 1;
936 }
Jens Axboe303032a2008-03-26 10:11:10 +0100937
938 log_info("fio: file %s failed allocating random map. Running "
939 "job without.\n", f->file_name);
Jens Axboe68727072007-03-15 20:49:25 +0100940 }
941
942 return 0;
943}
944
Jens Axboe53cdc682006-10-18 11:50:58 +0200945void close_files(struct thread_data *td)
946{
Jens Axboe0ab8db82006-10-18 17:16:23 +0200947 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100948 unsigned int i;
Jens Axboe53cdc682006-10-18 11:50:58 +0200949
Jens Axboe2be3eec2009-06-10 09:05:13 +0200950 for_each_file(td, f, i) {
951 if (fio_file_open(f))
952 td_io_close_file(td, f);
953 }
Jens Axboe24ffd2c2008-03-01 15:47:08 +0100954}
955
956void close_and_free_files(struct thread_data *td)
957{
958 struct fio_file *f;
959 unsigned int i;
960
Jens Axboeee56ad52008-02-01 10:30:20 +0100961 dprint(FD_FILE, "close files\n");
962
Jens Axboe0ab8db82006-10-18 17:16:23 +0200963 for_each_file(td, f, i) {
Jens Axboeffdfabd2008-03-26 09:18:14 +0100964 if (td->o.unlink && f->filetype == FIO_TYPE_FILE) {
965 dprint(FD_FILE, "free unlink %s\n", f->file_name);
Jens Axboe132ad462006-11-23 12:23:12 +0100966 unlink(f->file_name);
Jens Axboeffdfabd2008-03-26 09:18:14 +0100967 }
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100968
Jens Axboe22a57ba2009-06-09 14:34:35 +0200969 if (fio_file_open(f))
970 td_io_close_file(td, f);
971
Shaozhi Shawn Yeb9fbcf22008-09-10 09:05:17 +0200972 remove_file_hash(f);
Jens Axboeb3dc7f02007-03-09 12:52:15 +0100973
Jens Axboef17c4392008-03-01 18:40:46 +0100974 sfree(f->file_name);
Jens Axboefa1da862007-03-23 15:20:54 +0100975 f->file_name = NULL;
Jens Axboe7ebd7962012-11-28 21:24:46 +0100976 axmap_free(f->io_axmap);
977 f->io_axmap = NULL;
Jens Axboe78d99e62008-03-01 18:41:50 +0100978 sfree(f);
Jens Axboe53cdc682006-10-18 11:50:58 +0200979 }
Jens Axboeb4a6a592006-10-20 13:54:47 +0200980
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100981 td->o.filename = NULL;
Jens Axboecade3ef2007-03-23 15:29:45 +0100982 free(td->files);
Jens Axboe9efef3c2008-03-06 10:26:25 +0100983 td->files_index = 0;
Jens Axboeb4a6a592006-10-20 13:54:47 +0200984 td->files = NULL;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100985 td->o.nr_files = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200986}
Jens Axboeaf52b342007-03-13 10:07:47 +0100987
Jens Axboee3bab462007-03-13 11:22:09 +0100988static void get_file_type(struct fio_file *f)
Jens Axboeaf52b342007-03-13 10:07:47 +0100989{
990 struct stat sb;
991
Jens Axboe66159822007-04-16 21:54:24 +0200992 if (!strcmp(f->file_name, "-"))
993 f->filetype = FIO_TYPE_PIPE;
994 else
995 f->filetype = FIO_TYPE_FILE;
Jens Axboeaf52b342007-03-13 10:07:47 +0100996
Bruce Cran38921822012-02-22 17:55:16 +0000997 /* \\.\ is the device namespace in Windows, where every file is
998 * a block device */
999 if (strncmp(f->file_name, "\\\\.\\", 4) == 0)
1000 f->filetype = FIO_TYPE_BD;
1001
Jens Axboeb30d3952010-02-06 23:36:26 +01001002 if (!stat(f->file_name, &sb)) {
Bruce Cran38921822012-02-22 17:55:16 +00001003 if (S_ISBLK(sb.st_mode))
Jens Axboeaf52b342007-03-13 10:07:47 +01001004 f->filetype = FIO_TYPE_BD;
1005 else if (S_ISCHR(sb.st_mode))
1006 f->filetype = FIO_TYPE_CHAR;
Jens Axboeb5605e92007-04-16 20:42:33 +02001007 else if (S_ISFIFO(sb.st_mode))
1008 f->filetype = FIO_TYPE_PIPE;
Jens Axboeaf52b342007-03-13 10:07:47 +01001009 }
1010}
1011
Jens Axboef29b25a2007-07-23 08:56:43 +02001012int add_file(struct thread_data *td, const char *fname)
Jens Axboeaf52b342007-03-13 10:07:47 +01001013{
Jens Axboe7b4e4fe2007-03-13 12:51:40 +01001014 int cur_files = td->files_index;
Jens Axboebd0ee742007-03-23 14:26:23 +01001015 char file_name[PATH_MAX];
Jens Axboeaf52b342007-03-13 10:07:47 +01001016 struct fio_file *f;
Jens Axboebd0ee742007-03-23 14:26:23 +01001017 int len = 0;
Jens Axboeaf52b342007-03-13 10:07:47 +01001018
Jens Axboeee56ad52008-02-01 10:30:20 +01001019 dprint(FD_FILE, "add file %s\n", fname);
1020
Jens Axboef17c4392008-03-01 18:40:46 +01001021 f = smalloc(sizeof(*f));
Jens Axboec48c0be2008-07-31 19:55:02 +02001022 if (!f) {
1023 log_err("fio: smalloc OOM\n");
1024 assert(0);
1025 }
Jens Axboe0b9d69e2009-09-11 22:29:54 +02001026
Jens Axboeaf52b342007-03-13 10:07:47 +01001027 f->fd = -1;
Jens Axboe38dad622010-07-20 14:46:00 -06001028 fio_file_reset(f);
Jens Axboebd0ee742007-03-23 14:26:23 +01001029
Jens Axboefc99bc02009-03-04 15:27:42 +01001030 if (td->files_size <= td->files_index) {
Carl Henrik Lunde4b341fc2009-04-20 08:41:37 +02001031 int new_size = td->o.nr_files + 1;
Jens Axboe126d65c2008-03-01 18:04:31 +01001032
Jens Axboefc99bc02009-03-04 15:27:42 +01001033 dprint(FD_FILE, "resize file array to %d files\n", new_size);
1034
1035 td->files = realloc(td->files, new_size * sizeof(f));
Jens Axboefc99bc02009-03-04 15:27:42 +01001036 td->files_size = new_size;
1037 }
Jens Axboe8bb76792009-03-04 15:37:52 +01001038 td->files[cur_files] = f;
Shaohua Li89ac1d42012-06-11 08:56:32 +02001039 f->fileno = cur_files;
Jens Axboe126d65c2008-03-01 18:04:31 +01001040
Jens Axboe07eb79d2007-04-12 13:02:38 +02001041 /*
1042 * init function, io engine may not be loaded yet
1043 */
1044 if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO))
1045 f->real_file_size = -1ULL;
1046
Jens Axboebd0ee742007-03-23 14:26:23 +01001047 if (td->o.directory)
1048 len = sprintf(file_name, "%s/", td->o.directory);
1049
1050 sprintf(file_name + len, "%s", fname);
Jens Axboef17c4392008-03-01 18:40:46 +01001051 f->file_name = smalloc_strdup(file_name);
Jens Axboec48c0be2008-07-31 19:55:02 +02001052 if (!f->file_name) {
1053 log_err("fio: smalloc OOM\n");
1054 assert(0);
1055 }
Jens Axboe0b9d69e2009-09-11 22:29:54 +02001056
Jens Axboee3bab462007-03-13 11:22:09 +01001057 get_file_type(f);
Jens Axboeaf52b342007-03-13 10:07:47 +01001058
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001059 switch (td->o.file_lock_mode) {
1060 case FILE_LOCK_NONE:
1061 break;
1062 case FILE_LOCK_READWRITE:
1063 f->lock = fio_mutex_rw_init();
1064 break;
1065 case FILE_LOCK_EXCLUSIVE:
Jens Axboe521da522012-08-02 11:21:36 +02001066 f->lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001067 break;
1068 default:
1069 log_err("fio: unknown lock mode: %d\n", td->o.file_lock_mode);
1070 assert(0);
1071 }
Jens Axboe29c13492008-03-01 19:25:20 +01001072
Jens Axboe7b4e4fe2007-03-13 12:51:40 +01001073 td->files_index++;
Jens Axboe15494412007-03-13 11:52:47 +01001074 if (f->filetype == FIO_TYPE_FILE)
1075 td->nr_normal_files++;
Jens Axboef29b25a2007-07-23 08:56:43 +02001076
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001077 dprint(FD_FILE, "file %p \"%s\" added at %d\n", f, f->file_name,
1078 cur_files);
Jens Axboe9efef3c2008-03-06 10:26:25 +01001079
Jens Axboef29b25a2007-07-23 08:56:43 +02001080 return cur_files;
Jens Axboeaf52b342007-03-13 10:07:47 +01001081}
Jens Axboe0ad920e2007-03-13 11:06:45 +01001082
Jens Axboe49ffb4a2010-08-24 15:01:37 +02001083int add_file_exclusive(struct thread_data *td, const char *fname)
1084{
1085 struct fio_file *f;
1086 unsigned int i;
1087
1088 for_each_file(td, f, i) {
1089 if (!strcmp(f->file_name, fname))
1090 return i;
1091 }
1092
1093 return add_file(td, fname);
1094}
1095
Jens Axboe0ad920e2007-03-13 11:06:45 +01001096void get_file(struct fio_file *f)
1097{
Jens Axboe8172fe92008-02-01 21:51:02 +01001098 dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references);
Jens Axboed6aed792009-06-03 08:41:15 +02001099 assert(fio_file_open(f));
Jens Axboe0ad920e2007-03-13 11:06:45 +01001100 f->references++;
1101}
1102
Jens Axboe6977bcd2008-03-01 15:55:36 +01001103int put_file(struct thread_data *td, struct fio_file *f)
Jens Axboe0ad920e2007-03-13 11:06:45 +01001104{
Jens Axboe98e1ac42008-03-06 11:56:48 +01001105 int f_ret = 0, ret = 0;
Jens Axboe6977bcd2008-03-01 15:55:36 +01001106
Jens Axboe8172fe92008-02-01 21:51:02 +01001107 dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references);
Jens Axboeee56ad52008-02-01 10:30:20 +01001108
Jens Axboe22a57ba2009-06-09 14:34:35 +02001109 if (!fio_file_open(f)) {
1110 assert(f->fd == -1);
Jens Axboe6977bcd2008-03-01 15:55:36 +01001111 return 0;
Jens Axboe22a57ba2009-06-09 14:34:35 +02001112 }
Jens Axboe0ad920e2007-03-13 11:06:45 +01001113
1114 assert(f->references);
1115 if (--f->references)
Jens Axboe6977bcd2008-03-01 15:55:36 +01001116 return 0;
Jens Axboe0ad920e2007-03-13 11:06:45 +01001117
Jens Axboed424d4d2007-04-17 09:05:10 +02001118 if (should_fsync(td) && td->o.fsync_on_close)
Jens Axboe98e1ac42008-03-06 11:56:48 +01001119 f_ret = fsync(f->fd);
Jens Axboeebb14152007-03-13 14:42:15 +01001120
Jens Axboe0ad920e2007-03-13 11:06:45 +01001121 if (td->io_ops->close_file)
Jens Axboe6977bcd2008-03-01 15:55:36 +01001122 ret = td->io_ops->close_file(td, f);
Jens Axboe1020a132007-03-29 11:15:06 +02001123
Jens Axboe98e1ac42008-03-06 11:56:48 +01001124 if (!ret)
Jens Axboea5fb4612008-05-28 10:54:01 +02001125 ret = f_ret;
Jens Axboe98e1ac42008-03-06 11:56:48 +01001126
Jens Axboe0ad920e2007-03-13 11:06:45 +01001127 td->nr_open_files--;
Jens Axboed6aed792009-06-03 08:41:15 +02001128 fio_file_clear_open(f);
Jens Axboe22a57ba2009-06-09 14:34:35 +02001129 assert(f->fd == -1);
Jens Axboe6977bcd2008-03-01 15:55:36 +01001130 return ret;
Jens Axboe0ad920e2007-03-13 11:06:45 +01001131}
Jens Axboebbf6b542007-03-13 15:28:55 +01001132
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001133void lock_file(struct thread_data *td, struct fio_file *f, enum fio_ddir ddir)
Jens Axboeb2bd2bd2008-03-01 18:19:52 +01001134{
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001135 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1136 return;
Jens Axboe29c13492008-03-01 19:25:20 +01001137
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001138 if (f->lock_owner == td && f->lock_batch--)
1139 return;
1140
1141 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
1142 if (ddir == DDIR_READ)
1143 fio_mutex_down_read(f->lock);
1144 else
1145 fio_mutex_down_write(f->lock);
1146 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1147 fio_mutex_down(f->lock);
1148
1149 f->lock_owner = td;
1150 f->lock_batch = td->o.lockfile_batch;
1151 f->lock_ddir = ddir;
1152}
1153
1154void unlock_file(struct thread_data *td, struct fio_file *f)
1155{
1156 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1157 return;
1158 if (f->lock_batch)
1159 return;
1160
1161 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
1162 const int is_read = f->lock_ddir == DDIR_READ;
1163 int val = fio_mutex_getval(f->lock);
1164
1165 if ((is_read && val == 1) || (!is_read && val == -1))
1166 f->lock_owner = NULL;
1167
1168 if (is_read)
1169 fio_mutex_up_read(f->lock);
1170 else
1171 fio_mutex_up_write(f->lock);
1172 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE) {
1173 int val = fio_mutex_getval(f->lock);
1174
1175 if (val == 0)
1176 f->lock_owner = NULL;
1177
1178 fio_mutex_up(f->lock);
Jens Axboe29c13492008-03-01 19:25:20 +01001179 }
Jens Axboeb2bd2bd2008-03-01 18:19:52 +01001180}
1181
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001182void unlock_file_all(struct thread_data *td, struct fio_file *f)
Jens Axboeb2bd2bd2008-03-01 18:19:52 +01001183{
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001184 if (f->lock_owner != td)
1185 return;
Jens Axboe29c13492008-03-01 19:25:20 +01001186
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001187 f->lock_batch = 0;
1188 unlock_file(td, f);
Jens Axboeb2bd2bd2008-03-01 18:19:52 +01001189}
1190
Jens Axboebbf6b542007-03-13 15:28:55 +01001191static int recurse_dir(struct thread_data *td, const char *dirname)
1192{
1193 struct dirent *dir;
1194 int ret = 0;
1195 DIR *D;
1196
1197 D = opendir(dirname);
1198 if (!D) {
Jens Axboe0ddb2702007-03-27 19:43:53 +02001199 char buf[FIO_VERROR_SIZE];
1200
1201 snprintf(buf, FIO_VERROR_SIZE - 1, "opendir(%s)", dirname);
1202 td_verror(td, errno, buf);
Jens Axboebbf6b542007-03-13 15:28:55 +01001203 return 1;
1204 }
1205
1206 while ((dir = readdir(D)) != NULL) {
1207 char full_path[PATH_MAX];
1208 struct stat sb;
1209
Jens Axboee85b2b82007-03-14 09:39:06 +01001210 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
1211 continue;
Jens Axboe96d32d52007-03-14 09:16:09 +01001212
Bruce Cranb9fd7882012-02-20 17:01:46 +00001213 sprintf(full_path, "%s%s%s", dirname, FIO_OS_PATH_SEPARATOR, dir->d_name);
Jens Axboebbf6b542007-03-13 15:28:55 +01001214
1215 if (lstat(full_path, &sb) == -1) {
1216 if (errno != ENOENT) {
1217 td_verror(td, errno, "stat");
1218 return 1;
1219 }
1220 }
1221
1222 if (S_ISREG(sb.st_mode)) {
1223 add_file(td, full_path);
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001224 td->o.nr_files++;
Jens Axboebbf6b542007-03-13 15:28:55 +01001225 continue;
1226 }
Jens Axboe0ddb2702007-03-27 19:43:53 +02001227 if (!S_ISDIR(sb.st_mode))
1228 continue;
Jens Axboebbf6b542007-03-13 15:28:55 +01001229
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001230 ret = recurse_dir(td, full_path);
1231 if (ret)
Jens Axboebbf6b542007-03-13 15:28:55 +01001232 break;
1233 }
1234
1235 closedir(D);
1236 return ret;
1237}
1238
1239int add_dir_files(struct thread_data *td, const char *path)
1240{
Jens Axboe0ddb2702007-03-27 19:43:53 +02001241 int ret = recurse_dir(td, path);
1242
1243 if (!ret)
1244 log_info("fio: opendir added %d files\n", td->o.nr_files);
1245
1246 return ret;
Jens Axboebbf6b542007-03-13 15:28:55 +01001247}
Jens Axboecade3ef2007-03-23 15:29:45 +01001248
1249void dup_files(struct thread_data *td, struct thread_data *org)
1250{
1251 struct fio_file *f;
1252 unsigned int i;
Jens Axboe9efef3c2008-03-06 10:26:25 +01001253
1254 dprint(FD_FILE, "dup files: %d\n", org->files_index);
Jens Axboecade3ef2007-03-23 15:29:45 +01001255
1256 if (!org->files)
1257 return;
1258
Jens Axboe9efef3c2008-03-06 10:26:25 +01001259 td->files = malloc(org->files_index * sizeof(f));
Jens Axboecade3ef2007-03-23 15:29:45 +01001260
Jens Axboe9efef3c2008-03-06 10:26:25 +01001261 for_each_file(org, f, i) {
Jens Axboeb0fe4212008-03-01 18:22:27 +01001262 struct fio_file *__f;
1263
Jens Axboef17c4392008-03-01 18:40:46 +01001264 __f = smalloc(sizeof(*__f));
Jens Axboec48c0be2008-07-31 19:55:02 +02001265 if (!__f) {
1266 log_err("fio: smalloc OOM\n");
1267 assert(0);
1268 }
Jens Axboe22a57ba2009-06-09 14:34:35 +02001269 __f->fd = -1;
Jens Axboe38dad622010-07-20 14:46:00 -06001270 fio_file_reset(__f);
Jens Axboe0b9d69e2009-09-11 22:29:54 +02001271
Aaron Carrollbc3456f2008-06-25 09:20:37 +02001272 if (f->file_name) {
Jens Axboef17c4392008-03-01 18:40:46 +01001273 __f->file_name = smalloc_strdup(f->file_name);
Jens Axboec48c0be2008-07-31 19:55:02 +02001274 if (!__f->file_name) {
1275 log_err("fio: smalloc OOM\n");
1276 assert(0);
1277 }
Jens Axboe0b9d69e2009-09-11 22:29:54 +02001278
Aaron Carrollbc3456f2008-06-25 09:20:37 +02001279 __f->filetype = f->filetype;
1280 }
Jens Axboeb0fe4212008-03-01 18:22:27 +01001281
1282 td->files[i] = __f;
Jens Axboecade3ef2007-03-23 15:29:45 +01001283 }
1284}
Jens Axboef29b25a2007-07-23 08:56:43 +02001285
1286/*
1287 * Returns the index that matches the filename, or -1 if not there
1288 */
1289int get_fileno(struct thread_data *td, const char *fname)
1290{
1291 struct fio_file *f;
1292 unsigned int i;
1293
1294 for_each_file(td, f, i)
1295 if (!strcmp(f->file_name, fname))
1296 return i;
1297
1298 return -1;
1299}
1300
1301/*
1302 * For log usage, where we add/open/close files automatically
1303 */
1304void free_release_files(struct thread_data *td)
1305{
1306 close_files(td);
1307 td->files_index = 0;
1308 td->nr_normal_files = 0;
1309}