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 if(i=0;i<num_pages-1;i++
193 unsigned len = smb_buf_length + 4;
196 return -ENOTSOCK; /* BB eventually add reconnect code here */
197 iov.iov_base = smb_buffer;
200 smb_msg.msg_name = sin;
201 smb_msg.msg_namelen = sizeof (struct sockaddr);
202 smb_msg.msg_control = NULL;
203 smb_msg.msg_controllen = 0;
204 smb_msg.msg_flags = MSG_DONTWAIT + MSG_NOSIGNAL; /* BB add more flags?*/
206 /* smb header is converted in header_assemble. bcc and rest of SMB word
207 area, and byte area if necessary, is converted to littleendian in
208 cifssmb.c and RFC1001 len is converted to bigendian in smb_send
209 Flags2 is converted in SendReceive */
211 smb_buffer->smb_buf_length = cpu_to_be32(smb_buffer->smb_buf_length);
212 cFYI(1, ("Sending smb of length %d ", smb_buf_length));
213 dump_smb(smb_buffer, len);
216 rc = kernel_sendmsg(ssocket, &smb_msg, &iov, number_of_pages, len?);
217 if ((rc == -ENOSPC) || (rc == -EAGAIN)) {
221 ("sends on sock %p stuck for 30 seconds",
237 cERROR(1,("Error %d sending data on socket to server.", rc));
247 CIFSSendRcv(const unsigned int xid, struct cifsSesInfo *ses,
248 struct smb_hdr *in_buf, struct kvec * write_vector /* page list */, int *pbytes_returned, const int long_op)
251 unsigned long timeout = 15 * HZ;
252 struct mid_q_entry *midQ = NULL;
255 cERROR(1,("Null smb session"));
258 if(ses->server == NULL) {
259 cERROR(1,("Null tcp session"));
262 if(pbytes_returned == NULL)
265 *pbytes_returned = 0;
269 /* Ensure that we do not send more than 50 overlapping requests
270 to the same server. We may make this configurable later or
273 /* oplock breaks must not be held up */
274 atomic_inc(&ses->server->inFlight);
276 spin_lock(&GlobalMid_Lock);
278 if(atomic_read(&ses->server->inFlight) >= cifs_max_pending){
279 spin_unlock(&GlobalMid_Lock);
280 wait_event(ses->server->request_q,
281 atomic_read(&ses->server->inFlight)
283 spin_lock(&GlobalMid_Lock);
285 if(ses->server->tcpStatus == CifsExiting) {
286 spin_unlock(&GlobalMid_Lock);
290 /* can not count locking commands against total since
291 they are allowed to block on server */
294 /* update # of requests on the wire to server */
295 atomic_inc(&ses->server->inFlight);
297 spin_unlock(&GlobalMid_Lock);
302 /* make sure that we sign in the same order that we send on this socket
303 and avoid races inside tcp sendmsg code that could cause corruption
306 down(&ses->server->tcpSem);
308 if (ses->server->tcpStatus == CifsExiting) {
311 } else if (ses->server->tcpStatus == CifsNeedReconnect) {
312 cFYI(1,("tcp session dead - return to caller to retry"));
315 } else if (ses->status != CifsGood) {
316 /* check if SMB session is bad because we are setting it up */
317 if((in_buf->Command != SMB_COM_SESSION_SETUP_ANDX) &&
318 (in_buf->Command != SMB_COM_NEGOTIATE)) {
321 } /* else ok - we are setting up session */
323 midQ = AllocMidQEntry(in_buf, ses);
325 up(&ses->server->tcpSem);
326 /* If not lock req, update # of requests on wire to server */
328 atomic_dec(&ses->server->inFlight);
329 wake_up(&ses->server->request_q);
334 if (in_buf->smb_buf_length > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
335 up(&ses->server->tcpSem);
337 ("Illegal length, greater than maximum frame, %d ",
338 in_buf->smb_buf_length));
339 DeleteMidQEntry(midQ);
340 /* If not lock req, update # of requests on wire to server */
342 atomic_dec(&ses->server->inFlight);
343 wake_up(&ses->server->request_q);
348 /* BB can we sign efficiently in this path? */
349 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
351 midQ->midState = MID_REQUEST_SUBMITTED;
352 /* rc = smb_sendv(ses->server->ssocket, in_buf, in_buf->smb_buf_length, piovec,
353 (struct sockaddr *) &(ses->server->addr.sockAddr));*/
355 DeleteMidQEntry(midQ);
356 up(&ses->server->tcpSem);
357 /* If not lock req, update # of requests on wire to server */
359 atomic_dec(&ses->server->inFlight);
360 wake_up(&ses->server->request_q);
364 up(&ses->server->tcpSem);
367 DeleteMidQEntry(midQ);
370 atomic_dec(&ses->server->inFlight);
371 wake_up(&ses->server->request_q);
378 #endif /* CIFS_EXPERIMENTAL */
381 SendReceive(const unsigned int xid, struct cifsSesInfo *ses,
382 struct smb_hdr *in_buf, struct smb_hdr *out_buf,
383 int *pbytes_returned, const int long_op)
386 unsigned int receive_len;
387 unsigned long timeout;
388 struct mid_q_entry *midQ;
391 cERROR(1,("Null smb session"));
394 if(ses->server == NULL) {
395 cERROR(1,("Null tcp session"));
399 /* Ensure that we do not send more than 50 overlapping requests
400 to the same server. We may make this configurable later or
403 /* oplock breaks must not be held up */
404 atomic_inc(&ses->server->inFlight);
406 spin_lock(&GlobalMid_Lock);
408 if(atomic_read(&ses->server->inFlight) >= cifs_max_pending){
409 spin_unlock(&GlobalMid_Lock);
410 wait_event(ses->server->request_q,
411 atomic_read(&ses->server->inFlight)
413 spin_lock(&GlobalMid_Lock);
415 if(ses->server->tcpStatus == CifsExiting) {
416 spin_unlock(&GlobalMid_Lock);
420 /* can not count locking commands against total since
421 they are allowed to block on server */
424 /* update # of requests on the wire to server */
425 atomic_inc(&ses->server->inFlight);
427 spin_unlock(&GlobalMid_Lock);
432 /* make sure that we sign in the same order that we send on this socket
433 and avoid races inside tcp sendmsg code that could cause corruption
436 down(&ses->server->tcpSem);
438 if (ses->server->tcpStatus == CifsExiting) {
441 } else if (ses->server->tcpStatus == CifsNeedReconnect) {
442 cFYI(1,("tcp session dead - return to caller to retry"));
445 } else if (ses->status != CifsGood) {
446 /* check if SMB session is bad because we are setting it up */
447 if((in_buf->Command != SMB_COM_SESSION_SETUP_ANDX) &&
448 (in_buf->Command != SMB_COM_NEGOTIATE)) {
451 } /* else ok - we are setting up session */
453 midQ = AllocMidQEntry(in_buf, ses);
455 up(&ses->server->tcpSem);
456 /* If not lock req, update # of requests on wire to server */
458 atomic_dec(&ses->server->inFlight);
459 wake_up(&ses->server->request_q);
464 if (in_buf->smb_buf_length > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
465 up(&ses->server->tcpSem);
467 ("Illegal length, greater than maximum frame, %d ",
468 in_buf->smb_buf_length));
469 DeleteMidQEntry(midQ);
470 /* If not lock req, update # of requests on wire to server */
472 atomic_dec(&ses->server->inFlight);
473 wake_up(&ses->server->request_q);
478 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
480 midQ->midState = MID_REQUEST_SUBMITTED;
481 rc = smb_send(ses->server->ssocket, in_buf, in_buf->smb_buf_length,
482 (struct sockaddr *) &(ses->server->addr.sockAddr));
484 DeleteMidQEntry(midQ);
485 up(&ses->server->tcpSem);
486 /* If not lock req, update # of requests on wire to server */
488 atomic_dec(&ses->server->inFlight);
489 wake_up(&ses->server->request_q);
493 up(&ses->server->tcpSem);
495 goto cifs_no_response_exit;
496 else if (long_op == 2) /* writes past end of file can take looooong time */
498 else if (long_op == 1)
499 timeout = 45 * HZ; /* should be greater than
500 servers oplock break timeout (about 43 seconds) */
501 else if (long_op > 2) {
502 timeout = MAX_SCHEDULE_TIMEOUT;
505 /* wait for 15 seconds or until woken up due to response arriving or
506 due to last connection to this server being unmounted */
507 if (signal_pending(current)) {
508 /* if signal pending do not hold up user for full smb timeout
509 but we still give response a change to complete */
513 /* No user interrupts in wait - wreaks havoc with performance */
514 if(timeout != MAX_SCHEDULE_TIMEOUT) {
516 wait_event(ses->server->response_q,
517 (!(midQ->midState & MID_REQUEST_SUBMITTED)) ||
518 time_after(jiffies, timeout) ||
519 ((ses->server->tcpStatus != CifsGood) &&
520 (ses->server->tcpStatus != CifsNew)));
522 wait_event(ses->server->response_q,
523 (!(midQ->midState & MID_REQUEST_SUBMITTED)) ||
524 ((ses->server->tcpStatus != CifsGood) &&
525 (ses->server->tcpStatus != CifsNew)));
528 spin_lock(&GlobalMid_Lock);
529 if (midQ->resp_buf) {
530 spin_unlock(&GlobalMid_Lock);
531 receive_len = be32_to_cpu(*(__be32 *)midQ->resp_buf);
533 cERROR(1,("No response buffer"));
534 if(midQ->midState == MID_REQUEST_SUBMITTED) {
535 if(ses->server->tcpStatus == CifsExiting)
538 ses->server->tcpStatus = CifsNeedReconnect;
539 midQ->midState = MID_RETRY_NEEDED;
543 if (rc != -EHOSTDOWN) {
544 if(midQ->midState == MID_RETRY_NEEDED) {
546 cFYI(1,("marking request for retry"));
551 spin_unlock(&GlobalMid_Lock);
552 DeleteMidQEntry(midQ);
553 /* If not lock req, update # of requests on wire to server */
555 atomic_dec(&ses->server->inFlight);
556 wake_up(&ses->server->request_q);
561 if (receive_len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE) {
562 cERROR(1, ("Frame too large received. Length: %d Xid: %d",
565 } else { /* rcvd frame is ok */
567 if (midQ->resp_buf && out_buf
568 && (midQ->midState == MID_RESPONSE_RECEIVED)) {
569 out_buf->smb_buf_length = receive_len;
570 memcpy((char *)out_buf + 4,
571 (char *)midQ->resp_buf + 4,
574 dump_smb(out_buf, 92);
575 /* convert the length into a more usable form */
576 if((receive_len > 24) &&
577 (ses->server->secMode & (SECMODE_SIGN_REQUIRED |
578 SECMODE_SIGN_ENABLED))) {
579 rc = cifs_verify_signature(out_buf,
580 ses->server->mac_signing_key,
581 midQ->sequence_number+1);
583 cERROR(1,("Unexpected packet signature received from server"));
584 /* BB FIXME - add code to kill session here */
588 *pbytes_returned = out_buf->smb_buf_length;
590 /* BB special case reconnect tid and uid here? */
591 rc = map_smb_to_linux_error(out_buf);
593 /* convert ByteCount if necessary */
595 sizeof (struct smb_hdr) -
596 4 /* do not count RFC1001 header */ +
597 (2 * out_buf->WordCount) + 2 /* bcc */ )
598 BCC(out_buf) = le16_to_cpu(BCC(out_buf));
601 cFYI(1,("Bad MID state? "));
604 cifs_no_response_exit:
605 DeleteMidQEntry(midQ);
608 atomic_dec(&ses->server->inFlight);
609 wake_up(&ses->server->request_q);
615 up(&ses->server->tcpSem);
616 /* If not lock req, update # of requests on wire to server */
618 atomic_dec(&ses->server->inFlight);
619 wake_up(&ses->server->request_q);