blob: 48cdab0e70ed7bebbecfa022bedc4162a230f659 [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
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 French133672e2007-11-13 22:41:37 +0000311 if (long_op == CIFS_ASYNC_OP) {
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 */
Steve French133672e2007-11-13 22:41:37 +0000340 if (long_op != CIFS_BLOCKING_OP)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000341 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) &&
Steve Frenchad7a2922008-02-07 23:25:02 +0000361 (in_buf->Command != SMB_COM_NEGOTIATE))
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000362 return -EAGAIN;
Steve Frenchad7a2922008-02-07 23:25:02 +0000363 /* else ok - we are setting up session */
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000364 }
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
Steve French133672e2007-11-13 22:41:37 +0000418
419/*
420 *
421 * Send an SMB Request. No response info (other than return code)
422 * needs to be parsed.
423 *
424 * flags indicate the type of request buffer and how long to wait
425 * and whether to log NT STATUS code (error) before mapping it to POSIX error
426 *
427 */
428int
429SendReceiveNoRsp(const unsigned int xid, struct cifsSesInfo *ses,
430 struct smb_hdr *in_buf, int flags)
431{
432 int rc;
433 struct kvec iov[1];
434 int resp_buf_type;
435
436 iov[0].iov_base = (char *)in_buf;
437 iov[0].iov_len = in_buf->smb_buf_length + 4;
438 flags |= CIFS_NO_RESP;
439 rc = SendReceive2(xid, ses, iov, 1, &resp_buf_type, flags);
440#ifdef CONFIG_CIFS_DEBUG2
441 cFYI(1, ("SendRcvNoR flags %d rc %d", flags, rc));
442#endif
443 return rc;
444}
445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446int
Steve French79a58d12007-07-06 22:44:50 +0000447SendReceive2(const unsigned int xid, struct cifsSesInfo *ses,
448 struct kvec *iov, int n_vec, int *pRespBufType /* ret */,
Steve French133672e2007-11-13 22:41:37 +0000449 const int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
451 int rc = 0;
Steve French133672e2007-11-13 22:41:37 +0000452 int long_op;
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500453 unsigned int receive_len;
454 unsigned long timeout;
455 struct mid_q_entry *midQ;
Steve French3e844692005-10-03 13:37:24 -0700456 struct smb_hdr *in_buf = iov[0].iov_base;
Steve French50c2f752007-07-13 00:33:32 +0000457
Steve French133672e2007-11-13 22:41:37 +0000458 long_op = flags & CIFS_TIMEOUT_MASK;
459
Steve Frenchec637e32005-12-12 20:53:18 -0800460 *pRespBufType = CIFS_NO_BUFFER; /* no response buf yet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Steve French4b8f9302006-02-26 16:41:18 +0000462 if ((ses == NULL) || (ses->server == NULL)) {
463 cifs_small_buf_release(in_buf);
Steve French79a58d12007-07-06 22:44:50 +0000464 cERROR(1, ("Null session"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 return -EIO;
466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Steve French79a58d12007-07-06 22:44:50 +0000468 if (ses->server->tcpStatus == CifsExiting) {
Steve French4b8f9302006-02-26 16:41:18 +0000469 cifs_small_buf_release(in_buf);
Steve French31ca3bc2005-04-28 22:41:11 -0700470 return -ENOENT;
Steve French4b8f9302006-02-26 16:41:18 +0000471 }
Steve French31ca3bc2005-04-28 22:41:11 -0700472
Steve French79a58d12007-07-06 22:44:50 +0000473 /* Ensure that we do not send more than 50 overlapping requests
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 to the same server. We may make this configurable later or
475 use ses->maxReq */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000477 rc = wait_for_free_request(ses, long_op);
478 if (rc) {
479 cifs_small_buf_release(in_buf);
480 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 }
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000482
Steve French79a58d12007-07-06 22:44:50 +0000483 /* make sure that we sign in the same order that we send on this socket
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 and avoid races inside tcp sendmsg code that could cause corruption
485 of smb data */
486
Steve French79a58d12007-07-06 22:44:50 +0000487 down(&ses->server->tcpSem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000489 rc = allocate_mid(ses, in_buf, &midQ);
490 if (rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 up(&ses->server->tcpSem);
Steve French4b8f9302006-02-26 16:41:18 +0000492 cifs_small_buf_release(in_buf);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000493 /* Update # of requests on wire to server */
Steve French79a58d12007-07-06 22:44:50 +0000494 atomic_dec(&ses->server->inFlight);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000495 wake_up(&ses->server->request_q);
496 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 }
Steve French79a58d12007-07-06 22:44:50 +0000498 rc = cifs_sign_smb2(iov, n_vec, ses->server, &midQ->sequence_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500 midQ->midState = MID_REQUEST_SUBMITTED;
Steve French131afd0b2005-10-07 09:51:05 -0700501#ifdef CONFIG_CIFS_STATS2
502 atomic_inc(&ses->server->inSend);
503#endif
Steve French3e844692005-10-03 13:37:24 -0700504 rc = smb_send2(ses->server->ssocket, iov, n_vec,
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500505 (struct sockaddr *) &(ses->server->addr.sockAddr));
Steve French131afd0b2005-10-07 09:51:05 -0700506#ifdef CONFIG_CIFS_STATS2
507 atomic_dec(&ses->server->inSend);
Steve French1047abc2005-10-11 19:58:06 -0700508 midQ->when_sent = jiffies;
Steve French131afd0b2005-10-07 09:51:05 -0700509#endif
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000510
511 up(&ses->server->tcpSem);
512 cifs_small_buf_release(in_buf);
513
Steve French79a58d12007-07-06 22:44:50 +0000514 if (rc < 0)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000515 goto out;
Steve French4b8f9302006-02-26 16:41:18 +0000516
Steve French133672e2007-11-13 22:41:37 +0000517 if (long_op == CIFS_STD_OP)
518 timeout = 15 * HZ;
519 else if (long_op == CIFS_VLONG_OP) /* e.g. slow writes past EOF */
Steve French37c0eb42005-10-05 14:50:29 -0700520 timeout = 180 * HZ;
Steve French133672e2007-11-13 22:41:37 +0000521 else if (long_op == CIFS_LONG_OP)
Steve French79a58d12007-07-06 22:44:50 +0000522 timeout = 45 * HZ; /* should be greater than
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500523 servers oplock break timeout (about 43 seconds) */
Steve French133672e2007-11-13 22:41:37 +0000524 else if (long_op == CIFS_ASYNC_OP)
525 goto out;
526 else if (long_op == CIFS_BLOCKING_OP)
527 timeout = 0x7FFFFFFF; /* large, but not so large as to wrap */
528 else {
529 cERROR(1, ("unknown timeout flag %d", long_op));
530 rc = -EIO;
531 goto out;
532 }
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000533
Steve French79a58d12007-07-06 22:44:50 +0000534 /* wait for 15 seconds or until woken up due to response arriving or
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500535 due to last connection to this server being unmounted */
536 if (signal_pending(current)) {
537 /* if signal pending do not hold up user for full smb timeout
Steve French8a236262007-03-06 00:31:00 +0000538 but we still give response a chance to complete */
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500539 timeout = 2 * HZ;
Steve French79a58d12007-07-06 22:44:50 +0000540 }
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500541
542 /* No user interrupts in wait - wreaks havoc with performance */
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000543 wait_for_response(ses, midQ, timeout, 10 * HZ);
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500544
545 spin_lock(&GlobalMid_Lock);
546 if (midQ->resp_buf) {
547 spin_unlock(&GlobalMid_Lock);
Steve French70ca7342005-09-22 16:32:06 -0700548 receive_len = midQ->resp_buf->smb_buf_length;
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500549 } else {
Steve French79a58d12007-07-06 22:44:50 +0000550 cERROR(1, ("No response to cmd %d mid %d",
Steve French37c0eb42005-10-05 14:50:29 -0700551 midQ->command, midQ->mid));
Steve French79a58d12007-07-06 22:44:50 +0000552 if (midQ->midState == MID_REQUEST_SUBMITTED) {
553 if (ses->server->tcpStatus == CifsExiting)
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500554 rc = -EHOSTDOWN;
555 else {
556 ses->server->tcpStatus = CifsNeedReconnect;
557 midQ->midState = MID_RETRY_NEEDED;
558 }
559 }
560
561 if (rc != -EHOSTDOWN) {
Steve French79a58d12007-07-06 22:44:50 +0000562 if (midQ->midState == MID_RETRY_NEEDED) {
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500563 rc = -EAGAIN;
Steve French79a58d12007-07-06 22:44:50 +0000564 cFYI(1, ("marking request for retry"));
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500565 } else {
566 rc = -EIO;
567 }
568 }
569 spin_unlock(&GlobalMid_Lock);
570 DeleteMidQEntry(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000571 /* Update # of requests on wire to server */
Steve French79a58d12007-07-06 22:44:50 +0000572 atomic_dec(&ses->server->inFlight);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000573 wake_up(&ses->server->request_q);
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500574 return rc;
575 }
Steve French50c2f752007-07-13 00:33:32 +0000576
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500577 if (receive_len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE) {
578 cERROR(1, ("Frame too large received. Length: %d Xid: %d",
579 receive_len, xid));
580 rc = -EIO;
581 } else { /* rcvd frame is ok */
Steve French79a58d12007-07-06 22:44:50 +0000582 if (midQ->resp_buf &&
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500583 (midQ->midState == MID_RESPONSE_RECEIVED)) {
Steve French84afc292005-12-02 13:32:45 -0800584
Steve Frenchec637e32005-12-12 20:53:18 -0800585 iov[0].iov_base = (char *)midQ->resp_buf;
Steve French79a58d12007-07-06 22:44:50 +0000586 if (midQ->largeBuf)
Steve Frenchec637e32005-12-12 20:53:18 -0800587 *pRespBufType = CIFS_LARGE_BUFFER;
588 else
589 *pRespBufType = CIFS_SMALL_BUFFER;
590 iov[0].iov_len = receive_len + 4;
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500591
Steve Frenchec637e32005-12-12 20:53:18 -0800592 dump_smb(midQ->resp_buf, 80);
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500593 /* convert the length into a more usable form */
Steve French79a58d12007-07-06 22:44:50 +0000594 if ((receive_len > 24) &&
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500595 (ses->server->secMode & (SECMODE_SIGN_REQUIRED |
596 SECMODE_SIGN_ENABLED))) {
Steve Frenchec637e32005-12-12 20:53:18 -0800597 rc = cifs_verify_signature(midQ->resp_buf,
Steve Frenchb609f062007-07-09 07:55:14 +0000598 &ses->server->mac_signing_key,
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500599 midQ->sequence_number+1);
Steve French79a58d12007-07-06 22:44:50 +0000600 if (rc) {
601 cERROR(1, ("Unexpected SMB signature"));
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500602 /* BB FIXME add code to kill session */
603 }
604 }
605
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500606 /* BB special case reconnect tid and uid here? */
Steve French133672e2007-11-13 22:41:37 +0000607 rc = map_smb_to_linux_error(midQ->resp_buf,
608 flags & CIFS_LOG_ERROR);
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500609
610 /* convert ByteCount if necessary */
Steve French26f57362007-08-30 22:09:15 +0000611 if (receive_len >= sizeof(struct smb_hdr) - 4
612 /* do not count RFC1001 header */ +
Steve Frenchec637e32005-12-12 20:53:18 -0800613 (2 * midQ->resp_buf->WordCount) + 2 /* bcc */ )
Steve French79a58d12007-07-06 22:44:50 +0000614 BCC(midQ->resp_buf) =
Steve Frenchec637e32005-12-12 20:53:18 -0800615 le16_to_cpu(BCC_LE(midQ->resp_buf));
Steve French133672e2007-11-13 22:41:37 +0000616 if ((flags & CIFS_NO_RESP) == 0)
617 midQ->resp_buf = NULL; /* mark it so buf will
618 not be freed by
619 DeleteMidQEntry */
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500620 } else {
621 rc = -EIO;
Steve French79a58d12007-07-06 22:44:50 +0000622 cFYI(1, ("Bad MID state?"));
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500623 }
624 }
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000625
626out:
Steve Frenchd6e04ae2005-06-13 13:24:43 -0500627 DeleteMidQEntry(midQ);
Steve French79a58d12007-07-06 22:44:50 +0000628 atomic_dec(&ses->server->inFlight);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000629 wake_up(&ses->server->request_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
631 return rc;
632}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634int
635SendReceive(const unsigned int xid, struct cifsSesInfo *ses,
636 struct smb_hdr *in_buf, struct smb_hdr *out_buf,
637 int *pbytes_returned, const int long_op)
638{
639 int rc = 0;
640 unsigned int receive_len;
641 unsigned long timeout;
642 struct mid_q_entry *midQ;
643
644 if (ses == NULL) {
Steve French79a58d12007-07-06 22:44:50 +0000645 cERROR(1, ("Null smb session"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 return -EIO;
647 }
Steve French79a58d12007-07-06 22:44:50 +0000648 if (ses->server == NULL) {
649 cERROR(1, ("Null tcp session"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 return -EIO;
651 }
652
Steve French79a58d12007-07-06 22:44:50 +0000653 if (ses->server->tcpStatus == CifsExiting)
Steve French31ca3bc2005-04-28 22:41:11 -0700654 return -ENOENT;
655
Steve French79a58d12007-07-06 22:44:50 +0000656 /* Ensure that we do not send more than 50 overlapping requests
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 to the same server. We may make this configurable later or
658 use ses->maxReq */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000660 rc = wait_for_free_request(ses, long_op);
661 if (rc)
662 return rc;
663
Steve French79a58d12007-07-06 22:44:50 +0000664 /* make sure that we sign in the same order that we send on this socket
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 and avoid races inside tcp sendmsg code that could cause corruption
666 of smb data */
667
Steve French79a58d12007-07-06 22:44:50 +0000668 down(&ses->server->tcpSem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000670 rc = allocate_mid(ses, in_buf, &midQ);
671 if (rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 up(&ses->server->tcpSem);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000673 /* Update # of requests on wire to server */
Steve French79a58d12007-07-06 22:44:50 +0000674 atomic_dec(&ses->server->inFlight);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000675 wake_up(&ses->server->request_q);
676 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 }
678
679 if (in_buf->smb_buf_length > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
Steve French26a21b92006-05-31 18:05:34 +0000680 cERROR(1, ("Illegal length, greater than maximum frame, %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 in_buf->smb_buf_length));
682 DeleteMidQEntry(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000683 up(&ses->server->tcpSem);
684 /* Update # of requests on wire to server */
Steve French79a58d12007-07-06 22:44:50 +0000685 atomic_dec(&ses->server->inFlight);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000686 wake_up(&ses->server->request_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 return -EIO;
688 }
689
Steve Frenchad009ac2005-04-28 22:41:05 -0700690 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692 midQ->midState = MID_REQUEST_SUBMITTED;
Steve French131afd0b2005-10-07 09:51:05 -0700693#ifdef CONFIG_CIFS_STATS2
694 atomic_inc(&ses->server->inSend);
695#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 rc = smb_send(ses->server->ssocket, in_buf, in_buf->smb_buf_length,
697 (struct sockaddr *) &(ses->server->addr.sockAddr));
Steve French131afd0b2005-10-07 09:51:05 -0700698#ifdef CONFIG_CIFS_STATS2
699 atomic_dec(&ses->server->inSend);
Steve French1047abc2005-10-11 19:58:06 -0700700 midQ->when_sent = jiffies;
Steve French131afd0b2005-10-07 09:51:05 -0700701#endif
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000702 up(&ses->server->tcpSem);
703
Steve French79a58d12007-07-06 22:44:50 +0000704 if (rc < 0)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000705 goto out;
706
Steve French133672e2007-11-13 22:41:37 +0000707 if (long_op == CIFS_STD_OP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 timeout = 15 * HZ;
Steve French79a58d12007-07-06 22:44:50 +0000709 /* wait for 15 seconds or until woken up due to response arriving or
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 due to last connection to this server being unmounted */
Steve French133672e2007-11-13 22:41:37 +0000711 else if (long_op == CIFS_ASYNC_OP)
712 goto out;
713 else if (long_op == CIFS_VLONG_OP) /* writes past EOF can be slow */
714 timeout = 180 * HZ;
715 else if (long_op == CIFS_LONG_OP)
716 timeout = 45 * HZ; /* should be greater than
717 servers oplock break timeout (about 43 seconds) */
718 else if (long_op == CIFS_BLOCKING_OP)
719 timeout = 0x7FFFFFFF; /* large but no so large as to wrap */
720 else {
721 cERROR(1, ("unknown timeout flag %d", long_op));
722 rc = -EIO;
723 goto out;
724 }
725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 if (signal_pending(current)) {
727 /* if signal pending do not hold up user for full smb timeout
Steve French8a236262007-03-06 00:31:00 +0000728 but we still give response a chance to complete */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 timeout = 2 * HZ;
Steve French79a58d12007-07-06 22:44:50 +0000730 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
732 /* No user interrupts in wait - wreaks havoc with performance */
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000733 wait_for_response(ses, midQ, timeout, 10 * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
735 spin_lock(&GlobalMid_Lock);
736 if (midQ->resp_buf) {
737 spin_unlock(&GlobalMid_Lock);
Steve French70ca7342005-09-22 16:32:06 -0700738 receive_len = midQ->resp_buf->smb_buf_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 } else {
Steve French79a58d12007-07-06 22:44:50 +0000740 cERROR(1, ("No response for cmd %d mid %d",
Steve French37c0eb42005-10-05 14:50:29 -0700741 midQ->command, midQ->mid));
Steve French79a58d12007-07-06 22:44:50 +0000742 if (midQ->midState == MID_REQUEST_SUBMITTED) {
743 if (ses->server->tcpStatus == CifsExiting)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 rc = -EHOSTDOWN;
745 else {
746 ses->server->tcpStatus = CifsNeedReconnect;
747 midQ->midState = MID_RETRY_NEEDED;
748 }
749 }
750
751 if (rc != -EHOSTDOWN) {
Steve French79a58d12007-07-06 22:44:50 +0000752 if (midQ->midState == MID_RETRY_NEEDED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 rc = -EAGAIN;
Steve French79a58d12007-07-06 22:44:50 +0000754 cFYI(1, ("marking request for retry"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 } else {
756 rc = -EIO;
757 }
758 }
759 spin_unlock(&GlobalMid_Lock);
760 DeleteMidQEntry(midQ);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000761 /* Update # of requests on wire to server */
Steve French79a58d12007-07-06 22:44:50 +0000762 atomic_dec(&ses->server->inFlight);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000763 wake_up(&ses->server->request_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 return rc;
765 }
Steve French50c2f752007-07-13 00:33:32 +0000766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 if (receive_len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE) {
Steve Frenchad009ac2005-04-28 22:41:05 -0700768 cERROR(1, ("Frame too large received. Length: %d Xid: %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 receive_len, xid));
770 rc = -EIO;
771 } else { /* rcvd frame is ok */
772
773 if (midQ->resp_buf && out_buf
774 && (midQ->midState == MID_RESPONSE_RECEIVED)) {
775 out_buf->smb_buf_length = receive_len;
776 memcpy((char *)out_buf + 4,
777 (char *)midQ->resp_buf + 4,
778 receive_len);
779
780 dump_smb(out_buf, 92);
781 /* convert the length into a more usable form */
Steve French79a58d12007-07-06 22:44:50 +0000782 if ((receive_len > 24) &&
Steve Frenchad009ac2005-04-28 22:41:05 -0700783 (ses->server->secMode & (SECMODE_SIGN_REQUIRED |
784 SECMODE_SIGN_ENABLED))) {
785 rc = cifs_verify_signature(out_buf,
Steve Frenchb609f062007-07-09 07:55:14 +0000786 &ses->server->mac_signing_key,
Steve Frenchad009ac2005-04-28 22:41:05 -0700787 midQ->sequence_number+1);
Steve French79a58d12007-07-06 22:44:50 +0000788 if (rc) {
789 cERROR(1, ("Unexpected SMB signature"));
Steve French275cde12005-04-28 22:41:10 -0700790 /* BB FIXME add code to kill session */
Steve Frenchad009ac2005-04-28 22:41:05 -0700791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 }
793
794 *pbytes_returned = out_buf->smb_buf_length;
795
Steve Frenchad009ac2005-04-28 22:41:05 -0700796 /* BB special case reconnect tid and uid here? */
Steve Frencha761ac52007-10-18 21:45:27 +0000797 rc = map_smb_to_linux_error(out_buf, 0 /* no log */ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
799 /* convert ByteCount if necessary */
Steve French26f57362007-08-30 22:09:15 +0000800 if (receive_len >= sizeof(struct smb_hdr) - 4
801 /* do not count RFC1001 header */ +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 (2 * out_buf->WordCount) + 2 /* bcc */ )
Steve French0f2b27c2005-11-16 14:25:50 -0800803 BCC(out_buf) = le16_to_cpu(BCC_LE(out_buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 } else {
805 rc = -EIO;
Steve French79a58d12007-07-06 22:44:50 +0000806 cERROR(1, ("Bad MID state?"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 }
808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000810out:
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000811 DeleteMidQEntry(midQ);
Steve French79a58d12007-07-06 22:44:50 +0000812 atomic_dec(&ses->server->inFlight);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000813 wake_up(&ses->server->request_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
815 return rc;
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000816}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000818/* Send an NT_CANCEL SMB to cause the POSIX blocking lock to return. */
819
820static int
821send_nt_cancel(struct cifsTconInfo *tcon, struct smb_hdr *in_buf,
822 struct mid_q_entry *midQ)
823{
824 int rc = 0;
825 struct cifsSesInfo *ses = tcon->ses;
826 __u16 mid = in_buf->Mid;
827
828 header_assemble(in_buf, SMB_COM_NT_CANCEL, tcon, 0);
829 in_buf->Mid = mid;
Steve French79a58d12007-07-06 22:44:50 +0000830 down(&ses->server->tcpSem);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000831 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
832 if (rc) {
833 up(&ses->server->tcpSem);
834 return rc;
835 }
836 rc = smb_send(ses->server->ssocket, in_buf, in_buf->smb_buf_length,
837 (struct sockaddr *) &(ses->server->addr.sockAddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 up(&ses->server->tcpSem);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000839 return rc;
840}
841
842/* We send a LOCKINGX_CANCEL_LOCK to cause the Windows
843 blocking lock to return. */
844
845static int
846send_lock_cancel(const unsigned int xid, struct cifsTconInfo *tcon,
847 struct smb_hdr *in_buf,
848 struct smb_hdr *out_buf)
849{
850 int bytes_returned;
851 struct cifsSesInfo *ses = tcon->ses;
852 LOCK_REQ *pSMB = (LOCK_REQ *)in_buf;
853
854 /* We just modify the current in_buf to change
855 the type of lock from LOCKING_ANDX_SHARED_LOCK
856 or LOCKING_ANDX_EXCLUSIVE_LOCK to
857 LOCKING_ANDX_CANCEL_LOCK. */
858
859 pSMB->LockType = LOCKING_ANDX_CANCEL_LOCK|LOCKING_ANDX_LARGE_FILES;
860 pSMB->Timeout = 0;
861 pSMB->hdr.Mid = GetNextMid(ses->server);
862
863 return SendReceive(xid, ses, in_buf, out_buf,
Steve French133672e2007-11-13 22:41:37 +0000864 &bytes_returned, CIFS_STD_OP);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000865}
866
867int
868SendReceiveBlockingLock(const unsigned int xid, struct cifsTconInfo *tcon,
869 struct smb_hdr *in_buf, struct smb_hdr *out_buf,
870 int *pbytes_returned)
871{
872 int rc = 0;
873 int rstart = 0;
874 unsigned int receive_len;
875 struct mid_q_entry *midQ;
876 struct cifsSesInfo *ses;
877
878 if (tcon == NULL || tcon->ses == NULL) {
Steve French79a58d12007-07-06 22:44:50 +0000879 cERROR(1, ("Null smb session"));
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000880 return -EIO;
881 }
882 ses = tcon->ses;
883
Steve French79a58d12007-07-06 22:44:50 +0000884 if (ses->server == NULL) {
885 cERROR(1, ("Null tcp session"));
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000886 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 }
888
Steve French79a58d12007-07-06 22:44:50 +0000889 if (ses->server->tcpStatus == CifsExiting)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000890 return -ENOENT;
891
Steve French79a58d12007-07-06 22:44:50 +0000892 /* Ensure that we do not send more than 50 overlapping requests
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000893 to the same server. We may make this configurable later or
894 use ses->maxReq */
895
Steve French133672e2007-11-13 22:41:37 +0000896 rc = wait_for_free_request(ses, CIFS_BLOCKING_OP);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000897 if (rc)
898 return rc;
899
Steve French79a58d12007-07-06 22:44:50 +0000900 /* make sure that we sign in the same order that we send on this socket
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000901 and avoid races inside tcp sendmsg code that could cause corruption
902 of smb data */
903
Steve French79a58d12007-07-06 22:44:50 +0000904 down(&ses->server->tcpSem);
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000905
906 rc = allocate_mid(ses, in_buf, &midQ);
907 if (rc) {
908 up(&ses->server->tcpSem);
909 return rc;
910 }
911
912 if (in_buf->smb_buf_length > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
913 up(&ses->server->tcpSem);
914 cERROR(1, ("Illegal length, greater than maximum frame, %d",
915 in_buf->smb_buf_length));
916 DeleteMidQEntry(midQ);
917 return -EIO;
918 }
919
920 rc = cifs_sign_smb(in_buf, ses->server, &midQ->sequence_number);
921
922 midQ->midState = MID_REQUEST_SUBMITTED;
923#ifdef CONFIG_CIFS_STATS2
924 atomic_inc(&ses->server->inSend);
925#endif
926 rc = smb_send(ses->server->ssocket, in_buf, in_buf->smb_buf_length,
927 (struct sockaddr *) &(ses->server->addr.sockAddr));
928#ifdef CONFIG_CIFS_STATS2
929 atomic_dec(&ses->server->inSend);
930 midQ->when_sent = jiffies;
931#endif
932 up(&ses->server->tcpSem);
933
Steve French79a58d12007-07-06 22:44:50 +0000934 if (rc < 0) {
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000935 DeleteMidQEntry(midQ);
936 return rc;
937 }
938
939 /* Wait for a reply - allow signals to interrupt. */
940 rc = wait_event_interruptible(ses->server->response_q,
Steve French79a58d12007-07-06 22:44:50 +0000941 (!(midQ->midState == MID_REQUEST_SUBMITTED)) ||
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000942 ((ses->server->tcpStatus != CifsGood) &&
943 (ses->server->tcpStatus != CifsNew)));
944
945 /* Were we interrupted by a signal ? */
946 if ((rc == -ERESTARTSYS) &&
947 (midQ->midState == MID_REQUEST_SUBMITTED) &&
948 ((ses->server->tcpStatus == CifsGood) ||
949 (ses->server->tcpStatus == CifsNew))) {
950
951 if (in_buf->Command == SMB_COM_TRANSACTION2) {
952 /* POSIX lock. We send a NT_CANCEL SMB to cause the
953 blocking lock to return. */
954
955 rc = send_nt_cancel(tcon, in_buf, midQ);
956 if (rc) {
957 DeleteMidQEntry(midQ);
958 return rc;
959 }
960 } else {
961 /* Windows lock. We send a LOCKINGX_CANCEL_LOCK
962 to cause the blocking lock to return. */
963
964 rc = send_lock_cancel(xid, tcon, in_buf, out_buf);
965
966 /* If we get -ENOLCK back the lock may have
967 already been removed. Don't exit in this case. */
968 if (rc && rc != -ENOLCK) {
969 DeleteMidQEntry(midQ);
970 return rc;
971 }
972 }
973
974 /* Wait 5 seconds for the response. */
Steve French79a58d12007-07-06 22:44:50 +0000975 if (wait_for_response(ses, midQ, 5 * HZ, 5 * HZ) == 0) {
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000976 /* We got the response - restart system call. */
977 rstart = 1;
978 }
979 }
980
981 spin_lock(&GlobalMid_Lock);
982 if (midQ->resp_buf) {
983 spin_unlock(&GlobalMid_Lock);
984 receive_len = midQ->resp_buf->smb_buf_length;
985 } else {
Steve French79a58d12007-07-06 22:44:50 +0000986 cERROR(1, ("No response for cmd %d mid %d",
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000987 midQ->command, midQ->mid));
Steve French79a58d12007-07-06 22:44:50 +0000988 if (midQ->midState == MID_REQUEST_SUBMITTED) {
989 if (ses->server->tcpStatus == CifsExiting)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000990 rc = -EHOSTDOWN;
991 else {
992 ses->server->tcpStatus = CifsNeedReconnect;
993 midQ->midState = MID_RETRY_NEEDED;
994 }
995 }
996
997 if (rc != -EHOSTDOWN) {
Steve French79a58d12007-07-06 22:44:50 +0000998 if (midQ->midState == MID_RETRY_NEEDED) {
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000999 rc = -EAGAIN;
Steve French79a58d12007-07-06 22:44:50 +00001000 cFYI(1, ("marking request for retry"));
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001001 } else {
1002 rc = -EIO;
1003 }
1004 }
1005 spin_unlock(&GlobalMid_Lock);
1006 DeleteMidQEntry(midQ);
1007 return rc;
1008 }
Steve French50c2f752007-07-13 00:33:32 +00001009
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001010 if (receive_len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE) {
1011 cERROR(1, ("Frame too large received. Length: %d Xid: %d",
1012 receive_len, xid));
1013 rc = -EIO;
1014 } else { /* rcvd frame is ok */
1015
1016 if (midQ->resp_buf && out_buf
1017 && (midQ->midState == MID_RESPONSE_RECEIVED)) {
1018 out_buf->smb_buf_length = receive_len;
1019 memcpy((char *)out_buf + 4,
1020 (char *)midQ->resp_buf + 4,
1021 receive_len);
1022
1023 dump_smb(out_buf, 92);
1024 /* convert the length into a more usable form */
Steve French79a58d12007-07-06 22:44:50 +00001025 if ((receive_len > 24) &&
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001026 (ses->server->secMode & (SECMODE_SIGN_REQUIRED |
1027 SECMODE_SIGN_ENABLED))) {
1028 rc = cifs_verify_signature(out_buf,
Steve Frenchb609f062007-07-09 07:55:14 +00001029 &ses->server->mac_signing_key,
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001030 midQ->sequence_number+1);
Steve French79a58d12007-07-06 22:44:50 +00001031 if (rc) {
1032 cERROR(1, ("Unexpected SMB signature"));
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001033 /* BB FIXME add code to kill session */
1034 }
1035 }
1036
1037 *pbytes_returned = out_buf->smb_buf_length;
1038
1039 /* BB special case reconnect tid and uid here? */
Steve Frencha761ac52007-10-18 21:45:27 +00001040 rc = map_smb_to_linux_error(out_buf, 0 /* no log */ );
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001041
1042 /* convert ByteCount if necessary */
Steve French26f57362007-08-30 22:09:15 +00001043 if (receive_len >= sizeof(struct smb_hdr) - 4
1044 /* do not count RFC1001 header */ +
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001045 (2 * out_buf->WordCount) + 2 /* bcc */ )
1046 BCC(out_buf) = le16_to_cpu(BCC_LE(out_buf));
1047 } else {
1048 rc = -EIO;
Steve French79a58d12007-07-06 22:44:50 +00001049 cERROR(1, ("Bad MID state?"));
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001050 }
1051 }
1052 DeleteMidQEntry(midQ);
1053 if (rstart && rc == -EACCES)
1054 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 return rc;
1056}