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" | |
11 | #include "os.h" | |
12 | ||
25205e97 JA |
13 | /* |
14 | * Check if the file exists and it's large enough. | |
15 | */ | |
16 | static int file_ok(struct thread_data *td, struct fio_file *f) | |
53cdc686 | 17 | { |
b2a15192 | 18 | struct stat st; |
53cdc686 | 19 | |
af52b345 | 20 | if (f->filetype != FIO_TYPE_FILE || |
b5af8293 | 21 | (td->io_ops->flags & FIO_DISKLESSIO)) |
b2a15192 JA |
22 | return 0; |
23 | ||
fa01d139 | 24 | if (lstat(f->file_name, &st) == -1) |
25205e97 | 25 | return 1; |
fa01d139 JA |
26 | |
27 | /* | |
28 | * if it's a special file, size is always ok for now | |
29 | */ | |
30 | if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) | |
31 | return 0; | |
32 | if (st.st_size < (off_t) f->file_size) | |
25205e97 JA |
33 | return 1; |
34 | ||
35 | return 0; | |
36 | } | |
37 | ||
38 | static int create_file(struct thread_data *td, struct fio_file *f) | |
39 | { | |
40 | unsigned long long left; | |
41 | unsigned int bs; | |
42 | char *b; | |
43 | int r; | |
b2a15192 | 44 | |
53cdc686 JA |
45 | f->fd = open(f->file_name, O_WRONLY | O_CREAT | O_TRUNC, 0644); |
46 | if (f->fd < 0) { | |
e1161c32 | 47 | td_verror(td, errno, "open"); |
53cdc686 JA |
48 | return 1; |
49 | } | |
50 | ||
51 | if (ftruncate(f->fd, f->file_size) == -1) { | |
e1161c32 | 52 | td_verror(td, errno, "ftruncate"); |
53cdc686 JA |
53 | goto err; |
54 | } | |
55 | ||
40f8298c | 56 | if (posix_fallocate(f->fd, 0, f->file_size) < 0) { |
e1161c32 | 57 | td_verror(td, errno, "posix_fallocate"); |
40f8298c JA |
58 | goto err; |
59 | } | |
60 | ||
a00735e6 JA |
61 | b = malloc(td->max_bs[DDIR_WRITE]); |
62 | memset(b, 0, td->max_bs[DDIR_WRITE]); | |
53cdc686 JA |
63 | |
64 | left = f->file_size; | |
65 | while (left && !td->terminate) { | |
a00735e6 | 66 | bs = td->max_bs[DDIR_WRITE]; |
53cdc686 JA |
67 | if (bs > left) |
68 | bs = left; | |
69 | ||
70 | r = write(f->fd, b, bs); | |
71 | ||
72 | if (r == (int) bs) { | |
73 | left -= bs; | |
74 | continue; | |
75 | } else { | |
76 | if (r < 0) | |
e1161c32 | 77 | td_verror(td, errno, "write"); |
53cdc686 | 78 | else |
e1161c32 | 79 | td_verror(td, EIO, "write"); |
53cdc686 JA |
80 | |
81 | break; | |
82 | } | |
83 | } | |
84 | ||
85 | if (td->terminate) | |
86 | unlink(f->file_name); | |
87 | else if (td->create_fsync) | |
88 | fsync(f->fd); | |
89 | ||
90 | free(b); | |
91 | close(f->fd); | |
92 | f->fd = -1; | |
93 | return 0; | |
94 | err: | |
95 | close(f->fd); | |
96 | f->fd = -1; | |
97 | return 1; | |
98 | } | |
99 | ||
100 | static int create_files(struct thread_data *td) | |
101 | { | |
102 | struct fio_file *f; | |
af52b345 | 103 | int err, need_create, can_extend; |
96d32d51 | 104 | unsigned long long total_file_size; |
af52b345 | 105 | unsigned int i; |
53cdc686 | 106 | |
96d32d51 | 107 | total_file_size = td->total_file_size; |
1549441c | 108 | for_each_file(td, f, i) { |
96d32d51 JA |
109 | unsigned long long s; |
110 | ||
111 | f->file_offset = td->start_offset; | |
112 | ||
1549441c JA |
113 | if (f->filetype != FIO_TYPE_FILE) |
114 | continue; | |
115 | ||
96d32d51 JA |
116 | if (f->flags & FIO_FILE_EXISTS) { |
117 | s = f->file_size; | |
118 | if (s > total_file_size) | |
119 | s = total_file_size; | |
120 | ||
121 | total_file_size -= s; | |
122 | } | |
1549441c | 123 | } |
f697125a | 124 | |
53cdc686 JA |
125 | /* |
126 | * unless specifically asked for overwrite, let normal io extend it | |
127 | */ | |
0263882a JA |
128 | can_extend = !td->overwrite && !(td->io_ops->flags & FIO_NOEXTEND); |
129 | if (can_extend) | |
53cdc686 | 130 | return 0; |
53cdc686 | 131 | |
25205e97 | 132 | need_create = 0; |
af52b345 JA |
133 | for_each_file(td, f, i) { |
134 | int file_there; | |
135 | ||
136 | if (f->filetype != FIO_TYPE_FILE) | |
137 | continue; | |
96d32d51 JA |
138 | if (f->flags & FIO_FILE_EXISTS) |
139 | continue; | |
140 | ||
141 | f->file_size = total_file_size / td->nr_normal_files; | |
eff6861d | 142 | |
af52b345 | 143 | file_there = !file_ok(td, f); |
eff6861d | 144 | |
af52b345 JA |
145 | if (file_there && td_write(td) && !td->overwrite) { |
146 | unlink(f->file_name); | |
147 | file_there = 0; | |
eff6861d | 148 | } |
af52b345 JA |
149 | |
150 | need_create += !file_there; | |
eff6861d | 151 | } |
25205e97 | 152 | |
25205e97 JA |
153 | if (!need_create) |
154 | return 0; | |
155 | ||
f1027063 JA |
156 | if (!td->total_file_size) { |
157 | log_err("Need size for create\n"); | |
e1161c32 | 158 | td_verror(td, EINVAL, "file_size"); |
f1027063 JA |
159 | return 1; |
160 | } | |
161 | ||
53cdc686 | 162 | temp_stall_ts = 1; |
1e97cce9 | 163 | fprintf(f_out, "%s: Laying out IO file(s) (%u x %LuMiB == %LuMiB)\n", |
1549441c JA |
164 | td->name, td->nr_normal_files, |
165 | (td->total_file_size >> 20) / td->nr_normal_files, | |
25205e97 | 166 | td->total_file_size >> 20); |
53cdc686 JA |
167 | |
168 | err = 0; | |
169 | for_each_file(td, f, i) { | |
9b031dc8 JA |
170 | /* |
171 | * Only unlink files that we created. | |
172 | */ | |
f11bd94d | 173 | f->flags &= ~FIO_FILE_UNLINK; |
25205e97 | 174 | if (file_ok(td, f)) { |
f11bd94d JA |
175 | if (td->unlink) |
176 | f->flags |= FIO_FILE_UNLINK; | |
177 | ||
25205e97 JA |
178 | err = create_file(td, f); |
179 | if (err) | |
180 | break; | |
181 | } | |
53cdc686 JA |
182 | } |
183 | ||
184 | temp_stall_ts = 0; | |
185 | return err; | |
186 | } | |
187 | ||
188 | static int file_size(struct thread_data *td, struct fio_file *f) | |
189 | { | |
190 | struct stat st; | |
191 | ||
192 | if (td->overwrite) { | |
193 | if (fstat(f->fd, &st) == -1) { | |
e1161c32 | 194 | td_verror(td, errno, "fstat"); |
53cdc686 JA |
195 | return 1; |
196 | } | |
197 | ||
198 | f->real_file_size = st.st_size; | |
199 | ||
200 | if (!f->file_size || f->file_size > f->real_file_size) | |
201 | f->file_size = f->real_file_size; | |
745508dd JA |
202 | } else |
203 | f->real_file_size = f->file_size; | |
53cdc686 | 204 | |
53cdc686 JA |
205 | return 0; |
206 | } | |
207 | ||
208 | static int bdev_size(struct thread_data *td, struct fio_file *f) | |
209 | { | |
210 | unsigned long long bytes; | |
211 | int r; | |
212 | ||
213 | r = blockdev_size(f->fd, &bytes); | |
214 | if (r) { | |
e1161c32 | 215 | td_verror(td, r, "blockdev_size"); |
53cdc686 JA |
216 | return 1; |
217 | } | |
218 | ||
219 | f->real_file_size = bytes; | |
220 | ||
221 | /* | |
222 | * no extend possibilities, so limit size to device size if too large | |
223 | */ | |
224 | if (!f->file_size || f->file_size > f->real_file_size) | |
225 | f->file_size = f->real_file_size; | |
226 | ||
227 | f->file_size -= f->file_offset; | |
228 | return 0; | |
229 | } | |
230 | ||
231 | static int get_file_size(struct thread_data *td, struct fio_file *f) | |
232 | { | |
233 | int ret = 0; | |
234 | ||
96d32d51 JA |
235 | if (f->filetype == FIO_TYPE_FILE) { |
236 | if (!(f->flags & FIO_FILE_EXISTS)) | |
237 | ret = file_size(td, f); | |
238 | } else if (f->filetype == FIO_TYPE_BD) | |
53cdc686 JA |
239 | ret = bdev_size(td, f); |
240 | else | |
241 | f->real_file_size = -1; | |
242 | ||
243 | if (ret) | |
244 | return ret; | |
245 | ||
246 | if (f->file_offset > f->real_file_size) { | |
247 | log_err("%s: offset extends end (%Lu > %Lu)\n", td->name, f->file_offset, f->real_file_size); | |
248 | return 1; | |
249 | } | |
250 | ||
53cdc686 JA |
251 | return 0; |
252 | } | |
253 | ||
e5b401d4 JA |
254 | int file_invalidate_cache(struct thread_data *td, struct fio_file *f) |
255 | { | |
256 | int ret = 0; | |
257 | ||
da86ccb6 | 258 | if (td->odirect) |
b5af8293 JA |
259 | return 0; |
260 | ||
e5b401d4 JA |
261 | /* |
262 | * FIXME: add blockdev flushing too | |
263 | */ | |
b5af8293 | 264 | if (f->mmap) |
e5b401d4 | 265 | ret = madvise(f->mmap, f->file_size, MADV_DONTNEED); |
af52b345 | 266 | else if (f->filetype == FIO_TYPE_FILE) { |
b5af8293 | 267 | ret = fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_DONTNEED); |
af52b345 | 268 | } else if (f->filetype == FIO_TYPE_BD) { |
b5af8293 | 269 | ret = blockdev_invalidate_cache(f->fd); |
af52b345 | 270 | } else if (f->filetype == FIO_TYPE_CHAR) |
e5b401d4 JA |
271 | ret = 0; |
272 | ||
273 | if (ret < 0) { | |
e1161c32 | 274 | td_verror(td, errno, "invalidate_cache"); |
e5b401d4 JA |
275 | return 1; |
276 | } | |
277 | ||
ad2da605 | 278 | return ret; |
e5b401d4 JA |
279 | } |
280 | ||
b5af8293 | 281 | void generic_close_file(struct thread_data fio_unused *td, struct fio_file *f) |
53cdc686 | 282 | { |
b5af8293 JA |
283 | close(f->fd); |
284 | f->fd = -1; | |
53cdc686 JA |
285 | } |
286 | ||
b5af8293 | 287 | int generic_open_file(struct thread_data *td, struct fio_file *f) |
53cdc686 | 288 | { |
53cdc686 JA |
289 | int flags = 0; |
290 | ||
2fd233b7 JA |
291 | if (td->odirect) |
292 | flags |= OS_O_DIRECT; | |
293 | if (td->sync_io) | |
294 | flags |= O_SYNC; | |
53cdc686 | 295 | |
2fd233b7 JA |
296 | if (td_write(td) || td_rw(td)) { |
297 | flags |= O_RDWR; | |
53cdc686 | 298 | |
af52b345 | 299 | if (f->filetype == FIO_TYPE_FILE) |
2fd233b7 | 300 | flags |= O_CREAT; |
2fd233b7 | 301 | |
b5af8293 | 302 | f->fd = open(f->file_name, flags, 0600); |
2fd233b7 | 303 | } else { |
af52b345 | 304 | if (f->filetype == FIO_TYPE_CHAR) |
2fd233b7 JA |
305 | flags |= O_RDWR; |
306 | else | |
307 | flags |= O_RDONLY; | |
308 | ||
b5af8293 | 309 | f->fd = open(f->file_name, flags); |
53cdc686 JA |
310 | } |
311 | ||
312 | if (f->fd == -1) { | |
e1161c32 JA |
313 | int __e = errno; |
314 | ||
315 | td_verror(td, __e, "open"); | |
316 | if (__e == EINVAL && td->odirect) | |
9d80e114 | 317 | log_err("fio: destination does not support O_DIRECT\n"); |
8ab53873 JA |
318 | if (__e == EMFILE) |
319 | log_err("fio: try reducing/setting openfiles (failed at %u of %u)\n", td->nr_open_files, td->nr_files); | |
53cdc686 JA |
320 | return 1; |
321 | } | |
322 | ||
b5af8293 JA |
323 | if (get_file_size(td, f)) |
324 | goto err; | |
325 | ||
0263882a | 326 | if (td->invalidate_cache && file_invalidate_cache(td, f)) |
b5af8293 JA |
327 | goto err; |
328 | ||
329 | if (!td_random(td)) { | |
330 | if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_SEQUENTIAL) < 0) { | |
331 | td_verror(td, errno, "fadvise"); | |
332 | goto err; | |
333 | } | |
334 | } else { | |
335 | if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_RANDOM) < 0) { | |
336 | td_verror(td, errno, "fadvise"); | |
337 | goto err; | |
338 | } | |
bdb4e2e9 | 339 | } |
53cdc686 JA |
340 | |
341 | return 0; | |
b5af8293 JA |
342 | err: |
343 | close(f->fd); | |
344 | return 1; | |
345 | } | |
346 | ||
21972cde JA |
347 | int open_files(struct thread_data *td) |
348 | { | |
349 | struct fio_file *f; | |
af52b345 JA |
350 | unsigned int i; |
351 | int err = 0; | |
21972cde JA |
352 | |
353 | for_each_file(td, f, i) { | |
b5af8293 | 354 | err = td_io_open_file(td, f); |
21972cde JA |
355 | if (err) |
356 | break; | |
b5af8293 JA |
357 | |
358 | if (td->open_files == td->nr_open_files) | |
359 | break; | |
21972cde JA |
360 | } |
361 | ||
7abf833d JA |
362 | if (!err) |
363 | return 0; | |
364 | ||
bdb4e2e9 | 365 | for_each_file(td, f, i) |
b5af8293 | 366 | td_io_close_file(td, f); |
7abf833d | 367 | |
21972cde JA |
368 | return err; |
369 | } | |
370 | ||
53cdc686 JA |
371 | int setup_files(struct thread_data *td) |
372 | { | |
373 | struct fio_file *f; | |
af52b345 JA |
374 | unsigned int i; |
375 | int err; | |
53cdc686 JA |
376 | |
377 | /* | |
378 | * if ioengine defines a setup() method, it's responsible for | |
379 | * setting up everything in the td->files[] area. | |
380 | */ | |
381 | if (td->io_ops->setup) | |
382 | return td->io_ops->setup(td); | |
383 | ||
384 | if (create_files(td)) | |
385 | return 1; | |
386 | ||
21972cde | 387 | err = open_files(td); |
f1027063 JA |
388 | if (err) |
389 | return err; | |
390 | ||
0a7eb121 JA |
391 | /* |
392 | * Recalculate the total file size now that files are set up. | |
393 | */ | |
394 | td->total_file_size = 0; | |
395 | for_each_file(td, f, i) | |
396 | td->total_file_size += f->file_size; | |
397 | ||
f1027063 | 398 | td->io_size = td->total_file_size; |
53cdc686 JA |
399 | if (td->io_size == 0) { |
400 | log_err("%s: no io blocks\n", td->name); | |
e1161c32 | 401 | td_verror(td, EINVAL, "total_file_size"); |
53cdc686 JA |
402 | return 1; |
403 | } | |
404 | ||
405 | if (!td->zone_size) | |
406 | td->zone_size = td->io_size; | |
407 | ||
408 | td->total_io_size = td->io_size * td->loops; | |
409 | ||
bdb4e2e9 | 410 | for_each_file(td, f, i) |
b5af8293 | 411 | td_io_close_file(td, f); |
21972cde JA |
412 | |
413 | return err; | |
53cdc686 JA |
414 | } |
415 | ||
416 | void close_files(struct thread_data *td) | |
417 | { | |
0ab8db89 | 418 | struct fio_file *f; |
af52b345 | 419 | unsigned int i; |
53cdc686 | 420 | |
0ab8db89 | 421 | for_each_file(td, f, i) { |
1315a68a | 422 | if (!f->file_name && (f->flags & FIO_FILE_UNLINK) && |
af52b345 | 423 | f->filetype == FIO_TYPE_FILE) { |
132ad46d | 424 | unlink(f->file_name); |
1315a68a | 425 | free(f->file_name); |
132ad46d JA |
426 | f->file_name = NULL; |
427 | } | |
bdb4e2e9 | 428 | |
b5af8293 | 429 | td_io_close_file(td, f); |
b3dc7f07 JA |
430 | |
431 | if (f->file_map) | |
432 | free(f->file_map); | |
53cdc686 | 433 | } |
b4a6a59a | 434 | |
132ad46d | 435 | td->filename = NULL; |
b4a6a59a JA |
436 | free(td->files); |
437 | td->files = NULL; | |
438 | td->nr_files = 0; | |
53cdc686 | 439 | } |
af52b345 | 440 | |
e3bab463 | 441 | static void get_file_type(struct fio_file *f) |
af52b345 JA |
442 | { |
443 | struct stat sb; | |
444 | ||
445 | f->filetype = FIO_TYPE_FILE; | |
446 | ||
e3bab463 | 447 | if (!lstat(f->file_name, &sb)) { |
96d32d51 JA |
448 | f->flags |= FIO_FILE_EXISTS; |
449 | ||
af52b345 JA |
450 | if (S_ISBLK(sb.st_mode)) |
451 | f->filetype = FIO_TYPE_BD; | |
452 | else if (S_ISCHR(sb.st_mode)) | |
453 | f->filetype = FIO_TYPE_CHAR; | |
96d32d51 JA |
454 | else { |
455 | /* | |
456 | * might as well do this here, and save a stat later on | |
457 | */ | |
458 | f->real_file_size = sb.st_size; | |
459 | f->file_size = f->real_file_size; | |
460 | } | |
af52b345 JA |
461 | } |
462 | } | |
463 | ||
464 | void add_file(struct thread_data *td, const char *fname) | |
465 | { | |
7b4e4fe5 | 466 | int cur_files = td->files_index; |
af52b345 JA |
467 | struct fio_file *f; |
468 | ||
469 | td->files = realloc(td->files, (cur_files + 1) * sizeof(*f)); | |
470 | ||
471 | f = &td->files[cur_files]; | |
472 | memset(f, 0, sizeof(*f)); | |
473 | f->fd = -1; | |
cae61953 | 474 | f->file_name = strdup(fname); |
af52b345 | 475 | |
e3bab463 | 476 | get_file_type(f); |
af52b345 | 477 | |
7b4e4fe5 | 478 | td->files_index++; |
1549441c JA |
479 | if (f->filetype == FIO_TYPE_FILE) |
480 | td->nr_normal_files++; | |
af52b345 | 481 | } |
0ad920e7 JA |
482 | |
483 | void get_file(struct fio_file *f) | |
484 | { | |
485 | f->references++; | |
486 | } | |
487 | ||
488 | void put_file(struct thread_data *td, struct fio_file *f) | |
489 | { | |
490 | if (!(f->flags & FIO_FILE_OPEN)) | |
491 | return; | |
492 | ||
493 | assert(f->references); | |
494 | if (--f->references) | |
495 | return; | |
496 | ||
ebb1415f JA |
497 | if (should_fsync(td) && td->fsync_on_close) |
498 | fsync(f->fd); | |
499 | ||
0ad920e7 JA |
500 | if (td->io_ops->close_file) |
501 | td->io_ops->close_file(td, f); | |
502 | td->nr_open_files--; | |
503 | f->flags &= ~FIO_FILE_OPEN; | |
504 | } | |
bbf6b540 JA |
505 | |
506 | static int recurse_dir(struct thread_data *td, const char *dirname) | |
507 | { | |
508 | struct dirent *dir; | |
509 | int ret = 0; | |
510 | DIR *D; | |
511 | ||
512 | D = opendir(dirname); | |
513 | if (!D) { | |
514 | td_verror(td, errno, "opendir"); | |
515 | return 1; | |
516 | } | |
517 | ||
518 | while ((dir = readdir(D)) != NULL) { | |
519 | char full_path[PATH_MAX]; | |
520 | struct stat sb; | |
521 | ||
e85b2b83 JA |
522 | if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, "..")) |
523 | continue; | |
96d32d51 | 524 | |
bbf6b540 JA |
525 | sprintf(full_path, "%s/%s", dirname, dir->d_name); |
526 | ||
527 | if (lstat(full_path, &sb) == -1) { | |
528 | if (errno != ENOENT) { | |
529 | td_verror(td, errno, "stat"); | |
530 | return 1; | |
531 | } | |
532 | } | |
533 | ||
534 | if (S_ISREG(sb.st_mode)) { | |
535 | add_file(td, full_path); | |
536 | td->nr_files++; | |
537 | continue; | |
538 | } | |
539 | ||
bbf6b540 JA |
540 | if ((ret = recurse_dir(td, full_path)) != 0) |
541 | break; | |
542 | } | |
543 | ||
544 | closedir(D); | |
545 | return ret; | |
546 | } | |
547 | ||
548 | int add_dir_files(struct thread_data *td, const char *path) | |
549 | { | |
550 | return recurse_dir(td, path); | |
551 | } |