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[1].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: hdrlen %d datalen %d",
214 smb_hdr_length,datalen));
215 dump_smb(smb_buffer, len);
217 while (len + datalen > 0) {
218 rc = kernel_sendmsg(ssocket, &smb_msg, iov, 2, len);
219 if ((rc == -ENOSPC) || (rc == -EAGAIN)) {
223 ("sends on sock %p stuck for 30 seconds",
233 if(iov[0].iov_len > 0) {
238 } else { /* some of hdr was not sent */
240 iov[0].iov_len -= rc;
241 iov[0].iov_base += rc;
245 if((iov[0].iov_len == 0) && (rc > 0)){
246 iov[1].iov_base += rc;
247 iov[1].iov_len -= rc;
253 cERROR(1,("Error %d sending data on socket to server.", rc));
262 SendReceive2(const unsigned int xid, struct cifsSesInfo *ses,
263 struct smb_hdr *in_buf, int hdrlen, const char * data,
264 int datalen, int *pbytes_returned, const int long_op)
267 unsigned int receive_len;
268 unsigned long timeout;
269 struct mid_q_entry *midQ;
272 cERROR(1,("Null smb session"));
275 if(ses->server == NULL) {
276 cERROR(1,("Null tcp session"));
280 if(ses->server->tcpStatus == CifsExiting)
283 /* Ensure that we do not send more than 50 overlapping requests
284 to the same server. We may make this configurable later or
287 /* oplock breaks must not be held up */
288 atomic_inc(&ses->server->inFlight);
290 spin_lock(&GlobalMid_Lock);
292 if(atomic_read(&ses->server->inFlight) >=
294 spin_unlock(&GlobalMid_Lock);
295 wait_event(ses->server->request_q,
296 atomic_read(&ses->server->inFlight)
298 spin_lock(&GlobalMid_Lock);
300 if(ses->server->tcpStatus == CifsExiting) {
301 spin_unlock(&GlobalMid_Lock);
305 /* can not count locking commands against total since
306 they are allowed to block on server */
309 /* update # of requests on the wire to server */
310 atomic_inc(&ses->server->inFlight);
312 spin_unlock(&GlobalMid_Lock);
317 /* make sure that we sign in the same order that we send on this socket
318 and avoid races inside tcp sendmsg code that could cause corruption
321 down(&ses->server->tcpSem);
323 if (ses->server->tcpStatus == CifsExiting) {
326 } else if (ses->server->tcpStatus == CifsNeedReconnect) {
327 cFYI(1,("tcp session dead - return to caller to retry"));
330 } else if (ses->status != CifsGood) {
331 /* check if SMB session is bad because we are setting it up */
332 if((in_buf->Command != SMB_COM_SESSION_SETUP_ANDX) &&
333 (in_buf->Command != SMB_COM_NEGOTIATE)) {
336 } /* else ok - we are setting up session */
338 midQ = AllocMidQEntry(in_buf, ses);
340 up(&ses->server->tcpSem);
341 /* If not lock req, update # of requests on wire to server */
343 atomic_dec(&ses->server->inFlight);
344 wake_up(&ses->server->request_q);
349 if (in_buf->smb_buf_length > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
350 up(&ses->server->tcpSem);
352 ("Illegal length, greater than maximum frame, %d ",
353 in_buf->smb_buf_length));
354 DeleteMidQEntry(midQ);
355 /* If not lock req, update # of requests on wire to server */
357 atomic_dec(&ses->server->inFlight);
358 wake_up(&ses->server->request_q);
364 /* rc = cifs_sign_smb2(in_buf, data, ses->server, &midQ->sequence_number); */
366 midQ->midState = MID_REQUEST_SUBMITTED;
367 rc = smb_send2(ses->server->ssocket, in_buf, hdrlen, data, datalen,
368 (struct sockaddr *) &(ses->server->addr.sockAddr));
370 DeleteMidQEntry(midQ);
371 up(&ses->server->tcpSem);
372 /* If not lock req, update # of requests on wire to server */
374 atomic_dec(&ses->server->inFlight);
375 wake_up(&ses->server->request_q);
379 up(&ses->server->tcpSem);
381 goto cifs_no_response_exit2;
382 else if (long_op == 2) /* writes past end of file can take loong time */
384 else if (long_op == 1)
385 timeout = 45 * HZ; /* should be greater than
386 servers oplock break timeout (about 43 seconds) */
387 else if (long_op > 2) {
388 timeout = MAX_SCHEDULE_TIMEOUT;
391 /* wait for 15 seconds or until woken up due to response arriving or
392 due to last connection to this server being unmounted */
393 if (signal_pending(current)) {
394 /* if signal pending do not hold up user for full smb timeout
395 but we still give response a change to complete */
399 /* No user interrupts in wait - wreaks havoc with performance */
400 if(timeout != MAX_SCHEDULE_TIMEOUT) {
402 wait_event(ses->server->response_q,
403 (!(midQ->midState & MID_REQUEST_SUBMITTED)) ||
404 time_after(jiffies, timeout) ||
405 ((ses->server->tcpStatus != CifsGood) &&
406 (ses->server->tcpStatus != CifsNew)));
408 wait_event(ses->server->response_q,
409 (!(midQ->midState & MID_REQUEST_SUBMITTED)) ||
410 ((ses->server->tcpStatus != CifsGood) &&
411 (ses->server->tcpStatus != CifsNew)));
414 spin_lock(&GlobalMid_Lock);
415 if (midQ->resp_buf) {
416 spin_unlock(&GlobalMid_Lock);
417 receive_len = be32_to_cpu(*(__be32 *)midQ->resp_buf);
419 cERROR(1,("No response buffer"));
420 if(midQ->midState == MID_REQUEST_SUBMITTED) {
421 if(ses->server->tcpStatus == CifsExiting)
424 ses->server->tcpStatus = CifsNeedReconnect;
425 midQ->midState = MID_RETRY_NEEDED;
429 if (rc != -EHOSTDOWN) {
430 if(midQ->midState == MID_RETRY_NEEDED) {
432 cFYI(1,("marking request for retry"));
437 spin_unlock(&GlobalMid_Lock);
438 DeleteMidQEntry(midQ);
439 /* If not lock req, update # of requests on wire to server */
441 atomic_dec(&ses->server->inFlight);
442 wake_up(&ses->server->request_q);
447 if (receive_len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE) {
448 cERROR(1, ("Frame too large received. Length: %d Xid: %d",
451 } else { /* rcvd frame is ok */
453 if (midQ->resp_buf &&
454 (midQ->midState == MID_RESPONSE_RECEIVED)) {
455 in_buf->smb_buf_length = receive_len;
456 /* BB verify that length would not overrun small buf */
457 memcpy((char *)in_buf + 4,
458 (char *)midQ->resp_buf + 4,
461 dump_smb(in_buf, 80);
462 /* convert the length into a more usable form */
463 if((receive_len > 24) &&
464 (ses->server->secMode & (SECMODE_SIGN_REQUIRED |
465 SECMODE_SIGN_ENABLED))) {
466 rc = cifs_verify_signature(in_buf,
467 ses->server->mac_signing_key,
468 midQ->sequence_number+1);
470 cERROR(1,("Unexpected SMB signature"));
471 /* BB FIXME add code to kill session */
475 *pbytes_returned = in_buf->smb_buf_length;
477 /* BB special case reconnect tid and uid here? */
478 rc = map_smb_to_linux_error(in_buf);
480 /* convert ByteCount if necessary */
482 sizeof (struct smb_hdr) -
483 4 /* do not count RFC1001 header */ +
484 (2 * in_buf->WordCount) + 2 /* bcc */ )
485 BCC(in_buf) = le16_to_cpu(BCC(in_buf));
488 cFYI(1,("Bad MID state? "));
491 cifs_no_response_exit2:
492 DeleteMidQEntry(midQ);
495 atomic_dec(&ses->server->inFlight);
496 wake_up(&ses->server->request_q);
502 up(&ses->server->tcpSem);
503 /* If not lock req, update # of requests on wire to server */
505 atomic_dec(&ses->server->inFlight);
506 wake_up(&ses->server->request_q);
511 #endif /* CIFS_EXPERIMENTAL */
514 SendReceive(const unsigned int xid, struct cifsSesInfo *ses,
515 struct smb_hdr *in_buf, struct smb_hdr *out_buf,
516 int *pbytes_returned, const int long_op)
519 unsigned int receive_len;
520 unsigned long timeout;
521 struct mid_q_entry *midQ;
524 cERROR(1,("Null smb session"));
527 if(ses->server == NULL) {
528 cERROR(1,("Null tcp session"));
532 if(ses->server->tcpStatus == CifsExiting)
535 /* Ensure that we do not send more than 50 overlapping requests
536 to the same server. We may make this configurable later or
539 /* oplock breaks must not be held up */
540 atomic_inc(&ses->server->inFlight);
542 spin_lock(&GlobalMid_Lock);
544 if(atomic_read(&ses->server->inFlight) >=
546 spin_unlock(&GlobalMid_Lock);
547 wait_event(ses->server->request_q,
548 atomic_read(&ses->server->inFlight)
550 spin_lock(&GlobalMid_Lock);
552 if(ses->server->tcpStatus == CifsExiting) {
553 spin_unlock(&GlobalMid_Lock);
557 /* can not count locking commands against total since
558 they are allowed to block on server */
561 /* update # of requests on the wire to server */
562 atomic_inc(&ses->server->inFlight);
564 spin_unlock(&GlobalMid_Lock);
569 /* make sure that we sign in the same order that we send on this socket
570 and avoid races inside tcp sendmsg code that could cause corruption
573 down(&ses->server->tcpSem);
575 if (ses->server->tcpStatus == CifsExiting) {
578 } else if (ses->server->tcpStatus == CifsNeedReconnect) {
579 cFYI(1,("tcp session dead - return to caller to retry"));
582 } else if (ses->status != CifsGood) {
583 /* check if SMB session is bad because we are setting it up */
584 if((in_buf->Command != SMB_COM_SESSION_SETUP_ANDX) &&
585 (in_buf->Command != SMB_COM_NEGOTIATE)) {
588 } /* else ok - we are setting up session */
590 midQ = AllocMidQEntry(in_buf, ses);
592 up(&ses->server->tcpSem);
593 /* If not lock req, update # of requests on wire to server */
595 atomic_dec(&ses->server->inFlight);
596 wake_up(&ses->server->request_q);
601 if (in_buf->smb_buf_length > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
602 up(&ses->server->tcpSem);
604 ("Illegal length, greater than maximum frame, %d ",
605 in_buf->smb_buf_length));
606 DeleteMidQEntry(midQ);
607 /* If not lock req, update # of requests on wire to server */
609 atomic_dec(&ses->server->inFlight);
610 wake_up(&ses->server->request_q);
615 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
617 midQ->midState = MID_REQUEST_SUBMITTED;
618 rc = smb_send(ses->server->ssocket, in_buf, in_buf->smb_buf_length,
619 (struct sockaddr *) &(ses->server->addr.sockAddr));
621 DeleteMidQEntry(midQ);
622 up(&ses->server->tcpSem);
623 /* If not lock req, update # of requests on wire to server */
625 atomic_dec(&ses->server->inFlight);
626 wake_up(&ses->server->request_q);
630 up(&ses->server->tcpSem);
632 goto cifs_no_response_exit;
633 else if (long_op == 2) /* writes past end of file can take loong time */
635 else if (long_op == 1)
636 timeout = 45 * HZ; /* should be greater than
637 servers oplock break timeout (about 43 seconds) */
638 else if (long_op > 2) {
639 timeout = MAX_SCHEDULE_TIMEOUT;
642 /* wait for 15 seconds or until woken up due to response arriving or
643 due to last connection to this server being unmounted */
644 if (signal_pending(current)) {
645 /* if signal pending do not hold up user for full smb timeout
646 but we still give response a change to complete */
650 /* No user interrupts in wait - wreaks havoc with performance */
651 if(timeout != MAX_SCHEDULE_TIMEOUT) {
653 wait_event(ses->server->response_q,
654 (!(midQ->midState & MID_REQUEST_SUBMITTED)) ||
655 time_after(jiffies, timeout) ||
656 ((ses->server->tcpStatus != CifsGood) &&
657 (ses->server->tcpStatus != CifsNew)));
659 wait_event(ses->server->response_q,
660 (!(midQ->midState & MID_REQUEST_SUBMITTED)) ||
661 ((ses->server->tcpStatus != CifsGood) &&
662 (ses->server->tcpStatus != CifsNew)));
665 spin_lock(&GlobalMid_Lock);
666 if (midQ->resp_buf) {
667 spin_unlock(&GlobalMid_Lock);
668 receive_len = be32_to_cpu(*(__be32 *)midQ->resp_buf);
670 cERROR(1,("No response buffer"));
671 if(midQ->midState == MID_REQUEST_SUBMITTED) {
672 if(ses->server->tcpStatus == CifsExiting)
675 ses->server->tcpStatus = CifsNeedReconnect;
676 midQ->midState = MID_RETRY_NEEDED;
680 if (rc != -EHOSTDOWN) {
681 if(midQ->midState == MID_RETRY_NEEDED) {
683 cFYI(1,("marking request for retry"));
688 spin_unlock(&GlobalMid_Lock);
689 DeleteMidQEntry(midQ);
690 /* If not lock req, update # of requests on wire to server */
692 atomic_dec(&ses->server->inFlight);
693 wake_up(&ses->server->request_q);
698 if (receive_len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE) {
699 cERROR(1, ("Frame too large received. Length: %d Xid: %d",
702 } else { /* rcvd frame is ok */
704 if (midQ->resp_buf && out_buf
705 && (midQ->midState == MID_RESPONSE_RECEIVED)) {
706 out_buf->smb_buf_length = receive_len;
707 memcpy((char *)out_buf + 4,
708 (char *)midQ->resp_buf + 4,
711 dump_smb(out_buf, 92);
712 /* convert the length into a more usable form */
713 if((receive_len > 24) &&
714 (ses->server->secMode & (SECMODE_SIGN_REQUIRED |
715 SECMODE_SIGN_ENABLED))) {
716 rc = cifs_verify_signature(out_buf,
717 ses->server->mac_signing_key,
718 midQ->sequence_number+1);
720 cERROR(1,("Unexpected SMB signature"));
721 /* BB FIXME add code to kill session */
725 *pbytes_returned = out_buf->smb_buf_length;
727 /* BB special case reconnect tid and uid here? */
728 rc = map_smb_to_linux_error(out_buf);
730 /* convert ByteCount if necessary */
732 sizeof (struct smb_hdr) -
733 4 /* do not count RFC1001 header */ +
734 (2 * out_buf->WordCount) + 2 /* bcc */ )
735 BCC(out_buf) = le16_to_cpu(BCC(out_buf));
738 cFYI(1,("Bad MID state? "));
741 cifs_no_response_exit:
742 DeleteMidQEntry(midQ);
745 atomic_dec(&ses->server->inFlight);
746 wake_up(&ses->server->request_q);
752 up(&ses->server->tcpSem);
753 /* If not lock req, update # of requests on wire to server */
755 atomic_dec(&ses->server->inFlight);
756 wake_up(&ses->server->request_q);