blob: ec5d781ed0dea0e8c98c5a05289340969123e4c5 [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
Jens Axboe7bc8c2c2010-01-28 11:31:31 +010067#ifdef FIO_HAVE_FALLOCATE
68 if (td->o.fallocate && !td->o.fill_device) {
69 dprint(FD_FILE, "fallocate file %s size %llu\n", f->file_name,
70 f->real_file_size);
71
72 r = posix_fallocate(f->fd, 0, f->real_file_size);
73 if (r < 0) {
74 log_err("fio: posix_fallocate fails: %s\n",
75 strerror(-r));
76 }
77 }
78#endif
79
Shawn Lewisfc74ac12008-01-11 09:45:11 +010080 if (!new_layout)
81 goto done;
82
Jens Axboe5e0074c2009-06-02 21:40:09 +020083 /*
84 * The size will be -1ULL when fill_device is used, so don't truncate
85 * or fallocate this file, just write it
86 */
87 if (!td->o.fill_device) {
88 dprint(FD_FILE, "truncate file %s, size %llu\n", f->file_name,
Jens Axboeee56ad52008-02-01 10:30:20 +010089 f->real_file_size);
Jens Axboe5e0074c2009-06-02 21:40:09 +020090 if (ftruncate(f->fd, f->real_file_size) == -1) {
91 td_verror(td, errno, "ftruncate");
92 goto err;
93 }
Jens Axboe5e0074c2009-06-02 21:40:09 +020094 }
Jens Axboe40f82982006-10-25 11:21:05 +020095
Jens Axboe2dc1bbe2007-03-15 15:01:33 +010096 b = malloc(td->o.max_bs[DDIR_WRITE]);
97 memset(b, 0, td->o.max_bs[DDIR_WRITE]);
Jens Axboe53cdc682006-10-18 11:50:58 +020098
Jens Axboe7bb48f82007-03-27 15:30:28 +020099 left = f->real_file_size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200100 while (left && !td->terminate) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100101 bs = td->o.max_bs[DDIR_WRITE];
Jens Axboe53cdc682006-10-18 11:50:58 +0200102 if (bs > left)
103 bs = left;
104
105 r = write(f->fd, b, bs);
106
Jens Axboe5e0074c2009-06-02 21:40:09 +0200107 if (r > 0) {
108 left -= r;
Jens Axboe53cdc682006-10-18 11:50:58 +0200109 continue;
110 } else {
Jens Axboe5e0074c2009-06-02 21:40:09 +0200111 if (r < 0) {
112 int __e = errno;
113
114 if (__e == ENOSPC) {
115 if (td->o.fill_device)
116 break;
117 log_info("fio: ENOSPC on laying out "
118 "file, stopping\n");
119 break;
120 }
Jens Axboee1161c32007-02-22 19:36:48 +0100121 td_verror(td, errno, "write");
Jens Axboe5e0074c2009-06-02 21:40:09 +0200122 } else
Jens Axboee1161c32007-02-22 19:36:48 +0100123 td_verror(td, EIO, "write");
Jens Axboe53cdc682006-10-18 11:50:58 +0200124
125 break;
126 }
127 }
128
Jens Axboeffdfabd2008-03-26 09:18:14 +0100129 if (td->terminate) {
130 dprint(FD_FILE, "terminate unlink %s\n", f->file_name);
Jens Axboe53cdc682006-10-18 11:50:58 +0200131 unlink(f->file_name);
Jens Axboeffdfabd2008-03-26 09:18:14 +0100132 } else if (td->o.create_fsync) {
Jens Axboe98e1ac42008-03-06 11:56:48 +0100133 if (fsync(f->fd) < 0) {
134 td_verror(td, errno, "fsync");
135 goto err;
136 }
137 }
Jens Axboe0d1cd202009-06-05 22:11:52 +0200138 if (td->o.fill_device && !td_write(td)) {
Jens Axboed6aed792009-06-03 08:41:15 +0200139 fio_file_clear_size_known(f);
Jens Axboe5e0074c2009-06-02 21:40:09 +0200140 if (td_io_get_file_size(td, f))
141 goto err;
142 if (f->io_size > f->real_file_size)
143 f->io_size = f->real_file_size;
144 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200145
146 free(b);
Jens Axboe507a7022007-03-27 15:52:46 +0200147done:
Jens Axboe53cdc682006-10-18 11:50:58 +0200148 return 0;
149err:
150 close(f->fd);
151 f->fd = -1;
152 return 1;
153}
154
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200155static int pre_read_file(struct thread_data *td, struct fio_file *f)
156{
Jens Axboeb0f65862009-05-20 11:52:15 +0200157 int r, did_open = 0, old_runstate;
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200158 unsigned long long left;
159 unsigned int bs;
160 char *b;
161
Jens Axboe9c0d2242009-07-01 12:26:28 +0200162 if (td->io_ops->flags & FIO_PIPEIO)
163 return 0;
164
Jens Axboed6aed792009-06-03 08:41:15 +0200165 if (!fio_file_open(f)) {
Jens Axboeb0f65862009-05-20 11:52:15 +0200166 if (td->io_ops->open_file(td, f)) {
167 log_err("fio: cannot pre-read, failed to open file\n");
168 return 1;
169 }
170 did_open = 1;
171 }
172
173 old_runstate = td->runstate;
174 td_set_runstate(td, TD_PRE_READING);
175
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200176 bs = td->o.max_bs[DDIR_READ];
177 b = malloc(bs);
178 memset(b, 0, bs);
179
180 lseek(f->fd, f->file_offset, SEEK_SET);
181 left = f->io_size;
182
183 while (left && !td->terminate) {
184 if (bs > left)
185 bs = left;
186
187 r = read(f->fd, b, bs);
188
189 if (r == (int) bs) {
190 left -= bs;
191 continue;
192 } else {
193 td_verror(td, EIO, "pre_read");
194 break;
195 }
196 }
197
Jens Axboeb0f65862009-05-20 11:52:15 +0200198 td_set_runstate(td, old_runstate);
199
200 if (did_open)
201 td->io_ops->close_file(td, f);
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200202 free(b);
203 return 0;
204}
205
Jens Axboe7bb48f82007-03-27 15:30:28 +0200206static unsigned long long get_rand_file_size(struct thread_data *td)
Jens Axboe9c60ce62007-03-15 09:14:47 +0100207{
Jens Axboedc873b62008-06-04 20:13:04 +0200208 unsigned long long ret, sized;
Jens Axboe9c60ce62007-03-15 09:14:47 +0100209 long r;
210
Jens Axboe9c60ce62007-03-15 09:14:47 +0100211 r = os_random_long(&td->file_size_state);
Jens Axboedc873b62008-06-04 20:13:04 +0200212 sized = td->o.file_size_high - td->o.file_size_low;
213 ret = (unsigned long long) ((double) sized * (r / (OS_RAND_MAX + 1.0)));
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100214 ret += td->o.file_size_low;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100215 ret -= (ret % td->o.rw_min_bs);
Jens Axboe9c60ce62007-03-15 09:14:47 +0100216 return ret;
217}
218
Jens Axboe53cdc682006-10-18 11:50:58 +0200219static int file_size(struct thread_data *td, struct fio_file *f)
220{
221 struct stat st;
222
Jens Axboedf9c26b2009-03-05 10:13:58 +0100223 if (stat(f->file_name, &st) == -1) {
Jens Axboe7bb48f82007-03-27 15:30:28 +0200224 td_verror(td, errno, "fstat");
225 return 1;
226 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200227
Jens Axboe7bb48f82007-03-27 15:30:28 +0200228 f->real_file_size = st.st_size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200229 return 0;
230}
231
232static int bdev_size(struct thread_data *td, struct fio_file *f)
233{
234 unsigned long long bytes;
235 int r;
236
Jens Axboedf9c26b2009-03-05 10:13:58 +0100237 if (td->io_ops->open_file(td, f)) {
238 log_err("fio: failed opening blockdev %s for size check\n",
239 f->file_name);
240 return 1;
241 }
242
Jens Axboe53cdc682006-10-18 11:50:58 +0200243 r = blockdev_size(f->fd, &bytes);
244 if (r) {
Jens Axboee1161c32007-02-22 19:36:48 +0100245 td_verror(td, r, "blockdev_size");
Jens Axboedf9c26b2009-03-05 10:13:58 +0100246 goto err;
Jens Axboe53cdc682006-10-18 11:50:58 +0200247 }
248
Jens Axboe7ab077a2009-02-02 15:07:37 +0100249 if (!bytes) {
250 log_err("%s: zero sized block device?\n", f->file_name);
Jens Axboedf9c26b2009-03-05 10:13:58 +0100251 goto err;
Jens Axboe7ab077a2009-02-02 15:07:37 +0100252 }
253
Jens Axboe53cdc682006-10-18 11:50:58 +0200254 f->real_file_size = bytes;
Jens Axboe22a57ba2009-06-09 14:34:35 +0200255 td->io_ops->close_file(td, f);
Jens Axboe53cdc682006-10-18 11:50:58 +0200256 return 0;
Jens Axboedf9c26b2009-03-05 10:13:58 +0100257err:
258 td->io_ops->close_file(td, f);
259 return 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200260}
261
262static int get_file_size(struct thread_data *td, struct fio_file *f)
263{
264 int ret = 0;
265
Jens Axboed6aed792009-06-03 08:41:15 +0200266 if (fio_file_size_known(f))
Jens Axboe409b3412007-03-28 09:57:01 +0200267 return 0;
268
Jens Axboe7bb48f82007-03-27 15:30:28 +0200269 if (f->filetype == FIO_TYPE_FILE)
270 ret = file_size(td, f);
271 else if (f->filetype == FIO_TYPE_BD)
Jens Axboe53cdc682006-10-18 11:50:58 +0200272 ret = bdev_size(td, f);
273 else
274 f->real_file_size = -1;
275
276 if (ret)
277 return ret;
278
279 if (f->file_offset > f->real_file_size) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100280 log_err("%s: offset extends end (%Lu > %Lu)\n", td->o.name,
281 f->file_offset, f->real_file_size);
Jens Axboe53cdc682006-10-18 11:50:58 +0200282 return 1;
283 }
284
Jens Axboed6aed792009-06-03 08:41:15 +0200285 fio_file_set_size_known(f);
Jens Axboe53cdc682006-10-18 11:50:58 +0200286 return 0;
287}
288
Jens Axboe3baddf22008-04-04 11:10:30 +0200289static int __file_invalidate_cache(struct thread_data *td, struct fio_file *f,
290 unsigned long long off,
291 unsigned long long len)
Jens Axboee5b401d2006-10-18 16:03:40 +0200292{
293 int ret = 0;
294
Jens Axboe5e0074c2009-06-02 21:40:09 +0200295 if (len == -1ULL)
Jens Axboe3baddf22008-04-04 11:10:30 +0200296 len = f->io_size;
297 if (off == -1ULL)
298 off = f->file_offset;
Jens Axboeee56ad52008-02-01 10:30:20 +0100299
Jens Axboe0d1cd202009-06-05 22:11:52 +0200300 if (len == -1ULL || off == -1ULL)
301 return 0;
302
Jens Axboe3baddf22008-04-04 11:10:30 +0200303 dprint(FD_IO, "invalidate cache %s: %llu/%llu\n", f->file_name, off,
304 len);
Jens Axboeb5af8292007-03-08 12:43:13 +0100305
Jens Axboee5b401d2006-10-18 16:03:40 +0200306 /*
307 * FIXME: add blockdev flushing too
308 */
Jens Axboea1c58072009-08-04 23:17:02 +0200309 if (f->mmap_ptr) {
Jens Axboeac893112009-06-02 13:06:01 +0200310 ret = madvise(f->mmap_ptr, f->mmap_sz, MADV_DONTNEED);
Jens Axboea1c58072009-08-04 23:17:02 +0200311#ifdef FIO_MADV_FREE
312 (void) madvise(f->mmap_ptr, f->mmap_sz, FIO_MADV_FREE);
313#endif
314 } else if (f->filetype == FIO_TYPE_FILE) {
Jens Axboe3baddf22008-04-04 11:10:30 +0200315 ret = fadvise(f->fd, off, len, POSIX_FADV_DONTNEED);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100316 } else if (f->filetype == FIO_TYPE_BD) {
Jens Axboeb5af8292007-03-08 12:43:13 +0100317 ret = blockdev_invalidate_cache(f->fd);
Jens Axboe7e0e25c2007-03-27 12:49:56 +0200318 if (ret < 0 && errno == EACCES && geteuid()) {
Jens Axboe7172cfe2007-07-23 14:36:00 +0200319 if (!root_warn) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100320 log_err("fio: only root may flush block "
321 "devices. Cache flush bypassed!\n");
Jens Axboe7172cfe2007-07-23 14:36:00 +0200322 root_warn = 1;
323 }
Jens Axboe7e0e25c2007-03-27 12:49:56 +0200324 ret = 0;
325 }
Jens Axboeb5605e92007-04-16 20:42:33 +0200326 } else if (f->filetype == FIO_TYPE_CHAR || f->filetype == FIO_TYPE_PIPE)
Jens Axboee5b401d2006-10-18 16:03:40 +0200327 ret = 0;
328
329 if (ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100330 td_verror(td, errno, "invalidate_cache");
Jens Axboee5b401d2006-10-18 16:03:40 +0200331 return 1;
Jens Axboe3baddf22008-04-04 11:10:30 +0200332 } else if (ret > 0) {
333 td_verror(td, ret, "invalidate_cache");
334 return 1;
Jens Axboee5b401d2006-10-18 16:03:40 +0200335 }
336
Jens Axboead2da602007-02-26 12:33:27 +0100337 return ret;
Jens Axboe3baddf22008-04-04 11:10:30 +0200338
339}
340
341int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
342{
Jens Axboed6aed792009-06-03 08:41:15 +0200343 if (!fio_file_open(f))
Jens Axboea5fb4612008-05-28 10:54:01 +0200344 return 0;
345
Jens Axboe5e0074c2009-06-02 21:40:09 +0200346 return __file_invalidate_cache(td, f, -1ULL, -1ULL);
Jens Axboee5b401d2006-10-18 16:03:40 +0200347}
348
Jens Axboe6977bcd2008-03-01 15:55:36 +0100349int generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
Jens Axboe53cdc682006-10-18 11:50:58 +0200350{
Jens Axboe6977bcd2008-03-01 15:55:36 +0100351 int ret = 0;
352
Jens Axboeee56ad52008-02-01 10:30:20 +0100353 dprint(FD_FILE, "fd close %s\n", f->file_name);
Jens Axboe4906e0b2008-03-01 18:59:51 +0100354
355 remove_file_hash(f);
356
Jens Axboe6977bcd2008-03-01 15:55:36 +0100357 if (close(f->fd) < 0)
358 ret = errno;
359
Jens Axboeb5af8292007-03-08 12:43:13 +0100360 f->fd = -1;
Jens Axboe6977bcd2008-03-01 15:55:36 +0100361 return ret;
Jens Axboe53cdc682006-10-18 11:50:58 +0200362}
363
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100364static int file_lookup_open(struct fio_file *f, int flags)
Jens Axboe53cdc682006-10-18 11:50:58 +0200365{
Jens Axboe29c13492008-03-01 19:25:20 +0100366 struct fio_file *__f;
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100367 int from_hash;
368
369 __f = lookup_file_hash(f->file_name);
370 if (__f) {
Jens Axboe9efef3c2008-03-06 10:26:25 +0100371 dprint(FD_FILE, "found file in hash %s\n", f->file_name);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100372 /*
373 * racy, need the __f->lock locked
374 */
375 f->lock = __f->lock;
376 f->lock_owner = __f->lock_owner;
377 f->lock_batch = __f->lock_batch;
378 f->lock_ddir = __f->lock_ddir;
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100379 from_hash = 1;
380 } else {
Jens Axboe9efef3c2008-03-06 10:26:25 +0100381 dprint(FD_FILE, "file not found in hash %s\n", f->file_name);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100382 from_hash = 0;
383 }
384
Jens Axboee8670ef2008-03-06 12:06:30 +0100385 f->fd = open(f->file_name, flags, 0600);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100386 return from_hash;
387}
388
389int generic_open_file(struct thread_data *td, struct fio_file *f)
390{
Jens Axboe66159822007-04-16 21:54:24 +0200391 int is_std = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200392 int flags = 0;
Jens Axboe29c13492008-03-01 19:25:20 +0100393 int from_hash = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200394
Jens Axboeee56ad52008-02-01 10:30:20 +0100395 dprint(FD_FILE, "fd open %s\n", f->file_name);
396
Jens Axboe66159822007-04-16 21:54:24 +0200397 if (!strcmp(f->file_name, "-")) {
398 if (td_rw(td)) {
399 log_err("fio: can't read/write to stdin/out\n");
400 return 1;
401 }
402 is_std = 1;
Jens Axboece98fa62007-04-17 13:27:32 +0200403
404 /*
405 * move output logging to stderr, if we are writing to stdout
406 */
407 if (td_write(td))
408 f_out = stderr;
Jens Axboe66159822007-04-16 21:54:24 +0200409 }
410
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100411 if (td->o.odirect)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100412 flags |= OS_O_DIRECT;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100413 if (td->o.sync_io)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100414 flags |= O_SYNC;
Jens Axboead923962007-10-11 21:41:41 +0200415 if (f->filetype != FIO_TYPE_FILE)
Jens Axboe5921e802008-05-30 15:02:38 +0200416 flags |= FIO_O_NOATIME;
Jens Axboe814452b2009-03-04 12:53:13 +0100417 if (td->o.create_on_open)
418 flags |= O_CREAT;
Jens Axboe7abf8332006-11-23 15:01:19 +0100419
Aaron Carroll056f3452007-11-26 09:08:53 +0100420open_again:
Jens Axboe660a1cb2007-04-17 15:37:47 +0200421 if (td_write(td)) {
Jens Axboe17308152008-03-07 13:42:31 +0100422 if (!read_only)
423 flags |= O_RDWR;
Jens Axboe53cdc682006-10-18 11:50:58 +0200424
Jens Axboeaf52b342007-03-13 10:07:47 +0100425 if (f->filetype == FIO_TYPE_FILE)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100426 flags |= O_CREAT;
Jens Axboe2fd233b2007-03-02 08:44:25 +0100427
Jens Axboe66159822007-04-16 21:54:24 +0200428 if (is_std)
429 f->fd = dup(STDOUT_FILENO);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100430 else
431 from_hash = file_lookup_open(f, flags);
Jens Axboe2fd233b2007-03-02 08:44:25 +0100432 } else {
Jens Axboe4241ea82007-09-12 08:18:36 +0200433 if (f->filetype == FIO_TYPE_CHAR && !read_only)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100434 flags |= O_RDWR;
435 else
436 flags |= O_RDONLY;
437
Jens Axboe66159822007-04-16 21:54:24 +0200438 if (is_std)
439 f->fd = dup(STDIN_FILENO);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100440 else
441 from_hash = file_lookup_open(f, flags);
Jens Axboe53cdc682006-10-18 11:50:58 +0200442 }
443
444 if (f->fd == -1) {
Jens Axboee4e33252007-03-21 13:07:54 +0100445 char buf[FIO_VERROR_SIZE];
Jens Axboee1161c32007-02-22 19:36:48 +0100446 int __e = errno;
447
Jens Axboe835d9b92009-06-09 15:23:38 +0200448 if (__e == EPERM && (flags & FIO_O_NOATIME)) {
Jens Axboe5921e802008-05-30 15:02:38 +0200449 flags &= ~FIO_O_NOATIME;
Aaron Carroll056f3452007-11-26 09:08:53 +0100450 goto open_again;
451 }
452
Jens Axboee8670ef2008-03-06 12:06:30 +0100453 snprintf(buf, sizeof(buf) - 1, "open(%s)", f->file_name);
Jens Axboee4e33252007-03-21 13:07:54 +0100454
455 td_verror(td, __e, buf);
Jens Axboe53cdc682006-10-18 11:50:58 +0200456 }
457
Jens Axboe29c13492008-03-01 19:25:20 +0100458 if (!from_hash && f->fd != -1) {
459 if (add_file_hash(f)) {
460 int ret;
461
462 /*
463 * OK to ignore, we haven't done anything with it
464 */
465 ret = generic_close_file(td, f);
466 goto open_again;
467 }
468 }
Jens Axboe4906e0b2008-03-01 18:59:51 +0100469
Jens Axboe53cdc682006-10-18 11:50:58 +0200470 return 0;
Jens Axboeb5af8292007-03-08 12:43:13 +0100471}
472
Jens Axboedf9c26b2009-03-05 10:13:58 +0100473int generic_get_file_size(struct thread_data *td, struct fio_file *f)
Jens Axboe21972cd2006-11-23 12:39:16 +0100474{
Jens Axboedf9c26b2009-03-05 10:13:58 +0100475 return get_file_size(td, f);
Jens Axboe21972cd2006-11-23 12:39:16 +0100476}
477
Jens Axboe7bb48f82007-03-27 15:30:28 +0200478/*
479 * open/close all files, so that ->real_file_size gets set
480 */
Jens Axboebab3fd52007-04-02 10:34:18 +0200481static int get_file_sizes(struct thread_data *td)
Jens Axboe53cdc682006-10-18 11:50:58 +0200482{
483 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100484 unsigned int i;
Jens Axboebab3fd52007-04-02 10:34:18 +0200485 int err = 0;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200486
487 for_each_file(td, f, i) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100488 dprint(FD_FILE, "get file size for %p/%d/%p\n", f, i,
489 f->file_name);
Jens Axboe9efef3c2008-03-06 10:26:25 +0100490
Jens Axboe99a47c62009-04-07 22:20:56 +0200491 if (td_io_get_file_size(td, f)) {
Jens Axboe40b44f42007-04-11 12:43:35 +0200492 if (td->error != ENOENT) {
493 log_err("%s\n", td->verror);
494 err = 1;
495 }
Jens Axboe541d66d2007-03-27 19:50:11 +0200496 clear_error(td);
Jens Axboe07eb79d2007-04-12 13:02:38 +0200497 }
Jens Axboe409b3412007-03-28 09:57:01 +0200498
499 if (f->real_file_size == -1ULL && td->o.size)
500 f->real_file_size = td->o.size / td->o.nr_files;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200501 }
Jens Axboebab3fd52007-04-02 10:34:18 +0200502
503 return err;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200504}
505
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200506struct fio_mount {
507 struct flist_head list;
508 const char *base;
509 char __base[256];
510 unsigned int key;
511};
512
513/*
514 * Get free number of bytes for each file on each unique mount.
515 */
516static unsigned long long get_fs_free_counts(struct thread_data *td)
517{
518 struct flist_head *n, *tmp;
519 unsigned long long ret;
520 struct fio_mount *fm;
521 FLIST_HEAD(list);
522 struct fio_file *f;
523 unsigned int i;
524
525 for_each_file(td, f, i) {
526 struct stat sb;
527 char buf[256];
528
529 strcpy(buf, f->file_name);
530
531 if (stat(buf, &sb) < 0) {
532 if (errno != ENOENT)
533 break;
534 strcpy(buf, ".");
535 if (stat(buf, &sb) < 0)
536 break;
537 }
538
539 fm = NULL;
540 flist_for_each(n, &list) {
541 fm = flist_entry(n, struct fio_mount, list);
542 if (fm->key == sb.st_dev)
543 break;
544
545 fm = NULL;
546 }
547
548 if (fm)
549 continue;
550
551 fm = malloc(sizeof(*fm));
552 strcpy(fm->__base, buf);
553 fm->base = basename(fm->__base);
554 fm->key = sb.st_dev;
555 flist_add(&fm->list, &list);
556 }
557
558 ret = 0;
559 flist_for_each_safe(n, tmp, &list) {
560 unsigned long long sz;
561
562 fm = flist_entry(n, struct fio_mount, list);
563 flist_del(&fm->list);
564
565 sz = get_fs_size(fm->base);
566 if (sz && sz != -1ULL)
567 ret += sz;
568
569 free(fm);
570 }
571
572 return ret;
573}
574
Jens Axboe7bb48f82007-03-27 15:30:28 +0200575/*
576 * Open the files and setup files sizes, creating files if necessary.
577 */
578int setup_files(struct thread_data *td)
579{
580 unsigned long long total_size, extend_size;
581 struct fio_file *f;
582 unsigned int i;
Jens Axboe000b0802007-03-27 15:56:10 +0200583 int err = 0, need_extend;
Jens Axboe53cdc682006-10-18 11:50:58 +0200584
Jens Axboeee56ad52008-02-01 10:30:20 +0100585 dprint(FD_FILE, "setup files\n");
586
Jens Axboe691c8fb2008-03-07 14:26:26 +0100587 if (td->o.read_iolog_file)
588 return 0;
589
Jens Axboe53cdc682006-10-18 11:50:58 +0200590 /*
591 * if ioengine defines a setup() method, it's responsible for
Jens Axboe7bb48f82007-03-27 15:30:28 +0200592 * opening the files and setting f->real_file_size to indicate
593 * the valid range for that file.
Jens Axboe53cdc682006-10-18 11:50:58 +0200594 */
595 if (td->io_ops->setup)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200596 err = td->io_ops->setup(td);
597 else
Jens Axboebab3fd52007-04-02 10:34:18 +0200598 err = get_file_sizes(td);
Jens Axboe53cdc682006-10-18 11:50:58 +0200599
Jens Axboef1027062006-10-20 10:14:01 +0200600 if (err)
601 return err;
602
Jens Axboe0a7eb122006-10-20 10:36:42 +0200603 /*
Jens Axboe7bb48f82007-03-27 15:30:28 +0200604 * check sizes. if the files/devices do not exist and the size
605 * isn't passed to fio, abort.
Jens Axboe0a7eb122006-10-20 10:36:42 +0200606 */
Jens Axboe7bb48f82007-03-27 15:30:28 +0200607 total_size = 0;
608 for_each_file(td, f, i) {
609 if (f->real_file_size == -1ULL)
610 total_size = -1ULL;
611 else
612 total_size += f->real_file_size;
613 }
Jens Axboe0a7eb122006-10-20 10:36:42 +0200614
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200615 if (td->o.fill_device)
616 td->fill_device_size = get_fs_free_counts(td);
617
Jens Axboe7bb48f82007-03-27 15:30:28 +0200618 /*
619 * device/file sizes are zero and no size given, punt
620 */
Jens Axboe1f809d12007-10-25 18:34:02 +0200621 if ((!total_size || total_size == -1ULL) && !td->o.size &&
Shawn Lewisaa31f1f2008-01-11 09:45:11 +0100622 !(td->io_ops->flags & FIO_NOIO) && !td->o.fill_device) {
Jens Axboe7bb48f82007-03-27 15:30:28 +0200623 log_err("%s: you need to specify size=\n", td->o.name);
Jens Axboee1161c32007-02-22 19:36:48 +0100624 td_verror(td, EINVAL, "total_file_size");
Jens Axboe53cdc682006-10-18 11:50:58 +0200625 return 1;
626 }
627
Jens Axboe7bb48f82007-03-27 15:30:28 +0200628 /*
629 * now file sizes are known, so we can set ->io_size. if size= is
630 * not given, ->io_size is just equal to ->real_file_size. if size
631 * is given, ->io_size is size / nr_files.
632 */
633 extend_size = total_size = 0;
634 need_extend = 0;
635 for_each_file(td, f, i) {
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200636 f->file_offset = td->o.start_offset;
637
Jens Axboe7bb48f82007-03-27 15:30:28 +0200638 if (!td->o.file_size_low) {
639 /*
640 * no file size range given, file size is equal to
641 * total size divided by number of files. if that is
642 * zero, set it to the real file size.
643 */
644 f->io_size = td->o.size / td->o.nr_files;
Jens Axboe65bdb102008-01-24 13:13:12 +0100645 if (!f->io_size)
Jens Axboe273f8c92008-01-25 14:02:15 +0100646 f->io_size = f->real_file_size - f->file_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200647 } else if (f->real_file_size < td->o.file_size_low ||
648 f->real_file_size > td->o.file_size_high) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100649 if (f->file_offset > td->o.file_size_low)
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200650 goto err_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200651 /*
652 * file size given. if it's fixed, use that. if it's a
653 * range, generate a random size in-between.
654 */
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100655 if (td->o.file_size_low == td->o.file_size_high) {
656 f->io_size = td->o.file_size_low
657 - f->file_offset;
658 } else {
659 f->io_size = get_rand_file_size(td)
660 - f->file_offset;
661 }
Jens Axboe65bdb102008-01-24 13:13:12 +0100662 } else
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200663 f->io_size = f->real_file_size - f->file_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200664
665 if (f->io_size == -1ULL)
666 total_size = -1ULL;
667 else
668 total_size += f->io_size;
669
670 if (f->filetype == FIO_TYPE_FILE &&
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200671 (f->io_size + f->file_offset) > f->real_file_size &&
Jens Axboe7bb48f82007-03-27 15:30:28 +0200672 !(td->io_ops->flags & FIO_DISKLESSIO)) {
Jens Axboe814452b2009-03-04 12:53:13 +0100673 if (!td->o.create_on_open) {
674 need_extend++;
675 extend_size += (f->io_size + f->file_offset);
676 } else
677 f->real_file_size = f->io_size + f->file_offset;
Jens Axboed6aed792009-06-03 08:41:15 +0200678 fio_file_set_extend(f);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100679 }
Jens Axboe7bb48f82007-03-27 15:30:28 +0200680 }
681
ljzhang,Yaxin Hu,Jianchao Tang22982902007-07-27 13:21:28 +0200682 if (!td->o.size || td->o.size > total_size)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200683 td->o.size = total_size;
684
685 /*
686 * See if we need to extend some files
687 */
688 if (need_extend) {
689 temp_stall_ts = 1;
Shaozhi Shawn Yea7ba8c52008-09-10 09:04:18 +0200690 if (!terse_output)
691 log_info("%s: Laying out IO file(s) (%u file(s) /"
Jens Axboeb22989b2009-07-17 22:29:23 +0200692 " %LuMB)\n", td->o.name, need_extend,
Shaozhi Shawn Yea7ba8c52008-09-10 09:04:18 +0200693 extend_size >> 20);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200694
695 for_each_file(td, f, i) {
Jens Axboe5e0074c2009-06-02 21:40:09 +0200696 unsigned long long old_len = -1ULL, extend_len = -1ULL;
Jens Axboe3baddf22008-04-04 11:10:30 +0200697
Jens Axboed6aed792009-06-03 08:41:15 +0200698 if (!fio_file_extend(f))
Jens Axboe7bb48f82007-03-27 15:30:28 +0200699 continue;
700
Jens Axboe409b3412007-03-28 09:57:01 +0200701 assert(f->filetype == FIO_TYPE_FILE);
Jens Axboed6aed792009-06-03 08:41:15 +0200702 fio_file_clear_extend(f);
Jens Axboe5e0074c2009-06-02 21:40:09 +0200703 if (!td->o.fill_device) {
704 old_len = f->real_file_size;
Jens Axboe0b9d69e2009-09-11 22:29:54 +0200705 extend_len = f->io_size + f->file_offset -
706 old_len;
Jens Axboe5e0074c2009-06-02 21:40:09 +0200707 }
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200708 f->real_file_size = (f->io_size + f->file_offset);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200709 err = extend_file(td, f);
710 if (err)
711 break;
Jens Axboe5e0074c2009-06-02 21:40:09 +0200712
Jens Axboe3baddf22008-04-04 11:10:30 +0200713 err = __file_invalidate_cache(td, f, old_len,
714 extend_len);
715 close(f->fd);
716 f->fd = -1;
717 if (err)
718 break;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200719 }
720 temp_stall_ts = 0;
721 }
722
723 if (err)
724 return err;
725
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100726 if (!td->o.zone_size)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200727 td->o.zone_size = td->o.size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200728
Jens Axboeea966f82007-09-03 10:09:37 +0200729 /*
730 * iolog already set the total io size, if we read back
731 * stored entries.
732 */
733 if (!td->o.read_iolog_file)
734 td->total_io_size = td->o.size * td->o.loops;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200735 return 0;
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200736err_offset:
737 log_err("%s: you need to specify valid offset=\n", td->o.name);
738 return 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200739}
740
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200741int pre_read_files(struct thread_data *td)
742{
743 struct fio_file *f;
744 unsigned int i;
745
746 dprint(FD_FILE, "pre_read files\n");
747
748 for_each_file(td, f, i) {
749 pre_read_file(td, f);
750 }
751
752 return 1;
753}
754
Jens Axboe68727072007-03-15 20:49:25 +0100755int init_random_map(struct thread_data *td)
756{
Jens Axboe509eab12007-12-14 12:42:53 +0100757 unsigned long long blocks, num_maps;
Jens Axboe68727072007-03-15 20:49:25 +0100758 struct fio_file *f;
759 unsigned int i;
760
Jens Axboede8dd112008-03-01 15:34:44 +0100761 if (td->o.norandommap || !td_random(td))
Jens Axboe68727072007-03-15 20:49:25 +0100762 return 0;
763
764 for_each_file(td, f, i) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100765 blocks = (f->real_file_size + td->o.rw_min_bs - 1) /
766 (unsigned long long) td->o.rw_min_bs;
767 num_maps = (blocks + BLOCKS_PER_MAP - 1) /
768 (unsigned long long) BLOCKS_PER_MAP;
Jens Axboede605662008-05-31 00:21:12 +0200769 f->file_map = smalloc(num_maps * sizeof(int));
Jens Axboe303032a2008-03-26 10:11:10 +0100770 if (f->file_map) {
771 f->num_maps = num_maps;
772 continue;
Jens Axboe68727072007-03-15 20:49:25 +0100773 }
Jens Axboe2b386d22008-03-26 10:32:57 +0100774 if (!td->o.softrandommap) {
Jens Axboe68727072007-03-15 20:49:25 +0100775 log_err("fio: failed allocating random map. If running"
776 " a large number of jobs, try the 'norandommap'"
Jens Axboe2b386d22008-03-26 10:32:57 +0100777 " option or set 'softrandommap'. Or give"
778 " a larger --alloc-size to fio.\n");
Jens Axboe68727072007-03-15 20:49:25 +0100779 return 1;
780 }
Jens Axboe303032a2008-03-26 10:11:10 +0100781
782 log_info("fio: file %s failed allocating random map. Running "
783 "job without.\n", f->file_name);
784 f->num_maps = 0;
Jens Axboe68727072007-03-15 20:49:25 +0100785 }
786
787 return 0;
788}
789
Jens Axboe53cdc682006-10-18 11:50:58 +0200790void close_files(struct thread_data *td)
791{
Jens Axboe0ab8db82006-10-18 17:16:23 +0200792 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100793 unsigned int i;
Jens Axboe53cdc682006-10-18 11:50:58 +0200794
Jens Axboe2be3eec2009-06-10 09:05:13 +0200795 for_each_file(td, f, i) {
796 if (fio_file_open(f))
797 td_io_close_file(td, f);
798 }
Jens Axboe24ffd2c2008-03-01 15:47:08 +0100799}
800
801void close_and_free_files(struct thread_data *td)
802{
803 struct fio_file *f;
804 unsigned int i;
805
Jens Axboeee56ad52008-02-01 10:30:20 +0100806 dprint(FD_FILE, "close files\n");
807
Jens Axboe0ab8db82006-10-18 17:16:23 +0200808 for_each_file(td, f, i) {
Jens Axboeffdfabd2008-03-26 09:18:14 +0100809 if (td->o.unlink && f->filetype == FIO_TYPE_FILE) {
810 dprint(FD_FILE, "free unlink %s\n", f->file_name);
Jens Axboe132ad462006-11-23 12:23:12 +0100811 unlink(f->file_name);
Jens Axboeffdfabd2008-03-26 09:18:14 +0100812 }
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100813
Jens Axboe22a57ba2009-06-09 14:34:35 +0200814 if (fio_file_open(f))
815 td_io_close_file(td, f);
816
Shaozhi Shawn Yeb9fbcf22008-09-10 09:05:17 +0200817 remove_file_hash(f);
Jens Axboeb3dc7f02007-03-09 12:52:15 +0100818
Jens Axboef17c4392008-03-01 18:40:46 +0100819 sfree(f->file_name);
Jens Axboefa1da862007-03-23 15:20:54 +0100820 f->file_name = NULL;
Jens Axboe61eb3132010-03-31 20:05:59 +0200821 sfree(f->file_map);
822 f->file_map = NULL;
Jens Axboe78d99e62008-03-01 18:41:50 +0100823 sfree(f);
Jens Axboe53cdc682006-10-18 11:50:58 +0200824 }
Jens Axboeb4a6a592006-10-20 13:54:47 +0200825
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100826 td->o.filename = NULL;
Jens Axboecade3ef2007-03-23 15:29:45 +0100827 free(td->files);
Jens Axboe9efef3c2008-03-06 10:26:25 +0100828 td->files_index = 0;
Jens Axboeb4a6a592006-10-20 13:54:47 +0200829 td->files = NULL;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100830 td->o.nr_files = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200831}
Jens Axboeaf52b342007-03-13 10:07:47 +0100832
Jens Axboee3bab462007-03-13 11:22:09 +0100833static void get_file_type(struct fio_file *f)
Jens Axboeaf52b342007-03-13 10:07:47 +0100834{
835 struct stat sb;
836
Jens Axboe66159822007-04-16 21:54:24 +0200837 if (!strcmp(f->file_name, "-"))
838 f->filetype = FIO_TYPE_PIPE;
839 else
840 f->filetype = FIO_TYPE_FILE;
Jens Axboeaf52b342007-03-13 10:07:47 +0100841
Jens Axboeb30d3952010-02-06 23:36:26 +0100842 if (!stat(f->file_name, &sb)) {
Jens Axboeaf52b342007-03-13 10:07:47 +0100843 if (S_ISBLK(sb.st_mode))
844 f->filetype = FIO_TYPE_BD;
845 else if (S_ISCHR(sb.st_mode))
846 f->filetype = FIO_TYPE_CHAR;
Jens Axboeb5605e92007-04-16 20:42:33 +0200847 else if (S_ISFIFO(sb.st_mode))
848 f->filetype = FIO_TYPE_PIPE;
Jens Axboeaf52b342007-03-13 10:07:47 +0100849 }
850}
851
Jens Axboef29b25a2007-07-23 08:56:43 +0200852int add_file(struct thread_data *td, const char *fname)
Jens Axboeaf52b342007-03-13 10:07:47 +0100853{
Jens Axboe7b4e4fe2007-03-13 12:51:40 +0100854 int cur_files = td->files_index;
Jens Axboebd0ee742007-03-23 14:26:23 +0100855 char file_name[PATH_MAX];
Jens Axboeaf52b342007-03-13 10:07:47 +0100856 struct fio_file *f;
Jens Axboebd0ee742007-03-23 14:26:23 +0100857 int len = 0;
Jens Axboeaf52b342007-03-13 10:07:47 +0100858
Jens Axboeee56ad52008-02-01 10:30:20 +0100859 dprint(FD_FILE, "add file %s\n", fname);
860
Jens Axboef17c4392008-03-01 18:40:46 +0100861 f = smalloc(sizeof(*f));
Jens Axboec48c0be2008-07-31 19:55:02 +0200862 if (!f) {
863 log_err("fio: smalloc OOM\n");
864 assert(0);
865 }
Jens Axboe0b9d69e2009-09-11 22:29:54 +0200866
Jens Axboeaf52b342007-03-13 10:07:47 +0100867 f->fd = -1;
Jens Axboebd0ee742007-03-23 14:26:23 +0100868
Jens Axboefc99bc02009-03-04 15:27:42 +0100869 if (td->files_size <= td->files_index) {
Carl Henrik Lunde4b341fc2009-04-20 08:41:37 +0200870 int new_size = td->o.nr_files + 1;
Jens Axboe126d65c2008-03-01 18:04:31 +0100871
Jens Axboefc99bc02009-03-04 15:27:42 +0100872 dprint(FD_FILE, "resize file array to %d files\n", new_size);
873
874 td->files = realloc(td->files, new_size * sizeof(f));
Jens Axboefc99bc02009-03-04 15:27:42 +0100875 td->files_size = new_size;
876 }
Jens Axboe8bb76792009-03-04 15:37:52 +0100877 td->files[cur_files] = f;
Jens Axboe126d65c2008-03-01 18:04:31 +0100878
Jens Axboe07eb79d2007-04-12 13:02:38 +0200879 /*
880 * init function, io engine may not be loaded yet
881 */
882 if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO))
883 f->real_file_size = -1ULL;
884
Jens Axboebd0ee742007-03-23 14:26:23 +0100885 if (td->o.directory)
886 len = sprintf(file_name, "%s/", td->o.directory);
887
888 sprintf(file_name + len, "%s", fname);
Jens Axboef17c4392008-03-01 18:40:46 +0100889 f->file_name = smalloc_strdup(file_name);
Jens Axboec48c0be2008-07-31 19:55:02 +0200890 if (!f->file_name) {
891 log_err("fio: smalloc OOM\n");
892 assert(0);
893 }
Jens Axboe0b9d69e2009-09-11 22:29:54 +0200894
Jens Axboee3bab462007-03-13 11:22:09 +0100895 get_file_type(f);
Jens Axboeaf52b342007-03-13 10:07:47 +0100896
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100897 switch (td->o.file_lock_mode) {
898 case FILE_LOCK_NONE:
899 break;
900 case FILE_LOCK_READWRITE:
901 f->lock = fio_mutex_rw_init();
902 break;
903 case FILE_LOCK_EXCLUSIVE:
904 f->lock = fio_mutex_init(1);
905 break;
906 default:
907 log_err("fio: unknown lock mode: %d\n", td->o.file_lock_mode);
908 assert(0);
909 }
Jens Axboe29c13492008-03-01 19:25:20 +0100910
Jens Axboe7b4e4fe2007-03-13 12:51:40 +0100911 td->files_index++;
Jens Axboe15494412007-03-13 11:52:47 +0100912 if (f->filetype == FIO_TYPE_FILE)
913 td->nr_normal_files++;
Jens Axboef29b25a2007-07-23 08:56:43 +0200914
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100915 dprint(FD_FILE, "file %p \"%s\" added at %d\n", f, f->file_name,
916 cur_files);
Jens Axboe9efef3c2008-03-06 10:26:25 +0100917
Jens Axboef29b25a2007-07-23 08:56:43 +0200918 return cur_files;
Jens Axboeaf52b342007-03-13 10:07:47 +0100919}
Jens Axboe0ad920e2007-03-13 11:06:45 +0100920
921void get_file(struct fio_file *f)
922{
Jens Axboe8172fe92008-02-01 21:51:02 +0100923 dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references);
Jens Axboed6aed792009-06-03 08:41:15 +0200924 assert(fio_file_open(f));
Jens Axboe0ad920e2007-03-13 11:06:45 +0100925 f->references++;
926}
927
Jens Axboe6977bcd2008-03-01 15:55:36 +0100928int put_file(struct thread_data *td, struct fio_file *f)
Jens Axboe0ad920e2007-03-13 11:06:45 +0100929{
Jens Axboe98e1ac42008-03-06 11:56:48 +0100930 int f_ret = 0, ret = 0;
Jens Axboe6977bcd2008-03-01 15:55:36 +0100931
Jens Axboe8172fe92008-02-01 21:51:02 +0100932 dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references);
Jens Axboeee56ad52008-02-01 10:30:20 +0100933
Jens Axboe22a57ba2009-06-09 14:34:35 +0200934 if (!fio_file_open(f)) {
935 assert(f->fd == -1);
Jens Axboe6977bcd2008-03-01 15:55:36 +0100936 return 0;
Jens Axboe22a57ba2009-06-09 14:34:35 +0200937 }
Jens Axboe0ad920e2007-03-13 11:06:45 +0100938
939 assert(f->references);
940 if (--f->references)
Jens Axboe6977bcd2008-03-01 15:55:36 +0100941 return 0;
Jens Axboe0ad920e2007-03-13 11:06:45 +0100942
Jens Axboed424d4d2007-04-17 09:05:10 +0200943 if (should_fsync(td) && td->o.fsync_on_close)
Jens Axboe98e1ac42008-03-06 11:56:48 +0100944 f_ret = fsync(f->fd);
Jens Axboeebb14152007-03-13 14:42:15 +0100945
Jens Axboe0ad920e2007-03-13 11:06:45 +0100946 if (td->io_ops->close_file)
Jens Axboe6977bcd2008-03-01 15:55:36 +0100947 ret = td->io_ops->close_file(td, f);
Jens Axboe1020a132007-03-29 11:15:06 +0200948
Jens Axboe98e1ac42008-03-06 11:56:48 +0100949 if (!ret)
Jens Axboea5fb4612008-05-28 10:54:01 +0200950 ret = f_ret;
Jens Axboe98e1ac42008-03-06 11:56:48 +0100951
Jens Axboe0ad920e2007-03-13 11:06:45 +0100952 td->nr_open_files--;
Jens Axboed6aed792009-06-03 08:41:15 +0200953 fio_file_clear_open(f);
Jens Axboe22a57ba2009-06-09 14:34:35 +0200954 assert(f->fd == -1);
Jens Axboe6977bcd2008-03-01 15:55:36 +0100955 return ret;
Jens Axboe0ad920e2007-03-13 11:06:45 +0100956}
Jens Axboebbf6b542007-03-13 15:28:55 +0100957
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100958void lock_file(struct thread_data *td, struct fio_file *f, enum fio_ddir ddir)
Jens Axboeb2bd2bd2008-03-01 18:19:52 +0100959{
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100960 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
961 return;
Jens Axboe29c13492008-03-01 19:25:20 +0100962
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100963 if (f->lock_owner == td && f->lock_batch--)
964 return;
965
966 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
967 if (ddir == DDIR_READ)
968 fio_mutex_down_read(f->lock);
969 else
970 fio_mutex_down_write(f->lock);
971 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
972 fio_mutex_down(f->lock);
973
974 f->lock_owner = td;
975 f->lock_batch = td->o.lockfile_batch;
976 f->lock_ddir = ddir;
977}
978
979void unlock_file(struct thread_data *td, struct fio_file *f)
980{
981 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
982 return;
983 if (f->lock_batch)
984 return;
985
986 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
987 const int is_read = f->lock_ddir == DDIR_READ;
988 int val = fio_mutex_getval(f->lock);
989
990 if ((is_read && val == 1) || (!is_read && val == -1))
991 f->lock_owner = NULL;
992
993 if (is_read)
994 fio_mutex_up_read(f->lock);
995 else
996 fio_mutex_up_write(f->lock);
997 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE) {
998 int val = fio_mutex_getval(f->lock);
999
1000 if (val == 0)
1001 f->lock_owner = NULL;
1002
1003 fio_mutex_up(f->lock);
Jens Axboe29c13492008-03-01 19:25:20 +01001004 }
Jens Axboeb2bd2bd2008-03-01 18:19:52 +01001005}
1006
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001007void unlock_file_all(struct thread_data *td, struct fio_file *f)
Jens Axboeb2bd2bd2008-03-01 18:19:52 +01001008{
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001009 if (f->lock_owner != td)
1010 return;
Jens Axboe29c13492008-03-01 19:25:20 +01001011
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001012 f->lock_batch = 0;
1013 unlock_file(td, f);
Jens Axboeb2bd2bd2008-03-01 18:19:52 +01001014}
1015
Jens Axboebbf6b542007-03-13 15:28:55 +01001016static int recurse_dir(struct thread_data *td, const char *dirname)
1017{
1018 struct dirent *dir;
1019 int ret = 0;
1020 DIR *D;
1021
1022 D = opendir(dirname);
1023 if (!D) {
Jens Axboe0ddb2702007-03-27 19:43:53 +02001024 char buf[FIO_VERROR_SIZE];
1025
1026 snprintf(buf, FIO_VERROR_SIZE - 1, "opendir(%s)", dirname);
1027 td_verror(td, errno, buf);
Jens Axboebbf6b542007-03-13 15:28:55 +01001028 return 1;
1029 }
1030
1031 while ((dir = readdir(D)) != NULL) {
1032 char full_path[PATH_MAX];
1033 struct stat sb;
1034
Jens Axboee85b2b82007-03-14 09:39:06 +01001035 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
1036 continue;
Jens Axboe96d32d52007-03-14 09:16:09 +01001037
Jens Axboebbf6b542007-03-13 15:28:55 +01001038 sprintf(full_path, "%s/%s", dirname, dir->d_name);
1039
1040 if (lstat(full_path, &sb) == -1) {
1041 if (errno != ENOENT) {
1042 td_verror(td, errno, "stat");
1043 return 1;
1044 }
1045 }
1046
1047 if (S_ISREG(sb.st_mode)) {
1048 add_file(td, full_path);
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001049 td->o.nr_files++;
Jens Axboebbf6b542007-03-13 15:28:55 +01001050 continue;
1051 }
Jens Axboe0ddb2702007-03-27 19:43:53 +02001052 if (!S_ISDIR(sb.st_mode))
1053 continue;
Jens Axboebbf6b542007-03-13 15:28:55 +01001054
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001055 ret = recurse_dir(td, full_path);
1056 if (ret)
Jens Axboebbf6b542007-03-13 15:28:55 +01001057 break;
1058 }
1059
1060 closedir(D);
1061 return ret;
1062}
1063
1064int add_dir_files(struct thread_data *td, const char *path)
1065{
Jens Axboe0ddb2702007-03-27 19:43:53 +02001066 int ret = recurse_dir(td, path);
1067
1068 if (!ret)
1069 log_info("fio: opendir added %d files\n", td->o.nr_files);
1070
1071 return ret;
Jens Axboebbf6b542007-03-13 15:28:55 +01001072}
Jens Axboecade3ef2007-03-23 15:29:45 +01001073
1074void dup_files(struct thread_data *td, struct thread_data *org)
1075{
1076 struct fio_file *f;
1077 unsigned int i;
Jens Axboe9efef3c2008-03-06 10:26:25 +01001078
1079 dprint(FD_FILE, "dup files: %d\n", org->files_index);
Jens Axboecade3ef2007-03-23 15:29:45 +01001080
1081 if (!org->files)
1082 return;
1083
Jens Axboe9efef3c2008-03-06 10:26:25 +01001084 td->files = malloc(org->files_index * sizeof(f));
Jens Axboecade3ef2007-03-23 15:29:45 +01001085
Jens Axboe9efef3c2008-03-06 10:26:25 +01001086 for_each_file(org, f, i) {
Jens Axboeb0fe4212008-03-01 18:22:27 +01001087 struct fio_file *__f;
1088
Jens Axboef17c4392008-03-01 18:40:46 +01001089 __f = smalloc(sizeof(*__f));
Jens Axboec48c0be2008-07-31 19:55:02 +02001090 if (!__f) {
1091 log_err("fio: smalloc OOM\n");
1092 assert(0);
1093 }
Jens Axboe22a57ba2009-06-09 14:34:35 +02001094 __f->fd = -1;
Jens Axboe0b9d69e2009-09-11 22:29:54 +02001095
Aaron Carrollbc3456f2008-06-25 09:20:37 +02001096 if (f->file_name) {
Jens Axboef17c4392008-03-01 18:40:46 +01001097 __f->file_name = smalloc_strdup(f->file_name);
Jens Axboec48c0be2008-07-31 19:55:02 +02001098 if (!__f->file_name) {
1099 log_err("fio: smalloc OOM\n");
1100 assert(0);
1101 }
Jens Axboe0b9d69e2009-09-11 22:29:54 +02001102
Aaron Carrollbc3456f2008-06-25 09:20:37 +02001103 __f->filetype = f->filetype;
1104 }
Jens Axboeb0fe4212008-03-01 18:22:27 +01001105
1106 td->files[i] = __f;
Jens Axboecade3ef2007-03-23 15:29:45 +01001107 }
1108}
Jens Axboef29b25a2007-07-23 08:56:43 +02001109
1110/*
1111 * Returns the index that matches the filename, or -1 if not there
1112 */
1113int get_fileno(struct thread_data *td, const char *fname)
1114{
1115 struct fio_file *f;
1116 unsigned int i;
1117
1118 for_each_file(td, f, i)
1119 if (!strcmp(f->file_name, fname))
1120 return i;
1121
1122 return -1;
1123}
1124
1125/*
1126 * For log usage, where we add/open/close files automatically
1127 */
1128void free_release_files(struct thread_data *td)
1129{
1130 close_files(td);
1131 td->files_index = 0;
1132 td->nr_normal_files = 0;
1133}