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