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