cifs: Fix error in smb_send2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / cifs / transport.c
blobbb0f32960a7c697595d795e9d376a515d45a046e
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/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>
31 #include "cifspdu.h"
32 #include "cifsglob.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;
44 if (ses == NULL) {
45 cERROR(1, ("Null session passed in to AllocMidQEntry"));
46 return NULL;
48 if (ses->server == NULL) {
49 cERROR(1, ("Null TCP session in AllocMidQEntry"));
50 return NULL;
53 temp = (struct mid_q_entry *) mempool_alloc(cifs_mid_poolp,
54 GFP_KERNEL | 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 = 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;
66 temp->ses = ses;
67 temp->tsk = current;
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);
75 return temp;
78 static void
79 DeleteMidQEntry(struct mid_q_entry *midEntry)
81 #ifdef CONFIG_CIFS_STATS2
82 unsigned long now;
83 #endif
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);
91 else
92 cifs_small_buf_release(midEntry->resp_buf);
93 #ifdef CONFIG_CIFS_STATS2
94 now = jiffies;
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);
108 #endif
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"));
118 return NULL;
120 temp = (struct oplock_q_entry *) kmem_cache_alloc(cifs_oplock_cachep,
121 GFP_KERNEL);
122 if (temp == NULL)
123 return temp;
124 else {
125 temp->pinode = pinode;
126 temp->tcon = tcon;
127 temp->netfid = fid;
128 spin_lock(&GlobalMid_Lock);
129 list_add_tail(&temp->qhead, &GlobalOplock_Q);
130 spin_unlock(&GlobalMid_Lock);
132 return temp;
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 void DeleteTconOplockQEntries(struct cifsTconInfo *tcon)
148 struct oplock_q_entry *temp;
150 if (tcon == NULL)
151 return;
153 spin_lock(&GlobalMid_Lock);
154 list_for_each_entry(temp, &GlobalOplock_Q, qhead) {
155 if ((temp->tcon) && (temp->tcon == tcon)) {
156 list_del(&temp->qhead);
157 kmem_cache_free(cifs_oplock_cachep, temp);
160 spin_unlock(&GlobalMid_Lock);
164 smb_send(struct socket *ssocket, struct smb_hdr *smb_buffer,
165 unsigned int smb_buf_length, struct sockaddr *sin, bool noblocksnd)
167 int rc = 0;
168 int i = 0;
169 struct msghdr smb_msg;
170 struct kvec iov;
171 unsigned len = smb_buf_length + 4;
173 if (ssocket == NULL)
174 return -ENOTSOCK; /* BB eventually add reconnect code here */
175 iov.iov_base = smb_buffer;
176 iov.iov_len = len;
178 smb_msg.msg_name = sin;
179 smb_msg.msg_namelen = sizeof(struct sockaddr);
180 smb_msg.msg_control = NULL;
181 smb_msg.msg_controllen = 0;
182 if (noblocksnd)
183 smb_msg.msg_flags = MSG_DONTWAIT + MSG_NOSIGNAL;
184 else
185 smb_msg.msg_flags = MSG_NOSIGNAL;
187 /* smb header is converted in header_assemble. bcc and rest of SMB word
188 area, and byte area if necessary, is converted to littleendian in
189 cifssmb.c and RFC1001 len is converted to bigendian in smb_send
190 Flags2 is converted in SendReceive */
192 smb_buffer->smb_buf_length = cpu_to_be32(smb_buffer->smb_buf_length);
193 cFYI(1, ("Sending smb of length %d", smb_buf_length));
194 dump_smb(smb_buffer, len);
196 while (len > 0) {
197 rc = kernel_sendmsg(ssocket, &smb_msg, &iov, 1, len);
198 if ((rc == -ENOSPC) || (rc == -EAGAIN)) {
199 i++;
200 /* smaller timeout here than send2 since smaller size */
201 /* Although it may not be required, this also is smaller
202 oplock break time */
203 if (i > 12) {
204 cERROR(1,
205 ("sends on sock %p stuck for 7 seconds",
206 ssocket));
207 rc = -EAGAIN;
208 break;
210 msleep(1 << i);
211 continue;
213 if (rc < 0)
214 break;
215 else
216 i = 0; /* reset i after each successful send */
217 iov.iov_base += rc;
218 iov.iov_len -= rc;
219 len -= rc;
222 if (rc < 0) {
223 cERROR(1, ("Error %d sending data on socket to server", rc));
224 } else {
225 rc = 0;
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;
232 return rc;
235 static int
236 smb_send2(struct TCP_Server_Info *server, struct kvec *iov, int n_vec,
237 struct sockaddr *sin, bool noblocksnd)
239 int rc = 0;
240 int i = 0;
241 struct msghdr smb_msg;
242 struct smb_hdr *smb_buffer = iov[0].iov_base;
243 unsigned int len = iov[0].iov_len;
244 unsigned int total_len;
245 int first_vec = 0;
246 unsigned int smb_buf_length = smb_buffer->smb_buf_length;
247 struct socket *ssocket = server->ssocket;
249 if (ssocket == NULL)
250 return -ENOTSOCK; /* BB eventually add reconnect code here */
252 smb_msg.msg_name = sin;
253 smb_msg.msg_namelen = sizeof(struct sockaddr);
254 smb_msg.msg_control = NULL;
255 smb_msg.msg_controllen = 0;
256 if (noblocksnd)
257 smb_msg.msg_flags = MSG_DONTWAIT + MSG_NOSIGNAL;
258 else
259 smb_msg.msg_flags = MSG_NOSIGNAL;
261 /* smb header is converted in header_assemble. bcc and rest of SMB word
262 area, and byte area if necessary, is converted to littleendian in
263 cifssmb.c and RFC1001 len is converted to bigendian in smb_send
264 Flags2 is converted in SendReceive */
267 total_len = 0;
268 for (i = 0; i < n_vec; i++)
269 total_len += iov[i].iov_len;
271 smb_buffer->smb_buf_length = cpu_to_be32(smb_buffer->smb_buf_length);
272 cFYI(1, ("Sending smb: total_len %d", total_len));
273 dump_smb(smb_buffer, len);
275 i = 0;
276 while (total_len) {
277 rc = kernel_sendmsg(ssocket, &smb_msg, &iov[first_vec],
278 n_vec - first_vec, total_len);
279 if ((rc == -ENOSPC) || (rc == -EAGAIN)) {
280 i++;
281 if (i >= 14) {
282 cERROR(1,
283 ("sends on sock %p stuck for 15 seconds",
284 ssocket));
285 rc = -EAGAIN;
286 break;
288 msleep(1 << i);
289 continue;
291 if (rc < 0)
292 break;
294 if (rc == total_len) {
295 total_len = 0;
296 break;
297 } else if (rc > total_len) {
298 cERROR(1, ("sent %d requested %d", rc, total_len));
299 break;
301 if (rc == 0) {
302 /* should never happen, letting socket clear before
303 retrying is our only obvious option here */
304 cERROR(1, ("tcp sent no data"));
305 msleep(500);
306 continue;
308 total_len -= rc;
309 /* the line below resets i */
310 for (i = first_vec; i < n_vec; i++) {
311 if (iov[i].iov_len) {
312 if (rc > iov[i].iov_len) {
313 rc -= iov[i].iov_len;
314 iov[i].iov_len = 0;
315 } else {
316 iov[i].iov_base += rc;
317 iov[i].iov_len -= rc;
318 first_vec = i;
319 break;
323 i = 0; /* in case we get ENOSPC on the next send */
326 if ((total_len > 0) && (total_len != smb_buf_length + 4)) {
327 cFYI(1, ("partial send (%d remaining), terminating session",
328 total_len));
329 /* If we have only sent part of an SMB then the next SMB
330 could be taken as the remainder of this one. We need
331 to kill the socket so the server throws away the partial
332 SMB */
333 server->tcpStatus = CifsNeedReconnect;
336 if (rc < 0) {
337 cERROR(1, ("Error %d sending data on socket to server", rc));
338 } else
339 rc = 0;
341 /* Don't want to modify the buffer as a
342 side effect of this call. */
343 smb_buffer->smb_buf_length = smb_buf_length;
345 return rc;
348 static int wait_for_free_request(struct cifsSesInfo *ses, const int long_op)
350 if (long_op == CIFS_ASYNC_OP) {
351 /* oplock breaks must not be held up */
352 atomic_inc(&ses->server->inFlight);
353 } else {
354 spin_lock(&GlobalMid_Lock);
355 while (1) {
356 if (atomic_read(&ses->server->inFlight) >=
357 cifs_max_pending){
358 spin_unlock(&GlobalMid_Lock);
359 #ifdef CONFIG_CIFS_STATS2
360 atomic_inc(&ses->server->num_waiters);
361 #endif
362 wait_event(ses->server->request_q,
363 atomic_read(&ses->server->inFlight)
364 < cifs_max_pending);
365 #ifdef CONFIG_CIFS_STATS2
366 atomic_dec(&ses->server->num_waiters);
367 #endif
368 spin_lock(&GlobalMid_Lock);
369 } else {
370 if (ses->server->tcpStatus == CifsExiting) {
371 spin_unlock(&GlobalMid_Lock);
372 return -ENOENT;
375 /* can not count locking commands against total
376 as they are allowed to block on server */
378 /* update # of requests on the wire to server */
379 if (long_op != CIFS_BLOCKING_OP)
380 atomic_inc(&ses->server->inFlight);
381 spin_unlock(&GlobalMid_Lock);
382 break;
386 return 0;
389 static int allocate_mid(struct cifsSesInfo *ses, struct smb_hdr *in_buf,
390 struct mid_q_entry **ppmidQ)
392 if (ses->server->tcpStatus == CifsExiting) {
393 return -ENOENT;
394 } else if (ses->server->tcpStatus == CifsNeedReconnect) {
395 cFYI(1, ("tcp session dead - return to caller to retry"));
396 return -EAGAIN;
397 } else if (ses->status != CifsGood) {
398 /* check if SMB session is bad because we are setting it up */
399 if ((in_buf->Command != SMB_COM_SESSION_SETUP_ANDX) &&
400 (in_buf->Command != SMB_COM_NEGOTIATE))
401 return -EAGAIN;
402 /* else ok - we are setting up session */
404 *ppmidQ = AllocMidQEntry(in_buf, ses);
405 if (*ppmidQ == NULL)
406 return -ENOMEM;
407 return 0;
410 static int wait_for_response(struct cifsSesInfo *ses,
411 struct mid_q_entry *midQ,
412 unsigned long timeout,
413 unsigned long time_to_wait)
415 unsigned long curr_timeout;
417 for (;;) {
418 curr_timeout = timeout + jiffies;
419 wait_event(ses->server->response_q,
420 (!(midQ->midState == MID_REQUEST_SUBMITTED)) ||
421 time_after(jiffies, curr_timeout) ||
422 ((ses->server->tcpStatus != CifsGood) &&
423 (ses->server->tcpStatus != CifsNew)));
425 if (time_after(jiffies, curr_timeout) &&
426 (midQ->midState == MID_REQUEST_SUBMITTED) &&
427 ((ses->server->tcpStatus == CifsGood) ||
428 (ses->server->tcpStatus == CifsNew))) {
430 unsigned long lrt;
432 /* We timed out. Is the server still
433 sending replies ? */
434 spin_lock(&GlobalMid_Lock);
435 lrt = ses->server->lstrp;
436 spin_unlock(&GlobalMid_Lock);
438 /* Calculate time_to_wait past last receive time.
439 Although we prefer not to time out if the
440 server is still responding - we will time
441 out if the server takes more than 15 (or 45
442 or 180) seconds to respond to this request
443 and has not responded to any request from
444 other threads on the client within 10 seconds */
445 lrt += time_to_wait;
446 if (time_after(jiffies, lrt)) {
447 /* No replies for time_to_wait. */
448 cERROR(1, ("server not responding"));
449 return -1;
451 } else {
452 return 0;
460 * Send an SMB Request. No response info (other than return code)
461 * needs to be parsed.
463 * flags indicate the type of request buffer and how long to wait
464 * and whether to log NT STATUS code (error) before mapping it to POSIX error
468 SendReceiveNoRsp(const unsigned int xid, struct cifsSesInfo *ses,
469 struct smb_hdr *in_buf, int flags)
471 int rc;
472 struct kvec iov[1];
473 int resp_buf_type;
475 iov[0].iov_base = (char *)in_buf;
476 iov[0].iov_len = in_buf->smb_buf_length + 4;
477 flags |= CIFS_NO_RESP;
478 rc = SendReceive2(xid, ses, iov, 1, &resp_buf_type, flags);
479 cFYI(DBG2, ("SendRcvNoRsp flags %d rc %d", flags, rc));
481 return rc;
485 SendReceive2(const unsigned int xid, struct cifsSesInfo *ses,
486 struct kvec *iov, int n_vec, int *pRespBufType /* ret */,
487 const int flags)
489 int rc = 0;
490 int long_op;
491 unsigned int receive_len;
492 unsigned long timeout;
493 struct mid_q_entry *midQ;
494 struct smb_hdr *in_buf = iov[0].iov_base;
496 long_op = flags & CIFS_TIMEOUT_MASK;
498 *pRespBufType = CIFS_NO_BUFFER; /* no response buf yet */
500 if ((ses == NULL) || (ses->server == NULL)) {
501 cifs_small_buf_release(in_buf);
502 cERROR(1, ("Null session"));
503 return -EIO;
506 if (ses->server->tcpStatus == CifsExiting) {
507 cifs_small_buf_release(in_buf);
508 return -ENOENT;
511 /* Ensure that we do not send more than 50 overlapping requests
512 to the same server. We may make this configurable later or
513 use ses->maxReq */
515 rc = wait_for_free_request(ses, long_op);
516 if (rc) {
517 cifs_small_buf_release(in_buf);
518 return rc;
521 /* make sure that we sign in the same order that we send on this socket
522 and avoid races inside tcp sendmsg code that could cause corruption
523 of smb data */
525 down(&ses->server->tcpSem);
527 rc = allocate_mid(ses, in_buf, &midQ);
528 if (rc) {
529 up(&ses->server->tcpSem);
530 cifs_small_buf_release(in_buf);
531 /* Update # of requests on wire to server */
532 atomic_dec(&ses->server->inFlight);
533 wake_up(&ses->server->request_q);
534 return rc;
536 rc = cifs_sign_smb2(iov, n_vec, ses->server, &midQ->sequence_number);
538 midQ->midState = MID_REQUEST_SUBMITTED;
539 #ifdef CONFIG_CIFS_STATS2
540 atomic_inc(&ses->server->inSend);
541 #endif
542 rc = smb_send2(ses->server, iov, n_vec,
543 (struct sockaddr *) &(ses->server->addr.sockAddr),
544 ses->server->noblocksnd);
545 #ifdef CONFIG_CIFS_STATS2
546 atomic_dec(&ses->server->inSend);
547 midQ->when_sent = jiffies;
548 #endif
550 up(&ses->server->tcpSem);
551 cifs_small_buf_release(in_buf);
553 if (rc < 0)
554 goto out;
556 if (long_op == CIFS_STD_OP)
557 timeout = 15 * HZ;
558 else if (long_op == CIFS_VLONG_OP) /* e.g. slow writes past EOF */
559 timeout = 180 * HZ;
560 else if (long_op == CIFS_LONG_OP)
561 timeout = 45 * HZ; /* should be greater than
562 servers oplock break timeout (about 43 seconds) */
563 else if (long_op == CIFS_ASYNC_OP)
564 goto out;
565 else if (long_op == CIFS_BLOCKING_OP)
566 timeout = 0x7FFFFFFF; /* large, but not so large as to wrap */
567 else {
568 cERROR(1, ("unknown timeout flag %d", long_op));
569 rc = -EIO;
570 goto out;
573 /* wait for 15 seconds or until woken up due to response arriving or
574 due to last connection to this server being unmounted */
575 if (signal_pending(current)) {
576 /* if signal pending do not hold up user for full smb timeout
577 but we still give response a chance to complete */
578 timeout = 2 * HZ;
581 /* No user interrupts in wait - wreaks havoc with performance */
582 wait_for_response(ses, midQ, timeout, 10 * HZ);
584 spin_lock(&GlobalMid_Lock);
585 if (midQ->resp_buf) {
586 spin_unlock(&GlobalMid_Lock);
587 receive_len = midQ->resp_buf->smb_buf_length;
588 } else {
589 cERROR(1, ("No response to cmd %d mid %d",
590 midQ->command, midQ->mid));
591 if (midQ->midState == MID_REQUEST_SUBMITTED) {
592 if (ses->server->tcpStatus == CifsExiting)
593 rc = -EHOSTDOWN;
594 else {
595 ses->server->tcpStatus = CifsNeedReconnect;
596 midQ->midState = MID_RETRY_NEEDED;
600 if (rc != -EHOSTDOWN) {
601 if (midQ->midState == MID_RETRY_NEEDED) {
602 rc = -EAGAIN;
603 cFYI(1, ("marking request for retry"));
604 } else {
605 rc = -EIO;
608 spin_unlock(&GlobalMid_Lock);
609 DeleteMidQEntry(midQ);
610 /* Update # of requests on wire to server */
611 atomic_dec(&ses->server->inFlight);
612 wake_up(&ses->server->request_q);
613 return rc;
616 if (receive_len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE) {
617 cERROR(1, ("Frame too large received. Length: %d Xid: %d",
618 receive_len, xid));
619 rc = -EIO;
620 } else { /* rcvd frame is ok */
621 if (midQ->resp_buf &&
622 (midQ->midState == MID_RESPONSE_RECEIVED)) {
624 iov[0].iov_base = (char *)midQ->resp_buf;
625 if (midQ->largeBuf)
626 *pRespBufType = CIFS_LARGE_BUFFER;
627 else
628 *pRespBufType = CIFS_SMALL_BUFFER;
629 iov[0].iov_len = receive_len + 4;
631 dump_smb(midQ->resp_buf, 80);
632 /* convert the length into a more usable form */
633 if ((receive_len > 24) &&
634 (ses->server->secMode & (SECMODE_SIGN_REQUIRED |
635 SECMODE_SIGN_ENABLED))) {
636 rc = cifs_verify_signature(midQ->resp_buf,
637 &ses->server->mac_signing_key,
638 midQ->sequence_number+1);
639 if (rc) {
640 cERROR(1, ("Unexpected SMB signature"));
641 /* BB FIXME add code to kill session */
645 /* BB special case reconnect tid and uid here? */
646 rc = map_smb_to_linux_error(midQ->resp_buf,
647 flags & CIFS_LOG_ERROR);
649 /* convert ByteCount if necessary */
650 if (receive_len >= sizeof(struct smb_hdr) - 4
651 /* do not count RFC1001 header */ +
652 (2 * midQ->resp_buf->WordCount) + 2 /* bcc */ )
653 BCC(midQ->resp_buf) =
654 le16_to_cpu(BCC_LE(midQ->resp_buf));
655 if ((flags & CIFS_NO_RESP) == 0)
656 midQ->resp_buf = NULL; /* mark it so buf will
657 not be freed by
658 DeleteMidQEntry */
659 } else {
660 rc = -EIO;
661 cFYI(1, ("Bad MID state?"));
665 out:
666 DeleteMidQEntry(midQ);
667 atomic_dec(&ses->server->inFlight);
668 wake_up(&ses->server->request_q);
670 return rc;
674 SendReceive(const unsigned int xid, struct cifsSesInfo *ses,
675 struct smb_hdr *in_buf, struct smb_hdr *out_buf,
676 int *pbytes_returned, const int long_op)
678 int rc = 0;
679 unsigned int receive_len;
680 unsigned long timeout;
681 struct mid_q_entry *midQ;
683 if (ses == NULL) {
684 cERROR(1, ("Null smb session"));
685 return -EIO;
687 if (ses->server == NULL) {
688 cERROR(1, ("Null tcp session"));
689 return -EIO;
692 if (ses->server->tcpStatus == CifsExiting)
693 return -ENOENT;
695 /* Ensure that we do not send more than 50 overlapping requests
696 to the same server. We may make this configurable later or
697 use ses->maxReq */
699 rc = wait_for_free_request(ses, long_op);
700 if (rc)
701 return rc;
703 /* make sure that we sign in the same order that we send on this socket
704 and avoid races inside tcp sendmsg code that could cause corruption
705 of smb data */
707 down(&ses->server->tcpSem);
709 rc = allocate_mid(ses, in_buf, &midQ);
710 if (rc) {
711 up(&ses->server->tcpSem);
712 /* Update # of requests on wire to server */
713 atomic_dec(&ses->server->inFlight);
714 wake_up(&ses->server->request_q);
715 return rc;
718 if (in_buf->smb_buf_length > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
719 cERROR(1, ("Illegal length, greater than maximum frame, %d",
720 in_buf->smb_buf_length));
721 DeleteMidQEntry(midQ);
722 up(&ses->server->tcpSem);
723 /* Update # of requests on wire to server */
724 atomic_dec(&ses->server->inFlight);
725 wake_up(&ses->server->request_q);
726 return -EIO;
729 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
731 midQ->midState = MID_REQUEST_SUBMITTED;
732 #ifdef CONFIG_CIFS_STATS2
733 atomic_inc(&ses->server->inSend);
734 #endif
735 rc = smb_send(ses->server->ssocket, in_buf, in_buf->smb_buf_length,
736 (struct sockaddr *) &(ses->server->addr.sockAddr),
737 ses->server->noblocksnd);
738 #ifdef CONFIG_CIFS_STATS2
739 atomic_dec(&ses->server->inSend);
740 midQ->when_sent = jiffies;
741 #endif
742 up(&ses->server->tcpSem);
744 if (rc < 0)
745 goto out;
747 if (long_op == CIFS_STD_OP)
748 timeout = 15 * HZ;
749 /* wait for 15 seconds or until woken up due to response arriving or
750 due to last connection to this server being unmounted */
751 else if (long_op == CIFS_ASYNC_OP)
752 goto out;
753 else if (long_op == CIFS_VLONG_OP) /* writes past EOF can be slow */
754 timeout = 180 * HZ;
755 else if (long_op == CIFS_LONG_OP)
756 timeout = 45 * HZ; /* should be greater than
757 servers oplock break timeout (about 43 seconds) */
758 else if (long_op == CIFS_BLOCKING_OP)
759 timeout = 0x7FFFFFFF; /* large but no so large as to wrap */
760 else {
761 cERROR(1, ("unknown timeout flag %d", long_op));
762 rc = -EIO;
763 goto out;
766 if (signal_pending(current)) {
767 /* if signal pending do not hold up user for full smb timeout
768 but we still give response a chance to complete */
769 timeout = 2 * HZ;
772 /* No user interrupts in wait - wreaks havoc with performance */
773 wait_for_response(ses, midQ, timeout, 10 * HZ);
775 spin_lock(&GlobalMid_Lock);
776 if (midQ->resp_buf) {
777 spin_unlock(&GlobalMid_Lock);
778 receive_len = midQ->resp_buf->smb_buf_length;
779 } else {
780 cERROR(1, ("No response for cmd %d mid %d",
781 midQ->command, midQ->mid));
782 if (midQ->midState == MID_REQUEST_SUBMITTED) {
783 if (ses->server->tcpStatus == CifsExiting)
784 rc = -EHOSTDOWN;
785 else {
786 ses->server->tcpStatus = CifsNeedReconnect;
787 midQ->midState = MID_RETRY_NEEDED;
791 if (rc != -EHOSTDOWN) {
792 if (midQ->midState == MID_RETRY_NEEDED) {
793 rc = -EAGAIN;
794 cFYI(1, ("marking request for retry"));
795 } else {
796 rc = -EIO;
799 spin_unlock(&GlobalMid_Lock);
800 DeleteMidQEntry(midQ);
801 /* Update # of requests on wire to server */
802 atomic_dec(&ses->server->inFlight);
803 wake_up(&ses->server->request_q);
804 return rc;
807 if (receive_len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE) {
808 cERROR(1, ("Frame too large received. Length: %d Xid: %d",
809 receive_len, xid));
810 rc = -EIO;
811 } else { /* rcvd frame is ok */
813 if (midQ->resp_buf && out_buf
814 && (midQ->midState == MID_RESPONSE_RECEIVED)) {
815 out_buf->smb_buf_length = receive_len;
816 memcpy((char *)out_buf + 4,
817 (char *)midQ->resp_buf + 4,
818 receive_len);
820 dump_smb(out_buf, 92);
821 /* convert the length into a more usable form */
822 if ((receive_len > 24) &&
823 (ses->server->secMode & (SECMODE_SIGN_REQUIRED |
824 SECMODE_SIGN_ENABLED))) {
825 rc = cifs_verify_signature(out_buf,
826 &ses->server->mac_signing_key,
827 midQ->sequence_number+1);
828 if (rc) {
829 cERROR(1, ("Unexpected SMB signature"));
830 /* BB FIXME add code to kill session */
834 *pbytes_returned = out_buf->smb_buf_length;
836 /* BB special case reconnect tid and uid here? */
837 rc = map_smb_to_linux_error(out_buf, 0 /* no log */ );
839 /* convert ByteCount if necessary */
840 if (receive_len >= sizeof(struct smb_hdr) - 4
841 /* do not count RFC1001 header */ +
842 (2 * out_buf->WordCount) + 2 /* bcc */ )
843 BCC(out_buf) = le16_to_cpu(BCC_LE(out_buf));
844 } else {
845 rc = -EIO;
846 cERROR(1, ("Bad MID state?"));
850 out:
851 DeleteMidQEntry(midQ);
852 atomic_dec(&ses->server->inFlight);
853 wake_up(&ses->server->request_q);
855 return rc;
858 /* Send an NT_CANCEL SMB to cause the POSIX blocking lock to return. */
860 static int
861 send_nt_cancel(struct cifsTconInfo *tcon, struct smb_hdr *in_buf,
862 struct mid_q_entry *midQ)
864 int rc = 0;
865 struct cifsSesInfo *ses = tcon->ses;
866 __u16 mid = in_buf->Mid;
868 header_assemble(in_buf, SMB_COM_NT_CANCEL, tcon, 0);
869 in_buf->Mid = mid;
870 down(&ses->server->tcpSem);
871 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
872 if (rc) {
873 up(&ses->server->tcpSem);
874 return rc;
876 rc = smb_send(ses->server->ssocket, in_buf, in_buf->smb_buf_length,
877 (struct sockaddr *) &(ses->server->addr.sockAddr),
878 ses->server->noblocksnd);
879 up(&ses->server->tcpSem);
880 return rc;
883 /* We send a LOCKINGX_CANCEL_LOCK to cause the Windows
884 blocking lock to return. */
886 static int
887 send_lock_cancel(const unsigned int xid, struct cifsTconInfo *tcon,
888 struct smb_hdr *in_buf,
889 struct smb_hdr *out_buf)
891 int bytes_returned;
892 struct cifsSesInfo *ses = tcon->ses;
893 LOCK_REQ *pSMB = (LOCK_REQ *)in_buf;
895 /* We just modify the current in_buf to change
896 the type of lock from LOCKING_ANDX_SHARED_LOCK
897 or LOCKING_ANDX_EXCLUSIVE_LOCK to
898 LOCKING_ANDX_CANCEL_LOCK. */
900 pSMB->LockType = LOCKING_ANDX_CANCEL_LOCK|LOCKING_ANDX_LARGE_FILES;
901 pSMB->Timeout = 0;
902 pSMB->hdr.Mid = GetNextMid(ses->server);
904 return SendReceive(xid, ses, in_buf, out_buf,
905 &bytes_returned, CIFS_STD_OP);
909 SendReceiveBlockingLock(const unsigned int xid, struct cifsTconInfo *tcon,
910 struct smb_hdr *in_buf, struct smb_hdr *out_buf,
911 int *pbytes_returned)
913 int rc = 0;
914 int rstart = 0;
915 unsigned int receive_len;
916 struct mid_q_entry *midQ;
917 struct cifsSesInfo *ses;
919 if (tcon == NULL || tcon->ses == NULL) {
920 cERROR(1, ("Null smb session"));
921 return -EIO;
923 ses = tcon->ses;
925 if (ses->server == NULL) {
926 cERROR(1, ("Null tcp session"));
927 return -EIO;
930 if (ses->server->tcpStatus == CifsExiting)
931 return -ENOENT;
933 /* Ensure that we do not send more than 50 overlapping requests
934 to the same server. We may make this configurable later or
935 use ses->maxReq */
937 rc = wait_for_free_request(ses, CIFS_BLOCKING_OP);
938 if (rc)
939 return rc;
941 /* make sure that we sign in the same order that we send on this socket
942 and avoid races inside tcp sendmsg code that could cause corruption
943 of smb data */
945 down(&ses->server->tcpSem);
947 rc = allocate_mid(ses, in_buf, &midQ);
948 if (rc) {
949 up(&ses->server->tcpSem);
950 return rc;
953 if (in_buf->smb_buf_length > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
954 up(&ses->server->tcpSem);
955 cERROR(1, ("Illegal length, greater than maximum frame, %d",
956 in_buf->smb_buf_length));
957 DeleteMidQEntry(midQ);
958 return -EIO;
961 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
963 midQ->midState = MID_REQUEST_SUBMITTED;
964 #ifdef CONFIG_CIFS_STATS2
965 atomic_inc(&ses->server->inSend);
966 #endif
967 rc = smb_send(ses->server->ssocket, in_buf, in_buf->smb_buf_length,
968 (struct sockaddr *) &(ses->server->addr.sockAddr),
969 ses->server->noblocksnd);
970 #ifdef CONFIG_CIFS_STATS2
971 atomic_dec(&ses->server->inSend);
972 midQ->when_sent = jiffies;
973 #endif
974 up(&ses->server->tcpSem);
976 if (rc < 0) {
977 DeleteMidQEntry(midQ);
978 return rc;
981 /* Wait for a reply - allow signals to interrupt. */
982 rc = wait_event_interruptible(ses->server->response_q,
983 (!(midQ->midState == MID_REQUEST_SUBMITTED)) ||
984 ((ses->server->tcpStatus != CifsGood) &&
985 (ses->server->tcpStatus != CifsNew)));
987 /* Were we interrupted by a signal ? */
988 if ((rc == -ERESTARTSYS) &&
989 (midQ->midState == MID_REQUEST_SUBMITTED) &&
990 ((ses->server->tcpStatus == CifsGood) ||
991 (ses->server->tcpStatus == CifsNew))) {
993 if (in_buf->Command == SMB_COM_TRANSACTION2) {
994 /* POSIX lock. We send a NT_CANCEL SMB to cause the
995 blocking lock to return. */
997 rc = send_nt_cancel(tcon, in_buf, midQ);
998 if (rc) {
999 DeleteMidQEntry(midQ);
1000 return rc;
1002 } else {
1003 /* Windows lock. We send a LOCKINGX_CANCEL_LOCK
1004 to cause the blocking lock to return. */
1006 rc = send_lock_cancel(xid, tcon, in_buf, out_buf);
1008 /* If we get -ENOLCK back the lock may have
1009 already been removed. Don't exit in this case. */
1010 if (rc && rc != -ENOLCK) {
1011 DeleteMidQEntry(midQ);
1012 return rc;
1016 /* Wait 5 seconds for the response. */
1017 if (wait_for_response(ses, midQ, 5 * HZ, 5 * HZ) == 0) {
1018 /* We got the response - restart system call. */
1019 rstart = 1;
1023 spin_lock(&GlobalMid_Lock);
1024 if (midQ->resp_buf) {
1025 spin_unlock(&GlobalMid_Lock);
1026 receive_len = midQ->resp_buf->smb_buf_length;
1027 } else {
1028 cERROR(1, ("No response for cmd %d mid %d",
1029 midQ->command, midQ->mid));
1030 if (midQ->midState == MID_REQUEST_SUBMITTED) {
1031 if (ses->server->tcpStatus == CifsExiting)
1032 rc = -EHOSTDOWN;
1033 else {
1034 ses->server->tcpStatus = CifsNeedReconnect;
1035 midQ->midState = MID_RETRY_NEEDED;
1039 if (rc != -EHOSTDOWN) {
1040 if (midQ->midState == MID_RETRY_NEEDED) {
1041 rc = -EAGAIN;
1042 cFYI(1, ("marking request for retry"));
1043 } else {
1044 rc = -EIO;
1047 spin_unlock(&GlobalMid_Lock);
1048 DeleteMidQEntry(midQ);
1049 return rc;
1052 if (receive_len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE) {
1053 cERROR(1, ("Frame too large received. Length: %d Xid: %d",
1054 receive_len, xid));
1055 rc = -EIO;
1056 } else { /* rcvd frame is ok */
1058 if (midQ->resp_buf && out_buf
1059 && (midQ->midState == MID_RESPONSE_RECEIVED)) {
1060 out_buf->smb_buf_length = receive_len;
1061 memcpy((char *)out_buf + 4,
1062 (char *)midQ->resp_buf + 4,
1063 receive_len);
1065 dump_smb(out_buf, 92);
1066 /* convert the length into a more usable form */
1067 if ((receive_len > 24) &&
1068 (ses->server->secMode & (SECMODE_SIGN_REQUIRED |
1069 SECMODE_SIGN_ENABLED))) {
1070 rc = cifs_verify_signature(out_buf,
1071 &ses->server->mac_signing_key,
1072 midQ->sequence_number+1);
1073 if (rc) {
1074 cERROR(1, ("Unexpected SMB signature"));
1075 /* BB FIXME add code to kill session */
1079 *pbytes_returned = out_buf->smb_buf_length;
1081 /* BB special case reconnect tid and uid here? */
1082 rc = map_smb_to_linux_error(out_buf, 0 /* no log */ );
1084 /* convert ByteCount if necessary */
1085 if (receive_len >= sizeof(struct smb_hdr) - 4
1086 /* do not count RFC1001 header */ +
1087 (2 * out_buf->WordCount) + 2 /* bcc */ )
1088 BCC(out_buf) = le16_to_cpu(BCC_LE(out_buf));
1089 } else {
1090 rc = -EIO;
1091 cERROR(1, ("Bad MID state?"));
1094 DeleteMidQEntry(midQ);
1095 if (rstart && rc == -EACCES)
1096 return -ERESTARTSYS;
1097 return rc;