2 Unix SMB/CIFS implementation.
3 client file read/write routines
4 Copyright (C) Andrew Tridgell 1994-1998
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 /****************************************************************************
23 Calculate the recommended read buffer size
24 ****************************************************************************/
25 static size_t cli_read_max_bufsize(struct cli_state *cli)
27 if (!client_is_signing_on(cli) && !cli_encryption_on(cli)
28 && (cli->posix_capabilities & CIFS_UNIX_LARGE_READ_CAP)) {
29 return CLI_SAMBA_MAX_POSIX_LARGE_READX_SIZE;
31 if (cli->capabilities & CAP_LARGE_READX) {
33 ? CLI_SAMBA_MAX_LARGE_READX_SIZE
34 : CLI_WINDOWS_MAX_LARGE_READX_SIZE;
36 return (cli->max_xmit - (smb_size+32)) & ~1023;
39 /****************************************************************************
40 Calculate the recommended write buffer size
41 ****************************************************************************/
42 static size_t cli_write_max_bufsize(struct cli_state *cli, uint16_t write_mode)
44 if (write_mode == 0 &&
45 !client_is_signing_on(cli) &&
46 !cli_encryption_on(cli) &&
47 (cli->posix_capabilities & CIFS_UNIX_LARGE_WRITE_CAP) &&
48 (cli->capabilities & CAP_LARGE_FILES)) {
49 /* Only do massive writes if we can do them direct
50 * with no signing or encrypting - not on a pipe. */
51 return CLI_SAMBA_MAX_POSIX_LARGE_WRITEX_SIZE;
55 return CLI_SAMBA_MAX_LARGE_WRITEX_SIZE;
58 if (((cli->capabilities & CAP_LARGE_WRITEX) == 0)
59 || client_is_signing_on(cli)
60 || strequal(cli->dev, "LPT1:")) {
63 * Printer devices are restricted to max_xmit writesize in
64 * Vista and XPSP3 as are signing connections.
67 return (cli->max_xmit - (smb_size+32)) & ~1023;
70 return CLI_WINDOWS_MAX_LARGE_WRITEX_SIZE;
73 struct cli_read_andx_state {
81 static void cli_read_andx_done(struct tevent_req *subreq);
83 struct tevent_req *cli_read_andx_create(TALLOC_CTX *mem_ctx,
84 struct event_context *ev,
85 struct cli_state *cli, int fnum,
86 off_t offset, size_t size,
87 struct tevent_req **psmbreq)
89 struct tevent_req *req, *subreq;
90 struct cli_read_andx_state *state;
91 bool bigoffset = False;
94 if (size > cli_read_max_bufsize(cli)) {
95 DEBUG(0, ("cli_read_andx_send got size=%d, can only handle "
96 "size=%d\n", (int)size,
97 (int)cli_read_max_bufsize(cli)));
101 req = tevent_req_create(mem_ctx, &state, struct cli_read_andx_state);
107 SCVAL(state->vwv + 0, 0, 0xFF);
108 SCVAL(state->vwv + 0, 1, 0);
109 SSVAL(state->vwv + 1, 0, 0);
110 SSVAL(state->vwv + 2, 0, fnum);
111 SIVAL(state->vwv + 3, 0, offset);
112 SSVAL(state->vwv + 5, 0, size);
113 SSVAL(state->vwv + 6, 0, size);
114 SSVAL(state->vwv + 7, 0, (size >> 16));
115 SSVAL(state->vwv + 8, 0, 0);
116 SSVAL(state->vwv + 9, 0, 0);
118 if ((uint64_t)offset >> 32) {
120 SIVAL(state->vwv + 10, 0,
121 (((uint64_t)offset)>>32) & 0xffffffff);
125 subreq = cli_smb_req_create(state, ev, cli, SMBreadX, 0, wct,
126 state->vwv, 0, NULL);
127 if (subreq == NULL) {
131 tevent_req_set_callback(subreq, cli_read_andx_done, req);
136 struct tevent_req *cli_read_andx_send(TALLOC_CTX *mem_ctx,
137 struct event_context *ev,
138 struct cli_state *cli, int fnum,
139 off_t offset, size_t size)
141 struct tevent_req *req, *subreq;
143 req = cli_read_andx_create(mem_ctx, ev, cli, fnum, offset, size,
145 if ((req == NULL) || !cli_smb_req_send(subreq)) {
152 static void cli_read_andx_done(struct tevent_req *subreq)
154 struct tevent_req *req = tevent_req_callback_data(
155 subreq, struct tevent_req);
156 struct cli_read_andx_state *state = tevent_req_data(
157 req, struct cli_read_andx_state);
164 state->status = cli_smb_recv(subreq, 12, &wct, &vwv, &num_bytes,
166 if (NT_STATUS_IS_ERR(state->status)) {
167 tevent_req_nterror(req, state->status);
171 /* size is the number of bytes the server returned.
173 state->received = SVAL(vwv + 5, 0);
174 state->received |= (((unsigned int)SVAL(vwv + 7, 0)) << 16);
176 if (state->received > state->size) {
177 DEBUG(5,("server returned more than we wanted!\n"));
178 tevent_req_nterror(req, NT_STATUS_UNEXPECTED_IO_ERROR);
183 * bcc field must be valid for small reads, for large reads the 16-bit
184 * bcc field can't be correct.
187 if ((state->received < 0xffff) && (state->received > num_bytes)) {
188 DEBUG(5, ("server announced more bytes than sent\n"));
189 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
193 inbuf = cli_smb_inbuf(subreq);
194 state->buf = (uint8_t *)smb_base(inbuf) + SVAL(vwv+6, 0);
196 if (trans_oob(smb_len(inbuf), SVAL(vwv+6, 0), state->received)
197 || (state->buf < bytes)) {
198 DEBUG(5, ("server returned invalid read&x data offset\n"));
199 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
202 tevent_req_done(req);
206 * Pull the data out of a finished async read_and_x request. rcvbuf is
207 * talloced from the request, so better make sure that you copy it away before
208 * you talloc_free(req). "rcvbuf" is NOT a talloc_ctx of its own, so do not
212 NTSTATUS cli_read_andx_recv(struct tevent_req *req, ssize_t *received,
215 struct cli_read_andx_state *state = tevent_req_data(
216 req, struct cli_read_andx_state);
219 if (tevent_req_is_nterror(req, &status)) {
222 *received = state->received;
223 *rcvbuf = state->buf;
227 struct cli_pull_subreq {
228 struct tevent_req *req;
234 * Parallel read support.
236 * cli_pull sends as many read&x requests as the server would allow via
237 * max_mux at a time. When replies flow back in, the data is written into
238 * the callback function "sink" in the right order.
241 struct cli_pull_state {
242 struct async_req *req;
244 struct event_context *ev;
245 struct cli_state *cli;
250 NTSTATUS (*sink)(char *buf, size_t n, void *priv);
256 * Outstanding requests
259 struct cli_pull_subreq *reqs;
262 * For how many bytes did we send requests already?
267 * Next request index to push into "sink". This walks around the "req"
268 * array, taking care that the requests are pushed to "sink" in the
269 * right order. If necessary (i.e. replies don't come in in the right
270 * order), replies are held back in "reqs".
275 * How many bytes did we push into "sink"?
281 static char *cli_pull_print(TALLOC_CTX *mem_ctx, struct async_req *req)
283 struct cli_pull_state *state = talloc_get_type_abort(
284 req->private_data, struct cli_pull_state);
287 result = async_req_print(mem_ctx, req);
288 if (result == NULL) {
292 return talloc_asprintf_append_buffer(
293 result, "num_reqs=%d, top_req=%d",
294 state->num_reqs, state->top_req);
297 static void cli_pull_read_done(struct tevent_req *read_req);
300 * Prepare an async pull request
303 struct async_req *cli_pull_send(TALLOC_CTX *mem_ctx,
304 struct event_context *ev,
305 struct cli_state *cli,
306 uint16_t fnum, off_t start_offset,
307 SMB_OFF_T size, size_t window_size,
308 NTSTATUS (*sink)(char *buf, size_t n,
312 struct async_req *result;
313 struct cli_pull_state *state;
316 if (!async_req_setup(mem_ctx, &result, &state,
317 struct cli_pull_state)) {
320 result->print = cli_pull_print;
326 state->start_offset = start_offset;
335 if (!async_post_ntstatus(result, ev, NT_STATUS_OK)) {
341 state->chunk_size = cli_read_max_bufsize(cli);
343 state->num_reqs = MAX(window_size/state->chunk_size, 1);
344 state->num_reqs = MIN(state->num_reqs, cli->max_mux);
346 state->reqs = TALLOC_ZERO_ARRAY(state, struct cli_pull_subreq,
348 if (state->reqs == NULL) {
352 state->requested = 0;
354 for (i=0; i<state->num_reqs; i++) {
355 struct cli_pull_subreq *subreq = &state->reqs[i];
357 size_t request_thistime;
359 if (state->requested >= size) {
364 size_left = size - state->requested;
365 request_thistime = MIN(size_left, state->chunk_size);
367 subreq->req = cli_read_andx_send(
368 state->reqs, ev, cli, fnum,
369 state->start_offset + state->requested,
372 if (subreq->req == NULL) {
375 tevent_req_set_callback(subreq->req, cli_pull_read_done,
377 state->requested += request_thistime;
387 * Handle incoming read replies, push the data into sink and send out new
388 * requests if necessary.
391 static void cli_pull_read_done(struct tevent_req *subreq)
393 struct async_req *req = tevent_req_callback_data(
394 subreq, struct async_req);
395 struct cli_pull_state *state = talloc_get_type_abort(
396 req->private_data, struct cli_pull_state);
397 struct cli_pull_subreq *pull_subreq = NULL;
401 for (i = 0; i < state->num_reqs; i++) {
402 pull_subreq = &state->reqs[i];
403 if (subreq == pull_subreq->req) {
407 if (i == state->num_reqs) {
408 /* Huh -- received something we did not send?? */
409 async_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
413 status = cli_read_andx_recv(subreq, &pull_subreq->received,
415 if (!NT_STATUS_IS_OK(status)) {
416 async_req_nterror(state->req, status);
421 * This loop is the one to take care of out-of-order replies. All
422 * pending requests are in state->reqs, state->reqs[top_req] is the
423 * one that is to be pushed next. If however a request later than
424 * top_req is replied to, then we can't push yet. If top_req is
425 * replied to at a later point then, we need to push all the finished
429 while (state->reqs[state->top_req].req != NULL) {
430 struct cli_pull_subreq *top_subreq;
432 DEBUG(11, ("cli_pull_read_done: top_req = %d\n",
435 top_subreq = &state->reqs[state->top_req];
437 if (tevent_req_is_in_progress(top_subreq->req)) {
438 DEBUG(11, ("cli_pull_read_done: top request not yet "
443 DEBUG(10, ("cli_pull_read_done: Pushing %d bytes, %d already "
444 "pushed\n", (int)top_subreq->received,
445 (int)state->pushed));
447 status = state->sink((char *)top_subreq->buf,
448 top_subreq->received, state->priv);
449 if (!NT_STATUS_IS_OK(status)) {
450 async_req_nterror(state->req, status);
453 state->pushed += top_subreq->received;
455 TALLOC_FREE(state->reqs[state->top_req].req);
457 if (state->requested < state->size) {
458 struct tevent_req *new_req;
460 size_t request_thistime;
462 size_left = state->size - state->requested;
463 request_thistime = MIN(size_left, state->chunk_size);
465 DEBUG(10, ("cli_pull_read_done: Requesting %d bytes "
466 "at %d, position %d\n",
467 (int)request_thistime,
468 (int)(state->start_offset
472 new_req = cli_read_andx_send(
473 state->reqs, state->ev, state->cli,
475 state->start_offset + state->requested,
478 if (async_req_nomem(new_req, state->req)) {
481 tevent_req_set_callback(new_req, cli_pull_read_done,
484 state->reqs[state->top_req].req = new_req;
485 state->requested += request_thistime;
488 state->top_req = (state->top_req+1) % state->num_reqs;
494 NTSTATUS cli_pull_recv(struct async_req *req, SMB_OFF_T *received)
496 struct cli_pull_state *state = talloc_get_type_abort(
497 req->private_data, struct cli_pull_state);
500 if (async_req_is_nterror(req, &status)) {
503 *received = state->pushed;
507 NTSTATUS cli_pull(struct cli_state *cli, uint16_t fnum,
508 off_t start_offset, SMB_OFF_T size, size_t window_size,
509 NTSTATUS (*sink)(char *buf, size_t n, void *priv),
510 void *priv, SMB_OFF_T *received)
512 TALLOC_CTX *frame = talloc_stackframe();
513 struct event_context *ev;
514 struct async_req *req;
515 NTSTATUS status = NT_STATUS_OK;
517 if (cli_has_async_calls(cli)) {
519 * Can't use sync call while an async call is in flight
521 status = NT_STATUS_INVALID_PARAMETER;
525 ev = event_context_init(frame);
527 status = NT_STATUS_NO_MEMORY;
531 req = cli_pull_send(frame, ev, cli, fnum, start_offset, size,
532 window_size, sink, priv);
534 status = NT_STATUS_NO_MEMORY;
538 while (req->state < ASYNC_REQ_DONE) {
539 if (event_loop_once(ev) == -1) {
540 status = map_nt_error_from_unix(errno);
545 status = cli_pull_recv(req, received);
548 if (!NT_STATUS_IS_OK(status)) {
549 cli_set_error(cli, status);
554 static NTSTATUS cli_read_sink(char *buf, size_t n, void *priv)
556 char **pbuf = (char **)priv;
557 memcpy(*pbuf, buf, n);
562 ssize_t cli_read(struct cli_state *cli, int fnum, char *buf,
563 off_t offset, size_t size)
568 status = cli_pull(cli, fnum, offset, size, size,
569 cli_read_sink, &buf, &ret);
570 if (!NT_STATUS_IS_OK(status)) {
571 cli_set_error(cli, status);
577 /****************************************************************************
578 Issue a single SMBwrite and don't wait for a reply.
579 ****************************************************************************/
581 static bool cli_issue_write(struct cli_state *cli,
590 bool large_writex = false;
591 /* We can only do direct writes if not signing and not encrypting. */
592 bool direct_writes = !client_is_signing_on(cli) && !cli_encryption_on(cli);
594 if (!direct_writes && size + 1 > cli->bufsize) {
595 cli->outbuf = (char *)SMB_REALLOC(cli->outbuf, size + 1024);
599 cli->inbuf = (char *)SMB_REALLOC(cli->inbuf, size + 1024);
600 if (cli->inbuf == NULL) {
601 SAFE_FREE(cli->outbuf);
604 cli->bufsize = size + 1024;
607 memset(cli->outbuf,'\0',smb_size);
608 memset(cli->inbuf,'\0',smb_size);
610 if (cli->capabilities & CAP_LARGE_FILES) {
615 cli_set_message(cli->outbuf,14,0,True);
617 cli_set_message(cli->outbuf,12,0,True);
620 SCVAL(cli->outbuf,smb_com,SMBwriteX);
621 SSVAL(cli->outbuf,smb_tid,cli->cnum);
622 cli_setup_packet(cli);
624 SCVAL(cli->outbuf,smb_vwv0,0xFF);
625 SSVAL(cli->outbuf,smb_vwv2,fnum);
627 SIVAL(cli->outbuf,smb_vwv3,offset);
628 SIVAL(cli->outbuf,smb_vwv5,0);
629 SSVAL(cli->outbuf,smb_vwv7,mode);
631 SSVAL(cli->outbuf,smb_vwv8,(mode & 0x0008) ? size : 0);
633 * According to CIFS-TR-1p00, this following field should only
634 * be set if CAP_LARGE_WRITEX is set. We should check this
635 * locally. However, this check might already have been
636 * done by our callers.
638 SSVAL(cli->outbuf,smb_vwv9,(size>>16));
639 SSVAL(cli->outbuf,smb_vwv10,size);
640 /* +1 is pad byte. */
641 SSVAL(cli->outbuf,smb_vwv11,
642 smb_buf(cli->outbuf) - smb_base(cli->outbuf) + 1);
645 SIVAL(cli->outbuf,smb_vwv12,(((uint64_t)offset)>>32) & 0xffffffff);
648 p = smb_base(cli->outbuf) + SVAL(cli->outbuf,smb_vwv11) -1;
649 *p++ = '\0'; /* pad byte. */
650 if (!direct_writes) {
651 memcpy(p, buf, size);
653 if (size > 0x1FFFF) {
654 /* This is a POSIX 14 word large write. */
655 set_message_bcc(cli->outbuf, 0); /* Set bcc to zero. */
656 _smb_setlen_large(cli->outbuf,smb_size + 28 + 1 /* pad */ + size - 4);
658 cli_setup_bcc(cli, p+size);
661 SSVAL(cli->outbuf,smb_mid,cli->mid + i);
663 show_msg(cli->outbuf);
665 /* For direct writes we now need to write the data
666 * directly out of buf. */
667 return cli_send_smb_direct_writeX(cli, buf, size);
669 return cli_send_smb(cli);
673 /****************************************************************************
675 write_mode: 0x0001 disallow write cacheing
676 0x0002 return bytes remaining
677 0x0004 use raw named pipe protocol
678 0x0008 start of message mode named pipe protocol
679 ****************************************************************************/
681 ssize_t cli_write(struct cli_state *cli,
682 int fnum, uint16 write_mode,
683 const char *buf, off_t offset, size_t size)
685 ssize_t bwritten = 0;
686 unsigned int issued = 0;
687 unsigned int received = 0;
692 if(cli->max_mux > 1) {
693 mpx = cli->max_mux-1;
698 writesize = cli_write_max_bufsize(cli, write_mode);
700 blocks = (size + (writesize-1)) / writesize;
702 while (received < blocks) {
704 while ((issued - received < mpx) && (issued < blocks)) {
705 ssize_t bsent = issued * writesize;
706 ssize_t size1 = MIN(writesize, size - bsent);
708 if (!cli_issue_write(cli, fnum, offset + bsent,
716 if (!cli_receive_smb(cli)) {
722 if (cli_is_error(cli))
725 bwritten += SVAL(cli->inbuf, smb_vwv2);
726 if (writesize > 0xFFFF) {
727 bwritten += (((int)(SVAL(cli->inbuf, smb_vwv4)))<<16);
731 while (received < issued && cli_receive_smb(cli)) {
738 /****************************************************************************
739 write to a file using a SMBwrite and not bypassing 0 byte writes
740 ****************************************************************************/
742 ssize_t cli_smbwrite(struct cli_state *cli,
743 int fnum, char *buf, off_t offset, size_t size1)
749 size_t size = MIN(size1, cli->max_xmit - 48);
751 memset(cli->outbuf,'\0',smb_size);
752 memset(cli->inbuf,'\0',smb_size);
754 cli_set_message(cli->outbuf,5, 0,True);
756 SCVAL(cli->outbuf,smb_com,SMBwrite);
757 SSVAL(cli->outbuf,smb_tid,cli->cnum);
758 cli_setup_packet(cli);
760 SSVAL(cli->outbuf,smb_vwv0,fnum);
761 SSVAL(cli->outbuf,smb_vwv1,size);
762 SIVAL(cli->outbuf,smb_vwv2,offset);
763 SSVAL(cli->outbuf,smb_vwv4,0);
765 p = smb_buf(cli->outbuf);
767 SSVAL(p, 0, size); p += 2;
768 memcpy(p, buf + total, size); p += size;
770 cli_setup_bcc(cli, p);
772 if (!cli_send_smb(cli))
775 if (!cli_receive_smb(cli))
778 if (cli_is_error(cli))
781 size = SVAL(cli->inbuf,smb_vwv0);
795 * Send a write&x request
798 struct cli_write_andx_state {
806 static void cli_write_andx_done(struct tevent_req *subreq);
808 struct tevent_req *cli_write_andx_create(TALLOC_CTX *mem_ctx,
809 struct event_context *ev,
810 struct cli_state *cli, uint16_t fnum,
811 uint16_t mode, const uint8_t *buf,
812 off_t offset, size_t size,
813 struct tevent_req **reqs_before,
815 struct tevent_req **psmbreq)
817 struct tevent_req *req, *subreq;
818 struct cli_write_andx_state *state;
819 bool bigoffset = ((cli->capabilities & CAP_LARGE_FILES) != 0);
820 uint8_t wct = bigoffset ? 14 : 12;
821 size_t max_write = cli_write_max_bufsize(cli, mode);
824 req = tevent_req_create(mem_ctx, &state, struct cli_write_andx_state);
829 size = MIN(size, max_write);
833 SCVAL(vwv+0, 0, 0xFF);
836 SSVAL(vwv+2, 0, fnum);
837 SIVAL(vwv+3, 0, offset);
839 SSVAL(vwv+7, 0, mode);
841 SSVAL(vwv+9, 0, (size>>16));
842 SSVAL(vwv+10, 0, size);
845 cli_smb_wct_ofs(reqs_before, num_reqs_before)
846 + 1 /* the wct field */
848 + 2 /* num_bytes field */
852 SIVAL(vwv+12, 0, (((uint64_t)offset)>>32) & 0xffffffff);
856 state->iov[0].iov_base = &state->pad;
857 state->iov[0].iov_len = 1;
858 state->iov[1].iov_base = CONST_DISCARD(uint8_t *, buf);
859 state->iov[1].iov_len = size;
861 subreq = cli_smb_req_create(state, ev, cli, SMBwriteX, 0, wct, vwv,
863 if (tevent_req_nomem(subreq, req)) {
864 return tevent_req_post(req, ev);
866 tevent_req_set_callback(subreq, cli_write_andx_done, req);
871 struct tevent_req *cli_write_andx_send(TALLOC_CTX *mem_ctx,
872 struct event_context *ev,
873 struct cli_state *cli, uint16_t fnum,
874 uint16_t mode, const uint8_t *buf,
875 off_t offset, size_t size)
877 struct tevent_req *req, *subreq;
879 req = cli_write_andx_create(mem_ctx, ev, cli, fnum, mode, buf, offset,
880 size, NULL, 0, &subreq);
881 if ((req == NULL) || !cli_smb_req_send(subreq)) {
888 static void cli_write_andx_done(struct tevent_req *subreq)
890 struct tevent_req *req = tevent_req_callback_data(
891 subreq, struct tevent_req);
892 struct cli_write_andx_state *state = tevent_req_data(
893 req, struct cli_write_andx_state);
898 status = cli_smb_recv(subreq, 6, &wct, &vwv, NULL, NULL);
899 if (NT_STATUS_IS_ERR(status)) {
901 tevent_req_nterror(req, status);
904 state->written = SVAL(vwv+2, 0);
905 state->written |= SVAL(vwv+4, 0)<<16;
906 tevent_req_done(req);
909 NTSTATUS cli_write_andx_recv(struct tevent_req *req, size_t *pwritten)
911 struct cli_write_andx_state *state = tevent_req_data(
912 req, struct cli_write_andx_state);
915 if (tevent_req_is_nterror(req, &status)) {
918 *pwritten = state->written;
922 struct cli_writeall_state {
923 struct event_context *ev;
924 struct cli_state *cli;
933 static void cli_writeall_written(struct tevent_req *req);
935 static struct async_req *cli_writeall_send(TALLOC_CTX *mem_ctx,
936 struct event_context *ev,
937 struct cli_state *cli,
941 off_t offset, size_t size)
943 struct async_req *result;
944 struct tevent_req *subreq;
945 struct cli_writeall_state *state;
947 if (!async_req_setup(mem_ctx, &result, &state,
948 struct cli_writeall_state)) {
956 state->offset = offset;
960 subreq = cli_write_andx_send(state, state->ev, state->cli, state->fnum,
961 state->mode, state->buf, state->offset,
963 if (subreq == NULL) {
966 tevent_req_set_callback(subreq, cli_writeall_written, result);
974 static void cli_writeall_written(struct tevent_req *subreq)
976 struct async_req *req = tevent_req_callback_data(
977 subreq, struct async_req);
978 struct cli_writeall_state *state = talloc_get_type_abort(
979 req->private_data, struct cli_writeall_state);
981 size_t written, to_write;
983 status = cli_write_andx_recv(subreq, &written);
985 if (!NT_STATUS_IS_OK(status)) {
986 async_req_nterror(req, status);
990 state->written += written;
992 if (state->written > state->size) {
993 async_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
997 to_write = state->size - state->written;
1000 async_req_done(req);
1004 subreq = cli_write_andx_send(state, state->ev, state->cli, state->fnum,
1006 state->buf + state->written,
1007 state->offset + state->written, to_write);
1008 if (subreq == NULL) {
1009 async_req_nterror(req, NT_STATUS_NO_MEMORY);
1012 tevent_req_set_callback(subreq, cli_writeall_written, req);
1015 static NTSTATUS cli_writeall_recv(struct async_req *req)
1017 return async_req_simple_recv_ntstatus(req);
1020 struct cli_push_write_state {
1021 struct async_req *req;/* This is the main request! Not the subreq */
1028 struct cli_push_state {
1029 struct event_context *ev;
1030 struct cli_state *cli;
1036 size_t (*source)(uint8_t *buf, size_t n, void *priv);
1045 * Outstanding requests
1049 struct cli_push_write_state **reqs;
1052 static void cli_push_written(struct async_req *req);
1054 static bool cli_push_write_setup(struct async_req *req,
1055 struct cli_push_state *state,
1058 struct cli_push_write_state *substate;
1059 struct async_req *subreq;
1061 substate = talloc(state->reqs, struct cli_push_write_state);
1065 substate->req = req;
1066 substate->idx = idx;
1067 substate->ofs = state->next_offset;
1068 substate->buf = talloc_array(substate, uint8_t, state->chunk_size);
1069 if (!substate->buf) {
1070 talloc_free(substate);
1073 substate->size = state->source(substate->buf,
1076 if (substate->size == 0) {
1078 /* nothing to send */
1079 talloc_free(substate);
1083 subreq = cli_writeall_send(substate,
1084 state->ev, state->cli,
1085 state->fnum, state->mode,
1090 talloc_free(substate);
1093 subreq->async.fn = cli_push_written;
1094 subreq->async.priv = substate;
1096 state->reqs[idx] = substate;
1097 state->pending += 1;
1098 state->next_offset += substate->size;
1103 struct async_req *cli_push_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
1104 struct cli_state *cli,
1105 uint16_t fnum, uint16_t mode,
1106 off_t start_offset, size_t window_size,
1107 size_t (*source)(uint8_t *buf, size_t n,
1111 struct async_req *req;
1112 struct cli_push_state *state;
1115 if (!async_req_setup(mem_ctx, &req, &state,
1116 struct cli_push_state)) {
1122 state->start_offset = start_offset;
1124 state->source = source;
1128 state->next_offset = start_offset;
1130 state->chunk_size = cli_write_max_bufsize(cli, mode);
1132 if (window_size == 0) {
1133 window_size = cli->max_mux * state->chunk_size;
1135 state->num_reqs = window_size/state->chunk_size;
1136 if ((window_size % state->chunk_size) > 0) {
1137 state->num_reqs += 1;
1139 state->num_reqs = MIN(state->num_reqs, cli->max_mux);
1140 state->num_reqs = MAX(state->num_reqs, 1);
1142 state->reqs = TALLOC_ZERO_ARRAY(state, struct cli_push_write_state *,
1144 if (state->reqs == NULL) {
1148 for (i=0; i<state->num_reqs; i++) {
1149 if (!cli_push_write_setup(req, state, i)) {
1158 if (state->pending == 0) {
1159 if (!async_post_ntstatus(req, ev, NT_STATUS_OK)) {
1172 static void cli_push_written(struct async_req *subreq)
1174 struct cli_push_write_state *substate = talloc_get_type_abort(
1175 subreq->async.priv, struct cli_push_write_state);
1176 struct async_req *req = substate->req;
1177 struct cli_push_state *state = talloc_get_type_abort(
1178 req->private_data, struct cli_push_state);
1180 uint32_t idx = substate->idx;
1182 state->reqs[idx] = NULL;
1183 state->pending -= 1;
1185 status = cli_writeall_recv(subreq);
1186 TALLOC_FREE(subreq);
1187 TALLOC_FREE(substate);
1188 if (!NT_STATUS_IS_OK(status)) {
1189 async_req_nterror(req, status);
1194 if (!cli_push_write_setup(req, state, idx)) {
1195 async_req_nomem(NULL, req);
1200 if (state->pending == 0) {
1201 async_req_done(req);
1206 NTSTATUS cli_push_recv(struct async_req *req)
1208 return async_req_simple_recv_ntstatus(req);
1211 NTSTATUS cli_push(struct cli_state *cli, uint16_t fnum, uint16_t mode,
1212 off_t start_offset, size_t window_size,
1213 size_t (*source)(uint8_t *buf, size_t n, void *priv),
1216 TALLOC_CTX *frame = talloc_stackframe();
1217 struct event_context *ev;
1218 struct async_req *req;
1219 NTSTATUS status = NT_STATUS_OK;
1221 if (cli_has_async_calls(cli)) {
1223 * Can't use sync call while an async call is in flight
1225 status = NT_STATUS_INVALID_PARAMETER;
1229 ev = event_context_init(frame);
1231 status = NT_STATUS_NO_MEMORY;
1235 req = cli_push_send(frame, ev, cli, fnum, mode, start_offset,
1236 window_size, source, priv);
1238 status = NT_STATUS_NO_MEMORY;
1242 while (req->state < ASYNC_REQ_DONE) {
1243 if (event_loop_once(ev) == -1) {
1244 status = map_nt_error_from_unix(errno);
1249 status = cli_push_recv(req);
1252 if (!NT_STATUS_IS_OK(status)) {
1253 cli_set_error(cli, status);