Simplify async programming a bit with helper routines
[abartlet/samba.git/.git] / source3 / libsmb / clireadwrite.c
index d77875bae52f3b0e5144b82e8c583a03aa138bdb..ecf49396d119a0e23122bfb3da2c5fc6cf4a0192 100644 (file)
 #include "includes.h"
 
 /****************************************************************************
-Issue a single SMBread and don't wait for a reply.
+  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;
+       }
+       return (cli->max_xmit - (smb_size+32)) & ~1023;
+}
 
-static bool cli_issue_read(struct cli_state *cli, int fnum, off_t offset,
-                          size_t size, int i)
+/*
+ * Send a read&x request
+ */
+
+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 async_req *result;
+       struct cli_request *req;
        bool bigoffset = False;
 
-       memset(cli->outbuf,'\0',smb_size);
-       memset(cli->inbuf,'\0',smb_size);
+       uint16_t vwv[12];
+       uint8_t wct = 10;
 
-       if ((SMB_BIG_UINT)offset >> 32)
+       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;
+       }
+
+       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,
+                     (((uint64_t)offset)>>32) & 0xffffffff);
+               wct += 2;
+       }
 
-       set_message(cli->outbuf,bigoffset ? 12 : 10,0,True);
+       result = cli_request_send(mem_ctx, ev, cli, SMBreadX, 0, wct, vwv,
+                                 0, NULL);
+       if (result == NULL) {
+               return NULL;
+       }
 
-       SCVAL(cli->outbuf,smb_com,SMBreadX);
-       SSVAL(cli->outbuf,smb_tid,cli->cnum);
-       cli_setup_packet(cli);
+       req = talloc_get_type_abort(result->private_data, struct cli_request);
 
-       SCVAL(cli->outbuf,smb_vwv0,0xFF);
-       SSVAL(cli->outbuf,smb_vwv2,fnum);
-       SIVAL(cli->outbuf,smb_vwv3,offset);
-       SSVAL(cli->outbuf,smb_vwv5,size);
-       SSVAL(cli->outbuf,smb_vwv6,size);
-       SSVAL(cli->outbuf,smb_vwv7,(size >> 16));
-       SSVAL(cli->outbuf,smb_mid,cli->mid + i);
+       req->data.read.ofs = offset;
+       req->data.read.size = size;
+       req->data.read.received = 0;
+       req->data.read.rcvbuf = NULL;
+
+       return result;
+}
 
