blob: ad081fe7eb18b5442a1e1c8289c175e42596bc14 [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;
Christoph Lametere18b8902006-12-06 20:33:20 -080038extern struct kmem_cache *cifs_oplock_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40static struct mid_q_entry *
Jeff Layton24b9b062008-12-01 07:09:34 -050041AllocMidQEntry(const struct smb_hdr *smb_buffer, struct TCP_Server_Info *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
43 struct mid_q_entry *temp;
44
Jeff Layton24b9b062008-12-01 07:09:34 -050045 if (server == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 cERROR(1, ("Null TCP session in AllocMidQEntry"));
47 return NULL;
48 }
Steve French50c2f752007-07-13 00:33:32 +000049
Pekka Enberg232087c2008-09-15 13:22:54 +030050 temp = mempool_alloc(cifs_mid_poolp, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 if (temp == NULL)
52 return temp;
53 else {
Steve French26f57362007-08-30 22:09:15 +000054 memset(temp, 0, sizeof(struct mid_q_entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 temp->mid = smb_buffer->Mid; /* always LE */
56 temp->pid = current->pid;
57 temp->command = smb_buffer->Command;
58 cFYI(1, ("For smb_command %d", temp->command));
Steve French1047abc2005-10-11 19:58:06 -070059 /* do_gettimeofday(&temp->when_sent);*/ /* easier to use jiffies */
60 /* when mid allocated can be before when sent */
61 temp->when_alloc = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 temp->tsk = current;
63 }
64
65 spin_lock(&GlobalMid_Lock);
Jeff Layton24b9b062008-12-01 07:09:34 -050066 list_add_tail(&temp->qhead, &server->pending_mid_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 atomic_inc(&midCount);
68 temp->midState = MID_REQUEST_ALLOCATED;
69 spin_unlock(&GlobalMid_Lock);
70 return temp;
71}
72
73static void
74DeleteMidQEntry(struct mid_q_entry *midEntry)
75{
Steve French1047abc2005-10-11 19:58:06 -070076#ifdef CONFIG_CIFS_STATS2
77 unsigned long now;
78#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 spin_lock(&GlobalMid_Lock);
80 midEntry->midState = MID_FREE;
81 list_del(&midEntry->qhead);
82 atomic_dec(&midCount);
83 spin_unlock(&GlobalMid_Lock);
Steve French79a58d12007-07-06 22:44:50 +000084 if (midEntry->largeBuf)
Steve Frenchb8643e12005-04-28 22:41:07 -070085 cifs_buf_release(midEntry->resp_buf);
86 else
87 cifs_small_buf_release(midEntry->resp_buf);
Steve French1047abc2005-10-11 19:58:06 -070088#ifdef CONFIG_CIFS_STATS2
89 now = jiffies;
90 /* commands taking longer than one second are indications that
91 something is wrong, unless it is quite a slow link or server */
Steve French79a58d12007-07-06 22:44:50 +000092 if ((now - midEntry->when_alloc) > HZ) {
93 if ((cifsFYI & CIFS_TIMER) &&
Steve French1047abc2005-10-11 19:58:06 -070094 (midEntry->command != SMB_COM_LOCKING_ANDX)) {
95 printk(KERN_DEBUG " CIFS slow rsp: cmd %d mid %d",
96 midEntry->command, midEntry->mid);
97 printk(" A: 0x%lx S: 0x%lx R: 0x%lx\n",
98 now - midEntry->when_alloc,
99 now - midEntry->when_sent,
100 now - midEntry->when_received);
101 }
102 }
103#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 mempool_free(midEntry, cifs_mid_poolp);
105}
106
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500107static int
Jeff Layton0496e022008-12-30 12:39:16 -0500108smb_sendv(struct TCP_Server_Info *server, struct kvec *iov, int n_vec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
110 int rc = 0;
111 int i = 0;
112 struct msghdr smb_msg;
Steve French3e844692005-10-03 13:37:24 -0700113 struct smb_hdr *smb_buffer = iov[0].iov_base;
114 unsigned int len = iov[0].iov_len;
115 unsigned int total_len;
116 int first_vec = 0;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000117 unsigned int smb_buf_length = smb_buffer->smb_buf_length;
Steve Frenchedf1ae42008-10-29 00:47:57 +0000118 struct socket *ssocket = server->ssocket;
Steve French50c2f752007-07-13 00:33:32 +0000119
Steve French79a58d12007-07-06 22:44:50 +0000120 if (ssocket == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 return -ENOTSOCK; /* BB eventually add reconnect code here */
Steve French3e844692005-10-03 13:37:24 -0700122
Jeff Layton0496e022008-12-30 12:39:16 -0500123 smb_msg.msg_name = (struct sockaddr *) &server->addr.sockAddr;
Steve French26f57362007-08-30 22:09:15 +0000124 smb_msg.msg_namelen = sizeof(struct sockaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 smb_msg.msg_control = NULL;
126 smb_msg.msg_controllen = 0;
Jeff Layton0496e022008-12-30 12:39:16 -0500127 if (server->noblocksnd)
Steve Frenchedf1ae42008-10-29 00:47:57 +0000128 smb_msg.msg_flags = MSG_DONTWAIT + MSG_NOSIGNAL;
129 else
130 smb_msg.msg_flags = MSG_NOSIGNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132 /* smb header is converted in header_assemble. bcc and rest of SMB word
Steve French79a58d12007-07-06 22:44:50 +0000133 area, and byte area if necessary, is converted to littleendian in
134 cifssmb.c and RFC1001 len is converted to bigendian in smb_send
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 Flags2 is converted in SendReceive */
136
Steve French3e844692005-10-03 13:37:24 -0700137
138 total_len = 0;
139 for (i = 0; i < n_vec; i++)
140 total_len += iov[i].iov_len;
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 smb_buffer->smb_buf_length = cpu_to_be32(smb_buffer->smb_buf_length);
Steve French3e844692005-10-03 13:37:24 -0700143 cFYI(1, ("Sending smb: total_len %d", total_len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 dump_smb(smb_buffer, len);
145
Shirish Pargaonkar17680352008-07-29 21:26:13 +0000146 i = 0;
Steve French3e844692005-10-03 13:37:24 -0700147 while (total_len) {
148 rc = kernel_sendmsg(ssocket, &smb_msg, &iov[first_vec],
149 n_vec - first_vec, total_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 if ((rc == -ENOSPC) || (rc == -EAGAIN)) {
151 i++;
Steve Frenchda505c32009-01-19 03:49:35 +0000152 /* if blocking send we try 3 times, since each can block
153 for 5 seconds. For nonblocking we have to try more
154 but wait increasing amounts of time allowing time for
155 socket to clear. The overall time we wait in either
156 case to send on the socket is about 15 seconds.
157 Similarly we wait for 15 seconds for
158 a response from the server in SendReceive[2]
159 for the server to send a response back for
160 most types of requests (except SMB Write
161 past end of file which can be slow, and
162 blocking lock operations). NFS waits slightly longer
163 than CIFS, but this can make it take longer for
164 nonresponsive servers to be detected and 15 seconds
165 is more than enough time for modern networks to
166 send a packet. In most cases if we fail to send
167 after the retries we will kill the socket and
168 reconnect which may clear the network problem.
169 */
170 if ((i >= 14) || (!server->noblocksnd && (i > 2))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 cERROR(1,
Steve French68058e72005-10-10 10:34:22 -0700172 ("sends on sock %p stuck for 15 seconds",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 ssocket));
174 rc = -EAGAIN;
175 break;
176 }
Steve French68058e72005-10-10 10:34:22 -0700177 msleep(1 << i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 continue;
179 }
Steve French79a58d12007-07-06 22:44:50 +0000180 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 break;
Steve French3e844692005-10-03 13:37:24 -0700182
Steve French61de8002008-10-30 20:15:22 +0000183 if (rc == total_len) {
184 total_len = 0;
185 break;
186 } else if (rc > total_len) {
187 cERROR(1, ("sent %d requested %d", rc, total_len));
Steve French3e844692005-10-03 13:37:24 -0700188 break;
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500189 }
Steve French79a58d12007-07-06 22:44:50 +0000190 if (rc == 0) {
Steve French3e844692005-10-03 13:37:24 -0700191 /* should never happen, letting socket clear before
192 retrying is our only obvious option here */
Steve French79a58d12007-07-06 22:44:50 +0000193 cERROR(1, ("tcp sent no data"));
Steve French3e844692005-10-03 13:37:24 -0700194 msleep(500);
195 continue;
196 }
197 total_len -= rc;
Steve French68058e72005-10-10 10:34:22 -0700198 /* the line below resets i */
Steve French3e844692005-10-03 13:37:24 -0700199 for (i = first_vec; i < n_vec; i++) {
200 if (iov[i].iov_len) {
201 if (rc > iov[i].iov_len) {
202 rc -= iov[i].iov_len;
203 iov[i].iov_len = 0;
204 } else {
205 iov[i].iov_base += rc;
206 iov[i].iov_len -= rc;
207 first_vec = i;
208 break;
209 }
210 }
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500211 }
Steve French5e1253b2005-10-10 14:06:37 -0700212 i = 0; /* in case we get ENOSPC on the next send */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 }
214
Steve Frenchedf1ae42008-10-29 00:47:57 +0000215 if ((total_len > 0) && (total_len != smb_buf_length + 4)) {
216 cFYI(1, ("partial send (%d remaining), terminating session",
217 total_len));
218 /* If we have only sent part of an SMB then the next SMB
219 could be taken as the remainder of this one. We need
220 to kill the socket so the server throws away the partial
221 SMB */
222 server->tcpStatus = CifsNeedReconnect;
223 }
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 if (rc < 0) {
Steve French79a58d12007-07-06 22:44:50 +0000226 cERROR(1, ("Error %d sending data on socket to server", rc));
Steve French3e844692005-10-03 13:37:24 -0700227 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000230 /* Don't want to modify the buffer as a
231 side effect of this call. */
232 smb_buffer->smb_buf_length = smb_buf_length;
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 return rc;
235}
236
Jeff Layton0496e022008-12-30 12:39:16 -0500237int
238smb_send(struct TCP_Server_Info *server, struct smb_hdr *smb_buffer,
239 unsigned int smb_buf_length)
240{
241 struct kvec iov;
242
243 iov.iov_base = smb_buffer;
244 iov.iov_len = smb_buf_length + 4;
245
246 return smb_sendv(server, &iov, 1);
247}
248
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000249static int wait_for_free_request(struct cifsSesInfo *ses, const int long_op)
250{
Steve French133672e2007-11-13 22:41:37 +0000251 if (long_op == CIFS_ASYNC_OP) {
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000252 /* oplock breaks must not be held up */
253 atomic_inc(&ses->server->inFlight);
Volker Lendecke27a97a62008-12-08 20:59:39 +0000254 return 0;
255 }
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000256
Volker Lendecke27a97a62008-12-08 20:59:39 +0000257 spin_lock(&GlobalMid_Lock);
258 while (1) {
259 if (atomic_read(&ses->server->inFlight) >=
260 cifs_max_pending){
261 spin_unlock(&GlobalMid_Lock);
262#ifdef CONFIG_CIFS_STATS2
263 atomic_inc(&ses->server->num_waiters);
264#endif
265 wait_event(ses->server->request_q,
266 atomic_read(&ses->server->inFlight)
267 < cifs_max_pending);
268#ifdef CONFIG_CIFS_STATS2
269 atomic_dec(&ses->server->num_waiters);
270#endif
271 spin_lock(&GlobalMid_Lock);
272 } else {
273 if (ses->server->tcpStatus == CifsExiting) {
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000274 spin_unlock(&GlobalMid_Lock);
Volker Lendecke27a97a62008-12-08 20:59:39 +0000275 return -ENOENT;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000276 }
Volker Lendecke27a97a62008-12-08 20:59:39 +0000277
278 /* can not count locking commands against total
279 as they are allowed to block on server */
280
281 /* update # of requests on the wire to server */
282 if (long_op != CIFS_BLOCKING_OP)
283 atomic_inc(&ses->server->inFlight);
284 spin_unlock(&GlobalMid_Lock);
285 break;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000286 }
287 }
288 return 0;
289}
290
291static int allocate_mid(struct cifsSesInfo *ses, struct smb_hdr *in_buf,
292 struct mid_q_entry **ppmidQ)
293{
294 if (ses->server->tcpStatus == CifsExiting) {
295 return -ENOENT;
Volker Lendecke8fbbd362008-12-06 13:12:34 +0100296 }
297
298 if (ses->server->tcpStatus == CifsNeedReconnect) {
Steve French79a58d12007-07-06 22:44:50 +0000299 cFYI(1, ("tcp session dead - return to caller to retry"));
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000300 return -EAGAIN;
Volker Lendecke8fbbd362008-12-06 13:12:34 +0100301 }
302
303 if (ses->status != CifsGood) {
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000304 /* check if SMB session is bad because we are setting it up */
Steve French79a58d12007-07-06 22:44:50 +0000305 if ((in_buf->Command != SMB_COM_SESSION_SETUP_ANDX) &&
Steve Frenchad7a2922008-02-07 23:25:02 +0000306 (in_buf->Command != SMB_COM_NEGOTIATE))
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000307 return -EAGAIN;
Steve Frenchad7a2922008-02-07 23:25:02 +0000308 /* else ok - we are setting up session */
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000309 }
Jeff Layton24b9b062008-12-01 07:09:34 -0500310 *ppmidQ = AllocMidQEntry(in_buf, ses->server);
Steve French26f57362007-08-30 22:09:15 +0000311 if (*ppmidQ == NULL)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000312 return -ENOMEM;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000313 return 0;
314}
315
Steve French79a58d12007-07-06 22:44:50 +0000316static int wait_for_response(struct cifsSesInfo *ses,
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000317 struct mid_q_entry *midQ,
318 unsigned long timeout,
319 unsigned long time_to_wait)
320{
321 unsigned long curr_timeout;
322
323 for (;;) {
324 curr_timeout = timeout + jiffies;
Jeff Layton85705522008-12-05 20:41:21 -0500325 wait_event_timeout(ses->server->response_q,
326 midQ->midState != MID_REQUEST_SUBMITTED, timeout);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000327
328 if (time_after(jiffies, curr_timeout) &&
329 (midQ->midState == MID_REQUEST_SUBMITTED) &&
330 ((ses->server->tcpStatus == CifsGood) ||
331 (ses->server->tcpStatus == CifsNew))) {
332
333 unsigned long lrt;
334
335 /* We timed out. Is the server still
336 sending replies ? */
337 spin_lock(&GlobalMid_Lock);
338 lrt = ses->server->lstrp;
339 spin_unlock(&GlobalMid_Lock);
340
341 /* Calculate time_to_wait past last receive time.
Steve French79a58d12007-07-06 22:44:50 +0000342 Although we prefer not to time out if the
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000343 server is still responding - we will time
Steve French79a58d12007-07-06 22:44:50 +0000344 out if the server takes more than 15 (or 45
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000345 or 180) seconds to respond to this request
Steve French79a58d12007-07-06 22:44:50 +0000346 and has not responded to any request from
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000347 other threads on the client within 10 seconds */
348 lrt += time_to_wait;
349 if (time_after(jiffies, lrt)) {
350 /* No replies for time_to_wait. */
Steve French79a58d12007-07-06 22:44:50 +0000351 cERROR(1, ("server not responding"));
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000352 return -1;
353 }
354 } else {
355 return 0;
356 }
357 }
358}
359
Steve French133672e2007-11-13 22:41:37 +0000360
361/*
362 *
363 * Send an SMB Request. No response info (other than return code)
364 * needs to be parsed.
365 *
366 * flags indicate the type of request buffer and how long to wait
367 * and whether to log NT STATUS code (error) before mapping it to POSIX error
368 *
369 */
370int
371SendReceiveNoRsp(const unsigned int xid, struct cifsSesInfo *ses,
372 struct smb_hdr *in_buf, int flags)
373{
374 int rc;
375 struct kvec iov[1];
376 int resp_buf_type;
377
378 iov[0].iov_base = (char *)in_buf;
379 iov[0].iov_len = in_buf->smb_buf_length + 4;
380 flags |= CIFS_NO_RESP;
381 rc = SendReceive2(xid, ses, iov, 1, &resp_buf_type, flags);
Steve French90c81e02008-02-12 20:32:36 +0000382 cFYI(DBG2, ("SendRcvNoRsp flags %d rc %d", flags, rc));
383
Steve French133672e2007-11-13 22:41:37 +0000384 return rc;
385}
386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387int
Steve French79a58d12007-07-06 22:44:50 +0000388SendReceive2(const unsigned int xid, struct cifsSesInfo *ses,
389 struct kvec *iov, int n_vec, int *pRespBufType /* ret */,
Steve French133672e2007-11-13 22:41:37 +0000390 const int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
392 int rc = 0;
Steve French133672e2007-11-13 22:41:37 +0000393 int long_op;
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500394 unsigned int receive_len;
395 unsigned long timeout;
396 struct mid_q_entry *midQ;
Steve French3e844692005-10-03 13:37:24 -0700397 struct smb_hdr *in_buf = iov[0].iov_base;
Steve French50c2f752007-07-13 00:33:32 +0000398
Steve French133672e2007-11-13 22:41:37 +0000399 long_op = flags & CIFS_TIMEOUT_MASK;
400
Steve Frenchec637e32005-12-12 20:53:18 -0800401 *pRespBufType = CIFS_NO_BUFFER; /* no response buf yet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Steve French4b8f9302006-02-26 16:41:18 +0000403 if ((ses == NULL) || (ses->server == NULL)) {
404 cifs_small_buf_release(in_buf);
Steve French79a58d12007-07-06 22:44:50 +0000405 cERROR(1, ("Null session"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 return -EIO;
407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Steve French79a58d12007-07-06 22:44:50 +0000409 if (ses->server->tcpStatus == CifsExiting) {
Steve French4b8f9302006-02-26 16:41:18 +0000410 cifs_small_buf_release(in_buf);
Steve French31ca3bc2005-04-28 22:41:11 -0700411 return -ENOENT;
Steve French4b8f9302006-02-26 16:41:18 +0000412 }
Steve French31ca3bc2005-04-28 22:41:11 -0700413
Steve French79a58d12007-07-06 22:44:50 +0000414 /* Ensure that we do not send more than 50 overlapping requests
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 to the same server. We may make this configurable later or
416 use ses->maxReq */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000418 rc = wait_for_free_request(ses, long_op);
419 if (rc) {
420 cifs_small_buf_release(in_buf);
421 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 }
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000423
Steve French79a58d12007-07-06 22:44:50 +0000424 /* make sure that we sign in the same order that we send on this socket
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 and avoid races inside tcp sendmsg code that could cause corruption
426 of smb data */
427
Jeff Layton72ca5452008-12-01 07:09:36 -0500428 mutex_lock(&ses->server->srv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000430 rc = allocate_mid(ses, in_buf, &midQ);
431 if (rc) {
Jeff Layton72ca5452008-12-01 07:09:36 -0500432 mutex_unlock(&ses->server->srv_mutex);
Steve French4b8f9302006-02-26 16:41:18 +0000433 cifs_small_buf_release(in_buf);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000434 /* Update # of requests on wire to server */
Steve French79a58d12007-07-06 22:44:50 +0000435 atomic_dec(&ses->server->inFlight);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000436 wake_up(&ses->server->request_q);
437 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 }
Steve French79a58d12007-07-06 22:44:50 +0000439 rc = cifs_sign_smb2(iov, n_vec, ses->server, &midQ->sequence_number);
Volker Lendecke829049c2008-12-06 16:00:53 +0100440 if (rc) {
441 mutex_unlock(&ses->server->srv_mutex);
442 cifs_small_buf_release(in_buf);
443 goto out;
444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 midQ->midState = MID_REQUEST_SUBMITTED;
Steve French131afd0b2005-10-07 09:51:05 -0700447#ifdef CONFIG_CIFS_STATS2
448 atomic_inc(&ses->server->inSend);
449#endif
Jeff Layton0496e022008-12-30 12:39:16 -0500450 rc = smb_sendv(ses->server, iov, n_vec);
Steve French131afd0b2005-10-07 09:51:05 -0700451#ifdef CONFIG_CIFS_STATS2
452 atomic_dec(&ses->server->inSend);
Steve French1047abc2005-10-11 19:58:06 -0700453 midQ->when_sent = jiffies;
Steve French131afd0b2005-10-07 09:51:05 -0700454#endif
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000455
Jeff Layton72ca5452008-12-01 07:09:36 -0500456 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000457 cifs_small_buf_release(in_buf);
458
Steve French79a58d12007-07-06 22:44:50 +0000459 if (rc < 0)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000460 goto out;
Steve French4b8f9302006-02-26 16:41:18 +0000461
Steve French133672e2007-11-13 22:41:37 +0000462 if (long_op == CIFS_STD_OP)
463 timeout = 15 * HZ;
464 else if (long_op == CIFS_VLONG_OP) /* e.g. slow writes past EOF */
Steve French37c0eb42005-10-05 14:50:29 -0700465 timeout = 180 * HZ;
Steve French133672e2007-11-13 22:41:37 +0000466 else if (long_op == CIFS_LONG_OP)
Steve French79a58d12007-07-06 22:44:50 +0000467 timeout = 45 * HZ; /* should be greater than
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500468 servers oplock break timeout (about 43 seconds) */
Steve French133672e2007-11-13 22:41:37 +0000469 else if (long_op == CIFS_ASYNC_OP)
470 goto out;
471 else if (long_op == CIFS_BLOCKING_OP)
472 timeout = 0x7FFFFFFF; /* large, but not so large as to wrap */
473 else {
474 cERROR(1, ("unknown timeout flag %d", long_op));
475 rc = -EIO;
476 goto out;
477 }
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000478
Steve French79a58d12007-07-06 22:44:50 +0000479 /* wait for 15 seconds or until woken up due to response arriving or
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500480 due to last connection to this server being unmounted */
481 if (signal_pending(current)) {
482 /* if signal pending do not hold up user for full smb timeout
Steve French8a236262007-03-06 00:31:00 +0000483 but we still give response a chance to complete */
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500484 timeout = 2 * HZ;
Steve French79a58d12007-07-06 22:44:50 +0000485 }
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500486
487 /* No user interrupts in wait - wreaks havoc with performance */
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000488 wait_for_response(ses, midQ, timeout, 10 * HZ);
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500489
490 spin_lock(&GlobalMid_Lock);
Volker Lendecke8e4f2e82008-12-06 16:22:15 +0100491
492 if (midQ->resp_buf == NULL) {
Steve French79a58d12007-07-06 22:44:50 +0000493 cERROR(1, ("No response to cmd %d mid %d",
Steve French37c0eb42005-10-05 14:50:29 -0700494 midQ->command, midQ->mid));
Steve French79a58d12007-07-06 22:44:50 +0000495 if (midQ->midState == MID_REQUEST_SUBMITTED) {
496 if (ses->server->tcpStatus == CifsExiting)
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500497 rc = -EHOSTDOWN;
498 else {
499 ses->server->tcpStatus = CifsNeedReconnect;
500 midQ->midState = MID_RETRY_NEEDED;
501 }
502 }
503
504 if (rc != -EHOSTDOWN) {
Steve French79a58d12007-07-06 22:44:50 +0000505 if (midQ->midState == MID_RETRY_NEEDED) {
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500506 rc = -EAGAIN;
Steve French79a58d12007-07-06 22:44:50 +0000507 cFYI(1, ("marking request for retry"));
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500508 } else {
509 rc = -EIO;
510 }
511 }
512 spin_unlock(&GlobalMid_Lock);
513 DeleteMidQEntry(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000514 /* Update # of requests on wire to server */
Steve French79a58d12007-07-06 22:44:50 +0000515 atomic_dec(&ses->server->inFlight);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000516 wake_up(&ses->server->request_q);
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500517 return rc;
518 }
Steve French50c2f752007-07-13 00:33:32 +0000519
Volker Lendecke8e4f2e82008-12-06 16:22:15 +0100520 spin_unlock(&GlobalMid_Lock);
521 receive_len = midQ->resp_buf->smb_buf_length;
522
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500523 if (receive_len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE) {
524 cERROR(1, ("Frame too large received. Length: %d Xid: %d",
525 receive_len, xid));
526 rc = -EIO;
Steve French2b2bdfb2008-12-11 17:26:54 +0000527 goto out;
528 }
Steve French84afc292005-12-02 13:32:45 -0800529
Steve French2b2bdfb2008-12-11 17:26:54 +0000530 /* rcvd frame is ok */
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500531
Steve French2b2bdfb2008-12-11 17:26:54 +0000532 if (midQ->resp_buf &&
533 (midQ->midState == MID_RESPONSE_RECEIVED)) {
534
535 iov[0].iov_base = (char *)midQ->resp_buf;
536 if (midQ->largeBuf)
537 *pRespBufType = CIFS_LARGE_BUFFER;
538 else
539 *pRespBufType = CIFS_SMALL_BUFFER;
540 iov[0].iov_len = receive_len + 4;
541
542 dump_smb(midQ->resp_buf, 80);
543 /* convert the length into a more usable form */
544 if ((receive_len > 24) &&
545 (ses->server->secMode & (SECMODE_SIGN_REQUIRED |
546 SECMODE_SIGN_ENABLED))) {
547 rc = cifs_verify_signature(midQ->resp_buf,
Steve Frenchb609f062007-07-09 07:55:14 +0000548 &ses->server->mac_signing_key,
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500549 midQ->sequence_number+1);
Steve French2b2bdfb2008-12-11 17:26:54 +0000550 if (rc) {
551 cERROR(1, ("Unexpected SMB signature"));
552 /* BB FIXME add code to kill session */
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500553 }
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500554 }
Steve French2b2bdfb2008-12-11 17:26:54 +0000555
556 /* BB special case reconnect tid and uid here? */
557 rc = map_smb_to_linux_error(midQ->resp_buf,
558 flags & CIFS_LOG_ERROR);
559
560 /* convert ByteCount if necessary */
561 if (receive_len >= sizeof(struct smb_hdr) - 4
562 /* do not count RFC1001 header */ +
563 (2 * midQ->resp_buf->WordCount) + 2 /* bcc */ )
564 BCC(midQ->resp_buf) =
565 le16_to_cpu(BCC_LE(midQ->resp_buf));
566 if ((flags & CIFS_NO_RESP) == 0)
567 midQ->resp_buf = NULL; /* mark it so buf will
568 not be freed by
569 DeleteMidQEntry */
570 } else {
571 rc = -EIO;
572 cFYI(1, ("Bad MID state?"));
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500573 }
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000574
575out:
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500576 DeleteMidQEntry(midQ);
Steve French79a58d12007-07-06 22:44:50 +0000577 atomic_dec(&ses->server->inFlight);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000578 wake_up(&ses->server->request_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 return rc;
581}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583int
584SendReceive(const unsigned int xid, struct cifsSesInfo *ses,
585 struct smb_hdr *in_buf, struct smb_hdr *out_buf,
586 int *pbytes_returned, const int long_op)
587{
588 int rc = 0;
589 unsigned int receive_len;
590 unsigned long timeout;
591 struct mid_q_entry *midQ;
592
593 if (ses == NULL) {
Steve French79a58d12007-07-06 22:44:50 +0000594 cERROR(1, ("Null smb session"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 return -EIO;
596 }
Steve French79a58d12007-07-06 22:44:50 +0000597 if (ses->server == NULL) {
598 cERROR(1, ("Null tcp session"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 return -EIO;
600 }
601
Steve French79a58d12007-07-06 22:44:50 +0000602 if (ses->server->tcpStatus == CifsExiting)
Steve French31ca3bc2005-04-28 22:41:11 -0700603 return -ENOENT;
604
Steve French79a58d12007-07-06 22:44:50 +0000605 /* Ensure that we do not send more than 50 overlapping requests
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 to the same server. We may make this configurable later or
607 use ses->maxReq */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Volker Lendecke6d9c6d52008-12-08 20:50:24 +0000609 if (in_buf->smb_buf_length > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
610 cERROR(1, ("Illegal length, greater than maximum frame, %d",
611 in_buf->smb_buf_length));
612 return -EIO;
613 }
614
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000615 rc = wait_for_free_request(ses, long_op);
616 if (rc)
617 return rc;
618
Steve French79a58d12007-07-06 22:44:50 +0000619 /* make sure that we sign in the same order that we send on this socket
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 and avoid races inside tcp sendmsg code that could cause corruption
621 of smb data */
622
Jeff Layton72ca5452008-12-01 07:09:36 -0500623 mutex_lock(&ses->server->srv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000625 rc = allocate_mid(ses, in_buf, &midQ);
626 if (rc) {
Jeff Layton72ca5452008-12-01 07:09:36 -0500627 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000628 /* Update # of requests on wire to server */
Steve French79a58d12007-07-06 22:44:50 +0000629 atomic_dec(&ses->server->inFlight);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000630 wake_up(&ses->server->request_q);
631 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 }
633
Steve Frenchad009ac2005-04-28 22:41:05 -0700634 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
Volker Lendecke829049c2008-12-06 16:00:53 +0100635 if (rc) {
636 mutex_unlock(&ses->server->srv_mutex);
637 goto out;
638 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640 midQ->midState = MID_REQUEST_SUBMITTED;
Steve French131afd0b2005-10-07 09:51:05 -0700641#ifdef CONFIG_CIFS_STATS2
642 atomic_inc(&ses->server->inSend);
643#endif
Jeff Layton0496e022008-12-30 12:39:16 -0500644 rc = smb_send(ses->server, in_buf, in_buf->smb_buf_length);
Steve French131afd0b2005-10-07 09:51:05 -0700645#ifdef CONFIG_CIFS_STATS2
646 atomic_dec(&ses->server->inSend);
Steve French1047abc2005-10-11 19:58:06 -0700647 midQ->when_sent = jiffies;
Steve French131afd0b2005-10-07 09:51:05 -0700648#endif
Jeff Layton72ca5452008-12-01 07:09:36 -0500649 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000650
Steve French79a58d12007-07-06 22:44:50 +0000651 if (rc < 0)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000652 goto out;
653
Steve French133672e2007-11-13 22:41:37 +0000654 if (long_op == CIFS_STD_OP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 timeout = 15 * HZ;
Steve French79a58d12007-07-06 22:44:50 +0000656 /* wait for 15 seconds or until woken up due to response arriving or
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 due to last connection to this server being unmounted */
Steve French133672e2007-11-13 22:41:37 +0000658 else if (long_op == CIFS_ASYNC_OP)
659 goto out;
660 else if (long_op == CIFS_VLONG_OP) /* writes past EOF can be slow */
661 timeout = 180 * HZ;
662 else if (long_op == CIFS_LONG_OP)
663 timeout = 45 * HZ; /* should be greater than
664 servers oplock break timeout (about 43 seconds) */
665 else if (long_op == CIFS_BLOCKING_OP)
666 timeout = 0x7FFFFFFF; /* large but no so large as to wrap */
667 else {
668 cERROR(1, ("unknown timeout flag %d", long_op));
669 rc = -EIO;
670 goto out;
671 }
672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 if (signal_pending(current)) {
674 /* if signal pending do not hold up user for full smb timeout
Steve French8a236262007-03-06 00:31:00 +0000675 but we still give response a chance to complete */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 timeout = 2 * HZ;
Steve French79a58d12007-07-06 22:44:50 +0000677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
679 /* No user interrupts in wait - wreaks havoc with performance */
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000680 wait_for_response(ses, midQ, timeout, 10 * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682 spin_lock(&GlobalMid_Lock);
Volker Lendecke8e4f2e82008-12-06 16:22:15 +0100683 if (midQ->resp_buf == NULL) {
Steve French79a58d12007-07-06 22:44:50 +0000684 cERROR(1, ("No response for cmd %d mid %d",
Steve French37c0eb42005-10-05 14:50:29 -0700685 midQ->command, midQ->mid));
Steve French79a58d12007-07-06 22:44:50 +0000686 if (midQ->midState == MID_REQUEST_SUBMITTED) {
687 if (ses->server->tcpStatus == CifsExiting)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 rc = -EHOSTDOWN;
689 else {
690 ses->server->tcpStatus = CifsNeedReconnect;
691 midQ->midState = MID_RETRY_NEEDED;
692 }
693 }
694
695 if (rc != -EHOSTDOWN) {
Steve French79a58d12007-07-06 22:44:50 +0000696 if (midQ->midState == MID_RETRY_NEEDED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 rc = -EAGAIN;
Steve French79a58d12007-07-06 22:44:50 +0000698 cFYI(1, ("marking request for retry"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 } else {
700 rc = -EIO;
701 }
702 }
703 spin_unlock(&GlobalMid_Lock);
704 DeleteMidQEntry(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000705 /* Update # of requests on wire to server */
Steve French79a58d12007-07-06 22:44:50 +0000706 atomic_dec(&ses->server->inFlight);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000707 wake_up(&ses->server->request_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 return rc;
709 }
Steve French50c2f752007-07-13 00:33:32 +0000710
Volker Lendecke8e4f2e82008-12-06 16:22:15 +0100711 spin_unlock(&GlobalMid_Lock);
712 receive_len = midQ->resp_buf->smb_buf_length;
713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 if (receive_len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE) {
Steve Frenchad009ac2005-04-28 22:41:05 -0700715 cERROR(1, ("Frame too large received. Length: %d Xid: %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 receive_len, xid));
717 rc = -EIO;
Steve French2b2bdfb2008-12-11 17:26:54 +0000718 goto out;
719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Steve French2b2bdfb2008-12-11 17:26:54 +0000721 /* rcvd frame is ok */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Steve French2b2bdfb2008-12-11 17:26:54 +0000723 if (midQ->resp_buf && out_buf
724 && (midQ->midState == MID_RESPONSE_RECEIVED)) {
725 out_buf->smb_buf_length = receive_len;
726 memcpy((char *)out_buf + 4,
727 (char *)midQ->resp_buf + 4,
728 receive_len);
729
730 dump_smb(out_buf, 92);
731 /* convert the length into a more usable form */
732 if ((receive_len > 24) &&
733 (ses->server->secMode & (SECMODE_SIGN_REQUIRED |
734 SECMODE_SIGN_ENABLED))) {
735 rc = cifs_verify_signature(out_buf,
Steve Frenchb609f062007-07-09 07:55:14 +0000736 &ses->server->mac_signing_key,
Steve Frenchad009ac2005-04-28 22:41:05 -0700737 midQ->sequence_number+1);
Steve French2b2bdfb2008-12-11 17:26:54 +0000738 if (rc) {
739 cERROR(1, ("Unexpected SMB signature"));
740 /* BB FIXME add code to kill session */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 }
Steve French2b2bdfb2008-12-11 17:26:54 +0000743
744 *pbytes_returned = out_buf->smb_buf_length;
745
746 /* BB special case reconnect tid and uid here? */
747 rc = map_smb_to_linux_error(out_buf, 0 /* no log */ );
748
749 /* convert ByteCount if necessary */
750 if (receive_len >= sizeof(struct smb_hdr) - 4
751 /* do not count RFC1001 header */ +
752 (2 * out_buf->WordCount) + 2 /* bcc */ )
753 BCC(out_buf) = le16_to_cpu(BCC_LE(out_buf));
754 } else {
755 rc = -EIO;
756 cERROR(1, ("Bad MID state?"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000759out:
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000760 DeleteMidQEntry(midQ);
Steve French79a58d12007-07-06 22:44:50 +0000761 atomic_dec(&ses->server->inFlight);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000762 wake_up(&ses->server->request_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764 return rc;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000765}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000767/* Send an NT_CANCEL SMB to cause the POSIX blocking lock to return. */
768
769static int
770send_nt_cancel(struct cifsTconInfo *tcon, struct smb_hdr *in_buf,
771 struct mid_q_entry *midQ)
772{
773 int rc = 0;
774 struct cifsSesInfo *ses = tcon->ses;
775 __u16 mid = in_buf->Mid;
776
777 header_assemble(in_buf, SMB_COM_NT_CANCEL, tcon, 0);
778 in_buf->Mid = mid;
Jeff Layton72ca5452008-12-01 07:09:36 -0500779 mutex_lock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000780 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
781 if (rc) {
Jeff Layton72ca5452008-12-01 07:09:36 -0500782 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000783 return rc;
784 }
Jeff Layton0496e022008-12-30 12:39:16 -0500785 rc = smb_send(ses->server, in_buf, in_buf->smb_buf_length);
Jeff Layton72ca5452008-12-01 07:09:36 -0500786 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000787 return rc;
788}
789
790/* We send a LOCKINGX_CANCEL_LOCK to cause the Windows
791 blocking lock to return. */
792
793static int
794send_lock_cancel(const unsigned int xid, struct cifsTconInfo *tcon,
795 struct smb_hdr *in_buf,
796 struct smb_hdr *out_buf)
797{
798 int bytes_returned;
799 struct cifsSesInfo *ses = tcon->ses;
800 LOCK_REQ *pSMB = (LOCK_REQ *)in_buf;
801
802 /* We just modify the current in_buf to change
803 the type of lock from LOCKING_ANDX_SHARED_LOCK
804 or LOCKING_ANDX_EXCLUSIVE_LOCK to
805 LOCKING_ANDX_CANCEL_LOCK. */
806
807 pSMB->LockType = LOCKING_ANDX_CANCEL_LOCK|LOCKING_ANDX_LARGE_FILES;
808 pSMB->Timeout = 0;
809 pSMB->hdr.Mid = GetNextMid(ses->server);
810
811 return SendReceive(xid, ses, in_buf, out_buf,
Steve French133672e2007-11-13 22:41:37 +0000812 &bytes_returned, CIFS_STD_OP);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000813}
814
815int
816SendReceiveBlockingLock(const unsigned int xid, struct cifsTconInfo *tcon,
817 struct smb_hdr *in_buf, struct smb_hdr *out_buf,
818 int *pbytes_returned)
819{
820 int rc = 0;
821 int rstart = 0;
822 unsigned int receive_len;
823 struct mid_q_entry *midQ;
824 struct cifsSesInfo *ses;
825
826 if (tcon == NULL || tcon->ses == NULL) {
Steve French79a58d12007-07-06 22:44:50 +0000827 cERROR(1, ("Null smb session"));
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000828 return -EIO;
829 }
830 ses = tcon->ses;
831
Steve French79a58d12007-07-06 22:44:50 +0000832 if (ses->server == NULL) {
833 cERROR(1, ("Null tcp session"));
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000834 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 }
836
Steve French79a58d12007-07-06 22:44:50 +0000837 if (ses->server->tcpStatus == CifsExiting)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000838 return -ENOENT;
839
Steve French79a58d12007-07-06 22:44:50 +0000840 /* Ensure that we do not send more than 50 overlapping requests
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000841 to the same server. We may make this configurable later or
842 use ses->maxReq */
843
Volker Lendecke6d9c6d52008-12-08 20:50:24 +0000844 if (in_buf->smb_buf_length > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
845 cERROR(1, ("Illegal length, greater than maximum frame, %d",
846 in_buf->smb_buf_length));
847 return -EIO;
848 }
849
Steve French133672e2007-11-13 22:41:37 +0000850 rc = wait_for_free_request(ses, CIFS_BLOCKING_OP);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000851 if (rc)
852 return rc;
853
Steve French79a58d12007-07-06 22:44:50 +0000854 /* make sure that we sign in the same order that we send on this socket
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000855 and avoid races inside tcp sendmsg code that could cause corruption
856 of smb data */
857
Jeff Layton72ca5452008-12-01 07:09:36 -0500858 mutex_lock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000859
860 rc = allocate_mid(ses, in_buf, &midQ);
861 if (rc) {
Jeff Layton72ca5452008-12-01 07:09:36 -0500862 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000863 return rc;
864 }
865
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000866 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
Volker Lendecke829049c2008-12-06 16:00:53 +0100867 if (rc) {
868 DeleteMidQEntry(midQ);
869 mutex_unlock(&ses->server->srv_mutex);
870 return rc;
871 }
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000872
873 midQ->midState = MID_REQUEST_SUBMITTED;
874#ifdef CONFIG_CIFS_STATS2
875 atomic_inc(&ses->server->inSend);
876#endif
Jeff Layton0496e022008-12-30 12:39:16 -0500877 rc = smb_send(ses->server, in_buf, in_buf->smb_buf_length);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000878#ifdef CONFIG_CIFS_STATS2
879 atomic_dec(&ses->server->inSend);
880 midQ->when_sent = jiffies;
881#endif
Jeff Layton72ca5452008-12-01 07:09:36 -0500882 mutex_unlock(&ses->server->srv_mutex);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000883
Steve French79a58d12007-07-06 22:44:50 +0000884 if (rc < 0) {
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000885 DeleteMidQEntry(midQ);
886 return rc;
887 }
888
889 /* Wait for a reply - allow signals to interrupt. */
890 rc = wait_event_interruptible(ses->server->response_q,
Steve French79a58d12007-07-06 22:44:50 +0000891 (!(midQ->midState == MID_REQUEST_SUBMITTED)) ||
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000892 ((ses->server->tcpStatus != CifsGood) &&
893 (ses->server->tcpStatus != CifsNew)));
894
895 /* Were we interrupted by a signal ? */
896 if ((rc == -ERESTARTSYS) &&
897 (midQ->midState == MID_REQUEST_SUBMITTED) &&
898 ((ses->server->tcpStatus == CifsGood) ||
899 (ses->server->tcpStatus == CifsNew))) {
900
901 if (in_buf->Command == SMB_COM_TRANSACTION2) {
902 /* POSIX lock. We send a NT_CANCEL SMB to cause the
903 blocking lock to return. */
904
905 rc = send_nt_cancel(tcon, in_buf, midQ);
906 if (rc) {
907 DeleteMidQEntry(midQ);
908 return rc;
909 }
910 } else {
911 /* Windows lock. We send a LOCKINGX_CANCEL_LOCK
912 to cause the blocking lock to return. */
913
914 rc = send_lock_cancel(xid, tcon, in_buf, out_buf);
915
916 /* If we get -ENOLCK back the lock may have
917 already been removed. Don't exit in this case. */
918 if (rc && rc != -ENOLCK) {
919 DeleteMidQEntry(midQ);
920 return rc;
921 }
922 }
923
924 /* Wait 5 seconds for the response. */
Steve French79a58d12007-07-06 22:44:50 +0000925 if (wait_for_response(ses, midQ, 5 * HZ, 5 * HZ) == 0) {
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000926 /* We got the response - restart system call. */
927 rstart = 1;
928 }
929 }
930
931 spin_lock(&GlobalMid_Lock);
932 if (midQ->resp_buf) {
933 spin_unlock(&GlobalMid_Lock);
934 receive_len = midQ->resp_buf->smb_buf_length;
935 } else {
Steve French79a58d12007-07-06 22:44:50 +0000936 cERROR(1, ("No response for cmd %d mid %d",
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000937 midQ->command, midQ->mid));
Steve French79a58d12007-07-06 22:44:50 +0000938 if (midQ->midState == MID_REQUEST_SUBMITTED) {
939 if (ses->server->tcpStatus == CifsExiting)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000940 rc = -EHOSTDOWN;
941 else {
942 ses->server->tcpStatus = CifsNeedReconnect;
943 midQ->midState = MID_RETRY_NEEDED;
944 }
945 }
946
947 if (rc != -EHOSTDOWN) {
Steve French79a58d12007-07-06 22:44:50 +0000948 if (midQ->midState == MID_RETRY_NEEDED) {
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000949 rc = -EAGAIN;
Steve French79a58d12007-07-06 22:44:50 +0000950 cFYI(1, ("marking request for retry"));
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000951 } else {
952 rc = -EIO;
953 }
954 }
955 spin_unlock(&GlobalMid_Lock);
956 DeleteMidQEntry(midQ);
957 return rc;
958 }
Steve French50c2f752007-07-13 00:33:32 +0000959
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000960 if (receive_len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE) {
961 cERROR(1, ("Frame too large received. Length: %d Xid: %d",
962 receive_len, xid));
963 rc = -EIO;
Volker Lendecke17c8bfe2008-12-06 16:38:19 +0100964 goto out;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000965 }
Volker Lendecke17c8bfe2008-12-06 16:38:19 +0100966
967 /* rcvd frame is ok */
968
Volker Lendeckeac6a3ef2008-12-06 16:40:40 +0100969 if ((out_buf == NULL) || (midQ->midState != MID_RESPONSE_RECEIVED)) {
Volker Lendecke17c8bfe2008-12-06 16:38:19 +0100970 rc = -EIO;
971 cERROR(1, ("Bad MID state?"));
Volker Lendecke698e96a2008-12-06 16:39:31 +0100972 goto out;
Volker Lendecke17c8bfe2008-12-06 16:38:19 +0100973 }
974
Volker Lendecke698e96a2008-12-06 16:39:31 +0100975 out_buf->smb_buf_length = receive_len;
976 memcpy((char *)out_buf + 4,
977 (char *)midQ->resp_buf + 4,
978 receive_len);
979
980 dump_smb(out_buf, 92);
981 /* convert the length into a more usable form */
982 if ((receive_len > 24) &&
983 (ses->server->secMode & (SECMODE_SIGN_REQUIRED |
984 SECMODE_SIGN_ENABLED))) {
985 rc = cifs_verify_signature(out_buf,
986 &ses->server->mac_signing_key,
987 midQ->sequence_number+1);
988 if (rc) {
989 cERROR(1, ("Unexpected SMB signature"));
990 /* BB FIXME add code to kill session */
991 }
992 }
993
994 *pbytes_returned = out_buf->smb_buf_length;
995
996 /* BB special case reconnect tid and uid here? */
997 rc = map_smb_to_linux_error(out_buf, 0 /* no log */ );
998
999 /* convert ByteCount if necessary */
1000 if (receive_len >= sizeof(struct smb_hdr) - 4
1001 /* do not count RFC1001 header */ +
1002 (2 * out_buf->WordCount) + 2 /* bcc */ )
1003 BCC(out_buf) = le16_to_cpu(BCC_LE(out_buf));
1004
Volker Lendecke17c8bfe2008-12-06 16:38:19 +01001005out:
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001006 DeleteMidQEntry(midQ);
1007 if (rstart && rc == -EACCES)
1008 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 return rc;
1010}