Commit | Line | Data |
---|---|---|
53cdc686 JA |
1 | #include <unistd.h> |
2 | #include <fcntl.h> | |
3 | #include <string.h> | |
4 | #include <assert.h> | |
bbf6b540 | 5 | #include <dirent.h> |
53cdc686 JA |
6 | #include <sys/stat.h> |
7 | #include <sys/mman.h> | |
bbf6b540 | 8 | #include <sys/types.h> |
53cdc686 JA |
9 | |
10 | #include "fio.h" | |
11 | #include "os.h" | |
12 | ||
25205e97 JA |
13 | /* |
14 | * Check if the file exists and it's large enough. | |
15 | */ | |
16 | static int file_ok(struct thread_data *td, struct fio_file *f) | |
53cdc686 | 17 | { |
b2a15192 | 18 | struct stat st; |
53cdc686 | 19 | |
af52b345 | 20 | if (f->filetype != FIO_TYPE_FILE || |
b5af8293 | 21 | (td->io_ops->flags & FIO_DISKLESSIO)) |
b2a15192 JA |
22 | return 0; |
23 | ||
fa01d139 | 24 | if (lstat(f->file_name, &st) == -1) |
25205e97 | 25 | return 1; |
fa01d139 JA |
26 | |
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) | |
25205e97 JA |
33 | return 1; |
34 | ||
35 | return 0; | |
36 | } | |
37 | ||
38 | static 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; | |
b2a15192 | 44 | |
53cdc686 JA |
45 | f->fd = open(f->file_name, O_WRONLY | O_CREAT | O_TRUNC, 0644); |
46 | if (f->fd < 0) { | |
e1161c32 | 47 | td_verror(td, errno, "open"); |
53cdc686 JA |
48 | return 1; |
49 | } | |
50 | ||
51 | if (ftruncate(f->fd, f->file_size) == -1) { | |
e1161c32 | 52 | td_verror(td, errno, "ftruncate"); |
53cdc686 JA |
53 | goto err; |
54 | } | |
55 | ||
40f8298c | 56 | if (posix_fallocate(f->fd, 0, f->file_size) < 0) { |
e1161c32 | 57 | td_verror(td, errno, "posix_fallocate"); |
40f8298c JA |
58 | goto err; |
59 | } | |
60 | ||
2dc1bbeb JA |
61 | b = malloc(td->o.max_bs[DDIR_WRITE]); |
62 | memset(b, 0, td->o.max_bs[DDIR_WRITE]); | |
53cdc686 JA |
63 | |
64 | left = f->file_size; | |
65 | while (left && !td->terminate) { | |
2dc1bbeb | 66 | bs = td->o.max_bs[DDIR_WRITE]; |
53cdc686 JA |
67 | 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) | |
e1161c32 | 77 | td_verror(td, errno, "write"); |
53cdc686 | 78 | else |
e1161c32 | 79 | td_verror(td, EIO, "write"); |
53cdc686 JA |
80 | |
81 | break; | |
82 | } | |
83 | } | |
84 | ||
85 | if (td->terminate) | |
86 | unlink(f->file_name); | |
2dc1bbeb | 87 | else if (td->o.create_fsync) |
53cdc686 JA |
88 | fsync(f->fd); |
89 | ||
90 | free(b); | |
91 | close(f->fd); | |
92 | f->fd = -1; | |
93 | return 0; | |
94 | err: | |
95 | close(f->fd); | |
96 | f->fd = -1; | |
97 | return 1; | |
98 | } | |
99 | ||
9c60ce64 JA |
100 | static 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 | ||
2dc1bbeb JA |
107 | if (upper > td->o.file_size_high) |
108 | upper = td->o.file_size_high; | |
109 | else if (upper < td->o.file_size_low) | |
9c60ce64 JA |
110 | return 0; |
111 | else if (!upper) | |
112 | return 0; | |
113 | ||
114 | r = os_random_long(&td->file_size_state); | |
2dc1bbeb JA |
115 | ret = td->o.file_size_low + (unsigned long long) ((double) upper * (r / (RAND_MAX + 1.0))); |
116 | ret -= (ret % td->o.rw_min_bs); | |
9c60ce64 JA |
117 | if (ret > upper) |
118 | ret = upper; | |
119 | return ret; | |
120 | } | |
121 | ||
68727076 JA |
122 | static int fill_file_size(struct thread_data *td, struct fio_file *f, |
123 | unsigned long long *file_size, int new_files) | |
124 | { | |
125 | if (!td->o.file_size_low) { | |
126 | f->file_size = *file_size / new_files; | |
127 | f->real_file_size = f->file_size; | |
128 | } else { | |
129 | /* | |
130 | * If we don't have enough space left for a file | |
131 | * of the minimum size, bail. | |
132 | */ | |
133 | if (*file_size < td->o.file_size_low) | |
134 | return 1; | |
135 | ||
136 | f->file_size = set_rand_file_size(td, *file_size); | |
137 | f->real_file_size = f->file_size; | |
138 | *file_size -= f->file_size; | |
139 | } | |
140 | ||
141 | return 0; | |
142 | } | |
143 | ||
53cdc686 JA |
144 | static int create_files(struct thread_data *td) |
145 | { | |
146 | struct fio_file *f; | |
af52b345 | 147 | int err, need_create, can_extend; |
e8be395b | 148 | unsigned long long total_file_size, local_file_size, create_size; |
e407bec2 | 149 | unsigned int i, new_files; |
53cdc686 | 150 | |
e407bec2 | 151 | new_files = 0; |
2dc1bbeb | 152 | total_file_size = td->o.size; |
1549441c | 153 | for_each_file(td, f, i) { |
96d32d51 JA |
154 | unsigned long long s; |
155 | ||
2dc1bbeb | 156 | f->file_offset = td->o.start_offset; |
96d32d51 | 157 | |
1549441c JA |
158 | if (f->filetype != FIO_TYPE_FILE) |
159 | continue; | |
ee3dcd95 JA |
160 | if (!total_file_size) |
161 | continue; | |
1549441c | 162 | |
96d32d51 | 163 | if (f->flags & FIO_FILE_EXISTS) { |
ee3dcd95 JA |
164 | if ((f->file_size > td->o.size / td->o.nr_files) || |
165 | !f->file_size) | |
166 | f->file_size = td->o.size / td->o.nr_files; | |
167 | ||
96d32d51 JA |
168 | s = f->file_size; |
169 | if (s > total_file_size) | |
170 | s = total_file_size; | |
171 | ||
172 | total_file_size -= s; | |
e407bec2 JA |
173 | } else |
174 | new_files++; | |
1549441c | 175 | } |
f697125a | 176 | |
53cdc686 JA |
177 | /* |
178 | * unless specifically asked for overwrite, let normal io extend it | |
179 | */ | |
2dc1bbeb | 180 | can_extend = !td->o.overwrite && !(td->io_ops->flags & FIO_NOEXTEND); |
3f344202 | 181 | if (can_extend && new_files) { |
68727076 JA |
182 | for_each_file(td, f, i) { |
183 | if (fill_file_size(td, f, &total_file_size, new_files)) { | |
184 | log_info("fio: limited to %d files\n", i); | |
185 | td->o.nr_files = i; | |
186 | break; | |
187 | } | |
188 | } | |
189 | ||
53cdc686 | 190 | return 0; |
68727076 | 191 | } |
53cdc686 | 192 | |
9c60ce64 JA |
193 | local_file_size = total_file_size; |
194 | if (!local_file_size) | |
195 | local_file_size = -1; | |
196 | ||
f4c4f4d7 JA |
197 | total_file_size = 0; |
198 | need_create = 0; | |
e8be395b | 199 | create_size = 0; |
af52b345 JA |
200 | for_each_file(td, f, i) { |
201 | int file_there; | |
202 | ||
203 | if (f->filetype != FIO_TYPE_FILE) | |
204 | continue; | |
f4c4f4d7 JA |
205 | if (f->flags & FIO_FILE_EXISTS) { |
206 | total_file_size += f->file_size; | |
96d32d51 | 207 | continue; |
f4c4f4d7 | 208 | } |
96d32d51 | 209 | |
68727076 JA |
210 | if (fill_file_size(td, f, &local_file_size, new_files)) { |
211 | log_info("fio: limited to %d files\n", i); | |
212 | new_files -= (td->o.nr_files - i); | |
213 | td->o.nr_files = i; | |
214 | break; | |
9c60ce64 | 215 | } |
eff6861d | 216 | |
f4c4f4d7 | 217 | total_file_size += f->file_size; |
e8be395b | 218 | create_size += f->file_size; |
af52b345 | 219 | file_there = !file_ok(td, f); |
eff6861d | 220 | |
2dc1bbeb | 221 | if (file_there && td_write(td) && !td->o.overwrite) { |
af52b345 JA |
222 | unlink(f->file_name); |
223 | file_there = 0; | |
eff6861d | 224 | } |
af52b345 JA |
225 | |
226 | need_create += !file_there; | |
eff6861d | 227 | } |
25205e97 | 228 | |
25205e97 JA |
229 | if (!need_create) |
230 | return 0; | |
231 | ||
2dc1bbeb | 232 | if (!td->o.size && !total_file_size) { |
f1027063 | 233 | log_err("Need size for create\n"); |
e1161c32 | 234 | td_verror(td, EINVAL, "file_size"); |
f1027063 JA |
235 | return 1; |
236 | } | |
237 | ||
53cdc686 | 238 | temp_stall_ts = 1; |
6d86144d | 239 | log_info("%s: Laying out IO file(s) (%u files / %LuMiB)\n", |
2dc1bbeb | 240 | td->o.name, new_files, create_size >> 20); |
53cdc686 JA |
241 | |
242 | err = 0; | |
243 | for_each_file(td, f, i) { | |
9b031dc8 JA |
244 | /* |
245 | * Only unlink files that we created. | |
246 | */ | |
f11bd94d | 247 | f->flags &= ~FIO_FILE_UNLINK; |
25205e97 | 248 | if (file_ok(td, f)) { |
2dc1bbeb | 249 | if (td->o.unlink) |
f11bd94d JA |
250 | f->flags |= FIO_FILE_UNLINK; |
251 | ||
160b966d | 252 | f->flags |= FIO_FILE_NOSORT; |
25205e97 JA |
253 | err = create_file(td, f); |
254 | if (err) | |
255 | break; | |
256 | } | |
53cdc686 JA |
257 | } |
258 | ||
259 | temp_stall_ts = 0; | |
260 | return err; | |
261 | } | |
262 | ||
263 | static int file_size(struct thread_data *td, struct fio_file *f) | |
264 | { | |
265 | struct stat st; | |
266 | ||
2dc1bbeb | 267 | if (td->o.overwrite) { |
53cdc686 | 268 | if (fstat(f->fd, &st) == -1) { |
e1161c32 | 269 | td_verror(td, errno, "fstat"); |
53cdc686 JA |
270 | return 1; |
271 | } | |
272 | ||
273 | f->real_file_size = st.st_size; | |
274 | ||
275 | if (!f->file_size || f->file_size > f->real_file_size) | |
276 | f->file_size = f->real_file_size; | |
745508dd JA |
277 | } else |
278 | f->real_file_size = f->file_size; | |
53cdc686 | 279 | |
53cdc686 JA |
280 | return 0; |
281 | } | |
282 | ||
283 | static int bdev_size(struct thread_data *td, struct fio_file *f) | |
284 | { | |
285 | unsigned long long bytes; | |
286 | int r; | |
287 | ||
288 | r = blockdev_size(f->fd, &bytes); | |
289 | if (r) { | |
e1161c32 | 290 | td_verror(td, r, "blockdev_size"); |
53cdc686 JA |
291 | return 1; |
292 | } | |
293 | ||
294 | f->real_file_size = bytes; | |
295 | ||
296 | /* | |
297 | * no extend possibilities, so limit size to device size if too large | |
298 | */ | |
299 | if (!f->file_size || f->file_size > f->real_file_size) | |
300 | f->file_size = f->real_file_size; | |
301 | ||
302 | f->file_size -= f->file_offset; | |
303 | return 0; | |
304 | } | |
305 | ||
306 | static int get_file_size(struct thread_data *td, struct fio_file *f) | |
307 | { | |
308 | int ret = 0; | |
309 | ||
96d32d51 JA |
310 | if (f->filetype == FIO_TYPE_FILE) { |
311 | if (!(f->flags & FIO_FILE_EXISTS)) | |
312 | ret = file_size(td, f); | |
313 | } else if (f->filetype == FIO_TYPE_BD) | |
53cdc686 JA |
314 | ret = bdev_size(td, f); |
315 | else | |
316 | f->real_file_size = -1; | |
317 | ||
318 | if (ret) | |
319 | return ret; | |
320 | ||
321 | if (f->file_offset > f->real_file_size) { | |
2dc1bbeb | 322 | log_err("%s: offset extends end (%Lu > %Lu)\n", td->o.name, f->file_offset, f->real_file_size); |
53cdc686 JA |
323 | return 1; |
324 | } | |
325 | ||
53cdc686 JA |
326 | return 0; |
327 | } | |
328 | ||
e5b401d4 JA |
329 | int file_invalidate_cache(struct thread_data *td, struct fio_file *f) |
330 | { | |
331 | int ret = 0; | |
332 | ||
2dc1bbeb | 333 | if (td->o.odirect) |
b5af8293 JA |
334 | return 0; |
335 | ||
e5b401d4 JA |
336 | /* |
337 | * FIXME: add blockdev flushing too | |
338 | */ | |
b5af8293 | 339 | if (f->mmap) |
e5b401d4 | 340 | ret = madvise(f->mmap, f->file_size, MADV_DONTNEED); |
467d1b6b | 341 | else if (f->filetype == FIO_TYPE_FILE) |
b5af8293 | 342 | ret = fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_DONTNEED); |
467d1b6b | 343 | else if (f->filetype == FIO_TYPE_BD) |
b5af8293 | 344 | ret = blockdev_invalidate_cache(f->fd); |
467d1b6b | 345 | else if (f->filetype == FIO_TYPE_CHAR) |
e5b401d4 JA |
346 | ret = 0; |
347 | ||
348 | if (ret < 0) { | |
e1161c32 | 349 | td_verror(td, errno, "invalidate_cache"); |
e5b401d4 JA |
350 | return 1; |
351 | } | |
352 | ||
ad2da605 | 353 | return ret; |
e5b401d4 JA |
354 | } |
355 | ||
b5af8293 | 356 | void generic_close_file(struct thread_data fio_unused *td, struct fio_file *f) |
53cdc686 | 357 | { |
b5af8293 JA |
358 | close(f->fd); |
359 | f->fd = -1; | |
53cdc686 JA |
360 | } |
361 | ||
b5af8293 | 362 | int generic_open_file(struct thread_data *td, struct fio_file *f) |
53cdc686 | 363 | { |
53cdc686 JA |
364 | int flags = 0; |
365 | ||
2dc1bbeb | 366 | if (td->o.odirect) |
2fd233b7 | 367 | flags |= OS_O_DIRECT; |
2dc1bbeb | 368 | if (td->o.sync_io) |
2fd233b7 | 369 | flags |= O_SYNC; |
53cdc686 | 370 | |
2fd233b7 JA |
371 | if (td_write(td) || td_rw(td)) { |
372 | flags |= O_RDWR; | |
53cdc686 | 373 | |
af52b345 | 374 | if (f->filetype == FIO_TYPE_FILE) |
2fd233b7 | 375 | flags |= O_CREAT; |
2fd233b7 | 376 | |
b5af8293 | 377 | f->fd = open(f->file_name, flags, 0600); |
2fd233b7 | 378 | } else { |
af52b345 | 379 | if (f->filetype == FIO_TYPE_CHAR) |
2fd233b7 JA |
380 | flags |= O_RDWR; |
381 | else | |
382 | flags |= O_RDONLY; | |
383 | ||
b5af8293 | 384 | f->fd = open(f->file_name, flags); |
53cdc686 JA |
385 | } |
386 | ||
387 | if (f->fd == -1) { | |
e4e33258 | 388 | char buf[FIO_VERROR_SIZE]; |
e1161c32 JA |
389 | int __e = errno; |
390 | ||
e4e33258 JA |
391 | snprintf(buf, sizeof(buf) - 1, "open(%s)", f->file_name); |
392 | ||
393 | td_verror(td, __e, buf); | |
2dc1bbeb | 394 | if (__e == EINVAL && td->o.odirect) |
9d80e114 | 395 | log_err("fio: destination does not support O_DIRECT\n"); |
8ab53873 | 396 | if (__e == EMFILE) |
2dc1bbeb | 397 | log_err("fio: try reducing/setting openfiles (failed at %u of %u)\n", td->nr_open_files, td->o.nr_files); |
53cdc686 JA |
398 | return 1; |
399 | } | |
400 | ||
b5af8293 JA |
401 | if (get_file_size(td, f)) |
402 | goto err; | |
403 | ||
2dc1bbeb | 404 | if (td->o.invalidate_cache && file_invalidate_cache(td, f)) |
b5af8293 JA |
405 | goto err; |
406 | ||
d2f3ac35 JA |
407 | if (!td->o.fadvise_hint) |
408 | return 0; | |
409 | ||
b5af8293 JA |
410 | if (!td_random(td)) { |
411 | if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_SEQUENTIAL) < 0) { | |
412 | td_verror(td, errno, "fadvise"); | |
413 | goto err; | |
414 | } | |
415 | } else { | |
416 | if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_RANDOM) < 0) { | |
417 | td_verror(td, errno, "fadvise"); | |
418 | goto err; | |
419 | } | |
bdb4e2e9 | 420 | } |
53cdc686 JA |
421 | |
422 | return 0; | |
b5af8293 JA |
423 | err: |
424 | close(f->fd); | |
425 | return 1; | |
426 | } | |
427 | ||
21972cde JA |
428 | int open_files(struct thread_data *td) |
429 | { | |
430 | struct fio_file *f; | |
af52b345 JA |
431 | unsigned int i; |
432 | int err = 0; | |
21972cde JA |
433 | |
434 | for_each_file(td, f, i) { | |
b5af8293 | 435 | err = td_io_open_file(td, f); |
21972cde JA |
436 | if (err) |
437 | break; | |
b5af8293 | 438 | |
2dc1bbeb | 439 | if (td->o.open_files == td->nr_open_files) |
b5af8293 | 440 | break; |
21972cde JA |
441 | } |
442 | ||
7abf833d JA |
443 | if (!err) |
444 | return 0; | |
445 | ||
bdb4e2e9 | 446 | for_each_file(td, f, i) |
b5af8293 | 447 | td_io_close_file(td, f); |
7abf833d | 448 | |
21972cde JA |
449 | return err; |
450 | } | |
451 | ||
53cdc686 JA |
452 | int setup_files(struct thread_data *td) |
453 | { | |
454 | struct fio_file *f; | |
af52b345 JA |
455 | unsigned int i; |
456 | int err; | |
53cdc686 JA |
457 | |
458 | /* | |
459 | * if ioengine defines a setup() method, it's responsible for | |
460 | * setting up everything in the td->files[] area. | |
461 | */ | |
462 | if (td->io_ops->setup) | |
463 | return td->io_ops->setup(td); | |
464 | ||
465 | if (create_files(td)) | |
466 | return 1; | |
467 | ||
21972cde | 468 | err = open_files(td); |
f1027063 JA |
469 | if (err) |
470 | return err; | |
471 | ||
0a7eb121 JA |
472 | /* |
473 | * Recalculate the total file size now that files are set up. | |
474 | */ | |
2dc1bbeb | 475 | td->o.size = 0; |
0a7eb121 | 476 | for_each_file(td, f, i) |
2dc1bbeb | 477 | td->o.size += f->file_size; |
0a7eb121 | 478 | |
2dc1bbeb | 479 | td->io_size = td->o.size; |
53cdc686 | 480 | if (td->io_size == 0) { |
2dc1bbeb | 481 | log_err("%s: no io blocks\n", td->o.name); |
e1161c32 | 482 | td_verror(td, EINVAL, "total_file_size"); |
53cdc686 JA |
483 | return 1; |
484 | } | |
485 | ||
2dc1bbeb JA |
486 | if (!td->o.zone_size) |
487 | td->o.zone_size = td->io_size; | |
53cdc686 | 488 | |
2dc1bbeb | 489 | td->total_io_size = td->io_size * td->o.loops; |
53cdc686 | 490 | |
bdb4e2e9 | 491 | for_each_file(td, f, i) |
b5af8293 | 492 | td_io_close_file(td, f); |
21972cde JA |
493 | |
494 | return err; | |
53cdc686 JA |
495 | } |
496 | ||
68727076 JA |
497 | int init_random_map(struct thread_data *td) |
498 | { | |
499 | int num_maps, blocks; | |
500 | struct fio_file *f; | |
501 | unsigned int i; | |
502 | ||
503 | if (td->o.norandommap) | |
504 | return 0; | |
505 | ||
506 | for_each_file(td, f, i) { | |
507 | blocks = (f->real_file_size + td->o.rw_min_bs - 1) / td->o.rw_min_bs; | |
508 | num_maps = (blocks + BLOCKS_PER_MAP-1)/ BLOCKS_PER_MAP; | |
509 | f->file_map = malloc(num_maps * sizeof(long)); | |
510 | if (!f->file_map) { | |
511 | log_err("fio: failed allocating random map. If running a large number of jobs, try the 'norandommap' option\n"); | |
512 | return 1; | |
513 | } | |
514 | f->num_maps = num_maps; | |
515 | memset(f->file_map, 0, num_maps * sizeof(long)); | |
516 | } | |
517 | ||
518 | return 0; | |
519 | } | |
520 | ||
53cdc686 JA |
521 | void close_files(struct thread_data *td) |
522 | { | |
0ab8db89 | 523 | struct fio_file *f; |
af52b345 | 524 | unsigned int i; |
53cdc686 | 525 | |
0ab8db89 | 526 | for_each_file(td, f, i) { |
fa1da865 JA |
527 | if ((f->flags & FIO_FILE_UNLINK) && |
528 | f->filetype == FIO_TYPE_FILE) | |
132ad46d | 529 | unlink(f->file_name); |
bdb4e2e9 | 530 | |
b5af8293 | 531 | td_io_close_file(td, f); |
b3dc7f07 | 532 | |
fa1da865 JA |
533 | free(f->file_name); |
534 | f->file_name = NULL; | |
535 | ||
c343981b | 536 | if (f->file_map) { |
b3dc7f07 | 537 | free(f->file_map); |
c343981b JA |
538 | f->file_map = NULL; |
539 | } | |
53cdc686 | 540 | } |
b4a6a59a | 541 | |
2dc1bbeb | 542 | td->o.filename = NULL; |
cade3ef4 | 543 | free(td->files); |
b4a6a59a | 544 | td->files = NULL; |
2dc1bbeb | 545 | td->o.nr_files = 0; |
53cdc686 | 546 | } |
af52b345 | 547 | |
e3bab463 | 548 | static void get_file_type(struct fio_file *f) |
af52b345 JA |
549 | { |
550 | struct stat sb; | |
551 | ||
552 | f->filetype = FIO_TYPE_FILE; | |
553 | ||
e3bab463 | 554 | if (!lstat(f->file_name, &sb)) { |
96d32d51 JA |
555 | f->flags |= FIO_FILE_EXISTS; |
556 | ||
af52b345 JA |
557 | if (S_ISBLK(sb.st_mode)) |
558 | f->filetype = FIO_TYPE_BD; | |
559 | else if (S_ISCHR(sb.st_mode)) | |
560 | f->filetype = FIO_TYPE_CHAR; | |
96d32d51 JA |
561 | else { |
562 | /* | |
563 | * might as well do this here, and save a stat later on | |
564 | */ | |
565 | f->real_file_size = sb.st_size; | |
566 | f->file_size = f->real_file_size; | |
567 | } | |
af52b345 JA |
568 | } |
569 | } | |
570 | ||
571 | void add_file(struct thread_data *td, const char *fname) | |
572 | { | |
7b4e4fe5 | 573 | int cur_files = td->files_index; |
bd0ee748 | 574 | char file_name[PATH_MAX]; |
af52b345 | 575 | struct fio_file *f; |
bd0ee748 | 576 | int len = 0; |
af52b345 JA |
577 | |
578 | td->files = realloc(td->files, (cur_files + 1) * sizeof(*f)); | |
579 | ||
580 | f = &td->files[cur_files]; | |
581 | memset(f, 0, sizeof(*f)); | |
582 | f->fd = -1; | |
bd0ee748 JA |
583 | |
584 | if (td->o.directory) | |
585 | len = sprintf(file_name, "%s/", td->o.directory); | |
586 | ||
587 | sprintf(file_name + len, "%s", fname); | |
588 | f->file_name = strdup(file_name); | |
af52b345 | 589 | |
e3bab463 | 590 | get_file_type(f); |
af52b345 | 591 | |
7b4e4fe5 | 592 | td->files_index++; |
1549441c JA |
593 | if (f->filetype == FIO_TYPE_FILE) |
594 | td->nr_normal_files++; | |
af52b345 | 595 | } |
0ad920e7 JA |
596 | |
597 | void get_file(struct fio_file *f) | |
598 | { | |
599 | f->references++; | |
600 | } | |
601 | ||
602 | void put_file(struct thread_data *td, struct fio_file *f) | |
603 | { | |
604 | if (!(f->flags & FIO_FILE_OPEN)) | |
605 | return; | |
606 | ||
607 | assert(f->references); | |
608 | if (--f->references) | |
609 | return; | |
610 | ||
2dc1bbeb | 611 | if (should_fsync(td) && td->o.fsync_on_close) |
ebb1415f JA |
612 | fsync(f->fd); |
613 | ||
0ad920e7 JA |
614 | if (td->io_ops->close_file) |
615 | td->io_ops->close_file(td, f); | |
616 | td->nr_open_files--; | |
617 | f->flags &= ~FIO_FILE_OPEN; | |
618 | } | |
bbf6b540 JA |
619 | |
620 | static int recurse_dir(struct thread_data *td, const char *dirname) | |
621 | { | |
622 | struct dirent *dir; | |
623 | int ret = 0; | |
624 | DIR *D; | |
625 | ||
626 | D = opendir(dirname); | |
627 | if (!D) { | |
628 | td_verror(td, errno, "opendir"); | |
629 | return 1; | |
630 | } | |
631 | ||
632 | while ((dir = readdir(D)) != NULL) { | |
633 | char full_path[PATH_MAX]; | |
634 | struct stat sb; | |
635 | ||
e85b2b83 JA |
636 | if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, "..")) |
637 | continue; | |
96d32d51 | 638 | |
bbf6b540 JA |
639 | sprintf(full_path, "%s/%s", dirname, dir->d_name); |
640 | ||
641 | if (lstat(full_path, &sb) == -1) { | |
642 | if (errno != ENOENT) { | |
643 | td_verror(td, errno, "stat"); | |
644 | return 1; | |
645 | } | |
646 | } | |
647 | ||
648 | if (S_ISREG(sb.st_mode)) { | |
649 | add_file(td, full_path); | |
2dc1bbeb | 650 | td->o.nr_files++; |
bbf6b540 JA |
651 | continue; |
652 | } | |
653 | ||
bbf6b540 JA |
654 | if ((ret = recurse_dir(td, full_path)) != 0) |
655 | break; | |
656 | } | |
657 | ||
658 | closedir(D); | |
659 | return ret; | |
660 | } | |
661 | ||
662 | int add_dir_files(struct thread_data *td, const char *path) | |
663 | { | |
664 | return recurse_dir(td, path); | |
665 | } | |
cade3ef4 JA |
666 | |
667 | void dup_files(struct thread_data *td, struct thread_data *org) | |
668 | { | |
669 | struct fio_file *f; | |
670 | unsigned int i; | |
671 | size_t bytes; | |
672 | ||
673 | if (!org->files) | |
674 | return; | |
675 | ||
676 | bytes = org->files_index * sizeof(*f); | |
677 | td->files = malloc(bytes); | |
678 | memcpy(td->files, org->files, bytes); | |
679 | ||
680 | for_each_file(td, f, i) { | |
681 | if (f->file_name) | |
682 | f->file_name = strdup(f->file_name); | |
683 | } | |
684 | } |