4 * Copyright (C) International Business Machines Corp., 2002,2005
5 * Author(s): Steve French (sfrench@us.ibm.com)
7 * This library is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published
9 * by the Free Software Foundation; either version 2.1 of the License, or
10 * (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
15 * the GNU Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/list.h>
24 #include <linux/wait.h>
25 #include <linux/net.h>
26 #include <linux/delay.h>
27 #include <asm/uaccess.h>
28 #include <asm/processor.h>
29 #include <linux/mempool.h>
32 #include "cifsproto.h"
33 #include "cifs_debug.h"
35 extern mempool_t *cifs_mid_poolp;
36 extern kmem_cache_t *cifs_oplock_cachep;
38 static struct mid_q_entry *
39 AllocMidQEntry(struct smb_hdr *smb_buffer, struct cifsSesInfo *ses)
41 struct mid_q_entry *temp;
44 cERROR(1, ("Null session passed in to AllocMidQEntry"));
47 if (ses->server == NULL) {
48 cERROR(1, ("Null TCP session in AllocMidQEntry"));
52 temp = (struct mid_q_entry *) mempool_alloc(cifs_mid_poolp,
53 SLAB_KERNEL | SLAB_NOFS);
57 memset(temp, 0, sizeof (struct mid_q_entry));
58 temp->mid = smb_buffer->Mid; /* always LE */
59 temp->pid = current->pid;
60 temp->command = smb_buffer->Command;
61 cFYI(1, ("For smb_command %d", temp->command));
62 do_gettimeofday(&temp->when_sent);
67 spin_lock(&GlobalMid_Lock);
68 list_add_tail(&temp->qhead, &ses->server->pending_mid_q);
69 atomic_inc(&midCount);
70 temp->midState = MID_REQUEST_ALLOCATED;
71 spin_unlock(&GlobalMid_Lock);
76 DeleteMidQEntry(struct mid_q_entry *midEntry)
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 mempool_free(midEntry, cifs_mid_poolp);
90 struct oplock_q_entry *
91 AllocOplockQEntry(struct inode * pinode, __u16 fid, struct cifsTconInfo * tcon)
93 struct oplock_q_entry *temp;
94 if ((pinode== NULL) || (tcon == NULL)) {
95 cERROR(1, ("Null parms passed to AllocOplockQEntry"));
98 temp = (struct oplock_q_entry *) kmem_cache_alloc(cifs_oplock_cachep,
103 temp->pinode = pinode;
106 spin_lock(&GlobalMid_Lock);
107 list_add_tail(&temp->qhead, &GlobalOplock_Q);
108 spin_unlock(&GlobalMid_Lock);
114 void DeleteOplockQEntry(struct oplock_q_entry * oplockEntry)
116 spin_lock(&GlobalMid_Lock);
117 /* should we check if list empty first? */
118 list_del(&oplockEntry->qhead);
119 spin_unlock(&GlobalMid_Lock);
120 kmem_cache_free(cifs_oplock_cachep, oplockEntry);
124 smb_send(struct socket *ssocket, struct smb_hdr *smb_buffer,
125 unsigned int smb_buf_length, struct sockaddr *sin)
129 struct msghdr smb_msg;
131 unsigned len = smb_buf_length + 4;
134 return -ENOTSOCK; /* BB eventually add reconnect code here */
135 iov.iov_base = smb_buffer;
138 smb_msg.msg_name = sin;
139 smb_msg.msg_namelen = sizeof (struct sockaddr);
140 smb_msg.msg_control = NULL;
141 smb_msg.msg_controllen = 0;
142 smb_msg.msg_flags = MSG_DONTWAIT + MSG_NOSIGNAL; /* BB add more flags?*/
144 /* smb header is converted in header_assemble. bcc and rest of SMB word
145 area, and byte area if necessary, is converted to littleendian in
146 cifssmb.c and RFC1001 len is converted to bigendian in smb_send
147 Flags2 is converted in SendReceive */
149 smb_buffer->smb_buf_length = cpu_to_be32(smb_buffer->smb_buf_length);
150 cFYI(1, ("Sending smb of length %d ", smb_buf_length));
151 dump_smb(smb_buffer, len);
154 rc = kernel_sendmsg(ssocket, &smb_msg, &iov, 1, len);
155 if ((rc == -ENOSPC) || (rc == -EAGAIN)) {
159 ("sends on sock %p stuck for 30 seconds",
175 cERROR(1,("Error %d sending data on socket to server.", rc));
183 #ifdef CONFIG_CIFS_EXPERIMENTAL
185 smb_send2(struct socket *ssocket, struct smb_hdr *smb_buffer,
186 unsigned int smb_hdr_length, const char * data, unsigned int datalen,
187 struct sockaddr *sin)
191 struct msghdr smb_msg;
193 unsigned len = smb_hdr_length + 4;
196 return -ENOTSOCK; /* BB eventually add reconnect code here */
197 iov[0].iov_base = smb_buffer;
198 iov[0].iov_len = len;
199 iov[1].iov_base = data;
200 iov[2].iov_len = datalen;
201 smb_msg.msg_name = sin;
202 smb_msg.msg_namelen = sizeof (struct sockaddr);
203 smb_msg.msg_control = NULL;
204 smb_msg.msg_controllen = 0;
205 smb_msg.msg_flags = MSG_DONTWAIT + MSG_NOSIGNAL; /* BB add more flags?*/
207 /* smb header is converted in header_assemble. bcc and rest of SMB word
208 area, and byte area if necessary, is converted to littleendian in
209 cifssmb.c and RFC1001 len is converted to bigendian in smb_send
210 Flags2 is converted in SendReceive */
212 smb_buffer->smb_buf_length = cpu_to_be32(smb_buffer->smb_buf_length);
213 cFYI(1, ("Sending smb of length %d ", len + datalen));
214 dump_smb(smb_buffer, len);
216 while (len + datalen > 0) {
217 rc = kernel_sendmsg(ssocket, &smb_msg, iov, 2, len);
218 if ((rc == -ENOSPC) || (rc == -EAGAIN)) {
222 ("sends on sock %p stuck for 30 seconds",
232 if(iov[0].iov_len > 0) {
236 } else { /* some of hdr was not sent */
238 iov[0].iov_len -= rc;
239 iov[0].iov_base += rc;
243 if((iov[0].iov_len == 0) && (rc > 0)){
244 iov[1].iov_base += rc;
245 iov[1].iov_len -= rc;
251 cERROR(1,("Error %d sending data on socket to server.", rc));
260 SendReceive2(const unsigned int xid, struct cifsSesInfo *ses,
261 struct smb_hdr *in_buf, int hdrlen, const char * data,
262 int datalen, int *pbytes_returned, const int long_op)
265 unsigned int receive_len;
266 unsigned long timeout;
267 struct mid_q_entry *midQ;
270 cERROR(1,("Null smb session"));
273 if(ses->server == NULL) {
274 cERROR(1,("Null tcp session"));
278 if(ses->server->tcpStatus == CifsExiting)
281 /* Ensure that we do not send more than 50 overlapping requests
282 to the same server. We may make this configurable later or
285 /* oplock breaks must not be held up */
286 atomic_inc(&ses->server->inFlight);
288 spin_lock(&GlobalMid_Lock);
290 if(atomic_read(&ses->server->inFlight) >=
292 spin_unlock(&GlobalMid_Lock);
293 wait_event(ses->server->request_q,
294 atomic_read(&ses->server->inFlight)
296 spin_lock(&GlobalMid_Lock);
298 if(ses->server->tcpStatus == CifsExiting) {
299 spin_unlock(&GlobalMid_Lock);
303 /* can not count locking commands against total since
304 they are allowed to block on server */
307 /* update # of requests on the wire to server */
308 atomic_inc(&ses->server->inFlight);
310 spin_unlock(&GlobalMid_Lock);
315 /* make sure that we sign in the same order that we send on this socket
316 and avoid races inside tcp sendmsg code that could cause corruption
319 down(&ses->server->tcpSem);
321 if (ses->server->tcpStatus == CifsExiting) {
324 } else if (ses->server->tcpStatus == CifsNeedReconnect) {
325 cFYI(1,("tcp session dead - return to caller to retry"));
328 } else if (ses->status != CifsGood) {
329 /* check if SMB session is bad because we are setting it up */
330 if((in_buf->Command != SMB_COM_SESSION_SETUP_ANDX) &&
331 (in_buf->Command != SMB_COM_NEGOTIATE)) {
334 } /* else ok - we are setting up session */
336 midQ = AllocMidQEntry(in_buf, ses);
338 up(&ses->server->tcpSem);
339 /* If not lock req, update # of requests on wire to server */
341 atomic_dec(&ses->server->inFlight);
342 wake_up(&ses->server->request_q);
347 if (in_buf->smb_buf_length > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
348 up(&ses->server->tcpSem);
350 ("Illegal length, greater than maximum frame, %d ",
351 in_buf->smb_buf_length));
352 DeleteMidQEntry(midQ);
353 /* If not lock req, update # of requests on wire to server */
355 atomic_dec(&ses->server->inFlight);
356 wake_up(&ses->server->request_q);
362 /* rc = cifs_sign_smb2(in_buf, data, ses->server, &midQ->sequence_number); */
364 midQ->midState = MID_REQUEST_SUBMITTED;
365 rc = smb_send2(ses->server->ssocket, in_buf, hdrlen, data, datalen,
366 (struct sockaddr *) &(ses->server->addr.sockAddr));
368 DeleteMidQEntry(midQ);
369 up(&ses->server->tcpSem);
370 /* If not lock req, update # of requests on wire to server */
372 atomic_dec(&ses->server->inFlight);
373 wake_up(&ses->server->request_q);
377 up(&ses->server->tcpSem);
379 goto cifs_no_response_exit2;
380 else if (long_op == 2) /* writes past end of file can take loong time */
382 else if (long_op == 1)
383 timeout = 45 * HZ; /* should be greater than
384 servers oplock break timeout (about 43 seconds) */
385 else if (long_op > 2) {
386 timeout = MAX_SCHEDULE_TIMEOUT;
389 /* wait for 15 seconds or until woken up due to response arriving or
390 due to last connection to this server being unmounted */
391 if (signal_pending(current)) {
392 /* if signal pending do not hold up user for full smb timeout
393 but we still give response a change to complete */
397 /* No user interrupts in wait - wreaks havoc with performance */
398 if(timeout != MAX_SCHEDULE_TIMEOUT) {
400 wait_event(ses->server->response_q,
401 (!(midQ->midState & MID_REQUEST_SUBMITTED)) ||
402 time_after(jiffies, timeout) ||
403 ((ses->server->tcpStatus != CifsGood) &&
404 (ses->server->tcpStatus != CifsNew)));
406 wait_event(ses->server->response_q,
407 (!(midQ->midState & MID_REQUEST_SUBMITTED)) ||
408 ((ses->server->tcpStatus != CifsGood) &&
409 (ses->server->tcpStatus != CifsNew)));
412 spin_lock(&GlobalMid_Lock);
413 if (midQ->resp_buf) {
414 spin_unlock(&GlobalMid_Lock);
415 receive_len = be32_to_cpu(*(__be32 *)midQ->resp_buf);
417 cERROR(1,("No response buffer"));
418 if(midQ->midState == MID_REQUEST_SUBMITTED) {
419 if(ses->server->tcpStatus == CifsExiting)
422 ses->server->tcpStatus = CifsNeedReconnect;
423 midQ->midState = MID_RETRY_NEEDED;
427 if (rc != -EHOSTDOWN) {
428 if(midQ->midState == MID_RETRY_NEEDED) {
430 cFYI(1,("marking request for retry"));
435 spin_unlock(&GlobalMid_Lock);
436 DeleteMidQEntry(midQ);
437 /* If not lock req, update # of requests on wire to server */
439 atomic_dec(&ses->server->inFlight);
440 wake_up(&ses->server->request_q);
445 if (receive_len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE) {
446 cERROR(1, ("Frame too large received. Length: %d Xid: %d",
449 } else { /* rcvd frame is ok */
451 if (midQ->resp_buf &&
452 (midQ->midState == MID_RESPONSE_RECEIVED)) {
453 in_buf->smb_buf_length = receive_len;
454 /* BB verify that length would not overrun small buf */
455 memcpy((char *)in_buf + 4,
456 (char *)midQ->resp_buf + 4,
459 dump_smb(in_buf, 80);
460 /* convert the length into a more usable form */
461 if((receive_len > 24) &&
462 (ses->server->secMode & (SECMODE_SIGN_REQUIRED |
463 SECMODE_SIGN_ENABLED))) {
464 rc = cifs_verify_signature(in_buf,
465 ses->server->mac_signing_key,
466 midQ->sequence_number+1);
468 cERROR(1,("Unexpected SMB signature"));
469 /* BB FIXME add code to kill session */
473 *pbytes_returned = in_buf->smb_buf_length;
475 /* BB special case reconnect tid and uid here? */
476 rc = map_smb_to_linux_error(in_buf);
478 /* convert ByteCount if necessary */
480 sizeof (struct smb_hdr) -
481 4 /* do not count RFC1001 header */ +
482 (2 * in_buf->WordCount) + 2 /* bcc */ )
483 BCC(in_buf) = le16_to_cpu(BCC(in_buf));
486 cFYI(1,("Bad MID state? "));
489 cifs_no_response_exit2:
490 DeleteMidQEntry(midQ);
493 atomic_dec(&ses->server->inFlight);
494 wake_up(&ses->server->request_q);
500 up(&ses->server->tcpSem);
501 /* If not lock req, update # of requests on wire to server */
503 atomic_dec(&ses->server->inFlight);
504 wake_up(&ses->server->request_q);
509 #endif /* CIFS_EXPERIMENTAL */
512 SendReceive(const unsigned int xid, struct cifsSesInfo *ses,
513 struct smb_hdr *in_buf, struct smb_hdr *out_buf,
514 int *pbytes_returned, const int long_op)
517 unsigned int receive_len;
518 unsigned long timeout;
519 struct mid_q_entry *midQ;
522 cERROR(1,("Null smb session"));
525 if(ses->server == NULL) {
526 cERROR(1,("Null tcp session"));
530 if(ses->server->tcpStatus == CifsExiting)
533 /* Ensure that we do not send more than 50 overlapping requests
534 to the same server. We may make this configurable later or
537 /* oplock breaks must not be held up */
538 atomic_inc(&ses->server->inFlight);
540 spin_lock(&GlobalMid_Lock);
542 if(atomic_read(&ses->server->inFlight) >=
544 spin_unlock(&GlobalMid_Lock);
545 wait_event(ses->server->request_q,
546 atomic_read(&ses->server->inFlight)
548 spin_lock(&GlobalMid_Lock);
550 if(ses->server->tcpStatus == CifsExiting) {
551 spin_unlock(&GlobalMid_Lock);
555 /* can not count locking commands against total since
556 they are allowed to block on server */
559 /* update # of requests on the wire to server */
560 atomic_inc(&ses->server->inFlight);
562 spin_unlock(&GlobalMid_Lock);
567 /* make sure that we sign in the same order that we send on this socket
568 and avoid races inside tcp sendmsg code that could cause corruption
571 down(&ses->server->tcpSem);
573 if (ses->server->tcpStatus == CifsExiting) {
576 } else if (ses->server->tcpStatus == CifsNeedReconnect) {
577 cFYI(1,("tcp session dead - return to caller to retry"));
580 } else if (ses->status != CifsGood) {
581 /* check if SMB session is bad because we are setting it up */
582 if((in_buf->Command != SMB_COM_SESSION_SETUP_ANDX) &&
583 (in_buf->Command != SMB_COM_NEGOTIATE)) {
586 } /* else ok - we are setting up session */
588 midQ = AllocMidQEntry(in_buf, ses);
590 up(&ses->server->tcpSem);
591 /* If not lock req, update # of requests on wire to server */
593 atomic_dec(&ses->server->inFlight);
594 wake_up(&ses->server->request_q);
599 if (in_buf->smb_buf_length > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
600 up(&ses->server->tcpSem);
602 ("Illegal length, greater than maximum frame, %d ",
603 in_buf->smb_buf_length));
604 DeleteMidQEntry(midQ);
605 /* If not lock req, update # of requests on wire to server */
607 atomic_dec(&ses->server->inFlight);
608 wake_up(&ses->server->request_q);
613 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
615 midQ->midState = MID_REQUEST_SUBMITTED;
616 rc = smb_send(ses->server->ssocket, in_buf, in_buf->smb_buf_length,
617 (struct sockaddr *) &(ses->server->addr.sockAddr));
619 DeleteMidQEntry(midQ);
620 up(&ses->server->tcpSem);
621 /* If not lock req, update # of requests on wire to server */
623 atomic_dec(&ses->server->inFlight);
624 wake_up(&ses->server->request_q);
628 up(&ses->server->tcpSem);
630 goto cifs_no_response_exit;
631 else if (long_op == 2) /* writes past end of file can take loong time */
633 else if (long_op == 1)
634 timeout = 45 * HZ; /* should be greater than
635 servers oplock break timeout (about 43 seconds) */
636 else if (long_op > 2) {
637 timeout = MAX_SCHEDULE_TIMEOUT;
640 /* wait for 15 seconds or until woken up due to response arriving or
641 due to last connection to this server being unmounted */
642 if (signal_pending(current)) {
643 /* if signal pending do not hold up user for full smb timeout
644 but we still give response a change to complete */
648 /* No user interrupts in wait - wreaks havoc with performance */
649 if(timeout != MAX_SCHEDULE_TIMEOUT) {
651 wait_event(ses->server->response_q,
652 (!(midQ->midState & MID_REQUEST_SUBMITTED)) ||
653 time_after(jiffies, timeout) ||
654 ((ses->server->tcpStatus != CifsGood) &&
655 (ses->server->tcpStatus != CifsNew)));
657 wait_event(ses->server->response_q,
658 (!(midQ->midState & MID_REQUEST_SUBMITTED)) ||
659 ((ses->server->tcpStatus != CifsGood) &&
660 (ses->server->tcpStatus != CifsNew)));
663 spin_lock(&GlobalMid_Lock);
664 if (midQ->resp_buf) {
665 spin_unlock(&GlobalMid_Lock);
666 receive_len = be32_to_cpu(*(__be32 *)midQ->resp_buf);
668 cERROR(1,("No response buffer"));
669 if(midQ->midState == MID_REQUEST_SUBMITTED) {
670 if(ses->server->tcpStatus == CifsExiting)
673 ses->server->tcpStatus = CifsNeedReconnect;
674 midQ->midState = MID_RETRY_NEEDED;
678 if (rc != -EHOSTDOWN) {
679 if(midQ->midState == MID_RETRY_NEEDED) {
681 cFYI(1,("marking request for retry"));
686 spin_unlock(&GlobalMid_Lock);
687 DeleteMidQEntry(midQ);
688 /* If not lock req, update # of requests on wire to server */
690 atomic_dec(&ses->server->inFlight);
691 wake_up(&ses->server->request_q);
696 if (receive_len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE) {
697 cERROR(1, ("Frame too large received. Length: %d Xid: %d",
700 } else { /* rcvd frame is ok */
702 if (midQ->resp_buf && out_buf
703 && (midQ->midState == MID_RESPONSE_RECEIVED)) {
704 out_buf->smb_buf_length = receive_len;
705 memcpy((char *)out_buf + 4,
706 (char *)midQ->resp_buf + 4,
709 dump_smb(out_buf, 92);
710 /* convert the length into a more usable form */
711 if((receive_len > 24) &&
712 (ses->server->secMode & (SECMODE_SIGN_REQUIRED |
713 SECMODE_SIGN_ENABLED))) {
714 rc = cifs_verify_signature(out_buf,
715 ses->server->mac_signing_key,
716 midQ->sequence_number+1);
718 cERROR(1,("Unexpected SMB signature"));
719 /* BB FIXME add code to kill session */
723 *pbytes_returned = out_buf->smb_buf_length;
725 /* BB special case reconnect tid and uid here? */
726 rc = map_smb_to_linux_error(out_buf);
728 /* convert ByteCount if necessary */
730 sizeof (struct smb_hdr) -
731 4 /* do not count RFC1001 header */ +
732 (2 * out_buf->WordCount) + 2 /* bcc */ )
733 BCC(out_buf) = le16_to_cpu(BCC(out_buf));
736 cFYI(1,("Bad MID state? "));
739 cifs_no_response_exit:
740 DeleteMidQEntry(midQ);
743 atomic_dec(&ses->server->inFlight);
744 wake_up(&ses->server->request_q);
750 up(&ses->server->tcpSem);
751 /* If not lock req, update # of requests on wire to server */
753 atomic_dec(&ses->server->inFlight);
754 wake_up(&ses->server->request_q);