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"
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
)
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
)) {
51 if (smb1cli_conn_encryption_on(cli
->conn
)) {
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
);
70 /****************************************************************************
71 Calculate the recommended write buffer size
72 ****************************************************************************/
73 static size_t cli_write_max_bufsize(struct cli_state
*cli
,
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
;
96 if (write_mode
!= 0) {
100 if (smb1cli_conn_signing_is_active(cli
->conn
)) {
104 if (smb1cli_conn_encryption_on(cli
->conn
)) {
108 if (strequal(cli
->dev
, "LPT1:")) {
112 return useable_space
;
115 struct cli_read_andx_state
{
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
;
135 req
= tevent_req_create(mem_ctx
, &state
, struct cli_read_andx_state
);
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);
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
) {
171 tevent_req_set_callback(subreq
, cli_read_andx_done
, 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
;
184 req
= cli_read_andx_create(mem_ctx
, ev
, cli
, fnum
, offset
, size
,
190 status
= smb1cli_req_chain_submit(&subreq
, 1);
191 if (tevent_req_nterror(req
, status
)) {
192 return tevent_req_post(req
, ev
);
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
);
209 state
->status
= cli_smb_recv(subreq
, state
, &inbuf
, 12, &wct
, &vwv
,
212 if (NT_STATUS_IS_ERR(state
->status
)) {
213 tevent_req_nterror(req
, state
->status
);
217 /* size is the number of bytes the server returned.
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
);
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
);
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
);
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
257 NTSTATUS
cli_read_andx_recv(struct tevent_req
*req
, ssize_t
*received
,
260 struct cli_read_andx_state
*state
= tevent_req_data(
261 req
, struct cli_read_andx_state
);
264 if (tevent_req_is_nterror(req
, &status
)) {
267 *received
= state
->received
;
268 *rcvbuf
= state
->buf
;
272 struct cli_pull_chunk
;
274 struct cli_pull_state
{
275 struct tevent_context
*ev
;
276 struct cli_state
*cli
;
281 NTSTATUS (*sink
)(char *buf
, size_t n
, void *priv
);
289 * How many bytes did we push into "sink"?
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.
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
;
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
,
339 struct tevent_req
*req
;
340 struct cli_pull_state
*state
;
341 size_t page_size
= 1024;
344 req
= tevent_req_create(mem_ctx
, &state
, struct cli_pull_state
);
351 state
->start_offset
= start_offset
;
355 state
->next_offset
= start_offset
;
356 state
->remaining
= size
;
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
);
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) {
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
);
401 static void cli_pull_setup_chunks(struct tevent_req
*req
)
403 struct cli_pull_state
*state
=
405 struct cli_pull_state
);
406 struct cli_pull_chunk
*chunk
, *next
= NULL
;
409 for (chunk
= state
->chunks
; chunk
; chunk
= next
) {
411 * Note that chunk might be removed from this call.
414 cli_pull_chunk_ship(chunk
);
415 if (!tevent_req_is_in_progress(req
)) {
420 for (i
= state
->num_chunks
; i
< state
->max_chunks
; i
++) {
422 if (state
->num_waiting
> 0) {
426 if (state
->remaining
== 0) {
430 chunk
= talloc_zero(state
, struct cli_pull_chunk
);
431 if (tevent_req_nomem(chunk
, 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
);
442 state
->num_waiting
++;
444 cli_pull_chunk_ship(chunk
);
445 if (!tevent_req_is_in_progress(req
)) {
450 if (state
->remaining
> 0) {
454 if (state
->num_chunks
> 0) {
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
=
466 struct cli_pull_state
);
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.
485 if (chunk
->tmp_size
== 0) {
487 * we got a short read, we're done
489 tevent_req_done(req
);
493 status
= state
->sink((char *)chunk
->buf
,
496 if (tevent_req_nterror(req
, status
)) {
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
);
509 DLIST_REMOVE(state
->chunks
, chunk
);
510 SMB_ASSERT(state
->num_chunks
> 0);
517 if (chunk
->subreq
!= NULL
) {
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
) {
529 ok
= smb2cli_conn_req_possible(state
->cli
->conn
, &max_size
);
535 * downgrade depending on the available credits
537 size
= MIN(max_size
, size
);
539 chunk
->subreq
= cli_smb2_read_send(chunk
,
545 if (tevent_req_nomem(chunk
->subreq
, req
)) {
549 ok
= smb1cli_conn_req_possible(state
->cli
->conn
);
554 chunk
->subreq
= cli_read_andx_send(chunk
,
560 if (tevent_req_nomem(chunk
->subreq
, req
)) {
564 tevent_req_set_callback(chunk
->subreq
,
568 state
->num_waiting
--;
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
=
580 struct cli_pull_state
);
582 size_t expected
= chunk
->total_size
- chunk
->tmp_size
;
583 ssize_t received
= 0;
586 chunk
->subreq
= NULL
;
588 if (smbXcli_conn_protocol(state
->cli
->conn
) >= PROTOCOL_SMB2_02
) {
589 status
= cli_smb2_read_recv(subreq
, &received
, &buf
);
591 status
= cli_read_andx_recv(subreq
, &received
, &buf
);
593 if (NT_STATUS_EQUAL(status
, NT_STATUS_END_OF_FILE
)) {
595 status
= NT_STATUS_OK
;
597 if (tevent_req_nterror(req
, status
)) {
601 if (received
> expected
) {
602 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
608 * We got EOF we're done
611 cli_pull_setup_chunks(req
);
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.
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
)) {
630 if (received
!= chunk
->total_size
) {
631 uint8_t *p
= chunk
->buf
+ chunk
->tmp_size
;
632 memcpy(p
, buf
, received
);
636 chunk
->tmp_size
+= received
;
638 if (chunk
->tmp_size
== chunk
->total_size
) {
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
);
653 if (tevent_req_is_nterror(req
, &status
)) {
654 tevent_req_received(req
);
657 *received
= state
->pushed
;
658 tevent_req_received(req
);
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
;
680 ev
= samba_tevent_context_init(frame
);
682 status
= NT_STATUS_NO_MEMORY
;
686 req
= cli_pull_send(frame
, ev
, cli
, fnum
, start_offset
, size
,
687 window_size
, sink
, priv
);
689 status
= NT_STATUS_NO_MEMORY
;
693 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
697 status
= cli_pull_recv(req
, received
);
703 struct cli_read_state
{
704 struct cli_state
*cli
;
710 static void cli_read_done(struct tevent_req
*subreq
);
712 struct tevent_req
*cli_read_send(
714 struct tevent_context
*ev
,
715 struct cli_state
*cli
,
721 struct tevent_req
*req
, *subreq
;
722 struct cli_read_state
*state
;
724 req
= tevent_req_create(mem_ctx
, &state
, struct cli_read_state
);
730 state
->buflen
= size
;
732 if (smbXcli_conn_protocol(state
->cli
->conn
) >= PROTOCOL_SMB2_02
) {
736 ok
= smb2cli_conn_req_possible(state
->cli
->conn
, &max_size
);
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
);
756 ok
= smb1cli_conn_req_possible(state
->cli
->conn
);
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
);
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
);
786 if (smbXcli_conn_protocol(state
->cli
->conn
) >= PROTOCOL_SMB2_02
) {
787 status
= cli_smb2_read_recv(subreq
, &received
, &buf
);
789 status
= cli_read_andx_recv(subreq
, &received
, &buf
);
792 if (NT_STATUS_EQUAL(status
, NT_STATUS_END_OF_FILE
)) {
794 status
= NT_STATUS_OK
;
796 if (tevent_req_nterror(req
, status
)) {
799 if ((buf
== NULL
) || (received
< 0) || (received
> state
->buflen
)) {
801 tevent_req_nterror(req
, NT_STATUS_UNEXPECTED_IO_ERROR
);
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
);
816 if (tevent_req_is_nterror(req
, &status
)) {
819 if (received
!= NULL
) {
820 *received
= state
->received
;
826 * Helper function for cli_pull(). This takes a chunk of data (buf) read from
827 * a remote file and copies it into the return buffer (priv).
829 NTSTATUS
cli_read_sink(char *buf
, size_t n
, void *priv
)
831 char **pbuf
= (char **)priv
;
832 memcpy(*pbuf
, buf
, n
);
837 NTSTATUS
cli_read(struct cli_state
*cli
, uint16_t fnum
,
838 char *buf
, off_t offset
, size_t size
,
844 status
= cli_pull(cli
, fnum
, offset
, size
, size
,
845 cli_read_sink
, &buf
, &ret
);
846 if (!NT_STATUS_IS_OK(status
)) {
857 /****************************************************************************
858 write to a file using a SMBwrite and not bypassing 0 byte writes
859 ****************************************************************************/
861 NTSTATUS
cli_smbwrite(struct cli_state
*cli
, uint16_t fnum
, char *buf
,
862 off_t offset
, size_t size1
, size_t *ptotal
)
871 bytes
= talloc_array(talloc_tos(), uint8_t, 3);
873 return NT_STATUS_NO_MEMORY
;
878 uint32_t usable_space
= cli_state_available_size(cli
, 48);
879 size_t size
= MIN(size1
, usable_space
);
880 struct tevent_req
*req
;
885 SSVAL(vwv
+0, 0, fnum
);
886 SSVAL(vwv
+1, 0, size
);
887 SIVAL(vwv
+2, 0, offset
);
890 bytes
= talloc_realloc(talloc_tos(), bytes
, uint8_t,
893 return NT_STATUS_NO_MEMORY
;
895 SSVAL(bytes
, 1, size
);
896 memcpy(bytes
+ 3, buf
+ total
, size
);
898 status
= cli_smb(talloc_tos(), cli
, SMBwrite
, 0, 5, vwv
,
899 size
+3, bytes
, &req
, 1, NULL
, &ret_vwv
,
901 if (!NT_STATUS_IS_OK(status
)) {
906 size
= SVAL(ret_vwv
+0, 0);
919 if (ptotal
!= NULL
) {
926 * Send a write&x request
929 struct cli_write_andx_state
{
937 static void cli_write_andx_done(struct tevent_req
*subreq
);
939 struct tevent_req
*cli_write_andx_create(TALLOC_CTX
*mem_ctx
,
940 struct tevent_context
*ev
,
941 struct cli_state
*cli
, uint16_t fnum
,
942 uint16_t mode
, const uint8_t *buf
,
943 off_t offset
, size_t size
,
944 struct tevent_req
**reqs_before
,
946 struct tevent_req
**psmbreq
)
948 struct tevent_req
*req
, *subreq
;
949 struct cli_write_andx_state
*state
;
950 bool bigoffset
= ((smb1cli_conn_capabilities(cli
->conn
) & CAP_LARGE_FILES
) != 0);
951 uint8_t wct
= bigoffset
? 14 : 12;
952 size_t max_write
= cli_write_max_bufsize(cli
, mode
, wct
);
955 req
= tevent_req_create(mem_ctx
, &state
, struct cli_write_andx_state
);
960 state
->size
= MIN(size
, max_write
);
964 SCVAL(vwv
+0, 0, 0xFF);
967 SSVAL(vwv
+2, 0, fnum
);
968 SIVAL(vwv
+3, 0, offset
);
970 SSVAL(vwv
+7, 0, mode
);
972 SSVAL(vwv
+9, 0, (state
->size
>>16));
973 SSVAL(vwv
+10, 0, state
->size
);
976 smb1cli_req_wct_ofs(reqs_before
, num_reqs_before
)
977 + 1 /* the wct field */
979 + 2 /* num_bytes field */
983 SIVAL(vwv
+12, 0, (((uint64_t)offset
)>>32) & 0xffffffff);
987 state
->iov
[0].iov_base
= (void *)&state
->pad
;
988 state
->iov
[0].iov_len
= 1;
989 state
->iov
[1].iov_base
= discard_const_p(void, buf
);
990 state
->iov
[1].iov_len
= state
->size
;
992 subreq
= cli_smb_req_create(state
, ev
, cli
, SMBwriteX
, 0, 0, wct
, vwv
,
994 if (tevent_req_nomem(subreq
, req
)) {
995 return tevent_req_post(req
, ev
);
997 tevent_req_set_callback(subreq
, cli_write_andx_done
, req
);
1002 struct tevent_req
*cli_write_andx_send(TALLOC_CTX
*mem_ctx
,
1003 struct tevent_context
*ev
,
1004 struct cli_state
*cli
, uint16_t fnum
,
1005 uint16_t mode
, const uint8_t *buf
,
1006 off_t offset
, size_t size
)
1008 struct tevent_req
*req
, *subreq
;
1011 req
= cli_write_andx_create(mem_ctx
, ev
, cli
, fnum
, mode
, buf
, offset
,
1012 size
, NULL
, 0, &subreq
);
1017 status
= smb1cli_req_chain_submit(&subreq
, 1);
1018 if (tevent_req_nterror(req
, status
)) {
1019 return tevent_req_post(req
, ev
);
1024 static void cli_write_andx_done(struct tevent_req
*subreq
)
1026 struct tevent_req
*req
= tevent_req_callback_data(
1027 subreq
, struct tevent_req
);
1028 struct cli_write_andx_state
*state
= tevent_req_data(
1029 req
, struct cli_write_andx_state
);
1034 status
= cli_smb_recv(subreq
, state
, NULL
, 6, &wct
, &vwv
,
1036 TALLOC_FREE(subreq
);
1037 if (NT_STATUS_IS_ERR(status
)) {
1038 tevent_req_nterror(req
, status
);
1041 state
->written
= SVAL(vwv
+2, 0);
1042 if (state
->size
> UINT16_MAX
) {
1044 * It is important that we only set the
1045 * high bits only if we asked for a large write.
1047 * OS/2 print shares get this wrong and may send
1052 state
->written
|= SVAL(vwv
+4, 0)<<16;
1054 tevent_req_done(req
);
1057 NTSTATUS
cli_write_andx_recv(struct tevent_req
*req
, size_t *pwritten
)
1059 struct cli_write_andx_state
*state
= tevent_req_data(
1060 req
, struct cli_write_andx_state
);
1063 if (tevent_req_is_nterror(req
, &status
)) {
1066 if (pwritten
!= 0) {
1067 *pwritten
= state
->written
;
1069 return NT_STATUS_OK
;
1072 struct cli_write_state
{
1073 struct cli_state
*cli
;
1077 static void cli_write_done(struct tevent_req
*subreq
);
1080 * Used to write to a file remotely.
1081 * This is similar in functionality to cli_push_send(), except this is a more
1082 * finer-grain API. For example, if the data we want to write exceeds the max
1083 * write size of the underlying connection, then it's the caller's
1084 * responsibility to handle this.
1085 * For writing a small amount of data to file, this is a simpler API to use.
1087 struct tevent_req
*cli_write_send(TALLOC_CTX
*mem_ctx
,
1088 struct tevent_context
*ev
,
1089 struct cli_state
*cli
, uint16_t fnum
,
1090 uint16_t mode
, const uint8_t *buf
,
1091 off_t offset
, size_t size
)
1093 struct tevent_req
*req
= NULL
;
1094 struct cli_write_state
*state
= NULL
;
1095 struct tevent_req
*subreq
= NULL
;
1097 req
= tevent_req_create(mem_ctx
, &state
, struct cli_write_state
);
1103 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
1107 ok
= smb2cli_conn_req_possible(state
->cli
->conn
, &max_size
);
1111 NT_STATUS_INSUFFICIENT_RESOURCES
);
1112 return tevent_req_post(req
, ev
);
1116 * downgrade depending on the available credits
1118 size
= MIN(max_size
, size
);
1120 subreq
= cli_smb2_write_send(state
,
1131 ok
= smb1cli_conn_req_possible(state
->cli
->conn
);
1135 NT_STATUS_INSUFFICIENT_RESOURCES
);
1136 return tevent_req_post(req
, ev
);
1139 subreq
= cli_write_andx_send(state
,
1148 if (tevent_req_nomem(subreq
, req
)) {
1149 return tevent_req_post(req
, ev
);
1151 tevent_req_set_callback(subreq
, cli_write_done
, req
);
1156 static void cli_write_done(struct tevent_req
*subreq
)
1158 struct tevent_req
*req
=
1159 tevent_req_callback_data(subreq
,
1161 struct cli_write_state
*state
=
1162 tevent_req_data(req
,
1163 struct cli_write_state
);
1166 if (smbXcli_conn_protocol(state
->cli
->conn
) >= PROTOCOL_SMB2_02
) {
1167 status
= cli_smb2_write_recv(subreq
, &state
->written
);
1169 status
= cli_write_andx_recv(subreq
, &state
->written
);
1171 TALLOC_FREE(subreq
);
1172 if (tevent_req_nterror(req
, status
)) {
1175 tevent_req_done(req
);
1178 NTSTATUS
cli_write_recv(struct tevent_req
*req
, size_t *pwritten
)
1180 struct cli_write_state
*state
=
1181 tevent_req_data(req
,
1182 struct cli_write_state
);
1185 if (tevent_req_is_nterror(req
, &status
)) {
1186 tevent_req_received(req
);
1189 if (pwritten
!= NULL
) {
1190 *pwritten
= state
->written
;
1192 tevent_req_received(req
);
1193 return NT_STATUS_OK
;
1196 struct cli_smb1_writeall_state
{
1197 struct tevent_context
*ev
;
1198 struct cli_state
*cli
;
1207 static void cli_smb1_writeall_written(struct tevent_req
*req
);
1209 static struct tevent_req
*cli_smb1_writeall_send(TALLOC_CTX
*mem_ctx
,
1210 struct tevent_context
*ev
,
1211 struct cli_state
*cli
,
1215 off_t offset
, size_t size
)
1217 struct tevent_req
*req
, *subreq
;
1218 struct cli_smb1_writeall_state
*state
;
1220 req
= tevent_req_create(mem_ctx
, &state
,
1221 struct cli_smb1_writeall_state
);
1230 state
->offset
= offset
;
1234 subreq
= cli_write_andx_send(state
, state
->ev
, state
->cli
, state
->fnum
,
1235 state
->mode
, state
->buf
, state
->offset
,
1237 if (tevent_req_nomem(subreq
, req
)) {
1238 return tevent_req_post(req
, ev
);
1240 tevent_req_set_callback(subreq
, cli_smb1_writeall_written
, req
);
1244 static void cli_smb1_writeall_written(struct tevent_req
*subreq
)
1246 struct tevent_req
*req
= tevent_req_callback_data(
1247 subreq
, struct tevent_req
);
1248 struct cli_smb1_writeall_state
*state
= tevent_req_data(
1249 req
, struct cli_smb1_writeall_state
);
1251 size_t written
= 0, to_write
;
1253 status
= cli_write_andx_recv(subreq
, &written
);
1254 TALLOC_FREE(subreq
);
1255 if (tevent_req_nterror(req
, status
)) {
1259 state
->written
+= written
;
1261 if (state
->written
> state
->size
) {
1262 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
1266 to_write
= state
->size
- state
->written
;
1268 if (to_write
== 0) {
1269 tevent_req_done(req
);
1273 subreq
= cli_write_andx_send(state
, state
->ev
, state
->cli
, state
->fnum
,
1275 state
->buf
+ state
->written
,
1276 state
->offset
+ state
->written
, to_write
);
1277 if (tevent_req_nomem(subreq
, req
)) {
1280 tevent_req_set_callback(subreq
, cli_smb1_writeall_written
, req
);
1283 static NTSTATUS
cli_smb1_writeall_recv(struct tevent_req
*req
,
1286 struct cli_smb1_writeall_state
*state
= tevent_req_data(
1287 req
, struct cli_smb1_writeall_state
);
1290 if (tevent_req_is_nterror(req
, &status
)) {
1293 if (pwritten
!= NULL
) {
1294 *pwritten
= state
->written
;
1296 return NT_STATUS_OK
;
1299 struct cli_writeall_state
{
1300 struct cli_state
*cli
;
1304 static void cli_writeall_done(struct tevent_req
*subreq
);
1306 struct tevent_req
*cli_writeall_send(
1307 TALLOC_CTX
*mem_ctx
,
1308 struct tevent_context
*ev
,
1309 struct cli_state
*cli
,
1316 struct tevent_req
*req
, *subreq
;
1317 struct cli_writeall_state
*state
;
1319 req
= tevent_req_create(mem_ctx
, &state
, struct cli_writeall_state
);
1325 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
1326 subreq
= cli_smb2_writeall_send(
1336 subreq
= cli_smb1_writeall_send(
1347 if (tevent_req_nomem(subreq
, req
)) {
1348 return tevent_req_post(req
, ev
);
1350 tevent_req_set_callback(subreq
, cli_writeall_done
, req
);
1355 static void cli_writeall_done(struct tevent_req
*subreq
)
1357 struct tevent_req
*req
= tevent_req_callback_data(
1358 subreq
, struct tevent_req
);
1359 struct cli_writeall_state
*state
= tevent_req_data(
1360 req
, struct cli_writeall_state
);
1363 if (smbXcli_conn_protocol(state
->cli
->conn
) >= PROTOCOL_SMB2_02
) {
1364 status
= cli_smb2_writeall_recv(subreq
, &state
->written
);
1366 status
= cli_smb1_writeall_recv(subreq
, &state
->written
);
1368 TALLOC_FREE(subreq
);
1369 if (tevent_req_nterror(req
, status
)) {
1372 tevent_req_done(req
);
1375 NTSTATUS
cli_writeall_recv(struct tevent_req
*req
, size_t *pwritten
)
1377 struct cli_writeall_state
*state
= tevent_req_data(
1378 req
, struct cli_writeall_state
);
1381 if (tevent_req_is_nterror(req
, &status
)) {
1384 if (pwritten
!= NULL
) {
1385 *pwritten
= state
->written
;
1387 return NT_STATUS_OK
;
1391 NTSTATUS
cli_writeall(struct cli_state
*cli
, uint16_t fnum
, uint16_t mode
,
1392 const uint8_t *buf
, off_t offset
, size_t size
,
1395 TALLOC_CTX
*frame
= talloc_stackframe();
1396 struct tevent_context
*ev
;
1397 struct tevent_req
*req
;
1398 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
1400 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
1402 * Can't use sync call while an async call is in flight
1404 status
= NT_STATUS_INVALID_PARAMETER
;
1407 ev
= samba_tevent_context_init(frame
);
1411 req
= cli_writeall_send(frame
, ev
, cli
, fnum
, mode
, buf
, offset
, size
);
1415 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
1418 status
= cli_writeall_recv(req
, pwritten
);
1424 struct cli_push_chunk
;
1426 struct cli_push_state
{
1427 struct tevent_context
*ev
;
1428 struct cli_state
*cli
;
1433 size_t (*source
)(uint8_t *buf
, size_t n
, void *priv
);
1442 * Outstanding requests
1444 * The maximum is 256:
1445 * - which would be a window of 256 MByte
1446 * for SMB2 with multi-credit
1447 * or smb1 unix extensions.
1449 uint16_t max_chunks
;
1450 uint16_t num_chunks
;
1451 uint16_t num_waiting
;
1452 struct cli_push_chunk
*chunks
;
1455 struct cli_push_chunk
{
1456 struct cli_push_chunk
*prev
, *next
;
1457 struct tevent_req
*req
;/* This is the main request! Not the subreq */
1458 struct tevent_req
*subreq
;
1466 static void cli_push_setup_chunks(struct tevent_req
*req
);
1467 static void cli_push_chunk_ship(struct cli_push_chunk
*chunk
);
1468 static void cli_push_chunk_done(struct tevent_req
*subreq
);
1471 * Used to write to a file remotely.
1472 * This is similar in functionality to cli_write_send(), except this API
1473 * handles writing a large file by breaking the data into chunks (so we don't
1474 * exceed the max write size of the underlying connection). To do this, the
1475 * (*source) callback handles copying the underlying file data into a message
1476 * buffer, one chunk at a time.
1477 * This API is recommended when writing a potentially large amount of data,
1478 * e.g. when copying a file (or doing a 'put').
1480 struct tevent_req
*cli_push_send(TALLOC_CTX
*mem_ctx
, struct tevent_context
*ev
,
1481 struct cli_state
*cli
,
1482 uint16_t fnum
, uint16_t mode
,
1483 off_t start_offset
, size_t window_size
,
1484 size_t (*source
)(uint8_t *buf
, size_t n
,
1488 struct tevent_req
*req
;
1489 struct cli_push_state
*state
;
1490 size_t page_size
= 1024;
1493 req
= tevent_req_create(mem_ctx
, &state
, struct cli_push_state
);
1500 state
->start_offset
= start_offset
;
1502 state
->source
= source
;
1504 state
->next_offset
= start_offset
;
1506 if (smbXcli_conn_protocol(state
->cli
->conn
) >= PROTOCOL_SMB2_02
) {
1507 state
->chunk_size
= smb2cli_conn_max_write_size(cli
->conn
);
1509 state
->chunk_size
= cli_write_max_bufsize(cli
, mode
, 14);
1511 if (state
->chunk_size
> page_size
) {
1512 state
->chunk_size
&= ~(page_size
- 1);
1515 if (window_size
== 0) {
1517 * We use 16 MByte as default window size.
1519 window_size
= 16 * 1024 * 1024;
1522 tmp64
= window_size
/state
->chunk_size
;
1523 if ((window_size
% state
->chunk_size
) > 0) {
1526 tmp64
= MAX(tmp64
, 1);
1527 tmp64
= MIN(tmp64
, 256);
1528 state
->max_chunks
= tmp64
;
1531 * We defer the callback because of the complex
1532 * substate/subfunction logic
1534 tevent_req_defer_callback(req
, ev
);
1536 cli_push_setup_chunks(req
);
1537 if (!tevent_req_is_in_progress(req
)) {
1538 return tevent_req_post(req
, ev
);
1544 static void cli_push_setup_chunks(struct tevent_req
*req
)
1546 struct cli_push_state
*state
=
1547 tevent_req_data(req
,
1548 struct cli_push_state
);
1549 struct cli_push_chunk
*chunk
, *next
= NULL
;
1552 for (chunk
= state
->chunks
; chunk
; chunk
= next
) {
1554 * Note that chunk might be removed from this call.
1557 cli_push_chunk_ship(chunk
);
1558 if (!tevent_req_is_in_progress(req
)) {
1563 for (i
= state
->num_chunks
; i
< state
->max_chunks
; i
++) {
1565 if (state
->num_waiting
> 0) {
1573 chunk
= talloc_zero(state
, struct cli_push_chunk
);
1574 if (tevent_req_nomem(chunk
, req
)) {
1578 chunk
->ofs
= state
->next_offset
;
1579 chunk
->buf
= talloc_array(chunk
,
1582 if (tevent_req_nomem(chunk
->buf
, req
)) {
1585 chunk
->total_size
= state
->source(chunk
->buf
,
1588 if (chunk
->total_size
== 0) {
1589 /* nothing to send */
1594 state
->next_offset
+= chunk
->total_size
;
1596 DLIST_ADD_END(state
->chunks
, chunk
);
1597 state
->num_chunks
++;
1598 state
->num_waiting
++;
1600 cli_push_chunk_ship(chunk
);
1601 if (!tevent_req_is_in_progress(req
)) {
1610 if (state
->num_chunks
> 0) {
1614 tevent_req_done(req
);
1617 static void cli_push_chunk_ship(struct cli_push_chunk
*chunk
)
1619 struct tevent_req
*req
= chunk
->req
;
1620 struct cli_push_state
*state
=
1621 tevent_req_data(req
,
1622 struct cli_push_state
);
1629 DLIST_REMOVE(state
->chunks
, chunk
);
1630 SMB_ASSERT(state
->num_chunks
> 0);
1631 state
->num_chunks
--;
1637 if (chunk
->subreq
!= NULL
) {
1641 SMB_ASSERT(state
->num_waiting
> 0);
1643 buf
= chunk
->buf
+ chunk
->tmp_size
;
1644 ofs
= chunk
->ofs
+ chunk
->tmp_size
;
1645 size
= chunk
->total_size
- chunk
->tmp_size
;
1647 if (smbXcli_conn_protocol(state
->cli
->conn
) >= PROTOCOL_SMB2_02
) {
1650 ok
= smb2cli_conn_req_possible(state
->cli
->conn
, &max_size
);
1656 * downgrade depending on the available credits
1658 size
= MIN(max_size
, size
);
1660 chunk
->subreq
= cli_smb2_write_send(chunk
,
1668 if (tevent_req_nomem(chunk
->subreq
, req
)) {
1672 ok
= smb1cli_conn_req_possible(state
->cli
->conn
);
1677 chunk
->subreq
= cli_write_andx_send(chunk
,
1685 if (tevent_req_nomem(chunk
->subreq
, req
)) {
1689 tevent_req_set_callback(chunk
->subreq
,
1690 cli_push_chunk_done
,
1693 state
->num_waiting
--;
1697 static void cli_push_chunk_done(struct tevent_req
*subreq
)
1699 struct cli_push_chunk
*chunk
=
1700 tevent_req_callback_data(subreq
,
1701 struct cli_push_chunk
);
1702 struct tevent_req
*req
= chunk
->req
;
1703 struct cli_push_state
*state
=
1704 tevent_req_data(req
,
1705 struct cli_push_state
);
1707 size_t expected
= chunk
->total_size
- chunk
->tmp_size
;
1710 chunk
->subreq
= NULL
;
1712 if (smbXcli_conn_protocol(state
->cli
->conn
) >= PROTOCOL_SMB2_02
) {
1713 status
= cli_smb2_write_recv(subreq
, &written
);
1715 status
= cli_write_andx_recv(subreq
, &written
);
1717 TALLOC_FREE(subreq
);
1718 if (tevent_req_nterror(req
, status
)) {
1722 if (written
> expected
) {
1723 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
1728 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
1732 chunk
->tmp_size
+= written
;
1734 if (chunk
->tmp_size
== chunk
->total_size
) {
1737 state
->num_waiting
++;
1740 cli_push_setup_chunks(req
);
1743 NTSTATUS
cli_push_recv(struct tevent_req
*req
)
1745 return tevent_req_simple_recv_ntstatus(req
);
1748 NTSTATUS
cli_push(struct cli_state
*cli
, uint16_t fnum
, uint16_t mode
,
1749 off_t start_offset
, size_t window_size
,
1750 size_t (*source
)(uint8_t *buf
, size_t n
, void *priv
),
1753 TALLOC_CTX
*frame
= talloc_stackframe();
1754 struct tevent_context
*ev
;
1755 struct tevent_req
*req
;
1756 NTSTATUS status
= NT_STATUS_OK
;
1758 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
1760 * Can't use sync call while an async call is in flight
1762 status
= NT_STATUS_INVALID_PARAMETER
;
1766 ev
= samba_tevent_context_init(frame
);
1768 status
= NT_STATUS_NO_MEMORY
;
1772 req
= cli_push_send(frame
, ev
, cli
, fnum
, mode
, start_offset
,
1773 window_size
, source
, priv
);
1775 status
= NT_STATUS_NO_MEMORY
;
1779 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
1783 status
= cli_push_recv(req
);
1789 #define SPLICE_BLOCK_SIZE 1024 * 1024
1791 static NTSTATUS
cli_splice_fallback(TALLOC_CTX
*frame
,
1792 struct cli_state
*srccli
,
1793 struct cli_state
*dstcli
,
1794 uint16_t src_fnum
, uint16_t dst_fnum
,
1796 off_t src_offset
, off_t dst_offset
,
1798 int (*splice_cb
)(off_t n
, void *priv
),
1802 uint8_t *buf
= talloc_size(frame
, SPLICE_BLOCK_SIZE
);
1804 off_t remaining
= initial_size
;
1808 size_t to_read
= MIN(remaining
, SPLICE_BLOCK_SIZE
);
1810 status
= cli_read(srccli
, src_fnum
,
1811 (char *)buf
, src_offset
, to_read
,
1813 if (!NT_STATUS_IS_OK(status
)) {
1817 status
= cli_writeall(dstcli
, dst_fnum
, 0,
1818 buf
, dst_offset
, nread
, NULL
);
1819 if (!NT_STATUS_IS_OK(status
)) {
1823 if ((src_offset
> INT64_MAX
- nread
) ||
1824 (dst_offset
> INT64_MAX
- nread
)) {
1825 return NT_STATUS_FILE_TOO_LARGE
;
1827 src_offset
+= nread
;
1828 dst_offset
+= nread
;
1830 if (remaining
< nread
) {
1831 return NT_STATUS_INTERNAL_ERROR
;
1834 if (!splice_cb(initial_size
- remaining
, priv
)) {
1835 return NT_STATUS_CANCELLED
;
1839 return NT_STATUS_OK
;
1842 NTSTATUS
cli_splice(struct cli_state
*srccli
, struct cli_state
*dstcli
,
1843 uint16_t src_fnum
, uint16_t dst_fnum
,
1845 off_t src_offset
, off_t dst_offset
,
1847 int (*splice_cb
)(off_t n
, void *priv
), void *priv
)
1849 TALLOC_CTX
*frame
= talloc_stackframe();
1850 struct tevent_context
*ev
;
1851 struct tevent_req
*req
;
1852 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
1853 bool retry_fallback
= false;
1855 if (smbXcli_conn_has_async_calls(srccli
->conn
) ||
1856 smbXcli_conn_has_async_calls(dstcli
->conn
))
1859 * Can't use sync call while an async call is in flight
1861 status
= NT_STATUS_INVALID_PARAMETER
;
1866 ev
= samba_tevent_context_init(frame
);
1870 if (srccli
== dstcli
&&
1871 smbXcli_conn_protocol(srccli
->conn
) >= PROTOCOL_SMB2_02
&&
1874 req
= cli_smb2_splice_send(frame
, ev
,
1875 srccli
, src_fnum
, dst_fnum
,
1876 size
, src_offset
, dst_offset
,
1879 status
= cli_splice_fallback(frame
,
1883 src_offset
, dst_offset
,
1891 if (!tevent_req_poll(req
, ev
)) {
1892 status
= map_nt_error_from_unix(errno
);
1895 status
= cli_smb2_splice_recv(req
, written
);
1898 * Older versions of Samba don't support
1899 * FSCTL_SRV_COPYCHUNK_WRITE so use the fallback.
1901 retry_fallback
= NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_DEVICE_REQUEST
);
1902 } while (retry_fallback
);