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" | |
53cdc686 | 11 | |
7bb48f84 | 12 | static int extend_file(struct thread_data *td, struct fio_file *f) |
25205e97 | 13 | { |
ea443657 | 14 | int r, new_layout = 0, unlink_file = 0, flags; |
25205e97 JA |
15 | unsigned long long left; |
16 | unsigned int bs; | |
17 | char *b; | |
b2a15192 | 18 | |
507a702f JA |
19 | /* |
20 | * check if we need to lay the file out complete again. fio | |
21 | * does that for operations involving reads, or for writes | |
22 | * where overwrite is set | |
23 | */ | |
24 | if (td_read(td) || (td_write(td) && td->o.overwrite)) | |
25 | new_layout = 1; | |
ea443657 JA |
26 | if (td_write(td) && !td->o.overwrite) |
27 | unlink_file = 1; | |
507a702f | 28 | |
ea443657 | 29 | if ((unlink_file || new_layout) && (f->flags & FIO_FILE_EXISTS)) { |
7bb48f84 JA |
30 | if (unlink(f->file_name) < 0) { |
31 | td_verror(td, errno, "unlink"); | |
32 | return 1; | |
33 | } | |
34 | } | |
35 | ||
507a702f JA |
36 | flags = O_WRONLY | O_CREAT; |
37 | if (new_layout) | |
38 | flags |= O_TRUNC; | |
39 | ||
40 | f->fd = open(f->file_name, flags, 0644); | |
53cdc686 | 41 | if (f->fd < 0) { |
e1161c32 | 42 | td_verror(td, errno, "open"); |
53cdc686 JA |
43 | return 1; |
44 | } | |
45 | ||
7bb48f84 | 46 | if (ftruncate(f->fd, f->real_file_size) == -1) { |
e1161c32 | 47 | td_verror(td, errno, "ftruncate"); |
53cdc686 JA |
48 | goto err; |
49 | } | |
50 | ||
e8be2ad7 JA |
51 | if (!new_layout) |
52 | goto done; | |
53 | ||
7bb48f84 | 54 | if (posix_fallocate(f->fd, 0, f->real_file_size) < 0) { |
e1161c32 | 55 | td_verror(td, errno, "posix_fallocate"); |
40f8298c JA |
56 | goto err; |
57 | } | |
58 | ||
2dc1bbeb JA |
59 | b = malloc(td->o.max_bs[DDIR_WRITE]); |
60 | memset(b, 0, td->o.max_bs[DDIR_WRITE]); | |
53cdc686 | 61 | |
7bb48f84 | 62 | left = f->real_file_size; |
53cdc686 | 63 | while (left && !td->terminate) { |
2dc1bbeb | 64 | bs = td->o.max_bs[DDIR_WRITE]; |
53cdc686 JA |
65 | if (bs > left) |
66 | bs = left; | |
67 | ||
68 | r = write(f->fd, b, bs); | |
69 | ||
70 | if (r == (int) bs) { | |
71 | left -= bs; | |
72 | continue; | |
73 | } else { | |
74 | if (r < 0) | |
e1161c32 | 75 | td_verror(td, errno, "write"); |
53cdc686 | 76 | else |
e1161c32 | 77 | td_verror(td, EIO, "write"); |
53cdc686 JA |
78 | |
79 | break; | |
80 | } | |
81 | } | |
82 | ||
83 | if (td->terminate) | |
84 | unlink(f->file_name); | |
2dc1bbeb | 85 | else if (td->o.create_fsync) |
53cdc686 JA |
86 | fsync(f->fd); |
87 | ||
88 | free(b); | |
507a702f | 89 | done: |
53cdc686 JA |
90 | close(f->fd); |
91 | f->fd = -1; | |
92 | return 0; | |
93 | err: | |
94 | close(f->fd); | |
95 | f->fd = -1; | |
96 | return 1; | |
97 | } | |
98 | ||
7bb48f84 | 99 | static unsigned long long get_rand_file_size(struct thread_data *td) |
9c60ce64 | 100 | { |
9c60ce64 JA |
101 | unsigned long long ret; |
102 | long r; | |
103 | ||
9c60ce64 | 104 | r = os_random_long(&td->file_size_state); |
7bb48f84 | 105 | ret = td->o.file_size_low + (unsigned long long) ((double) td->o.file_size_high * (r / (RAND_MAX + 1.0))); |
2dc1bbeb | 106 | ret -= (ret % td->o.rw_min_bs); |
9c60ce64 JA |
107 | return ret; |
108 | } | |
109 | ||
53cdc686 JA |
110 | static int file_size(struct thread_data *td, struct fio_file *f) |
111 | { | |
112 | struct stat st; | |
113 | ||
7bb48f84 JA |
114 | if (fstat(f->fd, &st) == -1) { |
115 | td_verror(td, errno, "fstat"); | |
116 | return 1; | |
117 | } | |
53cdc686 | 118 | |
7bb48f84 | 119 | f->real_file_size = st.st_size; |
53cdc686 JA |
120 | return 0; |
121 | } | |
122 | ||
123 | static int bdev_size(struct thread_data *td, struct fio_file *f) | |
124 | { | |
125 | unsigned long long bytes; | |
126 | int r; | |
127 | ||
128 | r = blockdev_size(f->fd, &bytes); | |
129 | if (r) { | |
e1161c32 | 130 | td_verror(td, r, "blockdev_size"); |
53cdc686 JA |
131 | return 1; |
132 | } | |
133 | ||
134 | f->real_file_size = bytes; | |
53cdc686 JA |
135 | return 0; |
136 | } | |
137 | ||
138 | static int get_file_size(struct thread_data *td, struct fio_file *f) | |
139 | { | |
140 | int ret = 0; | |
141 | ||
409b3417 JA |
142 | if (f->flags & FIO_SIZE_KNOWN) |
143 | return 0; | |
144 | ||
7bb48f84 JA |
145 | if (f->filetype == FIO_TYPE_FILE) |
146 | ret = file_size(td, f); | |
147 | else if (f->filetype == FIO_TYPE_BD) | |
53cdc686 JA |
148 | ret = bdev_size(td, f); |
149 | else | |
150 | f->real_file_size = -1; | |
151 | ||
152 | if (ret) | |
153 | return ret; | |
154 | ||
155 | if (f->file_offset > f->real_file_size) { | |
2dc1bbeb | 156 | log_err("%s: offset extends end (%Lu > %Lu)\n", td->o.name, f->file_offset, f->real_file_size); |
53cdc686 JA |
157 | return 1; |
158 | } | |
159 | ||
409b3417 | 160 | f->flags |= FIO_SIZE_KNOWN; |
53cdc686 JA |
161 | return 0; |
162 | } | |
163 | ||
e5b401d4 JA |
164 | int file_invalidate_cache(struct thread_data *td, struct fio_file *f) |
165 | { | |
166 | int ret = 0; | |
167 | ||
2dc1bbeb | 168 | if (td->o.odirect) |
b5af8293 JA |
169 | return 0; |
170 | ||
e5b401d4 JA |
171 | /* |
172 | * FIXME: add blockdev flushing too | |
173 | */ | |
b5af8293 | 174 | if (f->mmap) |
7bb48f84 | 175 | ret = madvise(f->mmap, f->io_size, MADV_DONTNEED); |
467d1b6b | 176 | else if (f->filetype == FIO_TYPE_FILE) |
7bb48f84 | 177 | ret = fadvise(f->fd, f->file_offset, f->io_size, POSIX_FADV_DONTNEED); |
7e0e25c9 | 178 | else if (f->filetype == FIO_TYPE_BD) { |
b5af8293 | 179 | ret = blockdev_invalidate_cache(f->fd); |
7e0e25c9 JA |
180 | if (ret < 0 && errno == EACCES && geteuid()) { |
181 | log_err("fio: only root may flush block devices. Cache flush bypassed!\n"); | |
182 | ret = 0; | |
183 | } | |
b5605e9d | 184 | } else if (f->filetype == FIO_TYPE_CHAR || f->filetype == FIO_TYPE_PIPE) |
e5b401d4 JA |
185 | ret = 0; |
186 | ||
187 | if (ret < 0) { | |
e1161c32 | 188 | td_verror(td, errno, "invalidate_cache"); |
e5b401d4 JA |
189 | return 1; |
190 | } | |
191 | ||
ad2da605 | 192 | return ret; |
e5b401d4 JA |
193 | } |
194 | ||
b5af8293 | 195 | void generic_close_file(struct thread_data fio_unused *td, struct fio_file *f) |
53cdc686 | 196 | { |
b5af8293 JA |
197 | close(f->fd); |
198 | f->fd = -1; | |
53cdc686 JA |
199 | } |
200 | ||
b5af8293 | 201 | int generic_open_file(struct thread_data *td, struct fio_file *f) |
53cdc686 | 202 | { |
66159828 | 203 | int is_std = 0; |
53cdc686 JA |
204 | int flags = 0; |
205 | ||
66159828 JA |
206 | if (!strcmp(f->file_name, "-")) { |
207 | if (td_rw(td)) { | |
208 | log_err("fio: can't read/write to stdin/out\n"); | |
209 | return 1; | |
210 | } | |
211 | is_std = 1; | |
ce98fa66 JA |
212 | |
213 | /* | |
214 | * move output logging to stderr, if we are writing to stdout | |
215 | */ | |
216 | if (td_write(td)) | |
217 | f_out = stderr; | |
66159828 JA |
218 | } |
219 | ||
2dc1bbeb | 220 | if (td->o.odirect) |
2fd233b7 | 221 | flags |= OS_O_DIRECT; |
2dc1bbeb | 222 | if (td->o.sync_io) |
2fd233b7 | 223 | flags |= O_SYNC; |
53cdc686 | 224 | |
660a1cb5 | 225 | if (td_write(td)) { |
2fd233b7 | 226 | flags |= O_RDWR; |
53cdc686 | 227 | |
af52b345 | 228 | if (f->filetype == FIO_TYPE_FILE) |
2fd233b7 | 229 | flags |= O_CREAT; |
2fd233b7 | 230 | |
66159828 JA |
231 | if (is_std) |
232 | f->fd = dup(STDOUT_FILENO); | |
233 | else | |
234 | f->fd = open(f->file_name, flags, 0600); | |
2fd233b7 | 235 | } else { |
af52b345 | 236 | if (f->filetype == FIO_TYPE_CHAR) |
2fd233b7 JA |
237 | flags |= O_RDWR; |
238 | else | |
239 | flags |= O_RDONLY; | |
240 | ||
66159828 JA |
241 | if (is_std) |
242 | f->fd = dup(STDIN_FILENO); | |
243 | else | |
244 | f->fd = open(f->file_name, flags); | |
53cdc686 JA |
245 | } |
246 | ||
247 | if (f->fd == -1) { | |
e4e33258 | 248 | char buf[FIO_VERROR_SIZE]; |
e1161c32 JA |
249 | int __e = errno; |
250 | ||
e4e33258 JA |
251 | snprintf(buf, sizeof(buf) - 1, "open(%s)", f->file_name); |
252 | ||
253 | td_verror(td, __e, buf); | |
53cdc686 JA |
254 | } |
255 | ||
b5af8293 JA |
256 | if (get_file_size(td, f)) |
257 | goto err; | |
258 | ||
53cdc686 | 259 | return 0; |
b5af8293 JA |
260 | err: |
261 | close(f->fd); | |
262 | return 1; | |
263 | } | |
264 | ||
21972cde JA |
265 | int open_files(struct thread_data *td) |
266 | { | |
267 | struct fio_file *f; | |
af52b345 JA |
268 | unsigned int i; |
269 | int err = 0; | |
21972cde JA |
270 | |
271 | for_each_file(td, f, i) { | |
b5af8293 | 272 | err = td_io_open_file(td, f); |
9bf27b45 JA |
273 | if (err) { |
274 | if (td->error == EMFILE) { | |
275 | log_err("fio: limited open files to: %d\n", td->nr_open_files); | |
276 | td->o.open_files = td->nr_open_files; | |
277 | err = 0; | |
278 | clear_error(td); | |
279 | } | |
21972cde | 280 | break; |
9bf27b45 | 281 | } |
b5af8293 | 282 | |
2dc1bbeb | 283 | if (td->o.open_files == td->nr_open_files) |
b5af8293 | 284 | break; |
21972cde JA |
285 | } |
286 | ||
7abf833d JA |
287 | if (!err) |
288 | return 0; | |
289 | ||
bdb4e2e9 | 290 | for_each_file(td, f, i) |
b5af8293 | 291 | td_io_close_file(td, f); |
7abf833d | 292 | |
21972cde JA |
293 | return err; |
294 | } | |
295 | ||
7bb48f84 JA |
296 | /* |
297 | * open/close all files, so that ->real_file_size gets set | |
298 | */ | |
bab3fd58 | 299 | static int get_file_sizes(struct thread_data *td) |
7bb48f84 JA |
300 | { |
301 | struct fio_file *f; | |
302 | unsigned int i; | |
bab3fd58 | 303 | int err = 0; |
7bb48f84 JA |
304 | |
305 | for_each_file(td, f, i) { | |
bab3fd58 | 306 | if (td->io_ops->open_file(td, f)) { |
40b44f4a JA |
307 | if (td->error != ENOENT) { |
308 | log_err("%s\n", td->verror); | |
309 | err = 1; | |
310 | } | |
541d66d7 | 311 | clear_error(td); |
07eb79df JA |
312 | } else { |
313 | if (td->io_ops->close_file) | |
314 | td->io_ops->close_file(td, f); | |
315 | } | |
409b3417 JA |
316 | |
317 | if (f->real_file_size == -1ULL && td->o.size) | |
318 | f->real_file_size = td->o.size / td->o.nr_files; | |
7bb48f84 | 319 | } |
bab3fd58 JA |
320 | |
321 | return err; | |
7bb48f84 JA |
322 | } |
323 | ||
324 | /* | |
325 | * Open the files and setup files sizes, creating files if necessary. | |
326 | */ | |
53cdc686 JA |
327 | int setup_files(struct thread_data *td) |
328 | { | |
7bb48f84 | 329 | unsigned long long total_size, extend_size; |
53cdc686 | 330 | struct fio_file *f; |
af52b345 | 331 | unsigned int i; |
000b0803 | 332 | int err = 0, need_extend; |
53cdc686 JA |
333 | |
334 | /* | |
335 | * if ioengine defines a setup() method, it's responsible for | |
7bb48f84 JA |
336 | * opening the files and setting f->real_file_size to indicate |
337 | * the valid range for that file. | |
53cdc686 JA |
338 | */ |
339 | if (td->io_ops->setup) | |
7bb48f84 JA |
340 | err = td->io_ops->setup(td); |
341 | else | |
bab3fd58 | 342 | err = get_file_sizes(td); |
53cdc686 | 343 | |
f1027063 JA |
344 | if (err) |
345 | return err; | |
346 | ||
0a7eb121 | 347 | /* |
7bb48f84 JA |
348 | * check sizes. if the files/devices do not exist and the size |
349 | * isn't passed to fio, abort. | |
0a7eb121 | 350 | */ |
7bb48f84 JA |
351 | total_size = 0; |
352 | for_each_file(td, f, i) { | |
353 | if (f->real_file_size == -1ULL) | |
354 | total_size = -1ULL; | |
355 | else | |
356 | total_size += f->real_file_size; | |
357 | } | |
0a7eb121 | 358 | |
7bb48f84 JA |
359 | /* |
360 | * device/file sizes are zero and no size given, punt | |
361 | */ | |
409b3417 | 362 | if ((!total_size || total_size == -1ULL) && !td->o.size) { |
7bb48f84 | 363 | log_err("%s: you need to specify size=\n", td->o.name); |
e1161c32 | 364 | td_verror(td, EINVAL, "total_file_size"); |
53cdc686 JA |
365 | return 1; |
366 | } | |
367 | ||
7bb48f84 JA |
368 | /* |
369 | * now file sizes are known, so we can set ->io_size. if size= is | |
370 | * not given, ->io_size is just equal to ->real_file_size. if size | |
371 | * is given, ->io_size is size / nr_files. | |
372 | */ | |
373 | extend_size = total_size = 0; | |
374 | need_extend = 0; | |
375 | for_each_file(td, f, i) { | |
376 | if (!td->o.file_size_low) { | |
377 | /* | |
378 | * no file size range given, file size is equal to | |
379 | * total size divided by number of files. if that is | |
380 | * zero, set it to the real file size. | |
381 | */ | |
382 | f->io_size = td->o.size / td->o.nr_files; | |
383 | if (!f->io_size) | |
384 | f->io_size = f->real_file_size; | |
385 | } else if (f->real_file_size < td->o.file_size_low || | |
386 | f->real_file_size > td->o.file_size_high) { | |
387 | /* | |
388 | * file size given. if it's fixed, use that. if it's a | |
389 | * range, generate a random size in-between. | |
390 | */ | |
391 | if (td->o.file_size_low == td->o.file_size_high) | |
392 | f->io_size = td->o.file_size_low; | |
393 | else | |
394 | f->io_size = get_rand_file_size(td); | |
395 | } else | |
396 | f->io_size = f->real_file_size; | |
53cdc686 | 397 | |
7bb48f84 JA |
398 | if (f->io_size == -1ULL) |
399 | total_size = -1ULL; | |
400 | else | |
401 | total_size += f->io_size; | |
402 | ||
403 | if (f->filetype == FIO_TYPE_FILE && | |
404 | f->io_size > f->real_file_size && | |
405 | !(td->io_ops->flags & FIO_DISKLESSIO)) { | |
406 | need_extend++; | |
407 | extend_size += f->io_size; | |
408 | f->flags |= FIO_FILE_EXTEND; | |
409 | } | |
410 | } | |
53cdc686 | 411 | |
7bb48f84 JA |
412 | if (!td->o.size) |
413 | td->o.size = total_size; | |
21972cde | 414 | |
7bb48f84 JA |
415 | /* |
416 | * See if we need to extend some files | |
417 | */ | |
418 | if (need_extend) { | |
419 | temp_stall_ts = 1; | |
420 | log_info("%s: Laying out IO file(s) (%u files / %LuMiB)\n", | |
421 | td->o.name, need_extend, extend_size >> 20); | |
422 | ||
423 | for_each_file(td, f, i) { | |
424 | if (!(f->flags & FIO_FILE_EXTEND)) | |
425 | continue; | |
426 | ||
409b3417 | 427 | assert(f->filetype == FIO_TYPE_FILE); |
7bb48f84 JA |
428 | f->flags &= ~FIO_FILE_EXTEND; |
429 | f->real_file_size = f->io_size; | |
430 | err = extend_file(td, f); | |
431 | if (err) | |
432 | break; | |
433 | } | |
434 | temp_stall_ts = 0; | |
435 | } | |
436 | ||
437 | if (err) | |
438 | return err; | |
439 | ||
440 | if (!td->o.zone_size) | |
441 | td->o.zone_size = td->o.size; | |
442 | ||
443 | td->total_io_size = td->o.size * td->o.loops; | |
444 | return 0; | |
53cdc686 JA |
445 | } |
446 | ||
68727076 JA |
447 | int init_random_map(struct thread_data *td) |
448 | { | |
449 | int num_maps, blocks; | |
450 | struct fio_file *f; | |
451 | unsigned int i; | |
452 | ||
453 | if (td->o.norandommap) | |
454 | return 0; | |
455 | ||
456 | for_each_file(td, f, i) { | |
457 | blocks = (f->real_file_size + td->o.rw_min_bs - 1) / td->o.rw_min_bs; | |
458 | num_maps = (blocks + BLOCKS_PER_MAP-1)/ BLOCKS_PER_MAP; | |
459 | f->file_map = malloc(num_maps * sizeof(long)); | |
460 | if (!f->file_map) { | |
461 | log_err("fio: failed allocating random map. If running a large number of jobs, try the 'norandommap' option\n"); | |
462 | return 1; | |
463 | } | |
464 | f->num_maps = num_maps; | |
465 | memset(f->file_map, 0, num_maps * sizeof(long)); | |
466 | } | |
467 | ||
468 | return 0; | |
469 | } | |
470 | ||
53cdc686 JA |
471 | void close_files(struct thread_data *td) |
472 | { | |
0ab8db89 | 473 | struct fio_file *f; |
af52b345 | 474 | unsigned int i; |
53cdc686 | 475 | |
0ab8db89 | 476 | for_each_file(td, f, i) { |
a9b7b305 | 477 | if (td->o.unlink && f->filetype == FIO_TYPE_FILE) |
132ad46d | 478 | unlink(f->file_name); |
bdb4e2e9 | 479 | |
b5af8293 | 480 | td_io_close_file(td, f); |
b3dc7f07 | 481 | |
fa1da865 JA |
482 | free(f->file_name); |
483 | f->file_name = NULL; | |
484 | ||
c343981b | 485 | if (f->file_map) { |
b3dc7f07 | 486 | free(f->file_map); |
c343981b JA |
487 | f->file_map = NULL; |
488 | } | |
53cdc686 | 489 | } |
b4a6a59a | 490 | |
2dc1bbeb | 491 | td->o.filename = NULL; |
cade3ef4 | 492 | free(td->files); |
b4a6a59a | 493 | td->files = NULL; |
2dc1bbeb | 494 | td->o.nr_files = 0; |
53cdc686 | 495 | } |
af52b345 | 496 | |
e3bab463 | 497 | static void get_file_type(struct fio_file *f) |
af52b345 JA |
498 | { |
499 | struct stat sb; | |
500 | ||
66159828 JA |
501 | if (!strcmp(f->file_name, "-")) |
502 | f->filetype = FIO_TYPE_PIPE; | |
503 | else | |
504 | f->filetype = FIO_TYPE_FILE; | |
af52b345 | 505 | |
e3bab463 | 506 | if (!lstat(f->file_name, &sb)) { |
af52b345 JA |
507 | if (S_ISBLK(sb.st_mode)) |
508 | f->filetype = FIO_TYPE_BD; | |
509 | else if (S_ISCHR(sb.st_mode)) | |
510 | f->filetype = FIO_TYPE_CHAR; | |
b5605e9d JA |
511 | else if (S_ISFIFO(sb.st_mode)) |
512 | f->filetype = FIO_TYPE_PIPE; | |
af52b345 JA |
513 | } |
514 | } | |
515 | ||
f29b25a3 | 516 | int add_file(struct thread_data *td, const char *fname) |
af52b345 | 517 | { |
7b4e4fe5 | 518 | int cur_files = td->files_index; |
bd0ee748 | 519 | char file_name[PATH_MAX]; |
af52b345 | 520 | struct fio_file *f; |
bd0ee748 | 521 | int len = 0; |
af52b345 JA |
522 | |
523 | td->files = realloc(td->files, (cur_files + 1) * sizeof(*f)); | |
524 | ||
525 | f = &td->files[cur_files]; | |
526 | memset(f, 0, sizeof(*f)); | |
527 | f->fd = -1; | |
bd0ee748 | 528 | |
07eb79df JA |
529 | /* |
530 | * init function, io engine may not be loaded yet | |
531 | */ | |
532 | if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO)) | |
533 | f->real_file_size = -1ULL; | |
534 | ||
bd0ee748 JA |
535 | if (td->o.directory) |
536 | len = sprintf(file_name, "%s/", td->o.directory); | |
537 | ||
538 | sprintf(file_name + len, "%s", fname); | |
539 | f->file_name = strdup(file_name); | |
af52b345 | 540 | |
e3bab463 | 541 | get_file_type(f); |
af52b345 | 542 | |
7b4e4fe5 | 543 | td->files_index++; |
1549441c JA |
544 | if (f->filetype == FIO_TYPE_FILE) |
545 | td->nr_normal_files++; | |
f29b25a3 JA |
546 | |
547 | return cur_files; | |
af52b345 | 548 | } |
0ad920e7 JA |
549 | |
550 | void get_file(struct fio_file *f) | |
551 | { | |
97af62ce | 552 | assert(f->flags & FIO_FILE_OPEN); |
0ad920e7 JA |
553 | f->references++; |
554 | } | |
555 | ||
556 | void put_file(struct thread_data *td, struct fio_file *f) | |
557 | { | |
558 | if (!(f->flags & FIO_FILE_OPEN)) | |
559 | return; | |
560 | ||
561 | assert(f->references); | |
562 | if (--f->references) | |
563 | return; | |
564 | ||
d424d4dd | 565 | if (should_fsync(td) && td->o.fsync_on_close) |
ebb1415f JA |
566 | fsync(f->fd); |
567 | ||
0ad920e7 JA |
568 | if (td->io_ops->close_file) |
569 | td->io_ops->close_file(td, f); | |
1020a139 | 570 | |
0ad920e7 JA |
571 | td->nr_open_files--; |
572 | f->flags &= ~FIO_FILE_OPEN; | |
573 | } | |
bbf6b540 JA |
574 | |
575 | static int recurse_dir(struct thread_data *td, const char *dirname) | |
576 | { | |
577 | struct dirent *dir; | |
578 | int ret = 0; | |
579 | DIR *D; | |
580 | ||
581 | D = opendir(dirname); | |
582 | if (!D) { | |
0ddb270c JA |
583 | char buf[FIO_VERROR_SIZE]; |
584 | ||
585 | snprintf(buf, FIO_VERROR_SIZE - 1, "opendir(%s)", dirname); | |
586 | td_verror(td, errno, buf); | |
bbf6b540 JA |
587 | return 1; |
588 | } | |
589 | ||
590 | while ((dir = readdir(D)) != NULL) { | |
591 | char full_path[PATH_MAX]; | |
592 | struct stat sb; | |
593 | ||
e85b2b83 JA |
594 | if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, "..")) |
595 | continue; | |
96d32d51 | 596 | |
bbf6b540 JA |
597 | sprintf(full_path, "%s/%s", dirname, dir->d_name); |
598 | ||
599 | if (lstat(full_path, &sb) == -1) { | |
600 | if (errno != ENOENT) { | |
601 | td_verror(td, errno, "stat"); | |
602 | return 1; | |
603 | } | |
604 | } | |
605 | ||
606 | if (S_ISREG(sb.st_mode)) { | |
607 | add_file(td, full_path); | |
2dc1bbeb | 608 | td->o.nr_files++; |
bbf6b540 JA |
609 | continue; |
610 | } | |
0ddb270c JA |
611 | if (!S_ISDIR(sb.st_mode)) |
612 | continue; | |
bbf6b540 | 613 | |
bbf6b540 JA |
614 | if ((ret = recurse_dir(td, full_path)) != 0) |
615 | break; | |
616 | } | |
617 | ||
618 | closedir(D); | |
619 | return ret; | |
620 | } | |
621 | ||
622 | int add_dir_files(struct thread_data *td, const char *path) | |
623 | { | |
0ddb270c JA |
624 | int ret = recurse_dir(td, path); |
625 | ||
626 | if (!ret) | |
627 | log_info("fio: opendir added %d files\n", td->o.nr_files); | |
628 | ||
629 | return ret; | |
bbf6b540 | 630 | } |
cade3ef4 JA |
631 | |
632 | void dup_files(struct thread_data *td, struct thread_data *org) | |
633 | { | |
634 | struct fio_file *f; | |
635 | unsigned int i; | |
636 | size_t bytes; | |
637 | ||
638 | if (!org->files) | |
639 | return; | |
640 | ||
641 | bytes = org->files_index * sizeof(*f); | |
642 | td->files = malloc(bytes); | |
643 | memcpy(td->files, org->files, bytes); | |
644 | ||
645 | for_each_file(td, f, i) { | |
646 | if (f->file_name) | |
647 | f->file_name = strdup(f->file_name); | |
648 | } | |
649 | } | |
f29b25a3 JA |
650 | |
651 | /* | |
652 | * Returns the index that matches the filename, or -1 if not there | |
653 | */ | |
654 | int get_fileno(struct thread_data *td, const char *fname) | |
655 | { | |
656 | struct fio_file *f; | |
657 | unsigned int i; | |
658 | ||
659 | for_each_file(td, f, i) | |
660 | if (!strcmp(f->file_name, fname)) | |
661 | return i; | |
662 | ||
663 | return -1; | |
664 | } | |
665 | ||
666 | /* | |
667 | * For log usage, where we add/open/close files automatically | |
668 | */ | |
669 | void free_release_files(struct thread_data *td) | |
670 | { | |
671 | close_files(td); | |
672 | td->files_index = 0; | |
673 | td->nr_normal_files = 0; | |
674 | } |