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;
40 * Send a read&x request
43 struct async_req *cli_read_andx_send(TALLOC_CTX *mem_ctx,
44 struct event_context *ev,
45 struct cli_state *cli, int fnum,
46 off_t offset, size_t size)
48 struct async_req *result;
49 struct cli_request *req;
50 bool bigoffset = False;
55 if (size > cli_read_max_bufsize(cli)) {
56 DEBUG(0, ("cli_read_andx_send got size=%d, can only handle "
57 "size=%d\n", (int)size,
58 (int)cli_read_max_bufsize(cli)));
62 SCVAL(vwv + 0, 0, 0xFF);
65 SSVAL(vwv + 2, 0, fnum);
66 SIVAL(vwv + 3, 0, offset);
67 SSVAL(vwv + 5, 0, size);
68 SSVAL(vwv + 6, 0, size);
69 SSVAL(vwv + 7, 0, (size >> 16));
73 if ((uint64_t)offset >> 32) {
76 (((uint64_t)offset)>>32) & 0xffffffff);
80 result = cli_request_send(mem_ctx, ev, cli, SMBreadX, 0, wct, vwv,
86 req = talloc_get_type_abort(result->private_data, struct cli_request);
88 req->data.read.ofs = offset;
89 req->data.read.size = size;
90 req->data.read.received = 0;
91 req->data.read.rcvbuf = NULL;
97 * Pull the data out of a finished async read_and_x request. rcvbuf is
98 * talloced from the request, so better make sure that you copy it away before
99 * you talloc_free(req). "rcvbuf" is NOT a talloc_ctx of its own, so do not
103 NTSTATUS cli_read_andx_recv(struct async_req *req, ssize_t *received,
106 struct cli_request *cli_req = talloc_get_type_abort(
107 req->private_data, struct cli_request);
115 if (async_req_is_error(req, &status)) {
119 status = cli_pull_reply(req, &wct, &vwv, &num_bytes, &bytes);
121 if (NT_STATUS_IS_ERR(status)) {
126 return NT_STATUS_INVALID_NETWORK_RESPONSE;
129 /* size is the number of bytes the server returned.
131 size = SVAL(vwv + 5, 0);
132 size |= (((unsigned int)SVAL(vwv + 7, 0)) << 16);
134 if (size > cli_req->data.read.size) {
135 DEBUG(5,("server returned more than we wanted!\n"));
136 return NT_STATUS_UNEXPECTED_IO_ERROR;
139 *rcvbuf = (uint8_t *)(smb_base(cli_req->inbuf) + SVAL(vwv + 6, 0));
145 * Parallel read support.
147 * cli_pull sends as many read&x requests as the server would allow via
148 * max_mux at a time. When replies flow back in, the data is written into
149 * the callback function "sink" in the right order.
152 struct cli_pull_state {
153 struct async_req *req;
155 struct event_context *ev;
156 struct cli_state *cli;
161 NTSTATUS (*sink)(char *buf, size_t n, void *priv);
167 * Outstanding requests
170 struct async_req **reqs;
173 * For how many bytes did we send requests already?
178 * Next request index to push into "sink". This walks around the "req"
179 * array, taking care that the requests are pushed to "sink" in the
180 * right order. If necessary (i.e. replies don't come in in the right
181 * order), replies are held back in "reqs".
186 * How many bytes did we push into "sink"?
192 static char *cli_pull_print(TALLOC_CTX *mem_ctx, struct async_req *req)
194 struct cli_pull_state *state = talloc_get_type_abort(
195 req->private_data, struct cli_pull_state);
198 result = async_req_print(mem_ctx, req);
199 if (result == NULL) {
203 return talloc_asprintf_append_buffer(
204 result, "num_reqs=%d, top_req=%d",
205 state->num_reqs, state->top_req);
208 static void cli_pull_read_done(struct async_req *read_req);
211 * Prepare an async pull request
214 struct async_req *cli_pull_send(TALLOC_CTX *mem_ctx,
215 struct event_context *ev,
216 struct cli_state *cli,
217 uint16_t fnum, off_t start_offset,
218 SMB_OFF_T size, size_t window_size,
219 NTSTATUS (*sink)(char *buf, size_t n,
223 struct async_req *result;
224 struct cli_pull_state *state;
227 result = async_req_new(mem_ctx, ev);
228 if (result == NULL) {
231 state = talloc(result, struct cli_pull_state);
235 result->private_data = state;
236 result->print = cli_pull_print;
242 state->start_offset = start_offset;
251 if (!async_post_status(result, NT_STATUS_OK)) {
257 state->chunk_size = cli_read_max_bufsize(cli);
259 state->num_reqs = MAX(window_size/state->chunk_size, 1);
260 state->num_reqs = MIN(state->num_reqs, cli->max_mux);
262 state->reqs = TALLOC_ZERO_ARRAY(state, struct async_req *,
264 if (state->reqs == NULL) {
268 state->requested = 0;
270 for (i=0; i<state->num_reqs; i++) {
272 size_t request_thistime;
274 if (state->requested >= size) {
279 size_left = size - state->requested;
280 request_thistime = MIN(size_left, state->chunk_size);
282 state->reqs[i] = cli_read_andx_send(
283 state->reqs, ev, cli, fnum,
284 state->start_offset + state->requested,
287 if (state->reqs[i] == NULL) {
291 state->reqs[i]->async.fn = cli_pull_read_done;
292 state->reqs[i]->async.priv = result;
294 state->requested += request_thistime;
304 * Handle incoming read replies, push the data into sink and send out new
305 * requests if necessary.
308 static void cli_pull_read_done(struct async_req *read_req)
310 struct async_req *pull_req = talloc_get_type_abort(
311 read_req->async.priv, struct async_req);
312 struct cli_pull_state *state = talloc_get_type_abort(
313 pull_req->private_data, struct cli_pull_state);
314 struct cli_request *read_state = talloc_get_type_abort(
315 read_req->private_data, struct cli_request);
318 status = cli_read_andx_recv(read_req, &read_state->data.read.received,
319 &read_state->data.read.rcvbuf);
320 if (!NT_STATUS_IS_OK(status)) {
321 async_req_error(state->req, status);
326 * This loop is the one to take care of out-of-order replies. All
327 * pending requests are in state->reqs, state->reqs[top_req] is the
328 * one that is to be pushed next. If however a request later than
329 * top_req is replied to, then we can't push yet. If top_req is
330 * replied to at a later point then, we need to push all the finished
334 while (state->reqs[state->top_req] != NULL) {
335 struct cli_request *top_read;
337 DEBUG(11, ("cli_pull_read_done: top_req = %d\n",
340 if (state->reqs[state->top_req]->state < ASYNC_REQ_DONE) {
341 DEBUG(11, ("cli_pull_read_done: top request not yet "
346 top_read = talloc_get_type_abort(
347 state->reqs[state->top_req]->private_data,
350 DEBUG(10, ("cli_pull_read_done: Pushing %d bytes, %d already "
351 "pushed\n", (int)top_read->data.read.received,
352 (int)state->pushed));
354 status = state->sink((char *)top_read->data.read.rcvbuf,
355 top_read->data.read.received,
357 if (!NT_STATUS_IS_OK(status)) {
358 async_req_error(state->req, status);
361 state->pushed += top_read->data.read.received;
363 TALLOC_FREE(state->reqs[state->top_req]);
365 if (state->requested < state->size) {
366 struct async_req *new_req;
368 size_t request_thistime;
370 size_left = state->size - state->requested;
371 request_thistime = MIN(size_left, state->chunk_size);
373 DEBUG(10, ("cli_pull_read_done: Requesting %d bytes "
374 "at %d, position %d\n",
375 (int)request_thistime,
376 (int)(state->start_offset
380 new_req = cli_read_andx_send(
381 state->reqs, state->ev, state->cli,
383 state->start_offset + state->requested,
386 if (async_req_nomem(new_req, state->req)) {
390 new_req->async.fn = cli_pull_read_done;
391 new_req->async.priv = pull_req;
393 state->reqs[state->top_req] = new_req;
394 state->requested += request_thistime;
397 state->top_req = (state->top_req+1) % state->num_reqs;
400 async_req_done(pull_req);
403 NTSTATUS cli_pull_recv(struct async_req *req, SMB_OFF_T *received)
405 struct cli_pull_state *state = talloc_get_type_abort(
406 req->private_data, struct cli_pull_state);
409 if (async_req_is_error(req, &status)) {
412 *received = state->pushed;
416 NTSTATUS cli_pull(struct cli_state *cli, uint16_t fnum,
417 off_t start_offset, SMB_OFF_T size, size_t window_size,
418 NTSTATUS (*sink)(char *buf, size_t n, void *priv),
419 void *priv, SMB_OFF_T *received)
421 TALLOC_CTX *frame = talloc_stackframe();
422 struct event_context *ev;
423 struct async_req *req;
424 NTSTATUS result = NT_STATUS_NO_MEMORY;
426 if (cli->fd_event != NULL) {
428 * Can't use sync call while an async call is in flight
430 return NT_STATUS_INVALID_PARAMETER;
433 ev = event_context_init(frame);
438 req = cli_pull_send(frame, ev, cli, fnum, start_offset, size,
439 window_size, sink, priv);
444 while (req->state < ASYNC_REQ_DONE) {
448 result = cli_pull_recv(req, received);
454 static NTSTATUS cli_read_sink(char *buf, size_t n, void *priv)
456 char **pbuf = (char **)priv;
457 memcpy(*pbuf, buf, n);
462 ssize_t cli_read(struct cli_state *cli, int fnum, char *buf,
463 off_t offset, size_t size)
468 status = cli_pull(cli, fnum, offset, size, size,
469 cli_read_sink, &buf, &ret);
470 if (!NT_STATUS_IS_OK(status)) {
471 cli_set_error(cli, status);
477 /****************************************************************************
478 Issue a single SMBwrite and don't wait for a reply.
479 ****************************************************************************/
481 static bool cli_issue_write(struct cli_state *cli,
490 bool large_writex = false;
491 /* We can only do direct writes if not signing and not encrypting. */
492 bool direct_writes = !client_is_signing_on(cli) && !cli_encryption_on(cli);
494 if (!direct_writes && size + 1 > cli->bufsize) {
495 cli->outbuf = (char *)SMB_REALLOC(cli->outbuf, size + 1024);
499 cli->inbuf = (char *)SMB_REALLOC(cli->inbuf, size + 1024);
500 if (cli->inbuf == NULL) {
501 SAFE_FREE(cli->outbuf);
504 cli->bufsize = size + 1024;
507 memset(cli->outbuf,'\0',smb_size);
508 memset(cli->inbuf,'\0',smb_size);
510 if (cli->capabilities & CAP_LARGE_FILES) {
515 cli_set_message(cli->outbuf,14,0,True);
517 cli_set_message(cli->outbuf,12,0,True);
520 SCVAL(cli->outbuf,smb_com,SMBwriteX);
521 SSVAL(cli->outbuf,smb_tid,cli->cnum);
522 cli_setup_packet(cli);
524 SCVAL(cli->outbuf,smb_vwv0,0xFF);
525 SSVAL(cli->outbuf,smb_vwv2,fnum);
527 SIVAL(cli->outbuf,smb_vwv3,offset);
528 SIVAL(cli->outbuf,smb_vwv5,0);
529 SSVAL(cli->outbuf,smb_vwv7,mode);
531 SSVAL(cli->outbuf,smb_vwv8,(mode & 0x0008) ? size : 0);
533 * According to CIFS-TR-1p00, this following field should only
534 * be set if CAP_LARGE_WRITEX is set. We should check this
535 * locally. However, this check might already have been
536 * done by our callers.
538 SSVAL(cli->outbuf,smb_vwv9,(size>>16));
539 SSVAL(cli->outbuf,smb_vwv10,size);
540 /* +1 is pad byte. */
541 SSVAL(cli->outbuf,smb_vwv11,
542 smb_buf(cli->outbuf) - smb_base(cli->outbuf) + 1);
545 SIVAL(cli->outbuf,smb_vwv12,(((uint64_t)offset)>>32) & 0xffffffff);
548 p = smb_base(cli->outbuf) + SVAL(cli->outbuf,smb_vwv11) -1;
549 *p++ = '\0'; /* pad byte. */
550 if (!direct_writes) {
551 memcpy(p, buf, size);
553 if (size > 0x1FFFF) {
554 /* This is a POSIX 14 word large write. */
555 set_message_bcc(cli->outbuf, 0); /* Set bcc to zero. */
556 _smb_setlen_large(cli->outbuf,smb_size + 28 + 1 /* pad */ + size - 4);
558 cli_setup_bcc(cli, p+size);
561 SSVAL(cli->outbuf,smb_mid,cli->mid + i);
563 show_msg(cli->outbuf);
565 /* For direct writes we now need to write the data
566 * directly out of buf. */
567 return cli_send_smb_direct_writeX(cli, buf, size);
569 return cli_send_smb(cli);
573 /****************************************************************************
575 write_mode: 0x0001 disallow write cacheing
576 0x0002 return bytes remaining
577 0x0004 use raw named pipe protocol
578 0x0008 start of message mode named pipe protocol
579 ****************************************************************************/
581 ssize_t cli_write(struct cli_state *cli,
582 int fnum, uint16 write_mode,
583 const char *buf, off_t offset, size_t size)
585 ssize_t bwritten = 0;
586 unsigned int issued = 0;
587 unsigned int received = 0;
592 if(cli->max_mux > 1) {
593 mpx = cli->max_mux-1;
598 /* Default (small) writesize. */
599 writesize = (cli->max_xmit - (smb_size+32)) & ~1023;
601 if (write_mode == 0 &&
602 !client_is_signing_on(cli) &&
603 !cli_encryption_on(cli) &&
604 (cli->posix_capabilities & CIFS_UNIX_LARGE_WRITE_CAP) &&
605 (cli->capabilities & CAP_LARGE_FILES)) {
606 /* Only do massive writes if we can do them direct
607 * with no signing or encrypting - not on a pipe. */
608 writesize = CLI_SAMBA_MAX_POSIX_LARGE_WRITEX_SIZE;
609 } else if ((cli->capabilities & CAP_LARGE_WRITEX) &&
610 (strcmp(cli->dev, "LPT1:") != 0)) {
612 /* Printer devices are restricted to max_xmit
613 * writesize in Vista and XPSP3. */
616 writesize = CLI_SAMBA_MAX_LARGE_WRITEX_SIZE;
617 } else if (!client_is_signing_on(cli)) {
618 /* Windows restricts signed writes to max_xmit.
619 * Found by Volker. */
620 writesize = CLI_WINDOWS_MAX_LARGE_WRITEX_SIZE;
624 blocks = (size + (writesize-1)) / writesize;
626 while (received < blocks) {
628 while ((issued - received < mpx) && (issued < blocks)) {
629 ssize_t bsent = issued * writesize;
630 ssize_t size1 = MIN(writesize, size - bsent);
632 if (!cli_issue_write(cli, fnum, offset + bsent,
640 if (!cli_receive_smb(cli)) {
646 if (cli_is_error(cli))
649 bwritten += SVAL(cli->inbuf, smb_vwv2);
650 if (writesize > 0xFFFF) {
651 bwritten += (((int)(SVAL(cli->inbuf, smb_vwv4)))<<16);
655 while (received < issued && cli_receive_smb(cli)) {
662 /****************************************************************************
663 write to a file using a SMBwrite and not bypassing 0 byte writes
664 ****************************************************************************/
666 ssize_t cli_smbwrite(struct cli_state *cli,
667 int fnum, char *buf, off_t offset, size_t size1)
673 size_t size = MIN(size1, cli->max_xmit - 48);
675 memset(cli->outbuf,'\0',smb_size);
676 memset(cli->inbuf,'\0',smb_size);
678 cli_set_message(cli->outbuf,5, 0,True);
680 SCVAL(cli->outbuf,smb_com,SMBwrite);
681 SSVAL(cli->outbuf,smb_tid,cli->cnum);
682 cli_setup_packet(cli);
684 SSVAL(cli->outbuf,smb_vwv0,fnum);
685 SSVAL(cli->outbuf,smb_vwv1,size);
686 SIVAL(cli->outbuf,smb_vwv2,offset);
687 SSVAL(cli->outbuf,smb_vwv4,0);
689 p = smb_buf(cli->outbuf);
691 SSVAL(p, 0, size); p += 2;
692 memcpy(p, buf + total, size); p += size;
694 cli_setup_bcc(cli, p);
696 if (!cli_send_smb(cli))
699 if (!cli_receive_smb(cli))
702 if (cli_is_error(cli))
705 size = SVAL(cli->inbuf,smb_vwv0);