2 Unix SMB/CIFS implementation.
3 client file read/write routines
4 Copyright (C) Andrew Tridgell 1994-1998
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 /****************************************************************************
23 Calculate the recommended read buffer size
24 ****************************************************************************/
25 static size_t cli_read_max_bufsize(struct cli_state
*cli
)
27 if (!client_is_signing_on(cli
) && !cli_encryption_on(cli
)
28 && (cli
->posix_capabilities
& CIFS_UNIX_LARGE_READ_CAP
)) {
29 return CLI_SAMBA_MAX_POSIX_LARGE_READX_SIZE
;
31 if (cli
->capabilities
& CAP_LARGE_READX
) {
33 ? CLI_SAMBA_MAX_LARGE_READX_SIZE
34 : CLI_WINDOWS_MAX_LARGE_READX_SIZE
;
36 return (cli
->max_xmit
- (smb_size
+32)) & ~1023;
39 /****************************************************************************
40 Calculate the recommended write buffer size
41 ****************************************************************************/
42 static size_t cli_write_max_bufsize(struct cli_state
*cli
, uint16_t write_mode
)
44 if (write_mode
== 0 &&
45 !client_is_signing_on(cli
) &&
46 !cli_encryption_on(cli
) &&
47 (cli
->posix_capabilities
& CIFS_UNIX_LARGE_WRITE_CAP
) &&
48 (cli
->capabilities
& CAP_LARGE_FILES
)) {
49 /* Only do massive writes if we can do them direct
50 * with no signing or encrypting - not on a pipe. */
51 return CLI_SAMBA_MAX_POSIX_LARGE_WRITEX_SIZE
;
55 return CLI_SAMBA_MAX_LARGE_WRITEX_SIZE
;
58 if (((cli
->capabilities
& CAP_LARGE_WRITEX
) == 0)
59 || client_is_signing_on(cli
)
60 || strequal(cli
->dev
, "LPT1:")) {
63 * Printer devices are restricted to max_xmit writesize in
64 * Vista and XPSP3 as are signing connections.
67 return (cli
->max_xmit
- (smb_size
+32)) & ~1023;
70 return CLI_WINDOWS_MAX_LARGE_WRITEX_SIZE
;
75 * Send a read&x request
78 struct async_req
*cli_read_andx_send(TALLOC_CTX
*mem_ctx
,
79 struct event_context
*ev
,
80 struct cli_state
*cli
, int fnum
,
81 off_t offset
, size_t size
)
83 struct async_req
*result
;
84 struct cli_request
*req
;
85 bool bigoffset
= False
;
90 if (size
> cli_read_max_bufsize(cli
)) {
91 DEBUG(0, ("cli_read_andx_send got size=%d, can only handle "
92 "size=%d\n", (int)size
,
93 (int)cli_read_max_bufsize(cli
)));
97 SCVAL(vwv
+ 0, 0, 0xFF);
100 SSVAL(vwv
+ 2, 0, fnum
);
101 SIVAL(vwv
+ 3, 0, offset
);
102 SSVAL(vwv
+ 5, 0, size
);
103 SSVAL(vwv
+ 6, 0, size
);
104 SSVAL(vwv
+ 7, 0, (size
>> 16));
105 SSVAL(vwv
+ 8, 0, 0);
106 SSVAL(vwv
+ 9, 0, 0);
108 if ((uint64_t)offset
>> 32) {
111 (((uint64_t)offset
)>>32) & 0xffffffff);
115 result
= cli_request_send(mem_ctx
, ev
, cli
, SMBreadX
, 0, wct
, vwv
, 0,
117 if (result
== NULL
) {
121 req
= talloc_get_type_abort(result
->private_data
, struct cli_request
);
123 req
->data
.read
.ofs
= offset
;
124 req
->data
.read
.size
= size
;
125 req
->data
.read
.received
= 0;
126 req
->data
.read
.rcvbuf
= NULL
;
132 * Pull the data out of a finished async read_and_x request. rcvbuf is
133 * talloced from the request, so better make sure that you copy it away before
134 * you talloc_free(req). "rcvbuf" is NOT a talloc_ctx of its own, so do not
138 NTSTATUS
cli_read_andx_recv(struct async_req
*req
, ssize_t
*received
,
141 struct cli_request
*cli_req
= talloc_get_type_abort(
142 req
->private_data
, struct cli_request
);
151 if (async_req_is_error(req
, &status
)) {
155 status
= cli_pull_reply(req
, &wct
, &vwv
, &num_bytes
, &bytes
);
157 if (NT_STATUS_IS_ERR(status
)) {
162 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
165 /* size is the number of bytes the server returned.
167 size
= SVAL(vwv
+ 5, 0);
168 size
|= (((unsigned int)SVAL(vwv
+ 7, 0)) << 16);
170 if (size
> cli_req
->data
.read
.size
) {
171 DEBUG(5,("server returned more than we wanted!\n"));
172 return NT_STATUS_UNEXPECTED_IO_ERROR
;
176 * bcc field must be valid for small reads, for large reads the 16-bit
177 * bcc field can't be correct.
180 if ((size
< 0xffff) && (size
> num_bytes
)) {
181 DEBUG(5, ("server announced more bytes than sent\n"));
182 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
185 buf
= (uint8_t *)smb_base(cli_req
->inbuf
) + SVAL(vwv
+6, 0);
187 if (trans_oob(smb_len(cli_req
->inbuf
), SVAL(vwv
+6, 0), size
)
189 DEBUG(5, ("server returned invalid read&x data offset\n"));
190 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
193 *rcvbuf
= (uint8_t *)(smb_base(cli_req
->inbuf
) + SVAL(vwv
+ 6, 0));
199 * Parallel read support.
201 * cli_pull sends as many read&x requests as the server would allow via
202 * max_mux at a time. When replies flow back in, the data is written into
203 * the callback function "sink" in the right order.
206 struct cli_pull_state
{
207 struct async_req
*req
;
209 struct event_context
*ev
;
210 struct cli_state
*cli
;
215 NTSTATUS (*sink
)(char *buf
, size_t n
, void *priv
);
221 * Outstanding requests
224 struct async_req
**reqs
;
227 * For how many bytes did we send requests already?
232 * Next request index to push into "sink". This walks around the "req"
233 * array, taking care that the requests are pushed to "sink" in the
234 * right order. If necessary (i.e. replies don't come in in the right
235 * order), replies are held back in "reqs".
240 * How many bytes did we push into "sink"?
246 static char *cli_pull_print(TALLOC_CTX
*mem_ctx
, struct async_req
*req
)
248 struct cli_pull_state
*state
= talloc_get_type_abort(
249 req
->private_data
, struct cli_pull_state
);
252 result
= async_req_print(mem_ctx
, req
);
253 if (result
== NULL
) {
257 return talloc_asprintf_append_buffer(
258 result
, "num_reqs=%d, top_req=%d",
259 state
->num_reqs
, state
->top_req
);
262 static void cli_pull_read_done(struct async_req
*read_req
);
265 * Prepare an async pull request
268 struct async_req
*cli_pull_send(TALLOC_CTX
*mem_ctx
,
269 struct event_context
*ev
,
270 struct cli_state
*cli
,
271 uint16_t fnum
, off_t start_offset
,
272 SMB_OFF_T size
, size_t window_size
,
273 NTSTATUS (*sink
)(char *buf
, size_t n
,
277 struct async_req
*result
;
278 struct cli_pull_state
*state
;
281 result
= async_req_new(mem_ctx
);
282 if (result
== NULL
) {
285 state
= talloc(result
, struct cli_pull_state
);
289 result
->private_data
= state
;
290 result
->print
= cli_pull_print
;
296 state
->start_offset
= start_offset
;
305 if (!async_post_status(result
, ev
, NT_STATUS_OK
)) {
311 state
->chunk_size
= cli_read_max_bufsize(cli
);
313 state
->num_reqs
= MAX(window_size
/state
->chunk_size
, 1);
314 state
->num_reqs
= MIN(state
->num_reqs
, cli
->max_mux
);
316 state
->reqs
= TALLOC_ZERO_ARRAY(state
, struct async_req
*,
318 if (state
->reqs
== NULL
) {
322 state
->requested
= 0;
324 for (i
=0; i
<state
->num_reqs
; i
++) {
326 size_t request_thistime
;
328 if (state
->requested
>= size
) {
333 size_left
= size
- state
->requested
;
334 request_thistime
= MIN(size_left
, state
->chunk_size
);
336 state
->reqs
[i
] = cli_read_andx_send(
337 state
->reqs
, ev
, cli
, fnum
,
338 state
->start_offset
+ state
->requested
,
341 if (state
->reqs
[i
] == NULL
) {
345 state
->reqs
[i
]->async
.fn
= cli_pull_read_done
;
346 state
->reqs
[i
]->async
.priv
= result
;
348 state
->requested
+= request_thistime
;
358 * Handle incoming read replies, push the data into sink and send out new
359 * requests if necessary.
362 static void cli_pull_read_done(struct async_req
*read_req
)
364 struct async_req
*pull_req
= talloc_get_type_abort(
365 read_req
->async
.priv
, struct async_req
);
366 struct cli_pull_state
*state
= talloc_get_type_abort(
367 pull_req
->private_data
, struct cli_pull_state
);
368 struct cli_request
*read_state
= talloc_get_type_abort(
369 read_req
->private_data
, struct cli_request
);
372 status
= cli_read_andx_recv(read_req
, &read_state
->data
.read
.received
,
373 &read_state
->data
.read
.rcvbuf
);
374 if (!NT_STATUS_IS_OK(status
)) {
375 async_req_error(state
->req
, status
);
380 * This loop is the one to take care of out-of-order replies. All
381 * pending requests are in state->reqs, state->reqs[top_req] is the
382 * one that is to be pushed next. If however a request later than
383 * top_req is replied to, then we can't push yet. If top_req is
384 * replied to at a later point then, we need to push all the finished
388 while (state
->reqs
[state
->top_req
] != NULL
) {
389 struct cli_request
*top_read
;
391 DEBUG(11, ("cli_pull_read_done: top_req = %d\n",
394 if (state
->reqs
[state
->top_req
]->state
< ASYNC_REQ_DONE
) {
395 DEBUG(11, ("cli_pull_read_done: top request not yet "
400 top_read
= talloc_get_type_abort(
401 state
->reqs
[state
->top_req
]->private_data
,
404 DEBUG(10, ("cli_pull_read_done: Pushing %d bytes, %d already "
405 "pushed\n", (int)top_read
->data
.read
.received
,
406 (int)state
->pushed
));
408 status
= state
->sink((char *)top_read
->data
.read
.rcvbuf
,
409 top_read
->data
.read
.received
,
411 if (!NT_STATUS_IS_OK(status
)) {
412 async_req_error(state
->req
, status
);
415 state
->pushed
+= top_read
->data
.read
.received
;
417 TALLOC_FREE(state
->reqs
[state
->top_req
]);
419 if (state
->requested
< state
->size
) {
420 struct async_req
*new_req
;
422 size_t request_thistime
;
424 size_left
= state
->size
- state
->requested
;
425 request_thistime
= MIN(size_left
, state
->chunk_size
);
427 DEBUG(10, ("cli_pull_read_done: Requesting %d bytes "
428 "at %d, position %d\n",
429 (int)request_thistime
,
430 (int)(state
->start_offset
434 new_req
= cli_read_andx_send(
435 state
->reqs
, state
->ev
, state
->cli
,
437 state
->start_offset
+ state
->requested
,
440 if (async_req_nomem(new_req
, state
->req
)) {
444 new_req
->async
.fn
= cli_pull_read_done
;
445 new_req
->async
.priv
= pull_req
;
447 state
->reqs
[state
->top_req
] = new_req
;
448 state
->requested
+= request_thistime
;
451 state
->top_req
= (state
->top_req
+1) % state
->num_reqs
;
454 async_req_done(pull_req
);
457 NTSTATUS
cli_pull_recv(struct async_req
*req
, SMB_OFF_T
*received
)
459 struct cli_pull_state
*state
= talloc_get_type_abort(
460 req
->private_data
, struct cli_pull_state
);
463 if (async_req_is_error(req
, &status
)) {
466 *received
= state
->pushed
;
470 NTSTATUS
cli_pull(struct cli_state
*cli
, uint16_t fnum
,
471 off_t start_offset
, SMB_OFF_T size
, size_t window_size
,
472 NTSTATUS (*sink
)(char *buf
, size_t n
, void *priv
),
473 void *priv
, SMB_OFF_T
*received
)
475 TALLOC_CTX
*frame
= talloc_stackframe();
476 struct event_context
*ev
;
477 struct async_req
*req
;
478 NTSTATUS result
= NT_STATUS_NO_MEMORY
;
480 if (cli
->fd_event
!= NULL
) {
482 * Can't use sync call while an async call is in flight
484 return NT_STATUS_INVALID_PARAMETER
;
487 ev
= event_context_init(frame
);
492 req
= cli_pull_send(frame
, ev
, cli
, fnum
, start_offset
, size
,
493 window_size
, sink
, priv
);
498 while (req
->state
< ASYNC_REQ_DONE
) {
502 result
= cli_pull_recv(req
, received
);
508 static NTSTATUS
cli_read_sink(char *buf
, size_t n
, void *priv
)
510 char **pbuf
= (char **)priv
;
511 memcpy(*pbuf
, buf
, n
);
516 ssize_t
cli_read(struct cli_state
*cli
, int fnum
, char *buf
,
517 off_t offset
, size_t size
)
522 status
= cli_pull(cli
, fnum
, offset
, size
, size
,
523 cli_read_sink
, &buf
, &ret
);
524 if (!NT_STATUS_IS_OK(status
)) {
525 cli_set_error(cli
, status
);
531 /****************************************************************************
532 Issue a single SMBwrite and don't wait for a reply.
533 ****************************************************************************/
535 static bool cli_issue_write(struct cli_state
*cli
,
544 bool large_writex
= false;
545 /* We can only do direct writes if not signing and not encrypting. */
546 bool direct_writes
= !client_is_signing_on(cli
) && !cli_encryption_on(cli
);
548 if (!direct_writes
&& size
+ 1 > cli
->bufsize
) {
549 cli
->outbuf
= (char *)SMB_REALLOC(cli
->outbuf
, size
+ 1024);
553 cli
->inbuf
= (char *)SMB_REALLOC(cli
->inbuf
, size
+ 1024);
554 if (cli
->inbuf
== NULL
) {
555 SAFE_FREE(cli
->outbuf
);
558 cli
->bufsize
= size
+ 1024;
561 memset(cli
->outbuf
,'\0',smb_size
);
562 memset(cli
->inbuf
,'\0',smb_size
);
564 if (cli
->capabilities
& CAP_LARGE_FILES
) {
569 cli_set_message(cli
->outbuf
,14,0,True
);
571 cli_set_message(cli
->outbuf
,12,0,True
);
574 SCVAL(cli
->outbuf
,smb_com
,SMBwriteX
);
575 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
576 cli_setup_packet(cli
);
578 SCVAL(cli
->outbuf
,smb_vwv0
,0xFF);
579 SSVAL(cli
->outbuf
,smb_vwv2
,fnum
);
581 SIVAL(cli
->outbuf
,smb_vwv3
,offset
);
582 SIVAL(cli
->outbuf
,smb_vwv5
,0);
583 SSVAL(cli
->outbuf
,smb_vwv7
,mode
);
585 SSVAL(cli
->outbuf
,smb_vwv8
,(mode
& 0x0008) ? size
: 0);
587 * According to CIFS-TR-1p00, this following field should only
588 * be set if CAP_LARGE_WRITEX is set. We should check this
589 * locally. However, this check might already have been
590 * done by our callers.
592 SSVAL(cli
->outbuf
,smb_vwv9
,(size
>>16));
593 SSVAL(cli
->outbuf
,smb_vwv10
,size
);
594 /* +1 is pad byte. */
595 SSVAL(cli
->outbuf
,smb_vwv11
,
596 smb_buf(cli
->outbuf
) - smb_base(cli
->outbuf
) + 1);
599 SIVAL(cli
->outbuf
,smb_vwv12
,(((uint64_t)offset
)>>32) & 0xffffffff);
602 p
= smb_base(cli
->outbuf
) + SVAL(cli
->outbuf
,smb_vwv11
) -1;
603 *p
++ = '\0'; /* pad byte. */
604 if (!direct_writes
) {
605 memcpy(p
, buf
, size
);
607 if (size
> 0x1FFFF) {
608 /* This is a POSIX 14 word large write. */
609 set_message_bcc(cli
->outbuf
, 0); /* Set bcc to zero. */
610 _smb_setlen_large(cli
->outbuf
,smb_size
+ 28 + 1 /* pad */ + size
- 4);
612 cli_setup_bcc(cli
, p
+size
);
615 SSVAL(cli
->outbuf
,smb_mid
,cli
->mid
+ i
);
617 show_msg(cli
->outbuf
);
619 /* For direct writes we now need to write the data
620 * directly out of buf. */
621 return cli_send_smb_direct_writeX(cli
, buf
, size
);
623 return cli_send_smb(cli
);
627 /****************************************************************************
629 write_mode: 0x0001 disallow write cacheing
630 0x0002 return bytes remaining
631 0x0004 use raw named pipe protocol
632 0x0008 start of message mode named pipe protocol
633 ****************************************************************************/
635 ssize_t
cli_write(struct cli_state
*cli
,
636 int fnum
, uint16 write_mode
,
637 const char *buf
, off_t offset
, size_t size
)
639 ssize_t bwritten
= 0;
640 unsigned int issued
= 0;
641 unsigned int received
= 0;
646 if(cli
->max_mux
> 1) {
647 mpx
= cli
->max_mux
-1;
652 writesize
= cli_write_max_bufsize(cli
, write_mode
);
654 blocks
= (size
+ (writesize
-1)) / writesize
;
656 while (received
< blocks
) {
658 while ((issued
- received
< mpx
) && (issued
< blocks
)) {
659 ssize_t bsent
= issued
* writesize
;
660 ssize_t size1
= MIN(writesize
, size
- bsent
);
662 if (!cli_issue_write(cli
, fnum
, offset
+ bsent
,
670 if (!cli_receive_smb(cli
)) {
676 if (cli_is_error(cli
))
679 bwritten
+= SVAL(cli
->inbuf
, smb_vwv2
);
680 if (writesize
> 0xFFFF) {
681 bwritten
+= (((int)(SVAL(cli
->inbuf
, smb_vwv4
)))<<16);
685 while (received
< issued
&& cli_receive_smb(cli
)) {
692 /****************************************************************************
693 write to a file using a SMBwrite and not bypassing 0 byte writes
694 ****************************************************************************/
696 ssize_t
cli_smbwrite(struct cli_state
*cli
,
697 int fnum
, char *buf
, off_t offset
, size_t size1
)
703 size_t size
= MIN(size1
, cli
->max_xmit
- 48);
705 memset(cli
->outbuf
,'\0',smb_size
);
706 memset(cli
->inbuf
,'\0',smb_size
);
708 cli_set_message(cli
->outbuf
,5, 0,True
);
710 SCVAL(cli
->outbuf
,smb_com
,SMBwrite
);
711 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
712 cli_setup_packet(cli
);
714 SSVAL(cli
->outbuf
,smb_vwv0
,fnum
);
715 SSVAL(cli
->outbuf
,smb_vwv1
,size
);
716 SIVAL(cli
->outbuf
,smb_vwv2
,offset
);
717 SSVAL(cli
->outbuf
,smb_vwv4
,0);
719 p
= smb_buf(cli
->outbuf
);
721 SSVAL(p
, 0, size
); p
+= 2;
722 memcpy(p
, buf
+ total
, size
); p
+= size
;
724 cli_setup_bcc(cli
, p
);
726 if (!cli_send_smb(cli
))
729 if (!cli_receive_smb(cli
))
732 if (cli_is_error(cli
))
735 size
= SVAL(cli
->inbuf
,smb_vwv0
);
749 * Send a write&x request
752 struct async_req
*cli_write_andx_send(TALLOC_CTX
*mem_ctx
,
753 struct event_context
*ev
,
754 struct cli_state
*cli
, uint16_t fnum
,
755 uint16_t mode
, const uint8_t *buf
,
756 off_t offset
, size_t size
)
758 bool bigoffset
= ((cli
->capabilities
& CAP_LARGE_FILES
) != 0);
759 uint8_t wct
= bigoffset
? 14 : 12;
760 size_t max_write
= cli_write_max_bufsize(cli
, mode
);
763 size
= MIN(size
, max_write
);
765 SCVAL(vwv
+0, 0, 0xFF);
768 SSVAL(vwv
+2, 0, fnum
);
769 SIVAL(vwv
+3, 0, offset
);
771 SSVAL(vwv
+7, 0, mode
);
773 SSVAL(vwv
+9, 0, (size
>>16));
774 SSVAL(vwv
+10, 0, size
);
778 + 1 /* the wct field */
780 + 2 /* num_bytes field */
784 SIVAL(vwv
+12, 0, (((uint64_t)offset
)>>32) & 0xffffffff);
787 return cli_request_send(mem_ctx
, ev
, cli
, SMBwriteX
, 0, wct
, vwv
,
791 NTSTATUS
cli_write_andx_recv(struct async_req
*req
, size_t *pwritten
)
800 if (async_req_is_error(req
, &status
)) {
804 status
= cli_pull_reply(req
, &wct
, &vwv
, &num_bytes
, &bytes
);
806 if (NT_STATUS_IS_ERR(status
)) {
811 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
814 written
= SVAL(vwv
+2, 0);
815 written
|= SVAL(vwv
+4, 0)<<16;
821 struct cli_writeall_state
{
822 struct event_context
*ev
;
823 struct cli_state
*cli
;
832 static void cli_writeall_written(struct async_req
*req
);
834 static struct async_req
*cli_writeall_send(TALLOC_CTX
*mem_ctx
,
835 struct event_context
*ev
,
836 struct cli_state
*cli
,
840 off_t offset
, size_t size
)
842 struct async_req
*result
;
843 struct async_req
*subreq
;
844 struct cli_writeall_state
*state
;
846 result
= async_req_new(mem_ctx
);
847 if (result
== NULL
) {
850 state
= talloc(result
, struct cli_writeall_state
);
854 result
->private_data
= state
;
861 state
->offset
= offset
;
865 subreq
= cli_write_andx_send(state
, state
->ev
, state
->cli
, state
->fnum
,
866 state
->mode
, state
->buf
, state
->offset
,
868 if (subreq
== NULL
) {
872 subreq
->async
.fn
= cli_writeall_written
;
873 subreq
->async
.priv
= result
;
881 static void cli_writeall_written(struct async_req
*subreq
)
883 struct async_req
*req
= talloc_get_type_abort(
884 subreq
->async
.priv
, struct async_req
);
885 struct cli_writeall_state
*state
= talloc_get_type_abort(
886 req
->private_data
, struct cli_writeall_state
);
888 size_t written
, to_write
;
890 status
= cli_write_andx_recv(subreq
, &written
);
892 if (!NT_STATUS_IS_OK(status
)) {
893 async_req_error(req
, status
);
897 state
->written
+= written
;
899 if (state
->written
> state
->size
) {
900 async_req_error(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
904 to_write
= state
->size
- state
->written
;
911 subreq
= cli_write_andx_send(state
, state
->ev
, state
->cli
, state
->fnum
,
913 state
->buf
+ state
->written
,
914 state
->offset
+ state
->written
, to_write
);
915 if (subreq
== NULL
) {
916 async_req_error(req
, NT_STATUS_NO_MEMORY
);
920 subreq
->async
.fn
= cli_writeall_written
;
921 subreq
->async
.priv
= req
;
924 static NTSTATUS
cli_writeall_recv(struct async_req
*req
)
926 return async_req_simple_recv(req
);
929 struct cli_push_state
{
930 struct async_req
*req
;
932 struct event_context
*ev
;
933 struct cli_state
*cli
;
939 size_t (*source
)(uint8_t *buf
, size_t n
, void *priv
);
948 * Outstanding requests
951 struct async_req
**reqs
;
958 static void cli_push_written(struct async_req
*req
);
960 struct async_req
*cli_push_send(TALLOC_CTX
*mem_ctx
, struct event_context
*ev
,
961 struct cli_state
*cli
,
962 uint16_t fnum
, uint16_t mode
,
963 off_t start_offset
, size_t window_size
,
964 size_t (*source
)(uint8_t *buf
, size_t n
,
968 struct async_req
*result
;
969 struct cli_push_state
*state
;
972 result
= async_req_new(mem_ctx
);
973 if (result
== NULL
) {
976 state
= talloc(result
, struct cli_push_state
);
980 result
->private_data
= state
;
986 state
->start_offset
= start_offset
;
988 state
->source
= source
;
994 state
->chunk_size
= cli_write_max_bufsize(cli
, mode
);
996 state
->num_reqs
= MAX(window_size
/state
->chunk_size
, 1);
997 state
->num_reqs
= MIN(state
->num_reqs
, cli
->max_mux
);
999 state
->reqs
= TALLOC_ZERO_ARRAY(state
, struct async_req
*,
1001 if (state
->reqs
== NULL
) {
1005 state
->buf
= TALLOC_ARRAY(
1006 state
, uint8_t, state
->chunk_size
* state
->num_reqs
);
1007 if (state
->buf
== NULL
) {
1011 for (i
=0; i
<state
->num_reqs
; i
++) {
1013 uint8_t *buf
= state
->buf
+ i
*state
->chunk_size
;
1015 to_write
= state
->source(buf
, state
->chunk_size
, state
->priv
);
1016 if (to_write
== 0) {
1021 state
->reqs
[i
] = cli_writeall_send(
1022 state
->reqs
, state
->ev
, state
->cli
, state
->fnum
,
1023 state
->mode
, buf
, state
->start_offset
+ state
->sent
,
1025 if (state
->reqs
[i
] == NULL
) {
1029 state
->reqs
[i
]->async
.fn
= cli_push_written
;
1030 state
->reqs
[i
]->async
.priv
= state
;
1032 state
->sent
+= to_write
;
1033 state
->pending
+= 1;
1037 if (!async_post_status(result
, ev
, NT_STATUS_OK
)) {
1046 TALLOC_FREE(result
);
1050 static void cli_push_written(struct async_req
*req
)
1052 struct cli_push_state
*state
= talloc_get_type_abort(
1053 req
->async
.priv
, struct cli_push_state
);
1059 for (i
=0; i
<state
->num_reqs
; i
++) {
1060 if (state
->reqs
[i
] == req
) {
1065 if (i
== state
->num_reqs
) {
1066 async_req_error(state
->req
, NT_STATUS_INTERNAL_ERROR
);
1070 status
= cli_writeall_recv(req
);
1071 TALLOC_FREE(state
->reqs
[i
]);
1073 if (!NT_STATUS_IS_OK(status
)) {
1074 async_req_error(state
->req
, status
);
1078 state
->pending
-= 1;
1079 if (state
->pending
== 0) {
1080 async_req_done(state
->req
);
1088 buf
= state
->buf
+ i
* state
->chunk_size
;
1090 to_write
= state
->source(buf
, state
->chunk_size
, state
->priv
);
1091 if (to_write
== 0) {
1096 state
->reqs
[i
] = cli_writeall_send(
1097 state
->reqs
, state
->ev
, state
->cli
, state
->fnum
,
1098 state
->mode
, buf
, state
->start_offset
+ state
->sent
, to_write
);
1099 if (state
->reqs
[i
] == NULL
) {
1100 async_req_error(state
->req
, NT_STATUS_NO_MEMORY
);
1104 state
->reqs
[i
]->async
.fn
= cli_push_written
;
1105 state
->reqs
[i
]->async
.priv
= state
;
1107 state
->sent
+= to_write
;
1108 state
->pending
+= 1;
1111 NTSTATUS
cli_push_recv(struct async_req
*req
)
1113 return async_req_simple_recv(req
);
1116 NTSTATUS
cli_push(struct cli_state
*cli
, uint16_t fnum
, uint16_t mode
,
1117 off_t start_offset
, size_t window_size
,
1118 size_t (*source
)(uint8_t *buf
, size_t n
, void *priv
),
1121 TALLOC_CTX
*frame
= talloc_stackframe();
1122 struct event_context
*ev
;
1123 struct async_req
*req
;
1124 NTSTATUS result
= NT_STATUS_NO_MEMORY
;
1126 if (cli
->fd_event
!= NULL
) {
1128 * Can't use sync call while an async call is in flight
1130 return NT_STATUS_INVALID_PARAMETER
;
1133 ev
= event_context_init(frame
);
1138 req
= cli_push_send(frame
, ev
, cli
, fnum
, mode
, start_offset
,
1139 window_size
, source
, priv
);
1144 while (req
->state
< ASYNC_REQ_DONE
) {
1145 event_loop_once(ev
);
1148 result
= cli_push_recv(req
);