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