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 smb_buf_length = get_rfc1002_length(iov[0].iov_base);
245 unsigned long send_length;
247 size_t total_len = 0, sent, size;
248 struct socket *ssocket = server->ssocket;
249 struct msghdr smb_msg;
251 if (cifs_rdma_enabled(server) && server->smbd_conn) {
252 rc = smbd_send(server->smbd_conn, rqst);
258 /* sanity check send length */
259 send_length = rqst_len(rqst);
260 if (send_length != smb_buf_length + 4) {
261 WARN(1, "Send length mismatch(send_length=%lu smb_buf_length=%u)\n",
262 send_length, smb_buf_length);
269 cifs_dbg(FYI, "Sending smb: smb_len=%u\n", smb_buf_length);
270 dump_smb(iov[0].iov_base, iov[0].iov_len);
271 dump_smb(iov[1].iov_base, iov[1].iov_len);
273 /* cork the socket */
274 kernel_setsockopt(ssocket, SOL_TCP, TCP_CORK,
275 (char *)&val, sizeof(val));
278 for (i = 0; i < n_vec; i++)
279 size += iov[i].iov_len;
281 iov_iter_kvec(&smb_msg.msg_iter, WRITE | ITER_KVEC, iov, n_vec, size);
283 rc = smb_send_kvec(server, &smb_msg, &sent);
289 /* now walk the page array and send each page in it */
290 for (i = 0; i < rqst->rq_npages; i++) {
293 bvec.bv_page = rqst->rq_pages[i];
294 rqst_page_get_length(rqst, i, &bvec.bv_len, &bvec.bv_offset);
296 iov_iter_bvec(&smb_msg.msg_iter, WRITE | ITER_BVEC,
297 &bvec, 1, bvec.bv_len);
298 rc = smb_send_kvec(server, &smb_msg, &sent);
308 kernel_setsockopt(ssocket, SOL_TCP, TCP_CORK,
309 (char *)&val, sizeof(val));
311 if ((total_len > 0) && (total_len != smb_buf_length + 4)) {
312 cifs_dbg(FYI, "partial send (wanted=%u sent=%zu): terminating session\n",
313 smb_buf_length + 4, total_len);
315 * If we have only sent part of an SMB then the next SMB could
316 * be taken as the remainder of this one. We need to kill the
317 * socket so the server throws away the partial SMB
319 server->tcpStatus = CifsNeedReconnect;
322 if (rc < 0 && rc != -EINTR)
323 cifs_dbg(VFS, "Error %d sending data on socket to server\n",
332 smb_send_rqst(struct TCP_Server_Info *server, struct smb_rqst *rqst, int flags)
334 struct smb_rqst cur_rqst;
337 if (!(flags & CIFS_TRANSFORM_REQ))
338 return __smb_send_rqst(server, rqst);
340 if (!server->ops->init_transform_rq ||
341 !server->ops->free_transform_rq) {
342 cifs_dbg(VFS, "Encryption requested but transform callbacks are missed\n");
346 rc = server->ops->init_transform_rq(server, &cur_rqst, rqst);
350 rc = __smb_send_rqst(server, &cur_rqst);
351 server->ops->free_transform_rq(&cur_rqst);
356 smb_send(struct TCP_Server_Info *server, struct smb_hdr *smb_buffer,
357 unsigned int smb_buf_length)
360 struct smb_rqst rqst = { .rq_iov = iov,
363 iov[0].iov_base = smb_buffer;
365 iov[1].iov_base = (char *)smb_buffer + 4;
366 iov[1].iov_len = smb_buf_length;
368 return __smb_send_rqst(server, &rqst);
372 wait_for_free_credits(struct TCP_Server_Info *server, const int timeout,
377 spin_lock(&server->req_lock);
378 if (timeout == CIFS_ASYNC_OP) {
379 /* oplock breaks must not be held up */
382 spin_unlock(&server->req_lock);
388 spin_unlock(&server->req_lock);
389 cifs_num_waiters_inc(server);
390 rc = wait_event_killable(server->request_q,
391 has_credits(server, credits));
392 cifs_num_waiters_dec(server);
395 spin_lock(&server->req_lock);
397 if (server->tcpStatus == CifsExiting) {
398 spin_unlock(&server->req_lock);
403 * Can not count locking commands against total
404 * as they are allowed to block on server.
407 /* update # of requests on the wire to server */
408 if (timeout != CIFS_BLOCKING_OP) {
412 spin_unlock(&server->req_lock);
420 wait_for_free_request(struct TCP_Server_Info *server, const int timeout,
425 val = server->ops->get_credits_field(server, optype);
426 /* Since an echo is already inflight, no need to wait to send another */
427 if (*val <= 0 && optype == CIFS_ECHO_OP)
429 return wait_for_free_credits(server, timeout, val);
433 cifs_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
434 unsigned int *num, unsigned int *credits)
441 static int allocate_mid(struct cifs_ses *ses, struct smb_hdr *in_buf,
442 struct mid_q_entry **ppmidQ)
444 if (ses->server->tcpStatus == CifsExiting) {
448 if (ses->server->tcpStatus == CifsNeedReconnect) {
449 cifs_dbg(FYI, "tcp session dead - return to caller to retry\n");
453 if (ses->status == CifsNew) {
454 if ((in_buf->Command != SMB_COM_SESSION_SETUP_ANDX) &&
455 (in_buf->Command != SMB_COM_NEGOTIATE))
457 /* else ok - we are setting up session */
460 if (ses->status == CifsExiting) {
461 /* check if SMB session is bad because we are setting it up */
462 if (in_buf->Command != SMB_COM_LOGOFF_ANDX)
464 /* else ok - we are shutting down session */
467 *ppmidQ = AllocMidQEntry(in_buf, ses->server);
470 spin_lock(&GlobalMid_Lock);
471 list_add_tail(&(*ppmidQ)->qhead, &ses->server->pending_mid_q);
472 spin_unlock(&GlobalMid_Lock);
477 wait_for_response(struct TCP_Server_Info *server, struct mid_q_entry *midQ)
481 error = wait_event_freezekillable_unsafe(server->response_q,
482 midQ->mid_state != MID_REQUEST_SUBMITTED);
490 cifs_setup_async_request(struct TCP_Server_Info *server, struct smb_rqst *rqst)
493 struct smb_hdr *hdr = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
494 struct mid_q_entry *mid;
496 if (rqst->rq_iov[0].iov_len != 4 ||
497 rqst->rq_iov[0].iov_base + 4 != rqst->rq_iov[1].iov_base)
498 return ERR_PTR(-EIO);
500 /* enable signing if server requires it */
502 hdr->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
504 mid = AllocMidQEntry(hdr, server);
506 return ERR_PTR(-ENOMEM);
508 rc = cifs_sign_rqst(rqst, server, &mid->sequence_number);
510 DeleteMidQEntry(mid);
518 * Send a SMB request and set the callback function in the mid to handle
519 * the result. Caller is responsible for dealing with timeouts.
522 cifs_call_async(struct TCP_Server_Info *server, struct smb_rqst *rqst,
523 mid_receive_t *receive, mid_callback_t *callback,
524 mid_handle_t *handle, void *cbdata, const int flags)
526 int rc, timeout, optype;
527 struct mid_q_entry *mid;
528 unsigned int credits = 0;
530 timeout = flags & CIFS_TIMEOUT_MASK;
531 optype = flags & CIFS_OP_MASK;
533 if ((flags & CIFS_HAS_CREDITS) == 0) {
534 rc = wait_for_free_request(server, timeout, optype);
540 mutex_lock(&server->srv_mutex);
541 mid = server->ops->setup_async_request(server, rqst);
543 mutex_unlock(&server->srv_mutex);
544 add_credits_and_wake_if(server, credits, optype);
548 mid->receive = receive;
549 mid->callback = callback;
550 mid->callback_data = cbdata;
551 mid->handle = handle;
552 mid->mid_state = MID_REQUEST_SUBMITTED;
554 /* put it on the pending_mid_q */
555 spin_lock(&GlobalMid_Lock);
556 list_add_tail(&mid->qhead, &server->pending_mid_q);
557 spin_unlock(&GlobalMid_Lock);
560 * Need to store the time in mid before calling I/O. For call_async,
561 * I/O response may come back and free the mid entry on another thread.
563 cifs_save_when_sent(mid);
564 cifs_in_send_inc(server);
565 rc = smb_send_rqst(server, rqst, flags);
566 cifs_in_send_dec(server);
569 server->sequence_number -= 2;
570 cifs_delete_mid(mid);
573 mutex_unlock(&server->srv_mutex);
578 add_credits_and_wake_if(server, credits, optype);
584 * Send an SMB Request. No response info (other than return code)
585 * needs to be parsed.
587 * flags indicate the type of request buffer and how long to wait
588 * and whether to log NT STATUS code (error) before mapping it to POSIX error
592 SendReceiveNoRsp(const unsigned int xid, struct cifs_ses *ses,
593 char *in_buf, int flags)
600 iov[0].iov_base = in_buf;
601 iov[0].iov_len = get_rfc1002_length(in_buf) + 4;
602 flags |= CIFS_NO_RESP;
603 rc = SendReceive2(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
604 cifs_dbg(NOISY, "SendRcvNoRsp flags %d rc %d\n", flags, rc);
610 cifs_sync_mid_result(struct mid_q_entry *mid, struct TCP_Server_Info *server)
614 cifs_dbg(FYI, "%s: cmd=%d mid=%llu state=%d\n",
615 __func__, le16_to_cpu(mid->command), mid->mid, mid->mid_state);
617 spin_lock(&GlobalMid_Lock);
618 switch (mid->mid_state) {
619 case MID_RESPONSE_RECEIVED:
620 spin_unlock(&GlobalMid_Lock);
622 case MID_RETRY_NEEDED:
625 case MID_RESPONSE_MALFORMED:
632 list_del_init(&mid->qhead);
633 cifs_dbg(VFS, "%s: invalid mid state mid=%llu state=%d\n",
634 __func__, mid->mid, mid->mid_state);
637 spin_unlock(&GlobalMid_Lock);
639 DeleteMidQEntry(mid);
644 send_cancel(struct TCP_Server_Info *server, struct smb_rqst *rqst,
645 struct mid_q_entry *mid)
647 return server->ops->send_cancel ?
648 server->ops->send_cancel(server, rqst, mid) : 0;
652 cifs_check_receive(struct mid_q_entry *mid, struct TCP_Server_Info *server,
655 unsigned int len = get_rfc1002_length(mid->resp_buf) + 4;
657 dump_smb(mid->resp_buf, min_t(u32, 92, len));
659 /* convert the length into a more usable form */
663 struct smb_rqst rqst = { .rq_iov = iov,
666 iov[0].iov_base = mid->resp_buf;
668 iov[1].iov_base = (char *)mid->resp_buf + 4;
669 iov[1].iov_len = len - 4;
670 /* FIXME: add code to kill session */
671 rc = cifs_verify_signature(&rqst, server,
672 mid->sequence_number);
674 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
678 /* BB special case reconnect tid and uid here? */
679 return map_smb_to_linux_error(mid->resp_buf, log_error);
683 cifs_setup_request(struct cifs_ses *ses, struct smb_rqst *rqst)
686 struct smb_hdr *hdr = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
687 struct mid_q_entry *mid;
689 if (rqst->rq_iov[0].iov_len != 4 ||
690 rqst->rq_iov[0].iov_base + 4 != rqst->rq_iov[1].iov_base)
691 return ERR_PTR(-EIO);
693 rc = allocate_mid(ses, hdr, &mid);
696 rc = cifs_sign_rqst(rqst, ses->server, &mid->sequence_number);
698 cifs_delete_mid(mid);
705 cifs_send_recv(const unsigned int xid, struct cifs_ses *ses,
706 struct smb_rqst *rqst, int *resp_buf_type, const int flags,
707 struct kvec *resp_iov)
711 struct mid_q_entry *midQ;
712 unsigned int credits = 1;
715 timeout = flags & CIFS_TIMEOUT_MASK;
716 optype = flags & CIFS_OP_MASK;
718 *resp_buf_type = CIFS_NO_BUFFER; /* no response buf yet */
720 if ((ses == NULL) || (ses->server == NULL)) {
721 cifs_dbg(VFS, "Null session\n");
725 if (ses->server->tcpStatus == CifsExiting)
729 * Ensure that we do not send more than 50 overlapping requests
730 * to the same server. We may make this configurable later or
734 rc = wait_for_free_request(ses->server, timeout, optype);
739 * Make sure that we sign in the same order that we send on this socket
740 * and avoid races inside tcp sendmsg code that could cause corruption
744 mutex_lock(&ses->server->srv_mutex);
746 midQ = ses->server->ops->setup_request(ses, rqst);
748 mutex_unlock(&ses->server->srv_mutex);
749 /* Update # of requests on wire to server */
750 add_credits(ses->server, 1, optype);
751 return PTR_ERR(midQ);
754 midQ->mid_state = MID_REQUEST_SUBMITTED;
755 cifs_in_send_inc(ses->server);
756 rc = smb_send_rqst(ses->server, rqst, flags);
757 cifs_in_send_dec(ses->server);
758 cifs_save_when_sent(midQ);
761 ses->server->sequence_number -= 2;
762 mutex_unlock(&ses->server->srv_mutex);
767 #ifdef CONFIG_CIFS_SMB311
768 if ((ses->status == CifsNew) || (optype & CIFS_NEG_OP))
769 smb311_update_preauth_hash(ses, rqst->rq_iov+1,
773 if (timeout == CIFS_ASYNC_OP)
776 rc = wait_for_response(ses->server, midQ);
778 cifs_dbg(FYI, "Cancelling wait for mid %llu\n", midQ->mid);
779 send_cancel(ses->server, rqst, midQ);
780 spin_lock(&GlobalMid_Lock);
781 if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
782 midQ->mid_flags |= MID_WAIT_CANCELLED;
783 midQ->callback = DeleteMidQEntry;
784 spin_unlock(&GlobalMid_Lock);
785 add_credits(ses->server, 1, optype);
788 spin_unlock(&GlobalMid_Lock);
791 rc = cifs_sync_mid_result(midQ, ses->server);
793 add_credits(ses->server, 1, optype);
797 if (!midQ->resp_buf || midQ->mid_state != MID_RESPONSE_RECEIVED) {
799 cifs_dbg(FYI, "Bad MID state?\n");
803 buf = (char *)midQ->resp_buf;
804 resp_iov->iov_base = buf;
805 resp_iov->iov_len = midQ->resp_buf_size +
806 ses->server->vals->header_preamble_size;
808 *resp_buf_type = CIFS_LARGE_BUFFER;
810 *resp_buf_type = CIFS_SMALL_BUFFER;
812 #ifdef CONFIG_CIFS_SMB311
813 if ((ses->status == CifsNew) || (optype & CIFS_NEG_OP)) {
816 .iov_len = midQ->resp_buf_size
818 smb311_update_preauth_hash(ses, &iov, 1);
822 credits = ses->server->ops->get_credits(midQ);
824 rc = ses->server->ops->check_receive(midQ, ses->server,
825 flags & CIFS_LOG_ERROR);
827 /* mark it so buf will not be freed by cifs_delete_mid */
828 if ((flags & CIFS_NO_RESP) == 0)
829 midQ->resp_buf = NULL;
831 cifs_delete_mid(midQ);
832 add_credits(ses->server, credits, optype);
838 SendReceive2(const unsigned int xid, struct cifs_ses *ses,
839 struct kvec *iov, int n_vec, int *resp_buf_type /* ret */,
840 const int flags, struct kvec *resp_iov)
842 struct smb_rqst rqst;
843 struct kvec s_iov[CIFS_MAX_IOV_SIZE], *new_iov;
846 if (n_vec + 1 > CIFS_MAX_IOV_SIZE) {
847 new_iov = kmalloc_array(n_vec + 1, sizeof(struct kvec),
850 /* otherwise cifs_send_recv below sets resp_buf_type */
851 *resp_buf_type = CIFS_NO_BUFFER;
857 /* 1st iov is a RFC1001 length followed by the rest of the packet */
858 memcpy(new_iov + 1, iov, (sizeof(struct kvec) * n_vec));
860 new_iov[0].iov_base = new_iov[1].iov_base;
861 new_iov[0].iov_len = 4;
862 new_iov[1].iov_base += 4;
863 new_iov[1].iov_len -= 4;
865 memset(&rqst, 0, sizeof(struct smb_rqst));
866 rqst.rq_iov = new_iov;
867 rqst.rq_nvec = n_vec + 1;
869 rc = cifs_send_recv(xid, ses, &rqst, resp_buf_type, flags, resp_iov);
870 if (n_vec + 1 > CIFS_MAX_IOV_SIZE)
875 /* Like SendReceive2 but iov[0] does not contain an rfc1002 header */
877 smb2_send_recv(const unsigned int xid, struct cifs_ses *ses,
878 struct kvec *iov, int n_vec, int *resp_buf_type /* ret */,
879 const int flags, struct kvec *resp_iov)
881 struct smb_rqst rqst;
882 struct kvec s_iov[CIFS_MAX_IOV_SIZE], *new_iov;
886 __be32 rfc1002_marker;
888 if (n_vec + 1 > CIFS_MAX_IOV_SIZE) {
889 new_iov = kmalloc_array(n_vec + 1, sizeof(struct kvec),
896 /* 1st iov is an RFC1002 Session Message length */
897 memcpy(new_iov + 1, iov, (sizeof(struct kvec) * n_vec));
900 for (i = 1; i < n_vec + 1; i++)
901 count += new_iov[i].iov_len;
903 rfc1002_marker = cpu_to_be32(count);
905 new_iov[0].iov_base = &rfc1002_marker;
906 new_iov[0].iov_len = 4;
908 memset(&rqst, 0, sizeof(struct smb_rqst));
909 rqst.rq_iov = new_iov;
910 rqst.rq_nvec = n_vec + 1;
912 rc = cifs_send_recv(xid, ses, &rqst, resp_buf_type, flags, resp_iov);
913 if (n_vec + 1 > CIFS_MAX_IOV_SIZE)
919 SendReceive(const unsigned int xid, struct cifs_ses *ses,
920 struct smb_hdr *in_buf, struct smb_hdr *out_buf,
921 int *pbytes_returned, const int timeout)
924 struct mid_q_entry *midQ;
925 unsigned int len = be32_to_cpu(in_buf->smb_buf_length);
926 struct kvec iov = { .iov_base = in_buf, .iov_len = len };
927 struct smb_rqst rqst = { .rq_iov = &iov, .rq_nvec = 1 };
930 cifs_dbg(VFS, "Null smb session\n");
933 if (ses->server == NULL) {
934 cifs_dbg(VFS, "Null tcp session\n");
938 if (ses->server->tcpStatus == CifsExiting)
941 /* Ensure that we do not send more than 50 overlapping requests
942 to the same server. We may make this configurable later or
945 if (len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
946 cifs_dbg(VFS, "Illegal length, greater than maximum frame, %d\n",
951 rc = wait_for_free_request(ses->server, timeout, 0);
955 /* make sure that we sign in the same order that we send on this socket
956 and avoid races inside tcp sendmsg code that could cause corruption
959 mutex_lock(&ses->server->srv_mutex);
961 rc = allocate_mid(ses, in_buf, &midQ);
963 mutex_unlock(&ses->server->srv_mutex);
964 /* Update # of requests on wire to server */
965 add_credits(ses->server, 1, 0);
969 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
971 mutex_unlock(&ses->server->srv_mutex);
975 midQ->mid_state = MID_REQUEST_SUBMITTED;
977 cifs_in_send_inc(ses->server);
978 rc = smb_send(ses->server, in_buf, len);
979 cifs_in_send_dec(ses->server);
980 cifs_save_when_sent(midQ);
983 ses->server->sequence_number -= 2;
985 mutex_unlock(&ses->server->srv_mutex);
990 if (timeout == CIFS_ASYNC_OP)
993 rc = wait_for_response(ses->server, midQ);
995 send_cancel(ses->server, &rqst, midQ);
996 spin_lock(&GlobalMid_Lock);
997 if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
998 /* no longer considered to be "in-flight" */
999 midQ->callback = DeleteMidQEntry;
1000 spin_unlock(&GlobalMid_Lock);
1001 add_credits(ses->server, 1, 0);
1004 spin_unlock(&GlobalMid_Lock);
1007 rc = cifs_sync_mid_result(midQ, ses->server);
1009 add_credits(ses->server, 1, 0);
1013 if (!midQ->resp_buf || !out_buf ||
1014 midQ->mid_state != MID_RESPONSE_RECEIVED) {
1016 cifs_dbg(VFS, "Bad MID state?\n");
1020 *pbytes_returned = get_rfc1002_length(midQ->resp_buf);
1021 memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
1022 rc = cifs_check_receive(midQ, ses->server, 0);
1024 cifs_delete_mid(midQ);
1025 add_credits(ses->server, 1, 0);
1030 /* We send a LOCKINGX_CANCEL_LOCK to cause the Windows
1031 blocking lock to return. */
1034 send_lock_cancel(const unsigned int xid, struct cifs_tcon *tcon,
1035 struct smb_hdr *in_buf,
1036 struct smb_hdr *out_buf)
1039 struct cifs_ses *ses = tcon->ses;
1040 LOCK_REQ *pSMB = (LOCK_REQ *)in_buf;
1042 /* We just modify the current in_buf to change
1043 the type of lock from LOCKING_ANDX_SHARED_LOCK
1044 or LOCKING_ANDX_EXCLUSIVE_LOCK to
1045 LOCKING_ANDX_CANCEL_LOCK. */
1047 pSMB->LockType = LOCKING_ANDX_CANCEL_LOCK|LOCKING_ANDX_LARGE_FILES;
1049 pSMB->hdr.Mid = get_next_mid(ses->server);
1051 return SendReceive(xid, ses, in_buf, out_buf,
1052 &bytes_returned, 0);
1056 SendReceiveBlockingLock(const unsigned int xid, struct cifs_tcon *tcon,
1057 struct smb_hdr *in_buf, struct smb_hdr *out_buf,
1058 int *pbytes_returned)
1062 struct mid_q_entry *midQ;
1063 struct cifs_ses *ses;
1064 unsigned int len = be32_to_cpu(in_buf->smb_buf_length);
1065 struct kvec iov = { .iov_base = in_buf, .iov_len = len };
1066 struct smb_rqst rqst = { .rq_iov = &iov, .rq_nvec = 1 };
1068 if (tcon == NULL || tcon->ses == NULL) {
1069 cifs_dbg(VFS, "Null smb session\n");
1074 if (ses->server == NULL) {
1075 cifs_dbg(VFS, "Null tcp session\n");
1079 if (ses->server->tcpStatus == CifsExiting)
1082 /* Ensure that we do not send more than 50 overlapping requests
1083 to the same server. We may make this configurable later or
1086 if (len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
1087 cifs_dbg(VFS, "Illegal length, greater than maximum frame, %d\n",
1092 rc = wait_for_free_request(ses->server, CIFS_BLOCKING_OP, 0);
1096 /* make sure that we sign in the same order that we send on this socket
1097 and avoid races inside tcp sendmsg code that could cause corruption
1100 mutex_lock(&ses->server->srv_mutex);
1102 rc = allocate_mid(ses, in_buf, &midQ);
1104 mutex_unlock(&ses->server->srv_mutex);
1108 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
1110 cifs_delete_mid(midQ);
1111 mutex_unlock(&ses->server->srv_mutex);
1115 midQ->mid_state = MID_REQUEST_SUBMITTED;
1116 cifs_in_send_inc(ses->server);
1117 rc = smb_send(ses->server, in_buf, len);
1118 cifs_in_send_dec(ses->server);
1119 cifs_save_when_sent(midQ);
1122 ses->server->sequence_number -= 2;
1124 mutex_unlock(&ses->server->srv_mutex);
1127 cifs_delete_mid(midQ);
1131 /* Wait for a reply - allow signals to interrupt. */
1132 rc = wait_event_interruptible(ses->server->response_q,
1133 (!(midQ->mid_state == MID_REQUEST_SUBMITTED)) ||
1134 ((ses->server->tcpStatus != CifsGood) &&
1135 (ses->server->tcpStatus != CifsNew)));
1137 /* Were we interrupted by a signal ? */
1138 if ((rc == -ERESTARTSYS) &&
1139 (midQ->mid_state == MID_REQUEST_SUBMITTED) &&
1140 ((ses->server->tcpStatus == CifsGood) ||
1141 (ses->server->tcpStatus == CifsNew))) {
1143 if (in_buf->Command == SMB_COM_TRANSACTION2) {
1144 /* POSIX lock. We send a NT_CANCEL SMB to cause the
1145 blocking lock to return. */
1146 rc = send_cancel(ses->server, &rqst, midQ);
1148 cifs_delete_mid(midQ);
1152 /* Windows lock. We send a LOCKINGX_CANCEL_LOCK
1153 to cause the blocking lock to return. */
1155 rc = send_lock_cancel(xid, tcon, in_buf, out_buf);
1157 /* If we get -ENOLCK back the lock may have
1158 already been removed. Don't exit in this case. */
1159 if (rc && rc != -ENOLCK) {
1160 cifs_delete_mid(midQ);
1165 rc = wait_for_response(ses->server, midQ);
1167 send_cancel(ses->server, &rqst, midQ);
1168 spin_lock(&GlobalMid_Lock);
1169 if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
1170 /* no longer considered to be "in-flight" */
1171 midQ->callback = DeleteMidQEntry;
1172 spin_unlock(&GlobalMid_Lock);
1175 spin_unlock(&GlobalMid_Lock);
1178 /* We got the response - restart system call. */
1182 rc = cifs_sync_mid_result(midQ, ses->server);
1186 /* rcvd frame is ok */
1187 if (out_buf == NULL || midQ->mid_state != MID_RESPONSE_RECEIVED) {
1189 cifs_dbg(VFS, "Bad MID state?\n");
1193 *pbytes_returned = get_rfc1002_length(midQ->resp_buf);
1194 memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
1195 rc = cifs_check_receive(midQ, ses->server, 0);
1197 cifs_delete_mid(midQ);
1198 if (rstart && rc == -EACCES)
1199 return -ERESTARTSYS;