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