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 | |
1549441c JA |
104 | for_each_file(td, f, i) { |
105 | if (f->filetype != FIO_TYPE_FILE) | |
106 | continue; | |
107 | ||
108 | f->file_size = td->total_file_size / td->nr_normal_files; | |
109 | f->file_offset = td->start_offset; | |
110 | } | |
f697125a | 111 | |
53cdc686 JA |
112 | /* |
113 | * unless specifically asked for overwrite, let normal io extend it | |
114 | */ | |
0263882a JA |
115 | can_extend = !td->overwrite && !(td->io_ops->flags & FIO_NOEXTEND); |
116 | if (can_extend) | |
53cdc686 | 117 | return 0; |
53cdc686 | 118 | |
25205e97 | 119 | need_create = 0; |
af52b345 JA |
120 | for_each_file(td, f, i) { |
121 | int file_there; | |
122 | ||
123 | if (f->filetype != FIO_TYPE_FILE) | |
124 | continue; | |
eff6861d | 125 | |
af52b345 | 126 | file_there = !file_ok(td, f); |
eff6861d | 127 | |
af52b345 JA |
128 | if (file_there && td_write(td) && !td->overwrite) { |
129 | unlink(f->file_name); | |
130 | file_there = 0; | |
eff6861d | 131 | } |
af52b345 JA |
132 | |
133 | need_create += !file_there; | |
eff6861d | 134 | } |
25205e97 | 135 | |
25205e97 JA |
136 | if (!need_create) |
137 | return 0; | |
138 | ||
f1027063 JA |
139 | if (!td->total_file_size) { |
140 | log_err("Need size for create\n"); | |
e1161c32 | 141 | td_verror(td, EINVAL, "file_size"); |
f1027063 JA |
142 | return 1; |
143 | } | |
144 | ||
53cdc686 | 145 | temp_stall_ts = 1; |
1e97cce9 | 146 | fprintf(f_out, "%s: Laying out IO file(s) (%u x %LuMiB == %LuMiB)\n", |
1549441c JA |
147 | td->name, td->nr_normal_files, |
148 | (td->total_file_size >> 20) / td->nr_normal_files, | |
25205e97 | 149 | td->total_file_size >> 20); |
53cdc686 JA |
150 | |
151 | err = 0; | |
152 | for_each_file(td, f, i) { | |
9b031dc8 JA |
153 | /* |
154 | * Only unlink files that we created. | |
155 | */ | |
f11bd94d | 156 | f->flags &= ~FIO_FILE_UNLINK; |
25205e97 | 157 | if (file_ok(td, f)) { |
f11bd94d JA |
158 | if (td->unlink) |
159 | f->flags |= FIO_FILE_UNLINK; | |
160 | ||
25205e97 JA |
161 | err = create_file(td, f); |
162 | if (err) | |
163 | break; | |
164 | } | |
53cdc686 JA |
165 | } |
166 | ||
167 | temp_stall_ts = 0; | |
168 | return err; | |
169 | } | |
170 | ||
171 | static int file_size(struct thread_data *td, struct fio_file *f) | |
172 | { | |
173 | struct stat st; | |
174 | ||
175 | if (td->overwrite) { | |
176 | if (fstat(f->fd, &st) == -1) { | |
e1161c32 | 177 | td_verror(td, errno, "fstat"); |
53cdc686 JA |
178 | return 1; |
179 | } | |
180 | ||
181 | f->real_file_size = st.st_size; | |
182 | ||
183 | if (!f->file_size || f->file_size > f->real_file_size) | |
184 | f->file_size = f->real_file_size; | |
745508dd JA |
185 | } else |
186 | f->real_file_size = f->file_size; | |
53cdc686 | 187 | |
53cdc686 JA |
188 | return 0; |
189 | } | |
190 | ||
191 | static int bdev_size(struct thread_data *td, struct fio_file *f) | |
192 | { | |
193 | unsigned long long bytes; | |
194 | int r; | |
195 | ||
196 | r = blockdev_size(f->fd, &bytes); | |
197 | if (r) { | |
e1161c32 | 198 | td_verror(td, r, "blockdev_size"); |
53cdc686 JA |
199 | return 1; |
200 | } | |
201 | ||
202 | f->real_file_size = bytes; | |
203 | ||
204 | /* | |
205 | * no extend possibilities, so limit size to device size if too large | |
206 | */ | |
207 | if (!f->file_size || f->file_size > f->real_file_size) | |
208 | f->file_size = f->real_file_size; | |
209 | ||
210 | f->file_size -= f->file_offset; | |
211 | return 0; | |
212 | } | |
213 | ||
214 | static int get_file_size(struct thread_data *td, struct fio_file *f) | |
215 | { | |
216 | int ret = 0; | |
217 | ||
af52b345 | 218 | if (f->filetype == FIO_TYPE_FILE) |
53cdc686 | 219 | ret = file_size(td, f); |
af52b345 | 220 | else if (f->filetype == FIO_TYPE_BD) |
53cdc686 JA |
221 | ret = bdev_size(td, f); |
222 | else | |
223 | f->real_file_size = -1; | |
224 | ||
225 | if (ret) | |
226 | return ret; | |
227 | ||
228 | if (f->file_offset > f->real_file_size) { | |
229 | log_err("%s: offset extends end (%Lu > %Lu)\n", td->name, f->file_offset, f->real_file_size); | |
230 | return 1; | |
231 | } | |
232 | ||
53cdc686 JA |
233 | return 0; |
234 | } | |
235 | ||
e5b401d4 JA |
236 | int file_invalidate_cache(struct thread_data *td, struct fio_file *f) |
237 | { | |
238 | int ret = 0; | |
239 | ||
da86ccb6 | 240 | if (td->odirect) |
b5af8293 JA |
241 | return 0; |
242 | ||
e5b401d4 JA |
243 | /* |
244 | * FIXME: add blockdev flushing too | |
245 | */ | |
b5af8293 | 246 | if (f->mmap) |
e5b401d4 | 247 | ret = madvise(f->mmap, f->file_size, MADV_DONTNEED); |
af52b345 | 248 | else if (f->filetype == FIO_TYPE_FILE) { |
b5af8293 | 249 | ret = fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_DONTNEED); |
af52b345 | 250 | } else if (f->filetype == FIO_TYPE_BD) { |
b5af8293 | 251 | ret = blockdev_invalidate_cache(f->fd); |
af52b345 | 252 | } else if (f->filetype == FIO_TYPE_CHAR) |
e5b401d4 JA |
253 | ret = 0; |
254 | ||
255 | if (ret < 0) { | |
e1161c32 | 256 | td_verror(td, errno, "invalidate_cache"); |
e5b401d4 JA |
257 | return 1; |
258 | } | |
259 | ||
ad2da605 | 260 | return ret; |
e5b401d4 JA |
261 | } |
262 | ||
b5af8293 | 263 | void generic_close_file(struct thread_data fio_unused *td, struct fio_file *f) |
53cdc686 | 264 | { |
b5af8293 JA |
265 | close(f->fd); |
266 | f->fd = -1; | |
53cdc686 JA |
267 | } |
268 | ||
b5af8293 | 269 | int generic_open_file(struct thread_data *td, struct fio_file *f) |
53cdc686 | 270 | { |
53cdc686 JA |
271 | int flags = 0; |
272 | ||
2fd233b7 JA |
273 | if (td->odirect) |
274 | flags |= OS_O_DIRECT; | |
275 | if (td->sync_io) | |
276 | flags |= O_SYNC; | |
53cdc686 | 277 | |
2fd233b7 JA |
278 | if (td_write(td) || td_rw(td)) { |
279 | flags |= O_RDWR; | |
53cdc686 | 280 | |
af52b345 | 281 | if (f->filetype == FIO_TYPE_FILE) |
2fd233b7 | 282 | flags |= O_CREAT; |
2fd233b7 | 283 | |
b5af8293 | 284 | f->fd = open(f->file_name, flags, 0600); |
2fd233b7 | 285 | } else { |
af52b345 | 286 | if (f->filetype == FIO_TYPE_CHAR) |
2fd233b7 JA |
287 | flags |= O_RDWR; |
288 | else | |
289 | flags |= O_RDONLY; | |
290 | ||
b5af8293 | 291 | f->fd = open(f->file_name, flags); |
53cdc686 JA |
292 | } |
293 | ||
294 | if (f->fd == -1) { | |
e1161c32 JA |
295 | int __e = errno; |
296 | ||
297 | td_verror(td, __e, "open"); | |
298 | if (__e == EINVAL && td->odirect) | |
9d80e114 | 299 | log_err("fio: destination does not support O_DIRECT\n"); |
53cdc686 JA |
300 | return 1; |
301 | } | |
302 | ||
b5af8293 JA |
303 | if (get_file_size(td, f)) |
304 | goto err; | |
305 | ||
0263882a | 306 | if (td->invalidate_cache && file_invalidate_cache(td, f)) |
b5af8293 JA |
307 | goto err; |
308 | ||
309 | if (!td_random(td)) { | |
310 | if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_SEQUENTIAL) < 0) { | |
311 | td_verror(td, errno, "fadvise"); | |
312 | goto err; | |
313 | } | |
314 | } else { | |
315 | if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_RANDOM) < 0) { | |
316 | td_verror(td, errno, "fadvise"); | |
317 | goto err; | |
318 | } | |
bdb4e2e9 | 319 | } |
53cdc686 JA |
320 | |
321 | return 0; | |
b5af8293 JA |
322 | err: |
323 | close(f->fd); | |
324 | return 1; | |
325 | } | |
326 | ||
21972cde JA |
327 | int open_files(struct thread_data *td) |
328 | { | |
329 | struct fio_file *f; | |
af52b345 JA |
330 | unsigned int i; |
331 | int err = 0; | |
21972cde JA |
332 | |
333 | for_each_file(td, f, i) { | |
b5af8293 | 334 | err = td_io_open_file(td, f); |
21972cde JA |
335 | if (err) |
336 | break; | |
b5af8293 JA |
337 | |
338 | if (td->open_files == td->nr_open_files) | |
339 | break; | |
21972cde JA |
340 | } |
341 | ||
7abf833d JA |
342 | if (!err) |
343 | return 0; | |
344 | ||
bdb4e2e9 | 345 | for_each_file(td, f, i) |
b5af8293 | 346 | td_io_close_file(td, f); |
7abf833d | 347 | |
21972cde JA |
348 | return err; |
349 | } | |
350 | ||
53cdc686 JA |
351 | int setup_files(struct thread_data *td) |
352 | { | |
353 | struct fio_file *f; | |
af52b345 JA |
354 | unsigned int i; |
355 | int err; | |
53cdc686 JA |
356 | |
357 | /* | |
358 | * if ioengine defines a setup() method, it's responsible for | |
359 | * setting up everything in the td->files[] area. | |
360 | */ | |
361 | if (td->io_ops->setup) | |
362 | return td->io_ops->setup(td); | |
363 | ||
364 | if (create_files(td)) | |
365 | return 1; | |
366 | ||
21972cde | 367 | err = open_files(td); |
f1027063 JA |
368 | if (err) |
369 | return err; | |
370 | ||
0a7eb121 JA |
371 | /* |
372 | * Recalculate the total file size now that files are set up. | |
373 | */ | |
374 | td->total_file_size = 0; | |
375 | for_each_file(td, f, i) | |
376 | td->total_file_size += f->file_size; | |
377 | ||
f1027063 | 378 | td->io_size = td->total_file_size; |
53cdc686 JA |
379 | if (td->io_size == 0) { |
380 | log_err("%s: no io blocks\n", td->name); | |
e1161c32 | 381 | td_verror(td, EINVAL, "total_file_size"); |
53cdc686 JA |
382 | return 1; |
383 | } | |
384 | ||
385 | if (!td->zone_size) | |
386 | td->zone_size = td->io_size; | |
387 | ||
388 | td->total_io_size = td->io_size * td->loops; | |
389 | ||
bdb4e2e9 | 390 | for_each_file(td, f, i) |
b5af8293 | 391 | td_io_close_file(td, f); |
21972cde JA |
392 | |
393 | return err; | |
53cdc686 JA |
394 | } |
395 | ||
396 | void close_files(struct thread_data *td) | |
397 | { | |
0ab8db89 | 398 | struct fio_file *f; |
af52b345 | 399 | unsigned int i; |
53cdc686 | 400 | |
0ab8db89 | 401 | for_each_file(td, f, i) { |
1315a68a | 402 | if (!f->file_name && (f->flags & FIO_FILE_UNLINK) && |
af52b345 | 403 | f->filetype == FIO_TYPE_FILE) { |
132ad46d | 404 | unlink(f->file_name); |
1315a68a | 405 | free(f->file_name); |
132ad46d JA |
406 | f->file_name = NULL; |
407 | } | |
bdb4e2e9 | 408 | |
b5af8293 | 409 | td_io_close_file(td, f); |
b3dc7f07 JA |
410 | |
411 | if (f->file_map) | |
412 | free(f->file_map); | |
53cdc686 | 413 | } |
b4a6a59a | 414 | |
132ad46d | 415 | td->filename = NULL; |
b4a6a59a JA |
416 | free(td->files); |
417 | td->files = NULL; | |
418 | td->nr_files = 0; | |
53cdc686 | 419 | } |
af52b345 | 420 | |
e3bab463 | 421 | static void get_file_type(struct fio_file *f) |
af52b345 JA |
422 | { |
423 | struct stat sb; | |
424 | ||
425 | f->filetype = FIO_TYPE_FILE; | |
426 | ||
e3bab463 | 427 | if (!lstat(f->file_name, &sb)) { |
af52b345 JA |
428 | if (S_ISBLK(sb.st_mode)) |
429 | f->filetype = FIO_TYPE_BD; | |
430 | else if (S_ISCHR(sb.st_mode)) | |
431 | f->filetype = FIO_TYPE_CHAR; | |
432 | } | |
433 | } | |
434 | ||
435 | void add_file(struct thread_data *td, const char *fname) | |
436 | { | |
7b4e4fe5 | 437 | int cur_files = td->files_index; |
af52b345 JA |
438 | struct fio_file *f; |
439 | ||
440 | td->files = realloc(td->files, (cur_files + 1) * sizeof(*f)); | |
441 | ||
442 | f = &td->files[cur_files]; | |
443 | memset(f, 0, sizeof(*f)); | |
444 | f->fd = -1; | |
cae61953 | 445 | f->file_name = strdup(fname); |
af52b345 | 446 | |
e3bab463 | 447 | get_file_type(f); |
af52b345 | 448 | |
7b4e4fe5 | 449 | td->files_index++; |
1549441c JA |
450 | if (f->filetype == FIO_TYPE_FILE) |
451 | td->nr_normal_files++; | |
af52b345 | 452 | } |
0ad920e7 JA |
453 | |
454 | void get_file(struct fio_file *f) | |
455 | { | |
456 | f->references++; | |
457 | } | |
458 | ||
459 | void put_file(struct thread_data *td, struct fio_file *f) | |
460 | { | |
461 | if (!(f->flags & FIO_FILE_OPEN)) | |
462 | return; | |
463 | ||
464 | assert(f->references); | |
465 | if (--f->references) | |
466 | return; | |
467 | ||
468 | if (td->io_ops->close_file) | |
469 | td->io_ops->close_file(td, f); | |
470 | td->nr_open_files--; | |
471 | f->flags &= ~FIO_FILE_OPEN; | |
472 | } |