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