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