13 static int extend_file(struct thread_data *td, struct fio_file *f)
15 unsigned long long left;
20 if (f->flags & FIO_FILE_EXISTS) {
21 if (unlink(f->file_name) < 0) {
22 td_verror(td, errno, "unlink");
27 f->fd = open(f->file_name, O_WRONLY | O_CREAT | O_TRUNC, 0644);
29 td_verror(td, errno, "open");
33 if (ftruncate(f->fd, f->real_file_size) == -1) {
34 td_verror(td, errno, "ftruncate");
38 if (posix_fallocate(f->fd, 0, f->real_file_size) < 0) {
39 td_verror(td, errno, "posix_fallocate");
43 b = malloc(td->o.max_bs[DDIR_WRITE]);
44 memset(b, 0, td->o.max_bs[DDIR_WRITE]);
46 left = f->real_file_size;
47 while (left && !td->terminate) {
48 bs = td->o.max_bs[DDIR_WRITE];
52 r = write(f->fd, b, bs);
59 td_verror(td, errno, "write");
61 td_verror(td, EIO, "write");
69 else if (td->o.create_fsync)
82 static unsigned long long get_rand_file_size(struct thread_data *td)
84 unsigned long long ret;
87 r = os_random_long(&td->file_size_state);
88 ret = td->o.file_size_low + (unsigned long long) ((double) td->o.file_size_high * (r / (RAND_MAX + 1.0)));
89 ret -= (ret % td->o.rw_min_bs);
93 static int file_size(struct thread_data *td, struct fio_file *f)
97 if (fstat(f->fd, &st) == -1) {
98 td_verror(td, errno, "fstat");
102 f->real_file_size = st.st_size;
106 static int bdev_size(struct thread_data *td, struct fio_file *f)
108 unsigned long long bytes;
111 r = blockdev_size(f->fd, &bytes);
113 td_verror(td, r, "blockdev_size");
117 f->real_file_size = bytes;
121 static int get_file_size(struct thread_data *td, struct fio_file *f)
125 if (f->filetype == FIO_TYPE_FILE)
126 ret = file_size(td, f);
127 else if (f->filetype == FIO_TYPE_BD)
128 ret = bdev_size(td, f);
130 f->real_file_size = -1;
135 if (f->file_offset > f->real_file_size) {
136 log_err("%s: offset extends end (%Lu > %Lu)\n", td->o.name, f->file_offset, f->real_file_size);
143 int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
151 * FIXME: add blockdev flushing too
154 ret = madvise(f->mmap, f->io_size, MADV_DONTNEED);
155 else if (f->filetype == FIO_TYPE_FILE)
156 ret = fadvise(f->fd, f->file_offset, f->io_size, POSIX_FADV_DONTNEED);
157 else if (f->filetype == FIO_TYPE_BD) {
158 ret = blockdev_invalidate_cache(f->fd);
159 if (ret < 0 && errno == EACCES && geteuid()) {
160 log_err("fio: only root may flush block devices. Cache flush bypassed!\n");
163 } else if (f->filetype == FIO_TYPE_CHAR)
167 td_verror(td, errno, "invalidate_cache");
174 void generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
180 int generic_open_file(struct thread_data *td, struct fio_file *f)
185 flags |= OS_O_DIRECT;
189 if (td_write(td) || td_rw(td)) {
192 if (f->filetype == FIO_TYPE_FILE)
195 f->fd = open(f->file_name, flags, 0600);
197 if (f->filetype == FIO_TYPE_CHAR)
202 f->fd = open(f->file_name, flags);
206 char buf[FIO_VERROR_SIZE];
209 snprintf(buf, sizeof(buf) - 1, "open(%s)", f->file_name);
211 td_verror(td, __e, buf);
214 if (get_file_size(td, f))
223 int open_files(struct thread_data *td)
229 for_each_file(td, f, i) {
230 err = td_io_open_file(td, f);
234 if (td->o.open_files == td->nr_open_files)
241 for_each_file(td, f, i)
242 td_io_close_file(td, f);
248 * open/close all files, so that ->real_file_size gets set
250 static int get_file_sizes(struct thread_data *td)
256 for_each_file(td, f, i) {
257 err = td->io_ops->open_file(td, f);
260 memset(td->verror, 0, sizeof(td->verror));
265 td->io_ops->close_file(td, f);
272 * Open the files and setup files sizes, creating files if necessary.
274 int setup_files(struct thread_data *td)
276 unsigned long long total_size, extend_size;
279 int err, need_extend;
282 * if ioengine defines a setup() method, it's responsible for
283 * opening the files and setting f->real_file_size to indicate
284 * the valid range for that file.
286 if (td->io_ops->setup)
287 err = td->io_ops->setup(td);
289 err = get_file_sizes(td);
295 * check sizes. if the files/devices do not exist and the size
296 * isn't passed to fio, abort.
299 for_each_file(td, f, i) {
300 if (f->real_file_size == -1ULL)
303 total_size += f->real_file_size;
307 * device/file sizes are zero and no size given, punt
309 if (!total_size && !td->o.size) {
310 log_err("%s: you need to specify size=\n", td->o.name);
311 td_verror(td, EINVAL, "total_file_size");
316 * now file sizes are known, so we can set ->io_size. if size= is
317 * not given, ->io_size is just equal to ->real_file_size. if size
318 * is given, ->io_size is size / nr_files.
320 extend_size = total_size = 0;
322 for_each_file(td, f, i) {
323 if (!td->o.file_size_low) {
325 * no file size range given, file size is equal to
326 * total size divided by number of files. if that is
327 * zero, set it to the real file size.
329 f->io_size = td->o.size / td->o.nr_files;
331 f->io_size = f->real_file_size;
332 } else if (f->real_file_size < td->o.file_size_low ||
333 f->real_file_size > td->o.file_size_high) {
335 * file size given. if it's fixed, use that. if it's a
336 * range, generate a random size in-between.
338 if (td->o.file_size_low == td->o.file_size_high)
339 f->io_size = td->o.file_size_low;
341 f->io_size = get_rand_file_size(td);
343 f->io_size = f->real_file_size;
345 if (f->io_size == -1ULL)
348 total_size += f->io_size;
350 if (f->filetype == FIO_TYPE_FILE &&
351 f->io_size > f->real_file_size &&
352 !(td->io_ops->flags & FIO_DISKLESSIO)) {
354 extend_size += f->io_size;
355 f->flags |= FIO_FILE_EXTEND;
360 td->o.size = total_size;
363 * See if we need to extend some files
367 log_info("%s: Laying out IO file(s) (%u files / %LuMiB)\n",
368 td->o.name, need_extend, extend_size >> 20);
370 for_each_file(td, f, i) {
371 if (!(f->flags & FIO_FILE_EXTEND))
374 f->flags &= ~FIO_FILE_EXTEND;
375 f->real_file_size = f->io_size;
376 err = extend_file(td, f);
386 if (!td->o.zone_size)
387 td->o.zone_size = td->o.size;
389 td->total_io_size = td->o.size * td->o.loops;
393 int init_random_map(struct thread_data *td)
395 int num_maps, blocks;
399 if (td->o.norandommap)
402 for_each_file(td, f, i) {
403 blocks = (f->real_file_size + td->o.rw_min_bs - 1) / td->o.rw_min_bs;
404 num_maps = (blocks + BLOCKS_PER_MAP-1)/ BLOCKS_PER_MAP;
405 f->file_map = malloc(num_maps * sizeof(long));
407 log_err("fio: failed allocating random map. If running a large number of jobs, try the 'norandommap' option\n");
410 f->num_maps = num_maps;
411 memset(f->file_map, 0, num_maps * sizeof(long));
417 void close_files(struct thread_data *td)
422 for_each_file(td, f, i) {
423 if ((f->flags & FIO_FILE_UNLINK) &&
424 f->filetype == FIO_TYPE_FILE)
425 unlink(f->file_name);
427 td_io_close_file(td, f);
438 td->o.filename = NULL;
444 static void get_file_type(struct fio_file *f)
448 f->filetype = FIO_TYPE_FILE;
450 if (!lstat(f->file_name, &sb)) {
451 if (S_ISBLK(sb.st_mode))
452 f->filetype = FIO_TYPE_BD;
453 else if (S_ISCHR(sb.st_mode))
454 f->filetype = FIO_TYPE_CHAR;
458 void add_file(struct thread_data *td, const char *fname)
460 int cur_files = td->files_index;
461 char file_name[PATH_MAX];
465 td->files = realloc(td->files, (cur_files + 1) * sizeof(*f));
467 f = &td->files[cur_files];
468 memset(f, 0, sizeof(*f));
472 len = sprintf(file_name, "%s/", td->o.directory);
474 sprintf(file_name + len, "%s", fname);
475 f->file_name = strdup(file_name);
480 if (f->filetype == FIO_TYPE_FILE)
481 td->nr_normal_files++;
484 void get_file(struct fio_file *f)
489 void put_file(struct thread_data *td, struct fio_file *f)
491 if (!(f->flags & FIO_FILE_OPEN))
494 assert(f->references);
498 if (should_fsync(td) && td->o.fsync_on_close)
501 if (td->io_ops->close_file)
502 td->io_ops->close_file(td, f);
504 f->flags &= ~FIO_FILE_OPEN;
507 static int recurse_dir(struct thread_data *td, const char *dirname)
513 D = opendir(dirname);
515 td_verror(td, errno, "opendir");
519 while ((dir = readdir(D)) != NULL) {
520 char full_path[PATH_MAX];
523 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
526 sprintf(full_path, "%s/%s", dirname, dir->d_name);
528 if (lstat(full_path, &sb) == -1) {
529 if (errno != ENOENT) {
530 td_verror(td, errno, "stat");
535 if (S_ISREG(sb.st_mode)) {
536 add_file(td, full_path);
541 if ((ret = recurse_dir(td, full_path)) != 0)
549 int add_dir_files(struct thread_data *td, const char *path)
551 return recurse_dir(td, path);
554 void dup_files(struct thread_data *td, struct thread_data *org)
563 bytes = org->files_index * sizeof(*f);
564 td->files = malloc(bytes);
565 memcpy(td->files, org->files, bytes);
567 for_each_file(td, f, i) {
569 f->file_name = strdup(f->file_name);