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 <asm/uaccess.h>
30 #include <asm/processor.h>
31 #include <linux/mempool.h>
34 #include "cifsproto.h"
35 #include "cifs_debug.h"
37 extern mempool_t
*cifs_mid_poolp
;
39 static struct mid_q_entry
*
40 AllocMidQEntry(const struct smb_hdr
*smb_buffer
, struct TCP_Server_Info
*server
)
42 struct mid_q_entry
*temp
;
45 cERROR(1, "Null TCP session in AllocMidQEntry");
49 temp
= mempool_alloc(cifs_mid_poolp
, GFP_NOFS
);
53 memset(temp
, 0, sizeof(struct mid_q_entry
));
54 temp
->mid
= smb_buffer
->Mid
; /* always LE */
55 temp
->pid
= current
->pid
;
56 temp
->command
= smb_buffer
->Command
;
57 cFYI(1, "For smb_command %d", temp
->command
);
58 /* do_gettimeofday(&temp->when_sent);*/ /* easier to use jiffies */
59 /* when mid allocated can be before when sent */
60 temp
->when_alloc
= jiffies
;
64 spin_lock(&GlobalMid_Lock
);
65 list_add_tail(&temp
->qhead
, &server
->pending_mid_q
);
66 atomic_inc(&midCount
);
67 temp
->midState
= MID_REQUEST_ALLOCATED
;
68 spin_unlock(&GlobalMid_Lock
);
73 DeleteMidQEntry(struct mid_q_entry
*midEntry
)
75 #ifdef CONFIG_CIFS_STATS2
78 spin_lock(&GlobalMid_Lock
);
79 midEntry
->midState
= MID_FREE
;
80 list_del(&midEntry
->qhead
);
81 atomic_dec(&midCount
);
82 spin_unlock(&GlobalMid_Lock
);
83 if (midEntry
->largeBuf
)
84 cifs_buf_release(midEntry
->resp_buf
);
86 cifs_small_buf_release(midEntry
->resp_buf
);
87 #ifdef CONFIG_CIFS_STATS2
89 /* commands taking longer than one second are indications that
90 something is wrong, unless it is quite a slow link or server */
91 if ((now
- midEntry
->when_alloc
) > HZ
) {
92 if ((cifsFYI
& CIFS_TIMER
) &&
93 (midEntry
->command
!= SMB_COM_LOCKING_ANDX
)) {
94 printk(KERN_DEBUG
" CIFS slow rsp: cmd %d mid %d",
95 midEntry
->command
, midEntry
->mid
);
96 printk(" A: 0x%lx S: 0x%lx R: 0x%lx\n",
97 now
- midEntry
->when_alloc
,
98 now
- midEntry
->when_sent
,
99 now
- midEntry
->when_received
);
103 mempool_free(midEntry
, cifs_mid_poolp
);
107 smb_sendv(struct TCP_Server_Info
*server
, struct kvec
*iov
, int n_vec
)
111 struct msghdr smb_msg
;
112 struct smb_hdr
*smb_buffer
= iov
[0].iov_base
;
113 unsigned int len
= iov
[0].iov_len
;
114 unsigned int total_len
;
116 unsigned int smb_buf_length
= smb_buffer
->smb_buf_length
;
117 struct socket
*ssocket
= server
->ssocket
;
120 return -ENOTSOCK
; /* BB eventually add reconnect code here */
122 smb_msg
.msg_name
= (struct sockaddr
*) &server
->addr
.sockAddr
;
123 smb_msg
.msg_namelen
= sizeof(struct sockaddr
);
124 smb_msg
.msg_control
= NULL
;
125 smb_msg
.msg_controllen
= 0;
126 if (server
->noblocksnd
)
127 smb_msg
.msg_flags
= MSG_DONTWAIT
+ MSG_NOSIGNAL
;
129 smb_msg
.msg_flags
= MSG_NOSIGNAL
;
131 /* smb header is converted in header_assemble. bcc and rest of SMB word
132 area, and byte area if necessary, is converted to littleendian in
133 cifssmb.c and RFC1001 len is converted to bigendian in smb_send
134 Flags2 is converted in SendReceive */
138 for (i
= 0; i
< n_vec
; i
++)
139 total_len
+= iov
[i
].iov_len
;
141 smb_buffer
->smb_buf_length
= cpu_to_be32(smb_buffer
->smb_buf_length
);
142 cFYI(1, "Sending smb: total_len %d", total_len
);
143 dump_smb(smb_buffer
, len
);
147 rc
= kernel_sendmsg(ssocket
, &smb_msg
, &iov
[first_vec
],
148 n_vec
- first_vec
, total_len
);
149 if ((rc
== -ENOSPC
) || (rc
== -EAGAIN
)) {
151 /* if blocking send we try 3 times, since each can block
152 for 5 seconds. For nonblocking we have to try more
153 but wait increasing amounts of time allowing time for
154 socket to clear. The overall time we wait in either
155 case to send on the socket is about 15 seconds.
156 Similarly we wait for 15 seconds for
157 a response from the server in SendReceive[2]
158 for the server to send a response back for
159 most types of requests (except SMB Write
160 past end of file which can be slow, and
161 blocking lock operations). NFS waits slightly longer
162 than CIFS, but this can make it take longer for
163 nonresponsive servers to be detected and 15 seconds
164 is more than enough time for modern networks to
165 send a packet. In most cases if we fail to send
166 after the retries we will kill the socket and
167 reconnect which may clear the network problem.
169 if ((i
>= 14) || (!server
->noblocksnd
&& (i
> 2))) {
170 cERROR(1, "sends on sock %p stuck for 15 seconds",
181 if (rc
== total_len
) {
184 } else if (rc
> total_len
) {
185 cERROR(1, "sent %d requested %d", rc
, total_len
);
189 /* should never happen, letting socket clear before
190 retrying is our only obvious option here */
191 cERROR(1, "tcp sent no data");
196 /* the line below resets i */
197 for (i
= first_vec
; i
< n_vec
; i
++) {
198 if (iov
[i
].iov_len
) {
199 if (rc
> iov
[i
].iov_len
) {
200 rc
-= iov
[i
].iov_len
;
203 iov
[i
].iov_base
+= rc
;
204 iov
[i
].iov_len
-= rc
;
210 i
= 0; /* in case we get ENOSPC on the next send */
213 if ((total_len
> 0) && (total_len
!= smb_buf_length
+ 4)) {
214 cFYI(1, "partial send (%d remaining), terminating session",
216 /* If we have only sent part of an SMB then the next SMB
217 could be taken as the remainder of this one. We need
218 to kill the socket so the server throws away the partial
220 server
->tcpStatus
= CifsNeedReconnect
;
224 cERROR(1, "Error %d sending data on socket to server", rc
);
228 /* Don't want to modify the buffer as a
229 side effect of this call. */
230 smb_buffer
->smb_buf_length
= smb_buf_length
;
236 smb_send(struct TCP_Server_Info
*server
, struct smb_hdr
*smb_buffer
,
237 unsigned int smb_buf_length
)
241 iov
.iov_base
= smb_buffer
;
242 iov
.iov_len
= smb_buf_length
+ 4;
244 return smb_sendv(server
, &iov
, 1);
247 static int wait_for_free_request(struct cifsSesInfo
*ses
, const int long_op
)
249 if (long_op
== CIFS_ASYNC_OP
) {
250 /* oplock breaks must not be held up */
251 atomic_inc(&ses
->server
->inFlight
);
255 spin_lock(&GlobalMid_Lock
);
257 if (atomic_read(&ses
->server
->inFlight
) >=
259 spin_unlock(&GlobalMid_Lock
);
260 #ifdef CONFIG_CIFS_STATS2
261 atomic_inc(&ses
->server
->num_waiters
);
263 wait_event(ses
->server
->request_q
,
264 atomic_read(&ses
->server
->inFlight
)
266 #ifdef CONFIG_CIFS_STATS2
267 atomic_dec(&ses
->server
->num_waiters
);
269 spin_lock(&GlobalMid_Lock
);
271 if (ses
->server
->tcpStatus
== CifsExiting
) {
272 spin_unlock(&GlobalMid_Lock
);
276 /* can not count locking commands against total
277 as they are allowed to block on server */
279 /* update # of requests on the wire to server */
280 if (long_op
!= CIFS_BLOCKING_OP
)
281 atomic_inc(&ses
->server
->inFlight
);
282 spin_unlock(&GlobalMid_Lock
);
289 static int allocate_mid(struct cifsSesInfo
*ses
, struct smb_hdr
*in_buf
,
290 struct mid_q_entry
**ppmidQ
)
292 if (ses
->server
->tcpStatus
== CifsExiting
) {
296 if (ses
->server
->tcpStatus
== CifsNeedReconnect
) {
297 cFYI(1, "tcp session dead - return to caller to retry");
301 if (ses
->status
!= CifsGood
) {
302 /* check if SMB session is bad because we are setting it up */
303 if ((in_buf
->Command
!= SMB_COM_SESSION_SETUP_ANDX
) &&
304 (in_buf
->Command
!= SMB_COM_NEGOTIATE
))
306 /* else ok - we are setting up session */
308 *ppmidQ
= AllocMidQEntry(in_buf
, ses
->server
);
314 static int wait_for_response(struct cifsSesInfo
*ses
,
315 struct mid_q_entry
*midQ
,
316 unsigned long timeout
,
317 unsigned long time_to_wait
)
319 unsigned long curr_timeout
;
322 curr_timeout
= timeout
+ jiffies
;
323 wait_event_timeout(ses
->server
->response_q
,
324 midQ
->midState
!= MID_REQUEST_SUBMITTED
, timeout
);
326 if (time_after(jiffies
, curr_timeout
) &&
327 (midQ
->midState
== MID_REQUEST_SUBMITTED
) &&
328 ((ses
->server
->tcpStatus
== CifsGood
) ||
329 (ses
->server
->tcpStatus
== CifsNew
))) {
333 /* We timed out. Is the server still
335 spin_lock(&GlobalMid_Lock
);
336 lrt
= ses
->server
->lstrp
;
337 spin_unlock(&GlobalMid_Lock
);
339 /* Calculate time_to_wait past last receive time.
340 Although we prefer not to time out if the
341 server is still responding - we will time
342 out if the server takes more than 15 (or 45
343 or 180) seconds to respond to this request
344 and has not responded to any request from
345 other threads on the client within 10 seconds */
347 if (time_after(jiffies
, lrt
)) {
348 /* No replies for time_to_wait. */
349 cERROR(1, "server not responding");
361 * Send an SMB Request. No response info (other than return code)
362 * needs to be parsed.
364 * flags indicate the type of request buffer and how long to wait
365 * and whether to log NT STATUS code (error) before mapping it to POSIX error
369 SendReceiveNoRsp(const unsigned int xid
, struct cifsSesInfo
*ses
,
370 struct smb_hdr
*in_buf
, int flags
)
376 iov
[0].iov_base
= (char *)in_buf
;
377 iov
[0].iov_len
= in_buf
->smb_buf_length
+ 4;
378 flags
|= CIFS_NO_RESP
;
379 rc
= SendReceive2(xid
, ses
, iov
, 1, &resp_buf_type
, flags
);
380 cFYI(DBG2
, "SendRcvNoRsp flags %d rc %d", flags
, rc
);
386 SendReceive2(const unsigned int xid
, struct cifsSesInfo
*ses
,
387 struct kvec
*iov
, int n_vec
, int *pRespBufType
/* ret */,
392 unsigned int receive_len
;
393 unsigned long timeout
;
394 struct mid_q_entry
*midQ
;
395 struct smb_hdr
*in_buf
= iov
[0].iov_base
;
397 long_op
= flags
& CIFS_TIMEOUT_MASK
;
399 *pRespBufType
= CIFS_NO_BUFFER
; /* no response buf yet */
401 if ((ses
== NULL
) || (ses
->server
== NULL
)) {
402 cifs_small_buf_release(in_buf
);
403 cERROR(1, "Null session");
407 if (ses
->server
->tcpStatus
== CifsExiting
) {
408 cifs_small_buf_release(in_buf
);
412 /* Ensure that we do not send more than 50 overlapping requests
413 to the same server. We may make this configurable later or
416 rc
= wait_for_free_request(ses
, long_op
);
418 cifs_small_buf_release(in_buf
);
422 /* make sure that we sign in the same order that we send on this socket
423 and avoid races inside tcp sendmsg code that could cause corruption
426 mutex_lock(&ses
->server
->srv_mutex
);
428 rc
= allocate_mid(ses
, in_buf
, &midQ
);
430 mutex_unlock(&ses
->server
->srv_mutex
);
431 cifs_small_buf_release(in_buf
);
432 /* Update # of requests on wire to server */
433 atomic_dec(&ses
->server
->inFlight
);
434 wake_up(&ses
->server
->request_q
);
437 rc
= cifs_sign_smb2(iov
, n_vec
, ses
->server
, &midQ
->sequence_number
);
439 mutex_unlock(&ses
->server
->srv_mutex
);
440 cifs_small_buf_release(in_buf
);
444 midQ
->midState
= MID_REQUEST_SUBMITTED
;
445 #ifdef CONFIG_CIFS_STATS2
446 atomic_inc(&ses
->server
->inSend
);
448 rc
= smb_sendv(ses
->server
, iov
, n_vec
);
449 #ifdef CONFIG_CIFS_STATS2
450 atomic_dec(&ses
->server
->inSend
);
451 midQ
->when_sent
= jiffies
;
454 mutex_unlock(&ses
->server
->srv_mutex
);
455 cifs_small_buf_release(in_buf
);
460 if (long_op
== CIFS_STD_OP
)
462 else if (long_op
== CIFS_VLONG_OP
) /* e.g. slow writes past EOF */
464 else if (long_op
== CIFS_LONG_OP
)
465 timeout
= 45 * HZ
; /* should be greater than
466 servers oplock break timeout (about 43 seconds) */
467 else if (long_op
== CIFS_ASYNC_OP
)
469 else if (long_op
== CIFS_BLOCKING_OP
)
470 timeout
= 0x7FFFFFFF; /* large, but not so large as to wrap */
472 cERROR(1, "unknown timeout flag %d", long_op
);
477 /* wait for 15 seconds or until woken up due to response arriving or
478 due to last connection to this server being unmounted */
479 if (signal_pending(current
)) {
480 /* if signal pending do not hold up user for full smb timeout
481 but we still give response a chance to complete */
485 /* No user interrupts in wait - wreaks havoc with performance */
486 wait_for_response(ses
, midQ
, timeout
, 10 * HZ
);
488 spin_lock(&GlobalMid_Lock
);
490 if (midQ
->resp_buf
== NULL
) {
491 cERROR(1, "No response to cmd %d mid %d",
492 midQ
->command
, midQ
->mid
);
493 if (midQ
->midState
== MID_REQUEST_SUBMITTED
) {
494 if (ses
->server
->tcpStatus
== CifsExiting
)
497 ses
->server
->tcpStatus
= CifsNeedReconnect
;
498 midQ
->midState
= MID_RETRY_NEEDED
;
502 if (rc
!= -EHOSTDOWN
) {
503 if (midQ
->midState
== MID_RETRY_NEEDED
) {
505 cFYI(1, "marking request for retry");
510 spin_unlock(&GlobalMid_Lock
);
511 DeleteMidQEntry(midQ
);
512 /* Update # of requests on wire to server */
513 atomic_dec(&ses
->server
->inFlight
);
514 wake_up(&ses
->server
->request_q
);
518 spin_unlock(&GlobalMid_Lock
);
519 receive_len
= midQ
->resp_buf
->smb_buf_length
;
521 if (receive_len
> CIFSMaxBufSize
+ MAX_CIFS_HDR_SIZE
) {
522 cERROR(1, "Frame too large received. Length: %d Xid: %d",
528 /* rcvd frame is ok */
530 if (midQ
->resp_buf
&&
531 (midQ
->midState
== MID_RESPONSE_RECEIVED
)) {
533 iov
[0].iov_base
= (char *)midQ
->resp_buf
;
535 *pRespBufType
= CIFS_LARGE_BUFFER
;
537 *pRespBufType
= CIFS_SMALL_BUFFER
;
538 iov
[0].iov_len
= receive_len
+ 4;
540 dump_smb(midQ
->resp_buf
, 80);
541 /* convert the length into a more usable form */
542 if ((receive_len
> 24) &&
543 (ses
->server
->secMode
& (SECMODE_SIGN_REQUIRED
|
544 SECMODE_SIGN_ENABLED
))) {
545 rc
= cifs_verify_signature(midQ
->resp_buf
,
546 &ses
->server
->mac_signing_key
,
547 midQ
->sequence_number
+1);
549 cERROR(1, "Unexpected SMB signature");
553 /* BB special case reconnect tid and uid here? */
554 rc
= map_smb_to_linux_error(midQ
->resp_buf
,
555 flags
& CIFS_LOG_ERROR
);
557 /* convert ByteCount if necessary */
558 if (receive_len
>= sizeof(struct smb_hdr
) - 4
559 /* do not count RFC1001 header */ +
560 (2 * midQ
->resp_buf
->WordCount
) + 2 /* bcc */ )
561 BCC(midQ
->resp_buf
) =
562 le16_to_cpu(BCC_LE(midQ
->resp_buf
));
563 if ((flags
& CIFS_NO_RESP
) == 0)
564 midQ
->resp_buf
= NULL
; /* mark it so buf will
569 cFYI(1, "Bad MID state?");
573 DeleteMidQEntry(midQ
);
574 atomic_dec(&ses
->server
->inFlight
);
575 wake_up(&ses
->server
->request_q
);
581 SendReceive(const unsigned int xid
, struct cifsSesInfo
*ses
,
582 struct smb_hdr
*in_buf
, struct smb_hdr
*out_buf
,
583 int *pbytes_returned
, const int long_op
)
586 unsigned int receive_len
;
587 unsigned long timeout
;
588 struct mid_q_entry
*midQ
;
591 cERROR(1, "Null smb session");
594 if (ses
->server
== NULL
) {
595 cERROR(1, "Null tcp session");
599 if (ses
->server
->tcpStatus
== CifsExiting
)
602 /* Ensure that we do not send more than 50 overlapping requests
603 to the same server. We may make this configurable later or
606 if (in_buf
->smb_buf_length
> CIFSMaxBufSize
+ MAX_CIFS_HDR_SIZE
- 4) {
607 cERROR(1, "Illegal length, greater than maximum frame, %d",
608 in_buf
->smb_buf_length
);
612 rc
= wait_for_free_request(ses
, long_op
);
616 /* make sure that we sign in the same order that we send on this socket
617 and avoid races inside tcp sendmsg code that could cause corruption
620 mutex_lock(&ses
->server
->srv_mutex
);
622 rc
= allocate_mid(ses
, in_buf
, &midQ
);
624 mutex_unlock(&ses
->server
->srv_mutex
);
625 /* Update # of requests on wire to server */
626 atomic_dec(&ses
->server
->inFlight
);
627 wake_up(&ses
->server
->request_q
);
631 rc
= cifs_sign_smb(in_buf
, ses
->server
, &midQ
->sequence_number
);
633 mutex_unlock(&ses
->server
->srv_mutex
);
637 midQ
->midState
= MID_REQUEST_SUBMITTED
;
638 #ifdef CONFIG_CIFS_STATS2
639 atomic_inc(&ses
->server
->inSend
);
641 rc
= smb_send(ses
->server
, in_buf
, in_buf
->smb_buf_length
);
642 #ifdef CONFIG_CIFS_STATS2
643 atomic_dec(&ses
->server
->inSend
);
644 midQ
->when_sent
= jiffies
;
646 mutex_unlock(&ses
->server
->srv_mutex
);
651 if (long_op
== CIFS_STD_OP
)
653 /* wait for 15 seconds or until woken up due to response arriving or
654 due to last connection to this server being unmounted */
655 else if (long_op
== CIFS_ASYNC_OP
)
657 else if (long_op
== CIFS_VLONG_OP
) /* writes past EOF can be slow */
659 else if (long_op
== CIFS_LONG_OP
)
660 timeout
= 45 * HZ
; /* should be greater than
661 servers oplock break timeout (about 43 seconds) */
662 else if (long_op
== CIFS_BLOCKING_OP
)
663 timeout
= 0x7FFFFFFF; /* large but no so large as to wrap */
665 cERROR(1, "unknown timeout flag %d", long_op
);
670 if (signal_pending(current
)) {
671 /* if signal pending do not hold up user for full smb timeout
672 but we still give response a chance to complete */
676 /* No user interrupts in wait - wreaks havoc with performance */
677 wait_for_response(ses
, midQ
, timeout
, 10 * HZ
);
679 spin_lock(&GlobalMid_Lock
);
680 if (midQ
->resp_buf
== NULL
) {
681 cERROR(1, "No response for cmd %d mid %d",
682 midQ
->command
, midQ
->mid
);
683 if (midQ
->midState
== MID_REQUEST_SUBMITTED
) {
684 if (ses
->server
->tcpStatus
== CifsExiting
)
687 ses
->server
->tcpStatus
= CifsNeedReconnect
;
688 midQ
->midState
= MID_RETRY_NEEDED
;
692 if (rc
!= -EHOSTDOWN
) {
693 if (midQ
->midState
== MID_RETRY_NEEDED
) {
695 cFYI(1, "marking request for retry");
700 spin_unlock(&GlobalMid_Lock
);
701 DeleteMidQEntry(midQ
);
702 /* Update # of requests on wire to server */
703 atomic_dec(&ses
->server
->inFlight
);
704 wake_up(&ses
->server
->request_q
);
708 spin_unlock(&GlobalMid_Lock
);
709 receive_len
= midQ
->resp_buf
->smb_buf_length
;
711 if (receive_len
> CIFSMaxBufSize
+ MAX_CIFS_HDR_SIZE
) {
712 cERROR(1, "Frame too large received. Length: %d Xid: %d",
718 /* rcvd frame is ok */
720 if (midQ
->resp_buf
&& out_buf
721 && (midQ
->midState
== MID_RESPONSE_RECEIVED
)) {
722 out_buf
->smb_buf_length
= receive_len
;
723 memcpy((char *)out_buf
+ 4,
724 (char *)midQ
->resp_buf
+ 4,
727 dump_smb(out_buf
, 92);
728 /* convert the length into a more usable form */
729 if ((receive_len
> 24) &&
730 (ses
->server
->secMode
& (SECMODE_SIGN_REQUIRED
|
731 SECMODE_SIGN_ENABLED
))) {
732 rc
= cifs_verify_signature(out_buf
,
733 &ses
->server
->mac_signing_key
,
734 midQ
->sequence_number
+1);
736 cERROR(1, "Unexpected SMB signature");
740 *pbytes_returned
= out_buf
->smb_buf_length
;
742 /* BB special case reconnect tid and uid here? */
743 rc
= map_smb_to_linux_error(out_buf
, 0 /* no log */ );
745 /* convert ByteCount if necessary */
746 if (receive_len
>= sizeof(struct smb_hdr
) - 4
747 /* do not count RFC1001 header */ +
748 (2 * out_buf
->WordCount
) + 2 /* bcc */ )
749 BCC(out_buf
) = le16_to_cpu(BCC_LE(out_buf
));
752 cERROR(1, "Bad MID state?");
756 DeleteMidQEntry(midQ
);
757 atomic_dec(&ses
->server
->inFlight
);
758 wake_up(&ses
->server
->request_q
);
763 /* Send an NT_CANCEL SMB to cause the POSIX blocking lock to return. */
766 send_nt_cancel(struct cifsTconInfo
*tcon
, struct smb_hdr
*in_buf
,
767 struct mid_q_entry
*midQ
)
770 struct cifsSesInfo
*ses
= tcon
->ses
;
771 __u16 mid
= in_buf
->Mid
;
773 header_assemble(in_buf
, SMB_COM_NT_CANCEL
, tcon
, 0);
775 mutex_lock(&ses
->server
->srv_mutex
);
776 rc
= cifs_sign_smb(in_buf
, ses
->server
, &midQ
->sequence_number
);
778 mutex_unlock(&ses
->server
->srv_mutex
);
781 rc
= smb_send(ses
->server
, in_buf
, in_buf
->smb_buf_length
);
782 mutex_unlock(&ses
->server
->srv_mutex
);
786 /* We send a LOCKINGX_CANCEL_LOCK to cause the Windows
787 blocking lock to return. */
790 send_lock_cancel(const unsigned int xid
, struct cifsTconInfo
*tcon
,
791 struct smb_hdr
*in_buf
,
792 struct smb_hdr
*out_buf
)
795 struct cifsSesInfo
*ses
= tcon
->ses
;
796 LOCK_REQ
*pSMB
= (LOCK_REQ
*)in_buf
;
798 /* We just modify the current in_buf to change
799 the type of lock from LOCKING_ANDX_SHARED_LOCK
800 or LOCKING_ANDX_EXCLUSIVE_LOCK to
801 LOCKING_ANDX_CANCEL_LOCK. */
803 pSMB
->LockType
= LOCKING_ANDX_CANCEL_LOCK
|LOCKING_ANDX_LARGE_FILES
;
805 pSMB
->hdr
.Mid
= GetNextMid(ses
->server
);
807 return SendReceive(xid
, ses
, in_buf
, out_buf
,
808 &bytes_returned
, CIFS_STD_OP
);
812 SendReceiveBlockingLock(const unsigned int xid
, struct cifsTconInfo
*tcon
,
813 struct smb_hdr
*in_buf
, struct smb_hdr
*out_buf
,
814 int *pbytes_returned
)
818 unsigned int receive_len
;
819 struct mid_q_entry
*midQ
;
820 struct cifsSesInfo
*ses
;
822 if (tcon
== NULL
|| tcon
->ses
== NULL
) {
823 cERROR(1, "Null smb session");
828 if (ses
->server
== NULL
) {
829 cERROR(1, "Null tcp session");
833 if (ses
->server
->tcpStatus
== CifsExiting
)
836 /* Ensure that we do not send more than 50 overlapping requests
837 to the same server. We may make this configurable later or
840 if (in_buf
->smb_buf_length
> CIFSMaxBufSize
+ MAX_CIFS_HDR_SIZE
- 4) {
841 cERROR(1, "Illegal length, greater than maximum frame, %d",
842 in_buf
->smb_buf_length
);
846 rc
= wait_for_free_request(ses
, CIFS_BLOCKING_OP
);
850 /* make sure that we sign in the same order that we send on this socket
851 and avoid races inside tcp sendmsg code that could cause corruption
854 mutex_lock(&ses
->server
->srv_mutex
);
856 rc
= allocate_mid(ses
, in_buf
, &midQ
);
858 mutex_unlock(&ses
->server
->srv_mutex
);
862 rc
= cifs_sign_smb(in_buf
, ses
->server
, &midQ
->sequence_number
);
864 DeleteMidQEntry(midQ
);
865 mutex_unlock(&ses
->server
->srv_mutex
);
869 midQ
->midState
= MID_REQUEST_SUBMITTED
;
870 #ifdef CONFIG_CIFS_STATS2
871 atomic_inc(&ses
->server
->inSend
);
873 rc
= smb_send(ses
->server
, in_buf
, in_buf
->smb_buf_length
);
874 #ifdef CONFIG_CIFS_STATS2
875 atomic_dec(&ses
->server
->inSend
);
876 midQ
->when_sent
= jiffies
;
878 mutex_unlock(&ses
->server
->srv_mutex
);
881 DeleteMidQEntry(midQ
);
885 /* Wait for a reply - allow signals to interrupt. */
886 rc
= wait_event_interruptible(ses
->server
->response_q
,
887 (!(midQ
->midState
== MID_REQUEST_SUBMITTED
)) ||
888 ((ses
->server
->tcpStatus
!= CifsGood
) &&
889 (ses
->server
->tcpStatus
!= CifsNew
)));
891 /* Were we interrupted by a signal ? */
892 if ((rc
== -ERESTARTSYS
) &&
893 (midQ
->midState
== MID_REQUEST_SUBMITTED
) &&
894 ((ses
->server
->tcpStatus
== CifsGood
) ||
895 (ses
->server
->tcpStatus
== CifsNew
))) {
897 if (in_buf
->Command
== SMB_COM_TRANSACTION2
) {
898 /* POSIX lock. We send a NT_CANCEL SMB to cause the
899 blocking lock to return. */
901 rc
= send_nt_cancel(tcon
, in_buf
, midQ
);
903 DeleteMidQEntry(midQ
);
907 /* Windows lock. We send a LOCKINGX_CANCEL_LOCK
908 to cause the blocking lock to return. */
910 rc
= send_lock_cancel(xid
, tcon
, in_buf
, out_buf
);
912 /* If we get -ENOLCK back the lock may have
913 already been removed. Don't exit in this case. */
914 if (rc
&& rc
!= -ENOLCK
) {
915 DeleteMidQEntry(midQ
);
920 /* Wait 5 seconds for the response. */
921 if (wait_for_response(ses
, midQ
, 5 * HZ
, 5 * HZ
) == 0) {
922 /* We got the response - restart system call. */
927 spin_lock(&GlobalMid_Lock
);
928 if (midQ
->resp_buf
) {
929 spin_unlock(&GlobalMid_Lock
);
930 receive_len
= midQ
->resp_buf
->smb_buf_length
;
932 cERROR(1, "No response for cmd %d mid %d",
933 midQ
->command
, midQ
->mid
);
934 if (midQ
->midState
== MID_REQUEST_SUBMITTED
) {
935 if (ses
->server
->tcpStatus
== CifsExiting
)
938 ses
->server
->tcpStatus
= CifsNeedReconnect
;
939 midQ
->midState
= MID_RETRY_NEEDED
;
943 if (rc
!= -EHOSTDOWN
) {
944 if (midQ
->midState
== MID_RETRY_NEEDED
) {
946 cFYI(1, "marking request for retry");
951 spin_unlock(&GlobalMid_Lock
);
952 DeleteMidQEntry(midQ
);
956 if (receive_len
> CIFSMaxBufSize
+ MAX_CIFS_HDR_SIZE
) {
957 cERROR(1, "Frame too large received. Length: %d Xid: %d",
963 /* rcvd frame is ok */
965 if ((out_buf
== NULL
) || (midQ
->midState
!= MID_RESPONSE_RECEIVED
)) {
967 cERROR(1, "Bad MID state?");
971 out_buf
->smb_buf_length
= receive_len
;
972 memcpy((char *)out_buf
+ 4,
973 (char *)midQ
->resp_buf
+ 4,
976 dump_smb(out_buf
, 92);
977 /* convert the length into a more usable form */
978 if ((receive_len
> 24) &&
979 (ses
->server
->secMode
& (SECMODE_SIGN_REQUIRED
|
980 SECMODE_SIGN_ENABLED
))) {
981 rc
= cifs_verify_signature(out_buf
,
982 &ses
->server
->mac_signing_key
,
983 midQ
->sequence_number
+1);
985 cERROR(1, "Unexpected SMB signature");
989 *pbytes_returned
= out_buf
->smb_buf_length
;
991 /* BB special case reconnect tid and uid here? */
992 rc
= map_smb_to_linux_error(out_buf
, 0 /* no log */ );
994 /* convert ByteCount if necessary */
995 if (receive_len
>= sizeof(struct smb_hdr
) - 4
996 /* do not count RFC1001 header */ +
997 (2 * out_buf
->WordCount
) + 2 /* bcc */ )
998 BCC(out_buf
) = le16_to_cpu(BCC_LE(out_buf
));
1001 DeleteMidQEntry(midQ
);
1002 if (rstart
&& rc
== -EACCES
)
1003 return -ERESTARTSYS
;