s3:libsmb: add cli_write_send/recv which work with SMB1/2/3
[amitay/samba.git] / source3 / libsmb / clireadwrite.c
index bbe6a8127621545dd901533f04a4fcb44cde82cc..6d47ccd872ac01e6086a7381a78082d7d063daab 100644 (file)
@@ -22,6 +22,7 @@
 #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
@@ -43,16 +44,16 @@ static size_t cli_read_max_bufsize(struct cli_state *cli)
        if (cli->server_posix_capabilities & CIFS_UNIX_LARGE_READ_CAP) {
                useable_space = 0xFFFFFF - data_offset;
 
-               if (client_is_signing_on(cli)) {
+               if (smb1cli_conn_signing_is_active(cli->conn)) {
                        return min_space;
                }
 
-               if (cli_encryption_on(cli)) {
+               if (smb1cli_conn_encryption_on(cli->conn)) {
                        return min_space;
                }
 
                return useable_space;
-       } else if (cli_state_capabilities(cli) & CAP_LARGE_READX) {
+       } else if (smb1cli_conn_capabilities(cli->conn) & CAP_LARGE_READX) {
                /*
                 * Note: CAP_LARGE_READX also works with signing
                 */
@@ -86,7 +87,7 @@ static size_t cli_write_max_bufsize(struct cli_state *cli,
 
        if (cli->server_posix_capabilities & CIFS_UNIX_LARGE_WRITE_CAP) {
                useable_space = 0xFFFFFF - data_offset;
-       } else if (cli_state_capabilities(cli) & CAP_LARGE_WRITEX) {
+       } else if (smb1cli_conn_capabilities(cli->conn) & CAP_LARGE_WRITEX) {
                useable_space = 0x1FFFF - data_offset;
        } else {
                return min_space;
@@ -96,11 +97,11 @@ static size_t cli_write_max_bufsize(struct cli_state *cli,
                return min_space;
        }
 
-       if (client_is_signing_on(cli)) {
+       if (smb1cli_conn_signing_is_active(cli->conn)) {
                return min_space;
        }
 
-       if (cli_encryption_on(cli)) {
+       if (smb1cli_conn_encryption_on(cli->conn)) {
                return min_space;
        }
 
@@ -122,7 +123,7 @@ struct cli_read_andx_state {
 static void cli_read_andx_done(struct tevent_req *subreq);
 
 struct tevent_req *cli_read_andx_create(TALLOC_CTX *mem_ctx,
-                                       struct event_context *ev,
+                                       struct tevent_context *ev,
                                        struct cli_state *cli, uint16_t fnum,
                                        off_t offset, size_t size,
                                        struct tevent_req **psmbreq)
@@ -131,13 +132,6 @@ struct tevent_req *cli_read_andx_create(TALLOC_CTX *mem_ctx,
        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)));
-               return NULL;
-       }
-
        req = tevent_req_create(mem_ctx, &state, struct cli_read_andx_state);
        if (req == NULL) {
                return NULL;
@@ -155,13 +149,20 @@ struct tevent_req *cli_read_andx_create(TALLOC_CTX *mem_ctx,
        SSVAL(state->vwv + 8, 0, 0);
        SSVAL(state->vwv + 9, 0, 0);
 
-       if ((uint64_t)offset >> 32) {
+       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);
+               }
        }
 
-       subreq = cli_smb_req_create(state, ev, cli, SMBreadX, 0, wct,
+       subreq = cli_smb_req_create(state, ev, cli, SMBreadX, 0, 0, wct,
                                    state->vwv, 0, NULL);
        if (subreq == NULL) {
                TALLOC_FREE(req);
@@ -173,7 +174,7 @@ struct tevent_req *cli_read_andx_create(TALLOC_CTX *mem_ctx,
 }
 
 struct tevent_req *cli_read_andx_send(TALLOC_CTX *mem_ctx,
-                                     struct event_context *ev,
+                                     struct tevent_context *ev,
                                      struct cli_state *cli, uint16_t fnum,
                                      off_t offset, size_t size)
 {
@@ -186,7 +187,7 @@ struct tevent_req *cli_read_andx_send(TALLOC_CTX *mem_ctx,
                return NULL;
        }
 
-       status = cli_smb_req_send(subreq);
+       status = smb1cli_req_chain_submit(&subreq, 1);
        if (tevent_req_nterror(req, status)) {
                return tevent_req_post(req, ev);
        }
@@ -237,7 +238,7 @@ static void cli_read_andx_done(struct tevent_req *subreq)
 
        state->buf = discard_const_p(uint8_t, smb_base(inbuf)) + SVAL(vwv+6, 0);
 
-       if (trans_oob(smb_len(inbuf), SVAL(vwv+6, 0), state->received)
+       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);
@@ -268,223 +269,82 @@ NTSTATUS cli_read_andx_recv(struct tevent_req *req, ssize_t *received,
        return NT_STATUS_OK;
 }
 
-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;
-};
-
-static void cli_readall_done(struct tevent_req *subreq);
-
-static struct tevent_req *cli_readall_send(TALLOC_CTX *mem_ctx,
-                                          struct event_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;
-
-       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;
-}
-
-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;
-
-       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;
-       }
-
-       /*
-        * 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 (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;
-
-       TALLOC_FREE(subreq);
-
-       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;
-
-       if (tevent_req_is_nterror(req, &status)) {
-               return status;
-       }
-       *received = state->received;
-       *rcvbuf = state->buf;
-       return NT_STATUS_OK;
-}
-
-struct cli_pull_subreq {
-       struct tevent_req *req;
-       ssize_t received;
-       uint8_t *buf;
-};
-
-/*
- * 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 tevent_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
-        */
-       uint16_t max_reqs;
-       int num_reqs;
-       struct cli_pull_subreq *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 extensions.
         */
-
-       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(struct tevent_req *req, TALLOC_CTX *mem_ctx)
-{
-       struct cli_pull_state *state = tevent_req_data(
-               req, struct cli_pull_state);
-       char *result;
-
-       result = tevent_req_default_print(req, mem_ctx);
-       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 tevent_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 tevent_req *cli_pull_send(TALLOC_CTX *mem_ctx,
-                                struct event_context *ev,
+                                struct tevent_context *ev,
                                 struct cli_state *cli,
                                 uint16_t fnum, off_t start_offset,
-                                SMB_OFF_T size, size_t window_size,
+                                off_t size, size_t window_size,
                                 NTSTATUS (*sink)(char *buf, size_t n,
                                                  void *priv),
                                 void *priv)
 {
        struct tevent_req *req;
        struct cli_pull_state *state;
-       int i;
        size_t page_size = 1024;
+       uint64_t tmp64;
 
        req = tevent_req_create(mem_ctx, &state, struct cli_pull_state);
        if (req == NULL) {
                return NULL;
        }
-       tevent_req_set_print_fn(req, cli_pull_print);
-       state->req = req;
-
        state->cli = cli;
        state->ev = ev;
        state->fnum = fnum;
@@ -492,195 +352,324 @@ struct tevent_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) {
                tevent_req_done(req);
                return tevent_req_post(req, ev);
        }
 
-       state->chunk_size = cli_read_max_bufsize(cli);
+       if (smbXcli_conn_protocol(state->cli->conn) >= PROTOCOL_SMB2_02) {
+               state->chunk_size = smb2cli_conn_max_read_size(cli->conn);
+       } else {
+               state->chunk_size = cli_read_max_bufsize(cli);
+       }
        if (state->chunk_size > page_size) {
                state->chunk_size &= ~(page_size - 1);
        }
 
-       state->max_reqs = cli_state_max_requests(cli);
+       if (window_size == 0) {
+               /*
+                * We use 16 MByte as default window size.
+                */
+               window_size = 16 * 1024 * 1024;
+       }
+
+       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->num_reqs = MAX(window_size/state->chunk_size, 1);
-       state->num_reqs = MIN(state->num_reqs, state->max_reqs);
+       /*
+        * We defer the callback because of the complex
+        * substate/subfunction logic
+        */
+       tevent_req_defer_callback(req, ev);
 
-       state->reqs = talloc_zero_array(state, struct cli_pull_subreq,
-                                       state->num_reqs);
-       if (state->reqs == NULL) {
-               goto failed;
+       cli_pull_setup_chunks(req);
+       if (!tevent_req_is_in_progress(req)) {
+               return tevent_req_post(req, ev);
        }
 
-       state->requested = 0;
+       return req;
+}
 
-       for (i=0; i<state->num_reqs; i++) {
-               struct cli_pull_subreq *subreq = &state->reqs[i];
-               SMB_OFF_T size_left;
-               size_t 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->requested >= size) {
-                       state->num_reqs = i;
-                       break;
+       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;
                }
+       }
 
-               size_left = size - state->requested;
-               request_thistime = MIN(size_left, state->chunk_size);
+       for (i = state->num_chunks; i < state->max_chunks; i++) {
 
-               subreq->req = cli_readall_send(
-                       state->reqs, ev, cli, fnum,
-                       state->start_offset + state->requested,
-                       request_thistime);
+               if (state->num_waiting > 0) {
+                       return;
+               }
 
-               if (subreq->req == NULL) {
-                       goto failed;
+               if (state->remaining == 0) {
+                       break;
+               }
+
+               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);
+               state->num_chunks++;
+               state->num_waiting++;
+
+               cli_pull_chunk_ship(chunk);
+               if (!tevent_req_is_in_progress(req)) {
+                       return;
                }
-               tevent_req_set_callback(subreq->req, cli_pull_read_done, req);
-               state->requested += request_thistime;
        }
-       return req;
 
-failed:
-       TALLOC_FREE(req);
-       return NULL;
-}
+       if (state->remaining > 0) {
+               return;
+       }
 
-/*
- * Handle incoming read replies, push the data into sink and send out new
- * requests if necessary.
- */
+       if (state->num_chunks > 0) {
+               return;
+       }
+
+       tevent_req_done(req);
+}
 
-static void cli_pull_read_done(struct tevent_req *subreq)
+static void cli_pull_chunk_ship(struct cli_pull_chunk *chunk)
 {
-       struct tevent_req *req = tevent_req_callback_data(
-               subreq, struct tevent_req);
-       struct cli_pull_state *state = tevent_req_data(
-               req, struct cli_pull_state);
-       struct cli_pull_subreq *pull_subreq = NULL;
-       NTSTATUS status;
-       int i;
+       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;
 
-       for (i = 0; i < state->num_reqs; i++) {
-               pull_subreq = &state->reqs[i];
-               if (subreq == pull_subreq->req) {
-                       break;
+       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;
                }
-       }
-       if (i == state->num_reqs) {
-               /* Huh -- received something we did not send?? */
-               tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
+
+               if (chunk->tmp_size == 0) {
+                       /*
+                        * we got a short read, we're done
+                        */
+                       tevent_req_done(req);
+                       return;
+               }
+
+               status = state->sink((char *)chunk->buf,
+                                    chunk->tmp_size,
+                                    state->priv);
+               if (tevent_req_nterror(req, status)) {
+                       return;
+               }
+               state->pushed += chunk->tmp_size;
+
+               if (chunk->tmp_size < chunk->total_size) {
+                       /*
+                        * we got a short read, we're done
+                        */
+                       tevent_req_done(req);
+                       return;
+               }
+
+               DLIST_REMOVE(state->chunks, chunk);
+               SMB_ASSERT(state->num_chunks > 0);
+               state->num_chunks--;
+               TALLOC_FREE(chunk);
+
                return;
        }
 
-       status = cli_readall_recv(subreq, &pull_subreq->received,
-                                 &pull_subreq->buf);
-       if (!NT_STATUS_IS_OK(status)) {
-               tevent_req_nterror(state->req, status);
+       if (chunk->subreq != NULL) {
                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.
-        */
-
-       while (state->reqs[state->top_req].req != NULL) {
-               struct cli_pull_subreq *top_subreq;
+       SMB_ASSERT(state->num_waiting > 0);
 
-               DEBUG(11, ("cli_pull_read_done: top_req = %d\n",
-                          state->top_req));
+       ofs = chunk->ofs + chunk->tmp_size;
+       size = chunk->total_size - chunk->tmp_size;
 
-               top_subreq = &state->reqs[state->top_req];
+       if (smbXcli_conn_protocol(state->cli->conn) >= PROTOCOL_SMB2_02) {
+               uint32_t max_size;
 
-               if (tevent_req_is_in_progress(top_subreq->req)) {
-                       DEBUG(11, ("cli_pull_read_done: top request not yet "
-                                  "done\n"));
+               ok = smb2cli_conn_req_possible(state->cli->conn, &max_size);
+               if (!ok) {
                        return;
                }
 
-               DEBUG(10, ("cli_pull_read_done: Pushing %d bytes, %d already "
-                          "pushed\n", (int)top_subreq->received,
-                          (int)state->pushed));
+               /*
+                * downgrade depending on the available credits
+                */
+               size = MIN(max_size, size);
+
+               chunk->subreq = cli_smb2_read_send(chunk,
+                                                  state->ev,
+                                                  state->cli,
+                                                  state->fnum,
+                                                  ofs,
+                                                  size);
+               if (tevent_req_nomem(chunk->subreq, req)) {
+                       return;
+               }
+       } else {
+               ok = smb1cli_conn_req_possible(state->cli->conn);
+               if (!ok) {
+                       return;
+               }
 
-               status = state->sink((char *)top_subreq->buf,
-                                    top_subreq->received, state->priv);
-               if (tevent_req_nterror(state->req, status)) {
+               chunk->subreq = cli_read_andx_send(chunk,
+                                                  state->ev,
+                                                  state->cli,
+                                                  state->fnum,
+                                                  ofs,
+                                                  size);
+               if (tevent_req_nomem(chunk->subreq, req)) {
                        return;
                }
-               state->pushed += top_subreq->received;
-
-               TALLOC_FREE(state->reqs[state->top_req].req);
-
-               if (state->requested < state->size) {
-                       struct tevent_req *new_req;
-                       SMB_OFF_T size_left;
-                       size_t request_thistime;
-
-                       size_left = state->size - state->requested;
-                       request_thistime = MIN(size_left, state->chunk_size);
-
-                       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));
-
-                       new_req = cli_readall_send(
-                               state->reqs, state->ev, state->cli,
-                               state->fnum,
-                               state->start_offset + state->requested,
-                               request_thistime);
-
-                       if (tevent_req_nomem(new_req, state->req)) {
-                               return;
-                       }
-                       tevent_req_set_callback(new_req, cli_pull_read_done,
-                                               req);
-
-                       state->reqs[state->top_req].req = new_req;
-                       state->requested += request_thistime;
+       }
+       tevent_req_set_callback(chunk->subreq,
+                               cli_pull_chunk_done,
+                               chunk);
+
+       state->num_waiting--;
+       return;
+}
+
+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;
+
+       chunk->subreq = NULL;
+
+       if (smbXcli_conn_protocol(state->cli->conn) >= PROTOCOL_SMB2_02) {
+               status = cli_smb2_read_recv(subreq, &received, &buf);
+       } else {
+               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;
+       }
+
+       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;
+       }
+
+       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;
                }
+       }
 
-               state->top_req = (state->top_req+1) % state->num_reqs;
+       if (received != chunk->total_size) {
+               uint8_t *p = chunk->buf + chunk->tmp_size;
+               memcpy(p, buf, received);
+               TALLOC_FREE(subreq);
        }
 
-       tevent_req_done(req);
+       chunk->tmp_size += received;
+
+       if (chunk->tmp_size == chunk->total_size) {
+               chunk->done = true;
+       } else {
+               state->num_waiting++;
+       }
+
+       cli_pull_setup_chunks(req);
 }
 
-NTSTATUS cli_pull_recv(struct tevent_req *req, SMB_OFF_T *received)
+NTSTATUS cli_pull_recv(struct tevent_req *req, off_t *received)
 {
        struct cli_pull_state *state = tevent_req_data(
                req, struct cli_pull_state);
        NTSTATUS 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 tevent_context *ev;
        struct tevent_req *req;
        NTSTATUS status = NT_STATUS_OK;
 
-       if (cli_has_async_calls(cli)) {
+       if (smbXcli_conn_has_async_calls(cli->conn)) {
                /*
                 * Can't use sync call while an async call is in flight
                 */
@@ -688,7 +677,7 @@ NTSTATUS cli_pull(struct cli_state *cli, uint16_t fnum,
                goto fail;
        }
 
-       ev = event_context_init(frame);
+       ev = samba_tevent_context_init(frame);
        if (ev == NULL) {
                status = NT_STATUS_NO_MEMORY;
                goto fail;
@@ -701,8 +690,7 @@ NTSTATUS cli_pull(struct cli_state *cli, uint16_t fnum,
                goto fail;
        }
 
-       if (!tevent_req_poll(req, ev)) {
-               status = map_nt_error_from_unix(errno);
+       if (!tevent_req_poll_ntstatus(req, ev, &status)) {
                goto fail;
        }
 
@@ -712,6 +700,128 @@ NTSTATUS cli_pull(struct cli_state *cli, uint16_t fnum,
        return status;
 }
 
+struct cli_read_state {
+       struct cli_state *cli;
+       char *buf;
+       size_t buflen;
+       size_t received;
+};
+
+static void cli_read_done(struct tevent_req *subreq);
+
+struct tevent_req *cli_read_send(
+       TALLOC_CTX *mem_ctx,
+       struct tevent_context *ev,
+       struct cli_state *cli,
+       uint16_t fnum,
+       char *buf,
+       off_t offset,
+       size_t size)
+{
+       struct tevent_req *req, *subreq;
+       struct cli_read_state *state;
+
+       req = tevent_req_create(mem_ctx, &state, struct cli_read_state);
+       if (req == NULL) {
+               return NULL;
+       }
+       state->cli = cli;
+       state->buf = buf;
+       state->buflen = size;
+
+       if (smbXcli_conn_protocol(state->cli->conn) >= PROTOCOL_SMB2_02) {
+               uint32_t max_size;
+               bool ok;
+
+               ok = smb2cli_conn_req_possible(state->cli->conn, &max_size);
+               if (!ok) {
+                       tevent_req_nterror(
+                               req,
+                               NT_STATUS_INSUFFICIENT_RESOURCES);
+                       return tevent_req_post(req, ev);
+               }
+
+               /*
+                * downgrade depending on the available credits
+                */
+               size = MIN(max_size, size);
+
+               subreq = cli_smb2_read_send(
+                       state, ev, cli, fnum, offset, size);
+               if (tevent_req_nomem(subreq, req)) {
+                       return tevent_req_post(req, ev);
+               }
+       } else {
+               bool ok;
+               ok = smb1cli_conn_req_possible(state->cli->conn);
+               if (!ok) {
+                       tevent_req_nterror(
+                               req,
+                               NT_STATUS_INSUFFICIENT_RESOURCES);
+                       return tevent_req_post(req, ev);
+               }
+
+               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_read_done, req);
+
+       return req;
+}
+
+static void cli_read_done(struct tevent_req *subreq)
+{
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
+       struct cli_read_state *state = tevent_req_data(
+               req, struct cli_read_state);
+       NTSTATUS status;
+       ssize_t received;
+       uint8_t *buf;
+
+       if (smbXcli_conn_protocol(state->cli->conn) >= PROTOCOL_SMB2_02) {
+               status = cli_smb2_read_recv(subreq, &received, &buf);
+       } else {
+               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;
+       }
+       if ((received < 0) || (received > state->buflen)) {
+               state->received = 0;
+               tevent_req_nterror(req, NT_STATUS_UNEXPECTED_IO_ERROR);
+               return;
+       }
+
+       memcpy(state->buf, buf, received);
+       state->received = received;
+       tevent_req_done(req);
+}
+
+NTSTATUS cli_read_recv(struct tevent_req *req, size_t *received)
+{
+       struct cli_read_state *state = tevent_req_data(
+               req, struct cli_read_state);
+       NTSTATUS status;
+
+       if (tevent_req_is_nterror(req, &status)) {
+               return status;
+       }
+       if (received != NULL) {
+               *received = state->received;
+       }
+       return NT_STATUS_OK;
+}
+
 static NTSTATUS cli_read_sink(char *buf, size_t n, void *priv)
 {
        char **pbuf = (char **)priv;
@@ -725,7 +835,7 @@ NTSTATUS cli_read(struct cli_state *cli, uint16_t fnum,
                 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);
@@ -823,7 +933,7 @@ struct cli_write_andx_state {
 static void cli_write_andx_done(struct tevent_req *subreq);
 
 struct tevent_req *cli_write_andx_create(TALLOC_CTX *mem_ctx,
-                                        struct event_context *ev,
+                                        struct tevent_context *ev,
                                         struct cli_state *cli, uint16_t fnum,
                                         uint16_t mode, const uint8_t *buf,
                                         off_t offset, size_t size,
@@ -833,7 +943,7 @@ struct tevent_req *cli_write_andx_create(TALLOC_CTX *mem_ctx,
 {
        struct tevent_req *req, *subreq;
        struct cli_write_andx_state *state;
-       bool bigoffset = ((cli_state_capabilities(cli) & CAP_LARGE_FILES) != 0);
+       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, wct);
        uint16_t *vwv;
@@ -843,7 +953,7 @@ struct tevent_req *cli_write_andx_create(TALLOC_CTX *mem_ctx,
                return NULL;
        }
 
-       size = MIN(size, max_write);
+       state->size = MIN(size, max_write);
 
        vwv = state->vwv;
 
@@ -855,11 +965,11 @@ struct tevent_req *cli_write_andx_create(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_smb_wct_ofs(reqs_before, num_reqs_before)
+             smb1cli_req_wct_ofs(reqs_before, num_reqs_before)
              + 1               /* the wct field */
              + wct * 2         /* vwv */
              + 2               /* num_bytes field */
@@ -873,9 +983,9 @@ struct tevent_req *cli_write_andx_create(TALLOC_CTX *mem_ctx,
        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 = size;
+       state->iov[1].iov_len = state->size;
 
-       subreq = cli_smb_req_create(state, ev, cli, SMBwriteX, 0, wct, vwv,
+       subreq = cli_smb_req_create(state, ev, cli, SMBwriteX, 0, 0, wct, vwv,
                                    2, state->iov);
        if (tevent_req_nomem(subreq, req)) {
                return tevent_req_post(req, ev);
@@ -885,68 +995,194 @@ struct tevent_req *cli_write_andx_create(TALLOC_CTX *mem_ctx,
        return req;
 }
 
-struct tevent_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 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)
+{
+       struct tevent_req *req, *subreq;
+       NTSTATUS status;
+
+       req = cli_write_andx_create(mem_ctx, ev, cli, fnum, mode, buf, offset,
+                                   size, NULL, 0, &subreq);
+       if (req == NULL) {
+               return NULL;
+       }
+
+       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)) {
+               tevent_req_nterror(req, status);
+               return;
+       }
+       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);
+}
+
+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_write_state {
+       struct cli_state *cli;
+       size_t written;
+};
+
+static void cli_write_done(struct tevent_req *subreq);
+
+struct tevent_req *cli_write_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 tevent_req *req, *subreq;
-       NTSTATUS status;
+       struct tevent_req *req = NULL;
+       struct cli_write_state *state = NULL;
+       struct tevent_req *subreq = NULL;
 
-       req = cli_write_andx_create(mem_ctx, ev, cli, fnum, mode, buf, offset,
-                                   size, NULL, 0, &subreq);
+       req = tevent_req_create(mem_ctx, &state, struct cli_write_state);
        if (req == NULL) {
                return NULL;
        }
+       state->cli = cli;
 
-       status = cli_smb_req_send(subreq);
-       if (tevent_req_nterror(req, status)) {
+       if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
+               uint32_t max_size;
+               bool ok;
+
+               ok = smb2cli_conn_req_possible(state->cli->conn, &max_size);
+               if (!ok) {
+                       tevent_req_nterror(
+                               req,
+                               NT_STATUS_INSUFFICIENT_RESOURCES);
+                       return tevent_req_post(req, ev);
+               }
+
+               /*
+                * downgrade depending on the available credits
+                */
+               size = MIN(max_size, size);
+
+               subreq = cli_smb2_write_send(state,
+                                            ev,
+                                            cli,
+                                            fnum,
+                                            mode,
+                                            buf,
+                                            offset,
+                                            size);
+       } else {
+               bool ok;
+
+               ok = smb1cli_conn_req_possible(state->cli->conn);
+               if (!ok) {
+                       tevent_req_nterror(
+                               req,
+                               NT_STATUS_INSUFFICIENT_RESOURCES);
+                       return tevent_req_post(req, ev);
+               }
+
+               subreq = cli_write_andx_send(state,
+                                            ev,
+                                            cli,
+                                            fnum,
+                                            mode,
+                                            buf,
+                                            offset,
+                                            size);
+       }
+       if (tevent_req_nomem(subreq, req)) {
                return tevent_req_post(req, ev);
        }
+       tevent_req_set_callback(subreq, cli_write_done, req);
+
        return req;
 }
 
-static void cli_write_andx_done(struct tevent_req *subreq)
+static void cli_write_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;
-       uint8_t *inbuf;
+       struct tevent_req *req =
+               tevent_req_callback_data(subreq,
+               struct tevent_req);
+       struct cli_write_state *state =
+               tevent_req_data(req,
+               struct cli_write_state);
        NTSTATUS status;
 
-       status = cli_smb_recv(subreq, state, &inbuf, 6, &wct, &vwv,
-                             NULL, NULL);
+       if (smbXcli_conn_protocol(state->cli->conn) >= PROTOCOL_SMB2_02) {
+               status = cli_smb2_write_recv(subreq, &state->written);
+       } else {
+               status = cli_write_andx_recv(subreq, &state->written);
+       }
        TALLOC_FREE(subreq);
-       if (NT_STATUS_IS_ERR(status)) {
-               tevent_req_nterror(req, status);
+       if (tevent_req_nterror(req, status)) {
                return;
        }
-       state->written = SVAL(vwv+2, 0);
-       state->written |= SVAL(vwv+4, 0)<<16;
        tevent_req_done(req);
 }
 
-NTSTATUS cli_write_andx_recv(struct tevent_req *req, size_t *pwritten)
+NTSTATUS cli_write_recv(struct tevent_req *req, size_t *pwritten)
 {
-       struct cli_write_andx_state *state = tevent_req_data(
-               req, struct cli_write_andx_state);
+       struct cli_write_state *state =
+               tevent_req_data(req,
+               struct cli_write_state);
        NTSTATUS status;
 
        if (tevent_req_is_nterror(req, &status)) {
+               tevent_req_received(req);
                return status;
        }
-       if (pwritten != 0) {
+       if (pwritten != NULL) {
                *pwritten = state->written;
        }
+       tevent_req_received(req);
        return NT_STATUS_OK;
 }
 
-struct cli_writeall_state {
-       struct event_context *ev;
+struct cli_smb1_writeall_state {
+       struct tevent_context *ev;
        struct cli_state *cli;
        uint16_t fnum;
        uint16_t mode;
@@ -956,20 +1192,21 @@ struct cli_writeall_state {
        size_t written;
 };
 
-static void cli_writeall_written(struct tevent_req *req);
+static void cli_smb1_writeall_written(struct tevent_req *req);
 
-static struct tevent_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_smb1_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 tevent_req *req, *subreq;
-       struct cli_writeall_state *state;
+       struct cli_smb1_writeall_state *state;
 
-       req = tevent_req_create(mem_ctx, &state, struct cli_writeall_state);
+       req = tevent_req_create(mem_ctx, &state,
+                               struct cli_smb1_writeall_state);
        if (req == NULL) {
                return NULL;
        }
@@ -988,16 +1225,16 @@ static struct tevent_req *cli_writeall_send(TALLOC_CTX *mem_ctx,
        if (tevent_req_nomem(subreq, req)) {
                return tevent_req_post(req, ev);
        }
-       tevent_req_set_callback(subreq, cli_writeall_written, req);
+       tevent_req_set_callback(subreq, cli_smb1_writeall_written, req);
        return req;
 }
 
-static void cli_writeall_written(struct tevent_req *subreq)
+static void cli_smb1_writeall_written(struct tevent_req *subreq)
 {
        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);
+       struct cli_smb1_writeall_state *state = tevent_req_data(
+               req, struct cli_smb1_writeall_state);
        NTSTATUS status;
        size_t written, to_write;
 
@@ -1028,11 +1265,102 @@ static void cli_writeall_written(struct tevent_req *subreq)
        if (tevent_req_nomem(subreq, req)) {
                return;
        }
-       tevent_req_set_callback(subreq, cli_writeall_written, req);
+       tevent_req_set_callback(subreq, cli_smb1_writeall_written, req);
+}
+
+static NTSTATUS cli_smb1_writeall_recv(struct tevent_req *req,
+                                      size_t *pwritten)
+{
+       struct cli_smb1_writeall_state *state = tevent_req_data(
+               req, struct cli_smb1_writeall_state);
+       NTSTATUS status;
+
+       if (tevent_req_is_nterror(req, &status)) {
+               return status;
+       }
+       if (pwritten != NULL) {
+               *pwritten = state->written;
+       }
+       return NT_STATUS_OK;
+}
+
+struct cli_writeall_state {
+       struct cli_state *cli;
+       size_t written;
+};
+
+static void cli_writeall_done(struct tevent_req *subreq);
+
+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 tevent_req *req, *subreq;
+       struct cli_writeall_state *state;
+
+       req = tevent_req_create(mem_ctx, &state, struct cli_writeall_state);
+       if (req == NULL) {
+               return NULL;
+       }
+       state->cli = cli;
+
+       if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
+               subreq = cli_smb2_writeall_send(
+                       state,
+                       ev,
+                       cli,
+                       fnum,
+                       mode,
+                       buf,
+                       offset,
+                       size);
+       } else {
+               subreq = cli_smb1_writeall_send(
+                       state,
+                       ev,
+                       cli,
+                       fnum,
+                       mode,
+                       buf,
+                       offset,
+                       size);
+       }
+
+       if (tevent_req_nomem(subreq, req)) {
+               return tevent_req_post(req, ev);
+       }
+       tevent_req_set_callback(subreq, cli_writeall_done, req);
+
+       return req;
+}
+
+static void cli_writeall_done(struct tevent_req *subreq)
+{
+       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;
+
+       if (smbXcli_conn_protocol(state->cli->conn) >= PROTOCOL_SMB2_02) {
+               status = cli_smb2_writeall_recv(subreq, &state->written);
+       } else {
+               status = cli_smb1_writeall_recv(subreq, &state->written);
+       }
+       TALLOC_FREE(subreq);
+       if (tevent_req_nterror(req, status)) {
+               return;
+       }
+       tevent_req_done(req);
 }
 
-static NTSTATUS cli_writeall_recv(struct tevent_req *req,
-                                 size_t *pwritten)
+NTSTATUS cli_writeall_recv(struct tevent_req *req, size_t *pwritten)
 {
        struct cli_writeall_state *state = tevent_req_data(
                req, struct cli_writeall_state);
@@ -1047,23 +1375,24 @@ static NTSTATUS cli_writeall_recv(struct tevent_req *req,
        return NT_STATUS_OK;
 }
 
+
 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)
 {
        TALLOC_CTX *frame = talloc_stackframe();
-       struct event_context *ev;
+       struct tevent_context *ev;
        struct tevent_req *req;
        NTSTATUS status = NT_STATUS_NO_MEMORY;
 
-       if (cli_has_async_calls(cli)) {
+       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 = event_context_init(frame);
+       ev = samba_tevent_context_init(frame);
        if (ev == NULL) {
                goto fail;
        }
@@ -1071,8 +1400,7 @@ NTSTATUS cli_writeall(struct cli_state *cli, uint16_t fnum, uint16_t mode,
        if (req == NULL) {
                goto fail;
        }
-       if (!tevent_req_poll(req, ev)) {
-               status = map_nt_error_from_unix(errno);
+       if (!tevent_req_poll_ntstatus(req, ev, &status)) {
                goto fail;
        }
        status = cli_writeall_recv(req, pwritten);
@@ -1081,21 +1409,14 @@ NTSTATUS cli_writeall(struct cli_state *cli, uint16_t fnum, uint16_t mode,
        return status;
 }
 
-struct cli_push_write_state {
-       struct tevent_req *req;/* This is the main request! Not the subreq */
-       uint32_t idx;
-       off_t ofs;
-       uint8_t *buf;
-       size_t size;
-};
+struct cli_push_chunk;
 
 struct cli_push_state {
-       struct event_context *ev;
+       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;
@@ -1107,64 +1428,34 @@ struct cli_push_state {
 
        /*
         * Outstanding requests
+        *
+        * The maximum is 256:
+        * - which would be a window of 256 MByte
+        *   for SMB2 with multi-credit
+        *   or smb1 unix extensions.
         */
-       uint32_t pending;
-       uint16_t max_reqs;
-       uint32_t num_reqs;
-       struct cli_push_write_state **reqs;
+       uint16_t max_chunks;
+       uint16_t num_chunks;
+       uint16_t num_waiting;
+       struct cli_push_chunk *chunks;
 };
 
-static void cli_push_written(struct tevent_req *req);
-
-static bool cli_push_write_setup(struct tevent_req *req,
-                                struct cli_push_state *state,
-                                uint32_t idx)
-{
-       struct cli_push_write_state *substate;
+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;
+};
 
-       substate = talloc(state->reqs, struct cli_push_write_state);
-       if (!substate) {
-               return false;
-       }
-       substate->req = req;
-       substate->idx = idx;
-       substate->ofs = state->next_offset;
-       substate->buf = talloc_array(substate, uint8_t, state->chunk_size);
-       if (!substate->buf) {
-               talloc_free(substate);
-               return false;
-       }
-       substate->size = state->source(substate->buf,
-                                      state->chunk_size,
-                                      state->priv);
-       if (substate->size == 0) {
-               state->eof = true;
-               /* nothing to send */
-               talloc_free(substate);
-               return true;
-       }
-
-       subreq = cli_writeall_send(substate,
-                                  state->ev, state->cli,
-                                  state->fnum, state->mode,
-                                  substate->buf,
-                                  substate->ofs,
-                                  substate->size);
-       if (!subreq) {
-               talloc_free(substate);
-               return false;
-       }
-       tevent_req_set_callback(subreq, cli_push_written, substate);
-
-       state->reqs[idx] = substate;
-       state->pending += 1;
-       state->next_offset += substate->size;
-
-       return true;
-}
+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 event_context *ev,
+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,
@@ -1174,8 +1465,8 @@ struct tevent_req *cli_push_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
 {
        struct tevent_req *req;
        struct cli_push_state *state;
-       uint32_t i;
        size_t page_size = 1024;
+       uint64_t tmp64;
 
        req = tevent_req_create(mem_ctx, &state, struct cli_push_state);
        if (req == NULL) {
@@ -1188,86 +1479,243 @@ struct tevent_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->pending = 0;
        state->next_offset = start_offset;
 
-       state->chunk_size = cli_write_max_bufsize(cli, mode, 14);
+       if (smbXcli_conn_protocol(state->cli->conn) >= PROTOCOL_SMB2_02) {
+               state->chunk_size = smb2cli_conn_max_write_size(cli->conn);
+       } else {
+               state->chunk_size = cli_write_max_bufsize(cli, mode, 14);
+       }
        if (state->chunk_size > page_size) {
                state->chunk_size &= ~(page_size - 1);
        }
 
-       state->max_reqs = cli_state_max_requests(cli);
-
        if (window_size == 0) {
-               window_size = state->max_reqs * state->chunk_size;
+               /*
+                * We use 16 MByte as default window size.
+                */
+               window_size = 16 * 1024 * 1024;
        }
-       state->num_reqs = window_size/state->chunk_size;
+
+       tmp64 = window_size/state->chunk_size;
        if ((window_size % state->chunk_size) > 0) {
-               state->num_reqs += 1;
+               tmp64 += 1;
+       }
+       tmp64 = MAX(tmp64, 1);
+       tmp64 = MIN(tmp64, 256);
+       state->max_chunks = tmp64;
+
+       /*
+        * 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);
        }
-       state->num_reqs = MIN(state->num_reqs, state->max_reqs);
-       state->num_reqs = MAX(state->num_reqs, 1);
 
-       state->reqs = talloc_zero_array(state, struct cli_push_write_state *,
-                                       state->num_reqs);
-       if (state->reqs == NULL) {
-               goto failed;
+       return req;
+}
+
+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=0; i<state->num_reqs; i++) {
-               if (!cli_push_write_setup(req, state, i)) {
-                       goto failed;
+       for (i = state->num_chunks; i < state->max_chunks; i++) {
+
+               if (state->num_waiting > 0) {
+                       return;
                }
 
                if (state->eof) {
                        break;
                }
+
+               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;
+
+               DLIST_ADD_END(state->chunks, chunk);
+               state->num_chunks++;
+               state->num_waiting++;
+
+               cli_push_chunk_ship(chunk);
+               if (!tevent_req_is_in_progress(req)) {
+                       return;
+               }
        }
 
-       if (state->pending == 0) {
-               tevent_req_done(req);
-               return tevent_req_post(req, ev);
+       if (!state->eof) {
+               return;
        }
 
-       return req;
+       if (state->num_chunks > 0) {
+               return;
+       }
+
+       tevent_req_done(req);
+}
+
+static void cli_push_chunk_ship(struct cli_push_chunk *chunk)
+{
+       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;
+
+       if (chunk->done) {
+               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);
+
+       buf = chunk->buf + chunk->tmp_size;
+       ofs = chunk->ofs + chunk->tmp_size;
+       size = chunk->total_size - chunk->tmp_size;
+
+       if (smbXcli_conn_protocol(state->cli->conn) >= PROTOCOL_SMB2_02) {
+               uint32_t max_size;
+
+               ok = smb2cli_conn_req_possible(state->cli->conn, &max_size);
+               if (!ok) {
+                       return;
+               }
+
+               /*
+                * downgrade depending on the available credits
+                */
+               size = MIN(max_size, size);
+
+               chunk->subreq = cli_smb2_write_send(chunk,
+                                                   state->ev,
+                                                   state->cli,
+                                                   state->fnum,
+                                                   state->mode,
+                                                   buf,
+                                                   ofs,
+                                                   size);
+               if (tevent_req_nomem(chunk->subreq, req)) {
+                       return;
+               }
+       } else {
+               ok = smb1cli_conn_req_possible(state->cli->conn);
+               if (!ok) {
+                       return;
+               }
+
+               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);
 
- failed:
-       tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
-       return tevent_req_post(req, ev);
+       state->num_waiting--;
+       return;
 }
 
-static void cli_push_written(struct tevent_req *subreq)
+static void cli_push_chunk_done(struct tevent_req *subreq)
 {
-       struct cli_push_write_state *substate = tevent_req_callback_data(
-               subreq, struct cli_push_write_state);
-       struct tevent_req *req = substate->req;
-       struct cli_push_state *state = tevent_req_data(
-               req, struct cli_push_state);
+       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;
-       uint32_t idx = substate->idx;
+       size_t expected = chunk->total_size - chunk->tmp_size;
+       size_t written;
 
-       state->reqs[idx] = NULL;
-       state->pending -= 1;
+       chunk->subreq = NULL;
 
-       status = cli_writeall_recv(subreq, NULL);
+       if (smbXcli_conn_protocol(state->cli->conn) >= PROTOCOL_SMB2_02) {
+               status = cli_smb2_write_recv(subreq, &written);
+       } else {
+               status = cli_write_andx_recv(subreq, &written);
+       }
        TALLOC_FREE(subreq);
-       TALLOC_FREE(substate);
        if (tevent_req_nterror(req, status)) {
                return;
        }
 
-       if (!state->eof) {
-               if (!cli_push_write_setup(req, state, idx)) {
-                       tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
-                       return;
-               }
+       if (written > expected) {
+               tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
+               return;
        }
 
-       if (state->pending == 0) {
-               tevent_req_done(req);
+       if (written == 0) {
+               tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
                return;
        }
+
+       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 tevent_req *req)
@@ -1281,11 +1729,11 @@ 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 tevent_context *ev;
        struct tevent_req *req;
        NTSTATUS status = NT_STATUS_OK;
 
-       if (cli_has_async_calls(cli)) {
+       if (smbXcli_conn_has_async_calls(cli->conn)) {
                /*
                 * Can't use sync call while an async call is in flight
                 */
@@ -1293,7 +1741,7 @@ NTSTATUS cli_push(struct cli_state *cli, uint16_t fnum, uint16_t mode,
                goto fail;
        }
 
-       ev = event_context_init(frame);
+       ev = samba_tevent_context_init(frame);
        if (ev == NULL) {
                status = NT_STATUS_NO_MEMORY;
                goto fail;
@@ -1306,8 +1754,7 @@ NTSTATUS cli_push(struct cli_state *cli, uint16_t fnum, uint16_t mode,
                goto fail;
        }
 
-       if (!tevent_req_poll(req, ev)) {
-               status = map_nt_error_from_unix(errno);
+       if (!tevent_req_poll_ntstatus(req, ev, &status)) {
                goto fail;
        }
 
@@ -1316,3 +1763,123 @@ NTSTATUS cli_push(struct cli_state *cli, uint16_t fnum, uint16_t mode,
        TALLOC_FREE(frame);
        return status;
 }
+
+#define SPLICE_BLOCK_SIZE 1024 * 1024
+
+static NTSTATUS cli_splice_fallback(TALLOC_CTX *frame,
+                                   struct cli_state *srccli,
+                                   struct cli_state *dstcli,
+                                   uint16_t src_fnum, uint16_t dst_fnum,
+                                   off_t initial_size,
+                                   off_t src_offset, off_t dst_offset,
+                                   off_t *written,
+                                   int (*splice_cb)(off_t n, void *priv),
+                                   void *priv)
+{
+       NTSTATUS status;
+       uint8_t *buf = talloc_size(frame, SPLICE_BLOCK_SIZE);
+       size_t nread;
+       off_t remaining = initial_size;
+       *written = 0;
+
+       while (remaining) {
+               size_t to_read = MIN(remaining, SPLICE_BLOCK_SIZE);
+
+               status = cli_read(srccli, src_fnum,
+                                 (char *)buf, src_offset, to_read,
+                                 &nread);
+               if (!NT_STATUS_IS_OK(status)) {
+                       return status;
+               }
+
+               status = cli_writeall(dstcli, dst_fnum, 0,
+                                     buf, dst_offset, nread, NULL);
+               if (!NT_STATUS_IS_OK(status)) {
+                       return status;
+               }
+
+               if ((src_offset > INT64_MAX - nread) ||
+                   (dst_offset > INT64_MAX - nread)) {
+                       return NT_STATUS_FILE_TOO_LARGE;
+               }
+               src_offset += nread;
+               dst_offset += nread;
+               *written += nread;
+               if (remaining < nread) {
+                       return NT_STATUS_INTERNAL_ERROR;
+               }
+               remaining -= nread;
+               if (!splice_cb(initial_size - remaining, priv)) {
+                       return NT_STATUS_CANCELLED;
+               }
+       }
+
+       return NT_STATUS_OK;
+}
+
+NTSTATUS cli_splice(struct cli_state *srccli, struct cli_state *dstcli,
+                   uint16_t src_fnum, uint16_t dst_fnum,
+                   off_t size,
+                   off_t src_offset, off_t dst_offset,
+                   off_t *written,
+                   int (*splice_cb)(off_t n, void *priv), void *priv)
+{
+       TALLOC_CTX *frame = talloc_stackframe();
+       struct tevent_context *ev;
+       struct tevent_req *req;
+       NTSTATUS status = NT_STATUS_NO_MEMORY;
+       bool retry_fallback = false;
+
+       if (smbXcli_conn_has_async_calls(srccli->conn) ||
+           smbXcli_conn_has_async_calls(dstcli->conn))
+       {
+               /*
+                * Can't use sync call while an async call is in flight
+                */
+               status = NT_STATUS_INVALID_PARAMETER;
+               goto out;
+       }
+
+       do {
+               ev = samba_tevent_context_init(frame);
+               if (ev == NULL) {
+                       goto out;
+               }
+               if (srccli == dstcli &&
+                   smbXcli_conn_protocol(srccli->conn) >= PROTOCOL_SMB2_02 &&
+                   !retry_fallback)
+               {
+                       req = cli_smb2_splice_send(frame, ev,
+                                                  srccli, src_fnum, dst_fnum,
+                                                  size, src_offset, dst_offset,
+                                                  splice_cb, priv);
+               } else {
+                       status = cli_splice_fallback(frame,
+                                                    srccli, dstcli,
+                                                    src_fnum, dst_fnum,
+                                                    size,
+                                                    src_offset, dst_offset,
+                                                    written,
+                                                    splice_cb, priv);
+                       goto out;
+               }
+               if (req == NULL) {
+                       goto out;
+               }
+               if (!tevent_req_poll(req, ev)) {
+                       status = map_nt_error_from_unix(errno);
+                       goto out;
+               }
+               status = cli_smb2_splice_recv(req, written);
+
+               /*
+                * Older versions of Samba don't support
+                * FSCTL_SRV_COPYCHUNK_WRITE so use the fallback.
+                */
+               retry_fallback = NT_STATUS_EQUAL(status, NT_STATUS_INVALID_DEVICE_REQUEST);
+       } while (retry_fallback);
+
+ out:
+       TALLOC_FREE(frame);
+       return status;
+}