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 | ||
a00735e6 JA |
61 | b = malloc(td->max_bs[DDIR_WRITE]); |
62 | memset(b, 0, td->max_bs[DDIR_WRITE]); | |
53cdc686 JA |
63 | |
64 | left = f->file_size; | |
65 | while (left && !td->terminate) { | |
a00735e6 | 66 | bs = td->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); | |
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; | |
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 | ||
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 | ||
53cdc686 JA |
122 | static int create_files(struct thread_data *td) |
123 | { | |
124 | struct fio_file *f; | |
af52b345 | 125 | int err, need_create, can_extend; |
9c60ce64 | 126 | unsigned long long total_file_size, local_file_size; |
e407bec2 | 127 | unsigned int i, new_files; |
53cdc686 | 128 | |
e407bec2 | 129 | new_files = 0; |
96d32d51 | 130 | total_file_size = td->total_file_size; |
1549441c | 131 | for_each_file(td, f, i) { |
96d32d51 JA |
132 | unsigned long long s; |
133 | ||
134 | f->file_offset = td->start_offset; | |
135 | ||
1549441c JA |
136 | if (f->filetype != FIO_TYPE_FILE) |
137 | continue; | |
138 | ||
96d32d51 JA |
139 | 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; | |
e407bec2 JA |
145 | } else |
146 | new_files++; | |
1549441c | 147 | } |
f697125a | 148 | |
53cdc686 JA |
149 | /* |
150 | * unless specifically asked for overwrite, let normal io extend it | |
151 | */ | |
0263882a JA |
152 | can_extend = !td->overwrite && !(td->io_ops->flags & FIO_NOEXTEND); |
153 | if (can_extend) | |
53cdc686 | 154 | return 0; |
53cdc686 | 155 | |
9c60ce64 JA |
156 | local_file_size = total_file_size; |
157 | if (!local_file_size) | |
158 | local_file_size = -1; | |
159 | ||
f4c4f4d7 JA |
160 | total_file_size = 0; |
161 | need_create = 0; | |
af52b345 JA |
162 | for_each_file(td, f, i) { |
163 | int file_there; | |
164 | ||
165 | if (f->filetype != FIO_TYPE_FILE) | |
166 | continue; | |
f4c4f4d7 JA |
167 | if (f->flags & FIO_FILE_EXISTS) { |
168 | total_file_size += f->file_size; | |
96d32d51 | 169 | continue; |
f4c4f4d7 | 170 | } |
96d32d51 | 171 | |
9c60ce64 JA |
172 | if (!td->file_size_low) |
173 | f->file_size = total_file_size / new_files; | |
174 | else { | |
175 | /* | |
176 | * If we don't have enough space left for a file | |
177 | * of the minimum size, bail. | |
178 | */ | |
179 | if (local_file_size < td->file_size_low) { | |
180 | log_info("fio: limited to %d files\n", i); | |
181 | new_files -= (td->nr_files - i); | |
182 | td->nr_files = i; | |
183 | break; | |
184 | } | |
185 | ||
186 | f->file_size = set_rand_file_size(td, local_file_size); | |
187 | local_file_size -= f->file_size; | |
188 | } | |
eff6861d | 189 | |
f4c4f4d7 | 190 | total_file_size += f->file_size; |
af52b345 | 191 | file_there = !file_ok(td, f); |
eff6861d | 192 | |
af52b345 JA |
193 | if (file_there && td_write(td) && !td->overwrite) { |
194 | unlink(f->file_name); | |
195 | file_there = 0; | |
eff6861d | 196 | } |
af52b345 JA |
197 | |
198 | need_create += !file_there; | |
eff6861d | 199 | } |
25205e97 | 200 | |
25205e97 JA |
201 | if (!need_create) |
202 | return 0; | |
203 | ||
f1027063 JA |
204 | if (!td->total_file_size) { |
205 | log_err("Need size for create\n"); | |
e1161c32 | 206 | td_verror(td, EINVAL, "file_size"); |
f1027063 JA |
207 | return 1; |
208 | } | |
209 | ||
53cdc686 | 210 | temp_stall_ts = 1; |
6d86144d JA |
211 | log_info("%s: Laying out IO file(s) (%u files / %LuMiB)\n", |
212 | td->name, new_files, total_file_size >> 20); | |
53cdc686 JA |
213 | |
214 | err = 0; | |
215 | for_each_file(td, f, i) { | |
9b031dc8 JA |
216 | /* |
217 | * Only unlink files that we created. | |
218 | */ | |
f11bd94d | 219 | f->flags &= ~FIO_FILE_UNLINK; |
25205e97 | 220 | if (file_ok(td, f)) { |
f11bd94d JA |
221 | if (td->unlink) |
222 | f->flags |= FIO_FILE_UNLINK; | |
223 | ||
25205e97 JA |
224 | err = create_file(td, f); |
225 | if (err) | |
226 | break; | |
227 | } | |
53cdc686 JA |
228 | } |
229 | ||
230 | temp_stall_ts = 0; | |
231 | return err; | |
232 | } | |
233 | ||
234 | static int file_size(struct thread_data *td, struct fio_file *f) | |
235 | { | |
236 | struct stat st; | |
237 | ||
238 | if (td->overwrite) { | |
239 | if (fstat(f->fd, &st) == -1) { | |
e1161c32 | 240 | td_verror(td, errno, "fstat"); |
53cdc686 JA |
241 | return 1; |
242 | } | |
243 | ||
244 | f->real_file_size = st.st_size; | |
245 | ||
246 | if (!f->file_size || f->file_size > f->real_file_size) | |
247 | f->file_size = f->real_file_size; | |
745508dd JA |
248 | } else |
249 | f->real_file_size = f->file_size; | |
53cdc686 | 250 | |
53cdc686 JA |
251 | return 0; |
252 | } | |
253 | ||
254 | static int bdev_size(struct thread_data *td, struct fio_file *f) | |
255 | { | |
256 | unsigned long long bytes; | |
257 | int r; | |
258 | ||
259 | r = blockdev_size(f->fd, &bytes); | |
260 | if (r) { | |
e1161c32 | 261 | td_verror(td, r, "blockdev_size"); |
53cdc686 JA |
262 | return 1; |
263 | } | |
264 | ||
265 | f->real_file_size = bytes; | |
266 | ||
267 | /* | |
268 | * no extend possibilities, so limit size to device size if too large | |
269 | */ | |
270 | if (!f->file_size || f->file_size > f->real_file_size) | |
271 | f->file_size = f->real_file_size; | |
272 | ||
273 | f->file_size -= f->file_offset; | |
274 | return 0; | |
275 | } | |
276 | ||
277 | static int get_file_size(struct thread_data *td, struct fio_file *f) | |
278 | { | |
279 | int ret = 0; | |
280 | ||
96d32d51 JA |
281 | if (f->filetype == FIO_TYPE_FILE) { |
282 | if (!(f->flags & FIO_FILE_EXISTS)) | |
283 | ret = file_size(td, f); | |
284 | } else if (f->filetype == FIO_TYPE_BD) | |
53cdc686 JA |
285 | ret = bdev_size(td, f); |
286 | else | |
287 | f->real_file_size = -1; | |
288 | ||
289 | if (ret) | |
290 | return ret; | |
291 | ||
292 | if (f->file_offset > f->real_file_size) { | |
293 | log_err("%s: offset extends end (%Lu > %Lu)\n", td->name, f->file_offset, f->real_file_size); | |
294 | return 1; | |
295 | } | |
296 | ||
53cdc686 JA |
297 | return 0; |
298 | } | |
299 | ||
e5b401d4 JA |
300 | int file_invalidate_cache(struct thread_data *td, struct fio_file *f) |
301 | { | |
302 | int ret = 0; | |
303 | ||
da86ccb6 | 304 | if (td->odirect) |
b5af8293 JA |
305 | return 0; |
306 | ||
e5b401d4 JA |
307 | /* |
308 | * FIXME: add blockdev flushing too | |
309 | */ | |
b5af8293 | 310 | if (f->mmap) |
e5b401d4 | 311 | ret = madvise(f->mmap, f->file_size, MADV_DONTNEED); |
af52b345 | 312 | else if (f->filetype == FIO_TYPE_FILE) { |
b5af8293 | 313 | ret = fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_DONTNEED); |
af52b345 | 314 | } else if (f->filetype == FIO_TYPE_BD) { |
b5af8293 | 315 | ret = blockdev_invalidate_cache(f->fd); |
af52b345 | 316 | } else if (f->filetype == FIO_TYPE_CHAR) |
e5b401d4 JA |
317 | ret = 0; |
318 | ||
319 | if (ret < 0) { | |
e1161c32 | 320 | td_verror(td, errno, "invalidate_cache"); |
e5b401d4 JA |
321 | return 1; |
322 | } | |
323 | ||
ad2da605 | 324 | return ret; |
e5b401d4 JA |
325 | } |
326 | ||
b5af8293 | 327 | void generic_close_file(struct thread_data fio_unused *td, struct fio_file *f) |
53cdc686 | 328 | { |
b5af8293 JA |
329 | close(f->fd); |
330 | f->fd = -1; | |
53cdc686 JA |
331 | } |
332 | ||
b5af8293 | 333 | int generic_open_file(struct thread_data *td, struct fio_file *f) |
53cdc686 | 334 | { |
53cdc686 JA |
335 | int flags = 0; |
336 | ||
2fd233b7 JA |
337 | if (td->odirect) |
338 | flags |= OS_O_DIRECT; | |
339 | if (td->sync_io) | |
340 | flags |= O_SYNC; | |
53cdc686 | 341 | |
2fd233b7 JA |
342 | if (td_write(td) || td_rw(td)) { |
343 | flags |= O_RDWR; | |
53cdc686 | 344 | |
af52b345 | 345 | if (f->filetype == FIO_TYPE_FILE) |
2fd233b7 | 346 | flags |= O_CREAT; |
2fd233b7 | 347 | |
b5af8293 | 348 | f->fd = open(f->file_name, flags, 0600); |
2fd233b7 | 349 | } else { |
af52b345 | 350 | if (f->filetype == FIO_TYPE_CHAR) |
2fd233b7 JA |
351 | flags |= O_RDWR; |
352 | else | |
353 | flags |= O_RDONLY; | |
354 | ||
b5af8293 | 355 | f->fd = open(f->file_name, flags); |
53cdc686 JA |
356 | } |
357 | ||
358 | if (f->fd == -1) { | |
e1161c32 JA |
359 | int __e = errno; |
360 | ||
361 | td_verror(td, __e, "open"); | |
362 | if (__e == EINVAL && td->odirect) | |
9d80e114 | 363 | log_err("fio: destination does not support O_DIRECT\n"); |
8ab53873 JA |
364 | if (__e == EMFILE) |
365 | log_err("fio: try reducing/setting openfiles (failed at %u of %u)\n", td->nr_open_files, td->nr_files); | |
53cdc686 JA |
366 | return 1; |
367 | } | |
368 | ||
b5af8293 JA |
369 | if (get_file_size(td, f)) |
370 | goto err; | |
371 | ||
0263882a | 372 | if (td->invalidate_cache && file_invalidate_cache(td, f)) |
b5af8293 JA |
373 | goto err; |
374 | ||
375 | if (!td_random(td)) { | |
376 | if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_SEQUENTIAL) < 0) { | |
377 | td_verror(td, errno, "fadvise"); | |
378 | goto err; | |
379 | } | |
380 | } else { | |
381 | if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_RANDOM) < 0) { | |
382 | td_verror(td, errno, "fadvise"); | |
383 | goto err; | |
384 | } | |
bdb4e2e9 | 385 | } |
53cdc686 JA |
386 | |
387 | return 0; | |
b5af8293 JA |
388 | err: |
389 | close(f->fd); | |
390 | return 1; | |
391 | } | |
392 | ||
21972cde JA |
393 | int open_files(struct thread_data *td) |
394 | { | |
395 | struct fio_file *f; | |
af52b345 JA |
396 | unsigned int i; |
397 | int err = 0; | |
21972cde JA |
398 | |
399 | for_each_file(td, f, i) { | |
b5af8293 | 400 | err = td_io_open_file(td, f); |
21972cde JA |
401 | if (err) |
402 | break; | |
b5af8293 JA |
403 | |
404 | if (td->open_files == td->nr_open_files) | |
405 | break; | |
21972cde JA |
406 | } |
407 | ||
7abf833d JA |
408 | if (!err) |
409 | return 0; | |
410 | ||
bdb4e2e9 | 411 | for_each_file(td, f, i) |
b5af8293 | 412 | td_io_close_file(td, f); |
7abf833d | 413 | |
21972cde JA |
414 | return err; |
415 | } | |
416 | ||
53cdc686 JA |
417 | int setup_files(struct thread_data *td) |
418 | { | |
419 | struct fio_file *f; | |
af52b345 JA |
420 | unsigned int i; |
421 | int err; | |
53cdc686 JA |
422 | |
423 | /* | |
424 | * if ioengine defines a setup() method, it's responsible for | |
425 | * setting up everything in the td->files[] area. | |
426 | */ | |
427 | if (td->io_ops->setup) | |
428 | return td->io_ops->setup(td); | |
429 | ||
430 | if (create_files(td)) | |
431 | return 1; | |
432 | ||
21972cde | 433 | err = open_files(td); |
f1027063 JA |
434 | if (err) |
435 | return err; | |
436 | ||
0a7eb121 JA |
437 | /* |
438 | * Recalculate the total file size now that files are set up. | |
439 | */ | |
440 | td->total_file_size = 0; | |
441 | for_each_file(td, f, i) | |
442 | td->total_file_size += f->file_size; | |
443 | ||
f1027063 | 444 | td->io_size = td->total_file_size; |
53cdc686 JA |
445 | if (td->io_size == 0) { |
446 | log_err("%s: no io blocks\n", td->name); | |
e1161c32 | 447 | td_verror(td, EINVAL, "total_file_size"); |
53cdc686 JA |
448 | return 1; |
449 | } | |
450 | ||
451 | if (!td->zone_size) | |
452 | td->zone_size = td->io_size; | |
453 | ||
454 | td->total_io_size = td->io_size * td->loops; | |
455 | ||
bdb4e2e9 | 456 | for_each_file(td, f, i) |
b5af8293 | 457 | td_io_close_file(td, f); |
21972cde JA |
458 | |
459 | return err; | |
53cdc686 JA |
460 | } |
461 | ||
462 | void close_files(struct thread_data *td) | |
463 | { | |
0ab8db89 | 464 | struct fio_file *f; |
af52b345 | 465 | unsigned int i; |
53cdc686 | 466 | |
0ab8db89 | 467 | for_each_file(td, f, i) { |
1315a68a | 468 | if (!f->file_name && (f->flags & FIO_FILE_UNLINK) && |
af52b345 | 469 | f->filetype == FIO_TYPE_FILE) { |
132ad46d | 470 | unlink(f->file_name); |
1315a68a | 471 | free(f->file_name); |
132ad46d JA |
472 | f->file_name = NULL; |
473 | } | |
bdb4e2e9 | 474 | |
b5af8293 | 475 | td_io_close_file(td, f); |
b3dc7f07 JA |
476 | |
477 | if (f->file_map) | |
478 | free(f->file_map); | |
53cdc686 | 479 | } |
b4a6a59a | 480 | |
132ad46d | 481 | td->filename = NULL; |
b4a6a59a JA |
482 | td->files = NULL; |
483 | td->nr_files = 0; | |
53cdc686 | 484 | } |
af52b345 | 485 | |
e3bab463 | 486 | static void get_file_type(struct fio_file *f) |
af52b345 JA |
487 | { |
488 | struct stat sb; | |
489 | ||
490 | f->filetype = FIO_TYPE_FILE; | |
491 | ||
e3bab463 | 492 | if (!lstat(f->file_name, &sb)) { |
96d32d51 JA |
493 | f->flags |= FIO_FILE_EXISTS; |
494 | ||
af52b345 JA |
495 | if (S_ISBLK(sb.st_mode)) |
496 | f->filetype = FIO_TYPE_BD; | |
497 | else if (S_ISCHR(sb.st_mode)) | |
498 | f->filetype = FIO_TYPE_CHAR; | |
96d32d51 JA |
499 | else { |
500 | /* | |
501 | * might as well do this here, and save a stat later on | |
502 | */ | |
503 | f->real_file_size = sb.st_size; | |
504 | f->file_size = f->real_file_size; | |
505 | } | |
af52b345 JA |
506 | } |
507 | } | |
508 | ||
509 | void add_file(struct thread_data *td, const char *fname) | |
510 | { | |
7b4e4fe5 | 511 | int cur_files = td->files_index; |
af52b345 JA |
512 | struct fio_file *f; |
513 | ||
514 | td->files = realloc(td->files, (cur_files + 1) * sizeof(*f)); | |
515 | ||
516 | f = &td->files[cur_files]; | |
517 | memset(f, 0, sizeof(*f)); | |
518 | f->fd = -1; | |
cae61953 | 519 | f->file_name = strdup(fname); |
af52b345 | 520 | |
e3bab463 | 521 | get_file_type(f); |
af52b345 | 522 | |
7b4e4fe5 | 523 | td->files_index++; |
1549441c JA |
524 | if (f->filetype == FIO_TYPE_FILE) |
525 | td->nr_normal_files++; | |
af52b345 | 526 | } |
0ad920e7 JA |
527 | |
528 | void get_file(struct fio_file *f) | |
529 | { | |
530 | f->references++; | |
531 | } | |
532 | ||
533 | void put_file(struct thread_data *td, struct fio_file *f) | |
534 | { | |
535 | if (!(f->flags & FIO_FILE_OPEN)) | |
536 | return; | |
537 | ||
538 | assert(f->references); | |
539 | if (--f->references) | |
540 | return; | |
541 | ||
ebb1415f JA |
542 | if (should_fsync(td) && td->fsync_on_close) |
543 | fsync(f->fd); | |
544 | ||
0ad920e7 JA |
545 | if (td->io_ops->close_file) |
546 | td->io_ops->close_file(td, f); | |
547 | td->nr_open_files--; | |
548 | f->flags &= ~FIO_FILE_OPEN; | |
549 | } | |
bbf6b540 JA |
550 | |
551 | static int recurse_dir(struct thread_data *td, const char *dirname) | |
552 | { | |
553 | struct dirent *dir; | |
554 | int ret = 0; | |
555 | DIR *D; | |
556 | ||
557 | D = opendir(dirname); | |
558 | if (!D) { | |
559 | td_verror(td, errno, "opendir"); | |
560 | return 1; | |
561 | } | |
562 | ||
563 | while ((dir = readdir(D)) != NULL) { | |
564 | char full_path[PATH_MAX]; | |
565 | struct stat sb; | |
566 | ||
e85b2b83 JA |
567 | if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, "..")) |
568 | continue; | |
96d32d51 | 569 | |
bbf6b540 JA |
570 | sprintf(full_path, "%s/%s", dirname, dir->d_name); |
571 | ||
572 | if (lstat(full_path, &sb) == -1) { | |
573 | if (errno != ENOENT) { | |
574 | td_verror(td, errno, "stat"); | |
575 | return 1; | |
576 | } | |
577 | } | |
578 | ||
579 | if (S_ISREG(sb.st_mode)) { | |
580 | add_file(td, full_path); | |
581 | td->nr_files++; | |
582 | continue; | |
583 | } | |
584 | ||
bbf6b540 JA |
585 | if ((ret = recurse_dir(td, full_path)) != 0) |
586 | break; | |
587 | } | |
588 | ||
589 | closedir(D); | |
590 | return ret; | |
591 | } | |
592 | ||
593 | int add_dir_files(struct thread_data *td, const char *path) | |
594 | { | |
595 | return recurse_dir(td, path); | |
596 | } |