blob: 053bda9d70f684e6d6fac6eba093f806e87fb51d [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
Jens Axboe9c60ce62007-03-15 09:14:47 +0100100static unsigned long long set_rand_file_size(struct thread_data *td,
101 unsigned long long total_size)
102{
103 unsigned long long upper = total_size;
104 unsigned long long ret;
105 long r;
106
107 if (upper > td->file_size_high)
108 upper = td->file_size_high;
109 else if (upper < td->file_size_low)
110 return 0;
111 else if (!upper)
112 return 0;
113
114 r = os_random_long(&td->file_size_state);
115 ret = td->file_size_low + (unsigned long long) ((double) upper * (r / (RAND_MAX + 1.0)));
116 ret -= (ret % td->rw_min_bs);
117 if (ret > upper)
118 ret = upper;
119 return ret;
120}
121
Jens Axboe53cdc682006-10-18 11:50:58 +0200122static int create_files(struct thread_data *td)
123{
124 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100125 int err, need_create, can_extend;
Jens Axboe9c60ce62007-03-15 09:14:47 +0100126 unsigned long long total_file_size, local_file_size;
Jens Axboee407bec2007-03-14 11:21:27 +0100127 unsigned int i, new_files;
Jens Axboe53cdc682006-10-18 11:50:58 +0200128
Jens Axboee407bec2007-03-14 11:21:27 +0100129 new_files = 0;
Jens Axboe96d32d52007-03-14 09:16:09 +0100130 total_file_size = td->total_file_size;
Jens Axboe15494412007-03-13 11:52:47 +0100131 for_each_file(td, f, i) {
Jens Axboe96d32d52007-03-14 09:16:09 +0100132 unsigned long long s;
133
134 f->file_offset = td->start_offset;
135
Jens Axboe15494412007-03-13 11:52:47 +0100136 if (f->filetype != FIO_TYPE_FILE)
137 continue;
138
Jens Axboe96d32d52007-03-14 09:16:09 +0100139 if (f->flags & FIO_FILE_EXISTS) {
140 s = f->file_size;
141 if (s > total_file_size)
142 s = total_file_size;
143
144 total_file_size -= s;
Jens Axboee407bec2007-03-14 11:21:27 +0100145 } else
146 new_files++;
Jens Axboe15494412007-03-13 11:52:47 +0100147 }
Jens Axboef6971252006-11-15 10:00:31 +0100148
Jens Axboe53cdc682006-10-18 11:50:58 +0200149 /*
150 * unless specifically asked for overwrite, let normal io extend it
151 */
Jens Axboe02638822007-03-12 09:25:55 +0100152 can_extend = !td->overwrite && !(td->io_ops->flags & FIO_NOEXTEND);
153 if (can_extend)
Jens Axboe53cdc682006-10-18 11:50:58 +0200154 return 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200155
Jens Axboef1027062006-10-20 10:14:01 +0200156 need_create = 0;
Jens Axboe9c60ce62007-03-15 09:14:47 +0100157 local_file_size = total_file_size;
158 if (!local_file_size)
159 local_file_size = -1;
160
Jens Axboeaf52b342007-03-13 10:07:47 +0100161 for_each_file(td, f, i) {
162 int file_there;
Jens Axboeeff68612007-01-14 05:56:44 +0100163
Jens Axboeaf52b342007-03-13 10:07:47 +0100164 if (f->filetype != FIO_TYPE_FILE)
165 continue;
Jens Axboe96d32d52007-03-14 09:16:09 +0100166 if (f->flags & FIO_FILE_EXISTS)
167 continue;
168
Jens Axboe9c60ce62007-03-15 09:14:47 +0100169 if (!td->file_size_low)
170 f->file_size = total_file_size / new_files;
171 else {
172 /*
173 * If we don't have enough space left for a file
174 * of the minimum size, bail.
175 */
176 if (local_file_size < td->file_size_low) {
177 log_info("fio: limited to %d files\n", i);
178 new_files -= (td->nr_files - i);
179 td->nr_files = i;
180 break;
181 }
182
183 f->file_size = set_rand_file_size(td, local_file_size);
184 local_file_size -= f->file_size;
185 }
Jens Axboeeff68612007-01-14 05:56:44 +0100186
Jens Axboeaf52b342007-03-13 10:07:47 +0100187 file_there = !file_ok(td, f);
188
189 if (file_there && td_write(td) && !td->overwrite) {
190 unlink(f->file_name);
191 file_there = 0;
Jens Axboeeff68612007-01-14 05:56:44 +0100192 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100193
194 need_create += !file_there;
Jens Axboeeff68612007-01-14 05:56:44 +0100195 }
Jens Axboef1027062006-10-20 10:14:01 +0200196
197 if (!need_create)
198 return 0;
199
Jens Axboe53cdc682006-10-18 11:50:58 +0200200 if (!td->total_file_size) {
201 log_err("Need size for create\n");
Jens Axboee1161c32007-02-22 19:36:48 +0100202 td_verror(td, EINVAL, "file_size");
Jens Axboe53cdc682006-10-18 11:50:58 +0200203 return 1;
204 }
205
Jens Axboe25205e92006-10-19 20:50:14 +0200206 temp_stall_ts = 1;
Jens Axboe9c60ce62007-03-15 09:14:47 +0100207 fprintf(f_out, "%s: Laying out IO file(s) (%u files / %LuMiB)\n",
Jens Axboee407bec2007-03-14 11:21:27 +0100208 td->name, new_files,
Jens Axboee407bec2007-03-14 11:21:27 +0100209 total_file_size >> 20);
Jens Axboe25205e92006-10-19 20:50:14 +0200210
211 err = 0;
212 for_each_file(td, f, i) {
Jens Axboe9b031dc2006-12-15 16:01:54 +0100213 /*
214 * Only unlink files that we created.
215 */
Jens Axboef11bd942007-03-13 10:16:34 +0100216 f->flags &= ~FIO_FILE_UNLINK;
Jens Axboe25205e92006-10-19 20:50:14 +0200217 if (file_ok(td, f)) {
Jens Axboef11bd942007-03-13 10:16:34 +0100218 if (td->unlink)
219 f->flags |= FIO_FILE_UNLINK;
220
Jens Axboe25205e92006-10-19 20:50:14 +0200221 err = create_file(td, f);
222 if (err)
223 break;
224 }
225 }
226
Jens Axboe53cdc682006-10-18 11:50:58 +0200227 temp_stall_ts = 0;
228 return err;
229}
230
231static int file_size(struct thread_data *td, struct fio_file *f)
232{
233 struct stat st;
234
235 if (td->overwrite) {
236 if (fstat(f->fd, &st) == -1) {
Jens Axboee1161c32007-02-22 19:36:48 +0100237 td_verror(td, errno, "fstat");
Jens Axboe53cdc682006-10-18 11:50:58 +0200238 return 1;
239 }
240
241 f->real_file_size = st.st_size;
242
243 if (!f->file_size || f->file_size > f->real_file_size)
244 f->file_size = f->real_file_size;
Jens Axboe745508d2007-01-08 10:25:51 +0100245 } else
246 f->real_file_size = f->file_size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200247
Jens Axboe53cdc682006-10-18 11:50:58 +0200248 return 0;
249}
250
251static int bdev_size(struct thread_data *td, struct fio_file *f)
252{
253 unsigned long long bytes;
254 int r;
255
256 r = blockdev_size(f->fd, &bytes);
257 if (r) {
Jens Axboee1161c32007-02-22 19:36:48 +0100258 td_verror(td, r, "blockdev_size");
Jens Axboe53cdc682006-10-18 11:50:58 +0200259 return 1;
260 }
261
262 f->real_file_size = bytes;
263
264 /*
265 * no extend possibilities, so limit size to device size if too large
266 */
267 if (!f->file_size || f->file_size > f->real_file_size)
268 f->file_size = f->real_file_size;
269
270 f->file_size -= f->file_offset;
271 return 0;
272}
273
274static int get_file_size(struct thread_data *td, struct fio_file *f)
275{
276 int ret = 0;
277
Jens Axboe96d32d52007-03-14 09:16:09 +0100278 if (f->filetype == FIO_TYPE_FILE) {
279 if (!(f->flags & FIO_FILE_EXISTS))
280 ret = file_size(td, f);
281 } else if (f->filetype == FIO_TYPE_BD)
Jens Axboe53cdc682006-10-18 11:50:58 +0200282 ret = bdev_size(td, f);
283 else
284 f->real_file_size = -1;
285
286 if (ret)
287 return ret;
288
289 if (f->file_offset > f->real_file_size) {
290 log_err("%s: offset extends end (%Lu > %Lu)\n", td->name, f->file_offset, f->real_file_size);
291 return 1;
292 }
293
Jens Axboe53cdc682006-10-18 11:50:58 +0200294 return 0;
295}
296
Jens Axboee5b401d2006-10-18 16:03:40 +0200297int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
298{
299 int ret = 0;
300
Jens Axboeda86ccb2007-03-08 12:49:37 +0100301 if (td->odirect)
Jens Axboeb5af8292007-03-08 12:43:13 +0100302 return 0;
303
Jens Axboee5b401d2006-10-18 16:03:40 +0200304 /*
305 * FIXME: add blockdev flushing too
306 */
Jens Axboeb5af8292007-03-08 12:43:13 +0100307 if (f->mmap)
Jens Axboee5b401d2006-10-18 16:03:40 +0200308 ret = madvise(f->mmap, f->file_size, MADV_DONTNEED);
Jens Axboeaf52b342007-03-13 10:07:47 +0100309 else if (f->filetype == FIO_TYPE_FILE) {
Jens Axboeb5af8292007-03-08 12:43:13 +0100310 ret = fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_DONTNEED);
Jens Axboeaf52b342007-03-13 10:07:47 +0100311 } else if (f->filetype == FIO_TYPE_BD) {
Jens Axboeb5af8292007-03-08 12:43:13 +0100312 ret = blockdev_invalidate_cache(f->fd);
Jens Axboeaf52b342007-03-13 10:07:47 +0100313 } else if (f->filetype == FIO_TYPE_CHAR)
Jens Axboee5b401d2006-10-18 16:03:40 +0200314 ret = 0;
315
316 if (ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100317 td_verror(td, errno, "invalidate_cache");
Jens Axboee5b401d2006-10-18 16:03:40 +0200318 return 1;
319 }
320
Jens Axboead2da602007-02-26 12:33:27 +0100321 return ret;
Jens Axboee5b401d2006-10-18 16:03:40 +0200322}
323
Jens Axboeb5af8292007-03-08 12:43:13 +0100324void generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
Jens Axboe53cdc682006-10-18 11:50:58 +0200325{
Jens Axboeb5af8292007-03-08 12:43:13 +0100326 close(f->fd);
327 f->fd = -1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200328}
329
Jens Axboeb5af8292007-03-08 12:43:13 +0100330int generic_open_file(struct thread_data *td, struct fio_file *f)
Jens Axboe53cdc682006-10-18 11:50:58 +0200331{
Jens Axboe53cdc682006-10-18 11:50:58 +0200332 int flags = 0;
333
Jens Axboe2fd233b2007-03-02 08:44:25 +0100334 if (td->odirect)
335 flags |= OS_O_DIRECT;
336 if (td->sync_io)
337 flags |= O_SYNC;
Jens Axboe7abf8332006-11-23 15:01:19 +0100338
Jens Axboe2fd233b2007-03-02 08:44:25 +0100339 if (td_write(td) || td_rw(td)) {
340 flags |= O_RDWR;
Jens Axboe53cdc682006-10-18 11:50:58 +0200341
Jens Axboeaf52b342007-03-13 10:07:47 +0100342 if (f->filetype == FIO_TYPE_FILE)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100343 flags |= O_CREAT;
Jens Axboe2fd233b2007-03-02 08:44:25 +0100344
Jens Axboeb5af8292007-03-08 12:43:13 +0100345 f->fd = open(f->file_name, flags, 0600);
Jens Axboe2fd233b2007-03-02 08:44:25 +0100346 } else {
Jens Axboeaf52b342007-03-13 10:07:47 +0100347 if (f->filetype == FIO_TYPE_CHAR)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100348 flags |= O_RDWR;
349 else
350 flags |= O_RDONLY;
351
Jens Axboeb5af8292007-03-08 12:43:13 +0100352 f->fd = open(f->file_name, flags);
Jens Axboe53cdc682006-10-18 11:50:58 +0200353 }
354
355 if (f->fd == -1) {
Jens Axboee1161c32007-02-22 19:36:48 +0100356 int __e = errno;
357
358 td_verror(td, __e, "open");
359 if (__e == EINVAL && td->odirect)
Jens Axboe9d80e112007-02-22 19:37:53 +0100360 log_err("fio: destination does not support O_DIRECT\n");
Jens Axboe8ab53872007-03-13 21:41:38 +0100361 if (__e == EMFILE)
362 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 +0200363 return 1;
364 }
365
Jens Axboeb5af8292007-03-08 12:43:13 +0100366 if (get_file_size(td, f))
367 goto err;
368
Jens Axboe02638822007-03-12 09:25:55 +0100369 if (td->invalidate_cache && file_invalidate_cache(td, f))
Jens Axboeb5af8292007-03-08 12:43:13 +0100370 goto err;
371
372 if (!td_random(td)) {
373 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
374 td_verror(td, errno, "fadvise");
375 goto err;
376 }
377 } else {
378 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_RANDOM) < 0) {
379 td_verror(td, errno, "fadvise");
380 goto err;
381 }
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100382 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200383
384 return 0;
Jens Axboeb5af8292007-03-08 12:43:13 +0100385err:
386 close(f->fd);
387 return 1;
388}
389
Jens Axboe21972cd2006-11-23 12:39:16 +0100390int open_files(struct thread_data *td)
391{
392 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100393 unsigned int i;
394 int err = 0;
Jens Axboe21972cd2006-11-23 12:39:16 +0100395
396 for_each_file(td, f, i) {
Jens Axboeb5af8292007-03-08 12:43:13 +0100397 err = td_io_open_file(td, f);
Jens Axboe21972cd2006-11-23 12:39:16 +0100398 if (err)
399 break;
Jens Axboeb5af8292007-03-08 12:43:13 +0100400
401 if (td->open_files == td->nr_open_files)
402 break;
Jens Axboe21972cd2006-11-23 12:39:16 +0100403 }
404
Jens Axboe7abf8332006-11-23 15:01:19 +0100405 if (!err)
406 return 0;
407
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100408 for_each_file(td, f, i)
Jens Axboeb5af8292007-03-08 12:43:13 +0100409 td_io_close_file(td, f);
Jens Axboe7abf8332006-11-23 15:01:19 +0100410
Jens Axboe21972cd2006-11-23 12:39:16 +0100411 return err;
412}
413
Jens Axboe53cdc682006-10-18 11:50:58 +0200414int setup_files(struct thread_data *td)
415{
416 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100417 unsigned int i;
418 int err;
Jens Axboe53cdc682006-10-18 11:50:58 +0200419
420 /*
421 * if ioengine defines a setup() method, it's responsible for
422 * setting up everything in the td->files[] area.
423 */
424 if (td->io_ops->setup)
425 return td->io_ops->setup(td);
426
427 if (create_files(td))
428 return 1;
429
Jens Axboe21972cd2006-11-23 12:39:16 +0100430 err = open_files(td);
Jens Axboef1027062006-10-20 10:14:01 +0200431 if (err)
432 return err;
433
Jens Axboe0a7eb122006-10-20 10:36:42 +0200434 /*
435 * Recalculate the total file size now that files are set up.
436 */
437 td->total_file_size = 0;
438 for_each_file(td, f, i)
439 td->total_file_size += f->file_size;
440
Jens Axboef1027062006-10-20 10:14:01 +0200441 td->io_size = td->total_file_size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200442 if (td->io_size == 0) {
443 log_err("%s: no io blocks\n", td->name);
Jens Axboee1161c32007-02-22 19:36:48 +0100444 td_verror(td, EINVAL, "total_file_size");
Jens Axboe53cdc682006-10-18 11:50:58 +0200445 return 1;
446 }
447
448 if (!td->zone_size)
449 td->zone_size = td->io_size;
450
451 td->total_io_size = td->io_size * td->loops;
452
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100453 for_each_file(td, f, i)
Jens Axboeb5af8292007-03-08 12:43:13 +0100454 td_io_close_file(td, f);
Jens Axboe21972cd2006-11-23 12:39:16 +0100455
456 return err;
Jens Axboe53cdc682006-10-18 11:50:58 +0200457}
458
459void close_files(struct thread_data *td)
460{
Jens Axboe0ab8db82006-10-18 17:16:23 +0200461 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +0100462 unsigned int i;
Jens Axboe53cdc682006-10-18 11:50:58 +0200463
Jens Axboe0ab8db82006-10-18 17:16:23 +0200464 for_each_file(td, f, i) {
Jens Axboe1315a682007-03-13 11:25:07 +0100465 if (!f->file_name && (f->flags & FIO_FILE_UNLINK) &&
Jens Axboeaf52b342007-03-13 10:07:47 +0100466 f->filetype == FIO_TYPE_FILE) {
Jens Axboe132ad462006-11-23 12:23:12 +0100467 unlink(f->file_name);
Jens Axboe1315a682007-03-13 11:25:07 +0100468 free(f->file_name);
Jens Axboe132ad462006-11-23 12:23:12 +0100469 f->file_name = NULL;
470 }
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100471
Jens Axboeb5af8292007-03-08 12:43:13 +0100472 td_io_close_file(td, f);
Jens Axboeb3dc7f02007-03-09 12:52:15 +0100473
474 if (f->file_map)
475 free(f->file_map);
Jens Axboe53cdc682006-10-18 11:50:58 +0200476 }
Jens Axboeb4a6a592006-10-20 13:54:47 +0200477
Jens Axboe132ad462006-11-23 12:23:12 +0100478 td->filename = NULL;
Jens Axboeb4a6a592006-10-20 13:54:47 +0200479 td->files = NULL;
480 td->nr_files = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200481}
Jens Axboeaf52b342007-03-13 10:07:47 +0100482
Jens Axboee3bab462007-03-13 11:22:09 +0100483static void get_file_type(struct fio_file *f)
Jens Axboeaf52b342007-03-13 10:07:47 +0100484{
485 struct stat sb;
486
487 f->filetype = FIO_TYPE_FILE;
488
Jens Axboee3bab462007-03-13 11:22:09 +0100489 if (!lstat(f->file_name, &sb)) {
Jens Axboe96d32d52007-03-14 09:16:09 +0100490 f->flags |= FIO_FILE_EXISTS;
491
Jens Axboeaf52b342007-03-13 10:07:47 +0100492 if (S_ISBLK(sb.st_mode))
493 f->filetype = FIO_TYPE_BD;
494 else if (S_ISCHR(sb.st_mode))
495 f->filetype = FIO_TYPE_CHAR;
Jens Axboe96d32d52007-03-14 09:16:09 +0100496 else {
497 /*
498 * might as well do this here, and save a stat later on
499 */
500 f->real_file_size = sb.st_size;
501 f->file_size = f->real_file_size;
502 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100503 }
504}
505
506void add_file(struct thread_data *td, const char *fname)
507{
Jens Axboe7b4e4fe2007-03-13 12:51:40 +0100508 int cur_files = td->files_index;
Jens Axboeaf52b342007-03-13 10:07:47 +0100509 struct fio_file *f;
510
511 td->files = realloc(td->files, (cur_files + 1) * sizeof(*f));
512
513 f = &td->files[cur_files];
514 memset(f, 0, sizeof(*f));
515 f->fd = -1;
Jens Axboecae61952007-03-13 11:12:14 +0100516 f->file_name = strdup(fname);
Jens Axboeaf52b342007-03-13 10:07:47 +0100517
Jens Axboee3bab462007-03-13 11:22:09 +0100518 get_file_type(f);
Jens Axboeaf52b342007-03-13 10:07:47 +0100519
Jens Axboe7b4e4fe2007-03-13 12:51:40 +0100520 td->files_index++;
Jens Axboe15494412007-03-13 11:52:47 +0100521 if (f->filetype == FIO_TYPE_FILE)
522 td->nr_normal_files++;
Jens Axboeaf52b342007-03-13 10:07:47 +0100523}
Jens Axboe0ad920e2007-03-13 11:06:45 +0100524
525void get_file(struct fio_file *f)
526{
527 f->references++;
528}
529
530void put_file(struct thread_data *td, struct fio_file *f)
531{
532 if (!(f->flags & FIO_FILE_OPEN))
533 return;
534
535 assert(f->references);
536 if (--f->references)
537 return;
538
Jens Axboeebb14152007-03-13 14:42:15 +0100539 if (should_fsync(td) && td->fsync_on_close)
540 fsync(f->fd);
541
Jens Axboe0ad920e2007-03-13 11:06:45 +0100542 if (td->io_ops->close_file)
543 td->io_ops->close_file(td, f);
544 td->nr_open_files--;
545 f->flags &= ~FIO_FILE_OPEN;
546}
Jens Axboebbf6b542007-03-13 15:28:55 +0100547
548static int recurse_dir(struct thread_data *td, const char *dirname)
549{
550 struct dirent *dir;
551 int ret = 0;
552 DIR *D;
553
554 D = opendir(dirname);
555 if (!D) {
556 td_verror(td, errno, "opendir");
557 return 1;
558 }
559
560 while ((dir = readdir(D)) != NULL) {
561 char full_path[PATH_MAX];
562 struct stat sb;
563
Jens Axboee85b2b82007-03-14 09:39:06 +0100564 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
565 continue;
Jens Axboe96d32d52007-03-14 09:16:09 +0100566
Jens Axboebbf6b542007-03-13 15:28:55 +0100567 sprintf(full_path, "%s/%s", dirname, dir->d_name);
568
569 if (lstat(full_path, &sb) == -1) {
570 if (errno != ENOENT) {
571 td_verror(td, errno, "stat");
572 return 1;
573 }
574 }
575
576 if (S_ISREG(sb.st_mode)) {
577 add_file(td, full_path);
578 td->nr_files++;
579 continue;
580 }
581
Jens Axboebbf6b542007-03-13 15:28:55 +0100582 if ((ret = recurse_dir(td, full_path)) != 0)
583 break;
584 }
585
586 closedir(D);
587 return ret;
588}
589
590int add_dir_files(struct thread_data *td, const char *path)
591{
592 return recurse_dir(td, path);
593}