blob: 9cc487269fcc3434e8677d9ac74905a4bd698ca1 [file] [log] [blame]
Jens Axboe53cdc682006-10-18 11:50:58 +02001#include <unistd.h>
2#include <fcntl.h>
3#include <string.h>
4#include <assert.h>
Jens Axboebbf6b542007-03-13 15:28:55 +01005#include <dirent.h>
YAMAMOTO Takashid74ac842010-06-09 09:44:53 +02006#include <libgen.h>
Jens Axboe53cdc682006-10-18 11:50:58 +02007#include <sys/stat.h>
8#include <sys/mman.h>
Jens Axboebbf6b542007-03-13 15:28:55 +01009#include <sys/types.h>
Jens Axboe53cdc682006-10-18 11:50:58 +020010
11#include "fio.h"
Jens Axboef17c4392008-03-01 18:40:46 +010012#include "smalloc.h"
Jens Axboe4906e0b2008-03-01 18:59:51 +010013#include "filehash.h"
Jens Axboe53cdc682006-10-18 11:50:58 +020014
Jens Axboe7172cfe2007-07-23 14:36:00 +020015static int root_warn;
16
Jens Axboec592b9f2009-06-03 09:29:28 +020017static inline void clear_error(struct thread_data *td)
18{
19 td->error = 0;
20 td->verror[0] = '\0';
21}
22
Jens Axboe3baddf22008-04-04 11:10:30 +020023/*
24 * Leaves f->fd open on success, caller must close
25 */
Jens Axboe7bb48f82007-03-27 15:30:28 +020026static int extend_file(struct thread_data *td, struct fio_file *f)
Jens Axboe25205e92006-10-19 20:50:14 +020027{
Jens Axboeea443652007-03-29 14:52:13 +020028 int r, new_layout = 0, unlink_file = 0, flags;
Jens Axboe25205e92006-10-19 20:50:14 +020029 unsigned long long left;
30 unsigned int bs;
31 char *b;
Jens Axboeb2a15192006-10-18 14:10:42 +020032
Jens Axboe4241ea82007-09-12 08:18:36 +020033 if (read_only) {
34 log_err("fio: refusing extend of file due to read-only\n");
35 return 0;
36 }
37
Jens Axboe507a7022007-03-27 15:52:46 +020038 /*
39 * check if we need to lay the file out complete again. fio
40 * does that for operations involving reads, or for writes
41 * where overwrite is set
42 */
Jens Axboeffdfabd2008-03-26 09:18:14 +010043 if (td_read(td) || (td_write(td) && td->o.overwrite) ||
44 (td_write(td) && td->io_ops->flags & FIO_NOEXTEND))
Jens Axboe507a7022007-03-27 15:52:46 +020045 new_layout = 1;
Jens Axboeea443652007-03-29 14:52:13 +020046 if (td_write(td) && !td->o.overwrite)
47 unlink_file = 1;
Jens Axboe507a7022007-03-27 15:52:46 +020048
Jens Axboe6ae1f572008-02-21 10:20:00 +010049 if (unlink_file || new_layout) {
Jens Axboebd199f22008-03-26 09:57:18 +010050 dprint(FD_FILE, "layout unlink %s\n", f->file_name);
Zhang, Yanmin982016d2008-02-26 15:35:52 +010051 if ((unlink(f->file_name) < 0) && (errno != ENOENT)) {
Jens Axboe7bb48f82007-03-27 15:30:28 +020052 td_verror(td, errno, "unlink");
53 return 1;
54 }
55 }
56
Jens Axboe507a7022007-03-27 15:52:46 +020057 flags = O_WRONLY | O_CREAT;
58 if (new_layout)
59 flags |= O_TRUNC;
60
Jens Axboeee56ad52008-02-01 10:30:20 +010061 dprint(FD_FILE, "open file %s, flags %x\n", f->file_name, flags);
Jens Axboe507a7022007-03-27 15:52:46 +020062 f->fd = open(f->file_name, flags, 0644);
Jens Axboe53cdc682006-10-18 11:50:58 +020063 if (f->fd < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +010064 td_verror(td, errno, "open");
Jens Axboe53cdc682006-10-18 11:50:58 +020065 return 1;
66 }
67
Jens Axboe7bc8c2c2010-01-28 11:31:31 +010068#ifdef FIO_HAVE_FALLOCATE
69 if (td->o.fallocate && !td->o.fill_device) {
70 dprint(FD_FILE, "fallocate file %s size %llu\n", f->file_name,
71 f->real_file_size);
72
73 r = posix_fallocate(f->fd, 0, f->real_file_size);
74 if (r < 0) {
75 log_err("fio: posix_fallocate fails: %s\n",
76 strerror(-r));
77 }
78 }
79#endif
80
Shawn Lewisfc74ac12008-01-11 09:45:11 +010081 if (!new_layout)
82 goto done;
83
Jens Axboe5e0074c2009-06-02 21:40:09 +020084 /*
85 * The size will be -1ULL when fill_device is used, so don't truncate
86 * or fallocate this file, just write it
87 */
88 if (!td->o.fill_device) {
89 dprint(FD_FILE, "truncate file %s, size %llu\n", f->file_name,
Jens Axboeee56ad52008-02-01 10:30:20 +010090 f->real_file_size);
Jens Axboe5e0074c2009-06-02 21:40:09 +020091 if (ftruncate(f->fd, f->real_file_size) == -1) {
92 td_verror(td, errno, "ftruncate");
93 goto err;
94 }
Jens Axboe5e0074c2009-06-02 21:40:09 +020095 }
Jens Axboe40f82982006-10-25 11:21:05 +020096
Jens Axboe2dc1bbe2007-03-15 15:01:33 +010097 b = malloc(td->o.max_bs[DDIR_WRITE]);
98 memset(b, 0, td->o.max_bs[DDIR_WRITE]);
Jens Axboe53cdc682006-10-18 11:50:58 +020099
Jens Axboe7bb48f82007-03-27 15:30:28 +0200100 left = f->real_file_size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200101 while (left && !td->terminate) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100102 bs = td->o.max_bs[DDIR_WRITE];
Jens Axboe53cdc682006-10-18 11:50:58 +0200103 if (bs > left)
104 bs = left;
105
106 r = write(f->fd, b, bs);
107
Jens Axboe5e0074c2009-06-02 21:40:09 +0200108 if (r > 0) {
109 left -= r;
Jens Axboe53cdc682006-10-18 11:50:58 +0200110 continue;
111 } else {
Jens Axboe5e0074c2009-06-02 21:40:09 +0200112 if (r < 0) {
113 int __e = errno;
114
115 if (__e == ENOSPC) {
116 if (td->o.fill_device)
117 break;
118 log_info("fio: ENOSPC on laying out "
119 "file, stopping\n");
120 break;
121 }
Jens Axboee1161c32007-02-22 19:36:48 +0100122 td_verror(td, errno, "write");
Jens Axboe5e0074c2009-06-02 21:40:09 +0200123 } else
Jens Axboee1161c32007-02-22 19:36:48 +0100124 td_verror(td, EIO, "write");
Jens Axboe53cdc682006-10-18 11:50:58 +0200125
126 break;
127 }
128 }
129
Jens Axboeffdfabd2008-03-26 09:18:14 +0100130 if (td->terminate) {
131 dprint(FD_FILE, "terminate unlink %s\n", f->file_name);
Jens Axboe53cdc682006-10-18 11:50:58 +0200132 unlink(f->file_name);
Jens Axboeffdfabd2008-03-26 09:18:14 +0100133 } else if (td->o.create_fsync) {
Jens Axboe98e1ac42008-03-06 11:56:48 +0100134 if (fsync(f->fd) < 0) {
135 td_verror(td, errno, "fsync");
136 goto err;
137 }
138 }
Jens Axboe0d1cd202009-06-05 22:11:52 +0200139 if (td->o.fill_device && !td_write(td)) {
Jens Axboed6aed792009-06-03 08:41:15 +0200140 fio_file_clear_size_known(f);
Jens Axboe5e0074c2009-06-02 21:40:09 +0200141 if (td_io_get_file_size(td, f))
142 goto err;
143 if (f->io_size > f->real_file_size)
144 f->io_size = f->real_file_size;
145 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200146
147 free(b);
Jens Axboe507a7022007-03-27 15:52:46 +0200148done:
Jens Axboe53cdc682006-10-18 11:50:58 +0200149 return 0;
150err:
151 close(f->fd);
152 f->fd = -1;
153 return 1;
154}
155
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200156static int pre_read_file(struct thread_data *td, struct fio_file *f)
157{
Jens Axboeb0f65862009-05-20 11:52:15 +0200158 int r, did_open = 0, old_runstate;
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200159 unsigned long long left;
160 unsigned int bs;
161 char *b;
162
Jens Axboe9c0d2242009-07-01 12:26:28 +0200163 if (td->io_ops->flags & FIO_PIPEIO)
164 return 0;
165
Jens Axboed6aed792009-06-03 08:41:15 +0200166 if (!fio_file_open(f)) {
Jens Axboeb0f65862009-05-20 11:52:15 +0200167 if (td->io_ops->open_file(td, f)) {
168 log_err("fio: cannot pre-read, failed to open file\n");
169 return 1;
170 }
171 did_open = 1;
172 }
173
174 old_runstate = td->runstate;
175 td_set_runstate(td, TD_PRE_READING);
176
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200177 bs = td->o.max_bs[DDIR_READ];
178 b = malloc(bs);
179 memset(b, 0, bs);
180
181 lseek(f->fd, f->file_offset, SEEK_SET);
182 left = f->io_size;
183
184 while (left && !td->terminate) {
185 if (bs > left)
186 bs = left;
187
188 r = read(f->fd, b, bs);
189
190 if (r == (int) bs) {
191 left -= bs;
192 continue;
193 } else {
194 td_verror(td, EIO, "pre_read");
195 break;
196 }
197 }
198
Jens Axboeb0f65862009-05-20 11:52:15 +0200199 td_set_runstate(td, old_runstate);
200
201 if (did_open)
202 td->io_ops->close_file(td, f);
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200203 free(b);
204 return 0;
205}
206
Jens Axboe7bb48f82007-03-27 15:30:28 +0200207static unsigned long long get_rand_file_size(struct thread_data *td)
Jens Axboe9c60ce62007-03-15 09:14:47 +0100208{
Jens Axboedc873b62008-06-04 20:13:04 +0200209 unsigned long long ret, sized;
Jens Axboe9c60ce62007-03-15 09:14:47 +0100210 long r;
211
Jens Axboe9c60ce62007-03-15 09:14:47 +0100212 r = os_random_long(&td->file_size_state);
Jens Axboedc873b62008-06-04 20:13:04 +0200213 sized = td->o.file_size_high - td->o.file_size_low;
214 ret = (unsigned long long) ((double) sized * (r / (OS_RAND_MAX + 1.0)));
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100215 ret += td->o.file_size_low;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100216 ret -= (ret % td->o.rw_min_bs);
Jens Axboe9c60ce62007-03-15 09:14:47 +0100217 return ret;
218}
219
Jens Axboe53cdc682006-10-18 11:50:58 +0200220static int file_size(struct thread_data *td, struct fio_file *f)
221{
222 struct stat st;
223
Jens Axboedf9c26b2009-03-05 10:13:58 +0100224 if (stat(f->file_name, &st) == -1) {
Jens Axboe7bb48f82007-03-27 15:30:28 +0200225 td_verror(td, errno, "fstat");
226 return 1;
227 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200228
Jens Axboe7bb48f82007-03-27 15:30:28 +0200229 f->real_file_size = st.st_size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200230 return 0;
231}
232
233static int bdev_size(struct thread_data *td, struct fio_file *f)
234{
235 unsigned long long bytes;
236 int r;
237
Jens Axboedf9c26b2009-03-05 10:13:58 +0100238 if (td->io_ops->open_file(td, f)) {
239 log_err("fio: failed opening blockdev %s for size check\n",
240 f->file_name);
241 return 1;
242 }
243
Jens Axboe53cdc682006-10-18 11:50:58 +0200244 r = blockdev_size(f->fd, &bytes);
245 if (r) {
Jens Axboee1161c32007-02-22 19:36:48 +0100246 td_verror(td, r, "blockdev_size");
Jens Axboedf9c26b2009-03-05 10:13:58 +0100247 goto err;
Jens Axboe53cdc682006-10-18 11:50:58 +0200248 }
249
Jens Axboe7ab077a2009-02-02 15:07:37 +0100250 if (!bytes) {
251 log_err("%s: zero sized block device?\n", f->file_name);
Jens Axboedf9c26b2009-03-05 10:13:58 +0100252 goto err;
Jens Axboe7ab077a2009-02-02 15:07:37 +0100253 }
254
Jens Axboe53cdc682006-10-18 11:50:58 +0200255 f->real_file_size = bytes;
Jens Axboe22a57ba2009-06-09 14:34:35 +0200256 td->io_ops->close_file(td, f);
Jens Axboe53cdc682006-10-18 11:50:58 +0200257 return 0;
Jens Axboedf9c26b2009-03-05 10:13:58 +0100258err:
259 td->io_ops->close_file(td, f);
260 return 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200261}
262
263static int get_file_size(struct thread_data *td, struct fio_file *f)
264{
265 int ret = 0;
266
Jens Axboed6aed792009-06-03 08:41:15 +0200267 if (fio_file_size_known(f))
Jens Axboe409b3412007-03-28 09:57:01 +0200268 return 0;
269
Jens Axboe7bb48f82007-03-27 15:30:28 +0200270 if (f->filetype == FIO_TYPE_FILE)
271 ret = file_size(td, f);
272 else if (f->filetype == FIO_TYPE_BD)
Jens Axboe53cdc682006-10-18 11:50:58 +0200273 ret = bdev_size(td, f);
274 else
275 f->real_file_size = -1;
276
277 if (ret)
278 return ret;
279
280 if (f->file_offset > f->real_file_size) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100281 log_err("%s: offset extends end (%Lu > %Lu)\n", td->o.name,
282 f->file_offset, f->real_file_size);
Jens Axboe53cdc682006-10-18 11:50:58 +0200283 return 1;
284 }
285
Jens Axboed6aed792009-06-03 08:41:15 +0200286 fio_file_set_size_known(f);
Jens Axboe53cdc682006-10-18 11:50:58 +0200287 return 0;
288}
289
Jens Axboe3baddf22008-04-04 11:10:30 +0200290static int __file_invalidate_cache(struct thread_data *td, struct fio_file *f,
291 unsigned long long off,
292 unsigned long long len)
Jens Axboee5b401d2006-10-18 16:03:40 +0200293{
294 int ret = 0;
295
Jens Axboe5e0074c2009-06-02 21:40:09 +0200296 if (len == -1ULL)
Jens Axboe3baddf22008-04-04 11:10:30 +0200297 len = f->io_size;
298 if (off == -1ULL)
299 off = f->file_offset;
Jens Axboeee56ad52008-02-01 10:30:20 +0100300
Jens Axboe0d1cd202009-06-05 22:11:52 +0200301 if (len == -1ULL || off == -1ULL)
302 return 0;
303
Jens Axboe3baddf22008-04-04 11:10:30 +0200304 dprint(FD_IO, "invalidate cache %s: %llu/%llu\n", f->file_name, off,
305 len);
Jens Axboeb5af8292007-03-08 12:43:13 +0100306
Jens Axboee5b401d2006-10-18 16:03:40 +0200307 /*
308 * FIXME: add blockdev flushing too
309 */
Jens Axboea1c58072009-08-04 23:17:02 +0200310 if (f->mmap_ptr) {
Jens Axboeac893112009-06-02 13:06:01 +0200311 ret = madvise(f->mmap_ptr, f->mmap_sz, MADV_DONTNEED);
Jens Axboea1c58072009-08-04 23:17:02 +0200312#ifdef FIO_MADV_FREE
313 (void) madvise(f->mmap_ptr, f->mmap_sz, FIO_MADV_FREE);
314#endif
315 } else if (f->filetype == FIO_TYPE_FILE) {
Jens Axboe3baddf22008-04-04 11:10:30 +0200316 ret = fadvise(f->fd, off, len, POSIX_FADV_DONTNEED);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100317 } else if (f->filetype == FIO_TYPE_BD) {
Jens Axboeb5af8292007-03-08 12:43:13 +0100318 ret = blockdev_invalidate_cache(f->fd);
Jens Axboe7e0e25c2007-03-27 12:49:56 +0200319 if (ret < 0 && errno == EACCES && geteuid()) {
Jens Axboe7172cfe2007-07-23 14:36:00 +0200320 if (!root_warn) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100321 log_err("fio: only root may flush block "
322 "devices. Cache flush bypassed!\n");
Jens Axboe7172cfe2007-07-23 14:36:00 +0200323 root_warn = 1;
324 }
Jens Axboe7e0e25c2007-03-27 12:49:56 +0200325 ret = 0;
326 }
Jens Axboeb5605e92007-04-16 20:42:33 +0200327 } else if (f->filetype == FIO_TYPE_CHAR || f->filetype == FIO_TYPE_PIPE)
Jens Axboee5b401d2006-10-18 16:03:40 +0200328 ret = 0;
329
330 if (ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100331 td_verror(td, errno, "invalidate_cache");
Jens Axboee5b401d2006-10-18 16:03:40 +0200332 return 1;
Jens Axboe3baddf22008-04-04 11:10:30 +0200333 } else if (ret > 0) {
334 td_verror(td, ret, "invalidate_cache");
335 return 1;
Jens Axboee5b401d2006-10-18 16:03:40 +0200336 }
337
Jens Axboead2da602007-02-26 12:33:27 +0100338 return ret;
Jens Axboe3baddf22008-04-04 11:10:30 +0200339
340}
341
342int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
343{
Jens Axboed6aed792009-06-03 08:41:15 +0200344 if (!fio_file_open(f))
Jens Axboea5fb4612008-05-28 10:54:01 +0200345 return 0;
346
Jens Axboe5e0074c2009-06-02 21:40:09 +0200347 return __file_invalidate_cache(td, f, -1ULL, -1ULL);
Jens Axboee5b401d2006-10-18 16:03:40 +0200348}
349
Jens Axboe6977bcd2008-03-01 15:55:36 +0100350int generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
Jens Axboe53cdc682006-10-18 11:50:58 +0200351{
Jens Axboe6977bcd2008-03-01 15:55:36 +0100352 int ret = 0;
353
Jens Axboeee56ad52008-02-01 10:30:20 +0100354 dprint(FD_FILE, "fd close %s\n", f->file_name);
Jens Axboe4906e0b2008-03-01 18:59:51 +0100355
356 remove_file_hash(f);
357
Jens Axboe6977bcd2008-03-01 15:55:36 +0100358 if (close(f->fd) < 0)
359 ret = errno;
360
Jens Axboeb5af8292007-03-08 12:43:13 +0100361 f->fd = -1;
Jens Axboe6977bcd2008-03-01 15:55:36 +0100362 return ret;
Jens Axboe53cdc682006-10-18 11:50:58 +0200363}
364
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100365static int file_lookup_open(struct fio_file *f, int flags)
Jens Axboe53cdc682006-10-18 11:50:58 +0200366{
Jens Axboe29c13492008-03-01 19:25:20 +0100367 struct fio_file *__f;
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100368 int from_hash;
369
370 __f = lookup_file_hash(f->file_name);
371 if (__f) {
Jens Axboe9efef3c2008-03-06 10:26:25 +0100372 dprint(FD_FILE, "found file in hash %s\n", f->file_name);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100373 /*
374 * racy, need the __f->lock locked
375 */
376 f->lock = __f->lock;
377 f->lock_owner = __f->lock_owner;
378 f->lock_batch = __f->lock_batch;
379 f->lock_ddir = __f->lock_ddir;
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100380 from_hash = 1;
381 } else {
Jens Axboe9efef3c2008-03-06 10:26:25 +0100382 dprint(FD_FILE, "file not found in hash %s\n", f->file_name);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100383 from_hash = 0;
384 }
385
Jens Axboee8670ef2008-03-06 12:06:30 +0100386 f->fd = open(f->file_name, flags, 0600);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100387 return from_hash;
388}
389
390int generic_open_file(struct thread_data *td, struct fio_file *f)
391{
Jens Axboe66159822007-04-16 21:54:24 +0200392 int is_std = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200393 int flags = 0;
Jens Axboe29c13492008-03-01 19:25:20 +0100394 int from_hash = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200395
Jens Axboeee56ad52008-02-01 10:30:20 +0100396 dprint(FD_FILE, "fd open %s\n", f->file_name);
397
Jens Axboe66159822007-04-16 21:54:24 +0200398 if (!strcmp(f->file_name, "-")) {
399 if (td_rw(td)) {
400 log_err("fio: can't read/write to stdin/out\n");
401 return 1;
402 }
403 is_std = 1;
Jens Axboece98fa62007-04-17 13:27:32 +0200404
405 /*
406 * move output logging to stderr, if we are writing to stdout
407 */
408 if (td_write(td))
409 f_out = stderr;
Jens Axboe66159822007-04-16 21:54:24 +0200410 }
411
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100412 if (td->o.odirect)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100413 flags |= OS_O_DIRECT;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100414 if (td->o.sync_io)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100415 flags |= O_SYNC;
Jens Axboead923962007-10-11 21:41:41 +0200416 if (f->filetype != FIO_TYPE_FILE)
Jens Axboe5921e802008-05-30 15:02:38 +0200417 flags |= FIO_O_NOATIME;
Jens Axboe814452b2009-03-04 12:53:13 +0100418 if (td->o.create_on_open)
419 flags |= O_CREAT;
Jens Axboe7abf8332006-11-23 15:01:19 +0100420
Aaron Carroll056f3452007-11-26 09:08:53 +0100421open_again:
Jens Axboe660a1cb2007-04-17 15:37:47 +0200422 if (td_write(td)) {
Jens Axboe17308152008-03-07 13:42:31 +0100423 if (!read_only)
424 flags |= O_RDWR;
Jens Axboe53cdc682006-10-18 11:50:58 +0200425
Jens Axboeaf52b342007-03-13 10:07:47 +0100426 if (f->filetype == FIO_TYPE_FILE)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100427 flags |= O_CREAT;
Jens Axboe2fd233b2007-03-02 08:44:25 +0100428
Jens Axboe66159822007-04-16 21:54:24 +0200429 if (is_std)
430 f->fd = dup(STDOUT_FILENO);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100431 else
432 from_hash = file_lookup_open(f, flags);
Jens Axboe2fd233b2007-03-02 08:44:25 +0100433 } else {
Jens Axboe4241ea82007-09-12 08:18:36 +0200434 if (f->filetype == FIO_TYPE_CHAR && !read_only)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100435 flags |= O_RDWR;
436 else
437 flags |= O_RDONLY;
438
Jens Axboe66159822007-04-16 21:54:24 +0200439 if (is_std)
440 f->fd = dup(STDIN_FILENO);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100441 else
442 from_hash = file_lookup_open(f, flags);
Jens Axboe53cdc682006-10-18 11:50:58 +0200443 }
444
445 if (f->fd == -1) {
Jens Axboee4e33252007-03-21 13:07:54 +0100446 char buf[FIO_VERROR_SIZE];
Jens Axboee1161c32007-02-22 19:36:48 +0100447 int __e = errno;
448
Jens Axboe835d9b92009-06-09 15:23:38 +0200449 if (__e == EPERM && (flags & FIO_O_NOATIME)) {
Jens Axboe5921e802008-05-30 15:02:38 +0200450 flags &= ~FIO_O_NOATIME;
Aaron Carroll056f3452007-11-26 09:08:53 +0100451 goto open_again;
452 }
453
Jens Axboee8670ef2008-03-06 12:06:30 +0100454 snprintf(buf, sizeof(buf) - 1, "open(%s)", f->file_name);
Jens Axboee4e33252007-03-21 13:07:54 +0100455
456 td_verror(td, __e, buf);
Jens Axboe53cdc682006-10-18 11:50:58 +0200457 }
458
Jens Axboe29c13492008-03-01 19:25:20 +0100459 if (!from_hash && f->fd != -1) {
460 if (add_file_hash(f)) {
461 int ret;
462
463 /*
464 * OK to ignore, we haven't done anything with it
465 */
466 ret = generic_close_file(td, f);
467 goto open_again;
468 }
469 }
Jens Axboe4906e0b2008-03-01 18:59:51 +0100470
Jens Axboe53cdc682006-10-18 11:50:58 +0200471 return 0;
Jens Axboeb5af8292007-03-08 12:43:13 +0100472}
473
Jens Axboedf9c26b2009-03-05 10:13:58 +0100474int generic_get_file_size(struct thread_data *td, struct fio_file *f)
Jens Axboe21972cd2006-11-23 12:39:16 +0100475{
Jens Axboedf9c26b2009-03-05 10:13:58 +0100476 return get_file_size(td, f);
Jens Axboe21972cd2006-11-23 12:39:16 +0100477}
478
Jens Axboe7bb48f82007-03-27 15:30:28 +0200479/*
480 * open/close all files, so that ->real_file_size gets set
481 */
Jens Axboebab3fd52007-04-02 10:34:18 +0200482static int get_file_sizes(struct thread_data *td)
Jens Axboe53cdc682006-10-18 11:50:58 +0200483{
484 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100485 unsigned int i;
Jens Axboebab3fd52007-04-02 10:34:18 +0200486 int err = 0;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200487
488 for_each_file(td, f, i) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100489 dprint(FD_FILE, "get file size for %p/%d/%p\n", f, i,
490 f->file_name);
Jens Axboe9efef3c2008-03-06 10:26:25 +0100491
Jens Axboe99a47c62009-04-07 22:20:56 +0200492 if (td_io_get_file_size(td, f)) {
Jens Axboe40b44f42007-04-11 12:43:35 +0200493 if (td->error != ENOENT) {
494 log_err("%s\n", td->verror);
495 err = 1;
496 }
Jens Axboe541d66d2007-03-27 19:50:11 +0200497 clear_error(td);
Jens Axboe07eb79d2007-04-12 13:02:38 +0200498 }
Jens Axboe409b3412007-03-28 09:57:01 +0200499
500 if (f->real_file_size == -1ULL && td->o.size)
501 f->real_file_size = td->o.size / td->o.nr_files;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200502 }
Jens Axboebab3fd52007-04-02 10:34:18 +0200503
504 return err;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200505}
506
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200507struct fio_mount {
508 struct flist_head list;
509 const char *base;
510 char __base[256];
511 unsigned int key;
512};
513
514/*
515 * Get free number of bytes for each file on each unique mount.
516 */
517static unsigned long long get_fs_free_counts(struct thread_data *td)
518{
519 struct flist_head *n, *tmp;
520 unsigned long long ret;
521 struct fio_mount *fm;
522 FLIST_HEAD(list);
523 struct fio_file *f;
524 unsigned int i;
525
526 for_each_file(td, f, i) {
527 struct stat sb;
528 char buf[256];
529
530 strcpy(buf, f->file_name);
531
532 if (stat(buf, &sb) < 0) {
533 if (errno != ENOENT)
534 break;
535 strcpy(buf, ".");
536 if (stat(buf, &sb) < 0)
537 break;
538 }
539
540 fm = NULL;
541 flist_for_each(n, &list) {
542 fm = flist_entry(n, struct fio_mount, list);
543 if (fm->key == sb.st_dev)
544 break;
545
546 fm = NULL;
547 }
548
549 if (fm)
550 continue;
551
552 fm = malloc(sizeof(*fm));
553 strcpy(fm->__base, buf);
554 fm->base = basename(fm->__base);
555 fm->key = sb.st_dev;
556 flist_add(&fm->list, &list);
557 }
558
559 ret = 0;
560 flist_for_each_safe(n, tmp, &list) {
561 unsigned long long sz;
562
563 fm = flist_entry(n, struct fio_mount, list);
564 flist_del(&fm->list);
565
566 sz = get_fs_size(fm->base);
567 if (sz && sz != -1ULL)
568 ret += sz;
569
570 free(fm);
571 }
572
573 return ret;
574}
575
Jens Axboe7bb48f82007-03-27 15:30:28 +0200576/*
577 * Open the files and setup files sizes, creating files if necessary.
578 */
579int setup_files(struct thread_data *td)
580{
581 unsigned long long total_size, extend_size;
582 struct fio_file *f;
583 unsigned int i;
Jens Axboe000b0802007-03-27 15:56:10 +0200584 int err = 0, need_extend;
Jens Axboe53cdc682006-10-18 11:50:58 +0200585
Jens Axboeee56ad52008-02-01 10:30:20 +0100586 dprint(FD_FILE, "setup files\n");
587
Jens Axboe691c8fb2008-03-07 14:26:26 +0100588 if (td->o.read_iolog_file)
589 return 0;
590
Jens Axboe53cdc682006-10-18 11:50:58 +0200591 /*
592 * if ioengine defines a setup() method, it's responsible for
Jens Axboe7bb48f82007-03-27 15:30:28 +0200593 * opening the files and setting f->real_file_size to indicate
594 * the valid range for that file.
Jens Axboe53cdc682006-10-18 11:50:58 +0200595 */
596 if (td->io_ops->setup)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200597 err = td->io_ops->setup(td);
598 else
Jens Axboebab3fd52007-04-02 10:34:18 +0200599 err = get_file_sizes(td);
Jens Axboe53cdc682006-10-18 11:50:58 +0200600
Jens Axboef1027062006-10-20 10:14:01 +0200601 if (err)
602 return err;
603
Jens Axboe0a7eb122006-10-20 10:36:42 +0200604 /*
Jens Axboe7bb48f82007-03-27 15:30:28 +0200605 * check sizes. if the files/devices do not exist and the size
606 * isn't passed to fio, abort.
Jens Axboe0a7eb122006-10-20 10:36:42 +0200607 */
Jens Axboe7bb48f82007-03-27 15:30:28 +0200608 total_size = 0;
609 for_each_file(td, f, i) {
610 if (f->real_file_size == -1ULL)
611 total_size = -1ULL;
612 else
613 total_size += f->real_file_size;
614 }
Jens Axboe0a7eb122006-10-20 10:36:42 +0200615
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200616 if (td->o.fill_device)
617 td->fill_device_size = get_fs_free_counts(td);
618
Jens Axboe7bb48f82007-03-27 15:30:28 +0200619 /*
620 * device/file sizes are zero and no size given, punt
621 */
Jens Axboe1f809d12007-10-25 18:34:02 +0200622 if ((!total_size || total_size == -1ULL) && !td->o.size &&
Shawn Lewisaa31f1f2008-01-11 09:45:11 +0100623 !(td->io_ops->flags & FIO_NOIO) && !td->o.fill_device) {
Jens Axboe7bb48f82007-03-27 15:30:28 +0200624 log_err("%s: you need to specify size=\n", td->o.name);
Jens Axboee1161c32007-02-22 19:36:48 +0100625 td_verror(td, EINVAL, "total_file_size");
Jens Axboe53cdc682006-10-18 11:50:58 +0200626 return 1;
627 }
628
Jens Axboe7bb48f82007-03-27 15:30:28 +0200629 /*
630 * now file sizes are known, so we can set ->io_size. if size= is
631 * not given, ->io_size is just equal to ->real_file_size. if size
632 * is given, ->io_size is size / nr_files.
633 */
634 extend_size = total_size = 0;
635 need_extend = 0;
636 for_each_file(td, f, i) {
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200637 f->file_offset = td->o.start_offset;
638
Jens Axboe7bb48f82007-03-27 15:30:28 +0200639 if (!td->o.file_size_low) {
640 /*
641 * no file size range given, file size is equal to
642 * total size divided by number of files. if that is
643 * zero, set it to the real file size.
644 */
645 f->io_size = td->o.size / td->o.nr_files;
Jens Axboe65bdb102008-01-24 13:13:12 +0100646 if (!f->io_size)
Jens Axboe273f8c92008-01-25 14:02:15 +0100647 f->io_size = f->real_file_size - f->file_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200648 } else if (f->real_file_size < td->o.file_size_low ||
649 f->real_file_size > td->o.file_size_high) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100650 if (f->file_offset > td->o.file_size_low)
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200651 goto err_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200652 /*
653 * file size given. if it's fixed, use that. if it's a
654 * range, generate a random size in-between.
655 */
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100656 if (td->o.file_size_low == td->o.file_size_high) {
657 f->io_size = td->o.file_size_low
658 - f->file_offset;
659 } else {
660 f->io_size = get_rand_file_size(td)
661 - f->file_offset;
662 }
Jens Axboe65bdb102008-01-24 13:13:12 +0100663 } else
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200664 f->io_size = f->real_file_size - f->file_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200665
666 if (f->io_size == -1ULL)
667 total_size = -1ULL;
668 else
669 total_size += f->io_size;
670
671 if (f->filetype == FIO_TYPE_FILE &&
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200672 (f->io_size + f->file_offset) > f->real_file_size &&
Jens Axboe7bb48f82007-03-27 15:30:28 +0200673 !(td->io_ops->flags & FIO_DISKLESSIO)) {
Jens Axboe814452b2009-03-04 12:53:13 +0100674 if (!td->o.create_on_open) {
675 need_extend++;
676 extend_size += (f->io_size + f->file_offset);
677 } else
678 f->real_file_size = f->io_size + f->file_offset;
Jens Axboed6aed792009-06-03 08:41:15 +0200679 fio_file_set_extend(f);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100680 }
Jens Axboe7bb48f82007-03-27 15:30:28 +0200681 }
682
ljzhang,Yaxin Hu,Jianchao Tang22982902007-07-27 13:21:28 +0200683 if (!td->o.size || td->o.size > total_size)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200684 td->o.size = total_size;
685
686 /*
687 * See if we need to extend some files
688 */
689 if (need_extend) {
690 temp_stall_ts = 1;
Shaozhi Shawn Yea7ba8c52008-09-10 09:04:18 +0200691 if (!terse_output)
692 log_info("%s: Laying out IO file(s) (%u file(s) /"
Jens Axboeb22989b2009-07-17 22:29:23 +0200693 " %LuMB)\n", td->o.name, need_extend,
Shaozhi Shawn Yea7ba8c52008-09-10 09:04:18 +0200694 extend_size >> 20);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200695
696 for_each_file(td, f, i) {
Jens Axboe5e0074c2009-06-02 21:40:09 +0200697 unsigned long long old_len = -1ULL, extend_len = -1ULL;
Jens Axboe3baddf22008-04-04 11:10:30 +0200698
Jens Axboed6aed792009-06-03 08:41:15 +0200699 if (!fio_file_extend(f))
Jens Axboe7bb48f82007-03-27 15:30:28 +0200700 continue;
701
Jens Axboe409b3412007-03-28 09:57:01 +0200702 assert(f->filetype == FIO_TYPE_FILE);
Jens Axboed6aed792009-06-03 08:41:15 +0200703 fio_file_clear_extend(f);
Jens Axboe5e0074c2009-06-02 21:40:09 +0200704 if (!td->o.fill_device) {
705 old_len = f->real_file_size;
Jens Axboe0b9d69e2009-09-11 22:29:54 +0200706 extend_len = f->io_size + f->file_offset -
707 old_len;
Jens Axboe5e0074c2009-06-02 21:40:09 +0200708 }
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200709 f->real_file_size = (f->io_size + f->file_offset);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200710 err = extend_file(td, f);
711 if (err)
712 break;
Jens Axboe5e0074c2009-06-02 21:40:09 +0200713
Jens Axboe3baddf22008-04-04 11:10:30 +0200714 err = __file_invalidate_cache(td, f, old_len,
715 extend_len);
716 close(f->fd);
717 f->fd = -1;
718 if (err)
719 break;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200720 }
721 temp_stall_ts = 0;
722 }
723
724 if (err)
725 return err;
726
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100727 if (!td->o.zone_size)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200728 td->o.zone_size = td->o.size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200729
Jens Axboeea966f82007-09-03 10:09:37 +0200730 /*
731 * iolog already set the total io size, if we read back
732 * stored entries.
733 */
734 if (!td->o.read_iolog_file)
735 td->total_io_size = td->o.size * td->o.loops;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200736 return 0;
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200737err_offset:
738 log_err("%s: you need to specify valid offset=\n", td->o.name);
739 return 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200740}
741
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200742int pre_read_files(struct thread_data *td)
743{
744 struct fio_file *f;
745 unsigned int i;
746
747 dprint(FD_FILE, "pre_read files\n");
748
749 for_each_file(td, f, i) {
750 pre_read_file(td, f);
751 }
752
753 return 1;
754}
755
Jens Axboe68727072007-03-15 20:49:25 +0100756int init_random_map(struct thread_data *td)
757{
Jens Axboe509eab12007-12-14 12:42:53 +0100758 unsigned long long blocks, num_maps;
Jens Axboe68727072007-03-15 20:49:25 +0100759 struct fio_file *f;
760 unsigned int i;
761
Jens Axboede8dd112008-03-01 15:34:44 +0100762 if (td->o.norandommap || !td_random(td))
Jens Axboe68727072007-03-15 20:49:25 +0100763 return 0;
764
765 for_each_file(td, f, i) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100766 blocks = (f->real_file_size + td->o.rw_min_bs - 1) /
767 (unsigned long long) td->o.rw_min_bs;
768 num_maps = (blocks + BLOCKS_PER_MAP - 1) /
769 (unsigned long long) BLOCKS_PER_MAP;
Jens Axboede605662008-05-31 00:21:12 +0200770 f->file_map = smalloc(num_maps * sizeof(int));
Jens Axboe303032a2008-03-26 10:11:10 +0100771 if (f->file_map) {
772 f->num_maps = num_maps;
773 continue;
Jens Axboe68727072007-03-15 20:49:25 +0100774 }
Jens Axboe2b386d22008-03-26 10:32:57 +0100775 if (!td->o.softrandommap) {
Jens Axboe68727072007-03-15 20:49:25 +0100776 log_err("fio: failed allocating random map. If running"
777 " a large number of jobs, try the 'norandommap'"
Jens Axboe2b386d22008-03-26 10:32:57 +0100778 " option or set 'softrandommap'. Or give"
779 " a larger --alloc-size to fio.\n");
Jens Axboe68727072007-03-15 20:49:25 +0100780 return 1;
781 }
Jens Axboe303032a2008-03-26 10:11:10 +0100782
783 log_info("fio: file %s failed allocating random map. Running "
784 "job without.\n", f->file_name);
785 f->num_maps = 0;
Jens Axboe68727072007-03-15 20:49:25 +0100786 }
787
788 return 0;
789}
790
Jens Axboe53cdc682006-10-18 11:50:58 +0200791void close_files(struct thread_data *td)
792{
Jens Axboe0ab8db82006-10-18 17:16:23 +0200793 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100794 unsigned int i;
Jens Axboe53cdc682006-10-18 11:50:58 +0200795
Jens Axboe2be3eec2009-06-10 09:05:13 +0200796 for_each_file(td, f, i) {
797 if (fio_file_open(f))
798 td_io_close_file(td, f);
799 }
Jens Axboe24ffd2c2008-03-01 15:47:08 +0100800}
801
802void close_and_free_files(struct thread_data *td)
803{
804 struct fio_file *f;
805 unsigned int i;
806
Jens Axboeee56ad52008-02-01 10:30:20 +0100807 dprint(FD_FILE, "close files\n");
808
Jens Axboe0ab8db82006-10-18 17:16:23 +0200809 for_each_file(td, f, i) {
Jens Axboeffdfabd2008-03-26 09:18:14 +0100810 if (td->o.unlink && f->filetype == FIO_TYPE_FILE) {
811 dprint(FD_FILE, "free unlink %s\n", f->file_name);
Jens Axboe132ad462006-11-23 12:23:12 +0100812 unlink(f->file_name);
Jens Axboeffdfabd2008-03-26 09:18:14 +0100813 }
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100814
Jens Axboe22a57ba2009-06-09 14:34:35 +0200815 if (fio_file_open(f))
816 td_io_close_file(td, f);
817
Shaozhi Shawn Yeb9fbcf22008-09-10 09:05:17 +0200818 remove_file_hash(f);
Jens Axboeb3dc7f02007-03-09 12:52:15 +0100819
Jens Axboef17c4392008-03-01 18:40:46 +0100820 sfree(f->file_name);
Jens Axboefa1da862007-03-23 15:20:54 +0100821 f->file_name = NULL;
Jens Axboe61eb3132010-03-31 20:05:59 +0200822 sfree(f->file_map);
823 f->file_map = NULL;
Jens Axboe78d99e62008-03-01 18:41:50 +0100824 sfree(f);
Jens Axboe53cdc682006-10-18 11:50:58 +0200825 }
Jens Axboeb4a6a592006-10-20 13:54:47 +0200826
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100827 td->o.filename = NULL;
Jens Axboecade3ef2007-03-23 15:29:45 +0100828 free(td->files);
Jens Axboe9efef3c2008-03-06 10:26:25 +0100829 td->files_index = 0;
Jens Axboeb4a6a592006-10-20 13:54:47 +0200830 td->files = NULL;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100831 td->o.nr_files = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200832}
Jens Axboeaf52b342007-03-13 10:07:47 +0100833
Jens Axboee3bab462007-03-13 11:22:09 +0100834static void get_file_type(struct fio_file *f)
Jens Axboeaf52b342007-03-13 10:07:47 +0100835{
836 struct stat sb;
837
Jens Axboe66159822007-04-16 21:54:24 +0200838 if (!strcmp(f->file_name, "-"))
839 f->filetype = FIO_TYPE_PIPE;
840 else
841 f->filetype = FIO_TYPE_FILE;
Jens Axboeaf52b342007-03-13 10:07:47 +0100842
Jens Axboeb30d3952010-02-06 23:36:26 +0100843 if (!stat(f->file_name, &sb)) {
Jens Axboeaf52b342007-03-13 10:07:47 +0100844 if (S_ISBLK(sb.st_mode))
845 f->filetype = FIO_TYPE_BD;
846 else if (S_ISCHR(sb.st_mode))
847 f->filetype = FIO_TYPE_CHAR;
Jens Axboeb5605e92007-04-16 20:42:33 +0200848 else if (S_ISFIFO(sb.st_mode))
849 f->filetype = FIO_TYPE_PIPE;
Jens Axboeaf52b342007-03-13 10:07:47 +0100850 }
851}
852
Jens Axboef29b25a2007-07-23 08:56:43 +0200853int add_file(struct thread_data *td, const char *fname)
Jens Axboeaf52b342007-03-13 10:07:47 +0100854{
Jens Axboe7b4e4fe2007-03-13 12:51:40 +0100855 int cur_files = td->files_index;
Jens Axboebd0ee742007-03-23 14:26:23 +0100856 char file_name[PATH_MAX];
Jens Axboeaf52b342007-03-13 10:07:47 +0100857 struct fio_file *f;
Jens Axboebd0ee742007-03-23 14:26:23 +0100858 int len = 0;
Jens Axboeaf52b342007-03-13 10:07:47 +0100859
Jens Axboeee56ad52008-02-01 10:30:20 +0100860 dprint(FD_FILE, "add file %s\n", fname);
861
Jens Axboef17c4392008-03-01 18:40:46 +0100862 f = smalloc(sizeof(*f));
Jens Axboec48c0be2008-07-31 19:55:02 +0200863 if (!f) {
864 log_err("fio: smalloc OOM\n");
865 assert(0);
866 }
Jens Axboe0b9d69e2009-09-11 22:29:54 +0200867
Jens Axboeaf52b342007-03-13 10:07:47 +0100868 f->fd = -1;
Jens Axboebd0ee742007-03-23 14:26:23 +0100869
Jens Axboefc99bc02009-03-04 15:27:42 +0100870 if (td->files_size <= td->files_index) {
Carl Henrik Lunde4b341fc2009-04-20 08:41:37 +0200871 int new_size = td->o.nr_files + 1;
Jens Axboe126d65c2008-03-01 18:04:31 +0100872
Jens Axboefc99bc02009-03-04 15:27:42 +0100873 dprint(FD_FILE, "resize file array to %d files\n", new_size);
874
875 td->files = realloc(td->files, new_size * sizeof(f));
Jens Axboefc99bc02009-03-04 15:27:42 +0100876 td->files_size = new_size;
877 }
Jens Axboe8bb76792009-03-04 15:37:52 +0100878 td->files[cur_files] = f;
Jens Axboe126d65c2008-03-01 18:04:31 +0100879
Jens Axboe07eb79d2007-04-12 13:02:38 +0200880 /*
881 * init function, io engine may not be loaded yet
882 */
883 if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO))
884 f->real_file_size = -1ULL;
885
Jens Axboebd0ee742007-03-23 14:26:23 +0100886 if (td->o.directory)
887 len = sprintf(file_name, "%s/", td->o.directory);
888
889 sprintf(file_name + len, "%s", fname);
Jens Axboef17c4392008-03-01 18:40:46 +0100890 f->file_name = smalloc_strdup(file_name);
Jens Axboec48c0be2008-07-31 19:55:02 +0200891 if (!f->file_name) {
892 log_err("fio: smalloc OOM\n");
893 assert(0);
894 }
Jens Axboe0b9d69e2009-09-11 22:29:54 +0200895
Jens Axboee3bab462007-03-13 11:22:09 +0100896 get_file_type(f);
Jens Axboeaf52b342007-03-13 10:07:47 +0100897
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100898 switch (td->o.file_lock_mode) {
899 case FILE_LOCK_NONE:
900 break;
901 case FILE_LOCK_READWRITE:
902 f->lock = fio_mutex_rw_init();
903 break;
904 case FILE_LOCK_EXCLUSIVE:
905 f->lock = fio_mutex_init(1);
906 break;
907 default:
908 log_err("fio: unknown lock mode: %d\n", td->o.file_lock_mode);
909 assert(0);
910 }
Jens Axboe29c13492008-03-01 19:25:20 +0100911
Jens Axboe7b4e4fe2007-03-13 12:51:40 +0100912 td->files_index++;
Jens Axboe15494412007-03-13 11:52:47 +0100913 if (f->filetype == FIO_TYPE_FILE)
914 td->nr_normal_files++;
Jens Axboef29b25a2007-07-23 08:56:43 +0200915
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100916 dprint(FD_FILE, "file %p \"%s\" added at %d\n", f, f->file_name,
917 cur_files);
Jens Axboe9efef3c2008-03-06 10:26:25 +0100918
Jens Axboef29b25a2007-07-23 08:56:43 +0200919 return cur_files;
Jens Axboeaf52b342007-03-13 10:07:47 +0100920}
Jens Axboe0ad920e2007-03-13 11:06:45 +0100921
922void get_file(struct fio_file *f)
923{
Jens Axboe8172fe92008-02-01 21:51:02 +0100924 dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references);
Jens Axboed6aed792009-06-03 08:41:15 +0200925 assert(fio_file_open(f));
Jens Axboe0ad920e2007-03-13 11:06:45 +0100926 f->references++;
927}
928
Jens Axboe6977bcd2008-03-01 15:55:36 +0100929int put_file(struct thread_data *td, struct fio_file *f)
Jens Axboe0ad920e2007-03-13 11:06:45 +0100930{
Jens Axboe98e1ac42008-03-06 11:56:48 +0100931 int f_ret = 0, ret = 0;
Jens Axboe6977bcd2008-03-01 15:55:36 +0100932
Jens Axboe8172fe92008-02-01 21:51:02 +0100933 dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references);
Jens Axboeee56ad52008-02-01 10:30:20 +0100934
Jens Axboe22a57ba2009-06-09 14:34:35 +0200935 if (!fio_file_open(f)) {
936 assert(f->fd == -1);
Jens Axboe6977bcd2008-03-01 15:55:36 +0100937 return 0;
Jens Axboe22a57ba2009-06-09 14:34:35 +0200938 }
Jens Axboe0ad920e2007-03-13 11:06:45 +0100939
940 assert(f->references);
941 if (--f->references)
Jens Axboe6977bcd2008-03-01 15:55:36 +0100942 return 0;
Jens Axboe0ad920e2007-03-13 11:06:45 +0100943
Jens Axboed424d4d2007-04-17 09:05:10 +0200944 if (should_fsync(td) && td->o.fsync_on_close)
Jens Axboe98e1ac42008-03-06 11:56:48 +0100945 f_ret = fsync(f->fd);
Jens Axboeebb14152007-03-13 14:42:15 +0100946
Jens Axboe0ad920e2007-03-13 11:06:45 +0100947 if (td->io_ops->close_file)
Jens Axboe6977bcd2008-03-01 15:55:36 +0100948 ret = td->io_ops->close_file(td, f);
Jens Axboe1020a132007-03-29 11:15:06 +0200949
Jens Axboe98e1ac42008-03-06 11:56:48 +0100950 if (!ret)
Jens Axboea5fb4612008-05-28 10:54:01 +0200951 ret = f_ret;
Jens Axboe98e1ac42008-03-06 11:56:48 +0100952
Jens Axboe0ad920e2007-03-13 11:06:45 +0100953 td->nr_open_files--;
Jens Axboed6aed792009-06-03 08:41:15 +0200954 fio_file_clear_open(f);
Jens Axboe22a57ba2009-06-09 14:34:35 +0200955 assert(f->fd == -1);
Jens Axboe6977bcd2008-03-01 15:55:36 +0100956 return ret;
Jens Axboe0ad920e2007-03-13 11:06:45 +0100957}
Jens Axboebbf6b542007-03-13 15:28:55 +0100958
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100959void lock_file(struct thread_data *td, struct fio_file *f, enum fio_ddir ddir)
Jens Axboeb2bd2bd2008-03-01 18:19:52 +0100960{
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100961 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
962 return;
Jens Axboe29c13492008-03-01 19:25:20 +0100963
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100964 if (f->lock_owner == td && f->lock_batch--)
965 return;
966
967 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
968 if (ddir == DDIR_READ)
969 fio_mutex_down_read(f->lock);
970 else
971 fio_mutex_down_write(f->lock);
972 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
973 fio_mutex_down(f->lock);
974
975 f->lock_owner = td;
976 f->lock_batch = td->o.lockfile_batch;
977 f->lock_ddir = ddir;
978}
979
980void unlock_file(struct thread_data *td, struct fio_file *f)
981{
982 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
983 return;
984 if (f->lock_batch)
985 return;
986
987 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
988 const int is_read = f->lock_ddir == DDIR_READ;
989 int val = fio_mutex_getval(f->lock);
990
991 if ((is_read && val == 1) || (!is_read && val == -1))
992 f->lock_owner = NULL;
993
994 if (is_read)
995 fio_mutex_up_read(f->lock);
996 else
997 fio_mutex_up_write(f->lock);
998 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE) {
999 int val = fio_mutex_getval(f->lock);
1000
1001 if (val == 0)
1002 f->lock_owner = NULL;
1003
1004 fio_mutex_up(f->lock);
Jens Axboe29c13492008-03-01 19:25:20 +01001005 }
Jens Axboeb2bd2bd2008-03-01 18:19:52 +01001006}
1007
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001008void unlock_file_all(struct thread_data *td, struct fio_file *f)
Jens Axboeb2bd2bd2008-03-01 18:19:52 +01001009{
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001010 if (f->lock_owner != td)
1011 return;
Jens Axboe29c13492008-03-01 19:25:20 +01001012
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001013 f->lock_batch = 0;
1014 unlock_file(td, f);
Jens Axboeb2bd2bd2008-03-01 18:19:52 +01001015}
1016
Jens Axboebbf6b542007-03-13 15:28:55 +01001017static int recurse_dir(struct thread_data *td, const char *dirname)
1018{
1019 struct dirent *dir;
1020 int ret = 0;
1021 DIR *D;
1022
1023 D = opendir(dirname);
1024 if (!D) {
Jens Axboe0ddb2702007-03-27 19:43:53 +02001025 char buf[FIO_VERROR_SIZE];
1026
1027 snprintf(buf, FIO_VERROR_SIZE - 1, "opendir(%s)", dirname);
1028 td_verror(td, errno, buf);
Jens Axboebbf6b542007-03-13 15:28:55 +01001029 return 1;
1030 }
1031
1032 while ((dir = readdir(D)) != NULL) {
1033 char full_path[PATH_MAX];
1034 struct stat sb;
1035
Jens Axboee85b2b82007-03-14 09:39:06 +01001036 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
1037 continue;
Jens Axboe96d32d52007-03-14 09:16:09 +01001038
Jens Axboebbf6b542007-03-13 15:28:55 +01001039 sprintf(full_path, "%s/%s", dirname, dir->d_name);
1040
1041 if (lstat(full_path, &sb) == -1) {
1042 if (errno != ENOENT) {
1043 td_verror(td, errno, "stat");
1044 return 1;
1045 }
1046 }
1047
1048 if (S_ISREG(sb.st_mode)) {
1049 add_file(td, full_path);
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001050 td->o.nr_files++;
Jens Axboebbf6b542007-03-13 15:28:55 +01001051 continue;
1052 }
Jens Axboe0ddb2702007-03-27 19:43:53 +02001053 if (!S_ISDIR(sb.st_mode))
1054 continue;
Jens Axboebbf6b542007-03-13 15:28:55 +01001055
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001056 ret = recurse_dir(td, full_path);
1057 if (ret)
Jens Axboebbf6b542007-03-13 15:28:55 +01001058 break;
1059 }
1060
1061 closedir(D);
1062 return ret;
1063}
1064
1065int add_dir_files(struct thread_data *td, const char *path)
1066{
Jens Axboe0ddb2702007-03-27 19:43:53 +02001067 int ret = recurse_dir(td, path);
1068
1069 if (!ret)
1070 log_info("fio: opendir added %d files\n", td->o.nr_files);
1071
1072 return ret;
Jens Axboebbf6b542007-03-13 15:28:55 +01001073}
Jens Axboecade3ef2007-03-23 15:29:45 +01001074
1075void dup_files(struct thread_data *td, struct thread_data *org)
1076{
1077 struct fio_file *f;
1078 unsigned int i;
Jens Axboe9efef3c2008-03-06 10:26:25 +01001079
1080 dprint(FD_FILE, "dup files: %d\n", org->files_index);
Jens Axboecade3ef2007-03-23 15:29:45 +01001081
1082 if (!org->files)
1083 return;
1084
Jens Axboe9efef3c2008-03-06 10:26:25 +01001085 td->files = malloc(org->files_index * sizeof(f));
Jens Axboecade3ef2007-03-23 15:29:45 +01001086
Jens Axboe9efef3c2008-03-06 10:26:25 +01001087 for_each_file(org, f, i) {
Jens Axboeb0fe4212008-03-01 18:22:27 +01001088 struct fio_file *__f;
1089
Jens Axboef17c4392008-03-01 18:40:46 +01001090 __f = smalloc(sizeof(*__f));
Jens Axboec48c0be2008-07-31 19:55:02 +02001091 if (!__f) {
1092 log_err("fio: smalloc OOM\n");
1093 assert(0);
1094 }
Jens Axboe22a57ba2009-06-09 14:34:35 +02001095 __f->fd = -1;
Jens Axboe0b9d69e2009-09-11 22:29:54 +02001096
Aaron Carrollbc3456f2008-06-25 09:20:37 +02001097 if (f->file_name) {
Jens Axboef17c4392008-03-01 18:40:46 +01001098 __f->file_name = smalloc_strdup(f->file_name);
Jens Axboec48c0be2008-07-31 19:55:02 +02001099 if (!__f->file_name) {
1100 log_err("fio: smalloc OOM\n");
1101 assert(0);
1102 }
Jens Axboe0b9d69e2009-09-11 22:29:54 +02001103
Aaron Carrollbc3456f2008-06-25 09:20:37 +02001104 __f->filetype = f->filetype;
1105 }
Jens Axboeb0fe4212008-03-01 18:22:27 +01001106
1107 td->files[i] = __f;
Jens Axboecade3ef2007-03-23 15:29:45 +01001108 }
1109}
Jens Axboef29b25a2007-07-23 08:56:43 +02001110
1111/*
1112 * Returns the index that matches the filename, or -1 if not there
1113 */
1114int get_fileno(struct thread_data *td, const char *fname)
1115{
1116 struct fio_file *f;
1117 unsigned int i;
1118
1119 for_each_file(td, f, i)
1120 if (!strcmp(f->file_name, fname))
1121 return i;
1122
1123 return -1;
1124}
1125
1126/*
1127 * For log usage, where we add/open/close files automatically
1128 */
1129void free_release_files(struct thread_data *td)
1130{
1131 close_files(td);
1132 td->files_index = 0;
1133 td->nr_normal_files = 0;
1134}