Staging: bcm: Remove typedef for _stPhsRuleSI and call directly.
[linux-2.6.git] / fs / cifs / transport.c
blobd9b639b95fa8b8a5167ecf58fa3285f90ff2520f
1 /*
2 * fs/cifs/transport.c
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
23 #include <linux/fs.h>
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 <asm/uaccess.h>
31 #include <asm/processor.h>
32 #include <linux/mempool.h>
33 #include "cifspdu.h"
34 #include "cifsglob.h"
35 #include "cifsproto.h"
36 #include "cifs_debug.h"
38 void
39 cifs_wake_up_task(struct mid_q_entry *mid)
41 wake_up_process(mid->callback_data);
44 struct mid_q_entry *
45 AllocMidQEntry(const struct smb_hdr *smb_buffer, struct TCP_Server_Info *server)
47 struct mid_q_entry *temp;
49 if (server == NULL) {
50 cERROR(1, "Null TCP session in AllocMidQEntry");
51 return NULL;
54 temp = mempool_alloc(cifs_mid_poolp, GFP_NOFS);
55 if (temp == NULL)
56 return temp;
57 else {
58 memset(temp, 0, sizeof(struct mid_q_entry));
59 temp->mid = smb_buffer->Mid; /* always LE */
60 temp->pid = current->pid;
61 temp->command = cpu_to_le16(smb_buffer->Command);
62 cFYI(1, "For smb_command %d", smb_buffer->Command);
63 /* do_gettimeofday(&temp->when_sent);*/ /* easier to use jiffies */
64 /* when mid allocated can be before when sent */
65 temp->when_alloc = jiffies;
66 temp->server = server;
69 * The default is for the mid to be synchronous, so the
70 * default callback just wakes up the current task.
72 temp->callback = cifs_wake_up_task;
73 temp->callback_data = current;
76 atomic_inc(&midCount);
77 temp->mid_state = MID_REQUEST_ALLOCATED;
78 return temp;
81 void
82 DeleteMidQEntry(struct mid_q_entry *midEntry)
84 #ifdef CONFIG_CIFS_STATS2
85 __le16 command = midEntry->server->vals->lock_cmd;
86 unsigned long now;
87 #endif
88 midEntry->mid_state = MID_FREE;
89 atomic_dec(&midCount);
90 if (midEntry->large_buf)
91 cifs_buf_release(midEntry->resp_buf);
92 else
93 cifs_small_buf_release(midEntry->resp_buf);
94 #ifdef CONFIG_CIFS_STATS2
95 now = jiffies;
96 /* commands taking longer than one second are indications that
97 something is wrong, unless it is quite a slow link or server */
98 if ((now - midEntry->when_alloc) > HZ) {
99 if ((cifsFYI & CIFS_TIMER) && (midEntry->command != command)) {
100 printk(KERN_DEBUG " CIFS slow rsp: cmd %d mid %llu",
101 midEntry->command, midEntry->mid);
102 printk(" A: 0x%lx S: 0x%lx R: 0x%lx\n",
103 now - midEntry->when_alloc,
104 now - midEntry->when_sent,
105 now - midEntry->when_received);
108 #endif
109 mempool_free(midEntry, cifs_mid_poolp);
112 static void
113 delete_mid(struct mid_q_entry *mid)
115 spin_lock(&GlobalMid_Lock);
116 list_del(&mid->qhead);
117 spin_unlock(&GlobalMid_Lock);
119 DeleteMidQEntry(mid);
122 static int
123 smb_sendv(struct TCP_Server_Info *server, struct kvec *iov, int n_vec)
125 int rc = 0;
126 int i = 0;
127 struct msghdr smb_msg;
128 unsigned int len = iov[0].iov_len;
129 unsigned int total_len;
130 int first_vec = 0;
131 unsigned int smb_buf_length = get_rfc1002_length(iov[0].iov_base);
132 struct socket *ssocket = server->ssocket;
134 if (ssocket == NULL)
135 return -ENOTSOCK; /* BB eventually add reconnect code here */
137 smb_msg.msg_name = (struct sockaddr *) &server->dstaddr;
138 smb_msg.msg_namelen = sizeof(struct sockaddr);
139 smb_msg.msg_control = NULL;
140 smb_msg.msg_controllen = 0;
141 if (server->noblocksnd)
142 smb_msg.msg_flags = MSG_DONTWAIT + MSG_NOSIGNAL;
143 else
144 smb_msg.msg_flags = MSG_NOSIGNAL;
146 total_len = 0;
147 for (i = 0; i < n_vec; i++)
148 total_len += iov[i].iov_len;
150 cFYI(1, "Sending smb: total_len %d", total_len);
151 dump_smb(iov[0].iov_base, len);
153 i = 0;
154 while (total_len) {
155 rc = kernel_sendmsg(ssocket, &smb_msg, &iov[first_vec],
156 n_vec - first_vec, total_len);
157 if ((rc == -ENOSPC) || (rc == -EAGAIN)) {
158 i++;
160 * If blocking send we try 3 times, since each can block
161 * for 5 seconds. For nonblocking we have to try more
162 * but wait increasing amounts of time allowing time for
163 * socket to clear. The overall time we wait in either
164 * case to send on the socket is about 15 seconds.
165 * Similarly we wait for 15 seconds for a response from
166 * the server in SendReceive[2] for the server to send
167 * a response back for most types of requests (except
168 * SMB Write past end of file which can be slow, and
169 * blocking lock operations). NFS waits slightly longer
170 * than CIFS, but this can make it take longer for
171 * nonresponsive servers to be detected and 15 seconds
172 * is more than enough time for modern networks to
173 * send a packet. In most cases if we fail to send
174 * after the retries we will kill the socket and
175 * reconnect which may clear the network problem.
177 if ((i >= 14) || (!server->noblocksnd && (i > 2))) {
178 cERROR(1, "sends on sock %p stuck for 15 seconds",
179 ssocket);
180 rc = -EAGAIN;
181 break;
183 msleep(1 << i);
184 continue;
186 if (rc < 0)
187 break;
189 if (rc == total_len) {
190 total_len = 0;
191 break;
192 } else if (rc > total_len) {
193 cERROR(1, "sent %d requested %d", rc, total_len);
194 break;
196 if (rc == 0) {
197 /* should never happen, letting socket clear before
198 retrying is our only obvious option here */
199 cERROR(1, "tcp sent no data");
200 msleep(500);
201 continue;
203 total_len -= rc;
204 /* the line below resets i */
205 for (i = first_vec; i < n_vec; i++) {
206 if (iov[i].iov_len) {
207 if (rc > iov[i].iov_len) {
208 rc -= iov[i].iov_len;
209 iov[i].iov_len = 0;
210 } else {
211 iov[i].iov_base += rc;
212 iov[i].iov_len -= rc;
213 first_vec = i;
214 break;
218 i = 0; /* in case we get ENOSPC on the next send */
221 if ((total_len > 0) && (total_len != smb_buf_length + 4)) {
222 cFYI(1, "partial send (%d remaining), terminating session",
223 total_len);
224 /* If we have only sent part of an SMB then the next SMB
225 could be taken as the remainder of this one. We need
226 to kill the socket so the server throws away the partial
227 SMB */
228 server->tcpStatus = CifsNeedReconnect;
231 if (rc < 0 && rc != -EINTR)
232 cERROR(1, "Error %d sending data on socket to server", rc);
233 else
234 rc = 0;
236 return rc;
240 smb_send(struct TCP_Server_Info *server, struct smb_hdr *smb_buffer,
241 unsigned int smb_buf_length)
243 struct kvec iov;
245 iov.iov_base = smb_buffer;
246 iov.iov_len = smb_buf_length + 4;
248 return smb_sendv(server, &iov, 1);
251 static int
252 wait_for_free_credits(struct TCP_Server_Info *server, const int timeout,
253 int *credits)
255 int rc;
257 spin_lock(&server->req_lock);
258 if (timeout == CIFS_ASYNC_OP) {
259 /* oplock breaks must not be held up */
260 server->in_flight++;
261 *credits -= 1;
262 spin_unlock(&server->req_lock);
263 return 0;
266 while (1) {
267 if (*credits <= 0) {
268 spin_unlock(&server->req_lock);
269 cifs_num_waiters_inc(server);
270 rc = wait_event_killable(server->request_q,
271 has_credits(server, credits));
272 cifs_num_waiters_dec(server);
273 if (rc)
274 return rc;
275 spin_lock(&server->req_lock);
276 } else {
277 if (server->tcpStatus == CifsExiting) {
278 spin_unlock(&server->req_lock);
279 return -ENOENT;
283 * Can not count locking commands against total
284 * as they are allowed to block on server.
287 /* update # of requests on the wire to server */
288 if (timeout != CIFS_BLOCKING_OP) {
289 *credits -= 1;
290 server->in_flight++;
292 spin_unlock(&server->req_lock);
293 break;
296 return 0;
299 static int
300 wait_for_free_request(struct TCP_Server_Info *server, const int timeout,
301 const int optype)
303 return wait_for_free_credits(server, timeout,
304 server->ops->get_credits_field(server, optype));
307 static int allocate_mid(struct cifs_ses *ses, struct smb_hdr *in_buf,
308 struct mid_q_entry **ppmidQ)
310 if (ses->server->tcpStatus == CifsExiting) {
311 return -ENOENT;
314 if (ses->server->tcpStatus == CifsNeedReconnect) {
315 cFYI(1, "tcp session dead - return to caller to retry");
316 return -EAGAIN;
319 if (ses->status != CifsGood) {
320 /* check if SMB session is bad because we are setting it up */
321 if ((in_buf->Command != SMB_COM_SESSION_SETUP_ANDX) &&
322 (in_buf->Command != SMB_COM_NEGOTIATE))
323 return -EAGAIN;
324 /* else ok - we are setting up session */
326 *ppmidQ = AllocMidQEntry(in_buf, ses->server);
327 if (*ppmidQ == NULL)
328 return -ENOMEM;
329 spin_lock(&GlobalMid_Lock);
330 list_add_tail(&(*ppmidQ)->qhead, &ses->server->pending_mid_q);
331 spin_unlock(&GlobalMid_Lock);
332 return 0;
335 static int
336 wait_for_response(struct TCP_Server_Info *server, struct mid_q_entry *midQ)
338 int error;
340 error = wait_event_freezekillable(server->response_q,
341 midQ->mid_state != MID_REQUEST_SUBMITTED);
342 if (error < 0)
343 return -ERESTARTSYS;
345 return 0;
349 cifs_setup_async_request(struct TCP_Server_Info *server, struct kvec *iov,
350 unsigned int nvec, struct mid_q_entry **ret_mid)
352 int rc;
353 struct smb_hdr *hdr = (struct smb_hdr *)iov[0].iov_base;
354 struct mid_q_entry *mid;
356 /* enable signing if server requires it */
357 if (server->sec_mode & (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
358 hdr->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
360 mid = AllocMidQEntry(hdr, server);
361 if (mid == NULL)
362 return -ENOMEM;
364 rc = cifs_sign_smbv(iov, nvec, server, &mid->sequence_number);
365 if (rc) {
366 DeleteMidQEntry(mid);
367 return rc;
370 *ret_mid = mid;
371 return 0;
375 * Send a SMB request and set the callback function in the mid to handle
376 * the result. Caller is responsible for dealing with timeouts.
379 cifs_call_async(struct TCP_Server_Info *server, struct kvec *iov,
380 unsigned int nvec, mid_receive_t *receive,
381 mid_callback_t *callback, void *cbdata, const int flags)
383 int rc, timeout, optype;
384 struct mid_q_entry *mid;
386 timeout = flags & CIFS_TIMEOUT_MASK;
387 optype = flags & CIFS_OP_MASK;
389 rc = wait_for_free_request(server, timeout, optype);
390 if (rc)
391 return rc;
393 mutex_lock(&server->srv_mutex);
394 rc = server->ops->setup_async_request(server, iov, nvec, &mid);
395 if (rc) {
396 mutex_unlock(&server->srv_mutex);
397 add_credits(server, 1, optype);
398 wake_up(&server->request_q);
399 return rc;
402 mid->receive = receive;
403 mid->callback = callback;
404 mid->callback_data = cbdata;
405 mid->mid_state = MID_REQUEST_SUBMITTED;
407 /* put it on the pending_mid_q */
408 spin_lock(&GlobalMid_Lock);
409 list_add_tail(&mid->qhead, &server->pending_mid_q);
410 spin_unlock(&GlobalMid_Lock);
413 cifs_in_send_inc(server);
414 rc = smb_sendv(server, iov, nvec);
415 cifs_in_send_dec(server);
416 cifs_save_when_sent(mid);
417 mutex_unlock(&server->srv_mutex);
419 if (rc == 0)
420 return 0;
422 delete_mid(mid);
423 add_credits(server, 1, optype);
424 wake_up(&server->request_q);
425 return rc;
430 * Send an SMB Request. No response info (other than return code)
431 * needs to be parsed.
433 * flags indicate the type of request buffer and how long to wait
434 * and whether to log NT STATUS code (error) before mapping it to POSIX error
438 SendReceiveNoRsp(const unsigned int xid, struct cifs_ses *ses,
439 char *in_buf, int flags)
441 int rc;
442 struct kvec iov[1];
443 int resp_buf_type;
445 iov[0].iov_base = in_buf;
446 iov[0].iov_len = get_rfc1002_length(in_buf) + 4;
447 flags |= CIFS_NO_RESP;
448 rc = SendReceive2(xid, ses, iov, 1, &resp_buf_type, flags);
449 cFYI(DBG2, "SendRcvNoRsp flags %d rc %d", flags, rc);
451 return rc;
454 static int
455 cifs_sync_mid_result(struct mid_q_entry *mid, struct TCP_Server_Info *server)
457 int rc = 0;
459 cFYI(1, "%s: cmd=%d mid=%llu state=%d", __func__,
460 le16_to_cpu(mid->command), mid->mid, mid->mid_state);
462 spin_lock(&GlobalMid_Lock);
463 switch (mid->mid_state) {
464 case MID_RESPONSE_RECEIVED:
465 spin_unlock(&GlobalMid_Lock);
466 return rc;
467 case MID_RETRY_NEEDED:
468 rc = -EAGAIN;
469 break;
470 case MID_RESPONSE_MALFORMED:
471 rc = -EIO;
472 break;
473 case MID_SHUTDOWN:
474 rc = -EHOSTDOWN;
475 break;
476 default:
477 list_del_init(&mid->qhead);
478 cERROR(1, "%s: invalid mid state mid=%llu state=%d", __func__,
479 mid->mid, mid->mid_state);
480 rc = -EIO;
482 spin_unlock(&GlobalMid_Lock);
484 DeleteMidQEntry(mid);
485 return rc;
488 static inline int
489 send_cancel(struct TCP_Server_Info *server, void *buf, struct mid_q_entry *mid)
491 return server->ops->send_cancel ?
492 server->ops->send_cancel(server, buf, mid) : 0;
496 cifs_check_receive(struct mid_q_entry *mid, struct TCP_Server_Info *server,
497 bool log_error)
499 unsigned int len = get_rfc1002_length(mid->resp_buf) + 4;
501 dump_smb(mid->resp_buf, min_t(u32, 92, len));
503 /* convert the length into a more usable form */
504 if (server->sec_mode & (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED)) {
505 struct kvec iov;
506 int rc = 0;
508 iov.iov_base = mid->resp_buf;
509 iov.iov_len = len;
510 /* FIXME: add code to kill session */
511 rc = cifs_verify_signature(&iov, 1, server,
512 mid->sequence_number + 1);
513 if (rc)
514 cERROR(1, "SMB signature verification returned error = "
515 "%d", rc);
518 /* BB special case reconnect tid and uid here? */
519 return map_smb_to_linux_error(mid->resp_buf, log_error);
523 cifs_setup_request(struct cifs_ses *ses, struct kvec *iov,
524 unsigned int nvec, struct mid_q_entry **ret_mid)
526 int rc;
527 struct smb_hdr *hdr = (struct smb_hdr *)iov[0].iov_base;
528 struct mid_q_entry *mid;
530 rc = allocate_mid(ses, hdr, &mid);
531 if (rc)
532 return rc;
533 rc = cifs_sign_smbv(iov, nvec, ses->server, &mid->sequence_number);
534 if (rc)
535 delete_mid(mid);
536 *ret_mid = mid;
537 return rc;
541 SendReceive2(const unsigned int xid, struct cifs_ses *ses,
542 struct kvec *iov, int n_vec, int *resp_buf_type /* ret */,
543 const int flags)
545 int rc = 0;
546 int timeout, optype;
547 struct mid_q_entry *midQ;
548 char *buf = iov[0].iov_base;
549 unsigned int credits = 1;
551 timeout = flags & CIFS_TIMEOUT_MASK;
552 optype = flags & CIFS_OP_MASK;
554 *resp_buf_type = CIFS_NO_BUFFER; /* no response buf yet */
556 if ((ses == NULL) || (ses->server == NULL)) {
557 cifs_small_buf_release(buf);
558 cERROR(1, "Null session");
559 return -EIO;
562 if (ses->server->tcpStatus == CifsExiting) {
563 cifs_small_buf_release(buf);
564 return -ENOENT;
568 * Ensure that we do not send more than 50 overlapping requests
569 * to the same server. We may make this configurable later or
570 * use ses->maxReq.
573 rc = wait_for_free_request(ses->server, timeout, optype);
574 if (rc) {
575 cifs_small_buf_release(buf);
576 return rc;
580 * Make sure that we sign in the same order that we send on this socket
581 * and avoid races inside tcp sendmsg code that could cause corruption
582 * of smb data.
585 mutex_lock(&ses->server->srv_mutex);
587 rc = ses->server->ops->setup_request(ses, iov, n_vec, &midQ);
588 if (rc) {
589 mutex_unlock(&ses->server->srv_mutex);
590 cifs_small_buf_release(buf);
591 /* Update # of requests on wire to server */
592 add_credits(ses->server, 1, optype);
593 return rc;
596 midQ->mid_state = MID_REQUEST_SUBMITTED;
597 cifs_in_send_inc(ses->server);
598 rc = smb_sendv(ses->server, iov, n_vec);
599 cifs_in_send_dec(ses->server);
600 cifs_save_when_sent(midQ);
602 mutex_unlock(&ses->server->srv_mutex);
604 if (rc < 0) {
605 cifs_small_buf_release(buf);
606 goto out;
609 if (timeout == CIFS_ASYNC_OP) {
610 cifs_small_buf_release(buf);
611 goto out;
614 rc = wait_for_response(ses->server, midQ);
615 if (rc != 0) {
616 send_cancel(ses->server, buf, midQ);
617 spin_lock(&GlobalMid_Lock);
618 if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
619 midQ->callback = DeleteMidQEntry;
620 spin_unlock(&GlobalMid_Lock);
621 cifs_small_buf_release(buf);
622 add_credits(ses->server, 1, optype);
623 return rc;
625 spin_unlock(&GlobalMid_Lock);
628 cifs_small_buf_release(buf);
630 rc = cifs_sync_mid_result(midQ, ses->server);
631 if (rc != 0) {
632 add_credits(ses->server, 1, optype);
633 return rc;
636 if (!midQ->resp_buf || midQ->mid_state != MID_RESPONSE_RECEIVED) {
637 rc = -EIO;
638 cFYI(1, "Bad MID state?");
639 goto out;
642 buf = (char *)midQ->resp_buf;
643 iov[0].iov_base = buf;
644 iov[0].iov_len = get_rfc1002_length(buf) + 4;
645 if (midQ->large_buf)
646 *resp_buf_type = CIFS_LARGE_BUFFER;
647 else
648 *resp_buf_type = CIFS_SMALL_BUFFER;
650 credits = ses->server->ops->get_credits(midQ);
652 rc = ses->server->ops->check_receive(midQ, ses->server,
653 flags & CIFS_LOG_ERROR);
655 /* mark it so buf will not be freed by delete_mid */
656 if ((flags & CIFS_NO_RESP) == 0)
657 midQ->resp_buf = NULL;
658 out:
659 delete_mid(midQ);
660 add_credits(ses->server, credits, optype);
662 return rc;
666 SendReceive(const unsigned int xid, struct cifs_ses *ses,
667 struct smb_hdr *in_buf, struct smb_hdr *out_buf,
668 int *pbytes_returned, const int timeout)
670 int rc = 0;
671 struct mid_q_entry *midQ;
673 if (ses == NULL) {
674 cERROR(1, "Null smb session");
675 return -EIO;
677 if (ses->server == NULL) {
678 cERROR(1, "Null tcp session");
679 return -EIO;
682 if (ses->server->tcpStatus == CifsExiting)
683 return -ENOENT;
685 /* Ensure that we do not send more than 50 overlapping requests
686 to the same server. We may make this configurable later or
687 use ses->maxReq */
689 if (be32_to_cpu(in_buf->smb_buf_length) > CIFSMaxBufSize +
690 MAX_CIFS_HDR_SIZE - 4) {
691 cERROR(1, "Illegal length, greater than maximum frame, %d",
692 be32_to_cpu(in_buf->smb_buf_length));
693 return -EIO;
696 rc = wait_for_free_request(ses->server, timeout, 0);
697 if (rc)
698 return rc;
700 /* make sure that we sign in the same order that we send on this socket
701 and avoid races inside tcp sendmsg code that could cause corruption
702 of smb data */
704 mutex_lock(&ses->server->srv_mutex);
706 rc = allocate_mid(ses, in_buf, &midQ);
707 if (rc) {
708 mutex_unlock(&ses->server->srv_mutex);
709 /* Update # of requests on wire to server */
710 add_credits(ses->server, 1, 0);
711 return rc;
714 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
715 if (rc) {
716 mutex_unlock(&ses->server->srv_mutex);
717 goto out;
720 midQ->mid_state = MID_REQUEST_SUBMITTED;
722 cifs_in_send_inc(ses->server);
723 rc = smb_send(ses->server, in_buf, be32_to_cpu(in_buf->smb_buf_length));
724 cifs_in_send_dec(ses->server);
725 cifs_save_when_sent(midQ);
726 mutex_unlock(&ses->server->srv_mutex);
728 if (rc < 0)
729 goto out;
731 if (timeout == CIFS_ASYNC_OP)
732 goto out;
734 rc = wait_for_response(ses->server, midQ);
735 if (rc != 0) {
736 send_cancel(ses->server, in_buf, midQ);
737 spin_lock(&GlobalMid_Lock);
738 if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
739 /* no longer considered to be "in-flight" */
740 midQ->callback = DeleteMidQEntry;
741 spin_unlock(&GlobalMid_Lock);
742 add_credits(ses->server, 1, 0);
743 return rc;
745 spin_unlock(&GlobalMid_Lock);
748 rc = cifs_sync_mid_result(midQ, ses->server);
749 if (rc != 0) {
750 add_credits(ses->server, 1, 0);
751 return rc;
754 if (!midQ->resp_buf || !out_buf ||
755 midQ->mid_state != MID_RESPONSE_RECEIVED) {
756 rc = -EIO;
757 cERROR(1, "Bad MID state?");
758 goto out;
761 *pbytes_returned = get_rfc1002_length(midQ->resp_buf);
762 memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
763 rc = cifs_check_receive(midQ, ses->server, 0);
764 out:
765 delete_mid(midQ);
766 add_credits(ses->server, 1, 0);
768 return rc;
771 /* We send a LOCKINGX_CANCEL_LOCK to cause the Windows
772 blocking lock to return. */
774 static int
775 send_lock_cancel(const unsigned int xid, struct cifs_tcon *tcon,
776 struct smb_hdr *in_buf,
777 struct smb_hdr *out_buf)
779 int bytes_returned;
780 struct cifs_ses *ses = tcon->ses;
781 LOCK_REQ *pSMB = (LOCK_REQ *)in_buf;
783 /* We just modify the current in_buf to change
784 the type of lock from LOCKING_ANDX_SHARED_LOCK
785 or LOCKING_ANDX_EXCLUSIVE_LOCK to
786 LOCKING_ANDX_CANCEL_LOCK. */
788 pSMB->LockType = LOCKING_ANDX_CANCEL_LOCK|LOCKING_ANDX_LARGE_FILES;
789 pSMB->Timeout = 0;
790 pSMB->hdr.Mid = get_next_mid(ses->server);
792 return SendReceive(xid, ses, in_buf, out_buf,
793 &bytes_returned, 0);
797 SendReceiveBlockingLock(const unsigned int xid, struct cifs_tcon *tcon,
798 struct smb_hdr *in_buf, struct smb_hdr *out_buf,
799 int *pbytes_returned)
801 int rc = 0;
802 int rstart = 0;
803 struct mid_q_entry *midQ;
804 struct cifs_ses *ses;
806 if (tcon == NULL || tcon->ses == NULL) {
807 cERROR(1, "Null smb session");
808 return -EIO;
810 ses = tcon->ses;
812 if (ses->server == NULL) {
813 cERROR(1, "Null tcp session");
814 return -EIO;
817 if (ses->server->tcpStatus == CifsExiting)
818 return -ENOENT;
820 /* Ensure that we do not send more than 50 overlapping requests
821 to the same server. We may make this configurable later or
822 use ses->maxReq */
824 if (be32_to_cpu(in_buf->smb_buf_length) > CIFSMaxBufSize +
825 MAX_CIFS_HDR_SIZE - 4) {
826 cERROR(1, "Illegal length, greater than maximum frame, %d",
827 be32_to_cpu(in_buf->smb_buf_length));
828 return -EIO;
831 rc = wait_for_free_request(ses->server, CIFS_BLOCKING_OP, 0);
832 if (rc)
833 return rc;
835 /* make sure that we sign in the same order that we send on this socket
836 and avoid races inside tcp sendmsg code that could cause corruption
837 of smb data */
839 mutex_lock(&ses->server->srv_mutex);
841 rc = allocate_mid(ses, in_buf, &midQ);
842 if (rc) {
843 mutex_unlock(&ses->server->srv_mutex);
844 return rc;
847 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
848 if (rc) {
849 delete_mid(midQ);
850 mutex_unlock(&ses->server->srv_mutex);
851 return rc;
854 midQ->mid_state = MID_REQUEST_SUBMITTED;
855 cifs_in_send_inc(ses->server);
856 rc = smb_send(ses->server, in_buf, be32_to_cpu(in_buf->smb_buf_length));
857 cifs_in_send_dec(ses->server);
858 cifs_save_when_sent(midQ);
859 mutex_unlock(&ses->server->srv_mutex);
861 if (rc < 0) {
862 delete_mid(midQ);
863 return rc;
866 /* Wait for a reply - allow signals to interrupt. */
867 rc = wait_event_interruptible(ses->server->response_q,
868 (!(midQ->mid_state == MID_REQUEST_SUBMITTED)) ||
869 ((ses->server->tcpStatus != CifsGood) &&
870 (ses->server->tcpStatus != CifsNew)));
872 /* Were we interrupted by a signal ? */
873 if ((rc == -ERESTARTSYS) &&
874 (midQ->mid_state == MID_REQUEST_SUBMITTED) &&
875 ((ses->server->tcpStatus == CifsGood) ||
876 (ses->server->tcpStatus == CifsNew))) {
878 if (in_buf->Command == SMB_COM_TRANSACTION2) {
879 /* POSIX lock. We send a NT_CANCEL SMB to cause the
880 blocking lock to return. */
881 rc = send_cancel(ses->server, in_buf, midQ);
882 if (rc) {
883 delete_mid(midQ);
884 return rc;
886 } else {
887 /* Windows lock. We send a LOCKINGX_CANCEL_LOCK
888 to cause the blocking lock to return. */
890 rc = send_lock_cancel(xid, tcon, in_buf, out_buf);
892 /* If we get -ENOLCK back the lock may have
893 already been removed. Don't exit in this case. */
894 if (rc && rc != -ENOLCK) {
895 delete_mid(midQ);
896 return rc;
900 rc = wait_for_response(ses->server, midQ);
901 if (rc) {
902 send_cancel(ses->server, in_buf, midQ);
903 spin_lock(&GlobalMid_Lock);
904 if (midQ->mid_state == MID_REQUEST_SUBMITTED) {
905 /* no longer considered to be "in-flight" */
906 midQ->callback = DeleteMidQEntry;
907 spin_unlock(&GlobalMid_Lock);
908 return rc;
910 spin_unlock(&GlobalMid_Lock);
913 /* We got the response - restart system call. */
914 rstart = 1;
917 rc = cifs_sync_mid_result(midQ, ses->server);
918 if (rc != 0)
919 return rc;
921 /* rcvd frame is ok */
922 if (out_buf == NULL || midQ->mid_state != MID_RESPONSE_RECEIVED) {
923 rc = -EIO;
924 cERROR(1, "Bad MID state?");
925 goto out;
928 *pbytes_returned = get_rfc1002_length(midQ->resp_buf);
929 memcpy(out_buf, midQ->resp_buf, *pbytes_returned + 4);
930 rc = cifs_check_receive(midQ, ses->server, 0);
931 out:
932 delete_mid(midQ);
933 if (rstart && rc == -EACCES)
934 return -ERESTARTSYS;
935 return rc;