blob: 6abd1445c983cd8d281511d855b2a34b52a6de72 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/cifs/transport.c
3 *
Steve Frenchad7a2922008-02-07 23:25:02 +00004 * Copyright (C) International Business Machines Corp., 2002,2008
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Author(s): Steve French (sfrench@us.ibm.com)
Steve French14a441a2b2006-07-16 04:32:51 +00006 * Jeremy Allison (jra@samba.org) 2006.
Steve French79a58d12007-07-06 22:44:50 +00007 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * This library is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published
10 * by the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
16 * the GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this library; if not, write to the Free Software
Steve French79a58d12007-07-06 22:44:50 +000020 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 */
22
23#include <linux/fs.h>
24#include <linux/list.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/wait.h>
27#include <linux/net.h>
28#include <linux/delay.h>
29#include <asm/uaccess.h>
30#include <asm/processor.h>
31#include <linux/mempool.h>
32#include "cifspdu.h"
33#include "cifsglob.h"
34#include "cifsproto.h"
35#include "cifs_debug.h"
Steve French50c2f752007-07-13 00:33:32 +000036
Linus Torvalds1da177e2005-04-16 15:20:36 -070037extern mempool_t *cifs_mid_poolp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39static struct mid_q_entry *
Jeff Layton24b9b062008-12-01 07:09:34 -050040AllocMidQEntry(const struct smb_hdr *smb_buffer, struct TCP_Server_Info *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
42 struct mid_q_entry *temp;
43
Jeff Layton24b9b062008-12-01 07:09:34 -050044 if (server == NULL) {
Joe Perchesb6b38f72010-04-21 03:50:45 +000045 cERROR(1, "Null TCP session in AllocMidQEntry");
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 return NULL;
47 }
Steve French50c2f752007-07-13 00:33:32 +000048
Pekka Enberg232087c2008-09-15 13:22:54 +030049 temp = mempool_alloc(cifs_mid_poolp, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 if (temp == NULL)
51 return temp;
52 else {
Steve French26f57362007-08-30 22:09:15 +000053 memset(temp, 0, sizeof(struct mid_q_entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 temp->mid = smb_buffer->Mid; /* always LE */
55 temp->pid = current->pid;
56 temp->command = smb_buffer->Command;
Joe Perchesb6b38f72010-04-21 03:50:45 +000057 cFYI(1, "For smb_command %d", temp->command);
Steve French1047abc2005-10-11 19:58:06 -070058 /* do_gettimeofday(&temp->when_sent);*/ /* easier to use jiffies */
59 /* when mid allocated can be before when sent */
60 temp->when_alloc = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 temp->tsk = current;
62 }
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 atomic_inc(&midCount);
65 temp->midState = MID_REQUEST_ALLOCATED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 return temp;
67}
68
69static void
70DeleteMidQEntry(struct mid_q_entry *midEntry)
71{
Steve French1047abc2005-10-11 19:58:06 -070072#ifdef CONFIG_CIFS_STATS2
73 unsigned long now;
74#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 midEntry->midState = MID_FREE;
Jeff Layton80975312011-01-11 07:24:02 -050076 atomic_dec(&midCount);
Steve French79a58d12007-07-06 22:44:50 +000077 if (midEntry->largeBuf)
Steve Frenchb8643e12005-04-28 22:41:07 -070078 cifs_buf_release(midEntry->resp_buf);
79 else
80 cifs_small_buf_release(midEntry->resp_buf);
Steve French1047abc2005-10-11 19:58:06 -070081#ifdef CONFIG_CIFS_STATS2
82 now = jiffies;
83 /* commands taking longer than one second are indications that
84 something is wrong, unless it is quite a slow link or server */
Steve French79a58d12007-07-06 22:44:50 +000085 if ((now - midEntry->when_alloc) > HZ) {
86 if ((cifsFYI & CIFS_TIMER) &&
Steve French1047abc2005-10-11 19:58:06 -070087 (midEntry->command != SMB_COM_LOCKING_ANDX)) {
88 printk(KERN_DEBUG " CIFS slow rsp: cmd %d mid %d",
89 midEntry->command, midEntry->mid);
90 printk(" A: 0x%lx S: 0x%lx R: 0x%lx\n",
91 now - midEntry->when_alloc,
92 now - midEntry->when_sent,
93 now - midEntry->when_received);
94 }
95 }
96#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 mempool_free(midEntry, cifs_mid_poolp);
98}
99
Jeff Laytonddc8cf82011-01-11 07:24:02 -0500100static void
101delete_mid(struct mid_q_entry *mid)
102{
103 spin_lock(&GlobalMid_Lock);
104 list_del(&mid->qhead);
105 spin_unlock(&GlobalMid_Lock);
106
107 DeleteMidQEntry(mid);
108}
109
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500110static int
Jeff Layton0496e022008-12-30 12:39:16 -0500111smb_sendv(struct TCP_Server_Info *server, struct kvec *iov, int n_vec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
113 int rc = 0;
114 int i = 0;
115 struct msghdr smb_msg;
Steve French3e844692005-10-03 13:37:24 -0700116 struct smb_hdr *smb_buffer = iov[0].iov_base;
117 unsigned int len = iov[0].iov_len;
118 unsigned int total_len;
119 int first_vec = 0;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000120 unsigned int smb_buf_length = smb_buffer->smb_buf_length;
Steve Frenchedf1ae42008-10-29 00:47:57 +0000121 struct socket *ssocket = server->ssocket;
Steve French50c2f752007-07-13 00:33:32 +0000122
Steve French79a58d12007-07-06 22:44:50 +0000123 if (ssocket == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 return -ENOTSOCK; /* BB eventually add reconnect code here */
Steve French3e844692005-10-03 13:37:24 -0700125
Pavel Shilovskya9f1b852010-12-13 19:08:35 +0300126 smb_msg.msg_name = (struct sockaddr *) &server->dstaddr;
Steve French26f57362007-08-30 22:09:15 +0000127 smb_msg.msg_namelen = sizeof(struct sockaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 smb_msg.msg_control = NULL;
129 smb_msg.msg_controllen = 0;
Jeff Layton0496e022008-12-30 12:39:16 -0500130 if (server->noblocksnd)
Steve Frenchedf1ae42008-10-29 00:47:57 +0000131 smb_msg.msg_flags = MSG_DONTWAIT + MSG_NOSIGNAL;
132 else
133 smb_msg.msg_flags = MSG_NOSIGNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135 /* smb header is converted in header_assemble. bcc and rest of SMB word
Steve French79a58d12007-07-06 22:44:50 +0000136 area, and byte area if necessary, is converted to littleendian in
137 cifssmb.c and RFC1001 len is converted to bigendian in smb_send
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 Flags2 is converted in SendReceive */
139
Steve French3e844692005-10-03 13:37:24 -0700140
141 total_len = 0;
142 for (i = 0; i < n_vec; i++)
143 total_len += iov[i].iov_len;
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 smb_buffer->smb_buf_length = cpu_to_be32(smb_buffer->smb_buf_length);
Joe Perchesb6b38f72010-04-21 03:50:45 +0000146 cFYI(1, "Sending smb: total_len %d", total_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 dump_smb(smb_buffer, len);
148
Shirish Pargaonkar17680352008-07-29 21:26:13 +0000149 i = 0;
Steve French3e844692005-10-03 13:37:24 -0700150 while (total_len) {
151 rc = kernel_sendmsg(ssocket, &smb_msg, &iov[first_vec],
152 n_vec - first_vec, total_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 if ((rc == -ENOSPC) || (rc == -EAGAIN)) {
154 i++;
Steve Frenchda505c32009-01-19 03:49:35 +0000155 /* if blocking send we try 3 times, since each can block
156 for 5 seconds. For nonblocking we have to try more
157 but wait increasing amounts of time allowing time for
158 socket to clear. The overall time we wait in either
159 case to send on the socket is about 15 seconds.
160 Similarly we wait for 15 seconds for
161 a response from the server in SendReceive[2]
162 for the server to send a response back for
163 most types of requests (except SMB Write
164 past end of file which can be slow, and
165 blocking lock operations). NFS waits slightly longer
166 than CIFS, but this can make it take longer for
167 nonresponsive servers to be detected and 15 seconds
168 is more than enough time for modern networks to
169 send a packet. In most cases if we fail to send
170 after the retries we will kill the socket and
171 reconnect which may clear the network problem.
172 */
173 if ((i >= 14) || (!server->noblocksnd && (i > 2))) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000174 cERROR(1, "sends on sock %p stuck for 15 seconds",
175 ssocket);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 rc = -EAGAIN;
177 break;
178 }
Steve French68058e72005-10-10 10:34:22 -0700179 msleep(1 << i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 continue;
181 }
Steve French79a58d12007-07-06 22:44:50 +0000182 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 break;
Steve French3e844692005-10-03 13:37:24 -0700184
Steve French61de8002008-10-30 20:15:22 +0000185 if (rc == total_len) {
186 total_len = 0;
187 break;
188 } else if (rc > total_len) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000189 cERROR(1, "sent %d requested %d", rc, total_len);
Steve French3e844692005-10-03 13:37:24 -0700190 break;
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500191 }
Steve French79a58d12007-07-06 22:44:50 +0000192 if (rc == 0) {
Steve French3e844692005-10-03 13:37:24 -0700193 /* should never happen, letting socket clear before
194 retrying is our only obvious option here */
Joe Perchesb6b38f72010-04-21 03:50:45 +0000195 cERROR(1, "tcp sent no data");
Steve French3e844692005-10-03 13:37:24 -0700196 msleep(500);
197 continue;
198 }
199 total_len -= rc;
Steve French68058e72005-10-10 10:34:22 -0700200 /* the line below resets i */
Steve French3e844692005-10-03 13:37:24 -0700201 for (i = first_vec; i < n_vec; i++) {
202 if (iov[i].iov_len) {
203 if (rc > iov[i].iov_len) {
204 rc -= iov[i].iov_len;
205 iov[i].iov_len = 0;
206 } else {
207 iov[i].iov_base += rc;
208 iov[i].iov_len -= rc;
209 first_vec = i;
210 break;
211 }
212 }
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500213 }
Steve French5e1253b2005-10-10 14:06:37 -0700214 i = 0; /* in case we get ENOSPC on the next send */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 }
216
Steve Frenchedf1ae42008-10-29 00:47:57 +0000217 if ((total_len > 0) && (total_len != smb_buf_length + 4)) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000218 cFYI(1, "partial send (%d remaining), terminating session",
219 total_len);
Steve Frenchedf1ae42008-10-29 00:47:57 +0000220 /* If we have only sent part of an SMB then the next SMB
221 could be taken as the remainder of this one. We need
222 to kill the socket so the server throws away the partial
223 SMB */
224 server->tcpStatus = CifsNeedReconnect;
225 }
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 if (rc < 0) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000228 cERROR(1, "Error %d sending data on socket to server", rc);
Steve French3e844692005-10-03 13:37:24 -0700229 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000232 /* Don't want to modify the buffer as a
233 side effect of this call. */
234 smb_buffer->smb_buf_length = smb_buf_length;
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 return rc;
237}
238
Jeff Layton0496e022008-12-30 12:39:16 -0500239int
240smb_send(struct TCP_Server_Info *server, struct smb_hdr *smb_buffer,
241 unsigned int smb_buf_length)
242{
243 struct kvec iov;
244
245 iov.iov_base = smb_buffer;
246 iov.iov_len = smb_buf_length + 4;
247
248 return smb_sendv(server, &iov, 1);
249}
250
Jeff Laytonc5797a92011-01-11 07:24:01 -0500251static int wait_for_free_request(struct TCP_Server_Info *server,
252 const int long_op)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000253{
Steve French133672e2007-11-13 22:41:37 +0000254 if (long_op == CIFS_ASYNC_OP) {
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000255 /* oplock breaks must not be held up */
Jeff Laytonc5797a92011-01-11 07:24:01 -0500256 atomic_inc(&server->inFlight);
Volker Lendecke27a97a62008-12-08 20:59:39 +0000257 return 0;
258 }
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000259
Volker Lendecke27a97a62008-12-08 20:59:39 +0000260 spin_lock(&GlobalMid_Lock);
261 while (1) {
Jeff Laytonc5797a92011-01-11 07:24:01 -0500262 if (atomic_read(&server->inFlight) >= cifs_max_pending) {
Volker Lendecke27a97a62008-12-08 20:59:39 +0000263 spin_unlock(&GlobalMid_Lock);
264#ifdef CONFIG_CIFS_STATS2
Jeff Laytonc5797a92011-01-11 07:24:01 -0500265 atomic_inc(&server->num_waiters);
Volker Lendecke27a97a62008-12-08 20:59:39 +0000266#endif
Jeff Laytonc5797a92011-01-11 07:24:01 -0500267 wait_event(server->request_q,
268 atomic_read(&server->inFlight)
Volker Lendecke27a97a62008-12-08 20:59:39 +0000269 < cifs_max_pending);
270#ifdef CONFIG_CIFS_STATS2
Jeff Laytonc5797a92011-01-11 07:24:01 -0500271 atomic_dec(&server->num_waiters);
Volker Lendecke27a97a62008-12-08 20:59:39 +0000272#endif
273 spin_lock(&GlobalMid_Lock);
274 } else {
Jeff Laytonc5797a92011-01-11 07:24:01 -0500275 if (server->tcpStatus == CifsExiting) {
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000276 spin_unlock(&GlobalMid_Lock);
Volker Lendecke27a97a62008-12-08 20:59:39 +0000277 return -ENOENT;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000278 }
Volker Lendecke27a97a62008-12-08 20:59:39 +0000279
280 /* can not count locking commands against total
281 as they are allowed to block on server */
282
283 /* update # of requests on the wire to server */
284 if (long_op != CIFS_BLOCKING_OP)
Jeff Laytonc5797a92011-01-11 07:24:01 -0500285 atomic_inc(&server->inFlight);
Volker Lendecke27a97a62008-12-08 20:59:39 +0000286 spin_unlock(&GlobalMid_Lock);
287 break;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000288 }
289 }
290 return 0;
291}
292
293static int allocate_mid(struct cifsSesInfo *ses, struct smb_hdr *in_buf,
294 struct mid_q_entry **ppmidQ)
295{
296 if (ses->server->tcpStatus == CifsExiting) {
297 return -ENOENT;
Volker Lendecke8fbbd362008-12-06 13:12:34 +0100298 }
299
300 if (ses->server->tcpStatus == CifsNeedReconnect) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000301 cFYI(1, "tcp session dead - return to caller to retry");
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000302 return -EAGAIN;
Volker Lendecke8fbbd362008-12-06 13:12:34 +0100303 }
304
305 if (ses->status != CifsGood) {
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000306 /* check if SMB session is bad because we are setting it up */
Steve French79a58d12007-07-06 22:44:50 +0000307 if ((in_buf->Command != SMB_COM_SESSION_SETUP_ANDX) &&
Steve Frenchad7a2922008-02-07 23:25:02 +0000308 (in_buf->Command != SMB_COM_NEGOTIATE))
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000309 return -EAGAIN;
Steve Frenchad7a2922008-02-07 23:25:02 +0000310 /* else ok - we are setting up session */
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000311 }
Jeff Layton24b9b062008-12-01 07:09:34 -0500312 *ppmidQ = AllocMidQEntry(in_buf, ses->server);
Steve French26f57362007-08-30 22:09:15 +0000313 if (*ppmidQ == NULL)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000314 return -ENOMEM;
Jeff Laytonddc8cf82011-01-11 07:24:02 -0500315 spin_lock(&GlobalMid_Lock);
316 list_add_tail(&(*ppmidQ)->qhead, &ses->server->pending_mid_q);
317 spin_unlock(&GlobalMid_Lock);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000318 return 0;
319}
320
Jeff Layton0ade6402011-01-11 07:24:02 -0500321static int
322wait_for_response(struct TCP_Server_Info *server, struct mid_q_entry *midQ)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000323{
Jeff Layton0ade6402011-01-11 07:24:02 -0500324 int error;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000325
Jeff Layton0ade6402011-01-11 07:24:02 -0500326 error = wait_event_killable(server->response_q,
327 midQ->midState != MID_REQUEST_SUBMITTED);
328 if (error < 0)
329 return -ERESTARTSYS;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000330
Jeff Layton0ade6402011-01-11 07:24:02 -0500331 return 0;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000332}
333
Steve French133672e2007-11-13 22:41:37 +0000334
335/*
336 *
337 * Send an SMB Request. No response info (other than return code)
338 * needs to be parsed.
339 *
340 * flags indicate the type of request buffer and how long to wait
341 * and whether to log NT STATUS code (error) before mapping it to POSIX error
342 *
343 */
344int
345SendReceiveNoRsp(const unsigned int xid, struct cifsSesInfo *ses,
346 struct smb_hdr *in_buf, int flags)
347{
348 int rc;
349 struct kvec iov[1];
350 int resp_buf_type;
351
352 iov[0].iov_base = (char *)in_buf;
353 iov[0].iov_len = in_buf->smb_buf_length + 4;
354 flags |= CIFS_NO_RESP;
355 rc = SendReceive2(xid, ses, iov, 1, &resp_buf_type, flags);
Joe Perchesb6b38f72010-04-21 03:50:45 +0000356 cFYI(DBG2, "SendRcvNoRsp flags %d rc %d", flags, rc);
Steve French90c81e02008-02-12 20:32:36 +0000357
Steve French133672e2007-11-13 22:41:37 +0000358 return rc;
359}
360
Jeff Layton053d5032011-01-11 07:24:02 -0500361static int
362sync_mid_result(struct mid_q_entry *mid, struct TCP_Server_Info *server)
363{
364 int rc = 0;
365
Jeff Layton74dd92a2011-01-11 07:24:02 -0500366 cFYI(1, "%s: cmd=%d mid=%d state=%d", __func__, mid->command,
367 mid->mid, mid->midState);
Jeff Layton053d5032011-01-11 07:24:02 -0500368
Jeff Layton74dd92a2011-01-11 07:24:02 -0500369 spin_lock(&GlobalMid_Lock);
370 switch (mid->midState) {
371 case MID_RESPONSE_RECEIVED:
Jeff Layton053d5032011-01-11 07:24:02 -0500372 spin_unlock(&GlobalMid_Lock);
373 return rc;
Jeff Layton74dd92a2011-01-11 07:24:02 -0500374 case MID_REQUEST_SUBMITTED:
375 /* socket is going down, reject all calls */
376 if (server->tcpStatus == CifsExiting) {
377 cERROR(1, "%s: canceling mid=%d cmd=0x%x state=%d",
378 __func__, mid->mid, mid->command, mid->midState);
Jeff Layton053d5032011-01-11 07:24:02 -0500379 rc = -EHOSTDOWN;
Jeff Layton74dd92a2011-01-11 07:24:02 -0500380 break;
Jeff Layton053d5032011-01-11 07:24:02 -0500381 }
Jeff Layton74dd92a2011-01-11 07:24:02 -0500382 case MID_RETRY_NEEDED:
383 rc = -EAGAIN;
384 break;
385 default:
386 cERROR(1, "%s: invalid mid state mid=%d state=%d", __func__,
387 mid->mid, mid->midState);
388 rc = -EIO;
Jeff Layton053d5032011-01-11 07:24:02 -0500389 }
390 spin_unlock(&GlobalMid_Lock);
391
392 delete_mid(mid);
393 return rc;
394}
395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396int
Steve French79a58d12007-07-06 22:44:50 +0000397SendReceive2(const unsigned int xid, struct cifsSesInfo *ses,
398 struct kvec *iov, int n_vec, int *pRespBufType /* ret */,
Steve French133672e2007-11-13 22:41:37 +0000399 const int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
401 int rc = 0;
Steve French133672e2007-11-13 22:41:37 +0000402 int long_op;
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500403 unsigned int receive_len;
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500404 struct mid_q_entry *midQ;
Steve French3e844692005-10-03 13:37:24 -0700405 struct smb_hdr *in_buf = iov[0].iov_base;
Steve French50c2f752007-07-13 00:33:32 +0000406
Steve French133672e2007-11-13 22:41:37 +0000407 long_op = flags & CIFS_TIMEOUT_MASK;
408
Steve Frenchec637e32005-12-12 20:53:18 -0800409 *pRespBufType = CIFS_NO_BUFFER; /* no response buf yet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Steve French4b8f9302006-02-26 16:41:18 +0000411 if ((ses == NULL) || (ses->server == NULL)) {
412 cifs_small_buf_release(in_buf);
Joe Perchesb6b38f72010-04-21 03:50:45 +0000413 cERROR(1, "Null session");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 return -EIO;
415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Steve French79a58d12007-07-06 22:44:50 +0000417 if (ses->server->tcpStatus == CifsExiting) {
Steve French4b8f9302006-02-26 16:41:18 +0000418 cifs_small_buf_release(in_buf);
Steve French31ca3bc2005-04-28 22:41:11 -0700419 return -ENOENT;
Steve French4b8f9302006-02-26 16:41:18 +0000420 }
Steve French31ca3bc2005-04-28 22:41:11 -0700421
Steve French79a58d12007-07-06 22:44:50 +0000422 /* Ensure that we do not send more than 50 overlapping requests
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 to the same server. We may make this configurable later or
424 use ses->maxReq */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Jeff Laytonc5797a92011-01-11 07:24:01 -0500426 rc = wait_for_free_request(ses->server, long_op);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000427 if (rc) {
428 cifs_small_buf_release(in_buf);
429 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 }
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000431
Steve French79a58d12007-07-06 22:44:50 +0000432 /* make sure that we sign in the same order that we send on this socket
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 and avoid races inside tcp sendmsg code that could cause corruption
434 of smb data */
435
Jeff Layton72ca5452008-12-01 07:09:36 -0500436 mutex_lock(&ses->server->srv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000438 rc = allocate_mid(ses, in_buf, &midQ);
439 if (rc) {
Jeff Layton72ca5452008-12-01 07:09:36 -0500440 mutex_unlock(&ses->server->srv_mutex);
Steve French4b8f9302006-02-26 16:41:18 +0000441 cifs_small_buf_release(in_buf);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000442 /* Update # of requests on wire to server */
Steve French79a58d12007-07-06 22:44:50 +0000443 atomic_dec(&ses->server->inFlight);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000444 wake_up(&ses->server->request_q);
445 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 }
Steve French79a58d12007-07-06 22:44:50 +0000447 rc = cifs_sign_smb2(iov, n_vec, ses->server, &midQ->sequence_number);
Volker Lendecke829049c2008-12-06 16:00:53 +0100448 if (rc) {
449 mutex_unlock(&ses->server->srv_mutex);
450 cifs_small_buf_release(in_buf);
451 goto out;
452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454 midQ->midState = MID_REQUEST_SUBMITTED;
Steve French131afd0b2005-10-07 09:51:05 -0700455#ifdef CONFIG_CIFS_STATS2
456 atomic_inc(&ses->server->inSend);
457#endif
Jeff Layton0496e022008-12-30 12:39:16 -0500458 rc = smb_sendv(ses->server, iov, n_vec);
Steve French131afd0b2005-10-07 09:51:05 -0700459#ifdef CONFIG_CIFS_STATS2
460 atomic_dec(&ses->server->inSend);
Steve French1047abc2005-10-11 19:58:06 -0700461 midQ->when_sent = jiffies;
Steve French131afd0b2005-10-07 09:51:05 -0700462#endif
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000463
Jeff Layton72ca5452008-12-01 07:09:36 -0500464 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000465 cifs_small_buf_release(in_buf);
466
Steve French79a58d12007-07-06 22:44:50 +0000467 if (rc < 0)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000468 goto out;
Steve French4b8f9302006-02-26 16:41:18 +0000469
Jeff Layton0ade6402011-01-11 07:24:02 -0500470 if (long_op == CIFS_ASYNC_OP)
Steve French133672e2007-11-13 22:41:37 +0000471 goto out;
Jeff Layton0ade6402011-01-11 07:24:02 -0500472
473 rc = wait_for_response(ses->server, midQ);
474 if (rc != 0)
Steve French133672e2007-11-13 22:41:37 +0000475 goto out;
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500476
Jeff Layton053d5032011-01-11 07:24:02 -0500477 rc = sync_mid_result(midQ, ses->server);
478 if (rc != 0) {
Steve French79a58d12007-07-06 22:44:50 +0000479 atomic_dec(&ses->server->inFlight);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000480 wake_up(&ses->server->request_q);
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500481 return rc;
482 }
Steve French50c2f752007-07-13 00:33:32 +0000483
Volker Lendecke8e4f2e82008-12-06 16:22:15 +0100484 receive_len = midQ->resp_buf->smb_buf_length;
485
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500486 if (receive_len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000487 cERROR(1, "Frame too large received. Length: %d Xid: %d",
488 receive_len, xid);
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500489 rc = -EIO;
Steve French2b2bdfb2008-12-11 17:26:54 +0000490 goto out;
491 }
Steve French84afc292005-12-02 13:32:45 -0800492
Steve French2b2bdfb2008-12-11 17:26:54 +0000493 /* rcvd frame is ok */
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500494
Steve French2b2bdfb2008-12-11 17:26:54 +0000495 if (midQ->resp_buf &&
496 (midQ->midState == MID_RESPONSE_RECEIVED)) {
497
498 iov[0].iov_base = (char *)midQ->resp_buf;
499 if (midQ->largeBuf)
500 *pRespBufType = CIFS_LARGE_BUFFER;
501 else
502 *pRespBufType = CIFS_SMALL_BUFFER;
503 iov[0].iov_len = receive_len + 4;
504
505 dump_smb(midQ->resp_buf, 80);
506 /* convert the length into a more usable form */
507 if ((receive_len > 24) &&
508 (ses->server->secMode & (SECMODE_SIGN_REQUIRED |
509 SECMODE_SIGN_ENABLED))) {
510 rc = cifs_verify_signature(midQ->resp_buf,
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500511 ses->server,
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500512 midQ->sequence_number+1);
Steve French2b2bdfb2008-12-11 17:26:54 +0000513 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000514 cERROR(1, "Unexpected SMB signature");
Steve French2b2bdfb2008-12-11 17:26:54 +0000515 /* BB FIXME add code to kill session */
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500516 }
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500517 }
Steve French2b2bdfb2008-12-11 17:26:54 +0000518
519 /* BB special case reconnect tid and uid here? */
520 rc = map_smb_to_linux_error(midQ->resp_buf,
521 flags & CIFS_LOG_ERROR);
522
523 /* convert ByteCount if necessary */
524 if (receive_len >= sizeof(struct smb_hdr) - 4
525 /* do not count RFC1001 header */ +
526 (2 * midQ->resp_buf->WordCount) + 2 /* bcc */ )
527 BCC(midQ->resp_buf) =
528 le16_to_cpu(BCC_LE(midQ->resp_buf));
529 if ((flags & CIFS_NO_RESP) == 0)
530 midQ->resp_buf = NULL; /* mark it so buf will
531 not be freed by
Jeff Laytonddc8cf82011-01-11 07:24:02 -0500532 delete_mid */
Steve French2b2bdfb2008-12-11 17:26:54 +0000533 } else {
534 rc = -EIO;
Joe Perchesb6b38f72010-04-21 03:50:45 +0000535 cFYI(1, "Bad MID state?");
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500536 }
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000537
538out:
Jeff Laytonddc8cf82011-01-11 07:24:02 -0500539 delete_mid(midQ);
Steve French79a58d12007-07-06 22:44:50 +0000540 atomic_dec(&ses->server->inFlight);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000541 wake_up(&ses->server->request_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 return rc;
544}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546int
547SendReceive(const unsigned int xid, struct cifsSesInfo *ses,
548 struct smb_hdr *in_buf, struct smb_hdr *out_buf,
549 int *pbytes_returned, const int long_op)
550{
551 int rc = 0;
552 unsigned int receive_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 struct mid_q_entry *midQ;
554
555 if (ses == NULL) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000556 cERROR(1, "Null smb session");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 return -EIO;
558 }
Steve French79a58d12007-07-06 22:44:50 +0000559 if (ses->server == NULL) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000560 cERROR(1, "Null tcp session");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 return -EIO;
562 }
563
Steve French79a58d12007-07-06 22:44:50 +0000564 if (ses->server->tcpStatus == CifsExiting)
Steve French31ca3bc2005-04-28 22:41:11 -0700565 return -ENOENT;
566
Steve French79a58d12007-07-06 22:44:50 +0000567 /* Ensure that we do not send more than 50 overlapping requests
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 to the same server. We may make this configurable later or
569 use ses->maxReq */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Volker Lendecke6d9c6d52008-12-08 20:50:24 +0000571 if (in_buf->smb_buf_length > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000572 cERROR(1, "Illegal length, greater than maximum frame, %d",
573 in_buf->smb_buf_length);
Volker Lendecke6d9c6d52008-12-08 20:50:24 +0000574 return -EIO;
575 }
576
Jeff Laytonc5797a92011-01-11 07:24:01 -0500577 rc = wait_for_free_request(ses->server, long_op);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000578 if (rc)
579 return rc;
580
Steve French79a58d12007-07-06 22:44:50 +0000581 /* make sure that we sign in the same order that we send on this socket
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 and avoid races inside tcp sendmsg code that could cause corruption
583 of smb data */
584
Jeff Layton72ca5452008-12-01 07:09:36 -0500585 mutex_lock(&ses->server->srv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000587 rc = allocate_mid(ses, in_buf, &midQ);
588 if (rc) {
Jeff Layton72ca5452008-12-01 07:09:36 -0500589 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000590 /* Update # of requests on wire to server */
Steve French79a58d12007-07-06 22:44:50 +0000591 atomic_dec(&ses->server->inFlight);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000592 wake_up(&ses->server->request_q);
593 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 }
595
Steve Frenchad009ac2005-04-28 22:41:05 -0700596 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
Volker Lendecke829049c2008-12-06 16:00:53 +0100597 if (rc) {
598 mutex_unlock(&ses->server->srv_mutex);
599 goto out;
600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602 midQ->midState = MID_REQUEST_SUBMITTED;
Steve French131afd0b2005-10-07 09:51:05 -0700603#ifdef CONFIG_CIFS_STATS2
604 atomic_inc(&ses->server->inSend);
605#endif
Jeff Layton0496e022008-12-30 12:39:16 -0500606 rc = smb_send(ses->server, in_buf, in_buf->smb_buf_length);
Steve French131afd0b2005-10-07 09:51:05 -0700607#ifdef CONFIG_CIFS_STATS2
608 atomic_dec(&ses->server->inSend);
Steve French1047abc2005-10-11 19:58:06 -0700609 midQ->when_sent = jiffies;
Steve French131afd0b2005-10-07 09:51:05 -0700610#endif
Jeff Layton72ca5452008-12-01 07:09:36 -0500611 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000612
Steve French79a58d12007-07-06 22:44:50 +0000613 if (rc < 0)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000614 goto out;
615
Jeff Layton0ade6402011-01-11 07:24:02 -0500616 if (long_op == CIFS_ASYNC_OP)
Steve French133672e2007-11-13 22:41:37 +0000617 goto out;
Jeff Layton0ade6402011-01-11 07:24:02 -0500618
619 rc = wait_for_response(ses->server, midQ);
620 if (rc != 0)
Steve French133672e2007-11-13 22:41:37 +0000621 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Jeff Layton053d5032011-01-11 07:24:02 -0500623 rc = sync_mid_result(midQ, ses->server);
624 if (rc != 0) {
Steve French79a58d12007-07-06 22:44:50 +0000625 atomic_dec(&ses->server->inFlight);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000626 wake_up(&ses->server->request_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 return rc;
628 }
Steve French50c2f752007-07-13 00:33:32 +0000629
Volker Lendecke8e4f2e82008-12-06 16:22:15 +0100630 receive_len = midQ->resp_buf->smb_buf_length;
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 if (receive_len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000633 cERROR(1, "Frame too large received. Length: %d Xid: %d",
634 receive_len, xid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 rc = -EIO;
Steve French2b2bdfb2008-12-11 17:26:54 +0000636 goto out;
637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Steve French2b2bdfb2008-12-11 17:26:54 +0000639 /* rcvd frame is ok */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Steve French2b2bdfb2008-12-11 17:26:54 +0000641 if (midQ->resp_buf && out_buf
642 && (midQ->midState == MID_RESPONSE_RECEIVED)) {
643 out_buf->smb_buf_length = receive_len;
644 memcpy((char *)out_buf + 4,
645 (char *)midQ->resp_buf + 4,
646 receive_len);
647
648 dump_smb(out_buf, 92);
649 /* convert the length into a more usable form */
650 if ((receive_len > 24) &&
651 (ses->server->secMode & (SECMODE_SIGN_REQUIRED |
652 SECMODE_SIGN_ENABLED))) {
653 rc = cifs_verify_signature(out_buf,
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500654 ses->server,
Steve Frenchad009ac2005-04-28 22:41:05 -0700655 midQ->sequence_number+1);
Steve French2b2bdfb2008-12-11 17:26:54 +0000656 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000657 cERROR(1, "Unexpected SMB signature");
Steve French2b2bdfb2008-12-11 17:26:54 +0000658 /* BB FIXME add code to kill session */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 }
Steve French2b2bdfb2008-12-11 17:26:54 +0000661
662 *pbytes_returned = out_buf->smb_buf_length;
663
664 /* BB special case reconnect tid and uid here? */
665 rc = map_smb_to_linux_error(out_buf, 0 /* no log */ );
666
667 /* convert ByteCount if necessary */
668 if (receive_len >= sizeof(struct smb_hdr) - 4
669 /* do not count RFC1001 header */ +
670 (2 * out_buf->WordCount) + 2 /* bcc */ )
671 BCC(out_buf) = le16_to_cpu(BCC_LE(out_buf));
672 } else {
673 rc = -EIO;
Joe Perchesb6b38f72010-04-21 03:50:45 +0000674 cERROR(1, "Bad MID state?");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000677out:
Jeff Laytonddc8cf82011-01-11 07:24:02 -0500678 delete_mid(midQ);
Steve French79a58d12007-07-06 22:44:50 +0000679 atomic_dec(&ses->server->inFlight);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000680 wake_up(&ses->server->request_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682 return rc;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000683}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000685/* Send an NT_CANCEL SMB to cause the POSIX blocking lock to return. */
686
687static int
688send_nt_cancel(struct cifsTconInfo *tcon, struct smb_hdr *in_buf,
689 struct mid_q_entry *midQ)
690{
691 int rc = 0;
692 struct cifsSesInfo *ses = tcon->ses;
693 __u16 mid = in_buf->Mid;
694
695 header_assemble(in_buf, SMB_COM_NT_CANCEL, tcon, 0);
696 in_buf->Mid = mid;
Jeff Layton72ca5452008-12-01 07:09:36 -0500697 mutex_lock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000698 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
699 if (rc) {
Jeff Layton72ca5452008-12-01 07:09:36 -0500700 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000701 return rc;
702 }
Jeff Layton0496e022008-12-30 12:39:16 -0500703 rc = smb_send(ses->server, in_buf, in_buf->smb_buf_length);
Jeff Layton72ca5452008-12-01 07:09:36 -0500704 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000705 return rc;
706}
707
708/* We send a LOCKINGX_CANCEL_LOCK to cause the Windows
709 blocking lock to return. */
710
711static int
712send_lock_cancel(const unsigned int xid, struct cifsTconInfo *tcon,
713 struct smb_hdr *in_buf,
714 struct smb_hdr *out_buf)
715{
716 int bytes_returned;
717 struct cifsSesInfo *ses = tcon->ses;
718 LOCK_REQ *pSMB = (LOCK_REQ *)in_buf;
719
720 /* We just modify the current in_buf to change
721 the type of lock from LOCKING_ANDX_SHARED_LOCK
722 or LOCKING_ANDX_EXCLUSIVE_LOCK to
723 LOCKING_ANDX_CANCEL_LOCK. */
724
725 pSMB->LockType = LOCKING_ANDX_CANCEL_LOCK|LOCKING_ANDX_LARGE_FILES;
726 pSMB->Timeout = 0;
727 pSMB->hdr.Mid = GetNextMid(ses->server);
728
729 return SendReceive(xid, ses, in_buf, out_buf,
Steve French133672e2007-11-13 22:41:37 +0000730 &bytes_returned, CIFS_STD_OP);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000731}
732
733int
734SendReceiveBlockingLock(const unsigned int xid, struct cifsTconInfo *tcon,
735 struct smb_hdr *in_buf, struct smb_hdr *out_buf,
736 int *pbytes_returned)
737{
738 int rc = 0;
739 int rstart = 0;
740 unsigned int receive_len;
741 struct mid_q_entry *midQ;
742 struct cifsSesInfo *ses;
743
744 if (tcon == NULL || tcon->ses == NULL) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000745 cERROR(1, "Null smb session");
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000746 return -EIO;
747 }
748 ses = tcon->ses;
749
Steve French79a58d12007-07-06 22:44:50 +0000750 if (ses->server == NULL) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000751 cERROR(1, "Null tcp session");
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000752 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 }
754
Steve French79a58d12007-07-06 22:44:50 +0000755 if (ses->server->tcpStatus == CifsExiting)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000756 return -ENOENT;
757
Steve French79a58d12007-07-06 22:44:50 +0000758 /* Ensure that we do not send more than 50 overlapping requests
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000759 to the same server. We may make this configurable later or
760 use ses->maxReq */
761
Volker Lendecke6d9c6d52008-12-08 20:50:24 +0000762 if (in_buf->smb_buf_length > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000763 cERROR(1, "Illegal length, greater than maximum frame, %d",
764 in_buf->smb_buf_length);
Volker Lendecke6d9c6d52008-12-08 20:50:24 +0000765 return -EIO;
766 }
767
Jeff Laytonc5797a92011-01-11 07:24:01 -0500768 rc = wait_for_free_request(ses->server, CIFS_BLOCKING_OP);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000769 if (rc)
770 return rc;
771
Steve French79a58d12007-07-06 22:44:50 +0000772 /* make sure that we sign in the same order that we send on this socket
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000773 and avoid races inside tcp sendmsg code that could cause corruption
774 of smb data */
775
Jeff Layton72ca5452008-12-01 07:09:36 -0500776 mutex_lock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000777
778 rc = allocate_mid(ses, in_buf, &midQ);
779 if (rc) {
Jeff Layton72ca5452008-12-01 07:09:36 -0500780 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000781 return rc;
782 }
783
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000784 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
Volker Lendecke829049c2008-12-06 16:00:53 +0100785 if (rc) {
Jeff Laytonddc8cf82011-01-11 07:24:02 -0500786 delete_mid(midQ);
Volker Lendecke829049c2008-12-06 16:00:53 +0100787 mutex_unlock(&ses->server->srv_mutex);
788 return rc;
789 }
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000790
791 midQ->midState = MID_REQUEST_SUBMITTED;
792#ifdef CONFIG_CIFS_STATS2
793 atomic_inc(&ses->server->inSend);
794#endif
Jeff Layton0496e022008-12-30 12:39:16 -0500795 rc = smb_send(ses->server, in_buf, in_buf->smb_buf_length);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000796#ifdef CONFIG_CIFS_STATS2
797 atomic_dec(&ses->server->inSend);
798 midQ->when_sent = jiffies;
799#endif
Jeff Layton72ca5452008-12-01 07:09:36 -0500800 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000801
Steve French79a58d12007-07-06 22:44:50 +0000802 if (rc < 0) {
Jeff Laytonddc8cf82011-01-11 07:24:02 -0500803 delete_mid(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000804 return rc;
805 }
806
807 /* Wait for a reply - allow signals to interrupt. */
808 rc = wait_event_interruptible(ses->server->response_q,
Steve French79a58d12007-07-06 22:44:50 +0000809 (!(midQ->midState == MID_REQUEST_SUBMITTED)) ||
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000810 ((ses->server->tcpStatus != CifsGood) &&
811 (ses->server->tcpStatus != CifsNew)));
812
813 /* Were we interrupted by a signal ? */
814 if ((rc == -ERESTARTSYS) &&
815 (midQ->midState == MID_REQUEST_SUBMITTED) &&
816 ((ses->server->tcpStatus == CifsGood) ||
817 (ses->server->tcpStatus == CifsNew))) {
818
819 if (in_buf->Command == SMB_COM_TRANSACTION2) {
820 /* POSIX lock. We send a NT_CANCEL SMB to cause the
821 blocking lock to return. */
822
823 rc = send_nt_cancel(tcon, in_buf, midQ);
824 if (rc) {
Jeff Laytonddc8cf82011-01-11 07:24:02 -0500825 delete_mid(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000826 return rc;
827 }
828 } else {
829 /* Windows lock. We send a LOCKINGX_CANCEL_LOCK
830 to cause the blocking lock to return. */
831
832 rc = send_lock_cancel(xid, tcon, in_buf, out_buf);
833
834 /* If we get -ENOLCK back the lock may have
835 already been removed. Don't exit in this case. */
836 if (rc && rc != -ENOLCK) {
Jeff Laytonddc8cf82011-01-11 07:24:02 -0500837 delete_mid(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000838 return rc;
839 }
840 }
841
Jeff Layton0ade6402011-01-11 07:24:02 -0500842 if (wait_for_response(ses->server, midQ) == 0) {
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000843 /* We got the response - restart system call. */
844 rstart = 1;
845 }
846 }
847
Jeff Layton053d5032011-01-11 07:24:02 -0500848 rc = sync_mid_result(midQ, ses->server);
849 if (rc != 0)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000850 return rc;
Steve French50c2f752007-07-13 00:33:32 +0000851
Jeff Layton053d5032011-01-11 07:24:02 -0500852 receive_len = midQ->resp_buf->smb_buf_length;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000853 if (receive_len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000854 cERROR(1, "Frame too large received. Length: %d Xid: %d",
855 receive_len, xid);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000856 rc = -EIO;
Volker Lendecke17c8bfe2008-12-06 16:38:19 +0100857 goto out;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000858 }
Volker Lendecke17c8bfe2008-12-06 16:38:19 +0100859
860 /* rcvd frame is ok */
861
Volker Lendeckeac6a3ef2008-12-06 16:40:40 +0100862 if ((out_buf == NULL) || (midQ->midState != MID_RESPONSE_RECEIVED)) {
Volker Lendecke17c8bfe2008-12-06 16:38:19 +0100863 rc = -EIO;
Joe Perchesb6b38f72010-04-21 03:50:45 +0000864 cERROR(1, "Bad MID state?");
Volker Lendecke698e96a2008-12-06 16:39:31 +0100865 goto out;
Volker Lendecke17c8bfe2008-12-06 16:38:19 +0100866 }
867
Volker Lendecke698e96a2008-12-06 16:39:31 +0100868 out_buf->smb_buf_length = receive_len;
869 memcpy((char *)out_buf + 4,
870 (char *)midQ->resp_buf + 4,
871 receive_len);
872
873 dump_smb(out_buf, 92);
874 /* convert the length into a more usable form */
875 if ((receive_len > 24) &&
876 (ses->server->secMode & (SECMODE_SIGN_REQUIRED |
877 SECMODE_SIGN_ENABLED))) {
878 rc = cifs_verify_signature(out_buf,
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500879 ses->server,
Volker Lendecke698e96a2008-12-06 16:39:31 +0100880 midQ->sequence_number+1);
881 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000882 cERROR(1, "Unexpected SMB signature");
Volker Lendecke698e96a2008-12-06 16:39:31 +0100883 /* BB FIXME add code to kill session */
884 }
885 }
886
887 *pbytes_returned = out_buf->smb_buf_length;
888
889 /* BB special case reconnect tid and uid here? */
890 rc = map_smb_to_linux_error(out_buf, 0 /* no log */ );
891
892 /* convert ByteCount if necessary */
893 if (receive_len >= sizeof(struct smb_hdr) - 4
894 /* do not count RFC1001 header */ +
895 (2 * out_buf->WordCount) + 2 /* bcc */ )
896 BCC(out_buf) = le16_to_cpu(BCC_LE(out_buf));
897
Volker Lendecke17c8bfe2008-12-06 16:38:19 +0100898out:
Jeff Laytonddc8cf82011-01-11 07:24:02 -0500899 delete_mid(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000900 if (rstart && rc == -EACCES)
901 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 return rc;
903}