+ dprint(FD_VERIFY, "get_next_verify: ret io_u %p\n", io_u);
+
+ if (!td->o.verify_pattern_bytes) {
+ io_u->rand_seed = __rand(&td->verify_state);
+ if (sizeof(int) != sizeof(long *))
+ io_u->rand_seed *= __rand(&td->verify_state);
+ }
+ return 0;
+ }
+
+nothing:
+ dprint(FD_VERIFY, "get_next_verify: empty\n");
+ return 1;
+}
+
+void fio_verify_init(struct thread_data *td)
+{
+ if (td->o.verify == VERIFY_CRC32C_INTEL ||
+ td->o.verify == VERIFY_CRC32C) {
+ crc32c_intel_probe();
+ }
+}
+
+static void *verify_async_thread(void *data)
+{
+ struct thread_data *td = data;
+ struct io_u *io_u;
+ int ret = 0;
+
+ if (fio_option_is_set(&td->o, verify_cpumask) &&
+ fio_setaffinity(td->pid, td->o.verify_cpumask)) {
+ log_err("fio: failed setting verify thread affinity\n");
+ goto done;
+ }
+
+ do {
+ FLIST_HEAD(list);
+
+ read_barrier();
+ if (td->verify_thread_exit)
+ break;
+
+ pthread_mutex_lock(&td->io_u_lock);
+
+ while (flist_empty(&td->verify_list) &&
+ !td->verify_thread_exit) {
+ ret = pthread_cond_wait(&td->verify_cond,
+ &td->io_u_lock);
+ if (ret) {
+ pthread_mutex_unlock(&td->io_u_lock);
+ break;
+ }
+ }
+
+ flist_splice_init(&td->verify_list, &list);
+ pthread_mutex_unlock(&td->io_u_lock);
+
+ if (flist_empty(&list))
+ continue;
+
+ while (!flist_empty(&list)) {
+ io_u = flist_first_entry(&list, struct io_u, verify_list);
+ flist_del_init(&io_u->verify_list);
+
+ io_u_set(io_u, IO_U_F_NO_FILE_PUT);
+ ret = verify_io_u(td, &io_u);
+
+ put_io_u(td, io_u);
+ if (!ret)
+ continue;
+ if (td_non_fatal_error(td, ERROR_TYPE_VERIFY_BIT, ret)) {
+ update_error_count(td, ret);
+ td_clear_error(td);
+ ret = 0;
+ }
+ }
+ } while (!ret);
+
+ if (ret) {
+ td_verror(td, ret, "async_verify");
+ if (td->o.verify_fatal)
+ fio_mark_td_terminate(td);
+ }
+
+done:
+ pthread_mutex_lock(&td->io_u_lock);
+ td->nr_verify_threads--;
+ pthread_mutex_unlock(&td->io_u_lock);
+
+ pthread_cond_signal(&td->free_cond);
+ return NULL;
+}
+
+int verify_async_init(struct thread_data *td)
+{
+ int i, ret;
+ pthread_attr_t attr;
+
+ pthread_attr_init(&attr);
+ pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN);
+
+ td->verify_thread_exit = 0;
+
+ td->verify_threads = malloc(sizeof(pthread_t) * td->o.verify_async);
+ for (i = 0; i < td->o.verify_async; i++) {
+ ret = pthread_create(&td->verify_threads[i], &attr,
+ verify_async_thread, td);
+ if (ret) {
+ log_err("fio: async verify creation failed: %s\n",
+ strerror(ret));
+ break;
+ }
+ ret = pthread_detach(td->verify_threads[i]);
+ if (ret) {
+ log_err("fio: async verify thread detach failed: %s\n",
+ strerror(ret));
+ break;
+ }
+ td->nr_verify_threads++;
+ }
+
+ pthread_attr_destroy(&attr);
+
+ if (i != td->o.verify_async) {
+ log_err("fio: only %d verify threads started, exiting\n", i);
+ td->verify_thread_exit = 1;
+ write_barrier();
+ pthread_cond_broadcast(&td->verify_cond);
+ return 1;
+ }
+
+ return 0;
+}
+
+void verify_async_exit(struct thread_data *td)
+{
+ td->verify_thread_exit = 1;
+ write_barrier();
+ pthread_cond_broadcast(&td->verify_cond);
+
+ pthread_mutex_lock(&td->io_u_lock);
+
+ while (td->nr_verify_threads)
+ pthread_cond_wait(&td->free_cond, &td->io_u_lock);
+
+ pthread_mutex_unlock(&td->io_u_lock);
+ free(td->verify_threads);
+ td->verify_threads = NULL;
+}
+
+struct all_io_list *get_all_io_list(int save_mask, size_t *sz)
+{
+ struct all_io_list *rep;
+ struct thread_data *td;
+ size_t depth;
+ void *next;
+ int i, nr;
+
+ compiletime_assert(sizeof(struct all_io_list) == 8, "all_io_list");
+
+ /*
+ * Calculate reply space needed. We need one 'io_state' per thread,
+ * and the size will vary depending on depth.
+ */
+ depth = 0;
+ nr = 0;
+ for_each_td(td, i) {
+ if (save_mask != IO_LIST_ALL && (i + 1) != save_mask)
+ continue;
+ td->stop_io = 1;
+ td->flags |= TD_F_VSTATE_SAVED;
+ depth += td->o.iodepth;
+ nr++;
+ }
+
+ if (!nr)
+ return NULL;
+
+ *sz = sizeof(*rep);
+ *sz += nr * sizeof(struct thread_io_list);
+ *sz += depth * sizeof(uint64_t);
+ rep = malloc(*sz);
+
+ rep->threads = cpu_to_le64((uint64_t) nr);
+
+ next = &rep->state[0];
+ for_each_td(td, i) {
+ struct thread_io_list *s = next;
+ unsigned int comps;
+
+ if (save_mask != IO_LIST_ALL && (i + 1) != save_mask)
+ continue;
+
+ if (td->last_write_comp) {
+ int j, k;
+
+ if (td->io_blocks[DDIR_WRITE] < td->o.iodepth)
+ comps = td->io_blocks[DDIR_WRITE];
+ else
+ comps = td->o.iodepth;
+
+ k = td->last_write_idx - 1;
+ for (j = 0; j < comps; j++) {
+ if (k == -1)
+ k = td->o.iodepth - 1;
+ s->offsets[j] = cpu_to_le64(td->last_write_comp[k]);
+ k--;
+ }
+ } else
+ comps = 0;
+
+ s->no_comps = cpu_to_le64((uint64_t) comps);
+ s->depth = cpu_to_le64((uint64_t) td->o.iodepth);
+ s->numberio = cpu_to_le64((uint64_t) td->io_issues[DDIR_WRITE]);
+ s->index = cpu_to_le64((uint64_t) i);
+ if (td->random_state.use64) {
+ s->rand.state64.s[0] = cpu_to_le64(td->random_state.state64.s1);
+ s->rand.state64.s[1] = cpu_to_le64(td->random_state.state64.s2);
+ s->rand.state64.s[2] = cpu_to_le64(td->random_state.state64.s3);
+ s->rand.state64.s[3] = cpu_to_le64(td->random_state.state64.s4);
+ s->rand.state64.s[4] = cpu_to_le64(td->random_state.state64.s5);
+ s->rand.state64.s[5] = 0;
+ s->rand.use64 = cpu_to_le64((uint64_t)1);
+ } else {
+ s->rand.state32.s[0] = cpu_to_le32(td->random_state.state32.s1);
+ s->rand.state32.s[1] = cpu_to_le32(td->random_state.state32.s2);
+ s->rand.state32.s[2] = cpu_to_le32(td->random_state.state32.s3);
+ s->rand.state32.s[3] = 0;
+ s->rand.use64 = 0;
+ }
+ s->name[sizeof(s->name) - 1] = '\0';
+ strncpy((char *) s->name, td->o.name, sizeof(s->name) - 1);
+ next = io_list_next(s);
+ }
+
+ return rep;
+}
+
+static int open_state_file(const char *name, const char *prefix, int num,
+ int for_write)
+{
+ char out[64];
+ int flags;
+ int fd;
+
+ if (for_write)
+ flags = O_CREAT | O_TRUNC | O_WRONLY | O_SYNC;
+ else
+ flags = O_RDONLY;
+
+ verify_state_gen_name(out, sizeof(out), name, prefix, num);
+
+ fd = open(out, flags, 0644);
+ if (fd == -1) {
+ perror("fio: open state file");
+ return -1;
+ }
+
+ return fd;
+}
+
+static int write_thread_list_state(struct thread_io_list *s,
+ const char *prefix)
+{
+ struct verify_state_hdr hdr;
+ uint64_t crc;
+ ssize_t ret;
+ int fd;
+
+ fd = open_state_file((const char *) s->name, prefix, s->index, 1);
+ if (fd == -1)
+ return 1;
+
+ crc = fio_crc32c((void *)s, thread_io_list_sz(s));
+
+ hdr.version = cpu_to_le64((uint64_t) VSTATE_HDR_VERSION);
+ hdr.size = cpu_to_le64((uint64_t) thread_io_list_sz(s));
+ hdr.crc = cpu_to_le64(crc);
+ ret = write(fd, &hdr, sizeof(hdr));
+ if (ret != sizeof(hdr))
+ goto write_fail;
+
+ ret = write(fd, s, thread_io_list_sz(s));
+ if (ret != thread_io_list_sz(s)) {
+write_fail:
+ if (ret < 0)
+ perror("fio: write state file");
+ log_err("fio: failed to write state file\n");
+ ret = 1;
+ } else
+ ret = 0;
+
+ close(fd);
+ return ret;
+}
+
+void __verify_save_state(struct all_io_list *state, const char *prefix)
+{
+ struct thread_io_list *s = &state->state[0];
+ unsigned int i;
+
+ for (i = 0; i < le64_to_cpu(state->threads); i++) {
+ write_thread_list_state(s, prefix);
+ s = io_list_next(s);
+ }
+}
+
+void verify_save_state(void)
+{
+ struct all_io_list *state;
+ size_t sz;
+
+ state = get_all_io_list(IO_LIST_ALL, &sz);
+ if (state) {
+ __verify_save_state(state, "local");
+ free(state);
+ }
+}
+
+void verify_free_state(struct thread_data *td)
+{
+ if (td->vstate)
+ free(td->vstate);
+}
+
+static struct thread_io_list *convert_v1_list(struct thread_io_list_v1 *s)
+{
+ struct thread_io_list *til;
+ int i;
+
+ til = malloc(__thread_io_list_sz(s->no_comps));
+ til->no_comps = s->no_comps;
+ til->depth = s->depth;
+ til->numberio = s->numberio;
+ til->index = s->index;
+ memcpy(til->name, s->name, sizeof(til->name));
+
+ til->rand.use64 = 0;
+ for (i = 0; i < 4; i++)
+ til->rand.state32.s[i] = s->rand.s[i];
+
+ for (i = 0; i < s->no_comps; i++)
+ til->offsets[i] = s->offsets[i];
+
+ return til;
+}
+
+void verify_convert_assign_state(struct thread_data *td, void *p, int version)
+{
+ struct thread_io_list *til;
+ int i;
+
+ if (version == 1) {
+ struct thread_io_list_v1 *s = p;
+
+ s->no_comps = le64_to_cpu(s->no_comps);
+ s->depth = le64_to_cpu(s->depth);
+ s->numberio = le64_to_cpu(s->numberio);
+ for (i = 0; i < 4; i++)
+ s->rand.s[i] = le32_to_cpu(s->rand.s[i]);
+ for (i = 0; i < s->no_comps; i++)
+ s->offsets[i] = le64_to_cpu(s->offsets[i]);
+
+ til = convert_v1_list(s);
+ free(s);
+ } else {
+ struct thread_io_list *s = p;
+
+ s->no_comps = le64_to_cpu(s->no_comps);
+ s->depth = le64_to_cpu(s->depth);
+ s->numberio = le64_to_cpu(s->numberio);
+ s->rand.use64 = le64_to_cpu(s->rand.use64);
+
+ if (s->rand.use64) {
+ for (i = 0; i < 6; i++)
+ s->rand.state64.s[i] = le64_to_cpu(s->rand.state64.s[i]);
+ } else {
+ for (i = 0; i < 4; i++)
+ s->rand.state32.s[i] = le32_to_cpu(s->rand.state32.s[i]);
+ }
+ for (i = 0; i < s->no_comps; i++)
+ s->offsets[i] = le64_to_cpu(s->offsets[i]);
+
+ til = p;
+ }
+
+ td->vstate = til;
+}
+
+int verify_state_hdr(struct verify_state_hdr *hdr, struct thread_io_list *s,
+ int *version)
+{
+ uint64_t crc;
+
+ hdr->version = le64_to_cpu(hdr->version);
+ hdr->size = le64_to_cpu(hdr->size);
+ hdr->crc = le64_to_cpu(hdr->crc);
+
+ if (hdr->version != VSTATE_HDR_VERSION ||
+ hdr->version != VSTATE_HDR_VERSION_V1)
+ return 1;
+
+ crc = fio_crc32c((void *)s, hdr->size);
+ if (crc != hdr->crc)
+ return 1;
+
+ *version = hdr->version;
+ return 0;
+}
+
+int verify_load_state(struct thread_data *td, const char *prefix)
+{
+ struct verify_state_hdr hdr;
+ void *s = NULL;
+ uint64_t crc;
+ ssize_t ret;
+ int fd;
+
+ if (!td->o.verify_state)