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