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/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 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
);
106 struct oplock_q_entry
*
107 AllocOplockQEntry(struct inode
*pinode
, __u16 fid
, struct cifsTconInfo
*tcon
)
109 struct oplock_q_entry
*temp
;
110 if ((pinode
== NULL
) || (tcon
== NULL
)) {
111 cERROR(1, ("Null parms passed to AllocOplockQEntry"));
114 temp
= (struct oplock_q_entry
*) kmem_cache_alloc(cifs_oplock_cachep
,
119 temp
->pinode
= pinode
;
122 spin_lock(&GlobalMid_Lock
);
123 list_add_tail(&temp
->qhead
, &GlobalOplock_Q
);
124 spin_unlock(&GlobalMid_Lock
);
130 void DeleteOplockQEntry(struct oplock_q_entry
*oplockEntry
)
132 spin_lock(&GlobalMid_Lock
);
133 /* should we check if list empty first? */
134 list_del(&oplockEntry
->qhead
);
135 spin_unlock(&GlobalMid_Lock
);
136 kmem_cache_free(cifs_oplock_cachep
, oplockEntry
);
140 void DeleteTconOplockQEntries(struct cifsTconInfo
*tcon
)
142 struct oplock_q_entry
*temp
;
147 spin_lock(&GlobalMid_Lock
);
148 list_for_each_entry(temp
, &GlobalOplock_Q
, qhead
) {
149 if ((temp
->tcon
) && (temp
->tcon
== tcon
)) {
150 list_del(&temp
->qhead
);
151 kmem_cache_free(cifs_oplock_cachep
, temp
);
154 spin_unlock(&GlobalMid_Lock
);
158 smb_send(struct socket
*ssocket
, struct smb_hdr
*smb_buffer
,
159 unsigned int smb_buf_length
, struct sockaddr
*sin
, bool noblocksnd
)
163 struct msghdr smb_msg
;
165 unsigned len
= smb_buf_length
+ 4;
168 return -ENOTSOCK
; /* BB eventually add reconnect code here */
169 iov
.iov_base
= smb_buffer
;
172 smb_msg
.msg_name
= sin
;
173 smb_msg
.msg_namelen
= sizeof(struct sockaddr
);
174 smb_msg
.msg_control
= NULL
;
175 smb_msg
.msg_controllen
= 0;
177 smb_msg
.msg_flags
= MSG_DONTWAIT
+ MSG_NOSIGNAL
;
179 smb_msg
.msg_flags
= MSG_NOSIGNAL
;
181 /* smb header is converted in header_assemble. bcc and rest of SMB word
182 area, and byte area if necessary, is converted to littleendian in
183 cifssmb.c and RFC1001 len is converted to bigendian in smb_send
184 Flags2 is converted in SendReceive */
186 smb_buffer
->smb_buf_length
= cpu_to_be32(smb_buffer
->smb_buf_length
);
187 cFYI(1, ("Sending smb of length %d", smb_buf_length
));
188 dump_smb(smb_buffer
, len
);
191 rc
= kernel_sendmsg(ssocket
, &smb_msg
, &iov
, 1, len
);
192 if ((rc
== -ENOSPC
) || (rc
== -EAGAIN
)) {
194 /* smaller timeout here than send2 since smaller size */
195 /* Although it may not be required, this also is smaller
199 ("sends on sock %p stuck for 7 seconds",
210 i
= 0; /* reset i after each successful send */
217 cERROR(1, ("Error %d sending data on socket to server", rc
));
222 /* Don't want to modify the buffer as a
223 side effect of this call. */
224 smb_buffer
->smb_buf_length
= smb_buf_length
;
230 smb_send2(struct TCP_Server_Info
*server
, struct kvec
*iov
, int n_vec
,
231 struct sockaddr
*sin
, bool noblocksnd
)
235 struct msghdr smb_msg
;
236 struct smb_hdr
*smb_buffer
= iov
[0].iov_base
;
237 unsigned int len
= iov
[0].iov_len
;
238 unsigned int total_len
;
240 unsigned int smb_buf_length
= smb_buffer
->smb_buf_length
;
241 struct socket
*ssocket
= server
->ssocket
;
244 return -ENOTSOCK
; /* BB eventually add reconnect code here */
246 smb_msg
.msg_name
= sin
;
247 smb_msg
.msg_namelen
= sizeof(struct sockaddr
);
248 smb_msg
.msg_control
= NULL
;
249 smb_msg
.msg_controllen
= 0;
251 smb_msg
.msg_flags
= MSG_DONTWAIT
+ MSG_NOSIGNAL
;
253 smb_msg
.msg_flags
= MSG_NOSIGNAL
;
255 /* smb header is converted in header_assemble. bcc and rest of SMB word
256 area, and byte area if necessary, is converted to littleendian in
257 cifssmb.c and RFC1001 len is converted to bigendian in smb_send
258 Flags2 is converted in SendReceive */
262 for (i
= 0; i
< n_vec
; i
++)
263 total_len
+= iov
[i
].iov_len
;
265 smb_buffer
->smb_buf_length
= cpu_to_be32(smb_buffer
->smb_buf_length
);
266 cFYI(1, ("Sending smb: total_len %d", total_len
));
267 dump_smb(smb_buffer
, len
);
271 rc
= kernel_sendmsg(ssocket
, &smb_msg
, &iov
[first_vec
],
272 n_vec
- first_vec
, total_len
);
273 if ((rc
== -ENOSPC
) || (rc
== -EAGAIN
)) {
277 ("sends on sock %p stuck for 15 seconds",
288 if (rc
== total_len
) {
291 } else if (rc
> total_len
) {
292 cERROR(1, ("sent %d requested %d", rc
, total_len
));
296 /* should never happen, letting socket clear before
297 retrying is our only obvious option here */
298 cERROR(1, ("tcp sent no data"));
303 /* the line below resets i */
304 for (i
= first_vec
; i
< n_vec
; i
++) {
305 if (iov
[i
].iov_len
) {
306 if (rc
> iov
[i
].iov_len
) {
307 rc
-= iov
[i
].iov_len
;
310 iov
[i
].iov_base
+= rc
;
311 iov
[i
].iov_len
-= rc
;
317 i
= 0; /* in case we get ENOSPC on the next send */
320 if ((total_len
> 0) && (total_len
!= smb_buf_length
+ 4)) {
321 cFYI(1, ("partial send (%d remaining), terminating session",
323 /* If we have only sent part of an SMB then the next SMB
324 could be taken as the remainder of this one. We need
325 to kill the socket so the server throws away the partial
327 server
->tcpStatus
= CifsNeedReconnect
;
331 cERROR(1, ("Error %d sending data on socket to server", rc
));
335 /* Don't want to modify the buffer as a
336 side effect of this call. */
337 smb_buffer
->smb_buf_length
= smb_buf_length
;
342 static int wait_for_free_request(struct cifsSesInfo
*ses
, const int long_op
)
344 if (long_op
== CIFS_ASYNC_OP
) {
345 /* oplock breaks must not be held up */
346 atomic_inc(&ses
->server
->inFlight
);
348 spin_lock(&GlobalMid_Lock
);
350 if (atomic_read(&ses
->server
->inFlight
) >=
352 spin_unlock(&GlobalMid_Lock
);
353 #ifdef CONFIG_CIFS_STATS2
354 atomic_inc(&ses
->server
->num_waiters
);
356 wait_event(ses
->server
->request_q
,
357 atomic_read(&ses
->server
->inFlight
)
359 #ifdef CONFIG_CIFS_STATS2
360 atomic_dec(&ses
->server
->num_waiters
);
362 spin_lock(&GlobalMid_Lock
);
364 if (ses
->server
->tcpStatus
== CifsExiting
) {
365 spin_unlock(&GlobalMid_Lock
);
369 /* can not count locking commands against total
370 as they are allowed to block on server */
372 /* update # of requests on the wire to server */
373 if (long_op
!= CIFS_BLOCKING_OP
)
374 atomic_inc(&ses
->server
->inFlight
);
375 spin_unlock(&GlobalMid_Lock
);
383 static int allocate_mid(struct cifsSesInfo
*ses
, struct smb_hdr
*in_buf
,
384 struct mid_q_entry
**ppmidQ
)
386 if (ses
->server
->tcpStatus
== CifsExiting
) {
388 } else if (ses
->server
->tcpStatus
== CifsNeedReconnect
) {
389 cFYI(1, ("tcp session dead - return to caller to retry"));
391 } else if (ses
->status
!= CifsGood
) {
392 /* check if SMB session is bad because we are setting it up */
393 if ((in_buf
->Command
!= SMB_COM_SESSION_SETUP_ANDX
) &&
394 (in_buf
->Command
!= SMB_COM_NEGOTIATE
))
396 /* else ok - we are setting up session */
398 *ppmidQ
= AllocMidQEntry(in_buf
, ses
->server
);
404 static int wait_for_response(struct cifsSesInfo
*ses
,
405 struct mid_q_entry
*midQ
,
406 unsigned long timeout
,
407 unsigned long time_to_wait
)
409 unsigned long curr_timeout
;
412 curr_timeout
= timeout
+ jiffies
;
413 wait_event(ses
->server
->response_q
,
414 (!(midQ
->midState
== MID_REQUEST_SUBMITTED
)) ||
415 time_after(jiffies
, curr_timeout
) ||
416 ((ses
->server
->tcpStatus
!= CifsGood
) &&
417 (ses
->server
->tcpStatus
!= CifsNew
)));
419 if (time_after(jiffies
, curr_timeout
) &&
420 (midQ
->midState
== MID_REQUEST_SUBMITTED
) &&
421 ((ses
->server
->tcpStatus
== CifsGood
) ||
422 (ses
->server
->tcpStatus
== CifsNew
))) {
426 /* We timed out. Is the server still
428 spin_lock(&GlobalMid_Lock
);
429 lrt
= ses
->server
->lstrp
;
430 spin_unlock(&GlobalMid_Lock
);
432 /* Calculate time_to_wait past last receive time.
433 Although we prefer not to time out if the
434 server is still responding - we will time
435 out if the server takes more than 15 (or 45
436 or 180) seconds to respond to this request
437 and has not responded to any request from
438 other threads on the client within 10 seconds */
440 if (time_after(jiffies
, lrt
)) {
441 /* No replies for time_to_wait. */
442 cERROR(1, ("server not responding"));
454 * Send an SMB Request. No response info (other than return code)
455 * needs to be parsed.
457 * flags indicate the type of request buffer and how long to wait
458 * and whether to log NT STATUS code (error) before mapping it to POSIX error
462 SendReceiveNoRsp(const unsigned int xid
, struct cifsSesInfo
*ses
,
463 struct smb_hdr
*in_buf
, int flags
)
469 iov
[0].iov_base
= (char *)in_buf
;
470 iov
[0].iov_len
= in_buf
->smb_buf_length
+ 4;
471 flags
|= CIFS_NO_RESP
;
472 rc
= SendReceive2(xid
, ses
, iov
, 1, &resp_buf_type
, flags
);
473 cFYI(DBG2
, ("SendRcvNoRsp flags %d rc %d", flags
, rc
));
479 SendReceive2(const unsigned int xid
, struct cifsSesInfo
*ses
,
480 struct kvec
*iov
, int n_vec
, int *pRespBufType
/* ret */,
485 unsigned int receive_len
;
486 unsigned long timeout
;
487 struct mid_q_entry
*midQ
;
488 struct smb_hdr
*in_buf
= iov
[0].iov_base
;
490 long_op
= flags
& CIFS_TIMEOUT_MASK
;
492 *pRespBufType
= CIFS_NO_BUFFER
; /* no response buf yet */
494 if ((ses
== NULL
) || (ses
->server
== NULL
)) {
495 cifs_small_buf_release(in_buf
);
496 cERROR(1, ("Null session"));
500 if (ses
->server
->tcpStatus
== CifsExiting
) {
501 cifs_small_buf_release(in_buf
);
505 /* Ensure that we do not send more than 50 overlapping requests
506 to the same server. We may make this configurable later or
509 rc
= wait_for_free_request(ses
, long_op
);
511 cifs_small_buf_release(in_buf
);
515 /* make sure that we sign in the same order that we send on this socket
516 and avoid races inside tcp sendmsg code that could cause corruption
519 down(&ses
->server
->tcpSem
);
521 rc
= allocate_mid(ses
, in_buf
, &midQ
);
523 up(&ses
->server
->tcpSem
);
524 cifs_small_buf_release(in_buf
);
525 /* Update # of requests on wire to server */
526 atomic_dec(&ses
->server
->inFlight
);
527 wake_up(&ses
->server
->request_q
);
530 rc
= cifs_sign_smb2(iov
, n_vec
, ses
->server
, &midQ
->sequence_number
);
532 midQ
->midState
= MID_REQUEST_SUBMITTED
;
533 #ifdef CONFIG_CIFS_STATS2
534 atomic_inc(&ses
->server
->inSend
);
536 rc
= smb_send2(ses
->server
, iov
, n_vec
,
537 (struct sockaddr
*) &(ses
->server
->addr
.sockAddr
),
538 ses
->server
->noblocksnd
);
539 #ifdef CONFIG_CIFS_STATS2
540 atomic_dec(&ses
->server
->inSend
);
541 midQ
->when_sent
= jiffies
;
544 up(&ses
->server
->tcpSem
);
545 cifs_small_buf_release(in_buf
);
550 if (long_op
== CIFS_STD_OP
)
552 else if (long_op
== CIFS_VLONG_OP
) /* e.g. slow writes past EOF */
554 else if (long_op
== CIFS_LONG_OP
)
555 timeout
= 45 * HZ
; /* should be greater than
556 servers oplock break timeout (about 43 seconds) */
557 else if (long_op
== CIFS_ASYNC_OP
)
559 else if (long_op
== CIFS_BLOCKING_OP
)
560 timeout
= 0x7FFFFFFF; /* large, but not so large as to wrap */
562 cERROR(1, ("unknown timeout flag %d", long_op
));
567 /* wait for 15 seconds or until woken up due to response arriving or
568 due to last connection to this server being unmounted */
569 if (signal_pending(current
)) {
570 /* if signal pending do not hold up user for full smb timeout
571 but we still give response a chance to complete */
575 /* No user interrupts in wait - wreaks havoc with performance */
576 wait_for_response(ses
, midQ
, timeout
, 10 * HZ
);
578 spin_lock(&GlobalMid_Lock
);
579 if (midQ
->resp_buf
) {
580 spin_unlock(&GlobalMid_Lock
);
581 receive_len
= midQ
->resp_buf
->smb_buf_length
;
583 cERROR(1, ("No response to cmd %d mid %d",
584 midQ
->command
, midQ
->mid
));
585 if (midQ
->midState
== MID_REQUEST_SUBMITTED
) {
586 if (ses
->server
->tcpStatus
== CifsExiting
)
589 ses
->server
->tcpStatus
= CifsNeedReconnect
;
590 midQ
->midState
= MID_RETRY_NEEDED
;
594 if (rc
!= -EHOSTDOWN
) {
595 if (midQ
->midState
== MID_RETRY_NEEDED
) {
597 cFYI(1, ("marking request for retry"));
602 spin_unlock(&GlobalMid_Lock
);
603 DeleteMidQEntry(midQ
);
604 /* Update # of requests on wire to server */
605 atomic_dec(&ses
->server
->inFlight
);
606 wake_up(&ses
->server
->request_q
);
610 if (receive_len
> CIFSMaxBufSize
+ MAX_CIFS_HDR_SIZE
) {
611 cERROR(1, ("Frame too large received. Length: %d Xid: %d",
614 } else { /* rcvd frame is ok */
615 if (midQ
->resp_buf
&&
616 (midQ
->midState
== MID_RESPONSE_RECEIVED
)) {
618 iov
[0].iov_base
= (char *)midQ
->resp_buf
;
620 *pRespBufType
= CIFS_LARGE_BUFFER
;
622 *pRespBufType
= CIFS_SMALL_BUFFER
;
623 iov
[0].iov_len
= receive_len
+ 4;
625 dump_smb(midQ
->resp_buf
, 80);
626 /* convert the length into a more usable form */
627 if ((receive_len
> 24) &&
628 (ses
->server
->secMode
& (SECMODE_SIGN_REQUIRED
|
629 SECMODE_SIGN_ENABLED
))) {
630 rc
= cifs_verify_signature(midQ
->resp_buf
,
631 &ses
->server
->mac_signing_key
,
632 midQ
->sequence_number
+1);
634 cERROR(1, ("Unexpected SMB signature"));
635 /* BB FIXME add code to kill session */
639 /* BB special case reconnect tid and uid here? */
640 rc
= map_smb_to_linux_error(midQ
->resp_buf
,
641 flags
& CIFS_LOG_ERROR
);
643 /* convert ByteCount if necessary */
644 if (receive_len
>= sizeof(struct smb_hdr
) - 4
645 /* do not count RFC1001 header */ +
646 (2 * midQ
->resp_buf
->WordCount
) + 2 /* bcc */ )
647 BCC(midQ
->resp_buf
) =
648 le16_to_cpu(BCC_LE(midQ
->resp_buf
));
649 if ((flags
& CIFS_NO_RESP
) == 0)
650 midQ
->resp_buf
= NULL
; /* mark it so buf will
655 cFYI(1, ("Bad MID state?"));
660 DeleteMidQEntry(midQ
);
661 atomic_dec(&ses
->server
->inFlight
);
662 wake_up(&ses
->server
->request_q
);
668 SendReceive(const unsigned int xid
, struct cifsSesInfo
*ses
,
669 struct smb_hdr
*in_buf
, struct smb_hdr
*out_buf
,
670 int *pbytes_returned
, const int long_op
)
673 unsigned int receive_len
;
674 unsigned long timeout
;
675 struct mid_q_entry
*midQ
;
678 cERROR(1, ("Null smb session"));
681 if (ses
->server
== NULL
) {
682 cERROR(1, ("Null tcp session"));
686 if (ses
->server
->tcpStatus
== CifsExiting
)
689 /* Ensure that we do not send more than 50 overlapping requests
690 to the same server. We may make this configurable later or
693 rc
= wait_for_free_request(ses
, long_op
);
697 /* make sure that we sign in the same order that we send on this socket
698 and avoid races inside tcp sendmsg code that could cause corruption
701 down(&ses
->server
->tcpSem
);
703 rc
= allocate_mid(ses
, in_buf
, &midQ
);
705 up(&ses
->server
->tcpSem
);
706 /* Update # of requests on wire to server */
707 atomic_dec(&ses
->server
->inFlight
);
708 wake_up(&ses
->server
->request_q
);
712 if (in_buf
->smb_buf_length
> CIFSMaxBufSize
+ MAX_CIFS_HDR_SIZE
- 4) {
713 cERROR(1, ("Illegal length, greater than maximum frame, %d",
714 in_buf
->smb_buf_length
));
715 DeleteMidQEntry(midQ
);
716 up(&ses
->server
->tcpSem
);
717 /* Update # of requests on wire to server */
718 atomic_dec(&ses
->server
->inFlight
);
719 wake_up(&ses
->server
->request_q
);
723 rc
= cifs_sign_smb(in_buf
, ses
->server
, &midQ
->sequence_number
);
725 midQ
->midState
= MID_REQUEST_SUBMITTED
;
726 #ifdef CONFIG_CIFS_STATS2
727 atomic_inc(&ses
->server
->inSend
);
729 rc
= smb_send(ses
->server
->ssocket
, in_buf
, in_buf
->smb_buf_length
,
730 (struct sockaddr
*) &(ses
->server
->addr
.sockAddr
),
731 ses
->server
->noblocksnd
);
732 #ifdef CONFIG_CIFS_STATS2
733 atomic_dec(&ses
->server
->inSend
);
734 midQ
->when_sent
= jiffies
;
736 up(&ses
->server
->tcpSem
);
741 if (long_op
== CIFS_STD_OP
)
743 /* wait for 15 seconds or until woken up due to response arriving or
744 due to last connection to this server being unmounted */
745 else if (long_op
== CIFS_ASYNC_OP
)
747 else if (long_op
== CIFS_VLONG_OP
) /* writes past EOF can be slow */
749 else if (long_op
== CIFS_LONG_OP
)
750 timeout
= 45 * HZ
; /* should be greater than
751 servers oplock break timeout (about 43 seconds) */
752 else if (long_op
== CIFS_BLOCKING_OP
)
753 timeout
= 0x7FFFFFFF; /* large but no so large as to wrap */
755 cERROR(1, ("unknown timeout flag %d", long_op
));
760 if (signal_pending(current
)) {
761 /* if signal pending do not hold up user for full smb timeout
762 but we still give response a chance to complete */
766 /* No user interrupts in wait - wreaks havoc with performance */
767 wait_for_response(ses
, midQ
, timeout
, 10 * HZ
);
769 spin_lock(&GlobalMid_Lock
);
770 if (midQ
->resp_buf
) {
771 spin_unlock(&GlobalMid_Lock
);
772 receive_len
= midQ
->resp_buf
->smb_buf_length
;
774 cERROR(1, ("No response for cmd %d mid %d",
775 midQ
->command
, midQ
->mid
));
776 if (midQ
->midState
== MID_REQUEST_SUBMITTED
) {
777 if (ses
->server
->tcpStatus
== CifsExiting
)
780 ses
->server
->tcpStatus
= CifsNeedReconnect
;
781 midQ
->midState
= MID_RETRY_NEEDED
;
785 if (rc
!= -EHOSTDOWN
) {
786 if (midQ
->midState
== MID_RETRY_NEEDED
) {
788 cFYI(1, ("marking request for retry"));
793 spin_unlock(&GlobalMid_Lock
);
794 DeleteMidQEntry(midQ
);
795 /* Update # of requests on wire to server */
796 atomic_dec(&ses
->server
->inFlight
);
797 wake_up(&ses
->server
->request_q
);
801 if (receive_len
> CIFSMaxBufSize
+ MAX_CIFS_HDR_SIZE
) {
802 cERROR(1, ("Frame too large received. Length: %d Xid: %d",
805 } else { /* rcvd frame is ok */
807 if (midQ
->resp_buf
&& out_buf
808 && (midQ
->midState
== MID_RESPONSE_RECEIVED
)) {
809 out_buf
->smb_buf_length
= receive_len
;
810 memcpy((char *)out_buf
+ 4,
811 (char *)midQ
->resp_buf
+ 4,
814 dump_smb(out_buf
, 92);
815 /* convert the length into a more usable form */
816 if ((receive_len
> 24) &&
817 (ses
->server
->secMode
& (SECMODE_SIGN_REQUIRED
|
818 SECMODE_SIGN_ENABLED
))) {
819 rc
= cifs_verify_signature(out_buf
,
820 &ses
->server
->mac_signing_key
,
821 midQ
->sequence_number
+1);
823 cERROR(1, ("Unexpected SMB signature"));
824 /* BB FIXME add code to kill session */
828 *pbytes_returned
= out_buf
->smb_buf_length
;
830 /* BB special case reconnect tid and uid here? */
831 rc
= map_smb_to_linux_error(out_buf
, 0 /* no log */ );
833 /* convert ByteCount if necessary */
834 if (receive_len
>= sizeof(struct smb_hdr
) - 4
835 /* do not count RFC1001 header */ +
836 (2 * out_buf
->WordCount
) + 2 /* bcc */ )
837 BCC(out_buf
) = le16_to_cpu(BCC_LE(out_buf
));
840 cERROR(1, ("Bad MID state?"));
845 DeleteMidQEntry(midQ
);
846 atomic_dec(&ses
->server
->inFlight
);
847 wake_up(&ses
->server
->request_q
);
852 /* Send an NT_CANCEL SMB to cause the POSIX blocking lock to return. */
855 send_nt_cancel(struct cifsTconInfo
*tcon
, struct smb_hdr
*in_buf
,
856 struct mid_q_entry
*midQ
)
859 struct cifsSesInfo
*ses
= tcon
->ses
;
860 __u16 mid
= in_buf
->Mid
;
862 header_assemble(in_buf
, SMB_COM_NT_CANCEL
, tcon
, 0);
864 down(&ses
->server
->tcpSem
);
865 rc
= cifs_sign_smb(in_buf
, ses
->server
, &midQ
->sequence_number
);
867 up(&ses
->server
->tcpSem
);
870 rc
= smb_send(ses
->server
->ssocket
, in_buf
, in_buf
->smb_buf_length
,
871 (struct sockaddr
*) &(ses
->server
->addr
.sockAddr
),
872 ses
->server
->noblocksnd
);
873 up(&ses
->server
->tcpSem
);
877 /* We send a LOCKINGX_CANCEL_LOCK to cause the Windows
878 blocking lock to return. */
881 send_lock_cancel(const unsigned int xid
, struct cifsTconInfo
*tcon
,
882 struct smb_hdr
*in_buf
,
883 struct smb_hdr
*out_buf
)
886 struct cifsSesInfo
*ses
= tcon
->ses
;
887 LOCK_REQ
*pSMB
= (LOCK_REQ
*)in_buf
;
889 /* We just modify the current in_buf to change
890 the type of lock from LOCKING_ANDX_SHARED_LOCK
891 or LOCKING_ANDX_EXCLUSIVE_LOCK to
892 LOCKING_ANDX_CANCEL_LOCK. */
894 pSMB
->LockType
= LOCKING_ANDX_CANCEL_LOCK
|LOCKING_ANDX_LARGE_FILES
;
896 pSMB
->hdr
.Mid
= GetNextMid(ses
->server
);
898 return SendReceive(xid
, ses
, in_buf
, out_buf
,
899 &bytes_returned
, CIFS_STD_OP
);
903 SendReceiveBlockingLock(const unsigned int xid
, struct cifsTconInfo
*tcon
,
904 struct smb_hdr
*in_buf
, struct smb_hdr
*out_buf
,
905 int *pbytes_returned
)
909 unsigned int receive_len
;
910 struct mid_q_entry
*midQ
;
911 struct cifsSesInfo
*ses
;
913 if (tcon
== NULL
|| tcon
->ses
== NULL
) {
914 cERROR(1, ("Null smb session"));
919 if (ses
->server
== NULL
) {
920 cERROR(1, ("Null tcp session"));
924 if (ses
->server
->tcpStatus
== CifsExiting
)
927 /* Ensure that we do not send more than 50 overlapping requests
928 to the same server. We may make this configurable later or
931 rc
= wait_for_free_request(ses
, CIFS_BLOCKING_OP
);
935 /* make sure that we sign in the same order that we send on this socket
936 and avoid races inside tcp sendmsg code that could cause corruption
939 down(&ses
->server
->tcpSem
);
941 rc
= allocate_mid(ses
, in_buf
, &midQ
);
943 up(&ses
->server
->tcpSem
);
947 if (in_buf
->smb_buf_length
> CIFSMaxBufSize
+ MAX_CIFS_HDR_SIZE
- 4) {
948 up(&ses
->server
->tcpSem
);
949 cERROR(1, ("Illegal length, greater than maximum frame, %d",
950 in_buf
->smb_buf_length
));
951 DeleteMidQEntry(midQ
);
955 rc
= cifs_sign_smb(in_buf
, ses
->server
, &midQ
->sequence_number
);
957 midQ
->midState
= MID_REQUEST_SUBMITTED
;
958 #ifdef CONFIG_CIFS_STATS2
959 atomic_inc(&ses
->server
->inSend
);
961 rc
= smb_send(ses
->server
->ssocket
, in_buf
, in_buf
->smb_buf_length
,
962 (struct sockaddr
*) &(ses
->server
->addr
.sockAddr
),
963 ses
->server
->noblocksnd
);
964 #ifdef CONFIG_CIFS_STATS2
965 atomic_dec(&ses
->server
->inSend
);
966 midQ
->when_sent
= jiffies
;
968 up(&ses
->server
->tcpSem
);
971 DeleteMidQEntry(midQ
);
975 /* Wait for a reply - allow signals to interrupt. */
976 rc
= wait_event_interruptible(ses
->server
->response_q
,
977 (!(midQ
->midState
== MID_REQUEST_SUBMITTED
)) ||
978 ((ses
->server
->tcpStatus
!= CifsGood
) &&
979 (ses
->server
->tcpStatus
!= CifsNew
)));
981 /* Were we interrupted by a signal ? */
982 if ((rc
== -ERESTARTSYS
) &&
983 (midQ
->midState
== MID_REQUEST_SUBMITTED
) &&
984 ((ses
->server
->tcpStatus
== CifsGood
) ||
985 (ses
->server
->tcpStatus
== CifsNew
))) {
987 if (in_buf
->Command
== SMB_COM_TRANSACTION2
) {
988 /* POSIX lock. We send a NT_CANCEL SMB to cause the
989 blocking lock to return. */
991 rc
= send_nt_cancel(tcon
, in_buf
, midQ
);
993 DeleteMidQEntry(midQ
);
997 /* Windows lock. We send a LOCKINGX_CANCEL_LOCK
998 to cause the blocking lock to return. */
1000 rc
= send_lock_cancel(xid
, tcon
, in_buf
, out_buf
);
1002 /* If we get -ENOLCK back the lock may have
1003 already been removed. Don't exit in this case. */
1004 if (rc
&& rc
!= -ENOLCK
) {
1005 DeleteMidQEntry(midQ
);
1010 /* Wait 5 seconds for the response. */
1011 if (wait_for_response(ses
, midQ
, 5 * HZ
, 5 * HZ
) == 0) {
1012 /* We got the response - restart system call. */
1017 spin_lock(&GlobalMid_Lock
);
1018 if (midQ
->resp_buf
) {
1019 spin_unlock(&GlobalMid_Lock
);
1020 receive_len
= midQ
->resp_buf
->smb_buf_length
;
1022 cERROR(1, ("No response for cmd %d mid %d",
1023 midQ
->command
, midQ
->mid
));
1024 if (midQ
->midState
== MID_REQUEST_SUBMITTED
) {
1025 if (ses
->server
->tcpStatus
== CifsExiting
)
1028 ses
->server
->tcpStatus
= CifsNeedReconnect
;
1029 midQ
->midState
= MID_RETRY_NEEDED
;
1033 if (rc
!= -EHOSTDOWN
) {
1034 if (midQ
->midState
== MID_RETRY_NEEDED
) {
1036 cFYI(1, ("marking request for retry"));
1041 spin_unlock(&GlobalMid_Lock
);
1042 DeleteMidQEntry(midQ
);
1046 if (receive_len
> CIFSMaxBufSize
+ MAX_CIFS_HDR_SIZE
) {
1047 cERROR(1, ("Frame too large received. Length: %d Xid: %d",
1050 } else { /* rcvd frame is ok */
1052 if (midQ
->resp_buf
&& out_buf
1053 && (midQ
->midState
== MID_RESPONSE_RECEIVED
)) {
1054 out_buf
->smb_buf_length
= receive_len
;
1055 memcpy((char *)out_buf
+ 4,
1056 (char *)midQ
->resp_buf
+ 4,
1059 dump_smb(out_buf
, 92);
1060 /* convert the length into a more usable form */
1061 if ((receive_len
> 24) &&
1062 (ses
->server
->secMode
& (SECMODE_SIGN_REQUIRED
|
1063 SECMODE_SIGN_ENABLED
))) {
1064 rc
= cifs_verify_signature(out_buf
,
1065 &ses
->server
->mac_signing_key
,
1066 midQ
->sequence_number
+1);
1068 cERROR(1, ("Unexpected SMB signature"));
1069 /* BB FIXME add code to kill session */
1073 *pbytes_returned
= out_buf
->smb_buf_length
;
1075 /* BB special case reconnect tid and uid here? */
1076 rc
= map_smb_to_linux_error(out_buf
, 0 /* no log */ );
1078 /* convert ByteCount if necessary */
1079 if (receive_len
>= sizeof(struct smb_hdr
) - 4
1080 /* do not count RFC1001 header */ +
1081 (2 * out_buf
->WordCount
) + 2 /* bcc */ )
1082 BCC(out_buf
) = le16_to_cpu(BCC_LE(out_buf
));
1085 cERROR(1, ("Bad MID state?"));
1088 DeleteMidQEntry(midQ
);
1089 if (rstart
&& rc
== -EACCES
)
1090 return -ERESTARTSYS
;