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