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