blob: 48b034a6b4f83d4913e94a19a72611e8d1755e80 [file] [log] [blame]
Jens Axboe53cdc682006-10-18 11:50:58 +02001#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
Jens Axboe25205e92006-10-19 20:50:14 +020011/*
12 * Check if the file exists and it's large enough.
13 */
14static int file_ok(struct thread_data *td, struct fio_file *f)
Jens Axboe53cdc682006-10-18 11:50:58 +020015{
Jens Axboeb2a15192006-10-18 14:10:42 +020016 struct stat st;
Jens Axboe53cdc682006-10-18 11:50:58 +020017
Jens Axboeb5af8292007-03-08 12:43:13 +010018 if (td->filetype != FIO_TYPE_FILE ||
19 (td->io_ops->flags & FIO_DISKLESSIO))
Jens Axboeb2a15192006-10-18 14:10:42 +020020 return 0;
21
Jens Axboefa01d132007-02-22 14:07:39 +010022 if (lstat(f->file_name, &st) == -1)
Jens Axboe25205e92006-10-19 20:50:14 +020023 return 1;
Jens Axboefa01d132007-02-22 14:07:39 +010024
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)
Jens Axboe25205e92006-10-19 20:50:14 +020031 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;
Jens Axboeb2a15192006-10-18 14:10:42 +020042
Jens Axboe53cdc682006-10-18 11:50:58 +020043 f->fd = open(f->file_name, O_WRONLY | O_CREAT | O_TRUNC, 0644);
44 if (f->fd < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +010045 td_verror(td, errno, "open");
Jens Axboe53cdc682006-10-18 11:50:58 +020046 return 1;
47 }
48
49 if (ftruncate(f->fd, f->file_size) == -1) {
Jens Axboee1161c32007-02-22 19:36:48 +010050 td_verror(td, errno, "ftruncate");
Jens Axboe53cdc682006-10-18 11:50:58 +020051 goto err;
52 }
53
Jens Axboe40f82982006-10-25 11:21:05 +020054 if (posix_fallocate(f->fd, 0, f->file_size) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +010055 td_verror(td, errno, "posix_fallocate");
Jens Axboe40f82982006-10-25 11:21:05 +020056 goto err;
57 }
58
Jens Axboea00735e2006-11-03 08:58:08 +010059 b = malloc(td->max_bs[DDIR_WRITE]);
60 memset(b, 0, td->max_bs[DDIR_WRITE]);
Jens Axboe53cdc682006-10-18 11:50:58 +020061
62 left = f->file_size;
63 while (left && !td->terminate) {
Jens Axboea00735e2006-11-03 08:58:08 +010064 bs = td->max_bs[DDIR_WRITE];
Jens Axboe53cdc682006-10-18 11:50:58 +020065 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)
Jens Axboee1161c32007-02-22 19:36:48 +010075 td_verror(td, errno, "write");
Jens Axboe53cdc682006-10-18 11:50:58 +020076 else
Jens Axboee1161c32007-02-22 19:36:48 +010077 td_verror(td, EIO, "write");
Jens Axboe53cdc682006-10-18 11:50:58 +020078
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;
Jens Axboe02638822007-03-12 09:25:55 +0100101 int i, err, need_create, can_extend;
Jens Axboe53cdc682006-10-18 11:50:58 +0200102
Jens Axboef6971252006-11-15 10:00:31 +0100103 for_each_file(td, f, i)
104 f->file_size = td->total_file_size / td->nr_files;
105
Jens Axboe53cdc682006-10-18 11:50:58 +0200106 /*
107 * unless specifically asked for overwrite, let normal io extend it
108 */
Jens Axboe02638822007-03-12 09:25:55 +0100109 can_extend = !td->overwrite && !(td->io_ops->flags & FIO_NOEXTEND);
110 if (can_extend)
Jens Axboe53cdc682006-10-18 11:50:58 +0200111 return 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200112
Jens Axboef1027062006-10-20 10:14:01 +0200113 need_create = 0;
Jens Axboeeff68612007-01-14 05:56:44 +0100114 if (td->filetype == FIO_TYPE_FILE) {
115 for_each_file(td, f, i) {
116 int file_there = !file_ok(td, f);
117
Jens Axboecf3d6092007-03-12 09:32:47 +0100118 if (file_there && td_write(td) && !td->overwrite) {
Jens Axboeeff68612007-01-14 05:56:44 +0100119 unlink(f->file_name);
120 file_there = 0;
121 }
122
123 need_create += !file_there;
124 }
125 }
Jens Axboef1027062006-10-20 10:14:01 +0200126
127 if (!need_create)
128 return 0;
129
Jens Axboe53cdc682006-10-18 11:50:58 +0200130 if (!td->total_file_size) {
131 log_err("Need size for create\n");
Jens Axboee1161c32007-02-22 19:36:48 +0100132 td_verror(td, EINVAL, "file_size");
Jens Axboe53cdc682006-10-18 11:50:58 +0200133 return 1;
134 }
135
Jens Axboe25205e92006-10-19 20:50:14 +0200136 temp_stall_ts = 1;
Jens Axboe1e97cce2006-12-05 11:44:16 +0100137 fprintf(f_out, "%s: Laying out IO file(s) (%u x %LuMiB == %LuMiB)\n",
Jens Axboe13f8e2d2006-10-24 09:29:45 +0200138 td->name, td->nr_uniq_files,
139 (td->total_file_size >> 20) / td->nr_uniq_files,
Jens Axboe25205e92006-10-19 20:50:14 +0200140 td->total_file_size >> 20);
141
142 err = 0;
143 for_each_file(td, f, i) {
Jens Axboe9b031dc2006-12-15 16:01:54 +0100144 /*
145 * Only unlink files that we created.
146 */
147 f->unlink = 0;
Jens Axboe25205e92006-10-19 20:50:14 +0200148 if (file_ok(td, f)) {
Jens Axboe9b031dc2006-12-15 16:01:54 +0100149 f->unlink = td->unlink;
Jens Axboe25205e92006-10-19 20:50:14 +0200150 err = create_file(td, f);
151 if (err)
152 break;
153 }
154 }
155
Jens Axboe53cdc682006-10-18 11:50:58 +0200156 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) {
Jens Axboee1161c32007-02-22 19:36:48 +0100166 td_verror(td, errno, "fstat");
Jens Axboe53cdc682006-10-18 11:50:58 +0200167 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;
Jens Axboe745508d2007-01-08 10:25:51 +0100174 } else
175 f->real_file_size = f->file_size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200176
Jens Axboe53cdc682006-10-18 11:50:58 +0200177 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) {
Jens Axboee1161c32007-02-22 19:36:48 +0100187 td_verror(td, r, "blockdev_size");
Jens Axboe53cdc682006-10-18 11:50:58 +0200188 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
Jens Axboe53cdc682006-10-18 11:50:58 +0200222 return 0;
223}
224
Jens Axboee5b401d2006-10-18 16:03:40 +0200225int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
226{
227 int ret = 0;
228
Jens Axboeda86ccb2007-03-08 12:49:37 +0100229 if (td->odirect)
Jens Axboeb5af8292007-03-08 12:43:13 +0100230 return 0;
231
Jens Axboee5b401d2006-10-18 16:03:40 +0200232 /*
233 * FIXME: add blockdev flushing too
234 */
Jens Axboeb5af8292007-03-08 12:43:13 +0100235 if (f->mmap)
Jens Axboee5b401d2006-10-18 16:03:40 +0200236 ret = madvise(f->mmap, f->file_size, MADV_DONTNEED);
Jens Axboead2da602007-02-26 12:33:27 +0100237 else if (td->filetype == FIO_TYPE_FILE) {
Jens Axboeb5af8292007-03-08 12:43:13 +0100238 ret = fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_DONTNEED);
Jens Axboead2da602007-02-26 12:33:27 +0100239 } else if (td->filetype == FIO_TYPE_BD) {
Jens Axboeb5af8292007-03-08 12:43:13 +0100240 ret = blockdev_invalidate_cache(f->fd);
Jens Axboead2da602007-02-26 12:33:27 +0100241 } else if (td->filetype == FIO_TYPE_CHAR)
Jens Axboee5b401d2006-10-18 16:03:40 +0200242 ret = 0;
243
244 if (ret < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100245 td_verror(td, errno, "invalidate_cache");
Jens Axboee5b401d2006-10-18 16:03:40 +0200246 return 1;
247 }
248
Jens Axboead2da602007-02-26 12:33:27 +0100249 return ret;
Jens Axboee5b401d2006-10-18 16:03:40 +0200250}
251
Jens Axboeb5af8292007-03-08 12:43:13 +0100252void generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
Jens Axboe53cdc682006-10-18 11:50:58 +0200253{
Jens Axboeb5af8292007-03-08 12:43:13 +0100254 close(f->fd);
255 f->fd = -1;
Jens Axboe53cdc682006-10-18 11:50:58 +0200256}
257
Jens Axboeb5af8292007-03-08 12:43:13 +0100258int generic_open_file(struct thread_data *td, struct fio_file *f)
Jens Axboe53cdc682006-10-18 11:50:58 +0200259{
Jens Axboe53cdc682006-10-18 11:50:58 +0200260 int flags = 0;
261
Jens Axboe2fd233b2007-03-02 08:44:25 +0100262 if (td->odirect)
263 flags |= OS_O_DIRECT;
264 if (td->sync_io)
265 flags |= O_SYNC;
Jens Axboe7abf8332006-11-23 15:01:19 +0100266
Jens Axboe2fd233b2007-03-02 08:44:25 +0100267 if (td_write(td) || td_rw(td)) {
268 flags |= O_RDWR;
Jens Axboe53cdc682006-10-18 11:50:58 +0200269
Jens Axboe02638822007-03-12 09:25:55 +0100270 if (td->filetype == FIO_TYPE_FILE)
Jens Axboe2fd233b2007-03-02 08:44:25 +0100271 flags |= O_CREAT;
Jens Axboe2fd233b2007-03-02 08:44:25 +0100272
Jens Axboeb5af8292007-03-08 12:43:13 +0100273 f->fd = open(f->file_name, flags, 0600);
Jens Axboe2fd233b2007-03-02 08:44:25 +0100274 } else {
275 if (td->filetype == FIO_TYPE_CHAR)
276 flags |= O_RDWR;
277 else
278 flags |= O_RDONLY;
279
Jens Axboeb5af8292007-03-08 12:43:13 +0100280 f->fd = open(f->file_name, flags);
Jens Axboe53cdc682006-10-18 11:50:58 +0200281 }
282
283 if (f->fd == -1) {
Jens Axboee1161c32007-02-22 19:36:48 +0100284 int __e = errno;
285
286 td_verror(td, __e, "open");
287 if (__e == EINVAL && td->odirect)
Jens Axboe9d80e112007-02-22 19:37:53 +0100288 log_err("fio: destination does not support O_DIRECT\n");
Jens Axboe53cdc682006-10-18 11:50:58 +0200289 return 1;
290 }
291
Jens Axboeb5af8292007-03-08 12:43:13 +0100292 if (get_file_size(td, f))
293 goto err;
294
Jens Axboe02638822007-03-12 09:25:55 +0100295 if (td->invalidate_cache && file_invalidate_cache(td, f))
Jens Axboeb5af8292007-03-08 12:43:13 +0100296 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 }
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100308 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200309
310 return 0;
Jens Axboeb5af8292007-03-08 12:43:13 +0100311err:
312 close(f->fd);
313 return 1;
314}
315
Jens Axboe21972cd2006-11-23 12:39:16 +0100316int 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) {
Jens Axboeb5af8292007-03-08 12:43:13 +0100322 err = td_io_open_file(td, f);
Jens Axboe21972cd2006-11-23 12:39:16 +0100323 if (err)
324 break;
Jens Axboeb5af8292007-03-08 12:43:13 +0100325
326 if (td->open_files == td->nr_open_files)
327 break;
Jens Axboe21972cd2006-11-23 12:39:16 +0100328 }
329
Jens Axboe7abf8332006-11-23 15:01:19 +0100330 if (!err)
331 return 0;
332
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100333 for_each_file(td, f, i)
Jens Axboeb5af8292007-03-08 12:43:13 +0100334 td_io_close_file(td, f);
Jens Axboe7abf8332006-11-23 15:01:19 +0100335
Jens Axboe21972cd2006-11-23 12:39:16 +0100336 return err;
337}
338
Jens Axboe53cdc682006-10-18 11:50:58 +0200339int setup_files(struct thread_data *td)
340{
341 struct fio_file *f;
Jens Axboe21972cd2006-11-23 12:39:16 +0100342 int err, i;
Jens Axboe53cdc682006-10-18 11:50:58 +0200343
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
Jens Axboe21972cd2006-11-23 12:39:16 +0100354 err = open_files(td);
Jens Axboef1027062006-10-20 10:14:01 +0200355 if (err)
356 return err;
357
Jens Axboe0a7eb122006-10-20 10:36:42 +0200358 /*
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
Jens Axboe0f14fef2007-03-08 13:08:24 +0100365 td->total_file_size = (td->total_file_size * td->nr_files) / td->open_files;
366
Jens Axboef1027062006-10-20 10:14:01 +0200367 td->io_size = td->total_file_size;
Jens Axboe53cdc682006-10-18 11:50:58 +0200368 if (td->io_size == 0) {
369 log_err("%s: no io blocks\n", td->name);
Jens Axboee1161c32007-02-22 19:36:48 +0100370 td_verror(td, EINVAL, "total_file_size");
Jens Axboe53cdc682006-10-18 11:50:58 +0200371 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
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100379 for_each_file(td, f, i)
Jens Axboeb5af8292007-03-08 12:43:13 +0100380 td_io_close_file(td, f);
Jens Axboe21972cd2006-11-23 12:39:16 +0100381
382 return err;
Jens Axboe53cdc682006-10-18 11:50:58 +0200383}
384
385void close_files(struct thread_data *td)
386{
Jens Axboe0ab8db82006-10-18 17:16:23 +0200387 struct fio_file *f;
Jens Axboe53cdc682006-10-18 11:50:58 +0200388 int i;
389
Jens Axboe0ab8db82006-10-18 17:16:23 +0200390 for_each_file(td, f, i) {
Jens Axboe9b031dc2006-12-15 16:01:54 +0100391 if (!td->filename && f->unlink &&
Jens Axboe02bcaa82006-11-24 10:42:00 +0100392 td->filetype == FIO_TYPE_FILE) {
Jens Axboe132ad462006-11-23 12:23:12 +0100393 unlink(f->file_name);
394 free(f->file_name);
395 f->file_name = NULL;
396 }
Jens Axboebdb4e2e2007-03-01 09:54:57 +0100397
Jens Axboeb5af8292007-03-08 12:43:13 +0100398 td_io_close_file(td, f);
Jens Axboeb3dc7f02007-03-09 12:52:15 +0100399
400 if (f->file_map)
401 free(f->file_map);
Jens Axboe53cdc682006-10-18 11:50:58 +0200402 }
Jens Axboeb4a6a592006-10-20 13:54:47 +0200403
Jens Axboe132ad462006-11-23 12:23:12 +0100404 td->filename = NULL;
Jens Axboeb4a6a592006-10-20 13:54:47 +0200405 free(td->files);
406 td->files = NULL;
407 td->nr_files = 0;
Jens Axboe53cdc682006-10-18 11:50:58 +0200408}