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(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 SMB_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
, SMB_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
, SMB_OFF_T size
, size_t window_size
,
682 NTSTATUS (*sink
)(char *buf
, size_t n
, void *priv
),
683 void *priv
, SMB_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 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, (size
>>16));
866 SSVAL(vwv
+10, 0, 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
= 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 state
->written
|= SVAL(vwv
+4, 0)<<16;
937 tevent_req_done(req
);
940 NTSTATUS
cli_write_andx_recv(struct tevent_req
*req
, size_t *pwritten
)
942 struct cli_write_andx_state
*state
= tevent_req_data(
943 req
, struct cli_write_andx_state
);
946 if (tevent_req_is_nterror(req
, &status
)) {
950 *pwritten
= state
->written
;
955 struct cli_writeall_state
{
956 struct event_context
*ev
;
957 struct cli_state
*cli
;
966 static void cli_writeall_written(struct tevent_req
*req
);
968 static struct tevent_req
*cli_writeall_send(TALLOC_CTX
*mem_ctx
,
969 struct event_context
*ev
,
970 struct cli_state
*cli
,
974 off_t offset
, size_t size
)
976 struct tevent_req
*req
, *subreq
;
977 struct cli_writeall_state
*state
;
979 req
= tevent_req_create(mem_ctx
, &state
, struct cli_writeall_state
);
988 state
->offset
= offset
;
992 subreq
= cli_write_andx_send(state
, state
->ev
, state
->cli
, state
->fnum
,
993 state
->mode
, state
->buf
, state
->offset
,
995 if (tevent_req_nomem(subreq
, req
)) {
996 return tevent_req_post(req
, ev
);
998 tevent_req_set_callback(subreq
, cli_writeall_written
, req
);
1002 static void cli_writeall_written(struct tevent_req
*subreq
)
1004 struct tevent_req
*req
= tevent_req_callback_data(
1005 subreq
, struct tevent_req
);
1006 struct cli_writeall_state
*state
= tevent_req_data(
1007 req
, struct cli_writeall_state
);
1009 size_t written
, to_write
;
1011 status
= cli_write_andx_recv(subreq
, &written
);
1012 TALLOC_FREE(subreq
);
1013 if (tevent_req_nterror(req
, status
)) {
1017 state
->written
+= written
;
1019 if (state
->written
> state
->size
) {
1020 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
1024 to_write
= state
->size
- state
->written
;
1026 if (to_write
== 0) {
1027 tevent_req_done(req
);
1031 subreq
= cli_write_andx_send(state
, state
->ev
, state
->cli
, state
->fnum
,
1033 state
->buf
+ state
->written
,
1034 state
->offset
+ state
->written
, to_write
);
1035 if (tevent_req_nomem(subreq
, req
)) {
1038 tevent_req_set_callback(subreq
, cli_writeall_written
, req
);
1041 static NTSTATUS
cli_writeall_recv(struct tevent_req
*req
,
1044 struct cli_writeall_state
*state
= tevent_req_data(
1045 req
, struct cli_writeall_state
);
1048 if (tevent_req_is_nterror(req
, &status
)) {
1051 if (pwritten
!= NULL
) {
1052 *pwritten
= state
->written
;
1054 return NT_STATUS_OK
;
1057 NTSTATUS
cli_writeall(struct cli_state
*cli
, uint16_t fnum
, uint16_t mode
,
1058 const uint8_t *buf
, off_t offset
, size_t size
,
1061 TALLOC_CTX
*frame
= talloc_stackframe();
1062 struct event_context
*ev
;
1063 struct tevent_req
*req
;
1064 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
1066 if (cli_has_async_calls(cli
)) {
1068 * Can't use sync call while an async call is in flight
1070 status
= NT_STATUS_INVALID_PARAMETER
;
1073 ev
= event_context_init(frame
);
1077 req
= cli_writeall_send(frame
, ev
, cli
, fnum
, mode
, buf
, offset
, size
);
1081 if (!tevent_req_poll(req
, ev
)) {
1082 status
= map_nt_error_from_unix(errno
);
1085 status
= cli_writeall_recv(req
, pwritten
);
1091 struct cli_push_write_state
{
1092 struct tevent_req
*req
;/* This is the main request! Not the subreq */
1099 struct cli_push_state
{
1100 struct event_context
*ev
;
1101 struct cli_state
*cli
;
1107 size_t (*source
)(uint8_t *buf
, size_t n
, void *priv
);
1116 * Outstanding requests
1121 struct cli_push_write_state
**reqs
;
1124 static void cli_push_written(struct tevent_req
*req
);
1126 static bool cli_push_write_setup(struct tevent_req
*req
,
1127 struct cli_push_state
*state
,
1130 struct cli_push_write_state
*substate
;
1131 struct tevent_req
*subreq
;
1133 substate
= talloc(state
->reqs
, struct cli_push_write_state
);
1137 substate
->req
= req
;
1138 substate
->idx
= idx
;
1139 substate
->ofs
= state
->next_offset
;
1140 substate
->buf
= talloc_array(substate
, uint8_t, state
->chunk_size
);
1141 if (!substate
->buf
) {
1142 talloc_free(substate
);
1145 substate
->size
= state
->source(substate
->buf
,
1148 if (substate
->size
== 0) {
1150 /* nothing to send */
1151 talloc_free(substate
);
1155 subreq
= cli_writeall_send(substate
,
1156 state
->ev
, state
->cli
,
1157 state
->fnum
, state
->mode
,
1162 talloc_free(substate
);
1165 tevent_req_set_callback(subreq
, cli_push_written
, substate
);
1167 state
->reqs
[idx
] = substate
;
1168 state
->pending
+= 1;
1169 state
->next_offset
+= substate
->size
;
1174 struct tevent_req
*cli_push_send(TALLOC_CTX
*mem_ctx
, struct event_context
*ev
,
1175 struct cli_state
*cli
,
1176 uint16_t fnum
, uint16_t mode
,
1177 off_t start_offset
, size_t window_size
,
1178 size_t (*source
)(uint8_t *buf
, size_t n
,
1182 struct tevent_req
*req
;
1183 struct cli_push_state
*state
;
1185 size_t page_size
= 1024;
1187 req
= tevent_req_create(mem_ctx
, &state
, struct cli_push_state
);
1194 state
->start_offset
= start_offset
;
1196 state
->source
= source
;
1200 state
->next_offset
= start_offset
;
1202 state
->chunk_size
= cli_write_max_bufsize(cli
, mode
, 14);
1203 if (state
->chunk_size
> page_size
) {
1204 state
->chunk_size
&= ~(page_size
- 1);
1207 state
->max_reqs
= cli_state_max_requests(cli
);
1209 if (window_size
== 0) {
1210 window_size
= state
->max_reqs
* state
->chunk_size
;
1212 state
->num_reqs
= window_size
/state
->chunk_size
;
1213 if ((window_size
% state
->chunk_size
) > 0) {
1214 state
->num_reqs
+= 1;
1216 state
->num_reqs
= MIN(state
->num_reqs
, state
->max_reqs
);
1217 state
->num_reqs
= MAX(state
->num_reqs
, 1);
1219 state
->reqs
= talloc_zero_array(state
, struct cli_push_write_state
*,
1221 if (state
->reqs
== NULL
) {
1225 for (i
=0; i
<state
->num_reqs
; i
++) {
1226 if (!cli_push_write_setup(req
, state
, i
)) {
1235 if (state
->pending
== 0) {
1236 tevent_req_done(req
);
1237 return tevent_req_post(req
, ev
);
1243 tevent_req_nterror(req
, NT_STATUS_NO_MEMORY
);
1244 return tevent_req_post(req
, ev
);
1247 static void cli_push_written(struct tevent_req
*subreq
)
1249 struct cli_push_write_state
*substate
= tevent_req_callback_data(
1250 subreq
, struct cli_push_write_state
);
1251 struct tevent_req
*req
= substate
->req
;
1252 struct cli_push_state
*state
= tevent_req_data(
1253 req
, struct cli_push_state
);
1255 uint32_t idx
= substate
->idx
;
1257 state
->reqs
[idx
] = NULL
;
1258 state
->pending
-= 1;
1260 status
= cli_writeall_recv(subreq
, NULL
);
1261 TALLOC_FREE(subreq
);
1262 TALLOC_FREE(substate
);
1263 if (tevent_req_nterror(req
, status
)) {
1268 if (!cli_push_write_setup(req
, state
, idx
)) {
1269 tevent_req_nterror(req
, NT_STATUS_NO_MEMORY
);
1274 if (state
->pending
== 0) {
1275 tevent_req_done(req
);
1280 NTSTATUS
cli_push_recv(struct tevent_req
*req
)
1282 return tevent_req_simple_recv_ntstatus(req
);
1285 NTSTATUS
cli_push(struct cli_state
*cli
, uint16_t fnum
, uint16_t mode
,
1286 off_t start_offset
, size_t window_size
,
1287 size_t (*source
)(uint8_t *buf
, size_t n
, void *priv
),
1290 TALLOC_CTX
*frame
= talloc_stackframe();
1291 struct event_context
*ev
;
1292 struct tevent_req
*req
;
1293 NTSTATUS status
= NT_STATUS_OK
;
1295 if (cli_has_async_calls(cli
)) {
1297 * Can't use sync call while an async call is in flight
1299 status
= NT_STATUS_INVALID_PARAMETER
;
1303 ev
= event_context_init(frame
);
1305 status
= NT_STATUS_NO_MEMORY
;
1309 req
= cli_push_send(frame
, ev
, cli
, fnum
, mode
, start_offset
,
1310 window_size
, source
, priv
);
1312 status
= NT_STATUS_NO_MEMORY
;
1316 if (!tevent_req_poll(req
, ev
)) {
1317 status
= map_nt_error_from_unix(errno
);
1321 status
= cli_push_recv(req
);