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"
42 cifs_wake_up_task(struct mid_q_entry *mid)
44 wake_up_process(mid->callback_data);
48 AllocMidQEntry(const struct smb_hdr *smb_buffer, struct TCP_Server_Info *server)
50 struct mid_q_entry *temp;
53 cifs_dbg(VFS, "Null TCP session in AllocMidQEntry\n");
57 temp = mempool_alloc(cifs_mid_poolp, GFP_NOFS);
58 memset(temp, 0, sizeof(struct mid_q_entry));
59 kref_init(&temp->refcount);
60 temp->mid = get_mid(smb_buffer);
61 temp->pid = current->pid;
62 temp->command = cpu_to_le16(smb_buffer->Command);
63 cifs_dbg(FYI, "For smb_command %d\n", smb_buffer->Command);
64 /* do_gettimeofday(&temp->when_sent);*/ /* easier to use jiffies */
65 /* when mid allocated can be before when sent */
66 temp->when_alloc = jiffies;
67 temp->server = server;
70 * The default is for the mid to be synchronous, so the
71 * default callback just wakes up the current task.
73 temp->callback = cifs_wake_up_task;
74 temp->callback_data = current;
76 atomic_inc(&midCount);
77 temp->mid_state = MID_REQUEST_ALLOCATED;
81 static void _cifs_mid_q_entry_release(struct kref *refcount)
83 struct mid_q_entry *mid = container_of(refcount, struct mid_q_entry,
86 mempool_free(mid, cifs_mid_poolp);
89 void cifs_mid_q_entry_release(struct mid_q_entry *midEntry)
91 spin_lock(&GlobalMid_Lock);
92 kref_put(&midEntry->refcount, _cifs_mid_q_entry_release);
93 spin_unlock(&GlobalMid_Lock);
97 DeleteMidQEntry(struct mid_q_entry *midEntry)
99 #ifdef CONFIG_CIFS_STATS2
100 __le16 command = midEntry->server->vals->lock_cmd;
103 midEntry->mid_state = MID_FREE;
104 atomic_dec(&midCount);
105 if (midEntry->large_buf)
106 cifs_buf_release(midEntry->resp_buf);
108 cifs_small_buf_release(midEntry->resp_buf);
109 #ifdef CONFIG_CIFS_STATS2
111 /* commands taking longer than one second are indications that
112 something is wrong, unless it is quite a slow link or server */
113 if (time_after(now, midEntry->when_alloc + HZ)) {
114 if ((cifsFYI & CIFS_TIMER) && (midEntry->command != command)) {
115 pr_debug(" CIFS slow rsp: cmd %d mid %llu",
116 midEntry->command, midEntry->mid);
117 pr_info(" A: 0x%lx S: 0x%lx R: 0x%lx\n",
118 now - midEntry->when_alloc,
119 now - midEntry->when_sent,
120 now - midEntry->when_received);
124 cifs_mid_q_entry_release(midEntry);
128 cifs_delete_mid(struct mid_q_entry *mid)
130 spin_lock(&GlobalMid_Lock);
131 list_del(&mid->qhead);
132 spin_unlock(&GlobalMid_Lock);
134 DeleteMidQEntry(mid);
138 * smb_send_kvec - send an array of kvecs to the server
139 * @server: Server to send the data to
140 * @smb_msg: Message to send
141 * @sent: amount of data sent on socket is stored here
143 * Our basic "send data to server" function. Should be called with srv_mutex
144 * held. The caller is responsible for handling the results.
147 smb_send_kvec(struct TCP_Server_Info *server, struct msghdr *smb_msg,
152 struct socket *ssocket = server->ssocket;
156 smb_msg->msg_name = NULL;
157 smb_msg->msg_namelen = 0;
158 smb_msg->msg_control = NULL;
159 smb_msg->msg_controllen = 0;
160 if (server->noblocksnd)
161 smb_msg->msg_flags = MSG_DONTWAIT + MSG_NOSIGNAL;
163 smb_msg->msg_flags = MSG_NOSIGNAL;
165 while (msg_data_left(smb_msg)) {
167 * If blocking send, we try 3 times, since each can block
168 * for 5 seconds. For nonblocking we have to try more
169 * but wait increasing amounts of time allowing time for
170 * socket to clear. The overall time we wait in either
171 * case to send on the socket is about 15 seconds.
172 * Similarly we wait for 15 seconds for a response from
173 * the server in SendReceive[2] for the server to send
174 * a response back for most types of requests (except
175 * SMB Write past end of file which can be slow, and
176 * blocking lock operations). NFS waits slightly longer
177 * than CIFS, but this can make it take longer for
178 * nonresponsive servers to be detected and 15 seconds
179 * is more than enough time for modern networks to
180 * send a packet. In most cases if we fail to send
181 * after the retries we will kill the socket and
182 * reconnect which may clear the network problem.
184 rc = sock_sendmsg(ssocket, smb_msg);
188 (!server->noblocksnd && (retries > 2))) {
189 cifs_dbg(VFS, "sends on sock %p stuck for 15 seconds\n",
193 msleep(1 << retries);
201 /* should never happen, letting socket clear before
202 retrying is our only obvious option here */
203 cifs_dbg(VFS, "tcp sent no data\n");
208 /* send was at least partially successful */
210 retries = 0; /* in case we get ENOSPC on the next send */
216 rqst_len(struct smb_rqst *rqst)
219 struct kvec *iov = rqst->rq_iov;
220 unsigned long buflen = 0;
222 /* total up iov array first */
223 for (i = 0; i < rqst->rq_nvec; i++)
224 buflen += iov[i].iov_len;
226 /* add in the page array if there is one */
227 if (rqst->rq_npages) {
228 buflen += rqst->rq_pagesz * (rqst->rq_npages - 1);
229 buflen += rqst->rq_tailsz;
236 __smb_send_rqst(struct TCP_Server_Info *server, struct smb_rqst *rqst)
239 struct kvec *iov = rqst->rq_iov;
240 int n_vec = rqst->rq_nvec;
241 unsigned int smb_buf_length = get_rfc1002_length(iov[0].iov_base);
242 unsigned long send_length;
244 size_t total_len = 0, sent, size;
245 struct socket *ssocket = server->ssocket;
246 struct msghdr smb_msg;
252 /* sanity check send length */
253 send_length = rqst_len(rqst);
254 if (send_length != smb_buf_length + 4) {
255 WARN(1, "Send length mismatch(send_length=%lu smb_buf_length=%u)\n",
256 send_length, smb_buf_length);
263 cifs_dbg(FYI, "Sending smb: smb_len=%u\n", smb_buf_length);
264 dump_smb(iov[0].iov_base, iov[0].iov_len);
265 dump_smb(iov[1].iov_base, iov[1].iov_len);
267 /* cork the socket */
268 kernel_setsockopt(ssocket, SOL_TCP, TCP_CORK,
269 (char *)&val, sizeof(val));
272 for (i = 0; i < n_vec; i++)
273 size += iov[i].iov_len;
275 iov_iter_kvec(&smb_msg.msg_iter, WRITE | ITER_KVEC, iov, n_vec, size);
277 rc = smb_send_kvec(server, &smb_msg, &sent);
283 /* now walk the page array and send each page in it */
284 for (i = 0; i < rqst->rq_npages; i++) {
285 size_t len = i == rqst->rq_npages - 1
288 struct bio_vec bvec = {
289 .bv_page = rqst->rq_pages[i],
292 iov_iter_bvec(&smb_msg.msg_iter, WRITE | ITER_BVEC,
294 rc = smb_send_kvec(server, &smb_msg, &sent);
304 kernel_setsockopt(ssocket, SOL_TCP, TCP_CORK,
305 (char *)&val, sizeof(val));
307 if ((total_len > 0) && (total_len != smb_buf_length + 4)) {
308 cifs_dbg(FYI, "partial send (wanted=%u sent=%zu): terminating session\n",
309 smb_buf_length + 4, total_len);
311 * If we have only sent part of an SMB then the next SMB could
312 * be taken as the remainder of this one. We need to kill the
313 * socket so the server throws away the partial SMB
315 server->tcpStatus = CifsNeedReconnect;
318 if (rc < 0 && rc != -EINTR)
319 cifs_dbg(VFS, "Error %d sending data on socket to server\n",
328 smb_send_rqst(struct TCP_Server_Info *server, struct smb_rqst *rqst, int flags)
330 struct smb_rqst cur_rqst;
333 if (!(flags & CIFS_TRANSFORM_REQ))
334 return __smb_send_rqst(server, rqst);
336 if (!server->ops->init_transform_rq ||
337 !server->ops->free_transform_rq) {
338 cifs_dbg(VFS, "Encryption requested but transform callbacks are missed\n");
342 rc = server->ops->init_transform_rq(server, &cur_rqst, rqst);
346 rc = __smb_send_rqst(server, &cur_rqst);
347 server->ops->free_transform_rq(&cur_rqst);
352 smb_send(struct TCP_Server_Info *server, struct smb_hdr *smb_buffer,
353 unsigned int smb_buf_length)
356 struct smb_rqst rqst = { .rq_iov = iov,
359 iov[0].iov_base = smb_buffer;
361 iov[1].iov_base = (char *)smb_buffer + 4;
362 iov[1].iov_len = smb_buf_length;
364 return __smb_send_rqst(server, &rqst);
368 wait_for_free_credits(struct TCP_Server_Info *server, const int timeout,
373 spin_lock(&server->req_lock);
374 if (timeout == CIFS_ASYNC_OP) {
375 /* oplock breaks must not be held up */
378 spin_unlock(&server->req_lock);
384 spin_unlock(&server->req_lock);
385 cifs_num_waiters_inc(server);
386 rc = wait_event_killable(server->request_q,
387 has_credits(server, credits));
388 cifs_num_waiters_dec(server);
391 spin_lock(&server->req_lock);
393 if (server->tcpStatus == CifsExiting) {
394 spin_unlock(&server->req_lock);
399 * Can not count locking commands against total
400 * as they are allowed to block on server.
403 /* update # of requests on the wire to server */
404 if (timeout != CIFS_BLOCKING_OP) {
408 spin_unlock(&server->req_lock);
416 wait_for_free_request(struct TCP_Server_Info *server, const int timeout,
421 val = server->ops->get_credits_field(server, optype);
422 /* Since an echo is already inflight, no need to wait to send another */
423 if (*val <= 0 && optype == CIFS_ECHO_OP)
425 return wait_for_free_credits(server, timeout, val);
429 cifs_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
430 unsigned int *num, unsigned int *credits)
437 static int allocate_mid(struct cifs_ses *ses, struct smb_hdr *in_buf,
438 struct mid_q_entry **ppmidQ)
440 if (ses->server->tcpStatus == CifsExiting) {
444 if (ses->server->tcpStatus == CifsNeedReconnect) {
445 cifs_dbg(FYI, "tcp session dead - return to caller to retry\n");
449 if (ses->status == CifsNew) {
450 if ((in_buf->Command != SMB_COM_SESSION_SETUP_ANDX) &&
451 (in_buf->Command != SMB_COM_NEGOTIATE))
453 /* else ok - we are setting up session */
456 if (ses->status == CifsExiting) {
457 /* check if SMB session is bad because we are setting it up */
458 if (in_buf->Command != SMB_COM_LOGOFF_ANDX)
460 /* else ok - we are shutting down session */
463 *ppmidQ = AllocMidQEntry(in_buf, ses->server);
466 spin_lock(&GlobalMid_Lock);
467 list_add_tail(&(*ppmidQ)->qhead, &ses->server->pending_mid_q);
468 spin_unlock(&GlobalMid_Lock);
473 wait_for_response(struct TCP_Server_Info *server, struct mid_q_entry *midQ)
477 error = wait_event_freezekillable_unsafe(server->response_q,
478 midQ->mid_state != MID_REQUEST_SUBMITTED);
486 cifs_setup_async_request(struct TCP_Server_Info *server, struct smb_rqst *rqst)
489 struct smb_hdr *hdr = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
490 struct mid_q_entry *mid;
492 if (rqst->rq_iov[0].iov_len != 4 ||
493 rqst->rq_iov[0].iov_base + 4 != rqst->rq_iov[1].iov_base)
494 return ERR_PTR(-EIO);
496 /* enable signing if server requires it */
498 hdr->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
500 mid = AllocMidQEntry(hdr, server);
502 return ERR_PTR(-ENOMEM);
504 rc = cifs_sign_rqst(rqst, server, &mid->sequence_number);
506 DeleteMidQEntry(mid);
514 * Send a SMB request and set the callback function in the mid to handle
515 * the result. Caller is responsible for dealing with timeouts.
518 cifs_call_async(struct TCP_Server_Info *server, struct smb_rqst *rqst,
519 mid_receive_t *receive, mid_callback_t *callback,
520 mid_handle_t *handle, void *cbdata, const int flags)
522 int rc, timeout, optype;
523 struct mid_q_entry *mid;
524 unsigned int credits = 0;
526 timeout = flags & CIFS_TIMEOUT_MASK;
527 optype = flags & CIFS_OP_MASK;
529 if ((flags & CIFS_HAS_CREDITS) == 0) {
530 rc = wait_for_free_request(server, timeout, optype);
536 mutex_lock(&server->srv_mutex);
537 mid = server->ops->setup_async_request(server, rqst);
539 mutex_unlock(&server->srv_mutex);
540 add_credits_and_wake_if(server, credits, optype);
544 mid->receive = receive;
545 mid->callback = callback;
546 mid->callback_data = cbdata;
547 mid->handle = handle;
548 mid->mid_state = MID_REQUEST_SUBMITTED;
550 /* put it on the pending_mid_q */
551 spin_lock(&GlobalMid_Lock);
552 list_add_tail(&mid->qhead, &server->pending_mid_q);
553 spin_unlock(&GlobalMid_Lock);
556 * Need to store the time in mid before calling I/O. For call_async,
557 * I/O response may come back and free the mid entry on another thread.
559 cifs_save_when_sent(mid);
560 cifs_in_send_inc(server);
561 rc = smb_send_rqst(server, rqst, flags);
562 cifs_in_send_dec(server);
565 server->sequence_number -= 2;
566 cifs_delete_mid(mid);
569 mutex_unlock(&server->srv_mutex);
574 add_credits_and_wake_if(server, credits, optype);
580 * Send an SMB Request. No response info (other than return code)
581 * needs to be parsed.
583 * flags indicate the type of request buffer and how long to wait
584 * and whether to log NT STATUS code (error) before mapping it to POSIX error
588 SendReceiveNoRsp(const unsigned int xid, struct cifs_ses *ses,
589 char *in_buf, int flags)
596 iov[0].iov_base = in_buf;
597 iov[0].iov_len = get_rfc1002_length(in_buf) + 4;
598 flags |= CIFS_NO_RESP;
599 rc = SendReceive2(xid, ses, iov, 1, &resp_buf_type, flags, &rsp_iov);
600 cifs_dbg(NOISY, "SendRcvNoRsp flags %d rc %d\n", flags, rc);
606 cifs_sync_mid_result(struct mid_q_entry *mid, struct TCP_Server_Info *server)
610 cifs_dbg(FYI, "%s: cmd=%d mid=%llu state=%d\n",
611 __func__, le16_to_cpu(mid->command), mid->mid, mid->mid_state);
613 spin_lock(&GlobalMid_Lock);
614 switch (mid->mid_state) {
615 case MID_RESPONSE_RECEIVED:
616 spin_unlock(&GlobalMid_Lock);
618 case MID_RETRY_NEEDED:
621 case MID_RESPONSE_MALFORMED:
628 list_del_init(&mid->qhead);
629 cifs_dbg(VFS, "%s: invalid mid state mid=%llu state=%d\n",
630 __func__, mid->mid, mid->mid_state);
633 spin_unlock(&GlobalMid_Lock);
635 DeleteMidQEntry(mid);
640 send_cancel(struct TCP_Server_Info *server, struct smb_rqst *rqst,
641 struct mid_q_entry *mid)
643 return server->ops->send_cancel ?
644 server->ops->send_cancel(server, rqst, mid) : 0;
648 cifs_check_receive(struct mid_q_entry *mid, struct TCP_Server_Info *server,
651 unsigned int len = get_rfc1002_length(mid->resp_buf) + 4;
653 dump_smb(mid->resp_buf, min_t(u32, 92, len));
655 /* convert the length into a more usable form */
659 struct smb_rqst rqst = { .rq_iov = iov,
662 iov[0].iov_base = mid->resp_buf;
664 iov[1].iov_base = (char *)mid->resp_buf + 4;
665 iov[1].iov_len = len - 4;
666 /* FIXME: add code to kill session */
667 rc = cifs_verify_signature(&rqst, server,
668 mid->sequence_number);
670 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
674 /* BB special case reconnect tid and uid here? */
675 return map_smb_to_linux_error(mid->resp_buf, log_error);
679 cifs_setup_request(struct cifs_ses *ses, struct smb_rqst *rqst)
682 struct smb_hdr *hdr = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
683 struct mid_q_entry *mid;
685 if (rqst->rq_iov[0].iov_len != 4 ||
686 rqst->rq_iov[0].iov_base + 4 != rqst->rq_iov[1].iov_base)
687 return ERR_PTR(-EIO);
689 rc = allocate_mid(ses, hdr, &mid);
692 rc = cifs_sign_rqst(rqst, ses->server, &mid->sequence_number);
694 cifs_delete_mid(mid);
701 cifs_send_recv(const unsigned int xid, struct cifs_ses *ses,
702 struct smb_rqst *rqst, int *resp_buf_type, const int flags,
703 struct kvec *resp_iov)
707 struct mid_q_entry *midQ;
708 unsigned int credits = 1;
711 timeout = flags & CIFS_TIMEOUT_MASK;
712 optype = flags & CIFS_OP_MASK;
714 *resp_buf_type = CIFS_NO_BUFFER; /* no response buf yet */
716 if ((ses == NULL) || (ses->server == NULL)) {
717 cifs_dbg(VFS, "Null session\n");
721 if (ses->server->tcpStatus == CifsExiting)
725 * Ensure that we do not send more than 50 overlapping requests
726 * to the same server. We may make this configurable later or
730 rc = wait_for_free_request(ses->server, timeout, optype);
735 * Make sure that we sign in the same order that we send on this socket
736 * and avoid races inside tcp sendmsg code that could cause corruption
740 mutex_lock(&ses->server->srv_mutex);
742 midQ = ses->server->ops->setup_request(ses, rqst);
744 mutex_unlock(&ses->server->srv_mutex);
745 /* Update # of requests on wire to server */
746 add_credits(ses->server, 1, optype);
747 return PTR_ERR(midQ);
750 midQ->mid_state = MID_REQUEST_SUBMITTED;
751 cifs_in_send_inc(ses->server);
752 rc = smb_send_rqst(ses->server, rqst, flags);
753 cifs_in_send_dec(ses->server);
754 cifs_save_when_sent(midQ);
757 ses->server->sequence_number -= 2;
758 mutex_unlock(&ses->server->srv_mutex);
763 if (timeout == CIFS_ASYNC_OP)
766 rc = wait_for_response(ses->server, midQ);
768 cifs_dbg(FYI, "Cancelling wait for mid %llu\n", midQ->mid);
769 send_cancel(ses->server, rqst, midQ);
770 spin_lock(&GlobalMid_Lock);
771 if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
772 midQ->mid_flags |= MID_WAIT_CANCELLED;
773 midQ->callback = DeleteMidQEntry;
774 spin_unlock(&GlobalMid_Lock);
775 add_credits(ses->server, 1, optype);
778 spin_unlock(&GlobalMid_Lock);
781 rc = cifs_sync_mid_result(midQ, ses->server);
783 add_credits(ses->server, 1, optype);
787 if (!midQ->resp_buf || midQ->mid_state != MID_RESPONSE_RECEIVED) {
789 cifs_dbg(FYI, "Bad MID state?\n");
793 buf = (char *)midQ->resp_buf;
794 resp_iov->iov_base = buf;
795 resp_iov->iov_len = get_rfc1002_length(buf) + 4;
797 *resp_buf_type = CIFS_LARGE_BUFFER;
799 *resp_buf_type = CIFS_SMALL_BUFFER;
801 credits = ses->server->ops->get_credits(midQ);
803 rc = ses->server->ops->check_receive(midQ, ses->server,
804 flags & CIFS_LOG_ERROR);
806 /* mark it so buf will not be freed by cifs_delete_mid */
807 if ((flags & CIFS_NO_RESP) == 0)
808 midQ->resp_buf = NULL;
810 cifs_delete_mid(midQ);
811 add_credits(ses->server, credits, optype);
817 SendReceive2(const unsigned int xid, struct cifs_ses *ses,
818 struct kvec *iov, int n_vec, int *resp_buf_type /* ret */,
819 const int flags, struct kvec *resp_iov)
821 struct smb_rqst rqst;
822 struct kvec *new_iov;
825 new_iov = kmalloc(sizeof(struct kvec) * (n_vec + 1), GFP_KERNEL);
829 /* 1st iov is a RFC1001 length followed by the rest of the packet */
830 memcpy(new_iov + 1, iov, (sizeof(struct kvec) * n_vec));
832 new_iov[0].iov_base = new_iov[1].iov_base;
833 new_iov[0].iov_len = 4;
834 new_iov[1].iov_base += 4;
835 new_iov[1].iov_len -= 4;
837 memset(&rqst, 0, sizeof(struct smb_rqst));
838 rqst.rq_iov = new_iov;
839 rqst.rq_nvec = n_vec + 1;
841 rc = cifs_send_recv(xid, ses, &rqst, resp_buf_type, flags, resp_iov);
847 SendReceive(const unsigned int xid, struct cifs_ses *ses,
848 struct smb_hdr *in_buf, struct smb_hdr *out_buf,
849 int *pbytes_returned, const int timeout)
852 struct mid_q_entry *midQ;
853 unsigned int len = be32_to_cpu(in_buf->smb_buf_length);
854 struct kvec iov = { .iov_base = in_buf, .iov_len = len };
855 struct smb_rqst rqst = { .rq_iov = &iov, .rq_nvec = 1 };
858 cifs_dbg(VFS, "Null smb session\n");
861 if (ses->server == NULL) {
862 cifs_dbg(VFS, "Null tcp session\n");
866 if (ses->server->tcpStatus == CifsExiting)
869 /* Ensure that we do not send more than 50 overlapping requests
870 to the same server. We may make this configurable later or
873 if (len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
874 cifs_dbg(VFS, "Illegal length, greater than maximum frame, %d\n",
879 rc = wait_for_free_request(ses->server, timeout, 0);
883 /* make sure that we sign in the same order that we send on this socket
884 and avoid races inside tcp sendmsg code that could cause corruption
887 mutex_lock(&ses->server->srv_mutex);
889 rc = allocate_mid(ses, in_buf, &midQ);
891 mutex_unlock(&ses->server->srv_mutex);
892 /* Update # of requests on wire to server */
893 add_credits(ses->server, 1, 0);
897 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
899 mutex_unlock(&ses->server->srv_mutex);
903 midQ->mid_state = MID_REQUEST_SUBMITTED;
905 cifs_in_send_inc(ses->server);
906 rc = smb_send(ses->server, in_buf, len);
907 cifs_in_send_dec(ses->server);
908 cifs_save_when_sent(midQ);
911 ses->server->sequence_number -= 2;
913 mutex_unlock(&ses->server->srv_mutex);
918 if (timeout == CIFS_ASYNC_OP)
921 rc = wait_for_response(ses->server, midQ);
923 send_cancel(ses->server, &rqst, midQ);
924 spin_lock(&GlobalMid_Lock);
925 if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
926 /* no longer considered to be "in-flight" */
927 midQ->callback = DeleteMidQEntry;
928 spin_unlock(&GlobalMid_Lock);
929 add_credits(ses->server, 1, 0);
932 spin_unlock(&GlobalMid_Lock);
935 rc = cifs_sync_mid_result(midQ, ses->server);
937 add_credits(ses->server, 1, 0);
941 if (!midQ->resp_buf || !out_buf ||
942 midQ->mid_state != MID_RESPONSE_RECEIVED) {
944 cifs_dbg(VFS, "Bad MID state?\n");
948 *pbytes_returned = get_rfc1002_length(midQ->resp_buf);
949 memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
950 rc = cifs_check_receive(midQ, ses->server, 0);
952 cifs_delete_mid(midQ);
953 add_credits(ses->server, 1, 0);
958 /* We send a LOCKINGX_CANCEL_LOCK to cause the Windows
959 blocking lock to return. */
962 send_lock_cancel(const unsigned int xid, struct cifs_tcon *tcon,
963 struct smb_hdr *in_buf,
964 struct smb_hdr *out_buf)
967 struct cifs_ses *ses = tcon->ses;
968 LOCK_REQ *pSMB = (LOCK_REQ *)in_buf;
970 /* We just modify the current in_buf to change
971 the type of lock from LOCKING_ANDX_SHARED_LOCK
972 or LOCKING_ANDX_EXCLUSIVE_LOCK to
973 LOCKING_ANDX_CANCEL_LOCK. */
975 pSMB->LockType = LOCKING_ANDX_CANCEL_LOCK|LOCKING_ANDX_LARGE_FILES;
977 pSMB->hdr.Mid = get_next_mid(ses->server);
979 return SendReceive(xid, ses, in_buf, out_buf,
984 SendReceiveBlockingLock(const unsigned int xid, struct cifs_tcon *tcon,
985 struct smb_hdr *in_buf, struct smb_hdr *out_buf,
986 int *pbytes_returned)
990 struct mid_q_entry *midQ;
991 struct cifs_ses *ses;
992 unsigned int len = be32_to_cpu(in_buf->smb_buf_length);
993 struct kvec iov = { .iov_base = in_buf, .iov_len = len };
994 struct smb_rqst rqst = { .rq_iov = &iov, .rq_nvec = 1 };
996 if (tcon == NULL || tcon->ses == NULL) {
997 cifs_dbg(VFS, "Null smb session\n");
1002 if (ses->server == NULL) {
1003 cifs_dbg(VFS, "Null tcp session\n");
1007 if (ses->server->tcpStatus == CifsExiting)
1010 /* Ensure that we do not send more than 50 overlapping requests
1011 to the same server. We may make this configurable later or
1014 if (len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
1015 cifs_dbg(VFS, "Illegal length, greater than maximum frame, %d\n",
1020 rc = wait_for_free_request(ses->server, CIFS_BLOCKING_OP, 0);
1024 /* make sure that we sign in the same order that we send on this socket
1025 and avoid races inside tcp sendmsg code that could cause corruption
1028 mutex_lock(&ses->server->srv_mutex);
1030 rc = allocate_mid(ses, in_buf, &midQ);
1032 mutex_unlock(&ses->server->srv_mutex);
1036 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
1038 cifs_delete_mid(midQ);
1039 mutex_unlock(&ses->server->srv_mutex);
1043 midQ->mid_state = MID_REQUEST_SUBMITTED;
1044 cifs_in_send_inc(ses->server);
1045 rc = smb_send(ses->server, in_buf, len);
1046 cifs_in_send_dec(ses->server);
1047 cifs_save_when_sent(midQ);
1050 ses->server->sequence_number -= 2;
1052 mutex_unlock(&ses->server->srv_mutex);
1055 cifs_delete_mid(midQ);
1059 /* Wait for a reply - allow signals to interrupt. */
1060 rc = wait_event_interruptible(ses->server->response_q,
1061 (!(midQ->mid_state == MID_REQUEST_SUBMITTED)) ||
1062 ((ses->server->tcpStatus != CifsGood) &&
1063 (ses->server->tcpStatus != CifsNew)));
1065 /* Were we interrupted by a signal ? */
1066 if ((rc == -ERESTARTSYS) &&
1067 (midQ->mid_state == MID_REQUEST_SUBMITTED) &&
1068 ((ses->server->tcpStatus == CifsGood) ||
1069 (ses->server->tcpStatus == CifsNew))) {
1071 if (in_buf->Command == SMB_COM_TRANSACTION2) {
1072 /* POSIX lock. We send a NT_CANCEL SMB to cause the
1073 blocking lock to return. */
1074 rc = send_cancel(ses->server, &rqst, midQ);
1076 cifs_delete_mid(midQ);
1080 /* Windows lock. We send a LOCKINGX_CANCEL_LOCK
1081 to cause the blocking lock to return. */
1083 rc = send_lock_cancel(xid, tcon, in_buf, out_buf);
1085 /* If we get -ENOLCK back the lock may have
1086 already been removed. Don't exit in this case. */
1087 if (rc && rc != -ENOLCK) {
1088 cifs_delete_mid(midQ);
1093 rc = wait_for_response(ses->server, midQ);
1095 send_cancel(ses->server, &rqst, midQ);
1096 spin_lock(&GlobalMid_Lock);
1097 if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
1098 /* no longer considered to be "in-flight" */
1099 midQ->callback = DeleteMidQEntry;
1100 spin_unlock(&GlobalMid_Lock);
1103 spin_unlock(&GlobalMid_Lock);
1106 /* We got the response - restart system call. */
1110 rc = cifs_sync_mid_result(midQ, ses->server);
1114 /* rcvd frame is ok */
1115 if (out_buf == NULL || midQ->mid_state != MID_RESPONSE_RECEIVED) {
1117 cifs_dbg(VFS, "Bad MID state?\n");
1121 *pbytes_returned = get_rfc1002_length(midQ->resp_buf);
1122 memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
1123 rc = cifs_check_receive(midQ, ses->server, 0);
1125 cifs_delete_mid(midQ);
1126 if (rstart && rc == -EACCES)
1127 return -ERESTARTSYS;