blob: a0a4b80d33483d70c00db8fe56882ce073136286 [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 Axboed6aed792009-06-03 08:41:15 +0200159 if (!fio_file_open(f)) {
Jens Axboeb0f65862009-05-20 11:52:15 +0200160 if (td->io_ops->open_file(td, f)) {
161 log_err("fio: cannot pre-read, failed to open file\n");
162 return 1;
163 }
164 did_open = 1;
165 }
166
167 old_runstate = td->runstate;
168 td_set_runstate(td, TD_PRE_READING);
169
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200170 bs = td->o.max_bs[DDIR_READ];
171 b = malloc(bs);
172 memset(b, 0, bs);
173
174 lseek(f->fd, f->file_offset, SEEK_SET);
175 left = f->io_size;
176
177 while (left && !td->terminate) {
178 if (bs > left)
179 bs = left;
180
181 r = read(f->fd, b, bs);
182
183 if (r == (int) bs) {
184 left -= bs;
185 continue;
186 } else {
187 td_verror(td, EIO, "pre_read");
188 break;
189 }
190 }
191
Jens Axboeb0f65862009-05-20 11:52:15 +0200192 td_set_runstate(td, old_runstate);
193
194 if (did_open)
195 td->io_ops->close_file(td, f);
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200196 free(b);
197 return 0;
198}
199
Jens Axboe7bb48f82007-03-27 15:30:28 +0200200static unsigned long long get_rand_file_size(struct thread_data *td)
Jens Axboe9c60ce62007-03-15 09:14:47 +0100201{
Jens Axboedc873b62008-06-04 20:13:04 +0200202 unsigned long long ret, sized;
Jens Axboe9c60ce62007-03-15 09:14:47 +0100203 long r;
204
Jens Axboe9c60ce62007-03-15 09:14:47 +0100205 r = os_random_long(&td->file_size_state);
Jens Axboedc873b62008-06-04 20:13:04 +0200206 sized = td->o.file_size_high - td->o.file_size_low;
207 ret = (unsigned long long) ((double) sized * (r / (OS_RAND_MAX + 1.0)));
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100208 ret += td->o.file_size_low;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100209 ret -= (ret % td->o.rw_min_bs);
Jens Axboe9c60ce62007-03-15 09:14:47 +0100210 return ret;
211}
212
Jens Axboe53cdc682006-10-18 11:50:58 +0200213static int file_size(struct thread_data *td, struct fio_file *f)
214{
215 struct stat st;
216
Jens Axboedf9c26b2009-03-05 10:13:58 +0100217 if (stat(f->file_name, &st) == -1) {
Jens Axboe7bb48f82007-03-27 15:30:28 +0200218 td_verror(td, errno, "fstat");
219 return 1;
220 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200221
Jens Axboe7bb48f82007-03-27 15:30:28 +0200222 f->real_file_size = st.st_size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200223 return 0;
224}
225
226static int bdev_size(struct thread_data *td, struct fio_file *f)
227{
228 unsigned long long bytes;
229 int r;
230
Jens Axboedf9c26b2009-03-05 10:13:58 +0100231 if (td->io_ops->open_file(td, f)) {
232 log_err("fio: failed opening blockdev %s for size check\n",
233 f->file_name);
234 return 1;
235 }
236
Jens Axboe53cdc682006-10-18 11:50:58 +0200237 r = blockdev_size(f->fd, &bytes);
238 if (r) {
Jens Axboee1161c32007-02-22 19:36:48 +0100239 td_verror(td, r, "blockdev_size");
Jens Axboedf9c26b2009-03-05 10:13:58 +0100240 goto err;
Jens Axboe53cdc682006-10-18 11:50:58 +0200241 }
242
Jens Axboe7ab077a2009-02-02 15:07:37 +0100243 if (!bytes) {
244 log_err("%s: zero sized block device?\n", f->file_name);
Jens Axboedf9c26b2009-03-05 10:13:58 +0100245 goto err;
Jens Axboe7ab077a2009-02-02 15:07:37 +0100246 }
247
Jens Axboe53cdc682006-10-18 11:50:58 +0200248 f->real_file_size = bytes;
Jens Axboe22a57ba2009-06-09 14:34:35 +0200249 td->io_ops->close_file(td, f);
Jens Axboe53cdc682006-10-18 11:50:58 +0200250 return 0;
Jens Axboedf9c26b2009-03-05 10:13:58 +0100251err:
252 td->io_ops->close_file(td, f);
253 return 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200254}
255
256static int get_file_size(struct thread_data *td, struct fio_file *f)
257{
258 int ret = 0;
259
Jens Axboed6aed792009-06-03 08:41:15 +0200260 if (fio_file_size_known(f))
Jens Axboe409b3412007-03-28 09:57:01 +0200261 return 0;
262
Jens Axboe7bb48f82007-03-27 15:30:28 +0200263 if (f->filetype == FIO_TYPE_FILE)
264 ret = file_size(td, f);
265 else if (f->filetype == FIO_TYPE_BD)
Jens Axboe53cdc682006-10-18 11:50:58 +0200266 ret = bdev_size(td, f);
267 else
268 f->real_file_size = -1;
269
270 if (ret)
271 return ret;
272
273 if (f->file_offset > f->real_file_size) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100274 log_err("%s: offset extends end (%Lu > %Lu)\n", td->o.name,
275 f->file_offset, f->real_file_size);
Jens Axboe53cdc682006-10-18 11:50:58 +0200276 return 1;
277 }
278
Jens Axboed6aed792009-06-03 08:41:15 +0200279 fio_file_set_size_known(f);
Jens Axboe53cdc682006-10-18 11:50:58 +0200280 return 0;
281}
282
Jens Axboe3baddf22008-04-04 11:10:30 +0200283static int __file_invalidate_cache(struct thread_data *td, struct fio_file *f,
284 unsigned long long off,
285 unsigned long long len)
Jens Axboee5b401d2006-10-18 16:03:40 +0200286{
287 int ret = 0;
288
Jens Axboe5e0074c2009-06-02 21:40:09 +0200289 if (len == -1ULL)
Jens Axboe3baddf22008-04-04 11:10:30 +0200290 len = f->io_size;
291 if (off == -1ULL)
292 off = f->file_offset;
Jens Axboeee56ad52008-02-01 10:30:20 +0100293
Jens Axboe0d1cd202009-06-05 22:11:52 +0200294 if (len == -1ULL || off == -1ULL)
295 return 0;
296
Jens Axboe3baddf22008-04-04 11:10:30 +0200297 dprint(FD_IO, "invalidate cache %s: %llu/%llu\n", f->file_name, off,
298 len);
Jens Axboeb5af8292007-03-08 12:43:13 +0100299
Jens Axboee5b401d2006-10-18 16:03:40 +0200300 /*
301 * FIXME: add blockdev flushing too
302 */
Jens Axboeac893112009-06-02 13:06:01 +0200303 if (f->mmap_ptr)
304 ret = madvise(f->mmap_ptr, f->mmap_sz, MADV_DONTNEED);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100305 else if (f->filetype == FIO_TYPE_FILE) {
Jens Axboe3baddf22008-04-04 11:10:30 +0200306 ret = fadvise(f->fd, off, len, POSIX_FADV_DONTNEED);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100307 } else if (f->filetype == FIO_TYPE_BD) {
Jens Axboeb5af8292007-03-08 12:43:13 +0100308 ret = blockdev_invalidate_cache(f->fd);
Jens Axboe7e0e25c2007-03-27 12:49:56 +0200309 if (ret < 0 && errno == EACCES && geteuid()) {
Jens Axboe7172cfe2007-07-23 14:36:00 +0200310 if (!root_warn) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100311 log_err("fio: only root may flush block "
312 "devices. Cache flush bypassed!\n");
Jens Axboe7172cfe2007-07-23 14:36:00 +0200313 root_warn = 1;
314 }
Jens Axboe7e0e25c2007-03-27 12:49:56 +0200315 ret = 0;
316 }
Jens Axboeb5605e92007-04-16 20:42:33 +0200317 } else if (f->filetype == FIO_TYPE_CHAR || f->filetype == FIO_TYPE_PIPE)
Jens Axboee5b401d2006-10-18 16:03:40 +0200318 ret = 0;
319
320 if (ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100321 td_verror(td, errno, "invalidate_cache");
Jens Axboee5b401d2006-10-18 16:03:40 +0200322 return 1;
Jens Axboe3baddf22008-04-04 11:10:30 +0200323 } else if (ret > 0) {
324 td_verror(td, ret, "invalidate_cache");
325 return 1;
Jens Axboee5b401d2006-10-18 16:03:40 +0200326 }
327
Jens Axboead2da602007-02-26 12:33:27 +0100328 return ret;
Jens Axboe3baddf22008-04-04 11:10:30 +0200329
330}
331
332int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
333{
Jens Axboed6aed792009-06-03 08:41:15 +0200334 if (!fio_file_open(f))
Jens Axboea5fb4612008-05-28 10:54:01 +0200335 return 0;
336
Jens Axboe5e0074c2009-06-02 21:40:09 +0200337 return __file_invalidate_cache(td, f, -1ULL, -1ULL);
Jens Axboee5b401d2006-10-18 16:03:40 +0200338}
339
Jens Axboe6977bcd2008-03-01 15:55:36 +0100340int generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
Jens Axboe53cdc682006-10-18 11:50:58 +0200341{
Jens Axboe6977bcd2008-03-01 15:55:36 +0100342 int ret = 0;
343
Jens Axboeee56ad52008-02-01 10:30:20 +0100344 dprint(FD_FILE, "fd close %s\n", f->file_name);
Jens Axboe4906e0b2008-03-01 18:59:51 +0100345
346 remove_file_hash(f);
347
Jens Axboe6977bcd2008-03-01 15:55:36 +0100348 if (close(f->fd) < 0)
349 ret = errno;
350
Jens Axboeb5af8292007-03-08 12:43:13 +0100351 f->fd = -1;
Jens Axboe6977bcd2008-03-01 15:55:36 +0100352 return ret;
Jens Axboe53cdc682006-10-18 11:50:58 +0200353}
354
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100355static int file_lookup_open(struct fio_file *f, int flags)
Jens Axboe53cdc682006-10-18 11:50:58 +0200356{
Jens Axboe29c13492008-03-01 19:25:20 +0100357 struct fio_file *__f;
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100358 int from_hash;
359
360 __f = lookup_file_hash(f->file_name);
361 if (__f) {
Jens Axboe9efef3c2008-03-06 10:26:25 +0100362 dprint(FD_FILE, "found file in hash %s\n", f->file_name);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100363 /*
364 * racy, need the __f->lock locked
365 */
366 f->lock = __f->lock;
367 f->lock_owner = __f->lock_owner;
368 f->lock_batch = __f->lock_batch;
369 f->lock_ddir = __f->lock_ddir;
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100370 from_hash = 1;
371 } else {
Jens Axboe9efef3c2008-03-06 10:26:25 +0100372 dprint(FD_FILE, "file not found in hash %s\n", f->file_name);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100373 from_hash = 0;
374 }
375
Jens Axboee8670ef2008-03-06 12:06:30 +0100376 f->fd = open(f->file_name, flags, 0600);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100377 return from_hash;
378}
379
380int generic_open_file(struct thread_data *td, struct fio_file *f)
381{
Jens Axboe66159822007-04-16 21:54:24 +0200382 int is_std = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200383 int flags = 0;
Jens Axboe29c13492008-03-01 19:25:20 +0100384 int from_hash = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200385
Jens Axboeee56ad52008-02-01 10:30:20 +0100386 dprint(FD_FILE, "fd open %s\n", f->file_name);
387
Jens Axboe66159822007-04-16 21:54:24 +0200388 if (!strcmp(f->file_name, "-")) {
389 if (td_rw(td)) {
390 log_err("fio: can't read/write to stdin/out\n");
391 return 1;
392 }
393 is_std = 1;
Jens Axboece98fa62007-04-17 13:27:32 +0200394
395 /*
396 * move output logging to stderr, if we are writing to stdout
397 */
398 if (td_write(td))
399 f_out = stderr;
Jens Axboe66159822007-04-16 21:54:24 +0200400 }
401
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100402 if (td->o.odirect)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100403 flags |= OS_O_DIRECT;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100404 if (td->o.sync_io)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100405 flags |= O_SYNC;
Jens Axboead923962007-10-11 21:41:41 +0200406 if (f->filetype != FIO_TYPE_FILE)
Jens Axboe5921e802008-05-30 15:02:38 +0200407 flags |= FIO_O_NOATIME;
Jens Axboe814452b2009-03-04 12:53:13 +0100408 if (td->o.create_on_open)
409 flags |= O_CREAT;
Jens Axboe7abf8332006-11-23 15:01:19 +0100410
Aaron Carroll056f3452007-11-26 09:08:53 +0100411open_again:
Jens Axboe660a1cb2007-04-17 15:37:47 +0200412 if (td_write(td)) {
Jens Axboe17308152008-03-07 13:42:31 +0100413 if (!read_only)
414 flags |= O_RDWR;
Jens Axboe53cdc682006-10-18 11:50:58 +0200415
Jens Axboeaf52b342007-03-13 10:07:47 +0100416 if (f->filetype == FIO_TYPE_FILE)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100417 flags |= O_CREAT;
Jens Axboe2fd233b2007-03-02 08:44:25 +0100418
Jens Axboe66159822007-04-16 21:54:24 +0200419 if (is_std)
420 f->fd = dup(STDOUT_FILENO);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100421 else
422 from_hash = file_lookup_open(f, flags);
Jens Axboe2fd233b2007-03-02 08:44:25 +0100423 } else {
Jens Axboe4241ea82007-09-12 08:18:36 +0200424 if (f->filetype == FIO_TYPE_CHAR && !read_only)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100425 flags |= O_RDWR;
426 else
427 flags |= O_RDONLY;
428
Jens Axboe66159822007-04-16 21:54:24 +0200429 if (is_std)
430 f->fd = dup(STDIN_FILENO);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100431 else
432 from_hash = file_lookup_open(f, flags);
Jens Axboe53cdc682006-10-18 11:50:58 +0200433 }
434
435 if (f->fd == -1) {
Jens Axboee4e33252007-03-21 13:07:54 +0100436 char buf[FIO_VERROR_SIZE];
Jens Axboee1161c32007-02-22 19:36:48 +0100437 int __e = errno;
438
Jens Axboe835d9b92009-06-09 15:23:38 +0200439 if (__e == EPERM && (flags & FIO_O_NOATIME)) {
Jens Axboe5921e802008-05-30 15:02:38 +0200440 flags &= ~FIO_O_NOATIME;
Aaron Carroll056f3452007-11-26 09:08:53 +0100441 goto open_again;
442 }
443
Jens Axboee8670ef2008-03-06 12:06:30 +0100444 snprintf(buf, sizeof(buf) - 1, "open(%s)", f->file_name);
Jens Axboee4e33252007-03-21 13:07:54 +0100445
446 td_verror(td, __e, buf);
Jens Axboe53cdc682006-10-18 11:50:58 +0200447 }
448
Jens Axboe29c13492008-03-01 19:25:20 +0100449 if (!from_hash && f->fd != -1) {
450 if (add_file_hash(f)) {
451 int ret;
452
453 /*
454 * OK to ignore, we haven't done anything with it
455 */
456 ret = generic_close_file(td, f);
457 goto open_again;
458 }
459 }
Jens Axboe4906e0b2008-03-01 18:59:51 +0100460
Jens Axboe53cdc682006-10-18 11:50:58 +0200461 return 0;
Jens Axboeb5af8292007-03-08 12:43:13 +0100462}
463
Jens Axboedf9c26b2009-03-05 10:13:58 +0100464int generic_get_file_size(struct thread_data *td, struct fio_file *f)
Jens Axboe21972cd2006-11-23 12:39:16 +0100465{
Jens Axboedf9c26b2009-03-05 10:13:58 +0100466 return get_file_size(td, f);
Jens Axboe21972cd2006-11-23 12:39:16 +0100467}
468
Jens Axboe7bb48f82007-03-27 15:30:28 +0200469/*
470 * open/close all files, so that ->real_file_size gets set
471 */
Jens Axboebab3fd52007-04-02 10:34:18 +0200472static int get_file_sizes(struct thread_data *td)
Jens Axboe53cdc682006-10-18 11:50:58 +0200473{
474 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100475 unsigned int i;
Jens Axboebab3fd52007-04-02 10:34:18 +0200476 int err = 0;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200477
478 for_each_file(td, f, i) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100479 dprint(FD_FILE, "get file size for %p/%d/%p\n", f, i,
480 f->file_name);
Jens Axboe9efef3c2008-03-06 10:26:25 +0100481
Jens Axboe99a47c62009-04-07 22:20:56 +0200482 if (td_io_get_file_size(td, f)) {
Jens Axboe40b44f42007-04-11 12:43:35 +0200483 if (td->error != ENOENT) {
484 log_err("%s\n", td->verror);
485 err = 1;
486 }
Jens Axboe541d66d2007-03-27 19:50:11 +0200487 clear_error(td);
Jens Axboe07eb79d2007-04-12 13:02:38 +0200488 }
Jens Axboe409b3412007-03-28 09:57:01 +0200489
490 if (f->real_file_size == -1ULL && td->o.size)
491 f->real_file_size = td->o.size / td->o.nr_files;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200492 }
Jens Axboebab3fd52007-04-02 10:34:18 +0200493
494 return err;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200495}
496
497/*
498 * Open the files and setup files sizes, creating files if necessary.
499 */
500int setup_files(struct thread_data *td)
501{
502 unsigned long long total_size, extend_size;
503 struct fio_file *f;
504 unsigned int i;
Jens Axboe000b0802007-03-27 15:56:10 +0200505 int err = 0, need_extend;
Jens Axboe53cdc682006-10-18 11:50:58 +0200506
Jens Axboeee56ad52008-02-01 10:30:20 +0100507 dprint(FD_FILE, "setup files\n");
508
Jens Axboe691c8fb2008-03-07 14:26:26 +0100509 if (td->o.read_iolog_file)
510 return 0;
511
Jens Axboe53cdc682006-10-18 11:50:58 +0200512 /*
513 * if ioengine defines a setup() method, it's responsible for
Jens Axboe7bb48f82007-03-27 15:30:28 +0200514 * opening the files and setting f->real_file_size to indicate
515 * the valid range for that file.
Jens Axboe53cdc682006-10-18 11:50:58 +0200516 */
517 if (td->io_ops->setup)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200518 err = td->io_ops->setup(td);
519 else
Jens Axboebab3fd52007-04-02 10:34:18 +0200520 err = get_file_sizes(td);
Jens Axboe53cdc682006-10-18 11:50:58 +0200521
Jens Axboef1027062006-10-20 10:14:01 +0200522 if (err)
523 return err;
524
Jens Axboe0a7eb122006-10-20 10:36:42 +0200525 /*
Jens Axboe7bb48f82007-03-27 15:30:28 +0200526 * check sizes. if the files/devices do not exist and the size
527 * isn't passed to fio, abort.
Jens Axboe0a7eb122006-10-20 10:36:42 +0200528 */
Jens Axboe7bb48f82007-03-27 15:30:28 +0200529 total_size = 0;
530 for_each_file(td, f, i) {
531 if (f->real_file_size == -1ULL)
532 total_size = -1ULL;
533 else
534 total_size += f->real_file_size;
535 }
Jens Axboe0a7eb122006-10-20 10:36:42 +0200536
Jens Axboe7bb48f82007-03-27 15:30:28 +0200537 /*
538 * device/file sizes are zero and no size given, punt
539 */
Jens Axboe1f809d12007-10-25 18:34:02 +0200540 if ((!total_size || total_size == -1ULL) && !td->o.size &&
Shawn Lewisaa31f1f2008-01-11 09:45:11 +0100541 !(td->io_ops->flags & FIO_NOIO) && !td->o.fill_device) {
Jens Axboe7bb48f82007-03-27 15:30:28 +0200542 log_err("%s: you need to specify size=\n", td->o.name);
Jens Axboee1161c32007-02-22 19:36:48 +0100543 td_verror(td, EINVAL, "total_file_size");
Jens Axboe53cdc682006-10-18 11:50:58 +0200544 return 1;
545 }
546
Jens Axboe7bb48f82007-03-27 15:30:28 +0200547 /*
548 * now file sizes are known, so we can set ->io_size. if size= is
549 * not given, ->io_size is just equal to ->real_file_size. if size
550 * is given, ->io_size is size / nr_files.
551 */
552 extend_size = total_size = 0;
553 need_extend = 0;
554 for_each_file(td, f, i) {
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200555 f->file_offset = td->o.start_offset;
556
Jens Axboe7bb48f82007-03-27 15:30:28 +0200557 if (!td->o.file_size_low) {
558 /*
559 * no file size range given, file size is equal to
560 * total size divided by number of files. if that is
561 * zero, set it to the real file size.
562 */
563 f->io_size = td->o.size / td->o.nr_files;
Jens Axboe65bdb102008-01-24 13:13:12 +0100564 if (!f->io_size)
Jens Axboe273f8c92008-01-25 14:02:15 +0100565 f->io_size = f->real_file_size - f->file_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200566 } else if (f->real_file_size < td->o.file_size_low ||
567 f->real_file_size > td->o.file_size_high) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100568 if (f->file_offset > td->o.file_size_low)
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200569 goto err_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200570 /*
571 * file size given. if it's fixed, use that. if it's a
572 * range, generate a random size in-between.
573 */
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100574 if (td->o.file_size_low == td->o.file_size_high) {
575 f->io_size = td->o.file_size_low
576 - f->file_offset;
577 } else {
578 f->io_size = get_rand_file_size(td)
579 - f->file_offset;
580 }
Jens Axboe65bdb102008-01-24 13:13:12 +0100581 } else
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200582 f->io_size = f->real_file_size - f->file_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200583
584 if (f->io_size == -1ULL)
585 total_size = -1ULL;
586 else
587 total_size += f->io_size;
588
589 if (f->filetype == FIO_TYPE_FILE &&
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200590 (f->io_size + f->file_offset) > f->real_file_size &&
Jens Axboe7bb48f82007-03-27 15:30:28 +0200591 !(td->io_ops->flags & FIO_DISKLESSIO)) {
Jens Axboe814452b2009-03-04 12:53:13 +0100592 if (!td->o.create_on_open) {
593 need_extend++;
594 extend_size += (f->io_size + f->file_offset);
595 } else
596 f->real_file_size = f->io_size + f->file_offset;
Jens Axboed6aed792009-06-03 08:41:15 +0200597 fio_file_set_extend(f);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100598 }
Jens Axboe7bb48f82007-03-27 15:30:28 +0200599 }
600
ljzhang,Yaxin Hu,Jianchao Tang22982902007-07-27 13:21:28 +0200601 if (!td->o.size || td->o.size > total_size)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200602 td->o.size = total_size;
603
604 /*
605 * See if we need to extend some files
606 */
607 if (need_extend) {
608 temp_stall_ts = 1;
Shaozhi Shawn Yea7ba8c52008-09-10 09:04:18 +0200609 if (!terse_output)
610 log_info("%s: Laying out IO file(s) (%u file(s) /"
611 " %LuMiB)\n", td->o.name, need_extend,
612 extend_size >> 20);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200613
614 for_each_file(td, f, i) {
Jens Axboe5e0074c2009-06-02 21:40:09 +0200615 unsigned long long old_len = -1ULL, extend_len = -1ULL;
Jens Axboe3baddf22008-04-04 11:10:30 +0200616
Jens Axboed6aed792009-06-03 08:41:15 +0200617 if (!fio_file_extend(f))
Jens Axboe7bb48f82007-03-27 15:30:28 +0200618 continue;
619
Jens Axboe409b3412007-03-28 09:57:01 +0200620 assert(f->filetype == FIO_TYPE_FILE);
Jens Axboed6aed792009-06-03 08:41:15 +0200621 fio_file_clear_extend(f);
Jens Axboe5e0074c2009-06-02 21:40:09 +0200622 if (!td->o.fill_device) {
623 old_len = f->real_file_size;
624 extend_len = f->io_size + f->file_offset - old_len;
625 }
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200626 f->real_file_size = (f->io_size + f->file_offset);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200627 err = extend_file(td, f);
628 if (err)
629 break;
Jens Axboe5e0074c2009-06-02 21:40:09 +0200630
Jens Axboe3baddf22008-04-04 11:10:30 +0200631 err = __file_invalidate_cache(td, f, old_len,
632 extend_len);
633 close(f->fd);
634 f->fd = -1;
635 if (err)
636 break;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200637 }
638 temp_stall_ts = 0;
639 }
640
641 if (err)
642 return err;
643
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100644 if (!td->o.zone_size)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200645 td->o.zone_size = td->o.size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200646
Jens Axboeea966f82007-09-03 10:09:37 +0200647 /*
648 * iolog already set the total io size, if we read back
649 * stored entries.
650 */
651 if (!td->o.read_iolog_file)
652 td->total_io_size = td->o.size * td->o.loops;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200653 return 0;
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200654err_offset:
655 log_err("%s: you need to specify valid offset=\n", td->o.name);
656 return 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200657}
658
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200659int pre_read_files(struct thread_data *td)
660{
661 struct fio_file *f;
662 unsigned int i;
663
664 dprint(FD_FILE, "pre_read files\n");
665
666 for_each_file(td, f, i) {
667 pre_read_file(td, f);
668 }
669
670 return 1;
671}
672
Jens Axboe68727072007-03-15 20:49:25 +0100673int init_random_map(struct thread_data *td)
674{
Jens Axboe509eab12007-12-14 12:42:53 +0100675 unsigned long long blocks, num_maps;
Jens Axboe68727072007-03-15 20:49:25 +0100676 struct fio_file *f;
677 unsigned int i;
678
Jens Axboede8dd112008-03-01 15:34:44 +0100679 if (td->o.norandommap || !td_random(td))
Jens Axboe68727072007-03-15 20:49:25 +0100680 return 0;
681
682 for_each_file(td, f, i) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100683 blocks = (f->real_file_size + td->o.rw_min_bs - 1) /
684 (unsigned long long) td->o.rw_min_bs;
685 num_maps = (blocks + BLOCKS_PER_MAP - 1) /
686 (unsigned long long) BLOCKS_PER_MAP;
Jens Axboede605662008-05-31 00:21:12 +0200687 f->file_map = smalloc(num_maps * sizeof(int));
Jens Axboe303032a2008-03-26 10:11:10 +0100688 if (f->file_map) {
689 f->num_maps = num_maps;
690 continue;
Jens Axboe68727072007-03-15 20:49:25 +0100691 }
Jens Axboe2b386d22008-03-26 10:32:57 +0100692 if (!td->o.softrandommap) {
Jens Axboe68727072007-03-15 20:49:25 +0100693 log_err("fio: failed allocating random map. If running"
694 " a large number of jobs, try the 'norandommap'"
Jens Axboe2b386d22008-03-26 10:32:57 +0100695 " option or set 'softrandommap'. Or give"
696 " a larger --alloc-size to fio.\n");
Jens Axboe68727072007-03-15 20:49:25 +0100697 return 1;
698 }
Jens Axboe303032a2008-03-26 10:11:10 +0100699
700 log_info("fio: file %s failed allocating random map. Running "
701 "job without.\n", f->file_name);
702 f->num_maps = 0;
Jens Axboe68727072007-03-15 20:49:25 +0100703 }
704
705 return 0;
706}
707
Jens Axboe53cdc682006-10-18 11:50:58 +0200708void close_files(struct thread_data *td)
709{
Jens Axboe0ab8db82006-10-18 17:16:23 +0200710 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100711 unsigned int i;
Jens Axboe53cdc682006-10-18 11:50:58 +0200712
Jens Axboe2be3eec2009-06-10 09:05:13 +0200713 for_each_file(td, f, i) {
714 if (fio_file_open(f))
715 td_io_close_file(td, f);
716 }
Jens Axboe24ffd2c2008-03-01 15:47:08 +0100717}
718
719void close_and_free_files(struct thread_data *td)
720{
721 struct fio_file *f;
722 unsigned int i;
723
Jens Axboeee56ad52008-02-01 10:30:20 +0100724 dprint(FD_FILE, "close files\n");
725
Jens Axboe0ab8db82006-10-18 17:16:23 +0200726 for_each_file(td, f, i) {
Jens Axboeffdfabd2008-03-26 09:18:14 +0100727 if (td->o.unlink && f->filetype == FIO_TYPE_FILE) {
728 dprint(FD_FILE, "free unlink %s\n", f->file_name);
Jens Axboe132ad462006-11-23 12:23:12 +0100729 unlink(f->file_name);
Jens Axboeffdfabd2008-03-26 09:18:14 +0100730 }
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100731
Jens Axboe22a57ba2009-06-09 14:34:35 +0200732 if (fio_file_open(f))
733 td_io_close_file(td, f);
734
Shaozhi Shawn Yeb9fbcf22008-09-10 09:05:17 +0200735 remove_file_hash(f);
Jens Axboeb3dc7f02007-03-09 12:52:15 +0100736
Jens Axboef17c4392008-03-01 18:40:46 +0100737 sfree(f->file_name);
Jens Axboefa1da862007-03-23 15:20:54 +0100738 f->file_name = NULL;
739
Jens Axboec3439812007-03-20 13:54:53 +0100740 if (f->file_map) {
Jens Axboef17c4392008-03-01 18:40:46 +0100741 sfree(f->file_map);
Jens Axboec3439812007-03-20 13:54:53 +0100742 f->file_map = NULL;
743 }
Jens Axboe78d99e62008-03-01 18:41:50 +0100744 sfree(f);
Jens Axboe53cdc682006-10-18 11:50:58 +0200745 }
Jens Axboeb4a6a592006-10-20 13:54:47 +0200746
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100747 td->o.filename = NULL;
Jens Axboecade3ef2007-03-23 15:29:45 +0100748 free(td->files);
Jens Axboe9efef3c2008-03-06 10:26:25 +0100749 td->files_index = 0;
Jens Axboeb4a6a592006-10-20 13:54:47 +0200750 td->files = NULL;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100751 td->o.nr_files = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200752}
Jens Axboeaf52b342007-03-13 10:07:47 +0100753
Jens Axboee3bab462007-03-13 11:22:09 +0100754static void get_file_type(struct fio_file *f)
Jens Axboeaf52b342007-03-13 10:07:47 +0100755{
756 struct stat sb;
757
Jens Axboe66159822007-04-16 21:54:24 +0200758 if (!strcmp(f->file_name, "-"))
759 f->filetype = FIO_TYPE_PIPE;
760 else
761 f->filetype = FIO_TYPE_FILE;
Jens Axboeaf52b342007-03-13 10:07:47 +0100762
Jens Axboee3bab462007-03-13 11:22:09 +0100763 if (!lstat(f->file_name, &sb)) {
Jens Axboeaf52b342007-03-13 10:07:47 +0100764 if (S_ISBLK(sb.st_mode))
765 f->filetype = FIO_TYPE_BD;
766 else if (S_ISCHR(sb.st_mode))
767 f->filetype = FIO_TYPE_CHAR;
Jens Axboeb5605e92007-04-16 20:42:33 +0200768 else if (S_ISFIFO(sb.st_mode))
769 f->filetype = FIO_TYPE_PIPE;
Jens Axboeaf52b342007-03-13 10:07:47 +0100770 }
771}
772
Jens Axboef29b25a2007-07-23 08:56:43 +0200773int add_file(struct thread_data *td, const char *fname)
Jens Axboeaf52b342007-03-13 10:07:47 +0100774{
Jens Axboe7b4e4fe2007-03-13 12:51:40 +0100775 int cur_files = td->files_index;
Jens Axboebd0ee742007-03-23 14:26:23 +0100776 char file_name[PATH_MAX];
Jens Axboeaf52b342007-03-13 10:07:47 +0100777 struct fio_file *f;
Jens Axboebd0ee742007-03-23 14:26:23 +0100778 int len = 0;
Jens Axboeaf52b342007-03-13 10:07:47 +0100779
Jens Axboeee56ad52008-02-01 10:30:20 +0100780 dprint(FD_FILE, "add file %s\n", fname);
781
Jens Axboef17c4392008-03-01 18:40:46 +0100782 f = smalloc(sizeof(*f));
Jens Axboec48c0be2008-07-31 19:55:02 +0200783 if (!f) {
784 log_err("fio: smalloc OOM\n");
785 assert(0);
786 }
787
Jens Axboeaf52b342007-03-13 10:07:47 +0100788 f->fd = -1;
Jens Axboebd0ee742007-03-23 14:26:23 +0100789
Jens Axboefc99bc02009-03-04 15:27:42 +0100790 if (td->files_size <= td->files_index) {
Carl Henrik Lunde4b341fc2009-04-20 08:41:37 +0200791 int new_size = td->o.nr_files + 1;
Jens Axboe126d65c2008-03-01 18:04:31 +0100792
Jens Axboefc99bc02009-03-04 15:27:42 +0100793 dprint(FD_FILE, "resize file array to %d files\n", new_size);
794
795 td->files = realloc(td->files, new_size * sizeof(f));
Jens Axboefc99bc02009-03-04 15:27:42 +0100796 td->files_size = new_size;
797 }
Jens Axboe8bb76792009-03-04 15:37:52 +0100798 td->files[cur_files] = f;
Jens Axboe126d65c2008-03-01 18:04:31 +0100799
Jens Axboe07eb79d2007-04-12 13:02:38 +0200800 /*
801 * init function, io engine may not be loaded yet
802 */
803 if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO))
804 f->real_file_size = -1ULL;
805
Jens Axboebd0ee742007-03-23 14:26:23 +0100806 if (td->o.directory)
807 len = sprintf(file_name, "%s/", td->o.directory);
808
809 sprintf(file_name + len, "%s", fname);
Jens Axboef17c4392008-03-01 18:40:46 +0100810 f->file_name = smalloc_strdup(file_name);
Jens Axboec48c0be2008-07-31 19:55:02 +0200811 if (!f->file_name) {
812 log_err("fio: smalloc OOM\n");
813 assert(0);
814 }
815
Jens Axboee3bab462007-03-13 11:22:09 +0100816 get_file_type(f);
Jens Axboeaf52b342007-03-13 10:07:47 +0100817
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100818 switch (td->o.file_lock_mode) {
819 case FILE_LOCK_NONE:
820 break;
821 case FILE_LOCK_READWRITE:
822 f->lock = fio_mutex_rw_init();
823 break;
824 case FILE_LOCK_EXCLUSIVE:
825 f->lock = fio_mutex_init(1);
826 break;
827 default:
828 log_err("fio: unknown lock mode: %d\n", td->o.file_lock_mode);
829 assert(0);
830 }
Jens Axboe29c13492008-03-01 19:25:20 +0100831
Jens Axboe7b4e4fe2007-03-13 12:51:40 +0100832 td->files_index++;
Jens Axboe15494412007-03-13 11:52:47 +0100833 if (f->filetype == FIO_TYPE_FILE)
834 td->nr_normal_files++;
Jens Axboef29b25a2007-07-23 08:56:43 +0200835
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100836 dprint(FD_FILE, "file %p \"%s\" added at %d\n", f, f->file_name,
837 cur_files);
Jens Axboe9efef3c2008-03-06 10:26:25 +0100838
Jens Axboef29b25a2007-07-23 08:56:43 +0200839 return cur_files;
Jens Axboeaf52b342007-03-13 10:07:47 +0100840}
Jens Axboe0ad920e2007-03-13 11:06:45 +0100841
842void get_file(struct fio_file *f)
843{
Jens Axboe8172fe92008-02-01 21:51:02 +0100844 dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references);
Jens Axboed6aed792009-06-03 08:41:15 +0200845 assert(fio_file_open(f));
Jens Axboe0ad920e2007-03-13 11:06:45 +0100846 f->references++;
847}
848
Jens Axboe6977bcd2008-03-01 15:55:36 +0100849int put_file(struct thread_data *td, struct fio_file *f)
Jens Axboe0ad920e2007-03-13 11:06:45 +0100850{
Jens Axboe98e1ac42008-03-06 11:56:48 +0100851 int f_ret = 0, ret = 0;
Jens Axboe6977bcd2008-03-01 15:55:36 +0100852
Jens Axboe8172fe92008-02-01 21:51:02 +0100853 dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references);
Jens Axboeee56ad52008-02-01 10:30:20 +0100854
Jens Axboe22a57ba2009-06-09 14:34:35 +0200855 if (!fio_file_open(f)) {
856 assert(f->fd == -1);
Jens Axboe6977bcd2008-03-01 15:55:36 +0100857 return 0;
Jens Axboe22a57ba2009-06-09 14:34:35 +0200858 }
Jens Axboe0ad920e2007-03-13 11:06:45 +0100859
860 assert(f->references);
861 if (--f->references)
Jens Axboe6977bcd2008-03-01 15:55:36 +0100862 return 0;
Jens Axboe0ad920e2007-03-13 11:06:45 +0100863
Jens Axboed424d4d2007-04-17 09:05:10 +0200864 if (should_fsync(td) && td->o.fsync_on_close)
Jens Axboe98e1ac42008-03-06 11:56:48 +0100865 f_ret = fsync(f->fd);
Jens Axboeebb14152007-03-13 14:42:15 +0100866
Jens Axboe0ad920e2007-03-13 11:06:45 +0100867 if (td->io_ops->close_file)
Jens Axboe6977bcd2008-03-01 15:55:36 +0100868 ret = td->io_ops->close_file(td, f);
Jens Axboe1020a132007-03-29 11:15:06 +0200869
Jens Axboe98e1ac42008-03-06 11:56:48 +0100870 if (!ret)
Jens Axboea5fb4612008-05-28 10:54:01 +0200871 ret = f_ret;
Jens Axboe98e1ac42008-03-06 11:56:48 +0100872
Jens Axboe0ad920e2007-03-13 11:06:45 +0100873 td->nr_open_files--;
Jens Axboed6aed792009-06-03 08:41:15 +0200874 fio_file_clear_open(f);
Jens Axboe22a57ba2009-06-09 14:34:35 +0200875 assert(f->fd == -1);
Jens Axboe6977bcd2008-03-01 15:55:36 +0100876 return ret;
Jens Axboe0ad920e2007-03-13 11:06:45 +0100877}
Jens Axboebbf6b542007-03-13 15:28:55 +0100878
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100879void lock_file(struct thread_data *td, struct fio_file *f, enum fio_ddir ddir)
Jens Axboeb2bd2bd2008-03-01 18:19:52 +0100880{
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100881 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
882 return;
Jens Axboe29c13492008-03-01 19:25:20 +0100883
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100884 if (f->lock_owner == td && f->lock_batch--)
885 return;
886
887 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
888 if (ddir == DDIR_READ)
889 fio_mutex_down_read(f->lock);
890 else
891 fio_mutex_down_write(f->lock);
892 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
893 fio_mutex_down(f->lock);
894
895 f->lock_owner = td;
896 f->lock_batch = td->o.lockfile_batch;
897 f->lock_ddir = ddir;
898}
899
900void unlock_file(struct thread_data *td, struct fio_file *f)
901{
902 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
903 return;
904 if (f->lock_batch)
905 return;
906
907 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
908 const int is_read = f->lock_ddir == DDIR_READ;
909 int val = fio_mutex_getval(f->lock);
910
911 if ((is_read && val == 1) || (!is_read && val == -1))
912 f->lock_owner = NULL;
913
914 if (is_read)
915 fio_mutex_up_read(f->lock);
916 else
917 fio_mutex_up_write(f->lock);
918 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE) {
919 int val = fio_mutex_getval(f->lock);
920
921 if (val == 0)
922 f->lock_owner = NULL;
923
924 fio_mutex_up(f->lock);
Jens Axboe29c13492008-03-01 19:25:20 +0100925 }
Jens Axboeb2bd2bd2008-03-01 18:19:52 +0100926}
927
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100928void unlock_file_all(struct thread_data *td, struct fio_file *f)
Jens Axboeb2bd2bd2008-03-01 18:19:52 +0100929{
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100930 if (f->lock_owner != td)
931 return;
Jens Axboe29c13492008-03-01 19:25:20 +0100932
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100933 f->lock_batch = 0;
934 unlock_file(td, f);
Jens Axboeb2bd2bd2008-03-01 18:19:52 +0100935}
936
Jens Axboebbf6b542007-03-13 15:28:55 +0100937static int recurse_dir(struct thread_data *td, const char *dirname)
938{
939 struct dirent *dir;
940 int ret = 0;
941 DIR *D;
942
943 D = opendir(dirname);
944 if (!D) {
Jens Axboe0ddb2702007-03-27 19:43:53 +0200945 char buf[FIO_VERROR_SIZE];
946
947 snprintf(buf, FIO_VERROR_SIZE - 1, "opendir(%s)", dirname);
948 td_verror(td, errno, buf);
Jens Axboebbf6b542007-03-13 15:28:55 +0100949 return 1;
950 }
951
952 while ((dir = readdir(D)) != NULL) {
953 char full_path[PATH_MAX];
954 struct stat sb;
955
Jens Axboee85b2b82007-03-14 09:39:06 +0100956 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
957 continue;
Jens Axboe96d32d52007-03-14 09:16:09 +0100958
Jens Axboebbf6b542007-03-13 15:28:55 +0100959 sprintf(full_path, "%s/%s", dirname, dir->d_name);
960
961 if (lstat(full_path, &sb) == -1) {
962 if (errno != ENOENT) {
963 td_verror(td, errno, "stat");
964 return 1;
965 }
966 }
967
968 if (S_ISREG(sb.st_mode)) {
969 add_file(td, full_path);
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100970 td->o.nr_files++;
Jens Axboebbf6b542007-03-13 15:28:55 +0100971 continue;
972 }
Jens Axboe0ddb2702007-03-27 19:43:53 +0200973 if (!S_ISDIR(sb.st_mode))
974 continue;
Jens Axboebbf6b542007-03-13 15:28:55 +0100975
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100976 ret = recurse_dir(td, full_path);
977 if (ret)
Jens Axboebbf6b542007-03-13 15:28:55 +0100978 break;
979 }
980
981 closedir(D);
982 return ret;
983}
984
985int add_dir_files(struct thread_data *td, const char *path)
986{
Jens Axboe0ddb2702007-03-27 19:43:53 +0200987 int ret = recurse_dir(td, path);
988
989 if (!ret)
990 log_info("fio: opendir added %d files\n", td->o.nr_files);
991
992 return ret;
Jens Axboebbf6b542007-03-13 15:28:55 +0100993}
Jens Axboecade3ef2007-03-23 15:29:45 +0100994
995void dup_files(struct thread_data *td, struct thread_data *org)
996{
997 struct fio_file *f;
998 unsigned int i;
Jens Axboe9efef3c2008-03-06 10:26:25 +0100999
1000 dprint(FD_FILE, "dup files: %d\n", org->files_index);
Jens Axboecade3ef2007-03-23 15:29:45 +01001001
1002 if (!org->files)
1003 return;
1004
Jens Axboe9efef3c2008-03-06 10:26:25 +01001005 td->files = malloc(org->files_index * sizeof(f));
Jens Axboecade3ef2007-03-23 15:29:45 +01001006
Jens Axboe9efef3c2008-03-06 10:26:25 +01001007 for_each_file(org, f, i) {
Jens Axboeb0fe4212008-03-01 18:22:27 +01001008 struct fio_file *__f;
1009
Jens Axboef17c4392008-03-01 18:40:46 +01001010 __f = smalloc(sizeof(*__f));
Jens Axboec48c0be2008-07-31 19:55:02 +02001011 if (!__f) {
1012 log_err("fio: smalloc OOM\n");
1013 assert(0);
1014 }
Jens Axboe22a57ba2009-06-09 14:34:35 +02001015 __f->fd = -1;
Jens Axboec48c0be2008-07-31 19:55:02 +02001016
Aaron Carrollbc3456f2008-06-25 09:20:37 +02001017 if (f->file_name) {
Jens Axboef17c4392008-03-01 18:40:46 +01001018 __f->file_name = smalloc_strdup(f->file_name);
Jens Axboec48c0be2008-07-31 19:55:02 +02001019 if (!__f->file_name) {
1020 log_err("fio: smalloc OOM\n");
1021 assert(0);
1022 }
1023
Aaron Carrollbc3456f2008-06-25 09:20:37 +02001024 __f->filetype = f->filetype;
1025 }
Jens Axboeb0fe4212008-03-01 18:22:27 +01001026
1027 td->files[i] = __f;
Jens Axboecade3ef2007-03-23 15:29:45 +01001028 }
1029}
Jens Axboef29b25a2007-07-23 08:56:43 +02001030
1031/*
1032 * Returns the index that matches the filename, or -1 if not there
1033 */
1034int get_fileno(struct thread_data *td, const char *fname)
1035{
1036 struct fio_file *f;
1037 unsigned int i;
1038
1039 for_each_file(td, f, i)
1040 if (!strcmp(f->file_name, fname))
1041 return i;
1042
1043 return -1;
1044}
1045
1046/*
1047 * For log usage, where we add/open/close files automatically
1048 */
1049void free_release_files(struct thread_data *td)
1050{
1051 close_files(td);
1052 td->files_index = 0;
1053 td->nr_normal_files = 0;
1054}