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