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