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