blob: 000ac509c98a32e884322149e88b53e4bdb2a4c1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/cifs/transport.c
3 *
Steve Frenchad7a2922008-02-07 23:25:02 +00004 * Copyright (C) International Business Machines Corp., 2002,2008
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Author(s): Steve French (sfrench@us.ibm.com)
Steve French14a441a2b2006-07-16 04:32:51 +00006 * Jeremy Allison (jra@samba.org) 2006.
Steve French79a58d12007-07-06 22:44:50 +00007 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * This library is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published
10 * by the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
16 * the GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this library; if not, write to the Free Software
Steve French79a58d12007-07-06 22:44:50 +000020 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 */
22
23#include <linux/fs.h>
24#include <linux/list.h>
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
Steve French5d941ca2008-04-15 18:40:48 +0000145
146void DeleteTconOplockQEntries(struct cifsTconInfo *tcon)
147{
148 struct oplock_q_entry *temp;
149
150 if (tcon == NULL)
151 return;
152
153 spin_lock(&GlobalMid_Lock);
154 list_for_each_entry(temp, &GlobalOplock_Q, qhead) {
155 if ((temp->tcon) && (temp->tcon == tcon)) {
156 list_del(&temp->qhead);
157 kmem_cache_free(cifs_oplock_cachep, temp);
158 }
159 }
160 spin_unlock(&GlobalMid_Lock);
161}
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163int
164smb_send(struct socket *ssocket, struct smb_hdr *smb_buffer,
165 unsigned int smb_buf_length, struct sockaddr *sin)
166{
167 int rc = 0;
168 int i = 0;
169 struct msghdr smb_msg;
170 struct kvec iov;
171 unsigned len = smb_buf_length + 4;
172
Steve French79a58d12007-07-06 22:44:50 +0000173 if (ssocket == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 return -ENOTSOCK; /* BB eventually add reconnect code here */
175 iov.iov_base = smb_buffer;
176 iov.iov_len = len;
177
178 smb_msg.msg_name = sin;
Steve French26f57362007-08-30 22:09:15 +0000179 smb_msg.msg_namelen = sizeof(struct sockaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 smb_msg.msg_control = NULL;
181 smb_msg.msg_controllen = 0;
182 smb_msg.msg_flags = MSG_DONTWAIT + MSG_NOSIGNAL; /* BB add more flags?*/
183
184 /* smb header is converted in header_assemble. bcc and rest of SMB word
Steve French79a58d12007-07-06 22:44:50 +0000185 area, and byte area if necessary, is converted to littleendian in
186 cifssmb.c and RFC1001 len is converted to bigendian in smb_send
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 Flags2 is converted in SendReceive */
188
189 smb_buffer->smb_buf_length = cpu_to_be32(smb_buffer->smb_buf_length);
Steve French3e844692005-10-03 13:37:24 -0700190 cFYI(1, ("Sending smb of length %d", smb_buf_length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 dump_smb(smb_buffer, len);
192
193 while (len > 0) {
194 rc = kernel_sendmsg(ssocket, &smb_msg, &iov, 1, len);
195 if ((rc == -ENOSPC) || (rc == -EAGAIN)) {
196 i++;
Steve French3e844692005-10-03 13:37:24 -0700197 /* smaller timeout here than send2 since smaller size */
Steve French79a58d12007-07-06 22:44:50 +0000198 /* Although it may not be required, this also is smaller
199 oplock break time */
200 if (i > 12) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 cERROR(1,
Steve French68058e72005-10-10 10:34:22 -0700202 ("sends on sock %p stuck for 7 seconds",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 ssocket));
204 rc = -EAGAIN;
205 break;
206 }
Steve French68058e72005-10-10 10:34:22 -0700207 msleep(1 << i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 continue;
209 }
Steve French79a58d12007-07-06 22:44:50 +0000210 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 break;
Steve French5e1253b2005-10-10 14:06:37 -0700212 else
213 i = 0; /* reset i after each successful send */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 iov.iov_base += rc;
215 iov.iov_len -= rc;
216 len -= rc;
217 }
218
219 if (rc < 0) {
Steve French79a58d12007-07-06 22:44:50 +0000220 cERROR(1, ("Error %d sending data on socket to server", rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 } else {
222 rc = 0;
223 }
224
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000225 /* Don't want to modify the buffer as a
226 side effect of this call. */
227 smb_buffer->smb_buf_length = smb_buf_length;
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 return rc;
230}
231
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500232static int
Steve French3e844692005-10-03 13:37:24 -0700233smb_send2(struct socket *ssocket, struct kvec *iov, int n_vec,
234 struct sockaddr *sin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
236 int rc = 0;
237 int i = 0;
238 struct msghdr smb_msg;
Steve French3e844692005-10-03 13:37:24 -0700239 struct smb_hdr *smb_buffer = iov[0].iov_base;
240 unsigned int len = iov[0].iov_len;
241 unsigned int total_len;
242 int first_vec = 0;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000243 unsigned int smb_buf_length = smb_buffer->smb_buf_length;
Steve French50c2f752007-07-13 00:33:32 +0000244
Steve French79a58d12007-07-06 22:44:50 +0000245 if (ssocket == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 return -ENOTSOCK; /* BB eventually add reconnect code here */
Steve French3e844692005-10-03 13:37:24 -0700247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 smb_msg.msg_name = sin;
Steve French26f57362007-08-30 22:09:15 +0000249 smb_msg.msg_namelen = sizeof(struct sockaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 smb_msg.msg_control = NULL;
251 smb_msg.msg_controllen = 0;
252 smb_msg.msg_flags = MSG_DONTWAIT + MSG_NOSIGNAL; /* BB add more flags?*/
253
254 /* smb header is converted in header_assemble. bcc and rest of SMB word
Steve French79a58d12007-07-06 22:44:50 +0000255 area, and byte area if necessary, is converted to littleendian in
256 cifssmb.c and RFC1001 len is converted to bigendian in smb_send
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 Flags2 is converted in SendReceive */
258
Steve French3e844692005-10-03 13:37:24 -0700259
260 total_len = 0;
261 for (i = 0; i < n_vec; i++)
262 total_len += iov[i].iov_len;
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 smb_buffer->smb_buf_length = cpu_to_be32(smb_buffer->smb_buf_length);
Steve French3e844692005-10-03 13:37:24 -0700265 cFYI(1, ("Sending smb: total_len %d", total_len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 dump_smb(smb_buffer, len);
267
Steve French3e844692005-10-03 13:37:24 -0700268 while (total_len) {
269 rc = kernel_sendmsg(ssocket, &smb_msg, &iov[first_vec],
270 n_vec - first_vec, total_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 if ((rc == -ENOSPC) || (rc == -EAGAIN)) {
272 i++;
Steve French79a58d12007-07-06 22:44:50 +0000273 if (i >= 14) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 cERROR(1,
Steve French68058e72005-10-10 10:34:22 -0700275 ("sends on sock %p stuck for 15 seconds",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 ssocket));
277 rc = -EAGAIN;
278 break;
279 }
Steve French68058e72005-10-10 10:34:22 -0700280 msleep(1 << i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 continue;
282 }
Steve French79a58d12007-07-06 22:44:50 +0000283 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 break;
Steve French3e844692005-10-03 13:37:24 -0700285
286 if (rc >= total_len) {
287 WARN_ON(rc > total_len);
288 break;
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500289 }
Steve French79a58d12007-07-06 22:44:50 +0000290 if (rc == 0) {
Steve French3e844692005-10-03 13:37:24 -0700291 /* should never happen, letting socket clear before
292 retrying is our only obvious option here */
Steve French79a58d12007-07-06 22:44:50 +0000293 cERROR(1, ("tcp sent no data"));
Steve French3e844692005-10-03 13:37:24 -0700294 msleep(500);
295 continue;
296 }
297 total_len -= rc;
Steve French68058e72005-10-10 10:34:22 -0700298 /* the line below resets i */
Steve French3e844692005-10-03 13:37:24 -0700299 for (i = first_vec; i < n_vec; i++) {
300 if (iov[i].iov_len) {
301 if (rc > iov[i].iov_len) {
302 rc -= iov[i].iov_len;
303 iov[i].iov_len = 0;
304 } else {
305 iov[i].iov_base += rc;
306 iov[i].iov_len -= rc;
307 first_vec = i;
308 break;
309 }
310 }
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500311 }
Steve French5e1253b2005-10-10 14:06:37 -0700312 i = 0; /* in case we get ENOSPC on the next send */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 }
314
315 if (rc < 0) {
Steve French79a58d12007-07-06 22:44:50 +0000316 cERROR(1, ("Error %d sending data on socket to server", rc));
Steve French3e844692005-10-03 13:37:24 -0700317 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000320 /* Don't want to modify the buffer as a
321 side effect of this call. */
322 smb_buffer->smb_buf_length = smb_buf_length;
323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 return rc;
325}
326
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000327static int wait_for_free_request(struct cifsSesInfo *ses, const int long_op)
328{
Steve French133672e2007-11-13 22:41:37 +0000329 if (long_op == CIFS_ASYNC_OP) {
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000330 /* oplock breaks must not be held up */
331 atomic_inc(&ses->server->inFlight);
332 } else {
Steve French79a58d12007-07-06 22:44:50 +0000333 spin_lock(&GlobalMid_Lock);
334 while (1) {
335 if (atomic_read(&ses->server->inFlight) >=
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000336 cifs_max_pending){
337 spin_unlock(&GlobalMid_Lock);
338#ifdef CONFIG_CIFS_STATS2
339 atomic_inc(&ses->server->num_waiters);
340#endif
341 wait_event(ses->server->request_q,
342 atomic_read(&ses->server->inFlight)
343 < cifs_max_pending);
344#ifdef CONFIG_CIFS_STATS2
345 atomic_dec(&ses->server->num_waiters);
346#endif
347 spin_lock(&GlobalMid_Lock);
348 } else {
Steve French79a58d12007-07-06 22:44:50 +0000349 if (ses->server->tcpStatus == CifsExiting) {
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000350 spin_unlock(&GlobalMid_Lock);
351 return -ENOENT;
352 }
353
Steve French79a58d12007-07-06 22:44:50 +0000354 /* can not count locking commands against total
355 as they are allowed to block on server */
Steve French50c2f752007-07-13 00:33:32 +0000356
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000357 /* update # of requests on the wire to server */
Steve French133672e2007-11-13 22:41:37 +0000358 if (long_op != CIFS_BLOCKING_OP)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000359 atomic_inc(&ses->server->inFlight);
360 spin_unlock(&GlobalMid_Lock);
361 break;
362 }
363 }
364 }
365 return 0;
366}
367
368static int allocate_mid(struct cifsSesInfo *ses, struct smb_hdr *in_buf,
369 struct mid_q_entry **ppmidQ)
370{
371 if (ses->server->tcpStatus == CifsExiting) {
372 return -ENOENT;
373 } else if (ses->server->tcpStatus == CifsNeedReconnect) {
Steve French79a58d12007-07-06 22:44:50 +0000374 cFYI(1, ("tcp session dead - return to caller to retry"));
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000375 return -EAGAIN;
376 } else if (ses->status != CifsGood) {
377 /* check if SMB session is bad because we are setting it up */
Steve French79a58d12007-07-06 22:44:50 +0000378 if ((in_buf->Command != SMB_COM_SESSION_SETUP_ANDX) &&
Steve Frenchad7a2922008-02-07 23:25:02 +0000379 (in_buf->Command != SMB_COM_NEGOTIATE))
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000380 return -EAGAIN;
Steve Frenchad7a2922008-02-07 23:25:02 +0000381 /* else ok - we are setting up session */
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000382 }
383 *ppmidQ = AllocMidQEntry(in_buf, ses);
Steve French26f57362007-08-30 22:09:15 +0000384 if (*ppmidQ == NULL)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000385 return -ENOMEM;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000386 return 0;
387}
388
Steve French79a58d12007-07-06 22:44:50 +0000389static int wait_for_response(struct cifsSesInfo *ses,
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000390 struct mid_q_entry *midQ,
391 unsigned long timeout,
392 unsigned long time_to_wait)
393{
394 unsigned long curr_timeout;
395
396 for (;;) {
397 curr_timeout = timeout + jiffies;
398 wait_event(ses->server->response_q,
Steve French79a58d12007-07-06 22:44:50 +0000399 (!(midQ->midState == MID_REQUEST_SUBMITTED)) ||
400 time_after(jiffies, curr_timeout) ||
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000401 ((ses->server->tcpStatus != CifsGood) &&
402 (ses->server->tcpStatus != CifsNew)));
403
404 if (time_after(jiffies, curr_timeout) &&
405 (midQ->midState == MID_REQUEST_SUBMITTED) &&
406 ((ses->server->tcpStatus == CifsGood) ||
407 (ses->server->tcpStatus == CifsNew))) {
408
409 unsigned long lrt;
410
411 /* We timed out. Is the server still
412 sending replies ? */
413 spin_lock(&GlobalMid_Lock);
414 lrt = ses->server->lstrp;
415 spin_unlock(&GlobalMid_Lock);
416
417 /* Calculate time_to_wait past last receive time.
Steve French79a58d12007-07-06 22:44:50 +0000418 Although we prefer not to time out if the
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000419 server is still responding - we will time
Steve French79a58d12007-07-06 22:44:50 +0000420 out if the server takes more than 15 (or 45
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000421 or 180) seconds to respond to this request
Steve French79a58d12007-07-06 22:44:50 +0000422 and has not responded to any request from
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000423 other threads on the client within 10 seconds */
424 lrt += time_to_wait;
425 if (time_after(jiffies, lrt)) {
426 /* No replies for time_to_wait. */
Steve French79a58d12007-07-06 22:44:50 +0000427 cERROR(1, ("server not responding"));
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000428 return -1;
429 }
430 } else {
431 return 0;
432 }
433 }
434}
435
Steve French133672e2007-11-13 22:41:37 +0000436
437/*
438 *
439 * Send an SMB Request. No response info (other than return code)
440 * needs to be parsed.
441 *
442 * flags indicate the type of request buffer and how long to wait
443 * and whether to log NT STATUS code (error) before mapping it to POSIX error
444 *
445 */
446int
447SendReceiveNoRsp(const unsigned int xid, struct cifsSesInfo *ses,
448 struct smb_hdr *in_buf, int flags)
449{
450 int rc;
451 struct kvec iov[1];
452 int resp_buf_type;
453
454 iov[0].iov_base = (char *)in_buf;
455 iov[0].iov_len = in_buf->smb_buf_length + 4;
456 flags |= CIFS_NO_RESP;
457 rc = SendReceive2(xid, ses, iov, 1, &resp_buf_type, flags);
Steve French90c81e02008-02-12 20:32:36 +0000458 cFYI(DBG2, ("SendRcvNoRsp flags %d rc %d", flags, rc));
459
Steve French133672e2007-11-13 22:41:37 +0000460 return rc;
461}
462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463int
Steve French79a58d12007-07-06 22:44:50 +0000464SendReceive2(const unsigned int xid, struct cifsSesInfo *ses,
465 struct kvec *iov, int n_vec, int *pRespBufType /* ret */,
Steve French133672e2007-11-13 22:41:37 +0000466 const int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
468 int rc = 0;
Steve French133672e2007-11-13 22:41:37 +0000469 int long_op;
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500470 unsigned int receive_len;
471 unsigned long timeout;
472 struct mid_q_entry *midQ;
Steve French3e844692005-10-03 13:37:24 -0700473 struct smb_hdr *in_buf = iov[0].iov_base;
Steve French50c2f752007-07-13 00:33:32 +0000474
Steve French133672e2007-11-13 22:41:37 +0000475 long_op = flags & CIFS_TIMEOUT_MASK;
476
Steve Frenchec637e32005-12-12 20:53:18 -0800477 *pRespBufType = CIFS_NO_BUFFER; /* no response buf yet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Steve French4b8f9302006-02-26 16:41:18 +0000479 if ((ses == NULL) || (ses->server == NULL)) {
480 cifs_small_buf_release(in_buf);
Steve French79a58d12007-07-06 22:44:50 +0000481 cERROR(1, ("Null session"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 return -EIO;
483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Steve French79a58d12007-07-06 22:44:50 +0000485 if (ses->server->tcpStatus == CifsExiting) {
Steve French4b8f9302006-02-26 16:41:18 +0000486 cifs_small_buf_release(in_buf);
Steve French31ca3bc2005-04-28 22:41:11 -0700487 return -ENOENT;
Steve French4b8f9302006-02-26 16:41:18 +0000488 }
Steve French31ca3bc2005-04-28 22:41:11 -0700489
Steve French79a58d12007-07-06 22:44:50 +0000490 /* Ensure that we do not send more than 50 overlapping requests
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 to the same server. We may make this configurable later or
492 use ses->maxReq */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000494 rc = wait_for_free_request(ses, long_op);
495 if (rc) {
496 cifs_small_buf_release(in_buf);
497 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 }
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000499
Steve French79a58d12007-07-06 22:44:50 +0000500 /* make sure that we sign in the same order that we send on this socket
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 and avoid races inside tcp sendmsg code that could cause corruption
502 of smb data */
503
Steve French79a58d12007-07-06 22:44:50 +0000504 down(&ses->server->tcpSem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000506 rc = allocate_mid(ses, in_buf, &midQ);
507 if (rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 up(&ses->server->tcpSem);
Steve French4b8f9302006-02-26 16:41:18 +0000509 cifs_small_buf_release(in_buf);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000510 /* Update # of requests on wire to server */
Steve French79a58d12007-07-06 22:44:50 +0000511 atomic_dec(&ses->server->inFlight);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000512 wake_up(&ses->server->request_q);
513 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 }
Steve French79a58d12007-07-06 22:44:50 +0000515 rc = cifs_sign_smb2(iov, n_vec, ses->server, &midQ->sequence_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517 midQ->midState = MID_REQUEST_SUBMITTED;
Steve French131afd0b2005-10-07 09:51:05 -0700518#ifdef CONFIG_CIFS_STATS2
519 atomic_inc(&ses->server->inSend);
520#endif
Steve French3e844692005-10-03 13:37:24 -0700521 rc = smb_send2(ses->server->ssocket, iov, n_vec,
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500522 (struct sockaddr *) &(ses->server->addr.sockAddr));
Steve French131afd0b2005-10-07 09:51:05 -0700523#ifdef CONFIG_CIFS_STATS2
524 atomic_dec(&ses->server->inSend);
Steve French1047abc2005-10-11 19:58:06 -0700525 midQ->when_sent = jiffies;
Steve French131afd0b2005-10-07 09:51:05 -0700526#endif
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000527
528 up(&ses->server->tcpSem);
529 cifs_small_buf_release(in_buf);
530
Steve French79a58d12007-07-06 22:44:50 +0000531 if (rc < 0)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000532 goto out;
Steve French4b8f9302006-02-26 16:41:18 +0000533
Steve French133672e2007-11-13 22:41:37 +0000534 if (long_op == CIFS_STD_OP)
535 timeout = 15 * HZ;
536 else if (long_op == CIFS_VLONG_OP) /* e.g. slow writes past EOF */
Steve French37c0eb42005-10-05 14:50:29 -0700537 timeout = 180 * HZ;
Steve French133672e2007-11-13 22:41:37 +0000538 else if (long_op == CIFS_LONG_OP)
Steve French79a58d12007-07-06 22:44:50 +0000539 timeout = 45 * HZ; /* should be greater than
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500540 servers oplock break timeout (about 43 seconds) */
Steve French133672e2007-11-13 22:41:37 +0000541 else if (long_op == CIFS_ASYNC_OP)
542 goto out;
543 else if (long_op == CIFS_BLOCKING_OP)
544 timeout = 0x7FFFFFFF; /* large, but not so large as to wrap */
545 else {
546 cERROR(1, ("unknown timeout flag %d", long_op));
547 rc = -EIO;
548 goto out;
549 }
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000550
Steve French79a58d12007-07-06 22:44:50 +0000551 /* wait for 15 seconds or until woken up due to response arriving or
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500552 due to last connection to this server being unmounted */
553 if (signal_pending(current)) {
554 /* if signal pending do not hold up user for full smb timeout
Steve French8a236262007-03-06 00:31:00 +0000555 but we still give response a chance to complete */
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500556 timeout = 2 * HZ;
Steve French79a58d12007-07-06 22:44:50 +0000557 }
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500558
559 /* No user interrupts in wait - wreaks havoc with performance */
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000560 wait_for_response(ses, midQ, timeout, 10 * HZ);
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500561
562 spin_lock(&GlobalMid_Lock);
563 if (midQ->resp_buf) {
564 spin_unlock(&GlobalMid_Lock);
Steve French70ca7342005-09-22 16:32:06 -0700565 receive_len = midQ->resp_buf->smb_buf_length;
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500566 } else {
Steve French79a58d12007-07-06 22:44:50 +0000567 cERROR(1, ("No response to cmd %d mid %d",
Steve French37c0eb42005-10-05 14:50:29 -0700568 midQ->command, midQ->mid));
Steve French79a58d12007-07-06 22:44:50 +0000569 if (midQ->midState == MID_REQUEST_SUBMITTED) {
570 if (ses->server->tcpStatus == CifsExiting)
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500571 rc = -EHOSTDOWN;
572 else {
573 ses->server->tcpStatus = CifsNeedReconnect;
574 midQ->midState = MID_RETRY_NEEDED;
575 }
576 }
577
578 if (rc != -EHOSTDOWN) {
Steve French79a58d12007-07-06 22:44:50 +0000579 if (midQ->midState == MID_RETRY_NEEDED) {
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500580 rc = -EAGAIN;
Steve French79a58d12007-07-06 22:44:50 +0000581 cFYI(1, ("marking request for retry"));
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500582 } else {
583 rc = -EIO;
584 }
585 }
586 spin_unlock(&GlobalMid_Lock);
587 DeleteMidQEntry(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000588 /* Update # of requests on wire to server */
Steve French79a58d12007-07-06 22:44:50 +0000589 atomic_dec(&ses->server->inFlight);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000590 wake_up(&ses->server->request_q);
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500591 return rc;
592 }
Steve French50c2f752007-07-13 00:33:32 +0000593
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500594 if (receive_len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE) {
595 cERROR(1, ("Frame too large received. Length: %d Xid: %d",
596 receive_len, xid));
597 rc = -EIO;
598 } else { /* rcvd frame is ok */
Steve French79a58d12007-07-06 22:44:50 +0000599 if (midQ->resp_buf &&
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500600 (midQ->midState == MID_RESPONSE_RECEIVED)) {
Steve French84afc292005-12-02 13:32:45 -0800601
Steve Frenchec637e32005-12-12 20:53:18 -0800602 iov[0].iov_base = (char *)midQ->resp_buf;
Steve French79a58d12007-07-06 22:44:50 +0000603 if (midQ->largeBuf)
Steve Frenchec637e32005-12-12 20:53:18 -0800604 *pRespBufType = CIFS_LARGE_BUFFER;
605 else
606 *pRespBufType = CIFS_SMALL_BUFFER;
607 iov[0].iov_len = receive_len + 4;
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500608
Steve Frenchec637e32005-12-12 20:53:18 -0800609 dump_smb(midQ->resp_buf, 80);
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500610 /* convert the length into a more usable form */
Steve French79a58d12007-07-06 22:44:50 +0000611 if ((receive_len > 24) &&
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500612 (ses->server->secMode & (SECMODE_SIGN_REQUIRED |
613 SECMODE_SIGN_ENABLED))) {
Steve Frenchec637e32005-12-12 20:53:18 -0800614 rc = cifs_verify_signature(midQ->resp_buf,
Steve Frenchb609f062007-07-09 07:55:14 +0000615 &ses->server->mac_signing_key,
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500616 midQ->sequence_number+1);
Steve French79a58d12007-07-06 22:44:50 +0000617 if (rc) {
618 cERROR(1, ("Unexpected SMB signature"));
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500619 /* BB FIXME add code to kill session */
620 }
621 }
622
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500623 /* BB special case reconnect tid and uid here? */
Steve French133672e2007-11-13 22:41:37 +0000624 rc = map_smb_to_linux_error(midQ->resp_buf,
625 flags & CIFS_LOG_ERROR);
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500626
627 /* convert ByteCount if necessary */
Steve French26f57362007-08-30 22:09:15 +0000628 if (receive_len >= sizeof(struct smb_hdr) - 4
629 /* do not count RFC1001 header */ +
Steve Frenchec637e32005-12-12 20:53:18 -0800630 (2 * midQ->resp_buf->WordCount) + 2 /* bcc */ )
Steve French79a58d12007-07-06 22:44:50 +0000631 BCC(midQ->resp_buf) =
Steve Frenchec637e32005-12-12 20:53:18 -0800632 le16_to_cpu(BCC_LE(midQ->resp_buf));
Steve French133672e2007-11-13 22:41:37 +0000633 if ((flags & CIFS_NO_RESP) == 0)
634 midQ->resp_buf = NULL; /* mark it so buf will
635 not be freed by
636 DeleteMidQEntry */
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500637 } else {
638 rc = -EIO;
Steve French79a58d12007-07-06 22:44:50 +0000639 cFYI(1, ("Bad MID state?"));
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500640 }
641 }
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000642
643out:
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500644 DeleteMidQEntry(midQ);
Steve French79a58d12007-07-06 22:44:50 +0000645 atomic_dec(&ses->server->inFlight);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000646 wake_up(&ses->server->request_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648 return rc;
649}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651int
652SendReceive(const unsigned int xid, struct cifsSesInfo *ses,
653 struct smb_hdr *in_buf, struct smb_hdr *out_buf,
654 int *pbytes_returned, const int long_op)
655{
656 int rc = 0;
657 unsigned int receive_len;
658 unsigned long timeout;
659 struct mid_q_entry *midQ;
660
661 if (ses == NULL) {
Steve French79a58d12007-07-06 22:44:50 +0000662 cERROR(1, ("Null smb session"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 return -EIO;
664 }
Steve French79a58d12007-07-06 22:44:50 +0000665 if (ses->server == NULL) {
666 cERROR(1, ("Null tcp session"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 return -EIO;
668 }
669
Steve French79a58d12007-07-06 22:44:50 +0000670 if (ses->server->tcpStatus == CifsExiting)
Steve French31ca3bc2005-04-28 22:41:11 -0700671 return -ENOENT;
672
Steve French79a58d12007-07-06 22:44:50 +0000673 /* Ensure that we do not send more than 50 overlapping requests
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 to the same server. We may make this configurable later or
675 use ses->maxReq */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000677 rc = wait_for_free_request(ses, long_op);
678 if (rc)
679 return rc;
680
Steve French79a58d12007-07-06 22:44:50 +0000681 /* make sure that we sign in the same order that we send on this socket
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 and avoid races inside tcp sendmsg code that could cause corruption
683 of smb data */
684
Steve French79a58d12007-07-06 22:44:50 +0000685 down(&ses->server->tcpSem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000687 rc = allocate_mid(ses, in_buf, &midQ);
688 if (rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 up(&ses->server->tcpSem);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000690 /* Update # of requests on wire to server */
Steve French79a58d12007-07-06 22:44:50 +0000691 atomic_dec(&ses->server->inFlight);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000692 wake_up(&ses->server->request_q);
693 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 }
695
696 if (in_buf->smb_buf_length > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
Steve French26a21b92006-05-31 18:05:34 +0000697 cERROR(1, ("Illegal length, greater than maximum frame, %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 in_buf->smb_buf_length));
699 DeleteMidQEntry(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000700 up(&ses->server->tcpSem);
701 /* Update # of requests on wire to server */
Steve French79a58d12007-07-06 22:44:50 +0000702 atomic_dec(&ses->server->inFlight);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000703 wake_up(&ses->server->request_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 return -EIO;
705 }
706
Steve Frenchad009ac2005-04-28 22:41:05 -0700707 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 midQ->midState = MID_REQUEST_SUBMITTED;
Steve French131afd0b2005-10-07 09:51:05 -0700710#ifdef CONFIG_CIFS_STATS2
711 atomic_inc(&ses->server->inSend);
712#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 rc = smb_send(ses->server->ssocket, in_buf, in_buf->smb_buf_length,
714 (struct sockaddr *) &(ses->server->addr.sockAddr));
Steve French131afd0b2005-10-07 09:51:05 -0700715#ifdef CONFIG_CIFS_STATS2
716 atomic_dec(&ses->server->inSend);
Steve French1047abc2005-10-11 19:58:06 -0700717 midQ->when_sent = jiffies;
Steve French131afd0b2005-10-07 09:51:05 -0700718#endif
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000719 up(&ses->server->tcpSem);
720
Steve French79a58d12007-07-06 22:44:50 +0000721 if (rc < 0)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000722 goto out;
723
Steve French133672e2007-11-13 22:41:37 +0000724 if (long_op == CIFS_STD_OP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 timeout = 15 * HZ;
Steve French79a58d12007-07-06 22:44:50 +0000726 /* wait for 15 seconds or until woken up due to response arriving or
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 due to last connection to this server being unmounted */
Steve French133672e2007-11-13 22:41:37 +0000728 else if (long_op == CIFS_ASYNC_OP)
729 goto out;
730 else if (long_op == CIFS_VLONG_OP) /* writes past EOF can be slow */
731 timeout = 180 * HZ;
732 else if (long_op == CIFS_LONG_OP)
733 timeout = 45 * HZ; /* should be greater than
734 servers oplock break timeout (about 43 seconds) */
735 else if (long_op == CIFS_BLOCKING_OP)
736 timeout = 0x7FFFFFFF; /* large but no so large as to wrap */
737 else {
738 cERROR(1, ("unknown timeout flag %d", long_op));
739 rc = -EIO;
740 goto out;
741 }
742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 if (signal_pending(current)) {
744 /* if signal pending do not hold up user for full smb timeout
Steve French8a236262007-03-06 00:31:00 +0000745 but we still give response a chance to complete */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 timeout = 2 * HZ;
Steve French79a58d12007-07-06 22:44:50 +0000747 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749 /* No user interrupts in wait - wreaks havoc with performance */
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000750 wait_for_response(ses, midQ, timeout, 10 * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
752 spin_lock(&GlobalMid_Lock);
753 if (midQ->resp_buf) {
754 spin_unlock(&GlobalMid_Lock);
Steve French70ca7342005-09-22 16:32:06 -0700755 receive_len = midQ->resp_buf->smb_buf_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 } else {
Steve French79a58d12007-07-06 22:44:50 +0000757 cERROR(1, ("No response for cmd %d mid %d",
Steve French37c0eb42005-10-05 14:50:29 -0700758 midQ->command, midQ->mid));
Steve French79a58d12007-07-06 22:44:50 +0000759 if (midQ->midState == MID_REQUEST_SUBMITTED) {
760 if (ses->server->tcpStatus == CifsExiting)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 rc = -EHOSTDOWN;
762 else {
763 ses->server->tcpStatus = CifsNeedReconnect;
764 midQ->midState = MID_RETRY_NEEDED;
765 }
766 }
767
768 if (rc != -EHOSTDOWN) {
Steve French79a58d12007-07-06 22:44:50 +0000769 if (midQ->midState == MID_RETRY_NEEDED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 rc = -EAGAIN;
Steve French79a58d12007-07-06 22:44:50 +0000771 cFYI(1, ("marking request for retry"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 } else {
773 rc = -EIO;
774 }
775 }
776 spin_unlock(&GlobalMid_Lock);
777 DeleteMidQEntry(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000778 /* Update # of requests on wire to server */
Steve French79a58d12007-07-06 22:44:50 +0000779 atomic_dec(&ses->server->inFlight);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000780 wake_up(&ses->server->request_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 return rc;
782 }
Steve French50c2f752007-07-13 00:33:32 +0000783
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 if (receive_len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE) {
Steve Frenchad009ac2005-04-28 22:41:05 -0700785 cERROR(1, ("Frame too large received. Length: %d Xid: %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 receive_len, xid));
787 rc = -EIO;
788 } else { /* rcvd frame is ok */
789
790 if (midQ->resp_buf && out_buf
791 && (midQ->midState == MID_RESPONSE_RECEIVED)) {
792 out_buf->smb_buf_length = receive_len;
793 memcpy((char *)out_buf + 4,
794 (char *)midQ->resp_buf + 4,
795 receive_len);
796
797 dump_smb(out_buf, 92);
798 /* convert the length into a more usable form */
Steve French79a58d12007-07-06 22:44:50 +0000799 if ((receive_len > 24) &&
Steve Frenchad009ac2005-04-28 22:41:05 -0700800 (ses->server->secMode & (SECMODE_SIGN_REQUIRED |
801 SECMODE_SIGN_ENABLED))) {
802 rc = cifs_verify_signature(out_buf,
Steve Frenchb609f062007-07-09 07:55:14 +0000803 &ses->server->mac_signing_key,
Steve Frenchad009ac2005-04-28 22:41:05 -0700804 midQ->sequence_number+1);
Steve French79a58d12007-07-06 22:44:50 +0000805 if (rc) {
806 cERROR(1, ("Unexpected SMB signature"));
Steve French275cde12005-04-28 22:41:10 -0700807 /* BB FIXME add code to kill session */
Steve Frenchad009ac2005-04-28 22:41:05 -0700808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 }
810
811 *pbytes_returned = out_buf->smb_buf_length;
812
Steve Frenchad009ac2005-04-28 22:41:05 -0700813 /* BB special case reconnect tid and uid here? */
Steve Frencha761ac52007-10-18 21:45:27 +0000814 rc = map_smb_to_linux_error(out_buf, 0 /* no log */ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
816 /* convert ByteCount if necessary */
Steve French26f57362007-08-30 22:09:15 +0000817 if (receive_len >= sizeof(struct smb_hdr) - 4
818 /* do not count RFC1001 header */ +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 (2 * out_buf->WordCount) + 2 /* bcc */ )
Steve French0f2b27c2005-11-16 14:25:50 -0800820 BCC(out_buf) = le16_to_cpu(BCC_LE(out_buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 } else {
822 rc = -EIO;
Steve French79a58d12007-07-06 22:44:50 +0000823 cERROR(1, ("Bad MID state?"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 }
825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000827out:
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000828 DeleteMidQEntry(midQ);
Steve French79a58d12007-07-06 22:44:50 +0000829 atomic_dec(&ses->server->inFlight);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000830 wake_up(&ses->server->request_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
832 return rc;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000833}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000835/* Send an NT_CANCEL SMB to cause the POSIX blocking lock to return. */
836
837static int
838send_nt_cancel(struct cifsTconInfo *tcon, struct smb_hdr *in_buf,
839 struct mid_q_entry *midQ)
840{
841 int rc = 0;
842 struct cifsSesInfo *ses = tcon->ses;
843 __u16 mid = in_buf->Mid;
844
845 header_assemble(in_buf, SMB_COM_NT_CANCEL, tcon, 0);
846 in_buf->Mid = mid;
Steve French79a58d12007-07-06 22:44:50 +0000847 down(&ses->server->tcpSem);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000848 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
849 if (rc) {
850 up(&ses->server->tcpSem);
851 return rc;
852 }
853 rc = smb_send(ses->server->ssocket, in_buf, in_buf->smb_buf_length,
854 (struct sockaddr *) &(ses->server->addr.sockAddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 up(&ses->server->tcpSem);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000856 return rc;
857}
858
859/* We send a LOCKINGX_CANCEL_LOCK to cause the Windows
860 blocking lock to return. */
861
862static int
863send_lock_cancel(const unsigned int xid, struct cifsTconInfo *tcon,
864 struct smb_hdr *in_buf,
865 struct smb_hdr *out_buf)
866{
867 int bytes_returned;
868 struct cifsSesInfo *ses = tcon->ses;
869 LOCK_REQ *pSMB = (LOCK_REQ *)in_buf;
870
871 /* We just modify the current in_buf to change
872 the type of lock from LOCKING_ANDX_SHARED_LOCK
873 or LOCKING_ANDX_EXCLUSIVE_LOCK to
874 LOCKING_ANDX_CANCEL_LOCK. */
875
876 pSMB->LockType = LOCKING_ANDX_CANCEL_LOCK|LOCKING_ANDX_LARGE_FILES;
877 pSMB->Timeout = 0;
878 pSMB->hdr.Mid = GetNextMid(ses->server);
879
880 return SendReceive(xid, ses, in_buf, out_buf,
Steve French133672e2007-11-13 22:41:37 +0000881 &bytes_returned, CIFS_STD_OP);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000882}
883
884int
885SendReceiveBlockingLock(const unsigned int xid, struct cifsTconInfo *tcon,
886 struct smb_hdr *in_buf, struct smb_hdr *out_buf,
887 int *pbytes_returned)
888{
889 int rc = 0;
890 int rstart = 0;
891 unsigned int receive_len;
892 struct mid_q_entry *midQ;
893 struct cifsSesInfo *ses;
894
895 if (tcon == NULL || tcon->ses == NULL) {
Steve French79a58d12007-07-06 22:44:50 +0000896 cERROR(1, ("Null smb session"));
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000897 return -EIO;
898 }
899 ses = tcon->ses;
900
Steve French79a58d12007-07-06 22:44:50 +0000901 if (ses->server == NULL) {
902 cERROR(1, ("Null tcp session"));
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000903 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 }
905
Steve French79a58d12007-07-06 22:44:50 +0000906 if (ses->server->tcpStatus == CifsExiting)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000907 return -ENOENT;
908
Steve French79a58d12007-07-06 22:44:50 +0000909 /* Ensure that we do not send more than 50 overlapping requests
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000910 to the same server. We may make this configurable later or
911 use ses->maxReq */
912
Steve French133672e2007-11-13 22:41:37 +0000913 rc = wait_for_free_request(ses, CIFS_BLOCKING_OP);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000914 if (rc)
915 return rc;
916
Steve French79a58d12007-07-06 22:44:50 +0000917 /* make sure that we sign in the same order that we send on this socket
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000918 and avoid races inside tcp sendmsg code that could cause corruption
919 of smb data */
920
Steve French79a58d12007-07-06 22:44:50 +0000921 down(&ses->server->tcpSem);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000922
923 rc = allocate_mid(ses, in_buf, &midQ);
924 if (rc) {
925 up(&ses->server->tcpSem);
926 return rc;
927 }
928
929 if (in_buf->smb_buf_length > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
930 up(&ses->server->tcpSem);
931 cERROR(1, ("Illegal length, greater than maximum frame, %d",
932 in_buf->smb_buf_length));
933 DeleteMidQEntry(midQ);
934 return -EIO;
935 }
936
937 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
938
939 midQ->midState = MID_REQUEST_SUBMITTED;
940#ifdef CONFIG_CIFS_STATS2
941 atomic_inc(&ses->server->inSend);
942#endif
943 rc = smb_send(ses->server->ssocket, in_buf, in_buf->smb_buf_length,
944 (struct sockaddr *) &(ses->server->addr.sockAddr));
945#ifdef CONFIG_CIFS_STATS2
946 atomic_dec(&ses->server->inSend);
947 midQ->when_sent = jiffies;
948#endif
949 up(&ses->server->tcpSem);
950
Steve French79a58d12007-07-06 22:44:50 +0000951 if (rc < 0) {
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000952 DeleteMidQEntry(midQ);
953 return rc;
954 }
955
956 /* Wait for a reply - allow signals to interrupt. */
957 rc = wait_event_interruptible(ses->server->response_q,
Steve French79a58d12007-07-06 22:44:50 +0000958 (!(midQ->midState == MID_REQUEST_SUBMITTED)) ||
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000959 ((ses->server->tcpStatus != CifsGood) &&
960 (ses->server->tcpStatus != CifsNew)));
961
962 /* Were we interrupted by a signal ? */
963 if ((rc == -ERESTARTSYS) &&
964 (midQ->midState == MID_REQUEST_SUBMITTED) &&
965 ((ses->server->tcpStatus == CifsGood) ||
966 (ses->server->tcpStatus == CifsNew))) {
967
968 if (in_buf->Command == SMB_COM_TRANSACTION2) {
969 /* POSIX lock. We send a NT_CANCEL SMB to cause the
970 blocking lock to return. */
971
972 rc = send_nt_cancel(tcon, in_buf, midQ);
973 if (rc) {
974 DeleteMidQEntry(midQ);
975 return rc;
976 }
977 } else {
978 /* Windows lock. We send a LOCKINGX_CANCEL_LOCK
979 to cause the blocking lock to return. */
980
981 rc = send_lock_cancel(xid, tcon, in_buf, out_buf);
982
983 /* If we get -ENOLCK back the lock may have
984 already been removed. Don't exit in this case. */
985 if (rc && rc != -ENOLCK) {
986 DeleteMidQEntry(midQ);
987 return rc;
988 }
989 }
990
991 /* Wait 5 seconds for the response. */
Steve French79a58d12007-07-06 22:44:50 +0000992 if (wait_for_response(ses, midQ, 5 * HZ, 5 * HZ) == 0) {
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000993 /* We got the response - restart system call. */
994 rstart = 1;
995 }
996 }
997
998 spin_lock(&GlobalMid_Lock);
999 if (midQ->resp_buf) {
1000 spin_unlock(&GlobalMid_Lock);
1001 receive_len = midQ->resp_buf->smb_buf_length;
1002 } else {
Steve French79a58d12007-07-06 22:44:50 +00001003 cERROR(1, ("No response for cmd %d mid %d",
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001004 midQ->command, midQ->mid));
Steve French79a58d12007-07-06 22:44:50 +00001005 if (midQ->midState == MID_REQUEST_SUBMITTED) {
1006 if (ses->server->tcpStatus == CifsExiting)
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001007 rc = -EHOSTDOWN;
1008 else {
1009 ses->server->tcpStatus = CifsNeedReconnect;
1010 midQ->midState = MID_RETRY_NEEDED;
1011 }
1012 }
1013
1014 if (rc != -EHOSTDOWN) {
Steve French79a58d12007-07-06 22:44:50 +00001015 if (midQ->midState == MID_RETRY_NEEDED) {
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001016 rc = -EAGAIN;
Steve French79a58d12007-07-06 22:44:50 +00001017 cFYI(1, ("marking request for retry"));
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001018 } else {
1019 rc = -EIO;
1020 }
1021 }
1022 spin_unlock(&GlobalMid_Lock);
1023 DeleteMidQEntry(midQ);
1024 return rc;
1025 }
Steve French50c2f752007-07-13 00:33:32 +00001026
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001027 if (receive_len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE) {
1028 cERROR(1, ("Frame too large received. Length: %d Xid: %d",
1029 receive_len, xid));
1030 rc = -EIO;
1031 } else { /* rcvd frame is ok */
1032
1033 if (midQ->resp_buf && out_buf
1034 && (midQ->midState == MID_RESPONSE_RECEIVED)) {
1035 out_buf->smb_buf_length = receive_len;
1036 memcpy((char *)out_buf + 4,
1037 (char *)midQ->resp_buf + 4,
1038 receive_len);
1039
1040 dump_smb(out_buf, 92);
1041 /* convert the length into a more usable form */
Steve French79a58d12007-07-06 22:44:50 +00001042 if ((receive_len > 24) &&
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001043 (ses->server->secMode & (SECMODE_SIGN_REQUIRED |
1044 SECMODE_SIGN_ENABLED))) {
1045 rc = cifs_verify_signature(out_buf,
Steve Frenchb609f062007-07-09 07:55:14 +00001046 &ses->server->mac_signing_key,
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001047 midQ->sequence_number+1);
Steve French79a58d12007-07-06 22:44:50 +00001048 if (rc) {
1049 cERROR(1, ("Unexpected SMB signature"));
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001050 /* BB FIXME add code to kill session */
1051 }
1052 }
1053
1054 *pbytes_returned = out_buf->smb_buf_length;
1055
1056 /* BB special case reconnect tid and uid here? */
Steve Frencha761ac52007-10-18 21:45:27 +00001057 rc = map_smb_to_linux_error(out_buf, 0 /* no log */ );
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001058
1059 /* convert ByteCount if necessary */
Steve French26f57362007-08-30 22:09:15 +00001060 if (receive_len >= sizeof(struct smb_hdr) - 4
1061 /* do not count RFC1001 header */ +
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001062 (2 * out_buf->WordCount) + 2 /* bcc */ )
1063 BCC(out_buf) = le16_to_cpu(BCC_LE(out_buf));
1064 } else {
1065 rc = -EIO;
Steve French79a58d12007-07-06 22:44:50 +00001066 cERROR(1, ("Bad MID state?"));
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001067 }
1068 }
1069 DeleteMidQEntry(midQ);
1070 if (rstart && rc == -EACCES)
1071 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 return rc;
1073}