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 | |
b2a15192 JA |
18 | if (td->filetype != FIO_TYPE_FILE) |
19 | return 0; | |
20 | ||
25205e97 JA |
21 | if (stat(f->file_name, &st) == -1) |
22 | return 1; | |
23 | else if (st.st_size < (off_t) f->file_size) | |
24 | return 1; | |
25 | ||
26 | return 0; | |
27 | } | |
28 | ||
29 | static int create_file(struct thread_data *td, struct fio_file *f) | |
30 | { | |
31 | unsigned long long left; | |
32 | unsigned int bs; | |
33 | char *b; | |
34 | int r; | |
b2a15192 | 35 | |
53cdc686 JA |
36 | f->fd = open(f->file_name, O_WRONLY | O_CREAT | O_TRUNC, 0644); |
37 | if (f->fd < 0) { | |
38 | td_verror(td, errno); | |
39 | return 1; | |
40 | } | |
41 | ||
42 | if (ftruncate(f->fd, f->file_size) == -1) { | |
43 | td_verror(td, errno); | |
44 | goto err; | |
45 | } | |
46 | ||
40f8298c JA |
47 | if (posix_fallocate(f->fd, 0, f->file_size) < 0) { |
48 | td_verror(td, errno); | |
49 | goto err; | |
50 | } | |
51 | ||
a00735e6 JA |
52 | b = malloc(td->max_bs[DDIR_WRITE]); |
53 | memset(b, 0, td->max_bs[DDIR_WRITE]); | |
53cdc686 JA |
54 | |
55 | left = f->file_size; | |
56 | while (left && !td->terminate) { | |
a00735e6 | 57 | bs = td->max_bs[DDIR_WRITE]; |
53cdc686 JA |
58 | if (bs > left) |
59 | bs = left; | |
60 | ||
61 | r = write(f->fd, b, bs); | |
62 | ||
63 | if (r == (int) bs) { | |
64 | left -= bs; | |
65 | continue; | |
66 | } else { | |
67 | if (r < 0) | |
68 | td_verror(td, errno); | |
69 | else | |
70 | td_verror(td, EIO); | |
71 | ||
72 | break; | |
73 | } | |
74 | } | |
75 | ||
76 | if (td->terminate) | |
77 | unlink(f->file_name); | |
78 | else if (td->create_fsync) | |
79 | fsync(f->fd); | |
80 | ||
81 | free(b); | |
82 | close(f->fd); | |
83 | f->fd = -1; | |
84 | return 0; | |
85 | err: | |
86 | close(f->fd); | |
87 | f->fd = -1; | |
88 | return 1; | |
89 | } | |
90 | ||
91 | static int create_files(struct thread_data *td) | |
92 | { | |
93 | struct fio_file *f; | |
25205e97 | 94 | int i, err, need_create; |
53cdc686 JA |
95 | |
96 | /* | |
97 | * unless specifically asked for overwrite, let normal io extend it | |
98 | */ | |
99 | if (!td->overwrite) { | |
53cdc686 JA |
100 | for_each_file(td, f, i) |
101 | f->file_size = td->total_file_size / td->nr_files; | |
102 | ||
103 | return 0; | |
104 | } | |
105 | ||
25205e97 | 106 | need_create = 0; |
0a7eb121 JA |
107 | if (td->filetype == FIO_TYPE_FILE) |
108 | for_each_file(td, f, i) | |
f1027063 | 109 | need_create += file_ok(td, f); |
25205e97 | 110 | |
25205e97 JA |
111 | if (!need_create) |
112 | return 0; | |
113 | ||
f1027063 JA |
114 | if (!td->total_file_size) { |
115 | log_err("Need size for create\n"); | |
116 | td_verror(td, EINVAL); | |
117 | return 1; | |
118 | } | |
119 | ||
53cdc686 | 120 | temp_stall_ts = 1; |
25205e97 | 121 | fprintf(f_out, "%s: Laying out IO file(s) (%d x %LuMiB == %LuMiB)\n", |
13f8e2d2 JA |
122 | td->name, td->nr_uniq_files, |
123 | (td->total_file_size >> 20) / td->nr_uniq_files, | |
25205e97 | 124 | td->total_file_size >> 20); |
53cdc686 JA |
125 | |
126 | err = 0; | |
127 | for_each_file(td, f, i) { | |
25205e97 JA |
128 | if (file_ok(td, f)) { |
129 | err = create_file(td, f); | |
130 | if (err) | |
131 | break; | |
132 | } | |
53cdc686 JA |
133 | } |
134 | ||
135 | temp_stall_ts = 0; | |
136 | return err; | |
137 | } | |
138 | ||
139 | static int file_size(struct thread_data *td, struct fio_file *f) | |
140 | { | |
141 | struct stat st; | |
142 | ||
143 | if (td->overwrite) { | |
144 | if (fstat(f->fd, &st) == -1) { | |
145 | td_verror(td, errno); | |
146 | return 1; | |
147 | } | |
148 | ||
149 | f->real_file_size = st.st_size; | |
150 | ||
151 | if (!f->file_size || f->file_size > f->real_file_size) | |
152 | f->file_size = f->real_file_size; | |
153 | } | |
154 | ||
155 | f->file_size -= f->file_offset; | |
156 | return 0; | |
157 | } | |
158 | ||
159 | static int bdev_size(struct thread_data *td, struct fio_file *f) | |
160 | { | |
161 | unsigned long long bytes; | |
162 | int r; | |
163 | ||
164 | r = blockdev_size(f->fd, &bytes); | |
165 | if (r) { | |
166 | td_verror(td, r); | |
167 | return 1; | |
168 | } | |
169 | ||
170 | f->real_file_size = bytes; | |
171 | ||
172 | /* | |
173 | * no extend possibilities, so limit size to device size if too large | |
174 | */ | |
175 | if (!f->file_size || f->file_size > f->real_file_size) | |
176 | f->file_size = f->real_file_size; | |
177 | ||
178 | f->file_size -= f->file_offset; | |
179 | return 0; | |
180 | } | |
181 | ||
182 | static int get_file_size(struct thread_data *td, struct fio_file *f) | |
183 | { | |
184 | int ret = 0; | |
185 | ||
186 | if (td->filetype == FIO_TYPE_FILE) | |
187 | ret = file_size(td, f); | |
188 | else if (td->filetype == FIO_TYPE_BD) | |
189 | ret = bdev_size(td, f); | |
190 | else | |
191 | f->real_file_size = -1; | |
192 | ||
193 | if (ret) | |
194 | return ret; | |
195 | ||
196 | if (f->file_offset > f->real_file_size) { | |
197 | log_err("%s: offset extends end (%Lu > %Lu)\n", td->name, f->file_offset, f->real_file_size); | |
198 | return 1; | |
199 | } | |
200 | ||
53cdc686 JA |
201 | return 0; |
202 | } | |
203 | ||
e5b401d4 JA |
204 | int file_invalidate_cache(struct thread_data *td, struct fio_file *f) |
205 | { | |
206 | int ret = 0; | |
207 | ||
208 | /* | |
209 | * FIXME: add blockdev flushing too | |
210 | */ | |
211 | if (td->io_ops->flags & FIO_MMAPIO) | |
212 | ret = madvise(f->mmap, f->file_size, MADV_DONTNEED); | |
213 | else if (td->filetype == FIO_TYPE_FILE) | |
214 | ret = fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_DONTNEED); | |
215 | else if (td->filetype == FIO_TYPE_BD) | |
216 | ret = blockdev_invalidate_cache(f->fd); | |
217 | else if (td->filetype == FIO_TYPE_CHAR) | |
218 | ret = 0; | |
219 | ||
220 | if (ret < 0) { | |
221 | td_verror(td, errno); | |
222 | return 1; | |
223 | } | |
224 | ||
225 | return 0; | |
226 | } | |
227 | ||
53cdc686 JA |
228 | static int __setup_file_mmap(struct thread_data *td, struct fio_file *f) |
229 | { | |
230 | int flags; | |
231 | ||
232 | if (td_rw(td)) | |
233 | flags = PROT_READ | PROT_WRITE; | |
234 | else if (td_write(td)) { | |
235 | flags = PROT_WRITE; | |
236 | ||
237 | if (td->verify != VERIFY_NONE) | |
238 | flags |= PROT_READ; | |
239 | } else | |
240 | flags = PROT_READ; | |
241 | ||
242 | f->mmap = mmap(NULL, f->file_size, flags, MAP_SHARED, f->fd, f->file_offset); | |
243 | if (f->mmap == MAP_FAILED) { | |
244 | f->mmap = NULL; | |
245 | td_verror(td, errno); | |
246 | return 1; | |
247 | } | |
248 | ||
e5b401d4 JA |
249 | if (td->invalidate_cache && file_invalidate_cache(td, f)) |
250 | return 1; | |
53cdc686 JA |
251 | |
252 | if (td->sequential) { | |
253 | if (madvise(f->mmap, f->file_size, MADV_SEQUENTIAL) < 0) { | |
254 | td_verror(td, errno); | |
255 | return 1; | |
256 | } | |
257 | } else { | |
258 | if (madvise(f->mmap, f->file_size, MADV_RANDOM) < 0) { | |
259 | td_verror(td, errno); | |
260 | return 1; | |
261 | } | |
262 | } | |
263 | ||
264 | return 0; | |
265 | } | |
266 | ||
267 | static int setup_files_mmap(struct thread_data *td) | |
268 | { | |
269 | struct fio_file *f; | |
270 | int i, err = 0; | |
271 | ||
272 | for_each_file(td, f, i) { | |
273 | err = __setup_file_mmap(td, f); | |
274 | if (err) | |
275 | break; | |
276 | } | |
277 | ||
278 | return err; | |
279 | } | |
280 | ||
281 | static int __setup_file_plain(struct thread_data *td, struct fio_file *f) | |
282 | { | |
e5b401d4 JA |
283 | if (td->invalidate_cache && file_invalidate_cache(td, f)) |
284 | return 1; | |
53cdc686 JA |
285 | |
286 | if (td->sequential) { | |
287 | if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_SEQUENTIAL) < 0) { | |
288 | td_verror(td, errno); | |
289 | return 1; | |
290 | } | |
291 | } else { | |
292 | if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_RANDOM) < 0) { | |
293 | td_verror(td, errno); | |
294 | return 1; | |
295 | } | |
296 | } | |
297 | ||
298 | return 0; | |
299 | } | |
300 | ||
301 | static int setup_files_plain(struct thread_data *td) | |
302 | { | |
303 | struct fio_file *f; | |
304 | int i, err = 0; | |
305 | ||
306 | for_each_file(td, f, i) { | |
307 | err = __setup_file_plain(td, f); | |
308 | if (err) | |
309 | break; | |
310 | } | |
311 | ||
312 | return err; | |
313 | } | |
314 | ||
315 | static int setup_file(struct thread_data *td, struct fio_file *f) | |
316 | { | |
53cdc686 JA |
317 | int flags = 0; |
318 | ||
53cdc686 JA |
319 | if (td->odirect) |
320 | flags |= OS_O_DIRECT; | |
321 | ||
322 | if (td_write(td) || td_rw(td)) { | |
323 | if (td->filetype == FIO_TYPE_FILE) { | |
324 | if (!td->overwrite) | |
325 | flags |= O_TRUNC; | |
326 | ||
327 | flags |= O_CREAT; | |
328 | } | |
329 | if (td->sync_io) | |
330 | flags |= O_SYNC; | |
331 | ||
332 | flags |= O_RDWR; | |
333 | ||
334 | f->fd = open(f->file_name, flags, 0600); | |
335 | } else { | |
336 | if (td->filetype == FIO_TYPE_CHAR) | |
337 | flags |= O_RDWR; | |
338 | else | |
339 | flags |= O_RDONLY; | |
340 | ||
341 | f->fd = open(f->file_name, flags); | |
342 | } | |
343 | ||
344 | if (f->fd == -1) { | |
345 | td_verror(td, errno); | |
346 | return 1; | |
347 | } | |
348 | ||
349 | if (get_file_size(td, f)) | |
350 | return 1; | |
351 | ||
352 | return 0; | |
353 | } | |
354 | ||
355 | int setup_files(struct thread_data *td) | |
356 | { | |
357 | struct fio_file *f; | |
358 | int i, err; | |
359 | ||
360 | /* | |
361 | * if ioengine defines a setup() method, it's responsible for | |
362 | * setting up everything in the td->files[] area. | |
363 | */ | |
364 | if (td->io_ops->setup) | |
365 | return td->io_ops->setup(td); | |
366 | ||
367 | if (create_files(td)) | |
368 | return 1; | |
369 | ||
f1027063 | 370 | err = 0; |
53cdc686 JA |
371 | for_each_file(td, f, i) { |
372 | err = setup_file(td, f); | |
373 | if (err) | |
374 | break; | |
375 | } | |
376 | ||
f1027063 JA |
377 | if (err) |
378 | return err; | |
379 | ||
0a7eb121 JA |
380 | /* |
381 | * Recalculate the total file size now that files are set up. | |
382 | */ | |
383 | td->total_file_size = 0; | |
384 | for_each_file(td, f, i) | |
385 | td->total_file_size += f->file_size; | |
386 | ||
f1027063 | 387 | td->io_size = td->total_file_size; |
53cdc686 JA |
388 | if (td->io_size == 0) { |
389 | log_err("%s: no io blocks\n", td->name); | |
390 | td_verror(td, EINVAL); | |
391 | return 1; | |
392 | } | |
393 | ||
394 | if (!td->zone_size) | |
395 | td->zone_size = td->io_size; | |
396 | ||
397 | td->total_io_size = td->io_size * td->loops; | |
398 | ||
399 | if (td->io_ops->flags & FIO_MMAPIO) | |
400 | return setup_files_mmap(td); | |
401 | else | |
402 | return setup_files_plain(td); | |
403 | } | |
404 | ||
405 | void close_files(struct thread_data *td) | |
406 | { | |
0ab8db89 | 407 | struct fio_file *f; |
53cdc686 JA |
408 | int i; |
409 | ||
0ab8db89 | 410 | for_each_file(td, f, i) { |
53cdc686 | 411 | if (f->fd != -1) { |
c21ac0ff JA |
412 | if (td->unlink && td->filetype == FIO_TYPE_FILE && |
413 | td->filename) { | |
f6cbb269 | 414 | unlink(f->file_name); |
c21ac0ff JA |
415 | td->filename = NULL; |
416 | } | |
b4a6a59a JA |
417 | free(f->file_name); |
418 | f->file_name = NULL; | |
53cdc686 JA |
419 | close(f->fd); |
420 | f->fd = -1; | |
421 | } | |
422 | if (f->mmap) { | |
423 | munmap(f->mmap, f->file_size); | |
424 | f->mmap = NULL; | |
425 | } | |
426 | } | |
b4a6a59a JA |
427 | |
428 | free(td->files); | |
429 | td->files = NULL; | |
430 | td->nr_files = 0; | |
53cdc686 | 431 | } |