s3:libsmb: rewrite cli_pull* to use smb1cli_conn_req_possible()
[mat/samba.git] / source3 / libsmb / clireadwrite.c
index 9d17ff86a5eba9f39a45b08412264bf4d3751990..dd5d4c2865b2a4c4ced67be79bf6d529cbb42da0 100644 (file)
 */
 
 #include "includes.h"
+#include "libsmb/libsmb.h"
+#include "../lib/util/tevent_ntstatus.h"
+#include "async_smb.h"
+#include "trans2.h"
+#include "../libcli/smb/smbXcli_base.h"
 
 /****************************************************************************
   Calculate the recommended read buffer size
 ****************************************************************************/
 static size_t cli_read_max_bufsize(struct cli_state *cli)
 {
-       if (!client_is_signing_on(cli) && !cli_encryption_on(cli)
-           && (cli->posix_capabilities & CIFS_UNIX_LARGE_READ_CAP)) {
-               return CLI_SAMBA_MAX_POSIX_LARGE_READX_SIZE;
-       }
-       if (cli->capabilities & CAP_LARGE_READX) {
-               return cli->is_samba
-                       ? CLI_SAMBA_MAX_LARGE_READX_SIZE
-                       : CLI_WINDOWS_MAX_LARGE_READX_SIZE;
+       uint8_t wct = 12;
+       uint32_t min_space;
+       uint32_t data_offset;
+       uint32_t useable_space = 0;
+
+       data_offset = HDR_VWV;
+       data_offset += wct * sizeof(uint16_t);
+       data_offset += sizeof(uint16_t); /* byte count */
+       data_offset += 1; /* pad */
+
+       min_space = cli_state_available_size(cli, data_offset);
+
+       if (cli->server_posix_capabilities & CIFS_UNIX_LARGE_READ_CAP) {
+               useable_space = 0xFFFFFF - data_offset;
+
+               if (smb1cli_conn_signing_is_active(cli->conn)) {
+                       return min_space;
+               }
+
+               if (smb1cli_conn_encryption_on(cli->conn)) {
+                       return min_space;
+               }
+
+               return useable_space;
+       } else if (smb1cli_conn_capabilities(cli->conn) & CAP_LARGE_READX) {
+               /*
+                * Note: CAP_LARGE_READX also works with signing
+                */
+               useable_space = 0x1FFFF - data_offset;
+
+               useable_space = MIN(useable_space, UINT16_MAX);
+
+               return useable_space;
        }
-       return (cli->max_xmit - (smb_size+32)) & ~1023;
+
+       return min_space;
 }
 
 /****************************************************************************
   Calculate the recommended write buffer size
 ****************************************************************************/
-static size_t cli_write_max_bufsize(struct cli_state *cli, uint16_t write_mode)
+static size_t cli_write_max_bufsize(struct cli_state *cli,
+                                   uint16_t write_mode,
+                                   uint8_t wct)
 {
-        if (write_mode == 0 &&
-           !client_is_signing_on(cli) &&
-           !cli_encryption_on(cli) &&
-           (cli->posix_capabilities & CIFS_UNIX_LARGE_WRITE_CAP) &&
-           (cli->capabilities & CAP_LARGE_FILES)) {
-               /* Only do massive writes if we can do them direct
-                * with no signing or encrypting - not on a pipe. */
-               return CLI_SAMBA_MAX_POSIX_LARGE_WRITEX_SIZE;
+       uint32_t min_space;
+       uint32_t data_offset;
+       uint32_t useable_space = 0;
+
+       data_offset = HDR_VWV;
+       data_offset += wct * sizeof(uint16_t);
+       data_offset += sizeof(uint16_t); /* byte count */
+       data_offset += 1; /* pad */
+
+       min_space = cli_state_available_size(cli, data_offset);
+
+       if (cli->server_posix_capabilities & CIFS_UNIX_LARGE_WRITE_CAP) {
+               useable_space = 0xFFFFFF - data_offset;
+       } else if (smb1cli_conn_capabilities(cli->conn) & CAP_LARGE_WRITEX) {
+               useable_space = 0x1FFFF - data_offset;
+       } else {
+               return min_space;
        }
 
-       if (cli->is_samba) {
-               return CLI_SAMBA_MAX_LARGE_WRITEX_SIZE;
+       if (write_mode != 0) {
+               return min_space;
        }
 
-       if (((cli->capabilities & CAP_LARGE_WRITEX) == 0)
-           || client_is_signing_on(cli)
-           || strequal(cli->dev, "LPT1:")) {
+       if (smb1cli_conn_signing_is_active(cli->conn)) {
+               return min_space;
+       }
 
-               /*
-                * Printer devices are restricted to max_xmit writesize in
-                * Vista and XPSP3 as are signing connections.
-                */
+       if (smb1cli_conn_encryption_on(cli->conn)) {
+               return min_space;
+       }
 
-               return (cli->max_xmit - (smb_size+32)) & ~1023;
+       if (strequal(cli->dev, "LPT1:")) {
+               return min_space;
        }
 
-       return CLI_WINDOWS_MAX_LARGE_WRITEX_SIZE;
+       return useable_space;
 }
 
+struct cli_read_andx_state {
+       size_t size;
+       uint16_t vwv[12];
+       NTSTATUS status;
+       size_t received;
+       uint8_t *buf;
+};
 
-/*
- * Send a read&x request
- */
+static void cli_read_andx_done(struct tevent_req *subreq);
 
-struct async_req *cli_read_andx_send(TALLOC_CTX *mem_ctx,
-                                    struct event_context *ev,
-                                    struct cli_state *cli, int fnum,
-                                    off_t offset, size_t size)
+struct tevent_req *cli_read_andx_create(TALLOC_CTX *mem_ctx,
+                                       struct tevent_context *ev,
+                                       struct cli_state *cli, uint16_t fnum,
+                                       off_t offset, size_t size,
+                                       struct tevent_req **psmbreq)
 {
-       struct async_req *result;
-       struct cli_request *req;
-       bool bigoffset = False;
-
-       uint16_t vwv[12];
+       struct tevent_req *req, *subreq;
+       struct cli_read_andx_state *state;
        uint8_t wct = 10;
 
-       if (size > cli_read_max_bufsize(cli)) {
-               DEBUG(0, ("cli_read_andx_send got size=%d, can only handle "
-                         "size=%d\n", (int)size,
-                         (int)cli_read_max_bufsize(cli)));
+       req = tevent_req_create(mem_ctx, &state, struct cli_read_andx_state);
+       if (req == NULL) {
                return NULL;
        }
+       state->size = size;
 
-       SCVAL(vwv + 0, 0, 0xFF);
-       SCVAL(vwv + 0, 1, 0);
-       SSVAL(vwv + 1, 0, 0);
-       SSVAL(vwv + 2, 0, fnum);
-       SIVAL(vwv + 3, 0, offset);
-       SSVAL(vwv + 5, 0, size);
-       SSVAL(vwv + 6, 0, size);
-       SSVAL(vwv + 7, 0, (size >> 16));
-       SSVAL(vwv + 8, 0, 0);
-       SSVAL(vwv + 9, 0, 0);
-
-       if ((uint64_t)offset >> 32) {
-               bigoffset = True;
-               SIVAL(vwv + 10, 0,
+       SCVAL(state->vwv + 0, 0, 0xFF);
+       SCVAL(state->vwv + 0, 1, 0);
+       SSVAL(state->vwv + 1, 0, 0);
+       SSVAL(state->vwv + 2, 0, fnum);
+       SIVAL(state->vwv + 3, 0, offset);
+       SSVAL(state->vwv + 5, 0, size);
+       SSVAL(state->vwv + 6, 0, size);
+       SSVAL(state->vwv + 7, 0, (size >> 16));
+       SSVAL(state->vwv + 8, 0, 0);
+       SSVAL(state->vwv + 9, 0, 0);
+
+       if (smb1cli_conn_capabilities(cli->conn) & CAP_LARGE_FILES) {
+               SIVAL(state->vwv + 10, 0,
                      (((uint64_t)offset)>>32) & 0xffffffff);
-               wct += 2;
+               wct = 12;
+       } else {
+               if ((((uint64_t)offset) & 0xffffffff00000000LL) != 0) {
+                       DEBUG(10, ("cli_read_andx_send got large offset where "
+                                  "the server does not support it\n"));
+                       tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
+                       return tevent_req_post(req, ev);
+               }
        }
 
-       result = cli_request_send(mem_ctx, ev, cli, SMBreadX, 0, wct, vwv, 0,
-                                 0, NULL);
-       if (result == NULL) {
+       subreq = cli_smb_req_create(state, ev, cli, SMBreadX, 0, wct,
+                                   state->vwv, 0, NULL);
+       if (subreq == NULL) {
+               TALLOC_FREE(req);
                return NULL;
        }
+       tevent_req_set_callback(subreq, cli_read_andx_done, req);
+       *psmbreq = subreq;
+       return req;
+}
 
-       req = talloc_get_type_abort(result->private_data, struct cli_request);
+struct tevent_req *cli_read_andx_send(TALLOC_CTX *mem_ctx,
+                                     struct tevent_context *ev,
+                                     struct cli_state *cli, uint16_t fnum,
+                                     off_t offset, size_t size)
+{
+       struct tevent_req *req, *subreq;
+       NTSTATUS status;
 
-       req->data.read.ofs = offset;
-       req->data.read.size = size;
-       req->data.read.received = 0;
-       req->data.read.rcvbuf = NULL;
+       req = cli_read_andx_create(mem_ctx, ev, cli, fnum, offset, size,
+                                  &subreq);
+       if (req == NULL) {
+               return NULL;
+       }
 
-       return result;
+       status = smb1cli_req_chain_submit(&subreq, 1);
+       if (tevent_req_nterror(req, status)) {
+               return tevent_req_post(req, ev);
+       }
+       return req;
+}
+
+static void cli_read_andx_done(struct tevent_req *subreq)
+{
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
+       struct cli_read_andx_state *state = tevent_req_data(
+               req, struct cli_read_andx_state);
+       uint8_t *inbuf;
+       uint8_t wct;
+       uint16_t *vwv;
+       uint32_t num_bytes;
+       uint8_t *bytes;
+
+       state->status = cli_smb_recv(subreq, state, &inbuf, 12, &wct, &vwv,
+                                    &num_bytes, &bytes);
+       TALLOC_FREE(subreq);
+       if (NT_STATUS_IS_ERR(state->status)) {
+               tevent_req_nterror(req, state->status);
+               return;
+       }
+
+       /* size is the number of bytes the server returned.
+        * Might be zero. */
+       state->received = SVAL(vwv + 5, 0);
+       state->received |= (((unsigned int)SVAL(vwv + 7, 0)) << 16);
+
+       if (state->received > state->size) {
+               DEBUG(5,("server returned more than we wanted!\n"));
+               tevent_req_nterror(req, NT_STATUS_UNEXPECTED_IO_ERROR);
+               return;
+       }
+
+       /*
+        * bcc field must be valid for small reads, for large reads the 16-bit
+        * bcc field can't be correct.
+        */
+
+       if ((state->received < 0xffff) && (state->received > num_bytes)) {
+               DEBUG(5, ("server announced more bytes than sent\n"));
+               tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
+               return;
+       }
+
+       state->buf = discard_const_p(uint8_t, smb_base(inbuf)) + SVAL(vwv+6, 0);
+
+       if (trans_oob(smb_len_tcp(inbuf), SVAL(vwv+6, 0), state->received)
+           || ((state->received != 0) && (state->buf < bytes))) {
+               DEBUG(5, ("server returned invalid read&x data offset\n"));
+               tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
+               return;
+       }
+       tevent_req_done(req);
 }
 
 /*
@@ -135,156 +254,216 @@ struct async_req *cli_read_andx_send(TALLOC_CTX *mem_ctx,
  * talloc_move it!
  */
 
-NTSTATUS cli_read_andx_recv(struct async_req *req, ssize_t *received,
+NTSTATUS cli_read_andx_recv(struct tevent_req *req, ssize_t *received,
                            uint8_t **rcvbuf)
 {
-       struct cli_request *cli_req = talloc_get_type_abort(
-               req->private_data, struct cli_request);
-       uint8_t wct;
-       uint16_t *vwv;
-       uint16_t num_bytes;
-       uint8_t *bytes;
-       uint8_t *buf;
+       struct cli_read_andx_state *state = tevent_req_data(
+               req, struct cli_read_andx_state);
        NTSTATUS status;
-       size_t size;
 
-       if (async_req_is_nterror(req, &status)) {
+       if (tevent_req_is_nterror(req, &status)) {
                return status;
        }
+       *received = state->received;
+       *rcvbuf = state->buf;
+       return NT_STATUS_OK;
+}
 
-       status = cli_pull_reply(req, &wct, &vwv, &num_bytes, &bytes);
+struct cli_readall_state {
+       struct tevent_context *ev;
+       struct cli_state *cli;
+       uint16_t fnum;
+       off_t start_offset;
+       size_t size;
+       size_t received;
+       uint8_t *buf;
+};
 
-       if (NT_STATUS_IS_ERR(status)) {
-               return status;
+static void cli_readall_done(struct tevent_req *subreq);
+
+static struct tevent_req *cli_readall_send(TALLOC_CTX *mem_ctx,
+                                          struct tevent_context *ev,
+                                          struct cli_state *cli,
+                                          uint16_t fnum,
+                                          off_t offset, size_t size)
+{
+       struct tevent_req *req, *subreq;
+       struct cli_readall_state *state;
+
+       req = tevent_req_create(mem_ctx, &state, struct cli_readall_state);
+       if (req == NULL) {
+               return NULL;
        }
+       state->ev = ev;
+       state->cli = cli;
+       state->fnum = fnum;
+       state->start_offset = offset;
+       state->size = size;
+       state->received = 0;
+       state->buf = NULL;
 
-       if (wct < 12) {
-               return NT_STATUS_INVALID_NETWORK_RESPONSE;
+       subreq = cli_read_andx_send(state, ev, cli, fnum, offset, size);
+       if (tevent_req_nomem(subreq, req)) {
+               return tevent_req_post(req, ev);
        }
+       tevent_req_set_callback(subreq, cli_readall_done, req);
+       return req;
+}
 
-       /* size is the number of bytes the server returned.
-        * Might be zero. */
-       size = SVAL(vwv + 5, 0);
-       size |= (((unsigned int)SVAL(vwv + 7, 0)) << 16);
+static void cli_readall_done(struct tevent_req *subreq)
+{
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
+       struct cli_readall_state *state = tevent_req_data(
+               req, struct cli_readall_state);
+       ssize_t received;
+       uint8_t *buf;
+       NTSTATUS status;
 
-       if (size > cli_req->data.read.size) {
-               DEBUG(5,("server returned more than we wanted!\n"));
-               return NT_STATUS_UNEXPECTED_IO_ERROR;
+       status = cli_read_andx_recv(subreq, &received, &buf);
+       if (tevent_req_nterror(req, status)) {
+               return;
+       }
+
+       if (received == 0) {
+               /* EOF */
+               tevent_req_done(req);
+               return;
+       }
+
+       if ((state->received == 0) && (received == state->size)) {
+               /* Ideal case: Got it all in one run */
+               state->buf = buf;
+               state->received += received;
+               tevent_req_done(req);
+               return;
        }
 
        /*
-        * bcc field must be valid for small reads, for large reads the 16-bit
-        * bcc field can't be correct.
+        * We got a short read, issue a read for the
+        * rest. Unfortunately we have to allocate the buffer
+        * ourselves now, as our caller expects to receive a single
+        * buffer. cli_read_andx does it from the buffer received from
+        * the net, but with a short read we have to put it together
+        * from several reads.
         */
 
-       if ((size < 0xffff) && (size > num_bytes)) {
-               DEBUG(5, ("server announced more bytes than sent\n"));
-               return NT_STATUS_INVALID_NETWORK_RESPONSE;
+       if (state->buf == NULL) {
+               state->buf = talloc_array(state, uint8_t, state->size);
+               if (tevent_req_nomem(state->buf, req)) {
+                       return;
+               }
        }
+       memcpy(state->buf + state->received, buf, received);
+       state->received += received;
 
-       buf = (uint8_t *)smb_base(cli_req->inbuf) + SVAL(vwv+6, 0);
+       TALLOC_FREE(subreq);
 
-       if (trans_oob(smb_len(cli_req->inbuf), SVAL(vwv+6, 0), size)
-           || (buf < bytes)) {
-               DEBUG(5, ("server returned invalid read&x data offset\n"));
-               return NT_STATUS_INVALID_NETWORK_RESPONSE;
+       if (state->received >= state->size) {
+               tevent_req_done(req);
+               return;
+       }
+
+       subreq = cli_read_andx_send(state, state->ev, state->cli, state->fnum,
+                                   state->start_offset + state->received,
+                                   state->size - state->received);
+       if (tevent_req_nomem(subreq, req)) {
+               return;
        }
+       tevent_req_set_callback(subreq, cli_readall_done, req);
+}
+
+static NTSTATUS cli_readall_recv(struct tevent_req *req, ssize_t *received,
+                                uint8_t **rcvbuf)
+{
+       struct cli_readall_state *state = tevent_req_data(
+               req, struct cli_readall_state);
+       NTSTATUS status;
 
-       *rcvbuf = (uint8_t *)(smb_base(cli_req->inbuf) + SVAL(vwv + 6, 0));
-       *received = size;
+       if (tevent_req_is_nterror(req, &status)) {
+               return status;
+       }
+       *received = state->received;
+       *rcvbuf = state->buf;
        return NT_STATUS_OK;
 }
 
-/*
- * Parallel read support.
- *
- * cli_pull sends as many read&x requests as the server would allow via
- * max_mux at a time. When replies flow back in, the data is written into
- * the callback function "sink" in the right order.
- */
+struct cli_pull_chunk;
 
 struct cli_pull_state {
-       struct async_req *req;
-
-       struct event_context *ev;
+       struct tevent_context *ev;
        struct cli_state *cli;
        uint16_t fnum;
        off_t start_offset;
-       SMB_OFF_T size;
+       off_t size;
 
        NTSTATUS (*sink)(char *buf, size_t n, void *priv);
        void *priv;
 
        size_t chunk_size;
+       off_t next_offset;
+       off_t remaining;
 
        /*
-        * Outstanding requests
-        */
-       int num_reqs;
-       struct async_req **reqs;
-
-       /*
-        * For how many bytes did we send requests already?
-        */
-       SMB_OFF_T requested;
-
-       /*
-        * Next request index to push into "sink". This walks around the "req"
-        * array, taking care that the requests are pushed to "sink" in the
-        * right order. If necessary (i.e. replies don't come in in the right
-        * order), replies are held back in "reqs".
+        * How many bytes did we push into "sink"?
         */
-       int top_req;
+       off_t pushed;
 
        /*
-        * How many bytes did we push into "sink"?
+        * Outstanding requests
+        *
+        * The maximum is 256:
+        * - which would be a window of 256 MByte
+        *   for SMB2 with multi-credit
+        *   or smb1 unix extentions.
         */
-
-       SMB_OFF_T pushed;
+       uint16_t max_chunks;
+       uint16_t num_chunks;
+       uint16_t num_waiting;
+       struct cli_pull_chunk *chunks;
 };
 
-static char *cli_pull_print(TALLOC_CTX *mem_ctx, struct async_req *req)
-{
-       struct cli_pull_state *state = talloc_get_type_abort(
-               req->private_data, struct cli_pull_state);
-       char *result;
-
-       result = async_req_print(mem_ctx, req);
-       if (result == NULL) {
-               return NULL;
-       }
-
-       return talloc_asprintf_append_buffer(
-               result, "num_reqs=%d, top_req=%d",
-               state->num_reqs, state->top_req);
-}
+struct cli_pull_chunk {
+       struct cli_pull_chunk *prev, *next;
+       struct tevent_req *req;/* This is the main request! Not the subreq */
+       struct tevent_req *subreq;
+       off_t ofs;
+       uint8_t *buf;
+       size_t total_size;
+       size_t tmp_size;
+       bool done;
+};
 
-static void cli_pull_read_done(struct async_req *read_req);
+static void cli_pull_setup_chunks(struct tevent_req *req);
+static void cli_pull_chunk_ship(struct cli_pull_chunk *chunk);
+static void cli_pull_chunk_done(struct tevent_req *subreq);
 
 /*
- * Prepare an async pull request
+ * Parallel read support.
+ *
+ * cli_pull sends as many read&x requests as the server would allow via
+ * max_mux at a time. When replies flow back in, the data is written into
+ * the callback function "sink" in the right order.
  */
 
-struct async_req *cli_pull_send(TALLOC_CTX *mem_ctx,
-                               struct event_context *ev,
-                               struct cli_state *cli,
-                               uint16_t fnum, off_t start_offset,
-                               SMB_OFF_T size, size_t window_size,
-                               NTSTATUS (*sink)(char *buf, size_t n,
-                                                void *priv),
-                               void *priv)
+struct tevent_req *cli_pull_send(TALLOC_CTX *mem_ctx,
+                                struct tevent_context *ev,
+                                struct cli_state *cli,
+                                uint16_t fnum, off_t start_offset,
+                                off_t size, size_t window_size,
+                                NTSTATUS (*sink)(char *buf, size_t n,
+                                                 void *priv),
+                                void *priv)
 {
-       struct async_req *result;
+       struct tevent_req *req;
        struct cli_pull_state *state;
-       int i;
+       size_t page_size = 1024;
+       uint64_t tmp64;
 
-       if (!async_req_setup(mem_ctx, &result, &state,
-                            struct cli_pull_state)) {
+       req = tevent_req_create(mem_ctx, &state, struct cli_pull_state);
+       if (req == NULL) {
                return NULL;
        }
-       result->print = cli_pull_print;
-       state->req = result;
-
        state->cli = cli;
        state->ev = ev;
        state->fnum = fnum;
@@ -292,212 +471,321 @@ struct async_req *cli_pull_send(TALLOC_CTX *mem_ctx,
        state->size = size;
        state->sink = sink;
        state->priv = priv;
-
-       state->pushed = 0;
-       state->top_req = 0;
+       state->next_offset = start_offset;
+       state->remaining = size;
 
        if (size == 0) {
-               if (!async_post_ntstatus(result, ev, NT_STATUS_OK)) {
-                       goto failed;
-               }
-               return result;
+               tevent_req_done(req);
+               return tevent_req_post(req, ev);
        }
 
        state->chunk_size = cli_read_max_bufsize(cli);
+       if (state->chunk_size > page_size) {
+               state->chunk_size &= ~(page_size - 1);
+       }
 
-       state->num_reqs = MAX(window_size/state->chunk_size, 1);
-       state->num_reqs = MIN(state->num_reqs, cli->max_mux);
-
-       state->reqs = TALLOC_ZERO_ARRAY(state, struct async_req *,
-                                       state->num_reqs);
-       if (state->reqs == NULL) {
-               goto failed;
+       if (window_size == 0) {
+               /*
+                * We use 16 MByte as default window size.
+                */
+               window_size = 16 * 1024 * 1024;
        }
 
-       state->requested = 0;
+       tmp64 = window_size/state->chunk_size;
+       if ((window_size % state->chunk_size) > 0) {
+               tmp64 += 1;
+       }
+       tmp64 = MAX(tmp64, 1);
+       tmp64 = MIN(tmp64, 256);
+       state->max_chunks = tmp64;
 
-       for (i=0; i<state->num_reqs; i++) {
-               SMB_OFF_T size_left;
-               size_t request_thistime;
+       /*
+        * We defer the callback because of the complex
+        * substate/subfunction logic
+        */
+       tevent_req_defer_callback(req, ev);
 
-               if (state->requested >= size) {
-                       state->num_reqs = i;
-                       break;
-               }
+       cli_pull_setup_chunks(req);
+       if (!tevent_req_is_in_progress(req)) {
+               return tevent_req_post(req, ev);
+       }
 
-               size_left = size - state->requested;
-               request_thistime = MIN(size_left, state->chunk_size);
+       return req;
+}
 
-               state->reqs[i] = cli_read_andx_send(
-                       state->reqs, ev, cli, fnum,
-                       state->start_offset + state->requested,
-                       request_thistime);
+static void cli_pull_setup_chunks(struct tevent_req *req)
+{
+       struct cli_pull_state *state =
+               tevent_req_data(req,
+               struct cli_pull_state);
+       struct cli_pull_chunk *chunk, *next = NULL;
+       size_t i;
 
-               if (state->reqs[i] == NULL) {
-                       goto failed;
+       for (chunk = state->chunks; chunk; chunk = next) {
+               /*
+                * Note that chunk might be removed from this call.
+                */
+               next = chunk->next;
+               cli_pull_chunk_ship(chunk);
+               if (!tevent_req_is_in_progress(req)) {
+                       return;
                }
-
-               state->reqs[i]->async.fn = cli_pull_read_done;
-               state->reqs[i]->async.priv = result;
-
-               state->requested += request_thistime;
        }
-       return result;
 
-failed:
-       TALLOC_FREE(result);
-       return NULL;
-}
+       for (i = state->num_chunks; i < state->max_chunks; i++) {
 
-/*
- * Handle incoming read replies, push the data into sink and send out new
- * requests if necessary.
- */
+               if (state->num_waiting > 0) {
+                       return;
+               }
 
-static void cli_pull_read_done(struct async_req *read_req)
-{
-       struct async_req *pull_req = talloc_get_type_abort(
-               read_req->async.priv, struct async_req);
-       struct cli_pull_state *state = talloc_get_type_abort(
-               pull_req->private_data, struct cli_pull_state);
-       struct cli_request *read_state = talloc_get_type_abort(
-               read_req->private_data, struct cli_request);
-       NTSTATUS status;
+               if (state->remaining == 0) {
+                       break;
+               }
 
-       status = cli_read_andx_recv(read_req, &read_state->data.read.received,
-                                   &read_state->data.read.rcvbuf);
-       if (!NT_STATUS_IS_OK(status)) {
-               async_req_nterror(state->req, status);
+               chunk = talloc_zero(state, struct cli_pull_chunk);
+               if (tevent_req_nomem(chunk, req)) {
+                       return;
+               }
+               chunk->req = req;
+               chunk->ofs = state->next_offset;
+               chunk->total_size = MIN(state->remaining, state->chunk_size);
+               state->next_offset += chunk->total_size;
+               state->remaining -= chunk->total_size;
+
+               DLIST_ADD_END(state->chunks, chunk, NULL);
+               state->num_chunks++;
+               state->num_waiting++;
+
+               cli_pull_chunk_ship(chunk);
+               if (!tevent_req_is_in_progress(req)) {
+                       return;
+               }
+       }
+
+       if (state->remaining > 0) {
                return;
        }
 
-       /*
-        * This loop is the one to take care of out-of-order replies. All
-        * pending requests are in state->reqs, state->reqs[top_req] is the
-        * one that is to be pushed next. If however a request later than
-        * top_req is replied to, then we can't push yet. If top_req is
-        * replied to at a later point then, we need to push all the finished
-        * requests.
-        */
+       if (state->num_chunks > 0) {
+               return;
+       }
 
-       while (state->reqs[state->top_req] != NULL) {
-               struct cli_request *top_read;
+       tevent_req_done(req);
+}
 
-               DEBUG(11, ("cli_pull_read_done: top_req = %d\n",
-                          state->top_req));
+static void cli_pull_chunk_ship(struct cli_pull_chunk *chunk)
+{
+       struct tevent_req *req = chunk->req;
+       struct cli_pull_state *state =
+               tevent_req_data(req,
+               struct cli_pull_state);
+       bool ok;
+       off_t ofs;
+       size_t size;
 
-               if (state->reqs[state->top_req]->state < ASYNC_REQ_DONE) {
-                       DEBUG(11, ("cli_pull_read_done: top request not yet "
-                                  "done\n"));
+       if (chunk->done) {
+               NTSTATUS status;
+
+               if (chunk != state->chunks) {
+                       /*
+                        * this chunk is not the
+                        * first one in the list.
+                        *
+                        * which means we should not
+                        * push it into the sink yet.
+                        */
                        return;
                }
 
-               top_read = talloc_get_type_abort(
-                       state->reqs[state->top_req]->private_data,
-                       struct cli_request);
-
-               DEBUG(10, ("cli_pull_read_done: Pushing %d bytes, %d already "
-                          "pushed\n", (int)top_read->data.read.received,
-                          (int)state->pushed));
+               if (chunk->tmp_size == 0) {
+                       /*
+                        * we git a short read, we're done
+                        */
+                       tevent_req_done(req);
+                       return;
+               }
 
-               status = state->sink((char *)top_read->data.read.rcvbuf,
-                                    top_read->data.read.received,
+               status = state->sink((char *)chunk->buf,
+                                    chunk->tmp_size,
                                     state->priv);
-               if (!NT_STATUS_IS_OK(status)) {
-                       async_req_nterror(state->req, status);
+               if (tevent_req_nterror(req, status)) {
+                       return;
+               }
+               state->pushed += chunk->tmp_size;
+
+               if (chunk->tmp_size < chunk->total_size) {
+                       /*
+                        * we git a short read, we're done
+                        */
+                       tevent_req_done(req);
                        return;
                }
-               state->pushed += top_read->data.read.received;
 
-               TALLOC_FREE(state->reqs[state->top_req]);
+               DLIST_REMOVE(state->chunks, chunk);
+               SMB_ASSERT(state->num_chunks > 0);
+               state->num_chunks--;
+               TALLOC_FREE(chunk);
+
+               return;
+       }
+
+       if (chunk->subreq != NULL) {
+               return;
+       }
+
+       SMB_ASSERT(state->num_waiting > 0);
+
+       ofs = chunk->ofs + chunk->tmp_size;
+       size = chunk->total_size - chunk->tmp_size;
+
+       ok = smb1cli_conn_req_possible(state->cli->conn);
+       if (!ok) {
+               return;
+       }
 
-               if (state->requested < state->size) {
-                       struct async_req *new_req;
-                       SMB_OFF_T size_left;
-                       size_t request_thistime;
+       chunk->subreq = cli_read_andx_send(chunk,
+                                          state->ev,
+                                          state->cli,
+                                          state->fnum,
+                                          ofs,
+                                          size);
+       if (tevent_req_nomem(chunk->subreq, req)) {
+               return;
+       }
+       tevent_req_set_callback(chunk->subreq,
+                               cli_pull_chunk_done,
+                               chunk);
 
-                       size_left = state->size - state->requested;
-                       request_thistime = MIN(size_left, state->chunk_size);
+       state->num_waiting--;
+       return;
+}
 
-                       DEBUG(10, ("cli_pull_read_done: Requesting %d bytes "
-                                  "at %d, position %d\n",
-                                  (int)request_thistime,
-                                  (int)(state->start_offset
-                                        + state->requested),
-                                  state->top_req));
+static void cli_pull_chunk_done(struct tevent_req *subreq)
+{
+       struct cli_pull_chunk *chunk =
+               tevent_req_callback_data(subreq,
+               struct cli_pull_chunk);
+       struct tevent_req *req = chunk->req;
+       struct cli_pull_state *state =
+               tevent_req_data(req,
+               struct cli_pull_state);
+       NTSTATUS status;
+       size_t expected = chunk->total_size - chunk->tmp_size;
+       ssize_t received;
+       uint8_t *buf = NULL;
 
-                       new_req = cli_read_andx_send(
-                               state->reqs, state->ev, state->cli,
-                               state->fnum,
-                               state->start_offset + state->requested,
-                               request_thistime);
+       chunk->subreq = NULL;
 
-                       if (async_req_nomem(new_req, state->req)) {
-                               return;
-                       }
+       status = cli_read_andx_recv(subreq, &received, &buf);
+       if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
+               received = 0;
+               status = NT_STATUS_OK;
+       }
+       if (tevent_req_nterror(req, status)) {
+               return;
+       }
 
-                       new_req->async.fn = cli_pull_read_done;
-                       new_req->async.priv = pull_req;
+       if (received > expected) {
+               tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
+               return;
+       }
+
+       if (received == 0) {
+               /*
+                * We got EOF we're done
+                */
+               chunk->done = true;
+               cli_pull_setup_chunks(req);
+               return;
+       }
 
-                       state->reqs[state->top_req] = new_req;
-                       state->requested += request_thistime;
+       if (received == chunk->total_size) {
+               /*
+                * We got it in the first run.
+                *
+                * We don't call TALLOC_FREE(subreq)
+                * here and keep the returned buffer.
+                */
+               chunk->buf = buf;
+       } else if (chunk->buf == NULL) {
+               chunk->buf = talloc_array(chunk, uint8_t, chunk->total_size);
+               if (tevent_req_nomem(chunk->buf, req)) {
+                       return;
                }
+       }
+
+       if (received != chunk->total_size) {
+               uint8_t *p = chunk->buf + chunk->tmp_size;
+               memcpy(p, buf, received);
+               TALLOC_FREE(subreq);
+       }
 
-               state->top_req = (state->top_req+1) % state->num_reqs;
+       chunk->tmp_size += received;
+
+       if (chunk->tmp_size == chunk->total_size) {
+               chunk->done = true;
+       } else {
+               state->num_waiting++;
        }
 
-       async_req_done(pull_req);
+       cli_pull_setup_chunks(req);
 }
 
-NTSTATUS cli_pull_recv(struct async_req *req, SMB_OFF_T *received)
+NTSTATUS cli_pull_recv(struct tevent_req *req, off_t *received)
 {
-       struct cli_pull_state *state = talloc_get_type_abort(
-               req->private_data, struct cli_pull_state);
+       struct cli_pull_state *state = tevent_req_data(
+               req, struct cli_pull_state);
        NTSTATUS status;
 
-       if (async_req_is_nterror(req, &status)) {
+       if (tevent_req_is_nterror(req, &status)) {
+               tevent_req_received(req);
                return status;
        }
        *received = state->pushed;
+       tevent_req_received(req);
        return NT_STATUS_OK;
 }
 
 NTSTATUS cli_pull(struct cli_state *cli, uint16_t fnum,
-                 off_t start_offset, SMB_OFF_T size, size_t window_size,
+                 off_t start_offset, off_t size, size_t window_size,
                  NTSTATUS (*sink)(char *buf, size_t n, void *priv),
-                 void *priv, SMB_OFF_T *received)
+                 void *priv, off_t *received)
 {
        TALLOC_CTX *frame = talloc_stackframe();
-       struct event_context *ev;
-       struct async_req *req;
-       NTSTATUS result = NT_STATUS_NO_MEMORY;
+       struct tevent_context *ev;
+       struct tevent_req *req;
+       NTSTATUS status = NT_STATUS_OK;
 
-       if (cli->fd_event != NULL) {
+       if (smbXcli_conn_has_async_calls(cli->conn)) {
                /*
                 * Can't use sync call while an async call is in flight
                 */
-               return NT_STATUS_INVALID_PARAMETER;
+               status = NT_STATUS_INVALID_PARAMETER;
+               goto fail;
        }
 
-       ev = event_context_init(frame);
+       ev = samba_tevent_context_init(frame);
        if (ev == NULL) {
-               goto nomem;
+               status = NT_STATUS_NO_MEMORY;
+               goto fail;
        }
 
        req = cli_pull_send(frame, ev, cli, fnum, start_offset, size,
                            window_size, sink, priv);
        if (req == NULL) {
-               goto nomem;
+               status = NT_STATUS_NO_MEMORY;
+               goto fail;
        }
 
-       while (req->state < ASYNC_REQ_DONE) {
-               event_loop_once(ev);
+       if (!tevent_req_poll(req, ev)) {
+               status = map_nt_error_from_unix(errno);
+               goto fail;
        }
 
-       result = cli_pull_recv(req, received);
nomem:
+       status = cli_pull_recv(req, received);
fail:
        TALLOC_FREE(frame);
-       return result;
+       return status;
 }
 
 static NTSTATUS cli_read_sink(char *buf, size_t n, void *priv)
@@ -508,254 +796,132 @@ static NTSTATUS cli_read_sink(char *buf, size_t n, void *priv)
        return NT_STATUS_OK;
 }
 
-ssize_t cli_read(struct cli_state *cli, int fnum, char *buf,
-                off_t offset, size_t size)
+NTSTATUS cli_read(struct cli_state *cli, uint16_t fnum,
+                char *buf, off_t offset, size_t size,
+                size_t *nread)
 {
        NTSTATUS status;
-       SMB_OFF_T ret;
+       off_t ret;
 
        status = cli_pull(cli, fnum, offset, size, size,
                          cli_read_sink, &buf, &ret);
        if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-               return -1;
-       }
-       return ret;
-}
-
-/****************************************************************************
- Issue a single SMBwrite and don't wait for a reply.
-****************************************************************************/
-
-static bool cli_issue_write(struct cli_state *cli,
-                               int fnum,
-                               off_t offset,
-                               uint16 mode,
-                               const char *buf,
-                               size_t size,
-                               int i)
-{
-       char *p;
-       bool large_writex = false;
-       /* We can only do direct writes if not signing and not encrypting. */
-       bool direct_writes = !client_is_signing_on(cli) && !cli_encryption_on(cli);
-
-       if (!direct_writes && size + 1 > cli->bufsize) {
-               cli->outbuf = (char *)SMB_REALLOC(cli->outbuf, size + 1024);
-               if (!cli->outbuf) {
-                       return False;
-               }
-               cli->inbuf = (char *)SMB_REALLOC(cli->inbuf, size + 1024);
-               if (cli->inbuf == NULL) {
-                       SAFE_FREE(cli->outbuf);
-                       return False;
-               }
-               cli->bufsize = size + 1024;
-       }
-
-       memset(cli->outbuf,'\0',smb_size);
-       memset(cli->inbuf,'\0',smb_size);
-
-       if (cli->capabilities & CAP_LARGE_FILES) {
-               large_writex = True;
-       }
-
-       if (large_writex) {
-               cli_set_message(cli->outbuf,14,0,True);
-       } else {
-               cli_set_message(cli->outbuf,12,0,True);
-       }
-
-       SCVAL(cli->outbuf,smb_com,SMBwriteX);
-       SSVAL(cli->outbuf,smb_tid,cli->cnum);
-       cli_setup_packet(cli);
-
-       SCVAL(cli->outbuf,smb_vwv0,0xFF);
-       SSVAL(cli->outbuf,smb_vwv2,fnum);
-
-       SIVAL(cli->outbuf,smb_vwv3,offset);
-       SIVAL(cli->outbuf,smb_vwv5,0);
-       SSVAL(cli->outbuf,smb_vwv7,mode);
-
-       SSVAL(cli->outbuf,smb_vwv8,(mode & 0x0008) ? size : 0);
-       /*
-        * According to CIFS-TR-1p00, this following field should only
-        * be set if CAP_LARGE_WRITEX is set. We should check this
-        * locally. However, this check might already have been
-        * done by our callers.
-        */
-       SSVAL(cli->outbuf,smb_vwv9,(size>>16));
-       SSVAL(cli->outbuf,smb_vwv10,size);
-       /* +1 is pad byte. */
-       SSVAL(cli->outbuf,smb_vwv11,
-             smb_buf(cli->outbuf) - smb_base(cli->outbuf) + 1);
-
-       if (large_writex) {
-               SIVAL(cli->outbuf,smb_vwv12,(((uint64_t)offset)>>32) & 0xffffffff);
+               return status;
        }
 
-       p = smb_base(cli->outbuf) + SVAL(cli->outbuf,smb_vwv11) -1;
-       *p++ = '\0'; /* pad byte. */
-       if (!direct_writes) {
-               memcpy(p, buf, size);
-       }
-       if (size > 0x1FFFF) {
-               /* This is a POSIX 14 word large write. */
-               set_message_bcc(cli->outbuf, 0); /* Set bcc to zero. */
-               _smb_setlen_large(cli->outbuf,smb_size + 28 + 1 /* pad */ + size - 4);
-       } else {
-               cli_setup_bcc(cli, p+size);
+       if (nread) {
+               *nread = ret;
        }
 
-       SSVAL(cli->outbuf,smb_mid,cli->mid + i);
-
-       show_msg(cli->outbuf);
-       if (direct_writes) {
-               /* For direct writes we now need to write the data
-                * directly out of buf. */
-               return cli_send_smb_direct_writeX(cli, buf, size);
-       } else {
-               return cli_send_smb(cli);
-       }
+       return NT_STATUS_OK;
 }
 
 /****************************************************************************
-  write to a file
-  write_mode: 0x0001 disallow write cacheing
-              0x0002 return bytes remaining
-              0x0004 use raw named pipe protocol
-              0x0008 start of message mode named pipe protocol
+  write to a file using a SMBwrite and not bypassing 0 byte writes
 ****************************************************************************/
 
-ssize_t cli_write(struct cli_state *cli,
-                int fnum, uint16 write_mode,
-                const char *buf, off_t offset, size_t size)
+NTSTATUS cli_smbwrite(struct cli_state *cli, uint16_t fnum, char *buf,
+                     off_t offset, size_t size1, size_t *ptotal)
 {
-       ssize_t bwritten = 0;
-       unsigned int issued = 0;
-       unsigned int received = 0;
-       int mpx = 1;
-       size_t writesize;
-       int blocks;
-
-       if(cli->max_mux > 1) {
-               mpx = cli->max_mux-1;
-       } else {
-               mpx = 1;
-       }
-
-       writesize = cli_write_max_bufsize(cli, write_mode);
-
-       blocks = (size + (writesize-1)) / writesize;
+       uint8_t *bytes;
+       ssize_t total = 0;
 
-       while (received < blocks) {
+       /*
+        * 3 bytes prefix
+        */
 
-               while ((issued - received < mpx) && (issued < blocks)) {
-                       ssize_t bsent = issued * writesize;
-                       ssize_t size1 = MIN(writesize, size - bsent);
+       bytes = talloc_array(talloc_tos(), uint8_t, 3);
+       if (bytes == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
+       bytes[0] = 1;
 
-                       if (!cli_issue_write(cli, fnum, offset + bsent,
-                                       write_mode,
-                                       buf + bsent,
-                                       size1, issued))
-                               return -1;
-                       issued++;
+       do {
+               uint32_t usable_space = cli_state_available_size(cli, 48);
+               size_t size = MIN(size1, usable_space);
+               struct tevent_req *req;
+               uint16_t vwv[5];
+               uint16_t *ret_vwv;
+               NTSTATUS status;
+
+               SSVAL(vwv+0, 0, fnum);
+               SSVAL(vwv+1, 0, size);
+               SIVAL(vwv+2, 0, offset);
+               SSVAL(vwv+4, 0, 0);
+
+               bytes = talloc_realloc(talloc_tos(), bytes, uint8_t,
+                                            size+3);
+               if (bytes == NULL) {
+                       return NT_STATUS_NO_MEMORY;
                }
+               SSVAL(bytes, 1, size);
+               memcpy(bytes + 3, buf + total, size);
 
-               if (!cli_receive_smb(cli)) {
-                       return bwritten;
+               status = cli_smb(talloc_tos(), cli, SMBwrite, 0, 5, vwv,
+                                size+3, bytes, &req, 1, NULL, &ret_vwv,
+                                NULL, NULL);
+               if (!NT_STATUS_IS_OK(status)) {
+                       TALLOC_FREE(bytes);
+                       return status;
                }
 
-               received++;
-
-               if (cli_is_error(cli))
+               size = SVAL(ret_vwv+0, 0);
+               TALLOC_FREE(req);
+               if (size == 0) {
                        break;
-
-               bwritten += SVAL(cli->inbuf, smb_vwv2);
-               if (writesize > 0xFFFF) {
-                       bwritten += (((int)(SVAL(cli->inbuf, smb_vwv4)))<<16);
                }
-       }
-
-       while (received < issued && cli_receive_smb(cli)) {
-               received++;
-       }
-
-       return bwritten;
-}
-
-/****************************************************************************
-  write to a file using a SMBwrite and not bypassing 0 byte writes
-****************************************************************************/
-
-ssize_t cli_smbwrite(struct cli_state *cli,
-                    int fnum, char *buf, off_t offset, size_t size1)
-{
-       char *p;
-       ssize_t total = 0;
-
-       do {
-               size_t size = MIN(size1, cli->max_xmit - 48);
-
-               memset(cli->outbuf,'\0',smb_size);
-               memset(cli->inbuf,'\0',smb_size);
-
-               cli_set_message(cli->outbuf,5, 0,True);
-
-               SCVAL(cli->outbuf,smb_com,SMBwrite);
-               SSVAL(cli->outbuf,smb_tid,cli->cnum);
-               cli_setup_packet(cli);
-
-               SSVAL(cli->outbuf,smb_vwv0,fnum);
-               SSVAL(cli->outbuf,smb_vwv1,size);
-               SIVAL(cli->outbuf,smb_vwv2,offset);
-               SSVAL(cli->outbuf,smb_vwv4,0);
-
-               p = smb_buf(cli->outbuf);
-               *p++ = 1;
-               SSVAL(p, 0, size); p += 2;
-               memcpy(p, buf + total, size); p += size;
-
-               cli_setup_bcc(cli, p);
-
-               if (!cli_send_smb(cli))
-                       return -1;
-
-               if (!cli_receive_smb(cli))
-                       return -1;
-
-               if (cli_is_error(cli))
-                       return -1;
-
-               size = SVAL(cli->inbuf,smb_vwv0);
-               if (size == 0)
-                       break;
-
                size1 -= size;
                total += size;
                offset += size;
 
        } while (size1);
 
-       return total;
+       TALLOC_FREE(bytes);
+
+       if (ptotal != NULL) {
+               *ptotal = total;
+       }
+       return NT_STATUS_OK;
 }
 
 /*
  * Send a write&x request
  */
 
-struct async_req *cli_write_andx_send(TALLOC_CTX *mem_ctx,
-                                     struct event_context *ev,
-                                     struct cli_state *cli, uint16_t fnum,
-                                     uint16_t mode, const uint8_t *buf,
-                                     off_t offset, size_t size)
+struct cli_write_andx_state {
+       size_t size;
+       uint16_t vwv[14];
+       size_t written;
+       uint8_t pad;
+       struct iovec iov[2];
+};
+
+static void cli_write_andx_done(struct tevent_req *subreq);
+
+struct tevent_req *cli_write_andx_create(TALLOC_CTX *mem_ctx,
+                                        struct tevent_context *ev,
+                                        struct cli_state *cli, uint16_t fnum,
+                                        uint16_t mode, const uint8_t *buf,
+                                        off_t offset, size_t size,
+                                        struct tevent_req **reqs_before,
+                                        int num_reqs_before,
+                                        struct tevent_req **psmbreq)
 {
-       bool bigoffset = ((cli->capabilities & CAP_LARGE_FILES) != 0);
+       struct tevent_req *req, *subreq;
+       struct cli_write_andx_state *state;
+       bool bigoffset = ((smb1cli_conn_capabilities(cli->conn) & CAP_LARGE_FILES) != 0);
        uint8_t wct = bigoffset ? 14 : 12;
-       size_t max_write = cli_write_max_bufsize(cli, mode);
-       uint16_t vwv[14];
+       size_t max_write = cli_write_max_bufsize(cli, mode, wct);
+       uint16_t *vwv;
 
-       size = MIN(size, max_write);
+       req = tevent_req_create(mem_ctx, &state, struct cli_write_andx_state);
+       if (req == NULL) {
+               return NULL;
+       }
+
+       state->size = MIN(size, max_write);
+
+       vwv = state->vwv;
 
        SCVAL(vwv+0, 0, 0xFF);
        SCVAL(vwv+0, 1, 0);
@@ -765,11 +931,11 @@ struct async_req *cli_write_andx_send(TALLOC_CTX *mem_ctx,
        SIVAL(vwv+5, 0, 0);
        SSVAL(vwv+7, 0, mode);
        SSVAL(vwv+8, 0, 0);
-       SSVAL(vwv+9, 0, (size>>16));
-       SSVAL(vwv+10, 0, size);
+       SSVAL(vwv+9, 0, (state->size>>16));
+       SSVAL(vwv+10, 0, state->size);
 
        SSVAL(vwv+11, 0,
-             cli_wct_ofs(cli)
+             smb1cli_req_wct_ofs(reqs_before, num_reqs_before)
              + 1               /* the wct field */
              + wct * 2         /* vwv */
              + 2               /* num_bytes field */
@@ -779,42 +945,94 @@ struct async_req *cli_write_andx_send(TALLOC_CTX *mem_ctx,
                SIVAL(vwv+12, 0, (((uint64_t)offset)>>32) & 0xffffffff);
        }
 
-       return cli_request_send(mem_ctx, ev, cli, SMBwriteX, 0, wct, vwv,
-                               2, size, buf);
+       state->pad = 0;
+       state->iov[0].iov_base = (void *)&state->pad;
+       state->iov[0].iov_len = 1;
+       state->iov[1].iov_base = discard_const_p(void, buf);
+       state->iov[1].iov_len = state->size;
+
+       subreq = cli_smb_req_create(state, ev, cli, SMBwriteX, 0, wct, vwv,
+                                   2, state->iov);
+       if (tevent_req_nomem(subreq, req)) {
+               return tevent_req_post(req, ev);
+       }
+       tevent_req_set_callback(subreq, cli_write_andx_done, req);
+       *psmbreq = subreq;
+       return req;
 }
 
-NTSTATUS cli_write_andx_recv(struct async_req *req, size_t *pwritten)
+struct tevent_req *cli_write_andx_send(TALLOC_CTX *mem_ctx,
+                                      struct tevent_context *ev,
+                                      struct cli_state *cli, uint16_t fnum,
+                                      uint16_t mode, const uint8_t *buf,
+                                      off_t offset, size_t size)
 {
-       uint8_t wct;
-       uint16_t *vwv;
-       uint16_t num_bytes;
-       uint8_t *bytes;
+       struct tevent_req *req, *subreq;
        NTSTATUS status;
-       size_t written;
 
-       if (async_req_is_nterror(req, &status)) {
-               return status;
+       req = cli_write_andx_create(mem_ctx, ev, cli, fnum, mode, buf, offset,
+                                   size, NULL, 0, &subreq);
+       if (req == NULL) {
+               return NULL;
        }
 
-       status = cli_pull_reply(req, &wct, &vwv, &num_bytes, &bytes);
+       status = smb1cli_req_chain_submit(&subreq, 1);
+       if (tevent_req_nterror(req, status)) {
+               return tevent_req_post(req, ev);
+       }
+       return req;
+}
 
+static void cli_write_andx_done(struct tevent_req *subreq)
+{
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
+       struct cli_write_andx_state *state = tevent_req_data(
+               req, struct cli_write_andx_state);
+       uint8_t wct;
+       uint16_t *vwv;
+       NTSTATUS status;
+
+       status = cli_smb_recv(subreq, state, NULL, 6, &wct, &vwv,
+                             NULL, NULL);
+       TALLOC_FREE(subreq);
        if (NT_STATUS_IS_ERR(status)) {
-               return status;
+               tevent_req_nterror(req, status);
+               return;
        }
-
-       if (wct < 6) {
-               return NT_STATUS_INVALID_NETWORK_RESPONSE;
+       state->written = SVAL(vwv+2, 0);
+       if (state->size > UINT16_MAX) {
+               /*
+                * It is important that we only set the
+                * high bits only if we asked for a large write.
+                *
+                * OS/2 print shares get this wrong and may send
+                * invalid values.
+                *
+                * See bug #5326.
+                */
+               state->written |= SVAL(vwv+4, 0)<<16;
        }
+       tevent_req_done(req);
+}
 
-       written = SVAL(vwv+2, 0);
-       written |= SVAL(vwv+4, 0)<<16;
-       *pwritten = written;
+NTSTATUS cli_write_andx_recv(struct tevent_req *req, size_t *pwritten)
+{
+       struct cli_write_andx_state *state = tevent_req_data(
+               req, struct cli_write_andx_state);
+       NTSTATUS status;
 
+       if (tevent_req_is_nterror(req, &status)) {
+               return status;
+       }
+       if (pwritten != 0) {
+               *pwritten = state->written;
+       }
        return NT_STATUS_OK;
 }
 
 struct cli_writeall_state {
-       struct event_context *ev;
+       struct tevent_context *ev;
        struct cli_state *cli;
        uint16_t fnum;
        uint16_t mode;
@@ -824,22 +1042,21 @@ struct cli_writeall_state {
        size_t written;
 };
 
-static void cli_writeall_written(struct async_req *req);
+static void cli_writeall_written(struct tevent_req *req);
 
-static struct async_req *cli_writeall_send(TALLOC_CTX *mem_ctx,
-                                          struct event_context *ev,
-                                          struct cli_state *cli,
-                                          uint16_t fnum,
-                                          uint16_t mode,
-                                          const uint8_t *buf,
-                                          off_t offset, size_t size)
+static struct tevent_req *cli_writeall_send(TALLOC_CTX *mem_ctx,
+                                           struct tevent_context *ev,
+                                           struct cli_state *cli,
+                                           uint16_t fnum,
+                                           uint16_t mode,
+                                           const uint8_t *buf,
+                                           off_t offset, size_t size)
 {
-       struct async_req *result;
-       struct async_req *subreq;
+       struct tevent_req *req, *subreq;
        struct cli_writeall_state *state;
 
-       if (!async_req_setup(mem_ctx, &result, &state,
-                            struct cli_writeall_state)) {
+       req = tevent_req_create(mem_ctx, &state, struct cli_writeall_state);
+       if (req == NULL) {
                return NULL;
        }
        state->ev = ev;
@@ -854,46 +1071,39 @@ static struct async_req *cli_writeall_send(TALLOC_CTX *mem_ctx,
        subreq = cli_write_andx_send(state, state->ev, state->cli, state->fnum,
                                     state->mode, state->buf, state->offset,
                                     state->size);
-       if (subreq == NULL) {
-               goto fail;
+       if (tevent_req_nomem(subreq, req)) {
+               return tevent_req_post(req, ev);
        }
-
-       subreq->async.fn = cli_writeall_written;
-       subreq->async.priv = result;
-       return result;
-
- fail:
-       TALLOC_FREE(result);
-       return NULL;
+       tevent_req_set_callback(subreq, cli_writeall_written, req);
+       return req;
 }
 
-static void cli_writeall_written(struct async_req *subreq)
+static void cli_writeall_written(struct tevent_req *subreq)
 {
-       struct async_req *req = talloc_get_type_abort(
-               subreq->async.priv, struct async_req);
-       struct cli_writeall_state *state = talloc_get_type_abort(
-               req->private_data, struct cli_writeall_state);
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
+       struct cli_writeall_state *state = tevent_req_data(
+               req, struct cli_writeall_state);
        NTSTATUS status;
        size_t written, to_write;
 
        status = cli_write_andx_recv(subreq, &written);
        TALLOC_FREE(subreq);
-       if (!NT_STATUS_IS_OK(status)) {
-               async_req_nterror(req, status);
+       if (tevent_req_nterror(req, status)) {
                return;
        }
 
        state->written += written;
 
        if (state->written > state->size) {
-               async_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
+               tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
                return;
        }
 
        to_write = state->size - state->written;
 
        if (to_write == 0) {
-               async_req_done(req);
+               tevent_req_done(req);
                return;
        }
 
@@ -901,69 +1111,125 @@ static void cli_writeall_written(struct async_req *subreq)
                                     state->mode,
                                     state->buf + state->written,
                                     state->offset + state->written, to_write);
-       if (subreq == NULL) {
-               async_req_nterror(req, NT_STATUS_NO_MEMORY);
+       if (tevent_req_nomem(subreq, req)) {
                return;
        }
+       tevent_req_set_callback(subreq, cli_writeall_written, req);
+}
 
-       subreq->async.fn = cli_writeall_written;
-       subreq->async.priv = req;
+static NTSTATUS cli_writeall_recv(struct tevent_req *req,
+                                 size_t *pwritten)
+{
+       struct cli_writeall_state *state = tevent_req_data(
+               req, struct cli_writeall_state);
+       NTSTATUS status;
+
+       if (tevent_req_is_nterror(req, &status)) {
+               return status;
+       }
+       if (pwritten != NULL) {
+               *pwritten = state->written;
+       }
+       return NT_STATUS_OK;
 }
 
-static NTSTATUS cli_writeall_recv(struct async_req *req)
+NTSTATUS cli_writeall(struct cli_state *cli, uint16_t fnum, uint16_t mode,
+                     const uint8_t *buf, off_t offset, size_t size,
+                     size_t *pwritten)
 {
-       return async_req_simple_recv_ntstatus(req);
+       TALLOC_CTX *frame = talloc_stackframe();
+       struct tevent_context *ev;
+       struct tevent_req *req;
+       NTSTATUS status = NT_STATUS_NO_MEMORY;
+
+       if (smbXcli_conn_has_async_calls(cli->conn)) {
+               /*
+                * Can't use sync call while an async call is in flight
+                */
+               status = NT_STATUS_INVALID_PARAMETER;
+               goto fail;
+       }
+       ev = samba_tevent_context_init(frame);
+       if (ev == NULL) {
+               goto fail;
+       }
+       req = cli_writeall_send(frame, ev, cli, fnum, mode, buf, offset, size);
+       if (req == NULL) {
+               goto fail;
+       }
+       if (!tevent_req_poll(req, ev)) {
+               status = map_nt_error_from_unix(errno);
+               goto fail;
+       }
+       status = cli_writeall_recv(req, pwritten);
+ fail:
+       TALLOC_FREE(frame);
+       return status;
 }
 
-struct cli_push_state {
-       struct async_req *req;
+struct cli_push_chunk;
 
-       struct event_context *ev;
+struct cli_push_state {
+       struct tevent_context *ev;
        struct cli_state *cli;
        uint16_t fnum;
        uint16_t mode;
        off_t start_offset;
-       size_t window_size;
 
        size_t (*source)(uint8_t *buf, size_t n, void *priv);
        void *priv;
 
-       size_t chunk_size;
-
-       size_t sent;
        bool eof;
 
+       size_t chunk_size;
+       off_t next_offset;
+
        /*
         * Outstanding requests
+        *
+        * The maximum is 256:
+        * - which would be a window of 256 MByte
+        *   for SMB2 with multi-credit
+        *   or smb1 unix extentions.
         */
-       int num_reqs;
-       struct async_req **reqs;
-
-       int pending;
+       uint16_t max_chunks;
+       uint16_t num_chunks;
+       uint16_t num_waiting;
+       struct cli_push_chunk *chunks;
+};
 
+struct cli_push_chunk {
+       struct cli_push_chunk *prev, *next;
+       struct tevent_req *req;/* This is the main request! Not the subreq */
+       struct tevent_req *subreq;
+       off_t ofs;
        uint8_t *buf;
+       size_t total_size;
+       size_t tmp_size;
+       bool done;
 };
 
-static void cli_push_written(struct async_req *req);
-
-struct async_req *cli_push_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
-                               struct cli_state *cli,
-                               uint16_t fnum, uint16_t mode,
-                               off_t start_offset, size_t window_size,
-                               size_t (*source)(uint8_t *buf, size_t n,
-                                                void *priv),
-                               void *priv)
+static void cli_push_setup_chunks(struct tevent_req *req);
+static void cli_push_chunk_ship(struct cli_push_chunk *chunk);
+static void cli_push_chunk_done(struct tevent_req *subreq);
+
+struct tevent_req *cli_push_send(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
+                                struct cli_state *cli,
+                                uint16_t fnum, uint16_t mode,
+                                off_t start_offset, size_t window_size,
+                                size_t (*source)(uint8_t *buf, size_t n,
+                                                 void *priv),
+                                void *priv)
 {
-       struct async_req *result;
+       struct tevent_req *req;
        struct cli_push_state *state;
-       int i;
+       size_t page_size = 1024;
+       uint64_t tmp64;
 
-       if (!async_req_setup(mem_ctx, &result, &state,
-                            struct cli_push_state)) {
+       req = tevent_req_create(mem_ctx, &state, struct cli_push_state);
+       if (req == NULL) {
                return NULL;
        }
-       state->req = result;
-
        state->cli = cli;
        state->ev = ev;
        state->fnum = fnum;
@@ -971,130 +1237,214 @@ struct async_req *cli_push_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
        state->mode = mode;
        state->source = source;
        state->priv = priv;
-       state->eof = false;
-       state->sent = 0;
-       state->pending = 0;
+       state->next_offset = start_offset;
 
-       state->chunk_size = cli_write_max_bufsize(cli, mode);
+       state->chunk_size = cli_write_max_bufsize(cli, mode, 14);
+       if (state->chunk_size > page_size) {
+               state->chunk_size &= ~(page_size - 1);
+       }
 
-       state->num_reqs = MAX(window_size/state->chunk_size, 1);
-       state->num_reqs = MIN(state->num_reqs, cli->max_mux);
+       if (window_size == 0) {
+               /*
+                * We use 16 MByte as default window size.
+                */
+               window_size = 16 * 1024 * 1024;
+       }
 
-       state->reqs = TALLOC_ZERO_ARRAY(state, struct async_req *,
-                                       state->num_reqs);
-       if (state->reqs == NULL) {
-               goto failed;
+       tmp64 = window_size/state->chunk_size;
+       if ((window_size % state->chunk_size) > 0) {
+               tmp64 += 1;
        }
+       tmp64 = MAX(tmp64, 1);
+       tmp64 = MIN(tmp64, 256);
+       state->max_chunks = tmp64;
 
-       state->buf = TALLOC_ARRAY(
-               state, uint8_t, state->chunk_size * state->num_reqs);
-       if (state->buf == NULL) {
-               goto failed;
+       /*
+        * We defer the callback because of the complex
+        * substate/subfunction logic
+        */
+       tevent_req_defer_callback(req, ev);
+
+       cli_push_setup_chunks(req);
+       if (!tevent_req_is_in_progress(req)) {
+               return tevent_req_post(req, ev);
        }
 
-       for (i=0; i<state->num_reqs; i++) {
-               size_t to_write;
-               uint8_t *buf = state->buf + i*state->chunk_size;
+       return req;
+}
 
-               to_write = state->source(buf, state->chunk_size, state->priv);
-               if (to_write == 0) {
-                       state->eof = true;
+static void cli_push_setup_chunks(struct tevent_req *req)
+{
+       struct cli_push_state *state =
+               tevent_req_data(req,
+               struct cli_push_state);
+       struct cli_push_chunk *chunk, *next = NULL;
+       size_t i;
+
+       for (chunk = state->chunks; chunk; chunk = next) {
+               /*
+                * Note that chunk might be removed from this call.
+                */
+               next = chunk->next;
+               cli_push_chunk_ship(chunk);
+               if (!tevent_req_is_in_progress(req)) {
+                       return;
+               }
+       }
+
+       for (i = state->num_chunks; i < state->max_chunks; i++) {
+
+               if (state->num_waiting > 0) {
+                       return;
+               }
+
+               if (state->eof) {
                        break;
                }
 
-               state->reqs[i] = cli_writeall_send(
-                       state->reqs, state->ev, state->cli, state->fnum,
-                       state->mode, buf, state->start_offset + state->sent,
-                       to_write);
-               if (state->reqs[i] == NULL) {
-                       goto failed;
+               chunk = talloc_zero(state, struct cli_push_chunk);
+               if (tevent_req_nomem(chunk, req)) {
+                       return;
                }
+               chunk->req = req;
+               chunk->ofs = state->next_offset;
+               chunk->buf = talloc_array(chunk,
+                                         uint8_t,
+                                         state->chunk_size);
+               if (tevent_req_nomem(chunk->buf, req)) {
+                       return;
+               }
+               chunk->total_size = state->source(chunk->buf,
+                                                 state->chunk_size,
+                                                 state->priv);
+               if (chunk->total_size == 0) {
+                       /* nothing to send */
+                       talloc_free(chunk);
+                       state->eof = true;
+                       break;
+               }
+               state->next_offset += chunk->total_size;
 
-               state->reqs[i]->async.fn = cli_push_written;
-               state->reqs[i]->async.priv = state;
+               DLIST_ADD_END(state->chunks, chunk, NULL);
+               state->num_chunks++;
+               state->num_waiting++;
 
-               state->sent += to_write;
-               state->pending += 1;
+               cli_push_chunk_ship(chunk);
+               if (!tevent_req_is_in_progress(req)) {
+                       return;
+               }
        }
 
-       if (i == 0) {
-               if (!async_post_ntstatus(result, ev, NT_STATUS_OK)) {
-                       goto failed;
-               }
-               return result;
+       if (!state->eof) {
+               return;
        }
 
-       return result;
+       if (state->num_chunks > 0) {
+               return;
+       }
 
- failed:
-       TALLOC_FREE(result);
-       return NULL;
+       tevent_req_done(req);
 }
 
-static void cli_push_written(struct async_req *req)
+static void cli_push_chunk_ship(struct cli_push_chunk *chunk)
 {
-       struct cli_push_state *state = talloc_get_type_abort(
-               req->async.priv, struct cli_push_state);
-       NTSTATUS status;
-       int i;
-       uint8_t *buf;
-       size_t to_write;
+       struct tevent_req *req = chunk->req;
+       struct cli_push_state *state =
+               tevent_req_data(req,
+               struct cli_push_state);
+       bool ok;
+       const uint8_t *buf;
+       off_t ofs;
+       size_t size;
 
-       for (i=0; i<state->num_reqs; i++) {
-               if (state->reqs[i] == req) {
-                       break;
-               }
-       }
+       if (chunk->done) {
+               DLIST_REMOVE(state->chunks, chunk);
+               SMB_ASSERT(state->num_chunks > 0);
+               state->num_chunks--;
+               TALLOC_FREE(chunk);
 
-       if (i == state->num_reqs) {
-               async_req_nterror(state->req, NT_STATUS_INTERNAL_ERROR);
                return;
        }
 
-       status = cli_writeall_recv(req);
-       TALLOC_FREE(state->reqs[i]);
-       req = NULL;
-       if (!NT_STATUS_IS_OK(status)) {
-               async_req_nterror(state->req, status);
+       if (chunk->subreq != NULL) {
                return;
        }
 
-       state->pending -= 1;
-       if (state->pending == 0) {
-               async_req_done(state->req);
+       SMB_ASSERT(state->num_waiting > 0);
+
+       buf = chunk->buf + chunk->tmp_size;
+       ofs = chunk->ofs + chunk->tmp_size;
+       size = chunk->total_size - chunk->tmp_size;
+
+       ok = smb1cli_conn_req_possible(state->cli->conn);
+       if (!ok) {
                return;
        }
 
-       if (state->eof) {
+       chunk->subreq = cli_write_andx_send(chunk,
+                                           state->ev,
+                                           state->cli,
+                                           state->fnum,
+                                           state->mode,
+                                           buf,
+                                           ofs,
+                                           size);
+       if (tevent_req_nomem(chunk->subreq, req)) {
                return;
        }
+       tevent_req_set_callback(chunk->subreq,
+                               cli_push_chunk_done,
+                               chunk);
 
-       buf = state->buf + i * state->chunk_size;
+       state->num_waiting--;
+       return;
+}
 
-       to_write = state->source(buf, state->chunk_size, state->priv);
-       if (to_write == 0) {
-               state->eof = true;
+static void cli_push_chunk_done(struct tevent_req *subreq)
+{
+       struct cli_push_chunk *chunk =
+               tevent_req_callback_data(subreq,
+               struct cli_push_chunk);
+       struct tevent_req *req = chunk->req;
+       struct cli_push_state *state =
+               tevent_req_data(req,
+               struct cli_push_state);
+       NTSTATUS status;
+       size_t expected = chunk->total_size - chunk->tmp_size;
+       size_t written;
+
+       chunk->subreq = NULL;
+
+       status = cli_write_andx_recv(subreq, &written);
+       TALLOC_FREE(subreq);
+       if (tevent_req_nterror(req, status)) {
                return;
        }
 
-       state->reqs[i] = cli_writeall_send(
-               state->reqs, state->ev, state->cli, state->fnum,
-               state->mode, buf, state->start_offset + state->sent, to_write);
-       if (state->reqs[i] == NULL) {
-               async_req_nterror(state->req, NT_STATUS_NO_MEMORY);
+       if (written > expected) {
+               tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
                return;
        }
 
-       state->reqs[i]->async.fn = cli_push_written;
-       state->reqs[i]->async.priv = state;
+       if (written == 0) {
+               tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
+               return;
+       }
 
-       state->sent += to_write;
-       state->pending += 1;
+       chunk->tmp_size += written;
+
+       if (chunk->tmp_size == chunk->total_size) {
+               chunk->done = true;
+       } else {
+               state->num_waiting++;
+       }
+
+       cli_push_setup_chunks(req);
 }
 
-NTSTATUS cli_push_recv(struct async_req *req)
+NTSTATUS cli_push_recv(struct tevent_req *req)
 {
-       return async_req_simple_recv_ntstatus(req);
+       return tevent_req_simple_recv_ntstatus(req);
 }
 
 NTSTATUS cli_push(struct cli_state *cli, uint16_t fnum, uint16_t mode,
@@ -1103,34 +1453,38 @@ NTSTATUS cli_push(struct cli_state *cli, uint16_t fnum, uint16_t mode,
                  void *priv)
 {
        TALLOC_CTX *frame = talloc_stackframe();
-       struct event_context *ev;
-       struct async_req *req;
-       NTSTATUS result = NT_STATUS_NO_MEMORY;
+       struct tevent_context *ev;
+       struct tevent_req *req;
+       NTSTATUS status = NT_STATUS_OK;
 
-       if (cli->fd_event != NULL) {
+       if (smbXcli_conn_has_async_calls(cli->conn)) {
                /*
                 * Can't use sync call while an async call is in flight
                 */
-               return NT_STATUS_INVALID_PARAMETER;
+               status = NT_STATUS_INVALID_PARAMETER;
+               goto fail;
        }
 
-       ev = event_context_init(frame);
+       ev = samba_tevent_context_init(frame);
        if (ev == NULL) {
-               goto nomem;
+               status = NT_STATUS_NO_MEMORY;
+               goto fail;
        }
 
        req = cli_push_send(frame, ev, cli, fnum, mode, start_offset,
                            window_size, source, priv);
        if (req == NULL) {
-               goto nomem;
+               status = NT_STATUS_NO_MEMORY;
+               goto fail;
        }
 
-       while (req->state < ASYNC_REQ_DONE) {
-               event_loop_once(ev);
+       if (!tevent_req_poll(req, ev)) {
+               status = map_nt_error_from_unix(errno);
+               goto fail;
        }
 
-       result = cli_push_recv(req);
nomem:
+       status = cli_push_recv(req);
fail:
        TALLOC_FREE(frame);
-       return result;
+       return status;
 }