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> |
d74ac843 | 6 | #include <libgen.h> |
53cdc686 JA |
7 | #include <sys/stat.h> |
8 | #include <sys/mman.h> | |
bbf6b540 | 9 | #include <sys/types.h> |
53cdc686 JA |
10 | |
11 | #include "fio.h" | |
f17c4392 | 12 | #include "smalloc.h" |
4906e0b5 | 13 | #include "filehash.h" |
bcbfeefa | 14 | #include "options.h" |
ecc314ba | 15 | #include "os/os.h" |
2316296a | 16 | #include "hash.h" |
7ebd796f | 17 | #include "lib/axmap.h" |
53cdc686 | 18 | |
97ac992c | 19 | #ifdef CONFIG_LINUX_FALLOCATE |
a596f047 EG |
20 | #include <linux/falloc.h> |
21 | #endif | |
22 | ||
7172cfe8 JA |
23 | static int root_warn; |
24 | ||
bcbfeefa CE |
25 | static FLIST_HEAD(filename_list); |
26 | ||
bf651609 TK |
27 | /* |
28 | * List entry for filename_list | |
29 | */ | |
30 | struct file_name { | |
31 | struct flist_head list; | |
32 | char *filename; | |
33 | }; | |
34 | ||
c592b9fe JA |
35 | static inline void clear_error(struct thread_data *td) |
36 | { | |
37 | td->error = 0; | |
38 | td->verror[0] = '\0'; | |
39 | } | |
40 | ||
005eb303 JA |
41 | static void fallocate_file(struct thread_data *td, struct fio_file *f) |
42 | { | |
43 | int r; | |
44 | ||
45 | if (td->o.fill_device) | |
46 | return; | |
47 | ||
48 | #ifdef CONFIG_POSIX_FALLOCATE | |
49 | switch (td->o.fallocate_mode) { | |
50 | case FIO_FALLOCATE_NONE: | |
51 | break; | |
52 | case FIO_FALLOCATE_POSIX: | |
53 | dprint(FD_FILE, "posix_fallocate file %s size %llu\n", | |
54 | f->file_name, | |
55 | (unsigned long long) f->real_file_size); | |
56 | ||
57 | r = posix_fallocate(f->fd, 0, f->real_file_size); | |
58 | if (r > 0) | |
59 | log_err("fio: posix_fallocate fails: %s\n", strerror(r)); | |
60 | break; | |
61 | #ifdef CONFIG_LINUX_FALLOCATE | |
62 | case FIO_FALLOCATE_KEEP_SIZE: | |
63 | dprint(FD_FILE, "fallocate(FALLOC_FL_KEEP_SIZE) " | |
64 | "file %s size %llu\n", f->file_name, | |
65 | (unsigned long long) f->real_file_size); | |
66 | ||
67 | r = fallocate(f->fd, FALLOC_FL_KEEP_SIZE, 0, f->real_file_size); | |
68 | if (r != 0) | |
69 | td_verror(td, errno, "fallocate"); | |
70 | ||
71 | break; | |
72 | #endif /* CONFIG_LINUX_FALLOCATE */ | |
73 | default: | |
74 | log_err("fio: unknown fallocate mode: %d\n", td->o.fallocate_mode); | |
75 | assert(0); | |
76 | } | |
77 | #endif /* CONFIG_POSIX_FALLOCATE */ | |
78 | } | |
79 | ||
3baddf24 JA |
80 | /* |
81 | * Leaves f->fd open on success, caller must close | |
82 | */ | |
7bb48f84 | 83 | static int extend_file(struct thread_data *td, struct fio_file *f) |
25205e97 | 84 | { |
005eb303 | 85 | int new_layout = 0, unlink_file = 0, flags; |
25205e97 JA |
86 | unsigned long long left; |
87 | unsigned int bs; | |
f3e1eb23 | 88 | char *b = NULL; |
b2a15192 | 89 | |
4241ea8f JA |
90 | if (read_only) { |
91 | log_err("fio: refusing extend of file due to read-only\n"); | |
92 | return 0; | |
93 | } | |
94 | ||
507a702f JA |
95 | /* |
96 | * check if we need to lay the file out complete again. fio | |
97 | * does that for operations involving reads, or for writes | |
98 | * where overwrite is set | |
99 | */ | |
1417daeb JA |
100 | if (td_read(td) || |
101 | (td_write(td) && td->o.overwrite && !td->o.file_append) || | |
9b87f09b | 102 | (td_write(td) && td_ioengine_flagged(td, FIO_NOEXTEND))) |
507a702f | 103 | new_layout = 1; |
1417daeb | 104 | if (td_write(td) && !td->o.overwrite && !td->o.file_append) |
ea443657 | 105 | unlink_file = 1; |
507a702f | 106 | |
6ae1f57f | 107 | if (unlink_file || new_layout) { |
2442c935 JA |
108 | int ret; |
109 | ||
bd199f2b | 110 | dprint(FD_FILE, "layout unlink %s\n", f->file_name); |
2442c935 JA |
111 | |
112 | ret = td_io_unlink_file(td, f); | |
113 | if (ret != 0 && ret != ENOENT) { | |
7bb48f84 JA |
114 | td_verror(td, errno, "unlink"); |
115 | return 1; | |
116 | } | |
117 | } | |
118 | ||
2378826d JA |
119 | flags = O_WRONLY; |
120 | if (td->o.allow_create) | |
121 | flags |= O_CREAT; | |
507a702f JA |
122 | if (new_layout) |
123 | flags |= O_TRUNC; | |
124 | ||
9c0f3f32 BC |
125 | #ifdef WIN32 |
126 | flags |= _O_BINARY; | |
127 | #endif | |
128 | ||
ee56ad50 | 129 | dprint(FD_FILE, "open file %s, flags %x\n", f->file_name, flags); |
507a702f | 130 | f->fd = open(f->file_name, flags, 0644); |
53cdc686 | 131 | if (f->fd < 0) { |
2378826d JA |
132 | int err = errno; |
133 | ||
134 | if (err == ENOENT && !td->o.allow_create) | |
135 | log_err("fio: file creation disallowed by " | |
136 | "allow_file_create=0\n"); | |
137 | else | |
138 | td_verror(td, err, "open"); | |
53cdc686 JA |
139 | return 1; |
140 | } | |
141 | ||
005eb303 | 142 | fallocate_file(td, f); |
9b836561 | 143 | |
79591fa9 TK |
144 | /* |
145 | * If our jobs don't require regular files initially, we're done. | |
146 | */ | |
fc74ac1d SL |
147 | if (!new_layout) |
148 | goto done; | |
149 | ||
5e0074c2 JA |
150 | /* |
151 | * The size will be -1ULL when fill_device is used, so don't truncate | |
152 | * or fallocate this file, just write it | |
153 | */ | |
154 | if (!td->o.fill_device) { | |
155 | dprint(FD_FILE, "truncate file %s, size %llu\n", f->file_name, | |
4b91ee8f | 156 | (unsigned long long) f->real_file_size); |
5e0074c2 | 157 | if (ftruncate(f->fd, f->real_file_size) == -1) { |
3cd4c66f J |
158 | if (errno != EFBIG) { |
159 | td_verror(td, errno, "ftruncate"); | |
160 | goto err; | |
161 | } | |
5e0074c2 | 162 | } |
5e0074c2 | 163 | } |
40f8298c | 164 | |
82d8de31 TK |
165 | left = f->real_file_size; |
166 | bs = td->o.max_bs[DDIR_WRITE]; | |
167 | if (bs > left) | |
168 | bs = left; | |
169 | ||
170 | b = malloc(bs); | |
84af30a1 TK |
171 | if (!b) { |
172 | td_verror(td, errno, "malloc"); | |
173 | goto err; | |
174 | } | |
53cdc686 | 175 | |
53cdc686 | 176 | while (left && !td->terminate) { |
005eb303 JA |
177 | ssize_t r; |
178 | ||
53cdc686 JA |
179 | if (bs > left) |
180 | bs = left; | |
181 | ||
cc86c395 JA |
182 | fill_io_buffer(td, b, bs, bs); |
183 | ||
53cdc686 JA |
184 | r = write(f->fd, b, bs); |
185 | ||
5e0074c2 JA |
186 | if (r > 0) { |
187 | left -= r; | |
53cdc686 JA |
188 | continue; |
189 | } else { | |
5e0074c2 JA |
190 | if (r < 0) { |
191 | int __e = errno; | |
192 | ||
193 | if (__e == ENOSPC) { | |
194 | if (td->o.fill_device) | |
195 | break; | |
196 | log_info("fio: ENOSPC on laying out " | |
197 | "file, stopping\n"); | |
198 | break; | |
199 | } | |
e1161c32 | 200 | td_verror(td, errno, "write"); |
5e0074c2 | 201 | } else |
e1161c32 | 202 | td_verror(td, EIO, "write"); |
53cdc686 JA |
203 | |
204 | break; | |
205 | } | |
206 | } | |
207 | ||
ffdfabd4 JA |
208 | if (td->terminate) { |
209 | dprint(FD_FILE, "terminate unlink %s\n", f->file_name); | |
38ef9c90 | 210 | td_io_unlink_file(td, f); |
ffdfabd4 | 211 | } else if (td->o.create_fsync) { |
98e1ac4e JA |
212 | if (fsync(f->fd) < 0) { |
213 | td_verror(td, errno, "fsync"); | |
214 | goto err; | |
215 | } | |
216 | } | |
0d1cd207 | 217 | if (td->o.fill_device && !td_write(td)) { |
d6aed795 | 218 | fio_file_clear_size_known(f); |
5e0074c2 JA |
219 | if (td_io_get_file_size(td, f)) |
220 | goto err; | |
221 | if (f->io_size > f->real_file_size) | |
222 | f->io_size = f->real_file_size; | |
223 | } | |
53cdc686 JA |
224 | |
225 | free(b); | |
507a702f | 226 | done: |
53cdc686 JA |
227 | return 0; |
228 | err: | |
229 | close(f->fd); | |
230 | f->fd = -1; | |
f3e1eb23 JA |
231 | if (b) |
232 | free(b); | |
53cdc686 JA |
233 | return 1; |
234 | } | |
235 | ||
afad68f7 ZY |
236 | static int pre_read_file(struct thread_data *td, struct fio_file *f) |
237 | { | |
ef799a64 | 238 | int ret = 0, r, did_open = 0, old_runstate; |
afad68f7 ZY |
239 | unsigned long long left; |
240 | unsigned int bs; | |
241 | char *b; | |
242 | ||
736f1188 TK |
243 | if (td_ioengine_flagged(td, FIO_PIPEIO) || |
244 | td_ioengine_flagged(td, FIO_NOIO)) | |
9c0d2241 JA |
245 | return 0; |
246 | ||
e658078b TK |
247 | if (f->filetype == FIO_TYPE_CHAR) |
248 | return 0; | |
249 | ||
d6aed795 | 250 | if (!fio_file_open(f)) { |
b0f65863 JA |
251 | if (td->io_ops->open_file(td, f)) { |
252 | log_err("fio: cannot pre-read, failed to open file\n"); | |
253 | return 1; | |
254 | } | |
255 | did_open = 1; | |
256 | } | |
257 | ||
8edd973d | 258 | old_runstate = td_bump_runstate(td, TD_PRE_READING); |
b0f65863 | 259 | |
82d8de31 | 260 | left = f->io_size; |
afad68f7 | 261 | bs = td->o.max_bs[DDIR_READ]; |
82d8de31 TK |
262 | if (bs > left) |
263 | bs = left; | |
264 | ||
afad68f7 | 265 | b = malloc(bs); |
84af30a1 TK |
266 | if (!b) { |
267 | td_verror(td, errno, "malloc"); | |
268 | ret = 1; | |
269 | goto error; | |
270 | } | |
afad68f7 ZY |
271 | memset(b, 0, bs); |
272 | ||
ef799a64 JA |
273 | if (lseek(f->fd, f->file_offset, SEEK_SET) < 0) { |
274 | td_verror(td, errno, "lseek"); | |
275 | log_err("fio: failed to lseek pre-read file\n"); | |
276 | ret = 1; | |
277 | goto error; | |
278 | } | |
279 | ||
afad68f7 ZY |
280 | while (left && !td->terminate) { |
281 | if (bs > left) | |
282 | bs = left; | |
283 | ||
284 | r = read(f->fd, b, bs); | |
285 | ||
286 | if (r == (int) bs) { | |
287 | left -= bs; | |
288 | continue; | |
289 | } else { | |
290 | td_verror(td, EIO, "pre_read"); | |
291 | break; | |
292 | } | |
293 | } | |
294 | ||
ef799a64 | 295 | error: |
8edd973d | 296 | td_restore_runstate(td, old_runstate); |
b0f65863 JA |
297 | |
298 | if (did_open) | |
299 | td->io_ops->close_file(td, f); | |
ef799a64 | 300 | |
afad68f7 | 301 | free(b); |
ef799a64 | 302 | return ret; |
afad68f7 ZY |
303 | } |
304 | ||
a3f001f5 | 305 | unsigned long long get_rand_file_size(struct thread_data *td) |
9c60ce64 | 306 | { |
dc873b6f | 307 | unsigned long long ret, sized; |
c3546b53 | 308 | uint64_t frand_max; |
1294c3ec | 309 | unsigned long r; |
9c60ce64 | 310 | |
c3546b53 | 311 | frand_max = rand_max(&td->file_size_state); |
d6b72507 | 312 | r = __rand(&td->file_size_state); |
54a21917 | 313 | sized = td->o.file_size_high - td->o.file_size_low; |
c3546b53 | 314 | ret = (unsigned long long) ((double) sized * (r / (frand_max + 1.0))); |
5ec10eaa | 315 | ret += td->o.file_size_low; |
2dc1bbeb | 316 | ret -= (ret % td->o.rw_min_bs); |
9c60ce64 JA |
317 | return ret; |
318 | } | |
319 | ||
53cdc686 JA |
320 | static int file_size(struct thread_data *td, struct fio_file *f) |
321 | { | |
322 | struct stat st; | |
323 | ||
df9c26b1 | 324 | if (stat(f->file_name, &st) == -1) { |
7bb48f84 JA |
325 | td_verror(td, errno, "fstat"); |
326 | return 1; | |
327 | } | |
53cdc686 | 328 | |
7bb48f84 | 329 | f->real_file_size = st.st_size; |
53cdc686 JA |
330 | return 0; |
331 | } | |
332 | ||
333 | static int bdev_size(struct thread_data *td, struct fio_file *f) | |
334 | { | |
9b836561 | 335 | unsigned long long bytes = 0; |
53cdc686 JA |
336 | int r; |
337 | ||
df9c26b1 JA |
338 | if (td->io_ops->open_file(td, f)) { |
339 | log_err("fio: failed opening blockdev %s for size check\n", | |
340 | f->file_name); | |
341 | return 1; | |
342 | } | |
343 | ||
ecc314ba | 344 | r = blockdev_size(f, &bytes); |
53cdc686 | 345 | if (r) { |
e1161c32 | 346 | td_verror(td, r, "blockdev_size"); |
df9c26b1 | 347 | goto err; |
53cdc686 JA |
348 | } |
349 | ||
7ab077ab JA |
350 | if (!bytes) { |
351 | log_err("%s: zero sized block device?\n", f->file_name); | |
df9c26b1 | 352 | goto err; |
7ab077ab JA |
353 | } |
354 | ||
53cdc686 | 355 | f->real_file_size = bytes; |
22a57ba8 | 356 | td->io_ops->close_file(td, f); |
53cdc686 | 357 | return 0; |
df9c26b1 JA |
358 | err: |
359 | td->io_ops->close_file(td, f); | |
360 | return 1; | |
53cdc686 JA |
361 | } |
362 | ||
4ccdccd1 JA |
363 | static int char_size(struct thread_data *td, struct fio_file *f) |
364 | { | |
365 | #ifdef FIO_HAVE_CHARDEV_SIZE | |
9b836561 | 366 | unsigned long long bytes = 0; |
4ccdccd1 JA |
367 | int r; |
368 | ||
369 | if (td->io_ops->open_file(td, f)) { | |
b3ec877c | 370 | log_err("fio: failed opening chardev %s for size check\n", |
4ccdccd1 JA |
371 | f->file_name); |
372 | return 1; | |
373 | } | |
374 | ||
ecc314ba | 375 | r = chardev_size(f, &bytes); |
4ccdccd1 JA |
376 | if (r) { |
377 | td_verror(td, r, "chardev_size"); | |
378 | goto err; | |
379 | } | |
380 | ||
381 | if (!bytes) { | |
382 | log_err("%s: zero sized char device?\n", f->file_name); | |
383 | goto err; | |
384 | } | |
385 | ||
386 | f->real_file_size = bytes; | |
387 | td->io_ops->close_file(td, f); | |
388 | return 0; | |
389 | err: | |
390 | td->io_ops->close_file(td, f); | |
391 | return 1; | |
392 | #else | |
393 | f->real_file_size = -1ULL; | |
394 | return 0; | |
395 | #endif | |
396 | } | |
397 | ||
53cdc686 JA |
398 | static int get_file_size(struct thread_data *td, struct fio_file *f) |
399 | { | |
400 | int ret = 0; | |
401 | ||
d6aed795 | 402 | if (fio_file_size_known(f)) |
409b3417 JA |
403 | return 0; |
404 | ||
7bb48f84 JA |
405 | if (f->filetype == FIO_TYPE_FILE) |
406 | ret = file_size(td, f); | |
686fbd31 | 407 | else if (f->filetype == FIO_TYPE_BLOCK) |
53cdc686 | 408 | ret = bdev_size(td, f); |
4ccdccd1 JA |
409 | else if (f->filetype == FIO_TYPE_CHAR) |
410 | ret = char_size(td, f); | |
0f34169a TK |
411 | else |
412 | f->real_file_size = -1ULL; | |
53cdc686 | 413 | |
79591fa9 TK |
414 | /* |
415 | * Leave ->real_file_size with 0 since it could be expectation | |
416 | * of initial setup for regular files. | |
417 | */ | |
a0cb220b | 418 | if (ret) |
53cdc686 JA |
419 | return ret; |
420 | ||
0f34169a TK |
421 | /* |
422 | * If ->real_file_size is -1, a conditional for the message | |
423 | * "offset extends end" is always true, but it makes no sense, | |
424 | * so just return the same value here. | |
425 | */ | |
426 | if (f->real_file_size == -1ULL) { | |
427 | log_info("%s: failed to get file size of %s\n", td->o.name, | |
428 | f->file_name); | |
429 | return 1; | |
430 | } | |
431 | ||
432 | if (td->o.start_offset && f->file_offset == 0) | |
433 | dprint(FD_FILE, "offset of file %s not initialized yet\n", | |
434 | f->file_name); | |
957c81b9 TK |
435 | /* |
436 | * ->file_offset normally hasn't been initialized yet, so this | |
0f34169a | 437 | * is basically always false. |
957c81b9 | 438 | */ |
53cdc686 | 439 | if (f->file_offset > f->real_file_size) { |
0f2152c1 | 440 | log_err("%s: offset extends end (%llu > %llu)\n", td->o.name, |
4e0a8fa2 JA |
441 | (unsigned long long) f->file_offset, |
442 | (unsigned long long) f->real_file_size); | |
53cdc686 JA |
443 | return 1; |
444 | } | |
445 | ||
d6aed795 | 446 | fio_file_set_size_known(f); |
53cdc686 JA |
447 | return 0; |
448 | } | |
449 | ||
3baddf24 JA |
450 | static int __file_invalidate_cache(struct thread_data *td, struct fio_file *f, |
451 | unsigned long long off, | |
452 | unsigned long long len) | |
e5b401d4 | 453 | { |
942a851a | 454 | int errval = 0, ret = 0; |
e5b401d4 | 455 | |
c8931876 JA |
456 | #ifdef CONFIG_ESX |
457 | return 0; | |
458 | #endif | |
459 | ||
5e0074c2 | 460 | if (len == -1ULL) |
3baddf24 JA |
461 | len = f->io_size; |
462 | if (off == -1ULL) | |
463 | off = f->file_offset; | |
ee56ad50 | 464 | |
0d1cd207 JA |
465 | if (len == -1ULL || off == -1ULL) |
466 | return 0; | |
467 | ||
942a851a | 468 | if (td->io_ops->invalidate) { |
4499dbef TK |
469 | dprint(FD_IO, "invalidate %s cache %s\n", td->io_ops->name, |
470 | f->file_name); | |
d9b100fc | 471 | ret = td->io_ops->invalidate(td, f); |
942a851a | 472 | if (ret < 0) |
afd118db | 473 | errval = -ret; |
942a851a | 474 | } else if (f->filetype == FIO_TYPE_FILE) { |
4499dbef TK |
475 | dprint(FD_IO, "declare unneeded cache %s: %llu/%llu\n", |
476 | f->file_name, off, len); | |
ecc314ba | 477 | ret = posix_fadvise(f->fd, off, len, POSIX_FADV_DONTNEED); |
0b139881 | 478 | if (ret) |
942a851a | 479 | errval = ret; |
686fbd31 | 480 | } else if (f->filetype == FIO_TYPE_BLOCK) { |
2b9f9141 KR |
481 | int retry_count = 0; |
482 | ||
4499dbef | 483 | dprint(FD_IO, "drop page cache %s\n", f->file_name); |
ecc314ba | 484 | ret = blockdev_invalidate_cache(f); |
2b9f9141 KR |
485 | while (ret < 0 && errno == EAGAIN && retry_count++ < 25) { |
486 | /* | |
487 | * Linux multipath devices reject ioctl while | |
488 | * the maps are being updated. That window can | |
489 | * last tens of milliseconds; we'll try up to | |
490 | * a quarter of a second. | |
491 | */ | |
492 | usleep(10000); | |
493 | ret = blockdev_invalidate_cache(f); | |
494 | } | |
7e0e25c9 | 495 | if (ret < 0 && errno == EACCES && geteuid()) { |
7172cfe8 | 496 | if (!root_warn) { |
5ec10eaa JA |
497 | log_err("fio: only root may flush block " |
498 | "devices. Cache flush bypassed!\n"); | |
7172cfe8 JA |
499 | root_warn = 1; |
500 | } | |
7e0e25c9 JA |
501 | ret = 0; |
502 | } | |
942a851a JA |
503 | if (ret < 0) |
504 | errval = errno; | |
22de5d77 TK |
505 | else if (ret) /* probably not supported */ |
506 | errval = ret; | |
4499dbef TK |
507 | } else if (f->filetype == FIO_TYPE_CHAR || |
508 | f->filetype == FIO_TYPE_PIPE) { | |
509 | dprint(FD_IO, "invalidate not supported %s\n", f->file_name); | |
e5b401d4 | 510 | ret = 0; |
4499dbef | 511 | } |
e5b401d4 | 512 | |
dfe11fd1 JA |
513 | /* |
514 | * Cache flushing isn't a fatal condition, and we know it will | |
515 | * happen on some platforms where we don't have the proper | |
516 | * function to flush eg block device caches. So just warn and | |
517 | * continue on our way. | |
518 | */ | |
942a851a | 519 | if (errval) |
22de5d77 TK |
520 | log_info("fio: cache invalidation of %s failed: %s\n", |
521 | f->file_name, strerror(errval)); | |
e5b401d4 | 522 | |
dfe11fd1 | 523 | return 0; |
3baddf24 JA |
524 | |
525 | } | |
526 | ||
527 | int file_invalidate_cache(struct thread_data *td, struct fio_file *f) | |
528 | { | |
d6aed795 | 529 | if (!fio_file_open(f)) |
a5fb461f JA |
530 | return 0; |
531 | ||
5e0074c2 | 532 | return __file_invalidate_cache(td, f, -1ULL, -1ULL); |
e5b401d4 JA |
533 | } |
534 | ||
6977bcd0 | 535 | int generic_close_file(struct thread_data fio_unused *td, struct fio_file *f) |
53cdc686 | 536 | { |
6977bcd0 JA |
537 | int ret = 0; |
538 | ||
ee56ad50 | 539 | dprint(FD_FILE, "fd close %s\n", f->file_name); |
4906e0b5 JA |
540 | |
541 | remove_file_hash(f); | |
542 | ||
6977bcd0 JA |
543 | if (close(f->fd) < 0) |
544 | ret = errno; | |
545 | ||
b5af8293 | 546 | f->fd = -1; |
e6c4d732 JA |
547 | |
548 | if (f->shadow_fd != -1) { | |
549 | close(f->shadow_fd); | |
550 | f->shadow_fd = -1; | |
551 | } | |
552 | ||
710bf9c5 | 553 | f->engine_pos = 0; |
6977bcd0 | 554 | return ret; |
53cdc686 JA |
555 | } |
556 | ||
1ccc6dc7 | 557 | int file_lookup_open(struct fio_file *f, int flags) |
53cdc686 | 558 | { |
29c1349f | 559 | struct fio_file *__f; |
4d4e80f2 JA |
560 | int from_hash; |
561 | ||
562 | __f = lookup_file_hash(f->file_name); | |
563 | if (__f) { | |
9efef3c4 | 564 | dprint(FD_FILE, "found file in hash %s\n", f->file_name); |
4d4e80f2 | 565 | f->lock = __f->lock; |
4d4e80f2 JA |
566 | from_hash = 1; |
567 | } else { | |
9efef3c4 | 568 | dprint(FD_FILE, "file not found in hash %s\n", f->file_name); |
4d4e80f2 JA |
569 | from_hash = 0; |
570 | } | |
571 | ||
9c0f3f32 BC |
572 | #ifdef WIN32 |
573 | flags |= _O_BINARY; | |
574 | #endif | |
575 | ||
e8670ef8 | 576 | f->fd = open(f->file_name, flags, 0600); |
4d4e80f2 JA |
577 | return from_hash; |
578 | } | |
579 | ||
e6c4d732 JA |
580 | static int file_close_shadow_fds(struct thread_data *td) |
581 | { | |
582 | struct fio_file *f; | |
583 | int num_closed = 0; | |
584 | unsigned int i; | |
585 | ||
586 | for_each_file(td, f, i) { | |
587 | if (f->shadow_fd == -1) | |
588 | continue; | |
589 | ||
590 | close(f->shadow_fd); | |
591 | f->shadow_fd = -1; | |
592 | num_closed++; | |
593 | } | |
594 | ||
595 | return num_closed; | |
596 | } | |
597 | ||
4d4e80f2 JA |
598 | int generic_open_file(struct thread_data *td, struct fio_file *f) |
599 | { | |
66159828 | 600 | int is_std = 0; |
53cdc686 | 601 | int flags = 0; |
29c1349f | 602 | int from_hash = 0; |
53cdc686 | 603 | |
ee56ad50 JA |
604 | dprint(FD_FILE, "fd open %s\n", f->file_name); |
605 | ||
66159828 JA |
606 | if (!strcmp(f->file_name, "-")) { |
607 | if (td_rw(td)) { | |
608 | log_err("fio: can't read/write to stdin/out\n"); | |
609 | return 1; | |
610 | } | |
611 | is_std = 1; | |
ce98fa66 JA |
612 | |
613 | /* | |
614 | * move output logging to stderr, if we are writing to stdout | |
615 | */ | |
616 | if (td_write(td)) | |
617 | f_out = stderr; | |
66159828 JA |
618 | } |
619 | ||
6eaf09d6 SL |
620 | if (td_trim(td)) |
621 | goto skip_flags; | |
2dc1bbeb | 622 | if (td->o.odirect) |
2fd233b7 | 623 | flags |= OS_O_DIRECT; |
d01612f3 CM |
624 | if (td->o.oatomic) { |
625 | if (!FIO_O_ATOMIC) { | |
626 | td_verror(td, EINVAL, "OS does not support atomic IO"); | |
627 | return 1; | |
628 | } | |
629 | flags |= OS_O_DIRECT | FIO_O_ATOMIC; | |
630 | } | |
2dc1bbeb | 631 | if (td->o.sync_io) |
2fd233b7 | 632 | flags |= O_SYNC; |
2378826d | 633 | if (td->o.create_on_open && td->o.allow_create) |
814452bd | 634 | flags |= O_CREAT; |
6eaf09d6 SL |
635 | skip_flags: |
636 | if (f->filetype != FIO_TYPE_FILE) | |
637 | flags |= FIO_O_NOATIME; | |
53cdc686 | 638 | |
056f3459 | 639 | open_again: |
660a1cb5 | 640 | if (td_write(td)) { |
17308158 JA |
641 | if (!read_only) |
642 | flags |= O_RDWR; | |
53cdc686 | 643 | |
2378826d | 644 | if (f->filetype == FIO_TYPE_FILE && td->o.allow_create) |
2fd233b7 | 645 | flags |= O_CREAT; |
2fd233b7 | 646 | |
66159828 JA |
647 | if (is_std) |
648 | f->fd = dup(STDOUT_FILENO); | |
4d4e80f2 JA |
649 | else |
650 | from_hash = file_lookup_open(f, flags); | |
6eaf09d6 | 651 | } else if (td_read(td)) { |
4241ea8f | 652 | if (f->filetype == FIO_TYPE_CHAR && !read_only) |
2fd233b7 JA |
653 | flags |= O_RDWR; |
654 | else | |
655 | flags |= O_RDONLY; | |
656 | ||
66159828 JA |
657 | if (is_std) |
658 | f->fd = dup(STDIN_FILENO); | |
4d4e80f2 JA |
659 | else |
660 | from_hash = file_lookup_open(f, flags); | |
49cff020 TK |
661 | } else if (td_trim(td)) { |
662 | assert(!td_rw(td)); /* should have matched above */ | |
6eaf09d6 SL |
663 | flags |= O_RDWR; |
664 | from_hash = file_lookup_open(f, flags); | |
53cdc686 JA |
665 | } |
666 | ||
667 | if (f->fd == -1) { | |
e4e33258 | 668 | char buf[FIO_VERROR_SIZE]; |
e1161c32 JA |
669 | int __e = errno; |
670 | ||
835d9b9e | 671 | if (__e == EPERM && (flags & FIO_O_NOATIME)) { |
5921e80c | 672 | flags &= ~FIO_O_NOATIME; |
056f3459 AC |
673 | goto open_again; |
674 | } | |
e6c4d732 JA |
675 | if (__e == EMFILE && file_close_shadow_fds(td)) |
676 | goto open_again; | |
056f3459 | 677 | |
98ffb8f3 | 678 | snprintf(buf, sizeof(buf), "open(%s)", f->file_name); |
e4e33258 | 679 | |
a93c5f04 JA |
680 | if (__e == EINVAL && (flags & OS_O_DIRECT)) { |
681 | log_err("fio: looks like your file system does not " \ | |
682 | "support direct=1/buffered=0\n"); | |
683 | } | |
684 | ||
e4e33258 | 685 | td_verror(td, __e, buf); |
3ce3ad33 | 686 | return 1; |
53cdc686 JA |
687 | } |
688 | ||
29c1349f JA |
689 | if (!from_hash && f->fd != -1) { |
690 | if (add_file_hash(f)) { | |
3f0ca9b9 | 691 | int fio_unused ret; |
29c1349f JA |
692 | |
693 | /* | |
e6c4d732 JA |
694 | * Stash away descriptor for later close. This is to |
695 | * work-around a "feature" on Linux, where a close of | |
696 | * an fd that has been opened for write will trigger | |
697 | * udev to call blkid to check partitions, fs id, etc. | |
de8f6de9 | 698 | * That pollutes the device cache, which can slow down |
e6c4d732 | 699 | * unbuffered accesses. |
29c1349f | 700 | */ |
e6c4d732 JA |
701 | if (f->shadow_fd == -1) |
702 | f->shadow_fd = f->fd; | |
703 | else { | |
704 | /* | |
705 | * OK to ignore, we haven't done anything | |
706 | * with it | |
707 | */ | |
708 | ret = generic_close_file(td, f); | |
709 | } | |
29c1349f JA |
710 | goto open_again; |
711 | } | |
712 | } | |
4906e0b5 | 713 | |
53cdc686 | 714 | return 0; |
b5af8293 JA |
715 | } |
716 | ||
79591fa9 TK |
717 | /* |
718 | * This function i.e. get_file_size() is the default .get_file_size | |
719 | * implementation of majority of I/O engines. | |
720 | */ | |
df9c26b1 | 721 | int generic_get_file_size(struct thread_data *td, struct fio_file *f) |
21972cde | 722 | { |
df9c26b1 | 723 | return get_file_size(td, f); |
21972cde JA |
724 | } |
725 | ||
7bb48f84 JA |
726 | /* |
727 | * open/close all files, so that ->real_file_size gets set | |
728 | */ | |
bab3fd58 | 729 | static int get_file_sizes(struct thread_data *td) |
7bb48f84 JA |
730 | { |
731 | struct fio_file *f; | |
732 | unsigned int i; | |
bab3fd58 | 733 | int err = 0; |
7bb48f84 JA |
734 | |
735 | for_each_file(td, f, i) { | |
08c44a55 | 736 | dprint(FD_FILE, "get file size for %p/%d/%s\n", f, i, |
5ec10eaa | 737 | f->file_name); |
9efef3c4 | 738 | |
99a47c69 | 739 | if (td_io_get_file_size(td, f)) { |
40b44f4a JA |
740 | if (td->error != ENOENT) { |
741 | log_err("%s\n", td->verror); | |
742 | err = 1; | |
3ce3ad33 | 743 | break; |
40b44f4a | 744 | } |
541d66d7 | 745 | clear_error(td); |
07eb79df | 746 | } |
409b3417 | 747 | |
79591fa9 TK |
748 | /* |
749 | * There are corner cases where we end up with -1 for | |
750 | * ->real_file_size due to unsupported file type, etc. | |
751 | * We then just set to size option value divided by number | |
752 | * of files, similar to the way file ->io_size is set. | |
753 | * stat(2) failure doesn't set ->real_file_size to -1. | |
754 | */ | |
409b3417 JA |
755 | if (f->real_file_size == -1ULL && td->o.size) |
756 | f->real_file_size = td->o.size / td->o.nr_files; | |
7bb48f84 | 757 | } |
bab3fd58 JA |
758 | |
759 | return err; | |
7bb48f84 JA |
760 | } |
761 | ||
2e3bd4c2 JA |
762 | struct fio_mount { |
763 | struct flist_head list; | |
764 | const char *base; | |
765 | char __base[256]; | |
766 | unsigned int key; | |
767 | }; | |
768 | ||
769 | /* | |
770 | * Get free number of bytes for each file on each unique mount. | |
771 | */ | |
772 | static unsigned long long get_fs_free_counts(struct thread_data *td) | |
773 | { | |
774 | struct flist_head *n, *tmp; | |
68b0316f | 775 | unsigned long long ret = 0; |
2e3bd4c2 JA |
776 | struct fio_mount *fm; |
777 | FLIST_HEAD(list); | |
778 | struct fio_file *f; | |
779 | unsigned int i; | |
780 | ||
781 | for_each_file(td, f, i) { | |
782 | struct stat sb; | |
783 | char buf[256]; | |
784 | ||
686fbd31 | 785 | if (f->filetype == FIO_TYPE_BLOCK || f->filetype == FIO_TYPE_CHAR) { |
4ccdccd1 JA |
786 | if (f->real_file_size != -1ULL) |
787 | ret += f->real_file_size; | |
68b0316f JA |
788 | continue; |
789 | } else if (f->filetype != FIO_TYPE_FILE) | |
790 | continue; | |
791 | ||
da27a4bf JA |
792 | buf[255] = '\0'; |
793 | strncpy(buf, f->file_name, 255); | |
2e3bd4c2 JA |
794 | |
795 | if (stat(buf, &sb) < 0) { | |
796 | if (errno != ENOENT) | |
797 | break; | |
798 | strcpy(buf, "."); | |
799 | if (stat(buf, &sb) < 0) | |
800 | break; | |
801 | } | |
802 | ||
803 | fm = NULL; | |
804 | flist_for_each(n, &list) { | |
805 | fm = flist_entry(n, struct fio_mount, list); | |
806 | if (fm->key == sb.st_dev) | |
807 | break; | |
808 | ||
809 | fm = NULL; | |
810 | } | |
811 | ||
812 | if (fm) | |
813 | continue; | |
814 | ||
3660ceae JA |
815 | fm = calloc(1, sizeof(*fm)); |
816 | strncpy(fm->__base, buf, sizeof(fm->__base) - 1); | |
2e3bd4c2 JA |
817 | fm->base = basename(fm->__base); |
818 | fm->key = sb.st_dev; | |
819 | flist_add(&fm->list, &list); | |
820 | } | |
821 | ||
2e3bd4c2 JA |
822 | flist_for_each_safe(n, tmp, &list) { |
823 | unsigned long long sz; | |
824 | ||
825 | fm = flist_entry(n, struct fio_mount, list); | |
826 | flist_del(&fm->list); | |
827 | ||
c08ad04c | 828 | sz = get_fs_free_size(fm->base); |
2e3bd4c2 JA |
829 | if (sz && sz != -1ULL) |
830 | ret += sz; | |
831 | ||
832 | free(fm); | |
833 | } | |
834 | ||
835 | return ret; | |
836 | } | |
837 | ||
bedc9dc2 | 838 | uint64_t get_start_offset(struct thread_data *td, struct fio_file *f) |
ce95d651 | 839 | { |
bedc9dc2 | 840 | struct thread_options *o = &td->o; |
872e0415 JA |
841 | unsigned long long align_bs; |
842 | unsigned long long offset; | |
bedc9dc2 JA |
843 | |
844 | if (o->file_append && f->filetype == FIO_TYPE_FILE) | |
845 | return f->real_file_size; | |
846 | ||
89978a6b | 847 | if (o->start_offset_percent > 0) { |
872e0415 JA |
848 | /* |
849 | * if blockalign is provided, find the min across read, write, | |
850 | * and trim | |
851 | */ | |
89978a6b BW |
852 | if (fio_option_is_set(o, ba)) { |
853 | align_bs = (unsigned long long) min(o->ba[DDIR_READ], o->ba[DDIR_WRITE]); | |
854 | align_bs = min((unsigned long long) o->ba[DDIR_TRIM], align_bs); | |
872e0415 JA |
855 | } else { |
856 | /* else take the minimum block size */ | |
89978a6b BW |
857 | align_bs = td_min_bs(td); |
858 | } | |
859 | ||
860 | /* calculate the raw offset */ | |
861 | offset = (f->real_file_size * o->start_offset_percent / 100) + | |
862 | (td->subjob_number * o->offset_increment); | |
863 | ||
872e0415 JA |
864 | /* |
865 | * block align the offset at the next available boundary at | |
866 | * ceiling(offset / align_bs) * align_bs | |
867 | */ | |
89978a6b BW |
868 | offset = (offset / align_bs + (offset % align_bs != 0)) * align_bs; |
869 | ||
872e0415 JA |
870 | } else { |
871 | /* start_offset_percent not set */ | |
f0d13935 JA |
872 | offset = o->start_offset + |
873 | td->subjob_number * o->offset_increment; | |
89978a6b | 874 | } |
872e0415 | 875 | |
89978a6b | 876 | return offset; |
ce95d651 DE |
877 | } |
878 | ||
7bb48f84 JA |
879 | /* |
880 | * Open the files and setup files sizes, creating files if necessary. | |
881 | */ | |
53cdc686 JA |
882 | int setup_files(struct thread_data *td) |
883 | { | |
7bb48f84 | 884 | unsigned long long total_size, extend_size; |
de98bd30 | 885 | struct thread_options *o = &td->o; |
53cdc686 | 886 | struct fio_file *f; |
002fe734 | 887 | unsigned int i, nr_fs_extra = 0; |
000b0803 | 888 | int err = 0, need_extend; |
e90391eb | 889 | int old_state; |
002fe734 JA |
890 | const unsigned int bs = td_min_bs(td); |
891 | uint64_t fs = 0; | |
53cdc686 | 892 | |
ee56ad50 JA |
893 | dprint(FD_FILE, "setup files\n"); |
894 | ||
8edd973d | 895 | old_state = td_bump_runstate(td, TD_SETTING_UP); |
e90391eb | 896 | |
de98bd30 | 897 | if (o->read_iolog_file) |
25460cf6 | 898 | goto done; |
691c8fb0 | 899 | |
53cdc686 | 900 | /* |
79591fa9 TK |
901 | * Find out physical size of files or devices for this thread, |
902 | * before we determine I/O size and range of our targets. | |
903 | * If ioengine defines a setup() method, it's responsible for | |
7bb48f84 JA |
904 | * opening the files and setting f->real_file_size to indicate |
905 | * the valid range for that file. | |
53cdc686 JA |
906 | */ |
907 | if (td->io_ops->setup) | |
7bb48f84 JA |
908 | err = td->io_ops->setup(td); |
909 | else | |
bab3fd58 | 910 | err = get_file_sizes(td); |
53cdc686 | 911 | |
f1027063 | 912 | if (err) |
e90391eb | 913 | goto err_out; |
f1027063 | 914 | |
0a7eb121 | 915 | /* |
7bb48f84 JA |
916 | * check sizes. if the files/devices do not exist and the size |
917 | * isn't passed to fio, abort. | |
0a7eb121 | 918 | */ |
7bb48f84 JA |
919 | total_size = 0; |
920 | for_each_file(td, f, i) { | |
49a416bd | 921 | f->fileno = i; |
7bb48f84 JA |
922 | if (f->real_file_size == -1ULL) |
923 | total_size = -1ULL; | |
924 | else | |
925 | total_size += f->real_file_size; | |
926 | } | |
0a7eb121 | 927 | |
de98bd30 | 928 | if (o->fill_device) |
2e3bd4c2 JA |
929 | td->fill_device_size = get_fs_free_counts(td); |
930 | ||
7bb48f84 JA |
931 | /* |
932 | * device/file sizes are zero and no size given, punt | |
933 | */ | |
de98bd30 | 934 | if ((!total_size || total_size == -1ULL) && !o->size && |
9b87f09b | 935 | !td_ioengine_flagged(td, FIO_NOIO) && !o->fill_device && |
de98bd30 JA |
936 | !(o->nr_files && (o->file_size_low || o->file_size_high))) { |
937 | log_err("%s: you need to specify size=\n", o->name); | |
e1161c32 | 938 | td_verror(td, EINVAL, "total_file_size"); |
e90391eb | 939 | goto err_out; |
53cdc686 JA |
940 | } |
941 | ||
002fe734 JA |
942 | /* |
943 | * Calculate per-file size and potential extra size for the | |
79591fa9 | 944 | * first files, if needed (i.e. if we don't have a fixed size). |
002fe734 | 945 | */ |
d1e78e6b | 946 | if (!o->file_size_low && o->nr_files) { |
002fe734 JA |
947 | uint64_t all_fs; |
948 | ||
949 | fs = o->size / o->nr_files; | |
950 | all_fs = fs * o->nr_files; | |
951 | ||
952 | if (all_fs < o->size) | |
953 | nr_fs_extra = (o->size - all_fs) / bs; | |
954 | } | |
955 | ||
7bb48f84 JA |
956 | /* |
957 | * now file sizes are known, so we can set ->io_size. if size= is | |
958 | * not given, ->io_size is just equal to ->real_file_size. if size | |
959 | * is given, ->io_size is size / nr_files. | |
960 | */ | |
961 | extend_size = total_size = 0; | |
962 | need_extend = 0; | |
963 | for_each_file(td, f, i) { | |
bedc9dc2 | 964 | f->file_offset = get_start_offset(td, f); |
bcdedd0a | 965 | |
79591fa9 TK |
966 | /* |
967 | * Update ->io_size depending on options specified. | |
968 | * ->file_size_low being 0 means filesize option isn't set. | |
969 | * Non zero ->file_size_low equals ->file_size_high means | |
970 | * filesize option is set in a fixed size format. | |
971 | * Non zero ->file_size_low not equals ->file_size_high means | |
972 | * filesize option is set in a range format. | |
973 | */ | |
de98bd30 | 974 | if (!o->file_size_low) { |
7bb48f84 | 975 | /* |
79591fa9 | 976 | * no file size or range given, file size is equal to |
c4aa2d08 | 977 | * total size divided by number of files. If the size |
002fe734 JA |
978 | * doesn't divide nicely with the min blocksize, |
979 | * make the first files bigger. | |
7bb48f84 | 980 | */ |
002fe734 JA |
981 | f->io_size = fs; |
982 | if (nr_fs_extra) { | |
983 | nr_fs_extra--; | |
984 | f->io_size += bs; | |
985 | } | |
986 | ||
c4aa2d08 | 987 | /* |
c6dfd837 TK |
988 | * We normally don't come here for regular files, but |
989 | * if the result is 0 for a regular file, set it to the | |
990 | * real file size. This could be size of the existing | |
991 | * one if it already exists, but otherwise will be set | |
992 | * to 0. A new file won't be created because | |
c4aa2d08 TK |
993 | * ->io_size + ->file_offset equals ->real_file_size. |
994 | */ | |
995 | if (!f->io_size) { | |
996 | if (f->file_offset > f->real_file_size) | |
997 | goto err_offset; | |
273f8c91 | 998 | f->io_size = f->real_file_size - f->file_offset; |
c4aa2d08 TK |
999 | if (!f->io_size) |
1000 | log_info("fio: file %s may be ignored\n", | |
1001 | f->file_name); | |
1002 | } | |
de98bd30 JA |
1003 | } else if (f->real_file_size < o->file_size_low || |
1004 | f->real_file_size > o->file_size_high) { | |
1005 | if (f->file_offset > o->file_size_low) | |
bcdedd0a | 1006 | goto err_offset; |
7bb48f84 JA |
1007 | /* |
1008 | * file size given. if it's fixed, use that. if it's a | |
1009 | * range, generate a random size in-between. | |
1010 | */ | |
de98bd30 JA |
1011 | if (o->file_size_low == o->file_size_high) |
1012 | f->io_size = o->file_size_low - f->file_offset; | |
1013 | else { | |
5ec10eaa JA |
1014 | f->io_size = get_rand_file_size(td) |
1015 | - f->file_offset; | |
1016 | } | |
65bdb10a | 1017 | } else |
bcdedd0a | 1018 | f->io_size = f->real_file_size - f->file_offset; |
53cdc686 | 1019 | |
7bb48f84 JA |
1020 | if (f->io_size == -1ULL) |
1021 | total_size = -1ULL; | |
4d002569 | 1022 | else { |
e391c704 | 1023 | if (o->size_percent) { |
76e4bb4f SW |
1024 | uint64_t file_size; |
1025 | ||
1026 | file_size = f->io_size + f->file_offset; | |
1027 | f->io_size = (file_size * | |
1028 | o->size_percent) / 100; | |
1029 | if (f->io_size > (file_size - f->file_offset)) | |
1030 | f->io_size = file_size - f->file_offset; | |
1031 | ||
e391c704 JA |
1032 | f->io_size -= (f->io_size % td_min_bs(td)); |
1033 | } | |
7bb48f84 | 1034 | total_size += f->io_size; |
4d002569 | 1035 | } |
7bb48f84 JA |
1036 | |
1037 | if (f->filetype == FIO_TYPE_FILE && | |
bcdedd0a | 1038 | (f->io_size + f->file_offset) > f->real_file_size && |
9b87f09b | 1039 | !td_ioengine_flagged(td, FIO_DISKLESSIO)) { |
5fd31680 JA |
1040 | if (!o->create_on_open) { |
1041 | need_extend++; | |
814452bd | 1042 | extend_size += (f->io_size + f->file_offset); |
43558406 | 1043 | fio_file_set_extend(f); |
5fd31680 | 1044 | } else |
814452bd | 1045 | f->real_file_size = f->io_size + f->file_offset; |
5ec10eaa | 1046 | } |
7bb48f84 | 1047 | } |
53cdc686 | 1048 | |
66347cfa DE |
1049 | if (td->o.block_error_hist) { |
1050 | int len; | |
1051 | ||
1052 | assert(td->o.nr_files == 1); /* checked in fixup_options */ | |
1053 | f = td->files[0]; | |
1054 | len = f->io_size / td->o.bs[DDIR_TRIM]; | |
1055 | if (len > MAX_NR_BLOCK_INFOS || len <= 0) { | |
1056 | log_err("fio: cannot calculate block histogram with " | |
1057 | "%d trim blocks, maximum %d\n", | |
1058 | len, MAX_NR_BLOCK_INFOS); | |
1059 | td_verror(td, EINVAL, "block_error_hist"); | |
1060 | goto err_out; | |
1061 | } | |
1062 | ||
1063 | td->ts.nr_block_infos = len; | |
8a68c41c | 1064 | for (i = 0; i < len; i++) |
66347cfa DE |
1065 | td->ts.block_infos[i] = |
1066 | BLOCK_INFO(0, BLOCK_STATE_UNINIT); | |
1067 | } else | |
1068 | td->ts.nr_block_infos = 0; | |
1069 | ||
0bc27b0b | 1070 | if (!o->size || (total_size && o->size > total_size)) |
de98bd30 | 1071 | o->size = total_size; |
21972cde | 1072 | |
d1faa06d JA |
1073 | if (o->size < td_min_bs(td)) { |
1074 | log_err("fio: blocksize too large for data set\n"); | |
1075 | goto err_out; | |
1076 | } | |
1077 | ||
7bb48f84 | 1078 | /* |
79591fa9 TK |
1079 | * See if we need to extend some files, typically needed when our |
1080 | * target regular files don't exist yet, but our jobs require them | |
1081 | * initially due to read I/Os. | |
7bb48f84 JA |
1082 | */ |
1083 | if (need_extend) { | |
1084 | temp_stall_ts = 1; | |
7c5f4cdb TK |
1085 | if (output_format & FIO_OUTPUT_NORMAL) { |
1086 | log_info("%s: Laying out IO file%s (%u file%s / %s%lluMiB)\n", | |
1087 | o->name, | |
1088 | need_extend > 1 ? "s" : "", | |
1089 | need_extend, | |
1090 | need_extend > 1 ? "s" : "", | |
1091 | need_extend > 1 ? "total " : "", | |
1092 | extend_size >> 20); | |
1093 | } | |
7bb48f84 JA |
1094 | |
1095 | for_each_file(td, f, i) { | |
5e0074c2 | 1096 | unsigned long long old_len = -1ULL, extend_len = -1ULL; |
3baddf24 | 1097 | |
d6aed795 | 1098 | if (!fio_file_extend(f)) |
7bb48f84 JA |
1099 | continue; |
1100 | ||
409b3417 | 1101 | assert(f->filetype == FIO_TYPE_FILE); |
d6aed795 | 1102 | fio_file_clear_extend(f); |
de98bd30 | 1103 | if (!o->fill_device) { |
5e0074c2 | 1104 | old_len = f->real_file_size; |
0b9d69ec JA |
1105 | extend_len = f->io_size + f->file_offset - |
1106 | old_len; | |
5e0074c2 | 1107 | } |
bcdedd0a | 1108 | f->real_file_size = (f->io_size + f->file_offset); |
7bb48f84 JA |
1109 | err = extend_file(td, f); |
1110 | if (err) | |
3baddf24 | 1111 | break; |
5e0074c2 | 1112 | |
3baddf24 JA |
1113 | err = __file_invalidate_cache(td, f, old_len, |
1114 | extend_len); | |
9824f73b JA |
1115 | |
1116 | /* | |
1117 | * Shut up static checker | |
1118 | */ | |
1119 | if (f->fd != -1) | |
1120 | close(f->fd); | |
1121 | ||
3baddf24 JA |
1122 | f->fd = -1; |
1123 | if (err) | |
7bb48f84 JA |
1124 | break; |
1125 | } | |
1126 | temp_stall_ts = 0; | |
1127 | } | |
1128 | ||
1129 | if (err) | |
e90391eb | 1130 | goto err_out; |
7bb48f84 | 1131 | |
de98bd30 JA |
1132 | if (!o->zone_size) |
1133 | o->zone_size = o->size; | |
7bb48f84 | 1134 | |
ea966f81 JA |
1135 | /* |
1136 | * iolog already set the total io size, if we read back | |
1137 | * stored entries. | |
1138 | */ | |
77731b29 | 1139 | if (!o->read_iolog_file) { |
5be9bf09 TK |
1140 | if (o->io_size) |
1141 | td->total_io_size = o->io_size * o->loops; | |
77731b29 JA |
1142 | else |
1143 | td->total_io_size = o->size * o->loops; | |
1144 | } | |
25460cf6 JA |
1145 | |
1146 | done: | |
de98bd30 | 1147 | if (o->create_only) |
25460cf6 JA |
1148 | td->done = 1; |
1149 | ||
8edd973d | 1150 | td_restore_runstate(td, old_state); |
7bb48f84 | 1151 | return 0; |
bcdedd0a | 1152 | err_offset: |
de98bd30 | 1153 | log_err("%s: you need to specify valid offset=\n", o->name); |
e90391eb | 1154 | err_out: |
8edd973d | 1155 | td_restore_runstate(td, old_state); |
bcdedd0a | 1156 | return 1; |
53cdc686 JA |
1157 | } |
1158 | ||
afad68f7 ZY |
1159 | int pre_read_files(struct thread_data *td) |
1160 | { | |
1161 | struct fio_file *f; | |
1162 | unsigned int i; | |
1163 | ||
1164 | dprint(FD_FILE, "pre_read files\n"); | |
1165 | ||
1166 | for_each_file(td, f, i) { | |
afe0d59a TK |
1167 | if (pre_read_file(td, f)) |
1168 | return -1; | |
afad68f7 ZY |
1169 | } |
1170 | ||
afe0d59a | 1171 | return 0; |
afad68f7 ZY |
1172 | } |
1173 | ||
9c6f6316 JA |
1174 | static int __init_rand_distribution(struct thread_data *td, struct fio_file *f) |
1175 | { | |
2316296a | 1176 | unsigned int range_size, seed; |
9c6f6316 | 1177 | unsigned long nranges; |
42da5c8b | 1178 | uint64_t fsize; |
9c6f6316 JA |
1179 | |
1180 | range_size = min(td->o.min_bs[DDIR_READ], td->o.min_bs[DDIR_WRITE]); | |
42da5c8b | 1181 | fsize = min(f->real_file_size, f->io_size); |
9c6f6316 | 1182 | |
42da5c8b | 1183 | nranges = (fsize + range_size - 1) / range_size; |
9c6f6316 | 1184 | |
2316296a | 1185 | seed = jhash(f->file_name, strlen(f->file_name), 0) * td->thread_number; |
8425687e JA |
1186 | if (!td->o.rand_repeatable) |
1187 | seed = td->rand_seeds[4]; | |
1188 | ||
9c6f6316 | 1189 | if (td->o.random_distribution == FIO_RAND_DIST_ZIPF) |
888677a4 | 1190 | zipf_init(&f->zipf, nranges, td->o.zipf_theta.u.f, seed); |
56d9fa4b | 1191 | else if (td->o.random_distribution == FIO_RAND_DIST_PARETO) |
888677a4 | 1192 | pareto_init(&f->zipf, nranges, td->o.pareto_h.u.f, seed); |
56d9fa4b | 1193 | else if (td->o.random_distribution == FIO_RAND_DIST_GAUSS) |
f88cd222 | 1194 | gauss_init(&f->gauss, nranges, td->o.gauss_dev.u.f, seed); |
9c6f6316 JA |
1195 | |
1196 | return 1; | |
1197 | } | |
1198 | ||
1199 | static int init_rand_distribution(struct thread_data *td) | |
1200 | { | |
1201 | struct fio_file *f; | |
1202 | unsigned int i; | |
1203 | int state; | |
1204 | ||
1205 | if (td->o.random_distribution == FIO_RAND_DIST_RANDOM) | |
1206 | return 0; | |
1207 | ||
8edd973d JA |
1208 | state = td_bump_runstate(td, TD_SETTING_UP); |
1209 | ||
9c6f6316 JA |
1210 | for_each_file(td, f, i) |
1211 | __init_rand_distribution(td, f); | |
8edd973d JA |
1212 | |
1213 | td_restore_runstate(td, state); | |
9c6f6316 JA |
1214 | |
1215 | return 1; | |
1216 | } | |
1217 | ||
75f90d5f JA |
1218 | /* |
1219 | * Check if the number of blocks exceeds the randomness capability of | |
1220 | * the selected generator. Tausworthe is 32-bit, the others are fullly | |
1221 | * 64-bit capable. | |
1222 | */ | |
1223 | static int check_rand_gen_limits(struct thread_data *td, struct fio_file *f, | |
1224 | uint64_t blocks) | |
1225 | { | |
1226 | if (blocks <= FRAND32_MAX) | |
1227 | return 0; | |
1228 | if (td->o.random_generator != FIO_RAND_GEN_TAUSWORTHE) | |
1229 | return 0; | |
1230 | ||
1231 | /* | |
1232 | * If the user hasn't specified a random generator, switch | |
1233 | * to tausworthe64 with informational warning. If the user did | |
1234 | * specify one, just warn. | |
1235 | */ | |
1236 | log_info("fio: file %s exceeds 32-bit tausworthe random generator.\n", | |
1237 | f->file_name); | |
1238 | ||
1239 | if (!fio_option_is_set(&td->o, random_generator)) { | |
1240 | log_info("fio: Switching to tausworthe64. Use the " | |
1241 | "random_generator= option to get rid of this " | |
4e795a3e | 1242 | "warning.\n"); |
75f90d5f JA |
1243 | td->o.random_generator = FIO_RAND_GEN_TAUSWORTHE64; |
1244 | return 0; | |
1245 | } | |
1246 | ||
1247 | /* | |
1248 | * Just make this information to avoid breaking scripts. | |
1249 | */ | |
1250 | log_info("fio: Use the random_generator= option to switch to lfsr or " | |
1251 | "tausworthe64.\n"); | |
1252 | return 0; | |
1253 | } | |
1254 | ||
68727076 JA |
1255 | int init_random_map(struct thread_data *td) |
1256 | { | |
51ede0b1 | 1257 | unsigned long long blocks; |
68727076 JA |
1258 | struct fio_file *f; |
1259 | unsigned int i; | |
1260 | ||
9c6f6316 JA |
1261 | if (init_rand_distribution(td)) |
1262 | return 0; | |
3831a843 | 1263 | if (!td_random(td)) |
68727076 JA |
1264 | return 0; |
1265 | ||
1266 | for_each_file(td, f, i) { | |
42da5c8b | 1267 | uint64_t fsize = min(f->real_file_size, f->io_size); |
38f30c81 | 1268 | |
42da5c8b | 1269 | blocks = fsize / (unsigned long long) td->o.rw_min_bs; |
53737ae0 | 1270 | |
75f90d5f | 1271 | if (check_rand_gen_limits(td, f, blocks)) |
37fbd7e9 | 1272 | return 1; |
37fbd7e9 | 1273 | |
8055e41d | 1274 | if (td->o.random_generator == FIO_RAND_GEN_LFSR) { |
82af46be JA |
1275 | unsigned long seed; |
1276 | ||
1277 | seed = td->rand_seeds[FIO_RAND_BLOCK_OFF]; | |
9c0f3f32 | 1278 | |
967d1b63 JA |
1279 | if (!lfsr_init(&f->lfsr, blocks, seed, 0)) { |
1280 | fio_file_set_lfsr(f); | |
8055e41d | 1281 | continue; |
967d1b63 | 1282 | } |
3831a843 | 1283 | } else if (!td->o.norandommap) { |
7ebd796f | 1284 | f->io_axmap = axmap_new(blocks); |
967d1b63 JA |
1285 | if (f->io_axmap) { |
1286 | fio_file_set_axmap(f); | |
8055e41d | 1287 | continue; |
967d1b63 | 1288 | } |
1cad7121 JA |
1289 | } else if (td->o.norandommap) |
1290 | continue; | |
ceadd59e | 1291 | |
2b386d25 | 1292 | if (!td->o.softrandommap) { |
5ec10eaa JA |
1293 | log_err("fio: failed allocating random map. If running" |
1294 | " a large number of jobs, try the 'norandommap'" | |
2b386d25 JA |
1295 | " option or set 'softrandommap'. Or give" |
1296 | " a larger --alloc-size to fio.\n"); | |
68727076 JA |
1297 | return 1; |
1298 | } | |
303032ae JA |
1299 | |
1300 | log_info("fio: file %s failed allocating random map. Running " | |
1301 | "job without.\n", f->file_name); | |
68727076 JA |
1302 | } |
1303 | ||
1304 | return 0; | |
1305 | } | |
1306 | ||
53cdc686 JA |
1307 | void close_files(struct thread_data *td) |
1308 | { | |
0ab8db89 | 1309 | struct fio_file *f; |
af52b345 | 1310 | unsigned int i; |
53cdc686 | 1311 | |
2be3eec3 JA |
1312 | for_each_file(td, f, i) { |
1313 | if (fio_file_open(f)) | |
1314 | td_io_close_file(td, f); | |
1315 | } | |
24ffd2c2 JA |
1316 | } |
1317 | ||
1318 | void close_and_free_files(struct thread_data *td) | |
1319 | { | |
1320 | struct fio_file *f; | |
1321 | unsigned int i; | |
1322 | ||
ee56ad50 JA |
1323 | dprint(FD_FILE, "close files\n"); |
1324 | ||
0ab8db89 | 1325 | for_each_file(td, f, i) { |
38ef9c90 CF |
1326 | if (td->o.unlink && f->filetype == FIO_TYPE_FILE) { |
1327 | dprint(FD_FILE, "free unlink %s\n", f->file_name); | |
1328 | td_io_unlink_file(td, f); | |
1329 | } | |
1330 | ||
22a57ba8 JA |
1331 | if (fio_file_open(f)) |
1332 | td_io_close_file(td, f); | |
1333 | ||
b9fbcf21 | 1334 | remove_file_hash(f); |
b3dc7f07 | 1335 | |
b5f4d8ba JA |
1336 | if (td->o.unlink && f->filetype == FIO_TYPE_FILE) { |
1337 | dprint(FD_FILE, "free unlink %s\n", f->file_name); | |
38ef9c90 | 1338 | td_io_unlink_file(td, f); |
b5f4d8ba JA |
1339 | } |
1340 | ||
f17c4392 | 1341 | sfree(f->file_name); |
fa1da865 | 1342 | f->file_name = NULL; |
967d1b63 JA |
1343 | if (fio_file_axmap(f)) { |
1344 | axmap_free(f->io_axmap); | |
1345 | f->io_axmap = NULL; | |
1346 | } | |
78d99e6a | 1347 | sfree(f); |
53cdc686 | 1348 | } |
b4a6a59a | 1349 | |
2dc1bbeb | 1350 | td->o.filename = NULL; |
cade3ef4 | 1351 | free(td->files); |
d7df1d13 | 1352 | free(td->file_locks); |
9efef3c4 | 1353 | td->files_index = 0; |
b4a6a59a | 1354 | td->files = NULL; |
d7df1d13 | 1355 | td->file_locks = NULL; |
27ddbfa0 | 1356 | td->o.file_lock_mode = FILE_LOCK_NONE; |
2dc1bbeb | 1357 | td->o.nr_files = 0; |
53cdc686 | 1358 | } |
af52b345 | 1359 | |
e3bab463 | 1360 | static void get_file_type(struct fio_file *f) |
af52b345 JA |
1361 | { |
1362 | struct stat sb; | |
1363 | ||
66159828 JA |
1364 | if (!strcmp(f->file_name, "-")) |
1365 | f->filetype = FIO_TYPE_PIPE; | |
1366 | else | |
1367 | f->filetype = FIO_TYPE_FILE; | |
af52b345 | 1368 | |
d5b78f5a | 1369 | #ifdef WIN32 |
3892182a BC |
1370 | /* \\.\ is the device namespace in Windows, where every file is |
1371 | * a block device */ | |
1372 | if (strncmp(f->file_name, "\\\\.\\", 4) == 0) | |
686fbd31 | 1373 | f->filetype = FIO_TYPE_BLOCK; |
d5b78f5a | 1374 | #endif |
3892182a | 1375 | |
b30d395e | 1376 | if (!stat(f->file_name, &sb)) { |
3892182a | 1377 | if (S_ISBLK(sb.st_mode)) |
686fbd31 | 1378 | f->filetype = FIO_TYPE_BLOCK; |
af52b345 JA |
1379 | else if (S_ISCHR(sb.st_mode)) |
1380 | f->filetype = FIO_TYPE_CHAR; | |
b5605e9d JA |
1381 | else if (S_ISFIFO(sb.st_mode)) |
1382 | f->filetype = FIO_TYPE_PIPE; | |
af52b345 JA |
1383 | } |
1384 | } | |
1385 | ||
1b2a83dc | 1386 | static bool __is_already_allocated(const char *fname, bool set) |
190b8f0c | 1387 | { |
90426237 | 1388 | struct flist_head *entry; |
1b2a83dc | 1389 | bool ret; |
bcbfeefa | 1390 | |
1b2a83dc JA |
1391 | ret = file_bloom_exists(fname, set); |
1392 | if (!ret) | |
1393 | return ret; | |
90426237 JA |
1394 | |
1395 | flist_for_each(entry, &filename_list) { | |
04d6530f | 1396 | struct file_name *fn; |
90426237 | 1397 | |
04d6530f JA |
1398 | fn = flist_entry(entry, struct file_name, list); |
1399 | ||
1400 | if (!strcmp(fn->filename, fname)) | |
1401 | return true; | |
90426237 JA |
1402 | } |
1403 | ||
04d6530f | 1404 | return false; |
bcbfeefa CE |
1405 | } |
1406 | ||
04d6530f | 1407 | static bool is_already_allocated(const char *fname) |
bcbfeefa | 1408 | { |
04d6530f | 1409 | bool ret; |
bcbfeefa | 1410 | |
90426237 | 1411 | fio_file_hash_lock(); |
1b2a83dc | 1412 | ret = __is_already_allocated(fname, false); |
90426237 | 1413 | fio_file_hash_unlock(); |
04d6530f | 1414 | |
90426237 JA |
1415 | return ret; |
1416 | } | |
bcbfeefa | 1417 | |
90426237 JA |
1418 | static void set_already_allocated(const char *fname) |
1419 | { | |
1420 | struct file_name *fn; | |
1421 | ||
1422 | fn = malloc(sizeof(struct file_name)); | |
1423 | fn->filename = strdup(fname); | |
1424 | ||
1425 | fio_file_hash_lock(); | |
1b2a83dc | 1426 | if (!__is_already_allocated(fname, true)) { |
90426237 JA |
1427 | flist_add_tail(&fn->list, &filename_list); |
1428 | fn = NULL; | |
bcbfeefa | 1429 | } |
90426237 | 1430 | fio_file_hash_unlock(); |
bcbfeefa | 1431 | |
90426237 JA |
1432 | if (fn) { |
1433 | free(fn->filename); | |
1434 | free(fn); | |
1435 | } | |
bcbfeefa CE |
1436 | } |
1437 | ||
190b8f0c CF |
1438 | static void free_already_allocated(void) |
1439 | { | |
bcbfeefa CE |
1440 | struct flist_head *entry, *tmp; |
1441 | struct file_name *fn; | |
1442 | ||
90426237 JA |
1443 | if (flist_empty(&filename_list)) |
1444 | return; | |
1445 | ||
1446 | fio_file_hash_lock(); | |
1447 | flist_for_each_safe(entry, tmp, &filename_list) { | |
1448 | fn = flist_entry(entry, struct file_name, list); | |
1449 | free(fn->filename); | |
1450 | flist_del(&fn->list); | |
1451 | free(fn); | |
bcbfeefa | 1452 | } |
90426237 JA |
1453 | |
1454 | fio_file_hash_unlock(); | |
bcbfeefa CE |
1455 | } |
1456 | ||
7b5cb700 JA |
1457 | static struct fio_file *alloc_new_file(struct thread_data *td) |
1458 | { | |
1459 | struct fio_file *f; | |
1460 | ||
1461 | f = smalloc(sizeof(*f)); | |
1462 | if (!f) { | |
7b5cb700 JA |
1463 | assert(0); |
1464 | return NULL; | |
1465 | } | |
1466 | ||
1467 | f->fd = -1; | |
1468 | f->shadow_fd = -1; | |
1469 | fio_file_reset(td, f); | |
1470 | return f; | |
1471 | } | |
1472 | ||
04d6530f JA |
1473 | bool exists_and_not_regfile(const char *filename) |
1474 | { | |
1475 | struct stat sb; | |
1476 | ||
1477 | if (lstat(filename, &sb) == -1) | |
1478 | return false; | |
1479 | ||
1480 | #ifndef WIN32 /* NOT Windows */ | |
1481 | if (S_ISREG(sb.st_mode)) | |
1482 | return false; | |
1483 | #else | |
1484 | /* \\.\ is the device namespace in Windows, where every file | |
1485 | * is a device node */ | |
1486 | if (S_ISREG(sb.st_mode) && strncmp(filename, "\\\\.\\", 4) != 0) | |
1487 | return false; | |
1488 | #endif | |
1489 | ||
1490 | return true; | |
1491 | } | |
1492 | ||
5903e7b7 | 1493 | int add_file(struct thread_data *td, const char *fname, int numjob, int inc) |
af52b345 | 1494 | { |
7b4e4fe5 | 1495 | int cur_files = td->files_index; |
bd0ee748 | 1496 | char file_name[PATH_MAX]; |
af52b345 | 1497 | struct fio_file *f; |
bd0ee748 | 1498 | int len = 0; |
af52b345 | 1499 | |
ee56ad50 JA |
1500 | dprint(FD_FILE, "add file %s\n", fname); |
1501 | ||
bcbfeefa | 1502 | if (td->o.directory) |
922a5be8 JA |
1503 | len = set_name_idx(file_name, PATH_MAX, td->o.directory, numjob, |
1504 | td->o.unique_filename); | |
bcbfeefa CE |
1505 | |
1506 | sprintf(file_name + len, "%s", fname); | |
1507 | ||
1508 | /* clean cloned siblings using existing files */ | |
04d6530f JA |
1509 | if (numjob && is_already_allocated(file_name) && |
1510 | !exists_and_not_regfile(fname)) | |
bcbfeefa CE |
1511 | return 0; |
1512 | ||
7b5cb700 | 1513 | f = alloc_new_file(td); |
bd0ee748 | 1514 | |
fc99bc0d | 1515 | if (td->files_size <= td->files_index) { |
1983e327 | 1516 | unsigned int new_size = td->o.nr_files + 1; |
126d65c6 | 1517 | |
fc99bc0d JA |
1518 | dprint(FD_FILE, "resize file array to %d files\n", new_size); |
1519 | ||
1520 | td->files = realloc(td->files, new_size * sizeof(f)); | |
d537c08b JM |
1521 | if (td->files == NULL) { |
1522 | log_err("fio: realloc OOM\n"); | |
1523 | assert(0); | |
1524 | } | |
d7df1d13 JA |
1525 | if (td->o.file_lock_mode != FILE_LOCK_NONE) { |
1526 | td->file_locks = realloc(td->file_locks, new_size); | |
1527 | if (!td->file_locks) { | |
1528 | log_err("fio: realloc OOM\n"); | |
1529 | assert(0); | |
1530 | } | |
1531 | td->file_locks[cur_files] = FILE_LOCK_NONE; | |
1532 | } | |
fc99bc0d JA |
1533 | td->files_size = new_size; |
1534 | } | |
8bb7679e | 1535 | td->files[cur_files] = f; |
89ac1d48 | 1536 | f->fileno = cur_files; |
126d65c6 | 1537 | |
07eb79df JA |
1538 | /* |
1539 | * init function, io engine may not be loaded yet | |
1540 | */ | |
9b87f09b | 1541 | if (td->io_ops && td_ioengine_flagged(td, FIO_DISKLESSIO)) |
07eb79df JA |
1542 | f->real_file_size = -1ULL; |
1543 | ||
f17c4392 | 1544 | f->file_name = smalloc_strdup(file_name); |
81b3c86f | 1545 | if (!f->file_name) |
c48c0be7 | 1546 | assert(0); |
0b9d69ec | 1547 | |
e3bab463 | 1548 | get_file_type(f); |
af52b345 | 1549 | |
4d4e80f2 JA |
1550 | switch (td->o.file_lock_mode) { |
1551 | case FILE_LOCK_NONE: | |
1552 | break; | |
1553 | case FILE_LOCK_READWRITE: | |
d7df1d13 | 1554 | f->rwlock = fio_rwlock_init(); |
4d4e80f2 JA |
1555 | break; |
1556 | case FILE_LOCK_EXCLUSIVE: | |
521da527 | 1557 | f->lock = fio_mutex_init(FIO_MUTEX_UNLOCKED); |
4d4e80f2 JA |
1558 | break; |
1559 | default: | |
1560 | log_err("fio: unknown lock mode: %d\n", td->o.file_lock_mode); | |
1561 | assert(0); | |
1562 | } | |
29c1349f | 1563 | |
7b4e4fe5 | 1564 | td->files_index++; |
1549441c JA |
1565 | if (f->filetype == FIO_TYPE_FILE) |
1566 | td->nr_normal_files++; | |
f29b25a3 | 1567 | |
bcbfeefa CE |
1568 | set_already_allocated(file_name); |
1569 | ||
5903e7b7 JA |
1570 | if (inc) |
1571 | td->o.nr_files++; | |
1572 | ||
5ec10eaa JA |
1573 | dprint(FD_FILE, "file %p \"%s\" added at %d\n", f, f->file_name, |
1574 | cur_files); | |
9efef3c4 | 1575 | |
f29b25a3 | 1576 | return cur_files; |
af52b345 | 1577 | } |
0ad920e7 | 1578 | |
49ffb4a2 JA |
1579 | int add_file_exclusive(struct thread_data *td, const char *fname) |
1580 | { | |
1581 | struct fio_file *f; | |
1582 | unsigned int i; | |
1583 | ||
1584 | for_each_file(td, f, i) { | |
1585 | if (!strcmp(f->file_name, fname)) | |
1586 | return i; | |
1587 | } | |
1588 | ||
5903e7b7 | 1589 | return add_file(td, fname, 0, 1); |
49ffb4a2 JA |
1590 | } |
1591 | ||
0ad920e7 JA |
1592 | void get_file(struct fio_file *f) |
1593 | { | |
8172fe97 | 1594 | dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references); |
d6aed795 | 1595 | assert(fio_file_open(f)); |
0ad920e7 JA |
1596 | f->references++; |
1597 | } | |
1598 | ||
6977bcd0 | 1599 | int put_file(struct thread_data *td, struct fio_file *f) |
0ad920e7 | 1600 | { |
98e1ac4e | 1601 | int f_ret = 0, ret = 0; |
6977bcd0 | 1602 | |
8172fe97 | 1603 | dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references); |
ee56ad50 | 1604 | |
22a57ba8 JA |
1605 | if (!fio_file_open(f)) { |
1606 | assert(f->fd == -1); | |
6977bcd0 | 1607 | return 0; |
22a57ba8 | 1608 | } |
0ad920e7 JA |
1609 | |
1610 | assert(f->references); | |
1611 | if (--f->references) | |
6977bcd0 | 1612 | return 0; |
0ad920e7 | 1613 | |
71b84caa | 1614 | if (should_fsync(td) && td->o.fsync_on_close) { |
98e1ac4e | 1615 | f_ret = fsync(f->fd); |
71b84caa JA |
1616 | if (f_ret < 0) |
1617 | f_ret = errno; | |
1618 | } | |
ebb1415f | 1619 | |
0ad920e7 | 1620 | if (td->io_ops->close_file) |
6977bcd0 | 1621 | ret = td->io_ops->close_file(td, f); |
1020a139 | 1622 | |
98e1ac4e | 1623 | if (!ret) |
a5fb461f | 1624 | ret = f_ret; |
98e1ac4e | 1625 | |
0ad920e7 | 1626 | td->nr_open_files--; |
d6aed795 | 1627 | fio_file_clear_open(f); |
22a57ba8 | 1628 | assert(f->fd == -1); |
6977bcd0 | 1629 | return ret; |
0ad920e7 | 1630 | } |
bbf6b540 | 1631 | |
4d4e80f2 | 1632 | void lock_file(struct thread_data *td, struct fio_file *f, enum fio_ddir ddir) |
b2bd2bd9 | 1633 | { |
4d4e80f2 JA |
1634 | if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE) |
1635 | return; | |
29c1349f | 1636 | |
4d4e80f2 JA |
1637 | if (td->o.file_lock_mode == FILE_LOCK_READWRITE) { |
1638 | if (ddir == DDIR_READ) | |
d7df1d13 | 1639 | fio_rwlock_read(f->rwlock); |
4d4e80f2 | 1640 | else |
d7df1d13 | 1641 | fio_rwlock_write(f->rwlock); |
4d4e80f2 JA |
1642 | } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE) |
1643 | fio_mutex_down(f->lock); | |
1644 | ||
d7df1d13 | 1645 | td->file_locks[f->fileno] = td->o.file_lock_mode; |
b2bd2bd9 JA |
1646 | } |
1647 | ||
4d4e80f2 | 1648 | void unlock_file(struct thread_data *td, struct fio_file *f) |
b2bd2bd9 | 1649 | { |
4d4e80f2 JA |
1650 | if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE) |
1651 | return; | |
4d4e80f2 | 1652 | |
d7df1d13 JA |
1653 | if (td->o.file_lock_mode == FILE_LOCK_READWRITE) |
1654 | fio_rwlock_unlock(f->rwlock); | |
1655 | else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE) | |
4d4e80f2 | 1656 | fio_mutex_up(f->lock); |
d7df1d13 JA |
1657 | |
1658 | td->file_locks[f->fileno] = FILE_LOCK_NONE; | |
b2bd2bd9 JA |
1659 | } |
1660 | ||
4d4e80f2 JA |
1661 | void unlock_file_all(struct thread_data *td, struct fio_file *f) |
1662 | { | |
f2a2803a | 1663 | if (td->o.file_lock_mode == FILE_LOCK_NONE || !td->file_locks) |
27ddbfa0 | 1664 | return; |
d7df1d13 JA |
1665 | if (td->file_locks[f->fileno] != FILE_LOCK_NONE) |
1666 | unlock_file(td, f); | |
4d4e80f2 JA |
1667 | } |
1668 | ||
bbf6b540 JA |
1669 | static int recurse_dir(struct thread_data *td, const char *dirname) |
1670 | { | |
1671 | struct dirent *dir; | |
1672 | int ret = 0; | |
1673 | DIR *D; | |
1674 | ||
1675 | D = opendir(dirname); | |
1676 | if (!D) { | |
0ddb270c JA |
1677 | char buf[FIO_VERROR_SIZE]; |
1678 | ||
98ffb8f3 | 1679 | snprintf(buf, FIO_VERROR_SIZE, "opendir(%s)", dirname); |
0ddb270c | 1680 | td_verror(td, errno, buf); |
bbf6b540 JA |
1681 | return 1; |
1682 | } | |
1683 | ||
1684 | while ((dir = readdir(D)) != NULL) { | |
1685 | char full_path[PATH_MAX]; | |
1686 | struct stat sb; | |
1687 | ||
e85b2b83 JA |
1688 | if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, "..")) |
1689 | continue; | |
96d32d51 | 1690 | |
b9fd788f | 1691 | sprintf(full_path, "%s%s%s", dirname, FIO_OS_PATH_SEPARATOR, dir->d_name); |
bbf6b540 JA |
1692 | |
1693 | if (lstat(full_path, &sb) == -1) { | |
1694 | if (errno != ENOENT) { | |
1695 | td_verror(td, errno, "stat"); | |
68ace9e0 JA |
1696 | ret = 1; |
1697 | break; | |
bbf6b540 JA |
1698 | } |
1699 | } | |
1700 | ||
1701 | if (S_ISREG(sb.st_mode)) { | |
5903e7b7 | 1702 | add_file(td, full_path, 0, 1); |
bbf6b540 JA |
1703 | continue; |
1704 | } | |
0ddb270c JA |
1705 | if (!S_ISDIR(sb.st_mode)) |
1706 | continue; | |
bbf6b540 | 1707 | |
5ec10eaa JA |
1708 | ret = recurse_dir(td, full_path); |
1709 | if (ret) | |
bbf6b540 JA |
1710 | break; |
1711 | } | |
1712 | ||
1713 | closedir(D); | |
1714 | return ret; | |
1715 | } | |
1716 | ||
1717 | int add_dir_files(struct thread_data *td, const char *path) | |
1718 | { | |
0ddb270c JA |
1719 | int ret = recurse_dir(td, path); |
1720 | ||
1721 | if (!ret) | |
1722 | log_info("fio: opendir added %d files\n", td->o.nr_files); | |
1723 | ||
1724 | return ret; | |
bbf6b540 | 1725 | } |
cade3ef4 JA |
1726 | |
1727 | void dup_files(struct thread_data *td, struct thread_data *org) | |
1728 | { | |
1729 | struct fio_file *f; | |
1730 | unsigned int i; | |
9efef3c4 JA |
1731 | |
1732 | dprint(FD_FILE, "dup files: %d\n", org->files_index); | |
cade3ef4 JA |
1733 | |
1734 | if (!org->files) | |
1735 | return; | |
1736 | ||
9efef3c4 | 1737 | td->files = malloc(org->files_index * sizeof(f)); |
cade3ef4 | 1738 | |
d7df1d13 JA |
1739 | if (td->o.file_lock_mode != FILE_LOCK_NONE) |
1740 | td->file_locks = malloc(org->files_index); | |
1741 | ||
9efef3c4 | 1742 | for_each_file(org, f, i) { |
b0fe421a JA |
1743 | struct fio_file *__f; |
1744 | ||
7b5cb700 | 1745 | __f = alloc_new_file(td); |
0b9d69ec | 1746 | |
bc3456fa | 1747 | if (f->file_name) { |
f17c4392 | 1748 | __f->file_name = smalloc_strdup(f->file_name); |
81b3c86f | 1749 | if (!__f->file_name) |
c48c0be7 | 1750 | assert(0); |
0b9d69ec | 1751 | |
bc3456fa AC |
1752 | __f->filetype = f->filetype; |
1753 | } | |
b0fe421a | 1754 | |
67ad9242 JA |
1755 | if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE) |
1756 | __f->lock = f->lock; | |
1757 | else if (td->o.file_lock_mode == FILE_LOCK_READWRITE) | |
1758 | __f->rwlock = f->rwlock; | |
1759 | ||
b0fe421a | 1760 | td->files[i] = __f; |
cade3ef4 JA |
1761 | } |
1762 | } | |
f29b25a3 JA |
1763 | |
1764 | /* | |
1765 | * Returns the index that matches the filename, or -1 if not there | |
1766 | */ | |
1767 | int get_fileno(struct thread_data *td, const char *fname) | |
1768 | { | |
1769 | struct fio_file *f; | |
1770 | unsigned int i; | |
1771 | ||
1772 | for_each_file(td, f, i) | |
1773 | if (!strcmp(f->file_name, fname)) | |
1774 | return i; | |
1775 | ||
1776 | return -1; | |
1777 | } | |
1778 | ||
1779 | /* | |
1780 | * For log usage, where we add/open/close files automatically | |
1781 | */ | |
1782 | void free_release_files(struct thread_data *td) | |
1783 | { | |
1784 | close_files(td); | |
66ee4002 JA |
1785 | td->o.nr_files = 0; |
1786 | td->o.open_files = 0; | |
f29b25a3 JA |
1787 | td->files_index = 0; |
1788 | td->nr_normal_files = 0; | |
1789 | } | |
33c48814 JA |
1790 | |
1791 | void fio_file_reset(struct thread_data *td, struct fio_file *f) | |
1792 | { | |
f1dfb668 JA |
1793 | int i; |
1794 | ||
1795 | for (i = 0; i < DDIR_RWDIR_CNT; i++) { | |
1796 | f->last_pos[i] = f->file_offset; | |
1797 | f->last_start[i] = -1ULL; | |
1798 | } | |
1799 | ||
967d1b63 | 1800 | if (fio_file_axmap(f)) |
33c48814 | 1801 | axmap_reset(f->io_axmap); |
967d1b63 | 1802 | else if (fio_file_lfsr(f)) |
33c48814 JA |
1803 | lfsr_reset(&f->lfsr, td->rand_seeds[FIO_RAND_BLOCK_OFF]); |
1804 | } | |
002fe734 | 1805 | |
747b3105 | 1806 | bool fio_files_done(struct thread_data *td) |
002fe734 JA |
1807 | { |
1808 | struct fio_file *f; | |
1809 | unsigned int i; | |
1810 | ||
1811 | for_each_file(td, f, i) | |
1812 | if (!fio_file_done(f)) | |
747b3105 | 1813 | return false; |
002fe734 | 1814 | |
747b3105 | 1815 | return true; |
002fe734 | 1816 | } |
bcbfeefa CE |
1817 | |
1818 | /* free memory used in initialization phase only */ | |
190b8f0c CF |
1819 | void filesetup_mem_free(void) |
1820 | { | |
bcbfeefa CE |
1821 | free_already_allocated(); |
1822 | } |