blob: 7ed32b3cb781a0011a0560b7fe3898825f5c9e4f [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 {
Steve French26f57362007-08-30 22:09:15 +000058 memset(temp, 0, sizeof(struct mid_q_entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 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;
Steve French26f57362007-08-30 22:09:15 +0000161 smb_msg.msg_namelen = sizeof(struct sockaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 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;
Steve French26f57362007-08-30 22:09:15 +0000231 smb_msg.msg_namelen = sizeof(struct sockaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 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);
Steve French26f57362007-08-30 22:09:15 +0000366 if (*ppmidQ == NULL)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000367 return -ENOMEM;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000368 return 0;
369}
370
Steve French79a58d12007-07-06 22:44:50 +0000371static int wait_for_response(struct cifsSesInfo *ses,
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000372 struct mid_q_entry *midQ,
373 unsigned long timeout,
374 unsigned long time_to_wait)
375{
376 unsigned long curr_timeout;
377
378 for (;;) {
379 curr_timeout = timeout + jiffies;
380 wait_event(ses->server->response_q,
Steve French79a58d12007-07-06 22:44:50 +0000381 (!(midQ->midState == MID_REQUEST_SUBMITTED)) ||
382 time_after(jiffies, curr_timeout) ||
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000383 ((ses->server->tcpStatus != CifsGood) &&
384 (ses->server->tcpStatus != CifsNew)));
385
386 if (time_after(jiffies, curr_timeout) &&
387 (midQ->midState == MID_REQUEST_SUBMITTED) &&
388 ((ses->server->tcpStatus == CifsGood) ||
389 (ses->server->tcpStatus == CifsNew))) {
390
391 unsigned long lrt;
392
393 /* We timed out. Is the server still
394 sending replies ? */
395 spin_lock(&GlobalMid_Lock);
396 lrt = ses->server->lstrp;
397 spin_unlock(&GlobalMid_Lock);
398
399 /* Calculate time_to_wait past last receive time.
Steve French79a58d12007-07-06 22:44:50 +0000400 Although we prefer not to time out if the
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000401 server is still responding - we will time
Steve French79a58d12007-07-06 22:44:50 +0000402 out if the server takes more than 15 (or 45
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000403 or 180) seconds to respond to this request
Steve French79a58d12007-07-06 22:44:50 +0000404 and has not responded to any request from
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000405 other threads on the client within 10 seconds */
406 lrt += time_to_wait;
407 if (time_after(jiffies, lrt)) {
408 /* No replies for time_to_wait. */
Steve French79a58d12007-07-06 22:44:50 +0000409 cERROR(1, ("server not responding"));
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000410 return -1;
411 }
412 } else {
413 return 0;
414 }
415 }
416}
417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418int
Steve French79a58d12007-07-06 22:44:50 +0000419SendReceive2(const unsigned int xid, struct cifsSesInfo *ses,
420 struct kvec *iov, int n_vec, int *pRespBufType /* ret */,
Steve Frencha761ac52007-10-18 21:45:27 +0000421 const int long_op, const int logError)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
423 int rc = 0;
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500424 unsigned int receive_len;
425 unsigned long timeout;
426 struct mid_q_entry *midQ;
Steve French3e844692005-10-03 13:37:24 -0700427 struct smb_hdr *in_buf = iov[0].iov_base;
Steve French50c2f752007-07-13 00:33:32 +0000428
Steve Frenchec637e32005-12-12 20:53:18 -0800429 *pRespBufType = CIFS_NO_BUFFER; /* no response buf yet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Steve French4b8f9302006-02-26 16:41:18 +0000431 if ((ses == NULL) || (ses->server == NULL)) {
432 cifs_small_buf_release(in_buf);
Steve French79a58d12007-07-06 22:44:50 +0000433 cERROR(1, ("Null session"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 return -EIO;
435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Steve French79a58d12007-07-06 22:44:50 +0000437 if (ses->server->tcpStatus == CifsExiting) {
Steve French4b8f9302006-02-26 16:41:18 +0000438 cifs_small_buf_release(in_buf);
Steve French31ca3bc2005-04-28 22:41:11 -0700439 return -ENOENT;
Steve French4b8f9302006-02-26 16:41:18 +0000440 }
Steve French31ca3bc2005-04-28 22:41:11 -0700441
Steve French79a58d12007-07-06 22:44:50 +0000442 /* Ensure that we do not send more than 50 overlapping requests
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 to the same server. We may make this configurable later or
444 use ses->maxReq */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000446 rc = wait_for_free_request(ses, long_op);
447 if (rc) {
448 cifs_small_buf_release(in_buf);
449 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 }
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000451
Steve French79a58d12007-07-06 22:44:50 +0000452 /* make sure that we sign in the same order that we send on this socket
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 and avoid races inside tcp sendmsg code that could cause corruption
454 of smb data */
455
Steve French79a58d12007-07-06 22:44:50 +0000456 down(&ses->server->tcpSem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000458 rc = allocate_mid(ses, in_buf, &midQ);
459 if (rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 up(&ses->server->tcpSem);
Steve French4b8f9302006-02-26 16:41:18 +0000461 cifs_small_buf_release(in_buf);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000462 /* Update # of requests on wire to server */
Steve French79a58d12007-07-06 22:44:50 +0000463 atomic_dec(&ses->server->inFlight);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000464 wake_up(&ses->server->request_q);
465 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 }
Steve French79a58d12007-07-06 22:44:50 +0000467 rc = cifs_sign_smb2(iov, n_vec, ses->server, &midQ->sequence_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
469 midQ->midState = MID_REQUEST_SUBMITTED;
Steve French131afd0b2005-10-07 09:51:05 -0700470#ifdef CONFIG_CIFS_STATS2
471 atomic_inc(&ses->server->inSend);
472#endif
Steve French3e844692005-10-03 13:37:24 -0700473 rc = smb_send2(ses->server->ssocket, iov, n_vec,
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500474 (struct sockaddr *) &(ses->server->addr.sockAddr));
Steve French131afd0b2005-10-07 09:51:05 -0700475#ifdef CONFIG_CIFS_STATS2
476 atomic_dec(&ses->server->inSend);
Steve French1047abc2005-10-11 19:58:06 -0700477 midQ->when_sent = jiffies;
Steve French131afd0b2005-10-07 09:51:05 -0700478#endif
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000479
480 up(&ses->server->tcpSem);
481 cifs_small_buf_release(in_buf);
482
Steve French79a58d12007-07-06 22:44:50 +0000483 if (rc < 0)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000484 goto out;
Steve French4b8f9302006-02-26 16:41:18 +0000485
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500486 if (long_op == -1)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000487 goto out;
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500488 else if (long_op == 2) /* writes past end of file can take loong time */
Steve French37c0eb42005-10-05 14:50:29 -0700489 timeout = 180 * HZ;
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500490 else if (long_op == 1)
Steve French79a58d12007-07-06 22:44:50 +0000491 timeout = 45 * HZ; /* should be greater than
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500492 servers oplock break timeout (about 43 seconds) */
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000493 else
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500494 timeout = 15 * HZ;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000495
Steve French79a58d12007-07-06 22:44:50 +0000496 /* wait for 15 seconds or until woken up due to response arriving or
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500497 due to last connection to this server being unmounted */
498 if (signal_pending(current)) {
499 /* if signal pending do not hold up user for full smb timeout
Steve French8a236262007-03-06 00:31:00 +0000500 but we still give response a chance to complete */
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500501 timeout = 2 * HZ;
Steve French79a58d12007-07-06 22:44:50 +0000502 }
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500503
504 /* No user interrupts in wait - wreaks havoc with performance */
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000505 wait_for_response(ses, midQ, timeout, 10 * HZ);
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500506
507 spin_lock(&GlobalMid_Lock);
508 if (midQ->resp_buf) {
509 spin_unlock(&GlobalMid_Lock);
Steve French70ca7342005-09-22 16:32:06 -0700510 receive_len = midQ->resp_buf->smb_buf_length;
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500511 } else {
Steve French79a58d12007-07-06 22:44:50 +0000512 cERROR(1, ("No response to cmd %d mid %d",
Steve French37c0eb42005-10-05 14:50:29 -0700513 midQ->command, midQ->mid));
Steve French79a58d12007-07-06 22:44:50 +0000514 if (midQ->midState == MID_REQUEST_SUBMITTED) {
515 if (ses->server->tcpStatus == CifsExiting)
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500516 rc = -EHOSTDOWN;
517 else {
518 ses->server->tcpStatus = CifsNeedReconnect;
519 midQ->midState = MID_RETRY_NEEDED;
520 }
521 }
522
523 if (rc != -EHOSTDOWN) {
Steve French79a58d12007-07-06 22:44:50 +0000524 if (midQ->midState == MID_RETRY_NEEDED) {
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500525 rc = -EAGAIN;
Steve French79a58d12007-07-06 22:44:50 +0000526 cFYI(1, ("marking request for retry"));
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500527 } else {
528 rc = -EIO;
529 }
530 }
531 spin_unlock(&GlobalMid_Lock);
532 DeleteMidQEntry(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000533 /* Update # of requests on wire to server */
Steve French79a58d12007-07-06 22:44:50 +0000534 atomic_dec(&ses->server->inFlight);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000535 wake_up(&ses->server->request_q);
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500536 return rc;
537 }
Steve French50c2f752007-07-13 00:33:32 +0000538
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500539 if (receive_len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE) {
540 cERROR(1, ("Frame too large received. Length: %d Xid: %d",
541 receive_len, xid));
542 rc = -EIO;
543 } else { /* rcvd frame is ok */
Steve French79a58d12007-07-06 22:44:50 +0000544 if (midQ->resp_buf &&
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500545 (midQ->midState == MID_RESPONSE_RECEIVED)) {
Steve French84afc292005-12-02 13:32:45 -0800546
Steve Frenchec637e32005-12-12 20:53:18 -0800547 iov[0].iov_base = (char *)midQ->resp_buf;
Steve French79a58d12007-07-06 22:44:50 +0000548 if (midQ->largeBuf)
Steve Frenchec637e32005-12-12 20:53:18 -0800549 *pRespBufType = CIFS_LARGE_BUFFER;
550 else
551 *pRespBufType = CIFS_SMALL_BUFFER;
552 iov[0].iov_len = receive_len + 4;
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500553
Steve Frenchec637e32005-12-12 20:53:18 -0800554 dump_smb(midQ->resp_buf, 80);
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500555 /* convert the length into a more usable form */
Steve French79a58d12007-07-06 22:44:50 +0000556 if ((receive_len > 24) &&
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500557 (ses->server->secMode & (SECMODE_SIGN_REQUIRED |
558 SECMODE_SIGN_ENABLED))) {
Steve Frenchec637e32005-12-12 20:53:18 -0800559 rc = cifs_verify_signature(midQ->resp_buf,
Steve Frenchb609f062007-07-09 07:55:14 +0000560 &ses->server->mac_signing_key,
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500561 midQ->sequence_number+1);
Steve French79a58d12007-07-06 22:44:50 +0000562 if (rc) {
563 cERROR(1, ("Unexpected SMB signature"));
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500564 /* BB FIXME add code to kill session */
565 }
566 }
567
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500568 /* BB special case reconnect tid and uid here? */
Steve Frencha761ac52007-10-18 21:45:27 +0000569 rc = map_smb_to_linux_error(midQ->resp_buf, logError);
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500570
571 /* convert ByteCount if necessary */
Steve French26f57362007-08-30 22:09:15 +0000572 if (receive_len >= sizeof(struct smb_hdr) - 4
573 /* do not count RFC1001 header */ +
Steve Frenchec637e32005-12-12 20:53:18 -0800574 (2 * midQ->resp_buf->WordCount) + 2 /* bcc */ )
Steve French79a58d12007-07-06 22:44:50 +0000575 BCC(midQ->resp_buf) =
Steve Frenchec637e32005-12-12 20:53:18 -0800576 le16_to_cpu(BCC_LE(midQ->resp_buf));
577 midQ->resp_buf = NULL; /* mark it so will not be freed
578 by DeleteMidQEntry */
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500579 } else {
580 rc = -EIO;
Steve French79a58d12007-07-06 22:44:50 +0000581 cFYI(1, ("Bad MID state?"));
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500582 }
583 }
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000584
585out:
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500586 DeleteMidQEntry(midQ);
Steve French79a58d12007-07-06 22:44:50 +0000587 atomic_dec(&ses->server->inFlight);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000588 wake_up(&ses->server->request_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
590 return rc;
591}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
593int
594SendReceive(const unsigned int xid, struct cifsSesInfo *ses,
595 struct smb_hdr *in_buf, struct smb_hdr *out_buf,
596 int *pbytes_returned, const int long_op)
597{
598 int rc = 0;
599 unsigned int receive_len;
600 unsigned long timeout;
601 struct mid_q_entry *midQ;
602
603 if (ses == NULL) {
Steve French79a58d12007-07-06 22:44:50 +0000604 cERROR(1, ("Null smb session"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 return -EIO;
606 }
Steve French79a58d12007-07-06 22:44:50 +0000607 if (ses->server == NULL) {
608 cERROR(1, ("Null tcp session"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 return -EIO;
610 }
611
Steve French79a58d12007-07-06 22:44:50 +0000612 if (ses->server->tcpStatus == CifsExiting)
Steve French31ca3bc2005-04-28 22:41:11 -0700613 return -ENOENT;
614
Steve French79a58d12007-07-06 22:44:50 +0000615 /* Ensure that we do not send more than 50 overlapping requests
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 to the same server. We may make this configurable later or
617 use ses->maxReq */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000619 rc = wait_for_free_request(ses, long_op);
620 if (rc)
621 return rc;
622
Steve French79a58d12007-07-06 22:44:50 +0000623 /* make sure that we sign in the same order that we send on this socket
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 and avoid races inside tcp sendmsg code that could cause corruption
625 of smb data */
626
Steve French79a58d12007-07-06 22:44:50 +0000627 down(&ses->server->tcpSem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000629 rc = allocate_mid(ses, in_buf, &midQ);
630 if (rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 up(&ses->server->tcpSem);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000632 /* Update # of requests on wire to server */
Steve French79a58d12007-07-06 22:44:50 +0000633 atomic_dec(&ses->server->inFlight);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000634 wake_up(&ses->server->request_q);
635 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 }
637
638 if (in_buf->smb_buf_length > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
Steve French26a21b92006-05-31 18:05:34 +0000639 cERROR(1, ("Illegal length, greater than maximum frame, %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 in_buf->smb_buf_length));
641 DeleteMidQEntry(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000642 up(&ses->server->tcpSem);
643 /* Update # of requests on wire to server */
Steve French79a58d12007-07-06 22:44:50 +0000644 atomic_dec(&ses->server->inFlight);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000645 wake_up(&ses->server->request_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 return -EIO;
647 }
648
Steve Frenchad009ac2005-04-28 22:41:05 -0700649 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 midQ->midState = MID_REQUEST_SUBMITTED;
Steve French131afd0b2005-10-07 09:51:05 -0700652#ifdef CONFIG_CIFS_STATS2
653 atomic_inc(&ses->server->inSend);
654#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 rc = smb_send(ses->server->ssocket, in_buf, in_buf->smb_buf_length,
656 (struct sockaddr *) &(ses->server->addr.sockAddr));
Steve French131afd0b2005-10-07 09:51:05 -0700657#ifdef CONFIG_CIFS_STATS2
658 atomic_dec(&ses->server->inSend);
Steve French1047abc2005-10-11 19:58:06 -0700659 midQ->when_sent = jiffies;
Steve French131afd0b2005-10-07 09:51:05 -0700660#endif
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000661 up(&ses->server->tcpSem);
662
Steve French79a58d12007-07-06 22:44:50 +0000663 if (rc < 0)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000664 goto out;
665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 if (long_op == -1)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000667 goto out;
Steve French275cde12005-04-28 22:41:10 -0700668 else if (long_op == 2) /* writes past end of file can take loong time */
Steve French37c0eb42005-10-05 14:50:29 -0700669 timeout = 180 * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 else if (long_op == 1)
Steve French79a58d12007-07-06 22:44:50 +0000671 timeout = 45 * HZ; /* should be greater than
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 servers oplock break timeout (about 43 seconds) */
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000673 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 timeout = 15 * HZ;
Steve French79a58d12007-07-06 22:44:50 +0000675 /* wait for 15 seconds or until woken up due to response arriving or
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 due to last connection to this server being unmounted */
677 if (signal_pending(current)) {
678 /* if signal pending do not hold up user for full smb timeout
Steve French8a236262007-03-06 00:31:00 +0000679 but we still give response a chance to complete */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 timeout = 2 * HZ;
Steve French79a58d12007-07-06 22:44:50 +0000681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
683 /* No user interrupts in wait - wreaks havoc with performance */
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000684 wait_for_response(ses, midQ, timeout, 10 * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686 spin_lock(&GlobalMid_Lock);
687 if (midQ->resp_buf) {
688 spin_unlock(&GlobalMid_Lock);
Steve French70ca7342005-09-22 16:32:06 -0700689 receive_len = midQ->resp_buf->smb_buf_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 } else {
Steve French79a58d12007-07-06 22:44:50 +0000691 cERROR(1, ("No response for cmd %d mid %d",
Steve French37c0eb42005-10-05 14:50:29 -0700692 midQ->command, midQ->mid));
Steve French79a58d12007-07-06 22:44:50 +0000693 if (midQ->midState == MID_REQUEST_SUBMITTED) {
694 if (ses->server->tcpStatus == CifsExiting)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 rc = -EHOSTDOWN;
696 else {
697 ses->server->tcpStatus = CifsNeedReconnect;
698 midQ->midState = MID_RETRY_NEEDED;
699 }
700 }
701
702 if (rc != -EHOSTDOWN) {
Steve French79a58d12007-07-06 22:44:50 +0000703 if (midQ->midState == MID_RETRY_NEEDED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 rc = -EAGAIN;
Steve French79a58d12007-07-06 22:44:50 +0000705 cFYI(1, ("marking request for retry"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 } else {
707 rc = -EIO;
708 }
709 }
710 spin_unlock(&GlobalMid_Lock);
711 DeleteMidQEntry(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000712 /* Update # of requests on wire to server */
Steve French79a58d12007-07-06 22:44:50 +0000713 atomic_dec(&ses->server->inFlight);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000714 wake_up(&ses->server->request_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 return rc;
716 }
Steve French50c2f752007-07-13 00:33:32 +0000717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 if (receive_len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE) {
Steve Frenchad009ac2005-04-28 22:41:05 -0700719 cERROR(1, ("Frame too large received. Length: %d Xid: %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 receive_len, xid));
721 rc = -EIO;
722 } else { /* rcvd frame is ok */
723
724 if (midQ->resp_buf && out_buf
725 && (midQ->midState == MID_RESPONSE_RECEIVED)) {
726 out_buf->smb_buf_length = receive_len;
727 memcpy((char *)out_buf + 4,
728 (char *)midQ->resp_buf + 4,
729 receive_len);
730
731 dump_smb(out_buf, 92);
732 /* convert the length into a more usable form */
Steve French79a58d12007-07-06 22:44:50 +0000733 if ((receive_len > 24) &&
Steve Frenchad009ac2005-04-28 22:41:05 -0700734 (ses->server->secMode & (SECMODE_SIGN_REQUIRED |
735 SECMODE_SIGN_ENABLED))) {
736 rc = cifs_verify_signature(out_buf,
Steve Frenchb609f062007-07-09 07:55:14 +0000737 &ses->server->mac_signing_key,
Steve Frenchad009ac2005-04-28 22:41:05 -0700738 midQ->sequence_number+1);
Steve French79a58d12007-07-06 22:44:50 +0000739 if (rc) {
740 cERROR(1, ("Unexpected SMB signature"));
Steve French275cde12005-04-28 22:41:10 -0700741 /* BB FIXME add code to kill session */
Steve Frenchad009ac2005-04-28 22:41:05 -0700742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 }
744
745 *pbytes_returned = out_buf->smb_buf_length;
746
Steve Frenchad009ac2005-04-28 22:41:05 -0700747 /* BB special case reconnect tid and uid here? */
Steve Frencha761ac52007-10-18 21:45:27 +0000748 rc = map_smb_to_linux_error(out_buf, 0 /* no log */ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
750 /* convert ByteCount if necessary */
Steve French26f57362007-08-30 22:09:15 +0000751 if (receive_len >= sizeof(struct smb_hdr) - 4
752 /* do not count RFC1001 header */ +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 (2 * out_buf->WordCount) + 2 /* bcc */ )
Steve French0f2b27c2005-11-16 14:25:50 -0800754 BCC(out_buf) = le16_to_cpu(BCC_LE(out_buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 } else {
756 rc = -EIO;
Steve French79a58d12007-07-06 22:44:50 +0000757 cERROR(1, ("Bad MID state?"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 }
759 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000761out:
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000762 DeleteMidQEntry(midQ);
Steve French79a58d12007-07-06 22:44:50 +0000763 atomic_dec(&ses->server->inFlight);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000764 wake_up(&ses->server->request_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766 return rc;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000767}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000769/* Send an NT_CANCEL SMB to cause the POSIX blocking lock to return. */
770
771static int
772send_nt_cancel(struct cifsTconInfo *tcon, struct smb_hdr *in_buf,
773 struct mid_q_entry *midQ)
774{
775 int rc = 0;
776 struct cifsSesInfo *ses = tcon->ses;
777 __u16 mid = in_buf->Mid;
778
779 header_assemble(in_buf, SMB_COM_NT_CANCEL, tcon, 0);
780 in_buf->Mid = mid;
Steve French79a58d12007-07-06 22:44:50 +0000781 down(&ses->server->tcpSem);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000782 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
783 if (rc) {
784 up(&ses->server->tcpSem);
785 return rc;
786 }
787 rc = smb_send(ses->server->ssocket, in_buf, in_buf->smb_buf_length,
788 (struct sockaddr *) &(ses->server->addr.sockAddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 up(&ses->server->tcpSem);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000790 return rc;
791}
792
793/* We send a LOCKINGX_CANCEL_LOCK to cause the Windows
794 blocking lock to return. */
795
796static int
797send_lock_cancel(const unsigned int xid, struct cifsTconInfo *tcon,
798 struct smb_hdr *in_buf,
799 struct smb_hdr *out_buf)
800{
801 int bytes_returned;
802 struct cifsSesInfo *ses = tcon->ses;
803 LOCK_REQ *pSMB = (LOCK_REQ *)in_buf;
804
805 /* We just modify the current in_buf to change
806 the type of lock from LOCKING_ANDX_SHARED_LOCK
807 or LOCKING_ANDX_EXCLUSIVE_LOCK to
808 LOCKING_ANDX_CANCEL_LOCK. */
809
810 pSMB->LockType = LOCKING_ANDX_CANCEL_LOCK|LOCKING_ANDX_LARGE_FILES;
811 pSMB->Timeout = 0;
812 pSMB->hdr.Mid = GetNextMid(ses->server);
813
814 return SendReceive(xid, ses, in_buf, out_buf,
815 &bytes_returned, 0);
816}
817
818int
819SendReceiveBlockingLock(const unsigned int xid, struct cifsTconInfo *tcon,
820 struct smb_hdr *in_buf, struct smb_hdr *out_buf,
821 int *pbytes_returned)
822{
823 int rc = 0;
824 int rstart = 0;
825 unsigned int receive_len;
826 struct mid_q_entry *midQ;
827 struct cifsSesInfo *ses;
828
829 if (tcon == NULL || tcon->ses == NULL) {
Steve French79a58d12007-07-06 22:44:50 +0000830 cERROR(1, ("Null smb session"));
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000831 return -EIO;
832 }
833 ses = tcon->ses;
834
Steve French79a58d12007-07-06 22:44:50 +0000835 if (ses->server == NULL) {
836 cERROR(1, ("Null tcp session"));
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000837 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 }
839
Steve French79a58d12007-07-06 22:44:50 +0000840 if (ses->server->tcpStatus == CifsExiting)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000841 return -ENOENT;
842
Steve French79a58d12007-07-06 22:44:50 +0000843 /* Ensure that we do not send more than 50 overlapping requests
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000844 to the same server. We may make this configurable later or
845 use ses->maxReq */
846
847 rc = wait_for_free_request(ses, 3);
848 if (rc)
849 return rc;
850
Steve French79a58d12007-07-06 22:44:50 +0000851 /* make sure that we sign in the same order that we send on this socket
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000852 and avoid races inside tcp sendmsg code that could cause corruption
853 of smb data */
854
Steve French79a58d12007-07-06 22:44:50 +0000855 down(&ses->server->tcpSem);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000856
857 rc = allocate_mid(ses, in_buf, &midQ);
858 if (rc) {
859 up(&ses->server->tcpSem);
860 return rc;
861 }
862
863 if (in_buf->smb_buf_length > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
864 up(&ses->server->tcpSem);
865 cERROR(1, ("Illegal length, greater than maximum frame, %d",
866 in_buf->smb_buf_length));
867 DeleteMidQEntry(midQ);
868 return -EIO;
869 }
870
871 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
872
873 midQ->midState = MID_REQUEST_SUBMITTED;
874#ifdef CONFIG_CIFS_STATS2
875 atomic_inc(&ses->server->inSend);
876#endif
877 rc = smb_send(ses->server->ssocket, in_buf, in_buf->smb_buf_length,
878 (struct sockaddr *) &(ses->server->addr.sockAddr));
879#ifdef CONFIG_CIFS_STATS2
880 atomic_dec(&ses->server->inSend);
881 midQ->when_sent = jiffies;
882#endif
883 up(&ses->server->tcpSem);
884
Steve French79a58d12007-07-06 22:44:50 +0000885 if (rc < 0) {
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000886 DeleteMidQEntry(midQ);
887 return rc;
888 }
889
890 /* Wait for a reply - allow signals to interrupt. */
891 rc = wait_event_interruptible(ses->server->response_q,
Steve French79a58d12007-07-06 22:44:50 +0000892 (!(midQ->midState == MID_REQUEST_SUBMITTED)) ||
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000893 ((ses->server->tcpStatus != CifsGood) &&
894 (ses->server->tcpStatus != CifsNew)));
895
896 /* Were we interrupted by a signal ? */
897 if ((rc == -ERESTARTSYS) &&
898 (midQ->midState == MID_REQUEST_SUBMITTED) &&
899 ((ses->server->tcpStatus == CifsGood) ||
900 (ses->server->tcpStatus == CifsNew))) {
901
902 if (in_buf->Command == SMB_COM_TRANSACTION2) {
903 /* POSIX lock. We send a NT_CANCEL SMB to cause the
904 blocking lock to return. */
905
906 rc = send_nt_cancel(tcon, in_buf, midQ);
907 if (rc) {
908 DeleteMidQEntry(midQ);
909 return rc;
910 }
911 } else {
912 /* Windows lock. We send a LOCKINGX_CANCEL_LOCK
913 to cause the blocking lock to return. */
914
915 rc = send_lock_cancel(xid, tcon, in_buf, out_buf);
916
917 /* If we get -ENOLCK back the lock may have
918 already been removed. Don't exit in this case. */
919 if (rc && rc != -ENOLCK) {
920 DeleteMidQEntry(midQ);
921 return rc;
922 }
923 }
924
925 /* Wait 5 seconds for the response. */
Steve French79a58d12007-07-06 22:44:50 +0000926 if (wait_for_response(ses, midQ, 5 * HZ, 5 * HZ) == 0) {
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000927 /* We got the response - restart system call. */
928 rstart = 1;
929 }
930 }
931
932 spin_lock(&GlobalMid_Lock);
933 if (midQ->resp_buf) {
934 spin_unlock(&GlobalMid_Lock);
935 receive_len = midQ->resp_buf->smb_buf_length;
936 } else {
Steve French79a58d12007-07-06 22:44:50 +0000937 cERROR(1, ("No response for cmd %d mid %d",
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000938 midQ->command, midQ->mid));
Steve French79a58d12007-07-06 22:44:50 +0000939 if (midQ->midState == MID_REQUEST_SUBMITTED) {
940 if (ses->server->tcpStatus == CifsExiting)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000941 rc = -EHOSTDOWN;
942 else {
943 ses->server->tcpStatus = CifsNeedReconnect;
944 midQ->midState = MID_RETRY_NEEDED;
945 }
946 }
947
948 if (rc != -EHOSTDOWN) {
Steve French79a58d12007-07-06 22:44:50 +0000949 if (midQ->midState == MID_RETRY_NEEDED) {
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000950 rc = -EAGAIN;
Steve French79a58d12007-07-06 22:44:50 +0000951 cFYI(1, ("marking request for retry"));
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000952 } else {
953 rc = -EIO;
954 }
955 }
956 spin_unlock(&GlobalMid_Lock);
957 DeleteMidQEntry(midQ);
958 return rc;
959 }
Steve French50c2f752007-07-13 00:33:32 +0000960
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000961 if (receive_len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE) {
962 cERROR(1, ("Frame too large received. Length: %d Xid: %d",
963 receive_len, xid));
964 rc = -EIO;
965 } else { /* rcvd frame is ok */
966
967 if (midQ->resp_buf && out_buf
968 && (midQ->midState == MID_RESPONSE_RECEIVED)) {
969 out_buf->smb_buf_length = receive_len;
970 memcpy((char *)out_buf + 4,
971 (char *)midQ->resp_buf + 4,
972 receive_len);
973
974 dump_smb(out_buf, 92);
975 /* convert the length into a more usable form */
Steve French79a58d12007-07-06 22:44:50 +0000976 if ((receive_len > 24) &&
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000977 (ses->server->secMode & (SECMODE_SIGN_REQUIRED |
978 SECMODE_SIGN_ENABLED))) {
979 rc = cifs_verify_signature(out_buf,
Steve Frenchb609f062007-07-09 07:55:14 +0000980 &ses->server->mac_signing_key,
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000981 midQ->sequence_number+1);
Steve French79a58d12007-07-06 22:44:50 +0000982 if (rc) {
983 cERROR(1, ("Unexpected SMB signature"));
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000984 /* BB FIXME add code to kill session */
985 }
986 }
987
988 *pbytes_returned = out_buf->smb_buf_length;
989
990 /* BB special case reconnect tid and uid here? */
Steve Frencha761ac52007-10-18 21:45:27 +0000991 rc = map_smb_to_linux_error(out_buf, 0 /* no log */ );
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000992
993 /* convert ByteCount if necessary */
Steve French26f57362007-08-30 22:09:15 +0000994 if (receive_len >= sizeof(struct smb_hdr) - 4
995 /* do not count RFC1001 header */ +
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000996 (2 * out_buf->WordCount) + 2 /* bcc */ )
997 BCC(out_buf) = le16_to_cpu(BCC_LE(out_buf));
998 } else {
999 rc = -EIO;
Steve French79a58d12007-07-06 22:44:50 +00001000 cERROR(1, ("Bad MID state?"));
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001001 }
1002 }
1003 DeleteMidQEntry(midQ);
1004 if (rstart && rc == -EACCES)
1005 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 return rc;
1007}