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