-       if (bigoffset) {
-               SIVAL(cli->outbuf,smb_vwv10,(((SMB_BIG_UINT)offset)>>32) & 0xffffffff);
+/*
+ * Pull the data out of a finished async read_and_x request. rcvbuf is
+ * talloced from the request, so better make sure that you copy it away before
+ * you talloc_free(req). "rcvbuf" is NOT a talloc_ctx of its own, so do not
+ * talloc_move it!
+ */
+
+NTSTATUS cli_read_andx_recv(struct async_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;
+       NTSTATUS status;
+       size_t size;
+
+       if (async_req_is_error(req, &status)) {
+               return status;
        }
 
-       return cli_send_smb(cli);
+       status = cli_pull_reply(req, &wct, &vwv, &num_bytes, &bytes);
+
+       if (NT_STATUS_IS_ERR(status)) {
+               return status;
+       }
+
+       if (wct < 12) {
+               return NT_STATUS_INVALID_NETWORK_RESPONSE;
+       }
+
+       /* 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);
+
+       if (size > cli_req->data.read.size) {
+               DEBUG(5,("server returned more than we wanted!\n"));
+               return NT_STATUS_UNEXPECTED_IO_ERROR;
+       }
+
+       *rcvbuf = (uint8_t *)(smb_base(cli_req->inbuf) + SVAL(vwv + 6, 0));
+       *received = size;
+       return NT_STATUS_OK;
 }
 
-/****************************************************************************
-  Read size bytes at offset offset using SMBreadX.
-****************************************************************************/
+/*
+ * 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.
+ */
 
-ssize_t cli_read(struct cli_state *cli, int fnum, char *buf, off_t offset, size_t size)
-{
-       char *p;
-       size_t size2;
-       size_t readsize;
-       ssize_t total = 0;
-       /* We can only do direct reads if not signing. */
-       bool direct_reads = !client_is_signing_on(cli);
+struct cli_pull_state {
+       struct async_req *req;
+
+       struct event_context *ev;
+       struct cli_state *cli;
+       uint16_t fnum;
+       off_t start_offset;
+       SMB_OFF_T size;
 
-       if (size == 0)
-               return 0;
+       NTSTATUS (*sink)(char *buf, size_t n, void *priv);
+       void *priv;
+
+       size_t chunk_size;
 
        /*
-        * Set readsize to the maximum size we can handle in one readX,
-        * rounded down to a multiple of 1024.
+        * Outstanding requests
         */
+       int num_reqs;
+       struct async_req **reqs;
 
-       if (client_is_signing_on(cli) == False && (cli->posix_capabilities & CIFS_UNIX_LARGE_READ_CAP)) {
-               readsize = CLI_SAMBA_MAX_POSIX_LARGE_READX_SIZE;
-       } else if (cli->capabilities & CAP_LARGE_READX) {
-               if (cli->is_samba) {
-                       readsize = CLI_SAMBA_MAX_LARGE_READX_SIZE;
-               } else {
-                       readsize = CLI_WINDOWS_MAX_LARGE_READX_SIZE;
-               }
-       } else {
-               readsize = (cli->max_xmit - (smb_size+32)) & ~1023;
+       /*
+        * 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".
+        */
+       int top_req;
+
+       /*
+        * How many bytes did we push into "sink"?
+        */
+
+       SMB_OFF_T pushed;
+};
+
+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;
        }
 
-       while (total < size) {
-               readsize = MIN(readsize, size-total);
+       return talloc_asprintf_append_buffer(
+               result, "num_reqs=%d, top_req=%d",
+               state->num_reqs, state->top_req);
+}
 
-               /* Issue a read and receive a reply */
+static void cli_pull_read_done(struct async_req *read_req);
 
-               if (!cli_issue_read(cli, fnum, offset, readsize, 0))
-                       return -1;
+/*
+ * Prepare an async pull request
+ */
+
+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 async_req *result;
+       struct cli_pull_state *state;
+       int i;
 
-               if (direct_reads) {
-                       if (!cli_receive_smb_readX_header(cli))
-                               return -1;
-               } else {
-                       if (!cli_receive_smb(cli))
-                               return -1;
+       result = async_req_new(mem_ctx, ev);
+       if (result == NULL) {
+               goto failed;
+       }
+       state = talloc(result, struct cli_pull_state);
+       if (state == NULL) {
+               goto failed;
+       }
+       result->private_data = state;
+       result->print = cli_pull_print;
+       state->req = result;
+
+       state->cli = cli;
+       state->ev = ev;
+       state->fnum = fnum;
+       state->start_offset = start_offset;
+       state->size = size;
+       state->sink = sink;
+       state->priv = priv;
+
+       state->pushed = 0;
+       state->top_req = 0;
+
+       if (size == 0) {
+               if (!async_post_status(result, NT_STATUS_OK)) {
+                       goto failed;
                }
+               return result;
+       }
 
-               /* Check for error.  Make sure to check for DOS and NT
-                   errors. */
+       state->chunk_size = cli_read_max_bufsize(cli);
 
-                if (cli_is_error(cli)) {
-                       bool recoverable_error = False;
-                        NTSTATUS status = NT_STATUS_OK;
-                        uint8 eclass = 0;
-                       uint32 ecode = 0;
+       state->num_reqs = MAX(window_size/state->chunk_size, 1);
+       state->num_reqs = MIN(state->num_reqs, cli->max_mux);
 
-                        if (cli_is_nt_error(cli))
-                                status = cli_nt_error(cli);
-                        else
-                                cli_dos_error(cli, &eclass, &ecode);
+       state->reqs = TALLOC_ZERO_ARRAY(state, struct async_req *,
+                                       state->num_reqs);
+       if (state->reqs == NULL) {
+               goto failed;
+       }
 
-                       /*
-                        * ERRDOS ERRmoredata or STATUS_MORE_ENRTIES is a
-                        * recoverable error, plus we have valid data in the
-                        * packet so don't error out here.
-                        */
+       state->requested = 0;
 
-                        if ((eclass == ERRDOS && ecode == ERRmoredata) ||
-                            NT_STATUS_V(status) == NT_STATUS_V(STATUS_MORE_ENTRIES))
-                               recoverable_error = True;
+       for (i=0; i<state->num_reqs; i++) {
+               SMB_OFF_T size_left;
+               size_t request_thistime;
 
-                       if (!recoverable_error)
-                                return -1;
+               if (state->requested >= size) {
+                       state->num_reqs = i;
+                       break;
                }
 
-               size2 = SVAL(cli->inbuf, smb_vwv5);
-               size2 |= (((unsigned int)(SVAL(cli->inbuf, smb_vwv7))) << 16);
-
-               if (size2 > readsize) {
-                       DEBUG(5,("server returned more than we wanted!\n"));
-                       return -1;
-               } else if (size2 < 0) {
-                       DEBUG(5,("read return < 0!\n"));
-                       return -1;
-               }
+               size_left = size - state->requested;
+               request_thistime = MIN(size_left, state->chunk_size);
 
-               if (!direct_reads) {
-                       /* Copy data into buffer */
-                       p = smb_base(cli->inbuf) + SVAL(cli->inbuf,smb_vwv6);
-                       memcpy(buf + total, p, size2);
-               } else {
-                       /* Ensure the remaining data matches the return size. */
-                       ssize_t toread = smb_len_large(cli->inbuf) - SVAL(cli->inbuf,smb_vwv6);
-
-                       /* Ensure the size is correct. */
-                       if (toread != size2) {
-                               DEBUG(5,("direct read logic fail toread (%d) != size2 (%u)\n",
-                                       (int)toread, (unsigned int)size2 ));
-                               return -1;
-                       }
+               state->reqs[i] = cli_read_andx_send(
+                       state->reqs, ev, cli, fnum,
+                       state->start_offset + state->requested,
+                       request_thistime);
 
-                       /* Read data directly into buffer */
-                       toread = cli_receive_smb_data(cli,buf+total,size2);
-                       if (toread != size2) {
-                               DEBUG(5,("direct read read failure toread (%d) != size2 (%u)\n",
-                                       (int)toread, (unsigned int)size2 ));
-                               return -1;
-                       }
+               if (state->reqs[i] == NULL) {
+                       goto failed;
                }
 
-               total += size2;
-               offset += size2;
+               state->reqs[i]->async.fn = cli_pull_read_done;
+               state->reqs[i]->async.priv = result;
 
-               /*
-                * If the server returned less than we asked for we're at EOF.
-                */
-
-               if (size2 < readsize)
-                       break;
+               state->requested += request_thistime;
        }
+       return result;
 
-       return total;
+failed:
+       TALLOC_FREE(result);
+       return NULL;
 }
 
-#if 0  /* relies on client_receive_smb(), now a static in libsmb/clientgen.c */
-
-/* This call is INCOMPATIBLE with SMB signing.  If you remove the #if 0
-   you must fix ensure you don't attempt to sign the packets - data
-   *will* be currupted */
-
-/****************************************************************************
-Issue a single SMBreadraw and don't wait for a reply.
-****************************************************************************/
+/*
+ * Handle incoming read replies, push the data into sink and send out new
+ * requests if necessary.
+ */
 
-static bool cli_issue_readraw(struct cli_state *cli, int fnum, off_t offset, 
-                          size_t size, int i)
+static void cli_pull_read_done(struct async_req *read_req)
 {
-
-       if (!cli->sign_info.use_smb_signing) {
-               DEBUG(0, ("Cannot use readraw and SMB Signing\n"));
-               return False;
+       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;
+
+       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_error(state->req, status);
+               return;
        }
-       
-       memset(cli->outbuf,'\0',smb_size);
-       memset(cli->inbuf,'\0',smb_size);
 
-       set_message(cli->outbuf,10,0,True);
-               
-       SCVAL(cli->outbuf,smb_com,SMBreadbraw);
-       SSVAL(cli->outbuf,smb_tid,cli->cnum);
-       cli_setup_packet(cli);
+       /*
+        * 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.
+        */
 
-       SSVAL(cli->outbuf,smb_vwv0,fnum);
-       SIVAL(cli->outbuf,smb_vwv1,offset);
-       SSVAL(cli->outbuf,smb_vwv2,size);
-       SSVAL(cli->outbuf,smb_vwv3,size);
-       SSVAL(cli->outbuf,smb_mid,cli->mid + i);
+       while (state->reqs[state->top_req] != NULL) {
+               struct cli_request *top_read;
 
-       return cli_send_smb(cli);
-}
+               DEBUG(11, ("cli_pull_read_done: top_req = %d\n",
+                          state->top_req));
 
-/****************************************************************************
- Tester for the readraw call.
-****************************************************************************/
+               if (state->reqs[state->top_req]->state < ASYNC_REQ_DONE) {
+                       DEBUG(11, ("cli_pull_read_done: top request not yet "
+                                  "done\n"));
+                       return;
+               }
 
-ssize_t cli_readraw(struct cli_state *cli, int fnum, char *buf, off_t offset, size_t size)
-{
-       char *p;
-       int size2;
-       size_t readsize;
-       ssize_t total = 0;
+               top_read = talloc_get_type_abort(
+                       state->reqs[state->top_req]->private_data,
+                       struct cli_request);
 
-       if (size == 0) 
-               return 0;
+               DEBUG(10, ("cli_pull_read_done: Pushing %d bytes, %d already "
+                          "pushed\n", (int)top_read->data.read.received,
+                          (int)state->pushed));
 
-       /*
-        * Set readsize to the maximum size we can handle in one readraw.
-        */
+               status = state->sink((char *)top_read->data.read.rcvbuf,
+                                    top_read->data.read.received,
+                                    state->priv);
+               if (!NT_STATUS_IS_OK(status)) {
+                       async_req_error(state->req, status);
+                       return;
+               }
+               state->pushed += top_read->data.read.received;
 
-       readsize = 0xFFFF;
+               TALLOC_FREE(state->reqs[state->top_req]);
 
-       while (total < size) {
-               readsize = MIN(readsize, size-total);
+               if (state->requested < state->size) {
+                       struct async_req *new_req;
+                       SMB_OFF_T size_left;
+                       size_t request_thistime;
 
-               /* Issue a read and receive a reply */
+                       size_left = state->size - state->requested;
+                       request_thistime = MIN(size_left, state->chunk_size);
 
-               if (!cli_issue_readraw(cli, fnum, offset, readsize, 0))
-                       return -1;
+                       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));
 
-               if (!client_receive_smb(cli->fd, cli->inbuf, cli->timeout))
-                       return -1;
+                       new_req = cli_read_andx_send(
+                               state->reqs, state->ev, state->cli,
+                               state->fnum,
+                               state->start_offset + state->requested,
+                               request_thistime);
 
-               size2 = smb_len(cli->inbuf);
+                       if (async_req_nomem(new_req, state->req)) {
+                               return;
+                       }
 
-               if (size2 > readsize) {
-                       DEBUG(5,("server returned more than we wanted!\n"));
-                       return -1;
-               } else if (size2 < 0) {
-                       DEBUG(5,("read return < 0!\n"));
-                       return -1;
+                       new_req->async.fn = cli_pull_read_done;
+                       new_req->async.priv = pull_req;
+
+                       state->reqs[state->top_req] = new_req;
+                       state->requested += request_thistime;
                }
 
-               /* Copy data into buffer */
+               state->top_req = (state->top_req+1) % state->num_reqs;
+       }
 
-               if (size2) {
-                       p = cli->inbuf + 4;
-                       memcpy(buf + total, p, size2);
-               }
+       async_req_done(pull_req);
+}
 
-               total += size2;
-               offset += size2;
+NTSTATUS cli_pull_recv(struct async_req *req, SMB_OFF_T *received)
+{
+       struct cli_pull_state *state = talloc_get_type_abort(
+               req->private_data, struct cli_pull_state);
+       NTSTATUS status;
 
+       if (async_req_is_error(req, &status)) {
+               return status;
+       }
+       *received = state->pushed;
+       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,
+                 NTSTATUS (*sink)(char *buf, size_t n, void *priv),
+                 void *priv, SMB_OFF_T *received)
+{
+       TALLOC_CTX *frame = talloc_stackframe();
+       struct event_context *ev;
+       struct async_req *req;
+       NTSTATUS result = NT_STATUS_NO_MEMORY;
+
+       if (cli->fd_event != NULL) {
                /*
-                * If the server returned less than we asked for we're at EOF.
+                * Can't use sync call while an async call is in flight
                 */
+               return NT_STATUS_INVALID_PARAMETER;
+       }
 
-               if (size2 < readsize)
-                       break;
+       ev = event_context_init(frame);
+       if (ev == NULL) {
+               goto nomem;
        }
 
-       return total;
+       req = cli_pull_send(frame, ev, cli, fnum, start_offset, size,
+                           window_size, sink, priv);
+       if (req == NULL) {
+               goto nomem;
+       }
+
+       while (req->state < ASYNC_REQ_DONE) {
+               event_loop_once(ev);
+       }
+
+       result = cli_pull_recv(req, received);
+ nomem:
+       TALLOC_FREE(frame);
+       return result;
+}
+
+static NTSTATUS cli_read_sink(char *buf, size_t n, void *priv)
+{
+       char **pbuf = (char **)priv;
+       memcpy(*pbuf, buf, n);
+       *pbuf += n;
+       return NT_STATUS_OK;
+}
+
+ssize_t cli_read(struct cli_state *cli, int fnum, char *buf,
+                off_t offset, size_t size)
+{
+       NTSTATUS status;
+       SMB_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;
 }
-#endif
 
 /****************************************************************************
  Issue a single SMBwrite and don't wait for a reply.
@@ -295,8 +488,8 @@ static bool cli_issue_write(struct cli_state *cli,
 {
        char *p;
        bool large_writex = false;
-       /* We can only do direct writes if not signing. */
-       bool direct_writes = !client_is_signing_on(cli);
+       /* 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);
@@ -319,9 +512,9 @@ static bool cli_issue_write(struct cli_state *cli,
        }
 
        if (large_writex) {
-               set_message(cli->outbuf,14,0,True);
+               cli_set_message(cli->outbuf,14,0,True);
        } else {
-               set_message(cli->outbuf,12,0,True);
+               cli_set_message(cli->outbuf,12,0,True);
        }
 
        SCVAL(cli->outbuf,smb_com,SMBwriteX);
@@ -349,7 +542,7 @@ static bool cli_issue_write(struct cli_state *cli,
              smb_buf(cli->outbuf) - smb_base(cli->outbuf) + 1);
 
        if (large_writex) {
-               SIVAL(cli->outbuf,smb_vwv12,(((SMB_BIG_UINT)offset)>>32) & 0xffffffff);
+               SIVAL(cli->outbuf,smb_vwv12,(((uint64_t)offset)>>32) & 0xffffffff);
        }
 
        p = smb_base(cli->outbuf) + SVAL(cli->outbuf,smb_vwv11) -1;
@@ -402,21 +595,30 @@ ssize_t cli_write(struct cli_state *cli,
                mpx = 1;
        }
 
+       /* Default (small) writesize. */
+       writesize = (cli->max_xmit - (smb_size+32)) & ~1023;
+
         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 - not on a pipe. */
+                * with no signing or encrypting - not on a pipe. */
                writesize = CLI_SAMBA_MAX_POSIX_LARGE_WRITEX_SIZE;
-       } else if (cli->capabilities & CAP_LARGE_READX) {
+       } else if ((cli->capabilities & CAP_LARGE_WRITEX) &&
+                       (strcmp(cli->dev, "LPT1:") != 0)) {
+
+               /* Printer devices are restricted to max_xmit
+                * writesize in Vista and XPSP3. */
+
                if (cli->is_samba) {
-                       writesize = CLI_SAMBA_MAX_LARGE_READX_SIZE;
-               } else {
-                       writesize = CLI_WINDOWS_MAX_LARGE_READX_SIZE;
+                       writesize = CLI_SAMBA_MAX_LARGE_WRITEX_SIZE;
+               } else if (!client_is_signing_on(cli)) {
+                       /* Windows restricts signed writes to max_xmit.
+                        * Found by Volker. */
+                       writesize = CLI_WINDOWS_MAX_LARGE_WRITEX_SIZE;
                }
-       } else {
-               writesize = (cli->max_xmit - (smb_size+32)) & ~1023;
        }
 
        blocks = (size + (writesize-1)) / writesize;
@@ -445,7 +647,9 @@ ssize_t cli_write(struct cli_state *cli,
                        break;
 
                bwritten += SVAL(cli->inbuf, smb_vwv2);
-               bwritten += (((int)(SVAL(cli->inbuf, smb_vwv4)))<<16);
+               if (writesize > 0xFFFF) {
+                       bwritten += (((int)(SVAL(cli->inbuf, smb_vwv4)))<<16);
+               }
        }
 
        while (received < issued && cli_receive_smb(cli)) {
@@ -471,7 +675,7 @@ ssize_t cli_smbwrite(struct cli_state *cli,
                memset(cli->outbuf,'\0',smb_size);
                memset(cli->inbuf,'\0',smb_size);
 
-               set_message(cli->outbuf,5, 0,True);
+               cli_set_message(cli->outbuf,5, 0,True);
 
                SCVAL(cli->outbuf,smb_com,SMBwrite);
                SSVAL(cli->outbuf,smb_tid,cli->cnum);