+
+int td_io_open_file(struct thread_data *td, struct fio_file *f)
+{
+ assert(!fio_file_open(f));
+ assert(f->fd == -1);
+
+ if (td->io_ops->open_file(td, f)) {
+ if (td->error == EINVAL && td->o.odirect)
+ log_err("fio: destination does not support O_DIRECT\n");
+ if (td->error == EMFILE) {
+ log_err("fio: try reducing/setting openfiles (failed"
+ " at %u of %u)\n", td->nr_open_files,
+ td->o.nr_files);
+ }
+
+ assert(f->fd == -1);
+ assert(!fio_file_open(f));
+ return 1;
+ }
+
+ fio_file_reset(td, f);
+ fio_file_set_open(f);
+ fio_file_clear_closing(f);
+ disk_util_inc(f->du);
+
+ td->nr_open_files++;
+ get_file(f);
+
+ if (f->filetype == FIO_TYPE_PIPE) {
+ if (td_random(td)) {
+ log_err("fio: can't seek on pipes (no random io)\n");
+ goto err;
+ }
+ }
+
+ if (td_ioengine_flagged(td, FIO_DISKLESSIO))
+ goto done;
+
+ if (td->o.invalidate_cache && file_invalidate_cache(td, f))
+ goto err;
+
+ if (td->o.fadvise_hint != F_ADV_NONE &&
+ (f->filetype == FIO_TYPE_BLOCK || f->filetype == FIO_TYPE_FILE)) {
+ int flags;
+
+ if (td->o.fadvise_hint == F_ADV_TYPE) {
+ if (td_random(td))
+ flags = POSIX_FADV_RANDOM;
+ else
+ flags = POSIX_FADV_SEQUENTIAL;
+ } else if (td->o.fadvise_hint == F_ADV_RANDOM)
+ flags = POSIX_FADV_RANDOM;
+ else if (td->o.fadvise_hint == F_ADV_SEQUENTIAL)
+ flags = POSIX_FADV_SEQUENTIAL;
+ else {
+ log_err("fio: unknown fadvise type %d\n",
+ td->o.fadvise_hint);
+ flags = POSIX_FADV_NORMAL;
+ }
+
+ if (posix_fadvise(f->fd, f->file_offset, f->io_size, flags) < 0) {
+ td_verror(td, errno, "fadvise");
+ goto err;
+ }
+ }
+#ifdef FIO_HAVE_STREAMID
+ if (td->o.fadvise_stream &&
+ (f->filetype == FIO_TYPE_BLOCK || f->filetype == FIO_TYPE_FILE)) {
+ off_t stream = td->o.fadvise_stream;
+
+ if (posix_fadvise(f->fd, stream, f->io_size, POSIX_FADV_STREAMID) < 0) {
+ td_verror(td, errno, "fadvise streamid");
+ goto err;
+ }
+ }
+#endif
+
+#ifdef FIO_OS_DIRECTIO
+ /*
+ * Some OS's have a distinct call to mark the file non-buffered,
+ * instead of using O_DIRECT (Solaris)
+ */
+ if (td->o.odirect) {
+ int ret = fio_set_odirect(f->fd);
+
+ if (ret) {
+ td_verror(td, ret, "fio_set_odirect");
+ if (ret == ENOTTY) { /* ENOTTY suggests RAW device or ZFS */
+ log_err("fio: doing directIO to RAW devices or ZFS not supported\n");
+ } else {
+ log_err("fio: the file system does not seem to support direct IO\n");
+ }
+
+ goto err;
+ }
+ }
+#endif
+
+done:
+ log_file(td, f, FIO_LOG_OPEN_FILE);
+ return 0;
+err:
+ disk_util_dec(f->du);
+ if (td->io_ops->close_file)
+ td->io_ops->close_file(td, f);
+ return 1;
+}
+
+int td_io_close_file(struct thread_data *td, struct fio_file *f)
+{
+ if (!fio_file_closing(f))
+ log_file(td, f, FIO_LOG_CLOSE_FILE);
+
+ /*
+ * mark as closing, do real close when last io on it has completed
+ */
+ fio_file_set_closing(f);
+
+ disk_util_dec(f->du);
+
+ if (td->o.file_lock_mode != FILE_LOCK_NONE)
+ unlock_file_all(td, f);
+
+ return put_file(td, f);
+}
+
+int td_io_unlink_file(struct thread_data *td, struct fio_file *f)
+{
+ if (td->io_ops->unlink_file)
+ return td->io_ops->unlink_file(td, f);
+ else {
+ int ret;
+
+ ret = unlink(f->file_name);
+ if (ret < 0)
+ return errno;
+
+ return 0;
+ }
+}
+
+int td_io_get_file_size(struct thread_data *td, struct fio_file *f)
+{
+ if (!td->io_ops->get_file_size)
+ return 0;
+
+ return td->io_ops->get_file_size(td, f);
+}
+
+int fio_show_ioengine_help(const char *engine)
+{
+ struct flist_head *entry;
+ struct thread_data td;
+ struct ioengine_ops *io_ops;
+ char *sep;
+ int ret = 1;
+
+ if (!engine || !*engine) {
+ log_info("Available IO engines:\n");
+ flist_for_each(entry, &engine_list) {
+ io_ops = flist_entry(entry, struct ioengine_ops, list);
+ log_info("\t%s\n", io_ops->name);
+ }
+ return 0;
+ }
+ sep = strchr(engine, ',');
+ if (sep) {
+ *sep = 0;
+ sep++;
+ }
+
+ memset(&td, 0, sizeof(td));
+
+ io_ops = load_ioengine(&td, engine);
+ if (!io_ops) {
+ log_info("IO engine %s not found\n", engine);
+ return 1;
+ }
+
+ if (io_ops->options)
+ ret = show_cmd_help(io_ops->options, sep);
+ else
+ log_info("IO engine %s has no options\n", io_ops->name);
+
+ free_ioengine(&td);
+
+ return ret;
+}