blob: 79e29da872cb85ebbb8eab682c2f935c82ce0606 [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 Axboe53cdc682006-10-18 11:50:58 +020015
Eric Gourioua596f042011-06-17 09:11:45 +020016#ifdef FIO_HAVE_LINUX_FALLOCATE
17#include <linux/falloc.h>
18#endif
19
Jens Axboe7172cfe2007-07-23 14:36:00 +020020static int root_warn;
21
Jens Axboec592b9f2009-06-03 09:29:28 +020022static inline void clear_error(struct thread_data *td)
23{
24 td->error = 0;
25 td->verror[0] = '\0';
26}
27
Jens Axboe3baddf22008-04-04 11:10:30 +020028/*
29 * Leaves f->fd open on success, caller must close
30 */
Jens Axboe7bb48f82007-03-27 15:30:28 +020031static int extend_file(struct thread_data *td, struct fio_file *f)
Jens Axboe25205e92006-10-19 20:50:14 +020032{
Jens Axboeea443652007-03-29 14:52:13 +020033 int r, new_layout = 0, unlink_file = 0, flags;
Jens Axboe25205e92006-10-19 20:50:14 +020034 unsigned long long left;
35 unsigned int bs;
36 char *b;
Jens Axboeb2a15192006-10-18 14:10:42 +020037
Jens Axboe4241ea82007-09-12 08:18:36 +020038 if (read_only) {
39 log_err("fio: refusing extend of file due to read-only\n");
40 return 0;
41 }
42
Jens Axboe507a7022007-03-27 15:52:46 +020043 /*
44 * check if we need to lay the file out complete again. fio
45 * does that for operations involving reads, or for writes
46 * where overwrite is set
47 */
Jens Axboeffdfabd2008-03-26 09:18:14 +010048 if (td_read(td) || (td_write(td) && td->o.overwrite) ||
49 (td_write(td) && td->io_ops->flags & FIO_NOEXTEND))
Jens Axboe507a7022007-03-27 15:52:46 +020050 new_layout = 1;
Jens Axboeea443652007-03-29 14:52:13 +020051 if (td_write(td) && !td->o.overwrite)
52 unlink_file = 1;
Jens Axboe507a7022007-03-27 15:52:46 +020053
Jens Axboe6ae1f572008-02-21 10:20:00 +010054 if (unlink_file || new_layout) {
Jens Axboebd199f22008-03-26 09:57:18 +010055 dprint(FD_FILE, "layout unlink %s\n", f->file_name);
Zhang, Yanmin982016d2008-02-26 15:35:52 +010056 if ((unlink(f->file_name) < 0) && (errno != ENOENT)) {
Jens Axboe7bb48f82007-03-27 15:30:28 +020057 td_verror(td, errno, "unlink");
58 return 1;
59 }
60 }
61
Jens Axboe507a7022007-03-27 15:52:46 +020062 flags = O_WRONLY | O_CREAT;
63 if (new_layout)
64 flags |= O_TRUNC;
65
Jens Axboeee56ad52008-02-01 10:30:20 +010066 dprint(FD_FILE, "open file %s, flags %x\n", f->file_name, flags);
Jens Axboe507a7022007-03-27 15:52:46 +020067 f->fd = open(f->file_name, flags, 0644);
Jens Axboe53cdc682006-10-18 11:50:58 +020068 if (f->fd < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +010069 td_verror(td, errno, "open");
Jens Axboe53cdc682006-10-18 11:50:58 +020070 return 1;
71 }
72
Jens Axboe7bc8c2c2010-01-28 11:31:31 +010073#ifdef FIO_HAVE_FALLOCATE
Eric Gourioua596f042011-06-17 09:11:45 +020074 if (!td->o.fill_device) {
75 switch (td->o.fallocate_mode) {
76 case FIO_FALLOCATE_NONE:
77 break;
78 case FIO_FALLOCATE_POSIX:
79 dprint(FD_FILE, "posix_fallocate file %s size %llu\n",
80 f->file_name, f->real_file_size);
Jens Axboe7bc8c2c2010-01-28 11:31:31 +010081
Eric Gourioua596f042011-06-17 09:11:45 +020082 r = posix_fallocate(f->fd, 0, f->real_file_size);
83 if (r > 0) {
84 log_err("fio: posix_fallocate fails: %s\n",
85 strerror(r));
86 }
87 break;
88#ifdef FIO_HAVE_LINUX_FALLOCATE
89 case FIO_FALLOCATE_KEEP_SIZE:
90 dprint(FD_FILE,
91 "fallocate(FALLOC_FL_KEEP_SIZE) "
92 "file %s size %llu\n",
93 f->file_name, f->real_file_size);
94
95 r = fallocate(f->fd, FALLOC_FL_KEEP_SIZE, 0,
96 f->real_file_size);
Jens Axboe3c3ed072012-03-27 09:12:39 +020097 if (r != 0)
Eric Gourioua596f042011-06-17 09:11:45 +020098 td_verror(td, errno, "fallocate");
Jens Axboe3c3ed072012-03-27 09:12:39 +020099
Eric Gourioua596f042011-06-17 09:11:45 +0200100 break;
101#endif /* FIO_HAVE_LINUX_FALLOCATE */
102 default:
103 log_err("fio: unknown fallocate mode: %d\n",
104 td->o.fallocate_mode);
105 assert(0);
Jens Axboe7bc8c2c2010-01-28 11:31:31 +0100106 }
107 }
Eric Gourioua596f042011-06-17 09:11:45 +0200108#endif /* FIO_HAVE_FALLOCATE */
Bruce Cran9b836562011-01-08 19:49:54 +0100109
Shawn Lewisfc74ac12008-01-11 09:45:11 +0100110 if (!new_layout)
111 goto done;
112
Jens Axboe5e0074c2009-06-02 21:40:09 +0200113 /*
114 * The size will be -1ULL when fill_device is used, so don't truncate
115 * or fallocate this file, just write it
116 */
117 if (!td->o.fill_device) {
118 dprint(FD_FILE, "truncate file %s, size %llu\n", f->file_name,
Jens Axboeee56ad52008-02-01 10:30:20 +0100119 f->real_file_size);
Jens Axboe5e0074c2009-06-02 21:40:09 +0200120 if (ftruncate(f->fd, f->real_file_size) == -1) {
121 td_verror(td, errno, "ftruncate");
122 goto err;
123 }
Jens Axboe5e0074c2009-06-02 21:40:09 +0200124 }
Jens Axboe40f82982006-10-25 11:21:05 +0200125
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100126 b = malloc(td->o.max_bs[DDIR_WRITE]);
127 memset(b, 0, td->o.max_bs[DDIR_WRITE]);
Jens Axboe53cdc682006-10-18 11:50:58 +0200128
Jens Axboe7bb48f82007-03-27 15:30:28 +0200129 left = f->real_file_size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200130 while (left && !td->terminate) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100131 bs = td->o.max_bs[DDIR_WRITE];
Jens Axboe53cdc682006-10-18 11:50:58 +0200132 if (bs > left)
133 bs = left;
134
135 r = write(f->fd, b, bs);
136
Jens Axboe5e0074c2009-06-02 21:40:09 +0200137 if (r > 0) {
138 left -= r;
Jens Axboe53cdc682006-10-18 11:50:58 +0200139 continue;
140 } else {
Jens Axboe5e0074c2009-06-02 21:40:09 +0200141 if (r < 0) {
142 int __e = errno;
143
144 if (__e == ENOSPC) {
145 if (td->o.fill_device)
146 break;
147 log_info("fio: ENOSPC on laying out "
148 "file, stopping\n");
149 break;
150 }
Jens Axboee1161c32007-02-22 19:36:48 +0100151 td_verror(td, errno, "write");
Jens Axboe5e0074c2009-06-02 21:40:09 +0200152 } else
Jens Axboee1161c32007-02-22 19:36:48 +0100153 td_verror(td, EIO, "write");
Jens Axboe53cdc682006-10-18 11:50:58 +0200154
155 break;
156 }
157 }
158
Jens Axboeffdfabd2008-03-26 09:18:14 +0100159 if (td->terminate) {
160 dprint(FD_FILE, "terminate unlink %s\n", f->file_name);
Jens Axboe53cdc682006-10-18 11:50:58 +0200161 unlink(f->file_name);
Jens Axboeffdfabd2008-03-26 09:18:14 +0100162 } else if (td->o.create_fsync) {
Jens Axboe98e1ac42008-03-06 11:56:48 +0100163 if (fsync(f->fd) < 0) {
164 td_verror(td, errno, "fsync");
165 goto err;
166 }
167 }
Jens Axboe0d1cd202009-06-05 22:11:52 +0200168 if (td->o.fill_device && !td_write(td)) {
Jens Axboed6aed792009-06-03 08:41:15 +0200169 fio_file_clear_size_known(f);
Jens Axboe5e0074c2009-06-02 21:40:09 +0200170 if (td_io_get_file_size(td, f))
171 goto err;
172 if (f->io_size > f->real_file_size)
173 f->io_size = f->real_file_size;
174 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200175
176 free(b);
Jens Axboe507a7022007-03-27 15:52:46 +0200177done:
Jens Axboe53cdc682006-10-18 11:50:58 +0200178 return 0;
179err:
180 close(f->fd);
181 f->fd = -1;
182 return 1;
183}
184
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200185static int pre_read_file(struct thread_data *td, struct fio_file *f)
186{
Jens Axboeb0f65862009-05-20 11:52:15 +0200187 int r, did_open = 0, old_runstate;
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200188 unsigned long long left;
189 unsigned int bs;
190 char *b;
191
Jens Axboe9c0d2242009-07-01 12:26:28 +0200192 if (td->io_ops->flags & FIO_PIPEIO)
193 return 0;
194
Jens Axboed6aed792009-06-03 08:41:15 +0200195 if (!fio_file_open(f)) {
Jens Axboeb0f65862009-05-20 11:52:15 +0200196 if (td->io_ops->open_file(td, f)) {
197 log_err("fio: cannot pre-read, failed to open file\n");
198 return 1;
199 }
200 did_open = 1;
201 }
202
203 old_runstate = td->runstate;
204 td_set_runstate(td, TD_PRE_READING);
205
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200206 bs = td->o.max_bs[DDIR_READ];
207 b = malloc(bs);
208 memset(b, 0, bs);
209
210 lseek(f->fd, f->file_offset, SEEK_SET);
211 left = f->io_size;
212
213 while (left && !td->terminate) {
214 if (bs > left)
215 bs = left;
216
217 r = read(f->fd, b, bs);
218
219 if (r == (int) bs) {
220 left -= bs;
221 continue;
222 } else {
223 td_verror(td, EIO, "pre_read");
224 break;
225 }
226 }
227
Jens Axboeb0f65862009-05-20 11:52:15 +0200228 td_set_runstate(td, old_runstate);
229
230 if (did_open)
231 td->io_ops->close_file(td, f);
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200232 free(b);
233 return 0;
234}
235
Jens Axboe7bb48f82007-03-27 15:30:28 +0200236static unsigned long long get_rand_file_size(struct thread_data *td)
Jens Axboe9c60ce62007-03-15 09:14:47 +0100237{
Jens Axboedc873b62008-06-04 20:13:04 +0200238 unsigned long long ret, sized;
Jens Axboe1294c3e2011-05-11 08:15:18 +0200239 unsigned long r;
Jens Axboe9c60ce62007-03-15 09:14:47 +0100240
Jens Axboe4c07ad82011-03-28 09:51:09 +0200241 if (td->o.use_os_rand) {
242 r = os_random_long(&td->file_size_state);
243 sized = td->o.file_size_high - td->o.file_size_low;
244 ret = (unsigned long long) ((double) sized * (r / (OS_RAND_MAX + 1.0)));
245 } else {
246 r = __rand(&td->__file_size_state);
247 sized = td->o.file_size_high - td->o.file_size_low;
248 ret = (unsigned long long) ((double) sized * (r / (FRAND_MAX + 1.0)));
249 }
250
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100251 ret += td->o.file_size_low;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100252 ret -= (ret % td->o.rw_min_bs);
Jens Axboe9c60ce62007-03-15 09:14:47 +0100253 return ret;
254}
255
Jens Axboe53cdc682006-10-18 11:50:58 +0200256static int file_size(struct thread_data *td, struct fio_file *f)
257{
258 struct stat st;
259
Jens Axboedf9c26b2009-03-05 10:13:58 +0100260 if (stat(f->file_name, &st) == -1) {
Jens Axboe7bb48f82007-03-27 15:30:28 +0200261 td_verror(td, errno, "fstat");
262 return 1;
263 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200264
Jens Axboe7bb48f82007-03-27 15:30:28 +0200265 f->real_file_size = st.st_size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200266 return 0;
267}
268
269static int bdev_size(struct thread_data *td, struct fio_file *f)
270{
Bruce Cran9b836562011-01-08 19:49:54 +0100271 unsigned long long bytes = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200272 int r;
273
Jens Axboedf9c26b2009-03-05 10:13:58 +0100274 if (td->io_ops->open_file(td, f)) {
275 log_err("fio: failed opening blockdev %s for size check\n",
276 f->file_name);
277 return 1;
278 }
279
Bruce Cranecc314b2011-01-04 10:59:30 +0100280 r = blockdev_size(f, &bytes);
Jens Axboe53cdc682006-10-18 11:50:58 +0200281 if (r) {
Jens Axboee1161c32007-02-22 19:36:48 +0100282 td_verror(td, r, "blockdev_size");
Jens Axboedf9c26b2009-03-05 10:13:58 +0100283 goto err;
Jens Axboe53cdc682006-10-18 11:50:58 +0200284 }
285
Jens Axboe7ab077a2009-02-02 15:07:37 +0100286 if (!bytes) {
287 log_err("%s: zero sized block device?\n", f->file_name);
Jens Axboedf9c26b2009-03-05 10:13:58 +0100288 goto err;
Jens Axboe7ab077a2009-02-02 15:07:37 +0100289 }
290
Jens Axboe53cdc682006-10-18 11:50:58 +0200291 f->real_file_size = bytes;
Jens Axboe22a57ba2009-06-09 14:34:35 +0200292 td->io_ops->close_file(td, f);
Jens Axboe53cdc682006-10-18 11:50:58 +0200293 return 0;
Jens Axboedf9c26b2009-03-05 10:13:58 +0100294err:
295 td->io_ops->close_file(td, f);
296 return 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200297}
298
Jens Axboe4ccdccd2010-06-24 10:27:22 +0200299static int char_size(struct thread_data *td, struct fio_file *f)
300{
301#ifdef FIO_HAVE_CHARDEV_SIZE
Bruce Cran9b836562011-01-08 19:49:54 +0100302 unsigned long long bytes = 0;
Jens Axboe4ccdccd2010-06-24 10:27:22 +0200303 int r;
304
305 if (td->io_ops->open_file(td, f)) {
306 log_err("fio: failed opening blockdev %s for size check\n",
307 f->file_name);
308 return 1;
309 }
310
Bruce Cranecc314b2011-01-04 10:59:30 +0100311 r = chardev_size(f, &bytes);
Jens Axboe4ccdccd2010-06-24 10:27:22 +0200312 if (r) {
313 td_verror(td, r, "chardev_size");
314 goto err;
315 }
316
317 if (!bytes) {
318 log_err("%s: zero sized char device?\n", f->file_name);
319 goto err;
320 }
321
322 f->real_file_size = bytes;
323 td->io_ops->close_file(td, f);
324 return 0;
325err:
326 td->io_ops->close_file(td, f);
327 return 1;
328#else
329 f->real_file_size = -1ULL;
330 return 0;
331#endif
332}
333
Jens Axboe53cdc682006-10-18 11:50:58 +0200334static int get_file_size(struct thread_data *td, struct fio_file *f)
335{
336 int ret = 0;
337
Jens Axboed6aed792009-06-03 08:41:15 +0200338 if (fio_file_size_known(f))
Jens Axboe409b3412007-03-28 09:57:01 +0200339 return 0;
340
Jens Axboe7bb48f82007-03-27 15:30:28 +0200341 if (f->filetype == FIO_TYPE_FILE)
342 ret = file_size(td, f);
343 else if (f->filetype == FIO_TYPE_BD)
Jens Axboe53cdc682006-10-18 11:50:58 +0200344 ret = bdev_size(td, f);
Jens Axboe4ccdccd2010-06-24 10:27:22 +0200345 else if (f->filetype == FIO_TYPE_CHAR)
346 ret = char_size(td, f);
Jens Axboe53cdc682006-10-18 11:50:58 +0200347 else
348 f->real_file_size = -1;
349
350 if (ret)
351 return ret;
352
353 if (f->file_offset > f->real_file_size) {
Bruce Cran0f2152c2010-12-15 10:33:03 +0100354 log_err("%s: offset extends end (%llu > %llu)\n", td->o.name,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100355 f->file_offset, f->real_file_size);
Jens Axboe53cdc682006-10-18 11:50:58 +0200356 return 1;
357 }
358
Jens Axboed6aed792009-06-03 08:41:15 +0200359 fio_file_set_size_known(f);
Jens Axboe53cdc682006-10-18 11:50:58 +0200360 return 0;
361}
362
Jens Axboe3baddf22008-04-04 11:10:30 +0200363static int __file_invalidate_cache(struct thread_data *td, struct fio_file *f,
364 unsigned long long off,
365 unsigned long long len)
Jens Axboee5b401d2006-10-18 16:03:40 +0200366{
367 int ret = 0;
368
Jens Axboe5e0074c2009-06-02 21:40:09 +0200369 if (len == -1ULL)
Jens Axboe3baddf22008-04-04 11:10:30 +0200370 len = f->io_size;
371 if (off == -1ULL)
372 off = f->file_offset;
Jens Axboeee56ad52008-02-01 10:30:20 +0100373
Jens Axboe0d1cd202009-06-05 22:11:52 +0200374 if (len == -1ULL || off == -1ULL)
375 return 0;
376
Jens Axboe3baddf22008-04-04 11:10:30 +0200377 dprint(FD_IO, "invalidate cache %s: %llu/%llu\n", f->file_name, off,
378 len);
Jens Axboeb5af8292007-03-08 12:43:13 +0100379
Jens Axboee5b401d2006-10-18 16:03:40 +0200380 /*
381 * FIXME: add blockdev flushing too
382 */
Jens Axboea1c58072009-08-04 23:17:02 +0200383 if (f->mmap_ptr) {
Bruce Cran03e20d62011-01-02 20:14:54 +0100384 ret = posix_madvise(f->mmap_ptr, f->mmap_sz, POSIX_MADV_DONTNEED);
Jens Axboea1c58072009-08-04 23:17:02 +0200385#ifdef FIO_MADV_FREE
Bruce Cran03e20d62011-01-02 20:14:54 +0100386 (void) posix_madvise(f->mmap_ptr, f->mmap_sz, FIO_MADV_FREE);
Jens Axboea1c58072009-08-04 23:17:02 +0200387#endif
388 } else if (f->filetype == FIO_TYPE_FILE) {
Bruce Cranecc314b2011-01-04 10:59:30 +0100389 ret = posix_fadvise(f->fd, off, len, POSIX_FADV_DONTNEED);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100390 } else if (f->filetype == FIO_TYPE_BD) {
Bruce Cranecc314b2011-01-04 10:59:30 +0100391 ret = blockdev_invalidate_cache(f);
Jens Axboe7e0e25c2007-03-27 12:49:56 +0200392 if (ret < 0 && errno == EACCES && geteuid()) {
Jens Axboe7172cfe2007-07-23 14:36:00 +0200393 if (!root_warn) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100394 log_err("fio: only root may flush block "
395 "devices. Cache flush bypassed!\n");
Jens Axboe7172cfe2007-07-23 14:36:00 +0200396 root_warn = 1;
397 }
Jens Axboe7e0e25c2007-03-27 12:49:56 +0200398 ret = 0;
399 }
Jens Axboeb5605e92007-04-16 20:42:33 +0200400 } else if (f->filetype == FIO_TYPE_CHAR || f->filetype == FIO_TYPE_PIPE)
Jens Axboee5b401d2006-10-18 16:03:40 +0200401 ret = 0;
402
403 if (ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100404 td_verror(td, errno, "invalidate_cache");
Jens Axboee5b401d2006-10-18 16:03:40 +0200405 return 1;
Jens Axboe3baddf22008-04-04 11:10:30 +0200406 } else if (ret > 0) {
407 td_verror(td, ret, "invalidate_cache");
408 return 1;
Jens Axboee5b401d2006-10-18 16:03:40 +0200409 }
410
Jens Axboead2da602007-02-26 12:33:27 +0100411 return ret;
Jens Axboe3baddf22008-04-04 11:10:30 +0200412
413}
414
415int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
416{
Jens Axboed6aed792009-06-03 08:41:15 +0200417 if (!fio_file_open(f))
Jens Axboea5fb4612008-05-28 10:54:01 +0200418 return 0;
419
Jens Axboe5e0074c2009-06-02 21:40:09 +0200420 return __file_invalidate_cache(td, f, -1ULL, -1ULL);
Jens Axboee5b401d2006-10-18 16:03:40 +0200421}
422
Jens Axboe6977bcd2008-03-01 15:55:36 +0100423int generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
Jens Axboe53cdc682006-10-18 11:50:58 +0200424{
Jens Axboe6977bcd2008-03-01 15:55:36 +0100425 int ret = 0;
426
Jens Axboeee56ad52008-02-01 10:30:20 +0100427 dprint(FD_FILE, "fd close %s\n", f->file_name);
Jens Axboe4906e0b2008-03-01 18:59:51 +0100428
429 remove_file_hash(f);
430
Jens Axboe6977bcd2008-03-01 15:55:36 +0100431 if (close(f->fd) < 0)
432 ret = errno;
433
Jens Axboeb5af8292007-03-08 12:43:13 +0100434 f->fd = -1;
Jens Axboe6977bcd2008-03-01 15:55:36 +0100435 return ret;
Jens Axboe53cdc682006-10-18 11:50:58 +0200436}
437
Dmitry Monakhov1ccc6dc2012-09-19 23:22:53 +0400438int file_lookup_open(struct fio_file *f, int flags)
Jens Axboe53cdc682006-10-18 11:50:58 +0200439{
Jens Axboe29c13492008-03-01 19:25:20 +0100440 struct fio_file *__f;
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100441 int from_hash;
442
443 __f = lookup_file_hash(f->file_name);
444 if (__f) {
Jens Axboe9efef3c2008-03-06 10:26:25 +0100445 dprint(FD_FILE, "found file in hash %s\n", f->file_name);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100446 /*
447 * racy, need the __f->lock locked
448 */
449 f->lock = __f->lock;
450 f->lock_owner = __f->lock_owner;
451 f->lock_batch = __f->lock_batch;
452 f->lock_ddir = __f->lock_ddir;
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100453 from_hash = 1;
454 } else {
Jens Axboe9efef3c2008-03-06 10:26:25 +0100455 dprint(FD_FILE, "file not found in hash %s\n", f->file_name);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100456 from_hash = 0;
457 }
458
Jens Axboee8670ef2008-03-06 12:06:30 +0100459 f->fd = open(f->file_name, flags, 0600);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100460 return from_hash;
461}
462
463int generic_open_file(struct thread_data *td, struct fio_file *f)
464{
Jens Axboe66159822007-04-16 21:54:24 +0200465 int is_std = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200466 int flags = 0;
Jens Axboe29c13492008-03-01 19:25:20 +0100467 int from_hash = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200468
Jens Axboeee56ad52008-02-01 10:30:20 +0100469 dprint(FD_FILE, "fd open %s\n", f->file_name);
470
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200471 if (td_trim(td) && f->filetype != FIO_TYPE_BD) {
472 log_err("fio: trim only applies to block device\n");
473 return 1;
474 }
475
Jens Axboe66159822007-04-16 21:54:24 +0200476 if (!strcmp(f->file_name, "-")) {
477 if (td_rw(td)) {
478 log_err("fio: can't read/write to stdin/out\n");
479 return 1;
480 }
481 is_std = 1;
Jens Axboece98fa62007-04-17 13:27:32 +0200482
483 /*
484 * move output logging to stderr, if we are writing to stdout
485 */
486 if (td_write(td))
487 f_out = stderr;
Jens Axboe66159822007-04-16 21:54:24 +0200488 }
489
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200490 if (td_trim(td))
491 goto skip_flags;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100492 if (td->o.odirect)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100493 flags |= OS_O_DIRECT;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100494 if (td->o.sync_io)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100495 flags |= O_SYNC;
Jens Axboe814452b2009-03-04 12:53:13 +0100496 if (td->o.create_on_open)
497 flags |= O_CREAT;
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200498skip_flags:
499 if (f->filetype != FIO_TYPE_FILE)
500 flags |= FIO_O_NOATIME;
Jens Axboe7abf8332006-11-23 15:01:19 +0100501
Aaron Carroll056f3452007-11-26 09:08:53 +0100502open_again:
Jens Axboe660a1cb2007-04-17 15:37:47 +0200503 if (td_write(td)) {
Jens Axboe17308152008-03-07 13:42:31 +0100504 if (!read_only)
505 flags |= O_RDWR;
Jens Axboe53cdc682006-10-18 11:50:58 +0200506
Jens Axboeaf52b342007-03-13 10:07:47 +0100507 if (f->filetype == FIO_TYPE_FILE)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100508 flags |= O_CREAT;
Jens Axboe2fd233b2007-03-02 08:44:25 +0100509
Jens Axboe66159822007-04-16 21:54:24 +0200510 if (is_std)
511 f->fd = dup(STDOUT_FILENO);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100512 else
513 from_hash = file_lookup_open(f, flags);
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200514 } else if (td_read(td)) {
Jens Axboe4241ea82007-09-12 08:18:36 +0200515 if (f->filetype == FIO_TYPE_CHAR && !read_only)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100516 flags |= O_RDWR;
517 else
518 flags |= O_RDONLY;
519
Jens Axboe66159822007-04-16 21:54:24 +0200520 if (is_std)
521 f->fd = dup(STDIN_FILENO);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100522 else
523 from_hash = file_lookup_open(f, flags);
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200524 } else { //td trim
525 flags |= O_RDWR;
526 from_hash = file_lookup_open(f, flags);
Jens Axboe53cdc682006-10-18 11:50:58 +0200527 }
528
529 if (f->fd == -1) {
Jens Axboee4e33252007-03-21 13:07:54 +0100530 char buf[FIO_VERROR_SIZE];
Jens Axboee1161c32007-02-22 19:36:48 +0100531 int __e = errno;
532
Jens Axboe835d9b92009-06-09 15:23:38 +0200533 if (__e == EPERM && (flags & FIO_O_NOATIME)) {
Jens Axboe5921e802008-05-30 15:02:38 +0200534 flags &= ~FIO_O_NOATIME;
Aaron Carroll056f3452007-11-26 09:08:53 +0100535 goto open_again;
536 }
537
Jens Axboee8670ef2008-03-06 12:06:30 +0100538 snprintf(buf, sizeof(buf) - 1, "open(%s)", f->file_name);
Jens Axboee4e33252007-03-21 13:07:54 +0100539
Jens Axboea93c5f02012-06-11 08:53:53 +0200540 if (__e == EINVAL && (flags & OS_O_DIRECT)) {
541 log_err("fio: looks like your file system does not " \
542 "support direct=1/buffered=0\n");
543 }
544
Jens Axboee4e33252007-03-21 13:07:54 +0100545 td_verror(td, __e, buf);
Jens Axboe53cdc682006-10-18 11:50:58 +0200546 }
547
Jens Axboe29c13492008-03-01 19:25:20 +0100548 if (!from_hash && f->fd != -1) {
549 if (add_file_hash(f)) {
Jens Axboe3f0ca9b2011-05-24 21:36:24 +0200550 int fio_unused ret;
Jens Axboe29c13492008-03-01 19:25:20 +0100551
552 /*
553 * OK to ignore, we haven't done anything with it
554 */
555 ret = generic_close_file(td, f);
556 goto open_again;
557 }
558 }
Jens Axboe4906e0b2008-03-01 18:59:51 +0100559
Jens Axboe53cdc682006-10-18 11:50:58 +0200560 return 0;
Jens Axboeb5af8292007-03-08 12:43:13 +0100561}
562
Jens Axboedf9c26b2009-03-05 10:13:58 +0100563int generic_get_file_size(struct thread_data *td, struct fio_file *f)
Jens Axboe21972cd2006-11-23 12:39:16 +0100564{
Jens Axboedf9c26b2009-03-05 10:13:58 +0100565 return get_file_size(td, f);
Jens Axboe21972cd2006-11-23 12:39:16 +0100566}
567
Jens Axboe7bb48f82007-03-27 15:30:28 +0200568/*
569 * open/close all files, so that ->real_file_size gets set
570 */
Jens Axboebab3fd52007-04-02 10:34:18 +0200571static int get_file_sizes(struct thread_data *td)
Jens Axboe53cdc682006-10-18 11:50:58 +0200572{
573 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100574 unsigned int i;
Jens Axboebab3fd52007-04-02 10:34:18 +0200575 int err = 0;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200576
577 for_each_file(td, f, i) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100578 dprint(FD_FILE, "get file size for %p/%d/%p\n", f, i,
579 f->file_name);
Jens Axboe9efef3c2008-03-06 10:26:25 +0100580
Jens Axboe99a47c62009-04-07 22:20:56 +0200581 if (td_io_get_file_size(td, f)) {
Jens Axboe40b44f42007-04-11 12:43:35 +0200582 if (td->error != ENOENT) {
583 log_err("%s\n", td->verror);
584 err = 1;
585 }
Jens Axboe541d66d2007-03-27 19:50:11 +0200586 clear_error(td);
Jens Axboe07eb79d2007-04-12 13:02:38 +0200587 }
Jens Axboe409b3412007-03-28 09:57:01 +0200588
589 if (f->real_file_size == -1ULL && td->o.size)
590 f->real_file_size = td->o.size / td->o.nr_files;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200591 }
Jens Axboebab3fd52007-04-02 10:34:18 +0200592
593 return err;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200594}
595
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200596struct fio_mount {
597 struct flist_head list;
598 const char *base;
599 char __base[256];
600 unsigned int key;
601};
602
603/*
604 * Get free number of bytes for each file on each unique mount.
605 */
606static unsigned long long get_fs_free_counts(struct thread_data *td)
607{
608 struct flist_head *n, *tmp;
Jens Axboe68b03162010-06-24 09:22:41 +0200609 unsigned long long ret = 0;
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200610 struct fio_mount *fm;
611 FLIST_HEAD(list);
612 struct fio_file *f;
613 unsigned int i;
614
615 for_each_file(td, f, i) {
616 struct stat sb;
617 char buf[256];
618
Jens Axboe4ccdccd2010-06-24 10:27:22 +0200619 if (f->filetype == FIO_TYPE_BD || f->filetype == FIO_TYPE_CHAR) {
620 if (f->real_file_size != -1ULL)
621 ret += f->real_file_size;
Jens Axboe68b03162010-06-24 09:22:41 +0200622 continue;
623 } else if (f->filetype != FIO_TYPE_FILE)
624 continue;
625
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200626 strcpy(buf, f->file_name);
627
628 if (stat(buf, &sb) < 0) {
629 if (errno != ENOENT)
630 break;
631 strcpy(buf, ".");
632 if (stat(buf, &sb) < 0)
633 break;
634 }
635
636 fm = NULL;
637 flist_for_each(n, &list) {
638 fm = flist_entry(n, struct fio_mount, list);
639 if (fm->key == sb.st_dev)
640 break;
641
642 fm = NULL;
643 }
644
645 if (fm)
646 continue;
647
648 fm = malloc(sizeof(*fm));
649 strcpy(fm->__base, buf);
650 fm->base = basename(fm->__base);
651 fm->key = sb.st_dev;
652 flist_add(&fm->list, &list);
653 }
654
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200655 flist_for_each_safe(n, tmp, &list) {
656 unsigned long long sz;
657
658 fm = flist_entry(n, struct fio_mount, list);
659 flist_del(&fm->list);
660
661 sz = get_fs_size(fm->base);
662 if (sz && sz != -1ULL)
663 ret += sz;
664
665 free(fm);
666 }
667
668 return ret;
669}
670
Dan Ehrenbergce95d652012-08-16 08:58:21 +0200671unsigned long long get_start_offset(struct thread_data *td)
672{
673 return td->o.start_offset +
674 (td->thread_number - 1) * td->o.offset_increment;
675}
676
Jens Axboe7bb48f82007-03-27 15:30:28 +0200677/*
678 * Open the files and setup files sizes, creating files if necessary.
679 */
680int setup_files(struct thread_data *td)
681{
682 unsigned long long total_size, extend_size;
683 struct fio_file *f;
684 unsigned int i;
Jens Axboe000b0802007-03-27 15:56:10 +0200685 int err = 0, need_extend;
Jens Axboe53cdc682006-10-18 11:50:58 +0200686
Jens Axboeee56ad52008-02-01 10:30:20 +0100687 dprint(FD_FILE, "setup files\n");
688
Jens Axboe691c8fb2008-03-07 14:26:26 +0100689 if (td->o.read_iolog_file)
Jens Axboe25460cf2012-05-02 13:58:02 +0200690 goto done;
Jens Axboe691c8fb2008-03-07 14:26:26 +0100691
Jens Axboe53cdc682006-10-18 11:50:58 +0200692 /*
693 * if ioengine defines a setup() method, it's responsible for
Jens Axboe7bb48f82007-03-27 15:30:28 +0200694 * opening the files and setting f->real_file_size to indicate
695 * the valid range for that file.
Jens Axboe53cdc682006-10-18 11:50:58 +0200696 */
697 if (td->io_ops->setup)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200698 err = td->io_ops->setup(td);
699 else
Jens Axboebab3fd52007-04-02 10:34:18 +0200700 err = get_file_sizes(td);
Jens Axboe53cdc682006-10-18 11:50:58 +0200701
Jens Axboef1027062006-10-20 10:14:01 +0200702 if (err)
703 return err;
704
Jens Axboe0a7eb122006-10-20 10:36:42 +0200705 /*
Jens Axboe7bb48f82007-03-27 15:30:28 +0200706 * check sizes. if the files/devices do not exist and the size
707 * isn't passed to fio, abort.
Jens Axboe0a7eb122006-10-20 10:36:42 +0200708 */
Jens Axboe7bb48f82007-03-27 15:30:28 +0200709 total_size = 0;
710 for_each_file(td, f, i) {
711 if (f->real_file_size == -1ULL)
712 total_size = -1ULL;
713 else
714 total_size += f->real_file_size;
715 }
Jens Axboe0a7eb122006-10-20 10:36:42 +0200716
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200717 if (td->o.fill_device)
718 td->fill_device_size = get_fs_free_counts(td);
719
Jens Axboe7bb48f82007-03-27 15:30:28 +0200720 /*
721 * device/file sizes are zero and no size given, punt
722 */
Jens Axboe1f809d12007-10-25 18:34:02 +0200723 if ((!total_size || total_size == -1ULL) && !td->o.size &&
Shawn Lewisaa31f1f2008-01-11 09:45:11 +0100724 !(td->io_ops->flags & FIO_NOIO) && !td->o.fill_device) {
Jens Axboe7bb48f82007-03-27 15:30:28 +0200725 log_err("%s: you need to specify size=\n", td->o.name);
Jens Axboee1161c32007-02-22 19:36:48 +0100726 td_verror(td, EINVAL, "total_file_size");
Jens Axboe53cdc682006-10-18 11:50:58 +0200727 return 1;
728 }
729
Jens Axboe7bb48f82007-03-27 15:30:28 +0200730 /*
731 * now file sizes are known, so we can set ->io_size. if size= is
732 * not given, ->io_size is just equal to ->real_file_size. if size
733 * is given, ->io_size is size / nr_files.
734 */
735 extend_size = total_size = 0;
736 need_extend = 0;
737 for_each_file(td, f, i) {
Dan Ehrenbergce95d652012-08-16 08:58:21 +0200738 f->file_offset = get_start_offset(td);
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200739
Jens Axboe7bb48f82007-03-27 15:30:28 +0200740 if (!td->o.file_size_low) {
741 /*
742 * no file size range given, file size is equal to
743 * total size divided by number of files. if that is
744 * zero, set it to the real file size.
745 */
746 f->io_size = td->o.size / td->o.nr_files;
Jens Axboe65bdb102008-01-24 13:13:12 +0100747 if (!f->io_size)
Jens Axboe273f8c92008-01-25 14:02:15 +0100748 f->io_size = f->real_file_size - f->file_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200749 } else if (f->real_file_size < td->o.file_size_low ||
750 f->real_file_size > td->o.file_size_high) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100751 if (f->file_offset > td->o.file_size_low)
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200752 goto err_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200753 /*
754 * file size given. if it's fixed, use that. if it's a
755 * range, generate a random size in-between.
756 */
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100757 if (td->o.file_size_low == td->o.file_size_high) {
758 f->io_size = td->o.file_size_low
759 - f->file_offset;
760 } else {
761 f->io_size = get_rand_file_size(td)
762 - f->file_offset;
763 }
Jens Axboe65bdb102008-01-24 13:13:12 +0100764 } else
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200765 f->io_size = f->real_file_size - f->file_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200766
767 if (f->io_size == -1ULL)
768 total_size = -1ULL;
Shaohua Li4d002562012-09-21 08:32:32 +0200769 else {
770 if (td->o.size_percent)
771 f->io_size = (f->io_size * td->o.size_percent) / 100;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200772 total_size += f->io_size;
Shaohua Li4d002562012-09-21 08:32:32 +0200773 }
Jens Axboe7bb48f82007-03-27 15:30:28 +0200774
775 if (f->filetype == FIO_TYPE_FILE &&
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200776 (f->io_size + f->file_offset) > f->real_file_size &&
Jens Axboe7bb48f82007-03-27 15:30:28 +0200777 !(td->io_ops->flags & FIO_DISKLESSIO)) {
Jens Axboe814452b2009-03-04 12:53:13 +0100778 if (!td->o.create_on_open) {
779 need_extend++;
780 extend_size += (f->io_size + f->file_offset);
781 } else
782 f->real_file_size = f->io_size + f->file_offset;
Jens Axboed6aed792009-06-03 08:41:15 +0200783 fio_file_set_extend(f);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100784 }
Jens Axboe7bb48f82007-03-27 15:30:28 +0200785 }
786
ljzhang,Yaxin Hu,Jianchao Tang22982902007-07-27 13:21:28 +0200787 if (!td->o.size || td->o.size > total_size)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200788 td->o.size = total_size;
789
790 /*
791 * See if we need to extend some files
792 */
793 if (need_extend) {
794 temp_stall_ts = 1;
Jens Axboef3afa572012-09-17 13:34:16 +0200795 if (output_format == FIO_OUTPUT_NORMAL)
Shaozhi Shawn Yea7ba8c52008-09-10 09:04:18 +0200796 log_info("%s: Laying out IO file(s) (%u file(s) /"
Bruce Cran0f2152c2010-12-15 10:33:03 +0100797 " %lluMB)\n", td->o.name, need_extend,
Shaozhi Shawn Yea7ba8c52008-09-10 09:04:18 +0200798 extend_size >> 20);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200799
800 for_each_file(td, f, i) {
Jens Axboe5e0074c2009-06-02 21:40:09 +0200801 unsigned long long old_len = -1ULL, extend_len = -1ULL;
Jens Axboe3baddf22008-04-04 11:10:30 +0200802
Jens Axboed6aed792009-06-03 08:41:15 +0200803 if (!fio_file_extend(f))
Jens Axboe7bb48f82007-03-27 15:30:28 +0200804 continue;
805
Jens Axboe409b3412007-03-28 09:57:01 +0200806 assert(f->filetype == FIO_TYPE_FILE);
Jens Axboed6aed792009-06-03 08:41:15 +0200807 fio_file_clear_extend(f);
Jens Axboe5e0074c2009-06-02 21:40:09 +0200808 if (!td->o.fill_device) {
809 old_len = f->real_file_size;
Jens Axboe0b9d69e2009-09-11 22:29:54 +0200810 extend_len = f->io_size + f->file_offset -
811 old_len;
Jens Axboe5e0074c2009-06-02 21:40:09 +0200812 }
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200813 f->real_file_size = (f->io_size + f->file_offset);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200814 err = extend_file(td, f);
815 if (err)
816 break;
Jens Axboe5e0074c2009-06-02 21:40:09 +0200817
Jens Axboe3baddf22008-04-04 11:10:30 +0200818 err = __file_invalidate_cache(td, f, old_len,
819 extend_len);
820 close(f->fd);
821 f->fd = -1;
822 if (err)
823 break;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200824 }
825 temp_stall_ts = 0;
826 }
827
828 if (err)
829 return err;
830
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100831 if (!td->o.zone_size)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200832 td->o.zone_size = td->o.size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200833
Jens Axboeea966f82007-09-03 10:09:37 +0200834 /*
835 * iolog already set the total io size, if we read back
836 * stored entries.
837 */
838 if (!td->o.read_iolog_file)
839 td->total_io_size = td->o.size * td->o.loops;
Jens Axboe25460cf2012-05-02 13:58:02 +0200840
841done:
842 if (td->o.create_only)
843 td->done = 1;
844
Jens Axboe7bb48f82007-03-27 15:30:28 +0200845 return 0;
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200846err_offset:
847 log_err("%s: you need to specify valid offset=\n", td->o.name);
848 return 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200849}
850
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200851int pre_read_files(struct thread_data *td)
852{
853 struct fio_file *f;
854 unsigned int i;
855
856 dprint(FD_FILE, "pre_read files\n");
857
858 for_each_file(td, f, i) {
859 pre_read_file(td, f);
860 }
861
862 return 1;
863}
864
Jens Axboe68727072007-03-15 20:49:25 +0100865int init_random_map(struct thread_data *td)
866{
Jens Axboe509eab12007-12-14 12:42:53 +0100867 unsigned long long blocks, num_maps;
Jens Axboe68727072007-03-15 20:49:25 +0100868 struct fio_file *f;
869 unsigned int i;
870
Jens Axboede8dd112008-03-01 15:34:44 +0100871 if (td->o.norandommap || !td_random(td))
Jens Axboe68727072007-03-15 20:49:25 +0100872 return 0;
873
874 for_each_file(td, f, i) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100875 blocks = (f->real_file_size + td->o.rw_min_bs - 1) /
876 (unsigned long long) td->o.rw_min_bs;
877 num_maps = (blocks + BLOCKS_PER_MAP - 1) /
878 (unsigned long long) BLOCKS_PER_MAP;
Jens Axboeceadd592012-02-02 08:41:28 +0100879 if (num_maps == (unsigned long) num_maps) {
880 f->file_map = smalloc(num_maps * sizeof(unsigned long));
881 if (f->file_map) {
882 f->num_maps = num_maps;
883 continue;
884 }
885 } else
886 f->file_map = NULL;
887
Jens Axboe2b386d22008-03-26 10:32:57 +0100888 if (!td->o.softrandommap) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100889 log_err("fio: failed allocating random map. If running"
890 " a large number of jobs, try the 'norandommap'"
Jens Axboe2b386d22008-03-26 10:32:57 +0100891 " option or set 'softrandommap'. Or give"
892 " a larger --alloc-size to fio.\n");
Jens Axboe68727072007-03-15 20:49:25 +0100893 return 1;
894 }
Jens Axboe303032a2008-03-26 10:11:10 +0100895
896 log_info("fio: file %s failed allocating random map. Running "
897 "job without.\n", f->file_name);
898 f->num_maps = 0;
Jens Axboe68727072007-03-15 20:49:25 +0100899 }
900
901 return 0;
902}
903
Jens Axboe53cdc682006-10-18 11:50:58 +0200904void close_files(struct thread_data *td)
905{
Jens Axboe0ab8db82006-10-18 17:16:23 +0200906 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100907 unsigned int i;
Jens Axboe53cdc682006-10-18 11:50:58 +0200908
Jens Axboe2be3eec2009-06-10 09:05:13 +0200909 for_each_file(td, f, i) {
910 if (fio_file_open(f))
911 td_io_close_file(td, f);
912 }
Jens Axboe24ffd2c2008-03-01 15:47:08 +0100913}
914
915void close_and_free_files(struct thread_data *td)
916{
917 struct fio_file *f;
918 unsigned int i;
919
Jens Axboeee56ad52008-02-01 10:30:20 +0100920 dprint(FD_FILE, "close files\n");
921
Jens Axboe0ab8db82006-10-18 17:16:23 +0200922 for_each_file(td, f, i) {
Jens Axboeffdfabd2008-03-26 09:18:14 +0100923 if (td->o.unlink && f->filetype == FIO_TYPE_FILE) {
924 dprint(FD_FILE, "free unlink %s\n", f->file_name);
Jens Axboe132ad462006-11-23 12:23:12 +0100925 unlink(f->file_name);
Jens Axboeffdfabd2008-03-26 09:18:14 +0100926 }
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100927
Jens Axboe22a57ba2009-06-09 14:34:35 +0200928 if (fio_file_open(f))
929 td_io_close_file(td, f);
930
Shaozhi Shawn Yeb9fbcf22008-09-10 09:05:17 +0200931 remove_file_hash(f);
Jens Axboeb3dc7f02007-03-09 12:52:15 +0100932
Jens Axboef17c4392008-03-01 18:40:46 +0100933 sfree(f->file_name);
Jens Axboefa1da862007-03-23 15:20:54 +0100934 f->file_name = NULL;
Jens Axboe61eb3132010-03-31 20:05:59 +0200935 sfree(f->file_map);
936 f->file_map = NULL;
Jens Axboe78d99e62008-03-01 18:41:50 +0100937 sfree(f);
Jens Axboe53cdc682006-10-18 11:50:58 +0200938 }
Jens Axboeb4a6a592006-10-20 13:54:47 +0200939
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100940 td->o.filename = NULL;
Jens Axboecade3ef2007-03-23 15:29:45 +0100941 free(td->files);
Jens Axboe9efef3c2008-03-06 10:26:25 +0100942 td->files_index = 0;
Jens Axboeb4a6a592006-10-20 13:54:47 +0200943 td->files = NULL;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100944 td->o.nr_files = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200945}
Jens Axboeaf52b342007-03-13 10:07:47 +0100946
Jens Axboee3bab462007-03-13 11:22:09 +0100947static void get_file_type(struct fio_file *f)
Jens Axboeaf52b342007-03-13 10:07:47 +0100948{
949 struct stat sb;
950
Jens Axboe66159822007-04-16 21:54:24 +0200951 if (!strcmp(f->file_name, "-"))
952 f->filetype = FIO_TYPE_PIPE;
953 else
954 f->filetype = FIO_TYPE_FILE;
Jens Axboeaf52b342007-03-13 10:07:47 +0100955
Bruce Cran38921822012-02-22 17:55:16 +0000956 /* \\.\ is the device namespace in Windows, where every file is
957 * a block device */
958 if (strncmp(f->file_name, "\\\\.\\", 4) == 0)
959 f->filetype = FIO_TYPE_BD;
960
Jens Axboeb30d3952010-02-06 23:36:26 +0100961 if (!stat(f->file_name, &sb)) {
Bruce Cran38921822012-02-22 17:55:16 +0000962 if (S_ISBLK(sb.st_mode))
Jens Axboeaf52b342007-03-13 10:07:47 +0100963 f->filetype = FIO_TYPE_BD;
964 else if (S_ISCHR(sb.st_mode))
965 f->filetype = FIO_TYPE_CHAR;
Jens Axboeb5605e92007-04-16 20:42:33 +0200966 else if (S_ISFIFO(sb.st_mode))
967 f->filetype = FIO_TYPE_PIPE;
Jens Axboeaf52b342007-03-13 10:07:47 +0100968 }
969}
970
Jens Axboef29b25a2007-07-23 08:56:43 +0200971int add_file(struct thread_data *td, const char *fname)
Jens Axboeaf52b342007-03-13 10:07:47 +0100972{
Jens Axboe7b4e4fe2007-03-13 12:51:40 +0100973 int cur_files = td->files_index;
Jens Axboebd0ee742007-03-23 14:26:23 +0100974 char file_name[PATH_MAX];
Jens Axboeaf52b342007-03-13 10:07:47 +0100975 struct fio_file *f;
Jens Axboebd0ee742007-03-23 14:26:23 +0100976 int len = 0;
Jens Axboeaf52b342007-03-13 10:07:47 +0100977
Jens Axboeee56ad52008-02-01 10:30:20 +0100978 dprint(FD_FILE, "add file %s\n", fname);
979
Jens Axboef17c4392008-03-01 18:40:46 +0100980 f = smalloc(sizeof(*f));
Jens Axboec48c0be2008-07-31 19:55:02 +0200981 if (!f) {
982 log_err("fio: smalloc OOM\n");
983 assert(0);
984 }
Jens Axboe0b9d69e2009-09-11 22:29:54 +0200985
Jens Axboeaf52b342007-03-13 10:07:47 +0100986 f->fd = -1;
Jens Axboe38dad622010-07-20 14:46:00 -0600987 fio_file_reset(f);
Jens Axboebd0ee742007-03-23 14:26:23 +0100988
Jens Axboefc99bc02009-03-04 15:27:42 +0100989 if (td->files_size <= td->files_index) {
Carl Henrik Lunde4b341fc2009-04-20 08:41:37 +0200990 int new_size = td->o.nr_files + 1;
Jens Axboe126d65c2008-03-01 18:04:31 +0100991
Jens Axboefc99bc02009-03-04 15:27:42 +0100992 dprint(FD_FILE, "resize file array to %d files\n", new_size);
993
994 td->files = realloc(td->files, new_size * sizeof(f));
Jens Axboefc99bc02009-03-04 15:27:42 +0100995 td->files_size = new_size;
996 }
Jens Axboe8bb76792009-03-04 15:37:52 +0100997 td->files[cur_files] = f;
Shaohua Li89ac1d42012-06-11 08:56:32 +0200998 f->fileno = cur_files;
Jens Axboe126d65c2008-03-01 18:04:31 +0100999
Jens Axboe07eb79d2007-04-12 13:02:38 +02001000 /*
1001 * init function, io engine may not be loaded yet
1002 */
1003 if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO))
1004 f->real_file_size = -1ULL;
1005
Jens Axboebd0ee742007-03-23 14:26:23 +01001006 if (td->o.directory)
1007 len = sprintf(file_name, "%s/", td->o.directory);
1008
1009 sprintf(file_name + len, "%s", fname);
Jens Axboef17c4392008-03-01 18:40:46 +01001010 f->file_name = smalloc_strdup(file_name);
Jens Axboec48c0be2008-07-31 19:55:02 +02001011 if (!f->file_name) {
1012 log_err("fio: smalloc OOM\n");
1013 assert(0);
1014 }
Jens Axboe0b9d69e2009-09-11 22:29:54 +02001015
Jens Axboee3bab462007-03-13 11:22:09 +01001016 get_file_type(f);
Jens Axboeaf52b342007-03-13 10:07:47 +01001017
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001018 switch (td->o.file_lock_mode) {
1019 case FILE_LOCK_NONE:
1020 break;
1021 case FILE_LOCK_READWRITE:
1022 f->lock = fio_mutex_rw_init();
1023 break;
1024 case FILE_LOCK_EXCLUSIVE:
Jens Axboe521da522012-08-02 11:21:36 +02001025 f->lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001026 break;
1027 default:
1028 log_err("fio: unknown lock mode: %d\n", td->o.file_lock_mode);
1029 assert(0);
1030 }
Jens Axboe29c13492008-03-01 19:25:20 +01001031
Jens Axboe7b4e4fe2007-03-13 12:51:40 +01001032 td->files_index++;
Jens Axboe15494412007-03-13 11:52:47 +01001033 if (f->filetype == FIO_TYPE_FILE)
1034 td->nr_normal_files++;
Jens Axboef29b25a2007-07-23 08:56:43 +02001035
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001036 dprint(FD_FILE, "file %p \"%s\" added at %d\n", f, f->file_name,
1037 cur_files);
Jens Axboe9efef3c2008-03-06 10:26:25 +01001038
Jens Axboef29b25a2007-07-23 08:56:43 +02001039 return cur_files;
Jens Axboeaf52b342007-03-13 10:07:47 +01001040}
Jens Axboe0ad920e2007-03-13 11:06:45 +01001041
Jens Axboe49ffb4a2010-08-24 15:01:37 +02001042int add_file_exclusive(struct thread_data *td, const char *fname)
1043{
1044 struct fio_file *f;
1045 unsigned int i;
1046
1047 for_each_file(td, f, i) {
1048 if (!strcmp(f->file_name, fname))
1049 return i;
1050 }
1051
1052 return add_file(td, fname);
1053}
1054
Jens Axboe0ad920e2007-03-13 11:06:45 +01001055void get_file(struct fio_file *f)
1056{
Jens Axboe8172fe92008-02-01 21:51:02 +01001057 dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references);
Jens Axboed6aed792009-06-03 08:41:15 +02001058 assert(fio_file_open(f));
Jens Axboe0ad920e2007-03-13 11:06:45 +01001059 f->references++;
1060}
1061
Jens Axboe6977bcd2008-03-01 15:55:36 +01001062int put_file(struct thread_data *td, struct fio_file *f)
Jens Axboe0ad920e2007-03-13 11:06:45 +01001063{
Jens Axboe98e1ac42008-03-06 11:56:48 +01001064 int f_ret = 0, ret = 0;
Jens Axboe6977bcd2008-03-01 15:55:36 +01001065
Jens Axboe8172fe92008-02-01 21:51:02 +01001066 dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references);
Jens Axboeee56ad52008-02-01 10:30:20 +01001067
Jens Axboe22a57ba2009-06-09 14:34:35 +02001068 if (!fio_file_open(f)) {
1069 assert(f->fd == -1);
Jens Axboe6977bcd2008-03-01 15:55:36 +01001070 return 0;
Jens Axboe22a57ba2009-06-09 14:34:35 +02001071 }
Jens Axboe0ad920e2007-03-13 11:06:45 +01001072
1073 assert(f->references);
1074 if (--f->references)
Jens Axboe6977bcd2008-03-01 15:55:36 +01001075 return 0;
Jens Axboe0ad920e2007-03-13 11:06:45 +01001076
Jens Axboed424d4d2007-04-17 09:05:10 +02001077 if (should_fsync(td) && td->o.fsync_on_close)
Jens Axboe98e1ac42008-03-06 11:56:48 +01001078 f_ret = fsync(f->fd);
Jens Axboeebb14152007-03-13 14:42:15 +01001079
Jens Axboe0ad920e2007-03-13 11:06:45 +01001080 if (td->io_ops->close_file)
Jens Axboe6977bcd2008-03-01 15:55:36 +01001081 ret = td->io_ops->close_file(td, f);
Jens Axboe1020a132007-03-29 11:15:06 +02001082
Jens Axboe98e1ac42008-03-06 11:56:48 +01001083 if (!ret)
Jens Axboea5fb4612008-05-28 10:54:01 +02001084 ret = f_ret;
Jens Axboe98e1ac42008-03-06 11:56:48 +01001085
Jens Axboe0ad920e2007-03-13 11:06:45 +01001086 td->nr_open_files--;
Jens Axboed6aed792009-06-03 08:41:15 +02001087 fio_file_clear_open(f);
Jens Axboe22a57ba2009-06-09 14:34:35 +02001088 assert(f->fd == -1);
Jens Axboe6977bcd2008-03-01 15:55:36 +01001089 return ret;
Jens Axboe0ad920e2007-03-13 11:06:45 +01001090}
Jens Axboebbf6b542007-03-13 15:28:55 +01001091
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001092void lock_file(struct thread_data *td, struct fio_file *f, enum fio_ddir ddir)
Jens Axboeb2bd2bd2008-03-01 18:19:52 +01001093{
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001094 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1095 return;
Jens Axboe29c13492008-03-01 19:25:20 +01001096
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001097 if (f->lock_owner == td && f->lock_batch--)
1098 return;
1099
1100 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
1101 if (ddir == DDIR_READ)
1102 fio_mutex_down_read(f->lock);
1103 else
1104 fio_mutex_down_write(f->lock);
1105 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1106 fio_mutex_down(f->lock);
1107
1108 f->lock_owner = td;
1109 f->lock_batch = td->o.lockfile_batch;
1110 f->lock_ddir = ddir;
1111}
1112
1113void unlock_file(struct thread_data *td, struct fio_file *f)
1114{
1115 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1116 return;
1117 if (f->lock_batch)
1118 return;
1119
1120 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
1121 const int is_read = f->lock_ddir == DDIR_READ;
1122 int val = fio_mutex_getval(f->lock);
1123
1124 if ((is_read && val == 1) || (!is_read && val == -1))
1125 f->lock_owner = NULL;
1126
1127 if (is_read)
1128 fio_mutex_up_read(f->lock);
1129 else
1130 fio_mutex_up_write(f->lock);
1131 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE) {
1132 int val = fio_mutex_getval(f->lock);
1133
1134 if (val == 0)
1135 f->lock_owner = NULL;
1136
1137 fio_mutex_up(f->lock);
Jens Axboe29c13492008-03-01 19:25:20 +01001138 }
Jens Axboeb2bd2bd2008-03-01 18:19:52 +01001139}
1140
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001141void unlock_file_all(struct thread_data *td, struct fio_file *f)
Jens Axboeb2bd2bd2008-03-01 18:19:52 +01001142{
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001143 if (f->lock_owner != td)
1144 return;
Jens Axboe29c13492008-03-01 19:25:20 +01001145
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001146 f->lock_batch = 0;
1147 unlock_file(td, f);
Jens Axboeb2bd2bd2008-03-01 18:19:52 +01001148}
1149
Jens Axboebbf6b542007-03-13 15:28:55 +01001150static int recurse_dir(struct thread_data *td, const char *dirname)
1151{
1152 struct dirent *dir;
1153 int ret = 0;
1154 DIR *D;
1155
1156 D = opendir(dirname);
1157 if (!D) {
Jens Axboe0ddb2702007-03-27 19:43:53 +02001158 char buf[FIO_VERROR_SIZE];
1159
1160 snprintf(buf, FIO_VERROR_SIZE - 1, "opendir(%s)", dirname);
1161 td_verror(td, errno, buf);
Jens Axboebbf6b542007-03-13 15:28:55 +01001162 return 1;
1163 }
1164
1165 while ((dir = readdir(D)) != NULL) {
1166 char full_path[PATH_MAX];
1167 struct stat sb;
1168
Jens Axboee85b2b82007-03-14 09:39:06 +01001169 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
1170 continue;
Jens Axboe96d32d52007-03-14 09:16:09 +01001171
Bruce Cranb9fd7882012-02-20 17:01:46 +00001172 sprintf(full_path, "%s%s%s", dirname, FIO_OS_PATH_SEPARATOR, dir->d_name);
Jens Axboebbf6b542007-03-13 15:28:55 +01001173
1174 if (lstat(full_path, &sb) == -1) {
1175 if (errno != ENOENT) {
1176 td_verror(td, errno, "stat");
1177 return 1;
1178 }
1179 }
1180
1181 if (S_ISREG(sb.st_mode)) {
1182 add_file(td, full_path);
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001183 td->o.nr_files++;
Jens Axboebbf6b542007-03-13 15:28:55 +01001184 continue;
1185 }
Jens Axboe0ddb2702007-03-27 19:43:53 +02001186 if (!S_ISDIR(sb.st_mode))
1187 continue;
Jens Axboebbf6b542007-03-13 15:28:55 +01001188
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001189 ret = recurse_dir(td, full_path);
1190 if (ret)
Jens Axboebbf6b542007-03-13 15:28:55 +01001191 break;
1192 }
1193
1194 closedir(D);
1195 return ret;
1196}
1197
1198int add_dir_files(struct thread_data *td, const char *path)
1199{
Jens Axboe0ddb2702007-03-27 19:43:53 +02001200 int ret = recurse_dir(td, path);
1201
1202 if (!ret)
1203 log_info("fio: opendir added %d files\n", td->o.nr_files);
1204
1205 return ret;
Jens Axboebbf6b542007-03-13 15:28:55 +01001206}
Jens Axboecade3ef2007-03-23 15:29:45 +01001207
1208void dup_files(struct thread_data *td, struct thread_data *org)
1209{
1210 struct fio_file *f;
1211 unsigned int i;
Jens Axboe9efef3c2008-03-06 10:26:25 +01001212
1213 dprint(FD_FILE, "dup files: %d\n", org->files_index);
Jens Axboecade3ef2007-03-23 15:29:45 +01001214
1215 if (!org->files)
1216 return;
1217
Jens Axboe9efef3c2008-03-06 10:26:25 +01001218 td->files = malloc(org->files_index * sizeof(f));
Jens Axboecade3ef2007-03-23 15:29:45 +01001219
Jens Axboe9efef3c2008-03-06 10:26:25 +01001220 for_each_file(org, f, i) {
Jens Axboeb0fe4212008-03-01 18:22:27 +01001221 struct fio_file *__f;
1222
Jens Axboef17c4392008-03-01 18:40:46 +01001223 __f = smalloc(sizeof(*__f));
Jens Axboec48c0be2008-07-31 19:55:02 +02001224 if (!__f) {
1225 log_err("fio: smalloc OOM\n");
1226 assert(0);
1227 }
Jens Axboe22a57ba2009-06-09 14:34:35 +02001228 __f->fd = -1;
Jens Axboe38dad622010-07-20 14:46:00 -06001229 fio_file_reset(__f);
Jens Axboe0b9d69e2009-09-11 22:29:54 +02001230
Aaron Carrollbc3456f2008-06-25 09:20:37 +02001231 if (f->file_name) {
Jens Axboef17c4392008-03-01 18:40:46 +01001232 __f->file_name = smalloc_strdup(f->file_name);
Jens Axboec48c0be2008-07-31 19:55:02 +02001233 if (!__f->file_name) {
1234 log_err("fio: smalloc OOM\n");
1235 assert(0);
1236 }
Jens Axboe0b9d69e2009-09-11 22:29:54 +02001237
Aaron Carrollbc3456f2008-06-25 09:20:37 +02001238 __f->filetype = f->filetype;
1239 }
Jens Axboeb0fe4212008-03-01 18:22:27 +01001240
1241 td->files[i] = __f;
Jens Axboecade3ef2007-03-23 15:29:45 +01001242 }
1243}
Jens Axboef29b25a2007-07-23 08:56:43 +02001244
1245/*
1246 * Returns the index that matches the filename, or -1 if not there
1247 */
1248int get_fileno(struct thread_data *td, const char *fname)
1249{
1250 struct fio_file *f;
1251 unsigned int i;
1252
1253 for_each_file(td, f, i)
1254 if (!strcmp(f->file_name, fname))
1255 return i;
1256
1257 return -1;
1258}
1259
1260/*
1261 * For log usage, where we add/open/close files automatically
1262 */
1263void free_release_files(struct thread_data *td)
1264{
1265 close_files(td);
1266 td->files_index = 0;
1267 td->nr_normal_files = 0;
1268}