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 smb2_rqst_len(struct smb_rqst *rqst, bool skip_rfc1002_marker)
210 unsigned long buflen = 0;
212 if (skip_rfc1002_marker && rqst->rq_iov[0].iov_len == 4) {
213 iov = &rqst->rq_iov[1];
214 nvec = rqst->rq_nvec - 1;
217 nvec = rqst->rq_nvec;
220 /* total up iov array first */
221 for (i = 0; i < nvec; i++)
222 buflen += iov[i].iov_len;
225 * Add in the page array if there is one. The caller needs to make
226 * sure rq_offset and rq_tailsz are set correctly. If a buffer of
227 * multiple pages ends at page boundary, rq_tailsz needs to be set to
230 if (rqst->rq_npages) {
231 if (rqst->rq_npages == 1)
232 buflen += rqst->rq_tailsz;
235 * If there is more than one page, calculate the
236 * buffer length based on rq_offset and rq_tailsz
238 buflen += rqst->rq_pagesz * (rqst->rq_npages - 1) -
240 buflen += rqst->rq_tailsz;
248 __smb_send_rqst(struct TCP_Server_Info *server, int num_rqst,
249 struct smb_rqst *rqst)
254 unsigned int send_length = 0;
256 size_t total_len = 0, sent, size;
257 struct socket *ssocket = server->ssocket;
258 struct msghdr smb_msg;
260 __be32 rfc1002_marker;
262 if (cifs_rdma_enabled(server) && server->smbd_conn) {
263 rc = smbd_send(server->smbd_conn, rqst);
269 /* cork the socket */
270 kernel_setsockopt(ssocket, SOL_TCP, TCP_CORK,
271 (char *)&val, sizeof(val));
273 for (j = 0; j < num_rqst; j++)
274 send_length += smb2_rqst_len(&rqst[j], true);
275 rfc1002_marker = cpu_to_be32(send_length);
277 /* Generate a rfc1002 marker for SMB2+ */
278 if (server->vals->header_preamble_size == 0) {
280 .iov_base = &rfc1002_marker,
283 iov_iter_kvec(&smb_msg.msg_iter, WRITE | ITER_KVEC, &hiov,
285 rc = smb_send_kvec(server, &smb_msg, &sent);
293 cifs_dbg(FYI, "Sending smb: smb_len=%u\n", send_length);
295 for (j = 0; j < num_rqst; j++) {
296 iov = rqst[j].rq_iov;
297 n_vec = rqst[j].rq_nvec;
300 for (i = 0; i < n_vec; i++) {
301 dump_smb(iov[i].iov_base, iov[i].iov_len);
302 size += iov[i].iov_len;
305 iov_iter_kvec(&smb_msg.msg_iter, WRITE | ITER_KVEC,
308 rc = smb_send_kvec(server, &smb_msg, &sent);
314 /* now walk the page array and send each page in it */
315 for (i = 0; i < rqst[j].rq_npages; i++) {
318 bvec.bv_page = rqst[j].rq_pages[i];
319 rqst_page_get_length(&rqst[j], i, &bvec.bv_len,
322 iov_iter_bvec(&smb_msg.msg_iter, WRITE | ITER_BVEC,
323 &bvec, 1, bvec.bv_len);
324 rc = smb_send_kvec(server, &smb_msg, &sent);
335 kernel_setsockopt(ssocket, SOL_TCP, TCP_CORK,
336 (char *)&val, sizeof(val));
338 if ((total_len > 0) && (total_len != send_length)) {
339 cifs_dbg(FYI, "partial send (wanted=%u sent=%zu): terminating session\n",
340 send_length, total_len);
342 * If we have only sent part of an SMB then the next SMB could
343 * be taken as the remainder of this one. We need to kill the
344 * socket so the server throws away the partial SMB
346 server->tcpStatus = CifsNeedReconnect;
349 if (rc < 0 && rc != -EINTR)
350 cifs_dbg(VFS, "Error %d sending data on socket to server\n",
359 smb_send_rqst(struct TCP_Server_Info *server, struct smb_rqst *rqst, int flags)
361 struct smb_rqst cur_rqst;
364 if (!(flags & CIFS_TRANSFORM_REQ))
365 return __smb_send_rqst(server, 1, rqst);
367 if (!server->ops->init_transform_rq ||
368 !server->ops->free_transform_rq) {
369 cifs_dbg(VFS, "Encryption requested but transform callbacks are missed\n");
373 rc = server->ops->init_transform_rq(server, &cur_rqst, rqst);
377 rc = __smb_send_rqst(server, 1, &cur_rqst);
378 server->ops->free_transform_rq(&cur_rqst);
383 smb_send(struct TCP_Server_Info *server, struct smb_hdr *smb_buffer,
384 unsigned int smb_buf_length)
387 struct smb_rqst rqst = { .rq_iov = iov,
390 iov[0].iov_base = smb_buffer;
392 iov[1].iov_base = (char *)smb_buffer + 4;
393 iov[1].iov_len = smb_buf_length;
395 return __smb_send_rqst(server, 1, &rqst);
399 wait_for_free_credits(struct TCP_Server_Info *server, const int timeout,
404 spin_lock(&server->req_lock);
405 if (timeout == CIFS_ASYNC_OP) {
406 /* oplock breaks must not be held up */
409 spin_unlock(&server->req_lock);
415 spin_unlock(&server->req_lock);
416 cifs_num_waiters_inc(server);
417 rc = wait_event_killable(server->request_q,
418 has_credits(server, credits));
419 cifs_num_waiters_dec(server);
422 spin_lock(&server->req_lock);
424 if (server->tcpStatus == CifsExiting) {
425 spin_unlock(&server->req_lock);
430 * Can not count locking commands against total
431 * as they are allowed to block on server.
434 /* update # of requests on the wire to server */
435 if (timeout != CIFS_BLOCKING_OP) {
439 spin_unlock(&server->req_lock);
447 wait_for_free_request(struct TCP_Server_Info *server, const int timeout,
452 val = server->ops->get_credits_field(server, optype);
453 /* Since an echo is already inflight, no need to wait to send another */
454 if (*val <= 0 && optype == CIFS_ECHO_OP)
456 return wait_for_free_credits(server, timeout, val);
460 cifs_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
461 unsigned int *num, unsigned int *credits)
468 static int allocate_mid(struct cifs_ses *ses, struct smb_hdr *in_buf,
469 struct mid_q_entry **ppmidQ)
471 if (ses->server->tcpStatus == CifsExiting) {
475 if (ses->server->tcpStatus == CifsNeedReconnect) {
476 cifs_dbg(FYI, "tcp session dead - return to caller to retry\n");
480 if (ses->status == CifsNew) {
481 if ((in_buf->Command != SMB_COM_SESSION_SETUP_ANDX) &&
482 (in_buf->Command != SMB_COM_NEGOTIATE))
484 /* else ok - we are setting up session */
487 if (ses->status == CifsExiting) {
488 /* check if SMB session is bad because we are setting it up */
489 if (in_buf->Command != SMB_COM_LOGOFF_ANDX)
491 /* else ok - we are shutting down session */
494 *ppmidQ = AllocMidQEntry(in_buf, ses->server);
497 spin_lock(&GlobalMid_Lock);
498 list_add_tail(&(*ppmidQ)->qhead, &ses->server->pending_mid_q);
499 spin_unlock(&GlobalMid_Lock);
504 wait_for_response(struct TCP_Server_Info *server, struct mid_q_entry *midQ)
508 error = wait_event_freezekillable_unsafe(server->response_q,
509 midQ->mid_state != MID_REQUEST_SUBMITTED);
517 cifs_setup_async_request(struct TCP_Server_Info *server, struct smb_rqst *rqst)
520 struct smb_hdr *hdr = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
521 struct mid_q_entry *mid;
523 if (rqst->rq_iov[0].iov_len != 4 ||
524 rqst->rq_iov[0].iov_base + 4 != rqst->rq_iov[1].iov_base)
525 return ERR_PTR(-EIO);
527 /* enable signing if server requires it */
529 hdr->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
531 mid = AllocMidQEntry(hdr, server);
533 return ERR_PTR(-ENOMEM);
535 rc = cifs_sign_rqst(rqst, server, &mid->sequence_number);
537 DeleteMidQEntry(mid);
545 * Send a SMB request and set the callback function in the mid to handle
546 * the result. Caller is responsible for dealing with timeouts.
549 cifs_call_async(struct TCP_Server_Info *server, struct smb_rqst *rqst,
550 mid_receive_t *receive, mid_callback_t *callback,
551 mid_handle_t *handle, void *cbdata, const int flags)
553 int rc, timeout, optype;
554 struct mid_q_entry *mid;
555 unsigned int credits = 0;
557 timeout = flags & CIFS_TIMEOUT_MASK;
558 optype = flags & CIFS_OP_MASK;
560 if ((flags & CIFS_HAS_CREDITS) == 0) {
561 rc = wait_for_free_request(server, timeout, optype);
567 mutex_lock(&server->srv_mutex);
568 mid = server->ops->setup_async_request(server, rqst);
570 mutex_unlock(&server->srv_mutex);
571 add_credits_and_wake_if(server, credits, optype);
575 mid->receive = receive;
576 mid->callback = callback;
577 mid->callback_data = cbdata;
578 mid->handle = handle;
579 mid->mid_state = MID_REQUEST_SUBMITTED;
581 /* put it on the pending_mid_q */
582 spin_lock(&GlobalMid_Lock);
583 list_add_tail(&mid->qhead, &server->pending_mid_q);
584 spin_unlock(&GlobalMid_Lock);
587 * Need to store the time in mid before calling I/O. For call_async,
588 * I/O response may come back and free the mid entry on another thread.
590 cifs_save_when_sent(mid);
591 cifs_in_send_inc(server);
592 rc = smb_send_rqst(server, rqst, flags);
593 cifs_in_send_dec(server);
596 server->sequence_number -= 2;
597 cifs_delete_mid(mid);
600 mutex_unlock(&server->srv_mutex);
605 add_credits_and_wake_if(server, credits, optype);
611 * Send an SMB Request. No response info (other than return code)
612 * needs to be parsed.
614 * flags indicate the type of request buffer and how long to wait
615 * and whether to log NT STATUS code (error) before mapping it to POSIX error
619 SendReceiveNoRsp(const unsigned int xid, struct cifs_ses *ses,
620 char *in_buf, int flags)
627 iov[0].iov_base = in_buf;
628 iov[0].iov_len = get_rfc1002_length(in_buf) + 4;
629 flags |= CIFS_NO_RESP;
630 rc = SendReceive2(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
631 cifs_dbg(NOISY, "SendRcvNoRsp flags %d rc %d\n", flags, rc);
637 cifs_sync_mid_result(struct mid_q_entry *mid, struct TCP_Server_Info *server)
641 cifs_dbg(FYI, "%s: cmd=%d mid=%llu state=%d\n",
642 __func__, le16_to_cpu(mid->command), mid->mid, mid->mid_state);
644 spin_lock(&GlobalMid_Lock);
645 switch (mid->mid_state) {
646 case MID_RESPONSE_RECEIVED:
647 spin_unlock(&GlobalMid_Lock);
649 case MID_RETRY_NEEDED:
652 case MID_RESPONSE_MALFORMED:
659 list_del_init(&mid->qhead);
660 cifs_dbg(VFS, "%s: invalid mid state mid=%llu state=%d\n",
661 __func__, mid->mid, mid->mid_state);
664 spin_unlock(&GlobalMid_Lock);
666 DeleteMidQEntry(mid);
671 send_cancel(struct TCP_Server_Info *server, struct smb_rqst *rqst,
672 struct mid_q_entry *mid)
674 return server->ops->send_cancel ?
675 server->ops->send_cancel(server, rqst, mid) : 0;
679 cifs_check_receive(struct mid_q_entry *mid, struct TCP_Server_Info *server,
682 unsigned int len = get_rfc1002_length(mid->resp_buf) + 4;
684 dump_smb(mid->resp_buf, min_t(u32, 92, len));
686 /* convert the length into a more usable form */
690 struct smb_rqst rqst = { .rq_iov = iov,
693 iov[0].iov_base = mid->resp_buf;
695 iov[1].iov_base = (char *)mid->resp_buf + 4;
696 iov[1].iov_len = len - 4;
697 /* FIXME: add code to kill session */
698 rc = cifs_verify_signature(&rqst, server,
699 mid->sequence_number);
701 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
705 /* BB special case reconnect tid and uid here? */
706 return map_smb_to_linux_error(mid->resp_buf, log_error);
710 cifs_setup_request(struct cifs_ses *ses, struct smb_rqst *rqst)
713 struct smb_hdr *hdr = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
714 struct mid_q_entry *mid;
716 if (rqst->rq_iov[0].iov_len != 4 ||
717 rqst->rq_iov[0].iov_base + 4 != rqst->rq_iov[1].iov_base)
718 return ERR_PTR(-EIO);
720 rc = allocate_mid(ses, hdr, &mid);
723 rc = cifs_sign_rqst(rqst, ses->server, &mid->sequence_number);
725 cifs_delete_mid(mid);
732 cifs_send_recv(const unsigned int xid, struct cifs_ses *ses,
733 struct smb_rqst *rqst, int *resp_buf_type, const int flags,
734 struct kvec *resp_iov)
738 struct mid_q_entry *midQ;
739 unsigned int credits = 1;
742 timeout = flags & CIFS_TIMEOUT_MASK;
743 optype = flags & CIFS_OP_MASK;
745 *resp_buf_type = CIFS_NO_BUFFER; /* no response buf yet */
747 if ((ses == NULL) || (ses->server == NULL)) {
748 cifs_dbg(VFS, "Null session\n");
752 if (ses->server->tcpStatus == CifsExiting)
756 * Ensure that we do not send more than 50 overlapping requests
757 * to the same server. We may make this configurable later or
760 rc = wait_for_free_request(ses->server, timeout, optype);
765 * Make sure that we sign in the same order that we send on this socket
766 * and avoid races inside tcp sendmsg code that could cause corruption
770 mutex_lock(&ses->server->srv_mutex);
772 midQ = ses->server->ops->setup_request(ses, rqst);
774 mutex_unlock(&ses->server->srv_mutex);
775 /* Update # of requests on wire to server */
776 add_credits(ses->server, 1, optype);
777 return PTR_ERR(midQ);
780 midQ->mid_state = MID_REQUEST_SUBMITTED;
781 cifs_in_send_inc(ses->server);
782 rc = smb_send_rqst(ses->server, rqst, flags);
783 cifs_in_send_dec(ses->server);
784 cifs_save_when_sent(midQ);
787 ses->server->sequence_number -= 2;
788 mutex_unlock(&ses->server->srv_mutex);
793 #ifdef CONFIG_CIFS_SMB311
794 if ((ses->status == CifsNew) || (optype & CIFS_NEG_OP))
795 smb311_update_preauth_hash(ses, rqst->rq_iov,
799 if (timeout == CIFS_ASYNC_OP)
802 rc = wait_for_response(ses->server, midQ);
804 cifs_dbg(FYI, "Cancelling wait for mid %llu\n", midQ->mid);
805 send_cancel(ses->server, rqst, midQ);
806 spin_lock(&GlobalMid_Lock);
807 if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
808 midQ->mid_flags |= MID_WAIT_CANCELLED;
809 midQ->callback = DeleteMidQEntry;
810 spin_unlock(&GlobalMid_Lock);
811 add_credits(ses->server, 1, optype);
814 spin_unlock(&GlobalMid_Lock);
817 rc = cifs_sync_mid_result(midQ, ses->server);
819 add_credits(ses->server, 1, optype);
823 if (!midQ->resp_buf || midQ->mid_state != MID_RESPONSE_RECEIVED) {
825 cifs_dbg(FYI, "Bad MID state?\n");
829 buf = (char *)midQ->resp_buf;
830 resp_iov->iov_base = buf;
831 resp_iov->iov_len = midQ->resp_buf_size +
832 ses->server->vals->header_preamble_size;
834 *resp_buf_type = CIFS_LARGE_BUFFER;
836 *resp_buf_type = CIFS_SMALL_BUFFER;
838 #ifdef CONFIG_CIFS_SMB311
839 if ((ses->status == CifsNew) || (optype & CIFS_NEG_OP)) {
841 .iov_base = resp_iov->iov_base,
842 .iov_len = resp_iov->iov_len
844 smb311_update_preauth_hash(ses, &iov, 1);
848 credits = ses->server->ops->get_credits(midQ);
850 rc = ses->server->ops->check_receive(midQ, ses->server,
851 flags & CIFS_LOG_ERROR);
853 /* mark it so buf will not be freed by cifs_delete_mid */
854 if ((flags & CIFS_NO_RESP) == 0)
855 midQ->resp_buf = NULL;
857 cifs_delete_mid(midQ);
858 add_credits(ses->server, credits, optype);
864 SendReceive2(const unsigned int xid, struct cifs_ses *ses,
865 struct kvec *iov, int n_vec, int *resp_buf_type /* ret */,
866 const int flags, struct kvec *resp_iov)
868 struct smb_rqst rqst;
869 struct kvec s_iov[CIFS_MAX_IOV_SIZE], *new_iov;
872 if (n_vec + 1 > CIFS_MAX_IOV_SIZE) {
873 new_iov = kmalloc_array(n_vec + 1, sizeof(struct kvec),
876 /* otherwise cifs_send_recv below sets resp_buf_type */
877 *resp_buf_type = CIFS_NO_BUFFER;
883 /* 1st iov is a RFC1001 length followed by the rest of the packet */
884 memcpy(new_iov + 1, iov, (sizeof(struct kvec) * n_vec));
886 new_iov[0].iov_base = new_iov[1].iov_base;
887 new_iov[0].iov_len = 4;
888 new_iov[1].iov_base += 4;
889 new_iov[1].iov_len -= 4;
891 memset(&rqst, 0, sizeof(struct smb_rqst));
892 rqst.rq_iov = new_iov;
893 rqst.rq_nvec = n_vec + 1;
895 rc = cifs_send_recv(xid, ses, &rqst, resp_buf_type, flags, resp_iov);
896 if (n_vec + 1 > CIFS_MAX_IOV_SIZE)
902 SendReceive(const unsigned int xid, struct cifs_ses *ses,
903 struct smb_hdr *in_buf, struct smb_hdr *out_buf,
904 int *pbytes_returned, const int timeout)
907 struct mid_q_entry *midQ;
908 unsigned int len = be32_to_cpu(in_buf->smb_buf_length);
909 struct kvec iov = { .iov_base = in_buf, .iov_len = len };
910 struct smb_rqst rqst = { .rq_iov = &iov, .rq_nvec = 1 };
913 cifs_dbg(VFS, "Null smb session\n");
916 if (ses->server == NULL) {
917 cifs_dbg(VFS, "Null tcp session\n");
921 if (ses->server->tcpStatus == CifsExiting)
924 /* Ensure that we do not send more than 50 overlapping requests
925 to the same server. We may make this configurable later or
928 if (len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
929 cifs_dbg(VFS, "Illegal length, greater than maximum frame, %d\n",
934 rc = wait_for_free_request(ses->server, timeout, 0);
938 /* make sure that we sign in the same order that we send on this socket
939 and avoid races inside tcp sendmsg code that could cause corruption
942 mutex_lock(&ses->server->srv_mutex);
944 rc = allocate_mid(ses, in_buf, &midQ);
946 mutex_unlock(&ses->server->srv_mutex);
947 /* Update # of requests on wire to server */
948 add_credits(ses->server, 1, 0);
952 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
954 mutex_unlock(&ses->server->srv_mutex);
958 midQ->mid_state = MID_REQUEST_SUBMITTED;
960 cifs_in_send_inc(ses->server);
961 rc = smb_send(ses->server, in_buf, len);
962 cifs_in_send_dec(ses->server);
963 cifs_save_when_sent(midQ);
966 ses->server->sequence_number -= 2;
968 mutex_unlock(&ses->server->srv_mutex);
973 if (timeout == CIFS_ASYNC_OP)
976 rc = wait_for_response(ses->server, midQ);
978 send_cancel(ses->server, &rqst, midQ);
979 spin_lock(&GlobalMid_Lock);
980 if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
981 /* no longer considered to be "in-flight" */
982 midQ->callback = DeleteMidQEntry;
983 spin_unlock(&GlobalMid_Lock);
984 add_credits(ses->server, 1, 0);
987 spin_unlock(&GlobalMid_Lock);
990 rc = cifs_sync_mid_result(midQ, ses->server);
992 add_credits(ses->server, 1, 0);
996 if (!midQ->resp_buf || !out_buf ||
997 midQ->mid_state != MID_RESPONSE_RECEIVED) {
999 cifs_dbg(VFS, "Bad MID state?\n");
1003 *pbytes_returned = get_rfc1002_length(midQ->resp_buf);
1004 memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
1005 rc = cifs_check_receive(midQ, ses->server, 0);
1007 cifs_delete_mid(midQ);
1008 add_credits(ses->server, 1, 0);
1013 /* We send a LOCKINGX_CANCEL_LOCK to cause the Windows
1014 blocking lock to return. */
1017 send_lock_cancel(const unsigned int xid, struct cifs_tcon *tcon,
1018 struct smb_hdr *in_buf,
1019 struct smb_hdr *out_buf)
1022 struct cifs_ses *ses = tcon->ses;
1023 LOCK_REQ *pSMB = (LOCK_REQ *)in_buf;
1025 /* We just modify the current in_buf to change
1026 the type of lock from LOCKING_ANDX_SHARED_LOCK
1027 or LOCKING_ANDX_EXCLUSIVE_LOCK to
1028 LOCKING_ANDX_CANCEL_LOCK. */
1030 pSMB->LockType = LOCKING_ANDX_CANCEL_LOCK|LOCKING_ANDX_LARGE_FILES;
1032 pSMB->hdr.Mid = get_next_mid(ses->server);
1034 return SendReceive(xid, ses, in_buf, out_buf,
1035 &bytes_returned, 0);
1039 SendReceiveBlockingLock(const unsigned int xid, struct cifs_tcon *tcon,
1040 struct smb_hdr *in_buf, struct smb_hdr *out_buf,
1041 int *pbytes_returned)
1045 struct mid_q_entry *midQ;
1046 struct cifs_ses *ses;
1047 unsigned int len = be32_to_cpu(in_buf->smb_buf_length);
1048 struct kvec iov = { .iov_base = in_buf, .iov_len = len };
1049 struct smb_rqst rqst = { .rq_iov = &iov, .rq_nvec = 1 };
1051 if (tcon == NULL || tcon->ses == NULL) {
1052 cifs_dbg(VFS, "Null smb session\n");
1057 if (ses->server == NULL) {
1058 cifs_dbg(VFS, "Null tcp session\n");
1062 if (ses->server->tcpStatus == CifsExiting)
1065 /* Ensure that we do not send more than 50 overlapping requests
1066 to the same server. We may make this configurable later or
1069 if (len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
1070 cifs_dbg(VFS, "Illegal length, greater than maximum frame, %d\n",
1075 rc = wait_for_free_request(ses->server, CIFS_BLOCKING_OP, 0);
1079 /* make sure that we sign in the same order that we send on this socket
1080 and avoid races inside tcp sendmsg code that could cause corruption
1083 mutex_lock(&ses->server->srv_mutex);
1085 rc = allocate_mid(ses, in_buf, &midQ);
1087 mutex_unlock(&ses->server->srv_mutex);
1091 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
1093 cifs_delete_mid(midQ);
1094 mutex_unlock(&ses->server->srv_mutex);
1098 midQ->mid_state = MID_REQUEST_SUBMITTED;
1099 cifs_in_send_inc(ses->server);
1100 rc = smb_send(ses->server, in_buf, len);
1101 cifs_in_send_dec(ses->server);
1102 cifs_save_when_sent(midQ);
1105 ses->server->sequence_number -= 2;
1107 mutex_unlock(&ses->server->srv_mutex);
1110 cifs_delete_mid(midQ);
1114 /* Wait for a reply - allow signals to interrupt. */
1115 rc = wait_event_interruptible(ses->server->response_q,
1116 (!(midQ->mid_state == MID_REQUEST_SUBMITTED)) ||
1117 ((ses->server->tcpStatus != CifsGood) &&
1118 (ses->server->tcpStatus != CifsNew)));
1120 /* Were we interrupted by a signal ? */
1121 if ((rc == -ERESTARTSYS) &&
1122 (midQ->mid_state == MID_REQUEST_SUBMITTED) &&
1123 ((ses->server->tcpStatus == CifsGood) ||
1124 (ses->server->tcpStatus == CifsNew))) {
1126 if (in_buf->Command == SMB_COM_TRANSACTION2) {
1127 /* POSIX lock. We send a NT_CANCEL SMB to cause the
1128 blocking lock to return. */
1129 rc = send_cancel(ses->server, &rqst, midQ);
1131 cifs_delete_mid(midQ);
1135 /* Windows lock. We send a LOCKINGX_CANCEL_LOCK
1136 to cause the blocking lock to return. */
1138 rc = send_lock_cancel(xid, tcon, in_buf, out_buf);
1140 /* If we get -ENOLCK back the lock may have
1141 already been removed. Don't exit in this case. */
1142 if (rc && rc != -ENOLCK) {
1143 cifs_delete_mid(midQ);
1148 rc = wait_for_response(ses->server, midQ);
1150 send_cancel(ses->server, &rqst, midQ);
1151 spin_lock(&GlobalMid_Lock);
1152 if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
1153 /* no longer considered to be "in-flight" */
1154 midQ->callback = DeleteMidQEntry;
1155 spin_unlock(&GlobalMid_Lock);
1158 spin_unlock(&GlobalMid_Lock);
1161 /* We got the response - restart system call. */
1165 rc = cifs_sync_mid_result(midQ, ses->server);
1169 /* rcvd frame is ok */
1170 if (out_buf == NULL || midQ->mid_state != MID_RESPONSE_RECEIVED) {
1172 cifs_dbg(VFS, "Bad MID state?\n");
1176 *pbytes_returned = get_rfc1002_length(midQ->resp_buf);
1177 memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
1178 rc = cifs_check_receive(midQ, ses->server, 0);
1180 cifs_delete_mid(midQ);
1181 if (rstart && rc == -EACCES)
1182 return -ERESTARTSYS;