+ pdu->level = le32_to_cpu(pdu->level);
+ pdu->buf_len = le32_to_cpu(pdu->buf_len);
+ pdu->log_sec = le64_to_cpu(pdu->log_sec);
+ pdu->log_usec = le64_to_cpu(pdu->log_usec);
+}
+
+static struct cmd_iolog_pdu *convert_iolog_gz(struct fio_net_cmd *cmd,
+ struct cmd_iolog_pdu *pdu)
+{
+#ifdef CONFIG_ZLIB
+ struct cmd_iolog_pdu *ret;
+ z_stream stream;
+ uint32_t nr_samples;
+ size_t total;
+ char *p;
+
+ stream.zalloc = Z_NULL;
+ stream.zfree = Z_NULL;
+ stream.opaque = Z_NULL;
+ stream.avail_in = 0;
+ stream.next_in = Z_NULL;
+
+ if (inflateInit(&stream) != Z_OK)
+ return NULL;
+
+ /*
+ * Get header first, it's not compressed
+ */
+ nr_samples = le64_to_cpu(pdu->nr_samples);
+
+ if (pdu->log_type == IO_LOG_TYPE_HIST)
+ total = nr_samples * (__log_entry_sz(le32_to_cpu(pdu->log_offset)) +
+ sizeof(struct io_u_plat_entry));
+ else
+ total = nr_samples * __log_entry_sz(le32_to_cpu(pdu->log_offset));
+ ret = malloc(total + sizeof(*pdu));
+ ret->nr_samples = nr_samples;
+
+ memcpy(ret, pdu, sizeof(*pdu));
+
+ p = (char *) ret + sizeof(*pdu);
+
+ stream.avail_in = cmd->pdu_len - sizeof(*pdu);
+ stream.next_in = (void *)((char *) pdu + sizeof(*pdu));
+ while (stream.avail_in) {
+ unsigned int this_chunk = 65536;
+ unsigned int this_len;
+ int err;
+
+ if (this_chunk > total)
+ this_chunk = total;
+
+ stream.avail_out = this_chunk;
+ stream.next_out = (void *)p;
+ err = inflate(&stream, Z_NO_FLUSH);
+ /* may be Z_OK, or Z_STREAM_END */
+ if (err < 0) {
+ log_err("fio: inflate error %d\n", err);
+ free(ret);
+ ret = NULL;
+ goto err;
+ }
+
+ this_len = this_chunk - stream.avail_out;
+ p += this_len;
+ total -= this_len;
+ }
+
+err:
+ inflateEnd(&stream);
+ return ret;
+#else
+ return NULL;
+#endif
+}
+
+/*
+ * This has been compressed on the server side, since it can be big.
+ * Uncompress here.
+ */
+static struct cmd_iolog_pdu *convert_iolog(struct fio_net_cmd *cmd,
+ bool *store_direct)
+{
+ struct cmd_iolog_pdu *pdu = (struct cmd_iolog_pdu *) cmd->payload;
+ struct cmd_iolog_pdu *ret;
+ uint64_t i;
+ int compressed;
+ void *samples;
+
+ *store_direct = false;
+
+ /*
+ * Convert if compressed and we support it. If it's not
+ * compressed, we need not do anything.
+ */
+ compressed = le32_to_cpu(pdu->compressed);
+ if (compressed == XMIT_COMPRESSED) {
+#ifndef CONFIG_ZLIB
+ log_err("fio: server sent compressed data by mistake\n");
+ return NULL;
+#endif
+ ret = convert_iolog_gz(cmd, pdu);
+ if (!ret) {
+ log_err("fio: failed decompressing log\n");
+ return NULL;
+ }
+ } else if (compressed == STORE_COMPRESSED) {
+ *store_direct = true;
+ ret = pdu;
+ } else
+ ret = pdu;
+
+ ret->nr_samples = le64_to_cpu(ret->nr_samples);
+ ret->thread_number = le32_to_cpu(ret->thread_number);
+ ret->log_type = le32_to_cpu(ret->log_type);
+ ret->compressed = le32_to_cpu(ret->compressed);
+ ret->log_offset = le32_to_cpu(ret->log_offset);
+ ret->log_hist_coarseness = le32_to_cpu(ret->log_hist_coarseness);
+
+ if (*store_direct)
+ return ret;
+
+ samples = &ret->samples[0];
+ for (i = 0; i < ret->nr_samples; i++) {
+ struct io_sample *s;
+
+ s = __get_sample(samples, ret->log_offset, i);
+ if (ret->log_type == IO_LOG_TYPE_HIST)
+ s = (struct io_sample *)((char *)s + sizeof(struct io_u_plat_entry) * i);
+
+ s->time = le64_to_cpu(s->time);
+ s->data.val = le64_to_cpu(s->data.val);
+ s->__ddir = le32_to_cpu(s->__ddir);
+ s->bs = le32_to_cpu(s->bs);
+
+ if (ret->log_offset) {
+ struct io_sample_offset *so = (void *) s;
+
+ so->offset = le64_to_cpu(so->offset);
+ }
+
+ if (ret->log_type == IO_LOG_TYPE_HIST) {
+ s->data.plat_entry = (struct io_u_plat_entry *)(((char *)s) + sizeof(*s));
+ s->data.plat_entry->list.next = NULL;
+ s->data.plat_entry->list.prev = NULL;
+ }
+ }
+
+ return ret;
+}
+
+static void sendfile_reply(int fd, struct cmd_sendfile_reply *rep,
+ size_t size, uint64_t tag)
+{
+ rep->error = cpu_to_le32(rep->error);
+ fio_net_send_cmd(fd, FIO_NET_CMD_SENDFILE, rep, size, &tag, NULL);
+}
+
+static int fio_send_file(struct fio_client *client, struct cmd_sendfile *pdu,
+ uint64_t tag)
+{
+ struct cmd_sendfile_reply *rep;
+ struct stat sb;
+ size_t size;
+ int fd;
+
+ size = sizeof(*rep);
+ rep = malloc(size);
+
+ if (stat((char *)pdu->path, &sb) < 0) {
+fail:
+ rep->error = errno;
+ sendfile_reply(client->fd, rep, size, tag);
+ free(rep);
+ return 1;
+ }
+
+ size += sb.st_size;
+ rep = realloc(rep, size);
+ rep->size = cpu_to_le32((uint32_t) sb.st_size);
+
+ fd = open((char *)pdu->path, O_RDONLY);
+ if (fd == -1 )
+ goto fail;
+
+ rep->error = read_data(fd, &rep->data, sb.st_size);
+ sendfile_reply(client->fd, rep, size, tag);
+ free(rep);
+ close(fd);
+ return 0;