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