4 * Copyright (C) International Business Machines Corp., 2002,2004
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,SLAB_KERNEL | SLAB_NOFS);
56 memset(temp, 0, sizeof (struct mid_q_entry));
57 temp->mid = smb_buffer->Mid; /* always LE */
58 temp->pid = current->pid;
59 temp->command = smb_buffer->Command;
60 cFYI(1, ("For smb_command %d", temp->command));
61 do_gettimeofday(&temp->when_sent);
66 spin_lock(&GlobalMid_Lock);
67 list_add_tail(&temp->qhead, &ses->server->pending_mid_q);
68 atomic_inc(&midCount);
69 temp->midState = MID_REQUEST_ALLOCATED;
70 spin_unlock(&GlobalMid_Lock);
75 DeleteMidQEntry(struct mid_q_entry *midEntry)
77 spin_lock(&GlobalMid_Lock);
78 midEntry->midState = MID_FREE;
79 list_del(&midEntry->qhead);
80 atomic_dec(&midCount);
81 spin_unlock(&GlobalMid_Lock);
82 cifs_buf_release(midEntry->resp_buf);
83 mempool_free(midEntry, cifs_mid_poolp);
86 struct oplock_q_entry *
87 AllocOplockQEntry(struct inode * pinode, __u16 fid, struct cifsTconInfo * tcon)
89 struct oplock_q_entry *temp;
90 if ((pinode== NULL) || (tcon == NULL)) {
91 cERROR(1, ("Null parms passed to AllocOplockQEntry"));
94 temp = (struct oplock_q_entry *) kmem_cache_alloc(cifs_oplock_cachep,
99 temp->pinode = pinode;
102 spin_lock(&GlobalMid_Lock);
103 list_add_tail(&temp->qhead, &GlobalOplock_Q);
104 spin_unlock(&GlobalMid_Lock);
110 void DeleteOplockQEntry(struct oplock_q_entry * oplockEntry)
112 spin_lock(&GlobalMid_Lock);
113 /* should we check if list empty first? */
114 list_del(&oplockEntry->qhead);
115 spin_unlock(&GlobalMid_Lock);
116 kmem_cache_free(cifs_oplock_cachep, oplockEntry);
120 smb_send(struct socket *ssocket, struct smb_hdr *smb_buffer,
121 unsigned int smb_buf_length, struct sockaddr *sin)
125 struct msghdr smb_msg;
127 unsigned len = smb_buf_length + 4;
130 return -ENOTSOCK; /* BB eventually add reconnect code here */
131 iov.iov_base = smb_buffer;
134 smb_msg.msg_name = sin;
135 smb_msg.msg_namelen = sizeof (struct sockaddr);
136 smb_msg.msg_control = NULL;
137 smb_msg.msg_controllen = 0;
138 smb_msg.msg_flags = MSG_DONTWAIT + MSG_NOSIGNAL; /* BB add more flags?*/
140 /* smb header is converted in header_assemble. bcc and rest of SMB word
141 area, and byte area if necessary, is converted to littleendian in
142 cifssmb.c and RFC1001 len is converted to bigendian in smb_send
143 Flags2 is converted in SendReceive */
145 smb_buffer->smb_buf_length = cpu_to_be32(smb_buffer->smb_buf_length);
146 cFYI(1, ("Sending smb of length %d ", smb_buf_length));
147 dump_smb(smb_buffer, len);
150 rc = kernel_sendmsg(ssocket, &smb_msg, &iov, 1, len);
151 if ((rc == -ENOSPC) || (rc == -EAGAIN)) {
155 ("sends on sock %p stuck for 30 seconds",
171 cERROR(1,("Error %d sending data on socket to server.", rc));
179 #ifdef CIFS_EXPERIMENTAL
180 /* BB finish off this function, adding support for writing set of pages as iovec */
181 /* and also adding support for operations that need to parse the response smb */
184 smb_sendv(struct socket *ssocket, struct smb_hdr *smb_buffer,
185 unsigned int smb_buf_length, struct kvec * write_vector /* page list */, struct sockaddr *sin)
189 struct msghdr smb_msg;
190 number_of_pages += 1; /* account for SMB header */
191 struct kvec * piov = kmalloc(number_of_pages * sizeof(struct kvec));
192 unsigned len = smb_buf_length + 4;
195 return -ENOTSOCK; /* BB eventually add reconnect code here */
196 iov.iov_base = smb_buffer;
199 smb_msg.msg_name = sin;
200 smb_msg.msg_namelen = sizeof (struct sockaddr);
201 smb_msg.msg_control = NULL;
202 smb_msg.msg_controllen = 0;
203 smb_msg.msg_flags = MSG_DONTWAIT + MSG_NOSIGNAL; /* BB add more flags?*/
205 /* smb header is converted in header_assemble. bcc and rest of SMB word
206 area, and byte area if necessary, is converted to littleendian in
207 cifssmb.c and RFC1001 len is converted to bigendian in smb_send
208 Flags2 is converted in SendReceive */
210 smb_buffer->smb_buf_length = cpu_to_be32(smb_buffer->smb_buf_length);
211 cFYI(1, ("Sending smb of length %d ", smb_buf_length));
212 dump_smb(smb_buffer, len);
215 rc = kernel_sendmsg(ssocket, &smb_msg, &iov, number_of_pages, len?);
216 if ((rc == -ENOSPC) || (rc == -EAGAIN)) {
220 ("sends on sock %p stuck for 30 seconds",
236 cERROR(1,("Error %d sending data on socket to server.", rc));
246 CIFSSendRcv(const unsigned int xid, struct cifsSesInfo *ses,
247 struct smb_hdr *in_buf, struct kvec * write_vector /* page list */, int *pbytes_returned, const int long_op)
250 unsigned long timeout = 15 * HZ;
251 struct mid_q_entry *midQ = NULL;
254 cERROR(1,("Null smb session"));
257 if(ses->server == NULL) {
258 cERROR(1,("Null tcp session"));
261 if(pbytes_returned == NULL)
264 *pbytes_returned = 0;
268 /* Ensure that we do not send more than 50 overlapping requests
269 to the same server. We may make this configurable later or
272 /* oplock breaks must not be held up */
273 atomic_inc(&ses->server->inFlight);
275 spin_lock(&GlobalMid_Lock);
277 if(atomic_read(&ses->server->inFlight) >= cifs_max_pending){
278 spin_unlock(&GlobalMid_Lock);
279 wait_event(ses->server->request_q,
280 atomic_read(&ses->server->inFlight)
282 spin_lock(&GlobalMid_Lock);
284 if(ses->server->tcpStatus == CifsExiting) {
285 spin_unlock(&GlobalMid_Lock);
289 /* can not count locking commands against total since
290 they are allowed to block on server */
293 /* update # of requests on the wire to server */
294 atomic_inc(&ses->server->inFlight);
296 spin_unlock(&GlobalMid_Lock);
301 /* make sure that we sign in the same order that we send on this socket
302 and avoid races inside tcp sendmsg code that could cause corruption
305 down(&ses->server->tcpSem);
307 if (ses->server->tcpStatus == CifsExiting) {
310 } else if (ses->server->tcpStatus == CifsNeedReconnect) {
311 cFYI(1,("tcp session dead - return to caller to retry"));
314 } else if (ses->status != CifsGood) {
315 /* check if SMB session is bad because we are setting it up */
316 if((in_buf->Command != SMB_COM_SESSION_SETUP_ANDX) &&
317 (in_buf->Command != SMB_COM_NEGOTIATE)) {
320 } /* else ok - we are setting up session */
322 midQ = AllocMidQEntry(in_buf, ses);
324 up(&ses->server->tcpSem);
325 /* If not lock req, update # of requests on wire to server */
327 atomic_dec(&ses->server->inFlight);
328 wake_up(&ses->server->request_q);
333 if (in_buf->smb_buf_length > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
334 up(&ses->server->tcpSem);
336 ("Illegal length, greater than maximum frame, %d ",
337 in_buf->smb_buf_length));
338 DeleteMidQEntry(midQ);
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 /* BB can we sign efficiently in this path? */
348 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
350 midQ->midState = MID_REQUEST_SUBMITTED;
351 /* rc = smb_sendv(ses->server->ssocket, in_buf, in_buf->smb_buf_length, piovec,
352 (struct sockaddr *) &(ses->server->addr.sockAddr));*/
354 DeleteMidQEntry(midQ);
355 up(&ses->server->tcpSem);
356 /* If not lock req, update # of requests on wire to server */
358 atomic_dec(&ses->server->inFlight);
359 wake_up(&ses->server->request_q);
363 up(&ses->server->tcpSem);
366 DeleteMidQEntry(midQ);
369 atomic_dec(&ses->server->inFlight);
370 wake_up(&ses->server->request_q);
377 #endif /* CIFS_EXPERIMENTAL */
380 SendReceive(const unsigned int xid, struct cifsSesInfo *ses,
381 struct smb_hdr *in_buf, struct smb_hdr *out_buf,
382 int *pbytes_returned, const int long_op)
385 unsigned int receive_len;
386 unsigned long timeout;
387 struct mid_q_entry *midQ;
390 cERROR(1,("Null smb session"));
393 if(ses->server == NULL) {
394 cERROR(1,("Null tcp session"));
398 /* Ensure that we do not send more than 50 overlapping requests
399 to the same server. We may make this configurable later or
402 /* oplock breaks must not be held up */
403 atomic_inc(&ses->server->inFlight);
405 spin_lock(&GlobalMid_Lock);
407 if(atomic_read(&ses->server->inFlight) >= cifs_max_pending){
408 spin_unlock(&GlobalMid_Lock);
409 wait_event(ses->server->request_q,
410 atomic_read(&ses->server->inFlight)
412 spin_lock(&GlobalMid_Lock);
414 if(ses->server->tcpStatus == CifsExiting) {
415 spin_unlock(&GlobalMid_Lock);
419 /* can not count locking commands against total since
420 they are allowed to block on server */
423 /* update # of requests on the wire to server */
424 atomic_inc(&ses->server->inFlight);
426 spin_unlock(&GlobalMid_Lock);
431 /* make sure that we sign in the same order that we send on this socket
432 and avoid races inside tcp sendmsg code that could cause corruption
435 down(&ses->server->tcpSem);
437 if (ses->server->tcpStatus == CifsExiting) {
440 } else if (ses->server->tcpStatus == CifsNeedReconnect) {
441 cFYI(1,("tcp session dead - return to caller to retry"));
444 } else if (ses->status != CifsGood) {
445 /* check if SMB session is bad because we are setting it up */
446 if((in_buf->Command != SMB_COM_SESSION_SETUP_ANDX) &&
447 (in_buf->Command != SMB_COM_NEGOTIATE)) {
450 } /* else ok - we are setting up session */
452 midQ = AllocMidQEntry(in_buf, ses);
454 up(&ses->server->tcpSem);
455 /* If not lock req, update # of requests on wire to server */
457 atomic_dec(&ses->server->inFlight);
458 wake_up(&ses->server->request_q);
463 if (in_buf->smb_buf_length > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
464 up(&ses->server->tcpSem);
466 ("Illegal length, greater than maximum frame, %d ",
467 in_buf->smb_buf_length));
468 DeleteMidQEntry(midQ);
469 /* If not lock req, update # of requests on wire to server */
471 atomic_dec(&ses->server->inFlight);
472 wake_up(&ses->server->request_q);
477 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
479 midQ->midState = MID_REQUEST_SUBMITTED;
480 rc = smb_send(ses->server->ssocket, in_buf, in_buf->smb_buf_length,
481 (struct sockaddr *) &(ses->server->addr.sockAddr));
483 DeleteMidQEntry(midQ);
484 up(&ses->server->tcpSem);
485 /* If not lock req, update # of requests on wire to server */
487 atomic_dec(&ses->server->inFlight);
488 wake_up(&ses->server->request_q);
492 up(&ses->server->tcpSem);
494 goto cifs_no_response_exit;
495 else if (long_op == 2) /* writes past end of file can take looooong time */
497 else if (long_op == 1)
498 timeout = 45 * HZ; /* should be greater than
499 servers oplock break timeout (about 43 seconds) */
500 else if (long_op > 2) {
501 timeout = MAX_SCHEDULE_TIMEOUT;
504 /* wait for 15 seconds or until woken up due to response arriving or
505 due to last connection to this server being unmounted */
506 if (signal_pending(current)) {
507 /* if signal pending do not hold up user for full smb timeout
508 but we still give response a change to complete */
512 /* No user interrupts in wait - wreaks havoc with performance */
513 if(timeout != MAX_SCHEDULE_TIMEOUT) {
515 wait_event(ses->server->response_q,
516 (!(midQ->midState & MID_REQUEST_SUBMITTED)) ||
517 time_after(jiffies, timeout) ||
518 ((ses->server->tcpStatus != CifsGood) &&
519 (ses->server->tcpStatus != CifsNew)));
521 wait_event(ses->server->response_q,
522 (!(midQ->midState & MID_REQUEST_SUBMITTED)) ||
523 ((ses->server->tcpStatus != CifsGood) &&
524 (ses->server->tcpStatus != CifsNew)));
527 spin_lock(&GlobalMid_Lock);
528 if (midQ->resp_buf) {
529 spin_unlock(&GlobalMid_Lock);
530 receive_len = be32_to_cpu(*(__be32 *)midQ->resp_buf);
532 cERROR(1,("No response buffer"));
533 if(midQ->midState == MID_REQUEST_SUBMITTED) {
534 if(ses->server->tcpStatus == CifsExiting)
537 ses->server->tcpStatus = CifsNeedReconnect;
538 midQ->midState = MID_RETRY_NEEDED;
542 if (rc != -EHOSTDOWN) {
543 if(midQ->midState == MID_RETRY_NEEDED) {
545 cFYI(1,("marking request for retry"));
550 spin_unlock(&GlobalMid_Lock);
551 DeleteMidQEntry(midQ);
552 /* If not lock req, update # of requests on wire to server */
554 atomic_dec(&ses->server->inFlight);
555 wake_up(&ses->server->request_q);
560 if (receive_len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE) {
561 cERROR(1, ("Frame too large received. Length: %d Xid: %d",
564 } else { /* rcvd frame is ok */
566 if (midQ->resp_buf && out_buf
567 && (midQ->midState == MID_RESPONSE_RECEIVED)) {
568 out_buf->smb_buf_length = receive_len;
569 memcpy((char *)out_buf + 4,
570 (char *)midQ->resp_buf + 4,
573 dump_smb(out_buf, 92);
574 /* convert the length into a more usable form */
575 if((receive_len > 24) &&
576 (ses->server->secMode & (SECMODE_SIGN_REQUIRED |
577 SECMODE_SIGN_ENABLED))) {
578 rc = cifs_verify_signature(out_buf,
579 ses->server->mac_signing_key,
580 midQ->sequence_number+1);
582 cERROR(1,("Unexpected packet signature received from server"));
583 /* BB FIXME - add code to kill session here */
587 *pbytes_returned = out_buf->smb_buf_length;
589 /* BB special case reconnect tid and uid here? */
590 rc = map_smb_to_linux_error(out_buf);
592 /* convert ByteCount if necessary */
594 sizeof (struct smb_hdr) -
595 4 /* do not count RFC1001 header */ +
596 (2 * out_buf->WordCount) + 2 /* bcc */ )
597 BCC(out_buf) = le16_to_cpu(BCC(out_buf));
600 cFYI(1,("Bad MID state? "));
603 cifs_no_response_exit:
604 DeleteMidQEntry(midQ);
607 atomic_dec(&ses->server->inFlight);
608 wake_up(&ses->server->request_q);
614 up(&ses->server->tcpSem);
615 /* If not lock req, update # of requests on wire to server */
617 atomic_dec(&ses->server->inFlight);
618 wake_up(&ses->server->request_q);