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