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,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 if(midEntry->largeBuf)
83 cifs_buf_release(midEntry->resp_buf);
85 cifs_small_buf_release(midEntry->resp_buf);
86 mempool_free(midEntry, cifs_mid_poolp);
89 struct oplock_q_entry *
90 AllocOplockQEntry(struct inode * pinode, __u16 fid, struct cifsTconInfo * tcon)
92 struct oplock_q_entry *temp;
93 if ((pinode== NULL) || (tcon == NULL)) {
94 cERROR(1, ("Null parms passed to AllocOplockQEntry"));
97 temp = (struct oplock_q_entry *) kmem_cache_alloc(cifs_oplock_cachep,
102 temp->pinode = pinode;
105 spin_lock(&GlobalMid_Lock);
106 list_add_tail(&temp->qhead, &GlobalOplock_Q);
107 spin_unlock(&GlobalMid_Lock);
113 void DeleteOplockQEntry(struct oplock_q_entry * oplockEntry)
115 spin_lock(&GlobalMid_Lock);
116 /* should we check if list empty first? */
117 list_del(&oplockEntry->qhead);
118 spin_unlock(&GlobalMid_Lock);
119 kmem_cache_free(cifs_oplock_cachep, oplockEntry);
123 smb_send(struct socket *ssocket, struct smb_hdr *smb_buffer,
124 unsigned int smb_buf_length, struct sockaddr *sin)
128 struct msghdr smb_msg;
130 unsigned len = smb_buf_length + 4;
133 return -ENOTSOCK; /* BB eventually add reconnect code here */
134 iov.iov_base = smb_buffer;
137 smb_msg.msg_name = sin;
138 smb_msg.msg_namelen = sizeof (struct sockaddr);
139 smb_msg.msg_control = NULL;
140 smb_msg.msg_controllen = 0;
141 smb_msg.msg_flags = MSG_DONTWAIT + MSG_NOSIGNAL; /* BB add more flags?*/
143 /* smb header is converted in header_assemble. bcc and rest of SMB word
144 area, and byte area if necessary, is converted to littleendian in
145 cifssmb.c and RFC1001 len is converted to bigendian in smb_send
146 Flags2 is converted in SendReceive */
148 smb_buffer->smb_buf_length = cpu_to_be32(smb_buffer->smb_buf_length);
149 cFYI(1, ("Sending smb of length %d ", smb_buf_length));
150 dump_smb(smb_buffer, len);
153 rc = kernel_sendmsg(ssocket, &smb_msg, &iov, 1, len);
154 if ((rc == -ENOSPC) || (rc == -EAGAIN)) {
158 ("sends on sock %p stuck for 30 seconds",
174 cERROR(1,("Error %d sending data on socket to server.", rc));
182 #ifdef CIFS_EXPERIMENTAL
183 /* BB finish off this function, adding support for writing set of pages as iovec */
184 /* and also adding support for operations that need to parse the response smb */
187 smb_sendv(struct socket *ssocket, struct smb_hdr *smb_buffer,
188 unsigned int smb_buf_length, struct kvec * write_vector
189 /* page list */, struct sockaddr *sin)
193 struct msghdr smb_msg;
194 number_of_pages += 1; /* account for SMB header */
195 struct kvec * piov = kmalloc(number_of_pages * sizeof(struct kvec));
196 unsigned len = smb_buf_length + 4;
199 return -ENOTSOCK; /* BB eventually add reconnect code here */
200 iov.iov_base = smb_buffer;
203 smb_msg.msg_name = sin;
204 smb_msg.msg_namelen = sizeof (struct sockaddr);
205 smb_msg.msg_control = NULL;
206 smb_msg.msg_controllen = 0;
207 smb_msg.msg_flags = MSG_DONTWAIT + MSG_NOSIGNAL; /* BB add more flags?*/
209 /* smb header is converted in header_assemble. bcc and rest of SMB word
210 area, and byte area if necessary, is converted to littleendian in
211 cifssmb.c and RFC1001 len is converted to bigendian in smb_send
212 Flags2 is converted in SendReceive */
214 smb_buffer->smb_buf_length = cpu_to_be32(smb_buffer->smb_buf_length);
215 cFYI(1, ("Sending smb of length %d ", smb_buf_length));
216 dump_smb(smb_buffer, len);
219 rc = kernel_sendmsg(ssocket, &smb_msg, &iov, number_of_pages,
221 if ((rc == -ENOSPC) || (rc == -EAGAIN)) {
225 ("sends on sock %p stuck for 30 seconds",
241 cERROR(1,("Error %d sending data on socket to server.", rc));
251 CIFSSendRcv(const unsigned int xid, struct cifsSesInfo *ses,
252 struct smb_hdr *in_buf, struct kvec * write_vector /* page list */, int *pbytes_returned, const int long_op)
255 unsigned long timeout = 15 * HZ;
256 struct mid_q_entry *midQ = NULL;
259 cERROR(1,("Null smb session"));
262 if(ses->server == NULL) {
263 cERROR(1,("Null tcp session"));
266 if(pbytes_returned == NULL)
269 *pbytes_returned = 0;
273 /* Ensure that we do not send more than 50 overlapping requests
274 to the same server. We may make this configurable later or
277 /* oplock breaks must not be held up */
278 atomic_inc(&ses->server->inFlight);
280 spin_lock(&GlobalMid_Lock);
282 if(atomic_read(&ses->server->inFlight) >= cifs_max_pending){
283 spin_unlock(&GlobalMid_Lock);
284 wait_event(ses->server->request_q,
285 atomic_read(&ses->server->inFlight)
287 spin_lock(&GlobalMid_Lock);
289 if(ses->server->tcpStatus == CifsExiting) {
290 spin_unlock(&GlobalMid_Lock);
294 /* can not count locking commands against total since
295 they are allowed to block on server */
298 /* update # of requests on the wire to server */
299 atomic_inc(&ses->server->inFlight);
301 spin_unlock(&GlobalMid_Lock);
306 /* make sure that we sign in the same order that we send on this socket
307 and avoid races inside tcp sendmsg code that could cause corruption
310 down(&ses->server->tcpSem);
312 if (ses->server->tcpStatus == CifsExiting) {
315 } else if (ses->server->tcpStatus == CifsNeedReconnect) {
316 cFYI(1,("tcp session dead - return to caller to retry"));
319 } else if (ses->status != CifsGood) {
320 /* check if SMB session is bad because we are setting it up */
321 if((in_buf->Command != SMB_COM_SESSION_SETUP_ANDX) &&
322 (in_buf->Command != SMB_COM_NEGOTIATE)) {
325 } /* else ok - we are setting up session */
327 midQ = AllocMidQEntry(in_buf, ses);
329 up(&ses->server->tcpSem);
330 /* If not lock req, update # of requests on wire to server */
332 atomic_dec(&ses->server->inFlight);
333 wake_up(&ses->server->request_q);
338 if (in_buf->smb_buf_length > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
339 up(&ses->server->tcpSem);
341 ("Illegal length, greater than maximum frame, %d ",
342 in_buf->smb_buf_length));
343 DeleteMidQEntry(midQ);
344 /* If not lock req, update # of requests on wire to server */
346 atomic_dec(&ses->server->inFlight);
347 wake_up(&ses->server->request_q);
352 /* BB can we sign efficiently in this path? */
353 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
355 midQ->midState = MID_REQUEST_SUBMITTED;
356 /* rc = smb_sendv(ses->server->ssocket, in_buf, in_buf->smb_buf_length,
358 (struct sockaddr *) &(ses->server->addr.sockAddr));*/
360 DeleteMidQEntry(midQ);
361 up(&ses->server->tcpSem);
362 /* If not lock req, update # of requests on wire to server */
364 atomic_dec(&ses->server->inFlight);
365 wake_up(&ses->server->request_q);
369 up(&ses->server->tcpSem);
372 DeleteMidQEntry(midQ);
375 atomic_dec(&ses->server->inFlight);
376 wake_up(&ses->server->request_q);
383 #endif /* CIFS_EXPERIMENTAL */
386 SendReceive(const unsigned int xid, struct cifsSesInfo *ses,
387 struct smb_hdr *in_buf, struct smb_hdr *out_buf,
388 int *pbytes_returned, const int long_op)
391 unsigned int receive_len;
392 unsigned long timeout;
393 struct mid_q_entry *midQ;
396 cERROR(1,("Null smb session"));
399 if(ses->server == NULL) {
400 cERROR(1,("Null tcp session"));
404 /* Ensure that we do not send more than 50 overlapping requests
405 to the same server. We may make this configurable later or
408 /* oplock breaks must not be held up */
409 atomic_inc(&ses->server->inFlight);
411 spin_lock(&GlobalMid_Lock);
413 if(atomic_read(&ses->server->inFlight) >=
415 spin_unlock(&GlobalMid_Lock);
416 wait_event(ses->server->request_q,
417 atomic_read(&ses->server->inFlight)
419 spin_lock(&GlobalMid_Lock);
421 if(ses->server->tcpStatus == CifsExiting) {
422 spin_unlock(&GlobalMid_Lock);
426 /* can not count locking commands against total since
427 they are allowed to block on server */
430 /* update # of requests on the wire to server */
431 atomic_inc(&ses->server->inFlight);
433 spin_unlock(&GlobalMid_Lock);
438 /* make sure that we sign in the same order that we send on this socket
439 and avoid races inside tcp sendmsg code that could cause corruption
442 down(&ses->server->tcpSem);
444 if (ses->server->tcpStatus == CifsExiting) {
447 } else if (ses->server->tcpStatus == CifsNeedReconnect) {
448 cFYI(1,("tcp session dead - return to caller to retry"));
451 } else if (ses->status != CifsGood) {
452 /* check if SMB session is bad because we are setting it up */
453 if((in_buf->Command != SMB_COM_SESSION_SETUP_ANDX) &&
454 (in_buf->Command != SMB_COM_NEGOTIATE)) {
457 } /* else ok - we are setting up session */
459 midQ = AllocMidQEntry(in_buf, ses);
461 up(&ses->server->tcpSem);
462 /* If not lock req, update # of requests on wire to server */
464 atomic_dec(&ses->server->inFlight);
465 wake_up(&ses->server->request_q);
470 if (in_buf->smb_buf_length > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
471 up(&ses->server->tcpSem);
473 ("Illegal length, greater than maximum frame, %d ",
474 in_buf->smb_buf_length));
475 DeleteMidQEntry(midQ);
476 /* If not lock req, update # of requests on wire to server */
478 atomic_dec(&ses->server->inFlight);
479 wake_up(&ses->server->request_q);
484 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
486 midQ->midState = MID_REQUEST_SUBMITTED;
487 rc = smb_send(ses->server->ssocket, in_buf, in_buf->smb_buf_length,
488 (struct sockaddr *) &(ses->server->addr.sockAddr));
490 DeleteMidQEntry(midQ);
491 up(&ses->server->tcpSem);
492 /* If not lock req, update # of requests on wire to server */
494 atomic_dec(&ses->server->inFlight);
495 wake_up(&ses->server->request_q);
499 up(&ses->server->tcpSem);
501 goto cifs_no_response_exit;
502 else if (long_op == 2) /* writes past end of file can take loong time */
504 else if (long_op == 1)
505 timeout = 45 * HZ; /* should be greater than
506 servers oplock break timeout (about 43 seconds) */
507 else if (long_op > 2) {
508 timeout = MAX_SCHEDULE_TIMEOUT;
511 /* wait for 15 seconds or until woken up due to response arriving or
512 due to last connection to this server being unmounted */
513 if (signal_pending(current)) {
514 /* if signal pending do not hold up user for full smb timeout
515 but we still give response a change to complete */
519 /* No user interrupts in wait - wreaks havoc with performance */
520 if(timeout != MAX_SCHEDULE_TIMEOUT) {
522 wait_event(ses->server->response_q,
523 (!(midQ->midState & MID_REQUEST_SUBMITTED)) ||
524 time_after(jiffies, timeout) ||
525 ((ses->server->tcpStatus != CifsGood) &&
526 (ses->server->tcpStatus != CifsNew)));
528 wait_event(ses->server->response_q,
529 (!(midQ->midState & MID_REQUEST_SUBMITTED)) ||
530 ((ses->server->tcpStatus != CifsGood) &&
531 (ses->server->tcpStatus != CifsNew)));
534 spin_lock(&GlobalMid_Lock);
535 if (midQ->resp_buf) {
536 spin_unlock(&GlobalMid_Lock);
537 receive_len = be32_to_cpu(*(__be32 *)midQ->resp_buf);
539 cERROR(1,("No response buffer"));
540 if(midQ->midState == MID_REQUEST_SUBMITTED) {
541 if(ses->server->tcpStatus == CifsExiting)
544 ses->server->tcpStatus = CifsNeedReconnect;
545 midQ->midState = MID_RETRY_NEEDED;
549 if (rc != -EHOSTDOWN) {
550 if(midQ->midState == MID_RETRY_NEEDED) {
552 cFYI(1,("marking request for retry"));
557 spin_unlock(&GlobalMid_Lock);
558 DeleteMidQEntry(midQ);
559 /* If not lock req, update # of requests on wire to server */
561 atomic_dec(&ses->server->inFlight);
562 wake_up(&ses->server->request_q);
567 if (receive_len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE) {
568 cERROR(1, ("Frame too large received. Length: %d Xid: %d",
571 } else { /* rcvd frame is ok */
573 if (midQ->resp_buf && out_buf
574 && (midQ->midState == MID_RESPONSE_RECEIVED)) {
575 out_buf->smb_buf_length = receive_len;
576 memcpy((char *)out_buf + 4,
577 (char *)midQ->resp_buf + 4,
580 dump_smb(out_buf, 92);
581 /* convert the length into a more usable form */
582 if((receive_len > 24) &&
583 (ses->server->secMode & (SECMODE_SIGN_REQUIRED |
584 SECMODE_SIGN_ENABLED))) {
585 rc = cifs_verify_signature(out_buf,
586 ses->server->mac_signing_key,
587 midQ->sequence_number+1);
589 cERROR(1,("Unexpected SMB signature"));
590 /* BB FIXME add code to kill session */
594 *pbytes_returned = out_buf->smb_buf_length;
596 /* BB special case reconnect tid and uid here? */
597 rc = map_smb_to_linux_error(out_buf);
599 /* convert ByteCount if necessary */
601 sizeof (struct smb_hdr) -
602 4 /* do not count RFC1001 header */ +
603 (2 * out_buf->WordCount) + 2 /* bcc */ )
604 BCC(out_buf) = le16_to_cpu(BCC(out_buf));
607 cFYI(1,("Bad MID state? "));
610 cifs_no_response_exit:
611 DeleteMidQEntry(midQ);
614 atomic_dec(&ses->server->inFlight);
615 wake_up(&ses->server->request_q);
621 up(&ses->server->tcpSem);
622 /* If not lock req, update # of requests on wire to server */
624 atomic_dec(&ses->server->inFlight);
625 wake_up(&ses->server->request_q);