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, uint16_t 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;
93 if (size > cli_read_max_bufsize(cli)) {
94 DEBUG(0, ("cli_read_andx_send got size=%d, can only handle "
95 "size=%d\n", (int)size,
96 (int)cli_read_max_bufsize(cli)));
100 req = tevent_req_create(mem_ctx, &state, struct cli_read_andx_state);
106 SCVAL(state->vwv + 0, 0, 0xFF);
107 SCVAL(state->vwv + 0, 1, 0);
108 SSVAL(state->vwv + 1, 0, 0);
109 SSVAL(state->vwv + 2, 0, fnum);
110 SIVAL(state->vwv + 3, 0, offset);
111 SSVAL(state->vwv + 5, 0, size);
112 SSVAL(state->vwv + 6, 0, size);
113 SSVAL(state->vwv + 7, 0, (size >> 16));
114 SSVAL(state->vwv + 8, 0, 0);
115 SSVAL(state->vwv + 9, 0, 0);
117 if (cli->capabilities & CAP_LARGE_FILES) {
118 SIVAL(state->vwv + 10, 0,
119 (((uint64_t)offset)>>32) & 0xffffffff);
122 if ((((uint64_t)offset) & 0xffffffff00000000LL) != 0) {
123 DEBUG(10, ("cli_read_andx_send got large offset where "
124 "the server does not support it\n"));
125 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
126 return tevent_req_post(req, ev);
130 subreq = cli_smb_req_create(state, ev, cli, SMBreadX, 0, wct,
131 state->vwv, 0, NULL);
132 if (subreq == NULL) {
136 tevent_req_set_callback(subreq, cli_read_andx_done, req);
141 struct tevent_req *cli_read_andx_send(TALLOC_CTX *mem_ctx,
142 struct event_context *ev,
143 struct cli_state *cli, uint16_t fnum,
144 off_t offset, size_t size)
146 struct tevent_req *req, *subreq;
149 req = cli_read_andx_create(mem_ctx, ev, cli, fnum, offset, size,
155 status = cli_smb_req_send(subreq);
156 if (!NT_STATUS_IS_OK(status)) {
157 tevent_req_nterror(req, status);
158 return tevent_req_post(req, ev);
163 static void cli_read_andx_done(struct tevent_req *subreq)
165 struct tevent_req *req = tevent_req_callback_data(
166 subreq, struct tevent_req);
167 struct cli_read_andx_state *state = tevent_req_data(
168 req, struct cli_read_andx_state);
175 state->status = cli_smb_recv(subreq, 12, &wct, &vwv, &num_bytes,
177 if (NT_STATUS_IS_ERR(state->status)) {
178 tevent_req_nterror(req, state->status);
182 /* size is the number of bytes the server returned.
184 state->received = SVAL(vwv + 5, 0);
185 state->received |= (((unsigned int)SVAL(vwv + 7, 0)) << 16);
187 if (state->received > state->size) {
188 DEBUG(5,("server returned more than we wanted!\n"));
189 tevent_req_nterror(req, NT_STATUS_UNEXPECTED_IO_ERROR);
194 * bcc field must be valid for small reads, for large reads the 16-bit
195 * bcc field can't be correct.
198 if ((state->received < 0xffff) && (state->received > num_bytes)) {
199 DEBUG(5, ("server announced more bytes than sent\n"));
200 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
204 inbuf = cli_smb_inbuf(subreq);
205 state->buf = (uint8_t *)smb_base(inbuf) + SVAL(vwv+6, 0);
207 if (trans_oob(smb_len_large(inbuf), SVAL(vwv+6, 0), state->received)
208 || ((state->received != 0) && (state->buf < bytes))) {
209 DEBUG(5, ("server returned invalid read&x data offset\n"));
210 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
213 tevent_req_done(req);
217 * Pull the data out of a finished async read_and_x request. rcvbuf is
218 * talloced from the request, so better make sure that you copy it away before
219 * you talloc_free(req). "rcvbuf" is NOT a talloc_ctx of its own, so do not
223 NTSTATUS cli_read_andx_recv(struct tevent_req *req, ssize_t *received,
226 struct cli_read_andx_state *state = tevent_req_data(
227 req, struct cli_read_andx_state);
230 if (tevent_req_is_nterror(req, &status)) {
233 *received = state->received;
234 *rcvbuf = state->buf;
238 struct cli_readall_state {
239 struct tevent_context *ev;
240 struct cli_state *cli;
248 static void cli_readall_done(struct tevent_req *subreq);
250 static struct tevent_req *cli_readall_send(TALLOC_CTX *mem_ctx,
251 struct event_context *ev,
252 struct cli_state *cli,
254 off_t offset, size_t size)
256 struct tevent_req *req, *subreq;
257 struct cli_readall_state *state;
259 req = tevent_req_create(mem_ctx, &state, struct cli_readall_state);
266 state->start_offset = offset;
271 subreq = cli_read_andx_send(state, ev, cli, fnum, offset, size);
272 if (tevent_req_nomem(subreq, req)) {
273 return tevent_req_post(req, ev);
275 tevent_req_set_callback(subreq, cli_readall_done, req);
279 static void cli_readall_done(struct tevent_req *subreq)
281 struct tevent_req *req = tevent_req_callback_data(
282 subreq, struct tevent_req);
283 struct cli_readall_state *state = tevent_req_data(
284 req, struct cli_readall_state);
289 status = cli_read_andx_recv(subreq, &received, &buf);
290 if (!NT_STATUS_IS_OK(status)) {
291 tevent_req_nterror(req, status);
297 tevent_req_done(req);
301 if ((state->received == 0) && (received == state->size)) {
302 /* Ideal case: Got it all in one run */
304 state->received += received;
305 tevent_req_done(req);
310 * We got a short read, issue a read for the
311 * rest. Unfortunately we have to allocate the buffer
312 * ourselves now, as our caller expects to receive a single
313 * buffer. cli_read_andx does it from the buffer received from
314 * the net, but with a short read we have to put it together
315 * from several reads.
318 if (state->buf == NULL) {
319 state->buf = talloc_array(state, uint8_t, state->size);
320 if (tevent_req_nomem(state->buf, req)) {
324 memcpy(state->buf + state->received, buf, received);
325 state->received += received;
329 if (state->received >= state->size) {
330 tevent_req_done(req);
334 subreq = cli_read_andx_send(state, state->ev, state->cli, state->fnum,
335 state->start_offset + state->received,
336 state->size - state->received);
337 if (tevent_req_nomem(subreq, req)) {
340 tevent_req_set_callback(subreq, cli_readall_done, req);
343 static NTSTATUS cli_readall_recv(struct tevent_req *req, ssize_t *received,
346 struct cli_readall_state *state = tevent_req_data(
347 req, struct cli_readall_state);
350 if (tevent_req_is_nterror(req, &status)) {
353 *received = state->received;
354 *rcvbuf = state->buf;
358 struct cli_pull_subreq {
359 struct tevent_req *req;
365 * Parallel read support.
367 * cli_pull sends as many read&x requests as the server would allow via
368 * max_mux at a time. When replies flow back in, the data is written into
369 * the callback function "sink" in the right order.
372 struct cli_pull_state {
373 struct tevent_req *req;
375 struct event_context *ev;
376 struct cli_state *cli;
381 NTSTATUS (*sink)(char *buf, size_t n, void *priv);
387 * Outstanding requests
390 struct cli_pull_subreq *reqs;
393 * For how many bytes did we send requests already?
398 * Next request index to push into "sink". This walks around the "req"
399 * array, taking care that the requests are pushed to "sink" in the
400 * right order. If necessary (i.e. replies don't come in in the right
401 * order), replies are held back in "reqs".
406 * How many bytes did we push into "sink"?
412 static char *cli_pull_print(struct tevent_req *req, TALLOC_CTX *mem_ctx)
414 struct cli_pull_state *state = tevent_req_data(
415 req, struct cli_pull_state);
418 result = tevent_req_print(mem_ctx, req);
419 if (result == NULL) {
423 return talloc_asprintf_append_buffer(
424 result, "num_reqs=%d, top_req=%d",
425 state->num_reqs, state->top_req);
428 static void cli_pull_read_done(struct tevent_req *read_req);
431 * Prepare an async pull request
434 struct tevent_req *cli_pull_send(TALLOC_CTX *mem_ctx,
435 struct event_context *ev,
436 struct cli_state *cli,
437 uint16_t fnum, off_t start_offset,
438 SMB_OFF_T size, size_t window_size,
439 NTSTATUS (*sink)(char *buf, size_t n,
443 struct tevent_req *req;
444 struct cli_pull_state *state;
447 req = tevent_req_create(mem_ctx, &state, struct cli_pull_state);
451 tevent_req_set_print_fn(req, cli_pull_print);
457 state->start_offset = start_offset;
466 tevent_req_done(req);
467 return tevent_req_post(req, ev);
470 state->chunk_size = cli_read_max_bufsize(cli);
472 state->num_reqs = MAX(window_size/state->chunk_size, 1);
473 state->num_reqs = MIN(state->num_reqs, cli->max_mux);
475 state->reqs = TALLOC_ZERO_ARRAY(state, struct cli_pull_subreq,
477 if (state->reqs == NULL) {
481 state->requested = 0;
483 for (i=0; i<state->num_reqs; i++) {
484 struct cli_pull_subreq *subreq = &state->reqs[i];
486 size_t request_thistime;
488 if (state->requested >= size) {
493 size_left = size - state->requested;
494 request_thistime = MIN(size_left, state->chunk_size);
496 subreq->req = cli_readall_send(
497 state->reqs, ev, cli, fnum,
498 state->start_offset + state->requested,
501 if (subreq->req == NULL) {
504 tevent_req_set_callback(subreq->req, cli_pull_read_done, req);
505 state->requested += request_thistime;
515 * Handle incoming read replies, push the data into sink and send out new
516 * requests if necessary.
519 static void cli_pull_read_done(struct tevent_req *subreq)
521 struct tevent_req *req = tevent_req_callback_data(
522 subreq, struct tevent_req);
523 struct cli_pull_state *state = tevent_req_data(
524 req, struct cli_pull_state);
525 struct cli_pull_subreq *pull_subreq = NULL;
529 for (i = 0; i < state->num_reqs; i++) {
530 pull_subreq = &state->reqs[i];
531 if (subreq == pull_subreq->req) {
535 if (i == state->num_reqs) {
536 /* Huh -- received something we did not send?? */
537 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
541 status = cli_readall_recv(subreq, &pull_subreq->received,
543 if (!NT_STATUS_IS_OK(status)) {
544 tevent_req_nterror(state->req, status);
549 * This loop is the one to take care of out-of-order replies. All
550 * pending requests are in state->reqs, state->reqs[top_req] is the
551 * one that is to be pushed next. If however a request later than
552 * top_req is replied to, then we can't push yet. If top_req is
553 * replied to at a later point then, we need to push all the finished
557 while (state->reqs[state->top_req].req != NULL) {
558 struct cli_pull_subreq *top_subreq;
560 DEBUG(11, ("cli_pull_read_done: top_req = %d\n",
563 top_subreq = &state->reqs[state->top_req];
565 if (tevent_req_is_in_progress(top_subreq->req)) {
566 DEBUG(11, ("cli_pull_read_done: top request not yet "
571 DEBUG(10, ("cli_pull_read_done: Pushing %d bytes, %d already "
572 "pushed\n", (int)top_subreq->received,
573 (int)state->pushed));
575 status = state->sink((char *)top_subreq->buf,
576 top_subreq->received, state->priv);
577 if (!NT_STATUS_IS_OK(status)) {
578 tevent_req_nterror(state->req, status);
581 state->pushed += top_subreq->received;
583 TALLOC_FREE(state->reqs[state->top_req].req);
585 if (state->requested < state->size) {
586 struct tevent_req *new_req;
588 size_t request_thistime;
590 size_left = state->size - state->requested;
591 request_thistime = MIN(size_left, state->chunk_size);
593 DEBUG(10, ("cli_pull_read_done: Requesting %d bytes "
594 "at %d, position %d\n",
595 (int)request_thistime,
596 (int)(state->start_offset
600 new_req = cli_readall_send(
601 state->reqs, state->ev, state->cli,
603 state->start_offset + state->requested,
606 if (tevent_req_nomem(new_req, state->req)) {
609 tevent_req_set_callback(new_req, cli_pull_read_done,
612 state->reqs[state->top_req].req = new_req;
613 state->requested += request_thistime;
616 state->top_req = (state->top_req+1) % state->num_reqs;
619 tevent_req_done(req);
622 NTSTATUS cli_pull_recv(struct tevent_req *req, SMB_OFF_T *received)
624 struct cli_pull_state *state = tevent_req_data(
625 req, struct cli_pull_state);
628 if (tevent_req_is_nterror(req, &status)) {
631 *received = state->pushed;
635 NTSTATUS cli_pull(struct cli_state *cli, uint16_t fnum,
636 off_t start_offset, SMB_OFF_T size, size_t window_size,
637 NTSTATUS (*sink)(char *buf, size_t n, void *priv),
638 void *priv, SMB_OFF_T *received)
640 TALLOC_CTX *frame = talloc_stackframe();
641 struct event_context *ev;
642 struct tevent_req *req;
643 NTSTATUS status = NT_STATUS_OK;
645 if (cli_has_async_calls(cli)) {
647 * Can't use sync call while an async call is in flight
649 status = NT_STATUS_INVALID_PARAMETER;
653 ev = event_context_init(frame);
655 status = NT_STATUS_NO_MEMORY;
659 req = cli_pull_send(frame, ev, cli, fnum, start_offset, size,
660 window_size, sink, priv);
662 status = NT_STATUS_NO_MEMORY;
666 if (!tevent_req_poll(req, ev)) {
667 status = map_nt_error_from_unix(errno);
671 status = cli_pull_recv(req, received);
674 if (!NT_STATUS_IS_OK(status)) {
675 cli_set_error(cli, status);
680 static NTSTATUS cli_read_sink(char *buf, size_t n, void *priv)
682 char **pbuf = (char **)priv;
683 memcpy(*pbuf, buf, n);
688 ssize_t cli_read(struct cli_state *cli, uint16_t fnum, char *buf,
689 off_t offset, size_t size)
694 status = cli_pull(cli, fnum, offset, size, size,
695 cli_read_sink, &buf, &ret);
696 if (!NT_STATUS_IS_OK(status)) {
697 cli_set_error(cli, status);
703 /****************************************************************************
704 Issue a single SMBwrite and don't wait for a reply.
705 ****************************************************************************/
707 static bool cli_issue_write(struct cli_state *cli,
715 bool large_writex = false;
716 /* We can only do direct writes if not signing and not encrypting. */
717 bool direct_writes = !client_is_signing_on(cli) && !cli_encryption_on(cli);
719 if (!direct_writes && size + 1 > cli->bufsize) {
720 cli->outbuf = (char *)SMB_REALLOC(cli->outbuf, size + 1024);
724 cli->inbuf = (char *)SMB_REALLOC(cli->inbuf, size + 1024);
725 if (cli->inbuf == NULL) {
726 SAFE_FREE(cli->outbuf);
729 cli->bufsize = size + 1024;
732 memset(cli->outbuf,'\0',smb_size);
733 memset(cli->inbuf,'\0',smb_size);
735 if (cli->capabilities & CAP_LARGE_FILES) {
740 cli_set_message(cli->outbuf,14,0,True);
742 cli_set_message(cli->outbuf,12,0,True);
745 SCVAL(cli->outbuf,smb_com,SMBwriteX);
746 SSVAL(cli->outbuf,smb_tid,cli->cnum);
747 cli_setup_packet(cli);
749 SCVAL(cli->outbuf,smb_vwv0,0xFF);
750 SSVAL(cli->outbuf,smb_vwv2,fnum);
752 SIVAL(cli->outbuf,smb_vwv3,offset);
753 SIVAL(cli->outbuf,smb_vwv5,0);
754 SSVAL(cli->outbuf,smb_vwv7,mode);
756 SSVAL(cli->outbuf,smb_vwv8,(mode & 0x0008) ? size : 0);
758 * According to CIFS-TR-1p00, this following field should only
759 * be set if CAP_LARGE_WRITEX is set. We should check this
760 * locally. However, this check might already have been
761 * done by our callers.
763 SSVAL(cli->outbuf,smb_vwv9,(size>>16));
764 SSVAL(cli->outbuf,smb_vwv10,size);
765 /* +1 is pad byte. */
766 SSVAL(cli->outbuf,smb_vwv11,
767 smb_buf(cli->outbuf) - smb_base(cli->outbuf) + 1);
770 SIVAL(cli->outbuf,smb_vwv12,(((uint64_t)offset)>>32) & 0xffffffff);
773 p = smb_base(cli->outbuf) + SVAL(cli->outbuf,smb_vwv11) -1;
774 *p++ = '\0'; /* pad byte. */
775 if (!direct_writes) {
776 memcpy(p, buf, size);
778 if (size > 0x1FFFF) {
779 /* This is a POSIX 14 word large write. */
780 set_message_bcc(cli->outbuf, 0); /* Set bcc to zero. */
781 _smb_setlen_large(cli->outbuf,smb_size + 28 + 1 /* pad */ + size - 4);
783 cli_setup_bcc(cli, p+size);
786 show_msg(cli->outbuf);
788 /* For direct writes we now need to write the data
789 * directly out of buf. */
790 return cli_send_smb_direct_writeX(cli, buf, size);
792 return cli_send_smb(cli);
796 /****************************************************************************
798 write_mode: 0x0001 disallow write cacheing
799 0x0002 return bytes remaining
800 0x0004 use raw named pipe protocol
801 0x0008 start of message mode named pipe protocol
802 ****************************************************************************/
804 ssize_t cli_write(struct cli_state *cli,
805 uint16_t fnum, uint16 write_mode,
806 const char *buf, off_t offset, size_t size)
808 ssize_t bwritten = 0;
809 unsigned int issued = 0;
810 unsigned int received = 0;
815 if(cli->max_mux > 1) {
816 mpx = cli->max_mux-1;
821 writesize = cli_write_max_bufsize(cli, write_mode);
823 blocks = (size + (writesize-1)) / writesize;
825 while (received < blocks) {
827 while ((issued - received < mpx) && (issued < blocks)) {
828 ssize_t bsent = issued * writesize;
829 ssize_t size1 = MIN(writesize, size - bsent);
831 if (!cli_issue_write(cli, fnum, offset + bsent,
839 if (!cli_receive_smb(cli)) {
845 if (cli_is_error(cli))
848 bwritten += SVAL(cli->inbuf, smb_vwv2);
849 if (writesize > 0xFFFF) {
850 bwritten += (((int)(SVAL(cli->inbuf, smb_vwv4)))<<16);
854 while (received < issued && cli_receive_smb(cli)) {
861 /****************************************************************************
862 write to a file using a SMBwrite and not bypassing 0 byte writes
863 ****************************************************************************/
865 ssize_t cli_smbwrite(struct cli_state *cli,
866 uint16_t fnum, char *buf, off_t offset, size_t size1)
872 size_t size = MIN(size1, cli->max_xmit - 48);
874 memset(cli->outbuf,'\0',smb_size);
875 memset(cli->inbuf,'\0',smb_size);
877 cli_set_message(cli->outbuf,5, 0,True);
879 SCVAL(cli->outbuf,smb_com,SMBwrite);
880 SSVAL(cli->outbuf,smb_tid,cli->cnum);
881 cli_setup_packet(cli);
883 SSVAL(cli->outbuf,smb_vwv0,fnum);
884 SSVAL(cli->outbuf,smb_vwv1,size);
885 SIVAL(cli->outbuf,smb_vwv2,offset);
886 SSVAL(cli->outbuf,smb_vwv4,0);
888 p = smb_buf(cli->outbuf);
890 SSVAL(p, 0, size); p += 2;
891 memcpy(p, buf + total, size); p += size;
893 cli_setup_bcc(cli, p);
895 if (!cli_send_smb(cli))
898 if (!cli_receive_smb(cli))
901 if (cli_is_error(cli))
904 size = SVAL(cli->inbuf,smb_vwv0);
918 * Send a write&x request
921 struct cli_write_andx_state {
929 static void cli_write_andx_done(struct tevent_req *subreq);
931 struct tevent_req *cli_write_andx_create(TALLOC_CTX *mem_ctx,
932 struct event_context *ev,
933 struct cli_state *cli, uint16_t fnum,
934 uint16_t mode, const uint8_t *buf,
935 off_t offset, size_t size,
936 struct tevent_req **reqs_before,
938 struct tevent_req **psmbreq)
940 struct tevent_req *req, *subreq;
941 struct cli_write_andx_state *state;
942 bool bigoffset = ((cli->capabilities & CAP_LARGE_FILES) != 0);
943 uint8_t wct = bigoffset ? 14 : 12;
944 size_t max_write = cli_write_max_bufsize(cli, mode);
947 req = tevent_req_create(mem_ctx, &state, struct cli_write_andx_state);
952 state->size = MIN(size, max_write);
956 SCVAL(vwv+0, 0, 0xFF);
959 SSVAL(vwv+2, 0, fnum);
960 SIVAL(vwv+3, 0, offset);
962 SSVAL(vwv+7, 0, mode);
964 SSVAL(vwv+9, 0, (state->size>>16));
965 SSVAL(vwv+10, 0, state->size);
968 cli_smb_wct_ofs(reqs_before, num_reqs_before)
969 + 1 /* the wct field */
971 + 2 /* num_bytes field */
975 SIVAL(vwv+12, 0, (((uint64_t)offset)>>32) & 0xffffffff);
979 state->iov[0].iov_base = (void *)&state->pad;
980 state->iov[0].iov_len = 1;
981 state->iov[1].iov_base = CONST_DISCARD(void *, buf);
982 state->iov[1].iov_len = state->size;
984 subreq = cli_smb_req_create(state, ev, cli, SMBwriteX, 0, wct, vwv,
986 if (tevent_req_nomem(subreq, req)) {
987 return tevent_req_post(req, ev);
989 tevent_req_set_callback(subreq, cli_write_andx_done, req);
994 struct tevent_req *cli_write_andx_send(TALLOC_CTX *mem_ctx,
995 struct event_context *ev,
996 struct cli_state *cli, uint16_t fnum,
997 uint16_t mode, const uint8_t *buf,
998 off_t offset, size_t size)
1000 struct tevent_req *req, *subreq;
1003 req = cli_write_andx_create(mem_ctx, ev, cli, fnum, mode, buf, offset,
1004 size, NULL, 0, &subreq);
1009 status = cli_smb_req_send(subreq);
1010 if (!NT_STATUS_IS_OK(status)) {
1011 tevent_req_nterror(req, status);
1012 return tevent_req_post(req, ev);
1017 static void cli_write_andx_done(struct tevent_req *subreq)
1019 struct tevent_req *req = tevent_req_callback_data(
1020 subreq, struct tevent_req);
1021 struct cli_write_andx_state *state = tevent_req_data(
1022 req, struct cli_write_andx_state);
1027 status = cli_smb_recv(subreq, 6, &wct, &vwv, NULL, NULL);
1028 if (NT_STATUS_IS_ERR(status)) {
1029 TALLOC_FREE(subreq);
1030 tevent_req_nterror(req, status);
1033 state->written = SVAL(vwv+2, 0);
1034 if (state->size > UINT16_MAX) {
1036 * It is important that we only set the
1037 * high bits only if we asked for a large write.
1039 * OS/2 print shares get this wrong and may send
1044 state->written |= SVAL(vwv+4, 0)<<16;
1046 tevent_req_done(req);
1049 NTSTATUS cli_write_andx_recv(struct tevent_req *req, size_t *pwritten)
1051 struct cli_write_andx_state *state = tevent_req_data(
1052 req, struct cli_write_andx_state);
1055 if (tevent_req_is_nterror(req, &status)) {
1058 *pwritten = state->written;
1059 return NT_STATUS_OK;
1062 struct cli_writeall_state {
1063 struct event_context *ev;
1064 struct cli_state *cli;
1073 static void cli_writeall_written(struct tevent_req *req);
1075 static struct tevent_req *cli_writeall_send(TALLOC_CTX *mem_ctx,
1076 struct event_context *ev,
1077 struct cli_state *cli,
1081 off_t offset, size_t size)
1083 struct tevent_req *req, *subreq;
1084 struct cli_writeall_state *state;
1086 req = tevent_req_create(mem_ctx, &state, struct cli_writeall_state);
1095 state->offset = offset;
1099 subreq = cli_write_andx_send(state, state->ev, state->cli, state->fnum,
1100 state->mode, state->buf, state->offset,
1102 if (tevent_req_nomem(subreq, req)) {
1103 return tevent_req_post(req, ev);
1105 tevent_req_set_callback(subreq, cli_writeall_written, req);
1109 static void cli_writeall_written(struct tevent_req *subreq)
1111 struct tevent_req *req = tevent_req_callback_data(
1112 subreq, struct tevent_req);
1113 struct cli_writeall_state *state = tevent_req_data(
1114 req, struct cli_writeall_state);
1116 size_t written, to_write;
1118 status = cli_write_andx_recv(subreq, &written);
1119 TALLOC_FREE(subreq);
1120 if (!NT_STATUS_IS_OK(status)) {
1121 tevent_req_nterror(req, status);
1125 state->written += written;
1127 if (state->written > state->size) {
1128 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
1132 to_write = state->size - state->written;
1134 if (to_write == 0) {
1135 tevent_req_done(req);
1139 subreq = cli_write_andx_send(state, state->ev, state->cli, state->fnum,
1141 state->buf + state->written,
1142 state->offset + state->written, to_write);
1143 if (tevent_req_nomem(subreq, req)) {
1146 tevent_req_set_callback(subreq, cli_writeall_written, req);
1149 static NTSTATUS cli_writeall_recv(struct tevent_req *req)
1151 return tevent_req_simple_recv_ntstatus(req);
1154 struct cli_push_write_state {
1155 struct tevent_req *req;/* This is the main request! Not the subreq */
1162 struct cli_push_state {
1163 struct event_context *ev;
1164 struct cli_state *cli;
1170 size_t (*source)(uint8_t *buf, size_t n, void *priv);
1179 * Outstanding requests
1183 struct cli_push_write_state **reqs;
1186 static void cli_push_written(struct tevent_req *req);
1188 static bool cli_push_write_setup(struct tevent_req *req,
1189 struct cli_push_state *state,
1192 struct cli_push_write_state *substate;
1193 struct tevent_req *subreq;
1195 substate = talloc(state->reqs, struct cli_push_write_state);
1199 substate->req = req;
1200 substate->idx = idx;
1201 substate->ofs = state->next_offset;
1202 substate->buf = talloc_array(substate, uint8_t, state->chunk_size);
1203 if (!substate->buf) {
1204 talloc_free(substate);
1207 substate->size = state->source(substate->buf,
1210 if (substate->size == 0) {
1212 /* nothing to send */
1213 talloc_free(substate);
1217 subreq = cli_writeall_send(substate,
1218 state->ev, state->cli,
1219 state->fnum, state->mode,
1224 talloc_free(substate);
1227 tevent_req_set_callback(subreq, cli_push_written, substate);
1229 state->reqs[idx] = substate;
1230 state->pending += 1;
1231 state->next_offset += substate->size;
1236 struct tevent_req *cli_push_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
1237 struct cli_state *cli,
1238 uint16_t fnum, uint16_t mode,
1239 off_t start_offset, size_t window_size,
1240 size_t (*source)(uint8_t *buf, size_t n,
1244 struct tevent_req *req;
1245 struct cli_push_state *state;
1248 req = tevent_req_create(mem_ctx, &state, struct cli_push_state);
1255 state->start_offset = start_offset;
1257 state->source = source;
1261 state->next_offset = start_offset;
1263 state->chunk_size = cli_write_max_bufsize(cli, mode);
1265 if (window_size == 0) {
1266 window_size = cli->max_mux * state->chunk_size;
1268 state->num_reqs = window_size/state->chunk_size;
1269 if ((window_size % state->chunk_size) > 0) {
1270 state->num_reqs += 1;
1272 state->num_reqs = MIN(state->num_reqs, cli->max_mux);
1273 state->num_reqs = MAX(state->num_reqs, 1);
1275 state->reqs = TALLOC_ZERO_ARRAY(state, struct cli_push_write_state *,
1277 if (state->reqs == NULL) {
1281 for (i=0; i<state->num_reqs; i++) {
1282 if (!cli_push_write_setup(req, state, i)) {
1291 if (state->pending == 0) {
1292 tevent_req_done(req);
1293 return tevent_req_post(req, ev);
1299 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
1300 return tevent_req_post(req, ev);
1303 static void cli_push_written(struct tevent_req *subreq)
1305 struct cli_push_write_state *substate = tevent_req_callback_data(
1306 subreq, struct cli_push_write_state);
1307 struct tevent_req *req = substate->req;
1308 struct cli_push_state *state = tevent_req_data(
1309 req, struct cli_push_state);
1311 uint32_t idx = substate->idx;
1313 state->reqs[idx] = NULL;
1314 state->pending -= 1;
1316 status = cli_writeall_recv(subreq);
1317 TALLOC_FREE(subreq);
1318 TALLOC_FREE(substate);
1319 if (!NT_STATUS_IS_OK(status)) {
1320 tevent_req_nterror(req, status);
1325 if (!cli_push_write_setup(req, state, idx)) {
1326 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
1331 if (state->pending == 0) {
1332 tevent_req_done(req);
1337 NTSTATUS cli_push_recv(struct tevent_req *req)
1339 return tevent_req_simple_recv_ntstatus(req);
1342 NTSTATUS cli_push(struct cli_state *cli, uint16_t fnum, uint16_t mode,
1343 off_t start_offset, size_t window_size,
1344 size_t (*source)(uint8_t *buf, size_t n, void *priv),
1347 TALLOC_CTX *frame = talloc_stackframe();
1348 struct event_context *ev;
1349 struct tevent_req *req;
1350 NTSTATUS status = NT_STATUS_OK;
1352 if (cli_has_async_calls(cli)) {
1354 * Can't use sync call while an async call is in flight
1356 status = NT_STATUS_INVALID_PARAMETER;
1360 ev = event_context_init(frame);
1362 status = NT_STATUS_NO_MEMORY;
1366 req = cli_push_send(frame, ev, cli, fnum, mode, start_offset,
1367 window_size, source, priv);
1369 status = NT_STATUS_NO_MEMORY;
1373 if (!tevent_req_poll(req, ev)) {
1374 status = map_nt_error_from_unix(errno);
1378 status = cli_push_recv(req);
1381 if (!NT_STATUS_IS_OK(status)) {
1382 cli_set_error(cli, status);