4 * Copyright (C) International Business Machines Corp., 2002,2008
5 * Author(s): Steve French (sfrench@us.ibm.com)
6 * Jeremy Allison (jra@samba.org) 2006.
8 * This library is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published
10 * by the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
16 * the GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <linux/list.h>
25 #include <linux/gfp.h>
26 #include <linux/wait.h>
27 #include <linux/net.h>
28 #include <linux/delay.h>
29 #include <linux/freezer.h>
30 #include <linux/tcp.h>
31 #include <linux/bvec.h>
32 #include <linux/highmem.h>
33 #include <linux/uaccess.h>
34 #include <asm/processor.h>
35 #include <linux/mempool.h>
38 #include "cifsproto.h"
39 #include "cifs_debug.h"
40 #include "smb2proto.h"
41 #include "smbdirect.h"
43 /* Max number of iovectors we can use off the stack when sending requests. */
44 #define CIFS_MAX_IOV_SIZE 8
47 cifs_wake_up_task(struct mid_q_entry *mid)
49 wake_up_process(mid->callback_data);
53 AllocMidQEntry(const struct smb_hdr *smb_buffer, struct TCP_Server_Info *server)
55 struct mid_q_entry *temp;
58 cifs_dbg(VFS, "Null TCP session in AllocMidQEntry\n");
62 temp = mempool_alloc(cifs_mid_poolp, GFP_NOFS);
63 memset(temp, 0, sizeof(struct mid_q_entry));
64 temp->mid = get_mid(smb_buffer);
65 temp->pid = current->pid;
66 temp->command = cpu_to_le16(smb_buffer->Command);
67 cifs_dbg(FYI, "For smb_command %d\n", smb_buffer->Command);
68 /* do_gettimeofday(&temp->when_sent);*/ /* easier to use jiffies */
69 /* when mid allocated can be before when sent */
70 temp->when_alloc = jiffies;
71 temp->server = server;
74 * The default is for the mid to be synchronous, so the
75 * default callback just wakes up the current task.
77 temp->callback = cifs_wake_up_task;
78 temp->callback_data = current;
80 atomic_inc(&midCount);
81 temp->mid_state = MID_REQUEST_ALLOCATED;
86 DeleteMidQEntry(struct mid_q_entry *midEntry)
88 #ifdef CONFIG_CIFS_STATS2
89 __le16 command = midEntry->server->vals->lock_cmd;
92 midEntry->mid_state = MID_FREE;
93 atomic_dec(&midCount);
94 if (midEntry->large_buf)
95 cifs_buf_release(midEntry->resp_buf);
97 cifs_small_buf_release(midEntry->resp_buf);
98 #ifdef CONFIG_CIFS_STATS2
100 /* commands taking longer than one second are indications that
101 something is wrong, unless it is quite a slow link or server */
102 if (time_after(now, midEntry->when_alloc + HZ)) {
103 if ((cifsFYI & CIFS_TIMER) && (midEntry->command != command)) {
104 pr_debug(" CIFS slow rsp: cmd %d mid %llu",
105 midEntry->command, midEntry->mid);
106 pr_info(" A: 0x%lx S: 0x%lx R: 0x%lx\n",
107 now - midEntry->when_alloc,
108 now - midEntry->when_sent,
109 now - midEntry->when_received);
113 mempool_free(midEntry, cifs_mid_poolp);
117 cifs_delete_mid(struct mid_q_entry *mid)
119 spin_lock(&GlobalMid_Lock);
120 list_del(&mid->qhead);
121 spin_unlock(&GlobalMid_Lock);
123 DeleteMidQEntry(mid);
127 * smb_send_kvec - send an array of kvecs to the server
128 * @server: Server to send the data to
129 * @smb_msg: Message to send
130 * @sent: amount of data sent on socket is stored here
132 * Our basic "send data to server" function. Should be called with srv_mutex
133 * held. The caller is responsible for handling the results.
136 smb_send_kvec(struct TCP_Server_Info *server, struct msghdr *smb_msg,
141 struct socket *ssocket = server->ssocket;
145 smb_msg->msg_name = (struct sockaddr *) &server->dstaddr;
146 smb_msg->msg_namelen = sizeof(struct sockaddr);
147 smb_msg->msg_control = NULL;
148 smb_msg->msg_controllen = 0;
149 if (server->noblocksnd)
150 smb_msg->msg_flags = MSG_DONTWAIT + MSG_NOSIGNAL;
152 smb_msg->msg_flags = MSG_NOSIGNAL;
154 while (msg_data_left(smb_msg)) {
156 * If blocking send, we try 3 times, since each can block
157 * for 5 seconds. For nonblocking we have to try more
158 * but wait increasing amounts of time allowing time for
159 * socket to clear. The overall time we wait in either
160 * case to send on the socket is about 15 seconds.
161 * Similarly we wait for 15 seconds for a response from
162 * the server in SendReceive[2] for the server to send
163 * a response back for most types of requests (except
164 * SMB Write past end of file which can be slow, and
165 * blocking lock operations). NFS waits slightly longer
166 * than CIFS, but this can make it take longer for
167 * nonresponsive servers to be detected and 15 seconds
168 * is more than enough time for modern networks to
169 * send a packet. In most cases if we fail to send
170 * after the retries we will kill the socket and
171 * reconnect which may clear the network problem.
173 rc = sock_sendmsg(ssocket, smb_msg);
177 (!server->noblocksnd && (retries > 2))) {
178 cifs_dbg(VFS, "sends on sock %p stuck for 15 seconds\n",
182 msleep(1 << retries);
190 /* should never happen, letting socket clear before
191 retrying is our only obvious option here */
192 cifs_dbg(VFS, "tcp sent no data\n");
197 /* send was at least partially successful */
199 retries = 0; /* in case we get ENOSPC on the next send */
205 rqst_len(struct smb_rqst *rqst)
208 struct kvec *iov = rqst->rq_iov;
209 unsigned long buflen = 0;
211 /* total up iov array first */
212 for (i = 0; i < rqst->rq_nvec; i++)
213 buflen += iov[i].iov_len;
216 * Add in the page array if there is one. The caller needs to make
217 * sure rq_offset and rq_tailsz are set correctly. If a buffer of
218 * multiple pages ends at page boundary, rq_tailsz needs to be set to
221 if (rqst->rq_npages) {
222 if (rqst->rq_npages == 1)
223 buflen += rqst->rq_tailsz;
226 * If there is more than one page, calculate the
227 * buffer length based on rq_offset and rq_tailsz
229 buflen += rqst->rq_pagesz * (rqst->rq_npages - 1) -
231 buflen += rqst->rq_tailsz;
239 __smb_send_rqst(struct TCP_Server_Info *server, struct smb_rqst *rqst)
242 struct kvec *iov = rqst->rq_iov;
243 int n_vec = rqst->rq_nvec;
244 unsigned int send_length;
246 size_t total_len = 0, sent, size;
247 struct socket *ssocket = server->ssocket;
248 struct msghdr smb_msg;
250 __be32 rfc1002_marker;
252 if (cifs_rdma_enabled(server) && server->smbd_conn) {
253 rc = smbd_send(server->smbd_conn, rqst);
259 send_length = rqst_len(rqst);
260 rfc1002_marker = cpu_to_be32(send_length);
262 /* cork the socket */
263 kernel_setsockopt(ssocket, SOL_TCP, TCP_CORK,
264 (char *)&val, sizeof(val));
267 /* Generate a rfc1002 marker for SMB2+ */
268 if (server->vals->header_preamble_size == 0) {
270 .iov_base = &rfc1002_marker,
273 iov_iter_kvec(&smb_msg.msg_iter, WRITE | ITER_KVEC, &hiov,
275 rc = smb_send_kvec(server, &smb_msg, &sent);
283 cifs_dbg(FYI, "Sending smb: smb_len=%u\n", send_length);
284 dump_smb(iov[0].iov_base, iov[0].iov_len);
285 dump_smb(iov[1].iov_base, iov[1].iov_len);
287 for (i = 0; i < n_vec; i++)
288 size += iov[i].iov_len;
290 iov_iter_kvec(&smb_msg.msg_iter, WRITE | ITER_KVEC, iov, n_vec, size);
292 rc = smb_send_kvec(server, &smb_msg, &sent);
298 /* now walk the page array and send each page in it */
299 for (i = 0; i < rqst->rq_npages; i++) {
302 bvec.bv_page = rqst->rq_pages[i];
303 rqst_page_get_length(rqst, i, &bvec.bv_len, &bvec.bv_offset);
305 iov_iter_bvec(&smb_msg.msg_iter, WRITE | ITER_BVEC,
306 &bvec, 1, bvec.bv_len);
307 rc = smb_send_kvec(server, &smb_msg, &sent);
317 kernel_setsockopt(ssocket, SOL_TCP, TCP_CORK,
318 (char *)&val, sizeof(val));
320 if ((total_len > 0) && (total_len != send_length)) {
321 cifs_dbg(FYI, "partial send (wanted=%u sent=%zu): terminating session\n",
322 send_length, total_len);
324 * If we have only sent part of an SMB then the next SMB could
325 * be taken as the remainder of this one. We need to kill the
326 * socket so the server throws away the partial SMB
328 server->tcpStatus = CifsNeedReconnect;
331 if (rc < 0 && rc != -EINTR)
332 cifs_dbg(VFS, "Error %d sending data on socket to server\n",
341 smb_send_rqst(struct TCP_Server_Info *server, struct smb_rqst *rqst, int flags)
343 struct smb_rqst cur_rqst;
346 if (!(flags & CIFS_TRANSFORM_REQ))
347 return __smb_send_rqst(server, rqst);
349 if (!server->ops->init_transform_rq ||
350 !server->ops->free_transform_rq) {
351 cifs_dbg(VFS, "Encryption requested but transform callbacks are missed\n");
355 rc = server->ops->init_transform_rq(server, &cur_rqst, rqst);
359 rc = __smb_send_rqst(server, &cur_rqst);
360 server->ops->free_transform_rq(&cur_rqst);
365 smb_send(struct TCP_Server_Info *server, struct smb_hdr *smb_buffer,
366 unsigned int smb_buf_length)
369 struct smb_rqst rqst = { .rq_iov = iov,
372 iov[0].iov_base = smb_buffer;
374 iov[1].iov_base = (char *)smb_buffer + 4;
375 iov[1].iov_len = smb_buf_length;
377 return __smb_send_rqst(server, &rqst);
381 wait_for_free_credits(struct TCP_Server_Info *server, const int timeout,
386 spin_lock(&server->req_lock);
387 if (timeout == CIFS_ASYNC_OP) {
388 /* oplock breaks must not be held up */
391 spin_unlock(&server->req_lock);
397 spin_unlock(&server->req_lock);
398 cifs_num_waiters_inc(server);
399 rc = wait_event_killable(server->request_q,
400 has_credits(server, credits));
401 cifs_num_waiters_dec(server);
404 spin_lock(&server->req_lock);
406 if (server->tcpStatus == CifsExiting) {
407 spin_unlock(&server->req_lock);
412 * Can not count locking commands against total
413 * as they are allowed to block on server.
416 /* update # of requests on the wire to server */
417 if (timeout != CIFS_BLOCKING_OP) {
421 spin_unlock(&server->req_lock);
429 wait_for_free_request(struct TCP_Server_Info *server, const int timeout,
434 val = server->ops->get_credits_field(server, optype);
435 /* Since an echo is already inflight, no need to wait to send another */
436 if (*val <= 0 && optype == CIFS_ECHO_OP)
438 return wait_for_free_credits(server, timeout, val);
442 cifs_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
443 unsigned int *num, unsigned int *credits)
450 static int allocate_mid(struct cifs_ses *ses, struct smb_hdr *in_buf,
451 struct mid_q_entry **ppmidQ)
453 if (ses->server->tcpStatus == CifsExiting) {
457 if (ses->server->tcpStatus == CifsNeedReconnect) {
458 cifs_dbg(FYI, "tcp session dead - return to caller to retry\n");
462 if (ses->status == CifsNew) {
463 if ((in_buf->Command != SMB_COM_SESSION_SETUP_ANDX) &&
464 (in_buf->Command != SMB_COM_NEGOTIATE))
466 /* else ok - we are setting up session */
469 if (ses->status == CifsExiting) {
470 /* check if SMB session is bad because we are setting it up */
471 if (in_buf->Command != SMB_COM_LOGOFF_ANDX)
473 /* else ok - we are shutting down session */
476 *ppmidQ = AllocMidQEntry(in_buf, ses->server);
479 spin_lock(&GlobalMid_Lock);
480 list_add_tail(&(*ppmidQ)->qhead, &ses->server->pending_mid_q);
481 spin_unlock(&GlobalMid_Lock);
486 wait_for_response(struct TCP_Server_Info *server, struct mid_q_entry *midQ)
490 error = wait_event_freezekillable_unsafe(server->response_q,
491 midQ->mid_state != MID_REQUEST_SUBMITTED);
499 cifs_setup_async_request(struct TCP_Server_Info *server, struct smb_rqst *rqst)
502 struct smb_hdr *hdr = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
503 struct mid_q_entry *mid;
505 if (rqst->rq_iov[0].iov_len != 4 ||
506 rqst->rq_iov[0].iov_base + 4 != rqst->rq_iov[1].iov_base)
507 return ERR_PTR(-EIO);
509 /* enable signing if server requires it */
511 hdr->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
513 mid = AllocMidQEntry(hdr, server);
515 return ERR_PTR(-ENOMEM);
517 rc = cifs_sign_rqst(rqst, server, &mid->sequence_number);
519 DeleteMidQEntry(mid);
527 * Send a SMB request and set the callback function in the mid to handle
528 * the result. Caller is responsible for dealing with timeouts.
531 cifs_call_async(struct TCP_Server_Info *server, struct smb_rqst *rqst,
532 mid_receive_t *receive, mid_callback_t *callback,
533 mid_handle_t *handle, void *cbdata, const int flags)
535 int rc, timeout, optype;
536 struct mid_q_entry *mid;
537 unsigned int credits = 0;
539 timeout = flags & CIFS_TIMEOUT_MASK;
540 optype = flags & CIFS_OP_MASK;
542 if ((flags & CIFS_HAS_CREDITS) == 0) {
543 rc = wait_for_free_request(server, timeout, optype);
549 mutex_lock(&server->srv_mutex);
550 mid = server->ops->setup_async_request(server, rqst);
552 mutex_unlock(&server->srv_mutex);
553 add_credits_and_wake_if(server, credits, optype);
557 mid->receive = receive;
558 mid->callback = callback;
559 mid->callback_data = cbdata;
560 mid->handle = handle;
561 mid->mid_state = MID_REQUEST_SUBMITTED;
563 /* put it on the pending_mid_q */
564 spin_lock(&GlobalMid_Lock);
565 list_add_tail(&mid->qhead, &server->pending_mid_q);
566 spin_unlock(&GlobalMid_Lock);
569 * Need to store the time in mid before calling I/O. For call_async,
570 * I/O response may come back and free the mid entry on another thread.
572 cifs_save_when_sent(mid);
573 cifs_in_send_inc(server);
574 rc = smb_send_rqst(server, rqst, flags);
575 cifs_in_send_dec(server);
578 server->sequence_number -= 2;
579 cifs_delete_mid(mid);
582 mutex_unlock(&server->srv_mutex);
587 add_credits_and_wake_if(server, credits, optype);
593 * Send an SMB Request. No response info (other than return code)
594 * needs to be parsed.
596 * flags indicate the type of request buffer and how long to wait
597 * and whether to log NT STATUS code (error) before mapping it to POSIX error
601 SendReceiveNoRsp(const unsigned int xid, struct cifs_ses *ses,
602 char *in_buf, int flags)
609 iov[0].iov_base = in_buf;
610 iov[0].iov_len = get_rfc1002_length(in_buf) + 4;
611 flags |= CIFS_NO_RESP;
612 rc = SendReceive2(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
613 cifs_dbg(NOISY, "SendRcvNoRsp flags %d rc %d\n", flags, rc);
619 cifs_sync_mid_result(struct mid_q_entry *mid, struct TCP_Server_Info *server)
623 cifs_dbg(FYI, "%s: cmd=%d mid=%llu state=%d\n",
624 __func__, le16_to_cpu(mid->command), mid->mid, mid->mid_state);
626 spin_lock(&GlobalMid_Lock);
627 switch (mid->mid_state) {
628 case MID_RESPONSE_RECEIVED:
629 spin_unlock(&GlobalMid_Lock);
631 case MID_RETRY_NEEDED:
634 case MID_RESPONSE_MALFORMED:
641 list_del_init(&mid->qhead);
642 cifs_dbg(VFS, "%s: invalid mid state mid=%llu state=%d\n",
643 __func__, mid->mid, mid->mid_state);
646 spin_unlock(&GlobalMid_Lock);
648 DeleteMidQEntry(mid);
653 send_cancel(struct TCP_Server_Info *server, struct smb_rqst *rqst,
654 struct mid_q_entry *mid)
656 return server->ops->send_cancel ?
657 server->ops->send_cancel(server, rqst, mid) : 0;
661 cifs_check_receive(struct mid_q_entry *mid, struct TCP_Server_Info *server,
664 unsigned int len = get_rfc1002_length(mid->resp_buf) + 4;
666 dump_smb(mid->resp_buf, min_t(u32, 92, len));
668 /* convert the length into a more usable form */
672 struct smb_rqst rqst = { .rq_iov = iov,
675 iov[0].iov_base = mid->resp_buf;
677 iov[1].iov_base = (char *)mid->resp_buf + 4;
678 iov[1].iov_len = len - 4;
679 /* FIXME: add code to kill session */
680 rc = cifs_verify_signature(&rqst, server,
681 mid->sequence_number);
683 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
687 /* BB special case reconnect tid and uid here? */
688 return map_smb_to_linux_error(mid->resp_buf, log_error);
692 cifs_setup_request(struct cifs_ses *ses, struct smb_rqst *rqst)
695 struct smb_hdr *hdr = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
696 struct mid_q_entry *mid;
698 if (rqst->rq_iov[0].iov_len != 4 ||
699 rqst->rq_iov[0].iov_base + 4 != rqst->rq_iov[1].iov_base)
700 return ERR_PTR(-EIO);
702 rc = allocate_mid(ses, hdr, &mid);
705 rc = cifs_sign_rqst(rqst, ses->server, &mid->sequence_number);
707 cifs_delete_mid(mid);
714 cifs_send_recv(const unsigned int xid, struct cifs_ses *ses,
715 struct smb_rqst *rqst, int *resp_buf_type, const int flags,
716 struct kvec *resp_iov)
720 struct mid_q_entry *midQ;
721 unsigned int credits = 1;
724 timeout = flags & CIFS_TIMEOUT_MASK;
725 optype = flags & CIFS_OP_MASK;
727 *resp_buf_type = CIFS_NO_BUFFER; /* no response buf yet */
729 if ((ses == NULL) || (ses->server == NULL)) {
730 cifs_dbg(VFS, "Null session\n");
734 if (ses->server->tcpStatus == CifsExiting)
738 * Ensure that we do not send more than 50 overlapping requests
739 * to the same server. We may make this configurable later or
742 rc = wait_for_free_request(ses->server, timeout, optype);
747 * Make sure that we sign in the same order that we send on this socket
748 * and avoid races inside tcp sendmsg code that could cause corruption
752 mutex_lock(&ses->server->srv_mutex);
754 midQ = ses->server->ops->setup_request(ses, rqst);
756 mutex_unlock(&ses->server->srv_mutex);
757 /* Update # of requests on wire to server */
758 add_credits(ses->server, 1, optype);
759 return PTR_ERR(midQ);
762 midQ->mid_state = MID_REQUEST_SUBMITTED;
763 cifs_in_send_inc(ses->server);
764 rc = smb_send_rqst(ses->server, rqst, flags);
765 cifs_in_send_dec(ses->server);
766 cifs_save_when_sent(midQ);
769 ses->server->sequence_number -= 2;
770 mutex_unlock(&ses->server->srv_mutex);
775 #ifdef CONFIG_CIFS_SMB311
776 if ((ses->status == CifsNew) || (optype & CIFS_NEG_OP))
777 smb311_update_preauth_hash(ses, rqst->rq_iov,
781 if (timeout == CIFS_ASYNC_OP)
784 rc = wait_for_response(ses->server, midQ);
786 cifs_dbg(FYI, "Cancelling wait for mid %llu\n", midQ->mid);
787 send_cancel(ses->server, rqst, midQ);
788 spin_lock(&GlobalMid_Lock);
789 if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
790 midQ->mid_flags |= MID_WAIT_CANCELLED;
791 midQ->callback = DeleteMidQEntry;
792 spin_unlock(&GlobalMid_Lock);
793 add_credits(ses->server, 1, optype);
796 spin_unlock(&GlobalMid_Lock);
799 rc = cifs_sync_mid_result(midQ, ses->server);
801 add_credits(ses->server, 1, optype);
805 if (!midQ->resp_buf || midQ->mid_state != MID_RESPONSE_RECEIVED) {
807 cifs_dbg(FYI, "Bad MID state?\n");
811 buf = (char *)midQ->resp_buf;
812 resp_iov->iov_base = buf;
813 resp_iov->iov_len = midQ->resp_buf_size +
814 ses->server->vals->header_preamble_size;
816 *resp_buf_type = CIFS_LARGE_BUFFER;
818 *resp_buf_type = CIFS_SMALL_BUFFER;
820 #ifdef CONFIG_CIFS_SMB311
821 if ((ses->status == CifsNew) || (optype & CIFS_NEG_OP)) {
823 .iov_base = resp_iov->iov_base,
824 .iov_len = resp_iov->iov_len
826 smb311_update_preauth_hash(ses, &iov, 1);
830 credits = ses->server->ops->get_credits(midQ);
832 rc = ses->server->ops->check_receive(midQ, ses->server,
833 flags & CIFS_LOG_ERROR);
835 /* mark it so buf will not be freed by cifs_delete_mid */
836 if ((flags & CIFS_NO_RESP) == 0)
837 midQ->resp_buf = NULL;
839 cifs_delete_mid(midQ);
840 add_credits(ses->server, credits, optype);
846 SendReceive2(const unsigned int xid, struct cifs_ses *ses,
847 struct kvec *iov, int n_vec, int *resp_buf_type /* ret */,
848 const int flags, struct kvec *resp_iov)
850 struct smb_rqst rqst;
851 struct kvec s_iov[CIFS_MAX_IOV_SIZE], *new_iov;
854 if (n_vec + 1 > CIFS_MAX_IOV_SIZE) {
855 new_iov = kmalloc_array(n_vec + 1, sizeof(struct kvec),
858 /* otherwise cifs_send_recv below sets resp_buf_type */
859 *resp_buf_type = CIFS_NO_BUFFER;
865 /* 1st iov is a RFC1001 length followed by the rest of the packet */
866 memcpy(new_iov + 1, iov, (sizeof(struct kvec) * n_vec));
868 new_iov[0].iov_base = new_iov[1].iov_base;
869 new_iov[0].iov_len = 4;
870 new_iov[1].iov_base += 4;
871 new_iov[1].iov_len -= 4;
873 memset(&rqst, 0, sizeof(struct smb_rqst));
874 rqst.rq_iov = new_iov;
875 rqst.rq_nvec = n_vec + 1;
877 rc = cifs_send_recv(xid, ses, &rqst, resp_buf_type, flags, resp_iov);
878 if (n_vec + 1 > CIFS_MAX_IOV_SIZE)
883 /* Like SendReceive2 but iov[0] does not contain an rfc1002 header */
885 smb2_send_recv(const unsigned int xid, struct cifs_ses *ses,
886 struct kvec *iov, int n_vec, int *resp_buf_type /* ret */,
887 const int flags, struct kvec *resp_iov)
889 struct smb_rqst rqst;
892 memset(&rqst, 0, sizeof(struct smb_rqst));
894 rqst.rq_nvec = n_vec;
896 rc = cifs_send_recv(xid, ses, &rqst, resp_buf_type, flags, resp_iov);
901 SendReceive(const unsigned int xid, struct cifs_ses *ses,
902 struct smb_hdr *in_buf, struct smb_hdr *out_buf,
903 int *pbytes_returned, const int timeout)
906 struct mid_q_entry *midQ;
907 unsigned int len = be32_to_cpu(in_buf->smb_buf_length);
908 struct kvec iov = { .iov_base = in_buf, .iov_len = len };
909 struct smb_rqst rqst = { .rq_iov = &iov, .rq_nvec = 1 };
912 cifs_dbg(VFS, "Null smb session\n");
915 if (ses->server == NULL) {
916 cifs_dbg(VFS, "Null tcp session\n");
920 if (ses->server->tcpStatus == CifsExiting)
923 /* Ensure that we do not send more than 50 overlapping requests
924 to the same server. We may make this configurable later or
927 if (len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
928 cifs_dbg(VFS, "Illegal length, greater than maximum frame, %d\n",
933 rc = wait_for_free_request(ses->server, timeout, 0);
937 /* make sure that we sign in the same order that we send on this socket
938 and avoid races inside tcp sendmsg code that could cause corruption
941 mutex_lock(&ses->server->srv_mutex);
943 rc = allocate_mid(ses, in_buf, &midQ);
945 mutex_unlock(&ses->server->srv_mutex);
946 /* Update # of requests on wire to server */
947 add_credits(ses->server, 1, 0);
951 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
953 mutex_unlock(&ses->server->srv_mutex);
957 midQ->mid_state = MID_REQUEST_SUBMITTED;
959 cifs_in_send_inc(ses->server);
960 rc = smb_send(ses->server, in_buf, len);
961 cifs_in_send_dec(ses->server);
962 cifs_save_when_sent(midQ);
965 ses->server->sequence_number -= 2;
967 mutex_unlock(&ses->server->srv_mutex);
972 if (timeout == CIFS_ASYNC_OP)
975 rc = wait_for_response(ses->server, midQ);
977 send_cancel(ses->server, &rqst, midQ);
978 spin_lock(&GlobalMid_Lock);
979 if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
980 /* no longer considered to be "in-flight" */
981 midQ->callback = DeleteMidQEntry;
982 spin_unlock(&GlobalMid_Lock);
983 add_credits(ses->server, 1, 0);
986 spin_unlock(&GlobalMid_Lock);
989 rc = cifs_sync_mid_result(midQ, ses->server);
991 add_credits(ses->server, 1, 0);
995 if (!midQ->resp_buf || !out_buf ||
996 midQ->mid_state != MID_RESPONSE_RECEIVED) {
998 cifs_dbg(VFS, "Bad MID state?\n");
1002 *pbytes_returned = get_rfc1002_length(midQ->resp_buf);
1003 memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
1004 rc = cifs_check_receive(midQ, ses->server, 0);
1006 cifs_delete_mid(midQ);
1007 add_credits(ses->server, 1, 0);
1012 /* We send a LOCKINGX_CANCEL_LOCK to cause the Windows
1013 blocking lock to return. */
1016 send_lock_cancel(const unsigned int xid, struct cifs_tcon *tcon,
1017 struct smb_hdr *in_buf,
1018 struct smb_hdr *out_buf)
1021 struct cifs_ses *ses = tcon->ses;
1022 LOCK_REQ *pSMB = (LOCK_REQ *)in_buf;
1024 /* We just modify the current in_buf to change
1025 the type of lock from LOCKING_ANDX_SHARED_LOCK
1026 or LOCKING_ANDX_EXCLUSIVE_LOCK to
1027 LOCKING_ANDX_CANCEL_LOCK. */
1029 pSMB->LockType = LOCKING_ANDX_CANCEL_LOCK|LOCKING_ANDX_LARGE_FILES;
1031 pSMB->hdr.Mid = get_next_mid(ses->server);
1033 return SendReceive(xid, ses, in_buf, out_buf,
1034 &bytes_returned, 0);
1038 SendReceiveBlockingLock(const unsigned int xid, struct cifs_tcon *tcon,
1039 struct smb_hdr *in_buf, struct smb_hdr *out_buf,
1040 int *pbytes_returned)
1044 struct mid_q_entry *midQ;
1045 struct cifs_ses *ses;
1046 unsigned int len = be32_to_cpu(in_buf->smb_buf_length);
1047 struct kvec iov = { .iov_base = in_buf, .iov_len = len };
1048 struct smb_rqst rqst = { .rq_iov = &iov, .rq_nvec = 1 };
1050 if (tcon == NULL || tcon->ses == NULL) {
1051 cifs_dbg(VFS, "Null smb session\n");
1056 if (ses->server == NULL) {
1057 cifs_dbg(VFS, "Null tcp session\n");
1061 if (ses->server->tcpStatus == CifsExiting)
1064 /* Ensure that we do not send more than 50 overlapping requests
1065 to the same server. We may make this configurable later or
1068 if (len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
1069 cifs_dbg(VFS, "Illegal length, greater than maximum frame, %d\n",
1074 rc = wait_for_free_request(ses->server, CIFS_BLOCKING_OP, 0);
1078 /* make sure that we sign in the same order that we send on this socket
1079 and avoid races inside tcp sendmsg code that could cause corruption
1082 mutex_lock(&ses->server->srv_mutex);
1084 rc = allocate_mid(ses, in_buf, &midQ);
1086 mutex_unlock(&ses->server->srv_mutex);
1090 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
1092 cifs_delete_mid(midQ);
1093 mutex_unlock(&ses->server->srv_mutex);
1097 midQ->mid_state = MID_REQUEST_SUBMITTED;
1098 cifs_in_send_inc(ses->server);
1099 rc = smb_send(ses->server, in_buf, len);
1100 cifs_in_send_dec(ses->server);
1101 cifs_save_when_sent(midQ);
1104 ses->server->sequence_number -= 2;
1106 mutex_unlock(&ses->server->srv_mutex);
1109 cifs_delete_mid(midQ);
1113 /* Wait for a reply - allow signals to interrupt. */
1114 rc = wait_event_interruptible(ses->server->response_q,
1115 (!(midQ->mid_state == MID_REQUEST_SUBMITTED)) ||
1116 ((ses->server->tcpStatus != CifsGood) &&
1117 (ses->server->tcpStatus != CifsNew)));
1119 /* Were we interrupted by a signal ? */
1120 if ((rc == -ERESTARTSYS) &&
1121 (midQ->mid_state == MID_REQUEST_SUBMITTED) &&
1122 ((ses->server->tcpStatus == CifsGood) ||
1123 (ses->server->tcpStatus == CifsNew))) {
1125 if (in_buf->Command == SMB_COM_TRANSACTION2) {
1126 /* POSIX lock. We send a NT_CANCEL SMB to cause the
1127 blocking lock to return. */
1128 rc = send_cancel(ses->server, &rqst, midQ);
1130 cifs_delete_mid(midQ);
1134 /* Windows lock. We send a LOCKINGX_CANCEL_LOCK
1135 to cause the blocking lock to return. */
1137 rc = send_lock_cancel(xid, tcon, in_buf, out_buf);
1139 /* If we get -ENOLCK back the lock may have
1140 already been removed. Don't exit in this case. */
1141 if (rc && rc != -ENOLCK) {
1142 cifs_delete_mid(midQ);
1147 rc = wait_for_response(ses->server, midQ);
1149 send_cancel(ses->server, &rqst, midQ);
1150 spin_lock(&GlobalMid_Lock);
1151 if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
1152 /* no longer considered to be "in-flight" */
1153 midQ->callback = DeleteMidQEntry;
1154 spin_unlock(&GlobalMid_Lock);
1157 spin_unlock(&GlobalMid_Lock);
1160 /* We got the response - restart system call. */
1164 rc = cifs_sync_mid_result(midQ, ses->server);
1168 /* rcvd frame is ok */
1169 if (out_buf == NULL || midQ->mid_state != MID_RESPONSE_RECEIVED) {
1171 cifs_dbg(VFS, "Bad MID state?\n");
1175 *pbytes_returned = get_rfc1002_length(midQ->resp_buf);
1176 memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
1177 rc = cifs_check_receive(midQ, ses->server, 0);
1179 cifs_delete_mid(midQ);
1180 if (rstart && rc == -EACCES)
1181 return -ERESTARTSYS;