blob: 70589aa513eaa95d6108664df88d43270d8ba475 [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>
Jens Axboe53cdc682006-10-18 11:50:58 +02006#include <sys/stat.h>
7#include <sys/mman.h>
Jens Axboebbf6b542007-03-13 15:28:55 +01008#include <sys/types.h>
Jens Axboe53cdc682006-10-18 11:50:58 +02009
10#include "fio.h"
Jens Axboef17c4392008-03-01 18:40:46 +010011#include "smalloc.h"
Jens Axboe4906e0b2008-03-01 18:59:51 +010012#include "filehash.h"
Jens Axboe53cdc682006-10-18 11:50:58 +020013
Jens Axboe7172cfe2007-07-23 14:36:00 +020014static int root_warn;
15
Jens Axboec592b9f2009-06-03 09:29:28 +020016static inline void clear_error(struct thread_data *td)
17{
18 td->error = 0;
19 td->verror[0] = '\0';
20}
21
Jens Axboe3baddf22008-04-04 11:10:30 +020022/*
23 * Leaves f->fd open on success, caller must close
24 */
Jens Axboe7bb48f82007-03-27 15:30:28 +020025static int extend_file(struct thread_data *td, struct fio_file *f)
Jens Axboe25205e92006-10-19 20:50:14 +020026{
Jens Axboeea443652007-03-29 14:52:13 +020027 int r, new_layout = 0, unlink_file = 0, flags;
Jens Axboe25205e92006-10-19 20:50:14 +020028 unsigned long long left;
29 unsigned int bs;
30 char *b;
Jens Axboeb2a15192006-10-18 14:10:42 +020031
Jens Axboe4241ea82007-09-12 08:18:36 +020032 if (read_only) {
33 log_err("fio: refusing extend of file due to read-only\n");
34 return 0;
35 }
36
Jens Axboe507a7022007-03-27 15:52:46 +020037 /*
38 * check if we need to lay the file out complete again. fio
39 * does that for operations involving reads, or for writes
40 * where overwrite is set
41 */
Jens Axboeffdfabd2008-03-26 09:18:14 +010042 if (td_read(td) || (td_write(td) && td->o.overwrite) ||
43 (td_write(td) && td->io_ops->flags & FIO_NOEXTEND))
Jens Axboe507a7022007-03-27 15:52:46 +020044 new_layout = 1;
Jens Axboeea443652007-03-29 14:52:13 +020045 if (td_write(td) && !td->o.overwrite)
46 unlink_file = 1;
Jens Axboe507a7022007-03-27 15:52:46 +020047
Jens Axboe6ae1f572008-02-21 10:20:00 +010048 if (unlink_file || new_layout) {
Jens Axboebd199f22008-03-26 09:57:18 +010049 dprint(FD_FILE, "layout unlink %s\n", f->file_name);
Zhang, Yanmin982016d2008-02-26 15:35:52 +010050 if ((unlink(f->file_name) < 0) && (errno != ENOENT)) {
Jens Axboe7bb48f82007-03-27 15:30:28 +020051 td_verror(td, errno, "unlink");
52 return 1;
53 }
54 }
55
Jens Axboe507a7022007-03-27 15:52:46 +020056 flags = O_WRONLY | O_CREAT;
57 if (new_layout)
58 flags |= O_TRUNC;
59
Jens Axboeee56ad52008-02-01 10:30:20 +010060 dprint(FD_FILE, "open file %s, flags %x\n", f->file_name, flags);
Jens Axboe507a7022007-03-27 15:52:46 +020061 f->fd = open(f->file_name, flags, 0644);
Jens Axboe53cdc682006-10-18 11:50:58 +020062 if (f->fd < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +010063 td_verror(td, errno, "open");
Jens Axboe53cdc682006-10-18 11:50:58 +020064 return 1;
65 }
66
Shawn Lewisfc74ac12008-01-11 09:45:11 +010067 if (!new_layout)
68 goto done;
69
Jens Axboe5e0074c2009-06-02 21:40:09 +020070 /*
71 * The size will be -1ULL when fill_device is used, so don't truncate
72 * or fallocate this file, just write it
73 */
74 if (!td->o.fill_device) {
75 dprint(FD_FILE, "truncate file %s, size %llu\n", f->file_name,
Jens Axboeee56ad52008-02-01 10:30:20 +010076 f->real_file_size);
Jens Axboe5e0074c2009-06-02 21:40:09 +020077 if (ftruncate(f->fd, f->real_file_size) == -1) {
78 td_verror(td, errno, "ftruncate");
79 goto err;
80 }
Jens Axboe53cdc682006-10-18 11:50:58 +020081
Jens Axboefffca022008-06-02 12:23:40 +020082#ifdef FIO_HAVE_FALLOCATE
Jens Axboe5e0074c2009-06-02 21:40:09 +020083 dprint(FD_FILE, "fallocate file %s, size %llu\n", f->file_name,
Jens Axboeee56ad52008-02-01 10:30:20 +010084 f->real_file_size);
Jens Axboe5e0074c2009-06-02 21:40:09 +020085 r = posix_fallocate(f->fd, 0, f->real_file_size);
86 if (r < 0) {
87 log_err("fio: posix_fallocate fails: %s\n",
88 strerror(-r));
89 }
Jens Axboefffca022008-06-02 12:23:40 +020090#endif
Jens Axboe5e0074c2009-06-02 21:40:09 +020091 }
Jens Axboe40f82982006-10-25 11:21:05 +020092
Jens Axboe2dc1bbe2007-03-15 15:01:33 +010093 b = malloc(td->o.max_bs[DDIR_WRITE]);
94 memset(b, 0, td->o.max_bs[DDIR_WRITE]);
Jens Axboe53cdc682006-10-18 11:50:58 +020095
Jens Axboe7bb48f82007-03-27 15:30:28 +020096 left = f->real_file_size;
Jens Axboe53cdc682006-10-18 11:50:58 +020097 while (left && !td->terminate) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +010098 bs = td->o.max_bs[DDIR_WRITE];
Jens Axboe53cdc682006-10-18 11:50:58 +020099 if (bs > left)
100 bs = left;
101
102 r = write(f->fd, b, bs);
103
Jens Axboe5e0074c2009-06-02 21:40:09 +0200104 if (r > 0) {
105 left -= r;
Jens Axboe53cdc682006-10-18 11:50:58 +0200106 continue;
107 } else {
Jens Axboe5e0074c2009-06-02 21:40:09 +0200108 if (r < 0) {
109 int __e = errno;
110
111 if (__e == ENOSPC) {
112 if (td->o.fill_device)
113 break;
114 log_info("fio: ENOSPC on laying out "
115 "file, stopping\n");
116 break;
117 }
Jens Axboee1161c32007-02-22 19:36:48 +0100118 td_verror(td, errno, "write");
Jens Axboe5e0074c2009-06-02 21:40:09 +0200119 } else
Jens Axboee1161c32007-02-22 19:36:48 +0100120 td_verror(td, EIO, "write");
Jens Axboe53cdc682006-10-18 11:50:58 +0200121
122 break;
123 }
124 }
125
Jens Axboeffdfabd2008-03-26 09:18:14 +0100126 if (td->terminate) {
127 dprint(FD_FILE, "terminate unlink %s\n", f->file_name);
Jens Axboe53cdc682006-10-18 11:50:58 +0200128 unlink(f->file_name);
Jens Axboeffdfabd2008-03-26 09:18:14 +0100129 } else if (td->o.create_fsync) {
Jens Axboe98e1ac42008-03-06 11:56:48 +0100130 if (fsync(f->fd) < 0) {
131 td_verror(td, errno, "fsync");
132 goto err;
133 }
134 }
Jens Axboe0d1cd202009-06-05 22:11:52 +0200135 if (td->o.fill_device && !td_write(td)) {
Jens Axboed6aed792009-06-03 08:41:15 +0200136 fio_file_clear_size_known(f);
Jens Axboe5e0074c2009-06-02 21:40:09 +0200137 if (td_io_get_file_size(td, f))
138 goto err;
139 if (f->io_size > f->real_file_size)
140 f->io_size = f->real_file_size;
141 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200142
143 free(b);
Jens Axboe507a7022007-03-27 15:52:46 +0200144done:
Jens Axboe53cdc682006-10-18 11:50:58 +0200145 return 0;
146err:
147 close(f->fd);
148 f->fd = -1;
149 return 1;
150}
151
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200152static int pre_read_file(struct thread_data *td, struct fio_file *f)
153{
Jens Axboeb0f65862009-05-20 11:52:15 +0200154 int r, did_open = 0, old_runstate;
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200155 unsigned long long left;
156 unsigned int bs;
157 char *b;
158
Jens Axboe9c0d2242009-07-01 12:26:28 +0200159 if (td->io_ops->flags & FIO_PIPEIO)
160 return 0;
161
Jens Axboed6aed792009-06-03 08:41:15 +0200162 if (!fio_file_open(f)) {
Jens Axboeb0f65862009-05-20 11:52:15 +0200163 if (td->io_ops->open_file(td, f)) {
164 log_err("fio: cannot pre-read, failed to open file\n");
165 return 1;
166 }
167 did_open = 1;
168 }
169
170 old_runstate = td->runstate;
171 td_set_runstate(td, TD_PRE_READING);
172
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200173 bs = td->o.max_bs[DDIR_READ];
174 b = malloc(bs);
175 memset(b, 0, bs);
176
177 lseek(f->fd, f->file_offset, SEEK_SET);
178 left = f->io_size;
179
180 while (left && !td->terminate) {
181 if (bs > left)
182 bs = left;
183
184 r = read(f->fd, b, bs);
185
186 if (r == (int) bs) {
187 left -= bs;
188 continue;
189 } else {
190 td_verror(td, EIO, "pre_read");
191 break;
192 }
193 }
194
Jens Axboeb0f65862009-05-20 11:52:15 +0200195 td_set_runstate(td, old_runstate);
196
197 if (did_open)
198 td->io_ops->close_file(td, f);
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200199 free(b);
200 return 0;
201}
202
Jens Axboe7bb48f82007-03-27 15:30:28 +0200203static unsigned long long get_rand_file_size(struct thread_data *td)
Jens Axboe9c60ce62007-03-15 09:14:47 +0100204{
Jens Axboedc873b62008-06-04 20:13:04 +0200205 unsigned long long ret, sized;
Jens Axboe9c60ce62007-03-15 09:14:47 +0100206 long r;
207
Jens Axboe9c60ce62007-03-15 09:14:47 +0100208 r = os_random_long(&td->file_size_state);
Jens Axboedc873b62008-06-04 20:13:04 +0200209 sized = td->o.file_size_high - td->o.file_size_low;
210 ret = (unsigned long long) ((double) sized * (r / (OS_RAND_MAX + 1.0)));
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100211 ret += td->o.file_size_low;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100212 ret -= (ret % td->o.rw_min_bs);
Jens Axboe9c60ce62007-03-15 09:14:47 +0100213 return ret;
214}
215
Jens Axboe53cdc682006-10-18 11:50:58 +0200216static int file_size(struct thread_data *td, struct fio_file *f)
217{
218 struct stat st;
219
Jens Axboedf9c26b2009-03-05 10:13:58 +0100220 if (stat(f->file_name, &st) == -1) {
Jens Axboe7bb48f82007-03-27 15:30:28 +0200221 td_verror(td, errno, "fstat");
222 return 1;
223 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200224
Jens Axboe7bb48f82007-03-27 15:30:28 +0200225 f->real_file_size = st.st_size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200226 return 0;
227}
228
229static int bdev_size(struct thread_data *td, struct fio_file *f)
230{
231 unsigned long long bytes;
232 int r;
233
Jens Axboedf9c26b2009-03-05 10:13:58 +0100234 if (td->io_ops->open_file(td, f)) {
235 log_err("fio: failed opening blockdev %s for size check\n",
236 f->file_name);
237 return 1;
238 }
239
Jens Axboe53cdc682006-10-18 11:50:58 +0200240 r = blockdev_size(f->fd, &bytes);
241 if (r) {
Jens Axboee1161c32007-02-22 19:36:48 +0100242 td_verror(td, r, "blockdev_size");
Jens Axboedf9c26b2009-03-05 10:13:58 +0100243 goto err;
Jens Axboe53cdc682006-10-18 11:50:58 +0200244 }
245
Jens Axboe7ab077a2009-02-02 15:07:37 +0100246 if (!bytes) {
247 log_err("%s: zero sized block device?\n", f->file_name);
Jens Axboedf9c26b2009-03-05 10:13:58 +0100248 goto err;
Jens Axboe7ab077a2009-02-02 15:07:37 +0100249 }
250
Jens Axboe53cdc682006-10-18 11:50:58 +0200251 f->real_file_size = bytes;
Jens Axboe22a57ba2009-06-09 14:34:35 +0200252 td->io_ops->close_file(td, f);
Jens Axboe53cdc682006-10-18 11:50:58 +0200253 return 0;
Jens Axboedf9c26b2009-03-05 10:13:58 +0100254err:
255 td->io_ops->close_file(td, f);
256 return 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200257}
258
259static int get_file_size(struct thread_data *td, struct fio_file *f)
260{
261 int ret = 0;
262
Jens Axboed6aed792009-06-03 08:41:15 +0200263 if (fio_file_size_known(f))
Jens Axboe409b3412007-03-28 09:57:01 +0200264 return 0;
265
Jens Axboe7bb48f82007-03-27 15:30:28 +0200266 if (f->filetype == FIO_TYPE_FILE)
267 ret = file_size(td, f);
268 else if (f->filetype == FIO_TYPE_BD)
Jens Axboe53cdc682006-10-18 11:50:58 +0200269 ret = bdev_size(td, f);
270 else
271 f->real_file_size = -1;
272
273 if (ret)
274 return ret;
275
276 if (f->file_offset > f->real_file_size) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100277 log_err("%s: offset extends end (%Lu > %Lu)\n", td->o.name,
278 f->file_offset, f->real_file_size);
Jens Axboe53cdc682006-10-18 11:50:58 +0200279 return 1;
280 }
281
Jens Axboed6aed792009-06-03 08:41:15 +0200282 fio_file_set_size_known(f);
Jens Axboe53cdc682006-10-18 11:50:58 +0200283 return 0;
284}
285
Jens Axboe3baddf22008-04-04 11:10:30 +0200286static int __file_invalidate_cache(struct thread_data *td, struct fio_file *f,
287 unsigned long long off,
288 unsigned long long len)
Jens Axboee5b401d2006-10-18 16:03:40 +0200289{
290 int ret = 0;
291
Jens Axboe5e0074c2009-06-02 21:40:09 +0200292 if (len == -1ULL)
Jens Axboe3baddf22008-04-04 11:10:30 +0200293 len = f->io_size;
294 if (off == -1ULL)
295 off = f->file_offset;
Jens Axboeee56ad52008-02-01 10:30:20 +0100296
Jens Axboe0d1cd202009-06-05 22:11:52 +0200297 if (len == -1ULL || off == -1ULL)
298 return 0;
299
Jens Axboe3baddf22008-04-04 11:10:30 +0200300 dprint(FD_IO, "invalidate cache %s: %llu/%llu\n", f->file_name, off,
301 len);
Jens Axboeb5af8292007-03-08 12:43:13 +0100302
Jens Axboee5b401d2006-10-18 16:03:40 +0200303 /*
304 * FIXME: add blockdev flushing too
305 */
Jens Axboea1c58072009-08-04 23:17:02 +0200306 if (f->mmap_ptr) {
Jens Axboeac893112009-06-02 13:06:01 +0200307 ret = madvise(f->mmap_ptr, f->mmap_sz, MADV_DONTNEED);
Jens Axboea1c58072009-08-04 23:17:02 +0200308#ifdef FIO_MADV_FREE
309 (void) madvise(f->mmap_ptr, f->mmap_sz, FIO_MADV_FREE);
310#endif
311 } else if (f->filetype == FIO_TYPE_FILE) {
Jens Axboe3baddf22008-04-04 11:10:30 +0200312 ret = fadvise(f->fd, off, len, POSIX_FADV_DONTNEED);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100313 } else if (f->filetype == FIO_TYPE_BD) {
Jens Axboeb5af8292007-03-08 12:43:13 +0100314 ret = blockdev_invalidate_cache(f->fd);
Jens Axboe7e0e25c2007-03-27 12:49:56 +0200315 if (ret < 0 && errno == EACCES && geteuid()) {
Jens Axboe7172cfe2007-07-23 14:36:00 +0200316 if (!root_warn) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100317 log_err("fio: only root may flush block "
318 "devices. Cache flush bypassed!\n");
Jens Axboe7172cfe2007-07-23 14:36:00 +0200319 root_warn = 1;
320 }
Jens Axboe7e0e25c2007-03-27 12:49:56 +0200321 ret = 0;
322 }
Jens Axboeb5605e92007-04-16 20:42:33 +0200323 } else if (f->filetype == FIO_TYPE_CHAR || f->filetype == FIO_TYPE_PIPE)
Jens Axboee5b401d2006-10-18 16:03:40 +0200324 ret = 0;
325
326 if (ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100327 td_verror(td, errno, "invalidate_cache");
Jens Axboee5b401d2006-10-18 16:03:40 +0200328 return 1;
Jens Axboe3baddf22008-04-04 11:10:30 +0200329 } else if (ret > 0) {
330 td_verror(td, ret, "invalidate_cache");
331 return 1;
Jens Axboee5b401d2006-10-18 16:03:40 +0200332 }
333
Jens Axboead2da602007-02-26 12:33:27 +0100334 return ret;
Jens Axboe3baddf22008-04-04 11:10:30 +0200335
336}
337
338int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
339{
Jens Axboed6aed792009-06-03 08:41:15 +0200340 if (!fio_file_open(f))
Jens Axboea5fb4612008-05-28 10:54:01 +0200341 return 0;
342
Jens Axboe5e0074c2009-06-02 21:40:09 +0200343 return __file_invalidate_cache(td, f, -1ULL, -1ULL);
Jens Axboee5b401d2006-10-18 16:03:40 +0200344}
345
Jens Axboe6977bcd2008-03-01 15:55:36 +0100346int generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
Jens Axboe53cdc682006-10-18 11:50:58 +0200347{
Jens Axboe6977bcd2008-03-01 15:55:36 +0100348 int ret = 0;
349
Jens Axboeee56ad52008-02-01 10:30:20 +0100350 dprint(FD_FILE, "fd close %s\n", f->file_name);
Jens Axboe4906e0b2008-03-01 18:59:51 +0100351
352 remove_file_hash(f);
353
Jens Axboe6977bcd2008-03-01 15:55:36 +0100354 if (close(f->fd) < 0)
355 ret = errno;
356
Jens Axboeb5af8292007-03-08 12:43:13 +0100357 f->fd = -1;
Jens Axboe6977bcd2008-03-01 15:55:36 +0100358 return ret;
Jens Axboe53cdc682006-10-18 11:50:58 +0200359}
360
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100361static int file_lookup_open(struct fio_file *f, int flags)
Jens Axboe53cdc682006-10-18 11:50:58 +0200362{
Jens Axboe29c13492008-03-01 19:25:20 +0100363 struct fio_file *__f;
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100364 int from_hash;
365
366 __f = lookup_file_hash(f->file_name);
367 if (__f) {
Jens Axboe9efef3c2008-03-06 10:26:25 +0100368 dprint(FD_FILE, "found file in hash %s\n", f->file_name);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100369 /*
370 * racy, need the __f->lock locked
371 */
372 f->lock = __f->lock;
373 f->lock_owner = __f->lock_owner;
374 f->lock_batch = __f->lock_batch;
375 f->lock_ddir = __f->lock_ddir;
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100376 from_hash = 1;
377 } else {
Jens Axboe9efef3c2008-03-06 10:26:25 +0100378 dprint(FD_FILE, "file not found in hash %s\n", f->file_name);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100379 from_hash = 0;
380 }
381
Jens Axboee8670ef2008-03-06 12:06:30 +0100382 f->fd = open(f->file_name, flags, 0600);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100383 return from_hash;
384}
385
386int generic_open_file(struct thread_data *td, struct fio_file *f)
387{
Jens Axboe66159822007-04-16 21:54:24 +0200388 int is_std = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200389 int flags = 0;
Jens Axboe29c13492008-03-01 19:25:20 +0100390 int from_hash = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200391
Jens Axboeee56ad52008-02-01 10:30:20 +0100392 dprint(FD_FILE, "fd open %s\n", f->file_name);
393
Jens Axboe66159822007-04-16 21:54:24 +0200394 if (!strcmp(f->file_name, "-")) {
395 if (td_rw(td)) {
396 log_err("fio: can't read/write to stdin/out\n");
397 return 1;
398 }
399 is_std = 1;
Jens Axboece98fa62007-04-17 13:27:32 +0200400
401 /*
402 * move output logging to stderr, if we are writing to stdout
403 */
404 if (td_write(td))
405 f_out = stderr;
Jens Axboe66159822007-04-16 21:54:24 +0200406 }
407
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100408 if (td->o.odirect)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100409 flags |= OS_O_DIRECT;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100410 if (td->o.sync_io)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100411 flags |= O_SYNC;
Jens Axboead923962007-10-11 21:41:41 +0200412 if (f->filetype != FIO_TYPE_FILE)
Jens Axboe5921e802008-05-30 15:02:38 +0200413 flags |= FIO_O_NOATIME;
Jens Axboe814452b2009-03-04 12:53:13 +0100414 if (td->o.create_on_open)
415 flags |= O_CREAT;
Jens Axboe7abf8332006-11-23 15:01:19 +0100416
Aaron Carroll056f3452007-11-26 09:08:53 +0100417open_again:
Jens Axboe660a1cb2007-04-17 15:37:47 +0200418 if (td_write(td)) {
Jens Axboe17308152008-03-07 13:42:31 +0100419 if (!read_only)
420 flags |= O_RDWR;
Jens Axboe53cdc682006-10-18 11:50:58 +0200421
Jens Axboeaf52b342007-03-13 10:07:47 +0100422 if (f->filetype == FIO_TYPE_FILE)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100423 flags |= O_CREAT;
Jens Axboe2fd233b2007-03-02 08:44:25 +0100424
Jens Axboe66159822007-04-16 21:54:24 +0200425 if (is_std)
426 f->fd = dup(STDOUT_FILENO);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100427 else
428 from_hash = file_lookup_open(f, flags);
Jens Axboe2fd233b2007-03-02 08:44:25 +0100429 } else {
Jens Axboe4241ea82007-09-12 08:18:36 +0200430 if (f->filetype == FIO_TYPE_CHAR && !read_only)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100431 flags |= O_RDWR;
432 else
433 flags |= O_RDONLY;
434
Jens Axboe66159822007-04-16 21:54:24 +0200435 if (is_std)
436 f->fd = dup(STDIN_FILENO);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100437 else
438 from_hash = file_lookup_open(f, flags);
Jens Axboe53cdc682006-10-18 11:50:58 +0200439 }
440
441 if (f->fd == -1) {
Jens Axboee4e33252007-03-21 13:07:54 +0100442 char buf[FIO_VERROR_SIZE];
Jens Axboee1161c32007-02-22 19:36:48 +0100443 int __e = errno;
444
Jens Axboe835d9b92009-06-09 15:23:38 +0200445 if (__e == EPERM && (flags & FIO_O_NOATIME)) {
Jens Axboe5921e802008-05-30 15:02:38 +0200446 flags &= ~FIO_O_NOATIME;
Aaron Carroll056f3452007-11-26 09:08:53 +0100447 goto open_again;
448 }
449
Jens Axboee8670ef2008-03-06 12:06:30 +0100450 snprintf(buf, sizeof(buf) - 1, "open(%s)", f->file_name);
Jens Axboee4e33252007-03-21 13:07:54 +0100451
452 td_verror(td, __e, buf);
Jens Axboe53cdc682006-10-18 11:50:58 +0200453 }
454
Jens Axboe29c13492008-03-01 19:25:20 +0100455 if (!from_hash && f->fd != -1) {
456 if (add_file_hash(f)) {
457 int ret;
458
459 /*
460 * OK to ignore, we haven't done anything with it
461 */
462 ret = generic_close_file(td, f);
463 goto open_again;
464 }
465 }
Jens Axboe4906e0b2008-03-01 18:59:51 +0100466
Jens Axboe53cdc682006-10-18 11:50:58 +0200467 return 0;
Jens Axboeb5af8292007-03-08 12:43:13 +0100468}
469
Jens Axboedf9c26b2009-03-05 10:13:58 +0100470int generic_get_file_size(struct thread_data *td, struct fio_file *f)
Jens Axboe21972cd2006-11-23 12:39:16 +0100471{
Jens Axboedf9c26b2009-03-05 10:13:58 +0100472 return get_file_size(td, f);
Jens Axboe21972cd2006-11-23 12:39:16 +0100473}
474
Jens Axboe7bb48f82007-03-27 15:30:28 +0200475/*
476 * open/close all files, so that ->real_file_size gets set
477 */
Jens Axboebab3fd52007-04-02 10:34:18 +0200478static int get_file_sizes(struct thread_data *td)
Jens Axboe53cdc682006-10-18 11:50:58 +0200479{
480 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100481 unsigned int i;
Jens Axboebab3fd52007-04-02 10:34:18 +0200482 int err = 0;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200483
484 for_each_file(td, f, i) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100485 dprint(FD_FILE, "get file size for %p/%d/%p\n", f, i,
486 f->file_name);
Jens Axboe9efef3c2008-03-06 10:26:25 +0100487
Jens Axboe99a47c62009-04-07 22:20:56 +0200488 if (td_io_get_file_size(td, f)) {
Jens Axboe40b44f42007-04-11 12:43:35 +0200489 if (td->error != ENOENT) {
490 log_err("%s\n", td->verror);
491 err = 1;
492 }
Jens Axboe541d66d2007-03-27 19:50:11 +0200493 clear_error(td);
Jens Axboe07eb79d2007-04-12 13:02:38 +0200494 }
Jens Axboe409b3412007-03-28 09:57:01 +0200495
496 if (f->real_file_size == -1ULL && td->o.size)
497 f->real_file_size = td->o.size / td->o.nr_files;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200498 }
Jens Axboebab3fd52007-04-02 10:34:18 +0200499
500 return err;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200501}
502
503/*
504 * Open the files and setup files sizes, creating files if necessary.
505 */
506int setup_files(struct thread_data *td)
507{
508 unsigned long long total_size, extend_size;
509 struct fio_file *f;
510 unsigned int i;
Jens Axboe000b0802007-03-27 15:56:10 +0200511 int err = 0, need_extend;
Jens Axboe53cdc682006-10-18 11:50:58 +0200512
Jens Axboeee56ad52008-02-01 10:30:20 +0100513 dprint(FD_FILE, "setup files\n");
514
Jens Axboe691c8fb2008-03-07 14:26:26 +0100515 if (td->o.read_iolog_file)
516 return 0;
517
Jens Axboe53cdc682006-10-18 11:50:58 +0200518 /*
519 * if ioengine defines a setup() method, it's responsible for
Jens Axboe7bb48f82007-03-27 15:30:28 +0200520 * opening the files and setting f->real_file_size to indicate
521 * the valid range for that file.
Jens Axboe53cdc682006-10-18 11:50:58 +0200522 */
523 if (td->io_ops->setup)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200524 err = td->io_ops->setup(td);
525 else
Jens Axboebab3fd52007-04-02 10:34:18 +0200526 err = get_file_sizes(td);
Jens Axboe53cdc682006-10-18 11:50:58 +0200527
Jens Axboef1027062006-10-20 10:14:01 +0200528 if (err)
529 return err;
530
Jens Axboe0a7eb122006-10-20 10:36:42 +0200531 /*
Jens Axboe7bb48f82007-03-27 15:30:28 +0200532 * check sizes. if the files/devices do not exist and the size
533 * isn't passed to fio, abort.
Jens Axboe0a7eb122006-10-20 10:36:42 +0200534 */
Jens Axboe7bb48f82007-03-27 15:30:28 +0200535 total_size = 0;
536 for_each_file(td, f, i) {
537 if (f->real_file_size == -1ULL)
538 total_size = -1ULL;
539 else
540 total_size += f->real_file_size;
541 }
Jens Axboe0a7eb122006-10-20 10:36:42 +0200542
Jens Axboe7bb48f82007-03-27 15:30:28 +0200543 /*
544 * device/file sizes are zero and no size given, punt
545 */
Jens Axboe1f809d12007-10-25 18:34:02 +0200546 if ((!total_size || total_size == -1ULL) && !td->o.size &&
Shawn Lewisaa31f1f2008-01-11 09:45:11 +0100547 !(td->io_ops->flags & FIO_NOIO) && !td->o.fill_device) {
Jens Axboe7bb48f82007-03-27 15:30:28 +0200548 log_err("%s: you need to specify size=\n", td->o.name);
Jens Axboee1161c32007-02-22 19:36:48 +0100549 td_verror(td, EINVAL, "total_file_size");
Jens Axboe53cdc682006-10-18 11:50:58 +0200550 return 1;
551 }
552
Jens Axboe7bb48f82007-03-27 15:30:28 +0200553 /*
554 * now file sizes are known, so we can set ->io_size. if size= is
555 * not given, ->io_size is just equal to ->real_file_size. if size
556 * is given, ->io_size is size / nr_files.
557 */
558 extend_size = total_size = 0;
559 need_extend = 0;
560 for_each_file(td, f, i) {
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200561 f->file_offset = td->o.start_offset;
562
Jens Axboe7bb48f82007-03-27 15:30:28 +0200563 if (!td->o.file_size_low) {
564 /*
565 * no file size range given, file size is equal to
566 * total size divided by number of files. if that is
567 * zero, set it to the real file size.
568 */
569 f->io_size = td->o.size / td->o.nr_files;
Jens Axboe65bdb102008-01-24 13:13:12 +0100570 if (!f->io_size)
Jens Axboe273f8c92008-01-25 14:02:15 +0100571 f->io_size = f->real_file_size - f->file_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200572 } else if (f->real_file_size < td->o.file_size_low ||
573 f->real_file_size > td->o.file_size_high) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100574 if (f->file_offset > td->o.file_size_low)
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200575 goto err_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200576 /*
577 * file size given. if it's fixed, use that. if it's a
578 * range, generate a random size in-between.
579 */
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100580 if (td->o.file_size_low == td->o.file_size_high) {
581 f->io_size = td->o.file_size_low
582 - f->file_offset;
583 } else {
584 f->io_size = get_rand_file_size(td)
585 - f->file_offset;
586 }
Jens Axboe65bdb102008-01-24 13:13:12 +0100587 } else
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200588 f->io_size = f->real_file_size - f->file_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200589
590 if (f->io_size == -1ULL)
591 total_size = -1ULL;
592 else
593 total_size += f->io_size;
594
595 if (f->filetype == FIO_TYPE_FILE &&
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200596 (f->io_size + f->file_offset) > f->real_file_size &&
Jens Axboe7bb48f82007-03-27 15:30:28 +0200597 !(td->io_ops->flags & FIO_DISKLESSIO)) {
Jens Axboe814452b2009-03-04 12:53:13 +0100598 if (!td->o.create_on_open) {
599 need_extend++;
600 extend_size += (f->io_size + f->file_offset);
601 } else
602 f->real_file_size = f->io_size + f->file_offset;
Jens Axboed6aed792009-06-03 08:41:15 +0200603 fio_file_set_extend(f);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100604 }
Jens Axboe7bb48f82007-03-27 15:30:28 +0200605 }
606
ljzhang,Yaxin Hu,Jianchao Tang22982902007-07-27 13:21:28 +0200607 if (!td->o.size || td->o.size > total_size)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200608 td->o.size = total_size;
609
610 /*
611 * See if we need to extend some files
612 */
613 if (need_extend) {
614 temp_stall_ts = 1;
Shaozhi Shawn Yea7ba8c52008-09-10 09:04:18 +0200615 if (!terse_output)
616 log_info("%s: Laying out IO file(s) (%u file(s) /"
Jens Axboeb22989b2009-07-17 22:29:23 +0200617 " %LuMB)\n", td->o.name, need_extend,
Shaozhi Shawn Yea7ba8c52008-09-10 09:04:18 +0200618 extend_size >> 20);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200619
620 for_each_file(td, f, i) {
Jens Axboe5e0074c2009-06-02 21:40:09 +0200621 unsigned long long old_len = -1ULL, extend_len = -1ULL;
Jens Axboe3baddf22008-04-04 11:10:30 +0200622
Jens Axboed6aed792009-06-03 08:41:15 +0200623 if (!fio_file_extend(f))
Jens Axboe7bb48f82007-03-27 15:30:28 +0200624 continue;
625
Jens Axboe409b3412007-03-28 09:57:01 +0200626 assert(f->filetype == FIO_TYPE_FILE);
Jens Axboed6aed792009-06-03 08:41:15 +0200627 fio_file_clear_extend(f);
Jens Axboe5e0074c2009-06-02 21:40:09 +0200628 if (!td->o.fill_device) {
629 old_len = f->real_file_size;
Jens Axboe0b9d69e2009-09-11 22:29:54 +0200630 extend_len = f->io_size + f->file_offset -
631 old_len;
Jens Axboe5e0074c2009-06-02 21:40:09 +0200632 }
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200633 f->real_file_size = (f->io_size + f->file_offset);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200634 err = extend_file(td, f);
635 if (err)
636 break;
Jens Axboe5e0074c2009-06-02 21:40:09 +0200637
Jens Axboe3baddf22008-04-04 11:10:30 +0200638 err = __file_invalidate_cache(td, f, old_len,
639 extend_len);
640 close(f->fd);
641 f->fd = -1;
642 if (err)
643 break;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200644 }
645 temp_stall_ts = 0;
646 }
647
648 if (err)
649 return err;
650
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100651 if (!td->o.zone_size)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200652 td->o.zone_size = td->o.size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200653
Jens Axboeea966f82007-09-03 10:09:37 +0200654 /*
655 * iolog already set the total io size, if we read back
656 * stored entries.
657 */
658 if (!td->o.read_iolog_file)
659 td->total_io_size = td->o.size * td->o.loops;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200660 return 0;
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200661err_offset:
662 log_err("%s: you need to specify valid offset=\n", td->o.name);
663 return 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200664}
665
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200666int pre_read_files(struct thread_data *td)
667{
668 struct fio_file *f;
669 unsigned int i;
670
671 dprint(FD_FILE, "pre_read files\n");
672
673 for_each_file(td, f, i) {
674 pre_read_file(td, f);
675 }
676
677 return 1;
678}
679
Jens Axboe68727072007-03-15 20:49:25 +0100680int init_random_map(struct thread_data *td)
681{
Jens Axboe509eab12007-12-14 12:42:53 +0100682 unsigned long long blocks, num_maps;
Jens Axboe68727072007-03-15 20:49:25 +0100683 struct fio_file *f;
684 unsigned int i;
685
Jens Axboede8dd112008-03-01 15:34:44 +0100686 if (td->o.norandommap || !td_random(td))
Jens Axboe68727072007-03-15 20:49:25 +0100687 return 0;
688
689 for_each_file(td, f, i) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100690 blocks = (f->real_file_size + td->o.rw_min_bs - 1) /
691 (unsigned long long) td->o.rw_min_bs;
692 num_maps = (blocks + BLOCKS_PER_MAP - 1) /
693 (unsigned long long) BLOCKS_PER_MAP;
Jens Axboede605662008-05-31 00:21:12 +0200694 f->file_map = smalloc(num_maps * sizeof(int));
Jens Axboe303032a2008-03-26 10:11:10 +0100695 if (f->file_map) {
696 f->num_maps = num_maps;
697 continue;
Jens Axboe68727072007-03-15 20:49:25 +0100698 }
Jens Axboe2b386d22008-03-26 10:32:57 +0100699 if (!td->o.softrandommap) {
Jens Axboe68727072007-03-15 20:49:25 +0100700 log_err("fio: failed allocating random map. If running"
701 " a large number of jobs, try the 'norandommap'"
Jens Axboe2b386d22008-03-26 10:32:57 +0100702 " option or set 'softrandommap'. Or give"
703 " a larger --alloc-size to fio.\n");
Jens Axboe68727072007-03-15 20:49:25 +0100704 return 1;
705 }
Jens Axboe303032a2008-03-26 10:11:10 +0100706
707 log_info("fio: file %s failed allocating random map. Running "
708 "job without.\n", f->file_name);
709 f->num_maps = 0;
Jens Axboe68727072007-03-15 20:49:25 +0100710 }
711
712 return 0;
713}
714
Jens Axboe53cdc682006-10-18 11:50:58 +0200715void close_files(struct thread_data *td)
716{
Jens Axboe0ab8db82006-10-18 17:16:23 +0200717 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100718 unsigned int i;
Jens Axboe53cdc682006-10-18 11:50:58 +0200719
Jens Axboe2be3eec2009-06-10 09:05:13 +0200720 for_each_file(td, f, i) {
721 if (fio_file_open(f))
722 td_io_close_file(td, f);
723 }
Jens Axboe24ffd2c2008-03-01 15:47:08 +0100724}
725
726void close_and_free_files(struct thread_data *td)
727{
728 struct fio_file *f;
729 unsigned int i;
730
Jens Axboeee56ad52008-02-01 10:30:20 +0100731 dprint(FD_FILE, "close files\n");
732
Jens Axboe0ab8db82006-10-18 17:16:23 +0200733 for_each_file(td, f, i) {
Jens Axboeffdfabd2008-03-26 09:18:14 +0100734 if (td->o.unlink && f->filetype == FIO_TYPE_FILE) {
735 dprint(FD_FILE, "free unlink %s\n", f->file_name);
Jens Axboe132ad462006-11-23 12:23:12 +0100736 unlink(f->file_name);
Jens Axboeffdfabd2008-03-26 09:18:14 +0100737 }
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100738
Jens Axboe22a57ba2009-06-09 14:34:35 +0200739 if (fio_file_open(f))
740 td_io_close_file(td, f);
741
Shaozhi Shawn Yeb9fbcf22008-09-10 09:05:17 +0200742 remove_file_hash(f);
Jens Axboeb3dc7f02007-03-09 12:52:15 +0100743
Jens Axboef17c4392008-03-01 18:40:46 +0100744 sfree(f->file_name);
Jens Axboefa1da862007-03-23 15:20:54 +0100745 f->file_name = NULL;
746
Jens Axboec3439812007-03-20 13:54:53 +0100747 if (f->file_map) {
Jens Axboef17c4392008-03-01 18:40:46 +0100748 sfree(f->file_map);
Jens Axboec3439812007-03-20 13:54:53 +0100749 f->file_map = NULL;
750 }
Jens Axboe78d99e62008-03-01 18:41:50 +0100751 sfree(f);
Jens Axboe53cdc682006-10-18 11:50:58 +0200752 }
Jens Axboeb4a6a592006-10-20 13:54:47 +0200753
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100754 td->o.filename = NULL;
Jens Axboecade3ef2007-03-23 15:29:45 +0100755 free(td->files);
Jens Axboe9efef3c2008-03-06 10:26:25 +0100756 td->files_index = 0;
Jens Axboeb4a6a592006-10-20 13:54:47 +0200757 td->files = NULL;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100758 td->o.nr_files = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200759}
Jens Axboeaf52b342007-03-13 10:07:47 +0100760
Jens Axboee3bab462007-03-13 11:22:09 +0100761static void get_file_type(struct fio_file *f)
Jens Axboeaf52b342007-03-13 10:07:47 +0100762{
763 struct stat sb;
764
Jens Axboe66159822007-04-16 21:54:24 +0200765 if (!strcmp(f->file_name, "-"))
766 f->filetype = FIO_TYPE_PIPE;
767 else
768 f->filetype = FIO_TYPE_FILE;
Jens Axboeaf52b342007-03-13 10:07:47 +0100769
Jens Axboee3bab462007-03-13 11:22:09 +0100770 if (!lstat(f->file_name, &sb)) {
Jens Axboeaf52b342007-03-13 10:07:47 +0100771 if (S_ISBLK(sb.st_mode))
772 f->filetype = FIO_TYPE_BD;
773 else if (S_ISCHR(sb.st_mode))
774 f->filetype = FIO_TYPE_CHAR;
Jens Axboeb5605e92007-04-16 20:42:33 +0200775 else if (S_ISFIFO(sb.st_mode))
776 f->filetype = FIO_TYPE_PIPE;
Jens Axboeaf52b342007-03-13 10:07:47 +0100777 }
778}
779
Jens Axboef29b25a2007-07-23 08:56:43 +0200780int add_file(struct thread_data *td, const char *fname)
Jens Axboeaf52b342007-03-13 10:07:47 +0100781{
Jens Axboe7b4e4fe2007-03-13 12:51:40 +0100782 int cur_files = td->files_index;
Jens Axboebd0ee742007-03-23 14:26:23 +0100783 char file_name[PATH_MAX];
Jens Axboeaf52b342007-03-13 10:07:47 +0100784 struct fio_file *f;
Jens Axboebd0ee742007-03-23 14:26:23 +0100785 int len = 0;
Jens Axboeaf52b342007-03-13 10:07:47 +0100786
Jens Axboeee56ad52008-02-01 10:30:20 +0100787 dprint(FD_FILE, "add file %s\n", fname);
788
Jens Axboef17c4392008-03-01 18:40:46 +0100789 f = smalloc(sizeof(*f));
Jens Axboec48c0be2008-07-31 19:55:02 +0200790 if (!f) {
791 log_err("fio: smalloc OOM\n");
792 assert(0);
793 }
Jens Axboe0b9d69e2009-09-11 22:29:54 +0200794
Jens Axboeaf52b342007-03-13 10:07:47 +0100795 f->fd = -1;
Jens Axboebd0ee742007-03-23 14:26:23 +0100796
Jens Axboefc99bc02009-03-04 15:27:42 +0100797 if (td->files_size <= td->files_index) {
Carl Henrik Lunde4b341fc2009-04-20 08:41:37 +0200798 int new_size = td->o.nr_files + 1;
Jens Axboe126d65c2008-03-01 18:04:31 +0100799
Jens Axboefc99bc02009-03-04 15:27:42 +0100800 dprint(FD_FILE, "resize file array to %d files\n", new_size);
801
802 td->files = realloc(td->files, new_size * sizeof(f));
Jens Axboefc99bc02009-03-04 15:27:42 +0100803 td->files_size = new_size;
804 }
Jens Axboe8bb76792009-03-04 15:37:52 +0100805 td->files[cur_files] = f;
Jens Axboe126d65c2008-03-01 18:04:31 +0100806
Jens Axboe07eb79d2007-04-12 13:02:38 +0200807 /*
808 * init function, io engine may not be loaded yet
809 */
810 if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO))
811 f->real_file_size = -1ULL;
812
Jens Axboebd0ee742007-03-23 14:26:23 +0100813 if (td->o.directory)
814 len = sprintf(file_name, "%s/", td->o.directory);
815
816 sprintf(file_name + len, "%s", fname);
Jens Axboef17c4392008-03-01 18:40:46 +0100817 f->file_name = smalloc_strdup(file_name);
Jens Axboec48c0be2008-07-31 19:55:02 +0200818 if (!f->file_name) {
819 log_err("fio: smalloc OOM\n");
820 assert(0);
821 }
Jens Axboe0b9d69e2009-09-11 22:29:54 +0200822
Jens Axboee3bab462007-03-13 11:22:09 +0100823 get_file_type(f);
Jens Axboeaf52b342007-03-13 10:07:47 +0100824
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100825 switch (td->o.file_lock_mode) {
826 case FILE_LOCK_NONE:
827 break;
828 case FILE_LOCK_READWRITE:
829 f->lock = fio_mutex_rw_init();
830 break;
831 case FILE_LOCK_EXCLUSIVE:
832 f->lock = fio_mutex_init(1);
833 break;
834 default:
835 log_err("fio: unknown lock mode: %d\n", td->o.file_lock_mode);
836 assert(0);
837 }
Jens Axboe29c13492008-03-01 19:25:20 +0100838
Jens Axboe7b4e4fe2007-03-13 12:51:40 +0100839 td->files_index++;
Jens Axboe15494412007-03-13 11:52:47 +0100840 if (f->filetype == FIO_TYPE_FILE)
841 td->nr_normal_files++;
Jens Axboef29b25a2007-07-23 08:56:43 +0200842
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100843 dprint(FD_FILE, "file %p \"%s\" added at %d\n", f, f->file_name,
844 cur_files);
Jens Axboe9efef3c2008-03-06 10:26:25 +0100845
Jens Axboef29b25a2007-07-23 08:56:43 +0200846 return cur_files;
Jens Axboeaf52b342007-03-13 10:07:47 +0100847}
Jens Axboe0ad920e2007-03-13 11:06:45 +0100848
849void get_file(struct fio_file *f)
850{
Jens Axboe8172fe92008-02-01 21:51:02 +0100851 dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references);
Jens Axboed6aed792009-06-03 08:41:15 +0200852 assert(fio_file_open(f));
Jens Axboe0ad920e2007-03-13 11:06:45 +0100853 f->references++;
854}
855
Jens Axboe6977bcd2008-03-01 15:55:36 +0100856int put_file(struct thread_data *td, struct fio_file *f)
Jens Axboe0ad920e2007-03-13 11:06:45 +0100857{
Jens Axboe98e1ac42008-03-06 11:56:48 +0100858 int f_ret = 0, ret = 0;
Jens Axboe6977bcd2008-03-01 15:55:36 +0100859
Jens Axboe8172fe92008-02-01 21:51:02 +0100860 dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references);
Jens Axboeee56ad52008-02-01 10:30:20 +0100861
Jens Axboe22a57ba2009-06-09 14:34:35 +0200862 if (!fio_file_open(f)) {
863 assert(f->fd == -1);
Jens Axboe6977bcd2008-03-01 15:55:36 +0100864 return 0;
Jens Axboe22a57ba2009-06-09 14:34:35 +0200865 }
Jens Axboe0ad920e2007-03-13 11:06:45 +0100866
867 assert(f->references);
868 if (--f->references)
Jens Axboe6977bcd2008-03-01 15:55:36 +0100869 return 0;
Jens Axboe0ad920e2007-03-13 11:06:45 +0100870
Jens Axboed424d4d2007-04-17 09:05:10 +0200871 if (should_fsync(td) && td->o.fsync_on_close)
Jens Axboe98e1ac42008-03-06 11:56:48 +0100872 f_ret = fsync(f->fd);
Jens Axboeebb14152007-03-13 14:42:15 +0100873
Jens Axboe0ad920e2007-03-13 11:06:45 +0100874 if (td->io_ops->close_file)
Jens Axboe6977bcd2008-03-01 15:55:36 +0100875 ret = td->io_ops->close_file(td, f);
Jens Axboe1020a132007-03-29 11:15:06 +0200876
Jens Axboe98e1ac42008-03-06 11:56:48 +0100877 if (!ret)
Jens Axboea5fb4612008-05-28 10:54:01 +0200878 ret = f_ret;
Jens Axboe98e1ac42008-03-06 11:56:48 +0100879
Jens Axboe0ad920e2007-03-13 11:06:45 +0100880 td->nr_open_files--;
Jens Axboed6aed792009-06-03 08:41:15 +0200881 fio_file_clear_open(f);
Jens Axboe22a57ba2009-06-09 14:34:35 +0200882 assert(f->fd == -1);
Jens Axboe6977bcd2008-03-01 15:55:36 +0100883 return ret;
Jens Axboe0ad920e2007-03-13 11:06:45 +0100884}
Jens Axboebbf6b542007-03-13 15:28:55 +0100885
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100886void lock_file(struct thread_data *td, struct fio_file *f, enum fio_ddir ddir)
Jens Axboeb2bd2bd2008-03-01 18:19:52 +0100887{
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100888 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
889 return;
Jens Axboe29c13492008-03-01 19:25:20 +0100890
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100891 if (f->lock_owner == td && f->lock_batch--)
892 return;
893
894 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
895 if (ddir == DDIR_READ)
896 fio_mutex_down_read(f->lock);
897 else
898 fio_mutex_down_write(f->lock);
899 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
900 fio_mutex_down(f->lock);
901
902 f->lock_owner = td;
903 f->lock_batch = td->o.lockfile_batch;
904 f->lock_ddir = ddir;
905}
906
907void unlock_file(struct thread_data *td, struct fio_file *f)
908{
909 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
910 return;
911 if (f->lock_batch)
912 return;
913
914 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
915 const int is_read = f->lock_ddir == DDIR_READ;
916 int val = fio_mutex_getval(f->lock);
917
918 if ((is_read && val == 1) || (!is_read && val == -1))
919 f->lock_owner = NULL;
920
921 if (is_read)
922 fio_mutex_up_read(f->lock);
923 else
924 fio_mutex_up_write(f->lock);
925 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE) {
926 int val = fio_mutex_getval(f->lock);
927
928 if (val == 0)
929 f->lock_owner = NULL;
930
931 fio_mutex_up(f->lock);
Jens Axboe29c13492008-03-01 19:25:20 +0100932 }
Jens Axboeb2bd2bd2008-03-01 18:19:52 +0100933}
934
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100935void unlock_file_all(struct thread_data *td, struct fio_file *f)
Jens Axboeb2bd2bd2008-03-01 18:19:52 +0100936{
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100937 if (f->lock_owner != td)
938 return;
Jens Axboe29c13492008-03-01 19:25:20 +0100939
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100940 f->lock_batch = 0;
941 unlock_file(td, f);
Jens Axboeb2bd2bd2008-03-01 18:19:52 +0100942}
943
Jens Axboebbf6b542007-03-13 15:28:55 +0100944static int recurse_dir(struct thread_data *td, const char *dirname)
945{
946 struct dirent *dir;
947 int ret = 0;
948 DIR *D;
949
950 D = opendir(dirname);
951 if (!D) {
Jens Axboe0ddb2702007-03-27 19:43:53 +0200952 char buf[FIO_VERROR_SIZE];
953
954 snprintf(buf, FIO_VERROR_SIZE - 1, "opendir(%s)", dirname);
955 td_verror(td, errno, buf);
Jens Axboebbf6b542007-03-13 15:28:55 +0100956 return 1;
957 }
958
959 while ((dir = readdir(D)) != NULL) {
960 char full_path[PATH_MAX];
961 struct stat sb;
962
Jens Axboee85b2b82007-03-14 09:39:06 +0100963 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
964 continue;
Jens Axboe96d32d52007-03-14 09:16:09 +0100965
Jens Axboebbf6b542007-03-13 15:28:55 +0100966 sprintf(full_path, "%s/%s", dirname, dir->d_name);
967
968 if (lstat(full_path, &sb) == -1) {
969 if (errno != ENOENT) {
970 td_verror(td, errno, "stat");
971 return 1;
972 }
973 }
974
975 if (S_ISREG(sb.st_mode)) {
976 add_file(td, full_path);
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100977 td->o.nr_files++;
Jens Axboebbf6b542007-03-13 15:28:55 +0100978 continue;
979 }
Jens Axboe0ddb2702007-03-27 19:43:53 +0200980 if (!S_ISDIR(sb.st_mode))
981 continue;
Jens Axboebbf6b542007-03-13 15:28:55 +0100982
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100983 ret = recurse_dir(td, full_path);
984 if (ret)
Jens Axboebbf6b542007-03-13 15:28:55 +0100985 break;
986 }
987
988 closedir(D);
989 return ret;
990}
991
992int add_dir_files(struct thread_data *td, const char *path)
993{
Jens Axboe0ddb2702007-03-27 19:43:53 +0200994 int ret = recurse_dir(td, path);
995
996 if (!ret)
997 log_info("fio: opendir added %d files\n", td->o.nr_files);
998
999 return ret;
Jens Axboebbf6b542007-03-13 15:28:55 +01001000}
Jens Axboecade3ef2007-03-23 15:29:45 +01001001
1002void dup_files(struct thread_data *td, struct thread_data *org)
1003{
1004 struct fio_file *f;
1005 unsigned int i;
Jens Axboe9efef3c2008-03-06 10:26:25 +01001006
1007 dprint(FD_FILE, "dup files: %d\n", org->files_index);
Jens Axboecade3ef2007-03-23 15:29:45 +01001008
1009 if (!org->files)
1010 return;
1011
Jens Axboe9efef3c2008-03-06 10:26:25 +01001012 td->files = malloc(org->files_index * sizeof(f));
Jens Axboecade3ef2007-03-23 15:29:45 +01001013
Jens Axboe9efef3c2008-03-06 10:26:25 +01001014 for_each_file(org, f, i) {
Jens Axboeb0fe4212008-03-01 18:22:27 +01001015 struct fio_file *__f;
1016
Jens Axboef17c4392008-03-01 18:40:46 +01001017 __f = smalloc(sizeof(*__f));
Jens Axboec48c0be2008-07-31 19:55:02 +02001018 if (!__f) {
1019 log_err("fio: smalloc OOM\n");
1020 assert(0);
1021 }
Jens Axboe22a57ba2009-06-09 14:34:35 +02001022 __f->fd = -1;
Jens Axboe0b9d69e2009-09-11 22:29:54 +02001023
Aaron Carrollbc3456f2008-06-25 09:20:37 +02001024 if (f->file_name) {
Jens Axboef17c4392008-03-01 18:40:46 +01001025 __f->file_name = smalloc_strdup(f->file_name);
Jens Axboec48c0be2008-07-31 19:55:02 +02001026 if (!__f->file_name) {
1027 log_err("fio: smalloc OOM\n");
1028 assert(0);
1029 }
Jens Axboe0b9d69e2009-09-11 22:29:54 +02001030
Aaron Carrollbc3456f2008-06-25 09:20:37 +02001031 __f->filetype = f->filetype;
1032 }
Jens Axboeb0fe4212008-03-01 18:22:27 +01001033
1034 td->files[i] = __f;
Jens Axboecade3ef2007-03-23 15:29:45 +01001035 }
1036}
Jens Axboef29b25a2007-07-23 08:56:43 +02001037
1038/*
1039 * Returns the index that matches the filename, or -1 if not there
1040 */
1041int get_fileno(struct thread_data *td, const char *fname)
1042{
1043 struct fio_file *f;
1044 unsigned int i;
1045
1046 for_each_file(td, f, i)
1047 if (!strcmp(f->file_name, fname))
1048 return i;
1049
1050 return -1;
1051}
1052
1053/*
1054 * For log usage, where we add/open/close files automatically
1055 */
1056void free_release_files(struct thread_data *td)
1057{
1058 close_files(td);
1059 td->files_index = 0;
1060 td->nr_normal_files = 0;
1061}