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