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/>.
21 #include "libsmb/libsmb.h"
22 #include "../lib/util/tevent_ntstatus.h"
23 #include "async_smb.h"
26 /****************************************************************************
27 Calculate the recommended read buffer size
28 ****************************************************************************/
29 static size_t cli_read_max_bufsize(struct cli_state *cli)
34 uint32_t useable_space = 0;
36 data_offset = HDR_VWV;
37 data_offset += wct * sizeof(uint16_t);
38 data_offset += sizeof(uint16_t); /* byte count */
39 data_offset += 1; /* pad */
41 min_space = cli_state_available_size(cli, data_offset);
43 if (cli->server_posix_capabilities & CIFS_UNIX_LARGE_READ_CAP) {
44 useable_space = 0xFFFFFF - data_offset;
46 if (client_is_signing_on(cli)) {
50 if (cli_state_encryption_on(cli)) {
55 } else if (cli_state_capabilities(cli) & CAP_LARGE_READX) {
57 * Note: CAP_LARGE_READX also works with signing
59 useable_space = 0x1FFFF - data_offset;
61 useable_space = MIN(useable_space, UINT16_MAX);
69 /****************************************************************************
70 Calculate the recommended write buffer size
71 ****************************************************************************/
72 static size_t cli_write_max_bufsize(struct cli_state *cli,
78 uint32_t useable_space = 0;
80 data_offset = HDR_VWV;
81 data_offset += wct * sizeof(uint16_t);
82 data_offset += sizeof(uint16_t); /* byte count */
83 data_offset += 1; /* pad */
85 min_space = cli_state_available_size(cli, data_offset);
87 if (cli->server_posix_capabilities & CIFS_UNIX_LARGE_WRITE_CAP) {
88 useable_space = 0xFFFFFF - data_offset;
89 } else if (cli_state_capabilities(cli) & CAP_LARGE_WRITEX) {
90 useable_space = 0x1FFFF - data_offset;
95 if (write_mode != 0) {
99 if (client_is_signing_on(cli)) {
103 if (cli_state_encryption_on(cli)) {
107 if (strequal(cli->dev, "LPT1:")) {
111 return useable_space;
114 struct cli_read_andx_state {
122 static void cli_read_andx_done(struct tevent_req *subreq);
124 struct tevent_req *cli_read_andx_create(TALLOC_CTX *mem_ctx,
125 struct event_context *ev,
126 struct cli_state *cli, uint16_t fnum,
127 off_t offset, size_t size,
128 struct tevent_req **psmbreq)
130 struct tevent_req *req, *subreq;
131 struct cli_read_andx_state *state;
134 if (size > cli_read_max_bufsize(cli)) {
135 DEBUG(0, ("cli_read_andx_send got size=%d, can only handle "
136 "size=%d\n", (int)size,
137 (int)cli_read_max_bufsize(cli)));
141 req = tevent_req_create(mem_ctx, &state, struct cli_read_andx_state);
147 SCVAL(state->vwv + 0, 0, 0xFF);
148 SCVAL(state->vwv + 0, 1, 0);
149 SSVAL(state->vwv + 1, 0, 0);
150 SSVAL(state->vwv + 2, 0, fnum);
151 SIVAL(state->vwv + 3, 0, offset);
152 SSVAL(state->vwv + 5, 0, size);
153 SSVAL(state->vwv + 6, 0, size);
154 SSVAL(state->vwv + 7, 0, (size >> 16));
155 SSVAL(state->vwv + 8, 0, 0);
156 SSVAL(state->vwv + 9, 0, 0);
158 if (cli_state_capabilities(cli) & CAP_LARGE_FILES) {
159 SIVAL(state->vwv + 10, 0,
160 (((uint64_t)offset)>>32) & 0xffffffff);
163 if ((((uint64_t)offset) & 0xffffffff00000000LL) != 0) {
164 DEBUG(10, ("cli_read_andx_send got large offset where "
165 "the server does not support it\n"));
166 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
167 return tevent_req_post(req, ev);
171 subreq = cli_smb_req_create(state, ev, cli, SMBreadX, 0, wct,
172 state->vwv, 0, NULL);
173 if (subreq == NULL) {
177 tevent_req_set_callback(subreq, cli_read_andx_done, req);
182 struct tevent_req *cli_read_andx_send(TALLOC_CTX *mem_ctx,
183 struct event_context *ev,
184 struct cli_state *cli, uint16_t fnum,
185 off_t offset, size_t size)
187 struct tevent_req *req, *subreq;
190 req = cli_read_andx_create(mem_ctx, ev, cli, fnum, offset, size,
196 status = cli_smb_req_send(subreq);
197 if (tevent_req_nterror(req, status)) {
198 return tevent_req_post(req, ev);
203 static void cli_read_andx_done(struct tevent_req *subreq)
205 struct tevent_req *req = tevent_req_callback_data(
206 subreq, struct tevent_req);
207 struct cli_read_andx_state *state = tevent_req_data(
208 req, struct cli_read_andx_state);
215 state->status = cli_smb_recv(subreq, state, &inbuf, 12, &wct, &vwv,
218 if (NT_STATUS_IS_ERR(state->status)) {
219 tevent_req_nterror(req, state->status);
223 /* size is the number of bytes the server returned.
225 state->received = SVAL(vwv + 5, 0);
226 state->received |= (((unsigned int)SVAL(vwv + 7, 0)) << 16);
228 if (state->received > state->size) {
229 DEBUG(5,("server returned more than we wanted!\n"));
230 tevent_req_nterror(req, NT_STATUS_UNEXPECTED_IO_ERROR);
235 * bcc field must be valid for small reads, for large reads the 16-bit
236 * bcc field can't be correct.
239 if ((state->received < 0xffff) && (state->received > num_bytes)) {
240 DEBUG(5, ("server announced more bytes than sent\n"));
241 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
245 state->buf = discard_const_p(uint8_t, smb_base(inbuf)) + SVAL(vwv+6, 0);
247 if (trans_oob(smb_len_tcp(inbuf), SVAL(vwv+6, 0), state->received)
248 || ((state->received != 0) && (state->buf < bytes))) {
249 DEBUG(5, ("server returned invalid read&x data offset\n"));
250 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
253 tevent_req_done(req);
257 * Pull the data out of a finished async read_and_x request. rcvbuf is
258 * talloced from the request, so better make sure that you copy it away before
259 * you talloc_free(req). "rcvbuf" is NOT a talloc_ctx of its own, so do not
263 NTSTATUS cli_read_andx_recv(struct tevent_req *req, ssize_t *received,
266 struct cli_read_andx_state *state = tevent_req_data(
267 req, struct cli_read_andx_state);
270 if (tevent_req_is_nterror(req, &status)) {
273 *received = state->received;
274 *rcvbuf = state->buf;
278 struct cli_readall_state {
279 struct tevent_context *ev;
280 struct cli_state *cli;
288 static void cli_readall_done(struct tevent_req *subreq);
290 static struct tevent_req *cli_readall_send(TALLOC_CTX *mem_ctx,
291 struct event_context *ev,
292 struct cli_state *cli,
294 off_t offset, size_t size)
296 struct tevent_req *req, *subreq;
297 struct cli_readall_state *state;
299 req = tevent_req_create(mem_ctx, &state, struct cli_readall_state);
306 state->start_offset = offset;
311 subreq = cli_read_andx_send(state, ev, cli, fnum, offset, size);
312 if (tevent_req_nomem(subreq, req)) {
313 return tevent_req_post(req, ev);
315 tevent_req_set_callback(subreq, cli_readall_done, req);
319 static void cli_readall_done(struct tevent_req *subreq)
321 struct tevent_req *req = tevent_req_callback_data(
322 subreq, struct tevent_req);
323 struct cli_readall_state *state = tevent_req_data(
324 req, struct cli_readall_state);
329 status = cli_read_andx_recv(subreq, &received, &buf);
330 if (tevent_req_nterror(req, status)) {
336 tevent_req_done(req);
340 if ((state->received == 0) && (received == state->size)) {
341 /* Ideal case: Got it all in one run */
343 state->received += received;
344 tevent_req_done(req);
349 * We got a short read, issue a read for the
350 * rest. Unfortunately we have to allocate the buffer
351 * ourselves now, as our caller expects to receive a single
352 * buffer. cli_read_andx does it from the buffer received from
353 * the net, but with a short read we have to put it together
354 * from several reads.
357 if (state->buf == NULL) {
358 state->buf = talloc_array(state, uint8_t, state->size);
359 if (tevent_req_nomem(state->buf, req)) {
363 memcpy(state->buf + state->received, buf, received);
364 state->received += received;
368 if (state->received >= state->size) {
369 tevent_req_done(req);
373 subreq = cli_read_andx_send(state, state->ev, state->cli, state->fnum,
374 state->start_offset + state->received,
375 state->size - state->received);
376 if (tevent_req_nomem(subreq, req)) {
379 tevent_req_set_callback(subreq, cli_readall_done, req);
382 static NTSTATUS cli_readall_recv(struct tevent_req *req, ssize_t *received,
385 struct cli_readall_state *state = tevent_req_data(
386 req, struct cli_readall_state);
389 if (tevent_req_is_nterror(req, &status)) {
392 *received = state->received;
393 *rcvbuf = state->buf;
397 struct cli_pull_subreq {
398 struct tevent_req *req;
404 * Parallel read support.
406 * cli_pull sends as many read&x requests as the server would allow via
407 * max_mux at a time. When replies flow back in, the data is written into
408 * the callback function "sink" in the right order.
411 struct cli_pull_state {
412 struct tevent_req *req;
414 struct event_context *ev;
415 struct cli_state *cli;
420 NTSTATUS (*sink)(char *buf, size_t n, void *priv);
426 * Outstanding requests
430 struct cli_pull_subreq *reqs;
433 * For how many bytes did we send requests already?
438 * Next request index to push into "sink". This walks around the "req"
439 * array, taking care that the requests are pushed to "sink" in the
440 * right order. If necessary (i.e. replies don't come in in the right
441 * order), replies are held back in "reqs".
446 * How many bytes did we push into "sink"?
452 static char *cli_pull_print(struct tevent_req *req, TALLOC_CTX *mem_ctx)
454 struct cli_pull_state *state = tevent_req_data(
455 req, struct cli_pull_state);
458 result = tevent_req_default_print(req, mem_ctx);
459 if (result == NULL) {
463 return talloc_asprintf_append_buffer(
464 result, "num_reqs=%d, top_req=%d",
465 state->num_reqs, state->top_req);
468 static void cli_pull_read_done(struct tevent_req *read_req);
471 * Prepare an async pull request
474 struct tevent_req *cli_pull_send(TALLOC_CTX *mem_ctx,
475 struct event_context *ev,
476 struct cli_state *cli,
477 uint16_t fnum, off_t start_offset,
478 off_t size, size_t window_size,
479 NTSTATUS (*sink)(char *buf, size_t n,
483 struct tevent_req *req;
484 struct cli_pull_state *state;
486 size_t page_size = 1024;
488 req = tevent_req_create(mem_ctx, &state, struct cli_pull_state);
492 tevent_req_set_print_fn(req, cli_pull_print);
498 state->start_offset = start_offset;
507 tevent_req_done(req);
508 return tevent_req_post(req, ev);
511 state->chunk_size = cli_read_max_bufsize(cli);
512 if (state->chunk_size > page_size) {
513 state->chunk_size &= ~(page_size - 1);
516 state->max_reqs = cli_state_max_requests(cli);
518 state->num_reqs = MAX(window_size/state->chunk_size, 1);
519 state->num_reqs = MIN(state->num_reqs, state->max_reqs);
521 state->reqs = talloc_zero_array(state, struct cli_pull_subreq,
523 if (state->reqs == NULL) {
527 state->requested = 0;
529 for (i=0; i<state->num_reqs; i++) {
530 struct cli_pull_subreq *subreq = &state->reqs[i];
532 size_t request_thistime;
534 if (state->requested >= size) {
539 size_left = size - state->requested;
540 request_thistime = MIN(size_left, state->chunk_size);
542 subreq->req = cli_readall_send(
543 state->reqs, ev, cli, fnum,
544 state->start_offset + state->requested,
547 if (subreq->req == NULL) {
550 tevent_req_set_callback(subreq->req, cli_pull_read_done, req);
551 state->requested += request_thistime;
561 * Handle incoming read replies, push the data into sink and send out new
562 * requests if necessary.
565 static void cli_pull_read_done(struct tevent_req *subreq)
567 struct tevent_req *req = tevent_req_callback_data(
568 subreq, struct tevent_req);
569 struct cli_pull_state *state = tevent_req_data(
570 req, struct cli_pull_state);
571 struct cli_pull_subreq *pull_subreq = NULL;
575 for (i = 0; i < state->num_reqs; i++) {
576 pull_subreq = &state->reqs[i];
577 if (subreq == pull_subreq->req) {
581 if (i == state->num_reqs) {
582 /* Huh -- received something we did not send?? */
583 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
587 status = cli_readall_recv(subreq, &pull_subreq->received,
589 if (!NT_STATUS_IS_OK(status)) {
590 tevent_req_nterror(state->req, status);
595 * This loop is the one to take care of out-of-order replies. All
596 * pending requests are in state->reqs, state->reqs[top_req] is the
597 * one that is to be pushed next. If however a request later than
598 * top_req is replied to, then we can't push yet. If top_req is
599 * replied to at a later point then, we need to push all the finished
603 while (state->reqs[state->top_req].req != NULL) {
604 struct cli_pull_subreq *top_subreq;
606 DEBUG(11, ("cli_pull_read_done: top_req = %d\n",
609 top_subreq = &state->reqs[state->top_req];
611 if (tevent_req_is_in_progress(top_subreq->req)) {
612 DEBUG(11, ("cli_pull_read_done: top request not yet "
617 DEBUG(10, ("cli_pull_read_done: Pushing %d bytes, %d already "
618 "pushed\n", (int)top_subreq->received,
619 (int)state->pushed));
621 status = state->sink((char *)top_subreq->buf,
622 top_subreq->received, state->priv);
623 if (tevent_req_nterror(state->req, status)) {
626 state->pushed += top_subreq->received;
628 TALLOC_FREE(state->reqs[state->top_req].req);
630 if (state->requested < state->size) {
631 struct tevent_req *new_req;
633 size_t request_thistime;
635 size_left = state->size - state->requested;
636 request_thistime = MIN(size_left, state->chunk_size);
638 DEBUG(10, ("cli_pull_read_done: Requesting %d bytes "
639 "at %d, position %d\n",
640 (int)request_thistime,
641 (int)(state->start_offset
645 new_req = cli_readall_send(
646 state->reqs, state->ev, state->cli,
648 state->start_offset + state->requested,
651 if (tevent_req_nomem(new_req, state->req)) {
654 tevent_req_set_callback(new_req, cli_pull_read_done,
657 state->reqs[state->top_req].req = new_req;
658 state->requested += request_thistime;
661 state->top_req = (state->top_req+1) % state->num_reqs;
664 tevent_req_done(req);
667 NTSTATUS cli_pull_recv(struct tevent_req *req, off_t *received)
669 struct cli_pull_state *state = tevent_req_data(
670 req, struct cli_pull_state);
673 if (tevent_req_is_nterror(req, &status)) {
676 *received = state->pushed;
680 NTSTATUS cli_pull(struct cli_state *cli, uint16_t fnum,
681 off_t start_offset, off_t size, size_t window_size,
682 NTSTATUS (*sink)(char *buf, size_t n, void *priv),
683 void *priv, off_t *received)
685 TALLOC_CTX *frame = talloc_stackframe();
686 struct event_context *ev;
687 struct tevent_req *req;
688 NTSTATUS status = NT_STATUS_OK;
690 if (cli_has_async_calls(cli)) {
692 * Can't use sync call while an async call is in flight
694 status = NT_STATUS_INVALID_PARAMETER;
698 ev = event_context_init(frame);
700 status = NT_STATUS_NO_MEMORY;
704 req = cli_pull_send(frame, ev, cli, fnum, start_offset, size,
705 window_size, sink, priv);
707 status = NT_STATUS_NO_MEMORY;
711 if (!tevent_req_poll(req, ev)) {
712 status = map_nt_error_from_unix(errno);
716 status = cli_pull_recv(req, received);
722 static NTSTATUS cli_read_sink(char *buf, size_t n, void *priv)
724 char **pbuf = (char **)priv;
725 memcpy(*pbuf, buf, n);
730 NTSTATUS cli_read(struct cli_state *cli, uint16_t fnum,
731 char *buf, off_t offset, size_t size,
737 status = cli_pull(cli, fnum, offset, size, size,
738 cli_read_sink, &buf, &ret);
739 if (!NT_STATUS_IS_OK(status)) {
750 /****************************************************************************
751 write to a file using a SMBwrite and not bypassing 0 byte writes
752 ****************************************************************************/
754 NTSTATUS cli_smbwrite(struct cli_state *cli, uint16_t fnum, char *buf,
755 off_t offset, size_t size1, size_t *ptotal)
764 bytes = talloc_array(talloc_tos(), uint8_t, 3);
766 return NT_STATUS_NO_MEMORY;
771 uint32_t usable_space = cli_state_available_size(cli, 48);
772 size_t size = MIN(size1, usable_space);
773 struct tevent_req *req;
778 SSVAL(vwv+0, 0, fnum);
779 SSVAL(vwv+1, 0, size);
780 SIVAL(vwv+2, 0, offset);
783 bytes = talloc_realloc(talloc_tos(), bytes, uint8_t,
786 return NT_STATUS_NO_MEMORY;
788 SSVAL(bytes, 1, size);
789 memcpy(bytes + 3, buf + total, size);
791 status = cli_smb(talloc_tos(), cli, SMBwrite, 0, 5, vwv,
792 size+3, bytes, &req, 1, NULL, &ret_vwv,
794 if (!NT_STATUS_IS_OK(status)) {
799 size = SVAL(ret_vwv+0, 0);
812 if (ptotal != NULL) {
819 * Send a write&x request
822 struct cli_write_andx_state {
830 static void cli_write_andx_done(struct tevent_req *subreq);
832 struct tevent_req *cli_write_andx_create(TALLOC_CTX *mem_ctx,
833 struct event_context *ev,
834 struct cli_state *cli, uint16_t fnum,
835 uint16_t mode, const uint8_t *buf,
836 off_t offset, size_t size,
837 struct tevent_req **reqs_before,
839 struct tevent_req **psmbreq)
841 struct tevent_req *req, *subreq;
842 struct cli_write_andx_state *state;
843 bool bigoffset = ((cli_state_capabilities(cli) & CAP_LARGE_FILES) != 0);
844 uint8_t wct = bigoffset ? 14 : 12;
845 size_t max_write = cli_write_max_bufsize(cli, mode, wct);
848 req = tevent_req_create(mem_ctx, &state, struct cli_write_andx_state);
853 state->size = MIN(size, max_write);
857 SCVAL(vwv+0, 0, 0xFF);
860 SSVAL(vwv+2, 0, fnum);
861 SIVAL(vwv+3, 0, offset);
863 SSVAL(vwv+7, 0, mode);
865 SSVAL(vwv+9, 0, (state->size>>16));
866 SSVAL(vwv+10, 0, state->size);
869 cli_smb_wct_ofs(reqs_before, num_reqs_before)
870 + 1 /* the wct field */
872 + 2 /* num_bytes field */
876 SIVAL(vwv+12, 0, (((uint64_t)offset)>>32) & 0xffffffff);
880 state->iov[0].iov_base = (void *)&state->pad;
881 state->iov[0].iov_len = 1;
882 state->iov[1].iov_base = discard_const_p(void, buf);
883 state->iov[1].iov_len = state->size;
885 subreq = cli_smb_req_create(state, ev, cli, SMBwriteX, 0, wct, vwv,
887 if (tevent_req_nomem(subreq, req)) {
888 return tevent_req_post(req, ev);
890 tevent_req_set_callback(subreq, cli_write_andx_done, req);
895 struct tevent_req *cli_write_andx_send(TALLOC_CTX *mem_ctx,
896 struct event_context *ev,
897 struct cli_state *cli, uint16_t fnum,
898 uint16_t mode, const uint8_t *buf,
899 off_t offset, size_t size)
901 struct tevent_req *req, *subreq;
904 req = cli_write_andx_create(mem_ctx, ev, cli, fnum, mode, buf, offset,
905 size, NULL, 0, &subreq);
910 status = cli_smb_req_send(subreq);
911 if (tevent_req_nterror(req, status)) {
912 return tevent_req_post(req, ev);
917 static void cli_write_andx_done(struct tevent_req *subreq)
919 struct tevent_req *req = tevent_req_callback_data(
920 subreq, struct tevent_req);
921 struct cli_write_andx_state *state = tevent_req_data(
922 req, struct cli_write_andx_state);
928 status = cli_smb_recv(subreq, state, &inbuf, 6, &wct, &vwv,
931 if (NT_STATUS_IS_ERR(status)) {
932 tevent_req_nterror(req, status);
935 state->written = SVAL(vwv+2, 0);
936 if (state->size > UINT16_MAX) {
938 * It is important that we only set the
939 * high bits only if we asked for a large write.
941 * OS/2 print shares get this wrong and may send
946 state->written |= SVAL(vwv+4, 0)<<16;
948 tevent_req_done(req);
951 NTSTATUS cli_write_andx_recv(struct tevent_req *req, size_t *pwritten)
953 struct cli_write_andx_state *state = tevent_req_data(
954 req, struct cli_write_andx_state);
957 if (tevent_req_is_nterror(req, &status)) {
961 *pwritten = state->written;
966 struct cli_writeall_state {
967 struct event_context *ev;
968 struct cli_state *cli;
977 static void cli_writeall_written(struct tevent_req *req);
979 static struct tevent_req *cli_writeall_send(TALLOC_CTX *mem_ctx,
980 struct event_context *ev,
981 struct cli_state *cli,
985 off_t offset, size_t size)
987 struct tevent_req *req, *subreq;
988 struct cli_writeall_state *state;
990 req = tevent_req_create(mem_ctx, &state, struct cli_writeall_state);
999 state->offset = offset;
1003 subreq = cli_write_andx_send(state, state->ev, state->cli, state->fnum,
1004 state->mode, state->buf, state->offset,
1006 if (tevent_req_nomem(subreq, req)) {
1007 return tevent_req_post(req, ev);
1009 tevent_req_set_callback(subreq, cli_writeall_written, req);
1013 static void cli_writeall_written(struct tevent_req *subreq)
1015 struct tevent_req *req = tevent_req_callback_data(
1016 subreq, struct tevent_req);
1017 struct cli_writeall_state *state = tevent_req_data(
1018 req, struct cli_writeall_state);
1020 size_t written, to_write;
1022 status = cli_write_andx_recv(subreq, &written);
1023 TALLOC_FREE(subreq);
1024 if (tevent_req_nterror(req, status)) {
1028 state->written += written;
1030 if (state->written > state->size) {
1031 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
1035 to_write = state->size - state->written;
1037 if (to_write == 0) {
1038 tevent_req_done(req);
1042 subreq = cli_write_andx_send(state, state->ev, state->cli, state->fnum,
1044 state->buf + state->written,
1045 state->offset + state->written, to_write);
1046 if (tevent_req_nomem(subreq, req)) {
1049 tevent_req_set_callback(subreq, cli_writeall_written, req);
1052 static NTSTATUS cli_writeall_recv(struct tevent_req *req,
1055 struct cli_writeall_state *state = tevent_req_data(
1056 req, struct cli_writeall_state);
1059 if (tevent_req_is_nterror(req, &status)) {
1062 if (pwritten != NULL) {
1063 *pwritten = state->written;
1065 return NT_STATUS_OK;
1068 NTSTATUS cli_writeall(struct cli_state *cli, uint16_t fnum, uint16_t mode,
1069 const uint8_t *buf, off_t offset, size_t size,
1072 TALLOC_CTX *frame = talloc_stackframe();
1073 struct event_context *ev;
1074 struct tevent_req *req;
1075 NTSTATUS status = NT_STATUS_NO_MEMORY;
1077 if (cli_has_async_calls(cli)) {
1079 * Can't use sync call while an async call is in flight
1081 status = NT_STATUS_INVALID_PARAMETER;
1084 ev = event_context_init(frame);
1088 req = cli_writeall_send(frame, ev, cli, fnum, mode, buf, offset, size);
1092 if (!tevent_req_poll(req, ev)) {
1093 status = map_nt_error_from_unix(errno);
1096 status = cli_writeall_recv(req, pwritten);
1102 struct cli_push_write_state {
1103 struct tevent_req *req;/* This is the main request! Not the subreq */
1110 struct cli_push_state {
1111 struct event_context *ev;
1112 struct cli_state *cli;
1118 size_t (*source)(uint8_t *buf, size_t n, void *priv);
1127 * Outstanding requests
1132 struct cli_push_write_state **reqs;
1135 static void cli_push_written(struct tevent_req *req);
1137 static bool cli_push_write_setup(struct tevent_req *req,
1138 struct cli_push_state *state,
1141 struct cli_push_write_state *substate;
1142 struct tevent_req *subreq;
1144 substate = talloc(state->reqs, struct cli_push_write_state);
1148 substate->req = req;
1149 substate->idx = idx;
1150 substate->ofs = state->next_offset;
1151 substate->buf = talloc_array(substate, uint8_t, state->chunk_size);
1152 if (!substate->buf) {
1153 talloc_free(substate);
1156 substate->size = state->source(substate->buf,
1159 if (substate->size == 0) {
1161 /* nothing to send */
1162 talloc_free(substate);
1166 subreq = cli_writeall_send(substate,
1167 state->ev, state->cli,
1168 state->fnum, state->mode,
1173 talloc_free(substate);
1176 tevent_req_set_callback(subreq, cli_push_written, substate);
1178 state->reqs[idx] = substate;
1179 state->pending += 1;
1180 state->next_offset += substate->size;
1185 struct tevent_req *cli_push_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
1186 struct cli_state *cli,
1187 uint16_t fnum, uint16_t mode,
1188 off_t start_offset, size_t window_size,
1189 size_t (*source)(uint8_t *buf, size_t n,
1193 struct tevent_req *req;
1194 struct cli_push_state *state;
1196 size_t page_size = 1024;
1198 req = tevent_req_create(mem_ctx, &state, struct cli_push_state);
1205 state->start_offset = start_offset;
1207 state->source = source;
1211 state->next_offset = start_offset;
1213 state->chunk_size = cli_write_max_bufsize(cli, mode, 14);
1214 if (state->chunk_size > page_size) {
1215 state->chunk_size &= ~(page_size - 1);
1218 state->max_reqs = cli_state_max_requests(cli);
1220 if (window_size == 0) {
1221 window_size = state->max_reqs * state->chunk_size;
1223 state->num_reqs = window_size/state->chunk_size;
1224 if ((window_size % state->chunk_size) > 0) {
1225 state->num_reqs += 1;
1227 state->num_reqs = MIN(state->num_reqs, state->max_reqs);
1228 state->num_reqs = MAX(state->num_reqs, 1);
1230 state->reqs = talloc_zero_array(state, struct cli_push_write_state *,
1232 if (state->reqs == NULL) {
1236 for (i=0; i<state->num_reqs; i++) {
1237 if (!cli_push_write_setup(req, state, i)) {
1246 if (state->pending == 0) {
1247 tevent_req_done(req);
1248 return tevent_req_post(req, ev);
1254 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
1255 return tevent_req_post(req, ev);
1258 static void cli_push_written(struct tevent_req *subreq)
1260 struct cli_push_write_state *substate = tevent_req_callback_data(
1261 subreq, struct cli_push_write_state);
1262 struct tevent_req *req = substate->req;
1263 struct cli_push_state *state = tevent_req_data(
1264 req, struct cli_push_state);
1266 uint32_t idx = substate->idx;
1268 state->reqs[idx] = NULL;
1269 state->pending -= 1;
1271 status = cli_writeall_recv(subreq, NULL);
1272 TALLOC_FREE(subreq);
1273 TALLOC_FREE(substate);
1274 if (tevent_req_nterror(req, status)) {
1279 if (!cli_push_write_setup(req, state, idx)) {
1280 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
1285 if (state->pending == 0) {
1286 tevent_req_done(req);
1291 NTSTATUS cli_push_recv(struct tevent_req *req)
1293 return tevent_req_simple_recv_ntstatus(req);
1296 NTSTATUS cli_push(struct cli_state *cli, uint16_t fnum, uint16_t mode,
1297 off_t start_offset, size_t window_size,
1298 size_t (*source)(uint8_t *buf, size_t n, void *priv),
1301 TALLOC_CTX *frame = talloc_stackframe();
1302 struct event_context *ev;
1303 struct tevent_req *req;
1304 NTSTATUS status = NT_STATUS_OK;
1306 if (cli_has_async_calls(cli)) {
1308 * Can't use sync call while an async call is in flight
1310 status = NT_STATUS_INVALID_PARAMETER;
1314 ev = event_context_init(frame);
1316 status = NT_STATUS_NO_MEMORY;
1320 req = cli_push_send(frame, ev, cli, fnum, mode, start_offset,
1321 window_size, source, priv);
1323 status = NT_STATUS_NO_MEMORY;
1327 if (!tevent_req_poll(req, ev)) {
1328 status = map_nt_error_from_unix(errno);
1332 status = cli_push_recv(req);