blob: abcdc1d05da17051073124457d7be62ab461e124 [file] [log] [blame]
Jens Axboe53cdc682006-10-18 11:50:58 +02001#include <unistd.h>
2#include <fcntl.h>
3#include <string.h>
4#include <assert.h>
Jens Axboebbf6b542007-03-13 15:28:55 +01005#include <dirent.h>
Jens Axboe53cdc682006-10-18 11:50:58 +02006#include <sys/stat.h>
7#include <sys/mman.h>
Jens Axboebbf6b542007-03-13 15:28:55 +01008#include <sys/types.h>
Jens Axboe53cdc682006-10-18 11:50:58 +02009
10#include "fio.h"
11#include "os.h"
12
Jens Axboe25205e92006-10-19 20:50:14 +020013/*
14 * Check if the file exists and it's large enough.
15 */
16static int file_ok(struct thread_data *td, struct fio_file *f)
Jens Axboe53cdc682006-10-18 11:50:58 +020017{
Jens Axboeb2a15192006-10-18 14:10:42 +020018 struct stat st;
Jens Axboe53cdc682006-10-18 11:50:58 +020019
Jens Axboeaf52b342007-03-13 10:07:47 +010020 if (f->filetype != FIO_TYPE_FILE ||
Jens Axboeb5af8292007-03-08 12:43:13 +010021 (td->io_ops->flags & FIO_DISKLESSIO))
Jens Axboeb2a15192006-10-18 14:10:42 +020022 return 0;
23
Jens Axboefa01d132007-02-22 14:07:39 +010024 if (lstat(f->file_name, &st) == -1)
Jens Axboe25205e92006-10-19 20:50:14 +020025 return 1;
Jens Axboefa01d132007-02-22 14:07:39 +010026
27 /*
28 * if it's a special file, size is always ok for now
29 */
30 if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))
31 return 0;
32 if (st.st_size < (off_t) f->file_size)
Jens Axboe25205e92006-10-19 20:50:14 +020033 return 1;
34
35 return 0;
36}
37
38static int create_file(struct thread_data *td, struct fio_file *f)
39{
40 unsigned long long left;
41 unsigned int bs;
42 char *b;
43 int r;
Jens Axboeb2a15192006-10-18 14:10:42 +020044
Jens Axboe53cdc682006-10-18 11:50:58 +020045 f->fd = open(f->file_name, O_WRONLY | O_CREAT | O_TRUNC, 0644);
46 if (f->fd < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +010047 td_verror(td, errno, "open");
Jens Axboe53cdc682006-10-18 11:50:58 +020048 return 1;
49 }
50
51 if (ftruncate(f->fd, f->file_size) == -1) {
Jens Axboee1161c32007-02-22 19:36:48 +010052 td_verror(td, errno, "ftruncate");
Jens Axboe53cdc682006-10-18 11:50:58 +020053 goto err;
54 }
55
Jens Axboe40f82982006-10-25 11:21:05 +020056 if (posix_fallocate(f->fd, 0, f->file_size) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +010057 td_verror(td, errno, "posix_fallocate");
Jens Axboe40f82982006-10-25 11:21:05 +020058 goto err;
59 }
60
Jens Axboea00735e2006-11-03 08:58:08 +010061 b = malloc(td->max_bs[DDIR_WRITE]);
62 memset(b, 0, td->max_bs[DDIR_WRITE]);
Jens Axboe53cdc682006-10-18 11:50:58 +020063
64 left = f->file_size;
65 while (left && !td->terminate) {
Jens Axboea00735e2006-11-03 08:58:08 +010066 bs = td->max_bs[DDIR_WRITE];
Jens Axboe53cdc682006-10-18 11:50:58 +020067 if (bs > left)
68 bs = left;
69
70 r = write(f->fd, b, bs);
71
72 if (r == (int) bs) {
73 left -= bs;
74 continue;
75 } else {
76 if (r < 0)
Jens Axboee1161c32007-02-22 19:36:48 +010077 td_verror(td, errno, "write");
Jens Axboe53cdc682006-10-18 11:50:58 +020078 else
Jens Axboee1161c32007-02-22 19:36:48 +010079 td_verror(td, EIO, "write");
Jens Axboe53cdc682006-10-18 11:50:58 +020080
81 break;
82 }
83 }
84
85 if (td->terminate)
86 unlink(f->file_name);
87 else if (td->create_fsync)
88 fsync(f->fd);
89
90 free(b);
91 close(f->fd);
92 f->fd = -1;
93 return 0;
94err:
95 close(f->fd);
96 f->fd = -1;
97 return 1;
98}
99
100static int create_files(struct thread_data *td)
101{
102 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100103 int err, need_create, can_extend;
Jens Axboe96d32d52007-03-14 09:16:09 +0100104 unsigned long long total_file_size;
Jens Axboee407bec2007-03-14 11:21:27 +0100105 unsigned int i, new_files;
Jens Axboe53cdc682006-10-18 11:50:58 +0200106
Jens Axboee407bec2007-03-14 11:21:27 +0100107 new_files = 0;
Jens Axboe96d32d52007-03-14 09:16:09 +0100108 total_file_size = td->total_file_size;
Jens Axboe15494412007-03-13 11:52:47 +0100109 for_each_file(td, f, i) {
Jens Axboe96d32d52007-03-14 09:16:09 +0100110 unsigned long long s;
111
112 f->file_offset = td->start_offset;
113
Jens Axboe15494412007-03-13 11:52:47 +0100114 if (f->filetype != FIO_TYPE_FILE)
115 continue;
116
Jens Axboe96d32d52007-03-14 09:16:09 +0100117 if (f->flags & FIO_FILE_EXISTS) {
118 s = f->file_size;
119 if (s > total_file_size)
120 s = total_file_size;
121
122 total_file_size -= s;
Jens Axboee407bec2007-03-14 11:21:27 +0100123 } else
124 new_files++;
Jens Axboe15494412007-03-13 11:52:47 +0100125 }
Jens Axboef6971252006-11-15 10:00:31 +0100126
Jens Axboe53cdc682006-10-18 11:50:58 +0200127 /*
128 * unless specifically asked for overwrite, let normal io extend it
129 */
Jens Axboe02638822007-03-12 09:25:55 +0100130 can_extend = !td->overwrite && !(td->io_ops->flags & FIO_NOEXTEND);
131 if (can_extend)
Jens Axboe53cdc682006-10-18 11:50:58 +0200132 return 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200133
Jens Axboef1027062006-10-20 10:14:01 +0200134 need_create = 0;
Jens Axboeaf52b342007-03-13 10:07:47 +0100135 for_each_file(td, f, i) {
136 int file_there;
Jens Axboeeff68612007-01-14 05:56:44 +0100137
Jens Axboeaf52b342007-03-13 10:07:47 +0100138 if (f->filetype != FIO_TYPE_FILE)
139 continue;
Jens Axboe96d32d52007-03-14 09:16:09 +0100140 if (f->flags & FIO_FILE_EXISTS)
141 continue;
142
Jens Axboee407bec2007-03-14 11:21:27 +0100143 f->file_size = total_file_size / new_files;
Jens Axboeeff68612007-01-14 05:56:44 +0100144
Jens Axboeaf52b342007-03-13 10:07:47 +0100145 file_there = !file_ok(td, f);
146
147 if (file_there && td_write(td) && !td->overwrite) {
148 unlink(f->file_name);
149 file_there = 0;
Jens Axboeeff68612007-01-14 05:56:44 +0100150 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100151
152 need_create += !file_there;
Jens Axboeeff68612007-01-14 05:56:44 +0100153 }
Jens Axboef1027062006-10-20 10:14:01 +0200154
155 if (!need_create)
156 return 0;
157
Jens Axboe53cdc682006-10-18 11:50:58 +0200158 if (!td->total_file_size) {
159 log_err("Need size for create\n");
Jens Axboee1161c32007-02-22 19:36:48 +0100160 td_verror(td, EINVAL, "file_size");
Jens Axboe53cdc682006-10-18 11:50:58 +0200161 return 1;
162 }
163
Jens Axboe25205e92006-10-19 20:50:14 +0200164 temp_stall_ts = 1;
Jens Axboe1e97cce2006-12-05 11:44:16 +0100165 fprintf(f_out, "%s: Laying out IO file(s) (%u x %LuMiB == %LuMiB)\n",
Jens Axboee407bec2007-03-14 11:21:27 +0100166 td->name, new_files,
167 (total_file_size >> 20) / new_files,
168 total_file_size >> 20);
Jens Axboe25205e92006-10-19 20:50:14 +0200169
170 err = 0;
171 for_each_file(td, f, i) {
Jens Axboe9b031dc2006-12-15 16:01:54 +0100172 /*
173 * Only unlink files that we created.
174 */
Jens Axboef11bd942007-03-13 10:16:34 +0100175 f->flags &= ~FIO_FILE_UNLINK;
Jens Axboe25205e92006-10-19 20:50:14 +0200176 if (file_ok(td, f)) {
Jens Axboef11bd942007-03-13 10:16:34 +0100177 if (td->unlink)
178 f->flags |= FIO_FILE_UNLINK;
179
Jens Axboe25205e92006-10-19 20:50:14 +0200180 err = create_file(td, f);
181 if (err)
182 break;
183 }
184 }
185
Jens Axboe53cdc682006-10-18 11:50:58 +0200186 temp_stall_ts = 0;
187 return err;
188}
189
190static int file_size(struct thread_data *td, struct fio_file *f)
191{
192 struct stat st;
193
194 if (td->overwrite) {
195 if (fstat(f->fd, &st) == -1) {
Jens Axboee1161c32007-02-22 19:36:48 +0100196 td_verror(td, errno, "fstat");
Jens Axboe53cdc682006-10-18 11:50:58 +0200197 return 1;
198 }
199
200 f->real_file_size = st.st_size;
201
202 if (!f->file_size || f->file_size > f->real_file_size)
203 f->file_size = f->real_file_size;
Jens Axboe745508d2007-01-08 10:25:51 +0100204 } else
205 f->real_file_size = f->file_size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200206
Jens Axboe53cdc682006-10-18 11:50:58 +0200207 return 0;
208}
209
210static int bdev_size(struct thread_data *td, struct fio_file *f)
211{
212 unsigned long long bytes;
213 int r;
214
215 r = blockdev_size(f->fd, &bytes);
216 if (r) {
Jens Axboee1161c32007-02-22 19:36:48 +0100217 td_verror(td, r, "blockdev_size");
Jens Axboe53cdc682006-10-18 11:50:58 +0200218 return 1;
219 }
220
221 f->real_file_size = bytes;
222
223 /*
224 * no extend possibilities, so limit size to device size if too large
225 */
226 if (!f->file_size || f->file_size > f->real_file_size)
227 f->file_size = f->real_file_size;
228
229 f->file_size -= f->file_offset;
230 return 0;
231}
232
233static int get_file_size(struct thread_data *td, struct fio_file *f)
234{
235 int ret = 0;
236
Jens Axboe96d32d52007-03-14 09:16:09 +0100237 if (f->filetype == FIO_TYPE_FILE) {
238 if (!(f->flags & FIO_FILE_EXISTS))
239 ret = file_size(td, f);
240 } else if (f->filetype == FIO_TYPE_BD)
Jens Axboe53cdc682006-10-18 11:50:58 +0200241 ret = bdev_size(td, f);
242 else
243 f->real_file_size = -1;
244
245 if (ret)
246 return ret;
247
248 if (f->file_offset > f->real_file_size) {
249 log_err("%s: offset extends end (%Lu > %Lu)\n", td->name, f->file_offset, f->real_file_size);
250 return 1;
251 }
252
Jens Axboe53cdc682006-10-18 11:50:58 +0200253 return 0;
254}
255
Jens Axboee5b401d2006-10-18 16:03:40 +0200256int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
257{
258 int ret = 0;
259
Jens Axboeda86ccb2007-03-08 12:49:37 +0100260 if (td->odirect)
Jens Axboeb5af8292007-03-08 12:43:13 +0100261 return 0;
262
Jens Axboee5b401d2006-10-18 16:03:40 +0200263 /*
264 * FIXME: add blockdev flushing too
265 */
Jens Axboeb5af8292007-03-08 12:43:13 +0100266 if (f->mmap)
Jens Axboee5b401d2006-10-18 16:03:40 +0200267 ret = madvise(f->mmap, f->file_size, MADV_DONTNEED);
Jens Axboeaf52b342007-03-13 10:07:47 +0100268 else if (f->filetype == FIO_TYPE_FILE) {
Jens Axboeb5af8292007-03-08 12:43:13 +0100269 ret = fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_DONTNEED);
Jens Axboeaf52b342007-03-13 10:07:47 +0100270 } else if (f->filetype == FIO_TYPE_BD) {
Jens Axboeb5af8292007-03-08 12:43:13 +0100271 ret = blockdev_invalidate_cache(f->fd);
Jens Axboeaf52b342007-03-13 10:07:47 +0100272 } else if (f->filetype == FIO_TYPE_CHAR)
Jens Axboee5b401d2006-10-18 16:03:40 +0200273 ret = 0;
274
275 if (ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100276 td_verror(td, errno, "invalidate_cache");
Jens Axboee5b401d2006-10-18 16:03:40 +0200277 return 1;
278 }
279
Jens Axboead2da602007-02-26 12:33:27 +0100280 return ret;
Jens Axboee5b401d2006-10-18 16:03:40 +0200281}
282
Jens Axboeb5af8292007-03-08 12:43:13 +0100283void generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
Jens Axboe53cdc682006-10-18 11:50:58 +0200284{
Jens Axboeb5af8292007-03-08 12:43:13 +0100285 close(f->fd);
286 f->fd = -1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200287}
288
Jens Axboeb5af8292007-03-08 12:43:13 +0100289int generic_open_file(struct thread_data *td, struct fio_file *f)
Jens Axboe53cdc682006-10-18 11:50:58 +0200290{
Jens Axboe53cdc682006-10-18 11:50:58 +0200291 int flags = 0;
292
Jens Axboe2fd233b2007-03-02 08:44:25 +0100293 if (td->odirect)
294 flags |= OS_O_DIRECT;
295 if (td->sync_io)
296 flags |= O_SYNC;
Jens Axboe7abf8332006-11-23 15:01:19 +0100297
Jens Axboe2fd233b2007-03-02 08:44:25 +0100298 if (td_write(td) || td_rw(td)) {
299 flags |= O_RDWR;
Jens Axboe53cdc682006-10-18 11:50:58 +0200300
Jens Axboeaf52b342007-03-13 10:07:47 +0100301 if (f->filetype == FIO_TYPE_FILE)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100302 flags |= O_CREAT;
Jens Axboe2fd233b2007-03-02 08:44:25 +0100303
Jens Axboeb5af8292007-03-08 12:43:13 +0100304 f->fd = open(f->file_name, flags, 0600);
Jens Axboe2fd233b2007-03-02 08:44:25 +0100305 } else {
Jens Axboeaf52b342007-03-13 10:07:47 +0100306 if (f->filetype == FIO_TYPE_CHAR)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100307 flags |= O_RDWR;
308 else
309 flags |= O_RDONLY;
310
Jens Axboeb5af8292007-03-08 12:43:13 +0100311 f->fd = open(f->file_name, flags);
Jens Axboe53cdc682006-10-18 11:50:58 +0200312 }
313
314 if (f->fd == -1) {
Jens Axboee1161c32007-02-22 19:36:48 +0100315 int __e = errno;
316
317 td_verror(td, __e, "open");
318 if (__e == EINVAL && td->odirect)
Jens Axboe9d80e112007-02-22 19:37:53 +0100319 log_err("fio: destination does not support O_DIRECT\n");
Jens Axboe8ab53872007-03-13 21:41:38 +0100320 if (__e == EMFILE)
321 log_err("fio: try reducing/setting openfiles (failed at %u of %u)\n", td->nr_open_files, td->nr_files);
Jens Axboe53cdc682006-10-18 11:50:58 +0200322 return 1;
323 }
324
Jens Axboeb5af8292007-03-08 12:43:13 +0100325 if (get_file_size(td, f))
326 goto err;
327
Jens Axboe02638822007-03-12 09:25:55 +0100328 if (td->invalidate_cache && file_invalidate_cache(td, f))
Jens Axboeb5af8292007-03-08 12:43:13 +0100329 goto err;
330
331 if (!td_random(td)) {
332 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
333 td_verror(td, errno, "fadvise");
334 goto err;
335 }
336 } else {
337 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_RANDOM) < 0) {
338 td_verror(td, errno, "fadvise");
339 goto err;
340 }
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100341 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200342
343 return 0;
Jens Axboeb5af8292007-03-08 12:43:13 +0100344err:
345 close(f->fd);
346 return 1;
347}
348
Jens Axboe21972cd2006-11-23 12:39:16 +0100349int open_files(struct thread_data *td)
350{
351 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100352 unsigned int i;
353 int err = 0;
Jens Axboe21972cd2006-11-23 12:39:16 +0100354
355 for_each_file(td, f, i) {
Jens Axboeb5af8292007-03-08 12:43:13 +0100356 err = td_io_open_file(td, f);
Jens Axboe21972cd2006-11-23 12:39:16 +0100357 if (err)
358 break;
Jens Axboeb5af8292007-03-08 12:43:13 +0100359
360 if (td->open_files == td->nr_open_files)
361 break;
Jens Axboe21972cd2006-11-23 12:39:16 +0100362 }
363
Jens Axboe7abf8332006-11-23 15:01:19 +0100364 if (!err)
365 return 0;
366
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100367 for_each_file(td, f, i)
Jens Axboeb5af8292007-03-08 12:43:13 +0100368 td_io_close_file(td, f);
Jens Axboe7abf8332006-11-23 15:01:19 +0100369
Jens Axboe21972cd2006-11-23 12:39:16 +0100370 return err;
371}
372
Jens Axboe53cdc682006-10-18 11:50:58 +0200373int setup_files(struct thread_data *td)
374{
375 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100376 unsigned int i;
377 int err;
Jens Axboe53cdc682006-10-18 11:50:58 +0200378
379 /*
380 * if ioengine defines a setup() method, it's responsible for
381 * setting up everything in the td->files[] area.
382 */
383 if (td->io_ops->setup)
384 return td->io_ops->setup(td);
385
386 if (create_files(td))
387 return 1;
388
Jens Axboe21972cd2006-11-23 12:39:16 +0100389 err = open_files(td);
Jens Axboef1027062006-10-20 10:14:01 +0200390 if (err)
391 return err;
392
Jens Axboe0a7eb122006-10-20 10:36:42 +0200393 /*
394 * Recalculate the total file size now that files are set up.
395 */
396 td->total_file_size = 0;
397 for_each_file(td, f, i)
398 td->total_file_size += f->file_size;
399
Jens Axboef1027062006-10-20 10:14:01 +0200400 td->io_size = td->total_file_size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200401 if (td->io_size == 0) {
402 log_err("%s: no io blocks\n", td->name);
Jens Axboee1161c32007-02-22 19:36:48 +0100403 td_verror(td, EINVAL, "total_file_size");
Jens Axboe53cdc682006-10-18 11:50:58 +0200404 return 1;
405 }
406
407 if (!td->zone_size)
408 td->zone_size = td->io_size;
409
410 td->total_io_size = td->io_size * td->loops;
411
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100412 for_each_file(td, f, i)
Jens Axboeb5af8292007-03-08 12:43:13 +0100413 td_io_close_file(td, f);
Jens Axboe21972cd2006-11-23 12:39:16 +0100414
415 return err;
Jens Axboe53cdc682006-10-18 11:50:58 +0200416}
417
418void close_files(struct thread_data *td)
419{
Jens Axboe0ab8db82006-10-18 17:16:23 +0200420 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100421 unsigned int i;
Jens Axboe53cdc682006-10-18 11:50:58 +0200422
Jens Axboe0ab8db82006-10-18 17:16:23 +0200423 for_each_file(td, f, i) {
Jens Axboe1315a682007-03-13 11:25:07 +0100424 if (!f->file_name && (f->flags & FIO_FILE_UNLINK) &&
Jens Axboeaf52b342007-03-13 10:07:47 +0100425 f->filetype == FIO_TYPE_FILE) {
Jens Axboe132ad462006-11-23 12:23:12 +0100426 unlink(f->file_name);
Jens Axboe1315a682007-03-13 11:25:07 +0100427 free(f->file_name);
Jens Axboe132ad462006-11-23 12:23:12 +0100428 f->file_name = NULL;
429 }
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100430
Jens Axboeb5af8292007-03-08 12:43:13 +0100431 td_io_close_file(td, f);
Jens Axboeb3dc7f02007-03-09 12:52:15 +0100432
433 if (f->file_map)
434 free(f->file_map);
Jens Axboe53cdc682006-10-18 11:50:58 +0200435 }
Jens Axboeb4a6a592006-10-20 13:54:47 +0200436
Jens Axboe132ad462006-11-23 12:23:12 +0100437 td->filename = NULL;
Jens Axboeb4a6a592006-10-20 13:54:47 +0200438 free(td->files);
439 td->files = NULL;
440 td->nr_files = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200441}
Jens Axboeaf52b342007-03-13 10:07:47 +0100442
Jens Axboee3bab462007-03-13 11:22:09 +0100443static void get_file_type(struct fio_file *f)
Jens Axboeaf52b342007-03-13 10:07:47 +0100444{
445 struct stat sb;
446
447 f->filetype = FIO_TYPE_FILE;
448
Jens Axboee3bab462007-03-13 11:22:09 +0100449 if (!lstat(f->file_name, &sb)) {
Jens Axboe96d32d52007-03-14 09:16:09 +0100450 f->flags |= FIO_FILE_EXISTS;
451
Jens Axboeaf52b342007-03-13 10:07:47 +0100452 if (S_ISBLK(sb.st_mode))
453 f->filetype = FIO_TYPE_BD;
454 else if (S_ISCHR(sb.st_mode))
455 f->filetype = FIO_TYPE_CHAR;
Jens Axboe96d32d52007-03-14 09:16:09 +0100456 else {
457 /*
458 * might as well do this here, and save a stat later on
459 */
460 f->real_file_size = sb.st_size;
461 f->file_size = f->real_file_size;
462 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100463 }
464}
465
466void add_file(struct thread_data *td, const char *fname)
467{
Jens Axboe7b4e4fe2007-03-13 12:51:40 +0100468 int cur_files = td->files_index;
Jens Axboeaf52b342007-03-13 10:07:47 +0100469 struct fio_file *f;
470
471 td->files = realloc(td->files, (cur_files + 1) * sizeof(*f));
472
473 f = &td->files[cur_files];
474 memset(f, 0, sizeof(*f));
475 f->fd = -1;
Jens Axboecae61952007-03-13 11:12:14 +0100476 f->file_name = strdup(fname);
Jens Axboeaf52b342007-03-13 10:07:47 +0100477
Jens Axboee3bab462007-03-13 11:22:09 +0100478 get_file_type(f);
Jens Axboeaf52b342007-03-13 10:07:47 +0100479
Jens Axboe7b4e4fe2007-03-13 12:51:40 +0100480 td->files_index++;
Jens Axboe15494412007-03-13 11:52:47 +0100481 if (f->filetype == FIO_TYPE_FILE)
482 td->nr_normal_files++;
Jens Axboeaf52b342007-03-13 10:07:47 +0100483}
Jens Axboe0ad920e2007-03-13 11:06:45 +0100484
485void get_file(struct fio_file *f)
486{
487 f->references++;
488}
489
490void put_file(struct thread_data *td, struct fio_file *f)
491{
492 if (!(f->flags & FIO_FILE_OPEN))
493 return;
494
495 assert(f->references);
496 if (--f->references)
497 return;
498
Jens Axboeebb14152007-03-13 14:42:15 +0100499 if (should_fsync(td) && td->fsync_on_close)
500 fsync(f->fd);
501
Jens Axboe0ad920e2007-03-13 11:06:45 +0100502 if (td->io_ops->close_file)
503 td->io_ops->close_file(td, f);
504 td->nr_open_files--;
505 f->flags &= ~FIO_FILE_OPEN;
506}
Jens Axboebbf6b542007-03-13 15:28:55 +0100507
508static int recurse_dir(struct thread_data *td, const char *dirname)
509{
510 struct dirent *dir;
511 int ret = 0;
512 DIR *D;
513
514 D = opendir(dirname);
515 if (!D) {
516 td_verror(td, errno, "opendir");
517 return 1;
518 }
519
520 while ((dir = readdir(D)) != NULL) {
521 char full_path[PATH_MAX];
522 struct stat sb;
523
Jens Axboee85b2b82007-03-14 09:39:06 +0100524 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
525 continue;
Jens Axboe96d32d52007-03-14 09:16:09 +0100526
Jens Axboebbf6b542007-03-13 15:28:55 +0100527 sprintf(full_path, "%s/%s", dirname, dir->d_name);
528
529 if (lstat(full_path, &sb) == -1) {
530 if (errno != ENOENT) {
531 td_verror(td, errno, "stat");
532 return 1;
533 }
534 }
535
536 if (S_ISREG(sb.st_mode)) {
537 add_file(td, full_path);
538 td->nr_files++;
539 continue;
540 }
541
Jens Axboebbf6b542007-03-13 15:28:55 +0100542 if ((ret = recurse_dir(td, full_path)) != 0)
543 break;
544 }
545
546 closedir(D);
547 return ret;
548}
549
550int add_dir_files(struct thread_data *td, const char *path)
551{
552 return recurse_dir(td, path);
553}