blob: 746bc9405db15ef8e8d7a3a01f50ca21b5337db7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/cifs/transport.c
3 *
Steve French79a58d12007-07-06 22:44:50 +00004 * Copyright (C) International Business Machines Corp., 2002,2007
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>
25#include <linux/wait.h>
26#include <linux/net.h>
27#include <linux/delay.h>
28#include <asm/uaccess.h>
29#include <asm/processor.h>
30#include <linux/mempool.h>
31#include "cifspdu.h"
32#include "cifsglob.h"
33#include "cifsproto.h"
34#include "cifs_debug.h"
Steve French50c2f752007-07-13 00:33:32 +000035
Linus Torvalds1da177e2005-04-16 15:20:36 -070036extern mempool_t *cifs_mid_poolp;
Christoph Lametere18b8902006-12-06 20:33:20 -080037extern struct kmem_cache *cifs_oplock_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39static struct mid_q_entry *
Jeremy Allison7ee1af72006-08-02 21:56:33 +000040AllocMidQEntry(const struct smb_hdr *smb_buffer, struct cifsSesInfo *ses)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
42 struct mid_q_entry *temp;
43
44 if (ses == NULL) {
Steve French275cde12005-04-28 22:41:10 -070045 cERROR(1, ("Null session passed in to AllocMidQEntry"));
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 return NULL;
47 }
48 if (ses->server == NULL) {
49 cERROR(1, ("Null TCP session in AllocMidQEntry"));
50 return NULL;
51 }
Steve French50c2f752007-07-13 00:33:32 +000052
Steve Frenchd6e04ae2005-06-13 13:24:43 -050053 temp = (struct mid_q_entry *) mempool_alloc(cifs_mid_poolp,
Christoph Lametere94b1762006-12-06 20:33:17 -080054 GFP_KERNEL | GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 if (temp == NULL)
56 return temp;
57 else {
58 memset(temp, 0, sizeof (struct mid_q_entry));
59 temp->mid = smb_buffer->Mid; /* always LE */
60 temp->pid = current->pid;
61 temp->command = smb_buffer->Command;
62 cFYI(1, ("For smb_command %d", temp->command));
Steve French1047abc2005-10-11 19:58:06 -070063 /* do_gettimeofday(&temp->when_sent);*/ /* easier to use jiffies */
64 /* when mid allocated can be before when sent */
65 temp->when_alloc = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 temp->ses = ses;
67 temp->tsk = current;
68 }
69
70 spin_lock(&GlobalMid_Lock);
71 list_add_tail(&temp->qhead, &ses->server->pending_mid_q);
72 atomic_inc(&midCount);
73 temp->midState = MID_REQUEST_ALLOCATED;
74 spin_unlock(&GlobalMid_Lock);
75 return temp;
76}
77
78static void
79DeleteMidQEntry(struct mid_q_entry *midEntry)
80{
Steve French1047abc2005-10-11 19:58:06 -070081#ifdef CONFIG_CIFS_STATS2
82 unsigned long now;
83#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 spin_lock(&GlobalMid_Lock);
85 midEntry->midState = MID_FREE;
86 list_del(&midEntry->qhead);
87 atomic_dec(&midCount);
88 spin_unlock(&GlobalMid_Lock);
Steve French79a58d12007-07-06 22:44:50 +000089 if (midEntry->largeBuf)
Steve Frenchb8643e12005-04-28 22:41:07 -070090 cifs_buf_release(midEntry->resp_buf);
91 else
92 cifs_small_buf_release(midEntry->resp_buf);
Steve French1047abc2005-10-11 19:58:06 -070093#ifdef CONFIG_CIFS_STATS2
94 now = jiffies;
95 /* commands taking longer than one second are indications that
96 something is wrong, unless it is quite a slow link or server */
Steve French79a58d12007-07-06 22:44:50 +000097 if ((now - midEntry->when_alloc) > HZ) {
98 if ((cifsFYI & CIFS_TIMER) &&
Steve French1047abc2005-10-11 19:58:06 -070099 (midEntry->command != SMB_COM_LOCKING_ANDX)) {
100 printk(KERN_DEBUG " CIFS slow rsp: cmd %d mid %d",
101 midEntry->command, midEntry->mid);
102 printk(" A: 0x%lx S: 0x%lx R: 0x%lx\n",
103 now - midEntry->when_alloc,
104 now - midEntry->when_sent,
105 now - midEntry->when_received);
106 }
107 }
108#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 mempool_free(midEntry, cifs_mid_poolp);
110}
111
112struct oplock_q_entry *
Steve French79a58d12007-07-06 22:44:50 +0000113AllocOplockQEntry(struct inode *pinode, __u16 fid, struct cifsTconInfo *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
115 struct oplock_q_entry *temp;
Steve French79a58d12007-07-06 22:44:50 +0000116 if ((pinode == NULL) || (tcon == NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 cERROR(1, ("Null parms passed to AllocOplockQEntry"));
118 return NULL;
119 }
120 temp = (struct oplock_q_entry *) kmem_cache_alloc(cifs_oplock_cachep,
Christoph Lametere94b1762006-12-06 20:33:17 -0800121 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 if (temp == NULL)
123 return temp;
124 else {
125 temp->pinode = pinode;
126 temp->tcon = tcon;
127 temp->netfid = fid;
128 spin_lock(&GlobalMid_Lock);
129 list_add_tail(&temp->qhead, &GlobalOplock_Q);
130 spin_unlock(&GlobalMid_Lock);
131 }
132 return temp;
133
134}
135
Steve French79a58d12007-07-06 22:44:50 +0000136void DeleteOplockQEntry(struct oplock_q_entry *oplockEntry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
Steve French79a58d12007-07-06 22:44:50 +0000138 spin_lock(&GlobalMid_Lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 /* should we check if list empty first? */
140 list_del(&oplockEntry->qhead);
141 spin_unlock(&GlobalMid_Lock);
142 kmem_cache_free(cifs_oplock_cachep, oplockEntry);
143}
144
145int
146smb_send(struct socket *ssocket, struct smb_hdr *smb_buffer,
147 unsigned int smb_buf_length, struct sockaddr *sin)
148{
149 int rc = 0;
150 int i = 0;
151 struct msghdr smb_msg;
152 struct kvec iov;
153 unsigned len = smb_buf_length + 4;
154
Steve French79a58d12007-07-06 22:44:50 +0000155 if (ssocket == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 return -ENOTSOCK; /* BB eventually add reconnect code here */
157 iov.iov_base = smb_buffer;
158 iov.iov_len = len;
159
160 smb_msg.msg_name = sin;
161 smb_msg.msg_namelen = sizeof (struct sockaddr);
162 smb_msg.msg_control = NULL;
163 smb_msg.msg_controllen = 0;
164 smb_msg.msg_flags = MSG_DONTWAIT + MSG_NOSIGNAL; /* BB add more flags?*/
165
166 /* smb header is converted in header_assemble. bcc and rest of SMB word
Steve French79a58d12007-07-06 22:44:50 +0000167 area, and byte area if necessary, is converted to littleendian in
168 cifssmb.c and RFC1001 len is converted to bigendian in smb_send
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 Flags2 is converted in SendReceive */
170
171 smb_buffer->smb_buf_length = cpu_to_be32(smb_buffer->smb_buf_length);
Steve French3e844692005-10-03 13:37:24 -0700172 cFYI(1, ("Sending smb of length %d", smb_buf_length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 dump_smb(smb_buffer, len);
174
175 while (len > 0) {
176 rc = kernel_sendmsg(ssocket, &smb_msg, &iov, 1, len);
177 if ((rc == -ENOSPC) || (rc == -EAGAIN)) {
178 i++;
Steve French3e844692005-10-03 13:37:24 -0700179 /* smaller timeout here than send2 since smaller size */
Steve French79a58d12007-07-06 22:44:50 +0000180 /* Although it may not be required, this also is smaller
181 oplock break time */
182 if (i > 12) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 cERROR(1,
Steve French68058e72005-10-10 10:34:22 -0700184 ("sends on sock %p stuck for 7 seconds",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 ssocket));
186 rc = -EAGAIN;
187 break;
188 }
Steve French68058e72005-10-10 10:34:22 -0700189 msleep(1 << i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 continue;
191 }
Steve French79a58d12007-07-06 22:44:50 +0000192 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 break;
Steve French5e1253b2005-10-10 14:06:37 -0700194 else
195 i = 0; /* reset i after each successful send */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 iov.iov_base += rc;
197 iov.iov_len -= rc;
198 len -= rc;
199 }
200
201 if (rc < 0) {
Steve French79a58d12007-07-06 22:44:50 +0000202 cERROR(1, ("Error %d sending data on socket to server", rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 } else {
204 rc = 0;
205 }
206
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000207 /* Don't want to modify the buffer as a
208 side effect of this call. */
209 smb_buffer->smb_buf_length = smb_buf_length;
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 return rc;
212}
213
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500214static int
Steve French3e844692005-10-03 13:37:24 -0700215smb_send2(struct socket *ssocket, struct kvec *iov, int n_vec,
216 struct sockaddr *sin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
218 int rc = 0;
219 int i = 0;
220 struct msghdr smb_msg;
Steve French3e844692005-10-03 13:37:24 -0700221 struct smb_hdr *smb_buffer = iov[0].iov_base;
222 unsigned int len = iov[0].iov_len;
223 unsigned int total_len;
224 int first_vec = 0;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000225 unsigned int smb_buf_length = smb_buffer->smb_buf_length;
Steve French50c2f752007-07-13 00:33:32 +0000226
Steve French79a58d12007-07-06 22:44:50 +0000227 if (ssocket == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 return -ENOTSOCK; /* BB eventually add reconnect code here */
Steve French3e844692005-10-03 13:37:24 -0700229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 smb_msg.msg_name = sin;
231 smb_msg.msg_namelen = sizeof (struct sockaddr);
232 smb_msg.msg_control = NULL;
233 smb_msg.msg_controllen = 0;
234 smb_msg.msg_flags = MSG_DONTWAIT + MSG_NOSIGNAL; /* BB add more flags?*/
235
236 /* smb header is converted in header_assemble. bcc and rest of SMB word
Steve French79a58d12007-07-06 22:44:50 +0000237 area, and byte area if necessary, is converted to littleendian in
238 cifssmb.c and RFC1001 len is converted to bigendian in smb_send
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 Flags2 is converted in SendReceive */
240
Steve French3e844692005-10-03 13:37:24 -0700241
242 total_len = 0;
243 for (i = 0; i < n_vec; i++)
244 total_len += iov[i].iov_len;
245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 smb_buffer->smb_buf_length = cpu_to_be32(smb_buffer->smb_buf_length);
Steve French3e844692005-10-03 13:37:24 -0700247 cFYI(1, ("Sending smb: total_len %d", total_len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 dump_smb(smb_buffer, len);
249
Steve French3e844692005-10-03 13:37:24 -0700250 while (total_len) {
251 rc = kernel_sendmsg(ssocket, &smb_msg, &iov[first_vec],
252 n_vec - first_vec, total_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 if ((rc == -ENOSPC) || (rc == -EAGAIN)) {
254 i++;
Steve French79a58d12007-07-06 22:44:50 +0000255 if (i >= 14) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 cERROR(1,
Steve French68058e72005-10-10 10:34:22 -0700257 ("sends on sock %p stuck for 15 seconds",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 ssocket));
259 rc = -EAGAIN;
260 break;
261 }
Steve French68058e72005-10-10 10:34:22 -0700262 msleep(1 << i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 continue;
264 }
Steve French79a58d12007-07-06 22:44:50 +0000265 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 break;
Steve French3e844692005-10-03 13:37:24 -0700267
268 if (rc >= total_len) {
269 WARN_ON(rc > total_len);
270 break;
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500271 }
Steve French79a58d12007-07-06 22:44:50 +0000272 if (rc == 0) {
Steve French3e844692005-10-03 13:37:24 -0700273 /* should never happen, letting socket clear before
274 retrying is our only obvious option here */
Steve French79a58d12007-07-06 22:44:50 +0000275 cERROR(1, ("tcp sent no data"));
Steve French3e844692005-10-03 13:37:24 -0700276 msleep(500);
277 continue;
278 }
279 total_len -= rc;
Steve French68058e72005-10-10 10:34:22 -0700280 /* the line below resets i */
Steve French3e844692005-10-03 13:37:24 -0700281 for (i = first_vec; i < n_vec; i++) {
282 if (iov[i].iov_len) {
283 if (rc > iov[i].iov_len) {
284 rc -= iov[i].iov_len;
285 iov[i].iov_len = 0;
286 } else {
287 iov[i].iov_base += rc;
288 iov[i].iov_len -= rc;
289 first_vec = i;
290 break;
291 }
292 }
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500293 }
Steve French5e1253b2005-10-10 14:06:37 -0700294 i = 0; /* in case we get ENOSPC on the next send */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 }
296
297 if (rc < 0) {
Steve French79a58d12007-07-06 22:44:50 +0000298 cERROR(1, ("Error %d sending data on socket to server", rc));
Steve French3e844692005-10-03 13:37:24 -0700299 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000302 /* Don't want to modify the buffer as a
303 side effect of this call. */
304 smb_buffer->smb_buf_length = smb_buf_length;
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 return rc;
307}
308
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000309static int wait_for_free_request(struct cifsSesInfo *ses, const int long_op)
310{
Steve French79a58d12007-07-06 22:44:50 +0000311 if (long_op == -1) {
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000312 /* oplock breaks must not be held up */
313 atomic_inc(&ses->server->inFlight);
314 } else {
Steve French79a58d12007-07-06 22:44:50 +0000315 spin_lock(&GlobalMid_Lock);
316 while (1) {
317 if (atomic_read(&ses->server->inFlight) >=
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000318 cifs_max_pending){
319 spin_unlock(&GlobalMid_Lock);
320#ifdef CONFIG_CIFS_STATS2
321 atomic_inc(&ses->server->num_waiters);
322#endif
323 wait_event(ses->server->request_q,
324 atomic_read(&ses->server->inFlight)
325 < cifs_max_pending);
326#ifdef CONFIG_CIFS_STATS2
327 atomic_dec(&ses->server->num_waiters);
328#endif
329 spin_lock(&GlobalMid_Lock);
330 } else {
Steve French79a58d12007-07-06 22:44:50 +0000331 if (ses->server->tcpStatus == CifsExiting) {
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000332 spin_unlock(&GlobalMid_Lock);
333 return -ENOENT;
334 }
335
Steve French79a58d12007-07-06 22:44:50 +0000336 /* can not count locking commands against total
337 as they are allowed to block on server */
Steve French50c2f752007-07-13 00:33:32 +0000338
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000339 /* update # of requests on the wire to server */
340 if (long_op < 3)
341 atomic_inc(&ses->server->inFlight);
342 spin_unlock(&GlobalMid_Lock);
343 break;
344 }
345 }
346 }
347 return 0;
348}
349
350static int allocate_mid(struct cifsSesInfo *ses, struct smb_hdr *in_buf,
351 struct mid_q_entry **ppmidQ)
352{
353 if (ses->server->tcpStatus == CifsExiting) {
354 return -ENOENT;
355 } else if (ses->server->tcpStatus == CifsNeedReconnect) {
Steve French79a58d12007-07-06 22:44:50 +0000356 cFYI(1, ("tcp session dead - return to caller to retry"));
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000357 return -EAGAIN;
358 } else if (ses->status != CifsGood) {
359 /* check if SMB session is bad because we are setting it up */
Steve French79a58d12007-07-06 22:44:50 +0000360 if ((in_buf->Command != SMB_COM_SESSION_SETUP_ANDX) &&
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000361 (in_buf->Command != SMB_COM_NEGOTIATE)) {
362 return -EAGAIN;
363 } /* else ok - we are setting up session */
364 }
365 *ppmidQ = AllocMidQEntry(in_buf, ses);
366 if (*ppmidQ == NULL) {
367 return -ENOMEM;
368 }
369 return 0;
370}
371
Steve French79a58d12007-07-06 22:44:50 +0000372static int wait_for_response(struct cifsSesInfo *ses,
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000373 struct mid_q_entry *midQ,
374 unsigned long timeout,
375 unsigned long time_to_wait)
376{
377 unsigned long curr_timeout;
378
379 for (;;) {
380 curr_timeout = timeout + jiffies;
381 wait_event(ses->server->response_q,
Steve French79a58d12007-07-06 22:44:50 +0000382 (!(midQ->midState == MID_REQUEST_SUBMITTED)) ||
383 time_after(jiffies, curr_timeout) ||
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000384 ((ses->server->tcpStatus != CifsGood) &&
385 (ses->server->tcpStatus != CifsNew)));
386
387 if (time_after(jiffies, curr_timeout) &&
388 (midQ->midState == MID_REQUEST_SUBMITTED) &&
389 ((ses->server->tcpStatus == CifsGood) ||
390 (ses->server->tcpStatus == CifsNew))) {
391
392 unsigned long lrt;
393
394 /* We timed out. Is the server still
395 sending replies ? */
396 spin_lock(&GlobalMid_Lock);
397 lrt = ses->server->lstrp;
398 spin_unlock(&GlobalMid_Lock);
399
400 /* Calculate time_to_wait past last receive time.
Steve French79a58d12007-07-06 22:44:50 +0000401 Although we prefer not to time out if the
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000402 server is still responding - we will time
Steve French79a58d12007-07-06 22:44:50 +0000403 out if the server takes more than 15 (or 45
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000404 or 180) seconds to respond to this request
Steve French79a58d12007-07-06 22:44:50 +0000405 and has not responded to any request from
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000406 other threads on the client within 10 seconds */
407 lrt += time_to_wait;
408 if (time_after(jiffies, lrt)) {
409 /* No replies for time_to_wait. */
Steve French79a58d12007-07-06 22:44:50 +0000410 cERROR(1, ("server not responding"));
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000411 return -1;
412 }
413 } else {
414 return 0;
415 }
416 }
417}
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419int
Steve French79a58d12007-07-06 22:44:50 +0000420SendReceive2(const unsigned int xid, struct cifsSesInfo *ses,
421 struct kvec *iov, int n_vec, int *pRespBufType /* ret */,
Steve French3e844692005-10-03 13:37:24 -0700422 const int long_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
424 int rc = 0;
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500425 unsigned int receive_len;
426 unsigned long timeout;
427 struct mid_q_entry *midQ;
Steve French3e844692005-10-03 13:37:24 -0700428 struct smb_hdr *in_buf = iov[0].iov_base;
Steve French50c2f752007-07-13 00:33:32 +0000429
Steve Frenchec637e32005-12-12 20:53:18 -0800430 *pRespBufType = CIFS_NO_BUFFER; /* no response buf yet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Steve French4b8f9302006-02-26 16:41:18 +0000432 if ((ses == NULL) || (ses->server == NULL)) {
433 cifs_small_buf_release(in_buf);
Steve French79a58d12007-07-06 22:44:50 +0000434 cERROR(1, ("Null session"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 return -EIO;
436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Steve French79a58d12007-07-06 22:44:50 +0000438 if (ses->server->tcpStatus == CifsExiting) {
Steve French4b8f9302006-02-26 16:41:18 +0000439 cifs_small_buf_release(in_buf);
Steve French31ca3bc2005-04-28 22:41:11 -0700440 return -ENOENT;
Steve French4b8f9302006-02-26 16:41:18 +0000441 }
Steve French31ca3bc2005-04-28 22:41:11 -0700442
Steve French79a58d12007-07-06 22:44:50 +0000443 /* Ensure that we do not send more than 50 overlapping requests
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 to the same server. We may make this configurable later or
445 use ses->maxReq */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000447 rc = wait_for_free_request(ses, long_op);
448 if (rc) {
449 cifs_small_buf_release(in_buf);
450 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 }
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000452
Steve French79a58d12007-07-06 22:44:50 +0000453 /* make sure that we sign in the same order that we send on this socket
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 and avoid races inside tcp sendmsg code that could cause corruption
455 of smb data */
456
Steve French79a58d12007-07-06 22:44:50 +0000457 down(&ses->server->tcpSem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000459 rc = allocate_mid(ses, in_buf, &midQ);
460 if (rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 up(&ses->server->tcpSem);
Steve French4b8f9302006-02-26 16:41:18 +0000462 cifs_small_buf_release(in_buf);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000463 /* Update # of requests on wire to server */
Steve French79a58d12007-07-06 22:44:50 +0000464 atomic_dec(&ses->server->inFlight);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000465 wake_up(&ses->server->request_q);
466 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 }
468
Steve French79a58d12007-07-06 22:44:50 +0000469 rc = cifs_sign_smb2(iov, n_vec, ses->server, &midQ->sequence_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 midQ->midState = MID_REQUEST_SUBMITTED;
Steve French131afd0b2005-10-07 09:51:05 -0700472#ifdef CONFIG_CIFS_STATS2
473 atomic_inc(&ses->server->inSend);
474#endif
Steve French3e844692005-10-03 13:37:24 -0700475 rc = smb_send2(ses->server->ssocket, iov, n_vec,
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500476 (struct sockaddr *) &(ses->server->addr.sockAddr));
Steve French131afd0b2005-10-07 09:51:05 -0700477#ifdef CONFIG_CIFS_STATS2
478 atomic_dec(&ses->server->inSend);
Steve French1047abc2005-10-11 19:58:06 -0700479 midQ->when_sent = jiffies;
Steve French131afd0b2005-10-07 09:51:05 -0700480#endif
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000481
482 up(&ses->server->tcpSem);
483 cifs_small_buf_release(in_buf);
484
Steve French79a58d12007-07-06 22:44:50 +0000485 if (rc < 0)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000486 goto out;
Steve French4b8f9302006-02-26 16:41:18 +0000487
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500488 if (long_op == -1)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000489 goto out;
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500490 else if (long_op == 2) /* writes past end of file can take loong time */
Steve French37c0eb42005-10-05 14:50:29 -0700491 timeout = 180 * HZ;
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500492 else if (long_op == 1)
Steve French79a58d12007-07-06 22:44:50 +0000493 timeout = 45 * HZ; /* should be greater than
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500494 servers oplock break timeout (about 43 seconds) */
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000495 else
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500496 timeout = 15 * HZ;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000497
Steve French79a58d12007-07-06 22:44:50 +0000498 /* wait for 15 seconds or until woken up due to response arriving or
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500499 due to last connection to this server being unmounted */
500 if (signal_pending(current)) {
501 /* if signal pending do not hold up user for full smb timeout
Steve French8a236262007-03-06 00:31:00 +0000502 but we still give response a chance to complete */
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500503 timeout = 2 * HZ;
Steve French79a58d12007-07-06 22:44:50 +0000504 }
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500505
506 /* No user interrupts in wait - wreaks havoc with performance */
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000507 wait_for_response(ses, midQ, timeout, 10 * HZ);
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500508
509 spin_lock(&GlobalMid_Lock);
510 if (midQ->resp_buf) {
511 spin_unlock(&GlobalMid_Lock);
Steve French70ca7342005-09-22 16:32:06 -0700512 receive_len = midQ->resp_buf->smb_buf_length;
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500513 } else {
Steve French79a58d12007-07-06 22:44:50 +0000514 cERROR(1, ("No response to cmd %d mid %d",
Steve French37c0eb42005-10-05 14:50:29 -0700515 midQ->command, midQ->mid));
Steve French79a58d12007-07-06 22:44:50 +0000516 if (midQ->midState == MID_REQUEST_SUBMITTED) {
517 if (ses->server->tcpStatus == CifsExiting)
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500518 rc = -EHOSTDOWN;
519 else {
520 ses->server->tcpStatus = CifsNeedReconnect;
521 midQ->midState = MID_RETRY_NEEDED;
522 }
523 }
524
525 if (rc != -EHOSTDOWN) {
Steve French79a58d12007-07-06 22:44:50 +0000526 if (midQ->midState == MID_RETRY_NEEDED) {
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500527 rc = -EAGAIN;
Steve French79a58d12007-07-06 22:44:50 +0000528 cFYI(1, ("marking request for retry"));
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500529 } else {
530 rc = -EIO;
531 }
532 }
533 spin_unlock(&GlobalMid_Lock);
534 DeleteMidQEntry(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000535 /* Update # of requests on wire to server */
Steve French79a58d12007-07-06 22:44:50 +0000536 atomic_dec(&ses->server->inFlight);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000537 wake_up(&ses->server->request_q);
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500538 return rc;
539 }
Steve French50c2f752007-07-13 00:33:32 +0000540
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500541 if (receive_len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE) {
542 cERROR(1, ("Frame too large received. Length: %d Xid: %d",
543 receive_len, xid));
544 rc = -EIO;
545 } else { /* rcvd frame is ok */
Steve French79a58d12007-07-06 22:44:50 +0000546 if (midQ->resp_buf &&
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500547 (midQ->midState == MID_RESPONSE_RECEIVED)) {
Steve French84afc292005-12-02 13:32:45 -0800548
Steve Frenchec637e32005-12-12 20:53:18 -0800549 iov[0].iov_base = (char *)midQ->resp_buf;
Steve French79a58d12007-07-06 22:44:50 +0000550 if (midQ->largeBuf)
Steve Frenchec637e32005-12-12 20:53:18 -0800551 *pRespBufType = CIFS_LARGE_BUFFER;
552 else
553 *pRespBufType = CIFS_SMALL_BUFFER;
554 iov[0].iov_len = receive_len + 4;
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500555
Steve Frenchec637e32005-12-12 20:53:18 -0800556 dump_smb(midQ->resp_buf, 80);
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500557 /* convert the length into a more usable form */
Steve French79a58d12007-07-06 22:44:50 +0000558 if ((receive_len > 24) &&
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500559 (ses->server->secMode & (SECMODE_SIGN_REQUIRED |
560 SECMODE_SIGN_ENABLED))) {
Steve Frenchec637e32005-12-12 20:53:18 -0800561 rc = cifs_verify_signature(midQ->resp_buf,
Steve Frenchb609f062007-07-09 07:55:14 +0000562 &ses->server->mac_signing_key,
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500563 midQ->sequence_number+1);
Steve French79a58d12007-07-06 22:44:50 +0000564 if (rc) {
565 cERROR(1, ("Unexpected SMB signature"));
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500566 /* BB FIXME add code to kill session */
567 }
568 }
569
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500570 /* BB special case reconnect tid and uid here? */
Steve French6ab16d22005-11-29 20:55:11 -0800571 /* BB special case Errbadpassword and pwdexpired here */
Steve Frenchec637e32005-12-12 20:53:18 -0800572 rc = map_smb_to_linux_error(midQ->resp_buf);
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500573
574 /* convert ByteCount if necessary */
575 if (receive_len >=
576 sizeof (struct smb_hdr) -
577 4 /* do not count RFC1001 header */ +
Steve Frenchec637e32005-12-12 20:53:18 -0800578 (2 * midQ->resp_buf->WordCount) + 2 /* bcc */ )
Steve French79a58d12007-07-06 22:44:50 +0000579 BCC(midQ->resp_buf) =
Steve Frenchec637e32005-12-12 20:53:18 -0800580 le16_to_cpu(BCC_LE(midQ->resp_buf));
581 midQ->resp_buf = NULL; /* mark it so will not be freed
582 by DeleteMidQEntry */
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500583 } else {
584 rc = -EIO;
Steve French79a58d12007-07-06 22:44:50 +0000585 cFYI(1, ("Bad MID state?"));
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500586 }
587 }
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000588
589out:
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500590 DeleteMidQEntry(midQ);
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 return rc;
595}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597int
598SendReceive(const unsigned int xid, struct cifsSesInfo *ses,
599 struct smb_hdr *in_buf, struct smb_hdr *out_buf,
600 int *pbytes_returned, const int long_op)
601{
602 int rc = 0;
603 unsigned int receive_len;
604 unsigned long timeout;
605 struct mid_q_entry *midQ;
606
607 if (ses == NULL) {
Steve French79a58d12007-07-06 22:44:50 +0000608 cERROR(1, ("Null smb session"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 return -EIO;
610 }
Steve French79a58d12007-07-06 22:44:50 +0000611 if (ses->server == NULL) {
612 cERROR(1, ("Null tcp session"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 return -EIO;
614 }
615
Steve French79a58d12007-07-06 22:44:50 +0000616 if (ses->server->tcpStatus == CifsExiting)
Steve French31ca3bc2005-04-28 22:41:11 -0700617 return -ENOENT;
618
Steve French79a58d12007-07-06 22:44:50 +0000619 /* Ensure that we do not send more than 50 overlapping requests
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 to the same server. We may make this configurable later or
621 use ses->maxReq */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000623 rc = wait_for_free_request(ses, long_op);
624 if (rc)
625 return rc;
626
Steve French79a58d12007-07-06 22:44:50 +0000627 /* make sure that we sign in the same order that we send on this socket
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 and avoid races inside tcp sendmsg code that could cause corruption
629 of smb data */
630
Steve French79a58d12007-07-06 22:44:50 +0000631 down(&ses->server->tcpSem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000633 rc = allocate_mid(ses, in_buf, &midQ);
634 if (rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 up(&ses->server->tcpSem);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000636 /* Update # of requests on wire to server */
Steve French79a58d12007-07-06 22:44:50 +0000637 atomic_dec(&ses->server->inFlight);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000638 wake_up(&ses->server->request_q);
639 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 }
641
642 if (in_buf->smb_buf_length > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
Steve French26a21b92006-05-31 18:05:34 +0000643 cERROR(1, ("Illegal length, greater than maximum frame, %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 in_buf->smb_buf_length));
645 DeleteMidQEntry(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000646 up(&ses->server->tcpSem);
647 /* Update # of requests on wire to server */
Steve French79a58d12007-07-06 22:44:50 +0000648 atomic_dec(&ses->server->inFlight);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000649 wake_up(&ses->server->request_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 return -EIO;
651 }
652
Steve Frenchad009ac2005-04-28 22:41:05 -0700653 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 midQ->midState = MID_REQUEST_SUBMITTED;
Steve French131afd0b2005-10-07 09:51:05 -0700656#ifdef CONFIG_CIFS_STATS2
657 atomic_inc(&ses->server->inSend);
658#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 rc = smb_send(ses->server->ssocket, in_buf, in_buf->smb_buf_length,
660 (struct sockaddr *) &(ses->server->addr.sockAddr));
Steve French131afd0b2005-10-07 09:51:05 -0700661#ifdef CONFIG_CIFS_STATS2
662 atomic_dec(&ses->server->inSend);
Steve French1047abc2005-10-11 19:58:06 -0700663 midQ->when_sent = jiffies;
Steve French131afd0b2005-10-07 09:51:05 -0700664#endif
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000665 up(&ses->server->tcpSem);
666
Steve French79a58d12007-07-06 22:44:50 +0000667 if (rc < 0)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000668 goto out;
669
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 if (long_op == -1)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000671 goto out;
Steve French275cde12005-04-28 22:41:10 -0700672 else if (long_op == 2) /* writes past end of file can take loong time */
Steve French37c0eb42005-10-05 14:50:29 -0700673 timeout = 180 * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 else if (long_op == 1)
Steve French79a58d12007-07-06 22:44:50 +0000675 timeout = 45 * HZ; /* should be greater than
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 servers oplock break timeout (about 43 seconds) */
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000677 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 timeout = 15 * HZ;
Steve French79a58d12007-07-06 22:44:50 +0000679 /* wait for 15 seconds or until woken up due to response arriving or
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 due to last connection to this server being unmounted */
681 if (signal_pending(current)) {
682 /* if signal pending do not hold up user for full smb timeout
Steve French8a236262007-03-06 00:31:00 +0000683 but we still give response a chance to complete */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 timeout = 2 * HZ;
Steve French79a58d12007-07-06 22:44:50 +0000685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 /* No user interrupts in wait - wreaks havoc with performance */
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000688 wait_for_response(ses, midQ, timeout, 10 * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 spin_lock(&GlobalMid_Lock);
691 if (midQ->resp_buf) {
692 spin_unlock(&GlobalMid_Lock);
Steve French70ca7342005-09-22 16:32:06 -0700693 receive_len = midQ->resp_buf->smb_buf_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 } else {
Steve French79a58d12007-07-06 22:44:50 +0000695 cERROR(1, ("No response for cmd %d mid %d",
Steve French37c0eb42005-10-05 14:50:29 -0700696 midQ->command, midQ->mid));
Steve French79a58d12007-07-06 22:44:50 +0000697 if (midQ->midState == MID_REQUEST_SUBMITTED) {
698 if (ses->server->tcpStatus == CifsExiting)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 rc = -EHOSTDOWN;
700 else {
701 ses->server->tcpStatus = CifsNeedReconnect;
702 midQ->midState = MID_RETRY_NEEDED;
703 }
704 }
705
706 if (rc != -EHOSTDOWN) {
Steve French79a58d12007-07-06 22:44:50 +0000707 if (midQ->midState == MID_RETRY_NEEDED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 rc = -EAGAIN;
Steve French79a58d12007-07-06 22:44:50 +0000709 cFYI(1, ("marking request for retry"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 } else {
711 rc = -EIO;
712 }
713 }
714 spin_unlock(&GlobalMid_Lock);
715 DeleteMidQEntry(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000716 /* Update # of requests on wire to server */
Steve French79a58d12007-07-06 22:44:50 +0000717 atomic_dec(&ses->server->inFlight);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000718 wake_up(&ses->server->request_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 return rc;
720 }
Steve French50c2f752007-07-13 00:33:32 +0000721
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 if (receive_len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE) {
Steve Frenchad009ac2005-04-28 22:41:05 -0700723 cERROR(1, ("Frame too large received. Length: %d Xid: %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 receive_len, xid));
725 rc = -EIO;
726 } else { /* rcvd frame is ok */
727
728 if (midQ->resp_buf && out_buf
729 && (midQ->midState == MID_RESPONSE_RECEIVED)) {
730 out_buf->smb_buf_length = receive_len;
731 memcpy((char *)out_buf + 4,
732 (char *)midQ->resp_buf + 4,
733 receive_len);
734
735 dump_smb(out_buf, 92);
736 /* convert the length into a more usable form */
Steve French79a58d12007-07-06 22:44:50 +0000737 if ((receive_len > 24) &&
Steve Frenchad009ac2005-04-28 22:41:05 -0700738 (ses->server->secMode & (SECMODE_SIGN_REQUIRED |
739 SECMODE_SIGN_ENABLED))) {
740 rc = cifs_verify_signature(out_buf,
Steve Frenchb609f062007-07-09 07:55:14 +0000741 &ses->server->mac_signing_key,
Steve Frenchad009ac2005-04-28 22:41:05 -0700742 midQ->sequence_number+1);
Steve French79a58d12007-07-06 22:44:50 +0000743 if (rc) {
744 cERROR(1, ("Unexpected SMB signature"));
Steve French275cde12005-04-28 22:41:10 -0700745 /* BB FIXME add code to kill session */
Steve Frenchad009ac2005-04-28 22:41:05 -0700746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 }
748
749 *pbytes_returned = out_buf->smb_buf_length;
750
Steve Frenchad009ac2005-04-28 22:41:05 -0700751 /* BB special case reconnect tid and uid here? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 rc = map_smb_to_linux_error(out_buf);
753
754 /* convert ByteCount if necessary */
755 if (receive_len >=
756 sizeof (struct smb_hdr) -
757 4 /* do not count RFC1001 header */ +
758 (2 * out_buf->WordCount) + 2 /* bcc */ )
Steve French0f2b27c2005-11-16 14:25:50 -0800759 BCC(out_buf) = le16_to_cpu(BCC_LE(out_buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 } else {
761 rc = -EIO;
Steve French79a58d12007-07-06 22:44:50 +0000762 cERROR(1, ("Bad MID state?"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 }
764 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000766out:
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000767 DeleteMidQEntry(midQ);
Steve French79a58d12007-07-06 22:44:50 +0000768 atomic_dec(&ses->server->inFlight);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000769 wake_up(&ses->server->request_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
771 return rc;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000772}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000774/* Send an NT_CANCEL SMB to cause the POSIX blocking lock to return. */
775
776static int
777send_nt_cancel(struct cifsTconInfo *tcon, struct smb_hdr *in_buf,
778 struct mid_q_entry *midQ)
779{
780 int rc = 0;
781 struct cifsSesInfo *ses = tcon->ses;
782 __u16 mid = in_buf->Mid;
783
784 header_assemble(in_buf, SMB_COM_NT_CANCEL, tcon, 0);
785 in_buf->Mid = mid;
Steve French79a58d12007-07-06 22:44:50 +0000786 down(&ses->server->tcpSem);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000787 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
788 if (rc) {
789 up(&ses->server->tcpSem);
790 return rc;
791 }
792 rc = smb_send(ses->server->ssocket, in_buf, in_buf->smb_buf_length,
793 (struct sockaddr *) &(ses->server->addr.sockAddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 up(&ses->server->tcpSem);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000795 return rc;
796}
797
798/* We send a LOCKINGX_CANCEL_LOCK to cause the Windows
799 blocking lock to return. */
800
801static int
802send_lock_cancel(const unsigned int xid, struct cifsTconInfo *tcon,
803 struct smb_hdr *in_buf,
804 struct smb_hdr *out_buf)
805{
806 int bytes_returned;
807 struct cifsSesInfo *ses = tcon->ses;
808 LOCK_REQ *pSMB = (LOCK_REQ *)in_buf;
809
810 /* We just modify the current in_buf to change
811 the type of lock from LOCKING_ANDX_SHARED_LOCK
812 or LOCKING_ANDX_EXCLUSIVE_LOCK to
813 LOCKING_ANDX_CANCEL_LOCK. */
814
815 pSMB->LockType = LOCKING_ANDX_CANCEL_LOCK|LOCKING_ANDX_LARGE_FILES;
816 pSMB->Timeout = 0;
817 pSMB->hdr.Mid = GetNextMid(ses->server);
818
819 return SendReceive(xid, ses, in_buf, out_buf,
820 &bytes_returned, 0);
821}
822
823int
824SendReceiveBlockingLock(const unsigned int xid, struct cifsTconInfo *tcon,
825 struct smb_hdr *in_buf, struct smb_hdr *out_buf,
826 int *pbytes_returned)
827{
828 int rc = 0;
829 int rstart = 0;
830 unsigned int receive_len;
831 struct mid_q_entry *midQ;
832 struct cifsSesInfo *ses;
833
834 if (tcon == NULL || tcon->ses == NULL) {
Steve French79a58d12007-07-06 22:44:50 +0000835 cERROR(1, ("Null smb session"));
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000836 return -EIO;
837 }
838 ses = tcon->ses;
839
Steve French79a58d12007-07-06 22:44:50 +0000840 if (ses->server == NULL) {
841 cERROR(1, ("Null tcp session"));
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000842 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 }
844
Steve French79a58d12007-07-06 22:44:50 +0000845 if (ses->server->tcpStatus == CifsExiting)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000846 return -ENOENT;
847
Steve French79a58d12007-07-06 22:44:50 +0000848 /* Ensure that we do not send more than 50 overlapping requests
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000849 to the same server. We may make this configurable later or
850 use ses->maxReq */
851
852 rc = wait_for_free_request(ses, 3);
853 if (rc)
854 return rc;
855
Steve French79a58d12007-07-06 22:44:50 +0000856 /* make sure that we sign in the same order that we send on this socket
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000857 and avoid races inside tcp sendmsg code that could cause corruption
858 of smb data */
859
Steve French79a58d12007-07-06 22:44:50 +0000860 down(&ses->server->tcpSem);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000861
862 rc = allocate_mid(ses, in_buf, &midQ);
863 if (rc) {
864 up(&ses->server->tcpSem);
865 return rc;
866 }
867
868 if (in_buf->smb_buf_length > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
869 up(&ses->server->tcpSem);
870 cERROR(1, ("Illegal length, greater than maximum frame, %d",
871 in_buf->smb_buf_length));
872 DeleteMidQEntry(midQ);
873 return -EIO;
874 }
875
876 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
877
878 midQ->midState = MID_REQUEST_SUBMITTED;
879#ifdef CONFIG_CIFS_STATS2
880 atomic_inc(&ses->server->inSend);
881#endif
882 rc = smb_send(ses->server->ssocket, in_buf, in_buf->smb_buf_length,
883 (struct sockaddr *) &(ses->server->addr.sockAddr));
884#ifdef CONFIG_CIFS_STATS2
885 atomic_dec(&ses->server->inSend);
886 midQ->when_sent = jiffies;
887#endif
888 up(&ses->server->tcpSem);
889
Steve French79a58d12007-07-06 22:44:50 +0000890 if (rc < 0) {
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000891 DeleteMidQEntry(midQ);
892 return rc;
893 }
894
895 /* Wait for a reply - allow signals to interrupt. */
896 rc = wait_event_interruptible(ses->server->response_q,
Steve French79a58d12007-07-06 22:44:50 +0000897 (!(midQ->midState == MID_REQUEST_SUBMITTED)) ||
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000898 ((ses->server->tcpStatus != CifsGood) &&
899 (ses->server->tcpStatus != CifsNew)));
900
901 /* Were we interrupted by a signal ? */
902 if ((rc == -ERESTARTSYS) &&
903 (midQ->midState == MID_REQUEST_SUBMITTED) &&
904 ((ses->server->tcpStatus == CifsGood) ||
905 (ses->server->tcpStatus == CifsNew))) {
906
907 if (in_buf->Command == SMB_COM_TRANSACTION2) {
908 /* POSIX lock. We send a NT_CANCEL SMB to cause the
909 blocking lock to return. */
910
911 rc = send_nt_cancel(tcon, in_buf, midQ);
912 if (rc) {
913 DeleteMidQEntry(midQ);
914 return rc;
915 }
916 } else {
917 /* Windows lock. We send a LOCKINGX_CANCEL_LOCK
918 to cause the blocking lock to return. */
919
920 rc = send_lock_cancel(xid, tcon, in_buf, out_buf);
921
922 /* If we get -ENOLCK back the lock may have
923 already been removed. Don't exit in this case. */
924 if (rc && rc != -ENOLCK) {
925 DeleteMidQEntry(midQ);
926 return rc;
927 }
928 }
929
930 /* Wait 5 seconds for the response. */
Steve French79a58d12007-07-06 22:44:50 +0000931 if (wait_for_response(ses, midQ, 5 * HZ, 5 * HZ) == 0) {
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000932 /* We got the response - restart system call. */
933 rstart = 1;
934 }
935 }
936
937 spin_lock(&GlobalMid_Lock);
938 if (midQ->resp_buf) {
939 spin_unlock(&GlobalMid_Lock);
940 receive_len = midQ->resp_buf->smb_buf_length;
941 } else {
Steve French79a58d12007-07-06 22:44:50 +0000942 cERROR(1, ("No response for cmd %d mid %d",
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000943 midQ->command, midQ->mid));
Steve French79a58d12007-07-06 22:44:50 +0000944 if (midQ->midState == MID_REQUEST_SUBMITTED) {
945 if (ses->server->tcpStatus == CifsExiting)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000946 rc = -EHOSTDOWN;
947 else {
948 ses->server->tcpStatus = CifsNeedReconnect;
949 midQ->midState = MID_RETRY_NEEDED;
950 }
951 }
952
953 if (rc != -EHOSTDOWN) {
Steve French79a58d12007-07-06 22:44:50 +0000954 if (midQ->midState == MID_RETRY_NEEDED) {
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000955 rc = -EAGAIN;
Steve French79a58d12007-07-06 22:44:50 +0000956 cFYI(1, ("marking request for retry"));
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000957 } else {
958 rc = -EIO;
959 }
960 }
961 spin_unlock(&GlobalMid_Lock);
962 DeleteMidQEntry(midQ);
963 return rc;
964 }
Steve French50c2f752007-07-13 00:33:32 +0000965
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000966 if (receive_len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE) {
967 cERROR(1, ("Frame too large received. Length: %d Xid: %d",
968 receive_len, xid));
969 rc = -EIO;
970 } else { /* rcvd frame is ok */
971
972 if (midQ->resp_buf && out_buf
973 && (midQ->midState == MID_RESPONSE_RECEIVED)) {
974 out_buf->smb_buf_length = receive_len;
975 memcpy((char *)out_buf + 4,
976 (char *)midQ->resp_buf + 4,
977 receive_len);
978
979 dump_smb(out_buf, 92);
980 /* convert the length into a more usable form */
Steve French79a58d12007-07-06 22:44:50 +0000981 if ((receive_len > 24) &&
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000982 (ses->server->secMode & (SECMODE_SIGN_REQUIRED |
983 SECMODE_SIGN_ENABLED))) {
984 rc = cifs_verify_signature(out_buf,
Steve Frenchb609f062007-07-09 07:55:14 +0000985 &ses->server->mac_signing_key,
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000986 midQ->sequence_number+1);
Steve French79a58d12007-07-06 22:44:50 +0000987 if (rc) {
988 cERROR(1, ("Unexpected SMB signature"));
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000989 /* BB FIXME add code to kill session */
990 }
991 }
992
993 *pbytes_returned = out_buf->smb_buf_length;
994
995 /* BB special case reconnect tid and uid here? */
996 rc = map_smb_to_linux_error(out_buf);
997
998 /* convert ByteCount if necessary */
999 if (receive_len >=
1000 sizeof (struct smb_hdr) -
1001 4 /* do not count RFC1001 header */ +
1002 (2 * out_buf->WordCount) + 2 /* bcc */ )
1003 BCC(out_buf) = le16_to_cpu(BCC_LE(out_buf));
1004 } else {
1005 rc = -EIO;
Steve French79a58d12007-07-06 22:44:50 +00001006 cERROR(1, ("Bad MID state?"));
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001007 }
1008 }
1009 DeleteMidQEntry(midQ);
1010 if (rstart && rc == -EACCES)
1011 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 return rc;
1013}