s3:libsmb: add cli_write_send/recv which work with SMB1/2/3
[Samba.git] / source3 / libsmb / clireadwrite.c
blob6d47ccd872ac01e6086a7381a78082d7d063daab
1 /*
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/>.
20 #include "includes.h"
21 #include "libsmb/libsmb.h"
22 #include "../lib/util/tevent_ntstatus.h"
23 #include "async_smb.h"
24 #include "trans2.h"
25 #include "../libcli/smb/smbXcli_base.h"
27 /****************************************************************************
28 Calculate the recommended read buffer size
29 ****************************************************************************/
30 static size_t cli_read_max_bufsize(struct cli_state *cli)
32 uint8_t wct = 12;
33 uint32_t min_space;
34 uint32_t data_offset;
35 uint32_t useable_space = 0;
37 data_offset = HDR_VWV;
38 data_offset += wct * sizeof(uint16_t);
39 data_offset += sizeof(uint16_t); /* byte count */
40 data_offset += 1; /* pad */
42 min_space = cli_state_available_size(cli, data_offset);
44 if (cli->server_posix_capabilities & CIFS_UNIX_LARGE_READ_CAP) {
45 useable_space = 0xFFFFFF - data_offset;
47 if (smb1cli_conn_signing_is_active(cli->conn)) {
48 return min_space;
51 if (smb1cli_conn_encryption_on(cli->conn)) {
52 return min_space;
55 return useable_space;
56 } else if (smb1cli_conn_capabilities(cli->conn) & CAP_LARGE_READX) {
58 * Note: CAP_LARGE_READX also works with signing
60 useable_space = 0x1FFFF - data_offset;
62 useable_space = MIN(useable_space, UINT16_MAX);
64 return useable_space;
67 return min_space;
70 /****************************************************************************
71 Calculate the recommended write buffer size
72 ****************************************************************************/
73 static size_t cli_write_max_bufsize(struct cli_state *cli,
74 uint16_t write_mode,
75 uint8_t wct)
77 uint32_t min_space;
78 uint32_t data_offset;
79 uint32_t useable_space = 0;
81 data_offset = HDR_VWV;
82 data_offset += wct * sizeof(uint16_t);
83 data_offset += sizeof(uint16_t); /* byte count */
84 data_offset += 1; /* pad */
86 min_space = cli_state_available_size(cli, data_offset);
88 if (cli->server_posix_capabilities & CIFS_UNIX_LARGE_WRITE_CAP) {
89 useable_space = 0xFFFFFF - data_offset;
90 } else if (smb1cli_conn_capabilities(cli->conn) & CAP_LARGE_WRITEX) {
91 useable_space = 0x1FFFF - data_offset;
92 } else {
93 return min_space;
96 if (write_mode != 0) {
97 return min_space;
100 if (smb1cli_conn_signing_is_active(cli->conn)) {
101 return min_space;
104 if (smb1cli_conn_encryption_on(cli->conn)) {
105 return min_space;
108 if (strequal(cli->dev, "LPT1:")) {
109 return min_space;
112 return useable_space;
115 struct cli_read_andx_state {
116 size_t size;
117 uint16_t vwv[12];
118 NTSTATUS status;
119 size_t received;
120 uint8_t *buf;
123 static void cli_read_andx_done(struct tevent_req *subreq);
125 struct tevent_req *cli_read_andx_create(TALLOC_CTX *mem_ctx,
126 struct tevent_context *ev,
127 struct cli_state *cli, uint16_t fnum,
128 off_t offset, size_t size,
129 struct tevent_req **psmbreq)
131 struct tevent_req *req, *subreq;
132 struct cli_read_andx_state *state;
133 uint8_t wct = 10;
135 req = tevent_req_create(mem_ctx, &state, struct cli_read_andx_state);
136 if (req == NULL) {
137 return NULL;
139 state->size = size;
141 SCVAL(state->vwv + 0, 0, 0xFF);
142 SCVAL(state->vwv + 0, 1, 0);
143 SSVAL(state->vwv + 1, 0, 0);
144 SSVAL(state->vwv + 2, 0, fnum);
145 SIVAL(state->vwv + 3, 0, offset);
146 SSVAL(state->vwv + 5, 0, size);
147 SSVAL(state->vwv + 6, 0, size);
148 SSVAL(state->vwv + 7, 0, (size >> 16));
149 SSVAL(state->vwv + 8, 0, 0);
150 SSVAL(state->vwv + 9, 0, 0);
152 if (smb1cli_conn_capabilities(cli->conn) & CAP_LARGE_FILES) {
153 SIVAL(state->vwv + 10, 0,
154 (((uint64_t)offset)>>32) & 0xffffffff);
155 wct = 12;
156 } else {
157 if ((((uint64_t)offset) & 0xffffffff00000000LL) != 0) {
158 DEBUG(10, ("cli_read_andx_send got large offset where "
159 "the server does not support it\n"));
160 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
161 return tevent_req_post(req, ev);
165 subreq = cli_smb_req_create(state, ev, cli, SMBreadX, 0, 0, wct,
166 state->vwv, 0, NULL);
167 if (subreq == NULL) {
168 TALLOC_FREE(req);
169 return NULL;
171 tevent_req_set_callback(subreq, cli_read_andx_done, req);
172 *psmbreq = subreq;
173 return req;
176 struct tevent_req *cli_read_andx_send(TALLOC_CTX *mem_ctx,
177 struct tevent_context *ev,
178 struct cli_state *cli, uint16_t fnum,
179 off_t offset, size_t size)
181 struct tevent_req *req, *subreq;
182 NTSTATUS status;
184 req = cli_read_andx_create(mem_ctx, ev, cli, fnum, offset, size,
185 &subreq);
186 if (req == NULL) {
187 return NULL;
190 status = smb1cli_req_chain_submit(&subreq, 1);
191 if (tevent_req_nterror(req, status)) {
192 return tevent_req_post(req, ev);
194 return req;
197 static void cli_read_andx_done(struct tevent_req *subreq)
199 struct tevent_req *req = tevent_req_callback_data(
200 subreq, struct tevent_req);
201 struct cli_read_andx_state *state = tevent_req_data(
202 req, struct cli_read_andx_state);
203 uint8_t *inbuf;
204 uint8_t wct;
205 uint16_t *vwv;
206 uint32_t num_bytes;
207 uint8_t *bytes;
209 state->status = cli_smb_recv(subreq, state, &inbuf, 12, &wct, &vwv,
210 &num_bytes, &bytes);
211 TALLOC_FREE(subreq);
212 if (NT_STATUS_IS_ERR(state->status)) {
213 tevent_req_nterror(req, state->status);
214 return;
217 /* size is the number of bytes the server returned.
218 * Might be zero. */
219 state->received = SVAL(vwv + 5, 0);
220 state->received |= (((unsigned int)SVAL(vwv + 7, 0)) << 16);
222 if (state->received > state->size) {
223 DEBUG(5,("server returned more than we wanted!\n"));
224 tevent_req_nterror(req, NT_STATUS_UNEXPECTED_IO_ERROR);
225 return;
229 * bcc field must be valid for small reads, for large reads the 16-bit
230 * bcc field can't be correct.
233 if ((state->received < 0xffff) && (state->received > num_bytes)) {
234 DEBUG(5, ("server announced more bytes than sent\n"));
235 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
236 return;
239 state->buf = discard_const_p(uint8_t, smb_base(inbuf)) + SVAL(vwv+6, 0);
241 if (trans_oob(smb_len_tcp(inbuf), SVAL(vwv+6, 0), state->received)
242 || ((state->received != 0) && (state->buf < bytes))) {
243 DEBUG(5, ("server returned invalid read&x data offset\n"));
244 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
245 return;
247 tevent_req_done(req);
251 * Pull the data out of a finished async read_and_x request. rcvbuf is
252 * talloced from the request, so better make sure that you copy it away before
253 * you talloc_free(req). "rcvbuf" is NOT a talloc_ctx of its own, so do not
254 * talloc_move it!
257 NTSTATUS cli_read_andx_recv(struct tevent_req *req, ssize_t *received,
258 uint8_t **rcvbuf)
260 struct cli_read_andx_state *state = tevent_req_data(
261 req, struct cli_read_andx_state);
262 NTSTATUS status;
264 if (tevent_req_is_nterror(req, &status)) {
265 return status;
267 *received = state->received;
268 *rcvbuf = state->buf;
269 return NT_STATUS_OK;
272 struct cli_pull_chunk;
274 struct cli_pull_state {
275 struct tevent_context *ev;
276 struct cli_state *cli;
277 uint16_t fnum;
278 off_t start_offset;
279 off_t size;
281 NTSTATUS (*sink)(char *buf, size_t n, void *priv);
282 void *priv;
284 size_t chunk_size;
285 off_t next_offset;
286 off_t remaining;
289 * How many bytes did we push into "sink"?
291 off_t pushed;
294 * Outstanding requests
296 * The maximum is 256:
297 * - which would be a window of 256 MByte
298 * for SMB2 with multi-credit
299 * or smb1 unix extensions.
301 uint16_t max_chunks;
302 uint16_t num_chunks;
303 uint16_t num_waiting;
304 struct cli_pull_chunk *chunks;
307 struct cli_pull_chunk {
308 struct cli_pull_chunk *prev, *next;
309 struct tevent_req *req;/* This is the main request! Not the subreq */
310 struct tevent_req *subreq;
311 off_t ofs;
312 uint8_t *buf;
313 size_t total_size;
314 size_t tmp_size;
315 bool done;
318 static void cli_pull_setup_chunks(struct tevent_req *req);
319 static void cli_pull_chunk_ship(struct cli_pull_chunk *chunk);
320 static void cli_pull_chunk_done(struct tevent_req *subreq);
323 * Parallel read support.
325 * cli_pull sends as many read&x requests as the server would allow via
326 * max_mux at a time. When replies flow back in, the data is written into
327 * the callback function "sink" in the right order.
330 struct tevent_req *cli_pull_send(TALLOC_CTX *mem_ctx,
331 struct tevent_context *ev,
332 struct cli_state *cli,
333 uint16_t fnum, off_t start_offset,
334 off_t size, size_t window_size,
335 NTSTATUS (*sink)(char *buf, size_t n,
336 void *priv),
337 void *priv)
339 struct tevent_req *req;
340 struct cli_pull_state *state;
341 size_t page_size = 1024;
342 uint64_t tmp64;
344 req = tevent_req_create(mem_ctx, &state, struct cli_pull_state);
345 if (req == NULL) {
346 return NULL;
348 state->cli = cli;
349 state->ev = ev;
350 state->fnum = fnum;
351 state->start_offset = start_offset;
352 state->size = size;
353 state->sink = sink;
354 state->priv = priv;
355 state->next_offset = start_offset;
356 state->remaining = size;
358 if (size == 0) {
359 tevent_req_done(req);
360 return tevent_req_post(req, ev);
363 if (smbXcli_conn_protocol(state->cli->conn) >= PROTOCOL_SMB2_02) {
364 state->chunk_size = smb2cli_conn_max_read_size(cli->conn);
365 } else {
366 state->chunk_size = cli_read_max_bufsize(cli);
368 if (state->chunk_size > page_size) {
369 state->chunk_size &= ~(page_size - 1);
372 if (window_size == 0) {
374 * We use 16 MByte as default window size.
376 window_size = 16 * 1024 * 1024;
379 tmp64 = window_size/state->chunk_size;
380 if ((window_size % state->chunk_size) > 0) {
381 tmp64 += 1;
383 tmp64 = MAX(tmp64, 1);
384 tmp64 = MIN(tmp64, 256);
385 state->max_chunks = tmp64;
388 * We defer the callback because of the complex
389 * substate/subfunction logic
391 tevent_req_defer_callback(req, ev);
393 cli_pull_setup_chunks(req);
394 if (!tevent_req_is_in_progress(req)) {
395 return tevent_req_post(req, ev);
398 return req;
401 static void cli_pull_setup_chunks(struct tevent_req *req)
403 struct cli_pull_state *state =
404 tevent_req_data(req,
405 struct cli_pull_state);
406 struct cli_pull_chunk *chunk, *next = NULL;
407 size_t i;
409 for (chunk = state->chunks; chunk; chunk = next) {
411 * Note that chunk might be removed from this call.
413 next = chunk->next;
414 cli_pull_chunk_ship(chunk);
415 if (!tevent_req_is_in_progress(req)) {
416 return;
420 for (i = state->num_chunks; i < state->max_chunks; i++) {
422 if (state->num_waiting > 0) {
423 return;
426 if (state->remaining == 0) {
427 break;
430 chunk = talloc_zero(state, struct cli_pull_chunk);
431 if (tevent_req_nomem(chunk, req)) {
432 return;
434 chunk->req = req;
435 chunk->ofs = state->next_offset;
436 chunk->total_size = MIN(state->remaining, state->chunk_size);
437 state->next_offset += chunk->total_size;
438 state->remaining -= chunk->total_size;
440 DLIST_ADD_END(state->chunks, chunk);
441 state->num_chunks++;
442 state->num_waiting++;
444 cli_pull_chunk_ship(chunk);
445 if (!tevent_req_is_in_progress(req)) {
446 return;
450 if (state->remaining > 0) {
451 return;
454 if (state->num_chunks > 0) {
455 return;
458 tevent_req_done(req);
461 static void cli_pull_chunk_ship(struct cli_pull_chunk *chunk)
463 struct tevent_req *req = chunk->req;
464 struct cli_pull_state *state =
465 tevent_req_data(req,
466 struct cli_pull_state);
467 bool ok;
468 off_t ofs;
469 size_t size;
471 if (chunk->done) {
472 NTSTATUS status;
474 if (chunk != state->chunks) {
476 * this chunk is not the
477 * first one in the list.
479 * which means we should not
480 * push it into the sink yet.
482 return;
485 if (chunk->tmp_size == 0) {
487 * we got a short read, we're done
489 tevent_req_done(req);
490 return;
493 status = state->sink((char *)chunk->buf,
494 chunk->tmp_size,
495 state->priv);
496 if (tevent_req_nterror(req, status)) {
497 return;
499 state->pushed += chunk->tmp_size;
501 if (chunk->tmp_size < chunk->total_size) {
503 * we got a short read, we're done
505 tevent_req_done(req);
506 return;
509 DLIST_REMOVE(state->chunks, chunk);
510 SMB_ASSERT(state->num_chunks > 0);
511 state->num_chunks--;
512 TALLOC_FREE(chunk);
514 return;
517 if (chunk->subreq != NULL) {
518 return;
521 SMB_ASSERT(state->num_waiting > 0);
523 ofs = chunk->ofs + chunk->tmp_size;
524 size = chunk->total_size - chunk->tmp_size;
526 if (smbXcli_conn_protocol(state->cli->conn) >= PROTOCOL_SMB2_02) {
527 uint32_t max_size;
529 ok = smb2cli_conn_req_possible(state->cli->conn, &max_size);
530 if (!ok) {
531 return;
535 * downgrade depending on the available credits
537 size = MIN(max_size, size);
539 chunk->subreq = cli_smb2_read_send(chunk,
540 state->ev,
541 state->cli,
542 state->fnum,
543 ofs,
544 size);
545 if (tevent_req_nomem(chunk->subreq, req)) {
546 return;
548 } else {
549 ok = smb1cli_conn_req_possible(state->cli->conn);
550 if (!ok) {
551 return;
554 chunk->subreq = cli_read_andx_send(chunk,
555 state->ev,
556 state->cli,
557 state->fnum,
558 ofs,
559 size);
560 if (tevent_req_nomem(chunk->subreq, req)) {
561 return;
564 tevent_req_set_callback(chunk->subreq,
565 cli_pull_chunk_done,
566 chunk);
568 state->num_waiting--;
569 return;
572 static void cli_pull_chunk_done(struct tevent_req *subreq)
574 struct cli_pull_chunk *chunk =
575 tevent_req_callback_data(subreq,
576 struct cli_pull_chunk);
577 struct tevent_req *req = chunk->req;
578 struct cli_pull_state *state =
579 tevent_req_data(req,
580 struct cli_pull_state);
581 NTSTATUS status;
582 size_t expected = chunk->total_size - chunk->tmp_size;
583 ssize_t received;
584 uint8_t *buf = NULL;
586 chunk->subreq = NULL;
588 if (smbXcli_conn_protocol(state->cli->conn) >= PROTOCOL_SMB2_02) {
589 status = cli_smb2_read_recv(subreq, &received, &buf);
590 } else {
591 status = cli_read_andx_recv(subreq, &received, &buf);
593 if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
594 received = 0;
595 status = NT_STATUS_OK;
597 if (tevent_req_nterror(req, status)) {
598 return;
601 if (received > expected) {
602 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
603 return;
606 if (received == 0) {
608 * We got EOF we're done
610 chunk->done = true;
611 cli_pull_setup_chunks(req);
612 return;
615 if (received == chunk->total_size) {
617 * We got it in the first run.
619 * We don't call TALLOC_FREE(subreq)
620 * here and keep the returned buffer.
622 chunk->buf = buf;
623 } else if (chunk->buf == NULL) {
624 chunk->buf = talloc_array(chunk, uint8_t, chunk->total_size);
625 if (tevent_req_nomem(chunk->buf, req)) {
626 return;
630 if (received != chunk->total_size) {
631 uint8_t *p = chunk->buf + chunk->tmp_size;
632 memcpy(p, buf, received);
633 TALLOC_FREE(subreq);
636 chunk->tmp_size += received;
638 if (chunk->tmp_size == chunk->total_size) {
639 chunk->done = true;
640 } else {
641 state->num_waiting++;
644 cli_pull_setup_chunks(req);
647 NTSTATUS cli_pull_recv(struct tevent_req *req, off_t *received)
649 struct cli_pull_state *state = tevent_req_data(
650 req, struct cli_pull_state);
651 NTSTATUS status;
653 if (tevent_req_is_nterror(req, &status)) {
654 tevent_req_received(req);
655 return status;
657 *received = state->pushed;
658 tevent_req_received(req);
659 return NT_STATUS_OK;
662 NTSTATUS cli_pull(struct cli_state *cli, uint16_t fnum,
663 off_t start_offset, off_t size, size_t window_size,
664 NTSTATUS (*sink)(char *buf, size_t n, void *priv),
665 void *priv, off_t *received)
667 TALLOC_CTX *frame = talloc_stackframe();
668 struct tevent_context *ev;
669 struct tevent_req *req;
670 NTSTATUS status = NT_STATUS_OK;
672 if (smbXcli_conn_has_async_calls(cli->conn)) {
674 * Can't use sync call while an async call is in flight
676 status = NT_STATUS_INVALID_PARAMETER;
677 goto fail;
680 ev = samba_tevent_context_init(frame);
681 if (ev == NULL) {
682 status = NT_STATUS_NO_MEMORY;
683 goto fail;
686 req = cli_pull_send(frame, ev, cli, fnum, start_offset, size,
687 window_size, sink, priv);
688 if (req == NULL) {
689 status = NT_STATUS_NO_MEMORY;
690 goto fail;
693 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
694 goto fail;
697 status = cli_pull_recv(req, received);
698 fail:
699 TALLOC_FREE(frame);
700 return status;
703 struct cli_read_state {
704 struct cli_state *cli;
705 char *buf;
706 size_t buflen;
707 size_t received;
710 static void cli_read_done(struct tevent_req *subreq);
712 struct tevent_req *cli_read_send(
713 TALLOC_CTX *mem_ctx,
714 struct tevent_context *ev,
715 struct cli_state *cli,
716 uint16_t fnum,
717 char *buf,
718 off_t offset,
719 size_t size)
721 struct tevent_req *req, *subreq;
722 struct cli_read_state *state;
724 req = tevent_req_create(mem_ctx, &state, struct cli_read_state);
725 if (req == NULL) {
726 return NULL;
728 state->cli = cli;
729 state->buf = buf;
730 state->buflen = size;
732 if (smbXcli_conn_protocol(state->cli->conn) >= PROTOCOL_SMB2_02) {
733 uint32_t max_size;
734 bool ok;
736 ok = smb2cli_conn_req_possible(state->cli->conn, &max_size);
737 if (!ok) {
738 tevent_req_nterror(
739 req,
740 NT_STATUS_INSUFFICIENT_RESOURCES);
741 return tevent_req_post(req, ev);
745 * downgrade depending on the available credits
747 size = MIN(max_size, size);
749 subreq = cli_smb2_read_send(
750 state, ev, cli, fnum, offset, size);
751 if (tevent_req_nomem(subreq, req)) {
752 return tevent_req_post(req, ev);
754 } else {
755 bool ok;
756 ok = smb1cli_conn_req_possible(state->cli->conn);
757 if (!ok) {
758 tevent_req_nterror(
759 req,
760 NT_STATUS_INSUFFICIENT_RESOURCES);
761 return tevent_req_post(req, ev);
764 subreq = cli_read_andx_send(
765 state, ev, cli, fnum, offset, size);
766 if (tevent_req_nomem(subreq, req)) {
767 return tevent_req_post(req, ev);
771 tevent_req_set_callback(subreq, cli_read_done, req);
773 return req;
776 static void cli_read_done(struct tevent_req *subreq)
778 struct tevent_req *req = tevent_req_callback_data(
779 subreq, struct tevent_req);
780 struct cli_read_state *state = tevent_req_data(
781 req, struct cli_read_state);
782 NTSTATUS status;
783 ssize_t received;
784 uint8_t *buf;
786 if (smbXcli_conn_protocol(state->cli->conn) >= PROTOCOL_SMB2_02) {
787 status = cli_smb2_read_recv(subreq, &received, &buf);
788 } else {
789 status = cli_read_andx_recv(subreq, &received, &buf);
792 if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
793 received = 0;
794 status = NT_STATUS_OK;
796 if (tevent_req_nterror(req, status)) {
797 return;
799 if ((received < 0) || (received > state->buflen)) {
800 state->received = 0;
801 tevent_req_nterror(req, NT_STATUS_UNEXPECTED_IO_ERROR);
802 return;
805 memcpy(state->buf, buf, received);
806 state->received = received;
807 tevent_req_done(req);
810 NTSTATUS cli_read_recv(struct tevent_req *req, size_t *received)
812 struct cli_read_state *state = tevent_req_data(
813 req, struct cli_read_state);
814 NTSTATUS status;
816 if (tevent_req_is_nterror(req, &status)) {
817 return status;
819 if (received != NULL) {
820 *received = state->received;
822 return NT_STATUS_OK;
825 static NTSTATUS cli_read_sink(char *buf, size_t n, void *priv)
827 char **pbuf = (char **)priv;
828 memcpy(*pbuf, buf, n);
829 *pbuf += n;
830 return NT_STATUS_OK;
833 NTSTATUS cli_read(struct cli_state *cli, uint16_t fnum,
834 char *buf, off_t offset, size_t size,
835 size_t *nread)
837 NTSTATUS status;
838 off_t ret;
840 status = cli_pull(cli, fnum, offset, size, size,
841 cli_read_sink, &buf, &ret);
842 if (!NT_STATUS_IS_OK(status)) {
843 return status;
846 if (nread) {
847 *nread = ret;
850 return NT_STATUS_OK;
853 /****************************************************************************
854 write to a file using a SMBwrite and not bypassing 0 byte writes
855 ****************************************************************************/
857 NTSTATUS cli_smbwrite(struct cli_state *cli, uint16_t fnum, char *buf,
858 off_t offset, size_t size1, size_t *ptotal)
860 uint8_t *bytes;
861 ssize_t total = 0;
864 * 3 bytes prefix
867 bytes = talloc_array(talloc_tos(), uint8_t, 3);
868 if (bytes == NULL) {
869 return NT_STATUS_NO_MEMORY;
871 bytes[0] = 1;
873 do {
874 uint32_t usable_space = cli_state_available_size(cli, 48);
875 size_t size = MIN(size1, usable_space);
876 struct tevent_req *req;
877 uint16_t vwv[5];
878 uint16_t *ret_vwv;
879 NTSTATUS status;
881 SSVAL(vwv+0, 0, fnum);
882 SSVAL(vwv+1, 0, size);
883 SIVAL(vwv+2, 0, offset);
884 SSVAL(vwv+4, 0, 0);
886 bytes = talloc_realloc(talloc_tos(), bytes, uint8_t,
887 size+3);
888 if (bytes == NULL) {
889 return NT_STATUS_NO_MEMORY;
891 SSVAL(bytes, 1, size);
892 memcpy(bytes + 3, buf + total, size);
894 status = cli_smb(talloc_tos(), cli, SMBwrite, 0, 5, vwv,
895 size+3, bytes, &req, 1, NULL, &ret_vwv,
896 NULL, NULL);
897 if (!NT_STATUS_IS_OK(status)) {
898 TALLOC_FREE(bytes);
899 return status;
902 size = SVAL(ret_vwv+0, 0);
903 TALLOC_FREE(req);
904 if (size == 0) {
905 break;
907 size1 -= size;
908 total += size;
909 offset += size;
911 } while (size1);
913 TALLOC_FREE(bytes);
915 if (ptotal != NULL) {
916 *ptotal = total;
918 return NT_STATUS_OK;
922 * Send a write&x request
925 struct cli_write_andx_state {
926 size_t size;
927 uint16_t vwv[14];
928 size_t written;
929 uint8_t pad;
930 struct iovec iov[2];
933 static void cli_write_andx_done(struct tevent_req *subreq);
935 struct tevent_req *cli_write_andx_create(TALLOC_CTX *mem_ctx,
936 struct tevent_context *ev,
937 struct cli_state *cli, uint16_t fnum,
938 uint16_t mode, const uint8_t *buf,
939 off_t offset, size_t size,
940 struct tevent_req **reqs_before,
941 int num_reqs_before,
942 struct tevent_req **psmbreq)
944 struct tevent_req *req, *subreq;
945 struct cli_write_andx_state *state;
946 bool bigoffset = ((smb1cli_conn_capabilities(cli->conn) & CAP_LARGE_FILES) != 0);
947 uint8_t wct = bigoffset ? 14 : 12;
948 size_t max_write = cli_write_max_bufsize(cli, mode, wct);
949 uint16_t *vwv;
951 req = tevent_req_create(mem_ctx, &state, struct cli_write_andx_state);
952 if (req == NULL) {
953 return NULL;
956 state->size = MIN(size, max_write);
958 vwv = state->vwv;
960 SCVAL(vwv+0, 0, 0xFF);
961 SCVAL(vwv+0, 1, 0);
962 SSVAL(vwv+1, 0, 0);
963 SSVAL(vwv+2, 0, fnum);
964 SIVAL(vwv+3, 0, offset);
965 SIVAL(vwv+5, 0, 0);
966 SSVAL(vwv+7, 0, mode);
967 SSVAL(vwv+8, 0, 0);
968 SSVAL(vwv+9, 0, (state->size>>16));
969 SSVAL(vwv+10, 0, state->size);
971 SSVAL(vwv+11, 0,
972 smb1cli_req_wct_ofs(reqs_before, num_reqs_before)
973 + 1 /* the wct field */
974 + wct * 2 /* vwv */
975 + 2 /* num_bytes field */
976 + 1 /* pad */);
978 if (bigoffset) {
979 SIVAL(vwv+12, 0, (((uint64_t)offset)>>32) & 0xffffffff);
982 state->pad = 0;
983 state->iov[0].iov_base = (void *)&state->pad;
984 state->iov[0].iov_len = 1;
985 state->iov[1].iov_base = discard_const_p(void, buf);
986 state->iov[1].iov_len = state->size;
988 subreq = cli_smb_req_create(state, ev, cli, SMBwriteX, 0, 0, wct, vwv,
989 2, state->iov);
990 if (tevent_req_nomem(subreq, req)) {
991 return tevent_req_post(req, ev);
993 tevent_req_set_callback(subreq, cli_write_andx_done, req);
994 *psmbreq = subreq;
995 return req;
998 struct tevent_req *cli_write_andx_send(TALLOC_CTX *mem_ctx,
999 struct tevent_context *ev,
1000 struct cli_state *cli, uint16_t fnum,
1001 uint16_t mode, const uint8_t *buf,
1002 off_t offset, size_t size)
1004 struct tevent_req *req, *subreq;
1005 NTSTATUS status;
1007 req = cli_write_andx_create(mem_ctx, ev, cli, fnum, mode, buf, offset,
1008 size, NULL, 0, &subreq);
1009 if (req == NULL) {
1010 return NULL;
1013 status = smb1cli_req_chain_submit(&subreq, 1);
1014 if (tevent_req_nterror(req, status)) {
1015 return tevent_req_post(req, ev);
1017 return req;
1020 static void cli_write_andx_done(struct tevent_req *subreq)
1022 struct tevent_req *req = tevent_req_callback_data(
1023 subreq, struct tevent_req);
1024 struct cli_write_andx_state *state = tevent_req_data(
1025 req, struct cli_write_andx_state);
1026 uint8_t wct;
1027 uint16_t *vwv;
1028 NTSTATUS status;
1030 status = cli_smb_recv(subreq, state, NULL, 6, &wct, &vwv,
1031 NULL, NULL);
1032 TALLOC_FREE(subreq);
1033 if (NT_STATUS_IS_ERR(status)) {
1034 tevent_req_nterror(req, status);
1035 return;
1037 state->written = SVAL(vwv+2, 0);
1038 if (state->size > UINT16_MAX) {
1040 * It is important that we only set the
1041 * high bits only if we asked for a large write.
1043 * OS/2 print shares get this wrong and may send
1044 * invalid values.
1046 * See bug #5326.
1048 state->written |= SVAL(vwv+4, 0)<<16;
1050 tevent_req_done(req);
1053 NTSTATUS cli_write_andx_recv(struct tevent_req *req, size_t *pwritten)
1055 struct cli_write_andx_state *state = tevent_req_data(
1056 req, struct cli_write_andx_state);
1057 NTSTATUS status;
1059 if (tevent_req_is_nterror(req, &status)) {
1060 return status;
1062 if (pwritten != 0) {
1063 *pwritten = state->written;
1065 return NT_STATUS_OK;
1068 struct cli_write_state {
1069 struct cli_state *cli;
1070 size_t written;
1073 static void cli_write_done(struct tevent_req *subreq);
1075 struct tevent_req *cli_write_send(TALLOC_CTX *mem_ctx,
1076 struct tevent_context *ev,
1077 struct cli_state *cli, uint16_t fnum,
1078 uint16_t mode, const uint8_t *buf,
1079 off_t offset, size_t size)
1081 struct tevent_req *req = NULL;
1082 struct cli_write_state *state = NULL;
1083 struct tevent_req *subreq = NULL;
1085 req = tevent_req_create(mem_ctx, &state, struct cli_write_state);
1086 if (req == NULL) {
1087 return NULL;
1089 state->cli = cli;
1091 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1092 uint32_t max_size;
1093 bool ok;
1095 ok = smb2cli_conn_req_possible(state->cli->conn, &max_size);
1096 if (!ok) {
1097 tevent_req_nterror(
1098 req,
1099 NT_STATUS_INSUFFICIENT_RESOURCES);
1100 return tevent_req_post(req, ev);
1104 * downgrade depending on the available credits
1106 size = MIN(max_size, size);
1108 subreq = cli_smb2_write_send(state,
1110 cli,
1111 fnum,
1112 mode,
1113 buf,
1114 offset,
1115 size);
1116 } else {
1117 bool ok;
1119 ok = smb1cli_conn_req_possible(state->cli->conn);
1120 if (!ok) {
1121 tevent_req_nterror(
1122 req,
1123 NT_STATUS_INSUFFICIENT_RESOURCES);
1124 return tevent_req_post(req, ev);
1127 subreq = cli_write_andx_send(state,
1129 cli,
1130 fnum,
1131 mode,
1132 buf,
1133 offset,
1134 size);
1136 if (tevent_req_nomem(subreq, req)) {
1137 return tevent_req_post(req, ev);
1139 tevent_req_set_callback(subreq, cli_write_done, req);
1141 return req;
1144 static void cli_write_done(struct tevent_req *subreq)
1146 struct tevent_req *req =
1147 tevent_req_callback_data(subreq,
1148 struct tevent_req);
1149 struct cli_write_state *state =
1150 tevent_req_data(req,
1151 struct cli_write_state);
1152 NTSTATUS status;
1154 if (smbXcli_conn_protocol(state->cli->conn) >= PROTOCOL_SMB2_02) {
1155 status = cli_smb2_write_recv(subreq, &state->written);
1156 } else {
1157 status = cli_write_andx_recv(subreq, &state->written);
1159 TALLOC_FREE(subreq);
1160 if (tevent_req_nterror(req, status)) {
1161 return;
1163 tevent_req_done(req);
1166 NTSTATUS cli_write_recv(struct tevent_req *req, size_t *pwritten)
1168 struct cli_write_state *state =
1169 tevent_req_data(req,
1170 struct cli_write_state);
1171 NTSTATUS status;
1173 if (tevent_req_is_nterror(req, &status)) {
1174 tevent_req_received(req);
1175 return status;
1177 if (pwritten != NULL) {
1178 *pwritten = state->written;
1180 tevent_req_received(req);
1181 return NT_STATUS_OK;
1184 struct cli_smb1_writeall_state {
1185 struct tevent_context *ev;
1186 struct cli_state *cli;
1187 uint16_t fnum;
1188 uint16_t mode;
1189 const uint8_t *buf;
1190 off_t offset;
1191 size_t size;
1192 size_t written;
1195 static void cli_smb1_writeall_written(struct tevent_req *req);
1197 static struct tevent_req *cli_smb1_writeall_send(TALLOC_CTX *mem_ctx,
1198 struct tevent_context *ev,
1199 struct cli_state *cli,
1200 uint16_t fnum,
1201 uint16_t mode,
1202 const uint8_t *buf,
1203 off_t offset, size_t size)
1205 struct tevent_req *req, *subreq;
1206 struct cli_smb1_writeall_state *state;
1208 req = tevent_req_create(mem_ctx, &state,
1209 struct cli_smb1_writeall_state);
1210 if (req == NULL) {
1211 return NULL;
1213 state->ev = ev;
1214 state->cli = cli;
1215 state->fnum = fnum;
1216 state->mode = mode;
1217 state->buf = buf;
1218 state->offset = offset;
1219 state->size = size;
1220 state->written = 0;
1222 subreq = cli_write_andx_send(state, state->ev, state->cli, state->fnum,
1223 state->mode, state->buf, state->offset,
1224 state->size);
1225 if (tevent_req_nomem(subreq, req)) {
1226 return tevent_req_post(req, ev);
1228 tevent_req_set_callback(subreq, cli_smb1_writeall_written, req);
1229 return req;
1232 static void cli_smb1_writeall_written(struct tevent_req *subreq)
1234 struct tevent_req *req = tevent_req_callback_data(
1235 subreq, struct tevent_req);
1236 struct cli_smb1_writeall_state *state = tevent_req_data(
1237 req, struct cli_smb1_writeall_state);
1238 NTSTATUS status;
1239 size_t written, to_write;
1241 status = cli_write_andx_recv(subreq, &written);
1242 TALLOC_FREE(subreq);
1243 if (tevent_req_nterror(req, status)) {
1244 return;
1247 state->written += written;
1249 if (state->written > state->size) {
1250 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
1251 return;
1254 to_write = state->size - state->written;
1256 if (to_write == 0) {
1257 tevent_req_done(req);
1258 return;
1261 subreq = cli_write_andx_send(state, state->ev, state->cli, state->fnum,
1262 state->mode,
1263 state->buf + state->written,
1264 state->offset + state->written, to_write);
1265 if (tevent_req_nomem(subreq, req)) {
1266 return;
1268 tevent_req_set_callback(subreq, cli_smb1_writeall_written, req);
1271 static NTSTATUS cli_smb1_writeall_recv(struct tevent_req *req,
1272 size_t *pwritten)
1274 struct cli_smb1_writeall_state *state = tevent_req_data(
1275 req, struct cli_smb1_writeall_state);
1276 NTSTATUS status;
1278 if (tevent_req_is_nterror(req, &status)) {
1279 return status;
1281 if (pwritten != NULL) {
1282 *pwritten = state->written;
1284 return NT_STATUS_OK;
1287 struct cli_writeall_state {
1288 struct cli_state *cli;
1289 size_t written;
1292 static void cli_writeall_done(struct tevent_req *subreq);
1294 struct tevent_req *cli_writeall_send(
1295 TALLOC_CTX *mem_ctx,
1296 struct tevent_context *ev,
1297 struct cli_state *cli,
1298 uint16_t fnum,
1299 uint16_t mode,
1300 const uint8_t *buf,
1301 off_t offset,
1302 size_t size)
1304 struct tevent_req *req, *subreq;
1305 struct cli_writeall_state *state;
1307 req = tevent_req_create(mem_ctx, &state, struct cli_writeall_state);
1308 if (req == NULL) {
1309 return NULL;
1311 state->cli = cli;
1313 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1314 subreq = cli_smb2_writeall_send(
1315 state,
1317 cli,
1318 fnum,
1319 mode,
1320 buf,
1321 offset,
1322 size);
1323 } else {
1324 subreq = cli_smb1_writeall_send(
1325 state,
1327 cli,
1328 fnum,
1329 mode,
1330 buf,
1331 offset,
1332 size);
1335 if (tevent_req_nomem(subreq, req)) {
1336 return tevent_req_post(req, ev);
1338 tevent_req_set_callback(subreq, cli_writeall_done, req);
1340 return req;
1343 static void cli_writeall_done(struct tevent_req *subreq)
1345 struct tevent_req *req = tevent_req_callback_data(
1346 subreq, struct tevent_req);
1347 struct cli_writeall_state *state = tevent_req_data(
1348 req, struct cli_writeall_state);
1349 NTSTATUS status;
1351 if (smbXcli_conn_protocol(state->cli->conn) >= PROTOCOL_SMB2_02) {
1352 status = cli_smb2_writeall_recv(subreq, &state->written);
1353 } else {
1354 status = cli_smb1_writeall_recv(subreq, &state->written);
1356 TALLOC_FREE(subreq);
1357 if (tevent_req_nterror(req, status)) {
1358 return;
1360 tevent_req_done(req);
1363 NTSTATUS cli_writeall_recv(struct tevent_req *req, size_t *pwritten)
1365 struct cli_writeall_state *state = tevent_req_data(
1366 req, struct cli_writeall_state);
1367 NTSTATUS status;
1369 if (tevent_req_is_nterror(req, &status)) {
1370 return status;
1372 if (pwritten != NULL) {
1373 *pwritten = state->written;
1375 return NT_STATUS_OK;
1379 NTSTATUS cli_writeall(struct cli_state *cli, uint16_t fnum, uint16_t mode,
1380 const uint8_t *buf, off_t offset, size_t size,
1381 size_t *pwritten)
1383 TALLOC_CTX *frame = talloc_stackframe();
1384 struct tevent_context *ev;
1385 struct tevent_req *req;
1386 NTSTATUS status = NT_STATUS_NO_MEMORY;
1388 if (smbXcli_conn_has_async_calls(cli->conn)) {
1390 * Can't use sync call while an async call is in flight
1392 status = NT_STATUS_INVALID_PARAMETER;
1393 goto fail;
1395 ev = samba_tevent_context_init(frame);
1396 if (ev == NULL) {
1397 goto fail;
1399 req = cli_writeall_send(frame, ev, cli, fnum, mode, buf, offset, size);
1400 if (req == NULL) {
1401 goto fail;
1403 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1404 goto fail;
1406 status = cli_writeall_recv(req, pwritten);
1407 fail:
1408 TALLOC_FREE(frame);
1409 return status;
1412 struct cli_push_chunk;
1414 struct cli_push_state {
1415 struct tevent_context *ev;
1416 struct cli_state *cli;
1417 uint16_t fnum;
1418 uint16_t mode;
1419 off_t start_offset;
1421 size_t (*source)(uint8_t *buf, size_t n, void *priv);
1422 void *priv;
1424 bool eof;
1426 size_t chunk_size;
1427 off_t next_offset;
1430 * Outstanding requests
1432 * The maximum is 256:
1433 * - which would be a window of 256 MByte
1434 * for SMB2 with multi-credit
1435 * or smb1 unix extensions.
1437 uint16_t max_chunks;
1438 uint16_t num_chunks;
1439 uint16_t num_waiting;
1440 struct cli_push_chunk *chunks;
1443 struct cli_push_chunk {
1444 struct cli_push_chunk *prev, *next;
1445 struct tevent_req *req;/* This is the main request! Not the subreq */
1446 struct tevent_req *subreq;
1447 off_t ofs;
1448 uint8_t *buf;
1449 size_t total_size;
1450 size_t tmp_size;
1451 bool done;
1454 static void cli_push_setup_chunks(struct tevent_req *req);
1455 static void cli_push_chunk_ship(struct cli_push_chunk *chunk);
1456 static void cli_push_chunk_done(struct tevent_req *subreq);
1458 struct tevent_req *cli_push_send(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
1459 struct cli_state *cli,
1460 uint16_t fnum, uint16_t mode,
1461 off_t start_offset, size_t window_size,
1462 size_t (*source)(uint8_t *buf, size_t n,
1463 void *priv),
1464 void *priv)
1466 struct tevent_req *req;
1467 struct cli_push_state *state;
1468 size_t page_size = 1024;
1469 uint64_t tmp64;
1471 req = tevent_req_create(mem_ctx, &state, struct cli_push_state);
1472 if (req == NULL) {
1473 return NULL;
1475 state->cli = cli;
1476 state->ev = ev;
1477 state->fnum = fnum;
1478 state->start_offset = start_offset;
1479 state->mode = mode;
1480 state->source = source;
1481 state->priv = priv;
1482 state->next_offset = start_offset;
1484 if (smbXcli_conn_protocol(state->cli->conn) >= PROTOCOL_SMB2_02) {
1485 state->chunk_size = smb2cli_conn_max_write_size(cli->conn);
1486 } else {
1487 state->chunk_size = cli_write_max_bufsize(cli, mode, 14);
1489 if (state->chunk_size > page_size) {
1490 state->chunk_size &= ~(page_size - 1);
1493 if (window_size == 0) {
1495 * We use 16 MByte as default window size.
1497 window_size = 16 * 1024 * 1024;
1500 tmp64 = window_size/state->chunk_size;
1501 if ((window_size % state->chunk_size) > 0) {
1502 tmp64 += 1;
1504 tmp64 = MAX(tmp64, 1);
1505 tmp64 = MIN(tmp64, 256);
1506 state->max_chunks = tmp64;
1509 * We defer the callback because of the complex
1510 * substate/subfunction logic
1512 tevent_req_defer_callback(req, ev);
1514 cli_push_setup_chunks(req);
1515 if (!tevent_req_is_in_progress(req)) {
1516 return tevent_req_post(req, ev);
1519 return req;
1522 static void cli_push_setup_chunks(struct tevent_req *req)
1524 struct cli_push_state *state =
1525 tevent_req_data(req,
1526 struct cli_push_state);
1527 struct cli_push_chunk *chunk, *next = NULL;
1528 size_t i;
1530 for (chunk = state->chunks; chunk; chunk = next) {
1532 * Note that chunk might be removed from this call.
1534 next = chunk->next;
1535 cli_push_chunk_ship(chunk);
1536 if (!tevent_req_is_in_progress(req)) {
1537 return;
1541 for (i = state->num_chunks; i < state->max_chunks; i++) {
1543 if (state->num_waiting > 0) {
1544 return;
1547 if (state->eof) {
1548 break;
1551 chunk = talloc_zero(state, struct cli_push_chunk);
1552 if (tevent_req_nomem(chunk, req)) {
1553 return;
1555 chunk->req = req;
1556 chunk->ofs = state->next_offset;
1557 chunk->buf = talloc_array(chunk,
1558 uint8_t,
1559 state->chunk_size);
1560 if (tevent_req_nomem(chunk->buf, req)) {
1561 return;
1563 chunk->total_size = state->source(chunk->buf,
1564 state->chunk_size,
1565 state->priv);
1566 if (chunk->total_size == 0) {
1567 /* nothing to send */
1568 talloc_free(chunk);
1569 state->eof = true;
1570 break;
1572 state->next_offset += chunk->total_size;
1574 DLIST_ADD_END(state->chunks, chunk);
1575 state->num_chunks++;
1576 state->num_waiting++;
1578 cli_push_chunk_ship(chunk);
1579 if (!tevent_req_is_in_progress(req)) {
1580 return;
1584 if (!state->eof) {
1585 return;
1588 if (state->num_chunks > 0) {
1589 return;
1592 tevent_req_done(req);
1595 static void cli_push_chunk_ship(struct cli_push_chunk *chunk)
1597 struct tevent_req *req = chunk->req;
1598 struct cli_push_state *state =
1599 tevent_req_data(req,
1600 struct cli_push_state);
1601 bool ok;
1602 const uint8_t *buf;
1603 off_t ofs;
1604 size_t size;
1606 if (chunk->done) {
1607 DLIST_REMOVE(state->chunks, chunk);
1608 SMB_ASSERT(state->num_chunks > 0);
1609 state->num_chunks--;
1610 TALLOC_FREE(chunk);
1612 return;
1615 if (chunk->subreq != NULL) {
1616 return;
1619 SMB_ASSERT(state->num_waiting > 0);
1621 buf = chunk->buf + chunk->tmp_size;
1622 ofs = chunk->ofs + chunk->tmp_size;
1623 size = chunk->total_size - chunk->tmp_size;
1625 if (smbXcli_conn_protocol(state->cli->conn) >= PROTOCOL_SMB2_02) {
1626 uint32_t max_size;
1628 ok = smb2cli_conn_req_possible(state->cli->conn, &max_size);
1629 if (!ok) {
1630 return;
1634 * downgrade depending on the available credits
1636 size = MIN(max_size, size);
1638 chunk->subreq = cli_smb2_write_send(chunk,
1639 state->ev,
1640 state->cli,
1641 state->fnum,
1642 state->mode,
1643 buf,
1644 ofs,
1645 size);
1646 if (tevent_req_nomem(chunk->subreq, req)) {
1647 return;
1649 } else {
1650 ok = smb1cli_conn_req_possible(state->cli->conn);
1651 if (!ok) {
1652 return;
1655 chunk->subreq = cli_write_andx_send(chunk,
1656 state->ev,
1657 state->cli,
1658 state->fnum,
1659 state->mode,
1660 buf,
1661 ofs,
1662 size);
1663 if (tevent_req_nomem(chunk->subreq, req)) {
1664 return;
1667 tevent_req_set_callback(chunk->subreq,
1668 cli_push_chunk_done,
1669 chunk);
1671 state->num_waiting--;
1672 return;
1675 static void cli_push_chunk_done(struct tevent_req *subreq)
1677 struct cli_push_chunk *chunk =
1678 tevent_req_callback_data(subreq,
1679 struct cli_push_chunk);
1680 struct tevent_req *req = chunk->req;
1681 struct cli_push_state *state =
1682 tevent_req_data(req,
1683 struct cli_push_state);
1684 NTSTATUS status;
1685 size_t expected = chunk->total_size - chunk->tmp_size;
1686 size_t written;
1688 chunk->subreq = NULL;
1690 if (smbXcli_conn_protocol(state->cli->conn) >= PROTOCOL_SMB2_02) {
1691 status = cli_smb2_write_recv(subreq, &written);
1692 } else {
1693 status = cli_write_andx_recv(subreq, &written);
1695 TALLOC_FREE(subreq);
1696 if (tevent_req_nterror(req, status)) {
1697 return;
1700 if (written > expected) {
1701 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
1702 return;
1705 if (written == 0) {
1706 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
1707 return;
1710 chunk->tmp_size += written;
1712 if (chunk->tmp_size == chunk->total_size) {
1713 chunk->done = true;
1714 } else {
1715 state->num_waiting++;
1718 cli_push_setup_chunks(req);
1721 NTSTATUS cli_push_recv(struct tevent_req *req)
1723 return tevent_req_simple_recv_ntstatus(req);
1726 NTSTATUS cli_push(struct cli_state *cli, uint16_t fnum, uint16_t mode,
1727 off_t start_offset, size_t window_size,
1728 size_t (*source)(uint8_t *buf, size_t n, void *priv),
1729 void *priv)
1731 TALLOC_CTX *frame = talloc_stackframe();
1732 struct tevent_context *ev;
1733 struct tevent_req *req;
1734 NTSTATUS status = NT_STATUS_OK;
1736 if (smbXcli_conn_has_async_calls(cli->conn)) {
1738 * Can't use sync call while an async call is in flight
1740 status = NT_STATUS_INVALID_PARAMETER;
1741 goto fail;
1744 ev = samba_tevent_context_init(frame);
1745 if (ev == NULL) {
1746 status = NT_STATUS_NO_MEMORY;
1747 goto fail;
1750 req = cli_push_send(frame, ev, cli, fnum, mode, start_offset,
1751 window_size, source, priv);
1752 if (req == NULL) {
1753 status = NT_STATUS_NO_MEMORY;
1754 goto fail;
1757 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1758 goto fail;
1761 status = cli_push_recv(req);
1762 fail:
1763 TALLOC_FREE(frame);
1764 return status;
1767 #define SPLICE_BLOCK_SIZE 1024 * 1024
1769 static NTSTATUS cli_splice_fallback(TALLOC_CTX *frame,
1770 struct cli_state *srccli,
1771 struct cli_state *dstcli,
1772 uint16_t src_fnum, uint16_t dst_fnum,
1773 off_t initial_size,
1774 off_t src_offset, off_t dst_offset,
1775 off_t *written,
1776 int (*splice_cb)(off_t n, void *priv),
1777 void *priv)
1779 NTSTATUS status;
1780 uint8_t *buf = talloc_size(frame, SPLICE_BLOCK_SIZE);
1781 size_t nread;
1782 off_t remaining = initial_size;
1783 *written = 0;
1785 while (remaining) {
1786 size_t to_read = MIN(remaining, SPLICE_BLOCK_SIZE);
1788 status = cli_read(srccli, src_fnum,
1789 (char *)buf, src_offset, to_read,
1790 &nread);
1791 if (!NT_STATUS_IS_OK(status)) {
1792 return status;
1795 status = cli_writeall(dstcli, dst_fnum, 0,
1796 buf, dst_offset, nread, NULL);
1797 if (!NT_STATUS_IS_OK(status)) {
1798 return status;
1801 if ((src_offset > INT64_MAX - nread) ||
1802 (dst_offset > INT64_MAX - nread)) {
1803 return NT_STATUS_FILE_TOO_LARGE;
1805 src_offset += nread;
1806 dst_offset += nread;
1807 *written += nread;
1808 if (remaining < nread) {
1809 return NT_STATUS_INTERNAL_ERROR;
1811 remaining -= nread;
1812 if (!splice_cb(initial_size - remaining, priv)) {
1813 return NT_STATUS_CANCELLED;
1817 return NT_STATUS_OK;
1820 NTSTATUS cli_splice(struct cli_state *srccli, struct cli_state *dstcli,
1821 uint16_t src_fnum, uint16_t dst_fnum,
1822 off_t size,
1823 off_t src_offset, off_t dst_offset,
1824 off_t *written,
1825 int (*splice_cb)(off_t n, void *priv), void *priv)
1827 TALLOC_CTX *frame = talloc_stackframe();
1828 struct tevent_context *ev;
1829 struct tevent_req *req;
1830 NTSTATUS status = NT_STATUS_NO_MEMORY;
1831 bool retry_fallback = false;
1833 if (smbXcli_conn_has_async_calls(srccli->conn) ||
1834 smbXcli_conn_has_async_calls(dstcli->conn))
1837 * Can't use sync call while an async call is in flight
1839 status = NT_STATUS_INVALID_PARAMETER;
1840 goto out;
1843 do {
1844 ev = samba_tevent_context_init(frame);
1845 if (ev == NULL) {
1846 goto out;
1848 if (srccli == dstcli &&
1849 smbXcli_conn_protocol(srccli->conn) >= PROTOCOL_SMB2_02 &&
1850 !retry_fallback)
1852 req = cli_smb2_splice_send(frame, ev,
1853 srccli, src_fnum, dst_fnum,
1854 size, src_offset, dst_offset,
1855 splice_cb, priv);
1856 } else {
1857 status = cli_splice_fallback(frame,
1858 srccli, dstcli,
1859 src_fnum, dst_fnum,
1860 size,
1861 src_offset, dst_offset,
1862 written,
1863 splice_cb, priv);
1864 goto out;
1866 if (req == NULL) {
1867 goto out;
1869 if (!tevent_req_poll(req, ev)) {
1870 status = map_nt_error_from_unix(errno);
1871 goto out;
1873 status = cli_smb2_splice_recv(req, written);
1876 * Older versions of Samba don't support
1877 * FSCTL_SRV_COPYCHUNK_WRITE so use the fallback.
1879 retry_fallback = NT_STATUS_EQUAL(status, NT_STATUS_INVALID_DEVICE_REQUEST);
1880 } while (retry_fallback);
1882 out:
1883 TALLOC_FREE(frame);
1884 return status;