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_nterror(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 if (!async_req_setup(mem_ctx
, &result
, &state
,
282 struct cli_pull_state
)) {
285 result
->print
= cli_pull_print
;
291 state
->start_offset
= start_offset
;
300 if (!async_post_ntstatus(result
, ev
, NT_STATUS_OK
)) {
306 state
->chunk_size
= cli_read_max_bufsize(cli
);
308 state
->num_reqs
= MAX(window_size
/state
->chunk_size
, 1);
309 state
->num_reqs
= MIN(state
->num_reqs
, cli
->max_mux
);
311 state
->reqs
= TALLOC_ZERO_ARRAY(state
, struct async_req
*,
313 if (state
->reqs
== NULL
) {
317 state
->requested
= 0;
319 for (i
=0; i
<state
->num_reqs
; i
++) {
321 size_t request_thistime
;
323 if (state
->requested
>= size
) {
328 size_left
= size
- state
->requested
;
329 request_thistime
= MIN(size_left
, state
->chunk_size
);
331 state
->reqs
[i
] = cli_read_andx_send(
332 state
->reqs
, ev
, cli
, fnum
,
333 state
->start_offset
+ state
->requested
,
336 if (state
->reqs
[i
] == NULL
) {
340 state
->reqs
[i
]->async
.fn
= cli_pull_read_done
;
341 state
->reqs
[i
]->async
.priv
= result
;
343 state
->requested
+= request_thistime
;
353 * Handle incoming read replies, push the data into sink and send out new
354 * requests if necessary.
357 static void cli_pull_read_done(struct async_req
*read_req
)
359 struct async_req
*pull_req
= talloc_get_type_abort(
360 read_req
->async
.priv
, struct async_req
);
361 struct cli_pull_state
*state
= talloc_get_type_abort(
362 pull_req
->private_data
, struct cli_pull_state
);
363 struct cli_request
*read_state
= talloc_get_type_abort(
364 read_req
->private_data
, struct cli_request
);
367 status
= cli_read_andx_recv(read_req
, &read_state
->data
.read
.received
,
368 &read_state
->data
.read
.rcvbuf
);
369 if (!NT_STATUS_IS_OK(status
)) {
370 async_req_nterror(state
->req
, status
);
375 * This loop is the one to take care of out-of-order replies. All
376 * pending requests are in state->reqs, state->reqs[top_req] is the
377 * one that is to be pushed next. If however a request later than
378 * top_req is replied to, then we can't push yet. If top_req is
379 * replied to at a later point then, we need to push all the finished
383 while (state
->reqs
[state
->top_req
] != NULL
) {
384 struct cli_request
*top_read
;
386 DEBUG(11, ("cli_pull_read_done: top_req = %d\n",
389 if (state
->reqs
[state
->top_req
]->state
< ASYNC_REQ_DONE
) {
390 DEBUG(11, ("cli_pull_read_done: top request not yet "
395 top_read
= talloc_get_type_abort(
396 state
->reqs
[state
->top_req
]->private_data
,
399 DEBUG(10, ("cli_pull_read_done: Pushing %d bytes, %d already "
400 "pushed\n", (int)top_read
->data
.read
.received
,
401 (int)state
->pushed
));
403 status
= state
->sink((char *)top_read
->data
.read
.rcvbuf
,
404 top_read
->data
.read
.received
,
406 if (!NT_STATUS_IS_OK(status
)) {
407 async_req_nterror(state
->req
, status
);
410 state
->pushed
+= top_read
->data
.read
.received
;
412 TALLOC_FREE(state
->reqs
[state
->top_req
]);
414 if (state
->requested
< state
->size
) {
415 struct async_req
*new_req
;
417 size_t request_thistime
;
419 size_left
= state
->size
- state
->requested
;
420 request_thistime
= MIN(size_left
, state
->chunk_size
);
422 DEBUG(10, ("cli_pull_read_done: Requesting %d bytes "
423 "at %d, position %d\n",
424 (int)request_thistime
,
425 (int)(state
->start_offset
429 new_req
= cli_read_andx_send(
430 state
->reqs
, state
->ev
, state
->cli
,
432 state
->start_offset
+ state
->requested
,
435 if (async_req_nomem(new_req
, state
->req
)) {
439 new_req
->async
.fn
= cli_pull_read_done
;
440 new_req
->async
.priv
= pull_req
;
442 state
->reqs
[state
->top_req
] = new_req
;
443 state
->requested
+= request_thistime
;
446 state
->top_req
= (state
->top_req
+1) % state
->num_reqs
;
449 async_req_done(pull_req
);
452 NTSTATUS
cli_pull_recv(struct async_req
*req
, SMB_OFF_T
*received
)
454 struct cli_pull_state
*state
= talloc_get_type_abort(
455 req
->private_data
, struct cli_pull_state
);
458 if (async_req_is_nterror(req
, &status
)) {
461 *received
= state
->pushed
;
465 NTSTATUS
cli_pull(struct cli_state
*cli
, uint16_t fnum
,
466 off_t start_offset
, SMB_OFF_T size
, size_t window_size
,
467 NTSTATUS (*sink
)(char *buf
, size_t n
, void *priv
),
468 void *priv
, SMB_OFF_T
*received
)
470 TALLOC_CTX
*frame
= talloc_stackframe();
471 struct event_context
*ev
;
472 struct async_req
*req
;
473 NTSTATUS result
= NT_STATUS_NO_MEMORY
;
475 if (cli
->fd_event
!= NULL
) {
477 * Can't use sync call while an async call is in flight
479 return NT_STATUS_INVALID_PARAMETER
;
482 ev
= event_context_init(frame
);
487 req
= cli_pull_send(frame
, ev
, cli
, fnum
, start_offset
, size
,
488 window_size
, sink
, priv
);
493 while (req
->state
< ASYNC_REQ_DONE
) {
497 result
= cli_pull_recv(req
, received
);
503 static NTSTATUS
cli_read_sink(char *buf
, size_t n
, void *priv
)
505 char **pbuf
= (char **)priv
;
506 memcpy(*pbuf
, buf
, n
);
511 ssize_t
cli_read(struct cli_state
*cli
, int fnum
, char *buf
,
512 off_t offset
, size_t size
)
517 status
= cli_pull(cli
, fnum
, offset
, size
, size
,
518 cli_read_sink
, &buf
, &ret
);
519 if (!NT_STATUS_IS_OK(status
)) {
520 cli_set_error(cli
, status
);
526 /****************************************************************************
527 Issue a single SMBwrite and don't wait for a reply.
528 ****************************************************************************/
530 static bool cli_issue_write(struct cli_state
*cli
,
539 bool large_writex
= false;
540 /* We can only do direct writes if not signing and not encrypting. */
541 bool direct_writes
= !client_is_signing_on(cli
) && !cli_encryption_on(cli
);
543 if (!direct_writes
&& size
+ 1 > cli
->bufsize
) {
544 cli
->outbuf
= (char *)SMB_REALLOC(cli
->outbuf
, size
+ 1024);
548 cli
->inbuf
= (char *)SMB_REALLOC(cli
->inbuf
, size
+ 1024);
549 if (cli
->inbuf
== NULL
) {
550 SAFE_FREE(cli
->outbuf
);
553 cli
->bufsize
= size
+ 1024;
556 memset(cli
->outbuf
,'\0',smb_size
);
557 memset(cli
->inbuf
,'\0',smb_size
);
559 if (cli
->capabilities
& CAP_LARGE_FILES
) {
564 cli_set_message(cli
->outbuf
,14,0,True
);
566 cli_set_message(cli
->outbuf
,12,0,True
);
569 SCVAL(cli
->outbuf
,smb_com
,SMBwriteX
);
570 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
571 cli_setup_packet(cli
);
573 SCVAL(cli
->outbuf
,smb_vwv0
,0xFF);
574 SSVAL(cli
->outbuf
,smb_vwv2
,fnum
);
576 SIVAL(cli
->outbuf
,smb_vwv3
,offset
);
577 SIVAL(cli
->outbuf
,smb_vwv5
,0);
578 SSVAL(cli
->outbuf
,smb_vwv7
,mode
);
580 SSVAL(cli
->outbuf
,smb_vwv8
,(mode
& 0x0008) ? size
: 0);
582 * According to CIFS-TR-1p00, this following field should only
583 * be set if CAP_LARGE_WRITEX is set. We should check this
584 * locally. However, this check might already have been
585 * done by our callers.
587 SSVAL(cli
->outbuf
,smb_vwv9
,(size
>>16));
588 SSVAL(cli
->outbuf
,smb_vwv10
,size
);
589 /* +1 is pad byte. */
590 SSVAL(cli
->outbuf
,smb_vwv11
,
591 smb_buf(cli
->outbuf
) - smb_base(cli
->outbuf
) + 1);
594 SIVAL(cli
->outbuf
,smb_vwv12
,(((uint64_t)offset
)>>32) & 0xffffffff);
597 p
= smb_base(cli
->outbuf
) + SVAL(cli
->outbuf
,smb_vwv11
) -1;
598 *p
++ = '\0'; /* pad byte. */
599 if (!direct_writes
) {
600 memcpy(p
, buf
, size
);
602 if (size
> 0x1FFFF) {
603 /* This is a POSIX 14 word large write. */
604 set_message_bcc(cli
->outbuf
, 0); /* Set bcc to zero. */
605 _smb_setlen_large(cli
->outbuf
,smb_size
+ 28 + 1 /* pad */ + size
- 4);
607 cli_setup_bcc(cli
, p
+size
);
610 SSVAL(cli
->outbuf
,smb_mid
,cli
->mid
+ i
);
612 show_msg(cli
->outbuf
);
614 /* For direct writes we now need to write the data
615 * directly out of buf. */
616 return cli_send_smb_direct_writeX(cli
, buf
, size
);
618 return cli_send_smb(cli
);
622 /****************************************************************************
624 write_mode: 0x0001 disallow write cacheing
625 0x0002 return bytes remaining
626 0x0004 use raw named pipe protocol
627 0x0008 start of message mode named pipe protocol
628 ****************************************************************************/
630 ssize_t
cli_write(struct cli_state
*cli
,
631 int fnum
, uint16 write_mode
,
632 const char *buf
, off_t offset
, size_t size
)
634 ssize_t bwritten
= 0;
635 unsigned int issued
= 0;
636 unsigned int received
= 0;
641 if(cli
->max_mux
> 1) {
642 mpx
= cli
->max_mux
-1;
647 writesize
= cli_write_max_bufsize(cli
, write_mode
);
649 blocks
= (size
+ (writesize
-1)) / writesize
;
651 while (received
< blocks
) {
653 while ((issued
- received
< mpx
) && (issued
< blocks
)) {
654 ssize_t bsent
= issued
* writesize
;
655 ssize_t size1
= MIN(writesize
, size
- bsent
);
657 if (!cli_issue_write(cli
, fnum
, offset
+ bsent
,
665 if (!cli_receive_smb(cli
)) {
671 if (cli_is_error(cli
))
674 bwritten
+= SVAL(cli
->inbuf
, smb_vwv2
);
675 if (writesize
> 0xFFFF) {
676 bwritten
+= (((int)(SVAL(cli
->inbuf
, smb_vwv4
)))<<16);
680 while (received
< issued
&& cli_receive_smb(cli
)) {
687 /****************************************************************************
688 write to a file using a SMBwrite and not bypassing 0 byte writes
689 ****************************************************************************/
691 ssize_t
cli_smbwrite(struct cli_state
*cli
,
692 int fnum
, char *buf
, off_t offset
, size_t size1
)
698 size_t size
= MIN(size1
, cli
->max_xmit
- 48);
700 memset(cli
->outbuf
,'\0',smb_size
);
701 memset(cli
->inbuf
,'\0',smb_size
);
703 cli_set_message(cli
->outbuf
,5, 0,True
);
705 SCVAL(cli
->outbuf
,smb_com
,SMBwrite
);
706 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
707 cli_setup_packet(cli
);
709 SSVAL(cli
->outbuf
,smb_vwv0
,fnum
);
710 SSVAL(cli
->outbuf
,smb_vwv1
,size
);
711 SIVAL(cli
->outbuf
,smb_vwv2
,offset
);
712 SSVAL(cli
->outbuf
,smb_vwv4
,0);
714 p
= smb_buf(cli
->outbuf
);
716 SSVAL(p
, 0, size
); p
+= 2;
717 memcpy(p
, buf
+ total
, size
); p
+= size
;
719 cli_setup_bcc(cli
, p
);
721 if (!cli_send_smb(cli
))
724 if (!cli_receive_smb(cli
))
727 if (cli_is_error(cli
))
730 size
= SVAL(cli
->inbuf
,smb_vwv0
);
744 * Send a write&x request
747 struct async_req
*cli_write_andx_send(TALLOC_CTX
*mem_ctx
,
748 struct event_context
*ev
,
749 struct cli_state
*cli
, uint16_t fnum
,
750 uint16_t mode
, const uint8_t *buf
,
751 off_t offset
, size_t size
)
753 bool bigoffset
= ((cli
->capabilities
& CAP_LARGE_FILES
) != 0);
754 uint8_t wct
= bigoffset
? 14 : 12;
755 size_t max_write
= cli_write_max_bufsize(cli
, mode
);
758 size
= MIN(size
, max_write
);
760 SCVAL(vwv
+0, 0, 0xFF);
763 SSVAL(vwv
+2, 0, fnum
);
764 SIVAL(vwv
+3, 0, offset
);
766 SSVAL(vwv
+7, 0, mode
);
768 SSVAL(vwv
+9, 0, (size
>>16));
769 SSVAL(vwv
+10, 0, size
);
773 + 1 /* the wct field */
775 + 2 /* num_bytes field */
779 SIVAL(vwv
+12, 0, (((uint64_t)offset
)>>32) & 0xffffffff);
782 return cli_request_send(mem_ctx
, ev
, cli
, SMBwriteX
, 0, wct
, vwv
,
786 NTSTATUS
cli_write_andx_recv(struct async_req
*req
, size_t *pwritten
)
795 if (async_req_is_nterror(req
, &status
)) {
799 status
= cli_pull_reply(req
, &wct
, &vwv
, &num_bytes
, &bytes
);
801 if (NT_STATUS_IS_ERR(status
)) {
806 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
809 written
= SVAL(vwv
+2, 0);
810 written
|= SVAL(vwv
+4, 0)<<16;
816 struct cli_writeall_state
{
817 struct event_context
*ev
;
818 struct cli_state
*cli
;
827 static void cli_writeall_written(struct async_req
*req
);
829 static struct async_req
*cli_writeall_send(TALLOC_CTX
*mem_ctx
,
830 struct event_context
*ev
,
831 struct cli_state
*cli
,
835 off_t offset
, size_t size
)
837 struct async_req
*result
;
838 struct async_req
*subreq
;
839 struct cli_writeall_state
*state
;
841 if (!async_req_setup(mem_ctx
, &result
, &state
,
842 struct cli_writeall_state
)) {
850 state
->offset
= offset
;
854 subreq
= cli_write_andx_send(state
, state
->ev
, state
->cli
, state
->fnum
,
855 state
->mode
, state
->buf
, state
->offset
,
857 if (subreq
== NULL
) {
861 subreq
->async
.fn
= cli_writeall_written
;
862 subreq
->async
.priv
= result
;
870 static void cli_writeall_written(struct async_req
*subreq
)
872 struct async_req
*req
= talloc_get_type_abort(
873 subreq
->async
.priv
, struct async_req
);
874 struct cli_writeall_state
*state
= talloc_get_type_abort(
875 req
->private_data
, struct cli_writeall_state
);
877 size_t written
, to_write
;
879 status
= cli_write_andx_recv(subreq
, &written
);
881 if (!NT_STATUS_IS_OK(status
)) {
882 async_req_nterror(req
, status
);
886 state
->written
+= written
;
888 if (state
->written
> state
->size
) {
889 async_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
893 to_write
= state
->size
- state
->written
;
900 subreq
= cli_write_andx_send(state
, state
->ev
, state
->cli
, state
->fnum
,
902 state
->buf
+ state
->written
,
903 state
->offset
+ state
->written
, to_write
);
904 if (subreq
== NULL
) {
905 async_req_nterror(req
, NT_STATUS_NO_MEMORY
);
909 subreq
->async
.fn
= cli_writeall_written
;
910 subreq
->async
.priv
= req
;
913 static NTSTATUS
cli_writeall_recv(struct async_req
*req
)
915 return async_req_simple_recv_ntstatus(req
);
918 struct cli_push_state
{
919 struct async_req
*req
;
921 struct event_context
*ev
;
922 struct cli_state
*cli
;
928 size_t (*source
)(uint8_t *buf
, size_t n
, void *priv
);
937 * Outstanding requests
940 struct async_req
**reqs
;
947 static void cli_push_written(struct async_req
*req
);
949 struct async_req
*cli_push_send(TALLOC_CTX
*mem_ctx
, struct event_context
*ev
,
950 struct cli_state
*cli
,
951 uint16_t fnum
, uint16_t mode
,
952 off_t start_offset
, size_t window_size
,
953 size_t (*source
)(uint8_t *buf
, size_t n
,
957 struct async_req
*result
;
958 struct cli_push_state
*state
;
961 if (!async_req_setup(mem_ctx
, &result
, &state
,
962 struct cli_push_state
)) {
970 state
->start_offset
= start_offset
;
972 state
->source
= source
;
978 state
->chunk_size
= cli_write_max_bufsize(cli
, mode
);
980 state
->num_reqs
= MAX(window_size
/state
->chunk_size
, 1);
981 state
->num_reqs
= MIN(state
->num_reqs
, cli
->max_mux
);
983 state
->reqs
= TALLOC_ZERO_ARRAY(state
, struct async_req
*,
985 if (state
->reqs
== NULL
) {
989 state
->buf
= TALLOC_ARRAY(
990 state
, uint8_t, state
->chunk_size
* state
->num_reqs
);
991 if (state
->buf
== NULL
) {
995 for (i
=0; i
<state
->num_reqs
; i
++) {
997 uint8_t *buf
= state
->buf
+ i
*state
->chunk_size
;
999 to_write
= state
->source(buf
, state
->chunk_size
, state
->priv
);
1000 if (to_write
== 0) {
1005 state
->reqs
[i
] = cli_writeall_send(
1006 state
->reqs
, state
->ev
, state
->cli
, state
->fnum
,
1007 state
->mode
, buf
, state
->start_offset
+ state
->sent
,
1009 if (state
->reqs
[i
] == NULL
) {
1013 state
->reqs
[i
]->async
.fn
= cli_push_written
;
1014 state
->reqs
[i
]->async
.priv
= state
;
1016 state
->sent
+= to_write
;
1017 state
->pending
+= 1;
1021 if (!async_post_ntstatus(result
, ev
, NT_STATUS_OK
)) {
1030 TALLOC_FREE(result
);
1034 static void cli_push_written(struct async_req
*req
)
1036 struct cli_push_state
*state
= talloc_get_type_abort(
1037 req
->async
.priv
, struct cli_push_state
);
1043 for (i
=0; i
<state
->num_reqs
; i
++) {
1044 if (state
->reqs
[i
] == req
) {
1049 if (i
== state
->num_reqs
) {
1050 async_req_nterror(state
->req
, NT_STATUS_INTERNAL_ERROR
);
1054 status
= cli_writeall_recv(req
);
1055 TALLOC_FREE(state
->reqs
[i
]);
1057 if (!NT_STATUS_IS_OK(status
)) {
1058 async_req_nterror(state
->req
, status
);
1062 state
->pending
-= 1;
1063 if (state
->pending
== 0) {
1064 async_req_done(state
->req
);
1072 buf
= state
->buf
+ i
* state
->chunk_size
;
1074 to_write
= state
->source(buf
, state
->chunk_size
, state
->priv
);
1075 if (to_write
== 0) {
1080 state
->reqs
[i
] = cli_writeall_send(
1081 state
->reqs
, state
->ev
, state
->cli
, state
->fnum
,
1082 state
->mode
, buf
, state
->start_offset
+ state
->sent
, to_write
);
1083 if (state
->reqs
[i
] == NULL
) {
1084 async_req_nterror(state
->req
, NT_STATUS_NO_MEMORY
);
1088 state
->reqs
[i
]->async
.fn
= cli_push_written
;
1089 state
->reqs
[i
]->async
.priv
= state
;
1091 state
->sent
+= to_write
;
1092 state
->pending
+= 1;
1095 NTSTATUS
cli_push_recv(struct async_req
*req
)
1097 return async_req_simple_recv_ntstatus(req
);
1100 NTSTATUS
cli_push(struct cli_state
*cli
, uint16_t fnum
, uint16_t mode
,
1101 off_t start_offset
, size_t window_size
,
1102 size_t (*source
)(uint8_t *buf
, size_t n
, void *priv
),
1105 TALLOC_CTX
*frame
= talloc_stackframe();
1106 struct event_context
*ev
;
1107 struct async_req
*req
;
1108 NTSTATUS result
= NT_STATUS_NO_MEMORY
;
1110 if (cli
->fd_event
!= NULL
) {
1112 * Can't use sync call while an async call is in flight
1114 return NT_STATUS_INVALID_PARAMETER
;
1117 ev
= event_context_init(frame
);
1122 req
= cli_push_send(frame
, ev
, cli
, fnum
, mode
, start_offset
,
1123 window_size
, source
, priv
);
1128 while (req
->state
< ASYNC_REQ_DONE
) {
1129 event_loop_once(ev
);
1132 result
= cli_push_recv(req
);