blob: 9d42deb02d47dbab2866fcb9d2512409e60aa1b5 [file] [log] [blame]
Jens Axboe53cdc682006-10-18 11:50:58 +02001#include <unistd.h>
2#include <fcntl.h>
3#include <string.h>
4#include <assert.h>
Jens Axboebbf6b542007-03-13 15:28:55 +01005#include <dirent.h>
YAMAMOTO Takashid74ac842010-06-09 09:44:53 +02006#include <libgen.h>
Jens Axboe53cdc682006-10-18 11:50:58 +02007#include <sys/stat.h>
8#include <sys/mman.h>
Jens Axboebbf6b542007-03-13 15:28:55 +01009#include <sys/types.h>
Jens Axboe53cdc682006-10-18 11:50:58 +020010
11#include "fio.h"
Jens Axboef17c4392008-03-01 18:40:46 +010012#include "smalloc.h"
Jens Axboe4906e0b2008-03-01 18:59:51 +010013#include "filehash.h"
Bruce Cranecc314b2011-01-04 10:59:30 +010014#include "os/os.h"
Jens Axboe53cdc682006-10-18 11:50:58 +020015
Jens Axboe7172cfe2007-07-23 14:36:00 +020016static int root_warn;
17
Jens Axboec592b9f2009-06-03 09:29:28 +020018static inline void clear_error(struct thread_data *td)
19{
20 td->error = 0;
21 td->verror[0] = '\0';
22}
23
Jens Axboe3baddf22008-04-04 11:10:30 +020024/*
25 * Leaves f->fd open on success, caller must close
26 */
Jens Axboe7bb48f82007-03-27 15:30:28 +020027static int extend_file(struct thread_data *td, struct fio_file *f)
Jens Axboe25205e92006-10-19 20:50:14 +020028{
Jens Axboeea443652007-03-29 14:52:13 +020029 int r, new_layout = 0, unlink_file = 0, flags;
Jens Axboe25205e92006-10-19 20:50:14 +020030 unsigned long long left;
31 unsigned int bs;
32 char *b;
Jens Axboeb2a15192006-10-18 14:10:42 +020033
Jens Axboe4241ea82007-09-12 08:18:36 +020034 if (read_only) {
35 log_err("fio: refusing extend of file due to read-only\n");
36 return 0;
37 }
38
Jens Axboe507a7022007-03-27 15:52:46 +020039 /*
40 * check if we need to lay the file out complete again. fio
41 * does that for operations involving reads, or for writes
42 * where overwrite is set
43 */
Jens Axboeffdfabd2008-03-26 09:18:14 +010044 if (td_read(td) || (td_write(td) && td->o.overwrite) ||
45 (td_write(td) && td->io_ops->flags & FIO_NOEXTEND))
Jens Axboe507a7022007-03-27 15:52:46 +020046 new_layout = 1;
Jens Axboeea443652007-03-29 14:52:13 +020047 if (td_write(td) && !td->o.overwrite)
48 unlink_file = 1;
Jens Axboe507a7022007-03-27 15:52:46 +020049
Jens Axboe6ae1f572008-02-21 10:20:00 +010050 if (unlink_file || new_layout) {
Jens Axboebd199f22008-03-26 09:57:18 +010051 dprint(FD_FILE, "layout unlink %s\n", f->file_name);
Zhang, Yanmin982016d2008-02-26 15:35:52 +010052 if ((unlink(f->file_name) < 0) && (errno != ENOENT)) {
Jens Axboe7bb48f82007-03-27 15:30:28 +020053 td_verror(td, errno, "unlink");
54 return 1;
55 }
56 }
57
Jens Axboe507a7022007-03-27 15:52:46 +020058 flags = O_WRONLY | O_CREAT;
59 if (new_layout)
60 flags |= O_TRUNC;
61
Jens Axboeee56ad52008-02-01 10:30:20 +010062 dprint(FD_FILE, "open file %s, flags %x\n", f->file_name, flags);
Jens Axboe507a7022007-03-27 15:52:46 +020063 f->fd = open(f->file_name, flags, 0644);
Jens Axboe53cdc682006-10-18 11:50:58 +020064 if (f->fd < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +010065 td_verror(td, errno, "open");
Jens Axboe53cdc682006-10-18 11:50:58 +020066 return 1;
67 }
68
Jens Axboe7bc8c2c2010-01-28 11:31:31 +010069#ifdef FIO_HAVE_FALLOCATE
70 if (td->o.fallocate && !td->o.fill_device) {
71 dprint(FD_FILE, "fallocate file %s size %llu\n", f->file_name,
72 f->real_file_size);
73
74 r = posix_fallocate(f->fd, 0, f->real_file_size);
Greg Edwards10accd72010-06-25 09:25:22 -060075 if (r > 0) {
Jens Axboe7bc8c2c2010-01-28 11:31:31 +010076 log_err("fio: posix_fallocate fails: %s\n",
Greg Edwards10accd72010-06-25 09:25:22 -060077 strerror(r));
Jens Axboe7bc8c2c2010-01-28 11:31:31 +010078 }
79 }
80#endif
81
Shawn Lewisfc74ac12008-01-11 09:45:11 +010082 if (!new_layout)
83 goto done;
84
Jens Axboe5e0074c2009-06-02 21:40:09 +020085 /*
86 * The size will be -1ULL when fill_device is used, so don't truncate
87 * or fallocate this file, just write it
88 */
89 if (!td->o.fill_device) {
90 dprint(FD_FILE, "truncate file %s, size %llu\n", f->file_name,
Jens Axboeee56ad52008-02-01 10:30:20 +010091 f->real_file_size);
Jens Axboe5e0074c2009-06-02 21:40:09 +020092 if (ftruncate(f->fd, f->real_file_size) == -1) {
93 td_verror(td, errno, "ftruncate");
94 goto err;
95 }
Jens Axboe5e0074c2009-06-02 21:40:09 +020096 }
Jens Axboe40f82982006-10-25 11:21:05 +020097
Jens Axboe2dc1bbe2007-03-15 15:01:33 +010098 b = malloc(td->o.max_bs[DDIR_WRITE]);
99 memset(b, 0, td->o.max_bs[DDIR_WRITE]);
Jens Axboe53cdc682006-10-18 11:50:58 +0200100
Jens Axboe7bb48f82007-03-27 15:30:28 +0200101 left = f->real_file_size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200102 while (left && !td->terminate) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100103 bs = td->o.max_bs[DDIR_WRITE];
Jens Axboe53cdc682006-10-18 11:50:58 +0200104 if (bs > left)
105 bs = left;
106
107 r = write(f->fd, b, bs);
108
Jens Axboe5e0074c2009-06-02 21:40:09 +0200109 if (r > 0) {
110 left -= r;
Jens Axboe53cdc682006-10-18 11:50:58 +0200111 continue;
112 } else {
Jens Axboe5e0074c2009-06-02 21:40:09 +0200113 if (r < 0) {
114 int __e = errno;
115
116 if (__e == ENOSPC) {
117 if (td->o.fill_device)
118 break;
119 log_info("fio: ENOSPC on laying out "
120 "file, stopping\n");
121 break;
122 }
Jens Axboee1161c32007-02-22 19:36:48 +0100123 td_verror(td, errno, "write");
Jens Axboe5e0074c2009-06-02 21:40:09 +0200124 } else
Jens Axboee1161c32007-02-22 19:36:48 +0100125 td_verror(td, EIO, "write");
Jens Axboe53cdc682006-10-18 11:50:58 +0200126
127 break;
128 }
129 }
130
Jens Axboeffdfabd2008-03-26 09:18:14 +0100131 if (td->terminate) {
132 dprint(FD_FILE, "terminate unlink %s\n", f->file_name);
Jens Axboe53cdc682006-10-18 11:50:58 +0200133 unlink(f->file_name);
Jens Axboeffdfabd2008-03-26 09:18:14 +0100134 } else if (td->o.create_fsync) {
Jens Axboe98e1ac42008-03-06 11:56:48 +0100135 if (fsync(f->fd) < 0) {
136 td_verror(td, errno, "fsync");
137 goto err;
138 }
139 }
Jens Axboe0d1cd202009-06-05 22:11:52 +0200140 if (td->o.fill_device && !td_write(td)) {
Jens Axboed6aed792009-06-03 08:41:15 +0200141 fio_file_clear_size_known(f);
Jens Axboe5e0074c2009-06-02 21:40:09 +0200142 if (td_io_get_file_size(td, f))
143 goto err;
144 if (f->io_size > f->real_file_size)
145 f->io_size = f->real_file_size;
146 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200147
148 free(b);
Jens Axboe507a7022007-03-27 15:52:46 +0200149done:
Jens Axboe53cdc682006-10-18 11:50:58 +0200150 return 0;
151err:
152 close(f->fd);
153 f->fd = -1;
154 return 1;
155}
156
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200157static int pre_read_file(struct thread_data *td, struct fio_file *f)
158{
Jens Axboeb0f65862009-05-20 11:52:15 +0200159 int r, did_open = 0, old_runstate;
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200160 unsigned long long left;
161 unsigned int bs;
162 char *b;
163
Jens Axboe9c0d2242009-07-01 12:26:28 +0200164 if (td->io_ops->flags & FIO_PIPEIO)
165 return 0;
166
Jens Axboed6aed792009-06-03 08:41:15 +0200167 if (!fio_file_open(f)) {
Jens Axboeb0f65862009-05-20 11:52:15 +0200168 if (td->io_ops->open_file(td, f)) {
169 log_err("fio: cannot pre-read, failed to open file\n");
170 return 1;
171 }
172 did_open = 1;
173 }
174
175 old_runstate = td->runstate;
176 td_set_runstate(td, TD_PRE_READING);
177
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200178 bs = td->o.max_bs[DDIR_READ];
179 b = malloc(bs);
180 memset(b, 0, bs);
181
182 lseek(f->fd, f->file_offset, SEEK_SET);
183 left = f->io_size;
184
185 while (left && !td->terminate) {
186 if (bs > left)
187 bs = left;
188
189 r = read(f->fd, b, bs);
190
191 if (r == (int) bs) {
192 left -= bs;
193 continue;
194 } else {
195 td_verror(td, EIO, "pre_read");
196 break;
197 }
198 }
199
Jens Axboeb0f65862009-05-20 11:52:15 +0200200 td_set_runstate(td, old_runstate);
201
202 if (did_open)
203 td->io_ops->close_file(td, f);
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200204 free(b);
205 return 0;
206}
207
Jens Axboe7bb48f82007-03-27 15:30:28 +0200208static unsigned long long get_rand_file_size(struct thread_data *td)
Jens Axboe9c60ce62007-03-15 09:14:47 +0100209{
Jens Axboedc873b62008-06-04 20:13:04 +0200210 unsigned long long ret, sized;
Jens Axboe9c60ce62007-03-15 09:14:47 +0100211 long r;
212
Jens Axboe9c60ce62007-03-15 09:14:47 +0100213 r = os_random_long(&td->file_size_state);
Jens Axboedc873b62008-06-04 20:13:04 +0200214 sized = td->o.file_size_high - td->o.file_size_low;
215 ret = (unsigned long long) ((double) sized * (r / (OS_RAND_MAX + 1.0)));
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100216 ret += td->o.file_size_low;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100217 ret -= (ret % td->o.rw_min_bs);
Jens Axboe9c60ce62007-03-15 09:14:47 +0100218 return ret;
219}
220
Jens Axboe53cdc682006-10-18 11:50:58 +0200221static int file_size(struct thread_data *td, struct fio_file *f)
222{
223 struct stat st;
224
Jens Axboedf9c26b2009-03-05 10:13:58 +0100225 if (stat(f->file_name, &st) == -1) {
Jens Axboe7bb48f82007-03-27 15:30:28 +0200226 td_verror(td, errno, "fstat");
227 return 1;
228 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200229
Jens Axboe7bb48f82007-03-27 15:30:28 +0200230 f->real_file_size = st.st_size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200231 return 0;
232}
233
234static int bdev_size(struct thread_data *td, struct fio_file *f)
235{
236 unsigned long long bytes;
237 int r;
238
Jens Axboedf9c26b2009-03-05 10:13:58 +0100239 if (td->io_ops->open_file(td, f)) {
240 log_err("fio: failed opening blockdev %s for size check\n",
241 f->file_name);
242 return 1;
243 }
244
Bruce Cranecc314b2011-01-04 10:59:30 +0100245 r = blockdev_size(f, &bytes);
Jens Axboe53cdc682006-10-18 11:50:58 +0200246 if (r) {
Jens Axboee1161c32007-02-22 19:36:48 +0100247 td_verror(td, r, "blockdev_size");
Jens Axboe0e238572010-10-08 11:26:43 +0200248 printf("fd is %d\n", f->fd);
Jens Axboedf9c26b2009-03-05 10:13:58 +0100249 goto err;
Jens Axboe53cdc682006-10-18 11:50:58 +0200250 }
251
Jens Axboe7ab077a2009-02-02 15:07:37 +0100252 if (!bytes) {
253 log_err("%s: zero sized block device?\n", f->file_name);
Jens Axboedf9c26b2009-03-05 10:13:58 +0100254 goto err;
Jens Axboe7ab077a2009-02-02 15:07:37 +0100255 }
256
Jens Axboe53cdc682006-10-18 11:50:58 +0200257 f->real_file_size = bytes;
Jens Axboe22a57ba2009-06-09 14:34:35 +0200258 td->io_ops->close_file(td, f);
Jens Axboe53cdc682006-10-18 11:50:58 +0200259 return 0;
Jens Axboedf9c26b2009-03-05 10:13:58 +0100260err:
261 td->io_ops->close_file(td, f);
262 return 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200263}
264
Jens Axboe4ccdccd2010-06-24 10:27:22 +0200265static int char_size(struct thread_data *td, struct fio_file *f)
266{
267#ifdef FIO_HAVE_CHARDEV_SIZE
268 unsigned long long bytes;
269 int r;
270
271 if (td->io_ops->open_file(td, f)) {
272 log_err("fio: failed opening blockdev %s for size check\n",
273 f->file_name);
274 return 1;
275 }
276
Bruce Cranecc314b2011-01-04 10:59:30 +0100277 r = chardev_size(f, &bytes);
Jens Axboe4ccdccd2010-06-24 10:27:22 +0200278 if (r) {
279 td_verror(td, r, "chardev_size");
280 goto err;
281 }
282
283 if (!bytes) {
284 log_err("%s: zero sized char device?\n", f->file_name);
285 goto err;
286 }
287
288 f->real_file_size = bytes;
289 td->io_ops->close_file(td, f);
290 return 0;
291err:
292 td->io_ops->close_file(td, f);
293 return 1;
294#else
295 f->real_file_size = -1ULL;
296 return 0;
297#endif
298}
299
Jens Axboe53cdc682006-10-18 11:50:58 +0200300static int get_file_size(struct thread_data *td, struct fio_file *f)
301{
302 int ret = 0;
303
Jens Axboed6aed792009-06-03 08:41:15 +0200304 if (fio_file_size_known(f))
Jens Axboe409b3412007-03-28 09:57:01 +0200305 return 0;
306
Jens Axboe7bb48f82007-03-27 15:30:28 +0200307 if (f->filetype == FIO_TYPE_FILE)
308 ret = file_size(td, f);
309 else if (f->filetype == FIO_TYPE_BD)
Jens Axboe53cdc682006-10-18 11:50:58 +0200310 ret = bdev_size(td, f);
Jens Axboe4ccdccd2010-06-24 10:27:22 +0200311 else if (f->filetype == FIO_TYPE_CHAR)
312 ret = char_size(td, f);
Jens Axboe53cdc682006-10-18 11:50:58 +0200313 else
314 f->real_file_size = -1;
315
316 if (ret)
317 return ret;
318
319 if (f->file_offset > f->real_file_size) {
Bruce Cran0f2152c2010-12-15 10:33:03 +0100320 log_err("%s: offset extends end (%llu > %llu)\n", td->o.name,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100321 f->file_offset, f->real_file_size);
Jens Axboe53cdc682006-10-18 11:50:58 +0200322 return 1;
323 }
324
Jens Axboed6aed792009-06-03 08:41:15 +0200325 fio_file_set_size_known(f);
Jens Axboe53cdc682006-10-18 11:50:58 +0200326 return 0;
327}
328
Jens Axboe3baddf22008-04-04 11:10:30 +0200329static int __file_invalidate_cache(struct thread_data *td, struct fio_file *f,
330 unsigned long long off,
331 unsigned long long len)
Jens Axboee5b401d2006-10-18 16:03:40 +0200332{
333 int ret = 0;
334
Jens Axboe5e0074c2009-06-02 21:40:09 +0200335 if (len == -1ULL)
Jens Axboe3baddf22008-04-04 11:10:30 +0200336 len = f->io_size;
337 if (off == -1ULL)
338 off = f->file_offset;
Jens Axboeee56ad52008-02-01 10:30:20 +0100339
Jens Axboe0d1cd202009-06-05 22:11:52 +0200340 if (len == -1ULL || off == -1ULL)
341 return 0;
342
Jens Axboe3baddf22008-04-04 11:10:30 +0200343 dprint(FD_IO, "invalidate cache %s: %llu/%llu\n", f->file_name, off,
344 len);
Jens Axboeb5af8292007-03-08 12:43:13 +0100345
Jens Axboee5b401d2006-10-18 16:03:40 +0200346 /*
347 * FIXME: add blockdev flushing too
348 */
Jens Axboea1c58072009-08-04 23:17:02 +0200349 if (f->mmap_ptr) {
Bruce Cran03e20d62011-01-02 20:14:54 +0100350 ret = posix_madvise(f->mmap_ptr, f->mmap_sz, POSIX_MADV_DONTNEED);
Jens Axboea1c58072009-08-04 23:17:02 +0200351#ifdef FIO_MADV_FREE
Bruce Cran03e20d62011-01-02 20:14:54 +0100352 (void) posix_madvise(f->mmap_ptr, f->mmap_sz, FIO_MADV_FREE);
Jens Axboea1c58072009-08-04 23:17:02 +0200353#endif
354 } else if (f->filetype == FIO_TYPE_FILE) {
Bruce Cranecc314b2011-01-04 10:59:30 +0100355 ret = posix_fadvise(f->fd, off, len, POSIX_FADV_DONTNEED);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100356 } else if (f->filetype == FIO_TYPE_BD) {
Bruce Cranecc314b2011-01-04 10:59:30 +0100357 ret = blockdev_invalidate_cache(f);
Jens Axboe7e0e25c2007-03-27 12:49:56 +0200358 if (ret < 0 && errno == EACCES && geteuid()) {
Jens Axboe7172cfe2007-07-23 14:36:00 +0200359 if (!root_warn) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100360 log_err("fio: only root may flush block "
361 "devices. Cache flush bypassed!\n");
Jens Axboe7172cfe2007-07-23 14:36:00 +0200362 root_warn = 1;
363 }
Jens Axboe7e0e25c2007-03-27 12:49:56 +0200364 ret = 0;
365 }
Jens Axboeb5605e92007-04-16 20:42:33 +0200366 } else if (f->filetype == FIO_TYPE_CHAR || f->filetype == FIO_TYPE_PIPE)
Jens Axboee5b401d2006-10-18 16:03:40 +0200367 ret = 0;
368
369 if (ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100370 td_verror(td, errno, "invalidate_cache");
Jens Axboee5b401d2006-10-18 16:03:40 +0200371 return 1;
Jens Axboe3baddf22008-04-04 11:10:30 +0200372 } else if (ret > 0) {
373 td_verror(td, ret, "invalidate_cache");
374 return 1;
Jens Axboee5b401d2006-10-18 16:03:40 +0200375 }
376
Jens Axboead2da602007-02-26 12:33:27 +0100377 return ret;
Jens Axboe3baddf22008-04-04 11:10:30 +0200378
379}
380
381int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
382{
Jens Axboed6aed792009-06-03 08:41:15 +0200383 if (!fio_file_open(f))
Jens Axboea5fb4612008-05-28 10:54:01 +0200384 return 0;
385
Jens Axboe5e0074c2009-06-02 21:40:09 +0200386 return __file_invalidate_cache(td, f, -1ULL, -1ULL);
Jens Axboee5b401d2006-10-18 16:03:40 +0200387}
388
Jens Axboe6977bcd2008-03-01 15:55:36 +0100389int generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
Jens Axboe53cdc682006-10-18 11:50:58 +0200390{
Jens Axboe6977bcd2008-03-01 15:55:36 +0100391 int ret = 0;
392
Jens Axboeee56ad52008-02-01 10:30:20 +0100393 dprint(FD_FILE, "fd close %s\n", f->file_name);
Jens Axboe4906e0b2008-03-01 18:59:51 +0100394
395 remove_file_hash(f);
396
Jens Axboe6977bcd2008-03-01 15:55:36 +0100397 if (close(f->fd) < 0)
398 ret = errno;
399
Jens Axboeb5af8292007-03-08 12:43:13 +0100400 f->fd = -1;
Jens Axboe6977bcd2008-03-01 15:55:36 +0100401 return ret;
Jens Axboe53cdc682006-10-18 11:50:58 +0200402}
403
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100404static int file_lookup_open(struct fio_file *f, int flags)
Jens Axboe53cdc682006-10-18 11:50:58 +0200405{
Jens Axboe29c13492008-03-01 19:25:20 +0100406 struct fio_file *__f;
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100407 int from_hash;
408
409 __f = lookup_file_hash(f->file_name);
410 if (__f) {
Jens Axboe9efef3c2008-03-06 10:26:25 +0100411 dprint(FD_FILE, "found file in hash %s\n", f->file_name);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100412 /*
413 * racy, need the __f->lock locked
414 */
415 f->lock = __f->lock;
416 f->lock_owner = __f->lock_owner;
417 f->lock_batch = __f->lock_batch;
418 f->lock_ddir = __f->lock_ddir;
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100419 from_hash = 1;
420 } else {
Jens Axboe9efef3c2008-03-06 10:26:25 +0100421 dprint(FD_FILE, "file not found in hash %s\n", f->file_name);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100422 from_hash = 0;
423 }
424
Jens Axboee8670ef2008-03-06 12:06:30 +0100425 f->fd = open(f->file_name, flags, 0600);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100426 return from_hash;
427}
428
429int generic_open_file(struct thread_data *td, struct fio_file *f)
430{
Jens Axboe66159822007-04-16 21:54:24 +0200431 int is_std = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200432 int flags = 0;
Jens Axboe29c13492008-03-01 19:25:20 +0100433 int from_hash = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200434
Jens Axboeee56ad52008-02-01 10:30:20 +0100435 dprint(FD_FILE, "fd open %s\n", f->file_name);
436
Jens Axboe66159822007-04-16 21:54:24 +0200437 if (!strcmp(f->file_name, "-")) {
438 if (td_rw(td)) {
439 log_err("fio: can't read/write to stdin/out\n");
440 return 1;
441 }
442 is_std = 1;
Jens Axboece98fa62007-04-17 13:27:32 +0200443
444 /*
445 * move output logging to stderr, if we are writing to stdout
446 */
447 if (td_write(td))
448 f_out = stderr;
Jens Axboe66159822007-04-16 21:54:24 +0200449 }
450
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100451 if (td->o.odirect)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100452 flags |= OS_O_DIRECT;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100453 if (td->o.sync_io)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100454 flags |= O_SYNC;
Jens Axboead923962007-10-11 21:41:41 +0200455 if (f->filetype != FIO_TYPE_FILE)
Jens Axboe5921e802008-05-30 15:02:38 +0200456 flags |= FIO_O_NOATIME;
Jens Axboe814452b2009-03-04 12:53:13 +0100457 if (td->o.create_on_open)
458 flags |= O_CREAT;
Jens Axboe7abf8332006-11-23 15:01:19 +0100459
Aaron Carroll056f3452007-11-26 09:08:53 +0100460open_again:
Jens Axboe660a1cb2007-04-17 15:37:47 +0200461 if (td_write(td)) {
Jens Axboe17308152008-03-07 13:42:31 +0100462 if (!read_only)
463 flags |= O_RDWR;
Jens Axboe53cdc682006-10-18 11:50:58 +0200464
Jens Axboeaf52b342007-03-13 10:07:47 +0100465 if (f->filetype == FIO_TYPE_FILE)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100466 flags |= O_CREAT;
Jens Axboe2fd233b2007-03-02 08:44:25 +0100467
Jens Axboe66159822007-04-16 21:54:24 +0200468 if (is_std)
469 f->fd = dup(STDOUT_FILENO);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100470 else
471 from_hash = file_lookup_open(f, flags);
Jens Axboe2fd233b2007-03-02 08:44:25 +0100472 } else {
Jens Axboe4241ea82007-09-12 08:18:36 +0200473 if (f->filetype == FIO_TYPE_CHAR && !read_only)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100474 flags |= O_RDWR;
475 else
476 flags |= O_RDONLY;
477
Jens Axboe66159822007-04-16 21:54:24 +0200478 if (is_std)
479 f->fd = dup(STDIN_FILENO);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100480 else
481 from_hash = file_lookup_open(f, flags);
Jens Axboe53cdc682006-10-18 11:50:58 +0200482 }
483
484 if (f->fd == -1) {
Jens Axboee4e33252007-03-21 13:07:54 +0100485 char buf[FIO_VERROR_SIZE];
Jens Axboee1161c32007-02-22 19:36:48 +0100486 int __e = errno;
487
Jens Axboe835d9b92009-06-09 15:23:38 +0200488 if (__e == EPERM && (flags & FIO_O_NOATIME)) {
Jens Axboe5921e802008-05-30 15:02:38 +0200489 flags &= ~FIO_O_NOATIME;
Aaron Carroll056f3452007-11-26 09:08:53 +0100490 goto open_again;
491 }
492
Jens Axboee8670ef2008-03-06 12:06:30 +0100493 snprintf(buf, sizeof(buf) - 1, "open(%s)", f->file_name);
Jens Axboee4e33252007-03-21 13:07:54 +0100494
495 td_verror(td, __e, buf);
Jens Axboe53cdc682006-10-18 11:50:58 +0200496 }
497
Jens Axboe29c13492008-03-01 19:25:20 +0100498 if (!from_hash && f->fd != -1) {
499 if (add_file_hash(f)) {
500 int ret;
501
502 /*
503 * OK to ignore, we haven't done anything with it
504 */
505 ret = generic_close_file(td, f);
506 goto open_again;
507 }
508 }
Jens Axboe4906e0b2008-03-01 18:59:51 +0100509
Jens Axboe53cdc682006-10-18 11:50:58 +0200510 return 0;
Jens Axboeb5af8292007-03-08 12:43:13 +0100511}
512
Jens Axboedf9c26b2009-03-05 10:13:58 +0100513int generic_get_file_size(struct thread_data *td, struct fio_file *f)
Jens Axboe21972cd2006-11-23 12:39:16 +0100514{
Jens Axboedf9c26b2009-03-05 10:13:58 +0100515 return get_file_size(td, f);
Jens Axboe21972cd2006-11-23 12:39:16 +0100516}
517
Jens Axboe7bb48f82007-03-27 15:30:28 +0200518/*
519 * open/close all files, so that ->real_file_size gets set
520 */
Jens Axboebab3fd52007-04-02 10:34:18 +0200521static int get_file_sizes(struct thread_data *td)
Jens Axboe53cdc682006-10-18 11:50:58 +0200522{
523 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100524 unsigned int i;
Jens Axboebab3fd52007-04-02 10:34:18 +0200525 int err = 0;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200526
527 for_each_file(td, f, i) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100528 dprint(FD_FILE, "get file size for %p/%d/%p\n", f, i,
529 f->file_name);
Jens Axboe9efef3c2008-03-06 10:26:25 +0100530
Jens Axboe99a47c62009-04-07 22:20:56 +0200531 if (td_io_get_file_size(td, f)) {
Jens Axboe40b44f42007-04-11 12:43:35 +0200532 if (td->error != ENOENT) {
533 log_err("%s\n", td->verror);
534 err = 1;
535 }
Jens Axboe541d66d2007-03-27 19:50:11 +0200536 clear_error(td);
Jens Axboe07eb79d2007-04-12 13:02:38 +0200537 }
Jens Axboe409b3412007-03-28 09:57:01 +0200538
539 if (f->real_file_size == -1ULL && td->o.size)
540 f->real_file_size = td->o.size / td->o.nr_files;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200541 }
Jens Axboebab3fd52007-04-02 10:34:18 +0200542
543 return err;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200544}
545
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200546struct fio_mount {
547 struct flist_head list;
548 const char *base;
549 char __base[256];
550 unsigned int key;
551};
552
553/*
554 * Get free number of bytes for each file on each unique mount.
555 */
556static unsigned long long get_fs_free_counts(struct thread_data *td)
557{
558 struct flist_head *n, *tmp;
Jens Axboe68b03162010-06-24 09:22:41 +0200559 unsigned long long ret = 0;
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200560 struct fio_mount *fm;
561 FLIST_HEAD(list);
562 struct fio_file *f;
563 unsigned int i;
564
565 for_each_file(td, f, i) {
566 struct stat sb;
567 char buf[256];
568
Jens Axboe4ccdccd2010-06-24 10:27:22 +0200569 if (f->filetype == FIO_TYPE_BD || f->filetype == FIO_TYPE_CHAR) {
570 if (f->real_file_size != -1ULL)
571 ret += f->real_file_size;
Jens Axboe68b03162010-06-24 09:22:41 +0200572 continue;
573 } else if (f->filetype != FIO_TYPE_FILE)
574 continue;
575
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200576 strcpy(buf, f->file_name);
577
578 if (stat(buf, &sb) < 0) {
579 if (errno != ENOENT)
580 break;
581 strcpy(buf, ".");
582 if (stat(buf, &sb) < 0)
583 break;
584 }
585
586 fm = NULL;
587 flist_for_each(n, &list) {
588 fm = flist_entry(n, struct fio_mount, list);
589 if (fm->key == sb.st_dev)
590 break;
591
592 fm = NULL;
593 }
594
595 if (fm)
596 continue;
597
598 fm = malloc(sizeof(*fm));
599 strcpy(fm->__base, buf);
600 fm->base = basename(fm->__base);
601 fm->key = sb.st_dev;
602 flist_add(&fm->list, &list);
603 }
604
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200605 flist_for_each_safe(n, tmp, &list) {
606 unsigned long long sz;
607
608 fm = flist_entry(n, struct fio_mount, list);
609 flist_del(&fm->list);
610
611 sz = get_fs_size(fm->base);
612 if (sz && sz != -1ULL)
613 ret += sz;
614
615 free(fm);
616 }
617
618 return ret;
619}
620
Jens Axboe7bb48f82007-03-27 15:30:28 +0200621/*
622 * Open the files and setup files sizes, creating files if necessary.
623 */
624int setup_files(struct thread_data *td)
625{
626 unsigned long long total_size, extend_size;
627 struct fio_file *f;
628 unsigned int i;
Jens Axboe000b0802007-03-27 15:56:10 +0200629 int err = 0, need_extend;
Jens Axboe53cdc682006-10-18 11:50:58 +0200630
Jens Axboeee56ad52008-02-01 10:30:20 +0100631 dprint(FD_FILE, "setup files\n");
632
Jens Axboe691c8fb2008-03-07 14:26:26 +0100633 if (td->o.read_iolog_file)
634 return 0;
635
Jens Axboe53cdc682006-10-18 11:50:58 +0200636 /*
637 * if ioengine defines a setup() method, it's responsible for
Jens Axboe7bb48f82007-03-27 15:30:28 +0200638 * opening the files and setting f->real_file_size to indicate
639 * the valid range for that file.
Jens Axboe53cdc682006-10-18 11:50:58 +0200640 */
641 if (td->io_ops->setup)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200642 err = td->io_ops->setup(td);
643 else
Jens Axboebab3fd52007-04-02 10:34:18 +0200644 err = get_file_sizes(td);
Jens Axboe53cdc682006-10-18 11:50:58 +0200645
Jens Axboef1027062006-10-20 10:14:01 +0200646 if (err)
647 return err;
648
Jens Axboe0a7eb122006-10-20 10:36:42 +0200649 /*
Jens Axboe7bb48f82007-03-27 15:30:28 +0200650 * check sizes. if the files/devices do not exist and the size
651 * isn't passed to fio, abort.
Jens Axboe0a7eb122006-10-20 10:36:42 +0200652 */
Jens Axboe7bb48f82007-03-27 15:30:28 +0200653 total_size = 0;
654 for_each_file(td, f, i) {
655 if (f->real_file_size == -1ULL)
656 total_size = -1ULL;
657 else
658 total_size += f->real_file_size;
659 }
Jens Axboe0a7eb122006-10-20 10:36:42 +0200660
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200661 if (td->o.fill_device)
662 td->fill_device_size = get_fs_free_counts(td);
663
Jens Axboe7bb48f82007-03-27 15:30:28 +0200664 /*
665 * device/file sizes are zero and no size given, punt
666 */
Jens Axboe1f809d12007-10-25 18:34:02 +0200667 if ((!total_size || total_size == -1ULL) && !td->o.size &&
Shawn Lewisaa31f1f2008-01-11 09:45:11 +0100668 !(td->io_ops->flags & FIO_NOIO) && !td->o.fill_device) {
Jens Axboe7bb48f82007-03-27 15:30:28 +0200669 log_err("%s: you need to specify size=\n", td->o.name);
Jens Axboee1161c32007-02-22 19:36:48 +0100670 td_verror(td, EINVAL, "total_file_size");
Jens Axboe53cdc682006-10-18 11:50:58 +0200671 return 1;
672 }
673
Jens Axboe7bb48f82007-03-27 15:30:28 +0200674 /*
675 * now file sizes are known, so we can set ->io_size. if size= is
676 * not given, ->io_size is just equal to ->real_file_size. if size
677 * is given, ->io_size is size / nr_files.
678 */
679 extend_size = total_size = 0;
680 need_extend = 0;
681 for_each_file(td, f, i) {
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200682 f->file_offset = td->o.start_offset;
683
Jens Axboe7bb48f82007-03-27 15:30:28 +0200684 if (!td->o.file_size_low) {
685 /*
686 * no file size range given, file size is equal to
687 * total size divided by number of files. if that is
688 * zero, set it to the real file size.
689 */
690 f->io_size = td->o.size / td->o.nr_files;
Jens Axboe65bdb102008-01-24 13:13:12 +0100691 if (!f->io_size)
Jens Axboe273f8c92008-01-25 14:02:15 +0100692 f->io_size = f->real_file_size - f->file_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200693 } else if (f->real_file_size < td->o.file_size_low ||
694 f->real_file_size > td->o.file_size_high) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100695 if (f->file_offset > td->o.file_size_low)
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200696 goto err_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200697 /*
698 * file size given. if it's fixed, use that. if it's a
699 * range, generate a random size in-between.
700 */
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100701 if (td->o.file_size_low == td->o.file_size_high) {
702 f->io_size = td->o.file_size_low
703 - f->file_offset;
704 } else {
705 f->io_size = get_rand_file_size(td)
706 - f->file_offset;
707 }
Jens Axboe65bdb102008-01-24 13:13:12 +0100708 } else
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200709 f->io_size = f->real_file_size - f->file_offset;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200710
711 if (f->io_size == -1ULL)
712 total_size = -1ULL;
713 else
714 total_size += f->io_size;
715
716 if (f->filetype == FIO_TYPE_FILE &&
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200717 (f->io_size + f->file_offset) > f->real_file_size &&
Jens Axboe7bb48f82007-03-27 15:30:28 +0200718 !(td->io_ops->flags & FIO_DISKLESSIO)) {
Jens Axboe814452b2009-03-04 12:53:13 +0100719 if (!td->o.create_on_open) {
720 need_extend++;
721 extend_size += (f->io_size + f->file_offset);
722 } else
723 f->real_file_size = f->io_size + f->file_offset;
Jens Axboed6aed792009-06-03 08:41:15 +0200724 fio_file_set_extend(f);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100725 }
Jens Axboe7bb48f82007-03-27 15:30:28 +0200726 }
727
ljzhang,Yaxin Hu,Jianchao Tang22982902007-07-27 13:21:28 +0200728 if (!td->o.size || td->o.size > total_size)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200729 td->o.size = total_size;
730
731 /*
732 * See if we need to extend some files
733 */
734 if (need_extend) {
735 temp_stall_ts = 1;
Shaozhi Shawn Yea7ba8c52008-09-10 09:04:18 +0200736 if (!terse_output)
737 log_info("%s: Laying out IO file(s) (%u file(s) /"
Bruce Cran0f2152c2010-12-15 10:33:03 +0100738 " %lluMB)\n", td->o.name, need_extend,
Shaozhi Shawn Yea7ba8c52008-09-10 09:04:18 +0200739 extend_size >> 20);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200740
741 for_each_file(td, f, i) {
Jens Axboe5e0074c2009-06-02 21:40:09 +0200742 unsigned long long old_len = -1ULL, extend_len = -1ULL;
Jens Axboe3baddf22008-04-04 11:10:30 +0200743
Jens Axboed6aed792009-06-03 08:41:15 +0200744 if (!fio_file_extend(f))
Jens Axboe7bb48f82007-03-27 15:30:28 +0200745 continue;
746
Jens Axboe409b3412007-03-28 09:57:01 +0200747 assert(f->filetype == FIO_TYPE_FILE);
Jens Axboed6aed792009-06-03 08:41:15 +0200748 fio_file_clear_extend(f);
Jens Axboe5e0074c2009-06-02 21:40:09 +0200749 if (!td->o.fill_device) {
750 old_len = f->real_file_size;
Jens Axboe0b9d69e2009-09-11 22:29:54 +0200751 extend_len = f->io_size + f->file_offset -
752 old_len;
Jens Axboe5e0074c2009-06-02 21:40:09 +0200753 }
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200754 f->real_file_size = (f->io_size + f->file_offset);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200755 err = extend_file(td, f);
756 if (err)
757 break;
Jens Axboe5e0074c2009-06-02 21:40:09 +0200758
Jens Axboe3baddf22008-04-04 11:10:30 +0200759 err = __file_invalidate_cache(td, f, old_len,
760 extend_len);
761 close(f->fd);
762 f->fd = -1;
763 if (err)
764 break;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200765 }
766 temp_stall_ts = 0;
767 }
768
769 if (err)
770 return err;
771
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100772 if (!td->o.zone_size)
Jens Axboe7bb48f82007-03-27 15:30:28 +0200773 td->o.zone_size = td->o.size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200774
Jens Axboeea966f82007-09-03 10:09:37 +0200775 /*
776 * iolog already set the total io size, if we read back
777 * stored entries.
778 */
779 if (!td->o.read_iolog_file)
780 td->total_io_size = td->o.size * td->o.loops;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200781 return 0;
ljzhang,Yaxin Hu,Jianchao Tangbcdedd02007-07-27 13:28:26 +0200782err_offset:
783 log_err("%s: you need to specify valid offset=\n", td->o.name);
784 return 1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200785}
786
Zhang, Yanminafad68f2009-05-20 11:30:55 +0200787int pre_read_files(struct thread_data *td)
788{
789 struct fio_file *f;
790 unsigned int i;
791
792 dprint(FD_FILE, "pre_read files\n");
793
794 for_each_file(td, f, i) {
795 pre_read_file(td, f);
796 }
797
798 return 1;
799}
800
Jens Axboe68727072007-03-15 20:49:25 +0100801int init_random_map(struct thread_data *td)
802{
Jens Axboe509eab12007-12-14 12:42:53 +0100803 unsigned long long blocks, num_maps;
Jens Axboe68727072007-03-15 20:49:25 +0100804 struct fio_file *f;
805 unsigned int i;
806
Jens Axboede8dd112008-03-01 15:34:44 +0100807 if (td->o.norandommap || !td_random(td))
Jens Axboe68727072007-03-15 20:49:25 +0100808 return 0;
809
810 for_each_file(td, f, i) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100811 blocks = (f->real_file_size + td->o.rw_min_bs - 1) /
812 (unsigned long long) td->o.rw_min_bs;
813 num_maps = (blocks + BLOCKS_PER_MAP - 1) /
814 (unsigned long long) BLOCKS_PER_MAP;
Jens Axboede605662008-05-31 00:21:12 +0200815 f->file_map = smalloc(num_maps * sizeof(int));
Jens Axboe303032a2008-03-26 10:11:10 +0100816 if (f->file_map) {
817 f->num_maps = num_maps;
818 continue;
Jens Axboe68727072007-03-15 20:49:25 +0100819 }
Jens Axboe2b386d22008-03-26 10:32:57 +0100820 if (!td->o.softrandommap) {
Jens Axboe68727072007-03-15 20:49:25 +0100821 log_err("fio: failed allocating random map. If running"
822 " a large number of jobs, try the 'norandommap'"
Jens Axboe2b386d22008-03-26 10:32:57 +0100823 " option or set 'softrandommap'. Or give"
824 " a larger --alloc-size to fio.\n");
Jens Axboe68727072007-03-15 20:49:25 +0100825 return 1;
826 }
Jens Axboe303032a2008-03-26 10:11:10 +0100827
828 log_info("fio: file %s failed allocating random map. Running "
829 "job without.\n", f->file_name);
830 f->num_maps = 0;
Jens Axboe68727072007-03-15 20:49:25 +0100831 }
832
833 return 0;
834}
835
Jens Axboe53cdc682006-10-18 11:50:58 +0200836void close_files(struct thread_data *td)
837{
Jens Axboe0ab8db82006-10-18 17:16:23 +0200838 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100839 unsigned int i;
Jens Axboe53cdc682006-10-18 11:50:58 +0200840
Jens Axboe2be3eec2009-06-10 09:05:13 +0200841 for_each_file(td, f, i) {
842 if (fio_file_open(f))
843 td_io_close_file(td, f);
844 }
Jens Axboe24ffd2c2008-03-01 15:47:08 +0100845}
846
847void close_and_free_files(struct thread_data *td)
848{
849 struct fio_file *f;
850 unsigned int i;
851
Jens Axboeee56ad52008-02-01 10:30:20 +0100852 dprint(FD_FILE, "close files\n");
853
Jens Axboe0ab8db82006-10-18 17:16:23 +0200854 for_each_file(td, f, i) {
Jens Axboeffdfabd2008-03-26 09:18:14 +0100855 if (td->o.unlink && f->filetype == FIO_TYPE_FILE) {
856 dprint(FD_FILE, "free unlink %s\n", f->file_name);
Jens Axboe132ad462006-11-23 12:23:12 +0100857 unlink(f->file_name);
Jens Axboeffdfabd2008-03-26 09:18:14 +0100858 }
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100859
Jens Axboe22a57ba2009-06-09 14:34:35 +0200860 if (fio_file_open(f))
861 td_io_close_file(td, f);
862
Shaozhi Shawn Yeb9fbcf22008-09-10 09:05:17 +0200863 remove_file_hash(f);
Jens Axboeb3dc7f02007-03-09 12:52:15 +0100864
Jens Axboef17c4392008-03-01 18:40:46 +0100865 sfree(f->file_name);
Jens Axboefa1da862007-03-23 15:20:54 +0100866 f->file_name = NULL;
Jens Axboe61eb3132010-03-31 20:05:59 +0200867 sfree(f->file_map);
868 f->file_map = NULL;
Jens Axboe78d99e62008-03-01 18:41:50 +0100869 sfree(f);
Jens Axboe53cdc682006-10-18 11:50:58 +0200870 }
Jens Axboeb4a6a592006-10-20 13:54:47 +0200871
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100872 td->o.filename = NULL;
Jens Axboecade3ef2007-03-23 15:29:45 +0100873 free(td->files);
Jens Axboe9efef3c2008-03-06 10:26:25 +0100874 td->files_index = 0;
Jens Axboeb4a6a592006-10-20 13:54:47 +0200875 td->files = NULL;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100876 td->o.nr_files = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200877}
Jens Axboeaf52b342007-03-13 10:07:47 +0100878
Jens Axboee3bab462007-03-13 11:22:09 +0100879static void get_file_type(struct fio_file *f)
Jens Axboeaf52b342007-03-13 10:07:47 +0100880{
881 struct stat sb;
882
Jens Axboe66159822007-04-16 21:54:24 +0200883 if (!strcmp(f->file_name, "-"))
884 f->filetype = FIO_TYPE_PIPE;
885 else
886 f->filetype = FIO_TYPE_FILE;
Jens Axboeaf52b342007-03-13 10:07:47 +0100887
Jens Axboeb30d3952010-02-06 23:36:26 +0100888 if (!stat(f->file_name, &sb)) {
Bruce Cranecc314b2011-01-04 10:59:30 +0100889 /* \\.\ is the device namespace in Windows, where every file is
890 * a block device */
891 if (S_ISBLK(sb.st_mode) || strncmp(f->file_name, "\\\\.\\", 4) == 0)
Jens Axboeaf52b342007-03-13 10:07:47 +0100892 f->filetype = FIO_TYPE_BD;
893 else if (S_ISCHR(sb.st_mode))
894 f->filetype = FIO_TYPE_CHAR;
Jens Axboeb5605e92007-04-16 20:42:33 +0200895 else if (S_ISFIFO(sb.st_mode))
896 f->filetype = FIO_TYPE_PIPE;
Jens Axboeaf52b342007-03-13 10:07:47 +0100897 }
898}
899
Jens Axboef29b25a2007-07-23 08:56:43 +0200900int add_file(struct thread_data *td, const char *fname)
Jens Axboeaf52b342007-03-13 10:07:47 +0100901{
Jens Axboe7b4e4fe2007-03-13 12:51:40 +0100902 int cur_files = td->files_index;
Jens Axboebd0ee742007-03-23 14:26:23 +0100903 char file_name[PATH_MAX];
Jens Axboeaf52b342007-03-13 10:07:47 +0100904 struct fio_file *f;
Jens Axboebd0ee742007-03-23 14:26:23 +0100905 int len = 0;
Jens Axboeaf52b342007-03-13 10:07:47 +0100906
Jens Axboeee56ad52008-02-01 10:30:20 +0100907 dprint(FD_FILE, "add file %s\n", fname);
908
Jens Axboef17c4392008-03-01 18:40:46 +0100909 f = smalloc(sizeof(*f));
Jens Axboec48c0be2008-07-31 19:55:02 +0200910 if (!f) {
911 log_err("fio: smalloc OOM\n");
912 assert(0);
913 }
Jens Axboe0b9d69e2009-09-11 22:29:54 +0200914
Jens Axboeaf52b342007-03-13 10:07:47 +0100915 f->fd = -1;
Jens Axboe38dad622010-07-20 14:46:00 -0600916 fio_file_reset(f);
Jens Axboebd0ee742007-03-23 14:26:23 +0100917
Jens Axboefc99bc02009-03-04 15:27:42 +0100918 if (td->files_size <= td->files_index) {
Carl Henrik Lunde4b341fc2009-04-20 08:41:37 +0200919 int new_size = td->o.nr_files + 1;
Jens Axboe126d65c2008-03-01 18:04:31 +0100920
Jens Axboefc99bc02009-03-04 15:27:42 +0100921 dprint(FD_FILE, "resize file array to %d files\n", new_size);
922
923 td->files = realloc(td->files, new_size * sizeof(f));
Jens Axboefc99bc02009-03-04 15:27:42 +0100924 td->files_size = new_size;
925 }
Jens Axboe8bb76792009-03-04 15:37:52 +0100926 td->files[cur_files] = f;
Jens Axboe126d65c2008-03-01 18:04:31 +0100927
Jens Axboe07eb79d2007-04-12 13:02:38 +0200928 /*
929 * init function, io engine may not be loaded yet
930 */
931 if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO))
932 f->real_file_size = -1ULL;
933
Jens Axboebd0ee742007-03-23 14:26:23 +0100934 if (td->o.directory)
935 len = sprintf(file_name, "%s/", td->o.directory);
936
937 sprintf(file_name + len, "%s", fname);
Jens Axboef17c4392008-03-01 18:40:46 +0100938 f->file_name = smalloc_strdup(file_name);
Jens Axboec48c0be2008-07-31 19:55:02 +0200939 if (!f->file_name) {
940 log_err("fio: smalloc OOM\n");
941 assert(0);
942 }
Jens Axboe0b9d69e2009-09-11 22:29:54 +0200943
Jens Axboee3bab462007-03-13 11:22:09 +0100944 get_file_type(f);
Jens Axboeaf52b342007-03-13 10:07:47 +0100945
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100946 switch (td->o.file_lock_mode) {
947 case FILE_LOCK_NONE:
948 break;
949 case FILE_LOCK_READWRITE:
950 f->lock = fio_mutex_rw_init();
951 break;
952 case FILE_LOCK_EXCLUSIVE:
953 f->lock = fio_mutex_init(1);
954 break;
955 default:
956 log_err("fio: unknown lock mode: %d\n", td->o.file_lock_mode);
957 assert(0);
958 }
Jens Axboe29c13492008-03-01 19:25:20 +0100959
Jens Axboe7b4e4fe2007-03-13 12:51:40 +0100960 td->files_index++;
Jens Axboe15494412007-03-13 11:52:47 +0100961 if (f->filetype == FIO_TYPE_FILE)
962 td->nr_normal_files++;
Jens Axboef29b25a2007-07-23 08:56:43 +0200963
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100964 dprint(FD_FILE, "file %p \"%s\" added at %d\n", f, f->file_name,
965 cur_files);
Jens Axboe9efef3c2008-03-06 10:26:25 +0100966
Jens Axboef29b25a2007-07-23 08:56:43 +0200967 return cur_files;
Jens Axboeaf52b342007-03-13 10:07:47 +0100968}
Jens Axboe0ad920e2007-03-13 11:06:45 +0100969
Jens Axboe49ffb4a2010-08-24 15:01:37 +0200970int add_file_exclusive(struct thread_data *td, const char *fname)
971{
972 struct fio_file *f;
973 unsigned int i;
974
975 for_each_file(td, f, i) {
976 if (!strcmp(f->file_name, fname))
977 return i;
978 }
979
980 return add_file(td, fname);
981}
982
Jens Axboe0ad920e2007-03-13 11:06:45 +0100983void get_file(struct fio_file *f)
984{
Jens Axboe8172fe92008-02-01 21:51:02 +0100985 dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references);
Jens Axboed6aed792009-06-03 08:41:15 +0200986 assert(fio_file_open(f));
Jens Axboe0ad920e2007-03-13 11:06:45 +0100987 f->references++;
988}
989
Jens Axboe6977bcd2008-03-01 15:55:36 +0100990int put_file(struct thread_data *td, struct fio_file *f)
Jens Axboe0ad920e2007-03-13 11:06:45 +0100991{
Jens Axboe98e1ac42008-03-06 11:56:48 +0100992 int f_ret = 0, ret = 0;
Jens Axboe6977bcd2008-03-01 15:55:36 +0100993
Jens Axboe8172fe92008-02-01 21:51:02 +0100994 dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references);
Jens Axboeee56ad52008-02-01 10:30:20 +0100995
Jens Axboe22a57ba2009-06-09 14:34:35 +0200996 if (!fio_file_open(f)) {
997 assert(f->fd == -1);
Jens Axboe6977bcd2008-03-01 15:55:36 +0100998 return 0;
Jens Axboe22a57ba2009-06-09 14:34:35 +0200999 }
Jens Axboe0ad920e2007-03-13 11:06:45 +01001000
1001 assert(f->references);
1002 if (--f->references)
Jens Axboe6977bcd2008-03-01 15:55:36 +01001003 return 0;
Jens Axboe0ad920e2007-03-13 11:06:45 +01001004
Jens Axboed424d4d2007-04-17 09:05:10 +02001005 if (should_fsync(td) && td->o.fsync_on_close)
Jens Axboe98e1ac42008-03-06 11:56:48 +01001006 f_ret = fsync(f->fd);
Jens Axboeebb14152007-03-13 14:42:15 +01001007
Jens Axboe0ad920e2007-03-13 11:06:45 +01001008 if (td->io_ops->close_file)
Jens Axboe6977bcd2008-03-01 15:55:36 +01001009 ret = td->io_ops->close_file(td, f);
Jens Axboe1020a132007-03-29 11:15:06 +02001010
Jens Axboe98e1ac42008-03-06 11:56:48 +01001011 if (!ret)
Jens Axboea5fb4612008-05-28 10:54:01 +02001012 ret = f_ret;
Jens Axboe98e1ac42008-03-06 11:56:48 +01001013
Jens Axboe0ad920e2007-03-13 11:06:45 +01001014 td->nr_open_files--;
Jens Axboed6aed792009-06-03 08:41:15 +02001015 fio_file_clear_open(f);
Jens Axboe22a57ba2009-06-09 14:34:35 +02001016 assert(f->fd == -1);
Jens Axboe6977bcd2008-03-01 15:55:36 +01001017 return ret;
Jens Axboe0ad920e2007-03-13 11:06:45 +01001018}
Jens Axboebbf6b542007-03-13 15:28:55 +01001019
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001020void lock_file(struct thread_data *td, struct fio_file *f, enum fio_ddir ddir)
Jens Axboeb2bd2bd2008-03-01 18:19:52 +01001021{
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001022 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1023 return;
Jens Axboe29c13492008-03-01 19:25:20 +01001024
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001025 if (f->lock_owner == td && f->lock_batch--)
1026 return;
1027
1028 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
1029 if (ddir == DDIR_READ)
1030 fio_mutex_down_read(f->lock);
1031 else
1032 fio_mutex_down_write(f->lock);
1033 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1034 fio_mutex_down(f->lock);
1035
1036 f->lock_owner = td;
1037 f->lock_batch = td->o.lockfile_batch;
1038 f->lock_ddir = ddir;
1039}
1040
1041void unlock_file(struct thread_data *td, struct fio_file *f)
1042{
1043 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1044 return;
1045 if (f->lock_batch)
1046 return;
1047
1048 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
1049 const int is_read = f->lock_ddir == DDIR_READ;
1050 int val = fio_mutex_getval(f->lock);
1051
1052 if ((is_read && val == 1) || (!is_read && val == -1))
1053 f->lock_owner = NULL;
1054
1055 if (is_read)
1056 fio_mutex_up_read(f->lock);
1057 else
1058 fio_mutex_up_write(f->lock);
1059 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE) {
1060 int val = fio_mutex_getval(f->lock);
1061
1062 if (val == 0)
1063 f->lock_owner = NULL;
1064
1065 fio_mutex_up(f->lock);
Jens Axboe29c13492008-03-01 19:25:20 +01001066 }
Jens Axboeb2bd2bd2008-03-01 18:19:52 +01001067}
1068
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001069void unlock_file_all(struct thread_data *td, struct fio_file *f)
Jens Axboeb2bd2bd2008-03-01 18:19:52 +01001070{
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001071 if (f->lock_owner != td)
1072 return;
Jens Axboe29c13492008-03-01 19:25:20 +01001073
Jens Axboe4d4e80f2008-03-04 10:18:56 +01001074 f->lock_batch = 0;
1075 unlock_file(td, f);
Jens Axboeb2bd2bd2008-03-01 18:19:52 +01001076}
1077
Jens Axboebbf6b542007-03-13 15:28:55 +01001078static int recurse_dir(struct thread_data *td, const char *dirname)
1079{
1080 struct dirent *dir;
1081 int ret = 0;
1082 DIR *D;
1083
1084 D = opendir(dirname);
1085 if (!D) {
Jens Axboe0ddb2702007-03-27 19:43:53 +02001086 char buf[FIO_VERROR_SIZE];
1087
1088 snprintf(buf, FIO_VERROR_SIZE - 1, "opendir(%s)", dirname);
1089 td_verror(td, errno, buf);
Jens Axboebbf6b542007-03-13 15:28:55 +01001090 return 1;
1091 }
1092
1093 while ((dir = readdir(D)) != NULL) {
1094 char full_path[PATH_MAX];
1095 struct stat sb;
1096
Jens Axboee85b2b82007-03-14 09:39:06 +01001097 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
1098 continue;
Jens Axboe96d32d52007-03-14 09:16:09 +01001099
Jens Axboebbf6b542007-03-13 15:28:55 +01001100 sprintf(full_path, "%s/%s", dirname, dir->d_name);
1101
1102 if (lstat(full_path, &sb) == -1) {
1103 if (errno != ENOENT) {
1104 td_verror(td, errno, "stat");
1105 return 1;
1106 }
1107 }
1108
1109 if (S_ISREG(sb.st_mode)) {
1110 add_file(td, full_path);
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001111 td->o.nr_files++;
Jens Axboebbf6b542007-03-13 15:28:55 +01001112 continue;
1113 }
Jens Axboe0ddb2702007-03-27 19:43:53 +02001114 if (!S_ISDIR(sb.st_mode))
1115 continue;
Jens Axboebbf6b542007-03-13 15:28:55 +01001116
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001117 ret = recurse_dir(td, full_path);
1118 if (ret)
Jens Axboebbf6b542007-03-13 15:28:55 +01001119 break;
1120 }
1121
1122 closedir(D);
1123 return ret;
1124}
1125
1126int add_dir_files(struct thread_data *td, const char *path)
1127{
Jens Axboe0ddb2702007-03-27 19:43:53 +02001128 int ret = recurse_dir(td, path);
1129
1130 if (!ret)
1131 log_info("fio: opendir added %d files\n", td->o.nr_files);
1132
1133 return ret;
Jens Axboebbf6b542007-03-13 15:28:55 +01001134}
Jens Axboecade3ef2007-03-23 15:29:45 +01001135
1136void dup_files(struct thread_data *td, struct thread_data *org)
1137{
1138 struct fio_file *f;
1139 unsigned int i;
Jens Axboe9efef3c2008-03-06 10:26:25 +01001140
1141 dprint(FD_FILE, "dup files: %d\n", org->files_index);
Jens Axboecade3ef2007-03-23 15:29:45 +01001142
1143 if (!org->files)
1144 return;
1145
Jens Axboe9efef3c2008-03-06 10:26:25 +01001146 td->files = malloc(org->files_index * sizeof(f));
Jens Axboecade3ef2007-03-23 15:29:45 +01001147
Jens Axboe9efef3c2008-03-06 10:26:25 +01001148 for_each_file(org, f, i) {
Jens Axboeb0fe4212008-03-01 18:22:27 +01001149 struct fio_file *__f;
1150
Jens Axboef17c4392008-03-01 18:40:46 +01001151 __f = smalloc(sizeof(*__f));
Jens Axboec48c0be2008-07-31 19:55:02 +02001152 if (!__f) {
1153 log_err("fio: smalloc OOM\n");
1154 assert(0);
1155 }
Jens Axboe22a57ba2009-06-09 14:34:35 +02001156 __f->fd = -1;
Jens Axboe38dad622010-07-20 14:46:00 -06001157 fio_file_reset(__f);
Jens Axboe0b9d69e2009-09-11 22:29:54 +02001158
Aaron Carrollbc3456f2008-06-25 09:20:37 +02001159 if (f->file_name) {
Jens Axboef17c4392008-03-01 18:40:46 +01001160 __f->file_name = smalloc_strdup(f->file_name);
Jens Axboec48c0be2008-07-31 19:55:02 +02001161 if (!__f->file_name) {
1162 log_err("fio: smalloc OOM\n");
1163 assert(0);
1164 }
Jens Axboe0b9d69e2009-09-11 22:29:54 +02001165
Aaron Carrollbc3456f2008-06-25 09:20:37 +02001166 __f->filetype = f->filetype;
1167 }
Jens Axboeb0fe4212008-03-01 18:22:27 +01001168
1169 td->files[i] = __f;
Jens Axboecade3ef2007-03-23 15:29:45 +01001170 }
1171}
Jens Axboef29b25a2007-07-23 08:56:43 +02001172
1173/*
1174 * Returns the index that matches the filename, or -1 if not there
1175 */
1176int get_fileno(struct thread_data *td, const char *fname)
1177{
1178 struct fio_file *f;
1179 unsigned int i;
1180
1181 for_each_file(td, f, i)
1182 if (!strcmp(f->file_name, fname))
1183 return i;
1184
1185 return -1;
1186}
1187
1188/*
1189 * For log usage, where we add/open/close files automatically
1190 */
1191void free_release_files(struct thread_data *td)
1192{
1193 close_files(td);
1194 td->files_index = 0;
1195 td->nr_normal_files = 0;
1196}