4 * Copyright (C) International Business Machines Corp., 2002,2007
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/wait.h>
26 #include <linux/net.h>
27 #include <linux/delay.h>
28 #include <asm/uaccess.h>
29 #include <asm/processor.h>
30 #include <linux/mempool.h>
33 #include "cifsproto.h"
34 #include "cifs_debug.h"
36 extern mempool_t
*cifs_mid_poolp
;
37 extern struct kmem_cache
*cifs_oplock_cachep
;
39 static struct mid_q_entry
*
40 AllocMidQEntry(const struct smb_hdr
*smb_buffer
, struct cifsSesInfo
*ses
)
42 struct mid_q_entry
*temp
;
45 cERROR(1, ("Null session passed in to AllocMidQEntry"));
48 if (ses
->server
== NULL
) {
49 cERROR(1, ("Null TCP session in AllocMidQEntry"));
53 temp
= (struct mid_q_entry
*) mempool_alloc(cifs_mid_poolp
,
54 GFP_KERNEL
| GFP_NOFS
);
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
= smb_buffer
->Command
;
62 cFYI(1, ("For smb_command %d", temp
->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
;
70 spin_lock(&GlobalMid_Lock
);
71 list_add_tail(&temp
->qhead
, &ses
->server
->pending_mid_q
);
72 atomic_inc(&midCount
);
73 temp
->midState
= MID_REQUEST_ALLOCATED
;
74 spin_unlock(&GlobalMid_Lock
);
79 DeleteMidQEntry(struct mid_q_entry
*midEntry
)
81 #ifdef CONFIG_CIFS_STATS2
84 spin_lock(&GlobalMid_Lock
);
85 midEntry
->midState
= MID_FREE
;
86 list_del(&midEntry
->qhead
);
87 atomic_dec(&midCount
);
88 spin_unlock(&GlobalMid_Lock
);
89 if (midEntry
->largeBuf
)
90 cifs_buf_release(midEntry
->resp_buf
);
92 cifs_small_buf_release(midEntry
->resp_buf
);
93 #ifdef CONFIG_CIFS_STATS2
95 /* commands taking longer than one second are indications that
96 something is wrong, unless it is quite a slow link or server */
97 if ((now
- midEntry
->when_alloc
) > HZ
) {
98 if ((cifsFYI
& CIFS_TIMER
) &&
99 (midEntry
->command
!= SMB_COM_LOCKING_ANDX
)) {
100 printk(KERN_DEBUG
" CIFS slow rsp: cmd %d mid %d",
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
);
109 mempool_free(midEntry
, cifs_mid_poolp
);
112 struct oplock_q_entry
*
113 AllocOplockQEntry(struct inode
*pinode
, __u16 fid
, struct cifsTconInfo
*tcon
)
115 struct oplock_q_entry
*temp
;
116 if ((pinode
== NULL
) || (tcon
== NULL
)) {
117 cERROR(1, ("Null parms passed to AllocOplockQEntry"));
120 temp
= (struct oplock_q_entry
*) kmem_cache_alloc(cifs_oplock_cachep
,
125 temp
->pinode
= pinode
;
128 spin_lock(&GlobalMid_Lock
);
129 list_add_tail(&temp
->qhead
, &GlobalOplock_Q
);
130 spin_unlock(&GlobalMid_Lock
);
136 void DeleteOplockQEntry(struct oplock_q_entry
*oplockEntry
)
138 spin_lock(&GlobalMid_Lock
);
139 /* should we check if list empty first? */
140 list_del(&oplockEntry
->qhead
);
141 spin_unlock(&GlobalMid_Lock
);
142 kmem_cache_free(cifs_oplock_cachep
, oplockEntry
);
146 smb_send(struct socket
*ssocket
, struct smb_hdr
*smb_buffer
,
147 unsigned int smb_buf_length
, struct sockaddr
*sin
)
151 struct msghdr smb_msg
;
153 unsigned len
= smb_buf_length
+ 4;
156 return -ENOTSOCK
; /* BB eventually add reconnect code here */
157 iov
.iov_base
= smb_buffer
;
160 smb_msg
.msg_name
= sin
;
161 smb_msg
.msg_namelen
= sizeof(struct sockaddr
);
162 smb_msg
.msg_control
= NULL
;
163 smb_msg
.msg_controllen
= 0;
164 smb_msg
.msg_flags
= MSG_DONTWAIT
+ MSG_NOSIGNAL
; /* BB add more flags?*/
166 /* smb header is converted in header_assemble. bcc and rest of SMB word
167 area, and byte area if necessary, is converted to littleendian in
168 cifssmb.c and RFC1001 len is converted to bigendian in smb_send
169 Flags2 is converted in SendReceive */
171 smb_buffer
->smb_buf_length
= cpu_to_be32(smb_buffer
->smb_buf_length
);
172 cFYI(1, ("Sending smb of length %d", smb_buf_length
));
173 dump_smb(smb_buffer
, len
);
176 rc
= kernel_sendmsg(ssocket
, &smb_msg
, &iov
, 1, len
);
177 if ((rc
== -ENOSPC
) || (rc
== -EAGAIN
)) {
179 /* smaller timeout here than send2 since smaller size */
180 /* Although it may not be required, this also is smaller
184 ("sends on sock %p stuck for 7 seconds",
195 i
= 0; /* reset i after each successful send */
202 cERROR(1, ("Error %d sending data on socket to server", rc
));
207 /* Don't want to modify the buffer as a
208 side effect of this call. */
209 smb_buffer
->smb_buf_length
= smb_buf_length
;
215 smb_send2(struct socket
*ssocket
, struct kvec
*iov
, int n_vec
,
216 struct sockaddr
*sin
)
220 struct msghdr smb_msg
;
221 struct smb_hdr
*smb_buffer
= iov
[0].iov_base
;
222 unsigned int len
= iov
[0].iov_len
;
223 unsigned int total_len
;
225 unsigned int smb_buf_length
= smb_buffer
->smb_buf_length
;
228 return -ENOTSOCK
; /* BB eventually add reconnect code here */
230 smb_msg
.msg_name
= sin
;
231 smb_msg
.msg_namelen
= sizeof(struct sockaddr
);
232 smb_msg
.msg_control
= NULL
;
233 smb_msg
.msg_controllen
= 0;
234 smb_msg
.msg_flags
= MSG_DONTWAIT
+ MSG_NOSIGNAL
; /* BB add more flags?*/
236 /* smb header is converted in header_assemble. bcc and rest of SMB word
237 area, and byte area if necessary, is converted to littleendian in
238 cifssmb.c and RFC1001 len is converted to bigendian in smb_send
239 Flags2 is converted in SendReceive */
243 for (i
= 0; i
< n_vec
; i
++)
244 total_len
+= iov
[i
].iov_len
;
246 smb_buffer
->smb_buf_length
= cpu_to_be32(smb_buffer
->smb_buf_length
);
247 cFYI(1, ("Sending smb: total_len %d", total_len
));
248 dump_smb(smb_buffer
, len
);
251 rc
= kernel_sendmsg(ssocket
, &smb_msg
, &iov
[first_vec
],
252 n_vec
- first_vec
, total_len
);
253 if ((rc
== -ENOSPC
) || (rc
== -EAGAIN
)) {
257 ("sends on sock %p stuck for 15 seconds",
268 if (rc
>= total_len
) {
269 WARN_ON(rc
> total_len
);
273 /* should never happen, letting socket clear before
274 retrying is our only obvious option here */
275 cERROR(1, ("tcp sent no data"));
280 /* the line below resets i */
281 for (i
= first_vec
; i
< n_vec
; i
++) {
282 if (iov
[i
].iov_len
) {
283 if (rc
> iov
[i
].iov_len
) {
284 rc
-= iov
[i
].iov_len
;
287 iov
[i
].iov_base
+= rc
;
288 iov
[i
].iov_len
-= rc
;
294 i
= 0; /* in case we get ENOSPC on the next send */
298 cERROR(1, ("Error %d sending data on socket to server", rc
));
302 /* Don't want to modify the buffer as a
303 side effect of this call. */
304 smb_buffer
->smb_buf_length
= smb_buf_length
;
309 static int wait_for_free_request(struct cifsSesInfo
*ses
, const int long_op
)
311 if (long_op
== CIFS_ASYNC_OP
) {
312 /* oplock breaks must not be held up */
313 atomic_inc(&ses
->server
->inFlight
);
315 spin_lock(&GlobalMid_Lock
);
317 if (atomic_read(&ses
->server
->inFlight
) >=
319 spin_unlock(&GlobalMid_Lock
);
320 #ifdef CONFIG_CIFS_STATS2
321 atomic_inc(&ses
->server
->num_waiters
);
323 wait_event(ses
->server
->request_q
,
324 atomic_read(&ses
->server
->inFlight
)
326 #ifdef CONFIG_CIFS_STATS2
327 atomic_dec(&ses
->server
->num_waiters
);
329 spin_lock(&GlobalMid_Lock
);
331 if (ses
->server
->tcpStatus
== CifsExiting
) {
332 spin_unlock(&GlobalMid_Lock
);
336 /* can not count locking commands against total
337 as they are allowed to block on server */
339 /* update # of requests on the wire to server */
340 if (long_op
!= CIFS_BLOCKING_OP
)
341 atomic_inc(&ses
->server
->inFlight
);
342 spin_unlock(&GlobalMid_Lock
);
350 static int allocate_mid(struct cifsSesInfo
*ses
, struct smb_hdr
*in_buf
,
351 struct mid_q_entry
**ppmidQ
)
353 if (ses
->server
->tcpStatus
== CifsExiting
) {
355 } else if (ses
->server
->tcpStatus
== CifsNeedReconnect
) {
356 cFYI(1, ("tcp session dead - return to caller to retry"));
358 } else if (ses
->status
!= CifsGood
) {
359 /* check if SMB session is bad because we are setting it up */
360 if ((in_buf
->Command
!= SMB_COM_SESSION_SETUP_ANDX
) &&
361 (in_buf
->Command
!= SMB_COM_NEGOTIATE
)) {
363 } /* else ok - we are setting up session */
365 *ppmidQ
= AllocMidQEntry(in_buf
, ses
);
371 static int wait_for_response(struct cifsSesInfo
*ses
,
372 struct mid_q_entry
*midQ
,
373 unsigned long timeout
,
374 unsigned long time_to_wait
)
376 unsigned long curr_timeout
;
379 curr_timeout
= timeout
+ jiffies
;
380 wait_event(ses
->server
->response_q
,
381 (!(midQ
->midState
== MID_REQUEST_SUBMITTED
)) ||
382 time_after(jiffies
, curr_timeout
) ||
383 ((ses
->server
->tcpStatus
!= CifsGood
) &&
384 (ses
->server
->tcpStatus
!= CifsNew
)));
386 if (time_after(jiffies
, curr_timeout
) &&
387 (midQ
->midState
== MID_REQUEST_SUBMITTED
) &&
388 ((ses
->server
->tcpStatus
== CifsGood
) ||
389 (ses
->server
->tcpStatus
== CifsNew
))) {
393 /* We timed out. Is the server still
395 spin_lock(&GlobalMid_Lock
);
396 lrt
= ses
->server
->lstrp
;
397 spin_unlock(&GlobalMid_Lock
);
399 /* Calculate time_to_wait past last receive time.
400 Although we prefer not to time out if the
401 server is still responding - we will time
402 out if the server takes more than 15 (or 45
403 or 180) seconds to respond to this request
404 and has not responded to any request from
405 other threads on the client within 10 seconds */
407 if (time_after(jiffies
, lrt
)) {
408 /* No replies for time_to_wait. */
409 cERROR(1, ("server not responding"));
421 * Send an SMB Request. No response info (other than return code)
422 * needs to be parsed.
424 * flags indicate the type of request buffer and how long to wait
425 * and whether to log NT STATUS code (error) before mapping it to POSIX error
429 SendReceiveNoRsp(const unsigned int xid
, struct cifsSesInfo
*ses
,
430 struct smb_hdr
*in_buf
, int flags
)
436 iov
[0].iov_base
= (char *)in_buf
;
437 iov
[0].iov_len
= in_buf
->smb_buf_length
+ 4;
438 flags
|= CIFS_NO_RESP
;
439 rc
= SendReceive2(xid
, ses
, iov
, 1, &resp_buf_type
, flags
);
440 #ifdef CONFIG_CIFS_DEBUG2
441 cFYI(1, ("SendRcvNoR flags %d rc %d", flags
, rc
));
447 SendReceive2(const unsigned int xid
, struct cifsSesInfo
*ses
,
448 struct kvec
*iov
, int n_vec
, int *pRespBufType
/* ret */,
453 unsigned int receive_len
;
454 unsigned long timeout
;
455 struct mid_q_entry
*midQ
;
456 struct smb_hdr
*in_buf
= iov
[0].iov_base
;
458 long_op
= flags
& CIFS_TIMEOUT_MASK
;
460 *pRespBufType
= CIFS_NO_BUFFER
; /* no response buf yet */
462 if ((ses
== NULL
) || (ses
->server
== NULL
)) {
463 cifs_small_buf_release(in_buf
);
464 cERROR(1, ("Null session"));
468 if (ses
->server
->tcpStatus
== CifsExiting
) {
469 cifs_small_buf_release(in_buf
);
473 /* Ensure that we do not send more than 50 overlapping requests
474 to the same server. We may make this configurable later or
477 rc
= wait_for_free_request(ses
, long_op
);
479 cifs_small_buf_release(in_buf
);
483 /* make sure that we sign in the same order that we send on this socket
484 and avoid races inside tcp sendmsg code that could cause corruption
487 down(&ses
->server
->tcpSem
);
489 rc
= allocate_mid(ses
, in_buf
, &midQ
);
491 up(&ses
->server
->tcpSem
);
492 cifs_small_buf_release(in_buf
);
493 /* Update # of requests on wire to server */
494 atomic_dec(&ses
->server
->inFlight
);
495 wake_up(&ses
->server
->request_q
);
498 rc
= cifs_sign_smb2(iov
, n_vec
, ses
->server
, &midQ
->sequence_number
);
500 midQ
->midState
= MID_REQUEST_SUBMITTED
;
501 #ifdef CONFIG_CIFS_STATS2
502 atomic_inc(&ses
->server
->inSend
);
504 rc
= smb_send2(ses
->server
->ssocket
, iov
, n_vec
,
505 (struct sockaddr
*) &(ses
->server
->addr
.sockAddr
));
506 #ifdef CONFIG_CIFS_STATS2
507 atomic_dec(&ses
->server
->inSend
);
508 midQ
->when_sent
= jiffies
;
511 up(&ses
->server
->tcpSem
);
512 cifs_small_buf_release(in_buf
);
517 if (long_op
== CIFS_STD_OP
)
519 else if (long_op
== CIFS_VLONG_OP
) /* e.g. slow writes past EOF */
521 else if (long_op
== CIFS_LONG_OP
)
522 timeout
= 45 * HZ
; /* should be greater than
523 servers oplock break timeout (about 43 seconds) */
524 else if (long_op
== CIFS_ASYNC_OP
)
526 else if (long_op
== CIFS_BLOCKING_OP
)
527 timeout
= 0x7FFFFFFF; /* large, but not so large as to wrap */
529 cERROR(1, ("unknown timeout flag %d", long_op
));
534 /* wait for 15 seconds or until woken up due to response arriving or
535 due to last connection to this server being unmounted */
536 if (signal_pending(current
)) {
537 /* if signal pending do not hold up user for full smb timeout
538 but we still give response a chance to complete */
542 /* No user interrupts in wait - wreaks havoc with performance */
543 wait_for_response(ses
, midQ
, timeout
, 10 * HZ
);
545 spin_lock(&GlobalMid_Lock
);
546 if (midQ
->resp_buf
) {
547 spin_unlock(&GlobalMid_Lock
);
548 receive_len
= midQ
->resp_buf
->smb_buf_length
;
550 cERROR(1, ("No response to cmd %d mid %d",
551 midQ
->command
, midQ
->mid
));
552 if (midQ
->midState
== MID_REQUEST_SUBMITTED
) {
553 if (ses
->server
->tcpStatus
== CifsExiting
)
556 ses
->server
->tcpStatus
= CifsNeedReconnect
;
557 midQ
->midState
= MID_RETRY_NEEDED
;
561 if (rc
!= -EHOSTDOWN
) {
562 if (midQ
->midState
== MID_RETRY_NEEDED
) {
564 cFYI(1, ("marking request for retry"));
569 spin_unlock(&GlobalMid_Lock
);
570 DeleteMidQEntry(midQ
);
571 /* Update # of requests on wire to server */
572 atomic_dec(&ses
->server
->inFlight
);
573 wake_up(&ses
->server
->request_q
);
577 if (receive_len
> CIFSMaxBufSize
+ MAX_CIFS_HDR_SIZE
) {
578 cERROR(1, ("Frame too large received. Length: %d Xid: %d",
581 } else { /* rcvd frame is ok */
582 if (midQ
->resp_buf
&&
583 (midQ
->midState
== MID_RESPONSE_RECEIVED
)) {
585 iov
[0].iov_base
= (char *)midQ
->resp_buf
;
587 *pRespBufType
= CIFS_LARGE_BUFFER
;
589 *pRespBufType
= CIFS_SMALL_BUFFER
;
590 iov
[0].iov_len
= receive_len
+ 4;
592 dump_smb(midQ
->resp_buf
, 80);
593 /* convert the length into a more usable form */
594 if ((receive_len
> 24) &&
595 (ses
->server
->secMode
& (SECMODE_SIGN_REQUIRED
|
596 SECMODE_SIGN_ENABLED
))) {
597 rc
= cifs_verify_signature(midQ
->resp_buf
,
598 &ses
->server
->mac_signing_key
,
599 midQ
->sequence_number
+1);
601 cERROR(1, ("Unexpected SMB signature"));
602 /* BB FIXME add code to kill session */
606 /* BB special case reconnect tid and uid here? */
607 rc
= map_smb_to_linux_error(midQ
->resp_buf
,
608 flags
& CIFS_LOG_ERROR
);
610 /* convert ByteCount if necessary */
611 if (receive_len
>= sizeof(struct smb_hdr
) - 4
612 /* do not count RFC1001 header */ +
613 (2 * midQ
->resp_buf
->WordCount
) + 2 /* bcc */ )
614 BCC(midQ
->resp_buf
) =
615 le16_to_cpu(BCC_LE(midQ
->resp_buf
));
616 if ((flags
& CIFS_NO_RESP
) == 0)
617 midQ
->resp_buf
= NULL
; /* mark it so buf will
622 cFYI(1, ("Bad MID state?"));
627 DeleteMidQEntry(midQ
);
628 atomic_dec(&ses
->server
->inFlight
);
629 wake_up(&ses
->server
->request_q
);
635 SendReceive(const unsigned int xid
, struct cifsSesInfo
*ses
,
636 struct smb_hdr
*in_buf
, struct smb_hdr
*out_buf
,
637 int *pbytes_returned
, const int long_op
)
640 unsigned int receive_len
;
641 unsigned long timeout
;
642 struct mid_q_entry
*midQ
;
645 cERROR(1, ("Null smb session"));
648 if (ses
->server
== NULL
) {
649 cERROR(1, ("Null tcp session"));
653 if (ses
->server
->tcpStatus
== CifsExiting
)
656 /* Ensure that we do not send more than 50 overlapping requests
657 to the same server. We may make this configurable later or
660 rc
= wait_for_free_request(ses
, long_op
);
664 /* make sure that we sign in the same order that we send on this socket
665 and avoid races inside tcp sendmsg code that could cause corruption
668 down(&ses
->server
->tcpSem
);
670 rc
= allocate_mid(ses
, in_buf
, &midQ
);
672 up(&ses
->server
->tcpSem
);
673 /* Update # of requests on wire to server */
674 atomic_dec(&ses
->server
->inFlight
);
675 wake_up(&ses
->server
->request_q
);
679 if (in_buf
->smb_buf_length
> CIFSMaxBufSize
+ MAX_CIFS_HDR_SIZE
- 4) {
680 cERROR(1, ("Illegal length, greater than maximum frame, %d",
681 in_buf
->smb_buf_length
));
682 DeleteMidQEntry(midQ
);
683 up(&ses
->server
->tcpSem
);
684 /* Update # of requests on wire to server */
685 atomic_dec(&ses
->server
->inFlight
);
686 wake_up(&ses
->server
->request_q
);
690 rc
= cifs_sign_smb(in_buf
, ses
->server
, &midQ
->sequence_number
);
692 midQ
->midState
= MID_REQUEST_SUBMITTED
;
693 #ifdef CONFIG_CIFS_STATS2
694 atomic_inc(&ses
->server
->inSend
);
696 rc
= smb_send(ses
->server
->ssocket
, in_buf
, in_buf
->smb_buf_length
,
697 (struct sockaddr
*) &(ses
->server
->addr
.sockAddr
));
698 #ifdef CONFIG_CIFS_STATS2
699 atomic_dec(&ses
->server
->inSend
);
700 midQ
->when_sent
= jiffies
;
702 up(&ses
->server
->tcpSem
);
707 if (long_op
== CIFS_STD_OP
)
709 /* wait for 15 seconds or until woken up due to response arriving or
710 due to last connection to this server being unmounted */
711 else if (long_op
== CIFS_ASYNC_OP
)
713 else if (long_op
== CIFS_VLONG_OP
) /* writes past EOF can be slow */
715 else if (long_op
== CIFS_LONG_OP
)
716 timeout
= 45 * HZ
; /* should be greater than
717 servers oplock break timeout (about 43 seconds) */
718 else if (long_op
== CIFS_BLOCKING_OP
)
719 timeout
= 0x7FFFFFFF; /* large but no so large as to wrap */
721 cERROR(1, ("unknown timeout flag %d", long_op
));
726 if (signal_pending(current
)) {
727 /* if signal pending do not hold up user for full smb timeout
728 but we still give response a chance to complete */
732 /* No user interrupts in wait - wreaks havoc with performance */
733 wait_for_response(ses
, midQ
, timeout
, 10 * HZ
);
735 spin_lock(&GlobalMid_Lock
);
736 if (midQ
->resp_buf
) {
737 spin_unlock(&GlobalMid_Lock
);
738 receive_len
= midQ
->resp_buf
->smb_buf_length
;
740 cERROR(1, ("No response for cmd %d mid %d",
741 midQ
->command
, midQ
->mid
));
742 if (midQ
->midState
== MID_REQUEST_SUBMITTED
) {
743 if (ses
->server
->tcpStatus
== CifsExiting
)
746 ses
->server
->tcpStatus
= CifsNeedReconnect
;
747 midQ
->midState
= MID_RETRY_NEEDED
;
751 if (rc
!= -EHOSTDOWN
) {
752 if (midQ
->midState
== MID_RETRY_NEEDED
) {
754 cFYI(1, ("marking request for retry"));
759 spin_unlock(&GlobalMid_Lock
);
760 DeleteMidQEntry(midQ
);
761 /* Update # of requests on wire to server */
762 atomic_dec(&ses
->server
->inFlight
);
763 wake_up(&ses
->server
->request_q
);
767 if (receive_len
> CIFSMaxBufSize
+ MAX_CIFS_HDR_SIZE
) {
768 cERROR(1, ("Frame too large received. Length: %d Xid: %d",
771 } else { /* rcvd frame is ok */
773 if (midQ
->resp_buf
&& out_buf
774 && (midQ
->midState
== MID_RESPONSE_RECEIVED
)) {
775 out_buf
->smb_buf_length
= receive_len
;
776 memcpy((char *)out_buf
+ 4,
777 (char *)midQ
->resp_buf
+ 4,
780 dump_smb(out_buf
, 92);
781 /* convert the length into a more usable form */
782 if ((receive_len
> 24) &&
783 (ses
->server
->secMode
& (SECMODE_SIGN_REQUIRED
|
784 SECMODE_SIGN_ENABLED
))) {
785 rc
= cifs_verify_signature(out_buf
,
786 &ses
->server
->mac_signing_key
,
787 midQ
->sequence_number
+1);
789 cERROR(1, ("Unexpected SMB signature"));
790 /* BB FIXME add code to kill session */
794 *pbytes_returned
= out_buf
->smb_buf_length
;
796 /* BB special case reconnect tid and uid here? */
797 rc
= map_smb_to_linux_error(out_buf
, 0 /* no log */ );
799 /* convert ByteCount if necessary */
800 if (receive_len
>= sizeof(struct smb_hdr
) - 4
801 /* do not count RFC1001 header */ +
802 (2 * out_buf
->WordCount
) + 2 /* bcc */ )
803 BCC(out_buf
) = le16_to_cpu(BCC_LE(out_buf
));
806 cERROR(1, ("Bad MID state?"));
811 DeleteMidQEntry(midQ
);
812 atomic_dec(&ses
->server
->inFlight
);
813 wake_up(&ses
->server
->request_q
);
818 /* Send an NT_CANCEL SMB to cause the POSIX blocking lock to return. */
821 send_nt_cancel(struct cifsTconInfo
*tcon
, struct smb_hdr
*in_buf
,
822 struct mid_q_entry
*midQ
)
825 struct cifsSesInfo
*ses
= tcon
->ses
;
826 __u16 mid
= in_buf
->Mid
;
828 header_assemble(in_buf
, SMB_COM_NT_CANCEL
, tcon
, 0);
830 down(&ses
->server
->tcpSem
);
831 rc
= cifs_sign_smb(in_buf
, ses
->server
, &midQ
->sequence_number
);
833 up(&ses
->server
->tcpSem
);
836 rc
= smb_send(ses
->server
->ssocket
, in_buf
, in_buf
->smb_buf_length
,
837 (struct sockaddr
*) &(ses
->server
->addr
.sockAddr
));
838 up(&ses
->server
->tcpSem
);
842 /* We send a LOCKINGX_CANCEL_LOCK to cause the Windows
843 blocking lock to return. */
846 send_lock_cancel(const unsigned int xid
, struct cifsTconInfo
*tcon
,
847 struct smb_hdr
*in_buf
,
848 struct smb_hdr
*out_buf
)
851 struct cifsSesInfo
*ses
= tcon
->ses
;
852 LOCK_REQ
*pSMB
= (LOCK_REQ
*)in_buf
;
854 /* We just modify the current in_buf to change
855 the type of lock from LOCKING_ANDX_SHARED_LOCK
856 or LOCKING_ANDX_EXCLUSIVE_LOCK to
857 LOCKING_ANDX_CANCEL_LOCK. */
859 pSMB
->LockType
= LOCKING_ANDX_CANCEL_LOCK
|LOCKING_ANDX_LARGE_FILES
;
861 pSMB
->hdr
.Mid
= GetNextMid(ses
->server
);
863 return SendReceive(xid
, ses
, in_buf
, out_buf
,
864 &bytes_returned
, CIFS_STD_OP
);
868 SendReceiveBlockingLock(const unsigned int xid
, struct cifsTconInfo
*tcon
,
869 struct smb_hdr
*in_buf
, struct smb_hdr
*out_buf
,
870 int *pbytes_returned
)
874 unsigned int receive_len
;
875 struct mid_q_entry
*midQ
;
876 struct cifsSesInfo
*ses
;
878 if (tcon
== NULL
|| tcon
->ses
== NULL
) {
879 cERROR(1, ("Null smb session"));
884 if (ses
->server
== NULL
) {
885 cERROR(1, ("Null tcp session"));
889 if (ses
->server
->tcpStatus
== CifsExiting
)
892 /* Ensure that we do not send more than 50 overlapping requests
893 to the same server. We may make this configurable later or
896 rc
= wait_for_free_request(ses
, CIFS_BLOCKING_OP
);
900 /* make sure that we sign in the same order that we send on this socket
901 and avoid races inside tcp sendmsg code that could cause corruption
904 down(&ses
->server
->tcpSem
);
906 rc
= allocate_mid(ses
, in_buf
, &midQ
);
908 up(&ses
->server
->tcpSem
);
912 if (in_buf
->smb_buf_length
> CIFSMaxBufSize
+ MAX_CIFS_HDR_SIZE
- 4) {
913 up(&ses
->server
->tcpSem
);
914 cERROR(1, ("Illegal length, greater than maximum frame, %d",
915 in_buf
->smb_buf_length
));
916 DeleteMidQEntry(midQ
);
920 rc
= cifs_sign_smb(in_buf
, ses
->server
, &midQ
->sequence_number
);
922 midQ
->midState
= MID_REQUEST_SUBMITTED
;
923 #ifdef CONFIG_CIFS_STATS2
924 atomic_inc(&ses
->server
->inSend
);
926 rc
= smb_send(ses
->server
->ssocket
, in_buf
, in_buf
->smb_buf_length
,
927 (struct sockaddr
*) &(ses
->server
->addr
.sockAddr
));
928 #ifdef CONFIG_CIFS_STATS2
929 atomic_dec(&ses
->server
->inSend
);
930 midQ
->when_sent
= jiffies
;
932 up(&ses
->server
->tcpSem
);
935 DeleteMidQEntry(midQ
);
939 /* Wait for a reply - allow signals to interrupt. */
940 rc
= wait_event_interruptible(ses
->server
->response_q
,
941 (!(midQ
->midState
== MID_REQUEST_SUBMITTED
)) ||
942 ((ses
->server
->tcpStatus
!= CifsGood
) &&
943 (ses
->server
->tcpStatus
!= CifsNew
)));
945 /* Were we interrupted by a signal ? */
946 if ((rc
== -ERESTARTSYS
) &&
947 (midQ
->midState
== MID_REQUEST_SUBMITTED
) &&
948 ((ses
->server
->tcpStatus
== CifsGood
) ||
949 (ses
->server
->tcpStatus
== CifsNew
))) {
951 if (in_buf
->Command
== SMB_COM_TRANSACTION2
) {
952 /* POSIX lock. We send a NT_CANCEL SMB to cause the
953 blocking lock to return. */
955 rc
= send_nt_cancel(tcon
, in_buf
, midQ
);
957 DeleteMidQEntry(midQ
);
961 /* Windows lock. We send a LOCKINGX_CANCEL_LOCK
962 to cause the blocking lock to return. */
964 rc
= send_lock_cancel(xid
, tcon
, in_buf
, out_buf
);
966 /* If we get -ENOLCK back the lock may have
967 already been removed. Don't exit in this case. */
968 if (rc
&& rc
!= -ENOLCK
) {
969 DeleteMidQEntry(midQ
);
974 /* Wait 5 seconds for the response. */
975 if (wait_for_response(ses
, midQ
, 5 * HZ
, 5 * HZ
) == 0) {
976 /* We got the response - restart system call. */
981 spin_lock(&GlobalMid_Lock
);
982 if (midQ
->resp_buf
) {
983 spin_unlock(&GlobalMid_Lock
);
984 receive_len
= midQ
->resp_buf
->smb_buf_length
;
986 cERROR(1, ("No response for cmd %d mid %d",
987 midQ
->command
, midQ
->mid
));
988 if (midQ
->midState
== MID_REQUEST_SUBMITTED
) {
989 if (ses
->server
->tcpStatus
== CifsExiting
)
992 ses
->server
->tcpStatus
= CifsNeedReconnect
;
993 midQ
->midState
= MID_RETRY_NEEDED
;
997 if (rc
!= -EHOSTDOWN
) {
998 if (midQ
->midState
== MID_RETRY_NEEDED
) {
1000 cFYI(1, ("marking request for retry"));
1005 spin_unlock(&GlobalMid_Lock
);
1006 DeleteMidQEntry(midQ
);
1010 if (receive_len
> CIFSMaxBufSize
+ MAX_CIFS_HDR_SIZE
) {
1011 cERROR(1, ("Frame too large received. Length: %d Xid: %d",
1014 } else { /* rcvd frame is ok */
1016 if (midQ
->resp_buf
&& out_buf
1017 && (midQ
->midState
== MID_RESPONSE_RECEIVED
)) {
1018 out_buf
->smb_buf_length
= receive_len
;
1019 memcpy((char *)out_buf
+ 4,
1020 (char *)midQ
->resp_buf
+ 4,
1023 dump_smb(out_buf
, 92);
1024 /* convert the length into a more usable form */
1025 if ((receive_len
> 24) &&
1026 (ses
->server
->secMode
& (SECMODE_SIGN_REQUIRED
|
1027 SECMODE_SIGN_ENABLED
))) {
1028 rc
= cifs_verify_signature(out_buf
,
1029 &ses
->server
->mac_signing_key
,
1030 midQ
->sequence_number
+1);
1032 cERROR(1, ("Unexpected SMB signature"));
1033 /* BB FIXME add code to kill session */
1037 *pbytes_returned
= out_buf
->smb_buf_length
;
1039 /* BB special case reconnect tid and uid here? */
1040 rc
= map_smb_to_linux_error(out_buf
, 0 /* no log */ );
1042 /* convert ByteCount if necessary */
1043 if (receive_len
>= sizeof(struct smb_hdr
) - 4
1044 /* do not count RFC1001 header */ +
1045 (2 * out_buf
->WordCount
) + 2 /* bcc */ )
1046 BCC(out_buf
) = le16_to_cpu(BCC_LE(out_buf
));
1049 cERROR(1, ("Bad MID state?"));
1052 DeleteMidQEntry(midQ
);
1053 if (rstart
&& rc
== -EACCES
)
1054 return -ERESTARTSYS
;