blob: 3bd8633873cb7c40cb99c284a2d3ad7fdebadf4f [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 Axboeaf52b342007-03-13 10:07:47 +0100105 unsigned int i;
Jens Axboe53cdc682006-10-18 11:50:58 +0200106
Jens Axboe96d32d52007-03-14 09:16:09 +0100107 total_file_size = td->total_file_size;
Jens Axboe15494412007-03-13 11:52:47 +0100108 for_each_file(td, f, i) {
Jens Axboe96d32d52007-03-14 09:16:09 +0100109 unsigned long long s;
110
111 f->file_offset = td->start_offset;
112
Jens Axboe15494412007-03-13 11:52:47 +0100113 if (f->filetype != FIO_TYPE_FILE)
114 continue;
115
Jens Axboe96d32d52007-03-14 09:16:09 +0100116 if (f->flags & FIO_FILE_EXISTS) {
117 s = f->file_size;
118 if (s > total_file_size)
119 s = total_file_size;
120
121 total_file_size -= s;
122 }
Jens Axboe15494412007-03-13 11:52:47 +0100123 }
Jens Axboef6971252006-11-15 10:00:31 +0100124
Jens Axboe53cdc682006-10-18 11:50:58 +0200125 /*
126 * unless specifically asked for overwrite, let normal io extend it
127 */
Jens Axboe02638822007-03-12 09:25:55 +0100128 can_extend = !td->overwrite && !(td->io_ops->flags & FIO_NOEXTEND);
129 if (can_extend)
Jens Axboe53cdc682006-10-18 11:50:58 +0200130 return 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200131
Jens Axboef1027062006-10-20 10:14:01 +0200132 need_create = 0;
Jens Axboeaf52b342007-03-13 10:07:47 +0100133 for_each_file(td, f, i) {
134 int file_there;
Jens Axboeeff68612007-01-14 05:56:44 +0100135
Jens Axboeaf52b342007-03-13 10:07:47 +0100136 if (f->filetype != FIO_TYPE_FILE)
137 continue;
Jens Axboe96d32d52007-03-14 09:16:09 +0100138 if (f->flags & FIO_FILE_EXISTS)
139 continue;
140
141 f->file_size = total_file_size / td->nr_normal_files;
Jens Axboeeff68612007-01-14 05:56:44 +0100142
Jens Axboeaf52b342007-03-13 10:07:47 +0100143 file_there = !file_ok(td, f);
144
145 if (file_there && td_write(td) && !td->overwrite) {
146 unlink(f->file_name);
147 file_there = 0;
Jens Axboeeff68612007-01-14 05:56:44 +0100148 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100149
150 need_create += !file_there;
Jens Axboeeff68612007-01-14 05:56:44 +0100151 }
Jens Axboef1027062006-10-20 10:14:01 +0200152
153 if (!need_create)
154 return 0;
155
Jens Axboe53cdc682006-10-18 11:50:58 +0200156 if (!td->total_file_size) {
157 log_err("Need size for create\n");
Jens Axboee1161c32007-02-22 19:36:48 +0100158 td_verror(td, EINVAL, "file_size");
Jens Axboe53cdc682006-10-18 11:50:58 +0200159 return 1;
160 }
161
Jens Axboe25205e92006-10-19 20:50:14 +0200162 temp_stall_ts = 1;
Jens Axboe1e97cce2006-12-05 11:44:16 +0100163 fprintf(f_out, "%s: Laying out IO file(s) (%u x %LuMiB == %LuMiB)\n",
Jens Axboe15494412007-03-13 11:52:47 +0100164 td->name, td->nr_normal_files,
165 (td->total_file_size >> 20) / td->nr_normal_files,
Jens Axboe25205e92006-10-19 20:50:14 +0200166 td->total_file_size >> 20);
167
168 err = 0;
169 for_each_file(td, f, i) {
Jens Axboe9b031dc2006-12-15 16:01:54 +0100170 /*
171 * Only unlink files that we created.
172 */
Jens Axboef11bd942007-03-13 10:16:34 +0100173 f->flags &= ~FIO_FILE_UNLINK;
Jens Axboe25205e92006-10-19 20:50:14 +0200174 if (file_ok(td, f)) {
Jens Axboef11bd942007-03-13 10:16:34 +0100175 if (td->unlink)
176 f->flags |= FIO_FILE_UNLINK;
177
Jens Axboe25205e92006-10-19 20:50:14 +0200178 err = create_file(td, f);
179 if (err)
180 break;
181 }
182 }
183
Jens Axboe53cdc682006-10-18 11:50:58 +0200184 temp_stall_ts = 0;
185 return err;
186}
187
188static int file_size(struct thread_data *td, struct fio_file *f)
189{
190 struct stat st;
191
192 if (td->overwrite) {
193 if (fstat(f->fd, &st) == -1) {
Jens Axboee1161c32007-02-22 19:36:48 +0100194 td_verror(td, errno, "fstat");
Jens Axboe53cdc682006-10-18 11:50:58 +0200195 return 1;
196 }
197
198 f->real_file_size = st.st_size;
199
200 if (!f->file_size || f->file_size > f->real_file_size)
201 f->file_size = f->real_file_size;
Jens Axboe745508d2007-01-08 10:25:51 +0100202 } else
203 f->real_file_size = f->file_size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200204
Jens Axboe53cdc682006-10-18 11:50:58 +0200205 return 0;
206}
207
208static int bdev_size(struct thread_data *td, struct fio_file *f)
209{
210 unsigned long long bytes;
211 int r;
212
213 r = blockdev_size(f->fd, &bytes);
214 if (r) {
Jens Axboee1161c32007-02-22 19:36:48 +0100215 td_verror(td, r, "blockdev_size");
Jens Axboe53cdc682006-10-18 11:50:58 +0200216 return 1;
217 }
218
219 f->real_file_size = bytes;
220
221 /*
222 * no extend possibilities, so limit size to device size if too large
223 */
224 if (!f->file_size || f->file_size > f->real_file_size)
225 f->file_size = f->real_file_size;
226
227 f->file_size -= f->file_offset;
228 return 0;
229}
230
231static int get_file_size(struct thread_data *td, struct fio_file *f)
232{
233 int ret = 0;
234
Jens Axboe96d32d52007-03-14 09:16:09 +0100235 if (f->filetype == FIO_TYPE_FILE) {
236 if (!(f->flags & FIO_FILE_EXISTS))
237 ret = file_size(td, f);
238 } else if (f->filetype == FIO_TYPE_BD)
Jens Axboe53cdc682006-10-18 11:50:58 +0200239 ret = bdev_size(td, f);
240 else
241 f->real_file_size = -1;
242
243 if (ret)
244 return ret;
245
246 if (f->file_offset > f->real_file_size) {
247 log_err("%s: offset extends end (%Lu > %Lu)\n", td->name, f->file_offset, f->real_file_size);
248 return 1;
249 }
250
Jens Axboe53cdc682006-10-18 11:50:58 +0200251 return 0;
252}
253
Jens Axboee5b401d2006-10-18 16:03:40 +0200254int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
255{
256 int ret = 0;
257
Jens Axboeda86ccb2007-03-08 12:49:37 +0100258 if (td->odirect)
Jens Axboeb5af8292007-03-08 12:43:13 +0100259 return 0;
260
Jens Axboee5b401d2006-10-18 16:03:40 +0200261 /*
262 * FIXME: add blockdev flushing too
263 */
Jens Axboeb5af8292007-03-08 12:43:13 +0100264 if (f->mmap)
Jens Axboee5b401d2006-10-18 16:03:40 +0200265 ret = madvise(f->mmap, f->file_size, MADV_DONTNEED);
Jens Axboeaf52b342007-03-13 10:07:47 +0100266 else if (f->filetype == FIO_TYPE_FILE) {
Jens Axboeb5af8292007-03-08 12:43:13 +0100267 ret = fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_DONTNEED);
Jens Axboeaf52b342007-03-13 10:07:47 +0100268 } else if (f->filetype == FIO_TYPE_BD) {
Jens Axboeb5af8292007-03-08 12:43:13 +0100269 ret = blockdev_invalidate_cache(f->fd);
Jens Axboeaf52b342007-03-13 10:07:47 +0100270 } else if (f->filetype == FIO_TYPE_CHAR)
Jens Axboee5b401d2006-10-18 16:03:40 +0200271 ret = 0;
272
273 if (ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100274 td_verror(td, errno, "invalidate_cache");
Jens Axboee5b401d2006-10-18 16:03:40 +0200275 return 1;
276 }
277
Jens Axboead2da602007-02-26 12:33:27 +0100278 return ret;
Jens Axboee5b401d2006-10-18 16:03:40 +0200279}
280
Jens Axboeb5af8292007-03-08 12:43:13 +0100281void generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
Jens Axboe53cdc682006-10-18 11:50:58 +0200282{
Jens Axboeb5af8292007-03-08 12:43:13 +0100283 close(f->fd);
284 f->fd = -1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200285}
286
Jens Axboeb5af8292007-03-08 12:43:13 +0100287int generic_open_file(struct thread_data *td, struct fio_file *f)
Jens Axboe53cdc682006-10-18 11:50:58 +0200288{
Jens Axboe53cdc682006-10-18 11:50:58 +0200289 int flags = 0;
290
Jens Axboe2fd233b2007-03-02 08:44:25 +0100291 if (td->odirect)
292 flags |= OS_O_DIRECT;
293 if (td->sync_io)
294 flags |= O_SYNC;
Jens Axboe7abf8332006-11-23 15:01:19 +0100295
Jens Axboe2fd233b2007-03-02 08:44:25 +0100296 if (td_write(td) || td_rw(td)) {
297 flags |= O_RDWR;
Jens Axboe53cdc682006-10-18 11:50:58 +0200298
Jens Axboeaf52b342007-03-13 10:07:47 +0100299 if (f->filetype == FIO_TYPE_FILE)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100300 flags |= O_CREAT;
Jens Axboe2fd233b2007-03-02 08:44:25 +0100301
Jens Axboeb5af8292007-03-08 12:43:13 +0100302 f->fd = open(f->file_name, flags, 0600);
Jens Axboe2fd233b2007-03-02 08:44:25 +0100303 } else {
Jens Axboeaf52b342007-03-13 10:07:47 +0100304 if (f->filetype == FIO_TYPE_CHAR)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100305 flags |= O_RDWR;
306 else
307 flags |= O_RDONLY;
308
Jens Axboeb5af8292007-03-08 12:43:13 +0100309 f->fd = open(f->file_name, flags);
Jens Axboe53cdc682006-10-18 11:50:58 +0200310 }
311
312 if (f->fd == -1) {
Jens Axboee1161c32007-02-22 19:36:48 +0100313 int __e = errno;
314
315 td_verror(td, __e, "open");
316 if (__e == EINVAL && td->odirect)
Jens Axboe9d80e112007-02-22 19:37:53 +0100317 log_err("fio: destination does not support O_DIRECT\n");
Jens Axboe8ab53872007-03-13 21:41:38 +0100318 if (__e == EMFILE)
319 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 +0200320 return 1;
321 }
322
Jens Axboeb5af8292007-03-08 12:43:13 +0100323 if (get_file_size(td, f))
324 goto err;
325
Jens Axboe02638822007-03-12 09:25:55 +0100326 if (td->invalidate_cache && file_invalidate_cache(td, f))
Jens Axboeb5af8292007-03-08 12:43:13 +0100327 goto err;
328
329 if (!td_random(td)) {
330 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
331 td_verror(td, errno, "fadvise");
332 goto err;
333 }
334 } else {
335 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_RANDOM) < 0) {
336 td_verror(td, errno, "fadvise");
337 goto err;
338 }
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100339 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200340
341 return 0;
Jens Axboeb5af8292007-03-08 12:43:13 +0100342err:
343 close(f->fd);
344 return 1;
345}
346
Jens Axboe21972cd2006-11-23 12:39:16 +0100347int open_files(struct thread_data *td)
348{
349 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100350 unsigned int i;
351 int err = 0;
Jens Axboe21972cd2006-11-23 12:39:16 +0100352
353 for_each_file(td, f, i) {
Jens Axboeb5af8292007-03-08 12:43:13 +0100354 err = td_io_open_file(td, f);
Jens Axboe21972cd2006-11-23 12:39:16 +0100355 if (err)
356 break;
Jens Axboeb5af8292007-03-08 12:43:13 +0100357
358 if (td->open_files == td->nr_open_files)
359 break;
Jens Axboe21972cd2006-11-23 12:39:16 +0100360 }
361
Jens Axboe7abf8332006-11-23 15:01:19 +0100362 if (!err)
363 return 0;
364
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100365 for_each_file(td, f, i)
Jens Axboeb5af8292007-03-08 12:43:13 +0100366 td_io_close_file(td, f);
Jens Axboe7abf8332006-11-23 15:01:19 +0100367
Jens Axboe21972cd2006-11-23 12:39:16 +0100368 return err;
369}
370
Jens Axboe53cdc682006-10-18 11:50:58 +0200371int setup_files(struct thread_data *td)
372{
373 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100374 unsigned int i;
375 int err;
Jens Axboe53cdc682006-10-18 11:50:58 +0200376
377 /*
378 * if ioengine defines a setup() method, it's responsible for
379 * setting up everything in the td->files[] area.
380 */
381 if (td->io_ops->setup)
382 return td->io_ops->setup(td);
383
384 if (create_files(td))
385 return 1;
386
Jens Axboe21972cd2006-11-23 12:39:16 +0100387 err = open_files(td);
Jens Axboef1027062006-10-20 10:14:01 +0200388 if (err)
389 return err;
390
Jens Axboe0a7eb122006-10-20 10:36:42 +0200391 /*
392 * Recalculate the total file size now that files are set up.
393 */
394 td->total_file_size = 0;
395 for_each_file(td, f, i)
396 td->total_file_size += f->file_size;
397
Jens Axboef1027062006-10-20 10:14:01 +0200398 td->io_size = td->total_file_size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200399 if (td->io_size == 0) {
400 log_err("%s: no io blocks\n", td->name);
Jens Axboee1161c32007-02-22 19:36:48 +0100401 td_verror(td, EINVAL, "total_file_size");
Jens Axboe53cdc682006-10-18 11:50:58 +0200402 return 1;
403 }
404
405 if (!td->zone_size)
406 td->zone_size = td->io_size;
407
408 td->total_io_size = td->io_size * td->loops;
409
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100410 for_each_file(td, f, i)
Jens Axboeb5af8292007-03-08 12:43:13 +0100411 td_io_close_file(td, f);
Jens Axboe21972cd2006-11-23 12:39:16 +0100412
413 return err;
Jens Axboe53cdc682006-10-18 11:50:58 +0200414}
415
416void close_files(struct thread_data *td)
417{
Jens Axboe0ab8db82006-10-18 17:16:23 +0200418 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100419 unsigned int i;
Jens Axboe53cdc682006-10-18 11:50:58 +0200420
Jens Axboe0ab8db82006-10-18 17:16:23 +0200421 for_each_file(td, f, i) {
Jens Axboe1315a682007-03-13 11:25:07 +0100422 if (!f->file_name && (f->flags & FIO_FILE_UNLINK) &&
Jens Axboeaf52b342007-03-13 10:07:47 +0100423 f->filetype == FIO_TYPE_FILE) {
Jens Axboe132ad462006-11-23 12:23:12 +0100424 unlink(f->file_name);
Jens Axboe1315a682007-03-13 11:25:07 +0100425 free(f->file_name);
Jens Axboe132ad462006-11-23 12:23:12 +0100426 f->file_name = NULL;
427 }
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100428
Jens Axboeb5af8292007-03-08 12:43:13 +0100429 td_io_close_file(td, f);
Jens Axboeb3dc7f02007-03-09 12:52:15 +0100430
431 if (f->file_map)
432 free(f->file_map);
Jens Axboe53cdc682006-10-18 11:50:58 +0200433 }
Jens Axboeb4a6a592006-10-20 13:54:47 +0200434
Jens Axboe132ad462006-11-23 12:23:12 +0100435 td->filename = NULL;
Jens Axboeb4a6a592006-10-20 13:54:47 +0200436 free(td->files);
437 td->files = NULL;
438 td->nr_files = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200439}
Jens Axboeaf52b342007-03-13 10:07:47 +0100440
Jens Axboee3bab462007-03-13 11:22:09 +0100441static void get_file_type(struct fio_file *f)
Jens Axboeaf52b342007-03-13 10:07:47 +0100442{
443 struct stat sb;
444
445 f->filetype = FIO_TYPE_FILE;
446
Jens Axboee3bab462007-03-13 11:22:09 +0100447 if (!lstat(f->file_name, &sb)) {
Jens Axboe96d32d52007-03-14 09:16:09 +0100448 f->flags |= FIO_FILE_EXISTS;
449
Jens Axboeaf52b342007-03-13 10:07:47 +0100450 if (S_ISBLK(sb.st_mode))
451 f->filetype = FIO_TYPE_BD;
452 else if (S_ISCHR(sb.st_mode))
453 f->filetype = FIO_TYPE_CHAR;
Jens Axboe96d32d52007-03-14 09:16:09 +0100454 else {
455 /*
456 * might as well do this here, and save a stat later on
457 */
458 f->real_file_size = sb.st_size;
459 f->file_size = f->real_file_size;
460 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100461 }
462}
463
464void add_file(struct thread_data *td, const char *fname)
465{
Jens Axboe7b4e4fe2007-03-13 12:51:40 +0100466 int cur_files = td->files_index;
Jens Axboeaf52b342007-03-13 10:07:47 +0100467 struct fio_file *f;
468
469 td->files = realloc(td->files, (cur_files + 1) * sizeof(*f));
470
471 f = &td->files[cur_files];
472 memset(f, 0, sizeof(*f));
473 f->fd = -1;
Jens Axboecae61952007-03-13 11:12:14 +0100474 f->file_name = strdup(fname);
Jens Axboeaf52b342007-03-13 10:07:47 +0100475
Jens Axboee3bab462007-03-13 11:22:09 +0100476 get_file_type(f);
Jens Axboeaf52b342007-03-13 10:07:47 +0100477
Jens Axboe7b4e4fe2007-03-13 12:51:40 +0100478 td->files_index++;
Jens Axboe15494412007-03-13 11:52:47 +0100479 if (f->filetype == FIO_TYPE_FILE)
480 td->nr_normal_files++;
Jens Axboeaf52b342007-03-13 10:07:47 +0100481}
Jens Axboe0ad920e2007-03-13 11:06:45 +0100482
483void get_file(struct fio_file *f)
484{
485 f->references++;
486}
487
488void put_file(struct thread_data *td, struct fio_file *f)
489{
490 if (!(f->flags & FIO_FILE_OPEN))
491 return;
492
493 assert(f->references);
494 if (--f->references)
495 return;
496
Jens Axboeebb14152007-03-13 14:42:15 +0100497 if (should_fsync(td) && td->fsync_on_close)
498 fsync(f->fd);
499
Jens Axboe0ad920e2007-03-13 11:06:45 +0100500 if (td->io_ops->close_file)
501 td->io_ops->close_file(td, f);
502 td->nr_open_files--;
503 f->flags &= ~FIO_FILE_OPEN;
504}
Jens Axboebbf6b542007-03-13 15:28:55 +0100505
506static int recurse_dir(struct thread_data *td, const char *dirname)
507{
508 struct dirent *dir;
509 int ret = 0;
510 DIR *D;
511
512 D = opendir(dirname);
513 if (!D) {
514 td_verror(td, errno, "opendir");
515 return 1;
516 }
517
518 while ((dir = readdir(D)) != NULL) {
519 char full_path[PATH_MAX];
520 struct stat sb;
521
Jens Axboe96d32d52007-03-14 09:16:09 +0100522 /*
523 * check d_ino here?
524 */
525
Jens Axboebbf6b542007-03-13 15:28:55 +0100526 sprintf(full_path, "%s/%s", dirname, dir->d_name);
527
528 if (lstat(full_path, &sb) == -1) {
529 if (errno != ENOENT) {
530 td_verror(td, errno, "stat");
531 return 1;
532 }
533 }
534
535 if (S_ISREG(sb.st_mode)) {
536 add_file(td, full_path);
537 td->nr_files++;
538 continue;
539 }
540
541 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
542 continue;
543
544 if ((ret = recurse_dir(td, full_path)) != 0)
545 break;
546 }
547
548 closedir(D);
549 return ret;
550}
551
552int add_dir_files(struct thread_data *td, const char *path)
553{
554 return recurse_dir(td, path);
555}